mirror of
https://github.com/uutils/coreutils
synced 2024-12-14 07:12:44 +00:00
du: refactor pattern building in du
This commit is contained in:
parent
defde8c91e
commit
898689d924
1 changed files with 72 additions and 104 deletions
|
@ -488,55 +488,28 @@ fn file_as_vec(filename: impl AsRef<Path>) -> Vec<String> {
|
|||
|
||||
// Given the --exclude-from and/or --exclude arguments, returns the globset lists
|
||||
// to ignore the files
|
||||
fn get_glob_ignore(matches: &ArgMatches) -> UResult<Vec<Pattern>> {
|
||||
let mut excludes_from = if matches.contains_id(options::EXCLUDE_FROM) {
|
||||
match matches.values_of(options::EXCLUDE_FROM) {
|
||||
Some(all_files) => {
|
||||
let mut exclusion = Vec::<String>::new();
|
||||
// Read the exclude lists from all the files
|
||||
// and add them into a vector of string
|
||||
let files: Vec<String> = all_files.clone().map(|v| v.to_owned()).collect();
|
||||
for f in files {
|
||||
exclusion.extend(file_as_vec(&f));
|
||||
}
|
||||
exclusion
|
||||
}
|
||||
None => Vec::<String>::new(),
|
||||
}
|
||||
} else {
|
||||
Vec::<String>::new()
|
||||
};
|
||||
fn build_exclude_patterns(matches: &ArgMatches) -> UResult<Vec<Pattern>> {
|
||||
let exclude_from_iterator = matches
|
||||
.values_of(options::EXCLUDE_FROM)
|
||||
.unwrap_or_default()
|
||||
.flat_map(|f| file_as_vec(&f));
|
||||
|
||||
let mut excludes = if matches.contains_id(options::EXCLUDE) {
|
||||
match matches.values_of(options::EXCLUDE) {
|
||||
Some(v) => {
|
||||
// Read the various arguments
|
||||
v.clone().map(|v| v.to_owned()).collect()
|
||||
}
|
||||
None => Vec::<String>::new(),
|
||||
}
|
||||
} else {
|
||||
Vec::<String>::new()
|
||||
};
|
||||
let excludes_iterator = matches
|
||||
.values_of(options::EXCLUDE)
|
||||
.unwrap_or_default()
|
||||
.map(|v| v.to_owned());
|
||||
|
||||
// Merge the two lines
|
||||
excludes.append(&mut excludes_from);
|
||||
if !&excludes.is_empty() {
|
||||
let mut builder = Vec::new();
|
||||
// Create the `Vec` of excludes
|
||||
for f in excludes {
|
||||
if matches.contains_id(options::VERBOSE) {
|
||||
let mut exclude_patterns = Vec::new();
|
||||
for f in excludes_iterator.chain(exclude_from_iterator) {
|
||||
if matches.is_present(options::VERBOSE) {
|
||||
println!("adding {:?} to the exclude list ", &f);
|
||||
}
|
||||
match Pattern::new(&f) {
|
||||
Ok(glob) => builder.push(glob),
|
||||
Ok(glob) => exclude_patterns.push(glob),
|
||||
Err(err) => return Err(DuError::InvalidGlob(err.to_string()).into()),
|
||||
};
|
||||
}
|
||||
Ok(builder)
|
||||
} else {
|
||||
Ok(Vec::new())
|
||||
}
|
||||
Ok(exclude_patterns)
|
||||
}
|
||||
|
||||
#[uucore::main]
|
||||
|
@ -615,14 +588,13 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
|||
"\n"
|
||||
};
|
||||
|
||||
let excludes = get_glob_ignore(&matches)?;
|
||||
let excludes = build_exclude_patterns(&matches)?;
|
||||
|
||||
let mut grand_total = 0;
|
||||
'loop_file: for path_string in files {
|
||||
// Skip if we don't want to ignore anything
|
||||
if !&excludes.is_empty() {
|
||||
for pattern in &excludes {
|
||||
{
|
||||
if pattern.matches(path_string) {
|
||||
// if the directory is ignored, leave early
|
||||
if options.verbose {
|
||||
|
@ -632,11 +604,9 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let path = PathBuf::from(&path_string);
|
||||
match Stat::new(path, &options) {
|
||||
Ok(stat) => {
|
||||
if let Ok(stat) = Stat::new(path, &options) {
|
||||
let mut inodes: HashSet<FileInfo> = HashSet::new();
|
||||
if let Some(inode) = stat.inode {
|
||||
inodes.insert(inode);
|
||||
|
@ -651,7 +621,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
|||
continue;
|
||||
}
|
||||
|
||||
if matches.contains_id(options::TIME) {
|
||||
if matches.is_present(options::TIME) {
|
||||
let tm = {
|
||||
let secs = {
|
||||
match matches.value_of(options::TIME) {
|
||||
|
@ -686,8 +656,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
|||
grand_total += size;
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(_) => {
|
||||
} else {
|
||||
show_error!(
|
||||
"{}: {}",
|
||||
path_string.maybe_quote(),
|
||||
|
@ -695,7 +664,6 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
|||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if options.total {
|
||||
print!("{}\ttotal", convert_size(grand_total));
|
||||
|
|
Loading…
Reference in a new issue