Skip to content

Commit

Permalink
Try #2546:
Browse files Browse the repository at this point in the history
  • Loading branch information
bors[bot] authored Sep 2, 2021
2 parents 378550a + e19aed1 commit 93585cb
Show file tree
Hide file tree
Showing 16 changed files with 306 additions and 103 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 16 additions & 7 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,27 @@ fn main() -> anyhow::Result<()> {
buffer: String::new(),
path: vec![],
};
let wasi_versions = ["unstable", "snapshot1"];

with_test_module(&mut wasitests, "wasitests", |wasitests| {
for wasi_version in &wasi_versions {
for wasi_version in &["unstable", "snapshot1"] {
with_test_module(wasitests, wasi_version, |wasitests| {
let _wasi_tests = test_directory(
wasitests,
format!("tests/wasi-wast/wasi/{}", wasi_version),
wasi_processor,
)?;
for (wasi_filesystem_test_name, wasi_filesystem_kind) in &[
("host_fs", "WasiFileSystemKind::Host"),
("mem_fs", "WasiFileSystemKind::InMemory"),
] {
with_test_module(wasitests, wasi_filesystem_test_name, |wasitests| {
test_directory(
wasitests,
format!("tests/wasi-wast/wasi/{}", wasi_version),
|out, path| wasi_processor(out, path, wasi_filesystem_kind),
)
})?;
}

Ok(())
})?;
}

Ok(())
})?;

Expand Down
32 changes: 25 additions & 7 deletions lib/vfs/src/mem_fs/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl VirtualFile for FileHandle {
};

match fs.storage.get(self.inode) {
Some(Node::File { file, .. }) => file.buffer.len().try_into().unwrap_or(0),
Some(Node::File { file, .. }) => file.len().try_into().unwrap_or(0),
_ => 0,
}
}
Expand All @@ -107,9 +107,11 @@ impl VirtualFile for FileHandle {
.map_err(|_| FsError::Lock)?;

match fs.storage.get_mut(self.inode) {
Some(Node::File { file, .. }) => file
.buffer
.resize(new_size.try_into().map_err(|_| FsError::UnknownError)?, 0),
Some(Node::File { file, metadata, .. }) => {
file.buffer
.resize(new_size.try_into().map_err(|_| FsError::UnknownError)?, 0);
metadata.len = new_size;
}
_ => return Err(FsError::NotAFile),
}

Expand Down Expand Up @@ -561,8 +563,8 @@ impl Write for FileHandle {
io::Error::new(io::ErrorKind::Other, "failed to acquire a write lock")
})?;

let file = match fs.storage.get_mut(self.inode) {
Some(Node::File { file, .. }) => file,
let (file, metadata) = match fs.storage.get_mut(self.inode) {
Some(Node::File { file, metadata, .. }) => (file, metadata),
_ => {
return Err(io::Error::new(
io::ErrorKind::NotFound,
Expand All @@ -571,7 +573,11 @@ impl Write for FileHandle {
}
};

file.write(buf)
let bytes_written = file.write(buf)?;

metadata.len = file.len().try_into().unwrap();

Ok(bytes_written)
}

fn flush(&mut self) -> io::Result<()> {
Expand Down Expand Up @@ -687,6 +693,10 @@ mod test_read_write_seek {
.open(path!("/foo.txt"))
.expect("failed to create a new file");

assert!(
matches!(fs.metadata(path!("/foo.txt")), Ok(Metadata { len: 0, .. })),
"checking the `metadata.len` is 0",
);
assert!(
matches!(file.write(b"foobarbazqux"), Ok(12)),
"writing `foobarbazqux`",
Expand Down Expand Up @@ -715,6 +725,10 @@ mod test_read_write_seek {
"reading more bytes than available",
);
assert_eq!(buffer[..12], b"foobarbazqux"[..], "checking the 12 bytes");
assert!(
matches!(fs.metadata(path!("/foo.txt")), Ok(Metadata { len: 12, .. })),
"checking the `metadata.len` is 0",
);
}

#[test]
Expand Down Expand Up @@ -847,6 +861,10 @@ impl File {
self.buffer.clear();
self.cursor = 0;
}

pub(super) fn len(&self) -> usize {
self.buffer.len()
}
}

impl Read for File {
Expand Down
1 change: 1 addition & 0 deletions lib/vfs/src/mem_fs/file_opener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ impl crate::FileOpener for FileOpener {
// Truncate if needed.
if truncate {
file.truncate();
metadata.len = 0;
}

// Move the cursor to the end if needed.
Expand Down
59 changes: 42 additions & 17 deletions lib/vfs/src/mem_fs/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,7 @@ impl crate::FileSystem for FileSystem {
}

fn rename(&self, from: &Path, to: &Path) -> Result<()> {
let (
(position_of_from, inode, inode_of_from_parent),
(inode_of_to_parent, name_of_to_directory),
) = {
let ((position_of_from, inode, inode_of_from_parent), (inode_of_to_parent, name_of_to)) = {
// Read lock.
let fs = self.inner.try_read().map_err(|_| FsError::Lock)?;

Expand All @@ -172,28 +169,26 @@ impl crate::FileSystem for FileSystem {
let parent_of_from = from.parent().ok_or(FsError::BaseNotDirectory)?;
let parent_of_to = to.parent().ok_or(FsError::BaseNotDirectory)?;

// Check the directory names.
let name_of_from_directory = from
// Check the names.
let name_of_from = from
.file_name()
.ok_or(FsError::InvalidInput)?
.to_os_string();
let name_of_to_directory = to.file_name().ok_or(FsError::InvalidInput)?.to_os_string();
let name_of_to = to.file_name().ok_or(FsError::InvalidInput)?.to_os_string();

// Find the parent inodes.
let inode_of_from_parent = fs.inode_of_parent(parent_of_from)?;
let inode_of_to_parent = fs.inode_of_parent(parent_of_to)?;

// Get the child indexes to update in the parent nodes, in
// addition to the inode of the directory to update.
let (position_of_from, inode) = fs.from_parent_get_position_and_inode_of_directory(
inode_of_from_parent,
&name_of_from_directory,
DirectoryMustBeEmpty::No,
)?;
let (position_of_from, inode) = fs
.from_parent_get_position_and_inode(inode_of_from_parent, &name_of_from)?
.ok_or(FsError::NotAFile)?;

(
(position_of_from, inode, inode_of_from_parent),
(inode_of_to_parent, name_of_to_directory),
(inode_of_to_parent, name_of_to),
)
};

Expand All @@ -203,14 +198,14 @@ impl crate::FileSystem for FileSystem {

// Update the directory name, and update the modified
// time.
fs.update_node_name(inode, name_of_to_directory)?;
fs.update_node_name(inode, name_of_to)?;

// Remove the directory from its parent, and update the
// Remove the file from its parent, and update the
// modified time.
fs.remove_child_from_node(inode_of_from_parent, position_of_from)?;

// Add the directory to its new parent, and update the
// modified time.
// Add the file to its new parent, and update the modified
// time.
fs.add_child_to_node(inode_of_to_parent, inode)?;
}

Expand Down Expand Up @@ -399,6 +394,35 @@ impl FileSystemInner {
}
}

/// From the inode of a parent node (so, a directory), returns the
/// child index of `name_of` along with its inode, whatever the
/// type of inode is (directory or file).
fn from_parent_get_position_and_inode(
&self,
inode_of_parent: Inode,
name_of: &OsString,
) -> Result<Option<(usize, Inode)>> {
match self.storage.get(inode_of_parent) {
Some(Node::Directory { children, .. }) => children
.iter()
.enumerate()
.filter_map(|(nth, inode)| self.storage.get(*inode).map(|node| (nth, node)))
.find_map(|(nth, node)| match node {
Node::File { inode, name, .. } | Node::Directory { inode, name, .. }
if name.as_os_str() == name_of =>
{
Some(Some((nth, *inode)))
}

_ => None,
})
.or(Some(None))
.ok_or(FsError::InvalidInput),

_ => Err(FsError::BaseNotDirectory),
}
}

/// Set a new name for the node represented by `inode`.
pub(super) fn update_node_name(&mut self, inode: Inode, new_name: OsString) -> Result<()> {
let node = self.storage.get_mut(inode).ok_or(FsError::UnknownError)?;
Expand Down Expand Up @@ -1248,6 +1272,7 @@ mod test_filesystem {
}
}

#[allow(dead_code)] // The `No` variant.
pub(super) enum DirectoryMustBeEmpty {
Yes,
No,
Expand Down
3 changes: 3 additions & 0 deletions lib/wasi/src/state/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -554,9 +554,12 @@ impl PreopenDirBuilder {
}
let path = self.path.clone().unwrap();

/*
if !path.exists() {
return Err(WasiStateCreationError::PreopenedDirectoryNotFound(path));
}
*/

if let Some(alias) = &self.alias {
validate_mapped_dir_alias(alias)?;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/wasi/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ impl WasiFs {
&path.to_string_lossy(),
&alias
);
let cur_dir_metadata = path.metadata().map_err(|e| {
let cur_dir_metadata = wasi_fs.fs_backing.metadata(path).map_err(|e| {
format!(
"Could not get metadata for file {:?}: {}",
path,
Expand Down
1 change: 1 addition & 0 deletions tests/compilers/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ mod wast;
pub use crate::config::{Compiler, Config, Engine};
pub use crate::wasi::run_wasi;
pub use crate::wast::run_wast;
pub use wasmer_wast::WasiFileSystemKind;
24 changes: 16 additions & 8 deletions tests/compilers/wasi.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
use std::fs::File;
use std::io::Read;
use wasmer_wast::WasiTest;
use wasmer_wast::{WasiFileSystemKind, WasiTest};

// The generated tests (from build.rs) look like:
// #[cfg(test)]
// mod singlepass {
// mod spec {
// #[test]
// fn address() -> anyhow::Result<()> {
// crate::run_wast("tests/spectests/address.wast", "singlepass")
// mod [compiler] {
// mod [spec] {
// mod [vfs] {
// #[test]
// fn [test_name]() -> anyhow::Result<()> {
// crate::run_wasi("tests/spectests/[test_name].wast", "[compiler]", WasiFileSystemKind::[vfs])
// }
// }
// }
// }
include!(concat!(env!("OUT_DIR"), "/generated_wasitests.rs"));

pub fn run_wasi(config: crate::Config, wast_path: &str, base_dir: &str) -> anyhow::Result<()> {
pub fn run_wasi(
config: crate::Config,
wast_path: &str,
base_dir: &str,
filesystem_kind: WasiFileSystemKind,
) -> anyhow::Result<()> {
println!("Running wasi wast `{}`", wast_path);
let store = config.store();

Expand All @@ -27,7 +34,8 @@ pub fn run_wasi(config: crate::Config, wast_path: &str, base_dir: &str) -> anyho
let tokens = WasiTest::lex_string(&source)?;
let wasi_test = WasiTest::parse_tokens(&tokens)?;

let succeeded = wasi_test.run(&store, base_dir)?;
let succeeded = wasi_test.run(&store, base_dir, filesystem_kind)?;

assert!(succeeded);

Ok(())
Expand Down
12 changes: 7 additions & 5 deletions tests/compilers/wast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ use wasmer_wast::Wast;

// The generated tests (from build.rs) look like:
// #[cfg(test)]
// mod singlepass {
// mod spec {
// #[test]
// fn address() -> anyhow::Result<()> {
// crate::run_wast("tests/spectests/address.wast", "singlepass")
// mod [compiler] {
// mod [spec] {
// mod [vfs] {
// #[test]
// fn [test_name]() -> anyhow::Result<()> {
// crate::run_wasi("tests/spectests/[test_name].wast", "[compiler]", WasiFileSystemKind::[vfs])
// }
// }
// }
// }
Expand Down
Loading

0 comments on commit 93585cb

Please sign in to comment.