aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/IO/Archives/LibArchiveReader.h
blob: 5d5e9a5a25e24b6cbb5fb8c75ff4ec47813a6c86 (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
#pragma once

#include "clickhouse_config.h"

#include <IO/Archives/IArchiveReader.h>


namespace DB
{

#if USE_LIBARCHIVE

class ReadBuffer;
class ReadBufferFromFileBase;
class SeekableReadBuffer;

/// Implementation of IArchiveReader for reading archives using libarchive.
class LibArchiveReader : public IArchiveReader
{
public:
    ~LibArchiveReader() override;

    const std::string & getPath() const override;

    /// Returns true if there is a specified file in the archive.
    bool fileExists(const String & filename) override;

    /// Returns the information about a file stored in the archive.
    FileInfo getFileInfo(const String & filename) override;

    /// Starts enumerating files in the archive.
    std::unique_ptr<FileEnumerator> firstFile() override;

    /// Starts reading a file from the archive. The function returns a read buffer,
    /// you can read that buffer to extract uncompressed data from the archive.
    /// Several read buffers can be used at the same time in parallel.
    std::unique_ptr<ReadBufferFromFileBase> readFile(const String & filename, bool throw_on_not_found) override;
    std::unique_ptr<ReadBufferFromFileBase> readFile(NameFilter filter, bool throw_on_not_found) override;

    /// It's possible to convert a file enumerator to a read buffer and vice versa.
    std::unique_ptr<ReadBufferFromFileBase> readFile(std::unique_ptr<FileEnumerator> enumerator) override;
    std::unique_ptr<FileEnumerator> nextFile(std::unique_ptr<ReadBuffer> read_buffer) override;

    std::vector<std::string> getAllFiles() override;
    std::vector<std::string> getAllFiles(NameFilter filter) override;

    /// Sets password used to decrypt the contents of the files in the archive.
    void setPassword(const String & password_) override;

protected:
    /// Constructs an archive's reader that will read from a file in the local filesystem.
    LibArchiveReader(std::string archive_name_, bool lock_on_reading_, std::string path_to_archive_);

private:
    class ReadBufferFromLibArchive;
    class Handle;
    class FileEnumeratorImpl;

    const std::string archive_name;
    const bool lock_on_reading;
    const String path_to_archive;
};

class TarArchiveReader : public LibArchiveReader
{
public:
    explicit TarArchiveReader(std::string path_to_archive) : LibArchiveReader("tar", /*lock_on_reading_=*/ true, std::move(path_to_archive)) { }
};

class SevenZipArchiveReader : public LibArchiveReader
{
public:
    explicit SevenZipArchiveReader(std::string path_to_archive) : LibArchiveReader("7z", /*lock_on_reading_=*/ false, std::move(path_to_archive)) { }
};

#endif

}