summaryrefslogtreecommitdiffstats
path: root/src/libs/installer/binaryformat.cpp
blob: 9ed7742db438ff6aff3a0fff8813b6eb40b3c318 (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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/**************************************************************************
**
** Copyright (C) 2017 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 "binaryformat.h"

#include "errors.h"
#include "fileio.h"

#include <QFileInfo>
#include <QFlags>
#include <QUuid>

namespace QInstaller {

/*!
    \class QInstaller::OperationBlob
    \inmodule QtInstallerFramework
    \brief The OperationBlob class is a textual representation of an operation that can be
        instantiated and executed by the Qt Installer Framework.
*/

/*!
    \fn QInstaller::OperationBlob::OperationBlob(const QString &n, const QString &x)

    Constructs the operation blob with the given arguments, while \a n stands for the name part and
    \a x for the XML representation of the operation.
*/

/*!
    \variable QInstaller::OperationBlob::name
    \brief The name of the operation.
*/

/*!
    \variable QInstaller::OperationBlob::xml
    \brief The XML representation of the operation.
*/

/*!
    \class QInstaller::Resource
    \inmodule QtInstallerFramework
    \brief The Resource class is an interface for wrapping a file as read only device.

    Resource is an interface for reading inside a file, but is not supposed to write to the file it
    wraps. The \c Resource class is created by passing a path to an existing
    binary (such as a zipped archive or a Qt resource file).

    The resource name can be set at any time using setName() or during construction. The segment
    supplied during construction represents the offset and size of the resource inside the file.
*/

/*!
    \fn Range<qint64> QInstaller::Resource::segment() const

    Returns the range inside the file this resource represents.
*/

/*!
    \fn void QInstaller::Resource::setSegment(const Range<qint64> &segment)

    Sets the range to the \a segment of the file that this resource represents.
*/

/*!
    Creates a resource providing the data in \a path.
 */
Resource::Resource(const QString &path)
    : m_file(path)
    , m_name(QFileInfo(path).fileName().toUtf8())
    , m_segment(Range<qint64>::fromStartAndLength(0, m_file.size()))
{
}

/*!
    Creates a resource providing the data in \a path identified by \a name.
*/
Resource::Resource(const QString &path, const QByteArray &name)
    : m_file(path)
    , m_name(name)
    , m_segment(Range<qint64>::fromStartAndLength(0, m_file.size()))
{
}

/*!
    Creates a resource providing the data in \a path limited to \a segment.
*/
Resource::Resource(const QString &path, const Range<qint64> &segment)
    : m_file(path)
    , m_name(QFileInfo(path).fileName().toUtf8())
    , m_segment(segment)
{
}

/*!
    Destroys the resource. Calls close() if necessary before destroying the resource.
*/
Resource::~Resource()
{
    if (isOpen())
        close();
}

/*!
    \reimp
 */
bool Resource::seek(qint64 pos)
{
    return QIODevice::seek(pos);
}

/*!
    Returns the name of the resource.
*/
QByteArray Resource::name() const
{
    return m_name;
}

/*!
    Sets the name of the resource to \a name.
*/
void Resource::setName(const QByteArray &name)
{
    m_name = name;
}

/*!
    Opens a resource in QIODevice::ReadOnly mode. The function returns \c true
    if successful.
*/
bool Resource::open()
{
    if (isOpen())
        return false;

    if (!m_file.open(QIODevice::ReadOnly | QIODevice::Unbuffered)) {
        setErrorString(m_file.errorString());
        return false;
    }

    if (!QIODevice::open(QIODevice::ReadOnly)) {
        setErrorString(tr("Cannot open resource %1 for reading.").arg(QString::fromUtf8(m_name)));
        return false;
    }
    return true;
}
/*!
    \reimp
 */
void Resource::close()
{
    m_file.close();
    QIODevice::close();
}

/*!
    \reimp
 */
qint64 Resource::size() const
{
    return m_segment.length();
}

/*!
    \reimp
 */
qint64 Resource::readData(char* data, qint64 maxSize)
{
    // check if there is anything left to read
    maxSize = qMin<quint64>(maxSize, m_segment.length() - pos());
    if (maxSize <= 0)
        return 0;

    const qint64 p = m_file.pos();
    m_file.seek(m_segment.start() + pos());
    const qint64 amountRead = m_file.read(data, maxSize);
    m_file.seek(p);
    return amountRead;
}

/*!
    \reimp
 */
qint64 Resource::writeData(const char* data, qint64 maxSize)
{
    Q_UNUSED(data);
    Q_UNUSED(maxSize);
    // should never be called, as we're read only
    return -1;
}

/*!
    \fn void QInstaller::Resource::copyData(QFileDevice *out)

    Copies the resource data to a file called \a out. Throws Error on failure.
*/

/*!
    \overload

    Copies the resource data of \a resource to a file called \a out. Throws Error on failure.
*/
void Resource::copyData(Resource *resource, QFileDevice *out)
{
    qint64 left = resource->size();
    char data[4096];
    while (left > 0) {
        const qint64 len = qMin<qint64>(left, 4096);
        const qint64 bytesRead = resource->read(data, len);
        if (bytesRead != len) {
            throw QInstaller::Error(tr("Read failed after %1 bytes: %2")
                .arg(QString::number(resource->size() - left), resource->errorString()));
        }
        const qint64 bytesWritten = out->write(data, len);
        if (bytesWritten != len) {
            throw QInstaller::Error(tr("Write failed after %1 bytes: %2")
                .arg(QString::number(resource->size() - left), out->errorString()));
        }
        left -= len;
    }
}


/*!
    \class QInstaller::ResourceCollection
    \inmodule QtInstallerFramework
    \brief The ResourceCollection class is an abstraction that groups together a number of resources.

    The resources are supposed to be sequential, so the collection keeps them ordered once a new
    resource is added. The name can be set at any time using setName().
*/

/*!
    The class constructor creates an empty resource collection. By default the collection gets a
    unique name assigned using QUuid.
*/
ResourceCollection::ResourceCollection()
    : m_name(QUuid::createUuid().toByteArray())
{
}

/*!
    The class constructor creates an empty resource collection with a name set to \a name.
*/
ResourceCollection::ResourceCollection(const QByteArray &name)
    : m_name(name)
{}

/*!
    Returns the name of the resource collection.
*/
QByteArray ResourceCollection::name() const
{
    return m_name;
}

/*!
    Sets the name of the resource collection to \a name.
*/
void ResourceCollection::setName(const QByteArray &name)
{
    m_name = name;
}

/*!
    Appends \a resource to this collection. The collection takes ownership of \a resource.
 */
void ResourceCollection::appendResource(const QSharedPointer<Resource>& resource)
{
    Q_ASSERT(resource);
    resource->setParent(nullptr);
    m_resources.append(resource);
}

/*!
    Appends a list of \a resources to this collection. The collection takes ownership of \a
    resources.
*/
void ResourceCollection::appendResources(const QList<QSharedPointer<Resource> > &resources)
{
    foreach (const QSharedPointer<Resource> &resource, resources)
        appendResource(resource);
}

/*!
    Returns the resources associated with this collection.
*/
QList<QSharedPointer<Resource> > ResourceCollection::resources() const
{
    return m_resources;
}

/*!
    Returns the resource associated with the name \a name.
*/
QSharedPointer<Resource> ResourceCollection::resourceByName(const QByteArray &name) const
{
    foreach (const QSharedPointer<Resource>& i, m_resources) {
        if (i->name() == name)
            return i;
    }
    return QSharedPointer<Resource>();
}


/*!
    \class QInstaller::ResourceCollectionManager
    \inmodule QtInstallerFramework
    \brief The ResourceCollectionManager class is an abstraction that groups together a number of
        resource collections.

    The resource collections it groups can be written to and read from a QFileDevice.
*/

/*!
    Reads the resource collection from the file \a dev. The \a offset argument is used to
    set the collection's resources segment information.
*/
void ResourceCollectionManager::read(QFileDevice *dev, qint64 offset)
{
    const qint64 size = QInstaller::retrieveInt64(dev);
    for (int i = 0; i < size; ++i) {
        ResourceCollection collection(QInstaller::retrieveByteArray(dev));
        const Range<qint64> segment = QInstaller::retrieveInt64Range(dev).moved(offset);

        const qint64 pos = dev->pos();

        dev->seek(segment.start());
        const qint64 count = QInstaller::retrieveInt64(dev);
        for (int i = 0; i < count; ++i) {
            QSharedPointer<Resource> resource(new Resource(dev->fileName()));
            resource->setName(QInstaller::retrieveByteArray(dev));
            resource->setSegment(QInstaller::retrieveInt64Range(dev).moved(offset));
            collection.appendResource(resource);
        }
        dev->seek(pos);

        insertCollection(collection);
    }
}

/*!
    Writes the resource collection to the file \a out. The \a offset argument is used to
    set the collection's segment information.
*/
Range<qint64> ResourceCollectionManager::write(QFileDevice *out, qint64 offset) const
{
    QHash < QByteArray, Range<qint64> > table;
    QInstaller::appendInt64(out, collectionCount());
    foreach (const ResourceCollection &collection, m_collections) {
        const qint64 dataBegin = out->pos();
        QInstaller::appendInt64(out, collection.resources().count());

        qint64 start = out->pos() + offset;
        foreach (const QSharedPointer<Resource> &resource, collection.resources()) {
            start += (sizeof(qint64))   // the number of bytes that get written and the
            + resource->name().size()   // resource name (see QInstaller::appendByteArray)
            + (2 * sizeof(qint64));     // the resource range (see QInstaller::appendInt64Range)
        }

        foreach (const QSharedPointer<Resource> &resource, collection.resources()) {
            QInstaller::appendByteArray(out, resource->name());
            QInstaller::appendInt64Range(out, Range<qint64>::fromStartAndLength(start,
                resource->size()));     // the actual range once the table has been written
            start += resource->size();  // adjust for next resource data
        }

        foreach (const QSharedPointer<Resource> &resource, collection.resources()) {
            if (!resource->open()) {
                throw QInstaller::Error(tr("Cannot open resource %1: %2")
                    .arg(QString::fromUtf8(resource->name()), resource->errorString()));
            }
            resource->copyData(out);
        }

        table.insert(collection.name(), Range<qint64>::fromStartAndEnd(dataBegin, out->pos())
            .moved(offset));
    }

    const qint64 start = out->pos();

    // Q: why do we write the size twice?
    // A: for us to be able to read it beginning from the end of the file as well
    QInstaller::appendInt64(out, collectionCount());
    foreach (const QByteArray &name, table.keys()) {
        QInstaller::appendByteArray(out, name);
        QInstaller::appendInt64Range(out, table.value(name));
    }
    QInstaller::appendInt64(out, collectionCount());

    return Range<qint64>::fromStartAndEnd(start, out->pos());
}

/*!
    Returns the collection associated with the name \a name.
*/
ResourceCollection ResourceCollectionManager::collectionByName(const QByteArray &name) const
{
    return m_collections.value(name);
}

/*!
    Inserts the \a collection into the collection manager.
*/
void ResourceCollectionManager::insertCollection(const ResourceCollection& collection)
{
    m_collections.insert(collection.name(), collection);
}

/*!
    Removes all occurrences of \a name from the collection manager.
*/
void ResourceCollectionManager::removeCollection(const QByteArray &name)
{
    m_collections.remove(name);
}

/*!
    Returns the collections the collection manager contains.
*/
QList<ResourceCollection> ResourceCollectionManager::collections() const
{
    return m_collections.values();
}

/*!
    Clears the contents of the collection manager.
*/
void ResourceCollectionManager::clear()
{
    m_collections.clear();
}

/*!
    Returns the number of collections in the collection manager.
*/
int ResourceCollectionManager::collectionCount() const
{
    return m_collections.count();
}

} // namespace QInstaller