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 parent = FALSE;
31
32 static const GOptionEntry entries[] = {
33 { "parent", 'p', 0, G_OPTION_ARG_NONE, &parent, N_("Create parent directories"), NULL },
34 G_OPTION_ENTRY_NULL
35 };
36
37 int
38 handle_mkdir (int argc, char *argv[], gboolean do_help)
39 {
40 GOptionContext *context;
41 gchar *param;
42 GError *error = NULL;
43 GFile *file;
44 int retval = 0;
45 int i;
46
47 g_set_prgname ("gio mkdir");
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, _("Create directories."));
55 g_option_context_set_description (context,
56 _("gio mkdir is similar to the traditional mkdir utility, but using GIO\n"
57 "locations instead of local files: for example, you can use something\n"
58 "like smb://server/resource/mydir as location."));
59 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
60
61 if (do_help)
62 {
63 show_help (context, NULL);
64 g_option_context_free (context);
65 return 0;
66 }
67
68 if (!g_option_context_parse (context, &argc, &argv, &error))
69 {
70 show_help (context, error->message);
71 g_error_free (error);
72 g_option_context_free (context);
73 return 1;
74 }
75
76 if (argc < 2)
77 {
78 show_help (context, _("No locations given"));
79 g_option_context_free (context);
80 return 1;
81 }
82
83 g_option_context_free (context);
84
85 for (i = 1; i < argc; i++)
86 {
87 file = g_file_new_for_commandline_arg (argv[i]);
88 error = NULL;
89 if (parent)
90 {
91 if (!g_file_make_directory_with_parents (file, NULL, &error))
92 {
93 print_file_error (file, error->message);
94 g_error_free (error);
95 retval = 1;
96 }
97 }
98 else
99 {
100 if (!g_file_make_directory (file, NULL, &error))
101 {
102 print_file_error (file, error->message);
103 g_error_free (error);
104 retval = 1;
105 }
106 g_object_unref (file);
107 }
108 }
109
110 return retval;
111
112 }