Repository navigation
Expand file tree
/
Copy pathtarextract.cpp
More file actions
320 lines (263 loc) · 8.81 KB
/
Copy pathtarextract.cpp
File metadata and controls
320 lines (263 loc) · 8.81 KB
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
#include "tarextract.h"
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QDebug>
#include <archive.h>
#include <archive_entry.h>
namespace {
const QString ERROR_FILE_NOT_FOUND = "File not found: ";
const QString ERROR_UNSUPPORTED = "Unsupported archive format.";
const QString ERROR_OPEN = "Failed to open archive: ";
const QString ERROR_READ = "Failed to read archive: ";
const QString ERROR_CREATE_DIR = "Failed to create the destination folder: ";
const QString ERROR_NO_FILES = "No files were extracted.";
bool fileExists(const QString &filePath, QString &errorMessage)
{
if (!QFileInfo(filePath).exists()) {
errorMessage = ERROR_FILE_NOT_FOUND + filePath;
return false;
}
return true;
}
bool createOutputDir(const QString &outputPath, QString &errorMessage)
{
if (!QDir().mkpath(outputPath)) {
errorMessage = ERROR_CREATE_DIR + outputPath;
return false;
}
return true;
}
QString archiveError(archive *reader)
{
const char *message = archive_error_string(reader);
return message ? QString::fromLocal8Bit(message) : QStringLiteral("Unknown archive error.");
}
archive *openTarArchive(const QString &filePath, QString &errorMessage)
{
archive *reader = archive_read_new();
if (!reader) {
errorMessage = ERROR_OPEN + QStringLiteral("failed to allocate archive reader.");
return nullptr;
}
archive_read_support_filter_all(reader);
archive_read_support_format_tar(reader);
const QByteArray nativePath = QFile::encodeName(filePath);
if (archive_read_open_filename(reader, nativePath.constData(), 65536) != ARCHIVE_OK) {
errorMessage = ERROR_OPEN + archiveError(reader);
archive_read_free(reader);
return nullptr;
}
return reader;
}
QString safeOutputPath(const QString &rootPath, const QByteArray &archivePath, bool &valid)
{
valid = false;
if (archivePath.isEmpty()) {
return QString();
}
QString relativePath = QFile::decodeName(archivePath);
relativePath.replace(QLatin1Char('\\'), QLatin1Char('/'));
if (relativePath.isEmpty() || QDir::isAbsolutePath(relativePath)) {
return QString();
}
const QString cleanRoot = QDir::cleanPath(rootPath);
const QString candidate = QDir::cleanPath(cleanRoot + QLatin1Char('/') + relativePath);
if (candidate != cleanRoot && !candidate.startsWith(cleanRoot + QLatin1Char('/'))) {
return QString();
}
valid = true;
return candidate;
}
int countTarFiles(const QString &filePath, QString &errorMessage)
{
archive *reader = openTarArchive(filePath, errorMessage);
if (!reader) {
return -1;
}
int count = 0;
archive_entry *entry = nullptr;
int result = ARCHIVE_OK;
while ((result = archive_read_next_header(reader, &entry)) == ARCHIVE_OK) {
if (archive_entry_filetype(entry) != AE_IFDIR) {
++count;
}
archive_read_data_skip(reader);
}
if (result != ARCHIVE_EOF) {
errorMessage = ERROR_READ + archiveError(reader);
archive_read_free(reader);
return -1;
}
archive_read_free(reader);
return count;
}
}
bool isSupportedTarArchive(const QString &filePath)
{
const QString fileName = QFileInfo(filePath).fileName().toLower();
static const QStringList suffixes = {
QStringLiteral(".tar"),
QStringLiteral(".tgz"),
QStringLiteral(".tar.gz"),
QStringLiteral(".tbz"),
QStringLiteral(".tar.bz"),
QStringLiteral(".tbz2"),
QStringLiteral(".tar.bz2"),
QStringLiteral(".tar.xz")
};
for (const QString &suffix : suffixes) {
if (fileName.endsWith(suffix)) {
return true;
}
}
return false;
}
QStringList getTarArchiveContents(const QString &filePath, QString &errorMessage)
{
errorMessage.clear();
QStringList files;
if (!fileExists(filePath, errorMessage)) {
return files;
}
if (!isSupportedTarArchive(filePath)) {
errorMessage = ERROR_UNSUPPORTED;
return files;
}
archive *reader = openTarArchive(filePath, errorMessage);
if (!reader) {
return files;
}
archive_entry *entry = nullptr;
int result = ARCHIVE_OK;
while ((result = archive_read_next_header(reader, &entry)) == ARCHIVE_OK) {
const char *pathname = archive_entry_pathname(entry);
if (pathname && archive_entry_filetype(entry) != AE_IFDIR) {
files << QFile::decodeName(pathname);
}
archive_read_data_skip(reader);
}
if (result != ARCHIVE_EOF) {
errorMessage = ERROR_READ + archiveError(reader);
files.clear();
}
archive_read_free(reader);
return files;
}
bool extractTarArchive(const QString &filePath, const QString &outputPath,
QString &errorMessage,
ExtractProgressCallback progressCallback)
{
errorMessage.clear();
if (!fileExists(filePath, errorMessage)) {
return false;
}
if (!isSupportedTarArchive(filePath)) {
errorMessage = ERROR_UNSUPPORTED;
return false;
}
if (outputPath.isEmpty()) {
errorMessage = QStringLiteral("No output folder was selected.");
return false;
}
if (!createOutputDir(outputPath, errorMessage)) {
return false;
}
const QString rootPath = QDir::cleanPath(QDir(outputPath).absolutePath());
QString countError;
const int totalFiles = countTarFiles(filePath, countError);
if (totalFiles < 0) {
errorMessage = countError;
return false;
}
archive *reader = openTarArchive(filePath, errorMessage);
if (!reader) {
return false;
}
const int extractFlags = ARCHIVE_EXTRACT_TIME
| ARCHIVE_EXTRACT_PERM
| ARCHIVE_EXTRACT_ACL
| ARCHIVE_EXTRACT_FFLAGS
| ARCHIVE_EXTRACT_SECURE_SYMLINKS
| ARCHIVE_EXTRACT_SECURE_NODOTDOT;
int currentFile = 0;
int extractedCount = 0;
int failedCount = 0;
bool anySuccess = false;
bool fatalError = false;
archive_entry *entry = nullptr;
int result = ARCHIVE_OK;
while ((result = archive_read_next_header(reader, &entry)) == ARCHIVE_OK) {
const char *pathname = archive_entry_pathname(entry);
if (!pathname) {
archive_read_data_skip(reader);
continue;
}
const QString entryName = QFile::decodeName(pathname);
const mode_t entryType = archive_entry_filetype(entry);
bool validPath = false;
const QString targetPath = safeOutputPath(rootPath, QByteArray(pathname), validPath);
QByteArray hardlinkPath;
const char *hardlink = archive_entry_hardlink(entry);
if (validPath && hardlink) {
bool validHardlink = false;
const QString hardlinkTarget = safeOutputPath(rootPath, QByteArray(hardlink), validHardlink);
if (!validHardlink) {
validPath = false;
} else {
hardlinkPath = QFile::encodeName(hardlinkTarget);
archive_entry_set_hardlink(entry, hardlinkPath.constData());
}
}
if (!validPath) {
qWarning() << "Skipping unsafe tar path:" << entryName;
if (entryType != AE_IFDIR) {
++currentFile;
if (progressCallback) {
progressCallback(currentFile, totalFiles, entryName);
}
}
archive_read_data_skip(reader);
continue;
}
const QByteArray nativeTargetPath = QFile::encodeName(targetPath);
archive_entry_set_pathname(entry, nativeTargetPath.constData());
const int extractResult = archive_read_extract(reader, entry, extractFlags);
if (extractResult == ARCHIVE_OK || extractResult == ARCHIVE_WARN) {
++extractedCount;
anySuccess = true;
} else {
++failedCount;
qWarning() << "Failed to extract tar entry:" << entryName << archiveError(reader);
if (extractResult == ARCHIVE_FATAL) {
fatalError = true;
break;
}
}
if (entryType != AE_IFDIR) {
++currentFile;
if (progressCallback) {
progressCallback(currentFile, totalFiles, entryName);
}
}
}
if (fatalError) {
errorMessage = ERROR_READ + archiveError(reader);
archive_read_free(reader);
return false;
}
if (result != ARCHIVE_EOF) {
errorMessage = ERROR_READ + archiveError(reader);
archive_read_free(reader);
return false;
}
archive_read_free(reader);
if (!anySuccess) {
errorMessage = ERROR_NO_FILES;
return false;
}
if (failedCount > 0) {
qWarning() << failedCount << "tar file(s) failed to extract.";
}
return true;
}