Blame SOURCES/0017-Fix-an-issue-with-timestamps-during-DST.patch

d7fdbd
From 5ec0b86e5c1ff060720b5a6cd1af9d93ec993650 Mon Sep 17 00:00:00 2001
d7fdbd
From: Martin Sehnoutka <msehnout@redhat.com>
d7fdbd
Date: Thu, 29 Sep 2016 11:14:03 +0200
d7fdbd
Subject: [PATCH 17/59] Fix an issue with timestamps during DST.
d7fdbd
d7fdbd
vsftpd now checks whether a file was uploaded during DST and
d7fdbd
adjust the timestamp accordingly.
d7fdbd
---
d7fdbd
 sysutil.c | 104 ++++++++++++++++++++++++++++++++++++++++++++++----------------
d7fdbd
 1 file changed, 77 insertions(+), 27 deletions(-)
d7fdbd
d7fdbd
diff --git a/sysutil.c b/sysutil.c
d7fdbd
index c848356..2abdd13 100644
d7fdbd
--- a/sysutil.c
d7fdbd
+++ b/sysutil.c
d7fdbd
@@ -26,8 +26,10 @@
d7fdbd
 /* For Linux, this adds nothing :-) */
d7fdbd
 #include "port/porting_junk.h"
d7fdbd
 
d7fdbd
+#define F_LOCALTIME "/etc/localtime"
d7fdbd
+#define BUFTZSIZ 64
d7fdbd
+
d7fdbd
 #include <signal.h>
d7fdbd
-#include <string.h>
d7fdbd
 #include <stdlib.h>
d7fdbd
 #include <unistd.h>
d7fdbd
 #include <sys/types.h>
d7fdbd
@@ -56,6 +58,11 @@
d7fdbd
 #include <netdb.h>
d7fdbd
 #include <sys/resource.h>
d7fdbd
 
d7fdbd
+#ifndef __USE_GNU
d7fdbd
+  #define __USE_GNU
d7fdbd
+#endif
d7fdbd
+#include <string.h>
d7fdbd
+
d7fdbd
 /* Private variables to this file */
d7fdbd
 /* Current umask() */
d7fdbd
 static unsigned int s_current_umask;
d7fdbd
@@ -2574,49 +2581,92 @@ error:
d7fdbd
   die("reopening standard file descriptors to /dev/null failed");
d7fdbd
 }
d7fdbd
 
d7fdbd
+char* vsf_sysutil_get_tz()
d7fdbd
+{
d7fdbd
+  char *ret_tz = NULL;
d7fdbd
+  char buff[BUFTZSIZ];
d7fdbd
+  off_t s_pos, e_pos;
d7fdbd
+  size_t rcnt, rest;
d7fdbd
+  int fd;
d7fdbd
+
d7fdbd
+  if ((fd = open(F_LOCALTIME, O_RDONLY)) > -1)
d7fdbd
+  {
d7fdbd
+    if ((e_pos = lseek(fd, 0, SEEK_END)) <= 0)
d7fdbd
+    {
d7fdbd
+      close(fd);
d7fdbd
+      return NULL;
d7fdbd
+    }
d7fdbd
+    s_pos = e_pos > BUFTZSIZ ? e_pos - BUFTZSIZ : 0;
d7fdbd
+    lseek(fd, s_pos, SEEK_SET);
d7fdbd
+    rcnt = read(fd, buff, BUFTZSIZ);
d7fdbd
+
d7fdbd
+    if (rcnt && buff[rcnt-1] == '\n')
d7fdbd
+    {
d7fdbd
+      buff[rcnt-1] = 0;
d7fdbd
+      e_pos--;
d7fdbd
+    }
d7fdbd
+
d7fdbd
+    do {
d7fdbd
+       char *nl = memrchr(buff, '\n', rcnt);
d7fdbd
+       if (rcnt && nl)
d7fdbd
+       {
d7fdbd
+         int offset = (++nl) - buff;
d7fdbd
+         int len = e_pos - s_pos - offset;
d7fdbd
+         if (len)
d7fdbd
+         {
d7fdbd
+           lseek(fd, s_pos + offset, SEEK_SET);
d7fdbd
+           ret_tz = calloc(1, len+4);
d7fdbd
+           memcpy(ret_tz, "TZ=", 3);
d7fdbd
+           rcnt = read(fd, ret_tz+3, len);
d7fdbd
+         }
d7fdbd
+         break;
d7fdbd
+       }
d7fdbd
+       if (!s_pos)
d7fdbd
+       {
d7fdbd
+         break;
d7fdbd
+       }
d7fdbd
+       rest = s_pos > BUFTZSIZ ? s_pos - BUFTZSIZ : 0;
d7fdbd
+       s_pos -= rest;
d7fdbd
+       lseek(fd, s_pos, SEEK_SET);
d7fdbd
+       rcnt = read(fd, buff, rest);
d7fdbd
+    } while (rcnt > 0);
d7fdbd
+
d7fdbd
+    close (fd);
d7fdbd
+  }
d7fdbd
+
d7fdbd
+  return ret_tz;
d7fdbd
+}
d7fdbd
+
d7fdbd
 void
d7fdbd
 vsf_sysutil_tzset(void)
d7fdbd
 {
d7fdbd
   int retval;
d7fdbd
-  char tzbuf[sizeof("+HHMM!")];
d7fdbd
+  char *tz=NULL, tzbuf[sizeof("+HHMM!")];
d7fdbd
   time_t the_time = time(NULL);
d7fdbd
   struct tm* p_tm;
d7fdbd
+
d7fdbd
+  /* Set our timezone in the TZ environment variable to cater for the fact
d7fdbd
+   * that modern glibc does not cache /etc/localtime (which becomes inaccessible
d7fdbd
+   * when we chroot().
d7fdbd
+   */
d7fdbd
+  tz = vsf_sysutil_get_tz();;
d7fdbd
+  if (tz)
d7fdbd
+  {
d7fdbd
+    putenv(tz);
d7fdbd
+  }
d7fdbd
   tzset();
d7fdbd
   p_tm = localtime(&the_time);
d7fdbd
   if (p_tm == NULL)
d7fdbd
   {
d7fdbd
     die("localtime");
d7fdbd
   }
d7fdbd
-  /* Set our timezone in the TZ environment variable to cater for the fact
d7fdbd
-   * that modern glibc does not cache /etc/localtime (which becomes inaccessible
d7fdbd
-   * when we chroot().
d7fdbd
-   */
d7fdbd
   retval = strftime(tzbuf, sizeof(tzbuf), "%z", p_tm);
d7fdbd
   tzbuf[sizeof(tzbuf) - 1] = '\0';
d7fdbd
   if (retval == 5)
d7fdbd
   {
d7fdbd
-    /* Static because putenv() does not copy the string. */
d7fdbd
-    static char envtz[sizeof("TZ=UTC-hh:mm")];
d7fdbd
-    /* Insert a colon so we have e.g. -05:00 instead of -0500 */
d7fdbd
-    tzbuf[5] = tzbuf[4];
d7fdbd
-    tzbuf[4] = tzbuf[3];
d7fdbd
-    tzbuf[3] = ':';
d7fdbd
-    /* Invert the sign - we just got the offset _from_ UTC but for TZ, we need
d7fdbd
-     * the offset _to_ UTC.
d7fdbd
-     */
d7fdbd
-    if (tzbuf[0] == '+')
d7fdbd
-    {
d7fdbd
-      tzbuf[0] = '-';
d7fdbd
-    }
d7fdbd
-    else
d7fdbd
-    {
d7fdbd
-      tzbuf[0] = '+';
d7fdbd
-    }
d7fdbd
-    snprintf(envtz, sizeof(envtz), "TZ=UTC%s", tzbuf);
d7fdbd
-    putenv(envtz);
d7fdbd
     s_timezone = ((tzbuf[1] - '0') * 10 + (tzbuf[2] - '0')) * 60 * 60;
d7fdbd
-    s_timezone += ((tzbuf[4] - '0') * 10 + (tzbuf[5] - '0')) * 60;
d7fdbd
-    if (tzbuf[0] == '-')
d7fdbd
+    s_timezone += ((tzbuf[3] - '0') * 10 + (tzbuf[4] - '0')) * 60;
d7fdbd
+    if (tzbuf[0] == '+')
d7fdbd
     {
d7fdbd
       s_timezone *= -1;
d7fdbd
     }
d7fdbd
-- 
d7fdbd
2.14.4
d7fdbd