2023-12-03 20:18:31 +00:00
|
|
|
use crate::common::normalize_windows_path;
|
2020-10-14 16:41:37 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2022-04-23 18:21:46 +00:00
|
|
|
#[cfg(target_family = "unix")]
|
|
|
|
use std::{fs, os::unix::fs::MetadataExt};
|
2020-09-26 14:52:13 +00:00
|
|
|
|
2023-10-10 18:54:41 +00:00
|
|
|
use crate::common_messages::Messages;
|
2022-01-19 21:35:07 +00:00
|
|
|
use crate::flc;
|
|
|
|
use crate::localizer_core::generate_translation_hashmap;
|
2021-11-27 19:49:20 +00:00
|
|
|
|
2023-10-05 06:06:47 +00:00
|
|
|
#[derive(Debug, Clone, Default)]
|
2020-09-26 14:52:13 +00:00
|
|
|
pub struct Directories {
|
2020-10-14 16:41:37 +00:00
|
|
|
pub excluded_directories: Vec<PathBuf>,
|
|
|
|
pub included_directories: Vec<PathBuf>,
|
2021-12-24 08:18:55 +00:00
|
|
|
pub reference_directories: Vec<PathBuf>,
|
2023-06-09 20:11:47 +00:00
|
|
|
pub exclude_other_filesystems: Option<bool>,
|
2022-04-23 18:21:46 +00:00
|
|
|
#[cfg(target_family = "unix")]
|
2023-06-09 20:11:47 +00:00
|
|
|
pub included_dev_ids: Vec<u64>,
|
2020-09-26 14:52:13 +00:00
|
|
|
}
|
2021-11-27 19:57:10 +00:00
|
|
|
|
2020-09-26 14:52:13 +00:00
|
|
|
impl Directories {
|
2020-10-09 20:32:08 +00:00
|
|
|
pub fn new() -> Self {
|
|
|
|
Default::default()
|
2020-09-26 14:52:13 +00:00
|
|
|
}
|
|
|
|
|
2023-10-15 12:17:46 +00:00
|
|
|
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
|
2021-12-24 08:18:55 +00:00
|
|
|
}
|
|
|
|
|
2023-10-10 18:54:41 +00:00
|
|
|
pub fn set_included_directory(&mut self, included_directory: Vec<PathBuf>) -> Messages {
|
|
|
|
let mut messages: Messages = Messages::new();
|
2023-10-05 06:06:47 +00:00
|
|
|
|
2020-09-26 14:52:13 +00:00
|
|
|
if included_directory.is_empty() {
|
2023-10-10 18:54:41 +00:00
|
|
|
messages.errors.push(flc!("core_missing_no_chosen_included_directory"));
|
|
|
|
return messages;
|
2020-09-26 14:52:13 +00:00
|
|
|
}
|
|
|
|
|
2021-01-10 19:44:10 +00:00
|
|
|
let directories: Vec<PathBuf> = included_directory;
|
2020-09-26 14:52:13 +00:00
|
|
|
|
2020-10-14 16:41:37 +00:00
|
|
|
let mut checked_directories: Vec<PathBuf> = Vec::new();
|
2023-10-15 12:17:46 +00:00
|
|
|
for directory in directories {
|
|
|
|
let (dir, msg) = Self::canonicalize_and_clear_path(&directory, false);
|
2020-12-30 12:41:18 +00:00
|
|
|
|
2023-10-15 12:17:46 +00:00
|
|
|
messages.extend_with_another_messages(msg);
|
2023-10-12 16:48:46 +00:00
|
|
|
|
2023-10-15 12:17:46 +00:00
|
|
|
if let Some(dir) = dir {
|
|
|
|
checked_directories.push(dir);
|
2023-10-12 16:48:46 +00:00
|
|
|
}
|
2020-09-26 14:52:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if checked_directories.is_empty() {
|
2023-10-10 18:54:41 +00:00
|
|
|
messages.warnings.push(flc!("core_included_directory_zero_valid_directories"));
|
|
|
|
return messages;
|
2020-09-26 14:52:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.included_directories = checked_directories;
|
|
|
|
|
2023-10-10 18:54:41 +00:00
|
|
|
messages
|
2020-09-26 14:52:13 +00:00
|
|
|
}
|
|
|
|
|
2023-10-10 18:54:41 +00:00
|
|
|
pub fn set_excluded_directory(&mut self, excluded_directory: Vec<PathBuf>) -> Messages {
|
|
|
|
let mut messages: Messages = Messages::new();
|
2023-10-05 06:06:47 +00:00
|
|
|
|
2020-09-26 14:52:13 +00:00
|
|
|
if excluded_directory.is_empty() {
|
2023-10-10 18:54:41 +00:00
|
|
|
return messages;
|
2020-09-26 14:52:13 +00:00
|
|
|
}
|
|
|
|
|
2021-01-10 19:44:10 +00:00
|
|
|
let directories: Vec<PathBuf> = excluded_directory;
|
2020-09-26 14:52:13 +00:00
|
|
|
|
2020-10-14 16:41:37 +00:00
|
|
|
let mut checked_directories: Vec<PathBuf> = Vec::new();
|
|
|
|
for directory in directories {
|
|
|
|
let directory_as_string = directory.to_string_lossy();
|
|
|
|
if directory_as_string == "/" {
|
2023-10-10 18:54:41 +00:00
|
|
|
messages.errors.push(flc!("core_excluded_directory_pointless_slash"));
|
2020-09-26 14:52:13 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-12-30 12:41:18 +00:00
|
|
|
|
2023-10-15 12:17:46 +00:00
|
|
|
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);
|
2020-09-26 14:52:13 +00:00
|
|
|
}
|
2023-10-15 12:17:46 +00:00
|
|
|
}
|
|
|
|
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 {
|
2023-10-10 18:54:41 +00:00
|
|
|
messages.warnings.push(flc!(
|
2023-10-15 12:17:46 +00:00
|
|
|
"core_directory_must_exists",
|
2023-12-07 18:38:41 +00:00
|
|
|
generate_translation_hashmap(vec![("path", directory.to_string_lossy().to_string())])
|
2021-12-21 17:44:20 +00:00
|
|
|
));
|
2020-09-26 14:52:13 +00:00
|
|
|
}
|
2023-10-15 12:17:46 +00:00
|
|
|
return (None, messages);
|
2020-09-26 14:52:13 +00:00
|
|
|
}
|
2023-10-05 06:06:47 +00:00
|
|
|
|
2023-10-15 12:17:46 +00:00
|
|
|
if !directory.is_dir() {
|
|
|
|
messages.warnings.push(flc!(
|
|
|
|
"core_directory_must_be_directory",
|
2023-12-07 18:38:41 +00:00
|
|
|
generate_translation_hashmap(vec![("path", directory.to_string_lossy().to_string())])
|
2023-10-15 12:17:46 +00:00
|
|
|
));
|
|
|
|
return (None, messages);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to canonicalize them
|
|
|
|
if cfg!(windows) {
|
2023-10-16 07:22:22 +00:00
|
|
|
// Only canonicalize if it's not a network path
|
|
|
|
// This can be check by checking if path starts with \\?\UNC\
|
|
|
|
if let Ok(dir_can) = directory.canonicalize() {
|
|
|
|
let dir_can_str = dir_can.to_string_lossy().to_string();
|
|
|
|
if let Some(dir_can_str) = dir_can_str.strip_prefix(r"\\?\") {
|
|
|
|
if dir_can_str.chars().nth(1) == Some(':') {
|
|
|
|
directory = PathBuf::from(dir_can_str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if let Ok(dir) = directory.canonicalize() {
|
|
|
|
directory = dir;
|
2023-10-15 12:17:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
(Some(directory), messages)
|
2020-09-26 14:52:13 +00:00
|
|
|
}
|
|
|
|
|
2022-04-23 18:21:46 +00:00
|
|
|
#[cfg(target_family = "unix")]
|
|
|
|
pub fn set_exclude_other_filesystems(&mut self, exclude_other_filesystems: bool) {
|
2023-01-28 17:54:02 +00:00
|
|
|
self.exclude_other_filesystems = Some(exclude_other_filesystems);
|
2022-04-23 18:21:46 +00:00
|
|
|
}
|
|
|
|
|
2023-10-10 18:54:41 +00:00
|
|
|
pub fn optimize_directories(&mut self, recursive_search: bool) -> Messages {
|
|
|
|
let mut messages: Messages = Messages::new();
|
2023-10-05 06:06:47 +00:00
|
|
|
|
2020-10-14 16:41:37 +00:00
|
|
|
let mut optimized_included: Vec<PathBuf> = Vec::new();
|
|
|
|
let mut optimized_excluded: Vec<PathBuf> = Vec::new();
|
2020-09-26 14:52:13 +00:00
|
|
|
|
2020-10-14 16:41:37 +00:00
|
|
|
if cfg!(target_family = "windows") {
|
2023-12-03 20:18:31 +00:00
|
|
|
self.included_directories = self.included_directories.iter().map(normalize_windows_path).collect();
|
|
|
|
self.excluded_directories = self.excluded_directories.iter().map(normalize_windows_path).collect();
|
|
|
|
self.reference_directories = self.reference_directories.iter().map(normalize_windows_path).collect();
|
2020-10-10 13:18:04 +00:00
|
|
|
}
|
|
|
|
|
2020-09-26 14:52:13 +00:00
|
|
|
// Remove duplicated entries like: "/", "/"
|
|
|
|
|
2022-07-24 18:48:02 +00:00
|
|
|
self.excluded_directories.sort_unstable();
|
|
|
|
self.included_directories.sort_unstable();
|
|
|
|
self.reference_directories.sort_unstable();
|
2020-09-26 14:52:13 +00:00
|
|
|
|
|
|
|
self.excluded_directories.dedup();
|
|
|
|
self.included_directories.dedup();
|
2021-12-24 08:18:55 +00:00
|
|
|
self.reference_directories.dedup();
|
2020-09-26 14:52:13 +00:00
|
|
|
|
|
|
|
// Optimize for duplicated included directories - "/", "/home". "/home/Pulpit" to "/"
|
2022-07-19 17:09:52 +00:00
|
|
|
// Do not use when not using recursive search or using
|
|
|
|
if recursive_search && !self.exclude_other_filesystems.unwrap_or(false) {
|
2020-09-26 14:52:13 +00:00
|
|
|
// This is only point which can't be done when recursive search is disabled.
|
|
|
|
let mut is_inside: bool;
|
|
|
|
for ed_checked in &self.excluded_directories {
|
|
|
|
is_inside = false;
|
|
|
|
for ed_help in &self.excluded_directories {
|
|
|
|
if ed_checked == ed_help {
|
|
|
|
// We checking same element
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ed_checked.starts_with(ed_help) {
|
|
|
|
is_inside = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !is_inside {
|
2020-10-14 16:41:37 +00:00
|
|
|
optimized_excluded.push(ed_checked.clone());
|
2020-09-26 14:52:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for id_checked in &self.included_directories {
|
|
|
|
is_inside = false;
|
|
|
|
for id_help in &self.included_directories {
|
|
|
|
if id_checked == id_help {
|
|
|
|
// We checking same element
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if id_checked.starts_with(id_help) {
|
|
|
|
is_inside = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !is_inside {
|
2020-10-14 16:41:37 +00:00
|
|
|
optimized_included.push(id_checked.clone());
|
2020-09-26 14:52:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.included_directories = optimized_included;
|
2020-10-14 16:41:37 +00:00
|
|
|
optimized_included = Vec::new();
|
2020-09-26 14:52:13 +00:00
|
|
|
self.excluded_directories = optimized_excluded;
|
2020-10-14 16:41:37 +00:00
|
|
|
optimized_excluded = Vec::new();
|
2020-09-26 14:52:13 +00:00
|
|
|
}
|
|
|
|
|
2020-09-26 15:16:12 +00:00
|
|
|
// Remove included directories which are inside any excluded directory
|
2020-09-26 14:52:13 +00:00
|
|
|
for id in &self.included_directories {
|
|
|
|
let mut is_inside: bool = false;
|
|
|
|
for ed in &self.excluded_directories {
|
|
|
|
if id.starts_with(ed) {
|
|
|
|
is_inside = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !is_inside {
|
2020-10-14 16:41:37 +00:00
|
|
|
optimized_included.push(id.clone());
|
2020-09-26 14:52:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
self.included_directories = optimized_included;
|
2020-10-14 16:41:37 +00:00
|
|
|
optimized_included = Vec::new();
|
2020-09-26 14:52:13 +00:00
|
|
|
|
|
|
|
// Remove non existed directories
|
|
|
|
for id in &self.included_directories {
|
|
|
|
let path = Path::new(id);
|
|
|
|
if path.exists() {
|
2020-10-14 16:41:37 +00:00
|
|
|
optimized_included.push(id.clone());
|
2020-09-26 14:52:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for ed in &self.excluded_directories {
|
|
|
|
let path = Path::new(ed);
|
|
|
|
if path.exists() {
|
2020-10-14 16:41:37 +00:00
|
|
|
optimized_excluded.push(ed.clone());
|
2020-09-26 14:52:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.included_directories = optimized_included;
|
|
|
|
self.excluded_directories = optimized_excluded;
|
2020-10-14 16:41:37 +00:00
|
|
|
optimized_excluded = Vec::new();
|
2020-09-26 14:52:13 +00:00
|
|
|
|
2020-09-26 15:16:12 +00:00
|
|
|
// Excluded paths must are inside included path, because
|
2020-09-26 14:52:13 +00:00
|
|
|
for ed in &self.excluded_directories {
|
|
|
|
let mut is_inside: bool = false;
|
|
|
|
for id in &self.included_directories {
|
|
|
|
if ed.starts_with(id) {
|
|
|
|
is_inside = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if is_inside {
|
2020-10-14 16:41:37 +00:00
|
|
|
optimized_excluded.push(ed.clone());
|
2020-09-26 14:52:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.excluded_directories = optimized_excluded;
|
|
|
|
|
2021-12-24 08:18:55 +00:00
|
|
|
// Selecting Reference folders
|
|
|
|
{
|
|
|
|
let mut ref_folders = Vec::new();
|
|
|
|
for folder in &self.reference_directories {
|
2022-11-23 19:23:17 +00:00
|
|
|
if self.included_directories.iter().any(|e| folder.starts_with(e)) {
|
2021-12-24 08:18:55 +00:00
|
|
|
ref_folders.push(folder.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.reference_directories = ref_folders;
|
|
|
|
}
|
|
|
|
|
2020-09-26 14:52:13 +00:00
|
|
|
if self.included_directories.is_empty() {
|
2023-10-10 18:54:41 +00:00
|
|
|
messages.errors.push(flc!("core_directory_overlap"));
|
|
|
|
return messages;
|
2020-09-26 14:52:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Not needed, but better is to have sorted everything
|
2022-07-24 18:48:02 +00:00
|
|
|
self.excluded_directories.sort_unstable();
|
|
|
|
self.included_directories.sort_unstable();
|
2022-04-23 18:21:46 +00:00
|
|
|
|
|
|
|
// Get device IDs for included directories
|
|
|
|
#[cfg(target_family = "unix")]
|
|
|
|
if self.exclude_other_filesystems() {
|
|
|
|
for d in &self.included_directories {
|
|
|
|
match fs::metadata(d) {
|
|
|
|
Ok(m) => self.included_dev_ids.push(m.dev()),
|
2023-10-10 18:54:41 +00:00
|
|
|
Err(_) => messages.errors.push(flc!(
|
2022-04-23 18:21:46 +00:00
|
|
|
"core_directory_unable_to_get_device_id",
|
2023-12-07 18:38:41 +00:00
|
|
|
generate_translation_hashmap(vec![("path", d.to_string_lossy().to_string())])
|
2022-04-23 18:21:46 +00:00
|
|
|
)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-10 18:54:41 +00:00
|
|
|
messages
|
2020-09-26 14:52:13 +00:00
|
|
|
}
|
2020-10-14 16:41:37 +00:00
|
|
|
|
2023-05-07 18:54:05 +00:00
|
|
|
pub fn is_in_referenced_directory(&self, path: &Path) -> bool {
|
|
|
|
self.reference_directories.iter().any(|e| path.starts_with(e))
|
|
|
|
}
|
|
|
|
|
2020-10-14 16:41:37 +00:00
|
|
|
pub fn is_excluded(&self, path: impl AsRef<Path>) -> bool {
|
|
|
|
let path = path.as_ref();
|
|
|
|
#[cfg(target_family = "windows")]
|
2023-12-03 20:18:31 +00:00
|
|
|
let path = normalize_windows_path(path);
|
2020-10-14 16:41:37 +00:00
|
|
|
// We're assuming that `excluded_directories` are already normalized
|
|
|
|
self.excluded_directories.iter().any(|p| p.as_path() == path)
|
|
|
|
}
|
2022-04-23 18:21:46 +00:00
|
|
|
|
|
|
|
#[cfg(target_family = "unix")]
|
|
|
|
pub fn exclude_other_filesystems(&self) -> bool {
|
|
|
|
self.exclude_other_filesystems.unwrap_or(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_family = "unix")]
|
|
|
|
pub fn is_on_other_filesystems(&self, path: impl AsRef<Path>) -> Result<bool, String> {
|
|
|
|
let path = path.as_ref();
|
|
|
|
match fs::metadata(path) {
|
|
|
|
Ok(m) => Ok(!self.included_dev_ids.iter().any(|&id| id == m.dev())),
|
|
|
|
Err(_) => Err(flc!(
|
|
|
|
"core_directory_unable_to_get_device_id",
|
2023-12-07 18:38:41 +00:00
|
|
|
generate_translation_hashmap(vec![("path", path.to_string_lossy().to_string())])
|
2022-04-23 18:21:46 +00:00
|
|
|
)),
|
|
|
|
}
|
|
|
|
}
|
2020-09-26 14:52:13 +00:00
|
|
|
}
|