File indexing completed on 2024-04-28 15:18:43

0001 /* This file is part of the KDE libraries
0002    SPDX-FileCopyrightText: 2000-2005 David Faure <faure@kde.org>
0003    SPDX-FileCopyrightText: 2003 Leo Savernik <l.savernik@aon.at>
0004 
0005    SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 #ifndef KTAR_H
0008 #define KTAR_H
0009 
0010 #include <karchive.h>
0011 
0012 /**
0013  * @class KTar ktar.h KTar
0014  *
0015  * A class for reading / writing (optionally compressed) tar archives.
0016  *
0017  * KTar allows you to read and write tar archives, including those
0018  * that are compressed using gzip, bzip2 or xz.
0019  *
0020  * @author Torben Weis <weis@kde.org>, David Faure <faure@kde.org>
0021  */
0022 class KARCHIVE_EXPORT KTar : public KArchive
0023 {
0024     Q_DECLARE_TR_FUNCTIONS(KTar)
0025 
0026 public:
0027     /**
0028      * Creates an instance that operates on the given filename
0029      * using the compression filter associated to given mimetype.
0030      *
0031      * @param filename is a local path (e.g. "/home/weis/myfile.tgz")
0032      * @param mimetype "application/gzip" (before 5.85: "application/x-gzip"), "application/x-bzip",
0033      * "application/x-xz", "application/zstd" (since 5.82)
0034      * Do not use application/x-compressed-tar or similar - you only need to
0035      * specify the compression layer !  If the mimetype is omitted, it
0036      * will be determined from the filename.
0037      */
0038     explicit KTar(const QString &filename, const QString &mimetype = QString());
0039 
0040     /**
0041      * Creates an instance that operates on the given device.
0042      * The device can be compressed (KCompressionDevice) or not (QFile, etc.).
0043      * @warning Do not assume that giving a QFile here will decompress the file,
0044      * in case it's compressed!
0045      * @param dev the device to read from. If the source is compressed, the
0046      * QIODevice must take care of decompression
0047      */
0048     explicit KTar(QIODevice *dev);
0049 
0050     /**
0051      * If the tar ball is still opened, then it will be
0052      * closed automatically by the destructor.
0053      */
0054     ~KTar() override;
0055 
0056     /**
0057      * Special function for setting the "original file name" in the gzip header,
0058      * when writing a tar.gz file. It appears when using in the "file" command,
0059      * for instance. Should only be called if the underlying device is a KCompressionDevice!
0060      * @param fileName the original file name
0061      */
0062     void setOrigFileName(const QByteArray &fileName);
0063 
0064 protected:
0065     /// Reimplemented from KArchive
0066     bool doWriteSymLink(const QString &name,
0067                         const QString &target,
0068                         const QString &user,
0069                         const QString &group,
0070                         mode_t perm,
0071                         const QDateTime &atime,
0072                         const QDateTime &mtime,
0073                         const QDateTime &ctime) override;
0074     /// Reimplemented from KArchive
0075     bool doWriteDir(const QString &name,
0076                     const QString &user,
0077                     const QString &group,
0078                     mode_t perm,
0079                     const QDateTime &atime,
0080                     const QDateTime &mtime,
0081                     const QDateTime &ctime) override;
0082     /// Reimplemented from KArchive
0083     bool doPrepareWriting(const QString &name,
0084                           const QString &user,
0085                           const QString &group,
0086                           qint64 size,
0087                           mode_t perm,
0088                           const QDateTime &atime,
0089                           const QDateTime &mtime,
0090                           const QDateTime &ctime) override;
0091     /// Reimplemented from KArchive
0092     bool doFinishWriting(qint64 size) override;
0093 
0094     /**
0095      * Opens the archive for reading.
0096      * Parses the directory listing of the archive
0097      * and creates the KArchiveDirectory/KArchiveFile entries.
0098      * @param mode the mode of the file
0099      */
0100     bool openArchive(QIODevice::OpenMode mode) override;
0101     bool closeArchive() override;
0102 
0103     bool createDevice(QIODevice::OpenMode mode) override;
0104 
0105 private:
0106 protected:
0107     void virtual_hook(int id, void *data) override;
0108 
0109 private:
0110     class KTarPrivate;
0111     KTarPrivate *const d;
0112 };
0113 
0114 #endif