1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QFSFILEENGINE_P_H
41#define QFSFILEENGINE_P_H
42
43//
44// W A R N I N G
45// -------------
46//
47// This file is not part of the Qt API. It exists purely as an
48// implementation detail. This header file may change from version to
49// version without notice, or even be removed.
50//
51// We mean it.
52//
53
54#include "qplatformdefs.h"
55#include "QtCore/private/qabstractfileengine_p.h"
56#include <QtCore/private/qfilesystementry_p.h>
57#include <QtCore/private/qfilesystemmetadata_p.h>
58#include <qhash.h>
59
60#ifndef QT_NO_FSFILEENGINE
61
62QT_BEGIN_NAMESPACE
63
64struct ProcessOpenModeResult {
65 bool ok;
66 QIODevice::OpenMode openMode;
67 QString error;
68};
69Q_CORE_EXPORT ProcessOpenModeResult processOpenModeFlags(QIODevice::OpenMode mode);
70
71class QFSFileEnginePrivate;
72
73class Q_CORE_EXPORT QFSFileEngine : public QAbstractFileEngine
74{
75 Q_DECLARE_PRIVATE(QFSFileEngine)
76public:
77 QFSFileEngine();
78 explicit QFSFileEngine(const QString &file);
79 ~QFSFileEngine();
80
81 bool open(QIODevice::OpenMode openMode) override;
82 bool open(QIODevice::OpenMode flags, FILE *fh);
83 bool close() override;
84 bool flush() override;
85 bool syncToDisk() override;
86 qint64 size() const override;
87 qint64 pos() const override;
88 bool seek(qint64) override;
89 bool isSequential() const override;
90 bool remove() override;
91 bool copy(const QString &newName) override;
92 bool rename(const QString &newName) override;
93 bool renameOverwrite(const QString &newName) override;
94 bool link(const QString &newName) override;
95 bool mkdir(const QString &dirName, bool createParentDirectories) const override;
96 bool rmdir(const QString &dirName, bool recurseParentDirectories) const override;
97 bool setSize(qint64 size) override;
98 bool caseSensitive() const override;
99 bool isRelativePath() const override;
100 QStringList entryList(QDir::Filters filters, const QStringList &filterNames) const override;
101 FileFlags fileFlags(FileFlags type) const override;
102 bool setPermissions(uint perms) override;
103 QByteArray id() const override;
104 QString fileName(FileName file) const override;
105 uint ownerId(FileOwner) const override;
106 QString owner(FileOwner) const override;
107 bool setFileTime(const QDateTime &newDate, FileTime time) override;
108 QDateTime fileTime(FileTime time) const override;
109 void setFileName(const QString &file) override;
110 int handle() const override;
111
112#ifndef QT_NO_FILESYSTEMITERATOR
113 Iterator *beginEntryList(QDir::Filters filters, const QStringList &filterNames) override;
114 Iterator *endEntryList() override;
115#endif
116
117 qint64 read(char *data, qint64 maxlen) override;
118 qint64 readLine(char *data, qint64 maxlen) override;
119 qint64 write(const char *data, qint64 len) override;
120 bool cloneTo(QAbstractFileEngine *target) override;
121
122 virtual bool isUnnamedFile() const
123 { return false; }
124
125 bool extension(Extension extension, const ExtensionOption *option = nullptr, ExtensionReturn *output = nullptr) override;
126 bool supportsExtension(Extension extension) const override;
127
128 //FS only!!
129 bool open(QIODevice::OpenMode flags, int fd);
130 bool open(QIODevice::OpenMode flags, int fd, QFile::FileHandleFlags handleFlags);
131 bool open(QIODevice::OpenMode flags, FILE *fh, QFile::FileHandleFlags handleFlags);
132 static bool setCurrentPath(const QString &path);
133 static QString currentPath(const QString &path = QString());
134 static QString homePath();
135 static QString rootPath();
136 static QString tempPath();
137 static QFileInfoList drives();
138
139protected:
140 QFSFileEngine(QFSFileEnginePrivate &dd);
141};
142
143class Q_AUTOTEST_EXPORT QFSFileEnginePrivate : public QAbstractFileEnginePrivate
144{
145 Q_DECLARE_PUBLIC(QFSFileEngine)
146
147public:
148#ifdef Q_OS_WIN
149 static QString longFileName(const QString &path);
150#endif
151
152 QFileSystemEntry fileEntry;
153 QIODevice::OpenMode openMode;
154
155 bool nativeOpen(QIODevice::OpenMode openMode);
156 bool openFh(QIODevice::OpenMode flags, FILE *fh);
157 bool openFd(QIODevice::OpenMode flags, int fd);
158 bool nativeClose();
159 bool closeFdFh();
160 bool nativeFlush();
161 bool nativeSyncToDisk();
162 bool flushFh();
163 qint64 nativeSize() const;
164#ifndef Q_OS_WIN
165 qint64 sizeFdFh() const;
166#endif
167 qint64 nativePos() const;
168 qint64 posFdFh() const;
169 bool nativeSeek(qint64);
170 bool seekFdFh(qint64);
171 qint64 nativeRead(char *data, qint64 maxlen);
172 qint64 readFdFh(char *data, qint64 maxlen);
173 qint64 nativeReadLine(char *data, qint64 maxlen);
174 qint64 readLineFdFh(char *data, qint64 maxlen);
175 qint64 nativeWrite(const char *data, qint64 len);
176 qint64 writeFdFh(const char *data, qint64 len);
177 int nativeHandle() const;
178 bool nativeIsSequential() const;
179#ifndef Q_OS_WIN
180 bool isSequentialFdFh() const;
181#endif
182
183 uchar *map(qint64 offset, qint64 size, QFile::MemoryMapFlags flags);
184 bool unmap(uchar *ptr);
185 void unmapAll();
186
187 mutable QFileSystemMetaData metaData;
188
189 FILE *fh;
190
191#ifdef Q_OS_WIN
192 HANDLE fileHandle;
193 HANDLE mapHandle;
194 QHash<uchar *, DWORD /* offset % AllocationGranularity */> maps;
195
196 mutable int cachedFd;
197 mutable DWORD fileAttrib;
198#else
199 QHash<uchar *, QPair<int /*offset % PageSize*/, size_t /*length + offset % PageSize*/> > maps;
200#endif
201 int fd;
202
203 enum LastIOCommand
204 {
205 IOFlushCommand,
206 IOReadCommand,
207 IOWriteCommand
208 };
209 LastIOCommand lastIOCommand;
210 bool lastFlushFailed;
211 bool closeFileHandle;
212
213 mutable uint is_sequential : 2;
214 mutable uint tried_stat : 1;
215 mutable uint need_lstat : 1;
216 mutable uint is_link : 1;
217
218#if defined(Q_OS_WIN)
219 bool doStat(QFileSystemMetaData::MetaDataFlags flags) const;
220#else
221 bool doStat(QFileSystemMetaData::MetaDataFlags flags = QFileSystemMetaData::PosixStatFlags) const;
222#endif
223 bool isSymlink() const;
224
225#if defined(Q_OS_WIN32)
226 int sysOpen(const QString &, int flags);
227#endif
228
229 static bool openModeCanCreate(QIODevice::OpenMode openMode)
230 {
231 // WriteOnly can create, but only when ExistingOnly isn't specified.
232 // ReadOnly by itself never creates.
233 return (openMode & QFile::WriteOnly) && !(openMode & QFile::ExistingOnly);
234 }
235protected:
236 QFSFileEnginePrivate();
237
238 void init();
239
240 QAbstractFileEngine::FileFlags getPermissions(QAbstractFileEngine::FileFlags type) const;
241};
242
243QT_END_NAMESPACE
244
245#endif // QT_NO_FSFILEENGINE
246
247#endif // QFSFILEENGINE_P_H
248