Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 28 Apr 2007 19:11:50 GMT
From:      Jan Schaumann<jschauma@netmeister.org>
To:        freebsd-gnats-submit@FreeBSD.org
Subject:   misc/112212: touch(1)ing a directory and failing yields return code 0
Message-ID:  <200704281911.l3SJBoLT048085@www.freebsd.org>
Resent-Message-ID: <200704281920.l3SJK4oL050751@freefall.freebsd.org>

next in thread | raw e-mail | index | archive | help

>Number:         112212
>Category:       misc
>Synopsis:       touch(1)ing a directory and failing yields return code 0
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Sat Apr 28 19:20:04 GMT 2007
>Closed-Date:
>Last-Modified:
>Originator:     Jan Schaumann
>Release:        
>Organization:
>Environment:
>Description:
When using touch(1) on a directory that I can't update the timestamp on (say, if the filesystem is mounted read-only), it will return 0 as the return value, even though it failed.

The reason this happens is in touch.c#225:

    /* Try reading/writing. */
    if (!S_ISLNK(sb.st_mode) && !S_ISDIR(sb.st_mode) &&
        rw(*argv, &sb, fflag))
            rval = 1;
    else
            warn("%s", *argv);

At this point, it tries to update the timestamp using utimes(2), which
failed, so it would continue to try to update it by reading and writing
the file.

However, since the file in question is a directory, it doesn't try this
and simply warns instead of setting the return value to 1.

>How-To-Repeat:
$ mkdir foo
$ touch foo/bar
$ mount -u -o ro /
$ touch foo/bar
touch: foo/bar: Read-only file system
$ echo $?
1
$ touch foo
touch: foo: Read-only file system
$ echo $?
0
$
>Fix:
See NetBSD's touch(1):

                /* Try reading/writing. */
                if (!S_ISLNK(sb.st_mode) && rw(*argv, &sb, fflag))
                        rval = 1;


This still is slightly suboptimal, since the error message will be

$ touch foo
touch: foo: Is a directory
$ echo $?
1
$ 

But that's better than returning 0.
>Release-Note:
>Audit-Trail:
>Unformatted:



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200704281911.l3SJBoLT048085>