Skip to content

Commit

Permalink
merge bitcoin#21052: Replace fs::unique_path with GetUniquePath(path)…
Browse files Browse the repository at this point in the history
… calls
  • Loading branch information
kwvg committed Jul 16, 2021
1 parent d905728 commit e63a2cb
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down Expand Up @@ -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 \
Expand Down
18 changes: 17 additions & 1 deletion src/test/fs_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//
#include <fs.h>
#include <test/test_dash.h>
#include <util/getuniquepath.h>

#include <boost/test/unit_test.hpp>

Expand Down Expand Up @@ -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()
3 changes: 2 additions & 1 deletion src/test/util_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <clientversion.h>
#include <primitives/transaction.h>
#include <sync.h>
#include <util/getuniquepath.h>
#include <util/strencodings.h>
#include <util/string.h>
#include <util/moneystr.h>
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 10 additions & 0 deletions src/util/getuniquepath.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <random.h>
#include <fs.h>
#include <util/strencodings.h>

fs::path GetUniquePath(const fs::path& base)
{
FastRandomContext rnd;
fs::path tmpFile = base / HexStr(rnd.randbytes(8));
return tmpFile;
}
19 changes: 19 additions & 0 deletions src/util/getuniquepath.h
Original file line number Diff line number Diff line change
@@ -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 <fs.h>

/**
* 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
3 changes: 2 additions & 1 deletion src/util/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <random.h>
#include <serialize.h>
#include <stacktraces.h>
#include <util/getuniquepath.h>
#include <util/strencodings.h>

#include <stdarg.h>
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit e63a2cb

Please sign in to comment.