Fix Linux `move_to_trash` wrongly reporting files as not found

We can't rely on the error code from `gio` or `kioclient5`, in my
rudimentary testing they return `1` for both missing files and other
situations like not having a Trash can on the mounted volume.

Fixes #79108.

(cherry picked from commit a6e75f3971)
This commit is contained in:
Rémi Verschelde 2023-07-10 15:21:53 +02:00 committed by Yuri Sizov
parent 6ec2d6d7d0
commit 30ab0e2cf4
1 changed files with 11 additions and 23 deletions

View File

@ -954,45 +954,33 @@ static String get_mountpoint(const String &p_path) {
} }
Error OS_LinuxBSD::move_to_trash(const String &p_path) { Error OS_LinuxBSD::move_to_trash(const String &p_path) {
String path = p_path.rstrip("/"); // Strip trailing slash when path points to a directory // We try multiple methods, until we find one that works.
// So we only return on success until we exhausted possibilities.
String path = p_path.rstrip("/"); // Strip trailing slash when path points to a directory.
int err_code; int err_code;
List<String> args; List<String> args;
args.push_back(path); args.push_back(path);
args.push_front("trash"); // The command is `gio trash <file_name>` so we need to add it to args.
args.push_front("trash"); // The command is `gio trash <file_name>` so we add it before the path.
Error result = execute("gio", args, nullptr, &err_code); // For GNOME based machines. Error result = execute("gio", args, nullptr, &err_code); // For GNOME based machines.
if (result == OK) { // The `execute` function has done its job without errors. if (result == OK && err_code == 0) { // Success.
if (!err_code) { // The shell command has been executed without errors.
return OK; return OK;
} else if (err_code == 1) {
ERR_PRINT("move_to_trash: No such file or directory as " + path + ".");
return ERR_FILE_NOT_FOUND;
}
} }
args.pop_front(); args.pop_front();
args.push_front("move"); args.push_front("move");
args.push_back("trash:/"); // The command is `kioclient5 move <file_name> trash:/`. args.push_back("trash:/"); // The command is `kioclient5 move <file_name> trash:/`.
result = execute("kioclient5", args, nullptr, &err_code); // For KDE based machines. result = execute("kioclient5", args, nullptr, &err_code); // For KDE based machines.
if (result == OK) { // The `execute` function has done its job without errors. if (result == OK && err_code == 0) {
if (!err_code) { // The shell command has been executed without errors.
return OK; return OK;
} else if (err_code == 1) {
ERR_PRINT("move_to_trash: No such file or directory as " + path + ".");
return ERR_FILE_NOT_FOUND;
}
} }
args.pop_front(); args.pop_front();
args.pop_back(); args.pop_back();
result = execute("gvfs-trash", args, nullptr, &err_code); // For older Linux machines. result = execute("gvfs-trash", args, nullptr, &err_code); // For older Linux machines.
if (result == OK) { // The `execute` function has done its job without errors. if (result == OK && err_code == 0) {
if (!err_code) { // The shell command has been executed without errors.
return OK; return OK;
} else if (err_code == 1) {
ERR_PRINT("move_to_trash: No such file or directory as " + path + ".");
return ERR_FILE_NOT_FOUND;
}
} }
// If the commands `kioclient5`, `gio` or `gvfs-trash` don't work on the system we do it manually. // If the commands `kioclient5`, `gio` or `gvfs-trash` don't work on the system we do it manually.