mirror of
https://github.com/postgrespro/pg_probackup.git
synced 2026-06-21 01:34:15 +02:00
[PBCKP-368] pg_stop_backup_write_file_helper to use pioWriteFile
This commit is contained in:
+19
-37
@@ -1789,45 +1789,31 @@ pg_stop_backup_consume(PGconn *conn, int server_version,
|
||||
}
|
||||
|
||||
/* get backup_label_content */
|
||||
result->backup_label_content = NULL;
|
||||
// if (!PQgetisnull(query_result, 0, backup_label_colno))
|
||||
result->backup_label_content_len = PQgetlength(query_result, 0, backup_label_colno);
|
||||
if (result->backup_label_content_len > 0)
|
||||
result->backup_label_content = pgut_strndup(PQgetvalue(query_result, 0, backup_label_colno),
|
||||
result->backup_label_content_len);
|
||||
result->backup_label_content = ft_strdupc(PQgetvalue(query_result, 0, backup_label_colno));
|
||||
|
||||
/* get tablespace_map_content */
|
||||
result->tablespace_map_content = NULL;
|
||||
// if (!PQgetisnull(query_result, 0, tablespace_map_colno))
|
||||
result->tablespace_map_content_len = PQgetlength(query_result, 0, tablespace_map_colno);
|
||||
if (result->tablespace_map_content_len > 0)
|
||||
result->tablespace_map_content = pgut_strndup(PQgetvalue(query_result, 0, tablespace_map_colno),
|
||||
result->tablespace_map_content_len);
|
||||
result->tablespace_map_content = ft_strdupc(PQgetvalue(query_result, 0, tablespace_map_colno));
|
||||
}
|
||||
|
||||
/*
|
||||
* helper routine used to write backup_label and tablespace_map in pg_stop_backup()
|
||||
*/
|
||||
void
|
||||
pg_stop_backup_write_file_helper(const char *path, const char *filename, const char *error_msg_filename,
|
||||
const void *data, size_t len, parray *file_list)
|
||||
pg_stop_backup_write_file_helper(pioDrive_i drive, const char *path, const char *filename, const char *error_msg_filename,
|
||||
ft_str_t data, parray *file_list)
|
||||
{
|
||||
FILE *fp;
|
||||
pgFile *file;
|
||||
char full_filename[MAXPGPATH];
|
||||
pioDrive_i drive = pioDriveForLocation(FIO_BACKUP_HOST);
|
||||
err_i err = $noerr();
|
||||
|
||||
join_path_components(full_filename, path, filename);
|
||||
fp = fio_fopen(FIO_BACKUP_HOST, full_filename, PG_BINARY_W);
|
||||
if (fp == NULL)
|
||||
elog(ERROR, "can't open %s file \"%s\": %s",
|
||||
error_msg_filename, full_filename, strerror(errno));
|
||||
|
||||
if (fio_fwrite(fp, data, len) != len ||
|
||||
fio_fflush(fp) != 0 ||
|
||||
fio_fclose(fp))
|
||||
elog(ERROR, "can't write %s file \"%s\": %s",
|
||||
error_msg_filename, full_filename, strerror(errno));
|
||||
err = $i(pioWriteFile, drive, .path = full_filename,
|
||||
.content = ft_str2bytes(data));
|
||||
if ($haserr(err))
|
||||
ft_logerr(FT_FATAL, $errmsg(err), "writting stop backup file");
|
||||
|
||||
/*
|
||||
* It's vital to check if files_list is initialized,
|
||||
@@ -1894,26 +1880,22 @@ pg_stop_backup(InstanceState *instanceState, pgBackup *backup, PGconn *pg_startb
|
||||
wait_wal_and_calculate_stop_lsn(xlog_path, stop_backup_result.lsn, backup);
|
||||
|
||||
/* Write backup_label and tablespace_map */
|
||||
Assert(stop_backup_result.backup_label_content != NULL);
|
||||
Assert(stop_backup_result.backup_label_content.len != 0);
|
||||
|
||||
/* Write backup_label */
|
||||
pg_stop_backup_write_file_helper(backup->database_dir, PG_BACKUP_LABEL_FILE, "backup label",
|
||||
stop_backup_result.backup_label_content, stop_backup_result.backup_label_content_len,
|
||||
backup_files_list);
|
||||
free(stop_backup_result.backup_label_content);
|
||||
stop_backup_result.backup_label_content = NULL;
|
||||
stop_backup_result.backup_label_content_len = 0;
|
||||
pg_stop_backup_write_file_helper(backup->backup_location,
|
||||
backup->database_dir, PG_BACKUP_LABEL_FILE, "backup label",
|
||||
stop_backup_result.backup_label_content, backup_files_list);
|
||||
ft_str_free(&stop_backup_result.backup_label_content);
|
||||
|
||||
/* Write tablespace_map */
|
||||
if (stop_backup_result.tablespace_map_content != NULL)
|
||||
if (stop_backup_result.tablespace_map_content.len != 0)
|
||||
{
|
||||
pg_stop_backup_write_file_helper(backup->database_dir, PG_TABLESPACE_MAP_FILE, "tablespace map",
|
||||
stop_backup_result.tablespace_map_content, stop_backup_result.tablespace_map_content_len,
|
||||
backup_files_list);
|
||||
free(stop_backup_result.tablespace_map_content);
|
||||
stop_backup_result.tablespace_map_content = NULL;
|
||||
stop_backup_result.tablespace_map_content_len = 0;
|
||||
pg_stop_backup_write_file_helper(backup->backup_location,
|
||||
backup->database_dir, PG_TABLESPACE_MAP_FILE, "tablespace map",
|
||||
stop_backup_result.tablespace_map_content, backup_files_list);
|
||||
}
|
||||
ft_str_free(&stop_backup_result.tablespace_map_content);
|
||||
|
||||
if (backup->stream)
|
||||
{
|
||||
|
||||
+7
-11
@@ -1041,19 +1041,17 @@ do_catchup(const char *source_pgdata, const char *dest_pgdata, int num_threads,
|
||||
wait_wal_and_calculate_stop_lsn(dest_xlog_path, stop_backup_result.lsn, ¤t);
|
||||
|
||||
/* Write backup_label */
|
||||
Assert(stop_backup_result.backup_label_content != NULL);
|
||||
Assert(stop_backup_result.backup_label_content.len != 0);
|
||||
if (!dry_run)
|
||||
{
|
||||
pg_stop_backup_write_file_helper(dest_pgdata, PG_BACKUP_LABEL_FILE, "backup label",
|
||||
stop_backup_result.backup_label_content, stop_backup_result.backup_label_content_len,
|
||||
NULL);
|
||||
pg_stop_backup_write_file_helper(local_location,
|
||||
dest_pgdata, PG_BACKUP_LABEL_FILE, "backup label",
|
||||
stop_backup_result.backup_label_content, NULL);
|
||||
}
|
||||
free(stop_backup_result.backup_label_content);
|
||||
stop_backup_result.backup_label_content = NULL;
|
||||
stop_backup_result.backup_label_content_len = 0;
|
||||
ft_str_free(&stop_backup_result.backup_label_content);
|
||||
|
||||
/* tablespace_map */
|
||||
if (stop_backup_result.tablespace_map_content != NULL)
|
||||
if (stop_backup_result.tablespace_map_content.len != 0)
|
||||
{
|
||||
// TODO what if tablespace is created during catchup?
|
||||
/* Because we have already created symlinks in pg_tblspc earlier,
|
||||
@@ -1063,10 +1061,8 @@ do_catchup(const char *source_pgdata, const char *dest_pgdata, int num_threads,
|
||||
* stop_backup_result.tablespace_map_content, stop_backup_result.tablespace_map_content_len,
|
||||
* NULL);
|
||||
*/
|
||||
free(stop_backup_result.tablespace_map_content);
|
||||
stop_backup_result.tablespace_map_content = NULL;
|
||||
stop_backup_result.tablespace_map_content_len = 0;
|
||||
}
|
||||
ft_str_free(&stop_backup_result.tablespace_map_content);
|
||||
|
||||
/* wait for end of wal streaming and calculate wal size transfered */
|
||||
if (!dry_run)
|
||||
|
||||
@@ -357,7 +357,14 @@ ft_inline ft_str_t ft_cstr(const char* ptr) {
|
||||
return (ft_str_t){.ptr = (char*)ptr, .len = ptr ? strlen(ptr) : 0};
|
||||
}
|
||||
|
||||
ft_inline ft_bytes_t ft_str2bytes(ft_str_t str) {
|
||||
return ft_bytes(str.ptr, str.len);
|
||||
}
|
||||
|
||||
ft_inline ft_str_t ft_strdup(ft_str_t str);
|
||||
ft_inline ft_str_t ft_strdupc(const char* str);
|
||||
/* use only if string was allocated */
|
||||
ft_inline void ft_str_free(ft_str_t *str);
|
||||
|
||||
/* print string into ft_malloc-ed buffer */
|
||||
extern ft_str_t ft_asprintf(const char *fmt, ...) ft_gnu_printf(1,2);
|
||||
|
||||
@@ -661,7 +661,7 @@ fobj_newstr(ft_str_t s, enum FOBJ_STR_ALLOC ownership) {
|
||||
str = fobj_reservestr(s.len);
|
||||
memcpy(fobj_getstr(str).ptr, s.ptr, s.len);
|
||||
if (ownership == FOBJ_STR_GIFTED)
|
||||
ft_free(s.ptr);
|
||||
ft_str_free(&s);
|
||||
return str;
|
||||
}
|
||||
|
||||
|
||||
@@ -332,7 +332,7 @@ ft_bytes_move(ft_bytes_t *dest, ft_bytes_t *src) {
|
||||
// String utils
|
||||
ft_inline char *
|
||||
ft_cstrdup(const char *str) {
|
||||
return (char*)ft_strdup(ft_cstr(str)).ptr;
|
||||
return (char*)ft_strdupc(str).ptr;
|
||||
}
|
||||
|
||||
ft_inline ft_str_t
|
||||
@@ -346,6 +346,18 @@ ft_strdup(ft_str_t str) {
|
||||
return str;
|
||||
}
|
||||
|
||||
ft_inline ft_str_t
|
||||
ft_strdupc(const char *str) {
|
||||
return ft_strdup(ft_cstr(str));
|
||||
}
|
||||
|
||||
ft_inline void
|
||||
ft_str_free(ft_str_t *str) {
|
||||
ft_free(str->ptr);
|
||||
str->ptr = NULL;
|
||||
str->len = 0;
|
||||
}
|
||||
|
||||
ft_inline bool
|
||||
ft_streq(ft_str_t str, ft_str_t oth) {
|
||||
return str.len == oth.len && strncmp(str.ptr, oth.ptr, str.len) == 0;
|
||||
|
||||
+4
-6
@@ -1247,10 +1247,8 @@ typedef struct PGStopBackupResult
|
||||
* Fields that store pg_catalog.pg_stop_backup() result
|
||||
*/
|
||||
XLogRecPtr lsn;
|
||||
size_t backup_label_content_len;
|
||||
char *backup_label_content;
|
||||
size_t tablespace_map_content_len;
|
||||
char *tablespace_map_content;
|
||||
ft_str_t backup_label_content;
|
||||
ft_str_t tablespace_map_content;
|
||||
} PGStopBackupResult;
|
||||
|
||||
extern bool backup_in_progress;
|
||||
@@ -1265,8 +1263,8 @@ extern void pg_stop_backup_send(PGconn *conn, int server_version, bool is_starte
|
||||
extern void pg_stop_backup_consume(PGconn *conn, int server_version,
|
||||
uint32 timeout, const char *query_text,
|
||||
PGStopBackupResult *result);
|
||||
extern void pg_stop_backup_write_file_helper(const char *path, const char *filename, const char *error_msg_filename,
|
||||
const void *data, size_t len, parray *file_list);
|
||||
extern void pg_stop_backup_write_file_helper(pioDrive_i drive, const char *path, const char *filename, const char *error_msg_filename,
|
||||
ft_str_t data, parray *file_list);
|
||||
extern XLogRecPtr wait_wal_lsn(const char *wal_segment_dir, XLogRecPtr lsn, bool is_start_lsn, TimeLineID tli,
|
||||
bool in_prev_segment, bool segment_only,
|
||||
int timeout_elevel, bool in_stream_dir);
|
||||
|
||||
Reference in New Issue
Block a user