Skip to content

Commit

Permalink
Expose enhanced iteration syscall to the client and test them
Browse files Browse the repository at this point in the history
  • Loading branch information
sosthene-nitrokey committed Apr 6, 2023
1 parent 3807bf2 commit 997720f
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 35 deletions.
6 changes: 3 additions & 3 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ generate_enums! {
Hash: 12
// TODO: add ReadDir{First,Next}, not loading data, if needed for efficiency
ReadDirFilesFirst: 13
ReadDirFilesNth: 31
ReadDirFilesNth: 63
ReadDirFilesNext: 14
ReadFile: 15
Metadata: 26
Expand All @@ -64,7 +64,7 @@ generate_enums! {

// // CreateDir, <-- implied by WriteFile
ReadDirFirst: 31 // <-- gets Option<FileType> to restrict to just dir/file DirEntries,
ReadDirNth: 31
ReadDirNth: 64
ReadDirNext: 32 // <-- gets Option<FileType> to restrict to just dir/file DirEntries,
// // returns simplified Metadata
// // ReadDirFilesFirst: 23 // <-- returns contents
Expand Down Expand Up @@ -442,7 +442,7 @@ pub mod reply {
- entry: Option<DirEntry>

ReadDirNth:
- data: Option<Message>
- entry: Option<DirEntry>

ReadDirNext:
- entry: Option<DirEntry>
Expand Down
6 changes: 4 additions & 2 deletions src/api/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ macro_rules! generate_enums {
($($(#[$attr:meta])? $which:ident: $index:literal)*) => {

#[derive(Clone, Eq, PartialEq, Debug)]
#[repr(u8)]
#[allow(clippy::large_enum_variant)]
pub enum Request {
DummyRequest, // for testing
DummyRequest = 0, // for testing
$(
$(#[$attr])?
$which(request::$which),
$which(request::$which) = $index,
)*
}

#[derive(Clone, Eq, PartialEq, Debug)]
#[repr(u8)]
#[allow(clippy::large_enum_variant)]
pub enum Reply {
DummyReply, // for testing
Expand Down
28 changes: 28 additions & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,19 @@ pub trait FilesystemClient: PollClient {
})
}

fn read_dir_nth(
&mut self,
location: Location,
dir: PathBuf,
start_at: usize,
) -> ClientResult<'_, reply::ReadDirNth, Self> {
self.request(request::ReadDirNth {
location,
dir,
start_at,
})
}

fn read_dir_next(&mut self) -> ClientResult<'_, reply::ReadDirNext, Self> {
self.request(request::ReadDirNext {})
}
Expand All @@ -586,6 +599,21 @@ pub trait FilesystemClient: PollClient {
})
}

fn read_dir_files_nth(
&mut self,
location: Location,
dir: PathBuf,
start_at: usize,
user_attribute: Option<UserAttribute>,
) -> ClientResult<'_, reply::ReadDirFilesNth, Self> {
self.request(request::ReadDirFilesNth {
dir,
location,
start_at,
user_attribute,
})
}

fn read_dir_files_next(&mut self) -> ClientResult<'_, reply::ReadDirFilesNext, Self> {
self.request(request::ReadDirFilesNext {})
}
Expand Down
4 changes: 2 additions & 2 deletions src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ impl<P: Platform> ServiceResources<P> {

}
};
Ok(Reply::ReadDirFirst(reply::ReadDirFirst { entry: maybe_entry } ))
Ok(Reply::ReadDirNth(reply::ReadDirNth { entry: maybe_entry } ))
}

Request::ReadDirNext(_request) => {
Expand Down Expand Up @@ -416,7 +416,7 @@ impl<P: Platform> ServiceResources<P> {
None
}
};
Ok(Reply::ReadDirFilesFirst(reply::ReadDirFilesFirst { data: maybe_data } ))
Ok(Reply::ReadDirFilesNth(reply::ReadDirFilesNth { data: maybe_data } ))
}

Request::ReadDirFilesNext(_request) => {
Expand Down
4 changes: 2 additions & 2 deletions src/store/filestore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ use crate::{
Bytes,
};

#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct ReadDirState {
real_dir: PathBuf,
location: Location,
position: DirIterationTell,
}

#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct ReadDirFilesState {
real_dir: PathBuf,
position: DirIterationTell,
Expand Down
70 changes: 44 additions & 26 deletions tests/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,34 +55,52 @@ fn escape_namespace_root() {
}

fn iterating(location: Location) {
client::get(|client| {
syscall!(client.write_file(
location,
PathBuf::from("foo"),
Bytes::from_slice(b"foo").unwrap(),
None
));
syscall!(client.write_file(
location,
PathBuf::from("bar"),
Bytes::from_slice(b"bar").unwrap(),
None
));
let first_entry = syscall!(client.read_dir_first(location, PathBuf::from(""), None))
.entry
.unwrap();
assert_eq!(first_entry.file_name(), "bar");
for count in [0, 1, 10, 20] {
let files: Vec<_> = (0..count).map(|i| format!("file{i:04}")).collect();
client::get(|client| {
// Setup filesystem
for file in &files {
syscall!(client.write_file(
location,
PathBuf::from(&**file),
Bytes::from_slice(file.as_bytes()).unwrap(),
None
));
}

// Iteration over entries (filenames)
for i in 0..count {
if let Some(f) = files.get(i) {
let entry = syscall!(client.read_dir_nth(location, PathBuf::new(), i))
.entry
.unwrap();
assert_eq!(entry.path().as_ref(), f);
}

for j in i + 1..count {
let entry = syscall!(client.read_dir_next()).entry.unwrap();
assert_eq!(entry.path().as_ref(), &files[j]);
}
assert!(syscall!(client.read_dir_next()).entry.is_none());
}

let next_entry = syscall!(client.read_dir_next()).entry.unwrap();
assert_eq!(next_entry.file_name(), "foo");
for i in 0..count {
if let Some(f) = files.get(i) {
let data =
syscall!(client.read_dir_files_nth(location, PathBuf::new(), i, None))
.data
.unwrap();
assert_eq!(data, f.as_bytes());
}

let first_data = syscall!(client.read_dir_files_first(location, PathBuf::from(""), None))
.data
.unwrap();
assert_eq!(first_data, b"bar");
let next_data = syscall!(client.read_dir_files_next()).data.unwrap();
assert_eq!(next_data, b"foo");
});
for j in i + 1..count {
let data = syscall!(client.read_dir_files_next()).data.unwrap();
assert_eq!(data, files[j].as_bytes());
}
assert!(syscall!(client.read_dir_files_next()).data.is_none());
}
});
}
}

#[test]
Expand Down

0 comments on commit 997720f

Please sign in to comment.