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