bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: use lutimens


From: Eric Blake
Subject: Re: use lutimens
Date: Fri, 9 Oct 2009 16:13:49 +0000 (UTC)
User-agent: Loom/3.14 (http://gmane.org/)

Eric Blake <ebb9 <at> byu.net> writes:

> This patch is pending my utimensat work on gnulib, but looks pretty slick; 
> replacing a #if and 12 lines of code with just 1 (modulo the testsuite tweak).

Next cool patch, also pending on my gnulib utimensat changes.  Again, a net 
reduction in code size, plus the added benefit of fewer syscalls for 'touch -a' 
in the case where native utimensat works.

Hmm.  I just realized that I need to add $(LIB_CLOCK_GETTIME) to Makefile.am 
for anyone that uses utimens, now that I'm changing gnulib's utimens to call 
gettime() when falling back to utimes().  Followup patch to come to make that 
easier to do.


From: Eric Blake <address@hidden>
Date: Fri, 9 Oct 2009 06:56:03 -0600
Subject: [PATCH] touch: optimize use of utimens

* src/touch.c (main): Use UTIME_NOW rather than calling gettime.
(touch): Use UTIME_OMIT rather than stat.
---
 src/touch.c |   46 ++++++++++------------------------------------
 1 files changed, 10 insertions(+), 36 deletions(-)

diff --git a/src/touch.c b/src/touch.c
index d7ae9b6..b4c45a1 100644
--- a/src/touch.c
+++ b/src/touch.c
@@ -22,6 +22,7 @@
 #include <stdio.h>
 #include <getopt.h>
 #include <sys/types.h>
+#include <assert.h>

 #include "system.h"
 #include "argmatch.h"
@@ -119,11 +120,9 @@ static bool
 touch (const char *file)
 {
   bool ok;
-  struct stat sbuf;
   int fd = -1;
   int open_errno = 0;
-  struct timespec timespec[2];
-  struct timespec const *t;
+  struct timespec const *t = newtime;

   if (STREQ (file, "-"))
     fd = STDOUT_FILENO;
@@ -144,24 +143,13 @@ touch (const char *file)

   if (change_times != (CH_ATIME | CH_MTIME))
     {
-      /* We're setting only one of the time values.  stat the target to get
-         the other one.  If we have the file descriptor already, use fstat.
-         Otherwise, either we're in no-create mode (and hence didn't call open)
-         or FILE is inaccessible or a directory, so we have to use stat.  */
-      if (fd != -1 ? fstat (fd, &sbuf) : stat (file, &sbuf))
+      /* We're setting only one of the time values.  */
+      if (change_times == CH_MTIME)
+        newtime[0].tv_nsec = UTIME_OMIT;
+      else
         {
-          if (open_errno)
-            error (0, open_errno, _("creating %s"), quote (file));
-          else
-            {
-              if (no_create && (errno == ENOENT || errno == EBADF))
-                return true;
-              error (0, errno, _("failed to get attributes of %s"),
-                     quote (file));
-            }
-          if (fd == STDIN_FILENO)
-            close (fd);
-          return false;
+          assert (change_times == CH_ATIME);
+          newtime[1].tv_nsec = UTIME_OMIT;
         }
     }

@@ -171,16 +159,6 @@ touch (const char *file)
          write access to the file, but don't own it.  */
       t = NULL;
     }
-  else
-    {
-      timespec[0] = (change_times & CH_ATIME
-                     ? newtime[0]
-                     : get_stat_atime (&sbuf));
-      timespec[1] = (change_times & CH_MTIME
-                     ? newtime[1]
-                     : get_stat_mtime (&sbuf));
-      t = timespec;
-    }

   ok = (gl_futimens (fd, (fd == STDOUT_FILENO ? NULL : file), t) == 0);

@@ -195,8 +173,7 @@ touch (const char *file)
   else if (fd == STDOUT_FILENO)
     {
       /* Do not diagnose "touch -c - >&-".  */
-      if (!ok && errno == EBADF && no_create
-          && change_times == (CH_ATIME | CH_MTIME))
+      if (!ok && errno == EBADF && no_create)
         return true;
     }

@@ -429,10 +406,7 @@ main (int argc, char **argv)
       if (change_times == (CH_ATIME | CH_MTIME))
         amtime_now = true;
       else
-        {
-          gettime (&newtime[0]);
-          newtime[1] = newtime[0];
-        }
+        newtime[1].tv_nsec = newtime[0].tv_nsec = UTIME_NOW;
     }

   if (optind == argc)
-- 
1.6.4.2








reply via email to

[Prev in Thread] Current Thread [Next in Thread]