aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/IO/Archives/ZipArchiveWriter.cpp
blob: 4f8aa27df687c2e5101fb33b47f0d971d87200f2 (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#include <IO/Archives/ZipArchiveWriter.h>

#if USE_MINIZIP
#include <IO/WriteBufferFromFileBase.h>
#include <Common/quoteString.h>
#include <base/errnoToString.h>
#error #include <zip.h>
#include <boost/algorithm/string/predicate.hpp>


namespace DB
{
namespace ErrorCodes
{
    extern const int CANNOT_PACK_ARCHIVE;
    extern const int SUPPORT_IS_DISABLED;
    extern const int LOGICAL_ERROR;
}

using RawHandle = zipFile;


/// Holds a raw handle, calls acquireRawHandle() in the constructor and releaseRawHandle() in the destructor.
class ZipArchiveWriter::HandleHolder
{
public:
    HandleHolder() = default;

    explicit HandleHolder(const std::shared_ptr<ZipArchiveWriter> & writer_) : writer(writer_), raw_handle(writer->acquireRawHandle()) { }

    ~HandleHolder()
    {
        if (raw_handle)
        {
            try
            {
                int err = zipCloseFileInZip(raw_handle);
                /// If err == ZIP_PARAMERROR the file is already closed.
                if (err != ZIP_PARAMERROR)
                    checkResult(err);
            }
            catch (...)
            {
                tryLogCurrentException("ZipArchiveWriter");
            }
            writer->releaseRawHandle(raw_handle);
        }
    }

    HandleHolder(HandleHolder && src) noexcept
    {
        *this = std::move(src);
    }

    HandleHolder & operator=(HandleHolder && src) noexcept
    {
        writer = std::exchange(src.writer, nullptr);
        raw_handle = std::exchange(src.raw_handle, nullptr);
        return *this;
    }

    RawHandle getRawHandle() const { return raw_handle; }
    std::shared_ptr<ZipArchiveWriter> getWriter() const { return writer; }

    void checkResult(int code) const { writer->checkResult(code); }

private:
    std::shared_ptr<ZipArchiveWriter> writer;
    RawHandle raw_handle = nullptr;
};


/// This class represents a WriteBuffer actually returned by writeFile().
class ZipArchiveWriter::WriteBufferFromZipArchive : public WriteBufferFromFileBase
{
public:
    WriteBufferFromZipArchive(HandleHolder && handle_, const String & filename_)
        : WriteBufferFromFileBase(DBMS_DEFAULT_BUFFER_SIZE, nullptr, 0)
        , handle(std::move(handle_))
        , filename(filename_)
    {
        auto compress_method = handle.getWriter()->compression_method;
        auto compress_level = handle.getWriter()->compression_level;
        checkCompressionMethodIsEnabled(compress_method);

        const char * password_cstr = nullptr;
        const String & password_str = handle.getWriter()->password;
        if (!password_str.empty())
        {
            checkEncryptionIsEnabled();
            password_cstr = password_str.c_str();
        }

        RawHandle raw_handle = handle.getRawHandle();

        checkResult(zipOpenNewFileInZip3_64(
            raw_handle,
            filename_.c_str(),
            /* zipfi= */ nullptr,
            /* extrafield_local= */ nullptr,
            /* size_extrafield_local= */ 0,
            /* extrafield_global= */ nullptr,
            /* size_extrafield_global= */ 0,
            /* comment= */ nullptr,
            compress_method,
            compress_level,
            /* raw= */ false,
            /* windowBits= */ 0,
            /* memLevel= */ 0,
            /* strategy= */ 0,
            password_cstr,
            /* crc_for_crypting= */ 0,
            /* zip64= */ true));
    }

    ~WriteBufferFromZipArchive() override
    {
        try
        {
            finalize();
        }
        catch (...)
        {
            tryLogCurrentException("ZipArchiveWriter");
        }
    }

    void sync() override { next(); }
    std::string getFileName() const override { return filename; }

private:
    void nextImpl() override
    {
        if (!offset())
            return;
        RawHandle raw_handle = handle.getRawHandle();
        int code = zipWriteInFileInZip(raw_handle, working_buffer.begin(), static_cast<uint32_t>(offset()));
        checkResult(code);
    }

    void checkResult(int code) const { handle.checkResult(code); }

    HandleHolder handle;
    String filename;
};


namespace
{
    /// Provides a set of functions allowing the minizip library to write its output
    /// to a WriteBuffer instead of an ordinary file in the local filesystem.
    class StreamFromWriteBuffer
    {
    public:
        static RawHandle open(std::unique_ptr<WriteBuffer> archive_write_buffer)
        {
            Opaque opaque{std::move(archive_write_buffer)};

            zlib_filefunc64_def func_def;
            func_def.zopen64_file = &StreamFromWriteBuffer::openFileFunc;
            func_def.zclose_file = &StreamFromWriteBuffer::closeFileFunc;
            func_def.zread_file = &StreamFromWriteBuffer::readFileFunc;
            func_def.zwrite_file = &StreamFromWriteBuffer::writeFileFunc;
            func_def.zseek64_file = &StreamFromWriteBuffer::seekFunc;
            func_def.ztell64_file = &StreamFromWriteBuffer::tellFunc;
            func_def.zerror_file = &StreamFromWriteBuffer::testErrorFunc;
            func_def.opaque = &opaque;

            return zipOpen2_64(
                /* path= */ nullptr,
                /* append= */ false,
                /* globalcomment= */ nullptr,
                &func_def);
        }

    private:
        std::unique_ptr<WriteBuffer> write_buffer;
        UInt64 start_offset = 0;

        struct Opaque
        {
            std::unique_ptr<WriteBuffer> write_buffer;
        };

        static void * openFileFunc(void * opaque, const void *, int)
        {
            Opaque & opq = *reinterpret_cast<Opaque *>(opaque);
            return new StreamFromWriteBuffer(std::move(opq.write_buffer));
        }

        explicit StreamFromWriteBuffer(std::unique_ptr<WriteBuffer> write_buffer_)
            : write_buffer(std::move(write_buffer_)), start_offset(write_buffer->count()) {}

        ~StreamFromWriteBuffer()
        {
            write_buffer->finalize();
        }

        static int closeFileFunc(void *, void * stream)
        {
            delete reinterpret_cast<StreamFromWriteBuffer *>(stream);
            return ZIP_OK;
        }

        static StreamFromWriteBuffer & get(void * ptr)
        {
            return *reinterpret_cast<StreamFromWriteBuffer *>(ptr);
        }

        static unsigned long writeFileFunc(void *, void * stream, const void * buf, unsigned long size) // NOLINT(google-runtime-int)
        {
            auto & strm = get(stream);
            strm.write_buffer->write(reinterpret_cast<const char *>(buf), size);
            return size;
        }

        static int testErrorFunc(void *, void *)
        {
            return ZIP_OK;
        }

        static ZPOS64_T tellFunc(void *, void * stream)
        {
            auto & strm = get(stream);
            auto pos = strm.write_buffer->count() - strm.start_offset;
            return pos;
        }

        static long seekFunc(void *, void *, ZPOS64_T, int) // NOLINT(google-runtime-int)
        {
            throw Exception(ErrorCodes::LOGICAL_ERROR, "StreamFromWriteBuffer::seek must not be called");
        }

        static unsigned long readFileFunc(void *, void *, void *, unsigned long) // NOLINT(google-runtime-int)
        {
            throw Exception(ErrorCodes::LOGICAL_ERROR, "StreamFromWriteBuffer::readFile must not be called");
        }
    };
}


ZipArchiveWriter::ZipArchiveWriter(const String & path_to_archive_)
    : ZipArchiveWriter(path_to_archive_, nullptr)
{
}

ZipArchiveWriter::ZipArchiveWriter(const String & path_to_archive_, std::unique_ptr<WriteBuffer> archive_write_buffer_)
    : path_to_archive(path_to_archive_), compression_method(MZ_COMPRESS_METHOD_DEFLATE)
{
    if (archive_write_buffer_)
        handle = StreamFromWriteBuffer::open(std::move(archive_write_buffer_));
    else
        handle = zipOpen64(path_to_archive.c_str(), /* append= */ false);
    if (!handle)
        throw Exception(ErrorCodes::CANNOT_PACK_ARCHIVE, "Couldn't create zip archive {}", quoteString(path_to_archive));

}

ZipArchiveWriter::~ZipArchiveWriter()
{
    if (handle)
    {
        try
        {
            checkResult(zipClose(handle, /* global_comment= */ nullptr));
        }
        catch (...)
        {
            tryLogCurrentException("ZipArchiveWriter");
        }
    }
}

std::unique_ptr<WriteBufferFromFileBase> ZipArchiveWriter::writeFile(const String & filename)
{
    return std::make_unique<WriteBufferFromZipArchive>(acquireHandle(), filename);
}

bool ZipArchiveWriter::isWritingFile() const
{
    std::lock_guard lock{mutex};
    return !handle;
}

void ZipArchiveWriter::setCompression(const String & compression_method_, int compression_level_)
{
    std::lock_guard lock{mutex};
    compression_method = compressionMethodToInt(compression_method_);
    compression_level = compression_level_;
}

void ZipArchiveWriter::setPassword(const String & password_)
{
    std::lock_guard lock{mutex};
    password = password_;
}

int ZipArchiveWriter::compressionMethodToInt(const String & compression_method_)
{
    if (compression_method_.empty())
        return MZ_COMPRESS_METHOD_DEFLATE; /// By default the compression method is "deflate".
    else if (compression_method_ == kStore)
        return MZ_COMPRESS_METHOD_STORE;
    else if (compression_method_ == kDeflate)
        return MZ_COMPRESS_METHOD_DEFLATE;
    else if (compression_method_ == kBzip2)
        return MZ_COMPRESS_METHOD_BZIP2;
    else if (compression_method_ == kLzma)
        return MZ_COMPRESS_METHOD_LZMA;
    else if (compression_method_ == kZstd)
        return MZ_COMPRESS_METHOD_ZSTD;
    else if (compression_method_ == kXz)
        return MZ_COMPRESS_METHOD_XZ;
    else
        throw Exception(ErrorCodes::CANNOT_PACK_ARCHIVE, "Unknown compression method specified for a zip archive: {}", compression_method_);
}

String ZipArchiveWriter::intToCompressionMethod(int compression_method_)
{
    switch (compression_method_)
    {
        case MZ_COMPRESS_METHOD_STORE:   return kStore;
        case MZ_COMPRESS_METHOD_DEFLATE: return kDeflate;
        case MZ_COMPRESS_METHOD_BZIP2:   return kBzip2;
        case MZ_COMPRESS_METHOD_LZMA:    return kLzma;
        case MZ_COMPRESS_METHOD_ZSTD:    return kZstd;
        case MZ_COMPRESS_METHOD_XZ:      return kXz;
    }
    throw Exception(ErrorCodes::CANNOT_PACK_ARCHIVE, "Unknown compression method specified for a zip archive: {}", compression_method_);
}

/// Checks that a passed compression method can be used.
void ZipArchiveWriter::checkCompressionMethodIsEnabled(int compression_method_)
{
    switch (compression_method_)
    {
        case MZ_COMPRESS_METHOD_STORE: [[fallthrough]];
        case MZ_COMPRESS_METHOD_DEFLATE:
        case MZ_COMPRESS_METHOD_LZMA:
        case MZ_COMPRESS_METHOD_ZSTD:
        case MZ_COMPRESS_METHOD_XZ:
            return;

        case MZ_COMPRESS_METHOD_BZIP2:
        {
#if USE_BZIP2
            return;
#else
            throw Exception(ErrorCodes::SUPPORT_IS_DISABLED, "bzip2 compression method is disabled");
#endif
        }
    }
    throw Exception(ErrorCodes::CANNOT_PACK_ARCHIVE, "Unknown compression method specified for a zip archive: {}", compression_method_);
}

/// Checks that encryption is enabled.
void ZipArchiveWriter::checkEncryptionIsEnabled()
{
#if !USE_SSL
    throw Exception(ErrorCodes::SUPPORT_IS_DISABLED, "Encryption in zip archive is disabled");
#endif
}

ZipArchiveWriter::HandleHolder ZipArchiveWriter::acquireHandle()
{
    return HandleHolder{std::static_pointer_cast<ZipArchiveWriter>(shared_from_this())};
}

RawHandle ZipArchiveWriter::acquireRawHandle()
{
    std::lock_guard lock{mutex};
    if (!handle)
        throw Exception(ErrorCodes::LOGICAL_ERROR, "Cannot have more than one write buffer while writing a zip archive");
    return std::exchange(handle, nullptr);
}

void ZipArchiveWriter::releaseRawHandle(RawHandle raw_handle_)
{
    std::lock_guard lock{mutex};
    handle = raw_handle_;
}

void ZipArchiveWriter::checkResult(int code) const
{
    if (code >= ZIP_OK)
        return;

    String message = "Code = ";
    switch (code)
    {
        case ZIP_ERRNO: message += "ERRNO, errno = " + errnoToString(); break;
        case ZIP_PARAMERROR: message += "PARAMERROR"; break;
        case ZIP_BADZIPFILE: message += "BADZIPFILE"; break;
        case ZIP_INTERNALERROR: message += "INTERNALERROR"; break;
        default: message += std::to_string(code); break;
    }
    showError(message);
}

void ZipArchiveWriter::showError(const String & message) const
{
    throw Exception(ErrorCodes::CANNOT_PACK_ARCHIVE, "Couldn't pack zip archive {}: {}", quoteString(path_to_archive), message);
}

}

#endif