[augeas-devel] [PATCH] Properly record the new mtime of a saved file in the tree

lutter at redhat.com lutter at redhat.com
Thu Sep 9 20:12:30 UTC 2010


From: David Lutterkort <lutter at redhat.com>

transform_save used to pass an incorrect file name to add_file_info (the
name was missing the root prefix), leading to an mitme of 0 for all saved
files. That in turn causes freshly saved files to be unnecessarily reloaded
on the next aug_load.

  * src/transform.c (mtime_as_string): tolerate a NULL filename, and return
    an mtime of 0; (transform_save): pass the full file name, including the
    root to add_file_info
  * tests/test-load.c (testReloadDeletedMeta): the test is now
    time-sensitive; to work around that, set the mtime of the changed tree
    to the original mtime of the file
  * tests/test-save.c (testMtime): verify that we record the actual mtime
    and not just 0 in the tree after a save.

Thanks to Laine Stump for spotting this
---
 src/transform.c   |    9 ++++++++-
 tests/test-load.c |   12 +++++++++++-
 tests/test-save.c |   25 +++++++++++++++++++++++++
 3 files changed, 44 insertions(+), 2 deletions(-)

diff --git a/src/transform.c b/src/transform.c
index f655715..f449471 100644
--- a/src/transform.c
+++ b/src/transform.c
@@ -118,6 +118,12 @@ static char *mtime_as_string(struct augeas *aug, const char *fname) {
     struct stat st;
     char *result = NULL;
 
+    if (fname == NULL) {
+        result = strdup("0");
+        ERR_NOMEM(result == NULL, aug);
+        goto done;
+    }
+
     r = stat(fname, &st);
     if (r < 0) {
         /* If we fail to stat, silently ignore the error
@@ -128,6 +134,7 @@ static char *mtime_as_string(struct augeas *aug, const char *fname) {
         r = xasprintf(&result, "%ld", (long) st.st_mtime);
         ERR_NOMEM(r < 0, aug);
     }
+ done:
     return result;
  error:
     FREE(result);
@@ -1011,7 +1018,7 @@ int transform_save(struct augeas *aug, struct tree *xfm,
     result = 1;
 
  done:
-    r = add_file_info(aug, path, lens, lens_name, filename);
+    r = add_file_info(aug, path, lens, lens_name, augorig);
     if (r < 0) {
         err_status = "file_info";
         result = -1;
diff --git a/tests/test-load.c b/tests/test-load.c
index 2daf49c..f11b86f 100644
--- a/tests/test-load.c
+++ b/tests/test-load.c
@@ -406,13 +406,19 @@ static void testReloadDeletedMeta(CuTest *tc) {
 static void testReloadExternalMod(CuTest *tc) {
     augeas *aug = NULL;
     int r, created;
-    const char *aug_root;
+    const char *aug_root, *s;
+    char *mtime;
 
     aug = setup_writable_hosts(tc);
 
     r = aug_load(aug);
     CuAssertRetSuccess(tc, r);
 
+    r = aug_get(aug, "/augeas/files/etc/hosts/mtime", &s);
+    CuAssertIntEquals(tc, 1, r);
+    mtime = strdup(s);
+    CuAssertPtrNotNull(tc, mtime);
+
     /* Set up a new entry and save */
     r = aug_defnode(aug, "new", "/files/etc/hosts/3", NULL, &created);
     CuAssertIntEquals(tc, 1, r);
@@ -427,6 +433,10 @@ static void testReloadExternalMod(CuTest *tc) {
     r = aug_save(aug);
     CuAssertRetSuccess(tc, r);
 
+    /* Fake the mtime to be old */
+    r = aug_set(aug, "/augeas/files/etc/hosts/mtime", mtime);
+    CuAssertRetSuccess(tc, r);
+
     /* Now modify the file outside of Augeas */
     r = aug_get(aug, "/augeas/root", &aug_root);
     CuAssertIntEquals(tc, 1, r);
diff --git a/tests/test-save.c b/tests/test-save.c
index daeace3..673a5a1 100644
--- a/tests/test-save.c
+++ b/tests/test-save.c
@@ -121,6 +121,30 @@ static void testMultipleXfm(CuTest *tc) {
     CuAssertIntEquals(tc, AUG_EMXFM, r);
 }
 
+static void testMtime(CuTest *tc) {
+    const char *s, *mtime2;
+    char *mtime1;
+    int r;
+
+    r = aug_set(aug, "/files/etc/hosts/1/alias[last() + 1]", "new");
+    CuAssertIntEquals(tc, 0, r);
+
+    r = aug_get(aug, "/augeas/files/etc/hosts/mtime", &s);
+    CuAssertIntEquals(tc, 1, r);
+    mtime1 = strdup(s);
+    CuAssertPtrNotNull(tc, mtime1);
+
+
+    r = aug_save(aug);
+    CuAssertIntEquals(tc, 0, r);
+
+    r = aug_get(aug, "/augeas/files/etc/hosts/mtime", &mtime2);
+    CuAssertIntEquals(tc, 1, r);
+
+    CuAssertStrNotEqual(tc, mtime1, mtime2);
+    CuAssertStrNotEqual(tc, "0", mtime2);
+}
+
 int main(void) {
     char *output = NULL;
     CuSuite* suite = CuSuiteNew();
@@ -142,6 +166,7 @@ int main(void) {
     SUITE_ADD_TEST(suite, testSaveNewFile);
     SUITE_ADD_TEST(suite, testNonExistentLens);
     SUITE_ADD_TEST(suite, testMultipleXfm);
+    SUITE_ADD_TEST(suite, testMtime);
 
     CuSuiteRun(suite);
     CuSuiteSummary(suite, &output);
-- 
1.7.2.2




More information about the augeas-devel mailing list