From 10274b50ec4b8828c1d33aea0b3f87c86fb9f90e Mon Sep 17 00:00:00 2001 From: junderw Date: Thu, 5 Sep 2024 02:32:42 +0900 Subject: [PATCH] Add xor.dat support if the file exists --- src/new_index/fetch.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/new_index/fetch.rs b/src/new_index/fetch.rs index ec4c2cb6..3b748770 100644 --- a/src/new_index/fetch.rs +++ b/src/new_index/fetch.rs @@ -149,14 +149,33 @@ fn blkfiles_fetcher( fn blkfiles_reader(blk_files: Vec) -> Fetcher> { let chan = SyncChannel::new(1); let sender = chan.sender(); + let xor_key = blk_files.first().and_then(|p| { + let xor_file = p + .parent() + .expect("blk.dat files must exist in a directory") + .join("xor.dat"); + if xor_file.exists() { + Some(fs::read(xor_file).expect("xor.dat exists")) + } else { + None + } + }); Fetcher::from( chan.into_receiver(), spawn_thread("blkfiles_reader", move || { for path in blk_files { trace!("reading {:?}", path); - let blob = fs::read(&path) + let mut blob = fs::read(&path) .unwrap_or_else(|e| panic!("failed to read {:?}: {:?}", path, e)); + + // If the xor.dat exists. Use it to decrypt the block files. + if let Some(xor_key) = &xor_key { + for (&key, byte) in xor_key.iter().cycle().zip(blob.iter_mut()) { + *byte ^= key; + } + } + sender .send(blob) .unwrap_or_else(|_| panic!("failed to send {:?} contents", path));