2023-12-04 09:18:31 +13:00
|
|
|
#[cfg(not(target_family = "unix"))]
|
|
|
|
use crate::common::normalize_windows_path;
|
|
|
|
use crate::common::regex_check;
|
2020-10-15 05:41:37 +13:00
|
|
|
use std::path::Path;
|
2020-09-27 03:52:13 +13:00
|
|
|
|
2023-12-04 09:18:31 +13:00
|
|
|
use crate::common_messages::Messages;
|
2021-11-28 08:49:20 +13:00
|
|
|
|
2023-12-04 00:06:42 +13:00
|
|
|
#[cfg(target_family = "unix")]
|
|
|
|
pub const DEFAULT_EXCLUDED_DIRECTORIES: &[&str] = &["/proc", "/dev", "/sys", "/run", "/snap"];
|
|
|
|
#[cfg(not(target_family = "unix"))]
|
|
|
|
pub const DEFAULT_EXCLUDED_DIRECTORIES: &[&str] = &["C:\\Windows"];
|
|
|
|
|
2023-10-08 06:55:23 +13:00
|
|
|
#[cfg(target_family = "unix")]
|
|
|
|
pub const DEFAULT_EXCLUDED_ITEMS: &str = "*/.git/*,*/node_modules/*,*/lost+found/*,*/Trash/*,*/.Trash-*/*,*/snap/*,/home/*/.cache/*";
|
|
|
|
#[cfg(not(target_family = "unix"))]
|
|
|
|
pub const DEFAULT_EXCLUDED_ITEMS: &str = "*\\.git\\*,*\\node_modules\\*,*\\lost+found\\*,*:\\windows\\*,*:\\$RECYCLE.BIN\\*,*:\\$SysReset\\*,*:\\System Volume Information\\*,*:\\OneDriveTemp\\*,*:\\hiberfil.sys,*:\\pagefile.sys,*:\\swapfile.sys";
|
|
|
|
|
2023-10-05 19:06:47 +13:00
|
|
|
#[derive(Debug, Clone, Default)]
|
2020-09-27 03:52:13 +13:00
|
|
|
pub struct ExcludedItems {
|
2023-12-04 09:18:31 +13:00
|
|
|
expressions: Vec<String>,
|
|
|
|
connected_expressions: Vec<SingleExcludedItem>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Default)]
|
|
|
|
pub struct SingleExcludedItem {
|
|
|
|
pub expression: String,
|
|
|
|
pub expression_splits: Vec<String>,
|
|
|
|
pub unique_extensions_splits: Vec<String>,
|
2020-09-27 03:52:13 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ExcludedItems {
|
2020-10-10 09:32:08 +13:00
|
|
|
pub fn new() -> Self {
|
|
|
|
Default::default()
|
2020-09-27 03:52:13 +13:00
|
|
|
}
|
2023-10-11 07:54:41 +13:00
|
|
|
|
2023-12-04 09:18:31 +13:00
|
|
|
pub fn new_from(excluded_items: Vec<String>) -> Self {
|
|
|
|
let mut s = Self::new();
|
|
|
|
s.set_excluded_items(excluded_items);
|
|
|
|
s
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_excluded_items(&mut self, excluded_items: Vec<String>) -> Messages {
|
2023-10-05 19:06:47 +13:00
|
|
|
let mut warnings: Vec<String> = Vec::new();
|
2020-09-27 03:52:13 +13:00
|
|
|
if excluded_items.is_empty() {
|
2023-12-04 09:18:31 +13:00
|
|
|
return Messages::new();
|
2020-09-27 03:52:13 +13:00
|
|
|
}
|
|
|
|
|
2021-01-11 08:44:10 +13:00
|
|
|
let expressions: Vec<String> = excluded_items;
|
2020-09-27 03:52:13 +13:00
|
|
|
let mut checked_expressions: Vec<String> = Vec::new();
|
|
|
|
|
|
|
|
for expression in expressions {
|
|
|
|
let expression: String = expression.trim().to_string();
|
|
|
|
|
2021-01-01 07:53:49 +13:00
|
|
|
if expression.is_empty() {
|
2020-09-27 03:52:13 +13:00
|
|
|
continue;
|
|
|
|
}
|
2022-01-14 18:34:43 +13:00
|
|
|
|
|
|
|
#[cfg(target_family = "windows")]
|
|
|
|
let expression = expression.replace("/", "\\");
|
|
|
|
|
2020-09-27 03:52:13 +13:00
|
|
|
if expression == "DEFAULT" {
|
2023-10-08 06:55:23 +13:00
|
|
|
checked_expressions.push(DEFAULT_EXCLUDED_ITEMS.to_string());
|
2020-09-27 03:52:13 +13:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if !expression.contains('*') {
|
2023-10-05 19:06:47 +13:00
|
|
|
warnings.push("Excluded Items Warning: Wildcard * is required in expression, ignoring ".to_string() + expression.as_str());
|
2020-09-27 03:52:13 +13:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
checked_expressions.push(expression);
|
|
|
|
}
|
2023-12-04 09:18:31 +13:00
|
|
|
|
|
|
|
for checked_expression in &checked_expressions {
|
|
|
|
let item = new_excluded_item(checked_expression);
|
|
|
|
self.expressions.push(item.expression.clone());
|
|
|
|
self.connected_expressions.push(item);
|
|
|
|
}
|
|
|
|
Messages {
|
|
|
|
messages: vec![],
|
|
|
|
warnings,
|
|
|
|
errors: vec![],
|
|
|
|
}
|
2020-09-27 03:52:13 +13:00
|
|
|
}
|
2020-10-15 05:41:37 +13:00
|
|
|
|
2023-12-04 09:18:31 +13:00
|
|
|
pub fn get_excluded_items(&self) -> &Vec<String> {
|
|
|
|
&self.expressions
|
|
|
|
}
|
2024-01-14 01:57:51 +13:00
|
|
|
pub fn is_excluded(&self, path: &Path) -> bool {
|
2023-12-04 09:18:31 +13:00
|
|
|
if self.connected_expressions.is_empty() {
|
|
|
|
return false;
|
|
|
|
}
|
2020-10-15 05:41:37 +13:00
|
|
|
#[cfg(target_family = "windows")]
|
2023-12-04 09:18:31 +13:00
|
|
|
let path = normalize_windows_path(path);
|
2020-10-15 05:41:37 +13:00
|
|
|
|
2024-01-14 01:57:51 +13:00
|
|
|
let path_str = path.to_string_lossy();
|
|
|
|
|
2023-12-04 09:18:31 +13:00
|
|
|
for expression in &self.connected_expressions {
|
2024-01-14 01:57:51 +13:00
|
|
|
if regex_check(expression, &path_str) {
|
2020-10-15 05:41:37 +13:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
2020-09-27 03:52:13 +13:00
|
|
|
}
|
2023-12-04 09:18:31 +13:00
|
|
|
|
|
|
|
pub fn new_excluded_item(expression: &str) -> SingleExcludedItem {
|
|
|
|
let expression = expression.trim().to_string();
|
|
|
|
let expression_splits: Vec<String> = expression.split('*').filter_map(|e| if e.is_empty() { None } else { Some(e.to_string()) }).collect();
|
|
|
|
let mut unique_extensions_splits = expression_splits.clone();
|
|
|
|
unique_extensions_splits.sort();
|
|
|
|
unique_extensions_splits.dedup();
|
2024-01-14 01:57:51 +13:00
|
|
|
unique_extensions_splits.sort_by_key(|b| std::cmp::Reverse(b.len()));
|
2023-12-04 09:18:31 +13:00
|
|
|
SingleExcludedItem {
|
|
|
|
expression,
|
|
|
|
expression_splits,
|
|
|
|
unique_extensions_splits,
|
|
|
|
}
|
|
|
|
}
|