2014-07-11 15:26:35 +02:00
|
|
|
/**************************************************************************
|
|
|
|
**
|
2015-02-17 11:18:57 +01:00
|
|
|
** Copyright (C) 2015 The Qt Company Ltd.
|
|
|
|
** Contact: http://www.qt.io/licensing/
|
2014-07-11 15:26:35 +02:00
|
|
|
**
|
|
|
|
** This file is part of the Qt Installer Framework.
|
|
|
|
**
|
|
|
|
** $QT_BEGIN_LICENSE:LGPL$
|
|
|
|
** 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
|
2015-02-17 11:18:57 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
** and conditions see http://qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at http://www.qt.io/contact-us.
|
2014-07-11 15:26:35 +02:00
|
|
|
**
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
2014-10-16 11:51:47 +02:00
|
|
|
** General Public License version 2.1 or version 3 as published by the Free
|
|
|
|
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
|
|
|
|
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
|
|
|
|
** following information to ensure the GNU Lesser General Public License
|
|
|
|
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
|
|
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2014-07-11 15:26:35 +02:00
|
|
|
**
|
2015-02-17 11:18:57 +01:00
|
|
|
** As a special exception, The Qt Company gives you certain additional
|
|
|
|
** rights. These rights are described in The Qt Company LGPL Exception
|
2014-07-11 15:26:35 +02:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
**
|
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
#include "binarydump.h"
|
|
|
|
#include "binaryreplace.h"
|
|
|
|
#include "operationrunner.h"
|
|
|
|
|
2014-07-11 15:15:26 +02:00
|
|
|
#include <binarycontent.h>
|
2014-07-11 15:26:35 +02:00
|
|
|
#include <binaryformat.h>
|
|
|
|
#include <binaryformatenginehandler.h>
|
|
|
|
#include <errors.h>
|
|
|
|
#include <fileio.h>
|
|
|
|
#include <fileutils.h>
|
|
|
|
#include <init.h>
|
|
|
|
#include <utils.h>
|
|
|
|
|
|
|
|
#include <QCoreApplication>
|
|
|
|
#include <QCommandLineParser>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QResource>
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
QCoreApplication app(argc, argv);
|
|
|
|
app.setApplicationVersion(QLatin1String("1.0.0"));
|
|
|
|
|
|
|
|
QCommandLineOption verbose(QLatin1String("verbose"),
|
|
|
|
QLatin1String("Verbose mode. Prints out more information."));
|
|
|
|
QCommandLineOption dump(QLatin1String("dump"),
|
2015-02-05 16:15:52 +01:00
|
|
|
QLatin1String("Dumps the binary content that belongs to an installer or maintenance tool "
|
|
|
|
"into target."), QLatin1String("folder"));
|
2014-07-11 15:26:35 +02:00
|
|
|
QCommandLineOption run(QLatin1String("operation"),
|
|
|
|
QLatin1String("Executes an operation with a list of arguments. Mode can be DO or UNDO."),
|
|
|
|
QLatin1String("mode,name,args,..."));
|
|
|
|
QCommandLineOption update(QLatin1String("update"),
|
|
|
|
QLatin1String("Updates existing installer or maintenance tool with a new installer base."),
|
|
|
|
QLatin1String("file"));
|
|
|
|
|
|
|
|
QCommandLineParser parser;
|
2014-10-10 15:20:43 +02:00
|
|
|
parser.addHelpOption();
|
2014-07-11 15:26:35 +02:00
|
|
|
parser.addVersionOption();
|
|
|
|
parser.addOption(verbose);
|
|
|
|
parser.addOption(update);
|
|
|
|
parser.addOption(dump);
|
|
|
|
parser.addOption(run);
|
|
|
|
parser.addPositionalArgument(QLatin1String("binary"), QLatin1String("Existing installer or "
|
|
|
|
"maintenance tool."));
|
|
|
|
|
|
|
|
parser.process(app.arguments());
|
|
|
|
const QStringList arguments = parser.positionalArguments();
|
|
|
|
if (arguments.isEmpty() || (arguments.count() > 1))
|
|
|
|
parser.showHelp(EXIT_FAILURE);
|
|
|
|
|
|
|
|
QInstaller::init();
|
|
|
|
QInstaller::setVerbose(parser.isSet(verbose));
|
|
|
|
|
|
|
|
QString bundlePath;
|
|
|
|
QString path = QFileInfo(arguments.first()).absoluteFilePath();
|
|
|
|
if (QInstaller::isInBundle(path, &bundlePath))
|
|
|
|
path = QDir(bundlePath).filePath(QLatin1String("Contents/Resources/installer.dat"));
|
|
|
|
|
|
|
|
int result = EXIT_FAILURE;
|
|
|
|
QVector<QByteArray> resourceMappings;
|
2014-08-27 11:46:28 +02:00
|
|
|
quint64 cookie = QInstaller::BinaryContent::MagicCookie;
|
2014-07-11 15:26:35 +02:00
|
|
|
try {
|
2014-08-27 11:46:28 +02:00
|
|
|
{
|
|
|
|
QFile tmp(path);
|
|
|
|
QInstaller::openForRead(&tmp);
|
|
|
|
|
2014-10-10 15:31:35 +02:00
|
|
|
if (!tmp.seek(QInstaller::BinaryContent::findMagicCookie(&tmp, cookie) - sizeof(qint64)))
|
|
|
|
throw QInstaller::Error(QLatin1String("Could not seek to read magic marker."));
|
|
|
|
|
|
|
|
QInstaller::BinaryLayout layout;
|
|
|
|
layout.magicMarker = QInstaller::retrieveInt64(&tmp);
|
2014-08-27 11:46:28 +02:00
|
|
|
|
|
|
|
if (layout.magicMarker == QInstaller::BinaryContent::MagicUninstallerMarker) {
|
|
|
|
QFileInfo fi(path);
|
|
|
|
if (QInstaller::isInBundle(fi.absoluteFilePath(), &bundlePath))
|
|
|
|
fi.setFile(bundlePath);
|
|
|
|
path = fi.absolutePath() + QLatin1Char('/') + fi.baseName() + QLatin1String(".dat");
|
|
|
|
|
2014-10-10 15:55:08 +02:00
|
|
|
tmp.close();
|
2014-08-27 11:46:28 +02:00
|
|
|
tmp.setFileName(path);
|
2014-10-10 15:31:35 +02:00
|
|
|
QInstaller::openForRead(&tmp);
|
|
|
|
|
2014-08-27 11:46:28 +02:00
|
|
|
cookie = QInstaller::BinaryContent::MagicCookieDat;
|
|
|
|
}
|
2014-10-10 15:31:35 +02:00
|
|
|
layout = QInstaller::BinaryContent::binaryLayout(&tmp, cookie);
|
2014-08-27 11:46:28 +02:00
|
|
|
tmp.close();
|
|
|
|
|
|
|
|
if (parser.isSet(update)) {
|
2014-10-10 15:31:35 +02:00
|
|
|
BinaryReplace br(layout); // To update the binary we do not need any mapping.
|
|
|
|
return br.replace(parser.value(update), QFileInfo(arguments.first())
|
|
|
|
.absoluteFilePath());
|
2014-08-27 11:46:28 +02:00
|
|
|
}
|
2014-07-11 15:26:35 +02:00
|
|
|
}
|
|
|
|
|
2014-10-14 17:03:54 +02:00
|
|
|
QFile file(path);
|
|
|
|
QInstaller::openForRead(&file);
|
2014-08-27 11:46:28 +02:00
|
|
|
|
|
|
|
qint64 magicMarker;
|
|
|
|
QList<QInstaller::OperationBlob> operations;
|
|
|
|
QInstaller::ResourceCollectionManager manager;
|
2014-10-14 17:03:54 +02:00
|
|
|
QInstaller::BinaryContent::readBinaryContent(&file, &operations, &manager, &magicMarker,
|
|
|
|
cookie);
|
2014-07-11 15:26:35 +02:00
|
|
|
|
|
|
|
// map the inbuilt resources
|
2014-10-14 17:03:54 +02:00
|
|
|
const QInstaller::ResourceCollection meta = manager.collectionByName("QResources");
|
2014-08-27 11:46:28 +02:00
|
|
|
foreach (const QSharedPointer<QInstaller::Resource> &resource, meta.resources()) {
|
2014-10-14 17:03:54 +02:00
|
|
|
const bool isOpen = resource->isOpen();
|
|
|
|
if ((!isOpen) && (!resource->open()))
|
|
|
|
continue; // TODO: should we throw here?
|
|
|
|
|
2014-08-27 11:46:28 +02:00
|
|
|
const QByteArray ba = resource->readAll();
|
2014-07-11 15:26:35 +02:00
|
|
|
if (!QResource::registerResource((const uchar*) ba.data(), QLatin1String(":/metadata")))
|
|
|
|
throw QInstaller::Error(QLatin1String("Could not register in-binary resource."));
|
|
|
|
resourceMappings.append(ba);
|
2014-10-14 17:03:54 +02:00
|
|
|
if (!isOpen)
|
2014-08-27 11:46:28 +02:00
|
|
|
resource->close();
|
2014-07-11 15:26:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (parser.isSet(dump)) {
|
|
|
|
// To dump the content we do not need the binary format engine.
|
|
|
|
BinaryDump bd;
|
2014-10-30 17:39:32 +01:00
|
|
|
result = bd.dump(manager, parser.value(dump));
|
|
|
|
} else if (parser.isSet(run)) {
|
|
|
|
QInstaller::BinaryFormatEngineHandler::instance()->registerResources(manager
|
|
|
|
.collections()); // setup the binary format engine
|
2014-07-11 15:26:35 +02:00
|
|
|
|
2014-08-27 11:46:28 +02:00
|
|
|
OperationRunner runner(magicMarker, operations);
|
2014-10-30 17:39:32 +01:00
|
|
|
const QStringList arguments = parser.value(run).split(QLatin1Char(','));
|
2014-07-11 15:26:35 +02:00
|
|
|
if (arguments.first() == QLatin1String("DO"))
|
|
|
|
result = runner.runOperation(arguments.mid(1), OperationRunner::RunMode::Do);
|
|
|
|
else if (arguments.first() == QLatin1String("UNDO"))
|
|
|
|
result = runner.runOperation(arguments.mid(1), OperationRunner::RunMode::Undo);
|
2014-10-30 17:39:32 +01:00
|
|
|
else
|
|
|
|
std::cerr << "Malformed argument: " << qPrintable(parser.value(run)) << std::endl;
|
2014-07-11 15:26:35 +02:00
|
|
|
}
|
|
|
|
} catch (const QInstaller::Error &error) {
|
|
|
|
std::cerr << qPrintable(error.message()) << std::endl;
|
|
|
|
} catch (...) {
|
|
|
|
std::cerr << "Unknown exception caught." << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
// unmap resources
|
|
|
|
foreach (const QByteArray &rccData, resourceMappings)
|
|
|
|
QResource::unregisterResource((const uchar*) rccData.data(), QLatin1String(":/metadata"));
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|