dope-sys 0.77.0

Typed operating-system call contracts
Documentation
use std::{
    mem::MaybeUninit,
    os::fd::{BorrowedFd, OwnedFd},
};

use o3::{
    alloc,
    base::num::bounded::{self, U64},
    buffer::bytes::{self, bounded::MAX},
};

use crate::{
    Errno,
    io::request::Request,
    kernel::{self, errno},
};

mod kind;
pub mod metadata;
mod mode;
mod open;
pub mod raw;
mod sync;
pub mod watch;

pub use kind::Kind;
pub use mode::Mode;
pub use open::Open;
pub use sync::Sync;

use metadata::Metadata;

const _: () = assert!(libc::off_t::MAX as u64 == i64::MAX as u64);

/// An absolute file position, excluding native current-position sentinels.
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
#[repr(transparent)]
pub struct Offset(U64<0, { i64::MAX as u64 }>);

impl Offset {
    pub const fn new(value: u64) -> Option<Self> {
        match U64::new(value) {
            Some(value) => Some(Self(value)),
            None => None,
        }
    }

    pub const fn clamp(value: u64) -> Self {
        Self(U64::clamp(value))
    }

    pub const fn get(self) -> u64 {
        self.0.get()
    }

    /// Subtracts a byte count while retaining the admitted absolute range.
    pub const fn checked_sub(self, value: u64) -> Option<Self> {
        match self.0.checked_sub(value) {
            Some(value) => Some(Self(value)),
            None => None,
        }
    }

    pub fn read<'a, S: kernel::raw::Source + ?Sized, E: From<Errno>>(
        self,
        file: BorrowedFd<'_>,
        output: &'a mut [MaybeUninit<u8>],
    ) -> Result<&'a mut [u8], E> {
        raw::read::<S, E>(file, output, self)
    }
}

/// A native directory stream whose descriptor and buffered entries have one owner.
#[repr(transparent)]
pub struct Dir<S: raw::Directory>(S::Stream);

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum OpenError<E> {
    Syscall(E),
    Allocation(alloc::Exhausted),
}

impl<S: File> Dir<S> {
    pub fn open<E: From<Errno>>(path: &std::ffi::CStr) -> Result<Self, OpenError<E>> {
        S::open_dir(path).map(Self)
    }

    pub fn from_fd<E: From<Errno>>(file: OwnedFd) -> Result<Self, OpenError<E>> {
        S::from_fd(file).map(Self)
    }

    pub fn entries(&mut self) -> (BorrowedFd<'_>, Entries<'_, S>) {
        raw::entries::<S>(&mut self.0)
    }
}

#[repr(transparent)]
pub struct Entries<'a, S: raw::Directory>(&'a mut S::Stream);

/// One name and the file kind already supplied by its native directory record.
#[derive(Clone, Copy, Debug)]
pub struct Entry<'a> {
    pub name: &'a std::ffi::CStr,
    pub kind: Option<Kind>,
}

impl<S: raw::Directory> Entries<'_, S> {
    /// The next read requires the previous name's borrow to end.
    pub fn try_next(&mut self) -> Result<Option<Entry<'_>>, Errno> {
        S::next(self.0)
    }
}

/// Selects native file requests while preserving every caller-owned borrow.
pub trait File: errno::Source + raw::Directory {
    type Options;
    type Metadata;
    type Request<'a>: Request<
            'a,
            Options = Self::Options,
            Metadata = Self::Metadata,
            Buffer = bytes::Prefix<'a, MAX>,
        >
    where
        Self: 'a;

    fn open<E: From<Errno>>(path: &std::ffi::CStr, flags: Open, mode: Mode) -> Result<OwnedFd, E> {
        raw::open::<Self, E>(None, path, flags, mode)
    }

    fn metadata<E: From<Errno>>(file: BorrowedFd<'_>) -> Result<Metadata, E> {
        raw::metadata::<Self, E>(file)
    }

    fn stat<E: From<Errno>>(path: &std::ffi::CStr) -> Result<Metadata, E> {
        raw::stat::<Self, E, false>(None, path)
    }

    fn mkdir<E: From<Errno>>(path: &std::ffi::CStr, mode: Mode) -> Result<(), E> {
        raw::mkdir::<Self, E>(path, mode)
    }

    /// Classifies the named entry itself while retaining its parent directory.
    fn metadata_at<E: From<Errno>>(
        dir: Option<BorrowedFd<'_>>,
        name: &std::ffi::CStr,
    ) -> Result<Metadata, E> {
        raw::stat::<Self, E, true>(dir, name)
    }

    fn unlink<E: From<Errno>>(path: &std::ffi::CStr) -> Result<(), E> {
        raw::unlink_at::<Self, E>(None, path, 0)
    }

    fn rename<E: From<Errno>>(from: &std::ffi::CStr, to: &std::ffi::CStr) -> Result<(), E> {
        raw::rename::<Self, E>(None, from, to)
    }

    /// Attempts an exclusive advisory lock without waiting for its current owner.
    fn try_lock<E: From<Errno>>(file: BorrowedFd<'_>) -> Result<(), E> {
        raw::try_lock::<Self, E>(file)
    }

    fn sync<E: From<Errno>>(file: BorrowedFd<'_>) -> Result<(), E> {
        raw::sync::<Self, E>(file)
    }

    fn truncate<E: From<Errno>>(file: BorrowedFd<'_>, len: i64) -> Result<(), E> {
        raw::truncate::<Self, E>(file, len)
    }

    fn unlink_at<E: From<Errno>>(dir: BorrowedFd<'_>, name: &std::ffi::CStr) -> Result<(), E> {
        raw::unlink_at::<Self, E>(Some(dir), name, 0)
    }

    fn rmdir_at<E: From<Errno>>(
        dir: Option<BorrowedFd<'_>>,
        name: &std::ffi::CStr,
    ) -> Result<(), E> {
        raw::unlink_at::<Self, E>(dir, name, libc::AT_REMOVEDIR)
    }

    fn open_at<E: From<Errno>>(
        dir: BorrowedFd<'_>,
        name: &std::ffi::CStr,
        flags: Open,
        mode: Mode,
    ) -> Result<OwnedFd, E> {
        raw::open::<Self, E>(Some(dir), name, flags, mode)
    }

    fn rename_at<E: From<Errno>>(
        dir: BorrowedFd<'_>,
        old: &std::ffi::CStr,
        new: &std::ffi::CStr,
    ) -> Result<(), E> {
        raw::rename::<Self, E>(Some(dir), old, new)
    }

    /// Reads one prefix without changing the descriptor's current position.
    fn read_at<'a, E: From<Errno>>(
        file: BorrowedFd<'_>,
        output: &'a mut [u8],
        offset: Offset,
    ) -> Result<&'a mut [u8], E> {
        raw::read_at::<Self, E>(file, output, offset)
    }

    fn write_at<E: From<Errno>>(
        file: BorrowedFd<'_>,
        input: bytes::Prefix<'_, MAX>,
        offset: Offset,
    ) -> Result<bounded::raw::U32<0, { MAX as u32 }>, E> {
        raw::write_at::<Self, E>(file, input, offset)
    }
}