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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
use crate::{
Bytes,
error::{Error, Result},
// service::ReadDirState,
store::{self, Store},
types::{Message, Location as Location, UserAttribute},
};
#[derive(Clone)]
pub struct ReadDirState {
real_dir: PathBuf,
last: usize,
}
#[derive(Clone)]
pub struct ReadDirFilesState {
real_dir: PathBuf,
last: usize,
location: Location,
user_attribute: Option<UserAttribute>,
}
use littlefs2::{fs::DirEntry, path::{Path, PathBuf}};
pub type ClientId = PathBuf;
pub struct ClientFilestore<S>
where
S: Store,
{
client_id: ClientId,
store: S,
}
impl<S: Store> ClientFilestore<S> {
pub fn new(client_id: ClientId, store: S) -> Self {
Self { client_id, store }
}
/// Client files are store below `/<client_id>/dat/`.
pub fn actual_path(&self, client_path: &PathBuf) -> PathBuf {
let mut path = PathBuf::new();
path.push(&self.client_id);
path.push(&PathBuf::from("dat"));
path.push(client_path);
path
}
// TODO: this is waaay too fiddly, need an approach
// that totally excludes off-by-N type errors.
pub fn client_path(&self, actual_path: &Path) -> PathBuf {
let bytes = actual_path.as_ref().as_bytes();
let absolute = bytes[0] == b'/';
let offset = if absolute { 1 } else { 0 };
// we know `client_id` here, could use its length
let end_of_namespace = bytes[1..].iter().position(|&x| x == b'/')
// oh oh oh
.unwrap();
let dat_offset = "/dat/".len();
PathBuf::from(&bytes[end_of_namespace + 1 + offset + dat_offset..])
}
}
pub trait Filestore {
fn read<const N: usize>(&mut self, path: &PathBuf, location: Location) -> Result<Bytes<N>>;
fn write(&mut self, path: &PathBuf, location: Location, data: &[u8]) -> Result<()>;
fn exists(&mut self, path: &PathBuf, location: Location) -> bool;
fn remove_file(&mut self, path: &PathBuf, location: Location) -> Result<()>;
fn remove_dir(&mut self, path: &PathBuf, location: Location) -> Result<()>;
fn remove_dir_all(&mut self, path: &PathBuf, location: Location) -> Result<usize>;
fn locate_file(&mut self, location: Location, underneath: Option<PathBuf>, filename: PathBuf) -> Result<Option<PathBuf>>;
/// Iterate over entries of a directory (both file and directory entries).
///
/// This function is modeled after `std::fs::read_dir`, within the limitations of our setup.
///
/// The `not_before` parameter is an optimization for users to locate a specifically named
/// file in one call - if the filename exists (e.g., `my-data.txt`), then return it directly.
///
/// In case an entry was found, the returned option also contains state, so the expected
/// call to `read_dir_next` can resume operation.
fn read_dir_first(&mut self, dir: &PathBuf, location: Location, not_before: Option<&PathBuf>)
-> Result<Option<(DirEntry, ReadDirState)>>;
/// Continue iterating over entries of a directory.
///
/// Return the entry just after the previous one. If it exists, also return state for the
/// following call.
fn read_dir_next(&mut self, state: ReadDirState)
-> Result<Option<(DirEntry, ReadDirState)>>;
/// Iterate over contents of files inside a directory.
///
/// This has no equivalent in `std::fs`, it is an optimization to avoid duplicate
/// calls and a more complicated state machine (interspersing read_dir_first/next calls
/// with some sort of "fetch data").
///
/// Additionally, files may optionally be filtered via attributes.
fn read_dir_files_first(&mut self, clients_dir: &PathBuf, location: Location, user_attribute: Option<UserAttribute>)
-> Result<Option<(Option<Message>, ReadDirFilesState)>>;
/// Continuation of `read_dir_files_first`.
fn read_dir_files_next(&mut self, state: ReadDirFilesState)
-> Result<Option<(Option<Message>, ReadDirFilesState)>>;
}
impl<S: Store> Filestore for ClientFilestore<S> {
fn read<const N: usize>(&mut self, path: &PathBuf, location: Location) -> Result<Bytes<N>> {
let path = self.actual_path(path);
store::read(self.store, location, &path)
}
fn write(&mut self, path: &PathBuf, location: Location, data: &[u8]) -> Result<()> {
let path = self.actual_path(path);
store::store(self.store, location, &path, data)
}
fn exists(&mut self, path: &PathBuf, location: Location) -> bool {
let path = self.actual_path(path);
store::exists(self.store, location, &path)
}
fn remove_file(&mut self, path: &PathBuf, location: Location) -> Result<()> {
let path = self.actual_path(path);
match store::delete(self.store, location, &path) {
true => Ok(()),
false => Err(Error::InternalError),
}
}
fn remove_dir(&mut self, path: &PathBuf, location: Location) -> Result<()> {
let path = self.actual_path(path);
match store::delete(self.store, location, &path) {
true => Ok(()),
false => Err(Error::InternalError),
}
}
fn remove_dir_all(&mut self, path: &PathBuf, location: Location) -> Result<usize> {
let path = self.actual_path(path);
store::remove_dir_all_where(self.store, location, &path, |_| true)
.map_err(|_| Error::InternalError)
}
fn read_dir_first(&mut self, clients_dir: &PathBuf, location: Location, not_before: Option<&PathBuf>) -> Result<Option<(DirEntry, ReadDirState)>> {
if location != Location::Internal {
return Err(Error::RequestNotAvailable);
}
let fs = self.store.ifs();
let dir = self.actual_path(clients_dir);
Ok(fs.read_dir_and_then(&dir, |it| {
// this is an iterator with Item = (usize, Result<DirEntry>)
it.enumerate()
// skip over `.` and `..`
.skip(2)
// todo: try ?-ing out of this (the API matches std::fs, where read/write errors
// can occur during operation)
//
// Option<usize, Result<DirEntry>> -> ??
.map(|(i, entry)| (i, entry.unwrap()))
// if there is a "not_before" entry, skip all entries before it.
.find(|(_, entry)| {
if let Some(not_before) = not_before {
entry.file_name() == not_before.as_ref()
} else { true }
})
// if there is an entry, construct the state that needs storing out of it,
// remove the prefix from the entry's path to not leak implementation details to
// the client, and return both the entry and the state
.map(|(i, mut entry)| {
let read_dir_state = ReadDirState { real_dir: dir.clone(), last: i };
let entry_client_path = self.client_path(entry.path());
// trace_now!("converted path {} to client path {}", &entry.path(), &entry_client_path);
// This is a hidden function which allows us to modify `entry.path`.
// In regular use, `DirEntry` is not supposed to be constructable by the user
// (only by querying the filesystem), which is why the function is both
// hidden and tagged "unsafe" to discourage use. Our use case here is precisely
// the reason for its existence :)
*unsafe { entry.path_buf_mut() } = entry_client_path;
(entry, read_dir_state)
// the `ok_or` dummy error followed by the `ok` in the next line is because
// `read_dir_and_then` wants to see Results (although we naturally have an Option
// at this point)
}).ok_or(littlefs2::io::Error::Io)
}).ok())
}
fn read_dir_next(&mut self, state: ReadDirState) -> Result<Option<(DirEntry, ReadDirState)>> {
let ReadDirState { real_dir, last } = state;
let fs = self.store.ifs();
// all we want to do here is skip just past the previously found entry
// in the directory iterator, then return it (plus state to continue on next call)
Ok(fs.read_dir_and_then(&real_dir, |it| {
// skip over previous
it.enumerate().nth(last + 1)
// entry is still a Result :/ (see question in `read_dir_first`)
.map(|(i,entry)| (i, entry.unwrap()))
// convert Option into Result, again because `read_dir_and_then` expects this
.map(|(i, mut entry)| {
let read_dir_state = ReadDirState { real_dir: real_dir.clone(), last: i };
let entry_client_path = self.client_path(entry.path());
*unsafe { entry.path_buf_mut() } = entry_client_path;
(entry, read_dir_state)
})
.ok_or(littlefs2::io::Error::Io)
}).ok())
}
fn read_dir_files_first(
&mut self,
clients_dir: &PathBuf,
location: Location,
user_attribute: Option<UserAttribute>,
) -> Result<Option<(Option<Message>, ReadDirFilesState)>> {
if location != Location::Internal {
return Err(Error::RequestNotAvailable);
}
let fs = self.store.ifs();
let dir = self.actual_path(clients_dir);
Ok(fs.read_dir_and_then(&dir, |it| {
// this is an iterator with Item = (usize, Result<DirEntry>)
it.enumerate()
// todo: try ?-ing out of this (the API matches std::fs, where read/write errors
// can occur during operation)
//
// Option<usize, Result<DirEntry>> -> ??
.map(|(i, entry)| (i, entry.unwrap()))
// skip over directories (including `.` and `..`)
.filter(|(_, entry)| entry.file_type().is_file())
// take first entry that meets requirements
.find(|(_, entry)| {
if let Some(user_attribute) = user_attribute.as_ref() {
let mut path = dir.clone();
path.push(entry.file_name());
let attribute = fs.attribute(&path, crate::config::USER_ATTRIBUTE_NUMBER).unwrap();
if let Some(attribute) = attribute {
user_attribute == attribute.data()
} else {
false
}
} else { true }
})
// if there is an entry, construct the state that needs storing out of it,
// and return the file's contents.
// the client, and return both the entry and the state
.map(|(i, entry)| {
let read_dir_files_state = ReadDirFilesState { real_dir: dir.clone(), last: i, location, user_attribute };
// The semantics is that for a non-existent file, we return None (not an error)
let data = store::read(self.store, location, entry.path()).ok();
(data, read_dir_files_state)
// the `ok_or` dummy error followed by the `ok` in the next line is because
// `read_dir_and_then` wants to see Results (although we naturally have an Option
// at this point)
}).ok_or(littlefs2::io::Error::Io)
}).ok())
}
fn read_dir_files_next(&mut self, state: ReadDirFilesState)
-> Result<Option<(Option<Message>, ReadDirFilesState)>>
{
let ReadDirFilesState { real_dir, last, location, user_attribute } = state;
let fs = self.store.ifs();
// all we want to do here is skip just past the previously found entry
// in the directory iterator, then return it (plus state to continue on next call)
Ok(fs.read_dir_and_then(&real_dir, |it| {
// skip over previous
it.enumerate().skip(last + 1)
// entry is still a Result :/ (see question in `read_dir_first`)
.map(|(i,entry)| (i, entry.unwrap()))
// skip over directories (including `.` and `..`)
.filter(|(_, entry)| entry.file_type().is_file())
// take first entry that meets requirements
.find(|(_, entry)| {
if let Some(user_attribute) = user_attribute.as_ref() {
let mut path = real_dir.clone();
path.push(entry.file_name());
let attribute = fs.attribute(&path, crate::config::USER_ATTRIBUTE_NUMBER).unwrap();
if let Some(attribute) = attribute {
user_attribute == attribute.data()
} else {
false
}
} else { true }
})
.map(|(i, entry)| {
let read_dir_files_state = ReadDirFilesState { real_dir: real_dir.clone(), last: i, location, user_attribute };
// The semantics is that for a non-existent file, we return None (not an error)
let data = store::read(self.store, location, entry.path()).ok();
(data, read_dir_files_state)
})
// convert Option into Result, again because `read_dir_and_then` expects this
.ok_or(littlefs2::io::Error::Io)
}).ok())
}
fn locate_file(&mut self, location: Location, underneath: Option<PathBuf>, filename: PathBuf) -> Result<Option<PathBuf>> {
if location != Location::Internal {
return Err(Error::RequestNotAvailable);
}
let clients_dir = underneath.unwrap_or_else(|| PathBuf::from("/"));
let dir = self.actual_path(&clients_dir);
let fs = self.store.ifs();
info_now!("base dir {:?}", &dir);
fn recursively_locate<S: 'static + crate::types::LfsStorage>(
fs: &'static crate::store::Fs<S>,
dir: PathBuf,
filename: &Path
)
-> Option<PathBuf>
{
fs.read_dir_and_then(&dir, |it| {
it
.map(|entry| entry.unwrap())
.skip(2)
.filter_map(|entry| {
let is_file = entry.file_type().is_file();
if is_file {
if PathBuf::from(entry.file_name()) == PathBuf::from(filename) {
Some(PathBuf::from(entry.path()))
} else {
None
}
} else {
recursively_locate(fs, PathBuf::from(entry.path()), filename)
}
})
.next()
.ok_or(littlefs2::io::Error::Io)
}).ok()
}
let path = recursively_locate(fs, dir, &filename)
.map(|path| self.client_path(&path));
Ok(path)
}
}