Debian Codegraph @

src/dci/ddcifile.cpp

raw
1 // SPDX-FileCopyrightText: 2021 - 2022 UnionTech Software Technology Co., Ltd.
2 //
3 // SPDX-License-Identifier: LGPL-3.0-or-later
4
5 #include "ddcifile.h"
6 #include "private/ddcifileengine_p.h"
7
8 #ifndef DTK_NO_PROJECT
9 #include <DObjectPrivate>
10 #else
11 #define D_D(class)
12 #define D_DC(class)
13 #endif
14 #include <QDataStream>
15 #include <QFile>
16 #include <QLoggingCategory>
17 #include <QtEndian>
18 #include <QSaveFile>
19 #include <QDir>
20 #include <QBuffer>
21 #include <QCollator>
22
23 DCORE_BEGIN_NAMESPACE
24
25 #define MAGIC "DCI"
26
27 #define MAGIC_SIZE 4
28 #define VERSION_SIZE 1
29 #define FILE_COUNT_SIZE 3
30 #define FILE_META_SIZE 72
31
32 #define FILE_TYPE_SIZE 1
33 #define FILE_NAME_SIZE 63
34 #define FILE_DATA_SIZE 8
35
36 #define FILE_TYPE_FILE DDciFile::FileType::File
37 #define FILE_TYPE_DIR DDciFile::FileType::Directory
38 #define FILE_TYPE_SYMLINK DDciFile::FileType::Symlink
39
40 #ifdef QT_DEBUG
41 Q_LOGGING_CATEGORY(logDF, "dtk.dci.file")
42 #else
43 Q_LOGGING_CATEGORY(logDF, "dtk.dci.file", QtInfoMsg)
44 #endif
45
46 class DDciFilePrivate
47 #ifndef DTK_NO_PROJECT
48 : public DObjectPrivate
49 #endif
50 {
51 public:
52 DDciFilePrivate(DDciFile *qq)
53 #ifndef DTK_NO_PROJECT
54 : DObjectPrivate(qq)
55 {
56 #else
57 {
58 Q_UNUSED(qq)
59 #endif
60
61 }
62 ~DDciFilePrivate();
63
64 void setErrorString(const QString &message);
65
66 void load(const QString &fileName);
67 void load(const QByteArray &data);
68
69 QString errorMessage;
70 struct Node {
71 qint8 type = DDciFile::UnknowFile;
72 QString name;
73
74 Node *parent = nullptr;
75 QVector<Node*> children; // for directory
76 QByteArray data; // for file
77
78 ~Node() {
79 qDeleteAll(children);
80 }
81
82 QString path() const {
83 QString p = name;
84 Node *current = parent;
85 while (current) {
86 p.prepend(current->name + "/");
87 current = current->parent;
88 }
89
90 return p;
91 }
92
93 QString linkPath() const {
94 const QString &path = QString::fromUtf8(data);
95 QStringView pathView{path};
96 if (pathView.startsWith('/'))
97 return path;
98 // 转为绝对路径
99 auto pNode = parent;
100 int pathStart = 0;
101 while (pathStart < pathView.size()) {
102 if (pathView.mid(pathStart, 3) == QLatin1String("../")) {
103 pathStart += 3;
104 pNode = pNode->parent;
105 if (!pNode)
106 return QString();
107 } else if (pathView.mid(pathStart, 2) == QLatin1String("./")) {
108 pathStart += 2;
109 } else {
110 break;
111 }
112 }
113 Q_ASSERT(pNode);
114 return pNode->path() + QLatin1Char('/') + path.mid(pathStart);
115 }
116 };
117
118 qint64 writeMetaDataForNode(QIODevice *device, Node *node, qint64 dataSize) const;
119 qint64 writeDataForNode(QIODevice *device, Node *node) const;
120 qint64 writeNode(QIODevice *device, Node *node) const;
121
122 Node *mkNode(const QString &filePath);
123 void removeNode(Node *node);
124 void copyNode(const Node *from, Node *to);
125
126 bool loadDirectory(Node *directory,
127 const QByteArray &data, qint64 &begin, qint64 end,
128 QHash<QString, Node *> &pathToNode);
129
130 // 按标准中规定的文件排序计算此 name 在这个列表中的位置
131 static int getOrderedIndexOfNodeName(const decltype(Node::children) &list, const QString &name);
132
133 qint8 version = 0;
134 QScopedPointer<Node> root;
135 QHash<QString, Node*> pathToNode;
136 QByteArray rawData;
137 };
138
139 DDciFilePrivate::~DDciFilePrivate()
140 {
141
142 }
143
144 void DDciFilePrivate::setErrorString(const QString &message)
145 {
146 qCDebug(logDF, "%s", qPrintable(message));
147 errorMessage = message;
148 }
149
150 void DDciFilePrivate::load(const QString &fileName)
151 {
152 QFile file(fileName);
153
154 if (!file.open(QIODevice::ReadOnly)) {
155 setErrorString(file.errorString());
156 return;
157 }
158
159 return load(file.readAll());
160 }
161
162 void DDciFilePrivate::load(const QByteArray &data)
163 {
164 // check magic
165 if (!data.startsWith("DCI")) {
166 setErrorString(QString("Expect value is \"DCI\", "
167 "but actually value is \"%1\"")
168 .arg(QString::fromLatin1(data.left(3))));
169 return;
170 }
171
172 qint8 version = data.at(MAGIC_SIZE);
173 if (version != 1) {
174 setErrorString(QString("Not supported version: %1").arg(version));
175 return;
176 }
177
178 char fileCountData[4];
179 int fileCountOffset = MAGIC_SIZE + VERSION_SIZE;
180 memcpy(fileCountData, data.constData() + fileCountOffset, FILE_COUNT_SIZE);
181 fileCountData[3] = 0;
182 int fileCount = qFromLittleEndian<qint32>(fileCountData);
183
184 if (fileCount < 0) {
185 setErrorString(QString("Invalid file count: %1").arg(fileCount));
186 return;
187 }
188
189 qint64 offset = MAGIC_SIZE + VERSION_SIZE + FILE_COUNT_SIZE;
190 Node *root = new Node;
191 root->type = FILE_TYPE_DIR;
192 root->parent = nullptr;
193
194 QHash<QString, Node*> pathToNode;
195
196 if (!loadDirectory(root, data, offset, data.size() - 1, pathToNode)
197 || fileCount != root->children.count()) {
198 delete root;
199 return;
200 }
201
202 this->version = version;
203 this->root.reset(root);
204 this->pathToNode = pathToNode;
205 this->pathToNode["/"] = root;
206 // Node 中保存的文件数据仅是此数据的引用,因此要确保此数据一直存在
207 this->rawData = data;
208 }
209
210 qint64 DDciFilePrivate::writeMetaDataForNode(QIODevice *device, DDciFilePrivate::Node *node, qint64 dataSize) const
211 {
212 qint64 size = 0;
213 device->putChar(static_cast<char>(node->type));
214 size += FILE_TYPE_SIZE;
215
216 const QByteArray rawName = node->name.toUtf8().left(FILE_NAME_SIZE - 1);
217 size += device->write(rawName);
218 // 填充未使用的部分
219 size += device->write(QByteArray(FILE_NAME_SIZE - rawName.size(), '\0'));
220
221 char int64[FILE_DATA_SIZE];
222 qToLittleEndian<qint64>(dataSize, int64);
223 size += device->write(int64, FILE_DATA_SIZE);
224 Q_ASSERT(size == FILE_META_SIZE);
225
226 return size;
227 }
228
229 qint64 DDciFilePrivate::writeDataForNode(QIODevice *device, DDciFilePrivate::Node *node) const
230 {
231 if (node->type == FILE_TYPE_FILE
232 || node->type == FILE_TYPE_SYMLINK) {
233 return device->write(node->data);
234 } else if (node->type == FILE_TYPE_DIR) {
235 qint64 dataSize = 0;
236 for (Node *child : node->children) {
237 dataSize += writeNode(device, child);
238 }
239 return dataSize;
240 }
241
242 return 0;
243 }
244
245 qint64 DDciFilePrivate::writeNode(QIODevice *device, DDciFilePrivate::Node *node) const
246 {
247 const qint64 metaDataPos = device->pos();
248 device->seek(metaDataPos + FILE_META_SIZE);
249 const qint64 dataSize = writeDataForNode(device, node);
250 device->seek(metaDataPos);
251 const qint64 metaDataSize = writeMetaDataForNode(device, node, dataSize);
252 device->seek(device->pos() + dataSize);
253 return metaDataSize + dataSize;
254 }
255
256 DDciFilePrivate::Node *DDciFilePrivate::mkNode(const QString &filePath)
257 {
258 qCDebug(logDF, "Request create a node");
259
260 if (pathToNode.contains(filePath)) {
261 setErrorString(QString("The \"%1\" is existed").arg(filePath));
262 return nullptr;
263 }
264
265 const QFileInfo info(filePath);
266 qCDebug(logDF, "The parent directory is \"%s\"", qPrintable(info.path()));
267
268 if (Node *parentNode = pathToNode.value(info.path())) {
269 if (parentNode->type != FILE_TYPE_DIR) {
270 setErrorString(QString("The \"%1\" is not a directory").arg(info.path()));
271 return nullptr;
272 }
273
274 // 检查文件名长度,避免溢出
275 if (info.fileName().toUtf8().size() > FILE_NAME_SIZE - 1) {
276 setErrorString(QString("The file name size must less then %1 bytes").arg(FILE_NAME_SIZE));
277 return nullptr;
278 }
279
280 Node *newNode = new Node;
281 newNode->name = info.fileName();
282 newNode->parent = parentNode;
283
284 const int index = getOrderedIndexOfNodeName(parentNode->children, newNode->name);
285 parentNode->children.insert(index, newNode);
286 pathToNode[newNode->path()] = newNode;
287
288 return newNode;
289 } else {
290 setErrorString("The parent directory is not exists");
291 return nullptr;
292 }
293 }
294
295 void DDciFilePrivate::removeNode(DDciFilePrivate::Node *node)
296 {
297 Q_ASSERT(node != root.data());
298
299 node->parent->children.removeOne(node);
300 auto removedNode = pathToNode.take(node->path());
301 Q_ASSERT(removedNode == node);
302
303 for (Node *child : node->children) {
304 Q_ASSERT(child->parent == node);
305 auto removedNode = pathToNode.take(child->path());
306 Q_ASSERT(removedNode == child);
307 }
308
309 delete node;
310 }
311
312 void DDciFilePrivate::copyNode(const DDciFilePrivate::Node *from, DDciFilePrivate::Node *to)
313 {
314 QList<QPair<const Node*, Node*>> copyPendingList;
315 copyPendingList << qMakePair(from, to);
316
317 for (int i = 0; i < copyPendingList.size(); ++i) {
318 auto f = copyPendingList.at(i).first;
319 auto t = copyPendingList.at(i).second;
320
321 t->type = f->type;
322 t->data = f->data;
323
324 for (const auto child : f->children) {
325 if (child == to)
326 continue;
327
328 Node *newChild = new Node;
329 newChild->parent = t;
330 newChild->name = child->name;
331 pathToNode[newChild->path()] = newChild;
332
333 const int index = getOrderedIndexOfNodeName(t->children, newChild->name);
334 t->children.insert(index, newChild);
335 copyPendingList << qMakePair(child, newChild);
336 }
337 }
338 }
339
340 bool DDciFilePrivate::loadDirectory(DDciFilePrivate::Node *directory,
341 const QByteArray &data, qint64 &offset, qint64 end,
342 QHash<QString, DDciFilePrivate::Node *> &pathToNode)
343 {
344 // load files
345 while (offset < end) {
346 Node *node = new Node;
347
348 node->parent = directory;
349 node->type = data.at(offset);
350 offset += FILE_TYPE_SIZE;
351 // 计算文件名的长度
352 const int nameLength = data.indexOf('\0', offset) - offset;
353 if (nameLength <= 0 || nameLength >= FILE_NAME_SIZE) {
354 setErrorString(QString("Invalid file name, the data offset: %1").arg(offset));
355 delete node;
356 return false;
357 }
358 node->name = QString::fromUtf8(data.constData() + offset, nameLength);
359 offset += FILE_NAME_SIZE;
360
361 const qint64 dataSize = qFromLittleEndian<qint64>(data.constData() + offset);
362 offset += FILE_DATA_SIZE;
363
364 // 无失败时调用 break
365 do {
366 if (node->type == FILE_TYPE_DIR) {
367 if (loadDirectory(node, data, offset, offset + dataSize - 1, pathToNode)) {
368 break;
369 }
370 } else if (node->type == FILE_TYPE_FILE
371 || node->type == FILE_TYPE_SYMLINK) {
372 // 跳过文件内容
373 node->data = QByteArray::fromRawData(data.constData() + offset, dataSize);
374
375 if (node->data.size() == dataSize) {
376 offset += dataSize;
377 break;
378 } else {
379 setErrorString(QString("Invalid data size of \"%1\" file").arg(node->path()));
380 }
381 } else {
382 setErrorString(QString("Invalid file type: %1").arg(node->type));
383 }
384
385 delete node;
386 return false;
387 } while (false);
388
389 directory->children << node;
390 pathToNode[node->path()] = node;
391 }
392
393 return true;
394 }
395
396 int DDciFilePrivate::getOrderedIndexOfNodeName(const decltype(Node::children) &list, const QString &name)
397 {
398 QCollator collator(QLocale::English);
399 collator.setNumericMode(true);
400 for (int i = 0; i < list.count(); ++i) {
401 const Node *node = list.at(i);
402 if (collator.compare(name, node->name) < 0)
403 return i;
404 }
405
406 return list.count();
407 }
408
409 void DDciFile::registerFileEngine()
410 {
411 // 在 QAbstractFileEngineHandler 的构造函数中会注册自己,后续
412 // 在使用 QFile 时会调用 DDciFileEngineHandler::create
413 static DDciFileEngineHandler globalHandler;
414 Q_UNUSED(globalHandler);
415 }
416
417 #ifndef DTK_NO_PROJECT
418 DDciFile::DDciFile()
419 : DObject(*new DDciFilePrivate(this))
420 {
421 d_func()->load(QByteArrayLiteral("DCI\0\1\0\0\0"));
422 }
423
424 DDciFile::DDciFile(const QString &fileName)
425 : DObject(*new DDciFilePrivate(this))
426 {
427 d_func()->load(fileName);
428 }
429
430 DDciFile::DDciFile(const QByteArray &data)
431 : DObject(*new DDciFilePrivate(this))
432 {
433 d_func()->load(data);
434 }
435 #else
436 DDciFile::DDciFile()
437 : d(new DDciFilePrivate(this))
438 {
439 d->load(QByteArrayLiteral("DCI\0\1\0\0\0"));
440 }
441
442 DDciFile::DDciFile(const QString &fileName)
443 : d(new DDciFilePrivate(this))
444 {
445 d->load(fileName);
446 }
447
448 DDciFile::DDciFile(const QByteArray &data)
449 : d(new DDciFilePrivate(this))
450 {
451 d->load(data);
452 }
453 #endif
454
455 bool DDciFile::isValid() const
456 {
457 D_DC(DDciFile);
458 return !!(d->root);
459 }
460
461 QString DDciFile::lastErrorString() const
462 {
463 D_DC(DDciFile);
464 return d->errorMessage;
465 }
466
467 bool DDciFile::writeToFile(const QString &fileName) const
468 {
469 QSaveFile sf(fileName);
470 do {
471 if (!sf.open(QIODevice::WriteOnly)) {
472 break;
473 }
474 if (!writeToDevice(&sf))
475 return false;
476 if (!sf.commit())
477 break;
478 return true;
479 } while (false);
480
481 qCDebug(logDF, "Failed on write to file \"%s\", error message is: \"%s\"",
482 qPrintable(fileName), qPrintable(sf.errorString()));
483 return false;
484 }
485
486 bool DDciFile::writeToDevice(QIODevice *device) const
487 {
488 Q_ASSERT(isValid());
489 D_DC(DDciFile);
490
491 // magic
492 device->write(QByteArrayLiteral("DCI\0"));
493 // version
494 device->putChar(static_cast<char>(d->version));
495 char fileCountData[sizeof(int)];
496 qToLittleEndian<int>(d->root->children.count(), fileCountData);
497 // file count
498 device->write(fileCountData, FILE_COUNT_SIZE);
499 d->writeDataForNode(device, d->root.data());
500
501 return device->size() >= metadataSizeV1()
502 + (d->pathToNode.count() - 1) * FILE_META_SIZE;
503 }
504
505 QByteArray DDciFile::toData() const
506 {
507 if (!isValid())
508 return QByteArray();
509
510 D_DC(DDciFile);
511
512 qint64 allFilesContentSize = 0;
513 for (auto node : d->pathToNode) {
514 if (node->type == FILE_TYPE_FILE
515 || node->type == FILE_TYPE_SYMLINK)
516 allFilesContentSize += node->data.size();
517 }
518
519 QByteArray data;
520 // -1 是排除根目录
521 data.resize(metadataSizeV1() + (d->pathToNode.count() - 1) * FILE_META_SIZE
522 + allFilesContentSize);
523 QBuffer buffer(&data);
524
525 if (!buffer.open(QIODevice::WriteOnly) || !writeToDevice(&buffer))
526 return QByteArray();
527
528 return data;
529 }
530
531 constexpr int DDciFile::metadataSizeV1()
532 {
533 return MAGIC_SIZE + VERSION_SIZE + FILE_COUNT_SIZE;
534 }
535
536 QStringList DDciFile::list(const QString &dir, bool onlyFileName) const
537 {
538 if (!isValid())
539 return {};
540
541 D_DC(DDciFile);
542
543 auto dirNode = d->pathToNode.value(dir);
544 if (!dirNode) {
545 qCDebug(logDF, "The \"%s\" is not exists", qPrintable(dir));
546 return {};
547 }
548
549 if (dirNode->type != FILE_TYPE_DIR) {
550 qCWarning(logDF, "The \"%s\" is not a directory", qPrintable(dir));
551 return {};
552 }
553
554 QStringList children;
555 for (auto child : dirNode->children) {
556 children << (onlyFileName ? child->name : QDir(dir).filePath(child->name));
557 }
558
559 return children;
560 }
561
562 int DDciFile::childrenCount(const QString &dir) const
563 {
564 if (!isValid())
565 return 0;
566
567 D_DC(DDciFile);
568
569 auto dirNode = d->pathToNode.value(dir);
570 if (!dirNode) {
571 return 0;
572 }
573
574 return dirNode->children.count();
575 }
576
577 bool DDciFile::exists(const QString &filePath) const
578 {
579 if (!isValid())
580 return false;
581
582 D_DC(DDciFile);
583 return d->pathToNode.contains(filePath);
584 }
585
586 DDciFile::FileType DDciFile::type(const QString &filePath) const
587 {
588 if (!isValid())
589 return UnknowFile;
590
591 D_DC(DDciFile);
592
593 auto node = d->pathToNode.value(filePath);
594 if (!node) {
595 qCDebug(logDF, "The \"%s\" is not exists", qPrintable(filePath));
596 return DDciFile::UnknowFile;
597 }
598
599 return static_cast<DDciFile::FileType>(node->type);
600 }
601
602 QByteArray DDciFile::dataRef(const QString &filePath) const
603 {
604 if (!isValid())
605 return QByteArray();
606
607 D_DC(DDciFile);
608
609 auto node = d->pathToNode.value(filePath);
610 if (!node) {
611 qCDebug(logDF, "The \"%s\" is not exists", qPrintable(filePath));
612 return QByteArray();
613 }
614
615 if (node->type == FILE_TYPE_SYMLINK) {
616 return dataRef(node->linkPath());
617 }
618
619 return node->data;
620 }
621
622 QString DDciFile::name(const QString &filePath) const
623 {
624 if (!isValid())
625 return QString();
626
627 D_DC(DDciFile);
628 if (auto node = d->pathToNode.value(filePath)) {
629 return node->name;
630 }
631
632 return QString();
633 }
634
635 QString DDciFile::symlinkTarget(const QString &filePath, bool originData) const
636 {
637 if (!isValid())
638 return QString();
639
640 D_DC(DDciFile);
641 if (auto node = d->pathToNode.value(filePath)) {
642 if (node->type != FILE_TYPE_SYMLINK)
643 return QString();
644
645 if (originData) {
646 return QString::fromUtf8(node->data);
647 }
648
649 const QString &linkPath = node->linkPath();
650 const auto targetNode = d->pathToNode.value(linkPath);
651
652 // 链接的目标只能是“不存在的路径”、“文件”、“链接”,不可是目录
653 if (!targetNode || targetNode->type == FILE_TYPE_FILE
654 || targetNode->type == FILE_TYPE_SYMLINK)
655 return linkPath;
656 }
657
658 return QString();
659 }
660
661 bool DDciFile::mkdir(const QString &filePath)
662 {
663 Q_ASSERT(isValid());
664 D_D(DDciFile);
665
666 qCDebug(logDF, "Request create the \"%s\" directory", qPrintable(filePath));
667 auto node = d->mkNode(filePath);
668 if (!node)
669 return false;
670 node->type = FILE_TYPE_DIR;
671 return true;
672 }
673
674 bool DDciFile::writeFile(const QString &filePath, const QByteArray &data, bool override)
675 {
676 Q_ASSERT(isValid());
677 D_D(DDciFile);
678
679 qCDebug(logDF, "Request create the \"%s\" file", qPrintable(filePath));
680 // 先删除旧的数据
681 if (auto node = d->pathToNode.value(filePath)) {
682 if (override) {
683 if (node->type == FILE_TYPE_SYMLINK) {
684 const QString &linkPath = node->linkPath();
685 qCDebug(logDF(), "Follow the symlink to \"%s\"", qPrintable(linkPath));
686
687 if (!d->pathToNode.contains(linkPath)) {
688 qCDebug(logDF(), "Can't write to a symlink target file if it is not existed");
689 return false;
690 }
691
692 return writeFile(linkPath, data, override);
693 }
694
695 qCDebug(logDF, "Try override the file");
696 if (node->type != FILE_TYPE_FILE) {
697 qCWarning(logDF, "The \"%s\" is existed and it is not a file", qPrintable(filePath));
698 return false;
699 }
700
701 node->data = data;
702 return true;
703 } else {
704 d->setErrorString("No the \"override\" flag and the file is existed, can't write");
705 return false;
706 }
707 }
708
709 auto node = d->mkNode(filePath);
710 if (!node)
711 return false;
712
713 node->type = FILE_TYPE_FILE;
714 node->data = data;
715 return true;
716 }
717
718 bool DDciFile::remove(const QString &filePath)
719 {
720 Q_ASSERT(isValid());
721 D_D(DDciFile);
722
723 if (auto node = d->pathToNode.value(filePath)) {
724 if (node == d->root.data()) {
725 for (auto child : d->root->children)
726 d->removeNode(child);
727 d->root->children.clear();
728 } else {
729 d->removeNode(node);
730 }
731 return true;
732 } else {
733 d->setErrorString("The file is not exists");
734 return false;
735 }
736 }
737
738 bool DDciFile::rename(const QString &filePath, const QString &newFilePath, bool override)
739 {
740 Q_ASSERT(isValid());
741 D_D(DDciFile);
742
743 qCDebug(logDF, "Rename from \"%s\" to \"%s\"", qPrintable(filePath), qPrintable(newFilePath));
744 if (filePath == newFilePath)
745 return false;
746
747 if (newFilePath.toUtf8().size() >= FILE_META_SIZE) {
748 d->setErrorString(QString("The new name size must less then %1 bytes").arg(FILE_NAME_SIZE));
749 return false;
750 }
751
752 if (!override && d->pathToNode.contains(newFilePath)) {
753 d->setErrorString("The target file is existed");
754 return false;
755 }
756 auto overrideNode = override ? d->pathToNode.take(newFilePath) : nullptr;
757
758 if (auto node = d->pathToNode.take(filePath)) {
759 QFileInfo info(newFilePath);
760 if (auto parent = d->pathToNode.value(info.absolutePath())) {
761 node->name = info.fileName();
762
763 // 从旧节点删除添加到新的节点
764 if (node->parent != parent) {
765 bool ok = node->parent->children.removeOne(node);
766 Q_ASSERT(ok);
767 const int index = d->getOrderedIndexOfNodeName(parent->children, node->name);
768 parent->children.insert(index, node);
769 node->parent = parent;
770 }
771
772 d->pathToNode[info.absoluteFilePath()] = node;
773 Q_ASSERT(node->path() == info.absoluteFilePath());
774
775 // 删除被覆盖的节点
776 if (overrideNode) {
777 Q_ASSERT(overrideNode->parent == parent);
778 overrideNode->parent->children.removeOne(overrideNode);
779 delete overrideNode;
780 }
781
782 return true;
783 } else {
784 d->setErrorString(QString("The \"%1\" directory is not exists").arg(info.absolutePath()));
785 return false;
786 }
787 } else {
788 d->setErrorString("The file is not exists");
789 return false;
790 }
791 }
792
793 bool DDciFile::copy(const QString &from, const QString &to)
794 {
795 Q_ASSERT(isValid());
796 D_D(DDciFile);
797
798 const auto fromNode = d->pathToNode.value(from);
799 if (!fromNode) {
800 d->setErrorString(QString("The \"%1\" is not exists").arg(from));
801 return false;
802 }
803
804 auto toNode = d->mkNode(to);
805 if (!toNode) {
806 return false;
807 }
808
809 d->copyNode(fromNode, toNode);
810 return true;
811 }
812
813 bool DDciFile::link(const QString &source, const QString &to)
814 {
815 Q_ASSERT(isValid());
816 D_D(DDciFile);
817
818 if (source == to || source.isEmpty())
819 return false;
820
821 auto toNode = d->mkNode(to);
822 if (!toNode)
823 return false;
824 toNode->type = FILE_TYPE_SYMLINK;
825 toNode->data = source.toUtf8();
826
827 return true;
828 }
829
830 DCORE_END_NAMESPACE