Skip to content

Commit 6d7bb78

Browse files
committed
Up
1 parent a35611a commit 6d7bb78

4 files changed

Lines changed: 41 additions & 12 deletions

File tree

czkawka_core/src/common/dir_traversal.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ where
251251
&mut file_results,
252252
&extensions,
253253
&excluded_items,
254+
&directories,
254255
minimal_file_size,
255256
maximal_file_size,
256257
);
@@ -389,6 +390,10 @@ fn process_file_in_file_mode(
389390
return;
390391
}
391392

393+
if directories.is_excluded_file(&current_file_name) {
394+
return;
395+
}
396+
392397
#[cfg(target_family = "unix")]
393398
if directories.exclude_other_filesystems() {
394399
match directories.is_on_other_filesystems(&current_file_name) {
@@ -425,6 +430,7 @@ fn process_file_in_file_mode_path_check(
425430
fe_result: &mut Vec<FileEntry>,
426431
extensions: &Extensions,
427432
excluded_items: &ExcludedItems,
433+
directories: &Directories,
428434
minimal_file_size: u64,
429435
maximal_file_size: u64,
430436
) {
@@ -435,6 +441,13 @@ fn process_file_in_file_mode_path_check(
435441
return;
436442
}
437443

444+
if directories.is_excluded_file(path) {
445+
return;
446+
}
447+
if directories.is_excluded_item_in_dir(path) {
448+
return;
449+
}
450+
438451
if excluded_items.is_excluded(path) {
439452
return;
440453
}
@@ -464,7 +477,7 @@ fn process_dir_in_file_symlink_mode(
464477
}
465478

466479
let dir_path = entry_data.path();
467-
if directories.is_excluded(&dir_path) {
480+
if directories.is_excluded_dir(&dir_path) {
468481
return;
469482
}
470483

@@ -752,7 +765,7 @@ mod tests {
752765
Ok(())
753766
}
754767

755-
fn create_temp_structure(dir: &TempDir) -> io::Result<(PathBuf, PathBuf)> {
768+
fn create_temp_structure(dir: &TempDir) -> io::Result<(PathBuf, PathBuf, PathBuf)> {
756769
let global = dir.path().join("global.txt");
757770
let other_dir = dir.path().join("other");
758771
fs::create_dir_all(&other_dir)?;
@@ -766,13 +779,13 @@ mod tests {
766779
f2.write_all(b"other")?;
767780
f2.set_modified(*NOW)?;
768781

769-
Ok((global, other))
782+
Ok((global, other, other_dir))
770783
}
771784

772785
#[test]
773786
fn test_traversal_with_and_without_excluded_dir() -> io::Result<()> {
774787
let dir = tempfile::Builder::new().tempdir()?;
775-
let (global, other) = create_temp_structure(&dir)?;
788+
let (global, other, other_dir) = create_temp_structure(&dir)?;
776789
let secs = NOW.duration_since(SystemTime::UNIX_EPOCH).expect("Cannot fail calculating duration since epoch").as_secs();
777790

778791
let mut common_data = CommonToolData::new(ToolType::SimilarImages);
@@ -797,7 +810,7 @@ mod tests {
797810

798811
let mut common_data2 = CommonToolData::new(ToolType::SimilarImages);
799812
common_data2.directories.set_included_paths([dir.path().to_owned()].to_vec());
800-
common_data2.directories.set_excluded_paths([other.clone()].to_vec());
813+
common_data2.directories.set_excluded_paths([other_dir.clone()].to_vec());
801814
common_data2.set_minimal_file_size(0);
802815

803816
match DirTraversalBuilder::new()
@@ -817,7 +830,7 @@ mod tests {
817830

818831
let mut common_data2 = CommonToolData::new(ToolType::SimilarImages);
819832
common_data2.directories.set_included_paths([dir.path().to_owned()].to_vec());
820-
common_data2.directories.set_excluded_paths([other.clone().join("other.txt")].to_vec());
833+
common_data2.directories.set_excluded_paths([other.clone()].to_vec());
821834
common_data2.set_minimal_file_size(0);
822835

823836
match DirTraversalBuilder::new()

czkawka_core/src/common/directories.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ use crate::helpers::messages::Messages;
99

1010
#[derive(Debug, Clone, Default)]
1111
pub struct Directories {
12-
pub(crate) excluded_directories: Vec<PathBuf>,
1312
pub(crate) included_directories: Vec<PathBuf>,
13+
pub(crate) excluded_directories: Vec<PathBuf>,
1414
pub(crate) reference_directories: Vec<PathBuf>,
15-
pub(crate) excluded_files: Vec<PathBuf>,
1615
pub(crate) included_files: Vec<PathBuf>,
16+
pub(crate) excluded_files: Vec<PathBuf>,
1717
pub(crate) reference_files: Vec<PathBuf>,
1818

19-
pub(crate) original_excluded_paths: Vec<PathBuf>,
2019
pub(crate) original_included_paths: Vec<PathBuf>,
20+
pub(crate) original_excluded_paths: Vec<PathBuf>,
2121
pub(crate) original_reference_paths: Vec<PathBuf>,
2222

2323
pub(crate) exclude_other_filesystems: Option<bool>,
@@ -259,13 +259,29 @@ impl Directories {
259259
self.reference_directories.iter().any(|e| path.starts_with(e)) || self.reference_files.iter().any(|e| e.as_path() == path)
260260
}
261261

262-
pub(crate) fn is_excluded(&self, path: &Path) -> bool {
262+
pub(crate) fn is_excluded_dir(&self, path: &Path) -> bool {
263263
#[cfg(target_family = "windows")]
264264
let path = normalize_windows_path(path);
265265
// We're assuming that `excluded_directories` are already normalized
266266
self.excluded_directories.iter().any(|p| p.as_path() == path)
267267
}
268268

269+
pub(crate) fn is_excluded_file(&self, path: &Path) -> bool {
270+
#[cfg(target_family = "windows")]
271+
let path = normalize_windows_path(path);
272+
// We're assuming that `excluded_files` are already normalized
273+
self.excluded_files.iter().any(|p| p.as_path() == path)
274+
}
275+
276+
// Usually it is not required, because if main directory is excluded, then we don't run check on
277+
// every single children, different situation is with excluded single file
278+
pub(crate) fn is_excluded_item_in_dir(&self, path: &Path) -> bool {
279+
#[cfg(target_family = "windows")]
280+
let path = normalize_windows_path(path);
281+
// We're assuming that `excluded_directories` are already normalized
282+
self.excluded_directories.iter().any(|p| p.starts_with(path))
283+
}
284+
269285
#[cfg(target_family = "unix")]
270286
pub(crate) fn exclude_other_filesystems(&self) -> bool {
271287
self.exclude_other_filesystems.unwrap_or(false)

czkawka_core/src/tools/empty_folder/core.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ impl EmptyFolder {
209209
folder_entries_list: &mut Vec<FolderEntry>,
210210
) {
211211
let next_folder = entry_data.path();
212-
if excluded_items.is_excluded(&next_folder) || directories.is_excluded(&next_folder) {
212+
if excluded_items.is_excluded(&next_folder) || directories.is_excluded_dir(&next_folder) {
213213
if non_empty_folder.is_none() {
214214
*non_empty_folder = Some(current_folder_as_str.to_string());
215215
}

czkawka_core/src/tools/temporary/core.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ pub(crate) fn check_folder_children(
136136
}
137137

138138
let next_item = entry_data.path();
139-
if directories.is_excluded(&next_item) {
139+
if directories.is_excluded_dir(&next_item) {
140140
return;
141141
}
142142

0 commit comments

Comments
 (0)