| 1 | // Copyright (C) 2024 Ahmad Samir <a.samirh78@gmail.com> |
|---|---|
| 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 QDIRENTRYINFO_P_H |
| 6 | #define QDIRENTRYINFO_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 <QtCore/private/qfileinfo_p.h> |
| 20 | #include <QtCore/private/qfilesystementry_p.h> |
| 21 | #include <QtCore/private/qfilesystemmetadata_p.h> |
| 22 | |
| 23 | QT_BEGIN_NAMESPACE |
| 24 | |
| 25 | class QDirEntryInfo |
| 26 | { |
| 27 | const QFileSystemMetaData &ensureFilled(QFileSystemMetaData::MetaDataFlags what) |
| 28 | { |
| 29 | if (!metaData.hasFlags(flags: what)) |
| 30 | QFileSystemEngine::fillMetaData(entry, data&: metaData, what); |
| 31 | return metaData; |
| 32 | } |
| 33 | |
| 34 | public: |
| 35 | const QFileInfo &fileInfo() |
| 36 | { |
| 37 | if (!fileInfoOpt) { |
| 38 | fileInfoOpt.emplace(args: new QFileInfoPrivate(entry, metaData)); |
| 39 | metaData.clear(); |
| 40 | } |
| 41 | return *fileInfoOpt; |
| 42 | } |
| 43 | |
| 44 | QString fileName() |
| 45 | { return fileInfoOpt ? fileInfoOpt->fileName() : entry.fileName(); } |
| 46 | QString baseName() |
| 47 | { return fileInfoOpt ? fileInfoOpt->baseName() : entry.baseName(); } |
| 48 | QString completeBaseName() const |
| 49 | { return fileInfoOpt ? fileInfoOpt->completeBaseName() : entry.completeBaseName(); } |
| 50 | QString suffix() const |
| 51 | { return fileInfoOpt ? fileInfoOpt->suffix() : entry.suffix(); } |
| 52 | QString completeSuffix() const |
| 53 | { return fileInfoOpt ? fileInfoOpt->completeSuffix() : entry.completeSuffix(); } |
| 54 | QString filePath() |
| 55 | { return fileInfoOpt ? fileInfoOpt->filePath() : entry.filePath(); } |
| 56 | |
| 57 | QString bundleName() { return fileInfo().bundleName(); } |
| 58 | |
| 59 | QString canonicalFilePath() |
| 60 | { |
| 61 | // QFileInfo caches these strings |
| 62 | return fileInfo().canonicalFilePath(); |
| 63 | } |
| 64 | |
| 65 | QString absoluteFilePath() { |
| 66 | // QFileInfo caches these strings |
| 67 | return fileInfo().absoluteFilePath(); |
| 68 | } |
| 69 | |
| 70 | QString absolutePath() { |
| 71 | // QFileInfo caches these strings |
| 72 | return fileInfo().absolutePath(); |
| 73 | } |
| 74 | |
| 75 | |
| 76 | bool isDir() { |
| 77 | if (fileInfoOpt) |
| 78 | return fileInfoOpt->isDir(); |
| 79 | |
| 80 | return ensureFilled(what: QFileSystemMetaData::DirectoryType).isDirectory(); |
| 81 | } |
| 82 | |
| 83 | bool isFile() { |
| 84 | if (fileInfoOpt) |
| 85 | return fileInfoOpt->isFile(); |
| 86 | |
| 87 | return ensureFilled(what: QFileSystemMetaData::FileType).isFile(); |
| 88 | } |
| 89 | |
| 90 | bool isSymLink() { |
| 91 | if (fileInfoOpt) |
| 92 | return fileInfoOpt->isSymLink(); |
| 93 | |
| 94 | return ensureFilled(what: QFileSystemMetaData::LegacyLinkType).isLegacyLink(); |
| 95 | } |
| 96 | |
| 97 | bool isSymbolicLink() { |
| 98 | if (fileInfoOpt) |
| 99 | return fileInfoOpt->isSymbolicLink(); |
| 100 | |
| 101 | return ensureFilled(what: QFileSystemMetaData::LinkType).isLink(); |
| 102 | } |
| 103 | |
| 104 | bool exists() { |
| 105 | if (fileInfoOpt) |
| 106 | return fileInfoOpt->exists(); |
| 107 | |
| 108 | return ensureFilled(what: QFileSystemMetaData::ExistsAttribute).exists(); |
| 109 | } |
| 110 | |
| 111 | bool isHidden() { |
| 112 | if (fileInfoOpt) |
| 113 | return fileInfoOpt->isHidden(); |
| 114 | |
| 115 | return ensureFilled(what: QFileSystemMetaData::HiddenAttribute).isHidden(); |
| 116 | } |
| 117 | |
| 118 | bool isReadable() { |
| 119 | if (fileInfoOpt) |
| 120 | return fileInfoOpt->isReadable(); |
| 121 | |
| 122 | return ensureFilled(what: QFileSystemMetaData::UserReadPermission).isReadable(); |
| 123 | } |
| 124 | |
| 125 | bool isWritable() { |
| 126 | if (fileInfoOpt) |
| 127 | return fileInfoOpt->isWritable(); |
| 128 | |
| 129 | return ensureFilled(what: QFileSystemMetaData::UserWritePermission).isWritable(); |
| 130 | } |
| 131 | |
| 132 | bool isExecutable() { |
| 133 | if (fileInfoOpt) |
| 134 | return fileInfoOpt->isExecutable(); |
| 135 | |
| 136 | return ensureFilled(what: QFileSystemMetaData::UserExecutePermission).isExecutable(); |
| 137 | } |
| 138 | |
| 139 | qint64 size() { return fileInfo().size(); } |
| 140 | |
| 141 | QDateTime fileTime(QFile::FileTime type, const QTimeZone &tz) |
| 142 | { |
| 143 | return fileInfo().fileTime(time: type, tz); |
| 144 | } |
| 145 | |
| 146 | private: |
| 147 | friend class QDirListingPrivate; |
| 148 | friend class QDirListing; |
| 149 | |
| 150 | QFileSystemEntry entry; |
| 151 | QFileSystemMetaData metaData; |
| 152 | std::optional<QFileInfo> fileInfoOpt = std::nullopt; |
| 153 | }; |
| 154 | |
| 155 | QT_END_NAMESPACE |
| 156 | |
| 157 | #endif // QDIRENTRYINFO_P_H |
| 158 |
