Path: blob/main/crates/wasi/src/filesystem.rs
1692 views
use crate::clocks::Datetime;1use crate::runtime::{AbortOnDropJoinHandle, spawn_blocking};2use anyhow::Context as _;3use cap_fs_ext::{FileTypeExt as _, MetadataExt as _};4use fs_set_times::SystemTimeSpec;5use std::collections::hash_map;6use std::sync::Arc;7use tracing::debug;8use wasmtime::component::{HasData, Resource, ResourceTable};910/// A helper struct which implements [`HasData`] for the `wasi:filesystem` APIs.11///12/// This can be useful when directly calling `add_to_linker` functions directly,13/// such as [`wasmtime_wasi::p2::bindings::filesystem::types::add_to_linker`] as14/// the `D` type parameter. See [`HasData`] for more information about the type15/// parameter's purpose.16///17/// When using this type you can skip the [`WasiFilesystemView`] trait, for18/// example.19///20/// # Examples21///22/// ```23/// use wasmtime::component::{Linker, ResourceTable};24/// use wasmtime::{Engine, Result, Config};25/// use wasmtime_wasi::filesystem::*;26///27/// struct MyStoreState {28/// table: ResourceTable,29/// filesystem: WasiFilesystemCtx,30/// }31///32/// fn main() -> Result<()> {33/// let mut config = Config::new();34/// config.async_support(true);35/// let engine = Engine::new(&config)?;36/// let mut linker = Linker::new(&engine);37///38/// wasmtime_wasi::p2::bindings::filesystem::types::add_to_linker::<MyStoreState, WasiFilesystem>(39/// &mut linker,40/// |state| WasiFilesystemCtxView {41/// table: &mut state.table,42/// ctx: &mut state.filesystem,43/// },44/// )?;45/// Ok(())46/// }47/// ```48pub struct WasiFilesystem;4950impl HasData for WasiFilesystem {51type Data<'a> = WasiFilesystemCtxView<'a>;52}5354#[derive(Clone, Default)]55pub struct WasiFilesystemCtx {56pub(crate) allow_blocking_current_thread: bool,57pub(crate) preopens: Vec<(Dir, String)>,58}5960pub struct WasiFilesystemCtxView<'a> {61pub ctx: &'a mut WasiFilesystemCtx,62pub table: &'a mut ResourceTable,63}6465pub trait WasiFilesystemView: Send {66fn filesystem(&mut self) -> WasiFilesystemCtxView<'_>;67}6869bitflags::bitflags! {70#[derive(Copy, Clone, Debug, PartialEq, Eq)]71pub struct FilePerms: usize {72const READ = 0b1;73const WRITE = 0b10;74}75}7677bitflags::bitflags! {78#[derive(Copy, Clone, Debug, PartialEq, Eq)]79pub struct OpenMode: usize {80const READ = 0b1;81const WRITE = 0b10;82}83}8485bitflags::bitflags! {86/// Permission bits for operating on a directory.87///88/// Directories can be limited to being readonly. This will restrict what89/// can be done with them, for example preventing creation of new files.90#[derive(Copy, Clone, Debug, PartialEq, Eq)]91pub struct DirPerms: usize {92/// This directory can be read, for example its entries can be iterated93/// over and files can be opened.94const READ = 0b1;9596/// This directory can be mutated, for example by creating new files97/// within it.98const MUTATE = 0b10;99}100}101102bitflags::bitflags! {103/// Flags determining the method of how paths are resolved.104#[derive(Copy, Clone, Debug, PartialEq, Eq)]105pub(crate) struct PathFlags: usize {106/// This directory can be read, for example its entries can be iterated107/// over and files can be opened.108const SYMLINK_FOLLOW = 0b1;109}110}111112bitflags::bitflags! {113/// Open flags used by `open-at`.114#[derive(Copy, Clone, Debug, PartialEq, Eq)]115pub(crate) struct OpenFlags: usize {116/// Create file if it does not exist, similar to `O_CREAT` in POSIX.117const CREATE = 0b1;118/// Fail if not a directory, similar to `O_DIRECTORY` in POSIX.119const DIRECTORY = 0b10;120/// Fail if file already exists, similar to `O_EXCL` in POSIX.121const EXCLUSIVE = 0b100;122/// Truncate file to size 0, similar to `O_TRUNC` in POSIX.123const TRUNCATE = 0b1000;124}125}126127bitflags::bitflags! {128/// Descriptor flags.129///130/// Note: This was called `fdflags` in earlier versions of WASI.131#[derive(Copy, Clone, Debug, PartialEq, Eq)]132pub(crate) struct DescriptorFlags: usize {133/// Read mode: Data can be read.134const READ = 0b1;135/// Write mode: Data can be written to.136const WRITE = 0b10;137/// Request that writes be performed according to synchronized I/O file138/// integrity completion. The data stored in the file and the file's139/// metadata are synchronized. This is similar to `O_SYNC` in POSIX.140///141/// The precise semantics of this operation have not yet been defined for142/// WASI. At this time, it should be interpreted as a request, and not a143/// requirement.144const FILE_INTEGRITY_SYNC = 0b100;145/// Request that writes be performed according to synchronized I/O data146/// integrity completion. Only the data stored in the file is147/// synchronized. This is similar to `O_DSYNC` in POSIX.148///149/// The precise semantics of this operation have not yet been defined for150/// WASI. At this time, it should be interpreted as a request, and not a151/// requirement.152const DATA_INTEGRITY_SYNC = 0b1000;153/// Requests that reads be performed at the same level of integrity154/// requested for writes. This is similar to `O_RSYNC` in POSIX.155///156/// The precise semantics of this operation have not yet been defined for157/// WASI. At this time, it should be interpreted as a request, and not a158/// requirement.159const REQUESTED_WRITE_SYNC = 0b10000;160/// Mutating directories mode: Directory contents may be mutated.161///162/// When this flag is unset on a descriptor, operations using the163/// descriptor which would create, rename, delete, modify the data or164/// metadata of filesystem objects, or obtain another handle which165/// would permit any of those, shall fail with `error-code::read-only` if166/// they would otherwise succeed.167///168/// This may only be set on directories.169const MUTATE_DIRECTORY = 0b100000;170}171}172173/// Error codes returned by functions, similar to `errno` in POSIX.174/// Not all of these error codes are returned by the functions provided by this175/// API; some are used in higher-level library layers, and others are provided176/// merely for alignment with POSIX.177#[cfg_attr(178windows,179expect(dead_code, reason = "on Windows, some of these are not used")180)]181pub(crate) enum ErrorCode {182/// Permission denied, similar to `EACCES` in POSIX.183Access,184/// Connection already in progress, similar to `EALREADY` in POSIX.185Already,186/// Bad descriptor, similar to `EBADF` in POSIX.187BadDescriptor,188/// Device or resource busy, similar to `EBUSY` in POSIX.189Busy,190/// File exists, similar to `EEXIST` in POSIX.191Exist,192/// File too large, similar to `EFBIG` in POSIX.193FileTooLarge,194/// Illegal byte sequence, similar to `EILSEQ` in POSIX.195IllegalByteSequence,196/// Operation in progress, similar to `EINPROGRESS` in POSIX.197InProgress,198/// Interrupted function, similar to `EINTR` in POSIX.199Interrupted,200/// Invalid argument, similar to `EINVAL` in POSIX.201Invalid,202/// I/O error, similar to `EIO` in POSIX.203Io,204/// Is a directory, similar to `EISDIR` in POSIX.205IsDirectory,206/// Too many levels of symbolic links, similar to `ELOOP` in POSIX.207Loop,208/// Too many links, similar to `EMLINK` in POSIX.209TooManyLinks,210/// Filename too long, similar to `ENAMETOOLONG` in POSIX.211NameTooLong,212/// No such file or directory, similar to `ENOENT` in POSIX.213NoEntry,214/// Not enough space, similar to `ENOMEM` in POSIX.215InsufficientMemory,216/// No space left on device, similar to `ENOSPC` in POSIX.217InsufficientSpace,218/// Not a directory or a symbolic link to a directory, similar to `ENOTDIR` in POSIX.219NotDirectory,220/// Directory not empty, similar to `ENOTEMPTY` in POSIX.221NotEmpty,222/// Not supported, similar to `ENOTSUP` and `ENOSYS` in POSIX.223Unsupported,224/// Value too large to be stored in data type, similar to `EOVERFLOW` in POSIX.225Overflow,226/// Operation not permitted, similar to `EPERM` in POSIX.227NotPermitted,228/// Broken pipe, similar to `EPIPE` in POSIX.229Pipe,230/// Invalid seek, similar to `ESPIPE` in POSIX.231InvalidSeek,232}233234fn datetime_from(t: std::time::SystemTime) -> Datetime {235// FIXME make this infallible or handle errors properly236Datetime::try_from(cap_std::time::SystemTime::from_std(t)).unwrap()237}238239/// The type of a filesystem object referenced by a descriptor.240///241/// Note: This was called `filetype` in earlier versions of WASI.242pub(crate) enum DescriptorType {243/// The type of the descriptor or file is unknown or is different from244/// any of the other types specified.245Unknown,246/// The descriptor refers to a block device inode.247BlockDevice,248/// The descriptor refers to a character device inode.249CharacterDevice,250/// The descriptor refers to a directory inode.251Directory,252/// The file refers to a symbolic link inode.253SymbolicLink,254/// The descriptor refers to a regular file inode.255RegularFile,256}257258impl From<cap_std::fs::FileType> for DescriptorType {259fn from(ft: cap_std::fs::FileType) -> Self {260if ft.is_dir() {261DescriptorType::Directory262} else if ft.is_symlink() {263DescriptorType::SymbolicLink264} else if ft.is_block_device() {265DescriptorType::BlockDevice266} else if ft.is_char_device() {267DescriptorType::CharacterDevice268} else if ft.is_file() {269DescriptorType::RegularFile270} else {271DescriptorType::Unknown272}273}274}275276/// File attributes.277///278/// Note: This was called `filestat` in earlier versions of WASI.279pub(crate) struct DescriptorStat {280/// File type.281pub type_: DescriptorType,282/// Number of hard links to the file.283pub link_count: u64,284/// For regular files, the file size in bytes. For symbolic links, the285/// length in bytes of the pathname contained in the symbolic link.286pub size: u64,287/// Last data access timestamp.288///289/// If the `option` is none, the platform doesn't maintain an access290/// timestamp for this file.291pub data_access_timestamp: Option<Datetime>,292/// Last data modification timestamp.293///294/// If the `option` is none, the platform doesn't maintain a295/// modification timestamp for this file.296pub data_modification_timestamp: Option<Datetime>,297/// Last file status-change timestamp.298///299/// If the `option` is none, the platform doesn't maintain a300/// status-change timestamp for this file.301pub status_change_timestamp: Option<Datetime>,302}303304impl From<cap_std::fs::Metadata> for DescriptorStat {305fn from(meta: cap_std::fs::Metadata) -> Self {306Self {307type_: meta.file_type().into(),308link_count: meta.nlink(),309size: meta.len(),310data_access_timestamp: meta.accessed().map(|t| datetime_from(t.into_std())).ok(),311data_modification_timestamp: meta.modified().map(|t| datetime_from(t.into_std())).ok(),312status_change_timestamp: meta.created().map(|t| datetime_from(t.into_std())).ok(),313}314}315}316317/// A 128-bit hash value, split into parts because wasm doesn't have a318/// 128-bit integer type.319pub(crate) struct MetadataHashValue {320/// 64 bits of a 128-bit hash value.321pub lower: u64,322/// Another 64 bits of a 128-bit hash value.323pub upper: u64,324}325326impl From<&cap_std::fs::Metadata> for MetadataHashValue {327fn from(meta: &cap_std::fs::Metadata) -> Self {328use cap_fs_ext::MetadataExt;329// Without incurring any deps, std provides us with a 64 bit hash330// function:331use std::hash::Hasher;332// Note that this means that the metadata hash (which becomes a preview1 ino) may333// change when a different rustc release is used to build this host implementation:334let mut hasher = hash_map::DefaultHasher::new();335hasher.write_u64(meta.dev());336hasher.write_u64(meta.ino());337let lower = hasher.finish();338// MetadataHashValue has a pair of 64-bit members for representing a339// single 128-bit number. However, we only have 64 bits of entropy. To340// synthesize the upper 64 bits, lets xor the lower half with an arbitrary341// constant, in this case the 64 bit integer corresponding to the IEEE342// double representation of (a number as close as possible to) pi.343// This seems better than just repeating the same bits in the upper and344// lower parts outright, which could make folks wonder if the struct was345// mangled in the ABI, or worse yet, lead to consumers of this interface346// expecting them to be equal.347let upper = lower ^ 4614256656552045848u64;348Self { lower, upper }349}350}351352#[cfg(unix)]353fn from_raw_os_error(err: Option<i32>) -> Option<ErrorCode> {354use rustix::io::Errno as RustixErrno;355if err.is_none() {356return None;357}358Some(match RustixErrno::from_raw_os_error(err.unwrap()) {359RustixErrno::PIPE => ErrorCode::Pipe,360RustixErrno::PERM => ErrorCode::NotPermitted,361RustixErrno::NOENT => ErrorCode::NoEntry,362RustixErrno::NOMEM => ErrorCode::InsufficientMemory,363RustixErrno::IO => ErrorCode::Io,364RustixErrno::BADF => ErrorCode::BadDescriptor,365RustixErrno::BUSY => ErrorCode::Busy,366RustixErrno::ACCESS => ErrorCode::Access,367RustixErrno::NOTDIR => ErrorCode::NotDirectory,368RustixErrno::ISDIR => ErrorCode::IsDirectory,369RustixErrno::INVAL => ErrorCode::Invalid,370RustixErrno::EXIST => ErrorCode::Exist,371RustixErrno::FBIG => ErrorCode::FileTooLarge,372RustixErrno::NOSPC => ErrorCode::InsufficientSpace,373RustixErrno::SPIPE => ErrorCode::InvalidSeek,374RustixErrno::MLINK => ErrorCode::TooManyLinks,375RustixErrno::NAMETOOLONG => ErrorCode::NameTooLong,376RustixErrno::NOTEMPTY => ErrorCode::NotEmpty,377RustixErrno::LOOP => ErrorCode::Loop,378RustixErrno::OVERFLOW => ErrorCode::Overflow,379RustixErrno::ILSEQ => ErrorCode::IllegalByteSequence,380RustixErrno::NOTSUP => ErrorCode::Unsupported,381RustixErrno::ALREADY => ErrorCode::Already,382RustixErrno::INPROGRESS => ErrorCode::InProgress,383RustixErrno::INTR => ErrorCode::Interrupted,384385// On some platforms, these have the same value as other errno values.386#[allow(unreachable_patterns, reason = "see comment")]387RustixErrno::OPNOTSUPP => ErrorCode::Unsupported,388389_ => return None,390})391}392393#[cfg(windows)]394fn from_raw_os_error(raw_os_error: Option<i32>) -> Option<ErrorCode> {395use windows_sys::Win32::Foundation;396Some(match raw_os_error.map(|code| code as u32) {397Some(Foundation::ERROR_FILE_NOT_FOUND) => ErrorCode::NoEntry,398Some(Foundation::ERROR_PATH_NOT_FOUND) => ErrorCode::NoEntry,399Some(Foundation::ERROR_ACCESS_DENIED) => ErrorCode::Access,400Some(Foundation::ERROR_SHARING_VIOLATION) => ErrorCode::Access,401Some(Foundation::ERROR_PRIVILEGE_NOT_HELD) => ErrorCode::NotPermitted,402Some(Foundation::ERROR_INVALID_HANDLE) => ErrorCode::BadDescriptor,403Some(Foundation::ERROR_INVALID_NAME) => ErrorCode::NoEntry,404Some(Foundation::ERROR_NOT_ENOUGH_MEMORY) => ErrorCode::InsufficientMemory,405Some(Foundation::ERROR_OUTOFMEMORY) => ErrorCode::InsufficientMemory,406Some(Foundation::ERROR_DIR_NOT_EMPTY) => ErrorCode::NotEmpty,407Some(Foundation::ERROR_NOT_READY) => ErrorCode::Busy,408Some(Foundation::ERROR_BUSY) => ErrorCode::Busy,409Some(Foundation::ERROR_NOT_SUPPORTED) => ErrorCode::Unsupported,410Some(Foundation::ERROR_FILE_EXISTS) => ErrorCode::Exist,411Some(Foundation::ERROR_BROKEN_PIPE) => ErrorCode::Pipe,412Some(Foundation::ERROR_BUFFER_OVERFLOW) => ErrorCode::NameTooLong,413Some(Foundation::ERROR_NOT_A_REPARSE_POINT) => ErrorCode::Invalid,414Some(Foundation::ERROR_NEGATIVE_SEEK) => ErrorCode::Invalid,415Some(Foundation::ERROR_DIRECTORY) => ErrorCode::NotDirectory,416Some(Foundation::ERROR_ALREADY_EXISTS) => ErrorCode::Exist,417Some(Foundation::ERROR_STOPPED_ON_SYMLINK) => ErrorCode::Loop,418Some(Foundation::ERROR_DIRECTORY_NOT_SUPPORTED) => ErrorCode::IsDirectory,419_ => return None,420})421}422423impl<'a> From<&'a std::io::Error> for ErrorCode {424fn from(err: &'a std::io::Error) -> ErrorCode {425match from_raw_os_error(err.raw_os_error()) {426Some(errno) => errno,427None => {428debug!("unknown raw os error: {err}");429match err.kind() {430std::io::ErrorKind::NotFound => ErrorCode::NoEntry,431std::io::ErrorKind::PermissionDenied => ErrorCode::NotPermitted,432std::io::ErrorKind::AlreadyExists => ErrorCode::Exist,433std::io::ErrorKind::InvalidInput => ErrorCode::Invalid,434_ => ErrorCode::Io,435}436}437}438}439}440441impl From<std::io::Error> for ErrorCode {442fn from(err: std::io::Error) -> ErrorCode {443ErrorCode::from(&err)444}445}446447#[derive(Clone)]448pub enum Descriptor {449File(File),450Dir(Dir),451}452453impl Descriptor {454pub(crate) fn file(&self) -> Result<&File, ErrorCode> {455match self {456Descriptor::File(f) => Ok(f),457Descriptor::Dir(_) => Err(ErrorCode::BadDescriptor),458}459}460461pub(crate) fn dir(&self) -> Result<&Dir, ErrorCode> {462match self {463Descriptor::Dir(d) => Ok(d),464Descriptor::File(_) => Err(ErrorCode::NotDirectory),465}466}467468async fn get_metadata(&self) -> std::io::Result<cap_std::fs::Metadata> {469match self {470Self::File(f) => {471// No permissions check on metadata: if opened, allowed to stat it472f.run_blocking(|f| f.metadata()).await473}474Self::Dir(d) => {475// No permissions check on metadata: if opened, allowed to stat it476d.run_blocking(|d| d.dir_metadata()).await477}478}479}480481pub(crate) async fn sync_data(&self) -> Result<(), ErrorCode> {482match self {483Self::File(f) => {484match f.run_blocking(|f| f.sync_data()).await {485Ok(()) => Ok(()),486// On windows, `sync_data` uses `FileFlushBuffers` which fails with487// `ERROR_ACCESS_DENIED` if the file is not upen for writing. Ignore488// this error, for POSIX compatibility.489#[cfg(windows)]490Err(err)491if err.raw_os_error()492== Some(windows_sys::Win32::Foundation::ERROR_ACCESS_DENIED as _) =>493{494Ok(())495}496Err(err) => Err(err.into()),497}498}499Self::Dir(d) => {500d.run_blocking(|d| {501let d = d.open(std::path::Component::CurDir)?;502d.sync_data()?;503Ok(())504})505.await506}507}508}509510pub(crate) async fn get_flags(&self) -> Result<DescriptorFlags, ErrorCode> {511use system_interface::fs::{FdFlags, GetSetFdFlags};512513fn get_from_fdflags(flags: FdFlags) -> DescriptorFlags {514let mut out = DescriptorFlags::empty();515if flags.contains(FdFlags::DSYNC) {516out |= DescriptorFlags::REQUESTED_WRITE_SYNC;517}518if flags.contains(FdFlags::RSYNC) {519out |= DescriptorFlags::DATA_INTEGRITY_SYNC;520}521if flags.contains(FdFlags::SYNC) {522out |= DescriptorFlags::FILE_INTEGRITY_SYNC;523}524out525}526match self {527Self::File(f) => {528let flags = f.run_blocking(|f| f.get_fd_flags()).await?;529let mut flags = get_from_fdflags(flags);530if f.open_mode.contains(OpenMode::READ) {531flags |= DescriptorFlags::READ;532}533if f.open_mode.contains(OpenMode::WRITE) {534flags |= DescriptorFlags::WRITE;535}536Ok(flags)537}538Self::Dir(d) => {539let flags = d.run_blocking(|d| d.get_fd_flags()).await?;540let mut flags = get_from_fdflags(flags);541if d.open_mode.contains(OpenMode::READ) {542flags |= DescriptorFlags::READ;543}544if d.open_mode.contains(OpenMode::WRITE) {545flags |= DescriptorFlags::MUTATE_DIRECTORY;546}547Ok(flags)548}549}550}551552pub(crate) async fn get_type(&self) -> Result<DescriptorType, ErrorCode> {553match self {554Self::File(f) => {555let meta = f.run_blocking(|f| f.metadata()).await?;556Ok(meta.file_type().into())557}558Self::Dir(_) => Ok(DescriptorType::Directory),559}560}561562pub(crate) async fn set_times(563&self,564atim: Option<SystemTimeSpec>,565mtim: Option<SystemTimeSpec>,566) -> Result<(), ErrorCode> {567use fs_set_times::SetTimes as _;568match self {569Self::File(f) => {570if !f.perms.contains(FilePerms::WRITE) {571return Err(ErrorCode::NotPermitted);572}573f.run_blocking(|f| f.set_times(atim, mtim)).await?;574Ok(())575}576Self::Dir(d) => {577if !d.perms.contains(DirPerms::MUTATE) {578return Err(ErrorCode::NotPermitted);579}580d.run_blocking(|d| d.set_times(atim, mtim)).await?;581Ok(())582}583}584}585586pub(crate) async fn sync(&self) -> Result<(), ErrorCode> {587match self {588Self::File(f) => {589match f.run_blocking(|f| f.sync_all()).await {590Ok(()) => Ok(()),591// On windows, `sync_data` uses `FileFlushBuffers` which fails with592// `ERROR_ACCESS_DENIED` if the file is not upen for writing. Ignore593// this error, for POSIX compatibility.594#[cfg(windows)]595Err(err)596if err.raw_os_error()597== Some(windows_sys::Win32::Foundation::ERROR_ACCESS_DENIED as _) =>598{599Ok(())600}601Err(err) => Err(err.into()),602}603}604Self::Dir(d) => {605d.run_blocking(|d| {606let d = d.open(std::path::Component::CurDir)?;607d.sync_all()?;608Ok(())609})610.await611}612}613}614615pub(crate) async fn stat(&self) -> Result<DescriptorStat, ErrorCode> {616match self {617Self::File(f) => {618// No permissions check on stat: if opened, allowed to stat it619let meta = f.run_blocking(|f| f.metadata()).await?;620Ok(meta.into())621}622Self::Dir(d) => {623// No permissions check on stat: if opened, allowed to stat it624let meta = d.run_blocking(|d| d.dir_metadata()).await?;625Ok(meta.into())626}627}628}629630pub(crate) async fn is_same_object(&self, other: &Self) -> wasmtime::Result<bool> {631use cap_fs_ext::MetadataExt;632let meta_a = self.get_metadata().await?;633let meta_b = other.get_metadata().await?;634if meta_a.dev() == meta_b.dev() && meta_a.ino() == meta_b.ino() {635// MetadataHashValue does not derive eq, so use a pair of636// comparisons to check equality:637debug_assert_eq!(638MetadataHashValue::from(&meta_a).upper,639MetadataHashValue::from(&meta_b).upper,640);641debug_assert_eq!(642MetadataHashValue::from(&meta_a).lower,643MetadataHashValue::from(&meta_b).lower,644);645Ok(true)646} else {647// Hash collisions are possible, so don't assert the negative here648Ok(false)649}650}651652pub(crate) async fn metadata_hash(&self) -> Result<MetadataHashValue, ErrorCode> {653let meta = self.get_metadata().await?;654Ok(MetadataHashValue::from(&meta))655}656}657658#[derive(Clone)]659pub struct File {660/// The operating system File this struct is mediating access to.661///662/// Wrapped in an Arc because the same underlying file is used for663/// implementing the stream types. A copy is also needed for664/// [`spawn_blocking`].665///666/// [`spawn_blocking`]: Self::spawn_blocking667pub file: Arc<cap_std::fs::File>,668/// Permissions to enforce on access to the file. These permissions are669/// specified by a user of the `crate::WasiCtxBuilder`, and are670/// enforced prior to any enforced by the underlying operating system.671pub perms: FilePerms,672/// The mode the file was opened under: bits for reading, and writing.673/// Required to correctly report the DescriptorFlags, because cap-std674/// doesn't presently provide a cross-platform equivalent of reading the675/// oflags back out using fcntl.676pub open_mode: OpenMode,677678allow_blocking_current_thread: bool,679}680681impl File {682pub fn new(683file: cap_std::fs::File,684perms: FilePerms,685open_mode: OpenMode,686allow_blocking_current_thread: bool,687) -> Self {688Self {689file: Arc::new(file),690perms,691open_mode,692allow_blocking_current_thread,693}694}695696/// Execute the blocking `body` function.697///698/// Depending on how the WasiCtx was configured, the body may either be:699/// - Executed directly on the current thread. In this case the `async`700/// signature of this method is effectively a lie and the returned701/// Future will always be immediately Ready. Or:702/// - Spawned on a background thread using [`tokio::task::spawn_blocking`]703/// and immediately awaited.704///705/// Intentionally blocking the executor thread might seem unorthodox, but is706/// not actually a problem for specific workloads. See:707/// - [`crate::WasiCtxBuilder::allow_blocking_current_thread`]708/// - [Poor performance of wasmtime file I/O maybe because tokio](https://github.com/bytecodealliance/wasmtime/issues/7973)709/// - [Implement opt-in for enabling WASI to block the current thread](https://github.com/bytecodealliance/wasmtime/pull/8190)710pub(crate) async fn run_blocking<F, R>(&self, body: F) -> R711where712F: FnOnce(&cap_std::fs::File) -> R + Send + 'static,713R: Send + 'static,714{715match self.as_blocking_file() {716Some(file) => body(file),717None => self.spawn_blocking(body).await,718}719}720721pub(crate) fn spawn_blocking<F, R>(&self, body: F) -> AbortOnDropJoinHandle<R>722where723F: FnOnce(&cap_std::fs::File) -> R + Send + 'static,724R: Send + 'static,725{726let f = self.file.clone();727spawn_blocking(move || body(&f))728}729730/// Returns `Some` when the current thread is allowed to block in filesystem731/// operations, and otherwise returns `None` to indicate that732/// `spawn_blocking` must be used.733pub(crate) fn as_blocking_file(&self) -> Option<&cap_std::fs::File> {734if self.allow_blocking_current_thread {735Some(&self.file)736} else {737None738}739}740741/// Returns reference to the underlying [`cap_std::fs::File`]742#[cfg(feature = "p3")]743pub(crate) fn as_file(&self) -> &Arc<cap_std::fs::File> {744&self.file745}746747pub(crate) async fn advise(748&self,749offset: u64,750len: u64,751advice: system_interface::fs::Advice,752) -> Result<(), ErrorCode> {753use system_interface::fs::FileIoExt as _;754self.run_blocking(move |f| f.advise(offset, len, advice))755.await?;756Ok(())757}758759pub(crate) async fn set_size(&self, size: u64) -> Result<(), ErrorCode> {760if !self.perms.contains(FilePerms::WRITE) {761return Err(ErrorCode::NotPermitted);762}763self.run_blocking(move |f| f.set_len(size)).await?;764Ok(())765}766}767768#[derive(Clone)]769pub struct Dir {770/// The operating system file descriptor this struct is mediating access771/// to.772///773/// Wrapped in an Arc because a copy is needed for [`spawn_blocking`].774///775/// [`spawn_blocking`]: Self::spawn_blocking776pub dir: Arc<cap_std::fs::Dir>,777/// Permissions to enforce on access to this directory. These permissions778/// are specified by a user of the `crate::WasiCtxBuilder`, and779/// are enforced prior to any enforced by the underlying operating system.780///781/// These permissions are also enforced on any directories opened under782/// this directory.783pub perms: DirPerms,784/// Permissions to enforce on any files opened under this directory.785pub file_perms: FilePerms,786/// The mode the directory was opened under: bits for reading, and writing.787/// Required to correctly report the DescriptorFlags, because cap-std788/// doesn't presently provide a cross-platform equivalent of reading the789/// oflags back out using fcntl.790pub open_mode: OpenMode,791792pub(crate) allow_blocking_current_thread: bool,793}794795impl Dir {796pub fn new(797dir: cap_std::fs::Dir,798perms: DirPerms,799file_perms: FilePerms,800open_mode: OpenMode,801allow_blocking_current_thread: bool,802) -> Self {803Dir {804dir: Arc::new(dir),805perms,806file_perms,807open_mode,808allow_blocking_current_thread,809}810}811812/// Execute the blocking `body` function.813///814/// Depending on how the WasiCtx was configured, the body may either be:815/// - Executed directly on the current thread. In this case the `async`816/// signature of this method is effectively a lie and the returned817/// Future will always be immediately Ready. Or:818/// - Spawned on a background thread using [`tokio::task::spawn_blocking`]819/// and immediately awaited.820///821/// Intentionally blocking the executor thread might seem unorthodox, but is822/// not actually a problem for specific workloads. See:823/// - [`crate::WasiCtxBuilder::allow_blocking_current_thread`]824/// - [Poor performance of wasmtime file I/O maybe because tokio](https://github.com/bytecodealliance/wasmtime/issues/7973)825/// - [Implement opt-in for enabling WASI to block the current thread](https://github.com/bytecodealliance/wasmtime/pull/8190)826pub(crate) async fn run_blocking<F, R>(&self, body: F) -> R827where828F: FnOnce(&cap_std::fs::Dir) -> R + Send + 'static,829R: Send + 'static,830{831if self.allow_blocking_current_thread {832body(&self.dir)833} else {834let d = self.dir.clone();835spawn_blocking(move || body(&d)).await836}837}838839/// Returns reference to the underlying [`cap_std::fs::Dir`]840#[cfg(feature = "p3")]841pub(crate) fn as_dir(&self) -> &Arc<cap_std::fs::Dir> {842&self.dir843}844845pub(crate) async fn create_directory_at(&self, path: String) -> Result<(), ErrorCode> {846if !self.perms.contains(DirPerms::MUTATE) {847return Err(ErrorCode::NotPermitted);848}849self.run_blocking(move |d| d.create_dir(&path)).await?;850Ok(())851}852853pub(crate) async fn stat_at(854&self,855path_flags: PathFlags,856path: String,857) -> Result<DescriptorStat, ErrorCode> {858if !self.perms.contains(DirPerms::READ) {859return Err(ErrorCode::NotPermitted);860}861862let meta = if path_flags.contains(PathFlags::SYMLINK_FOLLOW) {863self.run_blocking(move |d| d.metadata(&path)).await?864} else {865self.run_blocking(move |d| d.symlink_metadata(&path))866.await?867};868Ok(meta.into())869}870871pub(crate) async fn set_times_at(872&self,873path_flags: PathFlags,874path: String,875atim: Option<SystemTimeSpec>,876mtim: Option<SystemTimeSpec>,877) -> Result<(), ErrorCode> {878use cap_fs_ext::DirExt as _;879880if !self.perms.contains(DirPerms::MUTATE) {881return Err(ErrorCode::NotPermitted);882}883if path_flags.contains(PathFlags::SYMLINK_FOLLOW) {884self.run_blocking(move |d| {885d.set_times(886&path,887atim.map(cap_fs_ext::SystemTimeSpec::from_std),888mtim.map(cap_fs_ext::SystemTimeSpec::from_std),889)890})891.await?;892} else {893self.run_blocking(move |d| {894d.set_symlink_times(895&path,896atim.map(cap_fs_ext::SystemTimeSpec::from_std),897mtim.map(cap_fs_ext::SystemTimeSpec::from_std),898)899})900.await?;901}902Ok(())903}904905pub(crate) async fn link_at(906&self,907old_path_flags: PathFlags,908old_path: String,909new_dir: &Self,910new_path: String,911) -> Result<(), ErrorCode> {912if !self.perms.contains(DirPerms::MUTATE) {913return Err(ErrorCode::NotPermitted);914}915if !new_dir.perms.contains(DirPerms::MUTATE) {916return Err(ErrorCode::NotPermitted);917}918if old_path_flags.contains(PathFlags::SYMLINK_FOLLOW) {919return Err(ErrorCode::Invalid);920}921let new_dir_handle = Arc::clone(&new_dir.dir);922self.run_blocking(move |d| d.hard_link(&old_path, &new_dir_handle, &new_path))923.await?;924Ok(())925}926927pub(crate) async fn open_at(928&self,929path_flags: PathFlags,930path: String,931oflags: OpenFlags,932flags: DescriptorFlags,933allow_blocking_current_thread: bool,934) -> Result<Descriptor, ErrorCode> {935use cap_fs_ext::{FollowSymlinks, OpenOptionsFollowExt, OpenOptionsMaybeDirExt};936use system_interface::fs::{FdFlags, GetSetFdFlags};937938if !self.perms.contains(DirPerms::READ) {939return Err(ErrorCode::NotPermitted);940}941942if !self.perms.contains(DirPerms::MUTATE) {943if oflags.contains(OpenFlags::CREATE) || oflags.contains(OpenFlags::TRUNCATE) {944return Err(ErrorCode::NotPermitted);945}946if flags.contains(DescriptorFlags::WRITE) {947return Err(ErrorCode::NotPermitted);948}949}950951// Track whether we are creating file, for permission check:952let mut create = false;953// Track open mode, for permission check and recording in created descriptor:954let mut open_mode = OpenMode::empty();955// Construct the OpenOptions to give the OS:956let mut opts = cap_std::fs::OpenOptions::new();957opts.maybe_dir(true);958959if oflags.contains(OpenFlags::CREATE) {960if oflags.contains(OpenFlags::EXCLUSIVE) {961opts.create_new(true);962} else {963opts.create(true);964}965create = true;966opts.write(true);967open_mode |= OpenMode::WRITE;968}969970if oflags.contains(OpenFlags::TRUNCATE) {971opts.truncate(true).write(true);972}973if flags.contains(DescriptorFlags::READ) {974opts.read(true);975open_mode |= OpenMode::READ;976}977if flags.contains(DescriptorFlags::WRITE) {978opts.write(true);979open_mode |= OpenMode::WRITE;980} else {981// If not opened write, open read. This way the OS lets us open982// the file, but we can use perms to reject use of the file later.983opts.read(true);984open_mode |= OpenMode::READ;985}986if path_flags.contains(PathFlags::SYMLINK_FOLLOW) {987opts.follow(FollowSymlinks::Yes);988} else {989opts.follow(FollowSymlinks::No);990}991992// These flags are not yet supported in cap-std:993if flags.contains(DescriptorFlags::FILE_INTEGRITY_SYNC)994|| flags.contains(DescriptorFlags::DATA_INTEGRITY_SYNC)995|| flags.contains(DescriptorFlags::REQUESTED_WRITE_SYNC)996{997return Err(ErrorCode::Unsupported);998}9991000if oflags.contains(OpenFlags::DIRECTORY) {1001if oflags.contains(OpenFlags::CREATE)1002|| oflags.contains(OpenFlags::EXCLUSIVE)1003|| oflags.contains(OpenFlags::TRUNCATE)1004{1005return Err(ErrorCode::Invalid);1006}1007}10081009// Now enforce this WasiCtx's permissions before letting the OS have1010// its shot:1011if !self.perms.contains(DirPerms::MUTATE) && create {1012return Err(ErrorCode::NotPermitted);1013}1014if !self.file_perms.contains(FilePerms::WRITE) && open_mode.contains(OpenMode::WRITE) {1015return Err(ErrorCode::NotPermitted);1016}10171018// Represents each possible outcome from the spawn_blocking operation.1019// This makes sure we don't have to give spawn_blocking any way to1020// manipulate the table.1021enum OpenResult {1022Dir(cap_std::fs::Dir),1023File(cap_std::fs::File),1024NotDir,1025}10261027let opened = self1028.run_blocking::<_, std::io::Result<OpenResult>>(move |d| {1029let mut opened = d.open_with(&path, &opts)?;1030if opened.metadata()?.is_dir() {1031Ok(OpenResult::Dir(cap_std::fs::Dir::from_std_file(1032opened.into_std(),1033)))1034} else if oflags.contains(OpenFlags::DIRECTORY) {1035Ok(OpenResult::NotDir)1036} else {1037// FIXME cap-std needs a nonblocking open option so that files reads and writes1038// are nonblocking. Instead we set it after opening here:1039let set_fd_flags = opened.new_set_fd_flags(FdFlags::NONBLOCK)?;1040opened.set_fd_flags(set_fd_flags)?;1041Ok(OpenResult::File(opened))1042}1043})1044.await?;10451046match opened {1047OpenResult::Dir(dir) => Ok(Descriptor::Dir(Dir::new(1048dir,1049self.perms,1050self.file_perms,1051open_mode,1052allow_blocking_current_thread,1053))),10541055OpenResult::File(file) => Ok(Descriptor::File(File::new(1056file,1057self.file_perms,1058open_mode,1059allow_blocking_current_thread,1060))),10611062OpenResult::NotDir => Err(ErrorCode::NotDirectory),1063}1064}10651066pub(crate) async fn readlink_at(&self, path: String) -> Result<String, ErrorCode> {1067if !self.perms.contains(DirPerms::READ) {1068return Err(ErrorCode::NotPermitted);1069}1070let link = self.run_blocking(move |d| d.read_link(&path)).await?;1071link.into_os_string()1072.into_string()1073.or(Err(ErrorCode::IllegalByteSequence))1074}10751076pub(crate) async fn remove_directory_at(&self, path: String) -> Result<(), ErrorCode> {1077if !self.perms.contains(DirPerms::MUTATE) {1078return Err(ErrorCode::NotPermitted);1079}1080self.run_blocking(move |d| d.remove_dir(&path)).await?;1081Ok(())1082}10831084pub(crate) async fn rename_at(1085&self,1086old_path: String,1087new_dir: &Self,1088new_path: String,1089) -> Result<(), ErrorCode> {1090if !self.perms.contains(DirPerms::MUTATE) {1091return Err(ErrorCode::NotPermitted);1092}1093if !new_dir.perms.contains(DirPerms::MUTATE) {1094return Err(ErrorCode::NotPermitted);1095}1096let new_dir_handle = Arc::clone(&new_dir.dir);1097self.run_blocking(move |d| d.rename(&old_path, &new_dir_handle, &new_path))1098.await?;1099Ok(())1100}11011102pub(crate) async fn symlink_at(1103&self,1104src_path: String,1105dest_path: String,1106) -> Result<(), ErrorCode> {1107// On windows, Dir.symlink is provided by DirExt1108#[cfg(windows)]1109use cap_fs_ext::DirExt;11101111if !self.perms.contains(DirPerms::MUTATE) {1112return Err(ErrorCode::NotPermitted);1113}1114self.run_blocking(move |d| d.symlink(&src_path, &dest_path))1115.await?;1116Ok(())1117}11181119pub(crate) async fn unlink_file_at(&self, path: String) -> Result<(), ErrorCode> {1120use cap_fs_ext::DirExt;11211122if !self.perms.contains(DirPerms::MUTATE) {1123return Err(ErrorCode::NotPermitted);1124}1125self.run_blocking(move |d| d.remove_file_or_symlink(&path))1126.await?;1127Ok(())1128}11291130pub(crate) async fn metadata_hash_at(1131&self,1132path_flags: PathFlags,1133path: String,1134) -> Result<MetadataHashValue, ErrorCode> {1135// No permissions check on metadata: if dir opened, allowed to stat it1136let meta = self1137.run_blocking(move |d| {1138if path_flags.contains(PathFlags::SYMLINK_FOLLOW) {1139d.metadata(path)1140} else {1141d.symlink_metadata(path)1142}1143})1144.await?;1145Ok(MetadataHashValue::from(&meta))1146}1147}11481149impl WasiFilesystemCtxView<'_> {1150pub(crate) fn get_directories(1151&mut self,1152) -> wasmtime::Result<Vec<(Resource<Descriptor>, String)>> {1153let preopens = self.ctx.preopens.clone();1154let mut results = Vec::with_capacity(preopens.len());1155for (dir, name) in preopens {1156let fd = self1157.table1158.push(Descriptor::Dir(dir))1159.with_context(|| format!("failed to push preopen {name}"))?;1160results.push((fd, name));1161}1162Ok(results)1163}1164}116511661167