fix reading from encrypted disk

This commit is contained in:
Anton Popov 2023-01-18 19:02:20 +00:00
parent 821c55463f
commit 65a71b4431
2 changed files with 12 additions and 2 deletions

View File

@ -289,6 +289,12 @@ std::unique_ptr<ReadBufferFromFileBase> DiskEncrypted::readFile(
std::optional<size_t> read_hint,
std::optional<size_t> file_size) const
{
if (read_hint && *read_hint > 0)
read_hint = *read_hint + FileEncryption::Header::kSize;
if (file_size && *file_size > 0)
file_size = *file_size + FileEncryption::Header::kSize;
auto wrapped_path = wrappedPath(path);
auto buffer = delegate->readFile(wrapped_path, settings, read_hint, file_size);
if (buffer->eof())

View File

@ -55,9 +55,9 @@ protected:
return temp_dir->path() + "/";
}
String getFileContents(const String & file_name)
String getFileContents(const String & file_name, std::optional<size_t> file_size = {})
{
auto buf = encrypted_disk->readFile(file_name, /* settings= */ {}, /* read_hint= */ {}, /* file_size= */ {});
auto buf = encrypted_disk->readFile(file_name, /* settings= */ {}, /* read_hint= */ {}, file_size);
String str;
readStringUntilEOF(str, *buf);
return str;
@ -108,6 +108,10 @@ TEST_F(DiskEncryptedTest, WriteAndRead)
EXPECT_EQ(getFileContents("a.txt"), "Some text");
checkBinaryRepresentation(getDirectory() + "a.txt", kHeaderSize + 9);
/// Read the file with specified file size.
EXPECT_EQ(getFileContents("a.txt", 9), "Some text");
checkBinaryRepresentation(getDirectory() + "a.txt", kHeaderSize + 9);
/// Remove the file.
encrypted_disk->removeFile("a.txt");