fix: hidden don't works on some files (#188)

like --hidden '*.abc-cba' matches xyz.abc-cba but do not matches 123.xyz.abc-cba
This commit is contained in:
sigoden 2023-03-03 07:15:46 +08:00 committed by GitHub
parent 45f4f5fc58
commit 0e12b285cd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 47 deletions

7
Cargo.lock generated
View file

@ -444,6 +444,7 @@ dependencies = [
"diqwest", "diqwest",
"form_urlencoded", "form_urlencoded",
"futures", "futures",
"glob",
"headers", "headers",
"hyper", "hyper",
"if-addrs", "if-addrs",
@ -683,6 +684,12 @@ dependencies = [
"wasi 0.11.0+wasi-snapshot-preview1", "wasi 0.11.0+wasi-snapshot-preview1",
] ]
[[package]]
name = "glob"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b"
[[package]] [[package]]
name = "globset" name = "globset"
version = "0.4.10" version = "0.4.10"

View file

@ -43,6 +43,7 @@ alphanumeric-sort = "1.4"
content_inspector = "0.2" content_inspector = "0.2"
anyhow = "1.0" anyhow = "1.0"
chardetng = "0.1" chardetng = "0.1"
glob = "0.3.1"
[features] [features]
default = ["tls"] default = ["tls"]

View file

@ -243,21 +243,18 @@ Since dufs only allows viewing/downloading, `admin` can only view/download files
### Hide Paths ### Hide Paths
Dufs supports hiding paths from directory listings via option `--hidden`. Dufs supports hiding paths from directory listings via option `--hidden <glob>,...`.
``` ```
dufs --hidden .git,.DS_Store,tmp dufs --hidden .git,.DS_Store,tmp
``` ```
`--hidden` also supports a variant glob: > The glob used in --hidden only matches file and directory names, not paths. So `--hidden dir1/file` is invalid.
- `?` matches any single character
- `*` matches any (possibly empty) sequence of characters
- `**`, `[..]`, `[!..]` is not supported
```sh ```sh
dufs --hidden '.*' dufs --hidden '.*' # hidden dotfiles
dufs --hidden '*.log,*.lock' dufs --hidden '*/' # hidden all folders
dufs --hidden '*.log,*.lock' # hidden by exts
``` ```
### Log Format ### Log Format

View file

@ -34,45 +34,12 @@ pub fn try_get_file_name(path: &Path) -> Result<&str> {
.ok_or_else(|| anyhow!("Failed to get file name of `{}`", path.display())) .ok_or_else(|| anyhow!("Failed to get file name of `{}`", path.display()))
} }
pub fn glob(source: &str, target: &str) -> bool { pub fn glob(pattern: &str, target: &str) -> bool {
let ss: Vec<char> = source.chars().collect(); let pat = match ::glob::Pattern::new(pattern) {
let mut iter = target.chars(); Ok(pat) => pat,
let mut i = 0; Err(_) => return false,
'outer: while i < ss.len() { };
let s = ss[i]; pat.matches(target)
match s {
'*' => match ss.get(i + 1) {
Some(s_next) => {
for t in iter.by_ref() {
if t == *s_next {
i += 2;
continue 'outer;
}
}
return false;
}
None => return true,
},
'?' => match iter.next() {
Some(_) => {
i += 1;
continue;
}
None => return false,
},
_ => match iter.next() {
Some(t) => {
if s == t {
i += 1;
continue;
}
return false;
}
None => return false,
},
}
}
iter.next().is_none()
} }
#[test] #[test]
@ -91,6 +58,10 @@ fn test_glob_key() {
assert!(!glob("abc", "abcd")); assert!(!glob("abc", "abcd"));
assert!(!glob("a?c", "abbc")); assert!(!glob("a?c", "abbc"));
assert!(!glob("*.log", "log")); assert!(!glob("*.log", "log"));
assert!(glob("*.abc-cba", "xyz.abc-cba"));
assert!(glob("*.abc-cba", "123.xyz.abc-cba"));
assert!(glob("*.log", ".log")); assert!(glob("*.log", ".log"));
assert!(glob("*.log", "a.log")); assert!(glob("*.log", "a.log"));
assert!(glob("*/", "abc/"));
assert!(!glob("*/", "abc"));
} }