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

Re: Any way to allow clobbering empty files when noclobber is set?



Bart Schaefer wrote on Thu, 04 Jun 2020 13:35 -0700:
> On Thu, Jun 4, 2020 at 5:16 AM Peter Stephenson
> <p.w.stephenson@xxxxxxxxxxxx> wrote:
> >
> > One thing I missed is that we already open the file and run fstat to
> > check if it's regular.  We can simply check if it's empty at the
> > same point  
> 
> Exactly where my earlier question came from.
> 
> > +           if (isset(CLOBBEREMPTY) && buf.st_size == 0)
> > +           {
> > +               close(fd);
> > +               return open(ufname, O_WRONLY | O_CREAT | O_TRUNC | O_NOCTTY,
> > +                           0666);
> > +           }  
> 
> Considering the concurrent-openers situation that Roman mentioned, I'm
> debating whether there is any benefit to doing:
> 
>   int newfd = open(ufname, O_WRONLY | O_CREAT | O_TRUNC | O_NOCTTY, 0666);
>   close(fd);
>   return newfd;
> 
> I have a vague sense that keeping the descriptor open until the new
> file is created might prevent some races, but I can't recite an
> example.

This wouldn't prevent races, because the filename might be unlinked
between the first and second open(2) calls.

I think the right fix here is to simply change the body of the quoted
if statement to «return fd».  What do we gain by re-opening the file
with the O_TRUNC flag set when we already know the file is
zero-length?  It seems to me we're just introducing a race condition
for no reason.



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