|
| 1 | +use reflink_copy::ReflinkSupport; |
| 2 | +use std::fs; |
| 3 | +use std::io; |
| 4 | +use std::path::Path; |
| 5 | +use walkdir::WalkDir; |
| 6 | + |
| 7 | +// cargo run --example copy_folder V:/1 V:/2 |
| 8 | + |
| 9 | +fn main() -> io::Result<()> { |
| 10 | + let args: Vec<_> = std::env::args().collect(); |
| 11 | + if args.len() < 3 { |
| 12 | + eprintln!("Usage: {} <source_folder> <target_folder>", args[0]); |
| 13 | + return Ok(()); |
| 14 | + } |
| 15 | + |
| 16 | + let from = Path::new(&args[1]); |
| 17 | + let to = Path::new(&args[2]); |
| 18 | + let reflink_support = reflink_copy::check_reflink_support(from, to)?; |
| 19 | + println!("Reflink support: {reflink_support:?}"); |
| 20 | + |
| 21 | + let mut reflinked_count = 0u64; |
| 22 | + let mut copied_count = 0u64; |
| 23 | + |
| 24 | + for entry in WalkDir::new(from) { |
| 25 | + let entry = entry?; |
| 26 | + let relative_path = entry.path().strip_prefix(from).unwrap(); |
| 27 | + let target_path = to.join(relative_path); |
| 28 | + |
| 29 | + if entry.file_type().is_dir() { |
| 30 | + fs::create_dir_all(&target_path)?; |
| 31 | + } else { |
| 32 | + match reflink_support { |
| 33 | + ReflinkSupport::Supported => { |
| 34 | + reflink_copy::reflink(entry.path(), target_path)?; |
| 35 | + reflinked_count = reflinked_count.saturating_add(1); |
| 36 | + } |
| 37 | + ReflinkSupport::Unknown => { |
| 38 | + let result = reflink_copy::reflink_or_copy(entry.path(), target_path)?; |
| 39 | + if result.is_some() { |
| 40 | + copied_count = copied_count.saturating_add(1); |
| 41 | + } else { |
| 42 | + reflinked_count = reflinked_count.saturating_add(1); |
| 43 | + } |
| 44 | + } |
| 45 | + ReflinkSupport::NotSupported => { |
| 46 | + fs::copy(entry.path(), target_path)?; |
| 47 | + copied_count = copied_count.saturating_add(1); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + println!("reflinked files count: {reflinked_count}"); |
| 54 | + println!("copied files count: {copied_count}"); |
| 55 | + Ok(()) |
| 56 | +} |
0 commit comments