aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/IO/WriteBufferFromEncryptedFile.h
blob: 12c1ba5f6f3feb202f2d40de2dea677f6f5021e4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#pragma once

#include "clickhouse_config.h"
#include <Common/assert_cast.h>

#if USE_SSL
#include <IO/WriteBufferFromFileBase.h>
#include <IO/FileEncryptionCommon.h>
#include <IO/WriteBufferDecorator.h>


namespace DB
{

/// Encrypts data and writes the encrypted data to the underlying write buffer.
class WriteBufferFromEncryptedFile : public WriteBufferDecorator<WriteBufferFromFileBase>
{
public:
    /// `old_file_size` should be set to non-zero if we're going to append an existing file.
    WriteBufferFromEncryptedFile(
        size_t buffer_size_,
        std::unique_ptr<WriteBufferFromFileBase> out_,
        const String & key_,
        const FileEncryption::Header & header_,
        size_t old_file_size = 0);

    ~WriteBufferFromEncryptedFile() override;

    void sync() override;

    std::string getFileName() const override { return assert_cast<WriteBufferFromFileBase *>(out.get())->getFileName(); }

private:
    void nextImpl() override;

    void finalizeBefore() override;

    FileEncryption::Header header;
    bool flush_header = false;

    FileEncryption::Encryptor encryptor;
};

}

#endif