This repository has been archived by the owner on Oct 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathTrashcan.cpp
169 lines (154 loc) · 6.7 KB
/
Trashcan.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* Copyright (C) 2018 Roman Chistokhodov <[email protected]>
* This file is part of Phototonic Image Viewer.
*
* Phototonic is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Phototonic is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Phototonic. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QtGlobal>
#include "Trashcan.h"
#if defined(Q_OS_UNIX) && !defined(Q_OS_ANDROID) && !defined(Q_OS_DARWIN)
// Implementation for freedesktop systems adheres to https://specifications.freedesktop.org/trash-spec/trashspec-latest.html
#include <QDateTime>
#include <QDir>
#include <QFileInfo>
#include <QFile>
#include <QStandardPaths>
#include <QStorageInfo>
#include <QTextStream>
#include <QUrl>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <cerrno>
static Trash::Result moveToTrashDir(const QString& filePath, const QDir& trashDir, QString& error, const QStorageInfo& nonHomeStorage)
{
const QDir trashInfoDir = QDir(trashDir.filePath("info"));
const QDir trashFilesDir = QDir(trashDir.filePath("files"));
if (trashInfoDir.mkpath(".") && trashFilesDir.mkpath(".")) {
QFileInfo fileInfo(filePath);
QString fileName = fileInfo.fileName();
QString infoFileName = fileName + ".trashinfo";
int fd;
const int flag = O_CREAT | O_WRONLY | O_EXCL;
const int mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
for (unsigned int n = 2; trashFilesDir.exists(fileName) ||
((fd = open(trashInfoDir.filePath(infoFileName).toUtf8().data(), flag, mode)) == -1 && errno == EEXIST); ++n) {
fileName = QString("%1.%2.%3").arg(fileInfo.baseName(), QString::number(n), fileInfo.completeSuffix());
infoFileName = fileName + ".trashinfo";
}
if (fd == -1) {
error = strerror(errno);
return Trash::Error;
}
const QString moveHere = trashFilesDir.filePath(fileName);
const QString deletionDate = QDateTime::currentDateTime().toString(Qt::ISODate);
const QString path = nonHomeStorage.isValid() ? QDir(nonHomeStorage.rootPath()).relativeFilePath(filePath) : filePath;
const QString escapedPath = QString::fromUtf8(QUrl::toPercentEncoding(path, "/"));
QFile infoFile;
if (infoFile.open(fd, QIODevice::WriteOnly, QFileDevice::AutoCloseHandle)) {
QTextStream out(&infoFile);
out << "[Trash Info]\nPath=" << escapedPath << "\nDeletionDate=" << deletionDate << '\n';
} else {
error = infoFile.errorString();
return Trash::Error;
}
if (QDir().rename(filePath, moveHere)) {
return Trash::Success;
} else {
error = QString("Could not rename %1 to %2").arg(filePath, moveHere);
return Trash::Error;
}
} else {
error = "Could not set up trash subdirectories";
return Trash::Error;
}
}
Trash::Result Trash::moveToTrash(const QString &path, QString &error, Trash::Options trashOptions)
{
if (path.isEmpty()) {
error = "Path is empty";
return Trash::Error;
}
const QString filePath = QFileInfo(path).absoluteFilePath();
const QStorageInfo filePathStorage(filePath);
if (!filePathStorage.isValid()) {
error = "Could not get device of the file being trashed";
return Trash::Error;
}
const QString homeDataLocation = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation);
const QDir homeDataDirectory(homeDataLocation);
if (homeDataLocation.isEmpty() || !homeDataDirectory.exists()) {
error = "Could not get home data folder";
return Trash::Error;
}
if (QStorageInfo(homeDataLocation) == filePathStorage || trashOptions == Trash::ForceDeletionToHomeTrash) {
const QDir homeTrashDirectory = QDir(homeDataDirectory.filePath("Trash"));
if (homeTrashDirectory.mkpath(".")) {
return moveToTrashDir(filePath, homeTrashDirectory, error, QStorageInfo());
} else {
error = "Could not ensure that home trash directory exists";
return Trash::Error;
}
} else {
const QDir topdir = QDir(filePathStorage.rootPath());
const QDir topdirTrash = QDir(topdir.filePath(".Trash"));
struct stat trashStat;
if (lstat(topdirTrash.path().toUtf8().data(), &trashStat) == 0) {
// should be a directory, not link, and have sticky bit
if (S_ISDIR(trashStat.st_mode) && !S_ISLNK(trashStat.st_mode) && (trashStat.st_mode & S_ISVTX)) {
const QString subdir = QString::number(getuid());
if (topdirTrash.mkpath(subdir)) {
return moveToTrashDir(filePath, QDir(topdirTrash.filePath(subdir)), error, filePathStorage);
}
}
}
// if we're still here, $topdir/.Trash does not exist or failed some check
QDir topdirUserTrash = QDir(topdir.filePath(QString(".Trash-%1").arg(getuid())));
if (topdirUserTrash.mkpath(".")) {
return moveToTrashDir(filePath, topdirUserTrash, error, filePathStorage);
}
error = "Could not find trash directory for the disk where the file resides";
return Trash::NeedsUserInput;
}
}
#elif defined(Q_OS_WIN)
#include <windows.h>
#include <shellapi.h>
Trash::Result Trash::moveToTrash(const QString &path, QString &error, Trash::Options trashOptions)
{
Q_UNUSED(trashOptions);
SHFILEOPSTRUCTW fileOp;
ZeroMemory(&fileOp, sizeof(fileOp));
fileOp.wFunc = FO_DELETE;
fileOp.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_NOCONFIRMMKDIR | FOF_ALLOWUNDO;
std::wstring wFileName = path.toStdWString();
wFileName.push_back('\0');
wFileName.push_back('\0');
fileOp.pFrom = wFileName.c_str();
int r = SHFileOperation(&fileOp);
if (r != 0) {
// Unfortunately there's no adequate way to get message from SHFileOperation failure
error = QString("SHFileOperation failed with code %1").arg(r);
return Trash::Error;
}
return Trash::Success;
}
#else
Trash::Result Trash::moveToTrash(const QString &path, QString &error, Trash::Options trashOptions)
{
error = "Putting files into trashcan is not supported for this platform yet";
return Trash::Error;
}
#endif