mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 12:07:46 +00:00
cp: implement update=[switch]
This commit is contained in:
parent
0e8dd894a3
commit
9dc84e9061
1 changed files with 14 additions and 15 deletions
|
@ -40,6 +40,7 @@ use uucore::error::{set_exit_code, UClapError, UError, UResult, UUsageError};
|
||||||
use uucore::fs::{
|
use uucore::fs::{
|
||||||
canonicalize, paths_refer_to_same_file, FileInformation, MissingHandling, ResolveMode,
|
canonicalize, paths_refer_to_same_file, FileInformation, MissingHandling, ResolveMode,
|
||||||
};
|
};
|
||||||
|
use uucore::update_control::{self, UpdateMode};
|
||||||
use uucore::{crash, format_usage, help_about, help_usage, prompt_yes, show_error, show_warning};
|
use uucore::{crash, format_usage, help_about, help_usage, prompt_yes, show_error, show_warning};
|
||||||
|
|
||||||
use crate::copydir::copy_directory;
|
use crate::copydir::copy_directory;
|
||||||
|
@ -224,7 +225,7 @@ pub struct Options {
|
||||||
recursive: bool,
|
recursive: bool,
|
||||||
backup_suffix: String,
|
backup_suffix: String,
|
||||||
target_dir: Option<PathBuf>,
|
target_dir: Option<PathBuf>,
|
||||||
update: bool,
|
update: UpdateMode,
|
||||||
verbose: bool,
|
verbose: bool,
|
||||||
progress_bar: bool,
|
progress_bar: bool,
|
||||||
}
|
}
|
||||||
|
@ -264,7 +265,6 @@ mod options {
|
||||||
pub const STRIP_TRAILING_SLASHES: &str = "strip-trailing-slashes";
|
pub const STRIP_TRAILING_SLASHES: &str = "strip-trailing-slashes";
|
||||||
pub const SYMBOLIC_LINK: &str = "symbolic-link";
|
pub const SYMBOLIC_LINK: &str = "symbolic-link";
|
||||||
pub const TARGET_DIRECTORY: &str = "target-directory";
|
pub const TARGET_DIRECTORY: &str = "target-directory";
|
||||||
pub const UPDATE: &str = "update";
|
|
||||||
pub const VERBOSE: &str = "verbose";
|
pub const VERBOSE: &str = "verbose";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -296,6 +296,8 @@ pub fn uu_app() -> Command {
|
||||||
.about(ABOUT)
|
.about(ABOUT)
|
||||||
.override_usage(format_usage(USAGE))
|
.override_usage(format_usage(USAGE))
|
||||||
.infer_long_args(true)
|
.infer_long_args(true)
|
||||||
|
.arg(update_control::arguments::update())
|
||||||
|
.arg(update_control::arguments::update_no_args())
|
||||||
.arg(
|
.arg(
|
||||||
Arg::new(options::TARGET_DIRECTORY)
|
Arg::new(options::TARGET_DIRECTORY)
|
||||||
.short('t')
|
.short('t')
|
||||||
|
@ -393,16 +395,6 @@ pub fn uu_app() -> Command {
|
||||||
.arg(backup_control::arguments::backup())
|
.arg(backup_control::arguments::backup())
|
||||||
.arg(backup_control::arguments::backup_no_args())
|
.arg(backup_control::arguments::backup_no_args())
|
||||||
.arg(backup_control::arguments::suffix())
|
.arg(backup_control::arguments::suffix())
|
||||||
.arg(
|
|
||||||
Arg::new(options::UPDATE)
|
|
||||||
.short('u')
|
|
||||||
.long(options::UPDATE)
|
|
||||||
.help(
|
|
||||||
"copy only when the SOURCE file is newer than the destination file \
|
|
||||||
or when the destination file is missing",
|
|
||||||
)
|
|
||||||
.action(ArgAction::SetTrue),
|
|
||||||
)
|
|
||||||
.arg(
|
.arg(
|
||||||
Arg::new(options::REFLINK)
|
Arg::new(options::REFLINK)
|
||||||
.long(options::REFLINK)
|
.long(options::REFLINK)
|
||||||
|
@ -641,7 +633,11 @@ impl CopyMode {
|
||||||
Self::Link
|
Self::Link
|
||||||
} else if matches.get_flag(options::SYMBOLIC_LINK) {
|
} else if matches.get_flag(options::SYMBOLIC_LINK) {
|
||||||
Self::SymLink
|
Self::SymLink
|
||||||
} else if matches.get_flag(options::UPDATE) {
|
} else if matches
|
||||||
|
.get_one::<String>(update_control::arguments::OPT_UPDATE)
|
||||||
|
.is_some()
|
||||||
|
|| matches.get_flag(update_control::arguments::OPT_UPDATE_NO_ARG)
|
||||||
|
{
|
||||||
Self::Update
|
Self::Update
|
||||||
} else if matches.get_flag(options::ATTRIBUTES_ONLY) {
|
} else if matches.get_flag(options::ATTRIBUTES_ONLY) {
|
||||||
Self::AttrOnly
|
Self::AttrOnly
|
||||||
|
@ -749,6 +745,7 @@ impl Options {
|
||||||
Err(e) => return Err(Error::Backup(format!("{e}"))),
|
Err(e) => return Err(Error::Backup(format!("{e}"))),
|
||||||
Ok(mode) => mode,
|
Ok(mode) => mode,
|
||||||
};
|
};
|
||||||
|
let update_mode = update_control::determine_update_mode(matches);
|
||||||
|
|
||||||
let backup_suffix = backup_control::determine_backup_suffix(matches);
|
let backup_suffix = backup_control::determine_backup_suffix(matches);
|
||||||
|
|
||||||
|
@ -826,7 +823,7 @@ impl Options {
|
||||||
|| matches.get_flag(options::DEREFERENCE),
|
|| matches.get_flag(options::DEREFERENCE),
|
||||||
one_file_system: matches.get_flag(options::ONE_FILE_SYSTEM),
|
one_file_system: matches.get_flag(options::ONE_FILE_SYSTEM),
|
||||||
parents: matches.get_flag(options::PARENTS),
|
parents: matches.get_flag(options::PARENTS),
|
||||||
update: matches.get_flag(options::UPDATE),
|
update: update_mode,
|
||||||
verbose: matches.get_flag(options::VERBOSE),
|
verbose: matches.get_flag(options::VERBOSE),
|
||||||
strip_trailing_slashes: matches.get_flag(options::STRIP_TRAILING_SLASHES),
|
strip_trailing_slashes: matches.get_flag(options::STRIP_TRAILING_SLASHES),
|
||||||
reflink_mode: {
|
reflink_mode: {
|
||||||
|
@ -1473,7 +1470,9 @@ fn copy_file(
|
||||||
symlinked_files: &mut HashSet<FileInformation>,
|
symlinked_files: &mut HashSet<FileInformation>,
|
||||||
source_in_command_line: bool,
|
source_in_command_line: bool,
|
||||||
) -> CopyResult<()> {
|
) -> CopyResult<()> {
|
||||||
if options.update && options.overwrite == OverwriteMode::Interactive(ClobberMode::Standard) {
|
if (options.update == UpdateMode::ReplaceIfOlder || options.update == UpdateMode::ReplaceNone)
|
||||||
|
&& options.overwrite == OverwriteMode::Interactive(ClobberMode::Standard)
|
||||||
|
{
|
||||||
// `cp -i --update old new` when `new` exists doesn't copy anything
|
// `cp -i --update old new` when `new` exists doesn't copy anything
|
||||||
// and exit with 0
|
// and exit with 0
|
||||||
return Ok(());
|
return Ok(());
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue