forked from Voidious/BerryBots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
guizipper.cpp
183 lines (168 loc) · 6.07 KB
/
guizipper.cpp
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
/*
Copyright (C) 2013 - Voidious
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include <fcntl.h>
#include <sstream>
#include <sys/stat.h>
#include <archive.h>
#include <archive_entry.h>
#include <platformstl/filesystem/filesystem_traits.hpp>
#include "filemanager.h"
#include "guizipper.h"
GuiZipper::GuiZipper() {
fileManager_ = new FileManager();
}
GuiZipper::~GuiZipper() {
delete fileManager_;
}
// Based on libarchive's public example code.
// https://github.com/libarchive/libarchive/wiki/Examples#wiki-A_Basic_Write_Example
void GuiZipper::packageFiles(const char *outputFile, const char *baseDir,
char **filenames, int numFiles, bool binary, const char *absMetaFilename,
const char *metaFilename) throw (ZipperException*) {
int r;
struct archive *a = archive_write_new();
r = archive_write_add_filter_gzip(a);
checkForErrors("Error adding filter gzip", a, r);
r = archive_write_set_format_pax_restricted(a);
checkForErrors("Error setting format pax restricted", a, r);
r = archive_write_open_filename(a, outputFile);
checkForErrors("Error opening file", a, r);
packageSingleFile(absMetaFilename, metaFilename, a, false);
for (int x = 0; x < numFiles; x++) {
char *filename = filenames[x];
char *filePath = fileManager_->getFilePath(baseDir, filename);
try {
packageSingleFile(filePath, filename, a, binary);
} catch (ZipperException *ze) {
delete filePath;
throw ze;
}
delete filePath;
}
r = archive_write_close(a);
checkForErrors("Error writing close", a, r);
r = archive_write_free(a);
checkForErrors("Error writing free", a, r);
}
void GuiZipper::packageSingleFile(const char *absFilename, const char *filename,
struct archive *a, bool binary) throw (ZipperException*) {
struct stat st;
char buff[8192];
int fd;
stat(absFilename, &st);
struct archive_entry *entry = archive_entry_new();
archive_entry_copy_stat(entry, &st);
archive_entry_set_filetype(entry, AE_IFREG);
archive_entry_set_pathname(entry, filename);
archive_entry_set_perm(entry, 0644);
int r = archive_write_header(a, entry);
checkForErrors("Error writing header", a, r);
int flags = O_RDONLY;
#ifdef __WIN32__
if (binary) {
flags |= O_BINARY;
}
#endif
fd = open(absFilename, flags);
ssize_t len = read(fd, buff, sizeof(buff));
while (len > 0) {
archive_write_data(a, buff, len);
len = read(fd, buff, sizeof(buff));
}
close(fd);
archive_entry_free(entry);
}
// Based on libarchive's public example code.
// https://github.com/libarchive/libarchive/wiki/Examples#wiki-Constructing_Objects_On_Disk
void GuiZipper::unpackFile(const char *zipFile, const char *outputDir)
throw (ZipperException*) {
// TODO: use archive_write_disk_open instead (if/when it exists)
char cwd[4096];
getcwd(cwd, 4096);
char *absZipFile = fileManager_->getAbsFilePath(zipFile);
platformstl::filesystem_traits<char> traits;
traits.set_current_directory(outputDir);
struct archive *a;
struct archive *ext;
struct archive_entry *entry;
int flags;
int r;
flags = ARCHIVE_EXTRACT_TIME;
flags |= ARCHIVE_EXTRACT_PERM;
flags |= ARCHIVE_EXTRACT_ACL;
flags |= ARCHIVE_EXTRACT_FFLAGS;
a = archive_read_new();
archive_read_support_format_tar(a);
archive_read_support_filter_gzip(a);
ext = archive_write_disk_new();
archive_write_disk_set_options(ext, flags);
archive_write_disk_set_standard_lookup(ext);
r = archive_read_open_filename(a, absZipFile, 10240);
checkForErrors("Error opening archive for reading", a, r);
for (;;) {
r = archive_read_next_header(a, &entry);
if (r == ARCHIVE_EOF) {
break;
}
checkForErrors("Error reading next archive header", a, r);
r = archive_write_header(ext, entry);
checkForErrors("Error writing next archive header", a, r);
copyData(a, ext, outputDir);
r = archive_write_finish_entry(ext);
checkForErrors("Error writing archive finish entry", a, r);
}
r = archive_read_close(a);
checkForErrors("Error closing read archive", a, r);
r = archive_read_free(a);
checkForErrors("Error freeing read archive", a, r);
r = archive_write_close(ext);
checkForErrors("Error closing write archive", a, r);
r = archive_write_free(ext);
checkForErrors("Error freeing write archive", a, r);
traits.set_current_directory(cwd);
delete absZipFile;
}
// Based on libarchive's public example code.
// https://github.com/libarchive/libarchive/wiki/Examples#wiki-Constructing_Objects_On_Disk
ssize_t GuiZipper::copyData(struct archive *ar, struct archive *aw,
const char *userDirPath) throw (ZipperException*) {
ssize_t r;
const void *buff;
size_t size;
int64_t offset;
for (;;) {
r = archive_read_data_block(ar, &buff, &size, &offset);
if (r == ARCHIVE_EOF) {
return (ARCHIVE_OK);
}
if (r != ARCHIVE_OK) {
return (r);
}
r = archive_write_data_block(aw, buff, size, offset);
checkForErrors("Error writing archive data block", ar, r);
return r;
}
}
void GuiZipper::checkForErrors(const char *message, struct archive *a, long r)
throw (ZipperException*) {
if (r != ARCHIVE_OK) {
std::stringstream msgStream;
msgStream << message << " (" << r << "): " << archive_error_string(a)
<< " (" << archive_errno(a) << ")";
throw new ZipperException(msgStream.str().c_str());
}
}