fsmon 0.2.0

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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
use anyhow::{Context, Result, bail};
use clap::{Parser, Subcommand};
use fsmon::config::Config;
use fsmon::help::{self, HelpTopic};
use fsmon::monitor::{Monitor, PathOptions};
use fsmon::query::Query;
use fsmon::socket::{self, SocketCmd};
use fsmon::managed::{PathEntry, Managed};
use fsmon::utils::parse_size;
use fsmon::{DEFAULT_KEEP_DAYS, EventType, clean_logs};
use std::fs;
use std::path::{Path, PathBuf};

#[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)]
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 a path from the monitoring list
    #[command(about = help::about(HelpTopic::Remove), long_about = help::long_about(HelpTopic::Remove))]
    Remove { path: 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),

    /// Generate a default configuration file
    #[command(about = help::about(HelpTopic::Generate), long_about = help::long_about(HelpTopic::Generate))]
    Generate {
        /// Overwrite existing configuration file if it exists
        #[arg(short, long)]
        force: bool,
    },
}

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

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

    /// Only monitor specified operation types, comma-separated
    #[arg(short, long, value_name = "TYPES")]
    types: Option<String>,

    /// Only record events with size change >= specified value
    #[arg(short = 'm', long, value_name = "SIZE")]
    min_size: Option<String>,

    /// Paths to exclude from monitoring (wildcards)
    #[arg(short = 'e', long, value_name = "PATTERN")]
    exclude: Option<String>,

    /// Process names to exclude (glob, e.g. "rsync|apt")
    #[arg(long, value_name = "PATTERN")]
    exclude_cmd: Option<String>,

    /// Only capture events from these process names (glob)
    #[arg(long, value_name = "PATTERN")]
    only_cmd: Option<String>,

    /// Capture all 14 fanotify events
    #[arg(long)]
    all_events: bool,
}

#[derive(Parser)]
struct QueryArgs {
    /// Path(s) to query. Repeatable. Default: all.
    #[arg(short, long, value_name = "PATH")]
    path: Vec<PathBuf>,
    #[arg(short = 'S', long)]
    since: Option<String>,
    #[arg(short = 'U', long)]
    until: Option<String>,
}

#[derive(Parser)]
struct CleanArgs {
    /// Path(s) to clean. Repeatable. Default: all.
    #[arg(short, long, value_name = "PATH")]
    path: Vec<PathBuf>,
    #[arg(short, long)]
    keep_days: Option<u32>,
    #[arg(short = 'm', long)]
    max_size: Option<String>,
    #[arg(short, long)]
    dry_run: bool,
}

#[tokio::main]
async fn main() -> Result<()> {
    let cli = Cli::parse();

    match cli.command {
        Commands::Daemon => cmd_daemon().await?,
        Commands::Add(args) => cmd_add(args)?,
        Commands::Remove { path } => cmd_remove(path)?,
        Commands::Managed => cmd_managed()?,
        Commands::Query(args) => cmd_query(args).await?,
        Commands::Clean(args) => cmd_clean(args).await?,
        Commands::Generate { force } => cmd_generate(force)?,
    }

    Ok(())
}

async fn cmd_daemon() -> Result<()> {
    // Auto-generate config if it doesn't exist
    let config_path = Config::path();
    if !config_path.exists() {
        eprintln!(
            "Config not found at {}, generating default config...",
            config_path.display()
        );
        Config::generate_default()?;
        eprintln!("Default config generated at {}", config_path.display());
    }

    let mut cfg = Config::load()?;
    cfg.resolve_paths()?;

    eprintln!("Config loaded:");
    eprintln!("  Managed path database:  {}", cfg.managed.file.display());
    eprintln!("  Event logs:     {}", cfg.logging.dir.display());
    eprintln!("  Command socket: {}", cfg.socket.path.display());

    let store = Managed::load(&cfg.managed.file)?;

    let socket_path = cfg.socket.path.clone();

    // Create parent directories for socket
    if socket_path.exists() {
        fs::remove_file(&socket_path)?;
    }
    if let Some(parent) = socket_path.parent() {
        fs::create_dir_all(parent)?;
    }

    let socket_listener = tokio::net::UnixListener::bind(&socket_path)
        .with_context(|| format!("Failed to bind socket at {}", socket_path.display()))?;

    // Set socket permissions to 0666 so any user can send commands
    set_socket_permissions(&socket_path)?;

    // Chown config file + store parent dir to the original user (daemon runs as root)
    let (uid, gid) = fsmon::config::resolve_uid_gid();
    chown_path(&config_path, uid, gid);
    if let Some(parent) = config_path.parent() {
        chown_path(parent, uid, gid);
    }
    if let Some(parent) = cfg.managed.file.parent() {
        chown_path(parent, uid, gid);
    }

    let paths_and_options = parse_path_entries(&store.entries)?;

    let store_path = cfg.managed.file.clone();
    let mut monitor = Monitor::new(
        paths_and_options,
        Some(cfg.logging.dir.clone()),
        Some(store_path),
        None,
        Some(socket_listener),
    )?;

    if !store.entries.is_empty() {
        eprintln!("Managed paths ({}):", store.entries.len());
        for entry in &store.entries {
            eprintln!("  {}", entry.path.display());
        }
    }

    monitor.run().await?;
    Ok(())
}

/// Chown a path to the given uid:gid (daemon runs as root, needs to give files back to the user).
fn chown_path(path: &Path, uid: u32, gid: u32) {
    if let Ok(cpath) = std::ffi::CString::new(path.to_string_lossy().as_bytes()) {
        unsafe {
            libc::chown(cpath.as_ptr(), uid, gid);
        }
    }
}

/// Set socket permissions to 0666 so non-root users can communicate with the daemon.
fn set_socket_permissions(path: &Path) -> Result<()> {
    use std::os::unix::fs::PermissionsExt;
    let perm = fs::Permissions::from_mode(0o666);
    fs::set_permissions(path, perm)
        .with_context(|| format!("Failed to set socket permissions on {}", path.display()))?;
    Ok(())
}

fn cmd_add(args: AddArgs) -> Result<()> {
    let mut cfg = Config::load()?;
    cfg.resolve_paths()?;

    // Local check: reject paths that would cause infinite recursion
    // Resolve tilde + symlinks to catch symlink-based conflicts
    let home = std::env::var("HOME").unwrap_or_else(|_| "/root".to_string());
    let expanded = fsmon::config::expand_tilde(&args.path, &home);
    let path = match expanded.canonicalize() {
        Ok(c) => c,
        Err(_) => {
            eprintln!("Note: path does not exist yet — will start monitoring when created.");
            expanded
        }
    };
    let log_dir_canon = cfg.logging.dir.canonicalize().unwrap_or_else(|_| cfg.logging.dir.clone());
    if log_dir_canon.starts_with(&path) {
        bail!(
            "Cannot monitor '{}': log directory '{}' is inside this path — \
             would cause infinite recursion on every log write.\n\
             Tip: use a different logging.dir or add a more specific path",
            args.path.display(),
            cfg.logging.dir.display()
        );
    }

    let mut store = Managed::load(&cfg.managed.file)?;
    let types: Option<Vec<String>> = args
        .types
        .map(|t| t.split(',').map(|s| s.trim().to_string()).collect());
    let min_size = args.min_size.clone();
    let exclude = args.exclude.clone();
    let exclude_cmd = args.exclude_cmd.clone();
    let only_cmd = args.only_cmd.clone();
    let recursive = if args.recursive { Some(true) } else { None };
    let all_events = if args.all_events { Some(true) } else { None };

    store.add_entry(PathEntry {
        path: path.clone(),
        recursive,
        types: types.clone(),
        min_size: min_size.clone(),
        exclude: exclude.clone(),
        exclude_cmd: exclude_cmd.clone(),
        only_cmd: only_cmd.clone(),
        all_events,
    });

    store.save(&cfg.managed.file)?;

    // Try live update via socket (non-fatal if fails)
    let socket_path = cfg.socket.path.clone();
    let result = socket::send_cmd(
        &socket_path,
        &SocketCmd {
            cmd: "add".to_string(),
            path: Some(path.clone()),
            recursive,
            types,
            min_size,
            exclude,
            exclude_cmd,
            only_cmd,
            all_events,
        },
    );

    match result {
        Ok(resp) if resp.ok => {
            println!("Path added: {}", path.display());
            println!("Daemon updated live");
        }
        Ok(resp) => {
            let is_permanent = resp.error_kind == Some(fsmon::socket::ErrorKind::Permanent);
            if is_permanent {
                // Revert store save — the error will persist after restart
                let mut store = Managed::load(&cfg.managed.file)?;
                store.remove_entry(&path);
                store.save(&cfg.managed.file)?;
                eprintln!("Error: {}", resp.error.unwrap_or_default());
            } else {
                println!("Path added: {}", path.display());
                eprintln!("Daemon error: {}", resp.error.unwrap_or_default());
                eprintln!("Path will be monitored after daemon restart");
            }
        }
        Err(_) => {
            println!("Path added: {}", path.display());
            eprintln!("Daemon not running — path will be monitored after daemon restart.");
        }
    }
    Ok(())
}

fn cmd_remove(raw: PathBuf) -> Result<()> {
    let mut cfg = Config::load()?;
    cfg.resolve_paths()?;

    // Normalize path: expand tilde + resolve symlinks/../.
    // Must match the normalization done by cmd_add, so store.remove_entry finds the entry.
    let home = std::env::var("HOME").unwrap_or_else(|_| "/root".to_string());
    let expanded = fsmon::config::expand_tilde(&raw, &home);
    let path = expanded.canonicalize().unwrap_or(expanded);

    let mut store = Managed::load(&cfg.managed.file)?;

    if !store.remove_entry(&path) {
        eprintln!("No monitored path: {}", path.display());
        std::process::exit(1);
    }

    store.save(&cfg.managed.file)?;
    println!("Path removed: {}", path.display());

    // Try live update via socket (non-fatal if fails)
    let socket_path = cfg.socket.path.clone();
    match socket::send_cmd(
        &socket_path,
        &SocketCmd {
            cmd: "remove".to_string(),
            path: Some(path),
            recursive: None,
            types: None,
            min_size: None,
            exclude: None,
            exclude_cmd: None,
            only_cmd: None,
            all_events: None,
        },
    ) {
        Ok(resp) if resp.ok => {
            println!("Daemon updated live");
        }
        Ok(resp) => {
            eprintln!("Daemon error: {}", resp.error.unwrap_or_default());
            eprintln!("Change will apply after daemon restart");
        }
        Err(_) => {
            // daemon not running — store already saved, change applies on restart
        }
    }
    Ok(())
}

fn cmd_managed() -> Result<()> {
    let mut cfg = Config::load()?;
    cfg.resolve_paths()?;
    let entries = Managed::load(&cfg.managed.file)
        .map(|s| s.entries)
        .unwrap_or_default();

    for entry in &entries {
        let types_str = entry
            .types
            .as_ref()
            .map(|v| v.join(","))
            .unwrap_or_else(|| "-".to_string());
        let recursive_str = if entry.recursive.unwrap_or(false) {
            "recursive"
        } else {
            "non-recursive"
        };
        let min_size_str = entry.min_size.as_deref().unwrap_or("-");
        let exclude_str = entry.exclude.as_deref().unwrap_or("-");
        let exclude_cmd_str = entry.exclude_cmd.as_deref().unwrap_or("-");
        let only_cmd_str = entry.only_cmd.as_deref().unwrap_or("-");
        let all_events_str = if entry.all_events.unwrap_or(false) {
            "all"
        } else {
            "filtered"
        };

        println!(
            "{} | types={} | {} | min_size={} | exclude-path={} | exclude-cmd={} | only-cmd={} | events={}",
            entry.path.display(),
            types_str,
            recursive_str,
            min_size_str,
            exclude_str,
            exclude_cmd_str,
            only_cmd_str,
            all_events_str,
        );
    }

    Ok(())
}

async fn cmd_query(args: QueryArgs) -> Result<()> {
    let mut cfg = Config::load()?;
    cfg.resolve_paths()?;

    let paths = if args.path.is_empty() {
        None
    } else {
        Some(args.path.clone())
    };

    let query = Query::new(
        cfg.logging.dir,
        paths,
        args.since,
        args.until,
    );

    query.execute().await?;
    Ok(())
}

fn cmd_generate(force: bool) -> Result<()> {
    let config_path = Config::path();
    if config_path.exists() && !force {
        eprintln!("Config already exists at {}", config_path.display());
        eprintln!("Use -f or --force to overwrite");
        std::process::exit(1);
    }
    Config::generate_default()?;
    println!("Default config generated at {}", config_path.display());
    Ok(())
}

async fn cmd_clean(args: CleanArgs) -> Result<()> {
    let mut cfg = Config::load()?;
    cfg.resolve_paths()?;

    let paths = if args.path.is_empty() {
        None
    } else {
        Some(args.path.clone())
    };
    let keep_days = args
        .keep_days
        .or(cfg.logging.keep_days)
        .unwrap_or(DEFAULT_KEEP_DAYS);
    let max_size_bytes = args
        .max_size
        .clone()
        .or(cfg.logging.max_size.clone())
        .map(|s| parse_size(&s))
        .transpose()?;
    clean_logs(
        &cfg.logging.dir,
        paths.as_deref(),
        keep_days,
        max_size_bytes,
        args.dry_run,
    )
    .await?;
    Ok(())
}

fn parse_path_entries(entries: &[PathEntry]) -> Result<Vec<(PathBuf, PathOptions)>> {
    let mut result = Vec::new();
    for entry in entries {
        let opts = parse_path_options(entry)?;
        result.push((entry.path.clone(), opts));
    }
    Ok(result)
}

fn parse_path_options(entry: &PathEntry) -> Result<PathOptions> {
    let min_size = entry.min_size.as_ref().map(|s| parse_size(s)).transpose()?;
    let event_types = entry
        .types
        .as_ref()
        .map(|v| {
            v.iter()
                .map(|s| s.parse::<EventType>())
                .collect::<std::result::Result<Vec<_>, _>>()
        })
        .transpose()
        .map_err(|e: String| anyhow::anyhow!(e))?;
    let exclude_regex = entry
        .exclude
        .as_ref()
        .map(|p| {
            let escaped = regex::escape(p);
            let pattern = escaped.replace("\\*", ".*");
            regex::Regex::new(&pattern).with_context(|| "invalid exclude pattern")
        })
        .transpose()?;
    let exclude_cmd_regex = entry
        .exclude_cmd
        .as_ref()
        .map(|p| {
            let pattern = p.replace("*", ".*");
            regex::Regex::new(&pattern).with_context(|| "invalid --exclude-cmd pattern")
        })
        .transpose()?;
    let only_cmd_regex = entry
        .only_cmd
        .as_ref()
        .map(|p| {
            let pattern = p.replace("*", ".*");
            regex::Regex::new(&pattern).with_context(|| "invalid --only-cmd pattern")
        })
        .transpose()?;
    Ok(PathOptions {
        min_size,
        event_types,
        exclude_regex,
        exclude_cmd_regex,
        only_cmd_regex,
        recursive: entry.recursive.unwrap_or(false),
        all_events: entry.all_events.unwrap_or(false),
    })
}