1 | // Copyright (C) 2016 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 | |
4 | #ifndef QFILESYSTEMENGINE_P_H |
5 | #define QFILESYSTEMENGINE_P_H |
6 | |
7 | // |
8 | // W A R N I N G |
9 | // ------------- |
10 | // |
11 | // This file is not part of the Qt API. It exists purely as an |
12 | // implementation detail. This header file may change from version to |
13 | // version without notice, or even be removed. |
14 | // |
15 | // We mean it. |
16 | // |
17 | |
18 | #include "qfile.h" |
19 | #include "qfilesystementry_p.h" |
20 | #include "qfilesystemmetadata_p.h" |
21 | #include <QtCore/private/qsystemerror_p.h> |
22 | |
23 | #include <memory> |
24 | #include <optional> |
25 | |
26 | QT_BEGIN_NAMESPACE |
27 | |
28 | #define Q_RETURN_ON_INVALID_FILENAME(message, result) \ |
29 | { \ |
30 | QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).warning(message); \ |
31 | errno = EINVAL; \ |
32 | return (result); \ |
33 | } |
34 | |
35 | inline bool qIsFilenameBroken(const QByteArray &name) |
36 | { |
37 | return name.contains(c: '\0'); |
38 | } |
39 | |
40 | inline bool qIsFilenameBroken(const QString &name) |
41 | { |
42 | return name.contains(c: QLatin1Char('\0')); |
43 | } |
44 | |
45 | inline bool qIsFilenameBroken(const QFileSystemEntry &entry) |
46 | { |
47 | return qIsFilenameBroken(name: entry.nativeFilePath()); |
48 | } |
49 | |
50 | #define Q_CHECK_FILE_NAME(name, result) \ |
51 | do { \ |
52 | if (Q_UNLIKELY((name).isEmpty())) \ |
53 | Q_RETURN_ON_INVALID_FILENAME("Empty filename passed to function", (result)); \ |
54 | if (Q_UNLIKELY(qIsFilenameBroken(name))) \ |
55 | Q_RETURN_ON_INVALID_FILENAME("Broken filename passed to function", (result)); \ |
56 | } while (false) |
57 | |
58 | Q_CORE_EXPORT bool qt_isCaseSensitive(const QFileSystemEntry &entry, QFileSystemMetaData &data); |
59 | |
60 | class Q_AUTOTEST_EXPORT QFileSystemEngine |
61 | { |
62 | public: |
63 | static bool isCaseSensitive(const QFileSystemEntry &entry, QFileSystemMetaData &data); |
64 | |
65 | static QFileSystemEntry getLinkTarget(const QFileSystemEntry &link, QFileSystemMetaData &data); |
66 | static QFileSystemEntry getRawLinkPath(const QFileSystemEntry &link, |
67 | QFileSystemMetaData &data); |
68 | static QFileSystemEntry getJunctionTarget(const QFileSystemEntry &link, QFileSystemMetaData &data); |
69 | static QFileSystemEntry canonicalName(const QFileSystemEntry &entry, QFileSystemMetaData &data); |
70 | static QFileSystemEntry absoluteName(const QFileSystemEntry &entry); |
71 | static QByteArray id(const QFileSystemEntry &entry); |
72 | static QString resolveUserName(const QFileSystemEntry &entry, QFileSystemMetaData &data); |
73 | static QString resolveGroupName(const QFileSystemEntry &entry, QFileSystemMetaData &data); |
74 | |
75 | #if defined(Q_OS_UNIX) |
76 | static QString resolveUserName(uint userId); |
77 | static QString resolveGroupName(uint groupId); |
78 | #endif |
79 | |
80 | #if defined(Q_OS_DARWIN) |
81 | static QString bundleName(const QFileSystemEntry &entry); |
82 | #else |
83 | static QString bundleName(const QFileSystemEntry &) { return QString(); } |
84 | #endif |
85 | |
86 | static bool fillMetaData(const QFileSystemEntry &entry, QFileSystemMetaData &data, |
87 | QFileSystemMetaData::MetaDataFlags what); |
88 | #if defined(Q_OS_UNIX) |
89 | static bool cloneFile(int srcfd, int dstfd, const QFileSystemMetaData &knownData); |
90 | static bool fillMetaData(int fd, QFileSystemMetaData &data); // what = PosixStatFlags |
91 | static QByteArray id(int fd); |
92 | static bool setFileTime(int fd, const QDateTime &newDate, |
93 | QFile::FileTime whatTime, QSystemError &error); |
94 | static bool setPermissions(int fd, QFile::Permissions permissions, QSystemError &error, |
95 | QFileSystemMetaData *data = nullptr); |
96 | #endif |
97 | #if defined(Q_OS_WIN) |
98 | static QFileSystemEntry junctionTarget(const QFileSystemEntry &link, QFileSystemMetaData &data); |
99 | static bool uncListSharesOnServer(const QString &server, QStringList *list); //Used also by QFSFileEngineIterator::hasNext() |
100 | static bool fillMetaData(int fd, QFileSystemMetaData &data, |
101 | QFileSystemMetaData::MetaDataFlags what); |
102 | static bool fillMetaData(HANDLE fHandle, QFileSystemMetaData &data, |
103 | QFileSystemMetaData::MetaDataFlags what); |
104 | static bool fillPermissions(const QFileSystemEntry &entry, QFileSystemMetaData &data, |
105 | QFileSystemMetaData::MetaDataFlags what); |
106 | static QByteArray id(HANDLE fHandle); |
107 | static bool setFileTime(HANDLE fHandle, const QDateTime &newDate, |
108 | QFile::FileTime whatTime, QSystemError &error); |
109 | static QString owner(const QFileSystemEntry &entry, QAbstractFileEngine::FileOwner own); |
110 | static QString nativeAbsoluteFilePath(const QString &path); |
111 | static bool isDirPath(const QString &path, bool *existed); |
112 | #endif |
113 | //homePath, rootPath and tempPath shall return clean paths |
114 | static QString homePath(); |
115 | static QString rootPath(); |
116 | static QString tempPath(); |
117 | |
118 | static bool createDirectory(const QFileSystemEntry &entry, bool createParents, |
119 | std::optional<QFile::Permissions> permissions = std::nullopt); |
120 | static bool removeDirectory(const QFileSystemEntry &entry, bool removeEmptyParents); |
121 | |
122 | static bool createLink(const QFileSystemEntry &source, const QFileSystemEntry &target, QSystemError &error); |
123 | |
124 | static bool copyFile(const QFileSystemEntry &source, const QFileSystemEntry &target, QSystemError &error); |
125 | static bool moveFileToTrash(const QFileSystemEntry &source, QFileSystemEntry &newLocation, QSystemError &error); |
126 | static bool renameFile(const QFileSystemEntry &source, const QFileSystemEntry &target, QSystemError &error); |
127 | static bool renameOverwriteFile(const QFileSystemEntry &source, const QFileSystemEntry &target, QSystemError &error); |
128 | static bool removeFile(const QFileSystemEntry &entry, QSystemError &error); |
129 | |
130 | static bool setPermissions(const QFileSystemEntry &entry, QFile::Permissions permissions, QSystemError &error, |
131 | QFileSystemMetaData *data = nullptr); |
132 | |
133 | // unused, therefore not implemented |
134 | static bool setFileTime(const QFileSystemEntry &entry, const QDateTime &newDate, |
135 | QFile::FileTime whatTime, QSystemError &error); |
136 | |
137 | static bool setCurrentPath(const QFileSystemEntry &entry); |
138 | static QFileSystemEntry currentPath(); |
139 | |
140 | static std::unique_ptr<QAbstractFileEngine> |
141 | createLegacyEngine(QFileSystemEntry &entry, QFileSystemMetaData &data); |
142 | |
143 | private: |
144 | static QString slowCanonicalized(const QString &path); |
145 | #if defined(Q_OS_WIN) |
146 | static void clearWinStatData(QFileSystemMetaData &data); |
147 | #endif |
148 | }; |
149 | |
150 | QT_END_NAMESPACE |
151 | |
152 | #endif // include guard |
153 | |