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;
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)
191 escaped = g_strescape (root, NULL);
192 root_string = g_strconcat ("[", escaped, "]", NULL);
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);
199 g_print (_("unix mount: %s%s %s %s %s\n"), device,
200 root_string ? root_string : "", mount, fs, options);
203 g_free (root_string);
208 g_unix_mount_free (entry);
213 show_attributes (info);
217 query_info (GFile *file)
219 GFileQueryInfoFlags flags;
226 if (attributes == NULL)
230 if (nofollow_symlinks)
231 flags |= G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS;
235 info = g_file_query_filesystem_info (file, attributes, NULL, &error);
237 info = g_file_query_info (file, attributes, flags, NULL, &error);
241 print_file_error (file, error->message);
242 g_error_free (error);
247 show_attributes (info);
249 show_info (file, info);
251 g_object_unref (info);
257 get_writable_info (GFile *file)
259 GFileAttributeInfoList *list;
269 list = g_file_query_settable_attributes (file, NULL, &error);
272 print_file_error (file, error->message);
273 g_error_free (error);
277 if (list->n_infos > 0)
279 g_print (_("Settable attributes:\n"));
280 for (i = 0; i < list->n_infos; i++)
282 flags = attribute_flags_to_string (list->infos[i].flags);
283 g_print (" %s (%s%s%s)\n",
285 attribute_type_to_string (list->infos[i].type),
286 (*flags != 0)?", ":"", flags);
291 g_file_attribute_info_list_unref (list);
293 list = g_file_query_writable_namespaces (file, NULL, &error);
296 print_file_error (file, error->message);
297 g_error_free (error);
301 if (list->n_infos > 0)
303 g_print (_("Writable attribute namespaces:\n"));
304 for (i = 0; i < list->n_infos; i++)
306 flags = attribute_flags_to_string (list->infos[i].flags);
307 g_print (" %s (%s%s%s)\n",
309 attribute_type_to_string (list->infos[i].type),
310 (*flags != 0)?", ":"", flags);
315 g_file_attribute_info_list_unref (list);
321 handle_info (int argc, char *argv[], gboolean do_help)
323 GOptionContext *context;
325 GError *error = NULL;
330 g_set_prgname ("gio info");
332 /* Translators: commandline placeholder */
333 param = g_strdup_printf ("%s…", _("LOCATION"));
334 context = g_option_context_new (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);
349 show_help (context, NULL);
350 g_option_context_free (context);
354 if (!g_option_context_parse (context, &argc, &argv, &error))
356 show_help (context, error->message);
357 g_error_free (error);
358 g_option_context_free (context);
364 show_help (context, _("No locations given"));
365 g_option_context_free (context);
369 g_option_context_free (context);
372 for (i = 1; i < argc; i++)
374 file = g_file_new_for_commandline_arg (argv[i]);
376 res &= get_writable_info (file);
378 res &= query_info (file);
379 g_object_unref (file);