Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/spark-rsi-mv/blob/main/src/ledger/blob.rs # spark-rsi-mv Native Rust recursive self-improvement engine with paired bootstrap evaluation and cryptographic ledger Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `spark-rsi-mv / src / ledger / blob.rs` 67 lines ยท 1933 bytes Raw Blame History 1 use sha2::{Digest, Sha256}; 2 use std::fs; 3 use std::path::{Path, PathBuf}; 4 5 #[derive(Debug, Clone)] 6 pub struct BlobStore { 7 root: PathBuf, 8 } 9 10 impl BlobStore { 11 pub fn new(rsi_root: &Path) -> Result { 12 let root = rsi_root.join("blobs"); 13 fs::create_dir_all(&root).map_err(|e| format!("Failed to create blobs dir: {}", e))?; 14 Ok(Self { root }) 15 } 16 17 pub fn put_blob(&self, data: &[u8]) -> Result { 18 let mut hasher = Sha256::new(); 19 hasher.update(data); 20 let digest = format!("{:x}", hasher.finalize()); 21 22 let dest = self.root.join(&digest); 23 if dest.exists() { 24 return Ok(digest); 25 } 26 27 let tmp_file = self 28 .root 29 .join(format!(".tmp.{}", uuid::Uuid::new_v4().simple())); 30 fs::write(&tmp_file, data).map_err(|e| format!("Failed to write tmp blob: {}", e))?; 31 fs::rename(&tmp_file, &dest).map_err(|e| format!("Failed to atomic rename blob: {}", e))?; 32 33 Ok(digest) 34 } 35 36 pub fn get_blob(&self, digest: &str) -> Result, String> { 37 let dest = self.root.join(digest); 38 if !dest.exists() { 39 return Err(format!("Blob not found: {}", digest)); 40 } 41 42 let data = fs::read(&dest).map_err(|e| format!("Failed to read blob: {}", e))?; 43 let mut hasher = Sha256::new(); 44 hasher.update(&data); 45 let computed = format!("{:x}", hasher.finalize()); 46 if computed != digest { 47 return Err(format!( 48 "Blob corrupted: expected={}, computed={}", 49 digest, computed 50 )); 51 } 52 53 Ok(data) 54 } 55 56 pub fn has_blob(&self, digest: &str) -> bool { 57 self.root.join(digest).exists() 58 } 59 60 pub fn verify_blob(&self, digest: &str) -> bool { 61 self.get_blob(digest).is_ok() 62 } 63 64 pub fn root_dir(&self) -> &Path { 65 &self.root 66 } 67 }