[powerdebug 1/7] follow symlinks when browsing the directory tree

Daniel Lezcano daniel.lezcano at linaro.org
Thu Aug 25 13:47:01 UTC 2011


Sometime we are interested in following the symlinks, sometime not.

Signed-off-by: Daniel Lezcano <daniel.lezcano at linaro.org>
---
 clocks.c    |    2 +-
 regulator.c |    2 +-
 sensor.c    |    2 +-
 tree.c      |   11 ++++++-----
 tree.h      |    2 +-
 5 files changed, 10 insertions(+), 9 deletions(-)

diff --git a/clocks.c b/clocks.c
index 20a245c..364c0af 100644
--- a/clocks.c
+++ b/clocks.c
@@ -413,7 +413,7 @@ int clock_init(void)
 	if (access(clk_dir_path, F_OK))
 		return -1;
 
-	clock_tree = tree_load(clk_dir_path, NULL);
+	clock_tree = tree_load(clk_dir_path, NULL, false);
 	if (!clock_tree)
 		return -1;
 
diff --git a/regulator.c b/regulator.c
index e9b01bb..55bd3e9 100644
--- a/regulator.c
+++ b/regulator.c
@@ -236,7 +236,7 @@ static struct display_ops regulator_ops = {
 
 int regulator_init(void)
 {
-	reg_tree = tree_load(SYSFS_REGULATOR, regulator_filter_cb);
+	reg_tree = tree_load(SYSFS_REGULATOR, regulator_filter_cb, false);
 	if (!reg_tree)
 		return -1;
 
diff --git a/sensor.c b/sensor.c
index e172f88..ff1e3dd 100644
--- a/sensor.c
+++ b/sensor.c
@@ -271,7 +271,7 @@ static struct display_ops sensor_ops = {
 
 int sensor_init(void)
 {
-	sensor_tree = tree_load(SYSFS_SENSOR, sensor_filter_cb);
+	sensor_tree = tree_load(SYSFS_SENSOR, sensor_filter_cb, false);
 	if (!sensor_tree)
 		return -1;
 
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;
 	}
diff --git a/tree.h b/tree.h
index c7f3ca9..5c1c697 100644
--- a/tree.h
+++ b/tree.h
@@ -41,7 +41,7 @@ typedef int (*tree_cb_t)(struct tree *t, void *data);
 
 typedef int (*tree_filter_t)(const char *name);
 
-extern struct tree *tree_load(const char *path, tree_filter_t filter);
+extern struct tree *tree_load(const char *path, tree_filter_t filter, bool follow);
 
 extern struct tree *tree_find(struct tree *tree, const char *name);
 
-- 
1.7.4.1




More information about the linaro-dev mailing list