2021-11-28 08:49:20 +13:00
use std ::collections ::BTreeMap ;
2021-01-14 04:03:05 +13:00
use std ::fs ::{ File , Metadata , OpenOptions } ;
2021-01-13 08:06:12 +13:00
use std ::io ::prelude ::* ;
2021-11-28 08:49:20 +13:00
use std ::io ::{ BufReader , BufWriter } ;
2021-01-14 04:03:05 +13:00
use std ::path ::{ Path , PathBuf } ;
2021-11-28 08:49:20 +13:00
use std ::sync ::atomic ::{ AtomicBool , AtomicUsize , Ordering } ;
use std ::sync ::Arc ;
use std ::thread ::sleep ;
2021-01-13 08:06:12 +13:00
use std ::time ::{ Duration , SystemTime , UNIX_EPOCH } ;
2021-12-30 01:43:38 +13:00
use std ::{ fs , mem , panic , thread } ;
2021-01-13 08:06:12 +13:00
2021-11-28 08:49:20 +13:00
use crossbeam_channel ::Receiver ;
use directories_next ::ProjectDirs ;
use rayon ::prelude ::* ;
2021-01-13 08:06:12 +13:00
use crate ::common ::Common ;
use crate ::common_directory ::Directories ;
use crate ::common_extensions ::Extensions ;
use crate ::common_items ::ExcludedItems ;
use crate ::common_messages ::Messages ;
use crate ::common_traits ::* ;
2021-12-20 02:45:10 +13:00
use crate ::fl ;
use crate ::localizer ::generate_translation_hashmap ;
2022-01-01 10:34:24 +13:00
use crate ::similar_images ::{ AUDIO_FILES_EXTENSIONS , IMAGE_RS_BROKEN_FILES_EXTENSIONS , ZIP_FILES_EXTENSIONS } ;
2021-01-13 08:06:12 +13:00
2021-01-14 04:03:05 +13:00
const CACHE_FILE_NAME : & str = " cache_broken_files.txt " ;
2021-01-13 08:06:12 +13:00
#[ derive(Debug) ]
pub struct ProgressData {
pub current_stage : u8 ,
pub max_stage : u8 ,
pub files_checked : usize ,
pub files_to_check : usize ,
}
#[ derive(Eq, PartialEq, Clone, Debug) ]
pub enum DeleteMethod {
None ,
Delete ,
}
#[ derive(Clone) ]
pub struct FileEntry {
pub path : PathBuf ,
pub modified_date : u64 ,
2021-01-14 04:03:05 +13:00
pub size : u64 ,
2021-01-13 08:06:12 +13:00
pub type_of_file : TypeOfFile ,
pub error_string : String ,
}
2021-01-14 04:03:05 +13:00
#[ derive(Copy, Clone, PartialEq, Eq) ]
2021-01-13 08:06:12 +13:00
pub enum TypeOfFile {
2021-01-14 04:03:05 +13:00
Unknown = - 1 ,
Image = 0 ,
2021-03-28 01:14:02 +13:00
ArchiveZip ,
2021-02-06 07:07:20 +13:00
#[ cfg(feature = " broken_audio " ) ]
2021-01-15 23:04:52 +13:00
Audio ,
2021-01-13 08:06:12 +13:00
}
/// Info struck with helpful information's about results
#[ derive(Default) ]
pub struct Info {
pub number_of_broken_files : usize ,
}
2021-11-28 08:57:10 +13:00
2021-01-13 08:06:12 +13:00
impl Info {
pub fn new ( ) -> Self {
Default ::default ( )
}
}
/// Struct with required information's to work
pub struct BrokenFiles {
text_messages : Messages ,
information : Info ,
2021-05-09 04:29:52 +12:00
files_to_check : BTreeMap < String , FileEntry > ,
2021-01-13 08:06:12 +13:00
broken_files : Vec < FileEntry > ,
directories : Directories ,
allowed_extensions : Extensions ,
excluded_items : ExcludedItems ,
recursive_search : bool ,
delete_method : DeleteMethod ,
stopped_search : bool ,
2021-03-05 00:09:53 +13:00
use_cache : bool ,
2021-01-13 08:06:12 +13:00
}
impl BrokenFiles {
pub fn new ( ) -> Self {
Self {
text_messages : Messages ::new ( ) ,
information : Info ::new ( ) ,
recursive_search : true ,
allowed_extensions : Extensions ::new ( ) ,
directories : Directories ::new ( ) ,
excluded_items : ExcludedItems ::new ( ) ,
2021-01-14 04:03:05 +13:00
files_to_check : Default ::default ( ) ,
2021-01-13 08:06:12 +13:00
delete_method : DeleteMethod ::None ,
stopped_search : false ,
2021-01-14 04:03:05 +13:00
broken_files : Default ::default ( ) ,
2021-03-05 00:09:53 +13:00
use_cache : true ,
2021-01-13 08:06:12 +13:00
}
}
2021-04-14 18:23:59 +12:00
pub fn find_broken_files ( & mut self , stop_receiver : Option < & Receiver < ( ) > > , progress_sender : Option < & futures ::channel ::mpsc ::UnboundedSender < ProgressData > > ) {
2021-01-13 08:06:12 +13:00
self . directories . optimize_directories ( self . recursive_search , & mut self . text_messages ) ;
if ! self . check_files ( stop_receiver , progress_sender ) {
self . stopped_search = true ;
return ;
}
if ! self . look_for_broken_files ( stop_receiver , progress_sender ) {
self . stopped_search = true ;
return ;
}
self . delete_files ( ) ;
self . debug_print ( ) ;
}
pub fn get_stopped_search ( & self ) -> bool {
self . stopped_search
}
pub const fn get_broken_files ( & self ) -> & Vec < FileEntry > {
& self . broken_files
}
pub const fn get_text_messages ( & self ) -> & Messages {
& self . text_messages
}
pub const fn get_information ( & self ) -> & Info {
& self . information
}
pub fn set_delete_method ( & mut self , delete_method : DeleteMethod ) {
self . delete_method = delete_method ;
}
2021-03-05 00:09:53 +13:00
pub fn set_use_cache ( & mut self , use_cache : bool ) {
self . use_cache = use_cache ;
}
2021-01-13 08:06:12 +13:00
pub fn set_recursive_search ( & mut self , recursive_search : bool ) {
self . recursive_search = recursive_search ;
}
pub fn set_included_directory ( & mut self , included_directory : Vec < PathBuf > ) -> bool {
self . directories . set_included_directory ( included_directory , & mut self . text_messages )
}
pub fn set_excluded_directory ( & mut self , excluded_directory : Vec < PathBuf > ) {
self . directories . set_excluded_directory ( excluded_directory , & mut self . text_messages ) ;
}
pub fn set_allowed_extensions ( & mut self , allowed_extensions : String ) {
self . allowed_extensions . set_allowed_extensions ( allowed_extensions , & mut self . text_messages ) ;
}
pub fn set_excluded_items ( & mut self , excluded_items : Vec < String > ) {
self . excluded_items . set_excluded_items ( excluded_items , & mut self . text_messages ) ;
}
2021-04-14 18:23:59 +12:00
fn check_files ( & mut self , stop_receiver : Option < & Receiver < ( ) > > , progress_sender : Option < & futures ::channel ::mpsc ::UnboundedSender < ProgressData > > ) -> bool {
2021-01-13 08:06:12 +13:00
let start_time : SystemTime = SystemTime ::now ( ) ;
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
// Add root folders for finding
for id in & self . directories . included_directories {
folders_to_check . push ( id . clone ( ) ) ;
}
//// PROGRESS THREAD START
const LOOP_DURATION : u32 = 200 ; //in ms
let progress_thread_run = Arc ::new ( AtomicBool ::new ( true ) ) ;
let atomic_file_counter = Arc ::new ( AtomicUsize ::new ( 0 ) ) ;
2021-12-19 11:45:37 +13:00
let progress_thread_handle = if let Some ( progress_sender ) = progress_sender {
2021-04-14 18:23:59 +12:00
let progress_send = progress_sender . clone ( ) ;
2021-01-13 08:06:12 +13:00
let progress_thread_run = progress_thread_run . clone ( ) ;
let atomic_file_counter = atomic_file_counter . clone ( ) ;
2021-12-19 11:45:37 +13:00
thread ::spawn ( move | | loop {
2021-01-13 08:06:12 +13:00
progress_send
2021-04-14 18:23:59 +12:00
. unbounded_send ( ProgressData {
2021-01-13 08:06:12 +13:00
current_stage : 0 ,
max_stage : 1 ,
files_checked : atomic_file_counter . load ( Ordering ::Relaxed ) as usize ,
files_to_check : 0 ,
} )
. unwrap ( ) ;
if ! progress_thread_run . load ( Ordering ::Relaxed ) {
break ;
}
sleep ( Duration ::from_millis ( LOOP_DURATION as u64 ) ) ;
2021-12-19 11:45:37 +13:00
} )
2021-01-13 08:06:12 +13:00
} else {
2021-12-19 11:45:37 +13:00
thread ::spawn ( | | { } )
} ;
2021-01-13 08:06:12 +13:00
//// PROGRESS THREAD END
while ! folders_to_check . is_empty ( ) {
if stop_receiver . is_some ( ) & & stop_receiver . unwrap ( ) . try_recv ( ) . is_ok ( ) {
// End thread which send info to gui
progress_thread_run . store ( false , Ordering ::Relaxed ) ;
progress_thread_handle . join ( ) . unwrap ( ) ;
return false ;
}
2021-12-18 07:29:37 +13: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! [ ] ;
// Read current dir childrens
let read_dir = match fs ::read_dir ( & current_folder ) {
2021-01-13 08:06:12 +13:00
Ok ( t ) = > t ,
2021-12-18 07:29:37 +13:00
Err ( e ) = > {
2021-12-22 06:44:20 +13:00
warnings . push ( fl! (
" core_cannot_open_dir " ,
generate_translation_hashmap ( vec! [ ( " dir " , current_folder . display ( ) . to_string ( ) ) , ( " reason " , e . to_string ( ) ) ] )
) ) ;
2021-12-18 07:29:37 +13:00
return ( dir_result , warnings , fe_result ) ;
2021-11-15 03:53:55 +13:00
}
2021-12-18 07:29:37 +13:00
} ;
2021-01-13 08:06:12 +13:00
2021-12-18 07:29:37 +13:00
// Check every sub folder/file/link etc.
' dir : for entry in read_dir {
let entry_data = match entry {
Ok ( t ) = > t ,
Err ( e ) = > {
2021-12-22 06:44:20 +13:00
warnings . push ( fl! (
" core_cannot_read_entry_dir " ,
generate_translation_hashmap ( vec! [ ( " dir " , current_folder . display ( ) . to_string ( ) ) , ( " reason " , e . to_string ( ) ) ] )
) ) ;
2021-12-18 07:29:37 +13:00
continue 'dir ;
}
} ;
let metadata : Metadata = match entry_data . metadata ( ) {
Ok ( t ) = > t ,
Err ( e ) = > {
2021-12-22 06:44:20 +13:00
warnings . push ( fl! (
" core_cannot_read_metadata_dir " ,
generate_translation_hashmap ( vec! [ ( " dir " , current_folder . display ( ) . to_string ( ) ) , ( " reason " , e . to_string ( ) ) ] )
) ) ;
2021-12-18 07:29:37 +13:00
continue 'dir ;
}
} ;
if metadata . is_dir ( ) {
if ! self . recursive_search {
continue 'dir ;
}
2021-01-13 08:06:12 +13:00
2021-12-18 07:29:37 +13:00
let next_folder = current_folder . join ( entry_data . file_name ( ) ) ;
if self . directories . is_excluded ( & next_folder ) {
continue 'dir ;
}
2021-01-13 08:06:12 +13:00
2021-12-18 07:29:37 +13:00
if self . excluded_items . is_excluded ( & next_folder ) {
continue 'dir ;
}
2021-01-13 08:06:12 +13:00
2021-12-18 07:29:37 +13: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-15 03:53:55 +13:00
Err ( _inspected ) = > {
2021-12-22 06:44:20 +13:00
warnings . push ( fl! (
" core_file_not_utf8_name " ,
generate_translation_hashmap ( vec! [ ( " name " , entry_data . path ( ) . display ( ) . to_string ( ) ) ] )
) ) ;
2021-12-18 07:29:37 +13:00
continue 'dir ;
2021-01-13 08:06:12 +13:00
}
2021-12-18 07:29:37 +13:00
}
. to_lowercase ( ) ;
if ! self . allowed_extensions . matches_filename ( & file_name_lowercase ) {
continue 'dir ;
}
let type_of_file = check_extension_avaibility ( & file_name_lowercase ) ;
if type_of_file = = TypeOfFile ::Unknown {
continue 'dir ;
}
let current_file_name = current_folder . join ( entry_data . file_name ( ) ) ;
if self . excluded_items . is_excluded ( & current_file_name ) {
continue 'dir ;
}
2021-01-13 08:06:12 +13:00
2021-12-18 07:29:37 +13:00
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 ) = > {
2021-12-22 06:44:20 +13:00
warnings . push ( fl! (
" core_file_modified_before_epoch " ,
generate_translation_hashmap ( vec! [ ( " name " , current_file_name . display ( ) . to_string ( ) ) ] )
) ) ;
2021-12-18 07:29:37 +13:00
0
}
} ,
Err ( e ) = > {
2021-12-20 02:45:10 +13:00
warnings . push ( fl! (
" core_file_no_modification_date " ,
generate_translation_hashmap ( vec! [ ( " name " , current_file_name . display ( ) . to_string ( ) ) , ( " reason " , e . to_string ( ) ) ] )
) ) ;
2021-12-18 07:29:37 +13:00
0
}
} ,
size : metadata . len ( ) ,
type_of_file ,
error_string : " " . to_string ( ) ,
} ;
fe_result . push ( ( current_file_name . to_string_lossy ( ) . to_string ( ) , 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 ( name , fe ) in fe_result {
self . files_to_check . insert ( name , fe ) ;
2021-01-13 08:06:12 +13:00
}
}
}
2021-12-18 07:29:37 +13:00
2021-01-13 08:06:12 +13:00
// End thread which send info to gui
progress_thread_run . store ( false , Ordering ::Relaxed ) ;
progress_thread_handle . join ( ) . unwrap ( ) ;
Common ::print_time ( start_time , SystemTime ::now ( ) , " check_files " . to_string ( ) ) ;
true
}
2021-04-14 18:23:59 +12:00
fn look_for_broken_files ( & mut self , stop_receiver : Option < & Receiver < ( ) > > , progress_sender : Option < & futures ::channel ::mpsc ::UnboundedSender < ProgressData > > ) -> bool {
2021-01-13 08:06:12 +13:00
let system_time = SystemTime ::now ( ) ;
2021-03-05 00:09:53 +13:00
let loaded_hash_map ;
2021-01-14 04:03:05 +13:00
2021-05-09 04:29:52 +12:00
let mut records_already_cached : BTreeMap < String , FileEntry > = Default ::default ( ) ;
let mut non_cached_files_to_check : BTreeMap < String , FileEntry > = Default ::default ( ) ;
2021-03-05 00:09:53 +13:00
if self . use_cache {
loaded_hash_map = match load_cache_from_file ( & mut self . text_messages ) {
Some ( t ) = > t ,
None = > Default ::default ( ) ,
} ;
for ( name , file_entry ) in & self . files_to_check {
2021-03-28 01:14:02 +13:00
#[ allow(clippy::if_same_then_else) ]
2021-03-05 00:09:53 +13:00
if ! loaded_hash_map . contains_key ( name ) {
// If loaded data doesn't contains current image info
2021-01-14 04:03:05 +13:00
non_cached_files_to_check . insert ( name . clone ( ) , file_entry . clone ( ) ) ;
2021-03-28 01:14:02 +13:00
} else if file_entry . size ! = loaded_hash_map . get ( name ) . unwrap ( ) . size | | file_entry . modified_date ! = loaded_hash_map . get ( name ) . unwrap ( ) . modified_date {
// When size or modification date of image changed, then it is clear that is different image
non_cached_files_to_check . insert ( name . clone ( ) , file_entry . clone ( ) ) ;
2021-01-14 04:03:05 +13:00
} else {
2021-03-28 01:14:02 +13:00
// Checking may be omitted when already there is entry with same size and modification date
records_already_cached . insert ( name . clone ( ) , loaded_hash_map . get ( name ) . unwrap ( ) . clone ( ) ) ;
2021-01-14 04:03:05 +13:00
}
}
2021-03-05 00:09:53 +13:00
} else {
loaded_hash_map = Default ::default ( ) ;
mem ::swap ( & mut self . files_to_check , & mut non_cached_files_to_check ) ;
2021-01-14 04:03:05 +13:00
}
2021-01-13 08:06:12 +13:00
let check_was_breaked = AtomicBool ::new ( false ) ; // Used for breaking from GUI and ending check thread
//// PROGRESS THREAD START
const LOOP_DURATION : u32 = 200 ; //in ms
let progress_thread_run = Arc ::new ( AtomicBool ::new ( true ) ) ;
let atomic_file_counter = Arc ::new ( AtomicUsize ::new ( 0 ) ) ;
2021-12-19 11:45:37 +13:00
let progress_thread_handle = if let Some ( progress_sender ) = progress_sender {
2021-04-14 18:23:59 +12:00
let progress_send = progress_sender . clone ( ) ;
2021-01-13 08:06:12 +13:00
let progress_thread_run = progress_thread_run . clone ( ) ;
let atomic_file_counter = atomic_file_counter . clone ( ) ;
2021-01-14 04:03:05 +13:00
let files_to_check = non_cached_files_to_check . len ( ) ;
2021-12-19 11:45:37 +13:00
thread ::spawn ( move | | loop {
2021-01-13 08:06:12 +13:00
progress_send
2021-04-14 18:23:59 +12:00
. unbounded_send ( ProgressData {
2021-01-13 08:06:12 +13:00
current_stage : 1 ,
max_stage : 1 ,
files_checked : atomic_file_counter . load ( Ordering ::Relaxed ) as usize ,
files_to_check ,
} )
. unwrap ( ) ;
if ! progress_thread_run . load ( Ordering ::Relaxed ) {
break ;
}
sleep ( Duration ::from_millis ( LOOP_DURATION as u64 ) ) ;
2021-12-19 11:45:37 +13:00
} )
2021-01-13 08:06:12 +13:00
} else {
2021-12-19 11:45:37 +13:00
thread ::spawn ( | | { } )
} ;
2021-01-13 08:06:12 +13:00
//// PROGRESS THREAD END
2021-01-14 04:03:05 +13:00
let mut vec_file_entry : Vec < FileEntry > = non_cached_files_to_check
2021-12-30 01:43:38 +13:00
. into_par_iter ( )
. map ( | ( _ , mut file_entry ) | {
2021-01-13 08:06:12 +13:00
atomic_file_counter . fetch_add ( 1 , Ordering ::Relaxed ) ;
if stop_receiver . is_some ( ) & & stop_receiver . unwrap ( ) . try_recv ( ) . is_ok ( ) {
2021-01-14 04:03:05 +13:00
check_was_breaked . store ( true , Ordering ::Relaxed ) ;
2021-01-13 08:06:12 +13:00
return None ;
}
2021-01-15 23:04:52 +13:00
match file_entry . type_of_file {
2021-01-14 04:03:05 +13:00
TypeOfFile ::Image = > {
2021-12-30 01:43:38 +13:00
let file_entry_clone = file_entry . clone ( ) ;
let result = panic ::catch_unwind ( | | {
match image ::open ( & file_entry . path ) {
Ok ( _ ) = > Some ( None ) ,
Err ( t ) = > {
let error_string = t . to_string ( ) ;
// This error is a problem with image library, remove check when https://github.com/image-rs/jpeg-decoder/issues/130 will be fixed
if ! error_string . contains ( " spectral selection is not allowed in non-progressive scan " ) {
file_entry . error_string = error_string ;
Some ( Some ( file_entry ) )
} else {
Some ( None )
}
2021-01-14 04:03:05 +13:00
}
2021-12-30 01:43:38 +13:00
}
} ) ;
// If image crashed during opening, we just skip checking its hash and go on
if let Ok ( image_result ) = result {
image_result
} else {
println! ( " Image-rs library crashed when opening \" {:?} \" image, please check if problem happens with latest image-rs version(this can be checked via https://github.com/qarmin/ImageOpening tool) and if it is not reported, please report bug here - https://github.com/image-rs/image/issues " , file_entry_clone . path ) ;
Some ( Some ( file_entry_clone ) )
2021-01-14 04:03:05 +13:00
}
}
2021-03-28 01:14:02 +13:00
TypeOfFile ::ArchiveZip = > match fs ::File ::open ( & file_entry . path ) {
2021-01-15 23:04:52 +13:00
Ok ( file ) = > match zip ::ZipArchive ::new ( file ) {
Ok ( _ ) = > Some ( None ) ,
Err ( e ) = > {
// TODO Maybe filter out unnecessary types of errors
2021-12-30 01:43:38 +13:00
file_entry . error_string = e . to_string ( ) ;
2021-01-15 23:04:52 +13:00
Some ( Some ( file_entry ) )
}
} ,
2021-11-15 03:53:55 +13:00
Err ( _inspected ) = > Some ( None ) , // TODO maybe throw error or something
2021-01-15 23:04:52 +13:00
} ,
2021-02-06 07:07:20 +13:00
#[ cfg(feature = " broken_audio " ) ]
2021-01-15 23:04:52 +13:00
TypeOfFile ::Audio = > match fs ::File ::open ( & file_entry . path ) {
Ok ( file ) = > match rodio ::Decoder ::new ( BufReader ::new ( file ) ) {
Ok ( _ ) = > Some ( None ) ,
Err ( e ) = > {
2021-12-30 01:43:38 +13:00
file_entry . error_string = e . to_string ( ) ;
2021-01-15 23:04:52 +13:00
Some ( Some ( file_entry ) )
}
} ,
2021-11-15 03:53:55 +13:00
Err ( _inspected ) = > Some ( None ) , // TODO maybe throw error or something
2021-01-15 23:04:52 +13:00
} ,
2021-01-14 04:03:05 +13:00
// This means that cache read invalid value because maybe cache comes from different czkawka version
TypeOfFile ::Unknown = > Some ( None ) ,
2021-01-13 08:06:12 +13:00
}
} )
. while_some ( )
. filter ( | file_entry | file_entry . is_some ( ) )
. map ( | file_entry | file_entry . unwrap ( ) )
. collect ::< Vec < FileEntry > > ( ) ;
// End thread which send info to gui
progress_thread_run . store ( false , Ordering ::Relaxed ) ;
progress_thread_handle . join ( ) . unwrap ( ) ;
2021-01-14 04:03:05 +13:00
// Break if stop was clicked
2021-01-13 08:06:12 +13:00
if check_was_breaked . load ( Ordering ::Relaxed ) {
return false ;
}
2021-01-14 04:03:05 +13:00
// Just connect loaded results with already calculated
for ( _name , file_entry ) in records_already_cached {
vec_file_entry . push ( file_entry . clone ( ) ) ;
}
2021-12-22 06:44:20 +13:00
self . broken_files = vec_file_entry
. iter ( )
. filter_map ( | f | if f . error_string . is_empty ( ) { None } else { Some ( f . clone ( ) ) } )
. collect ( ) ;
2021-01-14 04:03:05 +13:00
2021-03-05 00:09:53 +13:00
if self . use_cache {
// Must save all results to file, old loaded from file with all currently counted results
2021-05-09 04:29:52 +12:00
let mut all_results : BTreeMap < String , FileEntry > = self . files_to_check . clone ( ) ;
2021-01-14 04:03:05 +13:00
2021-03-05 00:09:53 +13:00
for file_entry in vec_file_entry {
all_results . insert ( file_entry . path . to_string_lossy ( ) . to_string ( ) , file_entry ) ;
}
for ( _name , file_entry ) in loaded_hash_map {
all_results . insert ( file_entry . path . to_string_lossy ( ) . to_string ( ) , file_entry ) ;
}
save_cache_to_file ( & all_results , & mut self . text_messages ) ;
2021-01-14 04:03:05 +13:00
}
self . information . number_of_broken_files = self . broken_files . len ( ) ;
2021-01-13 08:06:12 +13:00
Common ::print_time ( system_time , SystemTime ::now ( ) , " sort_images - reading data from files in parallel " . to_string ( ) ) ;
2021-01-16 00:41:45 +13:00
// Clean unused data
2021-01-14 04:03:05 +13:00
self . files_to_check = Default ::default ( ) ;
2021-01-13 08:06:12 +13:00
true
}
/// Function to delete files, from filed Vector
fn delete_files ( & mut self ) {
let start_time : SystemTime = SystemTime ::now ( ) ;
match self . delete_method {
DeleteMethod ::Delete = > {
2021-01-14 04:03:05 +13:00
for file_entry in self . broken_files . iter ( ) {
2021-01-13 08:06:12 +13:00
if fs ::remove_file ( & file_entry . path ) . is_err ( ) {
self . text_messages . warnings . push ( file_entry . path . display ( ) . to_string ( ) ) ;
}
}
}
DeleteMethod ::None = > {
//Just do nothing
}
}
Common ::print_time ( start_time , SystemTime ::now ( ) , " delete_files " . to_string ( ) ) ;
}
}
2021-11-28 08:57:10 +13:00
2021-01-13 08:06:12 +13:00
impl Default for BrokenFiles {
fn default ( ) -> Self {
Self ::new ( )
}
}
impl DebugPrint for BrokenFiles {
#[ allow(dead_code) ]
#[ allow(unreachable_code) ]
/// Debugging printing - only available on debug build
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! ( " Excluded items - {:?} " , self . excluded_items . items ) ;
println! ( " Included directories - {:?} " , self . directories . included_directories ) ;
println! ( " Excluded directories - {:?} " , self . directories . excluded_directories ) ;
2021-11-23 23:10:24 +13:00
println! ( " Recursive search - {} " , self . recursive_search ) ;
2021-01-13 08:06:12 +13:00
println! ( " Delete Method - {:?} " , self . delete_method ) ;
println! ( " ----------------------------------------- " ) ;
}
}
2021-11-28 08:57:10 +13:00
2021-01-13 08:06:12 +13:00
impl SaveResults for BrokenFiles {
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 ( ) ,
} ;
let file_handler = match File ::create ( & file_name ) {
Ok ( t ) = > t ,
2021-11-15 03:53:55 +13:00
Err ( e ) = > {
self . text_messages . errors . push ( format! ( " Failed to create file {} , reason {} " , file_name , e ) ) ;
2021-01-13 08:06:12 +13:00
return false ;
}
} ;
let mut writer = BufWriter ::new ( file_handler ) ;
2021-11-15 03:53:55 +13:00
if let Err ( e ) = writeln! (
2021-01-13 08:06:12 +13:00
writer ,
" Results of searching {:?} with excluded directories {:?} and excluded items {:?} " ,
self . directories . included_directories , self . directories . excluded_directories , self . excluded_items . items
2021-11-15 03:53:55 +13:00
) {
self . text_messages . errors . push ( format! ( " Failed to save results to file {} , reason {} " , file_name , e ) ) ;
2021-01-13 08:06:12 +13:00
return false ;
}
if ! self . broken_files . is_empty ( ) {
writeln! ( writer , " Found {} broken files. " , self . information . number_of_broken_files ) . unwrap ( ) ;
for file_entry in self . broken_files . iter ( ) {
writeln! ( writer , " {} - {} " , file_entry . path . display ( ) , file_entry . error_string ) . unwrap ( ) ;
}
} else {
write! ( writer , " Not found any broken files. " ) . unwrap ( ) ;
}
Common ::print_time ( start_time , SystemTime ::now ( ) , " save_results_to_file " . to_string ( ) ) ;
true
}
}
2021-11-28 08:57:10 +13:00
2021-01-13 08:06:12 +13:00
impl PrintResults for BrokenFiles {
/// Print information's about duplicated entries
/// Only needed for CLI
fn print_results ( & self ) {
let start_time : SystemTime = SystemTime ::now ( ) ;
println! ( " Found {} broken files. \n " , self . information . number_of_broken_files ) ;
for file_entry in self . broken_files . iter ( ) {
println! ( " {} - {} " , file_entry . path . display ( ) , file_entry . error_string ) ;
}
Common ::print_time ( start_time , SystemTime ::now ( ) , " print_entries " . to_string ( ) ) ;
}
}
2021-01-14 04:03:05 +13:00
2021-05-09 04:29:52 +12:00
fn save_cache_to_file ( hashmap_file_entry : & BTreeMap < String , FileEntry > , text_messages : & mut Messages ) {
2021-01-14 04:03:05 +13:00
if let Some ( proj_dirs ) = ProjectDirs ::from ( " pl " , " Qarmin " , " Czkawka " ) {
// Lin: /home/username/.cache/czkawka
// Win: C:\Users\Username\AppData\Local\Qarmin\Czkawka\cache
// Mac: /Users/Username/Library/Caches/pl.Qarmin.Czkawka
let cache_dir = PathBuf ::from ( proj_dirs . cache_dir ( ) ) ;
if cache_dir . exists ( ) {
if ! cache_dir . is_dir ( ) {
text_messages . messages . push ( format! ( " Config dir {} is a file! " , cache_dir . display ( ) ) ) ;
return ;
}
2021-11-15 03:53:55 +13:00
} else if let Err ( e ) = fs ::create_dir_all ( & cache_dir ) {
text_messages . messages . push ( format! ( " Cannot create config dir {} , reason {} " , cache_dir . display ( ) , e ) ) ;
2021-01-14 04:03:05 +13:00
return ;
}
let cache_file = cache_dir . join ( CACHE_FILE_NAME ) ;
let file_handler = match OpenOptions ::new ( ) . truncate ( true ) . write ( true ) . create ( true ) . open ( & cache_file ) {
Ok ( t ) = > t ,
2021-11-15 03:53:55 +13:00
Err ( e ) = > {
2021-12-22 06:44:20 +13:00
text_messages
. messages
. push ( format! ( " Cannot create or open cache file {} , reason {} " , cache_file . display ( ) , e ) ) ;
2021-01-14 04:03:05 +13:00
return ;
}
} ;
let mut writer = BufWriter ::new ( file_handler ) ;
for file_entry in hashmap_file_entry . values ( ) {
// Only save to cache files which have more than 1KB
if file_entry . size > 1024 {
2021-12-22 06:44:20 +13:00
let string : String = format! (
" {}//{}//{}//{} " ,
file_entry . path . display ( ) ,
file_entry . size ,
file_entry . modified_date ,
file_entry . error_string
) ;
2021-01-14 04:03:05 +13:00
2021-11-15 03:53:55 +13:00
if let Err ( e ) = writeln! ( writer , " {} " , string ) {
2021-12-22 06:44:20 +13:00
text_messages
. messages
. push ( format! ( " Failed to save some data to cache file {} , reason {} " , cache_file . display ( ) , e ) ) ;
2021-01-14 04:03:05 +13:00
return ;
} ;
}
}
}
}
2021-05-09 04:29:52 +12:00
fn load_cache_from_file ( text_messages : & mut Messages ) -> Option < BTreeMap < String , FileEntry > > {
2021-01-14 04:03:05 +13:00
if let Some ( proj_dirs ) = ProjectDirs ::from ( " pl " , " Qarmin " , " Czkawka " ) {
let cache_dir = PathBuf ::from ( proj_dirs . cache_dir ( ) ) ;
let cache_file = cache_dir . join ( CACHE_FILE_NAME ) ;
2021-11-15 03:53:55 +13:00
// TODO add before checking if cache exists(if not just return) but if exists then enable error
2021-01-14 04:03:05 +13:00
let file_handler = match OpenOptions ::new ( ) . read ( true ) . open ( & cache_file ) {
Ok ( t ) = > t ,
2021-11-15 03:53:55 +13:00
Err ( _inspected ) = > {
2021-01-14 04:03:05 +13:00
// text_messages.messages.push(format!("Cannot find or open cache file {}", cache_file.display())); // This shouldn't be write to output
return None ;
}
} ;
let reader = BufReader ::new ( file_handler ) ;
2021-05-09 04:29:52 +12:00
let mut hashmap_loaded_entries : BTreeMap < String , FileEntry > = Default ::default ( ) ;
2021-01-14 04:03:05 +13:00
// Read the file line by line using the lines() iterator from std::io::BufRead.
for ( index , line ) in reader . lines ( ) . enumerate ( ) {
let line = match line {
Ok ( t ) = > t ,
2021-11-15 03:53:55 +13:00
Err ( e ) = > {
2021-12-22 06:44:20 +13:00
text_messages
. warnings
. push ( format! ( " Failed to load line number {} from cache file {} , reason {} " , index + 1 , cache_file . display ( ) , e ) ) ;
2021-01-14 04:03:05 +13:00
return None ;
}
} ;
let uuu = line . split ( " // " ) . collect ::< Vec < & str > > ( ) ;
if uuu . len ( ) ! = 4 {
2021-12-22 06:44:20 +13:00
text_messages
. warnings
. push ( format! ( " Found invalid data in line {} - ( {} ) in cache file {} " , index + 1 , line , cache_file . display ( ) ) ) ;
2021-01-14 04:03:05 +13:00
continue ;
}
// Don't load cache data if destination file not exists
if Path ::new ( uuu [ 0 ] ) . exists ( ) {
hashmap_loaded_entries . insert (
uuu [ 0 ] . to_string ( ) ,
FileEntry {
path : PathBuf ::from ( uuu [ 0 ] ) ,
size : match uuu [ 1 ] . parse ::< u64 > ( ) {
Ok ( t ) = > t ,
2021-11-15 03:53:55 +13:00
Err ( e ) = > {
2021-12-22 06:44:20 +13:00
text_messages . warnings . push ( format! (
" Found invalid size value in line {} - ({}) in cache file {}, reason {} " ,
index + 1 ,
line ,
cache_file . display ( ) ,
e
) ) ;
2021-01-14 04:03:05 +13:00
continue ;
}
} ,
modified_date : match uuu [ 2 ] . parse ::< u64 > ( ) {
Ok ( t ) = > t ,
2021-11-15 03:53:55 +13:00
Err ( e ) = > {
2021-12-22 06:44:20 +13:00
text_messages . warnings . push ( format! (
" Found invalid modified date value in line {} - ({}) in cache file {}, reason {} " ,
index + 1 ,
line ,
cache_file . display ( ) ,
e
) ) ;
2021-01-14 04:03:05 +13:00
continue ;
}
} ,
type_of_file : check_extension_avaibility ( & uuu [ 0 ] . to_lowercase ( ) ) ,
error_string : uuu [ 3 ] . to_string ( ) ,
} ,
) ;
}
}
return Some ( hashmap_loaded_entries ) ;
}
text_messages . messages . push ( " Cannot find or open system config dir to save cache file " . to_string ( ) ) ;
None
}
fn check_extension_avaibility ( file_name_lowercase : & str ) -> TypeOfFile {
2022-01-01 10:34:24 +13:00
if IMAGE_RS_BROKEN_FILES_EXTENSIONS . iter ( ) . any ( | e | file_name_lowercase . ends_with ( e ) ) {
2021-01-14 04:03:05 +13:00
TypeOfFile ::Image
2022-01-01 10:34:24 +13:00
} else if ZIP_FILES_EXTENSIONS . iter ( ) . any ( | e | file_name_lowercase . ends_with ( e ) ) {
2021-03-28 01:14:02 +13:00
TypeOfFile ::ArchiveZip
2022-01-01 10:34:24 +13:00
} else if AUDIO_FILES_EXTENSIONS . iter ( ) . any ( | e | file_name_lowercase . ends_with ( e ) ) {
2021-02-06 07:07:20 +13:00
#[ cfg(feature = " broken_audio " ) ]
{
TypeOfFile ::Audio
}
#[ cfg(not(feature = " broken_audio " )) ]
{
TypeOfFile ::Unknown
}
2021-01-14 04:03:05 +13:00
} else {
TypeOfFile ::Unknown
}
}