Skip to content

Commit 8c28670

Browse files
committed
Merge #9902: Lightweight abstraction of boost::filesystem
f110272 Remove `namespace fs=fs` (Wladimir J. van der Laan) 75594bd torcontrol: Use fs::path instead of std::string for private key path (Wladimir J. van der Laan) 2a5f574 Use fsbridge for fopen and freopen (Wladimir J. van der Laan) bac5c9c Replace uses of boost::filesystem with fs (Wladimir J. van der Laan) 7d5172d Replace includes of boost/filesystem.h with fs.h (Wladimir J. van der Laan) 19e36bb Add fs.cpp/h (Wladimir J. van der Laan) Tree-SHA512: 2c34f059dfa6850b9323f3389e9090a6b5f839a457a2960d182c2ecfafd9883c956f5928bb796613402d3aad68ebc78259796a7a313f4a6cfa98aaf507a66842
2 parents c7e73ea + f110272 commit 8c28670

34 files changed

+233
-207
lines changed

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ BITCOIN_CORE_H = \
9898
core_io.h \
9999
core_memusage.h \
100100
cuckoocache.h \
101+
fs.h \
101102
httprpc.h \
102103
httpserver.h \
103104
indirectmap.h \
@@ -326,6 +327,7 @@ libbitcoin_util_a_SOURCES = \
326327
compat/glibc_sanity.cpp \
327328
compat/glibcxx_sanity.cpp \
328329
compat/strnlen.cpp \
330+
fs.cpp \
329331
random.cpp \
330332
rpc/protocol.cpp \
331333
support/cleanse.cpp \

src/addrdb.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
#include "addrman.h"
99
#include "chainparams.h"
1010
#include "clientversion.h"
11+
#include "fs.h"
1112
#include "hash.h"
1213
#include "random.h"
1314
#include "streams.h"
1415
#include "tinyformat.h"
1516
#include "util.h"
1617

17-
#include <boost/filesystem.hpp>
1818

1919
CBanDB::CBanDB()
2020
{
@@ -36,8 +36,8 @@ bool CBanDB::Write(const banmap_t& banSet)
3636
ssBanlist << hash;
3737

3838
// open temp output file, and associate with CAutoFile
39-
boost::filesystem::path pathTmp = GetDataDir() / tmpfn;
40-
FILE *file = fopen(pathTmp.string().c_str(), "wb");
39+
fs::path pathTmp = GetDataDir() / tmpfn;
40+
FILE *file = fsbridge::fopen(pathTmp, "wb");
4141
CAutoFile fileout(file, SER_DISK, CLIENT_VERSION);
4242
if (fileout.IsNull())
4343
return error("%s: Failed to open file %s", __func__, pathTmp.string());
@@ -62,13 +62,13 @@ bool CBanDB::Write(const banmap_t& banSet)
6262
bool CBanDB::Read(banmap_t& banSet)
6363
{
6464
// open input file, and associate with CAutoFile
65-
FILE *file = fopen(pathBanlist.string().c_str(), "rb");
65+
FILE *file = fsbridge::fopen(pathBanlist, "rb");
6666
CAutoFile filein(file, SER_DISK, CLIENT_VERSION);
6767
if (filein.IsNull())
6868
return error("%s: Failed to open file %s", __func__, pathBanlist.string());
6969

7070
// use file size to size memory buffer
71-
uint64_t fileSize = boost::filesystem::file_size(pathBanlist);
71+
uint64_t fileSize = fs::file_size(pathBanlist);
7272
uint64_t dataSize = 0;
7373
// Don't try to resize to a negative number if file is small
7474
if (fileSize >= sizeof(uint256))
@@ -133,8 +133,8 @@ bool CAddrDB::Write(const CAddrMan& addr)
133133
ssPeers << hash;
134134

135135
// open temp output file, and associate with CAutoFile
136-
boost::filesystem::path pathTmp = GetDataDir() / tmpfn;
137-
FILE *file = fopen(pathTmp.string().c_str(), "wb");
136+
fs::path pathTmp = GetDataDir() / tmpfn;
137+
FILE *file = fsbridge::fopen(pathTmp, "wb");
138138
CAutoFile fileout(file, SER_DISK, CLIENT_VERSION);
139139
if (fileout.IsNull())
140140
return error("%s: Failed to open file %s", __func__, pathTmp.string());
@@ -159,13 +159,13 @@ bool CAddrDB::Write(const CAddrMan& addr)
159159
bool CAddrDB::Read(CAddrMan& addr)
160160
{
161161
// open input file, and associate with CAutoFile
162-
FILE *file = fopen(pathAddr.string().c_str(), "rb");
162+
FILE *file = fsbridge::fopen(pathAddr, "rb");
163163
CAutoFile filein(file, SER_DISK, CLIENT_VERSION);
164164
if (filein.IsNull())
165165
return error("%s: Failed to open file %s", __func__, pathAddr.string());
166166

167167
// use file size to size memory buffer
168-
uint64_t fileSize = boost::filesystem::file_size(pathAddr);
168+
uint64_t fileSize = fs::file_size(pathAddr);
169169
uint64_t dataSize = 0;
170170
// Don't try to resize to a negative number if file is small
171171
if (fileSize >= sizeof(uint256))

src/addrdb.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
#ifndef BITCOIN_ADDRDB_H
77
#define BITCOIN_ADDRDB_H
88

9+
#include "fs.h"
910
#include "serialize.h"
1011

1112
#include <string>
1213
#include <map>
13-
#include <boost/filesystem/path.hpp>
1414

1515
class CSubNet;
1616
class CAddrMan;
@@ -80,7 +80,7 @@ typedef std::map<CSubNet, CBanEntry> banmap_t;
8080
class CAddrDB
8181
{
8282
private:
83-
boost::filesystem::path pathAddr;
83+
fs::path pathAddr;
8484
public:
8585
CAddrDB();
8686
bool Write(const CAddrMan& addr);
@@ -92,7 +92,7 @@ class CAddrDB
9292
class CBanDB
9393
{
9494
private:
95-
boost::filesystem::path pathBanlist;
95+
fs::path pathBanlist;
9696
public:
9797
CBanDB();
9898
bool Write(const banmap_t& banSet);

src/bitcoin-cli.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99

1010
#include "chainparamsbase.h"
1111
#include "clientversion.h"
12+
#include "fs.h"
1213
#include "rpc/client.h"
1314
#include "rpc/protocol.h"
1415
#include "util.h"
1516
#include "utilstrencodings.h"
1617

17-
#include <boost/filesystem/operations.hpp>
1818
#include <stdio.h>
1919

2020
#include <event2/buffer.h>
@@ -96,7 +96,7 @@ static int AppInitRPC(int argc, char* argv[])
9696
}
9797
return EXIT_SUCCESS;
9898
}
99-
if (!boost::filesystem::is_directory(GetDataDir(false))) {
99+
if (!fs::is_directory(GetDataDir(false))) {
100100
fprintf(stderr, "Error: Specified data directory \"%s\" does not exist.\n", GetArg("-datadir", "").c_str());
101101
return EXIT_FAILURE;
102102
}

src/bitcoind.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "chainparams.h"
1111
#include "clientversion.h"
1212
#include "compat.h"
13+
#include "fs.h"
1314
#include "rpc/server.h"
1415
#include "init.h"
1516
#include "noui.h"
@@ -20,7 +21,6 @@
2021
#include "utilstrencodings.h"
2122

2223
#include <boost/algorithm/string/predicate.hpp>
23-
#include <boost/filesystem.hpp>
2424
#include <boost/thread.hpp>
2525

2626
#include <stdio.h>
@@ -97,7 +97,7 @@ bool AppInit(int argc, char* argv[])
9797

9898
try
9999
{
100-
if (!boost::filesystem::is_directory(GetDataDir(false)))
100+
if (!fs::is_directory(GetDataDir(false)))
101101
{
102102
fprintf(stderr, "Error: Specified data directory \"%s\" does not exist.\n", GetArg("-datadir", "").c_str());
103103
return false;

src/dbwrapper.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44

55
#include "dbwrapper.h"
66

7+
#include "fs.h"
78
#include "util.h"
89
#include "random.h"
910

10-
#include <boost/filesystem.hpp>
11-
1211
#include <leveldb/cache.h>
1312
#include <leveldb/env.h>
1413
#include <leveldb/filter_policy.h>
@@ -91,7 +90,7 @@ static leveldb::Options GetOptions(size_t nCacheSize)
9190
return options;
9291
}
9392

94-
CDBWrapper::CDBWrapper(const boost::filesystem::path& path, size_t nCacheSize, bool fMemory, bool fWipe, bool obfuscate)
93+
CDBWrapper::CDBWrapper(const fs::path& path, size_t nCacheSize, bool fMemory, bool fWipe, bool obfuscate)
9594
{
9695
penv = NULL;
9796
readoptions.verify_checksums = true;

src/dbwrapper.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@
66
#define BITCOIN_DBWRAPPER_H
77

88
#include "clientversion.h"
9+
#include "fs.h"
910
#include "serialize.h"
1011
#include "streams.h"
1112
#include "util.h"
1213
#include "utilstrencodings.h"
1314
#include "version.h"
1415

15-
#include <boost/filesystem/path.hpp>
16-
1716
#include <leveldb/db.h>
1817
#include <leveldb/write_batch.h>
1918

@@ -195,7 +194,7 @@ class CDBWrapper
195194
* @param[in] obfuscate If true, store data obfuscated via simple XOR. If false, XOR
196195
* with a zero'd byte array.
197196
*/
198-
CDBWrapper(const boost::filesystem::path& path, size_t nCacheSize, bool fMemory = false, bool fWipe = false, bool obfuscate = false);
197+
CDBWrapper(const fs::path& path, size_t nCacheSize, bool fMemory = false, bool fWipe = false, bool obfuscate = false);
199198
~CDBWrapper();
200199

201200
template <typename K, typename V>

src/fs.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include "fs.h"
2+
3+
#include <boost/filesystem.hpp>
4+
5+
namespace fsbridge {
6+
7+
FILE *fopen(const fs::path& p, const char *mode)
8+
{
9+
return ::fopen(p.string().c_str(), mode);
10+
}
11+
12+
FILE *freopen(const fs::path& p, const char *mode, FILE *stream)
13+
{
14+
return ::freopen(p.string().c_str(), mode, stream);
15+
}
16+
17+
} // fsbridge

src/fs.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (c) 2017 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_FS_H
6+
#define BITCOIN_FS_H
7+
8+
#include <stdio.h>
9+
#include <string>
10+
11+
#include <boost/filesystem.hpp>
12+
#include <boost/filesystem/fstream.hpp>
13+
#include <boost/filesystem/detail/utf8_codecvt_facet.hpp>
14+
15+
/** Filesystem operations and types */
16+
namespace fs = boost::filesystem;
17+
18+
/** Bridge operations to C stdio */
19+
namespace fsbridge {
20+
FILE *fopen(const fs::path& p, const char *mode);
21+
FILE *freopen(const fs::path& p, const char *mode, FILE *stream);
22+
};
23+
24+
#endif

0 commit comments

Comments
 (0)