1
0
Fork 0
mirror of synced 2024-04-30 18:43:25 +12:00

Basic empty folder remover

This commit is contained in:
Rafał Mikrut 2020-08-31 19:37:30 +02:00
parent 6e5e96dcfb
commit ea85a40e2f
4 changed files with 331 additions and 29 deletions

12
src/common.rs Normal file
View file

@ -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.")
);
}
}

View file

@ -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 humansize::{file_size_opts as options, FileSize};
use std::collections::{BTreeMap, HashMap}; use std::collections::{BTreeMap, HashMap};
use std::fs::{File, Metadata}; use std::fs::{File, Metadata};
@ -7,14 +7,14 @@ use std::path::Path;
use std::time::{SystemTime, UNIX_EPOCH}; use std::time::{SystemTime, UNIX_EPOCH};
use std::{fs, process}; use std::{fs, process};
use crate::common::Common;
#[derive(PartialEq)] #[derive(PartialEq)]
pub enum CheckingMethod { pub enum CheckingMethod {
SIZE, SIZE,
HASH, HASH,
} }
// TODO
#[allow(dead_code)]
#[derive(Eq, PartialEq)] #[derive(Eq, PartialEq)]
pub enum DeleteMethod { pub enum DeleteMethod {
None, None,
@ -42,7 +42,7 @@ pub struct DuplicateFinder {
files_with_identical_hashes: BTreeMap<u64, Vec<Vec<FileEntry>>>, files_with_identical_hashes: BTreeMap<u64, Vec<Vec<FileEntry>>>,
allowed_extensions: Vec<String>, // jpg, jpeg, mp4 allowed_extensions: Vec<String>, // jpg, jpeg, mp4
lost_space: u64, lost_space: u64,
// excluded_items: Vec<String>, // excluded_items: Vec<String>, // TODO
excluded_directories: Vec<String>, excluded_directories: Vec<String>,
included_directories: Vec<String>, included_directories: Vec<String>,
min_file_size: u64, min_file_size: u64,
@ -166,7 +166,7 @@ impl DuplicateFinder {
self.included_directories = checked_directories; 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) { pub fn set_exclude_directory(&mut self, mut exclude_directory: String) {
@ -213,7 +213,7 @@ impl DuplicateFinder {
} }
self.excluded_directories = checked_directories; 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) { fn calculate_lost_space(&mut self, check_method: &CheckingMethod) {
let mut bytes: u64 = 0; let mut bytes: u64 = 0;
@ -332,7 +332,7 @@ impl DuplicateFinder {
} }
} }
self.debug_print(); 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")); //println!("Duration of finding duplicates {:?}", end_time.duration_since(start_time).expect("a"));
} }
// pub fn save_results_to_file(&self) {} // pub fn save_results_to_file(&self) {}
@ -355,7 +355,7 @@ impl DuplicateFinder {
self.files_with_identical_size = new_hashmap; self.files_with_identical_size = new_hashmap;
self.debug_print(); 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 /// Should be slower than checking in different ways, but still needs to be checked
@ -397,7 +397,7 @@ impl DuplicateFinder {
} }
} }
self.debug_print(); 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 // /// I'mm not sure about performance, so maybe I
// pub fn find_small_duplicates_by_hashing(mut self){ // 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 size_limit_for_small_files u64 = // 16 MB
// let mut new_hashmap // 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 /// Setting include directories, panics when there is not directories available
fn debug_print(&self) { 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 /// 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.excluded_directories.dedup();
self.included_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; let mut is_inside: bool;
for ed_checked in &self.excluded_directories { for ed_checked in &self.excluded_directories {
is_inside = false; is_inside = false;
@ -588,7 +580,7 @@ impl DuplicateFinder {
self.excluded_directories = optimized_excluded; self.excluded_directories = optimized_excluded;
optimized_excluded = Vec::<String>::new(); optimized_excluded = Vec::<String>::new();
// Excluded paths must are inside include path, because TODO // Excluded paths must are inside include path, because
for ed in &self.excluded_directories { for ed in &self.excluded_directories {
let mut is_inside: bool = false; let mut is_inside: bool = false;
for id in &self.included_directories { for id in &self.included_directories {
@ -613,7 +605,7 @@ impl DuplicateFinder {
// Not needed, but better is to have sorted everything // Not needed, but better is to have sorted everything
self.excluded_directories.sort(); self.excluded_directories.sort();
self.included_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) { fn delete_files(&mut self, check_method: &CheckingMethod, delete_method: &DeleteMethod) {
@ -636,10 +628,14 @@ impl DuplicateFinder {
} }
} }
} }
for i in errors { if !errors.is_empty() {
println!("Failed to delete {}", i); 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<String>) { fn delete_files(vector: &[FileEntry], delete_method: &DeleteMethod, errors: &mut Vec<String>) {

275
src/empty_folder.rs Normal file
View file

@ -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<String>,
excluded_directories: Vec<String>,
included_directories: Vec<String>,
}
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<String> = Vec::<String>::new();
let mut optimized_excluded: Vec<String> = Vec::<String>::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::<String>::new();
self.excluded_directories = optimized_excluded;
optimized_excluded = Vec::<String>::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::<String>::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::<String>::new();
self.excluded_directories = optimized_excluded;
optimized_excluded = Vec::<String>::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::<String>::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<String> = include_directory.split(',').map(String::from).collect();
let mut checked_directories: Vec<String> = 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<String> = exclude_directory.split(',').map(String::from).collect();
let mut checked_directories: Vec<String> = 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());
}
}

View file

@ -1,6 +1,8 @@
use std::{env, process}; use std::{env, process};
mod common;
mod duplicate; mod duplicate;
mod empty_folder;
fn main() { fn main() {
// Parse argument // Parse argument
@ -127,13 +129,30 @@ fn main() {
} }
df.find_duplicates(&check_method, &delete_method); df.find_duplicates(&check_method, &delete_method);
}, }
"--h" | "--help" => { "--h" | "--help" => {
print_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 => { argum => {
println!("FATAL ERROR: \"{}\" argument is not supported, check help for more info.", argum); println!("FATAL ERROR: \"{}\" argument is not supported, check help for more info.", argum);
process::exit(1); process::exit(1);