From 1db331ba764d27537d82abd91dde50fc9d7e0ff4 Mon Sep 17 00:00:00 2001 From: Cory Fields Date: Thu, 16 Jan 2025 21:01:40 +0000 Subject: [PATCH] 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. --- src/node/blockstorage.cpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/node/blockstorage.cpp b/src/node/blockstorage.cpp index 07878a56029..2d25274c865 100644 --- a/src/node/blockstorage.cpp +++ b/src/node/blockstorage.cpp @@ -1143,7 +1143,19 @@ static auto InitBlocksdirXorKey(const BlockManager::Options& opts) // size of the XOR-key file. std::array 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);