2020-10-15 05:41:37 +13:00
|
|
|
use std::path::Path;
|
2020-09-27 03:52:13 +13:00
|
|
|
|
2021-11-28 08:49:20 +13:00
|
|
|
use crate::common::Common;
|
|
|
|
|
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 {
|
|
|
|
pub items: Vec<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
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-10-05 19:06:47 +13:00
|
|
|
pub fn set_excluded_items(&mut self, excluded_items: Vec<String>) -> (Vec<String>, Vec<String>, Vec<String>) {
|
|
|
|
let messages: Vec<String> = Vec::new();
|
|
|
|
let mut warnings: Vec<String> = Vec::new();
|
|
|
|
let errors: Vec<String> = Vec::new();
|
2020-09-27 03:52:13 +13:00
|
|
|
if excluded_items.is_empty() {
|
2023-10-05 19:06:47 +13:00
|
|
|
return (messages, warnings, errors);
|
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);
|
|
|
|
}
|
|
|
|
self.items = checked_expressions;
|
2023-10-05 19:06:47 +13:00
|
|
|
(messages, warnings, errors)
|
2020-09-27 03:52:13 +13:00
|
|
|
}
|
2020-10-15 05:41:37 +13:00
|
|
|
|
|
|
|
pub fn is_excluded(&self, path: impl AsRef<Path>) -> bool {
|
|
|
|
#[cfg(target_family = "windows")]
|
|
|
|
let path = Common::normalize_windows_path(path);
|
|
|
|
|
|
|
|
for expression in &self.items {
|
|
|
|
if Common::regex_check(expression, &path) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
2020-09-27 03:52:13 +13:00
|
|
|
}
|