File tree 6 files changed +52
-3
lines changed
6 files changed +52
-3
lines changed Original file line number Diff line number Diff line change @@ -269,6 +269,7 @@ BITCOIN_CORE_H = \
269
269
util/fees.h \
270
270
util/system.h \
271
271
util/asmap.h \
272
+ util/getuniquepath.h \
272
273
util/macros.h \
273
274
util/memory.h \
274
275
util/moneystr.h \
@@ -610,6 +611,7 @@ libdash_util_a_SOURCES = \
610
611
util/error.cpp \
611
612
util/fees.cpp \
612
613
util/sock.cpp \
614
+ util/getuniquepath.cpp \
613
615
util/system.cpp \
614
616
util/asmap.cpp \
615
617
util/moneystr.cpp \
Original file line number Diff line number Diff line change 4
4
//
5
5
#include < fs.h>
6
6
#include < test/test_dash.h>
7
+ #include < util/getuniquepath.h>
7
8
8
9
#include < boost/test/unit_test.hpp>
9
10
@@ -51,6 +52,21 @@ BOOST_AUTO_TEST_CASE(fsbridge_fstream)
51
52
file >> input_buffer;
52
53
BOOST_CHECK_EQUAL (input_buffer, " bitcoin" );
53
54
}
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
+ }
54
70
}
55
71
56
- BOOST_AUTO_TEST_SUITE_END ()
72
+ BOOST_AUTO_TEST_SUITE_END ()
Original file line number Diff line number Diff line change 7
7
#include < clientversion.h>
8
8
#include < primitives/transaction.h>
9
9
#include < sync.h>
10
+ #include < util/getuniquepath.h>
10
11
#include < util/strencodings.h>
11
12
#include < util/string.h>
12
13
#include < util/moneystr.h>
@@ -1175,7 +1176,7 @@ BOOST_AUTO_TEST_CASE(test_DirIsWritable)
1175
1176
BOOST_CHECK_EQUAL (DirIsWritable (tmpdirname), true );
1176
1177
1177
1178
// Should not be able to write to a non-existent dir.
1178
- tmpdirname = tmpdirname / fs::unique_path ( );
1179
+ tmpdirname = GetUniquePath (tmpdirname );
1179
1180
BOOST_CHECK_EQUAL (DirIsWritable (tmpdirname), false );
1180
1181
1181
1182
fs::create_directory (tmpdirname);
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change 12
12
#include < random.h>
13
13
#include < serialize.h>
14
14
#include < stacktraces.h>
15
+ #include < util/getuniquepath.h>
15
16
#include < util/strencodings.h>
16
17
17
18
#include < stdarg.h>
@@ -190,7 +191,7 @@ void ReleaseDirectoryLocks()
190
191
191
192
bool DirIsWritable (const fs::path& directory)
192
193
{
193
- fs::path tmpFile = directory / fs::unique_path ( );
194
+ fs::path tmpFile = GetUniquePath (directory );
194
195
195
196
FILE* file = fsbridge::fopen (tmpFile, " a" );
196
197
if (!file) return false ;
You can’t perform that action at this time.
0 commit comments