1
0
Fork 0
mirror of synced 2024-06-01 18:19:46 +12:00

Fix excluded directories

This commit is contained in:
darksv 2020-10-14 17:01:29 +02:00
parent 6b6673d313
commit ebef8c2888
8 changed files with 55 additions and 86 deletions

View file

@ -144,15 +144,10 @@ impl BigFile {
}
let next_folder = current_folder.join(entry_data.file_name());
if self.directories.excluded_directories.contains(&next_folder) {
if self.directories.is_excluded(&next_folder) || self.excluded_items.is_excluded(&next_folder) {
continue 'dir;
}
for expression in &self.excluded_items.items {
if Common::regex_check(expression, &next_folder) {
continue 'dir;
}
}
folders_to_check.push(next_folder);
} else if metadata.is_file() {
// Extracting file extension
@ -169,15 +164,9 @@ impl BigFile {
}
// Checking expressions
let mut current_file_name = current_folder.join(entry_data.file_name());
for expression in &self.excluded_items.items {
if Common::regex_check(expression, &current_file_name) {
continue 'dir;
}
}
if cfg!(target_family = "windows") {
current_file_name = Common::prettier_windows_path(&current_file_name);
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

View file

@ -107,7 +107,7 @@ impl Common {
true
}
pub fn prettier_windows_path(path_to_change: impl AsRef<Path>) -> PathBuf {
pub fn normalize_windows_path(path_to_change: impl AsRef<Path>) -> PathBuf {
let path = path_to_change.as_ref();
match path.to_str() {
Some(path) if path.is_char_boundary(1) => {
@ -117,7 +117,7 @@ impl Common {
new_path.push(replaced[..1].to_ascii_uppercase());
new_path.push(replaced[1..].to_ascii_lowercase());
} else {
new_path.push(replaced);
new_path.push(replaced.to_ascii_lowercase());
}
PathBuf::from(new_path)
}
@ -153,8 +153,8 @@ mod test {
}
#[test]
fn test_windows_path() {
assert_eq!(PathBuf::from("C:/path.txt"), Common::prettier_windows_path("c:/PATH.tXt"));
assert_eq!(PathBuf::from("H:/reka/weza/roman.txt"), Common::prettier_windows_path("h:/RekA/Weza\\roMan.Txt"));
assert_eq!(PathBuf::from("T:/a"), Common::prettier_windows_path("T:\\A"));
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"));
}
}

View file

@ -106,8 +106,8 @@ impl Directories {
let mut optimized_excluded: Vec<PathBuf> = Vec::new();
if cfg!(target_family = "windows") {
self.included_directories = self.included_directories.iter().map(Common::prettier_windows_path).collect();
self.excluded_directories = self.excluded_directories.iter().map(Common::prettier_windows_path).collect();
self.included_directories = self.included_directories.iter().map(Common::normalize_windows_path).collect();
self.excluded_directories = self.excluded_directories.iter().map(Common::normalize_windows_path).collect();
}
// Remove duplicated entries like: "/", "/"
@ -224,4 +224,13 @@ impl Directories {
Common::print_time(start_time, SystemTime::now(), "optimize_directories".to_string());
true
}
/// Checks whether a specified directory is excluded from searching
pub fn is_excluded(&self, path: impl AsRef<Path>) -> bool {
let path = path.as_ref();
#[cfg(target_family = "windows")]
let path = Common::normalize_windows_path(path);
// We're assuming that `excluded_directories` are already normalized
self.excluded_directories.iter().any(|p| p.as_path() == path)
}
}

View file

@ -1,5 +1,6 @@
use crate::common::Common;
use crate::common_messages::Messages;
use std::path::Path;
use std::time::SystemTime;
#[derive(Default)]
@ -45,4 +46,17 @@ impl ExcludedItems {
self.items = checked_expressions;
Common::print_time(start_time, SystemTime::now(), "set_excluded_items".to_string());
}
/// Checks whether a specified path is excluded from searching
pub fn is_excluded(&self, path: impl AsRef<Path>) -> bool {
#[cfg(target_family = "windows")]
let path = Common::normalize_windows_path(path);
for expression in &self.items {
if Common::regex_check(expression, &path) {
return true;
}
}
false
}
}

View file

@ -226,16 +226,14 @@ impl DuplicateFinder {
}
let next_folder = current_folder.join(entry_data.file_name());
for ed in &self.directories.excluded_directories {
if next_folder == *ed {
continue 'dir;
}
if self.directories.is_excluded(&next_folder) {
continue 'dir;
}
for expression in &self.excluded_items.items {
if Common::regex_check(expression, &next_folder) {
continue 'dir;
}
if self.excluded_items.is_excluded(&next_folder) {
continue 'dir;
}
folders_to_check.push(next_folder);
} else if metadata.is_file() {
// let mut have_valid_extension: bool;
@ -256,17 +254,9 @@ impl DuplicateFinder {
}
// Checking files
if metadata.len() >= self.minimal_file_size {
let mut current_file_name = current_folder.join(entry_data.file_name());
// Checking expressions
for expression in &self.excluded_items.items {
if Common::regex_check(expression, &current_file_name) {
continue 'dir;
}
}
if cfg!(target_family = "windows") {
current_file_name = Common::prettier_windows_path(&current_file_name);
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

View file

@ -169,17 +169,10 @@ impl EmptyFiles {
}
let next_folder = current_folder.join(entry_data.file_name());
if self.directories.is_excluded(&next_folder) || self.excluded_items.is_excluded(&next_folder) {
continue 'dir;
}
for ed in &self.directories.excluded_directories {
if next_folder == *ed {
continue 'dir;
}
}
for expression in &self.excluded_items.items {
if Common::regex_check(expression, &next_folder) {
continue 'dir;
}
}
folders_to_check.push(next_folder);
} else if metadata.is_file() {
let file_name_lowercase: String = match entry_data.file_name().into_string() {
@ -199,17 +192,9 @@ impl EmptyFiles {
}
// Checking files
if metadata.len() == 0 {
let mut current_file_name = current_folder.join(entry_data.file_name());
// Checking expressions
for expression in &self.excluded_items.items {
if Common::regex_check(expression, &current_file_name) {
continue 'dir;
}
}
if cfg!(target_family = "windows") {
current_file_name = Common::prettier_windows_path(&current_file_name);
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

View file

@ -206,10 +206,6 @@ impl EmptyFolder {
#[allow(unused_mut)] // Used is later by Windows build
for (mut name, folder_entry) in folders_checked {
if folder_entry.is_empty != FolderEmptiness::No {
if cfg!(target_family = "windows") {
name = Common::prettier_windows_path(&name);
}
self.empty_folder_list.insert(name, folder_entry);
}
}

View file

@ -125,11 +125,8 @@ impl Temporary {
if rx.is_some() && rx.unwrap().try_recv().is_ok() {
return false;
}
let mut current_folder = folders_to_check.pop().unwrap();
if cfg!(target_family = "windows") {
current_folder = Common::prettier_windows_path(&current_folder);
}
let current_folder = folders_to_check.pop().unwrap();
// Read current dir, if permission are denied just go to next
let read_dir = match fs::read_dir(&current_folder) {
Ok(t) => t,
@ -163,14 +160,10 @@ impl Temporary {
}
let next_folder = current_folder.join(entry_data.file_name());
if self.directories.excluded_directories.contains(&next_folder) {
if self.directories.is_excluded(&next_folder) || self.excluded_items.is_excluded(&next_folder) {
continue 'dir;
}
for expression in &self.excluded_items.items {
if Common::regex_check(expression, &next_folder) {
continue 'dir;
}
}
folders_to_check.push(next_folder);
} else if metadata.is_file() {
let file_name_lowercase: String = match entry_data.file_name().into_string() {
@ -187,16 +180,9 @@ impl Temporary {
continue 'dir;
}
// Checking files
let mut current_file_name = current_folder.join(entry_data.file_name());
if cfg!(target_family = "windows") {
current_file_name = Common::prettier_windows_path(&current_file_name);
}
// Checking expressions
for expression in &self.excluded_items.items {
if Common::regex_check(expression, &current_file_name) {
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