Skip to content

Commit 997720f

Browse files
Expose enhanced iteration syscall to the client and test them
1 parent 3807bf2 commit 997720f

File tree

6 files changed

+83
-35
lines changed

6 files changed

+83
-35
lines changed

src/api.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ generate_enums! {
4141
Hash: 12
4242
// TODO: add ReadDir{First,Next}, not loading data, if needed for efficiency
4343
ReadDirFilesFirst: 13
44-
ReadDirFilesNth: 31
44+
ReadDirFilesNth: 63
4545
ReadDirFilesNext: 14
4646
ReadFile: 15
4747
Metadata: 26
@@ -64,7 +64,7 @@ generate_enums! {
6464

6565
// // CreateDir, <-- implied by WriteFile
6666
ReadDirFirst: 31 // <-- gets Option<FileType> to restrict to just dir/file DirEntries,
67-
ReadDirNth: 31
67+
ReadDirNth: 64
6868
ReadDirNext: 32 // <-- gets Option<FileType> to restrict to just dir/file DirEntries,
6969
// // returns simplified Metadata
7070
// // ReadDirFilesFirst: 23 // <-- returns contents
@@ -442,7 +442,7 @@ pub mod reply {
442442
- entry: Option<DirEntry>
443443

444444
ReadDirNth:
445-
- data: Option<Message>
445+
- entry: Option<DirEntry>
446446

447447
ReadDirNext:
448448
- entry: Option<DirEntry>

src/api/macros.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@ macro_rules! generate_enums {
22
($($(#[$attr:meta])? $which:ident: $index:literal)*) => {
33

44
#[derive(Clone, Eq, PartialEq, Debug)]
5+
#[repr(u8)]
56
#[allow(clippy::large_enum_variant)]
67
pub enum Request {
7-
DummyRequest, // for testing
8+
DummyRequest = 0, // for testing
89
$(
910
$(#[$attr])?
10-
$which(request::$which),
11+
$which(request::$which) = $index,
1112
)*
1213
}
1314

1415
#[derive(Clone, Eq, PartialEq, Debug)]
16+
#[repr(u8)]
1517
#[allow(clippy::large_enum_variant)]
1618
pub enum Reply {
1719
DummyReply, // for testing

src/client.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,19 @@ pub trait FilesystemClient: PollClient {
569569
})
570570
}
571571

572+
fn read_dir_nth(
573+
&mut self,
574+
location: Location,
575+
dir: PathBuf,
576+
start_at: usize,
577+
) -> ClientResult<'_, reply::ReadDirNth, Self> {
578+
self.request(request::ReadDirNth {
579+
location,
580+
dir,
581+
start_at,
582+
})
583+
}
584+
572585
fn read_dir_next(&mut self) -> ClientResult<'_, reply::ReadDirNext, Self> {
573586
self.request(request::ReadDirNext {})
574587
}
@@ -586,6 +599,21 @@ pub trait FilesystemClient: PollClient {
586599
})
587600
}
588601

602+
fn read_dir_files_nth(
603+
&mut self,
604+
location: Location,
605+
dir: PathBuf,
606+
start_at: usize,
607+
user_attribute: Option<UserAttribute>,
608+
) -> ClientResult<'_, reply::ReadDirFilesNth, Self> {
609+
self.request(request::ReadDirFilesNth {
610+
dir,
611+
location,
612+
start_at,
613+
user_attribute,
614+
})
615+
}
616+
589617
fn read_dir_files_next(&mut self) -> ClientResult<'_, reply::ReadDirFilesNext, Self> {
590618
self.request(request::ReadDirFilesNext {})
591619
}

src/service.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ impl<P: Platform> ServiceResources<P> {
365365

366366
}
367367
};
368-
Ok(Reply::ReadDirFirst(reply::ReadDirFirst { entry: maybe_entry } ))
368+
Ok(Reply::ReadDirNth(reply::ReadDirNth { entry: maybe_entry } ))
369369
}
370370

371371
Request::ReadDirNext(_request) => {
@@ -416,7 +416,7 @@ impl<P: Platform> ServiceResources<P> {
416416
None
417417
}
418418
};
419-
Ok(Reply::ReadDirFilesFirst(reply::ReadDirFilesFirst { data: maybe_data } ))
419+
Ok(Reply::ReadDirFilesNth(reply::ReadDirFilesNth { data: maybe_data } ))
420420
}
421421

422422
Request::ReadDirFilesNext(_request) => {

src/store/filestore.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ use crate::{
66
Bytes,
77
};
88

9-
#[derive(Clone)]
9+
#[derive(Clone, Debug)]
1010
pub struct ReadDirState {
1111
real_dir: PathBuf,
1212
location: Location,
1313
position: DirIterationTell,
1414
}
1515

16-
#[derive(Clone)]
16+
#[derive(Clone, Debug)]
1717
pub struct ReadDirFilesState {
1818
real_dir: PathBuf,
1919
position: DirIterationTell,

tests/filesystem.rs

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -55,34 +55,52 @@ fn escape_namespace_root() {
5555
}
5656

5757
fn iterating(location: Location) {
58-
client::get(|client| {
59-
syscall!(client.write_file(
60-
location,
61-
PathBuf::from("foo"),
62-
Bytes::from_slice(b"foo").unwrap(),
63-
None
64-
));
65-
syscall!(client.write_file(
66-
location,
67-
PathBuf::from("bar"),
68-
Bytes::from_slice(b"bar").unwrap(),
69-
None
70-
));
71-
let first_entry = syscall!(client.read_dir_first(location, PathBuf::from(""), None))
72-
.entry
73-
.unwrap();
74-
assert_eq!(first_entry.file_name(), "bar");
58+
for count in [0, 1, 10, 20] {
59+
let files: Vec<_> = (0..count).map(|i| format!("file{i:04}")).collect();
60+
client::get(|client| {
61+
// Setup filesystem
62+
for file in &files {
63+
syscall!(client.write_file(
64+
location,
65+
PathBuf::from(&**file),
66+
Bytes::from_slice(file.as_bytes()).unwrap(),
67+
None
68+
));
69+
}
70+
71+
// Iteration over entries (filenames)
72+
for i in 0..count {
73+
if let Some(f) = files.get(i) {
74+
let entry = syscall!(client.read_dir_nth(location, PathBuf::new(), i))
75+
.entry
76+
.unwrap();
77+
assert_eq!(entry.path().as_ref(), f);
78+
}
79+
80+
for j in i + 1..count {
81+
let entry = syscall!(client.read_dir_next()).entry.unwrap();
82+
assert_eq!(entry.path().as_ref(), &files[j]);
83+
}
84+
assert!(syscall!(client.read_dir_next()).entry.is_none());
85+
}
7586

76-
let next_entry = syscall!(client.read_dir_next()).entry.unwrap();
77-
assert_eq!(next_entry.file_name(), "foo");
87+
for i in 0..count {
88+
if let Some(f) = files.get(i) {
89+
let data =
90+
syscall!(client.read_dir_files_nth(location, PathBuf::new(), i, None))
91+
.data
92+
.unwrap();
93+
assert_eq!(data, f.as_bytes());
94+
}
7895

79-
let first_data = syscall!(client.read_dir_files_first(location, PathBuf::from(""), None))
80-
.data
81-
.unwrap();
82-
assert_eq!(first_data, b"bar");
83-
let next_data = syscall!(client.read_dir_files_next()).data.unwrap();
84-
assert_eq!(next_data, b"foo");
85-
});
96+
for j in i + 1..count {
97+
let data = syscall!(client.read_dir_files_next()).data.unwrap();
98+
assert_eq!(data, files[j].as_bytes());
99+
}
100+
assert!(syscall!(client.read_dir_files_next()).data.is_none());
101+
}
102+
});
103+
}
86104
}
87105

88106
#[test]

0 commit comments

Comments
 (0)