Debian Codegraph @

src/filesystem/dcapfsfileengine.cpp

raw
1 // SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
2 //
3 // SPDX-License-Identifier: LGPL-3.0-or-later
4
5 #include "private/dcapfsfileengine_p.h"
6 #include "private/dobject_p.h"
7 #include "dvtablehook.h"
8
9 #include "dcapmanager.h"
10 #include <QDebug>
11
12 DCORE_BEGIN_NAMESPACE
13
14 extern QString _d_cleanPath(const QString &path);
15 extern bool _d_isSubFileOf(const QString &filePath, const QString &directoryPath);
16
17 #if QT_VERSION < QT_VERSION_CHECK(6, 8, 0)
18 static bool capDirIteraterHasNext(QAbstractFileEngineIterator *it)
19 {
20 const QStringList &paths = DCapManager::instance()->paths();
21 QString path = it->path();
22 QFileInfo info(path);
23 if (info.isSymLink())
24 info = QFileInfo{info.symLinkTarget()};
25
26 bool ret = std::any_of(paths.cbegin(), paths.cend(), std::bind(_d_isSubFileOf, path, std::placeholders::_1));
27
28 if (!ret)
29 return ret;
30 return DVtableHook::callOriginalFun(it, &QAbstractFileEngineIterator::hasNext);
31 }
32 #endif
33
34 #if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
35 std::unique_ptr<QAbstractFileEngine> DCapFSFileEngineHandler::create(const QString &fileName) const
36 #else
37 QAbstractFileEngine *DCapFSFileEngineHandler::create(const QString &fileName) const
38 #endif
39 {
40 #if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
41 return std::unique_ptr<QAbstractFileEngine>(new DCapFSFileEngine(fileName));
42 #else
43 return new DCapFSFileEngine(fileName);
44 #endif
45 }
46
47
48 class DCapFSFileEnginePrivate : public DObjectPrivate
49 {
50 D_DECLARE_PUBLIC(DCapFSFileEngine)
51 public:
52 DCapFSFileEnginePrivate(const QString &file, DCapFSFileEngine *qq);
53
54 bool canReadWrite(const QString &path) const;
55
56 QString file;
57 };
58
59 DCapFSFileEnginePrivate::DCapFSFileEnginePrivate(const QString &file, DCapFSFileEngine *qq)
60 : DObjectPrivate(qq)
61 , file(file)
62 {
63
64 }
65
66 bool DCapFSFileEnginePrivate::canReadWrite(const QString &path) const
67 {
68 if (path.isEmpty())
69 return false;
70
71 QString target = path;
72 if (path == this->file) {
73 D_QC(DCapFSFileEngine);
74 target = q->fileName(DCapFSFileEngine::AbsoluteName);
75 } else {
76 QFSFileEngine engine(path);
77 target = engine.fileName(DCapFSFileEngine::AbsoluteName);
78 }
79
80 auto paths = DCapManager::instance()->paths();
81 return std::any_of(paths.cbegin(), paths.cend(),
82 std::bind(_d_isSubFileOf, target, std::placeholders::_1));
83 }
84
85 DCapFSFileEngine::DCapFSFileEngine(const QString &file)
86 : QFSFileEngine(file)
87 , DObject(*new DCapFSFileEnginePrivate(file, this))
88 {
89
90 }
91
92 DCapFSFileEngine::~DCapFSFileEngine()
93 {
94 }
95
96 #if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0)
97 bool DCapFSFileEngine::open(QIODevice::OpenMode openMode, std::optional<QFile::Permissions> permissions)
98 #else
99 bool DCapFSFileEngine::open(QIODevice::OpenMode openMode)
100 #endif
101 {
102 D_D(DCapFSFileEngine);
103 if (!d->canReadWrite(d->file))
104 return false;
105 #if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0)
106 return QFSFileEngine::open(openMode, permissions);
107 #else
108 return QFSFileEngine::open(openMode);
109 #endif
110 }
111
112 bool DCapFSFileEngine::remove()
113 {
114 D_D(DCapFSFileEngine);
115 if (!d->canReadWrite(d->file))
116 return false;
117 return QFSFileEngine::remove();
118 }
119
120 bool DCapFSFileEngine::copy(const QString &newName)
121 {
122 D_D(DCapFSFileEngine);
123 if (!d->canReadWrite(newName)) {
124 // ###(Chen Bin): If false is returned here, QFile
125 // will use the interface of qtemporaryfile for
126 // file operation, and the restrictions in
127 // DCapFSFileEngine cannot be used. And it will be
128 // copied successfully.
129 qWarning() << "DCapFSFileEngine: " << QStringLiteral("The file [%1] has no permission to copy!").arg(newName);
130 return true;
131 }
132 return QFSFileEngine::copy(newName);
133 }
134
135 bool DCapFSFileEngine::rename(const QString &newName)
136 {
137 D_D(DCapFSFileEngine);
138 if (!d->canReadWrite(newName))
139 return false;
140 return QFSFileEngine::rename(newName);
141 }
142
143 bool DCapFSFileEngine::renameOverwrite(const QString &newName)
144 {
145 D_D(DCapFSFileEngine);
146 if (!d->canReadWrite(newName))
147 return false;
148 return QFSFileEngine::renameOverwrite(newName);
149 }
150
151 bool DCapFSFileEngine::link(const QString &newName)
152 {
153 D_D(DCapFSFileEngine);
154 if (!d->canReadWrite(newName))
155 return false;
156 return QFSFileEngine::link(newName);
157 }
158
159 #if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0)
160 bool DCapFSFileEngine::mkdir(const QString &dirName,
161 bool createParentDirectories,
162 std::optional<QFile::Permissions> permissions) const
163 #else
164 bool DCapFSFileEngine::mkdir(const QString &dirName, bool createParentDirectories) const
165 #endif
166 {
167 D_DC(DCapFSFileEngine);
168 if (!d->canReadWrite(dirName))
169 return false;
170 #if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0)
171 return QFSFileEngine::mkdir(dirName, createParentDirectories, permissions);
172 #else
173 return QFSFileEngine::mkdir(dirName, createParentDirectories);
174 #endif
175 }
176
177 bool DCapFSFileEngine::rmdir(const QString &dirName, bool recurseParentDirectories) const
178 {
179 D_DC(DCapFSFileEngine);
180 if (!d->canReadWrite(dirName))
181 return false;
182 return QFSFileEngine::rmdir(dirName, recurseParentDirectories);
183 }
184
185 QAbstractFileEngine::FileFlags DCapFSFileEngine::fileFlags(QAbstractFileEngine::FileFlags type) const
186 {
187 D_DC(DCapFSFileEngine);
188 FileFlags ret = QFSFileEngine::fileFlags(type);
189 if (ret | ExistsFlag) {
190 if (!d->canReadWrite(d->file)) {
191 ret &= ~ExistsFlag;
192 }
193 }
194
195 return ret;
196 }
197
198 bool DCapFSFileEngine::cloneTo(QAbstractFileEngine *target)
199 {
200 D_DC(DCapFSFileEngine);
201 const QString targetPath = target->fileName(DCapFSFileEngine::AbsolutePathName);
202 if (!d->canReadWrite(targetPath))
203 return false;
204 return QFSFileEngine::cloneTo(target);
205 }
206
207 bool DCapFSFileEngine::setSize(qint64 size)
208 {
209 D_D(DCapFSFileEngine);
210 if (!d->canReadWrite(d->file))
211 return false;
212 return QFSFileEngine::setSize(size);
213 }
214
215 QStringList DCapFSFileEngine::entryList(QDir::Filters filters, const QStringList &filterNames) const
216 {
217 D_DC(DCapFSFileEngine);
218 if (!d->canReadWrite(d->file))
219 return {};
220 return QFSFileEngine::entryList(filters, filterNames);
221 }
222 #if QT_VERSION >= QT_VERSION_CHECK(6, 8, 1)
223 QAbstractFileEngine::IteratorUniquePtr DCapFSFileEngine::beginEntryList(const QString &path, QDirListing::IteratorFlags filters, const QStringList &filterNames)
224 #elif QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
225 QAbstractFileEngine::IteratorUniquePtr DCapFSFileEngine::beginEntryList(const QString &path, QDir::Filters filters, const QStringList &filterNames)
226 #else
227 QAbstractFileEngine::Iterator *DCapFSFileEngine::beginEntryList(QDir::Filters filters, const QStringList &filterNames)
228 #endif
229 {
230 #if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
231 auto ret = QFSFileEngine::beginEntryList(path, filters, filterNames);
232 #else
233 auto ret = QFSFileEngine::beginEntryList(filters, filterNames);
234 DVtableHook::overrideVfptrFun(ret, &QAbstractFileEngineIterator::hasNext, &capDirIteraterHasNext);
235 #endif
236 return ret;
237 }
238
239 bool DCapFSFileEngine::canReadWrite(const QString &path) const
240 {
241 D_DC(DCapFSFileEngine);
242 return d->canReadWrite(path);
243 }
244
245 DCORE_END_NAMESPACE
246