Skip to content

Commit 2a5f574

Browse files
committed
Use fsbridge for fopen and freopen
Abstracts away how a path is opened to a `FILE*`. Reduces the number of places where path is converted to a string for anything else but printing.
1 parent bac5c9c commit 2a5f574

File tree

5 files changed

+19
-19
lines changed

5 files changed

+19
-19
lines changed

src/addrdb.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ bool CBanDB::Write(const banmap_t& banSet)
3737

3838
// open temp output file, and associate with CAutoFile
3939
fs::path pathTmp = GetDataDir() / tmpfn;
40-
FILE *file = fopen(pathTmp.string().c_str(), "wb");
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,7 +62,7 @@ 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());
@@ -134,7 +134,7 @@ bool CAddrDB::Write(const CAddrMan& addr)
134134

135135
// open temp output file, and associate with CAutoFile
136136
fs::path pathTmp = GetDataDir() / tmpfn;
137-
FILE *file = fopen(pathTmp.string().c_str(), "wb");
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,7 +159,7 @@ 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());

src/init.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ void Shutdown()
213213
if (fFeeEstimatesInitialized)
214214
{
215215
fs::path est_path = GetDataDir() / FEE_ESTIMATES_FILENAME;
216-
CAutoFile est_fileout(fopen(est_path.string().c_str(), "wb"), SER_DISK, CLIENT_VERSION);
216+
CAutoFile est_fileout(fsbridge::fopen(est_path, "wb"), SER_DISK, CLIENT_VERSION);
217217
if (!est_fileout.IsNull())
218218
mempool.WriteFeeEstimates(est_fileout);
219219
else
@@ -643,7 +643,7 @@ void ThreadImport(std::vector<fs::path> vImportFiles)
643643
// hardcoded $DATADIR/bootstrap.dat
644644
fs::path pathBootstrap = GetDataDir() / "bootstrap.dat";
645645
if (fs::exists(pathBootstrap)) {
646-
FILE *file = fopen(pathBootstrap.string().c_str(), "rb");
646+
FILE *file = fsbridge::fopen(pathBootstrap, "rb");
647647
if (file) {
648648
fs::path pathBootstrapOld = GetDataDir() / "bootstrap.dat.old";
649649
LogPrintf("Importing bootstrap.dat...\n");
@@ -656,7 +656,7 @@ void ThreadImport(std::vector<fs::path> vImportFiles)
656656

657657
// -loadblock=
658658
BOOST_FOREACH(const fs::path& path, vImportFiles) {
659-
FILE *file = fopen(path.string().c_str(), "rb");
659+
FILE *file = fsbridge::fopen(path, "rb");
660660
if (file) {
661661
LogPrintf("Importing blocks file %s...\n", path.string());
662662
LoadExternalBlockFile(chainparams, file);
@@ -1124,7 +1124,7 @@ static bool LockDataDirectory(bool probeOnly)
11241124

11251125
// Make sure only a single Bitcoin process is using the data directory.
11261126
fs::path pathLockFile = GetDataDir() / ".lock";
1127-
FILE* file = fopen(pathLockFile.string().c_str(), "a"); // empty lock file; created if it doesn't exist.
1127+
FILE* file = fsbridge::fopen(pathLockFile, "a"); // empty lock file; created if it doesn't exist.
11281128
if (file) fclose(file);
11291129

11301130
try {
@@ -1535,7 +1535,7 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
15351535
LogPrintf(" block index %15dms\n", GetTimeMillis() - nStart);
15361536

15371537
fs::path est_path = GetDataDir() / FEE_ESTIMATES_FILENAME;
1538-
CAutoFile est_filein(fopen(est_path.string().c_str(), "rb"), SER_DISK, CLIENT_VERSION);
1538+
CAutoFile est_filein(fsbridge::fopen(est_path, "rb"), SER_DISK, CLIENT_VERSION);
15391539
// Allowed to fail as this file IS missing on first startup.
15401540
if (!est_filein.IsNull())
15411541
mempool.ReadFeeEstimates(est_filein);

src/util.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ void OpenDebugLog()
215215
assert(fileout == NULL);
216216
assert(vMsgsBeforeOpenLog);
217217
fs::path pathDebug = GetDataDir() / "debug.log";
218-
fileout = fopen(pathDebug.string().c_str(), "a");
218+
fileout = fsbridge::fopen(pathDebug, "a");
219219
if (fileout) {
220220
setbuf(fileout, NULL); // unbuffered
221221
// dump buffered messages from before we opened the log
@@ -354,7 +354,7 @@ int LogPrintStr(const std::string &str)
354354
if (fReopenDebugLog) {
355355
fReopenDebugLog = false;
356356
fs::path pathDebug = GetDataDir() / "debug.log";
357-
if (freopen(pathDebug.string().c_str(),"a",fileout) != NULL)
357+
if (fsbridge::freopen(pathDebug,"a",fileout) != NULL)
358358
setbuf(fileout, NULL); // unbuffered
359359
}
360360

@@ -625,7 +625,7 @@ fs::path GetPidFile()
625625

626626
void CreatePidFile(const fs::path &path, pid_t pid)
627627
{
628-
FILE* file = fopen(path.string().c_str(), "w");
628+
FILE* file = fsbridge::fopen(path, "w");
629629
if (file)
630630
{
631631
fprintf(file, "%d\n", pid);
@@ -764,7 +764,7 @@ void ShrinkDebugFile()
764764
constexpr size_t RECENT_DEBUG_HISTORY_SIZE = 10 * 1000000;
765765
// Scroll debug.log if it's getting too big
766766
fs::path pathLog = GetDataDir() / "debug.log";
767-
FILE* file = fopen(pathLog.string().c_str(), "r");
767+
FILE* file = fsbridge::fopen(pathLog, "r");
768768
// If debug.log file is more than 10% bigger the RECENT_DEBUG_HISTORY_SIZE
769769
// trim it down by saving only the last RECENT_DEBUG_HISTORY_SIZE bytes
770770
if (file && fs::file_size(pathLog) > 11 * (RECENT_DEBUG_HISTORY_SIZE / 10))
@@ -775,7 +775,7 @@ void ShrinkDebugFile()
775775
int nBytes = fread(vch.data(), 1, vch.size(), file);
776776
fclose(file);
777777

778-
file = fopen(pathLog.string().c_str(), "w");
778+
file = fsbridge::fopen(pathLog, "w");
779779
if (file)
780780
{
781781
fwrite(vch.data(), 1, nBytes, file);

src/validation.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3414,9 +3414,9 @@ FILE* OpenDiskFile(const CDiskBlockPos &pos, const char *prefix, bool fReadOnly)
34143414
return NULL;
34153415
fs::path path = GetBlockPosFilename(pos, prefix);
34163416
fs::create_directories(path.parent_path());
3417-
FILE* file = fopen(path.string().c_str(), "rb+");
3417+
FILE* file = fsbridge::fopen(path, "rb+");
34183418
if (!file && !fReadOnly)
3419-
file = fopen(path.string().c_str(), "wb+");
3419+
file = fsbridge::fopen(path, "wb+");
34203420
if (!file) {
34213421
LogPrintf("Unable to open file %s\n", path.string());
34223422
return NULL;
@@ -4164,7 +4164,7 @@ static const uint64_t MEMPOOL_DUMP_VERSION = 1;
41644164
bool LoadMempool(void)
41654165
{
41664166
int64_t nExpiryTimeout = GetArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY) * 60 * 60;
4167-
FILE* filestr = fopen((GetDataDir() / "mempool.dat").string().c_str(), "rb");
4167+
FILE* filestr = fsbridge::fopen(GetDataDir() / "mempool.dat", "rb");
41684168
CAutoFile file(filestr, SER_DISK, CLIENT_VERSION);
41694169
if (file.IsNull()) {
41704170
LogPrintf("Failed to open mempool file from disk. Continuing anyway.\n");
@@ -4244,7 +4244,7 @@ void DumpMempool(void)
42444244
int64_t mid = GetTimeMicros();
42454245

42464246
try {
4247-
FILE* filestr = fopen((GetDataDir() / "mempool.dat.new").string().c_str(), "wb");
4247+
FILE* filestr = fsbridge::fopen(GetDataDir() / "mempool.dat.new", "wb");
42484248
if (!filestr) {
42494249
return;
42504250
}

src/wallet/db.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ bool CDBEnv::Open(const fs::path& pathIn)
8989
dbenv->set_lg_max(1048576);
9090
dbenv->set_lk_max_locks(40000);
9191
dbenv->set_lk_max_objects(40000);
92-
dbenv->set_errfile(fopen(pathErrorFile.string().c_str(), "a")); /// debug
92+
dbenv->set_errfile(fsbridge::fopen(pathErrorFile, "a")); /// debug
9393
dbenv->set_flags(DB_AUTO_COMMIT, 1);
9494
dbenv->set_flags(DB_TXN_WRITE_NOSYNC, 1);
9595
dbenv->log_set_config(DB_LOG_AUTO_REMOVE, 1);

0 commit comments

Comments
 (0)