Pausa: 11 crates del fractal Ente #0 con cerebro completo
PID 1 boot + bus interno autenticado + cerebro KCL/Rust: - 6 lib crates de infra (card, bus, cas, kernel, soma, wasm, snapshot) - ente-brain: motor de reglas O(1), observer Shannon, cristalización, audit hash-chain, persistencia rules.k, Prometheus /metrics - KCL schemas card.k + rule.k como gramática autoritativa - compat-logind D-Bus, ente-echo demo provider, ente-zero PID 1 - 22 tests OK, ~3.8k LOC Rust + ~300 LOC KCL Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "ente-cas"
|
||||
version = "0.0.1"
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
publish.workspace = true
|
||||
|
||||
[dependencies]
|
||||
sha2 = { workspace = true }
|
||||
anyhow = { workspace = true }
|
||||
tracing = { workspace = true }
|
||||
@@ -0,0 +1,71 @@
|
||||
//! Content-addressable store. Resuelve `Payload::Wasm.module_sha256` (y en
|
||||
//! el futuro otros payloads firmados) desde el sistema de archivos con
|
||||
//! verificación de hash. Path por defecto: `$XDG_DATA_HOME/ente/cas/<hex>`.
|
||||
//!
|
||||
//! Override por env: `ENTE_CAS_ROOT`.
|
||||
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::path::PathBuf;
|
||||
use tracing::debug;
|
||||
|
||||
pub fn cas_root() -> PathBuf {
|
||||
if let Ok(p) = std::env::var("ENTE_CAS_ROOT") {
|
||||
return p.into();
|
||||
}
|
||||
let base = if let Ok(d) = std::env::var("XDG_DATA_HOME") {
|
||||
d
|
||||
} else if let Ok(h) = std::env::var("HOME") {
|
||||
format!("{h}/.local/share")
|
||||
} else {
|
||||
"/var/lib".into()
|
||||
};
|
||||
PathBuf::from(base).join("ente").join("cas")
|
||||
}
|
||||
|
||||
pub fn sha256_of(bytes: &[u8]) -> [u8; 32] {
|
||||
let mut hasher = Sha256::new();
|
||||
hasher.update(bytes);
|
||||
hasher.finalize().into()
|
||||
}
|
||||
|
||||
pub fn hex(sha: &[u8; 32]) -> String {
|
||||
let mut s = String::with_capacity(64);
|
||||
for b in sha {
|
||||
s.push_str(&format!("{:02x}", b));
|
||||
}
|
||||
s
|
||||
}
|
||||
|
||||
pub fn resolve(sha: &[u8; 32]) -> anyhow::Result<Vec<u8>> {
|
||||
let path = cas_root().join(hex(sha));
|
||||
let bytes = std::fs::read(&path)
|
||||
.map_err(|e| anyhow::anyhow!("CAS read {}: {e}", path.display()))?;
|
||||
let actual = sha256_of(&bytes);
|
||||
if &actual != sha {
|
||||
anyhow::bail!(
|
||||
"CAS hash mismatch en {}: declarado={} real={}",
|
||||
path.display(), hex(sha), hex(&actual)
|
||||
);
|
||||
}
|
||||
Ok(bytes)
|
||||
}
|
||||
|
||||
/// Almacena bytes en el CAS, devuelve su SHA. Idempotente: si el archivo ya
|
||||
/// existe con el mismo hash, no reescribe.
|
||||
pub fn store(bytes: &[u8]) -> anyhow::Result<[u8; 32]> {
|
||||
let sha = sha256_of(bytes);
|
||||
let root = cas_root();
|
||||
std::fs::create_dir_all(&root)
|
||||
.map_err(|e| anyhow::anyhow!("CAS mkdir {}: {e}", root.display()))?;
|
||||
let path = root.join(hex(&sha));
|
||||
if !path.exists() {
|
||||
// Escritura atómica: crear .tmp y rename.
|
||||
let tmp = path.with_extension("tmp");
|
||||
std::fs::write(&tmp, bytes)
|
||||
.map_err(|e| anyhow::anyhow!("CAS write {}: {e}", tmp.display()))?;
|
||||
std::fs::rename(&tmp, &path)
|
||||
.map_err(|e| anyhow::anyhow!("CAS rename {}: {e}", path.display()))?;
|
||||
debug!(hex = %hex(&sha), len = bytes.len(), path = %path.display(), "CAS store");
|
||||
}
|
||||
Ok(sha)
|
||||
}
|
||||
Reference in New Issue
Block a user