Guest User

dupper.rs

a guest
Dec 8th, 2018
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. extern crate clap;
  2. extern crate walkdir;
  3.  
  4. use clap::{App, Arg};
  5. use walkdir::WalkDir;
  6. use std::fmt::Debug;//Help in printing HashMap
  7. use std::hash::Hash;//Help in printing HashMap
  8. use std::fs::metadata;//read the meta data to check for dir vs file
  9. use std::collections::HashMap;
  10.  
  11. extern crate dupper;
  12. use dupper::{FileInfo};
  13.  
  14. fn main() {
  15. //clap method to accept teminal arguments.App
  16. let arguments = App::new("Duplucate Finder")
  17. .version("v0.1")
  18. .author("Clament John")
  19. .about("Find duplicate files in your filesystem")
  20. .arg(Arg::with_name("directories")
  21. .short("d")
  22. .long("directories")
  23. .value_name("Directories")
  24. .help("Directories to parse")
  25. .min_values(1)
  26. .required(true)
  27. .takes_value(true)
  28. .index(1))
  29. .get_matches();
  30.  
  31. //get the directory(s) passed in. We use "values_of" to collect more than one arg
  32. let search_dirs: Vec<_> = arguments.values_of("directories").unwrap().collect();
  33.  
  34. let mut file_counter: HashMap<Option<u64>, u64> = HashMap::new();
  35. let mut file_info: HashMap< Option<u64>, FileInfo> = HashMap::new();
  36.  
  37. //read the file paths all the way upto individual files
  38. for dir in search_dirs.iter(){
  39. for e in WalkDir::new(dir).into_iter().filter_map(|e| e.ok()) {
  40. if metadata(e.path().display().to_string()).unwrap().is_file(){
  41. let file_path = e.path().to_str().unwrap().clone();
  42. let mut file_obj = FileInfo::new(None, None, file_path);
  43. file_obj.generate_hash();
  44. file_obj.generate_path_hash();
  45.  
  46. *file_counter.entry( file_obj.get_hash() ).or_insert(0) += 1;
  47.  
  48. file_info.entry(file_obj.get_path_hash() ).or_insert(file_obj.clone());
  49. }
  50. }
  51. }
  52.  
  53. print_map(&file_counter);
  54. // print_map(&file_info);
  55. }
  56.  
  57. fn print_map<K: Debug + Eq + Hash, V: Debug>(map: &HashMap<K, V>) {
  58. for (k, v) in map.iter() {
  59. println!("{:?}: {:#?}", k, v);
  60. }
  61. }
Add Comment
Please, Sign In to add comment