cliplugin.cpp 11.1 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;
63
        p[ListProgram] = p[ExtractProgram] = p[DeleteProgram] = p[AddProgram] = 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 84 85 86
                                   << QStringLiteral("$Files");
        p[DeleteArgs] = QStringList() << QStringLiteral("d")
                                      << QStringLiteral("$Archive")
                                      << QStringLiteral("$Files");

87 88 89 90 91
        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:     \\./(.*)$");
92 93 94 95 96
        p[FileExistsInput] = QStringList() << QStringLiteral("Y")  //overwrite
                                           << QStringLiteral("N")  //skip
                                           << QStringLiteral("A")  //overwrite all
                                           << QStringLiteral("S")  //autoskip
                                           << QStringLiteral("Q"); //cancel
97
        p[PasswordPromptPattern] = QStringLiteral("Enter password \\(will not be echoed\\)");
98
        p[ExtractionFailedPatterns] = QStringList() << QStringLiteral("ERROR: E_FAIL");
99 100
        p[CorruptArchivePatterns] = QStringList() << QStringLiteral("Unexpected end of archive")
                                                  << QStringLiteral("Headers Error");
101
        p[DiskFullPatterns] = QStringList() << QStringLiteral("No space left on device");
102 103 104
    }

    return p;
105
}
106

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

Ragnar Thomsen's avatar
Ragnar Thomsen committed
114 115 116 117 118 119 120 121 122 123 124 125
    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) {

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

Ragnar Thomsen's avatar
Ragnar Thomsen committed
136 137
    } else if (m_parseState == ParseStateArchiveInformation) {

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

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

        } 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'));
180
        }
181

Ragnar Thomsen's avatar
Ragnar Thomsen committed
182
    } else if (m_parseState == ParseStateEntryInformation) {
183

184
        if (line.startsWith(QStringLiteral("Path = "))) {
185
            const QString entryFilename =
186
                QDir::fromNativeSeparators(line.mid(7).trimmed());
187 188 189
            m_currentArchiveEntry.clear();
            m_currentArchiveEntry[FileName] = entryFilename;
            m_currentArchiveEntry[InternalID] = entryFilename;
190
        } else if (line.startsWith(QStringLiteral("Size = "))) {
191
            m_currentArchiveEntry[ Size ] = line.mid(7).trimmed();
192
        } else if (line.startsWith(QStringLiteral("Packed Size = "))) {
193 194 195 196 197
            // #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();
            }
198
        } else if (line.startsWith(QStringLiteral("Modified = "))) {
199 200
            m_currentArchiveEntry[ Timestamp ] =
                QDateTime::fromString(line.mid(11).trimmed(),
201 202
                                      QStringLiteral("yyyy-MM-dd hh:mm:ss"));
        } else if (line.startsWith(QStringLiteral("Attributes = "))) {
203
            const QString attributes = line.mid(13).trimmed();
204

205
            const bool isDirectory = attributes.startsWith(QLatin1Char('D'));
206 207
            m_currentArchiveEntry[ IsDirectory ] = isDirectory;
            if (isDirectory) {
208 209
                const QString directoryName =
                    m_currentArchiveEntry[FileName].toString();
210 211
                if (!directoryName.endsWith(QLatin1Char('/'))) {
                    const bool isPasswordProtected = (line.at(12) == QLatin1Char('+'));
212
                    m_currentArchiveEntry[FileName] =
213
                        m_currentArchiveEntry[InternalID] = QString(directoryName + QLatin1Char('/'));
214 215
                    m_currentArchiveEntry[ IsPasswordProtected ] =
                        isPasswordProtected;
216 217 218 219
                }
            }

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

    return true;
236
}
237

238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
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;
}

254
#include "cliplugin.moc"