comparison rust/hg-core/src/revlog/revlog.rs @ 48198:61ce70fd420e

rhg: handle null changelog and manifest revisions Differential Revision: https://phab.mercurial-scm.org/D11650
author Arseniy Alekseyev <aalekseyev@janestreet.com>
date Tue, 12 Oct 2021 19:43:51 +0100
parents fecfea658127
children 9d0e5629cfbf
comparison
equal deleted inserted replaced
48197:63e86fc9bfec 48198:61ce70fd420e
70 data_path: Option<&Path>, 70 data_path: Option<&Path>,
71 ) -> Result<Self, HgError> { 71 ) -> Result<Self, HgError> {
72 let index_path = index_path.as_ref(); 72 let index_path = index_path.as_ref();
73 let index_mmap = repo.store_vfs().mmap_open(&index_path)?; 73 let index_mmap = repo.store_vfs().mmap_open(&index_path)?;
74 74
75 let version = get_version(&index_mmap); 75 let version = get_version(&index_mmap)?;
76 if version != 1 { 76 if version != 1 {
77 // A proper new version should have had a repo/store requirement. 77 // A proper new version should have had a repo/store requirement.
78 return Err(HgError::corrupted("corrupted revlog")); 78 return Err(HgError::corrupted("corrupted revlog"));
79 } 79 }
80 80
177 /// All entries required to build the final data out of deltas will be 177 /// All entries required to build the final data out of deltas will be
178 /// retrieved as needed, and the deltas will be applied to the inital 178 /// retrieved as needed, and the deltas will be applied to the inital
179 /// snapshot to rebuild the final data. 179 /// snapshot to rebuild the final data.
180 #[timed] 180 #[timed]
181 pub fn get_rev_data(&self, rev: Revision) -> Result<Vec<u8>, RevlogError> { 181 pub fn get_rev_data(&self, rev: Revision) -> Result<Vec<u8>, RevlogError> {
182 if rev == NULL_REVISION {
183 return Ok(vec![]);
184 };
182 // Todo return -> Cow 185 // Todo return -> Cow
183 let mut entry = self.get_entry(rev)?; 186 let mut entry = self.get_entry(rev)?;
184 let mut delta_chain = vec![]; 187 let mut delta_chain = vec![];
185 while let Some(base_rev) = entry.base_rev { 188 while let Some(base_rev) = entry.base_rev {
186 delta_chain.push(entry); 189 delta_chain.push(entry);
369 self.base_rev.is_some() 372 self.base_rev.is_some()
370 } 373 }
371 } 374 }
372 375
373 /// Format version of the revlog. 376 /// Format version of the revlog.
374 pub fn get_version(index_bytes: &[u8]) -> u16 { 377 pub fn get_version(index_bytes: &[u8]) -> Result<u16, HgError> {
375 BigEndian::read_u16(&index_bytes[2..=3]) 378 if index_bytes.len() == 0 {
379 return Ok(1);
380 };
381 if index_bytes.len() < 4 {
382 return Err(HgError::corrupted(
383 "corrupted revlog: can't read the index format header",
384 ));
385 };
386 Ok(BigEndian::read_u16(&index_bytes[2..=3]))
376 } 387 }
377 388
378 /// Calculate the hash of a revision given its data and its parents. 389 /// Calculate the hash of a revision given its data and its parents.
379 fn hash( 390 fn hash(
380 data: &[u8], 391 data: &[u8],