rusty_sweeper/scanner/
options.rs1use std::path::PathBuf;
2use std::time::Duration;
3
4#[derive(Debug, Clone)]
6pub struct ScanCacheOptions {
7 pub path: PathBuf,
8 pub ttl: Duration,
9}
10
11#[derive(Debug, Clone, Default)]
13pub struct ScanOptions {
14 pub max_depth: Option<usize>,
16
17 pub include_hidden: bool,
19
20 pub one_file_system: bool,
22
23 pub threads: usize,
25
26 pub exclude_patterns: Vec<String>,
28
29 pub follow_symlinks: bool,
31
32 pub cache: Option<ScanCacheOptions>,
34}
35
36pub const LINUX_VIRTUAL_FS_PATHS: &[&str] = &["/proc", "/dev", "/sys", "/run"];
39
40impl ScanOptions {
41 pub fn is_linux_virtual_fs(path: &std::path::Path) -> bool {
43 LINUX_VIRTUAL_FS_PATHS
44 .iter()
45 .map(std::path::Path::new)
46 .any(|virtual_root| path == virtual_root || path.starts_with(virtual_root))
47 }
48
49 pub fn new() -> Self {
51 Self::default()
52 }
53
54 pub fn with_max_depth(mut self, depth: usize) -> Self {
56 self.max_depth = Some(depth);
57 self
58 }
59
60 pub fn with_hidden(mut self, include: bool) -> Self {
62 self.include_hidden = include;
63 self
64 }
65
66 pub fn with_one_file_system(mut self, enabled: bool) -> Self {
68 self.one_file_system = enabled;
69 self
70 }
71
72 pub fn with_threads(mut self, threads: usize) -> Self {
74 self.threads = threads;
75 self
76 }
77
78 pub fn with_exclude(mut self, patterns: Vec<String>) -> Self {
80 self.exclude_patterns = patterns;
81 self
82 }
83
84 pub fn with_follow_symlinks(mut self, follow: bool) -> Self {
86 self.follow_symlinks = follow;
87 self
88 }
89
90 pub fn with_cache(mut self, path: PathBuf, ttl_seconds: u64) -> Self {
92 self.cache = Some(ScanCacheOptions {
93 path,
94 ttl: Duration::from_secs(ttl_seconds),
95 });
96 self
97 }
98
99 pub fn default_cache_path() -> Option<PathBuf> {
101 dirs::cache_dir().map(|directory| directory.join("rusty-sweeper/scan-metadata.json"))
102 }
103}
104
105#[cfg(test)]
106mod tests {
107 use super::*;
108
109 #[test]
110 fn test_default_options() {
111 let opts = ScanOptions::default();
112 assert_eq!(opts.max_depth, None);
113 assert!(!opts.include_hidden);
114 assert!(!opts.one_file_system);
115 assert_eq!(opts.threads, 0);
116 assert!(opts.exclude_patterns.is_empty());
117 assert!(!opts.follow_symlinks);
118 }
119
120 #[test]
121 fn test_scan_options_builder() {
122 let opts = ScanOptions::new()
123 .with_max_depth(5)
124 .with_hidden(true)
125 .with_one_file_system(true);
126
127 assert_eq!(opts.max_depth, Some(5));
128 assert!(opts.include_hidden);
129 assert!(opts.one_file_system);
130 }
131
132 #[test]
133 fn test_scan_options_threads() {
134 let opts = ScanOptions::new().with_threads(4);
135 assert_eq!(opts.threads, 4);
136 }
137
138 #[test]
139 fn test_scan_options_exclude() {
140 let opts =
141 ScanOptions::new().with_exclude(vec!["*.tmp".to_string(), "node_modules".to_string()]);
142 assert_eq!(opts.exclude_patterns.len(), 2);
143 assert_eq!(opts.exclude_patterns[0], "*.tmp");
144 }
145
146 #[test]
147 fn test_scan_options_follow_symlinks() {
148 let opts = ScanOptions::new().with_follow_symlinks(true);
149 assert!(opts.follow_symlinks);
150 }
151
152 #[test]
153 fn test_scan_options_chaining() {
154 let opts = ScanOptions::new()
155 .with_max_depth(10)
156 .with_hidden(true)
157 .with_threads(8)
158 .with_one_file_system(true)
159 .with_follow_symlinks(false)
160 .with_exclude(vec!["*.log".to_string()]);
161
162 assert_eq!(opts.max_depth, Some(10));
163 assert!(opts.include_hidden);
164 assert_eq!(opts.threads, 8);
165 assert!(opts.one_file_system);
166 assert!(!opts.follow_symlinks);
167 assert_eq!(opts.exclude_patterns.len(), 1);
168 }
169
170 #[test]
171 fn test_is_linux_virtual_fs() {
172 use std::path::Path;
173
174 assert!(ScanOptions::is_linux_virtual_fs(Path::new("/proc")));
176 assert!(ScanOptions::is_linux_virtual_fs(Path::new(
177 "/proc/1/status"
178 )));
179 assert!(ScanOptions::is_linux_virtual_fs(Path::new("/dev")));
180 assert!(ScanOptions::is_linux_virtual_fs(Path::new("/dev/sda")));
181 assert!(ScanOptions::is_linux_virtual_fs(Path::new("/sys")));
182 assert!(ScanOptions::is_linux_virtual_fs(Path::new(
183 "/sys/class/net"
184 )));
185 assert!(ScanOptions::is_linux_virtual_fs(Path::new("/run")));
186 assert!(ScanOptions::is_linux_virtual_fs(Path::new(
187 "/run/user/1000"
188 )));
189
190 assert!(!ScanOptions::is_linux_virtual_fs(Path::new("/home")));
192 assert!(!ScanOptions::is_linux_virtual_fs(Path::new("/home/user")));
193 assert!(!ScanOptions::is_linux_virtual_fs(Path::new("/tmp")));
194 assert!(!ScanOptions::is_linux_virtual_fs(Path::new("/var/log")));
195 assert!(!ScanOptions::is_linux_virtual_fs(Path::new("/usr/bin")));
196
197 assert!(!ScanOptions::is_linux_virtual_fs(Path::new("/procurement")));
200 assert!(!ScanOptions::is_linux_virtual_fs(Path::new("/development")));
201 assert!(!ScanOptions::is_linux_virtual_fs(Path::new("/system")));
202 assert!(!ScanOptions::is_linux_virtual_fs(Path::new("/runner")));
203 }
204}