use alloc::fmt;
use core::str::FromStr;
use std::{
fs,
path::{
Path,
PathBuf,
},
time::SystemTime,
};
use crate::{
data::resources::{
RESOURCES_DIR,
ResourceCache,
cache::FETCHING_DIR,
},
errors::{
BunsenError,
BunsenResult,
sys_at,
},
};
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CachedFile {
pub name: String,
pub pin: String,
pub file: String,
pub path: PathBuf,
pub bytes: u64,
pub partial: bool,
pub created: Option<SystemTime>,
pub last_used: Option<SystemTime>,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct CacheSelector {
pub prefix: Option<String>,
}
impl CacheSelector {
pub fn matches(
&self,
name: &str,
) -> bool {
self.prefix.as_deref().is_none_or(|p| {
name == p
|| name
.strip_prefix(p)
.is_some_and(|rest| rest.starts_with('/'))
})
}
}
impl FromStr for CacheSelector {
type Err = BunsenError;
fn from_str(s: &str) -> BunsenResult<Self> {
if s.is_empty() {
return Ok(Self::default());
}
if s.split('/').any(str::is_empty) {
return Err(BunsenError::illegal(format!(
"cache selector {s:?}: expected a `/`-separated name, with no empty segment"
)));
}
Ok(Self {
prefix: Some(s.to_string()),
})
}
}
impl fmt::Display for CacheSelector {
fn fmt(
&self,
f: &mut fmt::Formatter<'_>,
) -> fmt::Result {
match &self.prefix {
None => write!(f, "every resource"),
Some(prefix) => write!(f, "{prefix}"),
}
}
}
pub(super) fn is_sha256(segment: &str) -> bool {
segment.len() == 64
&& segment
.bytes()
.all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))
}
pub(super) fn files_under(dir: &Path) -> BunsenResult<Vec<(PathBuf, fs::Metadata)>> {
let mut out = Vec::new();
if !dir.is_dir() {
return Ok(out);
}
let mut stack = vec![dir.to_path_buf()];
while let Some(at) = stack.pop() {
for entry in fs::read_dir(&at).map_err(sys_at("read the directory", &at))? {
let entry = entry.map_err(sys_at("read the directory", &at))?;
let path = entry.path();
let meta = entry
.metadata()
.map_err(sys_at("read the metadata of", &path))?;
if meta.is_dir() {
stack.push(path);
} else if meta.is_file() {
out.push((path, meta));
}
}
}
out.sort_by(|a, b| a.0.cmp(&b.0));
Ok(out)
}
impl ResourceCache {
pub fn resources_dir(&self) -> PathBuf {
self.cache_dir().join(RESOURCES_DIR)
}
pub fn cached_files(
&self,
selector: &CacheSelector,
) -> BunsenResult<Vec<CachedFile>> {
let root = self.resources_dir();
let mut out = Vec::new();
for (path, meta) in files_under(&root)? {
let Ok(relative) = path.strip_prefix(&root) else {
continue;
};
let Some(segments) = relative
.components()
.map(|c| c.as_os_str().to_str().map(str::to_string))
.collect::<Option<Vec<String>>>()
else {
continue;
};
let at = match segments
.iter()
.position(|s| is_sha256(s) || s == FETCHING_DIR)
{
Some(at) if at > 0 => at,
_ => continue,
};
if at + 1 >= segments.len() {
continue;
}
let entry = segments[..at].join("/");
if !selector.matches(&entry) {
continue;
}
let name = segments[at + 1..].join("/");
let (file, partial) = match name.strip_suffix(".partial") {
Some(file) => (file.to_string(), true),
None => (name, false),
};
out.push(CachedFile {
name: entry,
pin: segments[at].clone(),
file,
path,
bytes: meta.len(),
partial,
created: meta.created().ok(),
last_used: meta.modified().ok(),
});
}
Ok(out)
}
pub fn remove_cached(
&self,
files: &[CachedFile],
) -> BunsenResult<u64> {
let root = self.resources_dir();
if let Some(stray) = files.iter().find(|f| !f.path.starts_with(&root)) {
return Err(BunsenError::illegal(format!(
"{} is not in the resource cache at {}",
stray.path.display(),
root.display()
)));
}
let mut freed = 0;
for file in files {
fs::remove_file(&file.path).map_err(sys_at("remove", &file.path))?;
freed += file.bytes;
let mut dir = file.path.parent();
while let Some(d) = dir.filter(|d| *d != root && d.starts_with(&root)) {
if fs::remove_dir(d).is_err() {
break;
}
dir = d.parent();
}
}
Ok(freed)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::data::{
cache::BunsenDiskCacheOptions,
resources::ResourceCacheOptions,
};
fn cache_in(dir: &Path) -> ResourceCache {
ResourceCache::new(
ResourceCacheOptions::default()
.with_disk(
BunsenDiskCacheOptions::default()
.with_cache_dir(Some(dir.join("cache")))
.without_transfer_observers(),
)
.with_offline(true),
)
.unwrap()
}
fn put(
cache: &ResourceCache,
rel: &str,
bytes: usize,
) {
let path = cache.resources_dir().join(rel);
fs::create_dir_all(path.parent().unwrap()).unwrap();
fs::write(path, vec![0u8; bytes]).unwrap();
}
#[test]
fn test_selector() {
assert_eq!(
"".parse::<CacheSelector>().unwrap(),
CacheSelector::default()
);
let qwen: CacheSelector = "qwen".parse().unwrap();
assert_eq!(qwen.prefix.as_deref(), Some("qwen"));
assert!(qwen.matches("qwen") && qwen.matches("qwen/qwen3/qwen3-0.6b/checkpoint"));
assert!(!qwen.matches("qwen3") && !qwen.matches("openai/whisper/base/checkpoint"));
let build: CacheSelector = "openai/whisper/base".parse().unwrap();
assert!(build.matches("openai/whisper/base/checkpoint"));
assert!(!build.matches("openai/whisper/base.en/checkpoint"));
assert_eq!(build.to_string(), "openai/whisper/base");
for bad in ["/b", "a/", "a//b"] {
assert!(bad.parse::<CacheSelector>().is_err(), "{bad}");
}
}
#[test]
fn test_list_and_remove() {
let dir = tempfile::tempdir().unwrap();
let cache = cache_in(dir.path());
assert!(
cache
.cached_files(&CacheSelector::default())
.unwrap()
.is_empty()
);
let a = "a".repeat(64);
let b = "b".repeat(64);
let c = "c".repeat(64);
let d = "d".repeat(64);
put(
&cache,
&format!("openai/whisper/tiny/checkpoint/{a}/tiny.pt"),
3,
);
put(
&cache,
&format!("openai/whisper/base/checkpoint/{b}/base.pt.partial"),
5,
);
put(&cache, &format!("hf/o/r/config/{c}/config.json"), 7);
put(
&cache,
&format!("laya/laya/laya/encoder_config/{d}/encoder/config.json"),
11,
);
put(&cache, "openai/stray.txt", 1);
put(
&cache,
"hf/01-ai/yi/listing/.fetching/01-ai--yi--main.json.partial",
13,
);
let all = cache.cached_files(&CacheSelector::default()).unwrap();
let names: Vec<(&str, &str, bool)> = all
.iter()
.map(|f| (f.name.as_str(), f.file.as_str(), f.partial))
.collect();
assert_eq!(
names,
[
("hf/01-ai/yi/listing", "01-ai--yi--main.json", true),
("hf/o/r/config", "config.json", false),
(
"laya/laya/laya/encoder_config",
"encoder/config.json",
false
),
("openai/whisper/base/checkpoint", "base.pt", true),
("openai/whisper/tiny/checkpoint", "tiny.pt", false),
]
);
assert_eq!(all[0].pin, FETCHING_DIR);
assert_eq!(all[1].pin, c);
assert_eq!(all[3].bytes, 5);
assert_eq!(all[4].pin, a);
let whisper = cache
.cached_files(&"openai/whisper".parse().unwrap())
.unwrap();
assert_eq!(whisper.len(), 2);
assert_eq!(cache.remove_cached(&whisper).unwrap(), 8);
assert!(!cache.resources_dir().join("openai/whisper").exists());
assert!(cache.resources_dir().join("openai/stray.txt").exists());
assert!(cache.resources_dir().join("hf/o/r/config").exists());
let hf = cache.cached_files(&"hf".parse().unwrap()).unwrap();
assert_eq!(cache.remove_cached(&hf).unwrap(), 20);
assert!(!cache.resources_dir().join("hf").exists());
assert_eq!(
cache.cached_files(&CacheSelector::default()).unwrap().len(),
1
);
let mut outside = all[1].clone();
outside.path = dir.path().join("elsewhere");
assert!(cache.remove_cached(&[outside]).is_err());
}
}