meson: Simplify the use of built tools
[glib.git] / gio / gio-tool-info.c
blobc446d60e8b1abc842607cd109a4cdaa12110b0d9
1 /*
2 * Copyright 2015 Red Hat, Inc.
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.
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.
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/>.
17 * Author: Matthias Clasen <mclasen@redhat.com>
20 #include "config.h"
22 #include <gio/gio.h>
23 #include <gi18n.h>
25 #include "gio-tool.h"
28 static gboolean writable = FALSE;
29 static gboolean filesystem = FALSE;
30 static char *attributes = NULL;
31 static gboolean nofollow_symlinks = FALSE;
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 }
41 static char *
42 escape_string (const char *in)
44 GString *str;
45 static char *hex_digits = "0123456789abcdef";
46 unsigned char c;
49 str = g_string_new ("");
51 while ((c = *in++) != 0)
53 if (c >= 32 && c <= 126 && c != '\\')
54 g_string_append_c (str, c);
55 else
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]);
63 return g_string_free (str, FALSE);
66 static void
67 show_attributes (GFileInfo *info)
69 char **attributes;
70 char *s;
71 int i;
73 attributes = g_file_info_list_attributes (info, NULL);
75 g_print (_("attributes:\n"));
76 for (i = 0; attributes[i] != NULL; i++)
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)
82 GIcon *icon;
83 int j;
84 const char * const *names = NULL;
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);
91 /* only look up names if GThemedIcon */
92 if (G_IS_THEMED_ICON(icon))
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");
100 else
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);
107 else
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);
114 g_strfreev (attributes);
117 static void
118 show_info (GFile *file, GFileInfo *info)
120 const char *name, *type;
121 char *escaped, *uri;
122 goffset size;
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);
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);
134 name = g_file_info_get_name (info);
135 if (name)
137 escaped = escape_string (name);
138 g_print (_("name: %s\n"), escaped);
139 g_free (escaped);
142 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_TYPE))
144 type = file_type_to_string (g_file_info_get_file_type (info));
145 g_print (_("type: %s\n"), type);
148 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
150 size = g_file_info_get_size (info);
151 g_print (_("size: "));
152 g_print (" %"G_GUINT64_FORMAT"\n", (guint64)size);
155 if (g_file_info_get_is_hidden (info))
156 g_print (_("hidden\n"));
158 uri = g_file_get_uri (file);
159 g_print (_("uri: %s\n"), uri);
160 g_free (uri);
162 show_attributes (info);
165 static gboolean
166 query_info (GFile *file)
168 GFileQueryInfoFlags flags;
169 GFileInfo *info;
170 GError *error;
172 if (file == NULL)
173 return FALSE;
175 if (attributes == NULL)
176 attributes = "*";
178 flags = 0;
179 if (nofollow_symlinks)
180 flags |= G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS;
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);
188 if (info == NULL)
190 print_file_error (file, error->message);
191 g_error_free (error);
192 return FALSE;
195 if (filesystem)
196 show_attributes (info);
197 else
198 show_info (file, info);
200 g_object_unref (info);
202 return TRUE;
205 static gboolean
206 get_writable_info (GFile *file)
208 GFileAttributeInfoList *list;
209 GError *error;
210 int i;
211 char *flags;
213 if (file == NULL)
214 return FALSE;
216 error = NULL;
218 list = g_file_query_settable_attributes (file, NULL, &error);
219 if (list == NULL)
221 print_file_error (file, error->message);
222 g_error_free (error);
223 return FALSE;
226 if (list->n_infos > 0)
228 g_print (_("Settable attributes:\n"));
229 for (i = 0; i < list->n_infos; i++)
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);
240 g_file_attribute_info_list_unref (list);
242 list = g_file_query_writable_namespaces (file, NULL, &error);
243 if (list == NULL)
245 print_file_error (file, error->message);
246 g_error_free (error);
247 return FALSE;
250 if (list->n_infos > 0)
252 g_print (_("Writable attribute namespaces:\n"));
253 for (i = 0; i < list->n_infos; i++)
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);
264 g_file_attribute_info_list_unref (list);
266 return TRUE;
270 handle_info (int argc, char *argv[], gboolean do_help)
272 GOptionContext *context;
273 gchar *param;
274 GError *error = NULL;
275 gboolean res;
276 gint i;
277 GFile *file;
279 g_set_prgname ("gio info");
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);
296 if (do_help)
298 show_help (context, NULL);
299 g_option_context_free (context);
300 return 0;
303 if (!g_option_context_parse (context, &argc, &argv, &error))
305 show_help (context, error->message);
306 g_error_free (error);
307 g_option_context_free (context);
308 return 1;
311 if (argc < 2)
313 show_help (context, _("No locations given"));
314 g_option_context_free (context);
315 return 1;
318 g_option_context_free (context);
320 res = TRUE;
321 for (i = 1; i < argc; i++)
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);
331 return res ? 0 : 2;