Subject: bug#11100: Racy code in copy.c

From: Paul Eggert <[email-masked]>
Date: Wed, 28 Mar 2012 11:26:32 -0700

On 03/28/2012 09:07 AM, Jim Meyering wrote:
>>        if ((use_stat
>> > -           ? stat (dst_name, &dst_sb)
>> > +           ? (stat (dst_name, &dst_sb) < 0 ? -1 :
>> > +	      (fd = open (dst_name, O_WRONLY)) < 0 ? -1 : 0)
>> >             : lstat (dst_name, &dst_sb))
>> >            != 0)
> At first glance, that might be reasonable: the additional open
> is incurred only after a failed stat.
> I'll look more closely in a week or two if no one else investigates.

Come to think of it, wouldn't it be more efficient to
do an open (dst_name, O_WRONLY | O_BINARY), and then
fstat the resulting fd, falling back on 'stat' only if the
open fails with errno == EACCES?   That should be
more efficient in the usual case, since it'd resolve
the file name fewer times in the usual case.