Remove DirIsWritable, GetUniquePath

This commit is contained in:
MarcoFalke 2023-07-14 12:41:53 +02:00
parent fad3a9793b
commit fa3da629a1
No known key found for this signature in database
7 changed files with 4 additions and 88 deletions

View file

@ -303,7 +303,6 @@ BITCOIN_CORE_H = \
util/fees.h \
util/fs.h \
util/fs_helpers.h \
util/getuniquepath.h \
util/golombrice.h \
util/hash_type.h \
util/hasher.h \
@ -739,7 +738,6 @@ libbitcoin_util_a_SOURCES = \
util/fees.cpp \
util/fs.cpp \
util/fs_helpers.cpp \
util/getuniquepath.cpp \
util/hasher.cpp \
util/sock.cpp \
util/syserror.cpp \
@ -982,7 +980,6 @@ libbitcoinkernel_la_SOURCES = \
util/exception.cpp \
util/fs.cpp \
util/fs_helpers.cpp \
util/getuniquepath.cpp \
util/hasher.cpp \
util/moneystr.cpp \
util/rbf.cpp \

View file

@ -5,7 +5,6 @@
#include <test/util/setup_common.h>
#include <util/fs.h>
#include <util/fs_helpers.h>
#include <util/getuniquepath.h>
#include <boost/test/unit_test.hpp>
@ -101,29 +100,14 @@ BOOST_AUTO_TEST_CASE(fsbridge_fstream)
BOOST_CHECK_EQUAL(tmpfile1, fsbridge::AbsPathJoin(tmpfile1, ""));
BOOST_CHECK_EQUAL(tmpfile1, fsbridge::AbsPathJoin(tmpfile1, {}));
}
{
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_CASE(rename)
{
const fs::path tmpfolder{m_args.GetDataDirBase()};
const fs::path path1{GetUniquePath(tmpfolder)};
const fs::path path2{GetUniquePath(tmpfolder)};
const fs::path path1{tmpfolder / "a"};
const fs::path path2{tmpfolder / "b"};
const std::string path1_contents{"1111"};
const std::string path2_contents{"2222"};
@ -158,13 +142,13 @@ BOOST_AUTO_TEST_CASE(create_directories)
// Test fs::create_directories workaround.
const fs::path tmpfolder{m_args.GetDataDirBase()};
const fs::path dir{GetUniquePath(tmpfolder)};
const fs::path dir{tmpfolder / "a"};
fs::create_directory(dir);
BOOST_CHECK(fs::exists(dir));
BOOST_CHECK(fs::is_directory(dir));
BOOST_CHECK(!fs::create_directories(dir));
const fs::path symlink{GetUniquePath(tmpfolder)};
const fs::path symlink{tmpfolder / "b"};
fs::create_directory_symlink(dir, symlink);
BOOST_CHECK(fs::exists(symlink));
BOOST_CHECK(fs::is_symlink(symlink));

View file

@ -12,7 +12,6 @@
#include <util/bitdeque.h>
#include <util/fs.h>
#include <util/fs_helpers.h>
#include <util/getuniquepath.h>
#include <util/message.h> // For MessageSign(), MessageVerify(), MESSAGE_MAGIC
#include <util/moneystr.h>
#include <util/overflow.h>
@ -1245,22 +1244,6 @@ BOOST_AUTO_TEST_CASE(test_LockDirectory)
fs::remove_all(dirname);
}
BOOST_AUTO_TEST_CASE(test_DirIsWritable)
{
// Should be able to write to the data dir.
fs::path tmpdirname = m_args.GetDataDirBase();
BOOST_CHECK_EQUAL(DirIsWritable(tmpdirname), true);
// Should not be able to write to a non-existent dir.
tmpdirname = GetUniquePath(tmpdirname);
BOOST_CHECK_EQUAL(DirIsWritable(tmpdirname), false);
fs::create_directory(tmpdirname);
// Should be able to write to it now.
BOOST_CHECK_EQUAL(DirIsWritable(tmpdirname), true);
fs::remove(tmpdirname);
}
BOOST_AUTO_TEST_CASE(test_ToLower)
{
BOOST_CHECK_EQUAL(ToLower('@'), '@');

View file

@ -13,7 +13,6 @@
#include <sync.h>
#include <tinyformat.h>
#include <util/fs.h>
#include <util/getuniquepath.h>
#include <util/syserror.h>
#include <cerrno>
@ -94,19 +93,6 @@ void ReleaseDirectoryLocks()
dir_locks.clear();
}
bool DirIsWritable(const fs::path& directory)
{
fs::path tmpFile = GetUniquePath(directory);
FILE* file = fsbridge::fopen(tmpFile, "a");
if (!file) return false;
fclose(file);
remove(tmpFile);
return true;
}
bool CheckDiskSpace(const fs::path& dir, uint64_t additional_bytes)
{
constexpr uint64_t min_disk_space = 52428800; // 50 MiB

View file

@ -44,7 +44,6 @@ enum class LockResult {
[[nodiscard]] LockResult LockDirectory(const fs::path& directory, const fs::path& lockfile_name, bool probe_only = false);
} // namespace util
void UnlockDirectory(const fs::path& directory, const fs::path& lockfile_name);
bool DirIsWritable(const fs::path& directory);
bool CheckDiskSpace(const fs::path& dir, uint64_t additional_bytes = 0);
/** Get the size of a file by scanning it.

View file

@ -1,14 +0,0 @@
// Copyright (c) 2021-2022 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <random.h>
#include <util/fs.h>
#include <util/strencodings.h>
fs::path GetUniquePath(const fs::path& base)
{
FastRandomContext rnd;
fs::path tmpFile = base / fs::u8path(HexStr(rnd.randbytes(8)));
return tmpFile;
}

View file

@ -1,19 +0,0 @@
// 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 <util/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