Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -784,10 +784,12 @@ scale-&-reach horizon):
- **`sc rewrap` on partial clones — refused (P37).** `Repo::rewrap` calls
`refuse_on_partial`; a paths-only/streaming rebuild that would lift the
refusal remains a possible follow-on.
- **Protected-cache hygiene (P33 final-review follow-on).** `.sc/protected-cache`
and `.sc/ws/cache-<i>` entries for a deleted or renamed path are never
pruned — unbounded but benign growth (a stale entry just goes unmatched).
Periodic or commit-time pruning of dead entries is the follow-on.
- **Protected-cache hygiene — shipped.** `ProtectedCache::save` skips entries
whose file is no longer on disk (deleted, renamed, outside the sparse view), so
`.sc/protected-cache` and `.sc/ws/cache-<i>` stay bounded by the protected
files actually present; a ws cache file is already removed with its session.
A dropped entry can only cost one re-seal if the path returns (randomized
sealing, so unchanged content gets a new blob id).
- **Theirs-side resolve carry (P33 final-review follow-on).** `sc resolve
--theirs` on a PROTECTED path records into the main-tree cache today, but
completion's format-dispatched carry only consults the cache against the
Expand Down
117 changes: 116 additions & 1 deletion crates/repo/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ struct CacheEntry {
/// checkout dir. The keyed-tag comparison that decides every `unchanged()`
/// hit does not depend on `root` at all (it hashes the caller's in-memory
/// plaintext), so a mismatched root only ever loses the advisory stat
/// bookkeeping — never incorrectness.
/// bookkeeping — never incorrectness. `save` resolves entries against `root`
/// too, so a mismatched root would also prune every entry at the next save (a
/// full re-seal, still never incorrect).
pub(crate) struct ProtectedCache {
key: [u8; 32],
/// Absolute base dir of the served checkout; `root.join(rel)` is the file
Expand All @@ -122,6 +124,31 @@ pub(crate) struct ProtectedCache {
entries: BTreeMap<String, CacheEntry>,
}

/// Whether `rel` under `root` is a regular file reached without crossing a
/// symlink, the only kind the worktree walker reads. Every component is checked
/// with `symlink_metadata`, so a file replaced by a symlink, or a directory
/// replaced by one, counts as gone.
fn is_plain_file(root: &Path, rel: &str) -> bool {
let mut cur = root.to_path_buf();
let mut comps = rel.split('/').peekable();
while let Some(c) = comps.next() {
cur.push(c);
let Ok(md) = std::fs::symlink_metadata(&cur) else {
return false;
};
let ft = md.file_type();
let ok = if comps.peek().is_some() {
ft.is_dir()
} else {
ft.is_file()
};
if !ok {
return false;
}
}
true
}

impl ProtectedCache {
pub(crate) fn open(key: [u8; 32], root: PathBuf, path: Option<PathBuf>) -> ProtectedCache {
let mut entries = BTreeMap::new();
Expand Down Expand Up @@ -214,12 +241,23 @@ impl ProtectedCache {
/// Durable atomic write via the shared `fsutil` helper (fsync temp +
/// rename + fsync parent dir, no residue on failure); no-op for an
/// ephemeral cache.
///
/// Entries whose file is gone from the checkout (deleted, renamed, or
/// outside the sparse view) are left out, so the file stays bounded by the
/// protected files actually on disk instead of growing for the life of the
/// repo. A lookup always compares the keyed tag of plaintext read from the
/// file, so an entry for an absent file can never be consulted; dropping it
/// at worst costs one re-seal if the path returns. Sealing is randomized,
/// so that re-seal yields a new blob id for unchanged content.
pub(crate) fn save(&self) -> Result<()> {
let Some(path) = &self.path else {
return Ok(());
};
let mut out = String::new();
for (p, e) in &self.entries {
if !is_plain_file(&self.root, p) {
continue;
}
out.push_str(&format!(
"{} {} {} {} {}\n",
e.mtime_ns,
Expand Down Expand Up @@ -387,6 +425,83 @@ mod tests {
cleanup(&layout);
}

#[test]
fn save_drops_entries_for_files_no_longer_on_disk() {
let layout = tmp("prune");
let key = local_key(&layout).unwrap();
let open = || {
ProtectedCache::open(
key,
layout.root.clone(),
Some(layout.protected_cache_path()),
)
};
for f in ["keep", "gone", "moved"] {
std::fs::write(layout.root.join(f), b"v").unwrap();
}
let id = ObjectId::of(b"c");
let mut c = open();
for f in ["keep", "gone", "moved"] {
c.record(f, b"v", id);
}
c.save().unwrap();
assert_eq!(open().entries.len(), 3);

std::fs::remove_file(layout.root.join("gone")).unwrap();
std::fs::rename(layout.root.join("moved"), layout.root.join("moved-to")).unwrap();
// A directory at a cached path is not a protected file either.
std::fs::create_dir(layout.root.join("gone")).unwrap();
let mut c = open();
c.record("moved-to", b"v", id);
c.save().unwrap();

let after = open();
assert_eq!(after.unchanged("keep", b"v"), Some(id));
assert_eq!(after.unchanged("moved-to", b"v"), Some(id));
assert_eq!(after.unchanged("gone", b"v"), None);
assert_eq!(after.unchanged("moved", b"v"), None);
assert_eq!(after.entries.len(), 2, "dead entries are pruned on save");
cleanup(&layout);
}

#[cfg(unix)]
#[test]
fn save_drops_entries_behind_a_symlink() {
let layout = tmp("prune-symlink");
let key = local_key(&layout).unwrap();
let open = || {
ProtectedCache::open(
key,
layout.root.clone(),
Some(layout.protected_cache_path()),
)
};
std::fs::create_dir(layout.root.join("d")).unwrap();
std::fs::write(layout.root.join("d/inner"), b"v").unwrap();
std::fs::write(layout.root.join("file"), b"v").unwrap();
std::fs::write(layout.root.join("target"), b"v").unwrap();
let id = ObjectId::of(b"c");
let mut c = open();
c.record("d/inner", b"v", id);
c.record("file", b"v", id);
c.save().unwrap();
assert_eq!(open().entries.len(), 2);

// The file becomes a symlink to an existing file, and its parent
// directory becomes a symlink to a directory that still holds `inner`.
std::fs::remove_file(layout.root.join("file")).unwrap();
std::os::unix::fs::symlink("target", layout.root.join("file")).unwrap();
std::fs::rename(layout.root.join("d"), layout.root.join("real-d")).unwrap();
std::os::unix::fs::symlink("real-d", layout.root.join("d")).unwrap();
open().save().unwrap();
assert_eq!(
open().entries.len(),
0,
"the walker never reads through a symlink"
);
cleanup(&layout);
}

#[test]
fn ephemeral_cache_never_touches_disk() {
let layout = tmp("ephemeral");
Expand Down
Loading