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

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