[hackers] [sbase] touch: Avoid unnecessary call to stat || Michael Forney

From: <git_AT_suckless.org>
Date: Sat, 9 Jul 2016 11:17:21 +0200 (CEST)

commit 3c12b287ec819006593db1011668e1279d787459
Author: Michael Forney <mforney_AT_mforney.org>
AuthorDate: Fri Jul 8 10:24:14 2016 -0700
Commit: sin <sin_AT_2f30.org>
CommitDate: Sat Jul 9 10:17:16 2016 +0100

    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.

diff --git a/touch.c b/touch.c
index a187241..6e63bf8 100644
--- a/touch.c
+++ b/touch.c
_AT_@ -18,25 +18,20 @@ static struct timespec times[2] = {{.tv_nsec = UTIME_NOW}};
 static void
 touch(const char *file)
 {
- int fd;
- struct stat st;
+ int fd, ret;
 
- if (stat(file, &st) < 0) {
- if (errno != ENOENT)
- eprintf("stat %s:", file);
- if (cflag)
- return;
- } else {
- if (utimensat(AT_FDCWD, file, times, 0) < 0)
- eprintf("utimensat %s:", file);
+ if (utimensat(AT_FDCWD, file, times, 0) == 0)
+ return;
+ if (errno != ENOENT)
+ eprintf("utimensat %s:", file);
+ if (cflag)
                 return;
- }
-
         if ((fd = open(file, O_WRONLY | O_CREAT | O_EXCL, 0666)) < 0)
                 eprintf("open %s:", file);
+ ret = futimens(fd, times);
         close(fd);
-
- touch(file);
+ if (ret < 0)
+ eprintf("futimens %s:", file);
 }
 
 static time_t
Received on Sat Jul 09 2016 - 11:17:21 CEST

This archive was generated by hypermail 2.3.0 : Sat Jul 09 2016 - 11:24:40 CEST