#![warn(missing_docs)]
use std::collections::{BTreeMap, BTreeSet};
use std::fs::{self, File, TryLockError};
use std::hash::{BuildHasher as _, Hasher as _};
use std::io::{self, Read as _, Write as _};
use std::ops::Range;
use std::path::{Component, Path, PathBuf};
use std::sync::{Arc, Mutex, PoisonError, RwLock};
use std::time::SystemTime;
use skif_core::cache::content_digest;
use skif_core::{CacheNamespace, CacheStore};
const MAGIC: [u8; 8] = *b"SKIFSTOR";
const FORMAT: u32 = 2;
const EXTENSION: &str = "bin";
const LOCK_FILE: &str = ".lock";
const BASE_FILE: &str = "base";
const TAG_FILE: &str = "CACHEDIR.TAG";
const IGNORE_FILE: &str = ".gitignore";
const IGNORE_ALL: &str = "*\n";
const TEMPORARY_SUFFIX: &str = ".tmp";
const TEMPORARY_TRIES: usize = 8;
const FIRST_NAMESPACE: &str = "modules";
pub const DEFAULT_MAX_SIZE: u64 = 512 * 1024 * 1024;
const CACHEDIR_TAG: &str = "Signature: 8a477f597d28d172789f06886806bc55\n\
# This file is a cache directory tag created by skif.\n\
# For information about cache directory tags, see https://bford.info/cachedir/\n";
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct DiskStoreOptions {
pub max_size: u64,
pub write: bool,
pub evict_others: bool,
}
impl Default for DiskStoreOptions {
fn default() -> Self {
Self {
max_size: DEFAULT_MAX_SIZE,
write: true,
evict_others: true,
}
}
}
impl DiskStoreOptions {
#[must_use]
pub const fn with_max_size(mut self, max_size: u64) -> Self {
self.max_size = max_size;
self
}
#[must_use]
pub const fn with_write(mut self, write: bool) -> Self {
self.write = write;
self
}
#[must_use]
pub const fn with_evict_others(mut self, evict_others: bool) -> Self {
self.evict_others = evict_others;
self
}
}
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum StoreError {
#[error("cannot write {path}: {source}")]
Io {
path: PathBuf,
#[source]
source: io::Error,
},
#[error("{path} does not exist and skif does not create node_modules directories")]
NodeModules {
path: PathBuf,
},
#[error("the cache location is empty")]
EmptyLocation,
}
impl StoreError {
fn io(path: &Path, source: io::Error) -> Self {
Self::Io {
path: path.to_path_buf(),
source,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Finish {
Unchanged,
Written,
Contended,
ReadOnly,
}
type Record = ([u8; 32], Arc<[u8]>);
struct Pending {
name: &'static str,
schema: u32,
records: Vec<Record>,
}
#[derive(Default)]
struct Stored {
bytes: Vec<u8>,
index: BTreeMap<[u8; 32], Range<usize>>,
}
struct Namespace {
schema: u32,
stored: RwLock<Stored>,
fresh: Mutex<BTreeMap<[u8; 32], Arc<[u8]>>>,
used: Mutex<BTreeSet<[u8; 32]>>,
}
pub struct DiskStore {
location: PathBuf,
dir: PathBuf,
base: String,
options: DiskStoreOptions,
namespaces: Mutex<BTreeMap<&'static str, Arc<Namespace>>>,
notes: Mutex<Vec<String>>,
}
impl std::fmt::Debug for DiskStore {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("DiskStore")
.field("dir", &self.dir)
.field("options", &self.options)
.finish_non_exhaustive()
}
}
impl DiskStore {
#[must_use]
pub fn open(location: &Path, base: &Path, options: DiskStoreOptions) -> Self {
let base = base_identity(base);
let dir = location.join(base_dir_name(&base));
Self {
location: location.to_path_buf(),
dir,
base,
options,
namespaces: Mutex::new(BTreeMap::new()),
notes: Mutex::new(Vec::new()),
}
}
#[must_use]
pub fn location(&self) -> &Path {
&self.location
}
#[must_use]
pub fn dir(&self) -> &Path {
&self.dir
}
#[must_use]
pub const fn options(&self) -> DiskStoreOptions {
self.options
}
#[must_use]
pub fn take_notes(&self) -> Vec<String> {
std::mem::take(&mut *self.notes.lock().unwrap_or_else(PoisonError::into_inner))
}
fn note(&self, note: String) {
self.notes.lock().unwrap_or_else(PoisonError::into_inner).push(note);
}
fn namespace(&self, namespace: CacheNamespace) -> Option<Arc<Namespace>> {
if !CacheNamespace::is_valid_name(namespace.name) {
return None;
}
let mut namespaces = self.namespaces.lock().unwrap_or_else(PoisonError::into_inner);
if let Some(loaded) = namespaces.get(namespace.name)
&& loaded.schema == namespace.schema
{
return Some(Arc::clone(loaded));
}
let file = self.dir.join(namespace_file(namespace.name));
let limit = self.options.max_size.saturating_add(header_size(namespace.name));
let stored = match read_at_most(&file, limit) {
Ok(Some(bytes)) => decode(bytes, namespace).unwrap_or_else(|reason| {
self.note(format!("ignoring the cache file {}: {reason}", file.display()));
Stored::default()
}),
Ok(None) => {
self.note(format!(
"ignoring the cache file {}: larger than the cap of {} bytes",
file.display(),
self.options.max_size
));
Stored::default()
}
Err(error) if error.kind() == io::ErrorKind::NotFound => Stored::default(),
Err(error) => {
self.note(format!("ignoring the cache file {}: {error}", file.display()));
Stored::default()
}
};
let loaded = Arc::new(Namespace {
schema: namespace.schema,
stored: RwLock::new(stored),
fresh: Mutex::new(BTreeMap::new()),
used: Mutex::new(BTreeSet::new()),
});
namespaces.insert(namespace.name, Arc::clone(&loaded));
Some(loaded)
}
pub fn finish_run(&self) -> Result<Finish, StoreError> {
self.finish(true)
}
pub fn finish_failed_run(&self) -> Result<Finish, StoreError> {
self.finish(false)
}
fn finish(&self, prune: bool) -> Result<Finish, StoreError> {
let namespaces: Vec<(&'static str, Arc<Namespace>)> = self
.namespaces
.lock()
.unwrap_or_else(PoisonError::into_inner)
.iter()
.map(|(name, namespace)| (*name, Arc::clone(namespace)))
.collect();
let mut pending: Vec<Pending> = Vec::new();
for &(name, ref namespace) in &namespaces {
let used = std::mem::take(&mut *namespace.used.lock().unwrap_or_else(PoisonError::into_inner));
let fresh = std::mem::take(&mut *namespace.fresh.lock().unwrap_or_else(PoisonError::into_inner));
if used.is_empty() {
continue;
}
let mut stored = namespace.stored.write().unwrap_or_else(PoisonError::into_inner);
if fresh.is_empty() && (!prune || used.iter().eq(stored.index.keys())) {
continue;
}
let kept: BTreeSet<[u8; 32]> = if prune {
used
} else {
stored.index.keys().chain(fresh.keys()).copied().collect()
};
let records: Vec<Record> = kept
.iter()
.filter_map(|key| {
let value = match fresh.get(key) {
Some(value) => Arc::clone(value),
None => Arc::from(&stored.bytes[stored.index.get(key)?.clone()]),
};
Some((*key, value))
})
.collect();
*stored = encode(
name,
namespace.schema,
records.iter().map(|(key, value)| (key, &value[..])),
);
drop(stored);
pending.push(Pending {
name,
schema: namespace.schema,
records,
});
}
if pending.is_empty() {
return Ok(Finish::Unchanged);
}
if !self.options.write {
return Ok(Finish::ReadOnly);
}
self.write(pending)
}
fn write(&self, mut pending: Vec<Pending>) -> Result<Finish, StoreError> {
create_tagged(&self.location)?;
create_tagged(&self.dir)?;
let Some(_lock) = try_lock(&self.dir)? else {
return Ok(Finish::Contended);
};
remove_temporaries(&self.dir);
let base_file = self.dir.join(BASE_FILE);
if fs::read_to_string(&base_file).ok().as_deref() != Some(self.base.as_str()) {
write_atomically(&base_file, self.base.as_bytes())?;
}
pending.sort_by_key(|pending| (pending.name != FIRST_NAMESPACE, pending.name));
let others: u64 = self
.namespace_files()
.iter()
.filter(|(name, _)| !pending.iter().any(|pending| pending.name == name))
.map(|(_, size)| size)
.sum();
let mut budget = self.options.max_size.saturating_sub(others);
for Pending { name, schema, records } in &pending {
let mut kept = Vec::new();
let mut size = header_size(name);
for (key, value) in records {
let record = 32 + 4 + value.len() as u64;
if size + record > budget {
break;
}
size += record;
kept.push((key, &value[..]));
}
budget = budget.saturating_sub(size);
let encoded = encode(name, *schema, kept.into_iter());
write_atomically(&self.dir.join(namespace_file(name)), &encoded.bytes)?;
}
if self.options.evict_others {
self.enforce_cap();
}
Ok(Finish::Written)
}
fn namespace_files(&self) -> Vec<(String, u64)> {
let Ok(entries) = fs::read_dir(&self.dir) else {
return Vec::new();
};
entries
.filter_map(Result::ok)
.filter_map(|entry| {
let path = entry.path();
let name = path.file_stem()?.to_str()?.to_owned();
(path.extension()? == EXTENSION).then_some(())?;
Some((name, entry.metadata().ok()?.len()))
})
.collect()
}
fn enforce_cap(&self) {
let Ok(stores) = list_stores(&self.location) else {
return;
};
let mut total: u64 = stores.iter().map(|store| store.bytes).sum();
let mut others: Vec<&StoreDir> = stores
.iter()
.filter(|store| store.readable && store.dir != self.dir)
.collect();
others.sort_by_key(|store| (store.modified, store.dir.clone()));
for store in others {
if total <= self.options.max_size {
break;
}
let Ok(Some(lock)) = try_lock(&store.dir) else {
continue;
};
drop(lock);
if remove_store(&store.dir).is_ok() {
total = total.saturating_sub(store.bytes);
}
}
}
}
impl CacheStore for DiskStore {
fn get(&self, namespace: CacheNamespace, key: &[u8; 32]) -> Option<Arc<[u8]>> {
let loaded = self.namespace(namespace)?;
let fresh = loaded
.fresh
.lock()
.unwrap_or_else(PoisonError::into_inner)
.get(key)
.cloned();
let value = if let Some(value) = fresh {
value
} else {
let stored = loaded.stored.read().unwrap_or_else(PoisonError::into_inner);
let range = stored.index.get(key)?.clone();
Arc::from(&stored.bytes[range])
};
loaded.used.lock().unwrap_or_else(PoisonError::into_inner).insert(*key);
Some(value)
}
fn put(&self, namespace: CacheNamespace, key: &[u8; 32], value: Arc<[u8]>) {
let Some(loaded) = self.namespace(namespace) else {
return;
};
loaded
.fresh
.lock()
.unwrap_or_else(PoisonError::into_inner)
.insert(*key, value);
loaded.used.lock().unwrap_or_else(PoisonError::into_inner).insert(*key);
}
}
fn base_identity(base: &Path) -> String {
let Some(top) = base.ancestors().find(|dir| dir.join(".git").exists()) else {
return format!("path:{}", base.display());
};
let parts: Vec<String> = base.strip_prefix(top).map_or_default(|relative| {
relative
.components()
.filter_map(|component| match component {
Component::Normal(part) => Some(part.to_string_lossy().into_owned()),
_ => None,
})
.collect()
});
format!("git:{}:{}", common_git_dir(top).display(), parts.join("/"))
}
fn common_git_dir(top: &Path) -> PathBuf {
let dot_git = top.join(".git");
let git_dir = if dot_git.is_dir() {
dot_git
} else {
fs::read_to_string(&dot_git)
.ok()
.and_then(|text| {
let named = text.lines().next()?.strip_prefix("gitdir:")?.trim();
Some(top.join(named))
})
.unwrap_or(dot_git)
};
let common = fs::read_to_string(git_dir.join("commondir"))
.map_or_else(|_| git_dir.clone(), |named| git_dir.join(named.trim()));
fs::canonicalize(&common).unwrap_or(common)
}
fn base_dir_name(identity: &str) -> String {
use std::fmt::Write as _;
let mut name = String::with_capacity(32);
for byte in &content_digest(identity.as_bytes())[..16] {
let _ = write!(name, "{byte:02x}");
}
name
}
#[must_use]
pub fn base_key(base: &Path) -> String {
base_dir_name(&base_identity(base))
}
fn namespace_file(name: &str) -> String {
format!("{name}.{EXTENSION}")
}
#[must_use]
pub fn default_location(base: &Path, user_cache_dir: Option<&Path>) -> Option<PathBuf> {
let node_modules = base.join("node_modules");
if node_modules.is_dir() {
return Some(node_modules.join(".cache").join("skif"));
}
user_cache_dir.map(|dir| dir.join("skif"))
}
fn header_size(name: &str) -> u64 {
(MAGIC.len() + 4 + 1 + name.len().min(usize::from(u8::MAX)) + 4 + 4) as u64
}
fn encode<'a>(name: &str, schema: u32, records: impl Iterator<Item = (&'a [u8; 32], &'a [u8])>) -> Stored {
let name = &name.as_bytes()[..name.len().min(usize::from(u8::MAX))];
let mut bytes = Vec::new();
bytes.extend_from_slice(&MAGIC);
bytes.extend_from_slice(&FORMAT.to_le_bytes());
#[allow(clippy::cast_possible_truncation)]
bytes.push(name.len() as u8);
bytes.extend_from_slice(name);
bytes.extend_from_slice(&schema.to_le_bytes());
let count_at = bytes.len();
bytes.extend_from_slice(&0_u32.to_le_bytes());
let mut index = BTreeMap::new();
let mut count: u32 = 0;
for (key, value) in records {
let Ok(length) = u32::try_from(value.len()) else {
continue;
};
bytes.extend_from_slice(key);
bytes.extend_from_slice(&length.to_le_bytes());
let start = bytes.len();
bytes.extend_from_slice(value);
index.insert(*key, start..bytes.len());
count += 1;
}
bytes[count_at..count_at + 4].copy_from_slice(&count.to_le_bytes());
Stored { bytes, index }
}
struct Reader<'a> {
bytes: &'a [u8],
at: usize,
}
impl Reader<'_> {
fn take(&mut self, length: usize) -> Result<Range<usize>, &'static str> {
let end = self
.at
.checked_add(length)
.filter(|end| *end <= self.bytes.len())
.ok_or("truncated")?;
let range = self.at..end;
self.at = end;
Ok(range)
}
fn u32(&mut self) -> Result<u32, &'static str> {
let range = self.take(4)?;
let mut word = [0; 4];
word.copy_from_slice(&self.bytes[range]);
Ok(u32::from_le_bytes(word))
}
fn u8(&mut self) -> Result<u8, &'static str> {
let range = self.take(1)?;
Ok(self.bytes[range.start])
}
}
struct Header {
name: String,
schema: u32,
count: u32,
}
fn read_header(reader: &mut Reader<'_>) -> Result<Header, &'static str> {
if reader.bytes.get(reader.take(MAGIC.len())?) != Some(&MAGIC[..]) {
return Err("not a skif cache file");
}
if reader.u32()? != FORMAT {
return Err("another format");
}
let length = usize::from(reader.u8()?);
let name = reader.take(length)?;
let name = String::from_utf8_lossy(&reader.bytes[name]).into_owned();
let schema = reader.u32()?;
let count = reader.u32()?;
Ok(Header { name, schema, count })
}
fn decode(bytes: Vec<u8>, namespace: CacheNamespace) -> Result<Stored, &'static str> {
let mut reader = Reader { bytes: &bytes, at: 0 };
let header = read_header(&mut reader)?;
if header.name != namespace.name {
return Err("another namespace");
}
if header.schema != namespace.schema {
return Err("written under another schema");
}
let mut index = BTreeMap::new();
let mut last: Option<[u8; 32]> = None;
for _ in 0..header.count {
let mut key = [0; 32];
key.copy_from_slice(&bytes[reader.take(32)?]);
if last.is_some_and(|last| last >= key) {
return Err("records out of order");
}
last = Some(key);
let length = reader.u32()? as usize;
index.insert(key, reader.take(length)?);
}
if reader.at != bytes.len() {
return Err("bytes after the last record");
}
Ok(Stored { bytes, index })
}
fn create_tagged(dir: &Path) -> Result<(), StoreError> {
if dir.as_os_str().is_empty() {
return Err(StoreError::EmptyLocation);
}
if dir.is_dir() {
return Ok(());
}
let missing: Vec<&Path> = dir.ancestors().take_while(|ancestor| !ancestor.exists()).collect();
if let Some(node_modules) = missing
.iter()
.find(|ancestor| ancestor.file_name() == Some("node_modules".as_ref()))
{
return Err(StoreError::NodeModules {
path: node_modules.to_path_buf(),
});
}
fs::create_dir_all(dir).map_err(|error| StoreError::io(dir, error))?;
tag(dir);
Ok(())
}
fn tag(dir: &Path) {
for (name, content) in [(TAG_FILE, CACHEDIR_TAG), (IGNORE_FILE, IGNORE_ALL)] {
if let Ok(mut file) = create_new(&dir.join(name)) {
let _ = file.write_all(content.as_bytes());
}
}
}
fn read_at_most(file: &Path, limit: u64) -> io::Result<Option<Vec<u8>>> {
let handle = File::open(file)?;
if handle.metadata()?.len() > limit {
return Ok(None);
}
let mut bytes = Vec::new();
handle.take(limit.saturating_add(1)).read_to_end(&mut bytes)?;
Ok((bytes.len() as u64 <= limit).then_some(bytes))
}
fn try_lock(dir: &Path) -> Result<Option<File>, StoreError> {
let path = dir.join(LOCK_FILE);
let file = File::options()
.create(true)
.truncate(false)
.write(true)
.open(&path)
.map_err(|error| StoreError::io(&path, error))?;
match file.try_lock() {
Ok(()) => Ok(Some(file)),
Err(TryLockError::WouldBlock) => Ok(None),
Err(TryLockError::Error(error)) => Err(StoreError::io(&path, error)),
}
}
fn write_atomically(file: &Path, bytes: &[u8]) -> Result<(), StoreError> {
let written = create_temporary(file).and_then(|(mut handle, temporary)| {
let renamed = handle.write_all(bytes).and_then(|()| {
drop(handle);
fs::rename(&temporary, file)
});
if renamed.is_err() {
let _ = fs::remove_file(&temporary);
}
renamed
});
written.map_err(|error| StoreError::io(file, error))
}
fn create_temporary(file: &Path) -> io::Result<(File, PathBuf)> {
let mut last = io::Error::from(io::ErrorKind::AlreadyExists);
for _ in 0..TEMPORARY_TRIES {
let random = std::collections::hash_map::RandomState::new().build_hasher().finish();
let mut name = file.file_name().unwrap_or_default().to_os_string();
name.push(format!(".{random:016x}{TEMPORARY_SUFFIX}"));
let temporary = file.with_file_name(name);
match create_new(&temporary) {
Ok(handle) => return Ok((handle, temporary)),
Err(error) if error.kind() == io::ErrorKind::AlreadyExists => last = error,
Err(error) => return Err(error),
}
}
Err(last)
}
fn create_new(path: &Path) -> io::Result<File> {
File::options().write(true).create_new(true).open(path)
}
fn remove_temporaries(dir: &Path) {
let Ok(entries) = fs::read_dir(dir) else {
return;
};
for entry in entries.filter_map(Result::ok) {
let name = entry.file_name();
if is_temporary(&name.to_string_lossy()) && entry.file_type().is_ok_and(|kind| !kind.is_dir()) {
let _ = fs::remove_file(entry.path());
}
}
}
struct StoreDir {
dir: PathBuf,
bytes: u64,
modified: SystemTime,
readable: bool,
}
fn is_file(path: &Path) -> Option<bool> {
match fs::metadata(path) {
Ok(metadata) => Some(metadata.is_file()),
Err(error) if error.kind() == io::ErrorKind::NotFound => Some(false),
Err(_) => None,
}
}
fn list_stores(location: &Path) -> io::Result<Vec<StoreDir>> {
let mut stores = Vec::new();
for entry in fs::read_dir(location)?.filter_map(Result::ok) {
let name = entry.file_name();
if !entry.file_type().is_ok_and(|kind| kind.is_dir()) || !is_base_dir_name(&name.to_string_lossy()) {
continue;
}
let dir = entry.path();
let markers = [is_file(&dir.join(LOCK_FILE)), is_file(&dir.join(BASE_FILE))];
if markers == [Some(false), Some(false)] {
continue;
}
let mut store = StoreDir {
dir,
bytes: 0,
modified: SystemTime::UNIX_EPOCH,
readable: false,
};
if markers.contains(&Some(true))
&& let Ok(files) = fs::read_dir(&store.dir)
{
store.readable = true;
for file in files.filter_map(Result::ok) {
let Ok(metadata) = file.metadata() else { continue };
let name = file.file_name();
let name = name.to_string_lossy();
if metadata.is_file() && is_store_file(&name) && !is_temporary(&name) {
store.bytes += metadata.len();
if let Ok(time) = metadata.modified() {
store.modified = store.modified.max(time);
}
}
}
}
stores.push(store);
}
stores.sort_by(|a, b| a.dir.cmp(&b.dir));
Ok(stores)
}
fn temporary_of(name: &str) -> Option<&str> {
name.strip_suffix(TEMPORARY_SUFFIX)
.and_then(|rest| rest.rsplit_once('.'))
.filter(|(_, random)| !random.is_empty() && random.bytes().all(|byte| byte.is_ascii_hexdigit()))
.map(|(file, _)| file)
}
fn is_temporary(name: &str) -> bool {
temporary_of(name).is_some_and(is_store_file)
}
fn is_store_file(name: &str) -> bool {
if matches!(name, LOCK_FILE | BASE_FILE | TAG_FILE | IGNORE_FILE) {
return true;
}
let written = temporary_of(name).unwrap_or(name);
written == BASE_FILE
|| written
.strip_suffix(EXTENSION)
.and_then(|stem| stem.strip_suffix('.'))
.is_some_and(CacheNamespace::is_valid_name)
}
fn remove_store(dir: &Path) -> io::Result<()> {
for entry in fs::read_dir(dir)? {
let entry = entry?;
if entry.file_type()?.is_file() && is_store_file(&entry.file_name().to_string_lossy()) {
fs::remove_file(entry.path())?;
}
}
match fs::remove_dir(dir) {
Err(error) if error.kind() != io::ErrorKind::DirectoryNotEmpty => Err(error),
_ => Ok(()),
}
}
fn is_base_dir_name(name: &str) -> bool {
name.len() == 32
&& name
.bytes()
.all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct NamespaceStats {
pub name: String,
pub entries: Option<u64>,
pub bytes: u64,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct StoreStats {
pub key: String,
pub base: Option<String>,
pub namespaces: Vec<NamespaceStats>,
pub bytes: u64,
pub modified: Option<SystemTime>,
pub readable: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
#[non_exhaustive]
pub struct CacheStats {
pub stores: Vec<StoreStats>,
pub bytes: u64,
}
pub fn stats(location: &Path) -> io::Result<CacheStats> {
let stores = match list_stores(location) {
Ok(stores) => stores,
Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(CacheStats::default()),
Err(error) => return Err(error),
};
let mut stats = CacheStats::default();
for store in stores {
let key = store
.dir
.file_name()
.map_or_default(|name| name.to_string_lossy().into_owned());
let base = fs::read_to_string(store.dir.join(BASE_FILE)).ok();
let mut namespaces = Vec::new();
let entries = fs::read_dir(&store.dir).into_iter().flatten();
for entry in entries.filter_map(Result::ok) {
let path = entry.path();
if path.extension().is_none_or(|extension| extension != EXTENSION) {
continue;
}
let Some(name) = path.file_stem().map(|stem| stem.to_string_lossy().into_owned()) else {
continue;
};
let bytes = entry.metadata().map_or(0, |metadata| metadata.len());
namespaces.push(NamespaceStats {
entries: header_count(&path, &name),
name,
bytes,
});
}
namespaces.sort_by(|a, b| a.name.cmp(&b.name));
stats.bytes += store.bytes;
stats.stores.push(StoreStats {
key,
base,
namespaces,
bytes: store.bytes,
modified: (store.modified != SystemTime::UNIX_EPOCH).then_some(store.modified),
readable: store.readable,
});
}
Ok(stats)
}
fn header_count(path: &Path, name: &str) -> Option<u64> {
let mut head = Vec::new();
File::open(path)
.ok()?
.take(header_size(name) + 8)
.read_to_end(&mut head)
.ok()?;
let header = read_header(&mut Reader { bytes: &head, at: 0 }).ok()?;
(header.name == name).then_some(u64::from(header.count))
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub struct CleanReport {
pub stores: usize,
pub bytes: u64,
}
pub fn clean(location: &Path, base: Option<&Path>) -> io::Result<CleanReport> {
let stores = match list_stores(location) {
Ok(stores) => stores,
Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(CleanReport::default()),
Err(error) => return Err(error),
};
let only = base.map(|base| location.join(base_key(base)));
let mut report = CleanReport::default();
let mut failed: Option<io::Error> = None;
for store in stores {
if only.as_ref().is_some_and(|only| *only != store.dir) {
continue;
}
match remove_store(&store.dir) {
Ok(()) => {
report.stores += 1;
report.bytes += store.bytes;
}
Err(error) => failed = failed.or(Some(error)),
}
}
if let Some(error) = failed {
return Err(error);
}
let leftover: Vec<String> = fs::read_dir(location)?
.filter_map(Result::ok)
.map(|entry| entry.file_name().to_string_lossy().into_owned())
.collect();
let own_tags = fs::read_to_string(location.join(TAG_FILE)).is_ok_and(|tag| tag == CACHEDIR_TAG)
&& fs::read_to_string(location.join(IGNORE_FILE)).is_ok_and(|ignore| ignore == IGNORE_ALL);
if own_tags && leftover.iter().all(|name| name == TAG_FILE || name == IGNORE_FILE) {
for name in leftover {
fs::remove_file(location.join(name))?;
}
fs::remove_dir(location)?;
}
Ok(report)
}
const _: () = {
fn assert_send_sync<T: Send + Sync>() {}
let _ = assert_send_sync::<DiskStore>;
let _ = assert_send_sync::<StoreError>;
};
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests;