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:
@@ -1,10 +1,8 @@
|
||||
#![allow(non_camel_case_types)]
|
||||
#![allow(unused_unsafe)]
|
||||
use super::fs_helpers::*;
|
||||
use crate::ctx::WasiCtx;
|
||||
use crate::fdentry::FdEntry;
|
||||
use crate::helpers::systemtime_to_timestamp;
|
||||
use crate::sys::fdentry_impl::determine_type_rights;
|
||||
use crate::hostcalls_impl::PathGet;
|
||||
use crate::sys::host_impl;
|
||||
use crate::sys::{errno_from_host, errno_from_ioerror};
|
||||
use crate::{host, wasm32, Result};
|
||||
@@ -88,38 +86,30 @@ pub(crate) fn fd_advise(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn path_create_directory(dirfd: &File, path: &str) -> Result<()> {
|
||||
pub(crate) fn path_create_directory(resolved: PathGet) -> Result<()> {
|
||||
use nix::libc::mkdirat;
|
||||
|
||||
let (dir, path) = path_get(dirfd, 0, path, false)?;
|
||||
let path_cstr = CString::new(path.as_bytes()).map_err(|_| host::__WASI_EILSEQ)?;
|
||||
|
||||
let path_cstr = CString::new(resolved.path().as_bytes()).map_err(|_| host::__WASI_EILSEQ)?;
|
||||
// nix doesn't expose mkdirat() yet
|
||||
match unsafe { mkdirat(dir.as_raw_fd(), path_cstr.as_ptr(), 0o777) } {
|
||||
match unsafe { mkdirat(resolved.dirfd().as_raw_fd(), path_cstr.as_ptr(), 0o777) } {
|
||||
0 => Ok(()),
|
||||
_ => Err(host_impl::errno_from_nix(nix::errno::Errno::last())),
|
||||
}
|
||||
}
|
||||
|
||||
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<()> {
|
||||
use nix::libc::linkat;
|
||||
let (old_dir, old_path) = path_get(old_dirfd, 0, old_path, false)?;
|
||||
let (new_dir, new_path) = path_get(new_dirfd, 0, new_path, false)?;
|
||||
let old_path_cstr = CString::new(old_path.as_bytes()).map_err(|_| host::__WASI_EILSEQ)?;
|
||||
let new_path_cstr = CString::new(new_path.as_bytes()).map_err(|_| host::__WASI_EILSEQ)?;
|
||||
let old_path_cstr =
|
||||
CString::new(resolved_old.path().as_bytes()).map_err(|_| host::__WASI_EILSEQ)?;
|
||||
let new_path_cstr =
|
||||
CString::new(resolved_new.path().as_bytes()).map_err(|_| host::__WASI_EILSEQ)?;
|
||||
|
||||
// Not setting AT_SYMLINK_FOLLOW fails on most filesystems
|
||||
let atflags = libc::AT_SYMLINK_FOLLOW;
|
||||
let res = unsafe {
|
||||
linkat(
|
||||
old_dir.as_raw_fd(),
|
||||
resolved_old.dirfd().as_raw_fd(),
|
||||
old_path_cstr.as_ptr(),
|
||||
new_dir.as_raw_fd(),
|
||||
resolved_new.dirfd().as_raw_fd(),
|
||||
new_path_cstr.as_ptr(),
|
||||
atflags,
|
||||
)
|
||||
@@ -132,17 +122,12 @@ pub(crate) fn 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,
|
||||
oflags: host::__wasi_oflags_t,
|
||||
fs_flags: host::__wasi_fdflags_t,
|
||||
) -> Result<FdEntry> {
|
||||
) -> Result<File> {
|
||||
use nix::errno::Errno;
|
||||
use nix::fcntl::{openat, AtFlags, OFlag};
|
||||
use nix::sys::stat::{fstatat, Mode, SFlag};
|
||||
@@ -159,34 +144,17 @@ pub(crate) fn path_open(
|
||||
nix_all_oflags.insert(OFlag::O_NOFOLLOW);
|
||||
|
||||
// convert open flags
|
||||
let nix_oflags = host_impl::nix_from_oflags(oflags);
|
||||
nix_all_oflags.insert(nix_oflags);
|
||||
if nix_all_oflags.contains(OFlag::O_CREAT) {
|
||||
needed_base |= host::__WASI_RIGHT_PATH_CREATE_FILE;
|
||||
}
|
||||
if nix_all_oflags.contains(OFlag::O_TRUNC) {
|
||||
needed_base |= host::__WASI_RIGHT_PATH_FILESTAT_SET_SIZE;
|
||||
}
|
||||
nix_all_oflags.insert(host_impl::nix_from_oflags(oflags));
|
||||
|
||||
// convert file descriptor flags
|
||||
nix_all_oflags.insert(host_impl::nix_from_fdflags(fs_flags));
|
||||
if nix_all_oflags.contains(OFlag::O_DSYNC) {
|
||||
needed_inheriting |= host::__WASI_RIGHT_FD_DATASYNC;
|
||||
}
|
||||
if nix_all_oflags.intersects(host_impl::O_RSYNC | OFlag::O_SYNC) {
|
||||
needed_inheriting |= host::__WASI_RIGHT_FD_SYNC;
|
||||
}
|
||||
|
||||
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, nix_oflags.contains(OFlag::O_CREAT))?;
|
||||
|
||||
// Call openat. Use mode 0o666 so that we follow whatever the user's
|
||||
// umask is, but don't set the executable flag, because it isn't yet
|
||||
// meaningful for WASI programs to create executable files.
|
||||
let new_fd = match openat(
|
||||
dir.as_raw_fd(),
|
||||
path.as_str(),
|
||||
resolved.dirfd().as_raw_fd(),
|
||||
resolved.path(),
|
||||
nix_all_oflags,
|
||||
Mode::from_bits_truncate(0o666),
|
||||
) {
|
||||
@@ -195,9 +163,11 @@ pub(crate) fn path_open(
|
||||
match e.as_errno() {
|
||||
// Linux returns ENXIO instead of EOPNOTSUPP when opening a socket
|
||||
Some(Errno::ENXIO) => {
|
||||
if let Ok(stat) =
|
||||
fstatat(dir.as_raw_fd(), path.as_str(), AtFlags::AT_SYMLINK_NOFOLLOW)
|
||||
{
|
||||
if let Ok(stat) = fstatat(
|
||||
resolved.dirfd().as_raw_fd(),
|
||||
resolved.path(),
|
||||
AtFlags::AT_SYMLINK_NOFOLLOW,
|
||||
) {
|
||||
if SFlag::from_bits_truncate(stat.st_mode).contains(SFlag::S_IFSOCK) {
|
||||
return Err(host::__WASI_ENOTSUP);
|
||||
} else {
|
||||
@@ -212,9 +182,11 @@ pub(crate) fn path_open(
|
||||
Some(Errno::ENOTDIR)
|
||||
if !(nix_all_oflags & (OFlag::O_NOFOLLOW | OFlag::O_DIRECTORY)).is_empty() =>
|
||||
{
|
||||
if let Ok(stat) =
|
||||
fstatat(dir.as_raw_fd(), path.as_str(), AtFlags::AT_SYMLINK_NOFOLLOW)
|
||||
{
|
||||
if let Ok(stat) = fstatat(
|
||||
resolved.dirfd().as_raw_fd(),
|
||||
resolved.path(),
|
||||
AtFlags::AT_SYMLINK_NOFOLLOW,
|
||||
) {
|
||||
if SFlag::from_bits_truncate(stat.st_mode).contains(SFlag::S_IFLNK) {
|
||||
return Err(host::__WASI_ELOOP);
|
||||
}
|
||||
@@ -233,14 +205,7 @@ pub(crate) fn path_open(
|
||||
};
|
||||
|
||||
// Determine the type of the new file descriptor and which rights contradict with this type
|
||||
let file = unsafe { File::from_raw_fd(new_fd) };
|
||||
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
|
||||
})
|
||||
})
|
||||
Ok(unsafe { File::from_raw_fd(new_fd) })
|
||||
}
|
||||
|
||||
pub(crate) fn fd_readdir(
|
||||
@@ -297,11 +262,9 @@ pub(crate) fn fd_readdir(
|
||||
Ok(host_buf_len - left)
|
||||
}
|
||||
|
||||
pub(crate) fn path_readlink(dirfd: &File, path: &str, buf: &mut [u8]) -> Result<usize> {
|
||||
pub(crate) fn path_readlink(resolved: PathGet, buf: &mut [u8]) -> Result<usize> {
|
||||
use nix::errno::Errno;
|
||||
|
||||
let (dir, path) = path_get(dirfd, 0, path, false)?;
|
||||
let path_cstr = CString::new(path.as_bytes()).map_err(|_| host::__WASI_EILSEQ)?;
|
||||
let path_cstr = CString::new(resolved.path().as_bytes()).map_err(|_| host::__WASI_EILSEQ)?;
|
||||
|
||||
// Linux requires that the buffer size is positive, whereas POSIX does not.
|
||||
// Use a fake buffer to store the results if the size is zero.
|
||||
@@ -311,7 +274,7 @@ pub(crate) fn path_readlink(dirfd: &File, path: &str, buf: &mut [u8]) -> Result<
|
||||
let buf_len = buf.len();
|
||||
let len = unsafe {
|
||||
libc::readlinkat(
|
||||
dir.as_raw_fd(),
|
||||
resolved.dirfd().as_raw_fd(),
|
||||
path_cstr.as_ptr() as *const libc::c_char,
|
||||
if buf_len == 0 {
|
||||
fakebuf.as_mut_ptr()
|
||||
@@ -330,24 +293,18 @@ pub(crate) fn path_readlink(dirfd: &File, path: &str, buf: &mut [u8]) -> Result<
|
||||
}
|
||||
}
|
||||
|
||||
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<()> {
|
||||
use nix::libc::renameat;
|
||||
|
||||
let (old_dir, old_path) = path_get(old_dirfd, 0, old_path, false)?;
|
||||
let (new_dir, new_path) = path_get(new_dirfd, 0, new_path, false)?;
|
||||
let old_path_cstr = CString::new(old_path.as_bytes()).map_err(|_| host::__WASI_EILSEQ)?;
|
||||
let new_path_cstr = CString::new(new_path.as_bytes()).map_err(|_| host::__WASI_EILSEQ)?;
|
||||
let old_path_cstr =
|
||||
CString::new(resolved_old.path().as_bytes()).map_err(|_| host::__WASI_EILSEQ)?;
|
||||
let new_path_cstr =
|
||||
CString::new(resolved_new.path().as_bytes()).map_err(|_| host::__WASI_EILSEQ)?;
|
||||
|
||||
let res = unsafe {
|
||||
renameat(
|
||||
old_dir.as_raw_fd(),
|
||||
resolved_old.dirfd().as_raw_fd(),
|
||||
old_path_cstr.as_ptr(),
|
||||
new_dir.as_raw_fd(),
|
||||
resolved_new.dirfd().as_raw_fd(),
|
||||
new_path_cstr.as_ptr(),
|
||||
)
|
||||
};
|
||||
@@ -458,35 +415,31 @@ 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> {
|
||||
use nix::fcntl::AtFlags;
|
||||
use nix::sys::stat::fstatat;
|
||||
|
||||
let (dir, path) = path_get(dirfd, dirflags, path, false)?;
|
||||
let atflags = match dirflags {
|
||||
0 => AtFlags::empty(),
|
||||
_ => AtFlags::AT_SYMLINK_NOFOLLOW,
|
||||
};
|
||||
|
||||
let filestat = fstatat(dir.as_raw_fd(), path.as_str(), atflags)
|
||||
let filestat = fstatat(resolved.dirfd().as_raw_fd(), resolved.path(), atflags)
|
||||
.map_err(|err| host_impl::errno_from_nix(err.as_errno().unwrap()))?;
|
||||
host_impl::filestat_from_nix(filestat)
|
||||
}
|
||||
|
||||
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,
|
||||
) -> Result<()> {
|
||||
use nix::sys::time::{TimeSpec, TimeValLike};
|
||||
|
||||
let (dir, path) = path_get(dirfd, dirflags, &path, false)?;
|
||||
let atflags = match dirflags {
|
||||
wasm32::__WASI_LOOKUP_SYMLINK_FOLLOW => 0,
|
||||
_ => libc::AT_SYMLINK_NOFOLLOW,
|
||||
@@ -519,10 +472,16 @@ pub(crate) fn path_filestat_set_times(
|
||||
let ts_mtime = *TimeSpec::nanoseconds(st_mtim as i64).as_ref();
|
||||
let times = [ts_atime, ts_mtime];
|
||||
|
||||
let path_cstr = CString::new(path.as_bytes()).map_err(|_| host::__WASI_EILSEQ)?;
|
||||
let path_cstr = CString::new(resolved.path().as_bytes()).map_err(|_| host::__WASI_EILSEQ)?;
|
||||
|
||||
let res =
|
||||
unsafe { libc::utimensat(dir.as_raw_fd(), path_cstr.as_ptr(), times.as_ptr(), atflags) };
|
||||
let res = unsafe {
|
||||
libc::utimensat(
|
||||
resolved.dirfd().as_raw_fd(),
|
||||
path_cstr.as_ptr(),
|
||||
times.as_ptr(),
|
||||
atflags,
|
||||
)
|
||||
};
|
||||
if res != 0 {
|
||||
Err(host_impl::errno_from_nix(nix::errno::Errno::last()))
|
||||
} else {
|
||||
@@ -530,17 +489,17 @@ pub(crate) fn path_filestat_set_times(
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn path_symlink(dirfd: &File, old_path: &str, new_path: &str) -> Result<()> {
|
||||
pub(crate) fn path_symlink(old_path: &str, resolved: PathGet) -> Result<()> {
|
||||
use nix::libc::symlinkat;
|
||||
|
||||
let (dir, new_path) = path_get(dirfd, 0, new_path, false)?;
|
||||
let old_path_cstr = CString::new(old_path.as_bytes()).map_err(|_| host::__WASI_EILSEQ)?;
|
||||
let new_path_cstr = CString::new(new_path.as_bytes()).map_err(|_| host::__WASI_EILSEQ)?;
|
||||
let new_path_cstr =
|
||||
CString::new(resolved.path().as_bytes()).map_err(|_| host::__WASI_EILSEQ)?;
|
||||
|
||||
let res = unsafe {
|
||||
symlinkat(
|
||||
old_path_cstr.as_ptr(),
|
||||
dir.as_raw_fd(),
|
||||
resolved.dirfd().as_raw_fd(),
|
||||
new_path_cstr.as_ptr(),
|
||||
)
|
||||
};
|
||||
@@ -551,15 +510,14 @@ pub(crate) fn path_symlink(dirfd: &File, old_path: &str, new_path: &str) -> Resu
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn path_unlink_file(dirfd: &File, path: &str) -> Result<()> {
|
||||
pub(crate) fn path_unlink_file(resolved: PathGet) -> Result<()> {
|
||||
use nix::errno;
|
||||
use nix::libc::unlinkat;
|
||||
|
||||
let (dir, path) = path_get(dirfd, 0, path, false)?;
|
||||
let path_cstr = CString::new(path.as_bytes()).map_err(|_| host::__WASI_EILSEQ)?;
|
||||
let path_cstr = CString::new(resolved.path().as_bytes()).map_err(|_| host::__WASI_EILSEQ)?;
|
||||
|
||||
// nix doesn't expose unlinkat() yet
|
||||
match unsafe { unlinkat(dir.as_raw_fd(), path_cstr.as_ptr(), 0) } {
|
||||
match unsafe { unlinkat(resolved.dirfd().as_raw_fd(), path_cstr.as_ptr(), 0) } {
|
||||
0 => Ok(()),
|
||||
_ => {
|
||||
let mut e = errno::Errno::last();
|
||||
@@ -577,9 +535,11 @@ pub(crate) fn path_unlink_file(dirfd: &File, path: &str) -> Result<()> {
|
||||
use nix::sys::stat::{fstatat, SFlag};
|
||||
|
||||
if e == errno::Errno::EPERM {
|
||||
if let Ok(stat) =
|
||||
fstatat(dir.as_raw_fd(), path.as_str(), AtFlags::AT_SYMLINK_NOFOLLOW)
|
||||
{
|
||||
if let Ok(stat) = fstatat(
|
||||
resolved.dirfd().as_raw_fd(),
|
||||
resolved.path(),
|
||||
AtFlags::AT_SYMLINK_NOFOLLOW,
|
||||
) {
|
||||
if SFlag::from_bits_truncate(stat.st_mode).contains(SFlag::S_IFDIR) {
|
||||
e = errno::Errno::EISDIR;
|
||||
}
|
||||
@@ -594,15 +554,20 @@ pub(crate) fn path_unlink_file(dirfd: &File, path: &str) -> Result<()> {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn path_remove_directory(dirfd: &File, path: &str) -> Result<()> {
|
||||
pub(crate) fn path_remove_directory(resolved: PathGet) -> Result<()> {
|
||||
use nix::errno;
|
||||
use nix::libc::{unlinkat, AT_REMOVEDIR};
|
||||
|
||||
let (dir, path) = path_get(dirfd, 0, path, false)?;
|
||||
let path_cstr = CString::new(path.as_bytes()).map_err(|_| host::__WASI_EILSEQ)?;
|
||||
let path_cstr = CString::new(resolved.path().as_bytes()).map_err(|_| host::__WASI_EILSEQ)?;
|
||||
|
||||
// nix doesn't expose unlinkat() yet
|
||||
match unsafe { unlinkat(dir.as_raw_fd(), path_cstr.as_ptr(), AT_REMOVEDIR) } {
|
||||
match unsafe {
|
||||
unlinkat(
|
||||
resolved.dirfd().as_raw_fd(),
|
||||
path_cstr.as_ptr(),
|
||||
AT_REMOVEDIR,
|
||||
)
|
||||
} {
|
||||
0 => Ok(()),
|
||||
_ => Err(host_impl::errno_from_nix(errno::Errno::last())),
|
||||
}
|
||||
|
||||
@@ -1,183 +1,44 @@
|
||||
#![allow(non_camel_case_types)]
|
||||
#![allow(unused_unsafe)]
|
||||
|
||||
use crate::sys::errno_from_host;
|
||||
use crate::sys::host_impl;
|
||||
use crate::{host, Result};
|
||||
use nix::libc::{self, c_long};
|
||||
use std::fs::File;
|
||||
use std::path::{Component, Path};
|
||||
|
||||
/// Normalizes a path to ensure that the target path is located under the directory provided.
|
||||
///
|
||||
/// This is a workaround for not having Capsicum support in the OS.
|
||||
pub(crate) fn path_get(
|
||||
dirfd: &File,
|
||||
dirflags: host::__wasi_lookupflags_t,
|
||||
path: &str,
|
||||
needs_final_component: bool,
|
||||
) -> Result<(File, String)> {
|
||||
const MAX_SYMLINK_EXPANSIONS: usize = 128;
|
||||
pub(crate) fn path_open_rights(
|
||||
rights_base: host::__wasi_rights_t,
|
||||
rights_inheriting: host::__wasi_rights_t,
|
||||
oflags: host::__wasi_oflags_t,
|
||||
fs_flags: host::__wasi_fdflags_t,
|
||||
) -> (host::__wasi_rights_t, host::__wasi_rights_t) {
|
||||
use nix::fcntl::OFlag;
|
||||
|
||||
if path.contains("\0") {
|
||||
// if contains NUL, return EILSEQ
|
||||
return Err(host::__WASI_EILSEQ);
|
||||
// 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
|
||||
let oflags = host_impl::nix_from_oflags(oflags);
|
||||
if oflags.contains(OFlag::O_CREAT) {
|
||||
needed_base |= host::__WASI_RIGHT_PATH_CREATE_FILE;
|
||||
}
|
||||
if oflags.contains(OFlag::O_TRUNC) {
|
||||
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)
|
||||
})?;
|
||||
|
||||
// Stack of directory file descriptors. Index 0 always corresponds with the directory provided
|
||||
// to this function. Entering a directory causes a file descriptor 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];
|
||||
|
||||
// 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()];
|
||||
|
||||
// Track the number of symlinks we've expanded, so we can return `ELOOP` after too many.
|
||||
let mut symlink_expansions = 0;
|
||||
|
||||
// TODO: rewrite this using a custom posix path type, with a component iterator that respects
|
||||
// trailing slashes. This version does way too much allocation, and is way too fiddly.
|
||||
loop {
|
||||
match path_stack.pop() {
|
||||
Some(cur_path) => {
|
||||
// eprintln!("cur_path = {:?}", 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("/");
|
||||
}
|
||||
|
||||
if !path_stack.is_empty() || (ends_with_slash && !needs_final_component) {
|
||||
match openat(dir_stack.last().ok_or(host::__WASI_ENOTCAPABLE)?, &head) {
|
||||
Ok(new_dir) => {
|
||||
dir_stack.push(new_dir);
|
||||
continue;
|
||||
}
|
||||
Err(e)
|
||||
if e == host::__WASI_ELOOP
|
||||
|| e == host::__WASI_EMLINK
|
||||
|| e == host::__WASI_ENOTDIR =>
|
||||
// Check to see if it was a symlink. Linux indicates
|
||||
// this with ENOTDIR because of the O_DIRECTORY flag.
|
||||
{
|
||||
// attempt symlink expansion
|
||||
match readlinkat(
|
||||
dir_stack.last().ok_or(host::__WASI_ENOTCAPABLE)?,
|
||||
&head,
|
||||
) {
|
||||
Ok(mut link_path) => {
|
||||
symlink_expansions += 1;
|
||||
if symlink_expansions > MAX_SYMLINK_EXPANSIONS {
|
||||
return Err(host::__WASI_ELOOP);
|
||||
}
|
||||
|
||||
if head.ends_with("/") {
|
||||
link_path.push_str("/");
|
||||
}
|
||||
|
||||
path_stack.push(link_path);
|
||||
continue;
|
||||
}
|
||||
Err(e) => {
|
||||
return Err(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
return Err(e);
|
||||
}
|
||||
}
|
||||
} else if ends_with_slash
|
||||
|| (dirflags & host::__WASI_LOOKUP_SYMLINK_FOLLOW) != 0
|
||||
{
|
||||
// if there's a trailing slash, or if `LOOKUP_SYMLINK_FOLLOW` is set, attempt
|
||||
// symlink expansion
|
||||
match readlinkat(
|
||||
dir_stack.last().ok_or(host::__WASI_ENOTCAPABLE)?,
|
||||
&head,
|
||||
) {
|
||||
Ok(mut link_path) => {
|
||||
symlink_expansions += 1;
|
||||
if symlink_expansions > MAX_SYMLINK_EXPANSIONS {
|
||||
return Err(host::__WASI_ELOOP);
|
||||
}
|
||||
|
||||
if head.ends_with("/") {
|
||||
link_path.push_str("/");
|
||||
}
|
||||
|
||||
path_stack.push(link_path);
|
||||
continue;
|
||||
}
|
||||
Err(e) => {
|
||||
if e != host::__WASI_EINVAL && e != host::__WASI_ENOENT {
|
||||
return Err(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// not a symlink, so we're done;
|
||||
return Ok((dir_stack.pop().ok_or(host::__WASI_ENOTCAPABLE)?, head));
|
||||
}
|
||||
}
|
||||
}
|
||||
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("."),
|
||||
));
|
||||
}
|
||||
}
|
||||
// convert file descriptor flags
|
||||
let fdflags = host_impl::nix_from_fdflags(fs_flags);
|
||||
if fdflags.contains(OFlag::O_DSYNC) {
|
||||
needed_inheriting |= host::__WASI_RIGHT_FD_DATASYNC;
|
||||
}
|
||||
if fdflags.intersects(host_impl::O_RSYNC | OFlag::O_SYNC) {
|
||||
needed_inheriting |= host::__WASI_RIGHT_FD_SYNC;
|
||||
}
|
||||
|
||||
(needed_base, needed_inheriting)
|
||||
}
|
||||
|
||||
fn openat(dirfd: &File, path: &str) -> Result<File> {
|
||||
pub(crate) fn openat(dirfd: &File, path: &str) -> Result<File> {
|
||||
use nix::fcntl::{self, OFlag};
|
||||
use nix::sys::stat::Mode;
|
||||
use std::os::unix::prelude::{AsRawFd, FromRawFd};
|
||||
@@ -192,7 +53,7 @@ fn openat(dirfd: &File, path: &str) -> Result<File> {
|
||||
.map_err(|e| host_impl::errno_from_nix(e.as_errno().unwrap()))
|
||||
}
|
||||
|
||||
fn readlinkat(dirfd: &File, path: &str) -> Result<String> {
|
||||
pub(crate) fn readlinkat(dirfd: &File, path: &str) -> Result<String> {
|
||||
use nix::fcntl;
|
||||
use std::os::unix::prelude::AsRawFd;
|
||||
|
||||
@@ -204,21 +65,21 @@ fn readlinkat(dirfd: &File, path: &str) -> Result<String> {
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
pub fn utime_now() -> c_long {
|
||||
pub(crate) fn utime_now() -> c_long {
|
||||
libc::UTIME_NOW
|
||||
}
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
pub fn utime_now() -> c_long {
|
||||
pub(crate) fn utime_now() -> c_long {
|
||||
-1
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
pub fn utime_omit() -> c_long {
|
||||
pub(crate) fn utime_omit() -> c_long {
|
||||
libc::UTIME_OMIT
|
||||
}
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
pub fn utime_omit() -> c_long {
|
||||
pub(crate) fn utime_omit() -> c_long {
|
||||
-2
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//! Unix-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