##// END OF EJS Templates
rust: normalize `_for_unchecked_rev` naming among revlogs and the index...
rust: normalize `_for_unchecked_rev` naming among revlogs and the index This normalizes the naming scheme between the `Revlog`, `Changelog`, etc. which is less suprising, though no real bugs could stem from this because of the type signature mismatch. The very high-level `Repo` object still uses an `UncheckedRevision` parameter for its methods because that's what most callers will want.

File last commit:

r53187:a3fa37bd default
r53187:a3fa37bd default
Show More
mod.rs
896 lines | 27.7 KiB | application/rls-services+xml | RustLexer
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 // Copyright 2018-2023 Georges Racinet <georges.racinet@octobus.net>
// and Mercurial contributors
//
// This software may be used and distributed according to the terms of the
// GNU General Public License version 2 or any later version.
//! Mercurial concepts for handling revision history
pub mod node;
pub mod nodemap;
mod nodemap_docket;
pub mod path_encode;
Raphaël Gomès
rust-revlog: add a Rust-only `InnerRevlog`...
r53057 use inner_revlog::CoreRevisionBuffer;
use inner_revlog::InnerRevlog;
use inner_revlog::RevisionBuffer;
use memmap2::MmapOptions;
Raphaël Gomès
rust: don't star export from the `revlog` module...
r53075 pub use node::{FromHexError, Node, NodePrefix, NULL_NODE, NULL_NODE_ID};
Raphaël Gomès
rust-revlog: introduce an `options` module...
r53053 use options::RevlogOpenOptions;
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 pub mod changelog;
Raphaël Gomès
rust-revlog: add compression helpers...
r53051 pub mod compression;
Raphaël Gomès
rust-revlog: add file IO helpers...
r53052 pub mod file_io;
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 pub mod filelog;
pub mod index;
Raphaël Gomès
rust-revlog: add a Rust-only `InnerRevlog`...
r53057 pub mod inner_revlog;
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 pub mod manifest;
Raphaël Gomès
rust-revlog: introduce an `options` module...
r53053 pub mod options;
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 pub mod patch;
use std::borrow::Cow;
Raphaël Gomès
rust-revlog: add a Rust-only `InnerRevlog`...
r53057 use std::io::ErrorKind;
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 use std::io::Read;
use std::ops::Deref;
use std::path::Path;
use self::nodemap_docket::NodeMapDocket;
use crate::errors::HgError;
Raphaël Gomès
rust-revlog: add a Rust-only `InnerRevlog`...
r53057 use crate::errors::IoResultExt;
Raphaël Gomès
rust-revlog: add revlog-specific config objects...
r52758 use crate::exit_codes;
Raphaël Gomès
rust: don't star export from the `revlog` module...
r53075 use crate::revlog::index::Index;
use crate::revlog::nodemap::{NodeMap, NodeMapError};
Raphaël Gomès
rust-revlog: add a Rust-only `InnerRevlog`...
r53057 use crate::vfs::Vfs;
Raphaël Gomès
rust: add Vfs trait...
r52761 use crate::vfs::VfsImpl;
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832
/// As noted in revlog.c, revision numbers are actually encoded in
/// 4 bytes, and are liberally converted to ints, whence the i32
Raphaël Gomès
rust: make `Revision` a newtype...
r51872 pub type BaseRevision = i32;
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832
Raphaël Gomès
rust: make `Revision` a newtype...
r51872 /// Mercurial revision numbers
/// In contrast to the more general [`UncheckedRevision`], these are "checked"
/// in the sense that they should only be used for revisions that are
/// valid for a given index (i.e. in bounds).
Raphaël Gomès
rust: use the new `UncheckedRevision` everywhere applicable...
r51870 #[derive(
Debug,
derive_more::Display,
Clone,
Copy,
Hash,
PartialEq,
Eq,
PartialOrd,
Ord,
)]
Raphaël Gomès
rust: make `Revision` a newtype...
r51872 pub struct Revision(pub BaseRevision);
impl format_bytes::DisplayBytes for Revision {
fn display_bytes(
&self,
output: &mut dyn std::io::Write,
) -> std::io::Result<()> {
self.0.display_bytes(output)
}
}
/// Unchecked Mercurial revision numbers.
///
/// Values of this type have no guarantee of being a valid revision number
/// in any context. Use method `check_revision` to get a valid revision within
/// the appropriate index object.
#[derive(
Debug,
derive_more::Display,
Clone,
Copy,
Hash,
PartialEq,
Eq,
PartialOrd,
Ord,
)]
pub struct UncheckedRevision(pub BaseRevision);
impl format_bytes::DisplayBytes for UncheckedRevision {
fn display_bytes(
&self,
output: &mut dyn std::io::Write,
) -> std::io::Result<()> {
self.0.display_bytes(output)
}
}
Raphaël Gomès
rust: use the new `UncheckedRevision` everywhere applicable...
r51870
impl From<Revision> for UncheckedRevision {
fn from(value: Revision) -> Self {
Raphaël Gomès
rust: make `Revision` a newtype...
r51872 Self(value.0)
}
}
impl From<BaseRevision> for UncheckedRevision {
fn from(value: BaseRevision) -> Self {
Raphaël Gomès
rust: use the new `UncheckedRevision` everywhere applicable...
r51870 Self(value)
}
}
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832
/// Marker expressing the absence of a parent
///
/// Independently of the actual representation, `NULL_REVISION` is guaranteed
/// to be smaller than all existing revisions.
Raphaël Gomès
rust: make `Revision` a newtype...
r51872 pub const NULL_REVISION: Revision = Revision(-1);
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832
/// Same as `mercurial.node.wdirrev`
///
/// This is also equal to `i32::max_value()`, but it's better to spell
/// it out explicitely, same as in `mercurial.node`
#[allow(clippy::unreadable_literal)]
Raphaël Gomès
rust: use the new `UncheckedRevision` everywhere applicable...
r51870 pub const WORKING_DIRECTORY_REVISION: UncheckedRevision =
UncheckedRevision(0x7fffffff);
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832
pub const WORKING_DIRECTORY_HEX: &str =
"ffffffffffffffffffffffffffffffffffffffff";
/// The simplest expression of what we need of Mercurial DAGs.
pub trait Graph {
/// Return the two parents of the given `Revision`.
///
/// Each of the parents can be independently `NULL_REVISION`
fn parents(&self, rev: Revision) -> Result<[Revision; 2], GraphError>;
}
#[derive(Clone, Debug, PartialEq)]
pub enum GraphError {
ParentOutOfRange(Revision),
}
Raphaël Gomès
rust: improve `InvalidRevision` error message...
r52938 impl std::fmt::Display for GraphError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
GraphError::ParentOutOfRange(revision) => {
write!(f, "parent out of range ({})", revision)
}
}
}
}
Georges Racinet
rust: blanket implementation of Graph for Graph references...
r52512 impl<T: Graph> Graph for &T {
fn parents(&self, rev: Revision) -> Result<[Revision; 2], GraphError> {
(*self).parents(rev)
}
}
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 /// The Mercurial Revlog Index
///
/// This is currently limited to the minimal interface that is needed for
/// the [`nodemap`](nodemap/index.html) module
pub trait RevlogIndex {
/// Total number of Revisions referenced in this index
fn len(&self) -> usize;
fn is_empty(&self) -> bool {
self.len() == 0
}
Raphaël Gomès
rust: use the new `UncheckedRevision` everywhere applicable...
r51870 /// Return a reference to the Node or `None` for `NULL_REVISION`
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 fn node(&self, rev: Revision) -> Option<&Node>;
Raphaël Gomès
rust: add `UncheckedRevision` type...
r51867
/// Return a [`Revision`] if `rev` is a valid revision number for this
Raphaël Gomès
rust-index: check that the entry bytes are the same in both indexes...
r52096 /// index.
///
/// [`NULL_REVISION`] is considered to be valid.
Raphaël Gomès
rust-index: allow inlining `check_revision` across crates
r52149 #[inline(always)]
Raphaël Gomès
rust: add `UncheckedRevision` type...
r51867 fn check_revision(&self, rev: UncheckedRevision) -> Option<Revision> {
Raphaël Gomès
rust: use the new `UncheckedRevision` everywhere applicable...
r51870 let rev = rev.0;
Raphaël Gomès
rust: make `Revision` a newtype...
r51872 if rev == NULL_REVISION.0 || (rev >= 0 && (rev as usize) < self.len())
{
Some(Revision(rev))
Raphaël Gomès
rust: add `UncheckedRevision` type...
r51867 } else {
None
}
}
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 }
const REVISION_FLAG_CENSORED: u16 = 1 << 15;
const REVISION_FLAG_ELLIPSIS: u16 = 1 << 14;
const REVISION_FLAG_EXTSTORED: u16 = 1 << 13;
const REVISION_FLAG_HASCOPIESINFO: u16 = 1 << 12;
// Keep this in sync with REVIDX_KNOWN_FLAGS in
// mercurial/revlogutils/flagutil.py
const REVIDX_KNOWN_FLAGS: u16 = REVISION_FLAG_CENSORED
| REVISION_FLAG_ELLIPSIS
| REVISION_FLAG_EXTSTORED
| REVISION_FLAG_HASCOPIESINFO;
const NULL_REVLOG_ENTRY_FLAGS: u16 = 0;
Raphaël Gomès
rust: use the new `UncheckedRevision` everywhere applicable...
r51870 #[derive(Debug, derive_more::From, derive_more::Display)]
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 pub enum RevlogError {
Raphaël Gomès
rust: improve `InvalidRevision` error message...
r52938 #[display(fmt = "invalid revision identifier: {}", "_0")]
InvalidRevision(String),
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 /// Working directory is not supported
WDirUnsupported,
/// Found more than one entry whose ID match the requested prefix
AmbiguousPrefix,
#[from]
Other(HgError),
}
impl From<NodeMapError> for RevlogError {
fn from(error: NodeMapError) -> Self {
match error {
NodeMapError::MultipleResults => RevlogError::AmbiguousPrefix,
NodeMapError::RevisionNotInIndex(rev) => RevlogError::corrupted(
format!("nodemap point to revision {} not in index", rev),
),
}
}
}
fn corrupted<S: AsRef<str>>(context: S) -> HgError {
HgError::corrupted(format!("corrupted revlog, {}", context.as_ref()))
}
impl RevlogError {
fn corrupted<S: AsRef<str>>(context: S) -> Self {
RevlogError::Other(corrupted(context))
}
}
Raphaël Gomès
rust: use new revlog configs in all revlog opening code...
r52760 #[derive(derive_more::Display, Debug, Copy, Clone, PartialEq, Eq)]
Raphaël Gomès
rust-revlog: add revlog-specific config objects...
r52758 pub enum RevlogType {
Changelog,
Manifestlog,
Filelog,
}
impl TryFrom<usize> for RevlogType {
type Error = HgError;
fn try_from(value: usize) -> Result<Self, Self::Error> {
match value {
1001 => Ok(Self::Changelog),
1002 => Ok(Self::Manifestlog),
1003 => Ok(Self::Filelog),
t => Err(HgError::abort(
format!("Unknown revlog type {}", t),
exit_codes::ABORT,
None,
)),
}
}
}
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 pub struct Revlog {
Raphaël Gomès
rust-revlog: add a Rust-only `InnerRevlog`...
r53057 inner: InnerRevlog,
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 /// When present on disk: the persistent nodemap for this revlog
nodemap: Option<nodemap::NodeTree>,
}
Raphaël Gomès
rust: implement the `Graph` trait for all revlogs...
r51871 impl Graph for Revlog {
fn parents(&self, rev: Revision) -> Result<[Revision; 2], GraphError> {
Raphaël Gomès
rust-revlog: add a Rust-only `InnerRevlog`...
r53057 self.index().parents(rev)
Raphaël Gomès
rust: implement the `Graph` trait for all revlogs...
r51871 }
}
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 impl Revlog {
/// Open a revlog index file.
///
/// It will also open the associated data file if index and data are not
/// interleaved.
pub fn open(
Raphaël Gomès
rust: add Vfs trait...
r52761 // Todo use the `Vfs` trait here once we create a function for mmap
store_vfs: &VfsImpl,
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 index_path: impl AsRef<Path>,
data_path: Option<&Path>,
Raphaël Gomès
rust-revlog: teach the revlog opening code to read the repo options...
r52084 options: RevlogOpenOptions,
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 ) -> Result<Self, HgError> {
Raphaël Gomès
rust-revlog: teach the revlog opening code to read the repo options...
r52084 Self::open_gen(store_vfs, index_path, data_path, options, None)
Arseniy Alekseyev
revlog: make the rust test for node hex prefix resolution exercise the nodemap
r51879 }
Raphaël Gomès
rust-revlog: add a Rust-only `InnerRevlog`...
r53057 fn index(&self) -> &Index {
&self.inner.index
}
Arseniy Alekseyev
revlog: make the rust test for node hex prefix resolution exercise the nodemap
r51879 fn open_gen(
Raphaël Gomès
rust: add Vfs trait...
r52761 // Todo use the `Vfs` trait here once we create a function for mmap
store_vfs: &VfsImpl,
Arseniy Alekseyev
revlog: make the rust test for node hex prefix resolution exercise the nodemap
r51879 index_path: impl AsRef<Path>,
data_path: Option<&Path>,
Raphaël Gomès
rust-revlog: teach the revlog opening code to read the repo options...
r52084 options: RevlogOpenOptions,
Arseniy Alekseyev
revlog: make the rust test for node hex prefix resolution exercise the nodemap
r51879 nodemap_for_test: Option<nodemap::NodeTree>,
) -> Result<Self, HgError> {
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 let index_path = index_path.as_ref();
Raphaël Gomès
rust-revlog: add a Rust-only `InnerRevlog`...
r53057 let index = open_index(store_vfs, index_path, options)?;
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832
let default_data_path = index_path.with_extension("d");
Raphaël Gomès
rust-revlog: add a Rust-only `InnerRevlog`...
r53057 let data_path = data_path.unwrap_or(&default_data_path);
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832
Raphaël Gomès
rust-revlog: teach the revlog opening code to read the repo options...
r52084 let nodemap = if index.is_inline() || !options.use_nodemap {
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 None
} else {
NodeMapDocket::read_from_file(store_vfs, index_path)?.map(
|(docket, data)| {
nodemap::NodeTree::load_bytes(
Box::new(data),
docket.data_length,
)
},
)
};
Arseniy Alekseyev
revlog: make the rust test for node hex prefix resolution exercise the nodemap
r51879 let nodemap = nodemap_for_test.or(nodemap);
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 Ok(Revlog {
Raphaël Gomès
rust-revlog: add a Rust-only `InnerRevlog`...
r53057 inner: InnerRevlog::new(
Box::new(store_vfs.clone()),
index,
index_path.to_path_buf(),
data_path.to_path_buf(),
options.data_config,
options.delta_config,
options.feature_config,
),
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 nodemap,
})
}
/// Return number of entries of the `Revlog`.
pub fn len(&self) -> usize {
Raphaël Gomès
rust-revlog: add a Rust-only `InnerRevlog`...
r53057 self.index().len()
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 }
/// Returns `true` if the `Revlog` has zero `entries`.
pub fn is_empty(&self) -> bool {
Raphaël Gomès
rust-revlog: add a Rust-only `InnerRevlog`...
r53057 self.index().is_empty()
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 }
/// Returns the node ID for the given revision number, if it exists in this
/// revlog
Raphaël Gomès
rust: use the new `UncheckedRevision` everywhere applicable...
r51870 pub fn node_from_rev(&self, rev: UncheckedRevision) -> Option<&Node> {
if rev == NULL_REVISION.into() {
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 return Some(&NULL_NODE);
}
Raphaël Gomès
rust-revlog: add a Rust-only `InnerRevlog`...
r53057 let rev = self.index().check_revision(rev)?;
Some(self.index().get_entry(rev)?.hash())
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 }
/// Return the revision number for the given node ID, if it exists in this
/// revlog
pub fn rev_from_node(
&self,
node: NodePrefix,
) -> Result<Revision, RevlogError> {
Arseniy Alekseyev
revlog: fix a bug where NULL_NODE failed to be resolved to NULL_REV...
r51878 if let Some(nodemap) = &self.nodemap {
Georges Racinet
rust-revlog: fix incorrect results with NULL_NODE prefixes...
r51637 nodemap
Raphaël Gomès
rust-revlog: add a Rust-only `InnerRevlog`...
r53057 .find_bin(self.index(), node)?
Raphaël Gomès
rust: improve `InvalidRevision` error message...
r52938 .ok_or(RevlogError::InvalidRevision(format!("{:x}", node)))
Georges Racinet
rust-revlog: fix incorrect results with NULL_NODE prefixes...
r51637 } else {
Raphaël Gomès
rust-revlog: move non-persistent-nodemap rev lookup to the index...
r53070 self.index().rev_from_node_no_persistent_nodemap(node)
Arseniy Alekseyev
revlog: fix a bug where NULL_NODE failed to be resolved to NULL_REV...
r51878 }
Georges Racinet
rust-revlog: split out method for `rev_from_node` without persistent nodemap...
r51636 }
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832
/// Returns whether the given revision exists in this revlog.
Raphaël Gomès
rust: use the new `UncheckedRevision` everywhere applicable...
r51870 pub fn has_rev(&self, rev: UncheckedRevision) -> bool {
Raphaël Gomès
rust-revlog: add a Rust-only `InnerRevlog`...
r53057 self.index().check_revision(rev).is_some()
}
Raphaël Gomès
rust: normalize `_for_unchecked_rev` naming among revlogs and the index...
r53187 pub fn get_entry(
Raphaël Gomès
rust-revlog: add a Rust-only `InnerRevlog`...
r53057 &self,
rev: Revision,
) -> Result<RevlogEntry, RevlogError> {
Raphaël Gomès
rust: normalize `_for_unchecked_rev` naming among revlogs and the index...
r53187 self.inner.get_entry(rev)
Raphaël Gomès
rust-revlog: add a Rust-only `InnerRevlog`...
r53057 }
Raphaël Gomès
rust: normalize `_for_unchecked_rev` naming among revlogs and the index...
r53187 pub fn get_entry_for_unchecked_rev(
Raphaël Gomès
rust-revlog: add a Rust-only `InnerRevlog`...
r53057 &self,
rev: UncheckedRevision,
) -> Result<RevlogEntry, RevlogError> {
Raphaël Gomès
rust: normalize `_for_unchecked_rev` naming among revlogs and the index...
r53187 self.inner.get_entry_for_unchecked_rev(rev)
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 }
/// Return the full data associated to a revision.
///
/// All entries required to build the final data out of deltas will be
Raphaël Gomès
rust: normalize `_for_unchecked_rev` naming among revlogs and the index...
r53187 /// retrieved as needed, and the deltas will be applied to the initial
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 /// snapshot to rebuild the final data.
Raphaël Gomès
rust: normalize `_for_unchecked_rev` naming among revlogs and the index...
r53187 pub fn get_data_for_unchecked_rev(
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 &self,
Raphaël Gomès
rust: use the new `UncheckedRevision` everywhere applicable...
r51870 rev: UncheckedRevision,
) -> Result<Cow<[u8]>, RevlogError> {
if rev == NULL_REVISION.into() {
return Ok(Cow::Borrowed(&[]));
};
Raphaël Gomès
rust: normalize `_for_unchecked_rev` naming among revlogs and the index...
r53187 self.get_entry_for_unchecked_rev(rev)?.data()
Raphaël Gomès
rust: use the new `UncheckedRevision` everywhere applicable...
r51870 }
Raphaël Gomès
rust: normalize `_for_unchecked_rev` naming among revlogs and the index...
r53187 /// [`Self::get_data_for_unchecked_rev`] for a checked [`Revision`].
pub fn get_data(&self, rev: Revision) -> Result<Cow<[u8]>, RevlogError> {
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 if rev == NULL_REVISION {
return Ok(Cow::Borrowed(&[]));
};
Raphaël Gomès
rust: normalize `_for_unchecked_rev` naming among revlogs and the index...
r53187 self.get_entry(rev)?.data()
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 }
/// Check the hash of some given data against the recorded hash.
pub fn check_hash(
&self,
p1: Revision,
p2: Revision,
expected: &[u8],
data: &[u8],
) -> bool {
Raphaël Gomès
rust-revlog: add a Rust-only `InnerRevlog`...
r53057 self.inner.check_hash(p1, p2, expected, data)
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 }
/// Build the full data of a revision out its snapshot
/// and its deltas.
Raphaël Gomès
rust-revlog: add a Rust-only `InnerRevlog`...
r53057 fn build_data_from_deltas<T>(
buffer: &mut dyn RevisionBuffer<Target = T>,
snapshot: &[u8],
deltas: &[impl AsRef<[u8]>],
) -> Result<(), RevlogError> {
if deltas.is_empty() {
buffer.extend_from_slice(snapshot);
return Ok(());
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 }
Raphaël Gomès
rust-revlog: add a Rust-only `InnerRevlog`...
r53057 let patches: Result<Vec<_>, _> = deltas
.iter()
.map(|d| patch::PatchList::new(d.as_ref()))
.collect();
let patch = patch::fold_patch_lists(&patches?);
patch.apply(buffer, snapshot);
Ok(())
}
}
type IndexData = Box<dyn Deref<Target = [u8]> + Send + Sync>;
Raphaël Gomès
rust: populate mmaps in a separate thread if possible...
r53074 /// TODO We should check for version 5.14+ at runtime, but we either should
/// add the `nix` dependency to get it efficiently, or vendor the code to read
/// both of which are overkill for such a feature. If we need this dependency
/// for more things later, we'll use it here too.
#[cfg(target_os = "linux")]
fn can_advise_populate_read() -> bool {
true
}
#[cfg(not(target_os = "linux"))]
fn can_advise_populate_read() -> bool {
false
}
/// Call `madvise` on the mmap with `MADV_POPULATE_READ` in a separate thread
/// to populate the mmap in the background for a small perf improvement.
#[cfg(target_os = "linux")]
fn advise_populate_read_mmap(mmap: &memmap2::Mmap) {
const MADV_POPULATE_READ: i32 = 22;
// This is fine because the mmap is still referenced for at least
// the duration of this function, and the kernel will reject any wrong
// address.
let ptr = mmap.as_ptr() as u64;
let len = mmap.len();
// Fire and forget. The `JoinHandle` returned by `spawn` is dropped right
// after the call, the thread is thus detached. We don't care about success
// or failure here.
std::thread::spawn(move || unsafe {
// mmap's pointer is always page-aligned on Linux. In the case of
// file-based mmap (which is our use-case), the length should be
// correct. If not, it's not a safety concern as the kernel will just
// ignore unmapped pages and return ENOMEM, which we will promptly
// ignore, because we don't care about any errors.
libc::madvise(ptr as *mut libc::c_void, len, MADV_POPULATE_READ);
});
}
#[cfg(not(target_os = "linux"))]
fn advise_populate_read_mmap(mmap: &memmap2::Mmap) {}
Raphaël Gomès
rust-revlog: add a Rust-only `InnerRevlog`...
r53057 /// Open the revlog [`Index`] at `index_path`, through the `store_vfs` and the
/// given `options`. This controls whether (and how) we `mmap` the index file,
/// and returns an empty buffer if the index does not exist on disk.
/// This is only used when doing pure-Rust work, in Python contexts this is
/// unused at the time of writing.
pub fn open_index(
store_vfs: &impl Vfs,
index_path: &Path,
options: RevlogOpenOptions,
) -> Result<Index, HgError> {
let buf: IndexData = match store_vfs.open_read(index_path) {
Ok(mut file) => {
let mut buf = if let Some(threshold) =
options.data_config.mmap_index_threshold
{
let size = store_vfs.file_size(&file)?;
if size >= threshold {
Raphaël Gomès
rust: populate mmap by default if available...
r53062 // TODO madvise populate read in a background thread
let mut mmap_options = MmapOptions::new();
Raphaël Gomès
rust: populate mmaps in a separate thread if possible...
r53074 if !can_advise_populate_read() {
// Fall back to populating in the main thread if
// post-creation advice is not supported.
// Does nothing on platforms where it's not defined.
mmap_options.populate();
}
Raphaël Gomès
rust-revlog: add a Rust-only `InnerRevlog`...
r53057 // Safety is "enforced" by locks and assuming other
// processes are well-behaved. If any misbehaving or
// malicious process does touch the index, it could lead
// to corruption. This is somewhat inherent to file-based
// `mmap`, though some platforms have some ways of
// mitigating.
// TODO linux: set the immutable flag with `chattr(1)`?
Raphaël Gomès
rust: populate mmap by default if available...
r53062 let mmap = unsafe { mmap_options.map(&file) }
Raphaël Gomès
rust-revlog: add a Rust-only `InnerRevlog`...
r53057 .when_reading_file(index_path)?;
Raphaël Gomès
rust: populate mmaps in a separate thread if possible...
r53074
if can_advise_populate_read() {
advise_populate_read_mmap(&mmap);
}
Raphaël Gomès
rust-revlog: add a Rust-only `InnerRevlog`...
r53057 Some(Box::new(mmap) as IndexData)
} else {
None
}
} else {
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 None
Raphaël Gomès
rust-revlog: add a Rust-only `InnerRevlog`...
r53057 };
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832
Raphaël Gomès
rust-revlog: add a Rust-only `InnerRevlog`...
r53057 if buf.is_none() {
let mut data = vec![];
file.read_to_end(&mut data).when_reading_file(index_path)?;
buf = Some(Box::new(data) as IndexData);
}
buf.unwrap()
Raphaël Gomès
rust: use the new `UncheckedRevision` everywhere applicable...
r51870 }
Raphaël Gomès
rust-revlog: add a Rust-only `InnerRevlog`...
r53057 Err(err) => match err {
HgError::IoError { error, context } => match error.kind() {
ErrorKind::NotFound => Box::<Vec<u8>>::default(),
_ => return Err(HgError::IoError { error, context }),
},
e => return Err(e),
},
};
let index = Index::new(buf, options.index_header())?;
Ok(index)
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 }
/// The revlog entry's bytes and the necessary informations to extract
/// the entry's data.
#[derive(Clone)]
Georges Racinet
rust-revlog: explicit naming for `RevlogEntry` lifetime...
r51269 pub struct RevlogEntry<'revlog> {
Raphaël Gomès
rust-revlog: add a Rust-only `InnerRevlog`...
r53057 revlog: &'revlog InnerRevlog,
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 rev: Revision,
uncompressed_len: i32,
p1: Revision,
p2: Revision,
flags: u16,
hash: Node,
}
Georges Racinet
rust-revlog: explicit naming for `RevlogEntry` lifetime...
r51269 impl<'revlog> RevlogEntry<'revlog> {
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 pub fn revision(&self) -> Revision {
self.rev
}
pub fn node(&self) -> &Node {
&self.hash
}
pub fn uncompressed_len(&self) -> Option<u32> {
u32::try_from(self.uncompressed_len).ok()
}
pub fn has_p1(&self) -> bool {
self.p1 != NULL_REVISION
}
Georges Racinet
rust-revlog: fix lifetime problem for RevlogEntry parent entries accessors...
r51270 pub fn p1_entry(
&self,
) -> Result<Option<RevlogEntry<'revlog>>, RevlogError> {
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 if self.p1 == NULL_REVISION {
Ok(None)
} else {
Raphaël Gomès
rust: normalize `_for_unchecked_rev` naming among revlogs and the index...
r53187 Ok(Some(self.revlog.get_entry(self.p1)?))
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 }
}
Georges Racinet
rust-revlog: fix lifetime problem for RevlogEntry parent entries accessors...
r51270 pub fn p2_entry(
&self,
) -> Result<Option<RevlogEntry<'revlog>>, RevlogError> {
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 if self.p2 == NULL_REVISION {
Ok(None)
} else {
Raphaël Gomès
rust: normalize `_for_unchecked_rev` naming among revlogs and the index...
r53187 Ok(Some(self.revlog.get_entry(self.p2)?))
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 }
}
pub fn p1(&self) -> Option<Revision> {
if self.p1 == NULL_REVISION {
None
} else {
Some(self.p1)
}
}
pub fn p2(&self) -> Option<Revision> {
if self.p2 == NULL_REVISION {
None
} else {
Some(self.p2)
}
}
pub fn is_censored(&self) -> bool {
(self.flags & REVISION_FLAG_CENSORED) != 0
}
pub fn has_length_affecting_flag_processor(&self) -> bool {
// Relevant Python code: revlog.size()
// note: ELLIPSIS is known to not change the content
(self.flags & (REVIDX_KNOWN_FLAGS ^ REVISION_FLAG_ELLIPSIS)) != 0
}
/// The data for this entry, after resolving deltas if any.
Raphaël Gomès
rust-revlog: add a Rust-only `InnerRevlog`...
r53057 /// Non-Python callers should probably call [`Self::data`] instead.
fn rawdata<G, T>(
&self,
stop_rev: Option<(Revision, &[u8])>,
with_buffer: G,
) -> Result<(), RevlogError>
where
G: FnOnce(
usize,
&mut dyn FnMut(
&mut dyn RevisionBuffer<Target = T>,
) -> Result<(), RevlogError>,
) -> Result<(), RevlogError>,
{
let (delta_chain, stopped) = self
.revlog
.delta_chain(self.revision(), stop_rev.map(|(r, _)| r))?;
let target_size =
self.uncompressed_len().map(|raw_size| 4 * raw_size as u64);
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832
Raphaël Gomès
rust-revlog: add a Rust-only `InnerRevlog`...
r53057 let deltas = self.revlog.chunks(delta_chain, target_size)?;
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832
Raphaël Gomès
rust-revlog: add a Rust-only `InnerRevlog`...
r53057 let (base_text, deltas) = if stopped {
(
stop_rev.as_ref().expect("last revision should be cached").1,
&deltas[..],
)
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 } else {
Raphaël Gomès
rust-revlog: add a Rust-only `InnerRevlog`...
r53057 let (buf, deltas) = deltas.split_at(1);
(buf[0].as_ref(), deltas)
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 };
Raphaël Gomès
rust-revlog: add a Rust-only `InnerRevlog`...
r53057 let size = self
.uncompressed_len()
.map(|l| l as usize)
.unwrap_or(base_text.len());
with_buffer(size, &mut |buf| {
Revlog::build_data_from_deltas(buf, base_text, deltas)?;
Ok(())
})?;
Ok(())
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 }
fn check_data(
&self,
Georges Racinet
rust-revlog: explicit naming for `RevlogEntry` lifetime...
r51269 data: Cow<'revlog, [u8]>,
Raphaël Gomès
rust: use the new `UncheckedRevision` everywhere applicable...
r51870 ) -> Result<Cow<'revlog, [u8]>, RevlogError> {
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 if self.revlog.check_hash(
self.p1,
self.p2,
self.hash.as_bytes(),
&data,
) {
Ok(data)
} else {
if (self.flags & REVISION_FLAG_ELLIPSIS) != 0 {
return Err(HgError::unsupported(
Raphaël Gomès
update: add a Rust fast-path when updating from null (and clean)...
r52953 "support for ellipsis nodes is missing",
Raphaël Gomès
rust: use the new `UncheckedRevision` everywhere applicable...
r51870 )
.into());
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 }
Err(corrupted(format!(
"hash check failed for revision {}",
self.rev
Raphaël Gomès
rust: use the new `UncheckedRevision` everywhere applicable...
r51870 ))
.into())
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 }
}
Raphaël Gomès
rust: use the new `UncheckedRevision` everywhere applicable...
r51870 pub fn data(&self) -> Result<Cow<'revlog, [u8]>, RevlogError> {
Raphaël Gomès
rust-revlog: add a Rust-only `InnerRevlog`...
r53057 // TODO figure out if there is ever a need for `Cow` here anymore.
let mut data = CoreRevisionBuffer::new();
Georges Racinet
rust-revlog: fix RevlogEntry.data() for NULL_REVISION...
r51639 if self.rev == NULL_REVISION {
Raphaël Gomès
rust-revlog: add a Rust-only `InnerRevlog`...
r53057 return Ok(data.finish().into());
Georges Racinet
rust-revlog: fix RevlogEntry.data() for NULL_REVISION...
r51639 }
Raphaël Gomès
rust-revlog: add a Rust-only `InnerRevlog`...
r53057 self.rawdata(None, |size, f| {
// Pre-allocate the expected size (received from the index)
data.resize(size);
// Actually fill the buffer
f(&mut data)?;
Ok(())
})?;
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 if self.is_censored() {
Raphaël Gomès
rust: use the new `UncheckedRevision` everywhere applicable...
r51870 return Err(HgError::CensoredNodeError.into());
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 }
Raphaël Gomès
rust-revlog: add a Rust-only `InnerRevlog`...
r53057 self.check_data(data.finish().into())
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 }
}
#[cfg(test)]
mod tests {
use super::*;
Raphaël Gomès
rust: don't star export from the `revlog` module...
r53075 use crate::revlog::index::IndexEntryBuilder;
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 use itertools::Itertools;
#[test]
fn test_empty() {
let temp = tempfile::tempdir().unwrap();
Raphaël Gomès
hg-core: add a complete VFS...
r53064 let vfs = VfsImpl::new(temp.path().to_owned(), false);
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 std::fs::write(temp.path().join("foo.i"), b"").unwrap();
Raphaël Gomès
rust: use new revlog configs in all revlog opening code...
r52760 std::fs::write(temp.path().join("foo.d"), b"").unwrap();
Raphaël Gomès
rust-revlog: teach the revlog opening code to read the repo options...
r52084 let revlog =
Raphaël Gomès
rust: use new revlog configs in all revlog opening code...
r52760 Revlog::open(&vfs, "foo.i", None, RevlogOpenOptions::default())
Raphaël Gomès
rust-revlog: teach the revlog opening code to read the repo options...
r52084 .unwrap();
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 assert!(revlog.is_empty());
assert_eq!(revlog.len(), 0);
Raphaël Gomès
rust: normalize `_for_unchecked_rev` naming among revlogs and the index...
r53187 assert!(revlog.get_entry_for_unchecked_rev(0.into()).is_err());
Raphaël Gomès
rust: use the new `UncheckedRevision` everywhere applicable...
r51870 assert!(!revlog.has_rev(0.into()));
Georges Racinet
rust-revlog: using constant in test...
r51638 assert_eq!(
revlog.rev_from_node(NULL_NODE.into()).unwrap(),
NULL_REVISION
);
Raphaël Gomès
rust: normalize `_for_unchecked_rev` naming among revlogs and the index...
r53187 let null_entry = revlog
.get_entry_for_unchecked_rev(NULL_REVISION.into())
.ok()
.unwrap();
Georges Racinet
rust-revlog: fix RevlogEntry.data() for NULL_REVISION...
r51639 assert_eq!(null_entry.revision(), NULL_REVISION);
assert!(null_entry.data().unwrap().is_empty());
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 }
#[test]
fn test_inline() {
let temp = tempfile::tempdir().unwrap();
Raphaël Gomès
hg-core: add a complete VFS...
r53064 let vfs = VfsImpl::new(temp.path().to_owned(), false);
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 let node0 = Node::from_hex("2ed2a3912a0b24502043eae84ee4b279c18b90dd")
.unwrap();
let node1 = Node::from_hex("b004912a8510032a0350a74daa2803dadfb00e12")
.unwrap();
let node2 = Node::from_hex("dd6ad206e907be60927b5a3117b97dffb2590582")
.unwrap();
let entry0_bytes = IndexEntryBuilder::new()
.is_first(true)
.with_version(1)
.with_inline(true)
.with_node(node0)
.build();
rust-index: stop calling `with_offset` in the tests...
r52337 let entry1_bytes = IndexEntryBuilder::new().with_node(node1).build();
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 let entry2_bytes = IndexEntryBuilder::new()
Raphaël Gomès
rust: make `Revision` a newtype...
r51872 .with_p1(Revision(0))
.with_p2(Revision(1))
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 .with_node(node2)
.build();
let contents = vec![entry0_bytes, entry1_bytes, entry2_bytes]
.into_iter()
.flatten()
.collect_vec();
std::fs::write(temp.path().join("foo.i"), contents).unwrap();
Raphaël Gomès
rust-revlog: teach the revlog opening code to read the repo options...
r52084 let revlog =
Raphaël Gomès
rust: use new revlog configs in all revlog opening code...
r52760 Revlog::open(&vfs, "foo.i", None, RevlogOpenOptions::default())
Raphaël Gomès
rust-revlog: teach the revlog opening code to read the repo options...
r52084 .unwrap();
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832
Raphaël Gomès
rust: normalize `_for_unchecked_rev` naming among revlogs and the index...
r53187 let entry0 =
revlog.get_entry_for_unchecked_rev(0.into()).ok().unwrap();
Raphaël Gomès
rust: make `Revision` a newtype...
r51872 assert_eq!(entry0.revision(), Revision(0));
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 assert_eq!(*entry0.node(), node0);
assert!(!entry0.has_p1());
assert_eq!(entry0.p1(), None);
assert_eq!(entry0.p2(), None);
let p1_entry = entry0.p1_entry().unwrap();
assert!(p1_entry.is_none());
let p2_entry = entry0.p2_entry().unwrap();
assert!(p2_entry.is_none());
Raphaël Gomès
rust: normalize `_for_unchecked_rev` naming among revlogs and the index...
r53187 let entry1 =
revlog.get_entry_for_unchecked_rev(1.into()).ok().unwrap();
Raphaël Gomès
rust: make `Revision` a newtype...
r51872 assert_eq!(entry1.revision(), Revision(1));
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 assert_eq!(*entry1.node(), node1);
assert!(!entry1.has_p1());
assert_eq!(entry1.p1(), None);
assert_eq!(entry1.p2(), None);
let p1_entry = entry1.p1_entry().unwrap();
assert!(p1_entry.is_none());
let p2_entry = entry1.p2_entry().unwrap();
assert!(p2_entry.is_none());
Raphaël Gomès
rust: normalize `_for_unchecked_rev` naming among revlogs and the index...
r53187 let entry2 =
revlog.get_entry_for_unchecked_rev(2.into()).ok().unwrap();
Raphaël Gomès
rust: make `Revision` a newtype...
r51872 assert_eq!(entry2.revision(), Revision(2));
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 assert_eq!(*entry2.node(), node2);
assert!(entry2.has_p1());
Raphaël Gomès
rust: make `Revision` a newtype...
r51872 assert_eq!(entry2.p1(), Some(Revision(0)));
assert_eq!(entry2.p2(), Some(Revision(1)));
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 let p1_entry = entry2.p1_entry().unwrap();
assert!(p1_entry.is_some());
Raphaël Gomès
rust: make `Revision` a newtype...
r51872 assert_eq!(p1_entry.unwrap().revision(), Revision(0));
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 let p2_entry = entry2.p2_entry().unwrap();
assert!(p2_entry.is_some());
Raphaël Gomès
rust: make `Revision` a newtype...
r51872 assert_eq!(p2_entry.unwrap().revision(), Revision(1));
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 }
Georges Racinet
rust-revlog: fix incorrect results with NULL_NODE prefixes...
r51637
#[test]
fn test_nodemap() {
let temp = tempfile::tempdir().unwrap();
Raphaël Gomès
hg-core: add a complete VFS...
r53064 let vfs = VfsImpl::new(temp.path().to_owned(), false);
Georges Racinet
rust-revlog: fix incorrect results with NULL_NODE prefixes...
r51637
// building a revlog with a forced Node starting with zeros
// This is a corruption, but it does not preclude using the nodemap
// if we don't try and access the data
let node0 = Node::from_hex("00d2a3912a0b24502043eae84ee4b279c18b90dd")
.unwrap();
let node1 = Node::from_hex("b004912a8510032a0350a74daa2803dadfb00e12")
.unwrap();
let entry0_bytes = IndexEntryBuilder::new()
.is_first(true)
.with_version(1)
.with_inline(true)
.with_node(node0)
.build();
rust-index: stop calling `with_offset` in the tests...
r52337 let entry1_bytes = IndexEntryBuilder::new().with_node(node1).build();
Georges Racinet
rust-revlog: fix incorrect results with NULL_NODE prefixes...
r51637 let contents = vec![entry0_bytes, entry1_bytes]
.into_iter()
.flatten()
.collect_vec();
std::fs::write(temp.path().join("foo.i"), contents).unwrap();
Arseniy Alekseyev
revlog: make the rust test for node hex prefix resolution exercise the nodemap
r51879
let mut idx = nodemap::tests::TestNtIndex::new();
branching: merge stable into default
r51886 idx.insert_node(Revision(0), node0).unwrap();
idx.insert_node(Revision(1), node1).unwrap();
Arseniy Alekseyev
revlog: make the rust test for node hex prefix resolution exercise the nodemap
r51879
Raphaël Gomès
rust-revlog: teach the revlog opening code to read the repo options...
r52084 let revlog = Revlog::open_gen(
&vfs,
"foo.i",
None,
Raphaël Gomès
rust: use new revlog configs in all revlog opening code...
r52760 RevlogOpenOptions::default(),
Raphaël Gomès
rust-revlog: teach the revlog opening code to read the repo options...
r52084 Some(idx.nt),
)
.unwrap();
Georges Racinet
rust-revlog: fix incorrect results with NULL_NODE prefixes...
r51637
// accessing the data shows the corruption
Raphaël Gomès
rust: normalize `_for_unchecked_rev` naming among revlogs and the index...
r53187 revlog
.get_entry_for_unchecked_rev(0.into())
.unwrap()
.data()
.unwrap_err();
Georges Racinet
rust-revlog: fix incorrect results with NULL_NODE prefixes...
r51637
Raphaël Gomès
rust: make `Revision` a newtype...
r51872 assert_eq!(
revlog.rev_from_node(NULL_NODE.into()).unwrap(),
Revision(-1)
);
assert_eq!(revlog.rev_from_node(node0.into()).unwrap(), Revision(0));
assert_eq!(revlog.rev_from_node(node1.into()).unwrap(), Revision(1));
Georges Racinet
rust-revlog: fix incorrect results with NULL_NODE prefixes...
r51637 assert_eq!(
revlog
.rev_from_node(NodePrefix::from_hex("000").unwrap())
.unwrap(),
Raphaël Gomès
rust: make `Revision` a newtype...
r51872 Revision(-1)
Georges Racinet
rust-revlog: fix incorrect results with NULL_NODE prefixes...
r51637 );
assert_eq!(
revlog
.rev_from_node(NodePrefix::from_hex("b00").unwrap())
.unwrap(),
Raphaël Gomès
rust: make `Revision` a newtype...
r51872 Revision(1)
Georges Racinet
rust-revlog: fix incorrect results with NULL_NODE prefixes...
r51637 );
// RevlogError does not implement PartialEq
// (ultimately because io::Error does not)
match revlog
.rev_from_node(NodePrefix::from_hex("00").unwrap())
.expect_err("Expected to give AmbiguousPrefix error")
{
RevlogError::AmbiguousPrefix => (),
e => {
panic!("Got another error than AmbiguousPrefix: {:?}", e);
}
};
}
Raphaël Gomès
rust-clippy: merge "revlog" module definition and struct implementation...
r50832 }