aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPieter Wuille <pieter.wuille@gmail.com>2012-10-27 22:01:38 +0200
committerPieter Wuille <pieter.wuille@gmail.com>2012-11-09 01:06:06 +0100
commit05d9726805969ff155e8a029579113f05529ea82 (patch)
tree15ad2a43d50a9d15ec86ae3e40a323f361f82756 /src
parentb5d5f44c95b1010bd6fa53d0a87abd4d6389df8b (diff)
downloadbitcoin-05d9726805969ff155e8a029579113f05529ea82.tar.xz
LoadExternalBlockFile switched to CBufferedFile
Diffstat (limited to 'src')
-rw-r--r--src/main.cpp65
1 files changed, 25 insertions, 40 deletions
diff --git a/src/main.cpp b/src/main.cpp
index a08e393caa..7a9a32a626 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -2493,55 +2493,40 @@ bool LoadExternalBlockFile(FILE* fileIn)
int nLoaded = 0;
{
- try {
- CAutoFile blkdat(fileIn, SER_DISK, CLIENT_VERSION);
- unsigned int nPos = 0;
- while (nPos != (unsigned int)-1 && blkdat.good() && !fRequestShutdown)
- {
- unsigned char pchData[65536];
- do {
- fseek(blkdat, nPos, SEEK_SET);
- int nRead = fread(pchData, 1, sizeof(pchData), blkdat);
- 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);
+ 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 {
+ // locate a header
+ unsigned char buf[4];
+ blkdat.FindByte(pchMessageStart[0]);
+ nRewind = blkdat.GetPos()+1;
+ blkdat >> FLATDATA(buf);
+ if (memcmp(buf, pchMessageStart, 4))
+ continue;
+ // read size
unsigned int 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);
if (ProcessBlock(NULL,&block))
- {
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) {
- printf("%s() : Deserialize or I/O error caught during load\n",
- __PRETTY_FUNCTION__);
- }
+ fclose(fileIn);
}
printf("Loaded %i blocks from external file in %"PRI64d"ms\n", nLoaded, GetTimeMillis() - nStart);
return nLoaded > 0;