Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions crates/core/src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,22 @@ impl Object {
}
}

/// The kind name for an encoding's leading tag byte, as [`Object::kind_name`]
/// reports it; `None` for a tag no current object uses.
pub fn kind_name_of_tag(tag: u8) -> Option<&'static str> {
Some(match tag {
TAG_BLOB => "blob",
TAG_TREE => "tree",
TAG_SNAPSHOT => "snapshot",
TAG_SECRET => "secret",
TAG_SIGNATURE => "signature",
TAG_TRANSCRIPT => "transcript",
TAG_SEALED => "sealed",
TAG_MANIFEST => "manifest",
_ => return None,
})
}

pub fn kind_name(&self) -> &'static str {
match self {
Object::Blob(_) => "blob",
Expand Down
92 changes: 92 additions & 0 deletions crates/core/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,61 @@ impl Store {
self.existing_loose_path(id).is_some() || self.pack_index.contains_key(id)
}

/// The kind name of a stored object (see [`Object::kind_name`]), read from
/// its first encoded byte without loading the rest or admitting it to the
/// cache, so asking about a large blob is cheap. It does not verify the
/// object's hash (reads do); ingest already did.
pub fn peek_kind(&self, id: &ObjectId) -> Result<&'static str> {
use std::io::{BufReader, Read, Seek, SeekFrom};
const ZSTD_MAGIC: [u8; 4] = [0x28, 0xb5, 0x2f, 0xfd];
if let Some(resident) = self.resident.get(id) {
return Ok(resident.obj.kind_name());
}
if self.spilled.contains_key(id) {
return Ok("blob");
}
let mut first = [0_u8; 1];
if let Some(path) = self.existing_loose_path(id) {
let mut file = std::fs::File::open(&path)?;
let mut prefix = [0_u8; 4];
let n = file.read(&mut prefix)?;
if n == prefix.len() && prefix == ZSTD_MAGIC {
let mut decoder =
zstd::stream::read::Decoder::new(BufReader::new(std::fs::File::open(&path)?))?;
decoder.read_exact(&mut first)?;
} else if n > 0 {
first[0] = prefix[0];
} else {
return Err(Error::Malformed(format!("object {id} is empty")));
}
} else if let Some(loc) = self.pack_index.get(id) {
let mut file = std::fs::File::open(&loc.pack_path)?;
file.seek(SeekFrom::Start(loc.offset))?;
let mut len_buf = [0_u8; 4];
file.read_exact(&mut len_buf)?;
let len = u32::from_le_bytes(len_buf) as u64;
let mut decoder = zstd::stream::read::Decoder::new(BufReader::new(file.take(len)))?;
decoder.read_exact(&mut first)?;
} else {
return Err(Error::NotFound(*id));
}
Object::kind_name_of_tag(first[0])
.ok_or_else(|| Error::Malformed(format!("object {id} has an unknown kind tag")))
}

/// Whether the object is on disk right now, ignoring the resident cache
/// and any spill: a loose file, or a pack this store has indexed that
/// still exists. A stat per call, so it is cheap enough to confirm each id
/// a long check relied on once the repo lock is held (a gc prune can
/// delete an unreferenced object a cached read still returns).
pub fn contains_on_disk(&self, id: &ObjectId) -> bool {
self.existing_loose_path(id).is_some()
|| self
.pack_index
.get(id)
.is_some_and(|loc| loc.pack_path.is_file())
}

/// Return a blob's logical byte length without admitting its contents to
/// the resident object cache. Persistent payloads are verified while they
/// are streamed through a fixed-size buffer, allowing callers to enforce
Expand Down Expand Up @@ -770,6 +825,43 @@ fn write_atomic(path: &std::path::Path, bytes: &[u8]) -> Result<()> {
mod tests {
use super::*;

/// `peek_kind` names a stored object's kind from disk alone, loose or
/// packed, without loading it.
#[test]
fn peek_kind_reads_the_kind_byte_from_disk() {
let dir = std::env::temp_dir().join(format!("scl-peek-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
let objs = [
Object::blob(vec![7; 100_000]),
Object::Tree(crate::object::Tree::default()),
Object::Sealed(crate::object::SealedObj {
payload: std::sync::Arc::from(vec![1, 2, 3].into_boxed_slice()),
}),
];
let mut st = Store::open_persistent(&dir, 1 << 20).unwrap();
for o in &objs {
st.put(o.clone()).unwrap();
}
// A fresh store has nothing resident, so this is the disk path.
let fresh = Store::open_persistent(&dir, 1 << 20).unwrap();
for o in &objs {
assert_eq!(fresh.peek_kind(&o.id()).unwrap(), o.kind_name());
}
assert!(fresh.peek_kind(&ObjectId::of(b"absent")).is_err());
// The same from a pack, once the loose files are gone.
let ids: Vec<ObjectId> = objs.iter().map(Object::id).collect();
st.write_pack(&ids).unwrap();
for id in &ids {
st.delete(id).unwrap();
}
let packed = Store::open_persistent(&dir, 1 << 20).unwrap();
for o in &objs {
assert_eq!(packed.peek_kind(&o.id()).unwrap(), o.kind_name());
}
drop((st, fresh, packed));
std::fs::remove_dir_all(&dir).unwrap();
}

fn blob(n: usize, fill: u8) -> Object {
Object::blob(vec![fill; n])
}
Expand Down
Loading
Loading