Merge branch 'gbytes-compare-docs' into 'master'
[glib.git] / gio / gio-tool-tree.c
blobe63752edb0a629d77b9fb5ededc31133eab03946
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 show_hidden = FALSE;
29 static gboolean follow_symlinks = FALSE;
31 static const GOptionEntry entries[] = {
32 { "hidden", 'h', 0, G_OPTION_ARG_NONE, &show_hidden, N_("Show hidden files"), NULL },
33 { "follow-symlinks", 'l', 0, G_OPTION_ARG_NONE, &follow_symlinks, N_("Follow symbolic links, mounts and shortcuts"), NULL },
34 { NULL }
37 static gint
38 sort_info_by_name (GFileInfo *a, GFileInfo *b)
40 const char *na;
41 const char *nb;
43 na = g_file_info_get_name (a);
44 nb = g_file_info_get_name (b);
46 if (na == NULL)
47 na = "";
48 if (nb == NULL)
49 nb = "";
51 return strcmp (na, nb);
54 static void
55 do_tree (GFile *f, int level, guint64 pattern)
57 GFileEnumerator *enumerator;
58 GError *error = NULL;
59 unsigned int n;
60 GFileInfo *info;
62 info = g_file_query_info (f,
63 G_FILE_ATTRIBUTE_STANDARD_TYPE ","
64 G_FILE_ATTRIBUTE_STANDARD_TARGET_URI,
66 NULL, NULL);
67 if (info != NULL)
69 if (g_file_info_get_attribute_uint32 (info, G_FILE_ATTRIBUTE_STANDARD_TYPE) == G_FILE_TYPE_MOUNTABLE)
71 /* don't process mountables; we avoid these by getting the target_uri below */
72 g_object_unref (info);
73 return;
75 g_object_unref (info);
78 enumerator = g_file_enumerate_children (f,
79 G_FILE_ATTRIBUTE_STANDARD_NAME ","
80 G_FILE_ATTRIBUTE_STANDARD_TYPE ","
81 G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN ","
82 G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK ","
83 G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET ","
84 G_FILE_ATTRIBUTE_STANDARD_TARGET_URI,
86 NULL,
87 &error);
88 if (enumerator != NULL)
90 GList *l;
91 GList *info_list;
93 info_list = NULL;
94 while ((info = g_file_enumerator_next_file (enumerator, NULL, NULL)) != NULL)
96 if (g_file_info_get_is_hidden (info) && !show_hidden)
98 g_object_unref (info);
100 else
102 info_list = g_list_prepend (info_list, info);
105 g_file_enumerator_close (enumerator, NULL, NULL);
107 info_list = g_list_sort (info_list, (GCompareFunc) sort_info_by_name);
109 for (l = info_list; l != NULL; l = l->next)
111 const char *name;
112 const char *target_uri;
113 GFileType type;
114 gboolean is_last_item;
116 info = l->data;
117 is_last_item = (l->next == NULL);
119 name = g_file_info_get_name (info);
120 type = g_file_info_get_attribute_uint32 (info, G_FILE_ATTRIBUTE_STANDARD_TYPE);
121 if (name != NULL)
124 for (n = 0; n < level; n++)
126 if (pattern & (1<<n))
128 g_print ("| ");
130 else
132 g_print (" ");
136 if (is_last_item)
138 g_print ("`-- %s", name);
140 else
142 g_print ("|-- %s", name);
145 target_uri = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_TARGET_URI);
146 if (target_uri != NULL)
148 g_print (" -> %s", target_uri);
150 else
152 if (g_file_info_get_is_symlink (info))
154 const char *target;
155 target = g_file_info_get_symlink_target (info);
156 g_print (" -> %s", target);
160 g_print ("\n");
162 if ((type & G_FILE_TYPE_DIRECTORY) &&
163 (follow_symlinks || !g_file_info_get_is_symlink (info)))
165 guint64 new_pattern;
166 GFile *child;
168 if (is_last_item)
169 new_pattern = pattern;
170 else
171 new_pattern = pattern | (1<<level);
173 child = NULL;
174 if (target_uri != NULL)
176 if (follow_symlinks)
177 child = g_file_new_for_uri (target_uri);
179 else
181 child = g_file_get_child (f, name);
184 if (child != NULL)
186 do_tree (child, level + 1, new_pattern);
187 g_object_unref (child);
191 g_object_unref (info);
193 g_list_free (info_list);
195 else
197 for (n = 0; n < level; n++)
199 if (pattern & (1<<n))
201 g_print ("| ");
203 else
205 g_print (" ");
209 g_print (" [%s]\n", error->message);
211 g_error_free (error);
215 static void
216 tree (GFile *f)
218 char *uri;
220 uri = g_file_get_uri (f);
221 g_print ("%s\n", uri);
222 g_free (uri);
224 do_tree (f, 0, 0);
228 handle_tree (int argc, char *argv[], gboolean do_help)
230 GOptionContext *context;
231 GError *error = NULL;
232 GFile *file;
233 gchar *param;
234 int i;
236 g_set_prgname ("gio tree");
238 /* Translators: commandline placeholder */
239 param = g_strdup_printf ("[%s…]", _("LOCATION"));
240 context = g_option_context_new (param);
241 g_free (param);
242 g_option_context_set_help_enabled (context, FALSE);
243 g_option_context_set_summary (context,
244 _("List contents of directories in a tree-like format."));
245 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
247 if (do_help)
249 show_help (context, NULL);
250 g_option_context_free (context);
251 return 0;
254 g_option_context_parse (context, &argc, &argv, &error);
256 if (error != NULL)
258 show_help (context, error->message);
259 g_error_free (error);
260 g_option_context_free (context);
261 return 1;
264 g_option_context_free (context);
266 if (argc > 1)
268 for (i = 1; i < argc; i++)
270 file = g_file_new_for_commandline_arg (argv[i]);
271 tree (file);
272 g_object_unref (file);
275 else
277 char *cwd;
279 cwd = g_get_current_dir ();
280 file = g_file_new_for_path (cwd);
281 g_free (cwd);
282 tree (file);
283 g_object_unref (file);
286 return 0;