LoadExternalBlockFile switched to CBufferedFile

This commit is contained in:
Pieter Wuille 2012-10-27 22:01:38 +02:00
parent b5d5f44c95
commit 05d9726805

View File

@ -2493,55 +2493,40 @@ bool LoadExternalBlockFile(FILE* fileIn)
int nLoaded = 0; int nLoaded = 0;
{ {
CBufferedFile blkdat(fileIn, 2*MAX_BLOCK_SIZE, MAX_BLOCK_SIZE+8, SER_DISK, CLIENT_VERSION);
uint64 nRewind = blkdat.GetPos();
while (blkdat.good() && !blkdat.eof() && !fShutdown) {
blkdat.SetPos(nRewind);
nRewind++; // start one byte further next time, in case of failure
blkdat.SetLimit(); // remove former limit
try { try {
CAutoFile blkdat(fileIn, SER_DISK, CLIENT_VERSION); // locate a header
unsigned int nPos = 0; unsigned char buf[4];
while (nPos != (unsigned int)-1 && blkdat.good() && !fRequestShutdown) blkdat.FindByte(pchMessageStart[0]);
{ nRewind = blkdat.GetPos()+1;
unsigned char pchData[65536]; blkdat >> FLATDATA(buf);
do { if (memcmp(buf, pchMessageStart, 4))
fseek(blkdat, nPos, SEEK_SET); continue;
int nRead = fread(pchData, 1, sizeof(pchData), blkdat); // read size
if (nRead <= 8)
{
nPos = (unsigned int)-1;
break;
}
void* nFind = memchr(pchData, pchMessageStart[0], nRead+1-sizeof(pchMessageStart));
if (nFind)
{
if (memcmp(nFind, pchMessageStart, sizeof(pchMessageStart))==0)
{
nPos += ((unsigned char*)nFind - pchData) + sizeof(pchMessageStart);
break;
}
nPos += ((unsigned char*)nFind - pchData) + 1;
}
else
nPos += sizeof(pchData) - sizeof(pchMessageStart) + 1;
} while(!fRequestShutdown);
if (nPos == (unsigned int)-1)
break;
fseek(blkdat, nPos, SEEK_SET);
unsigned int nSize; unsigned int nSize;
blkdat >> nSize; blkdat >> nSize;
if (nSize > 0 && nSize <= MAX_BLOCK_SIZE) if (nSize < 80 || nSize > MAX_BLOCK_SIZE)
{ continue;
// read block
blkdat.SetLimit(blkdat.GetPos() + nSize);
CBlock block; CBlock block;
blkdat >> block; blkdat >> block;
nRewind = blkdat.GetPos();
{
LOCK(cs_main); LOCK(cs_main);
if (ProcessBlock(NULL,&block)) if (ProcessBlock(NULL,&block))
{
nLoaded++; nLoaded++;
nPos += 4 + nSize; }
} catch (std::exception &e) {
printf("%s() : Deserialize or I/O error caught during load\n", __PRETTY_FUNCTION__);
} }
} }
} fclose(fileIn);
}
catch (std::exception &e) {
printf("%s() : Deserialize or I/O error caught during load\n",
__PRETTY_FUNCTION__);
}
} }
printf("Loaded %i blocks from external file in %"PRI64d"ms\n", nLoaded, GetTimeMillis() - nStart); printf("Loaded %i blocks from external file in %"PRI64d"ms\n", nLoaded, GetTimeMillis() - nStart);
return nLoaded > 0; return nLoaded > 0;