Debian Codegraph @

src/filesystem/dtrashmanager_linux.cpp

raw
1 // SPDX-FileCopyrightText: 2017 - 2022 UnionTech Software Technology Co., Ltd.
2 //
3 // SPDX-License-Identifier: LGPL-3.0-or-later
4
5 #include "dtrashmanager.h"
6 #include "dstandardpaths.h"
7 #include "base/private/dobject_p.h"
8
9 #include <QDirIterator>
10 #include <QStorageInfo>
11 #include <QCryptographicHash>
12 #include <QDateTime>
13 #include <QDebug>
14
15 #define TRASH_PATH \
16 DStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/Trash"
17 #define TRASH_INFO_PATH TRASH_PATH"/info"
18 #define TRASH_FILES_PATH TRASH_PATH"/files"
19
20 DCORE_BEGIN_NAMESPACE
21
22 class DTrashManager_ : public DTrashManager {};
23 Q_GLOBAL_STATIC(DTrashManager_, globalTrashManager)
24
25 static QString getNotExistsFileName(const QString &fileName, const QString &targetPath)
26 {
27 QByteArray name = fileName.toUtf8();
28
29 int index = name.lastIndexOf('.');
30 QByteArray suffix;
31
32 if (index >= 0) {
33 suffix = name.mid(index);
34 }
35
36 if (suffix.size() > 200) {
37 suffix = suffix.left(200);
38 }
39
40 name.chop(suffix.size());
41 name = name.left(200 - suffix.size());
42
43 while (QFile::exists(targetPath + "/" + name + suffix)) {
44 name = QCryptographicHash::hash(name, QCryptographicHash::Md5).toHex();
45 }
46
47 return QString::fromUtf8(name + suffix);
48 }
49
50 static bool writeTrashInfo(const QString &fileBaseName, const QString &sourceFilePath, const QDateTime &datetime, QString *errorString = NULL)
51 {
52 QFile metadata(TRASH_INFO_PATH"/" + fileBaseName + ".trashinfo");
53
54 if (metadata.exists()) {
55 if (errorString) {
56 *errorString = QString("The %1 file is exists").arg(metadata.fileName());
57 }
58
59 return false;
60 }
61
62 if (!metadata.open(QIODevice::WriteOnly)) {
63 if (errorString) {
64 *errorString = metadata.errorString();
65 }
66
67 return false;
68 }
69
70 QByteArray data;
71
72 data.append("[Trash Info]\n");
73 data.append("Path=").append(sourceFilePath.toUtf8().toPercentEncoding("/")).append("\n");
74 data.append("DeletionDate=").append(datetime.toString(Qt::ISODate).toLatin1()).append("\n");
75
76 qint64 size = metadata.write(data);
77 metadata.close();
78
79 if (size <= 0) {
80 if (errorString) {
81 *errorString = metadata.errorString();
82 }
83
84 return false;
85 }
86
87 return true;
88 }
89
90 static bool renameFile(const QFileInfo &fileInfo, const QString &target, QString *errorString = NULL)
91 {
92 if (fileInfo.isFile() || fileInfo.isSymLink()) {
93 QFile file(fileInfo.filePath());
94
95 if (!file.rename(target)) {
96 if (errorString) {
97 *errorString = file.errorString();
98 }
99
100 return false;
101 }
102
103 return true;
104 } else {
105 QDirIterator iterator(fileInfo.filePath(),
106 QDir::AllEntries | QDir::NoDotAndDotDot | QDir::Hidden | QDir::System);
107
108 while (iterator.hasNext()) {
109 iterator.next();
110
111 const QString newFile = iterator.filePath().replace(0, fileInfo.filePath().length(), target);
112
113 if (!QDir().mkpath(QFileInfo(newFile).path())) {
114 if (errorString) {
115 *errorString = QString("Make the %1 path is failed").arg(QFileInfo(newFile).path());
116 }
117
118 return false;
119 }
120
121 if (!renameFile(iterator.fileInfo(), newFile, errorString)) {
122 return false;
123 }
124 }
125
126 if (!QDir().rmdir(fileInfo.filePath())) {
127 if (errorString) {
128 *errorString = QString("Cannot remove the %1 dir").arg(fileInfo.filePath());
129 }
130
131 return false;
132 }
133 }
134
135 return true;
136 }
137
138 class DTrashManagerPrivate : public DTK_CORE_NAMESPACE::DObjectPrivate
139 {
140 public:
141 DTrashManagerPrivate(DTrashManager *q_ptr)
142 : DObjectPrivate(q_ptr) {}
143
144 static bool removeFileOrDir(const QString &path);
145 static bool removeFromIterator(QDirIterator &iter);
146
147 D_DECLARE_PUBLIC(DTrashManager)
148 };
149
150 DTrashManager *DTrashManager::instance()
151 {
152 return globalTrashManager;
153 }
154
155 bool DTrashManager::trashIsEmpty() const
156 {
157 QDirIterator iterator(TRASH_INFO_PATH,
158 // QStringList() << "*.trashinfo",
159 QDir::Files | QDir::NoDotAndDotDot | QDir::Hidden);
160
161 return !iterator.hasNext();
162 }
163
164 bool DTrashManager::cleanTrash()
165 {
166 QDirIterator iterator_info(TRASH_INFO_PATH,
167 QDir::Files | QDir::NoDotAndDotDot | QDir::Hidden);
168
169 QDirIterator iterator_files(TRASH_FILES_PATH,
170 QDir::AllEntries | QDir::NoDotAndDotDot | QDir::Hidden | QDir::System,
171 QDirIterator::Subdirectories);
172
173 return DTrashManagerPrivate::removeFromIterator(iterator_info) &&
174 DTrashManagerPrivate::removeFromIterator(iterator_files);
175 }
176
177 bool DTrashManager::moveToTrash(const QString &filePath, bool followSymlink)
178 {
179 QFileInfo fileInfo(filePath);
180
181 if (!fileInfo.exists() && (followSymlink || !fileInfo.isSymLink())) {
182 return false;
183 }
184
185 QDir trashDir(TRASH_FILES_PATH);
186 QStorageInfo storageInfo(fileInfo.filePath());
187 QStorageInfo trashStorageInfo(trashDir);
188
189 if (storageInfo != trashStorageInfo) {
190 return false;
191 }
192
193 if (!trashDir.mkpath(TRASH_INFO_PATH)) {
194 return false;
195 }
196
197 if (!trashDir.mkpath(TRASH_FILES_PATH)) {
198 return false;
199 }
200
201 if (followSymlink && fileInfo.isSymLink()) {
202 fileInfo.setFile(fileInfo.symLinkTarget());
203 }
204
205 const QString &fileName = getNotExistsFileName(fileInfo.fileName(), TRASH_FILES_PATH);
206
207 if (!writeTrashInfo(fileName, fileInfo.filePath(), QDateTime::currentDateTime())) {
208 return false;
209 }
210
211 const QString &newFilePath = TRASH_FILES_PATH"/" + fileName;
212
213 return renameFile(fileInfo, newFilePath);
214 }
215
216 DTrashManager::DTrashManager()
217 : QObject()
218 , DObject(*new DTrashManagerPrivate(this))
219 {
220
221 }
222
223 bool DTrashManagerPrivate::removeFileOrDir(const QString &path)
224 {
225 QFileInfo fileInfo(path);
226 if (fileInfo.isDir() && !fileInfo.isSymLink()) {
227 QDir dir(path);
228 return dir.removeRecursively();
229 } else {
230 return QFile::remove(path);
231 }
232 }
233
234 bool DTrashManagerPrivate::removeFromIterator(QDirIterator &iter)
235 {
236 bool ok = true;
237 while (iter.hasNext()) {
238 QString nextPath = iter.next();
239 // qDebug() << iter.fileName() << iterator_info.filePath();
240 if (!DTrashManagerPrivate::removeFileOrDir(nextPath)) {
241 ok = false;
242 }
243 }
244 return ok;
245 }
246
247 DCORE_END_NAMESPACE