Skip to content

Commit

Permalink
Implement path_link for Windows. Add a non-strict version of the
Browse files Browse the repository at this point in the history
path_link test.
  • Loading branch information
marmistrz committed Mar 1, 2020
1 parent 2c5be49 commit 9220fd5
Show file tree
Hide file tree
Showing 2 changed files with 319 additions and 8 deletions.
278 changes: 278 additions & 0 deletions crates/test-programs/wasi-tests/src/bin/path_link_nonstrict.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,278 @@
use more_asserts::assert_gt;
use std::{env, process};
use wasi_tests::{create_file, open_scratch_directory};

const TEST_RIGHTS: wasi::Rights = wasi::RIGHTS_FD_READ
| wasi::RIGHTS_PATH_LINK_SOURCE
| wasi::RIGHTS_PATH_LINK_TARGET
| wasi::RIGHTS_FD_FILESTAT_GET
| wasi::RIGHTS_PATH_OPEN
| wasi::RIGHTS_PATH_UNLINK_FILE;

unsafe fn create_or_open(dir_fd: wasi::Fd, name: &str, flags: wasi::Oflags) -> wasi::Fd {
let file_fd = wasi::path_open(dir_fd, 0, name, flags, TEST_RIGHTS, TEST_RIGHTS, 0)
.unwrap_or_else(|_| panic!("opening '{}'", name));
assert_gt!(
file_fd,
libc::STDERR_FILENO as wasi::Fd,
"file descriptor range check",
);
file_fd
}

unsafe fn open_link(dir_fd: wasi::Fd, name: &str) -> wasi::Fd {
let file_fd = wasi::path_open(dir_fd, 0, name, 0, TEST_RIGHTS, TEST_RIGHTS, 0)
.unwrap_or_else(|_| panic!("opening a link '{}'", name));
assert_gt!(
file_fd,
libc::STDERR_FILENO as wasi::Fd,
"file descriptor range check",
);
file_fd
}

// This is temporary until `wasi` implements `Debug` and `PartialEq` for
// `wasi::Filestat`.
fn filestats_assert_eq(left: wasi::Filestat, right: wasi::Filestat) {
assert_eq!(left.dev, right.dev, "dev should be equal");
assert_eq!(left.ino, right.ino, "ino should be equal");
assert_eq!(left.atim, right.atim, "atim should be equal");
assert_eq!(left.ctim, right.ctim, "ctim should be equal");
assert_eq!(left.mtim, right.mtim, "mtim should be equal");
assert_eq!(left.size, right.size, "size should be equal");
assert_eq!(left.nlink, right.nlink, "nlink should be equal");
assert_eq!(left.filetype, right.filetype, "filetype should be equal");
}

// This is temporary until `wasi` implements `Debug` and `PartialEq` for
// `wasi::Fdstat`.
fn fdstats_assert_eq(left: wasi::Fdstat, right: wasi::Fdstat) {
assert_eq!(left.fs_flags, right.fs_flags, "fs_flags should be equal");
assert_eq!(
left.fs_filetype, right.fs_filetype,
"fs_filetype should be equal"
);
assert_eq!(
left.fs_rights_base, right.fs_rights_base,
"fs_rights_base should be equal"
);
assert_eq!(
left.fs_rights_inheriting, right.fs_rights_inheriting,
"fs_rights_inheriting should be equal"
);
}

unsafe fn check_rights(orig_fd: wasi::Fd, link_fd: wasi::Fd) {
// Compare Filestats
let orig_filestat = wasi::fd_filestat_get(orig_fd).expect("reading filestat of the source");
let link_filestat = wasi::fd_filestat_get(link_fd).expect("reading filestat of the link");
filestats_assert_eq(orig_filestat, link_filestat);

// Compare Fdstats
let orig_fdstat = wasi::fd_fdstat_get(orig_fd).expect("reading fdstat of the source");
let link_fdstat = wasi::fd_fdstat_get(link_fd).expect("reading fdstat of the link");
fdstats_assert_eq(orig_fdstat, link_fdstat);
}

unsafe fn test_path_link(dir_fd: wasi::Fd) {
// Create a file
let file_fd = create_or_open(dir_fd, "file", wasi::OFLAGS_CREAT);

// Create a link in the same directory and compare rights
wasi::path_link(dir_fd, 0, "file", dir_fd, "link")
.expect("creating a link in the same directory");
let mut link_fd = open_link(dir_fd, "link");
check_rights(file_fd, link_fd);
wasi::path_unlink_file(dir_fd, "link").expect("removing a link");

// Create a link in a different directory and compare rights
wasi::path_create_directory(dir_fd, "subdir").expect("creating a subdirectory");
let subdir_fd = create_or_open(dir_fd, "subdir", wasi::OFLAGS_DIRECTORY);
wasi::path_link(dir_fd, 0, "file", subdir_fd, "link").expect("creating a link in subdirectory");
link_fd = open_link(subdir_fd, "link");
check_rights(file_fd, link_fd);
wasi::path_unlink_file(subdir_fd, "link").expect("removing a link");
wasi::path_remove_directory(dir_fd, "subdir").expect("removing a subdirectory");

// Create a link to a path that already exists
create_file(dir_fd, "link");

assert_eq!(
wasi::path_link(dir_fd, 0, "file", dir_fd, "link")
.expect_err("creating a link to existing path should fail")
.raw_error(),
wasi::ERRNO_EXIST,
"errno should be ERRNO_EXIST"
);
wasi::path_unlink_file(dir_fd, "link").expect("removing a file");

// Create a link to itself
assert_eq!(
wasi::path_link(dir_fd, 0, "file", dir_fd, "file")
.expect_err("creating a link to itself should fail")
.raw_error(),
wasi::ERRNO_EXIST,
"errno should be ERRNO_EXIST"
);

// Create a link where target is a directory
wasi::path_create_directory(dir_fd, "link").expect("creating a dir");

assert_eq!(
wasi::path_link(dir_fd, 0, "file", dir_fd, "link")
.expect_err("creating a link where target is a directory should fail")
.raw_error(),
wasi::ERRNO_EXIST,
"errno should be ERRNO_EXIST"
);
wasi::path_remove_directory(dir_fd, "link").expect("removing a dir");

// Create a link to a directory
//
// NON-STRICTNESS:
// The file descriptors will not be closed until wasmtime terminates
// and Windows will not remove a directory until it's closed. Therefore we need
// to use a different subdirectory for each test
wasi::path_create_directory(dir_fd, "subdir2").expect("creating a subdirectory");
create_or_open(dir_fd, "subdir2", wasi::OFLAGS_DIRECTORY);

// TODO EPERM vs EACCES
let err = wasi::path_link(dir_fd, 0, "subdir2", dir_fd, "link")
.expect_err("creating a link to a directory should fail")
.raw_error();
assert!(
err == wasi::ERRNO_PERM || err == wasi::ERRNO_ACCES,
"errno should be ERRNO_PERM or ERRNO_ACCESS, was: {}",
err
);
wasi::path_remove_directory(dir_fd, "subdir2").expect("removing a subdirectory");

// Create a link to a file with trailing slash
assert_eq!(
wasi::path_link(dir_fd, 0, "file", dir_fd, "link/")
.expect_err("creating a link to a file with trailing slash should fail")
.raw_error(),
wasi::ERRNO_NOENT,
"errno should be ERRNO_NOENT"
);

// NON-STRICTNESS
// The following tests are disabled because Windows doesn't support
// dangling symlinks or symlink loops
/*
// Create a link to a dangling symlink
wasi::path_symlink("target", dir_fd, "symlink").expect("creating a dangling symlink");
assert_eq!(
wasi::path_link(dir_fd, 0, "symlink", dir_fd, "link")
.expect_err("creating a link to a dangling symlink should fail")
.raw_error(),
wasi::ERRNO_NOENT,
"errno should be ERRNO_NOENT"
);
wasi::path_unlink_file(dir_fd, "symlink").expect("removing a symlink");*/

// Create a link to a symlink loop
// NON-STRICTNESS
// Windows doesn't support symlink loops
/*wasi::path_symlink("symlink", dir_fd, "symlink").expect("creating a symlink loop");
assert_eq!(
wasi::path_link(dir_fd, 0, "symlink", dir_fd, "link")
.expect_err("creating a link to a symlink loop")
.raw_error(),
wasi::ERRNO_LOOP,
"errno should be ERRNO_LOOP"
);
wasi::path_unlink_file(dir_fd, "symlink").expect("removing a symlink");
// Create a link where target is a dangling symlink
wasi::path_symlink("target", dir_fd, "symlink").expect("creating a dangling symlink");
assert_eq!(
wasi::path_link(dir_fd, 0, "file", dir_fd, "symlink")
.expect_err("creating a link where target is a dangling symlink")
.raw_error(),
wasi::ERRNO_EXIST,
"errno should be ERRNO_EXIST"
);
wasi::path_unlink_file(dir_fd, "symlink").expect("removing a symlink");
// Create a link to a file following symlinks
wasi::path_symlink("file", dir_fd, "symlink").expect("creating a valid symlink");
wasi::path_link(
dir_fd,
wasi::LOOKUPFLAGS_SYMLINK_FOLLOW,
"symlink",
dir_fd,
"link",
)
.expect("creating a link to a file following symlinks");
link_fd = open_link(dir_fd, "link");
check_rights(file_fd, link_fd);
wasi::path_unlink_file(dir_fd, "link").expect("removing a link");
wasi::path_unlink_file(dir_fd, "symlink").expect("removing a symlink");
// Create a link where target is a dangling symlink following symlinks
wasi::path_symlink("target", dir_fd, "symlink").expect("creating a dangling symlink");
assert_eq!(
wasi::path_link(
dir_fd,
wasi::LOOKUPFLAGS_SYMLINK_FOLLOW,
"symlink",
dir_fd,
"link",
)
.expect_err("creating a link where target is a dangling symlink following symlinks")
.raw_error(),
wasi::ERRNO_NOENT,
"errno should be ERRNO_NOENT"
);
wasi::path_unlink_file(dir_fd, "symlink").expect("removing a symlink");
// Create a link to a symlink loop following symlinks
wasi::path_symlink("symlink", dir_fd, "symlink").expect("creating a symlink loop");
assert_eq!(
wasi::path_link(
dir_fd,
wasi::LOOKUPFLAGS_SYMLINK_FOLLOW,
"symlink",
dir_fd,
"link",
)
.expect_err("creating a link to a symlink loop following symlinks")
.raw_error(),
wasi::ERRNO_LOOP,
"errno should be ERRNO_LOOP"
);
wasi::path_unlink_file(dir_fd, "symlink").expect("removing a symlink");
// Clean up.
wasi::path_unlink_file(dir_fd, "file").expect("removing a file");
*/
}

fn main() {
let mut args = env::args();
let prog = args.next().unwrap();
let arg = if let Some(arg) = args.next() {
arg
} else {
eprintln!("usage: {} <scratch directory>", prog);
process::exit(1);
};

// Open scratch directory
let dir_fd = match open_scratch_directory(&arg) {
Ok(dir_fd) => dir_fd,
Err(err) => {
eprintln!("{}", err);
process::exit(1)
}
};

// Run the tests.
unsafe { test_path_link(dir_fd) }
}
49 changes: 41 additions & 8 deletions crates/wasi-common/src/sys/windows/hostcalls_impl/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ 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};
use log::{debug, trace};
use log::{debug, trace, warn};
use std::convert::TryInto;
use std::fs::{File, Metadata, OpenOptions};
use std::io::{self, Seek, SeekFrom};
use std::os::windows::fs::{FileExt, OpenOptionsExt};
use std::os::windows::prelude::{AsRawHandle, FromRawHandle};
use std::path::{Path, PathBuf};
use winx::file::{AccessMode, CreationDisposition, FileModeInformation, Flags};
use winx::winerror::WinError;

fn read_at(mut file: &File, buf: &mut [u8], offset: u64) -> io::Result<usize> {
// get current cursor position
Expand Down Expand Up @@ -125,7 +126,40 @@ pub(crate) fn path_create_directory(resolved: PathGet) -> Result<()> {
}

pub(crate) fn path_link(resolved_old: PathGet, resolved_new: PathGet) -> Result<()> {
unimplemented!("path_link")
use std::fs;
let old_path = resolved_old.concatenate()?;
let new_path = resolved_new.concatenate()?;
fs::hard_link(&old_path, &new_path).or_else(|e| {
match e.raw_os_error() {
Some(e) => {
log::debug!("path_link at fs::hard_link error code={:?}", e);
match WinError::from_u32(e as u32) {
WinError::ERROR_ACCESS_DENIED => {
if new_path.exists() {
// the target already exists
Err(Error::EEXIST)
} else {
Err(WinError::ERROR_ACCESS_DENIED.into())
}
}
WinError::ERROR_INVALID_NAME => {
// does the target without trailing slashes exist?
if let Some(path) = strip_trailing_slashes_and_concatenate(&resolved_new)? {
if path.exists() {
return Err(Error::EEXIST);
}
}
Err(WinError::ERROR_INVALID_NAME.into())
}
e => Err(e.into()),
}
}
None => {
log::debug!("Inconvertible OS error: {}", e);
Err(Error::EIO)
}
}
})
}

pub(crate) fn path_open(
Expand Down Expand Up @@ -180,7 +214,6 @@ pub(crate) fn path_open(
}
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);

Expand Down Expand Up @@ -430,8 +463,6 @@ pub(crate) fn path_rename(resolved_old: PathGet, resolved_new: PathGet) -> Resul

fs::rename(&old_path, &new_path).or_else(|e| match e.raw_os_error() {
Some(e) => {
use winx::winerror::WinError;

log::debug!("path_rename at rename error code={:?}", e);
match WinError::from_u32(e as u32) {
WinError::ERROR_ACCESS_DENIED => {
Expand Down Expand Up @@ -497,7 +528,6 @@ pub(crate) fn path_filestat_set_times(

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 = resolved.concatenate()?;
Expand All @@ -513,9 +543,13 @@ pub(crate) fn path_symlink(old_path: &str, resolved: PathGet) -> Result<()> {
symlink_dir(old_path, new_path).map_err(Into::into)
}
WinError::ERROR_ACCESS_DENIED => {
// does the target exist?
if new_path.exists() {
// the target already exists
Err(Error::EEXIST)
} else if !old_path.exists() {
// attempt to create a dangling symlink
warn!("Dangling symlink or symlink loops unsupported on Windows");
Err(Error::ENOTSUP)
} else {
Err(WinError::ERROR_ACCESS_DENIED.into())
}
Expand All @@ -542,7 +576,6 @@ pub(crate) fn path_symlink(old_path: &str, resolved: PathGet) -> Result<()> {

pub(crate) fn path_unlink_file(resolved: PathGet) -> Result<()> {
use std::fs;
use winx::winerror::WinError;

let path = resolved.concatenate()?;
let file_type = path
Expand Down

0 comments on commit 9220fd5

Please sign in to comment.