WASI paths as &str and String (#37)
* Check if RawString operates on valid encodings * Use &str and String for WASI paths
This commit is contained in:
@@ -2,11 +2,8 @@
|
||||
#![allow(non_camel_case_types)]
|
||||
#![allow(non_snake_case)]
|
||||
#![allow(dead_code)]
|
||||
use crate::host;
|
||||
use crate::memory;
|
||||
use crate::wasm32;
|
||||
|
||||
use std::ffi::{OsStr, OsString};
|
||||
use crate::{host, memory, wasm32};
|
||||
use std::ffi::OsStr;
|
||||
use std::os::unix::prelude::OsStrExt;
|
||||
|
||||
pub fn errno_from_nix(errno: nix::errno::Errno) -> host::__wasi_errno_t {
|
||||
@@ -252,52 +249,10 @@ pub fn dirent_from_host(
|
||||
Ok(entry)
|
||||
}
|
||||
|
||||
/// `RawString` wraps `OsString` with Unix specific extensions
|
||||
/// enabling a common interface between different hosts for
|
||||
/// WASI raw string manipulation.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct RawString {
|
||||
s: OsString,
|
||||
}
|
||||
|
||||
impl RawString {
|
||||
pub fn new(s: OsString) -> Self {
|
||||
Self { s }
|
||||
}
|
||||
|
||||
pub fn from_bytes(slice: &[u8]) -> Self {
|
||||
Self {
|
||||
s: OsStr::from_bytes(slice).to_owned(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_bytes(&self) -> Vec<u8> {
|
||||
self.s.as_bytes().to_vec()
|
||||
}
|
||||
|
||||
pub fn contains(&self, c: &u8) -> bool {
|
||||
self.s.as_bytes().contains(c)
|
||||
}
|
||||
|
||||
pub fn ends_with(&self, c: &[u8]) -> bool {
|
||||
self.s.as_bytes().ends_with(c)
|
||||
}
|
||||
|
||||
pub fn push<T: AsRef<OsStr>>(&mut self, s: T) {
|
||||
self.s.push(s)
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<OsStr> for RawString {
|
||||
fn as_ref(&self) -> &OsStr {
|
||||
&self.s
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&OsStr> for RawString {
|
||||
fn from(os_str: &OsStr) -> Self {
|
||||
Self {
|
||||
s: os_str.to_owned(),
|
||||
}
|
||||
}
|
||||
/// Creates owned WASI path from OS string.
|
||||
///
|
||||
/// NB WASI spec requires OS string to be valid UTF-8. Otherwise,
|
||||
/// `__WASI_EILSEQ` error is returned.
|
||||
pub fn path_from_host<S: AsRef<OsStr>>(s: S) -> Result<String, host::__wasi_errno_t> {
|
||||
host::path_from_slice(s.as_ref().as_bytes()).map(String::from)
|
||||
}
|
||||
|
||||
@@ -1,15 +1,14 @@
|
||||
#![allow(non_camel_case_types)]
|
||||
#![allow(unused_unsafe)]
|
||||
use super::fs_helpers::*;
|
||||
use crate::ctx::WasiCtx;
|
||||
use crate::fdentry::FdEntry;
|
||||
use crate::sys::errno_from_host;
|
||||
use crate::sys::fdentry_impl::determine_type_rights;
|
||||
use crate::sys::host_impl::{self, RawString};
|
||||
|
||||
use crate::ctx::WasiCtx;
|
||||
use crate::sys::host_impl;
|
||||
use crate::{host, wasm32};
|
||||
|
||||
use nix::libc::{self, c_long, c_void, off_t};
|
||||
use std::ffi::CString;
|
||||
use std::fs::File;
|
||||
use std::os::unix::fs::FileExt;
|
||||
use std::os::unix::prelude::{AsRawFd, FromRawFd};
|
||||
@@ -164,7 +163,7 @@ pub(crate) fn fd_advise(
|
||||
pub(crate) fn path_create_directory(
|
||||
ctx: &WasiCtx,
|
||||
dirfd: host::__wasi_fd_t,
|
||||
path: &RawString,
|
||||
path: &str,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
use nix::libc::mkdirat;
|
||||
|
||||
@@ -180,10 +179,8 @@ pub(crate) fn path_create_directory(
|
||||
Ok((dir, path)) => (dir, path),
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
let path_cstr = match std::ffi::CString::new(path.to_bytes()) {
|
||||
Ok(path_cstr) => path_cstr,
|
||||
Err(_) => return Err(host::__WASI_EINVAL),
|
||||
};
|
||||
let path_cstr = CString::new(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) } {
|
||||
0 => Ok(()),
|
||||
@@ -195,8 +192,8 @@ pub(crate) fn path_link(
|
||||
ctx: &WasiCtx,
|
||||
old_dirfd: host::__wasi_fd_t,
|
||||
new_dirfd: host::__wasi_fd_t,
|
||||
old_path: &RawString,
|
||||
new_path: &RawString,
|
||||
old_path: &str,
|
||||
new_path: &str,
|
||||
source_rights: host::__wasi_rights_t,
|
||||
target_rights: host::__wasi_rights_t,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
@@ -209,14 +206,8 @@ pub(crate) fn path_link(
|
||||
Ok((dir, path)) => (dir, path),
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
let old_path_cstr = match std::ffi::CString::new(old_path.to_bytes()) {
|
||||
Ok(old_path_cstr) => old_path_cstr,
|
||||
Err(_) => return Err(host::__WASI_EINVAL),
|
||||
};
|
||||
let new_path_cstr = match std::ffi::CString::new(new_path.to_bytes()) {
|
||||
Ok(new_path_cstr) => new_path_cstr,
|
||||
Err(_) => return Err(host::__WASI_EINVAL),
|
||||
};
|
||||
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)?;
|
||||
|
||||
// Not setting AT_SYMLINK_FOLLOW fails on most filesystems
|
||||
let atflags = libc::AT_SYMLINK_FOLLOW;
|
||||
@@ -240,7 +231,7 @@ pub(crate) fn path_open(
|
||||
ctx: &WasiCtx,
|
||||
dirfd: host::__wasi_fd_t,
|
||||
dirflags: host::__wasi_lookupflags_t,
|
||||
path: &RawString,
|
||||
path: &str,
|
||||
oflags: host::__wasi_oflags_t,
|
||||
read: bool,
|
||||
write: bool,
|
||||
@@ -300,7 +291,7 @@ pub(crate) fn path_open(
|
||||
// meaningful for WASI programs to create executable files.
|
||||
let new_fd = match openat(
|
||||
dir.as_raw_fd(),
|
||||
path.as_ref(),
|
||||
path.as_str(),
|
||||
nix_all_oflags,
|
||||
Mode::from_bits_truncate(0o666),
|
||||
) {
|
||||
@@ -310,7 +301,7 @@ pub(crate) fn path_open(
|
||||
// Linux returns ENXIO instead of EOPNOTSUPP when opening a socket
|
||||
Some(Errno::ENXIO) => {
|
||||
if let Ok(stat) =
|
||||
fstatat(dir.as_raw_fd(), path.as_ref(), AtFlags::AT_SYMLINK_NOFOLLOW)
|
||||
fstatat(dir.as_raw_fd(), path.as_str(), AtFlags::AT_SYMLINK_NOFOLLOW)
|
||||
{
|
||||
if SFlag::from_bits_truncate(stat.st_mode).contains(SFlag::S_IFSOCK) {
|
||||
return Err(host::__WASI_ENOTSUP);
|
||||
@@ -327,7 +318,7 @@ pub(crate) fn path_open(
|
||||
if !(nix_all_oflags & (OFlag::O_NOFOLLOW | OFlag::O_DIRECTORY)).is_empty() =>
|
||||
{
|
||||
if let Ok(stat) =
|
||||
fstatat(dir.as_raw_fd(), path.as_ref(), AtFlags::AT_SYMLINK_NOFOLLOW)
|
||||
fstatat(dir.as_raw_fd(), path.as_str(), AtFlags::AT_SYMLINK_NOFOLLOW)
|
||||
{
|
||||
if SFlag::from_bits_truncate(stat.st_mode).contains(SFlag::S_IFLNK) {
|
||||
return Err(host::__WASI_ELOOP);
|
||||
@@ -421,7 +412,7 @@ pub(crate) fn fd_readdir(
|
||||
pub(crate) fn path_readlink(
|
||||
wasi_ctx: &WasiCtx,
|
||||
dirfd: host::__wasi_fd_t,
|
||||
path: &RawString,
|
||||
path: &str,
|
||||
rights: host::__wasi_rights_t,
|
||||
buf: &mut [u8],
|
||||
) -> Result<usize, host::__wasi_errno_t> {
|
||||
@@ -431,11 +422,7 @@ pub(crate) fn path_readlink(
|
||||
Ok((dir, path)) => (dir, path),
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
|
||||
let path_cstr = match std::ffi::CString::new(path.to_bytes()) {
|
||||
Ok(path_cstr) => path_cstr,
|
||||
Err(_) => return Err(host::__WASI_EINVAL),
|
||||
};
|
||||
let path_cstr = CString::new(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.
|
||||
@@ -467,10 +454,10 @@ pub(crate) fn path_readlink(
|
||||
pub(crate) fn path_rename(
|
||||
wasi_ctx: &WasiCtx,
|
||||
old_dirfd: host::__wasi_fd_t,
|
||||
old_path: &RawString,
|
||||
old_path: &str,
|
||||
old_rights: host::__wasi_rights_t,
|
||||
new_dirfd: host::__wasi_fd_t,
|
||||
new_path: &RawString,
|
||||
new_path: &str,
|
||||
new_rights: host::__wasi_rights_t,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
use nix::libc::renameat;
|
||||
@@ -485,14 +472,9 @@ pub(crate) fn path_rename(
|
||||
Ok((dir, path)) => (dir, path),
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
let old_path_cstr = match std::ffi::CString::new(old_path.to_bytes()) {
|
||||
Ok(old_path_cstr) => old_path_cstr,
|
||||
Err(_) => return Err(host::__WASI_EINVAL),
|
||||
};
|
||||
let new_path_cstr = match std::ffi::CString::new(new_path.to_bytes()) {
|
||||
Ok(new_path_cstr) => new_path_cstr,
|
||||
Err(_) => return Err(host::__WASI_EINVAL),
|
||||
};
|
||||
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 res = unsafe {
|
||||
renameat(
|
||||
old_dir.as_raw_fd(),
|
||||
@@ -582,7 +564,7 @@ pub(crate) fn path_filestat_get(
|
||||
wasi_ctx: &WasiCtx,
|
||||
dirfd: host::__wasi_fd_t,
|
||||
dirflags: host::__wasi_lookupflags_t,
|
||||
path: &RawString,
|
||||
path: &str,
|
||||
) -> Result<host::__wasi_filestat_t, host::__wasi_errno_t> {
|
||||
use nix::fcntl::AtFlags;
|
||||
use nix::sys::stat::fstatat;
|
||||
@@ -604,7 +586,7 @@ pub(crate) fn path_filestat_get(
|
||||
_ => AtFlags::AT_SYMLINK_NOFOLLOW,
|
||||
};
|
||||
|
||||
match fstatat(dir.as_raw_fd(), path.as_ref(), atflags) {
|
||||
match fstatat(dir.as_raw_fd(), path.as_str(), atflags) {
|
||||
Err(e) => Err(host_impl::errno_from_nix(e.as_errno().unwrap())),
|
||||
Ok(filestat) => Ok(host_impl::filestat_from_nix(filestat)?),
|
||||
}
|
||||
@@ -614,7 +596,7 @@ pub(crate) fn path_filestat_set_times(
|
||||
wasi_ctx: &WasiCtx,
|
||||
dirfd: host::__wasi_fd_t,
|
||||
dirflags: host::__wasi_lookupflags_t,
|
||||
path: &RawString,
|
||||
path: &str,
|
||||
rights: host::__wasi_rights_t,
|
||||
st_atim: host::__wasi_timestamp_t,
|
||||
mut st_mtim: host::__wasi_timestamp_t,
|
||||
@@ -622,7 +604,7 @@ pub(crate) fn path_filestat_set_times(
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
use nix::sys::time::{TimeSpec, TimeValLike};
|
||||
|
||||
let (dir, path) = match path_get(wasi_ctx, dirfd, dirflags, path, rights, 0, false) {
|
||||
let (dir, path) = match path_get(wasi_ctx, dirfd, dirflags, &path, rights, 0, false) {
|
||||
Ok((dir, path)) => (dir, path),
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
@@ -661,10 +643,9 @@ 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 = match std::ffi::CString::new(path.to_bytes()) {
|
||||
Ok(path_cstr) => path_cstr,
|
||||
Err(_) => return Err(host::__WASI_EINVAL),
|
||||
};
|
||||
|
||||
let path_cstr = CString::new(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) };
|
||||
if res != 0 {
|
||||
@@ -678,8 +659,8 @@ pub(crate) fn path_symlink(
|
||||
wasi_ctx: &WasiCtx,
|
||||
dirfd: host::__wasi_fd_t,
|
||||
rights: host::__wasi_rights_t,
|
||||
old_path: &RawString,
|
||||
new_path: &RawString,
|
||||
old_path: &str,
|
||||
new_path: &str,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
use nix::libc::symlinkat;
|
||||
|
||||
@@ -687,14 +668,9 @@ pub(crate) fn path_symlink(
|
||||
Ok((dir, path)) => (dir, path),
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
let old_path_cstr = match std::ffi::CString::new(old_path.to_bytes()) {
|
||||
Ok(old_path_cstr) => old_path_cstr,
|
||||
Err(_) => return Err(host::__WASI_EINVAL),
|
||||
};
|
||||
let new_path_cstr = match std::ffi::CString::new(new_path.to_bytes()) {
|
||||
Ok(new_path_cstr) => new_path_cstr,
|
||||
Err(_) => return Err(host::__WASI_EINVAL),
|
||||
};
|
||||
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 res = unsafe {
|
||||
symlinkat(
|
||||
old_path_cstr.as_ptr(),
|
||||
@@ -712,7 +688,7 @@ pub(crate) fn path_symlink(
|
||||
pub(crate) fn path_unlink_file(
|
||||
wasi_ctx: &WasiCtx,
|
||||
dirfd: host::__wasi_fd_t,
|
||||
path: &RawString,
|
||||
path: &str,
|
||||
rights: host::__wasi_rights_t,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
use nix::errno;
|
||||
@@ -722,10 +698,8 @@ pub(crate) fn path_unlink_file(
|
||||
Ok((dir, path)) => (dir, path),
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
let path_cstr = match std::ffi::CString::new(path.to_bytes()) {
|
||||
Ok(path_cstr) => path_cstr,
|
||||
Err(_) => return Err(host::__WASI_EINVAL),
|
||||
};
|
||||
let path_cstr = CString::new(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) } {
|
||||
0 => Ok(()),
|
||||
@@ -746,7 +720,7 @@ pub(crate) fn path_unlink_file(
|
||||
|
||||
if e == errno::Errno::EPERM {
|
||||
if let Ok(stat) =
|
||||
fstatat(dir.as_raw_fd(), path.as_ref(), AtFlags::AT_SYMLINK_NOFOLLOW)
|
||||
fstatat(dir.as_raw_fd(), path.as_str(), AtFlags::AT_SYMLINK_NOFOLLOW)
|
||||
{
|
||||
if SFlag::from_bits_truncate(stat.st_mode).contains(SFlag::S_IFDIR) {
|
||||
e = errno::Errno::EISDIR;
|
||||
@@ -765,7 +739,7 @@ pub(crate) fn path_unlink_file(
|
||||
pub(crate) fn path_remove_directory(
|
||||
wasi_ctx: &WasiCtx,
|
||||
dirfd: host::__wasi_fd_t,
|
||||
path: &RawString,
|
||||
path: &str,
|
||||
rights: host::__wasi_rights_t,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
use nix::errno;
|
||||
@@ -775,10 +749,8 @@ pub(crate) fn path_remove_directory(
|
||||
Ok((dir, path)) => (dir, path),
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
let path_cstr = match std::ffi::CString::new(path.to_bytes()) {
|
||||
Ok(path_cstr) => path_cstr,
|
||||
Err(_) => return Err(host::__WASI_EINVAL),
|
||||
};
|
||||
let path_cstr = CString::new(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) } {
|
||||
0 => Ok(()),
|
||||
|
||||
@@ -5,28 +5,26 @@ use crate::ctx::WasiCtx;
|
||||
use crate::fdentry::Descriptor;
|
||||
use crate::host;
|
||||
use crate::sys::errno_from_host;
|
||||
use crate::sys::host_impl::{self, RawString};
|
||||
|
||||
use crate::sys::host_impl;
|
||||
use nix::libc::{self, c_long};
|
||||
use std::ffi::OsStr;
|
||||
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 fn path_get(
|
||||
pub(crate) fn path_get(
|
||||
wasi_ctx: &WasiCtx,
|
||||
dirfd: host::__wasi_fd_t,
|
||||
dirflags: host::__wasi_lookupflags_t,
|
||||
path: &RawString,
|
||||
path: &str,
|
||||
needed_base: host::__wasi_rights_t,
|
||||
needed_inheriting: host::__wasi_rights_t,
|
||||
needs_final_component: bool,
|
||||
) -> Result<(File, RawString), host::__wasi_errno_t> {
|
||||
) -> Result<(File, String), host::__wasi_errno_t> {
|
||||
const MAX_SYMLINK_EXPANSIONS: usize = 128;
|
||||
|
||||
if path.contains(&b'\0') {
|
||||
if path.contains("\0") {
|
||||
// if contains NUL, return EILSEQ
|
||||
return Err(host::__WASI_EILSEQ);
|
||||
}
|
||||
@@ -48,7 +46,7 @@ pub fn path_get(
|
||||
|
||||
// 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.clone()];
|
||||
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;
|
||||
@@ -60,7 +58,7 @@ pub fn path_get(
|
||||
Some(cur_path) => {
|
||||
// eprintln!("cur_path = {:?}", cur_path);
|
||||
|
||||
let ends_with_slash = cur_path.ends_with(b"/");
|
||||
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),
|
||||
@@ -69,9 +67,9 @@ pub fn path_get(
|
||||
let tail = components.as_path();
|
||||
|
||||
if tail.components().next().is_some() {
|
||||
let mut tail = RawString::from(tail.as_os_str());
|
||||
let mut tail = host_impl::path_from_host(tail.as_os_str())?;
|
||||
if ends_with_slash {
|
||||
tail.push("/");
|
||||
tail.push_str("/");
|
||||
}
|
||||
path_stack.push(tail);
|
||||
}
|
||||
@@ -95,10 +93,10 @@ pub fn path_get(
|
||||
}
|
||||
}
|
||||
Component::Normal(head) => {
|
||||
let mut head = RawString::from(head);
|
||||
let mut head = host_impl::path_from_host(head)?;
|
||||
if ends_with_slash {
|
||||
// preserve trailing slash
|
||||
head.push("/");
|
||||
head.push_str("/");
|
||||
}
|
||||
|
||||
if !path_stack.is_empty() || (ends_with_slash && !needs_final_component) {
|
||||
@@ -125,8 +123,8 @@ pub fn path_get(
|
||||
return Err(host::__WASI_ELOOP);
|
||||
}
|
||||
|
||||
if head.ends_with(b"/") {
|
||||
link_path.push("/");
|
||||
if head.ends_with("/") {
|
||||
link_path.push_str("/");
|
||||
}
|
||||
|
||||
path_stack.push(link_path);
|
||||
@@ -156,8 +154,8 @@ pub fn path_get(
|
||||
return Err(host::__WASI_ELOOP);
|
||||
}
|
||||
|
||||
if head.ends_with(b"/") {
|
||||
link_path.push("/");
|
||||
if head.ends_with("/") {
|
||||
link_path.push_str("/");
|
||||
}
|
||||
|
||||
path_stack.push(link_path);
|
||||
@@ -181,21 +179,21 @@ pub fn path_get(
|
||||
// input path has trailing slashes and `needs_final_component` is not set
|
||||
return Ok((
|
||||
dir_stack.pop().ok_or(host::__WASI_ENOTCAPABLE)?,
|
||||
RawString::from(OsStr::new(".")),
|
||||
String::from("."),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn openat(dirfd: &File, path: &RawString) -> Result<File, host::__wasi_errno_t> {
|
||||
fn openat(dirfd: &File, path: &str) -> Result<File, host::__wasi_errno_t> {
|
||||
use nix::fcntl::{self, OFlag};
|
||||
use nix::sys::stat::Mode;
|
||||
use std::os::unix::prelude::{AsRawFd, FromRawFd};
|
||||
|
||||
fcntl::openat(
|
||||
dirfd.as_raw_fd(),
|
||||
path.as_ref(),
|
||||
path,
|
||||
OFlag::O_RDONLY | OFlag::O_DIRECTORY | OFlag::O_NOFOLLOW,
|
||||
Mode::empty(),
|
||||
)
|
||||
@@ -203,15 +201,15 @@ fn openat(dirfd: &File, path: &RawString) -> Result<File, host::__wasi_errno_t>
|
||||
.map_err(|e| host_impl::errno_from_nix(e.as_errno().unwrap()))
|
||||
}
|
||||
|
||||
fn readlinkat(dirfd: &File, path: &RawString) -> Result<RawString, host::__wasi_errno_t> {
|
||||
fn readlinkat(dirfd: &File, path: &str) -> Result<String, host::__wasi_errno_t> {
|
||||
use nix::fcntl;
|
||||
use std::os::unix::prelude::AsRawFd;
|
||||
|
||||
let readlink_buf = &mut [0u8; libc::PATH_MAX as usize + 1];
|
||||
|
||||
fcntl::readlinkat(dirfd.as_raw_fd(), path.as_ref(), readlink_buf)
|
||||
.map(RawString::from)
|
||||
fcntl::readlinkat(dirfd.as_raw_fd(), path, readlink_buf)
|
||||
.map_err(|e| host_impl::errno_from_nix(e.as_errno().unwrap()))
|
||||
.and_then(host_impl::path_from_host)
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
|
||||
@@ -3,11 +3,8 @@
|
||||
#![allow(non_snake_case)]
|
||||
#![allow(unused)]
|
||||
use crate::host;
|
||||
|
||||
use std::ffi::{OsStr, OsString};
|
||||
use std::marker::PhantomData;
|
||||
use std::os::windows::prelude::{OsStrExt, OsStringExt};
|
||||
use std::slice;
|
||||
use std::ffi::OsStr;
|
||||
use std::os::windows::ffi::OsStrExt;
|
||||
|
||||
pub fn errno_from_win(error: winx::winerror::WinError) -> host::__wasi_errno_t {
|
||||
// TODO: implement error mapping between Windows and WASI
|
||||
@@ -107,61 +104,11 @@ pub fn win_from_oflags(
|
||||
(win_disp, win_flags_attrs)
|
||||
}
|
||||
|
||||
/// `RawString` wraps `OsString` with Windows specific extensions
|
||||
/// enabling a common interface between different hosts for
|
||||
/// WASI raw string manipulation.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct RawString {
|
||||
s: OsString,
|
||||
}
|
||||
|
||||
impl RawString {
|
||||
pub fn new(s: OsString) -> Self {
|
||||
Self { s }
|
||||
}
|
||||
|
||||
pub fn from_bytes(slice: &[u8]) -> Self {
|
||||
Self {
|
||||
s: OsString::from_wide(&slice.iter().map(|&x| x as u16).collect::<Vec<u16>>()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_bytes(&self) -> Vec<u8> {
|
||||
self.s
|
||||
.encode_wide()
|
||||
.map(u16::to_le_bytes)
|
||||
.fold(Vec::new(), |mut acc, bytes| {
|
||||
acc.extend_from_slice(&bytes);
|
||||
acc
|
||||
})
|
||||
}
|
||||
|
||||
pub fn contains(&self, c: &u8) -> bool {
|
||||
let c = u16::from_le_bytes([*c, 0u8]);
|
||||
self.s.encode_wide().find(|&x| x == c).is_some()
|
||||
}
|
||||
|
||||
pub fn ends_with(&self, cs: &[u8]) -> bool {
|
||||
let cs = cs.iter().map(|c| u16::from_le_bytes([*c, 0u8])).rev();
|
||||
let ss: Vec<u16> = self.s.encode_wide().collect();
|
||||
ss.into_iter().rev().zip(cs).all(|(l, r)| l == r)
|
||||
}
|
||||
|
||||
pub fn push<T: AsRef<OsStr>>(&mut self, s: T) {
|
||||
self.s.push(s)
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<OsStr> for RawString {
|
||||
fn as_ref(&self) -> &OsStr {
|
||||
&self.s
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&OsStr> for RawString {
|
||||
fn from(os_str: &OsStr) -> Self {
|
||||
Self {
|
||||
s: os_str.to_owned(),
|
||||
}
|
||||
}
|
||||
/// Creates owned WASI path from OS string.
|
||||
///
|
||||
/// NB WASI spec requires OS string to be valid UTF-8. Otherwise,
|
||||
/// `__WASI_EILSEQ` error is returned.
|
||||
pub fn path_from_host<S: AsRef<OsStr>>(s: S) -> Result<String, host::__wasi_errno_t> {
|
||||
let vec: Vec<u16> = s.as_ref().encode_wide().collect();
|
||||
String::from_utf16(&vec).map_err(|_| host::__WASI_EILSEQ)
|
||||
}
|
||||
|
||||
@@ -6,8 +6,7 @@ use crate::fdentry::FdEntry;
|
||||
use crate::host;
|
||||
use crate::sys::errno_from_host;
|
||||
use crate::sys::fdentry_impl::determine_type_rights;
|
||||
use crate::sys::host_impl::{self, RawString};
|
||||
|
||||
use crate::sys::host_impl;
|
||||
use std::fs::File;
|
||||
use std::io::{self, Seek, SeekFrom};
|
||||
use std::os::windows::fs::FileExt;
|
||||
@@ -102,7 +101,7 @@ pub(crate) fn fd_advise(
|
||||
pub(crate) fn path_create_directory(
|
||||
ctx: &WasiCtx,
|
||||
dirfd: host::__wasi_fd_t,
|
||||
path: &RawString,
|
||||
path: &str,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
unimplemented!("path_create_directory")
|
||||
}
|
||||
@@ -111,8 +110,8 @@ pub(crate) fn path_link(
|
||||
ctx: &WasiCtx,
|
||||
old_dirfd: host::__wasi_fd_t,
|
||||
new_dirfd: host::__wasi_fd_t,
|
||||
old_path: &RawString,
|
||||
new_path: &RawString,
|
||||
old_path: &str,
|
||||
new_path: &str,
|
||||
source_rights: host::__wasi_rights_t,
|
||||
target_rights: host::__wasi_rights_t,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
@@ -123,7 +122,7 @@ pub(crate) fn path_open(
|
||||
ctx: &WasiCtx,
|
||||
dirfd: host::__wasi_fd_t,
|
||||
dirflags: host::__wasi_lookupflags_t,
|
||||
path: &RawString,
|
||||
path: &str,
|
||||
oflags: host::__wasi_oflags_t,
|
||||
read: bool,
|
||||
write: bool,
|
||||
@@ -175,7 +174,7 @@ pub(crate) fn path_open(
|
||||
|
||||
let new_handle = match winx::file::openat(
|
||||
dir.as_raw_handle(),
|
||||
&path,
|
||||
path.as_str(),
|
||||
win_rights,
|
||||
win_create_disp,
|
||||
win_flags_attrs,
|
||||
@@ -208,7 +207,7 @@ pub(crate) fn fd_readdir(
|
||||
pub(crate) fn path_readlink(
|
||||
wasi_ctx: &WasiCtx,
|
||||
dirfd: host::__wasi_fd_t,
|
||||
path: &RawString,
|
||||
path: &str,
|
||||
rights: host::__wasi_rights_t,
|
||||
buf: &mut [u8],
|
||||
) -> Result<usize, host::__wasi_errno_t> {
|
||||
@@ -218,10 +217,10 @@ pub(crate) fn path_readlink(
|
||||
pub(crate) fn path_rename(
|
||||
wasi_ctx: &WasiCtx,
|
||||
old_dirfd: host::__wasi_fd_t,
|
||||
old_path: &RawString,
|
||||
old_path: &str,
|
||||
old_rights: host::__wasi_rights_t,
|
||||
new_dirfd: host::__wasi_fd_t,
|
||||
new_path: &RawString,
|
||||
new_path: &str,
|
||||
new_rights: host::__wasi_rights_t,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
unimplemented!("path_rename")
|
||||
@@ -253,7 +252,7 @@ pub(crate) fn path_filestat_get(
|
||||
wasi_ctx: &WasiCtx,
|
||||
dirfd: host::__wasi_fd_t,
|
||||
dirflags: host::__wasi_lookupflags_t,
|
||||
path: &RawString,
|
||||
path: &str,
|
||||
) -> Result<host::__wasi_filestat_t, host::__wasi_errno_t> {
|
||||
unimplemented!("path_filestat_get")
|
||||
}
|
||||
@@ -262,7 +261,7 @@ pub(crate) fn path_filestat_set_times(
|
||||
wasi_ctx: &WasiCtx,
|
||||
dirfd: host::__wasi_fd_t,
|
||||
dirflags: host::__wasi_lookupflags_t,
|
||||
path: &RawString,
|
||||
path: &str,
|
||||
rights: host::__wasi_rights_t,
|
||||
st_atim: host::__wasi_timestamp_t,
|
||||
mut st_mtim: host::__wasi_timestamp_t,
|
||||
@@ -275,8 +274,8 @@ pub(crate) fn path_symlink(
|
||||
wasi_ctx: &WasiCtx,
|
||||
dirfd: host::__wasi_fd_t,
|
||||
rights: host::__wasi_rights_t,
|
||||
old_path: &RawString,
|
||||
new_path: &RawString,
|
||||
old_path: &str,
|
||||
new_path: &str,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
unimplemented!("path_symlink")
|
||||
}
|
||||
@@ -284,7 +283,7 @@ pub(crate) fn path_symlink(
|
||||
pub(crate) fn path_unlink_file(
|
||||
wasi_ctx: &WasiCtx,
|
||||
dirfd: host::__wasi_fd_t,
|
||||
path: &RawString,
|
||||
path: &str,
|
||||
rights: host::__wasi_rights_t,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
unimplemented!("path_unlink_file")
|
||||
@@ -293,7 +292,7 @@ pub(crate) fn path_unlink_file(
|
||||
pub(crate) fn path_remove_directory(
|
||||
wasi_ctx: &WasiCtx,
|
||||
dirfd: host::__wasi_fd_t,
|
||||
path: &RawString,
|
||||
path: &str,
|
||||
rights: host::__wasi_rights_t,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
unimplemented!("path_remove_directory")
|
||||
|
||||
@@ -5,24 +5,22 @@ use crate::ctx::WasiCtx;
|
||||
use crate::fdentry::Descriptor;
|
||||
use crate::host;
|
||||
use crate::sys::errno_from_host;
|
||||
use crate::sys::host_impl::{self, RawString};
|
||||
|
||||
use std::ffi::OsStr;
|
||||
use crate::sys::host_impl;
|
||||
use std::fs::File;
|
||||
use std::os::windows::prelude::{AsRawHandle, FromRawHandle};
|
||||
use std::path::{Component, Path};
|
||||
|
||||
/// Normalizes a path to ensure that the target path is located under the directory provided.
|
||||
pub fn path_get(
|
||||
pub(crate) fn path_get(
|
||||
wasi_ctx: &WasiCtx,
|
||||
dirfd: host::__wasi_fd_t,
|
||||
_dirflags: host::__wasi_lookupflags_t,
|
||||
path: &RawString,
|
||||
path: &str,
|
||||
needed_base: host::__wasi_rights_t,
|
||||
needed_inheriting: host::__wasi_rights_t,
|
||||
needs_final_component: bool,
|
||||
) -> Result<(File, RawString), host::__wasi_errno_t> {
|
||||
if path.contains(&b'\0') {
|
||||
) -> Result<(File, String), host::__wasi_errno_t> {
|
||||
if path.contains("\0") {
|
||||
// if contains NUL, return EILSEQ
|
||||
return Err(host::__WASI_EILSEQ);
|
||||
}
|
||||
@@ -44,13 +42,13 @@ pub fn path_get(
|
||||
|
||||
// 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.clone()];
|
||||
let mut path_stack = vec![path.to_owned()];
|
||||
|
||||
loop {
|
||||
match path_stack.pop() {
|
||||
Some(cur_path) => {
|
||||
// dbg!(&cur_path);
|
||||
let ends_with_slash = cur_path.ends_with(b"/");
|
||||
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),
|
||||
@@ -59,9 +57,9 @@ pub fn path_get(
|
||||
let tail = components.as_path();
|
||||
|
||||
if tail.components().next().is_some() {
|
||||
let mut tail = RawString::from(tail.as_os_str());
|
||||
let mut tail = host_impl::path_from_host(tail.as_os_str())?;
|
||||
if ends_with_slash {
|
||||
tail.push("/");
|
||||
tail.push_str("/");
|
||||
}
|
||||
path_stack.push(tail);
|
||||
}
|
||||
@@ -85,10 +83,10 @@ pub fn path_get(
|
||||
}
|
||||
}
|
||||
Component::Normal(head) => {
|
||||
let mut head = RawString::from(head);
|
||||
let mut head = host_impl::path_from_host(head)?;
|
||||
if ends_with_slash {
|
||||
// preserve trailing slash
|
||||
head.push("/");
|
||||
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
|
||||
@@ -98,7 +96,7 @@ pub fn path_get(
|
||||
.last()
|
||||
.ok_or(host::__WASI_ENOTCAPABLE)?
|
||||
.as_raw_handle(),
|
||||
head.as_ref(),
|
||||
head.as_str(),
|
||||
winx::file::AccessRight::FILE_GENERIC_READ,
|
||||
winx::file::CreationDisposition::OPEN_EXISTING,
|
||||
winx::file::FlagsAndAttributes::FILE_FLAG_BACKUP_SEMANTICS,
|
||||
@@ -123,7 +121,7 @@ pub fn path_get(
|
||||
// input path has trailing slashes and `needs_final_component` is not set
|
||||
return Ok((
|
||||
dir_stack.pop().ok_or(host::__WASI_ENOTCAPABLE)?,
|
||||
RawString::from(OsStr::new(".")),
|
||||
String::from("."),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user