Browse Source

LoadExternalBlockFile switched to CBufferedFile

0.8
Pieter Wuille 12 years ago
parent
commit
05d9726805
  1. 65
      src/main.cpp

65
src/main.cpp

@ -2493,55 +2493,40 @@ bool LoadExternalBlockFile(FILE* fileIn)
int nLoaded = 0; int nLoaded = 0;
{ {
try { CBufferedFile blkdat(fileIn, 2*MAX_BLOCK_SIZE, MAX_BLOCK_SIZE+8, SER_DISK, CLIENT_VERSION);
CAutoFile blkdat(fileIn, SER_DISK, CLIENT_VERSION); uint64 nRewind = blkdat.GetPos();
unsigned int nPos = 0; while (blkdat.good() && !blkdat.eof() && !fShutdown) {
while (nPos != (unsigned int)-1 && blkdat.good() && !fRequestShutdown) blkdat.SetPos(nRewind);
{ nRewind++; // start one byte further next time, in case of failure
unsigned char pchData[65536]; blkdat.SetLimit(); // remove former limit
do { try {
fseek(blkdat, nPos, SEEK_SET); // locate a header
int nRead = fread(pchData, 1, sizeof(pchData), blkdat); unsigned char buf[4];
if (nRead <= 8) blkdat.FindByte(pchMessageStart[0]);
{ nRewind = blkdat.GetPos()+1;
nPos = (unsigned int)-1; blkdat >> FLATDATA(buf);
break; if (memcmp(buf, pchMessageStart, 4))
} continue;
void* nFind = memchr(pchData, pchMessageStart[0], nRead+1-sizeof(pchMessageStart)); // read size
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;
blkdat >> block;
nRewind = blkdat.GetPos();
{ {
CBlock block;
blkdat >> block;
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__);
} }
} }
catch (std::exception &e) { fclose(fileIn);
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;

Loading…
Cancel
Save