Skip to main content

compio_driver/sys/op/fs/
poll.rs

1use rustix::fs;
2
3use crate::{Decision, PollOpCode as OpCode, sys::op::*};
4
5/// Get metadata of an opened file.
6pub struct FileStat<S> {
7    pub(crate) fd: S,
8    pub(crate) stat: FileAttr,
9}
10
11/// Get metadata from path.
12pub struct PathStat<S: AsFd> {
13    pub(crate) dirfd: S,
14    pub(crate) path: CString,
15    pub(crate) stat: FileAttr,
16    pub(crate) follow_symlink: bool,
17}
18
19unsafe impl<S: AsFd> OpCode for Sync<S> {
20    type Control = AioControl;
21
22    unsafe fn init(&mut self, ctrl: &mut Self::Control) {
23        ctrl.init_fd(&self.fd);
24    }
25
26    fn pre_submit(&mut self, ctrl: &mut Self::Control) -> io::Result<Decision> {
27        ctrl.decide_sync(self.datasync)
28    }
29
30    fn op_type(&mut self, ctrl: &mut Self::Control) -> Option<crate::OpType> {
31        ctrl.op_type()
32    }
33
34    fn operate(&mut self, _: &mut Self::Control) -> Poll<io::Result<usize>> {
35        #[cfg(datasync)]
36        if self.datasync {
37            fs::fdatasync(self.fd.as_fd())?
38        } else {
39            fs::fsync(self.fd.as_fd())?
40        }
41
42        #[cfg(not(datasync))]
43        fs::fsync(self.fd.as_fd())?;
44
45        Poll::Ready(Ok(0))
46    }
47}
48
49unsafe impl<S: AsFd> OpCode for Unlink<S> {
50    type Control = ();
51
52    fn pre_submit(&mut self, _: &mut Self::Control) -> io::Result<Decision> {
53        Ok(Decision::Blocking)
54    }
55
56    fn operate(&mut self, control: &mut Self::Control) -> Poll<io::Result<usize>> {
57        Poll::Ready(self.call(control))
58    }
59}
60
61unsafe impl<S: AsFd> OpCode for CreateDir<S> {
62    type Control = ();
63
64    fn pre_submit(&mut self, _: &mut Self::Control) -> io::Result<Decision> {
65        Ok(Decision::Blocking)
66    }
67
68    fn operate(&mut self, control: &mut Self::Control) -> Poll<io::Result<usize>> {
69        Poll::Ready(self.call(control))
70    }
71}
72
73unsafe impl<S1: AsFd, S2: AsFd> OpCode for Rename<S1, S2> {
74    type Control = ();
75
76    fn pre_submit(&mut self, _: &mut Self::Control) -> io::Result<Decision> {
77        Ok(Decision::Blocking)
78    }
79
80    fn operate(&mut self, control: &mut Self::Control) -> Poll<io::Result<usize>> {
81        Poll::Ready(self.call(control))
82    }
83}
84
85unsafe impl<S: AsFd> OpCode for Symlink<S> {
86    type Control = ();
87
88    fn pre_submit(&mut self, _: &mut Self::Control) -> io::Result<Decision> {
89        Ok(Decision::Blocking)
90    }
91
92    fn operate(&mut self, control: &mut Self::Control) -> Poll<io::Result<usize>> {
93        Poll::Ready(self.call(control))
94    }
95}
96
97unsafe impl<S1: AsFd, S2: AsFd> OpCode for HardLink<S1, S2> {
98    type Control = ();
99
100    fn pre_submit(&mut self, _: &mut Self::Control) -> io::Result<Decision> {
101        Ok(Decision::Blocking)
102    }
103
104    fn operate(&mut self, control: &mut Self::Control) -> Poll<io::Result<usize>> {
105        Poll::Ready(self.call(control))
106    }
107}
108
109unsafe impl<S: AsFd> OpCode for OpenFile<S> {
110    type Control = ();
111
112    fn pre_submit(&mut self, _: &mut Self::Control) -> io::Result<Decision> {
113        Ok(Decision::Blocking)
114    }
115
116    fn operate(&mut self, control: &mut Self::Control) -> Poll<io::Result<usize>> {
117        Poll::Ready(self.call(control))
118    }
119}
120
121unsafe impl OpCode for CloseFile {
122    type Control = ();
123
124    fn pre_submit(&mut self, _: &mut Self::Control) -> io::Result<Decision> {
125        Ok(Decision::Blocking)
126    }
127
128    fn operate(&mut self, control: &mut Self::Control) -> Poll<io::Result<usize>> {
129        Poll::Ready(self.call(control))
130    }
131}
132
133#[cfg(linux_all)]
134unsafe impl<T: IoBufMut> OpCode for GetXattr<T> {
135    type Control = ();
136
137    fn pre_submit(&mut self, _: &mut Self::Control) -> io::Result<Decision> {
138        Ok(Decision::Blocking)
139    }
140
141    fn operate(&mut self, control: &mut Self::Control) -> Poll<io::Result<usize>> {
142        Poll::Ready(self.call(control))
143    }
144}
145
146#[cfg(linux_all)]
147unsafe impl<S: AsFd, T: IoBufMut> OpCode for FGetXattr<S, T> {
148    type Control = ();
149
150    fn pre_submit(&mut self, _: &mut Self::Control) -> io::Result<Decision> {
151        Ok(Decision::Blocking)
152    }
153
154    fn operate(&mut self, control: &mut Self::Control) -> Poll<io::Result<usize>> {
155        Poll::Ready(self.call(control))
156    }
157}
158
159unsafe impl<S: AsFd> OpCode for TruncateFile<S> {
160    type Control = ();
161
162    fn pre_submit(&mut self, _: &mut Self::Control) -> io::Result<Decision> {
163        Ok(Decision::Blocking)
164    }
165
166    fn operate(&mut self, _: &mut Self::Control) -> Poll<io::Result<usize>> {
167        Poll::Ready(self.call())
168    }
169}
170
171impl<S> FileStat<S> {
172    /// Create [`FileStat`].
173    pub fn new(fd: S) -> Self {
174        Self {
175            fd,
176            stat: unsafe { std::mem::zeroed() },
177        }
178    }
179}
180
181impl<S> IntoInner for FileStat<S> {
182    type Inner = FileAttr;
183
184    fn into_inner(self) -> Self::Inner {
185        self.stat
186    }
187}
188
189unsafe impl<S: AsFd> OpCode for FileStat<S> {
190    type Control = ();
191
192    fn pre_submit(&mut self, _: &mut Self::Control) -> io::Result<Decision> {
193        Ok(Decision::Blocking)
194    }
195
196    fn operate(&mut self, _: &mut Self::Control) -> Poll<io::Result<usize>> {
197        self.stat = stat(self.fd.as_fd(), c"", false)?;
198
199        Poll::Ready(Ok(0))
200    }
201}
202
203impl<S: AsFd> PathStat<S> {
204    /// Create [`PathStat`].
205    pub fn new(dirfd: S, path: CString, follow_symlink: bool) -> Self {
206        Self {
207            dirfd,
208            path,
209            stat: unsafe { std::mem::zeroed() },
210            follow_symlink,
211        }
212    }
213}
214
215impl<S: AsFd> IntoInner for PathStat<S> {
216    type Inner = FileAttr;
217
218    fn into_inner(self) -> Self::Inner {
219        self.stat
220    }
221}
222
223unsafe impl<S: AsFd> OpCode for PathStat<S> {
224    type Control = ();
225
226    fn pre_submit(&mut self, _: &mut Self::Control) -> io::Result<Decision> {
227        Ok(Decision::Blocking)
228    }
229
230    fn operate(&mut self, _: &mut Self::Control) -> Poll<io::Result<usize>> {
231        self.stat = stat(self.dirfd.as_fd(), &self.path, self.follow_symlink)?;
232        Poll::Ready(Ok(0))
233    }
234}
235
236#[cfg(linux_all)]
237unsafe impl<S1: AsFd, S2: AsFd> OpCode for Splice<S1, S2> {
238    type Control = ();
239
240    fn pre_submit(&mut self, _: &mut Self::Control) -> io::Result<Decision> {
241        use crate::sys::WaitArg;
242
243        Ok(Decision::wait_for_many([
244            WaitArg::readable(self.fd_in.as_fd().as_raw_fd()),
245            WaitArg::writable(self.fd_out.as_fd().as_raw_fd()),
246        ]))
247    }
248
249    fn op_type(&mut self, _: &mut Self::Control) -> Option<crate::OpType> {
250        Some(crate::OpType::multi_fd([
251            self.fd_in.as_fd().as_raw_fd(),
252            self.fd_out.as_fd().as_raw_fd(),
253        ]))
254    }
255
256    fn operate(&mut self, control: &mut Self::Control) -> Poll<io::Result<usize>> {
257        poll_io(|| self.call(control))
258    }
259}