Blob Blame History Raw
From 4d69d88f20a7aa87d81004db44c1094ad1afea80 Mon Sep 17 00:00:00 2001
From: Dominic Cleal <dcleal@redhat.com>
Date: Thu, 4 Sep 2014 08:44:20 +0100
Subject: [PATCH] * src/augeas.c (unlink_removed_files): ensure aug_save
 returns non-zero result   when unable to delete files

Fixes RHBZ#1091143

(cherry picked from commit b61a78d2e629e4f1395270a1ee72876e27b12990)

Conflicts:
	NEWS
---
 src/augeas.c      |  3 ++-
 tests/test-save.c | 26 ++++++++++++++++++++++++++
 2 files changed, 28 insertions(+), 1 deletion(-)

diff --git a/src/augeas.c b/src/augeas.c
index 3c12443..014e145 100644
--- a/src/augeas.c
+++ b/src/augeas.c
@@ -1488,7 +1488,8 @@ static int unlink_removed_files(struct augeas *aug,
             for (struct tree *t = pathx_first(px);
                  t != NULL;
                  t = pathx_next(px)) {
-                remove_file(aug, t);
+                if (remove_file(aug, t) < 0)
+                    result = -1;
             }
             free_pathx(px);
         } else if (tf->dirty && ! tree_child(tm, "path")) {
diff --git a/tests/test-save.c b/tests/test-save.c
index f28f626..1dac75d 100644
--- a/tests/test-save.c
+++ b/tests/test-save.c
@@ -29,6 +29,7 @@
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <sys/wait.h>
+#include <unistd.h>
 
 const char *abs_top_srcdir;
 const char *abs_top_builddir;
@@ -70,6 +71,30 @@ static void teardown(ATTRIBUTE_UNUSED CuTest *tc) {
     root = NULL;
 }
 
+static void testRemoveNoPermission(CuTest *tc) {
+    if (getuid() == 0) {
+        puts("pending (testRemoveNoPermission): can't test permissions under root account");
+        return;
+    }
+
+    int r;
+    const char *errmsg;
+
+    // Prevent deletion of files
+    run(tc, "chmod 0500 %s/etc", root);
+
+    r = aug_rm(aug, "/files/etc/hosts");
+    CuAssertTrue(tc, r > 0);
+
+    r = aug_save(aug);
+    CuAssertIntEquals(tc, -1, r);
+
+    r = aug_get(aug, "/augeas/files/etc/hosts/error", &errmsg);
+    CuAssertIntEquals(tc, 1, r);
+    CuAssertPtrNotNull(tc, errmsg);
+    CuAssertStrEquals(tc, "unlink_orig", errmsg);
+}
+
 static void testSaveNewFile(CuTest *tc) {
     int r;
 
@@ -285,6 +310,7 @@ int main(void) {
     CuSuiteSetup(suite, setup, teardown);
 
     SUITE_ADD_TEST(suite, testSaveNewFile);
+    SUITE_ADD_TEST(suite, testRemoveNoPermission);
     SUITE_ADD_TEST(suite, testNonExistentLens);
     SUITE_ADD_TEST(suite, testMultipleXfm);
     SUITE_ADD_TEST(suite, testMtime);