0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-03-05 14:06:27 -05:00

init: allow a new xor key to be written if the blocksdir is newly created

A subsequent commit will add a .lock file to this dir at startup, meaning that
the blocksdir is never empty by the time the xor key is being read/written.

Ignore all hidden files when determining if this is the first run.
This commit is contained in:
Cory Fields 2025-01-16 21:01:40 +00:00
parent 712cab3a8f
commit 1db331ba76

View file

@ -1143,7 +1143,19 @@ static auto InitBlocksdirXorKey(const BlockManager::Options& opts)
// size of the XOR-key file.
std::array<std::byte, 8> xor_key{};
if (opts.use_xor && fs::is_empty(opts.blocks_dir)) {
// Consider this to be the first run if the blocksdir contains only hidden
// files (those which start with a .). Checking for a fully-empty dir would
// be too aggressive as a .lock file may have already been written.
bool first_run = true;
for (const auto& entry : fs::directory_iterator(opts.blocks_dir)) {
const std::string path = fs::PathToString(entry.path().filename());
if (!entry.is_regular_file() || !path.starts_with('.')) {
first_run = false;
break;
}
}
if (opts.use_xor && first_run) {
// Only use random fresh key when the boolean option is set and on the
// very first start of the program.
FastRandomContext{}.fillrand(xor_key);