Skip to content

Commit 10274b5

Browse files
committed
Add xor.dat support if the file exists
1 parent 78155de commit 10274b5

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

src/new_index/fetch.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,33 @@ fn blkfiles_fetcher(
149149
fn blkfiles_reader(blk_files: Vec<PathBuf>) -> Fetcher<Vec<u8>> {
150150
let chan = SyncChannel::new(1);
151151
let sender = chan.sender();
152+
let xor_key = blk_files.first().and_then(|p| {
153+
let xor_file = p
154+
.parent()
155+
.expect("blk.dat files must exist in a directory")
156+
.join("xor.dat");
157+
if xor_file.exists() {
158+
Some(fs::read(xor_file).expect("xor.dat exists"))
159+
} else {
160+
None
161+
}
162+
});
152163

153164
Fetcher::from(
154165
chan.into_receiver(),
155166
spawn_thread("blkfiles_reader", move || {
156167
for path in blk_files {
157168
trace!("reading {:?}", path);
158-
let blob = fs::read(&path)
169+
let mut blob = fs::read(&path)
159170
.unwrap_or_else(|e| panic!("failed to read {:?}: {:?}", path, e));
171+
172+
// If the xor.dat exists. Use it to decrypt the block files.
173+
if let Some(xor_key) = &xor_key {
174+
for (&key, byte) in xor_key.iter().cycle().zip(blob.iter_mut()) {
175+
*byte ^= key;
176+
}
177+
}
178+
160179
sender
161180
.send(blob)
162181
.unwrap_or_else(|_| panic!("failed to send {:?} contents", path));

0 commit comments

Comments
 (0)