diff --git a/examples/plugin.rs b/examples/plugin.rs index 19f16e5287d..1f50108afe7 100644 --- a/examples/plugin.rs +++ b/examples/plugin.rs @@ -3,7 +3,7 @@ use wasmer_runtime::{func, imports, instantiate}; use wasmer_runtime_core::vm::Ctx; use wasmer_wasi::{ generate_import_object, - state::{self, WasiFile}, + state::{self, WasiFile, WasiFsError}, types, }; @@ -102,6 +102,16 @@ impl WasiFile for LoggingWrapper { fn size(&self) -> u64 { 0 } + fn set_len(&mut self, _len: u64) -> Result<(), WasiFsError> { + Ok(()) + } + fn unlink(&mut self) -> Result<(), WasiFsError> { + Ok(()) + } + fn bytes_available(&self) -> Result { + // return an arbitrary amount + Ok(1024) + } } /// Called by the program when it wants to set itself up diff --git a/lib/wasi/src/state/mod.rs b/lib/wasi/src/state/mod.rs index 64a5aee84ca..43881620ad5 100644 --- a/lib/wasi/src/state/mod.rs +++ b/lib/wasi/src/state/mod.rs @@ -50,7 +50,7 @@ pub enum Kind { /// the open file, if it's open handle: Option>, /// The path on the host system where the file is located - /// This is deprecated and will be removed in 0.7.0 or a shortly thereafter + /// This is deprecated and will be removed soon path: PathBuf, }, Dir { diff --git a/lib/wasi/src/state/types.rs b/lib/wasi/src/state/types.rs index 0210f681463..83c0426c399 100644 --- a/lib/wasi/src/state/types.rs +++ b/lib/wasi/src/state/types.rs @@ -9,6 +9,7 @@ use std::{ path::PathBuf, time::SystemTime, }; +use wasmer_runtime_core::debug; /// Error type for external users #[derive(Copy, Clone, Debug, PartialEq, Eq)] @@ -122,58 +123,57 @@ impl WasiFsError { pub trait WasiFile: std::fmt::Debug + Write + Read + Seek { /// the last time the file was accessed in nanoseconds as a UNIX timestamp fn last_accessed(&self) -> __wasi_timestamp_t; + /// the last time the file was modified in nanoseconds as a UNIX timestamp fn last_modified(&self) -> __wasi_timestamp_t; + /// the time at which the file was created in nanoseconds as a UNIX timestamp fn created_time(&self) -> __wasi_timestamp_t; + /// set the last time the file was accessed in nanoseconds as a UNIX timestamp - // TODO: stablize this in 0.7.0 by removing default impl fn set_last_accessed(&self, _last_accessed: __wasi_timestamp_t) { - panic!("Default implementation for compatibilty in the 0.6.X releases; this will be removed in 0.7.0. Please implement WasiFile::set_last_accessed for your type before then"); + debug!("{:?} did nothing in WasiFile::set_last_accessed due to using the default implementation", self); } + /// set the last time the file was modified in nanoseconds as a UNIX timestamp - // TODO: stablize this in 0.7.0 by removing default impl fn set_last_modified(&self, _last_modified: __wasi_timestamp_t) { - panic!("Default implementation for compatibilty in the 0.6.X releases; this will be removed in 0.7.0. Please implement WasiFile::set_last_modified for your type before then"); + debug!("{:?} did nothing in WasiFile::set_last_modified due to using the default implementation", self); } + /// set the time at which the file was created in nanoseconds as a UNIX timestamp - // TODO: stablize this in 0.7.0 by removing default impl fn set_created_time(&self, _created_time: __wasi_timestamp_t) { - panic!("Default implementation for compatibilty in the 0.6.X releases; this will be removed in 0.7.0. Please implement WasiFile::set_created_time for your type before then"); + debug!( + "{:?} did nothing in WasiFile::set_created_time to using the default implementation", + self + ); } + /// the size of the file in bytes fn size(&self) -> u64; + /// Change the size of the file, if the `new_size` is greater than the current size /// the extra bytes will be allocated and zeroed - // TODO: stablize this in 0.7.0 by removing default impl - fn set_len(&mut self, _new_size: __wasi_filesize_t) -> Result<(), WasiFsError> { - panic!("Default implementation for compatibilty in the 0.6.X releases; this will be removed in 0.7.0. Please implement WasiFile::allocate for your type before then"); - } + fn set_len(&mut self, _new_size: __wasi_filesize_t) -> Result<(), WasiFsError>; /// Request deletion of the file - // TODO: break this out into a WasiPath trait which is dynamically in Kind::File - // this change can't be done until before release - fn unlink(&mut self) -> Result<(), WasiFsError> { - panic!("Default implementation for compatibilty in the 0.6.X releases; this will be removed in 0.7.0. Please implement WasiFile::unlink for your type before then"); - } + fn unlink(&mut self) -> Result<(), WasiFsError>; /// Store file contents and metadata to disk - // TODO: stablize this in 0.7.0 by removing default impl + /// Default implementation returns `Ok(())`. You should implement this method if you care + /// about flushing your cache to permanent storage fn sync_to_disk(&self) -> Result<(), WasiFsError> { - panic!("Default implementation for compatibilty in the 0.6.X releases; this will be removed in 0.7.0. Please implement WasiFile::sync_to_disk for your type before then"); + Ok(()) } /// Moves the file to a new location /// NOTE: the signature of this function will change before stabilization // TODO: stablizie this in 0.7.0 or 0.8.0 by removing default impl fn rename_file(&self, _new_name: &std::path::Path) -> Result<(), WasiFsError> { - panic!("Default implementation for compatibilty in the 0.6.X releases; this will be removed in 0.7.0 or 0.8.0. Please implement WasiFile::rename_file for your type before then"); + panic!("Default implementation for now as this method is unstable; this default implementation or this entire method may be removed in a future release."); } /// Returns the number of bytes available. This function must not block - fn bytes_available(&self) -> Result { - panic!("Default implementation for compatibilty in the 0.6.X releases; this will be removed in 0.7.0 or 0.8.0. Please implement WasiFile::bytes_available for your type before then"); - } + fn bytes_available(&self) -> Result; /// Used for polling. Default returns `None` because this method cannot be implemented for most types /// Returns the underlying host fd @@ -688,6 +688,13 @@ impl WasiFile for Stdout { fn size(&self) -> u64 { 0 } + fn set_len(&mut self, _new_size: __wasi_filesize_t) -> Result<(), WasiFsError> { + debug!("Calling WasiFile::set_len on stdout; this is probably a bug"); + Err(WasiFsError::PermissionDenied) + } + fn unlink(&mut self) -> Result<(), WasiFsError> { + Ok(()) + } fn bytes_available(&self) -> Result { // unwrap is safe because of get_raw_fd implementation @@ -772,6 +779,13 @@ impl WasiFile for Stderr { fn size(&self) -> u64 { 0 } + fn set_len(&mut self, _new_size: __wasi_filesize_t) -> Result<(), WasiFsError> { + debug!("Calling WasiFile::set_len on stderr; this is probably a bug"); + Err(WasiFsError::PermissionDenied) + } + fn unlink(&mut self) -> Result<(), WasiFsError> { + Ok(()) + } fn bytes_available(&self) -> Result { // unwrap is safe because of get_raw_fd implementation @@ -856,6 +870,14 @@ impl WasiFile for Stdin { fn size(&self) -> u64 { 0 } + fn set_len(&mut self, _new_size: __wasi_filesize_t) -> Result<(), WasiFsError> { + debug!("Calling WasiFile::set_len on stdin; this is probably a bug"); + Err(WasiFsError::PermissionDenied) + } + + fn unlink(&mut self) -> Result<(), WasiFsError> { + Ok(()) + } fn bytes_available(&self) -> Result { // unwrap is safe because of get_raw_fd implementation diff --git a/lib/wasi/src/syscalls/mod.rs b/lib/wasi/src/syscalls/mod.rs index 9801d4ac048..fb5a49664e0 100644 --- a/lib/wasi/src/syscalls/mod.rs +++ b/lib/wasi/src/syscalls/mod.rs @@ -1729,7 +1729,7 @@ pub fn path_open( path_to_symlink, relative_path, } => { - // I think this should return an error + // I think this should return an error (because symlinks should be resolved away by the path traversal) // TODO: investigate this unimplemented!("SYMLINKS IN PATH_OPEN"); } @@ -2202,7 +2202,7 @@ pub fn path_unlink_file( } else { // File is closed // problem with the abstraction, we can't call unlink because there's no handle - // TODO: replace this code in 0.7.0 + // TODO: replace this code wasi_try!(std::fs::remove_file(path).map_err(|_| __WASI_EIO)); } }