Skip to content

Commit

Permalink
Add simple du example
Browse files Browse the repository at this point in the history
  • Loading branch information
jessegrosjean committed Jun 1, 2021
1 parent 5ee29f5 commit ae905c6
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions examples/du.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
extern crate jwalk;

use jwalk::{DirEntry, Error, WalkDirGeneric};
use std::env;
use std::fs::Metadata;

fn main() {
let path = env::args().skip(1).next().unwrap_or("./".to_owned());
let mut total: u64 = 0;

for dir_entry_result in WalkDirGeneric::<((), Option<u64>)>::new(&path).process_read_dir(
|_, _, _, dir_entry_results| {
dir_entry_results.iter_mut().for_each(|dir_entry_result| {
if let Ok(dir_entry) = dir_entry_result {
if !dir_entry.file_type.is_dir() {
dir_entry.client_state =
Some(dir_entry.metadata().map(|m| m.len()).unwrap_or_default());
}
}
})
},
) {
match dir_entry_result {
Ok(dir_entry) => {
if let Some(len) = &dir_entry.client_state {
total += len;
}
}
Err(error) => {
println!("Read dir_entry error: {}", error);
}
}
}

println!("path: {} total bytes: {}", path, total);
}

0 comments on commit ae905c6

Please sign in to comment.