Merge pull request #45418 from CurtizJ/fix-disk-encrypted

Fix reading from encrypted disk with passed file size
This commit is contained in:
Anton Popov 2023-01-19 16:11:08 +01:00 committed by GitHub
commit 4ca359d57b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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");