/
cliplugin.cpp
383 lines (324 loc) · 15.7 KB
/
cliplugin.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
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
/*
* ark -- archiver for the KDE project
*
* Copyright (C) 2009 Harald Hvaal <haraldhv@stud.ntnu.no>
* Copyright (C) 2009-2011 Raphael Kubo da Costa <rakuco@FreeBSD.org>
* Copyright (c) 2016 Vladyslav Batyrenko <mvlabat@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "cliplugin.h"
#include "ark_debug.h"
#include "cliinterface.h"
#include <QDateTime>
#include <QDir>
#include <QRegularExpression>
#include <KLocalizedString>
#include <KPluginFactory>
using namespace Kerfuffle;
K_PLUGIN_CLASS_WITH_JSON(CliPlugin, "kerfuffle_cli7z.json")
CliPlugin::CliPlugin(QObject *parent, const QVariantList & args)
: CliInterface(parent, args)
, m_archiveType(ArchiveType7z)
, m_parseState(ParseStateTitle)
, m_linesComment(0)
, m_isFirstInformationEntry(true)
{
qCDebug(ARK) << "Loaded cli_7z plugin";
setupCliProperties();
}
CliPlugin::~CliPlugin()
{
}
void CliPlugin::resetParsing()
{
m_parseState = ParseStateTitle;
m_comment.clear();
m_numberOfVolumes = 0;
}
void CliPlugin::setupCliProperties()
{
qCDebug(ARK) << "Setting up parameters...";
m_cliProps->setProperty("captureProgress", false);
m_cliProps->setProperty("addProgram", QStringLiteral("7z"));
m_cliProps->setProperty("addSwitch", QStringList{QStringLiteral("a"),
QStringLiteral("-l")});
m_cliProps->setProperty("deleteProgram", QStringLiteral("7z"));
m_cliProps->setProperty("deleteSwitch", QStringLiteral("d"));
m_cliProps->setProperty("extractProgram", QStringLiteral("7z"));
m_cliProps->setProperty("extractSwitch", QStringList{QStringLiteral("x")});
m_cliProps->setProperty("extractSwitchNoPreserve", QStringList{QStringLiteral("e")});
m_cliProps->setProperty("listProgram", QStringLiteral("7z"));
m_cliProps->setProperty("listSwitch", QStringList{QStringLiteral("l"),
QStringLiteral("-slt")});
m_cliProps->setProperty("moveProgram", QStringLiteral("7z"));
m_cliProps->setProperty("moveSwitch", QStringLiteral("rn"));
m_cliProps->setProperty("testProgram", QStringLiteral("7z"));
m_cliProps->setProperty("testSwitch", QStringLiteral("t"));
m_cliProps->setProperty("passwordSwitch", QStringList{QStringLiteral("-p$Password")});
m_cliProps->setProperty("passwordSwitchHeaderEnc", QStringList{QStringLiteral("-p$Password"),
QStringLiteral("-mhe=on")});
m_cliProps->setProperty("compressionLevelSwitch", QStringLiteral("-mx=$CompressionLevel"));
m_cliProps->setProperty("compressionMethodSwitch", QHash<QString,QVariant>{{QStringLiteral("application/x-7z-compressed"), QStringLiteral("-m0=$CompressionMethod")},
{QStringLiteral("application/zip"), QStringLiteral("-mm=$CompressionMethod")}});
m_cliProps->setProperty("encryptionMethodSwitch", QHash<QString,QVariant>{{QStringLiteral("application/x-7z-compressed"), QString()},
{QStringLiteral("application/zip"), QStringLiteral("-mem=$EncryptionMethod")}});
m_cliProps->setProperty("multiVolumeSwitch", QStringLiteral("-v$VolumeSizek"));
m_cliProps->setProperty("testPassedPatterns", QStringList{QStringLiteral("^Everything is Ok$")});
m_cliProps->setProperty("fileExistsFileNameRegExp", QStringList{QStringLiteral("^file \\./(.*)$"),
QStringLiteral("^ Path: \\./(.*)$")});
m_cliProps->setProperty("fileExistsInput", QStringList{QStringLiteral("Y"), //Overwrite
QStringLiteral("N"), //Skip
QStringLiteral("A"), //Overwrite all
QStringLiteral("S"), //Autoskip
QStringLiteral("Q")}); //Cancel
m_cliProps->setProperty("multiVolumeSuffix", QStringList{QStringLiteral("$Suffix.001")});
}
void CliPlugin::fixDirectoryFullName()
{
if (m_currentArchiveEntry->isDir()) {
const QString directoryName = m_currentArchiveEntry->fullPath();
if (!directoryName.endsWith(QLatin1Char('/'))) {
m_currentArchiveEntry->setProperty("fullPath", QString(directoryName + QLatin1Char('/')));
}
}
}
bool CliPlugin::readListLine(const QString& line)
{
static const QLatin1String archiveInfoDelimiter1("--"); // 7z 9.13+
static const QLatin1String archiveInfoDelimiter2("----"); // 7z 9.04
static const QLatin1String entryInfoDelimiter("----------");
if (line.startsWith(QLatin1String("Open ERROR: Can not open the file as [7z] archive"))) {
emit error(i18n("Listing the archive failed."));
return false;
}
const QRegularExpression rxVersionLine(QStringLiteral("^p7zip Version ([\\d\\.]+) .*$"));
QRegularExpressionMatch matchVersion;
switch (m_parseState) {
case ParseStateTitle:
matchVersion = rxVersionLine.match(line);
if (matchVersion.hasMatch()) {
m_parseState = ParseStateHeader;
const QString p7zipVersion = matchVersion.captured(1);
qCDebug(ARK) << "p7zip version" << p7zipVersion << "detected";
}
break;
case ParseStateHeader:
if (line.startsWith(QLatin1String("Listing archive:"))) {
qCDebug(ARK) << "Archive name: "
<< line.right(line.size() - 16).trimmed();
} else if ((line == archiveInfoDelimiter1) ||
(line == archiveInfoDelimiter2)) {
m_parseState = ParseStateArchiveInformation;
} else if (line.contains(QLatin1String("Error: "))) {
qCWarning(ARK) << line.mid(7);
}
break;
case ParseStateArchiveInformation:
if (line == entryInfoDelimiter) {
m_parseState = ParseStateEntryInformation;
} else if (line.startsWith(QLatin1String("Type = "))) {
const QString type = line.mid(7).trimmed();
qCDebug(ARK) << "Archive type: " << type;
if (type == QLatin1String("7z")) {
m_archiveType = ArchiveType7z;
} else if (type == QLatin1String("bzip2")) {
m_archiveType = ArchiveTypeBZip2;
} else if (type == QLatin1String("gzip")) {
m_archiveType = ArchiveTypeGZip;
} else if (type == QLatin1String("xz")) {
m_archiveType = ArchiveTypeXz;
} else if (type == QLatin1String("tar")) {
m_archiveType = ArchiveTypeTar;
} else if (type == QLatin1String("zip")) {
m_archiveType = ArchiveTypeZip;
} else if (type == QLatin1String("Rar")) {
m_archiveType = ArchiveTypeRar;
} else if (type == QLatin1String("Split")) {
setMultiVolume(true);
} else {
// Should not happen
qCWarning(ARK) << "Unsupported archive type";
return false;
}
} else if (line.startsWith(QLatin1String("Volumes = "))) {
m_numberOfVolumes = line.section(QLatin1Char('='), 1).trimmed().toInt();
} else if (line.startsWith(QLatin1String("Method = "))) {
QStringList methods = line.section(QLatin1Char('='), 1).trimmed().split(QLatin1Char(' '), QString::SkipEmptyParts);
handleMethods(methods);
} else if (line.startsWith(QLatin1String("Comment = "))) {
m_parseState = ParseStateComment;
m_comment.append(line.section(QLatin1Char('='), 1) + QLatin1Char('\n'));
}
break;
case ParseStateComment:
if (line == entryInfoDelimiter) {
m_parseState = ParseStateEntryInformation;
if (!m_comment.trimmed().isEmpty()) {
m_comment = m_comment.trimmed();
m_linesComment = m_comment.count(QLatin1Char('\n')) + 1;
qCDebug(ARK) << "Found a comment with" << m_linesComment << "lines";
}
} else {
m_comment.append(line + QLatin1Char('\n'));
}
break;
case ParseStateEntryInformation:
if (m_isFirstInformationEntry) {
m_isFirstInformationEntry = false;
m_currentArchiveEntry = new Archive::Entry(this);
m_currentArchiveEntry->compressedSizeIsSet = false;
}
if (line.startsWith(QLatin1String("Path = "))) {
const QString entryFilename =
QDir::fromNativeSeparators(line.mid(7).trimmed());
m_currentArchiveEntry->setProperty("fullPath", entryFilename);
} else if (line.startsWith(QLatin1String("Size = "))) {
m_currentArchiveEntry->setProperty("size", line.mid(7).trimmed());
} else if (line.startsWith(QLatin1String("Packed Size = "))) {
// #236696: 7z files only show a single Packed Size value
// corresponding to the whole archive.
if (m_archiveType != ArchiveType7z) {
m_currentArchiveEntry->compressedSizeIsSet = true;
m_currentArchiveEntry->setProperty("compressedSize", line.mid(14).trimmed());
}
} else if (line.startsWith(QLatin1String("Modified = "))) {
m_currentArchiveEntry->setProperty("timestamp", QDateTime::fromString(line.mid(11).trimmed(),
QStringLiteral("yyyy-MM-dd hh:mm:ss")));
} else if (line.startsWith(QLatin1String("Folder = "))) {
const QString isDirectoryStr = line.mid(9).trimmed();
Q_ASSERT(isDirectoryStr == QLatin1String("+") || isDirectoryStr == QStringLiteral("-"));
const bool isDirectory = isDirectoryStr.startsWith(QLatin1Char('+'));
m_currentArchiveEntry->setProperty("isDirectory", isDirectory);
fixDirectoryFullName();
} else if (line.startsWith(QLatin1String("Attributes = "))) {
const QString attributes = line.mid(13).trimmed();
if (attributes.contains(QLatin1Char('D'))) {
m_currentArchiveEntry->setProperty("isDirectory", true);
fixDirectoryFullName();
}
if (attributes.contains(QLatin1Char('_'))) {
// Unix attributes
m_currentArchiveEntry->setProperty("permissions",
attributes.mid(attributes.indexOf(QLatin1Char(' ')) + 1));
} else {
// FAT attributes
m_currentArchiveEntry->setProperty("permissions", attributes);
}
} else if (line.startsWith(QLatin1String("CRC = "))) {
m_currentArchiveEntry->setProperty("CRC", line.mid(6).trimmed());
} else if (line.startsWith(QLatin1String("Method = "))) {
m_currentArchiveEntry->setProperty("method", line.mid(9).trimmed());
// For zip archives we need to check method for each entry.
if (m_archiveType == ArchiveTypeZip) {
QStringList methods = line.section(QLatin1Char('='), 1).trimmed().split(QLatin1Char(' '), QString::SkipEmptyParts);
handleMethods(methods);
}
} else if (line.startsWith(QLatin1String("Encrypted = ")) &&
line.size() >= 13) {
m_currentArchiveEntry->setProperty("isPasswordProtected", line.at(12) == QLatin1Char('+'));
} else if (line.startsWith(QLatin1String("Block = ")) ||
line.startsWith(QLatin1String("Version = "))) {
m_isFirstInformationEntry = true;
if (!m_currentArchiveEntry->fullPath().isEmpty()) {
emit entry(m_currentArchiveEntry);
}
else {
delete m_currentArchiveEntry;
}
m_currentArchiveEntry = nullptr;
}
break;
}
return true;
}
bool CliPlugin::readExtractLine(const QString &line)
{
if (line.startsWith(QLatin1String("ERROR: E_FAIL"))) {
emit error(i18n("Extraction failed due to an unknown error."));
return false;
}
if (line.startsWith(QLatin1String("ERROR: CRC Failed")) ||
line.startsWith(QLatin1String("ERROR: Headers Error"))) {
emit error(i18n("Extraction failed due to one or more corrupt files. Any extracted files may be damaged."));
return false;
}
return true;
}
bool CliPlugin::readDeleteLine(const QString &line)
{
if (line.startsWith(QLatin1String("Error: ")) &&
line.endsWith(QLatin1String(" is not supported archive"))) {
emit error(i18n("Delete operation failed. Try upgrading p7zip or disabling the p7zip plugin in the configuration dialog."));
return false;
}
return true;
}
void CliPlugin::handleMethods(const QStringList &methods)
{
for (const QString &method : methods) {
QRegularExpression rxEncMethod(QStringLiteral("^(7zAES|AES-128|AES-192|AES-256|ZipCrypto)$"));
if (rxEncMethod.match(method).hasMatch()) {
QRegularExpression rxAESMethods(QStringLiteral("^(AES-128|AES-192|AES-256)$"));
if (rxAESMethods.match(method).hasMatch()) {
// Remove dash for AES methods.
emit encryptionMethodFound(QString(method).remove(QLatin1Char('-')));
} else {
emit encryptionMethodFound(method);
}
continue;
}
// LZMA methods are output with some trailing numbers by 7z representing dictionary/block sizes.
// We are not interested in these, so remove them.
if (method.startsWith(QLatin1String("LZMA2"))) {
emit compressionMethodFound(method.left(5));
} else if (method.startsWith(QLatin1String("LZMA"))) {
emit compressionMethodFound(method.left(4));
} else if (method == QLatin1String("xz")) {
emit compressionMethodFound(method.toUpper());
} else {
emit compressionMethodFound(method);
}
}
}
bool CliPlugin::isPasswordPrompt(const QString &line)
{
return line.startsWith(QLatin1String("Enter password (will not be echoed):"));
}
bool CliPlugin::isWrongPasswordMsg(const QString &line)
{
return line.contains(QLatin1String("Wrong password"));
}
bool CliPlugin::isCorruptArchiveMsg(const QString &line)
{
return (line == QLatin1String("Unexpected end of archive") ||
line == QLatin1String("Headers Error"));
}
bool CliPlugin::isDiskFullMsg(const QString &line)
{
return line.contains(QLatin1String("No space left on device"));
}
bool CliPlugin::isFileExistsMsg(const QString &line)
{
return (line == QLatin1String("(Y)es / (N)o / (A)lways / (S)kip all / A(u)to rename all / (Q)uit? ") ||
line == QLatin1String("? (Y)es / (N)o / (A)lways / (S)kip all / A(u)to rename all / (Q)uit? "));
}
bool CliPlugin::isFileExistsFileName(const QString &line)
{
return (line.startsWith(QLatin1String("file ./")) ||
line.startsWith(QLatin1String(" Path: ./")));
}
#include "cliplugin.moc"