Show More
@@ -1,730 +1,730 b'' | |||||
1 | // Copyright 2018-2023 Georges Racinet <georges.racinet@octobus.net> |
|
1 | // Copyright 2018-2023 Georges Racinet <georges.racinet@octobus.net> | |
2 | // and Mercurial contributors |
|
2 | // and Mercurial contributors | |
3 | // |
|
3 | // | |
4 | // This software may be used and distributed according to the terms of the |
|
4 | // This software may be used and distributed according to the terms of the | |
5 | // GNU General Public License version 2 or any later version. |
|
5 | // GNU General Public License version 2 or any later version. | |
6 | //! Mercurial concepts for handling revision history |
|
6 | //! Mercurial concepts for handling revision history | |
7 |
|
7 | |||
8 | pub mod node; |
|
8 | pub mod node; | |
9 | pub mod nodemap; |
|
9 | pub mod nodemap; | |
10 | mod nodemap_docket; |
|
10 | mod nodemap_docket; | |
11 | pub mod path_encode; |
|
11 | pub mod path_encode; | |
12 | pub use node::{FromHexError, Node, NodePrefix}; |
|
12 | pub use node::{FromHexError, Node, NodePrefix}; | |
13 | pub mod changelog; |
|
13 | pub mod changelog; | |
14 | pub mod filelog; |
|
14 | pub mod filelog; | |
15 | pub mod index; |
|
15 | pub mod index; | |
16 | pub mod manifest; |
|
16 | pub mod manifest; | |
17 | pub mod patch; |
|
17 | pub mod patch; | |
18 |
|
18 | |||
19 | use std::borrow::Cow; |
|
19 | use std::borrow::Cow; | |
20 | use std::io::Read; |
|
20 | use std::io::Read; | |
21 | use std::ops::Deref; |
|
21 | use std::ops::Deref; | |
22 | use std::path::Path; |
|
22 | use std::path::Path; | |
23 |
|
23 | |||
24 | use flate2::read::ZlibDecoder; |
|
24 | use flate2::read::ZlibDecoder; | |
25 | use sha1::{Digest, Sha1}; |
|
25 | use sha1::{Digest, Sha1}; | |
26 | use std::cell::RefCell; |
|
26 | use std::cell::RefCell; | |
27 | use zstd; |
|
27 | use zstd; | |
28 |
|
28 | |||
29 | use self::node::{NODE_BYTES_LENGTH, NULL_NODE}; |
|
29 | use self::node::{NODE_BYTES_LENGTH, NULL_NODE}; | |
30 | use self::nodemap_docket::NodeMapDocket; |
|
30 | use self::nodemap_docket::NodeMapDocket; | |
31 | use super::index::Index; |
|
31 | use super::index::Index; | |
32 | use super::nodemap::{NodeMap, NodeMapError}; |
|
32 | use super::nodemap::{NodeMap, NodeMapError}; | |
33 | use crate::errors::HgError; |
|
33 | use crate::errors::HgError; | |
34 | use crate::vfs::Vfs; |
|
34 | use crate::vfs::Vfs; | |
35 |
|
35 | |||
36 | /// Mercurial revision numbers |
|
36 | /// Mercurial revision numbers | |
37 | /// |
|
37 | /// | |
38 | /// As noted in revlog.c, revision numbers are actually encoded in |
|
38 | /// As noted in revlog.c, revision numbers are actually encoded in | |
39 | /// 4 bytes, and are liberally converted to ints, whence the i32 |
|
39 | /// 4 bytes, and are liberally converted to ints, whence the i32 | |
40 | pub type Revision = i32; |
|
40 | pub type Revision = i32; | |
41 |
|
41 | |||
42 | /// Marker expressing the absence of a parent |
|
42 | /// Marker expressing the absence of a parent | |
43 | /// |
|
43 | /// | |
44 | /// Independently of the actual representation, `NULL_REVISION` is guaranteed |
|
44 | /// Independently of the actual representation, `NULL_REVISION` is guaranteed | |
45 | /// to be smaller than all existing revisions. |
|
45 | /// to be smaller than all existing revisions. | |
46 | pub const NULL_REVISION: Revision = -1; |
|
46 | pub const NULL_REVISION: Revision = -1; | |
47 |
|
47 | |||
48 | /// Same as `mercurial.node.wdirrev` |
|
48 | /// Same as `mercurial.node.wdirrev` | |
49 | /// |
|
49 | /// | |
50 | /// This is also equal to `i32::max_value()`, but it's better to spell |
|
50 | /// This is also equal to `i32::max_value()`, but it's better to spell | |
51 | /// it out explicitely, same as in `mercurial.node` |
|
51 | /// it out explicitely, same as in `mercurial.node` | |
52 | #[allow(clippy::unreadable_literal)] |
|
52 | #[allow(clippy::unreadable_literal)] | |
53 | pub const WORKING_DIRECTORY_REVISION: Revision = 0x7fffffff; |
|
53 | pub const WORKING_DIRECTORY_REVISION: Revision = 0x7fffffff; | |
54 |
|
54 | |||
55 | pub const WORKING_DIRECTORY_HEX: &str = |
|
55 | pub const WORKING_DIRECTORY_HEX: &str = | |
56 | "ffffffffffffffffffffffffffffffffffffffff"; |
|
56 | "ffffffffffffffffffffffffffffffffffffffff"; | |
57 |
|
57 | |||
58 | /// The simplest expression of what we need of Mercurial DAGs. |
|
58 | /// The simplest expression of what we need of Mercurial DAGs. | |
59 | pub trait Graph { |
|
59 | pub trait Graph { | |
60 | /// Return the two parents of the given `Revision`. |
|
60 | /// Return the two parents of the given `Revision`. | |
61 | /// |
|
61 | /// | |
62 | /// Each of the parents can be independently `NULL_REVISION` |
|
62 | /// Each of the parents can be independently `NULL_REVISION` | |
63 | fn parents(&self, rev: Revision) -> Result<[Revision; 2], GraphError>; |
|
63 | fn parents(&self, rev: Revision) -> Result<[Revision; 2], GraphError>; | |
64 | } |
|
64 | } | |
65 |
|
65 | |||
66 | #[derive(Clone, Debug, PartialEq)] |
|
66 | #[derive(Clone, Debug, PartialEq)] | |
67 | pub enum GraphError { |
|
67 | pub enum GraphError { | |
68 | ParentOutOfRange(Revision), |
|
68 | ParentOutOfRange(Revision), | |
69 | WorkingDirectoryUnsupported, |
|
69 | WorkingDirectoryUnsupported, | |
70 | } |
|
70 | } | |
71 |
|
71 | |||
72 | /// The Mercurial Revlog Index |
|
72 | /// The Mercurial Revlog Index | |
73 | /// |
|
73 | /// | |
74 | /// This is currently limited to the minimal interface that is needed for |
|
74 | /// This is currently limited to the minimal interface that is needed for | |
75 | /// the [`nodemap`](nodemap/index.html) module |
|
75 | /// the [`nodemap`](nodemap/index.html) module | |
76 | pub trait RevlogIndex { |
|
76 | pub trait RevlogIndex { | |
77 | /// Total number of Revisions referenced in this index |
|
77 | /// Total number of Revisions referenced in this index | |
78 | fn len(&self) -> usize; |
|
78 | fn len(&self) -> usize; | |
79 |
|
79 | |||
80 | fn is_empty(&self) -> bool { |
|
80 | fn is_empty(&self) -> bool { | |
81 | self.len() == 0 |
|
81 | self.len() == 0 | |
82 | } |
|
82 | } | |
83 |
|
83 | |||
84 | /// Return a reference to the Node or `None` if rev is out of bounds |
|
84 | /// Return a reference to the Node or `None` if rev is out of bounds | |
85 | /// |
|
85 | /// | |
86 | /// `NULL_REVISION` is not considered to be out of bounds. |
|
86 | /// `NULL_REVISION` is not considered to be out of bounds. | |
87 | fn node(&self, rev: Revision) -> Option<&Node>; |
|
87 | fn node(&self, rev: Revision) -> Option<&Node>; | |
88 | } |
|
88 | } | |
89 |
|
89 | |||
90 | const REVISION_FLAG_CENSORED: u16 = 1 << 15; |
|
90 | const REVISION_FLAG_CENSORED: u16 = 1 << 15; | |
91 | const REVISION_FLAG_ELLIPSIS: u16 = 1 << 14; |
|
91 | const REVISION_FLAG_ELLIPSIS: u16 = 1 << 14; | |
92 | const REVISION_FLAG_EXTSTORED: u16 = 1 << 13; |
|
92 | const REVISION_FLAG_EXTSTORED: u16 = 1 << 13; | |
93 | const REVISION_FLAG_HASCOPIESINFO: u16 = 1 << 12; |
|
93 | const REVISION_FLAG_HASCOPIESINFO: u16 = 1 << 12; | |
94 |
|
94 | |||
95 | // Keep this in sync with REVIDX_KNOWN_FLAGS in |
|
95 | // Keep this in sync with REVIDX_KNOWN_FLAGS in | |
96 | // mercurial/revlogutils/flagutil.py |
|
96 | // mercurial/revlogutils/flagutil.py | |
97 | const REVIDX_KNOWN_FLAGS: u16 = REVISION_FLAG_CENSORED |
|
97 | const REVIDX_KNOWN_FLAGS: u16 = REVISION_FLAG_CENSORED | |
98 | | REVISION_FLAG_ELLIPSIS |
|
98 | | REVISION_FLAG_ELLIPSIS | |
99 | | REVISION_FLAG_EXTSTORED |
|
99 | | REVISION_FLAG_EXTSTORED | |
100 | | REVISION_FLAG_HASCOPIESINFO; |
|
100 | | REVISION_FLAG_HASCOPIESINFO; | |
101 |
|
101 | |||
102 | const NULL_REVLOG_ENTRY_FLAGS: u16 = 0; |
|
102 | const NULL_REVLOG_ENTRY_FLAGS: u16 = 0; | |
103 |
|
103 | |||
104 | #[derive(Debug, derive_more::From)] |
|
104 | #[derive(Debug, derive_more::From)] | |
105 | pub enum RevlogError { |
|
105 | pub enum RevlogError { | |
106 | InvalidRevision, |
|
106 | InvalidRevision, | |
107 | /// Working directory is not supported |
|
107 | /// Working directory is not supported | |
108 | WDirUnsupported, |
|
108 | WDirUnsupported, | |
109 | /// Found more than one entry whose ID match the requested prefix |
|
109 | /// Found more than one entry whose ID match the requested prefix | |
110 | AmbiguousPrefix, |
|
110 | AmbiguousPrefix, | |
111 | #[from] |
|
111 | #[from] | |
112 | Other(HgError), |
|
112 | Other(HgError), | |
113 | } |
|
113 | } | |
114 |
|
114 | |||
115 | impl From<NodeMapError> for RevlogError { |
|
115 | impl From<NodeMapError> for RevlogError { | |
116 | fn from(error: NodeMapError) -> Self { |
|
116 | fn from(error: NodeMapError) -> Self { | |
117 | match error { |
|
117 | match error { | |
118 | NodeMapError::MultipleResults => RevlogError::AmbiguousPrefix, |
|
118 | NodeMapError::MultipleResults => RevlogError::AmbiguousPrefix, | |
119 | NodeMapError::RevisionNotInIndex(rev) => RevlogError::corrupted( |
|
119 | NodeMapError::RevisionNotInIndex(rev) => RevlogError::corrupted( | |
120 | format!("nodemap point to revision {} not in index", rev), |
|
120 | format!("nodemap point to revision {} not in index", rev), | |
121 | ), |
|
121 | ), | |
122 | } |
|
122 | } | |
123 | } |
|
123 | } | |
124 | } |
|
124 | } | |
125 |
|
125 | |||
126 | fn corrupted<S: AsRef<str>>(context: S) -> HgError { |
|
126 | fn corrupted<S: AsRef<str>>(context: S) -> HgError { | |
127 | HgError::corrupted(format!("corrupted revlog, {}", context.as_ref())) |
|
127 | HgError::corrupted(format!("corrupted revlog, {}", context.as_ref())) | |
128 | } |
|
128 | } | |
129 |
|
129 | |||
130 | impl RevlogError { |
|
130 | impl RevlogError { | |
131 | fn corrupted<S: AsRef<str>>(context: S) -> Self { |
|
131 | fn corrupted<S: AsRef<str>>(context: S) -> Self { | |
132 | RevlogError::Other(corrupted(context)) |
|
132 | RevlogError::Other(corrupted(context)) | |
133 | } |
|
133 | } | |
134 | } |
|
134 | } | |
135 |
|
135 | |||
136 | /// Read only implementation of revlog. |
|
136 | /// Read only implementation of revlog. | |
137 | pub struct Revlog { |
|
137 | pub struct Revlog { | |
138 | /// When index and data are not interleaved: bytes of the revlog index. |
|
138 | /// When index and data are not interleaved: bytes of the revlog index. | |
139 | /// When index and data are interleaved: bytes of the revlog index and |
|
139 | /// When index and data are interleaved: bytes of the revlog index and | |
140 | /// data. |
|
140 | /// data. | |
141 | index: Index, |
|
141 | index: Index, | |
142 | /// When index and data are not interleaved: bytes of the revlog data |
|
142 | /// When index and data are not interleaved: bytes of the revlog data | |
143 | data_bytes: Option<Box<dyn Deref<Target = [u8]> + Send>>, |
|
143 | data_bytes: Option<Box<dyn Deref<Target = [u8]> + Send>>, | |
144 | /// When present on disk: the persistent nodemap for this revlog |
|
144 | /// When present on disk: the persistent nodemap for this revlog | |
145 | nodemap: Option<nodemap::NodeTree>, |
|
145 | nodemap: Option<nodemap::NodeTree>, | |
146 | } |
|
146 | } | |
147 |
|
147 | |||
148 | impl Revlog { |
|
148 | impl Revlog { | |
149 | /// Open a revlog index file. |
|
149 | /// Open a revlog index file. | |
150 | /// |
|
150 | /// | |
151 | /// It will also open the associated data file if index and data are not |
|
151 | /// It will also open the associated data file if index and data are not | |
152 | /// interleaved. |
|
152 | /// interleaved. | |
153 | pub fn open( |
|
153 | pub fn open( | |
154 | store_vfs: &Vfs, |
|
154 | store_vfs: &Vfs, | |
155 | index_path: impl AsRef<Path>, |
|
155 | index_path: impl AsRef<Path>, | |
156 | data_path: Option<&Path>, |
|
156 | data_path: Option<&Path>, | |
157 | use_nodemap: bool, |
|
157 | use_nodemap: bool, | |
158 | ) -> Result<Self, HgError> { |
|
158 | ) -> Result<Self, HgError> { | |
159 | let index_path = index_path.as_ref(); |
|
159 | let index_path = index_path.as_ref(); | |
160 | let index = { |
|
160 | let index = { | |
161 | match store_vfs.mmap_open_opt(&index_path)? { |
|
161 | match store_vfs.mmap_open_opt(&index_path)? { | |
162 | None => Index::new(Box::new(vec![])), |
|
162 | None => Index::new(Box::new(vec![])), | |
163 | Some(index_mmap) => { |
|
163 | Some(index_mmap) => { | |
164 | let index = Index::new(Box::new(index_mmap))?; |
|
164 | let index = Index::new(Box::new(index_mmap))?; | |
165 | Ok(index) |
|
165 | Ok(index) | |
166 | } |
|
166 | } | |
167 | } |
|
167 | } | |
168 | }?; |
|
168 | }?; | |
169 |
|
169 | |||
170 | let default_data_path = index_path.with_extension("d"); |
|
170 | let default_data_path = index_path.with_extension("d"); | |
171 |
|
171 | |||
172 | // type annotation required |
|
172 | // type annotation required | |
173 | // won't recognize Mmap as Deref<Target = [u8]> |
|
173 | // won't recognize Mmap as Deref<Target = [u8]> | |
174 | let data_bytes: Option<Box<dyn Deref<Target = [u8]> + Send>> = |
|
174 | let data_bytes: Option<Box<dyn Deref<Target = [u8]> + Send>> = | |
175 | if index.is_inline() { |
|
175 | if index.is_inline() { | |
176 | None |
|
176 | None | |
177 | } else { |
|
177 | } else { | |
178 | let data_path = data_path.unwrap_or(&default_data_path); |
|
178 | let data_path = data_path.unwrap_or(&default_data_path); | |
179 | let data_mmap = store_vfs.mmap_open(data_path)?; |
|
179 | let data_mmap = store_vfs.mmap_open(data_path)?; | |
180 | Some(Box::new(data_mmap)) |
|
180 | Some(Box::new(data_mmap)) | |
181 | }; |
|
181 | }; | |
182 |
|
182 | |||
183 | let nodemap = if index.is_inline() || !use_nodemap { |
|
183 | let nodemap = if index.is_inline() || !use_nodemap { | |
184 | None |
|
184 | None | |
185 | } else { |
|
185 | } else { | |
186 | NodeMapDocket::read_from_file(store_vfs, index_path)?.map( |
|
186 | NodeMapDocket::read_from_file(store_vfs, index_path)?.map( | |
187 | |(docket, data)| { |
|
187 | |(docket, data)| { | |
188 | nodemap::NodeTree::load_bytes( |
|
188 | nodemap::NodeTree::load_bytes( | |
189 | Box::new(data), |
|
189 | Box::new(data), | |
190 | docket.data_length, |
|
190 | docket.data_length, | |
191 | ) |
|
191 | ) | |
192 | }, |
|
192 | }, | |
193 | ) |
|
193 | ) | |
194 | }; |
|
194 | }; | |
195 |
|
195 | |||
196 | Ok(Revlog { |
|
196 | Ok(Revlog { | |
197 | index, |
|
197 | index, | |
198 | data_bytes, |
|
198 | data_bytes, | |
199 | nodemap, |
|
199 | nodemap, | |
200 | }) |
|
200 | }) | |
201 | } |
|
201 | } | |
202 |
|
202 | |||
203 | /// Return number of entries of the `Revlog`. |
|
203 | /// Return number of entries of the `Revlog`. | |
204 | pub fn len(&self) -> usize { |
|
204 | pub fn len(&self) -> usize { | |
205 | self.index.len() |
|
205 | self.index.len() | |
206 | } |
|
206 | } | |
207 |
|
207 | |||
208 | /// Returns `true` if the `Revlog` has zero `entries`. |
|
208 | /// Returns `true` if the `Revlog` has zero `entries`. | |
209 | pub fn is_empty(&self) -> bool { |
|
209 | pub fn is_empty(&self) -> bool { | |
210 | self.index.is_empty() |
|
210 | self.index.is_empty() | |
211 | } |
|
211 | } | |
212 |
|
212 | |||
213 | /// Returns the node ID for the given revision number, if it exists in this |
|
213 | /// Returns the node ID for the given revision number, if it exists in this | |
214 | /// revlog |
|
214 | /// revlog | |
215 | pub fn node_from_rev(&self, rev: Revision) -> Option<&Node> { |
|
215 | pub fn node_from_rev(&self, rev: Revision) -> Option<&Node> { | |
216 | if rev == NULL_REVISION { |
|
216 | if rev == NULL_REVISION { | |
217 | return Some(&NULL_NODE); |
|
217 | return Some(&NULL_NODE); | |
218 | } |
|
218 | } | |
219 | Some(self.index.get_entry(rev)?.hash()) |
|
219 | Some(self.index.get_entry(rev)?.hash()) | |
220 | } |
|
220 | } | |
221 |
|
221 | |||
222 | /// Return the revision number for the given node ID, if it exists in this |
|
222 | /// Return the revision number for the given node ID, if it exists in this | |
223 | /// revlog |
|
223 | /// revlog | |
224 | pub fn rev_from_node( |
|
224 | pub fn rev_from_node( | |
225 | &self, |
|
225 | &self, | |
226 | node: NodePrefix, |
|
226 | node: NodePrefix, | |
227 | ) -> Result<Revision, RevlogError> { |
|
227 | ) -> Result<Revision, RevlogError> { | |
228 | if node.is_prefix_of(&NULL_NODE) { |
|
228 | if node.is_prefix_of(&NULL_NODE) { | |
229 | return Ok(NULL_REVISION); |
|
229 | return Ok(NULL_REVISION); | |
230 | } |
|
230 | } | |
231 |
|
231 | |||
232 | if let Some(nodemap) = &self.nodemap { |
|
232 | if let Some(nodemap) = &self.nodemap { | |
233 | return nodemap |
|
233 | return nodemap | |
234 | .find_bin(&self.index, node)? |
|
234 | .find_bin(&self.index, node)? | |
235 | .ok_or(RevlogError::InvalidRevision); |
|
235 | .ok_or(RevlogError::InvalidRevision); | |
236 | } |
|
236 | } | |
237 |
|
237 | |||
238 | // Fallback to linear scan when a persistent nodemap is not present. |
|
238 | // Fallback to linear scan when a persistent nodemap is not present. | |
239 | // This happens when the persistent-nodemap experimental feature is not |
|
239 | // This happens when the persistent-nodemap experimental feature is not | |
240 | // enabled, or for small revlogs. |
|
240 | // enabled, or for small revlogs. | |
241 | // |
|
241 | // | |
242 | // TODO: consider building a non-persistent nodemap in memory to |
|
242 | // TODO: consider building a non-persistent nodemap in memory to | |
243 | // optimize these cases. |
|
243 | // optimize these cases. | |
244 | let mut found_by_prefix = None; |
|
244 | let mut found_by_prefix = None; | |
245 | for rev in (0..self.len() as Revision).rev() { |
|
245 | for rev in (0..self.len() as Revision).rev() { | |
246 | let index_entry = self.index.get_entry(rev).ok_or_else(|| { |
|
246 | let index_entry = self.index.get_entry(rev).ok_or_else(|| { | |
247 | HgError::corrupted( |
|
247 | HgError::corrupted( | |
248 | "revlog references a revision not in the index", |
|
248 | "revlog references a revision not in the index", | |
249 | ) |
|
249 | ) | |
250 | })?; |
|
250 | })?; | |
251 | if node == *index_entry.hash() { |
|
251 | if node == *index_entry.hash() { | |
252 | return Ok(rev); |
|
252 | return Ok(rev); | |
253 | } |
|
253 | } | |
254 | if node.is_prefix_of(index_entry.hash()) { |
|
254 | if node.is_prefix_of(index_entry.hash()) { | |
255 | if found_by_prefix.is_some() { |
|
255 | if found_by_prefix.is_some() { | |
256 | return Err(RevlogError::AmbiguousPrefix); |
|
256 | return Err(RevlogError::AmbiguousPrefix); | |
257 | } |
|
257 | } | |
258 | found_by_prefix = Some(rev) |
|
258 | found_by_prefix = Some(rev) | |
259 | } |
|
259 | } | |
260 | } |
|
260 | } | |
261 | found_by_prefix.ok_or(RevlogError::InvalidRevision) |
|
261 | found_by_prefix.ok_or(RevlogError::InvalidRevision) | |
262 | } |
|
262 | } | |
263 |
|
263 | |||
264 | /// Returns whether the given revision exists in this revlog. |
|
264 | /// Returns whether the given revision exists in this revlog. | |
265 | pub fn has_rev(&self, rev: Revision) -> bool { |
|
265 | pub fn has_rev(&self, rev: Revision) -> bool { | |
266 | self.index.get_entry(rev).is_some() |
|
266 | self.index.get_entry(rev).is_some() | |
267 | } |
|
267 | } | |
268 |
|
268 | |||
269 | /// Return the full data associated to a revision. |
|
269 | /// Return the full data associated to a revision. | |
270 | /// |
|
270 | /// | |
271 | /// All entries required to build the final data out of deltas will be |
|
271 | /// All entries required to build the final data out of deltas will be | |
272 | /// retrieved as needed, and the deltas will be applied to the inital |
|
272 | /// retrieved as needed, and the deltas will be applied to the inital | |
273 | /// snapshot to rebuild the final data. |
|
273 | /// snapshot to rebuild the final data. | |
274 | pub fn get_rev_data( |
|
274 | pub fn get_rev_data( | |
275 | &self, |
|
275 | &self, | |
276 | rev: Revision, |
|
276 | rev: Revision, | |
277 | ) -> Result<Cow<[u8]>, RevlogError> { |
|
277 | ) -> Result<Cow<[u8]>, RevlogError> { | |
278 | if rev == NULL_REVISION { |
|
278 | if rev == NULL_REVISION { | |
279 | return Ok(Cow::Borrowed(&[])); |
|
279 | return Ok(Cow::Borrowed(&[])); | |
280 | }; |
|
280 | }; | |
281 | Ok(self.get_entry(rev)?.data()?) |
|
281 | Ok(self.get_entry(rev)?.data()?) | |
282 | } |
|
282 | } | |
283 |
|
283 | |||
284 | /// Check the hash of some given data against the recorded hash. |
|
284 | /// Check the hash of some given data against the recorded hash. | |
285 | pub fn check_hash( |
|
285 | pub fn check_hash( | |
286 | &self, |
|
286 | &self, | |
287 | p1: Revision, |
|
287 | p1: Revision, | |
288 | p2: Revision, |
|
288 | p2: Revision, | |
289 | expected: &[u8], |
|
289 | expected: &[u8], | |
290 | data: &[u8], |
|
290 | data: &[u8], | |
291 | ) -> bool { |
|
291 | ) -> bool { | |
292 | let e1 = self.index.get_entry(p1); |
|
292 | let e1 = self.index.get_entry(p1); | |
293 | let h1 = match e1 { |
|
293 | let h1 = match e1 { | |
294 | Some(ref entry) => entry.hash(), |
|
294 | Some(ref entry) => entry.hash(), | |
295 | None => &NULL_NODE, |
|
295 | None => &NULL_NODE, | |
296 | }; |
|
296 | }; | |
297 | let e2 = self.index.get_entry(p2); |
|
297 | let e2 = self.index.get_entry(p2); | |
298 | let h2 = match e2 { |
|
298 | let h2 = match e2 { | |
299 | Some(ref entry) => entry.hash(), |
|
299 | Some(ref entry) => entry.hash(), | |
300 | None => &NULL_NODE, |
|
300 | None => &NULL_NODE, | |
301 | }; |
|
301 | }; | |
302 |
|
302 | |||
303 | hash(data, h1.as_bytes(), h2.as_bytes()) == expected |
|
303 | hash(data, h1.as_bytes(), h2.as_bytes()) == expected | |
304 | } |
|
304 | } | |
305 |
|
305 | |||
306 | /// Build the full data of a revision out its snapshot |
|
306 | /// Build the full data of a revision out its snapshot | |
307 | /// and its deltas. |
|
307 | /// and its deltas. | |
308 | fn build_data_from_deltas( |
|
308 | fn build_data_from_deltas( | |
309 | snapshot: RevlogEntry, |
|
309 | snapshot: RevlogEntry, | |
310 | deltas: &[RevlogEntry], |
|
310 | deltas: &[RevlogEntry], | |
311 | ) -> Result<Vec<u8>, HgError> { |
|
311 | ) -> Result<Vec<u8>, HgError> { | |
312 | let snapshot = snapshot.data_chunk()?; |
|
312 | let snapshot = snapshot.data_chunk()?; | |
313 | let deltas = deltas |
|
313 | let deltas = deltas | |
314 | .iter() |
|
314 | .iter() | |
315 | .rev() |
|
315 | .rev() | |
316 | .map(RevlogEntry::data_chunk) |
|
316 | .map(RevlogEntry::data_chunk) | |
317 | .collect::<Result<Vec<_>, _>>()?; |
|
317 | .collect::<Result<Vec<_>, _>>()?; | |
318 | let patches: Vec<_> = |
|
318 | let patches: Vec<_> = | |
319 | deltas.iter().map(|d| patch::PatchList::new(d)).collect(); |
|
319 | deltas.iter().map(|d| patch::PatchList::new(d)).collect(); | |
320 | let patch = patch::fold_patch_lists(&patches); |
|
320 | let patch = patch::fold_patch_lists(&patches); | |
321 | Ok(patch.apply(&snapshot)) |
|
321 | Ok(patch.apply(&snapshot)) | |
322 | } |
|
322 | } | |
323 |
|
323 | |||
324 | /// Return the revlog data. |
|
324 | /// Return the revlog data. | |
325 | fn data(&self) -> &[u8] { |
|
325 | fn data(&self) -> &[u8] { | |
326 | match &self.data_bytes { |
|
326 | match &self.data_bytes { | |
327 | Some(data_bytes) => data_bytes, |
|
327 | Some(data_bytes) => data_bytes, | |
328 | None => panic!( |
|
328 | None => panic!( | |
329 | "forgot to load the data or trying to access inline data" |
|
329 | "forgot to load the data or trying to access inline data" | |
330 | ), |
|
330 | ), | |
331 | } |
|
331 | } | |
332 | } |
|
332 | } | |
333 |
|
333 | |||
334 | pub fn make_null_entry(&self) -> RevlogEntry { |
|
334 | pub fn make_null_entry(&self) -> RevlogEntry { | |
335 | RevlogEntry { |
|
335 | RevlogEntry { | |
336 | revlog: self, |
|
336 | revlog: self, | |
337 | rev: NULL_REVISION, |
|
337 | rev: NULL_REVISION, | |
338 | bytes: b"", |
|
338 | bytes: b"", | |
339 | compressed_len: 0, |
|
339 | compressed_len: 0, | |
340 | uncompressed_len: 0, |
|
340 | uncompressed_len: 0, | |
341 | base_rev_or_base_of_delta_chain: None, |
|
341 | base_rev_or_base_of_delta_chain: None, | |
342 | p1: NULL_REVISION, |
|
342 | p1: NULL_REVISION, | |
343 | p2: NULL_REVISION, |
|
343 | p2: NULL_REVISION, | |
344 | flags: NULL_REVLOG_ENTRY_FLAGS, |
|
344 | flags: NULL_REVLOG_ENTRY_FLAGS, | |
345 | hash: NULL_NODE, |
|
345 | hash: NULL_NODE, | |
346 | } |
|
346 | } | |
347 | } |
|
347 | } | |
348 |
|
348 | |||
349 | /// Get an entry of the revlog. |
|
349 | /// Get an entry of the revlog. | |
350 | pub fn get_entry( |
|
350 | pub fn get_entry( | |
351 | &self, |
|
351 | &self, | |
352 | rev: Revision, |
|
352 | rev: Revision, | |
353 | ) -> Result<RevlogEntry, RevlogError> { |
|
353 | ) -> Result<RevlogEntry, RevlogError> { | |
354 | if rev == NULL_REVISION { |
|
354 | if rev == NULL_REVISION { | |
355 | return Ok(self.make_null_entry()); |
|
355 | return Ok(self.make_null_entry()); | |
356 | } |
|
356 | } | |
357 | let index_entry = self |
|
357 | let index_entry = self | |
358 | .index |
|
358 | .index | |
359 | .get_entry(rev) |
|
359 | .get_entry(rev) | |
360 | .ok_or(RevlogError::InvalidRevision)?; |
|
360 | .ok_or(RevlogError::InvalidRevision)?; | |
361 | let start = index_entry.offset(); |
|
361 | let start = index_entry.offset(); | |
362 | let end = start + index_entry.compressed_len() as usize; |
|
362 | let end = start + index_entry.compressed_len() as usize; | |
363 | let data = if self.index.is_inline() { |
|
363 | let data = if self.index.is_inline() { | |
364 | self.index.data(start, end) |
|
364 | self.index.data(start, end) | |
365 | } else { |
|
365 | } else { | |
366 | &self.data()[start..end] |
|
366 | &self.data()[start..end] | |
367 | }; |
|
367 | }; | |
368 | let entry = RevlogEntry { |
|
368 | let entry = RevlogEntry { | |
369 | revlog: self, |
|
369 | revlog: self, | |
370 | rev, |
|
370 | rev, | |
371 | bytes: data, |
|
371 | bytes: data, | |
372 | compressed_len: index_entry.compressed_len(), |
|
372 | compressed_len: index_entry.compressed_len(), | |
373 | uncompressed_len: index_entry.uncompressed_len(), |
|
373 | uncompressed_len: index_entry.uncompressed_len(), | |
374 | base_rev_or_base_of_delta_chain: if index_entry |
|
374 | base_rev_or_base_of_delta_chain: if index_entry | |
375 | .base_revision_or_base_of_delta_chain() |
|
375 | .base_revision_or_base_of_delta_chain() | |
376 | == rev |
|
376 | == rev | |
377 | { |
|
377 | { | |
378 | None |
|
378 | None | |
379 | } else { |
|
379 | } else { | |
380 | Some(index_entry.base_revision_or_base_of_delta_chain()) |
|
380 | Some(index_entry.base_revision_or_base_of_delta_chain()) | |
381 | }, |
|
381 | }, | |
382 | p1: index_entry.p1(), |
|
382 | p1: index_entry.p1(), | |
383 | p2: index_entry.p2(), |
|
383 | p2: index_entry.p2(), | |
384 | flags: index_entry.flags(), |
|
384 | flags: index_entry.flags(), | |
385 | hash: *index_entry.hash(), |
|
385 | hash: *index_entry.hash(), | |
386 | }; |
|
386 | }; | |
387 | Ok(entry) |
|
387 | Ok(entry) | |
388 | } |
|
388 | } | |
389 |
|
389 | |||
390 | /// when resolving internal references within revlog, any errors |
|
390 | /// when resolving internal references within revlog, any errors | |
391 | /// should be reported as corruption, instead of e.g. "invalid revision" |
|
391 | /// should be reported as corruption, instead of e.g. "invalid revision" | |
392 | fn get_entry_internal( |
|
392 | fn get_entry_internal( | |
393 | &self, |
|
393 | &self, | |
394 | rev: Revision, |
|
394 | rev: Revision, | |
395 | ) -> Result<RevlogEntry, HgError> { |
|
395 | ) -> Result<RevlogEntry, HgError> { | |
396 | self.get_entry(rev) |
|
396 | self.get_entry(rev) | |
397 | .map_err(|_| corrupted(format!("revision {} out of range", rev))) |
|
397 | .map_err(|_| corrupted(format!("revision {} out of range", rev))) | |
398 | } |
|
398 | } | |
399 | } |
|
399 | } | |
400 |
|
400 | |||
401 | /// The revlog entry's bytes and the necessary informations to extract |
|
401 | /// The revlog entry's bytes and the necessary informations to extract | |
402 | /// the entry's data. |
|
402 | /// the entry's data. | |
403 | #[derive(Clone)] |
|
403 | #[derive(Clone)] | |
404 | pub struct RevlogEntry<'revlog> { |
|
404 | pub struct RevlogEntry<'revlog> { | |
405 | revlog: &'revlog Revlog, |
|
405 | revlog: &'revlog Revlog, | |
406 | rev: Revision, |
|
406 | rev: Revision, | |
407 | bytes: &'revlog [u8], |
|
407 | bytes: &'revlog [u8], | |
408 | compressed_len: u32, |
|
408 | compressed_len: u32, | |
409 | uncompressed_len: i32, |
|
409 | uncompressed_len: i32, | |
410 | base_rev_or_base_of_delta_chain: Option<Revision>, |
|
410 | base_rev_or_base_of_delta_chain: Option<Revision>, | |
411 | p1: Revision, |
|
411 | p1: Revision, | |
412 | p2: Revision, |
|
412 | p2: Revision, | |
413 | flags: u16, |
|
413 | flags: u16, | |
414 | hash: Node, |
|
414 | hash: Node, | |
415 | } |
|
415 | } | |
416 |
|
416 | |||
417 | thread_local! { |
|
417 | thread_local! { | |
418 | // seems fine to [unwrap] here: this can only fail due to memory allocation |
|
418 | // seems fine to [unwrap] here: this can only fail due to memory allocation | |
419 | // failing, and it's normal for that to cause panic. |
|
419 | // failing, and it's normal for that to cause panic. | |
420 | static ZSTD_DECODER : RefCell<zstd::bulk::Decompressor<'static>> = |
|
420 | static ZSTD_DECODER : RefCell<zstd::bulk::Decompressor<'static>> = | |
421 | RefCell::new(zstd::bulk::Decompressor::new().ok().unwrap()); |
|
421 | RefCell::new(zstd::bulk::Decompressor::new().ok().unwrap()); | |
422 | } |
|
422 | } | |
423 |
|
423 | |||
424 | fn zstd_decompress_to_buffer( |
|
424 | fn zstd_decompress_to_buffer( | |
425 | bytes: &[u8], |
|
425 | bytes: &[u8], | |
426 | buf: &mut Vec<u8>, |
|
426 | buf: &mut Vec<u8>, | |
427 | ) -> Result<usize, std::io::Error> { |
|
427 | ) -> Result<usize, std::io::Error> { | |
428 | ZSTD_DECODER |
|
428 | ZSTD_DECODER | |
429 | .with(|decoder| decoder.borrow_mut().decompress_to_buffer(bytes, buf)) |
|
429 | .with(|decoder| decoder.borrow_mut().decompress_to_buffer(bytes, buf)) | |
430 | } |
|
430 | } | |
431 |
|
431 | |||
432 | impl<'revlog> RevlogEntry<'revlog> { |
|
432 | impl<'revlog> RevlogEntry<'revlog> { | |
433 | pub fn revision(&self) -> Revision { |
|
433 | pub fn revision(&self) -> Revision { | |
434 | self.rev |
|
434 | self.rev | |
435 | } |
|
435 | } | |
436 |
|
436 | |||
437 | pub fn node(&self) -> &Node { |
|
437 | pub fn node(&self) -> &Node { | |
438 | &self.hash |
|
438 | &self.hash | |
439 | } |
|
439 | } | |
440 |
|
440 | |||
441 | pub fn uncompressed_len(&self) -> Option<u32> { |
|
441 | pub fn uncompressed_len(&self) -> Option<u32> { | |
442 | u32::try_from(self.uncompressed_len).ok() |
|
442 | u32::try_from(self.uncompressed_len).ok() | |
443 | } |
|
443 | } | |
444 |
|
444 | |||
445 | pub fn has_p1(&self) -> bool { |
|
445 | pub fn has_p1(&self) -> bool { | |
446 | self.p1 != NULL_REVISION |
|
446 | self.p1 != NULL_REVISION | |
447 | } |
|
447 | } | |
448 |
|
448 | |||
449 | pub fn p1_entry( |
|
449 | pub fn p1_entry( | |
450 | &self, |
|
450 | &self, | |
451 | ) -> Result<Option<RevlogEntry<'revlog>>, RevlogError> { |
|
451 | ) -> Result<Option<RevlogEntry<'revlog>>, RevlogError> { | |
452 | if self.p1 == NULL_REVISION { |
|
452 | if self.p1 == NULL_REVISION { | |
453 | Ok(None) |
|
453 | Ok(None) | |
454 | } else { |
|
454 | } else { | |
455 | Ok(Some(self.revlog.get_entry(self.p1)?)) |
|
455 | Ok(Some(self.revlog.get_entry(self.p1)?)) | |
456 | } |
|
456 | } | |
457 | } |
|
457 | } | |
458 |
|
458 | |||
459 | pub fn p2_entry( |
|
459 | pub fn p2_entry( | |
460 | &self, |
|
460 | &self, | |
461 | ) -> Result<Option<RevlogEntry<'revlog>>, RevlogError> { |
|
461 | ) -> Result<Option<RevlogEntry<'revlog>>, RevlogError> { | |
462 | if self.p2 == NULL_REVISION { |
|
462 | if self.p2 == NULL_REVISION { | |
463 | Ok(None) |
|
463 | Ok(None) | |
464 | } else { |
|
464 | } else { | |
465 | Ok(Some(self.revlog.get_entry(self.p2)?)) |
|
465 | Ok(Some(self.revlog.get_entry(self.p2)?)) | |
466 | } |
|
466 | } | |
467 | } |
|
467 | } | |
468 |
|
468 | |||
469 | pub fn p1(&self) -> Option<Revision> { |
|
469 | pub fn p1(&self) -> Option<Revision> { | |
470 | if self.p1 == NULL_REVISION { |
|
470 | if self.p1 == NULL_REVISION { | |
471 | None |
|
471 | None | |
472 | } else { |
|
472 | } else { | |
473 | Some(self.p1) |
|
473 | Some(self.p1) | |
474 | } |
|
474 | } | |
475 | } |
|
475 | } | |
476 |
|
476 | |||
477 | pub fn p2(&self) -> Option<Revision> { |
|
477 | pub fn p2(&self) -> Option<Revision> { | |
478 | if self.p2 == NULL_REVISION { |
|
478 | if self.p2 == NULL_REVISION { | |
479 | None |
|
479 | None | |
480 | } else { |
|
480 | } else { | |
481 | Some(self.p2) |
|
481 | Some(self.p2) | |
482 | } |
|
482 | } | |
483 | } |
|
483 | } | |
484 |
|
484 | |||
485 | pub fn is_censored(&self) -> bool { |
|
485 | pub fn is_censored(&self) -> bool { | |
486 | (self.flags & REVISION_FLAG_CENSORED) != 0 |
|
486 | (self.flags & REVISION_FLAG_CENSORED) != 0 | |
487 | } |
|
487 | } | |
488 |
|
488 | |||
489 | pub fn has_length_affecting_flag_processor(&self) -> bool { |
|
489 | pub fn has_length_affecting_flag_processor(&self) -> bool { | |
490 | // Relevant Python code: revlog.size() |
|
490 | // Relevant Python code: revlog.size() | |
491 | // note: ELLIPSIS is known to not change the content |
|
491 | // note: ELLIPSIS is known to not change the content | |
492 | (self.flags & (REVIDX_KNOWN_FLAGS ^ REVISION_FLAG_ELLIPSIS)) != 0 |
|
492 | (self.flags & (REVIDX_KNOWN_FLAGS ^ REVISION_FLAG_ELLIPSIS)) != 0 | |
493 | } |
|
493 | } | |
494 |
|
494 | |||
495 | /// The data for this entry, after resolving deltas if any. |
|
495 | /// The data for this entry, after resolving deltas if any. | |
496 | pub fn rawdata(&self) -> Result<Cow<'revlog, [u8]>, HgError> { |
|
496 | pub fn rawdata(&self) -> Result<Cow<'revlog, [u8]>, HgError> { | |
497 | let mut entry = self.clone(); |
|
497 | let mut entry = self.clone(); | |
498 | let mut delta_chain = vec![]; |
|
498 | let mut delta_chain = vec![]; | |
499 |
|
499 | |||
500 | // The meaning of `base_rev_or_base_of_delta_chain` depends on |
|
500 | // The meaning of `base_rev_or_base_of_delta_chain` depends on | |
501 | // generaldelta. See the doc on `ENTRY_DELTA_BASE` in |
|
501 | // generaldelta. See the doc on `ENTRY_DELTA_BASE` in | |
502 | // `mercurial/revlogutils/constants.py` and the code in |
|
502 | // `mercurial/revlogutils/constants.py` and the code in | |
503 | // [_chaininfo] and in [index_deltachain]. |
|
503 | // [_chaininfo] and in [index_deltachain]. | |
504 | let uses_generaldelta = self.revlog.index.uses_generaldelta(); |
|
504 | let uses_generaldelta = self.revlog.index.uses_generaldelta(); | |
505 | while let Some(base_rev) = entry.base_rev_or_base_of_delta_chain { |
|
505 | while let Some(base_rev) = entry.base_rev_or_base_of_delta_chain { | |
506 | let base_rev = if uses_generaldelta { |
|
506 | let base_rev = if uses_generaldelta { | |
507 | base_rev |
|
507 | base_rev | |
508 | } else { |
|
508 | } else { | |
509 | entry.rev - 1 |
|
509 | entry.rev - 1 | |
510 | }; |
|
510 | }; | |
511 | delta_chain.push(entry); |
|
511 | delta_chain.push(entry); | |
512 | entry = self.revlog.get_entry_internal(base_rev)?; |
|
512 | entry = self.revlog.get_entry_internal(base_rev)?; | |
513 | } |
|
513 | } | |
514 |
|
514 | |||
515 | let data = if delta_chain.is_empty() { |
|
515 | let data = if delta_chain.is_empty() { | |
516 | entry.data_chunk()? |
|
516 | entry.data_chunk()? | |
517 | } else { |
|
517 | } else { | |
518 | Revlog::build_data_from_deltas(entry, &delta_chain)?.into() |
|
518 | Revlog::build_data_from_deltas(entry, &delta_chain)?.into() | |
519 | }; |
|
519 | }; | |
520 |
|
520 | |||
521 | Ok(data) |
|
521 | Ok(data) | |
522 | } |
|
522 | } | |
523 |
|
523 | |||
524 | fn check_data( |
|
524 | fn check_data( | |
525 | &self, |
|
525 | &self, | |
526 | data: Cow<'revlog, [u8]>, |
|
526 | data: Cow<'revlog, [u8]>, | |
527 | ) -> Result<Cow<'revlog, [u8]>, HgError> { |
|
527 | ) -> Result<Cow<'revlog, [u8]>, HgError> { | |
528 | if self.revlog.check_hash( |
|
528 | if self.revlog.check_hash( | |
529 | self.p1, |
|
529 | self.p1, | |
530 | self.p2, |
|
530 | self.p2, | |
531 | self.hash.as_bytes(), |
|
531 | self.hash.as_bytes(), | |
532 | &data, |
|
532 | &data, | |
533 | ) { |
|
533 | ) { | |
534 | Ok(data) |
|
534 | Ok(data) | |
535 | } else { |
|
535 | } else { | |
536 | if (self.flags & REVISION_FLAG_ELLIPSIS) != 0 { |
|
536 | if (self.flags & REVISION_FLAG_ELLIPSIS) != 0 { | |
537 | return Err(HgError::unsupported( |
|
537 | return Err(HgError::unsupported( | |
538 | "ellipsis revisions are not supported by rhg", |
|
538 | "ellipsis revisions are not supported by rhg", | |
539 | )); |
|
539 | )); | |
540 | } |
|
540 | } | |
541 | Err(corrupted(format!( |
|
541 | Err(corrupted(format!( | |
542 | "hash check failed for revision {}", |
|
542 | "hash check failed for revision {}", | |
543 | self.rev |
|
543 | self.rev | |
544 | ))) |
|
544 | ))) | |
545 | } |
|
545 | } | |
546 | } |
|
546 | } | |
547 |
|
547 | |||
548 | pub fn data(&self) -> Result<Cow<'revlog, [u8]>, HgError> { |
|
548 | pub fn data(&self) -> Result<Cow<'revlog, [u8]>, HgError> { | |
549 | let data = self.rawdata()?; |
|
549 | let data = self.rawdata()?; | |
550 | if self.is_censored() { |
|
550 | if self.is_censored() { | |
551 | return Err(HgError::CensoredNodeError); |
|
551 | return Err(HgError::CensoredNodeError); | |
552 | } |
|
552 | } | |
553 | self.check_data(data) |
|
553 | self.check_data(data) | |
554 | } |
|
554 | } | |
555 |
|
555 | |||
556 | /// Extract the data contained in the entry. |
|
556 | /// Extract the data contained in the entry. | |
557 | /// This may be a delta. (See `is_delta`.) |
|
557 | /// This may be a delta. (See `is_delta`.) | |
558 | fn data_chunk(&self) -> Result<Cow<'revlog, [u8]>, HgError> { |
|
558 | fn data_chunk(&self) -> Result<Cow<'revlog, [u8]>, HgError> { | |
559 | if self.bytes.is_empty() { |
|
559 | if self.bytes.is_empty() { | |
560 | return Ok(Cow::Borrowed(&[])); |
|
560 | return Ok(Cow::Borrowed(&[])); | |
561 | } |
|
561 | } | |
562 | match self.bytes[0] { |
|
562 | match self.bytes[0] { | |
563 | // Revision data is the entirety of the entry, including this |
|
563 | // Revision data is the entirety of the entry, including this | |
564 | // header. |
|
564 | // header. | |
565 | b'\0' => Ok(Cow::Borrowed(self.bytes)), |
|
565 | b'\0' => Ok(Cow::Borrowed(self.bytes)), | |
566 | // Raw revision data follows. |
|
566 | // Raw revision data follows. | |
567 | b'u' => Ok(Cow::Borrowed(&self.bytes[1..])), |
|
567 | b'u' => Ok(Cow::Borrowed(&self.bytes[1..])), | |
568 | // zlib (RFC 1950) data. |
|
568 | // zlib (RFC 1950) data. | |
569 | b'x' => Ok(Cow::Owned(self.uncompressed_zlib_data()?)), |
|
569 | b'x' => Ok(Cow::Owned(self.uncompressed_zlib_data()?)), | |
570 | // zstd data. |
|
570 | // zstd data. | |
571 | b'\x28' => Ok(Cow::Owned(self.uncompressed_zstd_data()?)), |
|
571 | b'\x28' => Ok(Cow::Owned(self.uncompressed_zstd_data()?)), | |
572 | // A proper new format should have had a repo/store requirement. |
|
572 | // A proper new format should have had a repo/store requirement. | |
573 | format_type => Err(corrupted(format!( |
|
573 | format_type => Err(corrupted(format!( | |
574 | "unknown compression header '{}'", |
|
574 | "unknown compression header '{}'", | |
575 | format_type |
|
575 | format_type | |
576 | ))), |
|
576 | ))), | |
577 | } |
|
577 | } | |
578 | } |
|
578 | } | |
579 |
|
579 | |||
580 | fn uncompressed_zlib_data(&self) -> Result<Vec<u8>, HgError> { |
|
580 | fn uncompressed_zlib_data(&self) -> Result<Vec<u8>, HgError> { | |
581 | let mut decoder = ZlibDecoder::new(self.bytes); |
|
581 | let mut decoder = ZlibDecoder::new(self.bytes); | |
582 | if self.is_delta() { |
|
582 | if self.is_delta() { | |
583 | let mut buf = Vec::with_capacity(self.compressed_len as usize); |
|
583 | let mut buf = Vec::with_capacity(self.compressed_len as usize); | |
584 | decoder |
|
584 | decoder | |
585 | .read_to_end(&mut buf) |
|
585 | .read_to_end(&mut buf) | |
586 | .map_err(|e| corrupted(e.to_string()))?; |
|
586 | .map_err(|e| corrupted(e.to_string()))?; | |
587 | Ok(buf) |
|
587 | Ok(buf) | |
588 | } else { |
|
588 | } else { | |
589 | let cap = self.uncompressed_len.max(0) as usize; |
|
589 | let cap = self.uncompressed_len.max(0) as usize; | |
590 | let mut buf = vec![0; cap]; |
|
590 | let mut buf = vec![0; cap]; | |
591 | decoder |
|
591 | decoder | |
592 | .read_exact(&mut buf) |
|
592 | .read_exact(&mut buf) | |
593 | .map_err(|e| corrupted(e.to_string()))?; |
|
593 | .map_err(|e| corrupted(e.to_string()))?; | |
594 | Ok(buf) |
|
594 | Ok(buf) | |
595 | } |
|
595 | } | |
596 | } |
|
596 | } | |
597 |
|
597 | |||
598 | fn uncompressed_zstd_data(&self) -> Result<Vec<u8>, HgError> { |
|
598 | fn uncompressed_zstd_data(&self) -> Result<Vec<u8>, HgError> { | |
599 | if self.is_delta() { |
|
599 | if self.is_delta() { | |
600 | let mut buf = Vec::with_capacity(self.compressed_len as usize); |
|
600 | let mut buf = Vec::with_capacity(self.compressed_len as usize); | |
601 | zstd::stream::copy_decode(self.bytes, &mut buf) |
|
601 | zstd::stream::copy_decode(self.bytes, &mut buf) | |
602 | .map_err(|e| corrupted(e.to_string()))?; |
|
602 | .map_err(|e| corrupted(e.to_string()))?; | |
603 | Ok(buf) |
|
603 | Ok(buf) | |
604 | } else { |
|
604 | } else { | |
605 | let cap = self.uncompressed_len.max(0) as usize; |
|
605 | let cap = self.uncompressed_len.max(0) as usize; | |
606 |
let mut buf = |
|
606 | let mut buf = Vec::with_capacity(cap); | |
607 | let len = zstd_decompress_to_buffer(self.bytes, &mut buf) |
|
607 | let len = zstd_decompress_to_buffer(self.bytes, &mut buf) | |
608 | .map_err(|e| corrupted(e.to_string()))?; |
|
608 | .map_err(|e| corrupted(e.to_string()))?; | |
609 | if len != self.uncompressed_len as usize { |
|
609 | if len != self.uncompressed_len as usize { | |
610 | Err(corrupted("uncompressed length does not match")) |
|
610 | Err(corrupted("uncompressed length does not match")) | |
611 | } else { |
|
611 | } else { | |
612 | Ok(buf) |
|
612 | Ok(buf) | |
613 | } |
|
613 | } | |
614 | } |
|
614 | } | |
615 | } |
|
615 | } | |
616 |
|
616 | |||
617 | /// Tell if the entry is a snapshot or a delta |
|
617 | /// Tell if the entry is a snapshot or a delta | |
618 | /// (influences on decompression). |
|
618 | /// (influences on decompression). | |
619 | fn is_delta(&self) -> bool { |
|
619 | fn is_delta(&self) -> bool { | |
620 | self.base_rev_or_base_of_delta_chain.is_some() |
|
620 | self.base_rev_or_base_of_delta_chain.is_some() | |
621 | } |
|
621 | } | |
622 | } |
|
622 | } | |
623 |
|
623 | |||
624 | /// Calculate the hash of a revision given its data and its parents. |
|
624 | /// Calculate the hash of a revision given its data and its parents. | |
625 | fn hash( |
|
625 | fn hash( | |
626 | data: &[u8], |
|
626 | data: &[u8], | |
627 | p1_hash: &[u8], |
|
627 | p1_hash: &[u8], | |
628 | p2_hash: &[u8], |
|
628 | p2_hash: &[u8], | |
629 | ) -> [u8; NODE_BYTES_LENGTH] { |
|
629 | ) -> [u8; NODE_BYTES_LENGTH] { | |
630 | let mut hasher = Sha1::new(); |
|
630 | let mut hasher = Sha1::new(); | |
631 | let (a, b) = (p1_hash, p2_hash); |
|
631 | let (a, b) = (p1_hash, p2_hash); | |
632 | if a > b { |
|
632 | if a > b { | |
633 | hasher.update(b); |
|
633 | hasher.update(b); | |
634 | hasher.update(a); |
|
634 | hasher.update(a); | |
635 | } else { |
|
635 | } else { | |
636 | hasher.update(a); |
|
636 | hasher.update(a); | |
637 | hasher.update(b); |
|
637 | hasher.update(b); | |
638 | } |
|
638 | } | |
639 | hasher.update(data); |
|
639 | hasher.update(data); | |
640 | *hasher.finalize().as_ref() |
|
640 | *hasher.finalize().as_ref() | |
641 | } |
|
641 | } | |
642 |
|
642 | |||
643 | #[cfg(test)] |
|
643 | #[cfg(test)] | |
644 | mod tests { |
|
644 | mod tests { | |
645 | use super::*; |
|
645 | use super::*; | |
646 | use crate::index::{IndexEntryBuilder, INDEX_ENTRY_SIZE}; |
|
646 | use crate::index::{IndexEntryBuilder, INDEX_ENTRY_SIZE}; | |
647 | use itertools::Itertools; |
|
647 | use itertools::Itertools; | |
648 |
|
648 | |||
649 | #[test] |
|
649 | #[test] | |
650 | fn test_empty() { |
|
650 | fn test_empty() { | |
651 | let temp = tempfile::tempdir().unwrap(); |
|
651 | let temp = tempfile::tempdir().unwrap(); | |
652 | let vfs = Vfs { base: temp.path() }; |
|
652 | let vfs = Vfs { base: temp.path() }; | |
653 | std::fs::write(temp.path().join("foo.i"), b"").unwrap(); |
|
653 | std::fs::write(temp.path().join("foo.i"), b"").unwrap(); | |
654 | let revlog = Revlog::open(&vfs, "foo.i", None, false).unwrap(); |
|
654 | let revlog = Revlog::open(&vfs, "foo.i", None, false).unwrap(); | |
655 | assert!(revlog.is_empty()); |
|
655 | assert!(revlog.is_empty()); | |
656 | assert_eq!(revlog.len(), 0); |
|
656 | assert_eq!(revlog.len(), 0); | |
657 | assert!(revlog.get_entry(0).is_err()); |
|
657 | assert!(revlog.get_entry(0).is_err()); | |
658 | assert!(!revlog.has_rev(0)); |
|
658 | assert!(!revlog.has_rev(0)); | |
659 | } |
|
659 | } | |
660 |
|
660 | |||
661 | #[test] |
|
661 | #[test] | |
662 | fn test_inline() { |
|
662 | fn test_inline() { | |
663 | let temp = tempfile::tempdir().unwrap(); |
|
663 | let temp = tempfile::tempdir().unwrap(); | |
664 | let vfs = Vfs { base: temp.path() }; |
|
664 | let vfs = Vfs { base: temp.path() }; | |
665 | let node0 = Node::from_hex("2ed2a3912a0b24502043eae84ee4b279c18b90dd") |
|
665 | let node0 = Node::from_hex("2ed2a3912a0b24502043eae84ee4b279c18b90dd") | |
666 | .unwrap(); |
|
666 | .unwrap(); | |
667 | let node1 = Node::from_hex("b004912a8510032a0350a74daa2803dadfb00e12") |
|
667 | let node1 = Node::from_hex("b004912a8510032a0350a74daa2803dadfb00e12") | |
668 | .unwrap(); |
|
668 | .unwrap(); | |
669 | let node2 = Node::from_hex("dd6ad206e907be60927b5a3117b97dffb2590582") |
|
669 | let node2 = Node::from_hex("dd6ad206e907be60927b5a3117b97dffb2590582") | |
670 | .unwrap(); |
|
670 | .unwrap(); | |
671 | let entry0_bytes = IndexEntryBuilder::new() |
|
671 | let entry0_bytes = IndexEntryBuilder::new() | |
672 | .is_first(true) |
|
672 | .is_first(true) | |
673 | .with_version(1) |
|
673 | .with_version(1) | |
674 | .with_inline(true) |
|
674 | .with_inline(true) | |
675 | .with_offset(INDEX_ENTRY_SIZE) |
|
675 | .with_offset(INDEX_ENTRY_SIZE) | |
676 | .with_node(node0) |
|
676 | .with_node(node0) | |
677 | .build(); |
|
677 | .build(); | |
678 | let entry1_bytes = IndexEntryBuilder::new() |
|
678 | let entry1_bytes = IndexEntryBuilder::new() | |
679 | .with_offset(INDEX_ENTRY_SIZE) |
|
679 | .with_offset(INDEX_ENTRY_SIZE) | |
680 | .with_node(node1) |
|
680 | .with_node(node1) | |
681 | .build(); |
|
681 | .build(); | |
682 | let entry2_bytes = IndexEntryBuilder::new() |
|
682 | let entry2_bytes = IndexEntryBuilder::new() | |
683 | .with_offset(INDEX_ENTRY_SIZE) |
|
683 | .with_offset(INDEX_ENTRY_SIZE) | |
684 | .with_p1(0) |
|
684 | .with_p1(0) | |
685 | .with_p2(1) |
|
685 | .with_p2(1) | |
686 | .with_node(node2) |
|
686 | .with_node(node2) | |
687 | .build(); |
|
687 | .build(); | |
688 | let contents = vec![entry0_bytes, entry1_bytes, entry2_bytes] |
|
688 | let contents = vec![entry0_bytes, entry1_bytes, entry2_bytes] | |
689 | .into_iter() |
|
689 | .into_iter() | |
690 | .flatten() |
|
690 | .flatten() | |
691 | .collect_vec(); |
|
691 | .collect_vec(); | |
692 | std::fs::write(temp.path().join("foo.i"), contents).unwrap(); |
|
692 | std::fs::write(temp.path().join("foo.i"), contents).unwrap(); | |
693 | let revlog = Revlog::open(&vfs, "foo.i", None, false).unwrap(); |
|
693 | let revlog = Revlog::open(&vfs, "foo.i", None, false).unwrap(); | |
694 |
|
694 | |||
695 | let entry0 = revlog.get_entry(0).ok().unwrap(); |
|
695 | let entry0 = revlog.get_entry(0).ok().unwrap(); | |
696 | assert_eq!(entry0.revision(), 0); |
|
696 | assert_eq!(entry0.revision(), 0); | |
697 | assert_eq!(*entry0.node(), node0); |
|
697 | assert_eq!(*entry0.node(), node0); | |
698 | assert!(!entry0.has_p1()); |
|
698 | assert!(!entry0.has_p1()); | |
699 | assert_eq!(entry0.p1(), None); |
|
699 | assert_eq!(entry0.p1(), None); | |
700 | assert_eq!(entry0.p2(), None); |
|
700 | assert_eq!(entry0.p2(), None); | |
701 | let p1_entry = entry0.p1_entry().unwrap(); |
|
701 | let p1_entry = entry0.p1_entry().unwrap(); | |
702 | assert!(p1_entry.is_none()); |
|
702 | assert!(p1_entry.is_none()); | |
703 | let p2_entry = entry0.p2_entry().unwrap(); |
|
703 | let p2_entry = entry0.p2_entry().unwrap(); | |
704 | assert!(p2_entry.is_none()); |
|
704 | assert!(p2_entry.is_none()); | |
705 |
|
705 | |||
706 | let entry1 = revlog.get_entry(1).ok().unwrap(); |
|
706 | let entry1 = revlog.get_entry(1).ok().unwrap(); | |
707 | assert_eq!(entry1.revision(), 1); |
|
707 | assert_eq!(entry1.revision(), 1); | |
708 | assert_eq!(*entry1.node(), node1); |
|
708 | assert_eq!(*entry1.node(), node1); | |
709 | assert!(!entry1.has_p1()); |
|
709 | assert!(!entry1.has_p1()); | |
710 | assert_eq!(entry1.p1(), None); |
|
710 | assert_eq!(entry1.p1(), None); | |
711 | assert_eq!(entry1.p2(), None); |
|
711 | assert_eq!(entry1.p2(), None); | |
712 | let p1_entry = entry1.p1_entry().unwrap(); |
|
712 | let p1_entry = entry1.p1_entry().unwrap(); | |
713 | assert!(p1_entry.is_none()); |
|
713 | assert!(p1_entry.is_none()); | |
714 | let p2_entry = entry1.p2_entry().unwrap(); |
|
714 | let p2_entry = entry1.p2_entry().unwrap(); | |
715 | assert!(p2_entry.is_none()); |
|
715 | assert!(p2_entry.is_none()); | |
716 |
|
716 | |||
717 | let entry2 = revlog.get_entry(2).ok().unwrap(); |
|
717 | let entry2 = revlog.get_entry(2).ok().unwrap(); | |
718 | assert_eq!(entry2.revision(), 2); |
|
718 | assert_eq!(entry2.revision(), 2); | |
719 | assert_eq!(*entry2.node(), node2); |
|
719 | assert_eq!(*entry2.node(), node2); | |
720 | assert!(entry2.has_p1()); |
|
720 | assert!(entry2.has_p1()); | |
721 | assert_eq!(entry2.p1(), Some(0)); |
|
721 | assert_eq!(entry2.p1(), Some(0)); | |
722 | assert_eq!(entry2.p2(), Some(1)); |
|
722 | assert_eq!(entry2.p2(), Some(1)); | |
723 | let p1_entry = entry2.p1_entry().unwrap(); |
|
723 | let p1_entry = entry2.p1_entry().unwrap(); | |
724 | assert!(p1_entry.is_some()); |
|
724 | assert!(p1_entry.is_some()); | |
725 | assert_eq!(p1_entry.unwrap().revision(), 0); |
|
725 | assert_eq!(p1_entry.unwrap().revision(), 0); | |
726 | let p2_entry = entry2.p2_entry().unwrap(); |
|
726 | let p2_entry = entry2.p2_entry().unwrap(); | |
727 | assert!(p2_entry.is_some()); |
|
727 | assert!(p2_entry.is_some()); | |
728 | assert_eq!(p2_entry.unwrap().revision(), 1); |
|
728 | assert_eq!(p2_entry.unwrap().revision(), 1); | |
729 | } |
|
729 | } | |
730 | } |
|
730 | } |
General Comments 0
You need to be logged in to leave comments.
Login now