2020-09-27 08:31:14 +00:00
use std ::fs ::{ File , Metadata } ;
use std ::io ::prelude ::* ;
2021-11-27 19:49:20 +00:00
use std ::io ::BufWriter ;
2020-10-14 16:41:37 +00:00
use std ::path ::PathBuf ;
2021-11-27 19:49:20 +00:00
use std ::sync ::atomic ::{ AtomicBool , AtomicUsize , Ordering } ;
use std ::sync ::Arc ;
use std ::thread ::sleep ;
2020-12-02 09:25:27 +00:00
use std ::time ::{ Duration , SystemTime , UNIX_EPOCH } ;
use std ::{ fs , thread } ;
2020-09-27 08:31:14 +00:00
2021-11-27 19:49:20 +00:00
use crossbeam_channel ::Receiver ;
2021-12-17 18:29:37 +00:00
use rayon ::prelude ::* ;
2021-11-27 19:49:20 +00:00
2022-01-14 05:34:43 +00:00
use crate ::common ::{ Common , LOOP_DURATION } ;
2020-09-27 08:31:14 +00:00
use crate ::common_directory ::Directories ;
use crate ::common_items ::ExcludedItems ;
use crate ::common_messages ::Messages ;
use crate ::common_traits ::* ;
2022-01-19 21:35:07 +00:00
use crate ::flc ;
use crate ::localizer_core ::generate_translation_hashmap ;
2020-12-02 09:25:27 +00:00
#[ derive(Debug) ]
pub struct ProgressData {
pub current_stage : u8 ,
pub max_stage : u8 ,
pub files_checked : usize ,
}
2020-09-27 08:31:14 +00:00
2023-02-19 09:21:14 +00:00
#[ derive(Eq, PartialEq, Clone, Debug, Copy) ]
2020-09-27 08:31:14 +00:00
pub enum DeleteMethod {
None ,
Delete ,
}
#[ derive(Clone) ]
pub struct FileEntry {
2020-10-14 16:41:37 +00:00
pub path : PathBuf ,
2020-10-01 04:53:10 +00:00
pub modified_date : u64 ,
2020-09-27 08:31:14 +00:00
}
/// Info struck with helpful information's about results
2020-10-09 20:32:08 +00:00
#[ derive(Default) ]
2020-09-27 08:31:14 +00:00
pub struct Info {
pub number_of_temporary_files : usize ,
}
2021-11-27 19:57:10 +00:00
2020-09-27 08:31:14 +00:00
impl Info {
2023-01-28 17:54:02 +00:00
#[ must_use ]
2020-10-09 20:32:08 +00:00
pub fn new ( ) -> Self {
Default ::default ( )
2020-09-27 08:31:14 +00:00
}
}
/// Struct with required information's to work
pub struct Temporary {
text_messages : Messages ,
information : Info ,
temporary_files : Vec < FileEntry > ,
directories : Directories ,
excluded_items : ExcludedItems ,
recursive_search : bool ,
delete_method : DeleteMethod ,
2020-10-10 09:15:20 +00:00
stopped_search : bool ,
2020-09-27 08:31:14 +00:00
}
impl Temporary {
2023-01-28 17:54:02 +00:00
#[ must_use ]
2020-10-09 20:32:08 +00:00
pub fn new ( ) -> Self {
Self {
2020-09-27 08:31:14 +00:00
text_messages : Messages ::new ( ) ,
information : Info ::new ( ) ,
recursive_search : true ,
directories : Directories ::new ( ) ,
excluded_items : ExcludedItems ::new ( ) ,
delete_method : DeleteMethod ::None ,
temporary_files : vec ! [ ] ,
2020-10-10 09:15:20 +00:00
stopped_search : false ,
2020-09-27 08:31:14 +00:00
}
}
/// Finding temporary files, save results to internal struct variables
2021-04-14 06:23:59 +00:00
pub fn find_temporary_files ( & mut self , stop_receiver : Option < & Receiver < ( ) > > , progress_sender : Option < & futures ::channel ::mpsc ::UnboundedSender < ProgressData > > ) {
2020-09-27 08:31:14 +00:00
self . directories . optimize_directories ( self . recursive_search , & mut self . text_messages ) ;
2020-12-02 09:25:27 +00:00
if ! self . check_files ( stop_receiver , progress_sender ) {
2020-10-10 09:15:20 +00:00
self . stopped_search = true ;
return ;
}
2020-09-27 08:31:14 +00:00
self . delete_files ( ) ;
self . debug_print ( ) ;
}
2023-01-28 17:54:02 +00:00
#[ must_use ]
2020-10-10 09:15:20 +00:00
pub fn get_stopped_search ( & self ) -> bool {
self . stopped_search
}
2020-09-27 08:31:14 +00:00
2023-01-28 17:54:02 +00:00
#[ must_use ]
2020-10-09 20:32:08 +00:00
pub const fn get_temporary_files ( & self ) -> & Vec < FileEntry > {
2020-10-01 17:26:25 +00:00
& self . temporary_files
}
2023-01-28 17:54:02 +00:00
#[ must_use ]
2020-10-09 20:32:08 +00:00
pub const fn get_text_messages ( & self ) -> & Messages {
2020-09-27 08:31:14 +00:00
& self . text_messages
}
2023-01-28 17:54:02 +00:00
#[ must_use ]
2020-10-09 20:32:08 +00:00
pub const fn get_information ( & self ) -> & Info {
2020-09-27 08:31:14 +00:00
& self . information
}
pub fn set_delete_method ( & mut self , delete_method : DeleteMethod ) {
self . delete_method = delete_method ;
}
pub fn set_recursive_search ( & mut self , recursive_search : bool ) {
self . recursive_search = recursive_search ;
}
2022-04-23 18:21:46 +00:00
#[ cfg(target_family = " unix " ) ]
pub fn set_exclude_other_filesystems ( & mut self , exclude_other_filesystems : bool ) {
self . directories . set_exclude_other_filesystems ( exclude_other_filesystems ) ;
}
2022-07-19 17:09:52 +00:00
#[ cfg(not(target_family = " unix " )) ]
pub fn set_exclude_other_filesystems ( & mut self , _exclude_other_filesystems : bool ) { }
2022-04-23 18:21:46 +00:00
2021-01-10 19:44:10 +00:00
pub fn set_included_directory ( & mut self , included_directory : Vec < PathBuf > ) -> bool {
2020-09-27 08:31:14 +00:00
self . directories . set_included_directory ( included_directory , & mut self . text_messages )
}
2021-01-10 19:44:10 +00:00
pub fn set_excluded_directory ( & mut self , excluded_directory : Vec < PathBuf > ) {
2020-09-27 08:31:14 +00:00
self . directories . set_excluded_directory ( excluded_directory , & mut self . text_messages ) ;
}
2021-01-10 19:44:10 +00:00
pub fn set_excluded_items ( & mut self , excluded_items : Vec < String > ) {
2020-09-27 08:31:14 +00:00
self . excluded_items . set_excluded_items ( excluded_items , & mut self . text_messages ) ;
}
2021-04-14 06:23:59 +00:00
fn check_files ( & mut self , stop_receiver : Option < & Receiver < ( ) > > , progress_sender : Option < & futures ::channel ::mpsc ::UnboundedSender < ProgressData > > ) -> bool {
2020-09-27 08:31:14 +00:00
let start_time : SystemTime = SystemTime ::now ( ) ;
2020-10-14 16:41:37 +00:00
let mut folders_to_check : Vec < PathBuf > = 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-09-27 08:31:14 +00:00
// Add root folders for finding
for id in & self . directories . included_directories {
2020-10-14 16:41:37 +00:00
folders_to_check . push ( id . clone ( ) ) ;
2020-09-27 08:31:14 +00:00
}
2020-12-02 09:25:27 +00:00
//// PROGRESS THREAD START
let progress_thread_run = Arc ::new ( AtomicBool ::new ( true ) ) ;
let atomic_file_counter = Arc ::new ( AtomicUsize ::new ( 0 ) ) ;
2021-12-18 22:45:37 +00:00
let progress_thread_handle = if let Some ( progress_sender ) = progress_sender {
2021-04-14 06:23:59 +00:00
let progress_send = progress_sender . clone ( ) ;
2020-12-02 09:25:27 +00:00
let progress_thread_run = progress_thread_run . clone ( ) ;
let atomic_file_counter = atomic_file_counter . clone ( ) ;
2021-12-18 22:45:37 +00:00
thread ::spawn ( move | | loop {
2020-12-02 09:25:27 +00:00
progress_send
2021-04-14 06:23:59 +00:00
. unbounded_send ( ProgressData {
2020-12-02 09:25:27 +00:00
current_stage : 0 ,
max_stage : 0 ,
2022-11-23 19:23:17 +00:00
files_checked : atomic_file_counter . load ( Ordering ::Relaxed ) ,
2020-12-02 09:25:27 +00:00
} )
. unwrap ( ) ;
if ! progress_thread_run . load ( Ordering ::Relaxed ) {
break ;
}
sleep ( Duration ::from_millis ( LOOP_DURATION as u64 ) ) ;
2021-12-18 22:45:37 +00:00
} )
2020-12-02 09:25:27 +00:00
} else {
2021-12-18 22:45:37 +00:00
thread ::spawn ( | | { } )
} ;
2020-12-02 09:25:27 +00:00
//// PROGRESS THREAD END
2020-09-27 08:31:14 +00:00
while ! folders_to_check . is_empty ( ) {
2020-11-04 17:15:06 +00:00
if stop_receiver . is_some ( ) & & stop_receiver . unwrap ( ) . try_recv ( ) . is_ok ( ) {
2020-12-02 09:25:27 +00:00
// End thread which send info to gui
progress_thread_run . store ( false , Ordering ::Relaxed ) ;
progress_thread_handle . join ( ) . unwrap ( ) ;
2020-10-10 09:15:20 +00:00
return false ;
}
2020-09-27 08:31:14 +00:00
2021-12-17 18:29:37 +00:00
let segments : Vec < _ > = folders_to_check
. par_iter ( )
. map ( | current_folder | {
let mut dir_result = vec! [ ] ;
let mut warnings = vec! [ ] ;
let mut fe_result = vec! [ ] ;
2022-10-22 18:43:06 +00:00
// Read current dir children
2022-11-23 19:23:17 +00:00
let read_dir = match fs ::read_dir ( current_folder ) {
2020-10-06 15:10:34 +00:00
Ok ( t ) = > t ,
2021-12-17 18:29:37 +00:00
Err ( e ) = > {
2022-01-19 21:35:07 +00:00
warnings . push ( flc! (
2021-12-21 17:44:20 +00:00
" core_cannot_open_dir " ,
generate_translation_hashmap ( vec! [ ( " dir " , current_folder . display ( ) . to_string ( ) ) , ( " reason " , e . to_string ( ) ) ] )
) ) ;
2021-12-17 18:29:37 +00:00
return ( dir_result , warnings , fe_result ) ;
2021-11-14 14:53:55 +00:00
}
2021-12-17 18:29:37 +00:00
} ;
2020-10-10 13:18:04 +00:00
2021-12-17 18:29:37 +00:00
// Check every sub folder/file/link etc.
' dir : for entry in read_dir {
let entry_data = match entry {
Ok ( t ) = > t ,
Err ( e ) = > {
2022-01-19 21:35:07 +00:00
warnings . push ( flc! (
2021-12-21 17:44:20 +00:00
" core_cannot_read_entry_dir " ,
generate_translation_hashmap ( vec! [ ( " dir " , current_folder . display ( ) . to_string ( ) ) , ( " reason " , e . to_string ( ) ) ] )
) ) ;
2021-12-17 18:29:37 +00:00
continue 'dir ;
}
} ;
let metadata : Metadata = match entry_data . metadata ( ) {
Ok ( t ) = > t ,
Err ( e ) = > {
2022-01-19 21:35:07 +00:00
warnings . push ( flc! (
2021-12-21 17:44:20 +00:00
" core_cannot_read_metadata_dir " ,
generate_translation_hashmap ( vec! [ ( " dir " , current_folder . display ( ) . to_string ( ) ) , ( " reason " , e . to_string ( ) ) ] )
) ) ;
2021-12-17 18:29:37 +00:00
continue 'dir ;
}
} ;
if metadata . is_dir ( ) {
if ! self . recursive_search {
continue 'dir ;
}
let next_folder = current_folder . join ( entry_data . file_name ( ) ) ;
if self . directories . is_excluded ( & next_folder ) {
continue 'dir ;
}
if self . excluded_items . is_excluded ( & next_folder ) {
continue 'dir ;
}
2022-04-23 18:21:46 +00:00
#[ cfg(target_family = " unix " ) ]
if self . directories . exclude_other_filesystems ( ) {
match self . directories . is_on_other_filesystems ( & next_folder ) {
Ok ( true ) = > continue 'dir ,
Err ( e ) = > warnings . push ( e . to_string ( ) ) ,
_ = > ( ) ,
}
}
2021-12-17 18:29:37 +00:00
dir_result . push ( next_folder ) ;
} else if metadata . is_file ( ) {
atomic_file_counter . fetch_add ( 1 , Ordering ::Relaxed ) ;
let file_name_lowercase : String = match entry_data . file_name ( ) . into_string ( ) {
Ok ( t ) = > t ,
2021-11-14 14:53:55 +00:00
Err ( _inspected ) = > {
2022-01-19 21:35:07 +00:00
warnings . push ( flc! (
2021-12-21 17:44:20 +00:00
" core_file_not_utf8_name " ,
generate_translation_hashmap ( vec! [ ( " name " , entry_data . path ( ) . display ( ) . to_string ( ) ) ] )
) ) ;
2021-12-17 18:29:37 +00:00
continue 'dir ;
2020-10-09 20:32:08 +00:00
}
2021-12-17 18:29:37 +00:00
}
. to_lowercase ( ) ;
2021-12-21 17:44:20 +00:00
if ! [
" # " ,
" thumbs.db " ,
" .bak " ,
" ~ " ,
" .tmp " ,
" .temp " ,
" .ds_store " ,
" .crdownload " ,
" .part " ,
" .cache " ,
" .dmp " ,
" .download " ,
" .partial " ,
]
. iter ( )
. any ( | f | file_name_lowercase . ends_with ( f ) )
2021-12-17 18:29:37 +00:00
{
continue 'dir ;
}
let current_file_name = current_folder . join ( entry_data . file_name ( ) ) ;
if self . excluded_items . is_excluded ( & current_file_name ) {
continue 'dir ;
}
// Creating new file entry
let fe : FileEntry = FileEntry {
path : current_file_name . clone ( ) ,
modified_date : match metadata . modified ( ) {
Ok ( t ) = > match t . duration_since ( UNIX_EPOCH ) {
Ok ( d ) = > d . as_secs ( ) ,
Err ( _inspected ) = > {
2022-01-19 21:35:07 +00:00
warnings . push ( flc! (
2021-12-21 17:44:20 +00:00
" core_file_modified_before_epoch " ,
generate_translation_hashmap ( vec! [ ( " name " , current_file_name . display ( ) . to_string ( ) ) ] )
) ) ;
2021-12-17 18:29:37 +00:00
0
}
} ,
Err ( e ) = > {
2022-01-19 21:35:07 +00:00
warnings . push ( flc! (
2021-12-19 13:45:10 +00:00
" core_file_no_modification_date " ,
generate_translation_hashmap ( vec! [ ( " name " , current_file_name . display ( ) . to_string ( ) ) , ( " reason " , e . to_string ( ) ) ] )
) ) ;
2021-12-17 18:29:37 +00:00
0
} // Permissions Denied
} ,
} ;
fe_result . push ( fe ) ;
}
}
( dir_result , warnings , fe_result )
} )
. collect ( ) ;
// Advance the frontier
folders_to_check . clear ( ) ;
// Process collected data
for ( segment , warnings , fe_result ) in segments {
folders_to_check . extend ( segment ) ;
self . text_messages . warnings . extend ( warnings ) ;
for fe in fe_result {
2020-10-09 20:32:08 +00:00
self . temporary_files . push ( fe ) ;
2020-09-27 08:31:14 +00:00
}
}
}
2021-12-17 18:29:37 +00:00
2020-12-02 09:25:27 +00:00
// End thread which send info to gui
progress_thread_run . store ( false , Ordering ::Relaxed ) ;
progress_thread_handle . join ( ) . unwrap ( ) ;
2020-09-27 08:31:14 +00:00
self . information . number_of_temporary_files = self . temporary_files . len ( ) ;
2023-01-28 17:54:02 +00:00
Common ::print_time ( start_time , SystemTime ::now ( ) , " check_files_size " ) ;
2020-10-10 09:15:20 +00:00
true
2020-09-27 08:31:14 +00:00
}
/// Function to delete files, from filed Vector
fn delete_files ( & mut self ) {
let start_time : SystemTime = SystemTime ::now ( ) ;
match self . delete_method {
DeleteMethod ::Delete = > {
for file_entry in & self . temporary_files {
if fs ::remove_file ( file_entry . path . clone ( ) ) . is_err ( ) {
2020-10-14 16:41:37 +00:00
self . text_messages . warnings . push ( file_entry . path . display ( ) . to_string ( ) ) ;
2020-09-27 08:31:14 +00:00
}
}
}
DeleteMethod ::None = > {
//Just do nothing
}
}
2023-01-28 17:54:02 +00:00
Common ::print_time ( start_time , SystemTime ::now ( ) , " delete_files " ) ;
2020-09-27 08:31:14 +00:00
}
}
2021-11-27 19:57:10 +00:00
2020-09-27 08:31:14 +00:00
impl Default for Temporary {
fn default ( ) -> Self {
Self ::new ( )
}
}
impl DebugPrint for Temporary {
#[ allow(dead_code) ]
#[ allow(unreachable_code) ]
fn debug_print ( & self ) {
#[ cfg(not(debug_assertions)) ]
{
return ;
}
println! ( " ---------------DEBUG PRINT--------------- " ) ;
println! ( " ### Information's " ) ;
println! ( " Errors size - {} " , self . text_messages . errors . len ( ) ) ;
println! ( " Warnings size - {} " , self . text_messages . warnings . len ( ) ) ;
println! ( " Messages size - {} " , self . text_messages . messages . len ( ) ) ;
println! ( " ### Other " ) ;
println! ( " Temporary list size - {} " , self . temporary_files . len ( ) ) ;
println! ( " Excluded items - {:?} " , self . excluded_items . items ) ;
println! ( " Included directories - {:?} " , self . directories . included_directories ) ;
println! ( " Excluded directories - {:?} " , self . directories . excluded_directories ) ;
2021-11-23 10:10:24 +00:00
println! ( " Recursive search - {} " , self . recursive_search ) ;
2022-04-23 18:21:46 +00:00
#[ cfg(target_family = " unix " ) ]
2022-05-31 15:52:55 +00:00
println! ( " Skip other filesystems - {} " , self . directories . exclude_other_filesystems ( ) ) ;
2020-09-27 08:31:14 +00:00
println! ( " Delete Method - {:?} " , self . delete_method ) ;
println! ( " ----------------------------------------- " ) ;
}
}
2021-11-27 19:57:10 +00:00
2020-09-27 08:31:14 +00:00
impl SaveResults for Temporary {
fn save_results_to_file ( & mut self , file_name : & str ) -> bool {
let start_time : SystemTime = SystemTime ::now ( ) ;
let file_name : String = match file_name {
" " = > " results.txt " . to_string ( ) ,
k = > k . to_string ( ) ,
} ;
2021-01-09 10:52:43 +00:00
let file_handler = match File ::create ( & file_name ) {
2020-09-27 08:31:14 +00:00
Ok ( t ) = > t ,
2021-11-14 14:53:55 +00:00
Err ( e ) = > {
2022-12-21 07:44:26 +00:00
self . text_messages . errors . push ( format! ( " Failed to create file {file_name} , reason {e} " ) ) ;
2020-09-27 08:31:14 +00:00
return false ;
}
} ;
2021-01-09 10:52:43 +00:00
let mut writer = BufWriter ::new ( file_handler ) ;
2020-09-27 08:31:14 +00:00
2021-11-14 14:53:55 +00:00
if let Err ( e ) = writeln! (
2021-01-09 10:52:43 +00:00
writer ,
2020-10-14 16:41:37 +00:00
" Results of searching {:?} with excluded directories {:?} and excluded items {:?} " ,
self . directories . included_directories , self . directories . excluded_directories , self . excluded_items . items
2021-11-14 14:53:55 +00:00
) {
2022-12-21 07:44:26 +00:00
self . text_messages . errors . push ( format! ( " Failed to save results to file {file_name} , reason {e} " ) ) ;
2020-10-14 16:41:37 +00:00
return false ;
2020-09-27 08:31:14 +00:00
}
if ! self . temporary_files . is_empty ( ) {
2021-01-09 10:52:43 +00:00
writeln! ( writer , " Found {} temporary files. " , self . information . number_of_temporary_files ) . unwrap ( ) ;
2023-01-28 17:54:02 +00:00
for file_entry in & self . temporary_files {
2021-01-09 10:52:43 +00:00
writeln! ( writer , " {} " , file_entry . path . display ( ) ) . unwrap ( ) ;
2020-09-27 08:31:14 +00:00
}
} else {
2021-01-09 10:52:43 +00:00
write! ( writer , " Not found any temporary files. " ) . unwrap ( ) ;
2020-09-27 08:31:14 +00:00
}
2023-01-28 17:54:02 +00:00
Common ::print_time ( start_time , SystemTime ::now ( ) , " save_results_to_file " ) ;
2020-09-27 08:31:14 +00:00
true
}
}
2021-11-27 19:57:10 +00:00
2020-09-27 08:31:14 +00:00
impl PrintResults for Temporary {
fn print_results ( & self ) {
let start_time : SystemTime = SystemTime ::now ( ) ;
println! ( " Found {} temporary files. \n " , self . information . number_of_temporary_files ) ;
2023-01-28 17:54:02 +00:00
for file_entry in & self . temporary_files {
2020-10-14 16:41:37 +00:00
println! ( " {} " , file_entry . path . display ( ) ) ;
2020-09-27 08:31:14 +00:00
}
2023-01-28 17:54:02 +00:00
Common ::print_time ( start_time , SystemTime ::now ( ) , " print_entries " ) ;
2020-09-27 08:31:14 +00:00
}
}