touch: Avoid unnecessary call to stat
Now, we first try a utimensat. If it succeeds, we are done, if not (and no -c flag), create the file, then set the times with futimens.
This commit is contained in:
parent
2e5f1281ed
commit
3c12b287ec
19
touch.c
19
touch.c
|
@ -18,25 +18,20 @@ static struct timespec times[2] = {{.tv_nsec = UTIME_NOW}};
|
||||||
static void
|
static void
|
||||||
touch(const char *file)
|
touch(const char *file)
|
||||||
{
|
{
|
||||||
int fd;
|
int fd, ret;
|
||||||
struct stat st;
|
|
||||||
|
|
||||||
if (stat(file, &st) < 0) {
|
if (utimensat(AT_FDCWD, file, times, 0) == 0)
|
||||||
|
return;
|
||||||
if (errno != ENOENT)
|
if (errno != ENOENT)
|
||||||
eprintf("stat %s:", file);
|
eprintf("utimensat %s:", file);
|
||||||
if (cflag)
|
if (cflag)
|
||||||
return;
|
return;
|
||||||
} else {
|
|
||||||
if (utimensat(AT_FDCWD, file, times, 0) < 0)
|
|
||||||
eprintf("utimensat %s:", file);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((fd = open(file, O_WRONLY | O_CREAT | O_EXCL, 0666)) < 0)
|
if ((fd = open(file, O_WRONLY | O_CREAT | O_EXCL, 0666)) < 0)
|
||||||
eprintf("open %s:", file);
|
eprintf("open %s:", file);
|
||||||
|
ret = futimens(fd, times);
|
||||||
close(fd);
|
close(fd);
|
||||||
|
if (ret < 0)
|
||||||
touch(file);
|
eprintf("futimens %s:", file);
|
||||||
}
|
}
|
||||||
|
|
||||||
static time_t
|
static time_t
|
||||||
|
|
Loading…
Reference in New Issue
Block a user