PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
pgtz.c File Reference
#include "postgres.h"
#include <ctype.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <time.h>
#include "datatype/timestamp.h"
#include "miscadmin.h"
#include "pgtz.h"
#include "storage/fd.h"
#include "utils/hsearch.h"
Include dependency graph for pgtz.c:

Go to the source code of this file.

Data Structures

struct  pg_tz_cache
 
struct  pg_tzenum
 

Macros

#define MAX_TZDIR_DEPTH   10
 

Functions

static bool scan_directory_ci (const char *dirname, const char *fname, int fnamelen, char *canonname, int canonnamelen)
 
static const char * pg_TZDIR (void)
 
int pg_open_tzfile (const char *name, char *canonname)
 
static bool init_timezone_hashtable (void)
 
pg_tzpg_tzset (const char *name)
 
pg_tzpg_tzset_offset (long gmtoffset)
 
void pg_timezone_initialize (void)
 
pg_tzenumpg_tzenumerate_start (void)
 
void pg_tzenumerate_end (pg_tzenum *dir)
 
pg_tzpg_tzenumerate_next (pg_tzenum *dir)
 

Variables

pg_tzsession_timezone = NULL
 
pg_tzlog_timezone = NULL
 
static HTABtimezone_cache = NULL
 

Macro Definition Documentation

#define MAX_TZDIR_DEPTH   10

Definition at line 392 of file pgtz.c.

Referenced by pg_tzenumerate_next().

Function Documentation

static bool init_timezone_hashtable ( void  )
static

Definition at line 209 of file pgtz.c.

References HASHCTL::entrysize, hash_create(), HASH_ELEM, HASHCTL::keysize, MemSet, and TZ_STRLEN_MAX.

Referenced by pg_tzset().

210 {
211  HASHCTL hash_ctl;
212 
213  MemSet(&hash_ctl, 0, sizeof(hash_ctl));
214 
215  hash_ctl.keysize = TZ_STRLEN_MAX + 1;
216  hash_ctl.entrysize = sizeof(pg_tz_cache);
217 
218  timezone_cache = hash_create("Timezones",
219  4,
220  &hash_ctl,
221  HASH_ELEM);
222  if (!timezone_cache)
223  return false;
224 
225  return true;
226 }
#define HASH_ELEM
Definition: hsearch.h:87
Size entrysize
Definition: hsearch.h:73
#define MemSet(start, val, len)
Definition: c.h:846
#define TZ_STRLEN_MAX
Definition: pgtime.h:44
HTAB * hash_create(const char *tabname, long nelem, HASHCTL *info, int flags)
Definition: dynahash.c:316
Size keysize
Definition: hsearch.h:72
static HTAB * timezone_cache
Definition: pgtz.c:205
int pg_open_tzfile ( const char *  name,
char *  canonname 
)

Definition at line 76 of file pgtz.c.

References MAXPGPATH, name, PG_BINARY, pg_TZDIR(), result, scan_directory_ci(), strlcpy(), and TZ_STRLEN_MAX.

77 {
78  const char *fname;
79  char fullname[MAXPGPATH];
80  int fullnamelen;
81  int orignamelen;
82 
83  /* Initialize fullname with base name of tzdata directory */
84  strlcpy(fullname, pg_TZDIR(), sizeof(fullname));
85  orignamelen = fullnamelen = strlen(fullname);
86 
87  if (fullnamelen + 1 + strlen(name) >= MAXPGPATH)
88  return -1; /* not gonna fit */
89 
90  /*
91  * If the caller doesn't need the canonical spelling, first just try to
92  * open the name as-is. This can be expected to succeed if the given name
93  * is already case-correct, or if the filesystem is case-insensitive; and
94  * we don't need to distinguish those situations if we aren't tasked with
95  * reporting the canonical spelling.
96  */
97  if (canonname == NULL)
98  {
99  int result;
100 
101  fullname[fullnamelen] = '/';
102  /* test above ensured this will fit: */
103  strcpy(fullname + fullnamelen + 1, name);
104  result = open(fullname, O_RDONLY | PG_BINARY, 0);
105  if (result >= 0)
106  return result;
107  /* If that didn't work, fall through to do it the hard way */
108  fullname[fullnamelen] = '\0';
109  }
110 
111  /*
112  * Loop to split the given name into directory levels; for each level,
113  * search using scan_directory_ci().
114  */
115  fname = name;
116  for (;;)
117  {
118  const char *slashptr;
119  int fnamelen;
120 
121  slashptr = strchr(fname, '/');
122  if (slashptr)
123  fnamelen = slashptr - fname;
124  else
125  fnamelen = strlen(fname);
126  if (!scan_directory_ci(fullname, fname, fnamelen,
127  fullname + fullnamelen + 1,
128  MAXPGPATH - fullnamelen - 1))
129  return -1;
130  fullname[fullnamelen++] = '/';
131  fullnamelen += strlen(fullname + fullnamelen);
132  if (slashptr)
133  fname = slashptr + 1;
134  else
135  break;
136  }
137 
138  if (canonname)
139  strlcpy(canonname, fullname + orignamelen + 1, TZ_STRLEN_MAX + 1);
140 
141  return open(fullname, O_RDONLY | PG_BINARY, 0);
142 }
return result
Definition: formatting.c:1633
static const char * pg_TZDIR(void)
Definition: pgtz.c:43
#define PG_BINARY
Definition: c.h:1027
#define TZ_STRLEN_MAX
Definition: pgtime.h:44
#define MAXPGPATH
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
static bool scan_directory_ci(const char *dirname, const char *fname, int fnamelen, char *canonname, int canonnamelen)
Definition: pgtz.c:151
const char * name
Definition: encode.c:521
void pg_timezone_initialize ( void  )

Definition at line 370 of file pgtz.c.

References pg_tzset(), and session_timezone.

Referenced by InitializeGUCOptions().

371 {
372  /*
373  * We may not yet know where PGSHAREDIR is (in particular this is true in
374  * an EXEC_BACKEND subprocess). So use "GMT", which pg_tzset forces to be
375  * interpreted without reference to the filesystem. This corresponds to
376  * the bootstrap default for these variables in guc.c, although in
377  * principle it could be different.
378  */
379  session_timezone = pg_tzset("GMT");
381 }
pg_tz * log_timezone
Definition: pgtz.c:31
pg_tz * pg_tzset(const char *name)
Definition: pgtz.c:243
pg_tz * session_timezone
Definition: pgtz.c:28
static const char* pg_TZDIR ( void  )
static

Definition at line 43 of file pgtz.c.

References get_share_path(), MAXPGPATH, my_exec_path, and strlcpy().

Referenced by pg_open_tzfile(), and pg_tzenumerate_start().

44 {
45 #ifndef SYSTEMTZDIR
46  /* normal case: timezone stuff is under our share dir */
47  static bool done_tzdir = false;
48  static char tzdir[MAXPGPATH];
49 
50  if (done_tzdir)
51  return tzdir;
52 
54  strlcpy(tzdir + strlen(tzdir), "/timezone", MAXPGPATH - strlen(tzdir));
55 
56  done_tzdir = true;
57  return tzdir;
58 #else
59  /* we're configured to use system's timezone database */
60  return SYSTEMTZDIR;
61 #endif
62 }
#define MAXPGPATH
char my_exec_path[MAXPGPATH]
Definition: globals.c:64
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
void get_share_path(const char *my_exec_path, char *ret_path)
Definition: path.c:704
void pg_tzenumerate_end ( pg_tzenum dir)

Definition at line 423 of file pgtz.c.

References pg_tzenum::depth, pg_tzenum::dirdesc, pg_tzenum::dirname, FreeDir(), and pfree().

Referenced by pg_timezone_names().

424 {
425  while (dir->depth >= 0)
426  {
427  FreeDir(dir->dirdesc[dir->depth]);
428  pfree(dir->dirname[dir->depth]);
429  dir->depth--;
430  }
431  pfree(dir);
432 }
DIR * dirdesc[MAX_TZDIR_DEPTH]
Definition: pgtz.c:398
void pfree(void *pointer)
Definition: mcxt.c:949
char * dirname[MAX_TZDIR_DEPTH]
Definition: pgtz.c:399
int depth
Definition: pgtz.c:397
int FreeDir(DIR *dir)
Definition: fd.c:2444
pg_tz* pg_tzenumerate_next ( pg_tzenum dir)

Definition at line 435 of file pgtz.c.

References AllocateDir(), pg_tzenum::baselen, pg_tzenum::depth, pg_tzenum::dirdesc, pg_tzenum::dirname, ereport, errcode_for_file_access(), errmsg(), errmsg_internal(), ERROR, FreeDir(), MAX_TZDIR_DEPTH, MAXPGPATH, pfree(), pg_tz_acceptable(), pstrdup(), ReadDir(), snprintf(), pg_tz::state, strlcpy(), pg_tzenum::tz, tzload(), and pg_tz::TZname.

Referenced by pg_timezone_names().

436 {
437  while (dir->depth >= 0)
438  {
439  struct dirent *direntry;
440  char fullname[MAXPGPATH * 2];
441  struct stat statbuf;
442 
443  direntry = ReadDir(dir->dirdesc[dir->depth], dir->dirname[dir->depth]);
444 
445  if (!direntry)
446  {
447  /* End of this directory */
448  FreeDir(dir->dirdesc[dir->depth]);
449  pfree(dir->dirname[dir->depth]);
450  dir->depth--;
451  continue;
452  }
453 
454  if (direntry->d_name[0] == '.')
455  continue;
456 
457  snprintf(fullname, sizeof(fullname), "%s/%s",
458  dir->dirname[dir->depth], direntry->d_name);
459  if (stat(fullname, &statbuf) != 0)
460  ereport(ERROR,
462  errmsg("could not stat \"%s\": %m", fullname)));
463 
464  if (S_ISDIR(statbuf.st_mode))
465  {
466  /* Step into the subdirectory */
467  if (dir->depth >= MAX_TZDIR_DEPTH - 1)
468  ereport(ERROR,
469  (errmsg_internal("timezone directory stack overflow")));
470  dir->depth++;
471  dir->dirname[dir->depth] = pstrdup(fullname);
472  dir->dirdesc[dir->depth] = AllocateDir(fullname);
473  if (!dir->dirdesc[dir->depth])
474  ereport(ERROR,
476  errmsg("could not open directory \"%s\": %m",
477  fullname)));
478 
479  /* Start over reading in the new directory */
480  continue;
481  }
482 
483  /*
484  * Load this timezone using tzload() not pg_tzset(), so we don't fill
485  * the cache. Also, don't ask for the canonical spelling: we already
486  * know it, and pg_open_tzfile's way of finding it out is pretty
487  * inefficient.
488  */
489  if (tzload(fullname + dir->baselen, NULL, &dir->tz.state, true) != 0)
490  {
491  /* Zone could not be loaded, ignore it */
492  continue;
493  }
494 
495  if (!pg_tz_acceptable(&dir->tz))
496  {
497  /* Ignore leap-second zones */
498  continue;
499  }
500 
501  /* OK, return the canonical zone name spelling. */
502  strlcpy(dir->tz.TZname, fullname + dir->baselen,
503  sizeof(dir->tz.TZname));
504 
505  /* Timezone loaded OK. */
506  return &dir->tz;
507  }
508 
509  /* Nothing more found */
510  return NULL;
511 }
struct pg_tz tz
Definition: pgtz.c:400
int tzload(const char *name, char *canonname, struct state *sp, bool doextend)
Definition: localtime.c:556
char * pstrdup(const char *in)
Definition: mcxt.c:1076
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
Definition: dirent.h:9
DIR * dirdesc[MAX_TZDIR_DEPTH]
Definition: pgtz.c:398
void pfree(void *pointer)
Definition: mcxt.c:949
char * dirname[MAX_TZDIR_DEPTH]
Definition: pgtz.c:399
#define ERROR
Definition: elog.h:43
#define MAXPGPATH
#define MAX_TZDIR_DEPTH
Definition: pgtz.c:392
int errcode_for_file_access(void)
Definition: elog.c:598
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2335
#define ereport(elevel, rest)
Definition: elog.h:122
bool pg_tz_acceptable(pg_tz *tz)
Definition: localtime.c:1845
struct state state
Definition: pgtz.h:63
int baselen
Definition: pgtz.c:396
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
int errmsg_internal(const char *fmt,...)
Definition: elog.c:827
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2401
char TZname[TZ_STRLEN_MAX+1]
Definition: pgtz.h:62
int errmsg(const char *fmt,...)
Definition: elog.c:797
int depth
Definition: pgtz.c:397
int FreeDir(DIR *dir)
Definition: fd.c:2444
pg_tzenum* pg_tzenumerate_start ( void  )

Definition at line 406 of file pgtz.c.

References AllocateDir(), pg_tzenum::baselen, pg_tzenum::depth, pg_tzenum::dirdesc, pg_tzenum::dirname, ereport, errcode_for_file_access(), errmsg(), ERROR, palloc0(), pg_TZDIR(), and pstrdup().

Referenced by pg_timezone_names().

407 {
408  pg_tzenum *ret = (pg_tzenum *) palloc0(sizeof(pg_tzenum));
409  char *startdir = pstrdup(pg_TZDIR());
410 
411  ret->baselen = strlen(startdir) + 1;
412  ret->depth = 0;
413  ret->dirname[0] = startdir;
414  ret->dirdesc[0] = AllocateDir(startdir);
415  if (!ret->dirdesc[0])
416  ereport(ERROR,
418  errmsg("could not open directory \"%s\": %m", startdir)));
419  return ret;
420 }
char * pstrdup(const char *in)
Definition: mcxt.c:1076
DIR * dirdesc[MAX_TZDIR_DEPTH]
Definition: pgtz.c:398
static const char * pg_TZDIR(void)
Definition: pgtz.c:43
char * dirname[MAX_TZDIR_DEPTH]
Definition: pgtz.c:399
#define ERROR
Definition: elog.h:43
int errcode_for_file_access(void)
Definition: elog.c:598
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2335
#define ereport(elevel, rest)
Definition: elog.h:122
void * palloc0(Size size)
Definition: mcxt.c:877
int baselen
Definition: pgtz.c:396
int errmsg(const char *fmt,...)
Definition: elog.c:797
int depth
Definition: pgtz.c:397
pg_tz* pg_tzset ( const char *  name)

Definition at line 243 of file pgtz.c.

References elog, ERROR, HASH_ENTER, HASH_FIND, hash_search(), init_timezone_hashtable(), pg_toupper(), pg_tz::state, pg_tz_cache::tz, TZ_STRLEN_MAX, tzload(), pg_tz::TZname, and tzparse().

Referenced by check_log_timezone(), check_timezone(), DecodeDateTime(), DecodeTimeOnly(), FetchDynamicTimeZone(), parse_sane_timezone(), pg_timezone_initialize(), pg_tzset_offset(), timestamp_zone(), timestamptz_zone(), and timetz_zone().

244 {
245  pg_tz_cache *tzp;
246  struct state tzstate;
247  char uppername[TZ_STRLEN_MAX + 1];
248  char canonname[TZ_STRLEN_MAX + 1];
249  char *p;
250 
251  if (strlen(name) > TZ_STRLEN_MAX)
252  return NULL; /* not going to fit */
253 
254  if (!timezone_cache)
256  return NULL;
257 
258  /*
259  * Upcase the given name to perform a case-insensitive hashtable search.
260  * (We could alternatively downcase it, but we prefer upcase so that we
261  * can get consistently upcased results from tzparse() in case the name is
262  * a POSIX-style timezone spec.)
263  */
264  p = uppername;
265  while (*name)
266  *p++ = pg_toupper((unsigned char) *name++);
267  *p = '\0';
268 
270  uppername,
271  HASH_FIND,
272  NULL);
273  if (tzp)
274  {
275  /* Timezone found in cache, nothing more to do */
276  return &tzp->tz;
277  }
278 
279  /*
280  * "GMT" is always sent to tzparse(), as per discussion above.
281  */
282  if (strcmp(uppername, "GMT") == 0)
283  {
284  if (!tzparse(uppername, &tzstate, true))
285  {
286  /* This really, really should not happen ... */
287  elog(ERROR, "could not initialize GMT time zone");
288  }
289  /* Use uppercase name as canonical */
290  strcpy(canonname, uppername);
291  }
292  else if (tzload(uppername, canonname, &tzstate, true) != 0)
293  {
294  if (uppername[0] == ':' || !tzparse(uppername, &tzstate, false))
295  {
296  /* Unknown timezone. Fail our call instead of loading GMT! */
297  return NULL;
298  }
299  /* For POSIX timezone specs, use uppercase name as canonical */
300  strcpy(canonname, uppername);
301  }
302 
303  /* Save timezone in the cache */
305  uppername,
306  HASH_ENTER,
307  NULL);
308 
309  /* hash_search already copied uppername into the hash key */
310  strcpy(tzp->tz.TZname, canonname);
311  memcpy(&tzp->tz.state, &tzstate, sizeof(tzstate));
312 
313  return &tzp->tz;
314 }
int tzload(const char *name, char *canonname, struct state *sp, bool doextend)
Definition: localtime.c:556
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:902
#define TZ_STRLEN_MAX
Definition: pgtime.h:44
#define ERROR
Definition: elog.h:43
static bool init_timezone_hashtable(void)
Definition: pgtz.c:209
bool tzparse(const char *name, struct state *sp, bool lastditch)
Definition: localtime.c:897
pg_tz tz
Definition: pgtz.c:202
struct state state
Definition: pgtz.h:63
static HTAB * timezone_cache
Definition: pgtz.c:205
Definition: regguts.h:298
char TZname[TZ_STRLEN_MAX+1]
Definition: pgtz.h:62
const char * name
Definition: encode.c:521
#define elog
Definition: elog.h:219
unsigned char pg_toupper(unsigned char ch)
Definition: pgstrcasecmp.c:105
pg_tz* pg_tzset_offset ( long  gmtoffset)

Definition at line 329 of file pgtz.c.

References pg_tzset(), SECS_PER_HOUR, SECS_PER_MINUTE, and snprintf().

Referenced by check_timezone().

330 {
331  long absoffset = (gmtoffset < 0) ? -gmtoffset : gmtoffset;
332  char offsetstr[64];
333  char tzname[128];
334 
335  snprintf(offsetstr, sizeof(offsetstr),
336  "%02ld", absoffset / SECS_PER_HOUR);
337  absoffset %= SECS_PER_HOUR;
338  if (absoffset != 0)
339  {
340  snprintf(offsetstr + strlen(offsetstr),
341  sizeof(offsetstr) - strlen(offsetstr),
342  ":%02ld", absoffset / SECS_PER_MINUTE);
343  absoffset %= SECS_PER_MINUTE;
344  if (absoffset != 0)
345  snprintf(offsetstr + strlen(offsetstr),
346  sizeof(offsetstr) - strlen(offsetstr),
347  ":%02ld", absoffset);
348  }
349  if (gmtoffset > 0)
350  snprintf(tzname, sizeof(tzname), "<-%s>+%s",
351  offsetstr, offsetstr);
352  else
353  snprintf(tzname, sizeof(tzname), "<+%s>-%s",
354  offsetstr, offsetstr);
355 
356  return pg_tzset(tzname);
357 }
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define SECS_PER_MINUTE
Definition: timestamp.h:88
pg_tz * pg_tzset(const char *name)
Definition: pgtz.c:243
#define SECS_PER_HOUR
Definition: timestamp.h:87
static bool scan_directory_ci ( const char *  dirname,
const char *  fname,
int  fnamelen,
char *  canonname,
int  canonnamelen 
)
static

Definition at line 151 of file pgtz.c.

References AllocateDir(), dirent::d_name, ereport, errcode_for_file_access(), errmsg(), FreeDir(), LOG, pg_strncasecmp(), ReadDir(), and strlcpy().

Referenced by pg_open_tzfile().

153 {
154  bool found = false;
155  DIR *dirdesc;
156  struct dirent *direntry;
157 
158  dirdesc = AllocateDir(dirname);
159  if (!dirdesc)
160  {
161  ereport(LOG,
163  errmsg("could not open directory \"%s\": %m", dirname)));
164  return false;
165  }
166 
167  while ((direntry = ReadDir(dirdesc, dirname)) != NULL)
168  {
169  /*
170  * Ignore . and .., plus any other "hidden" files. This is a security
171  * measure to prevent access to files outside the timezone directory.
172  */
173  if (direntry->d_name[0] == '.')
174  continue;
175 
176  if (strlen(direntry->d_name) == fnamelen &&
177  pg_strncasecmp(direntry->d_name, fname, fnamelen) == 0)
178  {
179  /* Found our match */
180  strlcpy(canonname, direntry->d_name, canonnamelen);
181  found = true;
182  break;
183  }
184  }
185 
186  FreeDir(dirdesc);
187 
188  return found;
189 }
#define LOG
Definition: elog.h:26
Definition: dirent.h:9
int pg_strncasecmp(const char *s1, const char *s2, size_t n)
Definition: pgstrcasecmp.c:69
Definition: dirent.c:25
int errcode_for_file_access(void)
Definition: elog.c:598
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2335
#define ereport(elevel, rest)
Definition: elog.h:122
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2401
int errmsg(const char *fmt,...)
Definition: elog.c:797
char d_name[MAX_PATH]
Definition: dirent.h:14
int FreeDir(DIR *dir)
Definition: fd.c:2444

Variable Documentation

HTAB* timezone_cache = NULL
static

Definition at line 205 of file pgtz.c.