rightkit-fs 0.3.1

Safe state directories, atomic writes, cross-process locks, owner-only files, and stable file identity for Right Suite apps.
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
//! Stable file identity across renames.
//!
//! A [`StableId`] names "this file" independent of its path. It is bound to the filesystem's own
//! identity ([`FileKey`]: device + inode on unix, volume serial + file index on Windows), which
//! survives `rename` and move within a volume, and falls back to a content fingerprint when the
//! file was replaced (atomic save, editor "safe write") or copied to another volume.
//!
//! Resolution order, cheapest first:
//! 1. the last known path still holds the same `FileKey` -> [`Resolution::Unchanged`];
//! 2. a bounded walk of the caller's search roots finds the same `FileKey` -> [`Resolution::Moved`];
//! 3. the last known path (or a search-root file) has the same size and head/tail fingerprint
//!    but a new `FileKey` -> [`Resolution::Replaced`] (the ledger is rebound);
//! 4. otherwise [`Resolution::Missing`].
//!
//! The ledger is one JSON file written atomically under a cross-process lock, so any number of
//! processes can track and resolve concurrently. Donors: HeardRight/ScrapeRight/ViewRight file
//! grants and clipboard delivery, which each keyed files by path.

use crate::atomic::atomic_write_private;
use crate::error::{FsError, Result};
use crate::lock::{FileLock, LockOptions};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::fs::File;
use std::io::{Read, Seek, SeekFrom};
use std::path::{Path, PathBuf};
use std::time::{Duration, SystemTime, UNIX_EPOCH};

const LEDGER_FILE: &str = "identity-ledger.json";
const LOCK_FILE: &str = "identity-ledger.lock";
const SAMPLE: u64 = 64 * 1024;
const LEDGER_VERSION: u32 = 1;

pub type StableId = String;

/// The filesystem's own identity for an open file.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct FileKey {
    pub volume: u64,
    pub index: u64,
}

impl FileKey {
    pub fn of_path(path: &Path) -> Result<Self> {
        let file = File::open(path).map_err(|e| FsError::io(path, e))?;
        Self::of_file(&file, path)
    }

    pub fn of_file(file: &File, shown: &Path) -> Result<Self> {
        #[cfg(unix)]
        {
            use std::os::unix::fs::MetadataExt;
            let meta = file.metadata().map_err(|e| FsError::io(shown, e))?;
            Ok(Self {
                volume: meta.dev(),
                index: meta.ino(),
            })
        }
        #[cfg(windows)]
        {
            windows_key(file).map_err(|e| FsError::io(shown, e))
        }
        #[cfg(not(any(unix, windows)))]
        {
            let _ = (file, shown);
            Err(FsError::Unavailable("file identity"))
        }
    }

    fn of_meta(meta: &std::fs::Metadata, path: &Path) -> Option<Self> {
        #[cfg(unix)]
        {
            let _ = path;
            use std::os::unix::fs::MetadataExt;
            Some(Self {
                volume: meta.dev(),
                index: meta.ino(),
            })
        }
        #[cfg(not(unix))]
        {
            let _ = meta;
            Self::of_path(path).ok()
        }
    }
}

#[cfg(windows)]
fn windows_key(file: &File) -> std::io::Result<FileKey> {
    use std::os::windows::io::AsRawHandle;
    #[repr(C)]
    struct FileTime {
        low: u32,
        high: u32,
    }
    #[repr(C)]
    struct ByHandleFileInformation {
        attributes: u32,
        creation: FileTime,
        access: FileTime,
        write: FileTime,
        volume_serial: u32,
        size_high: u32,
        size_low: u32,
        links: u32,
        index_high: u32,
        index_low: u32,
    }
    #[link(name = "kernel32")]
    unsafe extern "system" {
        fn GetFileInformationByHandle(
            handle: *mut std::ffi::c_void,
            info: *mut ByHandleFileInformation,
        ) -> i32;
    }
    let mut info = std::mem::MaybeUninit::<ByHandleFileInformation>::zeroed();
    // SAFETY: the handle is a live file handle and `info` is a valid out-parameter.
    let ok = unsafe { GetFileInformationByHandle(file.as_raw_handle().cast(), info.as_mut_ptr()) };
    if ok == 0 {
        return Err(std::io::Error::last_os_error());
    }
    // SAFETY: the call succeeded and filled the struct.
    let info = unsafe { info.assume_init() };
    Ok(FileKey {
        volume: u64::from(info.volume_serial),
        index: (u64::from(info.index_high) << 32) | u64::from(info.index_low),
    })
}

/// Cheap content fingerprint: size plus FNV-1a over the first and last 64 KiB. Only ever used to
/// shortlist a replaced or copied file; never as proof of identity on its own.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Fingerprint {
    pub size: u64,
    pub sample_hash: u64,
}

impl Fingerprint {
    pub fn of_path(path: &Path) -> Result<Self> {
        let mut file = File::open(path).map_err(|e| FsError::io(path, e))?;
        let size = file.metadata().map_err(|e| FsError::io(path, e))?.len();
        let mut hash: u64 = 0xcbf2_9ce4_8422_2325;
        let mut feed = |bytes: &[u8]| {
            for b in bytes {
                hash ^= u64::from(*b);
                hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
            }
        };
        let mut buf = vec![0u8; SAMPLE as usize];
        let head = size.min(SAMPLE) as usize;
        file.read_exact(&mut buf[..head])
            .map_err(|e| FsError::io(path, e))?;
        feed(&buf[..head]);
        if size > SAMPLE {
            let tail = size.min(SAMPLE) as usize;
            file.seek(SeekFrom::Start(size - tail as u64))
                .map_err(|e| FsError::io(path, e))?;
            file.read_exact(&mut buf[..tail])
                .map_err(|e| FsError::io(path, e))?;
            feed(&buf[..tail]);
        }
        Ok(Self {
            size,
            sample_hash: hash,
        })
    }
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Tracked {
    pub id: StableId,
    pub key: FileKey,
    pub last_path: PathBuf,
    pub fingerprint: Fingerprint,
    pub tracked_at_ms: u64,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Resolution {
    /// Same file at the same path.
    Unchanged(PathBuf),
    /// Same file (same `FileKey`) found at a new path; the ledger now points there.
    Moved(PathBuf),
    /// The file's inode changed (replaced/copied) but size and fingerprint match; rebound.
    Replaced(PathBuf),
    Missing,
}

impl Resolution {
    pub fn path(&self) -> Option<&Path> {
        match self {
            Self::Unchanged(p) | Self::Moved(p) | Self::Replaced(p) => Some(p),
            Self::Missing => None,
        }
    }
}

#[derive(Clone, Debug)]
pub struct SearchLimits {
    pub max_entries: usize,
    pub max_depth: usize,
}

impl Default for SearchLimits {
    fn default() -> Self {
        Self {
            max_entries: 200_000,
            max_depth: 12,
        }
    }
}

#[derive(Default, Serialize, Deserialize)]
struct Ledger {
    version: u32,
    entries: BTreeMap<StableId, Tracked>,
}

pub struct IdentityStore {
    dir: PathBuf,
    limits: SearchLimits,
}

impl IdentityStore {
    /// `dir` is the app's state directory; the ledger and its lock live inside it.
    pub fn open(dir: &Path) -> Result<Self> {
        crate::private::ensure_private_dir(dir)?;
        Ok(Self {
            dir: dir.to_path_buf(),
            limits: SearchLimits::default(),
        })
    }

    pub fn with_limits(mut self, limits: SearchLimits) -> Self {
        self.limits = limits;
        self
    }

    fn locked<T>(&self, f: impl FnOnce(&mut Ledger) -> Result<(T, bool)>) -> Result<T> {
        let _lock = FileLock::acquire(
            &self.dir.join(LOCK_FILE),
            &LockOptions::wait(Duration::from_secs(10)).labeled("identity-ledger"),
        )?;
        let path = self.dir.join(LEDGER_FILE);
        let mut ledger = match std::fs::read(&path) {
            Ok(bytes) => {
                serde_json::from_slice::<Ledger>(&bytes).map_err(|e| FsError::Corrupt {
                    path: path.clone(),
                    reason: e.to_string(),
                })?
            }
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ledger {
                version: LEDGER_VERSION,
                ..Ledger::default()
            },
            Err(e) => return Err(FsError::io(&path, e)),
        };
        let (out, dirty) = f(&mut ledger)?;
        if dirty {
            let bytes = serde_json::to_vec_pretty(&ledger).expect("ledger serializes");
            atomic_write_private(&path, &bytes)?;
        }
        Ok(out)
    }

    /// Start tracking `path`; returns a new stable id.
    pub fn track(&self, path: &Path) -> Result<StableId> {
        let abs = absolute(path)?;
        let key = FileKey::of_path(&abs)?;
        let fingerprint = Fingerprint::of_path(&abs)?;
        let id = new_id()?;
        let entry = Tracked {
            id: id.clone(),
            key,
            last_path: abs,
            fingerprint,
            tracked_at_ms: now_ms(),
        };
        self.locked(|ledger| {
            ledger.entries.insert(id.clone(), entry);
            Ok(((), true))
        })?;
        Ok(id)
    }

    /// Same as [`track`](Self::track), but returns the existing id when this file (by `FileKey`)
    /// is already tracked.
    pub fn track_or_get(&self, path: &Path) -> Result<StableId> {
        let abs = absolute(path)?;
        let key = FileKey::of_path(&abs)?;
        let fingerprint = Fingerprint::of_path(&abs)?;
        self.locked(|ledger| {
            if let Some(existing) = ledger.entries.values().find(|t| t.key == key) {
                return Ok((existing.id.clone(), false));
            }
            let id = new_id()?;
            ledger.entries.insert(
                id.clone(),
                Tracked {
                    id: id.clone(),
                    key,
                    last_path: abs,
                    fingerprint,
                    tracked_at_ms: now_ms(),
                },
            );
            Ok((id, true))
        })
    }

    pub fn lookup(&self, id: &str) -> Result<Option<Tracked>> {
        self.locked(|ledger| Ok((ledger.entries.get(id).cloned(), false)))
    }

    pub fn forget(&self, id: &str) -> Result<bool> {
        self.locked(|ledger| {
            let removed = ledger.entries.remove(id).is_some();
            Ok((removed, removed))
        })
    }

    /// Re-record identity and fingerprint for `id` at `path` (after a deliberate rewrite).
    pub fn rebind(&self, id: &str, path: &Path) -> Result<()> {
        let abs = absolute(path)?;
        let key = FileKey::of_path(&abs)?;
        let fingerprint = Fingerprint::of_path(&abs)?;
        self.locked(|ledger| {
            let entry = ledger
                .entries
                .get_mut(id)
                .ok_or_else(|| FsError::UnknownId(id.to_string()))?;
            entry.key = key;
            entry.fingerprint = fingerprint;
            entry.last_path = abs;
            Ok(((), true))
        })
    }

    /// Find where `id` lives now, searching `roots` (bounded, never following symlinks) when the
    /// last known path no longer holds the file.
    pub fn resolve(&self, id: &str, roots: &[PathBuf]) -> Result<Resolution> {
        let tracked = self
            .lookup(id)?
            .ok_or_else(|| FsError::UnknownId(id.to_string()))?;

        // 1. Same file, same place.
        if let Ok(key) = FileKey::of_path(&tracked.last_path) {
            if key == tracked.key {
                return Ok(Resolution::Unchanged(tracked.last_path));
            }
        }

        // 2. Same file (by filesystem identity), new place; remember size-matching candidates.
        let mut candidates: Vec<PathBuf> = Vec::new();
        let mut moved: Option<PathBuf> = None;
        let mut budget = self.limits.max_entries;
        for root in roots {
            walk(
                root,
                0,
                self.limits.max_depth,
                &mut budget,
                &mut |path, meta| {
                    if FileKey::of_meta(meta, path) == Some(tracked.key) {
                        moved = Some(path.to_path_buf());
                        return true;
                    }
                    if meta.len() == tracked.fingerprint.size && candidates.len() < 64 {
                        candidates.push(path.to_path_buf());
                    }
                    false
                },
            );
            if moved.is_some() {
                break;
            }
        }
        if let Some(path) = moved {
            self.update_path(id, &path, None)?;
            return Ok(Resolution::Moved(path));
        }

        // 3. Replaced or copied: the old path itself first, then same-size candidates.
        let mut shortlist = Vec::new();
        if tracked.last_path.is_file() {
            shortlist.push(tracked.last_path.clone());
        }
        shortlist.extend(candidates);
        for path in shortlist {
            if Fingerprint::of_path(&path).ok() == Some(tracked.fingerprint) {
                let key = FileKey::of_path(&path)?;
                self.update_path(id, &path, Some(key))?;
                return Ok(Resolution::Replaced(path));
            }
        }
        Ok(Resolution::Missing)
    }

    fn update_path(&self, id: &str, path: &Path, new_key: Option<FileKey>) -> Result<()> {
        self.locked(|ledger| {
            if let Some(entry) = ledger.entries.get_mut(id) {
                entry.last_path = path.to_path_buf();
                if let Some(key) = new_key {
                    entry.key = key;
                }
            }
            Ok(((), true))
        })
    }
}

fn walk(
    dir: &Path,
    depth: usize,
    max_depth: usize,
    budget: &mut usize,
    visit: &mut dyn FnMut(&Path, &std::fs::Metadata) -> bool,
) -> bool {
    let Ok(entries) = std::fs::read_dir(dir) else {
        return false;
    };
    for entry in entries.flatten() {
        if *budget == 0 {
            return true;
        }
        *budget -= 1;
        // `DirEntry::metadata` does not traverse symlinks.
        let Ok(meta) = entry.metadata() else { continue };
        let path = entry.path();
        if meta.file_type().is_symlink() {
            continue;
        }
        if meta.is_file() {
            if visit(&path, &meta) {
                return true;
            }
        } else if meta.is_dir()
            && depth < max_depth
            && walk(&path, depth + 1, max_depth, budget, visit)
        {
            return true;
        }
    }
    false
}

fn absolute(path: &Path) -> Result<PathBuf> {
    if path.is_absolute() {
        Ok(path.to_path_buf())
    } else {
        Ok(std::env::current_dir()
            .map_err(|e| FsError::io(path, e))?
            .join(path))
    }
}

fn now_ms() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_millis() as u64)
        .unwrap_or(0)
}

fn new_id() -> Result<StableId> {
    let mut bytes = [0u8; 16];
    random_bytes(&mut bytes)?;
    Ok(bytes.iter().map(|b| format!("{b:02x}")).collect())
}

fn random_bytes(buf: &mut [u8]) -> Result<()> {
    #[cfg(unix)]
    {
        File::open("/dev/urandom")
            .and_then(|mut f| f.read_exact(buf))
            .map_err(|e| FsError::io("/dev/urandom", e))
    }
    #[cfg(windows)]
    {
        #[link(name = "bcrypt")]
        unsafe extern "system" {
            fn BCryptGenRandom(
                algorithm: *mut std::ffi::c_void,
                buffer: *mut u8,
                length: u32,
                flags: u32,
            ) -> i32;
        }
        // SAFETY: buffer is valid for its length; flag 2 = system-preferred RNG.
        let status =
            unsafe { BCryptGenRandom(std::ptr::null_mut(), buf.as_mut_ptr(), buf.len() as u32, 2) };
        if status >= 0 {
            Ok(())
        } else {
            Err(FsError::io(
                "BCryptGenRandom",
                std::io::Error::other("RNG failure"),
            ))
        }
    }
    #[cfg(not(any(unix, windows)))]
    {
        let _ = buf;
        Err(FsError::Unavailable("random bytes"))
    }
}