[augeas-devel] [PATCH 6/7] Add path_expand_tree

David Lutterkort lutter at redhat.com
Mon Dec 22 19:46:32 UTC 2008


Instead of calling path_find_one/tree_create
---
 src/augeas.c   |   19 ++++---------------
 src/internal.h |    3 +--
 src/pathx.c    |   35 ++++++++++++++++++++++-------------
 3 files changed, 27 insertions(+), 30 deletions(-)

diff --git a/src/augeas.c b/src/augeas.c
index eb52b75..ee4ef23 100644
--- a/src/augeas.c
+++ b/src/augeas.c
@@ -93,7 +93,6 @@ static char *format_path(struct tree *tree) {
     return path;
 }
 
-
 /* Propagate dirty flags towards the root */
 static int tree_propagate_dirty(struct tree *tree) {
     if (tree->dirty)
@@ -241,19 +240,14 @@ int aug_get(const struct augeas *aug, const char *path, const char **value) {
 struct tree *tree_set(struct tree *root, const char *path, const char *value) {
     struct tree *tree;
     struct path *p = make_path(root, path);
-    int r, segnr;
+    int r;
 
     if (p == NULL)
         goto error;
 
-    r = path_find_one(p, &tree, &segnr);
+    r = path_expand_tree(p, &tree);
     if (r == -1)
         goto error;
-
-    if (r == 0) {
-        if ((tree = tree_create(p, tree, segnr)) == NULL)
-            goto error;
-    }
     free_path(p);
 
     if (tree->value != NULL) {
@@ -447,7 +441,7 @@ int aug_mv(struct augeas *aug, const char *src, const char *dst) {
     struct path *s = make_path(root, src);
     struct path *d = make_path(root, dst);
     struct tree *ts, *td, *t;
-    int r, ret, segnr;
+    int r, ret;
 
     ret = -1;
     if (s == NULL || d == NULL)
@@ -457,15 +451,10 @@ int aug_mv(struct augeas *aug, const char *src, const char *dst) {
     if (r != 1)
         goto done;
 
-    r = path_find_one(d, &td, &segnr);
+    r = path_expand_tree(d, &td);
     if (r == -1)
         goto done;
 
-    if (r == 0) {
-        if ((td = tree_create(d, td, segnr)) == NULL)
-            goto done;
-    }
-
     /* Don't move SRC into its own descendent */
     t = td;
     do {
diff --git a/src/internal.h b/src/internal.h
index fc33630..a577ad6 100644
--- a/src/internal.h
+++ b/src/internal.h
@@ -340,8 +340,7 @@ struct path *make_path(const struct tree *root, const char *path);
 struct tree *path_first(struct path *path);
 struct tree *path_next(struct path *path, struct tree *cur);
 int path_find_one(struct path *path, struct tree **match, int *segnr);
-struct tree *tree_create(struct path *path, struct tree *parent,
-                         int segnr);
+int path_expand_tree(struct path *path, struct tree **tree);
 void free_path(struct path *path);
 
 #endif
diff --git a/src/pathx.c b/src/pathx.c
index 8d3479d..7ee3ec1 100644
--- a/src/pathx.c
+++ b/src/pathx.c
@@ -323,36 +323,45 @@ int path_find_one(struct path *path, struct tree **match, int *segnr) {
  * Return the first segment that was created by this operation, or NULL on
  * error.
  */
-struct tree *tree_create(struct path *path, struct tree *parent,
-                         int segnr) {
-    struct segment *s, *seg;
-    struct tree *first_child = NULL;
+int path_expand_tree(struct path *path, struct tree **tree) {
+    int r, segnr;
+
+    *tree = path->root;
+    r = path_find_one(path, tree, &segnr);
+    if (r == -1)
+        return -1;
 
     if (segnr == path->nsegments - 1)
         return 0;
 
+    struct tree *first_child = NULL;
+    struct tree *parent = *tree;
+    struct segment *seg = path->segments + segnr + 1;
+
     if (parent == NULL)
         parent = path->root->parent;
 
-    seg = path->segments + segnr + 1;
-    for (s = seg ; s <= last_segment(path); s++) {
-        struct tree *tree = make_tree(strdup(s->label), NULL, parent, NULL);
+    for (struct segment *s = seg ; s <= last_segment(path); s++) {
+        struct tree *t = make_tree(strdup(s->label), NULL, parent, NULL);
         if (first_child == NULL)
-            first_child = tree;
-        if (tree == NULL || tree->label == NULL)
+            first_child = t;
+        if (t == NULL || t->label == NULL)
             goto error;
-        list_append(parent->children, tree);
-        parent = tree;
+        list_append(parent->children, t);
+        parent = t;
     }
 
     while (first_child->children != NULL)
         first_child = first_child->children;
 
-    return first_child;
+    *tree = first_child;
+    return 0;
+
  error:
     list_remove(first_child, first_child->parent->children);
     free_tree(first_child);
-    return NULL;
+    *tree = NULL;
+    return -1;
 }
 
 /*
-- 
1.6.0.4




More information about the augeas-devel mailing list