Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: [Patch] No warnings for `rm /*`



On Tue, Apr 26, 2016 at 7:36 PM, Bart Schaefer
<schaefer@xxxxxxxxxxxxxxxx> wrote:
> On Apr 26, 10:31am, Glenn Smith wrote:
> } Subject: [Patch] No warnings for `rm /*`
> }
> } I discovered that zsh warns in every case except `rm /*`.
>
> Thanks.  I think the following simpler patch does the same.
>
> diff --git a/Src/exec.c b/Src/exec.c
> index 50eff72..2dcd5bc 100644
> --- a/Src/exec.c
> +++ b/Src/exec.c
> @@ -2902,11 +2902,11 @@ execcmd(Estate state, int input, int output, int how, int last1)
>             if (s[0] == Star && !s[1]) {
>                 if (!checkrmall(pwd))
>                     uremnode(args, node);
> -           } else if (l > 2 && s[l - 2] == '/' && s[l - 1] == Star) {
> +           } else if (l >= 2 && s[l - 2] == '/' && s[l - 1] == Star) {
>                 char t = s[l - 2];
>
>                 s[l - 2] = 0;
> -               if (!checkrmall(s))
> +               if (!checkrmall(*s ? s : "/"))
>                     uremnode(args, node);
>                 s[l - 2] = t;
>             }
>
>
> As an aside:
>
> % rm /nonexistentdir/*
> zsh: sure you want to delete all the files in /nonexistentdir [yn]?
>
> Seems like we could detect when opendir() fails with ENOENT and skip
> the prompt, but maybe there are cases of race condition (directory is
> created after I type "rm ..." but before /bin/rm is executed?) where
> the default answer of "y" would be incorrect.

This is pretty contrived, but it's not even a race condition.

% mkdir /tmp/r; rm /tmp/r/.(e:'echo hello; touch $REPLY/foo:') /tmp/r/*
zsh: sure you want to delete all the files in /tmp/r [yn]? y
hello
rm: cannot remove '/tmp/r/.': Is a directory
removed '/tmp/r/foo'

-- 
Mikael Magnusson



Messages sorted by: Reverse Date, Date, Thread, Author