refactor: Add AutoFile::size

This commit is contained in:
Fabian Jahr 2024-09-19 17:36:19 +02:00
parent ec0f75862e
commit b7af960eb8
No known key found for this signature in database
GPG Key ID: F13D1E9D890798CD
5 changed files with 24 additions and 6 deletions

View File

@ -57,6 +57,20 @@ int64_t AutoFile::tell()
return *m_position;
}
int64_t AutoFile::size()
{
if (IsNull()) {
throw std::ios_base::failure("AutoFile::size: file handle is nullptr");
}
// Temporarily save the current position
int64_t current_pos = tell();
seek(0, SEEK_END);
int64_t file_size = tell();
// Restore the original position
seek(current_pos, SEEK_SET);
return file_size;
}
void AutoFile::read(std::span<std::byte> dst)
{
if (detail_fread(dst) != dst.size()) {

View File

@ -435,6 +435,9 @@ public:
/** Find position within the file. Will throw if unknown. */
int64_t tell();
/** Return the size of the file. Will throw if unknown. */
int64_t size();
/** Wrapper around FileCommit(). */
bool Commit();

View File

@ -122,6 +122,7 @@ BOOST_AUTO_TEST_CASE(xor_file)
BOOST_CHECK_EXCEPTION(xor_file << std::byte{}, std::ios_base::failure, HasReason{"AutoFile::write: file handle is nullptr"});
BOOST_CHECK_EXCEPTION(xor_file >> std::byte{}, std::ios_base::failure, HasReason{"AutoFile::read: file handle is nullptr"});
BOOST_CHECK_EXCEPTION(xor_file.ignore(1), std::ios_base::failure, HasReason{"AutoFile::ignore: file handle is nullptr"});
BOOST_CHECK_EXCEPTION(xor_file.size(), std::ios_base::failure, HasReason{"AutoFile::size: file handle is nullptr"});
}
{
#ifdef __MINGW64__
@ -132,6 +133,7 @@ BOOST_AUTO_TEST_CASE(xor_file)
#endif
AutoFile xor_file{raw_file(mode), obfuscation};
xor_file << test1 << test2;
BOOST_CHECK_EQUAL(xor_file.size(), 7);
BOOST_REQUIRE_EQUAL(xor_file.fclose(), 0);
}
{
@ -142,6 +144,7 @@ BOOST_AUTO_TEST_CASE(xor_file)
BOOST_CHECK_EQUAL(HexStr(raw), "fc01fd03fd04fa");
// Check that no padding exists
BOOST_CHECK_EXCEPTION(non_xor_file.ignore(1), std::ios_base::failure, HasReason{"AutoFile::ignore: end of file"});
BOOST_CHECK_EQUAL(non_xor_file.size(), 7);
}
{
AutoFile xor_file{raw_file("rb"), obfuscation};
@ -151,6 +154,7 @@ BOOST_AUTO_TEST_CASE(xor_file)
BOOST_CHECK_EQUAL(HexStr(read2), HexStr(test2));
// Check that eof was reached
BOOST_CHECK_EXCEPTION(xor_file >> std::byte{}, std::ios_base::failure, HasReason{"AutoFile::read: end of file"});
BOOST_CHECK_EQUAL(xor_file.size(), 7);
}
{
AutoFile xor_file{raw_file("rb"), obfuscation};
@ -162,6 +166,7 @@ BOOST_AUTO_TEST_CASE(xor_file)
// Check that ignore and read fail now
BOOST_CHECK_EXCEPTION(xor_file.ignore(1), std::ios_base::failure, HasReason{"AutoFile::ignore: end of file"});
BOOST_CHECK_EXCEPTION(xor_file >> std::byte{}, std::ios_base::failure, HasReason{"AutoFile::read: end of file"});
BOOST_CHECK_EQUAL(xor_file.size(), 7);
}
}

View File

@ -203,10 +203,8 @@ std::vector<bool> DecodeAsmap(fs::path path)
LogWarning("Failed to open asmap file from disk");
return bits;
}
file.seek(0, SEEK_END);
int length = file.tell();
int64_t length{file.size()};
LogInfo("Opened asmap file %s (%d bytes) from disk", fs::quoted(fs::PathToString(path)), length);
file.seek(0, SEEK_SET);
uint8_t cur_byte;
for (int i = 0; i < length; ++i) {
file >> cur_byte;
@ -220,4 +218,3 @@ std::vector<bool> DecodeAsmap(fs::path path)
}
return bits;
}

View File

@ -544,8 +544,7 @@ void BerkeleyRODatabase::Open()
page_size = outer_meta.pagesize;
// Verify the size of the file is a multiple of the page size
db_file.seek(0, SEEK_END);
int64_t size = db_file.tell();
const int64_t size{db_file.size()};
// Since BDB stores everything in a page, the file size should be a multiple of the page size;
// However, BDB doesn't actually check that this is the case, and enforcing this check results