Skip to main content

compio_driver/sys/op/fs/
mod.rs

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