Skip to main content

rusty_sweeper/scanner/
options.rs

1use std::path::PathBuf;
2use std::time::Duration;
3
4/// Persistent metadata-cache configuration.
5#[derive(Debug, Clone)]
6pub struct ScanCacheOptions {
7    pub path: PathBuf,
8    pub ttl: Duration,
9}
10
11/// Configuration options for directory scanning operations.
12#[derive(Debug, Clone, Default)]
13pub struct ScanOptions {
14    /// Maximum depth to recurse (None = unlimited)
15    pub max_depth: Option<usize>,
16
17    /// Include hidden files/directories (starting with .)
18    pub include_hidden: bool,
19
20    /// Stay on the same filesystem (don't cross mount points)
21    pub one_file_system: bool,
22
23    /// Number of parallel threads (0 = auto)
24    pub threads: usize,
25
26    /// Paths to exclude (glob patterns)
27    pub exclude_patterns: Vec<String>,
28
29    /// Follow symbolic links
30    pub follow_symlinks: bool,
31
32    /// Optional persistent file-metadata cache.
33    pub cache: Option<ScanCacheOptions>,
34}
35
36/// Linux virtual filesystem paths that should be excluded by default.
37/// These can report incorrect/huge sizes and cause scanning issues.
38pub const LINUX_VIRTUAL_FS_PATHS: &[&str] = &["/proc", "/dev", "/sys", "/run"];
39
40impl ScanOptions {
41    /// Check if a path should be excluded based on Linux virtual filesystem paths
42    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    /// Create a new ScanOptions with default values
50    pub fn new() -> Self {
51        Self::default()
52    }
53
54    /// Set maximum recursion depth
55    pub fn with_max_depth(mut self, depth: usize) -> Self {
56        self.max_depth = Some(depth);
57        self
58    }
59
60    /// Set whether to include hidden files
61    pub fn with_hidden(mut self, include: bool) -> Self {
62        self.include_hidden = include;
63        self
64    }
65
66    /// Set whether to stay on the same filesystem
67    pub fn with_one_file_system(mut self, enabled: bool) -> Self {
68        self.one_file_system = enabled;
69        self
70    }
71
72    /// Set number of parallel threads
73    pub fn with_threads(mut self, threads: usize) -> Self {
74        self.threads = threads;
75        self
76    }
77
78    /// Set exclusion patterns
79    pub fn with_exclude(mut self, patterns: Vec<String>) -> Self {
80        self.exclude_patterns = patterns;
81        self
82    }
83
84    /// Set whether to follow symbolic links
85    pub fn with_follow_symlinks(mut self, follow: bool) -> Self {
86        self.follow_symlinks = follow;
87        self
88    }
89
90    /// Enable persistent metadata caching at `path`.
91    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    /// Default per-user cache location.
100    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        // Should detect virtual filesystem paths
175        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        // Should not detect regular paths
191        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        // Similar string prefixes are ordinary directory names, not virtual
198        // filesystem descendants.
199        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}