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: Stat,
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: Stat,
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
133unsafe impl<S: AsFd> OpCode for TruncateFile<S> {
134    type Control = ();
135
136    fn pre_submit(&mut self, _: &mut Self::Control) -> io::Result<Decision> {
137        Ok(Decision::Blocking)
138    }
139
140    fn operate(&mut self, _: &mut Self::Control) -> Poll<io::Result<usize>> {
141        Poll::Ready(self.call())
142    }
143}
144
145impl<S> FileStat<S> {
146    /// Create [`FileStat`].
147    pub fn new(fd: S) -> Self {
148        Self {
149            fd,
150            stat: unsafe { std::mem::zeroed() },
151        }
152    }
153}
154
155impl<S> IntoInner for FileStat<S> {
156    type Inner = Stat;
157
158    fn into_inner(self) -> Self::Inner {
159        self.stat
160    }
161}
162
163unsafe impl<S: AsFd> OpCode for FileStat<S> {
164    type Control = ();
165
166    fn pre_submit(&mut self, _: &mut Self::Control) -> io::Result<Decision> {
167        Ok(Decision::Blocking)
168    }
169
170    fn operate(&mut self, _: &mut Self::Control) -> Poll<io::Result<usize>> {
171        self.stat = stat(self.fd.as_fd(), c"", false)?;
172
173        Poll::Ready(Ok(0))
174    }
175}
176
177impl<S: AsFd> PathStat<S> {
178    /// Create [`PathStat`].
179    pub fn new(dirfd: S, path: CString, follow_symlink: bool) -> Self {
180        Self {
181            dirfd,
182            path,
183            stat: unsafe { std::mem::zeroed() },
184            follow_symlink,
185        }
186    }
187}
188
189impl<S: AsFd> IntoInner for PathStat<S> {
190    type Inner = Stat;
191
192    fn into_inner(self) -> Self::Inner {
193        self.stat
194    }
195}
196
197unsafe impl<S: AsFd> OpCode for PathStat<S> {
198    type Control = ();
199
200    fn pre_submit(&mut self, _: &mut Self::Control) -> io::Result<Decision> {
201        Ok(Decision::Blocking)
202    }
203
204    fn operate(&mut self, _: &mut Self::Control) -> Poll<io::Result<usize>> {
205        self.stat = stat(self.dirfd.as_fd(), &self.path, self.follow_symlink)?;
206        Poll::Ready(Ok(0))
207    }
208}
209
210#[cfg(linux_all)]
211unsafe impl<S1: AsFd, S2: AsFd> OpCode for Splice<S1, S2> {
212    type Control = ();
213
214    fn pre_submit(&mut self, _: &mut Self::Control) -> io::Result<Decision> {
215        use crate::sys::WaitArg;
216
217        Ok(Decision::wait_for_many([
218            WaitArg::readable(self.fd_in.as_fd().as_raw_fd()),
219            WaitArg::writable(self.fd_out.as_fd().as_raw_fd()),
220        ]))
221    }
222
223    fn op_type(&mut self, _: &mut Self::Control) -> Option<crate::OpType> {
224        Some(crate::OpType::multi_fd([
225            self.fd_in.as_fd().as_raw_fd(),
226            self.fd_out.as_fd().as_raw_fd(),
227        ]))
228    }
229
230    fn operate(&mut self, control: &mut Self::Control) -> Poll<io::Result<usize>> {
231        poll_io(|| self.call(control))
232    }
233}