A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
wasi: fix set-times on directory descriptors (#14420)
Xia Chao3c94b771f4dd
diff --git a/crates/test-programs/src/bin/p1_fd_filestat_set.rs b/crates/test-programs/src/bin/p1_fd_filestat_set.rsindex ccfe072a01..d6ce6fa532 100644--- a/crates/test-programs/src/bin/p1_fd_filestat_set.rs+++ b/crates/test-programs/src/bin/p1_fd_filestat_set.rs@@ -114,6 +114,28 @@ unsafe fn test_fd_filestat_set_times(dir_fd: wasip1::Fd, rights: wasip1::Rights)wasip1::fd_close(file_fd).expect("failed to close fd");wasip1::path_unlink_file(dir_fd, "file").expect("failed to remove file");}+unsafe fn test_fd_filestat_set_times_on_directory(dir_fd: wasip1::Fd) {+ // Setting times on the directory descriptor itself has to work too,+ // because preopened directories are held open with `O_PATH`.+ let stat = wasip1::fd_filestat_get(dir_fd).expect("failed filestat on dir");+ let new_mtim = Duration::from_nanos(stat.mtim) + Duration::from_secs(100);++ wasip1::fd_filestat_set_times(+ dir_fd,+ new_mtim.as_nanos() as u64,+ new_mtim.as_nanos() as u64,+ wasip1::FSTFLAGS_MTIM,+ )+ .expect("fd_filestat_set_times on a directory");++ let stat = wasip1::fd_filestat_get(dir_fd).expect("failed filestat on dir");+ assert_fs_time_eq!(+ Duration::from_nanos(stat.mtim),+ new_mtim,+ "dir mtim should change"+ );+}+fn main() {let mut args = env::args();let prog = args.next().unwrap();@@ -148,4 +170,10 @@ fn main() {unsafe { test_fd_filestat_set_times(dir_fd, wasip1::RIGHTS_FD_READ) }}unsafe { test_fd_filestat_set_times(dir_fd, wasip1::RIGHTS_FD_READ | wasip1::RIGHTS_FD_WRITE) }++ // Same guard as above: Windows rejects set-times on a directory handle that+ // was opened read-only, the way it does for read-only files.+ if test_programs::preview1::config().support_dangling_filesystem() {+ unsafe { test_fd_filestat_set_times_on_directory(dir_fd) }+ }}diff --git a/crates/wasi-preview1-component-adapter/src/descriptors.rs b/crates/wasi-preview1-component-adapter/src/descriptors.rsindex 9ee1cf7d34..11e9db7392 100644--- a/crates/wasi-preview1-component-adapter/src/descriptors.rs+++ b/crates/wasi-preview1-component-adapter/src/descriptors.rs@@ -421,6 +421,22 @@ impl Descriptors {self.get_file_with_error(fd, wasip1::ERRNO_INVAL)}+ /// Like [`Self::get_file`], but also accepts a directory.+ ///+ /// `fd_filestat_set_times` applies to both, so unlike the other callers of+ /// `get_file` it must not reject a directory.+ #[cfg(not(feature = "proxy"))]+ pub fn get_file_or_dir(&self, fd: Fd) -> Result<&File, Errno> {+ match self.get(fd)? {+ Descriptor::Streams(Streams {+ type_: StreamType::File(file),+ ..+ }) => Ok(file),+ Descriptor::Closed(_) => Err(wasip1::ERRNO_BADF),+ _ => Err(wasip1::ERRNO_INVAL),+ }+ }+#[cfg(not(feature = "proxy"))]pub fn get_dir(&self, fd: Fd) -> Result<&File, Errno> {match self.get(fd)? {diff --git a/crates/wasi-preview1-component-adapter/src/lib.rs b/crates/wasi-preview1-component-adapter/src/lib.rsindex 7ce52c6c26..e070206d4f 100644--- a/crates/wasi-preview1-component-adapter/src/lib.rs+++ b/crates/wasi-preview1-component-adapter/src/lib.rs@@ -1102,7 +1102,7 @@ pub unsafe extern "C" fn fd_filestat_set_times(fst_flags & FSTFLAGS_MTIM_NOW == FSTFLAGS_MTIM_NOW,)?;let ds = state.descriptors();- let file = ds.get_file(fd)?;+ let file = ds.get_file_or_dir(fd)?;file.fd.set_times(atim, mtim)?;Ok(())})diff --git a/crates/wasi/src/filesystem.rs b/crates/wasi/src/filesystem.rsindex d1d953c14d..eb7027c76f 100644--- a/crates/wasi/src/filesystem.rs+++ b/crates/wasi/src/filesystem.rs@@ -578,26 +578,25 @@ impl Descriptor {atim: Option<SystemTime>,mtim: Option<SystemTime>,) -> Result<(), ErrorCode> {- let mut times = std::fs::FileTimes::new();- if let Some(atim) = atim {- times = times.set_accessed(atim);- }- if let Some(mtim) = mtim {- times = times.set_modified(mtim);- }match self {Self::File(f) => {if f.perms.write_not_permitted() {return Err(ErrorCode::NotPermitted);}- f.run_blocking(move |f| f.set_times(times)).await?;+ f.run_blocking(move |f| {+ crate::filesystem::primitives::set_times_on_fd(f, atim, mtim)+ })+ .await?;Ok(())}Self::Dir(d) => {if d.perms.write_not_permitted() {return Err(ErrorCode::NotPermitted);}- d.run_blocking(move |d| d.set_times(times)).await?;+ d.run_blocking(move |d| {+ crate::filesystem::primitives::set_times_on_fd(d, atim, mtim)+ })+ .await?;Ok(())}}diff --git a/crates/wasi/src/filesystem/primitives/mod.rs b/crates/wasi/src/filesystem/primitives/mod.rsindex 6076f9b69a..6533c6b869 100644--- a/crates/wasi/src/filesystem/primitives/mod.rs+++ b/crates/wasi/src/filesystem/primitives/mod.rs@@ -8,6 +8,7 @@#![allow(clippy::useless_conversion, reason = "typedefs are platform-specific")]use std::path::{Component, Path, PathBuf};+use std::time::SystemTime;use std::{fs, io};mod file_type;@@ -45,6 +46,27 @@ pub(crate) use sys::read_dir;pub(crate) use sys::set_times;pub(crate) use sys::set_times_nofollow;+/// Set the timestamps of the file or directory that `fd` itself refers to.+pub(crate) fn set_times_on_fd(+ fd: &fs::File,+ atime: Option<SystemTime>,+ mtime: Option<SystemTime>,+) -> io::Result<()> {+ #[cfg(any(target_os = "linux", target_os = "android"))]+ if sys::set_times_fast(fd, 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);+ }+ fd.set_times(times)+}+pub(crate) fn open(start: &fs::File, path: &Path, options: &OpenOptions) -> io::Result<fs::File> {#[cfg(any(windows,diff --git a/crates/wasi/src/filesystem/primitives/unix/linux.rs b/crates/wasi/src/filesystem/primitives/unix/linux.rsindex e6629c16bc..c8d39503ea 100644--- a/crates/wasi/src/filesystem/primitives/unix/linux.rs+++ b/crates/wasi/src/filesystem/primitives/unix/linux.rs@@ -174,6 +174,25 @@ pub(crate) fn stat_fast(}}+/// Set the timestamps of the file or directory that `fd` itself refers to.+///+/// `futimens` fails with `EBADF` when `fd` was opened with `O_PATH`, which is+/// how preopened directories are held open (see `dir_utils::target_o_path`).+/// An empty path with `AT_EMPTY_PATH` acts on `fd` itself and is allowed for+/// `O_PATH` descriptors.+pub(crate) fn set_times_fast(+ fd: &fs::File,+ atime: Option<SystemTime>,+ mtime: Option<SystemTime>,+) -> io::Result<bool> {+ let times = Timestamps {+ last_access: to_timespec(atime)?,+ last_modification: to_timespec(mtime)?,+ };+ utimensat(fd, "", ×, AtFlags::EMPTY_PATH)?;+ Ok(true)+}+pub(crate) fn set_times_fallback(start: &fs::File,path: &Path,