1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright 2018, Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
26 #include "gtrashportal.h"
31 #include "gunixfdlist.h"
37 #define HAVE_O_CLOEXEC 1
45 ensure_trash_portal (void)
47 static GXdpTrash *trash = NULL;
49 if (g_once_init_enter (&trash))
51 GDBusConnection *connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
52 GXdpTrash *proxy = NULL;
54 if (connection != NULL)
56 proxy = gxdp_trash_proxy_new_sync (connection, 0,
57 "org.freedesktop.portal.Desktop",
58 "/org/freedesktop/portal/desktop",
60 g_object_unref (connection);
63 g_once_init_leave (&trash, proxy);
70 g_trash_portal_trash_file (GFile *file,
74 GUnixFDList *fd_list = NULL;
77 guint portal_result = 0;
80 proxy = ensure_trash_portal ();
83 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_INITIALIZED,
84 "Trash portal is not available");
88 path = g_file_get_path (file);
90 fd = g_open (path, O_RDWR | O_CLOEXEC | O_NOFOLLOW);
91 if (fd == -1 && errno == EISDIR)
92 /* If it is a directory, fall back to O_PATH */
93 fd = g_open (path, O_PATH | O_CLOEXEC | O_RDONLY | O_NOFOLLOW);
99 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
100 "Failed to open %s", path);
104 #ifndef HAVE_O_CLOEXEC
105 fcntl (fd, F_SETFD, FD_CLOEXEC);
108 fd_list = g_unix_fd_list_new ();
109 fd_in = g_unix_fd_list_append (fd_list, fd, error);
115 ret = gxdp_trash_call_trash_file_sync (proxy,
116 g_variant_new_handle (fd_in),
123 if (ret && portal_result != 1)
125 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Trash portal failed on %s", path);
130 g_clear_object (&fd_list);