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-2008, PostgreSQL Global Development Group
13 * Portions Copyright (c) 1994, Regents of the University of California
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
59 if (strlen(tzentry
->abbrev
) > TOKMAXLEN
)
62 (errcode(ERRCODE_INVALID_PARAMETER_VALUE
),
63 errmsg("time zone abbreviation \"%s\" is too long (maximum %d characters) in time zone file \"%s\", line %d",
64 tzentry
->abbrev
, TOKMAXLEN
,
65 tzentry
->filename
, tzentry
->lineno
)));
68 if (tzentry
->offset
% 900 != 0)
71 (errcode(ERRCODE_INVALID_PARAMETER_VALUE
),
72 errmsg("time zone offset %d is not a multiple of 900 sec (15 min) in time zone file \"%s\", line %d",
74 tzentry
->filename
, tzentry
->lineno
)));
79 * Sanity-check the offset: shouldn't exceed 14 hours
81 if (tzentry
->offset
> 14 * 60 * 60 ||
82 tzentry
->offset
< -14 * 60 * 60)
85 (errcode(ERRCODE_INVALID_PARAMETER_VALUE
),
86 errmsg("time zone offset %d is out of range in time zone file \"%s\", line %d",
88 tzentry
->filename
, tzentry
->lineno
)));
93 * Convert abbrev to lowercase (must match datetime.c's conversion)
95 for (p
= (unsigned char *) tzentry
->abbrev
; *p
; p
++)
102 * Attempt to parse the line as a timezone abbrev spec (name, offset, dst)
104 * Returns TRUE if OK, else false; data is stored in *tzentry
107 splitTzLine(const char *filename
, int lineno
, char *line
, tzEntry
*tzentry
)
115 tzentry
->lineno
= lineno
;
116 tzentry
->filename
= filename
;
118 abbrev
= strtok(line
, WHITESPACE
);
122 (errcode(ERRCODE_INVALID_PARAMETER_VALUE
),
123 errmsg("missing time zone abbreviation in time zone file \"%s\", line %d",
127 tzentry
->abbrev
= abbrev
;
129 offset
= strtok(NULL
, WHITESPACE
);
133 (errcode(ERRCODE_INVALID_PARAMETER_VALUE
),
134 errmsg("missing time zone offset in time zone file \"%s\", line %d",
138 tzentry
->offset
= strtol(offset
, &offset_endptr
, 10);
139 if (offset_endptr
== offset
|| *offset_endptr
!= '\0')
142 (errcode(ERRCODE_INVALID_PARAMETER_VALUE
),
143 errmsg("invalid number for time zone offset in time zone file \"%s\", line %d",
148 is_dst
= strtok(NULL
, WHITESPACE
);
149 if (is_dst
&& pg_strcasecmp(is_dst
, "D") == 0)
151 tzentry
->is_dst
= true;
152 remain
= strtok(NULL
, WHITESPACE
);
156 /* there was no 'D' dst specifier */
157 tzentry
->is_dst
= false;
161 if (!remain
) /* no more non-whitespace chars */
164 if (remain
[0] != '#') /* must be a comment */
167 (errcode(ERRCODE_INVALID_PARAMETER_VALUE
),
168 errmsg("invalid syntax in time zone file \"%s\", line %d",
176 * Insert entry into sorted array
178 * *base: base address of array (changeable if must enlarge array)
179 * *arraysize: allocated length of array (changeable if must enlarge array)
180 * n: current number of valid elements in array
181 * entry: new data to insert
182 * override: TRUE if OK to override
184 * Returns the new array length (new value for n), or -1 if error
187 addToArray(tzEntry
**base
, int *arraysize
, int n
,
188 tzEntry
*entry
, bool override
)
195 * Search the array for a duplicate; as a useful side effect, the array is
196 * maintained in sorted order. We use strcmp() to ensure we match the
197 * sort order datetime.c expects.
204 int mid
= (low
+ high
) >> 1;
205 tzEntry
*midptr
= arrayptr
+ mid
;
208 cmp
= strcmp(entry
->abbrev
, midptr
->abbrev
);
216 * Found a duplicate entry; complain unless it's the same.
218 if (midptr
->offset
== entry
->offset
&&
219 midptr
->is_dst
== entry
->is_dst
)
221 /* return unchanged array */
226 /* same abbrev but something is different, override */
227 midptr
->offset
= entry
->offset
;
228 midptr
->is_dst
= entry
->is_dst
;
231 /* same abbrev but something is different, complain */
233 (errcode(ERRCODE_INVALID_PARAMETER_VALUE
),
234 errmsg("time zone abbreviation \"%s\" is multiply defined",
236 errdetail("Entry in time zone file \"%s\", line %d, conflicts with entry in file \"%s\", line %d.",
237 midptr
->filename
, midptr
->lineno
,
238 entry
->filename
, entry
->lineno
)));
244 * No match, insert at position "low".
249 *base
= (tzEntry
*) repalloc(*base
, *arraysize
* sizeof(tzEntry
));
252 arrayptr
= *base
+ low
;
254 memmove(arrayptr
+ 1, arrayptr
, (n
- low
) * sizeof(tzEntry
));
256 memcpy(arrayptr
, entry
, sizeof(tzEntry
));
258 /* Must dup the abbrev to ensure it survives */
259 arrayptr
->abbrev
= pstrdup(entry
->abbrev
);
265 * Parse a single timezone abbrev file --- can recurse to handle @INCLUDE
267 * filename: user-specified file name (does not include path)
268 * depth: current recursion depth
269 * *base: array for results (changeable if must enlarge array)
270 * *arraysize: allocated length of array (changeable if must enlarge array)
271 * n: current number of valid elements in array
273 * Returns the new array length (new value for n), or -1 if error
276 ParseTzFile(const char *filename
, int depth
,
277 tzEntry
**base
, int *arraysize
, int n
)
279 char share_path
[MAXPGPATH
];
280 char file_path
[MAXPGPATH
];
286 bool override
= false;
290 * We enforce that the filename is all alpha characters. This may be
291 * overly restrictive, but we don't want to allow access to anything
292 * outside the timezonesets directory, so for instance '/' *must* be
295 for (p
= filename
; *p
; p
++)
297 if (!isalpha((unsigned char) *p
))
299 /* at level 0, we need no ereport since guc.c will say enough */
302 (errcode(ERRCODE_INVALID_PARAMETER_VALUE
),
303 errmsg("invalid time zone file name \"%s\"",
310 * The maximal recursion depth is a pretty arbitrary setting. It is hard
311 * to imagine that someone needs more than 3 levels so stick with this
312 * conservative setting until someone complains.
317 (errcode(ERRCODE_INVALID_PARAMETER_VALUE
),
318 errmsg("time zone file recursion limit exceeded in file \"%s\"",
323 get_share_path(my_exec_path
, share_path
);
324 snprintf(file_path
, sizeof(file_path
), "%s/timezonesets/%s",
325 share_path
, filename
);
326 tzFile
= AllocateFile(file_path
, "r");
329 /* at level 0, if file doesn't exist, guc.c's complaint is enough */
330 if (errno
!= ENOENT
|| depth
> 0)
332 (errcode_for_file_access(),
333 errmsg("could not read time zone file \"%s\": %m",
338 while (!feof(tzFile
))
341 if (fgets(tzbuf
, sizeof(tzbuf
), tzFile
) == NULL
)
346 (errcode_for_file_access(),
347 errmsg("could not read time zone file \"%s\": %m",
351 /* else we're at EOF after all */
354 if (strlen(tzbuf
) == sizeof(tzbuf
) - 1)
356 /* the line is too long for tzbuf */
358 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED
),
359 errmsg("line is too long in time zone file \"%s\", line %d",
364 /* skip over whitespace */
366 while (*line
&& isspace((unsigned char) *line
))
369 if (*line
== '\0') /* empty line */
371 if (*line
== '#') /* comment line */
374 if (pg_strncasecmp(line
, "@INCLUDE", strlen("@INCLUDE")) == 0)
376 /* pstrdup so we can use filename in result data structure */
377 char *includeFile
= pstrdup(line
+ strlen("@INCLUDE"));
379 includeFile
= strtok(includeFile
, WHITESPACE
);
380 if (!includeFile
|| !*includeFile
)
383 (errcode(ERRCODE_INVALID_PARAMETER_VALUE
),
384 errmsg("@INCLUDE without file name in time zone file \"%s\", line %d",
388 n
= ParseTzFile(includeFile
, depth
+ 1,
395 if (pg_strncasecmp(line
, "@OVERRIDE", strlen("@OVERRIDE")) == 0)
401 if (!splitTzLine(filename
, lineno
, line
, &tzentry
))
403 if (!validateTzEntry(&tzentry
))
405 n
= addToArray(base
, arraysize
, n
, &tzentry
, override
);
416 * load_tzoffsets --- read and parse the specified timezone offset file
418 * filename: name specified by user
419 * doit: whether to actually apply the new values, or just check
420 * elevel: elog reporting level (will be less than ERROR)
422 * Returns TRUE if OK, FALSE if not; should avoid erroring out
425 load_tzoffsets(const char *filename
, bool doit
, int elevel
)
427 MemoryContext tmpContext
;
428 MemoryContext oldContext
;
436 * Create a temp memory context to work in. This makes it easy to clean
439 tmpContext
= AllocSetContextCreate(CurrentMemoryContext
,
441 ALLOCSET_SMALL_MINSIZE
,
442 ALLOCSET_SMALL_INITSIZE
,
443 ALLOCSET_SMALL_MAXSIZE
);
444 oldContext
= MemoryContextSwitchTo(tmpContext
);
446 /* Initialize array at a reasonable size */
448 array
= (tzEntry
*) palloc(arraysize
* sizeof(tzEntry
));
450 /* Parse the file(s) */
451 n
= ParseTzFile(filename
, 0, &array
, &arraysize
, 0);
453 /* If no errors and we should apply the result, pass it to datetime.c */
455 InstallTimeZoneAbbrevs(array
, n
);
458 MemoryContextSwitchTo(oldContext
);
459 MemoryContextDelete(tmpContext
);