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

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