Repository: bitcoin/bitcoin
File: src/node/blockstorage.cpp (C++)

Code before:
        return FlatFilePos();
    }
    if (!WriteBlockToDisk(block, blockPos)) {
        m_opts.notifications.fatalError(_("Failed to write block."));
        return FlatFilePos();
    }
    return blockPos;
}

static auto InitBlocksdirXorKey(const BlockManager::Options& opts)
{
    // Bytes are serialized without length indicator, so this is also the exact
    // size of the XOR-key file.
    std::array<std::byte, 8> xor_key{};

    if (opts.use_xor && fs::is_empty(opts.blocks_dir)) {
        // Only use random fresh key when the boolean option is set and on the
        // very first start of the program.
        FastRandomContext{}.fillrand(xor_key);
    }

    const fs::path xor_key_path{opts.blocks_dir / "xor.dat"};
    if (fs::exists(xor_key_path)) {
        // A pre-existing xor key file has priority.
        AutoFile xor_key_file{fsbridge::fopen(xor_key_path, "rb")};
        xor_key_file >> xor_key;
    } else {
        // Create initial or missing xor key file
        AutoFile xor_key_file{fsbridge::fopen(xor_key_path,
#ifdef __MINGW64__
            "wb" // Temporary workaround for https://github.com/bitcoin/bitcoin/issues/30210
#else
            "wbx"
#endif
        )};
        xor_key_file << xor_key;
    }
    // If the user disabled the key, it must be zero.
    if (!opts.use_xor && xor_key != decltype(xor_key){}) {
        throw std::runtime_error{
            strprintf("The blocksdir XOR-key can not be disabled when a random key was already stored! "
                      "Stored key: '%s', stored path: '%s'.",
                      HexStr(xor_key), fs::PathToString(xor_key_path)),
        };
    }
    LogInfo("Using obfuscation key for blocksdir *.dat files (%s): '%s'\n", fs::PathToString(opts.blocks_dir), HexStr(xor_key));
    return std::vector<std::byte>{xor_key.begin(), xor_key.end()};
}

BlockManager::BlockManager(const util::SignalInterrupt& interrupt, Options opts)
    : m_prune_mode{opts.prune_target > 0},

Review comment (suggestion):
style nit: Could use ranges, if you feel fancy :sweat_smile: 

```suggestion
bool first_run = std::ranges::all_of(
    fs::directory_iterator(opts.blocks_dir),
    [](const auto& entry) {
        const std::string path = fs::PathToString(entry.path().filename());
        return entry.is_regular_file() && path.starts_with('.');
    }
);
```

Code after:
        return FlatFilePos();
    }
    if (!WriteBlockToDisk(block, blockPos)) {
        m_opts.notifications.fatalError(_("Failed to write block."));
        return FlatFilePos();
    }
    return blockPos;
}

static auto InitBlocksdirXorKey(const BlockManager::Options& opts)
{
    // Bytes are serialized without length indicator, so this is also the exact
    // size of the XOR-key file.
    std::array<std::byte, 8> xor_key{};

    // 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);
    }

    const fs::path xor_key_path{opts.blocks_dir / "xor.dat"};
    if (fs::exists(xor_key_path)) {
        // A pre-existing xor key file has priority.
        AutoFile xor_key_file{fsbridge::fopen(xor_key_path, "rb")};
        xor_key_file >> xor_key;
    } else {
        // Create initial or missing xor key file
        AutoFile xor_key_file{fsbridge::fopen(xor_key_path,
#ifdef __MINGW64__
            "wb" // Temporary workaround for https://github.com/bitcoin/bitcoin/issues/30210
#else
            "wbx"
#endif
        )};
        xor_key_file << xor_key;
    }
    // If the user disabled the key, it must be zero.
    if (!opts.use_xor && xor_key != decltype(xor_key){}) {