From 9657ce4219c732070987c6feb513f7d606a94c54 Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kittywhiskers@users.noreply.github.com> Date: Mon, 1 Feb 2021 13:35:28 +0100 Subject: [PATCH] merge #21052: Replace fs::unique_path with GetUniquePath(path) calls --- src/Makefile.am | 2 ++ src/test/fs_tests.cpp | 18 +++++++++++++++++- src/test/util_tests.cpp | 3 ++- src/util/getuniquepath.cpp | 10 ++++++++++ src/util/getuniquepath.h | 19 +++++++++++++++++++ src/util/system.cpp | 3 ++- 6 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 src/util/getuniquepath.cpp create mode 100644 src/util/getuniquepath.h diff --git a/src/Makefile.am b/src/Makefile.am index 065258d2e8d72e..519c3be9205152 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -269,6 +269,7 @@ BITCOIN_CORE_H = \ util/fees.h \ util/system.h \ util/asmap.h \ + util/getuniquepath.h \ util/macros.h \ util/memory.h \ util/moneystr.h \ @@ -610,6 +611,7 @@ libdash_util_a_SOURCES = \ util/error.cpp \ util/fees.cpp \ util/sock.cpp \ + util/getuniquepath.cpp \ util/system.cpp \ util/asmap.cpp \ util/moneystr.cpp \ diff --git a/src/test/fs_tests.cpp b/src/test/fs_tests.cpp index 6e9aed48d5640c..302ae4055f7f6b 100644 --- a/src/test/fs_tests.cpp +++ b/src/test/fs_tests.cpp @@ -4,6 +4,7 @@ // #include #include +#include #include @@ -51,6 +52,21 @@ BOOST_AUTO_TEST_CASE(fsbridge_fstream) file >> input_buffer; BOOST_CHECK_EQUAL(input_buffer, "bitcoin"); } + { + fs::path p1 = GetUniquePath(tmpfolder); + fs::path p2 = GetUniquePath(tmpfolder); + fs::path p3 = GetUniquePath(tmpfolder); + + // Ensure that the parent path is always the same. + BOOST_CHECK_EQUAL(tmpfolder, p1.parent_path()); + BOOST_CHECK_EQUAL(tmpfolder, p2.parent_path()); + BOOST_CHECK_EQUAL(tmpfolder, p3.parent_path()); + + // Ensure that generated paths are actually different. + BOOST_CHECK(p1 != p2); + BOOST_CHECK(p2 != p3); + BOOST_CHECK(p1 != p3); + } } -BOOST_AUTO_TEST_SUITE_END() +BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file diff --git a/src/test/util_tests.cpp b/src/test/util_tests.cpp index 3ca78f6d546327..57b5135b91b1f6 100644 --- a/src/test/util_tests.cpp +++ b/src/test/util_tests.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -1175,7 +1176,7 @@ BOOST_AUTO_TEST_CASE(test_DirIsWritable) BOOST_CHECK_EQUAL(DirIsWritable(tmpdirname), true); // Should not be able to write to a non-existent dir. - tmpdirname = tmpdirname / fs::unique_path(); + tmpdirname = GetUniquePath(tmpdirname); BOOST_CHECK_EQUAL(DirIsWritable(tmpdirname), false); fs::create_directory(tmpdirname); diff --git a/src/util/getuniquepath.cpp b/src/util/getuniquepath.cpp new file mode 100644 index 00000000000000..9839d2f6246e57 --- /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 00000000000000..e0c6147876b6d8 --- /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 46dc8c6d368298..455eb2dfe9114c 100644 --- a/src/util/system.cpp +++ b/src/util/system.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include @@ -190,7 +191,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;