use std::path::PathBuf;
use std::time::Duration;
#[derive(Debug, Clone)]
pub struct ScanCacheOptions {
pub path: PathBuf,
pub ttl: Duration,
}
#[derive(Debug, Clone, Default)]
pub struct ScanOptions {
pub max_depth: Option<usize>,
pub include_hidden: bool,
pub one_file_system: bool,
pub threads: usize,
pub exclude_patterns: Vec<String>,
pub follow_symlinks: bool,
pub cache: Option<ScanCacheOptions>,
}
pub const LINUX_VIRTUAL_FS_PATHS: &[&str] = &["/proc", "/dev", "/sys", "/run"];
impl ScanOptions {
pub fn is_linux_virtual_fs(path: &std::path::Path) -> bool {
LINUX_VIRTUAL_FS_PATHS
.iter()
.map(std::path::Path::new)
.any(|virtual_root| path == virtual_root || path.starts_with(virtual_root))
}
pub fn new() -> Self {
Self::default()
}
pub fn with_max_depth(mut self, depth: usize) -> Self {
self.max_depth = Some(depth);
self
}
pub fn with_hidden(mut self, include: bool) -> Self {
self.include_hidden = include;
self
}
pub fn with_one_file_system(mut self, enabled: bool) -> Self {
self.one_file_system = enabled;
self
}
pub fn with_threads(mut self, threads: usize) -> Self {
self.threads = threads;
self
}
pub fn with_exclude(mut self, patterns: Vec<String>) -> Self {
self.exclude_patterns = patterns;
self
}
pub fn with_follow_symlinks(mut self, follow: bool) -> Self {
self.follow_symlinks = follow;
self
}
pub fn with_cache(mut self, path: PathBuf, ttl_seconds: u64) -> Self {
self.cache = Some(ScanCacheOptions {
path,
ttl: Duration::from_secs(ttl_seconds),
});
self
}
pub fn default_cache_path() -> Option<PathBuf> {
dirs::cache_dir().map(|directory| directory.join("rusty-sweeper/scan-metadata.json"))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_options() {
let opts = ScanOptions::default();
assert_eq!(opts.max_depth, None);
assert!(!opts.include_hidden);
assert!(!opts.one_file_system);
assert_eq!(opts.threads, 0);
assert!(opts.exclude_patterns.is_empty());
assert!(!opts.follow_symlinks);
}
#[test]
fn test_scan_options_builder() {
let opts = ScanOptions::new()
.with_max_depth(5)
.with_hidden(true)
.with_one_file_system(true);
assert_eq!(opts.max_depth, Some(5));
assert!(opts.include_hidden);
assert!(opts.one_file_system);
}
#[test]
fn test_scan_options_threads() {
let opts = ScanOptions::new().with_threads(4);
assert_eq!(opts.threads, 4);
}
#[test]
fn test_scan_options_exclude() {
let opts =
ScanOptions::new().with_exclude(vec!["*.tmp".to_string(), "node_modules".to_string()]);
assert_eq!(opts.exclude_patterns.len(), 2);
assert_eq!(opts.exclude_patterns[0], "*.tmp");
}
#[test]
fn test_scan_options_follow_symlinks() {
let opts = ScanOptions::new().with_follow_symlinks(true);
assert!(opts.follow_symlinks);
}
#[test]
fn test_scan_options_chaining() {
let opts = ScanOptions::new()
.with_max_depth(10)
.with_hidden(true)
.with_threads(8)
.with_one_file_system(true)
.with_follow_symlinks(false)
.with_exclude(vec!["*.log".to_string()]);
assert_eq!(opts.max_depth, Some(10));
assert!(opts.include_hidden);
assert_eq!(opts.threads, 8);
assert!(opts.one_file_system);
assert!(!opts.follow_symlinks);
assert_eq!(opts.exclude_patterns.len(), 1);
}
#[test]
fn test_is_linux_virtual_fs() {
use std::path::Path;
assert!(ScanOptions::is_linux_virtual_fs(Path::new("/proc")));
assert!(ScanOptions::is_linux_virtual_fs(Path::new(
"/proc/1/status"
)));
assert!(ScanOptions::is_linux_virtual_fs(Path::new("/dev")));
assert!(ScanOptions::is_linux_virtual_fs(Path::new("/dev/sda")));
assert!(ScanOptions::is_linux_virtual_fs(Path::new("/sys")));
assert!(ScanOptions::is_linux_virtual_fs(Path::new(
"/sys/class/net"
)));
assert!(ScanOptions::is_linux_virtual_fs(Path::new("/run")));
assert!(ScanOptions::is_linux_virtual_fs(Path::new(
"/run/user/1000"
)));
assert!(!ScanOptions::is_linux_virtual_fs(Path::new("/home")));
assert!(!ScanOptions::is_linux_virtual_fs(Path::new("/home/user")));
assert!(!ScanOptions::is_linux_virtual_fs(Path::new("/tmp")));
assert!(!ScanOptions::is_linux_virtual_fs(Path::new("/var/log")));
assert!(!ScanOptions::is_linux_virtual_fs(Path::new("/usr/bin")));
assert!(!ScanOptions::is_linux_virtual_fs(Path::new("/procurement")));
assert!(!ScanOptions::is_linux_virtual_fs(Path::new("/development")));
assert!(!ScanOptions::is_linux_virtual_fs(Path::new("/system")));
assert!(!ScanOptions::is_linux_virtual_fs(Path::new("/runner")));
}
}