1use std::{fs, mem, os::fd};
2
3use dope_sys::io::{fd::Fd as _, file, request::Request};
4use file::File as _;
5use o3::{base::marker::ThreadBound, buffer::bytes};
6
7use crate::{
8 Backend, Errno,
9 driver::{
10 route,
11 route::{
12 kind,
13 kind::{Class, Open, Write},
14 },
15 },
16 io::{fs::metadata::Metadata, transfer},
17};
18
19mod dir;
20
21mod len;
22pub mod metadata;
23pub mod open;
24pub mod path;
25
26pub use dir::Dir;
27pub use len::Len;
28
29#[derive(Clone, Copy, Debug, Eq, PartialEq)]
31#[repr(transparent)]
32pub struct Kind(file::Kind);
33
34impl Kind {
35 pub(crate) const fn from_meta(raw: file::Metadata) -> Self {
36 Self(raw.kind())
37 }
38
39 pub const fn is_regular(self) -> bool {
40 self.0.is_regular()
41 }
42}
43
44#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
46#[repr(transparent)]
47pub struct Offset(file::Offset);
48
49#[derive(Clone, Copy, Debug, Eq, PartialEq)]
51pub struct Overflow {
52 pub actual: u64,
53 pub maximum: u64,
54}
55
56#[derive(Clone, Copy, Debug, Eq, PartialEq)]
57pub struct Extent {
58 offset: Offset,
59 length: transfer::Len,
60}
61
62impl From<Len> for Offset {
63 fn from(length: Len) -> Self {
64 Self(length.0)
65 }
66}
67
68impl From<transfer::Len> for Offset {
69 fn from(length: transfer::Len) -> Self {
70 Self(file::Offset::clamp(length.into_raw() as u64))
71 }
72}
73
74impl Offset {
75 pub const fn fixed<const OFFSET: u64>() -> Self {
76 const { assert!(OFFSET <= i64::MAX as u64) };
77 Self(file::Offset::clamp(OFFSET))
78 }
79
80 pub const fn try_new(value: u64) -> Result<Self, Overflow> {
81 match file::Offset::new(value) {
82 Some(value) => Ok(Self(value)),
83 None => Err(Overflow {
84 actual: value,
85 maximum: i64::MAX as u64,
86 }),
87 }
88 }
89
90 pub(crate) const fn get(self) -> u64 {
91 self.0.get()
92 }
93
94 pub const fn checked_add(self, length: transfer::Len) -> Result<Self, Extent> {
95 let value = self.get() + length.into_raw() as u64;
96 match Self::try_new(value) {
97 Ok(value) => Ok(value),
98 Err(_) => Err(Extent::new(self, length)),
99 }
100 }
101
102 pub const fn checked_sub(self, length: transfer::Len) -> Result<Self, Extent> {
103 match self.0.checked_sub(length.into_raw() as u64) {
104 Some(value) => Ok(Self(value)),
105 None => Err(Extent::new(self, length)),
106 }
107 }
108}
109
110impl Extent {
111 const fn new(offset: Offset, length: transfer::Len) -> Self {
112 Self { offset, length }
113 }
114}
115
116const _: () = {
117 assert!(mem::size_of::<Kind>() == mem::size_of::<file::Kind>());
118 assert!(mem::align_of::<Kind>() == mem::align_of::<file::Kind>());
119 assert!(mem::size_of::<Offset>() == mem::size_of::<u64>());
120 assert!(mem::align_of::<Offset>() == mem::align_of::<u64>());
121 assert!(mem::size_of::<Extent>() == 2 * mem::size_of::<u64>());
122 assert!(mem::align_of::<Extent>() == mem::align_of::<u64>());
123 assert!(mem::size_of::<Result<Offset, Extent>>() == 3 * mem::size_of::<u64>());
124 assert!(mem::align_of::<Result<Offset, Extent>>() == mem::align_of::<u64>());
125};
126
127pub struct Submission<'a, 'd, Tag, C = <Tag as route::Tag>::Class>
135where
136 Tag: route::Tag,
137 C: Class,
138{
139 raw: <Backend as file::File>::Request<'a>,
140 target: route::Operation<'d, Tag, C>,
141}
142
143pub struct OpenRequest {
144 path: path::Confined,
145 beneath: <Backend as file::File>::Options,
146}
147
148impl<'a, 'd, Tag, C> Submission<'a, 'd, Tag, C>
149where
150 Tag: route::Tag,
151 C: Class,
152{
153 fn bind(
154 raw: <Backend as file::File>::Request<'a>,
155 target: route::Operation<'d, Tag, C>,
156 ) -> Self {
157 Self { raw, target }
158 }
159
160 pub(crate) fn into_parts(
161 self,
162 ) -> (
163 <Backend as file::File>::Request<'a>,
164 route::Operation<'d, Tag, C>,
165 ) {
166 (self.raw, self.target)
167 }
168}
169
170impl<'a, 'd, Tag> Submission<'a, 'd, Tag, Write>
171where
172 Tag: route::Tag,
173{
174 #[doc(hidden)]
175 pub fn write_prefix(
176 fd: fd::BorrowedFd<'a>,
177 buffer: &'a [u8],
178 offset: Offset,
179 target: route::Target<'d, Tag>,
180 ) -> Self
181 where
182 Tag: route::Select<Write>,
183 {
184 let input = bytes::Prefix::new(buffer);
185 Self::bind(
186 Request::write(fd, input, offset.0),
187 target.operation::<Write>(),
188 )
189 }
190}
191
192impl<'a, 'd, Tag> Submission<'a, 'd, Tag, kind::Sync>
193where
194 Tag: route::Tag,
195{
196 pub fn sync(fd: fd::BorrowedFd<'a>, mode: Sync, target: route::Target<'d, Tag>) -> Self
198 where
199 Tag: route::Select<kind::Sync>,
200 {
201 Self::bind(
202 Request::sync(
203 fd,
204 match mode {
205 Sync::Data => file::Sync::Data,
206 Sync::All => file::Sync::All,
207 },
208 ),
209 target.operation::<kind::Sync>(),
210 )
211 }
212
213 #[doc(hidden)]
215 pub fn rename(rename: path::Rename<'a>, target: route::Target<'d, Tag>) -> Self
216 where
217 Tag: route::Select<kind::Sync>,
218 {
219 Self::bind(
220 Request::rename(
221 fd::AsFd::as_fd(rename.directory()),
222 rename.old(),
223 rename.new_name(),
224 ),
225 target.operation::<kind::Sync>(),
226 )
227 }
228
229 #[doc(hidden)]
231 pub fn unlink(entry: path::Entry<'a>, target: route::Target<'d, Tag>) -> Self
232 where
233 Tag: route::Select<kind::Sync>,
234 {
235 Self::bind(
236 Request::unlink(fd::AsFd::as_fd(entry.directory()), entry.name()),
237 target.operation::<kind::Sync>(),
238 )
239 }
240}
241
242impl OpenRequest {
243 pub fn new(path: path::Confined, options: open::Options) -> Self {
245 let (flags, mode) = options.nofollow().lower();
246 Self {
247 path,
248 beneath: <<Backend as file::File>::Request<'_> as Request>::options(flags, mode),
249 }
250 }
251
252 pub fn submission<'d, Tag>(
253 &self,
254 target: route::Target<'d, Tag>,
255 ) -> Submission<'_, 'd, Tag, Open>
256 where
257 Tag: route::Tag + route::Select<Open>,
258 {
259 let path = self.path.view();
260 Submission::bind(
261 Request::open(
262 fd::AsFd::as_fd(path.directory()),
263 path.name(),
264 &self.beneath,
265 ),
266 target.operation::<Open>(),
267 )
268 }
269
270 pub fn into_path(self) -> path::Confined {
271 self.path
272 }
273}
274
275pub enum Sync {
277 Data,
278 All,
279}
280
281#[repr(transparent)]
283pub struct File {
284 fd: fd::OwnedFd,
285 thread: ThreadBound,
286}
287
288impl File {
289 pub fn try_read<'a>(&self, bytes: &'a mut [u8]) -> Result<&'a mut [u8], Errno> {
290 Backend::read::<dope_sys::Errno>(fd::AsFd::as_fd(self), bytes)
291 }
292
293 pub fn try_read_at<'a>(
295 &self,
296 bytes: &'a mut [u8],
297 offset: Offset,
298 ) -> Result<&'a mut [u8], Errno> {
299 Backend::read_at::<dope_sys::Errno>(fd::AsFd::as_fd(self), bytes, offset.0)
300 }
301
302 pub fn try_write<'a>(&self, bytes: &'a [u8]) -> Result<&'a [u8], Errno> {
303 Backend::write::<dope_sys::Errno>(fd::AsFd::as_fd(self), bytes)
304 }
305
306 pub fn metadata(&self) -> Result<Metadata, Errno> {
307 let value = Backend::metadata::<dope_sys::Errno>(fd::AsFd::as_fd(self))?;
308 Ok(Metadata {
309 kind: Kind::from_meta(value),
310 length: value.size(),
311 })
312 }
313
314 pub fn try_lock(&self) -> Result<(), Errno> {
316 Backend::try_lock::<dope_sys::Errno>(fd::AsFd::as_fd(self))
317 }
318
319 pub fn try_sync(&self) -> Result<(), Errno> {
320 Backend::sync::<dope_sys::Errno>(fd::AsFd::as_fd(self))
321 }
322}
323
324impl fd::AsFd for File {
325 fn as_fd(&self) -> fd::BorrowedFd<'_> {
326 self.fd.as_fd()
327 }
328}
329
330impl From<fd::OwnedFd> for File {
331 fn from(file: fd::OwnedFd) -> Self {
332 Self {
333 fd: file,
334 thread: ThreadBound::NEW,
335 }
336 }
337}
338
339impl From<fs::File> for File {
340 fn from(file: fs::File) -> Self {
341 Self {
342 fd: file.into(),
343 thread: ThreadBound::NEW,
344 }
345 }
346}
347
348impl From<File> for fs::File {
349 fn from(file: File) -> Self {
350 file.fd.into()
351 }
352}
353
354impl From<File> for fd::OwnedFd {
355 fn from(file: File) -> Self {
356 file.fd
357 }
358}
359
360const _: () = {
361 assert!(size_of::<File>() == size_of::<fd::OwnedFd>());
362 assert!(align_of::<File>() == align_of::<fd::OwnedFd>());
363};