1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright 2018, Red Hat, Inc.
5 * SPDX-License-Identifier: LGPL-2.1-or-later
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
28 #include "gtrashportal.h"
33 #include "gunixfdlist.h"
39 #define HAVE_O_CLOEXEC 1
47 ensure_trash_portal (void)
49 static GXdpTrash *trash = NULL;
51 if (g_once_init_enter (&trash))
53 GDBusConnection *connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
54 GXdpTrash *proxy = NULL;
56 if (connection != NULL)
58 proxy = gxdp_trash_proxy_new_sync (connection, 0,
59 "org.freedesktop.portal.Desktop",
60 "/org/freedesktop/portal/desktop",
62 g_object_unref (connection);
65 g_once_init_leave (&trash, proxy);
72 g_trash_portal_trash_file (GFile *file,
76 GUnixFDList *fd_list = NULL;
79 guint portal_result = 0;
82 proxy = ensure_trash_portal ();
85 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_INITIALIZED,
86 "Trash portal is not available");
90 path = g_file_get_path (file);
92 fd = g_open (path, O_RDWR | O_CLOEXEC | O_NOFOLLOW);
93 if (fd == -1 && errno == EISDIR)
94 /* If it is a directory, fall back to O_PATH.
95 * Remove O_NOFOLLOW since
96 * a) we know it is a directory, not a symlink, and
97 * b) the portal reject this combination
99 fd = g_open (path, O_PATH | O_CLOEXEC | O_RDONLY);
105 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
106 "Failed to open %s", path);
110 #ifndef HAVE_O_CLOEXEC
111 fcntl (fd, F_SETFD, FD_CLOEXEC);
114 fd_list = g_unix_fd_list_new ();
115 fd_in = g_unix_fd_list_append (fd_list, fd, error);
121 ret = gxdp_trash_call_trash_file_sync (proxy,
122 g_variant_new_handle (fd_in),
129 if (ret && portal_result != 1)
131 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Trash portal failed on %s", path);
136 g_clear_object (&fd_list);