OXYGENHUB

Coverage / crates/spectra-core/src/attachments.rs 94.1%

16 of 17 lines run, 3 of 6 functions · run #28 on main · run missed

1
//! Bytes kept beside the ledger, named by their BLAKE3 (spec §7.2): tool
2
//! call arguments and results in full, so the audit is never truncated;
3
//! retention drops old bodies and keeps the hashes (spec §22.1).
4
5
use anyhow::Result;
6
use std::path::Path;
7
8
/// Store `bytes` under `<data>/attachments/` and return their hash (hex).
970×
pub fn put(data: &Path, bytes: &[u8]) -> Result<String> {
1070×
    let hash = blake3::hash(bytes).to_hex().to_string();
1170×
    let dir = data.join("attachments").join(&hash[..2]);
1270×
    let path = dir.join(&hash);
1370×
    if !path.exists() {
1454×
        std::fs::create_dir_all(&dir)?;
1554×
        crate::durable::write_atomic(&path, bytes)?;
1616×
    }
1770×
    Ok(hash)
1870×
}
19
20
/// The bytes stored under `hash`, if they are still kept.
211×
pub fn get(data: &Path, hash: &str) -> Option<Vec<u8>> {
2264×
    if hash.len() < 3 || !hash.bytes().all(|b| b.is_ascii_hexdigit()) {
230×
        return None;
241×
    }
251×
    std::fs::read(data.join("attachments").join(&hash[..2]).join(hash)).ok()
261×
}