Subject: bug#12339: Bug: rm -fr . doesn't dir depth first deletion yet it is documented to do so.

From: Jim Meyering <[email-masked]>
Date: Thu, 06 Sep 2012 08:56:20 +0200

Linda Walsh wrote:
> This doesn't check POSIX_CORRECTLY, but it does fix the reported problem
>
> rm -fr in . removes all files and leaves the dir:
>
> Ishtar:/tmp/ttt> touch one two three
> Ishtar:/tmp/ttt> cd ..
> Ishtar:/tmp> rm -fr ttt/.
> Ishtar:/tmp> ll ttt
> total 0
>
> ---
> Also works cd'd into the dir and doing an "rm -fr ."
>
> I'll be happy to put in the check for POSIX_CORRECTLY if you are amenable...
> Note.. am not claiming this is the most efficient way to do it, but it
> seemed to be the least impact on code I was new to.
>
> Ishtar:packages/sources/coreutils-8.14> cat coreutils-8.14.remove.c.diff
> --- src/remove.c        2011-10-10 00:56:46.000000000 -0700
> +++ src/remove.c        2012-09-05 18:23:58.449319142 -0700
> @@ -446,6 +446,20 @@
>    return RM_ERROR;
>  }
>
> +
> +static inline bool
> +dotdot (char const *file_name)
> +{
> +  if (file_name[0] == '.' && file_name[1])
> +    {
> +      char sep = file_name[(file_name[1] == '.') + 1];
> +      return (! sep || ISSLASH (sep));
> +    }
> +  else
> +    return false;
> +}
> +
> +
>  /* This function is called once for every file system object that fts
>     encounters.  fts performs a depth-first traversal.
>     A directory is usually processed twice, first with fts_info == FTS_D,
> @@ -476,7 +490,7 @@
>
>            /* If the basename of a command line argument is "." or "..",
>               diagnose it and do nothing more with that argument.  */
> -          if (dot_or_dotdot (last_component (ent->fts_accpath)))
> +          if (dotdot (last_component (ent->fts_accpath)))
>              {
>                error (0, 0, _("cannot remove directory: %s"),
>                       quote (ent->fts_path));

Thanks for the patch, but it would be pretty rotten for GNU rm to make
it so "rm -rf ." deletes everything under ".", while all other vendor
rm programs diagnose the POSIX-mandated error.  People would curse us
for making GNU rm remove their precious files when they accidentally ran
that command.  Even if that were done only when POSIX_CORRECTLY were not
set, they would still be burned.  Very few people ever set that envvar.