From bd7b9bb4a1c7f04bee29665a84b6a2b255e1be71 Mon Sep 17 00:00:00 2001 From: Arthur Zakirov Date: Wed, 6 Mar 2019 14:21:20 +0300 Subject: [PATCH] PGPRO-2079: Use .partial file to write backup configuration files backup.control and backup_content.control --- src/catalog.c | 54 ++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 43 insertions(+), 11 deletions(-) diff --git a/src/catalog.c b/src/catalog.c index 87be436c..4b2e3587 100644 --- a/src/catalog.c +++ b/src/catalog.c @@ -569,22 +569,38 @@ pgBackupWriteControl(FILE *out, pgBackup *backup) void write_backup(pgBackup *backup) { - FILE *fp = NULL; - char conf_path[MAXPGPATH]; + FILE *fp = NULL; + char path[MAXPGPATH]; + char path_temp[MAXPGPATH]; + int errno_temp; - pgBackupGetPath(backup, conf_path, lengthof(conf_path), BACKUP_CONTROL_FILE); - fp = fopen(conf_path, "wt"); + pgBackupGetPath(backup, path, lengthof(path), BACKUP_CONTROL_FILE); + snprintf(path_temp, sizeof(path_temp), "%s.partial", path); + + fp = fopen(path_temp, "wt"); if (fp == NULL) - elog(ERROR, "Cannot open configuration file \"%s\": %s", conf_path, - strerror(errno)); + elog(ERROR, "Cannot open configuration file \"%s\": %s", + path_temp, strerror(errno)); pgBackupWriteControl(fp, backup); if (fflush(fp) != 0 || fsync(fileno(fp)) != 0 || fclose(fp)) + { + errno_temp = errno; + unlink(path_temp); elog(ERROR, "Cannot write configuration file \"%s\": %s", - conf_path, strerror(errno)); + path_temp, strerror(errno_temp)); + } + + if (rename(path_temp, path) < 0) + { + errno_temp = errno; + unlink(path_temp); + elog(ERROR, "Cannot rename configuration file \"%s\" to \"%s\": %s", + path_temp, path, strerror(errno_temp)); + } } /* @@ -595,20 +611,36 @@ write_backup_filelist(pgBackup *backup, parray *files, const char *root) { FILE *fp; char path[MAXPGPATH]; + char path_temp[MAXPGPATH]; + int errno_temp; pgBackupGetPath(backup, path, lengthof(path), DATABASE_FILE_LIST); + snprintf(path_temp, sizeof(path_temp), "%s.partial", path); - fp = fopen(path, "wt"); + fp = fopen(path_temp, "wt"); if (fp == NULL) - elog(ERROR, "Cannot open file list \"%s\": %s", path, - strerror(errno)); + elog(ERROR, "Cannot open file list \"%s\": %s", path_temp, + strerror(errno)); print_file_list(fp, files, root); if (fflush(fp) != 0 || fsync(fileno(fp)) != 0 || fclose(fp)) - elog(ERROR, "Cannot write file list \"%s\": %s", path, strerror(errno)); + { + errno_temp = errno; + unlink(path_temp); + elog(ERROR, "Cannot write file list \"%s\": %s", + path_temp, strerror(errno)); + } + + if (rename(path_temp, path) < 0) + { + errno_temp = errno; + unlink(path_temp); + elog(ERROR, "Cannot rename configuration file \"%s\" to \"%s\": %s", + path_temp, path, strerror(errno_temp)); + } } /*