From 133344e880da96f719d2e76fc4233fe0b4b4c7e8 Mon Sep 17 00:00:00 2001 From: Pat Hickey Date: Sat, 30 Jan 2021 13:53:56 -0800 Subject: [PATCH] woo it passes six tests even though its awful --- Cargo.lock | 1 + crates/wasi-common/virtfs/Cargo.toml | 1 + crates/wasi-common/virtfs/src/lib.rs | 26 ++++++++++++++++---------- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ae57147bd3..93e3572b5a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3131,6 +3131,7 @@ version = "0.22.0" dependencies = [ "anyhow", "cap-std", + "tracing", "wasi-common", ] diff --git a/crates/wasi-common/virtfs/Cargo.toml b/crates/wasi-common/virtfs/Cargo.toml index 4a760434a6..0be09ac1e9 100644 --- a/crates/wasi-common/virtfs/Cargo.toml +++ b/crates/wasi-common/virtfs/Cargo.toml @@ -16,3 +16,4 @@ publish = false wasi-common = { path = "../", version = "0.22.0" } anyhow = "1.0" cap-std = "0.12" +tracing = "0.1.19" diff --git a/crates/wasi-common/virtfs/src/lib.rs b/crates/wasi-common/virtfs/src/lib.rs index 8fcd868dec..52fe3652e9 100644 --- a/crates/wasi-common/virtfs/src/lib.rs +++ b/crates/wasi-common/virtfs/src/lib.rs @@ -8,6 +8,7 @@ use std::io::{Cursor, IoSlice, IoSliceMut, Read, Seek, SeekFrom, Write}; use std::ops::Deref; use std::path::PathBuf; use std::rc::{Rc, Weak}; +use tracing::trace; use wasi_common::{ clocks::WasiSystemClock, dir::{ReaddirCursor, ReaddirEntity, WasiDir}, @@ -283,7 +284,7 @@ impl WasiFile for File { pub struct Dir(Rc>); impl Dir { - fn at_path(&self, path: &str, f: F) -> Result + fn at_path(&self, path: &str, accept_trailing_slash: bool, f: F) -> Result where F: FnOnce(&Dir, &str) -> Result, { @@ -292,12 +293,16 @@ impl Dir { let dirname = &path[..slash_index]; let rest = &path[slash_index + 1..]; if rest == "" { - return Err(Error::invalid_argument() - .context("empty filename, probably related to trailing slashes")); + if accept_trailing_slash { + return f(self, path); + } else { + return Err(Error::invalid_argument() + .context("empty filename, probably related to trailing slashes")); + } } if let Some(inode) = self.0.borrow().contents.get(dirname) { match inode { - Inode::Dir(d) => Dir(d.clone()).at_path(rest, f), + Inode::Dir(d) => Dir(d.clone()).at_path(rest, accept_trailing_slash, f), Inode::File { .. } => Err(Error::not_found()), } } else { @@ -308,6 +313,9 @@ impl Dir { } } fn child_dir(&self, name: &str) -> Result>, Error> { + if name == "." { + return Ok(self.0.clone()); + } match self.0.borrow().contents.get(name) { Some(Inode::Dir(d)) => Ok(d.clone()), _ => Err(Error::not_found()), @@ -336,15 +344,13 @@ impl WasiDir for Dir { ) -> Result, Error> { let mode = if read && write { FileMode::ReadWrite - } else if read { - FileMode::ReadOnly } else if write { FileMode::WriteOnly } else { - return Err(Error::invalid_argument().context("must be read or write")); + FileMode::ReadOnly }; // XXX TERRIBLE CODE DUPLICATION HERE - self.at_path(path, |dir, filename| { + self.at_path(path, false, |dir, filename| { if oflags.contains(OFlags::CREATE | OFlags::EXCLUSIVE) { match dir.child_file(filename) { Err(_notfound) => { @@ -421,14 +427,14 @@ impl WasiDir for Dir { } fn open_dir(&self, _symlink_follow: bool, path: &str) -> Result, Error> { - self.at_path(path, |dir, dirname| { + self.at_path(path, true, |dir, dirname| { let d = dir.child_dir(dirname)?; Ok(Box::new(Dir(d)) as Box) }) } fn create_dir(&self, path: &str) -> Result<(), Error> { - self.at_path(path, |dir, dirname| { + self.at_path(path, true, |dir, dirname| { let d = dir.0.borrow(); let fs = d.fs.clone(); let serial = d.fs().fresh_serial();