1
0
Fork 0
mirror of synced 2024-04-30 02:23:53 +12:00

Canonicalize also windows paths (#1093)

* Canonicalize also windows paths

* El capone

* Hehe

* HASASS

* Also regree

* K

* Kek

* Format
This commit is contained in:
Rafał Mikrut 2023-10-15 14:17:46 +02:00 committed by GitHub
parent 44400e08af
commit 59ff9f3e3c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 62 additions and 73 deletions

View file

@ -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

View file

@ -22,8 +22,21 @@ impl Directories {
Default::default()
}
pub fn set_reference_directory(&mut self, reference_directory: Vec<PathBuf>) {
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::<Vec<PathBuf>>();
messages
}
pub fn set_included_directory(&mut self, included_directory: Vec<PathBuf>) -> Messages {
@ -37,44 +50,14 @@ impl Directories {
let directories: Vec<PathBuf> = included_directory;
let mut checked_directories: Vec<PathBuf> = 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<PathBuf>, 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);

View file

@ -112,7 +112,8 @@ pub trait CommonData {
}
fn set_reference_directory(&mut self, reference_directory: Vec<PathBuf>) {
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")]