Imported Upstream version 2.64.5
[platform/upstream/glib.git] / gio / gio-tool-info.c
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 #ifdef G_OS_UNIX
26 #include <gio/gunixmounts.h>
27 #endif
28
29 #include "gio-tool.h"
30
31 static gboolean writable = FALSE;
32 static gboolean filesystem = FALSE;
33 static char *attributes = NULL;
34 static gboolean nofollow_symlinks = FALSE;
35
36 static const GOptionEntry entries[] = {
37   { "query-writable", 'w', 0, G_OPTION_ARG_NONE, &writable, N_("List writable attributes"), NULL },
38   { "filesystem", 'f', 0, G_OPTION_ARG_NONE, &filesystem, N_("Get file system info"), NULL },
39   { "attributes", 'a', 0, G_OPTION_ARG_STRING, &attributes, N_("The attributes to get"), N_("ATTRIBUTES") },
40   { "nofollow-symlinks", 'n', 0, G_OPTION_ARG_NONE, &nofollow_symlinks, N_("Don’t follow symbolic links"), NULL },
41   { NULL }
42 };
43
44 static char *
45 escape_string (const char *in)
46 {
47   GString *str;
48   static char *hex_digits = "0123456789abcdef";
49   unsigned char c;
50
51
52   str = g_string_new ("");
53
54   while ((c = *in++) != 0)
55     {
56       if (c >= 32 && c <= 126 && c != '\\')
57         g_string_append_c (str, c);
58       else
59         {
60           g_string_append (str, "\\x");
61           g_string_append_c (str, hex_digits[(c >> 4) & 0xf]);
62           g_string_append_c (str, hex_digits[c & 0xf]);
63         }
64     }
65
66   return g_string_free (str, FALSE);
67 }
68
69 static void
70 show_attributes (GFileInfo *info)
71 {
72   char **attributes;
73   char *s;
74   int i;
75
76   attributes = g_file_info_list_attributes (info, NULL);
77
78   g_print (_("attributes:\n"));
79   for (i = 0; attributes[i] != NULL; i++)
80     {
81       /* list the icons in order rather than displaying "GThemedIcon:0x8df7200" */
82       if (strcmp (attributes[i], "standard::icon") == 0 ||
83           strcmp (attributes[i], "standard::symbolic-icon") == 0)
84         {
85           GIcon *icon;
86           int j;
87           const char * const *names = NULL;
88
89           if (strcmp (attributes[i], "standard::symbolic-icon") == 0)
90             icon = g_file_info_get_symbolic_icon (info);
91           else
92             icon = g_file_info_get_icon (info);
93
94           /* only look up names if GThemedIcon */
95           if (G_IS_THEMED_ICON(icon))
96             {
97               names = g_themed_icon_get_names (G_THEMED_ICON (icon));
98               g_print ("  %s: ", attributes[i]);
99               for (j = 0; names[j] != NULL; j++)
100                 g_print ("%s%s", names[j], (names[j+1] == NULL)?"":", ");
101               g_print ("\n");
102             }
103           else
104             {
105               s = g_file_info_get_attribute_as_string (info, attributes[i]);
106               g_print ("  %s: %s\n", attributes[i], s);
107               g_free (s);
108             }
109         }
110       else
111         {
112           s = g_file_info_get_attribute_as_string (info, attributes[i]);
113           g_print ("  %s: %s\n", attributes[i], s);
114           g_free (s);
115         }
116     }
117   g_strfreev (attributes);
118 }
119
120 static void
121 show_info (GFile *file, GFileInfo *info)
122 {
123   const char *name, *type;
124   char *escaped, *uri;
125   goffset size;
126   const char *path;
127 #ifdef G_OS_UNIX
128   GUnixMountEntry *entry;
129 #endif
130
131   name = g_file_info_get_display_name (info);
132   if (name)
133     /* Translators: This is a noun and represents and attribute of a file */
134     g_print (_("display name: %s\n"), name);
135
136   name = g_file_info_get_edit_name (info);
137   if (name)
138     /* Translators: This is a noun and represents and attribute of a file */
139     g_print (_("edit name: %s\n"), name);
140
141   name = g_file_info_get_name (info);
142   if (name)
143     {
144       escaped = escape_string (name);
145       g_print (_("name: %s\n"), escaped);
146       g_free (escaped);
147     }
148
149   if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_TYPE))
150     {
151       type = file_type_to_string (g_file_info_get_file_type (info));
152       g_print (_("type: %s\n"), type);
153     }
154
155   if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
156     {
157       size = g_file_info_get_size (info);
158       g_print (_("size: "));
159       g_print (" %"G_GUINT64_FORMAT"\n", (guint64)size);
160     }
161
162   if (g_file_info_get_is_hidden (info))
163     g_print (_("hidden\n"));
164
165   uri = g_file_get_uri (file);
166   g_print (_("uri: %s\n"), uri);
167   g_free (uri);
168
169   path = g_file_peek_path (file);
170   if (path)
171     {
172       g_print (_("local path: %s\n"), path);
173
174 #ifdef G_OS_UNIX
175       entry = g_unix_mount_at (path, NULL);
176       if (entry == NULL)
177         entry = g_unix_mount_for (path, NULL);
178       if (entry != NULL)
179         {
180           gchar *device;
181           const gchar *root;
182           gchar *root_string = NULL;
183           gchar *mount;
184           gchar *fs;
185           gchar *options;
186
187           device = g_strescape (g_unix_mount_get_device_path (entry), NULL);
188           root = g_unix_mount_get_root_path (entry);
189           if (root != NULL && g_strcmp0 (root, "/") != 0)
190             {
191               escaped = g_strescape (root, NULL);
192               root_string = g_strconcat ("[", escaped, "]", NULL);
193               g_free (escaped);
194             }
195           mount = g_strescape (g_unix_mount_get_mount_path (entry), NULL);
196           fs = g_strescape (g_unix_mount_get_fs_type (entry), NULL);
197           options = g_strescape (g_unix_mount_get_options (entry), NULL);
198
199           g_print (_("unix mount: %s%s %s %s %s\n"), device,
200                    root_string ? root_string : "", mount, fs, options);
201
202           g_free (device);
203           g_free (root_string);
204           g_free (mount);
205           g_free (fs);
206           g_free (options);
207
208           g_unix_mount_free (entry);
209         }
210 #endif
211     }
212
213   show_attributes (info);
214 }
215
216 static gboolean
217 query_info (GFile *file)
218 {
219   GFileQueryInfoFlags flags;
220   GFileInfo *info;
221   GError *error;
222
223   if (file == NULL)
224     return FALSE;
225
226   if (attributes == NULL)
227     attributes = "*";
228
229   flags = 0;
230   if (nofollow_symlinks)
231     flags |= G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS;
232
233   error = NULL;
234   if (filesystem)
235     info = g_file_query_filesystem_info (file, attributes, NULL, &error);
236   else
237     info = g_file_query_info (file, attributes, flags, NULL, &error);
238
239   if (info == NULL)
240     {
241       print_file_error (file, error->message);
242       g_error_free (error);
243       return FALSE;
244     }
245
246   if (filesystem)
247     show_attributes (info);
248   else
249     show_info (file, info);
250
251   g_object_unref (info);
252
253   return TRUE;
254 }
255
256 static gboolean
257 get_writable_info (GFile *file)
258 {
259   GFileAttributeInfoList *list;
260   GError *error;
261   int i;
262   char *flags;
263
264   if (file == NULL)
265     return FALSE;
266
267   error = NULL;
268
269   list = g_file_query_settable_attributes (file, NULL, &error);
270   if (list == NULL)
271     {
272       print_file_error (file, error->message);
273       g_error_free (error);
274       return FALSE;
275     }
276
277   if (list->n_infos > 0)
278     {
279       g_print (_("Settable attributes:\n"));
280       for (i = 0; i < list->n_infos; i++)
281         {
282           flags = attribute_flags_to_string (list->infos[i].flags);
283           g_print (" %s (%s%s%s)\n",
284                    list->infos[i].name,
285                    attribute_type_to_string (list->infos[i].type),
286                    (*flags != 0)?", ":"", flags);
287           g_free (flags);
288         }
289     }
290
291   g_file_attribute_info_list_unref (list);
292
293   list = g_file_query_writable_namespaces (file, NULL, &error);
294   if (list == NULL)
295     {
296       print_file_error (file, error->message);
297       g_error_free (error);
298       return FALSE;
299     }
300
301   if (list->n_infos > 0)
302     {
303       g_print (_("Writable attribute namespaces:\n"));
304       for (i = 0; i < list->n_infos; i++)
305         {
306           flags = attribute_flags_to_string (list->infos[i].flags);
307           g_print (" %s (%s%s%s)\n",
308                    list->infos[i].name,
309                    attribute_type_to_string (list->infos[i].type),
310                    (*flags != 0)?", ":"", flags);
311           g_free (flags);
312         }
313     }
314
315   g_file_attribute_info_list_unref (list);
316
317   return TRUE;
318 }
319
320 int
321 handle_info (int argc, char *argv[], gboolean do_help)
322 {
323   GOptionContext *context;
324   gchar *param;
325   GError *error = NULL;
326   gboolean res;
327   gint i;
328   GFile *file;
329
330   g_set_prgname ("gio info");
331
332   /* Translators: commandline placeholder */
333   param = g_strdup_printf ("%s…", _("LOCATION"));
334   context = g_option_context_new (param);
335   g_free (param);
336   g_option_context_set_help_enabled (context, FALSE);
337   g_option_context_set_summary (context,
338       _("Show information about locations."));
339   g_option_context_set_description (context,
340       _("gio info is similar to the traditional ls utility, but using GIO\n"
341         "locations instead of local files: for example, you can use something\n"
342         "like smb://server/resource/file.txt as location. File attributes can\n"
343         "be specified with their GIO name, e.g. standard::icon, or just by\n"
344         "namespace, e.g. unix, or by “*”, which matches all attributes"));
345   g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
346
347   if (do_help)
348     {
349       show_help (context, NULL);
350       g_option_context_free (context);
351       return 0;
352     }
353
354   if (!g_option_context_parse (context, &argc, &argv, &error))
355     {
356       show_help (context, error->message);
357       g_error_free (error);
358       g_option_context_free (context);
359       return 1;
360     }
361
362   if (argc < 2)
363     {
364       show_help (context, _("No locations given"));
365       g_option_context_free (context);
366       return 1;
367     }
368
369   g_option_context_free (context);
370
371   res = TRUE;
372   for (i = 1; i < argc; i++)
373     {
374       file = g_file_new_for_commandline_arg (argv[i]);
375       if (writable)
376         res &= get_writable_info (file);
377       else
378         res &= query_info (file);
379       g_object_unref (file);
380     }
381
382   return res ? 0 : 2;
383 }