cliinterface.cpp 35.5 KB
Newer Older
1 2 3
/*
 * ark -- archiver for the KDE project
 *
4
 * Copyright (C) 2009 Harald Hvaal <haraldhv@stud.ntnu.no>
5
 * Copyright (C) 2009-2011 Raphael Kubo da Costa <rakuco@FreeBSD.org>
6
 *
7 8 9
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
10
 *
11 12 13 14 15
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
16
 *
17 18 19 20 21 22 23 24 25 26
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES ( INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION ) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * ( INCLUDING NEGLIGENCE OR OTHERWISE ) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 28 29
 */

#include "cliinterface.h"
30
#include "ark_debug.h"
31
#include "queries.h"
32

33 34 35 36 37 38 39
#ifdef Q_OS_WIN
# include <KProcess>
#else
# include <KPtyDevice>
# include <KPtyProcess>
#endif

40
#include <KLocalizedString>
41 42 43

#include <QApplication>
#include <QDateTime>
Andrius Štikonas's avatar
Andrius Štikonas committed
44
#include <QDebug>
45
#include <QDir>
46
#include <QDirIterator>
47
#include <QEventLoop>
48
#include <QFile>
49
#include <QProcess>
50 51 52
#include <QRegularExpression>
#include <QStandardPaths>
#include <QTemporaryDir>
53
#include <QThread>
54
#include <QTimer>
55
#include <QUrl>
56 57 58

namespace Kerfuffle
{
59 60
CliInterface::CliInterface(QObject *parent, const QVariantList & args)
        : ReadWriteArchiveInterface(parent, args),
Christoph Feck's avatar
Christoph Feck committed
61
        m_process(0),
62 63
        m_listEmptyLines(false),
        m_abortingOperation(false)
64 65 66
{
    //because this interface uses the event loop
    setWaitForFinishedSignal(true);
67 68 69 70

    if (QMetaType::type("QProcess::ExitStatus") == 0) {
        qRegisterMetaType<QProcess::ExitStatus>("QProcess::ExitStatus");
    }
71 72 73 74 75 76 77 78 79 80 81 82 83 84
}

void CliInterface::cacheParameterList()
{
    m_param = parameterList();
    Q_ASSERT(m_param.contains(ExtractProgram));
    Q_ASSERT(m_param.contains(ListProgram));
    Q_ASSERT(m_param.contains(PreservePathSwitch));
    Q_ASSERT(m_param.contains(FileExistsExpression));
    Q_ASSERT(m_param.contains(FileExistsInput));
}

CliInterface::~CliInterface()
{
85
    Q_ASSERT(!m_process);
86 87
}

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
bool CliInterface::isCliBased() const
{
    return true;
}

bool CliInterface::findExecutables(bool isReadWrite)
{
    cacheParameterList();

    QList<int> execTypes = QList<int>() << ListProgram << ExtractProgram;
    if (isReadWrite) {
        execTypes << AddProgram << DeleteProgram;
    }

    foreach (const int execType, execTypes) {
        bool execTypeFound = false;
        foreach (const QString &program, m_param.value(execType).toStringList()) {
            if (!QStandardPaths::findExecutable(program).isEmpty()) {
106
                qCDebug(ARK) << "Found executable type" << execType << ":" << program;
107 108 109 110 111 112 113 114 115 116 117 118
                execTypeFound = true;
                break;
            }
        }
        if (!execTypeFound) {
            return false;
        }
    }
    return true;
}


Christoph Feck's avatar
Christoph Feck committed
119 120 121 122 123
void CliInterface::setListEmptyLines(bool emptyLines)
{
    m_listEmptyLines = emptyLines;
}

124 125
bool CliInterface::list()
{
126
    resetParsing();
127
    cacheParameterList();
128
    m_operationMode = List;
129

130
    const auto args = substituteListVariables(m_param.value(ListArgs).toStringList(), password());
131

132
    if (!runProcess(m_param.value(ListProgram).toStringList(), args)) {
133 134 135
        failOperation();
        return false;
    }
136

137
    emit finished(true);
138 139 140
    return true;
}

141
bool CliInterface::copyFiles(const QVariantList &files, const QString &destinationDirectory, const ExtractionOptions &options)
142
{
143
    qCDebug(ARK) << Q_FUNC_INFO << "to" << destinationDirectory;
144

145
    cacheParameterList();
146
    m_operationMode = Copy;
147
    const QStringList extractArgs = m_param.value(ExtractArgs).toStringList();
148

149 150 151 152 153 154
    if (extractArgs.contains(QStringLiteral("$PasswordSwitch")) &&
        options.value(QStringLiteral("PasswordProtectedHint")).toBool() &&
        password().isEmpty()) {
        qCDebug(ARK) << "Password hint enabled, querying user";
        if (!passwordQuery()) {
            return false;
155 156 157
        }
    }

158 159 160 161 162 163 164
    // Populate the argument list.
    const QStringList args = substituteCopyVariables(extractArgs,
                                                     files,
                                                     options.value(QStringLiteral("PreservePaths")).toBool(),
                                                     password(),
                                                     options.value(QStringLiteral("RootNode"), QString()).toString());

165 166 167 168
    QUrl destDir = QUrl(destinationDirectory);
    QDir::setCurrent(destDir.adjusted(QUrl::RemoveScheme).url());

    QString oldCurrentDir;
169 170
    bool useTmpExtractDir = options.value(QStringLiteral("DragAndDrop")).toBool() ||
                            options.value(QStringLiteral("AlwaysUseTmpDir")).toBool();
171
    QTemporaryDir tmpExtractDir(QApplication::applicationName() + QLatin1Char('-'));
172 173

    if (useTmpExtractDir) {
174
        qCDebug(ARK) << "Using temporary extraction dir:" << tmpExtractDir.path();
175
        if (!tmpExtractDir.isValid()) {
176
            qCDebug(ARK) << "Creation of temporary directory failed.";
177 178 179
            failOperation();
            return false;
        }
180
        oldCurrentDir = QDir::currentPath();
181
        destDir = QUrl(tmpExtractDir.path());
182
        QDir::setCurrent(destDir.adjusted(QUrl::RemoveScheme).url());
183
    }
184

185
    if (!runProcess(m_param.value(ExtractProgram).toStringList(), args)) {
186 187 188
        failOperation();
        return false;
    }
189

190
    if (options.value(QStringLiteral("AlwaysUseTmpDir")).toBool()) {
191 192
        // unar exits with code 1 if extraction fails.
        // This happens at least with wrong passwords or not enough space in the destination folder.
193
        if (m_exitCode == 1) {
194 195 196 197 198 199 200 201 202
            if (password().isEmpty()) {
                qCWarning(ARK) << "Extraction aborted, destination folder might not have enough space.";
                emit error(i18n("Extraction failed. Make sure that enough space is available."));
            } else {
                qCWarning(ARK) << "Extraction aborted, either the password is wrong or the destination folder doesn't have enough space.";
                emit error(i18n("Extraction failed. Make sure you provided the correct password and that enough space is available."));
                setPassword(QString());
            }

203 204
            emit finished(false);
            failOperation();
205 206 207
            // If we don't do this, the temporary directory will not autodelete itself upon destruction.
            QDir::setCurrent(oldCurrentDir);

208 209 210 211
            return false;
        }

        if (!options.value(QStringLiteral("DragAndDrop")).toBool()) {
212
            if (!moveToDestination(QDir::current(), QDir(destinationDirectory), options[QStringLiteral("PreservePaths")].toBool())) {
213 214 215 216 217 218 219
                emit error(i18ncp("@info",
                                  "Could not move the extracted file to the destination directory.",
                                  "Could not move the extracted files to the destination directory.",
                                  files.size()));
                emit finished(false);
                return false;
            }
220 221
            // If we don't do this, the temporary directory will not autodelete itself upon destruction.
            QDir::setCurrent(oldCurrentDir);
222 223 224 225
        }
    }

    if (options.value(QStringLiteral("DragAndDrop")).toBool()) {
226
        if (!moveDroppedFilesToDest(files, destinationDirectory)) {
227 228 229 230 231 232 233
            emit error(i18ncp("@info",
                              "Could not move the extracted file to the destination directory.",
                              "Could not move the extracted files to the destination directory.",
                              files.size()));
            emit finished(false);
            return false;
        }
234 235
        // If we don't do this, the temporary directory will not autodelete itself upon destruction.
        QDir::setCurrent(oldCurrentDir);
236 237 238
    }

    emit finished(true);
239 240 241 242 243 244 245
    return true;
}

bool CliInterface::addFiles(const QStringList & files, const CompressionOptions& options)
{
    cacheParameterList();

246
    m_operationMode = Add;
247

248 249
    int compLevel = options.value(QStringLiteral("CompressionLevel"), -1).toInt();

250 251 252
    const auto args = substituteAddVariables(m_param.value(AddArgs).toStringList(),
                                             files,
                                             password(),
253
                                             isHeaderEncryptionEnabled(),
254
                                             compLevel);
255

256
    if (!runProcess(m_param.value(AddProgram).toStringList(), args)) {
257 258 259
        failOperation();
        return false;
    }
260
    emit finished(true);
261 262 263 264 265 266
    return true;
}

bool CliInterface::deleteFiles(const QList<QVariant> & files)
{
    cacheParameterList();
267
    m_operationMode = Delete;
268 269 270 271 272 273 274

    //start preparing the argument list
    QStringList args = m_param.value(DeleteArgs).toStringList();

    //now replace the various elements in the list
    for (int i = 0; i < args.size(); ++i) {
        QString argument = args.at(i);
275
        qCDebug(ARK) << "Processing argument " << argument;
276

277
        if (argument == QLatin1String( "$Archive" )) {
278
            args[i] = filename();
Raphael Kubo da Costa's avatar
Raphael Kubo da Costa committed
279
        } else if (argument == QLatin1String( "$Files" )) {
280 281
            args.removeAt(i);
            for (int j = 0; j < files.count(); ++j) {
282
                args.insert(i + j, escapeFileName(files.at(j).toString()));
283 284 285 286 287 288 289 290
                ++i;
            }
            --i;
        }
    }

    m_removedFiles = files;

291
    if (!runProcess(m_param.value(DeleteProgram).toStringList(), args)) {
292 293 294
        failOperation();
        return false;
    }
295
    emit finished(true);
296 297 298
    return true;
}

299
bool CliInterface::runProcess(const QStringList& programNames, const QStringList& arguments)
300
{
301 302
    QString programPath;
    for (int i = 0; i < programNames.count(); i++) {
303
        programPath = QStandardPaths::findExecutable(programNames.at(i));
304 305 306
        if (!programPath.isEmpty())
            break;
    }
307
    if (programPath.isEmpty()) {
308
        const QString names = programNames.join(QStringLiteral(", "));
309 310
        emit error(xi18ncp("@info", "Failed to locate program <filename>%2</filename> on disk.",
                           "Failed to locate programs <filename>%2</filename> on disk.", programNames.count(), names));
311
        emit finished(false);
312 313
        return false;
    }
314

315
    qCDebug(ARK) << "Executing" << programPath << arguments << "within directory" << QDir::currentPath();
316

317 318 319 320 321
    if (m_process) {
        m_process->waitForFinished();
        delete m_process;
    }

322
#ifdef Q_OS_WIN
323
    m_process = new KProcess;
324
#else
325
    m_process = new KPtyProcess;
326
    m_process->setPtyChannels(KPtyProcess::StdinChannel);
327
    QEventLoop loop;
Laurent Montel's avatar
Laurent Montel committed
328
    connect(m_process, static_cast<void (KPtyProcess::*)(int, QProcess::ExitStatus)>(&KPtyProcess::finished), &loop, &QEventLoop::quit, Qt::DirectConnection);
329 330
#endif

331
    m_process->setOutputChannelMode(KProcess::MergedChannels);
332
    m_process->setNextOpenMode(QIODevice::ReadWrite | QIODevice::Unbuffered | QIODevice::Text);
333
    m_process->setProgram(programPath, arguments);
334

335
    connect(m_process, SIGNAL(readyReadStandardOutput()), SLOT(readStdout()), Qt::DirectConnection);
Laurent Montel's avatar
Laurent Montel committed
336
    connect(m_process, static_cast<void (KPtyProcess::*)(int, QProcess::ExitStatus)>(&KPtyProcess::finished), this, &CliInterface::processFinished, Qt::DirectConnection);
337

338
    m_stdOutData.clear();
339

340
    m_process->start();
341 342 343 344

#ifdef Q_OS_WIN
    bool ret = m_process->waitForFinished(-1);
#else
345
    bool ret = (loop.exec(QEventLoop::WaitForMoreEvents | QEventLoop::ExcludeUserInputEvents) == 0);
346
#endif
347

348
    Q_ASSERT(!m_process);
349

350
    return ret;
351 352 353 354
}

void CliInterface::processFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
355
    m_exitCode = exitCode;
356
    qCDebug(ARK) << "Process finished, exitcode:" << exitCode << "exitstatus:" << exitStatus;
357 358 359

    //if the m_process pointer is gone, then there is nothing to worry
    //about here
Raphael Kubo da Costa's avatar
Raphael Kubo da Costa committed
360
    if (!m_process) {
361
        return;
Raphael Kubo da Costa's avatar
Raphael Kubo da Costa committed
362
    }
363

364
    if (m_operationMode == Delete) {
365
        foreach(const QVariant& v, m_removedFiles) {
366
            emit entryRemoved(v.toString());
367 368 369 370 371 372
        }
    }

    //handle all the remaining data in the process
    readStdout(true);

373 374 375
    delete m_process;
    m_process = 0;

376
    emit progress(1.0);
377

378
    if (m_operationMode == Add) {
379 380 381
        list();
        return;
    }
382
}
383

384
bool CliInterface::moveDroppedFilesToDest(const QVariantList &files, const QString &finalDest)
385 386 387 388
{
    // Move extracted files from a QTemporaryDir to the final destination.

    QDir finalDestDir(finalDest);
389
    qCDebug(ARK) << "Setting final dir to" << finalDest;
390 391 392 393 394 395 396

    bool overwriteAll = false;
    bool skipAll = false;

    foreach (const QVariant& file, files) {

        QFileInfo relEntry(file.value<fileRootNodePair>().file.remove(file.value<fileRootNodePair>().rootNode));
397
        QFileInfo absSourceEntry(QDir::current().absolutePath() + QLatin1Char('/') + file.value<fileRootNodePair>().file);
398 399 400 401 402 403
        QFileInfo absDestEntry(finalDestDir.path() + QLatin1Char('/') + relEntry.filePath());

        if (absSourceEntry.isDir()) {

            // For directories, just create the path.
            if (!finalDestDir.mkpath(relEntry.filePath())) {
404
                qCWarning(ARK) << "Failed to create directory" << relEntry.filePath() << "in final destination.";
405 406 407 408 409 410
            }

        } else {

            // If destination file exists, prompt the user.
            if (absDestEntry.exists()) {
411
                qCWarning(ARK) << "File" << absDestEntry.absoluteFilePath() << "exists.";
412 413 414 415 416 417 418 419 420 421 422 423 424

                if (!skipAll && !overwriteAll) {

                    Kerfuffle::OverwriteQuery query(absDestEntry.absoluteFilePath());
                    query.setNoRenameMode(true);
                    emit userQuery(&query);
                    query.waitForResponse();

                    if (query.responseOverwrite() || query.responseOverwriteAll()) {
                        if (query.responseOverwriteAll()) {
                            overwriteAll = true;
                        }
                        if (!QFile::remove(absDestEntry.absoluteFilePath())) {
425
                            qCWarning(ARK) << "Failed to remove" << absDestEntry.absoluteFilePath();
426 427 428 429 430 431 432 433 434
                        }

                    } else if (query.responseSkip() || query.responseAutoSkip()) {
                        if (query.responseAutoSkip()) {
                            skipAll = true;
                        }
                        continue;

                    } else if (query.responseCancelled()) {
435
                        qCDebug(ARK) << "Copy action cancelled.";
436 437 438 439 440 441 442 443 444
                        return false;
                    }

                } else if (skipAll) {
                    continue;

                } else if (overwriteAll) {

                    if (!QFile::remove(absDestEntry.absoluteFilePath())) {
445
                        qCWarning(ARK) << "Failed to remove" << absDestEntry.absoluteFilePath();
446 447 448 449 450 451 452
                    }
                }

            }

            // Create any parent directories.
            if (!finalDestDir.mkpath(relEntry.path())) {
Elvis Angelaccio's avatar
Elvis Angelaccio committed
453
                qCWarning(ARK) << "Failed to create parent directory for file:" << absDestEntry.filePath();
454 455 456 457
            }

            // Move files to the final destination.
            if (!QFile(absSourceEntry.absoluteFilePath()).rename(absDestEntry.absoluteFilePath())) {
458
                qCWarning(ARK) << "Failed to move file" << absSourceEntry.filePath() << "to final destination.";
459 460 461 462 463
                return false;
            }
        }
    }
    return true;
464
}
465

466 467 468 469 470 471 472 473
bool CliInterface::isEmptyDir(const QDir &dir)
{
    QDir d = dir;
    d.setFilter(QDir::AllEntries | QDir::NoDotAndDotDot);

    return d.count() == 0;
}

474
bool CliInterface::moveToDestination(const QDir &tempDir, const QDir &destDir, bool preservePaths)
475
{
476
    qCDebug(ARK) << "Moving extracted files from temp dir" << tempDir.path() << "to final destination" << destDir.path();
477 478 479 480 481 482 483 484

    bool overwriteAll = false;
    bool skipAll = false;

    QDirIterator dirIt(tempDir.path(), QDir::AllEntries | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);
    while (dirIt.hasNext()) {
        dirIt.next();

485 486 487 488
        // We skip directories if:
        // 1. We are not preserving paths
        // 2. The dir is not empty. Only empty directories need to be explicitly moved.
        // The non-empty ones are created by QDir::mkpath() below.
489
        if (dirIt.fileInfo().isDir()) {
490 491 492
            if (!preservePaths || !isEmptyDir(QDir(dirIt.filePath()))) {
                continue;
            }
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 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
        }

        QFileInfo relEntry;
        if (preservePaths) {
            relEntry = QFileInfo(dirIt.filePath().remove(tempDir.path() + QLatin1Char('/')));
        } else {
            relEntry = QFileInfo(dirIt.fileName());
        }

        QFileInfo absDestEntry(destDir.path() + QLatin1Char('/') + relEntry.filePath());

        if (absDestEntry.exists()) {
            qCWarning(ARK) << "File" << absDestEntry.absoluteFilePath() << "exists.";

            Kerfuffle::OverwriteQuery query(absDestEntry.absoluteFilePath());
            query.setNoRenameMode(true);
            emit userQuery(&query);
            query.waitForResponse();

            if (query.responseOverwrite() || query.responseOverwriteAll()) {
                if (query.responseOverwriteAll()) {
                    overwriteAll = true;
                }
                if (!QFile::remove(absDestEntry.absoluteFilePath())) {
                    qCWarning(ARK) << "Failed to remove" << absDestEntry.absoluteFilePath();
                }

            } else if (query.responseSkip() || query.responseAutoSkip()) {
                if (query.responseAutoSkip()) {
                    skipAll = true;
                }
                continue;
            } else if (query.responseCancelled()) {
                qCDebug(ARK) << "Copy action cancelled.";
                return false;
            }
        } else if (skipAll) {
            continue;
        } else if (overwriteAll) {
            if (!QFile::remove(absDestEntry.absoluteFilePath())) {
                qCWarning(ARK) << "Failed to remove" << absDestEntry.absoluteFilePath();
            }
        }

        if (preservePaths) {
            // Create any parent directories.
            if (!destDir.mkpath(relEntry.path())) {
                qCWarning(ARK) << "Failed to create parent directory for file:" << absDestEntry.filePath();
            }
        }

        // Move file to the final destination.
        if (!QFile(dirIt.filePath()).rename(absDestEntry.absoluteFilePath())) {
            qCWarning(ARK) << "Failed to move file" << dirIt.filePath() << "to final destination.";
            return false;
        }
    }

    return true;
}

554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
QStringList CliInterface::substituteListVariables(const QStringList &listArgs, const QString &password)
{
    // Required if we call this function from unit tests.
    cacheParameterList();

    QStringList args;
    foreach (const QString& arg, listArgs) {
        if (arg == QLatin1String("$Archive")) {
            args << filename();
            continue;
        }

        if (arg == QLatin1String("$PasswordSwitch")) {
            args << passwordSwitch(password);
            continue;
        }

        // Simple argument (e.g. -slt in 7z), nothing to substitute, just add it to the list.
        args << arg;
    }

    // Remove empty strings, if any.
    args.removeAll(QString());

    return args;
}

581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
QStringList CliInterface::substituteCopyVariables(const QStringList &extractArgs, const QVariantList &files, bool preservePaths, const QString &password, const QString &rootNode)
{
    // Required if we call this function from unit tests.
    cacheParameterList();

    QStringList args;
    foreach (const QString& arg, extractArgs) {
        qCDebug(ARK) << "Processing argument" << arg;

        if (arg == QLatin1String("$Archive")) {
            args << filename();
            continue;
        }

        if (arg == QLatin1String("$PreservePathSwitch")) {
            args << preservePathSwitch(preservePaths);
            continue;
        }

        if (arg == QLatin1String("$PasswordSwitch")) {
            args << passwordSwitch(password);
            continue;
        }

        if (arg == QLatin1String("$RootNodeSwitch")) {
            args << rootNodeSwitch(rootNode);
            continue;
        }

        if (arg == QLatin1String("$Files")) {
            args << copyFilesList(files);
            continue;
        }

        // Simple argument (e.g. -kb in unrar), nothing to substitute, just add it to the list.
        args << arg;
    }

    // Remove empty strings, if any.
    args.removeAll(QString());

    return args;
}

625
QStringList CliInterface::substituteAddVariables(const QStringList &addArgs, const QStringList &files, const QString &password, bool encryptHeader, int compLevel)
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
{
    // Required if we call this function from unit tests.
    cacheParameterList();

    QStringList args;
    foreach (const QString& arg, addArgs) {
        qCDebug(ARK) << "Processing argument " << arg;

        if (arg == QLatin1String("$Archive")) {
            args << filename();
            continue;
        }

        if (arg == QLatin1String("$PasswordSwitch")) {
            args << (encryptHeader ? passwordHeaderSwitch(password) : passwordSwitch(password));
            continue;
        }

644 645 646 647 648
        if (arg == QLatin1String("$CompressionLevelSwitch")) {
            args << compressionLevelSwitch(compLevel);
            continue;
        }

649
        if (arg == QLatin1String("$Files")) {
650
            args << files;
651 652 653 654 655 656 657 658 659 660 661 662 663
            continue;
        }

        // Simple argument (e.g. a in 7z), nothing to substitute, just add it to the list.
        args << arg;
    }

    // Remove empty strings, if any.
    args.removeAll(QString());

    return args;
}

664 665 666 667 668 669 670 671 672
QString CliInterface::preservePathSwitch(bool preservePaths) const
{
    Q_ASSERT(m_param.contains(PreservePathSwitch));
    const QStringList theSwitch = m_param.value(PreservePathSwitch).toStringList();
    Q_ASSERT(theSwitch.size() == 2);

    return (preservePaths ? theSwitch.at(0) : theSwitch.at(1));
}

673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
QStringList CliInterface::passwordHeaderSwitch(const QString& password) const
{
    if (password.isEmpty()) {
        return QStringList();
    }

    Q_ASSERT(m_param.contains(PasswordHeaderSwitch));

    QStringList passwordHeaderSwitch = m_param.value(PasswordHeaderSwitch).toStringList();
    Q_ASSERT(!passwordHeaderSwitch.isEmpty() && passwordHeaderSwitch.size() <= 2);

    if (passwordHeaderSwitch.size() == 1) {
        passwordHeaderSwitch[0].replace(QLatin1String("$Password"), password);
    } else {
        passwordHeaderSwitch[1] = password;
    }

    return passwordHeaderSwitch;
}

693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
QStringList CliInterface::passwordSwitch(const QString& password) const
{
    if (password.isEmpty()) {
        return QStringList();
    }

    Q_ASSERT(m_param.contains(PasswordSwitch));

    QStringList passwordSwitch = m_param.value(PasswordSwitch).toStringList();
    Q_ASSERT(!passwordSwitch.isEmpty() && passwordSwitch.size() <= 2);

    if (passwordSwitch.size() == 1) {
        passwordSwitch[0].replace(QLatin1String("$Password"), password);
    } else {
        passwordSwitch[1] = password;
    }

    return passwordSwitch;
711 712 713 714
}

QString CliInterface::compressionLevelSwitch(int level) const
{
715 716 717 718
    if (level < 0 || level > 9) {
        return QString();
    }

719 720 721 722 723 724 725 726
    Q_ASSERT(m_param.contains(CompressionLevelSwitch));

    QString compLevelSwitch = m_param.value(CompressionLevelSwitch).toString();
    Q_ASSERT(!compLevelSwitch.isEmpty());

    compLevelSwitch.replace(QLatin1String("$CompressionLevel"), QString::number(level));

    return compLevelSwitch;
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
}

QStringList CliInterface::rootNodeSwitch(const QString &rootNode) const
{
    if (rootNode.isEmpty()) {
        return QStringList();
    }

    Q_ASSERT(m_param.contains(RootNodeSwitch));

    QStringList rootNodeSwitch = m_param.value(RootNodeSwitch).toStringList();
    Q_ASSERT(!rootNodeSwitch.isEmpty() && rootNodeSwitch.size() <= 2);

    if (rootNodeSwitch.size() == 1) {
        rootNodeSwitch[0].replace(QLatin1String("$Path"), rootNode);
    } else {
        rootNodeSwitch[1] = rootNode;
    }

    return rootNodeSwitch;
}

QStringList CliInterface::copyFilesList(const QVariantList& files) const
{
    QStringList filesList;
    foreach (const QVariant& f, files) {
        filesList << escapeFileName(f.value<fileRootNodePair>().file);
    }

    return filesList;
}

759 760
void CliInterface::failOperation()
{
761
    // TODO: Would be good to unit test #304764/#304178.
762
    doKill();
763 764
}

765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
bool CliInterface::passwordQuery()
{
    Kerfuffle::PasswordNeededQuery query(filename());
    emit userQuery(&query);
    query.waitForResponse();

    if (query.responseCancelled()) {
        emit cancelled();
        // There is no process running, so finished() must be emitted manually.
        emit finished(false);
        failOperation();
        return false;
    }

    setPassword(query.password());
    return true;
}

783 784 785 786 787 788 789 790 791 792
void CliInterface::readStdout(bool handleAll)
{
    //when hacking this function, please remember the following:
    //- standard output comes in unpredictable chunks, this is why
    //you can never know if the last part of the output is a complete line or not
    //- console applications are not really consistent about what
    //characters they send out (newline, backspace, carriage return,
    //etc), so keep in mind that this function is supposed to handle
    //all those special cases and be the lowest common denominator

793 794 795
    if (m_abortingOperation)
        return;

796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811
    Q_ASSERT(m_process);

    if (!m_process->bytesAvailable()) {
        //if process has no more data, we can just bail out
        return;
    }

    QByteArray dd = m_process->readAllStandardOutput();
    m_stdOutData += dd;

    QList<QByteArray> lines = m_stdOutData.split('\n');

    //The reason for this check is that archivers often do not end
    //queries (such as file exists, wrong password) on a new line, but
    //freeze waiting for input. So we check for errors on the last line in
    //all cases.
812 813 814 815 816
    // TODO: QLatin1String() might not be the best choice here.
    //       The call to handleLine() at the end of the method uses
    //       QString::fromLocal8Bit(), for example.
    // TODO: The same check methods are called in handleLine(), this
    //       is suboptimal.
817

818 819
    bool wrongPasswordMessage = checkForErrorMessage(QLatin1String( lines.last() ), WrongPasswordPatterns);

820
    bool foundErrorMessage =
821
        (wrongPasswordMessage ||
822
         checkForErrorMessage(QLatin1String(lines.last()), DiskFullPatterns) ||
823
         checkForErrorMessage(QLatin1String(lines.last()), ExtractionFailedPatterns) ||
824
         checkForPasswordPromptMessage(QLatin1String(lines.last())) ||
825
         checkForErrorMessage(QLatin1String(lines.last()), FileExistsExpression));
826

Raphael Kubo da Costa's avatar
Raphael Kubo da Costa committed
827
    if (foundErrorMessage) {
828
        handleAll = true;
Raphael Kubo da Costa's avatar
Raphael Kubo da Costa committed
829
    }
830

831 832 833 834
    if (wrongPasswordMessage) {
        setPassword(QString());
    }

835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
    //this is complex, here's an explanation:
    //if there is no newline, then there is no guaranteed full line to
    //handle in the output. The exception is that it is supposed to handle
    //all the data, OR if there's been an error message found in the
    //partial data.
    if (lines.size() == 1 && !handleAll) {
        return;
    }

    if (handleAll) {
        m_stdOutData.clear();
    } else {
        //because the last line might be incomplete we leave it for now
        //note, this last line may be an empty string if the stdoutdata ends
        //with a newline
        m_stdOutData = lines.takeLast();
    }

    foreach(const QByteArray& line, lines) {
Christoph Feck's avatar
Christoph Feck committed
854
        if (!line.isEmpty() || (m_listEmptyLines && m_operationMode == List)) {
855
            handleLine(QString::fromLocal8Bit(line));
Raphael Kubo da Costa's avatar
Raphael Kubo da Costa committed
856
        }
857 858
    }
}
859

860 861
void CliInterface::handleLine(const QString& line)
{
862 863
    // TODO: This should be implemented by each plugin; the way progress is
    //       shown by each CLI application is subject to a lot of variation.
864
    if ((m_operationMode == Copy || m_operationMode == Add) && m_param.contains(CaptureProgress) && m_param.value(CaptureProgress).toBool()) {
865
        //read the percentage
866
        int pos = line.indexOf(QLatin1Char( '%' ));
867
        if (pos != -1 && pos > 1) {
868
            int percentage = line.midRef(pos - 2, 2).toInt();
869
            emit progress(float(percentage) / 100);
870 871 872 873
            return;
        }
    }

874
    if (m_operationMode == Copy) {
875

876
        if (checkForPasswordPromptMessage(line)) {
877
            qCDebug(ARK) << "Found a password prompt";
878 879

            Kerfuffle::PasswordNeededQuery query(filename());
880
            emit userQuery(&query);
881 882 883
            query.waitForResponse();

            if (query.responseCancelled()) {
884
                emit cancelled();
885 886 887 888 889 890 891 892 893 894 895 896
                failOperation();
                return;
            }

            setPassword(query.password());

            const QString response(password() + QLatin1Char('\n'));
            writeToProcess(response.toLocal8Bit());

            return;
        }

897 898 899 900 901 902 903
        if (checkForErrorMessage(line, DiskFullPatterns)) {
            qCWarning(ARK) << "Found disk full message:" << line;
            emit error(i18nc("@info", "Extraction failed because the disk is full."));
            failOperation();
            return;
        }

904
        if (checkForErrorMessage(line, WrongPasswordPatterns)) {
905
            qCWarning(ARK) << "Wrong password!";
906 907
            setPassword(QString());
            emit error(i18nc("@info", "Extraction failed: Incorrect password"));
908 909 910 911 912
            failOperation();
            return;
        }

        if (checkForErrorMessage(line, ExtractionFailedPatterns)) {
913
            qCWarning(ARK) << "Error in extraction:" << line;
914
            emit error(i18n("Extraction failed because of an unexpected error."));
915 916 917 918
            failOperation();
            return;
        }

Raphael Kubo da Costa's avatar
Raphael Kubo da Costa committed
919
        if (handleFileExistsMessage(line)) {
920
            return;
Raphael Kubo da Costa's avatar
Raphael Kubo da Costa committed
921
        }
922 923
    }

924
    if (m_operationMode == List) {
925
        if (checkForPasswordPromptMessage(line)) {
926
            qCDebug(ARK) << "Found a password prompt";
927 928

            Kerfuffle::PasswordNeededQuery query(filename());
929
            emit userQuery(&query);
930 931 932
            query.waitForResponse();

            if (query.responseCancelled()) {
933
                emit cancelled();
934 935 936 937 938 939 940 941 942 943 944 945
                failOperation();
                return;
            }

            setPassword(query.password());

            const QString response(password() + QLatin1Char('\n'));
            writeToProcess(response.toLocal8Bit());

            return;
        }

946
        if (checkForErrorMessage(line, WrongPasswordPatterns)) {
947
            qCWarning(ARK) << "Wrong password!";
948
            setPassword(QString());
949
            emit error(i18n("Incorrect password."));
950 951 952 953 954
            failOperation();
            return;
        }

        if (checkForErrorMessage(line, ExtractionFailedPatterns)) {
955
            qCWarning(ARK) << "Error in extraction!!";
956
            emit error(i18n("Extraction failed because of an unexpected error."));
957 958 959 960
            failOperation();
            return;
        }

961
        if (checkForErrorMessage(line, CorruptArchivePatterns)) {
962
            qCWarning(ARK) << "Archive corrupt";
963 964 965 966 967 968 969 970 971 972 973
            setCorrupt(true);
            Kerfuffle::LoadCorruptQuery query(filename());
            emit userQuery(&query);
            query.waitForResponse();
            if (!query.responseYes()) {
                emit cancelled();
                failOperation();
                return;
            }
        }

Raphael Kubo da Costa's avatar
Raphael Kubo da Costa committed
974
        if (handleFileExistsMessage(line)) {
975
            return;
Raphael Kubo da Costa's avatar
Raphael Kubo da Costa committed
976
        }
977

978 979 980 981
        readListLine(line);
        return;
    }
}
982

983 984 985 986 987 988 989
bool CliInterface::checkForPasswordPromptMessage(const QString& line)
{
    const QString passwordPromptPattern(m_param.value(PasswordPromptPattern).toString());

    if (passwordPromptPattern.isEmpty())
        return false;

990
    if (m_passwordPromptPattern.pattern().isEmpty()) {
991 992 993
        m_passwordPromptPattern.setPattern(m_param.value(PasswordPromptPattern).toString());
    }

994
    if (m_passwordPromptPattern.match(line).hasMatch()) {
995 996 997 998 999 1000
        return true;
    }

    return false;
}

1001 1002
bool CliInterface::handleFileExistsMessage(const QString& line)
{
1003 1004 1005 1006 1007 1008 1009
    // Check for a filename and store it.
    foreach (const QString &pattern, m_param.value(FileExistsFileName).toStringList()) {
        const QRegularExpression rxFileNamePattern(pattern);
        const QRegularExpressionMatch rxMatch = rxFileNamePattern.match(line);

        if (rxMatch.hasMatch()) {
            m_storedFileName = rxMatch.captured(1);
1010
            qCWarning(ARK) << "Detected existing file:" << m_storedFileName;
1011 1012 1013
        }
    }

1014
    if (!checkForErrorMessage(line, FileExistsExpression)) {
1015
        return false;
Raphael Kubo da Costa's avatar
Raphael Kubo da Costa committed
1016
    }
1017

1018
    Kerfuffle::OverwriteQuery query(QDir::current().path() + QLatin1Char( '/' ) + m_storedFileName);
1019
    query.setNoRenameMode(true);
1020
    emit userQuery(&query);
1021
    qCDebug(ARK) << "Waiting response";
1022
    query.waitForResponse();
1023

1024
    qCDebug(ARK) << "Finished response";
1025

1026
    QString responseToProcess;
1027
    const QStringList choices = m_param.value(FileExistsInput).toStringList();
1028

Raphael Kubo da Costa's avatar
Raphael Kubo da Costa committed
1029
    if (query.responseOverwrite()) {
1030
        responseToProcess = choices.at(0);
Raphael Kubo da Costa's avatar
Raphael Kubo da Costa committed
1031
    } else if (query.responseSkip()) {
1032
        responseToProcess = choices.at(1);
Raphael Kubo da Costa's avatar
Raphael Kubo da Costa committed
1033
    } else if (query.responseOverwriteAll()) {
1034
        responseToProcess = choices.at(2);
Raphael Kubo da Costa's avatar
Raphael Kubo da Costa committed
1035
    } else if (query.responseAutoSkip()) {
1036
        responseToProcess = choices.at(3);
Raphael Kubo da Costa's avatar
Raphael Kubo da Costa committed
1037
    } else if (query.responseCancelled()) {
1038 1039 1040
        if (choices.count() < 5) { // If the program has no way to cancel the extraction, we resort to killing it
            return doKill();
        }
1041
        responseToProcess = choices.at(4);
Raphael Kubo da Costa's avatar
Raphael Kubo da Costa committed
1042
    }
1043 1044 1045

    Q_ASSERT(!responseToProcess.isEmpty());

1046
    responseToProcess += QLatin1Char( '\n' );
1047

1048
    writeToProcess(responseToProcess.toLocal8Bit());
1049 1050 1051 1052 1053 1054

    return true;
}

bool CliInterface::checkForErrorMessage(const QString& line, int parameterIndex)
{
1055
    QList<QRegularExpression> patterns;
1056

1057 1058
    if (m_patternCache.contains(parameterIndex)) {
        patterns = m_patternCache.value(parameterIndex);
1059
    } else {
Raphael Kubo da Costa's avatar
Raphael Kubo da Costa committed
1060
        if (!m_param.contains(parameterIndex)) {
1061
            return false;
Raphael Kubo da Costa's avatar
Raphael Kubo da Costa committed
1062
        }
1063 1064

        foreach(const QString& rawPattern, m_param.value(parameterIndex).toStringList()) {
1065
            patterns << QRegularExpression(rawPattern);
1066
        }
1067
        m_patternCache[parameterIndex] = patterns;
1068 1069
    }

1070 1071
    foreach(const QRegularExpression& pattern, patterns) {
        if (pattern.match(line).hasMatch()) {
1072 1073 1074 1075 1076 1077 1078 1079 1080
            return true;
        }
    }
    return false;
}

bool CliInterface::doKill()
{
    if (m_process) {
1081
        // Give some time for the application to finish gracefully
1082
        m_abortingOperation = true;
1083 1084
        if (!m_process->waitForFinished(5)) {
            m_process->kill();
1085 1086 1087

            // It takes a few hundred ms for the process to be killed.
            m_process->waitForFinished(1000);
1088
        }