diff --git a/src/common.rs b/src/common.rs new file mode 100644 index 0000000..361bb60 --- /dev/null +++ b/src/common.rs @@ -0,0 +1,12 @@ +use std::time::SystemTime; + +pub struct Common(); +impl Common { + pub fn print_time(start_time: SystemTime, end_time: SystemTime, function_name: String) { + println!( + "Execution of function \"{}\" took {:?}", + function_name, + end_time.duration_since(start_time).expect("Time cannot go reverse.") + ); + } +} diff --git a/src/duplicate.rs b/src/duplicate.rs index 0b5a495..fa9fcac 100644 --- a/src/duplicate.rs +++ b/src/duplicate.rs @@ -1,4 +1,4 @@ -// Todo, należy upewnić się, że ma wystarczające uprawnienia do odczytu i usuwania +// TODO when using GUI all or most println!() should be used as variables passed by argument use humansize::{file_size_opts as options, FileSize}; use std::collections::{BTreeMap, HashMap}; use std::fs::{File, Metadata}; @@ -7,14 +7,14 @@ use std::path::Path; use std::time::{SystemTime, UNIX_EPOCH}; use std::{fs, process}; +use crate::common::Common; + #[derive(PartialEq)] pub enum CheckingMethod { SIZE, HASH, } -// TODO -#[allow(dead_code)] #[derive(Eq, PartialEq)] pub enum DeleteMethod { None, @@ -42,7 +42,7 @@ pub struct DuplicateFinder { files_with_identical_hashes: BTreeMap>>, allowed_extensions: Vec, // jpg, jpeg, mp4 lost_space: u64, - // excluded_items: Vec, + // excluded_items: Vec, // TODO excluded_directories: Vec, included_directories: Vec, min_file_size: u64, @@ -166,7 +166,7 @@ impl DuplicateFinder { self.included_directories = checked_directories; - //DuplicateFinder::print_time(start_time, SystemTime::now(), "set_include_directory".to_string()); + //Common::print_time(start_time, SystemTime::now(), "set_include_directory".to_string()); } pub fn set_exclude_directory(&mut self, mut exclude_directory: String) { @@ -213,7 +213,7 @@ impl DuplicateFinder { } self.excluded_directories = checked_directories; - //DuplicateFinder::print_time(start_time, SystemTime::now(), "set_exclude_directory".to_string()); + //Common::print_time(start_time, SystemTime::now(), "set_exclude_directory".to_string()); } fn calculate_lost_space(&mut self, check_method: &CheckingMethod) { let mut bytes: u64 = 0; @@ -332,7 +332,7 @@ impl DuplicateFinder { } } self.debug_print(); - DuplicateFinder::print_time(start_time, SystemTime::now(), "check_files_size".to_string()); + Common::print_time(start_time, SystemTime::now(), "check_files_size".to_string()); //println!("Duration of finding duplicates {:?}", end_time.duration_since(start_time).expect("a")); } // pub fn save_results_to_file(&self) {} @@ -355,7 +355,7 @@ impl DuplicateFinder { self.files_with_identical_size = new_hashmap; self.debug_print(); - DuplicateFinder::print_time(start_time, SystemTime::now(), "remove_files_with_unique_size".to_string()); + Common::print_time(start_time, SystemTime::now(), "remove_files_with_unique_size".to_string()); } /// Should be slower than checking in different ways, but still needs to be checked @@ -397,7 +397,7 @@ impl DuplicateFinder { } } self.debug_print(); - DuplicateFinder::print_time(start_time, SystemTime::now(), "check_files_hash".to_string()); + Common::print_time(start_time, SystemTime::now(), "check_files_hash".to_string()); } // /// I'mm not sure about performance, so maybe I // pub fn find_small_duplicates_by_hashing(mut self){ @@ -405,17 +405,9 @@ impl DuplicateFinder { // let size_limit_for_small_files u64 = // 16 MB // let mut new_hashmap // - // DuplicateFinder::print_time(start_time, SystemTime::now(), "find_duplicates_by_comparting_begin_bytes_of_file".to_string()); + // Common::print_time(start_time, SystemTime::now(), "find_duplicates_by_comparting_begin_bytes_of_file".to_string()); // } - fn print_time(start_time: SystemTime, end_time: SystemTime, function_name: String) { - println!( - "Execution of function \"{}\" took {:?}", - function_name, - end_time.duration_since(start_time).expect("Time cannot go reverse.") - ); - } - /// Setting include directories, panics when there is not directories available fn debug_print(&self) { @@ -492,7 +484,7 @@ impl DuplicateFinder { } } } - DuplicateFinder::print_time(start_time, SystemTime::now(), "print_duplicated_entries".to_string()); + Common::print_time(start_time, SystemTime::now(), "print_duplicated_entries".to_string()); } /// Remove unused entries when included or excluded overlaps with each other or are duplicated /// ``` @@ -511,7 +503,7 @@ impl DuplicateFinder { self.excluded_directories.dedup(); self.included_directories.dedup(); - // Optimize for duplicated included directories - "/", "/home". "/home/Pulpit" to "/"- TODO + // Optimize for duplicated included directories - "/", "/home". "/home/Pulpit" to "/" let mut is_inside: bool; for ed_checked in &self.excluded_directories { is_inside = false; @@ -588,7 +580,7 @@ impl DuplicateFinder { self.excluded_directories = optimized_excluded; optimized_excluded = Vec::::new(); - // Excluded paths must are inside include path, because TODO + // Excluded paths must are inside include path, because for ed in &self.excluded_directories { let mut is_inside: bool = false; for id in &self.included_directories { @@ -613,7 +605,7 @@ impl DuplicateFinder { // Not needed, but better is to have sorted everything self.excluded_directories.sort(); self.included_directories.sort(); - DuplicateFinder::print_time(start_time, SystemTime::now(), "optimize_directories".to_string()); + Common::print_time(start_time, SystemTime::now(), "optimize_directories".to_string()); } fn delete_files(&mut self, check_method: &CheckingMethod, delete_method: &DeleteMethod) { @@ -636,10 +628,14 @@ impl DuplicateFinder { } } } - for i in errors { - println!("Failed to delete {}", i); + if !errors.is_empty() { + println!("Failed to delete some files, because they have got deleted earlier or you have too low privileges - try run it as root."); + println!("List of files which wasn't deleted:"); } - DuplicateFinder::print_time(start_time, SystemTime::now(), "delete_files".to_string()); + for i in errors { + println!("{}", i); + } + Common::print_time(start_time, SystemTime::now(), "delete_files".to_string()); } } fn delete_files(vector: &[FileEntry], delete_method: &DeleteMethod, errors: &mut Vec) { diff --git a/src/empty_folder.rs b/src/empty_folder.rs new file mode 100644 index 0000000..efd4c9d --- /dev/null +++ b/src/empty_folder.rs @@ -0,0 +1,275 @@ +use crate::common::Common; +use std::path::Path; +use std::process; +use std::time::SystemTime; + +pub struct EmptyFolder { + number_of_checked_folders: usize, + number_of_empty_folders: usize, + empty_folder_list: Vec, + excluded_directories: Vec, + included_directories: Vec, +} + +impl EmptyFolder { + pub fn new() -> EmptyFolder { + EmptyFolder { + number_of_checked_folders: 0, + number_of_empty_folders: 0, + empty_folder_list: Vec::new(), + excluded_directories: vec![], + included_directories: vec![], + } + } + + pub fn find_empty_folders(mut self, delete_folders: bool) { + self.optimize_directories(); + self.debug_print(); + self.check_for_empty_folders(); + self.print_empty_folders(); + if delete_folders { + self.delete_empty_folders(); + } + } + fn check_for_empty_folders(&self) {} + fn delete_empty_folders(&self) {} + fn print_empty_folders(&self) { + if !self.empty_folder_list.is_empty() { + println!("Found {} empty folders", self.empty_folder_list.len()); + } + for i in &self.empty_folder_list { + println!("{}", i); + } + } + + fn debug_print(&self) { + println!("---------------DEBUG PRINT---------------"); + println!("Number of all checked folders - {}", self.number_of_checked_folders); + println!("Number of empty folders - {}", self.number_of_empty_folders); + for i in &self.empty_folder_list { + println!("# {} ", i.clone()); + } + println!("Excluded directories - {:?}", self.excluded_directories); + println!("Included directories - {:?}", self.included_directories); + println!("-----------------------------------------"); + } + + // TODO maybe move this and one from duplicated finder to one common class to avoid duplicating code + fn optimize_directories(&mut self) { + let start_time: SystemTime = SystemTime::now(); + + let mut optimized_included: Vec = Vec::::new(); + let mut optimized_excluded: Vec = Vec::::new(); + // Remove duplicated entries like: "/", "/" + + self.excluded_directories.sort(); + self.included_directories.sort(); + + self.excluded_directories.dedup(); + self.included_directories.dedup(); + + // Optimize for duplicated included directories - "/", "/home". "/home/Pulpit" to "/" + let mut is_inside: bool; + for ed_checked in &self.excluded_directories { + is_inside = false; + for ed_help in &self.excluded_directories { + if ed_checked == ed_help { + // We checking same element + continue; + } + if ed_checked.starts_with(ed_help) { + is_inside = true; + break; + } + } + if !is_inside { + optimized_excluded.push(ed_checked.to_string()); + } + } + + for id_checked in &self.included_directories { + is_inside = false; + for id_help in &self.included_directories { + if id_checked == id_help { + // We checking same element + continue; + } + if id_checked.starts_with(id_help) { + is_inside = true; + break; + } + } + if !is_inside { + optimized_included.push(id_checked.to_string()); + } + } + + self.included_directories = optimized_included; + optimized_included = Vec::::new(); + self.excluded_directories = optimized_excluded; + optimized_excluded = Vec::::new(); + + // Remove include directories which are inside any exclude directory + for id in &self.included_directories { + let mut is_inside: bool = false; + for ed in &self.excluded_directories { + if id.starts_with(ed) { + is_inside = true; + break; + } + } + if !is_inside { + optimized_included.push(id.to_string()); + } + } + self.included_directories = optimized_included; + optimized_included = Vec::::new(); + + // Remove non existed directories + for id in &self.included_directories { + let path = Path::new(id); + if path.exists() { + optimized_included.push(id.to_string()); + } + } + + for ed in &self.excluded_directories { + let path = Path::new(ed); + if path.exists() { + optimized_excluded.push(ed.to_string()); + } + } + + self.included_directories = optimized_included; + // optimized_included = Vec::::new(); + self.excluded_directories = optimized_excluded; + optimized_excluded = Vec::::new(); + + // Excluded paths must are inside include path, because + for ed in &self.excluded_directories { + let mut is_inside: bool = false; + for id in &self.included_directories { + if ed.starts_with(id) { + is_inside = true; + break; + } + } + if is_inside { + optimized_excluded.push(ed.to_string()); + } + } + + self.excluded_directories = optimized_excluded; + // optimized_excluded = Vec::::new(); + + if self.included_directories.is_empty() { + println!("Optimize Directories ERROR: Excluded directories overlaps all included directories."); + process::exit(1); + } + + // Not needed, but better is to have sorted everything + self.excluded_directories.sort(); + self.included_directories.sort(); + Common::print_time(start_time, SystemTime::now(), "optimize_directories".to_string()); + } + pub fn set_include_directory(&mut self, mut include_directory: String) { + // let start_time: SystemTime = SystemTime::now(); + + if include_directory.is_empty() { + println!("At least one directory must be provided"); + } + + include_directory = include_directory.replace("\"", ""); + let directories: Vec = include_directory.split(',').map(String::from).collect(); + let mut checked_directories: Vec = Vec::new(); + + for directory in directories { + if directory == "/" { + println!("Using / is probably not good idea, you may go out of ram."); + } + if directory.contains('*') { + println!("Include Directory ERROR: Wildcards are not supported, please don't use it."); + process::exit(1); + } + if directory.starts_with('~') { + println!("Include Directory ERROR: ~ in path isn't supported."); + process::exit(1); + } + if !directory.starts_with('/') { + println!("Include Directory ERROR: Relative path are not supported."); + process::exit(1); + } + if !Path::new(&directory).exists() { + println!("Include Directory ERROR: Path {} doesn't exists.", directory); + process::exit(1); + } + if !Path::new(&directory).exists() { + println!("Include Directory ERROR: {} isn't folder.", directory); + process::exit(1); + } + + // directory must end with /, due to possiblity of incorrect assumption, that e.g. /home/rafal is top folder to /home/rafalinho + if !directory.ends_with('/') { + checked_directories.push(directory.trim().to_string() + "/"); + } else { + checked_directories.push(directory.trim().to_string()); + } + } + + if checked_directories.is_empty() { + println!("Not found even one correct path to include."); + process::exit(1); + } + + self.included_directories = checked_directories; + + //Common::print_time(start_time, SystemTime::now(), "set_include_directory".to_string()); + } + + pub fn set_exclude_directory(&mut self, mut exclude_directory: String) { + //let start_time: SystemTime = SystemTime::now(); + if exclude_directory.is_empty() { + return; + } + + exclude_directory = exclude_directory.replace("\"", ""); + let directories: Vec = exclude_directory.split(',').map(String::from).collect(); + let mut checked_directories: Vec = Vec::new(); + + for directory in directories { + if directory == "/" { + println!("Exclude Directory ERROR: Excluding / is pointless, because it means that no files will be scanned."); + } + if directory.contains('*') { + println!("Exclude Directory ERROR: Wildcards are not supported, please don't use it."); + process::exit(1); + } + if directory.starts_with('~') { + println!("Exclude Directory ERROR: ~ in path isn't supported."); + process::exit(1); + } + if !directory.starts_with('/') { + println!("Exclude Directory ERROR: Relative path are not supported."); + process::exit(1); + } + if !Path::new(&directory).exists() { + println!("Exclude Directory WARNING: Path {} doesn't exists.", directory); + //process::exit(1); // Better just print warning witohut closing + } + if !Path::new(&directory).exists() { + println!("Exclude Directory ERROR: {} isn't folder.", directory); + process::exit(1); + } + + // directory must end with /, due to possiblity of incorrect assumption, that e.g. /home/rafal is top folder to /home/rafalinho + if !directory.ends_with('/') { + checked_directories.push(directory.trim().to_string() + "/"); + } else { + checked_directories.push(directory.trim().to_string()); + } + } + self.excluded_directories = checked_directories; + + //Common::print_time(start_time, SystemTime::now(), "set_exclude_directory".to_string()); + } +} diff --git a/src/main.rs b/src/main.rs index 9f62bf4..56a8bca 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,8 @@ use std::{env, process}; +mod common; mod duplicate; +mod empty_folder; fn main() { // Parse argument @@ -127,13 +129,30 @@ fn main() { } df.find_duplicates(&check_method, &delete_method); - }, + } "--h" | "--help" => { print_help(); - }, - "-e" =>{ + } + "--e" => { + let mut ef = empty_folder::EmptyFolder::new(); + let mut delete_folders: bool = false; - }, + if ArgumentsPair::has_command(&arguments, "-i") { + ef.set_include_directory(ArgumentsPair::get_argument(&arguments, "-i", false)); + } else { + println!("FATAL ERROR: Parameter -i with set of included files is required."); + process::exit(1); + } + if ArgumentsPair::has_command(&arguments, "-e") { + ef.set_exclude_directory(ArgumentsPair::get_argument(&arguments, "-e", false)); + } + + if ArgumentsPair::has_command(&arguments, "-delete") { + delete_folders = true; + } + + ef.find_empty_folders(delete_folders); + } argum => { println!("FATAL ERROR: \"{}\" argument is not supported, check help for more info.", argum); process::exit(1);