| 1 |
// Copyright 2019 Raphaël Gomès <[email protected]> |
| 2 |
// |
| 3 |
// This software may be used and distributed according to the terms of the |
| 4 |
// GNU General Public License version 2 or any later version. |
| 5 |
|
| 6 |
use byteorder::BigEndian; |
| 7 |
use byteorder::WriteBytesExt; |
| 8 |
use bytes_cast::unaligned; |
| 9 |
use bytes_cast::BytesCast; |
| 10 |
|
| 11 |
use crate::dirstate::entry::DirstateEntry; |
| 12 |
use crate::dirstate::entry::EntryState; |
| 13 |
use crate::errors::HgError; |
| 14 |
use crate::utils::hg_path::HgPath; |
| 15 |
use crate::DirstateParents; |
| 16 |
|
| 17 |
/// Parents are stored in the dirstate as byte hashes. |
| 18 |
pub const PARENT_SIZE: usize = 20; |
| 19 |
/// Dirstate entries have a static part of 8 + 32 + 32 + 32 + 32 bits. |
| 20 |
const MIN_ENTRY_SIZE: usize = 17; |
| 21 |
|
| 22 |
type ParseResult<'a> = ( |
| 23 |
&'a DirstateParents, |
| 24 |
Vec<(&'a HgPath, DirstateEntry)>, |
| 25 |
Vec<(&'a HgPath, &'a HgPath)>, |
| 26 |
); |
| 27 |
|
| 28 |
pub fn parse_dirstate_parents( |
| 29 |
contents: &[u8], |
| 30 |
) -> Result<&DirstateParents, HgError> { |
| 31 |
let contents_len = contents.len(); |
| 32 |
let (parents, _rest) = |
| 33 |
DirstateParents::from_bytes(contents).map_err(|_| { |
| 34 |
HgError::corrupted(format!( |
| 35 |
"Too little data for dirstate: {contents_len} bytes.", |
| 36 |
)) |
| 37 |
})?; |
| 38 |
Ok(parents) |
| 39 |
} |
| 40 |
|
| 41 |
#[tracing::instrument(level = "debug", skip_all)] |
| 42 |
pub fn parse_dirstate(contents: &[u8]) -> Result<ParseResult, HgError> { |
| 43 |
let mut copies = Vec::new(); |
| 44 |
let mut entries = Vec::new(); |
| 45 |
let parents = |
| 46 |
parse_dirstate_entries(contents, |path, entry, copy_source| { |
| 47 |
if let Some(source) = copy_source { |
| 48 |
copies.push((path, source)); |
| 49 |
} |
| 50 |
entries.push((path, *entry)); |
| 51 |
Ok(()) |
| 52 |
})?; |
| 53 |
Ok((parents, entries, copies)) |
| 54 |
} |
| 55 |
|
| 56 |
#[derive(BytesCast)] |
| 57 |
#[repr(C)] |
| 58 |
struct RawEntry { |
| 59 |
state: u8, |
| 60 |
mode: unaligned::I32Be, |
| 61 |
size: unaligned::I32Be, |
| 62 |
mtime: unaligned::I32Be, |
| 63 |
length: unaligned::I32Be, |
| 64 |
} |
| 65 |
|
| 66 |
pub fn parse_dirstate_entries<'a>( |
| 67 |
mut contents: &'a [u8], |
| 68 |
mut each_entry: impl FnMut( |
| 69 |
&'a HgPath, |
| 70 |
&DirstateEntry, |
| 71 |
Option<&'a HgPath>, |
| 72 |
) -> Result<(), HgError>, |
| 73 |
) -> Result<&'a DirstateParents, HgError> { |
| 74 |
let mut entry_idx = 0; |
| 75 |
let original_len = contents.len(); |
| 76 |
let (parents, rest) = |
| 77 |
DirstateParents::from_bytes(contents).map_err(|_| { |
| 78 |
HgError::corrupted(format!( |
| 79 |
"Too little data for dirstate: {} bytes.", |
| 80 |
original_len |
| 81 |
)) |
| 82 |
})?; |
| 83 |
contents = rest; |
| 84 |
while !contents.is_empty() { |
| 85 |
let (raw_entry, rest) = RawEntry::from_bytes(contents) |
| 86 |
.map_err(|_| HgError::corrupted(format!( |
| 87 |
"dirstate corrupted: ran out of bytes at entry header {}, offset {}.", |
| 88 |
entry_idx, original_len-contents.len())))?; |
| 89 |
|
| 90 |
let entry = DirstateEntry::from_v1_data( |
| 91 |
EntryState::try_from(raw_entry.state)?, |
| 92 |
raw_entry.mode.get(), |
| 93 |
raw_entry.size.get(), |
| 94 |
raw_entry.mtime.get(), |
| 95 |
); |
| 96 |
let filename_len = raw_entry.length.get() as usize; |
| 97 |
let (paths, rest) = |
| 98 |
u8::slice_from_bytes(rest, filename_len) |
| 99 |
.map_err(|_| |
| 100 |
HgError::corrupted(format!( |
| 101 |
"dirstate corrupted: ran out of bytes at entry {}, offset {} (expected {} bytes).", |
| 102 |
entry_idx, original_len-contents.len(), filename_len)) |
| 103 |
)?; |
| 104 |
|
| 105 |
// `paths` is either a single path, or two paths separated by a NULL |
| 106 |
// byte |
| 107 |
let mut iter = paths.splitn(2, |&byte| byte == b'\0'); |
| 108 |
let path = HgPath::new( |
| 109 |
iter.next().expect("splitn always yields at least one item"), |
| 110 |
); |
| 111 |
let copy_source = iter.next().map(HgPath::new); |
| 112 |
each_entry(path, &entry, copy_source)?; |
| 113 |
|
| 114 |
entry_idx += 1; |
| 115 |
contents = rest; |
| 116 |
} |
| 117 |
Ok(parents) |
| 118 |
} |
| 119 |
|
| 120 |
fn packed_filename_and_copy_source_size( |
| 121 |
filename: &HgPath, |
| 122 |
copy_source: Option<&HgPath>, |
| 123 |
) -> usize { |
| 124 |
filename.len() |
| 125 |
+ if let Some(source) = copy_source { |
| 126 |
b"\0".len() + source.len() |
| 127 |
} else { |
| 128 |
0 |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
pub fn packed_entry_size( |
| 133 |
filename: &HgPath, |
| 134 |
copy_source: Option<&HgPath>, |
| 135 |
) -> usize { |
| 136 |
MIN_ENTRY_SIZE + packed_filename_and_copy_source_size(filename, copy_source) |
| 137 |
} |
| 138 |
|
| 139 |
pub fn pack_entry( |
| 140 |
filename: &HgPath, |
| 141 |
entry: &DirstateEntry, |
| 142 |
copy_source: Option<&HgPath>, |
| 143 |
packed: &mut Vec<u8>, |
| 144 |
) { |
| 145 |
let length = packed_filename_and_copy_source_size(filename, copy_source); |
| 146 |
let (state, mode, size, mtime) = entry.v1_data(); |
| 147 |
|
| 148 |
// Unwrapping because `impl std::io::Write for Vec<u8>` never errors |
| 149 |
packed.write_u8(state).unwrap(); |
| 150 |
packed.write_i32::<BigEndian>(mode).unwrap(); |
| 151 |
packed.write_i32::<BigEndian>(size).unwrap(); |
| 152 |
packed.write_i32::<BigEndian>(mtime).unwrap(); |
| 153 |
packed.write_i32::<BigEndian>(length as i32).unwrap(); |
| 154 |
packed.extend(filename.as_bytes()); |
| 155 |
if let Some(source) = copy_source { |
| 156 |
packed.push(b'\0'); |
| 157 |
packed.extend(source.as_bytes()); |
| 158 |
} |
| 159 |
} |