main.rs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. use std::fs;
  2. use std::fs::DirEntry;
  3. use std::io;
  4. extern crate clap;
  5. use clap::{Arg,App};
  6. fn get_size(file: &DirEntry) -> io::Result<u64> {
  7. let unwrapped = file;
  8. // println!("Name: {}", unwrapped.path().display());
  9. let metad = fs::metadata(unwrapped.path())?;
  10. let file_size = metad.len();
  11. // println!("Size of {:?} is {:?}",unwrapped,file_size);
  12. Ok(file_size)
  13. }
  14. fn get_fill(target_dir: String) -> io::Result<u64> {
  15. let paths = fs::read_dir(target_dir).unwrap();
  16. let mut total: u64 = 0;
  17. for path in paths {
  18. let unwrapped = path.unwrap();
  19. let file_type = unwrapped.file_type().unwrap();
  20. if file_type.is_dir() {
  21. // println!("{} is a dir", unwrapped.path().display());
  22. let string_path = unwrapped.path().into_os_string().into_string().unwrap();
  23. // println!("String path is: {}", string_path);
  24. let working_total = get_fill(string_path).unwrap();
  25. total = total + working_total;
  26. } else {
  27. let file_size = get_size(&unwrapped).unwrap();
  28. total = total + file_size;
  29. }
  30. }
  31. Ok(total)
  32. }
  33. fn main() {
  34. let matches = App::new("fake_du")
  35. .version("0.1")
  36. .about("Faster(?) du")
  37. .arg(Arg::with_name("path")
  38. .short("p")
  39. .long("path")
  40. .required(true)
  41. .takes_value(true)
  42. .help("Path to find size of"))
  43. .get_matches();
  44. let path_string = String::from(matches.value_of("path").unwrap());
  45. let total = get_fill(path_string);
  46. let bytes_total = total.unwrap();
  47. let kb_total = bytes_total as f64 / 1024.0;
  48. let mb_total = bytes_total as f64 / 1024.0 / 1024.0;
  49. let gb_total = bytes_total as f64 / 1024.0 / 1024.0 / 1024.0;
  50. let tb_total = bytes_total as f64 / 1024.0 / 1024.0 / 1024.0 / 1024.0;
  51. // Recreate this cause we used if for get_fill already
  52. let path_string = String::from(matches.value_of("path").unwrap());
  53. print!("Total of {} is: | {} B | {} KB | {} MB | {} GB | {} TB |\n",
  54. path_string,bytes_total,kb_total,mb_total,gb_total,tb_total);
  55. }