From ac0a95044a5c36ee6b25765a4763a72de8acd737 Mon Sep 17 00:00:00 2001 From: Arne Keller Date: Mon, 12 Jul 2021 13:32:43 +0200 Subject: [PATCH] Only try to de-duplicate files + print progress --- src/main.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 41d4ff4..eeb8c9e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +use std::ffi::OsString; use std::{collections::HashMap, env, fs, hash::Hasher, os::unix::fs::symlink, path::PathBuf}; use ahash::AHasher; @@ -11,7 +12,9 @@ fn main() { let dir: PathBuf = PathBuf::from(&args[1]); let mut filenames = fs::read_dir(&dir).unwrap() .map(Result::unwrap) - .map(|x| (x.metadata().unwrap().modified().unwrap(), x.file_name())) + .map(|x| (x.metadata().unwrap(), x.file_name())) + .filter(|(metadata, _)| metadata.is_file()) // exclude symlinks and directories + .map(|(metadata, name)| (metadata.modified().unwrap(), name)) .collect::>(); filenames.sort(); let mut hashes = HashMap::new(); @@ -27,6 +30,7 @@ fn main() { if hashes.contains_key(&key) { fs::remove_file(&full_path).unwrap(); symlink(hashes[&key], &full_path).unwrap(); + println!("linking {} -> {}", full_path.display(), (&hashes[&key] as &OsString).to_string_lossy()); fs_set_times::set_symlink_times(&full_path, None, Some(file.0.into())).unwrap(); } else { hashes.insert(key, &file.1);