2020-08-31 00:00:22 +12:00
use humansize ::{ file_size_opts as options , FileSize } ;
2020-08-30 02:12:20 +12:00
use std ::collections ::{ BTreeMap , HashMap } ;
2020-09-12 01:52:06 +12:00
use std ::fs ;
2020-08-29 06:30:22 +12:00
use std ::fs ::{ File , Metadata } ;
use std ::io ::prelude ::* ;
2020-08-28 04:57:56 +12:00
use std ::path ::Path ;
2020-09-01 02:15:56 +12:00
use std ::time ::{ SystemTime , UNIX_EPOCH } ;
2020-08-27 06:50:07 +12:00
2020-09-01 05:37:30 +12:00
use crate ::common ::Common ;
2020-08-30 02:12:20 +12:00
#[ derive(PartialEq) ]
pub enum CheckingMethod {
2020-09-17 22:07:58 +12:00
NONE ,
2020-08-30 02:12:20 +12:00
SIZE ,
HASH ,
}
2020-09-01 02:15:56 +12:00
#[ derive(Eq, PartialEq) ]
pub enum DeleteMethod {
None ,
2020-08-31 03:18:04 +12:00
AllExceptNewest ,
AllExceptOldest ,
OneOldest ,
OneNewest ,
2020-09-01 02:15:56 +12:00
}
#[ derive(Clone) ]
struct FileEntry {
pub path : String ,
pub size : u64 ,
pub created_date : SystemTime ,
pub modified_date : SystemTime ,
2020-08-31 03:18:04 +12:00
}
2020-08-31 00:00:22 +12:00
2020-09-12 01:52:06 +12:00
/// Info struck with helpful information's about results
2020-09-10 22:42:44 +12:00
pub struct Info {
2020-09-12 01:52:06 +12:00
pub errors : Vec < String > ,
pub warnings : Vec < String > ,
pub messages : Vec < String > ,
pub number_of_checked_files : usize ,
pub number_of_checked_folders : usize ,
pub number_of_ignored_files : usize ,
pub number_of_ignored_things : usize ,
2020-09-16 05:17:13 +12:00
pub number_of_groups_by_size : usize ,
pub number_of_duplicated_files_by_size : usize ,
pub number_of_groups_by_hash : usize ,
pub number_of_duplicated_files_by_hash : usize ,
pub lost_space_by_size : u64 ,
pub lost_space_by_hash : u64 ,
2020-09-12 01:52:06 +12:00
pub number_of_removed_files : usize ,
pub number_of_failed_to_remove_files : usize ,
pub gained_space : u64 ,
2020-09-10 22:42:44 +12:00
}
impl Info {
pub fn new ( ) -> Info {
Info {
2020-09-12 01:52:06 +12:00
errors : vec ! [ ] ,
warnings : vec ! [ ] ,
messages : vec ! [ ] ,
2020-08-27 06:50:07 +12:00
number_of_checked_files : 0 ,
2020-08-30 02:12:20 +12:00
number_of_ignored_files : 0 ,
2020-08-28 04:57:56 +12:00
number_of_checked_folders : 0 ,
number_of_ignored_things : 0 ,
2020-09-16 05:17:13 +12:00
number_of_groups_by_size : 0 ,
number_of_duplicated_files_by_size : 0 ,
number_of_groups_by_hash : 0 ,
number_of_duplicated_files_by_hash : 0 ,
lost_space_by_size : 0 ,
lost_space_by_hash : 0 ,
2020-09-12 01:52:06 +12:00
number_of_removed_files : 0 ,
number_of_failed_to_remove_files : 0 ,
gained_space : 0 ,
2020-09-10 22:42:44 +12:00
}
}
}
impl Default for Info {
fn default ( ) -> Self {
Self ::new ( )
}
}
2020-09-17 22:07:58 +12:00
/// Struct with required information's to work
pub struct DuplicateFinder {
infos : Info ,
files_with_identical_size : BTreeMap < u64 , Vec < FileEntry > > ,
files_with_identical_hashes : BTreeMap < u64 , Vec < Vec < FileEntry > > > ,
allowed_extensions : Vec < String > ,
excluded_items : Vec < String > ,
excluded_directories : Vec < String > ,
included_directories : Vec < String > ,
recursive_search : bool ,
min_file_size : u64 ,
check_method : CheckingMethod ,
delete_method : DeleteMethod ,
}
2020-09-10 22:42:44 +12:00
impl DuplicateFinder {
pub fn new ( ) -> DuplicateFinder {
DuplicateFinder {
infos : Info ::new ( ) ,
2020-08-30 02:12:20 +12:00
files_with_identical_size : Default ::default ( ) ,
files_with_identical_hashes : Default ::default ( ) ,
2020-09-12 08:32:17 +12:00
excluded_items : vec ! [ ] ,
2020-08-27 06:50:07 +12:00
excluded_directories : vec ! [ ] ,
included_directories : vec ! [ ] ,
2020-09-12 23:25:23 +12:00
recursive_search : true ,
2020-08-31 00:00:22 +12:00
min_file_size : 1024 ,
2020-08-30 02:12:20 +12:00
allowed_extensions : vec ! [ ] ,
2020-09-17 22:07:58 +12:00
check_method : CheckingMethod ::NONE ,
delete_method : DeleteMethod ::None ,
2020-08-30 02:12:20 +12:00
}
}
2020-09-12 01:52:06 +12:00
pub fn get_infos ( & self ) -> & Info {
& self . infos
}
2020-08-30 02:12:20 +12:00
2020-09-17 22:07:58 +12:00
pub fn find_duplicates ( & mut self ) {
2020-08-30 02:12:20 +12:00
self . optimize_directories ( ) ;
self . check_files_size ( ) ;
2020-09-17 22:07:58 +12:00
if self . check_method = = CheckingMethod ::HASH {
2020-08-30 02:12:20 +12:00
self . check_files_hash ( ) ;
}
2020-09-17 22:07:58 +12:00
self . delete_files ( ) ;
2020-09-12 01:52:06 +12:00
self . debug_print ( ) ;
2020-08-30 02:12:20 +12:00
}
2020-09-17 22:07:58 +12:00
pub fn set_check_method ( & mut self , check_method : CheckingMethod ) {
self . check_method = check_method ;
}
pub fn set_delete_method ( & mut self , delete_method : DeleteMethod ) {
self . delete_method = delete_method ;
}
2020-08-30 02:12:20 +12:00
pub fn set_min_file_size ( & mut self , min_size : u64 ) {
self . min_file_size = min_size ;
}
2020-09-16 05:17:13 +12:00
pub fn set_recursive_search ( & mut self , recursive_search : bool ) {
self . recursive_search = recursive_search ;
2020-09-12 23:25:23 +12:00
}
2020-09-12 08:32:17 +12:00
pub fn set_excluded_items ( & mut self , mut excluded_items : String ) {
2020-09-17 22:07:58 +12:00
let start_time : SystemTime = SystemTime ::now ( ) ;
2020-09-12 08:32:17 +12:00
if excluded_items . is_empty ( ) {
return ;
}
excluded_items = excluded_items . replace ( " \" " , " " ) ;
let expressions : Vec < String > = excluded_items . split ( ',' ) . map ( String ::from ) . collect ( ) ;
let mut checked_expressions : Vec < String > = Vec ::new ( ) ;
for expression in expressions {
let expression : String = expression . trim ( ) . to_string ( ) ;
if expression = = " " {
continue ;
}
2020-09-16 05:17:13 +12:00
if expression = = " DEFAULT " {
// TODO add more files by default
checked_expressions . push ( " */.git/* " . to_string ( ) ) ;
continue ;
}
2020-09-12 08:32:17 +12:00
if ! expression . contains ( '*' ) {
self . infos . warnings . push ( " Excluded Items Warning: Wildcard * is required in expression, ignoring " . to_string ( ) + & * expression ) ;
continue ;
}
checked_expressions . push ( expression ) ;
}
self . excluded_items = checked_expressions ;
2020-09-17 22:07:58 +12:00
Common ::print_time ( start_time , SystemTime ::now ( ) , " set_excluded_items " . to_string ( ) ) ;
2020-08-30 02:12:20 +12:00
}
pub fn set_allowed_extensions ( & mut self , mut allowed_extensions : String ) {
2020-09-17 22:07:58 +12:00
let start_time : SystemTime = SystemTime ::now ( ) ;
2020-08-30 02:12:20 +12:00
if allowed_extensions . is_empty ( ) {
2020-09-12 01:52:06 +12:00
self . infos . messages . push ( " No allowed extension was provided, so all are allowed " . to_string ( ) ) ;
return ;
2020-08-30 02:12:20 +12:00
}
2020-08-30 05:11:55 +12:00
allowed_extensions = allowed_extensions . replace ( " IMAGE " , " jpg,kra,gif,png,bmp,tiff,webp,hdr,svg " ) ;
allowed_extensions = allowed_extensions . replace ( " VIDEO " , " mp4,flv,mkv,webm,vob,ogv,gifv,avi,mov,wmv,mpg,m4v,m4p,mpeg,3gp " ) ;
2020-08-30 02:12:20 +12:00
allowed_extensions = allowed_extensions . replace ( " MUSIC " , " mp3,flac,ogg,tta,wma,webm " ) ;
2020-08-31 00:00:22 +12:00
allowed_extensions = allowed_extensions . replace ( " TEXT " , " txt,doc,docx,odt,rtf " ) ;
2020-08-30 02:12:20 +12:00
let extensions : Vec < String > = allowed_extensions . split ( ',' ) . map ( String ::from ) . collect ( ) ;
2020-08-30 05:11:55 +12:00
for mut extension in extensions {
2020-09-17 22:07:58 +12:00
if extension = = " " | | extension . replace ( '.' , " " ) . trim ( ) = = " " {
2020-09-08 19:41:22 +12:00
continue ;
}
2020-09-17 22:07:58 +12:00
if extension . starts_with ( '.' ) {
extension = extension [ 1 .. ] . to_string ( ) ;
}
if extension [ 1 .. ] . contains ( '.' ) {
self . infos . warnings . push ( " . " . to_string ( ) + extension . as_str ( ) + " is not valid extension(valid extension doesn't have dot inside) " ) ;
continue ;
2020-08-30 05:11:55 +12:00
}
2020-09-17 22:07:58 +12:00
2020-08-30 05:11:55 +12:00
if ! self . allowed_extensions . contains ( & extension . trim ( ) . to_string ( ) ) {
self . allowed_extensions . push ( extension . trim ( ) . to_string ( ) ) ;
2020-08-30 02:12:20 +12:00
}
}
2020-08-30 05:11:55 +12:00
if self . allowed_extensions . is_empty ( ) {
2020-09-12 01:52:06 +12:00
self . infos . messages . push ( " No valid extensions were provided, so allowing all extensions by default. " . to_string ( ) ) ;
2020-08-30 02:12:20 +12:00
}
2020-09-17 22:07:58 +12:00
Common ::print_time ( start_time , SystemTime ::now ( ) , " set_allowed_extensions " . to_string ( ) ) ;
2020-08-30 02:12:20 +12:00
}
2020-09-12 01:52:06 +12:00
pub fn set_include_directory ( & mut self , mut include_directory : String ) -> bool {
2020-09-17 22:07:58 +12:00
let start_time : SystemTime = SystemTime ::now ( ) ;
2020-08-30 02:12:20 +12:00
if include_directory . is_empty ( ) {
2020-09-12 01:52:06 +12:00
self . infos . errors . push ( " At least one directory must be provided " . to_string ( ) ) ;
return false ;
2020-08-30 02:12:20 +12:00
}
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 {
2020-09-02 05:34:39 +12:00
let directory : String = directory . trim ( ) . to_string ( ) ;
2020-09-02 03:10:54 +12:00
if directory = = " " {
2020-09-02 05:34:39 +12:00
continue ;
2020-09-02 03:10:54 +12:00
}
2020-08-30 02:12:20 +12:00
if directory . contains ( '*' ) {
2020-09-12 01:52:06 +12:00
self . infos . warnings . push ( " Include Directory Warning: Wildcards in path are not supported, ignoring " . to_string ( ) + & * directory ) ;
2020-09-02 03:10:54 +12:00
continue ;
2020-08-30 02:12:20 +12:00
}
if ! directory . starts_with ( '/' ) {
2020-09-12 01:52:06 +12:00
self . infos . warnings . push ( " Include Directory Warning: Relative path are not supported, ignoring " . to_string ( ) + & * directory ) ;
2020-09-02 03:10:54 +12:00
continue ;
2020-08-30 02:12:20 +12:00
}
if ! Path ::new ( & directory ) . exists ( ) {
2020-09-12 01:52:06 +12:00
self . infos . warnings . push ( " Include Directory Warning: Provided folder path must exits, ignoring " . to_string ( ) + & * directory ) ;
2020-09-02 03:10:54 +12:00
continue ;
2020-08-30 02:12:20 +12:00
}
2020-09-02 08:48:20 +12:00
if ! Path ::new ( & directory ) . is_dir ( ) {
2020-09-12 01:52:06 +12:00
self . infos . warnings . push ( " Include Directory Warning: Provided path must point at the directory, ignoring " . to_string ( ) + & * directory ) ;
2020-09-02 03:10:54 +12:00
continue ;
2020-08-30 02:12:20 +12:00
}
2020-09-04 03:33:43 +12:00
// directory must end with /, due to possibility of incorrect assumption, that e.g. /home/rafal is top folder to /home/rafalinho
2020-08-30 02:12:20 +12:00
if ! directory . ends_with ( '/' ) {
2020-09-02 03:10:54 +12:00
checked_directories . push ( directory + " / " ) ;
2020-08-30 02:12:20 +12:00
} else {
2020-09-02 03:10:54 +12:00
checked_directories . push ( directory ) ;
2020-08-30 02:12:20 +12:00
}
}
if checked_directories . is_empty ( ) {
2020-09-12 01:52:06 +12:00
self . infos . errors . push ( " Include Directory ERROR: Not found even one correct path to include which is required. " . to_string ( ) ) ;
return false ;
2020-08-30 02:12:20 +12:00
}
self . included_directories = checked_directories ;
2020-09-17 22:07:58 +12:00
Common ::print_time ( start_time , SystemTime ::now ( ) , " set_include_directory " . to_string ( ) ) ;
2020-09-12 01:52:06 +12:00
true
2020-08-30 02:12:20 +12:00
}
pub fn set_exclude_directory ( & mut self , mut exclude_directory : String ) {
2020-09-17 22:07:58 +12:00
let start_time : SystemTime = SystemTime ::now ( ) ;
2020-08-30 02:12:20 +12:00
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 {
2020-09-02 05:34:39 +12:00
let directory : String = directory . trim ( ) . to_string ( ) ;
2020-09-02 03:10:54 +12:00
if directory = = " " {
2020-09-02 05:34:39 +12:00
continue ;
2020-09-02 03:10:54 +12:00
}
2020-08-30 02:12:20 +12:00
if directory = = " / " {
2020-09-12 01:52:06 +12:00
self . infos . errors . push ( " Exclude Directory ERROR: Excluding / is pointless, because it means that no files will be scanned. " . to_string ( ) ) ;
2020-09-02 03:10:54 +12:00
break ;
2020-08-30 02:12:20 +12:00
}
if directory . contains ( '*' ) {
2020-09-12 01:52:06 +12:00
self . infos . warnings . push ( " Exclude Directory Warning: Wildcards in path are not supported, ignoring " . to_string ( ) + & * directory ) ;
2020-09-02 03:10:54 +12:00
continue ;
2020-08-30 02:12:20 +12:00
}
if ! directory . starts_with ( '/' ) {
2020-09-12 01:52:06 +12:00
self . infos . warnings . push ( " Exclude Directory Warning: Relative path are not supported, ignoring " . to_string ( ) + & * directory ) ;
2020-09-02 03:10:54 +12:00
continue ;
2020-08-30 02:12:20 +12:00
}
if ! Path ::new ( & directory ) . exists ( ) {
2020-09-12 01:52:06 +12:00
self . infos . warnings . push ( " Exclude Directory Warning: Provided folder path must exits, ignoring " . to_string ( ) + & * directory ) ;
2020-09-02 03:10:54 +12:00
continue ;
2020-08-30 02:12:20 +12:00
}
2020-09-02 08:48:20 +12:00
if ! Path ::new ( & directory ) . is_dir ( ) {
2020-09-12 01:52:06 +12:00
self . infos . warnings . push ( " Exclude Directory Warning: Provided path must point at the directory, ignoring " . to_string ( ) + & * directory ) ;
2020-09-02 03:10:54 +12:00
continue ;
2020-08-30 02:12:20 +12:00
}
2020-09-04 03:33:43 +12:00
// directory must end with /, due to possibility of incorrect assumption, that e.g. /home/rafal is top folder to /home/rafalinho
2020-08-30 02:12:20 +12:00
if ! directory . ends_with ( '/' ) {
checked_directories . push ( directory . trim ( ) . to_string ( ) + " / " ) ;
} else {
checked_directories . push ( directory . trim ( ) . to_string ( ) ) ;
}
2020-08-27 06:50:07 +12:00
}
2020-08-30 02:12:20 +12:00
self . excluded_directories = checked_directories ;
2020-09-17 22:07:58 +12:00
Common ::print_time ( start_time , SystemTime ::now ( ) , " set_exclude_directory " . to_string ( ) ) ;
2020-08-27 06:50:07 +12:00
}
2020-08-30 02:12:20 +12:00
fn check_files_size ( & mut self ) {
2020-09-04 03:33:43 +12:00
// TODO maybe add multithreading checking for file hash
2020-08-28 04:57:56 +12:00
let start_time : SystemTime = SystemTime ::now ( ) ;
2020-09-12 01:52:06 +12:00
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
2020-08-28 04:57:56 +12:00
// Add root folders for finding
for id in & self . included_directories {
folders_to_check . push ( id . to_string ( ) ) ;
}
2020-09-16 05:17:13 +12:00
self . infos . number_of_checked_folders + = folders_to_check . len ( ) ;
2020-08-28 04:57:56 +12:00
let mut current_folder : String ;
let mut next_folder : String ;
while ! folders_to_check . is_empty ( ) {
current_folder = folders_to_check . pop ( ) . unwrap ( ) ;
2020-09-01 09:03:10 +12:00
let read_dir = match fs ::read_dir ( & current_folder ) {
2020-08-28 04:57:56 +12:00
Ok ( t ) = > t ,
2020-09-12 01:52:06 +12:00
Err ( _ ) = > {
self . infos . warnings . push ( " Cannot open dir " . to_string ( ) + & * current_folder ) ;
continue ;
} // Permissions denied
2020-08-28 04:57:56 +12:00
} ;
for entry in read_dir {
2020-09-10 07:32:23 +12:00
let entry_data = match entry {
2020-09-08 03:39:16 +12:00
Ok ( t ) = > t ,
2020-09-12 01:52:06 +12:00
Err ( _ ) = > {
self . infos . warnings . push ( " Cannot read entry in dir " . to_string ( ) + & * current_folder ) ;
continue ;
} //Permissions denied
2020-09-08 03:39:16 +12:00
} ;
2020-09-07 19:06:12 +12:00
let metadata : Metadata = match entry_data . metadata ( ) {
Ok ( t ) = > t ,
2020-09-12 01:52:06 +12:00
Err ( _ ) = > {
self . infos . warnings . push ( " Cannot read metadata in dir " . to_string ( ) + & * current_folder ) ;
continue ;
} //Permissions denied
2020-09-07 19:06:12 +12:00
} ;
2020-08-28 04:57:56 +12:00
if metadata . is_dir ( ) {
2020-09-16 05:17:13 +12:00
self . infos . number_of_checked_folders + = 1 ;
2020-09-12 01:52:06 +12:00
// if entry_data.file_name().into_string().is_err() { // Probably this can be removed, if crash still will be happens, then uncomment this line
// self.infos.warnings.push("Cannot read folder name in dir ".to_string() + &*current_folder);
// continue; // Permissions denied
// }
2020-09-07 19:06:12 +12:00
2020-09-16 05:17:13 +12:00
if ! self . recursive_search {
2020-09-12 23:25:23 +12:00
continue ;
}
2020-08-28 04:57:56 +12:00
let mut is_excluded_dir = false ;
next_folder = " " . to_owned ( ) + & current_folder + & entry_data . file_name ( ) . into_string ( ) . unwrap ( ) + " / " ;
2020-09-12 08:32:17 +12:00
2020-08-28 04:57:56 +12:00
for ed in & self . excluded_directories {
2020-08-30 02:12:20 +12:00
if next_folder = = * ed {
2020-08-28 04:57:56 +12:00
is_excluded_dir = true ;
break ;
}
}
if ! is_excluded_dir {
2020-09-12 08:32:17 +12:00
let mut found_expression : bool = false ;
for expression in & self . excluded_items {
if Common ::regex_check ( expression , & next_folder ) {
found_expression = true ;
break ;
}
}
if found_expression {
break ;
}
2020-08-28 04:57:56 +12:00
folders_to_check . push ( next_folder ) ;
}
} else if metadata . is_file ( ) {
2020-08-30 05:11:55 +12:00
let mut have_valid_extension : bool ;
let file_name_lowercase : String = entry_data . file_name ( ) . into_string ( ) . unwrap ( ) . to_lowercase ( ) ;
2020-09-12 08:32:17 +12:00
// Checking allowed extensions
2020-08-30 05:11:55 +12:00
if ! self . allowed_extensions . is_empty ( ) {
have_valid_extension = false ;
for i in & self . allowed_extensions {
2020-08-31 00:00:22 +12:00
if file_name_lowercase . ends_with ( & ( " . " . to_string ( ) + & * i . to_lowercase ( ) . to_string ( ) ) ) {
2020-08-30 05:11:55 +12:00
have_valid_extension = true ;
break ;
}
}
} else {
have_valid_extension = true ;
}
2020-09-12 08:32:17 +12:00
// Checking files
2020-08-31 00:00:22 +12:00
if metadata . len ( ) > = self . min_file_size & & have_valid_extension {
2020-08-30 02:12:20 +12:00
let current_file_name = " " . to_owned ( ) + & current_folder + & entry_data . file_name ( ) . into_string ( ) . unwrap ( ) ;
2020-09-12 01:52:06 +12:00
2020-09-12 08:32:17 +12:00
// Checking expressions
let mut found_expression : bool = false ;
for expression in & self . excluded_items {
if Common ::regex_check ( expression , & current_file_name ) {
found_expression = true ;
break ;
}
}
if found_expression {
break ;
}
// Creating new file entry
2020-08-30 02:12:20 +12:00
let fe : FileEntry = FileEntry {
2020-09-12 01:52:06 +12:00
path : current_file_name . clone ( ) ,
2020-08-30 02:12:20 +12:00
size : metadata . len ( ) ,
2020-09-07 19:06:12 +12:00
created_date : match metadata . created ( ) {
Ok ( t ) = > t ,
2020-09-12 01:52:06 +12:00
Err ( _ ) = > {
self . infos . warnings . push ( " Unable to get creation date from file " . to_string ( ) + & * current_file_name ) ;
SystemTime ::now ( )
} // Permissions Denied
2020-09-07 19:06:12 +12:00
} ,
modified_date : match metadata . modified ( ) {
Ok ( t ) = > t ,
2020-09-12 01:52:06 +12:00
Err ( _ ) = > {
self . infos . warnings . push ( " Unable to get modification date from file " . to_string ( ) + & * current_file_name ) ;
SystemTime ::now ( )
} // Permissions Denied
2020-09-07 19:06:12 +12:00
} ,
2020-08-30 02:12:20 +12:00
} ;
2020-09-12 01:52:06 +12:00
self . files_with_identical_size . entry ( metadata . len ( ) ) . or_insert_with ( Vec ::new ) ;
2020-08-30 02:12:20 +12:00
self . files_with_identical_size . get_mut ( & metadata . len ( ) ) . unwrap ( ) . push ( fe ) ;
2020-09-10 22:42:44 +12:00
self . infos . number_of_checked_files + = 1 ;
2020-08-30 02:12:20 +12:00
} else {
2020-09-10 22:42:44 +12:00
self . infos . number_of_ignored_files + = 1 ;
2020-08-29 06:30:22 +12:00
}
2020-08-28 04:57:56 +12:00
} else {
// Probably this is symbolic links so we are free to ignore this
2020-09-10 22:42:44 +12:00
self . infos . number_of_ignored_things + = 1 ;
2020-08-28 04:57:56 +12:00
}
}
}
2020-09-18 00:01:14 +12:00
// Remove files with unique size
let mut new_map : BTreeMap < u64 , Vec < FileEntry > > = Default ::default ( ) ;
self . infos . number_of_duplicated_files_by_size = 0 ;
for ( size , vector ) in & self . files_with_identical_size {
if vector . len ( ) > 1 {
self . infos . number_of_duplicated_files_by_size + = vector . len ( ) - 1 ;
self . infos . number_of_groups_by_size + = 1 ;
self . infos . lost_space_by_size + = ( vector . len ( ) as u64 - 1 ) * size ;
new_map . insert ( * size , vector . clone ( ) ) ;
}
}
self . files_with_identical_size = new_map ;
2020-09-01 05:37:30 +12:00
Common ::print_time ( start_time , SystemTime ::now ( ) , " check_files_size " . to_string ( ) ) ;
2020-08-28 04:57:56 +12:00
}
2020-09-16 05:17:13 +12:00
pub fn save_results_to_file ( & mut self , file_name : & str ) {
2020-09-17 22:07:58 +12:00
let start_time : SystemTime = SystemTime ::now ( ) ;
2020-09-16 05:17:13 +12:00
let file_name : String = match file_name {
" " = > " results.txt " . to_string ( ) ,
k = > k . to_string ( ) ,
} ;
let mut file = match File ::create ( & file_name ) {
Ok ( t ) = > t ,
Err ( _ ) = > {
self . infos . errors . push ( " Failed to create file " . to_string ( ) + file_name . as_str ( ) ) ;
return ;
}
} ;
match file . write_all ( b " Results of searching \n \n " ) {
Ok ( _ ) = > ( ) ,
Err ( _ ) = > {
self . infos . errors . push ( " Failed to save results to file " . to_string ( ) + file_name . as_str ( ) ) ;
return ;
}
}
if ! self . files_with_identical_size . is_empty ( ) {
file . write_all ( b " -------------------------------------------------Files with same size------------------------------------------------- \n " ) . unwrap ( ) ;
2020-09-17 23:35:11 +12:00
file . write_all (
( " Found " . to_string ( )
+ self . infos . number_of_duplicated_files_by_size . to_string ( ) . as_str ( )
+ " duplicated files which in "
+ self . infos . number_of_groups_by_size . to_string ( ) . as_str ( )
+ " groups which takes "
+ & * self . infos . lost_space_by_size . file_size ( options ::BINARY ) . unwrap ( )
+ " . \n " )
. as_bytes ( ) ,
)
. unwrap ( ) ;
2020-09-16 05:17:13 +12:00
for ( size , files ) in self . files_with_identical_size . iter ( ) . rev ( ) {
file . write_all ( b " \n ---- Size " ) . unwrap ( ) ;
file . write_all ( size . file_size ( options ::BINARY ) . unwrap ( ) . as_bytes ( ) ) . unwrap ( ) ;
file . write_all ( ( " ( " . to_string ( ) + size . to_string ( ) . as_str ( ) + " ) " ) . as_bytes ( ) ) . unwrap ( ) ;
2020-09-17 23:35:11 +12:00
file . write_all ( ( " - " . to_string ( ) + files . len ( ) . to_string ( ) . as_str ( ) + " files " ) . as_bytes ( ) ) . unwrap ( ) ;
2020-09-16 05:17:13 +12:00
file . write_all ( b " \n " ) . unwrap ( ) ;
for file_entry in files {
file . write_all ( ( file_entry . path . clone ( ) + " \n " ) . as_bytes ( ) ) . unwrap ( ) ;
}
}
}
if ! self . files_with_identical_hashes . is_empty ( ) {
file . write_all ( b " -------------------------------------------------Files with same hashes------------------------------------------------- \n " ) . unwrap ( ) ;
2020-09-17 23:35:11 +12:00
file . write_all (
( " Found " . to_string ( )
+ self . infos . number_of_duplicated_files_by_hash . to_string ( ) . as_str ( )
+ " duplicated files which in "
+ self . infos . number_of_groups_by_hash . to_string ( ) . as_str ( )
+ " groups which takes "
+ & * self . infos . lost_space_by_hash . file_size ( options ::BINARY ) . unwrap ( )
+ " . \n " )
. as_bytes ( ) ,
)
. unwrap ( ) ;
2020-09-16 05:17:13 +12:00
for ( size , files ) in self . files_with_identical_hashes . iter ( ) . rev ( ) {
for vector in files {
file . write_all ( b " \n ---- Size " ) . unwrap ( ) ;
file . write_all ( size . file_size ( options ::BINARY ) . unwrap ( ) . as_bytes ( ) ) . unwrap ( ) ;
file . write_all ( ( " ( " . to_string ( ) + size . to_string ( ) . as_str ( ) + " ) " ) . as_bytes ( ) ) . unwrap ( ) ;
2020-09-17 23:35:11 +12:00
file . write_all ( ( " - " . to_string ( ) + vector . len ( ) . to_string ( ) . as_str ( ) + " files " ) . as_bytes ( ) ) . unwrap ( ) ;
2020-09-16 05:17:13 +12:00
file . write_all ( b " \n " ) . unwrap ( ) ;
for file_entry in vector {
file . write_all ( ( file_entry . path . clone ( ) + " \n " ) . as_bytes ( ) ) . unwrap ( ) ;
}
}
}
}
2020-09-17 22:07:58 +12:00
Common ::print_time ( start_time , SystemTime ::now ( ) , " save_results_to_file " . to_string ( ) ) ;
2020-09-16 05:17:13 +12:00
}
2020-08-27 19:42:37 +12:00
2020-08-29 06:30:22 +12:00
/// Should be slower than checking in different ways, but still needs to be checked
2020-08-30 02:12:20 +12:00
fn check_files_hash ( & mut self ) {
2020-08-29 06:30:22 +12:00
let start_time : SystemTime = SystemTime ::now ( ) ;
let mut file_handler : File ;
2020-08-30 02:12:20 +12:00
let mut hashmap_with_hash : HashMap < String , Vec < FileEntry > > ;
2020-08-29 06:30:22 +12:00
2020-08-30 02:12:20 +12:00
for entry in & self . files_with_identical_size {
hashmap_with_hash = Default ::default ( ) ;
2020-08-29 06:30:22 +12:00
for file_entry in entry . 1. iter ( ) . enumerate ( ) {
file_handler = match File ::open ( & file_entry . 1. path ) {
2020-08-30 02:12:20 +12:00
Ok ( t ) = > t ,
2020-08-29 06:30:22 +12:00
Err ( _ ) = > {
2020-09-12 01:52:06 +12:00
self . infos . warnings . push ( " Unable to check hash of file " . to_string ( ) + & * file_entry . 1. path ) ;
2020-08-29 06:30:22 +12:00
continue ;
}
} ;
2020-09-07 19:06:12 +12:00
let mut error_reading_file : bool = false ;
2020-08-29 06:30:22 +12:00
let mut hasher : blake3 ::Hasher = blake3 ::Hasher ::new ( ) ;
let mut buffer = [ 0 u8 ; 16384 ] ;
loop {
2020-09-07 19:06:12 +12:00
let n = match file_handler . read ( & mut buffer ) {
Ok ( t ) = > t ,
Err ( _ ) = > {
2020-09-12 01:52:06 +12:00
self . infos . warnings . push ( " Error happened when checking hash of file " . to_string ( ) + & * file_entry . 1. path ) ;
2020-09-07 19:06:12 +12:00
error_reading_file = true ;
break ;
}
2020-09-16 05:17:13 +12:00
} ;
2020-08-29 06:30:22 +12:00
if n = = 0 {
break ;
}
hasher . update ( & buffer [ .. n ] ) ;
}
2020-09-07 19:06:12 +12:00
if ! error_reading_file {
let hash_string : String = hasher . finalize ( ) . to_hex ( ) . to_string ( ) ;
hashmap_with_hash . entry ( hash_string . to_string ( ) ) . or_insert_with ( Vec ::new ) ;
hashmap_with_hash . get_mut ( & * hash_string ) . unwrap ( ) . push ( file_entry . 1. to_owned ( ) ) ;
}
2020-08-30 02:12:20 +12:00
}
for hash_entry in hashmap_with_hash {
if hash_entry . 1. len ( ) > 1 {
self . files_with_identical_hashes . entry ( * entry . 0 ) . or_insert_with ( Vec ::new ) ;
self . files_with_identical_hashes . get_mut ( entry . 0 ) . unwrap ( ) . push ( hash_entry . 1 ) ;
}
2020-08-29 06:30:22 +12:00
}
}
2020-09-16 05:17:13 +12:00
for ( size , vector ) in & self . files_with_identical_hashes {
for vec_file_entry in vector {
self . infos . number_of_duplicated_files_by_hash + = vec_file_entry . len ( ) - 1 ;
self . infos . number_of_groups_by_hash + = 1 ;
self . infos . lost_space_by_hash + = ( vec_file_entry . len ( ) as u64 - 1 ) * size ;
}
}
2020-09-01 05:37:30 +12:00
Common ::print_time ( start_time , SystemTime ::now ( ) , " check_files_hash " . to_string ( ) ) ;
2020-08-29 06:30:22 +12:00
}
2020-09-10 22:42:44 +12:00
#[ allow(dead_code) ]
2020-09-17 22:07:58 +12:00
#[ allow(unreachable_code) ]
2020-08-27 19:42:37 +12:00
/// Setting include directories, panics when there is not directories available
2020-08-30 02:12:20 +12:00
fn debug_print ( & self ) {
2020-09-17 22:07:58 +12:00
#[ cfg(not(debug_assertions)) ]
{
return ;
}
2020-09-08 19:41:22 +12:00
println! ( " ---------------DEBUG PRINT--------------- " ) ;
2020-09-16 05:17:13 +12:00
println! ( " ### Infos " ) ;
println! ( " Errors size - {} " , self . infos . errors . len ( ) ) ;
println! ( " Warnings size - {} " , self . infos . warnings . len ( ) ) ;
println! ( " Messages size - {} " , self . infos . messages . len ( ) ) ;
2020-09-12 01:52:06 +12:00
println! ( " Number of checked files - {} " , self . infos . number_of_checked_files ) ;
println! ( " Number of checked folders - {} " , self . infos . number_of_checked_folders ) ;
println! ( " Number of ignored files - {} " , self . infos . number_of_ignored_files ) ;
println! ( " Number of ignored things(like symbolic links) - {} " , self . infos . number_of_ignored_things ) ;
2020-09-16 05:17:13 +12:00
println! ( " Number of duplicated files by size(in groups) - {} ( {} ) " , self . infos . number_of_duplicated_files_by_size , self . infos . number_of_groups_by_size ) ;
println! ( " Number of duplicated files by hash(in groups) - {} ( {} ) " , self . infos . number_of_duplicated_files_by_hash , self . infos . number_of_groups_by_hash ) ;
println! ( " Lost space by size - {} ( {} bytes) " , self . infos . lost_space_by_size . file_size ( options ::BINARY ) . unwrap ( ) , self . infos . lost_space_by_size ) ;
println! ( " Lost space by hash - {} ( {} bytes) " , self . infos . lost_space_by_hash . file_size ( options ::BINARY ) . unwrap ( ) , self . infos . lost_space_by_hash ) ;
println! ( " Gained space by removing duplicated entries - {} ( {} bytes) " , self . infos . gained_space . file_size ( options ::BINARY ) . unwrap ( ) , self . infos . gained_space ) ;
2020-09-12 01:52:06 +12:00
println! ( " Number of removed files - {} " , self . infos . number_of_removed_files ) ;
println! ( " Number of failed to remove files - {} " , self . infos . number_of_failed_to_remove_files ) ;
2020-09-16 05:17:13 +12:00
println! ( " ### Other " ) ;
println! ( " Files list size - {} " , self . files_with_identical_size . len ( ) ) ;
println! ( " Hashed Files list size - {} " , self . files_with_identical_hashes . len ( ) ) ;
println! ( " Allowed extensions - {:?} " , self . allowed_extensions ) ;
println! ( " Excluded items - {:?} " , self . excluded_items ) ;
2020-09-08 19:41:22 +12:00
println! ( " Excluded directories - {:?} " , self . excluded_directories ) ;
2020-09-16 05:17:13 +12:00
println! ( " Recursive search - {} " , self . recursive_search . to_string ( ) ) ;
println! ( " Minimum file size - {:?} " , self . min_file_size ) ;
2020-09-08 19:41:22 +12:00
println! ( " ----------------------------------------- " ) ;
2020-08-27 06:50:07 +12:00
}
2020-09-10 22:42:44 +12:00
/// Print information about duplicated entries
2020-09-17 23:35:11 +12:00
pub fn print_duplicated_entries ( & self ) {
2020-08-29 06:30:22 +12:00
let start_time : SystemTime = SystemTime ::now ( ) ;
2020-08-30 02:12:20 +12:00
let mut number_of_files : u64 = 0 ;
let mut number_of_groups : u64 = 0 ;
2020-09-17 23:35:11 +12:00
match self . check_method {
2020-08-30 02:12:20 +12:00
CheckingMethod ::HASH = > {
2020-09-10 22:42:44 +12:00
for ( _size , vector ) in self . files_with_identical_hashes . iter ( ) {
for j in vector {
2020-08-30 02:12:20 +12:00
number_of_files + = j . len ( ) as u64 ;
number_of_groups + = 1 ;
}
}
2020-08-31 00:00:22 +12:00
println! (
2020-09-02 03:10:54 +12:00
" Found {} duplicated files in {} groups with same content which took {}: " ,
2020-08-31 00:00:22 +12:00
number_of_files ,
number_of_groups ,
2020-09-16 05:17:13 +12:00
self . infos . lost_space_by_size . file_size ( options ::BINARY ) . unwrap ( )
2020-08-31 00:00:22 +12:00
) ;
2020-09-17 23:35:11 +12:00
for ( size , vector ) in self . files_with_identical_hashes . iter ( ) . rev ( ) {
2020-09-10 22:42:44 +12:00
for j in vector {
2020-09-17 23:35:11 +12:00
println! ( " Size - {} ( {} ) - {} files " , size . file_size ( options ::BINARY ) . unwrap ( ) , size , j . len ( ) ) ;
2020-08-30 02:12:20 +12:00
for k in j {
println! ( " {} " , k . path ) ;
}
println! ( " ---- " ) ;
}
println! ( ) ;
}
2020-08-28 04:57:56 +12:00
}
2020-08-30 02:12:20 +12:00
CheckingMethod ::SIZE = > {
for i in & self . files_with_identical_size {
number_of_files + = i . 1. len ( ) as u64 ;
number_of_groups + = 1 ;
}
2020-08-31 00:00:22 +12:00
println! (
" Found {} files in {} groups with same size(may have different content) which took {}: " ,
number_of_files ,
number_of_groups ,
2020-09-16 05:17:13 +12:00
self . infos . lost_space_by_size . file_size ( options ::BINARY ) . unwrap ( )
2020-08-31 00:00:22 +12:00
) ;
2020-09-17 23:35:11 +12:00
for ( size , vector ) in & self . files_with_identical_size {
println! ( " Size - {} ( {} ) - {} files " , size . file_size ( options ::BINARY ) . unwrap ( ) , size , vector . len ( ) ) ;
for j in vector {
2020-08-31 03:18:04 +12:00
println! ( " {} " , j . path ) ;
}
println! ( ) ;
}
2020-08-27 19:42:37 +12:00
}
2020-09-17 22:07:58 +12:00
CheckingMethod ::NONE = > {
panic! ( " Checking Method shouldn't be ever set to NONE " ) ;
}
2020-08-27 06:50:07 +12:00
}
2020-09-01 05:37:30 +12:00
Common ::print_time ( start_time , SystemTime ::now ( ) , " print_duplicated_entries " . to_string ( ) ) ;
2020-08-27 08:24:02 +12:00
}
2020-08-27 19:42:37 +12:00
/// Remove unused entries when included or excluded overlaps with each other or are duplicated
2020-09-12 01:52:06 +12:00
fn optimize_directories ( & mut self ) -> bool {
2020-08-29 06:30:22 +12:00
let start_time : SystemTime = SystemTime ::now ( ) ;
2020-08-27 08:24:02 +12:00
let mut optimized_included : Vec < String > = Vec ::< String > ::new ( ) ;
let mut optimized_excluded : Vec < String > = Vec ::< String > ::new ( ) ;
2020-09-12 23:25:23 +12:00
2020-08-27 08:24:02 +12:00
// Remove duplicated entries like: "/", "/"
self . excluded_directories . sort ( ) ;
self . included_directories . sort ( ) ;
self . excluded_directories . dedup ( ) ;
self . included_directories . dedup ( ) ;
2020-09-01 05:37:30 +12:00
// Optimize for duplicated included directories - "/", "/home". "/home/Pulpit" to "/"
2020-09-16 05:17:13 +12:00
if self . recursive_search {
// This is only point which can't be done when recursive search is disabled.
2020-09-12 23:25:23 +12:00
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 ;
}
2020-08-27 19:42:37 +12:00
}
2020-09-12 23:25:23 +12:00
if ! is_inside {
optimized_excluded . push ( ed_checked . to_string ( ) ) ;
2020-08-27 19:42:37 +12:00
}
}
2020-09-12 23:25:23 +12:00
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 ;
}
2020-08-27 19:42:37 +12:00
}
2020-09-12 23:25:23 +12:00
if ! is_inside {
optimized_included . push ( id_checked . to_string ( ) ) ;
2020-08-27 19:42:37 +12:00
}
}
2020-09-12 23:25:23 +12:00
self . included_directories = optimized_included ;
optimized_included = Vec ::< String > ::new ( ) ;
self . excluded_directories = optimized_excluded ;
optimized_excluded = Vec ::< String > ::new ( ) ;
}
2020-08-27 08:24:02 +12:00
// Remove include directories which are inside any exclude directory
2020-08-28 04:57:56 +12:00
for id in & self . included_directories {
let mut is_inside : bool = false ;
for ed in & self . excluded_directories {
2020-08-27 08:24:02 +12:00
if id . starts_with ( ed ) {
2020-08-28 04:57:56 +12:00
is_inside = true ;
break ;
2020-08-27 08:24:02 +12:00
}
2020-08-28 04:57:56 +12:00
}
if ! is_inside {
2020-08-27 08:24:02 +12:00
optimized_included . push ( id . to_string ( ) ) ;
}
}
2020-08-28 04:57:56 +12:00
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 ( ) ) ;
}
}
2020-08-27 08:24:02 +12:00
self . included_directories = optimized_included ;
2020-08-27 19:42:37 +12:00
// optimized_included = Vec::<String>::new();
2020-08-28 04:57:56 +12:00
self . excluded_directories = optimized_excluded ;
optimized_excluded = Vec ::< String > ::new ( ) ;
2020-09-01 05:37:30 +12:00
// Excluded paths must are inside include path, because
2020-08-28 04:57:56 +12:00
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();
2020-08-27 08:24:02 +12:00
2020-08-29 06:30:22 +12:00
if self . included_directories . is_empty ( ) {
2020-09-12 01:52:06 +12:00
self . infos . errors . push ( " Optimize Directories ERROR: Excluded directories overlaps all included directories. " . to_string ( ) ) ;
return false ;
2020-08-27 08:24:02 +12:00
}
2020-08-27 19:42:37 +12:00
// Not needed, but better is to have sorted everything
self . excluded_directories . sort ( ) ;
self . included_directories . sort ( ) ;
2020-09-01 05:37:30 +12:00
Common ::print_time ( start_time , SystemTime ::now ( ) , " optimize_directories " . to_string ( ) ) ;
2020-09-12 01:52:06 +12:00
true
2020-08-27 06:50:07 +12:00
}
2020-08-30 05:11:55 +12:00
2020-09-17 22:07:58 +12:00
fn delete_files ( & mut self ) {
2020-08-30 05:11:55 +12:00
let start_time : SystemTime = SystemTime ::now ( ) ;
2020-09-12 01:52:06 +12:00
2020-09-17 22:07:58 +12:00
match self . check_method {
2020-08-30 05:11:55 +12:00
CheckingMethod ::HASH = > {
for entry in & self . files_with_identical_hashes {
for vector in entry . 1 {
2020-09-17 22:07:58 +12:00
let tuple : ( u64 , usize , usize ) = delete_files ( & vector , & self . delete_method , & mut self . infos . warnings ) ;
2020-09-12 01:52:06 +12:00
self . infos . gained_space + = tuple . 0 ;
self . infos . number_of_removed_files + = tuple . 1 ;
self . infos . number_of_failed_to_remove_files + = tuple . 2 ;
2020-08-30 05:11:55 +12:00
}
}
}
CheckingMethod ::SIZE = > {
for entry in & self . files_with_identical_size {
2020-09-17 22:07:58 +12:00
let tuple : ( u64 , usize , usize ) = delete_files ( & entry . 1 , & self . delete_method , & mut self . infos . warnings ) ;
2020-09-12 01:52:06 +12:00
self . infos . gained_space + = tuple . 0 ;
self . infos . number_of_removed_files + = tuple . 1 ;
self . infos . number_of_failed_to_remove_files + = tuple . 2 ;
2020-08-30 05:11:55 +12:00
}
}
2020-09-17 22:07:58 +12:00
CheckingMethod ::NONE = > {
//Just do nothing
panic! ( " Checking method should never be none. " ) ;
}
2020-08-30 05:11:55 +12:00
}
2020-09-12 01:52:06 +12:00
2020-09-01 05:37:30 +12:00
Common ::print_time ( start_time , SystemTime ::now ( ) , " delete_files " . to_string ( ) ) ;
2020-08-30 05:11:55 +12:00
}
2020-08-27 06:50:07 +12:00
}
2020-09-07 19:06:12 +12:00
impl Default for DuplicateFinder {
fn default ( ) -> Self {
Self ::new ( )
}
}
2020-09-12 01:52:06 +12:00
fn delete_files ( vector : & [ FileEntry ] , delete_method : & DeleteMethod , warnings : & mut Vec < String > ) -> ( u64 , usize , usize ) {
2020-09-01 02:15:56 +12:00
assert! ( vector . len ( ) > 1 , " Vector length must be bigger than 1(This should be done in previous steps). " ) ;
let mut q_index : usize = 0 ;
let mut q_time : u64 = 0 ;
2020-09-12 01:52:06 +12:00
let mut gained_space : u64 = 0 ;
let mut removed_files : usize = 0 ;
let mut failed_to_remove_files : usize = 0 ;
2020-09-01 02:15:56 +12:00
match delete_method {
DeleteMethod ::OneOldest = > {
for files in vector . iter ( ) . enumerate ( ) {
let time_since_epoch = files . 1. created_date . duration_since ( UNIX_EPOCH ) . expect ( " Invalid file date " ) . as_secs ( ) ;
if q_time = = 0 | | q_time > time_since_epoch {
q_time = time_since_epoch ;
q_index = files . 0 ;
}
}
match fs ::remove_file ( vector [ q_index ] . path . clone ( ) ) {
2020-09-12 01:52:06 +12:00
Ok ( _ ) = > {
removed_files + = 1 ;
gained_space + = vector [ q_index ] . size ;
}
Err ( _ ) = > {
failed_to_remove_files + = 1 ;
warnings . push ( " Failed to delete " . to_string ( ) + & * vector [ q_index ] . path ) ;
}
2020-09-01 02:15:56 +12:00
} ;
}
DeleteMethod ::OneNewest = > {
for files in vector . iter ( ) . enumerate ( ) {
let time_since_epoch = files . 1. created_date . duration_since ( UNIX_EPOCH ) . expect ( " Invalid file date " ) . as_secs ( ) ;
if q_time = = 0 | | q_time < time_since_epoch {
q_time = time_since_epoch ;
q_index = files . 0 ;
}
}
match fs ::remove_file ( vector [ q_index ] . path . clone ( ) ) {
2020-09-12 01:52:06 +12:00
Ok ( _ ) = > {
removed_files + = 1 ;
gained_space + = vector [ q_index ] . size ;
}
Err ( _ ) = > {
failed_to_remove_files + = 1 ;
warnings . push ( " Failed to delete " . to_string ( ) + & * vector [ q_index ] . path ) ;
}
2020-09-01 02:15:56 +12:00
} ;
}
DeleteMethod ::AllExceptOldest = > {
for files in vector . iter ( ) . enumerate ( ) {
let time_since_epoch = files . 1. created_date . duration_since ( UNIX_EPOCH ) . expect ( " Invalid file date " ) . as_secs ( ) ;
if q_time = = 0 | | q_time > time_since_epoch {
q_time = time_since_epoch ;
q_index = files . 0 ;
}
}
for files in vector . iter ( ) . enumerate ( ) {
if q_index ! = files . 0 {
match fs ::remove_file ( vector [ files . 0 ] . path . clone ( ) ) {
2020-09-12 01:52:06 +12:00
Ok ( _ ) = > {
removed_files + = 1 ;
gained_space + = vector [ files . 0 ] . size ;
}
Err ( _ ) = > {
failed_to_remove_files + = 1 ;
warnings . push ( " Failed to delete " . to_string ( ) + & * vector [ files . 0 ] . path ) ;
}
2020-09-01 02:15:56 +12:00
} ;
}
}
}
DeleteMethod ::AllExceptNewest = > {
for files in vector . iter ( ) . enumerate ( ) {
let time_since_epoch = files . 1. created_date . duration_since ( UNIX_EPOCH ) . expect ( " Invalid file date " ) . as_secs ( ) ;
if q_time = = 0 | | q_time < time_since_epoch {
q_time = time_since_epoch ;
q_index = files . 0 ;
}
}
for files in vector . iter ( ) . enumerate ( ) {
if q_index ! = files . 0 {
match fs ::remove_file ( vector [ files . 0 ] . path . clone ( ) ) {
2020-09-12 01:52:06 +12:00
Ok ( _ ) = > {
removed_files + = 1 ;
gained_space + = vector [ files . 0 ] . size ;
}
Err ( _ ) = > {
failed_to_remove_files + = 1 ;
warnings . push ( " Failed to delete " . to_string ( ) + & * vector [ files . 0 ] . path ) ;
}
2020-09-01 02:15:56 +12:00
} ;
}
}
}
DeleteMethod ::None = > {
2020-09-17 22:07:58 +12:00
// Just don't remove files
2020-09-01 02:15:56 +12:00
}
} ;
2020-09-12 01:52:06 +12:00
( gained_space , removed_files , failed_to_remove_files )
2020-08-27 06:50:07 +12:00
}