mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 19:37:45 +02:00
Allow filesystem commands to access files with glob metachars in name (#10694)
(squashed version of #10557, clean commit history and review thread) Fixes #10571, also potentially: #10364, #10211, #9558, #9310, # Description Changes processing of arguments to filesystem commands that are source paths or globs. Applies to `cp, cp-old, mv, rm, du` but not `ls` (because it uses a different globbing interface) or `glob` (because it uses a different globbing library). The core of the change is to lookup the argument first as a file and only glob if it is not. That way, a path containing glob metacharacters can be referenced without glob quoting, though it will have to be single quoted to avoid nushell parsing. Before: A file path that looks like a glob is not matched by the glob specified as a (source) argument and takes some thinking about to access. You might say the glob pattern shadows a file with the same spelling. ``` > ls a* ╭───┬────────┬──────┬──────┬────────────────╮ │ # │ name │ type │ size │ modified │ ├───┼────────┼──────┼──────┼────────────────┤ │ 0 │ a[bc]d │ file │ 0 B │ 34 seconds ago │ │ 1 │ abd │ file │ 0 B │ now │ │ 2 │ acd │ file │ 0 B │ now │ ╰───┴────────┴──────┴──────┴────────────────╯ > cp --verbose 'a[bc]d' dest copied /home/bobhy/src/rust/work/r4/abd to /home/bobhy/src/rust/work/r4/dest/abd copied /home/bobhy/src/rust/work/r4/acd to /home/bobhy/src/rust/work/r4/dest/acd > ## Note -- a[bc]d *not* copied, and seemingly hard to access. > cp --verbose 'a\[bc\]d' dest Error: × No matches found ╭─[entry #33:1:1] 1 │ cp --verbose 'a\[bc\]d' dest · ─────┬──── · ╰── no matches found ╰──── > #.. but is accessible with enough glob quoting. > cp --verbose 'a[[]bc[]]d' dest copied /home/bobhy/src/rust/work/r4/a[bc]d to /home/bobhy/src/rust/work/r4/dest/a[bc]d ``` Before_2: if file has glob metachars but isn't a valid pattern, user gets a confusing error: ``` > touch 'a[b' > cp 'a[b' dest Error: × Pattern syntax error near position 30: invalid range pattern ╭─[entry #13:1:1] 1 │ cp 'a[b' dest · ──┬── · ╰── invalid pattern ╰──── ``` After: Args to cp, mv, etc. are tried first as literal files, and only as globs if not found to be files. ``` > cp --verbose 'a[bc]d' dest copied /home/bobhy/src/rust/work/r4/a[bc]d to /home/bobhy/src/rust/work/r4/dest/a[bc]d > cp --verbose '[a][bc]d' dest copied /home/bobhy/src/rust/work/r4/abd to /home/bobhy/src/rust/work/r4/dest/abd copied /home/bobhy/src/rust/work/r4/acd to /home/bobhy/src/rust/work/r4/dest/acd ``` After_2: file with glob metachars but invalid pattern just works. (though Windows does not allow file name to contain `*`.). ``` > cp --verbose 'a[b' dest copied /home/bobhy/src/rust/work/r4/a[b to /home/bobhy/src/rust/work/r4/dest/a[b ``` So, with this fix, a file shadows a glob pattern with the same spelling. If you have such a file and really want to use the glob pattern, you will have to glob quote some of the characters in the pattern. I think that's less confusing to the user: if ls shows a file with a weird name, s/he'll still be able to copy, rename or delete it. # User-Facing Changes Could break some existing scripts. If user happened to have a file with a globbish name but was using a glob pattern with the same spelling, the new version will process the file and not expand the glob. # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass (on Windows make sure to [enable developer mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging)) - `cargo run -- -c "use std testing; testing run-tests --path crates/nu-std"` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # 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. --> --------- Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
This commit is contained in:
@ -103,6 +103,19 @@ pub struct Paths {
|
||||
scope: Option<PathBuf>,
|
||||
}
|
||||
|
||||
impl Paths {
|
||||
/// An iterator representing a single path.
|
||||
pub fn single(path: &Path, relative_to: &Path) -> Self {
|
||||
Paths {
|
||||
dir_patterns: vec![Pattern::new("*").expect("hard coded pattern")],
|
||||
require_dir: false,
|
||||
options: MatchOptions::default(),
|
||||
todo: vec![Ok((path.to_path_buf(), 0))],
|
||||
scope: Some(relative_to.into()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Return an iterator that produces all the `Path`s that match the given
|
||||
/// pattern using default match options, which may be absolute or relative to
|
||||
/// the current working directory.
|
||||
@ -110,7 +123,7 @@ pub struct Paths {
|
||||
/// This may return an error if the pattern is invalid.
|
||||
///
|
||||
/// This method uses the default match options and is equivalent to calling
|
||||
/// `glob_with(pattern, MatchOptions::new())`. Use `glob_with` directly if you
|
||||
/// `glob_with(pattern, MatchOptions::default())`. Use `glob_with` directly if you
|
||||
/// want to use non-default match options.
|
||||
///
|
||||
/// When iterating, each result is a `GlobResult` which expresses the
|
||||
@ -273,6 +286,32 @@ pub fn glob_with(pattern: &str, options: MatchOptions) -> Result<Paths, PatternE
|
||||
})
|
||||
}
|
||||
|
||||
/// Return an iterator that produces all the `Path`s that match the given
|
||||
/// pattern relative to a specified parent directory and using specified match options.
|
||||
/// Paths may be absolute or relative to the current working directory.
|
||||
///
|
||||
/// This is provided primarily for testability, so multithreaded test runners can
|
||||
/// test pattern matches in different test directories at the same time without
|
||||
/// having to append the parent to the pattern under test.
|
||||
|
||||
pub fn glob_with_parent(
|
||||
pattern: &str,
|
||||
options: MatchOptions,
|
||||
parent: &Path,
|
||||
) -> Result<Paths, PatternError> {
|
||||
match glob_with(pattern, options) {
|
||||
Ok(mut p) => {
|
||||
p.scope = match p.scope {
|
||||
None => Some(parent.to_path_buf()),
|
||||
Some(s) if &s.to_string_lossy() == "." => Some(parent.to_path_buf()),
|
||||
Some(s) => Some(s),
|
||||
};
|
||||
Ok(p)
|
||||
}
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
/// A glob iteration error.
|
||||
///
|
||||
/// This is typically returned when a particular path cannot be read
|
||||
@ -347,7 +386,10 @@ impl Iterator for Paths {
|
||||
// Shouldn't happen, but we're using -1 as a special index.
|
||||
assert!(self.dir_patterns.len() < !0);
|
||||
|
||||
fill_todo(&mut self.todo, &self.dir_patterns, 0, &scope, self.options);
|
||||
// if there's one prefilled result, take it, otherwise fill the todo buffer
|
||||
if self.todo.len() != 1 {
|
||||
fill_todo(&mut self.todo, &self.dir_patterns, 0, &scope, self.options);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -704,7 +746,7 @@ impl Pattern {
|
||||
}
|
||||
|
||||
/// Return if the given `str` matches this `Pattern` using the default
|
||||
/// match options (i.e. `MatchOptions::new()`).
|
||||
/// match options (i.e. `MatchOptions::default()`).
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
@ -720,7 +762,7 @@ impl Pattern {
|
||||
}
|
||||
|
||||
/// Return if the given `Path`, when converted to a `str`, matches this
|
||||
/// `Pattern` using the default match options (i.e. `MatchOptions::new()`).
|
||||
/// `Pattern` using the default match options (i.e. `MatchOptions::default()`).
|
||||
pub fn matches_path(&self, path: &Path) -> bool {
|
||||
// FIXME (#9639): This needs to handle non-utf8 paths
|
||||
path.to_str().map_or(false, |s| self.matches(s))
|
||||
|
Reference in New Issue
Block a user