Merge pull request #5108

a873823 CAutoFile: Explicit Get() and remove unused methods (Wladimir J. van der Laan)
fef24ca Add IsNull() to class CAutoFile and remove operator ! (Ruben Dario Ponticeli)
This commit is contained in:
Wladimir J. van der Laan 2014-10-22 10:46:24 +02:00
commit f984c7d7ad
No known key found for this signature in database
GPG Key ID: 74810B012346C9A6
5 changed files with 31 additions and 24 deletions

View File

@ -139,7 +139,7 @@ void Shutdown()
{ {
boost::filesystem::path est_path = GetDataDir() / FEE_ESTIMATES_FILENAME; boost::filesystem::path est_path = GetDataDir() / FEE_ESTIMATES_FILENAME;
CAutoFile est_fileout(fopen(est_path.string().c_str(), "wb"), SER_DISK, CLIENT_VERSION); 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); mempool.WriteFeeEstimates(est_fileout);
else else
LogPrintf("%s: Failed to write fee estimates to %s\n", __func__, est_path.string()); 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; boost::filesystem::path est_path = GetDataDir() / FEE_ESTIMATES_FILENAME;
CAutoFile est_filein(fopen(est_path.string().c_str(), "rb"), SER_DISK, CLIENT_VERSION); 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. // Allowed to fail as this file IS missing on first startup.
if (est_filein) if (!est_filein.IsNull())
mempool.ReadFeeEstimates(est_filein); mempool.ReadFeeEstimates(est_filein);
fFeeEstimatesInitialized = true; fFeeEstimatesInitialized = true;

View File

@ -1051,7 +1051,7 @@ bool GetTransaction(const uint256 &hash, CTransaction &txOut, uint256 &hashBlock
CBlockHeader header; CBlockHeader header;
try { try {
file >> header; file >> header;
fseek(file, postx.nTxOffset, SEEK_CUR); fseek(file.Get(), postx.nTxOffset, SEEK_CUR);
file >> txOut; file >> txOut;
} catch (std::exception &e) { } catch (std::exception &e) {
return error("%s : Deserialize or I/O error - %s", __func__, e.what()); return error("%s : Deserialize or I/O error - %s", __func__, e.what());
@ -1106,7 +1106,7 @@ bool WriteBlockToDisk(CBlock& block, CDiskBlockPos& pos)
{ {
// Open history file to append // Open history file to append
CAutoFile fileout(OpenBlockFile(pos), SER_DISK, CLIENT_VERSION); CAutoFile fileout(OpenBlockFile(pos), SER_DISK, CLIENT_VERSION);
if (!fileout) if (fileout.IsNull())
return error("WriteBlockToDisk : OpenBlockFile failed"); return error("WriteBlockToDisk : OpenBlockFile failed");
// Write index header // Write index header
@ -1114,16 +1114,16 @@ bool WriteBlockToDisk(CBlock& block, CDiskBlockPos& pos)
fileout << FLATDATA(Params().MessageStart()) << nSize; fileout << FLATDATA(Params().MessageStart()) << nSize;
// Write block // Write block
long fileOutPos = ftell(fileout); long fileOutPos = ftell(fileout.Get());
if (fileOutPos < 0) if (fileOutPos < 0)
return error("WriteBlockToDisk : ftell failed"); return error("WriteBlockToDisk : ftell failed");
pos.nPos = (unsigned int)fileOutPos; pos.nPos = (unsigned int)fileOutPos;
fileout << block; fileout << block;
// Flush stdio buffers and commit to disk before returning // Flush stdio buffers and commit to disk before returning
fflush(fileout); fflush(fileout.Get());
if (!IsInitialBlockDownload()) if (!IsInitialBlockDownload())
FileCommit(fileout); FileCommit(fileout.Get());
return true; return true;
} }
@ -1134,7 +1134,7 @@ bool ReadBlockFromDisk(CBlock& block, const CDiskBlockPos& pos)
// Open history file to read // Open history file to read
CAutoFile filein(OpenBlockFile(pos, true), SER_DISK, CLIENT_VERSION); CAutoFile filein(OpenBlockFile(pos, true), SER_DISK, CLIENT_VERSION);
if (!filein) if (filein.IsNull())
return error("ReadBlockFromDisk : OpenBlockFile failed"); return error("ReadBlockFromDisk : OpenBlockFile failed");
// Read block // Read block
@ -2843,7 +2843,7 @@ bool static LoadBlockIndexDB()
for (std::set<int>::iterator it = setBlkDataFiles.begin(); it != setBlkDataFiles.end(); it++) for (std::set<int>::iterator it = setBlkDataFiles.begin(); it != setBlkDataFiles.end(); it++)
{ {
CDiskBlockPos pos(*it, 0); 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; return false;
} }
} }
@ -4548,7 +4548,7 @@ bool CBlockUndo::WriteToDisk(CDiskBlockPos &pos, const uint256 &hashBlock)
{ {
// Open history file to append // Open history file to append
CAutoFile fileout(OpenUndoFile(pos), SER_DISK, CLIENT_VERSION); CAutoFile fileout(OpenUndoFile(pos), SER_DISK, CLIENT_VERSION);
if (!fileout) if (fileout.IsNull())
return error("CBlockUndo::WriteToDisk : OpenUndoFile failed"); return error("CBlockUndo::WriteToDisk : OpenUndoFile failed");
// Write index header // Write index header
@ -4556,7 +4556,7 @@ bool CBlockUndo::WriteToDisk(CDiskBlockPos &pos, const uint256 &hashBlock)
fileout << FLATDATA(Params().MessageStart()) << nSize; fileout << FLATDATA(Params().MessageStart()) << nSize;
// Write undo data // Write undo data
long fileOutPos = ftell(fileout); long fileOutPos = ftell(fileout.Get());
if (fileOutPos < 0) if (fileOutPos < 0)
return error("CBlockUndo::WriteToDisk : ftell failed"); return error("CBlockUndo::WriteToDisk : ftell failed");
pos.nPos = (unsigned int)fileOutPos; pos.nPos = (unsigned int)fileOutPos;
@ -4569,9 +4569,9 @@ bool CBlockUndo::WriteToDisk(CDiskBlockPos &pos, const uint256 &hashBlock)
fileout << hasher.GetHash(); fileout << hasher.GetHash();
// Flush stdio buffers and commit to disk before returning // Flush stdio buffers and commit to disk before returning
fflush(fileout); fflush(fileout.Get());
if (!IsInitialBlockDownload()) if (!IsInitialBlockDownload())
FileCommit(fileout); FileCommit(fileout.Get());
return true; return true;
} }
@ -4580,7 +4580,7 @@ bool CBlockUndo::ReadFromDisk(const CDiskBlockPos &pos, const uint256 &hashBlock
{ {
// Open history file to read // Open history file to read
CAutoFile filein(OpenUndoFile(pos, true), SER_DISK, CLIENT_VERSION); CAutoFile filein(OpenUndoFile(pos, true), SER_DISK, CLIENT_VERSION);
if (!filein) if (filein.IsNull())
return error("CBlockUndo::ReadFromDisk : OpenBlockFile failed"); return error("CBlockUndo::ReadFromDisk : OpenBlockFile failed");
// Read block // Read block

View File

@ -1929,7 +1929,7 @@ bool CAddrDB::Write(const CAddrMan& addr)
boost::filesystem::path pathTmp = GetDataDir() / tmpfn; boost::filesystem::path pathTmp = GetDataDir() / tmpfn;
FILE *file = fopen(pathTmp.string().c_str(), "wb"); FILE *file = fopen(pathTmp.string().c_str(), "wb");
CAutoFile fileout(file, SER_DISK, CLIENT_VERSION); CAutoFile fileout(file, SER_DISK, CLIENT_VERSION);
if (!fileout) 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());
// Write and commit header, data // Write and commit header, data
@ -1939,7 +1939,7 @@ bool CAddrDB::Write(const CAddrMan& addr)
catch (std::exception &e) { catch (std::exception &e) {
return error("%s : Serialize or I/O error - %s", __func__, e.what()); return error("%s : Serialize or I/O error - %s", __func__, e.what());
} }
FileCommit(fileout); FileCommit(fileout.Get());
fileout.fclose(); fileout.fclose();
// replace existing peers.dat, if any, with new peers.dat.XXXX // replace existing peers.dat, if any, with new peers.dat.XXXX
@ -1954,7 +1954,7 @@ 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 = fopen(pathAddr.string().c_str(), "rb");
CAutoFile filein(file, SER_DISK, CLIENT_VERSION); CAutoFile filein(file, SER_DISK, CLIENT_VERSION);
if (!filein) 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());
// use file size to size memory buffer // use file size to size memory buffer

View File

@ -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; } 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 operator!() { 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 // Stream subset

View File

@ -36,7 +36,7 @@ bool read_block(const std::string& filename, CBlock& block)
fseek(fp, 8, SEEK_SET); // skip msgheader/size fseek(fp, 8, SEEK_SET); // skip msgheader/size
CAutoFile filein(fp, SER_DISK, CLIENT_VERSION); CAutoFile filein(fp, SER_DISK, CLIENT_VERSION);
if (!filein) return false; if (filein.IsNull()) return false;
filein >> block; filein >> block;