path_get refactor and implementation of missing path_ hostcalls on Windows (#41)
* Move path_get outside of sys module * Add implementation of readlinkat * Clean up path_open; use OpenOptions as much as possible * Enable close_preopen test * Implement path_create_directory; fix path_open * Refactor path concatenation onto a descriptor * Implement path_remove_directory * Implement path_unlink_file * Rewrite path_open using specific access mask * Fix error mapping when unlinking file * Fix readlinkat to pass nofollow_errors testcase * Clean up winerror to WASI conversion * Spoof creating dangling symlinks on windows (hacky!) * Add positive testcase for readlink * Implement path_readlink (for nonzero buffers for now) * Clean up * Add Symlink struct immitating *nix symlink * Fix path_readlink * Augment interesting_paths testcase with trailing slashes example * Encapsulate path_get return value as PathGet struct * Remove dangling symlink emulation * Extract dangling symlinks into its own testcase This way, we can re-enable nofollow_errors testcase on Windows also. * Return __WASI_ENOTCAPABLE if user lacks perms to symlink
This commit is contained in:
@@ -23,19 +23,18 @@ pub(crate) fn determine_type_and_access_rights<Handle: AsRawHandle>(
|
||||
host::__wasi_rights_t,
|
||||
host::__wasi_rights_t,
|
||||
)> {
|
||||
use winx::file::{get_file_access_rights, AccessRight};
|
||||
use winx::file::{get_file_access_mode, AccessMode};
|
||||
|
||||
let (file_type, mut rights_base, rights_inheriting) = determine_type_rights(handle)?;
|
||||
|
||||
match file_type {
|
||||
host::__WASI_FILETYPE_DIRECTORY | host::__WASI_FILETYPE_REGULAR_FILE => {
|
||||
let rights = get_file_access_rights(handle.as_raw_handle())
|
||||
.map_err(host_impl::errno_from_win)?;
|
||||
let rights = AccessRight::from_bits_truncate(rights);
|
||||
if rights.contains(AccessRight::FILE_GENERIC_READ) {
|
||||
let mode =
|
||||
get_file_access_mode(handle.as_raw_handle()).map_err(host_impl::errno_from_win)?;
|
||||
if mode.contains(AccessMode::FILE_GENERIC_READ) {
|
||||
rights_base |= host::__WASI_RIGHT_FD_READ;
|
||||
}
|
||||
if rights.contains(AccessRight::FILE_GENERIC_WRITE) {
|
||||
if mode.contains(AccessMode::FILE_GENERIC_WRITE) {
|
||||
rights_base |= host::__WASI_RIGHT_FD_WRITE;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,10 @@
|
||||
#![allow(unused)]
|
||||
use crate::{host, Result};
|
||||
use std::ffi::OsStr;
|
||||
use std::fs::OpenOptions;
|
||||
use std::os::windows::ffi::OsStrExt;
|
||||
use std::os::windows::fs::OpenOptionsExt;
|
||||
use winx::file::{AccessMode, Attributes, CreationDisposition, Flags};
|
||||
|
||||
pub(crate) fn errno_from_win(error: winx::winerror::WinError) -> host::__wasi_errno_t {
|
||||
// TODO: implement error mapping between Windows and WASI
|
||||
@@ -12,55 +15,36 @@ pub(crate) fn errno_from_win(error: winx::winerror::WinError) -> host::__wasi_er
|
||||
match error {
|
||||
ERROR_SUCCESS => host::__WASI_ESUCCESS,
|
||||
ERROR_BAD_ENVIRONMENT => host::__WASI_E2BIG,
|
||||
ERROR_FILE_NOT_FOUND | ERROR_PATH_NOT_FOUND => host::__WASI_ENOENT,
|
||||
ERROR_FILE_NOT_FOUND => host::__WASI_ENOENT,
|
||||
ERROR_PATH_NOT_FOUND => host::__WASI_ENOENT,
|
||||
ERROR_TOO_MANY_OPEN_FILES => host::__WASI_ENFILE,
|
||||
ERROR_ACCESS_DENIED | ERROR_SHARING_VIOLATION => host::__WASI_EACCES,
|
||||
ERROR_INVALID_HANDLE | ERROR_INVALID_NAME => host::__WASI_EBADF,
|
||||
ERROR_NOT_ENOUGH_MEMORY | ERROR_OUTOFMEMORY => host::__WASI_ENOMEM,
|
||||
ERROR_ACCESS_DENIED => host::__WASI_EACCES,
|
||||
ERROR_SHARING_VIOLATION => host::__WASI_EACCES,
|
||||
ERROR_PRIVILEGE_NOT_HELD => host::__WASI_ENOTCAPABLE, // TODO is this the correct mapping?
|
||||
ERROR_INVALID_HANDLE => host::__WASI_EBADF,
|
||||
ERROR_INVALID_NAME => host::__WASI_EINVAL,
|
||||
ERROR_NOT_ENOUGH_MEMORY => host::__WASI_ENOMEM,
|
||||
ERROR_OUTOFMEMORY => host::__WASI_ENOMEM,
|
||||
ERROR_DIR_NOT_EMPTY => host::__WASI_ENOTEMPTY,
|
||||
ERROR_DEV_NOT_EXIST => host::__WASI_EINVAL,
|
||||
ERROR_NOT_READY | ERROR_BUSY => host::__WASI_EBUSY,
|
||||
ERROR_NOT_READY => host::__WASI_EBUSY,
|
||||
ERROR_BUSY => host::__WASI_EBUSY,
|
||||
ERROR_NOT_SUPPORTED => host::__WASI_ENOTSUP,
|
||||
ERROR_FILE_EXISTS => host::__WASI_EEXIST,
|
||||
ERROR_BROKEN_PIPE => host::__WASI_EPIPE,
|
||||
ERROR_BUFFER_OVERFLOW => host::__WASI_ENAMETOOLONG,
|
||||
ERROR_DISK_FULL => host::__WASI_ENOSPC,
|
||||
ERROR_SHARING_BUFFER_EXCEEDED => host::__WASI_ENFILE,
|
||||
ERROR_NOT_A_REPARSE_POINT => host::__WASI_EINVAL,
|
||||
ERROR_NEGATIVE_SEEK => host::__WASI_EINVAL,
|
||||
_ => host::__WASI_ENOTSUP,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn win_from_fdflags(
|
||||
fdflags: host::__wasi_fdflags_t,
|
||||
) -> (winx::file::AccessRight, winx::file::FlagsAndAttributes) {
|
||||
use winx::file::{AccessRight, FlagsAndAttributes};
|
||||
// TODO verify this!
|
||||
let mut win_rights = AccessRight::empty();
|
||||
let mut win_flags_attrs = FlagsAndAttributes::empty();
|
||||
|
||||
if fdflags & host::__WASI_FDFLAG_NONBLOCK != 0 {
|
||||
win_flags_attrs.insert(FlagsAndAttributes::FILE_FLAG_OVERLAPPED);
|
||||
}
|
||||
if fdflags & host::__WASI_FDFLAG_APPEND != 0 {
|
||||
win_rights.insert(AccessRight::FILE_APPEND_DATA);
|
||||
}
|
||||
if fdflags & host::__WASI_FDFLAG_DSYNC != 0
|
||||
|| fdflags & host::__WASI_FDFLAG_RSYNC != 0
|
||||
|| fdflags & host::__WASI_FDFLAG_SYNC != 0
|
||||
{
|
||||
win_rights.insert(AccessRight::SYNCHRONIZE);
|
||||
}
|
||||
(win_rights, win_flags_attrs)
|
||||
}
|
||||
|
||||
pub(crate) fn fdflags_from_win(rights: winx::file::AccessRight) -> host::__wasi_fdflags_t {
|
||||
use winx::file::AccessRight;
|
||||
pub(crate) fn fdflags_from_win(mode: AccessMode) -> host::__wasi_fdflags_t {
|
||||
let mut fdflags = 0;
|
||||
// TODO verify this!
|
||||
if rights.contains(AccessRight::FILE_APPEND_DATA) {
|
||||
if mode.contains(AccessMode::FILE_APPEND_DATA) {
|
||||
fdflags |= host::__WASI_FDFLAG_APPEND;
|
||||
}
|
||||
if rights.contains(AccessRight::SYNCHRONIZE) {
|
||||
if mode.contains(AccessMode::SYNCHRONIZE) {
|
||||
fdflags |= host::__WASI_FDFLAG_DSYNC;
|
||||
fdflags |= host::__WASI_FDFLAG_RSYNC;
|
||||
fdflags |= host::__WASI_FDFLAG_SYNC;
|
||||
@@ -77,31 +61,39 @@ pub(crate) fn fdflags_from_win(rights: winx::file::AccessRight) -> host::__wasi_
|
||||
fdflags
|
||||
}
|
||||
|
||||
pub(crate) fn win_from_oflags(
|
||||
oflags: host::__wasi_oflags_t,
|
||||
) -> (
|
||||
winx::file::CreationDisposition,
|
||||
winx::file::FlagsAndAttributes,
|
||||
) {
|
||||
use winx::file::{CreationDisposition, FlagsAndAttributes};
|
||||
pub(crate) fn win_from_fdflags(fdflags: host::__wasi_fdflags_t) -> (AccessMode, Flags) {
|
||||
let mut access_mode = AccessMode::empty();
|
||||
let mut flags = Flags::empty();
|
||||
|
||||
let win_flags_attrs = if oflags & host::__WASI_O_DIRECTORY != 0 {
|
||||
FlagsAndAttributes::FILE_FLAG_BACKUP_SEMANTICS
|
||||
} else {
|
||||
FlagsAndAttributes::FILE_ATTRIBUTE_NORMAL
|
||||
};
|
||||
// TODO verify this!
|
||||
if fdflags & host::__WASI_FDFLAG_NONBLOCK != 0 {
|
||||
flags.insert(Flags::FILE_FLAG_OVERLAPPED);
|
||||
}
|
||||
if fdflags & host::__WASI_FDFLAG_APPEND != 0 {
|
||||
access_mode.insert(AccessMode::FILE_APPEND_DATA);
|
||||
}
|
||||
if fdflags & host::__WASI_FDFLAG_DSYNC != 0
|
||||
|| fdflags & host::__WASI_FDFLAG_RSYNC != 0
|
||||
|| fdflags & host::__WASI_FDFLAG_SYNC != 0
|
||||
{
|
||||
access_mode.insert(AccessMode::SYNCHRONIZE);
|
||||
}
|
||||
|
||||
let win_disp = if oflags & host::__WASI_O_CREAT != 0 && oflags & host::__WASI_O_EXCL != 0 {
|
||||
CreationDisposition::CREATE_NEW
|
||||
} else if oflags & host::__WASI_O_CREAT != 0 {
|
||||
CreationDisposition::CREATE_ALWAYS
|
||||
(access_mode, flags)
|
||||
}
|
||||
|
||||
pub(crate) fn win_from_oflags(oflags: host::__wasi_oflags_t) -> CreationDisposition {
|
||||
if oflags & host::__WASI_O_CREAT != 0 {
|
||||
if oflags & host::__WASI_O_EXCL != 0 {
|
||||
CreationDisposition::CREATE_NEW
|
||||
} else {
|
||||
CreationDisposition::CREATE_ALWAYS
|
||||
}
|
||||
} else if oflags & host::__WASI_O_TRUNC != 0 {
|
||||
CreationDisposition::TRUNCATE_EXISTING
|
||||
} else {
|
||||
CreationDisposition::OPEN_EXISTING
|
||||
};
|
||||
|
||||
(win_disp, win_flags_attrs)
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates owned WASI path from OS string.
|
||||
|
||||
@@ -4,15 +4,17 @@ use super::fs_helpers::*;
|
||||
use crate::ctx::WasiCtx;
|
||||
use crate::fdentry::FdEntry;
|
||||
use crate::helpers::systemtime_to_timestamp;
|
||||
use crate::sys::{errno_from_ioerror, errno_from_host};
|
||||
use crate::hostcalls_impl::PathGet;
|
||||
use crate::sys::fdentry_impl::determine_type_rights;
|
||||
use crate::sys::host_impl;
|
||||
use crate::sys::{errno_from_host, errno_from_ioerror};
|
||||
use crate::{host, Result};
|
||||
use std::convert::TryInto;
|
||||
use std::fs::{File, Metadata};
|
||||
use std::fs::{File, Metadata, OpenOptions};
|
||||
use std::io::{self, Seek, SeekFrom};
|
||||
use std::os::windows::fs::FileExt;
|
||||
use std::os::windows::fs::{FileExt, OpenOptionsExt};
|
||||
use std::os::windows::prelude::{AsRawHandle, FromRawHandle};
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
fn read_at(mut file: &File, buf: &mut [u8], offset: u64) -> io::Result<usize> {
|
||||
// get current cursor position
|
||||
@@ -49,13 +51,10 @@ pub(crate) fn fd_pwrite(file: &File, buf: &[u8], offset: host::__wasi_filesize_t
|
||||
}
|
||||
|
||||
pub(crate) fn fd_fdstat_get(fd: &File) -> Result<host::__wasi_fdflags_t> {
|
||||
use winx::file::AccessRight;
|
||||
match winx::file::get_file_access_rights(fd.as_raw_handle())
|
||||
.map(AccessRight::from_bits_truncate)
|
||||
{
|
||||
Ok(rights) => Ok(host_impl::fdflags_from_win(rights)),
|
||||
Err(e) => Err(host_impl::errno_from_win(e)),
|
||||
}
|
||||
use winx::file::AccessMode;
|
||||
winx::file::get_file_access_mode(fd.as_raw_handle())
|
||||
.map(host_impl::fdflags_from_win)
|
||||
.map_err(host_impl::errno_from_win)
|
||||
}
|
||||
|
||||
pub(crate) fn fd_fdstat_set_flags(fd: &File, fdflags: host::__wasi_fdflags_t) -> Result<()> {
|
||||
@@ -71,87 +70,90 @@ pub(crate) fn fd_advise(
|
||||
unimplemented!("fd_advise")
|
||||
}
|
||||
|
||||
pub(crate) fn path_create_directory(dirfd: &File, path: &str) -> Result<()> {
|
||||
unimplemented!("path_create_directory")
|
||||
pub(crate) fn path_create_directory(resolved: PathGet) -> Result<()> {
|
||||
let path = concatenate(resolved.dirfd(), Path::new(resolved.path()))?;
|
||||
std::fs::create_dir(&path).map_err(errno_from_ioerror)
|
||||
}
|
||||
|
||||
pub(crate) fn path_link(
|
||||
old_dirfd: &File,
|
||||
new_dirfd: &File,
|
||||
old_path: &str,
|
||||
new_path: &str,
|
||||
) -> Result<()> {
|
||||
pub(crate) fn path_link(resolved_old: PathGet, resolved_new: PathGet) -> Result<()> {
|
||||
unimplemented!("path_link")
|
||||
}
|
||||
|
||||
pub(crate) fn path_open(
|
||||
ctx: &WasiCtx,
|
||||
dirfd: host::__wasi_fd_t,
|
||||
dirflags: host::__wasi_lookupflags_t,
|
||||
path: &str,
|
||||
oflags: host::__wasi_oflags_t,
|
||||
resolved: PathGet,
|
||||
read: bool,
|
||||
write: bool,
|
||||
mut needed_base: host::__wasi_rights_t,
|
||||
mut needed_inheriting: host::__wasi_rights_t,
|
||||
fs_flags: host::__wasi_fdflags_t,
|
||||
) -> Result<FdEntry> {
|
||||
use winx::file::{AccessRight, CreationDisposition, FlagsAndAttributes, ShareMode};
|
||||
oflags: host::__wasi_oflags_t,
|
||||
fdflags: host::__wasi_fdflags_t,
|
||||
) -> Result<File> {
|
||||
use winx::file::{AccessMode, CreationDisposition, Flags};
|
||||
|
||||
let mut win_rights = AccessRight::READ_CONTROL;
|
||||
let mut access_mode = AccessMode::READ_CONTROL;
|
||||
if read {
|
||||
win_rights.insert(AccessRight::FILE_GENERIC_READ);
|
||||
access_mode.insert(AccessMode::FILE_GENERIC_READ);
|
||||
}
|
||||
if write {
|
||||
win_rights.insert(AccessRight::FILE_GENERIC_WRITE);
|
||||
access_mode.insert(AccessMode::FILE_GENERIC_WRITE);
|
||||
}
|
||||
|
||||
let mut flags = Flags::FILE_FLAG_BACKUP_SEMANTICS;
|
||||
|
||||
// convert open flags
|
||||
let (win_create_disp, mut win_flags_attrs) = host_impl::win_from_oflags(oflags);
|
||||
if win_create_disp == CreationDisposition::CREATE_NEW {
|
||||
needed_base |= host::__WASI_RIGHT_PATH_CREATE_FILE;
|
||||
} else if win_create_disp == CreationDisposition::CREATE_ALWAYS {
|
||||
needed_base |= host::__WASI_RIGHT_PATH_CREATE_FILE;
|
||||
} else if win_create_disp == CreationDisposition::TRUNCATE_EXISTING {
|
||||
needed_base |= host::__WASI_RIGHT_PATH_FILESTAT_SET_SIZE;
|
||||
let mut opts = OpenOptions::new();
|
||||
match host_impl::win_from_oflags(oflags) {
|
||||
CreationDisposition::CREATE_ALWAYS => {
|
||||
opts.create(true).append(true);
|
||||
}
|
||||
CreationDisposition::CREATE_NEW => {
|
||||
opts.create_new(true).write(true);
|
||||
}
|
||||
CreationDisposition::TRUNCATE_EXISTING => {
|
||||
opts.truncate(true);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
// convert file descriptor flags
|
||||
let win_fdflags_res = host_impl::win_from_fdflags(fs_flags);
|
||||
win_rights.insert(win_fdflags_res.0);
|
||||
win_flags_attrs.insert(win_fdflags_res.1);
|
||||
if win_rights.contains(AccessRight::SYNCHRONIZE) {
|
||||
needed_inheriting |= host::__WASI_RIGHT_FD_DATASYNC;
|
||||
needed_inheriting |= host::__WASI_RIGHT_FD_SYNC;
|
||||
let (add_access_mode, add_flags) = host_impl::win_from_fdflags(fdflags);
|
||||
access_mode.insert(add_access_mode);
|
||||
flags.insert(add_flags);
|
||||
|
||||
let path = concatenate(resolved.dirfd(), Path::new(resolved.path()))?;
|
||||
|
||||
match path.symlink_metadata().map(|metadata| metadata.file_type()) {
|
||||
Ok(file_type) => {
|
||||
// check if we are trying to open a symlink
|
||||
if file_type.is_symlink() {
|
||||
return Err(host::__WASI_ELOOP);
|
||||
}
|
||||
// check if we are trying to open a file as a dir
|
||||
if file_type.is_file() && oflags & host::__WASI_O_DIRECTORY != 0 {
|
||||
return Err(host::__WASI_ENOTDIR);
|
||||
}
|
||||
}
|
||||
Err(e) => match e.raw_os_error() {
|
||||
Some(e) => {
|
||||
use winx::winerror::WinError;
|
||||
log::debug!("path_open at symlink_metadata error code={:?}", e);
|
||||
let e = WinError::from_u32(e as u32);
|
||||
|
||||
if e != WinError::ERROR_FILE_NOT_FOUND {
|
||||
return Err(host_impl::errno_from_win(e));
|
||||
}
|
||||
// file not found, let it proceed to actually
|
||||
// trying to open it
|
||||
}
|
||||
None => {
|
||||
log::debug!("Inconvertible OS error: {}", e);
|
||||
return Err(host::__WASI_EIO);
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
let dirfe = ctx.get_fd_entry(dirfd, needed_base, needed_inheriting)?;
|
||||
let dirfd = dirfe.fd_object.descriptor.as_file()?;
|
||||
let (dir, path) = path_get(
|
||||
dirfd,
|
||||
dirflags,
|
||||
path,
|
||||
!win_flags_attrs.contains(FlagsAndAttributes::FILE_FLAG_BACKUP_SEMANTICS),
|
||||
)?;
|
||||
|
||||
let new_handle = winx::file::openat(
|
||||
dir.as_raw_handle(),
|
||||
path.as_str(),
|
||||
win_rights,
|
||||
win_create_disp,
|
||||
win_flags_attrs,
|
||||
)
|
||||
.map_err(host_impl::errno_from_win)?;
|
||||
|
||||
// Determine the type of the new file descriptor and which rights contradict with this type
|
||||
let file = unsafe { File::from_raw_handle(new_handle) };
|
||||
determine_type_rights(&file).and_then(|(_ty, max_base, max_inheriting)| {
|
||||
FdEntry::from(file).map(|mut fe| {
|
||||
fe.rights_base &= max_base;
|
||||
fe.rights_inheriting &= max_inheriting;
|
||||
fe
|
||||
})
|
||||
})
|
||||
opts.access_mode(access_mode.bits())
|
||||
.custom_flags(flags.bits())
|
||||
.open(&path)
|
||||
.map_err(errno_from_ioerror)
|
||||
}
|
||||
|
||||
pub(crate) fn fd_readdir(
|
||||
@@ -162,16 +164,45 @@ pub(crate) fn fd_readdir(
|
||||
unimplemented!("fd_readdir")
|
||||
}
|
||||
|
||||
pub(crate) fn path_readlink(dirfd: &File, path: &str, buf: &mut [u8]) -> Result<usize> {
|
||||
unimplemented!("path_readlink")
|
||||
pub(crate) fn path_readlink(resolved: PathGet, buf: &mut [u8]) -> Result<usize> {
|
||||
use winx::file::get_path_by_handle;
|
||||
|
||||
let path = concatenate(resolved.dirfd(), Path::new(resolved.path()))?;
|
||||
let target_path = path.read_link().map_err(errno_from_ioerror)?;
|
||||
|
||||
// since on Windows we are effectively emulating 'at' syscalls
|
||||
// we need to strip the prefix from the absolute path
|
||||
// as otherwise we will error out since WASI is not capable
|
||||
// of dealing with absolute paths
|
||||
let dir_path =
|
||||
get_path_by_handle(resolved.dirfd().as_raw_handle()).map_err(host_impl::errno_from_win)?;
|
||||
let dir_path = PathBuf::from(strip_extended_prefix(dir_path));
|
||||
let target_path = target_path
|
||||
.strip_prefix(dir_path)
|
||||
.map_err(|_| host::__WASI_ENOTCAPABLE)
|
||||
.and_then(|path| path.to_str().map(String::from).ok_or(host::__WASI_EILSEQ))?;
|
||||
|
||||
if buf.len() > 0 {
|
||||
let mut chars = target_path.chars();
|
||||
let mut nread = 0usize;
|
||||
|
||||
for i in 0..buf.len() {
|
||||
match chars.next() {
|
||||
Some(ch) => {
|
||||
buf[i] = ch as u8;
|
||||
nread += 1;
|
||||
}
|
||||
None => break,
|
||||
}
|
||||
}
|
||||
|
||||
Ok(nread)
|
||||
} else {
|
||||
Ok(0)
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn path_rename(
|
||||
old_dirfd: &File,
|
||||
old_path: &str,
|
||||
new_dirfd: &File,
|
||||
new_path: &str,
|
||||
) -> Result<()> {
|
||||
pub(crate) fn path_rename(resolved_old: PathGet, resolved_new: PathGet) -> Result<()> {
|
||||
unimplemented!("path_rename")
|
||||
}
|
||||
|
||||
@@ -250,17 +281,15 @@ pub(crate) fn fd_filestat_set_size(fd: &File, st_size: host::__wasi_filesize_t)
|
||||
}
|
||||
|
||||
pub(crate) fn path_filestat_get(
|
||||
dirfd: &File,
|
||||
resolved: PathGet,
|
||||
dirflags: host::__wasi_lookupflags_t,
|
||||
path: &str,
|
||||
) -> Result<host::__wasi_filestat_t> {
|
||||
unimplemented!("path_filestat_get")
|
||||
}
|
||||
|
||||
pub(crate) fn path_filestat_set_times(
|
||||
dirfd: &File,
|
||||
resolved: PathGet,
|
||||
dirflags: host::__wasi_lookupflags_t,
|
||||
path: &str,
|
||||
st_atim: host::__wasi_timestamp_t,
|
||||
mut st_mtim: host::__wasi_timestamp_t,
|
||||
fst_flags: host::__wasi_fstflags_t,
|
||||
@@ -268,14 +297,78 @@ pub(crate) fn path_filestat_set_times(
|
||||
unimplemented!("path_filestat_set_times")
|
||||
}
|
||||
|
||||
pub(crate) fn path_symlink(dirfd: &File, old_path: &str, new_path: &str) -> Result<()> {
|
||||
unimplemented!("path_symlink")
|
||||
pub(crate) fn path_symlink(old_path: &str, resolved: PathGet) -> Result<()> {
|
||||
use std::os::windows::fs::{symlink_dir, symlink_file};
|
||||
use winx::winerror::WinError;
|
||||
|
||||
let old_path = concatenate(resolved.dirfd(), Path::new(old_path))?;
|
||||
let new_path = concatenate(resolved.dirfd(), Path::new(resolved.path()))?;
|
||||
|
||||
// try creating a file symlink
|
||||
symlink_file(&old_path, &new_path).or_else(|e| {
|
||||
match e.raw_os_error() {
|
||||
Some(e) => {
|
||||
log::debug!("path_symlink at symlink_file error code={:?}", e);
|
||||
match WinError::from_u32(e as u32) {
|
||||
WinError::ERROR_NOT_A_REPARSE_POINT => {
|
||||
// try creating a dir symlink instead
|
||||
symlink_dir(old_path, new_path).map_err(errno_from_ioerror)
|
||||
}
|
||||
e => Err(host_impl::errno_from_win(e)),
|
||||
}
|
||||
}
|
||||
None => {
|
||||
log::debug!("Inconvertible OS error: {}", e);
|
||||
Err(host::__WASI_EIO)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn path_unlink_file(dirfd: &File, path: &str) -> Result<()> {
|
||||
unimplemented!("path_unlink_file")
|
||||
pub(crate) fn path_unlink_file(resolved: PathGet) -> Result<()> {
|
||||
use std::fs;
|
||||
use winx::winerror::WinError;
|
||||
|
||||
let path = concatenate(resolved.dirfd(), Path::new(resolved.path()))?;
|
||||
let file_type = path
|
||||
.symlink_metadata()
|
||||
.map(|metadata| metadata.file_type())
|
||||
.map_err(errno_from_ioerror)?;
|
||||
|
||||
// check if we're unlinking a symlink
|
||||
// NB this will get cleaned up a lot when [std::os::windows::fs::FileTypeExt]
|
||||
// stabilises
|
||||
//
|
||||
// [std::os::windows::fs::FileTypeExt]: https://doc.rust-lang.org/std/os/windows/fs/trait.FileTypeExt.html
|
||||
if file_type.is_symlink() {
|
||||
fs::remove_file(&path).or_else(|e| {
|
||||
match e.raw_os_error() {
|
||||
Some(e) => {
|
||||
log::debug!("path_unlink_file at symlink_file error code={:?}", e);
|
||||
match WinError::from_u32(e as u32) {
|
||||
WinError::ERROR_ACCESS_DENIED => {
|
||||
// try unlinking a dir symlink instead
|
||||
fs::remove_dir(path).map_err(errno_from_ioerror)
|
||||
}
|
||||
e => Err(host_impl::errno_from_win(e)),
|
||||
}
|
||||
}
|
||||
None => {
|
||||
log::debug!("Inconvertible OS error: {}", e);
|
||||
Err(host::__WASI_EIO)
|
||||
}
|
||||
}
|
||||
})
|
||||
} else if file_type.is_dir() {
|
||||
Err(host::__WASI_EISDIR)
|
||||
} else if file_type.is_file() {
|
||||
fs::remove_file(path).map_err(errno_from_ioerror)
|
||||
} else {
|
||||
Err(host::__WASI_EINVAL)
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn path_remove_directory(dirfd: &File, path: &str) -> Result<()> {
|
||||
unimplemented!("path_remove_directory")
|
||||
pub(crate) fn path_remove_directory(resolved: PathGet) -> Result<()> {
|
||||
let path = concatenate(resolved.dirfd(), Path::new(resolved.path()))?;
|
||||
std::fs::remove_dir(&path).map_err(errno_from_ioerror)
|
||||
}
|
||||
|
||||
@@ -1,120 +1,141 @@
|
||||
#![allow(non_camel_case_types)]
|
||||
#![allow(unused_unsafe)]
|
||||
|
||||
use crate::sys::errno_from_host;
|
||||
use crate::sys::host_impl;
|
||||
use crate::{host, Result};
|
||||
use std::ffi::{OsStr, OsString};
|
||||
use std::fs::File;
|
||||
use std::os::windows::prelude::{AsRawHandle, FromRawHandle};
|
||||
use std::path::{Component, Path};
|
||||
use std::os::windows::ffi::{OsStrExt, OsStringExt};
|
||||
use std::os::windows::prelude::AsRawHandle;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
/// Normalizes a path to ensure that the target path is located under the directory provided.
|
||||
pub(crate) fn path_get(
|
||||
dirfd: &File,
|
||||
_dirflags: host::__wasi_lookupflags_t,
|
||||
path: &str,
|
||||
needs_final_component: bool,
|
||||
) -> Result<(File, String)> {
|
||||
if path.contains("\0") {
|
||||
// if contains NUL, return EILSEQ
|
||||
return Err(host::__WASI_EILSEQ);
|
||||
pub(crate) fn path_open_rights(
|
||||
rights_base: host::__wasi_rights_t,
|
||||
rights_inheriting: host::__wasi_rights_t,
|
||||
oflags: host::__wasi_oflags_t,
|
||||
fdflags: host::__wasi_fdflags_t,
|
||||
) -> (host::__wasi_rights_t, host::__wasi_rights_t) {
|
||||
// which rights are needed on the dirfd?
|
||||
let mut needed_base = host::__WASI_RIGHT_PATH_OPEN;
|
||||
let mut needed_inheriting = rights_base | rights_inheriting;
|
||||
|
||||
// convert open flags
|
||||
if oflags & host::__WASI_O_CREAT != 0 {
|
||||
needed_base |= host::__WASI_RIGHT_PATH_CREATE_FILE;
|
||||
} else if oflags & host::__WASI_O_TRUNC != 0 {
|
||||
needed_base |= host::__WASI_RIGHT_PATH_FILESTAT_SET_SIZE;
|
||||
}
|
||||
|
||||
let dirfd = dirfd.try_clone().map_err(|err| {
|
||||
err.raw_os_error()
|
||||
.map_or(host::__WASI_EBADF, errno_from_host)
|
||||
})?;
|
||||
// convert file descriptor flags
|
||||
if fdflags & host::__WASI_FDFLAG_DSYNC != 0
|
||||
|| fdflags & host::__WASI_FDFLAG_RSYNC != 0
|
||||
|| fdflags & host::__WASI_FDFLAG_SYNC != 0
|
||||
{
|
||||
needed_inheriting |= host::__WASI_RIGHT_FD_DATASYNC;
|
||||
needed_inheriting |= host::__WASI_RIGHT_FD_SYNC;
|
||||
}
|
||||
|
||||
// Stack of directory handles. Index 0 always corresponds with the directory provided
|
||||
// to this function. Entering a directory causes a handle to be pushed, while handling
|
||||
// ".." entries causes an entry to be popped. Index 0 cannot be popped, as this would imply
|
||||
// escaping the base directory.
|
||||
let mut dir_stack = vec![dirfd];
|
||||
(needed_base, needed_inheriting)
|
||||
}
|
||||
|
||||
// Stack of paths left to process. This is initially the `path` argument to this function, but
|
||||
// any symlinks we encounter are processed by pushing them on the stack.
|
||||
let mut path_stack = vec![path.to_owned()];
|
||||
pub(crate) fn openat(dirfd: &File, path: &str) -> Result<File> {
|
||||
use std::fs::OpenOptions;
|
||||
use std::os::windows::fs::OpenOptionsExt;
|
||||
use winx::file::Flags;
|
||||
use winx::winerror::WinError;
|
||||
|
||||
loop {
|
||||
match path_stack.pop() {
|
||||
Some(cur_path) => {
|
||||
// dbg!(&cur_path);
|
||||
let ends_with_slash = cur_path.ends_with("/");
|
||||
let mut components = Path::new(&cur_path).components();
|
||||
let head = match components.next() {
|
||||
None => return Err(host::__WASI_ENOENT),
|
||||
Some(p) => p,
|
||||
};
|
||||
let tail = components.as_path();
|
||||
|
||||
if tail.components().next().is_some() {
|
||||
let mut tail = host_impl::path_from_host(tail.as_os_str())?;
|
||||
if ends_with_slash {
|
||||
tail.push_str("/");
|
||||
}
|
||||
path_stack.push(tail);
|
||||
}
|
||||
|
||||
match head {
|
||||
Component::Prefix(_) | Component::RootDir => {
|
||||
// path is absolute!
|
||||
return Err(host::__WASI_ENOTCAPABLE);
|
||||
}
|
||||
Component::CurDir => {
|
||||
// "." so skip
|
||||
continue;
|
||||
}
|
||||
Component::ParentDir => {
|
||||
// ".." so pop a dir
|
||||
let _ = dir_stack.pop().ok_or(host::__WASI_ENOTCAPABLE)?;
|
||||
|
||||
// we're not allowed to pop past the original directory
|
||||
if dir_stack.is_empty() {
|
||||
return Err(host::__WASI_ENOTCAPABLE);
|
||||
}
|
||||
}
|
||||
Component::Normal(head) => {
|
||||
let mut head = host_impl::path_from_host(head)?;
|
||||
if ends_with_slash {
|
||||
// preserve trailing slash
|
||||
head.push_str("/");
|
||||
}
|
||||
// should the component be a directory? it should if there is more path left to process, or
|
||||
// if it has a trailing slash and `needs_final_component` is not set
|
||||
if !path_stack.is_empty() || (ends_with_slash && !needs_final_component) {
|
||||
match winx::file::openat(
|
||||
dir_stack
|
||||
.last()
|
||||
.ok_or(host::__WASI_ENOTCAPABLE)?
|
||||
.as_raw_handle(),
|
||||
head.as_str(),
|
||||
winx::file::AccessRight::FILE_GENERIC_READ,
|
||||
winx::file::CreationDisposition::OPEN_EXISTING,
|
||||
winx::file::FlagsAndAttributes::FILE_FLAG_BACKUP_SEMANTICS,
|
||||
) {
|
||||
Ok(new_dir) => {
|
||||
dir_stack.push(unsafe { File::from_raw_handle(new_dir) });
|
||||
continue;
|
||||
}
|
||||
Err(e) => {
|
||||
return Err(host_impl::errno_from_win(e));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// we're done
|
||||
return Ok((dir_stack.pop().ok_or(host::__WASI_ENOTCAPABLE)?, head));
|
||||
}
|
||||
}
|
||||
let path = concatenate(dirfd, Path::new(path))?;
|
||||
OpenOptions::new()
|
||||
.read(true)
|
||||
.custom_flags(Flags::FILE_FLAG_BACKUP_SEMANTICS.bits())
|
||||
.open(&path)
|
||||
.map_err(|e| match e.raw_os_error() {
|
||||
Some(e) => {
|
||||
log::debug!("openat error={:?}", e);
|
||||
match WinError::from_u32(e as u32) {
|
||||
WinError::ERROR_INVALID_NAME => host::__WASI_ENOTDIR,
|
||||
e => host_impl::errno_from_win(e),
|
||||
}
|
||||
}
|
||||
None => {
|
||||
// no further components to process. means we've hit a case like "." or "a/..", or if the
|
||||
// input path has trailing slashes and `needs_final_component` is not set
|
||||
return Ok((
|
||||
dir_stack.pop().ok_or(host::__WASI_ENOTCAPABLE)?,
|
||||
String::from("."),
|
||||
));
|
||||
log::debug!("Inconvertible OS error: {}", e);
|
||||
host::__WASI_EIO
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn readlinkat(dirfd: &File, s_path: &str) -> Result<String> {
|
||||
use winx::file::get_path_by_handle;
|
||||
use winx::winerror::WinError;
|
||||
|
||||
let path = concatenate(dirfd, Path::new(s_path))?;
|
||||
match path.read_link() {
|
||||
Ok(target_path) => {
|
||||
// since on Windows we are effectively emulating 'at' syscalls
|
||||
// we need to strip the prefix from the absolute path
|
||||
// as otherwise we will error out since WASI is not capable
|
||||
// of dealing with absolute paths
|
||||
let dir_path =
|
||||
get_path_by_handle(dirfd.as_raw_handle()).map_err(host_impl::errno_from_win)?;
|
||||
let dir_path = PathBuf::from(strip_extended_prefix(dir_path));
|
||||
target_path
|
||||
.strip_prefix(dir_path)
|
||||
.map_err(|_| host::__WASI_ENOTCAPABLE)
|
||||
.and_then(|path| path.to_str().map(String::from).ok_or(host::__WASI_EILSEQ))
|
||||
}
|
||||
Err(e) => match e.raw_os_error() {
|
||||
Some(e) => {
|
||||
log::debug!("readlinkat error={:?}", e);
|
||||
match WinError::from_u32(e as u32) {
|
||||
WinError::ERROR_INVALID_NAME => {
|
||||
if s_path.ends_with("/") {
|
||||
// strip "/" and check if exists
|
||||
let path = concatenate(dirfd, Path::new(s_path.trim_end_matches("/")))?;
|
||||
if path.exists() && !path.is_dir() {
|
||||
Err(host::__WASI_ENOTDIR)
|
||||
} else {
|
||||
Err(host::__WASI_ENOENT)
|
||||
}
|
||||
} else {
|
||||
Err(host::__WASI_ENOENT)
|
||||
}
|
||||
}
|
||||
e => Err(host_impl::errno_from_win(e)),
|
||||
}
|
||||
}
|
||||
None => {
|
||||
log::debug!("Inconvertible OS error: {}", e);
|
||||
Err(host::__WASI_EIO)
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn strip_extended_prefix<P: AsRef<OsStr>>(path: P) -> OsString {
|
||||
let path: Vec<u16> = path.as_ref().encode_wide().collect();
|
||||
if &[92, 92, 63, 92] == &path[0..4] {
|
||||
OsString::from_wide(&path[4..])
|
||||
} else {
|
||||
OsString::from_wide(&path)
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn concatenate<P: AsRef<Path>>(dirfd: &File, path: P) -> Result<PathBuf> {
|
||||
use winx::file::get_path_by_handle;
|
||||
|
||||
// WASI is not able to deal with absolute paths
|
||||
// so error out if absolute
|
||||
if path.as_ref().is_absolute() {
|
||||
return Err(host::__WASI_ENOTCAPABLE);
|
||||
}
|
||||
|
||||
let dir_path = get_path_by_handle(dirfd.as_raw_handle()).map_err(host_impl::errno_from_win)?;
|
||||
// concatenate paths
|
||||
let mut out_path = PathBuf::from(dir_path);
|
||||
out_path.push(path.as_ref());
|
||||
// strip extended prefix; otherwise we will error out on any relative
|
||||
// components with `out_path`
|
||||
let out_path = PathBuf::from(strip_extended_prefix(out_path));
|
||||
|
||||
log::debug!("out_path={:?}", out_path);
|
||||
|
||||
Ok(out_path)
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//! Windows-specific hostcalls that implement
|
||||
//! [WASI](https://github.com/CraneStation/wasmtime-wasi/blob/wasi/docs/WASI-overview.md).
|
||||
mod fs;
|
||||
mod fs_helpers;
|
||||
pub(crate) mod fs_helpers;
|
||||
mod misc;
|
||||
|
||||
pub(crate) use self::fs::*;
|
||||
|
||||
Reference in New Issue
Block a user