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.
This commit is contained in:
Dan Gohman
2022-07-26 15:53:17 -07:00
committed by GitHub
parent ead6edb0c5
commit 0e6ffd0243
2 changed files with 26 additions and 14 deletions

View File

@@ -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() }
}