Remove useless and potentially dangerous rw() function which tries to

update access and modification times by reading and writing the file.
chmod(2) in rw() doesn't help because utimes(2) allow owner and the
super-user to change times. Using just utimes(2) should be sufficient.

The -f option becomes no-op.

Reviewed by:	jilles
This commit is contained in:
Jaakko Heinonen 2012-02-04 13:37:31 +00:00
parent 5c95e6ff60
commit 401c9fda42
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=230979
2 changed files with 8 additions and 67 deletions

View File

@ -31,7 +31,7 @@
.\" @(#)touch.1 8.3 (Berkeley) 4/28/95
.\" $FreeBSD$
.\"
.Dd April 28, 1995
.Dd February 4, 2012
.Dt TOUCH 1
.Os
.Sh NAME
@ -40,7 +40,7 @@
.Sh SYNOPSIS
.Nm
.Op Fl A Ar [-][[hh]mm]SS
.Op Fl acfhm
.Op Fl achm
.Op Fl r Ar file
.Op Fl t Ar [[CC]YY]MMDDhhmm[.SS]
.Ar
@ -109,9 +109,6 @@ The
.Nm
utility does not treat this as an error.
No error messages are displayed and the exit value is not affected.
.It Fl f
Attempt to force the update, even if the file permissions do not
currently permit it.
.It Fl h
If the file is a symbolic link, change the times of the link
itself rather than the file that the link points to.

View File

@ -55,7 +55,6 @@ static const char sccsid[] = "@(#)touch.c 8.1 (Berkeley) 6/6/93";
#include <time.h>
#include <unistd.h>
int rw(char *, struct stat *, int);
void stime_arg1(char *, struct timeval *);
void stime_arg2(char *, int, struct timeval *);
void stime_file(char *, struct timeval *);
@ -69,12 +68,12 @@ main(int argc, char *argv[])
struct timeval tv[2];
int (*stat_f)(const char *, struct stat *);
int (*utimes_f)(const char *, const struct timeval *);
int Aflag, aflag, cflag, fflag, mflag, ch, fd, len, rval, timeset;
int Aflag, aflag, cflag, mflag, ch, fd, len, rval, timeset;
char *p;
char *myname;
myname = basename(argv[0]);
Aflag = aflag = cflag = fflag = mflag = timeset = 0;
Aflag = aflag = cflag = mflag = timeset = 0;
stat_f = stat;
utimes_f = utimes;
if (gettimeofday(&tv[0], NULL))
@ -92,7 +91,7 @@ main(int argc, char *argv[])
cflag = 1;
break;
case 'f':
fflag = 1;
/* No-op for compatibility. */
break;
case 'h':
cflag = 1;
@ -222,14 +221,8 @@ main(int argc, char *argv[])
if (!utimes_f(*argv, NULL))
continue;
/* Try reading/writing. */
if (!S_ISLNK(sb.st_mode) && !S_ISDIR(sb.st_mode)) {
if (rw(*argv, &sb, fflag))
rval = 1;
} else {
rval = 1;
warn("%s", *argv);
}
rval = 1;
warn("%s", *argv);
}
exit(rval);
}
@ -368,59 +361,10 @@ stime_file(char *fname, struct timeval *tvp)
TIMESPEC_TO_TIMEVAL(tvp + 1, &sb.st_mtim);
}
int
rw(char *fname, struct stat *sbp, int force)
{
int fd, needed_chmod, rval;
u_char byte;
/* Try regular files. */
if (!S_ISREG(sbp->st_mode)) {
warnx("%s: %s", fname, strerror(EFTYPE));
return (1);
}
needed_chmod = rval = 0;
if ((fd = open(fname, O_RDWR, 0)) == -1) {
if (!force || chmod(fname, DEFFILEMODE))
goto err;
if ((fd = open(fname, O_RDWR, 0)) == -1)
goto err;
needed_chmod = 1;
}
if (sbp->st_size != 0) {
if (read(fd, &byte, sizeof(byte)) != sizeof(byte))
goto err;
if (lseek(fd, (off_t)0, SEEK_SET) == -1)
goto err;
if (write(fd, &byte, sizeof(byte)) != sizeof(byte))
goto err;
} else {
if (write(fd, &byte, sizeof(byte)) != sizeof(byte)) {
err: rval = 1;
warn("%s", fname);
} else if (ftruncate(fd, (off_t)0)) {
rval = 1;
warn("%s: file modified", fname);
}
}
if (close(fd) && rval != 1) {
rval = 1;
warn("%s", fname);
}
if (needed_chmod && chmod(fname, sbp->st_mode) && rval != 1) {
rval = 1;
warn("%s: permissions modified", fname);
}
return (rval);
}
void
usage(char *myname)
{
fprintf(stderr, "usage:\n" "%s [-A [-][[hh]mm]SS] [-acfhm] [-r file] "
fprintf(stderr, "usage:\n" "%s [-A [-][[hh]mm]SS] [-achm] [-r file] "
"[-t [[CC]YY]MMDDhhmm[.SS]] file ...\n", myname);
exit(1);
}