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.
This commit is contained in:
Wladimir J. van der Laan 2017-03-01 16:28:39 +00:00
parent bac5c9cf64
commit 2a5f574762
5 changed files with 19 additions and 19 deletions

View File

@ -37,7 +37,7 @@ bool CBanDB::Write(const banmap_t& banSet)
// open temp output file, and associate with CAutoFile // open temp output file, and associate with CAutoFile
fs::path pathTmp = GetDataDir() / tmpfn; fs::path pathTmp = GetDataDir() / tmpfn;
FILE *file = fopen(pathTmp.string().c_str(), "wb"); FILE *file = fsbridge::fopen(pathTmp, "wb");
CAutoFile fileout(file, SER_DISK, CLIENT_VERSION); CAutoFile fileout(file, SER_DISK, CLIENT_VERSION);
if (fileout.IsNull()) if (fileout.IsNull())
return error("%s: Failed to open file %s", __func__, pathTmp.string()); return error("%s: Failed to open file %s", __func__, pathTmp.string());
@ -62,7 +62,7 @@ bool CBanDB::Write(const banmap_t& banSet)
bool CBanDB::Read(banmap_t& banSet) bool CBanDB::Read(banmap_t& banSet)
{ {
// open input file, and associate with CAutoFile // open input file, and associate with CAutoFile
FILE *file = fopen(pathBanlist.string().c_str(), "rb"); FILE *file = fsbridge::fopen(pathBanlist, "rb");
CAutoFile filein(file, SER_DISK, CLIENT_VERSION); CAutoFile filein(file, SER_DISK, CLIENT_VERSION);
if (filein.IsNull()) if (filein.IsNull())
return error("%s: Failed to open file %s", __func__, pathBanlist.string()); return error("%s: Failed to open file %s", __func__, pathBanlist.string());
@ -134,7 +134,7 @@ bool CAddrDB::Write(const CAddrMan& addr)
// open temp output file, and associate with CAutoFile // open temp output file, and associate with CAutoFile
fs::path pathTmp = GetDataDir() / tmpfn; fs::path pathTmp = GetDataDir() / tmpfn;
FILE *file = fopen(pathTmp.string().c_str(), "wb"); FILE *file = fsbridge::fopen(pathTmp, "wb");
CAutoFile fileout(file, SER_DISK, CLIENT_VERSION); CAutoFile fileout(file, SER_DISK, CLIENT_VERSION);
if (fileout.IsNull()) if (fileout.IsNull())
return error("%s: Failed to open file %s", __func__, pathTmp.string()); return error("%s: Failed to open file %s", __func__, pathTmp.string());
@ -159,7 +159,7 @@ bool CAddrDB::Write(const CAddrMan& addr)
bool CAddrDB::Read(CAddrMan& addr) bool CAddrDB::Read(CAddrMan& addr)
{ {
// open input file, and associate with CAutoFile // 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); CAutoFile filein(file, SER_DISK, CLIENT_VERSION);
if (filein.IsNull()) if (filein.IsNull())
return error("%s: Failed to open file %s", __func__, pathAddr.string()); return error("%s: Failed to open file %s", __func__, pathAddr.string());

View File

@ -213,7 +213,7 @@ void Shutdown()
if (fFeeEstimatesInitialized) if (fFeeEstimatesInitialized)
{ {
fs::path est_path = GetDataDir() / FEE_ESTIMATES_FILENAME; fs::path est_path = GetDataDir() / FEE_ESTIMATES_FILENAME;
CAutoFile est_fileout(fopen(est_path.string().c_str(), "wb"), SER_DISK, CLIENT_VERSION); CAutoFile est_fileout(fsbridge::fopen(est_path, "wb"), SER_DISK, CLIENT_VERSION);
if (!est_fileout.IsNull()) if (!est_fileout.IsNull())
mempool.WriteFeeEstimates(est_fileout); mempool.WriteFeeEstimates(est_fileout);
else else
@ -643,7 +643,7 @@ void ThreadImport(std::vector<fs::path> vImportFiles)
// hardcoded $DATADIR/bootstrap.dat // hardcoded $DATADIR/bootstrap.dat
fs::path pathBootstrap = GetDataDir() / "bootstrap.dat"; fs::path pathBootstrap = GetDataDir() / "bootstrap.dat";
if (fs::exists(pathBootstrap)) { if (fs::exists(pathBootstrap)) {
FILE *file = fopen(pathBootstrap.string().c_str(), "rb"); FILE *file = fsbridge::fopen(pathBootstrap, "rb");
if (file) { if (file) {
fs::path pathBootstrapOld = GetDataDir() / "bootstrap.dat.old"; fs::path pathBootstrapOld = GetDataDir() / "bootstrap.dat.old";
LogPrintf("Importing bootstrap.dat...\n"); LogPrintf("Importing bootstrap.dat...\n");
@ -656,7 +656,7 @@ void ThreadImport(std::vector<fs::path> vImportFiles)
// -loadblock= // -loadblock=
BOOST_FOREACH(const fs::path& path, vImportFiles) { BOOST_FOREACH(const fs::path& path, vImportFiles) {
FILE *file = fopen(path.string().c_str(), "rb"); FILE *file = fsbridge::fopen(path, "rb");
if (file) { if (file) {
LogPrintf("Importing blocks file %s...\n", path.string()); LogPrintf("Importing blocks file %s...\n", path.string());
LoadExternalBlockFile(chainparams, file); LoadExternalBlockFile(chainparams, file);
@ -1124,7 +1124,7 @@ static bool LockDataDirectory(bool probeOnly)
// Make sure only a single Bitcoin process is using the data directory. // Make sure only a single Bitcoin process is using the data directory.
fs::path pathLockFile = GetDataDir() / ".lock"; fs::path pathLockFile = GetDataDir() / ".lock";
FILE* file = fopen(pathLockFile.string().c_str(), "a"); // empty lock file; created if it doesn't exist. FILE* file = fsbridge::fopen(pathLockFile, "a"); // empty lock file; created if it doesn't exist.
if (file) fclose(file); if (file) fclose(file);
try { try {
@ -1535,7 +1535,7 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
LogPrintf(" block index %15dms\n", GetTimeMillis() - nStart); LogPrintf(" block index %15dms\n", GetTimeMillis() - nStart);
fs::path est_path = GetDataDir() / FEE_ESTIMATES_FILENAME; fs::path est_path = GetDataDir() / FEE_ESTIMATES_FILENAME;
CAutoFile est_filein(fopen(est_path.string().c_str(), "rb"), SER_DISK, CLIENT_VERSION); CAutoFile est_filein(fsbridge::fopen(est_path, "rb"), SER_DISK, CLIENT_VERSION);
// Allowed to fail as this file IS missing on first startup. // Allowed to fail as this file IS missing on first startup.
if (!est_filein.IsNull()) if (!est_filein.IsNull())
mempool.ReadFeeEstimates(est_filein); mempool.ReadFeeEstimates(est_filein);

View File

@ -215,7 +215,7 @@ void OpenDebugLog()
assert(fileout == NULL); assert(fileout == NULL);
assert(vMsgsBeforeOpenLog); assert(vMsgsBeforeOpenLog);
fs::path pathDebug = GetDataDir() / "debug.log"; fs::path pathDebug = GetDataDir() / "debug.log";
fileout = fopen(pathDebug.string().c_str(), "a"); fileout = fsbridge::fopen(pathDebug, "a");
if (fileout) { if (fileout) {
setbuf(fileout, NULL); // unbuffered setbuf(fileout, NULL); // unbuffered
// dump buffered messages from before we opened the log // dump buffered messages from before we opened the log
@ -354,7 +354,7 @@ int LogPrintStr(const std::string &str)
if (fReopenDebugLog) { if (fReopenDebugLog) {
fReopenDebugLog = false; fReopenDebugLog = false;
fs::path pathDebug = GetDataDir() / "debug.log"; fs::path pathDebug = GetDataDir() / "debug.log";
if (freopen(pathDebug.string().c_str(),"a",fileout) != NULL) if (fsbridge::freopen(pathDebug,"a",fileout) != NULL)
setbuf(fileout, NULL); // unbuffered setbuf(fileout, NULL); // unbuffered
} }
@ -625,7 +625,7 @@ fs::path GetPidFile()
void CreatePidFile(const fs::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) if (file)
{ {
fprintf(file, "%d\n", pid); fprintf(file, "%d\n", pid);
@ -764,7 +764,7 @@ void ShrinkDebugFile()
constexpr size_t RECENT_DEBUG_HISTORY_SIZE = 10 * 1000000; constexpr size_t RECENT_DEBUG_HISTORY_SIZE = 10 * 1000000;
// Scroll debug.log if it's getting too big // Scroll debug.log if it's getting too big
fs::path pathLog = GetDataDir() / "debug.log"; fs::path pathLog = GetDataDir() / "debug.log";
FILE* file = fopen(pathLog.string().c_str(), "r"); FILE* file = fsbridge::fopen(pathLog, "r");
// If debug.log file is more than 10% bigger the RECENT_DEBUG_HISTORY_SIZE // If debug.log file is more than 10% bigger the RECENT_DEBUG_HISTORY_SIZE
// trim it down by saving only the last RECENT_DEBUG_HISTORY_SIZE bytes // trim it down by saving only the last RECENT_DEBUG_HISTORY_SIZE bytes
if (file && fs::file_size(pathLog) > 11 * (RECENT_DEBUG_HISTORY_SIZE / 10)) if (file && fs::file_size(pathLog) > 11 * (RECENT_DEBUG_HISTORY_SIZE / 10))
@ -775,7 +775,7 @@ void ShrinkDebugFile()
int nBytes = fread(vch.data(), 1, vch.size(), file); int nBytes = fread(vch.data(), 1, vch.size(), file);
fclose(file); fclose(file);
file = fopen(pathLog.string().c_str(), "w"); file = fsbridge::fopen(pathLog, "w");
if (file) if (file)
{ {
fwrite(vch.data(), 1, nBytes, file); fwrite(vch.data(), 1, nBytes, file);

View File

@ -3414,9 +3414,9 @@ FILE* OpenDiskFile(const CDiskBlockPos &pos, const char *prefix, bool fReadOnly)
return NULL; return NULL;
fs::path path = GetBlockPosFilename(pos, prefix); fs::path path = GetBlockPosFilename(pos, prefix);
fs::create_directories(path.parent_path()); fs::create_directories(path.parent_path());
FILE* file = fopen(path.string().c_str(), "rb+"); FILE* file = fsbridge::fopen(path, "rb+");
if (!file && !fReadOnly) if (!file && !fReadOnly)
file = fopen(path.string().c_str(), "wb+"); file = fsbridge::fopen(path, "wb+");
if (!file) { if (!file) {
LogPrintf("Unable to open file %s\n", path.string()); LogPrintf("Unable to open file %s\n", path.string());
return NULL; return NULL;
@ -4164,7 +4164,7 @@ static const uint64_t MEMPOOL_DUMP_VERSION = 1;
bool LoadMempool(void) bool LoadMempool(void)
{ {
int64_t nExpiryTimeout = GetArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY) * 60 * 60; int64_t nExpiryTimeout = GetArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY) * 60 * 60;
FILE* filestr = fopen((GetDataDir() / "mempool.dat").string().c_str(), "rb"); FILE* filestr = fsbridge::fopen(GetDataDir() / "mempool.dat", "rb");
CAutoFile file(filestr, SER_DISK, CLIENT_VERSION); CAutoFile file(filestr, SER_DISK, CLIENT_VERSION);
if (file.IsNull()) { if (file.IsNull()) {
LogPrintf("Failed to open mempool file from disk. Continuing anyway.\n"); LogPrintf("Failed to open mempool file from disk. Continuing anyway.\n");
@ -4244,7 +4244,7 @@ void DumpMempool(void)
int64_t mid = GetTimeMicros(); int64_t mid = GetTimeMicros();
try { try {
FILE* filestr = fopen((GetDataDir() / "mempool.dat.new").string().c_str(), "wb"); FILE* filestr = fsbridge::fopen(GetDataDir() / "mempool.dat.new", "wb");
if (!filestr) { if (!filestr) {
return; return;
} }

View File

@ -89,7 +89,7 @@ bool CDBEnv::Open(const fs::path& pathIn)
dbenv->set_lg_max(1048576); dbenv->set_lg_max(1048576);
dbenv->set_lk_max_locks(40000); dbenv->set_lk_max_locks(40000);
dbenv->set_lk_max_objects(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_AUTO_COMMIT, 1);
dbenv->set_flags(DB_TXN_WRITE_NOSYNC, 1); dbenv->set_flags(DB_TXN_WRITE_NOSYNC, 1);
dbenv->log_set_config(DB_LOG_AUTO_REMOVE, 1); dbenv->log_set_config(DB_LOG_AUTO_REMOVE, 1);