fsmon 0.2.5

Lightweight High-Performance File System Change Tracking Tool
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
use anyhow::Result;
use clap::{Parser, Subcommand};
use fsmon::help::{self, HelpTopic};
use std::path::PathBuf;

mod commands;

#[derive(Parser)]
#[command(name = "fsmon")]
#[command(author = "fsmon contributors")]
#[command(version = env!("CARGO_PKG_VERSION"))]
#[command(about = help::about(HelpTopic::Root))]
#[command(after_help = help::after_help())]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
pub enum Commands {
    /// Run the fsmon daemon (requires sudo for fanotify)
    #[command(about = help::about(HelpTopic::Daemon), long_about = help::long_about(HelpTopic::Daemon))]
    Daemon,

    /// Add a path to the monitoring list
    #[command(about = help::about(HelpTopic::Add), long_about = help::long_about(HelpTopic::Add))]
    Add(AddArgs),

    /// Remove one or more paths from the monitoring list
    #[command(about = help::about(HelpTopic::Remove), long_about = help::long_about(HelpTopic::Remove))]
    Remove {
        /// Path(s) to remove. Multiple paths can be specified.
        paths: Vec<PathBuf>,
    },

    /// List all monitored paths with their configuration
    #[command(about = help::about(HelpTopic::Managed), long_about = help::long_about(HelpTopic::Managed))]
    Managed,

    /// Query historical file change events
    #[command(about = help::about(HelpTopic::Query), long_about = help::long_about(HelpTopic::Query))]
    Query(QueryArgs),

    /// Clean historical log files
    #[command(about = help::about(HelpTopic::Clean), long_about = help::long_about(HelpTopic::Clean))]
    Clean(CleanArgs),

    /// Initialize log and managed data directories
    #[command(about = help::about(HelpTopic::Init), long_about = help::long_about(HelpTopic::Init))]
    Init,

    /// Print the log directory path
    #[command(about = help::about(HelpTopic::Cd), long_about = help::long_about(HelpTopic::Cd))]
    Cd,

    /// List managed paths (one per line, for shell completion use)
    #[command(hide = true)]
    ListManagedPaths,
}

#[derive(Parser)]
pub struct AddArgs {
    /// Path to monitor
    pub path: PathBuf,

    /// Watch subdirectories recursively
    #[arg(short)]
    pub recursive: bool,

    /// Event types to monitor (repeatable; use "all" for all 14 types). Controls kernel mask.
    #[arg(short, long, value_name = "TYPE")]
    pub types: Vec<String>,

    /// Size filter with operator (required: >=, >, <=, <, =). e.g. >1MB, >=500KB, <100MB, =0
    #[arg(short, long, value_name = "SIZE")]
    pub size: Option<String>,

    /// Path glob patterns to exclude (repeatable, prefix ! to invert)
    #[arg(short, long, value_name = "PATTERN")]
    pub exclude: Vec<String>,

    /// Process names to exclude (glob, repeatable, prefix ! to invert)
    #[arg(long, value_name = "PATTERN")]
    pub exclude_cmd: Vec<String>,
}

#[derive(Parser)]
pub struct QueryArgs {
    /// Path(s) to query. Repeatable. Default: all.
    #[arg(short, long, value_name = "PATH")]
    pub path: Vec<PathBuf>,
    /// Time filter with operator (repeatable: >1h for since, <2026-05-01 for until)
    #[arg(short, long, value_name = "FILTER")]
    pub time: Vec<String>,
}

#[derive(Parser)]
pub struct CleanArgs {
    /// Path(s) to clean. Repeatable. Default: all.
    #[arg(short, long, value_name = "PATH")]
    pub path: Vec<PathBuf>,
    /// Time filter with operator (e.g. >30d — delete entries older than 30 days)
    #[arg(long, value_name = "FILTER")]
    pub time: Option<String>,
    /// Size limit for log file truncation with operator (e.g. >500MB, >=1GB)
    #[arg(short, long)]
    pub size: Option<String>,
    #[arg(short, long)]
    pub dry_run: bool,
}

fn main() -> Result<()> {
    let cli = Cli::parse();
    commands::run(cli.command)
}

#[cfg(test)]
mod tests {
    use super::*;
    use clap::Parser;

    // ---- AddArgs CLI parsing ----

    #[test]
    fn test_add_no_flags() {
        let args = AddArgs::try_parse_from(&["add", "/tmp"]).unwrap();
        assert_eq!(args.path, PathBuf::from("/tmp"));
        assert!(args.types.is_empty());
        assert!(args.exclude.is_empty());
        assert!(args.exclude_cmd.is_empty());
        assert!(!args.recursive);
        assert!(args.size.is_none());
    }

    #[test]
    fn test_add_types_long() {
        let args = AddArgs::try_parse_from(&[
            "add", "/tmp",
            "--types", "MODIFY", "--types", "CREATE",
        ]).unwrap();
        assert_eq!(args.types, vec!["MODIFY", "CREATE"]);
    }

    #[test]
    fn test_add_types_short() {
        let args = AddArgs::try_parse_from(&[
            "add", "/tmp",
            "-t", "MODIFY", "-t", "CREATE",
        ]).unwrap();
        assert_eq!(args.types, vec!["MODIFY", "CREATE"]);
    }

    #[test]
    fn test_add_types_all_long() {
        let args = AddArgs::try_parse_from(&["add", "/tmp", "--types", "all"]).unwrap();
        assert_eq!(args.types, vec!["all"]);
    }

    #[test]
    fn test_add_types_mixed() {
        let args = AddArgs::try_parse_from(&[
            "add", "/tmp",
            "-t", "MODIFY", "--types", "CREATE",
        ]).unwrap();
        assert_eq!(args.types, vec!["MODIFY", "CREATE"]);
    }

    #[test]
    fn test_add_exclude_long() {
        let args = AddArgs::try_parse_from(&[
            "add", "/tmp",
            "--exclude", "*.tmp", "--exclude", "*.log",
        ]).unwrap();
        assert_eq!(args.exclude, vec!["*.tmp", "*.log"]);
    }

    #[test]
    fn test_add_exclude_short() {
        let args = AddArgs::try_parse_from(&[
            "add", "/tmp",
            "-e", "*.tmp", "-e", "*.log",
        ]).unwrap();
        assert_eq!(args.exclude, vec!["*.tmp", "*.log"]);
    }

    #[test]
    fn test_add_exclude_invert() {
        let args = AddArgs::try_parse_from(&[
            "add", "/tmp",
            "--exclude", "!*.py",
        ]).unwrap();
        assert_eq!(args.exclude, vec!["!*.py"]);
    }

    #[test]
    fn test_add_exclude_cmd_long() {
        let args = AddArgs::try_parse_from(&[
            "add", "/tmp",
            "--exclude-cmd", "rsync", "--exclude-cmd", "apt",
        ]).unwrap();
        assert_eq!(args.exclude_cmd, vec!["rsync", "apt"]);
    }

    #[test]
    fn test_add_exclude_cmd_short_not_applicable() {
        let args = AddArgs::try_parse_from(&[
            "add", "/tmp",
            "--exclude-cmd", "nginx",
        ]).unwrap();
        assert_eq!(args.exclude_cmd, vec!["nginx"]);
    }

    #[test]
    fn test_add_recursive_short() {
        let args = AddArgs::try_parse_from(&["add", "/tmp", "-r"]).unwrap();
        assert!(args.recursive);
    }

    #[test]
    fn test_add_size_short() {
        let args = AddArgs::try_parse_from(&["add", "/tmp", "-s", "1GB"]).unwrap();
        assert_eq!(args.size, Some("1GB".into()));
    }

    #[test]
    fn test_add_size_long() {
        let args = AddArgs::try_parse_from(&["add", "/tmp", "--size", "100MB"]).unwrap();
        assert_eq!(args.size, Some("100MB".into()));
    }

    #[test]
    fn test_add_size_with_operator() {
        let args = AddArgs::try_parse_from(&["add", "/tmp", "-s", ">=1MB"]).unwrap();
        assert_eq!(args.size, Some(">=1MB".into()));

        let args = AddArgs::try_parse_from(&["add", "/tmp", "--size", "<500KB"]).unwrap();
        assert_eq!(args.size, Some("<500KB".into()));

        let args = AddArgs::try_parse_from(&["add", "/tmp", "-s", "=0"]).unwrap();
        assert_eq!(args.size, Some("=0".into()));
    }

    #[test]
    fn test_add_size_decimal_and_negative() {
        let args = AddArgs::try_parse_from(&["add", "/tmp", "-s", "1.5KB"]).unwrap();
        assert_eq!(args.size, Some("1.5KB".into()));

        let args = AddArgs::try_parse_from(&["add", "/tmp", "--size", ">-1KB"]).unwrap();
        assert_eq!(args.size, Some(">-1KB".into()));
    }

    #[test]
    fn test_add_size_case_insensitive_unit() {
        let args = AddArgs::try_parse_from(&["add", "/tmp", "-s", "1mb"]).unwrap();
        assert_eq!(args.size, Some("1mb".into()));

        let args = AddArgs::try_parse_from(&["add", "/tmp", "--size", "100Kb"]).unwrap();
        assert_eq!(args.size, Some("100Kb".into()));
    }

    #[test]
    fn test_add_all_flags() {
        let args = AddArgs::try_parse_from(&[
            "add", "/tmp",
            "-r",
            "-t", "MODIFY", "--types", "CREATE",
            "-e", "*.tmp", "--exclude", "*.log",
            "--exclude-cmd", "rsync",
            "-s", "1KB",
        ]).unwrap();
        assert_eq!(args.path, PathBuf::from("/tmp"));
        assert!(args.recursive);
        assert_eq!(args.types, vec!["MODIFY", "CREATE"]);
        assert_eq!(args.exclude, vec!["*.tmp", "*.log"]);
        assert_eq!(args.exclude_cmd, vec!["rsync"]);
        assert_eq!(args.size, Some("1KB".into()));
    }

    // ---- QueryArgs CLI parsing ----

    #[test]
    fn test_query_no_flags() {
        let args = QueryArgs::try_parse_from(&["query"]).unwrap();
        assert!(args.path.is_empty());
        assert!(args.time.is_empty());
    }

    #[test]
    fn test_query_path_long() {
        let args = QueryArgs::try_parse_from(&[
            "query",
            "--path", "/tmp", "--path", "/home",
        ]).unwrap();
        assert_eq!(args.path, vec![PathBuf::from("/tmp"), PathBuf::from("/home")]);
    }

    #[test]
    fn test_query_path_short() {
        let args = QueryArgs::try_parse_from(&[
            "query",
            "-p", "/tmp", "-p", "/home",
        ]).unwrap();
        assert_eq!(args.path, vec![PathBuf::from("/tmp"), PathBuf::from("/home")]);
    }

    #[test]
    fn test_query_time_since() {
        let args = QueryArgs::try_parse_from(&["query", "-t", ">1h"]).unwrap();
        assert_eq!(args.time, vec![">1h".to_string()]);
    }

    #[test]
    fn test_query_time_until() {
        let args = QueryArgs::try_parse_from(&["query", "--time", "<2026-05-01"]).unwrap();
        assert_eq!(args.time, vec!["<2026-05-01".to_string()]);
    }

    #[test]
    fn test_query_time_repeatable() {
        let args = QueryArgs::try_parse_from(&[
            "query",
            "--time", ">1h", "--time", "<now",
        ]).unwrap();
        assert_eq!(args.time, vec![">1h".to_string(), "<now".to_string()]);
    }

    #[test]
    fn test_query_time_with_path() {
        let args = QueryArgs::try_parse_from(&[
            "query",
            "-p", "/tmp",
            "-t", ">1h",
        ]).unwrap();
        assert_eq!(args.path, vec![PathBuf::from("/tmp")]);
        assert_eq!(args.time, vec![">1h".to_string()]);
    }

    // ---- CleanArgs CLI parsing ----

    #[test]
    fn test_clean_no_flags() {
        let args = CleanArgs::try_parse_from(&["clean"]).unwrap();
        assert!(args.path.is_empty());
        assert!(args.time.is_none());
        assert!(args.size.is_none());
        assert!(!args.dry_run);
    }

    #[test]
    fn test_clean_path_long() {
        let args = CleanArgs::try_parse_from(&[
            "clean",
            "--path", "/tmp", "--path", "/var/log",
        ]).unwrap();
        assert_eq!(args.path, vec![PathBuf::from("/tmp"), PathBuf::from("/var/log")]);
    }

    #[test]
    fn test_clean_path_short() {
        let args = CleanArgs::try_parse_from(&[
            "clean",
            "-p", "/tmp", "-p", "/var/log",
        ]).unwrap();
        assert_eq!(args.path, vec![PathBuf::from("/tmp"), PathBuf::from("/var/log")]);
    }

    #[test]
    fn test_clean_time() {
        let args = CleanArgs::try_parse_from(&["clean", "--time", ">30d"]).unwrap();
        assert_eq!(args.time, Some(">30d".into()));
    }

    #[test]
    fn test_clean_size_short() {
        let args = CleanArgs::try_parse_from(&["clean", "-s", "500MB"]).unwrap();
        assert_eq!(args.size, Some("500MB".into()));
    }

    #[test]
    fn test_clean_size_long() {
        let args = CleanArgs::try_parse_from(&["clean", "--size", ">=1GB"]).unwrap();
        assert_eq!(args.size, Some(">=1GB".into()));
    }

    #[test]
    fn test_clean_dry_run_long() {
        let args = CleanArgs::try_parse_from(&["clean", "--dry-run"]).unwrap();
        assert!(args.dry_run);
    }

    #[test]
    fn test_clean_all_flags() {
        let args = CleanArgs::try_parse_from(&[
            "clean",
            "-p", "/tmp", "--path", "/var/log",
            "--time", ">30d",
            "-s", ">=100MB",
            "--dry-run",
        ]).unwrap();
        assert_eq!(args.path, vec![PathBuf::from("/tmp"), PathBuf::from("/var/log")]);
        assert_eq!(args.time, Some(">30d".into()));
        assert_eq!(args.size, Some(">=100MB".into()));
        assert!(args.dry_run);
    }

    // ---- Remove command (positional paths) ----

    #[test]
    fn test_remove_single_path() {
        let cli = Cli::try_parse_from(&["fsmon", "remove", "/tmp"]).unwrap();
        let paths = match cli.command {
            Commands::Remove { paths } => paths,
            _ => panic!("expected Remove"),
        };
        assert_eq!(paths, vec![PathBuf::from("/tmp")]);
    }

    #[test]
    fn test_remove_multiple_paths() {
        let cli = Cli::try_parse_from(&["fsmon", "remove", "/tmp", "/home", "/var/log"]).unwrap();
        let paths = match cli.command {
            Commands::Remove { paths } => paths,
            _ => panic!("expected Remove"),
        };
        assert_eq!(paths, vec![
            PathBuf::from("/tmp"),
            PathBuf::from("/home"),
            PathBuf::from("/var/log"),
        ]);
    }

    #[test]
    fn test_remove_empty_ok() {
        let cli = Cli::try_parse_from(&["fsmon", "remove"]).unwrap();
        let paths = match cli.command {
            Commands::Remove { paths } => paths,
            _ => panic!("expected Remove"),
        };
        assert!(paths.is_empty());
    }
}