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