GNU bug report logs - #9742
touch option for existence?

Previous Next

Package: coreutils;

Reported by: Morty <morty+coreutils <at> frakir.org>

Date: Thu, 13 Oct 2011 01:52:01 UTC

Severity: normal

Tags: notabug

Done: Jim Meyering <jim <at> meyering.net>

Bug is archived. No further changes may be made.

Full log


View this message in rfc822 format

From: Eric Blake <eblake <at> redhat.com>
To: Pádraig Brady <P <at> draigBrady.com>
Cc: 9742 <at> debbugs.gnu.org, Morty <morty+coreutils <at> frakir.org>
Subject: bug#9742: touch option for existence?
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   eblake <at> redhat.com    +1-801-349-2682
Libvirt virtualization library http://libvirt.org




This bug report was last modified 13 years and 228 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.