diff options
author | Andrew Gallant <jamslam@gmail.com> | 2020-01-14 04:35:26 +0300 |
---|---|---|
committer | Andrew Gallant <jamslam@gmail.com> | 2020-01-14 04:59:07 +0300 |
commit | e4bd92f6a791f35593185539d91f9516161ab5ac (patch) | |
tree | c1a099e5a0949e6b21e271186fb7ea115d7c03d5 | |
parent | 9c838a67c16b06ce12be70e3a435043ececb25bf (diff) |
walkdir-list
-rw-r--r-- | walkdir-list/main.rs | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/walkdir-list/main.rs b/walkdir-list/main.rs index b24f98c..1b08b9b 100644 --- a/walkdir-list/main.rs +++ b/walkdir-list/main.rs @@ -21,6 +21,7 @@ use std::result; use std::time::Instant; use bstr::BString; +use walkdir::Cursor; use walkdir::WalkDir; type Result<T> = result::Result<T, Box<dyn Error>>; @@ -112,6 +113,33 @@ where Ok(res) } + fn count_cursor<W: io::Write>( + args: &Args, + mut stderr: W, + dir: &Path, + ) -> Result<CountResult> { + let mut res = args.empty_count_result(); + let mut cursor = args.cursor(dir); + loop { + match cursor.read() { + Ok(None) => break, + Ok(Some(entry)) => { + res.count += 1; + if let Some(ref mut size) = res.size { + let md = entry.path().metadata()?; + *size = md.len(); + } + } + Err(err) => { + if !args.ignore_errors { + writeln!(stderr, "ERROR: {}", err)?; + } + } + } + } + Ok(res) + } + #[cfg(windows)] fn count_windows<W: io::Write>( args: &Args, @@ -246,6 +274,8 @@ where res = res.add(count_unix(args, &mut stderr, &dir)?); } else if args.flat_linux { res = res.add(count_linux(args, &mut stderr, &dir)?); + } else if args.flat_cursor { + res = res.add(count_cursor(args, &mut stderr, &dir)?); } else { res = res.add(count_walkdir(args, &mut stderr, &dir)?); } @@ -322,6 +352,30 @@ where Ok(()) } + fn print_cursor<W1: io::Write, W2: io::Write>( + args: &Args, + mut stdout: W1, + mut stderr: W2, + dir: &Path, + ) -> Result<()> { + let mut cursor = args.cursor(dir); + loop { + match cursor.read() { + Ok(None) => break, + Ok(Some(entry)) => { + write_os_str(&mut stdout, entry.path().as_os_str())?; + stdout.write_all(b"\n")?; + } + Err(err) => { + if !args.ignore_errors { + writeln!(stderr, "ERROR: {}", err)?; + } + } + } + } + Ok(()) + } + #[cfg(windows)] fn print_windows<W1, W2>( args: &Args, @@ -472,6 +526,8 @@ where print_unix(&args, &mut stdout, &mut stderr, dir)?; } else if args.flat_linux { print_linux(&args, &mut stdout, &mut stderr, dir)?; + } else if args.flat_cursor { + print_cursor(&args, &mut stdout, &mut stderr, dir)?; } else { print_walkdir(&args, &mut stdout, &mut stderr, dir)?; } @@ -497,6 +553,7 @@ struct Args { flat_windows: bool, flat_unix: bool, flat_linux: bool, + flat_cursor: bool, } impl Args { @@ -579,10 +636,23 @@ impl Args { .conflicts_with("flat-unix") .conflicts_with("flat-linux") .conflicts_with("flat-windows") + .conflicts_with("flat-cursor") .help( "Use std::fs::read_dir to list contents of a single \ directory. This is NOT recursive.", ), + ) + .arg( + Arg::with_name("flat-cursor") + .long("flat-cursor") + .conflicts_with("flat-std") + .conflicts_with("flat-unix") + .conflicts_with("flat-linux") + .conflicts_with("flat-windows") + .help( + "Use walkdir::Cursor to recursively list the contents \ + of a single directory.", + ), ); if cfg!(unix) { app = app.arg( @@ -591,6 +661,7 @@ impl Args { .conflicts_with("flat-std") .conflicts_with("flat-linux") .conflicts_with("flat-windows") + .conflicts_with("flat-cursor") .help( "Use Unix-specific APIs to list contents of a single \ directory. This is NOT recursive.", @@ -604,6 +675,7 @@ impl Args { .conflicts_with("flat-std") .conflicts_with("flat-unix") .conflicts_with("flat-windows") + .conflicts_with("flat-cursor") .help( "Use Linux-specific syscalls (getdents64) to list \ contents of a single directory. This is NOT \ @@ -618,6 +690,7 @@ impl Args { .conflicts_with("flat-std") .conflicts_with("flat-unix") .conflicts_with("flat-linux") + .conflicts_with("flat-cursor") .help("Use Windows-specific APIs to list contents of a single \ directory. This is NOT recursive.")); } @@ -644,6 +717,7 @@ impl Args { flat_windows: parsed.is_present("flat-windows"), flat_unix: parsed.is_present("flat-unix"), flat_linux: parsed.is_present("flat-linux"), + flat_cursor: parsed.is_present("flat-cursor"), }) } @@ -667,6 +741,10 @@ impl Args { walkdir } + fn cursor(&self, path: &Path) -> Cursor { + Cursor::new(path) + } + fn empty_count_result(&self) -> CountResult { CountResult { count: 0, |