Skip to content

Commit db273cd

Browse files
michaelpqpull[bot]
authored andcommitted
Extend sendFileWithContent() to handle custom content length in basebackup.c
sendFileWithContent() previously got the content length by using strlen(), assuming that the content given is always a string. Some patches are under discussion to pass binary contents to a base backup stream, where an arbitrary length needs to be given by the caller instead. The patch extends sendFileWithContent() to be able to handle this case, where len < 0 can be used to indicate an arbitrary length rather than rely on strlen() for the content length. A comment in sendFileWithContent() mentioned the backup_label file. However, this routine is used by more file types, like the tablespace map, so adjust it in passing. Author: David Steele Discussion: https://postgr.es/m/2daf8adc-8db7-4204-a7f2-a7e94e2bfa4b@pgmasters.net
1 parent 967c6e9 commit db273cd

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

src/backend/backup/basebackup.c

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ static bool verify_page_checksum(Page page, XLogRecPtr start_lsn,
9494
BlockNumber blkno,
9595
uint16 *expected_checksum);
9696
static void sendFileWithContent(bbsink *sink, const char *filename,
97-
const char *content,
97+
const char *content, int len,
9898
backup_manifest_info *manifest);
9999
static int64 _tarWriteHeader(bbsink *sink, const char *filename,
100100
const char *linktarget, struct stat *statbuf,
@@ -334,14 +334,14 @@ perform_base_backup(basebackup_options *opt, bbsink *sink)
334334
/* In the main tar, include the backup_label first... */
335335
backup_label = build_backup_content(backup_state, false);
336336
sendFileWithContent(sink, BACKUP_LABEL_FILE,
337-
backup_label, &manifest);
337+
backup_label, -1, &manifest);
338338
pfree(backup_label);
339339

340340
/* Then the tablespace_map file, if required... */
341341
if (opt->sendtblspcmapfile)
342342
{
343343
sendFileWithContent(sink, TABLESPACE_MAP,
344-
tablespace_map->data, &manifest);
344+
tablespace_map->data, -1, &manifest);
345345
sendtblspclinks = false;
346346
}
347347

@@ -601,7 +601,7 @@ perform_base_backup(basebackup_options *opt, bbsink *sink)
601601
* complete segment.
602602
*/
603603
StatusFilePath(pathbuf, walFileName, ".done");
604-
sendFileWithContent(sink, pathbuf, "", &manifest);
604+
sendFileWithContent(sink, pathbuf, "", -1, &manifest);
605605
}
606606

607607
/*
@@ -629,7 +629,7 @@ perform_base_backup(basebackup_options *opt, bbsink *sink)
629629

630630
/* unconditionally mark file as archived */
631631
StatusFilePath(pathbuf, fname, ".done");
632-
sendFileWithContent(sink, pathbuf, "", &manifest);
632+
sendFileWithContent(sink, pathbuf, "", -1, &manifest);
633633
}
634634

635635
/* Properly terminate the tar file. */
@@ -1037,26 +1037,29 @@ SendBaseBackup(BaseBackupCmd *cmd)
10371037

10381038
/*
10391039
* Inject a file with given name and content in the output tar stream.
1040+
*
1041+
* "len" can optionally be set to an arbitrary length of data sent. If set
1042+
* to -1, the content sent is treated as a string with strlen() as length.
10401043
*/
10411044
static void
10421045
sendFileWithContent(bbsink *sink, const char *filename, const char *content,
1043-
backup_manifest_info *manifest)
1046+
int len, backup_manifest_info *manifest)
10441047
{
10451048
struct stat statbuf;
1046-
int bytes_done = 0,
1047-
len;
1049+
int bytes_done = 0;
10481050
pg_checksum_context checksum_ctx;
10491051

10501052
if (pg_checksum_init(&checksum_ctx, manifest->checksum_type) < 0)
10511053
elog(ERROR, "could not initialize checksum of file \"%s\"",
10521054
filename);
10531055

1054-
len = strlen(content);
1056+
if (len < 0)
1057+
len = strlen(content);
10551058

10561059
/*
1057-
* Construct a stat struct for the backup_label file we're injecting in
1058-
* the tar.
1060+
* Construct a stat struct for the file we're injecting in the tar.
10591061
*/
1062+
10601063
/* Windows doesn't have the concept of uid and gid */
10611064
#ifdef WIN32
10621065
statbuf.st_uid = 0;

0 commit comments

Comments
 (0)