diff --git a/COPYRIGHT b/COPYRIGHT index 17f79264..ad2ad272 100644 --- a/COPYRIGHT +++ b/COPYRIGHT @@ -1,5 +1,7 @@ -Copyright (c) 2008-2009, NIPPON TELEGRAPH AND TELEPHONE CORPORATION -All rights reserved. +Copyright (c) 2009-2010, NIPPON TELEGRAPH AND TELEPHONE CORPORATION + +Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group +Portions Copyright (c) 1994, The Regents of the University of California Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: diff --git a/backup.c b/backup.c index 83d73f38..ac2e01ab 100644 --- a/backup.c +++ b/backup.c @@ -2,7 +2,7 @@ * * backup.c: backup DB cluster, archived WAL, serverlog. * - * Copyright (c) 2009, NIPPON TELEGRAPH AND TELEPHONE CORPORATION + * Copyright (c) 2009-2010, NIPPON TELEGRAPH AND TELEPHONE CORPORATION * *------------------------------------------------------------------------- */ diff --git a/catalog.c b/catalog.c index f48e67e7..a42b192b 100644 --- a/catalog.c +++ b/catalog.c @@ -2,7 +2,7 @@ * * catalog.c: backup catalog opration * - * Copyright (c) 2009, NIPPON TELEGRAPH AND TELEPHONE CORPORATION + * Copyright (c) 2009-2010, NIPPON TELEGRAPH AND TELEPHONE CORPORATION * *------------------------------------------------------------------------- */ @@ -430,7 +430,7 @@ catalog_read_ini(const char *path) { 0 } }; - backup = (pgBackup *) pgut_malloc(sizeof(*backup)); + backup = pgut_new(pgBackup); catalog_init_config(backup); i = 0; diff --git a/data.c b/data.c index 77617f70..4a89fbaf 100644 --- a/data.c +++ b/data.c @@ -2,7 +2,7 @@ * * data.c: compress / uncompress data pages * - * Copyright (c) 2009, NIPPON TELEGRAPH AND TELEPHONE CORPORATION + * Copyright (c) 2009-2010, NIPPON TELEGRAPH AND TELEPHONE CORPORATION * *------------------------------------------------------------------------- */ diff --git a/delete.c b/delete.c index c21665a2..10179a86 100644 --- a/delete.c +++ b/delete.c @@ -2,7 +2,7 @@ * * delete.c: delete backup files. * - * Copyright (c) 2009, NIPPON TELEGRAPH AND TELEPHONE CORPORATION + * Copyright (c) 2009-2010, NIPPON TELEGRAPH AND TELEPHONE CORPORATION * *------------------------------------------------------------------------- */ diff --git a/dir.c b/dir.c index c0538167..3fd7a0f5 100644 --- a/dir.c +++ b/dir.c @@ -2,7 +2,7 @@ * * dir.c: directory operation utility. * - * Copyright (c) 2009, NIPPON TELEGRAPH AND TELEPHONE CORPORATION + * Copyright (c) 2009-2010, NIPPON TELEGRAPH AND TELEPHONE CORPORATION * *------------------------------------------------------------------------- */ diff --git a/init.c b/init.c index ce59288b..0964eb8e 100644 --- a/init.c +++ b/init.c @@ -2,7 +2,7 @@ * * init.c: manage backup catalog. * - * Copyright (c) 2009, NIPPON TELEGRAPH AND TELEPHONE CORPORATION + * Copyright (c) 2009-2010, NIPPON TELEGRAPH AND TELEPHONE CORPORATION * *------------------------------------------------------------------------- */ diff --git a/parray.c b/parray.c index 07b05f4e..5c61476b 100644 --- a/parray.c +++ b/parray.c @@ -2,7 +2,7 @@ * * parray.c: pointer array collection. * - * Copyright (c) 2009, NIPPON TELEGRAPH AND TELEPHONE CORPORATION + * Copyright (c) 2009-2010, NIPPON TELEGRAPH AND TELEPHONE CORPORATION * *------------------------------------------------------------------------- */ @@ -24,7 +24,7 @@ struct parray parray * parray_new(void) { - parray *a = pgut_malloc(sizeof(parray)); + parray *a = pgut_new(parray); a->data = NULL; a->used = 0; diff --git a/parray.h b/parray.h index 169cb194..de6f4296 100644 --- a/parray.h +++ b/parray.h @@ -2,7 +2,7 @@ * * parray.h: pointer array collection. * - * Copyright (c) 2009, NIPPON TELEGRAPH AND TELEPHONE CORPORATION + * Copyright (c) 2009-2010, NIPPON TELEGRAPH AND TELEPHONE CORPORATION * *------------------------------------------------------------------------- */ diff --git a/pg_rman.c b/pg_rman.c index f5eb2324..35a77d14 100644 --- a/pg_rman.c +++ b/pg_rman.c @@ -2,7 +2,7 @@ * * pg_rman.c: Backup/Recovery manager for PostgreSQL. * - * Copyright (c) 2009, NIPPON TELEGRAPH AND TELEPHONE CORPORATION + * Copyright (c) 2009-2010, NIPPON TELEGRAPH AND TELEPHONE CORPORATION * *------------------------------------------------------------------------- */ diff --git a/pg_rman.h b/pg_rman.h index d420350c..70fcd530 100644 --- a/pg_rman.h +++ b/pg_rman.h @@ -2,7 +2,7 @@ * * pg_rman.h: Backup/Recovery manager for PostgreSQL. * - * Copyright (c) 2009, NIPPON TELEGRAPH AND TELEPHONE CORPORATION + * Copyright (c) 2009-2010, NIPPON TELEGRAPH AND TELEPHONE CORPORATION * *------------------------------------------------------------------------- */ diff --git a/pgut/pgut-port.c b/pgut/pgut-port.c index 82cb05ff..865a01a0 100644 --- a/pgut/pgut-port.c +++ b/pgut/pgut-port.c @@ -2,7 +2,7 @@ * * pgut-port.c * - * Copyright (c) 2009, NIPPON TELEGRAPH AND TELEPHONE CORPORATION + * Copyright (c) 2009-2010, NIPPON TELEGRAPH AND TELEPHONE CORPORATION * *------------------------------------------------------------------------- */ diff --git a/pgut/pgut-port.h b/pgut/pgut-port.h index 9960b2e0..2dcd49b1 100644 --- a/pgut/pgut-port.h +++ b/pgut/pgut-port.h @@ -2,7 +2,7 @@ * * pgut-port.h * - * Copyright (c) 2009, NIPPON TELEGRAPH AND TELEPHONE CORPORATION + * Copyright (c) 2009-2010, NIPPON TELEGRAPH AND TELEPHONE CORPORATION * *------------------------------------------------------------------------- */ diff --git a/pgut/pgut.c b/pgut/pgut.c index 6d80888f..7ebe71d6 100644 --- a/pgut/pgut.c +++ b/pgut/pgut.c @@ -2,7 +2,7 @@ * * pgut.c * - * Copyright (c) 2009, NIPPON TELEGRAPH AND TELEPHONE CORPORATION + * Copyright (c) 2009-2010, NIPPON TELEGRAPH AND TELEPHONE CORPORATION * *------------------------------------------------------------------------- */ @@ -18,6 +18,15 @@ #include "pgut.h" +/* old gcc doesn't have LLONG_MAX. */ +#ifndef LLONG_MAX +#if defined(HAVE_LONG_INT_64) || !defined(HAVE_LONG_LONG_INT_64) +#define LLONG_MAX LONG_MAX +#else +#define LLONG_MAX INT64CONST(0x7FFFFFFFFFFFFFFF) +#endif +#endif + const char *PROGRAM_NAME = NULL; const char *dbname = NULL; @@ -111,7 +120,7 @@ option_merge(const pgut_option opts1[], const pgut_option opts2[]) size_t len2 = option_length(opts2); size_t n = len1 + len2; - result = pgut_malloc(sizeof(struct option) * (n + 1)); + result = pgut_newarray(struct option, n + 1); option_copy(result, opts1, len1); option_copy(result + len1, opts2, len2); memset(&result[n], 0, sizeof(pgut_option)); @@ -404,13 +413,7 @@ parse_int64(const char *value, int64 *result) if (strcmp(value, INFINITE_STR) == 0) { -#if defined(HAVE_LONG_INT_64) - *result = LONG_MAX; -#elif defined(HAVE_LONG_LONG_INT_64) *result = LLONG_MAX; -#else - *result = LONG_MAX; -#endif return true; } @@ -474,56 +477,50 @@ parse_uint64(const char *value, uint64 *result) return true; } -#ifdef WIN32 /* == not defined(HAVE_STRPTIME) */ - -static char * -strptime(const char *s, const char *format, struct tm *tm) -{ - int n; - char c; - - /* only support single format for now */ - if (strcmp(format, "%Y-%m-%d %H:%M:%S") != 0) - { - errno = EINVAL; - return NULL; - } - - n = sscanf(s, "%d-%d-%d %d:%d:%d%c", - &tm->tm_year, &tm->tm_mon, &tm->tm_mday, - &tm->tm_hour, &tm->tm_min, &tm->tm_sec, &c); - if (tm->tm_year >= 1900) - tm->tm_year -= 1900; /* years since 1900 */ - if (tm->tm_mon > 0) - tm->tm_mon -= 1; /* tm_mon is [0,11] */ - - if (n == 6) - return (char *) (s + strlen(s)); - else - return strchr(s, c); -} - -#endif - /* * Convert ISO-8601 format string to time_t value. */ bool parse_time(const char *value, time_t *time) { - char *endp; - struct tm tm = { 0 }; + size_t len; + char *tmp; + int i; + struct tm tm; + char junk[2]; - /* special case for invalid time */ - if (strcmp(value, "****-**-** **:**:**") == 0) - { - *time = (time_t) 0; - return true; - } + /* tmp = replace( value, !isalnum, ' ' ) */ + tmp = pgut_malloc(strlen(value) + + 1); + len = 0; + for (i = 0; value[i]; i++) + tmp[len++] = (IsAlnum(value[i]) ? value[i] : ' '); + tmp[len] = '\0'; - endp = strptime(value, "%Y-%m-%d %H:%M:%S", &tm); - if (endp == NULL || *endp) + /* parse for "YYYY-MM-DD HH:MI:SS" */ + tm.tm_year = 0; /* tm_year is year - 1900 */ + tm.tm_mon = 0; /* tm_mon is 0 - 11 */ + tm.tm_mday = 1; /* tm_mday is 1 - 31 */ + tm.tm_hour = 0; + tm.tm_min = 0; + tm.tm_sec = 0; + i = sscanf(tmp, "%04d %02d %02d %02d %02d %02d%1s", + &tm.tm_year, &tm.tm_mon, &tm.tm_mday, + &tm.tm_hour, &tm.tm_min, &tm.tm_sec, junk); + free(tmp); + + if (i < 1 || 6 < i) return false; + + /* adjust year */ + if (tm.tm_year < 100) + tm.tm_year += 2000 - 1900; + else if (tm.tm_year >= 1900) + tm.tm_year -= 1900; + + /* adjust month */ + if (i > 1) + tm.tm_mon -= 1; + *time = mktime(&tm); return true; @@ -670,14 +667,8 @@ pgut_readopt(const char *path, pgut_option options[], int elevel) if (!options) return; - fp = fopen(path, "rt"); - if (fp == NULL) - { - if (errno != ENOENT) - elog(WARNING, _("can't open config file \"%s\": %s"), path, - strerror(errno)); + if ((fp = pgut_fopen(path, "rt", true)) == NULL) return; - } while (fgets(buf, lengthof(buf), fp)) { @@ -823,7 +814,7 @@ parse_pair(const char buffer[], char key[], char value[]) if (end - start <= 0) { if (*start == '=') - elog(WARNING, _("syntax error in \"%s\"."), buffer); + elog(WARNING, "syntax error in \"%s\"", buffer); return false; } @@ -837,7 +828,7 @@ parse_pair(const char buffer[], char key[], char value[]) if (*start != '=') { - elog(WARNING, _("syntax error in \"%s\"."), buffer); + elog(WARNING, "syntax error in \"%s\"", buffer); return false; } @@ -854,7 +845,7 @@ parse_pair(const char buffer[], char key[], char value[]) if (*start != '\0' && *start != '#') { - elog(WARNING, _("syntax error in \"%s\"."), buffer); + elog(WARNING, "syntax error in \"%s\"", buffer); return false; } @@ -1297,7 +1288,7 @@ pgut_atexit_push(pgut_atexit_callback callback, void *userdata) AssertArg(callback != NULL); - item = pgut_malloc(sizeof(pgut_atexit_item)); + item = pgut_new(pgut_atexit_item); item->callback = callback; item->userdata = userdata; item->next = pgut_atexit_stack; @@ -1383,11 +1374,16 @@ help(bool details) printf(" --debug debug mode\n"); } printf(" --help show this help, then exit\n"); - printf(" --version output version information, then exit\n\n"); - if (PROGRAM_URL) - printf("Read the website for details. <%s>\n", PROGRAM_URL); - if (PROGRAM_EMAIL) - printf("Report bugs to <%s>.\n", PROGRAM_EMAIL); + printf(" --version output version information, then exit\n"); + + if (details && (PROGRAM_URL || PROGRAM_EMAIL)) + { + printf("\n"); + if (PROGRAM_URL) + printf("Read the website for details. <%s>\n", PROGRAM_URL); + if (PROGRAM_EMAIL) + printf("Report bugs to <%s>.\n", PROGRAM_EMAIL); + } } /* @@ -1482,7 +1478,7 @@ pgut_malloc(size_t size) char *ret; if ((ret = malloc(size)) == NULL) - elog(ERROR_NOMEM, "can't allocate memory (%lu bytes): %s", + elog(ERROR_NOMEM, "could not allocate memory (%lu bytes): %s", (unsigned long) size, strerror(errno)); return ret; } @@ -1493,7 +1489,7 @@ pgut_realloc(void *p, size_t size) char *ret; if ((ret = realloc(p, size)) == NULL) - elog(ERROR_NOMEM, "can't re-allocate memory (%lu bytes): %s", + elog(ERROR_NOMEM, "could not re-allocate memory (%lu bytes): %s", (unsigned long) size, strerror(errno)); return ret; } @@ -1507,7 +1503,7 @@ pgut_strdup(const char *str) return NULL; if ((ret = strdup(str)) == NULL) - elog(ERROR_NOMEM, "can't duplicate string \"%s\": %s", + elog(ERROR_NOMEM, "could not duplicate string \"%s\": %s", str, strerror(errno)); return ret; } @@ -1542,6 +1538,23 @@ strdup_trim(const char *str) return strdup_with_len(str, len); } +FILE * +pgut_fopen(const char *path, const char *mode, bool missing_ok) +{ + FILE *fp; + + if ((fp = fopen(path, mode)) == NULL) + { + if (missing_ok && errno == ENOENT) + return NULL; + + elog(ERROR_SYSTEM, "could not open file \"%s\": %s", + path, strerror(errno)); + } + + return fp; +} + #ifdef WIN32 static int select_win32(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, const struct timeval * timeout); #define select select_win32 diff --git a/pgut/pgut.h b/pgut/pgut.h index e2ec3b41..25d00046 100644 --- a/pgut/pgut.h +++ b/pgut/pgut.h @@ -2,7 +2,7 @@ * * pgut.h * - * Copyright (c) 2009, NIPPON TELEGRAPH AND TELEPHONE CORPORATION + * Copyright (c) 2009-2010, NIPPON TELEGRAPH AND TELEPHONE CORPORATION * *------------------------------------------------------------------------- */ @@ -51,9 +51,9 @@ typedef enum pgut_optsrc * b: bool (true) * B: bool (false) * f: pgut_optfn - * i: 32bit singed integer + * i: 32bit signed integer * u: 32bit unsigned integer - * I: 64bit singed integer + * I: 64bit signed integer * U: 64bit unsigned integer * s: string * t: time_t @@ -133,6 +133,14 @@ extern char *pgut_strdup(const char *str); extern char *strdup_with_len(const char *str, size_t len); extern char *strdup_trim(const char *str); +#define pgut_new(type) ((type *) pgut_malloc(sizeof(type))) +#define pgut_newarray(type, n) ((type *) pgut_malloc(sizeof(type) * (n))) + +/* + * file operations + */ +extern FILE *pgut_fopen(const char *path, const char *mode, bool missing_ok); + /* * elog */ diff --git a/restore.c b/restore.c index e8cd71ed..ce3e5b88 100644 --- a/restore.c +++ b/restore.c @@ -2,7 +2,7 @@ * * restore.c: restore DB cluster and archived WAL. * - * Copyright (c) 2009, NIPPON TELEGRAPH AND TELEPHONE CORPORATION + * Copyright (c) 2009-2010, NIPPON TELEGRAPH AND TELEPHONE CORPORATION * *------------------------------------------------------------------------- */ @@ -741,7 +741,7 @@ readTimeLineHistory(TimeLineID targetTLI) if (*ptr == '\0' || *ptr == '#') continue; - timeline = pgut_malloc(sizeof(*timeline)); + timeline = pgut_new(pgTimeLine); timeline->tli = 0; timeline->end.xlogid = 0; timeline->end.xrecoff = 0; @@ -785,7 +785,7 @@ readTimeLineHistory(TimeLineID targetTLI) _("Timeline IDs must be less than child timeline's ID.")); /* append target timeline */ - timeline = pgut_malloc(sizeof(*timeline)); + timeline = pgut_new(pgTimeLine); timeline->tli = targetTLI; timeline->end.xlogid = (uint32) -1; /* lsn in target timelie is valid */ timeline->end.xrecoff = (uint32) -1; /* lsn target timelie is valid */ diff --git a/show.c b/show.c index 4459a6ff..f7c16638 100644 --- a/show.c +++ b/show.c @@ -2,7 +2,7 @@ * * show.c: show backup catalog. * - * Copyright (c) 2009, NIPPON TELEGRAPH AND TELEPHONE CORPORATION + * Copyright (c) 2009-2010, NIPPON TELEGRAPH AND TELEPHONE CORPORATION * *------------------------------------------------------------------------- */ diff --git a/util.c b/util.c index e4152786..ebe7dd2d 100644 --- a/util.c +++ b/util.c @@ -2,7 +2,7 @@ * * util.c: log messages to log file or stderr, and misc code. * - * Copyright (c) 2009, NIPPON TELEGRAPH AND TELEPHONE CORPORATION + * Copyright (c) 2009-2010, NIPPON TELEGRAPH AND TELEPHONE CORPORATION * *------------------------------------------------------------------------- */ diff --git a/validate.c b/validate.c index 7e35e8c4..99ffd82c 100644 --- a/validate.c +++ b/validate.c @@ -2,7 +2,7 @@ * * validate.c: validate backup files. * - * Copyright (c) 2009, NIPPON TELEGRAPH AND TELEPHONE CORPORATION + * Copyright (c) 2009-2010, NIPPON TELEGRAPH AND TELEPHONE CORPORATION * *------------------------------------------------------------------------- */ diff --git a/xlog.c b/xlog.c index 693625e1..ddd13e20 100644 --- a/xlog.c +++ b/xlog.c @@ -2,7 +2,7 @@ * * xlog.c: Parse WAL files. * - * Copyright (c) 2009, NIPPON TELEGRAPH AND TELEPHONE CORPORATION + * Copyright (c) 2009-2010, NIPPON TELEGRAPH AND TELEPHONE CORPORATION * *------------------------------------------------------------------------- */