Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/src/wasi-test-gaps.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ The WASI guest only sees directories explicitly mapped with `--dir`. Host paths

The WASI specification requires that argv entries and filenames are valid UTF-8. Tests that pass non-UTF-8 bytes as arguments or create files with non-UTF-8 names cannot run under WASI.

## WASI: cp does not support `--one-file-system`

The `cp -x` option is only implemented on Unix and Windows.

## WASI: no FIFO/mkfifo support

WASI does not support creating or opening FIFOs (named pipes). Tests that use `mkfifo` are skipped.
Expand Down
27 changes: 25 additions & 2 deletions src/uu/cp/src/copydir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,11 @@ pub(crate) fn copy_directory(
return Err(translate!("cp-error-omitting-directory", "dir" => root.quote()).into());
}

// check if root is a prefix of target
if path_has_prefix(target, root)? {
if path_has_prefix(target, root)?
&& !(options.one_file_system
&& !options.parents
&& is_on_different_file_system(target, root))
{
let dest_name = root.file_name().unwrap_or(root.as_os_str());
return Err(translate!("cp-error-cannot-copy-directory-into-itself", "source" => root.quote(), "dest" => target.join(dest_name).quote())
.into());
Expand Down Expand Up @@ -631,6 +634,26 @@ pub fn path_has_prefix(p1: &Path, p2: &Path) -> io::Result<bool> {
Ok(pathbuf1.starts_with(pathbuf2))
}

fn is_on_different_file_system(target: &Path, root: &Path) -> bool {
let Ok(source) = FileInformation::from_path(root, true) else {
return false;
};
// A new destination will be on the filesystem of its closest existing ancestor.
for ancestor in target.ancestors() {
let ancestor = if ancestor.as_os_str().is_empty() {
Path::new(".")
} else {
ancestor
};
match FileInformation::from_path(ancestor, true) {
Ok(dest) => return dest.dev() != source.dev(),
Err(err) if err.kind() == io::ErrorKind::NotFound => {}
Err(_) => return false,
}
}
false
}

/// Builds a directory at the specified path with the given options.
///
/// # Notes
Expand Down
28 changes: 25 additions & 3 deletions tests/by-util/test_cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3255,6 +3255,25 @@ fn test_cp_one_file_system() {
.arg(TEST_COPY_TO_FOLDER_NEW)
.succeeds();

let at_mount = AtPath::new(mountpoint_path);
scene
.ucmd()
.args(&["-ax", TEST_MOUNT_COPY_FROM_FOLDER])
.arg(mountpoint_path)
.succeeds()
.no_stderr();
assert!(at_mount.file_exists("dir_with_mount/copy_me.txt"));
assert!(!at_mount.file_exists("dir_with_mount/mount/DO_NOT_copy_me.txt"));

scene
.ucmd()
.args(&["-rx", TEST_MOUNT_COPY_FROM_FOLDER])
.arg(at_mount.plus("new"))
.succeeds()
.no_stderr();
assert!(at_mount.file_exists("new/copy_me.txt"));
assert!(!at_mount.file_exists("new/mount/DO_NOT_copy_me.txt"));

// Ditch the mount before the asserts
scene.umount_temp_fs();

Expand Down Expand Up @@ -4921,8 +4940,11 @@ fn test_copy_directory_to_itself_disallowed() {
}

/// Test that copying a nested directory to itself is disallowed.
#[test]
fn test_copy_nested_directory_to_itself_disallowed() {
#[rstest]
#[case("-R")]
#[uutests::wasi_ignore(OneFileSystemUnsupported)]
#[case("-Rx")]
fn test_copy_nested_directory_to_itself_disallowed(#[case] flag: &str) {
let (at, mut ucmd) = at_and_ucmd!();
at.mkdir("a");
at.mkdir("a/b");
Expand All @@ -4931,7 +4953,7 @@ fn test_copy_nested_directory_to_itself_disallowed() {
let expected = "cp: cannot copy a directory, 'a/b', into itself, 'a/b/c/b'\n";
#[cfg(windows)]
let expected = "cp: cannot copy a directory, 'a/b', into itself, 'a/b/c\\b'\n";
ucmd.args(&["-R", "a/b", "a/b/c"])
ucmd.args(&[flag, "a/b", "a/b/c"])
.fails()
.stderr_only(expected);
}
Expand Down
1 change: 1 addition & 0 deletions tests/uutests_procs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ fn wasi_reason(key: &str) -> Option<&'static str> {
"NonUtf8ArgsUnsupported" => {
"WASI: argv, env values, and paths must be valid UTF-8; wasmtime cannot pass non-UTF-8 data through the sandboxed binary or the spawned test harness"
}
"OneFileSystemUnsupported" => "WASI: cp does not support --one-file-system",
"PermissionErrorsSurfaceAsEnoent" => {
"WASI: filesystem permission errors surface as ENOENT rather than EACCES"
}
Expand Down
Loading