File indexing completed on 2024-05-12 05:54:58

0001 /*
0002     SPDX-FileCopyrightText: 2003 Rafi Yanai <yanai@users.sf.net>
0003     SPDX-FileCopyrightText: 2003 Shie Erlich <yanai@users.sf.net>
0004     SPDX-FileCopyrightText: 2004-2022 Krusader Krew <https://krusader.org>
0005     SPDX-FileCopyrightText: 2022 Harald Sitter <sitter@kde.org>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 #ifndef KRARC_H
0010 #define KRARC_H
0011 
0012 // QtCore
0013 #include <QFile>
0014 #include <QHash>
0015 #include <QString>
0016 #include <QUrl>
0017 
0018 #include <KIO/Global>
0019 #include <kservice_version.h>
0020 #if KSERVICE_VERSION >= QT_VERSION_CHECK(5, 96, 0)
0021 #include <KIO/WorkerBase>
0022 #else
0023 #include <KIO/SlaveBase>
0024 #endif
0025 
0026 #include <KCoreAddons/KProcess>
0027 
0028 #include "../../app/krdebuglogger.h"
0029 #include "krarcbasemanager.h"
0030 #include "krlinecountingprocess.h"
0031 
0032 class KFileItem;
0033 class QByteArray;
0034 class QTextCodec;
0035 
0036 #if KSERVICE_VERSION >= QT_VERSION_CHECK(5, 96, 0)
0037 class kio_krarcProtocol : public QObject, public KIO::WorkerBase, public KrArcBaseManager
0038 #else
0039 class kio_krarcProtocol : public QObject, public KIO::SlaveBase, public KrArcBaseManager
0040 #endif
0041 {
0042     Q_OBJECT
0043 public:
0044     kio_krarcProtocol(const QByteArray &pool_socket, const QByteArray &app_socket);
0045     ~kio_krarcProtocol() override;
0046 #if KSERVICE_VERSION >= QT_VERSION_CHECK(5, 96, 0)
0047     KIO::WorkerResult stat(const QUrl &url) override;
0048     KIO::WorkerResult get(const QUrl &url) override;
0049     KIO::WorkerResult put(const QUrl &url, int permissions, KIO::JobFlags flags) override;
0050     KIO::WorkerResult mkdir(const QUrl &url, int permissions) override;
0051     KIO::WorkerResult listDir(const QUrl &url) override;
0052     KIO::WorkerResult del(QUrl const &url, bool isFile) override;
0053     KIO::WorkerResult copy(const QUrl &src, const QUrl &dest, int permissions, KIO::JobFlags flags) override;
0054     KIO::WorkerResult rename(const QUrl &src, const QUrl &dest, KIO::JobFlags flags) override;
0055 #else
0056     void stat(const QUrl &url) override;
0057     void get(const QUrl &url) override;
0058     void put(const QUrl &url, int permissions, KIO::JobFlags flags) override;
0059     void mkdir(const QUrl &url, int permissions) override;
0060     void listDir(const QUrl &url) override;
0061     void del(QUrl const &url, bool isFile) override;
0062     void copy(const QUrl &src, const QUrl &dest, int permissions, KIO::JobFlags flags) override;
0063     void rename(const QUrl &src, const QUrl &dest, KIO::JobFlags flags) override;
0064 #endif
0065 public slots:
0066     void receivedData(KProcess *, QByteArray &);
0067     void check7zOutputForPassword(KProcess *, QByteArray &);
0068 
0069 protected:
0070 #if KSERVICE_VERSION >= QT_VERSION_CHECK(5, 96, 0)
0071     Q_REQUIRED_RESULT virtual bool initDirDict(const QUrl &url, bool forced = false);
0072     Q_REQUIRED_RESULT virtual KIO::WorkerResult initArcParameters();
0073 #else
0074     virtual bool initDirDict(const QUrl &url, bool forced = false);
0075     virtual bool initArcParameters();
0076 #endif
0077     void checkIf7zIsEncrypted(bool &, QString) override;
0078 #if KSERVICE_VERSION >= QT_VERSION_CHECK(5, 96, 0)
0079     Q_REQUIRED_RESULT virtual KIO::WorkerResult setArcFile(const QUrl &url);
0080     Q_REQUIRED_RESULT virtual QString getPassword();
0081 #else
0082     virtual bool setArcFile(const QUrl &url);
0083     virtual QString getPassword();
0084 #endif
0085     virtual void invalidatePassword();
0086     QString getPath(const QUrl &url, QUrl::FormattingOptions options = nullptr);
0087     /** parses a text line from the listing of an archive. */
0088     virtual void parseLine(int lineNo, QString line);
0089 
0090     QString localeEncodedString(QString str);
0091     QByteArray encodeString(const QString &);
0092     QString decodeString(char *);
0093 
0094     // archive specific commands
0095     QString cmd; ///< the archiver name.
0096     QStringList listCmd; ///< list files.
0097     QStringList getCmd; ///< unpack files command.
0098     QStringList delCmd; ///< delete files command.
0099     QStringList putCmd; ///< add file command.
0100     QStringList copyCmd; ///< copy to file command.
0101     QStringList renCmd; ///< rename file command.
0102 
0103 private:
0104 #if KSERVICE_VERSION >= QT_VERSION_CHECK(5, 96, 0)
0105     KIO::WorkerResult get(const QUrl &url, int tries);
0106 #else
0107     void get(const QUrl &url, int tries);
0108 #endif
0109     /** checks if a returned status ("exit code") of an archiving-related process is OK. */
0110     bool checkStatus(int exitCode);
0111     /** service function for parseLine. */
0112     QString nextWord(QString &s, char d = ' ');
0113     /** translate permission string to mode_t. */
0114     mode_t parsePermString(QString perm);
0115     /** return the name of the directory inside the archive. */
0116     QString findArcDirectory(const QUrl &url);
0117     /** find the UDSEntry of a file in a directory. */
0118     KIO::UDSEntry *findFileEntry(const QUrl &url);
0119     /** add a new directory (file list container). */
0120     KIO::UDSEntryList *addNewDir(const QString &path);
0121 #if KSERVICE_VERSION >= QT_VERSION_CHECK(5, 96, 0)
0122     Q_REQUIRED_RESULT KIO::WorkerResult checkWriteSupport();
0123 #else
0124     bool checkWriteSupport();
0125 #endif
0126 
0127     QHash<QString, KIO::UDSEntryList *> dirDict; //< the directories data structure.
0128     bool encrypted; //< tells whether the archive is encrypted
0129     bool archiveChanged; //< true if the archive was changed.
0130     bool archiveChanging; //< true if the archive is currently changing.
0131     bool newArchiveURL; //< true if new archive was entered for the protocol
0132     bool noencoding; //< 7z files use UTF-16, so encoding is unnecessary
0133     KIO::filesize_t decompressedLen; //< the number of the decompressed bytes
0134     KFileItem *arcFile; //< the archive file item.
0135     QString arcPath; //< the archive location
0136     QString arcTempDir; //< the currently used temp directory.
0137     QString arcType; //< the archive type.
0138     bool extArcReady; //< Used for RPM & DEB files.
0139     QString password; //< Password for the archives
0140 
0141     QString lastData;
0142     QString encryptedArchPath;
0143 
0144     QString currentCharset;
0145     QTextCodec *codec;
0146 };
0147 
0148 #ifdef Q_OS_WIN
0149 #define DIR_SEPARATOR "/"
0150 #define DIR_SEPARATOR2 "\\"
0151 #define DIR_SEPARATOR_CHAR '/'
0152 #define DIR_SEPARATOR_CHAR2 '\\'
0153 #define REPLACE_DIR_SEP2(x) x = x.replace(DIR_SEPARATOR2, DIR_SEPARATOR);
0154 #define ROOT_DIR "C:\\"
0155 #define EXEC_SUFFIX ".exe"
0156 #else
0157 #define DIR_SEPARATOR "/"
0158 #define DIR_SEPARATOR2 "/"
0159 #define DIR_SEPARATOR_CHAR '/'
0160 #define DIR_SEPARATOR_CHAR2 '/'
0161 #define REPLACE_DIR_SEP2(x)
0162 #define ROOT_DIR "/"
0163 #define EXEC_SUFFIX ""
0164 #endif
0165 
0166 #endif