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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
use crate::LibUserResult;
use sunrise_libuser::error::{Error, FileSystemError};
use super::error::from_driver;
use crate::interface::filesystem::*;
use alloc::boxed::Box;
use sunrise_libuser::fs::{DirectoryEntryType, FileTimeStampRaw, FileSystemType};
use core::fmt;
use core::fmt::{Debug, Formatter};
use libfat::FatFsType;
use alloc::sync::Arc;
use spin::Mutex;
use storage_device::StorageDevice;
use super::file::FileInterface;
use super::directory::DirectoryInterface;
use super::directory::DirectoryFilterPredicate;
use libfat::FileSystemIterator;
use arrayvec::ArrayString;
pub struct FatFileSystem {
inner: Arc<Mutex<libfat::filesystem::FatFileSystem<Box<dyn StorageDevice<Error = Error> + Send>>>>,
}
impl Debug for FatFileSystem {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("FatFileSystem")
.finish()
}
}
impl FatFileSystem {
pub fn new(inner: libfat::filesystem::FatFileSystem<Box<dyn StorageDevice<Error = Error> + Send>>) -> Self {
FatFileSystem { inner: Arc::new(Mutex::new(inner)) }
}
pub fn from_storage(storage: Box<dyn StorageDevice<Error = Error> + Send>) -> LibUserResult<Self> {
let filesystem = libfat::get_raw_partition(storage).map_err(from_driver)?;
Ok(Self::new(filesystem))
}
}
impl FileSystemOperations for FatFileSystem {
fn create_file(&self, path: &str, size: u64) -> LibUserResult<()> {
self.inner
.lock()
.create_file(path)
.map_err(from_driver)?;
let mut file = FileSystemOperations::open_file(self, path, FileModeFlags::APPENDABLE)?;
file.set_len(size)
}
fn create_directory(&self, path: &str) -> LibUserResult<()> {
self.inner
.lock()
.create_directory(path)
.map_err(from_driver)
}
fn rename_file(&self, old_path: &str, new_path: &str) -> LibUserResult<()> {
self.inner
.lock()
.rename_file(old_path, new_path)
.map_err(from_driver)
}
fn rename_directory(&self, old_path: &str, new_path: &str) -> LibUserResult<()> {
self.inner
.lock()
.rename_directory(old_path, new_path)
.map_err(from_driver)
}
fn delete_file(&self, path: &str) -> LibUserResult<()> {
self.inner
.lock()
.delete_file(path)
.map_err(from_driver)
}
fn delete_directory(&self, path: &str) -> LibUserResult<()> {
self.inner
.lock()
.delete_directory(path)
.map_err(from_driver)
}
fn get_entry_type(&self, path: &str) -> LibUserResult<DirectoryEntryType> {
self.inner
.lock()
.search_entry(path)
.map_err(from_driver)
.map(|result| {
if result.attribute.is_directory() {
DirectoryEntryType::Directory
} else {
DirectoryEntryType::File
}
})
}
fn open_file(
&self,
path: &str,
mode: FileModeFlags,
) -> LibUserResult<Box<dyn FileOperations>> {
let file_entry = self
.inner
.lock()
.open_file(path)
.map_err(from_driver)?;
let inner_fs = self.inner.clone();
let res = Box::new(FileInterface::new(inner_fs, file_entry, mode));
Ok(res as Box<dyn FileOperations>)
}
fn open_directory(
&self,
path: &str,
filter: DirFilterFlags,
) -> LibUserResult<Box<dyn DirectoryOperations>> {
let filter_fn: fn(&_) -> bool =
if (filter & DirFilterFlags::ALL) == DirFilterFlags::ALL {
DirectoryFilterPredicate::all as _
} else if (filter & DirFilterFlags::DIRECTORY) == DirFilterFlags::DIRECTORY {
DirectoryFilterPredicate::dirs as _
} else {
DirectoryFilterPredicate::files as _
};
let filesystem = self.inner.lock();
let target_dir = filesystem.open_directory(path).map_err(from_driver)?;
let mut entry_count = 0u64;
for entry in target_dir.iter().to_iterator(&filesystem) {
let entry = entry.map_err(from_driver)?;
if !filter_fn(&entry) {
continue;
}
entry_count += 1;
}
let mut data: ArrayString<[u8; PATH_LEN]> = ArrayString::new();
if data.try_push_str(path).is_err() {
return Err(FileSystemError::InvalidInput.into());
}
if let Some('/') = path.chars().last() {
} else if data.try_push('/').is_err() {
return Err(FileSystemError::InvalidInput.into());
}
let res = Box::new(DirectoryInterface::new(
data,
self.inner.clone(),
target_dir.iter(),
filter_fn,
entry_count,
));
Ok(res as Box<dyn DirectoryOperations>)
}
fn get_free_space_size(&self, _path: &str) -> LibUserResult<u64> {
self.inner
.lock()
.get_free_space_size()
.map_err(from_driver)
}
fn get_total_space_size(&self, _path: &str) -> LibUserResult<u64> {
self.inner
.lock()
.get_total_space_size()
.map_err(from_driver)
}
fn get_file_timestamp_raw(&self, path: &str) -> LibUserResult<FileTimeStampRaw> {
let file_entry = self
.inner
.lock()
.search_entry(path)
.map_err(from_driver)?;
let result = FileTimeStampRaw {
creation_timestamp: file_entry.creation_timestamp,
modified_timestamp: file_entry.last_modification_timestamp,
accessed_timestamp: file_entry.last_access_timestamp,
is_valid: true,
};
Ok(result)
}
fn get_filesystem_type(&self) -> FileSystemType {
match self.inner.lock().get_type() {
FatFsType::Fat12 => FileSystemType::FAT12,
FatFsType::Fat16 => FileSystemType::FAT16,
FatFsType::Fat32 => FileSystemType::FAT32,
}
}
}