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