Skip to main content

dope_sys/io/file/
raw.rs

1use std::os::fd;
2
3use crate::{
4    Errno,
5    io::file::{self, Opened},
6    kernel,
7};
8
9/// # Safety
10/// The live descriptor remains unchanged until stream drop.
11/// Returned names retain exclusive storage until their borrows end.
12pub unsafe trait Directory: kernel::raw::Source {
13    type Stream;
14
15    fn open_dir<E: From<Errno>>(path: &std::ffi::CStr) -> Result<Self::Stream, file::OpenError<E>>
16    where
17        Self: file::File,
18    {
19        let file = Self::open(
20            path,
21            file::Open::DIRECTORY | file::Open::CLOSE_ON_EXEC,
22            file::Mode::new(0),
23        )
24        .map_err(file::OpenError::Syscall)?;
25        Self::from_fd(file)
26    }
27
28    fn from_fd<E: From<Errno>>(file: fd::OwnedFd) -> Result<Self::Stream, file::OpenError<E>>;
29    fn descriptor(stream: &Self::Stream) -> kernel::raw::Descriptor;
30    fn next<E: From<Errno>>(
31        stream: &mut Self::Stream,
32    ) -> Result<Option<file::Entry<'_>>, file::ReadError<E>>;
33}
34
35/// # Safety
36/// Each decoded success retains the uniquely transferred descriptor until
37/// delivery or reclamation. Closing consumes that ownership.
38pub unsafe trait Completion {
39    type Outcome;
40    type Invalid;
41
42    /// # Safety
43    /// A successful outcome transfers one live descriptor from a completed
44    /// open operation. Its descriptor has not been decoded or reclaimed before.
45    unsafe fn open(outcome: Self::Outcome) -> Result<Opened, Self::Invalid>;
46
47    fn close(file: fd::OwnedFd) -> Result<(), Errno>;
48}