Skip to content

Commit

Permalink
minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
matrixhead committed Aug 23, 2024
1 parent 62944a2 commit d1dfdf0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
13 changes: 9 additions & 4 deletions src/uu/cp/src/cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ use quick_error::ResultExt;
use platform::copy_on_write;
use uucore::display::Quotable;
use uucore::error::{set_exit_code, UClapError, UError, UResult, UUsageError};
#[cfg(unix)]
use uucore::fs::display_permissions;
use uucore::fs::{
are_hardlinks_to_same_file, canonicalize, display_permissions, get_filename, is_symlink_loop,
are_hardlinks_to_same_file, canonicalize, get_filename, is_symlink_loop,
path_ends_with_terminator, paths_refer_to_same_file, FileInformation, MissingHandling,
ResolveMode,
};
Expand Down Expand Up @@ -1406,8 +1408,11 @@ impl OverwriteMode {
}
Err(Error::Skipped(false))
}
// allow `unused_variables` because windows doesn't use `clobber_mode`
#[allow(unused_variables)]
Self::Interactive(clobber_mode) => {
let prompt = if cfg!(unix) {
#[cfg(unix)]
let prompt = {
let path_md = path.metadata()?;
if path_md.permissions().readonly() {
match clobber_mode {
Expand All @@ -1427,9 +1432,9 @@ impl OverwriteMode {
} else {
format!("overwrite {}?", path.quote())
}
} else {
format!("overwrite {}?", path.quote())
};
#[cfg(not(unix))]
let prompt = { format!("overwrite {}?", path.quote()) };
if prompt_yes!("{prompt}") {
Ok(())
} else {
Expand Down
23 changes: 19 additions & 4 deletions tests/by-util/test_cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5748,6 +5748,11 @@ fn test_cp_interactive_with_read_only_dest() {
.permissions();
permissions.set_readonly(true);
set_permissions(at.plus(dest), permissions).expect("couldn't set permissions");
let perms_prompt = if cfg!(target_os = "android") {
" (mode 0400, r--------); try anyway?"
} else {
"(mode 0444, r--r--r--); try anyway?"
};
scene
.ucmd()
.args(&[
Expand All @@ -5758,7 +5763,7 @@ fn test_cp_interactive_with_read_only_dest() {
.pipe_in("y\n")
.fails()
.stderr_contains("unwritable")
.stderr_contains("(mode 0444, r--r--r--); try anyway?")
.stderr_contains(perms_prompt)
.stderr_contains("Permission denied");
assert_eq!(at.read(&at.plus(dest).to_string_lossy()), "dest contents");
}
Expand All @@ -5781,6 +5786,11 @@ fn test_cp_interactive_with_read_only_dest_and_force() {
.permissions();
permissions.set_readonly(true);
set_permissions(at.plus(dest), permissions).expect("couldn't set permissions");
let perms_prompt = if cfg!(target_os = "android") {
" overriding mode 0400 (r--------)?"
} else {
" overriding mode 0444 (r--r--r--)?"
};
scene
.ucmd()
.args(&[
Expand All @@ -5791,10 +5801,11 @@ fn test_cp_interactive_with_read_only_dest_and_force() {
.pipe_in("y\n")
.succeeds()
.stderr_contains("replace")
.stderr_contains(" overriding mode 0444 (r--r--r--)?");
.stderr_contains(perms_prompt);
assert_eq!(at.read(&at.plus(dest).to_string_lossy()), "src contents");
}

#[cfg(unix)]
#[test]
fn test_cp_interactive_with_read_only_dest_and_rem() {
let src = "src";
Expand All @@ -5812,7 +5823,11 @@ fn test_cp_interactive_with_read_only_dest_and_rem() {
.permissions();
permissions.set_readonly(true);
set_permissions(at.plus(dest), permissions).expect("couldn't set permissions");

let perms_prompt = if cfg!(target_os = "android") {
" overriding mode 0400 (r--------)?"
} else {
" overriding mode 0444 (r--r--r--)?"
};
scene
.ucmd()
.args(&[
Expand All @@ -5824,7 +5839,7 @@ fn test_cp_interactive_with_read_only_dest_and_rem() {
.pipe_in("y\n")
.succeeds()
.stderr_contains("replace")
.stderr_contains(" overriding mode 0444 (r--r--r--)?");
.stderr_contains(perms_prompt);
assert_eq!(at.read(&at.plus(dest).to_string_lossy()), "src contents");
}

Expand Down

0 comments on commit d1dfdf0

Please sign in to comment.