File indexing completed on 2024-12-15 04:01:21

0001 /*
0002  * SPDX-FileCopyrightText: 2019-2023 Mattia Basaglia <dev@dragon.best>
0003  *
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  */
0006 
0007 #pragma once
0008 
0009 #include <functional>
0010 #include <memory>
0011 
0012 #include <QString>
0013 #include <QDir>
0014 
0015 #include "app/log/log_line.hpp"
0016 
0017 namespace glaxnimate::utils::tar {
0018 
0019 class ArchiveEntry
0020 {
0021 public:
0022     ArchiveEntry(const ArchiveEntry& oth);
0023     ArchiveEntry(ArchiveEntry&& oth);
0024     ArchiveEntry& operator=(const ArchiveEntry& oth);
0025     ArchiveEntry& operator=(ArchiveEntry&& oth);
0026     bool operator==(const ArchiveEntry& oth) const;
0027     bool operator!=(const ArchiveEntry& oth) const;
0028     ~ArchiveEntry();
0029     const QString& path() const;
0030     bool valid() const;
0031 
0032 private:
0033     class Private;
0034     ArchiveEntry(std::unique_ptr<Private> d);
0035     std::unique_ptr<Private> d;
0036     friend class TapeArchive;
0037 };
0038 
0039 class TapeArchive : public QObject
0040 {
0041     Q_OBJECT
0042 
0043 public:
0044     class iterator
0045     {
0046     public:
0047         iterator(TapeArchive* archive, ArchiveEntry entry)
0048             : archive(archive), entry(std::move(entry))
0049         {}
0050 
0051         const ArchiveEntry* operator->() const { return &entry; }
0052         const ArchiveEntry& operator*() const { return entry; }
0053         iterator& operator++()
0054         {
0055             entry = archive->next();
0056             return *this;
0057 
0058         };
0059         bool operator==(const iterator& oth) const
0060         {
0061             return entry == oth.entry && archive == oth.archive;
0062         }
0063         bool operator!=(const iterator& oth) const
0064         {
0065             return !(*this == oth);
0066         }
0067 
0068     private:
0069         TapeArchive* archive;
0070         ArchiveEntry entry;
0071     };
0072 
0073     explicit TapeArchive(const QString& filename);
0074     explicit TapeArchive(const QByteArray& data);
0075     ~TapeArchive();
0076 
0077     bool extract(const ArchiveEntry& entry, const QDir& destination);
0078 
0079     bool finished() const;
0080     ArchiveEntry next();
0081     iterator begin();
0082     iterator end();
0083 
0084     const QString& error() const;
0085 
0086 Q_SIGNALS:
0087     void message(const QString& message, app::log::Severity Severity);
0088 
0089 private:
0090     class Private;
0091     std::unique_ptr<Private> d;
0092     friend class ArchiveEntry;
0093 };
0094 
0095 
0096 QString libarchive_version();
0097 
0098 } // namespace glaxnimate::utils::tar