From a873823864a00f68772eb2b85a70e933839ca3f5 Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Mon, 20 Oct 2014 12:45:50 +0200 Subject: CAutoFile: Explicit Get() and remove unused methods Also add documentation to some methods. --- src/init.cpp | 4 ++-- src/main.cpp | 16 ++++++++-------- src/net.cpp | 2 +- src/serialize.h | 19 +++++++++++++------ src/test/checkblock_tests.cpp | 2 +- 5 files changed, 25 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/init.cpp b/src/init.cpp index 55991ac9b3..76eb48a177 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -139,7 +139,7 @@ void Shutdown() { boost::filesystem::path est_path = GetDataDir() / FEE_ESTIMATES_FILENAME; CAutoFile est_fileout(fopen(est_path.string().c_str(), "wb"), SER_DISK, CLIENT_VERSION); - if (est_fileout) + if (!est_fileout.IsNull()) mempool.WriteFeeEstimates(est_fileout); else LogPrintf("%s: Failed to write fee estimates to %s\n", __func__, est_path.string()); @@ -1064,7 +1064,7 @@ bool AppInit2(boost::thread_group& threadGroup) boost::filesystem::path est_path = GetDataDir() / FEE_ESTIMATES_FILENAME; CAutoFile est_filein(fopen(est_path.string().c_str(), "rb"), SER_DISK, CLIENT_VERSION); // Allowed to fail as this file IS missing on first startup. - if (est_filein) + if (!est_filein.IsNull()) mempool.ReadFeeEstimates(est_filein); fFeeEstimatesInitialized = true; diff --git a/src/main.cpp b/src/main.cpp index 05f1a2eb4d..45e679b3c3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1051,7 +1051,7 @@ bool GetTransaction(const uint256 &hash, CTransaction &txOut, uint256 &hashBlock CBlockHeader header; try { file >> header; - fseek(file, postx.nTxOffset, SEEK_CUR); + fseek(file.Get(), postx.nTxOffset, SEEK_CUR); file >> txOut; } catch (std::exception &e) { return error("%s : Deserialize or I/O error - %s", __func__, e.what()); @@ -1114,16 +1114,16 @@ bool WriteBlockToDisk(CBlock& block, CDiskBlockPos& pos) fileout << FLATDATA(Params().MessageStart()) << nSize; // Write block - long fileOutPos = ftell(fileout); + long fileOutPos = ftell(fileout.Get()); if (fileOutPos < 0) return error("WriteBlockToDisk : ftell failed"); pos.nPos = (unsigned int)fileOutPos; fileout << block; // Flush stdio buffers and commit to disk before returning - fflush(fileout); + fflush(fileout.Get()); if (!IsInitialBlockDownload()) - FileCommit(fileout); + FileCommit(fileout.Get()); return true; } @@ -2843,7 +2843,7 @@ bool static LoadBlockIndexDB() for (std::set::iterator it = setBlkDataFiles.begin(); it != setBlkDataFiles.end(); it++) { CDiskBlockPos pos(*it, 0); - if (!CAutoFile(OpenBlockFile(pos, true), SER_DISK, CLIENT_VERSION)) { + if (CAutoFile(OpenBlockFile(pos, true), SER_DISK, CLIENT_VERSION).IsNull()) { return false; } } @@ -4556,7 +4556,7 @@ bool CBlockUndo::WriteToDisk(CDiskBlockPos &pos, const uint256 &hashBlock) fileout << FLATDATA(Params().MessageStart()) << nSize; // Write undo data - long fileOutPos = ftell(fileout); + long fileOutPos = ftell(fileout.Get()); if (fileOutPos < 0) return error("CBlockUndo::WriteToDisk : ftell failed"); pos.nPos = (unsigned int)fileOutPos; @@ -4569,9 +4569,9 @@ bool CBlockUndo::WriteToDisk(CDiskBlockPos &pos, const uint256 &hashBlock) fileout << hasher.GetHash(); // Flush stdio buffers and commit to disk before returning - fflush(fileout); + fflush(fileout.Get()); if (!IsInitialBlockDownload()) - FileCommit(fileout); + FileCommit(fileout.Get()); return true; } diff --git a/src/net.cpp b/src/net.cpp index b7c958143c..6cf64f51c3 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -1939,7 +1939,7 @@ bool CAddrDB::Write(const CAddrMan& addr) catch (std::exception &e) { return error("%s : Serialize or I/O error - %s", __func__, e.what()); } - FileCommit(fileout); + FileCommit(fileout.Get()); fileout.fclose(); // replace existing peers.dat, if any, with new peers.dat.XXXX diff --git a/src/serialize.h b/src/serialize.h index d8f0b76d59..b9d5f95463 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -1116,14 +1116,21 @@ public: } } + /** Get wrapped FILE* with transfer of ownership. + * @note This will invalidate the CAutoFile object, and makes it the responsibility of the caller + * of this function to clean up the returned FILE*. + */ FILE* release() { FILE* ret = file; file = NULL; return ret; } - operator FILE*() { return file; } - FILE* operator->() { return file; } - FILE& operator*() { return *file; } - FILE** operator&() { return &file; } - FILE* operator=(FILE* pnew) { return file = pnew; } - bool IsNull() const { return (file == NULL); } + /** Get wrapped FILE* without transfer of ownership. + * @note Ownership of the FILE* will remain with this class. Use this only if the scope of the + * CAutoFile outlives use of the passed pointer. + */ + FILE* Get() const { return file; } + + /** Return true if the wrapped FILE* is NULL, false otherwise. + */ + bool IsNull() const { return (file == NULL); } // // Stream subset diff --git a/src/test/checkblock_tests.cpp b/src/test/checkblock_tests.cpp index 67d40a45c7..9151fdc0c8 100644 --- a/src/test/checkblock_tests.cpp +++ b/src/test/checkblock_tests.cpp @@ -36,7 +36,7 @@ bool read_block(const std::string& filename, CBlock& block) fseek(fp, 8, SEEK_SET); // skip msgheader/size CAutoFile filein(fp, SER_DISK, CLIENT_VERSION); - if (!filein) return false; + if (filein.IsNull()) return false; filein >> block; -- cgit v1.2.3