OXYGENHUB

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

16 of 17 lines run, 3 of 6 functions · run #22 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).
954×
pub fn put(data: &Path, bytes: &[u8]) -> Result<String> {
1054×
    let hash = blake3::hash(bytes).to_hex().to_string();
1154×
    let dir = data.join("attachments").join(&hash[..2]);
1254×
    let path = dir.join(&hash);
1354×
    if !path.exists() {
1447×
        std::fs::create_dir_all(&dir)?;
1547×
        oxy_core::durable::write_atomic(&path, bytes)?;
167×
    }
1754×
    Ok(hash)
1854×
}
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×
}