Skip to content
Open
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
65 changes: 28 additions & 37 deletions src/uu/cp/src/cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1626,49 +1626,40 @@ fn copy_source(
// This fix adds yet another metadata read.
// Should this metadata be read once and then reused throughout the execution?
// https://github.com/uutils/coreutils/issues/6658
#[allow(clippy::if_not_else)]
fn file_mode_for_interactive_overwrite(
#[cfg_attr(not(unix), allow(unused_variables))] path: &Path,
) -> Option<(String, String)> {
// Retain outer braces to ensure only one branch is included
{
#[cfg(unix)]
{
use libc::{S_IWUSR, mode_t};
use std::os::unix::prelude::MetadataExt;

match path.metadata() {
Ok(me) => {
// Cast is necessary on some platforms
#[allow(clippy::unnecessary_cast)]
let mode: mode_t = me.mode() as mode_t;

// It looks like this extra information is added to the prompt iff the file's user write bit is 0
// write permission, owner
if uucore::has!(mode, S_IWUSR) {
None
} else {
// Discard leading digits
let mode_without_leading_digits = mode & 0o7777;

Some((
format!("{mode_without_leading_digits:04o}"),
uucore::fs::display_permissions_unix(mode as u32, false),
))
}
}
// TODO: How should failure to read the metadata be handled? Ignoring for now.
Err(_) => None,
}
}
#[cfg(unix)]
fn file_mode_for_interactive_overwrite(path: &Path) -> Option<(String, String)> {
use libc::{S_IWUSR, mode_t};
use std::os::unix::prelude::MetadataExt;

#[cfg(not(unix))]
{
match path.metadata() {
Ok(me) => {
// Cast is necessary on some platforms
#[allow(clippy::unnecessary_cast)]
let mode: mode_t = me.mode() as mode_t;

// It looks like this extra information is added to the prompt iff the file's user write bit is 0
// write permission, owner
if !uucore::has!(mode, S_IWUSR) {
// Discard leading digits
let mode_without_leading_digits = mode & 0o7777;

return Some((
format!("{mode_without_leading_digits:04o}"),
uucore::fs::display_permissions_unix(mode as u32, false),
));
}
None
}
// TODO: How should failure to read the metadata be handled? Ignoring for now.
Err(_) => None,
}
}

#[cfg(not(unix))]
fn file_mode_for_interactive_overwrite(_: &Path) -> Option<(String, String)> {
None
}

impl OverwriteMode {
fn verify(self, path: &Path, debug: bool) -> CopyResult<()> {
match self {
Expand Down
Loading