summaryrefslogtreecommitdiff
path: root/src/remove.c
diff options
context:
space:
mode:
authorRob Day <robertkday@gmail.com>2012-08-22 23:04:19 +0100
committerJim Meyering <meyering@redhat.com>2012-08-23 20:53:04 +0200
commitdd22da8e9539cc88193987b6997769ae4ede2b15 (patch)
tree630d3469f4d7710c3252682a51f495eba1846924 /src/remove.c
parentfadf9c525787f063e6fe84bdd5566e53a622c1ce (diff)
downloadcoreutils-dd22da8e9539cc88193987b6997769ae4ede2b15.tar.xz
rm: fix the new --dir (-d) option to work with -i
* src/remove.c (prompt): Hoist the computation of is_empty, since we'll need it slightly earlier. Before, this function would arrange to fail with EISDIR when processing a directory without --recursive (-r). Adjust the condition to exempt an empty directory when --dir has been specified. Improve comments. * tests/rm/d-3: New file, to ensure that rm -d -i dir works. * tests/Makefile.am (TESTS): Add it. * NEWS (Bug fixes): Mention it. * THANKS.in: Update. Reported by Michael Price in http://bugs.gnu.org/12260
Diffstat (limited to 'src/remove.c')
-rw-r--r--src/remove.c24
1 files changed, 13 insertions, 11 deletions
diff --git a/src/remove.c b/src/remove.c
index c4972ace2..69faae6de 100644
--- a/src/remove.c
+++ b/src/remove.c
@@ -190,6 +190,13 @@ 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 = false;
+ if (is_empty_p)
+ {
+ is_empty = is_empty_dir (fd_cwd, filename);
+ *is_empty_p = is_empty ? T_YES : T_NO;
+ }
+
/* 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,7 +245,10 @@ prompt (FTS const *fts, FTSENT const *ent, bool is_dir,
break;
case DT_DIR:
- if (!x->recursive)
+ /* 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)))
{
write_protected = -1;
wp_errno = EISDIR;
@@ -254,15 +264,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
@@ -420,7 +421,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);