Skip to content

Commit e63a2cb

Browse files
committed
merge bitcoin#21052: Replace fs::unique_path with GetUniquePath(path) calls
1 parent d905728 commit e63a2cb

File tree

6 files changed

+52
-3
lines changed

6 files changed

+52
-3
lines changed

src/Makefile.am

+2
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ BITCOIN_CORE_H = \
269269
util/fees.h \
270270
util/system.h \
271271
util/asmap.h \
272+
util/getuniquepath.h \
272273
util/macros.h \
273274
util/memory.h \
274275
util/moneystr.h \
@@ -610,6 +611,7 @@ libdash_util_a_SOURCES = \
610611
util/error.cpp \
611612
util/fees.cpp \
612613
util/sock.cpp \
614+
util/getuniquepath.cpp \
613615
util/system.cpp \
614616
util/asmap.cpp \
615617
util/moneystr.cpp \

src/test/fs_tests.cpp

+17-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//
55
#include <fs.h>
66
#include <test/test_dash.h>
7+
#include <util/getuniquepath.h>
78

89
#include <boost/test/unit_test.hpp>
910

@@ -51,6 +52,21 @@ BOOST_AUTO_TEST_CASE(fsbridge_fstream)
5152
file >> input_buffer;
5253
BOOST_CHECK_EQUAL(input_buffer, "bitcoin");
5354
}
55+
{
56+
fs::path p1 = GetUniquePath(tmpfolder);
57+
fs::path p2 = GetUniquePath(tmpfolder);
58+
fs::path p3 = GetUniquePath(tmpfolder);
59+
60+
// Ensure that the parent path is always the same.
61+
BOOST_CHECK_EQUAL(tmpfolder, p1.parent_path());
62+
BOOST_CHECK_EQUAL(tmpfolder, p2.parent_path());
63+
BOOST_CHECK_EQUAL(tmpfolder, p3.parent_path());
64+
65+
// Ensure that generated paths are actually different.
66+
BOOST_CHECK(p1 != p2);
67+
BOOST_CHECK(p2 != p3);
68+
BOOST_CHECK(p1 != p3);
69+
}
5470
}
5571

56-
BOOST_AUTO_TEST_SUITE_END()
72+
BOOST_AUTO_TEST_SUITE_END()

src/test/util_tests.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <clientversion.h>
88
#include <primitives/transaction.h>
99
#include <sync.h>
10+
#include <util/getuniquepath.h>
1011
#include <util/strencodings.h>
1112
#include <util/string.h>
1213
#include <util/moneystr.h>
@@ -1175,7 +1176,7 @@ BOOST_AUTO_TEST_CASE(test_DirIsWritable)
11751176
BOOST_CHECK_EQUAL(DirIsWritable(tmpdirname), true);
11761177

11771178
// Should not be able to write to a non-existent dir.
1178-
tmpdirname = tmpdirname / fs::unique_path();
1179+
tmpdirname = GetUniquePath(tmpdirname);
11791180
BOOST_CHECK_EQUAL(DirIsWritable(tmpdirname), false);
11801181

11811182
fs::create_directory(tmpdirname);

src/util/getuniquepath.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <random.h>
2+
#include <fs.h>
3+
#include <util/strencodings.h>
4+
5+
fs::path GetUniquePath(const fs::path& base)
6+
{
7+
FastRandomContext rnd;
8+
fs::path tmpFile = base / HexStr(rnd.randbytes(8));
9+
return tmpFile;
10+
}

src/util/getuniquepath.h

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) 2021 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_UTIL_GETUNIQUEPATH_H
6+
#define BITCOIN_UTIL_GETUNIQUEPATH_H
7+
8+
#include <fs.h>
9+
10+
/**
11+
* Helper function for getting a unique path
12+
*
13+
* @param[in] base Base path
14+
* @returns base joined with a random 8-character long string.
15+
* @post Returned path is unique with high probability.
16+
*/
17+
fs::path GetUniquePath(const fs::path& base);
18+
19+
#endif // BITCOIN_UTIL_GETUNIQUEPATH_H

src/util/system.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <random.h>
1313
#include <serialize.h>
1414
#include <stacktraces.h>
15+
#include <util/getuniquepath.h>
1516
#include <util/strencodings.h>
1617

1718
#include <stdarg.h>
@@ -190,7 +191,7 @@ void ReleaseDirectoryLocks()
190191

191192
bool DirIsWritable(const fs::path& directory)
192193
{
193-
fs::path tmpFile = directory / fs::unique_path();
194+
fs::path tmpFile = GetUniquePath(directory);
194195

195196
FILE* file = fsbridge::fopen(tmpFile, "a");
196197
if (!file) return false;

0 commit comments

Comments
 (0)