[augeas-devel] [PATCH 2/2] Minor changes to handling of save flag

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


* complain if /augeas/save is missing or has an invalid value
* do not clobber non-save flags when updating from /augeas/save
* add a test
---
 AUTHORS                 |    1 +
 src/augeas.c            |   45 +++++++++++--------
 tests/Makefile.am       |    2 +-
 tests/test-save-mode.sh |  111 +++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 140 insertions(+), 19 deletions(-)
 create mode 100755 tests/test-save-mode.sh

diff --git a/AUTHORS b/AUTHORS
index a5628e8..570aa65 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -9,6 +9,7 @@ Contributions by:
   Free Ekanayaka   <free at 64studio.com>
   Marc Fournier    <marc.fournier at camptocamp.com>
   Harald Hoyer     <harald at redhat.com>
+  Bryan Kearney    <bkearney at redhat.com>
   Jim Meyering     <meyering at redhat.com>
   Sean Millichamp  <sean at bruenor.org>
   Joel Nimety      <jnimety at perimeterusa.com>
diff --git a/src/augeas.c b/src/augeas.c
index 7b220c0..11c5cd6 100644
--- a/src/augeas.c
+++ b/src/augeas.c
@@ -604,30 +604,39 @@ static int tree_save(struct augeas *aug, struct tree *tree, const char *path,
     return result;
 }
 
-int aug_save(struct augeas *aug) {
-    int ret = 0;
-    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 */
+/* Reset the flags based on what is set in the tree. */
+static int update_save_flags(struct augeas *aug) {
     const char *savemode ;
     int noop = 0 ;
 
-    aug_get(aug, AUGEAS_META_SAVE_MODE, &savemode) ;
+    aug_get(aug, AUGEAS_META_SAVE_MODE, &savemode);
+    if (savemode == NULL)
+        return -1;
 
-    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 ;
+    aug->flags &= ~(AUG_SAVE_BACKUP|AUG_SAVE_NEWFILE|AUG_SAVE_NOOP);
+    if (STREQ(savemode, AUG_SAVE_NEWFILE_TEXT)) {
+        aug->flags |= AUG_SAVE_NEWFILE;
+    } else if (STREQ(savemode, AUG_SAVE_BACKUP_TEXT)) {
+        aug->flags |= AUG_SAVE_BACKUP;
+    } else if (STREQ(savemode, AUG_SAVE_NOOP_TEXT)) {
+        aug->flags |= AUG_SAVE_NOOP ;
         noop = 1 ;
-    } else {
-        aug->flags = AUG_NONE ;
+    } else if (STRNEQ(savemode, AUG_SAVE_OVERWRITE_TEXT)) {
+        return -1;
     }
 
+    return 0;
+}
+
+int aug_save(struct augeas *aug) {
+    int ret = 0;
+    struct tree *files;
+    struct path *p = NULL;
+
+    if (update_save_flags(aug) < 0)
+        return -1;
+
+    p = make_path(aug->origin->children, AUGEAS_FILES_TREE);
     if (p == NULL || path_find_one(p, &files) != 1) {
         free_path(p);
         return -1;
@@ -644,7 +653,7 @@ int aug_save(struct augeas *aug) {
                 ret = -1;
         }
     }
-    if (!noop) {
+    if (!(aug->flags & AUG_SAVE_NOOP)) {
         tree_clean(aug->origin->children);
     }
     return ret;
diff --git a/tests/Makefile.am b/tests/Makefile.am
index b23b05c..bd29917 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -12,7 +12,7 @@ valgrind: fatest
 check_SCRIPTS=test-lenses.sh test-interpreter.sh test-get.sh \
               test-put-symlink.sh test-save-empty.sh test-mv.sh \
               test-bug-1.sh test-idempotent.sh test-preserve.sh \
-	      test-events-saved.sh
+	      test-events-saved.sh test-save-mode.sh
 
 EXTRA_DIST=augtest $(AUGTESTS) root \
 	   $(check_SCRIPTS) $(wildcard modules/*.aug)
diff --git a/tests/test-save-mode.sh b/tests/test-save-mode.sh
new file mode 100755
index 0000000..0b0d67b
--- /dev/null
+++ b/tests/test-save-mode.sh
@@ -0,0 +1,111 @@
+#! /bin/sh
+
+# Test manipulating the save flags in /augeas/save
+
+root=$abs_top_builddir/build/test-save-mode
+hosts=$root/etc/hosts
+augopts="--nostdinc -r $root -I $abs_top_srcdir/lenses"
+
+run_augtool() {
+    exp=$1
+    shift
+    augtool $augopts "$@" > /dev/null
+    status=$?
+    if [ "x$exp" = ok -a $status -ne 0 ] ; then
+        echo "augtool failed"
+        exit 1
+    elif [ "x$exp" = fail -a $status -eq 0 ] ; then
+        echo "augtool succeeded but should have failed"
+        exit 1
+    fi
+}
+
+assert_ipaddr() {
+    exp="/files/etc/hosts/1/ipaddr = $1"
+    act=$(augtool $augopts get /files/etc/hosts/1/ipaddr)
+
+    if [ "$act" != "$exp" ] ; then
+        printf "Expected: %s\n" "$exp"
+        printf "Actual  : %s\n" "$act"
+        exit 1
+    fi
+}
+
+assert_file_exists() {
+    if [ ! -f "$1" ] ; then
+        echo "File $1 does not exist, but should"
+        exit 1
+    fi
+}
+
+assert_file_exists_not() {
+    if [ -f "$1" ] ; then
+        echo "File $1 exists, but should not"
+        exit 1
+    fi
+}
+
+setup() {
+#    echo $*
+    rm -rf $root
+    mkdir -p $(dirname $hosts)
+    cat > $hosts <<EOF
+127.0.0.1 localhost
+EOF
+}
+
+setup "No /augeas/save"
+run_augtool fail <<EOF
+set /files/etc/hosts/1/ipaddr 127.0.0.2
+rm /augeas/save
+save
+EOF
+assert_ipaddr 127.0.0.1
+
+setup "Invalid /augeas/save"
+run_augtool fail <<EOF
+set /files/etc/hosts/1/ipaddr 127.0.0.2
+set /augeas/save "not a valid flag"
+save
+EOF
+assert_ipaddr 127.0.0.1
+
+setup "noop"
+run_augtool fail <<EOF
+set /files/etc/hosts/1/ipaddr 127.0.0.2
+set /augeas/save noop
+save
+EOF
+assert_ipaddr 127.0.0.1
+assert_file_exists_not $hosts.augnew
+assert_file_exists_not $hosts.augsave
+
+setup "newfile"
+run_augtool ok <<EOF
+set /files/etc/hosts/1/ipaddr 127.0.0.2
+set /augeas/save newfile
+save
+EOF
+assert_ipaddr 127.0.0.1
+assert_file_exists $hosts.augnew
+assert_file_exists_not $hosts.augsave
+
+setup "overwrite"
+run_augtool ok <<EOF
+set /files/etc/hosts/1/ipaddr 127.0.0.2
+set /augeas/save overwrite
+save
+EOF
+assert_ipaddr 127.0.0.2
+assert_file_exists_not $hosts.augnew
+assert_file_exists_not $hosts.augsave
+
+setup "backup"
+run_augtool ok <<EOF
+set /files/etc/hosts/1/ipaddr 127.0.0.2
+set /augeas/save backup
+save
+EOF
+assert_ipaddr 127.0.0.2
+assert_file_exists_not $hosts.augnew
+assert_file_exists $hosts.augsave
-- 
1.6.0.6




More information about the augeas-devel mailing list