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
//! The policy lock: a pinned set of file digests the kernel refuses to run without.
//!
//! `moat init` pins the policy and every host hook file it installed. `moat guard`
//! recomputes the digests on every call and denies everything when a pinned file
//! changed or disappeared, so an agent that edits `~/.moat/policy.yaml` or removes
//! a hook gets a `deny`, not a quieter kernel. Only a human can re-pin
//! (`moat init`, or `moat doctor --accept` from a terminal). Drift of only the
//! keys a host's spec lists as relaxed in its hook file (`relaxed.rs`,
//! `spec_keys.rs`) is reported the same way, but the guard keeps deciding
//! through it.
use std::collections::BTreeMap;
use std::fmt;
use std::fs;
use std::path::{Path, PathBuf};
use anyhow::{Context as _, Result, bail};
use openmoat_core::rule_id::KERNEL_INTEGRITY;
use openmoat_core::{Decision, Verdict};
use openmoat_hosts::Host;
use serde::{Deserialize, Serialize};
use crate::home::{Home, write_private};
use crate::install::{HookState, HostConfig};
pub mod binary;
mod checkpoint;
mod digest;
mod forms;
mod kept;
mod keys;
mod owner;
mod relaxed;
mod spec_keys;
pub use checkpoint::{Checkpoint, repin_after};
pub use digest::sha256_hex;
use digest::{digest, digests};
pub use kept::{Kept, verify_state};
pub use relaxed::{check, split};
const LOCK_VERSION: u32 = 1;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Lock {
pub version: u32,
pub pinned_at_ms: i64,
/// Absolute path of the `moat` binary the hooks point at.
pub binary: String,
/// SHA-256 of that binary when the lock was written, which the guard
/// compares its own file with (`binary.rs`, ADR-071). `None` in a lock
/// from before ADR-071.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub binary_sha256: Option<String>,
/// The release that binary reported, which tells an upgrade in place
/// from a file changed under the same release.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub binary_version: Option<String>,
/// Canonical path → lowercase hex SHA-256 of the file contents (of a hook
/// file, without the keys its host's spec leaves out, Claude Code's
/// `theme`; see `digest.rs`).
pub entries: BTreeMap<String, String>,
/// Codex `config.toml` → SHA-256 of the part OpenMoat owns (its permissions
/// profile, ADR-018). Codex edits the rest of that file itself.
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub codex_profiles: BTreeMap<String, String>,
/// Pinned JSON or TOML file → top-level key → SHA-256 of its value, so drift
/// can name the keys that changed. Locks written before #288 have none.
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub keys: BTreeMap<String, BTreeMap<String, String>>,
/// Hook file whose host compares its relaxed keys one by one → key of the
/// object they are in (Claude Code's `permissions`) → SHA-256 of its
/// value, so the guard can tell a changed toggle from a changed hook
/// (`relaxed.rs`).
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub permission_keys: BTreeMap<String, BTreeMap<String, String>>,
/// A hook file whose host lists relaxed keys (Gemini CLI's `settings.json`)
/// → SHA-256 of the file without them, so the guard can tell a new model
/// or theme from any other change (`spec_keys.rs`).
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub relaxed_rest: BTreeMap<String, String>,
/// MCP server definitions a person accepted (`mcp/pins.rs`). `None` in a
/// lock from before MCP pinning; their drift holds back only that server.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub mcp_servers: Option<crate::mcp::Pins>,
/// Pinned hook file → id of the host that owns it, for each file the lock
/// pins in that host's pinned form rather than by its bytes (OpenClaw's
/// `openclaw.json` without `mcp.servers`; `forms.rs`). `None` in a lock
/// from before #653, which `forms.rs` reads by file name.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub pinned_forms: Option<BTreeMap<String, String>>,
/// Pinned hook file → id of the host that owns it, for every hook file,
/// so drift in it is read by that host's spec and never by the file's
/// name (`owner.rs`). `None` in a lock from before it, which `owner.rs`
/// reads by the place the spec puts each host's file.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub hook_owners: Option<BTreeMap<String, String>>,
}
/// Which hook files a re-pin covers, besides OpenMoat's own state files.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HookPins {
/// `moat init`: the hook files already pinned plus every hook file installed
/// under this shell's environment.
Adopt,
/// `moat allow`, `moat doctor --accept`: the hook files already pinned. The
/// hook file an agent reads depends on the agent's environment
/// (its spec's `InstallSpec::dir_variable`: `CLAUDE_CONFIG_DIR`, `CODEX_HOME`,
/// …), which need not be this shell's, so a re-pin must not decide again which files those are.
Keep,
}
/// Pin everything the kernel trusts: policy, environment snapshot, approval
/// files and the host hook files `hooks` selects. Used by `init`,
/// `doctor --accept` and `allow`, which are the only human paths that may re-pin.
pub fn repin(home: &Home, binary: &Path, hooks: HookPins) -> Result<Lock> {
repin_with(home, binary, hooks, &[], &[])
}
/// [`repin`], also pinning `files` whole and `by_part` by the part OpenMoat
/// owns: the files `moat sandbox sync` just wrote.
pub fn repin_with(
home: &Home,
binary: &Path,
hooks: HookPins,
files: &[PathBuf],
by_part: &[PathBuf],
) -> Result<Lock> {
let mut paths = state_files(home);
paths.extend_from_slice(files);
let mut profiles = by_part.to_vec();
let lock_path = home.lock_path();
let had_lock = lock_path.exists();
let mut mcp_servers = None;
let mut owners = Vec::new();
if had_lock {
match (Lock::load(&lock_path), hooks) {
(Ok(old), _) => {
// This shell's hook files, which an older lock's guessed
// owners are kept for (`owner.rs`); none when they cannot be
// told, and none needed when the lock records its owners.
let here: Vec<(PathBuf, Host)> = if old.hook_owners.is_some() {
Vec::new()
} else {
installed_hooks(home, binary)
.map(|found| {
found
.iter()
.map(|c| (c.settings_path.clone(), c.host))
.collect()
})
.unwrap_or_default()
};
owners = old.owners_with(&here);
// Only `moat mcp accept` and `moat doctor --accept` change these.
mcp_servers = old.mcp_servers;
paths.extend(old.entries.into_keys().map(PathBuf::from));
profiles.extend(old.codex_profiles.into_keys().map(PathBuf::from));
}
(Err(e), HookPins::Keep) => {
return Err(
e.context("cannot tell which hook files are pinned; run `openmoat init`")
);
}
// `init` is how a person recovers from an unreadable lock.
(Err(_), HookPins::Adopt) => {}
}
}
// Without a lock there is nothing to keep, so this environment's hooks it is.
if hooks == HookPins::Adopt || !had_lock {
let installed = installed_hooks(home, binary)?;
owners.extend(installed.iter().map(|c| (c.settings_path.clone(), c.host)));
paths.extend(installed.iter().flat_map(pinned_with));
profiles.extend(crate::sandbox::install::part_pinned_settings(home)?);
paths.extend(crate::sandbox::install::whole_pinned_settings(home)?);
}
let mut lock = Lock::pin_owned(binary, &paths, &owners)?;
// A state file that is missing (one a person accepted as deleted) is
// pinned as missing: the guard would otherwise read one planted there
// later from disk, unpinned.
for path in state_files(home) {
lock.entries
.entry(key(&path))
.or_insert_with(|| kept::ABSENT.to_owned());
}
lock.mcp_servers = mcp_servers;
// What the guard compares its own file with (ADR-071); a person's re-pin
// is what records it.
(lock.binary_sha256, lock.binary_version) = binary::record(home, binary).unzip();
for path in profiles.iter().filter(|p| p.is_file()) {
let digest = crate::sandbox::install::part_digest(path)?;
lock.codex_profiles.insert(key(path), digest);
}
lock.save(&lock_path)?;
Ok(lock)
}
/// OpenMoat's own files, pinned by every lock whatever the environment.
fn state_files(home: &Home) -> Vec<PathBuf> {
vec![
home.policy_path(),
home.environment_path(),
home.grants_path(),
home.overlay_path(),
home.trust_path(),
home.hosts_path(),
// Where the audit log is sent (ADR-081): a changed file could send it
// to another place, so it is pinned as the policy is.
home.delivery_config_path(),
]
}
/// The signing files only `moat policy trust-key`, `install` and `remove` pin
/// (ADR-031): the allowed signers, the pack record and each installed pack.
/// Another re-pin keeps their pins and never adds them, so a file planted
/// there is never trusted by a re-pin made for something else.
pub fn signing_files(home: &Home) -> Vec<PathBuf> {
let mut files = vec![home.signers_path(), home.packs_path()];
files.extend(crate::packs::installed_files(home));
files
}
/// Hook files with a current `moat` hook under this shell's environment.
pub fn installed_hook_files(home: &Home, binary: &Path) -> Result<Vec<PathBuf>> {
Ok(installed_hooks(home, binary)?
.iter()
.flat_map(pinned_with)
.collect())
}
/// The files the lock pins for `config`'s hook: its extra files, then the hook file.
fn pinned_with(config: &HostConfig) -> Vec<PathBuf> {
let mut files = config.extra_files();
files.push(config.settings_path.clone());
files
}
/// Each host with a current `moat` hook under this shell's environment.
fn installed_hooks(home: &Home, binary: &Path) -> Result<Vec<HostConfig>> {
let mut installed = Vec::new();
for host in Host::ALL {
let config = HostConfig::for_host(home, host)?;
if config.state(binary) == HookState::Installed {
installed.push(config);
}
}
Ok(installed)
}
/// How this shell's hook files differ from the ones the lock pins.
#[derive(Debug, Default, PartialEq, Eq)]
pub struct HookPinGap {
/// Installed here but not pinned: changes to them go unseen until `moat init`.
pub unpinned: Vec<PathBuf>,
/// Pinned but not this shell's: another environment's hook files, still verified.
pub elsewhere: Vec<PathBuf>,
}
impl HookPinGap {
pub fn new(lock: &Lock, home: &Home, installed: &[PathBuf]) -> Self {
// A sandbox settings file pinned whole here is pinned, but is no hook file.
let mut state = state_files(home);
state.extend(signing_files(home));
state.extend(crate::sandbox::install::whole_pinned_settings(home).unwrap_or_default());
let state: Vec<String> = state.iter().map(|p| key(p)).collect();
let here: Vec<String> = installed.iter().map(|p| key(p)).collect();
Self {
unpinned: installed
.iter()
.filter(|p| !lock.pins(p))
.cloned()
.collect(),
elsewhere: lock
.entries
.keys()
.filter(|k| !state.contains(k) && !here.contains(k))
.map(PathBuf::from)
.collect(),
}
}
/// Why a pinned hook file may not be this shell's: one of the variables
/// that move a host's directory differs. Every host's is named.
pub fn moved_by() -> String {
let names: Vec<&str> = openmoat_hosts::Host::ALL
.iter()
.filter_map(|host| host.spec().install.as_ref())
.map(|install| install.dir_variable)
.collect();
format!("one of {} differs", names.join(", "))
}
}
/// `Some(deny)` when the pinned policy or hook files changed since `moat init`.
pub fn violation(home: &Home) -> Result<Option<Decision>> {
let lock_path = home.lock_path();
if !lock_path.is_file() {
bail!(
"no policy lock at {}; run `openmoat init`",
lock_path.display()
);
}
Ok(deny_for(&Lock::load(&lock_path)?.verify()))
}
/// The `kernel-integrity` deny naming `drift`; `None` when there is none.
fn deny_for(drift: &[Drift]) -> Option<Decision> {
if drift.is_empty() {
return None;
}
let mut decision = Decision::new(Verdict::Deny);
decision.rules.push(KERNEL_INTEGRITY.to_owned());
for d in drift {
decision.reasons.push(d.to_string());
}
decision.reasons.push(
"run `openmoat doctor` to inspect; `openmoat doctor --accept` or `openmoat init` to re-pin"
.to_owned(),
);
Some(decision)
}
/// Refuse a person's command that re-pins while pinned files drifted: the
/// re-pin would accept a tampered file unseen. `doing` completes "refusing to …".
pub fn refuse_drift(home: &Home, doing: &str) -> Result<()> {
if let Some(deny) = violation(home)? {
bail!(
"refusing to {doing} while the policy lock shows drift ({KERNEL_INTEGRITY}):\n {}",
deny.reasons.join("\n ")
);
}
Ok(())
}
/// Forget the pins of `paths`, the host files `moat uninstall` just undid, and
/// pin `rewritten` whole, the files a person's command just wrote (`hosts.json`,
/// or a pinned file `moat mcp wrap` changed). Every other pin stays as it is,
/// so no drift elsewhere is accepted.
pub fn unpin(home: &Home, paths: &[PathBuf], rewritten: &[PathBuf]) -> Result<()> {
let lock_path = home.lock_path();
if !lock_path.is_file() {
return Ok(());
}
let mut lock = Lock::load(&lock_path)?;
// A rewritten file keeps the host it is pinned for.
let owners: Vec<(PathBuf, Host)> = lock
.owners()
.into_iter()
.filter(|(stored, _)| rewritten.iter().any(|p| key(p) == key(stored)))
.collect();
let mut forms = lock.owner_ids();
let mut hook_owners = lock.hook_owner_ids();
for path in paths.iter().map(|p| key(p)) {
lock.codex_profiles.remove(&path);
}
for path in paths.iter().chain(rewritten).map(|p| key(p)) {
lock.entries.remove(&path);
lock.keys.remove(&path);
lock.permission_keys.remove(&path);
lock.relaxed_rest.remove(&path);
forms.remove(&path);
hook_owners.remove(&path);
}
let fresh = Lock::pin_owned(Path::new(&lock.binary), rewritten, &owners)?;
forms.extend(fresh.pinned_forms.unwrap_or_default());
lock.pinned_forms = Some(forms);
hook_owners.extend(fresh.hook_owners.unwrap_or_default());
lock.hook_owners = Some(hook_owners);
lock.entries.extend(fresh.entries);
lock.keys.extend(fresh.keys);
lock.permission_keys.extend(fresh.permission_keys);
lock.relaxed_rest.extend(fresh.relaxed_rest);
lock.save(&lock_path)
}
/// One way a pinned file differs from the lock.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Drift {
Modified(PathBuf),
/// Modified, with the top-level keys that differ from the pin.
KeysChanged(PathBuf, String),
Missing(PathBuf),
Unreadable(PathBuf, String),
/// A staged copy (`proposal`) that would change the pinned `target`.
Proposed {
proposal: PathBuf,
target: PathBuf,
},
}
impl fmt::Display for Drift {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Modified(p) => write!(f, "{} was modified", p.display()),
Self::KeysChanged(p, keys) => write!(f, "{} was modified: {keys}", p.display()),
Self::Missing(p) => write!(f, "{} is missing", p.display()),
Self::Unreadable(p, why) => write!(f, "{} is unreadable: {why}", p.display()),
Self::Proposed { proposal, target } => write!(
f,
"{} would change {}",
proposal.display(),
target.display()
),
}
}
}
impl Lock {
/// [`Self::pin_owned`], each file owned by the host a lock from before
/// the owners were recorded reads it as (`owner.rs`): a `settings.json`
/// is Claude Code's, one in `.gemini` Gemini CLI's, any other no host's.
#[cfg(test)]
pub fn pin(binary: &Path, paths: &[PathBuf]) -> Result<Self> {
let owners: Vec<(PathBuf, Host)> = paths
.iter()
.filter_map(|path| Some((path.clone(), owner::assumed(path)?)))
.collect();
Self::pin_owned(binary, paths, &owners)
}
/// Pin the current contents of `paths`. Files that do not exist are skipped;
/// callers pass only files they just wrote or verified. Each hook file in
/// `owners` is pinned as its host's spec says, with that host recorded: in
/// its pinned form (`forms.rs`), without its unpinned keys, with what its
/// relaxed keys are compared by (`owner.rs`); every other file by its bytes.
pub fn pin_owned(binary: &Path, paths: &[PathBuf], owners: &[(PathBuf, Host)]) -> Result<Self> {
let owners: BTreeMap<String, Host> = owners
.iter()
.map(|(path, host)| (key(path), *host))
.collect();
let (mut entries, mut keys, mut permission_keys, mut relaxed_rest, mut pinned_forms) = (
BTreeMap::new(),
BTreeMap::new(),
BTreeMap::new(),
BTreeMap::new(),
BTreeMap::new(),
);
let mut hook_owners = BTreeMap::new();
for path in paths.iter().filter(|path| path.is_file()) {
let owner = owners.get(&key(path)).copied();
let form = owner.and_then(forms::form_of);
if let Some(host) = owner {
hook_owners.insert(key(path), host.id().to_owned());
if form.is_some() {
pinned_forms.insert(key(path), host.id().to_owned());
}
}
let unpinned = owner::unpinned_of(owner);
entries.insert(key(path), digest(path, unpinned, form)?);
if let Some(digests) = keys::key_digests(path, unpinned) {
keys.insert(key(path), digests);
}
let Some(install) = owner.and_then(|host| host.spec().install.as_ref()) else {
continue;
};
if let Some(digests) = relaxed::parent_digests(path, install) {
permission_keys.insert(key(path), digests);
}
if let Some(rest) = spec_keys::pinned_rest(path, install) {
relaxed_rest.insert(key(path), rest);
}
}
Ok(Self {
version: LOCK_VERSION,
pinned_at_ms: crate::time::now_ms(),
binary: binary.to_string_lossy().into_owned(),
binary_sha256: None,
binary_version: None,
entries,
codex_profiles: BTreeMap::new(),
keys,
permission_keys,
relaxed_rest,
mcp_servers: None,
pinned_forms: Some(pinned_forms),
hook_owners: Some(hook_owners),
})
}
pub fn load(path: &Path) -> Result<Self> {
let bytes = fs::read(path).with_context(|| format!("reading lock {}", path.display()))?;
Self::parse(&bytes, path)
}
/// The lock in `bytes`, read from `path` (named in errors).
pub fn parse(bytes: &[u8], path: &Path) -> Result<Self> {
let lock: Self = serde_json::from_slice(bytes)
.with_context(|| format!("parsing lock {}", path.display()))?;
if lock.version != LOCK_VERSION {
bail!(
"lock {} has version {}; this build supports {LOCK_VERSION}",
path.display(),
lock.version
);
}
Ok(lock)
}
pub fn save(&self, path: &Path) -> Result<()> {
debug_assert!(
self.pinned_twice().is_none(),
"{:?} is pinned whole and by the part of it OpenMoat owns",
self.pinned_twice()
);
// No save writes a server's variable or header value in clear, even
// one an older version left in the copy it shows (`mcp/pins.rs`).
let mut lock = self.clone();
if let Some(pins) = lock.mcp_servers.as_mut() {
crate::mcp::pins::scrub(pins);
}
let text = serde_json::to_string_pretty(&lock)? + "\n";
write_private(path, text.as_bytes())
}
/// Compare pinned digests with the files on disk. Empty means intact.
pub fn verify(&self) -> Vec<Drift> {
self.verify_keeping(&[], &[]).0
}
/// A file pinned both whole ([`Self::entries`]) and by the part OpenMoat
/// owns ([`Self::codex_profiles`]). Never one: Codex rewrites the rest of
/// its `config.toml` itself, so a whole pin of it would deny every call
/// after Codex's next edit, and a re-pin path that adds one has lost track
/// of which form a file is pinned in.
pub fn pinned_twice(&self) -> Option<&str> {
self.codex_profiles
.keys()
.find(|path| self.entries.contains_key(*path))
.map(String::as_str)
}
/// Whether the lock pins the part OpenMoat owns of the settings file at `path`.
pub fn pins_owned_part(&self, path: &Path) -> bool {
self.codex_profiles.contains_key(&key(path))
}
/// Drift for one pinned file, or `None` when it is intact or not pinned.
pub fn verify_one(&self, path: &Path) -> Option<Drift> {
let expected = self.entries.get(&key(path))?;
self.check_one(&PathBuf::from(key(path)), expected)
.err()
.map(|(drift, _)| drift)
}
/// Drift that renaming `proposal` over the pinned `target` would leave: the
/// target's own drift, else any difference between the proposal and the
/// pinned contents. `None` when the target is not pinned.
pub fn verify_proposal(&self, proposal: &Path, target: &Path) -> Option<Drift> {
let expected = self.entries.get(&key(target))?;
if let Some(drift) = self.verify_one(target) {
return Some(drift);
}
match digests(
proposal,
self.unpinned(&key(target)),
self.form(&key(target)),
) {
Ok(actual) if actual.contains(expected) => None,
Ok(_) => Some(Drift::Proposed {
proposal: proposal.to_path_buf(),
target: PathBuf::from(key(target)),
}),
Err(e) => Some(Drift::Unreadable(proposal.to_path_buf(), format!("{e:#}"))),
}
}
pub fn pins(&self, path: &Path) -> bool {
self.entries.contains_key(&key(path))
}
/// The digest the lock records for `path`, a file pinned by its bytes
/// alone (a state file); `None` when it is not pinned.
pub fn pinned_digest(&self, path: &Path) -> Option<&str> {
self.entries.get(&key(path)).map(String::as_str)
}
}
/// Identity of a pinned file: its canonical directory plus its own name. The
/// leaf is deliberately not resolved, so a file that is later replaced by a
/// symlink keeps the same key and is compared, not forgotten. The parent is
/// canonicalised so `/var/...` and `/private/var/...` agree on macOS and a
/// deleted file still has a key.
fn key(path: &Path) -> String {
let located = match (path.parent(), path.file_name()) {
(Some(parent), Some(name)) => {
fs::canonicalize(parent).map_or_else(|_| path.to_path_buf(), |p| p.join(name))
}
_ => path.to_path_buf(),
};
let text = located.to_string_lossy();
if cfg!(windows) {
crate::context::strip_verbatim(&text)
} else {
text.into_owned()
}
}
#[cfg(test)]
mod tests;