Skip to content

Commit 6302f95

Browse files
committed
refactor: Add AutoFile::size
1 parent 3964da5 commit 6302f95

File tree

5 files changed

+24
-5
lines changed

5 files changed

+24
-5
lines changed

src/streams.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,17 @@ size_t DataStream::GetMemoryUsage() const noexcept
116116
{
117117
return sizeof(*this) + memusage::DynamicUsage(vch);
118118
}
119+
120+
int64_t AutoFile::size()
121+
{
122+
if (IsNull()) {
123+
throw std::ios_base::failure("AutoFile::size: file handle is nullptr");
124+
}
125+
// Temporarily save the current position
126+
int64_t current_pos = tell();
127+
seek(current_pos, SEEK_END);
128+
int64_t file_size = tell();
129+
// Restore the original position
130+
seek(current_pos, SEEK_SET);
131+
return file_size;
132+
}

src/streams.h

+3
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,9 @@ class AutoFile
439439
/** Find position within the file. Will throw if unknown. */
440440
int64_t tell();
441441

442+
/** Return the size of the file. Will throw if unknown. */
443+
int64_t size();
444+
442445
/** Wrapper around FileCommit(). */
443446
bool Commit();
444447

src/test/streams_tests.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ BOOST_AUTO_TEST_CASE(xor_file)
2727
BOOST_CHECK_EXCEPTION(xor_file << std::byte{}, std::ios_base::failure, HasReason{"AutoFile::write: file handle is nullpt"});
2828
BOOST_CHECK_EXCEPTION(xor_file >> std::byte{}, std::ios_base::failure, HasReason{"AutoFile::read: file handle is nullpt"});
2929
BOOST_CHECK_EXCEPTION(xor_file.ignore(1), std::ios_base::failure, HasReason{"AutoFile::ignore: file handle is nullpt"});
30+
BOOST_CHECK_EXCEPTION(xor_file.size(), std::ios_base::failure, HasReason{"AutoFile::size: file handle is nullptr"});
3031
}
3132
{
3233
#ifdef __MINGW64__
@@ -37,6 +38,7 @@ BOOST_AUTO_TEST_CASE(xor_file)
3738
#endif
3839
AutoFile xor_file{raw_file(mode), xor_pat};
3940
xor_file << test1 << test2;
41+
BOOST_CHECK_EQUAL(xor_file.size(), 14);
4042
}
4143
{
4244
// Read raw from disk
@@ -46,6 +48,7 @@ BOOST_AUTO_TEST_CASE(xor_file)
4648
BOOST_CHECK_EQUAL(HexStr(raw), "fc01fd03fd04fa");
4749
// Check that no padding exists
4850
BOOST_CHECK_EXCEPTION(non_xor_file.ignore(1), std::ios_base::failure, HasReason{"AutoFile::ignore: end of file"});
51+
BOOST_CHECK_EQUAL(non_xor_file.size(), 14);
4952
}
5053
{
5154
AutoFile xor_file{raw_file("rb"), xor_pat};
@@ -55,6 +58,7 @@ BOOST_AUTO_TEST_CASE(xor_file)
5558
BOOST_CHECK_EQUAL(HexStr(read2), HexStr(test2));
5659
// Check that eof was reached
5760
BOOST_CHECK_EXCEPTION(xor_file >> std::byte{}, std::ios_base::failure, HasReason{"AutoFile::read: end of file"});
61+
BOOST_CHECK_EQUAL(xor_file.size(), 14);
5862
}
5963
{
6064
AutoFile xor_file{raw_file("rb"), xor_pat};
@@ -66,6 +70,7 @@ BOOST_AUTO_TEST_CASE(xor_file)
6670
// Check that ignore and read fail now
6771
BOOST_CHECK_EXCEPTION(xor_file.ignore(1), std::ios_base::failure, HasReason{"AutoFile::ignore: end of file"});
6872
BOOST_CHECK_EXCEPTION(xor_file >> std::byte{}, std::ios_base::failure, HasReason{"AutoFile::read: end of file"});
73+
BOOST_CHECK_EQUAL(xor_file.size(), 14);
6974
}
7075
}
7176

src/util/asmap.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,8 @@ std::vector<bool> DecodeAsmap(fs::path path)
223223
return {};
224224
}
225225

226-
file.seek(0, SEEK_END);
227-
int length = file.tell();
226+
int64_t length{file.size()};
228227
LogInfo("Opened asmap file %s (%d bytes) from disk\n", fs::quoted(fs::PathToString(path)), length);
229-
file.seek(0, SEEK_SET);
230228

231229
std::vector<std::byte> buffer(length);
232230
file.read(buffer);

src/wallet/migrate.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -539,8 +539,7 @@ void BerkeleyRODatabase::Open()
539539
page_size = outer_meta.pagesize;
540540

541541
// Verify the size of the file is a multiple of the page size
542-
db_file.seek(0, SEEK_END);
543-
int64_t size = db_file.tell();
542+
int64_t size{db_file.size()};
544543

545544
// Since BDB stores everything in a page, the file size should be a multiple of the page size;
546545
// However, BDB doesn't actually check that this is the case, and enforcing this check results

0 commit comments

Comments
 (0)