]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
libcgroup: Introduce post order walk
authorDhaval Giani <dhaval@linux.vnet.ibm.com>
Thu, 18 Jun 2009 14:12:46 +0000 (19:42 +0530)
committerDhaval Giani <dhaval@linux.vnet.ibm.com>
Thu, 18 Jun 2009 14:19:39 +0000 (19:49 +0530)
With the introduction of the flags, we now actually make use of them.
This patch adds a post mode and modifies the test case to also do a post
order walk.

Signed-off-by: Dhaval Giani <dhaval@linux.vnet.ibm.com>
Acked-by: Balbir Singh <balbir@linux.vnet.ibm.com>
Acked-by: Bharata B Rao <bharata@linux.vnet.ibm.com>
src/api.c
src/libcgroup.map
tests/walk_test.c

index f746bb2168bfaba271cc7b6a559e5880d90bbc6e..2facfc833d77b01135aaa3dfcc689cba48179902 100644 (file)
--- a/src/api.c
+++ b/src/api.c
@@ -2212,7 +2212,7 @@ int cgroup_get_last_errno()
 
 
 static int cg_walk_node(FTS *fts, FTSENT *ent, const int depth,
-                       struct cgroup_file_info *info)
+                       struct cgroup_file_info *info, int dir)
 {
        int ret = 0;
 
@@ -2236,12 +2236,15 @@ static int cg_walk_node(FTS *fts, FTSENT *ent, const int depth,
                errno = ent->fts_errno;
                break;
        case FTS_D:
-               info->type = CGROUP_FILE_TYPE_DIR;
+               if (dir & CGROUP_WALK_TYPE_PRE_DIR)
+                       info->type = CGROUP_FILE_TYPE_DIR;
                break;
        case FTS_DC:
        case FTS_NSOK:
        case FTS_NS:
        case FTS_DP:
+               if (dir & CGROUP_WALK_TYPE_POST_DIR)
+                       info->type = CGROUP_FILE_TYPE_DIR;
                break;
        case FTS_F:
                info->type = CGROUP_FILE_TYPE_FILE;
@@ -2272,7 +2275,9 @@ int cgroup_walk_tree_next(const int depth, void **handle,
                return ECGEOF;
        if (!base_level && depth)
                base_level = ent->fts_level + depth;
-       ret = cg_walk_node(entry->fts, ent, base_level, info);
+
+       ret = cg_walk_node(entry->fts, ent, base_level, info, entry->flags);
+
        *handle = entry;
        return ret;
 }
@@ -2326,6 +2331,8 @@ int cgroup_walk_tree_begin(char *controller, char *base_path, const int depth,
                return ECGOTHER;
        }
 
+       entry->flags |= CGROUP_WALK_TYPE_PRE_DIR;
+
        *base_level = 0;
        cg_path[0] = full_path;
        cg_path[1] = NULL;
@@ -2339,7 +2346,9 @@ int cgroup_walk_tree_begin(char *controller, char *base_path, const int depth,
        }
        if (!*base_level && depth)
                *base_level = ent->fts_level + depth;
-       ret = cg_walk_node(entry->fts, ent, *base_level, info);
+
+       ret = cg_walk_node(entry->fts, ent, base_level, info, entry->flags);
+
        *handle = entry;
        return ret;
 }
index 8d330e61eb27c113f3cdaa4d61307066ed793c5b..0748bb348b42fe59a3aa5d3cc6fa6d4fe3db69fa 100644 (file)
@@ -62,6 +62,7 @@ global:
        cgroup_read_stats_begin;
        cgroup_read_stats_next;
        cgroup_read_stats_end;
+       cgroup_walk_tree_set_flags;
        cgroup_get_controller;
        cgroup_get_uid_gid_from_procfs;
 } CGROUP_0.33;
index 2e16ecd37f8333e81bc365c7b6556379627a7b4c..e09f705a5662b044806ccd849b3532a680b2d040 100644 (file)
@@ -41,6 +41,7 @@ int main(int argc, char *argv[])
                exit(EXIT_FAILURE);
        }
        strcpy(root, info.full_path);
+       printf("Begin pre-order walk\n");
        printf("root is %s\n", root);
        visit_node(&info, root);
        while ((ret = cgroup_walk_tree_next(0, &handle, &info, lvl)) !=
@@ -49,6 +50,32 @@ int main(int argc, char *argv[])
        }
        cgroup_walk_tree_end(&handle);
 
+       printf("pre-order walk finished\n");
+       ret = cgroup_walk_tree_begin(controller, "/", 0, &handle, &info, &lvl);
+
+       if (ret != 0) {
+               fprintf(stderr, "Walk failed\n");
+               exit(EXIT_FAILURE);
+       }
+
+       ret = cgroup_walk_tree_set_flags(&handle, CGROUP_WALK_TYPE_POST_DIR);
+
+       if (ret) {
+               fprintf(stderr, "Walk failed with %s\n", cgroup_strerror(ret));
+               exit(EXIT_FAILURE);
+       }
+
+       strcpy(root, info.full_path);
+       printf("Begin post-order walk\n");
+       printf("root is %s\n", root);
+       visit_node(&info, root);
+       while ((ret = cgroup_walk_tree_next(0, &handle, &info, lvl)) !=
+                       ECGEOF) {
+               visit_node(&info, root);
+       }
+       cgroup_walk_tree_end(&handle);
+       printf("post order walk finished\n");
+
        ret = cgroup_walk_tree_begin(controller, "/a", 2, &handle, &info, &lvl);
 
        if (ret != 0) {