//! Loose object store (spec ยง13.1): one file per object under //! `objects/loose//`, written atomically and verified on read. //! Packs come later. use crate::hash::{hash_object, header, ObjectRef, ObjectType}; use crate::objects::{Change, ObjectError}; use rand::RngCore; use std::fs; use std::io::{self, Write}; use std::path::{Path, PathBuf}; #[derive(Debug, thiserror::Error)] pub enum StoreError { #[error("io: {0}")] Io(#[from] io::Error), #[error("corrupt object {0}: {1}")] Corrupt(ObjectRef, String), #[error("object {0}")] Object(#[from] ObjectError), #[error("object {0} is a {1}, expected {2}")] WrongType(ObjectRef, &'static str, &'static str), #[error("object {0} not found")] NotFound(ObjectRef), } pub type Result = std::result::Result; #[derive(Debug, Clone)] pub struct Store { dir: PathBuf, } impl Store { pub fn open(dir: impl Into) -> io::Result { let dir = dir.into(); fs::create_dir_all(dir.join("loose"))?; Ok(Store { dir }) } pub fn dir(&self) -> &Path { &self.dir } fn path_for(&self, r: &ObjectRef) -> PathBuf { let h = r.to_hex(); self.dir.join("loose").join(&h[..2]).join(&h[2..]) } pub fn has(&self, r: &ObjectRef) -> bool { self.path_for(r).is_file() } /// Store `body` as an object of type `ty`; idempotent. pub fn put(&self, ty: ObjectType, body: &[u8]) -> Result { let r = hash_object(ty, body); let path = self.path_for(&r); if path.is_file() { return Ok(r); } fs::create_dir_all(path.parent().unwrap())?; let mut nonce = [0u8; 8]; rand::rngs::OsRng.fill_bytes(&mut nonce); let tmp = self.dir.join("loose").join(format!("tmp-{}", hex::encode(nonce))); { let mut f = fs::File::create(&tmp)?; f.write_all(&header(ty, body.len()))?; f.write_all(body)?; f.sync_all()?; } fs::rename(&tmp, &path)?; Ok(r) } /// Read and verify an object. pub fn get(&self, r: &ObjectRef) -> Result<(ObjectType, Vec)> { let path = self.path_for(r); let data = match fs::read(&path) { Ok(d) => d, Err(e) if e.kind() == io::ErrorKind::NotFound => return Err(StoreError::NotFound(*r)), Err(e) => return Err(e.into()), }; let nul = data.iter().position(|&b| b == 0).ok_or_else(|| StoreError::Corrupt(*r, "no header".into()))?; let head = std::str::from_utf8(&data[..nul]).map_err(|_| StoreError::Corrupt(*r, "header not UTF-8".into()))?; let mut parts = head.split(' '); let (Some("oxy1"), Some(ty), Some(len), None) = (parts.next(), parts.next(), parts.next(), parts.next()) else { return Err(StoreError::Corrupt(*r, format!("bad header {head:?}"))); }; let ty = ObjectType::from_name(ty).ok_or_else(|| StoreError::Corrupt(*r, format!("unknown type {ty}")))?; let len: usize = len.parse().map_err(|_| StoreError::Corrupt(*r, "bad length".into()))?; let body = &data[nul + 1..]; if body.len() != len { return Err(StoreError::Corrupt(*r, format!("length {} != header {len}", body.len()))); } if hash_object(ty, body) != *r { return Err(StoreError::Corrupt(*r, "hash mismatch".into())); } Ok((ty, body.to_vec())) } pub fn get_typed(&self, r: &ObjectRef, want: ObjectType) -> Result> { let (ty, body) = self.get(r)?; if ty != want { return Err(StoreError::WrongType(*r, ty.name(), want.name())); } Ok(body) } pub fn put_change(&self, c: &Change) -> Result { self.put(ObjectType::Change, &c.encode()) } pub fn get_change(&self, r: &ObjectRef) -> Result { Ok(Change::decode(&self.get_typed(r, ObjectType::Change)?)?) } pub fn put_chunk(&self, bytes: &[u8]) -> Result { self.put(ObjectType::Chunk, bytes) } /// Every object reference in the store (slow; for gc and tests). pub fn list(&self) -> io::Result> { let mut out = Vec::new(); let loose = self.dir.join("loose"); for d in fs::read_dir(&loose)? { let d = d?; if !d.file_type()?.is_dir() { continue; } let prefix = d.file_name().to_string_lossy().to_string(); for f in fs::read_dir(d.path())? { let f = f?; let name = f.file_name().to_string_lossy().to_string(); if let Some(r) = ObjectRef::from_hex(&format!("{prefix}{name}")) { out.push(r); } } } out.sort(); Ok(out) } } #[cfg(test)] mod tests { use super::*; #[test] fn put_get_verify() { let dir = tempfile::tempdir().unwrap(); let s = Store::open(dir.path().join("objects")).unwrap(); let r = s.put(ObjectType::Chunk, b"hello").unwrap(); assert!(s.has(&r)); assert_eq!(s.get(&r).unwrap(), (ObjectType::Chunk, b"hello".to_vec())); assert_eq!(s.put(ObjectType::Chunk, b"hello").unwrap(), r); assert_eq!(s.list().unwrap(), vec![r]); assert!(matches!(s.get_typed(&r, ObjectType::Blob), Err(StoreError::WrongType(..)))); assert!(matches!(s.get(&ObjectRef([9; 32])), Err(StoreError::NotFound(_)))); // Tamper: flip a byte, read must fail. let path = s.path_for(&r); let mut data = fs::read(&path).unwrap(); let last = data.len() - 1; data[last] ^= 1; fs::write(&path, data).unwrap(); assert!(matches!(s.get(&r), Err(StoreError::Corrupt(..)))); } #[test] fn change_round_trip_through_store() { let dir = tempfile::tempdir().unwrap(); let s = Store::open(dir.path()).unwrap(); let c = crate::objects::tests::sample_change(); let r = s.put_change(&c).unwrap(); assert_eq!(r, c.hash()); assert_eq!(s.get_change(&r).unwrap(), c); } }