use std::collections::HashSet;
use std::path::PathBuf;
use std::sync::{Arc, Mutex, Weak};
use crate::error::{BlobError, Result};
use crate::hash::Hash;
use crate::paths::fsync_dir;
use crate::store::ContentStore;
use crate::tree::TreeIndex;
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct TagRecord {
pub version: u16,
pub root: Hash,
pub chunks: Vec<Hash>,
}
#[derive(Debug)]
pub struct SnapshotTags {
dir: PathBuf,
}
impl SnapshotTags {
pub fn open(dir: impl Into<PathBuf>) -> std::io::Result<Self> {
let dir = dir.into();
std::fs::create_dir_all(&dir)?;
Ok(SnapshotTags { dir })
}
fn path(&self, name: &str) -> Result<PathBuf> {
crate::manifest::validate_id(name)
.map_err(|_| BlobError::Usage(format!("invalid tag name {name:?}")))?;
Ok(self.dir.join(format!("{name}.tag")))
}
pub fn set(&self, name: &str, index: &TreeIndex) -> Result<()> {
use std::io::Write;
let path = self.path(name)?;
let record = TagRecord {
version: crate::wire::WIRE_VERSION,
root: index.root_hash,
chunks: index.needed_chunks(),
};
let payload = crate::wire::encode(&record)?;
let mut tmp = tempfile::NamedTempFile::new_in(&self.dir)?;
tmp.write_all(&payload)?;
tmp.as_file().sync_all()?;
tmp.persist(&path).map_err(|e| e.error)?;
fsync_dir(&self.dir)?;
Ok(())
}
pub fn get(&self, name: &str) -> Result<Option<TagRecord>> {
let path = self.path(name)?;
match std::fs::read(&path) {
Ok(bytes) => Ok(Some(crate::wire::decode(&bytes)?)),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
Err(e) => Err(e.into()),
}
}
pub fn remove(&self, name: &str) -> Result<bool> {
let path = self.path(name)?;
match std::fs::remove_file(&path) {
Ok(()) => Ok(true),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(false),
Err(e) => Err(e.into()),
}
}
pub fn list(&self) -> std::io::Result<Vec<String>> {
let mut out = Vec::new();
for entry in std::fs::read_dir(&self.dir)? {
let name = entry?.file_name();
if let Some(n) = name.to_str().and_then(|n| n.strip_suffix(".tag")) {
out.push(n.to_string());
}
}
out.sort();
Ok(out)
}
pub fn live_set(&self) -> Result<HashSet<Hash>> {
let mut live = HashSet::new();
for name in self.list()? {
if let Some(record) = self.get(&name)? {
live.extend(record.chunks);
}
}
Ok(live)
}
}
#[derive(Default, Debug)]
pub struct TempTags {
sets: Mutex<Vec<Weak<HashSet<Hash>>>>,
}
#[derive(Debug)]
pub struct TempTag {
_set: Arc<HashSet<Hash>>,
}
impl TempTags {
pub fn new() -> Self {
Self::default()
}
#[must_use = "protection lasts only as long as the returned TempTag is held; \
dropping it immediately protects nothing"]
pub fn protect(&self, hashes: impl IntoIterator<Item = Hash>) -> TempTag {
let set = Arc::new(hashes.into_iter().collect::<HashSet<_>>());
let mut sets = self.sets.lock().unwrap_or_else(|e| e.into_inner());
sets.retain(|w| w.strong_count() > 0);
sets.push(Arc::downgrade(&set));
TempTag { _set: set }
}
pub fn live_set(&self) -> HashSet<Hash> {
let mut sets = self.sets.lock().unwrap_or_else(|e| e.into_inner());
sets.retain(|w| w.strong_count() > 0);
let mut live = HashSet::new();
for w in sets.iter() {
if let Some(set) = w.upgrade() {
live.extend(set.iter().copied());
}
}
live
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct GcStats {
pub kept: u64,
pub removed: u64,
}
pub fn sweep<'a>(
store: &dyn ContentStore,
tags: &SnapshotTags,
temps: &TempTags,
extra_roots: impl IntoIterator<Item = &'a TreeIndex>,
) -> Result<GcStats> {
let mut live = tags.live_set()?;
live.extend(temps.live_set());
for index in extra_roots {
live.extend(index.needed_chunks());
}
let mut stats = GcStats {
kept: 0,
removed: 0,
};
for hash in store.hashes()? {
if live.contains(&hash) {
stats.kept += 1;
} else if store.remove(&hash)? {
stats.removed += 1;
}
}
Ok(stats)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::chunk::CdcParams;
use crate::store::{DirStore, MemoryStore};
use crate::tree::build_tree;
fn snapshot(dir: &std::path::Path, store: &dyn ContentStore, id: &str) -> TreeIndex {
build_tree(dir, id, &CdcParams::default(), store).unwrap()
}
#[test]
fn tags_persist_across_reopen() {
let src = tempfile::tempdir().unwrap();
std::fs::write(src.path().join("f.txt"), b"tagged content").unwrap();
let store = MemoryStore::new();
let index = snapshot(src.path(), &store, "snap");
let tag_dir = tempfile::tempdir().unwrap();
let tags = SnapshotTags::open(tag_dir.path()).unwrap();
tags.set("current", &index).unwrap();
assert_eq!(tags.list().unwrap(), vec!["current"]);
let tags2 = SnapshotTags::open(tag_dir.path()).unwrap();
let record = tags2.get("current").unwrap().unwrap();
assert_eq!(record.root, index.root_hash);
assert_eq!(record.chunks, index.needed_chunks());
assert_eq!(
tags2.live_set().unwrap(),
index.needed_chunks().into_iter().collect()
);
assert!(tags2.remove("current").unwrap());
assert!(!tags2.remove("current").unwrap());
assert!(tags2.list().unwrap().is_empty());
}
#[test]
fn invalid_tag_names_rejected() {
let tag_dir = tempfile::tempdir().unwrap();
let tags = SnapshotTags::open(tag_dir.path()).unwrap();
for bad in ["", "../escape", "a/b", "a*"] {
assert!(tags.list().unwrap().is_empty());
assert!(tags.get(bad).is_err(), "tag name {bad:?} must be rejected");
}
}
#[test]
fn sweep_keeps_tagged_removes_garbage() {
let store_dir = tempfile::tempdir().unwrap();
let store = DirStore::open(store_dir.path()).unwrap();
let src = tempfile::tempdir().unwrap();
std::fs::write(src.path().join("keep.txt"), b"keep me around").unwrap();
let index = snapshot(src.path(), &store, "keep");
for junk in [&b"orphan 1"[..], b"orphan 2", b"orphan 3"] {
store.put(&Hash::of(junk), junk).unwrap();
}
let tags = SnapshotTags::open(store_dir.path().join("tags")).unwrap();
tags.set("keep", &index).unwrap();
let temps = TempTags::new();
let stats = sweep(&store, &tags, &temps, []).unwrap();
assert_eq!(stats.kept as usize, index.needed_chunks().len());
assert_eq!(stats.removed, 3);
for h in index.needed_chunks() {
assert!(store.has(&h).unwrap(), "tagged chunk must survive");
}
assert!(!store.has(&Hash::of(b"orphan 1")).unwrap());
}
#[test]
fn temp_tag_protects_until_dropped() {
let store = MemoryStore::new();
let h = Hash::of(b"in flight");
store.put(&h, b"in flight").unwrap();
let tag_dir = tempfile::tempdir().unwrap();
let tags = SnapshotTags::open(tag_dir.path()).unwrap();
let temps = TempTags::new();
let guard = temps.protect([h]);
let stats = sweep(&store, &tags, &temps, []).unwrap();
assert_eq!((stats.kept, stats.removed), (1, 0));
assert!(store.has(&h).unwrap());
drop(guard);
let stats = sweep(&store, &tags, &temps, []).unwrap();
assert_eq!((stats.kept, stats.removed), (0, 1));
assert!(!store.has(&h).unwrap());
}
#[test]
fn extra_roots_protect_registered_snapshots() {
let store = MemoryStore::new();
let src = tempfile::tempdir().unwrap();
std::fs::write(src.path().join("live.txt"), b"registered on a server").unwrap();
let index = snapshot(src.path(), &store, "live");
let tag_dir = tempfile::tempdir().unwrap();
let tags = SnapshotTags::open(tag_dir.path()).unwrap();
let stats = sweep(&store, &tags, &TempTags::new(), [&index]).unwrap();
assert_eq!(stats.removed, 0);
assert_eq!(stats.kept as usize, index.needed_chunks().len());
}
}