Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qfile.h
Go to the documentation of this file.
1// Copyright (C) 2020 The Qt Company Ltd.
2// Copyright (C) 2016 Intel Corporation.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4// Qt-Security score:significant reason:default
5
6#ifndef QFILE_H
7#define QFILE_H
8
9#include <QtCore/qfiledevice.h>
10#include <QtCore/qstring.h>
11#include <stdio.h>
12
13#ifdef open
14#error qfile.h must be included before any header file that defines open
15#endif
16
17QT_BEGIN_NAMESPACE
18
19#if defined(Q_OS_WIN) || defined(Q_QDOC)
20
21#if QT_DEPRECATED_SINCE(6,6)
22QT_DEPRECATED_VERSION_X_6_6("Use QNtfsPermissionCheckGuard RAII class instead.")
23Q_CORE_EXPORT extern int qt_ntfs_permission_lookup; // defined in qfilesystemengine_win.cpp
24#endif
25
26Q_CORE_EXPORT bool qEnableNtfsPermissionChecks() noexcept;
27Q_CORE_EXPORT bool qDisableNtfsPermissionChecks() noexcept;
28Q_CORE_EXPORT bool qAreNtfsPermissionChecksEnabled() noexcept;
29
30class QNtfsPermissionCheckGuard
31{
32 Q_DISABLE_COPY_MOVE(QNtfsPermissionCheckGuard)
33public:
34 Q_NODISCARD_CTOR
35 QNtfsPermissionCheckGuard()
36 {
37 qEnableNtfsPermissionChecks();
38 }
39
40 ~QNtfsPermissionCheckGuard()
41 {
42 qDisableNtfsPermissionChecks();
43 }
44};
45#endif // Q_OS_WIN
46
47#if QT_CONFIG(cxx17_filesystem)
48namespace QtPrivate {
49// Both std::filesystem::path and QString (without QT_NO_CAST_FROM_ASCII) can be implicitly
50// constructed from string literals so we force the std::fs::path parameter to only
51// accept std::fs::path with no implicit conversions.
52// ### Qt7: use Q_WEAK_OVERLOAD
53template<typename T>
54using ForceFilesystemPath = typename std::enable_if_t<std::is_same_v<std::filesystem::path, T>, int>;
55}
56#endif // QT_CONFIG(cxx17_filesystem)
57
58class QTemporaryFile;
59class QFilePrivate;
60
61// ### Qt 7: remove this, and make constructors always explicit.
62#if (QT_VERSION >= QT_VERSION_CHECK(6, 9, 0)) || defined(QT_EXPLICIT_QFILE_CONSTRUCTION_FROM_PATH)
63# define QFILE_MAYBE_EXPLICIT explicit
64#else
65# define QFILE_MAYBE_EXPLICIT Q_IMPLICIT
66#endif
67
68class Q_CORE_EXPORT QFile : public QFileDevice
69{
70#ifndef QT_NO_QOBJECT
71 Q_OBJECT
72#endif
73 Q_DECLARE_PRIVATE(QFile)
74
75public:
76 QFile();
77 QFILE_MAYBE_EXPLICIT QFile(const QString &name);
78#ifdef Q_QDOC
79 QFILE_MAYBE_EXPLICIT QFile(const std::filesystem::path &name);
80#elif QT_CONFIG(cxx17_filesystem)
81 template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
82 QFILE_MAYBE_EXPLICIT QFile(const T &name) : QFile(QtPrivate::fromFilesystemPath(name))
83 {
84 }
85#endif // QT_CONFIG(cxx17_filesystem)
86
87#ifndef QT_NO_QOBJECT
88 explicit QFile(QObject *parent);
89 QFile(const QString &name, QObject *parent);
90
91#ifdef Q_QDOC
92 QFile(const std::filesystem::path &path, QObject *parent);
93#elif QT_CONFIG(cxx17_filesystem)
94 template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
95 QFile(const T &path, QObject *parent) : QFile(QtPrivate::fromFilesystemPath(path), parent)
96 {
97 }
98#endif // QT_CONFIG(cxx17_filesystem)
99#endif // !QT_NO_QOBJECT
100 ~QFile();
101
102 QString fileName() const override;
103#if QT_CONFIG(cxx17_filesystem) || defined(Q_QDOC)
104 std::filesystem::path filesystemFileName() const
105 { return QtPrivate::toFilesystemPath(fileName()); }
106#endif
107 void setFileName(const QString &name);
108#ifdef Q_QDOC
109 void setFileName(const std::filesystem::path &name);
110#elif QT_CONFIG(cxx17_filesystem)
111 template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
112 void setFileName(const T &name)
113 {
114 setFileName(QtPrivate::fromFilesystemPath(name));
115 }
116#endif // QT_CONFIG(cxx17_filesystem)
117
118#if defined(Q_OS_DARWIN)
119 // Mac always expects filenames in UTF-8... and decomposed...
120 static inline QByteArray encodeName(const QString &fileName)
121 {
122 return fileName.normalized(QString::NormalizationForm_D).toUtf8();
123 }
124 static QString decodeName(const QByteArray &localFileName)
125 {
126 // note: duplicated in qglobal.cpp (qEnvironmentVariable)
127 return QString::fromUtf8(localFileName).normalized(QString::NormalizationForm_C);
128 }
129 static inline QString decodeName(const char *localFileName)
130 {
131 return QString::fromUtf8(localFileName).normalized(QString::NormalizationForm_C);
132 }
133#else
134 static inline QByteArray encodeName(const QString &fileName)
135 {
136 return fileName.toLocal8Bit();
137 }
138 static QString decodeName(const QByteArray &localFileName)
139 {
140 return QString::fromLocal8Bit(localFileName);
141 }
142 static inline QString decodeName(const char *localFileName)
143 {
144 return QString::fromLocal8Bit(localFileName);
145 }
146#endif
147
148 bool exists() const;
149 static bool exists(const QString &fileName);
150#ifdef Q_QDOC
151 static bool exists(const std::filesystem::path &fileName);
152#elif QT_CONFIG(cxx17_filesystem)
153 template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
154 static bool exists(const T &fileName)
155 {
156 return exists(QtPrivate::fromFilesystemPath(fileName));
157 }
158#endif // QT_CONFIG(cxx17_filesystem)
159
160 QString symLinkTarget() const;
161 static QString symLinkTarget(const QString &fileName);
162#ifdef Q_QDOC
163 std::filesystem::path filesystemSymLinkTarget() const;
164 static std::filesystem::path filesystemSymLinkTarget(const std::filesystem::path &fileName);
165#elif QT_CONFIG(cxx17_filesystem)
166 std::filesystem::path filesystemSymLinkTarget() const
167 {
168 return QtPrivate::toFilesystemPath(symLinkTarget());
169 }
170 template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
171 static std::filesystem::path filesystemSymLinkTarget(const T &fileName)
172 {
173 return QtPrivate::toFilesystemPath(symLinkTarget(QtPrivate::fromFilesystemPath(fileName)));
174 }
175#endif // QT_CONFIG(cxx17_filesystem)
176
177 bool remove();
178 static bool remove(const QString &fileName);
179#ifdef Q_QDOC
180 static bool remove(const std::filesystem::path &fileName);
181#elif QT_CONFIG(cxx17_filesystem)
182 template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
183 static bool remove(const T &fileName)
184 {
185 return remove(QtPrivate::fromFilesystemPath(fileName));
186 }
187#endif // QT_CONFIG(cxx17_filesystem)
188
189 Q_DECL_PURE_FUNCTION static bool supportsMoveToTrash();
190 bool moveToTrash();
191 static bool moveToTrash(const QString &fileName, QString *pathInTrash = nullptr);
192#ifdef Q_QDOC
193 static bool moveToTrash(const std::filesystem::path &fileName, QString *pathInTrash = nullptr);
194#elif QT_CONFIG(cxx17_filesystem)
195 template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
196 static bool moveToTrash(const T &fileName, QString *pathInTrash = nullptr)
197 {
198 return moveToTrash(QtPrivate::fromFilesystemPath(fileName), pathInTrash);
199 }
200#endif // QT_CONFIG(cxx17_filesystem)
201
202 bool rename(const QString &newName);
203 static bool rename(const QString &oldName, const QString &newName);
204#ifdef Q_QDOC
205 bool rename(const std::filesystem::path &newName);
206 static bool rename(const std::filesystem::path &oldName,
207 const std::filesystem::path &newName);
208#elif QT_CONFIG(cxx17_filesystem)
209 template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
210 bool rename(const T &newName)
211 {
212 return rename(QtPrivate::fromFilesystemPath(newName));
213 }
214 template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
215 static bool rename(const T &oldName, const T &newName)
216 {
217 return rename(QtPrivate::fromFilesystemPath(oldName),
218 QtPrivate::fromFilesystemPath(newName));
219 }
220#endif // QT_CONFIG(cxx17_filesystem)
221
222 bool link(const QString &newName);
223 static bool link(const QString &fileName, const QString &newName);
224#ifdef Q_QDOC
225 bool link(const std::filesystem::path &newName);
226 static bool link(const std::filesystem::path &fileName,
227 const std::filesystem::path &newName);
228#elif QT_CONFIG(cxx17_filesystem)
229 template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
230 bool link(const T &newName)
231 {
232 return link(QtPrivate::fromFilesystemPath(newName));
233 }
234 template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
235 static bool link(const T &fileName, const T &newName)
236 {
237 return link(QtPrivate::fromFilesystemPath(fileName),
238 QtPrivate::fromFilesystemPath(newName));
239 }
240#endif // QT_CONFIG(cxx17_filesystem)
241
242#if QT_CONFIG(temporaryfile)
243 bool copy(const QString &newName);
244 static bool copy(const QString &fileName, const QString &newName);
245#endif
246#ifdef Q_QDOC
247 bool copy(const std::filesystem::path &newName);
248 static bool copy(const std::filesystem::path &fileName,
249 const std::filesystem::path &newName);
250#elif QT_CONFIG(cxx17_filesystem) && QT_CONFIG(temporaryfile)
251 template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
252 bool copy(const T &newName)
253 {
254 return copy(QtPrivate::fromFilesystemPath(newName));
255 }
256 template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
257 static bool copy(const T &fileName, const T &newName)
258 {
259 return copy(QtPrivate::fromFilesystemPath(fileName),
260 QtPrivate::fromFilesystemPath(newName));
261 }
262#endif // QT_CONFIG(cxx17_filesystem)
263
264 QFILE_MAYBE_NODISCARD bool open(OpenMode flags) override;
265 QFILE_MAYBE_NODISCARD bool open(OpenMode flags, Permissions permissions);
266 QFILE_MAYBE_NODISCARD bool open(FILE *f, OpenMode ioFlags, FileHandleFlags handleFlags=DontCloseHandle);
267 QFILE_MAYBE_NODISCARD bool open(int fd, OpenMode ioFlags, FileHandleFlags handleFlags=DontCloseHandle);
268
269 qint64 size() const override;
270
271 bool resize(qint64 sz) override;
272 static bool resize(const QString &filename, qint64 sz);
273
274 Permissions permissions() const override;
275 static Permissions permissions(const QString &filename);
276 bool setPermissions(Permissions permissionSpec) override;
277 static bool setPermissions(const QString &filename, Permissions permissionSpec);
278#ifdef Q_QDOC
279 static Permissions permissions(const std::filesystem::path &filename);
280 static bool setPermissions(const std::filesystem::path &filename, Permissions permissionSpec);
281#elif QT_CONFIG(cxx17_filesystem)
282 template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
283 static Permissions permissions(const T &filename)
284 {
285 return permissions(QtPrivate::fromFilesystemPath(filename));
286 }
287 template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
288 static bool setPermissions(const T &filename, Permissions permissionSpec)
289 {
290 return setPermissions(QtPrivate::fromFilesystemPath(filename), permissionSpec);
291 }
292#endif // QT_CONFIG(cxx17_filesystem)
293
294protected:
295#ifdef QT_NO_QOBJECT
296 QFile(QFilePrivate &dd);
297#else
298 QFile(QFilePrivate &dd, QObject *parent = nullptr);
299#endif
300
301private:
302 friend class QTemporaryFile;
303 Q_DISABLE_COPY(QFile)
304};
305
306QT_END_NAMESPACE
307
308#endif // QFILE_H
#define ARCH_FULL
\inmodule QtCore
Definition qfile.h:69
Combined button and popup list for selecting options.
#define QFILE_MAYBE_EXPLICIT
Definition qfile.h:63
static const char * qt_build_string() noexcept
static bool pathIsRelative(const QString &path)
static QString getPrefix(QLibraryInfoPrivate::UsageMode usageMode)
void qDumpCPUFeatures()
Definition qsimd.cpp:688
static bool pathIsAbsolute(const QString &path)
static QString prefixFromAppDirHelper()
#define SHARED_STRING
static bool keepQtBuildDefaults()
#define COMPILER_STRING
#define DEBUG_STRING
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
qsizetype indexOf(const QString &str, qsizetype from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
bool contains(const QString &str, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
QStringList filter(const QLatin1StringMatcher &matcher) const
void sort(Qt::CaseSensitivity cs=Qt::CaseSensitive)
Definition qstringlist.h:88
QString join(const QString &sep) const
qsizetype indexOf(QStringView needle, qsizetype from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
QStringList & replaceInStrings(const QString &before, const QString &after, Qt::CaseSensitivity cs=Qt::CaseSensitive)
QString join(QStringView sep) const
Definition qstringlist.h:93
qsizetype lastIndexOf(QStringView str, qsizetype from=-1, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
QStringList filter(QLatin1StringView needle, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
QStringList filter(const QString &str, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
QStringList & replaceInStrings(QStringView before, const QString &after, Qt::CaseSensitivity cs=Qt::CaseSensitive)
QStringList & replaceInStrings(QStringView before, QStringView after, Qt::CaseSensitivity cs=Qt::CaseSensitive)
QStringList & replaceInStrings(const QString &before, QStringView after, Qt::CaseSensitivity cs=Qt::CaseSensitive)
QStringList filter(const QStringMatcher &matcher) const
qsizetype lastIndexOf(const QString &str, qsizetype from=-1, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept