1 /*
2 * Copyright 2015 Red Hat, Inc.
3 *
4 * SPDX-License-Identifier: LGPL-2.1-or-later
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 * Author: Matthias Clasen <mclasen@redhat.com>
20 */
21
22 #include "config.h"
23
24 #include <gio/gio.h>
25 #include <gi18n.h>
26
27 #include "gio-tool.h"
28
29
30 static gboolean force = FALSE;
31
32 static const GOptionEntry entries[] = {
33 {"force", 'f', 0, G_OPTION_ARG_NONE, &force, N_("Ignore nonexistent files, never prompt"), NULL},
34 G_OPTION_ENTRY_NULL
35 };
36
37 int
38 handle_remove (int argc, char *argv[], gboolean do_help)
39 {
40 GOptionContext *context;
41 gchar *param;
42 GError *error = NULL;
43 GFile *file;
44 int retval;
45 int i;
46
47 g_set_prgname ("gio remove");
48
49 /* Translators: commandline placeholder */
50 param = g_strdup_printf ("%s…", _("LOCATION"));
51 context = g_option_context_new (param);
52 g_free (param);
53 g_option_context_set_help_enabled (context, FALSE);
54 g_option_context_set_summary (context, _("Delete the given files."));
55 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
56
57 if (do_help)
58 {
59 show_help (context, NULL);
60 g_option_context_free (context);
61 return 0;
62 }
63
64 if (!g_option_context_parse (context, &argc, &argv, &error))
65 {
66 show_help (context, error->message);
67 g_error_free (error);
68 g_option_context_free (context);
69 return 1;
70 }
71
72 if (argc == 1)
73 {
74 show_help (context, _("No locations given"));
75 g_option_context_free (context);
76 return 1;
77 }
78
79 g_option_context_free (context);
80
81 retval = 0;
82 for (i = 1; i < argc; i++)
83 {
84 file = g_file_new_for_commandline_arg (argv[i]);
85 if (!g_file_delete (file, NULL, &error))
86 {
87 if (!force ||
88 !g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
89 {
90 print_file_error (file, error->message);
91 retval = 1;
92 }
93 g_clear_error (&error);
94 }
95 g_object_unref (file);
96 }
97
98 return retval;
99 }