| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | // Qt-Security score:significant reason:default |
| 4 | |
| 5 | #ifndef QABSTRACTFILEENGINE_P_H |
| 6 | #define QABSTRACTFILEENGINE_P_H |
| 7 | |
| 8 | // |
| 9 | // W A R N I N G |
| 10 | // ------------- |
| 11 | // |
| 12 | // This file is not part of the Qt API. It exists purely as an |
| 13 | // implementation detail. This header file may change from version to |
| 14 | // version without notice, or even be removed. |
| 15 | // |
| 16 | // We mean it. |
| 17 | // |
| 18 | |
| 19 | #include <QtCore/private/qglobal_p.h> |
| 20 | #include "QtCore/qfile.h" |
| 21 | #include "QtCore/qdir.h" |
| 22 | #include "QtCore/qdirlisting.h" |
| 23 | |
| 24 | #include <memory> |
| 25 | #include <optional> |
| 26 | |
| 27 | #ifdef open |
| 28 | #error qabstractfileengine_p.h must be included before any header file that defines open |
| 29 | #endif |
| 30 | |
| 31 | QT_BEGIN_NAMESPACE |
| 32 | |
| 33 | class QVariant; |
| 34 | class QAbstractFileEngineIterator; |
| 35 | class QAbstractFileEnginePrivate; |
| 36 | |
| 37 | class Q_CORE_EXPORT QAbstractFileEngine |
| 38 | { |
| 39 | public: |
| 40 | enum FileFlag { |
| 41 | //perms (overlaps the QFile::Permission) |
| 42 | ReadOwnerPerm = 0x4000, WriteOwnerPerm = 0x2000, ExeOwnerPerm = 0x1000, |
| 43 | ReadUserPerm = 0x0400, WriteUserPerm = 0x0200, ExeUserPerm = 0x0100, |
| 44 | ReadGroupPerm = 0x0040, WriteGroupPerm = 0x0020, ExeGroupPerm = 0x0010, |
| 45 | ReadOtherPerm = 0x0004, WriteOtherPerm = 0x0002, ExeOtherPerm = 0x0001, |
| 46 | |
| 47 | //types |
| 48 | LinkType = 0x10000, |
| 49 | FileType = 0x20000, |
| 50 | DirectoryType = 0x40000, |
| 51 | BundleType = 0x80000, |
| 52 | |
| 53 | //flags |
| 54 | HiddenFlag = 0x0100000, |
| 55 | LocalDiskFlag = 0x0200000, |
| 56 | ExistsFlag = 0x0400000, |
| 57 | RootFlag = 0x0800000, |
| 58 | Refresh = 0x1000000, |
| 59 | |
| 60 | //masks |
| 61 | PermsMask = 0x0000FFFF, |
| 62 | TypesMask = 0x000F0000, |
| 63 | FlagsMask = 0x0FF00000, |
| 64 | FileInfoAll = FlagsMask | PermsMask | TypesMask |
| 65 | }; |
| 66 | Q_DECLARE_FLAGS(FileFlags, FileFlag) |
| 67 | |
| 68 | enum FileName { |
| 69 | DefaultName, |
| 70 | BaseName, |
| 71 | PathName, |
| 72 | AbsoluteName, |
| 73 | AbsolutePathName, |
| 74 | AbsoluteLinkTarget, |
| 75 | CanonicalName, |
| 76 | CanonicalPathName, |
| 77 | BundleName, |
| 78 | JunctionName, |
| 79 | RawLinkPath, |
| 80 | NFileNames // Must be last. |
| 81 | }; |
| 82 | enum FileOwner { |
| 83 | OwnerUser, |
| 84 | OwnerGroup |
| 85 | }; |
| 86 | |
| 87 | enum class TriStateResult : qint8 { |
| 88 | NotSupported = -1, |
| 89 | Failed = 0, |
| 90 | Success = 1, |
| 91 | }; |
| 92 | |
| 93 | virtual ~QAbstractFileEngine(); |
| 94 | |
| 95 | virtual bool open(QIODevice::OpenMode openMode, |
| 96 | std::optional<QFile::Permissions> permissions = std::nullopt); |
| 97 | virtual bool close(); |
| 98 | virtual bool flush(); |
| 99 | virtual bool syncToDisk(); |
| 100 | virtual qint64 size() const; |
| 101 | virtual qint64 pos() const; |
| 102 | virtual bool seek(qint64 pos); |
| 103 | virtual bool isSequential() const; |
| 104 | virtual bool remove(); |
| 105 | virtual bool copy(const QString &newName); |
| 106 | virtual bool rename(const QString &newName); |
| 107 | virtual bool renameOverwrite(const QString &newName); |
| 108 | virtual bool link(const QString &newName); |
| 109 | virtual bool mkdir(const QString &dirName, bool createParentDirectories, |
| 110 | std::optional<QFile::Permissions> permissions = std::nullopt) const; |
| 111 | virtual bool rmdir(const QString &dirName, bool recurseParentDirectories) const; |
| 112 | virtual bool setSize(qint64 size); |
| 113 | virtual bool caseSensitive() const; |
| 114 | virtual bool isRelativePath() const; |
| 115 | virtual QStringList entryList(QDir::Filters filters, const QStringList &filterNames) const; |
| 116 | virtual QStringList entryList(QDirListing::IteratorFlags filters, |
| 117 | const QStringList &filterNames) const; |
| 118 | virtual FileFlags fileFlags(FileFlags type=FileInfoAll) const; |
| 119 | virtual bool setPermissions(uint perms); |
| 120 | virtual QByteArray id() const; |
| 121 | virtual QString fileName(FileName file=DefaultName) const; |
| 122 | virtual uint ownerId(FileOwner) const; |
| 123 | virtual QString owner(FileOwner) const; |
| 124 | virtual bool setFileTime(const QDateTime &newDate, QFile::FileTime time); |
| 125 | virtual QDateTime fileTime(QFile::FileTime time) const; |
| 126 | virtual void setFileName(const QString &file); |
| 127 | virtual int handle() const; |
| 128 | virtual TriStateResult cloneTo(QAbstractFileEngine *target); |
| 129 | bool atEnd() const; |
| 130 | uchar *map(qint64 offset, qint64 size, QFile::MemoryMapFlags flags); |
| 131 | bool unmap(uchar *ptr); |
| 132 | |
| 133 | typedef QAbstractFileEngineIterator Iterator; |
| 134 | using IteratorUniquePtr = std::unique_ptr<Iterator>; |
| 135 | |
| 136 | virtual IteratorUniquePtr endEntryList() { return {}; } |
| 137 | virtual IteratorUniquePtr |
| 138 | beginEntryList(const QString &path, QDirListing::IteratorFlags filters, |
| 139 | const QStringList &filterNames); |
| 140 | |
| 141 | virtual qint64 read(char *data, qint64 maxlen); |
| 142 | virtual qint64 readLine(char *data, qint64 maxlen); |
| 143 | virtual qint64 write(const char *data, qint64 len); |
| 144 | |
| 145 | QFile::FileError error() const; |
| 146 | QString errorString() const; |
| 147 | |
| 148 | enum Extension { |
| 149 | AtEndExtension, |
| 150 | FastReadLineExtension, |
| 151 | MapExtension, |
| 152 | UnMapExtension |
| 153 | }; |
| 154 | class ExtensionOption |
| 155 | {}; |
| 156 | class ExtensionReturn |
| 157 | {}; |
| 158 | |
| 159 | class MapExtensionOption : public ExtensionOption { |
| 160 | Q_DISABLE_COPY_MOVE(MapExtensionOption) |
| 161 | public: |
| 162 | qint64 offset; |
| 163 | qint64 size; |
| 164 | QFile::MemoryMapFlags flags; |
| 165 | constexpr MapExtensionOption(qint64 off, qint64 sz, QFile::MemoryMapFlags f) |
| 166 | : offset(off), size(sz), flags(f) {} |
| 167 | }; |
| 168 | class MapExtensionReturn : public ExtensionReturn { |
| 169 | Q_DISABLE_COPY_MOVE(MapExtensionReturn) |
| 170 | public: |
| 171 | MapExtensionReturn() = default; |
| 172 | uchar *address = nullptr; |
| 173 | }; |
| 174 | |
| 175 | class UnMapExtensionOption : public ExtensionOption { |
| 176 | Q_DISABLE_COPY_MOVE(UnMapExtensionOption) |
| 177 | public: |
| 178 | uchar *address = nullptr; |
| 179 | constexpr UnMapExtensionOption(uchar *p) : address(p) {} |
| 180 | }; |
| 181 | |
| 182 | virtual bool extension(Extension extension, const ExtensionOption *option = nullptr, ExtensionReturn *output = nullptr); |
| 183 | virtual bool supportsExtension(Extension extension) const; |
| 184 | |
| 185 | // Factory |
| 186 | static std::unique_ptr<QAbstractFileEngine> create(const QString &fileName); |
| 187 | |
| 188 | protected: |
| 189 | void setError(QFile::FileError error, const QString &str); |
| 190 | |
| 191 | QAbstractFileEngine(); |
| 192 | QAbstractFileEngine(QAbstractFileEnginePrivate &); |
| 193 | |
| 194 | QScopedPointer<QAbstractFileEnginePrivate> d_ptr; |
| 195 | private: |
| 196 | Q_DECLARE_PRIVATE(QAbstractFileEngine) |
| 197 | Q_DISABLE_COPY_MOVE(QAbstractFileEngine) |
| 198 | }; |
| 199 | |
| 200 | Q_DECLARE_OPERATORS_FOR_FLAGS(QAbstractFileEngine::FileFlags) |
| 201 | |
| 202 | class Q_CORE_EXPORT QAbstractFileEngineHandler |
| 203 | { |
| 204 | Q_DISABLE_COPY_MOVE(QAbstractFileEngineHandler) |
| 205 | public: |
| 206 | QAbstractFileEngineHandler(); |
| 207 | virtual ~QAbstractFileEngineHandler(); |
| 208 | virtual std::unique_ptr<QAbstractFileEngine> create(const QString &fileName) const = 0; |
| 209 | }; |
| 210 | |
| 211 | class Q_CORE_EXPORT QAbstractFileEngineIterator |
| 212 | { |
| 213 | public: |
| 214 | QAbstractFileEngineIterator(const QString &path, QDir::Filters filters, |
| 215 | const QStringList &nameFilters); |
| 216 | QAbstractFileEngineIterator(const QString &path, QDirListing::IteratorFlags filters, |
| 217 | const QStringList &nameFilters); |
| 218 | virtual ~QAbstractFileEngineIterator(); |
| 219 | |
| 220 | virtual bool advance() = 0; |
| 221 | |
| 222 | QString path() const; |
| 223 | QStringList nameFilters() const; |
| 224 | QDir::Filters filters() const; |
| 225 | |
| 226 | virtual QString currentFileName() const = 0; |
| 227 | virtual QFileInfo currentFileInfo() const; |
| 228 | virtual QString currentFilePath() const; |
| 229 | |
| 230 | protected: |
| 231 | mutable QFileInfo m_fileInfo; |
| 232 | |
| 233 | private: |
| 234 | Q_DISABLE_COPY_MOVE(QAbstractFileEngineIterator) |
| 235 | friend class QDirIterator; |
| 236 | friend class QDirIteratorPrivate; |
| 237 | friend class QDirListingPrivate; |
| 238 | |
| 239 | QDir::Filters m_filters; |
| 240 | QDirListing::IteratorFlags m_listingFilters; |
| 241 | QStringList m_nameFilters; |
| 242 | QString m_path; |
| 243 | }; |
| 244 | |
| 245 | class QAbstractFileEnginePrivate |
| 246 | { |
| 247 | public: |
| 248 | inline QAbstractFileEnginePrivate(QAbstractFileEngine *q) |
| 249 | : fileError(QFile::UnspecifiedError), q_ptr(q) |
| 250 | { |
| 251 | } |
| 252 | virtual ~QAbstractFileEnginePrivate(); |
| 253 | |
| 254 | QFile::FileError fileError; |
| 255 | QString errorString; |
| 256 | |
| 257 | QAbstractFileEngine *const q_ptr; |
| 258 | Q_DECLARE_PUBLIC(QAbstractFileEngine) |
| 259 | }; |
| 260 | |
| 261 | std::unique_ptr<QAbstractFileEngine> qt_custom_file_engine_handler_create(const QString &path); |
| 262 | |
| 263 | QT_END_NAMESPACE |
| 264 | |
| 265 | #endif // QABSTRACTFILEENGINE_P_H |
| 266 | |