diff --git a/czkawka_core/i18n/en/czkawka_core.ftl b/czkawka_core/i18n/en/czkawka_core.ftl index 1bac2fc..da3fd04 100644 --- a/czkawka_core/i18n/en/czkawka_core.ftl +++ b/czkawka_core/i18n/en/czkawka_core.ftl @@ -18,7 +18,6 @@ core_folder_no_modification_date = Unable to get modification date from folder { core_missing_no_chosen_included_directory = At least one directory must be provided core_directory_wildcard_no_supported = Directories: Wildcards in path are not supported, ignoring { $path } -core_directory_relative_path = Directories: Relative path are not supported, ignoring { $path } core_directory_must_exists = Directories: Provided folder path must exist, ignoring { $path } core_directory_must_be_directory = Directories: Provided path must point at the directory, ignoring { $path } core_included_directory_zero_valid_directories = Included Directory ERROR: Not found even one correct path to included which is required diff --git a/czkawka_core/src/common_directory.rs b/czkawka_core/src/common_directory.rs index 9c6eb8c..f85390e 100644 --- a/czkawka_core/src/common_directory.rs +++ b/czkawka_core/src/common_directory.rs @@ -22,8 +22,21 @@ impl Directories { Default::default() } - pub fn set_reference_directory(&mut self, reference_directory: Vec) { - self.reference_directories = reference_directory; + pub fn set_reference_directory(&mut self, reference_directory: &[PathBuf]) -> Messages { + let mut messages: Messages = Messages::new(); + + self.reference_directories = reference_directory + .iter() + .filter_map(|directory| { + let (dir, msg) = Self::canonicalize_and_clear_path(directory, false); + + messages.extend_with_another_messages(msg); + + dir + }) + .collect::>(); + + messages } pub fn set_included_directory(&mut self, included_directory: Vec) -> Messages { @@ -37,44 +50,14 @@ impl Directories { let directories: Vec = included_directory; let mut checked_directories: Vec = Vec::new(); - for mut directory in directories { - if directory.to_string_lossy().contains('*') { - messages.warnings.push(flc!( - "core_directory_wildcard_no_supported", - generate_translation_hashmap(vec![("path", directory.display().to_string())]) - )); - continue; - } + for directory in directories { + let (dir, msg) = Self::canonicalize_and_clear_path(&directory, false); - if !directory.exists() { - messages.warnings.push(flc!( - "core_directory_must_exists", - generate_translation_hashmap(vec![("path", directory.display().to_string())]) - )); - continue; - } - if !directory.is_dir() { - messages.warnings.push(flc!( - "core_directory_must_be_directory", - generate_translation_hashmap(vec![("path", directory.display().to_string())]) - )); - continue; - } + messages.extend_with_another_messages(msg); - // If not checking windows strange paths, try to canonicalize them - if !directory.starts_with("\\") { - let Ok(dir2) = directory.canonicalize() else { - messages.warnings.push(flc!( - "core_directory_must_exists", - generate_translation_hashmap(vec![("path", directory.display().to_string())]) - )); - continue; - }; - - directory = dir2; + if let Some(dir) = dir { + checked_directories.push(dir); } - - checked_directories.push(directory); } if checked_directories.is_empty() { @@ -103,48 +86,54 @@ impl Directories { messages.errors.push(flc!("core_excluded_directory_pointless_slash")); break; } - if directory_as_string.contains('*') { - messages.warnings.push(flc!( - "core_directory_wildcard_no_supported", - generate_translation_hashmap(vec![("path", directory.display().to_string())]) - )); - continue; - } - #[cfg(not(target_family = "windows"))] - if directory.is_relative() { - messages.warnings.push(flc!( - "core_directory_relative_path", - generate_translation_hashmap(vec![("path", directory.display().to_string())]) - )); - continue; - } - #[cfg(target_family = "windows")] - if directory.is_relative() && !directory.starts_with("\\") { - messages.warnings.push(flc!( - "core_directory_relative_path", - generate_translation_hashmap(vec![("path", directory.display().to_string())]) - )); - continue; - } - if !directory.exists() { - // No error when excluded directories are missing - continue; + let (dir, msg) = Self::canonicalize_and_clear_path(&directory, true); + + messages.extend_with_another_messages(msg); + + if let Some(dir) = dir { + checked_directories.push(dir); } - if !directory.is_dir() { - messages.warnings.push(flc!( - "core_directory_must_be_directory", - generate_translation_hashmap(vec![("path", directory.display().to_string())]) - )); - continue; - } - checked_directories.push(directory); } self.excluded_directories = checked_directories; messages } + fn canonicalize_and_clear_path(directory: &Path, is_excluded: bool) -> (Option, Messages) { + let mut messages = Messages::new(); + let mut directory = directory.to_path_buf(); + if !directory.exists() { + if !is_excluded { + messages.warnings.push(flc!( + "core_directory_must_exists", + generate_translation_hashmap(vec![("path", directory.display().to_string())]) + )); + } + return (None, messages); + } + + if !directory.is_dir() { + messages.warnings.push(flc!( + "core_directory_must_be_directory", + generate_translation_hashmap(vec![("path", directory.display().to_string())]) + )); + return (None, messages); + } + + // Try to canonicalize them + if let Ok(dir) = directory.canonicalize() { + directory = dir; + } + if cfg!(windows) { + let path_str = directory.to_string_lossy().to_string(); + if let Some(path_str) = path_str.strip_prefix(r"\\?\") { + directory = PathBuf::from(path_str); + } + } + (Some(directory), messages) + } + #[cfg(target_family = "unix")] pub fn set_exclude_other_filesystems(&mut self, exclude_other_filesystems: bool) { self.exclude_other_filesystems = Some(exclude_other_filesystems); diff --git a/czkawka_core/src/common_tool.rs b/czkawka_core/src/common_tool.rs index 694abe0..6964d1b 100644 --- a/czkawka_core/src/common_tool.rs +++ b/czkawka_core/src/common_tool.rs @@ -112,7 +112,8 @@ pub trait CommonData { } fn set_reference_directory(&mut self, reference_directory: Vec) { - self.get_cd_mut().directories.set_reference_directory(reference_directory); + let messages = self.get_cd_mut().directories.set_reference_directory(&reference_directory); + self.get_cd_mut().text_messages.extend_with_another_messages(messages); } #[cfg(target_family = "unix")]