Empty folder nuker almost complete

This commit is contained in:
Rafał Mikrut 2020-09-01 17:10:54 +02:00
parent 8daf08c744
commit 8e4de34f84
4 changed files with 175 additions and 78 deletions

View File

@ -3,10 +3,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.")
);
if false {
println!(
"Execution of function \"{}\" took {:?}",
function_name,
end_time.duration_since(start_time).expect("Time cannot go reverse.")
);
}
}
}

View File

@ -127,35 +127,40 @@ impl DuplicateFinder {
let mut checked_directories: Vec<String> = Vec::new();
for directory in directories {
let directory : String = directory.trim().to_string();
if directory == "" {
continue
}
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);
println!("Include Directory ERROR: Wildcards are not supported, ignoring path {}.", directory);
continue;
}
if directory.starts_with('~') {
println!("Include Directory ERROR: ~ in path isn't supported.");
process::exit(1);
println!("Include Directory ERROR: ~ in path isn't supported, ignoring path {}.", directory);
continue;
}
if !directory.starts_with('/') {
println!("Include Directory ERROR: Relative path are not supported.");
process::exit(1);
println!("Include Directory ERROR: Relative path are not supported, ignoring path {}.", directory);
continue;
}
if !Path::new(&directory).exists() {
println!("Include Directory ERROR: Path {} doesn't exists.", directory);
process::exit(1);
continue;
}
if !Path::new(&directory).exists() {
println!("Include Directory ERROR: {} isn't folder.", directory);
process::exit(1);
continue;
}
// 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() + "/");
checked_directories.push(directory + "/");
} else {
checked_directories.push(directory.trim().to_string());
checked_directories.push(directory);
}
}
@ -180,28 +185,34 @@ impl DuplicateFinder {
let mut checked_directories: Vec<String> = Vec::new();
for directory in directories {
let directory : String = directory.trim().to_string();
if directory == "" {
continue
}
if directory == "/" {
println!("Exclude Directory ERROR: Excluding / is pointless, because it means that no files will be scanned.");
break;
}
if directory.contains('*') {
println!("Exclude Directory ERROR: Wildcards are not supported, please don't use it.");
process::exit(1);
println!("Exclude Directory ERROR: Wildcards are not supported, ignoring path {}.", directory);
continue;
}
if directory.starts_with('~') {
println!("Exclude Directory ERROR: ~ in path isn't supported.");
process::exit(1);
println!("Exclude Directory ERROR: ~ in path isn't supported, ignoring path {}.", directory);
continue;
}
if !directory.starts_with('/') {
println!("Exclude Directory ERROR: Relative path are not supported.");
process::exit(1);
println!("Exclude Directory ERROR: Relative path are not supported, ignoring path {}.", directory);
continue;
}
if !Path::new(&directory).exists() {
println!("Exclude Directory WARNING: Path {} doesn't exists.", directory);
//process::exit(1); // Better just print warning witohut closing
println!("Exclude Directory ERROR: Path {} doesn't exists.", directory);
continue;
}
if !Path::new(&directory).exists() {
println!("Exclude Directory ERROR: {} isn't folder.", directory);
process::exit(1);
continue;
}
// directory must end with /, due to possiblity of incorrect assumption, that e.g. /home/rafal is top folder to /home/rafalinho
@ -447,13 +458,13 @@ impl DuplicateFinder {
}
}
println!(
"Found {} files in {} groups with same content which took {}:",
"Found {} duplicated files in {} groups with same content which took {}:",
number_of_files,
number_of_groups,
self.lost_space.file_size(options::BINARY).unwrap()
);
for i in &self.files_with_identical_hashes {
println!("Size - {}", i.0);
println!("Size - {}", i.0.file_size(options::BINARY).unwrap());
for j in i.1 {
for k in j {
println!("{}", k.path);

View File

@ -5,22 +5,22 @@ use std::path::Path;
use std::time::SystemTime;
use std::{fs, process};
#[derive(Eq, PartialEq, Copy, Clone)]
enum FolderEmptiness {
Yes,
No,
Maybe,
}
#[derive(Clone)]
struct FolderEntry {
self_path: String,
parent_path: Option<String>,
is_empty: FolderEmptiness,
}
impl FolderEntry {}
pub struct EmptyFolder {
number_of_checked_folders: usize,
number_of_empty_folders: usize,
empty_folder_list: Vec<String>,
empty_folder_list: HashMap<String, FolderEntry>,
excluded_directories: Vec<String>,
included_directories: Vec<String>,
}
@ -30,7 +30,7 @@ impl EmptyFolder {
EmptyFolder {
number_of_checked_folders: 0,
number_of_empty_folders: 0,
empty_folder_list: Vec::new(),
empty_folder_list: Default::default(),
excluded_directories: vec![],
included_directories: vec![],
}
@ -39,28 +39,65 @@ impl EmptyFolder {
pub fn find_empty_folders(mut self, delete_folders: bool) {
self.optimize_directories();
self.debug_print();
self.check_for_empty_folders();
self.check_for_empty_folders(true);
self.check_for_empty_folders(false); // Not needed for CLI, but it is better to check this
self.optimize_folders();
self.print_empty_folders();
if delete_folders {
self.delete_empty_folders();
}
}
fn check_for_empty_folders(&self) {
/// Clean directory tree
/// If directory contains only 2 empty folders, then this directory should be removed instead two empty folders inside because it will produce another empty folder.
fn optimize_folders(&mut self) {
let mut new_directory_folders: HashMap<String, FolderEntry> = Default::default();
for entry in &self.empty_folder_list {
match &entry.1.parent_path {
Some(t) => {
if !self.empty_folder_list.contains_key(t) {
new_directory_folders.insert(entry.0.clone(), entry.1.clone());
}
}
None => {
new_directory_folders.insert(entry.0.clone(), entry.1.clone());
}
}
}
self.empty_folder_list = new_directory_folders;
}
/// Function to check if folder are empty, initial_checking is used to check again if folder is
fn check_for_empty_folders(&mut self, initial_checking: bool) {
let start_time: SystemTime = SystemTime::now();
let mut folders_to_check: Vec<String> = Vec::with_capacity(1024 * 2); // This should be small enough too not see to big difference and big enough to store most of paths without needing to resize vector
let mut folders_checked: HashMap<String, FolderEntry> = Default::default();
// Add root folders for finding
for id in &self.included_directories {
folders_checked.insert(
id.clone(),
FolderEntry {
self_path: id.clone(),
parent_path: None,
is_empty: FolderEmptiness::Maybe,
},
);
folders_to_check.push(id.to_string());
if initial_checking {
// Add root folders for finding
for id in &self.included_directories {
folders_checked.insert(
id.clone(),
FolderEntry {
parent_path: None,
is_empty: FolderEmptiness::Maybe,
},
);
folders_to_check.push(id.clone());
}
} else {
// Add root folders for finding
for id in &self.empty_folder_list {
folders_checked.insert(
id.0.clone(),
FolderEntry {
parent_path: None,
is_empty: FolderEmptiness::Maybe,
},
);
folders_to_check.push(id.0.clone());
}
}
let mut current_folder: String;
@ -93,7 +130,6 @@ impl EmptyFolder {
folders_checked.insert(
next_folder.clone(),
FolderEntry {
self_path: next_folder,
parent_path: Option::from(current_folder.clone()),
is_empty: FolderEmptiness::Maybe,
},
@ -103,7 +139,7 @@ impl EmptyFolder {
// Not folder so it may be a file or symbolic link
folders_checked.get_mut(&current_folder).unwrap().is_empty = FolderEmptiness::No;
let mut d = folders_checked.get_mut(&current_folder).unwrap();
let mut cf: String = current_folder.clone();
let mut cf: String;
loop {
d.is_empty = FolderEmptiness::No;
if d.parent_path != None {
@ -116,34 +152,70 @@ impl EmptyFolder {
}
}
}
self.debug_print();
Common::print_time(start_time, SystemTime::now(), "check_files_size".to_string());
if initial_checking {
for entry in folders_checked {
if entry.1.is_empty != FolderEmptiness::No {
self.empty_folder_list.insert(entry.0, entry.1);
}
}
} else {
// Sprawdzenie
let mut new_folders_list: HashMap<String, FolderEntry> = Default::default();
for entry in folders_checked {
if entry.1.is_empty != FolderEmptiness::No && self.empty_folder_list.contains_key(&entry.0) {
new_folders_list.insert(entry.0, entry.1);
}
}
self.empty_folder_list = new_folders_list;
}
Common::print_time(start_time, SystemTime::now(), "check_for_empty_folder".to_string());
}
fn delete_empty_folders(&self) {
// Need to check again because folder may stop be empty
let start_time: SystemTime = SystemTime::now();
let mut errors: Vec<String> = Vec::new();
for entry in &self.empty_folder_list {
match fs::remove_dir_all(entry.0) {
Ok(_) => (),
Err(_) => errors.push(entry.0.clone()),
};
}
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:");
}
for i in errors {
println!("{}", i);
}
Common::print_time(start_time, SystemTime::now(), "delete_files".to_string());
}
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);
println!("{}", i.0);
}
}
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());
if false {
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.0.clone());
}
println!("Excluded directories - {:?}", self.excluded_directories);
println!("Included directories - {:?}", self.included_directories);
println!("-----------------------------------------");
}
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
// 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();
@ -273,35 +345,40 @@ impl EmptyFolder {
let mut checked_directories: Vec<String> = Vec::new();
for directory in directories {
let directory : String = directory.trim().to_string();
if directory == "" {
continue
}
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);
println!("Include Directory ERROR: Wildcards are not supported, ignoring path {}.", directory);
continue;
}
if directory.starts_with('~') {
println!("Include Directory ERROR: ~ in path isn't supported.");
process::exit(1);
println!("Include Directory ERROR: ~ in path isn't supported, ignoring path {}.", directory);
continue;
}
if !directory.starts_with('/') {
println!("Include Directory ERROR: Relative path are not supported.");
process::exit(1);
println!("Include Directory ERROR: Relative path are not supported, ignoring path {}.", directory);
continue;
}
if !Path::new(&directory).exists() {
println!("Include Directory ERROR: Path {} doesn't exists.", directory);
process::exit(1);
continue;
}
if !Path::new(&directory).exists() {
println!("Include Directory ERROR: {} isn't folder.", directory);
process::exit(1);
continue;
}
// 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() + "/");
checked_directories.push(directory + "/");
} else {
checked_directories.push(directory.trim().to_string());
checked_directories.push(directory);
}
}
@ -326,28 +403,34 @@ impl EmptyFolder {
let mut checked_directories: Vec<String> = Vec::new();
for directory in directories {
let directory : String = directory.trim().to_string();
if directory == "" {
continue
}
if directory == "/" {
println!("Exclude Directory ERROR: Excluding / is pointless, because it means that no files will be scanned.");
break;
}
if directory.contains('*') {
println!("Exclude Directory ERROR: Wildcards are not supported, please don't use it.");
process::exit(1);
println!("Exclude Directory ERROR: Wildcards are not supported, ignoring path {}.", directory);
continue;
}
if directory.starts_with('~') {
println!("Exclude Directory ERROR: ~ in path isn't supported.");
process::exit(1);
println!("Exclude Directory ERROR: ~ in path isn't supported, ignoring path {}.", directory);
continue;
}
if !directory.starts_with('/') {
println!("Exclude Directory ERROR: Relative path are not supported.");
process::exit(1);
println!("Exclude Directory ERROR: Relative path are not supported, ignoring path {}.", directory);
continue;
}
if !Path::new(&directory).exists() {
println!("Exclude Directory WARNING: Path {} doesn't exists.", directory);
//process::exit(1); // Better just print warning witohut closing
println!("Exclude Directory ERROR: Path {} doesn't exists.", directory);
continue;
}
if !Path::new(&directory).exists() {
println!("Exclude Directory ERROR: {} isn't folder.", directory);
process::exit(1);
continue;
}
// directory must end with /, due to possiblity of incorrect assumption, that e.g. /home/rafal is top folder to /home/rafalinho

View File

@ -186,6 +186,7 @@ Usage of Czkawka:
-i directory_to_search - list of directories which should will be searched like /home/rafal
-e exclude_directories - list of directories which will be excluded from search.
-delete - delete found empty folders
czkawka --e -i "/home/rafal/rr, /home/gateway" -e "/home/rafal/rr/2" -delete
"###
);
}