1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#ifndef QDIR_H
6#define QDIR_H
7
8#include <QtCore/qcompare.h>
9#include <QtCore/qstring.h>
10#include <QtCore/qdirlisting.h>
11#include <QtCore/qfile.h>
12#include <QtCore/qfileinfo.h>
13#include <QtCore/qstringlist.h>
14#include <QtCore/qshareddata.h>
15
16QT_BEGIN_NAMESPACE
17
18class QDirIterator;
19class QDirPrivate;
20
21class Q_CORE_EXPORT QDir
22{
23public:
24 enum Filter { Dirs = 0x001,
25 Files = 0x002,
26 Drives = 0x004,
27 NoSymLinks = 0x008,
28 AllEntries = Dirs | Files | Drives,
29 TypeMask = 0x00f,
30
31 Readable = 0x010,
32 Writable = 0x020,
33 Executable = 0x040,
34 PermissionMask = 0x070,
35
36 Modified = 0x080,
37 Hidden = 0x100,
38 System = 0x200,
39
40 AccessMask = 0x3F0,
41
42 AllDirs = 0x400,
43 CaseSensitive = 0x800,
44 NoDot = 0x2000,
45 NoDotDot = 0x4000,
46 NoDotAndDotDot = NoDot | NoDotDot,
47
48 NoFilter = -1
49 };
50 Q_DECLARE_FLAGS(Filters, Filter)
51
52 enum SortFlag { Name = 0x00,
53 Time = 0x01,
54 Size = 0x02,
55 Unsorted = 0x03,
56 SortByMask = 0x03,
57
58 DirsFirst = 0x04,
59 Reversed = 0x08,
60 IgnoreCase = 0x10,
61 DirsLast = 0x20,
62 LocaleAware = 0x40,
63 Type = 0x80,
64 NoSort = -1
65 };
66 Q_DECLARE_FLAGS(SortFlags, SortFlag)
67
68 QDir(const QDir &);
69 QDir(const QString &path = QString());
70 QDir(const QString &path, const QString &nameFilter,
71 SortFlags sort = SortFlags(Name | IgnoreCase), Filters filter = AllEntries);
72#ifdef Q_QDOC
73 QDir(const std::filesystem::path &path);
74 QDir(const std::filesystem::path &path, const QString &nameFilter,
75 SortFlags sort = SortFlags(Name | IgnoreCase), Filters filter = AllEntries);
76#elif QT_CONFIG(cxx17_filesystem)
77 template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
78 QDir(const T &path) : QDir(QtPrivate::fromFilesystemPath(path))
79 {
80 }
81 template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
82 QDir(const T &path, const QString &nameFilter,
83 SortFlags sort = SortFlags(Name | IgnoreCase), Filters filter = AllEntries)
84 : QDir(QtPrivate::fromFilesystemPath(path), nameFilter, sort, filter)
85 {
86 }
87#endif // QT_CONFIG(cxx17_filesystem)
88 ~QDir();
89
90 QDir &operator=(const QDir &);
91 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QDir)
92
93 void swap(QDir &other) noexcept
94 { d_ptr.swap(other&: other.d_ptr); }
95
96 void setPath(const QString &path);
97#ifdef Q_QDOC
98 void setPath(const std::filesystem::path &path);
99#elif QT_CONFIG(cxx17_filesystem)
100 template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
101 void setPath(const T &path)
102 {
103 setPath(QtPrivate::fromFilesystemPath(path));
104 }
105#endif // QT_CONFIG(cxx17_filesystem)
106 QString path() const;
107 QString absolutePath() const;
108 QString canonicalPath() const;
109#if QT_CONFIG(cxx17_filesystem) || defined(Q_QDOC)
110 std::filesystem::path filesystemPath() const
111 { return QtPrivate::toFilesystemPath(path: path()); }
112 std::filesystem::path filesystemAbsolutePath() const
113 { return QtPrivate::toFilesystemPath(path: absolutePath()); }
114 std::filesystem::path filesystemCanonicalPath() const
115 { return QtPrivate::toFilesystemPath(path: canonicalPath()); }
116#endif // QT_CONFIG(cxx17_filesystem)
117
118#ifndef QT_BOOTSTRAPPED
119 static void setSearchPaths(const QString &prefix, const QStringList &searchPaths);
120 static void addSearchPath(const QString &prefix, const QString &path);
121#ifdef Q_QDOC
122 static void addSearchPath(const QString &prefix, const std::filesystem::path &path);
123#elif QT_CONFIG(cxx17_filesystem)
124 template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
125 static void addSearchPath(const QString &prefix, const T &path)
126 {
127 addSearchPath(prefix, QtPrivate::fromFilesystemPath(path));
128 }
129#endif // QT_CONFIG(cxx17_filesystem)
130 static QStringList searchPaths(const QString &prefix);
131#endif // QT_BOOTSTRAPPED
132
133 QString dirName() const;
134 QString filePath(const QString &fileName) const;
135 QString absoluteFilePath(const QString &fileName) const;
136 QString relativeFilePath(const QString &fileName) const;
137
138 static QString toNativeSeparators(const QString &pathName);
139 static QString fromNativeSeparators(const QString &pathName);
140
141 bool cd(const QString &dirName);
142 bool cdUp();
143
144 QStringList nameFilters() const;
145 void setNameFilters(const QStringList &nameFilters);
146
147 Filters filter() const;
148 void setFilter(Filters filter);
149 SortFlags sorting() const;
150 void setSorting(SortFlags sort);
151
152#if QT_CORE_REMOVED_SINCE(6, 5)
153 uint count() const;
154#endif
155 qsizetype count(QT6_DECL_NEW_OVERLOAD) const;
156 bool isEmpty(Filters filters = Filters(AllEntries | NoDotAndDotDot)) const;
157
158#if QT_CORE_REMOVED_SINCE(6, 5) && QT_POINTER_SIZE != 4
159 QString operator[](int) const;
160#endif
161 QString operator[](qsizetype) const;
162
163 static QStringList nameFiltersFromString(const QString &nameFilter);
164
165 QStringList entryList(Filters filters = NoFilter, SortFlags sort = NoSort) const;
166 QStringList entryList(const QStringList &nameFilters, Filters filters = NoFilter,
167 SortFlags sort = NoSort) const;
168
169 QFileInfoList entryInfoList(Filters filters = NoFilter, SortFlags sort = NoSort) const;
170 QFileInfoList entryInfoList(const QStringList &nameFilters, Filters filters = NoFilter,
171 SortFlags sort = NoSort) const;
172
173#if QT_CORE_REMOVED_SINCE(6, 10)
174 bool mkdir(const QString &dirName) const;
175 bool mkdir(const QString &dirName, QFile::Permissions permissions) const;
176#endif
177 bool mkdir(const QString &dirName, std::optional<QFile::Permissions> p = std::nullopt) const;
178 bool rmdir(const QString &dirName) const;
179#if QT_CORE_REMOVED_SINCE(6, 10)
180 bool mkpath(const QString &dirPath) const;
181#endif
182 bool mkpath(const QString &dirPath, std::optional<QFile::Permissions> p = std::nullopt) const;
183 bool rmpath(const QString &dirPath) const;
184
185 bool removeRecursively();
186
187 bool isReadable() const;
188 bool exists() const;
189 bool isRoot() const;
190
191 static bool isRelativePath(const QString &path);
192 inline static bool isAbsolutePath(const QString &path) { return !isRelativePath(path); }
193 bool isRelative() const;
194 inline bool isAbsolute() const { return !isRelative(); }
195 bool makeAbsolute();
196
197#if QT_CORE_REMOVED_SINCE(6, 8)
198 bool operator==(const QDir &dir) const;
199 inline bool operator!=(const QDir &dir) const { return !operator==(dir); }
200#endif
201
202 bool remove(const QString &fileName);
203 bool rename(const QString &oldName, const QString &newName);
204 bool exists(const QString &name) const;
205
206 static QFileInfoList drives();
207
208 constexpr static inline QChar listSeparator() noexcept
209 {
210#if defined(Q_OS_WIN)
211 return u';';
212#else
213 return u':';
214#endif
215 }
216
217 static QChar separator()
218 {
219#if defined(Q_OS_WIN)
220 return u'\\';
221#else
222 return u'/';
223#endif
224 }
225
226 static bool setCurrent(const QString &path);
227 static inline QDir current() { return QDir(currentPath()); }
228 static QString currentPath();
229
230 static inline QDir home() { return QDir(homePath()); }
231 static QString homePath();
232 static inline QDir root() { return QDir(rootPath()); }
233 static QString rootPath();
234 static inline QDir temp() { return QDir(tempPath()); }
235 static QString tempPath();
236
237#if QT_CONFIG(regularexpression)
238 static bool match(const QStringList &filters, const QString &fileName);
239 static bool match(const QString &filter, const QString &fileName);
240#endif
241
242 static QString cleanPath(const QString &path);
243 void refresh() const;
244
245protected:
246 explicit QDir(QDirPrivate &d);
247
248 QSharedDataPointer<QDirPrivate> d_ptr;
249
250private:
251 friend Q_CORE_EXPORT bool comparesEqual(const QDir &lhs, const QDir &rhs);
252 Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(QDir)
253 friend class QDirIterator;
254 friend class QDirListing;
255 friend class QDirListingPrivate;
256 // Q_DECLARE_PRIVATE equivalent for shared data pointers
257 QDirPrivate *d_func();
258 const QDirPrivate *d_func() const { return d_ptr.constData(); }
259};
260
261Q_DECLARE_SHARED(QDir)
262Q_DECLARE_OPERATORS_FOR_FLAGS(QDir::Filters)
263Q_DECLARE_OPERATORS_FOR_FLAGS(QDir::SortFlags)
264
265#ifndef QT_NO_DEBUG_STREAM
266class QDebug;
267Q_CORE_EXPORT QDebug operator<<(QDebug debug, QDir::Filters filters);
268Q_CORE_EXPORT QDebug operator<<(QDebug debug, const QDir &dir);
269#endif
270
271QT_END_NAMESPACE
272
273#endif // QDIR_H
274

source code of qtbase/src/corelib/io/qdir.h