nushell/crates/nu-command/src/filesystem/ucp.rs
WindSoilder d646903161
Unify glob behavior on open, rm, cp-old, mv, umv, cp and du commands (#11621)
# Description
This pr is a follow up to
[#11569](https://github.com/nushell/nushell/pull/11569#issuecomment-1902279587)
> Revert the logic in https://github.com/nushell/nushell/pull/10694 and
apply the logic in this pr to mv, cp, rv will require a larger change, I
need to think how to achieve the bahavior

And sorry @bobhy for reverting some of your changes.

This pr is going to unify glob behavior on the given commands:
* open
* rm
* cp-old
* mv
* umv
* cp
* du

So they have the same behavior to `ls`, which is:
If given parameter is quoted by single quote(`'`) or double quote(`"`),
don't auto-expand the glob pattern. If not quoted, auto-expand the glob
pattern.

Fixes: #9558  Fixes: #10211 Fixes: #9310 Fixes: #10364 

# TODO
But there is one thing remains: if we give a variable to the command, it
will always auto-expand the glob pattern, e.g:
```nushell
let path = "a[123]b"
rm $path
```
I don't think it's expected. But I also think user might want to
auto-expand the glob pattern in variables.

So I'll introduce a new command called `glob escape`, then if user
doesn't want to auto-expand the glob pattern, he can just do this: `rm
($path | glob escape)`

# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->

# Tests + Formatting
Done

# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->

## NOTE
This pr changes the semantic of `GlobPattern`, before this pr, it will
`expand path` after evaluated, this makes `nu_engine::glob_from` have no
chance to glob things right if a path contains glob pattern.

e.g: [#9310
](https://github.com/nushell/nushell/issues/9310#issuecomment-1886824030)
#10211

I think changing the semantic is fine, because it makes glob works if
path contains something like '*'.

It maybe a breaking change if a custom command's argument are annotated
by `: glob`.
2024-01-26 21:57:35 +08:00

369 lines
13 KiB
Rust

use nu_engine::{current_dir, CallExt};
use nu_protocol::NuPath;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, Example, PipelineData, ShellError, Signature, Spanned, SyntaxShape, Type, Value,
};
use std::path::PathBuf;
use uu_cp::{BackupMode, CopyMode, UpdateMode};
// TODO: related to uucore::error::set_exit_code(EXIT_ERR)
// const EXIT_ERR: i32 = 1;
#[cfg(not(target_os = "windows"))]
const PATH_SEPARATOR: &str = "/";
#[cfg(target_os = "windows")]
const PATH_SEPARATOR: &str = "\\";
#[derive(Clone)]
pub struct UCp;
impl Command for UCp {
fn name(&self) -> &str {
"cp"
}
fn usage(&self) -> &str {
"Copy files using uutils/coreutils cp."
}
fn search_terms(&self) -> Vec<&str> {
vec!["copy", "file", "files", "coreutils"]
}
fn signature(&self) -> Signature {
Signature::build("cp")
.input_output_types(vec![(Type::Nothing, Type::Nothing)])
.switch("recursive", "copy directories recursively", Some('r'))
.switch("verbose", "explicitly state what is being done", Some('v'))
.switch(
"force",
"if an existing destination file cannot be opened, remove it and try
again (this option is ignored when the -n option is also used).
currently not implemented for windows",
Some('f'),
)
.switch("interactive", "ask before overwriting files", Some('i'))
.switch(
"update",
"copy only when the SOURCE file is newer than the destination file or when the destination file is missing",
Some('u')
)
.switch("progress", "display a progress bar", Some('p'))
.switch("no-clobber", "do not overwrite an existing file", Some('n'))
.named(
"preserve",
SyntaxShape::List(Box::new(SyntaxShape::String)),
"preserve only the specified attributes (empty list means no attributes preserved)
if not specified only mode is preserved
possible values: mode, ownership (unix only), timestamps, context, link, links, xattr",
None
)
.switch("debug", "explain how a file is copied. Implies -v", None)
.rest("paths", SyntaxShape::GlobPattern, "Copy SRC file/s to DEST.")
.allow_variants_without_examples(true)
.category(Category::FileSystem)
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Copy myfile to dir_b",
example: "cp myfile dir_b",
result: None,
},
Example {
description: "Recursively copy dir_a to dir_b",
example: "cp -r dir_a dir_b",
result: None,
},
Example {
description: "Recursively copy dir_a to dir_b, and print the feedbacks",
example: "cp -r -v dir_a dir_b",
result: None,
},
Example {
description: "Move many files into a directory",
example: "cp *.txt dir_a",
result: None,
},
Example {
description: "Copy only if source file is newer than target file",
example: "cp -u a b",
result: None,
},
Example {
description: "Copy file preserving mode and timestamps attributes",
example: "cp --preserve [ mode timestamps ] a b",
result: None,
},
Example {
description: "Copy file erasing all attributes",
example: "cp --preserve [] a b",
result: None,
},
]
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
let interactive = call.has_flag(engine_state, stack, "interactive")?;
let (update, copy_mode) = if call.has_flag(engine_state, stack, "update")? {
(UpdateMode::ReplaceIfOlder, CopyMode::Update)
} else {
(UpdateMode::ReplaceAll, CopyMode::Copy)
};
let force = call.has_flag(engine_state, stack, "force")?;
let no_clobber = call.has_flag(engine_state, stack, "no-clobber")?;
let progress = call.has_flag(engine_state, stack, "progress")?;
let recursive = call.has_flag(engine_state, stack, "recursive")?;
let verbose = call.has_flag(engine_state, stack, "verbose")?;
let preserve: Option<Value> = call.get_flag(engine_state, stack, "preserve")?;
let debug = call.has_flag(engine_state, stack, "debug")?;
let overwrite = if no_clobber {
uu_cp::OverwriteMode::NoClobber
} else if interactive {
if force {
uu_cp::OverwriteMode::Interactive(uu_cp::ClobberMode::Force)
} else {
uu_cp::OverwriteMode::Interactive(uu_cp::ClobberMode::Standard)
}
} else if force {
uu_cp::OverwriteMode::Clobber(uu_cp::ClobberMode::Force)
} else {
uu_cp::OverwriteMode::Clobber(uu_cp::ClobberMode::Standard)
};
#[cfg(any(target_os = "linux", target_os = "android", target_os = "macos"))]
let reflink_mode = uu_cp::ReflinkMode::Auto;
#[cfg(not(any(target_os = "linux", target_os = "android", target_os = "macos")))]
let reflink_mode = uu_cp::ReflinkMode::Never;
let mut paths: Vec<Spanned<NuPath>> = call.rest(engine_state, stack, 0)?;
if paths.is_empty() {
return Err(ShellError::GenericError {
error: "Missing file operand".into(),
msg: "Missing file operand".into(),
span: Some(call.head),
help: Some("Please provide source and destination paths".into()),
inner: vec![],
});
}
if paths.len() == 1 {
return Err(ShellError::GenericError {
error: "Missing destination path".into(),
msg: format!(
"Missing destination path operand after {}",
paths[0].item.as_ref()
),
span: Some(paths[0].span),
help: None,
inner: vec![],
});
}
let target = paths.pop().expect("Should not be reached?");
let target_path = PathBuf::from(&nu_utils::strip_ansi_string_unlikely(
target.item.to_string(),
));
if target.item.as_ref().ends_with(PATH_SEPARATOR) && !target_path.is_dir() {
return Err(ShellError::GenericError {
error: "is not a directory".into(),
msg: "is not a directory".into(),
span: Some(target.span),
help: None,
inner: vec![],
});
};
// paths now contains the sources
let cwd = current_dir(engine_state, stack)?;
let mut sources: Vec<PathBuf> = Vec::new();
for mut p in paths {
p.item = p.item.strip_ansi_string_unlikely();
let exp_files: Vec<Result<PathBuf, ShellError>> =
nu_engine::glob_from(&p, &cwd, call.head, None)
.map(|f| f.1)?
.collect();
if exp_files.is_empty() {
return Err(ShellError::FileNotFound { span: p.span });
};
let mut app_vals: Vec<PathBuf> = Vec::new();
for v in exp_files {
match v {
Ok(path) => {
if !recursive && path.is_dir() {
return Err(ShellError::GenericError {
error: "could_not_copy_directory".into(),
msg: "resolves to a directory (not copied)".into(),
span: Some(p.span),
help: Some(
"Directories must be copied using \"--recursive\"".into(),
),
inner: vec![],
});
};
app_vals.push(path)
}
Err(e) => return Err(e),
}
}
sources.append(&mut app_vals);
}
// Make sure to send absolute paths to avoid uu_cp looking for cwd in std::env which is not
// supported in Nushell
for src in sources.iter_mut() {
if !src.is_absolute() {
*src = nu_path::expand_path_with(&src, &cwd);
}
}
let target_path = nu_path::expand_path_with(&target_path, &cwd);
let attributes = make_attributes(preserve)?;
let options = uu_cp::Options {
overwrite,
reflink_mode,
recursive,
debug,
attributes,
verbose: verbose || debug,
dereference: !recursive,
progress_bar: progress,
attributes_only: false,
backup: BackupMode::NoBackup,
copy_contents: false,
cli_dereference: false,
copy_mode,
no_target_dir: false,
one_file_system: false,
parents: false,
sparse_mode: uu_cp::SparseMode::Auto,
strip_trailing_slashes: false,
backup_suffix: String::from("~"),
target_dir: None,
update,
};
if let Err(error) = uu_cp::copy(&sources, &target_path, &options) {
match error {
// code should still be EXIT_ERR as does GNU cp
uu_cp::Error::NotAllFilesCopied => {}
_ => {
return Err(ShellError::GenericError {
error: format!("{}", error),
msg: format!("{}", error),
span: None,
help: None,
inner: vec![],
})
}
};
// TODO: What should we do in place of set_exit_code?
// uucore::error::set_exit_code(EXIT_ERR);
}
Ok(PipelineData::empty())
}
}
const ATTR_UNSET: uu_cp::Preserve = uu_cp::Preserve::No { explicit: true };
const ATTR_SET: uu_cp::Preserve = uu_cp::Preserve::Yes { required: true };
fn make_attributes(preserve: Option<Value>) -> Result<uu_cp::Attributes, ShellError> {
if let Some(preserve) = preserve {
let mut attributes = uu_cp::Attributes {
#[cfg(any(target_os = "linux", target_os = "android", target_os = "macos"))]
ownership: ATTR_UNSET,
mode: ATTR_UNSET,
timestamps: ATTR_UNSET,
context: ATTR_UNSET,
links: ATTR_UNSET,
xattr: ATTR_UNSET,
};
parse_and_set_attributes_list(&preserve, &mut attributes)?;
Ok(attributes)
} else {
// By default preseerve only mode
Ok(uu_cp::Attributes {
mode: ATTR_SET,
#[cfg(any(target_os = "linux", target_os = "android", target_os = "macos"))]
ownership: ATTR_UNSET,
timestamps: ATTR_UNSET,
context: ATTR_UNSET,
links: ATTR_UNSET,
xattr: ATTR_UNSET,
})
}
}
fn parse_and_set_attributes_list(
list: &Value,
attribute: &mut uu_cp::Attributes,
) -> Result<(), ShellError> {
match list {
Value::List { vals, .. } => {
for val in vals {
parse_and_set_attribute(val, attribute)?;
}
Ok(())
}
_ => Err(ShellError::IncompatibleParametersSingle {
msg: "--preserve flag expects a list of strings".into(),
span: list.span(),
}),
}
}
fn parse_and_set_attribute(
value: &Value,
attribute: &mut uu_cp::Attributes,
) -> Result<(), ShellError> {
match value {
Value::String { val, .. } => {
let attribute = match val.as_str() {
"mode" => &mut attribute.mode,
#[cfg(any(target_os = "linux", target_os = "android", target_os = "macos"))]
"ownership" => &mut attribute.ownership,
"timestamps" => &mut attribute.timestamps,
"context" => &mut attribute.context,
"link" | "links" => &mut attribute.links,
"xattr" => &mut attribute.xattr,
_ => {
return Err(ShellError::IncompatibleParametersSingle {
msg: format!("--preserve flag got an unexpected attribute \"{}\"", val),
span: value.span(),
});
}
};
*attribute = ATTR_SET;
Ok(())
}
_ => Err(ShellError::IncompatibleParametersSingle {
msg: "--preserve flag expects a list of strings".into(),
span: value.span(),
}),
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_examples() {
use crate::test_examples;
test_examples(UCp {})
}
}