Additional PR feedback changes.

* Add more comments.
* Use `contains` from bitflags.
* Format wasi-test source.
* Remove permission check from Windows `path_open` impl.
This commit is contained in:
Peter Huene
2020-01-09 14:09:56 -08:00
committed by Dan Gohman
parent 1c4c78ab03
commit 4b7677e4da
3 changed files with 18 additions and 18 deletions

View File

@@ -25,8 +25,10 @@ pub fn open_scratch_directory(path: &str) -> Result<wasi::Fd, String> {
dst.set_len(stat.u.dir.pr_name_len);
if dst == path.as_bytes() {
let (base, inherit) = fd_get_rights(i);
return Ok(wasi::path_open(i, 0, ".", wasi::OFLAGS_DIRECTORY, base, inherit, 0)
.expect("failed to open dir"));
return Ok(
wasi::path_open(i, 0, ".", wasi::OFLAGS_DIRECTORY, base, inherit, 0)
.expect("failed to open dir"),
);
}
}
@@ -51,17 +53,11 @@ pub unsafe fn fd_get_rights(fd: wasi::Fd) -> (wasi::Rights, wasi::Rights) {
(fdstat.fs_rights_base, fdstat.fs_rights_inheriting)
}
pub unsafe fn drop_rights(
fd: wasi::Fd,
drop_base: wasi::Rights,
drop_inheriting: wasi::Rights,
) {
pub unsafe fn drop_rights(fd: wasi::Fd, drop_base: wasi::Rights, drop_inheriting: wasi::Rights) {
let (current_base, current_inheriting) = fd_get_rights(fd);
let new_base = current_base & !drop_base;
let new_inheriting = current_inheriting & !drop_inheriting;
wasi::fd_fdstat_set_rights(fd, new_base, new_inheriting).expect(
"dropping fd rights",
);
wasi::fd_fdstat_set_rights(fd, new_base, new_inheriting).expect("dropping fd rights");
}

View File

@@ -88,8 +88,9 @@ pub(crate) fn fd_fdstat_set_flags(
let new_access_mode = file_access_mode_from_fdflags(
fdflags,
(access_mode & AccessMode::FILE_READ_DATA).bits() != 0,
(access_mode & (AccessMode::FILE_WRITE_DATA | AccessMode::FILE_APPEND_DATA)).bits() != 0,
access_mode.contains(AccessMode::FILE_READ_DATA),
access_mode.contains(AccessMode::FILE_WRITE_DATA)
| access_mode.contains(AccessMode::FILE_APPEND_DATA),
);
unsafe {
@@ -139,13 +140,11 @@ pub(crate) fn path_open(
let is_trunc = oflags & wasi::__WASI_OFLAGS_TRUNC != 0;
if is_trunc {
// Windows requires write access for truncation
if !write {
return Err(Error::ENOTCAPABLE);
}
// Windows does not support append mode with truncation
// Windows does not support append mode when opening for truncation
// This is because truncation requires `GENERIC_WRITE` access, which will override the removal
// of the `FILE_WRITE_DATA` permission.
if fdflags & wasi::__WASI_FDFLAGS_APPEND != 0 {
return Err(Error::EINVAL);
return Err(Error::ENOTSUP);
}
}
@@ -232,6 +231,9 @@ fn file_access_mode_from_fdflags(
) -> AccessMode {
let mut access_mode = AccessMode::READ_CONTROL;
// Note that `GENERIC_READ` and `GENERIC_WRITE` cannot be used to properly support append-only mode
// The file-specific flags `FILE_GENERIC_READ` and `FILE_GENERIC_WRITE` are used here instead
// These flags have the same semantic meaning for file objects, but allow removal of specific permissions (see below)
if read {
access_mode.insert(AccessMode::FILE_GENERIC_READ);
}

View File

@@ -436,6 +436,8 @@ pub fn query_mode_information(handle: RawHandle) -> Result<FileModeInformation>
}
pub fn reopen_file(handle: RawHandle, access_mode: AccessMode, flags: Flags) -> Result<RawHandle> {
// Files on Windows are opened with DELETE, READ, and WRITE share mode by default (see OpenOptions in stdlib)
// This keeps the same share mode when reopening the file handle
let new_handle = unsafe {
winbase::ReOpenFile(
handle,