summaryrefslogtreecommitdiffstats
path: root/src/libs/installer/extractarchiveoperation.cpp
blob: 162bd16098a2010c2c9830a4558d341b3a69b730 (plain)
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
/**************************************************************************
**
** Copyright (C) 2022 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Installer Framework.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
**************************************************************************/

#include "extractarchiveoperation_p.h"

#include "constants.h"
#include "globals.h"
#include "fileguard.h"

#include <QEventLoop>
#include <QThreadPool>
#include <QFileInfo>
#include <QDataStream>

namespace QInstaller {

/*!
    \inmodule QtInstallerFramework
    \class QInstaller::ExtractArchiveOperation
    \internal
*/

/*!
    \typedef QInstaller::ExtractArchiveOperation::Backup

    Synonym for QPair<QString, QString>. Contains a pair
    of an original and a generated backup filename for a file.
*/

/*!
    \typedef QInstaller::ExtractArchiveOperation::BackupFiles

    Synonym for QVector<Backup>.
*/

/*!
    \inmodule QtInstallerFramework
    \class QInstaller::WorkerThread
    \internal
*/

ExtractArchiveOperation::ExtractArchiveOperation(PackageManagerCore *core)
    : UpdateOperation(core)
    , m_totalEntries(0)
{
    setName(QLatin1String("Extract"));
    setGroup(OperationGroup::Unpack);
}

void ExtractArchiveOperation::backup()
{
    if (!checkArgumentCount(2))
        return;

    const QStringList args = arguments();
    const QString archivePath = args.at(0);
    const QString targetDir = args.at(1);

    QScopedPointer<AbstractArchive> archive(ArchiveFactory::instance().create(archivePath));
    if (!archive) {
        setError(UserDefinedError);
        setErrorString(tr("Unsupported archive \"%1\": no handler registered for file suffix \"%2\".")
            .arg(archivePath, QFileInfo(archivePath).suffix()));
        return;
    }

    if (!(archive->open(QIODevice::ReadOnly) && archive->isSupported())) {
        setError(UserDefinedError);
        setErrorString(tr("Cannot open archive \"%1\" for reading: %2")
            .arg(archivePath, archive->errorString()));
        return;
    }
    const QVector<ArchiveEntry> entries = archive->list();
    if (entries.isEmpty()) {
        setError(UserDefinedError);
        setErrorString(tr("Error while reading contents of archive \"%1\": %2")
            .arg(archivePath, archive->errorString()));
        return;
    }

    const bool hasAdminRights = (AdminAuthorization::hasAdminRights() || RemoteClient::instance().isActive());
    const bool canCreateSymLinks = QInstaller::canCreateSymbolicLinks();
    bool needsAdminRights = false;

    for (auto &entry : entries) {
        const QString completeFilePath = targetDir + QDir::separator() + entry.path;
        if (!entry.isDirectory) {
            // Ignore failed backups, existing files are overwritten when extracting.
            // Should the backups be used on rollback too, this may not be the
            // desired behavior anymore.
            prepareForFile(completeFilePath);
        }
        if (!hasAdminRights && !canCreateSymLinks && entry.isSymbolicLink)
            needsAdminRights = true;
    }
    m_totalEntries = entries.size();
    if (needsAdminRights)
        setValue(QLatin1String("admin"), true);

    // Show something was done
    emit progressChanged(scBackupProgressPart);
}

bool ExtractArchiveOperation::performOperation()
{
    if (!checkArgumentCount(2))
        return false;

    const QStringList args = arguments();
    const QString archivePath = args.at(0);
    const QString targetDir = args.at(1);

    Receiver receiver;
    Callback callback;

    connect(&callback, &Callback::progressChanged, this, &ExtractArchiveOperation::progressChanged);

    Worker *worker = new Worker(archivePath, targetDir, m_totalEntries, &callback);
    connect(worker, &Worker::finished, &receiver, &Receiver::workerFinished,
        Qt::QueuedConnection);

    if (PackageManagerCore *core = packageManager())
        connect(core, &PackageManagerCore::statusChanged, worker, &Worker::onStatusChanged);

    QFileInfo fileInfo(archivePath);
    emit outputTextChanged(tr("Extracting \"%1\"").arg(fileInfo.fileName()));
    {
        QEventLoop loop;
        QThread workerThread;
        worker->moveToThread(&workerThread);

        connect(&workerThread, &QThread::started, worker, &Worker::run);
        connect(&receiver, &Receiver::finished, &workerThread, &QThread::quit);
        connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater);
        connect(&workerThread, &QThread::finished, &loop, &QEventLoop::quit);

        workerThread.start();
        loop.exec();
    }
    // Write all file names which belongs to a package to a separate file and only the separate
    // filename to a .dat file. There can be enormous amount of files in a package, which makes
    // the dat file very slow to read and write. The .dat file is read into memory in startup,
    // writing the file names to a separate file we don't need to load all the file names into
    // memory as we need those only in uninstall. This will save a lot of memory.
    // Parse a file and directorory structure using archivepath syntax
    // installer://<component_name>/<filename>.7z Resulting structure is:
    // -installerResources (dir)
    //   -<component_name> (dir)
    //    -<filename>.txt (file)

    QStringList files = callback.extractedFiles();

    QString installDir = targetDir;
    // If we have package manager in use (normal installer run) then use
    // TargetDir for saving filenames, otherwise those would be saved to
    // extracted folder. Also initialize installerbasebinary which we use later
    // to check if the extracted file in question is the maintenancetool itself.
    QString installerBaseBinary;
    if (PackageManagerCore *core = packageManager()) {
        installDir = core->value(scTargetDir);
        installerBaseBinary = core->toNativeSeparators(core->replaceVariables(core->installerBaseBinary()));
    }
    const QString resourcesPath = installDir + QLatin1Char('/') + QLatin1String("installerResources");

    QString fileDirectory = resourcesPath + QLatin1Char('/') + archivePath.section(QLatin1Char('/'), 1, 1,
                            QString::SectionSkipEmpty) + QLatin1Char('/');
    QString archiveFileName = archivePath.section(QLatin1Char('/'), 2, 2, QString::SectionSkipEmpty);
    QFileInfo fileInfo2(archiveFileName);
    QString suffix = fileInfo2.suffix();
    archiveFileName.chop(suffix.length() + 1); // removes suffix (e.g. '.7z') from archive filename
    QString fileName = archiveFileName + QLatin1String(".txt");

    QFileInfo targetDirectoryInfo(fileDirectory);

    QInstaller::createDirectoryWithParents(targetDirectoryInfo.absolutePath());
    setDefaultFilePermissions(resourcesPath, DefaultFilePermissions::Executable);
    setDefaultFilePermissions(targetDirectoryInfo.absolutePath(), DefaultFilePermissions::Executable);

    QFile file(targetDirectoryInfo.absolutePath() + QLatin1Char('/') + fileName);
    if (file.open(QIODevice::WriteOnly)) {
        setDefaultFilePermissions(file.fileName(), DefaultFilePermissions::NonExecutable);
        QDataStream out (&file);
        for (int i = 0; i < files.count(); ++i) {
            if (!installerBaseBinary.isEmpty() && files[i].startsWith(installerBaseBinary)) {
                // Do not write installerbase binary filename to extracted files. Installer binary
                // is maintenance tool program, the binary is removed elsewhere
                // when we do full uninstall.
                files.clear();
                break;
            }
            files[i] = replacePath(files.at(i), installDir, QLatin1String(scRelocatable));
        }
        out << files;
        setValue(QLatin1String("files"), file.fileName());
        file.close();
    } else {
        qCWarning(QInstaller::lcInstallerInstallLog) << "Cannot open file for writing " << file.fileName() << ":" << file.errorString();
    }

    // TODO: Use backups for rollback, too? Doesn't work for uninstallation though.

    // delete all backups we can delete right now, remember the rest
    foreach (const Backup &i, m_backupFiles)
        deleteFileNowOrLater(i.second);

    if (!receiver.success()) {
        setError(UserDefinedError);
        setErrorString(receiver.errorString());
        return false;
    }
    return true;
}

bool ExtractArchiveOperation::undoOperation()
{
    Q_ASSERT(arguments().count() == 2);

    // For backward compatibility, check if "files" can be converted to QStringList.
    // If yes, files are listed in .dat instead of in a separate file.
    bool useStringListType(value(QLatin1String("files")).type() == QVariant::StringList);
    QString targetDir = arguments().at(1);
    if (packageManager())
        targetDir = packageManager()->value(scTargetDir);
    QStringList files;
    if (useStringListType) {
        files = value(QLatin1String("files")).toStringList();
    } else {
        if (!readDataFileContents(targetDir, &files))
            return false;
    }
    startUndoProcess(files);
    if (!useStringListType)
        deleteDataFile(m_relocatedDataFileName);

    // Remove the installerResources directory if it is empty.
    QDir(targetDir).rmdir(QLatin1String("installerResources"));

    return true;
}

void ExtractArchiveOperation::startUndoProcess(const QStringList &files)
{
    WorkerThread *const thread = new WorkerThread(this, files);
    connect(thread, &WorkerThread::currentFileChanged, this,
        &ExtractArchiveOperation::outputTextChanged);
    connect(thread, &WorkerThread::progressChanged, this,
        &ExtractArchiveOperation::progressChanged);

    const QFileInfo archive(arguments().at(0));
    emit outputTextChanged(tr("Removing files extracted from \"%1\"").arg(archive.fileName()));

    QEventLoop loop;
    connect(thread, &QThread::finished, &loop, &QEventLoop::quit, Qt::QueuedConnection);
    thread->start();
    loop.exec();
    thread->deleteLater();
}

void ExtractArchiveOperation::deleteDataFile(const QString &fileName)
{
    if (fileName.isEmpty()) {
        qCWarning(QInstaller::lcInstallerInstallLog) << Q_FUNC_INFO << "data file name cannot be empty.";
        return;
    }
    QFile file(fileName);
    QFileInfo fileInfo(file);
    if (file.remove()) {
        QDir directory(fileInfo.absoluteDir());
        if (directory.exists() && directory.isEmpty())
            directory.rmdir(directory.path());
    } else {
        qCWarning(QInstaller::lcInstallerInstallLog) << "Cannot remove data file" << file.fileName();
    }
}

QString ExtractArchiveOperation::generateBackupName(const QString &fn)
{
    const QString bfn = fn + QLatin1String(".tmpUpdate");
    QString res = bfn;
    int i = 0;
    while (QFile::exists(res))
        res = bfn + QString::fromLatin1(".%1").arg(i++);
    return res;
}

bool ExtractArchiveOperation::prepareForFile(const QString &filename)
{
    if (!QFile::exists(filename))
        return true;

    FileGuardLocker locker(filename, FileGuard::globalObject());

    const QString backup = generateBackupName(filename);
    QFile f(filename);
    const bool renamed = f.rename(backup);
    if (f.exists() && !renamed) {
        qCritical("Cannot rename %s to %s: %s", qPrintable(filename), qPrintable(backup),
                  qPrintable(f.errorString()));
        return false;
    }
    m_backupFiles.append(qMakePair(filename, backup));
    return true;
}

bool ExtractArchiveOperation::testOperation()
{
    return true;
}

quint64 ExtractArchiveOperation::sizeHint()
{
    if (!checkArgumentCount(2))
        return UpdateOperation::sizeHint();

    if (hasValue(QLatin1String("sizeHint")))
        return value(QLatin1String("sizeHint")).toULongLong();

    const QString archivePath = arguments().at(0);
    const quint64 compressedSize = QFileInfo(archivePath).size();

    setValue(QLatin1String("sizeHint"), QString::number(compressedSize));

    // A rough estimate of how much time it takes to extract this archive. Other
    // affecting parameters are the archive format, compression filter and -level.
    return compressedSize;
}

bool ExtractArchiveOperation::readDataFileContents(QString &targetDir, QStringList *resultList)
{
    const QString filePath = value(QLatin1String("files")).toString();
    // Does not change target on non macOS platforms.
    if (QInstaller::isInBundle(targetDir, &targetDir))
        targetDir = QDir::cleanPath(targetDir + QLatin1String("/.."));
    m_relocatedDataFileName = replacePath(filePath, QLatin1String(scRelocatable), targetDir);
    QFile file(m_relocatedDataFileName);

    if (file.open(QIODevice::ReadOnly)) {
        QDataStream in(&file);
        in >> *resultList;
        for (int i = 0; i < resultList->count(); ++i)
            resultList->replace(i, replacePath(resultList->at(i),  QLatin1String(scRelocatable), targetDir));

    } else {
        // We should not be here. Either user has manually deleted the installer related
        // files or same component is installed several times.
        qCWarning(QInstaller::lcInstallerInstallLog) << "Cannot open file " << file.fileName() << " for reading:"
                << file.errorString() << ". Component is already uninstalled "
                << "or file is manually deleted.";
    }
    return true;
}

} // namespace QInstaller