ntfs-mac-core 0.1.6

Core library for ntfs-mac: subprocess wrappers around ntfs-3g, diskutil, hdiutil, mkntfs, ntfsfix.
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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 kodephp contributors

//! Auto-mount daemon.
//!
//! Watches for newly inserted NTFS volumes and mounts them
//! automatically — the core Mounty-like feature. Uses periodic
//! polling of `diskutil list -plist` (portable, no fseventsd
//! dependency). Can be installed as a macOS LaunchAgent for
//! login-time auto-start.
//!
//! # Design
//!
//! - Polls every 3 seconds (configurable via `DaemonOptions`).
//! - Compares current volumes against a known-state snapshot.
//! - Only mounts volumes that are unmounted and newly appeared.
//! - Logs to stderr (daemon-friendly; stdout stays clean).
//! - Graceful shutdown on SIGINT / SIGTERM.
//!
//! # LaunchAgent
//!
//! Installs `~/Library/LaunchAgents/com.kodephp.ntfs-mac.plist`
//! that runs `ntfs-mac daemon` at login.

use std::collections::HashSet;
use std::path::PathBuf;
use std::time::Duration;

use serde::{Deserialize, Serialize};

use crate::error::{ConfigError, Error, Result};
use crate::{Config, device, mount};

/// Options for the daemon.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DaemonOptions {
    /// Poll interval in seconds.
    #[serde(default = "default_interval")]
    pub interval_secs: u64,
    /// Max consecutive poll errors before giving up.
    #[serde(default = "default_max_errors")]
    pub max_consecutive_errors: u32,
    /// Auto-mount only external volumes.
    #[serde(default = "default_true")]
    pub external_only: bool,
}

impl Default for DaemonOptions {
    fn default() -> Self {
        Self {
            interval_secs: default_interval(),
            max_consecutive_errors: default_max_errors(),
            external_only: default_true(),
        }
    }
}

fn default_interval() -> u64 {
    3
}

fn default_max_errors() -> u32 {
    10
}

fn default_true() -> bool {
    true
}

/// Result of a single daemon tick.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TickResult {
    /// Volumes that were mounted this tick.
    pub mounted: Vec<String>,
    /// Total volumes seen.
    pub total_volumes: usize,
    /// Total mounted (pre-existing + newly mounted).
    pub total_mounted: usize,
    /// Errors encountered.
    pub errors: Vec<String>,
}

/// Run the daemon until interrupted.
///
/// This function blocks. Call from a dedicated thread or process.
/// Installs SIGINT/SIGTERM handlers; returns `Ok(())` on graceful
/// shutdown, `Err(Error::Cancelled)` if the signal was received.
pub fn run(cfg: &Config, opts: &DaemonOptions) -> Result<()> {
    crate::hardening::install_signal_handlers();

    let mut known: HashSet<String> = HashSet::new();
    let mut consecutive_errors: u32 = 0;
    let mut last_tick = std::time::Instant::now();

    log_info(&format!(
        "ntfs-mac daemon started (interval={}s, external_only={})",
        opts.interval_secs, opts.external_only
    ));
    if let Ok(volumes) = device::list_volumes() {
        known = collect_unmounted_ids(&volumes);
        log_info(&format!(
            "initial state: {} volumes, {} unmounted",
            volumes.len(),
            known.len()
        ));
    }

    // Poll interval is short (500ms) so SIGINT/SIGTERM is noticed
    // quickly; the actual tick cadence is controlled by `last_tick`.
    let poll_interval = Duration::from_millis(500);

    loop {
        if crate::hardening::should_exit() {
            log_info("shutdown signal received, exiting");
            return Err(Error::Cancelled);
        }

        std::thread::sleep(poll_interval);

        if last_tick.elapsed() < Duration::from_secs(opts.interval_secs) {
            continue;
        }
        last_tick = std::time::Instant::now();

        match device::list_volumes() {
            Ok(volumes) => {
                consecutive_errors = 0;
                let result = tick(&volumes, &mut known, cfg, opts);
                if !result.mounted.is_empty() {
                    log_info(&format!("mounted: {}", result.mounted.join(", ")));
                }
            }
            Err(e) => {
                consecutive_errors += 1;
                log_warn(&format!("poll error ({}): {e}", consecutive_errors));
                if consecutive_errors >= opts.max_consecutive_errors {
                    return Err(Error::CommandFailed {
                        cmd: "daemon".into(),
                        status: -1,
                        stderr: format!("too many consecutive errors: {consecutive_errors}"),
                        io: None,
                    });
                }
            }
        }
    }
}

/// Single poll tick: detect new volumes and mount them.
fn tick(
    volumes: &[crate::Volume],
    known: &mut HashSet<String>,
    cfg: &Config,
    opts: &DaemonOptions,
) -> TickResult {
    let mut mounted = Vec::new();
    let mut errors = Vec::new();

    for vol in volumes {
        if vol.mounted {
            continue;
        }
        if opts.external_only && vol.location != "external" {
            continue;
        }
        if known.contains(&vol.device_identifier) {
            continue;
        }

        log_info(&format!(
            "detected new volume: {} ({})",
            vol.display_label(),
            vol.size_pretty
        ));
        let result = mount::mount(vol, &mount::MountOptions::default(), cfg);
        match result {
            Ok(mp) => {
                mounted.push(format!("{} -> {}", vol.display_label(), mp));
                log_info(&format!("mounted at {mp}"));
            }
            Err(e) => {
                errors.push(format!("{}: {e}", vol.display_label()));
                log_warn(&format!("mount failed for {}: {e}", vol.display_label()));
            }
        }
    }

    *known = collect_unmounted_ids(volumes);

    let total_mounted = volumes.iter().filter(|v| v.mounted).count() + mounted.len();
    TickResult {
        mounted,
        total_volumes: volumes.len(),
        total_mounted,
        errors,
    }
}

/// Collect identifiers of unmounted volumes.
fn collect_unmounted_ids(volumes: &[crate::Volume]) -> HashSet<String> {
    volumes
        .iter()
        .filter(|v| !v.mounted)
        .map(|v| v.device_identifier.clone())
        .collect()
}

// --- LaunchAgent ---

const LAUNCHAGENT_LABEL: &str = "com.kodephp.ntfs-mac";
const LAUNCHAGENT_PLIST_NAME: &str = "com.kodephp.ntfs-mac.plist";

/// Outcome of installing the LaunchAgent.
#[derive(Debug, Clone)]
pub struct LaunchAgentInstall {
    /// Path of the written plist.
    pub plist_path: PathBuf,
    /// Whether the agent was successfully loaded into the user's
    /// launchd session (`launchctl bootstrap`/`load`).
    pub loaded: bool,
    /// Diagnostic when `loaded` is `false` (the plist is still
    /// installed and will start at next login).
    pub load_error: Option<String>,
}

/// Numeric UID of the current user, via `id -u` (std has no uid API).
fn user_uid() -> Option<String> {
    use crate::runner::{RunOptions, run};
    let out = run("id", &["-u"], &RunOptions::default()).ok()?;
    if out.success() {
        let s = out.stdout.trim().to_string();
        if s.chars().all(|c| c.is_ascii_digit()) && !s.is_empty() {
            return Some(s);
        }
    }
    None
}

/// Load the agent into the current user's launchd session.
///
/// Prefers the modern `launchctl bootstrap gui/<uid> <plist>`; falls
/// back to the legacy `launchctl load <plist>`.
///
/// The legacy `load` subcommand can print "Load failed" and still exit
/// with status 0, so the exit code alone proves nothing — success is
/// always verified with `launchctl print`.
fn load_launchagent(plist_path: &std::path::Path) -> Result<()> {
    use crate::runner::{RunOptions, run};
    let plist = plist_path.to_string_lossy().to_string();
    let mut failures: Vec<String> = Vec::new();

    if let Some(uid) = user_uid() {
        if let Ok(out) = run(
            "launchctl",
            &["bootstrap", &format!("gui/{uid}"), &plist],
            &RunOptions::default(),
        ) {
            if out.success() && is_launchagent_loaded() {
                return Ok(());
            }
            let msg = out.stderr.trim();
            if !msg.is_empty() {
                failures.push(format!("bootstrap: {msg}"));
            }
        }
        // bootstrap may have failed because the job is already loaded;
        // check before falling back to the legacy path.
        if is_launchagent_loaded() {
            return Ok(());
        }
    }

    if let Ok(out) = run("launchctl", &["load", &plist], &RunOptions::default()) {
        if is_launchagent_loaded() {
            return Ok(());
        }
        let msg = out.stderr.trim();
        if !msg.is_empty() {
            failures.push(format!("load: {msg}"));
        }
    }

    Err(Error::CommandFailed {
        cmd: "launchctl".into(),
        status: -1,
        stderr: if failures.is_empty() {
            "could not verify the agent in launchd (`launchctl print` failed)".into()
        } else {
            failures.join("; ")
        },
        io: None,
    })
}

/// Best-effort unload. Never fails the caller: if the agent is not
/// loaded there is nothing to do, and a failed bootout must not stop
/// the plist removal.
fn unload_launchagent(plist_path: &std::path::Path) {
    use crate::runner::{RunOptions, run};
    let plist = plist_path.to_string_lossy().to_string();
    if let Some(uid) = user_uid() {
        let _ = run(
            "launchctl",
            &["bootout", &format!("gui/{uid}/{LAUNCHAGENT_LABEL}")],
            &RunOptions::default(),
        );
    }
    let _ = run("launchctl", &["unload", &plist], &RunOptions::default());
}

/// Whether launchd currently has the agent loaded.
pub fn is_launchagent_loaded() -> bool {
    use crate::runner::{RunOptions, run};
    if let Some(uid) = user_uid() {
        if let Ok(out) = run(
            "launchctl",
            &["print", &format!("gui/{uid}/{LAUNCHAGENT_LABEL}")],
            &RunOptions::default(),
        ) {
            return out.success();
        }
    }
    false
}

/// Install the daemon as a macOS LaunchAgent and load it into the
/// current launchd session so it starts immediately (not just at the
/// next login).
pub fn install_launchagent(binary_path: &std::path::Path) -> Result<LaunchAgentInstall> {
    let home = dirs::home_dir().ok_or_else(|| {
        Error::Config(ConfigError::Read {
            path: PathBuf::from("~"),
            reason: "cannot determine home directory".into(),
        })
    })?;
    let agent_dir = home.join("Library").join("LaunchAgents");
    std::fs::create_dir_all(&agent_dir).map_err(|e| {
        Error::Config(ConfigError::Write {
            path: agent_dir.clone(),
            reason: format!("cannot create {}: {e}", agent_dir.display()),
        })
    })?;
    // launchd opens the log files itself; the directory must exist.
    let logs_dir = home.join("Library").join("Logs");
    let _ = std::fs::create_dir_all(&logs_dir);

    let plist_path = agent_dir.join(LAUNCHAGENT_PLIST_NAME);
    let plist = generate_launchagent_plist(binary_path, &home);
    std::fs::write(&plist_path, plist).map_err(|e| {
        Error::Config(ConfigError::Write {
            path: plist_path.clone(),
            reason: format!("cannot write {}: {e}", plist_path.display()),
        })
    })?;

    log_info(&format!("LaunchAgent installed: {}", plist_path.display()));

    // Replace any previously loaded instance so reinstall is idempotent.
    unload_launchagent(&plist_path);
    match load_launchagent(&plist_path) {
        Ok(()) => Ok(LaunchAgentInstall {
            plist_path,
            loaded: true,
            load_error: None,
        }),
        Err(e) => Ok(LaunchAgentInstall {
            plist_path,
            loaded: false,
            load_error: Some(e.to_string()),
        }),
    }
}

/// Uninstall the LaunchAgent: unload it first (stopping the daemon),
/// then remove the plist.
pub fn uninstall_launchagent() -> Result<()> {
    let home = dirs::home_dir().ok_or_else(|| {
        Error::Config(ConfigError::Read {
            path: PathBuf::from("~"),
            reason: "cannot determine home directory".into(),
        })
    })?;
    let plist_path = home
        .join("Library")
        .join("LaunchAgents")
        .join(LAUNCHAGENT_PLIST_NAME);

    if plist_path.exists() {
        unload_launchagent(&plist_path);
        std::fs::remove_file(&plist_path).map_err(|e| {
            Error::Config(ConfigError::Write {
                path: plist_path.clone(),
                reason: format!("cannot remove {}: {e}", plist_path.display()),
            })
        })?;
        log_info(&format!("LaunchAgent removed: {}", plist_path.display()));
    } else {
        log_info("LaunchAgent not installed");
    }
    Ok(())
}

/// Check if the LaunchAgent is installed.
pub fn is_launchagent_installed() -> bool {
    let home = dirs::home_dir();
    match home {
        Some(h) => h
            .join("Library")
            .join("LaunchAgents")
            .join(LAUNCHAGENT_PLIST_NAME)
            .exists(),
        None => false,
    }
}

/// Generate the LaunchAgent plist content.
///
/// launchd does NOT expand `~` in `StandardOutPath`, `StandardErrorPath`
/// or `WorkingDirectory` — the values must be absolute paths, expanded
/// against the user's home at generation time.
fn generate_launchagent_plist(binary_path: &std::path::Path, home: &std::path::Path) -> String {
    let log_path = home.join("Library").join("Logs").join("ntfs-mac.out.log");
    let err_log_path = home.join("Library").join("Logs").join("ntfs-mac.err.log");
    format!(
        r#"<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>{label}</string>
    <key>ProgramArguments</key>
    <array>
        <string>{binary}</string>
        <string>daemon</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <false/>
    <key>StandardOutPath</key>
    <string>{out_log}</string>
    <key>StandardErrorPath</key>
    <string>{err_log}</string>
    <key>WorkingDirectory</key>
    <string>{home}</string>
</dict>
</plist>
"#,
        label = LAUNCHAGENT_LABEL,
        binary = binary_path.display(),
        out_log = log_path.display(),
        err_log = err_log_path.display(),
        home = home.display(),
    )
}

// --- Logging (uses tracing for consistent structured output) ---

fn log_info(msg: &str) {
    tracing::info!(target: "ntfs_mac_core::daemon", "{msg}");
}

fn log_warn(msg: &str) {
    tracing::warn!(target: "ntfs_mac_core::daemon", "{msg}");
}

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

    #[test]
    fn daemon_options_defaults() {
        let opts = DaemonOptions::default();
        assert_eq!(opts.interval_secs, 3);
        assert_eq!(opts.max_consecutive_errors, 10);
        assert!(opts.external_only);
    }

    #[test]
    fn tick_mounts_new_volume() {
        let cfg = Config::default();
        let opts = DaemonOptions {
            external_only: false,
            ..Default::default()
        };
        let volumes = vec![crate::Volume {
            device_identifier: "disk2s2".into(),
            volume_name: "TestVol".into(),
            media_type: "com.microsoft.ntfs".into(),
            uuid: None,
            size_bytes: 1000000000,
            mounted: false,
            mount_point: None,
            parent_disk: Some("disk2".into()),
            size_pretty: "0.9 GiB".into(),
            location: "external".into(),
            contents: Some("GUID_partition_scheme".into()),
        }];
        let mut known: HashSet<String> = HashSet::new();
        let result = tick(&volumes, &mut known, &cfg, &opts);
        assert_eq!(result.total_volumes, 1);
        assert!(!result.errors.is_empty() || !result.mounted.is_empty());
    }

    #[test]
    fn tick_skips_already_mounted() {
        let cfg = Config::default();
        let opts = DaemonOptions {
            external_only: false,
            ..Default::default()
        };
        let volumes = vec![crate::Volume {
            device_identifier: "disk2s2".into(),
            volume_name: "Mounted".into(),
            media_type: "com.microsoft.ntfs".into(),
            uuid: None,
            size_bytes: 0,
            mounted: true,
            mount_point: Some("/Volumes/Mounted".into()),
            parent_disk: Some("disk2".into()),
            size_pretty: "0 B".into(),
            location: "external".into(),
            contents: None,
        }];
        let mut known: HashSet<String> = HashSet::new();
        let result = tick(&volumes, &mut known, &cfg, &opts);
        assert!(result.mounted.is_empty());
        assert!(result.errors.is_empty());
        assert_eq!(result.total_mounted, 1);
    }

    #[test]
    fn tick_skips_internal_when_external_only() {
        let cfg = Config::default();
        let opts = DaemonOptions {
            external_only: true,
            ..Default::default()
        };
        let volumes = vec![crate::Volume {
            device_identifier: "disk0s2".into(),
            volume_name: "Internal".into(),
            media_type: "com.microsoft.ntfs".into(),
            uuid: None,
            size_bytes: 0,
            mounted: false,
            mount_point: None,
            parent_disk: Some("disk0".into()),
            size_pretty: "0 B".into(),
            location: "internal".into(),
            contents: None,
        }];
        let mut known: HashSet<String> = HashSet::new();
        let result = tick(&volumes, &mut known, &cfg, &opts);
        assert!(result.mounted.is_empty());
        assert!(result.errors.is_empty());
    }

    #[test]
    fn launchagent_plist_generation() {
        let home = std::path::Path::new("/Users/tester");
        let plist =
            generate_launchagent_plist(std::path::Path::new("/usr/local/bin/ntfs-mac"), home);
        assert!(plist.contains("com.kodephp.ntfs-mac"));
        assert!(plist.contains("/usr/local/bin/ntfs-mac"));
        assert!(plist.contains("RunAtLoad"));
        // launchd does not expand `~`: all paths must be absolute.
        assert!(
            !plist.contains(">~<"),
            "plist must not contain literal ~ paths"
        );
        assert!(plist.contains("/Users/tester/Library/Logs/ntfs-mac.out.log"));
        assert!(plist.contains("/Users/tester/Library/Logs/ntfs-mac.err.log"));
        assert!(plist.contains("<string>/Users/tester</string>"));
    }

    #[test]
    fn user_uid_is_numeric() {
        // Not a hard requirement in exotic sandboxes, but on any real
        // macOS box `id -u` works and returns digits.
        if let Some(uid) = user_uid() {
            assert!(!uid.is_empty());
            assert!(uid.chars().all(|c| c.is_ascii_digit()));
        }
    }

    #[test]
    fn test_collect_unmounted_ids() {
        let volumes = vec![
            crate::Volume {
                device_identifier: "disk2s2".into(),
                volume_name: "A".into(),
                media_type: "com.microsoft.ntfs".into(),
                uuid: None,
                size_bytes: 0,
                mounted: false,
                mount_point: None,
                parent_disk: None,
                size_pretty: "0 B".into(),
                location: "external".into(),
                contents: None,
            },
            crate::Volume {
                device_identifier: "disk3s2".into(),
                volume_name: "B".into(),
                media_type: "com.microsoft.ntfs".into(),
                uuid: None,
                size_bytes: 0,
                mounted: true,
                mount_point: Some("/Volumes/B".into()),
                parent_disk: None,
                size_pretty: "0 B".into(),
                location: "external".into(),
                contents: None,
            },
        ];
        let ids = collect_unmounted_ids(&volumes);
        assert!(ids.contains("disk2s2"));
        assert!(!ids.contains("disk3s2"));
        assert_eq!(ids.len(), 1);
    }
}