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
30QT_BEGIN_NAMESPACE
31
32class QFileInfoPrivate : public QSharedData
33{
34public:
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 inline QFileInfoPrivate()
49 : QSharedData(), fileEngine(nullptr),
50 cachedFlags(0),
51 isDefaultConstructed(true),
52 cache_enabled(true), fileFlags(0), fileSize(0)
53 {}
54 inline QFileInfoPrivate(const QFileInfoPrivate &copy)
55 : QSharedData(copy),
56 fileEntry(copy.fileEntry),
57 metaData(copy.metaData),
58 fileEngine(QFileSystemEngine::resolveEntryAndCreateLegacyEngine(entry&: fileEntry, data&: metaData)),
59 cachedFlags(0),
60#ifndef QT_NO_FSFILEENGINE
61 isDefaultConstructed(false),
62#else
63 isDefaultConstructed(!fileEngine),
64#endif
65 cache_enabled(copy.cache_enabled), fileFlags(0), fileSize(0)
66 {}
67 inline QFileInfoPrivate(const QString &file)
68 : fileEntry(file),
69 fileEngine(QFileSystemEngine::resolveEntryAndCreateLegacyEngine(entry&: fileEntry, data&: metaData)),
70 cachedFlags(0),
71#ifndef QT_NO_FSFILEENGINE
72 isDefaultConstructed(file.isEmpty()),
73#else
74 isDefaultConstructed(!fileEngine),
75#endif
76 cache_enabled(true), fileFlags(0), fileSize(0)
77 {
78 }
79
80 inline QFileInfoPrivate(const QFileSystemEntry &file, const QFileSystemMetaData &data)
81 : QSharedData(),
82 fileEntry(file),
83 metaData(data),
84 fileEngine(QFileSystemEngine::resolveEntryAndCreateLegacyEngine(entry&: fileEntry, data&: metaData)),
85 cachedFlags(0),
86 isDefaultConstructed(false),
87 cache_enabled(true), fileFlags(0), fileSize(0)
88 {
89 //If the file engine is not null, this maybe a "mount point" for a custom file engine
90 //in which case we can't trust the metadata
91 if (fileEngine)
92 metaData = QFileSystemMetaData();
93 }
94
95 inline QFileInfoPrivate(const QFileSystemEntry &file, const QFileSystemMetaData &data, std::unique_ptr<QAbstractFileEngine> engine)
96 : fileEntry(file),
97 metaData(data),
98 fileEngine{std::move(engine)},
99 cachedFlags(0),
100#ifndef QT_NO_FSFILEENGINE
101 isDefaultConstructed(false),
102#else
103 isDefaultConstructed(!fileEngine),
104#endif
105 cache_enabled(true), fileFlags(0), fileSize(0)
106 {
107 }
108
109 inline void clearFlags() const {
110 fileFlags = 0;
111 cachedFlags = 0;
112 if (fileEngine)
113 (void)fileEngine->fileFlags(type: QAbstractFileEngine::Refresh);
114 }
115 inline void clear() {
116 metaData.clear();
117 clearFlags();
118 for (int i = QAbstractFileEngine::NFileNames - 1 ; i >= 0 ; --i)
119 fileNames[i].clear();
120 fileOwners[1].clear();
121 fileOwners[0].clear();
122 }
123
124 uint getFileFlags(QAbstractFileEngine::FileFlags) const;
125 QDateTime &getFileTime(QAbstractFileEngine::FileTime) const;
126 QString getFileName(QAbstractFileEngine::FileName) const;
127 QString getFileOwner(QAbstractFileEngine::FileOwner own) const;
128
129 QFileSystemEntry fileEntry;
130 mutable QFileSystemMetaData metaData;
131
132 std::unique_ptr<QAbstractFileEngine> const fileEngine;
133
134 mutable QString fileNames[QAbstractFileEngine::NFileNames];
135 mutable QString fileOwners[2]; // QAbstractFileEngine::FileOwner: OwnerUser and OwnerGroup
136 mutable QDateTime fileTimes[4]; // QAbstractFileEngine::FileTime: BirthTime, MetadataChangeTime, ModificationTime, AccessTime
137
138 mutable uint cachedFlags : 30;
139 bool const isDefaultConstructed : 1; // QFileInfo is a default constructed instance
140 bool cache_enabled : 1;
141 mutable uint fileFlags;
142 mutable qint64 fileSize;
143 inline bool getCachedFlag(uint c) const
144 { return cache_enabled ? (cachedFlags & c) : 0; }
145 inline void setCachedFlag(uint c) const
146 { if (cache_enabled) cachedFlags |= c; }
147
148 template <typename Ret, typename FSLambda, typename EngineLambda>
149 Ret checkAttribute(Ret defaultValue, QFileSystemMetaData::MetaDataFlags fsFlags, const FSLambda &fsLambda,
150 const EngineLambda &engineLambda) const
151 {
152 if (isDefaultConstructed)
153 return defaultValue;
154 if (fileEngine)
155 return engineLambda();
156 if (!cache_enabled || !metaData.hasFlags(flags: fsFlags)) {
157 QFileSystemEngine::fillMetaData(entry: fileEntry, data&: metaData, what: fsFlags);
158 // ignore errors, fillMetaData will have cleared the flags
159 }
160 return fsLambda();
161 }
162
163 template <typename Ret, typename FSLambda, typename EngineLambda>
164 Ret checkAttribute(QFileSystemMetaData::MetaDataFlags fsFlags, const FSLambda &fsLambda,
165 const EngineLambda &engineLambda) const
166 {
167 return checkAttribute(Ret(), fsFlags, fsLambda, engineLambda);
168 }
169};
170
171QT_END_NAMESPACE
172
173#endif // QFILEINFO_P_H
174

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