Browse Source

add a readdir unit test to wasi-cap-std-sync

pull/2487/head
Pat Hickey 4 years ago
parent
commit
dec0038ba0
  1. 62
      crates/wasi-common/cap-std-sync/src/dir.rs
  2. 1
      crates/wasi-common/src/dir.rs
  3. 4
      crates/wasi-common/src/file.rs

62
crates/wasi-common/cap-std-sync/src/dir.rs

@ -259,6 +259,9 @@ fn convert_systimespec(t: Option<wasi_common::SystemTimeSpec>) -> Option<SystemT
#[cfg(test)]
mod test {
use super::Dir;
use std::collections::HashMap;
use wasi_common::dir::{ReaddirCursor, ReaddirEntity, WasiDir};
use wasi_common::file::{FdFlags, FileType, OFlags};
#[test]
fn scratch_dir() {
let tempdir = tempfile::Builder::new()
@ -271,4 +274,63 @@ mod test {
wasi_common::WasiDir::open_dir(&preopen_dir, false, ".")
.expect("open the same directory via WasiDir abstraction");
}
#[test]
fn readdir() {
let tempdir = tempfile::Builder::new()
.prefix("cap-std-sync")
.tempdir()
.expect("create temporary dir");
let preopen_dir = unsafe { cap_std::fs::Dir::open_ambient_dir(tempdir.path()) }
.expect("open ambient temporary dir");
let preopen_dir = Dir::from_cap_std(preopen_dir);
let entities = readdir_into_map(&preopen_dir);
assert_eq!(
entities.len(),
2,
"should just be . and .. in empty dir: {:?}",
entities
);
assert!(entities.get(".").is_some());
assert!(entities.get("..").is_some());
preopen_dir
.open_file(
false,
"file1",
OFlags::CREATE,
true,
false,
FdFlags::empty(),
)
.expect("create file1");
let entities = readdir_into_map(&preopen_dir);
assert_eq!(entities.len(), 3, "should be ., .., file1 {:?}", entities);
assert_eq!(
entities.get(".").expect(". entry").filetype,
FileType::Directory
);
assert_eq!(
entities.get("..").expect(".. entry").filetype,
FileType::Directory
);
assert_eq!(
entities.get("file1").expect("file1 entry").filetype,
FileType::RegularFile
);
}
fn readdir_into_map(dir: &dyn WasiDir) -> HashMap<String, ReaddirEntity> {
let mut out = HashMap::new();
for readdir_result in dir
.readdir(ReaddirCursor::from(0))
.expect("readdir succeeds")
{
let (entity, name) = readdir_result.expect("readdir entry is valid");
out.insert(name, entity);
}
out
}
}

1
crates/wasi-common/src/dir.rs

@ -162,6 +162,7 @@ impl TableDirExt for crate::table::Table {
}
}
#[derive(Debug, Clone)]
pub struct ReaddirEntity {
pub next: ReaddirCursor,
pub inode: u64,

4
crates/wasi-common/src/file.rs

@ -30,7 +30,7 @@ pub trait WasiFile {
fn num_ready_bytes(&self) -> Result<u64, Error>; // read op
}
#[derive(Debug, Copy, Clone)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum FileType {
Unknown,
BlockDevice,
@ -62,7 +62,7 @@ bitflags! {
}
}
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Filestat {
pub device_id: u64,
pub inode: u64,

Loading…
Cancel
Save