diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.rs | 63 |
1 files changed, 37 insertions, 26 deletions
diff --git a/src/main.rs b/src/main.rs index 5ade5c6..c6b3f1e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,18 @@ +use clap::Parser; use std::path::Path; use anyhow::Result; -use kdam::tqdm; use walkdir::WalkDir; +#[derive(Parser, Debug)] +#[command(version, about, author)] +struct Args { + source: String, + destination: String, + #[arg(short, long, default_value_t = 8)] + jobs: usize, +} + async fn copy(from: String, to: String) -> std::io::Result<()> { let from_path = std::path::Path::new(&from); let to_path = std::path::Path::new(&to); @@ -20,29 +29,14 @@ async fn copy(from: String, to: String) -> std::io::Result<()> { Ok(()) } -#[tokio::main] -async fn main() -> Result<()> { - let args: Vec<String> = std::env::args().collect(); +async fn run(args: Args) -> Result<()> { + let source = Path::new(&args.source); + let destination = Path::new(&args.destination); - if args.len() != 3 { - println!("{} <source> <destination>", args.first().unwrap()); - return Ok(()); - } + let mut tasks: Vec<tokio::task::JoinHandle<std::prelude::v1::Result<(), std::io::Error>>> = + Vec::new(); - let source = Path::new(&args[1]); - let destination = Path::new(&args[2]); - const MAX: usize = 100; - let mut tasks = Vec::new(); - let count = WalkDir::new(source) - .into_iter() - .filter_map(|e| e.ok()) - .filter(|e| e.file_type().is_file()).count(); - - for entry in tqdm!(WalkDir::new(source) - .into_iter() - .filter_map(|e| e.ok()) - .filter(|e| e.file_type().is_file()), total = count) - { + for (count, entry) in WalkDir::new(source).into_iter().filter_map(|e| e.ok()).enumerate() { let from = entry.path().to_str().unwrap().to_string(); let rel = entry.path().strip_prefix(source)?; @@ -54,15 +48,32 @@ async fn main() -> Result<()> { tasks.push(tokio::spawn(copy(from, to))); - if tasks.len() > MAX { - for task in tasks { - if let Err(e) = task.await.unwrap() { + print!("copied {count} files\r"); + } + + println!("\nsyncing filesystem"); + + for task in tasks { + match task.await { + Ok(result) => { + if let Err(e) = result { eprintln!("Error: {:?}", e); } } - tasks = Vec::new(); + Err(e) => eprintln!("Runtime Error: {:?}", e) } } Ok(()) } + +fn main() -> Result<()> { + let args = Args::parse(); + let runtime = tokio::runtime::Builder::new_multi_thread() + .worker_threads(args.jobs) + .enable_all() + .build() + .unwrap(); + + runtime.block_on(run(args)) +} |