Introduce GetUniquePath(base) helper method to replace boost::filesystem::unique_path() which is not available in std::filesystem.

This commit is contained in:
Kiminuo
2021-02-01 13:35:28 +01:00
parent ea5a50f92a
commit 1bca2aa694
6 changed files with 52 additions and 3 deletions

View 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
View 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

View File

@@ -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;