kdbus: Fixup signal subscription
[platform/upstream/glib.git] / gio / gio-tool.c
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 #include <locale.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <errno.h>
31
32 #include "gio-tool.h"
33 #include "glib/glib-private.h"
34
35 void
36 print_error (const gchar *format, ...)
37 {
38   gchar *message;
39   va_list args;
40
41   va_start (args, format);
42   message = g_strdup_vprintf (format, args);
43   va_end (args);
44
45   g_printerr ("gio: %s\n", message);
46   g_free (message);
47 }
48
49 void
50 print_file_error (GFile *file, const gchar *message)
51 {
52   gchar *uri;
53
54   uri = g_file_get_uri (file);
55   print_error ("%s: %s", uri, message);
56   g_free (uri);
57 }
58
59 void
60 show_help (GOptionContext *context, const char *message)
61 {
62   char *help;
63
64   if (message)
65     g_printerr ("gio: %s\n\n", message);
66
67   help = g_option_context_get_help (context, TRUE, NULL);
68   g_printerr ("%s", help);
69   g_free (help);
70 }
71
72 const char *
73 file_type_to_string (GFileType type)
74 {
75   switch (type)
76     {
77     case G_FILE_TYPE_UNKNOWN:
78       return "unknown";
79     case G_FILE_TYPE_REGULAR:
80       return "regular";
81     case G_FILE_TYPE_DIRECTORY:
82       return "directory";
83     case G_FILE_TYPE_SYMBOLIC_LINK:
84       return "symlink";
85     case G_FILE_TYPE_SPECIAL:
86       return "special";
87     case G_FILE_TYPE_SHORTCUT:
88       return "shortcut";
89     case G_FILE_TYPE_MOUNTABLE:
90       return "mountable";
91     default:
92       return "invalid type";
93     }
94 }
95
96 const char *
97 attribute_type_to_string (GFileAttributeType type)
98 {
99   switch (type)
100     {
101     case G_FILE_ATTRIBUTE_TYPE_INVALID:
102       return "invalid";
103     case G_FILE_ATTRIBUTE_TYPE_STRING:
104       return "string";
105     case G_FILE_ATTRIBUTE_TYPE_BYTE_STRING:
106       return "bytestring";
107     case G_FILE_ATTRIBUTE_TYPE_BOOLEAN:
108       return "boolean";
109     case G_FILE_ATTRIBUTE_TYPE_UINT32:
110       return "uint32";
111     case G_FILE_ATTRIBUTE_TYPE_INT32:
112       return "int32";
113     case G_FILE_ATTRIBUTE_TYPE_UINT64:
114       return "uint64";
115     case G_FILE_ATTRIBUTE_TYPE_INT64:
116       return "int64";
117     case G_FILE_ATTRIBUTE_TYPE_OBJECT:
118       return "object";
119     default:
120       return "unknown type";
121     }
122 }
123
124 GFileAttributeType
125 attribute_type_from_string (const char *str)
126 {
127   if (strcmp (str, "string") == 0)
128     return G_FILE_ATTRIBUTE_TYPE_STRING;
129   if (strcmp (str, "stringv") == 0)
130     return G_FILE_ATTRIBUTE_TYPE_STRINGV;
131   if (strcmp (str, "bytestring") == 0)
132     return G_FILE_ATTRIBUTE_TYPE_BYTE_STRING;
133   if (strcmp (str, "boolean") == 0)
134     return G_FILE_ATTRIBUTE_TYPE_BOOLEAN;
135   if (strcmp (str, "uint32") == 0)
136     return G_FILE_ATTRIBUTE_TYPE_UINT32;
137   if (strcmp (str, "int32") == 0)
138     return G_FILE_ATTRIBUTE_TYPE_INT32;
139   if (strcmp (str, "uint64") == 0)
140     return G_FILE_ATTRIBUTE_TYPE_UINT64;
141   if (strcmp (str, "int64") == 0)
142     return G_FILE_ATTRIBUTE_TYPE_INT64;
143   if (strcmp (str, "object") == 0)
144     return G_FILE_ATTRIBUTE_TYPE_OBJECT;
145   if (strcmp (str, "unset") == 0)
146     return G_FILE_ATTRIBUTE_TYPE_INVALID;
147   return -1;
148 }
149
150 char *
151 attribute_flags_to_string (GFileAttributeInfoFlags flags)
152 {
153   GString *s;
154   gsize i;
155   gboolean first;
156   struct {
157     guint32 mask;
158     char *descr;
159   } flag_descr[] = {
160     {
161       G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE,
162       N_("Copy with file")
163     },
164     {
165       G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED,
166       N_("Keep with file when moved")
167     }
168   };
169
170   first = TRUE;
171
172   s = g_string_new ("");
173   for (i = 0; i < G_N_ELEMENTS (flag_descr); i++)
174     {
175       if (flags & flag_descr[i].mask)
176         {
177           if (!first)
178             g_string_append (s, ", ");
179           g_string_append (s, gettext (flag_descr[i].descr));
180           first = FALSE;
181         }
182     }
183
184   return g_string_free (s, FALSE);
185 }
186
187 gboolean
188 file_is_dir (GFile *file)
189 {
190   GFileInfo *info;
191   gboolean res;
192
193   info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, 0, NULL, NULL);
194   res = info && g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY;
195   if (info)
196     g_object_unref (info);
197   return res;
198 }
199
200
201 static int
202 handle_version (int argc, char *argv[], gboolean do_help)
203 {
204   if (do_help || argc > 1)
205     {
206       if (!do_help)
207         g_printerr ("gio: %s\n\n", _("“version” takes no arguments"));
208
209       g_printerr ("%s\n", _("Usage:"));
210       g_printerr ("  gio version\n");
211       g_printerr ("\n");
212       g_printerr ("%s\n", _("Print version information and exit."));
213
214       return do_help ? 0 : 2;
215     }
216
217   g_print ("%d.%d.%d\n", glib_major_version, glib_minor_version, glib_micro_version);
218
219   return 0;
220 }
221
222 static void
223 usage (void)
224 {
225   g_printerr ("%s\n", _("Usage:"));
226   g_printerr ("  gio %s %s\n", _("COMMAND"), _("[ARGS…]"));
227   g_printerr ("\n");
228   g_printerr ("%s\n", _("Commands:"));
229   g_printerr ("  help     %s\n", _("Print help"));
230   g_printerr ("  version  %s\n", _("Print version"));
231   g_printerr ("  cat      %s\n", _("Concatenate files to standard output"));
232   g_printerr ("  copy     %s\n", _("Copy one or more files"));
233   g_printerr ("  info     %s\n", _("Show information about locations"));
234   g_printerr ("  launch   %s\n", _("Launch an application from a desktop file"));
235   g_printerr ("  list     %s\n", _("List the contents of locations"));
236   g_printerr ("  mime     %s\n", _("Get or set the handler for a mimetype"));
237   g_printerr ("  mkdir    %s\n", _("Create directories"));
238   g_printerr ("  monitor  %s\n", _("Monitor files and directories for changes"));
239   g_printerr ("  mount    %s\n", _("Mount or unmount the locations"));
240   g_printerr ("  move     %s\n", _("Move one or more files"));
241   g_printerr ("  open     %s\n", _("Open files with the default application"));
242   g_printerr ("  rename   %s\n", _("Rename a file"));
243   g_printerr ("  remove   %s\n", _("Delete one or more files"));
244   g_printerr ("  save     %s\n", _("Read from standard input and save"));
245   g_printerr ("  set      %s\n", _("Set a file attribute"));
246   g_printerr ("  trash    %s\n", _("Move files or directories to the trash"));
247   g_printerr ("  tree     %s\n", _("Lists the contents of locations in a tree"));
248   g_printerr ("\n");
249   g_printerr (_("Use %s to get detailed help.\n"), "“gio help COMMAND”");
250   exit (1);
251 }
252
253 int
254 main (int argc, char **argv)
255 {
256   const char *command;
257   gboolean do_help;
258
259 #ifdef G_OS_WIN32
260   gchar *localedir;
261 #endif
262
263   setlocale (LC_ALL, GLIB_DEFAULT_LOCALE);
264   textdomain (GETTEXT_PACKAGE);
265
266 #ifdef G_OS_WIN32
267   localedir = _glib_get_locale_dir ();
268   bindtextdomain (GETTEXT_PACKAGE, localedir);
269   g_free (localedir);
270 #else
271   bindtextdomain (GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
272 #endif
273
274 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
275   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
276 #endif
277
278   if (argc < 2)
279     {
280       usage ();
281       return 1;
282     }
283
284   command = argv[1];
285   argc -= 1;
286   argv += 1;
287
288   do_help = FALSE;
289   if (g_str_equal (command, "help"))
290     {
291       if (argc == 1)
292         {
293           usage ();
294           return 0;
295         }
296       else
297         {
298           command = argv[1];
299           do_help = TRUE;
300         }
301     }
302   else if (g_str_equal (command, "--help"))
303     {
304       usage ();
305       return 0;
306     }
307   else if (g_str_equal (command, "--version"))
308     command = "version";
309
310   if (g_str_equal (command, "version"))
311     return handle_version (argc, argv, do_help);
312   else if (g_str_equal (command, "cat"))
313     return handle_cat (argc, argv, do_help);
314   else if (g_str_equal (command, "copy"))
315     return handle_copy (argc, argv, do_help);
316   else if (g_str_equal (command, "info"))
317     return handle_info (argc, argv, do_help);
318   else if (g_str_equal (command, "launch"))
319     return handle_launch (argc, argv, do_help);
320   else if (g_str_equal (command, "list"))
321     return handle_list (argc, argv, do_help);
322   else if (g_str_equal (command, "mime"))
323     return handle_mime (argc, argv, do_help);
324   else if (g_str_equal (command, "mkdir"))
325     return handle_mkdir (argc, argv, do_help);
326   else if (g_str_equal (command, "monitor"))
327     return handle_monitor (argc, argv, do_help);
328   else if (g_str_equal (command, "mount"))
329     return handle_mount (argc, argv, do_help);
330   else if (g_str_equal (command, "move"))
331     return handle_move (argc, argv, do_help);
332   else if (g_str_equal (command, "open"))
333     return handle_open (argc, argv, do_help);
334   else if (g_str_equal (command, "rename"))
335     return handle_rename (argc, argv, do_help);
336   else if (g_str_equal (command, "remove"))
337     return handle_remove (argc, argv, do_help);
338   else if (g_str_equal (command, "save"))
339     return handle_save (argc, argv, do_help);
340   else if (g_str_equal (command, "set"))
341     return handle_set (argc, argv, do_help);
342   else if (g_str_equal (command, "trash"))
343     return handle_trash (argc, argv, do_help);
344   else if (g_str_equal (command, "tree"))
345     return handle_tree (argc, argv, do_help);
346   else
347     usage ();
348
349   return 1;
350 }