From 807cc27ebe4f3cbb260aec85691b8710c844651f Mon Sep 17 00:00:00 2001 From: Grigory Smolkin Date: Wed, 27 Jan 2021 00:23:59 +0300 Subject: [PATCH] Fix overwriting of 0-sized postgresql.auto.conf in remote mode, restore nonedata files asynchronously --- src/data.c | 2 +- src/restore.c | 22 ++++++++++++++-------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/data.c b/src/data.c index a3baa3d3..d3f67f43 100644 --- a/src/data.c +++ b/src/data.c @@ -1145,7 +1145,7 @@ restore_non_data_file_internal(FILE *in, FILE *out, pgFile *file, if (read_len > 0) { - if (fio_fwrite(out, buf, read_len) != read_len) + if (fio_fwrite_async(out, buf, read_len) != read_len) elog(ERROR, "Cannot write to \"%s\": %s", to_fullpath, strerror(errno)); } diff --git a/src/restore.c b/src/restore.c index aa9bff9e..a1f009b0 100644 --- a/src/restore.c +++ b/src/restore.c @@ -1515,8 +1515,8 @@ update_recovery_options(pgBackup *backup, char postgres_auto_path[MAXPGPATH]; char postgres_auto_path_tmp[MAXPGPATH]; char path[MAXPGPATH]; - FILE *fp; - FILE *fp_tmp; + FILE *fp = NULL; + FILE *fp_tmp = NULL; struct stat st; char current_time_str[100]; /* postgresql.auto.conf parsing */ @@ -1540,9 +1540,13 @@ update_recovery_options(pgBackup *backup, strerror(errno)); } - fp = fio_open_stream(postgres_auto_path, FIO_DB_HOST); - if (fp == NULL && errno != ENOENT) - elog(ERROR, "cannot open \"%s\": %s", postgres_auto_path, strerror(errno)); + /* Kludge for 0-sized postgresql.auto.conf file. TODO: make something more intelligent */ + if (st.st_size > 0) + { + fp = fio_open_stream(postgres_auto_path, FIO_DB_HOST); + if (fp == NULL && errno != ENOENT) + elog(ERROR, "cannot open \"%s\": %s", postgres_auto_path, strerror(errno)); + } sprintf(postgres_auto_path_tmp, "%s.tmp", postgres_auto_path); fp_tmp = fio_fopen(postgres_auto_path_tmp, "w", FIO_DB_HOST); @@ -1582,9 +1586,11 @@ update_recovery_options(pgBackup *backup, if (fp) fio_close_stream(fp); - /* TODO: detect remote error */ - if (buf_len > 0) - fio_fwrite(fp_tmp, buf, buf_len); + /* Write data to postgresql.auto.conf.tmp */ + if (buf_len > 0 && + (fio_fwrite(fp_tmp, buf, buf_len) != buf_len)) + elog(ERROR, "Cannot write to \"%s\": %s", + postgres_auto_path_tmp, strerror(errno)); if (fio_fflush(fp_tmp) != 0 || fio_fclose(fp_tmp))