Revert fstatat on *nix and test symlinks in path_filestat calls (#1725)

* Revert fstatat on *nix and test symlinks in path_filestat calls

This commit effectively reverts too eager refactoring on my part which
resulted in incorrect `path_filestat_{get, set_times}` behaviour on
*nix hosts. In the presence of symlinks, neither of the calls would
work properly.

In order to shield ourselves from similar errors in the future, I've
augmented the `path_filestat` test cases with symlink checks as well.

* Pass appropriate flags to fstatat and utimensat

* Fix formatting

* Fix Windows build

* Expand final symlinks if follow is set on Windows

* Fix formatting

* Do not follow symlinks unless specified on Windows

* Update comments and restart CI

* Skip testing volatile atim field
This commit is contained in:
Jakub Konka
2020-05-20 21:02:24 +02:00
committed by GitHub
parent 1f620e1b46
commit 348be6f3ed
7 changed files with 202 additions and 42 deletions

View File

@@ -1,7 +1,8 @@
use crate::handle::{Handle, HandleRights};
use crate::sys::osdir::OsDir;
use crate::sys::AsFile;
use crate::wasi::{types, Errno, Result};
use std::convert::TryFrom;
use std::convert::{TryFrom, TryInto};
use std::ffi::OsStr;
use std::fs::File;
use std::os::unix::prelude::{AsRawFd, FromRawFd, OsStrExt};
@@ -204,3 +205,57 @@ pub(crate) fn remove_directory(dirfd: &OsDir, path: &str) -> Result<()> {
unsafe { unlinkat(dirfd.as_raw_fd(), path, AtFlag::REMOVEDIR)? };
Ok(())
}
pub(crate) fn filestat_get_at(dirfd: &OsDir, path: &str, follow: bool) -> Result<types::Filestat> {
use yanix::file::{fstatat, AtFlag};
let flags = if follow {
AtFlag::empty()
} else {
AtFlag::SYMLINK_NOFOLLOW
};
let stat = unsafe { fstatat(dirfd.as_raw_fd(), path, flags)? };
let stat = stat.try_into()?;
Ok(stat)
}
pub(crate) fn filestat_set_times_at(
dirfd: &OsDir,
path: &str,
atim: types::Timestamp,
mtim: types::Timestamp,
fst_flags: types::Fstflags,
follow: bool,
) -> Result<()> {
use std::time::{Duration, UNIX_EPOCH};
use yanix::filetime::*;
let set_atim = fst_flags.contains(&types::Fstflags::ATIM);
let set_atim_now = fst_flags.contains(&types::Fstflags::ATIM_NOW);
let set_mtim = fst_flags.contains(&types::Fstflags::MTIM);
let set_mtim_now = fst_flags.contains(&types::Fstflags::MTIM_NOW);
if (set_atim && set_atim_now) || (set_mtim && set_mtim_now) {
return Err(Errno::Inval);
}
let atim = if set_atim {
let time = UNIX_EPOCH + Duration::from_nanos(atim);
FileTime::FileTime(filetime::FileTime::from_system_time(time))
} else if set_atim_now {
FileTime::Now
} else {
FileTime::Omit
};
let mtim = if set_mtim {
let time = UNIX_EPOCH + Duration::from_nanos(mtim);
FileTime::FileTime(filetime::FileTime::from_system_time(time))
} else if set_mtim_now {
FileTime::Now
} else {
FileTime::Omit
};
utimensat(&*dirfd.as_file()?, path, atim, mtim, !follow)?;
Ok(())
}