finish implementation of wasi::fd_seek, fix bug in filestat

This commit is contained in:
Mark McCaskey 2019-07-12 15:10:16 -07:00
parent bd2a082a78
commit cbac3ed92d
16 changed files with 115 additions and 8 deletions

View File

@ -32,7 +32,6 @@ pub fn compile(file: &str, ignores: &HashSet<String>) -> Option<String> {
};
Command::new("rustc")
.arg("+nightly")
.arg(file)
.arg("-o")
.arg(&normalized_name)

View File

@ -54,10 +54,9 @@ macro_rules! assert_wasi_output {
let expected_output = include_str!($expected);
assert!(
output.contains(expected_output),
assert_cond,
"Output: `{}` does not contain expected output: `{}`",
output,
expected_output
output, expected_output
);
}};
}

View File

@ -0,0 +1,10 @@
#[test]
fn test_fseek() {
assert_wasi_output!(
"../../wasitests/fseek.wasm",
"fseek",
vec![],
vec![],
"../../wasitests/fseek.out"
);
}

View File

@ -9,6 +9,7 @@ mod create_dir;
mod envvar;
mod file_metadata;
mod fs_sandbox_test;
mod fseek;
mod hello;
mod mapdir;
mod quine;

Binary file not shown.

View File

@ -0,0 +1,11 @@
SCENE III. A room in Polonius' h
ouse.
Enter LAERTES and OPH
And, sister, as the winds gi
r talk with the Lord Hamlet.
uits,
Breathing like sanctif
is is for all:
I would not,

View File

@ -0,0 +1,47 @@
// Args:
// mapdir: .:wasitests/test_fs/hamlet
use std::fs;
use std::io::{Read, Seek, SeekFrom};
use std::path::PathBuf;
fn main() {
#[cfg(not(target_os = "wasi"))]
let mut base = PathBuf::from("wasitests/test_fs/hamlet");
#[cfg(target_os = "wasi")]
let mut base = PathBuf::from(".");
base.push("act1/scene3.txt");
let mut file = fs::File::open(&base).expect("Could not open file");
let mut buffer = [0u8; 32];
assert_eq!(file.read(&mut buffer).unwrap(), 32);
let str_val = std::str::from_utf8(&buffer[..]).unwrap();
println!("{}", str_val);
assert_eq!(file.read(&mut buffer).unwrap(), 32);
let str_val = std::str::from_utf8(&buffer[..]).unwrap();
println!("{}", str_val);
assert_eq!(file.seek(SeekFrom::Start(123)).unwrap(), 123);
assert_eq!(file.read(&mut buffer).unwrap(), 32);
let str_val = std::str::from_utf8(&buffer[..]).unwrap();
println!("{}", str_val);
assert_eq!(file.seek(SeekFrom::End(-123)).unwrap(), 6617);
assert_eq!(file.read(&mut buffer).unwrap(), 32);
let str_val = std::str::from_utf8(&buffer[..]).unwrap();
println!("{}", str_val);
assert_eq!(file.seek(SeekFrom::Current(-250)).unwrap(), 6399);
assert_eq!(file.read(&mut buffer).unwrap(), 32);
let str_val = std::str::from_utf8(&buffer[..]).unwrap();
println!("{}", str_val);
assert_eq!(file.seek(SeekFrom::Current(50)).unwrap(), 6481);
assert_eq!(file.read(&mut buffer).unwrap(), 32);
let str_val = std::str::from_utf8(&buffer[..]).unwrap();
println!("{}", str_val);
}

Binary file not shown.

Binary file not shown.

View File

@ -4,9 +4,9 @@
use std::fs;
fn main() {
#[cfg(not(target = "wasi"))]
#[cfg(not(target_os = "wasi"))]
let read_dir = fs::read_dir("wasitests/test_fs/hamlet").unwrap();
#[cfg(target = "wasi")]
#[cfg(target_os = "wasi")]
let read_dir = fs::read_dir(".").unwrap();
let mut out = vec![];
for entry in read_dir {

Binary file not shown.

Binary file not shown.

View File

@ -903,7 +903,28 @@ pub fn fd_seek(
// TODO: handle case if fd is a dir?
match whence {
__WASI_WHENCE_CUR => fd_entry.offset = (fd_entry.offset as i64 + offset) as u64,
__WASI_WHENCE_END => unimplemented!("__WASI__WHENCE_END in wasi::fd_seek"),
__WASI_WHENCE_END => {
use std::io::SeekFrom;
match state.fs.inodes[fd_entry.inode].kind {
Kind::File { ref mut handle } => {
let end = wasi_try!(handle.seek(SeekFrom::End(0)).ok().ok_or(__WASI_EIO));
// TODO: handle case if fd_entry.offset uses 64 bits of a u64
fd_entry.offset = (end as i64 + offset) as u64;
}
Kind::Symlink { .. } => {
unimplemented!("wasi::fd_seek not implemented for symlinks")
}
Kind::Dir { .. } => {
// TODO: check this
return __WASI_EINVAL;
}
Kind::Buffer { .. } => {
// seeking buffers probably makes sense
// TODO: implement this
return __WASI_EINVAL;
}
}
}
__WASI_WHENCE_SET => fd_entry.offset = offset as u64,
_ => return __WASI_EINVAL,
}
@ -1235,6 +1256,7 @@ pub fn path_filestat_get(
let last_segment = path_vec.last().unwrap();
cumulative_path.push(last_segment);
// read it from wasi FS cache first, otherwise check host system
if entries.contains_key(last_segment) {
state.fs.inodes[entries[last_segment]].stat
} else {
@ -1244,7 +1266,25 @@ pub fn path_filestat_get(
}
let final_path_metadata =
wasi_try!(cumulative_path.metadata().map_err(|_| __WASI_EIO));
wasi_try!(get_stat_for_kind(&state.fs.inodes[inode].kind).ok_or(__WASI_EIO))
let kind = if final_path_metadata.is_file() {
let file =
wasi_try!(std::fs::File::open(&cumulative_path).ok().ok_or(__WASI_EIO));
Kind::File {
handle: WasiFile::HostFile(file),
}
} else if final_path_metadata.is_dir() {
Kind::Dir {
parent: Some(inode),
// TODO: verify that this doesn't cause issues with relative paths
path: cumulative_path.clone(),
entries: Default::default(),
}
} else {
// TODO: check this
return __WASI_EINVAL;
};
wasi_try!(get_stat_for_kind(&kind).ok_or(__WASI_EIO))
}
}
_ => {