Subject: bug#16669: inconsistent 'rm -ir' prompting behavior

From: Eric Blake <[email-masked]>
Date: Thu, 06 Feb 2014 09:39:10 -0700

On today's Austin Group call, we discussed
http://austingroupbugs.net/view.php?id=802 regarding 'rm' behavior.

They pointed out that GNU has an optimization not allowed by a strict
reading of the current standard:

$ mkdir foo
$ rm -ir foo
rm: remove directory ‘foo’?

Whether you answer yes or no, the point remains: rm only prompted once
about removing the empty directory.  Compare that to other
implementations, such as Solaris:

$ rm -ir /tmp/foo
rm: examine files in directory /tmp/foo (yes/no)? y
rm: remove /tmp/foo (yes/no)? y
$

Note that the optimization implies that we did a readdir() on the
directory before deciding whether to prompt, in order to learn if it was
empty; and that readdir() modifies directory atime; in the Solaris
implementation, readdir() is not even attempted until after a positive
prompt response, which means directory atime is unchanged if the user
chooses not to descend.

Meanwhile, for a non-empty directory, we DO prompt twice for the directory:

$ touch foo/bar
$ rm -ir foo
rm: descend into directory ‘foo’? y
rm: remove regular empty file ‘foo/bar’? y
rm: remove directory ‘foo’?

Here, we get the POSIX-mandated double prompting, once to descend, and
once after recursion is complete to see whether to remove the now-empty
directory.  The argument is whether the GNU optimization of prompting
only once for an empty directory violates POSIX (and where it is
observable by a new directory atime), or whether we should patch POSIX
to allow the GNU optimization.  In the meeting, I ended up with an
action item to right the bug report against POSIX to propose wording
that would allow the GNU behavior.

Conversely, consider:

$ rm -ir foo
rm: descend into directory ‘foo’? y
rm: remove regular empty file ‘foo/bar’? n
rm: remove directory ‘foo’? y
rm: cannot remove ‘foo’: Directory not empty

Why on earth are we prompting to remove 'foo' when we KNOW it is
non-empty because the user specifically asked to not remove foo/bar?  If
we can optimize from two prompts down to one for the empty directory
case, why are we not optimizing and avoiding asking a useless prompt for
a known non-empty directory?  So, while writing my POSIX bug report,
should I also allow for an optimization of omitting the second prompt
for a known non-empty directory (known because of a negative answer to
prompts on its children), even though GNU does not yet implement that
optimization?

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org