Only try to de-duplicate files + print progress

This commit is contained in:
Arne Keller 2021-07-12 13:32:43 +02:00
parent 6872bca04b
commit ac0a95044a

View File

@ -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::<Vec<_>>();
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);