1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use alloc::boxed::Box;
use sunrise_libuser::fs::{DirectoryEntry, DirectoryEntryType, FileTimeStampRaw, FileSystemType};
use sunrise_libuser::error::FileSystemError;
use crate::LibUserResult;
pub const PATH_LEN: usize = 0x300;
pub fn convert_path(raw_path: &[u8]) -> LibUserResult<&str> {
core::str::from_utf8(raw_path).ok()
.and_then(|str_path: &str| str_path.split('\0').next())
.ok_or_else(|| FileSystemError::InvalidInput.into())
}
bitflags! {
pub struct FileModeFlags: u32 {
const READABLE = 0b0000_0001;
const WRITABLE = 0b0000_0010;
const APPENDABLE = 0b0000_0100;
}
}
bitflags! {
pub struct DirFilterFlags: u32 {
const DIRECTORY = 0b0000_0001;
const FILE = 0b0000_0010;
const ALL = Self::DIRECTORY.bits | Self::FILE.bits;
}
}
pub trait FileOperations : core::fmt::Debug + Sync + Send {
fn read(&mut self, offset: u64, buf: &mut [u8]) -> LibUserResult<u64>;
fn write(&mut self, offset: u64, buf: &[u8]) -> LibUserResult<()>;
fn flush(&mut self) -> LibUserResult<()>;
fn set_len(&mut self, size: u64) -> LibUserResult<()>;
fn get_len(&mut self) -> LibUserResult<u64>;
}
pub trait DirectoryOperations : core::fmt::Debug + Sync + Send {
fn read(&mut self, buf: &mut [DirectoryEntry]) -> LibUserResult<u64>;
fn entry_count(&self) -> LibUserResult<u64>;
}
pub trait FileSystemOperations : core::fmt::Debug + Sync + Send {
fn create_file(&self, path: &str, size: u64) -> LibUserResult<()>;
fn create_directory(&self, path: &str) -> LibUserResult<()>;
fn rename_file(&self, old_path: &str, new_path: &str) -> LibUserResult<()>;
fn rename_directory(&self, old_path: &str, new_path: &str) -> LibUserResult<()>;
fn delete_file(&self, path: &str) -> LibUserResult<()>;
fn delete_directory(&self, path: &str) -> LibUserResult<()>;
fn get_entry_type(&self, path: &str) -> LibUserResult<DirectoryEntryType>;
fn open_file(
&self,
path: &str,
mode: FileModeFlags,
) -> LibUserResult<Box<dyn FileOperations>>;
fn open_directory(
&self,
path: &str,
filter: DirFilterFlags,
) -> LibUserResult<Box<dyn DirectoryOperations>>;
fn get_free_space_size(&self, path: &str) -> LibUserResult<u64>;
fn get_total_space_size(&self, path: &str) -> LibUserResult<u64>;
fn get_file_timestamp_raw(&self, path: &str) -> LibUserResult<FileTimeStampRaw>;
fn get_filesystem_type(&self) -> FileSystemType;
}
#[cfg(test)]
mod tests {
use super::convert_path;
#[test]
pub fn test_convert_path() {
assert_eq!(convert_path(b"/etc/motd\0").ok(), Some("/etc/motd"));
assert_eq!(convert_path(b"/etc/motd\0garbage").ok(), Some("/etc/motd"));
assert_eq!(convert_path(b"/etc/motd\0/nope\0/help").ok(), Some("/etc/motd"));
assert_eq!(convert_path(b"\0/etc/motd").ok(), Some(""));
}
}