2 * Copyright 2015 Red Hat, Inc.
4 * SPDX-License-Identifier: LGPL-2.1-or-later
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 * Author: Matthias Clasen <mclasen@redhat.com>
30 static gboolean global_force = FALSE;
31 static gboolean empty = FALSE;
32 static gboolean restore = FALSE;
33 static gboolean list = FALSE;
34 static const GOptionEntry entries[] = {
35 { "force", 'f', 0, G_OPTION_ARG_NONE, &global_force, N_("Ignore nonexistent files, never prompt"), NULL },
36 { "empty", 0, 0, G_OPTION_ARG_NONE, &empty, N_("Empty the trash"), NULL },
37 { "list", 0, 0, G_OPTION_ARG_NONE, &list, N_("List files in the trash with their original locations"), NULL },
38 { "restore", 0, 0, G_OPTION_ARG_NONE, &restore, N_("Restore a file from trash to its original location (possibly "
39 "recreating the directory)"), NULL },
44 delete_trash_file (GFile *file, gboolean del_file, gboolean del_children)
48 GFileEnumerator *enumerator;
50 g_return_if_fail (g_file_has_uri_scheme (file, "trash"));
54 enumerator = g_file_enumerate_children (file,
55 G_FILE_ATTRIBUTE_STANDARD_NAME ","
56 G_FILE_ATTRIBUTE_STANDARD_TYPE,
57 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
62 while ((info = g_file_enumerator_next_file (enumerator, NULL, NULL)) != NULL)
64 child = g_file_get_child (file, g_file_info_get_name (info));
66 /* The g_file_delete operation works differently for locations
67 * provided by the trash backend as it prevents modifications of
68 * trashed items. For that reason, it is enough to call
69 * g_file_delete on top-level items only.
71 delete_trash_file (child, TRUE, FALSE);
73 g_object_unref (child);
74 g_object_unref (info);
76 g_file_enumerator_close (enumerator, NULL, NULL);
77 g_object_unref (enumerator);
82 g_file_delete (file, NULL, NULL);
86 restore_trash (GFile *file,
88 GCancellable *cancellable,
91 GFileInfo *info = NULL;
93 GFile *dir_target = NULL;
95 gchar *orig_path = NULL;
96 GError *local_error = NULL;
98 info = g_file_query_info (file, G_FILE_ATTRIBUTE_TRASH_ORIG_PATH, G_FILE_QUERY_INFO_NONE, cancellable, &local_error);
101 g_propagate_error (error, local_error);
105 orig_path = g_file_info_get_attribute_as_string (info, G_FILE_ATTRIBUTE_TRASH_ORIG_PATH);
108 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND, _("Unable to find original path"));
112 target = g_file_new_for_commandline_arg (orig_path);
115 dir_target = g_file_get_parent (target);
118 g_file_make_directory_with_parents (dir_target, cancellable, &local_error);
119 if (g_error_matches (local_error, G_IO_ERROR, G_IO_ERROR_EXISTS))
121 g_clear_error (&local_error);
123 else if (local_error != NULL)
125 g_propagate_prefixed_error (error, local_error, _("Unable to recreate original location: "));
130 if (!g_file_move (file,
132 force ? G_FILE_COPY_OVERWRITE : G_FILE_COPY_NONE,
138 g_propagate_prefixed_error (error, local_error, _("Unable to move file to its original location: "));
144 g_clear_object (&target);
145 g_clear_object (&dir_target);
146 g_clear_object (&info);
151 trash_list (GFile *file,
152 GCancellable *cancellable,
155 GFileEnumerator *enumerator;
157 GError *local_error = NULL;
160 enumerator = g_file_enumerate_children (file,
161 G_FILE_ATTRIBUTE_STANDARD_NAME ","
162 G_FILE_ATTRIBUTE_TRASH_ORIG_PATH,
163 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
168 g_propagate_error (error, local_error);
173 while ((info = g_file_enumerator_next_file (enumerator, cancellable, &local_error)) != NULL)
180 name = g_file_info_get_name (info);
181 child = g_file_get_child (file, name);
182 uri = g_file_get_uri (child);
183 g_object_unref (child);
184 orig_path = g_file_info_get_attribute_as_string (info, G_FILE_ATTRIBUTE_TRASH_ORIG_PATH);
186 g_print ("%s\t%s\n", uri, orig_path);
188 g_object_unref (info);
195 g_propagate_error (error, local_error);
200 if (!g_file_enumerator_close (enumerator, cancellable, &local_error))
202 print_file_error (file, local_error->message);
203 g_clear_error (&local_error);
211 handle_trash (int argc, char *argv[], gboolean do_help)
213 GOptionContext *context;
215 GError *error = NULL;
219 g_set_prgname ("gio trash");
221 /* Translators: commandline placeholder */
222 param = g_strdup_printf ("[%s…]", _("LOCATION"));
223 context = g_option_context_new (param);
225 g_option_context_set_help_enabled (context, FALSE);
226 g_option_context_set_summary (context,
227 _("Move/Restore files or directories to the trash."));
228 g_option_context_set_description (context,
229 _("Note: for --restore switch, if the original location of the trashed file \n"
230 "already exists, it will not be overwritten unless --force is set."));
231 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
235 show_help (context, NULL);
236 g_option_context_free (context);
240 if (!g_option_context_parse (context, &argc, &argv, &error))
242 show_help (context, error->message);
243 g_error_free (error);
244 g_option_context_free (context);
252 for (i = 1; i < argc; i++)
254 file = g_file_new_for_commandline_arg (argv[i]);
258 if (!g_file_has_uri_scheme (file, "trash"))
260 print_file_error (file, _("Location given doesn't start with trash:///"));
263 else if (!restore_trash (file, global_force, NULL, &error))
265 print_file_error (file, error->message);
269 else if (!g_file_trash (file, NULL, &error))
272 !g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
274 print_file_error (file, error->message);
278 g_clear_error (&error);
279 g_object_unref (file);
284 file = g_file_new_for_uri ("trash:");
285 trash_list (file, NULL, &error);
288 print_file_error (file, error->message);
289 g_clear_error (&error);
292 g_object_unref (file);
296 file = g_file_new_for_uri ("trash:");
297 delete_trash_file (file, FALSE, TRUE);
298 g_object_unref (file);
301 if (argc == 1 && !empty && !list)
303 show_help (context, _("No locations given"));
304 g_option_context_free (context);
308 g_option_context_free (context);