Subject: bug#9742: touch option for existence?

From: Eric Blake <[email-masked]>
Date: Thu, 13 Oct 2011 08:09:13 -0600

On 10/13/2011 03:05 AM, Pádraig Brady wrote:
> Note the above is easier to express in shell like:
>
>    [ -e "$file" ] || touch "$file"
>
> But that is racy.  If you were using touch for locking purposes
> then adding -e (must create), would allow one to add O_EXCL to the open(),

You can avoid that race, while still accomplishing your goal of creating 
the file if it does not exist, all without corrupting timestamps if it 
does exist, by using straight shell:

(set -C; : > "$file") 2>/dev/null || :

-- 
Eric Blake   [email-masked]    +1-801-349-2682
Libvirt virtualization library http://libvirt.org