carlwgeorge / rpms / php

Forked from rpms/php 5 years ago
Clone
b1f466f
b1f466f
Add support for use of the system timezone database, rather
b1f466f
than embedding a copy.  Discussed upstream but was not desired.
b1f466f
b1f466f
History:
b1f466f
r3: fix a crash if /usr/share/zoneinfo doesn't exist (Raphael Geissert)
b1f466f
r2: add filesystem trawl to set up name alias index
b1f466f
r1: initial revision
b1f466f
a5c5fb3
--- php-5.2.5/ext/date/lib/timelib.m4.systzdata
a5c5fb3
+++ php-5.2.5/ext/date/lib/timelib.m4
a5c5fb3
@@ -78,3 +78,17 @@ stdlib.h
a5c5fb3
 
a5c5fb3
 dnl Check for strtoll, atoll
a5c5fb3
 AC_CHECK_FUNCS(strtoll atoll strftime)
a5c5fb3
+
a5c5fb3
+PHP_ARG_WITH(system-tzdata, for use of system timezone data,
a5c5fb3
+[  --with-system-tzdata[=DIR]      to specify use of system timezone data],
a5c5fb3
+no, no)
a5c5fb3
+
a5c5fb3
+if test "$PHP_SYSTEM_TZDATA" != "no"; then
a5c5fb3
+   AC_DEFINE(HAVE_SYSTEM_TZDATA, 1, [Define if system timezone data is used])
a5c5fb3
+
a5c5fb3
+   if test "$PHP_SYSTEM_TZDATA" != "yes"; then
a5c5fb3
+      AC_DEFINE_UNQUOTED(HAVE_SYSTEM_TZDATA_PREFIX, "$PHP_SYSTEM_TZDATA",
a5c5fb3
+                         [Define for location of system timezone data])
a5c5fb3
+   fi
a5c5fb3
+fi
a5c5fb3
+
a5c5fb3
--- php-5.2.5/ext/date/lib/parse_tz.c.systzdata
a5c5fb3
+++ php-5.2.5/ext/date/lib/parse_tz.c
a5c5fb3
@@ -20,6 +20,16 @@
a5c5fb3
 
a5c5fb3
 #include "timelib.h"
a5c5fb3
 
a5c5fb3
+#ifdef HAVE_SYSTEM_TZDATA
a5c5fb3
+#include <sys/mman.h>
a5c5fb3
+#include <sys/stat.h>
a5c5fb3
+#include <limits.h>
a5c5fb3
+#include <fcntl.h>
a5c5fb3
+#include <unistd.h>
a5c5fb3
+
a5c5fb3
+#include "php_scandir.h"
a5c5fb3
+#endif
a5c5fb3
+
a5c5fb3
 #include <stdio.h>
a5c5fb3
 
a5c5fb3
 #ifdef HAVE_LOCALE_H
a5c5fb3
@@ -31,7 +41,10 @@
a5c5fb3
 #else
a5c5fb3
 #include <strings.h>
a5c5fb3
 #endif
a5c5fb3
+
a5c5fb3
+#ifndef HAVE_SYSTEM_TZDATA
a5c5fb3
 #include "timezonedb.h"
a5c5fb3
+#endif
a5c5fb3
 
a5c5fb3
 #if (defined(__APPLE__) || defined(__APPLE_CC__)) && (defined(__BIG_ENDIAN__) || defined(__LITTLE_ENDIAN__))
a5c5fb3
 # if defined(__LITTLE_ENDIAN__)
a5c5fb3
@@ -206,6 +219,195 @@ void timelib_dump_tzinfo(timelib_tzinfo 
a5c5fb3
 	}
a5c5fb3
 }
a5c5fb3
 
a5c5fb3
+#ifdef HAVE_SYSTEM_TZDATA
a5c5fb3
+
a5c5fb3
+#ifdef HAVE_SYSTEM_TZDATA_PREFIX
a5c5fb3
+#define ZONEINFO_PREFIX HAVE_SYSTEM_TZDATA_PREFIX
a5c5fb3
+#else
a5c5fb3
+#define ZONEINFO_PREFIX "/usr/share/zoneinfo"
a5c5fb3
+#endif
a5c5fb3
+
a5c5fb3
+static const timelib_tzdb *timezonedb_system = NULL;
a5c5fb3
+
a5c5fb3
+/* Filter out some non-tzdata files and the posix/right databases, if
a5c5fb3
+ * present. */
a5c5fb3
+static int index_filter(const struct dirent *ent)
a5c5fb3
+{
a5c5fb3
+	return strcmp(ent->d_name, ".") != 0
a5c5fb3
+		&& strcmp(ent->d_name, "..") != 0
a5c5fb3
+		&& strcmp(ent->d_name, "posix") != 0
a5c5fb3
+		&& strcmp(ent->d_name, "posixrules") != 0
a5c5fb3
+		&& strcmp(ent->d_name, "right") != 0
a5c5fb3
+		&& strstr(ent->d_name, ".tab") == NULL;
a5c5fb3
+}
a5c5fb3
+
a5c5fb3
+/* Create the zone identifier index by trawling the filesystem. */
a5c5fb3
+static void create_zone_index(timelib_tzdb *db)
a5c5fb3
+{
a5c5fb3
+	size_t dirstack_size,  dirstack_top;
a5c5fb3
+	size_t index_size, index_next;
a5c5fb3
+	timelib_tzdb_index_entry *db_index;
a5c5fb3
+	char **dirstack;
a5c5fb3
+
b1f466f
+	/* LIFO stack to hold directory entries to scan; each slot is a
a5c5fb3
+	 * directory name relative to the zoneinfo prefix. */
a5c5fb3
+	dirstack_size = 32;
a5c5fb3
+	dirstack = malloc(dirstack_size * sizeof *dirstack);
a5c5fb3
+	dirstack_top = 1;
a5c5fb3
+	dirstack[0] = strdup("");
a5c5fb3
+	
a5c5fb3
+	/* Index array. */
a5c5fb3
+	index_size = 64;
a5c5fb3
+	db_index = malloc(index_size * sizeof *db_index);
a5c5fb3
+	index_next = 0;
a5c5fb3
+
a5c5fb3
+	do {
a5c5fb3
+		struct dirent **ents;
a5c5fb3
+		char name[PATH_MAX], *top;
a5c5fb3
+		int count;
a5c5fb3
+
a5c5fb3
+		/* Pop the top stack entry, and iterate through its contents. */
a5c5fb3
+		top = dirstack[--dirstack_top];
a5c5fb3
+		snprintf(name, sizeof name, ZONEINFO_PREFIX "/%s", top);
a5c5fb3
+
a5c5fb3
+		count = php_scandir(name, &ents, index_filter, php_alphasort);
a5c5fb3
+
a5c5fb3
+		while (count > 0) {
a5c5fb3
+			struct stat st;
a5c5fb3
+			const char *leaf = ents[count - 1]->d_name;
a5c5fb3
+
a5c5fb3
+			snprintf(name, sizeof name, ZONEINFO_PREFIX "/%s/%s", 
a5c5fb3
+				 top, leaf);
a5c5fb3
+			
a5c5fb3
+			if (strlen(name) && stat(name, &st) == 0) {
a5c5fb3
+				/* Name, relative to the zoneinfo prefix. */
a5c5fb3
+				const char *root = top;
a5c5fb3
+
a5c5fb3
+				if (root[0] == '/') root++;
a5c5fb3
+
a5c5fb3
+				snprintf(name, sizeof name, "%s%s%s", root, 
a5c5fb3
+					 *root ? "/": "", leaf);
a5c5fb3
+
a5c5fb3
+				if (S_ISDIR(st.st_mode)) {
a5c5fb3
+					if (dirstack_top == dirstack_size) {
a5c5fb3
+						dirstack_size *= 2;
a5c5fb3
+						dirstack = realloc(dirstack, 
a5c5fb3
+								   dirstack_size * sizeof *dirstack);
a5c5fb3
+					}
a5c5fb3
+					dirstack[dirstack_top++] = strdup(name);
a5c5fb3
+				}
a5c5fb3
+				else {
a5c5fb3
+					if (index_next == index_size) {
a5c5fb3
+						index_size *= 2;
a5c5fb3
+						db_index = realloc(db_index,
a5c5fb3
+								   index_size * sizeof *db_index);
a5c5fb3
+					}
a5c5fb3
+
a5c5fb3
+					db_index[index_next].id = strdup(name);
a5c5fb3
+					db_index[index_next++].pos = 0;
a5c5fb3
+				}
a5c5fb3
+			}
a5c5fb3
+
a5c5fb3
+			free(ents[--count]);
a5c5fb3
+		}
a5c5fb3
+		
b1f466f
+		if (count != -1) free(ents);
a5c5fb3
+		free(top);
a5c5fb3
+	} while (dirstack_top);
a5c5fb3
+
a5c5fb3
+	db->index = db_index;
a5c5fb3
+	db->index_size = index_next;
a5c5fb3
+
a5c5fb3
+	free(dirstack);
a5c5fb3
+}
a5c5fb3
+
a5c5fb3
+/* Return the mmap()ed tzfile if found, else NULL.  On success, the
a5c5fb3
+ * length of the mapped data is placed in *length. */
a5c5fb3
+static char *map_tzfile(const char *timezone, size_t *length)
a5c5fb3
+{
a5c5fb3
+	char fname[PATH_MAX];
a5c5fb3
+	struct stat st;
a5c5fb3
+	char *p;
a5c5fb3
+	int fd;
a5c5fb3
+	
a5c5fb3
+	if (strstr(timezone, "..") != NULL) {
a5c5fb3
+		return NULL;
a5c5fb3
+	}
a5c5fb3
+
a5c5fb3
+	snprintf(fname, sizeof fname, ZONEINFO_PREFIX "/%s", timezone);
a5c5fb3
+	
a5c5fb3
+	fd = open(fname, O_RDONLY);
a5c5fb3
+	if (fd == -1) {
a5c5fb3
+		return NULL;
a5c5fb3
+	} else if (fstat(fd, &st) != 0 || st.st_size < 21) {
a5c5fb3
+		close(fd);
a5c5fb3
+		return NULL;
a5c5fb3
+	}
a5c5fb3
+
a5c5fb3
+	*length = st.st_size;
a5c5fb3
+	p = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
a5c5fb3
+	close(fd);
a5c5fb3
+	
a5c5fb3
+	return p != MAP_FAILED ? p : NULL;
a5c5fb3
+}
a5c5fb3
+
a5c5fb3
+const timelib_tzdb *timelib_builtin_db(void)
a5c5fb3
+{
a5c5fb3
+	if (timezonedb_system == NULL) {
a5c5fb3
+		timelib_tzdb *tmp = malloc(sizeof *tmp);
a5c5fb3
+
a5c5fb3
+		tmp->version = "0.system";
a5c5fb3
+		tmp->data = NULL;
a5c5fb3
+		create_zone_index(tmp);
a5c5fb3
+		timezonedb_system = tmp;
a5c5fb3
+	}
a5c5fb3
+			
a5c5fb3
+	return timezonedb_system;
a5c5fb3
+}
a5c5fb3
+
a5c5fb3
+const timelib_tzdb_index_entry *timelib_timezone_builtin_identifiers_list(int *count)
a5c5fb3
+{
a5c5fb3
+	*count = timezonedb_system->index_size;
a5c5fb3
+	return timezonedb_system->index;
a5c5fb3
+}
a5c5fb3
+
a5c5fb3
+int timelib_timezone_id_is_valid(char *timezone, const timelib_tzdb *tzdb)
a5c5fb3
+{
a5c5fb3
+	char fname[PATH_MAX];
a5c5fb3
+
a5c5fb3
+	if (strstr(timezone, "..") != NULL) {
a5c5fb3
+		return 0;
a5c5fb3
+	}
a5c5fb3
+	
a5c5fb3
+	snprintf(fname, sizeof fname, ZONEINFO_PREFIX "/%s", timezone);
a5c5fb3
+
a5c5fb3
+	return access(fname, R_OK) == 0 ? 1 : 0;
a5c5fb3
+}
a5c5fb3
+
a5c5fb3
+timelib_tzinfo *timelib_parse_tzfile(char *timezone, const timelib_tzdb *tzdb)
a5c5fb3
+{
a5c5fb3
+	char *tzf, *orig;
a5c5fb3
+	timelib_tzinfo *tmp;
a5c5fb3
+	size_t len;
a5c5fb3
+
a5c5fb3
+	orig = map_tzfile(timezone, &len;;
a5c5fb3
+	if (orig == NULL) {
a5c5fb3
+		return NULL;
a5c5fb3
+	}
a5c5fb3
+
a5c5fb3
+	tmp = timelib_tzinfo_ctor(timezone);
a5c5fb3
+
a5c5fb3
+	tzf = orig + 20;
a5c5fb3
+	read_header(&tzf, tmp);
a5c5fb3
+	read_transistions(&tzf, tmp);
a5c5fb3
+	read_types(&tzf, tmp);
a5c5fb3
+
a5c5fb3
+	munmap(orig, len);
a5c5fb3
+
a5c5fb3
+	return tmp;
a5c5fb3
+}
a5c5fb3
+#else /* !HAVE_SYSTEM_TZDATA */
a5c5fb3
+
a5c5fb3
 static int seek_to_tz_position(const unsigned char **tzf, char *timezone, const timelib_tzdb *tzdb)
a5c5fb3
 {
a5c5fb3
 	int left = 0, right = tzdb->index_size - 1;
a5c5fb3
@@ -279,6 +481,7 @@ timelib_tzinfo *timelib_parse_tzfile(cha
a5c5fb3
 
a5c5fb3
 	return tmp;
a5c5fb3
 }
a5c5fb3
+#endif
a5c5fb3
 
a5c5fb3
 static ttinfo* fetch_timezone_offset(timelib_tzinfo *tz, timelib_sll ts, timelib_sll *transition_time)
a5c5fb3
 {