From 0e6ffd024307563cb8f2bff3e7a44e4293b263d5 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Tue, 26 Jul 2022 15:53:17 -0700 Subject: [PATCH] Don't try to report file size or timestamps for stdio streams. (#4531) * Don't try to report file size or timestamps for stdio streams. Calling `File::metadata()` on a stdio stream handle fails on Windows, where the stdio streams are not files. This `File::metadata()` call was effectively only being used to add file size and timestamps to the result of `filestat_get`. It's common for users to redirect stdio streams to interesting places, and applications generally shouldn't change their behavior depending on the size or timestamps of the file, if the streams are redirected to a file, so just leave these fields to 0, which is commonly understood to represent "unknown". Fixes #4497. --- .../wasi-tests/src/bin/fd_filestat_get.rs | 25 +++++++++++++++++++ crates/wasi-common/cap-std-sync/src/stdio.rs | 15 +---------- 2 files changed, 26 insertions(+), 14 deletions(-) create mode 100644 crates/test-programs/wasi-tests/src/bin/fd_filestat_get.rs diff --git a/crates/test-programs/wasi-tests/src/bin/fd_filestat_get.rs b/crates/test-programs/wasi-tests/src/bin/fd_filestat_get.rs new file mode 100644 index 0000000000..d2ba27a1dd --- /dev/null +++ b/crates/test-programs/wasi-tests/src/bin/fd_filestat_get.rs @@ -0,0 +1,25 @@ +unsafe fn test_fd_filestat_get() { + + let stat = wasi::fd_filestat_get(libc::STDIN_FILENO as u32).expect("failed filestat 0"); + assert_eq!(stat.size, 0, "stdio size should be 0"); + assert_eq!(stat.atim, 0, "stdio atim should be 0"); + assert_eq!(stat.mtim, 0, "stdio mtim should be 0"); + assert_eq!(stat.ctim, 0, "stdio ctim should be 0"); + + let stat = wasi::fd_filestat_get(libc::STDOUT_FILENO as u32).expect("failed filestat 1"); + assert_eq!(stat.size, 0, "stdio size should be 0"); + assert_eq!(stat.atim, 0, "stdio atim should be 0"); + assert_eq!(stat.mtim, 0, "stdio mtim should be 0"); + assert_eq!(stat.ctim, 0, "stdio ctim should be 0"); + + let stat = wasi::fd_filestat_get(libc::STDERR_FILENO as u32).expect("failed filestat 2"); + assert_eq!(stat.size, 0, "stdio size should be 0"); + assert_eq!(stat.atim, 0, "stdio atim should be 0"); + assert_eq!(stat.mtim, 0, "stdio mtim should be 0"); + assert_eq!(stat.ctim, 0, "stdio ctim should be 0"); +} + +fn main() { + // Run the tests. + unsafe { test_fd_filestat_get() } +} diff --git a/crates/wasi-common/cap-std-sync/src/stdio.rs b/crates/wasi-common/cap-std-sync/src/stdio.rs index 85dcd21e67..d53702b03f 100644 --- a/crates/wasi-common/cap-std-sync/src/stdio.rs +++ b/crates/wasi-common/cap-std-sync/src/stdio.rs @@ -16,7 +16,7 @@ use io_lifetimes::{AsFd, BorrowedFd}; #[cfg(windows)] use io_lifetimes::{AsHandle, BorrowedHandle}; use wasi_common::{ - file::{FdFlags, FileType, Filestat, WasiFile}, + file::{FdFlags, FileType, WasiFile}, Error, ErrorExt, }; @@ -126,19 +126,6 @@ macro_rules! wasi_file_write_impl { async fn get_fdflags(&mut self) -> Result { Ok(FdFlags::APPEND) } - async fn get_filestat(&mut self) -> Result { - let meta = self.0.as_filelike_view::().metadata()?; - Ok(Filestat { - device_id: 0, - inode: 0, - filetype: self.get_filetype().await?, - nlink: 0, - size: meta.len(), - atim: meta.accessed().ok(), - mtim: meta.modified().ok(), - ctim: meta.created().ok(), - }) - } async fn write_vectored<'a>(&mut self, bufs: &[io::IoSlice<'a>]) -> Result { let n = (&*self.0.as_filelike_view::()).write_vectored(bufs)?; Ok(n.try_into().map_err(|c| Error::range().context(c))?)