Skip to content

Commit

Permalink
Try #554:
Browse files Browse the repository at this point in the history
  • Loading branch information
bors[bot] committed Jul 12, 2019
2 parents bd2a082 + a608f3a commit a5635b8
Show file tree
Hide file tree
Showing 15 changed files with 113 additions and 5 deletions.
1 change: 0 additions & 1 deletion lib/wasi-tests/build/wasitests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions lib/wasi-tests/tests/wasitests/fseek.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#[test]
fn test_fseek() {
assert_wasi_output!(
"../../wasitests/fseek.wasm",
"fseek",
vec![],
vec![],
"../../wasitests/fseek.out"
);
}
1 change: 1 addition & 0 deletions lib/wasi-tests/tests/wasitests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 modified lib/wasi-tests/wasitests/create_dir.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/envvar.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/file_metadata.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/fs_sandbox_test.wasm
Binary file not shown.
11 changes: 11 additions & 0 deletions lib/wasi-tests/wasitests/fseek.out
Original file line number Diff line number Diff line change
@@ -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,
47 changes: 47 additions & 0 deletions lib/wasi-tests/wasitests/fseek.rs
Original file line number Diff line number Diff line change
@@ -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 added lib/wasi-tests/wasitests/fseek.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/hello.wasm
Binary file not shown.
4 changes: 2 additions & 2 deletions lib/wasi-tests/wasitests/mapdir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Binary file modified lib/wasi-tests/wasitests/mapdir.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/quine.wasm
Binary file not shown.
44 changes: 42 additions & 2 deletions lib/wasi/src/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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))
}
}
_ => {
Expand Down

0 comments on commit a5635b8

Please sign in to comment.