mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-06-05 10:42:13 +02:00
Merge #21052: refactor: Replace fs::unique_path with GetUniquePath(path) calls
1bca2aa694Introduce GetUniquePath(base) helper method to replace boost::filesystem::unique_path() which is not available in std::filesystem. (Kiminuo) Pull request description: This PR makes it easier in #20744 to remove our dependency on the `boost::filesystem::unique_path()` function which does not have a direct equivalent in C++17. This PR attempts to re-implement `boost::filesystem::unique_path()` as `GetUniquePath(path)` but the implementations are not meant to be the same. Note: * Boost 1.75.0 implementation of `unique_path`:9cab675b71/src/unique_path.cpp (L235)* In the previous implementation, I attempted to add: ```cpp fs::path GetUniquePath(const fs::path& base) { FastRandomContext rnd; fs::path tmpFile = base / HexStr(rnd.randbytes(8)); return tmpFile; } ``` to `fs.cpp` but this leads to a circular dependency: "fs -> random -> logging -> fs". That is why the modified implementation adds a new file. ACKs for top commit: laanwj: Code review ACK1bca2aa694ryanofsky: Code review ACK1bca2aa694. It's a simple change and extra test coverage is nice Tree-SHA512: f324bdf0e254160c616b5033c3ece33d87db23eb0135acee99346ade7b5cf0d30f3ceefe359a25a8e9b53ba8e4419f459c2bdd369e10fc0152ce95031d1f221c
This commit is contained in:
10
src/util/getuniquepath.cpp
Normal file
10
src/util/getuniquepath.cpp
Normal file
@@ -0,0 +1,10 @@
|
||||
#include <random.h>
|
||||
#include <fs.h>
|
||||
#include <util/strencodings.h>
|
||||
|
||||
fs::path GetUniquePath(const fs::path& base)
|
||||
{
|
||||
FastRandomContext rnd;
|
||||
fs::path tmpFile = base / HexStr(rnd.randbytes(8));
|
||||
return tmpFile;
|
||||
}
|
||||
19
src/util/getuniquepath.h
Normal file
19
src/util/getuniquepath.h
Normal file
@@ -0,0 +1,19 @@
|
||||
// Copyright (c) 2021 The Bitcoin Core developers
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#ifndef BITCOIN_UTIL_GETUNIQUEPATH_H
|
||||
#define BITCOIN_UTIL_GETUNIQUEPATH_H
|
||||
|
||||
#include <fs.h>
|
||||
|
||||
/**
|
||||
* Helper function for getting a unique path
|
||||
*
|
||||
* @param[in] base Base path
|
||||
* @returns base joined with a random 8-character long string.
|
||||
* @post Returned path is unique with high probability.
|
||||
*/
|
||||
fs::path GetUniquePath(const fs::path& base);
|
||||
|
||||
#endif // BITCOIN_UTIL_GETUNIQUEPATH_H
|
||||
@@ -12,6 +12,7 @@
|
||||
#include <chainparamsbase.h>
|
||||
#include <sync.h>
|
||||
#include <util/check.h>
|
||||
#include <util/getuniquepath.h>
|
||||
#include <util/strencodings.h>
|
||||
#include <util/string.h>
|
||||
#include <util/translation.h>
|
||||
@@ -124,7 +125,7 @@ void ReleaseDirectoryLocks()
|
||||
|
||||
bool DirIsWritable(const fs::path& directory)
|
||||
{
|
||||
fs::path tmpFile = directory / fs::unique_path();
|
||||
fs::path tmpFile = GetUniquePath(directory);
|
||||
|
||||
FILE* file = fsbridge::fopen(tmpFile, "a");
|
||||
if (!file) return false;
|
||||
|
||||
Reference in New Issue
Block a user