dope-core 0.76.0

Owned native I/O and completion driver for dope
Documentation
use std::{
    mem::{align_of, size_of},
    os::fd,
    path::Path,
    ptr,
};

use dope_sys::io::file::{File as _, Mode, Open};
use o3::{alloc, mem, mem::rc};

use crate::{
    Backend,
    io::fs::{open, path},
};

pub struct Dir {
    inner: mem::raw::Owner<Header>,
}

impl Dir {
    pub fn open(path: impl AsRef<Path>) -> Result<Self, open::Error> {
        use open::Error;
        use path::CStrExt as _;

        let path = Box::try_copy(path.as_ref())?;
        let file = Backend::open::<dope_sys::Errno>(
            &path,
            Open::DIRECTORY | Open::CLOSE_ON_EXEC,
            Mode::new(0),
        )
        .map_err(Error::Directory)?;
        Self::try_new(file).map_err(|error| Error::Allocation(alloc::Error::from(error)))
    }

    pub(in crate::io::fs) fn try_new(file: fd::OwnedFd) -> Result<Self, alloc::Exhausted> {
        Ok(Self {
            inner: Header::try_new(file)?,
        })
    }

    pub fn confine_name(
        &self,
        name: path::Component<'_>,
    ) -> Result<path::Confined, path::ConfineError> {
        use path::ConfineError;

        let path = name.try_to_owned().map_err(ConfineError::Allocation)?;
        self.confine(path).map_err(ConfineError::Retain)
    }

    pub fn confine(&self, path: path::Owned) -> Result<path::Confined, rc::Error> {
        Ok(path::Confined::new(self.try_retain()?, path))
    }

    pub(super) fn try_retain(&self) -> Result<Self, rc::Error> {
        Ok(Self {
            inner: self.inner.try_clone()?,
        })
    }

    pub fn entry<'a>(&'a self, name: &'a path::Owned) -> path::Entry<'a> {
        path::Entry::new(self, name)
    }

    pub fn rename<'a>(&'a self, old: &'a path::Owned, new: &'a path::Owned) -> path::Rename<'a> {
        path::Rename::new(self, old, new)
    }
}

impl fd::AsFd for Dir {
    fn as_fd(&self) -> fd::BorrowedFd<'_> {
        self.inner.get().file.as_fd()
    }
}

const _: () = {
    assert!(size_of::<Dir>() == size_of::<usize>());
    assert!(size_of::<Option<Dir>>() == size_of::<usize>());
    assert!(align_of::<Dir>() == align_of::<usize>());
};

struct Header {
    count: mem::raw::Count,
    file: fd::OwnedFd,
}

impl Header {
    fn try_new(file: fd::OwnedFd) -> Result<mem::raw::Owner<Self>, alloc::Exhausted> {
        let header: Box<Self> = alloc::BoxExt::try_new(Self {
            count: mem::raw::Count::one(),
            file,
        })?;
        // SAFETY: the allocation transfers its one initialized counted edge.
        Ok(unsafe { mem::raw::Owner::from_raw(ptr::NonNull::from(Box::leak(header))) })
    }
}

// SAFETY: every owner refers to this header's stable, private count.
unsafe impl mem::raw::Edge for Header {
    fn count(&self) -> &mem::raw::Count {
        &self.count
    }
}

// SAFETY: the last edge transfers the original Box and its owned descriptor.
unsafe impl mem::raw::Target for Header {
    unsafe fn destroy(pointer: ptr::NonNull<Self>) {
        // SAFETY: the transferred `Box` frees this exact header and its owned descriptor.
        unsafe { drop(Box::from_raw(pointer.as_ptr())) };
    }
}