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