]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
tool_filetime: make -z work with file dates before 1970
authorDaniel Stenberg <daniel@haxx.se>
Fri, 1 Sep 2023 21:46:22 +0000 (23:46 +0200)
committerDaniel Stenberg <daniel@haxx.se>
Sun, 3 Sep 2023 15:45:19 +0000 (17:45 +0200)
Fixes #11785
Reported-by: Harry Sintonen
Closes #11786

src/tool_filetime.c
src/tool_filetime.h
src/tool_getparam.c

index 054d34fe2ef23d9b2ac1f2bd3eaf49a91e669473..9c2e80429b15586fb08d168d9d392e209dc18275 100644 (file)
 #  include <sys/utime.h>
 #endif
 
-curl_off_t getfiletime(const char *filename, struct GlobalConfig *global)
+/* Returns 0 on success, non-zero on file problems */
+int getfiletime(const char *filename, struct GlobalConfig *global,
+                curl_off_t *stamp)
 {
-  curl_off_t result = -1;
+  int rc = 1;
 
 /* Windows stat() may attempt to adjust the unix GMT file time by a daylight
    saving time offset and since it's GMT that is bad behavior. When we have
@@ -52,13 +54,13 @@ curl_off_t getfiletime(const char *filename, struct GlobalConfig *global)
     FILETIME ft;
     if(GetFileTime(hfile, NULL, NULL, &ft)) {
       curl_off_t converted = (curl_off_t)ft.dwLowDateTime
-          | ((curl_off_t)ft.dwHighDateTime) << 32;
+        | ((curl_off_t)ft.dwHighDateTime) << 32;
 
-      if(converted < CURL_OFF_T_C(116444736000000000)) {
+      if(converted < CURL_OFF_T_C(116444736000000000))
         warnf(global, "Failed to get filetime: underflow");
-      }
       else {
-        result = (converted - CURL_OFF_T_C(116444736000000000)) / 10000000;
+        *stamp = (converted - CURL_OFF_T_C(116444736000000000)) / 10000000;
+        rc = 0;
       }
     }
     else {
@@ -76,13 +78,13 @@ curl_off_t getfiletime(const char *filename, struct GlobalConfig *global)
 #else
   struct_stat statbuf;
   if(-1 != stat(filename, &statbuf)) {
-    result = (curl_off_t)statbuf.st_mtime;
+    *stamp = (curl_off_t)statbuf.st_mtime;
+    rc = 0;
   }
-  else if(errno != ENOENT) {
+  else
     warnf(global, "Failed to get filetime: %s", strerror(errno));
-  }
 #endif
-  return result;
+  return rc;
 }
 
 #if defined(HAVE_UTIME) || defined(HAVE_UTIMES) || defined(WIN32)
index 923ec06407be5c4adbce039272256327c0bd9d45..908b2d72bffaf81822f6d8034b779586a1a761bb 100644 (file)
@@ -27,7 +27,8 @@
 
 struct GlobalConfig;
 
-curl_off_t getfiletime(const char *filename, struct GlobalConfig *global);
+int getfiletime(const char *filename, struct GlobalConfig *global,
+                curl_off_t *stamp);
 
 #if defined(HAVE_UTIME) || defined(HAVE_UTIMES) ||      \
   (defined(WIN32) && (SIZEOF_CURL_OFF_T >= 8))
index 2e8d6d7b757ed05b3897f80ef65c21bbe01d7d4e..e4467628d5d4102838e7ebc8a53683952b71af12 100644 (file)
@@ -2648,11 +2648,11 @@ ParameterError getparameter(const char *flag, /* f or -long-flag */
       config->condtime = (curl_off_t)curl_getdate(nextarg, &now);
       if(-1 == config->condtime) {
         /* now let's see if it is a file name to get the time from instead! */
-        curl_off_t filetime = getfiletime(nextarg, global);
-        if(filetime >= 0) {
+        curl_off_t filetime;
+        rc = getfiletime(nextarg, global, &filetime);
+        if(!rc)
           /* pull the time out from the file */
           config->condtime = filetime;
-        }
         else {
           /* failed, remove time condition */
           config->timecond = CURL_TIMECOND_NONE;