Skip to content

Commit

Permalink
cp: context_for() should quote long paths
Browse files Browse the repository at this point in the history
Change the behavior of the `context_for()` helper function so that it
adds quotes around long paths but not short paths.
  • Loading branch information
jfinkels committed Oct 10, 2022
1 parent 38cbada commit 22baf9e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
28 changes: 27 additions & 1 deletion src/uu/cp/src/cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1344,8 +1344,34 @@ fn symlink_file(
Ok(())
}

/// Join the source and destination paths with an arrow in between.
///
/// If the paths are sufficiently long, then they will be surrounded
/// by quotes.
///
/// # Examples
///
/// Short paths:
///
/// ```rust,ignore
/// let src = Path::new("/a/b");
/// let dest = Path::new("/c/d");
/// assert_eq!(context_for(src, dest), "/a/b -> /c/d".to_string());
/// ```
///
/// Long paths:
///
/// ```rust,ignore
/// let src = Path::new("/a/b/c");
/// let dest = Path::new("/d/e/f");
/// assert_eq!(context_for(src, dest), "'/a/b/c' -> '/d/e/f'".to_string());
/// ```
fn context_for(src: &Path, dest: &Path) -> String {
format!("{} -> {}", src.quote(), dest.quote())
if src.components().count() < 3 {
format!("{} -> {}", src.display(), dest.display())
} else {
format!("{} -> {}", src.quote(), dest.quote())
}
}

/// Implements a simple backup copy for the destination file.
Expand Down
6 changes: 5 additions & 1 deletion tests/by-util/test_cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1410,12 +1410,16 @@ fn test_cp_reflink_insufficient_permission() {
.set_permissions(PermissionsExt::from_mode(0o000))
.unwrap();

// FIXME On GNU cp v8.30, the error message is:
//
// cp: cannot open 'unreadable' for reading: Permission denied
//
ucmd.arg("-r")
.arg("--reflink=auto")
.arg("unreadable")
.arg(TEST_EXISTING_FILE)
.fails()
.stderr_only("cp: 'unreadable' -> 'existing_file.txt': Permission denied (os error 13)");
.stderr_only("cp: unreadable -> existing_file.txt: Permission denied (os error 13)");
}

#[cfg(any(target_os = "linux", target_os = "android"))]
Expand Down

0 comments on commit 22baf9e

Please sign in to comment.