[augeas-devel] [PATCH 1/2] Dynamically change behavior of aug_save; add noop save mode

David Lutterkort lutter at redhat.com
Wed Jan 14 00:26:29 UTC 2009


From: Bryan Kearney <bkearney at redhat.com>

* control behavior of aug_save through changing /augeas/save
* noop mode allows checking if changes will be written, without
  changing file system
---
 src/augeas.c    |   31 +++++++++++++++++++++++++++----
 src/augeas.h    |    4 +++-
 src/internal.h  |    6 ++++++
 src/transform.c |    4 ++++
 4 files changed, 40 insertions(+), 5 deletions(-)

diff --git a/src/augeas.c b/src/augeas.c
index 4d61ffc..7b220c0 100644
--- a/src/augeas.c
+++ b/src/augeas.c
@@ -192,11 +192,13 @@ struct augeas *aug_init(const char *root, const char *loadpath,
     aug_set(result, AUGEAS_META_ROOT, result->root);
 
     if (flags & AUG_SAVE_NEWFILE) {
-        aug_set(result, AUGEAS_META_SAVE_MODE, "newfile");
+        aug_set(result, AUGEAS_META_SAVE_MODE, AUG_SAVE_NEWFILE_TEXT);
     } else if (flags & AUG_SAVE_BACKUP) {
-        aug_set(result, AUGEAS_META_SAVE_MODE, "backup");
+        aug_set(result, AUGEAS_META_SAVE_MODE, AUG_SAVE_BACKUP_TEXT);
+    } else if (flags & AUG_SAVE_NOOP) {
+        aug_set(result, AUGEAS_META_SAVE_MODE, AUG_SAVE_NOOP_TEXT);
     } else {
-        aug_set(result, AUGEAS_META_SAVE_MODE, "overwrite");
+        aug_set(result, AUGEAS_META_SAVE_MODE, AUG_SAVE_OVERWRITE_TEXT);
     }
 
     if (interpreter_init(result) == -1)
@@ -607,6 +609,25 @@ int aug_save(struct augeas *aug) {
     struct tree *files;
     struct path *p = make_path(aug->origin->children, AUGEAS_FILES_TREE);
 
+    /* Reset the flags based on what is set in the true.
+     * Note, this does cuase us to loose init flags such as
+     * AUG_TYPE_CHECK and AUG_NO_STDINC */
+    const char *savemode ;
+    int noop = 0 ;
+
+    aug_get(aug, AUGEAS_META_SAVE_MODE, &savemode) ;
+
+    if (strcmp(savemode,AUG_SAVE_NEWFILE_TEXT)==0) {
+        aug->flags = AUG_SAVE_NEWFILE ;
+    } else if (strcmp(savemode,AUG_SAVE_BACKUP_TEXT)==0) {
+        aug->flags = AUG_SAVE_BACKUP ;
+    } else if (strcmp(savemode,AUG_SAVE_NOOP_TEXT)==0) {
+        aug->flags = AUG_SAVE_NOOP ;
+        noop = 1 ;
+    } else {
+        aug->flags = AUG_NONE ;
+    }
+
     if (p == NULL || path_find_one(p, &files) != 1) {
         free_path(p);
         return -1;
@@ -623,7 +644,9 @@ int aug_save(struct augeas *aug) {
                 ret = -1;
         }
     }
-    tree_clean(aug->origin->children);
+    if (!noop) {
+        tree_clean(aug->origin->children);
+    }
     return ret;
 }
 
diff --git a/src/augeas.h b/src/augeas.h
index e569e55..03ed324 100644
--- a/src/augeas.h
+++ b/src/augeas.h
@@ -42,8 +42,10 @@ enum aug_flags {
                                      precedence over AUG_SAVE_BACKUP */
     AUG_TYPE_CHECK   = (1 << 2),  /* Typecheck lenses; since it can be very
                                      expensive it is not done by default */
-    AUG_NO_STDINC    = (1 << 3)   /* Do not use the builtin load path for
+    AUG_NO_STDINC    = (1 << 3),   /* Do not use the builtin load path for
                                      modules */
+    AUG_SAVE_NOOP    = (1 << 4)   /* Make save a no-op process, just record
+                                     what would have changed */
 };
 
 /* Function: aug_init
diff --git a/src/internal.h b/src/internal.h
index f7c8fd4..0317c99 100644
--- a/src/internal.h
+++ b/src/internal.h
@@ -97,6 +97,12 @@
  * Character separating paths in a list of paths */
 #define PATH_SEP_CHAR ':'
 
+/* Constants for setting the save mode via the augeas path at
+ * AUGEAS_META_SAVE_MODE */
+#define AUG_SAVE_BACKUP_TEXT "backup"
+#define AUG_SAVE_NEWFILE_TEXT "newfile"
+#define AUG_SAVE_NOOP_TEXT "noop"
+#define AUG_SAVE_OVERWRITE_TEXT "overwrite"
 
 #ifdef __GNUC__
 
diff --git a/src/transform.c b/src/transform.c
index ca5d5f9..67f1d77 100644
--- a/src/transform.c
+++ b/src/transform.c
@@ -622,6 +622,10 @@ int transform_save(struct augeas *aug, struct transform *xform,
             result = 0;
             unlink(augnew);
             goto done;
+        } else if (aug->flags & AUG_SAVE_NOOP) {
+            result = 1;
+            unlink(augnew);
+            goto done;
         }
     }
 
-- 
1.6.0.6




More information about the augeas-devel mailing list