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

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