A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Continue to reorganize `filesystem::primitives` (#14345)
Alex Crichton05f1d0990350
diff --git a/crates/wasi/src/filesystem.rs b/crates/wasi/src/filesystem.rsindex e6f00ea704..d1d953c14d 100644--- a/crates/wasi/src/filesystem.rs+++ b/crates/wasi/src/filesystem.rs@@ -1,5 +1,5 @@use crate::clocks::Datetime;-use crate::filesystem::primitives::{DirOptions, FollowSymlinks, Metadata, OpenOptions};+use crate::filesystem::primitives::{FollowSymlinks, Metadata, OpenOptions};use crate::runtime::{AbortOnDropJoinHandle, spawn_blocking};use crate::{NamedId, WasiCtxNamedView};use std::collections::hash_map;@@ -855,10 +855,8 @@ impl Dir {if self.perms.write_not_permitted() {return Err(ErrorCode::NotPermitted);}- self.run_blocking(move |d| {- crate::filesystem::primitives::create_dir(d, path.as_ref(), &DirOptions::new())- })- .await?;+ self.run_blocking(move |d| crate::filesystem::primitives::create_dir(d, path.as_ref()))+ .await?;Ok(())}diff --git a/crates/wasi/src/filesystem/primitives/dir_options.rs b/crates/wasi/src/filesystem/primitives/dir_options.rsdeleted file mode 100644index 096bfa4469..0000000000--- a/crates/wasi/src/filesystem/primitives/dir_options.rs+++ /dev/null@@ -1,34 +0,0 @@-#[cfg(not(target_os = "wasi"))]-use crate::filesystem::primitives::DirOptionsExt;--/// Options and flags which can be used to configure how a directory is-/// created.-///-/// This is to `create_dir` what to `OpenOptions` is to `open`.-#[derive(Debug, Clone)]-pub struct DirOptions {- #[cfg(not(target_os = "wasi"))]- #[allow(dead_code)]- pub(crate) ext: DirOptionsExt,-}--impl DirOptions {- /// Creates a blank new set of options ready for configuration.- #[allow(clippy::new_without_default)]- #[inline]- pub const fn new() -> Self {- Self {- #[cfg(not(target_os = "wasi"))]- ext: DirOptionsExt::new(),- }- }-}--#[cfg(target_os = "vxworks")]-impl crate::fs::DirBuilderExt for DirOptions {- #[inline]- fn mode(&mut self, mode: u32) -> &mut Self {- self.ext.mode(mode);- self- }-}diff --git a/crates/wasi/src/filesystem/primitives/errors.rs b/crates/wasi/src/filesystem/primitives/errors.rsindex d0dcc94911..f61a81b071 100644--- a/crates/wasi/src/filesystem/primitives/errors.rs+++ b/crates/wasi/src/filesystem/primitives/errors.rs@@ -1,9 +1,9 @@use std::io;#[cfg(not(windows))]-pub(crate) use crate::filesystem::primitives::rustix::fs::errors::*;+pub(crate) use crate::filesystem::primitives::unix::errors::*;#[cfg(windows)]-pub(crate) use crate::filesystem::primitives::windows::fs::errors::*;+pub(crate) use crate::filesystem::primitives::windows::errors::*;#[cold]pub(crate) fn escape_attempt() -> io::Error {diff --git a/crates/wasi/src/filesystem/primitives/mod.rs b/crates/wasi/src/filesystem/primitives/mod.rsindex 3756ca09c7..4f6e1d89d9 100644--- a/crates/wasi/src/filesystem/primitives/mod.rs+++ b/crates/wasi/src/filesystem/primitives/mod.rs@@ -17,43 +17,41 @@reason = "preexisting from when cap-primitives was imported")]-use std::path::{Path, PathBuf};+use std::path::{Component, Path, PathBuf};use std::{fs, io};mod dir_entry;-mod dir_options;mod file_type;mod maybe_owned_file;mod metadata;mod open_options;+mod open_parent;mod open_unchecked_error;mod read_dir;mod errors;mod manually;-mod via_parent;#[cfg(test)]mod tests;#[cfg(not(windows))]-mod rustix;+mod unix;#[cfg(not(windows))]-use self::rustix::fs as sys;+use self::unix as sys;#[cfg(windows)]mod windows;#[cfg(windows)]-use self::windows::fs as sys;+use self::windows as sys;#[cfg(windows)]use file_type::_WindowsFileTypeExt;use maybe_owned_file::MaybeOwnedFile;+use open_parent::open_parent;use open_unchecked_error::*;-use sys::read_link_impl as read_link_contents;use sys::*;pub(crate) use dir_entry::DirEntry;-pub(crate) use dir_options::DirOptions;pub(crate) use file_type::FileType;#[cfg(any(unix, target_os = "vxworks"))]pub(crate) use file_type::FileTypeExt;@@ -62,16 +60,22 @@ pub(crate) use metadata::_WindowsByHandle;pub(crate) use metadata::{Metadata, MetadataExt};pub(crate) use open_options::*;pub(crate) use read_dir::read_base_dir;-pub(crate) use sys::create_dir_impl as create_dir;-pub(crate) use sys::hard_link_impl as hard_link;-pub(crate) use sys::open_ambient_dir_impl as open_ambient_dir;-pub(crate) use sys::open_impl as open;-pub(crate) use sys::remove_dir_impl as remove_dir;-pub(crate) use sys::remove_file_impl as remove_file;-pub(crate) use sys::rename_impl as rename;-pub(crate) use sys::set_times_impl as set_times;-pub(crate) use sys::set_times_nofollow_impl as set_times_nofollow;-pub(crate) use sys::stat_impl as stat;+pub(crate) use sys::open_ambient_dir;+pub(crate) use sys::set_times;+pub(crate) use sys::set_times_nofollow;++pub(crate) fn open(start: &fs::File, path: &Path, options: &OpenOptions) -> io::Result<fs::File> {+ #[cfg(any(+ windows,+ target_os = "freebsd",+ target_os = "android",+ target_os = "linux",+ ))]+ if let Some(file) = sys::open_fast(start, path, options)? {+ return Ok(file);+ }+ manually::open(start, path, options)+}/// Should symlinks be followed in the last component of a path?///@@ -90,11 +94,21 @@ pub(crate) enum FollowSymlinks {No,}-/// Like [`read_link_contents`], but additionally verifies that the link target-/// is not absolute.-#[inline]+/// Perform a `readlinkat`-like operation, ensuring that the resolution of the+/// path never escapes the directory tree rooted at `start`, and additionally+/// verifying that the link target is not absolute.pub(crate) fn read_link(start: &fs::File, path: &Path) -> io::Result<PathBuf> {- let contents = read_link_contents(start, path)?;+ // On most platforms `read_link` is implemented by `open`ing up the parent+ // component of the path and then calling `read_link_unchecked` on the last+ // component.+ //+ // This technique doesn't work in all cases on Windows. In particular, a+ // directory symlink such as `C:\Documents and Settings` may not grant any+ // access other than what is needed to resolve the symlink, so+ // `open_parent`'s technique of returning a relative path of `.` from that+ // point doesn't work, because opening `.` within such a directory is+ // denied. Consequently, we use a different implementation on Windows.+ let contents = sys::read_link(start, path)?;// Don't allow reading symlinks to absolute paths. This isn't strictly// necessary to preserve the sandbox, since `open` will refuse to follow@@ -123,7 +137,9 @@ pub(crate) fn symlink(old_path: &Path, new_start: &fs::File, new_path: &Path) ->return Err(errors::escape_attempt());}- sys::symlink_impl(old_path, new_start, new_path)+ let new_start = MaybeOwnedFile::borrowed(new_start);+ let (new_dir, new_basename) = open_parent(new_start, new_path)?;+ symlink_unchecked(old_path, &new_dir, new_basename.as_ref())}/// Perform a `symlink_file`-like operation, ensuring that the resolution of@@ -140,7 +156,9 @@ pub(crate) fn symlink_file(return Err(errors::escape_attempt());}- sys::symlink_file_impl(old_path, new_start, new_path)+ let new_start = MaybeOwnedFile::borrowed(new_start);+ let (new_dir, new_basename) = open_parent(new_start, new_path)?;+ symlink_file_unchecked(old_path, &new_dir, new_basename.as_ref())}/// Perform a `symlink_dir`-like operation, ensuring that the resolution of the@@ -157,7 +175,99 @@ pub(crate) fn symlink_dir(return Err(errors::escape_attempt());}- sys::symlink_dir_impl(old_path, new_start, new_path)+ let new_start = MaybeOwnedFile::borrowed(new_start);+ let (new_dir, new_basename) = open_parent(new_start, new_path)?;+ symlink_dir_unchecked(old_path, &new_dir, new_basename.as_ref())+}++/// Perform a `mkdirat`-like operation, ensuring that the resolution of the+/// path never escapes the directory tree rooted at `start`.+///+/// This is implemented by `open`ing up the parent component of the path and+/// then calling `create_dir_unchecked` on the last component.+pub(crate) fn create_dir(start: &fs::File, path: &Path) -> io::Result<()> {+ let start = MaybeOwnedFile::borrowed(start);++ // As a special case, `create_dir` ignores a trailing slash rather than+ // treating it as equivalent to a trailing slash-dot, so strip any trailing+ // slashes.+ let path = strip_dir_suffix(path);++ let (dir, basename) = open_parent(start, &path)?;++ create_dir_unchecked(&dir, basename.as_ref())+}++/// Perform a `linkat`-like operation, ensuring that the resolution of both+/// paths never escapes the directory tree rooted at their respective starts.+///+/// This is implemented by `open`ing up the parent component of each path and+/// then calling `hard_link_unchecked` on the last components.+pub(crate) fn hard_link(+ old_start: &fs::File,+ old_path: &Path,+ new_start: &fs::File,+ new_path: &Path,+) -> io::Result<()> {+ let old_start = MaybeOwnedFile::borrowed(old_start);+ let new_start = MaybeOwnedFile::borrowed(new_start);++ let (old_dir, old_basename) = open_parent(old_start, old_path)?;+ let (new_dir, new_basename) = open_parent(new_start, new_path)?;++ hard_link_unchecked(+ &old_dir,+ old_basename.as_ref(),+ &new_dir,+ new_basename.as_ref(),+ )+}++/// Perform a `renameat`-like operation, ensuring that the resolution of both+/// paths never escapes the directory tree rooted at their respective starts.+///+/// This is implemented by `open`ing up the parent component of each path and+/// then calling `rename_unchecked` on the last components.+pub(crate) fn rename(+ old_start: &fs::File,+ old_path: &Path,+ new_start: &fs::File,+ new_path: &Path,+) -> io::Result<()> {+ let old_start = MaybeOwnedFile::borrowed(old_start);+ let new_start = MaybeOwnedFile::borrowed(new_start);++ // As a special case, `rename` ignores a trailing slash rather than treating+ // it as equivalent to a trailing slash-dot, so strip any trailing slashes+ // for the purposes of `open_parent`.+ //+ // And on Unix, remember whether the source started with a slash so that we+ // can still fail if it is and the source is a regular file.+ #[cfg(unix)]+ let old_starts_with_slash = path_has_trailing_slash(old_path);+ let old_path = strip_dir_suffix(old_path);+ let new_path = strip_dir_suffix(new_path);++ let (old_dir, old_basename) = open_parent(old_start, &old_path)?;+ let (new_dir, new_basename) = open_parent(new_start, &new_path)?;++ // On Unix, re-append a slash if needed.+ #[cfg(unix)]+ let concat;+ #[cfg(unix)]+ let old_basename = if old_starts_with_slash {+ concat = append_dir_suffix(old_basename.to_owned().into());+ concat.as_os_str()+ } else {+ old_basename+ };++ rename_unchecked(+ &old_dir,+ old_basename.as_ref(),+ &new_dir,+ new_basename.as_ref(),+ )}/// Open a directory by performing an `openat`-like operation, ensuring that@@ -186,3 +296,46 @@ fn open_dir_for_reading_unchecked() -> io::Result<fs::File> {open_unchecked(start, path, readdir_options().follow(follow)).map_err(Into::into)}++pub(crate) fn remove_file(start: &fs::File, path: &Path) -> io::Result<()> {+ #[cfg(target_os = "freebsd")]+ if sys::remove_file_fast(start, path)? {+ return Ok(());+ }++ let start = MaybeOwnedFile::borrowed(start);+ let (dir, basename) = open_parent(start, path)?;+ remove_file_unchecked(&dir, basename.as_ref())+}++pub(crate) fn remove_dir(start: &fs::File, path: &Path) -> io::Result<()> {+ #[cfg(target_os = "freebsd")]+ if sys::remove_dir_fast(start, path)? {+ return Ok(());+ }++ let start = MaybeOwnedFile::borrowed(start);+ let (dir, basename) = open_parent(start, path)?;+ remove_dir_unchecked(&dir, basename.as_ref())+}++pub(crate) fn stat(start: &fs::File, path: &Path, follow: FollowSymlinks) -> io::Result<Metadata> {+ // Optimization: if path has exactly one component and it's not ".." or+ // anything non-normal and we're not following symlinks we can go straight+ // to `stat_unchecked`, which can be faster than various paths below.+ if follow == FollowSymlinks::No {+ let mut components = path.components();+ if let Some(Component::Normal(component)) = components.next() {+ if components.next().is_none() {+ return stat_unchecked(start, component.as_ref(), FollowSymlinks::No);+ }+ }+ }++ #[cfg(any(target_os = "freebsd", target_os = "android", target_os = "linux",))]+ if let Some(stat) = sys::stat_fast(start, path, follow)? {+ return Ok(stat);+ }++ manually::stat(start, path, follow)+}diff --git a/crates/wasi/src/filesystem/primitives/via_parent/open_parent.rs b/crates/wasi/src/filesystem/primitives/open_parent.rssimilarity index 98%rename from crates/wasi/src/filesystem/primitives/via_parent/open_parent.rsrename to crates/wasi/src/filesystem/primitives/open_parent.rsindex 974e4ebfd5..8eb7d59e9c 100644--- a/crates/wasi/src/filesystem/primitives/via_parent/open_parent.rs+++ b/crates/wasi/src/filesystem/primitives/open_parent.rs@@ -12,7 +12,7 @@ use std::path::{Component, Path};/// the single remaining path component. This last component will not be `..`,/// though it may be `.` or a symbolic link to anywhere (possibly/// including `..` or an absolute path).-pub(super) fn open_parent<'path, 'borrow>(+pub(crate) fn open_parent<'path, 'borrow>(start: MaybeOwnedFile<'borrow>,path: &'path Path,) -> io::Result<(MaybeOwnedFile<'borrow>, &'path OsStr)> {diff --git a/crates/wasi/src/filesystem/primitives/rustix/freebsd/fs/check.rs b/crates/wasi/src/filesystem/primitives/rustix/freebsd/fs/check.rsdeleted file mode 100644index 6e7c706051..0000000000--- a/crates/wasi/src/filesystem/primitives/rustix/freebsd/fs/check.rs+++ /dev/null@@ -1,40 +0,0 @@-use rustix::cstr;-use rustix::fs::{AtFlags, CWD, Mode, OFlags, openat, statat};-use rustix::io::Errno;-use std::sync::atomic::{AtomicBool, Ordering::Relaxed};--static WORKING: AtomicBool = AtomicBool::new(false);-static CHECKED: AtomicBool = AtomicBool::new(false);--#[inline]-pub(crate) fn beneath_supported() -> bool {- if WORKING.load(Relaxed) {- return true;- }- if CHECKED.load(Relaxed) {- return false;- }- check_beneath_supported()-}--#[cold]-fn check_beneath_supported() -> bool {- // `RESOLVE_BENEATH` was introduced in FreeBSD 13, but opening `..` within- // the root directory re-opened the root directory. In FreeBSD 14, it fails- // as cap-std expects.- if let Ok(root) = openat(- CWD,- cstr!("/"),- OFlags::RDONLY | OFlags::CLOEXEC,- Mode::empty(),- ) {- // Unknown O_ flags get ignored but AT_ flags have strict checks, so we use that.- if let Err(Errno::NOTCAPABLE) = statat(root, cstr!(".."), AtFlags::RESOLVE_BENEATH) {- WORKING.store(true, Relaxed);- return true;- }- }-- CHECKED.store(true, Relaxed);- false-}diff --git a/crates/wasi/src/filesystem/primitives/rustix/freebsd/fs/mod.rs b/crates/wasi/src/filesystem/primitives/rustix/freebsd/fs/mod.rsdeleted file mode 100644index dd8fcfc547..0000000000--- a/crates/wasi/src/filesystem/primitives/rustix/freebsd/fs/mod.rs+++ /dev/null@@ -1,13 +0,0 @@-mod check;-mod open_impl;-mod remove_dir_impl;-mod remove_file_impl;-mod set_times_impl;-mod stat_impl;--pub(crate) use check::beneath_supported;-pub(crate) use open_impl::open_impl;-pub(crate) use remove_dir_impl::remove_dir_impl;-pub(crate) use remove_file_impl::remove_file_impl;-pub(crate) use set_times_impl::{set_times_impl, set_times_nofollow_impl};-pub(crate) use stat_impl::stat_impl;diff --git a/crates/wasi/src/filesystem/primitives/rustix/freebsd/fs/open_impl.rs b/crates/wasi/src/filesystem/primitives/rustix/freebsd/fs/open_impl.rsdeleted file mode 100644index d29ce5b9f1..0000000000--- a/crates/wasi/src/filesystem/primitives/rustix/freebsd/fs/open_impl.rs+++ /dev/null@@ -1,29 +0,0 @@-use super::super::super::fs::compute_oflags;-use crate::filesystem::primitives::{OpenOptions, errors, manually};-use rustix::fs::{Mode, OFlags, RawMode, openat};-use std::path::Path;-use std::{fs, io};--pub(crate) fn open_impl(- start: &fs::File,- path: &Path,- options: &OpenOptions,-) -> io::Result<fs::File> {- if !super::beneath_supported() {- return manually::open(start, path, options);- }-- let oflags = compute_oflags(options)? | OFlags::RESOLVE_BENEATH;-- let mode = if oflags.contains(OFlags::CREATE) {- Mode::from_bits((options.ext.mode & 0o7777) as RawMode).unwrap()- } else {- Mode::empty()- };-- match openat(start, path, oflags, mode) {- Ok(file) => Ok(file.into()),- Err(rustix::io::Errno::NOTCAPABLE) => Err(errors::escape_attempt()),- Err(err) => Err(err.into()),- }-}diff --git a/crates/wasi/src/filesystem/primitives/rustix/freebsd/fs/remove_dir_impl.rs b/crates/wasi/src/filesystem/primitives/rustix/freebsd/fs/remove_dir_impl.rsdeleted file mode 100644index e4a440dbfb..0000000000--- a/crates/wasi/src/filesystem/primitives/rustix/freebsd/fs/remove_dir_impl.rs+++ /dev/null@@ -1,16 +0,0 @@-use crate::filesystem::primitives::via_parent;-use rustix::fs::{AtFlags, unlinkat};-use std::path::Path;-use std::{fs, io};--pub(crate) fn remove_dir_impl(start: &fs::File, path: &Path) -> io::Result<()> {- if !super::beneath_supported() {- return via_parent::remove_dir(start, path);- }-- Ok(unlinkat(- start,- path,- AtFlags::RESOLVE_BENEATH | AtFlags::REMOVEDIR,- )?)-}diff --git a/crates/wasi/src/filesystem/primitives/rustix/freebsd/fs/remove_file_impl.rs b/crates/wasi/src/filesystem/primitives/rustix/freebsd/fs/remove_file_impl.rsdeleted file mode 100644index 5c50e64097..0000000000--- a/crates/wasi/src/filesystem/primitives/rustix/freebsd/fs/remove_file_impl.rs+++ /dev/null@@ -1,12 +0,0 @@-use crate::filesystem::primitives::via_parent;-use rustix::fs::{AtFlags, unlinkat};-use std::path::Path;-use std::{fs, io};--pub(crate) fn remove_file_impl(start: &fs::File, path: &Path) -> io::Result<()> {- if !super::beneath_supported() {- return via_parent::remove_file(start, path);- }-- Ok(unlinkat(start, path, AtFlags::RESOLVE_BENEATH)?)-}diff --git a/crates/wasi/src/filesystem/primitives/rustix/freebsd/fs/set_times_impl.rs b/crates/wasi/src/filesystem/primitives/rustix/freebsd/fs/set_times_impl.rsdeleted file mode 100644index d11303eb11..0000000000--- a/crates/wasi/src/filesystem/primitives/rustix/freebsd/fs/set_times_impl.rs+++ /dev/null@@ -1,46 +0,0 @@-use crate::filesystem::primitives::{to_timespec, via_parent};-use rustix::fs::{AtFlags, Timestamps, utimensat};-use std::path::Path;-use std::time::SystemTime;-use std::{fs, io};--pub(crate) fn set_times_impl(- start: &fs::File,- path: &Path,- atime: Option<SystemTime>,- mtime: Option<SystemTime>,-) -> io::Result<()> {- if !super::beneath_supported() {- return super::super::super::fs::set_times_manually(start, path, atime, mtime);- }-- let times = Timestamps {- last_access: to_timespec(atime)?,- last_modification: to_timespec(mtime)?,- };-- Ok(utimensat(start, path, ×, AtFlags::RESOLVE_BENEATH)?)-}--pub(crate) fn set_times_nofollow_impl(- start: &fs::File,- path: &Path,- atime: Option<SystemTime>,- mtime: Option<SystemTime>,-) -> io::Result<()> {- if !super::beneath_supported() {- return via_parent::set_times_nofollow(start, path, atime, mtime);- }-- let times = Timestamps {- last_access: to_timespec(atime)?,- last_modification: to_timespec(mtime)?,- };-- Ok(utimensat(- start,- path,- ×,- AtFlags::RESOLVE_BENEATH | AtFlags::SYMLINK_NOFOLLOW,- )?)-}diff --git a/crates/wasi/src/filesystem/primitives/rustix/freebsd/fs/stat_impl.rs b/crates/wasi/src/filesystem/primitives/rustix/freebsd/fs/stat_impl.rsdeleted file mode 100644index 1299360118..0000000000--- a/crates/wasi/src/filesystem/primitives/rustix/freebsd/fs/stat_impl.rs+++ /dev/null@@ -1,22 +0,0 @@-use crate::filesystem::primitives::{FollowSymlinks, ImplMetadataExt, Metadata, manually};-use rustix::fs::{AtFlags, statat};-use std::path::Path;-use std::{fs, io};--pub(crate) fn stat_impl(- start: &fs::File,- path: &Path,- follow: FollowSymlinks,-) -> io::Result<Metadata> {- if !super::beneath_supported() {- return manually::stat(start, path, follow);- }-- let flags = AtFlags::RESOLVE_BENEATH- | if follow == FollowSymlinks::Yes {- AtFlags::empty()- } else {- AtFlags::SYMLINK_NOFOLLOW- };- Ok(ImplMetadataExt::from_rustix(statat(start, path, flags)?))-}diff --git a/crates/wasi/src/filesystem/primitives/rustix/freebsd/mod.rs b/crates/wasi/src/filesystem/primitives/rustix/freebsd/mod.rsdeleted file mode 100644index aabc2afc5a..0000000000--- a/crates/wasi/src/filesystem/primitives/rustix/freebsd/mod.rs+++ /dev/null@@ -1 +0,0 @@-pub(crate) mod fs;diff --git a/crates/wasi/src/filesystem/primitives/rustix/fs/create_dir_unchecked.rs b/crates/wasi/src/filesystem/primitives/rustix/fs/create_dir_unchecked.rsdeleted file mode 100644index 246bed20da..0000000000--- a/crates/wasi/src/filesystem/primitives/rustix/fs/create_dir_unchecked.rs+++ /dev/null@@ -1,19 +0,0 @@-use crate::filesystem::primitives::DirOptions;-use rustix::fs::{Mode, RawMode, mkdirat};-use std::path::Path;-use std::{fs, io};--/// *Unsandboxed* function similar to `create_dir`, but which does not perform-/// sandboxing.-pub(crate) fn create_dir_unchecked(- start: &fs::File,- path: &Path,- options: &DirOptions,-) -> io::Result<()> {- #[cfg(not(target_os = "wasi"))]- let raw_mode = options.ext.mode as RawMode;- #[cfg(target_os = "wasi")]- let raw_mode = 0;-- Ok(mkdirat(start, path, Mode::from_bits(raw_mode).unwrap())?)-}diff --git a/crates/wasi/src/filesystem/primitives/rustix/fs/dir_options_ext.rs b/crates/wasi/src/filesystem/primitives/rustix/fs/dir_options_ext.rsdeleted file mode 100644index 474d6aea40..0000000000--- a/crates/wasi/src/filesystem/primitives/rustix/fs/dir_options_ext.rs+++ /dev/null@@ -1,14 +0,0 @@-#[derive(Debug, Clone)]-pub(crate) struct DirOptionsExt {- pub(super) mode: u32,-}--impl DirOptionsExt {- pub(crate) const fn new() -> Self {- Self {- // The default value; see- // <https://doc.rust-lang.org/std/os/unix/fs/trait.DirBuilderExt.html#tymethod.mode>- mode: 0o777,- }- }-}diff --git a/crates/wasi/src/filesystem/primitives/rustix/fs/mod.rs b/crates/wasi/src/filesystem/primitives/rustix/fs/mod.rsdeleted file mode 100644index df34362c94..0000000000--- a/crates/wasi/src/filesystem/primitives/rustix/fs/mod.rs+++ /dev/null@@ -1,87 +0,0 @@-mod create_dir_unchecked;-mod dir_entry_inner;-#[cfg(not(target_os = "wasi"))]-mod dir_options_ext;-mod dir_utils;-mod file_type_ext;-mod hard_link_unchecked;-mod is_same_file;-mod metadata_ext;-mod oflags;-mod open_options_ext;-mod open_unchecked;-mod read_dir_inner;-mod read_link_unchecked;-mod remove_dir_unchecked;-mod remove_file_unchecked;-mod rename_unchecked;-#[cfg(not(any(target_os = "android", target_os = "linux")))]-mod set_times_impl;-mod stat_unchecked;-mod symlink_unchecked;-mod times;--pub(crate) mod errors;--// On Linux, use optimized implementations based on-// `openat2` and `O_PATH` when available.-//-// On FreeBSD, use optimized implementations based on-// `O_RESOLVE_BENEATH`/`AT_RESOLVE_BENEATH` and `O_PATH` when available.-#[cfg(target_os = "freebsd")]-pub(crate) use crate::filesystem::primitives::rustix::freebsd::fs::*;-#[cfg(any(target_os = "android", target_os = "linux"))]-pub(crate) use crate::filesystem::primitives::rustix::linux::fs::*;-#[cfg(not(any(target_os = "android", target_os = "linux", target_os = "freebsd")))]-#[rustfmt::skip]-pub(crate) use crate::filesystem::primitives::{- manually::open as open_impl,- manually::stat as stat_impl,- via_parent::set_times_nofollow as set_times_nofollow_impl,-};-#[cfg(not(any(target_os = "android", target_os = "linux", target_os = "freebsd")))]-pub(crate) use set_times_impl::set_times_impl;-#[cfg(target_os = "freebsd")]-pub(crate) use set_times_impl::set_times_impl as set_times_manually;-#[rustfmt::skip]-pub(crate) use crate::filesystem::primitives::{- via_parent::hard_link as hard_link_impl,- via_parent::create_dir as create_dir_impl,- via_parent::read_link as read_link_impl,- via_parent::rename as rename_impl,- via_parent::symlink as symlink_impl,-};-#[cfg(not(target_os = "freebsd"))]-#[rustfmt::skip]-pub(crate) use crate::filesystem::primitives::{- via_parent::remove_dir as remove_dir_impl,- via_parent::remove_file as remove_file_impl,-};--pub(crate) use create_dir_unchecked::create_dir_unchecked;-pub(crate) use dir_entry_inner::DirEntryInner;-#[cfg(not(target_os = "wasi"))]-pub(crate) use dir_options_ext::DirOptionsExt;-pub(crate) use dir_utils::*;-pub(crate) use file_type_ext::ImplFileTypeExt;-pub(crate) use hard_link_unchecked::hard_link_unchecked;-#[allow(unused_imports)]-pub(crate) use is_same_file::{is_different_file, is_different_file_metadata, is_same_file};-pub(crate) use metadata_ext::ImplMetadataExt;-pub(crate) use open_options_ext::ImplOpenOptionsExt;-pub(crate) use open_unchecked::open_unchecked;-pub(crate) use read_dir_inner::ReadDirInner;-pub(crate) use read_link_unchecked::read_link_unchecked;-pub(crate) use remove_dir_unchecked::remove_dir_unchecked;-pub(crate) use remove_file_unchecked::remove_file_unchecked;-pub(crate) use rename_unchecked::rename_unchecked;-pub(crate) use stat_unchecked::stat_unchecked;-pub(crate) use symlink_unchecked::symlink_unchecked;-#[allow(unused_imports)]-pub(crate) use times::{set_times_follow_unchecked, set_times_nofollow_unchecked, to_timespec};--// On Linux, there is a limit of 40 symlink expansions.-// Source: <https://man7.org/linux/man-pages/man7/path_resolution.7.html>-pub(crate) const MAX_SYMLINK_EXPANSIONS: u8 = 40;--pub(super) use oflags::*;diff --git a/crates/wasi/src/filesystem/primitives/rustix/fs/set_times_impl.rs b/crates/wasi/src/filesystem/primitives/rustix/fs/set_times_impl.rsdeleted file mode 100644index 4e7363c50d..0000000000--- a/crates/wasi/src/filesystem/primitives/rustix/fs/set_times_impl.rs+++ /dev/null@@ -1,52 +0,0 @@-//! This module consists of helper types and functions for dealing-//! with setting the file times.--use crate::filesystem::primitives::{OpenOptions, open};-use rustix::io::Errno;-use std::path::Path;-use std::time::SystemTime;-use std::{fs, io};--pub(crate) fn set_times_impl(- start: &fs::File,- path: &Path,- atime: Option<SystemTime>,- mtime: Option<SystemTime>,-) -> io::Result<()> {- let mut times = fs::FileTimes::new();- if let Some(atime) = atime {- times = times.set_accessed(atime);- }- if let Some(mtime) = mtime {- times = times.set_modified(mtime);- }- // Try `futimens` with a normal handle. Normal handles need some kind of- // access, so first try write.- match open(start, path, OpenOptions::new().write(true)) {- Ok(file) => return fs::File::from(file).set_times(times),- Err(err) => match Errno::from_io_error(&err) {- Some(Errno::ACCESS) | Some(Errno::ISDIR) => (),- _ => return Err(err),- },- }-- // Next try read.- match open(start, path, OpenOptions::new().read(true)) {- Ok(file) => return fs::File::from(file).set_times(times),- Err(err) => match Errno::from_io_error(&err) {- Some(Errno::ACCESS) => (),- _ => return Err(err),- },- }-- // It's not possible to do anything else with generic POSIX. Plain- // `utimensat` has two options:- // - Follow symlinks, which would open up a race in which a concurrent- // modification of the symlink could point outside the sandbox and we- // wouldn't be able to detect it, or- // - Don't follow symlinks, which would modify the timestamp of the symlink- // instead of the file we're trying to get to.- //- // So neither does what we need.- Err(Errno::NOTSUP.into())-}diff --git a/crates/wasi/src/filesystem/primitives/rustix/fs/times.rs b/crates/wasi/src/filesystem/primitives/rustix/fs/times.rsdeleted file mode 100644index b3253e94c4..0000000000--- a/crates/wasi/src/filesystem/primitives/rustix/fs/times.rs+++ /dev/null@@ -1,57 +0,0 @@-use rustix::fs::{AtFlags, Timestamps, UTIME_NOW, UTIME_OMIT, utimensat};-use rustix::time::Timespec;-use std::os::fd::BorrowedFd;-use std::path::Path;-use std::time::SystemTime;-use std::{fs, io};--#[allow(clippy::useless_conversion)]-pub(crate) fn to_timespec(ft: Option<SystemTime>) -> io::Result<Timespec> {- Ok(match ft {- None => Timespec {- tv_sec: 0,- tv_nsec: UTIME_OMIT.into(),- },- Some(ft) => {- let duration = ft.duration_since(SystemTime::UNIX_EPOCH).unwrap();- let nanoseconds = duration.subsec_nanos();- assert_ne!(i64::from(nanoseconds), i64::from(UTIME_OMIT));- assert_ne!(i64::from(nanoseconds), i64::from(UTIME_NOW));- Timespec {- tv_sec: duration- .as_secs()- .try_into()- .map_err(|err| io::Error::new(io::ErrorKind::Other, err))?,- tv_nsec: nanoseconds.try_into().unwrap(),- }- }- })-}--#[allow(dead_code)]-pub(crate) fn set_times_nofollow_unchecked(- start: &fs::File,- path: &Path,- atime: Option<SystemTime>,- mtime: Option<SystemTime>,-) -> io::Result<()> {- let times = Timestamps {- last_access: to_timespec(atime)?,- last_modification: to_timespec(mtime)?,- };- Ok(utimensat(start, path, ×, AtFlags::SYMLINK_NOFOLLOW)?)-}--#[allow(dead_code)]-pub(crate) fn set_times_follow_unchecked(- start: BorrowedFd<'_>,- path: &Path,- atime: Option<SystemTime>,- mtime: Option<SystemTime>,-) -> io::Result<()> {- let times = Timestamps {- last_access: to_timespec(atime)?,- last_modification: to_timespec(mtime)?,- };- Ok(utimensat(start, path, ×, AtFlags::empty())?)-}diff --git a/crates/wasi/src/filesystem/primitives/rustix/linux/fs/file_metadata.rs b/crates/wasi/src/filesystem/primitives/rustix/linux/fs/file_metadata.rsdeleted file mode 100644index b02b95c3b7..0000000000--- a/crates/wasi/src/filesystem/primitives/rustix/linux/fs/file_metadata.rs+++ /dev/null@@ -1,27 +0,0 @@-use crate::filesystem::primitives::{ImplMetadataExt, Metadata};-use rustix::fs::{AtFlags, statat};-use std::sync::atomic::AtomicBool;-use std::sync::atomic::Ordering::Relaxed;-use std::{fs, io};--/// Like `file.metadata()`, but works with `O_PATH` descriptors on old (pre-/// 3.6) versions of Linux too.-pub(super) fn file_metadata(file: &fs::File) -> io::Result<Metadata> {- // Record whether we've seen an `EBADF` from an `fstat` on an `O_PATH`- // file descriptor, meaning we're on a Linux that doesn't support it.- static FSTAT_PATH_BADF: AtomicBool = AtomicBool::new(false);-- if !FSTAT_PATH_BADF.load(Relaxed) {- match Metadata::from_file(file) {- Ok(metadata) => return Ok(metadata),- Err(err) => match rustix::io::Errno::from_io_error(&err) {- // Before Linux 3.6, `fstat` with `O_PATH` returned `EBADF`.- Some(rustix::io::Errno::BADF) => FSTAT_PATH_BADF.store(true, Relaxed),- _ => return Err(err),- },- }- }-- // If `fstat` with `O_PATH` isn't supported, use `statat` with `AT_EMPTY_PATH`.- Ok(statat(file, "", AtFlags::EMPTY_PATH).map(ImplMetadataExt::from_rustix)?)-}diff --git a/crates/wasi/src/filesystem/primitives/rustix/linux/fs/mod.rs b/crates/wasi/src/filesystem/primitives/rustix/linux/fs/mod.rsdeleted file mode 100644index 300e5a7714..0000000000--- a/crates/wasi/src/filesystem/primitives/rustix/linux/fs/mod.rs+++ /dev/null@@ -1,21 +0,0 @@-#[cfg(target_os = "linux")]-mod file_metadata;-mod open_impl;-mod procfs;-mod set_times_impl;-#[cfg(target_os = "linux")]-mod stat_impl;--#[cfg(target_os = "android")]-pub(crate) use crate::filesystem::primitives::manually::stat as stat_impl;-pub(crate) use crate::filesystem::primitives::via_parent::set_times_nofollow as set_times_nofollow_impl;-#[cfg(target_os = "linux")]-pub(crate) use open_impl::open_beneath;-pub(crate) use open_impl::open_impl;-pub(crate) use set_times_impl::set_times_impl;-#[cfg(target_os = "linux")]-pub(crate) use stat_impl::stat_impl;--// In theory we could optimize `link` using `openat2` with `O_PATH` and-// `linkat` with `AT_EMPTY_PATH`, however that requires `CAP_DAC_READ_SEARCH`,-// so it isn't very widely applicable.diff --git a/crates/wasi/src/filesystem/primitives/rustix/linux/fs/open_impl.rs b/crates/wasi/src/filesystem/primitives/rustix/linux/fs/open_impl.rsdeleted file mode 100644index 0cd20cdde9..0000000000--- a/crates/wasi/src/filesystem/primitives/rustix/linux/fs/open_impl.rs+++ /dev/null@@ -1,126 +0,0 @@-//! Linux 5.6 and later have a syscall `openat2`, with flags that allow it to-//! enforce the sandboxing property we want. See the [LWN article] for an-//! overview and the [`openat2` documentation] for details.-//!-//! [LWN article]: https://lwn.net/Articles/796868/-//! [`openat2` documentation]: https://man7.org/linux/man-pages/man2/openat2.2.html-//!-//! On older Linux, fall back to `manually::open`.--use crate::filesystem::primitives::{OpenOptions, manually};-use std::path::Path;-use std::{fs, io};-#[cfg(target_os = "linux")]-use {- super::super::super::fs::compute_oflags,- crate::filesystem::primitives::errors,- rustix::fs::{Mode, OFlags, RawMode, ResolveFlags, openat2},- rustix::path::Arg,- std::sync::atomic::AtomicBool,- std::sync::atomic::Ordering::Relaxed,-};--/// Call the `openat2` system call, or use a fallback if that's unavailable.-pub(crate) fn open_impl(- start: &fs::File,- path: &Path,- options: &OpenOptions,-) -> io::Result<fs::File> {- // On regular Linux, attempt to use `openat2` to accelerate sandboxed- // lookups. On Android, the [seccomp policy] prevents us from even- // detecting whether `openat2` is supported, so don't even try.- //- // [seccomp policy]: https://android-developers.googleblog.com/2017/07/seccomp-filter-in-android-o.html- #[cfg(target_os = "linux")]- {- let result = open_beneath(start, path, options);-- // If we got anything other than a `ENOSYS` error, that's our result.- match result {- Err(err) if err.raw_os_error() == Some(rustix::io::Errno::NOSYS.raw_os_error()) => {}- Err(err) => return Err(err),- Ok(fd) => return Ok(fd),- }- }-- manually::open(start, path, options)-}--/// Call the `openat2` system call with `RESOLVE_BENEATH`. If the syscall is-/// unavailable, mark it so for future calls. If `openat2` is unavailable-/// either permanently or temporarily, return `ENOSYS`.-#[cfg(target_os = "linux")]-pub(crate) fn open_beneath(- start: &fs::File,- path: &Path,- options: &OpenOptions,-) -> io::Result<fs::File> {- static INVALID: AtomicBool = AtomicBool::new(false);- if INVALID.load(Relaxed) {- // `openat2` is permanently unavailable.- return Err(rustix::io::Errno::NOSYS.into());- }-- let oflags = compute_oflags(options)?;-- // Do two `contains` checks because `TMPFILE` may be represented with- // multiple flags and we need to ensure they're all set.- let mode = if oflags.contains(OFlags::CREATE) || oflags.contains(OFlags::TMPFILE) {- Mode::from_bits((options.ext.mode & 0o7777) as RawMode).unwrap()- } else {- Mode::empty()- };-- // We know `openat2` needs a `&CStr` internally; to avoid allocating on- // each iteration of the loop below, allocate the `CString` now.- path.into_with_c_str(|path_c_str| {- // `openat2` fails with `EAGAIN` if a rename happens anywhere on the host- // while it's running, so use a loop to retry it a few times. But not too many- // times, because there's no limit on how often this can happen. The actual- // number here is currently an arbitrarily chosen guess.- for _ in 0..4 {- match openat2(- start,- path_c_str,- oflags,- mode,- ResolveFlags::BENEATH | ResolveFlags::NO_MAGICLINKS,- ) {- Ok(file) => {- let file = fs::File::from(file);-- return Ok(file);- }- Err(err) => match err {- // A rename or similar happened. Try again.- rustix::io::Errno::AGAIN => continue,-- // `EPERM` is used by some `seccomp` sandboxes to indicate- // that `openat2` is unimplemented:- // <https://github.com/systemd/systemd/blob/e2357b1c8a87b610066b8b2a59517bcfb20b832e/src/shared/seccomp-util.c#L2066>- //- // However, `EPERM` may also indicate a failed `O_NOATIME`- // or a file seal prevented the operation, and it's complex- // to detect those cases, so exit the loop and use the- // fallback.- rustix::io::Errno::PERM => break,-- // `ENOSYS` means `openat2` is permanently unavailable;- // mark it so and exit the loop.- rustix::io::Errno::NOSYS => {- INVALID.store(true, Relaxed);- break;- }-- _ => return Err(err),- },- }- }-- Err(rustix::io::Errno::NOSYS)- })- .map_err(|err| match err {- rustix::io::Errno::XDEV => errors::escape_attempt(),- err => err.into(),- })-}diff --git a/crates/wasi/src/filesystem/primitives/rustix/linux/fs/procfs.rs b/crates/wasi/src/filesystem/primitives/rustix/linux/fs/procfs.rsdeleted file mode 100644index 3fba32fadf..0000000000--- a/crates/wasi/src/filesystem/primitives/rustix/linux/fs/procfs.rs+++ /dev/null@@ -1,43 +0,0 @@-//! Utilities for working with `/proc`, where Linux's `procfs` is typically-//! mounted. `/proc` serves as an adjunct to Linux's main syscall surface area,-//! providing additional features with an awkward interface.-//!-//! This module does a considerable amount of work to determine whether `/proc`-//! is mounted, with actual `procfs`, and without any additional mount points-//! on top of the paths we open.--use crate::filesystem::primitives::OpenOptionsExt;-use crate::filesystem::primitives::{OpenOptions, open, set_times_follow_unchecked};-use rustix::fd::AsFd;-use rustix::fs::OFlags;-use rustix::path::DecInt;-use rustix_linux_procfs::proc_self_fd;-use std::path::Path;-use std::time::SystemTime;-use std::{fs, io};--pub(crate) fn set_times_through_proc_self_fd(- start: &fs::File,- path: &Path,- atime: Option<SystemTime>,- mtime: Option<SystemTime>,-) -> io::Result<()> {- let opath = open(- start,- path,- OpenOptions::new()- .read(true)- .custom_flags(OFlags::PATH.bits() as i32),- )?;-- // Don't pass `AT_SYMLINK_NOFOLLOW`, because we do actually want to follow- // the first symlink. We don't want to follow any subsequent symlinks, but- // omitting `O_NOFOLLOW` above ensures that the destination of the link- // isn't a symlink.- set_times_follow_unchecked(- proc_self_fd()?.as_fd(),- DecInt::from_fd(&opath).as_ref(),- atime,- mtime,- )-}diff --git a/crates/wasi/src/filesystem/primitives/rustix/linux/fs/set_times_impl.rs b/crates/wasi/src/filesystem/primitives/rustix/linux/fs/set_times_impl.rsdeleted file mode 100644index 9372ce2353..0000000000--- a/crates/wasi/src/filesystem/primitives/rustix/linux/fs/set_times_impl.rs+++ /dev/null@@ -1,44 +0,0 @@-//! This module consists of helper types and functions for dealing-//! with setting the file times specific to Linux.--use super::procfs::set_times_through_proc_self_fd;-use crate::filesystem::primitives::{OpenOptions, open};-use std::path::Path;-use std::time::SystemTime;-use std::{fs, io};--pub(crate) fn set_times_impl(- start: &fs::File,- path: &Path,- atime: Option<SystemTime>,- mtime: Option<SystemTime>,-) -> io::Result<()> {- let mut times = fs::FileTimes::new();- if let Some(atime) = atime {- times = times.set_accessed(atime);- }- if let Some(mtime) = mtime {- times = times.set_modified(mtime);- }- // Try `futimens` with a normal handle. Normal handles need some kind of- // access, so first try write.- match open(start, path, OpenOptions::new().write(true)) {- Ok(file) => return file.set_times(times),- Err(err) => match rustix::io::Errno::from_io_error(&err) {- Some(rustix::io::Errno::ACCESS) | Some(rustix::io::Errno::ISDIR) => (),- _ => return Err(err),- },- }-- // Next try read.- match open(start, path, OpenOptions::new().read(true)) {- Ok(file) => return file.set_times(times),- Err(err) => match rustix::io::Errno::from_io_error(&err) {- Some(rustix::io::Errno::ACCESS) => (),- _ => return Err(err),- },- }-- // If neither of those worked, turn to `/proc`.- set_times_through_proc_self_fd(start, path, atime, mtime)-}diff --git a/crates/wasi/src/filesystem/primitives/rustix/linux/fs/stat_impl.rs b/crates/wasi/src/filesystem/primitives/rustix/linux/fs/stat_impl.rsdeleted file mode 100644index 01a6e09a5f..0000000000--- a/crates/wasi/src/filesystem/primitives/rustix/linux/fs/stat_impl.rs+++ /dev/null@@ -1,58 +0,0 @@-//! Linux has an `O_PATH` flag which allows opening a file without necessary-//! having read or write access to it; we can use that with `openat2` and-//! `fstat` to perform a fast sandboxed `stat`.--use super::file_metadata::file_metadata;-use crate::filesystem::primitives::{- FollowSymlinks, Metadata, OpenOptions, manually, open_beneath,-};-use rustix::fs::OFlags;-use std::path::Path;-use std::{fs, io};--/// Use `openat2` with `O_PATH` and `fstat`. If that's not available, fallback-/// to `manually::stat`.-pub(crate) fn stat_impl(- start: &fs::File,- path: &Path,- follow: FollowSymlinks,-) -> io::Result<Metadata> {- use crate::filesystem::primitives::{OpenOptionsExt, stat_unchecked};- use std::path::Component;-- // Optimization: if path has exactly one component and it's not ".." or- // anything non-normal and we're not following symlinks we can go straight- // to `stat_unchecked`, which is faster than doing an open with a separate- // `fstat`.- if follow == FollowSymlinks::No {- let mut components = path.components();- if let Some(Component::Normal(component)) = components.next() {- if components.next().is_none() {- return stat_unchecked(start, component.as_ref(), FollowSymlinks::No);- }- }- }-- // Open the path with `O_PATH`. Use `read(true)` even though we don't need- // `read` permissions, because Rust's libstd requires an access mode, and- // Linux ignores `O_RDONLY` with `O_PATH`.- let result = open_beneath(- start,- path,- OpenOptions::new()- .read(true)- .follow(follow)- .custom_flags(OFlags::PATH.bits() as i32),- );-- // If that worked, call `fstat`.- match result {- Ok(file) => file_metadata(&file),- Err(err) => match rustix::io::Errno::from_io_error(&err) {- // `ENOSYS` from `open_beneath` means `openat2` is unavailable- // and we should use a fallback.- Some(rustix::io::Errno::NOSYS) => manually::stat(start, path, follow),- _ => Err(err),- },- }-}diff --git a/crates/wasi/src/filesystem/primitives/rustix/linux/mod.rs b/crates/wasi/src/filesystem/primitives/rustix/linux/mod.rsdeleted file mode 100644index 3ddb2c3266..0000000000--- a/crates/wasi/src/filesystem/primitives/rustix/linux/mod.rs+++ /dev/null@@ -1,6 +0,0 @@-//! Following [`std`], we don't carry workarounds for Linux versions-//! older than 2.6.32.-//!-//! [`std`]: https://github.com/rust-lang/rust/pull/74163--pub(crate) mod fs;diff --git a/crates/wasi/src/filesystem/primitives/rustix/mod.rs b/crates/wasi/src/filesystem/primitives/rustix/mod.rsdeleted file mode 100644index 94e6b2f297..0000000000--- a/crates/wasi/src/filesystem/primitives/rustix/mod.rs+++ /dev/null@@ -1,9 +0,0 @@-//! The `rustix` module contains code specific to the Posix-ish platforms-//! supported by the `rustix` crate.--pub(crate) mod fs;--#[cfg(target_os = "freebsd")]-mod freebsd;-#[cfg(any(target_os = "android", target_os = "linux"))]-mod linux;diff --git a/crates/wasi/src/filesystem/primitives/tests/fs.rs b/crates/wasi/src/filesystem/primitives/tests/fs.rsindex 205878a47f..71a5b221b2 100644--- a/crates/wasi/src/filesystem/primitives/tests/fs.rs+++ b/crates/wasi/src/filesystem/primitives/tests/fs.rs@@ -45,7 +45,7 @@ fn open_directory_with_truncate_is_error() {let mut options = p::OpenOptions::new();// The `maybe_dir` part of this test is gone along with the option itself.options.truncate(true).read(true).write(true);- p::create_dir(&start, Path::new("test"), &p::DirOptions::new()).unwrap();+ p::create_dir(&start, Path::new("test")).unwrap();assert!(p::open(&start, Path::new("test"), &options).is_err());}@@ -828,7 +828,7 @@ fn readlink_not_symlink() {#[cfg(not(windows))]#[test]-fn read_link_contents() {+fn read_link_relative() {let tmpdir = tmpdir();let start = h::dir_of(&tmpdir);let link = "link";@@ -837,16 +837,19 @@ fn read_link_contents() {};check!(h::symlink_file(&start, &"foo", &link));assert_eq!(- check!(super::super::read_link_contents(&start, Path::new(link)))+ check!(super::super::read_link(&start, Path::new(link))).to_str().unwrap(),"foo");}+/// Reading a symlink whose target is absolute is refused, even though the+/// underlying `readlinkat` would succeed, to avoid leaking information about+/// the host filesystem outside the sandbox.#[cfg(not(windows))]#[test]-fn read_link_contents_absolute() {+fn read_link_absolute() {let tmpdir = tmpdir();let start = h::dir_of(&tmpdir);let link = "link";@@ -854,11 +857,9 @@ fn read_link_contents_absolute() {return;};check!(std::os::unix::fs::symlink("/foo", tmpdir.path().join(link)));- assert_eq!(- check!(super::super::read_link_contents(&start, Path::new(link)))- .to_str()- .unwrap(),- "/foo"+ error_contains!(+ super::super::read_link(&start, Path::new(link)),+ "a path led outside of the filesystem");}diff --git a/crates/wasi/src/filesystem/primitives/tests/fs_additional.rs b/crates/wasi/src/filesystem/primitives/tests/fs_additional.rsindex d804538ac6..ce0faa4315 100644--- a/crates/wasi/src/filesystem/primitives/tests/fs_additional.rs+++ b/crates/wasi/src/filesystem/primitives/tests/fs_additional.rs@@ -99,15 +99,9 @@ fn optionally_nonrecursive_mkdir() {let start = h::dir_of(&tmpdir);let dir = "d1/d2";#[cfg(not(windows))]- error!(- p::create_dir(&start, Path::new(dir), &p::DirOptions::new()),- "No such file"- );+ error!(p::create_dir(&start, Path::new(dir)), "No such file");#[cfg(windows)]- error!(- p::create_dir(&start, Path::new(dir), &p::DirOptions::new()),- 2- );+ error!(p::create_dir(&start, Path::new(dir)), 2);assert!(!h::exists(&start, dir));}diff --git a/crates/wasi/src/filesystem/primitives/tests/helpers/mod.rs b/crates/wasi/src/filesystem/primitives/tests/helpers/mod.rsindex 22a36c6a00..ff6b5b1a67 100644--- a/crates/wasi/src/filesystem/primitives/tests/helpers/mod.rs+++ b/crates/wasi/src/filesystem/primitives/tests/helpers/mod.rs@@ -38,9 +38,9 @@ pub fn open(d: &File, path: impl AsRef<Path>) -> io::Result<File> {p::open(d, path.as_ref(), p::OpenOptions::new().read(true))}-/// `Dir::create_dir`, with the default `DirOptions`.+/// `Dir::create_dir`.pub fn create_dir(d: &File, path: impl AsRef<Path>) -> io::Result<()> {- p::create_dir(d, path.as_ref(), &p::DirOptions::new())+ p::create_dir(d, path.as_ref())}/// `Dir::create_dir_all`, in terms of the single-level `create_dir`.@@ -48,7 +48,7 @@ pub fn create_dir_all(d: &File, path: impl AsRef<Path>) -> io::Result<()> {let mut acc = PathBuf::new();for component in path.as_ref().components() {acc.push(component);- match p::create_dir(d, &acc, &p::DirOptions::new()) {+ match p::create_dir(d, &acc) {Ok(()) => {}Err(e) if e.kind() == io::ErrorKind::AlreadyExists => {}Err(e) => return Err(e),diff --git a/crates/wasi/src/filesystem/primitives/tests/paths_containing_nul.rs b/crates/wasi/src/filesystem/primitives/tests/paths_containing_nul.rsindex 4c9247ab19..5332c3cac8 100644--- a/crates/wasi/src/filesystem/primitives/tests/paths_containing_nul.rs+++ b/crates/wasi/src/filesystem/primitives/tests/paths_containing_nul.rs@@ -11,8 +11,8 @@use super::sys_common::io::tmpdir;use crate::filesystem::primitives::{- DirOptions, FollowSymlinks, OpenOptions, create_dir, hard_link, open, open_ambient_dir,- read_link, remove_dir, remove_file, rename, stat,+ FollowSymlinks, OpenOptions, create_dir, hard_link, open, open_ambient_dir, read_link,+ remove_dir, remove_file, rename, stat,};use std::io;use std::path::Path;@@ -72,6 +72,6 @@ fn paths_containing_nul() {assert_invalid_input("hard_link1", hard_link(&dir, nul, &dir, Path::new("a")));assert_invalid_input("hard_link2", hard_link(&dir, dummy_file, &dir, nul));assert_invalid_input("read_link", read_link(&dir, nul));- assert_invalid_input("create_dir", create_dir(&dir, nul, &DirOptions::new()));+ assert_invalid_input("create_dir", create_dir(&dir, nul));assert_invalid_input("remove_dir", remove_dir(&dir, nul));}diff --git a/crates/wasi/src/filesystem/primitives/tests/sys_common/symlink_junction.rs b/crates/wasi/src/filesystem/primitives/tests/sys_common/symlink_junction.rsindex 840dd8fd14..7dad47dc29 100644--- a/crates/wasi/src/filesystem/primitives/tests/sys_common/symlink_junction.rs+++ b/crates/wasi/src/filesystem/primitives/tests/sys_common/symlink_junction.rs@@ -66,16 +66,14 @@ pub fn cvt(i: windows_sys::core::BOOL) -> io::Result<windows_sys::core::BOOL> {#[cfg(windows)]#[allow(dead_code)]fn symlink_junction_inner(original: &Path, dir: &fs::File, junction: &Path) -> io::Result<()> {- use crate::filesystem::primitives::{- DirOptions, OpenOptions, OpenOptionsExt, create_dir, open,- };+ use crate::filesystem::primitives::{OpenOptions, OpenOptionsExt, create_dir, open};use std::mem::MaybeUninit;use std::os::windows::ffi::OsStrExt;use std::os::windows::io::AsRawHandle;use std::{mem, ptr};use windows_sys::Win32::Storage::FileSystem::MAXIMUM_REPARSE_DATA_BUFFER_SIZE;- create_dir(dir, junction, &DirOptions::new())?;+ create_dir(dir, junction)?;let mut opts = OpenOptions::new();opts.write(true);diff --git a/crates/wasi/src/filesystem/primitives/unix/create_dir_unchecked.rs b/crates/wasi/src/filesystem/primitives/unix/create_dir_unchecked.rsnew file mode 100644index 0000000000..9194320862--- /dev/null+++ b/crates/wasi/src/filesystem/primitives/unix/create_dir_unchecked.rs@@ -0,0 +1,11 @@+use rustix::fs::{Mode, mkdirat};+use std::path::Path;+use std::{fs, io};++/// *Unsandboxed* function similar to `create_dir`, but which does not perform+/// sandboxing.+pub(crate) fn create_dir_unchecked(start: &fs::File, path: &Path) -> io::Result<()> {+ // Mirror `std::fs::DirBuilder` by requesting a mode of `0o777`; the+ // process umask will then filter this down.+ Ok(mkdirat(start, path, Mode::RWXU | Mode::RWXG | Mode::RWXO)?)+}diff --git a/crates/wasi/src/filesystem/primitives/rustix/fs/cvt.rs b/crates/wasi/src/filesystem/primitives/unix/cvt.rssimilarity index 100%rename from crates/wasi/src/filesystem/primitives/rustix/fs/cvt.rsrename to crates/wasi/src/filesystem/primitives/unix/cvt.rsdiff --git a/crates/wasi/src/filesystem/primitives/rustix/fs/dir_entry_inner.rs b/crates/wasi/src/filesystem/primitives/unix/dir_entry_inner.rssimilarity index 100%rename from crates/wasi/src/filesystem/primitives/rustix/fs/dir_entry_inner.rsrename to crates/wasi/src/filesystem/primitives/unix/dir_entry_inner.rsdiff --git a/crates/wasi/src/filesystem/primitives/rustix/fs/dir_utils.rs b/crates/wasi/src/filesystem/primitives/unix/dir_utils.rssimilarity index 99%rename from crates/wasi/src/filesystem/primitives/rustix/fs/dir_utils.rsrename to crates/wasi/src/filesystem/primitives/unix/dir_utils.rsindex 078b7042f2..469a33a0ec 100644--- a/crates/wasi/src/filesystem/primitives/rustix/fs/dir_utils.rs+++ b/crates/wasi/src/filesystem/primitives/unix/dir_utils.rs@@ -96,7 +96,7 @@ pub(crate) fn readdir_options() -> OpenOptions {////// This function is not sandboxed and may trivially access any path that the/// host process has access to.-pub(crate) fn open_ambient_dir_impl(path: &Path) -> io::Result<fs::File> {+pub(crate) fn open_ambient_dir(path: &Path) -> io::Result<fs::File> {let mut options = fs::OpenOptions::new();options.read(true);diff --git a/crates/wasi/src/filesystem/primitives/rustix/fs/errors.rs b/crates/wasi/src/filesystem/primitives/unix/errors.rssimilarity index 100%rename from crates/wasi/src/filesystem/primitives/rustix/fs/errors.rsrename to crates/wasi/src/filesystem/primitives/unix/errors.rsdiff --git a/crates/wasi/src/filesystem/primitives/rustix/fs/file_type_ext.rs b/crates/wasi/src/filesystem/primitives/unix/file_type_ext.rssimilarity index 100%rename from crates/wasi/src/filesystem/primitives/rustix/fs/file_type_ext.rsrename to crates/wasi/src/filesystem/primitives/unix/file_type_ext.rsdiff --git a/crates/wasi/src/filesystem/primitives/unix/freebsd.rs b/crates/wasi/src/filesystem/primitives/unix/freebsd.rsnew file mode 100644index 0000000000..0328cd95ba--- /dev/null+++ b/crates/wasi/src/filesystem/primitives/unix/freebsd.rs@@ -0,0 +1,138 @@+use crate::filesystem::primitives::unix::{compute_oflags, to_timespec};+use crate::filesystem::primitives::{+ FollowSymlinks, ImplMetadataExt, Metadata, OpenOptions, errors,+};+use rustix::fs::{+ AtFlags, CWD, Mode, OFlags, RawMode, Timestamps, openat, statat, unlinkat, utimensat,+};+use rustix::io::Errno;+use std::fs;+use std::io;+use std::path::Path;+use std::sync::atomic::{AtomicBool, Ordering::Relaxed};+use std::time::SystemTime;++static WORKING: AtomicBool = AtomicBool::new(false);+static CHECKED: AtomicBool = AtomicBool::new(false);++#[inline]+fn beneath_supported() -> bool {+ if WORKING.load(Relaxed) {+ return true;+ }+ if CHECKED.load(Relaxed) {+ return false;+ }+ check_beneath_supported()+}++#[cold]+fn check_beneath_supported() -> bool {+ // `RESOLVE_BENEATH` was introduced in FreeBSD 13, but opening `..` within+ // the root directory re-opened the root directory. In FreeBSD 14, it fails+ // as cap-std expects.+ if let Ok(root) = openat(CWD, c"/", OFlags::RDONLY | OFlags::CLOEXEC, Mode::empty()) {+ // Unknown O_ flags get ignored but AT_ flags have strict checks, so we use that.+ if let Err(Errno::NOTCAPABLE) = statat(root, c"..", AtFlags::RESOLVE_BENEATH) {+ WORKING.store(true, Relaxed);+ return true;+ }+ }++ CHECKED.store(true, Relaxed);+ false+}++pub(crate) fn open_fast(+ start: &fs::File,+ path: &Path,+ options: &OpenOptions,+) -> io::Result<Option<fs::File>> {+ if !beneath_supported() {+ return Ok(None);+ }++ let oflags = compute_oflags(options)? | OFlags::RESOLVE_BENEATH;++ let mode = if oflags.contains(OFlags::CREATE) {+ Mode::from_bits((options.ext.mode & 0o7777) as RawMode).unwrap()+ } else {+ Mode::empty()+ };++ match openat(start, path, oflags, mode) {+ Ok(file) => Ok(Some(file.into())),+ Err(rustix::io::Errno::NOTCAPABLE) => Err(errors::escape_attempt()),+ Err(err) => Err(err.into()),+ }+}++pub(crate) fn remove_file_fast(start: &fs::File, path: &Path) -> io::Result<bool> {+ if !beneath_supported() {+ return Ok(false);+ }+ unlinkat(start, path, AtFlags::RESOLVE_BENEATH)?;+ Ok(true)+}++pub(crate) fn remove_dir_fast(start: &fs::File, path: &Path) -> io::Result<bool> {+ if !beneath_supported() {+ return Ok(false);+ }+ unlinkat(start, path, AtFlags::RESOLVE_BENEATH | AtFlags::REMOVEDIR)?;+ Ok(true)+}++pub(crate) fn set_times_nofollow_fast(+ start: &fs::File,+ path: &Path,+ times: &Timestamps,+) -> io::Result<bool> {+ if !beneath_supported() {+ return Ok(false);+ }+ utimensat(+ start,+ path,+ times,+ AtFlags::RESOLVE_BENEATH | AtFlags::SYMLINK_NOFOLLOW,+ )?;+ Ok(true)+}++pub(crate) fn stat_fast(+ start: &fs::File,+ path: &Path,+ follow: FollowSymlinks,+) -> io::Result<Option<Metadata>> {+ if !beneath_supported() {+ return Ok(None);+ }+ let flags = AtFlags::RESOLVE_BENEATH+ | if follow == FollowSymlinks::Yes {+ AtFlags::empty()+ } else {+ AtFlags::SYMLINK_NOFOLLOW+ };+ let stat = ImplMetadataExt::from_rustix(statat(start, path, flags)?);+ Ok(Some(stat))+}++pub(crate) fn set_times_fast(+ start: &fs::File,+ path: &Path,+ atime: Option<SystemTime>,+ mtime: Option<SystemTime>,+) -> io::Result<bool> {+ if !beneath_supported() {+ return Ok(false);+ }++ let times = Timestamps {+ last_access: to_timespec(atime)?,+ last_modification: to_timespec(mtime)?,+ };++ utimensat(start, path, ×, AtFlags::RESOLVE_BENEATH)?;+ Ok(true)+}diff --git a/crates/wasi/src/filesystem/primitives/rustix/fs/hard_link_unchecked.rs b/crates/wasi/src/filesystem/primitives/unix/hard_link_unchecked.rssimilarity index 100%rename from crates/wasi/src/filesystem/primitives/rustix/fs/hard_link_unchecked.rsrename to crates/wasi/src/filesystem/primitives/unix/hard_link_unchecked.rsdiff --git a/crates/wasi/src/filesystem/primitives/rustix/fs/is_same_file.rs b/crates/wasi/src/filesystem/primitives/unix/is_same_file.rssimilarity index 100%rename from crates/wasi/src/filesystem/primitives/rustix/fs/is_same_file.rsrename to crates/wasi/src/filesystem/primitives/unix/is_same_file.rsdiff --git a/crates/wasi/src/filesystem/primitives/unix/linux.rs b/crates/wasi/src/filesystem/primitives/unix/linux.rsnew file mode 100644index 0000000000..97d2cf3af0--- /dev/null+++ b/crates/wasi/src/filesystem/primitives/unix/linux.rs@@ -0,0 +1,207 @@+use crate::filesystem::primitives::unix::{compute_oflags, to_timespec};+use crate::filesystem::primitives::{+ FollowSymlinks, ImplMetadataExt, Metadata, OpenOptions, OpenOptionsExt, errors, open,+};+use rustix::fs::{+ AtFlags, Mode, OFlags, RawMode, ResolveFlags, Timestamps, openat2, statat, utimensat,+};+use rustix::io::Errno;+use rustix::path::{Arg, DecInt};+use rustix_linux_procfs::proc_self_fd;+use std::fs;+use std::io;+use std::os::fd::AsFd;+use std::path::Path;+use std::sync::atomic::{AtomicBool, Ordering::Relaxed};+use std::time::SystemTime;++/// Linux 5.6 and later have a syscall `openat2`, with flags that allow it to+/// enforce the sandboxing property we want. See the [LWN article] for an+/// overview and the [`openat2` documentation] for details.+///+/// [LWN article]: https://lwn.net/Articles/796868/+/// [`openat2` documentation]: https://man7.org/linux/man-pages/man2/openat2.2.html+///+/// On older Linux, fall back to `manually::open`.+pub(crate) fn open_fast(+ start: &fs::File,+ path: &Path,+ options: &OpenOptions,+) -> io::Result<Option<fs::File>> {+ // On regular Linux, attempt to use `openat2` to accelerate sandboxed+ // lookups. On Android, the [seccomp policy] prevents us from even+ // detecting whether `openat2` is supported, so don't even try.+ //+ // [seccomp policy]: https://android-developers.googleblog.com/2017/07/seccomp-filter-in-android-o.html+ let result = open_beneath(start, path, options);++ // If we got anything other than a `ENOSYS` error, that's our result.+ match result {+ Err(err) if err.raw_os_error() == Some(rustix::io::Errno::NOSYS.raw_os_error()) => Ok(None),+ Err(err) => Err(err),+ Ok(fd) => Ok(Some(fd)),+ }+}++/// Call the `openat2` system call with `RESOLVE_BENEATH`. If the syscall is+/// unavailable, mark it so for future calls. If `openat2` is unavailable+/// either permanently or temporarily, return `ENOSYS`.+fn open_beneath(start: &fs::File, path: &Path, options: &OpenOptions) -> io::Result<fs::File> {+ static INVALID: AtomicBool = AtomicBool::new(false);+ if INVALID.load(Relaxed) {+ // `openat2` is permanently unavailable.+ return Err(rustix::io::Errno::NOSYS.into());+ }++ let oflags = compute_oflags(options)?;++ // Do two `contains` checks because `TMPFILE` may be represented with+ // multiple flags and we need to ensure they're all set.+ let mode = if oflags.contains(OFlags::CREATE) || oflags.contains(OFlags::TMPFILE) {+ Mode::from_bits((options.ext.mode & 0o7777) as RawMode).unwrap()+ } else {+ Mode::empty()+ };++ // We know `openat2` needs a `&CStr` internally; to avoid allocating on+ // each iteration of the loop below, allocate the `CString` now.+ path.into_with_c_str(|path_c_str| {+ // `openat2` fails with `EAGAIN` if a rename happens anywhere on the host+ // while it's running, so use a loop to retry it a few times. But not too many+ // times, because there's no limit on how often this can happen. The actual+ // number here is currently an arbitrarily chosen guess.+ for _ in 0..4 {+ match openat2(+ start,+ path_c_str,+ oflags,+ mode,+ ResolveFlags::BENEATH | ResolveFlags::NO_MAGICLINKS,+ ) {+ Ok(file) => {+ let file = fs::File::from(file);++ return Ok(file);+ }+ Err(err) => match err {+ // A rename or similar happened. Try again.+ rustix::io::Errno::AGAIN => continue,++ // `EPERM` is used by some `seccomp` sandboxes to indicate+ // that `openat2` is unimplemented:+ // <https://github.com/systemd/systemd/blob/e2357b1c8a87b610066b8b2a59517bcfb20b832e/src/shared/seccomp-util.c#L2066>+ //+ // However, `EPERM` may also indicate a failed `O_NOATIME`+ // or a file seal prevented the operation, and it's complex+ // to detect those cases, so exit the loop and use the+ // fallback.+ rustix::io::Errno::PERM => break,++ // `ENOSYS` means `openat2` is permanently unavailable;+ // mark it so and exit the loop.+ rustix::io::Errno::NOSYS => {+ INVALID.store(true, Relaxed);+ break;+ }++ _ => return Err(err),+ },+ }+ }++ Err(rustix::io::Errno::NOSYS)+ })+ .map_err(|err| match err {+ rustix::io::Errno::XDEV => errors::escape_attempt(),+ err => err.into(),+ })+}++// In theory we could optimize `link` using `openat2` with `O_PATH` and+// `linkat` with `AT_EMPTY_PATH`, however that requires `CAP_DAC_READ_SEARCH`,+// so it isn't very widely applicable.++/// Open the path with `O_PATH`. Use `read(true)` even though we don't need+/// `read` permissions, because Rust's libstd requires an access mode, and Linux+/// ignores `O_RDONLY` with `O_PATH`.+pub(crate) fn stat_fast(+ start: &fs::File,+ path: &Path,+ follow: FollowSymlinks,+) -> io::Result<Option<Metadata>> {+ let result = open_beneath(+ start,+ path,+ OpenOptions::new()+ .read(true)+ .follow(follow)+ .custom_flags(OFlags::PATH.bits() as i32),+ );++ match result {+ // Like `file.metadata()`, but works with `O_PATH` descriptors on old+ // (pre 3.6) versions of Linux too.+ Ok(file) => {+ // Record whether we've seen an `EBADF` from an `fstat` on an+ // `O_PATH` file descriptor, meaning we're on a Linux that doesn't+ // support it.+ static FSTAT_PATH_BADF: AtomicBool = AtomicBool::new(false);++ if !FSTAT_PATH_BADF.load(Relaxed) {+ match Metadata::from_file(&file) {+ Ok(metadata) => return Ok(Some(metadata)),+ Err(err) => match Errno::from_io_error(&err) {+ // Before Linux 3.6, `fstat` with `O_PATH` returned+ // `EBADF`.+ Some(Errno::BADF) => FSTAT_PATH_BADF.store(true, Relaxed),+ _ => return Err(err),+ },+ }+ }++ // If `fstat` with `O_PATH` isn't supported, use `statat` with+ // `AT_EMPTY_PATH`.+ Ok(Some(+ statat(file, "", AtFlags::EMPTY_PATH).map(ImplMetadataExt::from_rustix)?,+ ))+ }+ Err(err) => match Errno::from_io_error(&err) {+ // `ENOSYS` from `open_beneath` means `openat2` is unavailable+ // and we should use a fallback.+ Some(Errno::NOSYS) => Ok(None),+ _ => Err(err),+ },+ }+}++pub(crate) fn set_times_fallback(+ start: &fs::File,+ path: &Path,+ atime: Option<SystemTime>,+ mtime: Option<SystemTime>,+) -> io::Result<bool> {+ let opath = open(+ start,+ path,+ OpenOptions::new()+ .read(true)+ .custom_flags(OFlags::PATH.bits() as i32),+ )?;++ let times = Timestamps {+ last_access: to_timespec(atime)?,+ last_modification: to_timespec(mtime)?,+ };++ // Don't pass `AT_SYMLINK_NOFOLLOW`, because we do actually want to follow+ // the first symlink. We don't want to follow any subsequent symlinks, but+ // omitting `O_NOFOLLOW` above ensures that the destination of the link+ // isn't a symlink.+ utimensat(+ proc_self_fd()?.as_fd(),+ DecInt::from_fd(&opath).as_ref(),+ ×,+ AtFlags::empty(),+ )?;+ Ok(true)+}diff --git a/crates/wasi/src/filesystem/primitives/rustix/fs/metadata_ext.rs b/crates/wasi/src/filesystem/primitives/unix/metadata_ext.rssimilarity index 100%rename from crates/wasi/src/filesystem/primitives/rustix/fs/metadata_ext.rsrename to crates/wasi/src/filesystem/primitives/unix/metadata_ext.rsdiff --git a/crates/wasi/src/filesystem/primitives/unix/mod.rs b/crates/wasi/src/filesystem/primitives/unix/mod.rsnew file mode 100644index 0000000000..50d509d1ef--- /dev/null+++ b/crates/wasi/src/filesystem/primitives/unix/mod.rs@@ -0,0 +1,154 @@+use crate::filesystem::primitives::{MaybeOwnedFile, OpenOptions, open, open_parent};+use rustix::fs::{AtFlags, utimensat};+use rustix::io::Errno;+use std::fs;+use std::io;+use std::path::{Path, PathBuf};+use std::time::SystemTime;++mod create_dir_unchecked;+mod dir_entry_inner;+mod dir_utils;+mod file_type_ext;+mod hard_link_unchecked;+mod is_same_file;+mod metadata_ext;+mod oflags;+mod open_options_ext;+mod open_unchecked;+mod read_dir_inner;+mod read_link_unchecked;+mod remove_dir_unchecked;+mod remove_file_unchecked;+mod rename_unchecked;+mod stat_unchecked;+mod symlink_unchecked;+mod times;++pub(crate) mod errors;++// On FreeBSD, use optimized implementations based on+// `O_RESOLVE_BENEATH`/`AT_RESOLVE_BENEATH` and `O_PATH` when available.+#[cfg(target_os = "freebsd")]+mod freebsd;+#[cfg(target_os = "freebsd")]+pub(crate) use self::freebsd::*;++// On Linux, use optimized implementations based on+// `openat2` and `O_PATH` when available.+#[cfg(any(target_os = "android", target_os = "linux"))]+mod linux;+#[cfg(any(target_os = "android", target_os = "linux"))]+pub(crate) use self::linux::*;++pub(crate) use create_dir_unchecked::create_dir_unchecked;+pub(crate) use dir_entry_inner::DirEntryInner;+pub(crate) use dir_utils::*;+pub(crate) use file_type_ext::ImplFileTypeExt;+pub(crate) use hard_link_unchecked::hard_link_unchecked;+#[allow(unused_imports)]+pub(crate) use is_same_file::{is_different_file, is_different_file_metadata, is_same_file};+pub(crate) use metadata_ext::ImplMetadataExt;+pub(crate) use open_options_ext::ImplOpenOptionsExt;+pub(crate) use open_unchecked::open_unchecked;+pub(crate) use read_dir_inner::ReadDirInner;+pub(crate) use read_link_unchecked::read_link_unchecked;+pub(crate) use remove_dir_unchecked::remove_dir_unchecked;+pub(crate) use remove_file_unchecked::remove_file_unchecked;+pub(crate) use rename_unchecked::rename_unchecked;+pub(crate) use stat_unchecked::stat_unchecked;+pub(crate) use symlink_unchecked::symlink_unchecked;+pub(crate) use times::to_timespec;++// On Linux, there is a limit of 40 symlink expansions.+// Source: <https://man7.org/linux/man-pages/man7/path_resolution.7.html>+pub(crate) const MAX_SYMLINK_EXPANSIONS: u8 = 40;++pub(super) use oflags::*;++pub(crate) fn read_link(start: &fs::File, path: &Path) -> io::Result<PathBuf> {+ let start = MaybeOwnedFile::borrowed(start);+ let (dir, basename) = open_parent(start, path)?;+ read_link_unchecked(&dir, basename.as_ref(), PathBuf::new())+}++pub(crate) fn set_times_nofollow(+ start: &fs::File,+ path: &Path,+ atime: Option<SystemTime>,+ mtime: Option<SystemTime>,+) -> io::Result<()> {+ let times = rustix::fs::Timestamps {+ last_access: to_timespec(atime)?,+ last_modification: to_timespec(mtime)?,+ };++ #[cfg(target_os = "freebsd")]+ if set_times_nofollow_fast(start, path, ×)? {+ return Ok(());+ }++ let start = MaybeOwnedFile::borrowed(start);+ let (dir, basename) = open_parent(start, path)?;++ Ok(utimensat(+ &*dir,+ basename,+ ×,+ AtFlags::SYMLINK_NOFOLLOW,+ )?)+}++pub(crate) fn set_times(+ start: &fs::File,+ path: &Path,+ atime: Option<SystemTime>,+ mtime: Option<SystemTime>,+) -> io::Result<()> {+ #[cfg(target_os = "freebsd")]+ if set_times_fast(start, path, atime, mtime)? {+ return Ok(());+ }++ let mut times = fs::FileTimes::new();+ if let Some(atime) = atime {+ times = times.set_accessed(atime);+ }+ if let Some(mtime) = mtime {+ times = times.set_modified(mtime);+ }+ // Try `futimens` with a normal handle. Normal handles need some kind of+ // access, so first try write.+ match open(start, path, OpenOptions::new().write(true)) {+ Ok(file) => return file.set_times(times),+ Err(err) => match Errno::from_io_error(&err) {+ Some(Errno::ACCESS) | Some(Errno::ISDIR) => (),+ _ => return Err(err),+ },+ }++ // Next try read.+ match open(start, path, OpenOptions::new().read(true)) {+ Ok(file) => return file.set_times(times),+ Err(err) => match Errno::from_io_error(&err) {+ Some(Errno::ACCESS) => (),+ _ => return Err(err),+ },+ }++ #[cfg(any(target_os = "linux", target_os = "android"))]+ if set_times_fallback(start, path, atime, mtime)? {+ return Ok(());+ }++ // It's not possible to do anything else with generic POSIX. Plain+ // `utimensat` has two options:+ // - Follow symlinks, which would open up a race in which a concurrent+ // modification of the symlink could point outside the sandbox and we+ // wouldn't be able to detect it, or+ // - Don't follow symlinks, which would modify the timestamp of the symlink+ // instead of the file we're trying to get to.+ //+ // So neither does what we need.+ Err(Errno::NOTSUP.into())+}diff --git a/crates/wasi/src/filesystem/primitives/rustix/fs/oflags.rs b/crates/wasi/src/filesystem/primitives/unix/oflags.rssimilarity index 100%rename from crates/wasi/src/filesystem/primitives/rustix/fs/oflags.rsrename to crates/wasi/src/filesystem/primitives/unix/oflags.rsdiff --git a/crates/wasi/src/filesystem/primitives/rustix/fs/open_options_ext.rs b/crates/wasi/src/filesystem/primitives/unix/open_options_ext.rssimilarity index 100%rename from crates/wasi/src/filesystem/primitives/rustix/fs/open_options_ext.rsrename to crates/wasi/src/filesystem/primitives/unix/open_options_ext.rsdiff --git a/crates/wasi/src/filesystem/primitives/rustix/fs/open_unchecked.rs b/crates/wasi/src/filesystem/primitives/unix/open_unchecked.rssimilarity index 100%rename from crates/wasi/src/filesystem/primitives/rustix/fs/open_unchecked.rsrename to crates/wasi/src/filesystem/primitives/unix/open_unchecked.rsdiff --git a/crates/wasi/src/filesystem/primitives/rustix/fs/read_dir_inner.rs b/crates/wasi/src/filesystem/primitives/unix/read_dir_inner.rssimilarity index 100%rename from crates/wasi/src/filesystem/primitives/rustix/fs/read_dir_inner.rsrename to crates/wasi/src/filesystem/primitives/unix/read_dir_inner.rsdiff --git a/crates/wasi/src/filesystem/primitives/rustix/fs/read_link_unchecked.rs b/crates/wasi/src/filesystem/primitives/unix/read_link_unchecked.rssimilarity index 100%rename from crates/wasi/src/filesystem/primitives/rustix/fs/read_link_unchecked.rsrename to crates/wasi/src/filesystem/primitives/unix/read_link_unchecked.rsdiff --git a/crates/wasi/src/filesystem/primitives/rustix/fs/remove_dir_unchecked.rs b/crates/wasi/src/filesystem/primitives/unix/remove_dir_unchecked.rssimilarity index 100%rename from crates/wasi/src/filesystem/primitives/rustix/fs/remove_dir_unchecked.rsrename to crates/wasi/src/filesystem/primitives/unix/remove_dir_unchecked.rsdiff --git a/crates/wasi/src/filesystem/primitives/rustix/fs/remove_file_unchecked.rs b/crates/wasi/src/filesystem/primitives/unix/remove_file_unchecked.rssimilarity index 100%rename from crates/wasi/src/filesystem/primitives/rustix/fs/remove_file_unchecked.rsrename to crates/wasi/src/filesystem/primitives/unix/remove_file_unchecked.rsdiff --git a/crates/wasi/src/filesystem/primitives/rustix/fs/rename_unchecked.rs b/crates/wasi/src/filesystem/primitives/unix/rename_unchecked.rssimilarity index 100%rename from crates/wasi/src/filesystem/primitives/rustix/fs/rename_unchecked.rsrename to crates/wasi/src/filesystem/primitives/unix/rename_unchecked.rsdiff --git a/crates/wasi/src/filesystem/primitives/rustix/fs/stat_unchecked.rs b/crates/wasi/src/filesystem/primitives/unix/stat_unchecked.rssimilarity index 100%rename from crates/wasi/src/filesystem/primitives/rustix/fs/stat_unchecked.rsrename to crates/wasi/src/filesystem/primitives/unix/stat_unchecked.rsdiff --git a/crates/wasi/src/filesystem/primitives/rustix/fs/symlink_unchecked.rs b/crates/wasi/src/filesystem/primitives/unix/symlink_unchecked.rssimilarity index 100%rename from crates/wasi/src/filesystem/primitives/rustix/fs/symlink_unchecked.rsrename to crates/wasi/src/filesystem/primitives/unix/symlink_unchecked.rsdiff --git a/crates/wasi/src/filesystem/primitives/unix/times.rs b/crates/wasi/src/filesystem/primitives/unix/times.rsnew file mode 100644index 0000000000..67425497d1--- /dev/null+++ b/crates/wasi/src/filesystem/primitives/unix/times.rs@@ -0,0 +1,27 @@+use rustix::fs::{UTIME_NOW, UTIME_OMIT};+use rustix::time::Timespec;+use std::io;+use std::time::SystemTime;++#[allow(clippy::useless_conversion)]+pub(crate) fn to_timespec(ft: Option<SystemTime>) -> io::Result<Timespec> {+ Ok(match ft {+ None => Timespec {+ tv_sec: 0,+ tv_nsec: UTIME_OMIT.into(),+ },+ Some(ft) => {+ let duration = ft.duration_since(SystemTime::UNIX_EPOCH).unwrap();+ let nanoseconds = duration.subsec_nanos();+ assert_ne!(i64::from(nanoseconds), i64::from(UTIME_OMIT));+ assert_ne!(i64::from(nanoseconds), i64::from(UTIME_NOW));+ Timespec {+ tv_sec: duration+ .as_secs()+ .try_into()+ .map_err(|err| io::Error::new(io::ErrorKind::Other, err))?,+ tv_nsec: nanoseconds.try_into().unwrap(),+ }+ }+ })+}diff --git a/crates/wasi/src/filesystem/primitives/via_parent/create_dir.rs b/crates/wasi/src/filesystem/primitives/via_parent/create_dir.rsdeleted file mode 100644index 900420de1e..0000000000--- a/crates/wasi/src/filesystem/primitives/via_parent/create_dir.rs+++ /dev/null@@ -1,21 +0,0 @@-use super::open_parent;-use crate::filesystem::primitives::{- DirOptions, MaybeOwnedFile, create_dir_unchecked, strip_dir_suffix,-};-use std::path::Path;-use std::{fs, io};--/// Implement `create_dir` by `open`ing up the parent component of the path and-/// then calling `create_dir_unchecked` on the last component.-pub(crate) fn create_dir(start: &fs::File, path: &Path, options: &DirOptions) -> io::Result<()> {- let start = MaybeOwnedFile::borrowed(start);-- // As a special case, `create_dir` ignores a trailing slash rather than- // treating it as equivalent to a trailing slash-dot, so strip any trailing- // slashes.- let path = strip_dir_suffix(path);-- let (dir, basename) = open_parent(start, &path)?;-- create_dir_unchecked(&dir, basename.as_ref(), options)-}diff --git a/crates/wasi/src/filesystem/primitives/via_parent/hard_link.rs b/crates/wasi/src/filesystem/primitives/via_parent/hard_link.rsdeleted file mode 100644index b725ca7f6f..0000000000--- a/crates/wasi/src/filesystem/primitives/via_parent/hard_link.rs+++ /dev/null@@ -1,26 +0,0 @@-use super::open_parent;-use crate::filesystem::primitives::{MaybeOwnedFile, hard_link_unchecked};-use std::path::Path;-use std::{fs, io};--/// Implement `hard_link` by `open`ing up the parent component of the path and-/// then calling `hard_link_unchecked` on the last component.-pub(crate) fn hard_link(- old_start: &fs::File,- old_path: &Path,- new_start: &fs::File,- new_path: &Path,-) -> io::Result<()> {- let old_start = MaybeOwnedFile::borrowed(old_start);- let new_start = MaybeOwnedFile::borrowed(new_start);-- let (old_dir, old_basename) = open_parent(old_start, old_path)?;- let (new_dir, new_basename) = open_parent(new_start, new_path)?;-- hard_link_unchecked(- &old_dir,- old_basename.as_ref(),- &new_dir,- new_basename.as_ref(),- )-}diff --git a/crates/wasi/src/filesystem/primitives/via_parent/mod.rs b/crates/wasi/src/filesystem/primitives/via_parent/mod.rsdeleted file mode 100644index 5c6241e7f2..0000000000--- a/crates/wasi/src/filesystem/primitives/via_parent/mod.rs+++ /dev/null@@ -1,31 +0,0 @@-//! In many operations, the last component of a path is special. For example,-//! in `create_dir`, the last component names the path to be created, while the-//! rest of the components just name the place to create it in.--mod create_dir;-mod hard_link;-mod open_parent;-#[cfg(not(windows))] // doesn't work on windows; use a windows-specific impl-mod read_link;-mod remove_dir;-mod remove_file;-mod rename;-#[cfg(not(windows))]-mod set_times_nofollow;-mod symlink;--use open_parent::open_parent;--pub(crate) use create_dir::create_dir;-pub(crate) use hard_link::hard_link;-#[cfg(not(windows))] // doesn't work on windows; use a windows-specific impl-pub(crate) use read_link::read_link;-pub(crate) use remove_dir::remove_dir;-pub(crate) use remove_file::remove_file;-pub(crate) use rename::rename;-#[cfg(not(windows))]-pub(crate) use set_times_nofollow::set_times_nofollow;-#[cfg(not(windows))]-pub(crate) use symlink::symlink;-#[cfg(windows)]-pub(crate) use symlink::{symlink_dir, symlink_file};diff --git a/crates/wasi/src/filesystem/primitives/via_parent/read_link.rs b/crates/wasi/src/filesystem/primitives/via_parent/read_link.rsdeleted file mode 100644index edd03a4baf..0000000000--- a/crates/wasi/src/filesystem/primitives/via_parent/read_link.rs+++ /dev/null@@ -1,21 +0,0 @@-use super::open_parent;-use crate::filesystem::primitives::{MaybeOwnedFile, read_link_unchecked};-use std::path::{Path, PathBuf};-use std::{fs, io};--/// Implement `read_link` by `open`ing up the parent component of the path and-/// then calling `read_link_unchecked` on the last component.-///-/// This technique doesn't work in all cases on Windows. In particular, a-/// directory symlink such as `C:\Documents and Settings` may not grant any-/// access other than what is needed to resolve the symlink, so `open_parent`'s-/// technique of returning a relative path of `.` from that point doesn't work,-/// because opening `.` within such a directory is denied. Consequently, we use-/// a different implementation on Windows.-pub(crate) fn read_link(start: &fs::File, path: &Path) -> io::Result<PathBuf> {- let start = MaybeOwnedFile::borrowed(start);-- let (dir, basename) = open_parent(start, path)?;-- read_link_unchecked(&dir, basename.as_ref(), PathBuf::new())-}diff --git a/crates/wasi/src/filesystem/primitives/via_parent/remove_dir.rs b/crates/wasi/src/filesystem/primitives/via_parent/remove_dir.rsdeleted file mode 100644index 9a46149df7..0000000000--- a/crates/wasi/src/filesystem/primitives/via_parent/remove_dir.rs+++ /dev/null@@ -1,14 +0,0 @@-use super::open_parent;-use crate::filesystem::primitives::{MaybeOwnedFile, remove_dir_unchecked};-use std::path::Path;-use std::{fs, io};--/// Implement `remove_dir` by `open`ing up the parent component of the path and-/// then calling `remove_dir_unchecked` on the last component.-pub(crate) fn remove_dir(start: &fs::File, path: &Path) -> io::Result<()> {- let start = MaybeOwnedFile::borrowed(start);-- let (dir, basename) = open_parent(start, path)?;-- remove_dir_unchecked(&dir, basename.as_ref())-}diff --git a/crates/wasi/src/filesystem/primitives/via_parent/remove_file.rs b/crates/wasi/src/filesystem/primitives/via_parent/remove_file.rsdeleted file mode 100644index 7b06f727e4..0000000000--- a/crates/wasi/src/filesystem/primitives/via_parent/remove_file.rs+++ /dev/null@@ -1,14 +0,0 @@-use super::open_parent;-use crate::filesystem::primitives::{MaybeOwnedFile, remove_file_unchecked};-use std::path::Path;-use std::{fs, io};--/// Implement `remove_file` by `open`ing up the parent component of the path-/// and then calling `remove_file_unchecked` on the last component.-pub(crate) fn remove_file(start: &fs::File, path: &Path) -> io::Result<()> {- let start = MaybeOwnedFile::borrowed(start);-- let (dir, basename) = open_parent(start, path)?;-- remove_file_unchecked(&dir, basename.as_ref())-}diff --git a/crates/wasi/src/filesystem/primitives/via_parent/rename.rs b/crates/wasi/src/filesystem/primitives/via_parent/rename.rsdeleted file mode 100644index 0e62eb3625..0000000000--- a/crates/wasi/src/filesystem/primitives/via_parent/rename.rs+++ /dev/null@@ -1,50 +0,0 @@-use super::open_parent;-use crate::filesystem::primitives::{MaybeOwnedFile, rename_unchecked, strip_dir_suffix};-#[cfg(unix)]-use crate::filesystem::primitives::{append_dir_suffix, path_has_trailing_slash};-use std::path::Path;-use std::{fs, io};--/// Implement `rename` by `open`ing up the parent component of the path and-/// then calling `rename_unchecked` on the last component.-pub(crate) fn rename(- old_start: &fs::File,- old_path: &Path,- new_start: &fs::File,- new_path: &Path,-) -> io::Result<()> {- let old_start = MaybeOwnedFile::borrowed(old_start);- let new_start = MaybeOwnedFile::borrowed(new_start);-- // As a special case, `rename` ignores a trailing slash rather than treating- // it as equivalent to a trailing slash-dot, so strip any trailing slashes- // for the purposes of `open_parent`.- //- // And on Unix, remember whether the source started with a slash so that we- // can still fail if it is and the source is a regular file.- #[cfg(unix)]- let old_starts_with_slash = path_has_trailing_slash(old_path);- let old_path = strip_dir_suffix(old_path);- let new_path = strip_dir_suffix(new_path);-- let (old_dir, old_basename) = open_parent(old_start, &old_path)?;- let (new_dir, new_basename) = open_parent(new_start, &new_path)?;-- // On Unix, re-append a slash if needed.- #[cfg(unix)]- let concat;- #[cfg(unix)]- let old_basename = if old_starts_with_slash {- concat = append_dir_suffix(old_basename.to_owned().into());- concat.as_os_str()- } else {- old_basename- };-- rename_unchecked(- &old_dir,- old_basename.as_ref(),- &new_dir,- new_basename.as_ref(),- )-}diff --git a/crates/wasi/src/filesystem/primitives/via_parent/set_times_nofollow.rs b/crates/wasi/src/filesystem/primitives/via_parent/set_times_nofollow.rsdeleted file mode 100644index 8a4146e0af..0000000000--- a/crates/wasi/src/filesystem/primitives/via_parent/set_times_nofollow.rs+++ /dev/null@@ -1,19 +0,0 @@-use super::open_parent;-use crate::filesystem::primitives::{MaybeOwnedFile, set_times_nofollow_unchecked};-use std::path::Path;-use std::time::SystemTime;-use std::{fs, io};--#[inline]-pub(crate) fn set_times_nofollow(- start: &fs::File,- path: &Path,- atime: Option<SystemTime>,- mtime: Option<SystemTime>,-) -> io::Result<()> {- let start = MaybeOwnedFile::borrowed(start);-- let (dir, basename) = open_parent(start, path)?;-- set_times_nofollow_unchecked(&dir, basename.as_ref(), atime, mtime)-}diff --git a/crates/wasi/src/filesystem/primitives/via_parent/symlink.rs b/crates/wasi/src/filesystem/primitives/via_parent/symlink.rsdeleted file mode 100644index 6ea53afc8f..0000000000--- a/crates/wasi/src/filesystem/primitives/via_parent/symlink.rs+++ /dev/null@@ -1,48 +0,0 @@-use super::open_parent;-use crate::filesystem::primitives::MaybeOwnedFile;-use std::path::Path;-use std::{fs, io};--/// Implement `symlink` by `open`ing up the parent component of the path and-/// then calling `symlink_unchecked` on the last component.-#[cfg(not(windows))]-pub(crate) fn symlink(old_path: &Path, new_start: &fs::File, new_path: &Path) -> io::Result<()> {- use crate::filesystem::primitives::symlink_unchecked;- let new_start = MaybeOwnedFile::borrowed(new_start);-- let (new_dir, new_basename) = open_parent(new_start, new_path)?;-- symlink_unchecked(old_path, &new_dir, new_basename.as_ref())-}--/// Implement `symlink_file` by `open`ing up the parent component of the path-/// and then calling `symlink_file` on the last component.-#[cfg(windows)]-pub(crate) fn symlink_file(- old_path: &Path,- new_start: &fs::File,- new_path: &Path,-) -> io::Result<()> {- use crate::filesystem::primitives::symlink_file_unchecked;- let new_start = MaybeOwnedFile::borrowed(new_start);-- let (new_dir, new_basename) = open_parent(new_start, new_path)?;-- symlink_file_unchecked(old_path, &new_dir, new_basename.as_ref())-}--/// Implement `symlink_dir` by `open`ing up the parent component of the path-/// and then calling `symlink_dir` on the last component.-#[cfg(windows)]-pub(crate) fn symlink_dir(- old_path: &Path,- new_start: &fs::File,- new_path: &Path,-) -> io::Result<()> {- use crate::filesystem::primitives::symlink_dir_unchecked;- let new_start = MaybeOwnedFile::borrowed(new_start);-- let (new_dir, new_basename) = open_parent(new_start, new_path)?;-- symlink_dir_unchecked(old_path, &new_dir, new_basename.as_ref())-}diff --git a/crates/wasi/src/filesystem/primitives/windows/fs/create_dir_unchecked.rs b/crates/wasi/src/filesystem/primitives/windows/create_dir_unchecked.rssimilarity index 52%rename from crates/wasi/src/filesystem/primitives/windows/fs/create_dir_unchecked.rsrename to crates/wasi/src/filesystem/primitives/windows/create_dir_unchecked.rsindex 442b389169..eb7d4bfbe1 100644--- a/crates/wasi/src/filesystem/primitives/windows/fs/create_dir_unchecked.rs+++ b/crates/wasi/src/filesystem/primitives/windows/create_dir_unchecked.rs@@ -1,18 +1,11 @@use super::get_path::concatenate;-use crate::filesystem::primitives::DirOptions;use std::path::Path;use std::{fs, io};/// *Unsandboxed* function similar to `create_dir`, but which does not perform/// sandboxing.///-/// Windows doesn't have any extra flags in `DirOptions`, so the `options`-/// parameter is ignored.-pub(crate) fn create_dir_unchecked(- start: &fs::File,- path: &Path,- _options: &DirOptions,-) -> io::Result<()> {+pub(crate) fn create_dir_unchecked(start: &fs::File, path: &Path) -> io::Result<()> {let out_path = concatenate(start, path)?;fs::create_dir(out_path)}diff --git a/crates/wasi/src/filesystem/primitives/windows/fs/create_file_at_w.rs b/crates/wasi/src/filesystem/primitives/windows/create_file_at_w.rssimilarity index 100%rename from crates/wasi/src/filesystem/primitives/windows/fs/create_file_at_w.rsrename to crates/wasi/src/filesystem/primitives/windows/create_file_at_w.rsdiff --git a/crates/wasi/src/filesystem/primitives/windows/fs/dir_entry_inner.rs b/crates/wasi/src/filesystem/primitives/windows/dir_entry_inner.rssimilarity index 100%rename from crates/wasi/src/filesystem/primitives/windows/fs/dir_entry_inner.rsrename to crates/wasi/src/filesystem/primitives/windows/dir_entry_inner.rsdiff --git a/crates/wasi/src/filesystem/primitives/windows/fs/dir_utils.rs b/crates/wasi/src/filesystem/primitives/windows/dir_utils.rssimilarity index 99%rename from crates/wasi/src/filesystem/primitives/windows/fs/dir_utils.rsrename to crates/wasi/src/filesystem/primitives/windows/dir_utils.rsindex e4b8c28a44..e0897fa38a 100644--- a/crates/wasi/src/filesystem/primitives/windows/fs/dir_utils.rs+++ b/crates/wasi/src/filesystem/primitives/windows/dir_utils.rs@@ -83,7 +83,7 @@ pub(crate) fn readdir_options() -> OpenOptions {////// This function is not sandboxed and may trivially access any path that the/// host process has access to.-pub(crate) fn open_ambient_dir_impl(path: &Path) -> io::Result<fs::File> {+pub(crate) fn open_ambient_dir(path: &Path) -> io::Result<fs::File> {use std::os::windows::fs::OpenOptionsExt;// Set `FILE_FLAG_BACKUP_SEMANTICS` so that we can open directories. Unsetdiff --git a/crates/wasi/src/filesystem/primitives/windows/fs/errors.rs b/crates/wasi/src/filesystem/primitives/windows/errors.rssimilarity index 100%rename from crates/wasi/src/filesystem/primitives/windows/fs/errors.rsrename to crates/wasi/src/filesystem/primitives/windows/errors.rsdiff --git a/crates/wasi/src/filesystem/primitives/windows/fs/file_type_ext.rs b/crates/wasi/src/filesystem/primitives/windows/file_type_ext.rssimilarity index 100%rename from crates/wasi/src/filesystem/primitives/windows/fs/file_type_ext.rsrename to crates/wasi/src/filesystem/primitives/windows/file_type_ext.rsdiff --git a/crates/wasi/src/filesystem/primitives/windows/fs/dir_options_ext.rs b/crates/wasi/src/filesystem/primitives/windows/fs/dir_options_ext.rsdeleted file mode 100644index 728f59629b..0000000000--- a/crates/wasi/src/filesystem/primitives/windows/fs/dir_options_ext.rs+++ /dev/null@@ -1,8 +0,0 @@-#[derive(Debug, Clone)]-pub(crate) struct DirOptionsExt {}--impl DirOptionsExt {- pub(crate) const fn new() -> Self {- Self {}- }-}diff --git a/crates/wasi/src/filesystem/primitives/windows/fs/mod.rs b/crates/wasi/src/filesystem/primitives/windows/fs/mod.rsdeleted file mode 100644index 75ff00fbe8..0000000000--- a/crates/wasi/src/filesystem/primitives/windows/fs/mod.rs+++ /dev/null@@ -1,66 +0,0 @@-mod create_dir_unchecked;-mod create_file_at_w;-mod dir_entry_inner;-mod dir_options_ext;-mod dir_utils;-mod file_type_ext;-mod get_path;-mod hard_link_unchecked;-mod metadata_ext;-mod oflags;-mod open_impl;-mod open_options_ext;-mod open_unchecked;-mod read_dir_inner;-mod read_link_impl;-mod read_link_unchecked;-mod remove_dir_unchecked;-mod remove_file_unchecked;-mod rename_unchecked;-mod set_times_impl;-mod stat_unchecked;-mod symlink_unchecked;--pub(crate) mod errors;--#[rustfmt::skip]-pub(crate) use crate::filesystem::primitives::{- via_parent::hard_link as hard_link_impl,- via_parent::create_dir as create_dir_impl,- via_parent::rename as rename_impl,- via_parent::remove_dir as remove_dir_impl,- manually::stat as stat_impl,- via_parent::symlink_dir as symlink_dir_impl,- via_parent::symlink_file as symlink_file_impl,- via_parent::remove_file as remove_file_impl,-};--pub(crate) use create_dir_unchecked::*;-pub(crate) use dir_entry_inner::*;-pub(crate) use dir_options_ext::*;-pub(crate) use dir_utils::*;-pub(crate) use file_type_ext::*;-pub(crate) use hard_link_unchecked::*;-pub(crate) use metadata_ext::*;-pub(crate) use open_impl::open_impl;-pub(crate) use open_options_ext::*;-pub(crate) use open_unchecked::*;-pub(crate) use read_dir_inner::*;-pub(crate) use read_link_impl::*;-pub(crate) use read_link_unchecked::*;-pub(crate) use remove_dir_unchecked::*;-pub(crate) use remove_file_unchecked::*;-pub(crate) use rename_unchecked::*;-pub(crate) use set_times_impl::*;-pub(crate) use stat_unchecked::*;-pub(crate) use symlink_unchecked::*;--// On Windows, there is a limit of 63 reparse points on any given path.-// <https://docs.microsoft.com/en-us/windows/win32/fileio/reparse-points>-pub(crate) const MAX_SYMLINK_EXPANSIONS: u8 = 63;--pub(crate) fn file_path(file: &std::fs::File) -> Option<std::path::PathBuf> {- get_path::get_path(file).ok()-}--pub(super) use oflags::*;diff --git a/crates/wasi/src/filesystem/primitives/windows/fs/get_path.rs b/crates/wasi/src/filesystem/primitives/windows/get_path.rssimilarity index 100%rename from crates/wasi/src/filesystem/primitives/windows/fs/get_path.rsrename to crates/wasi/src/filesystem/primitives/windows/get_path.rsdiff --git a/crates/wasi/src/filesystem/primitives/windows/fs/hard_link_unchecked.rs b/crates/wasi/src/filesystem/primitives/windows/hard_link_unchecked.rssimilarity index 100%rename from crates/wasi/src/filesystem/primitives/windows/fs/hard_link_unchecked.rsrename to crates/wasi/src/filesystem/primitives/windows/hard_link_unchecked.rsdiff --git a/crates/wasi/src/filesystem/primitives/windows/fs/metadata_ext.rs b/crates/wasi/src/filesystem/primitives/windows/metadata_ext.rssimilarity index 100%rename from crates/wasi/src/filesystem/primitives/windows/fs/metadata_ext.rsrename to crates/wasi/src/filesystem/primitives/windows/metadata_ext.rsdiff --git a/crates/wasi/src/filesystem/primitives/windows/mod.rs b/crates/wasi/src/filesystem/primitives/windows/mod.rsindex 5553d02ec5..cdab84db1b 100644--- a/crates/wasi/src/filesystem/primitives/windows/mod.rs+++ b/crates/wasi/src/filesystem/primitives/windows/mod.rs@@ -1,4 +1,52 @@-//! The `winx` module contains code specific to Windows, supported by the-//! `winx` crate.+mod create_dir_unchecked;+mod create_file_at_w;+mod dir_entry_inner;+mod dir_utils;+mod file_type_ext;+mod get_path;+mod hard_link_unchecked;+mod metadata_ext;+mod oflags;+mod open_fast;+mod open_options_ext;+mod open_unchecked;+mod read_dir_inner;+mod read_link;+mod read_link_unchecked;+mod remove_dir_unchecked;+mod remove_file_unchecked;+mod rename_unchecked;+mod set_times;+mod stat_unchecked;+mod symlink_unchecked;-pub(crate) mod fs;+pub(crate) mod errors;++pub(crate) use create_dir_unchecked::*;+pub(crate) use dir_entry_inner::*;+pub(crate) use dir_utils::*;+pub(crate) use file_type_ext::*;+pub(crate) use hard_link_unchecked::*;+pub(crate) use metadata_ext::*;+pub(crate) use open_fast::*;+pub(crate) use open_options_ext::*;+pub(crate) use open_unchecked::*;+pub(crate) use read_dir_inner::*;+pub(crate) use read_link::*;+pub(crate) use read_link_unchecked::*;+pub(crate) use remove_dir_unchecked::*;+pub(crate) use remove_file_unchecked::*;+pub(crate) use rename_unchecked::*;+pub(crate) use set_times::*;+pub(crate) use stat_unchecked::*;+pub(crate) use symlink_unchecked::*;++// On Windows, there is a limit of 63 reparse points on any given path.+// <https://docs.microsoft.com/en-us/windows/win32/fileio/reparse-points>+pub(crate) const MAX_SYMLINK_EXPANSIONS: u8 = 63;++pub(crate) fn file_path(file: &std::fs::File) -> Option<std::path::PathBuf> {+ get_path::get_path(file).ok()+}++pub(super) use oflags::*;diff --git a/crates/wasi/src/filesystem/primitives/windows/fs/oflags.rs b/crates/wasi/src/filesystem/primitives/windows/oflags.rssimilarity index 100%rename from crates/wasi/src/filesystem/primitives/windows/fs/oflags.rsrename to crates/wasi/src/filesystem/primitives/windows/oflags.rsdiff --git a/crates/wasi/src/filesystem/primitives/windows/fs/open_impl.rs b/crates/wasi/src/filesystem/primitives/windows/open_fast.rssimilarity index 92%rename from crates/wasi/src/filesystem/primitives/windows/fs/open_impl.rsrename to crates/wasi/src/filesystem/primitives/windows/open_fast.rsindex 95c94f710a..b1a52e58c0 100644--- a/crates/wasi/src/filesystem/primitives/windows/fs/open_impl.rs+++ b/crates/wasi/src/filesystem/primitives/windows/open_fast.rs@@ -1,14 +1,14 @@-use crate::filesystem::primitives::{OpenOptions, manually};+use crate::filesystem::primitives::OpenOptions;use std::ffi::OsStr;use std::path::Path;use std::{fs, io};use windows_sys::Win32::Foundation::ERROR_FILE_NOT_FOUND;-pub(crate) fn open_impl(- start: &fs::File,+pub(crate) fn open_fast(+ _start: &fs::File,path: &Path,- options: &OpenOptions,-) -> io::Result<fs::File> {+ _options: &OpenOptions,+) -> io::Result<Option<fs::File>> {// Windows reserves several special device paths. Disallow opening any// of them.// See: https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file#naming-conventions@@ -26,7 +26,7 @@ pub(crate) fn open_impl(}}- manually::open(start, path, options)+ Ok(None)}// TODO: Replace this with `Path::file_prefix` once that's stable. For now,diff --git a/crates/wasi/src/filesystem/primitives/windows/fs/open_options_ext.rs b/crates/wasi/src/filesystem/primitives/windows/open_options_ext.rssimilarity index 100%rename from crates/wasi/src/filesystem/primitives/windows/fs/open_options_ext.rsrename to crates/wasi/src/filesystem/primitives/windows/open_options_ext.rsdiff --git a/crates/wasi/src/filesystem/primitives/windows/fs/open_unchecked.rs b/crates/wasi/src/filesystem/primitives/windows/open_unchecked.rssimilarity index 100%rename from crates/wasi/src/filesystem/primitives/windows/fs/open_unchecked.rsrename to crates/wasi/src/filesystem/primitives/windows/open_unchecked.rsdiff --git a/crates/wasi/src/filesystem/primitives/windows/fs/read_dir_inner.rs b/crates/wasi/src/filesystem/primitives/windows/read_dir_inner.rssimilarity index 100%rename from crates/wasi/src/filesystem/primitives/windows/fs/read_dir_inner.rsrename to crates/wasi/src/filesystem/primitives/windows/read_dir_inner.rsdiff --git a/crates/wasi/src/filesystem/primitives/windows/fs/read_link_impl.rs b/crates/wasi/src/filesystem/primitives/windows/read_link.rssimilarity index 90%rename from crates/wasi/src/filesystem/primitives/windows/fs/read_link_impl.rsrename to crates/wasi/src/filesystem/primitives/windows/read_link.rsindex b93086268a..c42981039f 100644--- a/crates/wasi/src/filesystem/primitives/windows/fs/read_link_impl.rs+++ b/crates/wasi/src/filesystem/primitives/windows/read_link.rs@@ -7,7 +7,7 @@ use windows_sys::Win32::Storage::FileSystem::{/// *Unsandboxed* function similar to `read_link`, but which does not perform/// sandboxing.-pub(crate) fn read_link_impl(start: &fs::File, path: &Path) -> io::Result<PathBuf> {+pub(crate) fn read_link(start: &fs::File, path: &Path) -> io::Result<PathBuf> {// Open the link with no access mode, instead of generic read.// By default FILE_LIST_DIRECTORY is denied for the junction "C:\Documents and// Settings", so this is needed for a common case.diff --git a/crates/wasi/src/filesystem/primitives/windows/fs/read_link_unchecked.rs b/crates/wasi/src/filesystem/primitives/windows/read_link_unchecked.rssimilarity index 100%rename from crates/wasi/src/filesystem/primitives/windows/fs/read_link_unchecked.rsrename to crates/wasi/src/filesystem/primitives/windows/read_link_unchecked.rsdiff --git a/crates/wasi/src/filesystem/primitives/windows/fs/remove_dir_unchecked.rs b/crates/wasi/src/filesystem/primitives/windows/remove_dir_unchecked.rssimilarity index 100%rename from crates/wasi/src/filesystem/primitives/windows/fs/remove_dir_unchecked.rsrename to crates/wasi/src/filesystem/primitives/windows/remove_dir_unchecked.rsdiff --git a/crates/wasi/src/filesystem/primitives/windows/fs/remove_file_unchecked.rs b/crates/wasi/src/filesystem/primitives/windows/remove_file_unchecked.rssimilarity index 100%rename from crates/wasi/src/filesystem/primitives/windows/fs/remove_file_unchecked.rsrename to crates/wasi/src/filesystem/primitives/windows/remove_file_unchecked.rsdiff --git a/crates/wasi/src/filesystem/primitives/windows/fs/rename_unchecked.rs b/crates/wasi/src/filesystem/primitives/windows/rename_unchecked.rssimilarity index 100%rename from crates/wasi/src/filesystem/primitives/windows/fs/rename_unchecked.rsrename to crates/wasi/src/filesystem/primitives/windows/rename_unchecked.rsdiff --git a/crates/wasi/src/filesystem/primitives/windows/fs/set_times_impl.rs b/crates/wasi/src/filesystem/primitives/windows/set_times.rssimilarity index 94%rename from crates/wasi/src/filesystem/primitives/windows/fs/set_times_impl.rsrename to crates/wasi/src/filesystem/primitives/windows/set_times.rsindex c0acd4c9cc..67e9cbf884 100644--- a/crates/wasi/src/filesystem/primitives/windows/fs/set_times_impl.rs+++ b/crates/wasi/src/filesystem/primitives/windows/set_times.rs@@ -7,7 +7,7 @@ use windows_sys::Win32::Storage::FileSystem::{};#[inline]-pub(crate) fn set_times_impl(+pub(crate) fn set_times(start: &fs::File,path: &Path,atime: Option<SystemTime>,@@ -17,7 +17,7 @@ pub(crate) fn set_times_impl(}#[inline]-pub(crate) fn set_times_nofollow_impl(+pub(crate) fn set_times_nofollow(start: &fs::File,path: &Path,atime: Option<SystemTime>,diff --git a/crates/wasi/src/filesystem/primitives/windows/fs/stat_unchecked.rs b/crates/wasi/src/filesystem/primitives/windows/stat_unchecked.rssimilarity index 100%rename from crates/wasi/src/filesystem/primitives/windows/fs/stat_unchecked.rsrename to crates/wasi/src/filesystem/primitives/windows/stat_unchecked.rsdiff --git a/crates/wasi/src/filesystem/primitives/windows/fs/symlink_unchecked.rs b/crates/wasi/src/filesystem/primitives/windows/symlink_unchecked.rssimilarity index 100%rename from crates/wasi/src/filesystem/primitives/windows/fs/symlink_unchecked.rsrename to crates/wasi/src/filesystem/primitives/windows/symlink_unchecked.rs