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:
parent
5c95e6ff60
commit
401c9fda42
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=230979
@ -31,7 +31,7 @@
|
|||||||
.\" @(#)touch.1 8.3 (Berkeley) 4/28/95
|
.\" @(#)touch.1 8.3 (Berkeley) 4/28/95
|
||||||
.\" $FreeBSD$
|
.\" $FreeBSD$
|
||||||
.\"
|
.\"
|
||||||
.Dd April 28, 1995
|
.Dd February 4, 2012
|
||||||
.Dt TOUCH 1
|
.Dt TOUCH 1
|
||||||
.Os
|
.Os
|
||||||
.Sh NAME
|
.Sh NAME
|
||||||
@ -40,7 +40,7 @@
|
|||||||
.Sh SYNOPSIS
|
.Sh SYNOPSIS
|
||||||
.Nm
|
.Nm
|
||||||
.Op Fl A Ar [-][[hh]mm]SS
|
.Op Fl A Ar [-][[hh]mm]SS
|
||||||
.Op Fl acfhm
|
.Op Fl achm
|
||||||
.Op Fl r Ar file
|
.Op Fl r Ar file
|
||||||
.Op Fl t Ar [[CC]YY]MMDDhhmm[.SS]
|
.Op Fl t Ar [[CC]YY]MMDDhhmm[.SS]
|
||||||
.Ar
|
.Ar
|
||||||
@ -109,9 +109,6 @@ The
|
|||||||
.Nm
|
.Nm
|
||||||
utility does not treat this as an error.
|
utility does not treat this as an error.
|
||||||
No error messages are displayed and the exit value is not affected.
|
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
|
.It Fl h
|
||||||
If the file is a symbolic link, change the times of the link
|
If the file is a symbolic link, change the times of the link
|
||||||
itself rather than the file that the link points to.
|
itself rather than the file that the link points to.
|
||||||
|
@ -55,7 +55,6 @@ static const char sccsid[] = "@(#)touch.c 8.1 (Berkeley) 6/6/93";
|
|||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
int rw(char *, struct stat *, int);
|
|
||||||
void stime_arg1(char *, struct timeval *);
|
void stime_arg1(char *, struct timeval *);
|
||||||
void stime_arg2(char *, int, struct timeval *);
|
void stime_arg2(char *, int, struct timeval *);
|
||||||
void stime_file(char *, struct timeval *);
|
void stime_file(char *, struct timeval *);
|
||||||
@ -69,12 +68,12 @@ main(int argc, char *argv[])
|
|||||||
struct timeval tv[2];
|
struct timeval tv[2];
|
||||||
int (*stat_f)(const char *, struct stat *);
|
int (*stat_f)(const char *, struct stat *);
|
||||||
int (*utimes_f)(const char *, const struct timeval *);
|
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 *p;
|
||||||
char *myname;
|
char *myname;
|
||||||
|
|
||||||
myname = basename(argv[0]);
|
myname = basename(argv[0]);
|
||||||
Aflag = aflag = cflag = fflag = mflag = timeset = 0;
|
Aflag = aflag = cflag = mflag = timeset = 0;
|
||||||
stat_f = stat;
|
stat_f = stat;
|
||||||
utimes_f = utimes;
|
utimes_f = utimes;
|
||||||
if (gettimeofday(&tv[0], NULL))
|
if (gettimeofday(&tv[0], NULL))
|
||||||
@ -92,7 +91,7 @@ main(int argc, char *argv[])
|
|||||||
cflag = 1;
|
cflag = 1;
|
||||||
break;
|
break;
|
||||||
case 'f':
|
case 'f':
|
||||||
fflag = 1;
|
/* No-op for compatibility. */
|
||||||
break;
|
break;
|
||||||
case 'h':
|
case 'h':
|
||||||
cflag = 1;
|
cflag = 1;
|
||||||
@ -222,14 +221,8 @@ main(int argc, char *argv[])
|
|||||||
if (!utimes_f(*argv, NULL))
|
if (!utimes_f(*argv, NULL))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
/* Try reading/writing. */
|
rval = 1;
|
||||||
if (!S_ISLNK(sb.st_mode) && !S_ISDIR(sb.st_mode)) {
|
warn("%s", *argv);
|
||||||
if (rw(*argv, &sb, fflag))
|
|
||||||
rval = 1;
|
|
||||||
} else {
|
|
||||||
rval = 1;
|
|
||||||
warn("%s", *argv);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
exit(rval);
|
exit(rval);
|
||||||
}
|
}
|
||||||
@ -368,59 +361,10 @@ stime_file(char *fname, struct timeval *tvp)
|
|||||||
TIMESPEC_TO_TIMEVAL(tvp + 1, &sb.st_mtim);
|
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
|
void
|
||||||
usage(char *myname)
|
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);
|
"[-t [[CC]YY]MMDDhhmm[.SS]] file ...\n", myname);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user