From 1bca2aa694cd85984c09699ae28daec313077462 Mon Sep 17 00:00:00 2001 From: Kiminuo Date: Mon, 1 Feb 2021 13:35:28 +0100 Subject: Introduce GetUniquePath(base) helper method to replace boost::filesystem::unique_path() which is not available in std::filesystem. --- src/util/getuniquepath.cpp | 10 ++++++++++ src/util/getuniquepath.h | 19 +++++++++++++++++++ src/util/system.cpp | 3 ++- 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 src/util/getuniquepath.cpp create mode 100644 src/util/getuniquepath.h (limited to 'src/util') diff --git a/src/util/getuniquepath.cpp b/src/util/getuniquepath.cpp new file mode 100644 index 0000000000..9839d2f624 --- /dev/null +++ b/src/util/getuniquepath.cpp @@ -0,0 +1,10 @@ +#include +#include +#include + +fs::path GetUniquePath(const fs::path& base) +{ + FastRandomContext rnd; + fs::path tmpFile = base / HexStr(rnd.randbytes(8)); + return tmpFile; +} \ No newline at end of file diff --git a/src/util/getuniquepath.h b/src/util/getuniquepath.h new file mode 100644 index 0000000000..e0c6147876 --- /dev/null +++ b/src/util/getuniquepath.h @@ -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 + +/** + * 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 \ No newline at end of file diff --git a/src/util/system.cpp b/src/util/system.cpp index 9fe9d67411..9a2e719bbc 100644 --- a/src/util/system.cpp +++ b/src/util/system.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -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; -- cgit v1.2.3