Skip to main content

compio_driver/sys/op/fs/
mod.rs

1cfg_if::cfg_if! {
2    if #[cfg(windows)] {
3        mod iocp;
4    } else if #[cfg(fusion)] {
5        mod iour;
6        mod poll;
7        mod_use![fusion];
8    } else if #[cfg(io_uring)] {
9        mod_use![iour];
10    } else if #[cfg(polling)] {
11        mod_use![poll];
12    } else if #[cfg(stub)] {
13        mod_use![stub];
14    }
15}
16
17use crate::sys::prelude::*;
18
19/// Close the file fd.
20pub struct CloseFile {
21    pub(crate) fd: ManuallyDrop<OwnedFd>,
22}
23
24impl CloseFile {
25    /// Create [`CloseFile`].
26    pub fn new(fd: OwnedFd) -> Self {
27        Self {
28            fd: ManuallyDrop::new(fd),
29        }
30    }
31}
32
33/// Sync data to the disk.
34pub struct Sync<S> {
35    pub(crate) fd: S,
36    #[allow(dead_code)]
37    pub(crate) datasync: bool,
38}
39
40impl<S> Sync<S> {
41    /// Create [`Sync`].
42    ///
43    /// If `datasync` is `true`, the file metadata may not be synchronized.
44    pub fn new(fd: S, datasync: bool) -> Self {
45        Self { fd, datasync }
46    }
47}
48
49/// Splice data between two file descriptors.
50#[cfg(linux_all)]
51pub struct Splice<S1, S2> {
52    pub(crate) fd_in: S1,
53    pub(crate) offset_in: i64,
54    pub(crate) fd_out: S2,
55    pub(crate) offset_out: i64,
56    pub(crate) len: usize,
57    pub(crate) flags: rustix::pipe::SpliceFlags,
58}
59
60#[cfg(linux_all)]
61impl<S1, S2> Splice<S1, S2> {
62    /// Create [`Splice`].
63    ///
64    /// `offset_in` and `offset_out` specify the offset to read from and write
65    /// to. Use `-1` for pipe ends or to use/update the current file
66    /// position.
67    pub fn new(
68        fd_in: S1,
69        offset_in: i64,
70        fd_out: S2,
71        offset_out: i64,
72        len: usize,
73        flags: rustix::pipe::SpliceFlags,
74    ) -> Self {
75        Self {
76            fd_in,
77            offset_in,
78            fd_out,
79            offset_out,
80            len,
81            flags,
82        }
83    }
84
85    pub(crate) fn call(&self, _: &mut ()) -> io::Result<usize>
86    where
87        S1: AsFd,
88        S2: AsFd,
89    {
90        let off_in = self.offset_in;
91        let off_out = self.offset_out;
92
93        rustix::pipe::splice(
94            &self.fd_in,
95            (off_in >= 0).then_some(&mut (off_in as u64)),
96            &self.fd_out,
97            (off_out >= 0).then_some(&mut (off_out as u64)),
98            self.len,
99            self.flags,
100        )
101        .map_err(Into::into)
102    }
103}
104
105#[cfg(linux_all)]
106impl<S1, S2> IntoInner for Splice<S1, S2> {
107    type Inner = (S1, S2);
108
109    fn into_inner(self) -> Self::Inner {
110        (self.fd_in, self.fd_out)
111    }
112}