cliplugin.cpp 11.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 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * 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.
 *
 */
22 23

#include "cliplugin.h"
24
#include "ark_debug.h"
25
#include "kerfuffle/cliinterface.h"
26
#include "kerfuffle/kerfuffle_export.h"
27

28
#include <QDateTime>
29
#include <QDir>
Ragnar Thomsen's avatar
Ragnar Thomsen committed
30
#include <QRegularExpression>
31

Bhushan Shah's avatar
Bhushan Shah committed
32 33
#include <KPluginFactory>

34 35
using namespace Kerfuffle;

36
K_PLUGIN_FACTORY_WITH_JSON(CliPluginFactory, "kerfuffle_cli7z.json", registerPlugin<CliPlugin>();)
Bhushan Shah's avatar
Bhushan Shah committed
37

38 39
CliPlugin::CliPlugin(QObject *parent, const QVariantList & args)
        : CliInterface(parent, args)
40
        , m_archiveType(ArchiveType7z)
Ragnar Thomsen's avatar
Ragnar Thomsen committed
41
        , m_parseState(ParseStateTitle)
42
        , m_linesComment(0)
43
{
44
    qCDebug(ARK) << "Loaded cli_7z plugin";
45
}
46

47 48 49
CliPlugin::~CliPlugin()
{
}
50

51 52
void CliPlugin::resetParsing()
{
Ragnar Thomsen's avatar
Ragnar Thomsen committed
53
    m_parseState = ParseStateTitle;
54
    m_comment.clear();
55 56
}

57 58
ParameterList CliPlugin::parameterList() const
{
59 60 61 62
    static ParameterList p;

    if (p.isEmpty()) {
        //p[CaptureProgress] = true;
Ragnar Thomsen's avatar
Ragnar Thomsen committed
63
        p[ListProgram] = p[ExtractProgram] = p[DeleteProgram] = p[AddProgram] = p[TestProgram] = QStringList() << QStringLiteral("7z");
64 65 66 67 68 69 70 71 72 73 74
        p[ListArgs] = QStringList() << QStringLiteral("l")
                                    << QStringLiteral("-slt")
                                    << QStringLiteral("$PasswordSwitch")
                                    << QStringLiteral("$Archive");
        p[ExtractArgs] = QStringList() << QStringLiteral("$PreservePathSwitch")
                                       << QStringLiteral("$PasswordSwitch")
                                       << QStringLiteral("$Archive")
                                       << QStringLiteral("$Files");
        p[PreservePathSwitch] = QStringList() << QStringLiteral("x")
                                              << QStringLiteral("e");
        p[PasswordSwitch] = QStringList() << QStringLiteral("-p$Password");
75
        p[PasswordHeaderSwitch] = QStringList { QStringLiteral("-p$Password"), QStringLiteral("-mhe=on") };
76
        p[WrongPasswordPatterns] = QStringList() << QStringLiteral("Wrong password");
77
        p[CompressionLevelSwitch] = QStringLiteral("-mx=$CompressionLevel");
78 79 80
        p[AddArgs] = QStringList() << QStringLiteral("a")
                                   << QStringLiteral("$Archive")
                                   << QStringLiteral("$PasswordSwitch")
81
                                   << QStringLiteral("$CompressionLevelSwitch")
82 83
                                   << QStringLiteral("$Files");
        p[DeleteArgs] = QStringList() << QStringLiteral("d")
84
                                      << QStringLiteral("$PasswordSwitch")
85 86
                                      << QStringLiteral("$Archive")
                                      << QStringLiteral("$Files");
Ragnar Thomsen's avatar
Ragnar Thomsen committed
87 88 89
        p[TestArgs] = QStringList() << QStringLiteral("t")
                                    << QStringLiteral("$Archive");
        p[TestPassedPattern] = QStringLiteral("^Everything is Ok$");
90

91 92 93 94 95
        p[FileExistsExpression] = QStringList()
            << QStringLiteral("^\\(Y\\)es / \\(N\\)o / \\(A\\)lways / \\(S\\)kip all / A\\(u\\)to rename all / \\(Q\\)uit\\? $")
            << QStringLiteral("^\\? \\(Y\\)es / \\(N\\)o / \\(A\\)lways / \\(S\\)kip all / A\\(u\\)to rename all / \\(Q\\)uit\\? $");
        p[FileExistsFileName] = QStringList() << QStringLiteral("^file \\./(.*)$")
                                              << QStringLiteral("^  Path:     \\./(.*)$");
96 97 98 99 100
        p[FileExistsInput] = QStringList() << QStringLiteral("Y")  //overwrite
                                           << QStringLiteral("N")  //skip
                                           << QStringLiteral("A")  //overwrite all
                                           << QStringLiteral("S")  //autoskip
                                           << QStringLiteral("Q"); //cancel
101
        p[PasswordPromptPattern] = QStringLiteral("Enter password \\(will not be echoed\\)");
102
        p[ExtractionFailedPatterns] = QStringList() << QStringLiteral("ERROR: E_FAIL");
103 104
        p[CorruptArchivePatterns] = QStringList() << QStringLiteral("Unexpected end of archive")
                                                  << QStringLiteral("Headers Error");
105
        p[DiskFullPatterns] = QStringList() << QStringLiteral("No space left on device");
106 107 108
    }

    return p;
109
}
110

111
bool CliPlugin::readListLine(const QString& line)
112
{
113 114
    static const QLatin1String archiveInfoDelimiter1("--"); // 7z 9.13+
    static const QLatin1String archiveInfoDelimiter2("----"); // 7z 9.04
115
    static const QLatin1String entryInfoDelimiter("----------");
116
    const QRegularExpression rxComment(QStringLiteral("Comment = .+$"));
117

Ragnar Thomsen's avatar
Ragnar Thomsen committed
118 119 120 121 122 123 124 125 126 127 128 129
    if (m_parseState == ParseStateTitle) {

        const QRegularExpression rxVersionLine(QStringLiteral("^p7zip Version ([\\d\\.]+) .*$"));
        QRegularExpressionMatch matchVersion = rxVersionLine.match(line);
        if (matchVersion.hasMatch()) {
            m_parseState = ParseStateHeader;
            const QString p7zipVersion = matchVersion.captured(1);
            qCDebug(ARK) << "p7zip version" << p7zipVersion << "detected";
        }

    } else if (m_parseState == ParseStateHeader) {

130
        if (line.startsWith(QStringLiteral("Listing archive:"))) {
131
            qCDebug(ARK) << "Archive name: "
132
                     << line.right(line.size() - 16).trimmed();
133 134
        } else if ((line == archiveInfoDelimiter1) ||
                   (line == archiveInfoDelimiter2)) {
Ragnar Thomsen's avatar
Ragnar Thomsen committed
135
            m_parseState = ParseStateArchiveInformation;
136
        } else if (line.contains(QStringLiteral("Error: "))) {
137
            qCWarning(ARK) << line.mid(7);
138
        }
139

Ragnar Thomsen's avatar
Ragnar Thomsen committed
140 141
    } else if (m_parseState == ParseStateArchiveInformation) {

142
        if (line == entryInfoDelimiter) {
Ragnar Thomsen's avatar
Ragnar Thomsen committed
143
            m_parseState = ParseStateEntryInformation;
144
        } else if (line.startsWith(QStringLiteral("Type = "))) {
145
            const QString type = line.mid(7).trimmed();
146
            qCDebug(ARK) << "Archive type: " << type;
147 148 149

            if (type == QLatin1String("7z")) {
                m_archiveType = ArchiveType7z;
150
            } else if (type == QLatin1String("bzip2")) {
151
                m_archiveType = ArchiveTypeBZip2;
152
            } else if (type == QLatin1String("gzip")) {
153
                m_archiveType = ArchiveTypeGZip;
154 155
            } else if (type == QLatin1String("xz")) {
                m_archiveType = ArchiveTypeXz;
156
            } else if (type == QLatin1String("tar")) {
157
                m_archiveType = ArchiveTypeTar;
158
            } else if (type == QLatin1String("zip")) {
159
                m_archiveType = ArchiveTypeZip;
160 161
            } else if (type == QLatin1String("Rar")) {
                m_archiveType = ArchiveTypeRar;
162 163
            } else if (type == QLatin1String("Split")) {
                m_isMultiVolume = true;
164 165
            } else {
                // Should not happen
166
                qCWarning(ARK) << "Unsupported archive type";
167 168
                return false;
            }
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185

        } else if (rxComment.match(line).hasMatch()) {
            m_parseState = ParseStateComment;
            m_comment.append(line.section(QLatin1Char('='), 1) + QLatin1Char('\n'));
        }

    } else if (m_parseState == 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'));
186
        }
187

Ragnar Thomsen's avatar
Ragnar Thomsen committed
188
    } else if (m_parseState == ParseStateEntryInformation) {
189

190
        if (line.startsWith(QStringLiteral("Path = "))) {
191
            const QString entryFilename =
192
                QDir::fromNativeSeparators(line.mid(7).trimmed());
193 194 195
            m_currentArchiveEntry.clear();
            m_currentArchiveEntry[FileName] = entryFilename;
            m_currentArchiveEntry[InternalID] = entryFilename;
196
        } else if (line.startsWith(QStringLiteral("Size = "))) {
197
            m_currentArchiveEntry[ Size ] = line.mid(7).trimmed();
198
        } else if (line.startsWith(QStringLiteral("Packed Size = "))) {
199 200 201 202 203
            // #236696: 7z files only show a single Packed Size value
            //          corresponding to the whole archive.
            if (m_archiveType != ArchiveType7z) {
                m_currentArchiveEntry[CompressedSize] = line.mid(14).trimmed();
            }
204
        } else if (line.startsWith(QStringLiteral("Modified = "))) {
205 206
            m_currentArchiveEntry[ Timestamp ] =
                QDateTime::fromString(line.mid(11).trimmed(),
207 208
                                      QStringLiteral("yyyy-MM-dd hh:mm:ss"));
        } else if (line.startsWith(QStringLiteral("Attributes = "))) {
209
            const QString attributes = line.mid(13).trimmed();
210

211
            const bool isDirectory = attributes.startsWith(QLatin1Char('D'));
212 213
            m_currentArchiveEntry[ IsDirectory ] = isDirectory;
            if (isDirectory) {
214 215
                const QString directoryName =
                    m_currentArchiveEntry[FileName].toString();
216 217
                if (!directoryName.endsWith(QLatin1Char('/'))) {
                    const bool isPasswordProtected = (line.at(12) == QLatin1Char('+'));
218
                    m_currentArchiveEntry[FileName] =
219
                        m_currentArchiveEntry[InternalID] = QString(directoryName + QLatin1Char('/'));
220 221
                    m_currentArchiveEntry[ IsPasswordProtected ] =
                        isPasswordProtected;
222 223 224 225
                }
            }

            m_currentArchiveEntry[ Permissions ] = attributes.mid(1);
226
        } else if (line.startsWith(QStringLiteral("CRC = "))) {
227
            m_currentArchiveEntry[ CRC ] = line.mid(6).trimmed();
228
        } else if (line.startsWith(QStringLiteral("Method = "))) {
229
            m_currentArchiveEntry[ Method ] = line.mid(9).trimmed();
230
        } else if (line.startsWith(QStringLiteral("Encrypted = ")) &&
231
                   line.size() >= 13) {
232 233 234
            m_currentArchiveEntry[ IsPasswordProtected ] = (line.at(12) == QLatin1Char('+'));
        } else if (line.startsWith(QStringLiteral("Block = ")) ||
                   line.startsWith(QStringLiteral("Version = "))) {
235
            if (m_currentArchiveEntry.contains(FileName)) {
236
                emit entry(m_currentArchiveEntry);
237 238 239 240 241
            }
        }
    }

    return true;
242
}
243

244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
QStringList CliPlugin::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);

    passwordHeaderSwitch[0].replace(QLatin1String("$Password"), password);

    return passwordHeaderSwitch;
}

260
#include "cliplugin.moc"