@ -29,7 +29,7 @@ use symphonia::core::conv::IntoSample;
// use libheif_rs::LibHeif;
use crate ::common_dir_traversal ::{ CheckingMethod , ProgressData , ToolType } ;
use crate ::common_directory ::Directories ;
use crate ::common_items ::ExcludedItems ;
use crate ::common_items ::{ ExcludedItems , SingleExcludedItem } ;
use crate ::common_messages ::Messages ;
use crate ::common_tool ::DeleteMethod ;
use crate ::common_traits ::ResultEntry ;
@ -150,8 +150,6 @@ pub const VIDEO_FILES_EXTENSIONS: &[&str] = &[
pub const LOOP_DURATION : u32 = 20 ; //ms
pub const SEND_PROGRESS_DATA_TIME_BETWEEN : u32 = 200 ; //ms
pub struct Common ( ) ;
pub fn remove_folder_if_contains_only_empty_folders ( path : impl AsRef < Path > ) -> bool {
let path = path . as_ref ( ) ;
if ! path . is_dir ( ) {
@ -333,81 +331,70 @@ pub fn create_crash_message(library_name: &str, file_path: &str, home_library_ur
format! ( "{library_name} library crashed when opening \"{file_path}\", please check if this is fixed with the latest version of {library_name} (e.g. with https://github.com/qarmin/crates_tester) and if it is not fixed, please report bug here - {home_library_url}" )
}
impl Common {
pub fn regex_check ( expression : & str , directory : impl AsRef < Path > ) -> bool {
if expression = = "*" {
return true ;
}
let temp_splits : Vec < & str > = expression . split ( '*' ) . collect ( ) ;
let mut splits : Vec < & str > = Vec ::new ( ) ;
for i in temp_splits {
if ! i . is_empty ( ) {
splits . push ( i ) ;
}
}
if splits . is_empty ( ) {
return false ;
}
// Get rid of non unicode characters
let directory = directory . as_ref ( ) . to_string_lossy ( ) ;
pub fn regex_check ( expression_item : & SingleExcludedItem , directory : impl AsRef < Path > ) -> bool {
if expression_item . expression = = "*" {
return true ;
}
// Early checking if directory contains all parts needed by expression
for split in & splits {
if ! directory . contains ( split ) {
return false ;
}
}
if expression_item . expression_splits . is_empty ( ) {
return false ;
}
let mut position_of_splits : Vec < usize > = Vec ::new ( ) ;
// Get rid of non unicode characters
let directory_name = directory . as_ref ( ) . to_string_lossy ( ) ;
// `git*` shouldn't be true for `/gitsfafasfs`
if ! expression . starts_with ( '*' ) & & directory . find ( splits [ 0 ] ) . unwrap ( ) > 0 {
return false ;
}
// `*home` shouldn't be true for `/homeowner`
if ! expression . ends_with ( '*' ) & & ! directory . ends_with ( splits . last ( ) . unwrap ( ) ) {
// Early checking if directory contains all parts needed by expression
for split in & expression_item . unique_extensions_splits {
if ! directory_name . contains ( split ) {
return false ;
}
}
// At the end we check if parts between * are correctly positioned
position_of_splits . push ( directory . find ( splits [ 0 ] ) . unwrap ( ) ) ;
let mut current_index : usize ;
let mut found_index : usize ;
for i in splits [ 1 .. ] . iter ( ) . enumerate ( ) {
current_index = * position_of_splits . get ( i . 0 ) . unwrap ( ) + i . 1. len ( ) ;
found_index = match directory [ current_index .. ] . find ( i . 1 ) {
Some ( t ) = > t ,
None = > return false ,
} ;
position_of_splits . push ( found_index + current_index ) ;
}
true
// `git*` shouldn't be true for `/gitsfafasfs`
if ! expression_item . expression . starts_with ( '*' ) & & directory_name . find ( & expression_item . expression_splits [ 0 ] ) . unwrap ( ) > 0 {
return false ;
}
// `*home` shouldn't be true for `/homeowner`
if ! expression_item . expression . ends_with ( '*' ) & & ! directory_name . ends_with ( expression_item . expression_splits . last ( ) . unwrap ( ) ) {
return false ;
}
// At the end we check if parts between * are correctly positioned
let mut last_split_point = directory_name . find ( & expression_item . expression_splits [ 0 ] ) . unwrap ( ) ;
let mut current_index : usize = 0 ;
let mut found_index : usize ;
for spl in & expression_item . expression_splits [ 1 .. ] {
found_index = match directory_name [ current_index .. ] . find ( spl ) {
Some ( t ) = > t ,
None = > return false ,
} ;
current_index = last_split_point + spl . len ( ) ;
last_split_point = found_index + current_index ;
}
true
}
pub fn normalize_windows_path ( path_to_change : impl AsRef < Path > ) -> PathBuf {
let path = path_to_change . as_ref ( ) ;
pub fn normalize_windows_path ( path_to_change : impl AsRef < Path > ) -> PathBuf {
let path = path_to_change . as_ref ( ) ;
// Don't do anything, because network path may be case intensive
if path . to_string_lossy ( ) . starts_with ( '\\' ) {
return path . to_path_buf ( ) ;
}
// Don't do anything, because network path may be case intensive
if path . to_string_lossy ( ) . starts_with ( '\\' ) {
return path . to_path_buf ( ) ;
}
match path . to_str ( ) {
Some ( path ) if path . is_char_boundary ( 1 ) = > {
let replaced = path . replace ( '/' , "\\" ) ;
let mut new_path = OsString ::new ( ) ;
if replaced [ 1 .. ] . starts_with ( ':' ) {
new_path . push ( replaced [ .. 1 ] . to_ascii_uppercase ( ) ) ;
new_path . push ( replaced [ 1 .. ] . to_ascii_lowercase ( ) ) ;
} else {
new_path . push ( replaced . to_ascii_lowercase ( ) ) ;
}
PathBuf ::from ( new_path )
match path . to_str ( ) {
Some ( path ) if path . is_char_boundary ( 1 ) = > {
let replaced = path . replace ( '/' , "\\" ) ;
let mut new_path = OsString ::new ( ) ;
if replaced [ 1 .. ] . starts_with ( ':' ) {
new_path . push ( replaced [ .. 1 ] . to_ascii_uppercase ( ) ) ;
new_path . push ( replaced [ 1 .. ] . to_ascii_lowercase ( ) ) ;
} else {
new_path . push ( replaced . to_ascii_lowercase ( ) ) ;
}
_ = > path . to_path_buf ( ) ,
PathBuf ::from ( new_path )
}
_ = > path . to_path_buf ( ) ,
}
}
@ -624,7 +611,8 @@ mod test {
use std ::path ::{ Path , PathBuf } ;
use tempfile ::tempdir ;
use crate ::common ::{ remove_folder_if_contains_only_empty_folders , Common } ;
use crate ::common ::{ normalize_windows_path , regex_check , remove_folder_if_contains_only_empty_folders } ;
use crate ::common_items ::{ new_excluded_item , ExcludedItems } ;
#[ test ]
fn test_remove_folder_if_contains_only_empty_folders ( ) {
@ -652,39 +640,42 @@ mod test {
#[ test ]
fn test_regex ( ) {
assert! ( Common ::regex_check ( "*home*" , "/home/rafal" ) ) ;
assert! ( Common ::regex_check ( "*home" , "/home" ) ) ;
assert! ( Common ::regex_check ( "*home/" , "/home/" ) ) ;
assert! ( Common ::regex_check ( "*home/*" , "/home/" ) ) ;
assert! ( Common ::regex_check ( "*.git*" , "/home/.git" ) ) ;
assert! ( Common ::regex_check ( "*/home/rafal*rafal*rafal*rafal*" , "/home/rafal/rafalrafalrafal" ) ) ;
assert! ( Common ::regex_check ( "AAA" , "AAA" ) ) ;
assert! ( Common ::regex_check ( "AAA*" , "AAABDGG/QQPW*" ) ) ;
assert! ( ! Common ::regex_check ( "*home" , "/home/" ) ) ;
assert! ( ! Common ::regex_check ( "*home" , "/homefasfasfasfasf/" ) ) ;
assert! ( ! Common ::regex_check ( "*home" , "/homefasfasfasfasf" ) ) ;
assert! ( ! Common ::regex_check ( "rafal*afal*fal" , "rafal" ) ) ;
assert! ( ! Common ::regex_check ( "rafal*a" , "rafal" ) ) ;
assert! ( ! Common ::regex_check ( "AAAAAAAA****" , "/AAAAAAAAAAAAAAAAA" ) ) ;
assert! ( ! Common ::regex_check ( "*.git/*" , "/home/.git" ) ) ;
assert! ( ! Common ::regex_check ( "*home/*koc" , "/koc/home/" ) ) ;
assert! ( ! Common ::regex_check ( "*home/" , "/home" ) ) ;
assert! ( ! Common ::regex_check ( "*TTT" , "/GGG" ) ) ;
#[ cfg(target_family = " windows " ) ]
{
assert! ( Common ::regex_check ( "*\\home" , "C:\\home" ) ) ;
assert! ( Common ::regex_check ( "*/home" , "C:\\home" ) ) ;
assert! ( regex_check ( & new_excluded_item ( "*home*" ) , "/home/rafal" ) ) ;
assert! ( regex_check ( & new_excluded_item ( "*home" ) , "/home" ) ) ;
assert! ( regex_check ( & new_excluded_item ( "*home/" ) , "/home/" ) ) ;
assert! ( regex_check ( & new_excluded_item ( "*home/*" ) , "/home/" ) ) ;
assert! ( regex_check ( & new_excluded_item ( "*.git*" ) , "/home/.git" ) ) ;
assert! ( regex_check ( & new_excluded_item ( "*/home/rafal*rafal*rafal*rafal*" ) , "/home/rafal/rafalrafalrafal" ) ) ;
assert! ( regex_check ( & new_excluded_item ( "AAA" ) , "AAA" ) ) ;
assert! ( regex_check ( & new_excluded_item ( "AAA*" ) , "AAABDGG/QQPW*" ) ) ;
assert! ( ! regex_check ( & new_excluded_item ( "*home" ) , "/home/" ) ) ;
assert! ( ! regex_check ( & new_excluded_item ( "*home" ) , "/homefasfasfasfasf/" ) ) ;
assert! ( ! regex_check ( & new_excluded_item ( "*home" ) , "/homefasfasfasfasf" ) ) ;
assert! ( ! regex_check ( & new_excluded_item ( "rafal*afal*fal" ) , "rafal" ) ) ;
assert! ( ! regex_check ( & new_excluded_item ( "rafal*a" ) , "rafal" ) ) ;
assert! ( ! regex_check ( & new_excluded_item ( "AAAAAAAA****" ) , "/AAAAAAAAAAAAAAAAA" ) ) ;
assert! ( ! regex_check ( & new_excluded_item ( "*.git/*" ) , "/home/.git" ) ) ;
assert! ( ! regex_check ( & new_excluded_item ( "*home/*koc" ) , "/koc/home/" ) ) ;
assert! ( ! regex_check ( & new_excluded_item ( "*home/" ) , "/home" ) ) ;
assert! ( ! regex_check ( & new_excluded_item ( "*TTT" ) , "/GGG" ) ) ;
assert! ( regex_check (
& new_excluded_item ( "*/home/*/.local/share/containers" ) ,
"/var/home/roman/.local/share/containers"
) ) ;
if cfg! ( target_family = "windows" ) {
assert! ( regex_check ( & new_excluded_item ( "*\\home" ) , "C:\\home" ) ) ;
assert! ( regex_check ( & new_excluded_item ( "*/home" ) , "C:\\home" ) ) ;
}
}
#[ test ]
fn test_windows_path ( ) {
assert_eq! ( PathBuf ::from ( "C:\\path.txt" ) , Common:: normalize_windows_path( "c:/PATH.tXt" ) ) ;
assert_eq! ( PathBuf ::from ( "H:\\reka\\weza\\roman.txt" ) , Common:: normalize_windows_path( "h:/RekA/Weza\\roMan.Txt" ) ) ;
assert_eq! ( PathBuf ::from ( "T:\\a" ) , Common:: normalize_windows_path( "T:\\A" ) ) ;
assert_eq! ( PathBuf ::from ( "\\\\aBBa" ) , Common:: normalize_windows_path( "\\\\aBBa" ) ) ;
assert_eq! ( PathBuf ::from ( "a" ) , Common:: normalize_windows_path( "a" ) ) ;
assert_eq! ( PathBuf ::from ( "" ) , Common:: normalize_windows_path( "" ) ) ;
assert_eq! ( PathBuf ::from ( "C:\\path.txt" ) , normalize_windows_path( "c:/PATH.tXt" ) ) ;
assert_eq! ( PathBuf ::from ( "H:\\reka\\weza\\roman.txt" ) , normalize_windows_path( "h:/RekA/Weza\\roMan.Txt" ) ) ;
assert_eq! ( PathBuf ::from ( "T:\\a" ) , normalize_windows_path( "T:\\A" ) ) ;
assert_eq! ( PathBuf ::from ( "\\\\aBBa" ) , normalize_windows_path( "\\\\aBBa" ) ) ;
assert_eq! ( PathBuf ::from ( "a" ) , normalize_windows_path( "a" ) ) ;
assert_eq! ( PathBuf ::from ( "" ) , normalize_windows_path( "" ) ) ;
}
}