| 1 |
use std::path::Path; |
| 2 |
use std::path::PathBuf; |
| 3 |
|
| 4 |
use bytes_cast::unaligned; |
| 5 |
use bytes_cast::BytesCast; |
| 6 |
use memmap2::Mmap; |
| 7 |
|
| 8 |
use super::Node; |
| 9 |
use super::UncheckedRevision; |
| 10 |
use crate::errors::HgError; |
| 11 |
use crate::errors::HgResultExt; |
| 12 |
use crate::vfs::VfsImpl; |
| 13 |
use crate::BaseRevision; |
| 14 |
|
| 15 |
const ONDISK_VERSION: u8 = 1; |
| 16 |
|
| 17 |
pub(super) struct NodeMapDocket { |
| 18 |
pub data_length: usize, |
| 19 |
/// This is [`UncheckedRevision`] and not [`Revision`] because the nodemap |
| 20 |
/// can be out-of-date (because of strip for example) |
| 21 |
pub tip_rev: UncheckedRevision, |
| 22 |
pub tip_node: Node, |
| 23 |
// TODO: keep here more of the data from `parse()` when we need it |
| 24 |
} |
| 25 |
|
| 26 |
#[derive(BytesCast)] |
| 27 |
#[repr(C)] |
| 28 |
struct DocketHeader { |
| 29 |
uid_size: u8, |
| 30 |
tip_rev: unaligned::U64Be, |
| 31 |
data_length: unaligned::U64Be, |
| 32 |
_data_unused: unaligned::U64Be, |
| 33 |
tip_node_size: unaligned::U64Be, |
| 34 |
} |
| 35 |
|
| 36 |
impl NodeMapDocket { |
| 37 |
/// Return `Ok(None)` when the caller should proceed without a persistent |
| 38 |
/// nodemap: |
| 39 |
/// |
| 40 |
/// * This revlog does not have a `.n` docket file (it is not generated for |
| 41 |
/// small revlogs), or |
| 42 |
/// * The docket has an unsupported version number (repositories created by |
| 43 |
/// later hg, maybe that should be a requirement instead?), or |
| 44 |
/// * The docket file points to a missing (likely deleted) data file (this |
| 45 |
/// can happen in a rare race condition). |
| 46 |
pub fn read_from_file( |
| 47 |
store_vfs: &VfsImpl, |
| 48 |
index_path: &Path, |
| 49 |
) -> Result<Option<(Self, Mmap)>, HgError> { |
| 50 |
let docket_path = index_path.with_extension("n"); |
| 51 |
let docket_bytes = if let Some(bytes) = |
| 52 |
store_vfs.read(&docket_path).io_not_found_as_none()? |
| 53 |
{ |
| 54 |
bytes |
| 55 |
} else { |
| 56 |
return Ok(None); |
| 57 |
}; |
| 58 |
|
| 59 |
let input = |
| 60 |
if let Some((&ONDISK_VERSION, rest)) = docket_bytes.split_first() { |
| 61 |
rest |
| 62 |
} else { |
| 63 |
return Ok(None); |
| 64 |
}; |
| 65 |
|
| 66 |
/// Treat any error as a parse error |
| 67 |
fn parse<T, E>(result: Result<T, E>) -> Result<T, HgError> { |
| 68 |
result.map_err(|_| HgError::corrupted("nodemap docket parse error")) |
| 69 |
} |
| 70 |
|
| 71 |
let (header, rest) = parse(DocketHeader::from_bytes(input))?; |
| 72 |
let uid_size = header.uid_size as usize; |
| 73 |
// TODO: do we care about overflow for 4 GB+ nodemap files on 32-bit |
| 74 |
// systems? |
| 75 |
let tip_node_size = header.tip_node_size.get() as usize; |
| 76 |
let data_length = header.data_length.get() as usize; |
| 77 |
let (uid, rest) = parse(u8::slice_from_bytes(rest, uid_size))?; |
| 78 |
let (tip_node, _rest) = |
| 79 |
parse(u8::slice_from_bytes(rest, tip_node_size))?; |
| 80 |
let uid = parse(std::str::from_utf8(uid))?; |
| 81 |
let tip_node = parse(Node::from_bytes(tip_node))?.0.to_owned(); |
| 82 |
let revnum: BaseRevision = parse(header.tip_rev.get().try_into())?; |
| 83 |
let docket = |
| 84 |
NodeMapDocket { data_length, tip_rev: revnum.into(), tip_node }; |
| 85 |
|
| 86 |
let data_path = rawdata_path(&docket_path, uid); |
| 87 |
// TODO: use `vfs.read()` here when the `persistent-nodemap.mmap` |
| 88 |
// config is false? |
| 89 |
if let Some(mmap) = |
| 90 |
store_vfs.mmap_open(data_path).io_not_found_as_none()? |
| 91 |
{ |
| 92 |
if mmap.len() >= data_length { |
| 93 |
Ok(Some((docket, mmap))) |
| 94 |
} else { |
| 95 |
Err(HgError::corrupted("persistent nodemap too short")) |
| 96 |
} |
| 97 |
} else { |
| 98 |
// Even if .hg/requires opted in, some revlogs are deemed small |
| 99 |
// enough to not need a persistent nodemap. |
| 100 |
Ok(None) |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
fn rawdata_path(docket_path: &Path, uid: &str) -> PathBuf { |
| 106 |
let docket_name = docket_path |
| 107 |
.file_name() |
| 108 |
.expect("expected a base name") |
| 109 |
.to_str() |
| 110 |
.expect("expected an ASCII file name in the store"); |
| 111 |
let prefix = docket_name |
| 112 |
.strip_suffix(".n.a") |
| 113 |
.or_else(|| docket_name.strip_suffix(".n")) |
| 114 |
.expect("expected docket path in .n or .n.a"); |
| 115 |
let name = format!("{}-{}.nd", prefix, uid); |
| 116 |
docket_path.parent().expect("expected a non-root path").join(name) |
| 117 |
} |