[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[augeas-devel] augeas: master - * src/augeas.c: create various /augeas nodes when they go missing



Gitweb:        http://git.fedorahosted.org/git/augeas.git?p=augeas.git;a=commitdiff;h=2ec6ba63fe018cafb90f9ac0fe9209fdfad2f123
Commit:        2ec6ba63fe018cafb90f9ac0fe9209fdfad2f123
Parent:        9da198dc2cc2e34929bed67b41aec20054e1a934
Author:        David Lutterkort <lutter redhat com>
AuthorDate:    Thu Mar 19 20:35:46 2009 -0700
Committer:     David Lutterkort <lutter redhat com>
CommitterDate: Thu Mar 19 20:35:46 2009 -0700

* src/augeas.c: create various /augeas nodes when they go missing

---
 src/augeas.c |   76 ++++++++++++++++++++++++++++++++-------------------------
 1 files changed, 43 insertions(+), 33 deletions(-)

diff --git a/src/augeas.c b/src/augeas.c
index a6ac078..6d55d80 100644
--- a/src/augeas.c
+++ b/src/augeas.c
@@ -60,6 +60,34 @@ static void tree_clean(struct tree *tree) {
     tree->dirty = 0;
 }
 
+static struct tree *tree_child(struct tree *tree, const char *label) {
+    if (tree == NULL)
+        return NULL;
+
+    list_for_each(child, tree->children) {
+        if (streqv(label, child->label))
+            return child;
+    }
+    return NULL;
+}
+
+/* Get first existing child or create one */
+static struct tree *tree_child_cr(struct tree *tree, const char *label) {
+    static struct tree *child = NULL;
+
+    if (tree == NULL)
+        return NULL;
+
+    child = tree_child(tree, label);
+    if (child == NULL) {
+        char *l = strdup(label);
+        if (l == NULL)
+            return NULL;
+        child = tree_append(tree, l, NULL);
+    }
+    return child;
+}
+
 /* Parse a path expression. Report errors in /augeas/pathx/error */
 static struct pathx *parse_user_pathx(const struct augeas *aug,
                                       const char *path) {
@@ -94,17 +122,6 @@ static struct pathx *parse_user_pathx(const struct augeas *aug,
     return NULL;
 }
 
-static struct tree *tree_child(struct tree *tree, const char *label) {
-    if (tree == NULL)
-        return NULL;
-
-    list_for_each(child, tree->children) {
-        if (streqv(label, child->label))
-            return child;
-    }
-    return NULL;
-}
-
 static const char *init_root(const char *root0) {
     char *root;
 
@@ -136,20 +153,16 @@ struct tree *tree_append(struct tree *parent,
 static struct tree *tree_from_transform(struct augeas *aug,
                                         const char *modname,
                                         struct transform *xfm) {
-    struct tree *meta = tree_child(aug->origin, "augeas");
+    struct tree *meta = tree_child_cr(aug->origin, "augeas");
     struct tree *load = NULL, *txfm = NULL;
     char *m = NULL, *q = NULL;
 
     if (meta == NULL)
         goto error;
 
-    load = tree_child(meta, "load");
-    if (load == NULL) {
-        char *l = strdup("load");
-        load = tree_append(meta, l, NULL);
-        if (load == NULL)
+    load = tree_child_cr(meta, "load");
+    if (load == NULL)
             goto error;
-    }
     if (modname != NULL) {
         m = strdup(modname);
         if (m == NULL)
@@ -285,13 +298,13 @@ static void tree_unlink_children(struct tree *tree) {
 }
 
 int aug_load(struct augeas *aug) {
-    struct tree *meta = tree_child(aug->origin, "augeas");
-    struct tree *meta_files = tree_child(meta, "files");
-    struct tree *files = tree_child(aug->origin, "files");
-    struct tree *load = tree_child(meta, "load");
+    struct tree *meta = tree_child_cr(aug->origin, "augeas");
+    struct tree *meta_files = tree_child_cr(meta, "files");
+    struct tree *files = tree_child_cr(aug->origin, "files");
+    struct tree *load = tree_child_cr(meta, "load");
 
     if (load == NULL)
-        return 0;
+        return -1;
 
     tree_unlink_children(meta_files);
     tree_unlink_children(files);
@@ -646,8 +659,8 @@ int aug_match(const struct augeas *aug, const char *pathin, char ***matches) {
 static int tree_save(struct augeas *aug, struct tree *tree,
                      const char *path) {
     int result = 0;
-    struct tree *meta = tree_child(aug->origin, "augeas");
-    struct tree *load = tree_child(meta, "load");
+    struct tree *meta = tree_child_cr(aug->origin, "augeas");
+    struct tree *load = tree_child_cr(meta, "load");
 
     // FIXME: We need to detect subtrees that aren't saved by anything
 
@@ -752,20 +765,17 @@ static int unlink_removed_files(struct augeas *aug,
 
 int aug_save(struct augeas *aug) {
     int ret = 0;
-    struct tree *meta = tree_child(aug->origin, "augeas");
-    struct tree *meta_files = tree_child(meta, "files");
-    struct tree *files = tree_child(aug->origin, "files");
-    struct tree *load = tree_child(meta, "load");
+    struct tree *meta = tree_child_cr(aug->origin, "augeas");
+    struct tree *meta_files = tree_child_cr(meta, "files");
+    struct tree *files = tree_child_cr(aug->origin, "files");
+    struct tree *load = tree_child_cr(meta, "load");
 
     if (update_save_flags(aug) < 0)
         return -1;
 
-    if (files == NULL)
+    if (files == NULL || meta == NULL || load == NULL)
         return -1;
 
-    if (meta == NULL || load == NULL)
-        return 0;
-
     aug_rm(aug, AUGEAS_EVENTS_SAVED);
 
     list_for_each(xfm, load->children)



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]