Debian Codegraph @

src/filesystem/dcapfile.cpp

raw
1 // SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
2 //
3 // SPDX-License-Identifier: LGPL-3.0-or-later
4
5 #include "dcapfile.h"
6 #include "dobject_p.h"
7 #include "private/dcapfsfileengine_p.h"
8
9 #include <private/qdir_p.h>
10
11 DCORE_BEGIN_NAMESPACE
12
13 extern QString _d_cleanPath(const QString &path);
14 extern bool _d_isSubFileOf(const QString &filePath, const QString &directoryPath);
15
16 class DCapFilePrivate : public DObjectPrivate
17 {
18 D_DECLARE_PUBLIC(DCapFile)
19 public:
20 DCapFilePrivate(DCapFile *qq, const QString &fileName = QString());
21 static bool canReadWrite(const QString &path);
22
23 QString fileName;
24 };
25
26 DCapFilePrivate::DCapFilePrivate(DCapFile *qq, const QString &fileName)
27 : DObjectPrivate(qq)
28 , fileName(fileName)
29 {
30 }
31
32 bool DCapFilePrivate::canReadWrite(const QString &path)
33 {
34 DCapFSFileEngine engine(path);
35 return engine.canReadWrite(path);
36 }
37
38 DCapFile::DCapFile(QObject *parent)
39 : QFile(parent)
40 , DObject(*new DCapFilePrivate(this))
41 {
42
43 }
44
45 DCapFile::DCapFile(const QString &name, QObject *parent)
46 : QFile(name, parent)
47 , DObject(*new DCapFilePrivate(this, name))
48 {
49 }
50
51 DCapFile::~DCapFile()
52 {
53
54 }
55
56 void DCapFile::setFileName(const QString &name)
57 {
58 D_D(DCapFile);
59 d->fileName = name;
60 return QFile::setFileName(name);
61 }
62
63 bool DCapFile::exists() const
64 {
65 D_DC(DCapFile);
66 if (!d->canReadWrite(d->fileName))
67 return false;
68
69 return QFile::exists();
70 }
71
72 bool DCapFile::exists(const QString &fileName)
73 {
74 return DCapFile(fileName).exists();
75 }
76
77 #if DTK_VERSION < DTK_VERSION_CHECK(6, 0, 0, 0)
78 QString DCapFile::readLink() const
79 {
80 D_DC(DCapFile);
81 if (!d->canReadWrite(d->fileName))
82 return {};
83
84 return QFile::symLinkTarget();
85 }
86 #endif
87
88 #if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
89 QString DCapFile::symLinkTarget() const
90 {
91 return QFile::symLinkTarget();
92 }
93 #endif
94
95 bool DCapFile::remove()
96 {
97 D_D(DCapFile);
98 if (!d->canReadWrite(d->fileName))
99 return false;
100
101 return QFile::remove();
102 }
103
104 bool DCapFile::remove(const QString &fileName)
105 {
106 return DCapFile(fileName).remove();
107 }
108
109 #if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
110 bool DCapFile::moveToTrash()
111 {
112 D_D(DCapFile);
113 if (!d->canReadWrite(d->fileName))
114 return false;
115
116 return QFile::moveToTrash();
117 }
118
119 bool DCapFile::moveToTrash(const QString &fileName, QString *pathInTrash)
120 {
121 DCapFile file(fileName);
122 if (file.moveToTrash()) {
123 if (pathInTrash)
124 *pathInTrash = file.fileName();
125 return true;
126 }
127 return false;
128 }
129 #endif
130
131 bool DCapFile::rename(const QString &newName)
132 {
133 D_D(DCapFile);
134 if (!d->canReadWrite(newName))
135 return false;
136
137 return QFile::rename(newName);
138 }
139
140 bool DCapFile::rename(const QString &oldName, const QString &newName)
141 {
142 if (!DCapFilePrivate::canReadWrite(oldName))
143 return false;
144
145 return DCapFile(oldName).rename(newName);
146 }
147
148 bool DCapFile::link(const QString &newName)
149 {
150 D_D(DCapFile);
151 if (!d->canReadWrite(newName))
152 return false;
153
154 return QFile::link(newName);
155 }
156
157 bool DCapFile::link(const QString &oldName, const QString &newName)
158 {
159 if (!DCapFilePrivate::canReadWrite(oldName))
160 return false;
161
162 return DCapFile(oldName).link(newName);
163 }
164
165 bool DCapFile::copy(const QString &newName)
166 {
167 D_D(DCapFile);
168 if (!d->canReadWrite(newName))
169 return false;
170
171 return QFile::copy(newName);
172 }
173
174 bool DCapFile::copy(const QString &fileName, const QString &newName)
175 {
176 if (!DCapFilePrivate::canReadWrite(fileName))
177 return false;
178
179 return DCapFile(fileName).copy(newName);
180 }
181
182 bool DCapFile::open(QIODevice::OpenMode flags)
183 {
184 D_D(DCapFile);
185 if (!d->canReadWrite(d->fileName))
186 return false;
187
188 return QFile::open(flags);
189 }
190
191 bool DCapFile::resize(qint64 sz)
192 {
193 D_D(DCapFile);
194 if (!d->canReadWrite(d->fileName))
195 return false;
196
197 return QFile::resize(sz);
198 }
199
200 bool DCapFile::resize(const QString &fileName, qint64 sz)
201 {
202 return DCapFile(fileName).resize(sz);
203 }
204
205 bool DCapFile::open(FILE *, QIODevice::OpenMode, QFileDevice::FileHandleFlags)
206 {
207 return false;
208 }
209
210 bool DCapFile::open(int, QIODevice::OpenMode, QFileDevice::FileHandleFlags)
211 {
212 return false;
213 }
214
215 class DCapDirPrivate : public QSharedData
216 {
217 public:
218 DCapDirPrivate(QString filePath);
219 explicit DCapDirPrivate(const DCapDirPrivate &copy);
220
221 QString filePath;
222 };
223
224 DCapDirPrivate::DCapDirPrivate(QString filePath)
225 : filePath(filePath)
226 {
227 }
228
229 DCapDirPrivate::DCapDirPrivate(const DCapDirPrivate &copy)
230 : QSharedData(copy)
231 , filePath(copy.filePath)
232 {
233 }
234
235 DCapDir::DCapDir(const DCapDir &dir)
236 : QDir(dir)
237 , dd_ptr(dir.dd_ptr)
238 {
239
240 }
241
242 DCapDir::DCapDir(const QString &path)
243 : QDir(path)
244 , dd_ptr(new DCapDirPrivate(path))
245 {
246
247 }
248
249 DCapDir::DCapDir(const QString &path, const QString &nameFilter,
250 QDir::SortFlags sort, QDir::Filters filter)
251 : QDir(path, nameFilter, sort, filter)
252 , dd_ptr(new DCapDirPrivate(path))
253 {
254
255 }
256
257 DCapDir::~DCapDir()
258 {
259
260 }
261
262 void DCapDir::setPath(const QString &path)
263 {
264 dd_ptr = new DCapDirPrivate(path);
265 return QDir::setPath(path);
266 }
267
268 bool DCapDir::cd(const QString &dirName)
269 {
270 auto old_d = d_ptr;
271 bool ret = QDir::cd(dirName);
272 if (!ret)
273 return ret;
274
275 // take the new path.
276 auto path = QDir::filePath("");
277 QScopedPointer<DCapFSFileEngine> fsEngine(new DCapFSFileEngine(path));
278 if (fsEngine->fileFlags(QAbstractFileEngine::FlagsMask) & QAbstractFileEngine::ExistsFlag) {
279 dd_ptr = new DCapDirPrivate(path);
280 return true;
281 }
282 d_ptr = old_d;
283 return false;
284 }
285
286 QStringList DCapDir::entryList(DCapDir::Filters filters, DCapDir::SortFlags sort) const
287 {
288 const QDirPrivate* d = d_ptr.constData();
289 return entryList(d->nameFilters, filters, sort);
290 }
291
292 QStringList DCapDir::entryList(const QStringList &nameFilters, DCapDir::Filters filters, DCapDir::SortFlags sort) const
293 {
294 if (!DCapFilePrivate::canReadWrite(dd_ptr->filePath))
295 return {};
296 return QDir::entryList(nameFilters, filters, sort);
297 }
298
299 QFileInfoList DCapDir::entryInfoList(QDir::Filters filters, QDir::SortFlags sort) const
300 {
301 const QDirPrivate* d = d_ptr.constData();
302 return entryInfoList(d->nameFilters, filters, sort);
303 }
304
305 QFileInfoList DCapDir::entryInfoList(const QStringList &nameFilters, DCapDir::Filters filters, DCapDir::SortFlags sort) const
306 {
307 if (!DCapFilePrivate::canReadWrite(dd_ptr->filePath))
308 return {};
309 return QDir::entryInfoList(nameFilters, filters, sort);
310 }
311
312 bool DCapDir::mkdir(const QString &dirName) const
313 {
314 QString fn = filePath(dirName);
315 if (!DCapFilePrivate::canReadWrite(fn))
316 return false;
317
318 return QDir::mkdir(dirName);
319 }
320
321 bool DCapDir::rmdir(const QString &dirName) const
322 {
323 QString fn = filePath(dirName);
324 if (!DCapFilePrivate::canReadWrite(fn))
325 return false;
326
327 return QDir::rmdir(dirName);
328 }
329
330 bool DCapDir::mkpath(const QString &dirPath) const
331 {
332 QString fn = filePath(dirPath);
333 if (!DCapFilePrivate::canReadWrite(fn))
334 return false;
335
336 return QDir::mkpath(dirPath);
337 }
338
339 bool DCapDir::rmpath(const QString &dirPath) const
340 {
341 QString fn = filePath(dirPath);
342 if (!DCapFilePrivate::canReadWrite(fn))
343 return false;
344
345 return QDir::rmpath(dirPath);
346 }
347
348 bool DCapDir::exists() const
349 {
350 if (!DCapFilePrivate::canReadWrite(dd_ptr->filePath))
351 return false;
352
353 return QDir::exists();
354 }
355
356 bool DCapDir::exists(const QString &name) const
357 {
358 if (name.isEmpty()) {
359 qWarning("DCapFile::exists: Empty or null file name");
360 return false;
361 }
362 return DCapFile::exists(filePath(name));
363 }
364
365 bool DCapDir::remove(const QString &fileName)
366 {
367 if (fileName.isEmpty()) {
368 qWarning("DCapDir::remove: Empty or null file name");
369 return false;
370 }
371 return DCapFile::remove(filePath(fileName));
372 }
373
374 bool DCapDir::rename(const QString &oldName, const QString &newName)
375 {
376 if (oldName.isEmpty() || newName.isEmpty()) {
377 qWarning("DCapDir::rename: Empty or null file name(s)");
378 return false;
379 }
380
381 DCapFile file(filePath(oldName));
382 if (!file.exists())
383 return false;
384 return file.rename(filePath(newName));
385 }
386
387 DCORE_END_NAMESPACE