From dd22da8e9539cc88193987b6997769ae4ede2b15 Mon Sep 17 00:00:00 2001 From: Rob Day Date: Wed, 22 Aug 2012 23:04:19 +0100 Subject: 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 --- src/remove.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) (limited to 'src') 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); -- cgit v1.2.3-70-g09d2