Skip to content

Commit

Permalink
add FILE* argument back to CBufferedFile ctor
Browse files Browse the repository at this point in the history
  • Loading branch information
LarryRuane committed Oct 30, 2019
1 parent dd802ab commit 856be70
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 16 deletions.
8 changes: 3 additions & 5 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ static void ThreadImport(std::vector<fs::path> vImportFiles)

// -reindex
if (fReindex) {
CBufferedFile blkdat(8*MAX_BLOCK_SERIALIZED_SIZE, 6*MAX_BLOCK_SERIALIZED_SIZE, SER_DISK, CLIENT_VERSION);
CBufferedFile blkdat(nullptr, 8*MAX_BLOCK_SERIALIZED_SIZE, 6*MAX_BLOCK_SERIALIZED_SIZE, SER_DISK, CLIENT_VERSION);
int nFile = 0;
while (true) {
FlatFilePos pos(nFile, 0);
Expand All @@ -689,12 +689,11 @@ static void ThreadImport(std::vector<fs::path> vImportFiles)
// hardcoded $DATADIR/bootstrap.dat
fs::path pathBootstrap = GetDataDir() / "bootstrap.dat";
if (fs::exists(pathBootstrap)) {
CBufferedFile blkdat(2*MAX_BLOCK_SERIALIZED_SIZE, MAX_BLOCK_SERIALIZED_SIZE+8, SER_DISK, CLIENT_VERSION);
FILE *file = fsbridge::fopen(pathBootstrap, "rb");
if (file) {
CBufferedFile blkdat(file, 2*MAX_BLOCK_SERIALIZED_SIZE, MAX_BLOCK_SERIALIZED_SIZE+8, SER_DISK, CLIENT_VERSION);
fs::path pathBootstrapOld = GetDataDir() / "bootstrap.dat.old";
LogPrintf("Importing bootstrap.dat...\n");
blkdat.SetFile(file);
LoadExternalBlockFile(chainparams, blkdat);
RenameOver(pathBootstrap, pathBootstrapOld);
} else {
Expand All @@ -704,11 +703,10 @@ static void ThreadImport(std::vector<fs::path> vImportFiles)

// -loadblock=
for (const fs::path& path : vImportFiles) {
CBufferedFile blkdat(2*MAX_BLOCK_SERIALIZED_SIZE, MAX_BLOCK_SERIALIZED_SIZE+8, SER_DISK, CLIENT_VERSION);
FILE *file = fsbridge::fopen(path, "rb");
if (file) {
CBufferedFile blkdat(file, 2*MAX_BLOCK_SERIALIZED_SIZE, MAX_BLOCK_SERIALIZED_SIZE+8, SER_DISK, CLIENT_VERSION);
LogPrintf("Importing blocks file %s...\n", path.string());
blkdat.SetFile(file);
LoadExternalBlockFile(chainparams, blkdat);
} else {
LogPrintf("Warning: Could not open blocks file %s\n", path.string());
Expand Down
6 changes: 3 additions & 3 deletions src/streams.h
Original file line number Diff line number Diff line change
Expand Up @@ -761,12 +761,12 @@ class CBufferedFile
}

public:
CBufferedFile(uint64_t nBufSize, uint64_t nRewindIn, int nTypeIn, int nVersionIn) :
nType(nTypeIn), nVersion(nVersionIn), src(nullptr), nSrcPos(0), nReadPos(0),
nReadLimit(std::numeric_limits<uint64_t>::max()), nRewind(nRewindIn), vchBuf(nBufSize)
CBufferedFile(FILE *fileIn, uint64_t nBufSize, uint64_t nRewindIn, int nTypeIn, int nVersionIn) :
nType(nTypeIn), nVersion(nVersionIn), nSrcPos(0), nReadPos(0), nReadLimit(std::numeric_limits<uint64_t>::max()), nRewind(nRewindIn), vchBuf(nBufSize, 0)
{
if (nRewindIn >= nBufSize)
throw std::ios_base::failure("Rewind limit must be less than buffer size");
src = fileIn;
}

~CBufferedFile()
Expand Down
13 changes: 5 additions & 8 deletions src/test/streams_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,16 +214,15 @@ BOOST_AUTO_TEST_CASE(streams_buffered_file)
// The buffer size (second arg) must be greater than the rewind
// amount (third arg).
try {
CBufferedFile bfbad(25, 25, 222, 333);
CBufferedFile bfbad(file, 25, 25, 222, 333);
BOOST_CHECK(false);
} catch (const std::exception& e) {
BOOST_CHECK(strstr(e.what(),
"Rewind limit must be less than buffer size") != nullptr);
}

// The buffer is 25 bytes, allow rewinding 10 bytes.
CBufferedFile bf(25, 10, 222, 333);
bf.SetFile(file);
CBufferedFile bf(file, 25, 10, 222, 333);
BOOST_CHECK(!bf.eof());

// These two members have no functional effect.
Expand Down Expand Up @@ -350,8 +349,7 @@ BOOST_AUTO_TEST_CASE(streams_buffered_file_skip)
rewind(file);

// The buffer is 25 bytes, allow rewinding 10 bytes.
CBufferedFile bf(25, 10, 222, 333);
bf.SetFile(file);
CBufferedFile bf(file, 25, 10, 222, 333);

uint8_t i;
// This is like bf >> (7-byte-variable), in that it will cause data
Expand Down Expand Up @@ -409,7 +407,7 @@ BOOST_AUTO_TEST_CASE(streams_buffered_file_multifile)
rewind(file2);

// The buffer is 25 bytes, allow rewinding 10 bytes.
CBufferedFile bf(25, 10, 222, 333);
CBufferedFile bf(nullptr, 25, 10, 222, 333);
bf.SetFile(file1);
BOOST_CHECK_EQUAL(bf.GetPos(), 0);
BOOST_CHECK_EQUAL(bf.GetFilePos(), 0);
Expand Down Expand Up @@ -483,8 +481,7 @@ BOOST_AUTO_TEST_CASE(streams_buffered_file_rand)

size_t bufSize = InsecureRandRange(300) + 1;
size_t rewindSize = InsecureRandRange(bufSize);
CBufferedFile bf(bufSize, rewindSize, 222, 333);
bf.SetFile(file);
CBufferedFile bf(file, bufSize, rewindSize, 222, 333);
size_t currentPos = 0;
size_t maxPos = 0;
for (int step = 0; step < 100; ++step) {
Expand Down

0 comments on commit 856be70

Please sign in to comment.