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 QFILEINFO_P_H |
5 | #define QFILEINFO_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 "qfileinfo.h" |
19 | #include "qdatetime.h" |
20 | #include "qatomic.h" |
21 | #include "qshareddata.h" |
22 | #include "qfilesystemengine_p.h" |
23 | |
24 | #include <QtCore/private/qabstractfileengine_p.h> |
25 | #include <QtCore/private/qfilesystementry_p.h> |
26 | #include <QtCore/private/qfilesystemmetadata_p.h> |
27 | |
28 | #include <memory> |
29 | |
30 | QT_BEGIN_NAMESPACE |
31 | |
32 | class QFileInfoPrivate : public QSharedData |
33 | { |
34 | public: |
35 | enum { |
36 | // note: cachedFlags is only 30-bits wide |
37 | CachedFileFlags = 0x01, |
38 | CachedLinkTypeFlag = 0x02, |
39 | CachedBundleTypeFlag = 0x04, |
40 | CachedSize = 0x08, |
41 | CachedATime = 0x10, |
42 | CachedBTime = 0x20, |
43 | CachedMCTime = 0x40, |
44 | CachedMTime = 0x80, |
45 | CachedPerms = 0x100 |
46 | }; |
47 | |
48 | static QFileInfoPrivate *get(QFileInfo *fi) { return fi->d_func(); } |
49 | |
50 | inline QFileInfoPrivate() |
51 | : QSharedData(), fileEngine(nullptr), |
52 | cachedFlags(0), |
53 | isDefaultConstructed(true), |
54 | cache_enabled(true), fileFlags(0), fileSize(0) |
55 | {} |
56 | inline QFileInfoPrivate(const QFileInfoPrivate ©) |
57 | : QSharedData(copy), |
58 | fileEntry(copy.fileEntry), |
59 | metaData(copy.metaData), |
60 | fileEngine(QFileSystemEngine::createLegacyEngine(entry&: fileEntry, data&: metaData)), |
61 | cachedFlags(0), |
62 | #ifndef QT_NO_FSFILEENGINE |
63 | isDefaultConstructed(false), |
64 | #else |
65 | isDefaultConstructed(!fileEngine), |
66 | #endif |
67 | cache_enabled(copy.cache_enabled), fileFlags(0), fileSize(0) |
68 | {} |
69 | inline QFileInfoPrivate(const QString &file) |
70 | : fileEntry(file), |
71 | fileEngine(QFileSystemEngine::createLegacyEngine(entry&: fileEntry, data&: metaData)), |
72 | cachedFlags(0), |
73 | #ifndef QT_NO_FSFILEENGINE |
74 | isDefaultConstructed(file.isEmpty()), |
75 | #else |
76 | isDefaultConstructed(!fileEngine), |
77 | #endif |
78 | cache_enabled(true), fileFlags(0), fileSize(0) |
79 | { |
80 | } |
81 | |
82 | inline QFileInfoPrivate(const QFileSystemEntry &file, const QFileSystemMetaData &data) |
83 | : QSharedData(), |
84 | fileEntry(file), |
85 | metaData(data), |
86 | fileEngine(QFileSystemEngine::createLegacyEngine(entry&: fileEntry, data&: metaData)), |
87 | cachedFlags(0), |
88 | isDefaultConstructed(false), |
89 | cache_enabled(true), fileFlags(0), fileSize(0) |
90 | { |
91 | //If the file engine is not null, this maybe a "mount point" for a custom file engine |
92 | //in which case we can't trust the metadata |
93 | if (fileEngine) |
94 | metaData = QFileSystemMetaData(); |
95 | } |
96 | |
97 | inline QFileInfoPrivate(const QFileSystemEntry &file, const QFileSystemMetaData &data, std::unique_ptr<QAbstractFileEngine> engine) |
98 | : fileEntry(file), |
99 | metaData(data), |
100 | fileEngine{std::move(engine)}, |
101 | cachedFlags(0), |
102 | #ifndef QT_NO_FSFILEENGINE |
103 | isDefaultConstructed(false), |
104 | #else |
105 | isDefaultConstructed(!fileEngine), |
106 | #endif |
107 | cache_enabled(true), fileFlags(0), fileSize(0) |
108 | { |
109 | } |
110 | |
111 | inline void clearFlags() const { |
112 | fileFlags = 0; |
113 | cachedFlags = 0; |
114 | if (fileEngine) |
115 | (void)fileEngine->fileFlags(type: QAbstractFileEngine::Refresh); |
116 | } |
117 | inline void clear() { |
118 | metaData.clear(); |
119 | clearFlags(); |
120 | for (int i = QAbstractFileEngine::NFileNames - 1 ; i >= 0 ; --i) |
121 | fileNames[i].clear(); |
122 | fileOwners[1].clear(); |
123 | fileOwners[0].clear(); |
124 | } |
125 | |
126 | uint getFileFlags(QAbstractFileEngine::FileFlags) const; |
127 | QDateTime &getFileTime(QFile::FileTime) const; |
128 | QString getFileName(QAbstractFileEngine::FileName) const; |
129 | QString getFileOwner(QAbstractFileEngine::FileOwner own) const; |
130 | |
131 | QFileSystemEntry fileEntry; |
132 | mutable QFileSystemMetaData metaData; |
133 | |
134 | std::unique_ptr<QAbstractFileEngine> const fileEngine; |
135 | |
136 | mutable QString fileNames[QAbstractFileEngine::NFileNames]; |
137 | mutable QString fileOwners[2]; // QAbstractFileEngine::FileOwner: OwnerUser and OwnerGroup |
138 | mutable QDateTime fileTimes[4]; // QFile::FileTime: FileBirthTime, FileMetadataChangeTime, FileModificationTime, FileAccessTime |
139 | |
140 | mutable uint cachedFlags : 30; |
141 | bool const isDefaultConstructed : 1; // QFileInfo is a default constructed instance |
142 | bool cache_enabled : 1; |
143 | mutable uint fileFlags; |
144 | mutable qint64 fileSize; |
145 | inline bool getCachedFlag(uint c) const |
146 | { return cache_enabled ? (cachedFlags & c) : 0; } |
147 | inline void setCachedFlag(uint c) const |
148 | { if (cache_enabled) cachedFlags |= c; } |
149 | |
150 | template <typename Ret, typename FSLambda, typename EngineLambda> |
151 | Ret checkAttribute(Ret defaultValue, QFileSystemMetaData::MetaDataFlags fsFlags, |
152 | FSLambda fsLambda, EngineLambda engineLambda) const |
153 | { |
154 | if (isDefaultConstructed) |
155 | return defaultValue; |
156 | if (fileEngine) |
157 | return engineLambda(); |
158 | if (!cache_enabled || !metaData.hasFlags(flags: fsFlags)) { |
159 | QFileSystemEngine::fillMetaData(entry: fileEntry, data&: metaData, what: fsFlags); |
160 | // ignore errors, fillMetaData will have cleared the flags |
161 | } |
162 | return fsLambda(); |
163 | } |
164 | |
165 | template <typename Ret, typename FSLambda, typename EngineLambda> |
166 | Ret checkAttribute(QFileSystemMetaData::MetaDataFlags fsFlags, FSLambda fsLambda, |
167 | EngineLambda engineLambda) const |
168 | { |
169 | return checkAttribute(Ret(), std::move(fsFlags), std::move(fsLambda), engineLambda); |
170 | } |
171 | }; |
172 | |
173 | QT_END_NAMESPACE |
174 | |
175 | #endif // QFILEINFO_P_H |
176 | |