Mercurial > public > mercurial-scm > hg
view rust/hg-core/src/operations/cat.rs @ 49000:dd6b67d5c256 stable
rust: fix unsound `OwningDirstateMap`
As per the previous patch, `OwningDirstateMap` is unsound. Self-referential
structs are difficult to implement correctly in Rust since the compiler is
free to move structs around as much as it wants to. They are also very rarely
needed in practice, so the state-of-the-art on how they should be done within
the Rust rules is still a bit new.
The crate `ouroboros` is an attempt at providing a safe way (in the Rust sense)
of declaring self-referential structs. It is getting a lot attention and was
improved very quickly when soundness issues were found in the past: rather than
relying on our own (limited) review circle, we might as well use the de-facto
common crate to fix this problem. This will give us a much better chance of
finding issues should any new ones be discovered as well as the benefit of
fewer `unsafe` APIs of our own.
I was starting to think about how I would present a safe API to the old struct
but soon realized that the callback-based approach was already done in
`ouroboros`, along with a lot more care towards refusing incorrect structs.
In short: we don't return a mutable reference to the `DirstateMap` anymore, we
expect users of its API to pass a `FnOnce` that takes the map as an argument.
This allows our `OwningDirstateMap` to control the input and output lifetimes
of the code that modifies it to prevent such issues.
Changing to `ouroboros` meant changing every API with it, but it is relatively
low churn in the end. It correctly identified the example buggy modification of
`copy_map_insert` outlined in the previous patch as violating the borrow rules.
Differential Revision: https://phab.mercurial-scm.org/D12429
author | Rapha?l Gom?s <rgomes@octobus.net> |
---|---|
date | Tue, 05 Apr 2022 10:55:28 +0200 |
parents | 20d0d896183e |
children | 49131579db62 |
line wrap: on
line source
// list_tracked_files.rs // // Copyright 2020 Antoine Cezar <antoine.cezar@octobus.net> // // This software may be used and distributed according to the terms of the // GNU General Public License version 2 or any later version. use crate::repo::Repo; use crate::revlog::revlog::RevlogError; use crate::revlog::Node; use crate::utils::hg_path::HgPath; use crate::errors::HgError; use crate::manifest::Manifest; use crate::manifest::ManifestEntry; use itertools::put_back; use itertools::PutBack; use std::cmp::Ordering; pub struct CatOutput<'a> { /// Whether any file in the manifest matched the paths given as CLI /// arguments pub found_any: bool, /// The contents of matching files, in manifest order pub results: Vec<(&'a HgPath, Vec<u8>)>, /// Which of the CLI arguments did not match any manifest file pub missing: Vec<&'a HgPath>, /// The node ID that the given revset was resolved to pub node: Node, } // Find an item in an iterator over a sorted collection. fn find_item<'a>( i: &mut PutBack<impl Iterator<Item = Result<ManifestEntry<'a>, HgError>>>, needle: &HgPath, ) -> Result<Option<Node>, HgError> { loop { match i.next() { None => return Ok(None), Some(result) => { let entry = result?; match needle.as_bytes().cmp(entry.path.as_bytes()) { Ordering::Less => { i.put_back(Ok(entry)); return Ok(None); } Ordering::Greater => continue, Ordering::Equal => return Ok(Some(entry.node_id()?)), } } } } } fn find_files_in_manifest<'query>( manifest: &Manifest, query: impl Iterator<Item = &'query HgPath>, ) -> Result<(Vec<(&'query HgPath, Node)>, Vec<&'query HgPath>), HgError> { let mut manifest = put_back(manifest.iter()); let mut res = vec![]; let mut missing = vec![]; for file in query { match find_item(&mut manifest, file)? { None => missing.push(file), Some(item) => res.push((file, item)), } } return Ok((res, missing)); } /// Output the given revision of files /// /// * `root`: Repository root /// * `rev`: The revision to cat the files from. /// * `files`: The files to output. pub fn cat<'a>( repo: &Repo, revset: &str, mut files: Vec<&'a HgPath>, ) -> Result<CatOutput<'a>, RevlogError> { let rev = crate::revset::resolve_single(revset, repo)?; let manifest = repo.manifest_for_rev(rev)?; let node = *repo .changelog()? .node_from_rev(rev) .expect("should succeed when repo.manifest did"); let mut results: Vec<(&'a HgPath, Vec<u8>)> = vec![]; let mut found_any = false; files.sort_unstable(); let (found, missing) = find_files_in_manifest( &manifest, files.into_iter().map(|f| f.as_ref()), )?; for (file_path, file_node) in found { found_any = true; let file_log = repo.filelog(file_path)?; results.push(( file_path, file_log.data_for_node(file_node)?.into_file_data()?, )); } Ok(CatOutput { found_any, results, missing, node, }) }