1 /*-------------------------------------------------------------------------
4 * Functions for parsing timezone offset files
6 * Note: we generally should not throw any errors in this file, but instead
7 * try to return an error code. This is not completely bulletproof at
8 * present --- in particular out-of-memory will throw an error. Could
9 * probably fix with PG_TRY if necessary.
12 * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
13 * Portions Copyright (c) 1994, Regents of the University of California
16 * $PostgreSQL: pgsql/src/backend/utils/misc/tzparser.c,v 1.1 2006/07/25 03:51:21 tgl Exp $
18 *-------------------------------------------------------------------------
25 #include "miscadmin.h"
26 #include "storage/fd.h"
27 #include "utils/datetime.h"
28 #include "utils/memutils.h"
29 #include "utils/tzparser.h"
32 #define WHITESPACE " \t\n\r"
34 static int tz_elevel; /* to avoid passing this around a lot */
36 static bool validateTzEntry(tzEntry *tzentry);
37 static bool splitTzLine(const char *filename, int lineno,
38 char *line, tzEntry *tzentry);
39 static int addToArray(tzEntry **base, int *arraysize, int n,
40 tzEntry *entry, bool override);
41 static int ParseTzFile(const char *filename, int depth,
42 tzEntry **base, int *arraysize, int n);
46 * Apply additional validation checks to a tzEntry
48 * Returns TRUE if OK, else false
51 validateTzEntry(tzEntry *tzentry)
56 * Check restrictions imposed by datetkntbl storage format (see datetime.c)
58 if (strlen(tzentry->abbrev) > TOKMAXLEN)
61 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
62 errmsg("time zone abbreviation \"%s\" is too long (maximum %d characters) in time zone file \"%s\", line %d",
63 tzentry->abbrev, TOKMAXLEN,
64 tzentry->filename, tzentry->lineno)));
67 if (tzentry->offset % 900 != 0)
70 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
71 errmsg("time zone offset %d is not a multiple of 900 sec (15 min) in time zone file \"%s\", line %d",
73 tzentry->filename, tzentry->lineno)));
78 * Sanity-check the offset: shouldn't exceed 14 hours
80 if (tzentry->offset > 14*60*60 ||
81 tzentry->offset < -14*60*60)
84 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
85 errmsg("time zone offset %d is out of range in time zone file \"%s\", line %d",
87 tzentry->filename, tzentry->lineno)));
92 * Convert abbrev to lowercase (must match datetime.c's conversion)
94 for (p = (unsigned char *) tzentry->abbrev; *p; p++)
101 * Attempt to parse the line as a timezone abbrev spec (name, offset, dst)
103 * Returns TRUE if OK, else false; data is stored in *tzentry
106 splitTzLine(const char *filename, int lineno, char *line, tzEntry *tzentry)
114 tzentry->lineno = lineno;
115 tzentry->filename = filename;
117 abbrev = strtok(line, WHITESPACE);
121 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
122 errmsg("missing time zone abbreviation in time zone file \"%s\", line %d",
126 tzentry->abbrev = abbrev;
128 offset = strtok(NULL, WHITESPACE);
132 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
133 errmsg("missing time zone offset in time zone file \"%s\", line %d",
137 tzentry->offset = strtol(offset, &offset_endptr, 10);
138 if (offset_endptr == offset || *offset_endptr != '\0')
141 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
142 errmsg("invalid number for time zone offset in time zone file \"%s\", line %d",
147 is_dst = strtok(NULL, WHITESPACE);
148 if (is_dst && pg_strcasecmp(is_dst, "D") == 0)
150 tzentry->is_dst = true;
151 remain = strtok(NULL, WHITESPACE);
155 /* there was no 'D' dst specifier */
156 tzentry->is_dst = false;
160 if (!remain) /* no more non-whitespace chars */
163 if (remain[0] != '#') /* must be a comment */
166 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
167 errmsg("invalid syntax in time zone file \"%s\", line %d",
175 * Insert entry into sorted array
177 * *base: base address of array (changeable if must enlarge array)
178 * *arraysize: allocated length of array (changeable if must enlarge array)
179 * n: current number of valid elements in array
180 * entry: new data to insert
181 * override: TRUE if OK to override
183 * Returns the new array length (new value for n), or -1 if error
186 addToArray(tzEntry **base, int *arraysize, int n,
187 tzEntry *entry, bool override)
194 * Search the array for a duplicate; as a useful side effect, the array
195 * is maintained in sorted order. We use strcmp() to ensure we match
196 * the sort order datetime.c expects.
203 int mid = (low + high) >> 1;
204 tzEntry *midptr = arrayptr + mid;
207 cmp = strcmp(entry->abbrev, midptr->abbrev);
215 * Found a duplicate entry; complain unless it's the same.
217 if (midptr->offset == entry->offset &&
218 midptr->is_dst == entry->is_dst)
220 /* return unchanged array */
225 /* same abbrev but something is different, override */
226 midptr->offset = entry->offset;
227 midptr->is_dst = entry->is_dst;
230 /* same abbrev but something is different, complain */
232 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
233 errmsg("time zone abbreviation \"%s\" is multiply defined",
235 errdetail("Time zone file \"%s\", line %d conflicts with file \"%s\", line %d.",
236 midptr->filename, midptr->lineno,
237 entry->filename, entry->lineno)));
243 * No match, insert at position "low".
248 *base = (tzEntry *) repalloc(*base, *arraysize * sizeof(tzEntry));
251 arrayptr = *base + low;
253 memmove(arrayptr + 1, arrayptr, (n - low) * sizeof(tzEntry));
255 memcpy(arrayptr, entry, sizeof(tzEntry));
257 /* Must dup the abbrev to ensure it survives */
258 arrayptr->abbrev = pstrdup(entry->abbrev);
264 * Parse a single timezone abbrev file --- can recurse to handle @INCLUDE
266 * filename: user-specified file name (does not include path)
267 * depth: current recursion depth
268 * *base: array for results (changeable if must enlarge array)
269 * *arraysize: allocated length of array (changeable if must enlarge array)
270 * n: current number of valid elements in array
272 * Returns the new array length (new value for n), or -1 if error
275 ParseTzFile(const char *filename, int depth,
276 tzEntry **base, int *arraysize, int n)
278 char share_path[MAXPGPATH];
279 char file_path[MAXPGPATH];
285 bool override = false;
289 * We enforce that the filename is all alpha characters. This may be
290 * overly restrictive, but we don't want to allow access to anything
291 * outside the timezonesets directory, so for instance '/' *must* be
294 for (p = filename; *p; p++)
296 if (!isalpha((unsigned char) *p))
298 /* at level 0, we need no ereport since guc.c will say enough */
301 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
302 errmsg("invalid time zone file name \"%s\"",
309 * The maximal recursion depth is a pretty arbitrary setting.
310 * It is hard to imagine that someone needs more than 3 levels so stick
311 * with this conservative setting until someone complains.
316 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
317 errmsg("time zone file recursion limit exceeded in file \"%s\"",
322 get_share_path(my_exec_path, share_path);
323 snprintf(file_path, sizeof(file_path), "%s/timezonesets/%s",
324 share_path, filename);
325 tzFile = AllocateFile(file_path, "r");
328 /* at level 0, if file doesn't exist, guc.c's complaint is enough */
329 if (errno != ENOENT || depth > 0)
331 (errcode_for_file_access(),
332 errmsg("could not read time zone file \"%s\": %m",
337 while (!feof(tzFile))
340 if (fgets(tzbuf, sizeof(tzbuf), tzFile) == NULL)
345 (errcode_for_file_access(),
346 errmsg("could not read time zone file \"%s\": %m",
350 /* else we're at EOF after all */
353 if (strlen(tzbuf) == sizeof(tzbuf)-1)
355 /* the line is too long for tzbuf */
357 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
358 errmsg("line is too long in time zone file \"%s\", line %d",
363 /* skip over whitespace */
365 while (*line && isspace((unsigned char) *line))
368 if (*line == '\0') /* empty line */
370 if (*line == '#') /* comment line */
373 if (pg_strncasecmp(line, "@INCLUDE", strlen("@INCLUDE")) == 0)
375 /* pstrdup so we can use filename in result data structure */
376 char* includeFile = pstrdup(line + strlen("@INCLUDE"));
378 includeFile = strtok(includeFile, WHITESPACE);
379 if (!includeFile || !*includeFile)
382 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
383 errmsg("@INCLUDE without filename in time zone file \"%s\", line %d",
387 n = ParseTzFile(includeFile, depth + 1,
394 if (pg_strncasecmp(line, "@OVERRIDE", strlen("@OVERRIDE")) == 0)
400 if (!splitTzLine(filename, lineno, line, &tzentry))
402 if (!validateTzEntry(&tzentry))
404 n = addToArray(base, arraysize, n, &tzentry, override);
415 * load_tzoffsets --- read and parse the specified timezone offset file
417 * filename: name specified by user
418 * doit: whether to actually apply the new values, or just check
419 * elevel: elog reporting level (will be less than ERROR)
421 * Returns TRUE if OK, FALSE if not; should avoid erroring out
424 load_tzoffsets(const char *filename, bool doit, int elevel)
426 MemoryContext tmpContext;
427 MemoryContext oldContext;
435 * Create a temp memory context to work in. This makes it easy to
436 * clean up afterwards.
438 tmpContext = AllocSetContextCreate(CurrentMemoryContext,
440 ALLOCSET_SMALL_MINSIZE,
441 ALLOCSET_SMALL_INITSIZE,
442 ALLOCSET_SMALL_MAXSIZE);
443 oldContext = MemoryContextSwitchTo(tmpContext);
445 /* Initialize array at a reasonable size */
447 array = (tzEntry *) palloc(arraysize * sizeof(tzEntry));
449 /* Parse the file(s) */
450 n = ParseTzFile(filename, 0, &array, &arraysize, 0);
452 /* If no errors and we should apply the result, pass it to datetime.c */
454 InstallTimeZoneAbbrevs(array, n);
457 MemoryContextSwitchTo(oldContext);
458 MemoryContextDelete(tmpContext);