use std::fs; use std::fs::DirEntry; use std::io; extern crate clap; use clap::{Arg,App}; fn get_size(file: &DirEntry) -> io::Result { let unwrapped = file; // println!("Name: {}", unwrapped.path().display()); let metad = fs::metadata(unwrapped.path())?; let file_size = metad.len(); // println!("Size of {:?} is {:?}",unwrapped,file_size); Ok(file_size) } fn get_fill(target_dir: String) -> io::Result { let paths = fs::read_dir(target_dir).unwrap(); let mut total: u64 = 0; for path in paths { let unwrapped = path.unwrap(); let file_type = unwrapped.file_type().unwrap(); if file_type.is_dir() { // println!("{} is a dir", unwrapped.path().display()); let string_path = unwrapped.path().into_os_string().into_string().unwrap(); // println!("String path is: {}", string_path); let working_total = get_fill(string_path).unwrap(); total = total + working_total; } else { let file_size = get_size(&unwrapped).unwrap(); total = total + file_size; } } Ok(total) } fn main() { let matches = App::new("fake_du") .version("0.1") .about("Faster(?) du") .arg(Arg::with_name("path") .short("p") .long("path") .required(true) .takes_value(true) .help("Path to find size of")) .get_matches(); let path_string = String::from(matches.value_of("path").unwrap()); let total = get_fill(path_string); let bytes_total = total.unwrap(); let kb_total = bytes_total as f64 / 1024.0; let mb_total = bytes_total as f64 / 1024.0 / 1024.0; let gb_total = bytes_total as f64 / 1024.0 / 1024.0 / 1024.0; let tb_total = bytes_total as f64 / 1024.0 / 1024.0 / 1024.0 / 1024.0; // Recreate this cause we used if for get_fill already let path_string = String::from(matches.value_of("path").unwrap()); print!("Total of {} is: | {} B | {} KB | {} MB | {} GB | {} TB |\n", path_string,bytes_total,kb_total,mb_total,gb_total,tb_total); }