use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::collections::HashSet;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct BlobId(pub String);
impl BlobId {
fn of(json: &str) -> Self {
let digest = Sha256::digest(json.as_bytes());
Self(hex::encode(&digest[..8]))
}
pub fn of_value<T: Serialize>(value: &T) -> Self {
match serde_json::to_string(value) {
Ok(json) => Self::of(&json),
Err(_) => Self(String::new()),
}
}
}
impl std::fmt::Display for BlobId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.0)
}
}
#[derive(Debug, Clone)]
pub struct BlobStore {
dir: PathBuf,
known: Arc<Mutex<HashSet<BlobId>>>,
}
impl BlobStore {
pub fn new(recording_dir: &Path) -> Self {
Self {
dir: recording_dir.join("blobs"),
known: Arc::default(),
}
}
pub fn put<T: Serialize>(&self, value: &T) -> Result<BlobId, std::io::Error> {
let json = serde_json::to_string(value)
.map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?;
self.put_raw(&json)
}
pub fn put_raw(&self, json: &str) -> Result<BlobId, std::io::Error> {
let id = BlobId::of(json);
if self.is_known(&id) {
return Ok(id);
}
let path = self.dir.join(&id.0);
let on_disk = match std::fs::metadata(&path) {
Ok(meta) if meta.len() == json.len() as u64 => std::fs::read_to_string(&path),
Ok(_) => Ok(String::new()),
Err(e) => Err(e),
};
match on_disk {
Ok(existing) if existing == json => {
self.remember(id.clone());
return Ok(id);
}
Ok(_) => {
tracing::warn!(
blob = %id,
dir = %self.dir.display(),
"recorder: blob content does not match its id — an edit is being overwritten \
by a fresh recording; copy it elsewhere first"
);
}
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
Err(e) => return Err(e),
}
super::private_dir(&self.dir)?;
std::io::Write::write_all(&mut super::private_file(&path)?, json.as_bytes())?;
self.remember(id.clone());
Ok(id)
}
fn is_known(&self, id: &BlobId) -> bool {
self.known
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.contains(id)
}
fn remember(&self, id: BlobId) {
self.known
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.insert(id);
}
pub fn get<T: DeserializeOwned>(&self, id: &BlobId) -> Result<Option<T>, std::io::Error> {
let Some(json) = self.get_raw(id)? else {
return Ok(None);
};
serde_json::from_str(&json)
.map(Some)
.map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))
}
pub fn len(&self, id: &BlobId) -> Option<u64> {
std::fs::metadata(self.dir.join(&id.0))
.ok()
.map(|m| m.len())
}
pub fn get_raw(&self, id: &BlobId) -> Result<Option<String>, std::io::Error> {
match std::fs::read_to_string(self.dir.join(&id.0)) {
Ok(s) => Ok(Some(s)),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
Err(e) => Err(e),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn store() -> (BlobStore, tempfile::TempDir) {
let dir = tempfile::tempdir().unwrap();
(BlobStore::new(dir.path()), dir)
}
#[test]
fn same_content_yields_one_file() {
let (store, dir) = store();
let a = store.put(&"hello").unwrap();
let b = store.put(&"hello").unwrap();
assert_eq!(a, b);
let count = std::fs::read_dir(dir.path().join("blobs")).unwrap().count();
assert_eq!(count, 1);
}
#[test]
fn different_content_yields_different_ids() {
let (store, _dir) = store();
assert_ne!(store.put(&"a").unwrap(), store.put(&"b").unwrap());
}
#[test]
fn round_trips_a_typed_value() {
let (store, _dir) = store();
let value = vec![1u32, 2, 3];
let id = store.put(&value).unwrap();
assert_eq!(store.get::<Vec<u32>>(&id).unwrap(), Some(value));
}
#[test]
fn missing_blob_reads_as_none() {
let (store, _dir) = store();
let absent = BlobId("0123456789abcdef".into());
assert_eq!(store.get_raw(&absent).unwrap(), None);
}
#[test]
fn id_is_sixteen_hex_chars() {
let (store, _dir) = store();
let id = store.put(&"anything").unwrap();
assert_eq!(id.0.len(), 16);
assert!(id.0.chars().all(|c| c.is_ascii_hexdigit()));
}
#[test]
fn a_hand_edited_blob_is_restored_rather_than_trusted() {
let (store, dir) = store();
let id = store.put(&"original").unwrap();
let path = dir.path().join("blobs").join(&id.0);
std::fs::write(&path, "tampered").unwrap();
assert_eq!(store.get_raw(&id).unwrap().as_deref(), Some("tampered"));
let later = BlobStore::new(dir.path());
let again = later.put(&"original").unwrap();
assert_eq!(again, id);
assert_eq!(
store.get::<String>(&id).unwrap().as_deref(),
Some("original"),
"re-recording must not leave a blob whose content contradicts its id"
);
}
#[test]
fn a_blob_put_again_is_not_read_back() {
let (store, dir) = store();
let id = store.put(&"original").unwrap();
let path = dir.path().join("blobs").join(&id.0);
std::fs::remove_file(&path).unwrap();
assert_eq!(store.put(&"original").unwrap(), id);
assert!(!path.exists(), "the second put went to disk");
}
}