hadris_core/
file.rs

1use spin::Mutex;
2use crate::internal::{FileSystemRead, FileSystemWrite};
3
4bitflags::bitflags! {
5    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
6    pub struct OpenOptions: u8 {
7        /// Open the file in read only mode
8        const READ = 0x01;
9        /// Open the file in write mode
10        const WRITE = 0x02;
11        /// Open the file in append mode
12        const APPEND = 0x04;
13        /// Create the file when opening if it doesn't exist
14        const CREATE = 0x08;
15    }
16
17    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
18    pub struct FileAttributes: u16 {
19        /// A read only file
20        const READ_ONLY = 0x01;
21        /// A file that should be hidden, should only display with a certain flag
22        const HIDDEN = 0x02;
23    }
24}
25
26#[derive(Debug)]
27pub struct File {
28    descriptor: u32,
29    seek: Mutex<u32>,
30}
31
32impl File {
33    /// # Safety
34    ///
35    /// The descriptor must be valid
36    pub unsafe fn with_descriptor(descriptor: u32) -> Self {
37        Self {
38            descriptor,
39            seek: Mutex::new(0),
40        }
41    }
42
43    pub fn read<T: FileSystemRead + ?Sized>(&self, fs: &T, buffer: &mut [u8]) -> Result<usize, ()> {
44        fs.read(self, buffer)
45    }
46
47    pub fn write<T: FileSystemWrite + ?Sized>(&self, fs: &mut T, buffer: &[u8]) -> Result<usize, ()> {
48        fs.write(self, buffer)
49    }
50
51    pub fn descriptor(&self) -> u32 {
52        self.descriptor
53    }
54
55    pub fn seek(&self) -> u32 {
56        *self.seek.lock()
57    }
58
59    pub fn set_seek(&self, seek: u32) {
60        *self.seek.lock() = seek;
61    }
62}