LCOV - differential code coverage report
Current view: top level - src/bin/pg_verifybackup - pg_verifybackup.c (source / functions) Coverage Total Hit UNC UBC GBC GNC CBC DUB DCB
Current: bed3ffbf9d952be6c7d739d068cdce44c046dfb7 vs 574581b50ac9c63dd9e4abebb731a3b67e5b50f6 Lines: 90.5 % 482 436 46 34 402 4 26
Current Date: 2026-05-05 10:23:31 +0900 Functions: 100.0 % 23 23 8 15 1
Baseline: lcov-20260505-025707-baseline Branches: 80.2 % 278 223 1 54 1 13 209 3 15
Baseline Date: 2026-05-05 10:27:06 +0900 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 100.0 % 36 36 34 2
(360..) days: 89.7 % 446 400 46 400
Function coverage date bins:
(30,360] days: 100.0 % 3 3 1 2
(360..) days: 100.0 % 20 20 7 13
Branch coverage date bins:
(30,360] days: 92.9 % 14 13 1 13
(360..) days: 79.5 % 264 210 54 1 209

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * pg_verifybackup.c
                                  4                 :                :  *    Verify a backup against a backup manifest.
                                  5                 :                :  *
                                  6                 :                :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
                                  7                 :                :  * Portions Copyright (c) 1994, Regents of the University of California
                                  8                 :                :  *
                                  9                 :                :  * src/bin/pg_verifybackup/pg_verifybackup.c
                                 10                 :                :  *
                                 11                 :                :  *-------------------------------------------------------------------------
                                 12                 :                :  */
                                 13                 :                : 
                                 14                 :                : #include "postgres_fe.h"
                                 15                 :                : 
                                 16                 :                : #include <dirent.h>
                                 17                 :                : #include <fcntl.h>
                                 18                 :                : #include <limits.h>
                                 19                 :                : #include <sys/stat.h>
                                 20                 :                : #include <time.h>
                                 21                 :                : 
                                 22                 :                : #include "access/xlog_internal.h"
                                 23                 :                : #include "common/logging.h"
                                 24                 :                : #include "common/parse_manifest.h"
                                 25                 :                : #include "fe_utils/simple_list.h"
                                 26                 :                : #include "getopt_long.h"
                                 27                 :                : #include "pg_verifybackup.h"
                                 28                 :                : #include "pgtime.h"
                                 29                 :                : 
                                 30                 :                : /*
                                 31                 :                :  * For efficiency, we'd like our hash table containing information about the
                                 32                 :                :  * manifest to start out with approximately the correct number of entries.
                                 33                 :                :  * There's no way to know the exact number of entries without reading the whole
                                 34                 :                :  * file, but we can get an estimate by dividing the file size by the estimated
                                 35                 :                :  * number of bytes per line.
                                 36                 :                :  *
                                 37                 :                :  * This could be off by about a factor of two in either direction, because the
                                 38                 :                :  * checksum algorithm has a big impact on the line lengths; e.g. a SHA512
                                 39                 :                :  * checksum is 128 hex bytes, whereas a CRC-32C value is only 8, and there
                                 40                 :                :  * might be no checksum at all.
                                 41                 :                :  */
                                 42                 :                : #define ESTIMATED_BYTES_PER_MANIFEST_LINE   100
                                 43                 :                : 
                                 44                 :                : /*
                                 45                 :                :  * How many bytes should we try to read from a file at once?
                                 46                 :                :  */
                                 47                 :                : #define READ_CHUNK_SIZE             (128 * 1024)
                                 48                 :                : 
                                 49                 :                : /*
                                 50                 :                :  * Tar file information needed for content verification.
                                 51                 :                :  */
                                 52                 :                : typedef struct tar_file
                                 53                 :                : {
                                 54                 :                :     char       *relpath;
                                 55                 :                :     Oid         tblspc_oid;
                                 56                 :                :     pg_compress_algorithm compress_algorithm;
                                 57                 :                : } tar_file;
                                 58                 :                : 
                                 59                 :                : static manifest_data *parse_manifest_file(char *manifest_path);
                                 60                 :                : static void verifybackup_version_cb(JsonManifestParseContext *context,
                                 61                 :                :                                     int manifest_version);
                                 62                 :                : static void verifybackup_system_identifier(JsonManifestParseContext *context,
                                 63                 :                :                                            uint64 manifest_system_identifier);
                                 64                 :                : static void verifybackup_per_file_cb(JsonManifestParseContext *context,
                                 65                 :                :                                      const char *pathname, uint64 size,
                                 66                 :                :                                      pg_checksum_type checksum_type,
                                 67                 :                :                                      int checksum_length,
                                 68                 :                :                                      uint8 *checksum_payload);
                                 69                 :                : static void verifybackup_per_wal_range_cb(JsonManifestParseContext *context,
                                 70                 :                :                                           TimeLineID tli,
                                 71                 :                :                                           XLogRecPtr start_lsn,
                                 72                 :                :                                           XLogRecPtr end_lsn);
                                 73                 :                : pg_noreturn static void report_manifest_error(JsonManifestParseContext *context,
                                 74                 :                :                                               const char *fmt,...)
                                 75                 :                :             pg_attribute_printf(2, 3);
                                 76                 :                : 
                                 77                 :                : static void verify_tar_backup(verifier_context *context, DIR *dir,
                                 78                 :                :                               char **base_archive_path,
                                 79                 :                :                               char **wal_archive_path);
                                 80                 :                : static void verify_plain_backup_directory(verifier_context *context,
                                 81                 :                :                                           char *relpath, char *fullpath,
                                 82                 :                :                                           DIR *dir);
                                 83                 :                : static void verify_plain_backup_file(verifier_context *context, char *relpath,
                                 84                 :                :                                      char *fullpath);
                                 85                 :                : static void verify_control_file(const char *controlpath,
                                 86                 :                :                                 uint64 manifest_system_identifier);
                                 87                 :                : static void precheck_tar_backup_file(verifier_context *context, char *relpath,
                                 88                 :                :                                      char *fullpath, SimplePtrList *tarfiles,
                                 89                 :                :                                      char **base_archive_path,
                                 90                 :                :                                      char **wal_archive_path);
                                 91                 :                : static void verify_tar_file(verifier_context *context, char *relpath,
                                 92                 :                :                             char *fullpath, astreamer *streamer);
                                 93                 :                : static void report_extra_backup_files(verifier_context *context);
                                 94                 :                : static void verify_backup_checksums(verifier_context *context);
                                 95                 :                : static void verify_file_checksum(verifier_context *context,
                                 96                 :                :                                  manifest_file *m, char *fullpath,
                                 97                 :                :                                  uint8 *buffer);
                                 98                 :                : static void parse_required_wal(verifier_context *context,
                                 99                 :                :                                char *pg_waldump_path,
                                100                 :                :                                char *wal_path);
                                101                 :                : static astreamer *create_archive_verifier(verifier_context *context,
                                102                 :                :                                           char *archive_name,
                                103                 :                :                                           Oid tblspc_oid,
                                104                 :                :                                           pg_compress_algorithm compress_algo);
                                105                 :                : 
                                106                 :                : static void progress_report(bool finished);
                                107                 :                : static void usage(void);
                                108                 :                : 
                                109                 :                : static const char *progname;
                                110                 :                : 
                                111                 :                : /* is progress reporting enabled? */
                                112                 :                : static bool show_progress = false;
                                113                 :                : 
                                114                 :                : /* Progress indicators */
                                115                 :                : static uint64 total_size = 0;
                                116                 :                : static uint64 done_size = 0;
                                117                 :                : 
                                118                 :                : /*
                                119                 :                :  * Main entry point.
                                120                 :                :  */
                                121                 :                : int
 2223 rhaas@postgresql.org      122                 :CBC         129 : main(int argc, char **argv)
                                123                 :                : {
                                124                 :                :     static struct option long_options[] = {
                                125                 :                :         {"exit-on-error", no_argument, NULL, 'e'},
                                126                 :                :         {"ignore", required_argument, NULL, 'i'},
                                127                 :                :         {"manifest-path", required_argument, NULL, 'm'},
                                128                 :                :         {"format", required_argument, NULL, 'F'},
                                129                 :                :         {"no-parse-wal", no_argument, NULL, 'n'},
                                130                 :                :         {"progress", no_argument, NULL, 'P'},
                                131                 :                :         {"quiet", no_argument, NULL, 'q'},
                                132                 :                :         {"skip-checksums", no_argument, NULL, 's'},
                                133                 :                :         {"wal-path", required_argument, NULL, 'w'},
                                134                 :                :         {"wal-directory", required_argument, NULL, 'w'},  /* deprecated */
                                135                 :                :         {NULL, 0, NULL, 0}
                                136                 :                :     };
                                137                 :                : 
                                138                 :                :     int         c;
                                139                 :                :     verifier_context context;
                                140                 :            129 :     char       *manifest_path = NULL;
                                141                 :            129 :     bool        no_parse_wal = false;
                                142                 :            129 :     bool        quiet = false;
   46 andrew@dunslane.net       143                 :GNC         129 :     char       *wal_path = NULL;
                                144                 :            129 :     char       *base_archive_path = NULL;
                                145                 :            129 :     char       *wal_archive_path = NULL;
 2223 rhaas@postgresql.org      146                 :CBC         129 :     char       *pg_waldump_path = NULL;
                                147                 :                :     DIR        *dir;
                                148                 :                : 
                                149                 :            129 :     pg_logging_init(argv[0]);
 2214                           150                 :            129 :     set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_verifybackup"));
 2223                           151                 :            129 :     progname = get_progname(argv[0]);
                                152                 :                : 
                                153                 :            129 :     memset(&context, 0, sizeof(context));
                                154                 :                : 
                                155         [ +  + ]:            129 :     if (argc > 1)
                                156                 :                :     {
                                157   [ +  +  -  + ]:            128 :         if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
                                158                 :                :         {
                                159                 :              1 :             usage();
                                160                 :              1 :             exit(0);
                                161                 :                :         }
                                162   [ +  +  -  + ]:            127 :         if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
                                163                 :                :         {
 2214                           164                 :              1 :             puts("pg_verifybackup (PostgreSQL) " PG_VERSION);
 2223                           165                 :              1 :             exit(0);
                                166                 :                :         }
                                167                 :                :     }
                                168                 :                : 
                                169                 :                :     /*
                                170                 :                :      * Skip certain files in the toplevel directory.
                                171                 :                :      *
                                172                 :                :      * Ignore the backup_manifest file, because it's not included in the
                                173                 :                :      * backup manifest.
                                174                 :                :      *
                                175                 :                :      * Ignore the pg_wal directory, because those files are not included in
                                176                 :                :      * the backup manifest either, since they are fetched separately from the
                                177                 :                :      * backup itself, and verified via a separate mechanism.
                                178                 :                :      *
                                179                 :                :      * Ignore postgresql.auto.conf, recovery.signal, and standby.signal,
                                180                 :                :      * because we expect that those files may sometimes be created or changed
                                181                 :                :      * as part of the backup process. For example, pg_basebackup -R will
                                182                 :                :      * modify postgresql.auto.conf and create standby.signal.
                                183                 :                :      */
                                184                 :            127 :     simple_string_list_append(&context.ignore_list, "backup_manifest");
                                185                 :            127 :     simple_string_list_append(&context.ignore_list, "pg_wal");
                                186                 :            127 :     simple_string_list_append(&context.ignore_list, "postgresql.auto.conf");
                                187                 :            127 :     simple_string_list_append(&context.ignore_list, "recovery.signal");
                                188                 :            127 :     simple_string_list_append(&context.ignore_list, "standby.signal");
                                189                 :                : 
  585                           190         [ +  + ]:            182 :     while ((c = getopt_long(argc, argv, "eF:i:m:nPqsw:", long_options, NULL)) != -1)
                                191                 :                :     {
 2223                           192   [ +  +  +  +  :             57 :         switch (c)
                                     +  +  +  +  +  
                                                 + ]
                                193                 :                :         {
                                194                 :             33 :             case 'e':
                                195                 :             33 :                 context.exit_on_error = true;
                                196                 :             33 :                 break;
                                197                 :              4 :             case 'i':
                                198                 :                :                 {
                                199                 :              4 :                     char       *arg = pstrdup(optarg);
                                200                 :                : 
                                201                 :              4 :                     canonicalize_path(arg);
                                202                 :              4 :                     simple_string_list_append(&context.ignore_list, arg);
                                203                 :              4 :                     break;
                                204                 :                :                 }
                                205                 :              4 :             case 'm':
                                206                 :              4 :                 manifest_path = pstrdup(optarg);
                                207                 :              4 :                 canonicalize_path(manifest_path);
                                208                 :              4 :                 break;
  585                           209                 :              3 :             case 'F':
                                210   [ +  -  +  + ]:              3 :                 if (strcmp(optarg, "p") == 0 || strcmp(optarg, "plain") == 0)
                                211                 :              1 :                     context.format = 'p';
                                212   [ +  -  +  + ]:              2 :                 else if (strcmp(optarg, "t") == 0 || strcmp(optarg, "tar") == 0)
                                213                 :              1 :                     context.format = 't';
                                214                 :                :                 else
                                215                 :              1 :                     pg_fatal("invalid backup format \"%s\", must be \"plain\" or \"tar\"",
                                216                 :                :                              optarg);
                                217                 :              2 :                 break;
 2223                           218                 :              4 :             case 'n':
                                219                 :              4 :                 no_parse_wal = true;
                                220                 :              4 :                 break;
 1184 michael@paquier.xyz       221                 :              2 :             case 'P':
                                222                 :              2 :                 show_progress = true;
                                223                 :              2 :                 break;
 2223 rhaas@postgresql.org      224                 :              3 :             case 'q':
                                225                 :              3 :                 quiet = true;
                                226                 :              3 :                 break;
                                227                 :              2 :             case 's':
  627                           228                 :              2 :                 context.skip_checksums = true;
 2223                           229                 :              2 :                 break;
                                230                 :              1 :             case 'w':
   46 andrew@dunslane.net       231                 :GNC           1 :                 wal_path = pstrdup(optarg);
                                232                 :              1 :                 canonicalize_path(wal_path);
 2223 rhaas@postgresql.org      233                 :CBC           1 :                 break;
                                234                 :              1 :             default:
                                235                 :                :                 /* getopt_long already emitted a complaint */
 1488 tgl@sss.pgh.pa.us         236                 :              1 :                 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
 2223 rhaas@postgresql.org      237                 :              1 :                 exit(1);
                                238                 :                :         }
                                239                 :                :     }
                                240                 :                : 
                                241                 :                :     /* Get backup directory name */
                                242         [ +  + ]:            125 :     if (optind >= argc)
                                243                 :                :     {
 1488 tgl@sss.pgh.pa.us         244                 :              1 :         pg_log_error("no backup directory specified");
                                245                 :              1 :         pg_log_error_hint("Try \"%s --help\" for more information.", progname);
 2223 rhaas@postgresql.org      246                 :              1 :         exit(1);
                                247                 :                :     }
                                248                 :            124 :     context.backup_directory = pstrdup(argv[optind++]);
                                249                 :            124 :     canonicalize_path(context.backup_directory);
                                250                 :                : 
                                251                 :                :     /* Complain if any arguments remain */
                                252         [ +  + ]:            124 :     if (optind < argc)
                                253                 :                :     {
 1488 tgl@sss.pgh.pa.us         254                 :              1 :         pg_log_error("too many command-line arguments (first is \"%s\")",
                                255                 :                :                      argv[optind]);
                                256                 :              1 :         pg_log_error_hint("Try \"%s --help\" for more information.", progname);
 2223 rhaas@postgresql.org      257                 :              1 :         exit(1);
                                258                 :                :     }
                                259                 :                : 
                                260                 :                :     /* Complain if the specified arguments conflict */
 1184 michael@paquier.xyz       261   [ +  +  +  + ]:            123 :     if (show_progress && quiet)
                                262                 :              1 :         pg_fatal("cannot specify both %s and %s",
                                263                 :                :                  "-P/--progress", "-q/--quiet");
                                264                 :                : 
                                265                 :                :     /* Unless --no-parse-wal was specified, we will need pg_waldump. */
 2223 rhaas@postgresql.org      266         [ +  + ]:            122 :     if (!no_parse_wal)
                                267                 :                :     {
                                268                 :                :         int         ret;
                                269                 :                : 
                                270                 :            118 :         pg_waldump_path = pg_malloc(MAXPGPATH);
                                271                 :            118 :         ret = find_other_exec(argv[0], "pg_waldump",
                                272                 :                :                               "pg_waldump (PostgreSQL) " PG_VERSION "\n",
                                273                 :                :                               pg_waldump_path);
                                274         [ -  + ]:            118 :         if (ret < 0)
                                275                 :                :         {
                                276                 :                :             char        full_path[MAXPGPATH];
                                277                 :                : 
 2223 rhaas@postgresql.org      278         [ #  # ]:UBC           0 :             if (find_my_exec(argv[0], full_path) < 0)
                                279                 :              0 :                 strlcpy(full_path, progname, sizeof(full_path));
                                280                 :                : 
                                281         [ #  # ]:              0 :             if (ret == -1)
 1488 tgl@sss.pgh.pa.us         282                 :              0 :                 pg_fatal("program \"%s\" is needed by %s but was not found in the same directory as \"%s\"",
                                283                 :                :                          "pg_waldump", "pg_verifybackup", full_path);
                                284                 :                :             else
                                285                 :              0 :                 pg_fatal("program \"%s\" was found by \"%s\" but was not the same version as %s",
                                286                 :                :                          "pg_waldump", full_path, "pg_verifybackup");
                                287                 :                :         }
                                288                 :                :     }
                                289                 :                : 
                                290                 :                :     /* By default, look for the manifest in the backup directory. */
 2223 rhaas@postgresql.org      291         [ +  + ]:CBC         122 :     if (manifest_path == NULL)
                                292                 :            118 :         manifest_path = psprintf("%s/backup_manifest",
                                293                 :                :                                  context.backup_directory);
                                294                 :                : 
                                295                 :                :     /*
                                296                 :                :      * Try to read the manifest. We treat any errors encountered while parsing
                                297                 :                :      * the manifest as fatal; there doesn't seem to be much point in trying to
                                298                 :                :      * verify the backup directory against a corrupted manifest.
                                299                 :                :      */
  792                           300                 :            122 :     context.manifest = parse_manifest_file(manifest_path);
                                301                 :                : 
                                302                 :                :     /*
                                303                 :                :      * If the backup directory cannot be found, treat this as a fatal error.
                                304                 :                :      */
  585                           305                 :             87 :     dir = opendir(context.backup_directory);
                                306         [ +  + ]:             87 :     if (dir == NULL)
                                307                 :              1 :         report_fatal_error("could not open directory \"%s\": %m",
                                308                 :                :                            context.backup_directory);
                                309                 :                : 
                                310                 :                :     /*
                                311                 :                :      * At this point, we know that the backup directory exists, so it's now
                                312                 :                :      * reasonable to check for files immediately inside it. Thus, before going
                                313                 :                :      * further, if the user did not specify the backup format, check for
                                314                 :                :      * PG_VERSION to distinguish between tar and plain format.
                                315                 :                :      */
                                316         [ +  + ]:             86 :     if (context.format == '\0')
                                317                 :                :     {
                                318                 :                :         struct stat sb;
                                319                 :                :         char       *path;
                                320                 :                : 
                                321                 :             84 :         path = psprintf("%s/%s", context.backup_directory, "PG_VERSION");
                                322         [ +  + ]:             84 :         if (stat(path, &sb) == 0)
                                323                 :             56 :             context.format = 'p';
                                324         [ -  + ]:             28 :         else if (errno != ENOENT)
                                325                 :                :         {
  585 rhaas@postgresql.org      326                 :UBC           0 :             pg_log_error("could not stat file \"%s\": %m", path);
                                327                 :              0 :             exit(1);
                                328                 :                :         }
                                329                 :                :         else
                                330                 :                :         {
                                331                 :                :             /* No PG_VERSION, so assume tar format. */
  585 rhaas@postgresql.org      332                 :CBC          28 :             context.format = 't';
                                333                 :                :         }
                                334                 :             84 :         pfree(path);
                                335                 :                :     }
                                336                 :                : 
                                337                 :                :     /*
                                338                 :                :      * Perform the appropriate type of verification appropriate based on the
                                339                 :                :      * backup format. This will close 'dir'.
                                340                 :                :      */
                                341         [ +  + ]:             86 :     if (context.format == 'p')
                                342                 :             57 :         verify_plain_backup_directory(&context, NULL, context.backup_directory,
                                343                 :                :                                       dir);
                                344                 :                :     else
   46 andrew@dunslane.net       345                 :GNC          29 :         verify_tar_backup(&context, dir, &base_archive_path, &wal_archive_path);
                                346                 :                : 
                                347                 :                :     /*
                                348                 :                :      * The "matched" flag should now be set on every entry in the hash table.
                                349                 :                :      * Any entries for which the bit is not set are files mentioned in the
                                350                 :                :      * manifest that don't exist on disk (or in the relevant tar files).
                                351                 :                :      */
 2223 rhaas@postgresql.org      352                 :CBC          84 :     report_extra_backup_files(&context);
                                353                 :                : 
                                354                 :                :     /*
                                355                 :                :      * If this is a tar-format backup, checksums were already verified above;
                                356                 :                :      * but if it's a plain-format backup, we postpone it until this point,
                                357                 :                :      * since the earlier checks can be performed just by knowing which files
                                358                 :                :      * are present, without needing to read all of them.
                                359                 :                :      */
  585                           360   [ +  +  +  + ]:             83 :     if (context.format == 'p' && !context.skip_checksums)
 2214                           361                 :             53 :         verify_backup_checksums(&context);
                                362                 :                : 
                                363                 :                :     /*
                                364                 :                :      * By default, WAL files are expected to be found in the backup directory
                                365                 :                :      * for plain-format backups. In the case of tar-format backups, if a
                                366                 :                :      * separate WAL archive is not found, the WAL files are most likely
                                367                 :                :      * included within the main data directory archive.
                                368                 :                :      */
   46 andrew@dunslane.net       369         [ +  + ]:GNC          83 :     if (wal_path == NULL)
                                370                 :                :     {
                                371         [ +  + ]:             82 :         if (context.format == 'p')
                                372                 :             54 :             wal_path = psprintf("%s/pg_wal", context.backup_directory);
                                373         [ +  + ]:             28 :         else if (wal_archive_path)
                                374                 :             14 :             wal_path = wal_archive_path;
                                375         [ +  + ]:             14 :         else if (base_archive_path)
                                376                 :             13 :             wal_path = base_archive_path;
                                377                 :                :         else
                                378                 :                :         {
                                379                 :              1 :             pg_log_error("WAL archive not found");
                                380                 :              1 :             pg_log_error_hint("Specify the correct path using the option -w/--wal-path.  "
                                381                 :                :                               "Or you must use -n/--no-parse-wal when verifying a tar-format backup.");
                                382                 :              1 :             exit(1);
                                383                 :                :         }
                                384                 :                :     }
                                385                 :                : 
                                386                 :                :     /*
                                387                 :                :      * Try to parse the required ranges of WAL records, unless we were told
                                388                 :                :      * not to do so.
                                389                 :                :      */
 2223 rhaas@postgresql.org      390         [ +  + ]:CBC          82 :     if (!no_parse_wal)
   46 andrew@dunslane.net       391                 :GNC          79 :         parse_required_wal(&context, pg_waldump_path, wal_path);
                                392                 :                : 
                                393                 :                :     /*
                                394                 :                :      * If everything looks OK, tell the user this, unless we were asked to
                                395                 :                :      * work quietly.
                                396                 :                :      */
 2223 rhaas@postgresql.org      397   [ +  +  +  + ]:CBC          82 :     if (!context.saw_any_error && !quiet)
 2194 peter@eisentraut.org      398                 :             59 :         printf(_("backup successfully verified\n"));
                                399                 :                : 
 2223 rhaas@postgresql.org      400                 :             82 :     return context.saw_any_error ? 1 : 0;
                                401                 :                : }
                                402                 :                : 
                                403                 :                : /*
                                404                 :                :  * Parse a manifest file and return a data structure describing the contents.
                                405                 :                :  */
                                406                 :                : static manifest_data *
  792                           407                 :            122 : parse_manifest_file(char *manifest_path)
                                408                 :                : {
                                409                 :                :     int         fd;
                                410                 :                :     struct stat statbuf;
                                411                 :                :     off_t       estimate;
                                412                 :                :     uint32      initial_size;
                                413                 :                :     manifest_files_hash *ht;
                                414                 :                :     char       *buffer;
                                415                 :                :     int         rc;
                                416                 :                :     JsonManifestParseContext context;
                                417                 :                :     manifest_data *result;
                                418                 :                : 
  785 andrew@dunslane.net       419                 :            122 :     int         chunk_size = READ_CHUNK_SIZE;
                                420                 :                : 
                                421                 :                :     /* Open the manifest file. */
 2223 rhaas@postgresql.org      422         [ +  + ]:            122 :     if ((fd = open(manifest_path, O_RDONLY | PG_BINARY, 0)) < 0)
                                423                 :              3 :         report_fatal_error("could not open file \"%s\": %m", manifest_path);
                                424                 :                : 
                                425                 :                :     /* Figure out how big the manifest is. */
                                426         [ -  + ]:            119 :     if (fstat(fd, &statbuf) != 0)
 2223 rhaas@postgresql.org      427                 :UBC           0 :         report_fatal_error("could not stat file \"%s\": %m", manifest_path);
                                428                 :                : 
                                429                 :                :     /* Guess how large to make the hash table based on the manifest size. */
 2223 rhaas@postgresql.org      430                 :CBC         119 :     estimate = statbuf.st_size / ESTIMATED_BYTES_PER_MANIFEST_LINE;
                                431         [ +  - ]:            119 :     initial_size = Min(PG_UINT32_MAX, Max(estimate, 256));
                                432                 :                : 
                                433                 :                :     /* Create the hash table. */
                                434                 :            119 :     ht = manifest_files_create(initial_size, NULL);
                                435                 :                : 
   67 michael@paquier.xyz       436                 :GNC         119 :     result = pg_malloc0_object(manifest_data);
  792 rhaas@postgresql.org      437                 :CBC         119 :     result->files = ht;
                                438                 :            119 :     context.private_data = result;
  783                           439                 :            119 :     context.version_cb = verifybackup_version_cb;
                                440                 :            119 :     context.system_identifier_cb = verifybackup_system_identifier;
  882                           441                 :            119 :     context.per_file_cb = verifybackup_per_file_cb;
                                442                 :            119 :     context.per_wal_range_cb = verifybackup_per_wal_range_cb;
 2223                           443                 :            119 :     context.error_cb = report_manifest_error;
                                444                 :                : 
                                445                 :                :     /*
                                446                 :                :      * Parse the file, in chunks if necessary.
                                447                 :                :      */
  785 andrew@dunslane.net       448         [ +  + ]:            119 :     if (statbuf.st_size <= chunk_size)
                                449                 :                :     {
                                450                 :             33 :         buffer = pg_malloc(statbuf.st_size);
                                451                 :             33 :         rc = read(fd, buffer, statbuf.st_size);
                                452         [ -  + ]:             33 :         if (rc != statbuf.st_size)
                                453                 :                :         {
  785 andrew@dunslane.net       454         [ #  # ]:UBC           0 :             if (rc < 0)
                                455                 :              0 :                 pg_fatal("could not read file \"%s\": %m", manifest_path);
                                456                 :                :             else
                                457                 :              0 :                 pg_fatal("could not read file \"%s\": read %d of %lld",
                                458                 :                :                          manifest_path, rc, (long long int) statbuf.st_size);
                                459                 :                :         }
                                460                 :                : 
                                461                 :                :         /* Close the manifest file. */
  785 andrew@dunslane.net       462                 :CBC          33 :         close(fd);
                                463                 :                : 
                                464                 :                :         /* Parse the manifest. */
                                465                 :             33 :         json_parse_manifest(&context, buffer, statbuf.st_size);
                                466                 :                :     }
                                467                 :                :     else
                                468                 :                :     {
                                469                 :             86 :         int         bytes_left = statbuf.st_size;
                                470                 :                :         JsonManifestParseIncrementalState *inc_state;
                                471                 :                : 
                                472                 :             86 :         inc_state = json_parse_manifest_incremental_init(&context);
                                473                 :                : 
                                474                 :             86 :         buffer = pg_malloc(chunk_size + 1);
                                475                 :                : 
                                476         [ +  + ]:            258 :         while (bytes_left > 0)
                                477                 :                :         {
                                478                 :            174 :             int         bytes_to_read = chunk_size;
                                479                 :                : 
                                480                 :                :             /*
                                481                 :                :              * Make sure that the last chunk is sufficiently large. (i.e. at
                                482                 :                :              * least half the chunk size) so that it will contain fully the
                                483                 :                :              * piece at the end with the checksum.
                                484                 :                :              */
                                485         [ +  + ]:            174 :             if (bytes_left < chunk_size)
                                486                 :             86 :                 bytes_to_read = bytes_left;
                                487         [ +  + ]:             88 :             else if (bytes_left < 2 * chunk_size)
                                488                 :             86 :                 bytes_to_read = bytes_left / 2;
                                489                 :            174 :             rc = read(fd, buffer, bytes_to_read);
                                490         [ -  + ]:            174 :             if (rc != bytes_to_read)
                                491                 :                :             {
  785 andrew@dunslane.net       492         [ #  # ]:UBC           0 :                 if (rc < 0)
                                493                 :              0 :                     pg_fatal("could not read file \"%s\": %m", manifest_path);
                                494                 :                :                 else
                                495                 :              0 :                     pg_fatal("could not read file \"%s\": read %lld of %lld",
                                496                 :                :                              manifest_path,
                                497                 :                :                              (long long int) (statbuf.st_size + rc - bytes_left),
                                498                 :                :                              (long long int) statbuf.st_size);
                                499                 :                :             }
  785 andrew@dunslane.net       500                 :CBC         174 :             bytes_left -= rc;
  753                           501                 :            174 :             json_parse_manifest_incremental_chunk(inc_state, buffer, rc,
                                502                 :                :                                                   bytes_left == 0);
                                503                 :                :         }
                                504                 :                : 
                                505                 :                :         /* Release the incremental state memory */
  756                           506                 :             84 :         json_parse_manifest_incremental_shutdown(inc_state);
                                507                 :                : 
  785                           508                 :             84 :         close(fd);
                                509                 :                :     }
                                510                 :                : 
                                511                 :                :     /* Done with the buffer. */
 2223 rhaas@postgresql.org      512                 :             87 :     pfree(buffer);
                                513                 :                : 
  792                           514                 :             87 :     return result;
                                515                 :                : }
                                516                 :                : 
                                517                 :                : /*
                                518                 :                :  * Report an error while parsing the manifest.
                                519                 :                :  *
                                520                 :                :  * We consider all such errors to be fatal errors. The manifest parser
                                521                 :                :  * expects this function not to return.
                                522                 :                :  */
                                523                 :                : static void
 2194 peter@eisentraut.org      524                 :             31 : report_manifest_error(JsonManifestParseContext *context, const char *fmt,...)
                                525                 :                : {
                                526                 :                :     va_list     ap;
                                527                 :                : 
 2223 rhaas@postgresql.org      528                 :             31 :     va_start(ap, fmt);
 1488 tgl@sss.pgh.pa.us         529                 :             31 :     pg_log_generic_v(PG_LOG_ERROR, PG_LOG_PRIMARY, gettext(fmt), ap);
 2223 rhaas@postgresql.org      530                 :             31 :     va_end(ap);
                                531                 :                : 
                                532                 :             31 :     exit(1);
                                533                 :                : }
                                534                 :                : 
                                535                 :                : /*
                                536                 :                :  * Record details extracted from the backup manifest.
                                537                 :                :  */
                                538                 :                : static void
  783                           539                 :            113 : verifybackup_version_cb(JsonManifestParseContext *context,
                                540                 :                :                         int manifest_version)
                                541                 :                : {
                                542                 :            113 :     manifest_data *manifest = context->private_data;
                                543                 :                : 
                                544                 :                :     /* Validation will be at the later stage */
                                545                 :            113 :     manifest->version = manifest_version;
                                546                 :            113 : }
                                547                 :                : 
                                548                 :                : /*
                                549                 :                :  * Record details extracted from the backup manifest.
                                550                 :                :  */
                                551                 :                : static void
                                552                 :             89 : verifybackup_system_identifier(JsonManifestParseContext *context,
                                553                 :                :                                uint64 manifest_system_identifier)
                                554                 :                : {
                                555                 :             89 :     manifest_data *manifest = context->private_data;
                                556                 :                : 
                                557                 :                :     /* Validation will be at the later stage */
                                558                 :             89 :     manifest->system_identifier = manifest_system_identifier;
                                559                 :             89 : }
                                560                 :                : 
                                561                 :                : /*
                                562                 :                :  * Record details extracted from the backup manifest for one file.
                                563                 :                :  */
                                564                 :                : static void
  882                           565                 :          91718 : verifybackup_per_file_cb(JsonManifestParseContext *context,
                                566                 :                :                          const char *pathname, uint64 size,
                                567                 :                :                          pg_checksum_type checksum_type,
                                568                 :                :                          int checksum_length, uint8 *checksum_payload)
                                569                 :                : {
  792                           570                 :          91718 :     manifest_data *manifest = context->private_data;
                                571                 :          91718 :     manifest_files_hash *ht = manifest->files;
                                572                 :                :     manifest_file *m;
                                573                 :                :     bool        found;
                                574                 :                : 
                                575                 :                :     /* Make a new entry in the hash table for this file. */
 2223                           576                 :          91718 :     m = manifest_files_insert(ht, pathname, &found);
                                577         [ +  + ]:          91718 :     if (found)
 2059 peter@eisentraut.org      578                 :              1 :         report_fatal_error("duplicate path name in backup manifest: \"%s\"",
                                579                 :                :                            pathname);
                                580                 :                : 
                                581                 :                :     /* Initialize the entry. */
 2223 rhaas@postgresql.org      582                 :          91717 :     m->size = size;
                                583                 :          91717 :     m->checksum_type = checksum_type;
                                584                 :          91717 :     m->checksum_length = checksum_length;
                                585                 :          91717 :     m->checksum_payload = checksum_payload;
                                586                 :          91717 :     m->matched = false;
                                587                 :          91717 :     m->bad = false;
                                588                 :          91717 : }
                                589                 :                : 
                                590                 :                : /*
                                591                 :                :  * Record details extracted from the backup manifest for one WAL range.
                                592                 :                :  */
                                593                 :                : static void
  882                           594                 :             90 : verifybackup_per_wal_range_cb(JsonManifestParseContext *context,
                                595                 :                :                               TimeLineID tli,
                                596                 :                :                               XLogRecPtr start_lsn, XLogRecPtr end_lsn)
                                597                 :                : {
  792                           598                 :             90 :     manifest_data *manifest = context->private_data;
                                599                 :                :     manifest_wal_range *range;
                                600                 :                : 
                                601                 :                :     /* Allocate and initialize a struct describing this WAL range. */
  147 michael@paquier.xyz       602                 :GNC          90 :     range = palloc_object(manifest_wal_range);
 2223 rhaas@postgresql.org      603                 :CBC          90 :     range->tli = tli;
                                604                 :             90 :     range->start_lsn = start_lsn;
                                605                 :             90 :     range->end_lsn = end_lsn;
  792                           606                 :             90 :     range->prev = manifest->last_wal_range;
 2223                           607                 :             90 :     range->next = NULL;
                                608                 :                : 
                                609                 :                :     /* Add it to the end of the list. */
  792                           610         [ +  - ]:             90 :     if (manifest->first_wal_range == NULL)
                                611                 :             90 :         manifest->first_wal_range = range;
                                612                 :                :     else
  792 rhaas@postgresql.org      613                 :UBC           0 :         manifest->last_wal_range->next = range;
  792 rhaas@postgresql.org      614                 :CBC          90 :     manifest->last_wal_range = range;
 2223                           615                 :             90 : }
                                616                 :                : 
                                617                 :                : /*
                                618                 :                :  * Verify one directory of a plain-format backup.
                                619                 :                :  *
                                620                 :                :  * 'relpath' is NULL if we are to verify the top-level backup directory,
                                621                 :                :  * and otherwise the relative path to the directory that is to be verified.
                                622                 :                :  *
                                623                 :                :  * 'fullpath' is the backup directory with 'relpath' appended; i.e. the actual
                                624                 :                :  * filesystem path at which it can be found.
                                625                 :                :  *
                                626                 :                :  * 'dir' is an open directory handle, or NULL if the caller wants us to
                                627                 :                :  * open it. If the caller chooses to pass a handle, we'll close it when
                                628                 :                :  * we're done with it.
                                629                 :                :  */
                                630                 :                : static void
  585                           631                 :           1403 : verify_plain_backup_directory(verifier_context *context, char *relpath,
                                632                 :                :                               char *fullpath, DIR *dir)
                                633                 :                : {
                                634                 :                :     struct dirent *dirent;
                                635                 :                : 
                                636                 :                :     /* Open the directory unless the caller did it. */
                                637   [ +  +  +  + ]:           1403 :     if (dir == NULL && ((dir = opendir(fullpath)) == NULL))
                                638                 :                :     {
 2223                           639                 :              1 :         report_backup_error(context,
                                640                 :                :                             "could not open directory \"%s\": %m", fullpath);
                                641                 :              1 :         simple_string_list_append(&context->ignore_list, relpath);
                                642                 :                : 
                                643                 :              1 :         return;
                                644                 :                :     }
                                645                 :                : 
                                646         [ +  + ]:          62396 :     while (errno = 0, (dirent = readdir(dir)) != NULL)
                                647                 :                :     {
                                648                 :          60996 :         char       *filename = dirent->d_name;
                                649                 :          60996 :         char       *newfullpath = psprintf("%s/%s", fullpath, filename);
                                650                 :                :         char       *newrelpath;
                                651                 :                : 
                                652                 :                :         /* Skip "." and ".." */
                                653   [ +  +  +  + ]:          60996 :         if (filename[0] == '.' && (filename[1] == '\0'
                                654         [ +  - ]:           1400 :                                    || strcmp(filename, "..") == 0))
                                655                 :           2800 :             continue;
                                656                 :                : 
                                657         [ +  + ]:          58196 :         if (relpath == NULL)
                                658                 :           1398 :             newrelpath = pstrdup(filename);
                                659                 :                :         else
                                660                 :          56798 :             newrelpath = psprintf("%s/%s", relpath, filename);
                                661                 :                : 
                                662         [ +  + ]:          58196 :         if (!should_ignore_relpath(context, newrelpath))
  585                           663                 :          58029 :             verify_plain_backup_file(context, newrelpath, newfullpath);
                                664                 :                : 
 2223                           665                 :          58194 :         pfree(newfullpath);
                                666                 :          58194 :         pfree(newrelpath);
                                667                 :                :     }
                                668                 :                : 
                                669         [ -  + ]:           1400 :     if (closedir(dir))
                                670                 :                :     {
 2223 rhaas@postgresql.org      671                 :UBC           0 :         report_backup_error(context,
                                672                 :                :                             "could not close directory \"%s\": %m", fullpath);
                                673                 :              0 :         return;
                                674                 :                :     }
                                675                 :                : }
                                676                 :                : 
                                677                 :                : /*
                                678                 :                :  * Verify one file (which might actually be a directory or a symlink).
                                679                 :                :  *
                                680                 :                :  * The arguments to this function have the same meaning as the similarly named
                                681                 :                :  * arguments to verify_plain_backup_directory.
                                682                 :                :  */
                                683                 :                : static void
  585 rhaas@postgresql.org      684                 :CBC       58029 : verify_plain_backup_file(verifier_context *context, char *relpath,
                                685                 :                :                          char *fullpath)
                                686                 :                : {
                                687                 :                :     struct stat sb;
                                688                 :                :     manifest_file *m;
                                689                 :                : 
 2223                           690         [ +  + ]:          58029 :     if (stat(fullpath, &sb) != 0)
                                691                 :                :     {
                                692                 :              3 :         report_backup_error(context,
                                693                 :                :                             "could not stat file or directory \"%s\": %m",
                                694                 :                :                             relpath);
                                695                 :                : 
                                696                 :                :         /*
                                697                 :                :          * Suppress further errors related to this path name and, if it's a
                                698                 :                :          * directory, anything underneath it.
                                699                 :                :          */
                                700                 :              3 :         simple_string_list_append(&context->ignore_list, relpath);
                                701                 :                : 
                                702                 :           1350 :         return;
                                703                 :                :     }
                                704                 :                : 
                                705                 :                :     /* If it's a directory, just recurse. */
                                706         [ +  + ]:          58026 :     if (S_ISDIR(sb.st_mode))
                                707                 :                :     {
  585                           708                 :           1346 :         verify_plain_backup_directory(context, relpath, fullpath, NULL);
 2223                           709                 :           1345 :         return;
                                710                 :                :     }
                                711                 :                : 
                                712                 :                :     /* If it's not a directory, it should be a regular file. */
                                713         [ -  + ]:          56680 :     if (!S_ISREG(sb.st_mode))
                                714                 :                :     {
 2223 rhaas@postgresql.org      715                 :UBC           0 :         report_backup_error(context,
                                716                 :                :                             "\"%s\" is not a regular file or directory",
                                717                 :                :                             relpath);
                                718                 :              0 :         return;
                                719                 :                :     }
                                720                 :                : 
                                721                 :                :     /* Check whether there's an entry in the manifest hash. */
  792 rhaas@postgresql.org      722                 :CBC       56680 :     m = manifest_files_lookup(context->manifest->files, relpath);
 2223                           723         [ +  + ]:          56680 :     if (m == NULL)
                                724                 :                :     {
                                725                 :              2 :         report_backup_error(context,
                                726                 :                :                             "\"%s\" is present on disk but not in the manifest",
                                727                 :                :                             relpath);
                                728                 :              2 :         return;
                                729                 :                :     }
                                730                 :                : 
                                731                 :                :     /* Flag this entry as having been encountered in the filesystem. */
                                732                 :          56678 :     m->matched = true;
                                733                 :                : 
                                734                 :                :     /* Check that the size matches. */
                                735         [ +  + ]:          56678 :     if (m->size != sb.st_size)
                                736                 :                :     {
                                737                 :              2 :         report_backup_error(context,
                                738                 :                :                             "\"%s\" has size %llu on disk but size %llu in the manifest",
  580                           739                 :              2 :                             relpath, (unsigned long long) sb.st_size,
                                740                 :              2 :                             (unsigned long long) m->size);
 2223                           741                 :              2 :         m->bad = true;
                                742                 :                :     }
                                743                 :                : 
                                744                 :                :     /*
                                745                 :                :      * Validate the manifest system identifier, not available in manifest
                                746                 :                :      * version 1.
                                747                 :                :      */
  783                           748         [ +  - ]:          56678 :     if (context->manifest->version != 1 &&
  393 fujii@postgresql.org      749         [ +  + ]:          56678 :         strcmp(relpath, XLOG_CONTROL_FILE) == 0)
  783 rhaas@postgresql.org      750                 :             57 :         verify_control_file(fullpath, context->manifest->system_identifier);
                                751                 :                : 
                                752                 :                :     /* Update statistics for progress report, if necessary */
  627                           753   [ +  +  +  - ]:          56677 :     if (show_progress && !context->skip_checksums &&
                                754   [ +  -  +  -  :           1027 :         should_verify_checksum(m))
                                              +  - ]
 1184 michael@paquier.xyz       755                 :           1027 :         total_size += m->size;
                                756                 :                : 
                                757                 :                :     /*
                                758                 :                :      * We don't verify checksums at this stage. We first finish verifying that
                                759                 :                :      * we have the expected set of files with the expected sizes, and only
                                760                 :                :      * afterwards verify the checksums. That's because computing checksums may
                                761                 :                :      * take a while, and we'd like to report more obvious problems quickly.
                                762                 :                :      */
                                763                 :                : }
                                764                 :                : 
                                765                 :                : /*
                                766                 :                :  * Sanity check control file and validate system identifier against manifest
                                767                 :                :  * system identifier.
                                768                 :                :  */
                                769                 :                : static void
  783 rhaas@postgresql.org      770                 :             57 : verify_control_file(const char *controlpath, uint64 manifest_system_identifier)
                                771                 :                : {
                                772                 :                :     ControlFileData *control_file;
                                773                 :                :     bool        crc_ok;
                                774                 :                : 
                                775         [ -  + ]:             57 :     pg_log_debug("reading \"%s\"", controlpath);
                                776                 :             57 :     control_file = get_controlfile_by_exact_path(controlpath, &crc_ok);
                                777                 :                : 
                                778                 :                :     /* Control file contents not meaningful if CRC is bad. */
                                779         [ -  + ]:             57 :     if (!crc_ok)
  783 rhaas@postgresql.org      780                 :UBC           0 :         report_fatal_error("%s: CRC is incorrect", controlpath);
                                781                 :                : 
                                782                 :                :     /* Can't interpret control file if not current version. */
  783 rhaas@postgresql.org      783         [ -  + ]:CBC          57 :     if (control_file->pg_control_version != PG_CONTROL_VERSION)
  783 rhaas@postgresql.org      784                 :UBC           0 :         report_fatal_error("%s: unexpected control file version",
                                785                 :                :                            controlpath);
                                786                 :                : 
                                787                 :                :     /* System identifiers should match. */
  783 rhaas@postgresql.org      788         [ +  + ]:CBC          57 :     if (manifest_system_identifier != control_file->system_identifier)
  402 peter@eisentraut.org      789                 :              1 :         report_fatal_error("%s: manifest system identifier is %" PRIu64 ", but control file has %" PRIu64,
                                790                 :                :                            controlpath,
                                791                 :                :                            manifest_system_identifier,
                                792                 :                :                            control_file->system_identifier);
                                793                 :                : 
                                794                 :                :     /* Release memory. */
  783 rhaas@postgresql.org      795                 :             56 :     pfree(control_file);
                                796                 :             56 : }
                                797                 :                : 
                                798                 :                : /*
                                799                 :                :  * Verify tar backup.
                                800                 :                :  *
                                801                 :                :  * The caller should pass a handle to the target directory, which we will
                                802                 :                :  * close when we're done with it.
                                803                 :                :  */
                                804                 :                : static void
   46 andrew@dunslane.net       805                 :GNC          29 : verify_tar_backup(verifier_context *context, DIR *dir, char **base_archive_path,
                                806                 :                :                   char **wal_archive_path)
                                807                 :                : {
                                808                 :                :     struct dirent *dirent;
  585 rhaas@postgresql.org      809                 :CBC          29 :     SimplePtrList tarfiles = {NULL, NULL};
                                810                 :                :     SimplePtrListCell *cell;
                                811                 :                : 
                                812         [ -  + ]:             29 :     Assert(context->format != 'p');
                                813                 :                : 
                                814                 :             29 :     progress_report(false);
                                815                 :                : 
                                816                 :                :     /* First pass: scan the directory for tar files. */
                                817         [ +  + ]:            196 :     while (errno = 0, (dirent = readdir(dir)) != NULL)
                                818                 :                :     {
                                819                 :            167 :         char       *filename = dirent->d_name;
                                820                 :                : 
                                821                 :                :         /* Skip "." and ".." */
                                822   [ +  +  +  + ]:            167 :         if (filename[0] == '.' && (filename[1] == '\0'
                                823         [ +  - ]:             29 :                                    || strcmp(filename, "..") == 0))
                                824                 :             58 :             continue;
                                825                 :                : 
                                826                 :                :         /*
                                827                 :                :          * Unless it's something we should ignore, perform prechecks and add
                                828                 :                :          * it to the list.
                                829                 :                :          */
                                830         [ +  + ]:            109 :         if (!should_ignore_relpath(context, filename))
                                831                 :                :         {
                                832                 :                :             char       *fullpath;
                                833                 :                : 
                                834                 :             78 :             fullpath = psprintf("%s/%s", context->backup_directory, filename);
   46 andrew@dunslane.net       835                 :GNC          78 :             precheck_tar_backup_file(context, filename, fullpath, &tarfiles,
                                836                 :                :                                      base_archive_path, wal_archive_path);
  585 rhaas@postgresql.org      837                 :CBC          78 :             pfree(fullpath);
                                838                 :                :         }
                                839                 :                :     }
                                840                 :                : 
                                841         [ -  + ]:             29 :     if (closedir(dir))
                                842                 :                :     {
  585 rhaas@postgresql.org      843                 :UBC           0 :         report_backup_error(context,
                                844                 :                :                             "could not close directory \"%s\": %m",
                                845                 :                :                             context->backup_directory);
                                846                 :              0 :         return;
                                847                 :                :     }
                                848                 :                : 
                                849                 :                :     /* Second pass: Perform the final verification of the tar contents. */
  585 rhaas@postgresql.org      850         [ +  + ]:CBC          68 :     for (cell = tarfiles.head; cell != NULL; cell = cell->next)
                                851                 :                :     {
                                852                 :             40 :         tar_file   *tar = (tar_file *) cell->ptr;
                                853                 :                :         astreamer  *streamer;
                                854                 :                :         char       *fullpath;
                                855                 :                : 
                                856                 :                :         /*
                                857                 :                :          * Prepares the archive streamer stack according to the tar
                                858                 :                :          * compression format.
                                859                 :                :          */
                                860                 :             40 :         streamer = create_archive_verifier(context,
                                861                 :                :                                            tar->relpath,
                                862                 :                :                                            tar->tblspc_oid,
                                863                 :                :                                            tar->compress_algorithm);
                                864                 :                : 
                                865                 :                :         /* Compute the full pathname to the target file. */
                                866                 :             40 :         fullpath = psprintf("%s/%s", context->backup_directory,
                                867                 :                :                             tar->relpath);
                                868                 :                : 
                                869                 :                :         /* Invoke the streamer for reading, decompressing, and verifying. */
                                870                 :             40 :         verify_tar_file(context, tar->relpath, fullpath, streamer);
                                871                 :                : 
                                872                 :                :         /* Cleanup. */
                                873                 :             39 :         pfree(tar->relpath);
                                874                 :             39 :         pfree(tar);
                                875                 :             39 :         pfree(fullpath);
                                876                 :                : 
                                877                 :             39 :         astreamer_finalize(streamer);
                                878                 :             39 :         astreamer_free(streamer);
                                879                 :                :     }
                                880                 :             28 :     simple_ptr_list_destroy(&tarfiles);
                                881                 :                : 
                                882                 :             28 :     progress_report(true);
                                883                 :                : }
                                884                 :                : 
                                885                 :                : /*
                                886                 :                :  * Preparatory steps for verifying files in tar format backups.
                                887                 :                :  *
                                888                 :                :  * Carries out basic validation of the tar format backup file, detects the
                                889                 :                :  * compression type, and appends that information to the tarfiles list. An
                                890                 :                :  * error will be reported if the tar file is inaccessible, or if the file type,
                                891                 :                :  * name, or compression type is not as expected.
                                892                 :                :  *
                                893                 :                :  * The arguments to this function are mostly the same as the
                                894                 :                :  * verify_plain_backup_file. The additional argument outputs a list of valid
                                895                 :                :  * tar files, along with the full paths to the main archive and the WAL
                                896                 :                :  * directory archive.
                                897                 :                :  */
                                898                 :                : static void
                                899                 :             78 : precheck_tar_backup_file(verifier_context *context, char *relpath,
                                900                 :                :                          char *fullpath, SimplePtrList *tarfiles,
                                901                 :                :                          char **base_archive_path, char **wal_archive_path)
                                902                 :                : {
                                903                 :                :     struct stat sb;
                                904                 :             78 :     Oid         tblspc_oid = InvalidOid;
                                905                 :                :     pg_compress_algorithm compress_algorithm;
                                906                 :                :     tar_file   *tar;
                                907                 :             78 :     char       *suffix = NULL;
   46 andrew@dunslane.net       908                 :GNC          78 :     bool        is_base_archive = false;
                                909                 :             78 :     bool        is_wal_archive = false;
                                910                 :                : 
                                911                 :                :     /* Should be tar format backup */
  585 rhaas@postgresql.org      912         [ -  + ]:CBC          78 :     Assert(context->format == 't');
                                913                 :                : 
                                914                 :                :     /* Get file information */
                                915         [ -  + ]:             78 :     if (stat(fullpath, &sb) != 0)
                                916                 :                :     {
  585 rhaas@postgresql.org      917                 :UBC           0 :         report_backup_error(context,
                                918                 :                :                             "could not stat file or directory \"%s\": %m",
                                919                 :                :                             relpath);
  585 rhaas@postgresql.org      920                 :CBC          37 :         return;
                                921                 :                :     }
                                922                 :                : 
                                923                 :                :     /* In a tar format backup, we expect only regular files. */
                                924         [ +  + ]:             78 :     if (!S_ISREG(sb.st_mode))
                                925                 :                :     {
                                926                 :             16 :         report_backup_error(context,
                                927                 :                :                             "file \"%s\" is not a regular file",
                                928                 :                :                             relpath);
                                929                 :             16 :         return;
                                930                 :                :     }
                                931                 :                : 
                                932                 :                :     /*
                                933                 :                :      * We expect tar files for backing up the main directory, tablespace, and
                                934                 :                :      * pg_wal directory.
                                935                 :                :      *
                                936                 :                :      * pg_basebackup writes the main data directory to an archive file named
                                937                 :                :      * base.tar, the pg_wal directory to pg_wal.tar, and the tablespace
                                938                 :                :      * directory to <tablespaceoid>.tar, each followed by a compression type
                                939                 :                :      * extension such as .gz, .lz4, or .zst.
                                940                 :                :      */
                                941         [ +  + ]:             62 :     if (strncmp("base", relpath, 4) == 0)
                                942                 :                :     {
                                943                 :             28 :         suffix = relpath + 4;
   46 andrew@dunslane.net       944                 :GNC          28 :         is_base_archive = true;
                                945                 :                :     }
  585 rhaas@postgresql.org      946         [ +  + ]:CBC          34 :     else if (strncmp("pg_wal", relpath, 6) == 0)
                                947                 :                :     {
                                948                 :             15 :         suffix = relpath + 6;
   46 andrew@dunslane.net       949                 :GNC          15 :         is_wal_archive = true;
                                950                 :                :     }
                                951                 :                :     else
                                952                 :                :     {
                                953                 :                :         /* Expected a <tablespaceoid>.tar file here. */
  585 rhaas@postgresql.org      954                 :CBC          19 :         uint64      num = strtoul(relpath, &suffix, 10);
                                955                 :                : 
                                956                 :                :         /*
                                957                 :                :          * Report an error if we didn't consume at least one character, if the
                                958                 :                :          * result is 0, or if the value is too large to be a valid OID.
                                959                 :                :          */
                                960   [ +  -  +  +  :             19 :         if (suffix == NULL || num <= 0 || num > OID_MAX)
                                              -  + ]
                                961                 :                :         {
                                962                 :              6 :             report_backup_error(context,
                                963                 :                :                                 "file \"%s\" is not expected in a tar format backup",
                                964                 :                :                                 relpath);
  581                           965                 :              6 :             return;
                                966                 :                :         }
  585                           967                 :             13 :         tblspc_oid = (Oid) num;
                                968                 :                :     }
                                969                 :                : 
                                970                 :                :     /* Now, check the compression type of the tar */
   46 andrew@dunslane.net       971         [ -  + ]:GNC          56 :     if (!parse_tar_compress_algorithm(suffix, &compress_algorithm))
                                972                 :                :     {
  585 rhaas@postgresql.org      973                 :UBC           0 :         report_backup_error(context,
                                974                 :                :                             "file \"%s\" is not expected in a tar format backup",
                                975                 :                :                             relpath);
                                976                 :              0 :         return;
                                977                 :                :     }
                                978                 :                : 
                                979                 :                :     /*
                                980                 :                :      * Ignore WALs, as reading and verification will be handled through
                                981                 :                :      * pg_waldump.
                                982                 :                :      */
   46 andrew@dunslane.net       983         [ +  + ]:GNC          56 :     if (is_wal_archive)
                                984                 :                :     {
                                985                 :             15 :         *wal_archive_path = pstrdup(fullpath);
  585 rhaas@postgresql.org      986                 :CBC          15 :         return;
                                987                 :                :     }
   46 andrew@dunslane.net       988         [ +  + ]:GNC          41 :     else if (is_base_archive)
                                989                 :             28 :         *base_archive_path = pstrdup(fullpath);
                                990                 :                : 
                                991                 :                :     /*
                                992                 :                :      * Append the information to the list for complete verification at a later
                                993                 :                :      * stage.
                                994                 :                :      */
   67 michael@paquier.xyz       995                 :             41 :     tar = pg_malloc_object(tar_file);
  585 rhaas@postgresql.org      996                 :CBC          41 :     tar->relpath = pstrdup(relpath);
                                997                 :             41 :     tar->tblspc_oid = tblspc_oid;
                                998                 :             41 :     tar->compress_algorithm = compress_algorithm;
                                999                 :                : 
                               1000                 :             41 :     simple_ptr_list_append(tarfiles, tar);
                               1001                 :                : 
                               1002                 :                :     /* Update statistics for progress report, if necessary */
                               1003         [ -  + ]:             41 :     if (show_progress)
  585 rhaas@postgresql.org     1004                 :UBC           0 :         total_size += sb.st_size;
                               1005                 :                : }
                               1006                 :                : 
                               1007                 :                : /*
                               1008                 :                :  * Verification of a single tar file content.
                               1009                 :                :  *
                               1010                 :                :  * It reads a given tar archive in predefined chunks and passes it to the
                               1011                 :                :  * streamer, which initiates routines for decompression (if necessary) and then
                               1012                 :                :  * verifies each member within the tar file.
                               1013                 :                :  */
                               1014                 :                : static void
  585 rhaas@postgresql.org     1015                 :CBC          40 : verify_tar_file(verifier_context *context, char *relpath, char *fullpath,
                               1016                 :                :                 astreamer *streamer)
                               1017                 :                : {
                               1018                 :                :     int         fd;
                               1019                 :                :     int         rc;
                               1020                 :                :     char       *buffer;
                               1021                 :                : 
                               1022         [ -  + ]:             40 :     pg_log_debug("reading \"%s\"", fullpath);
                               1023                 :                : 
                               1024                 :                :     /* Open the target file. */
                               1025         [ -  + ]:             40 :     if ((fd = open(fullpath, O_RDONLY | PG_BINARY, 0)) < 0)
                               1026                 :                :     {
  585 rhaas@postgresql.org     1027                 :UBC           0 :         report_backup_error(context, "could not open file \"%s\": %m",
                               1028                 :                :                             relpath);
                               1029                 :              0 :         return;
                               1030                 :                :     }
                               1031                 :                : 
  585 rhaas@postgresql.org     1032                 :CBC          40 :     buffer = pg_malloc(READ_CHUNK_SIZE * sizeof(uint8));
                               1033                 :                : 
                               1034                 :                :     /* Perform the reads */
                               1035         [ +  + ]:           3731 :     while ((rc = read(fd, buffer, READ_CHUNK_SIZE)) > 0)
                               1036                 :                :     {
                               1037                 :           3692 :         astreamer_content(streamer, NULL, buffer, rc, ASTREAMER_UNKNOWN);
                               1038                 :                : 
                               1039                 :                :         /* Report progress */
                               1040                 :           3691 :         done_size += rc;
                               1041                 :           3691 :         progress_report(false);
                               1042                 :                :     }
                               1043                 :                : 
  581                          1044                 :             39 :     pg_free(buffer);
                               1045                 :                : 
  585                          1046         [ -  + ]:             39 :     if (rc < 0)
  585 rhaas@postgresql.org     1047                 :UBC           0 :         report_backup_error(context, "could not read file \"%s\": %m",
                               1048                 :                :                             relpath);
                               1049                 :                : 
                               1050                 :                :     /* Close the file. */
  585 rhaas@postgresql.org     1051         [ -  + ]:CBC          39 :     if (close(fd) != 0)
  585 rhaas@postgresql.org     1052                 :UBC           0 :         report_backup_error(context, "could not close file \"%s\": %m",
                               1053                 :                :                             relpath);
                               1054                 :                : }
                               1055                 :                : 
                               1056                 :                : /*
                               1057                 :                :  * Scan the hash table for entries where the 'matched' flag is not set; report
                               1058                 :                :  * that such files are present in the manifest but not on disk.
                               1059                 :                :  */
                               1060                 :                : static void
 2214 rhaas@postgresql.org     1061                 :CBC          84 : report_extra_backup_files(verifier_context *context)
                               1062                 :                : {
  792                          1063                 :             84 :     manifest_data *manifest = context->manifest;
                               1064                 :                :     manifest_files_iterator it;
                               1065                 :                :     manifest_file *m;
                               1066                 :                : 
                               1067                 :             84 :     manifest_files_start_iterate(manifest->files, &it);
                               1068         [ +  + ]:          86094 :     while ((m = manifest_files_iterate(manifest->files, &it)) != NULL)
 2223                          1069   [ +  +  +  + ]:          85927 :         if (!m->matched && !should_ignore_relpath(context, m->pathname))
                               1070                 :           1035 :             report_backup_error(context,
                               1071                 :                :                                 "\"%s\" is present in the manifest but not on disk",
                               1072                 :                :                                 m->pathname);
                               1073                 :             83 : }
                               1074                 :                : 
                               1075                 :                : /*
                               1076                 :                :  * Verify checksums for hash table entries that are otherwise unproblematic.
                               1077                 :                :  * If we've already reported some problem related to a hash table entry, or
                               1078                 :                :  * if it has no checksum, just skip it.
                               1079                 :                :  */
                               1080                 :                : static void
 2214                          1081                 :             53 : verify_backup_checksums(verifier_context *context)
                               1082                 :                : {
  792                          1083                 :             53 :     manifest_data *manifest = context->manifest;
                               1084                 :                :     manifest_files_iterator it;
                               1085                 :                :     manifest_file *m;
                               1086                 :                :     uint8      *buffer;
                               1087                 :                : 
 1184 michael@paquier.xyz      1088                 :             53 :     progress_report(false);
                               1089                 :                : 
   67 michael@paquier.xyz      1090                 :GNC          53 :     buffer = pg_malloc_array(uint8, READ_CHUNK_SIZE);
                               1091                 :                : 
  792 rhaas@postgresql.org     1092                 :CBC          53 :     manifest_files_start_iterate(manifest->files, &it);
                               1093         [ +  + ]:          54655 :     while ((m = manifest_files_iterate(manifest->files, &it)) != NULL)
                               1094                 :                :     {
 1184 michael@paquier.xyz      1095   [ +  +  +  +  :          54602 :         if (should_verify_checksum(m) &&
                                              +  + ]
 2223 rhaas@postgresql.org     1096         [ +  - ]:          51513 :             !should_ignore_relpath(context, m->pathname))
                               1097                 :                :         {
                               1098                 :                :             char       *fullpath;
                               1099                 :                : 
                               1100                 :                :             /* Compute the full pathname to the target file. */
                               1101                 :          51513 :             fullpath = psprintf("%s/%s", context->backup_directory,
                               1102                 :                :                                 m->pathname);
                               1103                 :                : 
                               1104                 :                :             /* Do the actual checksum verification. */
  753 andrew@dunslane.net      1105                 :          51513 :             verify_file_checksum(context, m, fullpath, buffer);
                               1106                 :                : 
                               1107                 :                :             /* Avoid leaking memory. */
 2223 rhaas@postgresql.org     1108                 :          51513 :             pfree(fullpath);
                               1109                 :                :         }
                               1110                 :                :     }
                               1111                 :                : 
  753 andrew@dunslane.net      1112                 :             53 :     pfree(buffer);
                               1113                 :                : 
 1184 michael@paquier.xyz      1114                 :             53 :     progress_report(true);
 2223 rhaas@postgresql.org     1115                 :             53 : }
                               1116                 :                : 
                               1117                 :                : /*
                               1118                 :                :  * Verify the checksum of a single file.
                               1119                 :                :  */
                               1120                 :                : static void
 2214                          1121                 :          51513 : verify_file_checksum(verifier_context *context, manifest_file *m,
                               1122                 :                :                      char *fullpath, uint8 *buffer)
                               1123                 :                : {
                               1124                 :                :     pg_checksum_context checksum_ctx;
  683 peter@eisentraut.org     1125                 :          51513 :     const char *relpath = m->pathname;
                               1126                 :                :     int         fd;
                               1127                 :                :     int         rc;
  580 rhaas@postgresql.org     1128                 :          51513 :     uint64      bytes_read = 0;
                               1129                 :                :     uint8       checksumbuf[PG_CHECKSUM_MAX_LENGTH];
                               1130                 :                :     int         checksumlen;
                               1131                 :                : 
                               1132                 :                :     /* Open the target file. */
 2223                          1133         [ +  + ]:          51513 :     if ((fd = open(fullpath, O_RDONLY | PG_BINARY, 0)) < 0)
                               1134                 :                :     {
                               1135                 :              1 :         report_backup_error(context, "could not open file \"%s\": %m",
                               1136                 :                :                             relpath);
                               1137                 :              1 :         return;
                               1138                 :                :     }
                               1139                 :                : 
                               1140                 :                :     /* Initialize checksum context. */
 1980 michael@paquier.xyz      1141         [ -  + ]:          51512 :     if (pg_checksum_init(&checksum_ctx, m->checksum_type) < 0)
                               1142                 :                :     {
 1980 michael@paquier.xyz      1143                 :UBC           0 :         report_backup_error(context, "could not initialize checksum of file \"%s\"",
                               1144                 :                :                             relpath);
 1975                          1145                 :              0 :         close(fd);
 1980                          1146                 :              0 :         return;
                               1147                 :                :     }
                               1148                 :                : 
                               1149                 :                :     /* Read the file chunk by chunk, updating the checksum as we go. */
 2223 rhaas@postgresql.org     1150         [ +  + ]:CBC       96365 :     while ((rc = read(fd, buffer, READ_CHUNK_SIZE)) > 0)
                               1151                 :                :     {
                               1152                 :          44853 :         bytes_read += rc;
 1980 michael@paquier.xyz      1153         [ -  + ]:          44853 :         if (pg_checksum_update(&checksum_ctx, buffer, rc) < 0)
                               1154                 :                :         {
 1980 michael@paquier.xyz      1155                 :UBC           0 :             report_backup_error(context, "could not update checksum of file \"%s\"",
                               1156                 :                :                                 relpath);
                               1157                 :              0 :             close(fd);
                               1158                 :              0 :             return;
                               1159                 :                :         }
                               1160                 :                : 
                               1161                 :                :         /* Report progress */
 1184 michael@paquier.xyz      1162                 :CBC       44853 :         done_size += rc;
                               1163                 :          44853 :         progress_report(false);
                               1164                 :                :     }
 2223 rhaas@postgresql.org     1165         [ -  + ]:          51512 :     if (rc < 0)
 2223 rhaas@postgresql.org     1166                 :UBC           0 :         report_backup_error(context, "could not read file \"%s\": %m",
                               1167                 :                :                             relpath);
                               1168                 :                : 
                               1169                 :                :     /* Close the file. */
 2223 rhaas@postgresql.org     1170         [ -  + ]:CBC       51512 :     if (close(fd) != 0)
                               1171                 :                :     {
 2223 rhaas@postgresql.org     1172                 :UBC           0 :         report_backup_error(context, "could not close file \"%s\": %m",
                               1173                 :                :                             relpath);
                               1174                 :              0 :         return;
                               1175                 :                :     }
                               1176                 :                : 
                               1177                 :                :     /* If we didn't manage to read the whole file, bail out now. */
 2223 rhaas@postgresql.org     1178         [ -  + ]:CBC       51512 :     if (rc < 0)
 2223 rhaas@postgresql.org     1179                 :UBC           0 :         return;
                               1180                 :                : 
                               1181                 :                :     /*
                               1182                 :                :      * Double-check that we read the expected number of bytes from the file.
                               1183                 :                :      * Normally, mismatches would be caught in verify_plain_backup_file and
                               1184                 :                :      * this check would never be reached, but this provides additional safety
                               1185                 :                :      * and clarity in the event of concurrent modifications or filesystem
                               1186                 :                :      * misbehavior.
                               1187                 :                :      */
 2223 rhaas@postgresql.org     1188         [ -  + ]:CBC       51512 :     if (bytes_read != m->size)
                               1189                 :                :     {
 2223 rhaas@postgresql.org     1190                 :UBC           0 :         report_backup_error(context,
                               1191                 :                :                             "file \"%s\" should contain %" PRIu64 " bytes, but read %" PRIu64,
                               1192                 :                :                             relpath, m->size, bytes_read);
                               1193                 :              0 :         return;
                               1194                 :                :     }
                               1195                 :                : 
                               1196                 :                :     /* Get the final checksum. */
 2223 rhaas@postgresql.org     1197                 :CBC       51512 :     checksumlen = pg_checksum_final(&checksum_ctx, checksumbuf);
 1980 michael@paquier.xyz      1198         [ -  + ]:          51512 :     if (checksumlen < 0)
                               1199                 :                :     {
 1980 michael@paquier.xyz      1200                 :UBC           0 :         report_backup_error(context,
                               1201                 :                :                             "could not finalize checksum of file \"%s\"",
                               1202                 :                :                             relpath);
                               1203                 :              0 :         return;
                               1204                 :                :     }
                               1205                 :                : 
                               1206                 :                :     /* And check it against the manifest. */
 2223 rhaas@postgresql.org     1207         [ -  + ]:CBC       51512 :     if (checksumlen != m->checksum_length)
 2223 rhaas@postgresql.org     1208                 :UBC           0 :         report_backup_error(context,
                               1209                 :                :                             "file \"%s\" has checksum of length %d, but expected %d",
                               1210                 :                :                             relpath, m->checksum_length, checksumlen);
 2223 rhaas@postgresql.org     1211         [ +  + ]:CBC       51512 :     else if (memcmp(checksumbuf, m->checksum_payload, checksumlen) != 0)
                               1212                 :              3 :         report_backup_error(context,
                               1213                 :                :                             "checksum mismatch for file \"%s\"",
                               1214                 :                :                             relpath);
                               1215                 :                : }
                               1216                 :                : 
                               1217                 :                : /*
                               1218                 :                :  * Attempt to parse the WAL files required to restore from backup using
                               1219                 :                :  * pg_waldump.
                               1220                 :                :  */
                               1221                 :                : static void
 2214                          1222                 :             79 : parse_required_wal(verifier_context *context, char *pg_waldump_path,
                               1223                 :                :                    char *wal_path)
                               1224                 :                : {
  792                          1225                 :             79 :     manifest_data *manifest = context->manifest;
                               1226                 :             79 :     manifest_wal_range *this_wal_range = manifest->first_wal_range;
                               1227                 :                : 
 2223                          1228         [ +  + ]:            158 :     while (this_wal_range != NULL)
                               1229                 :                :     {
                               1230                 :                :         char       *pg_waldump_cmd;
                               1231                 :                : 
  302 alvherre@kurilemu.de     1232                 :GNC          79 :         pg_waldump_cmd = psprintf("\"%s\" --quiet --path=\"%s\" --timeline=%u --start=%X/%08X --end=%X/%08X\n",
                               1233                 :                :                                   pg_waldump_path, wal_path, this_wal_range->tli,
 1897 peter@eisentraut.org     1234                 :CBC          79 :                                   LSN_FORMAT_ARGS(this_wal_range->start_lsn),
                               1235                 :             79 :                                   LSN_FORMAT_ARGS(this_wal_range->end_lsn));
 1345 tgl@sss.pgh.pa.us        1236                 :             79 :         fflush(NULL);
 2223 rhaas@postgresql.org     1237         [ +  + ]:             79 :         if (system(pg_waldump_cmd) != 0)
                               1238                 :              2 :             report_backup_error(context,
                               1239                 :                :                                 "WAL parsing failed for timeline %u",
                               1240                 :                :                                 this_wal_range->tli);
                               1241                 :                : 
                               1242                 :             79 :         this_wal_range = this_wal_range->next;
                               1243                 :                :     }
                               1244                 :             79 : }
                               1245                 :                : 
                               1246                 :                : /*
                               1247                 :                :  * Report a problem with the backup.
                               1248                 :                :  *
                               1249                 :                :  * Update the context to indicate that we saw an error, and exit if the
                               1250                 :                :  * context says we should.
                               1251                 :                :  */
                               1252                 :                : void
  111 peter@eisentraut.org     1253                 :           1077 : report_backup_error(verifier_context *context, const char *pg_restrict fmt,...)
                               1254                 :                : {
                               1255                 :                :     va_list     ap;
                               1256                 :                : 
 2223 rhaas@postgresql.org     1257                 :           1077 :     va_start(ap, fmt);
 1488 tgl@sss.pgh.pa.us        1258                 :           1077 :     pg_log_generic_v(PG_LOG_ERROR, PG_LOG_PRIMARY, gettext(fmt), ap);
 2223 rhaas@postgresql.org     1259                 :           1077 :     va_end(ap);
                               1260                 :                : 
                               1261                 :           1077 :     context->saw_any_error = true;
                               1262         [ +  + ]:           1077 :     if (context->exit_on_error)
                               1263                 :              1 :         exit(1);
                               1264                 :           1076 : }
                               1265                 :                : 
                               1266                 :                : /*
                               1267                 :                :  * Report a fatal error and exit
                               1268                 :                :  */
                               1269                 :                : void
  111 peter@eisentraut.org     1270                 :              7 : report_fatal_error(const char *pg_restrict fmt,...)
                               1271                 :                : {
                               1272                 :                :     va_list     ap;
                               1273                 :                : 
 2223 rhaas@postgresql.org     1274                 :              7 :     va_start(ap, fmt);
 1488 tgl@sss.pgh.pa.us        1275                 :              7 :     pg_log_generic_v(PG_LOG_ERROR, PG_LOG_PRIMARY, gettext(fmt), ap);
 2223 rhaas@postgresql.org     1276                 :              7 :     va_end(ap);
                               1277                 :                : 
                               1278                 :              7 :     exit(1);
                               1279                 :                : }
                               1280                 :                : 
                               1281                 :                : /*
                               1282                 :                :  * Is the specified relative path, or some prefix of it, listed in the set
                               1283                 :                :  * of paths to ignore?
                               1284                 :                :  *
                               1285                 :                :  * Note that by "prefix" we mean a parent directory; for this purpose,
                               1286                 :                :  * "aa/bb" is not a prefix of "aa/bbb", but it is a prefix of "aa/bb/cc".
                               1287                 :                :  */
                               1288                 :                : bool
  683 peter@eisentraut.org     1289                 :         139809 : should_ignore_relpath(verifier_context *context, const char *relpath)
                               1290                 :                : {
                               1291                 :                :     SimpleStringListCell *cell;
                               1292                 :                : 
 2223 rhaas@postgresql.org     1293         [ +  + ]:         849045 :     for (cell = context->ignore_list.head; cell != NULL; cell = cell->next)
                               1294                 :                :     {
  683 peter@eisentraut.org     1295                 :         710557 :         const char *r = relpath;
 2223 rhaas@postgresql.org     1296                 :         710557 :         char       *v = cell->val;
                               1297                 :                : 
                               1298   [ +  +  +  + ]:         995714 :         while (*v != '\0' && *r == *v)
                               1299                 :         285157 :             ++r, ++v;
                               1300                 :                : 
                               1301   [ +  +  +  +  :         710557 :         if (*v == '\0' && (*r == '\0' || *r == '/'))
                                              +  + ]
                               1302                 :           1321 :             return true;
                               1303                 :                :     }
                               1304                 :                : 
                               1305                 :         138488 :     return false;
                               1306                 :                : }
                               1307                 :                : 
                               1308                 :                : /*
                               1309                 :                :  * Create a chain of archive streamers appropriate for verifying a given
                               1310                 :                :  * archive.
                               1311                 :                :  */
                               1312                 :                : static astreamer *
  585                          1313                 :             40 : create_archive_verifier(verifier_context *context, char *archive_name,
                               1314                 :                :                         Oid tblspc_oid, pg_compress_algorithm compress_algo)
                               1315                 :                : {
                               1316                 :             40 :     astreamer  *streamer = NULL;
                               1317                 :                : 
                               1318                 :                :     /* Should be here only for tar backup */
                               1319         [ -  + ]:             40 :     Assert(context->format == 't');
                               1320                 :                : 
                               1321                 :                :     /* Last step is the actual verification. */
                               1322                 :             40 :     streamer = astreamer_verify_content_new(streamer, context, archive_name,
                               1323                 :                :                                             tblspc_oid);
                               1324                 :                : 
                               1325                 :                :     /* Before that we must parse the tar file. */
                               1326                 :             40 :     streamer = astreamer_tar_parser_new(streamer);
                               1327                 :                : 
                               1328                 :                :     /* Before that we must decompress, if archive is compressed. */
                               1329         [ +  + ]:             40 :     if (compress_algo == PG_COMPRESSION_GZIP)
                               1330                 :              3 :         streamer = astreamer_gzip_decompressor_new(streamer);
                               1331         [ +  + ]:             37 :     else if (compress_algo == PG_COMPRESSION_LZ4)
                               1332                 :              6 :         streamer = astreamer_lz4_decompressor_new(streamer);
                               1333         [ +  + ]:             31 :     else if (compress_algo == PG_COMPRESSION_ZSTD)
                               1334                 :              7 :         streamer = astreamer_zstd_decompressor_new(streamer);
                               1335                 :                : 
                               1336                 :             40 :     return streamer;
                               1337                 :                : }
                               1338                 :                : 
                               1339                 :                : /*
                               1340                 :                :  * Print a progress report based on the global variables.
                               1341                 :                :  *
                               1342                 :                :  * Progress report is written at maximum once per second, unless the finished
                               1343                 :                :  * parameter is set to true.
                               1344                 :                :  *
                               1345                 :                :  * If finished is set to true, this is the last progress report. The cursor
                               1346                 :                :  * is moved to the next line.
                               1347                 :                :  */
                               1348                 :                : static void
 1184 michael@paquier.xyz      1349                 :          48707 : progress_report(bool finished)
                               1350                 :                : {
                               1351                 :                :     static pg_time_t last_progress_report = 0;
                               1352                 :                :     pg_time_t   now;
                               1353                 :          48707 :     int         percent_size = 0;
                               1354                 :                :     char        totalsize_str[32];
                               1355                 :                :     char        donesize_str[32];
                               1356                 :                : 
                               1357         [ +  + ]:          48707 :     if (!show_progress)
                               1358                 :          48705 :         return;
                               1359                 :                : 
                               1360                 :            899 :     now = time(NULL);
                               1361   [ +  +  +  + ]:            899 :     if (now == last_progress_report && !finished)
                               1362                 :            897 :         return;                 /* Max once per second */
                               1363                 :                : 
                               1364                 :              2 :     last_progress_report = now;
                               1365         [ +  - ]:              2 :     percent_size = total_size ? (int) ((done_size * 100 / total_size)) : 0;
                               1366                 :                : 
                               1367                 :              2 :     snprintf(totalsize_str, sizeof(totalsize_str), UINT64_FORMAT,
                               1368                 :                :              total_size / 1024);
                               1369                 :              2 :     snprintf(donesize_str, sizeof(donesize_str), UINT64_FORMAT,
                               1370                 :                :              done_size / 1024);
                               1371                 :                : 
                               1372                 :              2 :     fprintf(stderr,
                               1373                 :              2 :             _("%*s/%s kB (%d%%) verified"),
                               1374                 :              2 :             (int) strlen(totalsize_str),
                               1375                 :                :             donesize_str, totalsize_str, percent_size);
                               1376                 :                : 
                               1377                 :                :     /*
                               1378                 :                :      * Stay on the same line if reporting to a terminal and we're not done
                               1379                 :                :      * yet.
                               1380                 :                :      */
                               1381   [ +  +  -  + ]:              2 :     fputc((!finished && isatty(fileno(stderr))) ? '\r' : '\n', stderr);
                               1382                 :                : }
                               1383                 :                : 
                               1384                 :                : /*
                               1385                 :                :  * Print out usage information and exit.
                               1386                 :                :  */
                               1387                 :                : static void
 2223 rhaas@postgresql.org     1388                 :              1 : usage(void)
                               1389                 :                : {
 2214                          1390                 :              1 :     printf(_("%s verifies a backup against the backup manifest.\n\n"), progname);
 2223                          1391                 :              1 :     printf(_("Usage:\n  %s [OPTION]... BACKUPDIR\n\n"), progname);
                               1392                 :              1 :     printf(_("Options:\n"));
                               1393                 :              1 :     printf(_("  -e, --exit-on-error         exit immediately on error\n"));
  585                          1394                 :              1 :     printf(_("  -F, --format=p|t            backup format (plain, tar)\n"));
 2223                          1395                 :              1 :     printf(_("  -i, --ignore=RELATIVE_PATH  ignore indicated path\n"));
 2203 fujii@postgresql.org     1396                 :              1 :     printf(_("  -m, --manifest-path=PATH    use specified path for manifest\n"));
 2223 rhaas@postgresql.org     1397                 :              1 :     printf(_("  -n, --no-parse-wal          do not try to parse WAL files\n"));
 1184 michael@paquier.xyz      1398                 :              1 :     printf(_("  -P, --progress              show progress information\n"));
 2203 fujii@postgresql.org     1399                 :              1 :     printf(_("  -q, --quiet                 do not print any output, except for errors\n"));
 2223 rhaas@postgresql.org     1400                 :              1 :     printf(_("  -s, --skip-checksums        skip checksum verification\n"));
   46 andrew@dunslane.net      1401                 :GNC           1 :     printf(_("  -w, --wal-path=PATH         use specified path for WAL files\n"));
 2223 rhaas@postgresql.org     1402                 :CBC           1 :     printf(_("  -V, --version               output version information, then exit\n"));
                               1403                 :              1 :     printf(_("  -?, --help                  show this help, then exit\n"));
                               1404                 :              1 :     printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
                               1405                 :              1 :     printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
                               1406                 :              1 : }
        

Generated by: LCOV version 2.5.0-beta