Implement fd_fdstat_set_flags for Windows.
This commit implements `fd_fdstat_set_flags` for Windows. Additionally, it fixes a problem where `O_APPEND` was not working correctly because `GENERIC_WRITE` was always being set; as a result, `FILE_WRITE_DATA` could not be removed from the permission set to properly enable append-only mode. It also treats `O_TRUNC` with `O_APPEND` as an invalid argument error. This is because Windows cannot support these two flags together. To support `O_TRUNC`, the `GENERIC_WRITE` bit must be set for the file access flags. Setting this bit will cause `FILE_WRITE_DATA` to be set, which will not properly treat the file as append-only (it requires `FILE_APPEND_DATA` without `FILE_WRITE_DATA`).
This commit is contained in:
@@ -67,7 +67,7 @@ hostcalls! {
|
||||
) -> wasi::__wasi_errno_t;
|
||||
|
||||
pub unsafe fn fd_fdstat_set_flags(
|
||||
wasi_ctx: &WasiCtx,
|
||||
wasi_ctx: &mut WasiCtx,
|
||||
memory: &mut [u8],
|
||||
fd: wasi::__wasi_fd_t,
|
||||
fdflags: wasi::__wasi_fdflags_t,
|
||||
|
||||
@@ -298,19 +298,24 @@ pub(crate) unsafe fn fd_fdstat_get(
|
||||
}
|
||||
|
||||
pub(crate) unsafe fn fd_fdstat_set_flags(
|
||||
wasi_ctx: &WasiCtx,
|
||||
wasi_ctx: &mut WasiCtx,
|
||||
_memory: &mut [u8],
|
||||
fd: wasi::__wasi_fd_t,
|
||||
fdflags: wasi::__wasi_fdflags_t,
|
||||
) -> Result<()> {
|
||||
trace!("fd_fdstat_set_flags(fd={:?}, fdflags={:#x?})", fd, fdflags);
|
||||
|
||||
let fd = wasi_ctx
|
||||
.get_fd_entry(fd)?
|
||||
.as_descriptor(wasi::__WASI_RIGHTS_FD_FDSTAT_SET_FLAGS, 0)?
|
||||
.as_os_handle();
|
||||
let descriptor = wasi_ctx
|
||||
.get_fd_entry_mut(fd)?
|
||||
.as_descriptor_mut(wasi::__WASI_RIGHTS_FD_FDSTAT_SET_FLAGS, 0)?;
|
||||
|
||||
hostcalls_impl::fd_fdstat_set_flags(&fd, fdflags)
|
||||
if let Some(new_handle) =
|
||||
hostcalls_impl::fd_fdstat_set_flags(&descriptor.as_os_handle(), fdflags)?
|
||||
{
|
||||
*descriptor = Descriptor::OsHandle(new_handle);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) unsafe fn fd_fdstat_set_rights(
|
||||
|
||||
@@ -29,9 +29,14 @@ pub(crate) fn fd_fdstat_get(fd: &File) -> Result<wasi::__wasi_fdflags_t> {
|
||||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
pub(crate) fn fd_fdstat_set_flags(fd: &File, fdflags: wasi::__wasi_fdflags_t) -> Result<()> {
|
||||
pub(crate) fn fd_fdstat_set_flags(
|
||||
fd: &File,
|
||||
fdflags: wasi::__wasi_fdflags_t,
|
||||
) -> Result<Option<OsHandle>> {
|
||||
let nix_flags = host_impl::nix_from_fdflags(fdflags);
|
||||
unsafe { yanix::fcntl::set_status_flags(fd.as_raw_fd(), nix_flags) }.map_err(Into::into)
|
||||
unsafe { yanix::fcntl::set_status_flags(fd.as_raw_fd(), nix_flags) }
|
||||
.map(|_| None)
|
||||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
pub(crate) fn fd_advise(
|
||||
|
||||
@@ -5,7 +5,7 @@ use crate::ctx::WasiCtx;
|
||||
use crate::fdentry::FdEntry;
|
||||
use crate::host::{Dirent, FileType};
|
||||
use crate::hostcalls_impl::{fd_filestat_set_times_impl, PathGet};
|
||||
use crate::sys::fdentry_impl::determine_type_rights;
|
||||
use crate::sys::fdentry_impl::{determine_type_rights, OsHandle};
|
||||
use crate::sys::host_impl::{self, path_from_host};
|
||||
use crate::sys::hostcalls_impl::fs_helpers::PathGetExt;
|
||||
use crate::{wasi, Error, Result};
|
||||
@@ -78,8 +78,25 @@ pub(crate) fn fd_fdstat_get(fd: &File) -> Result<wasi::__wasi_fdflags_t> {
|
||||
Ok(fdflags)
|
||||
}
|
||||
|
||||
pub(crate) fn fd_fdstat_set_flags(fd: &File, fdflags: wasi::__wasi_fdflags_t) -> Result<()> {
|
||||
unimplemented!("fd_fdstat_set_flags")
|
||||
pub(crate) fn fd_fdstat_set_flags(
|
||||
fd: &File,
|
||||
fdflags: wasi::__wasi_fdflags_t,
|
||||
) -> Result<Option<OsHandle>> {
|
||||
let handle = unsafe { fd.as_raw_handle() };
|
||||
|
||||
let access_mode = winx::file::query_access_information(handle)?;
|
||||
|
||||
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,
|
||||
);
|
||||
|
||||
unsafe {
|
||||
Ok(Some(OsHandle::from(File::from_raw_handle(
|
||||
winx::file::reopen_file(handle, new_access_mode, file_flags_from_fdflags(fdflags))?,
|
||||
))))
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn fd_advise(
|
||||
@@ -119,9 +136,22 @@ pub(crate) fn path_open(
|
||||
) -> Result<File> {
|
||||
use winx::file::{AccessMode, CreationDisposition, Flags};
|
||||
|
||||
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
|
||||
if fdflags & wasi::__WASI_FDFLAGS_APPEND != 0 {
|
||||
return Err(Error::EINVAL);
|
||||
}
|
||||
}
|
||||
|
||||
// convert open flags
|
||||
// note: the calls to `write(true)` are to bypass an internal OpenOption check
|
||||
// the write flag will ultimately be ignored when `access_mode` is called below.
|
||||
// the write flag will ultimately be ignored when `access_mode` is calculated below.
|
||||
let mut opts = OpenOptions::new();
|
||||
match creation_disposition_from_oflags(oflags) {
|
||||
CreationDisposition::CREATE_ALWAYS => {
|
||||
@@ -168,7 +198,14 @@ pub(crate) fn path_open(
|
||||
},
|
||||
}
|
||||
|
||||
opts.access_mode(file_access_mode_from_fdflags(fdflags, read, write).bits())
|
||||
let mut access_mode = file_access_mode_from_fdflags(fdflags, read, write);
|
||||
|
||||
// Truncation requires the special `GENERIC_WRITE` bit set (this is why it doesn't work with append-only mode)
|
||||
if is_trunc {
|
||||
access_mode |= AccessMode::GENERIC_WRITE;
|
||||
}
|
||||
|
||||
opts.access_mode(access_mode.bits())
|
||||
.custom_flags(file_flags_from_fdflags(fdflags).bits())
|
||||
.open(&path)
|
||||
.map_err(Into::into)
|
||||
@@ -196,11 +233,11 @@ fn file_access_mode_from_fdflags(
|
||||
let mut access_mode = AccessMode::READ_CONTROL;
|
||||
|
||||
if read {
|
||||
access_mode.insert(AccessMode::GENERIC_READ);
|
||||
access_mode.insert(AccessMode::FILE_GENERIC_READ);
|
||||
}
|
||||
|
||||
if write {
|
||||
access_mode.insert(AccessMode::GENERIC_WRITE);
|
||||
access_mode.insert(AccessMode::FILE_GENERIC_WRITE);
|
||||
}
|
||||
|
||||
// For append, grant the handle FILE_APPEND_DATA access but *not* FILE_WRITE_DATA.
|
||||
|
||||
@@ -434,3 +434,20 @@ pub fn query_mode_information(handle: RawHandle) -> Result<FileModeInformation>
|
||||
|
||||
Ok(FileModeInformation::from_bits_truncate(info.Mode))
|
||||
}
|
||||
|
||||
pub fn reopen_file(handle: RawHandle, access_mode: AccessMode, flags: Flags) -> Result<RawHandle> {
|
||||
let new_handle = unsafe {
|
||||
winbase::ReOpenFile(
|
||||
handle,
|
||||
access_mode.bits(),
|
||||
winnt::FILE_SHARE_DELETE | winnt::FILE_SHARE_READ | winnt::FILE_SHARE_WRITE,
|
||||
flags.bits(),
|
||||
)
|
||||
};
|
||||
|
||||
if new_handle == winapi::um::handleapi::INVALID_HANDLE_VALUE {
|
||||
return Err(winerror::WinError::last());
|
||||
}
|
||||
|
||||
Ok(new_handle)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user