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 committed by Jack Grigg
parent 73bc7a068d
commit c0603a9fa6
8 changed files with 15 additions and 15 deletions

View File

@ -211,7 +211,7 @@ void Shutdown()
if (fFeeEstimatesInitialized)
{
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())
mempool.WriteFeeEstimates(est_fileout);
else
@ -628,7 +628,7 @@ void ThreadImport(std::vector<fs::path> vImportFiles)
// hardcoded $DATADIR/bootstrap.dat
fs::path pathBootstrap = GetDataDir() / "bootstrap.dat";
if (fs::exists(pathBootstrap)) {
FILE *file = fopen(pathBootstrap.string().c_str(), "rb");
FILE *file = fsbridge::fopen(pathBootstrap, "rb");
if (file) {
CImportingNow imp;
fs::path pathBootstrapOld = GetDataDir() / "bootstrap.dat.old";
@ -642,7 +642,7 @@ void ThreadImport(std::vector<fs::path> vImportFiles)
// -loadblock=
BOOST_FOREACH(const fs::path& path, vImportFiles) {
FILE *file = fopen(path.string().c_str(), "rb");
FILE *file = fsbridge::fopen(path, "rb");
if (file) {
CImportingNow imp;
LogPrintf("Importing blocks file %s...\n", path.string());
@ -1162,7 +1162,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
// Make sure only a single Bitcoin process is using the data directory.
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);
try {
@ -1539,7 +1539,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
LogPrintf(" block index %15dms\n", GetTimeMillis() - nStart);
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.
if (!est_filein.IsNull())
mempool.ReadFeeEstimates(est_filein);

View File

@ -142,7 +142,7 @@ void ShrinkDebugFile()
{
// Scroll debug.log if it's getting too big
fs::path pathLog = GetDebugLogPath();
FILE* file = fopen(pathLog.string().c_str(), "r");
FILE* file = fsbridge::fopen(pathLog, "r");
if (file && fs::file_size(pathLog) > 10 * 1000000)
{
// Restart the file with some of the end
@ -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);

View File

@ -4552,9 +4552,9 @@ FILE* OpenDiskFile(const CDiskBlockPos &pos, const char *prefix, bool fReadOnly)
return NULL;
fs::path path = GetBlockPosFilename(pos, prefix);
fs::create_directories(path.parent_path());
FILE* file = fopen(path.string().c_str(), "rb+");
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;

View File

@ -1990,7 +1990,7 @@ bool CAddrDB::Write(const CAddrMan& addr)
// open temp output file, and associate with CAutoFile
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);
if (fileout.IsNull())
return error("%s: Failed to open file %s", __func__, pathTmp.string());
@ -2015,7 +2015,7 @@ 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());

View File

@ -28,7 +28,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

View File

@ -454,7 +454,7 @@ fs::path GetPidFile()
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);

View File

@ -94,7 +94,7 @@ bool CDBEnv::Open(const fs::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);

View File

@ -513,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;