-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathclirarplugin.cpp
More file actions
353 lines (297 loc) · 14.4 KB
/
clirarplugin.cpp
File metadata and controls
353 lines (297 loc) · 14.4 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
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
/*
* ark -- archiver for the KDE project
*
* Copyright (C) 2009 Harald Hvaal <haraldhv@stud.ntnu.no>
* Copyright (C) 2010-2011,2014 Raphael Kubo da Costa <rakuco@FreeBSD.org>
* Copyright (C) 2015-2016 Ragnar Thomsen <rthomsen6@gmail.com>
* 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 "clirarplugin.h"
#include "datamanager.h"
#include <QRegularExpression>
#include <QFileInfo>
#include <QDir>
#include <QDebug>
#include <linux/limits.h>
CliRarPluginFactory::CliRarPluginFactory()
{
registerPlugin<CliRarPlugin>();
}
CliRarPluginFactory::~CliRarPluginFactory()
{
}
CliRarPlugin::CliRarPlugin(QObject *parent, const QVariantList &args)
: CliInterface(parent, args)
{
// 如果rar压缩包含有注释信息,没有标志出comment,直接显示注释内容,需要根据空的line进行解析
setListEmptyLines(true);
setupCliProperties();
}
CliRarPlugin::~CliRarPlugin()
{
}
void CliRarPlugin::setupCliProperties()
{
m_cliProps->setProperty("captureProgress", true);
m_cliProps->setProperty("addProgram", QStringLiteral("rar"));
m_cliProps->setProperty("addSwitch", QStringList({QStringLiteral("a")}));
m_cliProps->setProperty("deleteProgram", QStringLiteral("rar"));
m_cliProps->setProperty("deleteSwitch", QStringLiteral("d"));
m_cliProps->setProperty("extractProgram", QStringLiteral("unrar"));
m_cliProps->setProperty("extractSwitch", QStringList{QStringLiteral("x"),
QStringLiteral("-kb")/*,
QStringLiteral("-p-")*/});
m_cliProps->setProperty("extractSwitchNoPreserve", QStringList{QStringLiteral("e"),
QStringLiteral("-kb")/*,
QStringLiteral("-p-")*/});
m_cliProps->setProperty("listProgram", QStringLiteral("unrar"));
m_cliProps->setProperty("listSwitch", QStringList{QStringLiteral("vt"),
QStringLiteral("-v")});
m_cliProps->setProperty("moveProgram", QStringLiteral("rar"));
m_cliProps->setProperty("moveSwitch", QStringLiteral("rn"));
m_cliProps->setProperty("testProgram", QStringLiteral("unrar"));
m_cliProps->setProperty("testSwitch", QStringLiteral("t"));
m_cliProps->setProperty("commentSwitch", QStringList{QStringLiteral("c"),
QStringLiteral("-z$CommentFile")});
m_cliProps->setProperty("passwordSwitch", QStringList{QStringLiteral("-p$Password")});
m_cliProps->setProperty("passwordSwitchHeaderEnc", QStringList{QStringLiteral("-hp$Password")});
m_cliProps->setProperty("compressionLevelSwitch", QStringLiteral("-m$CompressionLevel"));
m_cliProps->setProperty("compressionMethodSwitch", QHash<QString, QVariant> {{QStringLiteral("application/vnd.rar"), QStringLiteral("-ma$CompressionMethod")},
{QStringLiteral("application/x-rar"), QStringLiteral("-ma$CompressionMethod")}
});
m_cliProps->setProperty("multiVolumeSwitch", QStringLiteral("-v$VolumeSizek"));
m_cliProps->setProperty("testPassedPatterns", QStringList{QStringLiteral("^All OK$")});
m_cliProps->setProperty("fileExistsFileNameRegExp", QStringList{QStringLiteral("^(.+) already exists. Overwrite it"), // unrar 3 & 4
QStringLiteral("^Would you like to replace the existing file (.+)$")}); // unrar 5
m_cliProps->setProperty("fileExistsInput", QStringList{QStringLiteral("Y"), //Overwrite
QStringLiteral("N"), //Skip
QStringLiteral("A"), //Overwrite all
QStringLiteral("E"), //Autoskip
QStringLiteral("Q")}); //Cancel
// rar will sometimes create multi-volume archives where first volume is
// called name.part1.rar and other times name.part01.rar.
m_cliProps->setProperty("multiVolumeSuffix", QStringList{QStringLiteral("part01.$Suffix"),
QStringLiteral("part1.$Suffix")});
}
bool CliRarPlugin::isPasswordPrompt(const QString &line)
{
return line.startsWith(QLatin1String("Enter password (will not be echoed) for ")) && line.endsWith(": ");
}
bool CliRarPlugin::isWrongPasswordMsg(const QString &line)
{
return line.startsWith(QLatin1String("The specified password is incorrect"))
|| line.startsWith(QLatin1String("Checksum error in the encrypted file"))
|| line.contains(QLatin1String("password incorrect"))
/*|| line.contains(QLatin1String("wrong password"))*/;
}
bool CliRarPlugin::isCorruptArchiveMsg(const QString &line)
{
return (line == QLatin1String("Unexpected end of archive") || line.contains(QLatin1String("the file header is corrupt"))
|| line.endsWith(QLatin1String("checksum error")));
}
bool CliRarPlugin::isDiskFullMsg(const QString &line)
{
return line.contains(QLatin1String("No space left on device")) ||
(line.startsWith(QLatin1String("Write error in the file"))
&& line.endsWith(QLatin1String("[R]etry, [A]bort ")));
}
bool CliRarPlugin::isFileExistsMsg(const QString &line)
{
return (line == QLatin1String("[Y]es, [N]o, [A]ll, n[E]ver, [R]ename, [Q]uit "));
}
bool CliRarPlugin::isFileExistsFileName(const QString &line)
{
return (line.startsWith(QLatin1String("Would you like to replace the existing file ")));
}
bool CliRarPlugin::isMultiPasswordPrompt(const QString &line)
{
return line.contains("use current password ? [Y]es, [N]o, [A]ll");
}
bool CliRarPlugin::isOpenFileFailed(const QString &line)
{
// 文件名过长情况下会输出
return line.startsWith("Cannot create ");
}
void CliRarPlugin::killProcess(bool emitFinished)
{
Q_UNUSED(emitFinished);
if (!m_process) {
return;
}
qint64 processID = m_process->processId();
// 结束进程,先continue再kill,保证能删除缓存文件
// 使用SIGTERM不能立马结束,需使用SIGKILL强制结束
if (processID > 0) {
kill(static_cast<__pid_t>(processID), SIGCONT);
kill(static_cast<__pid_t>(processID), SIGKILL);
}
m_isProcessKilled = true;
}
bool CliRarPlugin::readListLine(const QString &line)
{
ArchiveData &stArchiveData = DataManager::get_instance().archiveData();
QRegularExpression rxVersionLine(QStringLiteral("^UNRAR (\\d+\\.\\d+)( beta \\d)? .*$"));
QRegularExpressionMatch matchVersion = rxVersionLine.match(line);
switch (m_parseState) {
case ParseStateHeader:
case ParseStateTitle:
if (matchVersion.hasMatch()) { // unrar信息读取完,开始读压缩包相关信息
m_parseState = ParseStateArchiveInformation;
}
break;
case ParseStateArchiveInformation: // // 读取压缩包信息
if (line.startsWith(QLatin1String("Archive:"))) { // 压缩包全路径
// 压缩包注释信息
m_comment = m_comment.trimmed();
stArchiveData.strComment = m_comment;
// 通过压缩包全路径,获取压缩包大小
QFileInfo file(line.mid(9).trimmed());
stArchiveData.qComressSize = file.size();
} else if (line.startsWith(QLatin1String("Details:"))) {
// 读完压缩包信息,开始读内部文件信息
m_parseState = ParseStateEntryInformation;
} else {
// 读取注释信息
m_comment.append(line + QLatin1Char('\n'));
}
break;
case ParseStateEntryInformation: // 读压缩包内文件具体信息
// ':'左侧字符串
QString parseLineLeft = line.section(QLatin1Char(':'), 0, 0).trimmed();
// ':'右侧字符串
QString parseLineRight = line.section(QLatin1Char(':'), -1).trimmed();
if (parseLineLeft == QLatin1String("Name")) {
// 文件在压缩包内绝对路径路径
m_fileEntry.strFullPath = parseLineRight;
// 文件名称
const QStringList pieces = m_fileEntry.strFullPath.split(QLatin1Char('/'),
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QString::SkipEmptyParts
#else
Qt::SkipEmptyParts
#endif
);
m_fileEntry.strFileName = pieces.isEmpty() ? QString() : pieces.last();
} else if (parseLineLeft == QLatin1String("Type")) {
if (parseLineRight == QLatin1String("Directory")) {
// 是文件夹
m_fileEntry.isDirectory = true;
// 如果是文件夹且路径最后没有分隔符,手动添加
if (!m_fileEntry.strFullPath.endsWith(QLatin1Char('/'))) {
m_fileEntry.strFullPath = m_fileEntry.strFullPath + QLatin1Char('/');
}
} else {
// 不是文件夹
m_fileEntry.isDirectory = false;
}
} else if (parseLineLeft == QLatin1String("Size")) {
// 单文件实际大小
m_fileEntry.qSize = parseLineRight.toLongLong();
// 压缩包内所有文件总大小
stArchiveData.qSize += m_fileEntry.qSize;
} else if (parseLineLeft == QLatin1String("mtime")) {
QString time = line.left((line.length() - 10));
// 文件最后修改时间
m_fileEntry.uLastModifiedTime = QDateTime::fromString(time.right(time.length() - 14),
QStringLiteral("yyyy-MM-dd hh:mm:ss"))
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
.toTime_t();
#else
.toSecsSinceEpoch();
#endif
QString name = m_fileEntry.strFullPath;
handleEntry(m_fileEntry);
// 获取第一层数据
// if (!name.contains(QDir::separator()) || (name.count(QDir::separator()) == 1 && name.endsWith(QDir::separator()))) {
// stArchiveData.listRootEntry.push_back(m_fileEntry);
// }
// 存储总数据
stArchiveData.mapFileEntry.insert(name, m_fileEntry);
// clear m_fileEntry
m_fileEntry.reset();
}
break;
}
return true;
}
bool CliRarPlugin::handleLine(const QString &line, WorkType workStatus)
{
if (isPasswordPrompt(line)) { // 提示需要输入密码 提示密码错误
if (workStatus == WT_List) {
m_strEncryptedFileName = m_strArchiveName; // list时文件名为压缩包名
DataManager::get_instance().archiveData().isListEncrypted = true; // 列表加密文件
} else { // 解压时的文件名通过解析命令行获取
m_strEncryptedFileName = line.right(line.length() - 40).remove(": ");
}
m_eErrorType = ET_NeedPassword;
if (handlePassword() == PFT_Cancel) {
m_finishType = PFT_Cancel;
return false;
}
return true;
}
if (isWrongPasswordMsg(line)) { // 提示密码错误
m_eErrorType = ET_WrongPassword;
// RAR4密码错误直接结束
if (line.startsWith(QLatin1String("Checksum error in the encrypted file"))) {
m_finishType = PFT_Error;
return false;
} else { // RAR5密码错误:发送错误提示但不中断进程,允许用户重新输入密码
emit error(tr("Wrong password"), tr("The password entered is incorrect. Please try again."));
return true;
}
}
if (workStatus == WT_List) {
return readListLine(line); // 加载压缩文件,处理命令行内容
} else if (workStatus == WT_Extract) { // 解压进度
if (isMultiPasswordPrompt(line)) { // rar多密码文件解压提示是否使用上一次输入的密码
writeToProcess(QString("Y" + QLatin1Char('\n')).toLocal8Bit()); // 默认选择Y,使用上一次的密码
}
if (handleFileExists(line)) { // 判断解压是否存在同名文件
return true;
}
//-------文件名过长-------
if (isOpenFileFailed(line)) {
QByteArray diskPath = line.toLocal8Bit();
diskPath = diskPath.mid(QString("Cannot create ").length());
QList<QByteArray> entryNameList = diskPath.split('/');
foreach (auto &tmp, entryNameList) {
// 判断文件名是否过长
if (NAME_MAX < tmp.length()) {
m_finishType = PFT_Error;
m_eErrorType = ET_LongNameError;
emit signalCurFileName(diskPath);
return false;
}
}
}
// ------磁盘空间不足------
if (isDiskFullMsg(line)) {
if (line.startsWith(QLatin1String("Write error in the file"))) {
writeToProcess(QString("A" + QLatin1Char('\n')).toLocal8Bit()); // 默认选择A,终止解压
}
if (isInsufficientDiskSpace(getTargetPath(), 10 * 1024 * 1024)) { // 暂取小于10M作为磁盘空间不足的判断标准
m_eErrorType = ET_InsufficientDiskSpace;
emit signalFinished(PFT_Error);
return false;
}
}
handleProgress(line); // 处理进度
}
return true;
}