Skip to main content

dope_sys/io/fd/
mod.rs

1use std::os::fd::{BorrowedFd, OwnedFd};
2
3use crate::{Errno, kernel::errno};
4
5pub mod raw;
6
7pub trait Flags: Sized {
8    const READ: libc::c_int;
9    const WRITE: libc::c_int;
10    const BIT: libc::c_int;
11    fn failure<const SET: bool>(error: Errno) -> Self;
12}
13
14/// Native descriptor operations using the calling thread's exact error slot.
15pub trait Fd: errno::Source {
16    fn try_limit() -> Result<(libc::rlim_t, libc::rlim_t), Errno> {
17        raw::try_limit::<Self>()
18    }
19
20    /// Adds a descriptor flag, retrying only each interrupted native attempt.
21    fn update<E: Flags>(file: BorrowedFd<'_>) -> Result<(), E> {
22        update::<Self, E, true, true>(file)
23    }
24
25    /// Removes one descriptor flag, preserving all other native flags.
26    fn remove<E: Flags>(file: BorrowedFd<'_>) -> Result<(), E> {
27        update::<Self, E, true, false>(file)
28    }
29
30    /// Adds a descriptor flag with one get attempt followed by one set attempt.
31    fn try_update<E: Flags>(file: BorrowedFd<'_>) -> Result<(), E> {
32        update::<Self, E, false, true>(file)
33    }
34
35    /// Consumes descriptor ownership through one native close attempt.
36    /// The returned errno is observable; ownership is never issued for a retry.
37    fn close<E: From<Errno>>(file: OwnedFd) -> Result<(), E> {
38        raw::close::<Self, E>(file)
39    }
40
41    fn duplicate<E: From<Errno>>(file: BorrowedFd<'_>) -> Result<OwnedFd, E> {
42        raw::duplicate::<Self, E>(file)
43    }
44
45    fn set<const COMMAND: libc::c_int, E: From<Errno>>(
46        file: BorrowedFd<'_>,
47        flags: libc::c_int,
48    ) -> Result<(), E> {
49        set::<Self, E, COMMAND, false>(file, flags)
50    }
51
52    /// Applies scalar descriptor flags, retrying only interrupted attempts.
53    fn set_retry<const COMMAND: libc::c_int, E: From<Errno>>(
54        file: BorrowedFd<'_>,
55        flags: libc::c_int,
56    ) -> Result<(), E> {
57        set::<Self, E, COMMAND, true>(file, flags)
58    }
59    fn read<'a, E: From<Errno>>(
60        file: BorrowedFd<'_>,
61        output: &'a mut [u8],
62    ) -> Result<&'a mut [u8], E> {
63        raw::read::<Self, E>(file, output)
64    }
65
66    fn write<'a, E: From<Errno>>(file: BorrowedFd<'_>, input: &'a [u8]) -> Result<&'a [u8], E> {
67        raw::try_write::<Self, E>(file, input)
68    }
69}
70
71impl<S: errno::Source> Fd for S {}
72
73fn update<S: Fd + ?Sized, E: Flags, const RETRY: bool, const ENABLED: bool>(
74    file: BorrowedFd<'_>,
75) -> Result<(), E> {
76    let flags = raw::flags::<S, E, false, RETRY>(file, 0).map_err(E::failure::<false>)?;
77    let flags = if ENABLED {
78        flags | E::BIT
79    } else {
80        flags & !E::BIT
81    };
82    raw::flags::<S, E, true, RETRY>(file, flags).map_err(E::failure::<true>)?;
83    Ok(())
84}
85
86fn set<S: Fd + ?Sized, E: From<Errno>, const COMMAND: libc::c_int, const RETRY: bool>(
87    file: BorrowedFd<'_>,
88    flags: libc::c_int,
89) -> Result<(), E> {
90    const { assert!(COMMAND == libc::F_SETFD || COMMAND == libc::F_SETFL) };
91    if COMMAND == libc::F_SETFD {
92        raw::flags::<S, crate::Cloexec, true, RETRY>(file, flags)?;
93    } else {
94        raw::flags::<S, crate::Nonblock, true, RETRY>(file, flags)?;
95    }
96    Ok(())
97}