diskr 0.1.70

Lightweight terminal file explorer and disk/storage manager for macOS
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
//! FSEvents-backed cache freshness.
//!
//! The persistent size cache marks every directory stale at startup; its
//! stale-while-revalidate then rescans the oldest stale directories. That is
//! correct but does redundant work on directories that did not change. This
//! module replays the volume's filesystem events since the last launch and
//! tells the cache which directories actually changed, so unchanged trees keep
//! their proven-fresh sizes and only touched subtrees are revalidated.
//!
//! The persisted state is the volume UUID plus the last `FSEventStreamEventId`.
//! On launch, if the volume still matches, the historical event stream is
//! replayed since that id on a dedicated CoreFoundation run-loop thread, and
//! the set of changed directories is returned. Anything that makes the replay
//! untrustworthy — first run, a different/reformatted volume, dropped or
//! coalesced events (`MustScanSubDirs`/`UserDropped`/`KernelDropped`), a wrapped
//! id, a mount/unmount/root change, or a timeout — falls back to treating
//! everything as stale so the age-based revalidation can take over.

use std::ffi::CStr;
use std::os::raw::{c_char, c_void};
use std::os::unix::ffi::OsStrExt;
use std::os::unix::fs::MetadataExt;
use std::path::{Path, PathBuf};
use std::ptr;
use std::time::{Duration, Instant};

use anyhow::Result;

use crate::state;

const STATE_VERSION: u64 = 1;

/// Result of consulting FSEvents at startup. `is_stale` answers, per cached
/// directory, whether it must be revalidated.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Freshness {
    /// FSEvents could not be trusted; every cached directory is stale and falls
    /// through to age-based revalidation.
    All,
    /// FSEvents replayed cleanly: only directories whose subtree contains a
    /// changed path (and anything outside the watched root) are stale.
    Touched {
        watched_root: PathBuf,
        changed: Vec<PathBuf>,
    },
}

impl Freshness {
    /// Whether `dir`'s cached size should be treated as stale.
    pub fn is_stale(&self, dir: &Path) -> bool {
        match self {
            Freshness::All => true,
            Freshness::Touched {
                watched_root,
                changed,
            } => {
                // We only learned about changes under the watched root; treat
                // anything outside it as unknown, hence stale.
                if !dir.starts_with(watched_root) {
                    return true;
                }
                // A directory is stale when a change happened anywhere inside
                // its subtree — its recursive size may have moved.
                changed.iter().any(|change| change.starts_with(dir))
            }
        }
    }
}

/// Decide cache freshness for a fresh launch rooted at `root`, and persist the
/// current event id for next time. Never panics or blocks indefinitely; any
/// failure degrades to [`Freshness::All`].
pub fn plan_freshness(root: &Path) -> Freshness {
    let Ok(root) = root.canonicalize() else {
        return Freshness::All;
    };
    let Some(uuid) = volume_uuid(&root) else {
        return Freshness::All;
    };
    let now_id = current_event_id();
    let saved = load_state(&state_file());

    let outcome = match saved {
        Some(saved)
            if saved.volume_uuid == uuid
                && saved.last_event_id > 0
                && saved.last_event_id < now_id =>
        {
            replay_into_freshness(&root, saved.last_event_id)
        }
        Some(saved) if saved.volume_uuid == uuid && saved.last_event_id == now_id => {
            Freshness::Touched {
                watched_root: root.clone(),
                changed: Vec::new(),
            }
        }
        // First run on this volume, a different volume, or a non-advancing id:
        // nothing to replay from.
        _ => Freshness::All,
    };

    // Persist the current id regardless, so the next launch has a baseline.
    let _ = save_state(&state_file(), &uuid, now_id);
    outcome
}

fn replay_into_freshness(root: &Path, since: FSEventStreamEventId) -> Freshness {
    match changes_since(root, since) {
        Some(report) if report.reliable => Freshness::Touched {
            watched_root: root.to_path_buf(),
            changed: report.changed_dirs,
        },
        _ => Freshness::All,
    }
}

// --- persisted state (sidecar file next to the size cache) ---

struct SavedState {
    volume_uuid: String,
    last_event_id: u64,
}

fn state_file() -> PathBuf {
    state::state_dir().join("fsevents.json")
}

fn load_state(path: &Path) -> Option<SavedState> {
    let text = std::fs::read_to_string(path).ok()?;
    let value: serde_json::Value = serde_json::from_str(&text).ok()?;
    if value.get("version").and_then(|v| v.as_u64())? != STATE_VERSION {
        return None;
    }
    Some(SavedState {
        volume_uuid: value.get("volume_uuid")?.as_str()?.to_string(),
        last_event_id: value.get("last_event_id")?.as_u64()?,
    })
}

fn save_state(path: &Path, volume_uuid: &str, last_event_id: u64) -> Result<()> {
    let value = serde_json::json!({
        "version": STATE_VERSION,
        "volume_uuid": volume_uuid,
        "last_event_id": last_event_id,
    });
    state::atomic_write(path, &serde_json::to_string_pretty(&value)?)
}

// --- CoreServices / CoreFoundation FFI ---

type CFAllocatorRef = *const c_void;
type CFArrayRef = *const c_void;
type CFStringRef = *const c_void;
type CFRunLoopRef = *const c_void;
type CFUUIDRef = *const c_void;
type CFTypeRef = *const c_void;
type FSEventStreamRef = *mut c_void;
type ConstFSEventStreamRef = *const c_void;
type CFIndex = isize;
type Boolean = u8;
type CFTimeInterval = f64;
type CFStringEncoding = u32;
type FSEventStreamEventId = u64;
type FSEventStreamEventFlags = u32;
type FSEventStreamCreateFlags = u32;

const KCF_STRING_ENCODING_UTF8: CFStringEncoding = 0x0800_0100;
const FS_CREATE_FLAG_NO_DEFER: FSEventStreamCreateFlags = 0x0000_0002;

const FS_FLAG_MUST_SCAN_SUBDIRS: u32 = 0x0000_0001;
const FS_FLAG_USER_DROPPED: u32 = 0x0000_0002;
const FS_FLAG_KERNEL_DROPPED: u32 = 0x0000_0004;
const FS_FLAG_EVENT_IDS_WRAPPED: u32 = 0x0000_0008;
const FS_FLAG_HISTORY_DONE: u32 = 0x0000_0010;
const FS_FLAG_ROOT_CHANGED: u32 = 0x0000_0020;
const FS_FLAG_MOUNT: u32 = 0x0000_0040;
const FS_FLAG_UNMOUNT: u32 = 0x0000_0080;
/// Flags that mean we may have missed changes, so "unchanged ⇒ fresh" no longer
/// holds and the whole replay must be discarded.
const FS_FLAGS_UNRELIABLE: u32 = FS_FLAG_USER_DROPPED
    | FS_FLAG_MUST_SCAN_SUBDIRS
    | FS_FLAG_KERNEL_DROPPED
    | FS_FLAG_EVENT_IDS_WRAPPED
    | FS_FLAG_MOUNT
    | FS_FLAG_UNMOUNT
    | FS_FLAG_ROOT_CHANGED;

#[repr(C)]
struct FSEventStreamContext {
    version: CFIndex,
    info: *mut c_void,
    retain: *const c_void,
    release: *const c_void,
    copy_description: *const c_void,
}

#[repr(C)]
struct CFArrayCallBacks {
    version: CFIndex,
    retain: *const c_void,
    release: *const c_void,
    copy_description: *const c_void,
    equal: *const c_void,
}

type FSEventStreamCallback = extern "C" fn(
    ConstFSEventStreamRef,
    *mut c_void,
    usize,
    *mut c_void,
    *const FSEventStreamEventFlags,
    *const FSEventStreamEventId,
);

extern "C" {
    #[link_name = "kCFRunLoopDefaultMode"]
    static CF_RUN_LOOP_DEFAULT_MODE: CFStringRef;
    #[link_name = "kCFTypeArrayCallBacks"]
    static CF_TYPE_ARRAY_CALLBACKS: CFArrayCallBacks;
}

#[link(name = "CoreFoundation", kind = "framework")]
extern "C" {
    fn CFStringCreateWithBytes(
        alloc: CFAllocatorRef,
        bytes: *const u8,
        num_bytes: CFIndex,
        encoding: CFStringEncoding,
        is_external: Boolean,
    ) -> CFStringRef;
    fn CFStringGetLength(s: CFStringRef) -> CFIndex;
    fn CFStringGetCString(
        s: CFStringRef,
        buffer: *mut c_char,
        size: CFIndex,
        encoding: CFStringEncoding,
    ) -> Boolean;
    fn CFArrayCreate(
        alloc: CFAllocatorRef,
        values: *const *const c_void,
        num_values: CFIndex,
        callbacks: *const CFArrayCallBacks,
    ) -> CFArrayRef;
    fn CFRelease(cf: CFTypeRef);
    fn CFUUIDCreateString(alloc: CFAllocatorRef, uuid: CFUUIDRef) -> CFStringRef;
    fn CFRunLoopGetCurrent() -> CFRunLoopRef;
    fn CFRunLoopRunInMode(
        mode: CFStringRef,
        seconds: CFTimeInterval,
        return_after_source_handled: Boolean,
    ) -> i32;
}

#[link(name = "CoreServices", kind = "framework")]
extern "C" {
    fn FSEventStreamCreate(
        alloc: CFAllocatorRef,
        callback: FSEventStreamCallback,
        context: *const FSEventStreamContext,
        paths_to_watch: CFArrayRef,
        since_when: FSEventStreamEventId,
        latency: CFTimeInterval,
        flags: FSEventStreamCreateFlags,
    ) -> FSEventStreamRef;
    fn FSEventStreamScheduleWithRunLoop(
        stream: FSEventStreamRef,
        run_loop: CFRunLoopRef,
        run_loop_mode: CFStringRef,
    );
    fn FSEventStreamStart(stream: FSEventStreamRef) -> Boolean;
    fn FSEventStreamStop(stream: FSEventStreamRef);
    fn FSEventStreamInvalidate(stream: FSEventStreamRef);
    fn FSEventStreamRelease(stream: FSEventStreamRef);
    fn FSEventsGetCurrentEventId() -> FSEventStreamEventId;
    fn FSEventsCopyUUIDForDevice(dev: libc::dev_t) -> CFUUIDRef;
}

struct CallbackState {
    changed_dirs: Vec<PathBuf>,
    history_done: bool,
    reliable: bool,
}

struct ChangeReport {
    changed_dirs: Vec<PathBuf>,
    reliable: bool,
}

/// The current global event id, used as the next launch's replay baseline.
fn current_event_id() -> FSEventStreamEventId {
    unsafe { FSEventsGetCurrentEventId() }
}

/// Stable identity string for the volume that `path` lives on, so a replay from
/// a saved id is only trusted on the same volume.
fn volume_uuid(path: &Path) -> Option<String> {
    let dev = std::fs::metadata(path).ok()?.dev() as libc::dev_t;
    unsafe {
        let uuid = FSEventsCopyUUIDForDevice(dev);
        if uuid.is_null() {
            return None;
        }
        let string = CFUUIDCreateString(ptr::null(), uuid);
        CFRelease(uuid);
        if string.is_null() {
            return None;
        }
        let result = cfstring_to_string(string);
        CFRelease(string);
        result
    }
}

/// Replay the historical event stream for `root` since `since`, returning the
/// directories that changed. Runs the CoreFoundation run loop on a dedicated
/// thread (so it never touches the main thread's run loop) and is bounded by a
/// hard timeout. `None` means the replay could not be completed and the caller
/// must fall back.
fn changes_since(root: &Path, since: FSEventStreamEventId) -> Option<ChangeReport> {
    let root = root.to_path_buf();
    std::thread::Builder::new()
        .name(String::from("diskr-fsevents"))
        .spawn(move || changes_since_on_run_loop(&root, since))
        .ok()?
        .join()
        .ok()?
}

fn changes_since_on_run_loop(root: &Path, since: FSEventStreamEventId) -> Option<ChangeReport> {
    let cf_root = make_cfstring(root.as_os_str().as_bytes())?;
    let values = [cf_root];
    let array = unsafe {
        CFArrayCreate(
            ptr::null(),
            values.as_ptr(),
            1,
            ptr::addr_of!(CF_TYPE_ARRAY_CALLBACKS),
        )
    };
    // The array retained the string; release our reference either way.
    unsafe { CFRelease(cf_root) };
    if array.is_null() {
        return None;
    }

    let mut callback_state = CallbackState {
        changed_dirs: Vec::new(),
        history_done: false,
        reliable: true,
    };
    let context = FSEventStreamContext {
        version: 0,
        info: &mut callback_state as *mut CallbackState as *mut c_void,
        retain: ptr::null(),
        release: ptr::null(),
        copy_description: ptr::null(),
    };

    let stream = unsafe {
        FSEventStreamCreate(
            ptr::null(),
            fsevents_callback,
            &context,
            array,
            since,
            0.0,
            FS_CREATE_FLAG_NO_DEFER,
        )
    };
    unsafe { CFRelease(array) };
    if stream.is_null() {
        return None;
    }

    let run_loop = unsafe { CFRunLoopGetCurrent() };
    let mode = unsafe { CF_RUN_LOOP_DEFAULT_MODE };
    let started = unsafe {
        FSEventStreamScheduleWithRunLoop(stream, run_loop, mode);
        FSEventStreamStart(stream)
    };
    if started == 0 {
        unsafe {
            FSEventStreamInvalidate(stream);
            FSEventStreamRelease(stream);
        }
        return None;
    }

    // Pump the run loop until the historical replay finishes (HistoryDone) or a
    // hard deadline elapses. The callback runs synchronously on this thread
    // inside CFRunLoopRunInMode, so reading callback_state between turns is
    // race-free.
    let deadline = Instant::now() + Duration::from_secs(3);
    while !callback_state.history_done && Instant::now() < deadline {
        unsafe { CFRunLoopRunInMode(mode, 0.2, 1) };
    }
    let completed = callback_state.history_done;

    unsafe {
        FSEventStreamStop(stream);
        FSEventStreamInvalidate(stream);
        FSEventStreamRelease(stream);
    }

    if !completed {
        return None;
    }
    Some(ChangeReport {
        changed_dirs: std::mem::take(&mut callback_state.changed_dirs),
        reliable: callback_state.reliable,
    })
}

extern "C" fn fsevents_callback(
    _stream: ConstFSEventStreamRef,
    info: *mut c_void,
    num_events: usize,
    event_paths: *mut c_void,
    event_flags: *const FSEventStreamEventFlags,
    _event_ids: *const FSEventStreamEventId,
) {
    if info.is_null() || event_paths.is_null() || event_flags.is_null() {
        return;
    }
    // Without kFSEventStreamCreateFlagUseCFTypes, eventPaths is a C array of
    // NUL-terminated strings.
    let paths = event_paths as *const *const c_char;
    let state = unsafe { &mut *(info as *mut CallbackState) };

    for i in 0..num_events {
        let flags = unsafe { *event_flags.add(i) };
        if flags & FS_FLAG_HISTORY_DONE != 0 {
            state.history_done = true;
            continue;
        }
        if !flags_are_reliable(flags) {
            state.reliable = false;
        }
        let raw = unsafe { *paths.add(i) };
        if raw.is_null() {
            continue;
        }
        let bytes = unsafe { CStr::from_ptr(raw) }.to_bytes();
        if !bytes.is_empty() {
            state.changed_dirs.push(PathBuf::from(
                std::str::from_utf8(bytes)
                    .map(str::to_owned)
                    .unwrap_or_else(|_| String::from_utf8_lossy(bytes).into_owned()),
            ));
        }
    }
}

fn flags_are_reliable(flags: u32) -> bool {
    flags & FS_FLAGS_UNRELIABLE == 0
}

fn make_cfstring(bytes: &[u8]) -> Option<*const c_void> {
    let s = unsafe {
        CFStringCreateWithBytes(
            ptr::null(),
            bytes.as_ptr(),
            bytes.len() as CFIndex,
            KCF_STRING_ENCODING_UTF8,
            0,
        )
    };
    if s.is_null() {
        None
    } else {
        Some(s)
    }
}

fn cfstring_to_string(s: CFStringRef) -> Option<String> {
    if s.is_null() {
        return None;
    }
    let len = unsafe { CFStringGetLength(s) };
    // UTF-8 needs at most 4 bytes per UTF-16 unit, plus a NUL.
    let capacity = (len.max(0) as usize)
        .saturating_mul(4)
        .saturating_add(1)
        .max(8);
    let mut buf = vec![0_i8; capacity];
    let ok = unsafe {
        CFStringGetCString(
            s,
            buf.as_mut_ptr(),
            capacity as CFIndex,
            KCF_STRING_ENCODING_UTF8,
        )
    };
    if ok == 0 {
        return None;
    }
    let cstr = unsafe { CStr::from_ptr(buf.as_ptr()) };
    Some(cstr.to_string_lossy().into_owned())
}

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

    #[test]
    fn freshness_all_marks_everything_stale() {
        let f = Freshness::All;
        assert!(f.is_stale(Path::new("/anything")));
    }

    #[test]
    fn touched_marks_only_affected_subtrees() {
        let f = Freshness::Touched {
            watched_root: PathBuf::from("/root"),
            changed: vec![PathBuf::from("/root/a/b")],
        };
        // The changed directory itself and its ancestors are stale.
        assert!(f.is_stale(Path::new("/root/a/b")));
        assert!(f.is_stale(Path::new("/root/a")));
        assert!(f.is_stale(Path::new("/root")));
        // A sibling subtree with no change inside it stays fresh.
        assert!(!f.is_stale(Path::new("/root/a/c")));
        assert!(!f.is_stale(Path::new("/root/x")));
        // Anything outside the watched root is unknown, hence stale.
        assert!(f.is_stale(Path::new("/elsewhere")));
    }

    #[test]
    fn recursive_or_dropped_events_invalidate_the_replay() {
        assert!(!flags_are_reliable(FS_FLAG_MUST_SCAN_SUBDIRS));
        assert!(!flags_are_reliable(FS_FLAG_USER_DROPPED));
        assert!(flags_are_reliable(0));
        assert!(flags_are_reliable(FS_FLAG_HISTORY_DONE));
    }

    #[test]
    fn state_round_trips_through_sidecar() {
        let path = std::env::temp_dir().join(format!(
            "diskr_fsevents_{}_{}.json",
            std::process::id(),
            current_event_id()
        ));
        save_state(&path, "ABC-123", 987_654).unwrap();
        let loaded = load_state(&path).expect("state present");
        assert_eq!(loaded.volume_uuid, "ABC-123");
        assert_eq!(loaded.last_event_id, 987_654);
        // A wrong version is ignored rather than trusted.
        std::fs::write(
            &path,
            r#"{"version":99,"volume_uuid":"x","last_event_id":1}"#,
        )
        .unwrap();
        assert!(load_state(&path).is_none());
        let _ = std::fs::remove_file(path);
    }

    #[test]
    fn ffi_symbols_link_and_do_not_panic() {
        // Exercises the CoreServices/CoreFoundation bindings: a wrong symbol
        // name would fail to link. Values are environment-dependent, so we only
        // assert the calls return without crashing.
        let _ = current_event_id();
        let _ = volume_uuid(&std::env::temp_dir());
    }

    #[test]
    fn replay_run_loop_completes_with_no_history() {
        // Replaying "since the current id" has no historical events, so
        // HistoryDone fires immediately. This drives the whole run-loop FFI
        // path — CFArray/CFString creation, FSEventStreamCreate/Schedule/Start,
        // the callback, and CFRunLoopRunInMode — deterministically, without
        // depending on a specific change being journaled. If FSEvents is
        // unavailable it returns None; either way it must not hang or panic.
        let dir = std::env::temp_dir();
        if let Some(report) = changes_since(&dir, current_event_id()) {
            assert!(report.reliable);
        }
    }
}