hadris_core/
file.rs

1use crate::{
2    internal::{FileSystemRead, FileSystemWrite},
3    UtcTime,
4};
5use spin::Mutex;
6
7bitflags::bitflags! {
8    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
9    pub struct OpenOptions: u8 {
10        /// Open the file in read only mode
11        const READ = 0x01;
12        /// Open the file in write mode
13        const WRITE = 0x02;
14        /// Open the file in append mode
15        const APPEND = 0x04;
16        /// Create the file when opening if it doesn't exist
17        const CREATE = 0x08;
18    }
19
20    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
21    pub struct FileAttributes: u16 {
22        /// A read only file
23        const READ_ONLY = 0x01;
24        /// A file that should be hidden, should only display with a certain flag
25        const HIDDEN = 0x02;
26    }
27}
28
29#[derive(Debug)]
30pub struct File {
31    descriptor: u32,
32    seek: Mutex<u32>,
33}
34
35impl File {
36    /// # Safety
37    ///
38    /// The descriptor must be valid
39    pub unsafe fn with_descriptor(descriptor: u32) -> Self {
40        Self {
41            descriptor,
42            seek: Mutex::new(0),
43        }
44    }
45
46    pub fn read_with_timestamp<T: FileSystemRead + ?Sized>(
47        &self,
48        fs: &mut T,
49        buffer: &mut [u8],
50        time: UtcTime,
51    ) -> Result<usize, ()> {
52        fs.read(self, buffer, time)
53    }
54
55    pub fn write_with_timestamp<T: FileSystemWrite + ?Sized>(
56        &self,
57        fs: &mut T,
58        buffer: &[u8],
59        time: UtcTime,
60    ) -> Result<usize, ()> {
61        fs.write(self, buffer, time)
62    }
63
64    #[cfg(feature = "std")]
65    pub fn read<T: FileSystemRead + ?Sized>(
66        &self,
67        fs: &mut T,
68        buffer: &mut [u8],
69    ) -> Result<usize, ()> {
70        fs.read(self, buffer, std::time::SystemTime::now().into())
71    }
72
73    #[cfg(feature = "std")]
74    pub fn write<T: FileSystemWrite + ?Sized>(
75        &self,
76        fs: &mut T,
77        buffer: &[u8],
78    ) -> Result<usize, ()> {
79        fs.write(self, buffer, std::time::SystemTime::now().into())
80    }
81
82    pub fn descriptor(&self) -> u32 {
83        self.descriptor
84    }
85
86    pub fn seek(&self) -> u32 {
87        *self.seek.lock()
88    }
89
90    pub fn set_seek(&self, seek: u32) {
91        *self.seek.lock() = seek;
92    }
93}