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.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 9742 in the body.
You can then email your comments to 9742 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-coreutils <at> gnu.org:
bug#9742; Package coreutils. (Thu, 13 Oct 2011 01:52:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Morty <morty+coreutils <at> frakir.org>:
New bug report received and forwarded. Copy sent to bug-coreutils <at> gnu.org. (Thu, 13 Oct 2011 01:52:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Morty <morty+coreutils <at> frakir.org>
To: bug-coreutils <at> gnu.org
Subject: touch option for existence?
Date: Wed, 12 Oct 2011 21:45:25 -0400
[This is more of a feature request than a bug request.  But I don't
see where I can make feature requests.]

It would be nice if touch had an option to only "touch" if the file
doesn't already exist.  Sort of like -c, but the other way around.
This is useful because often, the reason one is using "touch" in a
script is because one wants to make sure that a file exists before
doing an operation that expects the file to already exist.  But if the
file already exists, then touch has a side effect of changing the
mtime or the atime.  [On many systems, one can work on a file without
changing the atime thanks to mount options such as noatime or
relatime.]

It's easy enough to wrap touch in an if:

  if [ ! -e $file ]; then touch $file; fi

But it would be nicer if this common case were built in.  I would
suggest -e, for exists.  Thanks!

- Morty




Information forwarded to bug-coreutils <at> gnu.org:
bug#9742; Package coreutils. (Thu, 13 Oct 2011 09:06:02 GMT) Full text and rfc822 format available.

Message #8 received at 9742 <at> debbugs.gnu.org (full text, mbox):

From: Pádraig Brady <P <at> draigBrady.com>
To: Morty <morty+coreutils <at> frakir.org>
Cc: 9742 <at> debbugs.gnu.org
Subject: Re: bug#9742: touch option for existence?
Date: Thu, 13 Oct 2011 10:05:05 +0100
On 10/13/2011 02:45 AM, Morty wrote:
> [This is more of a feature request than a bug request.  But I don't
> see where I can make feature requests.]
> 
> It would be nice if touch had an option to only "touch" if the file
> doesn't already exist.  Sort of like -c, but the other way around.
> This is useful because often, the reason one is using "touch" in a
> script is because one wants to make sure that a file exists before
> doing an operation that expects the file to already exist.  But if the
> file already exists, then touch has a side effect of changing the
> mtime or the atime.  [On many systems, one can work on a file without
> changing the atime thanks to mount options such as noatime or
> relatime.]
> 
> It's easy enough to wrap touch in an if:
> 
>   if [ ! -e $file ]; then touch $file; fi
> 
> But it would be nicer if this common case were built in.  I would
> suggest -e, for exists.  Thanks!

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(),
so you could then do things like:

  until touch -e "$lock_file"; do sleep .1; done

However that functionality is already supported like:

  until mkdir "$lock_dir"; do sleep .1; done

So I'm not sure this is warranted.
I'm 60:40 against currently.

cheers,
Pádraig.




Information forwarded to bug-coreutils <at> gnu.org:
bug#9742; Package coreutils. (Thu, 13 Oct 2011 13:22:01 GMT) Full text and rfc822 format available.

Message #11 received at 9742 <at> debbugs.gnu.org (full text, mbox):

From: Morty <morty+coreutils <at> frakir.org>
To: Pádraig Brady <P <at> draigBrady.com>
Cc: 9742 <at> debbugs.gnu.org, Morty <morty+coreutils <at> frakir.org>
Subject: Re: bug#9742: touch option for existence?
Date: Thu, 13 Oct 2011 09:21:05 -0400
On Thu, Oct 13, 2011 at 10:05:05AM +0100, Pádraig Brady wrote:

> Note the above is easier to express in shell like:
>
>   [ -e "$file" ] || touch "$file"

Thanks, I'm familiar with it.  :)

> But that is racy.  If you were using touch for locking purposes

I'm using touch to make sure the file exists before an operation that
will yield an error if it doesn't exist.  For example:

resolv=/etc/resolv.conf
touch $resolv
ci -l -t-$resolv -m"check-in existing" $resolv
grep -q domain $resolv || echo domain $domain >> $resolv
ci -l -m"add domain $domain" $resolv

The intent of code like the above is to make sure a certain file is in
revision control and contains a certain setting.  If the file already
is in revision control and has the setting, the code should do
nothing.  But touch changes the mtime.  The problem could be fixed by
slinging around ifs, but that's inelegant.

- Morty




Added tag(s) notabug. Request was from Jim Meyering <jim <at> meyering.net> to control <at> debbugs.gnu.org. (Thu, 13 Oct 2011 13:39:02 GMT) Full text and rfc822 format available.

Reply sent to Jim Meyering <jim <at> meyering.net>:
You have taken responsibility. (Thu, 13 Oct 2011 13:39:02 GMT) Full text and rfc822 format available.

Notification sent to Morty <morty+coreutils <at> frakir.org>:
bug acknowledged by developer. (Thu, 13 Oct 2011 13:39:03 GMT) Full text and rfc822 format available.

Message #18 received at 9742-close <at> debbugs.gnu.org (full text, mbox):

From: Jim Meyering <jim <at> meyering.net>
To: Morty <morty+coreutils <at> frakir.org>
Cc: Pádraig Brady <P <at> draigBrady.com>,
	9742-close <at> debbugs.gnu.org
Subject: Re: bug#9742: touch option for existence?
Date: Thu, 13 Oct 2011 15:37:42 +0200
tags 9742 + notabug
thanks

Morty wrote:
> On Thu, Oct 13, 2011 at 10:05:05AM +0100, Pádraig Brady wrote:
>
>> Note the above is easier to express in shell like:
>>
>>   [ -e "$file" ] || touch "$file"
>
> Thanks, I'm familiar with it.  :)
>
>> But that is racy.  If you were using touch for locking purposes
>
> I'm using touch to make sure the file exists before an operation that
> will yield an error if it doesn't exist.  For example:

Thanks for the suggestion.

However, when there's such an easy and portable way to do what you want,

  touch_if_absent() { test -e "$1" || touch "$1"; }

it is counterproductive (and contrary to the "Unix way") to encumber
a tool like "touch" with an option to provide that functionality.
Besides, then you'd have to wait until a release including your
option makes it into whatever distribution you use (or build from source).
With the above, you have something that is portable and works now.

> resolv=/etc/resolv.conf
> touch $resolv
> ci -l -t-$resolv -m"check-in existing" $resolv
> grep -q domain $resolv || echo domain $domain >> $resolv
> ci -l -m"add domain $domain" $resolv
>
> The intent of code like the above is to make sure a certain file is in
> revision control and contains a certain setting.  If the file already
> is in revision control and has the setting, the code should do
> nothing.  But touch changes the mtime.  The problem could be fixed by
> slinging around ifs, but that's inelegant.




Information forwarded to bug-coreutils <at> gnu.org:
bug#9742; Package coreutils. (Thu, 13 Oct 2011 14:10:01 GMT) Full text and rfc822 format available.

Message #21 received at 9742 <at> debbugs.gnu.org (full text, mbox):

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: Re: 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




Information forwarded to bug-coreutils <at> gnu.org:
bug#9742; Package coreutils. (Thu, 13 Oct 2011 15:13:02 GMT) Full text and rfc822 format available.

Message #24 received at 9742 <at> debbugs.gnu.org (full text, mbox):

From: Paul Eggert <eggert <at> cs.ucla.edu>
To: 9742 <at> debbugs.gnu.org, jim <at> meyering.net
Subject: Re: bug#9742: touch option for existence?
Date: Thu, 13 Oct 2011 08:11:53 -0700
Classic "touch" has always bothered me:

  touch -a FOO updates FOO's last-access time.
  touch -m FOO updates FOO's last-modification time.
  touch -a -m FOO updates both times.

Logically, then, "touch FOO", with neither -a nor -m,
should update neither time.  But it doesn't.  It
updates both times.

I know why this is so -- among other things, it's
historical practice and we can't change the above behavior.
Still, there ought to be a way for "touch" to update
neither time, via a GNU extension.  This
would make the behavior of GNU touch more orthogonal.

This is why I have long been thinking of adding a new option that
would mean "neither -a nor -m".  Admittedly it's low priority.




Message #25 received at 9742-close <at> debbugs.gnu.org (full text, mbox):

From: Morty <morty+coreutils <at> frakir.org>
To: Jim Meyering <jim <at> meyering.net>
Cc: Pádraig Brady <P <at> draigBrady.com>,
	Morty <morty+coreutils <at> frakir.org>, 9742-close <at> debbugs.gnu.org
Subject: Re: bug#9742: touch option for existence?
Date: Thu, 13 Oct 2011 11:43:52 -0400
On Thu, Oct 13, 2011 at 03:37:42PM +0200, Jim Meyering wrote:

> Thanks for the suggestion.
> 
> However, when there's such an easy and portable way to do what you want,
> 
>   touch_if_absent() { test -e "$1" || touch "$1"; }
> 
> it is counterproductive (and contrary to the "Unix way") to encumber
> a tool like "touch" with an option to provide that functionality.
> Besides, then you'd have to wait until a release including your
> option makes it into whatever distribution you use (or build from source).
> With the above, you have something that is portable and works now.

Couldn't the same be said of touch's -c option?  An equivalent
function would be almost identical to what you had above:

  touch_if_present() { test -e "$1" && touch "$1"; }

And yet, we have touch -c.

I know touch -e isn't really necessary, but it makes for nice
syntactic sugar.  This is analagous to GNU touch's -c, GNU tar's -z,
GNU grep's -q, or GNU sed's -i.  Just because you can easily script an
equivalent doesn't mean you should have to.  I also know that even if
-e gets put in today, it will be years before it's on enough systems
that we will actually be able to rely on it.  But I've been around
long enough to see new versions of GNU tools gain really useful
options and then become ubiquitous.  "You won't be able to rely on it
for N years" is not a good reason to refrain from doing something
useful.  N years will pass sooner than you realize, and our future
selves will be grateful to our present selves for planning ahead.

- Morty




bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Fri, 11 Nov 2011 12:24:02 GMT) Full text and rfc822 format available.

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

Previous Next


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