From 6a320c72901c7e1d4b69e4613a4af7ceb1b0da36 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Thu, 30 Dec 2021 09:53:42 +0200 Subject: feat: traverse directories Sponsored-by: author --- Cargo.lock | 32 ++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/bin/summain.rs | 20 ++++++++++++++++---- 3 files changed, 49 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a8b2a30..c82e0a9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,7 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +version = 3 + [[package]] name = "ansi_term" version = "0.11.0" @@ -309,6 +311,15 @@ dependencies = [ "bitflags", ] +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + [[package]] name = "scopeguard" version = "1.1.0" @@ -418,6 +429,7 @@ dependencies = [ "structopt", "tokio", "unix_mode", + "walkdir", ] [[package]] @@ -513,6 +525,17 @@ version = "0.9.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed" +[[package]] +name = "walkdir" +version = "2.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "808cf2735cd4b6866113f648b791c6adc5714537bc222d9347bb203386ffda56" +dependencies = [ + "same-file", + "winapi", + "winapi-util", +] + [[package]] name = "winapi" version = "0.3.9" @@ -529,6 +552,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index 444263f..878b6cf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,3 +20,4 @@ sha2 = "0.9" structopt = "0.3" tokio = { version = "1", features = ["full"] } unix_mode = "0.1" +walkdir = "2.3.2" diff --git a/src/bin/summain.rs b/src/bin/summain.rs index 58d8788..9365243 100644 --- a/src/bin/summain.rs +++ b/src/bin/summain.rs @@ -1,12 +1,17 @@ use anyhow::Context; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use structopt::StructOpt; use summain::ManifestEntry; +use walkdir::WalkDir; #[tokio::main] async fn main() -> anyhow::Result<()> { - let mut opt = Opt::from_args(); - opt.pathnames[..].sort(); + let opt = Opt::from_args(); + let mut files = vec![]; + for pathname in opt.pathnames { + find(&pathname, &mut files)?; + } + files.sort(); // Get metadata about all files, but don't compute checksum yet. // @@ -18,7 +23,7 @@ async fn main() -> anyhow::Result<()> { // large: Linux only allows 128 KiB of command line arguments. let mut handles = vec![]; - for filename in opt.pathnames.iter().cloned() { + for filename in files.iter().cloned() { handles.push(tokio::spawn(async move { manifest(filename).await })); } @@ -49,6 +54,13 @@ async fn main() -> anyhow::Result<()> { Ok(()) } +fn find(root: &Path, files: &mut Vec) -> anyhow::Result<()> { + for e in WalkDir::new(root) { + files.push(e?.path().to_path_buf()); + } + Ok(()) +} + #[derive(StructOpt, Debug)] struct Opt { #[structopt(parse(from_os_str))] -- cgit v1.2.1