diff --git a/src/Makefile.am b/src/Makefile.am index c2b660874..417fac8e0 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -154,6 +154,7 @@ BITCOIN_CORE_H = \ core_memusage.h \ deprecation.h \ experimental_features.h \ + fs.h \ hash.h \ httprpc.h \ httpserver.h \ @@ -414,6 +415,7 @@ libbitcoin_util_a_SOURCES = \ compat/glibc_sanity.cpp \ compat/glibcxx_sanity.cpp \ compat/strnlen.cpp \ + fs.cpp \ logging.cpp \ random.cpp \ rpc/protocol.cpp \ diff --git a/src/bench/bench_bitcoin.cpp b/src/bench/bench_bitcoin.cpp index 719c72746..a20a67f9e 100644 --- a/src/bench/bench_bitcoin.cpp +++ b/src/bench/bench_bitcoin.cpp @@ -4,12 +4,11 @@ #include "bench.h" +#include "fs.h" #include "key.h" #include "main.h" #include "util.h" -#include - #include "librustzcash.h" int @@ -20,12 +19,12 @@ main(int argc, char** argv) SetupEnvironment(); fPrintToDebugLog = false; // don't want to write to debug.log file - boost::filesystem::path sapling_spend = ZC_GetParamsDir() / "sapling-spend.params"; - boost::filesystem::path sapling_output = ZC_GetParamsDir() / "sapling-output.params"; - boost::filesystem::path sprout_groth16 = ZC_GetParamsDir() / "sprout-groth16.params"; + fs::path sapling_spend = ZC_GetParamsDir() / "sapling-spend.params"; + fs::path sapling_output = ZC_GetParamsDir() / "sapling-output.params"; + fs::path sprout_groth16 = ZC_GetParamsDir() / "sprout-groth16.params"; static_assert( - sizeof(boost::filesystem::path::value_type) == sizeof(codeunit), + sizeof(fs::path::value_type) == sizeof(codeunit), "librustzcash not configured correctly"); auto sapling_spend_str = sapling_spend.native(); auto sapling_output_str = sapling_output.native(); diff --git a/src/bitcoin-cli.cpp b/src/bitcoin-cli.cpp index b0278864f..0b695303b 100644 --- a/src/bitcoin-cli.cpp +++ b/src/bitcoin-cli.cpp @@ -5,12 +5,12 @@ #include "chainparamsbase.h" #include "clientversion.h" +#include "fs.h" #include "rpc/client.h" #include "rpc/protocol.h" #include "util.h" #include "utilstrencodings.h" -#include #include #include @@ -95,7 +95,7 @@ static int AppInitRPC(int argc, char* argv[]) } return EXIT_SUCCESS; } - if (!boost::filesystem::is_directory(GetDataDir(false))) { + if (!fs::is_directory(GetDataDir(false))) { fprintf(stderr, "Error: Specified data directory \"%s\" does not exist.\n", mapArgs["-datadir"].c_str()); return EXIT_FAILURE; } diff --git a/src/bitcoind.cpp b/src/bitcoind.cpp index 54b6359ed..7cc186387 100644 --- a/src/bitcoind.cpp +++ b/src/bitcoind.cpp @@ -4,6 +4,7 @@ // file COPYING or https://www.opensource.org/licenses/mit-license.php . #include "clientversion.h" +#include "fs.h" #include "rpc/server.h" #include "init.h" #include "main.h" @@ -14,7 +15,6 @@ #include "httprpc.h" #include -#include #include #include @@ -92,7 +92,7 @@ bool AppInit(int argc, char* argv[]) try { - if (!boost::filesystem::is_directory(GetDataDir(false))) + if (!fs::is_directory(GetDataDir(false))) { fprintf(stderr, "Error: Specified data directory \"%s\" does not exist.\n", mapArgs["-datadir"].c_str()); return false; diff --git a/src/dbwrapper.cpp b/src/dbwrapper.cpp index 494ab98ce..aeddcead8 100644 --- a/src/dbwrapper.cpp +++ b/src/dbwrapper.cpp @@ -4,10 +4,9 @@ #include "dbwrapper.h" +#include "fs.h" #include "util.h" -#include - #include #include #include @@ -30,7 +29,7 @@ static leveldb::Options GetOptions(size_t nCacheSize) return options; } -CDBWrapper::CDBWrapper(const boost::filesystem::path& path, size_t nCacheSize, bool fMemory, bool fWipe) +CDBWrapper::CDBWrapper(const fs::path& path, size_t nCacheSize, bool fMemory, bool fWipe) { penv = NULL; readoptions.verify_checksums = true; diff --git a/src/dbwrapper.h b/src/dbwrapper.h index 0996e9cda..e80baccab 100644 --- a/src/dbwrapper.h +++ b/src/dbwrapper.h @@ -6,13 +6,12 @@ #define BITCOIN_DBWRAPPER_H #include "clientversion.h" +#include "fs.h" #include "serialize.h" #include "streams.h" #include "util.h" #include "version.h" -#include - #include #include @@ -173,7 +172,7 @@ public: * @param[in] fMemory If true, use leveldb's memory environment. * @param[in] fWipe If true, remove all existing data. */ - CDBWrapper(const boost::filesystem::path& path, size_t nCacheSize, bool fMemory = false, bool fWipe = false); + CDBWrapper(const fs::path& path, size_t nCacheSize, bool fMemory = false, bool fWipe = false); ~CDBWrapper(); template diff --git a/src/fs.cpp b/src/fs.cpp new file mode 100644 index 000000000..a5e12f1cf --- /dev/null +++ b/src/fs.cpp @@ -0,0 +1,15 @@ +#include "fs.h" + +namespace fsbridge { + +FILE *fopen(const fs::path& p, const char *mode) +{ + return ::fopen(p.string().c_str(), mode); +} + +FILE *freopen(const fs::path& p, const char *mode, FILE *stream) +{ + return ::freopen(p.string().c_str(), mode, stream); +} + +} // fsbridge diff --git a/src/fs.h b/src/fs.h new file mode 100644 index 000000000..abb4be254 --- /dev/null +++ b/src/fs.h @@ -0,0 +1,24 @@ +// Copyright (c) 2017 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_FS_H +#define BITCOIN_FS_H + +#include +#include + +#include +#include +#include + +/** Filesystem operations and types */ +namespace fs = boost::filesystem; + +/** Bridge operations to C stdio */ +namespace fsbridge { + FILE *fopen(const fs::path& p, const char *mode); + FILE *freopen(const fs::path& p, const char *mode, FILE *stream); +}; + +#endif // BITCOIN_FS_H diff --git a/src/gtest/main.cpp b/src/gtest/main.cpp index 51da9123c..ea33fe46a 100644 --- a/src/gtest/main.cpp +++ b/src/gtest/main.cpp @@ -17,12 +17,12 @@ int main(int argc, char **argv) { assert(init_and_check_sodium() != -1); ECC_Start(); - boost::filesystem::path sapling_spend = ZC_GetParamsDir() / "sapling-spend.params"; - boost::filesystem::path sapling_output = ZC_GetParamsDir() / "sapling-output.params"; - boost::filesystem::path sprout_groth16 = ZC_GetParamsDir() / "sprout-groth16.params"; + fs::path sapling_spend = ZC_GetParamsDir() / "sapling-spend.params"; + fs::path sapling_output = ZC_GetParamsDir() / "sapling-output.params"; + fs::path sprout_groth16 = ZC_GetParamsDir() / "sprout-groth16.params"; static_assert( - sizeof(boost::filesystem::path::value_type) == sizeof(codeunit), + sizeof(fs::path::value_type) == sizeof(codeunit), "librustzcash not configured correctly"); auto sapling_spend_str = sapling_spend.native(); auto sapling_output_str = sapling_output.native(); diff --git a/src/gtest/test_deprecation.cpp b/src/gtest/test_deprecation.cpp index badd29956..27048b6a4 100644 --- a/src/gtest/test_deprecation.cpp +++ b/src/gtest/test_deprecation.cpp @@ -4,12 +4,12 @@ #include "chainparams.h" #include "clientversion.h" #include "deprecation.h" +#include "fs.h" #include "init.h" #include "ui_interface.h" #include "util.h" #include "utilstrencodings.h" -#include #include using namespace boost::placeholders; @@ -49,7 +49,7 @@ protected: StrictMock mock_; - static std::vector read_lines(boost::filesystem::path filepath) { + static std::vector read_lines(fs::path filepath) { std::vector result; std::ifstream f(filepath.string().c_str()); @@ -123,8 +123,8 @@ TEST_F(DeprecationTest, DeprecatedNodeIgnoredOnTestnet) { } TEST_F(DeprecationTest, AlertNotify) { - boost::filesystem::path temp = GetTempPath() / - boost::filesystem::unique_path("alertnotify-%%%%.txt"); + fs::path temp = fs::temp_directory_path() / + fs::unique_path("alertnotify-%%%%.txt"); mapArgs["-alertnotify"] = std::string("echo %s >> ") + temp.string(); @@ -146,5 +146,5 @@ TEST_F(DeprecationTest, AlertNotify) { #else EXPECT_EQ(r[0], strprintf("'%s' ", expectedMsg)); #endif - boost::filesystem::remove(temp); + fs::remove(temp); } diff --git a/src/gtest/test_foundersreward.cpp b/src/gtest/test_foundersreward.cpp index 538f42598..2f526d79c 100644 --- a/src/gtest/test_foundersreward.cpp +++ b/src/gtest/test_foundersreward.cpp @@ -4,6 +4,7 @@ #include "utilmoneystr.h" #include "chainparams.h" #include "consensus/funding.h" +#include "fs.h" #include "key_io.h" #include "utilstrencodings.h" #include "zcash/Address.hpp" @@ -13,7 +14,6 @@ #include #include #include -#include #include "util.h" #include "utiltest.h" @@ -28,8 +28,8 @@ #if 0 TEST(FoundersRewardTest, create_testnet_2of3multisig) { SelectParams(CBaseChainParams::TESTNET); - boost::filesystem::path pathTemp = boost::filesystem::temp_directory_path() / boost::filesystem::unique_path(); - boost::filesystem::create_directories(pathTemp); + fs::path pathTemp = fs::temp_directory_path() / fs::unique_path(); + fs::create_directories(pathTemp); mapArgs["-datadir"] = pathTemp.string(); bool fFirstRun; auto pWallet = std::make_shared("wallet.dat"); diff --git a/src/init.cpp b/src/init.cpp index 38c71fcd5..7a16b422d 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -16,6 +16,7 @@ #include "consensus/upgrades.h" #include "consensus/validation.h" #include "experimental_features.h" +#include "fs.h" #include "httpserver.h" #include "httprpc.h" #include "key.h" @@ -209,8 +210,8 @@ void Shutdown() if (fFeeEstimatesInitialized) { - boost::filesystem::path est_path = GetDataDir() / FEE_ESTIMATES_FILENAME; - CAutoFile est_fileout(fopen(est_path.string().c_str(), "wb"), SER_DISK, CLIENT_VERSION); + fs::path est_path = GetDataDir() / FEE_ESTIMATES_FILENAME; + CAutoFile est_fileout(fsbridge::fopen(est_path, "wb"), SER_DISK, CLIENT_VERSION); if (!est_fileout.IsNull()) mempool.WriteFeeEstimates(est_fileout); else @@ -247,8 +248,8 @@ void Shutdown() #ifndef WIN32 try { - boost::filesystem::remove(GetPidFile()); - } catch (const boost::filesystem::filesystem_error& e) { + fs::remove(GetPidFile()); + } catch (const fs::filesystem_error& e) { LogPrintf("%s: Unable to remove pidfile: %s\n", __func__, e.what()); } #endif @@ -549,7 +550,7 @@ struct CImportingNow // works correctly. void CleanupBlockRevFiles() { - using namespace boost::filesystem; + using namespace fs; map mapBlockFiles; // Glob all blk?????.dat and rev?????.dat files from the blocks directory. @@ -583,7 +584,7 @@ void CleanupBlockRevFiles() } } -void ThreadImport(std::vector vImportFiles) +void ThreadImport(std::vector vImportFiles) { const CChainParams& chainparams = Params(); RenameThread("zcash-loadblk"); @@ -596,17 +597,17 @@ void ThreadImport(std::vector vImportFiles) size_t fullSize = 0; while (true) { CDiskBlockPos pos(nFile, 0); - boost::filesystem::path blkFile = GetBlockPosFilename(pos, "blk"); - if (!boost::filesystem::exists(blkFile)) + fs::path blkFile = GetBlockPosFilename(pos, "blk"); + if (!fs::exists(blkFile)) break; // No block files left to reindex nFile++; - fullSize += boost::filesystem::file_size(blkFile); + fullSize += fs::file_size(blkFile); } nFullSizeToReindex = std::max(1, fullSize); nFile = 0; while (true) { CDiskBlockPos pos(nFile, 0); - if (!boost::filesystem::exists(GetBlockPosFilename(pos, "blk"))) + if (!fs::exists(GetBlockPosFilename(pos, "blk"))) break; // No block files left to reindex FILE *file = OpenBlockFile(pos, true); if (!file) @@ -625,12 +626,12 @@ void ThreadImport(std::vector vImportFiles) } // hardcoded $DATADIR/bootstrap.dat - boost::filesystem::path pathBootstrap = GetDataDir() / "bootstrap.dat"; - if (boost::filesystem::exists(pathBootstrap)) { - FILE *file = fopen(pathBootstrap.string().c_str(), "rb"); + fs::path pathBootstrap = GetDataDir() / "bootstrap.dat"; + if (fs::exists(pathBootstrap)) { + FILE *file = fsbridge::fopen(pathBootstrap, "rb"); if (file) { CImportingNow imp; - boost::filesystem::path pathBootstrapOld = GetDataDir() / "bootstrap.dat.old"; + fs::path pathBootstrapOld = GetDataDir() / "bootstrap.dat.old"; LogPrintf("Importing bootstrap.dat...\n"); LoadExternalBlockFile(chainparams, file); RenameOver(pathBootstrap, pathBootstrapOld); @@ -640,8 +641,8 @@ void ThreadImport(std::vector vImportFiles) } // -loadblock= - BOOST_FOREACH(const boost::filesystem::path& path, vImportFiles) { - FILE *file = fopen(path.string().c_str(), "rb"); + BOOST_FOREACH(const fs::path& path, vImportFiles) { + FILE *file = fsbridge::fopen(path, "rb"); if (file) { CImportingNow imp; LogPrintf("Importing blocks file %s...\n", path.string()); @@ -681,14 +682,14 @@ static void ZC_LoadParams( struct timeval tv_start, tv_end; float elapsed; - boost::filesystem::path sapling_spend = ZC_GetParamsDir() / "sapling-spend.params"; - boost::filesystem::path sapling_output = ZC_GetParamsDir() / "sapling-output.params"; - boost::filesystem::path sprout_groth16 = ZC_GetParamsDir() / "sprout-groth16.params"; + fs::path sapling_spend = ZC_GetParamsDir() / "sapling-spend.params"; + fs::path sapling_output = ZC_GetParamsDir() / "sapling-output.params"; + fs::path sprout_groth16 = ZC_GetParamsDir() / "sprout-groth16.params"; if (!( - boost::filesystem::exists(sapling_spend) && - boost::filesystem::exists(sapling_output) && - boost::filesystem::exists(sprout_groth16) + fs::exists(sapling_spend) && + fs::exists(sapling_output) && + fs::exists(sprout_groth16) )) { uiInterface.ThreadSafeMessageBox(strprintf( _("Cannot find the Zcash network parameters in the following directory:\n" @@ -701,7 +702,7 @@ static void ZC_LoadParams( } static_assert( - sizeof(boost::filesystem::path::value_type) == sizeof(codeunit), + sizeof(fs::path::value_type) == sizeof(codeunit), "librustzcash not configured correctly"); auto sapling_spend_str = sapling_spend.native(); auto sapling_output_str = sapling_output.native(); @@ -809,9 +810,9 @@ void InitLogging() // Set up the initial filtering directive from the -debug flags. std::string initialFilter = LogConfigFilter(); - boost::filesystem::path pathDebug = GetDebugLogPath(); - const boost::filesystem::path::string_type& pathDebugStr = pathDebug.native(); - static_assert(sizeof(boost::filesystem::path::value_type) == sizeof(codeunit), + fs::path pathDebug = GetDebugLogPath(); + const fs::path::string_type& pathDebugStr = pathDebug.native(); + static_assert(sizeof(fs::path::value_type) == sizeof(codeunit), "native path has unexpected code unit size"); const codeunit* pathDebugCStr = nullptr; size_t pathDebugLen = 0; @@ -1160,8 +1161,8 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler) std::string strDataDir = GetDataDir().string(); // Make sure only a single Bitcoin process is using the data directory. - boost::filesystem::path pathLockFile = GetDataDir() / ".lock"; - FILE* file = fopen(pathLockFile.string().c_str(), "a"); // empty lock file; created if it doesn't exist. + fs::path pathLockFile = GetDataDir() / ".lock"; + FILE* file = fsbridge::fopen(pathLockFile, "a"); // empty lock file; created if it doesn't exist. if (file) fclose(file); try { @@ -1366,7 +1367,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler) fReindex = GetBoolArg("-reindex", false); - boost::filesystem::create_directories(GetDataDir() / "blocks"); + fs::create_directories(GetDataDir() / "blocks"); // cache size calculations int64_t nTotalCache = (GetArg("-dbcache", nDefaultDbCache) << 20); @@ -1537,8 +1538,8 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler) } LogPrintf(" block index %15dms\n", GetTimeMillis() - nStart); - boost::filesystem::path est_path = GetDataDir() / FEE_ESTIMATES_FILENAME; - CAutoFile est_filein(fopen(est_path.string().c_str(), "rb"), SER_DISK, CLIENT_VERSION); + fs::path est_path = GetDataDir() / FEE_ESTIMATES_FILENAME; + CAutoFile est_filein(fsbridge::fopen(est_path, "rb"), SER_DISK, CLIENT_VERSION); // Allowed to fail as this file IS missing on first startup. if (!est_filein.IsNull()) mempool.ReadFeeEstimates(est_filein); @@ -1652,7 +1653,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler) if (!ActivateBestChain(state, chainparams)) strErrors << "Failed to connect best block"; - std::vector vImportFiles; + std::vector vImportFiles; if (mapArgs.count("-loadblock")) { BOOST_FOREACH(const std::string& strFile, mapMultiArgs["-loadblock"]) diff --git a/src/logging.cpp b/src/logging.cpp index 2b75d642c..54cb8bdec 100644 --- a/src/logging.cpp +++ b/src/logging.cpp @@ -6,12 +6,12 @@ #include "logging.h" +#include "fs.h" #include "serialize.h" #include "util.h" #include -#include #include #include #include @@ -80,9 +80,9 @@ static void DebugPrintInit() vMsgsBeforeOpenLog = new list; } -boost::filesystem::path GetDebugLogPath() +fs::path GetDebugLogPath() { - boost::filesystem::path logfile(GetArg("-debuglogfile", DEFAULT_DEBUGLOGFILE)); + fs::path logfile(GetArg("-debuglogfile", DEFAULT_DEBUGLOGFILE)); if (logfile.is_absolute()) { return logfile; } else { @@ -141,9 +141,9 @@ bool LogAcceptCategory(const char* category) void ShrinkDebugFile() { // Scroll debug.log if it's getting too big - boost::filesystem::path pathLog = GetDebugLogPath(); - FILE* file = fopen(pathLog.string().c_str(), "r"); - if (file && boost::filesystem::file_size(pathLog) > 10 * 1000000) + fs::path pathLog = GetDebugLogPath(); + FILE* file = fsbridge::fopen(pathLog, "r"); + if (file && fs::file_size(pathLog) > 10 * 1000000) { // Restart the file with some of the end std::vector vch(200000,0); @@ -151,7 +151,7 @@ void ShrinkDebugFile() int nBytes = fread(begin_ptr(vch), 1, vch.size(), file); fclose(file); - file = fopen(pathLog.string().c_str(), "w"); + file = fsbridge::fopen(pathLog, "w"); if (file) { fwrite(begin_ptr(vch), 1, nBytes, file); diff --git a/src/logging.h b/src/logging.h index bd40efcff..edee5506a 100644 --- a/src/logging.h +++ b/src/logging.h @@ -7,13 +7,12 @@ #ifndef ZCASH_LOGGING_H #define ZCASH_LOGGING_H +#include "fs.h" #include "tinyformat.h" #include #include -#include - #include static const bool DEFAULT_LOGTIMEMICROS = false; @@ -54,7 +53,7 @@ bool LogAcceptCategory(const char* category); return false; \ }()) -boost::filesystem::path GetDebugLogPath(); +fs::path GetDebugLogPath(); void ShrinkDebugFile(); #endif // ZCASH_LOGGING_H diff --git a/src/main.cpp b/src/main.cpp index 2d43a1263..63316eaf3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -42,8 +42,6 @@ #include #include -#include -#include #include #include #include @@ -4483,8 +4481,8 @@ void UnlinkPrunedFiles(std::set& setFilesToPrune) { for (set::iterator it = setFilesToPrune.begin(); it != setFilesToPrune.end(); ++it) { CDiskBlockPos pos(*it, 0); - boost::filesystem::remove(GetBlockPosFilename(pos, "blk")); - boost::filesystem::remove(GetBlockPosFilename(pos, "rev")); + fs::remove(GetBlockPosFilename(pos, "blk")); + fs::remove(GetBlockPosFilename(pos, "rev")); LogPrintf("Prune: %s deleted blk/rev (%05u)\n", __func__, *it); } } @@ -4539,7 +4537,7 @@ void FindFilesToPrune(std::set& setFilesToPrune, uint64_t nPruneAfterHeight bool CheckDiskSpace(uint64_t nAdditionalBytes) { - uint64_t nFreeBytesAvailable = boost::filesystem::space(GetDataDir()).available; + uint64_t nFreeBytesAvailable = fs::space(GetDataDir()).available; // Check for nMinDiskSpace bytes (currently 50MB) if (nFreeBytesAvailable < nMinDiskSpace + nAdditionalBytes) @@ -4552,11 +4550,11 @@ FILE* OpenDiskFile(const CDiskBlockPos &pos, const char *prefix, bool fReadOnly) { if (pos.IsNull()) return NULL; - boost::filesystem::path path = GetBlockPosFilename(pos, prefix); - boost::filesystem::create_directories(path.parent_path()); - FILE* file = fopen(path.string().c_str(), "rb+"); + fs::path path = GetBlockPosFilename(pos, prefix); + fs::create_directories(path.parent_path()); + FILE* file = fsbridge::fopen(path, "rb+"); if (!file && !fReadOnly) - file = fopen(path.string().c_str(), "wb+"); + file = fsbridge::fopen(path, "wb+"); if (!file) { LogPrintf("Unable to open file %s\n", path.string()); return NULL; @@ -4579,7 +4577,7 @@ FILE* OpenUndoFile(const CDiskBlockPos &pos, bool fReadOnly) { return OpenDiskFile(pos, "rev", fReadOnly); } -boost::filesystem::path GetBlockPosFilename(const CDiskBlockPos &pos, const char *prefix) +fs::path GetBlockPosFilename(const CDiskBlockPos &pos, const char *prefix) { return GetDataDir() / "blocks" / strprintf("%s%05u.dat", prefix, pos.nFile); } diff --git a/src/main.h b/src/main.h index d3b9afea1..830deeeb1 100644 --- a/src/main.h +++ b/src/main.h @@ -15,6 +15,7 @@ #include "chainparams.h" #include "coins.h" #include "consensus/upgrades.h" +#include "fs.h" #include "net.h" #include "primitives/block.h" #include "primitives/transaction.h" @@ -232,7 +233,7 @@ FILE* OpenBlockFile(const CDiskBlockPos &pos, bool fReadOnly = false); /** Open an undo file (rev?????.dat) */ FILE* OpenUndoFile(const CDiskBlockPos &pos, bool fReadOnly = false); /** Translation to a filesystem path */ -boost::filesystem::path GetBlockPosFilename(const CDiskBlockPos &pos, const char *prefix); +fs::path GetBlockPosFilename(const CDiskBlockPos &pos, const char *prefix); /** Import blocks from an external file */ bool LoadExternalBlockFile(const CChainParams& chainparams, FILE* fileIn, CDiskBlockPos *dbp = NULL); /** Initialize a new block tree database + block data on disk */ diff --git a/src/net.cpp b/src/net.cpp index 40b5a6e46..81164f8a4 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -7,6 +7,7 @@ #include "config/bitcoin-config.h" #endif +#include "fs.h" #include "main.h" #include "net.h" @@ -24,7 +25,6 @@ #include #endif -#include #include // Dump addresses to peers.dat every 15 minutes (900s) @@ -1989,8 +1989,8 @@ bool CAddrDB::Write(const CAddrMan& addr) ssPeers << hash; // open temp output file, and associate with CAutoFile - boost::filesystem::path pathTmp = GetDataDir() / tmpfn; - FILE *file = fopen(pathTmp.string().c_str(), "wb"); + fs::path pathTmp = GetDataDir() / tmpfn; + FILE *file = fsbridge::fopen(pathTmp, "wb"); CAutoFile fileout(file, SER_DISK, CLIENT_VERSION); if (fileout.IsNull()) return error("%s: Failed to open file %s", __func__, pathTmp.string()); @@ -2015,13 +2015,13 @@ bool CAddrDB::Write(const CAddrMan& addr) bool CAddrDB::Read(CAddrMan& addr) { // open input file, and associate with CAutoFile - FILE *file = fopen(pathAddr.string().c_str(), "rb"); + FILE *file = fsbridge::fopen(pathAddr, "rb"); CAutoFile filein(file, SER_DISK, CLIENT_VERSION); if (filein.IsNull()) return error("%s: Failed to open file %s", __func__, pathAddr.string()); // use file size to size memory buffer - int fileSize = boost::filesystem::file_size(pathAddr); + int fileSize = fs::file_size(pathAddr); int dataSize = fileSize - sizeof(uint256); // Don't try to resize to a negative number if file is small if (dataSize < 0) diff --git a/src/net.h b/src/net.h index b8392e083..4c3388bf7 100644 --- a/src/net.h +++ b/src/net.h @@ -8,6 +8,7 @@ #include "bloom.h" #include "compat.h" +#include "fs.h" #include "hash.h" #include "limitedmap.h" #include "mruset.h" @@ -26,7 +27,6 @@ #include #endif -#include #include #include @@ -660,7 +660,7 @@ void RelayTransaction(const CTransaction& tx, const CDataStream& ss); class CAddrDB { private: - boost::filesystem::path pathAddr; + fs::path pathAddr; public: CAddrDB(); bool Write(const CAddrMan& addr); diff --git a/src/rpc/protocol.cpp b/src/rpc/protocol.cpp index 4125757e1..cd92119e4 100644 --- a/src/rpc/protocol.cpp +++ b/src/rpc/protocol.cpp @@ -68,9 +68,9 @@ static const std::string COOKIEAUTH_USER = "__cookie__"; /** Default name for auth cookie file */ static const std::string COOKIEAUTH_FILE = ".cookie"; -boost::filesystem::path GetAuthCookieFile() +fs::path GetAuthCookieFile() { - boost::filesystem::path path(GetArg("-rpccookiefile", COOKIEAUTH_FILE)); + fs::path path(GetArg("-rpccookiefile", COOKIEAUTH_FILE)); if (!path.is_complete()) path = GetDataDir() / path; return path; } @@ -85,7 +85,7 @@ bool GenerateAuthCookie(std::string *cookie_out) * these are set to 077 in init.cpp unless overridden with -sysperms. */ std::ofstream file; - boost::filesystem::path filepath = GetAuthCookieFile(); + fs::path filepath = GetAuthCookieFile(); file.open(filepath.string().c_str()); if (!file.is_open()) { LogPrintf("Unable to open cookie authentication file %s for writing\n", filepath.string()); @@ -104,7 +104,7 @@ bool GetAuthCookie(std::string *cookie_out) { std::ifstream file; std::string cookie; - boost::filesystem::path filepath = GetAuthCookieFile(); + fs::path filepath = GetAuthCookieFile(); file.open(filepath.string().c_str()); if (!file.is_open()) return false; @@ -119,8 +119,8 @@ bool GetAuthCookie(std::string *cookie_out) void DeleteAuthCookie() { try { - boost::filesystem::remove(GetAuthCookieFile()); - } catch (const boost::filesystem::filesystem_error& e) { + fs::remove(GetAuthCookieFile()); + } catch (const fs::filesystem_error& e) { LogPrintf("%s: Unable to remove random auth cookie file: %s\n", __func__, e.what()); } } diff --git a/src/rpc/protocol.h b/src/rpc/protocol.h index 5e12d0a4f..e79e54888 100644 --- a/src/rpc/protocol.h +++ b/src/rpc/protocol.h @@ -6,11 +6,12 @@ #ifndef BITCOIN_RPCPROTOCOL_H #define BITCOIN_RPCPROTOCOL_H +#include "fs.h" + #include #include #include #include -#include #include @@ -82,7 +83,7 @@ std::string JSONRPCReply(const UniValue& result, const UniValue& error, const Un UniValue JSONRPCError(int code, const std::string& message); /** Get name of RPC authentication cookie file */ -boost::filesystem::path GetAuthCookieFile(); +fs::path GetAuthCookieFile(); /** Generate a new RPC authentication cookie and write it to disk */ bool GenerateAuthCookie(std::string *cookie_out); /** Read the RPC authentication cookie from disk */ diff --git a/src/rpc/server.cpp b/src/rpc/server.cpp index fc9c6da17..2c39da620 100644 --- a/src/rpc/server.cpp +++ b/src/rpc/server.cpp @@ -5,6 +5,7 @@ #include "rpc/server.h" +#include "fs.h" #include "init.h" #include "key_io.h" #include "random.h" @@ -19,7 +20,6 @@ #include #include -#include #include #include #include diff --git a/src/test/alert_tests.cpp b/src/test/alert_tests.cpp index f215eccd0..629c29f21 100644 --- a/src/test/alert_tests.cpp +++ b/src/test/alert_tests.cpp @@ -10,6 +10,7 @@ #include "chain.h" #include "chainparams.h" #include "clientversion.h" +#include "fs.h" #include "test/data/alertTests.raw.h" #include "main.h" @@ -17,7 +18,6 @@ #include "rpc/server.h" #include "serialize.h" #include "streams.h" -#include "util.h" #include "utilstrencodings.h" #include "utiltest.h" #include "warnings.h" @@ -26,7 +26,6 @@ #include -#include #include #include @@ -272,7 +271,7 @@ struct ReadAlerts : public TestingSetup } ~ReadAlerts() { } - static std::vector read_lines(boost::filesystem::path filepath) + static std::vector read_lines(fs::path filepath) { std::vector result; @@ -354,8 +353,8 @@ BOOST_AUTO_TEST_CASE(AlertNotify) SetMockTime(11); const std::vector& alertKey = Params(CBaseChainParams::MAIN).AlertKey(); - boost::filesystem::path temp = GetTempPath() / - boost::filesystem::unique_path("alertnotify-%%%%.txt"); + fs::path temp = fs::temp_directory_path() / + fs::unique_path("alertnotify-%%%%.txt"); mapArgs["-alertnotify"] = std::string("echo %s >> ") + temp.string(); @@ -383,7 +382,7 @@ BOOST_AUTO_TEST_CASE(AlertNotify) BOOST_CHECK_EQUAL(r[4], "'Alert 4, reenables RPC' "); // dashes should be removed BOOST_CHECK_EQUAL(r[5], "'Evil Alert; /bin/ls; echo ' "); #endif - boost::filesystem::remove(temp); + fs::remove(temp); SetMockTime(0); mapAlerts.clear(); diff --git a/src/test/checkblock_tests.cpp b/src/test/checkblock_tests.cpp index 494ce4255..ae4c53ce2 100644 --- a/src/test/checkblock_tests.cpp +++ b/src/test/checkblock_tests.cpp @@ -4,6 +4,7 @@ #include "clientversion.h" #include "consensus/validation.h" +#include "fs.h" #include "main.h" #include "proof_verifier.h" #include "test/test_bitcoin.h" @@ -12,8 +13,6 @@ #include -#include -#include #include @@ -21,7 +20,6 @@ BOOST_FIXTURE_TEST_SUITE(CheckBlock_tests, BasicTestingSetup) bool read_block(const std::string& filename, CBlock& block) { - namespace fs = boost::filesystem; fs::path testFile = fs::current_path() / "data" / filename; #ifdef TEST_DATA_DIR if (!fs::exists(testFile)) @@ -29,7 +27,7 @@ bool read_block(const std::string& filename, CBlock& block) testFile = fs::path(BOOST_PP_STRINGIZE(TEST_DATA_DIR)) / filename; } #endif - FILE* fp = fopen(testFile.string().c_str(), "rb"); + FILE* fp = fsbridge::fopen(testFile, "rb"); if (!fp) return false; fseek(fp, 8, SEEK_SET); // skip msgheader/size diff --git a/src/test/dbwrapper_tests.cpp b/src/test/dbwrapper_tests.cpp index 1853cbf1d..611311379 100644 --- a/src/test/dbwrapper_tests.cpp +++ b/src/test/dbwrapper_tests.cpp @@ -14,7 +14,7 @@ using namespace std; using namespace boost::assign; // bring 'operator+=()' into scope -using namespace boost::filesystem; +using namespace fs; // Test if a string consists entirely of null characters bool is_null_key(const vector& key) { diff --git a/src/test/streams_tests.cpp b/src/test/streams_tests.cpp index 1d9dcf550..99afd1fd0 100644 --- a/src/test/streams_tests.cpp +++ b/src/test/streams_tests.cpp @@ -2,12 +2,12 @@ // Distributed under the MIT software license, see the accompanying // file COPYING or https://www.opensource.org/licenses/mit-license.php. +#include "fs.h" #include "main.h" #include "test/test_bitcoin.h" #include "test/test_random.h" #include -#include BOOST_FIXTURE_TEST_SUITE(streams_tests, TestingSetup) @@ -141,7 +141,7 @@ BOOST_AUTO_TEST_CASE(streams_buffered_file) // We can explicitly close the file, or the destructor will do it. bf.fclose(); - boost::filesystem::remove("streams_test_tmp"); + fs::remove("streams_test_tmp"); } BOOST_AUTO_TEST_CASE(streams_buffered_file_rand) @@ -251,7 +251,7 @@ BOOST_AUTO_TEST_CASE(streams_buffered_file_rand) maxPos = currentPos; } } - boost::filesystem::remove("streams_test_tmp"); + fs::remove("streams_test_tmp"); } BOOST_AUTO_TEST_SUITE_END() diff --git a/src/test/test_bitcoin.cpp b/src/test/test_bitcoin.cpp index 362b6e035..6289b1edb 100644 --- a/src/test/test_bitcoin.cpp +++ b/src/test/test_bitcoin.cpp @@ -14,6 +14,7 @@ #ifdef ENABLE_MINING #include "crypto/equihash.h" #endif +#include "fs.h" #include "key.h" #include "main.h" #include "miner.h" @@ -25,7 +26,6 @@ #include "rpc/server.h" #include "rpc/register.h" -#include #include #include @@ -41,12 +41,12 @@ extern void noui_connect(); JoinSplitTestingSetup::JoinSplitTestingSetup(const std::string& chainName) : BasicTestingSetup(chainName) { - boost::filesystem::path sapling_spend = ZC_GetParamsDir() / "sapling-spend.params"; - boost::filesystem::path sapling_output = ZC_GetParamsDir() / "sapling-output.params"; - boost::filesystem::path sprout_groth16 = ZC_GetParamsDir() / "sprout-groth16.params"; + fs::path sapling_spend = ZC_GetParamsDir() / "sapling-spend.params"; + fs::path sapling_output = ZC_GetParamsDir() / "sapling-output.params"; + fs::path sprout_groth16 = ZC_GetParamsDir() / "sprout-groth16.params"; static_assert( - sizeof(boost::filesystem::path::value_type) == sizeof(codeunit), + sizeof(fs::path::value_type) == sizeof(codeunit), "librustzcash not configured correctly"); auto sapling_spend_str = sapling_spend.native(); auto sapling_output_str = sapling_output.native(); @@ -91,11 +91,11 @@ TestingSetup::TestingSetup(const std::string& chainName) : JoinSplitTestingSetup RegisterAllCoreRPCCommands(tableRPC); // Save current path, in case a test changes it - orig_current_path = boost::filesystem::current_path(); + orig_current_path = fs::current_path(); ClearDatadirCache(); - pathTemp = GetTempPath() / strprintf("test_bitcoin_%lu_%i", (unsigned long)GetTime(), (int)(GetRand(100000))); - boost::filesystem::create_directories(pathTemp); + pathTemp = fs::temp_directory_path() / strprintf("test_bitcoin_%lu_%i", (unsigned long)GetTime(), (int)(GetRand(100000))); + fs::create_directories(pathTemp); mapArgs["-datadir"] = pathTemp.string(); pblocktree = new CBlockTreeDB(1 << 20, true); pcoinsdbview = new CCoinsViewDB(1 << 23, true); @@ -118,9 +118,9 @@ TestingSetup::~TestingSetup() delete pblocktree; // Restore the previous current path so temporary directory can be deleted - boost::filesystem::current_path(orig_current_path); + fs::current_path(orig_current_path); - boost::filesystem::remove_all(pathTemp); + fs::remove_all(pathTemp); } #ifdef ENABLE_MINING diff --git a/src/test/test_bitcoin.h b/src/test/test_bitcoin.h index d61f85622..8b236a84a 100644 --- a/src/test/test_bitcoin.h +++ b/src/test/test_bitcoin.h @@ -3,11 +3,11 @@ #include "chainparamsbase.h" #include "consensus/upgrades.h" +#include "fs.h" #include "key.h" #include "pubkey.h" #include "txdb.h" -#include #include /** Basic testing setup. @@ -31,8 +31,8 @@ struct JoinSplitTestingSetup: public BasicTestingSetup { */ struct TestingSetup: public JoinSplitTestingSetup { CCoinsViewDB *pcoinsdbview; - boost::filesystem::path orig_current_path; - boost::filesystem::path pathTemp; + fs::path orig_current_path; + fs::path pathTemp; boost::thread_group threadGroup; TestingSetup(const std::string& chainName = CBaseChainParams::MAIN); diff --git a/src/torcontrol.cpp b/src/torcontrol.cpp index 4ac7dd58d..ddbe3f48d 100644 --- a/src/torcontrol.cpp +++ b/src/torcontrol.cpp @@ -366,9 +366,9 @@ static std::map ParseTorReplyMapping(const std::string * @param maxsize Puts a maximum size limit on the file that is read. If the file is larger than this, truncated data * (with len > maxsize) will be returned. */ -static std::pair ReadBinaryFile(const std::string &filename, size_t maxsize=std::numeric_limits::max()) +static std::pair ReadBinaryFile(const fs::path &filename, size_t maxsize=std::numeric_limits::max()) { - FILE *f = fopen(filename.c_str(), "rb"); + FILE *f = fsbridge::fopen(filename, "rb"); if (f == NULL) return std::make_pair(false,""); std::string retval; @@ -392,9 +392,9 @@ static std::pair ReadBinaryFile(const std::string &filename, s /** Write contents of std::string to a file. * @return true on success. */ -static bool WriteBinaryFile(const std::string &filename, const std::string &data) +static bool WriteBinaryFile(const fs::path &filename, const std::string &data) { - FILE *f = fopen(filename.c_str(), "wb"); + FILE *f = fsbridge::fopen(filename, "wb"); if (f == NULL) return false; if (fwrite(data.data(), 1, data.size(), f) != data.size()) { @@ -417,7 +417,7 @@ public: ~TorController(); /** Get name for file to store private key in */ - std::string GetPrivateKeyFile(); + fs::path GetPrivateKeyFile(); /** Reconnect, after getting disconnected */ void Reconnect(); @@ -469,7 +469,7 @@ TorController::TorController(struct event_base* baseIn, const std::string& targe // Read service private key if cached std::pair pkf = ReadBinaryFile(GetPrivateKeyFile()); if (pkf.first) { - LogPrint("tor", "tor: Reading cached private key from %s\n", GetPrivateKeyFile()); + LogPrint("tor", "tor: Reading cached private key from %s\n", GetPrivateKeyFile().string()); private_key = pkf.second; } } @@ -508,9 +508,9 @@ void TorController::add_onion_cb(TorControlConnection& conn, const TorControlRep service = CService(service_id+".onion", GetListenPort(), false); LogPrintf("tor: Got service ID %s, advertizing service %s\n", service_id, service.ToString()); if (WriteBinaryFile(GetPrivateKeyFile(), private_key)) { - LogPrint("tor", "tor: Cached service private key to %s\n", GetPrivateKeyFile()); + LogPrint("tor", "tor: Cached service private key to %s\n", GetPrivateKeyFile().string()); } else { - LogPrintf("tor: Error writing service private key to %s\n", GetPrivateKeyFile()); + LogPrintf("tor: Error writing service private key to %s\n", GetPrivateKeyFile().string()); } AddLocal(service, LOCAL_MANUAL); // ... onion requested - keep connection open @@ -720,9 +720,9 @@ void TorController::Reconnect() } } -std::string TorController::GetPrivateKeyFile() +fs::path TorController::GetPrivateKeyFile() { - return (GetDataDir() / "onion_private_key").string(); + return GetDataDir() / "onion_private_key"; } void TorController::reconnect_cb(evutil_socket_t fd, short what, void *arg) diff --git a/src/util.cpp b/src/util.cpp index 8c6f54aae..bfaed333d 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -11,6 +11,7 @@ #include "util.h" #include "chainparamsbase.h" +#include "fs.h" #include "random.h" #include "serialize.h" #include "sync.h" @@ -77,8 +78,6 @@ #include // for to_lower() #include #include // for startswith() and endswith() -#include -#include #include #include #include @@ -237,9 +236,8 @@ void PrintExceptionContinue(const std::exception* pex, const char* pszThread) fprintf(stderr, "\n\n************************\n%s\n", message.c_str()); } -boost::filesystem::path GetDefaultDataDir() +fs::path GetDefaultDataDir() { - namespace fs = boost::filesystem; // Windows < Vista: C:\Documents and Settings\Username\Application Data\Zcash // Windows >= Vista: C:\Users\Username\AppData\Roaming\Zcash // Mac: ~/Library/Application Support/Zcash @@ -266,16 +264,15 @@ boost::filesystem::path GetDefaultDataDir() #endif } -static boost::filesystem::path pathCached; -static boost::filesystem::path pathCachedNetSpecific; -static boost::filesystem::path zc_paramsPathCached; +static fs::path pathCached; +static fs::path pathCachedNetSpecific; +static fs::path zc_paramsPathCached; static CCriticalSection csPathCached; -static boost::filesystem::path ZC_GetDefaultBaseParamsDir() +static fs::path ZC_GetDefaultBaseParamsDir() { // Copied from GetDefaultDataDir and adapter for zcash params. - namespace fs = boost::filesystem; // Windows < Vista: C:\Documents and Settings\Username\Application Data\ZcashParams // Windows >= Vista: C:\Users\Username\AppData\Roaming\ZcashParams // Mac: ~/Library/Application Support/ZcashParams @@ -302,10 +299,8 @@ static boost::filesystem::path ZC_GetDefaultBaseParamsDir() #endif } -const boost::filesystem::path &ZC_GetParamsDir() +const fs::path &ZC_GetParamsDir() { - namespace fs = boost::filesystem; - LOCK(csPathCached); // Reuse the same lock as upstream. fs::path &path = zc_paramsPathCached; @@ -330,9 +325,8 @@ const boost::filesystem::path &ZC_GetParamsDir() // Return the user specified export directory. Create directory if it doesn't exist. // If user did not set option, return an empty path. // If there is a filesystem problem, throw an exception. -const boost::filesystem::path GetExportDir() +const fs::path GetExportDir() { - namespace fs = boost::filesystem; fs::path path; if (mapArgs.count("-exportdir")) { path = fs::system_complete(mapArgs["-exportdir"]); @@ -347,9 +341,8 @@ const boost::filesystem::path GetExportDir() } -const boost::filesystem::path &GetDataDir(bool fNetSpecific) +const fs::path &GetDataDir(bool fNetSpecific) { - namespace fs = boost::filesystem; LOCK(csPathCached); @@ -379,13 +372,13 @@ const boost::filesystem::path &GetDataDir(bool fNetSpecific) void ClearDatadirCache() { - pathCached = boost::filesystem::path(); - pathCachedNetSpecific = boost::filesystem::path(); + pathCached = fs::path(); + pathCachedNetSpecific = fs::path(); } -boost::filesystem::path GetConfigFile(const std::string& confPath) +fs::path GetConfigFile(const std::string& confPath) { - boost::filesystem::path pathConfigFile(confPath); + fs::path pathConfigFile(confPath); if (!pathConfigFile.is_complete()) pathConfigFile = GetDataDir(false) / pathConfigFile; @@ -396,7 +389,7 @@ void ReadConfigFile(const std::string& confPath, map& mapSettingsRet, map >& mapMultiSettingsRet) { - boost::filesystem::ifstream streamConfig(GetConfigFile(confPath)); + fs::ifstream streamConfig(GetConfigFile(confPath)); if (!streamConfig.good()) throw missing_zcash_conf(); @@ -446,16 +439,16 @@ void ReadConfigFile(const std::string& confPath, } #ifndef WIN32 -boost::filesystem::path GetPidFile() +fs::path GetPidFile() { - boost::filesystem::path pathPidFile(GetArg("-pid", BITCOIN_PID_FILENAME)); + fs::path pathPidFile(GetArg("-pid", BITCOIN_PID_FILENAME)); if (!pathPidFile.is_complete()) pathPidFile = GetDataDir() / pathPidFile; return pathPidFile; } -void CreatePidFile(const boost::filesystem::path &path, pid_t pid) +void CreatePidFile(const fs::path &path, pid_t pid) { - FILE* file = fopen(path.string().c_str(), "w"); + FILE* file = fsbridge::fopen(path, "w"); if (file) { fprintf(file, "%d\n", pid); @@ -464,7 +457,7 @@ void CreatePidFile(const boost::filesystem::path &path, pid_t pid) } #endif -bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest) +bool RenameOver(fs::path src, fs::path dest) { #ifdef WIN32 return MoveFileExA(src.string().c_str(), dest.string().c_str(), @@ -480,13 +473,13 @@ bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest) * Specifically handles case where path p exists, but it wasn't possible for the user to * write to the parent directory. */ -bool TryCreateDirectory(const boost::filesystem::path& p) +bool TryCreateDirectory(const fs::path& p) { try { - return boost::filesystem::create_directory(p); - } catch (const boost::filesystem::filesystem_error&) { - if (!boost::filesystem::exists(p) || !boost::filesystem::is_directory(p)) + return fs::create_directory(p); + } catch (const fs::filesystem_error&) { + if (!fs::exists(p) || !fs::is_directory(p)) throw; } @@ -589,10 +582,8 @@ void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length) { } #ifdef WIN32 -boost::filesystem::path GetSpecialFolderPath(int nFolder, bool fCreate) +fs::path GetSpecialFolderPath(int nFolder, bool fCreate) { - namespace fs = boost::filesystem; - char pszPath[MAX_PATH] = ""; if(SHGetSpecialFolderPathA(NULL, pszPath, nFolder, fCreate)) @@ -605,28 +596,6 @@ boost::filesystem::path GetSpecialFolderPath(int nFolder, bool fCreate) } #endif -boost::filesystem::path GetTempPath() { -#if BOOST_FILESYSTEM_VERSION == 3 - return boost::filesystem::temp_directory_path(); -#else - // TODO: remove when we don't support filesystem v2 anymore - boost::filesystem::path path; -#ifdef WIN32 - char pszPath[MAX_PATH] = ""; - - if (GetTempPathA(MAX_PATH, pszPath)) - path = boost::filesystem::path(pszPath); -#else - path = boost::filesystem::path("/tmp"); -#endif - if (path.empty() || !boost::filesystem::is_directory(path)) { - LogPrintf("GetTempPath(): failed to find temp path\n"); - return boost::filesystem::path(""); - } - return path; -#endif -} - void runCommand(const std::string& strCommand) { int nErr = ::system(strCommand.c_str()); @@ -664,9 +633,9 @@ void SetupEnvironment() // The path locale is lazy initialized and to avoid deinitialization errors // in multithreading environments, it is set explicitly by the main thread. // A dummy locale is used to extract the internal default locale, used by - // boost::filesystem::path, which is then used to explicitly imbue the path. - std::locale loc = boost::filesystem::path::imbue(std::locale::classic()); - boost::filesystem::path::imbue(loc); + // fs::path, which is then used to explicitly imbue the path. + std::locale loc = fs::path::imbue(std::locale::classic()); + fs::path::imbue(loc); } bool SetupNetworking() diff --git a/src/util.h b/src/util.h index 77b9a5142..c9304a788 100644 --- a/src/util.h +++ b/src/util.h @@ -15,6 +15,7 @@ #endif #include "compat.h" +#include "fs.h" #include "logging.h" #include "tinyformat.h" #include "utiltime.h" @@ -26,7 +27,6 @@ #include #include -#include #include #include @@ -69,7 +69,7 @@ static inline bool error(const char* format, const Args&... args) return LogError("main", format, args...); } -const boost::filesystem::path &ZC_GetParamsDir(); +const fs::path &ZC_GetParamsDir(); void PrintExceptionContinue(const std::exception *pex, const char* pszThread); void ParseParameters(int argc, const char*const argv[]); @@ -77,15 +77,15 @@ void FileCommit(FILE *fileout); bool TruncateFile(FILE *file, unsigned int length); int RaiseFileDescriptorLimit(int nMinFD); void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length); -bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest); -bool TryCreateDirectory(const boost::filesystem::path& p); -boost::filesystem::path GetDefaultDataDir(); -const boost::filesystem::path &GetDataDir(bool fNetSpecific = true); +bool RenameOver(fs::path src, fs::path dest); +bool TryCreateDirectory(const fs::path& p); +fs::path GetDefaultDataDir(); +const fs::path &GetDataDir(bool fNetSpecific = true); void ClearDatadirCache(); -boost::filesystem::path GetConfigFile(const std::string& confPath); +fs::path GetConfigFile(const std::string& confPath); #ifndef WIN32 -boost::filesystem::path GetPidFile(); -void CreatePidFile(const boost::filesystem::path &path, pid_t pid); +fs::path GetPidFile(); +void CreatePidFile(const fs::path &path, pid_t pid); #endif class missing_zcash_conf : public std::runtime_error { public: @@ -93,11 +93,10 @@ public: }; void ReadConfigFile(const std::string& confPath, std::map& mapSettingsRet, std::map >& mapMultiSettingsRet); #ifdef WIN32 -boost::filesystem::path GetSpecialFolderPath(int nFolder, bool fCreate = true); +fs::path GetSpecialFolderPath(int nFolder, bool fCreate = true); #endif -boost::filesystem::path GetTempPath(); void runCommand(const std::string& strCommand); -const boost::filesystem::path GetExportDir(); +const fs::path GetExportDir(); /** Returns privacy notice (for -version, -help and metrics screen) */ std::string PrivacyInfo(); diff --git a/src/wallet/db.cpp b/src/wallet/db.cpp index ab80929c5..d45f3662f 100644 --- a/src/wallet/db.cpp +++ b/src/wallet/db.cpp @@ -6,6 +6,7 @@ #include "db.h" #include "addrman.h" +#include "fs.h" #include "hash.h" #include "protocol.h" #include "util.h" @@ -17,7 +18,6 @@ #include #endif -#include #include #include @@ -71,7 +71,7 @@ void CDBEnv::Close() EnvShutdown(); } -bool CDBEnv::Open(const boost::filesystem::path& pathIn) +bool CDBEnv::Open(const fs::path& pathIn) { if (fDbEnvInit) return true; @@ -79,9 +79,9 @@ bool CDBEnv::Open(const boost::filesystem::path& pathIn) boost::this_thread::interruption_point(); strPath = pathIn.string(); - boost::filesystem::path pathLogDir = pathIn / "database"; + fs::path pathLogDir = pathIn / "database"; TryCreateDirectory(pathLogDir); - boost::filesystem::path pathErrorFile = pathIn / "db.log"; + fs::path pathErrorFile = pathIn / "db.log"; LogPrintf("CDBEnv::Open: LogDir=%s ErrorFile=%s\n", pathLogDir.string(), pathErrorFile.string()); unsigned int nEnvFlags = 0; @@ -94,7 +94,7 @@ bool CDBEnv::Open(const boost::filesystem::path& pathIn) dbenv->set_lg_max(1048576); dbenv->set_lk_max_locks(40000); dbenv->set_lk_max_objects(40000); - dbenv->set_errfile(fopen(pathErrorFile.string().c_str(), "a")); /// debug + dbenv->set_errfile(fsbridge::fopen(pathErrorFile, "a")); /// debug dbenv->set_flags(DB_AUTO_COMMIT, 1); dbenv->set_flags(DB_TXN_WRITE_NOSYNC, 1); dbenv->log_set_config(DB_LOG_AUTO_REMOVE, 1); @@ -455,7 +455,7 @@ void CDBEnv::Flush(bool fShutdown) dbenv->log_archive(&listp, DB_ARCH_REMOVE); Close(); if (!fMockDb) - boost::filesystem::remove_all(boost::filesystem::path(strPath) / "database"); + fs::remove_all(fs::path(strPath) / "database"); } } } diff --git a/src/wallet/db.h b/src/wallet/db.h index 5fd512024..1b0da7882 100644 --- a/src/wallet/db.h +++ b/src/wallet/db.h @@ -7,6 +7,7 @@ #define BITCOIN_WALLET_DB_H #include "clientversion.h" +#include "fs.h" #include "serialize.h" #include "streams.h" #include "sync.h" @@ -16,7 +17,6 @@ #include #include -#include #include #include @@ -31,7 +31,7 @@ class CDBEnv private: bool fDbEnvInit; bool fMockDb; - // Don't change into boost::filesystem::path, as that can result in + // Don't change into fs::path, as that can result in // shutdown problems/crashes caused by a static initialized internal pointer. std::string strPath; @@ -70,7 +70,7 @@ public: typedef std::pair, std::vector > KeyValPair; bool Salvage(const std::string& strFile, bool fAggressive, std::vector& vResult); - bool Open(const boost::filesystem::path& path); + bool Open(const fs::path& path); void Close(); void Flush(bool fShutdown); void CheckpointLSN(const std::string& strFile); diff --git a/src/wallet/gtest/test_paymentdisclosure.cpp b/src/wallet/gtest/test_paymentdisclosure.cpp index 8dfb48bd2..c60546a97 100644 --- a/src/wallet/gtest/test_paymentdisclosure.cpp +++ b/src/wallet/gtest/test_paymentdisclosure.cpp @@ -1,5 +1,6 @@ #include +#include "fs.h" #include "main.h" #include "random.h" #include "utilmoneystr.h" @@ -14,7 +15,6 @@ #include #include #include -#include #include #include "util.h" @@ -47,7 +47,7 @@ static boost::uuids::random_generator uuidgen; // Subclass of PaymentDisclosureDB to add debugging methods class PaymentDisclosureDBTest : public PaymentDisclosureDB { public: - PaymentDisclosureDBTest(const boost::filesystem::path& dbPath) : PaymentDisclosureDB(dbPath) {} + PaymentDisclosureDBTest(const fs::path& dbPath) : PaymentDisclosureDB(dbPath) {} void DebugDumpAllStdout() { ASSERT_NE(db, nullptr); @@ -89,8 +89,8 @@ public: TEST(paymentdisclosure, mainnet) { SelectParams(CBaseChainParams::MAIN); - boost::filesystem::path pathTemp = boost::filesystem::temp_directory_path() / boost::filesystem::unique_path(); - boost::filesystem::create_directories(pathTemp); + fs::path pathTemp = fs::temp_directory_path() / fs::unique_path(); + fs::create_directories(pathTemp); mapArgs["-datadir"] = pathTemp.string(); PaymentDisclosureDBTest mydb(pathTemp); diff --git a/src/wallet/gtest/test_wallet.cpp b/src/wallet/gtest/test_wallet.cpp index 328369fea..f451e8275 100644 --- a/src/wallet/gtest/test_wallet.cpp +++ b/src/wallet/gtest/test_wallet.cpp @@ -3,6 +3,7 @@ #include "base58.h" #include "chainparams.h" +#include "fs.h" #include "key_io.h" #include "main.h" #include "primitives/block.h" @@ -13,8 +14,6 @@ #include "zcash/Note.hpp" #include "zcash/NoteEncryption.hpp" -#include - using ::testing::Return; ACTION(ThrowLogicError) { @@ -117,8 +116,8 @@ std::pair GetWitnessesAndAnchors(TestWallet& wallet, TEST(WalletTests, SetupDatadirLocationRunAsFirstTest) { // Get temporary and unique path for file. - boost::filesystem::path pathTemp = boost::filesystem::temp_directory_path() / boost::filesystem::unique_path(); - boost::filesystem::create_directories(pathTemp); + fs::path pathTemp = fs::temp_directory_path() / fs::unique_path(); + fs::create_directories(pathTemp); mapArgs["-datadir"] = pathTemp.string(); } diff --git a/src/wallet/gtest/test_wallet_zkeys.cpp b/src/wallet/gtest/test_wallet_zkeys.cpp index 619a61343..1cc5752b0 100644 --- a/src/wallet/gtest/test_wallet_zkeys.cpp +++ b/src/wallet/gtest/test_wallet_zkeys.cpp @@ -1,12 +1,11 @@ #include +#include "fs.h" #include "zcash/Address.hpp" #include "wallet/wallet.h" #include "wallet/walletdb.h" #include "util.h" -#include - /** * This test covers Sapling methods on CWallet * GenerateNewSaplingZKey() @@ -222,8 +221,8 @@ TEST(WalletZkeysTest, WriteZkeyDirectToDb) { // Get temporary and unique path for file. // Note: / operator to append paths - boost::filesystem::path pathTemp = boost::filesystem::temp_directory_path() / boost::filesystem::unique_path(); - boost::filesystem::create_directories(pathTemp); + fs::path pathTemp = fs::temp_directory_path() / fs::unique_path(); + fs::create_directories(pathTemp); mapArgs["-datadir"] = pathTemp.string(); bool fFirstRun; @@ -295,8 +294,8 @@ TEST(WalletZkeysTest, WriteViewingKeyDirectToDB) { // Get temporary and unique path for file. // Note: / operator to append paths - boost::filesystem::path pathTemp = boost::filesystem::temp_directory_path() / boost::filesystem::unique_path(); - boost::filesystem::create_directories(pathTemp); + fs::path pathTemp = fs::temp_directory_path() / fs::unique_path(); + fs::create_directories(pathTemp); mapArgs["-datadir"] = pathTemp.string(); bool fFirstRun; @@ -342,8 +341,8 @@ TEST(WalletZkeysTest, WriteCryptedzkeyDirectToDb) { // Get temporary and unique path for file. // Note: / operator to append paths - boost::filesystem::path pathTemp = boost::filesystem::temp_directory_path() / boost::filesystem::unique_path(); - boost::filesystem::create_directories(pathTemp); + fs::path pathTemp = fs::temp_directory_path() / fs::unique_path(); + fs::create_directories(pathTemp); mapArgs["-datadir"] = pathTemp.string(); bool fFirstRun; @@ -417,8 +416,8 @@ TEST(wallet_zkeys_tests, WriteCryptedSaplingZkeyDirectToDb) { // Get temporary and unique path for file. // Note: / operator to append paths - boost::filesystem::path pathTemp = boost::filesystem::temp_directory_path() / boost::filesystem::unique_path(); - boost::filesystem::create_directories(pathTemp); + fs::path pathTemp = fs::temp_directory_path() / fs::unique_path(); + fs::create_directories(pathTemp); mapArgs["-datadir"] = pathTemp.string(); bool fFirstRun; diff --git a/src/wallet/paymentdisclosuredb.cpp b/src/wallet/paymentdisclosuredb.cpp index 21b489e3c..b6b12c43f 100644 --- a/src/wallet/paymentdisclosuredb.cpp +++ b/src/wallet/paymentdisclosuredb.cpp @@ -4,14 +4,13 @@ #include "wallet/paymentdisclosuredb.h" +#include "fs.h" #include "util.h" #include "dbwrapper.h" -#include - using namespace std; -static boost::filesystem::path emptyPath; +static fs::path emptyPath; /** * Static method to return the shared/default payment disclosure database. @@ -26,8 +25,8 @@ shared_ptr PaymentDisclosureDB::sharedInstance() { PaymentDisclosureDB::PaymentDisclosureDB() : PaymentDisclosureDB(emptyPath) { } -PaymentDisclosureDB::PaymentDisclosureDB(const boost::filesystem::path& dbPath) { - boost::filesystem::path path(dbPath); +PaymentDisclosureDB::PaymentDisclosureDB(const fs::path& dbPath) { + fs::path path(dbPath); if (path.empty()) { path = GetDataDir() / "paymentdisclosure"; LogPrintf("PaymentDisclosure: using default path for database: %s\n", path.string()); diff --git a/src/wallet/paymentdisclosuredb.h b/src/wallet/paymentdisclosuredb.h index e4a797e45..293d00576 100644 --- a/src/wallet/paymentdisclosuredb.h +++ b/src/wallet/paymentdisclosuredb.h @@ -31,7 +31,7 @@ public: static std::shared_ptr sharedInstance(); PaymentDisclosureDB(); - PaymentDisclosureDB(const boost::filesystem::path& dbPath); + PaymentDisclosureDB(const fs::path& dbPath); ~PaymentDisclosureDB(); bool Put(const PaymentDisclosureKey& key, const PaymentDisclosureInfo& info); diff --git a/src/wallet/rpcdump.cpp b/src/wallet/rpcdump.cpp index 77d614926..2927ab5c1 100644 --- a/src/wallet/rpcdump.cpp +++ b/src/wallet/rpcdump.cpp @@ -562,7 +562,7 @@ UniValue dumpwallet_impl(const UniValue& params, bool fDumpZKeys) EnsureWalletIsUnlocked(); - boost::filesystem::path exportdir; + fs::path exportdir; try { exportdir = GetExportDir(); } catch (const std::runtime_error& e) { @@ -576,9 +576,9 @@ UniValue dumpwallet_impl(const UniValue& params, bool fDumpZKeys) if (clean.compare(unclean) != 0) { throw JSONRPCError(RPC_WALLET_ERROR, strprintf("Filename is invalid as only alphanumeric characters are allowed. Try '%s' instead.", clean)); } - boost::filesystem::path exportfilepath = exportdir / clean; + fs::path exportfilepath = exportdir / clean; - if (boost::filesystem::exists(exportfilepath)) { + if (fs::exists(exportfilepath)) { throw JSONRPCError(RPC_INVALID_PARAMETER, "Cannot overwrite existing file " + exportfilepath.string()); } diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index f2cebf5a5..c3c2a5a99 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -1898,7 +1898,7 @@ UniValue backupwallet(const UniValue& params, bool fHelp) LOCK2(cs_main, pwalletMain->cs_wallet); - boost::filesystem::path exportdir; + fs::path exportdir; try { exportdir = GetExportDir(); } catch (const std::runtime_error& e) { @@ -1912,7 +1912,7 @@ UniValue backupwallet(const UniValue& params, bool fHelp) if (clean.compare(unclean) != 0) { throw JSONRPCError(RPC_WALLET_ERROR, strprintf("Filename is invalid as only alphanumeric characters are allowed. Try '%s' instead.", clean)); } - boost::filesystem::path exportfilepath = exportdir / clean; + fs::path exportfilepath = exportdir / clean; if (!BackupWallet(*pwalletMain, exportfilepath.string())) throw JSONRPCError(RPC_WALLET_ERROR, "Error: Wallet backup failed!"); diff --git a/src/wallet/test/rpc_wallet_tests.cpp b/src/wallet/test/rpc_wallet_tests.cpp index 1778a8b14..282dc1b15 100644 --- a/src/wallet/test/rpc_wallet_tests.cpp +++ b/src/wallet/test/rpc_wallet_tests.cpp @@ -5,6 +5,7 @@ #include "rpc/server.h" #include "rpc/client.h" +#include "fs.h" #include "key_io.h" #include "main.h" #include "wallet/wallet.h" @@ -35,7 +36,6 @@ #include #include #include -#include #include #include @@ -54,15 +54,15 @@ bool find_error(const UniValue& objError, const std::string& expected) { class PushCurrentDirectory { public: PushCurrentDirectory(const std::string &new_cwd) - : old_cwd(boost::filesystem::current_path()) { - boost::filesystem::current_path(new_cwd); + : old_cwd(fs::current_path()) { + fs::current_path(new_cwd); } ~PushCurrentDirectory() { - boost::filesystem::current_path(old_cwd); + fs::current_path(old_cwd); } private: - boost::filesystem::path old_cwd; + fs::path old_cwd; }; } @@ -534,9 +534,9 @@ BOOST_AUTO_TEST_CASE(rpc_wallet_z_exportwallet) BOOST_CHECK(addrs.size()==1); // Set up paths - boost::filesystem::path tmppath = boost::filesystem::temp_directory_path(); - boost::filesystem::path tmpfilename = boost::filesystem::unique_path("%%%%%%%%"); - boost::filesystem::path exportfilepath = tmppath / tmpfilename; + fs::path tmppath = fs::temp_directory_path(); + fs::path tmpfilename = fs::unique_path("%%%%%%%%"); + fs::path exportfilepath = tmppath / tmpfilename; // export will fail since exportdir is not set BOOST_CHECK_THROW(CallRPC(string("z_exportwallet ") + tmpfilename.string()), runtime_error); @@ -622,8 +622,8 @@ BOOST_AUTO_TEST_CASE(rpc_wallet_z_importwallet) std::string testWalletDump = (formatobject % testKey % testAddr).str(); // write test data to file - boost::filesystem::path temp = boost::filesystem::temp_directory_path() / - boost::filesystem::unique_path(); + fs::path temp = fs::temp_directory_path() / + fs::unique_path(); const std::string path = temp.string(); std::ofstream file(path); file << testWalletDump; diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index aec7e0060..5298b2ab7 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -12,6 +12,7 @@ #include "consensus/upgrades.h" #include "consensus/validation.h" #include "consensus/consensus.h" +#include "fs.h" #include "init.h" #include "key_io.h" #include "main.h" @@ -32,7 +33,6 @@ #include #include -#include #include using namespace std; @@ -883,15 +883,15 @@ bool CWallet::Verify() LogPrintf("Using wallet %s\n", walletFile); uiInterface.InitMessage(_("Verifying wallet...")); - if (walletFile != boost::filesystem::basename(walletFile) + boost::filesystem::extension(walletFile)) { - boost::filesystem::path path(walletFile); + if (walletFile != fs::basename(walletFile) + fs::extension(walletFile)) { + fs::path path(walletFile); if (path.is_absolute()) { - if (!boost::filesystem::exists(path.parent_path())) { + if (!fs::exists(path.parent_path())) { return UIError(strprintf(_("Absolute path %s does not exist"), walletFile)); } } else { - boost::filesystem::path full_path = GetDataDir() / path; - if (!boost::filesystem::exists(full_path.parent_path())) { + fs::path full_path = GetDataDir() / path; + if (!fs::exists(full_path.parent_path())) { return UIError(strprintf(_("Relative path %s does not exist"), walletFile)); } } @@ -900,12 +900,12 @@ bool CWallet::Verify() if (!bitdb.Open(GetDataDir())) { // try moving the database env out of the way - boost::filesystem::path pathDatabase = GetDataDir() / "database"; - boost::filesystem::path pathDatabaseBak = GetDataDir() / strprintf("database.%d.bak", GetTime()); + fs::path pathDatabase = GetDataDir() / "database"; + fs::path pathDatabaseBak = GetDataDir() / strprintf("database.%d.bak", GetTime()); try { - boost::filesystem::rename(pathDatabase, pathDatabaseBak); + fs::rename(pathDatabase, pathDatabaseBak); LogPrintf("Moved old %s to %s. Retrying.\n", pathDatabase.string(), pathDatabaseBak.string()); - } catch (const boost::filesystem::filesystem_error&) { + } catch (const fs::filesystem_error&) { // failure is ok (well, not really, but it's not worse than what we started with) } @@ -923,7 +923,7 @@ bool CWallet::Verify() return false; } - if (boost::filesystem::exists(GetDataDir() / walletFile)) + if (fs::exists(GetDataDir() / walletFile)) { CDBEnv::VerifyResult r = bitdb.Verify(walletFile, CWalletDB::Recover); if (r == CDBEnv::RECOVER_OK) diff --git a/src/wallet/walletdb.cpp b/src/wallet/walletdb.cpp index 52222ea6d..609b86363 100644 --- a/src/wallet/walletdb.cpp +++ b/src/wallet/walletdb.cpp @@ -6,6 +6,7 @@ #include "wallet/walletdb.h" #include "consensus/validation.h" +#include "fs.h" #include "key_io.h" #include "main.h" #include "proof_verifier.h" @@ -17,7 +18,6 @@ #include "wallet/wallet.h" #include "zcash/Proof.hpp" -#include #include #include #include @@ -1172,16 +1172,16 @@ bool BackupWallet(const CWallet& wallet, const string& strDest) bitdb.mapFileUseCount.erase(wallet.strWalletFile); // Copy wallet file - boost::filesystem::path pathSrc = GetDataDir() / wallet.strWalletFile; - boost::filesystem::path pathDest(strDest); - if (boost::filesystem::is_directory(pathDest)) + fs::path pathSrc = GetDataDir() / wallet.strWalletFile; + fs::path pathDest(strDest); + if (fs::is_directory(pathDest)) pathDest /= wallet.strWalletFile; try { - boost::filesystem::copy_file(pathSrc, pathDest, boost::filesystem::copy_option::overwrite_if_exists); + fs::copy_file(pathSrc, pathDest, fs::copy_option::overwrite_if_exists); LogPrintf("copied %s to %s\n", wallet.strWalletFile, pathDest.string()); return true; - } catch (const boost::filesystem::filesystem_error& e) { + } catch (const fs::filesystem_error& e) { LogPrintf("error copying %s to %s - %s\n", wallet.strWalletFile, pathDest.string(), e.what()); return false; } diff --git a/src/zcbenchmarks.cpp b/src/zcbenchmarks.cpp index 419398ae4..6e8bc1126 100644 --- a/src/zcbenchmarks.cpp +++ b/src/zcbenchmarks.cpp @@ -3,7 +3,6 @@ #include #include #include -#include #include "coins.h" #include "util.h" @@ -514,7 +513,7 @@ double benchmark_connectblock_slow() // Test for issue 2017-05-01.a SelectParams(CBaseChainParams::MAIN); CBlock block; - FILE* fp = fopen((GetDataDir() / "benchmark/block-107134.dat").string().c_str(), "rb"); + FILE* fp = fsbridge::fopen(GetDataDir() / "benchmark/block-107134.dat", "rb"); if (!fp) throw new std::runtime_error("Failed to open block data file"); CAutoFile blkFile(fp, SER_DISK, CLIENT_VERSION); blkFile >> block;