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 g_printerr ("Error getting info: %s\n", 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 g_printerr (_("Error getting writable attributes: %s\n"), error
->message
);
222 g_error_free (error
);
226 g_print (_("Settable attributes:\n"));
227 for (i
= 0; i
< list
->n_infos
; i
++)
229 flags
= attribute_flags_to_string (list
->infos
[i
].flags
);
230 g_print (" %s (%s%s%s)\n",
232 attribute_type_to_string (list
->infos
[i
].type
),
233 (*flags
!= 0)?", ":"", flags
);
237 g_file_attribute_info_list_unref (list
);
239 list
= g_file_query_writable_namespaces (file
, NULL
, &error
);
242 g_printerr ("Error getting writable namespaces: %s\n", error
->message
);
243 g_error_free (error
);
247 if (list
->n_infos
> 0)
249 g_print (_("Writable attribute namespaces:\n"));
250 for (i
= 0; i
< list
->n_infos
; i
++)
252 flags
= attribute_flags_to_string (list
->infos
[i
].flags
);
253 g_print (" %s (%s%s%s)\n",
255 attribute_type_to_string (list
->infos
[i
].type
),
256 (*flags
!= 0)?", ":"", flags
);
260 g_file_attribute_info_list_unref (list
);
266 handle_info (int argc
, char *argv
[], gboolean do_help
)
268 GOptionContext
*context
;
270 GError
*error
= NULL
;
275 g_set_prgname ("gio info");
277 /* Translators: commandline placeholder */
278 param
= g_strdup_printf ("%s...", _("LOCATION"));
279 context
= g_option_context_new (param
);
281 g_option_context_set_help_enabled (context
, FALSE
);
282 g_option_context_set_summary (context
,
283 _("Show information about locations."));
284 g_option_context_set_description (context
,
285 _("gio info is similar to the traditional ls utility, but using GIO\n"
286 "locations instead of local files: for example, you can use something\n"
287 "like smb://server/resource/file.txt as location. File attributes can\n"
288 "be specified with their GIO name, e.g. standard::icon, or just by\n"
289 "namespace, e.g. unix, or by “*”, which matches all attributes"));
290 g_option_context_add_main_entries (context
, entries
, GETTEXT_PACKAGE
);
294 show_help (context
, NULL
);
298 if (!g_option_context_parse (context
, &argc
, &argv
, &error
))
300 show_help (context
, error
->message
);
301 g_error_free (error
);
307 show_help (context
, _("No locations given"));
311 g_option_context_free (context
);
314 for (i
= 1; i
< argc
; i
++)
316 file
= g_file_new_for_commandline_arg (argv
[i
]);
318 res
&= get_writable_info (file
);
320 res
&= query_info (file
);
321 g_object_unref (file
);