Skip to main content

dope_sys/io/file/
mod.rs

1use std::{
2    mem::MaybeUninit,
3    os::fd::{BorrowedFd, OwnedFd},
4};
5
6use o3::{
7    alloc,
8    base::num::bounded::{self, U64},
9    buffer::bytes::{self, bounded::MAX},
10};
11
12use crate::{
13    Errno,
14    io::request::Request,
15    kernel::{self, errno},
16};
17
18mod kind;
19pub mod metadata;
20mod mode;
21mod open;
22pub mod raw;
23mod sync;
24pub mod watch;
25
26pub use kind::Kind;
27pub use mode::Mode;
28pub use open::Open;
29pub use sync::Sync;
30
31use metadata::Metadata;
32
33const _: () = assert!(libc::off_t::MAX as u64 == i64::MAX as u64);
34
35/// An absolute file position, excluding native current-position sentinels.
36#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
37#[repr(transparent)]
38pub struct Offset(U64<0, { i64::MAX as u64 }>);
39
40impl Offset {
41    pub const fn new(value: u64) -> Option<Self> {
42        match U64::new(value) {
43            Some(value) => Some(Self(value)),
44            None => None,
45        }
46    }
47
48    pub const fn clamp(value: u64) -> Self {
49        Self(U64::clamp(value))
50    }
51
52    pub const fn get(self) -> u64 {
53        self.0.get()
54    }
55
56    /// Subtracts a byte count while retaining the admitted absolute range.
57    pub const fn checked_sub(self, value: u64) -> Option<Self> {
58        match self.0.checked_sub(value) {
59            Some(value) => Some(Self(value)),
60            None => None,
61        }
62    }
63
64    pub fn read<'a, S: kernel::raw::Source + ?Sized, E: From<Errno>>(
65        self,
66        file: BorrowedFd<'_>,
67        output: &'a mut [MaybeUninit<u8>],
68    ) -> Result<&'a mut [u8], E> {
69        raw::read::<S, E>(file, output, self)
70    }
71}
72
73/// A native directory stream whose descriptor and buffered entries have one owner.
74#[repr(transparent)]
75pub struct Dir<S: raw::Directory>(S::Stream);
76
77#[derive(Clone, Copy, Debug, PartialEq, Eq)]
78pub enum OpenError<E> {
79    Syscall(E),
80    Allocation(alloc::Exhausted),
81}
82
83impl<S: File> Dir<S> {
84    pub fn open<E: From<Errno>>(path: &std::ffi::CStr) -> Result<Self, OpenError<E>> {
85        S::open_dir(path).map(Self)
86    }
87
88    pub fn from_fd<E: From<Errno>>(file: OwnedFd) -> Result<Self, OpenError<E>> {
89        S::from_fd(file).map(Self)
90    }
91
92    pub fn entries(&mut self) -> (BorrowedFd<'_>, Entries<'_, S>) {
93        raw::entries::<S>(&mut self.0)
94    }
95}
96
97#[repr(transparent)]
98pub struct Entries<'a, S: raw::Directory>(&'a mut S::Stream);
99
100/// One name and the file kind already supplied by its native directory record.
101#[derive(Clone, Copy, Debug)]
102pub struct Entry<'a> {
103    pub name: &'a std::ffi::CStr,
104    pub kind: Option<Kind>,
105}
106
107impl<S: raw::Directory> Entries<'_, S> {
108    /// The next read requires the previous name's borrow to end.
109    pub fn try_next(&mut self) -> Result<Option<Entry<'_>>, Errno> {
110        S::next(self.0)
111    }
112}
113
114/// Selects native file requests while preserving every caller-owned borrow.
115pub trait File: errno::Source + raw::Directory {
116    type Options;
117    type Metadata;
118    type Request<'a>: Request<
119            'a,
120            Options = Self::Options,
121            Metadata = Self::Metadata,
122            Buffer = bytes::Prefix<'a, MAX>,
123        >
124    where
125        Self: 'a;
126
127    fn open<E: From<Errno>>(path: &std::ffi::CStr, flags: Open, mode: Mode) -> Result<OwnedFd, E> {
128        raw::open::<Self, E>(None, path, flags, mode)
129    }
130
131    fn metadata<E: From<Errno>>(file: BorrowedFd<'_>) -> Result<Metadata, E> {
132        raw::metadata::<Self, E>(file)
133    }
134
135    fn stat<E: From<Errno>>(path: &std::ffi::CStr) -> Result<Metadata, E> {
136        raw::stat::<Self, E, false>(None, path)
137    }
138
139    fn mkdir<E: From<Errno>>(path: &std::ffi::CStr, mode: Mode) -> Result<(), E> {
140        raw::mkdir::<Self, E>(path, mode)
141    }
142
143    /// Classifies the named entry itself while retaining its parent directory.
144    fn metadata_at<E: From<Errno>>(
145        dir: Option<BorrowedFd<'_>>,
146        name: &std::ffi::CStr,
147    ) -> Result<Metadata, E> {
148        raw::stat::<Self, E, true>(dir, name)
149    }
150
151    fn unlink<E: From<Errno>>(path: &std::ffi::CStr) -> Result<(), E> {
152        raw::unlink_at::<Self, E>(None, path, 0)
153    }
154
155    fn rename<E: From<Errno>>(from: &std::ffi::CStr, to: &std::ffi::CStr) -> Result<(), E> {
156        raw::rename::<Self, E>(None, from, to)
157    }
158
159    /// Attempts an exclusive advisory lock without waiting for its current owner.
160    fn try_lock<E: From<Errno>>(file: BorrowedFd<'_>) -> Result<(), E> {
161        raw::try_lock::<Self, E>(file)
162    }
163
164    fn sync<E: From<Errno>>(file: BorrowedFd<'_>) -> Result<(), E> {
165        raw::sync::<Self, E>(file)
166    }
167
168    fn truncate<E: From<Errno>>(file: BorrowedFd<'_>, len: i64) -> Result<(), E> {
169        raw::truncate::<Self, E>(file, len)
170    }
171
172    fn unlink_at<E: From<Errno>>(dir: BorrowedFd<'_>, name: &std::ffi::CStr) -> Result<(), E> {
173        raw::unlink_at::<Self, E>(Some(dir), name, 0)
174    }
175
176    fn rmdir_at<E: From<Errno>>(
177        dir: Option<BorrowedFd<'_>>,
178        name: &std::ffi::CStr,
179    ) -> Result<(), E> {
180        raw::unlink_at::<Self, E>(dir, name, libc::AT_REMOVEDIR)
181    }
182
183    fn open_at<E: From<Errno>>(
184        dir: BorrowedFd<'_>,
185        name: &std::ffi::CStr,
186        flags: Open,
187        mode: Mode,
188    ) -> Result<OwnedFd, E> {
189        raw::open::<Self, E>(Some(dir), name, flags, mode)
190    }
191
192    fn rename_at<E: From<Errno>>(
193        dir: BorrowedFd<'_>,
194        old: &std::ffi::CStr,
195        new: &std::ffi::CStr,
196    ) -> Result<(), E> {
197        raw::rename::<Self, E>(Some(dir), old, new)
198    }
199
200    /// Reads one prefix without changing the descriptor's current position.
201    fn read_at<'a, E: From<Errno>>(
202        file: BorrowedFd<'_>,
203        output: &'a mut [u8],
204        offset: Offset,
205    ) -> Result<&'a mut [u8], E> {
206        raw::read_at::<Self, E>(file, output, offset)
207    }
208
209    fn write_at<E: From<Errno>>(
210        file: BorrowedFd<'_>,
211        input: bytes::Prefix<'_, MAX>,
212        offset: Offset,
213    ) -> Result<bounded::raw::U32<0, { MAX as u32 }>, E> {
214        raw::write_at::<Self, E>(file, input, offset)
215    }
216}