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 of the licence, 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>
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 },
42 escape_string (const char *in)
45 static char *hex_digits = "0123456789abcdef";
49 str = g_string_new ("");
51 while ((c = *in++) != 0)
53 if (c >= 32 && c <= 126 && c != '\\')
54 g_string_append_c (str, c);
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);
67 show_attributes (GFileInfo *info)
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)
84 const char * const *names = NULL;
86 if (strcmp (attributes[i], "standard::symbolic-icon") == 0)
87 icon = g_file_info_get_symbolic_icon (info);
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)?"":", ");
102 s = g_file_info_get_attribute_as_string (info, attributes[i]);
103 g_print (" %s: %s\n", attributes[i], s);
109 s = g_file_info_get_attribute_as_string (info, attributes[i]);
110 g_print (" %s: %s\n", attributes[i], s);
114 g_strfreev (attributes);
118 show_info (GFile *file, GFileInfo *info)
120 const char *name, *type;
124 name = g_file_info_get_display_name (info);
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);
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);
137 escaped = escape_string (name);
138 g_print (_("name: %s\n"), 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);
162 show_attributes (info);
166 query_info (GFile *file)
168 GFileQueryInfoFlags flags;
175 if (attributes == NULL)
179 if (nofollow_symlinks)
180 flags |= G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS;
184 info = g_file_query_filesystem_info (file, attributes, NULL, &error);
186 info = g_file_query_info (file, attributes, flags, NULL, &error);
190 print_file_error (file, error->message);
191 g_error_free (error);
196 show_attributes (info);
198 show_info (file, info);
200 g_object_unref (info);
206 get_writable_info (GFile *file)
208 GFileAttributeInfoList *list;
218 list = g_file_query_settable_attributes (file, NULL, &error);
221 print_file_error (file, error->message);
222 g_error_free (error);
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",
234 attribute_type_to_string (list->infos[i].type),
235 (*flags != 0)?", ":"", flags);
240 g_file_attribute_info_list_unref (list);
242 list = g_file_query_writable_namespaces (file, NULL, &error);
245 print_file_error (file, error->message);
246 g_error_free (error);
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",
258 attribute_type_to_string (list->infos[i].type),
259 (*flags != 0)?", ":"", flags);
264 g_file_attribute_info_list_unref (list);
270 handle_info (int argc, char *argv[], gboolean do_help)
272 GOptionContext *context;
274 GError *error = NULL;
279 g_set_prgname ("gio info");
281 /* Translators: commandline placeholder */
282 param = g_strdup_printf ("%s...", _("LOCATION"));
283 context = g_option_context_new (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);
298 show_help (context, NULL);
299 g_option_context_free (context);
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);
313 show_help (context, _("No locations given"));
314 g_option_context_free (context);
318 g_option_context_free (context);
321 for (i = 1; i < argc; i++)
323 file = g_file_new_for_commandline_arg (argv[i]);
325 res &= get_writable_info (file);
327 res &= query_info (file);
328 g_object_unref (file);