touch(1)ing a directory and failing yields return code 0

Jan Schaumann <[email protected]>
Newsgroups gmane.os.freebsd.bugbusters
Message-ID <[email protected]>
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.

$ 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
$

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.

-- 
Probability factor of one to one. We have normality. I repeat, we have 
normality. Anything you still can't cope with is therefore your own lookout.
signature.asc (application/pgp-signature, 186 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (NetBSD)

iD8DBQFGM51NfFtkr68iakwRAv12AKDx50xVD9WjKzuBYs1ZrOKT54urNwCdFQnP
2+ZB1wezbGtRAOQAgIAWPdw=
=KYYY
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.