Skip to content

Commit

Permalink
Implement path_link for Windows.
Browse files Browse the repository at this point in the history
  • Loading branch information
marmistrz committed Mar 9, 2020
1 parent e47de6f commit a022784
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 96 deletions.
1 change: 0 additions & 1 deletion crates/test-programs/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ mod wasi_tests {
"dangling_symlink" => true,
"symlink_loop" => true,
"truncation_rights" => true,
"path_link" => true,
"dangling_fd" => true,
_ => false,
}
Expand Down
190 changes: 103 additions & 87 deletions crates/test-programs/wasi-tests/src/bin/path_link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ unsafe fn test_path_link(dir_fd: wasi::Fd) {
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");
// This is needed for Windows, which will not remove the directory until all
// handles are closed
wasi::fd_close(subdir_fd).expect("Closing subdir_fd");

// Create a link to a path that already exists
create_file(dir_fd, "link");
Expand Down Expand Up @@ -131,12 +134,13 @@ unsafe fn test_path_link(dir_fd: wasi::Fd) {
wasi::path_create_directory(dir_fd, "subdir").expect("creating a subdirectory");
create_or_open(dir_fd, "subdir", wasi::OFLAGS_DIRECTORY);

assert_eq!(
wasi::path_link(dir_fd, 0, "subdir", dir_fd, "link")
.expect_err("creating a link to a directory should fail")
.raw_error(),
wasi::ERRNO_PERM,
"errno should be ERRNO_PERM"
let err = wasi::path_link(dir_fd, 0, "subdir", 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, "subdir").expect("removing a subdirectory");

Expand All @@ -150,94 +154,106 @@ unsafe fn test_path_link(dir_fd: wasi::Fd) {
);

// 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
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!(
let dangling_symlinks_supported = match wasi::path_symlink("target", dir_fd, "symlink") {
Ok(_) => true,
Err(e) => {
if e.raw_error() == wasi::ERRNO_NOTSUP {
false
} else {
panic!("Unexpected failure in path_symlink: {}", e);
}
}
};
if dangling_symlinks_supported {
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
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");

// If a platform doesn't support creating dangling symlinks skip further tests.
// In particular, the following tests will not be run on a Windows host.
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_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");
.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() {
Expand Down
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 a022784

Please sign in to comment.