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 ************************/
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.
43 /* Called when we insert an element that was already there. */
44 static void value_destroy_notify(gpointer 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
)
56 if (dir1
->ino
> dir2
->ino
)
59 if (dir1
->dev
< dir2
->dev
)
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
)
70 struct dirent
*dir_ent
;
72 gchar
*name
, *fullname
;
74 dir
= opendir(dirname
);
78 for (dir_ent
= readdir(dir
); dir_ent
!= NULL
; dir_ent
= readdir(dir
)) {
79 name
= dir_ent
->d_name
;
82 (name
[1] == '\0' || (name
[1] == '.' && name
[2] == '\0')))
83 /* Skip "." and "..". */
86 fullname
= fast_build_filename(dirname
, name
);
88 if (stat(fullname
, &st
) < 0)
89 /* Shouldn't happen... */
92 if (S_ISDIR(st
.st_mode
))
93 stack
= g_slist_prepend(stack
, fullname
);
104 /* Return TRUE if the dir has been correctly inserted. */
105 static gboolean
tree_insert_dir(GTree
* tree
, struct stat
*st
)
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
;
124 void foreach_file(const gchar
* path
, foreach_file_func func
)
126 GSList
*stack
= NULL
;
131 if (stat(path
, &st
) < 0)
132 /* The path is not usable. */
135 if (S_ISDIR(st
.st_mode
) == FALSE
) {
136 /* The path is a file, not a directory. */
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
);
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
);
159 g_tree_destroy(tree
);
160 destroyed_node
= FALSE
;