From 9b8d6e752767e286ccc847f03518dec5af9abbc5 Mon Sep 17 00:00:00 2001
From: Rob Day <robertkday@gmail.com>
Date: Wed, 22 Aug 2012 23:04:19 +0100
Subject: [PATCH 1/5] rm: add test to check that rm -d -i works

---
 tests/Makefile.am |  1 +
 tests/rm/d-3      | 37 +++++++++++++++++++++++++++++++++++++
 2 files changed, 38 insertions(+)
 create mode 100755 tests/rm/d-3

diff --git a/tests/Makefile.am b/tests/Makefile.am
index acd816d..87d6cad 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -101,6 +101,7 @@ TESTS =						\
   misc/ls-time					\
   rm/d-1					\
   rm/d-2					\
+  rm/d-3					\
   rm/deep-1					\
   rm/deep-2					\
   rm/dir-no-w					\
diff --git a/tests/rm/d-3 b/tests/rm/d-3
new file mode 100755
index 0000000..6ec067d
--- /dev/null
+++ b/tests/rm/d-3
@@ -0,0 +1,37 @@
+#!/bin/sh
+# Ensure that 'rm -d dir' (i.e., without --recursive) gives a reasonable
+# diagnostic when failing.
+
+# Copyright (C) 2012 Free Software Foundation, Inc.
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+. "${srcdir=.}/init.sh"; path_prepend_ ../src
+print_ver_ rm
+
+mkdir d || framework_failure_
+
+echo "y" | rm -i -d --verbose d > out 2> out.err || fail=1
+printf "%s" \
+    "rm: remove directory 'd'? " \
+    > exp.err || framework_failure_
+
+printf "%s\n" \
+    "removed directory: 'd'" \
+    > exp || framework_failure_
+
+compare exp out || fail=1
+compare exp.err out.err || fail=1
+
+Exit $fail
-- 
1.7.11.2


From 86d479f33bfe49c2fa2b95465012e501e35ae5b1 Mon Sep 17 00:00:00 2001
From: Rob Day <robertkday@gmail.com>
Date: Wed, 22 Aug 2012 23:06:55 +0100
Subject: [PATCH 2/5] rm: fixed bug #12260 (rm -d -i)

---
 src/remove.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/src/remove.c b/src/remove.c
index c4972ac..2f9e86b 100644
--- a/src/remove.c
+++ b/src/remove.c
@@ -238,7 +238,9 @@ prompt (FTS const *fts, FTSENT const *ent, bool is_dir,
             break;
 
           case DT_DIR:
-            if (!x->recursive)
+            if (!x->recursive
+                && !(x->remove_empty_directories
+                     && is_empty_dir (fts->fts_cwd_fd, ent->fts_accpath)))
               {
                 write_protected = -1;
                 wp_errno = EISDIR;
-- 
1.7.11.2


From 0ea3dbd6232bcbfd8fff01667f28cb86032e3200 Mon Sep 17 00:00:00 2001
From: Rob Day <robertkday@gmail.com>
Date: Thu, 23 Aug 2012 00:36:33 +0100
Subject: [PATCH 3/5] rm: updated comment in test rm/d-3

---
 tests/rm/d-3 | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/tests/rm/d-3 b/tests/rm/d-3
index 6ec067d..2f2cf74 100755
--- a/tests/rm/d-3
+++ b/tests/rm/d-3
@@ -1,6 +1,6 @@
 #!/bin/sh
-# Ensure that 'rm -d dir' (i.e., without --recursive) gives a reasonable
-# diagnostic when failing.
+# Ensure that 'rm -d -i dir' (i.e., without --recursive) gives a prompt and
+# then deletes the directory if it is empty
 
 # Copyright (C) 2012 Free Software Foundation, Inc.
 
-- 
1.7.11.2


From b4e03b8834fb7c606a0000f1af0666ba52c32bb3 Mon Sep 17 00:00:00 2001
From: Rob Day <robertkday@gmail.com>
Date: Thu, 23 Aug 2012 00:45:57 +0100
Subject: [PATCH 4/5] rm: updated NEWS to include fix of bug #12260

---
 NEWS | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/NEWS b/NEWS
index d8a47ab..257489b 100644
--- a/NEWS
+++ b/NEWS
@@ -9,6 +9,8 @@ GNU coreutils NEWS                                    -*- outline -*-
   it detects this precise type of cycle, diagnoses it as such and
   eventually exits nonzero.
 
+  rm -i -d now prompts the user then removes an empty directory, rather than
+  ignoring the -d option and failing with an 'Is a directory' error.
 
 * Noteworthy changes in release 8.19 (2012-08-20) [stable]
 
-- 
1.7.11.2


From 6c6ca8cf7f11ea41f2013aa06634e119e516744e Mon Sep 17 00:00:00 2001
From: Rob Day <robertkday@gmail.com>
Date: Thu, 23 Aug 2012 08:23:28 +0100
Subject: [PATCH 5/5] rm: improved comments; moved the is_empty bool earlier
 in the code

---
 src/remove.c | 28 ++++++++++++++++------------
 1 file changed, 16 insertions(+), 12 deletions(-)

diff --git a/src/remove.c b/src/remove.c
index 2f9e86b..06bc926 100644
--- a/src/remove.c
+++ b/src/remove.c
@@ -190,6 +190,16 @@ prompt (FTS const *fts, FTSENT const *ent, bool is_dir,
   int dirent_type = is_dir ? DT_DIR : DT_UNKNOWN;
   int write_protected = 0;
 
+  bool is_empty;
+  if (is_empty_p)
+    {
+      is_empty = is_empty_dir (fd_cwd, filename);
+      *is_empty_p = is_empty ? T_YES : T_NO;
+    }
+  else
+    is_empty = false;
+
+
   /* When nonzero, this indicates that we failed to remove a child entry,
      either because the user declined an interactive prompt, or due to
      some other failure, like permissions.  */
@@ -238,9 +248,11 @@ prompt (FTS const *fts, FTSENT const *ent, bool is_dir,
             break;
 
           case DT_DIR:
+             /* Unless we're either deleting directories or deleting
+              * recursively, we want to raise an EISDIR error rather than
+              * prompting the user  */
             if (!x->recursive
-                && !(x->remove_empty_directories
-                     && is_empty_dir (fts->fts_cwd_fd, ent->fts_accpath)))
+                && !(x->remove_empty_directories && is_empty))
               {
                 write_protected = -1;
                 wp_errno = EISDIR;
@@ -256,15 +268,6 @@ prompt (FTS const *fts, FTSENT const *ent, bool is_dir,
           return RM_ERROR;
         }
 
-      bool is_empty;
-      if (is_empty_p)
-        {
-          is_empty = is_empty_dir (fd_cwd, filename);
-          *is_empty_p = is_empty ? T_YES : T_NO;
-        }
-      else
-        is_empty = false;
-
       /* Issue the prompt.  */
       if (dirent_type == DT_DIR
           && mode == PA_DESCEND_INTO_DIR
@@ -422,7 +425,8 @@ rm_fts (FTS *fts, FTSENT *ent, struct rm_options const *x)
         {
           /* This is the first (pre-order) encounter with a directory
              that we cannot delete.
-             Not recursive, so arrange to skip contents.  */
+             Not recursive, and it's not an empty directory (if we're removing
+             them)  so arrange to skip contents.  */
           int err = x->remove_empty_directories ? ENOTEMPTY : EISDIR;
           error (0, err, _("cannot remove %s"), quote (ent->fts_path));
           mark_ancestor_dirs (ent);
-- 
1.7.11.2

