File tree Expand file tree Collapse file tree 6 files changed +52
-3
lines changed Expand file tree Collapse file tree 6 files changed +52
-3
lines changed Original file line number Diff line number Diff line change @@ -233,6 +233,7 @@ BITCOIN_CORE_H = \
233
233
util/check.h \
234
234
util/error.h \
235
235
util/fees.h \
236
+ util/getuniquepath.h \
236
237
util/golombrice.h \
237
238
util/hasher.h \
238
239
util/macros.h \
@@ -556,6 +557,7 @@ libbitcoin_util_a_SOURCES = \
556
557
util/bytevectorhash.cpp \
557
558
util/error.cpp \
558
559
util/fees.cpp \
560
+ util/getuniquepath.cpp \
559
561
util/hasher.cpp \
560
562
util/system.cpp \
561
563
util/message.cpp \
Original file line number Diff line number Diff line change 5
5
#include < fs.h>
6
6
#include < test/util/setup_common.h>
7
7
#include < util/system.h>
8
+ #include < util/getuniquepath.h>
8
9
9
10
#include < boost/test/unit_test.hpp>
10
11
@@ -69,6 +70,21 @@ BOOST_AUTO_TEST_CASE(fsbridge_fstream)
69
70
BOOST_CHECK_EQUAL (tmpfile1, fsbridge::AbsPathJoin (tmpfile1, " " ));
70
71
BOOST_CHECK_EQUAL (tmpfile1, fsbridge::AbsPathJoin (tmpfile1, {}));
71
72
}
73
+ {
74
+ fs::path p1 = GetUniquePath (tmpfolder);
75
+ fs::path p2 = GetUniquePath (tmpfolder);
76
+ fs::path p3 = GetUniquePath (tmpfolder);
77
+
78
+ // Ensure that the parent path is always the same.
79
+ BOOST_CHECK_EQUAL (tmpfolder, p1.parent_path ());
80
+ BOOST_CHECK_EQUAL (tmpfolder, p2.parent_path ());
81
+ BOOST_CHECK_EQUAL (tmpfolder, p3.parent_path ());
82
+
83
+ // Ensure that generated paths are actually different.
84
+ BOOST_CHECK (p1 != p2);
85
+ BOOST_CHECK (p2 != p3);
86
+ BOOST_CHECK (p1 != p3);
87
+ }
72
88
}
73
89
74
- BOOST_AUTO_TEST_SUITE_END ()
90
+ BOOST_AUTO_TEST_SUITE_END ()
Original file line number Diff line number Diff line change 13
13
#include < test/util/setup_common.h>
14
14
#include < test/util/str.h>
15
15
#include < uint256.h>
16
+ #include < util/getuniquepath.h>
16
17
#include < util/message.h> // For MessageSign(), MessageVerify(), MESSAGE_MAGIC
17
18
#include < util/moneystr.h>
18
19
#include < util/spanparsing.h>
@@ -1816,7 +1817,7 @@ BOOST_AUTO_TEST_CASE(test_DirIsWritable)
1816
1817
BOOST_CHECK_EQUAL (DirIsWritable (tmpdirname), true );
1817
1818
1818
1819
// Should not be able to write to a non-existent dir.
1819
- tmpdirname = tmpdirname / fs::unique_path ( );
1820
+ tmpdirname = GetUniquePath (tmpdirname );
1820
1821
BOOST_CHECK_EQUAL (DirIsWritable (tmpdirname), false );
1821
1822
1822
1823
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 < chainparamsbase.h>
13
13
#include < sync.h>
14
14
#include < util/check.h>
15
+ #include < util/getuniquepath.h>
15
16
#include < util/strencodings.h>
16
17
#include < util/string.h>
17
18
#include < util/translation.h>
@@ -124,7 +125,7 @@ void ReleaseDirectoryLocks()
124
125
125
126
bool DirIsWritable (const fs::path& directory)
126
127
{
127
- fs::path tmpFile = directory / fs::unique_path ( );
128
+ fs::path tmpFile = GetUniquePath (directory );
128
129
129
130
FILE* file = fsbridge::fopen (tmpFile, " a" );
130
131
if (!file) return false ;
You can’t perform that action at this time.
0 commit comments