aboutsummaryrefslogtreecommitdiff
path: root/tree.c
diff options
context:
space:
mode:
authorDaniel Lezcano <daniel.lezcano@linaro.org>2011-08-25 15:46:13 +0200
committerDaniel Lezcano <daniel.lezcano@linaro.org>2011-08-25 15:46:13 +0200
commit25fc4a3e710aec3632a6f34a073427fe90b365a9 (patch)
treec59cf3e55966ae5e973655e70037aaa623acabd0 /tree.c
parentc80da0255e5d9e99da95b38d1c078887f6020e40 (diff)
follow symlinks when browsing the directory tree
Sometime we are interested in following the symlinks, sometime not. Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>
Diffstat (limited to 'tree.c')
-rw-r--r--tree.c11
1 files changed, 6 insertions, 5 deletions
diff --git a/tree.c b/tree.c
index aefe0fe..d331c60 100644
--- a/tree.c
+++ b/tree.c
@@ -17,6 +17,7 @@
#include <stdio.h>
#undef _GNU_SOURCE
#include <stdlib.h>
+#include <stdbool.h>
#include <string.h>
#include <dirent.h>
#include <sys/types.h>
@@ -111,7 +112,7 @@ static inline void tree_add_child(struct tree *parent, struct tree *child)
* @filter : a callback to filter out the directories
* Returns 0 on success, -1 otherwise
*/
-static int tree_scan(struct tree *tree, tree_filter_t filter)
+static int tree_scan(struct tree *tree, tree_filter_t filter, bool follow)
{
DIR *dir;
char *basedir, *newpath;
@@ -152,7 +153,7 @@ static int tree_scan(struct tree *tree, tree_filter_t filter)
if (ret)
goto out_free_newpath;
- if (S_ISDIR(s.st_mode)) {
+ if (S_ISDIR(s.st_mode) || (S_ISLNK(s.st_mode) && follow)) {
ret = -1;
@@ -164,7 +165,7 @@ static int tree_scan(struct tree *tree, tree_filter_t filter)
tree->nrchild++;
- ret = tree_scan(child, filter);
+ ret = tree_scan(child, filter, follow);
}
out_free_newpath:
@@ -190,7 +191,7 @@ static int tree_scan(struct tree *tree, tree_filter_t filter)
* Returns a tree structure corresponding to the root node of the
* directory tree representation on success, NULL otherwise
*/
-struct tree *tree_load(const char *path, tree_filter_t filter)
+struct tree *tree_load(const char *path, tree_filter_t filter, bool follow)
{
struct tree *tree;
@@ -198,7 +199,7 @@ struct tree *tree_load(const char *path, tree_filter_t filter)
if (!tree)
return NULL;
- if (tree_scan(tree, filter)) {
+ if (tree_scan(tree, filter, follow)) {
tree_free(tree);
return NULL;
}