forked from memvid/memvid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfooter.rs
More file actions
179 lines (164 loc) · 5.49 KB
/
footer.rs
File metadata and controls
179 lines (164 loc) · 5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
use std::convert::TryInto;
use blake3::Hasher;
use memchr::memrchr;
/// Magic trailer marker appended to every committed TOC.
pub const FOOTER_MAGIC: &[u8; 8] = b"MV2FOOT!";
/// Total size of a commit footer in bytes.
pub const FOOTER_SIZE: usize = FOOTER_MAGIC.len() + 8 + 32 + 8;
/// Parsed representation of the footer trailer.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CommitFooter {
pub toc_len: u64,
pub toc_hash: [u8; 32],
pub generation: u64,
}
impl CommitFooter {
/// Serialises the footer into a fixed-size byte array.
#[must_use]
pub fn encode(&self) -> [u8; FOOTER_SIZE] {
let mut buf = [0u8; FOOTER_SIZE];
buf[..FOOTER_MAGIC.len()].copy_from_slice(FOOTER_MAGIC);
buf[FOOTER_MAGIC.len()..FOOTER_MAGIC.len() + 8]
.copy_from_slice(&self.toc_len.to_le_bytes());
buf[FOOTER_MAGIC.len() + 8..FOOTER_MAGIC.len() + 40].copy_from_slice(&self.toc_hash);
buf[FOOTER_MAGIC.len() + 40..].copy_from_slice(&self.generation.to_le_bytes());
buf
}
/// Attempts to decode a footer from a byte slice.
#[must_use]
pub fn decode(bytes: &[u8]) -> Option<Self> {
if bytes.len() != FOOTER_SIZE {
return None;
}
if &bytes[..FOOTER_MAGIC.len()] != FOOTER_MAGIC {
return None;
}
let toc_len = u64::from_le_bytes(
bytes[FOOTER_MAGIC.len()..FOOTER_MAGIC.len() + 8]
.try_into()
.ok()?,
);
let mut toc_hash = [0u8; 32];
toc_hash.copy_from_slice(&bytes[FOOTER_MAGIC.len() + 8..FOOTER_MAGIC.len() + 40]);
let generation = u64::from_le_bytes(bytes[FOOTER_MAGIC.len() + 40..].try_into().ok()?);
Some(Self {
toc_len,
toc_hash,
generation,
})
}
#[must_use]
pub fn hash_matches(&self, toc_bytes: &[u8]) -> bool {
let mut hasher = Hasher::new();
hasher.update(toc_bytes);
hasher.finalize().as_bytes() == &self.toc_hash
}
}
/// Result of scanning a file for the last valid commit footer.
#[derive(Debug)]
pub struct FooterSlice<'a> {
pub footer_offset: usize,
pub toc_offset: usize,
pub footer: CommitFooter,
pub toc_bytes: &'a [u8],
}
/// Scan the provided bytes backwards to locate the most recent valid footer.
#[must_use]
pub fn find_last_valid_footer(bytes: &[u8]) -> Option<FooterSlice<'_>> {
if bytes.len() < FOOTER_SIZE {
return None;
}
let total_len = bytes.len();
let mut search_end = bytes.len();
while let Some(pos) = memrchr(FOOTER_MAGIC[0], &bytes[..search_end]) {
if pos + FOOTER_SIZE > total_len {
if pos == 0 {
break;
}
search_end = pos;
continue;
}
let candidate = &bytes[pos..pos + FOOTER_SIZE];
if let Some(footer) = CommitFooter::decode(candidate) {
let toc_end = pos;
let toc_len = footer.toc_len as usize;
if toc_len == 0 || toc_len > toc_end {
search_end = pos;
continue;
}
let toc_offset = toc_end - toc_len;
let toc_bytes = &bytes[toc_offset..toc_end];
if !footer.hash_matches(toc_bytes) {
search_end = pos;
continue;
}
return Some(FooterSlice {
footer_offset: pos,
toc_offset,
footer,
toc_bytes,
});
}
if pos == 0 {
break;
}
search_end = pos;
}
None
}
#[cfg(test)]
mod tests {
use super::*;
fn build_sample_bytes(generation: u64, toc: &[u8]) -> Vec<u8> {
let mut footer_hash = Hasher::new();
footer_hash.update(toc);
let mut buffer = Vec::new();
buffer.extend_from_slice(toc);
let footer = CommitFooter {
toc_len: toc.len() as u64,
toc_hash: *footer_hash.finalize().as_bytes(),
generation,
};
buffer.extend_from_slice(&footer.encode());
buffer
}
#[test]
fn encode_decode_roundtrip() {
let footer = CommitFooter {
toc_len: 123,
toc_hash: [0xAB; 32],
generation: 99,
};
let encoded = footer.encode();
let decoded = CommitFooter::decode(&encoded).expect("decode");
assert_eq!(footer, decoded);
}
#[test]
fn scan_finds_footer() {
let toc = vec![0xAA, 0xBB, 0xCC];
let bytes = build_sample_bytes(7, &toc);
let slice = find_last_valid_footer(&bytes).expect("footer present");
assert_eq!(slice.footer.generation, 7);
assert_eq!(slice.toc_bytes, toc);
assert_eq!(
&bytes[slice.footer_offset..slice.footer_offset + FOOTER_SIZE],
&slice.footer.encode()
);
}
#[test]
fn scan_skips_corrupt_footer() {
let toc = vec![1u8, 2, 3, 4];
let mut bytes = build_sample_bytes(1, &toc);
// Corrupt the hash of the first footer.
let idx = bytes.len() - FOOTER_SIZE + FOOTER_MAGIC.len() + 12;
bytes[idx] ^= 0xFF;
// Append a valid second footer.
let mut extra_toc = vec![9u8; 10];
extra_toc.push(42);
let appended = build_sample_bytes(2, &extra_toc);
bytes.extend_from_slice(&appended);
let slice = find_last_valid_footer(&bytes).expect("footer present");
assert_eq!(slice.footer.generation, 2);
assert_eq!(slice.toc_bytes, &extra_toc);
}
}