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 const READ = 0x01;
12 const WRITE = 0x02;
14 const APPEND = 0x04;
16 const CREATE = 0x08;
18 }
19
20 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
21 pub struct FileAttributes: u16 {
22 const READ_ONLY = 0x01;
24 const HIDDEN = 0x02;
26 }
27}
28
29#[derive(Debug)]
30pub struct File {
31 descriptor: u32,
32 seek: Mutex<u32>,
33}
34
35impl File {
36 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}