use std::collections::HashSet;
use std::path::Path;
use std::time::{Duration, SystemTime};
use anyhow::Context;
use super::OciStore;
pub const GC_GRACE_PERIOD: Duration = Duration::from_secs(60 * 60);
#[derive(Debug, Default, Clone, Copy)]
pub struct GcStats {
pub count: usize,
pub bytes: u64,
}
pub fn referenced_digests(store: &OciStore) -> anyhow::Result<HashSet<String>> {
let mut live = HashSet::new();
for desc in store.list_refs_strict()? {
live.insert(desc.digest.clone());
let manifest = store
.read_manifest(&desc.digest)
.with_context(|| format!("read manifest {} for GC reference scan", desc.digest))?;
live.insert(manifest.config.digest.clone());
live.extend(manifest.layers.iter().map(|l| l.digest.clone()));
}
Ok(live)
}
pub fn prune_blobs(
store_root: &Path,
live: &HashSet<String>,
grace: Duration,
) -> anyhow::Result<GcStats> {
let blobs_dir = store_root.join("blobs").join("sha256");
let mut stats = GcStats::default();
let entries = match std::fs::read_dir(&blobs_dir) {
Ok(e) => e,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(stats),
Err(e) => return Err(e).with_context(|| format!("read {}", blobs_dir.display())),
};
for entry in entries {
let Ok(entry) = entry else { continue };
let path = entry.path();
let Some(name) = path.file_name().and_then(|n| n.to_str()) else {
continue;
};
if name.starts_with("tmp-") || name.ends_with(".tmp") {
continue;
}
if live.contains(&format!("sha256:{name}")) {
continue;
}
if !is_older_than(&path, grace) {
continue;
}
let size = freed_file_size(&path);
if let Err(e) = std::fs::remove_file(&path) {
eprintln!("[llmman] couldn't remove unreferenced blob {name}: {e:#}");
continue;
}
stats.count += 1;
stats.bytes += size;
}
Ok(stats)
}
pub fn prune_cache(
cache_path: &Path,
live: &HashSet<String>,
grace: Duration,
) -> anyhow::Result<GcStats> {
let live_hex: HashSet<&str> = live
.iter()
.filter_map(|d| d.strip_prefix("sha256:"))
.collect();
let mut stats = GcStats::default();
let entries = match std::fs::read_dir(cache_path) {
Ok(e) => e,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(stats),
Err(e) => return Err(e).with_context(|| format!("read {}", cache_path.display())),
};
for entry in entries {
let Ok(entry) = entry else { continue };
let path = entry.path();
if !entry.file_type().map(|t| t.is_dir()).unwrap_or(false) {
continue;
}
let Some(name) = path.file_name().and_then(|n| n.to_str()) else {
continue;
};
if live_hex.contains(name) {
continue;
}
if !is_older_than(&path, grace) {
continue;
}
let size = dir_size(&path);
if let Err(e) = std::fs::remove_dir_all(&path) {
eprintln!("[llmman] couldn't remove unreferenced cache dir {name}: {e:#}");
continue;
}
stats.count += 1;
stats.bytes += size;
}
Ok(stats)
}
fn is_older_than(path: &Path, grace: Duration) -> bool {
if grace.is_zero() {
return true;
}
let Ok(modified) = std::fs::metadata(path).and_then(|m| m.modified()) else {
return false;
};
SystemTime::now()
.duration_since(modified)
.is_ok_and(|age| age >= grace)
}
fn dir_size(dir: &Path) -> u64 {
let Ok(entries) = std::fs::read_dir(dir) else {
return 0;
};
entries.flatten().map(|e| freed_file_size(&e.path())).sum()
}
fn freed_file_size(path: &Path) -> u64 {
let Ok(meta) = std::fs::symlink_metadata(path) else {
return 0;
};
if meta.is_file() && !has_multiple_links(&meta) {
meta.len()
} else {
0
}
}
#[cfg(unix)]
fn has_multiple_links(meta: &std::fs::Metadata) -> bool {
use std::os::unix::fs::MetadataExt as _;
meta.nlink() > 1
}
#[cfg(not(unix))]
fn has_multiple_links(_meta: &std::fs::Metadata) -> bool {
false
}
pub fn noprune_from_env() -> bool {
crate::env_flag_set("LLMMAN_NOPRUNE")
}
#[cfg(test)]
mod tests {
use super::*;
fn temp_dir(name: &str) -> std::path::PathBuf {
std::env::temp_dir().join(format!(
"llmman-gc-test-{name}-{}-{}",
std::process::id(),
SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_nanos()
))
}
#[test]
fn prune_blobs_removes_only_unreferenced_blobs() {
let root = temp_dir("prune-blobs");
let blobs = root.join("blobs").join("sha256");
std::fs::create_dir_all(&blobs).unwrap();
std::fs::write(blobs.join("aaaa"), b"referenced").unwrap();
std::fs::write(blobs.join("bbbb"), b"orphan").unwrap();
std::fs::write(blobs.join("tmp-123"), b"in-progress").unwrap();
let mut live = HashSet::new();
live.insert("sha256:aaaa".to_string());
let stats = prune_blobs(&root, &live, Duration::ZERO).unwrap();
assert_eq!(stats.count, 1);
assert!(blobs.join("aaaa").exists(), "referenced blob must survive");
assert!(!blobs.join("bbbb").exists(), "orphan blob must be removed");
assert!(
blobs.join("tmp-123").exists(),
"in-progress temp file must be left to repair's sweep"
);
std::fs::remove_dir_all(&root).unwrap();
}
#[test]
#[cfg(unix)]
fn prune_blobs_respects_the_grace_window() {
let root = temp_dir("prune-blobs-grace");
let blobs = root.join("blobs").join("sha256");
std::fs::create_dir_all(&blobs).unwrap();
let fresh = blobs.join("cccc");
let stale = blobs.join("dddd");
std::fs::write(&fresh, b"just written by an in-flight pull").unwrap();
std::fs::write(&stale, b"long-abandoned orphan").unwrap();
let old = SystemTime::now() - GC_GRACE_PERIOD - Duration::from_secs(60);
filetime_set(&stale, old);
let live = HashSet::new(); let stats = prune_blobs(&root, &live, GC_GRACE_PERIOD).unwrap();
assert_eq!(stats.count, 1);
assert!(
fresh.exists(),
"a fresh unreferenced blob must survive — it may be a live pull's untagged layer"
);
assert!(!stale.exists(), "a stale unreferenced blob must be swept");
std::fs::remove_dir_all(&root).unwrap();
}
#[test]
fn prune_cache_removes_only_unreferenced_dirs() {
let cache = temp_dir("prune-cache");
std::fs::create_dir_all(cache.join("aaaa")).unwrap();
std::fs::write(cache.join("aaaa").join("model.gguf"), b"kept").unwrap();
std::fs::create_dir_all(cache.join("bbbb")).unwrap();
std::fs::write(cache.join("bbbb").join("model.gguf"), b"orphan").unwrap();
let mut live = HashSet::new();
live.insert("sha256:aaaa".to_string());
let stats = prune_cache(&cache, &live, Duration::ZERO).unwrap();
assert_eq!(stats.count, 1);
assert!(cache.join("aaaa").exists(), "referenced cache dir survives");
assert!(!cache.join("bbbb").exists(), "orphan cache dir removed");
std::fs::remove_dir_all(&cache).unwrap();
}
#[test]
#[cfg(unix)]
fn prune_counts_hardlinked_blob_and_cache_bytes_once() {
let root = temp_dir("prune-hardlinked-cache");
let blobs = root.join("blobs").join("sha256");
let cache = root.join("cache");
std::fs::create_dir_all(&blobs).unwrap();
std::fs::create_dir_all(cache.join("bbbb")).unwrap();
let blob = blobs.join("aaaa");
let cache_file = cache.join("bbbb").join("model.safetensors");
let weights = b"complete-weights-bytes";
std::fs::write(&blob, weights).unwrap();
std::fs::hard_link(&blob, &cache_file).unwrap();
let live = HashSet::new();
let blob_stats = prune_blobs(&root, &live, Duration::ZERO).unwrap();
let cache_stats = prune_cache(&cache, &live, Duration::ZERO).unwrap();
assert_eq!(blob_stats.count, 1);
assert_eq!(cache_stats.count, 1);
assert_eq!(blob_stats.bytes + cache_stats.bytes, weights.len() as u64);
std::fs::remove_dir_all(&root).unwrap();
}
#[test]
fn referenced_digests_aborts_on_an_unreadable_reference() {
let root = temp_dir("referenced-digests-strict");
let store = OciStore::open(&root).unwrap();
let cfg = store
.write_blob("application/vnd.cncf.model.config.v1+json", b"{}")
.unwrap();
let manifest = crate::storage::oci::Manifest {
schema_version: 2,
media_type: "application/vnd.oci.image.manifest.v1+json".into(),
artifact_type: None,
config: cfg,
layers: vec![],
annotations: None,
};
let desc = store.write_manifest(&manifest).unwrap();
store.tag(desc, "hf.co/ai/healthy:latest").unwrap();
let corrupt = root
.join("manifests")
.join("hf.co")
.join("ai")
.join("broken")
.join("latest");
std::fs::create_dir_all(corrupt.parent().unwrap()).unwrap();
std::fs::write(&corrupt, b"not json").unwrap();
assert!(
referenced_digests(&store).is_err(),
"an unparsable reference must abort the live-set scan, not be skipped"
);
std::fs::remove_dir_all(&root).unwrap();
}
#[test]
fn prune_is_a_no_op_on_missing_directories() {
let root = temp_dir("missing");
let live = HashSet::new();
assert_eq!(prune_blobs(&root, &live, Duration::ZERO).unwrap().count, 0);
assert_eq!(
prune_cache(&root.join("cache"), &live, Duration::ZERO)
.unwrap()
.count,
0
);
}
#[cfg(unix)]
fn filetime_set(path: &Path, when: SystemTime) {
use std::os::unix::io::AsRawFd;
let file = std::fs::File::open(path).unwrap();
let d = when.duration_since(SystemTime::UNIX_EPOCH).unwrap();
let tv = libc::timeval {
tv_sec: d.as_secs() as _,
tv_usec: d.subsec_micros() as _,
};
let times = [tv, tv];
unsafe {
libc::futimes(file.as_raw_fd(), times.as_ptr());
}
}
}