1a18f20
From 11b37b65d08c2a8b6d967fd866ebbdbe7e864949 Mon Sep 17 00:00:00 2001
1a18f20
From: Nishant Nayan <nishant.nayan@oracle.com>
1a18f20
Date: Thu, 26 Nov 2020 14:35:17 +0000
1a18f20
Subject: [PATCH] rm: do not skip files upon failure to remove an empty dir
1a18f20
1a18f20
When removing a directory fails for some reason, and that directory
1a18f20
is empty, the rm_fts code gets the return value of the excise call
1a18f20
confused with the return value of its earlier call to prompt,
1a18f20
causing fts_skip_tree to be called again and the next file
1a18f20
that rm would otherwise have deleted to survive.
1a18f20
1a18f20
* src/remove.c (rm_fts): Ensure we only skip a single fts entry,
1a18f20
when processing empty dirs.  I.e. only skip the entry
1a18f20
having successfully removed it.
1a18f20
* tests/rm/empty-immutable-skip.sh: New root-only test.
1a18f20
* tests/local.mk: Add it.
1a18f20
* NEWS: Mention the bug fix.
1a18f20
Fixes https://bugs.gnu.org/44883
1a18f20
1a18f20
Upstream-commit: 6bf108358a6104ec1c694c9530b3cd56b95f4b57
1a18f20
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
1a18f20
---
1a18f20
 src/remove.c                     |  3 ++-
1a18f20
 tests/local.mk                   |  1 +
1a18f20
 tests/rm/empty-immutable-skip.sh | 46 ++++++++++++++++++++++++++++++++
1a18f20
 3 files changed, 49 insertions(+), 1 deletion(-)
1a18f20
 create mode 100755 tests/rm/empty-immutable-skip.sh
1a18f20
1a18f20
diff --git a/src/remove.c b/src/remove.c
1a18f20
index 2d40c55..adf9489 100644
1a18f20
--- a/src/remove.c
1a18f20
+++ b/src/remove.c
1a18f20
@@ -506,7 +506,8 @@ rm_fts (FTS *fts, FTSENT *ent, struct rm_options const *x)
1a18f20
             /* When we know (from prompt when in interactive mode)
1a18f20
                that this is an empty directory, don't prompt twice.  */
1a18f20
             s = excise (fts, ent, x, true);
1a18f20
-            fts_skip_tree (fts, ent);
1a18f20
+            if (s == RM_OK)
1a18f20
+              fts_skip_tree (fts, ent);
1a18f20
           }
1a18f20
 
1a18f20
         if (s != RM_OK)
1a18f20
diff --git a/tests/local.mk b/tests/local.mk
1a18f20
index 5f7f775..2aeff2b 100644
1a18f20
--- a/tests/local.mk
1a18f20
+++ b/tests/local.mk
1a18f20
@@ -136,6 +136,7 @@ all_root_tests =				\
1a18f20
   tests/rm/no-give-up.sh			\
1a18f20
   tests/rm/one-file-system.sh			\
1a18f20
   tests/rm/read-only.sh				\
1a18f20
+  tests/rm/empty-immutable-skip.sh		\
1a18f20
   tests/tail-2/append-only.sh			\
1a18f20
   tests/tail-2/end-of-device.sh			\
1a18f20
   tests/touch/now-owned-by-other.sh
1a18f20
diff --git a/tests/rm/empty-immutable-skip.sh b/tests/rm/empty-immutable-skip.sh
1a18f20
new file mode 100755
1a18f20
index 0000000..c91d8d4
1a18f20
--- /dev/null
1a18f20
+++ b/tests/rm/empty-immutable-skip.sh
1a18f20
@@ -0,0 +1,46 @@
1a18f20
+#!/bin/sh
1a18f20
+# Ensure that rm does not skip extra files after hitting an empty immutable dir.
1a18f20
+# Requires root access to do chattr +i, as well as an ext[23] or xfs file system
1a18f20
+
1a18f20
+# Copyright (C) 2020 Free Software Foundation, Inc.
1a18f20
+
1a18f20
+# This program is free software: you can redistribute it and/or modify
1a18f20
+# it under the terms of the GNU General Public License as published by
1a18f20
+# the Free Software Foundation, either version 3 of the License, or
1a18f20
+# (at your option) any later version.
1a18f20
+
1a18f20
+# This program is distributed in the hope that it will be useful,
1a18f20
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1a18f20
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1a18f20
+# GNU General Public License for more details.
1a18f20
+
1a18f20
+# You should have received a copy of the GNU General Public License
1a18f20
+# along with this program.  If not, see <https://www.gnu.org/licenses/>.
1a18f20
+
1a18f20
+. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
1a18f20
+print_ver_ rm
1a18f20
+require_root_
1a18f20
+
1a18f20
+# These simple one-file operations are expected to work even in the
1a18f20
+# presence of this bug, and we need them to set up the rest of the test.
1a18f20
+chattr_i_works=1
1a18f20
+touch f
1a18f20
+chattr +i f 2>/dev/null || chattr_i_works=0
1a18f20
+rm f 2>/dev/null
1a18f20
+test -f f || chattr_i_works=0
1a18f20
+chattr -i f 2>/dev/null || chattr_i_works=0
1a18f20
+rm f 2>/dev/null || chattr_i_works=0
1a18f20
+test -f f && chattr_i_works=0
1a18f20
+
1a18f20
+if test $chattr_i_works = 0; then
1a18f20
+  skip_ "chattr +i doesn't work on this file system"
1a18f20
+fi
1a18f20
+
1a18f20
+mkdir empty || framework_failure_
1a18f20
+touch x y || framework_failure_
1a18f20
+chattr +i empty || framework_failure_
1a18f20
+rm -rf empty x y
1a18f20
+{ test -f x || test -f y; } && fail=1
1a18f20
+chattr -i empty
1a18f20
+
1a18f20
+Exit $fail
1a18f20
-- 
1a18f20
2.26.2
1a18f20