mod checksums;
mod config_diff;
mod management;
mod operations;
mod validity;
use anyhow::{Context, Result};
use std::fs;
use std::path::{Path, PathBuf};
use serde::{Deserialize, Serialize};
use redb::{Database, ReadableDatabase, TableDefinition};
use crate::checksum;
use crate::config::RestoreMethod;
use crate::remote_cache::RemoteCache;
const CHECKSUM_PREFIX_LEN: usize = 2;
fn walk_files(dir: &Path) -> Vec<PathBuf> {
let mut result = Vec::new();
let mut stack = vec![dir.to_path_buf()];
while let Some(current) = stack.pop() {
if let Ok(entries) = fs::read_dir(¤t) {
for entry in entries.flatten() {
let path = entry.path();
if path.is_dir() {
stack.push(path);
} else if path.is_file() {
result.push(path);
}
}
}
}
result
}
const RSBUILD_DIR: &str = ".rsconstruct";
const OBJECTS_DIR: &str = "objects";
const DB_FILE: &str = "db.redb";
const CACHE_TABLE: TableDefinition<&str, &[u8]> = TableDefinition::new("cache");
const CONFIGS_TABLE: TableDefinition<&str, &[u8]> = TableDefinition::new("processor_configs");
const MTIME_TABLE: TableDefinition<&str, &[u8]> = TableDefinition::new("mtime_cache");
#[derive(Serialize, Deserialize)]
struct MtimeEntry {
mtime_secs: i64,
mtime_nanos: u32,
checksum: String,
}
#[derive(Debug)]
pub enum RebuildReason {
NoCacheEntry,
InputsChanged,
OutputMissing(String),
Force,
}
impl std::fmt::Display for RebuildReason {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
RebuildReason::NoCacheEntry => write!(f, "no cache entry"),
RebuildReason::InputsChanged => write!(f, "inputs changed"),
RebuildReason::OutputMissing(path) => write!(f, "output missing: {}", path),
RebuildReason::Force => write!(f, "forced"),
}
}
}
#[derive(Debug)]
pub enum ExplainAction {
Skip,
Restore(RebuildReason),
Rebuild(RebuildReason),
}
impl std::fmt::Display for ExplainAction {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ExplainAction::Skip => write!(f, "SKIP (inputs unchanged)"),
ExplainAction::Restore(reason) => write!(f, "RESTORE ({})", reason),
ExplainAction::Rebuild(reason) => write!(f, "BUILD ({})", reason),
}
}
}
pub struct ObjectStore {
objects_dir: PathBuf,
db: Database,
restore_method: RestoreMethod,
compression: bool,
remote: Option<Box<dyn RemoteCache>>,
remote_push: bool,
remote_pull: bool,
mtime_check: bool,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
struct CacheEntry {
input_checksum: String,
outputs: Vec<OutputEntry>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
struct OutputEntry {
path: String,
checksum: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
mode: Option<u32>,
}
#[derive(Debug, Default, Serialize)]
pub struct ProcessorCacheStats {
pub entry_count: usize,
pub output_count: usize,
pub output_bytes: u64,
}
#[derive(Debug, Serialize)]
pub struct CacheListEntry {
pub cache_key: String,
pub input_checksum: String,
pub outputs: Vec<CacheListOutput>,
}
#[derive(Debug, Serialize)]
pub struct CacheListOutput {
pub path: String,
pub exists: bool,
}
pub struct ObjectStoreOptions {
pub restore_method: RestoreMethod,
pub compression: bool,
pub remote: Option<Box<dyn RemoteCache>>,
pub remote_push: bool,
pub remote_pull: bool,
pub mtime_check: bool,
}
impl ObjectStore {
pub fn new(opts: ObjectStoreOptions) -> Result<Self> {
let rsconstruct_dir = PathBuf::from(RSBUILD_DIR);
let objects_dir = rsconstruct_dir.join(OBJECTS_DIR);
let db_path = rsconstruct_dir.join(DB_FILE);
fs::create_dir_all(&rsconstruct_dir)
.context("Failed to create .rsconstruct directory")?;
let db = crate::db::open_or_recreate(&db_path, "Cache database")?;
Ok(Self {
objects_dir,
db,
restore_method: opts.restore_method,
compression: opts.compression,
remote: opts.remote,
remote_push: opts.remote_push,
remote_pull: opts.remote_pull,
mtime_check: opts.mtime_check,
})
}
pub fn set_mtime_check(&mut self, enabled: bool) {
self.mtime_check = enabled;
}
fn get_entry(&self, cache_key: &str) -> Option<CacheEntry> {
let read_txn = self.db.begin_read().ok()?;
let table = read_txn.open_table(CACHE_TABLE).ok()?;
let data = table.get(cache_key).ok()??;
serde_json::from_slice(data.value()).ok()
}
fn insert_entry(&self, cache_key: &str, entry: &CacheEntry) -> Result<()> {
let value = serde_json::to_vec(entry)
.context("Failed to serialize cache entry")?;
let write_txn = self.db.begin_write()
.context("Failed to begin write transaction")?;
{
let mut table = write_txn.open_table(CACHE_TABLE)
.context("Failed to open cache table")?;
table.insert(cache_key, value.as_slice())
.context("Failed to insert cache entry")?;
}
write_txn.commit()
.context("Failed to commit cache entry")?;
Ok(())
}
pub fn calculate_checksum(file_path: &Path) -> Result<String> {
checksum::file_checksum(file_path)
}
pub fn calculate_checksum_bytes(data: &[u8]) -> String {
checksum::bytes_checksum(data)
}
fn object_path(&self, checksum: &str) -> PathBuf {
let (prefix, rest) = checksum.split_at(CHECKSUM_PREFIX_LEN.min(checksum.len()));
self.objects_dir.join(prefix).join(rest)
}
fn store_object(&self, content: &[u8]) -> Result<String> {
let checksum = Self::calculate_checksum_bytes(content);
let object_path = self.object_path(&checksum);
if !object_path.exists() {
if let Some(parent) = object_path.parent() {
fs::create_dir_all(parent)
.context("Failed to create object directory")?;
}
let blob = if self.compression {
zstd::encode_all(content, 0)
.context("Failed to zstd-compress object")?
} else {
content.to_vec()
};
fs::write(&object_path, &blob)
.context("Failed to write object")?;
let mut perms = fs::metadata(&object_path)
.context("Failed to read object metadata")?
.permissions();
perms.set_readonly(true);
fs::set_permissions(&object_path, perms)
.context("Failed to set object read-only")?;
}
Ok(checksum)
}
fn has_object(&self, checksum: &str) -> bool {
self.object_path(checksum).exists()
}
fn restore_file(&self, checksum: &str, output_path: &Path) -> Result<()> {
let object_path = self.object_path(checksum);
if self.compression {
let content = self.read_object(checksum)?;
fs::write(output_path, &content)
.with_context(|| format!("Failed to write decompressed output: {}", output_path.display()))?;
crate::platform::set_permissions_mode(output_path, 0o644)
.context("Failed to make restored file writable")?;
return Ok(());
}
match self.restore_method {
RestoreMethod::Hardlink => {
fs::hard_link(&object_path, output_path)
.with_context(|| format!("Failed to hard link from cache: {}. If on a cross-filesystem setup, set restore_method = \"copy\" in rsconstruct.toml.", checksum))?;
}
RestoreMethod::Copy => {
fs::copy(&object_path, output_path)
.with_context(|| format!("Failed to copy from cache: {}", checksum))?;
crate::platform::set_permissions_mode(output_path, 0o644)
.context("Failed to make restored file writable")?;
}
RestoreMethod::Auto => unreachable!("Auto should be resolved before use"),
}
Ok(())
}
pub(crate) fn read_object(&self, checksum: &str) -> Result<Vec<u8>> {
let object_path = self.object_path(checksum);
let raw = fs::read(&object_path)
.with_context(|| format!("Failed to read object: {}", checksum))?;
if self.compression {
zstd::decode_all(raw.as_slice())
.with_context(|| format!("Failed to decompress object: {}", checksum))
} else {
Ok(raw)
}
}
fn path_string(path: &Path) -> String {
path.display().to_string()
}
}