LCOV - differential code coverage report
Current view: top level - src/bin/pg_combinebackup - pg_combinebackup.c (source / functions) Coverage Total Hit UBC GNC CBC DCB
Current: c70b6db34ffeab48beef1fb4ce61bcad3772b8dd vs 06473f5a344df8c9594ead90a609b86f6724cff8 Lines: 79.0 % 477 377 100 1 376 1
Current Date: 2025-09-06 07:49:51 +0900 Functions: 100.0 % 15 15 1 14
Baseline: lcov-20250906-005545-baseline Branches: 62.0 % 358 222 136 222
Baseline Date: 2025-09-05 08:21:35 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 100.0 % 14 14 1 13
(360..) days: 78.4 % 463 363 100 363
Function coverage date bins:
(360..) days: 100.0 % 15 15 1 14
Branch coverage date bins:
(30,360] days: 100.0 % 4 4 4
(360..) days: 61.6 % 354 218 136 218

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * pg_combinebackup.c
                                  4                 :                :  *      Combine incremental backups with prior backups.
                                  5                 :                :  *
                                  6                 :                :  * Copyright (c) 2017-2025, PostgreSQL Global Development Group
                                  7                 :                :  *
                                  8                 :                :  * IDENTIFICATION
                                  9                 :                :  *    src/bin/pg_combinebackup/pg_combinebackup.c
                                 10                 :                :  *
                                 11                 :                :  *-------------------------------------------------------------------------
                                 12                 :                :  */
                                 13                 :                : #include "postgres_fe.h"
                                 14                 :                : 
                                 15                 :                : #include <dirent.h>
                                 16                 :                : #include <fcntl.h>
                                 17                 :                : #include <limits.h>
                                 18                 :                : 
                                 19                 :                : #ifdef HAVE_COPYFILE_H
                                 20                 :                : #include <copyfile.h>
                                 21                 :                : #endif
                                 22                 :                : #ifdef __linux__
                                 23                 :                : #include <sys/ioctl.h>
                                 24                 :                : #include <linux/fs.h>
                                 25                 :                : #endif
                                 26                 :                : 
                                 27                 :                : #include "access/xlog_internal.h"
                                 28                 :                : #include "backup_label.h"
                                 29                 :                : #include "common/checksum_helper.h"
                                 30                 :                : #include "common/controldata_utils.h"
                                 31                 :                : #include "common/file_perm.h"
                                 32                 :                : #include "common/file_utils.h"
                                 33                 :                : #include "common/logging.h"
                                 34                 :                : #include "common/relpath.h"
                                 35                 :                : #include "copy_file.h"
                                 36                 :                : #include "fe_utils/option_utils.h"
                                 37                 :                : #include "getopt_long.h"
                                 38                 :                : #include "lib/stringinfo.h"
                                 39                 :                : #include "load_manifest.h"
                                 40                 :                : #include "reconstruct.h"
                                 41                 :                : #include "write_manifest.h"
                                 42                 :                : 
                                 43                 :                : /* Incremental file naming convention. */
                                 44                 :                : #define INCREMENTAL_PREFIX          "INCREMENTAL."
                                 45                 :                : #define INCREMENTAL_PREFIX_LENGTH   (sizeof(INCREMENTAL_PREFIX) - 1)
                                 46                 :                : 
                                 47                 :                : /*
                                 48                 :                :  * Tracking for directories that need to be removed, or have their contents
                                 49                 :                :  * removed, if the operation fails.
                                 50                 :                :  */
                                 51                 :                : typedef struct cb_cleanup_dir
                                 52                 :                : {
                                 53                 :                :     char       *target_path;
                                 54                 :                :     bool        rmtopdir;
                                 55                 :                :     struct cb_cleanup_dir *next;
                                 56                 :                : } cb_cleanup_dir;
                                 57                 :                : 
                                 58                 :                : /*
                                 59                 :                :  * Stores a tablespace mapping provided using -T, --tablespace-mapping.
                                 60                 :                :  */
                                 61                 :                : typedef struct cb_tablespace_mapping
                                 62                 :                : {
                                 63                 :                :     char        old_dir[MAXPGPATH];
                                 64                 :                :     char        new_dir[MAXPGPATH];
                                 65                 :                :     struct cb_tablespace_mapping *next;
                                 66                 :                : } cb_tablespace_mapping;
                                 67                 :                : 
                                 68                 :                : /*
                                 69                 :                :  * Stores data parsed from all command-line options.
                                 70                 :                :  */
                                 71                 :                : typedef struct cb_options
                                 72                 :                : {
                                 73                 :                :     bool        debug;
                                 74                 :                :     char       *output;
                                 75                 :                :     bool        dry_run;
                                 76                 :                :     bool        no_sync;
                                 77                 :                :     cb_tablespace_mapping *tsmappings;
                                 78                 :                :     pg_checksum_type manifest_checksums;
                                 79                 :                :     bool        no_manifest;
                                 80                 :                :     DataDirSyncMethod sync_method;
                                 81                 :                :     CopyMethod  copy_method;
                                 82                 :                : } cb_options;
                                 83                 :                : 
                                 84                 :                : /*
                                 85                 :                :  * Data about a tablespace.
                                 86                 :                :  *
                                 87                 :                :  * Every normal tablespace needs a tablespace mapping, but in-place tablespaces
                                 88                 :                :  * don't, so the list of tablespaces can contain more entries than the list of
                                 89                 :                :  * tablespace mappings.
                                 90                 :                :  */
                                 91                 :                : typedef struct cb_tablespace
                                 92                 :                : {
                                 93                 :                :     Oid         oid;
                                 94                 :                :     bool        in_place;
                                 95                 :                :     char        old_dir[MAXPGPATH];
                                 96                 :                :     char        new_dir[MAXPGPATH];
                                 97                 :                :     struct cb_tablespace *next;
                                 98                 :                : } cb_tablespace;
                                 99                 :                : 
                                100                 :                : /* Directories to be removed if we exit uncleanly. */
                                101                 :                : static cb_cleanup_dir *cleanup_dir_list = NULL;
                                102                 :                : 
                                103                 :                : static void add_tablespace_mapping(cb_options *opt, char *arg);
                                104                 :                : static StringInfo check_backup_label_files(int n_backups, char **backup_dirs);
                                105                 :                : static uint64 check_control_files(int n_backups, char **backup_dirs);
                                106                 :                : static void check_input_dir_permissions(char *dir);
                                107                 :                : static void cleanup_directories_atexit(void);
                                108                 :                : static void create_output_directory(char *dirname, cb_options *opt);
                                109                 :                : static void help(const char *progname);
                                110                 :                : static bool parse_oid(char *s, Oid *result);
                                111                 :                : static void process_directory_recursively(Oid tsoid,
                                112                 :                :                                           char *input_directory,
                                113                 :                :                                           char *output_directory,
                                114                 :                :                                           char *relative_path,
                                115                 :                :                                           int n_prior_backups,
                                116                 :                :                                           char **prior_backup_dirs,
                                117                 :                :                                           manifest_data **manifests,
                                118                 :                :                                           manifest_writer *mwriter,
                                119                 :                :                                           cb_options *opt);
                                120                 :                : static int  read_pg_version_file(char *directory);
                                121                 :                : static void remember_to_cleanup_directory(char *target_path, bool rmtopdir);
                                122                 :                : static void reset_directory_cleanup_list(void);
                                123                 :                : static cb_tablespace *scan_for_existing_tablespaces(char *pathname,
                                124                 :                :                                                     cb_options *opt);
                                125                 :                : static void slurp_file(int fd, char *filename, StringInfo buf, int maxlen);
                                126                 :                : 
                                127                 :                : /*
                                128                 :                :  * Main program.
                                129                 :                :  */
                                130                 :                : int
  626 rhaas@postgresql.org      131                 :CBC          25 : main(int argc, char *argv[])
                                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                 :                :     int         version;
                                157                 :                :     uint64      system_identifier;
                                158                 :                :     char      **prior_backup_dirs;
                                159                 :                :     cb_options  opt;
                                160                 :                :     cb_tablespace *tablespaces;
                                161                 :                :     cb_tablespace *ts;
                                162                 :                :     StringInfo  last_backup_label;
                                163                 :                :     manifest_data **manifests;
                                164                 :                :     manifest_writer *mwriter;
                                165                 :                : 
                                166                 :             25 :     pg_logging_init(argv[0]);
                                167                 :             25 :     progname = get_progname(argv[0]);
  515 michael@paquier.xyz       168                 :             25 :     set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_combinebackup"));
  626 rhaas@postgresql.org      169                 :             25 :     handle_help_version_opts(argc, argv, progname, help);
                                170                 :                : 
                                171                 :             23 :     memset(&opt, 0, sizeof(opt));
                                172                 :             23 :     opt.manifest_checksums = CHECKSUM_TYPE_CRC32C;
                                173                 :             23 :     opt.sync_method = DATA_DIR_SYNC_METHOD_FSYNC;
  519 tomas.vondra@postgre      174                 :             23 :     opt.copy_method = COPY_METHOD_COPY;
                                175                 :                : 
                                176                 :                :     /* process command-line options */
  173 rhaas@postgresql.org      177                 :             72 :     while ((c = getopt_long(argc, argv, "dknNo:T:",
  626                           178         [ +  + ]:             72 :                             long_options, &optindex)) != -1)
                                179                 :                :     {
                                180   [ +  +  -  +  :             50 :         switch (c)
                                     +  +  +  +  -  
                                        -  +  -  + ]
                                181                 :                :         {
                                182                 :              6 :             case 'd':
                                183                 :              6 :                 opt.debug = true;
                                184                 :              6 :                 pg_logging_increase_verbosity();
                                185                 :              6 :                 break;
  173                           186                 :              1 :             case 'k':
                                187                 :              1 :                 opt.copy_method = COPY_METHOD_LINK;
                                188                 :              1 :                 break;
  626 rhaas@postgresql.org      189                 :UBC           0 :             case 'n':
                                190                 :              0 :                 opt.dry_run = true;
                                191                 :              0 :                 break;
  626 rhaas@postgresql.org      192                 :CBC           3 :             case 'N':
                                193                 :              3 :                 opt.no_sync = true;
                                194                 :              3 :                 break;
                                195                 :             20 :             case 'o':
                                196                 :             20 :                 opt.output = optarg;
                                197                 :             20 :                 break;
                                198                 :              1 :             case 'T':
                                199                 :              1 :                 add_tablespace_mapping(&opt, optarg);
                                200                 :              1 :                 break;
                                201                 :              2 :             case 1:
                                202         [ -  + ]:              2 :                 if (!pg_checksum_parse_type(optarg,
                                203                 :                :                                             &opt.manifest_checksums))
  626 rhaas@postgresql.org      204                 :UBC           0 :                     pg_fatal("unrecognized checksum algorithm: \"%s\"",
                                205                 :                :                              optarg);
  626 rhaas@postgresql.org      206                 :CBC           2 :                 break;
                                207                 :              1 :             case 2:
                                208                 :              1 :                 opt.no_manifest = true;
                                209                 :              1 :                 break;
  626 rhaas@postgresql.org      210                 :UBC           0 :             case 3:
                                211         [ #  # ]:              0 :                 if (!parse_sync_method(optarg, &opt.sync_method))
                                212                 :              0 :                     exit(1);
                                213                 :              0 :                 break;
  519 tomas.vondra@postgre      214                 :              0 :             case 4:
                                215                 :              0 :                 opt.copy_method = COPY_METHOD_CLONE;
                                216                 :              0 :                 break;
  519 tomas.vondra@postgre      217                 :CBC          15 :             case 5:
  433                           218                 :             15 :                 opt.copy_method = COPY_METHOD_COPY;
                                219                 :             15 :                 break;
  433 tomas.vondra@postgre      220                 :UBC           0 :             case 6:
  519                           221                 :              0 :                 opt.copy_method = COPY_METHOD_COPY_FILE_RANGE;
                                222                 :              0 :                 break;
  626 rhaas@postgresql.org      223                 :CBC           1 :             default:
                                224                 :                :                 /* getopt_long already emitted a complaint */
                                225                 :              1 :                 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
                                226                 :              1 :                 exit(1);
                                227                 :                :         }
                                228                 :                :     }
                                229                 :                : 
                                230         [ +  + ]:             22 :     if (optind >= argc)
                                231                 :                :     {
  443 peter@eisentraut.org      232                 :              1 :         pg_log_error("no input directories specified");
  626 rhaas@postgresql.org      233                 :              1 :         pg_log_error_hint("Try \"%s --help\" for more information.", progname);
                                234                 :              1 :         exit(1);
                                235                 :                :     }
                                236                 :                : 
                                237         [ +  + ]:             21 :     if (opt.output == NULL)
                                238                 :              1 :         pg_fatal("no output directory specified");
                                239                 :                : 
                                240                 :                :     /* If no manifest is needed, no checksums are needed, either. */
                                241         [ +  + ]:             20 :     if (opt.no_manifest)
                                242                 :              1 :         opt.manifest_checksums = CHECKSUM_TYPE_NONE;
                                243                 :                : 
                                244                 :                :     /* Check that the platform supports the requested copy method. */
  519 tomas.vondra@postgre      245         [ -  + ]:             20 :     if (opt.copy_method == COPY_METHOD_CLONE)
                                246                 :                :     {
                                247                 :                : #if (defined(HAVE_COPYFILE) && defined(COPYFILE_CLONE_FORCE)) || \
                                248                 :                :     (defined(__linux__) && defined(FICLONE))
                                249                 :                : 
  519 tomas.vondra@postgre      250         [ #  # ]:UBC           0 :         if (opt.dry_run)
                                251         [ #  # ]:              0 :             pg_log_debug("would use cloning to copy files");
                                252                 :                :         else
                                253         [ #  # ]:              0 :             pg_log_debug("will use cloning to copy files");
                                254                 :                : 
                                255                 :                : #else
                                256                 :                :         pg_fatal("file cloning not supported on this platform");
                                257                 :                : #endif
                                258                 :                :     }
  519 tomas.vondra@postgre      259         [ -  + ]:CBC          20 :     else if (opt.copy_method == COPY_METHOD_COPY_FILE_RANGE)
                                260                 :                :     {
                                261                 :                : #if defined(HAVE_COPY_FILE_RANGE)
                                262                 :                : 
  519 tomas.vondra@postgre      263         [ #  # ]:UBC           0 :         if (opt.dry_run)
                                264         [ #  # ]:              0 :             pg_log_debug("would use copy_file_range to copy blocks");
                                265                 :                :         else
                                266         [ #  # ]:              0 :             pg_log_debug("will use copy_file_range to copy blocks");
                                267                 :                : 
                                268                 :                : #else
                                269                 :                :         pg_fatal("copy_file_range not supported on this platform");
                                270                 :                : #endif
                                271                 :                :     }
                                272                 :                : 
                                273                 :                :     /* Read the server version from the final backup. */
  626 rhaas@postgresql.org      274                 :CBC          20 :     version = read_pg_version_file(argv[argc - 1]);
                                275                 :                : 
                                276                 :                :     /* Sanity-check control files. */
                                277                 :             20 :     n_backups = argc - optind;
  542                           278                 :             20 :     system_identifier = check_control_files(n_backups, argv + optind);
                                279                 :                : 
                                280                 :                :     /* Sanity-check backup_label files, and get the contents of the last one. */
  626                           281                 :             19 :     last_backup_label = check_backup_label_files(n_backups, argv + optind);
                                282                 :                : 
                                283                 :                :     /*
                                284                 :                :      * We'll need the pathnames to the prior backups. By "prior" we mean all
                                285                 :                :      * but the last one listed on the command line.
                                286                 :                :      */
                                287                 :             14 :     n_prior_backups = argc - optind - 1;
                                288                 :             14 :     prior_backup_dirs = argv + optind;
                                289                 :                : 
                                290                 :                :     /* Load backup manifests. */
                                291                 :             14 :     manifests = load_backup_manifests(n_backups, prior_backup_dirs);
                                292                 :                : 
                                293                 :                :     /*
                                294                 :                :      * Validate the manifest system identifier against the backup system
                                295                 :                :      * identifier.
                                296                 :                :      */
  542                           297         [ +  + ]:             40 :     for (i = 0; i < n_backups; i++)
                                298                 :                :     {
                                299         [ +  - ]:             27 :         if (manifests[i] &&
                                300         [ +  + ]:             27 :             manifests[i]->system_identifier != system_identifier)
                                301                 :                :         {
                                302                 :                :             char       *controlpath;
                                303                 :                : 
  152 fujii@postgresql.org      304                 :              1 :             controlpath = psprintf("%s/%s", prior_backup_dirs[i], XLOG_CONTROL_FILE);
                                305                 :                : 
  161 peter@eisentraut.org      306                 :              1 :             pg_fatal("%s: manifest system identifier is %" PRIu64 ", but control file has %" PRIu64,
                                307                 :                :                      controlpath,
                                308                 :                :                      manifests[i]->system_identifier,
                                309                 :                :                      system_identifier);
                                310                 :                :         }
                                311                 :                :     }
                                312                 :                : 
                                313                 :                :     /* Figure out which tablespaces are going to be included in the output. */
  626 rhaas@postgresql.org      314                 :             13 :     last_input_dir = argv[argc - 1];
                                315                 :             13 :     check_input_dir_permissions(last_input_dir);
                                316                 :             13 :     tablespaces = scan_for_existing_tablespaces(last_input_dir, &opt);
                                317                 :                : 
                                318                 :                :     /*
                                319                 :                :      * Create output directories.
                                320                 :                :      *
                                321                 :                :      * We create one output directory for the main data directory plus one for
                                322                 :                :      * each non-in-place tablespace. create_output_directory() will arrange
                                323                 :                :      * for those directories to be cleaned up on failure. In-place tablespaces
                                324                 :                :      * aren't handled at this stage because they're located beneath the main
                                325                 :                :      * output directory, and thus the cleanup of that directory will get rid
                                326                 :                :      * of them. Plus, the pg_tblspc directory that needs to contain them
                                327                 :                :      * doesn't exist yet.
                                328                 :                :      */
                                329                 :             13 :     atexit(cleanup_directories_atexit);
                                330                 :             13 :     create_output_directory(opt.output, &opt);
                                331         [ +  + ]:             14 :     for (ts = tablespaces; ts != NULL; ts = ts->next)
                                332         [ +  - ]:              1 :         if (!ts->in_place)
                                333                 :              1 :             create_output_directory(ts->new_dir, &opt);
                                334                 :                : 
                                335                 :                :     /* If we need to write a backup_manifest, prepare to do so. */
                                336   [ +  -  +  + ]:             13 :     if (!opt.dry_run && !opt.no_manifest)
                                337                 :                :     {
  542                           338                 :             12 :         mwriter = create_manifest_writer(opt.output, system_identifier);
                                339                 :                : 
                                340                 :                :         /*
                                341                 :                :          * Verify that we have a backup manifest for the final backup; else we
                                342                 :                :          * won't have the WAL ranges for the resulting manifest.
                                343                 :                :          */
  626                           344         [ -  + ]:             12 :         if (manifests[n_prior_backups] == NULL)
  375 peter@eisentraut.org      345                 :UBC           0 :             pg_fatal("cannot generate a manifest because no manifest is available for the final input backup");
                                346                 :                :     }
                                347                 :                :     else
  626 rhaas@postgresql.org      348                 :CBC           1 :         mwriter = NULL;
                                349                 :                : 
                                350                 :                :     /* Write backup label into output directory. */
                                351         [ -  + ]:             13 :     if (opt.dry_run)
  626 rhaas@postgresql.org      352         [ #  # ]:UBC           0 :         pg_log_debug("would generate \"%s/backup_label\"", opt.output);
                                353                 :                :     else
                                354                 :                :     {
  626 rhaas@postgresql.org      355         [ +  + ]:CBC          13 :         pg_log_debug("generating \"%s/backup_label\"", opt.output);
                                356                 :             13 :         last_backup_label->cursor = 0;
                                357                 :             13 :         write_backup_label(opt.output, last_backup_label,
                                358                 :                :                            opt.manifest_checksums, mwriter);
                                359                 :                :     }
                                360                 :                : 
                                361                 :                :     /* Process everything that's not part of a user-defined tablespace. */
                                362         [ +  + ]:             13 :     pg_log_debug("processing backup directory \"%s\"", last_input_dir);
                                363                 :             13 :     process_directory_recursively(InvalidOid, last_input_dir, opt.output,
                                364                 :                :                                   NULL, n_prior_backups, prior_backup_dirs,
                                365                 :                :                                   manifests, mwriter, &opt);
                                366                 :                : 
                                367                 :                :     /* Process user-defined tablespaces. */
                                368         [ +  + ]:             13 :     for (ts = tablespaces; ts != NULL; ts = ts->next)
                                369                 :                :     {
                                370         [ +  - ]:              1 :         pg_log_debug("processing tablespace directory \"%s\"", ts->old_dir);
                                371                 :                : 
                                372                 :                :         /*
                                373                 :                :          * If it's a normal tablespace, we need to set up a symbolic link from
                                374                 :                :          * pg_tblspc/${OID} to the target directory; if it's an in-place
                                375                 :                :          * tablespace, we need to create a directory at pg_tblspc/${OID}.
                                376                 :                :          */
                                377         [ +  - ]:              1 :         if (!ts->in_place)
                                378                 :                :         {
                                379                 :                :             char        linkpath[MAXPGPATH];
                                380                 :                : 
  368 michael@paquier.xyz       381                 :              1 :             snprintf(linkpath, MAXPGPATH, "%s/%s/%u", opt.output, PG_TBLSPC_DIR,
                                382                 :                :                      ts->oid);
                                383                 :                : 
  626 rhaas@postgresql.org      384         [ -  + ]:              1 :             if (opt.dry_run)
  626 rhaas@postgresql.org      385         [ #  # ]:UBC           0 :                 pg_log_debug("would create symbolic link from \"%s\" to \"%s\"",
                                386                 :                :                              linkpath, ts->new_dir);
                                387                 :                :             else
                                388                 :                :             {
  626 rhaas@postgresql.org      389         [ +  - ]:CBC           1 :                 pg_log_debug("creating symbolic link from \"%s\" to \"%s\"",
                                390                 :                :                              linkpath, ts->new_dir);
                                391         [ -  + ]:              1 :                 if (symlink(ts->new_dir, linkpath) != 0)
  626 rhaas@postgresql.org      392                 :UBC           0 :                     pg_fatal("could not create symbolic link from \"%s\" to \"%s\": %m",
                                393                 :                :                              linkpath, ts->new_dir);
                                394                 :                :             }
                                395                 :                :         }
                                396                 :                :         else
                                397                 :                :         {
                                398         [ #  # ]:              0 :             if (opt.dry_run)
                                399         [ #  # ]:              0 :                 pg_log_debug("would create directory \"%s\"", ts->new_dir);
                                400                 :                :             else
                                401                 :                :             {
                                402         [ #  # ]:              0 :                 pg_log_debug("creating directory \"%s\"", ts->new_dir);
                                403         [ #  # ]:              0 :                 if (pg_mkdir_p(ts->new_dir, pg_dir_create_mode) == -1)
                                404                 :              0 :                     pg_fatal("could not create directory \"%s\": %m",
                                405                 :                :                              ts->new_dir);
                                406                 :                :             }
                                407                 :                :         }
                                408                 :                : 
                                409                 :                :         /* OK, now handle the directory contents. */
  626 rhaas@postgresql.org      410                 :CBC           1 :         process_directory_recursively(ts->oid, ts->old_dir, ts->new_dir,
                                411                 :                :                                       NULL, n_prior_backups, prior_backup_dirs,
                                412                 :                :                                       manifests, mwriter, &opt);
                                413                 :                :     }
                                414                 :                : 
                                415                 :                :     /* Finalize the backup_manifest, if we're generating one. */
                                416         [ +  + ]:             12 :     if (mwriter != NULL)
                                417                 :             11 :         finalize_manifest(mwriter,
                                418                 :             11 :                           manifests[n_prior_backups]->first_wal_range);
                                419                 :                : 
                                420                 :                :     /* fsync that output directory unless we've been told not to do so */
                                421         [ +  + ]:             12 :     if (!opt.no_sync)
                                422                 :                :     {
                                423         [ -  + ]:              9 :         if (opt.dry_run)
  626 rhaas@postgresql.org      424         [ #  # ]:UBC           0 :             pg_log_debug("would recursively fsync \"%s\"", opt.output);
                                425                 :                :         else
                                426                 :                :         {
  626 rhaas@postgresql.org      427         [ +  + ]:CBC           9 :             pg_log_debug("recursively fsyncing \"%s\"", opt.output);
  165 nathan@postgresql.or      428                 :              9 :             sync_pgdata(opt.output, version * 10000, opt.sync_method, true);
                                429                 :                :         }
                                430                 :                :     }
                                431                 :                : 
                                432                 :                :     /* Warn about the possibility of compromising the backups, when link mode */
  173 rhaas@postgresql.org      433         [ +  + ]:             12 :     if (opt.copy_method == COPY_METHOD_LINK)
                                434                 :              1 :         pg_log_warning("--link mode was used; any modifications to the output "
                                435                 :                :                        "directory might destructively modify input directories");
                                436                 :                : 
                                437                 :                :     /* It's a success, so don't remove the output directories. */
  626                           438                 :             12 :     reset_directory_cleanup_list();
                                439                 :             12 :     exit(0);
                                440                 :                : }
                                441                 :                : 
                                442                 :                : /*
                                443                 :                :  * Process the option argument for the -T, --tablespace-mapping switch.
                                444                 :                :  */
                                445                 :                : static void
                                446                 :              1 : add_tablespace_mapping(cb_options *opt, char *arg)
                                447                 :                : {
                                448                 :              1 :     cb_tablespace_mapping *tsmap = pg_malloc0(sizeof(cb_tablespace_mapping));
                                449                 :                :     char       *dst;
                                450                 :                :     char       *dst_ptr;
                                451                 :                :     char       *arg_ptr;
                                452                 :                : 
                                453                 :                :     /*
                                454                 :                :      * Basically, we just want to copy everything before the equals sign to
                                455                 :                :      * tsmap->old_dir and everything afterwards to tsmap->new_dir, but if
                                456                 :                :      * there's more or less than one equals sign, that's an error, and if
                                457                 :                :      * there's an equals sign preceded by a backslash, don't treat it as a
                                458                 :                :      * field separator but instead copy a literal equals sign.
                                459                 :                :      */
                                460                 :              1 :     dst_ptr = dst = tsmap->old_dir;
                                461         [ +  + ]:             50 :     for (arg_ptr = arg; *arg_ptr != '\0'; arg_ptr++)
                                462                 :                :     {
                                463         [ -  + ]:             49 :         if (dst_ptr - dst >= MAXPGPATH)
  626 rhaas@postgresql.org      464                 :UBC           0 :             pg_fatal("directory name too long");
                                465                 :                : 
  626 rhaas@postgresql.org      466   [ -  +  -  - ]:CBC          49 :         if (*arg_ptr == '\\' && *(arg_ptr + 1) == '=')
                                467                 :                :             ;                   /* skip backslash escaping = */
                                468   [ +  +  +  -  :             49 :         else if (*arg_ptr == '=' && (arg_ptr == arg || *(arg_ptr - 1) != '\\'))
                                              +  - ]
                                469                 :                :         {
                                470         [ -  + ]:              1 :             if (tsmap->new_dir[0] != '\0')
  626 rhaas@postgresql.org      471                 :UBC           0 :                 pg_fatal("multiple \"=\" signs in tablespace mapping");
                                472                 :                :             else
  626 rhaas@postgresql.org      473                 :CBC           1 :                 dst = dst_ptr = tsmap->new_dir;
                                474                 :                :         }
                                475                 :                :         else
                                476                 :             48 :             *dst_ptr++ = *arg_ptr;
                                477                 :                :     }
                                478   [ +  -  -  + ]:              1 :     if (!tsmap->old_dir[0] || !tsmap->new_dir[0])
  626 rhaas@postgresql.org      479                 :UBC           0 :         pg_fatal("invalid tablespace mapping format \"%s\", must be \"OLDDIR=NEWDIR\"", arg);
                                480                 :                : 
                                481                 :                :     /*
                                482                 :                :      * All tablespaces are created with absolute directories, so specifying a
                                483                 :                :      * non-absolute path here would never match, possibly confusing users.
                                484                 :                :      *
                                485                 :                :      * In contrast to pg_basebackup, both the old and new directories are on
                                486                 :                :      * the local machine, so the local machine's definition of an absolute
                                487                 :                :      * path is the only relevant one.
                                488                 :                :      */
  626 rhaas@postgresql.org      489         [ -  + ]:CBC           1 :     if (!is_absolute_path(tsmap->old_dir))
  626 rhaas@postgresql.org      490                 :UBC           0 :         pg_fatal("old directory is not an absolute path in tablespace mapping: %s",
                                491                 :                :                  tsmap->old_dir);
                                492                 :                : 
  626 rhaas@postgresql.org      493         [ -  + ]:CBC           1 :     if (!is_absolute_path(tsmap->new_dir))
  626 rhaas@postgresql.org      494                 :UBC           0 :         pg_fatal("old directory is not an absolute path in tablespace mapping: %s",
                                495                 :                :                  tsmap->new_dir);
                                496                 :                : 
                                497                 :                :     /* Canonicalize paths to avoid spurious failures when comparing. */
  626 rhaas@postgresql.org      498                 :CBC           1 :     canonicalize_path(tsmap->old_dir);
                                499                 :              1 :     canonicalize_path(tsmap->new_dir);
                                500                 :                : 
                                501                 :                :     /* Add it to the list. */
                                502                 :              1 :     tsmap->next = opt->tsmappings;
                                503                 :              1 :     opt->tsmappings = tsmap;
                                504                 :              1 : }
                                505                 :                : 
                                506                 :                : /*
                                507                 :                :  * Check that the backup_label files form a coherent backup chain, and return
                                508                 :                :  * the contents of the backup_label file from the latest backup.
                                509                 :                :  */
                                510                 :                : static StringInfo
                                511                 :             19 : check_backup_label_files(int n_backups, char **backup_dirs)
                                512                 :                : {
                                513                 :             19 :     StringInfo  buf = makeStringInfo();
                                514                 :             19 :     StringInfo  lastbuf = buf;
                                515                 :                :     int         i;
                                516                 :             19 :     TimeLineID  check_tli = 0;
                                517                 :             19 :     XLogRecPtr  check_lsn = InvalidXLogRecPtr;
                                518                 :                : 
                                519                 :                :     /* Try to read each backup_label file in turn, last to first. */
                                520         [ +  + ]:             51 :     for (i = n_backups - 1; i >= 0; --i)
                                521                 :                :     {
                                522                 :                :         char        pathbuf[MAXPGPATH];
                                523                 :                :         int         fd;
                                524                 :                :         TimeLineID  start_tli;
                                525                 :                :         TimeLineID  previous_tli;
                                526                 :                :         XLogRecPtr  start_lsn;
                                527                 :                :         XLogRecPtr  previous_lsn;
                                528                 :                : 
                                529                 :                :         /* Open the backup_label file. */
                                530                 :             37 :         snprintf(pathbuf, MAXPGPATH, "%s/backup_label", backup_dirs[i]);
                                531         [ +  + ]:             37 :         pg_log_debug("reading \"%s\"", pathbuf);
                                532         [ -  + ]:             37 :         if ((fd = open(pathbuf, O_RDONLY, 0)) < 0)
  626 rhaas@postgresql.org      533                 :UBC           0 :             pg_fatal("could not open file \"%s\": %m", pathbuf);
                                534                 :                : 
                                535                 :                :         /*
                                536                 :                :          * Slurp the whole file into memory.
                                537                 :                :          *
                                538                 :                :          * The exact size limit that we impose here doesn't really matter --
                                539                 :                :          * most of what's supposed to be in the file is fixed size and quite
                                540                 :                :          * short. However, the length of the backup_label is limited (at least
                                541                 :                :          * by some parts of the code) to MAXPGPATH, so include that value in
                                542                 :                :          * the maximum length that we tolerate.
                                543                 :                :          */
  626 rhaas@postgresql.org      544                 :CBC          37 :         slurp_file(fd, pathbuf, buf, 10000 + MAXPGPATH);
                                545                 :                : 
                                546                 :                :         /* Close the file. */
                                547         [ -  + ]:             37 :         if (close(fd) != 0)
  442 peter@eisentraut.org      548                 :UBC           0 :             pg_fatal("could not close file \"%s\": %m", pathbuf);
                                549                 :                : 
                                550                 :                :         /* Parse the file contents. */
  626 rhaas@postgresql.org      551                 :CBC          37 :         parse_backup_label(pathbuf, buf, &start_tli, &start_lsn,
                                552                 :                :                            &previous_tli, &previous_lsn);
                                553                 :                : 
                                554                 :                :         /*
                                555                 :                :          * Sanity checks.
                                556                 :                :          *
                                557                 :                :          * XXX. It's actually not required that start_lsn == check_lsn. It
                                558                 :                :          * would be OK if start_lsn > check_lsn provided that start_lsn is
                                559                 :                :          * less than or equal to the relevant switchpoint. But at the moment
                                560                 :                :          * we don't have that information.
                                561                 :                :          */
                                562   [ +  +  +  + ]:             37 :         if (i > 0 && previous_tli == 0)
                                563                 :              1 :             pg_fatal("backup at \"%s\" is a full backup, but only the first backup should be a full backup",
                                564                 :                :                      backup_dirs[i]);
                                565   [ +  +  +  + ]:             36 :         if (i == 0 && previous_tli != 0)
                                566                 :              1 :             pg_fatal("backup at \"%s\" is an incremental backup, but the first backup should be a full backup",
                                567                 :                :                      backup_dirs[i]);
                                568   [ +  +  -  + ]:             35 :         if (i < n_backups - 1 && start_tli != check_tli)
  626 rhaas@postgresql.org      569                 :UBC           0 :             pg_fatal("backup at \"%s\" starts on timeline %u, but expected %u",
                                570                 :                :                      backup_dirs[i], start_tli, check_tli);
  626 rhaas@postgresql.org      571   [ +  +  +  + ]:CBC          35 :         if (i < n_backups - 1 && start_lsn != check_lsn)
   61 alvherre@kurilemu.de      572                 :GNC           3 :             pg_fatal("backup at \"%s\" starts at LSN %X/%08X, but expected %X/%08X",
                                573                 :                :                      backup_dirs[i],
                                574                 :                :                      LSN_FORMAT_ARGS(start_lsn),
                                575                 :                :                      LSN_FORMAT_ARGS(check_lsn));
  626 rhaas@postgresql.org      576                 :CBC          32 :         check_tli = previous_tli;
                                577                 :             32 :         check_lsn = previous_lsn;
                                578                 :                : 
                                579                 :                :         /*
                                580                 :                :          * The last backup label in the chain needs to be saved for later use,
                                581                 :                :          * while the others are only needed within this loop.
                                582                 :                :          */
                                583         [ +  + ]:             32 :         if (lastbuf == buf)
                                584                 :             18 :             buf = makeStringInfo();
                                585                 :                :         else
                                586                 :             14 :             resetStringInfo(buf);
                                587                 :                :     }
                                588                 :                : 
                                589                 :                :     /* Free memory that we don't need any more. */
                                590         [ +  - ]:             14 :     if (lastbuf != buf)
  539 dgustafsson@postgres      591                 :             14 :         destroyStringInfo(buf);
                                592                 :                : 
                                593                 :                :     /*
                                594                 :                :      * Return the data from the first backup_info that we read (which is the
                                595                 :                :      * backup_label from the last directory specified on the command line).
                                596                 :                :      */
  626 rhaas@postgresql.org      597                 :             14 :     return lastbuf;
                                598                 :                : }
                                599                 :                : 
                                600                 :                : /*
                                601                 :                :  * Sanity check control files and return system_identifier.
                                602                 :                :  */
                                603                 :                : static uint64
                                604                 :             20 : check_control_files(int n_backups, char **backup_dirs)
                                605                 :                : {
                                606                 :                :     int         i;
                                607                 :             20 :     uint64      system_identifier = 0;  /* placate compiler */
  499                           608                 :             20 :     uint32      data_checksum_version = 0;  /* placate compiler */
                                609                 :             20 :     bool        data_checksum_mismatch = false;
                                610                 :                : 
                                611                 :                :     /* Try to read each control file in turn, last to first. */
  626                           612         [ +  + ]:             60 :     for (i = n_backups - 1; i >= 0; --i)
                                613                 :                :     {
                                614                 :                :         ControlFileData *control_file;
                                615                 :                :         bool        crc_ok;
                                616                 :                :         char       *controlpath;
                                617                 :                : 
  152 fujii@postgresql.org      618                 :             41 :         controlpath = psprintf("%s/%s", backup_dirs[i], XLOG_CONTROL_FILE);
  626 rhaas@postgresql.org      619         [ +  + ]:             41 :         pg_log_debug("reading \"%s\"", controlpath);
  542                           620                 :             41 :         control_file = get_controlfile_by_exact_path(controlpath, &crc_ok);
                                621                 :                : 
                                622                 :                :         /* Control file contents not meaningful if CRC is bad. */
  626                           623         [ -  + ]:             41 :         if (!crc_ok)
  619 john.naylor@postgres      624                 :UBC           0 :             pg_fatal("%s: CRC is incorrect", controlpath);
                                625                 :                : 
                                626                 :                :         /* Can't interpret control file if not current version. */
  626 rhaas@postgresql.org      627         [ -  + ]:CBC          41 :         if (control_file->pg_control_version != PG_CONTROL_VERSION)
  626 rhaas@postgresql.org      628                 :UBC           0 :             pg_fatal("%s: unexpected control file version",
                                629                 :                :                      controlpath);
                                630                 :                : 
                                631                 :                :         /* System identifiers should all match. */
  626 rhaas@postgresql.org      632         [ +  + ]:CBC          41 :         if (i == n_backups - 1)
                                633                 :             20 :             system_identifier = control_file->system_identifier;
                                634         [ +  + ]:             21 :         else if (system_identifier != control_file->system_identifier)
  161 peter@eisentraut.org      635                 :              1 :             pg_fatal("%s: expected system identifier %" PRIu64 ", but found %" PRIu64,
                                636                 :                :                      controlpath, system_identifier,
                                637                 :                :                      control_file->system_identifier);
                                638                 :                : 
                                639                 :                :         /*
                                640                 :                :          * Detect checksum mismatches, but only if the last backup in the
                                641                 :                :          * chain has checksums enabled.
                                642                 :                :          */
  499 rhaas@postgresql.org      643         [ +  + ]:             40 :         if (i == n_backups - 1)
                                644                 :             20 :             data_checksum_version = control_file->data_checksum_version;
                                645         [ +  - ]:             20 :         else if (data_checksum_version != 0 &&
                                646         [ -  + ]:             20 :                  data_checksum_version != control_file->data_checksum_version)
  499 rhaas@postgresql.org      647                 :UBC           0 :             data_checksum_mismatch = true;
                                648                 :                : 
                                649                 :                :         /* Release memory. */
  626 rhaas@postgresql.org      650                 :CBC          40 :         pfree(control_file);
                                651                 :             40 :         pfree(controlpath);
                                652                 :                :     }
                                653                 :                : 
                                654                 :                :     /*
                                655                 :                :      * If debug output is enabled, make a note of the system identifier that
                                656                 :                :      * we found in all of the relevant control files.
                                657                 :                :      */
  161 peter@eisentraut.org      658         [ +  + ]:             19 :     pg_log_debug("system identifier is %" PRIu64, system_identifier);
                                659                 :                : 
                                660                 :                :     /*
                                661                 :                :      * Warn the user if not all backups are in the same state with regards to
                                662                 :                :      * checksums.
                                663                 :                :      */
  499 rhaas@postgresql.org      664         [ -  + ]:             19 :     if (data_checksum_mismatch)
                                665                 :                :     {
  499 rhaas@postgresql.org      666                 :UBC           0 :         pg_log_warning("only some backups have checksums enabled");
  375 peter@eisentraut.org      667                 :              0 :         pg_log_warning_hint("Disable, and optionally reenable, checksums on the output directory to avoid failures.");
                                668                 :                :     }
                                669                 :                : 
  542 rhaas@postgresql.org      670                 :CBC          19 :     return system_identifier;
                                671                 :                : }
                                672                 :                : 
                                673                 :                : /*
                                674                 :                :  * Set default permissions for new files and directories based on the
                                675                 :                :  * permissions of the given directory. The intent here is that the output
                                676                 :                :  * directory should use the same permissions scheme as the final input
                                677                 :                :  * directory.
                                678                 :                :  */
                                679                 :                : static void
  626                           680                 :             13 : check_input_dir_permissions(char *dir)
                                681                 :                : {
                                682                 :                :     struct stat st;
                                683                 :                : 
                                684         [ -  + ]:             13 :     if (stat(dir, &st) != 0)
  442 peter@eisentraut.org      685                 :UBC           0 :         pg_fatal("could not stat file \"%s\": %m", dir);
                                686                 :                : 
  626 rhaas@postgresql.org      687                 :CBC          13 :     SetDataDirectoryCreatePerm(st.st_mode);
                                688                 :             13 : }
                                689                 :                : 
                                690                 :                : /*
                                691                 :                :  * Clean up output directories before exiting.
                                692                 :                :  */
                                693                 :                : static void
                                694                 :             13 : cleanup_directories_atexit(void)
                                695                 :                : {
                                696         [ +  + ]:             14 :     while (cleanup_dir_list != NULL)
                                697                 :                :     {
                                698                 :              1 :         cb_cleanup_dir *dir = cleanup_dir_list;
                                699                 :                : 
                                700         [ +  - ]:              1 :         if (dir->rmtopdir)
                                701                 :                :         {
                                702                 :              1 :             pg_log_info("removing output directory \"%s\"", dir->target_path);
                                703         [ -  + ]:              1 :             if (!rmtree(dir->target_path, dir->rmtopdir))
  626 rhaas@postgresql.org      704                 :UBC           0 :                 pg_log_error("failed to remove output directory");
                                705                 :                :         }
                                706                 :                :         else
                                707                 :                :         {
                                708                 :              0 :             pg_log_info("removing contents of output directory \"%s\"",
                                709                 :                :                         dir->target_path);
                                710         [ #  # ]:              0 :             if (!rmtree(dir->target_path, dir->rmtopdir))
                                711                 :              0 :                 pg_log_error("failed to remove contents of output directory");
                                712                 :                :         }
                                713                 :                : 
  626 rhaas@postgresql.org      714                 :CBC           1 :         cleanup_dir_list = cleanup_dir_list->next;
                                715                 :              1 :         pfree(dir);
                                716                 :                :     }
                                717                 :             13 : }
                                718                 :                : 
                                719                 :                : /*
                                720                 :                :  * Create the named output directory, unless it already exists or we're in
                                721                 :                :  * dry-run mode. If it already exists but is not empty, that's a fatal error.
                                722                 :                :  *
                                723                 :                :  * Adds the created directory to the list of directories to be cleaned up
                                724                 :                :  * at process exit.
                                725                 :                :  */
                                726                 :                : static void
                                727                 :             14 : create_output_directory(char *dirname, cb_options *opt)
                                728                 :                : {
                                729   [ +  -  -  -  :             14 :     switch (pg_check_dir(dirname))
                                                 - ]
                                730                 :                :     {
                                731                 :             14 :         case 0:
                                732         [ -  + ]:             14 :             if (opt->dry_run)
                                733                 :                :             {
  626 rhaas@postgresql.org      734         [ #  # ]:UBC           0 :                 pg_log_debug("would create directory \"%s\"", dirname);
                                735                 :              0 :                 return;
                                736                 :                :             }
  626 rhaas@postgresql.org      737         [ +  + ]:CBC          14 :             pg_log_debug("creating directory \"%s\"", dirname);
                                738         [ -  + ]:             14 :             if (pg_mkdir_p(dirname, pg_dir_create_mode) == -1)
  626 rhaas@postgresql.org      739                 :UBC           0 :                 pg_fatal("could not create directory \"%s\": %m", dirname);
  626 rhaas@postgresql.org      740                 :CBC          14 :             remember_to_cleanup_directory(dirname, true);
                                741                 :             14 :             break;
                                742                 :                : 
  626 rhaas@postgresql.org      743                 :UBC           0 :         case 1:
                                744         [ #  # ]:              0 :             pg_log_debug("using existing directory \"%s\"", dirname);
                                745                 :              0 :             remember_to_cleanup_directory(dirname, false);
                                746                 :              0 :             break;
                                747                 :                : 
                                748                 :              0 :         case 2:
                                749                 :                :         case 3:
                                750                 :                :         case 4:
                                751                 :              0 :             pg_fatal("directory \"%s\" exists but is not empty", dirname);
                                752                 :                : 
                                753                 :              0 :         case -1:
                                754                 :              0 :             pg_fatal("could not access directory \"%s\": %m", dirname);
                                755                 :                :     }
                                756                 :                : }
                                757                 :                : 
                                758                 :                : /*
                                759                 :                :  * help
                                760                 :                :  *
                                761                 :                :  * Prints help page for the program
                                762                 :                :  *
                                763                 :                :  * progname: the name of the executed program, such as "pg_combinebackup"
                                764                 :                :  */
                                765                 :                : static void
  626 rhaas@postgresql.org      766                 :CBC           1 : help(const char *progname)
                                767                 :                : {
                                768                 :              1 :     printf(_("%s reconstructs full backups from incrementals.\n\n"), progname);
                                769                 :              1 :     printf(_("Usage:\n"));
                                770                 :              1 :     printf(_("  %s [OPTION]... DIRECTORY...\n"), progname);
                                771                 :              1 :     printf(_("\nOptions:\n"));
                                772                 :              1 :     printf(_("  -d, --debug               generate lots of debugging output\n"));
  173                           773                 :              1 :     printf(_("  -k, --link                link files instead of copying\n"));
  498                           774                 :              1 :     printf(_("  -n, --dry-run             do not actually do anything\n"));
  626                           775                 :              1 :     printf(_("  -N, --no-sync             do not wait for changes to be written safely to disk\n"));
  443 peter@eisentraut.org      776                 :              1 :     printf(_("  -o, --output=DIRECTORY    output directory\n"));
  620 michael@paquier.xyz       777                 :              1 :     printf(_("  -T, --tablespace-mapping=OLDDIR=NEWDIR\n"
                                778                 :                :              "                            relocate tablespace in OLDDIR to NEWDIR\n"));
  375 peter@eisentraut.org      779                 :              1 :     printf(_("      --clone               clone (reflink) files instead of copying\n"));
  433 tomas.vondra@postgre      780                 :              1 :     printf(_("      --copy                copy files (default)\n"));
  375 peter@eisentraut.org      781                 :              1 :     printf(_("      --copy-file-range     copy using copy_file_range() system call\n"));
  626 rhaas@postgresql.org      782                 :              1 :     printf(_("      --manifest-checksums=SHA{224,256,384,512}|CRC32C|NONE\n"
                                783                 :                :              "                            use algorithm for manifest checksums\n"));
                                784                 :              1 :     printf(_("      --no-manifest         suppress generation of backup manifest\n"));
                                785                 :              1 :     printf(_("      --sync-method=METHOD  set method for syncing files to disk\n"));
  500 peter@eisentraut.org      786                 :              1 :     printf(_("  -V, --version             output version information, then exit\n"));
  626 rhaas@postgresql.org      787                 :              1 :     printf(_("  -?, --help                show this help, then exit\n"));
                                788                 :                : 
                                789                 :              1 :     printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
                                790                 :              1 :     printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
                                791                 :              1 : }
                                792                 :                : 
                                793                 :                : /*
                                794                 :                :  * Try to parse a string as a non-zero OID without leading zeroes.
                                795                 :                :  *
                                796                 :                :  * If it works, return true and set *result to the answer, else return false.
                                797                 :                :  */
                                798                 :                : static bool
                                799                 :              2 : parse_oid(char *s, Oid *result)
                                800                 :                : {
                                801                 :                :     Oid         oid;
                                802                 :                :     char       *ep;
                                803                 :                : 
                                804                 :              2 :     errno = 0;
                                805                 :              2 :     oid = strtoul(s, &ep, 10);
                                806   [ +  -  +  -  :              2 :     if (errno != 0 || *ep != '\0' || oid < 1 || oid > PG_UINT32_MAX)
                                              -  + ]
  626 rhaas@postgresql.org      807                 :UBC           0 :         return false;
                                808                 :                : 
  626 rhaas@postgresql.org      809                 :CBC           2 :     *result = oid;
                                810                 :              2 :     return true;
                                811                 :                : }
                                812                 :                : 
                                813                 :                : /*
                                814                 :                :  * Copy files from the input directory to the output directory, reconstructing
                                815                 :                :  * full files from incremental files as required.
                                816                 :                :  *
                                817                 :                :  * If processing a user-defined tablespace, the tsoid should be the OID
                                818                 :                :  * of that tablespace and input_directory and output_directory should be the
                                819                 :                :  * toplevel input and output directories for that tablespace. Otherwise,
                                820                 :                :  * tsoid should be InvalidOid and input_directory and output_directory should
                                821                 :                :  * be the main input and output directories.
                                822                 :                :  *
                                823                 :                :  * relative_path is the path beneath the given input and output directories
                                824                 :                :  * that we are currently processing. If NULL, it indicates that we're
                                825                 :                :  * processing the input and output directories themselves.
                                826                 :                :  *
                                827                 :                :  * n_prior_backups is the number of prior backups that we have available.
                                828                 :                :  * This doesn't count the very last backup, which is referenced by
                                829                 :                :  * input_directory, just the older ones. prior_backup_dirs is an array of
                                830                 :                :  * the locations of those previous backups.
                                831                 :                :  */
                                832                 :                : static void
                                833                 :            341 : process_directory_recursively(Oid tsoid,
                                834                 :                :                               char *input_directory,
                                835                 :                :                               char *output_directory,
                                836                 :                :                               char *relative_path,
                                837                 :                :                               int n_prior_backups,
                                838                 :                :                               char **prior_backup_dirs,
                                839                 :                :                               manifest_data **manifests,
                                840                 :                :                               manifest_writer *mwriter,
                                841                 :                :                               cb_options *opt)
                                842                 :                : {
                                843                 :                :     char        ifulldir[MAXPGPATH];
                                844                 :                :     char        ofulldir[MAXPGPATH];
                                845                 :                :     char        manifest_prefix[MAXPGPATH];
                                846                 :                :     DIR        *dir;
                                847                 :                :     struct dirent *de;
  506                           848                 :            341 :     bool        is_pg_tblspc = false;
                                849                 :            341 :     bool        is_pg_wal = false;
                                850                 :            341 :     bool        is_incremental_dir = false;
  626                           851                 :            341 :     manifest_data *latest_manifest = manifests[n_prior_backups];
                                852                 :                :     pg_checksum_type checksum_type;
                                853                 :                : 
                                854                 :                :     /*
                                855                 :                :      * Classify this directory.
                                856                 :                :      *
                                857                 :                :      * We set is_pg_tblspc only for the toplevel pg_tblspc directory, because
                                858                 :                :      * the symlinks in that specific directory require special handling.
                                859                 :                :      *
                                860                 :                :      * We set is_pg_wal for the toplevel WAL directory and all of its
                                861                 :                :      * subdirectories, because those files are not included in the backup
                                862                 :                :      * manifest and hence need special treatment. (Since incremental backup
                                863                 :                :      * does not exist in pre-v10 versions, we don't have to worry about the
                                864                 :                :      * old pg_xlog naming.)
                                865                 :                :      *
                                866                 :                :      * We set is_incremental_dir for directories that can contain incremental
                                867                 :                :      * files requiring reconstruction. If such files occur outside these
                                868                 :                :      * directories, we want to just copy them straight to the output
                                869                 :                :      * directory. This is to protect against a user creating a file with a
                                870                 :                :      * strange name like INCREMENTAL.config and then complaining that
                                871                 :                :      * incremental backups don't work properly. The test here is a bit tricky:
                                872                 :                :      * incremental files occur in subdirectories of base, in pg_global itself,
                                873                 :                :      * and in subdirectories of pg_tblspc only if in-place tablespaces are
                                874                 :                :      * used.
                                875                 :                :      */
  506                           876         [ +  + ]:            341 :     if (OidIsValid(tsoid))
                                877                 :              3 :         is_incremental_dir = true;
                                878         [ +  + ]:            338 :     else if (relative_path != NULL)
                                879                 :                :     {
  368 michael@paquier.xyz       880                 :            325 :         is_pg_tblspc = strcmp(relative_path, PG_TBLSPC_DIR) == 0;
  506 rhaas@postgresql.org      881         [ +  + ]:            638 :         is_pg_wal = (strcmp(relative_path, "pg_wal") == 0 ||
                                882         [ +  + ]:            313 :                      strncmp(relative_path, "pg_wal/", 7) == 0);
                                883                 :            325 :         is_incremental_dir = strncmp(relative_path, "base/", 5) == 0 ||
                                884   [ +  +  +  + ]:            596 :             strcmp(relative_path, "global") == 0 ||
  368 michael@paquier.xyz       885         [ -  + ]:            271 :             strncmp(relative_path, PG_TBLSPC_DIR_SLASH, 10) == 0;
                                886                 :                :     }
                                887                 :                : 
                                888                 :                :     /*
                                889                 :                :      * If we're under pg_wal, then we don't need checksums, because these
                                890                 :                :      * files aren't included in the backup manifest. Otherwise use whatever
                                891                 :                :      * type of checksum is configured.
                                892                 :                :      */
  626 rhaas@postgresql.org      893         [ +  + ]:            341 :     if (!is_pg_wal)
                                894                 :            305 :         checksum_type = opt->manifest_checksums;
                                895                 :                :     else
                                896                 :             36 :         checksum_type = CHECKSUM_TYPE_NONE;
                                897                 :                : 
                                898                 :                :     /*
                                899                 :                :      * Append the relative path to the input and output directories, and
                                900                 :                :      * figure out the appropriate prefix to add to files in this directory
                                901                 :                :      * when looking them up in a backup manifest.
                                902                 :                :      */
                                903         [ +  + ]:            341 :     if (relative_path == NULL)
                                904                 :                :     {
  604                           905                 :             14 :         strlcpy(ifulldir, input_directory, MAXPGPATH);
                                906                 :             14 :         strlcpy(ofulldir, output_directory, MAXPGPATH);
  626                           907         [ +  + ]:             14 :         if (OidIsValid(tsoid))
  368 michael@paquier.xyz       908                 :              1 :             snprintf(manifest_prefix, MAXPGPATH, "%s/%u/", PG_TBLSPC_DIR, tsoid);
                                909                 :                :         else
  626 rhaas@postgresql.org      910                 :             13 :             manifest_prefix[0] = '\0';
                                911                 :                :     }
                                912                 :                :     else
                                913                 :                :     {
                                914                 :            327 :         snprintf(ifulldir, MAXPGPATH, "%s/%s", input_directory,
                                915                 :                :                  relative_path);
                                916                 :            327 :         snprintf(ofulldir, MAXPGPATH, "%s/%s", output_directory,
                                917                 :                :                  relative_path);
                                918         [ +  + ]:            327 :         if (OidIsValid(tsoid))
  368 michael@paquier.xyz       919                 :              2 :             snprintf(manifest_prefix, MAXPGPATH, "%s/%u/%s/",
                                920                 :                :                      PG_TBLSPC_DIR, tsoid, relative_path);
                                921                 :                :         else
  626 rhaas@postgresql.org      922                 :            325 :             snprintf(manifest_prefix, MAXPGPATH, "%s/", relative_path);
                                923                 :                :     }
                                924                 :                : 
                                925                 :                :     /*
                                926                 :                :      * Toplevel output directories have already been created by the time this
                                927                 :                :      * function is called, but any subdirectories are our responsibility.
                                928                 :                :      */
                                929         [ +  + ]:            341 :     if (relative_path != NULL)
                                930                 :                :     {
                                931         [ -  + ]:            327 :         if (opt->dry_run)
  626 rhaas@postgresql.org      932         [ #  # ]:UBC           0 :             pg_log_debug("would create directory \"%s\"", ofulldir);
                                933                 :                :         else
                                934                 :                :         {
  626 rhaas@postgresql.org      935         [ +  + ]:CBC         327 :             pg_log_debug("creating directory \"%s\"", ofulldir);
                                936         [ -  + ]:            327 :             if (mkdir(ofulldir, pg_dir_create_mode) == -1)
  626 rhaas@postgresql.org      937                 :UBC           0 :                 pg_fatal("could not create directory \"%s\": %m", ofulldir);
                                938                 :                :         }
                                939                 :                :     }
                                940                 :                : 
                                941                 :                :     /* It's time to scan the directory. */
  626 rhaas@postgresql.org      942         [ -  + ]:CBC         341 :     if ((dir = opendir(ifulldir)) == NULL)
  626 rhaas@postgresql.org      943                 :UBC           0 :         pg_fatal("could not open directory \"%s\": %m", ifulldir);
  626 rhaas@postgresql.org      944         [ +  + ]:CBC       14303 :     while (errno = 0, (de = readdir(dir)) != NULL)
                                945                 :                :     {
                                946                 :                :         PGFileType  type;
                                947                 :                :         char        ifullpath[MAXPGPATH];
                                948                 :                :         char        ofullpath[MAXPGPATH];
                                949                 :                :         char        manifest_path[MAXPGPATH];
                                950                 :          13965 :         Oid         oid = InvalidOid;
                                951                 :          13965 :         int         checksum_length = 0;
                                952                 :          13965 :         uint8      *checksum_payload = NULL;
                                953                 :                :         pg_checksum_context checksum_ctx;
                                954                 :                : 
                                955                 :                :         /* Ignore "." and ".." entries. */
                                956         [ +  + ]:          13965 :         if (strcmp(de->d_name, ".") == 0 ||
                                957         [ +  + ]:          13626 :             strcmp(de->d_name, "..") == 0)
                                958                 :           1032 :             continue;
                                959                 :                : 
                                960                 :                :         /* Construct input path. */
                                961                 :          13285 :         snprintf(ifullpath, MAXPGPATH, "%s/%s", ifulldir, de->d_name);
                                962                 :                : 
                                963                 :                :         /* Figure out what kind of directory entry this is. */
                                964                 :          13285 :         type = get_dirent_type(ifullpath, de, false, PG_LOG_ERROR);
                                965         [ -  + ]:          13285 :         if (type == PGFILETYPE_ERROR)
  626 rhaas@postgresql.org      966                 :UBC           0 :             exit(1);
                                967                 :                : 
                                968                 :                :         /*
                                969                 :                :          * If we're processing pg_tblspc, then check whether the filename
                                970                 :                :          * looks like it could be a tablespace OID. If so, and if the
                                971                 :                :          * directory entry is a symbolic link or a directory, skip it.
                                972                 :                :          *
                                973                 :                :          * Our goal here is to ignore anything that would have been considered
                                974                 :                :          * by scan_for_existing_tablespaces to be a tablespace.
                                975                 :                :          */
  626 rhaas@postgresql.org      976   [ +  +  +  -  :CBC       13285 :         if (is_pg_tblspc && parse_oid(de->d_name, &oid) &&
                                              -  + ]
  626 rhaas@postgresql.org      977         [ #  # ]:UBC           0 :             (type == PGFILETYPE_LNK || type == PGFILETYPE_DIR))
  626 rhaas@postgresql.org      978                 :CBC           1 :             continue;
                                979                 :                : 
                                980                 :                :         /* If it's a directory, recurse. */
                                981         [ +  + ]:          13284 :         if (type == PGFILETYPE_DIR)
                                982                 :            325 :         {
                                983                 :                :             char        new_relative_path[MAXPGPATH];
                                984                 :                : 
                                985                 :                :             /* Append new pathname component to relative path. */
                                986         [ +  + ]:            327 :             if (relative_path == NULL)
  604                           987                 :            213 :                 strlcpy(new_relative_path, de->d_name, MAXPGPATH);
                                988                 :                :             else
  626                           989                 :            114 :                 snprintf(new_relative_path, MAXPGPATH, "%s/%s", relative_path,
                                990                 :            114 :                          de->d_name);
                                991                 :                : 
                                992                 :                :             /* And recurse. */
                                993                 :            327 :             process_directory_recursively(tsoid,
                                994                 :                :                                           input_directory, output_directory,
                                995                 :                :                                           new_relative_path,
                                996                 :                :                                           n_prior_backups, prior_backup_dirs,
                                997                 :                :                                           manifests, mwriter, opt);
                                998                 :            325 :             continue;
                                999                 :                :         }
                               1000                 :                : 
                               1001                 :                :         /* Skip anything that's not a regular file. */
                               1002         [ -  + ]:          12957 :         if (type != PGFILETYPE_REG)
                               1003                 :                :         {
  626 rhaas@postgresql.org     1004         [ #  # ]:UBC           0 :             if (type == PGFILETYPE_LNK)
                               1005                 :              0 :                 pg_log_warning("skipping symbolic link \"%s\"", ifullpath);
                               1006                 :                :             else
                               1007                 :              0 :                 pg_log_warning("skipping special file \"%s\"", ifullpath);
                               1008                 :              0 :             continue;
                               1009                 :                :         }
                               1010                 :                : 
                               1011                 :                :         /*
                               1012                 :                :          * Skip the backup_label and backup_manifest files; they require
                               1013                 :                :          * special handling and are handled elsewhere.
                               1014                 :                :          */
  626 rhaas@postgresql.org     1015         [ +  + ]:CBC       12957 :         if (relative_path == NULL &&
                               1016         [ +  + ]:             92 :             (strcmp(de->d_name, "backup_label") == 0 ||
                               1017         [ +  + ]:             79 :              strcmp(de->d_name, "backup_manifest") == 0))
                               1018                 :             26 :             continue;
                               1019                 :                : 
                               1020                 :                :         /*
                               1021                 :                :          * If it's an incremental file, hand it off to the reconstruction
                               1022                 :                :          * code, which will figure out what to do.
                               1023                 :                :          */
  506                          1024         [ +  + ]:          12931 :         if (is_incremental_dir &&
                               1025         [ +  + ]:          12803 :             strncmp(de->d_name, INCREMENTAL_PREFIX,
                               1026                 :                :                     INCREMENTAL_PREFIX_LENGTH) == 0)
                               1027                 :                :         {
                               1028                 :                :             /* Output path should not include "INCREMENTAL." prefix. */
  626                          1029                 :           6453 :             snprintf(ofullpath, MAXPGPATH, "%s/%s", ofulldir,
                               1030                 :           6453 :                      de->d_name + INCREMENTAL_PREFIX_LENGTH);
                               1031                 :                : 
                               1032                 :                : 
                               1033                 :                :             /* Manifest path likewise omits incremental prefix. */
                               1034                 :           6453 :             snprintf(manifest_path, MAXPGPATH, "%s%s", manifest_prefix,
                               1035                 :           6453 :                      de->d_name + INCREMENTAL_PREFIX_LENGTH);
                               1036                 :                : 
                               1037                 :                :             /* Reconstruction logic will do the rest. */
                               1038                 :           6453 :             reconstruct_from_incremental_file(ifullpath, ofullpath,
                               1039                 :                :                                               manifest_prefix,
                               1040                 :           6453 :                                               de->d_name + INCREMENTAL_PREFIX_LENGTH,
                               1041                 :                :                                               n_prior_backups,
                               1042                 :                :                                               prior_backup_dirs,
                               1043                 :                :                                               manifests,
                               1044                 :                :                                               manifest_path,
                               1045                 :                :                                               checksum_type,
                               1046                 :                :                                               &checksum_length,
                               1047                 :                :                                               &checksum_payload,
                               1048                 :                :                                               opt->copy_method,
                               1049                 :           6453 :                                               opt->debug,
                               1050                 :           6453 :                                               opt->dry_run);
                               1051                 :                :         }
                               1052                 :                :         else
                               1053                 :                :         {
                               1054                 :                :             /* Construct the path that the backup_manifest will use. */
                               1055                 :           6478 :             snprintf(manifest_path, MAXPGPATH, "%s%s", manifest_prefix,
                               1056                 :           6478 :                      de->d_name);
                               1057                 :                : 
                               1058                 :                :             /*
                               1059                 :                :              * It's not an incremental file, so we need to copy the entire
                               1060                 :                :              * file to the output directory.
                               1061                 :                :              *
                               1062                 :                :              * If a checksum of the required type already exists in the
                               1063                 :                :              * backup_manifest for the final input directory, we can save some
                               1064                 :                :              * work by reusing that checksum instead of computing a new one.
                               1065                 :                :              */
                               1066   [ +  +  +  - ]:           6478 :             if (checksum_type != CHECKSUM_TYPE_NONE &&
                               1067                 :                :                 latest_manifest != NULL)
                               1068                 :                :             {
                               1069                 :                :                 manifest_file *mfile;
                               1070                 :                : 
                               1071                 :           4531 :                 mfile = manifest_files_lookup(latest_manifest->files,
                               1072                 :                :                                               manifest_path);
                               1073         [ -  + ]:           4531 :                 if (mfile == NULL)
                               1074                 :                :                 {
                               1075                 :                :                     char       *bmpath;
                               1076                 :                : 
                               1077                 :                :                     /*
                               1078                 :                :                      * The directory is out of sync with the backup_manifest,
                               1079                 :                :                      * so emit a warning.
                               1080                 :                :                      */
  626 rhaas@postgresql.org     1081                 :UBC           0 :                     bmpath = psprintf("%s/%s", input_directory,
                               1082                 :                :                                       "backup_manifest");
  375 peter@eisentraut.org     1083                 :              0 :                     pg_log_warning("manifest file \"%s\" contains no entry for file \"%s\"",
                               1084                 :                :                                    bmpath, manifest_path);
  626 rhaas@postgresql.org     1085                 :              0 :                     pfree(bmpath);
                               1086                 :                :                 }
  626 rhaas@postgresql.org     1087         [ +  + ]:CBC        4531 :                 else if (mfile->checksum_type == checksum_type)
                               1088                 :                :                 {
                               1089                 :           3564 :                     checksum_length = mfile->checksum_length;
                               1090                 :           3564 :                     checksum_payload = mfile->checksum_payload;
                               1091                 :                :                 }
                               1092                 :                :             }
                               1093                 :                : 
                               1094                 :                :             /*
                               1095                 :                :              * If we're reusing a checksum, then we don't need copy_file() to
                               1096                 :                :              * compute one for us, but otherwise, it needs to compute whatever
                               1097                 :                :              * type of checksum we need.
                               1098                 :                :              */
                               1099         [ +  + ]:           6478 :             if (checksum_length != 0)
                               1100                 :           3564 :                 pg_checksum_init(&checksum_ctx, CHECKSUM_TYPE_NONE);
                               1101                 :                :             else
                               1102                 :           2914 :                 pg_checksum_init(&checksum_ctx, checksum_type);
                               1103                 :                : 
                               1104                 :                :             /* Actually copy the file. */
                               1105                 :           6478 :             snprintf(ofullpath, MAXPGPATH, "%s/%s", ofulldir, de->d_name);
  519 tomas.vondra@postgre     1106                 :           6478 :             copy_file(ifullpath, ofullpath, &checksum_ctx,
                               1107                 :           6478 :                       opt->copy_method, opt->dry_run);
                               1108                 :                : 
                               1109                 :                :             /*
                               1110                 :                :              * If copy_file() performed a checksum calculation for us, then
                               1111                 :                :              * save the results (except in dry-run mode, when there's no
                               1112                 :                :              * point).
                               1113                 :                :              */
  626 rhaas@postgresql.org     1114   [ +  +  +  - ]:           6478 :             if (checksum_ctx.type != CHECKSUM_TYPE_NONE && !opt->dry_run)
                               1115                 :                :             {
                               1116                 :            967 :                 checksum_payload = pg_malloc(PG_CHECKSUM_MAX_LENGTH);
                               1117                 :            967 :                 checksum_length = pg_checksum_final(&checksum_ctx,
                               1118                 :                :                                                     checksum_payload);
                               1119                 :                :             }
                               1120                 :                :         }
                               1121                 :                : 
                               1122                 :                :         /* Generate manifest entry, if needed. */
                               1123         [ +  + ]:          12930 :         if (mwriter != NULL)
                               1124                 :                :         {
                               1125                 :                :             struct stat sb;
                               1126                 :                : 
                               1127                 :                :             /*
                               1128                 :                :              * In order to generate a manifest entry, we need the file size
                               1129                 :                :              * and mtime. We have no way to know the correct mtime except to
                               1130                 :                :              * stat() the file, so just do that and get the size as well.
                               1131                 :                :              *
                               1132                 :                :              * If we didn't need the mtime here, we could try to obtain the
                               1133                 :                :              * file size from the reconstruction or file copy process above,
                               1134                 :                :              * although that is actually not convenient in all cases. If we
                               1135                 :                :              * write the file ourselves then clearly we can keep a count of
                               1136                 :                :              * bytes, but if we use something like CopyFile() then it's
                               1137                 :                :              * trickier. Since we have to stat() anyway to get the mtime,
                               1138                 :                :              * there's no point in worrying about it.
                               1139                 :                :              */
                               1140         [ -  + ]:          11962 :             if (stat(ofullpath, &sb) < 0)
  626 rhaas@postgresql.org     1141                 :UBC           0 :                 pg_fatal("could not stat file \"%s\": %m", ofullpath);
                               1142                 :                : 
                               1143                 :                :             /* OK, now do the work. */
  626 rhaas@postgresql.org     1144                 :CBC       11962 :             add_file_to_manifest(mwriter, manifest_path,
                               1145                 :          11962 :                                  sb.st_size, sb.st_mtime,
                               1146                 :                :                                  checksum_type, checksum_length,
                               1147                 :                :                                  checksum_payload);
                               1148                 :                :         }
                               1149                 :                : 
                               1150                 :                :         /* Avoid leaking memory. */
                               1151         [ +  + ]:          12930 :         if (checksum_payload != NULL)
                               1152                 :          10983 :             pfree(checksum_payload);
                               1153                 :                :     }
                               1154                 :                : 
                               1155                 :            338 :     closedir(dir);
                               1156                 :            338 : }
                               1157                 :                : 
                               1158                 :                : /*
                               1159                 :                :  * Read the version number from PG_VERSION and convert it to the usual server
                               1160                 :                :  * version number format. (e.g. If PG_VERSION contains "14\n" this function
                               1161                 :                :  * will return 140000)
                               1162                 :                :  */
                               1163                 :                : static int
                               1164                 :             20 : read_pg_version_file(char *directory)
                               1165                 :                : {
                               1166                 :                :     char        filename[MAXPGPATH];
                               1167                 :                :     StringInfoData buf;
                               1168                 :                :     int         fd;
                               1169                 :                :     int         version;
                               1170                 :                :     char       *ep;
                               1171                 :                : 
                               1172                 :                :     /* Construct pathname. */
                               1173                 :             20 :     snprintf(filename, MAXPGPATH, "%s/PG_VERSION", directory);
                               1174                 :                : 
                               1175                 :                :     /* Open file. */
                               1176         [ -  + ]:             20 :     if ((fd = open(filename, O_RDONLY, 0)) < 0)
  626 rhaas@postgresql.org     1177                 :UBC           0 :         pg_fatal("could not open file \"%s\": %m", filename);
                               1178                 :                : 
                               1179                 :                :     /* Read into memory. Length limit of 128 should be more than generous. */
  626 rhaas@postgresql.org     1180                 :CBC          20 :     initStringInfo(&buf);
                               1181                 :             20 :     slurp_file(fd, filename, &buf, 128);
                               1182                 :                : 
                               1183                 :                :     /* Close the file. */
                               1184         [ -  + ]:             20 :     if (close(fd) != 0)
  442 peter@eisentraut.org     1185                 :UBC           0 :         pg_fatal("could not close file \"%s\": %m", filename);
                               1186                 :                : 
                               1187                 :                :     /* Convert to integer. */
  626 rhaas@postgresql.org     1188                 :CBC          20 :     errno = 0;
                               1189                 :             20 :     version = strtoul(buf.data, &ep, 10);
                               1190   [ +  -  -  + ]:             20 :     if (errno != 0 || *ep != '\n')
                               1191                 :                :     {
                               1192                 :                :         /*
                               1193                 :                :          * Incremental backup is not relevant to very old server versions that
                               1194                 :                :          * used multi-part version number (e.g. 9.6, or 8.4). So if we see
                               1195                 :                :          * what looks like the beginning of such a version number, just bail
                               1196                 :                :          * out.
                               1197                 :                :          */
  626 rhaas@postgresql.org     1198   [ #  #  #  # ]:UBC           0 :         if (version < 10 && *ep == '.')
  442 peter@eisentraut.org     1199                 :              0 :             pg_fatal("%s: server version too old", filename);
                               1200                 :              0 :         pg_fatal("%s: could not parse version number", filename);
                               1201                 :                :     }
                               1202                 :                : 
                               1203                 :                :     /* Debugging output. */
  442 peter@eisentraut.org     1204         [ +  + ]:CBC          20 :     pg_log_debug("read server version %d from file \"%s\"", version, filename);
                               1205                 :                : 
                               1206                 :                :     /* Release memory and return result. */
  626 rhaas@postgresql.org     1207                 :             20 :     pfree(buf.data);
                               1208                 :             20 :     return version * 10000;
                               1209                 :                : }
                               1210                 :                : 
                               1211                 :                : /*
                               1212                 :                :  * Add a directory to the list of output directories to clean up.
                               1213                 :                :  */
                               1214                 :                : static void
                               1215                 :             14 : remember_to_cleanup_directory(char *target_path, bool rmtopdir)
                               1216                 :                : {
                               1217                 :             14 :     cb_cleanup_dir *dir = pg_malloc(sizeof(cb_cleanup_dir));
                               1218                 :                : 
                               1219                 :             14 :     dir->target_path = target_path;
                               1220                 :             14 :     dir->rmtopdir = rmtopdir;
                               1221                 :             14 :     dir->next = cleanup_dir_list;
                               1222                 :             14 :     cleanup_dir_list = dir;
                               1223                 :             14 : }
                               1224                 :                : 
                               1225                 :                : /*
                               1226                 :                :  * Empty out the list of directories scheduled for cleanup at exit.
                               1227                 :                :  *
                               1228                 :                :  * We want to remove the output directories only on a failure, so call this
                               1229                 :                :  * function when we know that the operation has succeeded.
                               1230                 :                :  *
                               1231                 :                :  * Since we only expect this to be called when we're about to exit, we could
                               1232                 :                :  * just set cleanup_dir_list to NULL and be done with it, but we free the
                               1233                 :                :  * memory to be tidy.
                               1234                 :                :  */
                               1235                 :                : static void
                               1236                 :             12 : reset_directory_cleanup_list(void)
                               1237                 :                : {
                               1238         [ +  + ]:             25 :     while (cleanup_dir_list != NULL)
                               1239                 :                :     {
                               1240                 :             13 :         cb_cleanup_dir *dir = cleanup_dir_list;
                               1241                 :                : 
                               1242                 :             13 :         cleanup_dir_list = cleanup_dir_list->next;
                               1243                 :             13 :         pfree(dir);
                               1244                 :                :     }
                               1245                 :             12 : }
                               1246                 :                : 
                               1247                 :                : /*
                               1248                 :                :  * Scan the pg_tblspc directory of the final input backup to get a canonical
                               1249                 :                :  * list of what tablespaces are part of the backup.
                               1250                 :                :  *
                               1251                 :                :  * 'pathname' should be the path to the toplevel backup directory for the
                               1252                 :                :  * final backup in the backup chain.
                               1253                 :                :  */
                               1254                 :                : static cb_tablespace *
                               1255                 :             13 : scan_for_existing_tablespaces(char *pathname, cb_options *opt)
                               1256                 :                : {
                               1257                 :                :     char        pg_tblspc[MAXPGPATH];
                               1258                 :                :     DIR        *dir;
                               1259                 :                :     struct dirent *de;
                               1260                 :             13 :     cb_tablespace *tslist = NULL;
                               1261                 :                : 
  368 michael@paquier.xyz      1262                 :             13 :     snprintf(pg_tblspc, MAXPGPATH, "%s/%s", pathname, PG_TBLSPC_DIR);
  626 rhaas@postgresql.org     1263         [ +  + ]:             13 :     pg_log_debug("scanning \"%s\"", pg_tblspc);
                               1264                 :                : 
                               1265         [ -  + ]:             13 :     if ((dir = opendir(pg_tblspc)) == NULL)
  604 rhaas@postgresql.org     1266                 :UBC           0 :         pg_fatal("could not open directory \"%s\": %m", pg_tblspc);
                               1267                 :                : 
  626 rhaas@postgresql.org     1268         [ +  + ]:CBC          40 :     while (errno = 0, (de = readdir(dir)) != NULL)
                               1269                 :                :     {
                               1270                 :                :         Oid         oid;
                               1271                 :                :         char        tblspcdir[MAXPGPATH];
                               1272                 :                :         char        link_target[MAXPGPATH];
                               1273                 :                :         int         link_length;
                               1274                 :                :         cb_tablespace *ts;
                               1275                 :                :         cb_tablespace *otherts;
                               1276                 :                :         PGFileType  type;
                               1277                 :                : 
                               1278                 :                :         /* Silently ignore "." and ".." entries. */
                               1279   [ +  +  +  + ]:             27 :         if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0)
                               1280                 :             26 :             continue;
                               1281                 :                : 
                               1282                 :                :         /* Construct full pathname. */
                               1283                 :              1 :         snprintf(tblspcdir, MAXPGPATH, "%s/%s", pg_tblspc, de->d_name);
                               1284                 :                : 
                               1285                 :                :         /* Ignore any file name that doesn't look like a proper OID. */
                               1286         [ -  + ]:              1 :         if (!parse_oid(de->d_name, &oid))
                               1287                 :                :         {
  626 rhaas@postgresql.org     1288         [ #  # ]:UBC           0 :             pg_log_debug("skipping \"%s\" because the filename is not a legal tablespace OID",
                               1289                 :                :                          tblspcdir);
                               1290                 :              0 :             continue;
                               1291                 :                :         }
                               1292                 :                : 
                               1293                 :                :         /* Only symbolic links and directories are tablespaces. */
  626 rhaas@postgresql.org     1294                 :CBC           1 :         type = get_dirent_type(tblspcdir, de, false, PG_LOG_ERROR);
                               1295         [ -  + ]:              1 :         if (type == PGFILETYPE_ERROR)
  626 rhaas@postgresql.org     1296                 :UBC           0 :             exit(1);
  626 rhaas@postgresql.org     1297   [ -  +  -  - ]:CBC           1 :         if (type != PGFILETYPE_LNK && type != PGFILETYPE_DIR)
                               1298                 :                :         {
  626 rhaas@postgresql.org     1299         [ #  # ]:UBC           0 :             pg_log_debug("skipping \"%s\" because it is neither a symbolic link nor a directory",
                               1300                 :                :                          tblspcdir);
                               1301                 :              0 :             continue;
                               1302                 :                :         }
                               1303                 :                : 
                               1304                 :                :         /* Create a new tablespace object. */
  626 rhaas@postgresql.org     1305                 :CBC           1 :         ts = pg_malloc0(sizeof(cb_tablespace));
                               1306                 :              1 :         ts->oid = oid;
                               1307                 :                : 
                               1308                 :                :         /*
                               1309                 :                :          * If it's a link, it's not an in-place tablespace. Otherwise, it must
                               1310                 :                :          * be a directory, and thus an in-place tablespace.
                               1311                 :                :          */
                               1312         [ +  - ]:              1 :         if (type == PGFILETYPE_LNK)
                               1313                 :                :         {
                               1314                 :                :             cb_tablespace_mapping *tsmap;
                               1315                 :                : 
                               1316                 :                :             /* Read the link target. */
                               1317                 :              1 :             link_length = readlink(tblspcdir, link_target, sizeof(link_target));
                               1318         [ -  + ]:              1 :             if (link_length < 0)
  626 rhaas@postgresql.org     1319                 :UBC           0 :                 pg_fatal("could not read symbolic link \"%s\": %m",
                               1320                 :                :                          tblspcdir);
  626 rhaas@postgresql.org     1321         [ -  + ]:CBC           1 :             if (link_length >= sizeof(link_target))
  442 peter@eisentraut.org     1322                 :UBC           0 :                 pg_fatal("target of symbolic link \"%s\" is too long", tblspcdir);
  626 rhaas@postgresql.org     1323                 :CBC           1 :             link_target[link_length] = '\0';
                               1324         [ -  + ]:              1 :             if (!is_absolute_path(link_target))
  442 peter@eisentraut.org     1325                 :UBC           0 :                 pg_fatal("target of symbolic link \"%s\" is relative", tblspcdir);
                               1326                 :                : 
                               1327                 :                :             /* Canonicalize the link target. */
  626 rhaas@postgresql.org     1328                 :CBC           1 :             canonicalize_path(link_target);
                               1329                 :                : 
                               1330                 :                :             /*
                               1331                 :                :              * Find the corresponding tablespace mapping and copy the relevant
                               1332                 :                :              * details into the new tablespace entry.
                               1333                 :                :              */
                               1334         [ +  - ]:              1 :             for (tsmap = opt->tsmappings; tsmap != NULL; tsmap = tsmap->next)
                               1335                 :                :             {
                               1336         [ +  - ]:              1 :                 if (strcmp(tsmap->old_dir, link_target) == 0)
                               1337                 :                :                 {
  604                          1338                 :              1 :                     strlcpy(ts->old_dir, tsmap->old_dir, MAXPGPATH);
                               1339                 :              1 :                     strlcpy(ts->new_dir, tsmap->new_dir, MAXPGPATH);
  626                          1340                 :              1 :                     ts->in_place = false;
                               1341                 :              1 :                     break;
                               1342                 :                :                 }
                               1343                 :                :             }
                               1344                 :                : 
                               1345                 :                :             /* Every non-in-place tablespace must be mapped. */
                               1346         [ -  + ]:              1 :             if (tsmap == NULL)
  626 rhaas@postgresql.org     1347                 :UBC           0 :                 pg_fatal("tablespace at \"%s\" has no tablespace mapping",
                               1348                 :                :                          link_target);
                               1349                 :                :         }
                               1350                 :                :         else
                               1351                 :                :         {
                               1352                 :                :             /*
                               1353                 :                :              * For an in-place tablespace, there's no separate directory, so
                               1354                 :                :              * we just record the paths within the data directories.
                               1355                 :                :              */
                               1356                 :              0 :             snprintf(ts->old_dir, MAXPGPATH, "%s/%s", pg_tblspc, de->d_name);
  368 michael@paquier.xyz      1357                 :              0 :             snprintf(ts->new_dir, MAXPGPATH, "%s/%s/%s", opt->output,
                               1358                 :              0 :                      PG_TBLSPC_DIR, de->d_name);
  626 rhaas@postgresql.org     1359                 :              0 :             ts->in_place = true;
                               1360                 :                :         }
                               1361                 :                : 
                               1362                 :                :         /* Tablespaces should not share a directory. */
  626 rhaas@postgresql.org     1363         [ -  + ]:CBC           1 :         for (otherts = tslist; otherts != NULL; otherts = otherts->next)
  626 rhaas@postgresql.org     1364         [ #  # ]:UBC           0 :             if (strcmp(ts->new_dir, otherts->new_dir) == 0)
  442 peter@eisentraut.org     1365                 :              0 :                 pg_fatal("tablespaces with OIDs %u and %u both point at directory \"%s\"",
                               1366                 :                :                          otherts->oid, oid, ts->new_dir);
                               1367                 :                : 
                               1368                 :                :         /* Add this tablespace to the list. */
  626 rhaas@postgresql.org     1369                 :CBC           1 :         ts->next = tslist;
                               1370                 :              1 :         tslist = ts;
                               1371                 :                :     }
                               1372                 :                : 
  604                          1373         [ -  + ]:             13 :     if (closedir(dir) != 0)
  604 rhaas@postgresql.org     1374                 :UBC           0 :         pg_fatal("could not close directory \"%s\": %m", pg_tblspc);
                               1375                 :                : 
  626 rhaas@postgresql.org     1376                 :CBC          13 :     return tslist;
                               1377                 :                : }
                               1378                 :                : 
                               1379                 :                : /*
                               1380                 :                :  * Read a file into a StringInfo.
                               1381                 :                :  *
                               1382                 :                :  * fd is used for the actual file I/O, filename for error reporting purposes.
                               1383                 :                :  * A file longer than maxlen is a fatal error.
                               1384                 :                :  */
                               1385                 :                : static void
                               1386                 :             57 : slurp_file(int fd, char *filename, StringInfo buf, int maxlen)
                               1387                 :                : {
                               1388                 :                :     struct stat st;
                               1389                 :                :     ssize_t     rb;
                               1390                 :                : 
                               1391                 :                :     /* Check file size, and complain if it's too large. */
                               1392         [ -  + ]:             57 :     if (fstat(fd, &st) != 0)
  442 peter@eisentraut.org     1393                 :UBC           0 :         pg_fatal("could not stat file \"%s\": %m", filename);
  626 rhaas@postgresql.org     1394         [ -  + ]:CBC          57 :     if (st.st_size > maxlen)
  626 rhaas@postgresql.org     1395                 :UBC           0 :         pg_fatal("file \"%s\" is too large", filename);
                               1396                 :                : 
                               1397                 :                :     /* Make sure we have enough space. */
  626 rhaas@postgresql.org     1398                 :CBC          57 :     enlargeStringInfo(buf, st.st_size);
                               1399                 :                : 
                               1400                 :                :     /* Read the data. */
                               1401                 :             57 :     rb = read(fd, &buf->data[buf->len], st.st_size);
                               1402                 :                : 
                               1403                 :                :     /*
                               1404                 :                :      * We don't expect any concurrent changes, so we should read exactly the
                               1405                 :                :      * expected number of bytes.
                               1406                 :                :      */
                               1407         [ -  + ]:             57 :     if (rb != st.st_size)
                               1408                 :                :     {
  626 rhaas@postgresql.org     1409         [ #  # ]:UBC           0 :         if (rb < 0)
                               1410                 :              0 :             pg_fatal("could not read file \"%s\": %m", filename);
                               1411                 :                :         else
  442 peter@eisentraut.org     1412                 :              0 :             pg_fatal("could not read file \"%s\": read %zd of %lld",
                               1413                 :                :                      filename, rb, (long long int) st.st_size);
                               1414                 :                :     }
                               1415                 :                : 
                               1416                 :                :     /* Adjust buffer length for new data and restore trailing-\0 invariant */
  626 rhaas@postgresql.org     1417                 :CBC          57 :     buf->len += rb;
                               1418                 :             57 :     buf->data[buf->len] = '\0';
                               1419                 :             57 : }
        

Generated by: LCOV version 2.4-beta