summaryrefslogtreecommitdiffstats
path: root/src/libs/installer/libarchivearchive.cpp
blob: 0ef317c67f0621a6f9c6c6bd4c716360aa8e9d57 (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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
/**************************************************************************
**
** 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 "libarchivearchive.h"

#include "directoryguard.h"
#include "fileguard.h"
#include "errors.h"
#include "globals.h"

#include <stdio.h>
#include <string.h>

#include <QApplication>
#include <QFileInfo>
#include <QDir>
#include <QTimer>

#ifdef Q_OS_WIN
#include <locale.h>
#endif

#if defined(Q_OS_WIN) && !defined(SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE)
#define SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE
#endif

namespace QInstaller {

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

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

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

namespace ArchiveEntryPaths {

// TODO: it is expected that the filename handling will change in the major
// version jump to libarchive 4.x, and the *_w methods will disappear.

/*!
    \internal

    Returns the path name from the archive \a entry as a \c QString. The path is
    stored internally in a multistring that can contain a combination of a wide
    character or multibyte string in current locale or unicode string encoded as UTF-8.

    \note The MBS version is expected to be convertable from latin-1 which might
    not actually be the case, as the encoding depends on the current locale.
*/
static QString pathname(archive_entry *const entry)
{
    if (!entry)
        return QString();
#ifdef Q_OS_WIN
    if (const wchar_t *path = archive_entry_pathname_w(entry))
        return QString::fromWCharArray(path);
#endif
    if (const char *path = archive_entry_pathname_utf8(entry))
        return QString::fromUtf8(path);

    return QString::fromLatin1(archive_entry_pathname(entry));
}

/*!
    \internal

    Sets the path name for the archive \a entry to \a path. This function
    tries to update all variants of the string in the internal multistring
    struct.
*/
static void setPathname(archive_entry *const entry, const QString &path)
{
    if (!entry)
        return;

    // Try updating all variants at once, stops on failure
    if (archive_entry_update_pathname_utf8(entry, path.toUtf8()))
        return;

    // If that does not work, then set them individually
    archive_entry_set_pathname(entry, path.toLatin1());
    archive_entry_set_pathname_utf8(entry, path.toUtf8());
#ifdef Q_OS_WIN
    wchar_t *wpath = new wchar_t[path.length() + 1];
    path.toWCharArray(wpath);
    wpath[path.length()] = '\0';

    archive_entry_copy_pathname_w(entry, wpath);
    delete[] wpath;
#endif
}

/*!
    \internal

    Returns the source path on disk from the current archive \a entry as a \c QString.
    The path is stored internally in a multistring that can contain a combination
    of a wide character or multibyte string in current locale.

    \note The MBS version is expected to be convertable from UTF-8.
*/
static QString sourcepath(archive_entry * const entry)
{
    if (!entry)
        return QString();
#ifdef Q_OS_WIN
    if (const wchar_t *path = archive_entry_sourcepath_w(entry))
        return QString::fromWCharArray(path);
#endif
    return QString::fromUtf8(archive_entry_sourcepath(entry));
}

/*!
    \internal

    Returns the hardlink path from the current archive \a entry as a \c QString.
    The path is stored internally in a multistring that can contain a combination of a wide
    character or multibyte string in current locale or unicode string encoded as UTF-8.

    \note The MBS version is expected to be convertable from latin-1 which might
    not actually be the case, as the encoding depends on the current locale.
*/
static QString hardlink(archive_entry * const entry)
{
    if (!entry)
        return QString();
#ifdef Q_OS_WIN
    if (const wchar_t *path = archive_entry_hardlink_w(entry))
        return QString::fromWCharArray(path);
#endif
    if (const char *path = archive_entry_hardlink_utf8(entry))
        return QString::fromUtf8(path);

    return QString::fromLatin1(archive_entry_hardlink(entry));
}

/*!
    \internal

    Sets the hard link path for the archive \a entry to \a path. This function
    tries to update all variants of the string in the internal multistring
    struct.
*/
static void setHardlink(archive_entry *const entry, const QString &path)
{
    if (!entry)
        return;

    // Try updating all variants at once, stops on failure
    if (archive_entry_update_hardlink_utf8(entry, path.toUtf8()))
        return;

    // If that does not work, then set them individually
    archive_entry_set_hardlink(entry, path.toLatin1());
    archive_entry_set_hardlink_utf8(entry, path.toUtf8());
#ifdef Q_OS_WIN
    wchar_t *wpath = new wchar_t[path.length() + 1];
    path.toWCharArray(wpath);
    wpath[path.length()] = '\0';

    archive_entry_copy_hardlink_w(entry, wpath);
    delete[] wpath;
#endif
}

/*!
    \internal

    Calls a function object or pointer \a func with any number of extra
    arguments \a args. Returns the return value of the function of type T.

    On Windows, this changes the locale category LC_CTYPE from C to system
    locale. The original LC_CTYPE is restored after the function call.
    Currently the locale is unchanged on other platforms.
*/
template <typename T, typename F, typename... Args>
static T callWithSystemLocale(F func, Args... args)
{
#ifdef Q_OS_WIN
    const QByteArray oldLocale = setlocale(LC_CTYPE, "");
#endif
    T returnValue = func(std::forward<Args>(args)...);
#ifdef Q_OS_WIN
    setlocale(LC_CTYPE, oldLocale.constData());
#endif
    return returnValue;
}

/*!
    \internal

    Calls a function object or pointer \a func with any number of extra
    arguments \a args.

    On Windows, this changes the locale category LC_CTYPE from C to system
    locale. The original LC_CTYPE is restored after the function call.
    Currently the locale is unchanged on other platforms.
*/
template <typename F, typename... Args>
static void callWithSystemLocale(F func, Args... args)
{
#ifdef Q_OS_WIN
    const QByteArray oldLocale = setlocale(LC_CTYPE, "");
#endif
    func(std::forward<Args>(args)...);
#ifdef Q_OS_WIN
    setlocale(LC_CTYPE, oldLocale.constData());
#endif
}

} // namespace ArchiveEntryPaths

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

ExtractWorker::Status ExtractWorker::status() const
{
    return m_status;
}

void ExtractWorker::extract(const QString &dirPath, const quint64 totalFiles)
{
    m_status = Unfinished;
    quint64 completed = 0;

    if (!totalFiles) {
        m_status = Failure;
        emit finished(QLatin1String("The file count for current archive is null!"));
        return;
    }

    QScopedPointer<archive, ScopedPointerReaderDeleter> reader(archive_read_new());
    QScopedPointer<archive, ScopedPointerWriterDeleter> writer(archive_write_disk_new());
    archive_entry *entry = nullptr;

    LibArchiveArchive::configureReader(reader.get());
    LibArchiveArchive::configureDiskWriter(writer.get());

    DirectoryGuard targetDir(QFileInfo(dirPath).absolutePath());
    try {
        const QStringList createdDirs = targetDir.tryCreate();
        // Make sure that all leading directories created get removed as well
        foreach (const QString &directory, createdDirs)
            emit currentEntryChanged(directory);

        archive_read_set_read_callback(reader.get(), readCallback);
        archive_read_set_callback_data(reader.get(), this);
        archive_read_set_seek_callback(reader.get(), seekCallback);

        int status = archive_read_open1(reader.get());
        if (status != ARCHIVE_OK) {
            m_status = Failure;
            emit finished(tr("Cannot open archive for reading: %1")
                .arg(LibArchiveArchive::errorStringWithCode(reader.get())));
            return;
        }

        forever {
            if (m_status == Canceled) {
                emit finished(QLatin1String("Extract canceled."));
                return;
            }
            status = ArchiveEntryPaths::callWithSystemLocale<int>(archive_read_next_header, reader.get(), &entry);
            if (status == ARCHIVE_EOF)
                break;
            if (status != ARCHIVE_OK) {
                m_status = Failure;
                emit finished(tr("Cannot read entry header: %1")
                    .arg(LibArchiveArchive::errorStringWithCode(reader.get())));
                return;
            }
            const QString current = ArchiveEntryPaths::callWithSystemLocale<QString>(ArchiveEntryPaths::pathname, entry);
            const QString outputPath = dirPath + QDir::separator() + current;
            ArchiveEntryPaths::callWithSystemLocale(&ArchiveEntryPaths::setPathname, entry, outputPath);

            const QString hardlink = ArchiveEntryPaths::callWithSystemLocale<QString>(ArchiveEntryPaths::hardlink, entry);
            if (!hardlink.isEmpty()) {
                const QString hardLinkPath = dirPath + QDir::separator() + hardlink;
                ArchiveEntryPaths::callWithSystemLocale(ArchiveEntryPaths::setHardlink, entry, hardLinkPath);
            }

            emit currentEntryChanged(outputPath);
            if (!writeEntry(reader.get(), writer.get(), entry))
                return;

            ++completed;
            emit completedChanged(completed, totalFiles);

            qApp->processEvents();
        }
    } catch (const Error &e) {
        m_status = Failure;
        emit finished(e.message());
        return;
    }
    targetDir.release();
    m_status = Success;
    emit finished();
}

void ExtractWorker::addDataBlock(const QByteArray buffer)
{
    m_buffer.append(buffer);
    emit dataReadyForRead();
}

void ExtractWorker::onFilePositionChanged(qint64 pos)
{
    m_lastPos = pos;
    emit seekReady();
}

void ExtractWorker::cancel()
{
    m_status = Canceled;
}

ssize_t ExtractWorker::readCallback(archive *reader, void *caller, const void **buff)
{
    Q_UNUSED(reader)

    ExtractWorker *obj;
    if (!(obj = static_cast<ExtractWorker *>(caller)))
        return ARCHIVE_FATAL;

    QByteArray *buffer = &obj->m_buffer;
    if (!buffer->isEmpty())
        buffer->clear();

    emit obj->dataBlockRequested();

    // It's a bit bad that we have to wait here, but libarchive doesn't
    // provide an event based reading method.
    {
        QEventLoop loop;
        QTimer::singleShot(30000, &loop, &QEventLoop::quit);
        connect(obj, &ExtractWorker::dataReadyForRead, &loop, &QEventLoop::quit);
        connect(obj, &ExtractWorker::dataAtEnd, &loop, &QEventLoop::quit);
        loop.exec();
    }

    if (!(*buff = static_cast<const void *>(buffer->constData())))
        return ARCHIVE_FATAL;

    return buffer->size();
}

la_int64_t ExtractWorker::seekCallback(archive *reader, void *caller, la_int64_t offset, int whence)
{
    Q_UNUSED(reader)

    ExtractWorker *obj;
    if (!(obj = static_cast<ExtractWorker *>(caller)))
        return ARCHIVE_FATAL;

    emit obj->seekRequested(static_cast<qint64>(offset), whence);

    {
        QEventLoop loop;
        QTimer::singleShot(30000, &loop, &QEventLoop::quit);
        connect(obj, &ExtractWorker::seekReady, &loop, &QEventLoop::quit);
        loop.exec();
    }

    return static_cast<la_int64_t>(obj->m_lastPos);
}

bool ExtractWorker::writeEntry(archive *reader, archive *writer, archive_entry *entry)
{
    int status;
    const void *buff;
    size_t size;
    int64_t offset;

    const QString entryPath = ArchiveEntryPaths::callWithSystemLocale
        <QString>(ArchiveEntryPaths::pathname, entry);

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

    status = archive_write_header(writer, entry);
    if (status != ARCHIVE_OK) {
        emit finished(tr("Cannot write entry \"%1\" to disk: %2")
            .arg(entryPath, LibArchiveArchive::errorStringWithCode(writer)));
        return false;
    }

    forever {
        status = archive_read_data_block(reader, &buff, &size, &offset);
        if (status == ARCHIVE_EOF) {
            status = archive_write_finish_entry(writer);
            if (status == ARCHIVE_OK)
                return true;
        }
        if (status != ARCHIVE_OK) {
            m_status = Failure;
            emit finished(tr("Cannot write entry \"%1\" to disk: %2")
                .arg(entryPath, LibArchiveArchive::errorStringWithCode(reader)));
            return false;
        }
        status = archive_write_data_block(writer, buff, size, offset);
        if (status != ARCHIVE_OK) {
            m_status = Failure;
            emit finished(tr("Cannot write entry \"%1\" to disk: %2")
                .arg(entryPath, LibArchiveArchive::errorStringWithCode(writer)));
            return false;
        }
    }
}

/*!
    \inmodule QtInstallerFramework
    \class QInstaller::LibArchiveArchive
    \brief The LibArchiveArchive class represents an archive file
           handled with libarchive archive and compression library.

    In addition to extracting data from the underlying file device,
    the class supports a non-blocking mode of extracting from an external
    data source. When using this mode, the calling client must pass the data
    to be read in chunks of arbitrary size, and inform the object when there
    is no more data to read.
*/

/*!
    \inmodule QtInstallerFramework
    \class QInstaller::LibArchiveArchive::ArchiveData
    \brief Bundles a file device and associated read buffer for access
           as client data in libarchive callbacks.
*/

/*!
    \fn QInstaller::LibArchiveArchive::dataBlockRequested()

    Emitted when the worker object requires more data to continue extracting.
*/

/*!
    \fn QInstaller::LibArchiveArchive::seekRequested(qint64 offset, int whence)

    Emitted when the worker object requires a seek to \a offset to continue extracting.
    The \a whence value defines the starting position for \a offset.
*/

/*!
    \fn QInstaller::LibArchiveArchive::workerFinished()

    Emitted when the worker object finished extracting an archive.
*/

/*!
    \fn QInstaller::LibArchiveArchive::workerAboutToExtract(const QString &dirPath, const quint64 totalFiles)

    Emitted when the worker object is about to extract \a totalFiles
    from an archive to \a dirPath.
*/

/*!
    \fn QInstaller::LibArchiveArchive::workerAboutToAddDataBlock(const QByteArray buffer)

    Emitted when the worker object is about to add and read the data block in \a buffer.
*/

/*!
    \fn QInstaller::LibArchiveArchive::workerAboutToSetDataAtEnd()

    Emitted when the worker object is about to set data-at-end, meaning there
    will be no further read requests for the calling client.
*/

/*!
    \fn QInstaller::LibArchiveArchive::workerAboutToCancel()

    Emitted when the worker object is about to cancel extracting.
*/

/*!
    \fn QInstaller::LibArchiveArchive::workerAboutToSetFilePosition(qint64 pos)

    Emitted when the worker object is about to set new file position \a pos.
*/

/*!
    Constructs an archive object representing an archive file
    specified by \a filename with \a parent as the parent object.
*/
LibArchiveArchive::LibArchiveArchive(const QString &filename, QObject *parent)
    : AbstractArchive(parent)
    , m_data(new ArchiveData())
    , m_cancelScheduled(false)
{
    LibArchiveArchive::setFilename(filename);
    initExtractWorker();
}

/*!
    Constructs an archive object with the given \a parent.
*/
LibArchiveArchive::LibArchiveArchive(QObject *parent)
    : AbstractArchive(parent)
    , m_data(new ArchiveData())
    , m_cancelScheduled(false)
{
    initExtractWorker();
}

/*!
    Destroys the instance and releases resources.
*/
LibArchiveArchive::~LibArchiveArchive()
{
    m_workerThread.quit();
    m_workerThread.wait();

    delete m_data;
}

/*!
    \reimp

    Opens the underlying file device using \a mode. Returns \c true if
    succesfull; otherwise \c false.
*/
bool LibArchiveArchive::open(QIODevice::OpenMode mode)
{
    if (!m_data->file.open(mode)) {
        setErrorString(m_data->file.errorString());
        return false;
    }
    return true;
}

/*!
    \reimp

    Closes the underlying file device.
*/
void LibArchiveArchive::close()
{
    m_data->file.close();
}

/*!
    \reimp

    Sets the \a filename of the underlying file device.
*/
void LibArchiveArchive::setFilename(const QString &filename)
{
    m_data->file.setFileName(filename);
}

/*!
    \reimp

    Extracts the contents of this archive to \a dirPath.
    Returns \c true on success; \c false otherwise.
*/
bool LibArchiveArchive::extract(const QString &dirPath)
{
    return extract(dirPath, totalFiles());
}

/*!
    \reimp

    Extracts the contents of this archive to \a dirPath with
    precalculated count of \a totalFiles. Returns \c true on
    success; \c false otherwise.
*/
bool LibArchiveArchive::extract(const QString &dirPath, const quint64 totalFiles)
{
    m_cancelScheduled = false;
    quint64 completed = 0;
    if (!totalFiles) {
        setErrorString(QLatin1String("The file count for current archive is null!"));
        return false;
    }

    QScopedPointer<archive, ScopedPointerReaderDeleter> reader(archive_read_new());
    QScopedPointer<archive, ScopedPointerWriterDeleter> writer(archive_write_disk_new());
    archive_entry *entry = nullptr;

    configureReader(reader.get());
    configureDiskWriter(writer.get());

    DirectoryGuard targetDir(QFileInfo(dirPath).absolutePath());
    try {
        const QStringList createdDirs = targetDir.tryCreate();
        // Make sure that all leading directories created get removed as well
        foreach (const QString &directory, createdDirs)
            emit currentEntryChanged(directory);

        int status = archiveReadOpenWithCallbacks(reader.get());
        if (status != ARCHIVE_OK) {
            throw Error(tr("Cannot open archive for reading: %1")
                .arg(errorStringWithCode(reader.get())));
        }

        forever {
            if (m_cancelScheduled)
                throw Error(QLatin1String("Extract canceled."));

            status = ArchiveEntryPaths::callWithSystemLocale<int>(archive_read_next_header, reader.get(), &entry);
            if (status == ARCHIVE_EOF)
                break;
            if (status != ARCHIVE_OK) {
                throw Error(tr("Cannot read entry header: %1")
                    .arg(errorStringWithCode(reader.get())));
            }

            const QString current = ArchiveEntryPaths::callWithSystemLocale<QString>(ArchiveEntryPaths::pathname, entry);
            const QString outputPath = dirPath + QDir::separator() + current;
            ArchiveEntryPaths::callWithSystemLocale(ArchiveEntryPaths::setPathname, entry, outputPath);

            const QString hardlink = ArchiveEntryPaths::callWithSystemLocale<QString>(ArchiveEntryPaths::hardlink, entry);
            if (!hardlink.isEmpty()) {
                const QString hardLinkPath = dirPath + QDir::separator() + hardlink;
                ArchiveEntryPaths::callWithSystemLocale(ArchiveEntryPaths::setHardlink, entry, hardLinkPath);
            }

            emit currentEntryChanged(outputPath);
            if (!writeEntry(reader.get(), writer.get(), entry)) {
                throw Error(tr("Cannot write entry \"%1\" to disk: %2")
                    .arg(outputPath, errorString())); // appropriate error string set in writeEntry()
            }

            ++completed;
            emit completedChanged(completed, totalFiles);

            qApp->processEvents();
        }
    } catch (const Error &e) {
        setErrorString(e.message());
        m_data->file.seek(0);
        return false;
    }
    targetDir.release();
    m_data->file.seek(0);
    return true;
}

/*!
    \reimp

    Packages the given \a data into the archive and creates the file on disk.
*/
bool LibArchiveArchive::create(const QStringList &data)
{
    QScopedPointer<archive, ScopedPointerWriterDeleter> writer(archive_write_new());
    configureWriter(writer.get());

    QStringList globbedData;
    for (auto &dataEntry : data) { // Expand glob pattern entries with proper filenames
        if (!dataEntry.contains(QLatin1Char('*'))) {
            globbedData.append(dataEntry);
            continue;
        }
        const QFileInfo entryInfo(dataEntry);
        if (entryInfo.path().contains(QLatin1Char('*'))) {
            setErrorString(QString::fromLatin1("Invalid argument \"%1\": glob patterns "
                "are not supported between directory paths.").arg(dataEntry));
            return false;
        }
        const QDir parentDir = entryInfo.dir();
        const QList<QFileInfo> infoList = parentDir.entryInfoList(QStringList()
            << entryInfo.fileName(), QDir::AllEntries | QDir::Hidden | QDir::NoDotAndDotDot);

        for (auto &info : infoList)
            globbedData.append(info.absoluteFilePath());
    }

    try {
        int status;
#ifdef Q_OS_WIN
        QScopedPointer<wchar_t, QScopedPointerArrayDeleter<wchar_t>> fileName_w(
            new wchar_t[m_data->file.fileName().length() + 1]);

        m_data->file.fileName().toWCharArray(fileName_w.get());
        fileName_w.get()[m_data->file.fileName().length()] = '\0';

        if ((status = archive_write_open_filename_w(writer.get(), fileName_w.get()))) {
            throw Error(tr("Cannot open file \"%1\" for writing: %2")
                .arg(m_data->file.fileName(), errorStringWithCode(writer.get())));
        }
#else
        if ((status = archive_write_open_filename(writer.get(), m_data->file.fileName().toUtf8()))) {
            throw Error(tr("Cannot open file \"%1\" for writing: %2")
                .arg(m_data->file.fileName(), errorStringWithCode(writer.get())));
        }
#endif
        for (auto &dataEntry : globbedData) {
            QScopedPointer<archive, ScopedPointerReaderDeleter> reader(archive_read_disk_new());
            configureDiskReader(reader.get());

#ifdef Q_OS_WIN
            QScopedPointer<wchar_t, QScopedPointerArrayDeleter<wchar_t>> dataEntry_w(
                new wchar_t[dataEntry.length() + 1]);

            dataEntry.toWCharArray(dataEntry_w.get());
            dataEntry_w.get()[dataEntry.length()] = '\0';

            if ((status = archive_read_disk_open_w(reader.get(), dataEntry_w.get()))) {
                throw Error(tr("Cannot open file \"%1\" for reading: %2")
                    .arg(dataEntry, errorStringWithCode(reader.get())));
            }
#else
            if ((status = archive_read_disk_open(reader.get(), dataEntry.toUtf8()))) {
                throw Error(tr("Cannot open file \"%1\" for reading: %2")
                    .arg(dataEntry, errorStringWithCode(reader.get())));
            }
#endif
            QDir basePath = QFileInfo(dataEntry).dir();
            forever {
                QScopedPointer<archive_entry, ScopedPointerEntryDeleter> entry(archive_entry_new());
                status = ArchiveEntryPaths::callWithSystemLocale<int>(archive_read_next_header2, reader.get(), entry.get());
                if (status == ARCHIVE_EOF)
                    break;
                if (status != ARCHIVE_OK) {
                    throw Error(tr("Cannot read entry header: %1")
                        .arg(errorStringWithCode(reader.get())));
                }

                const QFileInfo fileOrDir(pathWithoutNamespace(
                    ArchiveEntryPaths::callWithSystemLocale<QString>(ArchiveEntryPaths::sourcepath, entry.get())));
                // Set new path name in archive, otherwise we add all directories from absolute path
                const QString newPath = basePath.relativeFilePath(fileOrDir.filePath());
                ArchiveEntryPaths::callWithSystemLocale(ArchiveEntryPaths::setPathname, entry.get(), newPath);

                archive_read_disk_descend(reader.get());
                status = archive_write_header(writer.get(), entry.get());
                if (status < ARCHIVE_OK) {
                    throw Error(tr("Cannot write entry header for \"%1\": %2")
                        .arg(fileOrDir.filePath(), errorStringWithCode(writer.get())));
                }
                if (fileOrDir.isDir() || archive_entry_size(entry.get()) == 0)
                    continue; // nothing to copy

                QFile file(pathWithoutNamespace(ArchiveEntryPaths::callWithSystemLocale<QString>(
                    ArchiveEntryPaths::sourcepath, entry.get())));
                if (!file.open(QIODevice::ReadOnly)) {
                    throw Error(tr("Cannot open file \"%1\" for reading: %2")
                        .arg(file.fileName(), file.errorString()));
                }

                QByteArray buffer;
                constexpr qint64 blockSize = 4 * 1024;
                buffer.resize(blockSize);

                ssize_t bytesRead = readData(&file, buffer.data(), blockSize);
                while (bytesRead > 0) {
                    archive_write_data(writer.get(), buffer.constData(), blockSize);
                    bytesRead = readData(&file, buffer.data(), blockSize);
                }
                file.close();
            }
        }
    } catch (const Error &e) {
        setErrorString(e.message());
        return false;
    }
    return true;
}

/*!
    \reimp

    Returns the contents of this archive as an array of \c ArchiveEntry objects.
    On failure, returns an empty array.
*/
QVector<ArchiveEntry> LibArchiveArchive::list()
{
    QScopedPointer<archive, ScopedPointerReaderDeleter> reader(archive_read_new());
    archive_entry *entry = nullptr;

    configureReader(reader.get());

    QVector<ArchiveEntry> entries;
    try {
        int status = archiveReadOpenWithCallbacks(reader.get());
        if (status != ARCHIVE_OK) {
            throw Error(tr("Cannot open archive for reading: %1")
                .arg(errorStringWithCode(reader.get())));
        }

        forever {
            status = ArchiveEntryPaths::callWithSystemLocale<int>(archive_read_next_header, reader.get(), &entry);
            if (status == ARCHIVE_EOF)
                break;
            if (status != ARCHIVE_OK) {
                throw Error(tr("Cannot read entry header: %1")
                    .arg(errorStringWithCode(reader.get())));
            }

            ArchiveEntry archiveEntry;
            archiveEntry.path = ArchiveEntryPaths::callWithSystemLocale<QString>(ArchiveEntryPaths::pathname, entry);
            archiveEntry.utcTime = QDateTime::fromTime_t(archive_entry_mtime(entry));
            archiveEntry.isDirectory = (archive_entry_filetype(entry) == AE_IFDIR);
            archiveEntry.isSymbolicLink = (archive_entry_filetype(entry) == AE_IFLNK);
            archiveEntry.uncompressedSize = archive_entry_size(entry);
            archiveEntry.permissions_mode = archive_entry_perm(entry);

            entries.append(archiveEntry);
        }
    } catch (const Error &e) {
        setErrorString(e.message());
        m_data->file.seek(0);
        return QVector<ArchiveEntry>();
    }
    m_data->file.seek(0);
    return entries;
}

/*!
    \reimp

    Returns \c true if the current archive is of a supported format;
    \c false otherwise.
*/
bool LibArchiveArchive::isSupported()
{
    QScopedPointer<archive, ScopedPointerReaderDeleter> reader(archive_read_new());
    configureReader(reader.get());

    try {
        const int status = archiveReadOpenWithCallbacks(reader.get());
        if (status != ARCHIVE_OK) {
            throw Error(tr("Cannot open archive for reading: %1")
                .arg(errorStringWithCode(reader.get())));
        }
    } catch (const Error &e) {
        setErrorString(e.message());
        m_data->file.seek(0);
        return false;
    }
    m_data->file.seek(0);
    return true;
}

/*!
    Requests to extract the archive to \a dirPath with \a totalFiles
    in a separate thread with a worker object.
*/
void LibArchiveArchive::workerExtract(const QString &dirPath, const quint64 totalFiles)
{
    emit workerAboutToExtract(dirPath, totalFiles);
}

/*!
    Adds data to be read by the worker object in \a buffer.
*/
void LibArchiveArchive::workerAddDataBlock(const QByteArray buffer)
{
    emit workerAboutToAddDataBlock(buffer);
}

/*!
    Signals the worker object that the client data is at end.
*/
void LibArchiveArchive::workerSetDataAtEnd()
{
    emit workerAboutToSetDataAtEnd();
}

/*!
    Signals the worker object that the client file position changed to \a pos.
*/
void LibArchiveArchive::workerSetFilePosition(qint64 pos)
{
    emit workerAboutToSetFilePosition(pos);
}

/*!
    Cancels the extract in progress for the worker object.
*/
void LibArchiveArchive::workerCancel()
{
    emit workerAboutToCancel();
}

/*!
    Returns the status of the worker object.
*/
ExtractWorker::Status LibArchiveArchive::workerStatus() const
{
    return m_worker.status();
}

/*!
    \reimp

    Cancels the extract in progress.
*/
void LibArchiveArchive::cancel()
{
    m_cancelScheduled = true;
}

/*!
    \internal
*/
void LibArchiveArchive::onWorkerFinished(const QString &errorString)
{
    setErrorString(errorString);
    emit workerFinished();
}

/*!
    \internal
*/
void LibArchiveArchive::configureReader(archive *archive)
{
    archive_read_support_filter_bzip2(archive);
    archive_read_support_filter_gzip(archive);
    archive_read_support_filter_xz(archive);

    archive_read_support_format_tar(archive);
    archive_read_support_format_zip(archive);
    archive_read_support_format_7zip(archive);
}

/*!
    \internal
*/
void LibArchiveArchive::configureWriter(archive *archive)
{
    const QString fileName = m_data->file.fileName();
    if (fileName.endsWith(QLatin1String(".qbsp"), Qt::CaseInsensitive)) {
        // The Qt board support package file extension is really a 7z.
        archive_write_set_format_7zip(archive);
    } else {
        archive_write_set_format_filter_by_ext(archive, fileName.toUtf8());
    }

    const QByteArray charset = "hdrcharset=UTF-8";
    // not checked as this is ignored on some archive formats like 7z
    archive_write_set_options(archive, charset);

    if (compressionLevel() == CompressionLevel::Normal)
        return;

    const QByteArray compression = "compression-level=" + QString::number(compressionLevel()).toLatin1();
    if (archive_write_set_options(archive, compression.constData())) { // not fatal
        qCWarning(QInstaller::lcInstallerInstallLog) << "Could not set option" << compression
            << "for archive" << m_data->file.fileName() << ":" << errorStringWithCode(archive);
    }
}

/*!
    \internal
*/
void LibArchiveArchive::configureDiskReader(archive *archive)
{
    archive_read_disk_set_standard_lookup(archive);
}

/*!
    \internal
*/
void LibArchiveArchive::configureDiskWriter(archive *archive)
{
    constexpr int flags = ARCHIVE_EXTRACT_TIME
        | ARCHIVE_EXTRACT_PERM
        | ARCHIVE_EXTRACT_ACL
        | ARCHIVE_EXTRACT_FFLAGS;

    archive_write_disk_set_options(archive, flags);
    archive_write_disk_set_standard_lookup(archive);
}

/*!
    \internal
*/
void LibArchiveArchive::initExtractWorker()
{
    m_worker.moveToThread(&m_workerThread);

    connect(this, &LibArchiveArchive::workerAboutToExtract, &m_worker, &ExtractWorker::extract);
    connect(this, &LibArchiveArchive::workerAboutToAddDataBlock, &m_worker, &ExtractWorker::addDataBlock);
    connect(this, &LibArchiveArchive::workerAboutToSetDataAtEnd, &m_worker, &ExtractWorker::dataAtEnd);
    connect(this, &LibArchiveArchive::workerAboutToSetFilePosition, &m_worker, &ExtractWorker::onFilePositionChanged);
    connect(this, &LibArchiveArchive::workerAboutToCancel, &m_worker, &ExtractWorker::cancel);

    connect(&m_worker, &ExtractWorker::dataBlockRequested, this, &LibArchiveArchive::dataBlockRequested);
    connect(&m_worker, &ExtractWorker::seekRequested, this, &LibArchiveArchive::seekRequested);
    connect(&m_worker, &ExtractWorker::finished, this, &LibArchiveArchive::onWorkerFinished);

    connect(&m_worker, &ExtractWorker::currentEntryChanged, this, &LibArchiveArchive::currentEntryChanged);
    connect(&m_worker, &ExtractWorker::completedChanged, this, &LibArchiveArchive::completedChanged);

    m_workerThread.start();
}

/*!
    \internal

    Sets default callbacks for archive \a reader and opens for reading.
*/
int LibArchiveArchive::archiveReadOpenWithCallbacks(archive *reader)
{
    archive_read_set_read_callback(reader, readCallback);
    archive_read_set_callback_data(reader, m_data);
    archive_read_set_seek_callback(reader, seekCallback);

    return archive_read_open1(reader);
}

/*!
    Writes the current \a entry header, then pulls data from the archive \a reader
    and writes it to the \a writer handle.
*/
bool LibArchiveArchive::writeEntry(archive *reader, archive *writer, archive_entry *entry)
{
    int status;
    const void *buff;
    size_t size;
    int64_t offset;

    const QString entryPath = ArchiveEntryPaths::callWithSystemLocale
        <QString>(ArchiveEntryPaths::pathname, entry);

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

    status = archive_write_header(writer, entry);
    if (status != ARCHIVE_OK) {
        setErrorString(errorStringWithCode(writer));
        return false;
    }

    forever {
        status = archive_read_data_block(reader, &buff, &size, &offset);
        if (status == ARCHIVE_EOF) {
            status = archive_write_finish_entry(writer);
            if (status == ARCHIVE_OK)
                return true;
        }
        if (status != ARCHIVE_OK) {
            setErrorString(errorStringWithCode(reader));
            return false;
        }
        status = archive_write_data_block(writer, buff, size, offset);
        if (status != ARCHIVE_OK) {
            setErrorString(errorStringWithCode(writer));
            return false;
        }
    }
}

/*!
    \internal

    Reads \a data from the current position of \a file. The maximum bytes to
    read are specified by \a maxSize. Returns the amount of bytes read.
*/
qint64 LibArchiveArchive::readData(QFile *file, char *data, qint64 maxSize)
{
    if (!file->isOpen() || file->isSequential())
        return ARCHIVE_FATAL;

    if (file->atEnd() && file->seek(0))
        return ARCHIVE_OK;

    const qint64 bytesRead = file->read(data, maxSize);
    if (bytesRead == -1)
        return ARCHIVE_FATAL;

    return bytesRead;
}

/*!
    \internal

    Called by libarchive when new data is needed. Reads data from the file device
    in \a archiveData into the buffer referenced by \a buff. Returns the number of bytes read.
*/
ssize_t LibArchiveArchive::readCallback(archive *reader, void *archiveData, const void **buff)
{
    Q_UNUSED(reader)
    constexpr qint64 blockSize = 1024 * 1024; // 1MB

    ArchiveData *data;
    if (!(data = static_cast<ArchiveData *>(archiveData)))
        return ARCHIVE_FATAL;

    if (!data->buffer.isEmpty())
        data->buffer.clear();

    if (data->buffer.size() != blockSize)
        data->buffer.resize(blockSize);

    if (!(*buff = static_cast<const void *>(data->buffer.constData())))
        return ARCHIVE_FATAL;

    // Doesn't matter if the buffer size exceeds the actual data read,
    // the return value indicates the length of relevant bytes.
    return readData(&data->file, data->buffer.data(), data->buffer.size());
}

/*!
    \internal

    Seeks to specified \a offset in the file device in \a archiveData and returns the position.
    Possible \a whence values are \c SEEK_SET, \c SEEK_CUR, and \c SEEK_END. Returns
    \c ARCHIVE_FATAL if the seek fails.
*/
la_int64_t LibArchiveArchive::seekCallback(archive *reader, void *archiveData, la_int64_t offset, int whence)
{
    Q_UNUSED(reader)

    ArchiveData *data;
    if (!(data = static_cast<ArchiveData *>(archiveData)))
        return ARCHIVE_FATAL;

    if (!data->file.isOpen() || data->file.isSequential())
        return ARCHIVE_FATAL;

    switch (whence) {
    case SEEK_SET: // moves file pointer position to the beginning of the file
        if (!data->file.seek(offset))
            return ARCHIVE_FATAL;
        break;
    case SEEK_CUR: // moves file pointer position to given location
        if (!data->file.seek(data->file.pos() + offset))
            return ARCHIVE_FATAL;
        break;
    case SEEK_END: // moves file pointer position to the end of file
        if (!data->file.seek(data->file.size() + offset))
            return ARCHIVE_FATAL;
        break;
    default:
        return ARCHIVE_FATAL;
    }
    return data->file.pos();
}

/*!
    Returns the \a path to a file or directory, without the Win32 namespace prefix.
    On Unix platforms, the \a path is returned unaltered.
*/
QString LibArchiveArchive::pathWithoutNamespace(const QString &path)
{
    QString aPath = path;
#ifdef Q_OS_WIN
    if (aPath.size() > 4 && aPath.at(0) == QLatin1Char('\\')
            && aPath.at(2) == QLatin1Char('?') && aPath.at(3) == QLatin1Char('\\')) {
        aPath = aPath.mid(4);
    }
#endif
    return aPath;
}

/*!
    Returns an error message and a textual representaion of the numeric error code
    indicating the reason for the most recent error return for the \a archive object.
*/
QString LibArchiveArchive::errorStringWithCode(archive *const archive)
{
    if (!archive)
        return QString();

    return QString::fromLatin1("%1: %2.").arg(
        QLatin1String(archive_error_string(archive)),
        QLatin1String(strerror(archive_errno(archive)))
    );
}

/*!
    Returns the number of files in this archive.
*/
quint64 LibArchiveArchive::totalFiles()
{
    QScopedPointer<archive, ScopedPointerReaderDeleter> reader(archive_read_new());
    archive_entry *entry = nullptr;
    quint64 files = 0;

    configureReader(reader.get());

    try {
        int status = archiveReadOpenWithCallbacks(reader.get());
        if (status != ARCHIVE_OK) {
            throw Error(tr("Cannot open archive for reading: %1")
                .arg(errorStringWithCode(reader.get())));
        }

        forever {
            status = ArchiveEntryPaths::callWithSystemLocale<int>(archive_read_next_header, reader.get(), &entry);
            if (status == ARCHIVE_EOF)
                break;
            if (status != ARCHIVE_OK) {
                throw Error(tr("Cannot read entry header: %1")
                    .arg(errorStringWithCode(reader.get())));
            }

            ++files;
        }
    } catch (const Error &e) {
        setErrorString(e.message());
        m_data->file.seek(0);
        return 0;
    }
    m_data->file.seek(0);
    return files;
}

} // namespace QInstaller