aboutsummaryrefslogtreecommitdiff
path: root/src/Archive.cpp
blob: 08106c31ff1892075bd2029cff7c8f7cb95bfccb (plain)
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
#include "../include/Archive.hpp"
#include "../include/utils.hpp"
#include <cstring>
#include <archive.h>
#include <archive_entry.h>

using namespace std;

#if OS_FAMILY == OS_FAMILY_POSIX
#define archive_read_open_filename_native archive_read_open_filename
#define archive_entry_pathname_native archive_entry_pathname
#else
#define archive_read_open_filename_native archive_read_open_filename_w
#define archive_entry_pathname_native archive_entry_pathname_w
#endif

class FileHandler
{
    DISABLE_COPY(FileHandler)
public:
    FileHandler(FILE *_file) : file(_file)
    {

    }

    FileHandler(FileHandler &&other)
    {
        file = other.file;
        other.file = nullptr;
    }

    ~FileHandler()
    {
        fclose(file);
    }

    FILE *file;
};

// libarchive code is modified version of: https://github.com/libarchive/libarchive/wiki/Examples#A_Complete_Extractor
namespace sibs
{
    static int copy_data(struct archive *ar, struct archive *aw)
    {
        int r;
        const void *buff;
        size_t size;
        int64_t offset;

        while(true)
        {
            r = archive_read_data_block(ar, &buff, &size, &offset);
            if (r == ARCHIVE_EOF)
                return ARCHIVE_OK;
            else if (r < ARCHIVE_OK)
                return r;

            r = archive_write_data_block(aw, buff, size, offset);
            if (r < ARCHIVE_OK)
                return r;
        }
    }

    Result<bool> Archive::extract(const _tinydir_char_t *source, const _tinydir_char_t *destination)
    {
        struct archive *a;
        struct archive *ext;
        struct archive_entry *entry;
        int flags;
        int r;

        /* Select which attributes we want to restore. */
        flags = ARCHIVE_EXTRACT_TIME;
        flags |= ARCHIVE_EXTRACT_PERM;
        flags |= ARCHIVE_EXTRACT_ACL;
        flags |= ARCHIVE_EXTRACT_FFLAGS;

        FileString rootName;

        a = archive_read_new();
        archive_read_support_format_all(a);
        archive_read_support_compression_all(a);
        ext = archive_write_disk_new();
        archive_write_disk_set_options(ext, flags);
        archive_write_disk_set_standard_lookup(ext);


        if ((r = archive_read_open_filename_native(a, source, 10240)))
        {
            string errMsg = "Failed to extract archive: ";
            errMsg += toUtf8(source);
            return Result<bool>::Err(errMsg);
        }

        while(true)
        {
            r = archive_read_next_header(a, &entry);
            if (r == ARCHIVE_EOF)
                break;
            else if (r < ARCHIVE_OK)
            {
                string errMsg = "Failed to extract archive: ";
                errMsg += toUtf8(source);
                errMsg += "; reason: ";
                errMsg += archive_error_string(a);
                return Result<bool>::Err(errMsg);
            }
            else if (r < ARCHIVE_WARN)
            {
                string errMsg = "Failed to extract archive: ";
                errMsg += toUtf8(source);
                errMsg += "; reason: ";
                errMsg += archive_error_string(a);
                return Result<bool>::Err(errMsg);
            }

            const _tinydir_char_t* currentFile = archive_entry_pathname_native(entry);
            if(rootName.empty())
                rootName = currentFile;

            FileString fullOutputPath = destination;
            fullOutputPath += (currentFile + (rootName.empty() ? 0 : rootName.size() - 1));
            // TODO: Verify if this really works. Why doesn't libarchive have wide string version of archive_entry_set_pathname?
            string fullOutputPathUtf8 = toUtf8(fullOutputPath);
            archive_entry_set_pathname(entry, fullOutputPathUtf8.c_str());

            r = archive_write_header(ext, entry);
            if (r < ARCHIVE_OK)
            {
                string errMsg = "Failed to extract archive: ";
                errMsg += toUtf8(source);
                errMsg += "; reason: ";
                errMsg += archive_error_string(ext);
                return Result<bool>::Err(errMsg);
            }
            else if (archive_entry_size(entry) > 0) {
                r = copy_data(a, ext);
                if (r < ARCHIVE_OK)
                {
                    string errMsg = "Failed to extract archive: ";
                    errMsg += toUtf8(source);
                    errMsg += "; reason: ";
                    errMsg += archive_error_string(ext);
                    return Result<bool>::Err(errMsg);
                }
                else if (r < ARCHIVE_WARN)
                {
                    string errMsg = "Failed to extract archive: ";
                    errMsg += toUtf8(source);
                    errMsg += "; reason: ";
                    errMsg += archive_error_string(ext);
                    return Result<bool>::Err(errMsg);
                }
            }

            r = archive_write_finish_entry(ext);
            if (r < ARCHIVE_OK)
            {
                string errMsg = "Failed to extract archive: ";
                errMsg += toUtf8(source);
                errMsg += "; reason: ";
                errMsg += archive_error_string(ext);
                return Result<bool>::Err(errMsg);
            }
            else if (r < ARCHIVE_WARN)
            {
                string errMsg = "Failed to extract archive: ";
                errMsg += toUtf8(source);
                errMsg += "; reason: ";
                errMsg += archive_error_string(ext);
                return Result<bool>::Err(errMsg);
            }
        }

        archive_read_close(a);
        archive_read_free(a);
        archive_write_close(ext);
        archive_write_free(ext);
        return Result<bool>::Ok(true);
    }
}