mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 20:17:45 +00:00
cp: make more types public and add more documentation
This commit is contained in:
parent
84bfbb0e5a
commit
ffa08f4741
2 changed files with 90 additions and 43 deletions
|
@ -25,7 +25,7 @@ use walkdir::{DirEntry, WalkDir};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
aligned_ancestors, context_for, copy_attributes, copy_file, copy_link, preserve_hardlinks,
|
aligned_ancestors, context_for, copy_attributes, copy_file, copy_link, preserve_hardlinks,
|
||||||
CopyResult, Error, Options, TargetSlice,
|
CopyResult, Error, Options,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Ensure a Windows path starts with a `\\?`.
|
/// Ensure a Windows path starts with a `\\?`.
|
||||||
|
@ -307,7 +307,7 @@ fn copy_direntry(
|
||||||
pub(crate) fn copy_directory(
|
pub(crate) fn copy_directory(
|
||||||
progress_bar: &Option<ProgressBar>,
|
progress_bar: &Option<ProgressBar>,
|
||||||
root: &Path,
|
root: &Path,
|
||||||
target: &TargetSlice,
|
target: &Path,
|
||||||
options: &Options,
|
options: &Options,
|
||||||
symlinked_files: &mut HashSet<FileInformation>,
|
symlinked_files: &mut HashSet<FileInformation>,
|
||||||
source_in_command_line: bool,
|
source_in_command_line: bool,
|
||||||
|
|
|
@ -9,7 +9,7 @@
|
||||||
// For the full copyright and license information, please view the LICENSE file
|
// For the full copyright and license information, please view the LICENSE file
|
||||||
// that was distributed with this source code.
|
// that was distributed with this source code.
|
||||||
|
|
||||||
// spell-checker:ignore (ToDO) copydir ficlone fiemap ftruncate linkgs lstat nlink nlinks pathbuf pwrite reflink strs xattrs symlinked deduplicated advcpmv
|
// spell-checker:ignore (ToDO) copydir ficlone fiemap ftruncate linkgs lstat nlink nlinks pathbuf pwrite reflink strs xattrs symlinked deduplicated advcpmv nushell
|
||||||
|
|
||||||
use quick_error::quick_error;
|
use quick_error::quick_error;
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
@ -51,6 +51,7 @@ use crate::copydir::copy_directory;
|
||||||
|
|
||||||
mod copydir;
|
mod copydir;
|
||||||
mod platform;
|
mod platform;
|
||||||
|
|
||||||
quick_error! {
|
quick_error! {
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
|
@ -108,12 +109,8 @@ impl UError for Error {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type CopyResult<T> = Result<T, Error>;
|
pub type CopyResult<T> = Result<T, Error>;
|
||||||
pub type Source = PathBuf;
|
|
||||||
pub type SourceSlice = Path;
|
|
||||||
pub type Target = PathBuf;
|
|
||||||
pub type TargetSlice = Path;
|
|
||||||
|
|
||||||
/// Specifies whether when overwrite files
|
/// Specifies how to overwrite files.
|
||||||
#[derive(Clone, Copy, Eq, PartialEq)]
|
#[derive(Clone, Copy, Eq, PartialEq)]
|
||||||
pub enum ClobberMode {
|
pub enum ClobberMode {
|
||||||
Force,
|
Force,
|
||||||
|
@ -121,7 +118,7 @@ pub enum ClobberMode {
|
||||||
Standard,
|
Standard,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Specifies whether when overwrite files
|
/// Specifies whether files should be overwritten.
|
||||||
#[derive(Clone, Copy, Eq, PartialEq)]
|
#[derive(Clone, Copy, Eq, PartialEq)]
|
||||||
pub enum OverwriteMode {
|
pub enum OverwriteMode {
|
||||||
/// [Default] Always overwrite existing files
|
/// [Default] Always overwrite existing files
|
||||||
|
@ -148,12 +145,13 @@ pub enum SparseMode {
|
||||||
Never,
|
Never,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Specifies the expected file type of copy target
|
/// The expected file type of copy target
|
||||||
pub enum TargetType {
|
pub enum TargetType {
|
||||||
Directory,
|
Directory,
|
||||||
File,
|
File,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Copy action to perform
|
||||||
pub enum CopyMode {
|
pub enum CopyMode {
|
||||||
Link,
|
Link,
|
||||||
SymLink,
|
SymLink,
|
||||||
|
@ -162,6 +160,7 @@ pub enum CopyMode {
|
||||||
AttrOnly,
|
AttrOnly,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Preservation settings for various attributes
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Attributes {
|
pub struct Attributes {
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
|
@ -209,30 +208,76 @@ impl Preserve {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Re-usable, extensible copy options
|
/// Options for the `cp` command
|
||||||
|
///
|
||||||
|
/// All options are public so that the options can be programmatically
|
||||||
|
/// constructed by other crates, such as nushell. That means that this struct
|
||||||
|
/// is part of our public API. It should therefore not be changed without good
|
||||||
|
/// reason.
|
||||||
|
///
|
||||||
|
/// The fields are documented with the arguments that determine their value.
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub struct Options {
|
pub struct Options {
|
||||||
attributes_only: bool,
|
/// `--attributes-only`
|
||||||
backup: BackupMode,
|
pub attributes_only: bool,
|
||||||
copy_contents: bool,
|
/// `--backup[=CONTROL]`, `-b`
|
||||||
cli_dereference: bool,
|
pub backup: BackupMode,
|
||||||
copy_mode: CopyMode,
|
/// `--copy-contents`
|
||||||
dereference: bool,
|
pub copy_contents: bool,
|
||||||
no_target_dir: bool,
|
/// `-H`
|
||||||
one_file_system: bool,
|
pub cli_dereference: bool,
|
||||||
overwrite: OverwriteMode,
|
/// Determines the type of copying that should be done
|
||||||
parents: bool,
|
///
|
||||||
sparse_mode: SparseMode,
|
/// Set by the following arguments:
|
||||||
strip_trailing_slashes: bool,
|
/// - `-l`, `--link`: [`CopyMode::Link`]
|
||||||
reflink_mode: ReflinkMode,
|
/// - `-s`, `--symbolic-link`: [`CopyMode::SymLink`]
|
||||||
attributes: Attributes,
|
/// - `-u`, `--update[=WHEN]`: [`CopyMode::Update`]
|
||||||
recursive: bool,
|
/// - `--attributes-only`: [`CopyMode::AttrOnly`]
|
||||||
backup_suffix: String,
|
/// - otherwise: [`CopyMode::Copy`]
|
||||||
target_dir: Option<PathBuf>,
|
pub copy_mode: CopyMode,
|
||||||
update: UpdateMode,
|
/// `-L`, `--dereference`
|
||||||
debug: bool,
|
pub dereference: bool,
|
||||||
verbose: bool,
|
/// `-T`, `--no-target-dir`
|
||||||
progress_bar: bool,
|
pub no_target_dir: bool,
|
||||||
|
/// `-x`, `--one-file-system`
|
||||||
|
pub one_file_system: bool,
|
||||||
|
/// Specifies what to do with an existing destination
|
||||||
|
///
|
||||||
|
/// Set by the following arguments:
|
||||||
|
/// - `-i`, `--interactive`: [`OverwriteMode::Interactive`]
|
||||||
|
/// - `-n`, `--no-clobber`: [`OverwriteMode::NoClobber`]
|
||||||
|
/// - otherwise: [`OverwriteMode::Clobber`]
|
||||||
|
///
|
||||||
|
/// The `Interactive` and `Clobber` variants have a [`ClobberMode`] argument,
|
||||||
|
/// set by the following arguments:
|
||||||
|
/// - `-f`, `--force`: [`ClobberMode::Force`]
|
||||||
|
/// - `--remove-destination`: [`ClobberMode::RemoveDestination`]
|
||||||
|
/// - otherwise: [`ClobberMode::Standard`]
|
||||||
|
pub overwrite: OverwriteMode,
|
||||||
|
/// `--parents`
|
||||||
|
pub parents: bool,
|
||||||
|
/// `--sparse[=WHEN]`
|
||||||
|
pub sparse_mode: SparseMode,
|
||||||
|
/// `--strip-trailing-slashes`
|
||||||
|
pub strip_trailing_slashes: bool,
|
||||||
|
/// `--reflink[=WHEN]`
|
||||||
|
pub reflink_mode: ReflinkMode,
|
||||||
|
/// `--preserve=[=ATTRIBUTE_LIST]` and `--no-preserve=ATTRIBUTE_LIST`
|
||||||
|
pub attributes: Attributes,
|
||||||
|
/// `-R`, `-r`, `--recursive`
|
||||||
|
pub recursive: bool,
|
||||||
|
/// `-S`, `--suffix`
|
||||||
|
pub backup_suffix: String,
|
||||||
|
/// `-t`, `--target-directory`
|
||||||
|
pub target_dir: Option<PathBuf>,
|
||||||
|
/// `--update[=UPDATE]`
|
||||||
|
pub update: UpdateMode,
|
||||||
|
/// `--debug`
|
||||||
|
pub debug: bool,
|
||||||
|
/// `-v`, `--verbose`
|
||||||
|
pub verbose: bool,
|
||||||
|
/// `-g`, `--progress`
|
||||||
|
pub progress_bar: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Enum representing various debug states of the offload and reflink actions.
|
/// Enum representing various debug states of the offload and reflink actions.
|
||||||
|
@ -1000,7 +1045,7 @@ impl TargetType {
|
||||||
///
|
///
|
||||||
/// Treat target as a dir if we have multiple sources or the target
|
/// Treat target as a dir if we have multiple sources or the target
|
||||||
/// exists and already is a directory
|
/// exists and already is a directory
|
||||||
fn determine(sources: &[Source], target: &TargetSlice) -> Self {
|
fn determine(sources: &[PathBuf], target: &Path) -> Self {
|
||||||
if sources.len() > 1 || target.is_dir() {
|
if sources.len() > 1 || target.is_dir() {
|
||||||
Self::Directory
|
Self::Directory
|
||||||
} else {
|
} else {
|
||||||
|
@ -1010,7 +1055,10 @@ impl TargetType {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns tuple of (Source paths, Target)
|
/// Returns tuple of (Source paths, Target)
|
||||||
fn parse_path_args(mut paths: Vec<Source>, options: &Options) -> CopyResult<(Vec<Source>, Target)> {
|
fn parse_path_args(
|
||||||
|
mut paths: Vec<PathBuf>,
|
||||||
|
options: &Options,
|
||||||
|
) -> CopyResult<(Vec<PathBuf>, PathBuf)> {
|
||||||
if paths.is_empty() {
|
if paths.is_empty() {
|
||||||
// No files specified
|
// No files specified
|
||||||
return Err("missing file operand".into());
|
return Err("missing file operand".into());
|
||||||
|
@ -1122,14 +1170,13 @@ fn show_error_if_needed(error: &Error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Copy all `sources` to `target`. Returns an
|
/// Copy all `sources` to `target`.
|
||||||
/// `Err(Error::NotAllFilesCopied)` if at least one non-fatal error was
|
|
||||||
/// encountered.
|
|
||||||
///
|
///
|
||||||
/// Behavior depends on path`options`, see [`Options`] for details.
|
/// Returns an `Err(Error::NotAllFilesCopied)` if at least one non-fatal error
|
||||||
|
/// was encountered.
|
||||||
///
|
///
|
||||||
/// [`Options`]: ./struct.Options.html
|
/// Behavior is determined by the `options` parameter, see [`Options`] for details.
|
||||||
fn copy(sources: &[Source], target: &TargetSlice, options: &Options) -> CopyResult<()> {
|
pub fn copy(sources: &[PathBuf], target: &Path, options: &Options) -> CopyResult<()> {
|
||||||
let target_type = TargetType::determine(sources, target);
|
let target_type = TargetType::determine(sources, target);
|
||||||
verify_target_type(target, &target_type)?;
|
verify_target_type(target, &target_type)?;
|
||||||
|
|
||||||
|
@ -1192,7 +1239,7 @@ fn copy(sources: &[Source], target: &TargetSlice, options: &Options) -> CopyResu
|
||||||
|
|
||||||
fn construct_dest_path(
|
fn construct_dest_path(
|
||||||
source_path: &Path,
|
source_path: &Path,
|
||||||
target: &TargetSlice,
|
target: &Path,
|
||||||
target_type: &TargetType,
|
target_type: &TargetType,
|
||||||
options: &Options,
|
options: &Options,
|
||||||
) -> CopyResult<PathBuf> {
|
) -> CopyResult<PathBuf> {
|
||||||
|
@ -1223,8 +1270,8 @@ fn construct_dest_path(
|
||||||
|
|
||||||
fn copy_source(
|
fn copy_source(
|
||||||
progress_bar: &Option<ProgressBar>,
|
progress_bar: &Option<ProgressBar>,
|
||||||
source: &SourceSlice,
|
source: &Path,
|
||||||
target: &TargetSlice,
|
target: &Path,
|
||||||
target_type: &TargetType,
|
target_type: &TargetType,
|
||||||
options: &Options,
|
options: &Options,
|
||||||
symlinked_files: &mut HashSet<FileInformation>,
|
symlinked_files: &mut HashSet<FileInformation>,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue