This commit is contained in:
thePR0M3TH3AN
2025-05-18 21:28:05 -04:00
parent 6ddb40ca18
commit a7660df45f
16 changed files with 523 additions and 69 deletions

View File

@@ -3,16 +3,34 @@
use std::path::PathBuf;
/// Determine a filesystem root to limit recursive walking on glob scans.
///
/// If the pattern contains any of `*?[`, we take everything up to the
/// first such character, and then (if that still contains metacharacters)
/// walk up until there arent any left. If there are *no* metachars at
/// all, we treat the entire string as a path and return its parent
/// directory (or `.` if it has no parent).
pub fn determine_scan_root(pattern: &str) -> PathBuf {
// find first wildcard char
let first_wild = pattern
.find(|c| matches!(c, '*' | '?' | '['))
.unwrap_or(pattern.len());
let mut root = PathBuf::from(&pattern[..first_wild]);
// everything up to the wildcard (or the whole string if none)
let prefix = &pattern[..first_wild];
let mut root = PathBuf::from(prefix);
// If there were NO wildcards at all, just return the parent directory
if first_wild == pattern.len() {
return root.parent().map(|p| p.to_path_buf()).unwrap_or_else(|| PathBuf::from("."));
}
// Otherwise, if the prefix still has any wildcards (e.g. "foo*/bar"),
// walk back up until it doesnt
while root
.as_os_str()
.to_string_lossy()
.contains(|c| matches!(c, '*' | '?' | '['))
.chars()
.any(|c| matches!(c, '*' | '?' | '['))
{
root = root.parent().map(|p| p.to_path_buf()).unwrap_or_default();
}