Skip to content

Commit 1db331b

Browse files
committed
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.
1 parent 712cab3 commit 1db331b

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/node/blockstorage.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1143,7 +1143,19 @@ static auto InitBlocksdirXorKey(const BlockManager::Options& opts)
11431143
// size of the XOR-key file.
11441144
std::array<std::byte, 8> xor_key{};
11451145

1146-
if (opts.use_xor && fs::is_empty(opts.blocks_dir)) {
1146+
// Consider this to be the first run if the blocksdir contains only hidden
1147+
// files (those which start with a .). Checking for a fully-empty dir would
1148+
// be too aggressive as a .lock file may have already been written.
1149+
bool first_run = true;
1150+
for (const auto& entry : fs::directory_iterator(opts.blocks_dir)) {
1151+
const std::string path = fs::PathToString(entry.path().filename());
1152+
if (!entry.is_regular_file() || !path.starts_with('.')) {
1153+
first_run = false;
1154+
break;
1155+
}
1156+
}
1157+
1158+
if (opts.use_xor && first_run) {
11471159
// Only use random fresh key when the boolean option is set and on the
11481160
// very first start of the program.
11491161
FastRandomContext{}.fillrand(xor_key);

0 commit comments

Comments
 (0)