• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2015 Red Hat, Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
16  *
17  * Author: Matthias Clasen <mclasen@redhat.com>
18  */
19 
20 #include "config.h"
21 
22 #include <gio/gio.h>
23 #include <gi18n.h>
24 
25 #include "gio-tool.h"
26 
27 
28 static gboolean force = FALSE;
29 
30 static const GOptionEntry entries[] = {
31   {"force", 'f', 0, G_OPTION_ARG_NONE, &force, N_("Ignore nonexistent files, never prompt"), NULL},
32   { NULL }
33 };
34 
35 int
handle_remove(int argc,char * argv[],gboolean do_help)36 handle_remove (int argc, char *argv[], gboolean do_help)
37 {
38   GOptionContext *context;
39   gchar *param;
40   GError *error = NULL;
41   GFile *file;
42   int retval;
43   int i;
44 
45   g_set_prgname ("gio remove");
46 
47   /* Translators: commandline placeholder */
48   param = g_strdup_printf ("%s…", _("LOCATION"));
49   context = g_option_context_new (param);
50   g_free (param);
51   g_option_context_set_help_enabled (context, FALSE);
52   g_option_context_set_summary (context, _("Delete the given files."));
53   g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
54 
55   if (do_help)
56     {
57       show_help (context, NULL);
58       g_option_context_free (context);
59       return 0;
60     }
61 
62   if (!g_option_context_parse (context, &argc, &argv, &error))
63     {
64       show_help (context, error->message);
65       g_error_free (error);
66       g_option_context_free (context);
67       return 1;
68     }
69 
70   if (argc == 1)
71     {
72       show_help (context, _("No locations given"));
73       g_option_context_free (context);
74       return 1;
75     }
76 
77   g_option_context_free (context);
78 
79   retval = 0;
80   for (i = 1; i < argc; i++)
81     {
82       file = g_file_new_for_commandline_arg (argv[i]);
83       if (!g_file_delete (file, NULL, &error))
84         {
85           if (!force ||
86               !g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
87             {
88               print_file_error (file, error->message);
89               retval = 1;
90             }
91           g_clear_error (&error);
92         }
93       g_object_unref (file);
94     }
95 
96   return retval;
97 }
98