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 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
|
/*
* 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>
*
* 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 "kerfuffle/kerfuffle_export.h"
#include <QDateTime>
#include <QRegularExpression>
#include <KPluginFactory>
using namespace Kerfuffle;
K_PLUGIN_FACTORY_WITH_JSON(CliPluginFactory, "kerfuffle_clirar.json", registerPlugin<CliPlugin>();)
CliPlugin::CliPlugin(QObject *parent, const QVariantList& args)
: CliInterface(parent, args)
, m_parseState(ParseStateTitle)
, m_isUnrar5(false)
, m_isPasswordProtected(false)
, m_isSolid(false)
, m_remainingIgnoreLines(1) //The first line of UNRAR output is empty.
, m_linesComment(0)
{
qCDebug(ARK) << "Loaded cli_rar plugin";
// Empty lines are needed for parsing output of unrar.
setListEmptyLines(true);
}
CliPlugin::~CliPlugin()
{
}
void CliPlugin::resetParsing()
{
m_parseState = ParseStateTitle;
m_remainingIgnoreLines = 1;
m_comment.clear();
m_numberOfVolumes = 0;
}
// #272281: the proprietary unrar program does not like trailing '/'s
// in directories passed to it when extracting only part of
// the files in an archive.
QString CliPlugin::escapeFileName(const QString &fileName) const
{
if (fileName.endsWith(QLatin1Char('/'))) {
return fileName.left(fileName.length() - 1);
}
return fileName;
}
ParameterList CliPlugin::parameterList() const
{
static ParameterList p;
if (p.isEmpty()) {
p[CaptureProgress] = true;
p[ListProgram] = p[ExtractProgram] = p[TestProgram] = QStringList() << QStringLiteral( "unrar" );
p[DeleteProgram] = p[AddProgram] = QStringList() << QStringLiteral( "rar" );
p[ListArgs] = QStringList() << QStringLiteral("vt")
<< QStringLiteral("-v")
<< QStringLiteral("$PasswordSwitch")
<< QStringLiteral("$Archive");
p[ExtractArgs] = QStringList() << QStringLiteral( "-kb" )
<< QStringLiteral( "-p-" )
<< QStringLiteral( "$PreservePathSwitch" )
<< QStringLiteral( "$PasswordSwitch" )
<< QStringLiteral( "$Archive" )
<< QStringLiteral( "$Files" );
p[PreservePathSwitch] = QStringList() << QStringLiteral( "x" )
<< QStringLiteral( "e" );
p[PasswordSwitch] = QStringList() << QStringLiteral( "-p$Password" );
p[PasswordHeaderSwitch] = QStringList() << QStringLiteral("-hp$Password");
p[CompressionLevelSwitch] = QStringLiteral("-m$CompressionLevel");
p[MultiVolumeSwitch] = QStringLiteral("-v$VolumeSizek");
p[DeleteArgs] = QStringList() << QStringLiteral( "d" )
<< QStringLiteral( "$PasswordSwitch" )
<< QStringLiteral( "$Archive" )
<< QStringLiteral( "$Files" );
p[FileExistsExpression] = QStringList()
<< QStringLiteral("^\\[Y\\]es, \\[N\\]o, \\[A\\]ll, n\\[E\\]ver, \\[R\\]ename, \\[Q\\]uit $");
p[FileExistsFileName] = QStringList() << QStringLiteral("^(.+) already exists. Overwrite it") // unrar 3 & 4
<< QStringLiteral("^Would you like to replace the existing file (.+)$"); // unrar 5
p[FileExistsInput] = QStringList() << QStringLiteral( "Y" ) //overwrite
<< QStringLiteral( "N" ) //skip
<< QStringLiteral( "A" ) //overwrite all
<< QStringLiteral( "E" ) //autoskip
<< QStringLiteral( "Q" ); //cancel
p[AddArgs] = QStringList() << QStringLiteral( "a" )
<< QStringLiteral( "$Archive" )
<< QStringLiteral("$PasswordSwitch")
<< QStringLiteral("$CompressionLevelSwitch")
<< QStringLiteral("$MultiVolumeSwitch")
<< QStringLiteral( "$Files" );
p[PasswordPromptPattern] = QLatin1String("Enter password \\(will not be echoed\\) for");
p[WrongPasswordPatterns] = QStringList() << QStringLiteral("password incorrect") << QStringLiteral("wrong password");
p[ExtractionFailedPatterns] = QStringList() << QStringLiteral( "CRC failed" )
<< QStringLiteral( "Cannot find volume" );
p[CorruptArchivePatterns] = QStringList() << QStringLiteral("Unexpected end of archive")
<< QStringLiteral("the file header is corrupt");
p[DiskFullPatterns] = QStringList() << QStringLiteral("No space left on device");
p[CommentArgs] = QStringList() << QStringLiteral("c")
<< QStringLiteral("$CommentSwitch")
<< QStringLiteral("$Archive");
p[CommentSwitch] = QStringLiteral("-z$CommentFile");
p[TestArgs] = QStringList() << QStringLiteral("t")
<< QStringLiteral("$Archive")
<< QStringLiteral("$PasswordSwitch");
p[TestPassedPattern] = QStringLiteral("^All OK$");
// rar will sometimes create multi-volume archives where first volume is
// called name.part1.rar and other times name.part01.rar.
p[MultiVolumeSuffix] = QStringList() << QStringLiteral("part01.$Suffix")
<< QStringLiteral("part1.$Suffix");
}
return p;
}
bool CliPlugin::readListLine(const QString &line)
{
// Ignore number of lines corresponding to m_remainingIgnoreLines.
if (m_remainingIgnoreLines > 0) {
--m_remainingIgnoreLines;
return true;
}
// Parse the title line, which contains the version of unrar.
if (m_parseState == ParseStateTitle) {
QRegularExpression rxVersionLine(QStringLiteral("^UNRAR (\\d+\\.\\d+)( beta \\d)? .*$"));
QRegularExpressionMatch matchVersion = rxVersionLine.match(line);
if (matchVersion.hasMatch()) {
m_parseState = ParseStateComment;
QString unrarVersion = matchVersion.captured(1);
qCDebug(ARK) << "UNRAR version" << unrarVersion << "detected";
if (unrarVersion.toFloat() >= 5) {
m_isUnrar5 = true;
qCDebug(ARK) << "Using UNRAR 5 parser";
} else {
qCDebug(ARK) << "Using UNRAR 4 parser";
}
} else {
// If the second line doesn't contain an UNRAR title, something
// is wrong.
qCCritical(ARK) << "Failed to detect UNRAR output.";
return false;
}
// Or see what version of unrar we are dealing with and call specific
// handler functions.
} else if (m_isUnrar5) {
handleUnrar5Line(line);
} else {
handleUnrar4Line(line);
}
return true;
}
void CliPlugin::handleUnrar5Line(const QString &line) {
// Parses the comment field.
if (m_parseState == ParseStateComment) {
// RegExp matching end of comment field.
// FIXME: Comment itself could also contain the Archive path string here.
QRegularExpression rxCommentEnd(QStringLiteral("^Archive: .+$"));
if (rxCommentEnd.match(line).hasMatch()) {
m_parseState = ParseStateHeader;
m_comment = m_comment.trimmed();
m_linesComment = m_comment.count(QLatin1Char('\n')) + 1;
if (!m_comment.isEmpty()) {
qCDebug(ARK) << "Found a comment with" << m_linesComment << "lines";
}
} else {
m_comment.append(line + QLatin1Char('\n'));
}
return;
}
// Parses the header, which is whatever is between the comment field
// and the entries.
else if (m_parseState == ParseStateHeader) {
// "Details: " indicates end of header.
if (line.startsWith(QStringLiteral("Details: "))) {
ignoreLines(1, ParseStateEntryDetails);
if (line.contains(QLatin1String("volume"))) {
m_numberOfVolumes++;
if (!isMultiVolume()) {
setMultiVolume(true);
qCDebug(ARK) << "Multi-volume archive detected";
}
}
if (line.contains(QLatin1String("solid")) && !m_isSolid) {
m_isSolid = true;
qCDebug(ARK) << "Solid archive detected";
}
}
return;
}
// Parses the entry details for each entry.
else if (m_parseState == ParseStateEntryDetails) {
// For multi-volume archives there is a header between the entries in
// each volume.
if (line.startsWith(QLatin1String("Archive: "))) {
m_parseState = ParseStateHeader;
return;
// Empty line indicates end of entry.
} else if (line.trimmed().isEmpty() && !m_unrar5Details.isEmpty()) {
handleUnrar5Entry();
} else {
// All detail lines should contain a colon.
if (!line.contains(QLatin1Char(':'))) {
qCWarning(ARK) << "Unrecognized line:" << line;
return;
}
// The details are on separate lines, so we store them in the QHash
// m_unrar5Details.
m_unrar5Details.insert(line.section(QLatin1Char(':'), 0, 0).trimmed().toLower(),
line.section(QLatin1Char(':'), 1).trimmed());
}
return;
}
}
void CliPlugin::handleUnrar5Entry() {
ArchiveEntry e;
QString compressionRatio = m_unrar5Details.value(QStringLiteral("ratio"));
compressionRatio.chop(1); // Remove the '%'
e[Ratio] = compressionRatio;
QString time = m_unrar5Details.value(QStringLiteral("mtime"));
QDateTime ts = QDateTime::fromString(time, QStringLiteral("yyyy-MM-dd HH:mm:ss,zzz"));
e[Timestamp] = ts;
bool isDirectory = (m_unrar5Details.value(QStringLiteral("type")) == QLatin1String("Directory"));
e[IsDirectory] = isDirectory;
if (isDirectory && !m_unrar5Details.value(QStringLiteral("name")).endsWith(QLatin1Char('/'))) {
m_unrar5Details[QStringLiteral("name")] += QLatin1Char('/');
}
QString compression = m_unrar5Details.value(QStringLiteral("compression"));
int optionPos = compression.indexOf(QLatin1Char('-'));
if (optionPos != -1) {
e[Method] = compression.mid(optionPos);
e[Version] = compression.left(optionPos).trimmed();
} else {
// No method specified.
e[Method].clear();
e[Version] = compression;
}
m_isPasswordProtected = m_unrar5Details.value(QStringLiteral("flags")).contains(QStringLiteral("encrypted"));
e[IsPasswordProtected] = m_isPasswordProtected;
e[FileName] = m_unrar5Details.value(QStringLiteral("name"));
e[InternalID] = m_unrar5Details.value(QStringLiteral("name"));
e[Size] = m_unrar5Details.value(QStringLiteral("size"));
e[CompressedSize] = m_unrar5Details.value(QStringLiteral("packed size"));
e[Permissions] = m_unrar5Details.value(QStringLiteral("attributes"));
e[CRC] = m_unrar5Details.value(QStringLiteral("crc32"));
if (e[Permissions].toString().startsWith(QLatin1Char('l'))) {
e[Link] = m_unrar5Details.value(QStringLiteral("target"));
}
m_unrar5Details.clear();
emit entry(e);
}
void CliPlugin::handleUnrar4Line(const QString &line) {
// Parses the comment field.
if (m_parseState == ParseStateComment) {
// RegExp matching end of comment field.
// FIXME: Comment itself could also contain the Archive path string here.
QRegularExpression rxCommentEnd(QStringLiteral("^(Solid archive|Archive|Volume) .+$"));
if (rxCommentEnd.match(line).hasMatch()) {
if (line.startsWith(QLatin1String("Volume "))) {
m_numberOfVolumes++;
if (!isMultiVolume()) {
setMultiVolume(true);
qCDebug(ARK) << "Multi-volume archive detected";
}
}
if (line.startsWith(QLatin1String("Solid archive")) && !m_isSolid) {
m_isSolid = true;
qCDebug(ARK) << "Solid archive detected";
}
m_parseState = ParseStateHeader;
m_comment = m_comment.trimmed();
m_linesComment = m_comment.count(QLatin1Char('\n')) + 1;
if (!m_comment.isEmpty()) {
qCDebug(ARK) << "Found a comment with" << m_linesComment << "lines";
}
} else {
m_comment.append(line + QLatin1Char('\n'));
}
return;
}
// Parses the header, which is whatever is between the comment field
// and the entries.
else if (m_parseState == ParseStateHeader) {
// Horizontal line indicates end of header.
if (line.startsWith(QStringLiteral("--------------------"))) {
m_parseState = ParseStateEntryFileName;
} else if (line.startsWith(QLatin1String("Volume "))) {
m_numberOfVolumes++;
}
return;
}
// Parses the entry name, which is on the first line of each entry.
else if (m_parseState == ParseStateEntryFileName) {
// Ignore empty lines.
if (line.trimmed().isEmpty()) {
return;
}
// Three types of subHeaders can be displayed for unrar 3 and 4.
// STM has 4 lines, RR has 3, and CMT has lines corresponding to
// length of comment field +3. We ignore the subheaders.
QRegularExpression rxSubHeader(QStringLiteral("^Data header type: (CMT|STM|RR)$"));
QRegularExpressionMatch matchSubHeader = rxSubHeader.match(line);
if (matchSubHeader.hasMatch()) {
qCDebug(ARK) << "SubHeader of type" << matchSubHeader.captured(1) << "found";
if (matchSubHeader.captured(1) == QLatin1String("STM")) {
ignoreLines(4, ParseStateEntryFileName);
} else if (matchSubHeader.captured(1) == QLatin1String("CMT")) {
ignoreLines(m_linesComment + 3, ParseStateEntryFileName);
} else if (matchSubHeader.captured(1) == QLatin1String("RR")) {
ignoreLines(3, ParseStateEntryFileName);
}
return;
}
// The entries list ends with a horizontal line, followed by a
// single summary line or, for multi-volume archives, another header.
if (line.startsWith(QStringLiteral("-----------------"))) {
m_parseState = ParseStateHeader;
return;
// Encrypted files are marked with an asterisk.
} else if (line.startsWith(QLatin1Char('*'))) {
m_isPasswordProtected = true;
m_unrar4Details.append(QString(line.trimmed()).remove(0, 1)); //Remove the asterisk
// Entry names always start at the second position, so a line not
// starting with a space is not an entry name.
} else if (!line.startsWith(QLatin1Char(' '))) {
qCWarning(ARK) << "Unrecognized line:" << line;
return;
// If we reach this, then we can assume the line is an entry name, so
// save it, and move on to the rest of the entry details.
} else {
m_unrar4Details.append(line.trimmed());
}
m_parseState = ParseStateEntryDetails;
return;
}
// Parses the remainder of the entry details for each entry.
else if (m_parseState == ParseStateEntryDetails) {
// If the line following an entry name is empty, we did something
// wrong.
Q_ASSERT(!line.trimmed().isEmpty());
// If we reach a horizontal line, then the previous line was not an
// entry name, so go back to header.
if (line.startsWith(QStringLiteral("-----------------"))) {
m_parseState = ParseStateHeader;
return;
}
// In unrar 3 and 4 the details are on a single line, so we
// pass a QStringList containing the details. We need to store
// it due to symlinks (see below).
m_unrar4Details.append(line.split(QLatin1Char(' '),
QString::SkipEmptyParts));
// The details line contains 9 fields, so m_unrar4Details
// should now contain 9 + the filename = 10 strings. If not, this is
// not an archive entry.
if (m_unrar4Details.size() != 10) {
m_parseState = ParseStateHeader;
return;
}
// When unrar 3 and 4 list a symlink, they output an extra line
// containing the link target. The extra line is output after
// the line we ignore, so we first need to ignore one line.
if (m_unrar4Details.at(6).startsWith(QLatin1Char('l'))) {
ignoreLines(1, ParseStateLinkTarget);
return;
} else {
handleUnrar4Entry();
}
// Unrar 3 & 4 show a third line for each entry, which contains
// three details: Host OS, Solid, and Old. We can ignore this
// line.
ignoreLines(1, ParseStateEntryFileName);
return;
}
// Parses a symlink target.
else if (m_parseState == ParseStateLinkTarget) {
m_unrar4Details.append(QString(line).remove(QStringLiteral("-->")).trimmed());
handleUnrar4Entry();
m_parseState = ParseStateEntryFileName;
return;
}
}
void CliPlugin::handleUnrar4Entry() {
ArchiveEntry e;
QDateTime ts = QDateTime::fromString(QString(m_unrar4Details.at(4) + QLatin1Char(' ') + m_unrar4Details.at(5)),
QStringLiteral("dd-MM-yy hh:mm"));
// Unrar 3 & 4 output dates with a 2-digit year but QDateTime takes it as
// 19??. Let's take 1950 as cut-off; similar to KDateTime.
if (ts.date().year() < 1950) {
ts = ts.addYears(100);
}
e[Timestamp] = ts;
bool isDirectory = ((m_unrar4Details.at(6).at(0) == QLatin1Char('d')) ||
(m_unrar4Details.at(6).at(1) == QLatin1Char('D')));
e[IsDirectory] = isDirectory;
if (isDirectory && !m_unrar4Details.at(0).endsWith(QLatin1Char('/'))) {
m_unrar4Details[0] += QLatin1Char('/');
}
// Unrar reports the ratio as ((compressed size * 100) / size);
// we consider ratio as (100 * ((size - compressed size) / size)).
// If the archive is a multivolume archive, a string indicating
// whether the archive's position in the volume is displayed
// instead of the compression ratio.
QString compressionRatio = m_unrar4Details.at(3);
if ((compressionRatio == QStringLiteral("<--")) ||
(compressionRatio == QStringLiteral("<->")) ||
(compressionRatio == QStringLiteral("-->"))) {
compressionRatio = QLatin1Char('0');
} else {
compressionRatio.chop(1); // Remove the '%'
}
e[Ratio] = compressionRatio;
// TODO:
// - Permissions differ depending on the system the entry was added
// to the archive.
e[FileName] = m_unrar4Details.at(0);
e[InternalID] = m_unrar4Details.at(0);
e[Size] = m_unrar4Details.at(1);
e[CompressedSize] = m_unrar4Details.at(2);
e[Permissions] = m_unrar4Details.at(6);
e[CRC] = m_unrar4Details.at(7);
e[Method] = m_unrar4Details.at(8);
e[Version] = m_unrar4Details.at(9);
e[IsPasswordProtected] = m_isPasswordProtected;
if (e[Permissions].toString().startsWith(QLatin1Char('l'))) {
e[Link] = m_unrar4Details.at(10);
}
m_unrar4Details.clear();
emit entry(e);
}
void CliPlugin::ignoreLines(int lines, ParseState nextState) {
m_remainingIgnoreLines = lines;
m_parseState = nextState;
}
#include "cliplugin.moc"
|