1/****************************************************************************
2**
3** Copyright (C) 2020 The Qt Company Ltd.
4** Copyright (C) 2016 Intel Corporation.
5** Contact: https://www.qt.io/licensing/
6**
7** This file is part of the QtCore module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial License Usage
11** Licensees holding valid commercial Qt licenses may use this file in
12** accordance with the commercial license agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and The Qt Company. For licensing terms
15** and conditions see https://www.qt.io/terms-conditions. For further
16** information use the contact form at https://www.qt.io/contact-us.
17**
18** GNU Lesser General Public License Usage
19** Alternatively, this file may be used under the terms of the GNU Lesser
20** General Public License version 3 as published by the Free Software
21** Foundation and appearing in the file LICENSE.LGPL3 included in the
22** packaging of this file. Please review the following information to
23** ensure the GNU Lesser General Public License version 3 requirements
24** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
25**
26** GNU General Public License Usage
27** Alternatively, this file may be used under the terms of the GNU
28** General Public License version 2.0 or (at your option) the GNU General
29** Public license version 3 or any later version approved by the KDE Free
30** Qt Foundation. The licenses are as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
32** included in the packaging of this file. Please review the following
33** information to ensure the GNU General Public License requirements will
34** be met: https://www.gnu.org/licenses/gpl-2.0.html and
35** https://www.gnu.org/licenses/gpl-3.0.html.
36**
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41#ifndef QFILE_H
42#define QFILE_H
43
44#include <QtCore/qfiledevice.h>
45#include <QtCore/qstring.h>
46#include <stdio.h>
47
48#if QT_CONFIG(cxx17_filesystem)
49#include <filesystem>
50#elif defined(Q_CLANG_QDOC)
51namespace std {
52 namespace filesystem {
53 class path {
54 };
55 };
56};
57#endif
58
59#ifdef open
60#error qfile.h must be included before any header file that defines open
61#endif
62
63QT_BEGIN_NAMESPACE
64
65#if QT_CONFIG(cxx17_filesystem)
66namespace QtPrivate {
67inline QString fromFilesystemPath(const std::filesystem::path &path)
68{
69#ifdef Q_OS_WIN
70 return QString::fromStdWString(path.native());
71#else
72 return QString::fromStdString(path.native());
73#endif
74}
75
76inline std::filesystem::path toFilesystemPath(const QString &path)
77{
78 return std::filesystem::path(reinterpret_cast<const char16_t *>(path.cbegin()),
79 reinterpret_cast<const char16_t *>(path.cend()));
80}
81
82// Both std::filesystem::path and QString (without QT_NO_CAST_FROM_ASCII) can be implicitly
83// constructed from string literals so we force the std::fs::path parameter to only
84// accept std::fs::path with no implicit conversions.
85template<typename T>
86using ForceFilesystemPath = typename std::enable_if_t<std::is_same_v<std::filesystem::path, T>, int>;
87}
88#endif // QT_CONFIG(cxx17_filesystem)
89
90class QTemporaryFile;
91class QFilePrivate;
92
93class Q_CORE_EXPORT QFile : public QFileDevice
94{
95#ifndef QT_NO_QOBJECT
96 Q_OBJECT
97#endif
98 Q_DECLARE_PRIVATE(QFile)
99
100public:
101 QFile();
102 QFile(const QString &name);
103#ifdef Q_CLANG_QDOC
104 QFile(const std::filesystem::path &name);
105#elif QT_CONFIG(cxx17_filesystem)
106 template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
107 QFile(const T &name) : QFile(QtPrivate::fromFilesystemPath(name))
108 {
109 }
110#endif // QT_CONFIG(cxx17_filesystem)
111
112#ifndef QT_NO_QOBJECT
113 explicit QFile(QObject *parent);
114 QFile(const QString &name, QObject *parent);
115
116#ifdef Q_CLANG_QDOC
117 QFile(const std::filesystem::path &path, QObject *parent);
118#elif QT_CONFIG(cxx17_filesystem)
119 template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
120 QFile(const T &path, QObject *parent) : QFile(QtPrivate::fromFilesystemPath(path), parent)
121 {
122 }
123#endif // QT_CONFIG(cxx17_filesystem)
124#endif // !QT_NO_QOBJECT
125 ~QFile();
126
127 QString fileName() const override;
128#if QT_CONFIG(cxx17_filesystem) || defined(Q_CLANG_QDOC)
129 std::filesystem::path filesystemFileName() const
130 { return QtPrivate::toFilesystemPath(fileName()); }
131#endif
132 void setFileName(const QString &name);
133#ifdef Q_CLANG_QDOC
134 void setFileName(const std::filesystem::path &name);
135#elif QT_CONFIG(cxx17_filesystem)
136 template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
137 void setFileName(const T &name)
138 {
139 setFileName(QtPrivate::fromFilesystemPath(name));
140 }
141#endif // QT_CONFIG(cxx17_filesystem)
142
143#if defined(Q_OS_DARWIN)
144 // Mac always expects filenames in UTF-8... and decomposed...
145 static inline QByteArray encodeName(const QString &fileName)
146 {
147 return fileName.normalized(QString::NormalizationForm_D).toUtf8();
148 }
149 static QString decodeName(const QByteArray &localFileName)
150 {
151 // note: duplicated in qglobal.cpp (qEnvironmentVariable)
152 return QString::fromUtf8(localFileName).normalized(QString::NormalizationForm_C);
153 }
154 static inline QString decodeName(const char *localFileName)
155 {
156 return QString::fromUtf8(localFileName).normalized(QString::NormalizationForm_C);
157 }
158#else
159 static inline QByteArray encodeName(const QString &fileName)
160 {
161 return fileName.toLocal8Bit();
162 }
163 static QString decodeName(const QByteArray &localFileName)
164 {
165 return QString::fromLocal8Bit(localFileName);
166 }
167 static inline QString decodeName(const char *localFileName)
168 {
169 return QString::fromLocal8Bit(localFileName);
170 }
171#endif
172
173 bool exists() const;
174 static bool exists(const QString &fileName);
175
176 QString symLinkTarget() const;
177 static QString symLinkTarget(const QString &fileName);
178
179 bool remove();
180 static bool remove(const QString &fileName);
181
182 bool moveToTrash();
183 static bool moveToTrash(const QString &fileName, QString *pathInTrash = nullptr);
184
185 bool rename(const QString &newName);
186#ifdef Q_CLANG_QDOC
187 bool rename(const std::filesystem::path &newName);
188#elif QT_CONFIG(cxx17_filesystem)
189 template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
190 bool rename(const T &newName)
191 {
192 return rename(QtPrivate::fromFilesystemPath(newName));
193 }
194#endif // QT_CONFIG(cxx17_filesystem)
195 static bool rename(const QString &oldName, const QString &newName);
196
197 bool link(const QString &newName);
198#ifdef Q_CLANG_QDOC
199 bool link(const std::filesystem::path &newName);
200#elif QT_CONFIG(cxx17_filesystem)
201 template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
202 bool link(const T &newName)
203 {
204 return link(QtPrivate::fromFilesystemPath(newName));
205 }
206#endif // QT_CONFIG(cxx17_filesystem)
207 static bool link(const QString &oldname, const QString &newName);
208
209 bool copy(const QString &newName);
210#ifdef Q_CLANG_QDOC
211 bool copy(const std::filesystem::path &newName);
212#elif QT_CONFIG(cxx17_filesystem)
213 template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
214 bool copy(const T &newName)
215 {
216 return copy(QtPrivate::fromFilesystemPath(newName));
217 }
218#endif // QT_CONFIG(cxx17_filesystem)
219 static bool copy(const QString &fileName, const QString &newName);
220
221 bool open(OpenMode flags) override;
222 bool open(FILE *f, OpenMode ioFlags, FileHandleFlags handleFlags=DontCloseHandle);
223 bool open(int fd, OpenMode ioFlags, FileHandleFlags handleFlags=DontCloseHandle);
224
225 qint64 size() const override;
226
227 bool resize(qint64 sz) override;
228 static bool resize(const QString &filename, qint64 sz);
229
230 Permissions permissions() const override;
231 static Permissions permissions(const QString &filename);
232 bool setPermissions(Permissions permissionSpec) override;
233 static bool setPermissions(const QString &filename, Permissions permissionSpec);
234#ifdef Q_CLANG_QDOC
235 static Permissions permissions(const std::filesystem::path &filename);
236 static bool setPermissions(const std::filesystem::path &filename, Permissions permissionSpec);
237#elif QT_CONFIG(cxx17_filesystem)
238 template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
239 static Permissions permissions(const T &filename)
240 {
241 return permissions(QtPrivate::fromFilesystemPath(filename));
242 }
243 template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
244 static bool setPermissions(const T &filename, Permissions permissionSpec)
245 {
246 return setPermissions(QtPrivate::fromFilesystemPath(filename), permissionSpec);
247 }
248#endif // QT_CONFIG(cxx17_filesystem)
249
250protected:
251#ifdef QT_NO_QOBJECT
252 QFile(QFilePrivate &dd);
253#else
254 QFile(QFilePrivate &dd, QObject *parent = nullptr);
255#endif
256
257private:
258 friend class QTemporaryFile;
259 Q_DISABLE_COPY(QFile)
260};
261
262QT_END_NAMESPACE
263
264#endif // QFILE_H
265