Skip to content

Commit

Permalink
FileCommit() Related Update
Browse files Browse the repository at this point in the history
  • Loading branch information
ghostlander committed Aug 10, 2014
1 parent 1a8239e commit 9a906f1
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 52 deletions.
40 changes: 21 additions & 19 deletions src/db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -508,33 +508,35 @@ bool CAddrDB::Write(const CAddrMan& addr)
boost::filesystem::path pathTmp = GetDataDir() / tmpfn;
FILE *file = fopen(pathTmp.string().c_str(), "wb");
CAutoFile fileout = CAutoFile(file, SER_DISK, CLIENT_VERSION);
if (!fileout)
return error("CAddrman::Write() : open failed");
if(!fileout)
return(error("CAddrDB::Write() : fopen(%s) failed", pathTmp.string().c_str()));

// Write and commit header, data
try {
fileout << ssPeers;
}
catch (std::exception &e) {
return error("CAddrman::Write() : I/O error");
catch(std::exception &e) {
return(error("CAddrDB::Write() : I/O error"));
}
FileCommit(fileout);
fflush(fileout);
if(FileCommit(fileout))
return(error("CAddrDB::Write() : FileCommit() failed"));
fileout.fclose();

// replace existing peers.dat, if any, with new peers.dat.XXXX
if (!RenameOver(pathTmp, pathAddr))
return error("CAddrman::Write() : Rename-into-place failed");
if(!RenameOver(pathTmp, pathAddr))
return(error("CAddrDB::Write() : RenameOver() failed"));

return true;
return(true);
}

bool CAddrDB::Read(CAddrMan& addr)
{
// open input file, and associate with CAutoFile
FILE *file = fopen(pathAddr.string().c_str(), "rb");
CAutoFile filein = CAutoFile(file, SER_DISK, CLIENT_VERSION);
if (!filein)
return error("CAddrman::Read() : open failed");
if(!filein)
return(error("CAddrDB::Read() : fopen(%s) failed", pathAddr.string().c_str()));

// use file size to size memory buffer
int fileSize = GetFilesize(filein);
Expand All @@ -548,32 +550,32 @@ bool CAddrDB::Read(CAddrMan& addr)
filein.read((char *)&vchData[0], dataSize);
filein >> hashIn;
}
catch (std::exception &e) {
return error("CAddrman::Read() 2 : I/O error or stream data corrupted");
catch(std::exception &e) {
return(error("CAddrDB::Read() : I/O error"));
}
filein.fclose();

CDataStream ssPeers(vchData, SER_DISK, CLIENT_VERSION);

// verify stored checksum matches input data
uint256 hashTmp = Hash(ssPeers.begin(), ssPeers.end());
if (hashIn != hashTmp)
return error("CAddrman::Read() : checksum mismatch; data corrupted");
if(hashIn != hashTmp)
return(error("CAddrDB::Read() : checksum mismatch"));

// de-serialize address data
unsigned char pchMsgTmp[4];
try {
ssPeers >> FLATDATA(pchMsgTmp);
ssPeers >> addr;
}
catch (std::exception &e) {
return error("CAddrman::Read() : I/O error or stream data corrupted");
catch(std::exception &e) {
return(error("CAddrDB::Read() #2 : I/O error"));
}

// finally, verify the network matches ours
if (memcmp(pchMsgTmp, pchMessageStart, sizeof(pchMsgTmp)))
return error("CAddrman::Read() : invalid network magic number");
if(memcmp(pchMsgTmp, pchMessageStart, sizeof(pchMsgTmp)))
return(error("CAddrDB::Read() : invalid network magic number"));

return true;
return(true);
}

50 changes: 25 additions & 25 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -782,26 +782,28 @@ class CBlockUndo
{
// Open history file to append
CAutoFile fileout = CAutoFile(OpenUndoFile(pos), SER_DISK, CLIENT_VERSION);
if (!fileout)
return error("CBlockUndo::WriteToDisk() : OpenUndoFile failed");
if(!fileout)
return(error("CBlockUndo::WriteToDisk() : OpenUndoFile() failed"));

// Write index header
unsigned int nSize = fileout.GetSerializeSize(*this);
fileout << FLATDATA(pchMessageStart) << nSize;

// Write undo data
long fileOutPos = ftell(fileout);
if (fileOutPos < 0)
return error("CBlockUndo::WriteToDisk() : ftell failed");
if(fileOutPos < 0)
return(error("CBlockUndo::WriteToDisk() : ftell() failed"));
pos.nPos = (unsigned int)fileOutPos;
fileout << *this;

// Flush stdio buffers and commit to disk before returning
fflush(fileout);
if (!IsInitialBlockDownload())
FileCommit(fileout);
if(!IsInitialBlockDownload()) {
if(FileCommit(fileout))
return(error("CBlockUndo::WriteToDisk() : FileCommit() failed"));
}

return true;
return(true);
}

};
Expand Down Expand Up @@ -1406,26 +1408,28 @@ class CBlock
{
// Open history file to append
CAutoFile fileout = CAutoFile(OpenBlockFile(pos), SER_DISK, CLIENT_VERSION);
if (!fileout)
return error("CBlock::WriteToDisk() : OpenBlockFile failed");
if(!fileout)
return(error("CBlock::WriteToDisk() : OpenBlockFile() failed"));

// Write index header
unsigned int nSize = fileout.GetSerializeSize(*this);
fileout << FLATDATA(pchMessageStart) << nSize;

// Write block
long fileOutPos = ftell(fileout);
if (fileOutPos < 0)
return error("CBlock::WriteToDisk() : ftell failed");
if(fileOutPos < 0)
return(error("CBlock::WriteToDisk() : ftell() failed"));
pos.nPos = (unsigned int)fileOutPos;
fileout << *this;

// Flush stdio buffers and commit to disk before returning
fflush(fileout);
if (!IsInitialBlockDownload())
FileCommit(fileout);
if(!IsInitialBlockDownload() || !((nBestHeight + 1) % 100)) {
if(FileCommit(fileout))
return(error("CBlock::WriteToDisk() : FileCommit() failed"));
}

return true;
return(true);
}

bool ReadFromDisk(const CDiskBlockPos &pos, bool fReadTransactions = true)
Expand All @@ -1434,24 +1438,20 @@ class CBlock

// Open history file to read
CAutoFile filein = CAutoFile(OpenBlockFile(pos, true), SER_DISK, CLIENT_VERSION);
if (!filein)
return error("CBlock::ReadFromDisk() : OpenBlockFile failed");
if (!fReadTransactions)
filein.nType |= SER_BLOCKHEADERONLY;
if(!filein)
return(error("CBlock::ReadFromDisk() : OpenBlockFile() failed"));
if(!fReadTransactions)
filein.nType |= SER_BLOCKHEADERONLY;

// Read block
try {
filein >> *this;
}
catch (std::exception &e) {
return error("%s() : deserialize or I/O error", __PRETTY_FUNCTION__);
catch(std::exception &e) {
return(error("CBlock::ReadFromDisk() : I/O error"));
}

// Check the header
if (fReadTransactions && IsProofOfWork() && !CheckProofOfWork(GetHash(), nBits))
return error("CBlock::ReadFromDisk() : errors in block header");

return true;
return(true);
}

void print() const
Expand Down
30 changes: 23 additions & 7 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ namespace boost {
#include <io.h> /* for _commit */
#include "shlobj.h"
#elif defined(__linux__)
# include <sys/prctl.h>
#include <sys/prctl.h>
#endif

#ifndef WIN32
Expand Down Expand Up @@ -1141,14 +1141,30 @@ bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest)
#endif /* WIN32 */
}

void FileCommit(FILE *fileout)
{
fflush(fileout); // harmless if redundantly called
#ifdef WIN32
_commit(_fileno(fileout));
/* Returns zero on success and -1 on failure */
int FileCommit(FILE *fileout) {
int ret, fd;

/* fflush() is a caller's responsibility */

/* Get a file descriptor and perform the synchronisation */
#if (WIN32)
fd = _fileno(fileout);
ret = _commit(fd);
#else
fd = fileno(fileout);
#if (__linux__)
ret = fdatasync(fd);
#elif (__APPLE__) && (F_FULLFSYNC)
/* F_FULLFSYNC means fsync with device flush to medium;
* works with HFS only as of 10.4, so fail over to fsync */
ret = fcntl(fd, F_FULLFSYNC, 0);
if(!ret) ret = fsync(fd);
#else
fsync(fileno(fileout));
ret = fsync(fd);
#endif
#endif /* WIN32 */
return(ret);
}

int GetFilesize(FILE* file)
Expand Down
2 changes: 1 addition & 1 deletion src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ std::string EncodeBase32(const std::string& str);
void ParseParameters(int argc, const char*const argv[]);
bool WildcardMatch(const char* psz, const char* mask);
bool WildcardMatch(const std::string& str, const std::string& mask);
void FileCommit(FILE *fileout);
int FileCommit(FILE *fileout);
int GetFilesize(FILE* file);
void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length);
bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest);
Expand Down

0 comments on commit 9a906f1

Please sign in to comment.