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