Skip to content
Merged
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
83 changes: 65 additions & 18 deletions src/uu/cp/src/cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,16 @@ impl UError for Error {
pub type CopyResult<T> = Result<T, Error>;

/// Specifies how to overwrite files.
#[derive(Clone, Copy, Eq, PartialEq)]
#[derive(Debug, Clone, Copy, Eq, PartialEq, Default)]
pub enum ClobberMode {
Force,
RemoveDestination,
#[default]
Standard,
}

/// Specifies whether files should be overwritten.
#[derive(Clone, Copy, Eq, PartialEq)]
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum OverwriteMode {
/// [Default] Always overwrite existing files
Clobber(ClobberMode),
Expand All @@ -128,18 +129,39 @@ pub enum OverwriteMode {
NoClobber,
}

impl Default for OverwriteMode {
fn default() -> Self {
Self::Clobber(ClobberMode::default())
}
}

/// Possible arguments for `--reflink`.
#[derive(Copy, Clone, Eq, PartialEq)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum ReflinkMode {
Always,
Auto,
Never,
}

impl Default for ReflinkMode {
#[allow(clippy::derivable_impls)]
fn default() -> Self {
#[cfg(any(target_os = "linux", target_os = "android", target_os = "macos"))]
{
ReflinkMode::Auto
}
#[cfg(not(any(target_os = "linux", target_os = "android", target_os = "macos")))]
{
ReflinkMode::Never
}
}
}

/// Possible arguments for `--sparse`.
#[derive(Copy, Clone, Eq, PartialEq)]
#[derive(Debug, Copy, Clone, Eq, PartialEq, Default)]
pub enum SparseMode {
Always,
#[default]
Auto,
Never,
}
Expand All @@ -152,10 +174,11 @@ pub enum TargetType {
}

/// Copy action to perform
#[derive(PartialEq)]
#[derive(Debug, Clone, Eq, PartialEq, Default)]
pub enum CopyMode {
Link,
SymLink,
#[default]
Copy,
Update,
AttrOnly,
Expand All @@ -174,7 +197,7 @@ pub enum CopyMode {
/// For full compatibility with GNU, these options should also combine. We
/// currently only do a best effort imitation of that behavior, because it is
/// difficult to achieve in clap, especially with `--no-preserve`.
#[derive(Debug, PartialEq, Clone, Copy)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub struct Attributes {
#[cfg(unix)]
pub ownership: Preserve,
Expand All @@ -185,6 +208,12 @@ pub struct Attributes {
pub xattr: Preserve,
}

impl Default for Attributes {
fn default() -> Self {
Self::NONE
}
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Preserve {
// explicit means whether the --no-preserve flag is used or not to distinguish out the default value.
Expand Down Expand Up @@ -224,6 +253,7 @@ impl Ord for Preserve {
///
/// The fields are documented with the arguments that determine their value.
#[allow(dead_code)]
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Options {
/// `--attributes-only`
pub attributes_only: bool,
Expand Down Expand Up @@ -287,6 +317,34 @@ pub struct Options {
pub progress_bar: bool,
}

impl Default for Options {
fn default() -> Self {
Self {
attributes_only: false,
backup: BackupMode::default(),
copy_contents: false,
cli_dereference: false,
copy_mode: CopyMode::default(),
dereference: false,
no_target_dir: false,
one_file_system: false,
overwrite: OverwriteMode::default(),
parents: false,
sparse_mode: SparseMode::default(),
strip_trailing_slashes: false,
reflink_mode: ReflinkMode::default(),
attributes: Attributes::default(),
recursive: false,
backup_suffix: backup_control::DEFAULT_BACKUP_SUFFIX.to_owned(),
target_dir: None,
update: UpdateMode::default(),
debug: false,
verbose: false,
progress_bar: false,
}
}
}

/// Enum representing if a file has been skipped.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum PerformedAction {
Expand Down Expand Up @@ -1091,18 +1149,7 @@ impl Options {
}
}
} else {
#[cfg(any(target_os = "linux", target_os = "android", target_os = "macos"))]
{
ReflinkMode::Auto
}
#[cfg(not(any(
target_os = "linux",
target_os = "android",
target_os = "macos"
)))]
{
ReflinkMode::Never
}
ReflinkMode::default()
}
},
sparse_mode: {
Expand Down
20 changes: 19 additions & 1 deletion src/uu/mv/src/mv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,32 @@ pub struct Options {
pub debug: bool,
}

impl Default for Options {
fn default() -> Self {
Self {
overwrite: OverwriteMode::default(),
backup: BackupMode::default(),
suffix: backup_control::DEFAULT_BACKUP_SUFFIX.to_owned(),
update: UpdateMode::default(),
target_dir: None,
no_target_dir: false,
verbose: false,
strip_slashes: false,
progress_bar: false,
debug: false,
}
}
}

/// specifies behavior of the overwrite flag
#[derive(Clone, Debug, Eq, PartialEq)]
#[derive(Clone, Debug, Eq, PartialEq, Default)]
pub enum OverwriteMode {
/// '-n' '--no-clobber' do not overwrite
NoClobber,
/// '-i' '--interactive' prompt before overwrite
Interactive,
///'-f' '--force' overwrite without prompt
#[default]
Force,
}

Expand Down
7 changes: 5 additions & 2 deletions src/uucore/src/lib/features/backup_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,16 @@ static VALID_ARGS_HELP: &str = "Valid arguments are:
- 'existing', 'nil'
- 'numbered', 't'";

pub const DEFAULT_BACKUP_SUFFIX: &str = "~";

/// Available backup modes.
///
/// The mapping of the backup modes to the CLI arguments is annotated on the
/// enum variants.
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
#[derive(Debug, Clone, Copy, Eq, PartialEq, Default)]
pub enum BackupMode {
/// Argument 'none', 'off'
#[default]
NoBackup,
/// Argument 'simple', 'never'
SimpleBackup,
Expand Down Expand Up @@ -254,7 +257,7 @@ pub fn determine_backup_suffix(matches: &ArgMatches) -> String {
if let Some(suffix) = supplied_suffix {
String::from(suffix)
} else {
env::var("SIMPLE_BACKUP_SUFFIX").unwrap_or_else(|_| "~".to_owned())
env::var("SIMPLE_BACKUP_SUFFIX").unwrap_or_else(|_| DEFAULT_BACKUP_SUFFIX.to_owned())
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/uucore/src/lib/features/update_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@
use clap::ArgMatches;

/// Available update mode
#[derive(Clone, Debug, Eq, PartialEq)]
#[derive(Clone, Debug, Eq, PartialEq, Default)]
pub enum UpdateMode {
/// --update=`all`, ``
#[default]
ReplaceAll,
/// --update=`none`
ReplaceNone,
Expand Down
Loading