PostgreSQL Source Code git master
Loading...
Searching...
No Matches
pg_combinebackup.c File Reference
#include "postgres_fe.h"
#include <dirent.h>
#include <fcntl.h>
#include <limits.h>
#include "access/xlog_internal.h"
#include "backup_label.h"
#include "common/checksum_helper.h"
#include "common/controldata_utils.h"
#include "common/file_perm.h"
#include "common/file_utils.h"
#include "common/logging.h"
#include "common/relpath.h"
#include "copy_file.h"
#include "fe_utils/option_utils.h"
#include "fe_utils/version.h"
#include "getopt_long.h"
#include "lib/stringinfo.h"
#include "load_manifest.h"
#include "reconstruct.h"
#include "write_manifest.h"
Include dependency graph for pg_combinebackup.c:

Go to the source code of this file.

Data Structures

struct  cb_cleanup_dir
 
struct  cb_tablespace_mapping
 
struct  cb_options
 
struct  cb_tablespace
 

Macros

#define INCREMENTAL_PREFIX   "INCREMENTAL."
 
#define INCREMENTAL_PREFIX_LENGTH   (sizeof(INCREMENTAL_PREFIX) - 1)
 

Typedefs

typedef struct cb_cleanup_dir cb_cleanup_dir
 
typedef struct cb_tablespace_mapping cb_tablespace_mapping
 
typedef struct cb_options cb_options
 
typedef struct cb_tablespace cb_tablespace
 

Functions

static void add_tablespace_mapping (cb_options *opt, char *arg)
 
static StringInfo check_backup_label_files (int n_backups, char **backup_dirs)
 
static uint64 check_control_files (int n_backups, char **backup_dirs)
 
static void check_input_dir_permissions (char *dir)
 
static void cleanup_directories_atexit (void)
 
static void create_output_directory (char *dirname, cb_options *opt)
 
static void help (const char *progname)
 
static bool parse_oid (char *s, Oid *result)
 
static void process_directory_recursively (Oid tsoid, char *input_directory, char *output_directory, char *relative_path, int n_prior_backups, char **prior_backup_dirs, manifest_data **manifests, manifest_writer *mwriter, cb_options *opt)
 
static void remember_to_cleanup_directory (char *target_path, bool rmtopdir)
 
static void reset_directory_cleanup_list (void)
 
static cb_tablespacescan_for_existing_tablespaces (char *pathname, cb_options *opt)
 
static void slurp_file (int fd, char *filename, StringInfo buf, int maxlen)
 
int main (int argc, char *argv[])
 

Variables

static cb_cleanup_dircleanup_dir_list = NULL
 

Macro Definition Documentation

◆ INCREMENTAL_PREFIX

#define INCREMENTAL_PREFIX   "INCREMENTAL."

Definition at line 45 of file pg_combinebackup.c.

◆ INCREMENTAL_PREFIX_LENGTH

#define INCREMENTAL_PREFIX_LENGTH   (sizeof(INCREMENTAL_PREFIX) - 1)

Definition at line 46 of file pg_combinebackup.c.

Typedef Documentation

◆ cb_cleanup_dir

◆ cb_options

◆ cb_tablespace

◆ cb_tablespace_mapping

Function Documentation

◆ add_tablespace_mapping()

static void add_tablespace_mapping ( cb_options opt,
char arg 
)
static

Definition at line 458 of file pg_combinebackup.c.

459{
461 char *dst;
462 char *dst_ptr;
463 char *arg_ptr;
464
465 /*
466 * Basically, we just want to copy everything before the equals sign to
467 * tsmap->old_dir and everything afterwards to tsmap->new_dir, but if
468 * there's more or less than one equals sign, that's an error, and if
469 * there's an equals sign preceded by a backslash, don't treat it as a
470 * field separator but instead copy a literal equals sign.
471 */
472 dst_ptr = dst = tsmap->old_dir;
473 for (arg_ptr = arg; *arg_ptr != '\0'; arg_ptr++)
474 {
475 if (dst_ptr - dst >= MAXPGPATH)
476 pg_fatal("directory name too long");
477
478 if (*arg_ptr == '\\' && *(arg_ptr + 1) == '=')
479 ; /* skip backslash escaping = */
480 else if (*arg_ptr == '=' && (arg_ptr == arg || *(arg_ptr - 1) != '\\'))
481 {
482 if (tsmap->new_dir[0] != '\0')
483 pg_fatal("multiple \"=\" signs in tablespace mapping");
484 else
485 dst = dst_ptr = tsmap->new_dir;
486 }
487 else
488 *dst_ptr++ = *arg_ptr;
489 }
490 if (!tsmap->old_dir[0] || !tsmap->new_dir[0])
491 pg_fatal("invalid tablespace mapping format \"%s\", must be \"OLDDIR=NEWDIR\"", arg);
492
493 /*
494 * All tablespaces are created with absolute directories, so specifying a
495 * non-absolute path here would never match, possibly confusing users.
496 *
497 * In contrast to pg_basebackup, both the old and new directories are on
498 * the local machine, so the local machine's definition of an absolute
499 * path is the only relevant one.
500 */
501 if (!is_absolute_path(tsmap->old_dir))
502 pg_fatal("old directory is not an absolute path in tablespace mapping: %s",
503 tsmap->old_dir);
504
505 if (!is_absolute_path(tsmap->new_dir))
506 pg_fatal("new directory is not an absolute path in tablespace mapping: %s",
507 tsmap->new_dir);
508
509 /* Canonicalize paths to avoid spurious failures when comparing. */
510 canonicalize_path(tsmap->old_dir);
511 canonicalize_path(tsmap->new_dir);
512
513 /* Add it to the list. */
514 tsmap->next = opt->tsmappings;
515 opt->tsmappings = tsmap;
516}
Datum arg
Definition elog.c:1323
#define pg_malloc0_object(type)
Definition fe_memutils.h:61
#define pg_fatal(...)
#define MAXPGPATH
#define is_absolute_path(filename)
Definition port.h:105
void canonicalize_path(char *path)
Definition path.c:337
static int fb(int x)
cb_tablespace_mapping * tsmappings

References arg, canonicalize_path(), fb(), is_absolute_path, MAXPGPATH, pg_fatal, pg_malloc0_object, and cb_options::tsmappings.

Referenced by main().

◆ check_backup_label_files()

static StringInfo check_backup_label_files ( int  n_backups,
char **  backup_dirs 
)
static

Definition at line 523 of file pg_combinebackup.c.

524{
527 int i;
530
531 /* Try to read each backup_label file in turn, last to first. */
532 for (i = n_backups - 1; i >= 0; --i)
533 {
534 char pathbuf[MAXPGPATH];
535 int fd;
538 XLogRecPtr start_lsn;
540
541 /* Open the backup_label file. */
542 snprintf(pathbuf, MAXPGPATH, "%s/backup_label", backup_dirs[i]);
543 pg_log_debug("reading \"%s\"", pathbuf);
544 if ((fd = open(pathbuf, O_RDONLY, 0)) < 0)
545 pg_fatal("could not open file \"%s\": %m", pathbuf);
546
547 /*
548 * Slurp the whole file into memory.
549 *
550 * The exact size limit that we impose here doesn't really matter --
551 * most of what's supposed to be in the file is fixed size and quite
552 * short. However, the length of the backup_label is limited (at least
553 * by some parts of the code) to MAXPGPATH, so include that value in
554 * the maximum length that we tolerate.
555 */
556 slurp_file(fd, pathbuf, buf, 10000 + MAXPGPATH);
557
558 /* Close the file. */
559 if (close(fd) != 0)
560 pg_fatal("could not close file \"%s\": %m", pathbuf);
561
562 /* Parse the file contents. */
565
566 /*
567 * Sanity checks.
568 *
569 * XXX. It's actually not required that start_lsn == check_lsn. It
570 * would be OK if start_lsn > check_lsn provided that start_lsn is
571 * less than or equal to the relevant switchpoint. But at the moment
572 * we don't have that information.
573 */
574 if (i > 0 && previous_tli == 0)
575 pg_fatal("backup at \"%s\" is a full backup, but only the first backup should be a full backup",
576 backup_dirs[i]);
577 if (i == 0 && previous_tli != 0)
578 pg_fatal("backup at \"%s\" is an incremental backup, but the first backup should be a full backup",
579 backup_dirs[i]);
580 if (i < n_backups - 1 && start_tli != check_tli)
581 pg_fatal("backup at \"%s\" starts on timeline %u, but expected %u",
583 if (i < n_backups - 1 && start_lsn != check_lsn)
584 pg_fatal("backup at \"%s\" starts at LSN %X/%08X, but expected %X/%08X",
585 backup_dirs[i],
586 LSN_FORMAT_ARGS(start_lsn),
590
591 /*
592 * The last backup label in the chain needs to be saved for later use,
593 * while the others are only needed within this loop.
594 */
595 if (lastbuf == buf)
597 else
599 }
600
601 /* Free memory that we don't need any more. */
602 if (lastbuf != buf)
604
605 /*
606 * Return the data from the first backup_info that we read (which is the
607 * backup_label from the last directory specified on the command line).
608 */
609 return lastbuf;
610}
void parse_backup_label(char *filename, StringInfo buf, TimeLineID *start_tli, XLogRecPtr *start_lsn, TimeLineID *previous_tli, XLogRecPtr *previous_lsn)
#define close(a)
Definition win32.h:12
int i
Definition isn.c:77
#define pg_log_debug(...)
Definition logging.h:135
static void slurp_file(int fd, char *filename, StringInfo buf, int maxlen)
static char buf[DEFAULT_XLOG_SEG_SIZE]
#define snprintf
Definition port.h:261
static int fd(const char *x, int i)
void destroyStringInfo(StringInfo str)
Definition stringinfo.c:409
StringInfo makeStringInfo(void)
Definition stringinfo.c:72
void resetStringInfo(StringInfo str)
Definition stringinfo.c:126
#define LSN_FORMAT_ARGS(lsn)
Definition xlogdefs.h:47
uint64 XLogRecPtr
Definition xlogdefs.h:21
#define InvalidXLogRecPtr
Definition xlogdefs.h:28
uint32 TimeLineID
Definition xlogdefs.h:63

References buf, close, destroyStringInfo(), fb(), fd(), i, InvalidXLogRecPtr, LSN_FORMAT_ARGS, makeStringInfo(), MAXPGPATH, parse_backup_label(), pg_fatal, pg_log_debug, resetStringInfo(), slurp_file(), and snprintf.

Referenced by main().

◆ check_control_files()

static uint64 check_control_files ( int  n_backups,
char **  backup_dirs 
)
static

Definition at line 616 of file pg_combinebackup.c.

617{
618 int i;
619 uint64 system_identifier = 0; /* placate compiler */
620 uint32 data_checksum_version = PG_DATA_CHECKSUM_OFF; /* placate compiler */
621 bool data_checksum_mismatch = false;
622
623 /* Try to read each control file in turn, last to first. */
624 for (i = n_backups - 1; i >= 0; --i)
625 {
626 ControlFileData *control_file;
627 bool crc_ok;
628 char *controlpath;
629
631 pg_log_debug("reading \"%s\"", controlpath);
633
634 /* Control file contents not meaningful if CRC is bad. */
635 if (!crc_ok)
636 pg_fatal("%s: CRC is incorrect", controlpath);
637
638 /* Can't interpret control file if not current version. */
639 if (control_file->pg_control_version != PG_CONTROL_VERSION)
640 pg_fatal("%s: unexpected control file version",
642
643 /* System identifiers should all match. */
644 if (i == n_backups - 1)
645 system_identifier = control_file->system_identifier;
646 else if (system_identifier != control_file->system_identifier)
647 pg_fatal("%s: expected system identifier %" PRIu64 ", but found %" PRIu64,
648 controlpath, system_identifier,
649 control_file->system_identifier);
650
651 /*
652 * Detect checksum mismatches, but only if the last backup in the
653 * chain has checksums enabled.
654 */
655 if (i == n_backups - 1)
656 data_checksum_version = control_file->data_checksum_version;
657 else if (data_checksum_version != PG_DATA_CHECKSUM_OFF &&
658 data_checksum_version != control_file->data_checksum_version)
660
661 /* Release memory. */
662 pfree(control_file);
664 }
665
666 /*
667 * If debug output is enabled, make a note of the system identifier that
668 * we found in all of the relevant control files.
669 */
670 pg_log_debug("system identifier is %" PRIu64, system_identifier);
671
672 /*
673 * Warn the user if not all backups are in the same state with regards to
674 * checksums.
675 */
677 {
678 pg_log_warning("only some backups have checksums enabled");
679 pg_log_warning_hint("Disable, and optionally reenable, checksums on the output directory to avoid failures.");
680 }
681
682 return system_identifier;
683}
uint64_t uint64
Definition c.h:684
uint32_t uint32
Definition c.h:683
@ PG_DATA_CHECKSUM_OFF
Definition checksum.h:28
ControlFileData * get_controlfile_by_exact_path(const char *ControlFilePath, bool *crc_ok_p)
#define pg_log_warning_hint(...)
Definition logging.h:123
void pfree(void *pointer)
Definition mcxt.c:1619
#define PG_CONTROL_VERSION
Definition pg_control.h:25
#define pg_log_warning(...)
Definition pgfnames.c:24
char * psprintf(const char *fmt,...)
Definition psprintf.c:43
uint32 pg_control_version
Definition pg_control.h:133
uint32 data_checksum_version
Definition pg_control.h:232
uint64 system_identifier
Definition pg_control.h:118
#define XLOG_CONTROL_FILE

References ControlFileData::data_checksum_version, fb(), get_controlfile_by_exact_path(), i, pfree(), PG_CONTROL_VERSION, ControlFileData::pg_control_version, PG_DATA_CHECKSUM_OFF, pg_fatal, pg_log_debug, pg_log_warning, pg_log_warning_hint, psprintf(), ControlFileData::system_identifier, and XLOG_CONTROL_FILE.

Referenced by main().

◆ check_input_dir_permissions()

static void check_input_dir_permissions ( char dir)
static

Definition at line 692 of file pg_combinebackup.c.

693{
694 struct stat st;
695
696 if (stat(dir, &st) != 0)
697 pg_fatal("could not stat file \"%s\": %m", dir);
698
699 SetDataDirectoryCreatePerm(st.st_mode);
700}
void SetDataDirectoryCreatePerm(int dataDirMode)
Definition file_perm.c:34
#define stat
Definition win32_port.h:74

References pg_fatal, SetDataDirectoryCreatePerm(), stat::st_mode, and stat.

Referenced by main().

◆ cleanup_directories_atexit()

static void cleanup_directories_atexit ( void  )
static

Definition at line 706 of file pg_combinebackup.c.

707{
708 while (cleanup_dir_list != NULL)
709 {
711
712 if (dir->rmtopdir)
713 {
714 pg_log_info("removing output directory \"%s\"", dir->target_path);
715 if (!rmtree(dir->target_path, dir->rmtopdir))
716 pg_log_error("failed to remove output directory");
717 }
718 else
719 {
720 pg_log_info("removing contents of output directory \"%s\"",
721 dir->target_path);
722 if (!rmtree(dir->target_path, dir->rmtopdir))
723 pg_log_error("failed to remove contents of output directory");
724 }
725
727 pfree(dir);
728 }
729}
#define pg_log_error(...)
Definition logging.h:108
#define pg_log_info(...)
Definition logging.h:126
static cb_cleanup_dir * cleanup_dir_list
bool rmtree(const char *path, bool rmtopdir)
Definition rmtree.c:50
struct cb_cleanup_dir * next

References cleanup_dir_list, fb(), cb_cleanup_dir::next, pfree(), pg_log_error, pg_log_info, cb_cleanup_dir::rmtopdir, rmtree(), and cb_cleanup_dir::target_path.

Referenced by main().

◆ create_output_directory()

static void create_output_directory ( char dirname,
cb_options opt 
)
static

Definition at line 739 of file pg_combinebackup.c.

740{
741 switch (pg_check_dir(dirname))
742 {
743 case 0:
744 if (opt->dry_run)
745 {
746 pg_log_debug("would create directory \"%s\"", dirname);
747 return;
748 }
749 pg_log_debug("creating directory \"%s\"", dirname);
750 if (pg_mkdir_p(dirname, pg_dir_create_mode) == -1)
751 pg_fatal("could not create directory \"%s\": %m", dirname);
752 remember_to_cleanup_directory(dirname, true);
753 break;
754
755 case 1:
756 pg_log_debug("using existing directory \"%s\"", dirname);
757 remember_to_cleanup_directory(dirname, false);
758 break;
759
760 case 2:
761 case 3:
762 case 4:
763 pg_fatal("directory \"%s\" exists but is not empty", dirname);
764
765 case -1:
766 pg_fatal("could not access directory \"%s\": %m", dirname);
767 }
768}
int pg_dir_create_mode
Definition file_perm.c:18
static void remember_to_cleanup_directory(char *target_path, bool rmtopdir)
int pg_mkdir_p(char *path, int omode)
Definition pgmkdirp.c:57
int pg_check_dir(const char *dir)
Definition pgcheckdir.c:33

References cb_options::dry_run, pg_check_dir(), pg_dir_create_mode, pg_fatal, pg_log_debug, pg_mkdir_p(), and remember_to_cleanup_directory().

Referenced by main().

◆ help()

static void help ( const char progname)
static

Definition at line 778 of file pg_combinebackup.c.

779{
780 printf(_("%s reconstructs full backups from incrementals.\n\n"), progname);
781 printf(_("Usage:\n"));
782 printf(_(" %s [OPTION]... DIRECTORY...\n"), progname);
783 printf(_("\nOptions:\n"));
784 printf(_(" -d, --debug generate lots of debugging output\n"));
785 printf(_(" -k, --link link files instead of copying\n"));
786 printf(_(" -n, --dry-run do not actually do anything\n"));
787 printf(_(" -N, --no-sync do not wait for changes to be written safely to disk\n"));
788 printf(_(" -o, --output=DIRECTORY output directory\n"));
789 printf(_(" -T, --tablespace-mapping=OLDDIR=NEWDIR\n"
790 " relocate tablespace in OLDDIR to NEWDIR\n"));
791 printf(_(" --clone clone (reflink) files instead of copying\n"));
792 printf(_(" --copy copy files (default)\n"));
793 printf(_(" --copy-file-range copy using copy_file_range() system call\n"));
794 printf(_(" --manifest-checksums=SHA{224,256,384,512}|CRC32C|NONE\n"
795 " use algorithm for manifest checksums\n"));
796 printf(_(" --no-manifest suppress generation of backup manifest\n"));
797 printf(_(" --sync-method=METHOD set method for syncing files to disk\n"));
798 printf(_(" -V, --version output version information, then exit\n"));
799 printf(_(" -?, --help show this help, then exit\n"));
800
801 printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
802 printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
803}
#define _(x)
Definition elog.c:96
const char * progname
Definition main.c:44
#define printf(...)
Definition port.h:267

References _, fb(), printf, and progname.

◆ main()

int main ( int  argc,
char argv[] 
)

Definition at line 131 of file pg_combinebackup.c.

132{
133 static struct option long_options[] = {
134 {"debug", no_argument, NULL, 'd'},
135 {"dry-run", no_argument, NULL, 'n'},
136 {"no-sync", no_argument, NULL, 'N'},
137 {"output", required_argument, NULL, 'o'},
138 {"tablespace-mapping", required_argument, NULL, 'T'},
139 {"link", no_argument, NULL, 'k'},
140 {"manifest-checksums", required_argument, NULL, 1},
141 {"no-manifest", no_argument, NULL, 2},
142 {"sync-method", required_argument, NULL, 3},
143 {"clone", no_argument, NULL, 4},
144 {"copy", no_argument, NULL, 5},
145 {"copy-file-range", no_argument, NULL, 6},
146 {NULL, 0, NULL, 0}
147 };
148
149 const char *progname;
150 char *last_input_dir;
151 int i;
152 int optindex;
153 int c;
154 int n_backups;
155 int n_prior_backups;
156 uint32 version;
157 uint64 system_identifier;
158 char **prior_backup_dirs;
159 cb_options opt;
160 cb_tablespace *tablespaces;
161 cb_tablespace *ts;
165 char *pgdata;
166
167 pg_logging_init(argv[0]);
168 progname = get_progname(argv[0]);
169 set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_combinebackup"));
171
172 memset(&opt, 0, sizeof(opt));
176
177 /* process command-line options */
178 while ((c = getopt_long(argc, argv, "dknNo:T:",
179 long_options, &optindex)) != -1)
180 {
181 switch (c)
182 {
183 case 'd':
184 opt.debug = true;
186 break;
187 case 'k':
189 break;
190 case 'n':
191 opt.dry_run = true;
192 break;
193 case 'N':
194 opt.no_sync = true;
195 break;
196 case 'o':
197 opt.output = optarg;
198 break;
199 case 'T':
201 break;
202 case 1:
204 &opt.manifest_checksums))
205 pg_fatal("unrecognized checksum algorithm: \"%s\"",
206 optarg);
207 break;
208 case 2:
209 opt.no_manifest = true;
210 break;
211 case 3:
213 exit(1);
214 break;
215 case 4:
217 break;
218 case 5:
220 break;
221 case 6:
223 break;
224 default:
225 /* getopt_long already emitted a complaint */
226 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
227 exit(1);
228 }
229 }
230
231 if (optind >= argc)
232 {
233 pg_log_error("no input directories specified");
234 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
235 exit(1);
236 }
237
238 if (opt.output == NULL)
239 pg_fatal("no output directory specified");
240
241 /* If no manifest is needed, no checksums are needed, either. */
242 if (opt.no_manifest)
244
245 if (opt.dry_run)
246 {
247 pg_log_info("executing in dry-run mode");
248 pg_log_info_detail("The target directory will not be modified.");
249 }
250
251 /* Check that the platform supports the requested copy method. */
253 {
254#if (defined(HAVE_COPYFILE) && defined(COPYFILE_CLONE_FORCE)) || \
255 (defined(__linux__) && defined(FICLONE))
256
257 if (opt.dry_run)
258 pg_log_debug("would use cloning to copy files");
259 else
260 pg_log_debug("will use cloning to copy files");
261
262#else
263 pg_fatal("file cloning not supported on this platform");
264#endif
265 }
267 {
268#if defined(HAVE_COPY_FILE_RANGE)
269
270 if (opt.dry_run)
271 pg_log_debug("would use copy_file_range to copy blocks");
272 else
273 pg_log_debug("will use copy_file_range to copy blocks");
274
275#else
276 pg_fatal("copy_file_range not supported on this platform");
277#endif
278 }
279
280 /* Read the server version from the final backup. */
281 pgdata = argv[argc - 1];
282 version = get_pg_version(pgdata, NULL);
283 if (GET_PG_MAJORVERSION_NUM(version) < 10)
284 pg_fatal("server version too old");
285 pg_log_debug("read server version %u from file \"%s/%s\"",
286 GET_PG_MAJORVERSION_NUM(version), pgdata, "PG_VERSION");
287
288 /* Sanity-check control files. */
289 n_backups = argc - optind;
290 system_identifier = check_control_files(n_backups, argv + optind);
291
292 /* Sanity-check backup_label files, and get the contents of the last one. */
294
295 /*
296 * We'll need the pathnames to the prior backups. By "prior" we mean all
297 * but the last one listed on the command line.
298 */
299 n_prior_backups = argc - optind - 1;
300 prior_backup_dirs = argv + optind;
301
302 /* Load backup manifests. */
304
305 /*
306 * Validate the manifest system identifier against the backup system
307 * identifier.
308 */
309 for (i = 0; i < n_backups; i++)
310 {
311 if (manifests[i] &&
312 manifests[i]->system_identifier != system_identifier)
313 {
314 char *controlpath;
315
317
318 pg_fatal("%s: manifest system identifier is %" PRIu64 ", but control file has %" PRIu64,
320 manifests[i]->system_identifier,
321 system_identifier);
322 }
323 }
324
325 /* Figure out which tablespaces are going to be included in the output. */
326 last_input_dir = argv[argc - 1];
329
330 /*
331 * Create output directories.
332 *
333 * We create one output directory for the main data directory plus one for
334 * each non-in-place tablespace. create_output_directory() will arrange
335 * for those directories to be cleaned up on failure. In-place tablespaces
336 * aren't handled at this stage because they're located beneath the main
337 * output directory, and thus the cleanup of that directory will get rid
338 * of them. Plus, the pg_tblspc directory that needs to contain them
339 * doesn't exist yet.
340 */
343 for (ts = tablespaces; ts != NULL; ts = ts->next)
344 if (!ts->in_place)
346
347 /* If we need to write a backup_manifest, prepare to do so. */
348 if (!opt.dry_run && !opt.no_manifest)
349 {
350 mwriter = create_manifest_writer(opt.output, system_identifier);
351
352 /*
353 * Verify that we have a backup manifest for the final backup; else we
354 * won't have the WAL ranges for the resulting manifest.
355 */
357 pg_fatal("cannot generate a manifest because no manifest is available for the final input backup");
358 }
359 else
360 mwriter = NULL;
361
362 /* Write backup label into output directory. */
363 if (opt.dry_run)
364 pg_log_debug("would generate \"%s/backup_label\"", opt.output);
365 else
366 {
367 pg_log_debug("generating \"%s/backup_label\"", opt.output);
368 last_backup_label->cursor = 0;
371 }
372
373 /* Process everything that's not part of a user-defined tablespace. */
374 pg_log_debug("processing backup directory \"%s\"", last_input_dir);
377 manifests, mwriter, &opt);
378
379 /* Process user-defined tablespaces. */
380 for (ts = tablespaces; ts != NULL; ts = ts->next)
381 {
382 pg_log_debug("processing tablespace directory \"%s\"", ts->old_dir);
383
384 /*
385 * If it's a normal tablespace, we need to set up a symbolic link from
386 * pg_tblspc/${OID} to the target directory; if it's an in-place
387 * tablespace, we need to create a directory at pg_tblspc/${OID}.
388 */
389 if (!ts->in_place)
390 {
391 char linkpath[MAXPGPATH];
392
394 ts->oid);
395
396 if (opt.dry_run)
397 pg_log_debug("would create symbolic link from \"%s\" to \"%s\"",
398 linkpath, ts->new_dir);
399 else
400 {
401 pg_log_debug("creating symbolic link from \"%s\" to \"%s\"",
402 linkpath, ts->new_dir);
403 if (symlink(ts->new_dir, linkpath) != 0)
404 pg_fatal("could not create symbolic link from \"%s\" to \"%s\": %m",
405 linkpath, ts->new_dir);
406 }
407 }
408 else
409 {
410 if (opt.dry_run)
411 pg_log_debug("would create directory \"%s\"", ts->new_dir);
412 else
413 {
414 pg_log_debug("creating directory \"%s\"", ts->new_dir);
415 if (pg_mkdir_p(ts->new_dir, pg_dir_create_mode) == -1)
416 pg_fatal("could not create directory \"%s\": %m",
417 ts->new_dir);
418 }
419 }
420
421 /* OK, now handle the directory contents. */
424 manifests, mwriter, &opt);
425 }
426
427 /* Finalize the backup_manifest, if we're generating one. */
428 if (mwriter != NULL)
430 manifests[n_prior_backups]->first_wal_range);
431
432 /* fsync that output directory unless we've been told not to do so */
433 if (!opt.no_sync)
434 {
435 if (opt.dry_run)
436 pg_log_debug("would recursively fsync \"%s\"", opt.output);
437 else
438 {
439 pg_log_debug("recursively fsyncing \"%s\"", opt.output);
440 sync_pgdata(opt.output, version, opt.sync_method, true);
441 }
442 }
443
444 /* Warn about the possibility of compromising the backups, when link mode */
445 if (opt.copy_method == COPY_METHOD_LINK)
446 pg_log_warning("--link mode was used; any modifications to the output "
447 "directory might destructively modify input directories");
448
449 /* It's a success, so don't remove the output directories. */
451 exit(0);
452}
void write_backup_label(char *output_directory, StringInfo buf, pg_checksum_type checksum_type, manifest_writer *mwriter)
static void help(void)
Definition pg_config.c:71
#define PG_TEXTDOMAIN(domain)
Definition c.h:1343
bool pg_checksum_parse_type(char *name, pg_checksum_type *type)
@ CHECKSUM_TYPE_NONE
@ CHECKSUM_TYPE_CRC32C
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition exec.c:430
@ COPY_METHOD_CLONE
Definition copy_file.h:21
@ COPY_METHOD_LINK
Definition copy_file.h:27
@ COPY_METHOD_COPY
Definition copy_file.h:22
@ COPY_METHOD_COPY_FILE_RANGE
Definition copy_file.h:23
uint32 get_pg_version(const char *datadir, char **version_str)
Definition version.c:44
@ DATA_DIR_SYNC_METHOD_FSYNC
Definition file_utils.h:29
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Definition getopt_long.c:60
#define no_argument
Definition getopt_long.h:25
#define required_argument
Definition getopt_long.h:26
static pg_noreturn void manifest_data ** load_backup_manifests(int n_backups, char **backup_directories)
void pg_logging_increase_verbosity(void)
Definition logging.c:187
void pg_logging_init(const char *argv0)
Definition logging.c:85
#define pg_log_info_detail(...)
Definition logging.h:129
#define pg_log_error_hint(...)
Definition logging.h:114
void handle_help_version_opts(int argc, char *argv[], const char *fixed_progname, help_handler hlp)
bool parse_sync_method(const char *optarg, DataDirSyncMethod *sync_method)
static cb_tablespace * scan_for_existing_tablespaces(char *pathname, cb_options *opt)
static void process_directory_recursively(Oid tsoid, char *input_directory, char *output_directory, char *relative_path, int n_prior_backups, char **prior_backup_dirs, manifest_data **manifests, manifest_writer *mwriter, cb_options *opt)
static void create_output_directory(char *dirname, cb_options *opt)
static void check_input_dir_permissions(char *dir)
static uint64 check_control_files(int n_backups, char **backup_dirs)
static void cleanup_directories_atexit(void)
static StringInfo check_backup_label_files(int n_backups, char **backup_dirs)
static void add_tablespace_mapping(cb_options *opt, char *arg)
static void reset_directory_cleanup_list(void)
PGDLLIMPORT int optind
Definition getopt.c:47
PGDLLIMPORT char * optarg
Definition getopt.c:49
const char * get_progname(const char *argv0)
Definition path.c:669
#define InvalidOid
char * c
#define PG_TBLSPC_DIR
Definition relpath.h:41
DataDirSyncMethod sync_method
CopyMethod copy_method
pg_checksum_type manifest_checksums
char new_dir[MAXPGPATH]
struct cb_tablespace * next
char old_dir[MAXPGPATH]
#define GET_PG_MAJORVERSION_NUM(v)
Definition version.h:19
#define symlink(oldpath, newpath)
Definition win32_port.h:242
manifest_writer * create_manifest_writer(char *directory, uint64 system_identifier)
void finalize_manifest(manifest_writer *mwriter, manifest_wal_range *first_wal_range)

References add_tablespace_mapping(), check_backup_label_files(), check_control_files(), check_input_dir_permissions(), CHECKSUM_TYPE_CRC32C, CHECKSUM_TYPE_NONE, cleanup_directories_atexit(), cb_options::copy_method, COPY_METHOD_CLONE, COPY_METHOD_COPY, COPY_METHOD_COPY_FILE_RANGE, COPY_METHOD_LINK, create_manifest_writer(), create_output_directory(), DATA_DIR_SYNC_METHOD_FSYNC, cb_options::debug, cb_options::dry_run, fb(), finalize_manifest(), GET_PG_MAJORVERSION_NUM, get_pg_version(), get_progname(), getopt_long(), handle_help_version_opts(), help(), i, cb_tablespace::in_place, InvalidOid, load_backup_manifests(), cb_options::manifest_checksums, MAXPGPATH, cb_tablespace::new_dir, cb_tablespace::next, no_argument, cb_options::no_manifest, cb_options::no_sync, cb_tablespace::oid, cb_tablespace::old_dir, optarg, optind, cb_options::output, parse_sync_method(), pg_checksum_parse_type(), pg_dir_create_mode, pg_fatal, pg_log_debug, pg_log_error, pg_log_error_hint, pg_log_info, pg_log_info_detail, pg_log_warning, pg_logging_increase_verbosity(), pg_logging_init(), pg_mkdir_p(), PG_TBLSPC_DIR, PG_TEXTDOMAIN, process_directory_recursively(), progname, psprintf(), required_argument, reset_directory_cleanup_list(), scan_for_existing_tablespaces(), set_pglocale_pgservice(), snprintf, symlink, cb_options::sync_method, write_backup_label(), and XLOG_CONTROL_FILE.

◆ parse_oid()

static bool parse_oid ( char s,
Oid result 
)
static

Definition at line 811 of file pg_combinebackup.c.

812{
813 Oid oid;
814 char *ep;
815
816 errno = 0;
817 oid = strtoul(s, &ep, 10);
818 if (errno != 0 || *ep != '\0' || oid < 1 || oid > PG_UINT32_MAX)
819 return false;
820
821 *result = oid;
822 return true;
823}
#define PG_UINT32_MAX
Definition c.h:733
uint32 result
unsigned int Oid

References fb(), PG_UINT32_MAX, and result.

Referenced by process_directory_recursively(), and scan_for_existing_tablespaces().

◆ process_directory_recursively()

static void process_directory_recursively ( Oid  tsoid,
char input_directory,
char output_directory,
char relative_path,
int  n_prior_backups,
char **  prior_backup_dirs,
manifest_data **  manifests,
manifest_writer mwriter,
cb_options opt 
)
static

Definition at line 845 of file pg_combinebackup.c.

854{
855 char ifulldir[MAXPGPATH];
856 char ofulldir[MAXPGPATH];
858 DIR *dir;
859 struct dirent *de;
860 bool is_pg_tblspc = false;
861 bool is_pg_wal = false;
862 bool is_incremental_dir = false;
864 pg_checksum_type checksum_type;
865
866 /*
867 * Classify this directory.
868 *
869 * We set is_pg_tblspc only for the toplevel pg_tblspc directory, because
870 * the symlinks in that specific directory require special handling.
871 *
872 * We set is_pg_wal for the toplevel WAL directory and all of its
873 * subdirectories, because those files are not included in the backup
874 * manifest and hence need special treatment. (Since incremental backup
875 * does not exist in pre-v10 versions, we don't have to worry about the
876 * old pg_xlog naming.)
877 *
878 * We set is_incremental_dir for directories that can contain incremental
879 * files requiring reconstruction. If such files occur outside these
880 * directories, we want to just copy them straight to the output
881 * directory. This is to protect against a user creating a file with a
882 * strange name like INCREMENTAL.config and then complaining that
883 * incremental backups don't work properly. The test here is a bit tricky:
884 * incremental files occur in subdirectories of base, in pg_global itself,
885 * and in subdirectories of pg_tblspc only if in-place tablespaces are
886 * used.
887 */
888 if (OidIsValid(tsoid))
889 is_incremental_dir = true;
890 else if (relative_path != NULL)
891 {
893 is_pg_wal = (strcmp(relative_path, "pg_wal") == 0 ||
894 strncmp(relative_path, "pg_wal/", 7) == 0);
895 is_incremental_dir = strncmp(relative_path, "base/", 5) == 0 ||
896 strcmp(relative_path, "global") == 0 ||
898 }
899
900 /*
901 * If we're under pg_wal, then we don't need checksums, because these
902 * files aren't included in the backup manifest. Otherwise use whatever
903 * type of checksum is configured.
904 */
905 if (!is_pg_wal)
906 checksum_type = opt->manifest_checksums;
907 else
908 checksum_type = CHECKSUM_TYPE_NONE;
909
910 /*
911 * Append the relative path to the input and output directories, and
912 * figure out the appropriate prefix to add to files in this directory
913 * when looking them up in a backup manifest.
914 */
915 if (relative_path == NULL)
916 {
919 if (OidIsValid(tsoid))
921 else
922 manifest_prefix[0] = '\0';
923 }
924 else
925 {
930 if (OidIsValid(tsoid))
931 snprintf(manifest_prefix, MAXPGPATH, "%s/%u/%s/",
933 else
935 }
936
937 /*
938 * Toplevel output directories have already been created by the time this
939 * function is called, but any subdirectories are our responsibility.
940 */
941 if (relative_path != NULL)
942 {
943 if (opt->dry_run)
944 pg_log_debug("would create directory \"%s\"", ofulldir);
945 else
946 {
947 pg_log_debug("creating directory \"%s\"", ofulldir);
949 pg_fatal("could not create directory \"%s\": %m", ofulldir);
950 }
951 }
952
953 /* It's time to scan the directory. */
954 if ((dir = opendir(ifulldir)) == NULL)
955 pg_fatal("could not open directory \"%s\": %m", ifulldir);
956 while (errno = 0, (de = readdir(dir)) != NULL)
957 {
959 char ifullpath[MAXPGPATH];
960 char ofullpath[MAXPGPATH];
962 Oid oid = InvalidOid;
963 int checksum_length = 0;
964 uint8 *checksum_payload = NULL;
965 pg_checksum_context checksum_ctx;
966
967 /* Ignore "." and ".." entries. */
968 if (strcmp(de->d_name, ".") == 0 ||
969 strcmp(de->d_name, "..") == 0)
970 continue;
971
972 /* Construct input path. */
973 snprintf(ifullpath, MAXPGPATH, "%s/%s", ifulldir, de->d_name);
974
975 /* Figure out what kind of directory entry this is. */
977 if (type == PGFILETYPE_ERROR)
978 exit(1);
979
980 /*
981 * If we're processing pg_tblspc, then check whether the filename
982 * looks like it could be a tablespace OID. If so, and if the
983 * directory entry is a symbolic link or a directory, skip it.
984 *
985 * Our goal here is to ignore anything that would have been considered
986 * by scan_for_existing_tablespaces to be a tablespace.
987 */
988 if (is_pg_tblspc && parse_oid(de->d_name, &oid) &&
990 continue;
991
992 /* If it's a directory, recurse. */
993 if (type == PGFILETYPE_DIR)
994 {
996
997 /* Append new pathname component to relative path. */
998 if (relative_path == NULL)
1000 else
1002 de->d_name);
1003
1004 /* And recurse. */
1009 manifests, mwriter, opt);
1010 continue;
1011 }
1012
1013 /* Skip anything that's not a regular file. */
1014 if (type != PGFILETYPE_REG)
1015 {
1016 if (type == PGFILETYPE_LNK)
1017 pg_log_warning("skipping symbolic link \"%s\"", ifullpath);
1018 else
1019 pg_log_warning("skipping special file \"%s\"", ifullpath);
1020 continue;
1021 }
1022
1023 /*
1024 * Skip the backup_label and backup_manifest files; they require
1025 * special handling and are handled elsewhere.
1026 */
1027 if (relative_path == NULL &&
1028 (strcmp(de->d_name, "backup_label") == 0 ||
1029 strcmp(de->d_name, "backup_manifest") == 0))
1030 continue;
1031
1032 /*
1033 * If it's an incremental file, hand it off to the reconstruction
1034 * code, which will figure out what to do.
1035 */
1036 if (is_incremental_dir &&
1037 strncmp(de->d_name, INCREMENTAL_PREFIX,
1039 {
1040 /* Output path should not include "INCREMENTAL." prefix. */
1042 de->d_name + INCREMENTAL_PREFIX_LENGTH);
1043
1044
1045 /* Manifest path likewise omits incremental prefix. */
1047 de->d_name + INCREMENTAL_PREFIX_LENGTH);
1048
1049 /* Reconstruction logic will do the rest. */
1052 de->d_name + INCREMENTAL_PREFIX_LENGTH,
1055 manifests,
1057 checksum_type,
1058 &checksum_length,
1059 &checksum_payload,
1060 opt->copy_method,
1061 opt->debug,
1062 opt->dry_run);
1063 }
1064 else
1065 {
1066 /* Construct the path that the backup_manifest will use. */
1068 de->d_name);
1069
1070 /*
1071 * It's not an incremental file, so we need to copy the entire
1072 * file to the output directory.
1073 *
1074 * If a checksum of the required type already exists in the
1075 * backup_manifest for the final input directory, we can save some
1076 * work by reusing that checksum instead of computing a new one.
1077 */
1078 if (checksum_type != CHECKSUM_TYPE_NONE &&
1080 {
1081 manifest_file *mfile;
1082
1085 if (mfile == NULL)
1086 {
1087 char *bmpath;
1088
1089 /*
1090 * The directory is out of sync with the backup_manifest,
1091 * so emit a warning.
1092 */
1093 bmpath = psprintf("%s/%s", input_directory,
1094 "backup_manifest");
1095 pg_log_warning("manifest file \"%s\" contains no entry for file \"%s\"",
1097 pfree(bmpath);
1098 }
1099 else if (mfile->checksum_type == checksum_type)
1100 {
1101 checksum_length = mfile->checksum_length;
1102 checksum_payload = mfile->checksum_payload;
1103 }
1104 }
1105
1106 /*
1107 * If we're reusing a checksum, then we don't need copy_file() to
1108 * compute one for us, but otherwise, it needs to compute whatever
1109 * type of checksum we need.
1110 */
1111 if (checksum_length != 0)
1112 pg_checksum_init(&checksum_ctx, CHECKSUM_TYPE_NONE);
1113 else
1114 pg_checksum_init(&checksum_ctx, checksum_type);
1115
1116 /* Actually copy the file. */
1117 snprintf(ofullpath, MAXPGPATH, "%s/%s", ofulldir, de->d_name);
1118 copy_file(ifullpath, ofullpath, &checksum_ctx,
1119 opt->copy_method, opt->dry_run);
1120
1121 /*
1122 * If copy_file() performed a checksum calculation for us, then
1123 * save the results (except in dry-run mode, when there's no
1124 * point).
1125 */
1126 if (checksum_ctx.type != CHECKSUM_TYPE_NONE && !opt->dry_run)
1127 {
1128 checksum_payload = pg_malloc(PG_CHECKSUM_MAX_LENGTH);
1129 checksum_length = pg_checksum_final(&checksum_ctx,
1130 checksum_payload);
1131 }
1132 }
1133
1134 /* Generate manifest entry, if needed. */
1135 if (mwriter != NULL)
1136 {
1137 struct stat sb;
1138
1139 /*
1140 * In order to generate a manifest entry, we need the file size
1141 * and mtime. We have no way to know the correct mtime except to
1142 * stat() the file, so just do that and get the size as well.
1143 *
1144 * If we didn't need the mtime here, we could try to obtain the
1145 * file size from the reconstruction or file copy process above,
1146 * although that is actually not convenient in all cases. If we
1147 * write the file ourselves then clearly we can keep a count of
1148 * bytes, but if we use something like CopyFile() then it's
1149 * trickier. Since we have to stat() anyway to get the mtime,
1150 * there's no point in worrying about it.
1151 */
1152 if (stat(ofullpath, &sb) < 0)
1153 pg_fatal("could not stat file \"%s\": %m", ofullpath);
1154
1155 /* OK, now do the work. */
1157 sb.st_size, sb.st_mtime,
1158 checksum_type, checksum_length,
1159 checksum_payload);
1160 }
1161
1162 /* Avoid leaking memory. */
1163 if (checksum_payload != NULL)
1164 pg_free(checksum_payload);
1165 }
1166
1167 closedir(dir);
1168}
uint8_t uint8
Definition c.h:681
#define OidIsValid(objectId)
Definition c.h:917
int pg_checksum_final(pg_checksum_context *context, uint8 *output)
int pg_checksum_init(pg_checksum_context *context, pg_checksum_type type)
#define PG_CHECKSUM_MAX_LENGTH
pg_checksum_type
void copy_file(const char *fromfile, const char *tofile)
Definition copydir.c:134
int closedir(DIR *)
Definition dirent.c:127
struct dirent * readdir(DIR *)
Definition dirent.c:78
DIR * opendir(const char *)
Definition dirent.c:33
void * pg_malloc(size_t size)
Definition fe_memutils.c:53
void pg_free(void *ptr)
PGFileType get_dirent_type(const char *path, const struct dirent *de, bool look_through_symlinks, int elevel)
Definition file_utils.c:547
PGFileType
Definition file_utils.h:19
@ PGFILETYPE_LNK
Definition file_utils.h:24
@ PGFILETYPE_DIR
Definition file_utils.h:23
@ PGFILETYPE_REG
Definition file_utils.h:22
@ PGFILETYPE_ERROR
Definition file_utils.h:20
@ PG_LOG_ERROR
Definition logging.h:43
#define INCREMENTAL_PREFIX_LENGTH
#define INCREMENTAL_PREFIX
static bool parse_oid(char *s, Oid *result)
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition strlcpy.c:45
void reconstruct_from_incremental_file(char *input_filename, char *output_filename, char *relative_path, char *bare_file_name, int n_prior_backups, char **prior_backup_dirs, manifest_data **manifests, char *manifest_path, pg_checksum_type checksum_type, int *checksum_length, uint8 **checksum_payload, CopyMethod copy_method, bool debug, bool dry_run)
Definition reconstruct.c:88
#define PG_TBLSPC_DIR_SLASH
Definition relpath.h:42
Definition dirent.c:26
uint8 * checksum_payload
pg_checksum_type checksum_type
pg_checksum_type type
const char * type
#define mkdir(a, b)
Definition win32_port.h:80
void add_file_to_manifest(manifest_writer *mwriter, const char *manifest_path, uint64 size, time_t mtime, pg_checksum_type checksum_type, int checksum_length, uint8 *checksum_payload)

References add_file_to_manifest(), manifest_file::checksum_length, manifest_file::checksum_payload, manifest_file::checksum_type, CHECKSUM_TYPE_NONE, closedir(), copy_file(), cb_options::copy_method, cb_options::debug, cb_options::dry_run, fb(), get_dirent_type(), INCREMENTAL_PREFIX, INCREMENTAL_PREFIX_LENGTH, InvalidOid, cb_options::manifest_checksums, MAXPGPATH, mkdir, OidIsValid, opendir(), parse_oid(), pfree(), pg_checksum_final(), pg_checksum_init(), PG_CHECKSUM_MAX_LENGTH, pg_dir_create_mode, pg_fatal, pg_free(), pg_log_debug, PG_LOG_ERROR, pg_log_warning, pg_malloc(), PG_TBLSPC_DIR, PG_TBLSPC_DIR_SLASH, PGFILETYPE_DIR, PGFILETYPE_ERROR, PGFILETYPE_LNK, PGFILETYPE_REG, process_directory_recursively(), psprintf(), readdir(), reconstruct_from_incremental_file(), snprintf, stat, strlcpy(), type, and pg_checksum_context::type.

Referenced by main(), and process_directory_recursively().

◆ remember_to_cleanup_directory()

static void remember_to_cleanup_directory ( char target_path,
bool  rmtopdir 
)
static

Definition at line 1174 of file pg_combinebackup.c.

1175{
1177
1178 dir->target_path = target_path;
1179 dir->rmtopdir = rmtopdir;
1180 dir->next = cleanup_dir_list;
1181 cleanup_dir_list = dir;
1182}
#define pg_malloc_object(type)
Definition fe_memutils.h:60

References cleanup_dir_list, cb_cleanup_dir::next, pg_malloc_object, cb_cleanup_dir::rmtopdir, and cb_cleanup_dir::target_path.

Referenced by create_output_directory().

◆ reset_directory_cleanup_list()

static void reset_directory_cleanup_list ( void  )
static

Definition at line 1195 of file pg_combinebackup.c.

1196{
1197 while (cleanup_dir_list != NULL)
1198 {
1200
1202 pfree(dir);
1203 }
1204}

References cleanup_dir_list, fb(), cb_cleanup_dir::next, and pfree().

Referenced by main().

◆ scan_for_existing_tablespaces()

static cb_tablespace * scan_for_existing_tablespaces ( char pathname,
cb_options opt 
)
static

Definition at line 1214 of file pg_combinebackup.c.

1215{
1216 char pg_tblspc[MAXPGPATH];
1217 DIR *dir;
1218 struct dirent *de;
1220
1221 snprintf(pg_tblspc, MAXPGPATH, "%s/%s", pathname, PG_TBLSPC_DIR);
1222 pg_log_debug("scanning \"%s\"", pg_tblspc);
1223
1224 if ((dir = opendir(pg_tblspc)) == NULL)
1225 pg_fatal("could not open directory \"%s\": %m", pg_tblspc);
1226
1227 while (errno = 0, (de = readdir(dir)) != NULL)
1228 {
1229 Oid oid;
1230 char tblspcdir[MAXPGPATH];
1231 char link_target[MAXPGPATH];
1232 cb_tablespace *ts;
1235
1236 /* Silently ignore "." and ".." entries. */
1237 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0)
1238 continue;
1239
1240 /* Construct full pathname. */
1241 snprintf(tblspcdir, MAXPGPATH, "%s/%s", pg_tblspc, de->d_name);
1242
1243 /* Ignore any file name that doesn't look like a proper OID. */
1244 if (!parse_oid(de->d_name, &oid))
1245 {
1246 pg_log_debug("skipping \"%s\" because the filename is not a legal tablespace OID",
1247 tblspcdir);
1248 continue;
1249 }
1250
1251 /* Only symbolic links and directories are tablespaces. */
1253 if (type == PGFILETYPE_ERROR)
1254 exit(1);
1256 {
1257 pg_log_debug("skipping \"%s\" because it is neither a symbolic link nor a directory",
1258 tblspcdir);
1259 continue;
1260 }
1261
1262 /* Create a new tablespace object. */
1264 ts->oid = oid;
1265
1266 /*
1267 * If it's a link, it's not an in-place tablespace. Otherwise, it must
1268 * be a directory, and thus an in-place tablespace.
1269 */
1270 if (type == PGFILETYPE_LNK)
1271 {
1274
1275 /* Read the link target. */
1277 if (link_length < 0)
1278 pg_fatal("could not read symbolic link \"%s\": %m",
1279 tblspcdir);
1280 if (link_length >= sizeof(link_target))
1281 pg_fatal("target of symbolic link \"%s\" is too long", tblspcdir);
1282 link_target[link_length] = '\0';
1284 pg_fatal("target of symbolic link \"%s\" is relative", tblspcdir);
1285
1286 /* Canonicalize the link target. */
1288
1289 /*
1290 * Find the corresponding tablespace mapping and copy the relevant
1291 * details into the new tablespace entry.
1292 */
1293 for (tsmap = opt->tsmappings; tsmap != NULL; tsmap = tsmap->next)
1294 {
1295 if (strcmp(tsmap->old_dir, link_target) == 0)
1296 {
1297 strlcpy(ts->old_dir, tsmap->old_dir, MAXPGPATH);
1298 strlcpy(ts->new_dir, tsmap->new_dir, MAXPGPATH);
1299 ts->in_place = false;
1300 break;
1301 }
1302 }
1303
1304 /* Every non-in-place tablespace must be mapped. */
1305 if (tsmap == NULL)
1306 pg_fatal("tablespace at \"%s\" has no tablespace mapping",
1307 link_target);
1308 }
1309 else
1310 {
1311 /*
1312 * For an in-place tablespace, there's no separate directory, so
1313 * we just record the paths within the data directories.
1314 */
1315 snprintf(ts->old_dir, MAXPGPATH, "%s/%s", pg_tblspc, de->d_name);
1316 snprintf(ts->new_dir, MAXPGPATH, "%s/%s/%s", opt->output,
1317 PG_TBLSPC_DIR, de->d_name);
1318 ts->in_place = true;
1319 }
1320
1321 /* Tablespaces should not share a directory. */
1322 for (otherts = tslist; otherts != NULL; otherts = otherts->next)
1323 if (strcmp(ts->new_dir, otherts->new_dir) == 0)
1324 pg_fatal("tablespaces with OIDs %u and %u both point at directory \"%s\"",
1325 otherts->oid, oid, ts->new_dir);
1326
1327 /* Add this tablespace to the list. */
1328 ts->next = tslist;
1329 tslist = ts;
1330 }
1331
1332 if (closedir(dir) != 0)
1333 pg_fatal("could not close directory \"%s\": %m", pg_tblspc);
1334
1335 return tslist;
1336}
#define readlink(path, buf, size)
Definition win32_port.h:243

References canonicalize_path(), closedir(), fb(), get_dirent_type(), cb_tablespace::in_place, is_absolute_path, MAXPGPATH, cb_tablespace::new_dir, cb_tablespace::next, cb_tablespace::oid, cb_tablespace::old_dir, opendir(), cb_options::output, parse_oid(), pg_fatal, pg_log_debug, PG_LOG_ERROR, pg_malloc0_object, PG_TBLSPC_DIR, PGFILETYPE_DIR, PGFILETYPE_ERROR, PGFILETYPE_LNK, readdir(), readlink, snprintf, strlcpy(), cb_options::tsmappings, and type.

Referenced by main().

◆ slurp_file()

static void slurp_file ( int  fd,
char filename,
StringInfo  buf,
int  maxlen 
)
static

Definition at line 1345 of file pg_combinebackup.c.

1346{
1347 struct stat st;
1348 ssize_t rb;
1349 size_t len;
1350
1351 /* Check file size, and complain if it's too large. */
1352 if (fstat(fd, &st) != 0)
1353 pg_fatal("could not stat file \"%s\": %m", filename);
1354 if (st.st_size > maxlen)
1355 pg_fatal("file \"%s\" is too large", filename);
1356
1357 len = st.st_size;
1358
1359 /* Make sure we have enough space. */
1361
1362 /* Read the data. */
1363 rb = read(fd, &buf->data[buf->len], len);
1364
1365 /*
1366 * We don't expect any concurrent changes, so we should read exactly the
1367 * expected number of bytes.
1368 */
1369 if (rb != len)
1370 {
1371 if (rb < 0)
1372 pg_fatal("could not read file \"%s\": %m", filename);
1373 else
1374 pg_fatal("could not read file \"%s\": read %zd of %zu",
1375 filename, rb, len);
1376 }
1377
1378 /* Adjust buffer length for new data and restore trailing-\0 invariant */
1379 buf->len += rb;
1380 buf->data[buf->len] = '\0';
1381}
#define read(a, b, c)
Definition win32.h:13
const void size_t len
static char * filename
Definition pg_dumpall.c:120
void enlargeStringInfo(StringInfo str, int needed)
Definition stringinfo.c:337
#define fstat
Definition win32_port.h:73

References buf, enlargeStringInfo(), fb(), fd(), filename, fstat, len, pg_fatal, read, and stat::st_size.

Referenced by check_backup_label_files().

Variable Documentation

◆ cleanup_dir_list