From 957de55a85116696c2c316b076af4d831e03d0eb Mon Sep 17 00:00:00 2001 From: Artur Zakirov Date: Wed, 1 Mar 2017 12:35:26 +0300 Subject: [PATCH] Check that OLDDIR has an entry in links first --- dir.c | 10 ++++++++++ pg_probackup.h | 1 + restore.c | 35 +++++++++++++++++------------------ 3 files changed, 28 insertions(+), 18 deletions(-) diff --git a/dir.c b/dir.c index ffd37f69..6489c765 100644 --- a/dir.c +++ b/dir.c @@ -206,6 +206,16 @@ pgFileComparePathDesc(const void *f1, const void *f2) return -pgFileComparePath(f1, f2); } +/* Compare two pgFile with their linked directory path. */ +int +pgFileCompareLinked(const void *f1, const void *f2) +{ + pgFile *f1p = *(pgFile **)f1; + pgFile *f2p = *(pgFile **)f2; + + return strcmp(f1p->linked, f2p->linked); +} + /* Compare two pgFile with their size */ int pgFileCompareSize(const void *f1, const void *f2) diff --git a/pg_probackup.h b/pg_probackup.h index c896bf7d..29d47a6b 100644 --- a/pg_probackup.h +++ b/pg_probackup.h @@ -330,6 +330,7 @@ extern void pgFileFree(void *file); extern pg_crc32 pgFileGetCRC(pgFile *file); extern int pgFileComparePath(const void *f1, const void *f2); extern int pgFileComparePathDesc(const void *f1, const void *f2); +extern int pgFileCompareLinked(const void *f1, const void *f2); extern int pgFileCompareSize(const void *f1, const void *f2); extern int pgFileCompareMtime(const void *f1, const void *f2); extern int pgFileCompareMtimeDesc(const void *f1, const void *f2); diff --git a/restore.c b/restore.c index b6c0ebd7..692a1eb4 100644 --- a/restore.c +++ b/restore.c @@ -30,8 +30,6 @@ typedef struct TablespaceListCell struct TablespaceListCell *next; char old_dir[MAXPGPATH]; char new_dir[MAXPGPATH]; - bool checked; /* If this mapping was checked during - restore */ } TablespaceListCell; typedef struct TablespaceList @@ -508,10 +506,10 @@ restore_directories(const char *pg_data_dir, const char *backup_dir) dir_create_dir(to_path, DIR_PERMISSION); } - parray_walk(links, pgBackupFree); + parray_walk(links, pgFileFree); parray_free(links); - parray_walk(dirs, pgBackupFree); + parray_walk(dirs, pgFileFree); parray_free(dirs); } @@ -529,6 +527,7 @@ check_tablespace_mapping(pgBackup *backup) parray *links; size_t i; TablespaceListCell *cell; + pgFile *tmp_file = pgut_new(pgFile); links = parray_new(); @@ -537,7 +536,18 @@ check_tablespace_mapping(pgBackup *backup) elog(LOG, "check tablespace directories..."); - /* 1 - all linked directories should be empty */ + /* 1 - OLDDIR should has an entry in links */ + for (cell = tablespace_dirs.head; cell; cell = cell->next) + { + tmp_file->linked = cell->old_dir; + + if (parray_bsearch(links, tmp_file, pgFileCompareLinked) == NULL) + elog(ERROR, "--tablespace-mapping option's old directory " + "has not an entry in tablespace_map file: \"%s\"", + cell->old_dir); + } + + /* 2 - all linked directories should be empty */ for (i = 0; i < parray_num(links); i++) { pgFile *link = (pgFile *) parray_get(links, i); @@ -548,7 +558,6 @@ check_tablespace_mapping(pgBackup *backup) if (strcmp(link->linked, cell->old_dir) == 0) { linked_path = cell->new_dir; - cell->checked = true; break; } @@ -561,16 +570,8 @@ check_tablespace_mapping(pgBackup *backup) linked_path); } - /* 2 - OLDDIR should has an entry in links */ - for (cell = tablespace_dirs.head; cell; cell = cell->next) - { - if (!cell->checked) - elog(ERROR, "--tablespace-mapping option's old directory " - "has not an entry in tablespace_map file: \"%s\"", - cell->old_dir); - } - - parray_walk(links, pgBackupFree); + free(tmp_file); + parray_walk(links, pgFileFree); parray_free(links); } @@ -1049,8 +1050,6 @@ opt_tablespace_map(pgut_option *opt, const char *arg) elog(ERROR, "new directory is not an absolute path in tablespace mapping: %s\n", cell->new_dir); - cell->checked = false; - if (tablespace_dirs.tail) tablespace_dirs.tail->next = cell; else