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