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>
26 #include <gio/gunixmounts.h>
31 static gboolean writable = FALSE;
32 static gboolean filesystem = FALSE;
33 static char *attributes = NULL;
34 static gboolean nofollow_symlinks = FALSE;
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 },
45 escape_string (const char *in)
48 static char *hex_digits = "0123456789abcdef";
52 str = g_string_new ("");
54 while ((c = *in++) != 0)
56 if (c >= 32 && c <= 126 && c != '\\')
57 g_string_append_c (str, c);
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]);
66 return g_string_free (str, FALSE);
70 show_attributes (GFileInfo *info)
76 attributes = g_file_info_list_attributes (info, NULL);
78 g_print (_("attributes:\n"));
79 for (i = 0; attributes[i] != NULL; i++)
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)
87 const char * const *names = NULL;
89 if (strcmp (attributes[i], "standard::symbolic-icon") == 0)
90 icon = g_file_info_get_symbolic_icon (info);
92 icon = g_file_info_get_icon (info);
94 /* only look up names if GThemedIcon */
95 if (G_IS_THEMED_ICON(icon))
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)?"":", ");
105 s = g_file_info_get_attribute_as_string (info, attributes[i]);
106 g_print (" %s: %s\n", attributes[i], s);
112 s = g_file_info_get_attribute_as_string (info, attributes[i]);
113 g_print (" %s: %s\n", attributes[i], s);
117 g_strfreev (attributes);
121 show_info (GFile *file, GFileInfo *info)
123 const char *name, *type;
128 GUnixMountEntry *entry;
131 name = g_file_info_get_display_name (info);
133 /* Translators: This is a noun and represents and attribute of a file */
134 g_print (_("display name: %s\n"), name);
136 name = g_file_info_get_edit_name (info);
138 /* Translators: This is a noun and represents and attribute of a file */
139 g_print (_("edit name: %s\n"), name);
141 name = g_file_info_get_name (info);
144 escaped = escape_string (name);
145 g_print (_("name: %s\n"), escaped);
149 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_TYPE))
151 type = file_type_to_string (g_file_info_get_file_type (info));
152 g_print (_("type: %s\n"), type);
155 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
157 size = g_file_info_get_size (info);
158 g_print (_("size: "));
159 g_print (" %"G_GUINT64_FORMAT"\n", (guint64)size);
162 if (g_file_info_get_is_hidden (info))
163 g_print (_("hidden\n"));
165 uri = g_file_get_uri (file);
166 g_print (_("uri: %s\n"), uri);
169 path = g_file_peek_path (file);
172 g_print (_("local path: %s\n"), path);
175 entry = g_unix_mount_at (path, NULL);
177 entry = g_unix_mount_for (path, NULL);
182 gchar *root_string = NULL;
185 const gchar *options;
186 gchar *options_string = NULL;
188 device = g_strescape (g_unix_mount_get_device_path (entry), NULL);
189 root = g_unix_mount_get_root_path (entry);
190 if (root != NULL && g_strcmp0 (root, "/") != 0)
192 escaped = g_strescape (root, NULL);
193 root_string = g_strconcat ("[", escaped, "]", NULL);
196 mount = g_strescape (g_unix_mount_get_mount_path (entry), NULL);
197 fs = g_strescape (g_unix_mount_get_fs_type (entry), NULL);
199 options = g_unix_mount_get_options (entry);
202 options_string = g_strescape (options, NULL);
205 g_print (_("unix mount: %s%s %s %s %s\n"), device,
206 root_string ? root_string : "", mount, fs,
207 options_string ? options_string : "");
210 g_free (root_string);
213 g_free (options_string);
215 g_unix_mount_free (entry);
220 show_attributes (info);
224 query_info (GFile *file)
226 GFileQueryInfoFlags flags;
233 if (attributes == NULL)
237 if (nofollow_symlinks)
238 flags |= G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS;
242 info = g_file_query_filesystem_info (file, attributes, NULL, &error);
244 info = g_file_query_info (file, attributes, flags, NULL, &error);
248 print_file_error (file, error->message);
249 g_error_free (error);
254 show_attributes (info);
256 show_info (file, info);
258 g_object_unref (info);
264 get_writable_info (GFile *file)
266 GFileAttributeInfoList *list;
276 list = g_file_query_settable_attributes (file, NULL, &error);
279 print_file_error (file, error->message);
280 g_error_free (error);
284 if (list->n_infos > 0)
286 g_print (_("Settable attributes:\n"));
287 for (i = 0; i < list->n_infos; i++)
289 flags = attribute_flags_to_string (list->infos[i].flags);
290 g_print (" %s (%s%s%s)\n",
292 attribute_type_to_string (list->infos[i].type),
293 (*flags != 0)?", ":"", flags);
298 g_file_attribute_info_list_unref (list);
300 list = g_file_query_writable_namespaces (file, NULL, &error);
303 print_file_error (file, error->message);
304 g_error_free (error);
308 if (list->n_infos > 0)
310 g_print (_("Writable attribute namespaces:\n"));
311 for (i = 0; i < list->n_infos; i++)
313 flags = attribute_flags_to_string (list->infos[i].flags);
314 g_print (" %s (%s%s%s)\n",
316 attribute_type_to_string (list->infos[i].type),
317 (*flags != 0)?", ":"", flags);
322 g_file_attribute_info_list_unref (list);
328 handle_info (int argc, char *argv[], gboolean do_help)
330 GOptionContext *context;
332 GError *error = NULL;
337 g_set_prgname ("gio info");
339 /* Translators: commandline placeholder */
340 param = g_strdup_printf ("%s…", _("LOCATION"));
341 context = g_option_context_new (param);
343 g_option_context_set_help_enabled (context, FALSE);
344 g_option_context_set_summary (context,
345 _("Show information about locations."));
346 g_option_context_set_description (context,
347 _("gio info is similar to the traditional ls utility, but using GIO\n"
348 "locations instead of local files: for example, you can use something\n"
349 "like smb://server/resource/file.txt as location. File attributes can\n"
350 "be specified with their GIO name, e.g. standard::icon, or just by\n"
351 "namespace, e.g. unix, or by “*”, which matches all attributes"));
352 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
356 show_help (context, NULL);
357 g_option_context_free (context);
361 if (!g_option_context_parse (context, &argc, &argv, &error))
363 show_help (context, error->message);
364 g_error_free (error);
365 g_option_context_free (context);
371 show_help (context, _("No locations given"));
372 g_option_context_free (context);
376 g_option_context_free (context);
379 for (i = 1; i < argc; i++)
381 file = g_file_new_for_commandline_arg (argv[i]);
383 res &= get_writable_info (file);
385 res &= query_info (file);
386 g_object_unref (file);