Mercurial > hg
view rust/hg-core/src/revlog/nodemap_docket.rs @ 46390:0800aa42bb4c
rust: use the bytes-cast crate to parse persistent nodemaps
This crate casts pointers to custom structs, with compile-time safety checks,
for easy and efficient binary data parsing.
See https://crates.io/crates/bytes-cast and
https://docs.rs/bytes-cast/0.1.0/bytes_cast/
Differential Revision: https://phab.mercurial-scm.org/D9788
author | Simon Sapin <simon.sapin@octobus.net> |
---|---|
date | Fri, 15 Jan 2021 16:11:54 +0100 |
parents | 8a4914397d02 |
children | 43d63979a75e |
line wrap: on
line source
use bytes_cast::{unaligned, BytesCast}; use memmap::Mmap; use std::path::{Path, PathBuf}; use super::revlog::RevlogError; use crate::repo::Repo; use crate::utils::strip_suffix; const ONDISK_VERSION: u8 = 1; pub(super) struct NodeMapDocket { pub data_length: usize, // TODO: keep here more of the data from `parse()` when we need it } #[derive(BytesCast)] #[repr(C)] struct DocketHeader { uid_size: u8, _tip_rev: unaligned::U64Be, data_length: unaligned::U64Be, _data_unused: unaligned::U64Be, tip_node_size: unaligned::U64Be, } impl NodeMapDocket { /// Return `Ok(None)` when the caller should proceed without a persistent /// nodemap: /// /// * This revlog does not have a `.n` docket file (it is not generated for /// small revlogs), or /// * The docket has an unsupported version number (repositories created by /// later hg, maybe that should be a requirement instead?), or /// * The docket file points to a missing (likely deleted) data file (this /// can happen in a rare race condition). pub fn read_from_file( repo: &Repo, index_path: &Path, ) -> Result<Option<(Self, Mmap)>, RevlogError> { let docket_path = index_path.with_extension("n"); let docket_bytes = match repo.store_vfs().read(&docket_path) { Err(e) if e.kind() == std::io::ErrorKind::NotFound => { return Ok(None) } Err(e) => return Err(RevlogError::IoError(e)), Ok(bytes) => bytes, }; let input = if let Some((&ONDISK_VERSION, rest)) = docket_bytes.split_first() { rest } else { return Ok(None); }; let (header, rest) = DocketHeader::from_bytes(input)?; let uid_size = header.uid_size as usize; // TODO: do we care about overflow for 4 GB+ nodemap files on 32-bit // systems? let tip_node_size = header.tip_node_size.get() as usize; let data_length = header.data_length.get() as usize; let (uid, rest) = u8::slice_from_bytes(rest, uid_size)?; let (_tip_node, _rest) = u8::slice_from_bytes(rest, tip_node_size)?; let uid = std::str::from_utf8(uid).map_err(|_| RevlogError::Corrupted)?; let docket = NodeMapDocket { data_length }; let data_path = rawdata_path(&docket_path, uid); // TODO: use `std::fs::read` here when the `persistent-nodemap.mmap` // config is false? match repo.store_vfs().mmap_open(&data_path) { Ok(mmap) => { if mmap.len() >= data_length { Ok(Some((docket, mmap))) } else { Err(RevlogError::Corrupted) } } Err(error) => { if error.kind() == std::io::ErrorKind::NotFound { Ok(None) } else { Err(RevlogError::IoError(error)) } } } } } fn rawdata_path(docket_path: &Path, uid: &str) -> PathBuf { let docket_name = docket_path .file_name() .expect("expected a base name") .to_str() .expect("expected an ASCII file name in the store"); let prefix = strip_suffix(docket_name, ".n.a") .or_else(|| strip_suffix(docket_name, ".n")) .expect("expected docket path in .n or .n.a"); let name = format!("{}-{}.nd", prefix, uid); docket_path .parent() .expect("expected a non-root path") .join(name) }