diff options
Diffstat (limited to 'platform/x11/os_x11.cpp')
-rw-r--r-- | platform/x11/os_x11.cpp | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/platform/x11/os_x11.cpp b/platform/x11/os_x11.cpp index 599c0d2278..041666a594 100644 --- a/platform/x11/os_x11.cpp +++ b/platform/x11/os_x11.cpp @@ -35,6 +35,7 @@ #include "servers/physics/physics_server_sw.h" #include "servers/visual/visual_server_raster.h" #include "servers/visual/visual_server_wrap_mt.h" +#include <mntent.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -2175,6 +2176,77 @@ bool OS_X11::is_disable_crash_handler() const { return crash_handler.is_disabled(); } +static String get_mountpoint(const String &p_path) { + struct stat s; + if (stat(p_path.utf8().get_data(), &s)) { + return ""; + } + + dev_t dev = s.st_dev; + FILE *fd = setmntent("/proc/mounts", "r"); + if (!fd) { + return ""; + } + + struct mntent mnt; + char buf[1024]; + size_t buflen = 1024; + while (getmntent_r(fd, &mnt, buf, buflen)) { + if (!stat(mnt.mnt_dir, &s) && s.st_dev == dev) { + endmntent(fd); + return String(mnt.mnt_dir); + } + } + + endmntent(fd); + return ""; +} + +Error OS_X11::move_to_trash(const String &p_path) { + String trashcan = ""; + String mnt = get_mountpoint(p_path); + + if (mnt != "") { + String path(mnt + "/.Trash-" + itos(getuid()) + "/files"); + struct stat s; + if (!stat(path.utf8().get_data(), &s)) { + trashcan = path; + } + } + + if (trashcan == "") { + char *dhome = getenv("XDG_DATA_HOME"); + if (dhome) { + trashcan = String(dhome) + "/Trash/files"; + } + } + + if (trashcan == "") { + char *home = getenv("HOME"); + if (home) { + trashcan = String(home) + "/.local/share/Trash/files"; + } + } + + if (trashcan == "") { + ERR_PRINTS("move_to_trash: Could not determine trashcan location"); + return FAILED; + } + + List<String> args; + args.push_back("-p"); + args.push_back(trashcan); + Error err = execute("/bin/mkdir", args, true); + if (err == OK) { + List<String> args2; + args2.push_back(p_path); + args2.push_back(trashcan); + err = execute("/bin/mv", args2, true); + } + + return err; +} + OS_X11::OS_X11() { #ifdef PULSEAUDIO_ENABLED |