2021-07-11 09:22:30 +00:00
|
|
|
#include <IO/WriteBufferFromEncryptedFile.h>
|
|
|
|
|
|
|
|
#if USE_SSL
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
WriteBufferFromEncryptedFile::WriteBufferFromEncryptedFile(
|
2021-07-11 19:26:39 +00:00
|
|
|
size_t buffer_size_,
|
2021-07-11 09:22:30 +00:00
|
|
|
std::unique_ptr<WriteBufferFromFileBase> out_,
|
2021-07-11 19:26:39 +00:00
|
|
|
const String & key_,
|
2021-07-23 08:18:11 +00:00
|
|
|
const FileEncryption::Header & header_,
|
2021-07-11 19:26:39 +00:00
|
|
|
size_t old_file_size)
|
2021-11-10 22:58:56 +00:00
|
|
|
: WriteBufferDecorator<WriteBufferFromFileBase>(std::move(out_), buffer_size_, nullptr, 0)
|
2021-07-23 08:18:11 +00:00
|
|
|
, header(header_)
|
|
|
|
, flush_header(!old_file_size)
|
|
|
|
, encryptor(header.algorithm, key_, header.init_vector)
|
2021-07-11 09:22:30 +00:00
|
|
|
{
|
2021-07-11 19:26:39 +00:00
|
|
|
encryptor.setOffset(old_file_size);
|
2021-07-11 09:22:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
WriteBufferFromEncryptedFile::~WriteBufferFromEncryptedFile()
|
|
|
|
{
|
2021-11-11 17:27:23 +00:00
|
|
|
finalize();
|
2021-07-11 09:22:30 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 11:19:26 +00:00
|
|
|
void WriteBufferFromEncryptedFile::finalizeBefore()
|
2021-07-11 09:22:30 +00:00
|
|
|
{
|
|
|
|
/// If buffer has pending data - write it.
|
|
|
|
next();
|
2021-07-11 19:26:39 +00:00
|
|
|
|
|
|
|
/// Note that if there is no data to write an empty file will be written, even without the initialization vector
|
|
|
|
/// (see nextImpl(): it writes the initialization vector only if there is some data ready to write).
|
|
|
|
/// That's fine because DiskEncrypted allows files without initialization vectors when they're empty.
|
2021-07-11 09:22:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WriteBufferFromEncryptedFile::sync()
|
|
|
|
{
|
|
|
|
/// If buffer has pending data - write it.
|
|
|
|
next();
|
2021-07-11 19:26:39 +00:00
|
|
|
|
2021-07-11 09:22:30 +00:00
|
|
|
out->sync();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WriteBufferFromEncryptedFile::nextImpl()
|
|
|
|
{
|
|
|
|
if (!offset())
|
|
|
|
return;
|
|
|
|
|
2021-07-23 08:18:11 +00:00
|
|
|
if (flush_header)
|
2021-07-11 09:22:30 +00:00
|
|
|
{
|
2021-07-23 08:18:11 +00:00
|
|
|
header.write(*out);
|
|
|
|
flush_header = false;
|
2021-07-11 09:22:30 +00:00
|
|
|
}
|
|
|
|
|
2021-07-11 19:26:39 +00:00
|
|
|
encryptor.encrypt(working_buffer.begin(), offset(), *out);
|
2021-07-11 09:22:30 +00:00
|
|
|
}
|
2021-07-11 19:26:39 +00:00
|
|
|
|
2021-07-11 09:22:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|