1 /*
2     SPDX-FileCopyrightText: 2007 Henrique Pinto <henrique.pinto@kdemail.net>
3     SPDX-FileCopyrightText: 2008-2009 Harald Hvaal <haraldhv@stud.ntnu.no>
4     SPDX-FileCopyrightText: 2016 Vladyslav Batyrenko <mvlabat@gmail.com>
5 
6     SPDX-License-Identifier: BSD-2-Clause
7 */
8 
9 #ifndef LIBARCHIVEPLUGIN_H
10 #define LIBARCHIVEPLUGIN_H
11 
12 #include "archiveinterface.h"
13 
14 #include <archive.h>
15 
16 #include <QScopedPointer>
17 
18 using namespace Kerfuffle;
19 
20 class LibarchivePlugin : public ReadWriteArchiveInterface
21 {
22     Q_OBJECT
23 
24 public:
25     explicit LibarchivePlugin(QObject *parent, const QVariantList &args);
26     ~LibarchivePlugin() override;
27 
28     bool list() override;
29     bool doKill() override;
30     bool extractFiles(const QVector<Archive::Entry*> &files, const QString &destinationDirectory, const ExtractionOptions &options) override;
31 
32     bool addFiles(const QVector<Archive::Entry*> &files, const Archive::Entry *destination, const CompressionOptions &options, uint numberOfEntriesToAdd = 0) override;
33     bool moveFiles(const QVector<Archive::Entry*> &files, Archive::Entry *destination, const CompressionOptions &options) override;
34     bool copyFiles(const QVector<Archive::Entry*> &files, Archive::Entry *destination, const CompressionOptions &options) override;
35     bool deleteFiles(const QVector<Archive::Entry*> &files) override;
36     bool addComment(const QString &comment) override;
37     bool testArchive() override;
38     bool hasBatchExtractionProgress() const override;
39 
40 protected:
41     struct ArchiveReadCustomDeleter
42     {
cleanupArchiveReadCustomDeleter43         static inline void cleanup(struct archive *a)
44         {
45             if (a) {
46                 archive_read_free(a);
47             }
48         }
49     };
50 
51     struct ArchiveWriteCustomDeleter
52     {
cleanupArchiveWriteCustomDeleter53         static inline void cleanup(struct archive *a)
54         {
55             if (a) {
56                 archive_write_free(a);
57             }
58         }
59     };
60 
61     typedef QScopedPointer<struct archive, ArchiveReadCustomDeleter> ArchiveRead;
62     typedef QScopedPointer<struct archive, ArchiveWriteCustomDeleter> ArchiveWrite;
63 
64     bool initializeReader();
65     void emitEntryFromArchiveEntry(struct archive_entry *entry);
66     void copyData(const QString& filename, struct archive *dest, bool partialprogress = true);
67     void copyData(const QString& filename, struct archive *source, struct archive *dest, bool partialprogress = true);
68 
69     ArchiveRead m_archiveReader;
70     ArchiveRead m_archiveReadDisk;
71 
72 private Q_SLOTS:
73     void slotRestoreWorkingDir();
74 
75 private:
76     int extractionFlags() const;
77     QString convertCompressionName(const QString &method);
78     bool emitCorruptArchive();
79 
80     int m_cachedArchiveEntryCount;
81     qlonglong m_currentExtractedFilesSize;
82     bool m_emitNoEntries;
83     qlonglong m_extractedFilesSize;
84     QVector<Archive::Entry*> m_emittedEntries;
85     QString m_oldWorkingDir;
86 };
87 
88 #endif // LIBARCHIVEPLUGIN_H
89