diff --git a/src/bin/pg_basebackup/pg_basebackup.c b/src/bin/pg_basebackup/pg_basebackup.c index 3d26e22..594755e 100644 --- a/src/bin/pg_basebackup/pg_basebackup.c +++ b/src/bin/pg_basebackup/pg_basebackup.c @@ -109,7 +109,6 @@ static bool reached_end_position(XLogRecPtr segendpos, uint32 timeline, bool segment_finished); static const char *get_tablespace_mapping(const char *dir); -static void update_tablespace_symlink(Oid oid, const char *old_dir); static void tablespace_list_append(const char *arg); @@ -1110,34 +1109,6 @@ get_tablespace_mapping(const char *dir) /* - * Update symlinks to reflect relocated tablespace. - */ -static void -update_tablespace_symlink(Oid oid, const char *old_dir) -{ - const char *new_dir = get_tablespace_mapping(old_dir); - - if (strcmp(old_dir, new_dir) != 0) - { - char *linkloc = psprintf("%s/pg_tblspc/%d", basedir, oid); - - if (unlink(linkloc) != 0 && errno != ENOENT) - { - fprintf(stderr, _("%s: could not remove symbolic link \"%s\": %s\n"), - progname, linkloc, strerror(errno)); - disconnect_and_exit(1); - } - if (symlink(new_dir, linkloc) != 0) - { - fprintf(stderr, _("%s: could not create symbolic link \"%s\": %s\n"), - progname, linkloc, strerror(errno)); - disconnect_and_exit(1); - } - } -} - - -/* * Receive a tar format stream from the connection to the server, and unpack * the contents of it into a directory. Only files, directories and * symlinks are supported, no other kinds of special files. @@ -1151,6 +1122,7 @@ ReceiveAndUnpackTarFile(PGconn *conn, PGresult *res, int rownum) { char current_path[MAXPGPATH]; char filename[MAXPGPATH]; + char mapped_tbs_path[MAXPGPATH]; int current_len_left; int current_padding = 0; bool basetablespace = PQgetisnull(res, rownum, 0); @@ -1286,7 +1258,17 @@ ReceiveAndUnpackTarFile(PGconn *conn, PGresult *res, int rownum) * Symbolic link */ filename[strlen(filename) - 1] = '\0'; /* Remove trailing slash */ - if (symlink(©buf[157], filename) != 0) + + /* + * Get the mapping for tablespace if any specified by user. + * This is to ensure that we create the symbolic link to + * the alternate tablespace location specified by user. + */ + strlcpy(mapped_tbs_path, + get_tablespace_mapping(©buf[157]), + sizeof(mapped_tbs_path)); + + if (symlink(mapped_tbs_path, filename) != 0) { fprintf(stderr, _("%s: could not create symbolic link from \"%s\" to \"%s\": %s\n"), @@ -1793,17 +1775,6 @@ BaseBackup(void) fprintf(stderr, "\n"); /* Need to move to next line */ } - if (format == 'p' && tablespace_dirs.head != NULL) - { - for (i = 0; i < PQntuples(res); i++) - { - Oid tblspc_oid = atooid(PQgetvalue(res, i, 0)); - - if (tblspc_oid) - update_tablespace_symlink(tblspc_oid, PQgetvalue(res, i, 1)); - } - } - PQclear(res); /*