gliv-1.7
[gliv.git] / src / foreach_file.c
blobcc99a5c49b30798239b6a48acdf99de7804b83f2
1 /*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version 2
5 * of the License, or (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 * See the COPYING file for license information.
18 * Guillaume Chazarain <gfc@altern.org>
21 /************************
22 * A simplified ftw(3). *
23 ************************/
25 #include "gliv.h"
27 #include <sys/types.h> /* dev_t, ino_t */
28 #include <sys/stat.h> /* struct stat, stat() */
29 #include <dirent.h> /* struct dirent, *dir() */
31 /* Used to detect insertions of already present elements. */
32 static gboolean destroyed_node = FALSE;
35 * Directories informations saved into
36 * the AVL to avoid infinite loops.
38 typedef struct {
39 dev_t dev;
40 ino_t ino;
41 } dir_info;
43 /* Called when we insert an element that was already there. */
44 static void value_destroy_notify(gpointer data)
46 g_free(data);
47 destroyed_node = TRUE;
50 /* To build and search the AVL. */
51 static gint cmp_func(const dir_info * dir1, const dir_info * dir2)
53 if (dir1->ino < dir2->ino)
54 return -1;
56 if (dir1->ino > dir2->ino)
57 return 1;
59 if (dir1->dev < dir2->dev)
60 return -1;
62 return dir1->dev > dir2->dev;
65 /* Push directories and handle files. */
66 static GSList *process_dir(GSList * stack, const gchar * dirname,
67 foreach_file_func func)
69 DIR *dir;
70 struct dirent *dir_ent;
71 struct stat st;
72 gchar *name, *fullname;
74 dir = opendir(dirname);
75 if (dir == NULL)
76 return stack;
78 for (dir_ent = readdir(dir); dir_ent != NULL; dir_ent = readdir(dir)) {
79 name = dir_ent->d_name;
81 if (name[0] == '.' &&
82 (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')))
83 /* Skip "." and "..". */
84 continue;
86 fullname = fast_build_filename(dirname, name);
88 if (stat(fullname, &st) < 0)
89 /* Shouldn't happen... */
90 continue;
92 if (S_ISDIR(st.st_mode))
93 stack = g_slist_prepend(stack, fullname);
94 else {
95 (*func) (fullname);
96 g_free(fullname);
100 closedir(dir);
101 return stack;
104 /* Return TRUE if the dir has been correctly inserted. */
105 static gboolean tree_insert_dir(GTree * tree, struct stat *st)
107 dir_info *dir;
109 dir = g_new(dir_info, 1);
111 dir->dev = st->st_dev;
112 dir->ino = st->st_ino;
114 g_tree_insert(tree, dir, NULL);
116 if (destroyed_node) {
117 destroyed_node = FALSE;
118 return FALSE;
121 return TRUE;
124 void foreach_file(const gchar * path, foreach_file_func func)
126 GSList *stack = NULL;
127 GTree *tree;
128 gchar *current;
129 struct stat st;
131 if (stat(path, &st) < 0)
132 /* The path is not usable. */
133 return;
135 if (S_ISDIR(st.st_mode) == FALSE) {
136 /* The path is a file, not a directory. */
137 (*func) (path);
138 return;
141 /* The path is a valid directory. */
143 stack = g_slist_prepend(stack, g_strdup(path)); /* push */
145 tree = g_tree_new_full((GCompareDataFunc) cmp_func, NULL,
146 g_free, value_destroy_notify);
148 while (stack) {
149 current = stack->data;
150 stack = g_slist_remove_link(stack, stack); /* pop */
152 if (!stat(current, &st) && tree_insert_dir(tree, &st))
153 /* Not already scanned. */
154 stack = process_dir(stack, current, func);
156 g_free(current);
159 g_tree_destroy(tree);
160 destroyed_node = FALSE;