LCOV - differential code coverage report
Current view: top level - src/backend/backup - basebackup.c (source / functions) Coverage Total Hit UNC UBC GIC GNC CBC DUB DCB
Current: Differential Code Coverage 16@8cea358b128 vs 17@8cea358b128 Lines: 83.6 % 672 562 22 88 1 149 412 9 79
Current Date: 2024-04-14 14:21:10 Functions: 93.3 % 15 14 1 10 4 4
Baseline: 16@8cea358b128 Branches: 59.5 % 622 370 34 218 90 280
Baseline Date: 2024-04-14 14:21:09 Line coverage date bins:
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed [..60] days: 100.0 % 7 7 7
(60,120] days: 84.4 % 96 81 15 79 2
(120,180] days: 93.3 % 30 28 2 28
(180,240] days: 87.5 % 40 35 5 35
(240..) days: 82.4 % 499 411 88 1 410
Function coverage date bins:
(60,120] days: 100.0 % 3 3 3
(120,180] days: 100.0 % 1 1 1
(180,240] days: 100.0 % 2 2 2
(240..) days: 88.9 % 9 8 1 4 4
Branch coverage date bins:
[..60] days: 70.0 % 10 7 3 7
(60,120] days: 66.1 % 62 41 21 39 2
(120,180] days: 91.7 % 24 22 2 22
(180,240] days: 73.3 % 30 22 8 22
(240..) days: 56.0 % 496 278 218 278

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * basebackup.c
                                  4                 :                :  *    code for taking a base backup and streaming it to a standby
                                  5                 :                :  *
                                  6                 :                :  * Portions Copyright (c) 2010-2024, PostgreSQL Global Development Group
                                  7                 :                :  *
                                  8                 :                :  * IDENTIFICATION
                                  9                 :                :  *    src/backend/backup/basebackup.c
                                 10                 :                :  *
                                 11                 :                :  *-------------------------------------------------------------------------
                                 12                 :                :  */
                                 13                 :                : #include "postgres.h"
                                 14                 :                : 
                                 15                 :                : #include <sys/stat.h>
                                 16                 :                : #include <unistd.h>
                                 17                 :                : #include <time.h>
                                 18                 :                : 
                                 19                 :                : #include "access/xlog_internal.h"
                                 20                 :                : #include "access/xlogbackup.h"
                                 21                 :                : #include "backup/backup_manifest.h"
                                 22                 :                : #include "backup/basebackup.h"
                                 23                 :                : #include "backup/basebackup_incremental.h"
                                 24                 :                : #include "backup/basebackup_sink.h"
                                 25                 :                : #include "backup/basebackup_target.h"
                                 26                 :                : #include "catalog/pg_tablespace_d.h"
                                 27                 :                : #include "commands/defrem.h"
                                 28                 :                : #include "common/compression.h"
                                 29                 :                : #include "common/file_perm.h"
                                 30                 :                : #include "common/file_utils.h"
                                 31                 :                : #include "lib/stringinfo.h"
                                 32                 :                : #include "miscadmin.h"
                                 33                 :                : #include "nodes/pg_list.h"
                                 34                 :                : #include "pgstat.h"
                                 35                 :                : #include "pgtar.h"
                                 36                 :                : #include "port.h"
                                 37                 :                : #include "postmaster/syslogger.h"
                                 38                 :                : #include "postmaster/walsummarizer.h"
                                 39                 :                : #include "replication/walsender.h"
                                 40                 :                : #include "replication/walsender_private.h"
                                 41                 :                : #include "storage/bufpage.h"
                                 42                 :                : #include "storage/checksum.h"
                                 43                 :                : #include "storage/dsm_impl.h"
                                 44                 :                : #include "storage/ipc.h"
                                 45                 :                : #include "storage/reinit.h"
                                 46                 :                : #include "utils/builtins.h"
                                 47                 :                : #include "utils/guc.h"
                                 48                 :                : #include "utils/ps_status.h"
                                 49                 :                : #include "utils/relcache.h"
                                 50                 :                : #include "utils/resowner.h"
                                 51                 :                : 
                                 52                 :                : /*
                                 53                 :                :  * How much data do we want to send in one CopyData message? Note that
                                 54                 :                :  * this may also result in reading the underlying files in chunks of this
                                 55                 :                :  * size.
                                 56                 :                :  *
                                 57                 :                :  * NB: The buffer size is required to be a multiple of the system block
                                 58                 :                :  * size, so use that value instead if it's bigger than our preference.
                                 59                 :                :  */
                                 60                 :                : #define SINK_BUFFER_LENGTH          Max(32768, BLCKSZ)
                                 61                 :                : 
                                 62                 :                : typedef struct
                                 63                 :                : {
                                 64                 :                :     const char *label;
                                 65                 :                :     bool        progress;
                                 66                 :                :     bool        fastcheckpoint;
                                 67                 :                :     bool        nowait;
                                 68                 :                :     bool        includewal;
                                 69                 :                :     bool        incremental;
                                 70                 :                :     uint32      maxrate;
                                 71                 :                :     bool        sendtblspcmapfile;
                                 72                 :                :     bool        send_to_client;
                                 73                 :                :     bool        use_copytblspc;
                                 74                 :                :     BaseBackupTargetHandle *target_handle;
                                 75                 :                :     backup_manifest_option manifest;
                                 76                 :                :     pg_compress_algorithm compression;
                                 77                 :                :     pg_compress_specification compression_specification;
                                 78                 :                :     pg_checksum_type manifest_checksum_type;
                                 79                 :                : } basebackup_options;
                                 80                 :                : 
                                 81                 :                : static int64 sendTablespace(bbsink *sink, char *path, Oid spcoid, bool sizeonly,
                                 82                 :                :                             struct backup_manifest_info *manifest,
                                 83                 :                :                             IncrementalBackupInfo *ib);
                                 84                 :                : static int64 sendDir(bbsink *sink, const char *path, int basepathlen, bool sizeonly,
                                 85                 :                :                      List *tablespaces, bool sendtblspclinks,
                                 86                 :                :                      backup_manifest_info *manifest, Oid spcoid,
                                 87                 :                :                      IncrementalBackupInfo *ib);
                                 88                 :                : static bool sendFile(bbsink *sink, const char *readfilename, const char *tarfilename,
                                 89                 :                :                      struct stat *statbuf, bool missing_ok,
                                 90                 :                :                      Oid dboid, Oid spcoid, RelFileNumber relfilenumber,
                                 91                 :                :                      unsigned segno,
                                 92                 :                :                      backup_manifest_info *manifest,
                                 93                 :                :                      unsigned num_incremental_blocks,
                                 94                 :                :                      BlockNumber *incremental_blocks,
                                 95                 :                :                      unsigned truncation_block_length);
                                 96                 :                : static off_t read_file_data_into_buffer(bbsink *sink,
                                 97                 :                :                                         const char *readfilename, int fd,
                                 98                 :                :                                         off_t offset, size_t length,
                                 99                 :                :                                         BlockNumber blkno,
                                100                 :                :                                         bool verify_checksum,
                                101                 :                :                                         int *checksum_failures);
                                102                 :                : static void push_to_sink(bbsink *sink, pg_checksum_context *checksum_ctx,
                                103                 :                :                          size_t *bytes_done, void *data, size_t length);
                                104                 :                : static bool verify_page_checksum(Page page, XLogRecPtr start_lsn,
                                105                 :                :                                  BlockNumber blkno,
                                106                 :                :                                  uint16 *expected_checksum);
                                107                 :                : static void sendFileWithContent(bbsink *sink, const char *filename,
                                108                 :                :                                 const char *content, int len,
                                109                 :                :                                 backup_manifest_info *manifest);
                                110                 :                : static int64 _tarWriteHeader(bbsink *sink, const char *filename,
                                111                 :                :                              const char *linktarget, struct stat *statbuf,
                                112                 :                :                              bool sizeonly);
                                113                 :                : static void _tarWritePadding(bbsink *sink, int len);
                                114                 :                : static void convert_link_to_directory(const char *pathbuf, struct stat *statbuf);
                                115                 :                : static void perform_base_backup(basebackup_options *opt, bbsink *sink,
                                116                 :                :                                 IncrementalBackupInfo *ib);
                                117                 :                : static void parse_basebackup_options(List *options, basebackup_options *opt);
                                118                 :                : static int  compareWalFileNames(const ListCell *a, const ListCell *b);
                                119                 :                : static ssize_t basebackup_read_file(int fd, char *buf, size_t nbytes, off_t offset,
                                120                 :                :                                     const char *filename, bool partial_read_ok);
                                121                 :                : 
                                122                 :                : /* Was the backup currently in-progress initiated in recovery mode? */
                                123                 :                : static bool backup_started_in_recovery = false;
                                124                 :                : 
                                125                 :                : /* Total number of checksum failures during base backup. */
                                126                 :                : static long long int total_checksum_failures;
                                127                 :                : 
                                128                 :                : /* Do not verify checksums. */
                                129                 :                : static bool noverify_checksums = false;
                                130                 :                : 
                                131                 :                : /*
                                132                 :                :  * Definition of one element part of an exclusion list, used for paths part
                                133                 :                :  * of checksum validation or base backups.  "name" is the name of the file
                                134                 :                :  * or path to check for exclusion.  If "match_prefix" is true, any items
                                135                 :                :  * matching the name as prefix are excluded.
                                136                 :                :  */
                                137                 :                : struct exclude_list_item
                                138                 :                : {
                                139                 :                :     const char *name;
                                140                 :                :     bool        match_prefix;
                                141                 :                : };
                                142                 :                : 
                                143                 :                : /*
                                144                 :                :  * The contents of these directories are removed or recreated during server
                                145                 :                :  * start so they are not included in backups.  The directories themselves are
                                146                 :                :  * kept and included as empty to preserve access permissions.
                                147                 :                :  *
                                148                 :                :  * Note: this list should be kept in sync with the filter lists in pg_rewind's
                                149                 :                :  * filemap.c.
                                150                 :                :  */
                                151                 :                : static const char *const excludeDirContents[] =
                                152                 :                : {
                                153                 :                :     /*
                                154                 :                :      * Skip temporary statistics files. PG_STAT_TMP_DIR must be skipped
                                155                 :                :      * because extensions like pg_stat_statements store data there.
                                156                 :                :      */
                                157                 :                :     PG_STAT_TMP_DIR,
                                158                 :                : 
                                159                 :                :     /*
                                160                 :                :      * It is generally not useful to backup the contents of this directory
                                161                 :                :      * even if the intention is to restore to another primary. See backup.sgml
                                162                 :                :      * for a more detailed description.
                                163                 :                :      */
                                164                 :                :     "pg_replslot",
                                165                 :                : 
                                166                 :                :     /* Contents removed on startup, see dsm_cleanup_for_mmap(). */
                                167                 :                :     PG_DYNSHMEM_DIR,
                                168                 :                : 
                                169                 :                :     /* Contents removed on startup, see AsyncShmemInit(). */
                                170                 :                :     "pg_notify",
                                171                 :                : 
                                172                 :                :     /*
                                173                 :                :      * Old contents are loaded for possible debugging but are not required for
                                174                 :                :      * normal operation, see SerialInit().
                                175                 :                :      */
                                176                 :                :     "pg_serial",
                                177                 :                : 
                                178                 :                :     /* Contents removed on startup, see DeleteAllExportedSnapshotFiles(). */
                                179                 :                :     "pg_snapshots",
                                180                 :                : 
                                181                 :                :     /* Contents zeroed on startup, see StartupSUBTRANS(). */
                                182                 :                :     "pg_subtrans",
                                183                 :                : 
                                184                 :                :     /* end of list */
                                185                 :                :     NULL
                                186                 :                : };
                                187                 :                : 
                                188                 :                : /*
                                189                 :                :  * List of files excluded from backups.
                                190                 :                :  */
                                191                 :                : static const struct exclude_list_item excludeFiles[] =
                                192                 :                : {
                                193                 :                :     /* Skip auto conf temporary file. */
                                194                 :                :     {PG_AUTOCONF_FILENAME ".tmp", false},
                                195                 :                : 
                                196                 :                :     /* Skip current log file temporary file */
                                197                 :                :     {LOG_METAINFO_DATAFILE_TMP, false},
                                198                 :                : 
                                199                 :                :     /*
                                200                 :                :      * Skip relation cache because it is rebuilt on startup.  This includes
                                201                 :                :      * temporary files.
                                202                 :                :      */
                                203                 :                :     {RELCACHE_INIT_FILENAME, true},
                                204                 :                : 
                                205                 :                :     /*
                                206                 :                :      * backup_label and tablespace_map should not exist in a running cluster
                                207                 :                :      * capable of doing an online backup, but exclude them just in case.
                                208                 :                :      */
                                209                 :                :     {BACKUP_LABEL_FILE, false},
                                210                 :                :     {TABLESPACE_MAP, false},
                                211                 :                : 
                                212                 :                :     /*
                                213                 :                :      * If there's a backup_manifest, it belongs to a backup that was used to
                                214                 :                :      * start this server. It is *not* correct for this backup. Our
                                215                 :                :      * backup_manifest is injected into the backup separately if users want
                                216                 :                :      * it.
                                217                 :                :      */
                                218                 :                :     {"backup_manifest", false},
                                219                 :                : 
                                220                 :                :     {"postmaster.pid", false},
                                221                 :                :     {"postmaster.opts", false},
                                222                 :                : 
                                223                 :                :     /* end of list */
                                224                 :                :     {NULL, false}
                                225                 :                : };
                                226                 :                : 
                                227                 :                : /*
                                228                 :                :  * Actually do a base backup for the specified tablespaces.
                                229                 :                :  *
                                230                 :                :  * This is split out mainly to avoid complaints about "variable might be
                                231                 :                :  * clobbered by longjmp" from stupider versions of gcc.
                                232                 :                :  */
                                233                 :                : static void
  116 rhaas@postgresql.org      234                 :GNC         149 : perform_base_backup(basebackup_options *opt, bbsink *sink,
                                235                 :                :                     IncrementalBackupInfo *ib)
                                236                 :                : {
                                237                 :                :     bbsink_state state;
                                238                 :                :     XLogRecPtr  endptr;
                                239                 :                :     TimeLineID  endtli;
                                240                 :                :     backup_manifest_info manifest;
                                241                 :                :     BackupState *backup_state;
                                242                 :                :     StringInfo  tablespace_map;
                                243                 :                : 
                                244                 :                :     /* Initial backup state, insofar as we know it now. */
  891 rhaas@postgresql.org      245                 :CBC         149 :     state.tablespaces = NIL;
                                246                 :            149 :     state.tablespace_num = 0;
                                247                 :            149 :     state.bytes_done = 0;
                                248                 :            149 :     state.bytes_total = 0;
                                249                 :            149 :     state.bytes_total_is_valid = false;
                                250                 :                : 
                                251                 :                :     /* we're going to use a BufFile, so we need a ResourceOwner */
 1472                           252         [ -  + ]:            149 :     Assert(CurrentResourceOwner == NULL);
                                253                 :            149 :     CurrentResourceOwner = ResourceOwnerCreate(NULL, "base backup");
                                254                 :                : 
 4140 heikki.linnakangas@i      255                 :            149 :     backup_started_in_recovery = RecoveryInProgress();
                                256                 :                : 
 1452 rhaas@postgresql.org      257                 :            149 :     InitializeBackupManifest(&manifest, opt->manifest,
                                258                 :                :                              opt->manifest_checksum_type);
                                259                 :                : 
 2203 magnus@hagander.net       260                 :            149 :     total_checksum_failures = 0;
                                261                 :                : 
                                262                 :                :     /* Allocate backup related variables. */
  566 michael@paquier.xyz       263                 :            149 :     backup_state = (BackupState *) palloc0(sizeof(BackupState));
                                264                 :            149 :     tablespace_map = makeStringInfo();
                                265                 :                : 
  891 rhaas@postgresql.org      266                 :            149 :     basebackup_progress_wait_checkpoint();
  566 michael@paquier.xyz       267                 :            149 :     do_pg_backup_start(opt->label, opt->fastcheckpoint, &state.tablespaces,
                                268                 :                :                        backup_state, tablespace_map);
                                269                 :                : 
                                270                 :            149 :     state.startptr = backup_state->startpoint;
                                271                 :            149 :     state.starttli = backup_state->starttli;
                                272                 :                : 
                                273                 :                :     /*
                                274                 :                :      * Once do_pg_backup_start has been called, ensure that any failure causes
                                275                 :                :      * us to abort the backup so we don't "leak" a backup counter. For this
                                276                 :                :      * reason, *all* functionality between do_pg_backup_start() and the end of
                                277                 :                :      * do_pg_backup_stop() should be inside the error cleanup block!
                                278                 :                :      */
                                279                 :                : 
 1578 rhaas@postgresql.org      280         [ +  + ]:            149 :     PG_ENSURE_ERROR_CLEANUP(do_pg_abort_backup, BoolGetDatum(false));
                                281                 :                :     {
                                282                 :                :         ListCell   *lc;
                                283                 :                :         tablespaceinfo *newti;
                                284                 :                : 
                                285                 :                :         /* If this is an incremental backup, execute preparatory steps. */
  116 rhaas@postgresql.org      286         [ +  + ]:GNC         149 :         if (ib != NULL)
                                287                 :              7 :             PrepareForIncrementalBackup(ib, backup_state);
                                288                 :                : 
                                289                 :                :         /* Add a node for the base directory at the end */
  557 drowley@postgresql.o      290                 :CBC         149 :         newti = palloc0(sizeof(tablespaceinfo));
                                291                 :            149 :         newti->size = -1;
                                292                 :            149 :         state.tablespaces = lappend(state.tablespaces, newti);
                                293                 :                : 
                                294                 :                :         /*
                                295                 :                :          * Calculate the total backup size by summing up the size of each
                                296                 :                :          * tablespace
                                297                 :                :          */
 1503 fujii@postgresql.org      298         [ +  - ]:            149 :         if (opt->progress)
                                299                 :                :         {
  891 rhaas@postgresql.org      300                 :            149 :             basebackup_progress_estimate_backup_size();
                                301                 :                : 
                                302   [ +  -  +  +  :            329 :             foreach(lc, state.tablespaces)
                                              +  + ]
                                303                 :                :             {
 1503 fujii@postgresql.org      304                 :            180 :                 tablespaceinfo *tmp = (tablespaceinfo *) lfirst(lc);
                                305                 :                : 
 1397 rhaas@postgresql.org      306         [ +  + ]:            180 :                 if (tmp->path == NULL)
  891                           307                 :            149 :                     tmp->size = sendDir(sink, ".", 1, true, state.tablespaces,
                                308                 :                :                                         true, NULL, InvalidOid, NULL);
                                309                 :                :                 else
                                310                 :             31 :                     tmp->size = sendTablespace(sink, tmp->path, tmp->oid, true,
                                311                 :                :                                                NULL, NULL);
                                312                 :            180 :                 state.bytes_total += tmp->size;
                                313                 :                :             }
                                314                 :            149 :             state.bytes_total_is_valid = true;
                                315                 :                :         }
                                316                 :                : 
                                317                 :                :         /* notify basebackup sink about start of backup */
                                318                 :            149 :         bbsink_begin_backup(sink, &state, SINK_BUFFER_LENGTH);
                                319                 :                : 
                                320                 :                :         /* Send off our tablespaces one by one */
                                321   [ +  -  +  +  :            323 :         foreach(lc, state.tablespaces)
                                              +  + ]
                                322                 :                :         {
 4842 tgl@sss.pgh.pa.us         323                 :            180 :             tablespaceinfo *ti = (tablespaceinfo *) lfirst(lc);
                                324                 :                : 
 4463 simon@2ndQuadrant.co      325         [ +  + ]:            180 :             if (ti->path == NULL)
                                326                 :                :             {
                                327                 :                :                 struct stat statbuf;
 1068 tgl@sss.pgh.pa.us         328                 :            149 :                 bool        sendtblspclinks = true;
                                329                 :                :                 char       *backup_label;
                                330                 :                : 
  891 rhaas@postgresql.org      331                 :            149 :                 bbsink_begin_archive(sink, "base.tar");
                                332                 :                : 
                                333                 :                :                 /* In the main tar, include the backup_label first... */
  566 michael@paquier.xyz       334                 :            149 :                 backup_label = build_backup_content(backup_state, false);
                                335                 :            149 :                 sendFileWithContent(sink, BACKUP_LABEL_FILE,
                                336                 :                :                                     backup_label, -1, &manifest);
                                337                 :            149 :                 pfree(backup_label);
                                338                 :                : 
                                339                 :                :                 /* Then the tablespace_map file, if required... */
 1397 rhaas@postgresql.org      340         [ +  + ]:            149 :                 if (opt->sendtblspcmapfile)
                                341                 :                :                 {
  566 michael@paquier.xyz       342                 :             24 :                     sendFileWithContent(sink, TABLESPACE_MAP,
  153 michael@paquier.xyz       343                 :GNC          24 :                                         tablespace_map->data, -1, &manifest);
 1397 rhaas@postgresql.org      344                 :CBC          24 :                     sendtblspclinks = false;
                                345                 :                :                 }
                                346                 :                : 
                                347                 :                :                 /* Then the bulk of the files... */
  891                           348                 :            149 :                 sendDir(sink, ".", 1, false, state.tablespaces,
                                349                 :                :                         sendtblspclinks, &manifest, InvalidOid, ib);
                                350                 :                : 
                                351                 :                :                 /* ... and pg_control after everything else. */
 4463 simon@2ndQuadrant.co      352         [ -  + ]:            143 :                 if (lstat(XLOG_CONTROL_FILE, &statbuf) != 0)
 4463 simon@2ndQuadrant.co      353         [ #  # ]:UBC           0 :                     ereport(ERROR,
                                354                 :                :                             (errcode_for_file_access(),
                                355                 :                :                              errmsg("could not stat file \"%s\": %m",
                                356                 :                :                                     XLOG_CONTROL_FILE)));
  891 rhaas@postgresql.org      357                 :CBC         143 :                 sendFile(sink, XLOG_CONTROL_FILE, XLOG_CONTROL_FILE, &statbuf,
                                358                 :                :                          false, InvalidOid, InvalidOid,
                                359                 :                :                          InvalidRelFileNumber, 0, &manifest, 0, NULL, 0);
                                360                 :                :             }
                                361                 :                :             else
                                362                 :                :             {
  174 rhaas@postgresql.org      363                 :GNC          31 :                 char       *archive_name = psprintf("%u.tar", ti->oid);
                                364                 :                : 
  891 rhaas@postgresql.org      365                 :CBC          31 :                 bbsink_begin_archive(sink, archive_name);
                                366                 :                : 
  116 rhaas@postgresql.org      367                 :GNC          31 :                 sendTablespace(sink, ti->path, ti->oid, false, &manifest, ib);
                                368                 :                :             }
                                369                 :                : 
                                370                 :                :             /*
                                371                 :                :              * If we're including WAL, and this is the main data directory we
                                372                 :                :              * don't treat this as the end of the tablespace. Instead, we will
                                373                 :                :              * include the xlog files below and stop afterwards. This is safe
                                374                 :                :              * since the main data directory is always sent *last*.
                                375                 :                :              */
 4823 magnus@hagander.net       376   [ +  +  +  - ]:CBC         174 :             if (opt->includewal && ti->path == NULL)
                                377                 :                :             {
  891 rhaas@postgresql.org      378         [ -  + ]:             20 :                 Assert(lnext(state.tablespaces, lc) == NULL);
                                379                 :                :             }
                                380                 :                :             else
                                381                 :                :             {
                                382                 :                :                 /* Properly terminate the tarfile. */
                                383                 :                :                 StaticAssertDecl(2 * TAR_BLOCK_SIZE <= BLCKSZ,
                                384                 :                :                                  "BLCKSZ too small for 2 tar blocks");
  887                           385                 :            154 :                 memset(sink->bbs_buffer, 0, 2 * TAR_BLOCK_SIZE);
                                386                 :            154 :                 bbsink_archive_contents(sink, 2 * TAR_BLOCK_SIZE);
                                387                 :                : 
                                388                 :                :                 /* OK, that's the end of the archive. */
  891                           389                 :            154 :                 bbsink_end_archive(sink);
                                390                 :                :             }
                                391                 :                :         }
                                392                 :                : 
                                393                 :            143 :         basebackup_progress_wait_wal_archive(&state);
  566 michael@paquier.xyz       394                 :            143 :         do_pg_backup_stop(backup_state, !opt->nowait);
                                395                 :                : 
                                396                 :            143 :         endptr = backup_state->stoppoint;
                                397                 :            143 :         endtli = backup_state->stoptli;
                                398                 :                : 
                                399                 :                :         /* Deallocate backup-related variables. */
   29 dgustafsson@postgres      400                 :GNC         143 :         destroyStringInfo(tablespace_map);
  566 michael@paquier.xyz       401                 :CBC         143 :         pfree(backup_state);
                                402                 :                :     }
 1578 rhaas@postgresql.org      403         [ -  + ]:            144 :     PG_END_ENSURE_ERROR_CLEANUP(do_pg_abort_backup, BoolGetDatum(false));
                                404                 :                : 
                                405                 :                : 
 4823 magnus@hagander.net       406         [ +  + ]:            143 :     if (opt->includewal)
                                407                 :                :     {
                                408                 :                :         /*
                                409                 :                :          * We've left the last tar file "open", so we can now append the
                                410                 :                :          * required WAL files to it.
                                411                 :                :          */
                                412                 :                :         char        pathbuf[MAXPGPATH];
                                413                 :                :         XLogSegNo   segno;
                                414                 :                :         XLogSegNo   startsegno;
                                415                 :                :         XLogSegNo   endsegno;
                                416                 :                :         struct stat statbuf;
 4119 heikki.linnakangas@i      417                 :             20 :         List       *historyFileList = NIL;
                                418                 :             20 :         List       *walFileList = NIL;
                                419                 :                :         char        firstoff[MAXFNAMELEN];
                                420                 :                :         char        lastoff[MAXFNAMELEN];
                                421                 :                :         DIR        *dir;
                                422                 :                :         struct dirent *de;
                                423                 :                :         ListCell   *lc;
                                424                 :                :         TimeLineID  tli;
                                425                 :                : 
  891 rhaas@postgresql.org      426                 :             20 :         basebackup_progress_transfer_wal();
                                427                 :                : 
                                428                 :                :         /*
                                429                 :                :          * I'd rather not worry about timelines here, so scan pg_wal and
                                430                 :                :          * include all WAL files in the range between 'startptr' and 'endptr',
                                431                 :                :          * regardless of the timeline the file is stamped with. If there are
                                432                 :                :          * some spurious WAL files belonging to timelines that don't belong in
                                433                 :                :          * this server's history, they will be included too. Normally there
                                434                 :                :          * shouldn't be such files, but if there are, there's little harm in
                                435                 :                :          * including them.
                                436                 :                :          */
                                437                 :             20 :         XLByteToSeg(state.startptr, startsegno, wal_segment_size);
                                438                 :             20 :         XLogFileName(firstoff, state.starttli, startsegno, wal_segment_size);
 2399 andres@anarazel.de        439                 :             20 :         XLByteToPrevSeg(endptr, endsegno, wal_segment_size);
  898 rhaas@postgresql.org      440                 :             20 :         XLogFileName(lastoff, endtli, endsegno, wal_segment_size);
                                441                 :                : 
 2733                           442                 :             20 :         dir = AllocateDir("pg_wal");
                                443         [ +  + ]:            143 :         while ((de = ReadDir(dir, "pg_wal")) != NULL)
                                444                 :                :         {
                                445                 :                :             /* Does it look like a WAL segment, and is it in the range? */
 3264 heikki.linnakangas@i      446         [ +  + ]:            123 :             if (IsXLogFileName(de->d_name) &&
 4119                           447         [ +  - ]:             43 :                 strcmp(de->d_name + 8, firstoff + 8) >= 0 &&
                                448         [ +  + ]:             43 :                 strcmp(de->d_name + 8, lastoff + 8) <= 0)
                                449                 :                :             {
                                450                 :             20 :                 walFileList = lappend(walFileList, pstrdup(de->d_name));
                                451                 :                :             }
                                452                 :                :             /* Does it look like a timeline history file? */
 3264                           453         [ -  + ]:            103 :             else if (IsTLHistoryFileName(de->d_name))
                                454                 :                :             {
 4119 heikki.linnakangas@i      455                 :UBC           0 :                 historyFileList = lappend(historyFileList, pstrdup(de->d_name));
                                456                 :                :             }
                                457                 :                :         }
 4119 heikki.linnakangas@i      458                 :CBC          20 :         FreeDir(dir);
                                459                 :                : 
                                460                 :                :         /*
                                461                 :                :          * Before we go any further, check that none of the WAL segments we
                                462                 :                :          * need were removed.
                                463                 :                :          */
  891 rhaas@postgresql.org      464                 :             20 :         CheckXLogRemoved(startsegno, state.starttli);
                                465                 :                : 
                                466                 :                :         /*
                                467                 :                :          * Sort the WAL filenames.  We want to send the files in order from
                                468                 :                :          * oldest to newest, to reduce the chance that a file is recycled
                                469                 :                :          * before we get a chance to send it over.
                                470                 :                :          */
 1734 tgl@sss.pgh.pa.us         471                 :             20 :         list_sort(walFileList, compareWalFileNames);
                                472                 :                : 
                                473                 :                :         /*
                                474                 :                :          * There must be at least one xlog file in the pg_wal directory, since
                                475                 :                :          * we are doing backup-including-xlog.
                                476                 :                :          */
                                477         [ -  + ]:             20 :         if (walFileList == NIL)
 3886 magnus@hagander.net       478         [ #  # ]:UBC           0 :             ereport(ERROR,
                                479                 :                :                     (errmsg("could not find any WAL files")));
                                480                 :                : 
                                481                 :                :         /*
                                482                 :                :          * Sanity check: the first and last segment should cover startptr and
                                483                 :                :          * endptr, with no gaps in between.
                                484                 :                :          */
 1734 tgl@sss.pgh.pa.us         485                 :CBC          20 :         XLogFromFileName((char *) linitial(walFileList),
                                486                 :                :                          &tli, &segno, wal_segment_size);
 4119 heikki.linnakangas@i      487         [ -  + ]:             20 :         if (segno != startsegno)
                                488                 :                :         {
                                489                 :                :             char        startfname[MAXFNAMELEN];
                                490                 :                : 
  891 rhaas@postgresql.org      491                 :UBC           0 :             XLogFileName(startfname, state.starttli, startsegno,
                                492                 :                :                          wal_segment_size);
 4119 heikki.linnakangas@i      493         [ #  # ]:              0 :             ereport(ERROR,
                                494                 :                :                     (errmsg("could not find WAL file \"%s\"", startfname)));
                                495                 :                :         }
 1734 tgl@sss.pgh.pa.us         496   [ +  -  +  +  :CBC          40 :         foreach(lc, walFileList)
                                              +  + ]
                                497                 :                :         {
                                498                 :             20 :             char       *walFileName = (char *) lfirst(lc);
 3973 bruce@momjian.us          499                 :             20 :             XLogSegNo   currsegno = segno;
                                500                 :             20 :             XLogSegNo   nextsegno = segno + 1;
                                501                 :                : 
 1734 tgl@sss.pgh.pa.us         502                 :             20 :             XLogFromFileName(walFileName, &tli, &segno, wal_segment_size);
 4119 heikki.linnakangas@i      503   [ +  -  -  + ]:             20 :             if (!(nextsegno == segno || currsegno == segno))
                                504                 :                :             {
                                505                 :                :                 char        nextfname[MAXFNAMELEN];
                                506                 :                : 
  898 rhaas@postgresql.org      507                 :UBC           0 :                 XLogFileName(nextfname, tli, nextsegno, wal_segment_size);
 4119 heikki.linnakangas@i      508         [ #  # ]:              0 :                 ereport(ERROR,
                                509                 :                :                         (errmsg("could not find WAL file \"%s\"", nextfname)));
                                510                 :                :             }
                                511                 :                :         }
 4119 heikki.linnakangas@i      512         [ -  + ]:CBC          20 :         if (segno != endsegno)
                                513                 :                :         {
                                514                 :                :             char        endfname[MAXFNAMELEN];
                                515                 :                : 
  898 rhaas@postgresql.org      516                 :UBC           0 :             XLogFileName(endfname, endtli, endsegno, wal_segment_size);
 4119 heikki.linnakangas@i      517         [ #  # ]:              0 :             ereport(ERROR,
                                518                 :                :                     (errmsg("could not find WAL file \"%s\"", endfname)));
                                519                 :                :         }
                                520                 :                : 
                                521                 :                :         /* Ok, we have everything we need. Send the WAL files. */
 1734 tgl@sss.pgh.pa.us         522   [ +  -  +  +  :CBC          40 :         foreach(lc, walFileList)
                                              +  + ]
                                523                 :                :         {
                                524                 :             20 :             char       *walFileName = (char *) lfirst(lc);
                                525                 :                :             int         fd;
                                526                 :                :             ssize_t     cnt;
 4119 heikki.linnakangas@i      527                 :             20 :             pgoff_t     len = 0;
                                528                 :                : 
 1734 tgl@sss.pgh.pa.us         529                 :             20 :             snprintf(pathbuf, MAXPGPATH, XLOGDIR "/%s", walFileName);
                                530                 :             20 :             XLogFromFileName(walFileName, &tli, &segno, wal_segment_size);
                                531                 :                : 
 1397 rhaas@postgresql.org      532                 :             20 :             fd = OpenTransientFile(pathbuf, O_RDONLY | PG_BINARY);
                                533         [ -  + ]:             20 :             if (fd < 0)
                                534                 :                :             {
 2120 michael@paquier.xyz       535                 :UBC           0 :                 int         save_errno = errno;
                                536                 :                : 
                                537                 :                :                 /*
                                538                 :                :                  * Most likely reason for this is that the file was already
                                539                 :                :                  * removed by a checkpoint, so check for that to get a better
                                540                 :                :                  * error message.
                                541                 :                :                  */
 4119 heikki.linnakangas@i      542                 :              0 :                 CheckXLogRemoved(segno, tli);
                                543                 :                : 
 2120 michael@paquier.xyz       544                 :              0 :                 errno = save_errno;
 4119 heikki.linnakangas@i      545         [ #  # ]:              0 :                 ereport(ERROR,
                                546                 :                :                         (errcode_for_file_access(),
                                547                 :                :                          errmsg("could not open file \"%s\": %m", pathbuf)));
                                548                 :                :             }
                                549                 :                : 
 1397 rhaas@postgresql.org      550         [ -  + ]:CBC          20 :             if (fstat(fd, &statbuf) != 0)
 4119 heikki.linnakangas@i      551         [ #  # ]:UBC           0 :                 ereport(ERROR,
                                552                 :                :                         (errcode_for_file_access(),
                                553                 :                :                          errmsg("could not stat file \"%s\": %m",
                                554                 :                :                                 pathbuf)));
 2399 andres@anarazel.de        555         [ -  + ]:CBC          20 :             if (statbuf.st_size != wal_segment_size)
                                556                 :                :             {
 4119 heikki.linnakangas@i      557                 :UBC           0 :                 CheckXLogRemoved(segno, tli);
                                558         [ #  # ]:              0 :                 ereport(ERROR,
                                559                 :                :                         (errcode_for_file_access(),
                                560                 :                :                          errmsg("unexpected WAL file size \"%s\"", walFileName)));
                                561                 :                :             }
                                562                 :                : 
                                563                 :                :             /* send the WAL file itself */
  891 rhaas@postgresql.org      564                 :CBC          20 :             _tarWriteHeader(sink, pathbuf, NULL, &statbuf, false);
                                565                 :                : 
                                566                 :             20 :             while ((cnt = basebackup_read_file(fd, sink->bbs_buffer,
                                567                 :          10240 :                                                Min(sink->bbs_buffer_length,
                                568                 :                :                                                    wal_segment_size - len),
 1397                           569         [ +  - ]:          10240 :                                                len, pathbuf, true)) > 0)
                                570                 :                :             {
 4119 heikki.linnakangas@i      571                 :          10240 :                 CheckXLogRemoved(segno, tli);
  891 rhaas@postgresql.org      572                 :          10240 :                 bbsink_archive_contents(sink, cnt);
                                573                 :                : 
 4119 heikki.linnakangas@i      574                 :          10240 :                 len += cnt;
                                575                 :                : 
 2399 andres@anarazel.de        576         [ +  + ]:          10240 :                 if (len == wal_segment_size)
 4119 heikki.linnakangas@i      577                 :             20 :                     break;
                                578                 :                :             }
                                579                 :                : 
 2399 andres@anarazel.de        580         [ -  + ]:             20 :             if (len != wal_segment_size)
                                581                 :                :             {
 4119 heikki.linnakangas@i      582                 :UBC           0 :                 CheckXLogRemoved(segno, tli);
                                583         [ #  # ]:              0 :                 ereport(ERROR,
                                584                 :                :                         (errcode_for_file_access(),
                                585                 :                :                          errmsg("unexpected WAL file size \"%s\"", walFileName)));
                                586                 :                :             }
                                587                 :                : 
                                588                 :                :             /*
                                589                 :                :              * wal_segment_size is a multiple of TAR_BLOCK_SIZE, so no need
                                590                 :                :              * for padding.
                                591                 :                :              */
 1451 rhaas@postgresql.org      592         [ -  + ]:CBC          20 :             Assert(wal_segment_size % TAR_BLOCK_SIZE == 0);
                                593                 :                : 
 1397                           594                 :             20 :             CloseTransientFile(fd);
                                595                 :                : 
                                596                 :                :             /*
                                597                 :                :              * Mark file as archived, otherwise files can get archived again
                                598                 :                :              * after promotion of a new node. This is in line with
                                599                 :                :              * walreceiver.c always doing an XLogArchiveForceDone() after a
                                600                 :                :              * complete segment.
                                601                 :                :              */
 1734 tgl@sss.pgh.pa.us         602                 :             20 :             StatusFilePath(pathbuf, walFileName, ".done");
  153 michael@paquier.xyz       603                 :GNC          20 :             sendFileWithContent(sink, pathbuf, "", -1, &manifest);
                                604                 :                :         }
                                605                 :                : 
                                606                 :                :         /*
                                607                 :                :          * Send timeline history files too. Only the latest timeline history
                                608                 :                :          * file is required for recovery, and even that only if there happens
                                609                 :                :          * to be a timeline switch in the first WAL segment that contains the
                                610                 :                :          * checkpoint record, or if we're taking a base backup from a standby
                                611                 :                :          * server and the target timeline changes while the backup is taken.
                                612                 :                :          * But they are small and highly useful for debugging purposes, so
                                613                 :                :          * better include them all, always.
                                614                 :                :          */
 4119 heikki.linnakangas@i      615   [ -  +  -  -  :CBC          20 :         foreach(lc, historyFileList)
                                              -  + ]
                                616                 :                :         {
 3973 bruce@momjian.us          617                 :UBC           0 :             char       *fname = lfirst(lc);
                                618                 :                : 
 4119 heikki.linnakangas@i      619                 :              0 :             snprintf(pathbuf, MAXPGPATH, XLOGDIR "/%s", fname);
                                620                 :                : 
                                621         [ #  # ]:              0 :             if (lstat(pathbuf, &statbuf) != 0)
                                622         [ #  # ]:              0 :                 ereport(ERROR,
                                623                 :                :                         (errcode_for_file_access(),
                                624                 :                :                          errmsg("could not stat file \"%s\": %m", pathbuf)));
                                625                 :                : 
  174 rhaas@postgresql.org      626                 :UNC           0 :             sendFile(sink, pathbuf, pathbuf, &statbuf, false,
                                627                 :                :                      InvalidOid, InvalidOid, InvalidRelFileNumber, 0,
                                628                 :                :                      &manifest, 0, NULL, 0);
                                629                 :                : 
                                630                 :                :             /* unconditionally mark file as archived */
 3389 andres@anarazel.de        631                 :UBC           0 :             StatusFilePath(pathbuf, fname, ".done");
  153 michael@paquier.xyz       632                 :UNC           0 :             sendFileWithContent(sink, pathbuf, "", -1, &manifest);
                                633                 :                :         }
                                634                 :                : 
                                635                 :                :         /* Properly terminate the tar file. */
                                636                 :                :         StaticAssertStmt(2 * TAR_BLOCK_SIZE <= BLCKSZ,
                                637                 :                :                          "BLCKSZ too small for 2 tar blocks");
  887 rhaas@postgresql.org      638                 :CBC          20 :         memset(sink->bbs_buffer, 0, 2 * TAR_BLOCK_SIZE);
                                639                 :             20 :         bbsink_archive_contents(sink, 2 * TAR_BLOCK_SIZE);
                                640                 :                : 
                                641                 :                :         /* OK, that's the end of the archive. */
  891                           642                 :             20 :         bbsink_end_archive(sink);
                                643                 :                :     }
                                644                 :                : 
                                645                 :            143 :     AddWALInfoToBackupManifest(&manifest, state.startptr, state.starttli,
                                646                 :                :                                endptr, endtli);
                                647                 :                : 
                                648                 :            143 :     SendBackupManifest(&manifest, sink);
                                649                 :                : 
                                650                 :            143 :     bbsink_end_backup(sink, endptr, endtli);
                                651                 :                : 
 2203 magnus@hagander.net       652         [ +  + ]:            143 :     if (total_checksum_failures)
                                653                 :                :     {
                                654         [ +  + ]:              3 :         if (total_checksum_failures > 1)
                                655         [ +  - ]:              2 :             ereport(WARNING,
                                656                 :                :                     (errmsg_plural("%lld total checksum verification failure",
                                657                 :                :                                    "%lld total checksum verification failures",
                                658                 :                :                                    total_checksum_failures,
                                659                 :                :                                    total_checksum_failures)));
                                660                 :                : 
                                661         [ +  - ]:              3 :         ereport(ERROR,
                                662                 :                :                 (errcode(ERRCODE_DATA_CORRUPTED),
                                663                 :                :                  errmsg("checksum verification failure during base backup")));
                                664                 :                :     }
                                665                 :                : 
                                666                 :                :     /*
                                667                 :                :      * Make sure to free the manifest before the resource owners as manifests
                                668                 :                :      * use cryptohash contexts that may depend on resource owners (like
                                669                 :                :      * OpenSSL).
                                670                 :                :      */
 1227 michael@paquier.xyz       671                 :            140 :     FreeBackupManifest(&manifest);
                                672                 :                : 
                                673                 :                :     /* clean up the resource owner we created */
 1472 rhaas@postgresql.org      674                 :            140 :     WalSndResourceCleanup(true);
                                675                 :                : 
  891                           676                 :            140 :     basebackup_progress_done();
 4842 tgl@sss.pgh.pa.us         677                 :            140 : }
                                678                 :                : 
                                679                 :                : /*
                                680                 :                :  * list_sort comparison function, to compare log/seg portion of WAL segment
                                681                 :                :  * filenames, ignoring the timeline portion.
                                682                 :                :  */
                                683                 :                : static int
 1734 tgl@sss.pgh.pa.us         684                 :UBC           0 : compareWalFileNames(const ListCell *a, const ListCell *b)
                                685                 :                : {
                                686                 :              0 :     char       *fna = (char *) lfirst(a);
                                687                 :              0 :     char       *fnb = (char *) lfirst(b);
                                688                 :                : 
 4119 heikki.linnakangas@i      689                 :              0 :     return strcmp(fna + 8, fnb + 8);
                                690                 :                : }
                                691                 :                : 
                                692                 :                : /*
                                693                 :                :  * Parse the base backup options passed down by the parser
                                694                 :                :  */
                                695                 :                : static void
 4830 magnus@hagander.net       696                 :CBC         165 : parse_basebackup_options(List *options, basebackup_options *opt)
                                697                 :                : {
                                698                 :                :     ListCell   *lopt;
                                699                 :            165 :     bool        o_label = false;
                                700                 :            165 :     bool        o_progress = false;
  922 rhaas@postgresql.org      701                 :            165 :     bool        o_checkpoint = false;
 4813 magnus@hagander.net       702                 :            165 :     bool        o_nowait = false;
 4823                           703                 :            165 :     bool        o_wal = false;
  116 rhaas@postgresql.org      704                 :GNC         165 :     bool        o_incremental = false;
 3699 alvherre@alvh.no-ip.      705                 :CBC         165 :     bool        o_maxrate = false;
 3260 andrew@dunslane.net       706                 :            165 :     bool        o_tablespace_map = false;
 2203 magnus@hagander.net       707                 :            165 :     bool        o_noverify_checksums = false;
 1472 rhaas@postgresql.org      708                 :            165 :     bool        o_manifest = false;
                                709                 :            165 :     bool        o_manifest_checksums = false;
  817                           710                 :            165 :     bool        o_target = false;
  880                           711                 :            165 :     bool        o_target_detail = false;
  761                           712                 :            165 :     char       *target_str = NULL;
                                713                 :            165 :     char       *target_detail_str = NULL;
  811                           714                 :            165 :     bool        o_compression = false;
  753                           715                 :            165 :     bool        o_compression_detail = false;
                                716                 :            165 :     char       *compression_detail_str = NULL;
                                717                 :                : 
 4828 magnus@hagander.net       718   [ +  -  +  -  :           1815 :     MemSet(opt, 0, sizeof(*opt));
                                     +  -  +  -  +  
                                                 + ]
 1472 rhaas@postgresql.org      719                 :            165 :     opt->manifest = MANIFEST_OPTION_NO;
                                720                 :            165 :     opt->manifest_checksum_type = CHECKSUM_TYPE_CRC32C;
  733 michael@paquier.xyz       721                 :            165 :     opt->compression = PG_COMPRESSION_NONE;
                                722                 :            165 :     opt->compression_specification.algorithm = PG_COMPRESSION_NONE;
                                723                 :                : 
 4830 magnus@hagander.net       724   [ +  -  +  +  :           1255 :     foreach(lopt, options)
                                              +  + ]
                                725                 :                :     {
                                726                 :           1092 :         DefElem    *defel = (DefElem *) lfirst(lopt);
                                727                 :                : 
                                728         [ +  + ]:           1092 :         if (strcmp(defel->defname, "label") == 0)
                                729                 :                :         {
                                730         [ -  + ]:            165 :             if (o_label)
 4830 magnus@hagander.net       731         [ #  # ]:UBC           0 :                 ereport(ERROR,
                                732                 :                :                         (errcode(ERRCODE_SYNTAX_ERROR),
                                733                 :                :                          errmsg("duplicate option \"%s\"", defel->defname)));
  922 rhaas@postgresql.org      734                 :CBC         165 :             opt->label = defGetString(defel);
 4830 magnus@hagander.net       735                 :            165 :             o_label = true;
                                736                 :                :         }
                                737         [ +  + ]:            927 :         else if (strcmp(defel->defname, "progress") == 0)
                                738                 :                :         {
                                739         [ -  + ]:            165 :             if (o_progress)
 4830 magnus@hagander.net       740         [ #  # ]:UBC           0 :                 ereport(ERROR,
                                741                 :                :                         (errcode(ERRCODE_SYNTAX_ERROR),
                                742                 :                :                          errmsg("duplicate option \"%s\"", defel->defname)));
  922 rhaas@postgresql.org      743                 :CBC         165 :             opt->progress = defGetBoolean(defel);
 4830 magnus@hagander.net       744                 :            165 :             o_progress = true;
                                745                 :                :         }
  922 rhaas@postgresql.org      746         [ +  + ]:            762 :         else if (strcmp(defel->defname, "checkpoint") == 0)
                                747                 :                :         {
                                748                 :            155 :             char       *optval = defGetString(defel);
                                749                 :                : 
                                750         [ -  + ]:            155 :             if (o_checkpoint)
 4830 magnus@hagander.net       751         [ #  # ]:UBC           0 :                 ereport(ERROR,
                                752                 :                :                         (errcode(ERRCODE_SYNTAX_ERROR),
                                753                 :                :                          errmsg("duplicate option \"%s\"", defel->defname)));
  922 rhaas@postgresql.org      754         [ +  - ]:CBC         155 :             if (pg_strcasecmp(optval, "fast") == 0)
                                755                 :            155 :                 opt->fastcheckpoint = true;
  922 rhaas@postgresql.org      756         [ #  # ]:UBC           0 :             else if (pg_strcasecmp(optval, "spread") == 0)
                                757                 :              0 :                 opt->fastcheckpoint = false;
                                758                 :                :             else
                                759         [ #  # ]:              0 :                 ereport(ERROR,
                                760                 :                :                         (errcode(ERRCODE_SYNTAX_ERROR),
                                761                 :                :                          errmsg("unrecognized checkpoint type: \"%s\"",
                                762                 :                :                                 optval)));
  922 rhaas@postgresql.org      763                 :CBC         155 :             o_checkpoint = true;
                                764                 :                :         }
                                765         [ +  + ]:            607 :         else if (strcmp(defel->defname, "wait") == 0)
                                766                 :                :         {
 4813 magnus@hagander.net       767         [ -  + ]:            158 :             if (o_nowait)
 4813 magnus@hagander.net       768         [ #  # ]:UBC           0 :                 ereport(ERROR,
                                769                 :                :                         (errcode(ERRCODE_SYNTAX_ERROR),
                                770                 :                :                          errmsg("duplicate option \"%s\"", defel->defname)));
  922 rhaas@postgresql.org      771                 :CBC         158 :             opt->nowait = !defGetBoolean(defel);
 4813 magnus@hagander.net       772                 :            158 :             o_nowait = true;
                                773                 :                :         }
 4823                           774         [ +  + ]:            449 :         else if (strcmp(defel->defname, "wal") == 0)
                                775                 :                :         {
                                776         [ -  + ]:             24 :             if (o_wal)
 4823 magnus@hagander.net       777         [ #  # ]:UBC           0 :                 ereport(ERROR,
                                778                 :                :                         (errcode(ERRCODE_SYNTAX_ERROR),
                                779                 :                :                          errmsg("duplicate option \"%s\"", defel->defname)));
  922 rhaas@postgresql.org      780                 :CBC          24 :             opt->includewal = defGetBoolean(defel);
 4823 magnus@hagander.net       781                 :             24 :             o_wal = true;
                                782                 :                :         }
  116 rhaas@postgresql.org      783         [ +  + ]:GNC         425 :         else if (strcmp(defel->defname, "incremental") == 0)
                                784                 :                :         {
                                785         [ -  + ]:              7 :             if (o_incremental)
  116 rhaas@postgresql.org      786         [ #  # ]:UNC           0 :                 ereport(ERROR,
                                787                 :                :                         (errcode(ERRCODE_SYNTAX_ERROR),
                                788                 :                :                          errmsg("duplicate option \"%s\"", defel->defname)));
  116 rhaas@postgresql.org      789                 :GNC           7 :             opt->incremental = defGetBoolean(defel);
                                790   [ +  -  -  + ]:              7 :             if (opt->incremental && !summarize_wal)
  116 rhaas@postgresql.org      791         [ #  # ]:UNC           0 :                 ereport(ERROR,
                                792                 :                :                         (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
                                793                 :                :                          errmsg("incremental backups cannot be taken unless WAL summarization is enabled")));
  116 rhaas@postgresql.org      794                 :GNC           7 :             o_incremental = true;
                                795                 :                :         }
 3699 alvherre@alvh.no-ip.      796         [ +  + ]:CBC         418 :         else if (strcmp(defel->defname, "max_rate") == 0)
                                797                 :                :         {
                                798                 :                :             int64       maxrate;
                                799                 :                : 
                                800         [ -  + ]:              1 :             if (o_maxrate)
 3699 alvherre@alvh.no-ip.      801         [ #  # ]:UBC           0 :                 ereport(ERROR,
                                802                 :                :                         (errcode(ERRCODE_SYNTAX_ERROR),
                                803                 :                :                          errmsg("duplicate option \"%s\"", defel->defname)));
                                804                 :                : 
  922 rhaas@postgresql.org      805                 :CBC           1 :             maxrate = defGetInt64(defel);
 3699 alvherre@alvh.no-ip.      806   [ +  -  -  + ]:              1 :             if (maxrate < MAX_RATE_LOWER || maxrate > MAX_RATE_UPPER)
 3699 alvherre@alvh.no-ip.      807         [ #  # ]:UBC           0 :                 ereport(ERROR,
                                808                 :                :                         (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
                                809                 :                :                          errmsg("%d is outside the valid range for parameter \"%s\" (%d .. %d)",
                                810                 :                :                                 (int) maxrate, "MAX_RATE", MAX_RATE_LOWER, MAX_RATE_UPPER)));
                                811                 :                : 
 3699 alvherre@alvh.no-ip.      812                 :CBC           1 :             opt->maxrate = (uint32) maxrate;
                                813                 :              1 :             o_maxrate = true;
                                814                 :                :         }
 3260 andrew@dunslane.net       815         [ +  + ]:            417 :         else if (strcmp(defel->defname, "tablespace_map") == 0)
                                816                 :                :         {
                                817         [ -  + ]:             29 :             if (o_tablespace_map)
 3260 andrew@dunslane.net       818         [ #  # ]:UBC           0 :                 ereport(ERROR,
                                819                 :                :                         (errcode(ERRCODE_SYNTAX_ERROR),
                                820                 :                :                          errmsg("duplicate option \"%s\"", defel->defname)));
  922 rhaas@postgresql.org      821                 :CBC          29 :             opt->sendtblspcmapfile = defGetBoolean(defel);
 3260 andrew@dunslane.net       822                 :             29 :             o_tablespace_map = true;
                                823                 :                :         }
  922 rhaas@postgresql.org      824         [ +  + ]:            388 :         else if (strcmp(defel->defname, "verify_checksums") == 0)
                                825                 :                :         {
 2203 magnus@hagander.net       826         [ -  + ]:              1 :             if (o_noverify_checksums)
 2203 magnus@hagander.net       827         [ #  # ]:UBC           0 :                 ereport(ERROR,
                                828                 :                :                         (errcode(ERRCODE_SYNTAX_ERROR),
                                829                 :                :                          errmsg("duplicate option \"%s\"", defel->defname)));
  922 rhaas@postgresql.org      830                 :CBC           1 :             noverify_checksums = !defGetBoolean(defel);
 2203 magnus@hagander.net       831                 :              1 :             o_noverify_checksums = true;
                                832                 :                :         }
 1472 rhaas@postgresql.org      833         [ +  + ]:            387 :         else if (strcmp(defel->defname, "manifest") == 0)
                                834                 :                :         {
  922                           835                 :            164 :             char       *optval = defGetString(defel);
                                836                 :                :             bool        manifest_bool;
                                837                 :                : 
 1472                           838         [ -  + ]:            164 :             if (o_manifest)
 1472 rhaas@postgresql.org      839         [ #  # ]:UBC           0 :                 ereport(ERROR,
                                840                 :                :                         (errcode(ERRCODE_SYNTAX_ERROR),
                                841                 :                :                          errmsg("duplicate option \"%s\"", defel->defname)));
 1472 rhaas@postgresql.org      842         [ +  + ]:CBC         164 :             if (parse_bool(optval, &manifest_bool))
                                843                 :                :             {
                                844         [ +  - ]:            163 :                 if (manifest_bool)
                                845                 :            163 :                     opt->manifest = MANIFEST_OPTION_YES;
                                846                 :                :                 else
 1472 rhaas@postgresql.org      847                 :UBC           0 :                     opt->manifest = MANIFEST_OPTION_NO;
                                848                 :                :             }
 1472 rhaas@postgresql.org      849         [ +  - ]:CBC           1 :             else if (pg_strcasecmp(optval, "force-encode") == 0)
                                850                 :              1 :                 opt->manifest = MANIFEST_OPTION_FORCE_ENCODE;
                                851                 :                :             else
 1472 rhaas@postgresql.org      852         [ #  # ]:UBC           0 :                 ereport(ERROR,
                                853                 :                :                         (errcode(ERRCODE_SYNTAX_ERROR),
                                854                 :                :                          errmsg("unrecognized manifest option: \"%s\"",
                                855                 :                :                                 optval)));
 1472 rhaas@postgresql.org      856                 :CBC         164 :             o_manifest = true;
                                857                 :                :         }
                                858         [ +  + ]:            223 :         else if (strcmp(defel->defname, "manifest_checksums") == 0)
                                859                 :                :         {
  922                           860                 :              7 :             char       *optval = defGetString(defel);
                                861                 :                : 
 1472                           862         [ -  + ]:              7 :             if (o_manifest_checksums)
 1472 rhaas@postgresql.org      863         [ #  # ]:UBC           0 :                 ereport(ERROR,
                                864                 :                :                         (errcode(ERRCODE_SYNTAX_ERROR),
                                865                 :                :                          errmsg("duplicate option \"%s\"", defel->defname)));
 1472 rhaas@postgresql.org      866         [ +  + ]:CBC           7 :             if (!pg_checksum_parse_type(optval,
                                867                 :                :                                         &opt->manifest_checksum_type))
                                868         [ +  - ]:              1 :                 ereport(ERROR,
                                869                 :                :                         (errcode(ERRCODE_SYNTAX_ERROR),
                                870                 :                :                          errmsg("unrecognized checksum algorithm: \"%s\"",
                                871                 :                :                                 optval)));
                                872                 :              6 :             o_manifest_checksums = true;
                                873                 :                :         }
  817                           874         [ +  + ]:            216 :         else if (strcmp(defel->defname, "target") == 0)
                                875                 :                :         {
                                876         [ -  + ]:            164 :             if (o_target)
  817 rhaas@postgresql.org      877         [ #  # ]:UBC           0 :                 ereport(ERROR,
                                878                 :                :                         (errcode(ERRCODE_SYNTAX_ERROR),
                                879                 :                :                          errmsg("duplicate option \"%s\"", defel->defname)));
  761 rhaas@postgresql.org      880                 :CBC         164 :             target_str = defGetString(defel);
  817                           881                 :            164 :             o_target = true;
                                882                 :                :         }
  880                           883         [ +  + ]:             52 :         else if (strcmp(defel->defname, "target_detail") == 0)
                                884                 :                :         {
                                885                 :              9 :             char       *optval = defGetString(defel);
                                886                 :                : 
                                887         [ -  + ]:              9 :             if (o_target_detail)
  880 rhaas@postgresql.org      888         [ #  # ]:UBC           0 :                 ereport(ERROR,
                                889                 :                :                         (errcode(ERRCODE_SYNTAX_ERROR),
                                890                 :                :                          errmsg("duplicate option \"%s\"", defel->defname)));
  761 rhaas@postgresql.org      891                 :CBC           9 :             target_detail_str = optval;
  880                           892                 :              9 :             o_target_detail = true;
                                893                 :                :         }
  811                           894         [ +  + ]:             43 :         else if (strcmp(defel->defname, "compression") == 0)
                                895                 :                :         {
                                896                 :             29 :             char       *optval = defGetString(defel);
                                897                 :                : 
                                898         [ -  + ]:             29 :             if (o_compression)
  811 rhaas@postgresql.org      899         [ #  # ]:UBC           0 :                 ereport(ERROR,
                                900                 :                :                         (errcode(ERRCODE_SYNTAX_ERROR),
                                901                 :                :                          errmsg("duplicate option \"%s\"", defel->defname)));
  733 michael@paquier.xyz       902         [ +  + ]:CBC          29 :             if (!parse_compress_algorithm(optval, &opt->compression))
  811 rhaas@postgresql.org      903         [ +  - ]:              1 :                 ereport(ERROR,
                                904                 :                :                         (errcode(ERRCODE_SYNTAX_ERROR),
                                905                 :                :                          errmsg("unrecognized compression algorithm: \"%s\"",
                                906                 :                :                                 optval)));
                                907                 :             28 :             o_compression = true;
                                908                 :                :         }
  753                           909         [ +  - ]:             14 :         else if (strcmp(defel->defname, "compression_detail") == 0)
                                910                 :                :         {
                                911         [ -  + ]:             14 :             if (o_compression_detail)
  811 rhaas@postgresql.org      912         [ #  # ]:UBC           0 :                 ereport(ERROR,
                                913                 :                :                         (errcode(ERRCODE_SYNTAX_ERROR),
                                914                 :                :                          errmsg("duplicate option \"%s\"", defel->defname)));
  753 rhaas@postgresql.org      915                 :CBC          14 :             compression_detail_str = defGetString(defel);
                                916                 :             14 :             o_compression_detail = true;
                                917                 :                :         }
                                918                 :                :         else
  811 rhaas@postgresql.org      919         [ #  # ]:UBC           0 :             ereport(ERROR,
                                920                 :                :                     (errcode(ERRCODE_SYNTAX_ERROR),
                                921                 :                :                      errmsg("unrecognized base backup option: \"%s\"",
                                922                 :                :                             defel->defname)));
                                923                 :                :     }
                                924                 :                : 
 4830 magnus@hagander.net       925         [ -  + ]:CBC         163 :     if (opt->label == NULL)
 4830 magnus@hagander.net       926                 :UBC           0 :         opt->label = "base backup";
 1472 rhaas@postgresql.org      927         [ +  + ]:CBC         163 :     if (opt->manifest == MANIFEST_OPTION_NO)
                                928                 :                :     {
                                929         [ -  + ]:              1 :         if (o_manifest_checksums)
 1472 rhaas@postgresql.org      930         [ #  # ]:UBC           0 :             ereport(ERROR,
                                931                 :                :                     (errcode(ERRCODE_SYNTAX_ERROR),
                                932                 :                :                      errmsg("manifest checksums require a backup manifest")));
 1472 rhaas@postgresql.org      933                 :CBC           1 :         opt->manifest_checksum_type = CHECKSUM_TYPE_NONE;
                                934                 :                :     }
                                935                 :                : 
  761                           936         [ -  + ]:            163 :     if (target_str == NULL)
                                937                 :                :     {
  761 rhaas@postgresql.org      938         [ #  # ]:UBC           0 :         if (target_detail_str != NULL)
  880                           939         [ #  # ]:              0 :             ereport(ERROR,
                                940                 :                :                     (errcode(ERRCODE_SYNTAX_ERROR),
                                941                 :                :                      errmsg("target detail cannot be used without target")));
  761                           942                 :              0 :         opt->use_copytblspc = true;
                                943                 :              0 :         opt->send_to_client = true;
                                944                 :                :     }
  761 rhaas@postgresql.org      945         [ +  + ]:CBC         163 :     else if (strcmp(target_str, "client") == 0)
                                946                 :                :     {
                                947         [ -  + ]:            148 :         if (target_detail_str != NULL)
  880 rhaas@postgresql.org      948         [ #  # ]:UBC           0 :             ereport(ERROR,
                                949                 :                :                     (errcode(ERRCODE_SYNTAX_ERROR),
                                950                 :                :                      errmsg("target \"%s\" does not accept a target detail",
                                951                 :                :                             target_str)));
  761 rhaas@postgresql.org      952                 :CBC         148 :         opt->send_to_client = true;
                                953                 :                :     }
                                954                 :                :     else
                                955                 :             13 :         opt->target_handle =
                                956                 :             15 :             BaseBackupGetTargetHandle(target_str, target_detail_str);
                                957                 :                : 
  753                           958   [ +  +  -  + ]:            161 :     if (o_compression_detail && !o_compression)
  811 rhaas@postgresql.org      959         [ #  # ]:UBC           0 :         ereport(ERROR,
                                960                 :                :                 (errcode(ERRCODE_SYNTAX_ERROR),
                                961                 :                :                  errmsg("compression detail cannot be specified unless compression is enabled")));
                                962                 :                : 
  753 rhaas@postgresql.org      963         [ +  + ]:CBC         161 :     if (o_compression)
                                964                 :                :     {
                                965                 :                :         char       *error_detail;
                                966                 :                : 
  733 michael@paquier.xyz       967                 :             26 :         parse_compress_specification(opt->compression, compression_detail_str,
                                968                 :                :                                      &opt->compression_specification);
                                969                 :                :         error_detail =
                                970                 :             26 :             validate_compress_specification(&opt->compression_specification);
  753 rhaas@postgresql.org      971         [ +  + ]:             26 :         if (error_detail != NULL)
                                972         [ +  - ]:              9 :             ereport(ERROR,
                                973                 :                :                     errcode(ERRCODE_SYNTAX_ERROR),
                                974                 :                :                     errmsg("invalid compression specification: %s",
                                975                 :                :                            error_detail));
                                976                 :                :     }
 4830 magnus@hagander.net       977                 :            152 : }
                                978                 :                : 
                                979                 :                : 
                                980                 :                : /*
                                981                 :                :  * SendBaseBackup() - send a complete base backup.
                                982                 :                :  *
                                983                 :                :  * The function will put the system into backup mode like pg_backup_start()
                                984                 :                :  * does, so that the backup is consistent even though we read directly from
                                985                 :                :  * the filesystem, bypassing the buffer cache.
                                986                 :                :  */
                                987                 :                : void
  116 rhaas@postgresql.org      988                 :GNC         166 : SendBaseBackup(BaseBackupCmd *cmd, IncrementalBackupInfo *ib)
                                989                 :                : {
                                990                 :                :     basebackup_options opt;
                                991                 :                :     bbsink     *sink;
  642 fujii@postgresql.org      992                 :CBC         166 :     SessionBackupState status = get_backup_status();
                                993                 :                : 
                                994         [ +  + ]:            166 :     if (status == SESSION_BACKUP_RUNNING)
                                995         [ +  - ]:              1 :         ereport(ERROR,
                                996                 :                :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
                                997                 :                :                  errmsg("a backup is already in progress in this session")));
                                998                 :                : 
 4830 magnus@hagander.net       999                 :            165 :     parse_basebackup_options(cmd->options, &opt);
                               1000                 :                : 
 4842                          1001                 :            152 :     WalSndSetState(WALSNDSTATE_BACKUP);
                               1002                 :                : 
 4843                          1003         [ +  - ]:            152 :     if (update_process_title)
                               1004                 :                :     {
                               1005                 :                :         char        activitymsg[50];
                               1006                 :                : 
                               1007                 :            152 :         snprintf(activitymsg, sizeof(activitymsg), "sending backup \"%s\"",
                               1008                 :                :                  opt.label);
 1495 peter@eisentraut.org     1009                 :            152 :         set_ps_display(activitymsg);
                               1010                 :                :     }
                               1011                 :                : 
                               1012                 :                :     /*
                               1013                 :                :      * If we're asked to perform an incremental backup and the user has not
                               1014                 :                :      * supplied a manifest, that's an ERROR.
                               1015                 :                :      *
                               1016                 :                :      * If we're asked to perform a full backup and the user did supply a
                               1017                 :                :      * manifest, just ignore it.
                               1018                 :                :      */
  116 rhaas@postgresql.org     1019         [ +  + ]:GNC         152 :     if (!opt.incremental)
                               1020                 :            145 :         ib = NULL;
                               1021         [ -  + ]:              7 :     else if (ib == NULL)
  116 rhaas@postgresql.org     1022         [ #  # ]:UNC           0 :         ereport(ERROR,
                               1023                 :                :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
                               1024                 :                :                  errmsg("must UPLOAD_MANIFEST before performing an incremental BASE_BACKUP")));
                               1025                 :                : 
                               1026                 :                :     /*
                               1027                 :                :      * If the target is specifically 'client' then set up to stream the backup
                               1028                 :                :      * to the client; otherwise, it's being sent someplace else and should not
                               1029                 :                :      * be sent to the client. BaseBackupGetSink has the job of setting up a
                               1030                 :                :      * sink to send the backup data wherever it needs to go.
                               1031                 :                :      */
  761 rhaas@postgresql.org     1032                 :CBC         152 :     sink = bbsink_copystream_new(opt.send_to_client);
                               1033         [ +  + ]:            152 :     if (opt.target_handle != NULL)
                               1034                 :             13 :         sink = BaseBackupGetSink(opt.target_handle, sink);
                               1035                 :                : 
                               1036                 :                :     /* Set up network throttling, if client requested it */
  891                          1037         [ +  + ]:            149 :     if (opt.maxrate > 0)
                               1038                 :              1 :         sink = bbsink_throttle_new(sink, opt.maxrate);
                               1039                 :                : 
                               1040                 :                :     /* Set up server-side compression, if client requested it */
  733 michael@paquier.xyz      1041         [ +  + ]:            149 :     if (opt.compression == PG_COMPRESSION_GZIP)
  753 rhaas@postgresql.org     1042                 :              2 :         sink = bbsink_gzip_new(sink, &opt.compression_specification);
  733 michael@paquier.xyz      1043         [ +  + ]:            147 :     else if (opt.compression == PG_COMPRESSION_LZ4)
  753 rhaas@postgresql.org     1044                 :              2 :         sink = bbsink_lz4_new(sink, &opt.compression_specification);
  733 michael@paquier.xyz      1045         [ +  + ]:            145 :     else if (opt.compression == PG_COMPRESSION_ZSTD)
  753 rhaas@postgresql.org     1046                 :              4 :         sink = bbsink_zstd_new(sink, &opt.compression_specification);
                               1047                 :                : 
                               1048                 :                :     /* Set up progress reporting. */
  891                          1049                 :            149 :     sink = bbsink_progress_new(sink, opt.progress);
                               1050                 :                : 
                               1051                 :                :     /*
                               1052                 :                :      * Perform the base backup, but make sure we clean up the bbsink even if
                               1053                 :                :      * an error occurs.
                               1054                 :                :      */
                               1055         [ +  + ]:            149 :     PG_TRY();
                               1056                 :                :     {
  116 rhaas@postgresql.org     1057                 :GNC         149 :         perform_base_backup(&opt, sink, ib);
                               1058                 :                :     }
  891 rhaas@postgresql.org     1059                 :CBC           4 :     PG_FINALLY();
                               1060                 :                :     {
                               1061                 :            144 :         bbsink_cleanup(sink);
                               1062                 :                :     }
                               1063         [ +  + ]:            144 :     PG_END_TRY();
 4819 magnus@hagander.net      1064                 :            140 : }
                               1065                 :                : 
                               1066                 :                : /*
                               1067                 :                :  * Inject a file with given name and content in the output tar stream.
                               1068                 :                :  *
                               1069                 :                :  * "len" can optionally be set to an arbitrary length of data sent.  If set
                               1070                 :                :  * to -1, the content sent is treated as a string with strlen() as length.
                               1071                 :                :  */
                               1072                 :                : static void
  891 rhaas@postgresql.org     1073                 :            193 : sendFileWithContent(bbsink *sink, const char *filename, const char *content,
                               1074                 :                :                     int len, backup_manifest_info *manifest)
                               1075                 :                : {
                               1076                 :                :     struct stat statbuf;
  153 michael@paquier.xyz      1077                 :GNC         193 :     int         bytes_done = 0;
                               1078                 :                :     pg_checksum_context checksum_ctx;
                               1079                 :                : 
 1229 michael@paquier.xyz      1080         [ -  + ]:CBC         193 :     if (pg_checksum_init(&checksum_ctx, manifest->checksum_type) < 0)
 1229 michael@paquier.xyz      1081         [ #  # ]:UBC           0 :         elog(ERROR, "could not initialize checksum of file \"%s\"",
                               1082                 :                :              filename);
                               1083                 :                : 
  153 michael@paquier.xyz      1084         [ +  - ]:GNC         193 :     if (len < 0)
                               1085                 :            193 :         len = strlen(content);
                               1086                 :                : 
                               1087                 :                :     /*
                               1088                 :                :      * Construct a stat struct for the file we're injecting in the tar.
                               1089                 :                :      */
                               1090                 :                : 
                               1091                 :                :     /* Windows doesn't have the concept of uid and gid */
                               1092                 :                : #ifdef WIN32
                               1093                 :                :     statbuf.st_uid = 0;
                               1094                 :                :     statbuf.st_gid = 0;
                               1095                 :                : #else
 4822 heikki.linnakangas@i     1096                 :CBC         193 :     statbuf.st_uid = geteuid();
                               1097                 :            193 :     statbuf.st_gid = getegid();
                               1098                 :                : #endif
                               1099                 :            193 :     statbuf.st_mtime = time(NULL);
 2199 sfrost@snowman.net       1100                 :            193 :     statbuf.st_mode = pg_file_create_mode;
 4822 heikki.linnakangas@i     1101                 :            193 :     statbuf.st_size = len;
                               1102                 :                : 
  891 rhaas@postgresql.org     1103                 :            193 :     _tarWriteHeader(sink, filename, NULL, &statbuf, false);
                               1104                 :                : 
                               1105         [ -  + ]:            193 :     if (pg_checksum_update(&checksum_ctx, (uint8 *) content, len) < 0)
  891 rhaas@postgresql.org     1106         [ #  # ]:UBC           0 :         elog(ERROR, "could not update checksum of file \"%s\"",
                               1107                 :                :              filename);
                               1108                 :                : 
  891 rhaas@postgresql.org     1109         [ +  + ]:CBC         344 :     while (bytes_done < len)
                               1110                 :                :     {
                               1111                 :            151 :         size_t      remaining = len - bytes_done;
                               1112                 :            151 :         size_t      nbytes = Min(sink->bbs_buffer_length, remaining);
                               1113                 :                : 
                               1114                 :            151 :         memcpy(sink->bbs_buffer, content, nbytes);
                               1115                 :            151 :         bbsink_archive_contents(sink, nbytes);
                               1116                 :            151 :         bytes_done += nbytes;
  489                          1117                 :            151 :         content += nbytes;
                               1118                 :                :     }
                               1119                 :                : 
  891                          1120                 :            193 :     _tarWritePadding(sink, len);
                               1121                 :                : 
  174 rhaas@postgresql.org     1122                 :GNC         193 :     AddFileToBackupManifest(manifest, InvalidOid, filename, len,
 1452 rhaas@postgresql.org     1123                 :CBC         193 :                             (pg_time_t) statbuf.st_mtime, &checksum_ctx);
 4822 heikki.linnakangas@i     1124                 :            193 : }
                               1125                 :                : 
                               1126                 :                : /*
                               1127                 :                :  * Include the tablespace directory pointed to by 'path' in the output tar
                               1128                 :                :  * stream.  If 'sizeonly' is true, we just calculate a total length and return
                               1129                 :                :  * it, without actually sending anything.
                               1130                 :                :  *
                               1131                 :                :  * Only used to send auxiliary tablespaces, not PGDATA.
                               1132                 :                :  */
                               1133                 :                : static int64
  174 rhaas@postgresql.org     1134                 :GNC          62 : sendTablespace(bbsink *sink, char *path, Oid spcoid, bool sizeonly,
                               1135                 :                :                backup_manifest_info *manifest, IncrementalBackupInfo *ib)
                               1136                 :                : {
                               1137                 :                :     int64       size;
                               1138                 :                :     char        pathbuf[MAXPGPATH];
                               1139                 :                :     struct stat statbuf;
                               1140                 :                : 
                               1141                 :                :     /*
                               1142                 :                :      * 'path' points to the tablespace location, but we only want to include
                               1143                 :                :      * the version directory in it that belongs to us.
                               1144                 :                :      */
 4038 heikki.linnakangas@i     1145                 :CBC          62 :     snprintf(pathbuf, sizeof(pathbuf), "%s/%s", path,
                               1146                 :                :              TABLESPACE_VERSION_DIRECTORY);
                               1147                 :                : 
                               1148                 :                :     /*
                               1149                 :                :      * Store a directory entry in the tar file so we get the permissions
                               1150                 :                :      * right.
                               1151                 :                :      */
                               1152         [ -  + ]:             62 :     if (lstat(pathbuf, &statbuf) != 0)
                               1153                 :                :     {
 4038 heikki.linnakangas@i     1154         [ #  # ]:UBC           0 :         if (errno != ENOENT)
                               1155         [ #  # ]:              0 :             ereport(ERROR,
                               1156                 :                :                     (errcode_for_file_access(),
                               1157                 :                :                      errmsg("could not stat file or directory \"%s\": %m",
                               1158                 :                :                             pathbuf)));
                               1159                 :                : 
                               1160                 :                :         /* If the tablespace went away while scanning, it's no error. */
                               1161                 :              0 :         return 0;
                               1162                 :                :     }
                               1163                 :                : 
  891 rhaas@postgresql.org     1164                 :CBC          62 :     size = _tarWriteHeader(sink, TABLESPACE_VERSION_DIRECTORY, NULL, &statbuf,
                               1165                 :                :                            sizeonly);
                               1166                 :                : 
                               1167                 :                :     /* Send all the files in the tablespace version directory */
                               1168                 :             62 :     size += sendDir(sink, pathbuf, strlen(path), sizeonly, NIL, true, manifest,
                               1169                 :                :                     spcoid, ib);
                               1170                 :                : 
 4038 heikki.linnakangas@i     1171                 :             62 :     return size;
                               1172                 :                : }
                               1173                 :                : 
                               1174                 :                : /*
                               1175                 :                :  * Include all files from the given directory in the output tar stream. If
                               1176                 :                :  * 'sizeonly' is true, we just calculate a total length and return it, without
                               1177                 :                :  * actually sending anything.
                               1178                 :                :  *
                               1179                 :                :  * Omit any directory in the tablespaces list, to avoid backing up
                               1180                 :                :  * tablespaces twice when they were created inside PGDATA.
                               1181                 :                :  *
                               1182                 :                :  * If sendtblspclinks is true, we need to include symlink
                               1183                 :                :  * information in the tar file. If not, we can skip that
                               1184                 :                :  * as it will be sent separately in the tablespace_map file.
                               1185                 :                :  */
                               1186                 :                : static int64
  891 rhaas@postgresql.org     1187                 :           5107 : sendDir(bbsink *sink, const char *path, int basepathlen, bool sizeonly,
                               1188                 :                :         List *tablespaces, bool sendtblspclinks, backup_manifest_info *manifest,
                               1189                 :                :         Oid spcoid, IncrementalBackupInfo *ib)
                               1190                 :                : {
                               1191                 :                :     DIR        *dir;
                               1192                 :                :     struct dirent *de;
                               1193                 :                :     char        pathbuf[MAXPGPATH * 2];
                               1194                 :                :     struct stat statbuf;
 4843 magnus@hagander.net      1195                 :           5107 :     int64       size = 0;
                               1196                 :                :     const char *lastDir;        /* Split last dir from parent path. */
  152 rhaas@postgresql.org     1197                 :GNC        5107 :     bool        isRelationDir = false;  /* Does directory contain relations? */
  116                          1198                 :           5107 :     bool        isGlobalDir = false;
  152                          1199                 :           5107 :     Oid         dboid = InvalidOid;
  116                          1200                 :           5107 :     BlockNumber *relative_block_numbers = NULL;
                               1201                 :                : 
                               1202                 :                :     /*
                               1203                 :                :      * Since this array is relatively large, avoid putting it on the stack.
                               1204                 :                :      * But we don't need it at all if this is not an incremental backup.
                               1205                 :                :      */
                               1206         [ +  + ]:           5107 :     if (ib != NULL)
                               1207                 :            121 :         relative_block_numbers = palloc(sizeof(BlockNumber) * RELSEG_SIZE);
                               1208                 :                : 
                               1209                 :                :     /*
                               1210                 :                :      * Determine if the current path is a database directory that can contain
                               1211                 :                :      * relations.
                               1212                 :                :      *
                               1213                 :                :      * Start by finding the location of the delimiter between the parent path
                               1214                 :                :      * and the current path.
                               1215                 :                :      */
 2214 teodor@sigaev.ru         1216                 :CBC        5107 :     lastDir = last_dir_separator(path);
                               1217                 :                : 
                               1218                 :                :     /* Does this path look like a database path (i.e. all digits)? */
                               1219         [ +  + ]:           5107 :     if (lastDir != NULL &&
                               1220         [ +  + ]:           4809 :         strspn(lastDir + 1, "0123456789") == strlen(lastDir + 1))
 2214 teodor@sigaev.ru         1221                 :GIC         949 :     {
                               1222                 :                :         /* Part of path that contains the parent directory. */
 2180 tgl@sss.pgh.pa.us        1223                 :CBC         949 :         int         parentPathLen = lastDir - path;
                               1224                 :                : 
                               1225                 :                :         /*
                               1226                 :                :          * Mark path as a database directory if the parent path is either
                               1227                 :                :          * $PGDATA/base or a tablespace version path.
                               1228                 :                :          */
 2214 teodor@sigaev.ru         1229         [ +  + ]:            949 :         if (strncmp(path, "./base", parentPathLen) == 0 ||
                               1230         [ +  - ]:             40 :             (parentPathLen >= (sizeof(TABLESPACE_VERSION_DIRECTORY) - 1) &&
                               1231         [ +  - ]:             40 :              strncmp(lastDir - (sizeof(TABLESPACE_VERSION_DIRECTORY) - 1),
                               1232                 :                :                      TABLESPACE_VERSION_DIRECTORY,
                               1233                 :                :                      sizeof(TABLESPACE_VERSION_DIRECTORY) - 1) == 0))
                               1234                 :                :         {
  152 rhaas@postgresql.org     1235                 :GNC         949 :             isRelationDir = true;
                               1236                 :            949 :             dboid = atooid(lastDir + 1);
                               1237                 :                :         }
                               1238                 :                :     }
                               1239         [ +  + ]:           4158 :     else if (strcmp(path, "./global") == 0)
                               1240                 :                :     {
                               1241                 :            298 :         isRelationDir = true;
  116                          1242                 :            298 :         isGlobalDir = true;
                               1243                 :                :     }
                               1244                 :                : 
 4843 magnus@hagander.net      1245                 :CBC        5107 :     dir = AllocateDir(path);
                               1246         [ +  + ]:         317088 :     while ((de = ReadDir(dir, path)) != NULL)
                               1247                 :                :     {
                               1248                 :                :         int         excludeIdx;
                               1249                 :                :         bool        excludeFound;
  152 rhaas@postgresql.org     1250                 :GNC      311992 :         RelFileNumber relfilenumber = InvalidRelFileNumber;
                               1251                 :         311992 :         ForkNumber  relForkNum = InvalidForkNumber;
                               1252                 :         311992 :         unsigned    segno = 0;
                               1253                 :         311992 :         bool        isRelationFile = false;
                               1254                 :                : 
                               1255                 :                :         /* Skip special stuff */
 4843 magnus@hagander.net      1256   [ +  +  +  + ]:CBC      311992 :         if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0)
                               1257                 :          14493 :             continue;
                               1258                 :                : 
                               1259                 :                :         /* Skip temporary files */
      heikki.linnakangas@i     1260         [ +  + ]:         301778 :         if (strncmp(de->d_name,
                               1261                 :                :                     PG_TEMP_FILE_PREFIX,
                               1262                 :                :                     strlen(PG_TEMP_FILE_PREFIX)) == 0)
                               1263                 :            293 :             continue;
                               1264                 :                : 
                               1265                 :                :         /* Skip macOS system files */
   61 dgustafsson@postgres     1266         [ +  + ]:         301485 :         if (strcmp(de->d_name, ".DS_Store") == 0)
                               1267                 :             67 :             continue;
                               1268                 :                : 
                               1269                 :                :         /*
                               1270                 :                :          * Check if the postmaster has signaled us to exit, and abort with an
                               1271                 :                :          * error in that case. The error handler further up will call
                               1272                 :                :          * do_pg_abort_backup() for us. Also check that if the backup was
                               1273                 :                :          * started while still in recovery, the server wasn't promoted.
                               1274                 :                :          * do_pg_backup_stop() will check that too, but it's better to stop
                               1275                 :                :          * the backup early than continue to the end and fail there.
                               1276                 :                :          */
 4140 heikki.linnakangas@i     1277         [ +  + ]:         301418 :         CHECK_FOR_INTERRUPTS();
                               1278         [ -  + ]:         301413 :         if (RecoveryInProgress() != backup_started_in_recovery)
 4839 magnus@hagander.net      1279         [ #  # ]:UBC           0 :             ereport(ERROR,
                               1280                 :                :                     (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
                               1281                 :                :                      errmsg("the standby was promoted during online backup"),
                               1282                 :                :                      errhint("This means that the backup being taken is corrupt "
                               1283                 :                :                              "and should not be used. "
                               1284                 :                :                              "Try taking another online backup.")));
                               1285                 :                : 
                               1286                 :                :         /* Scan for files that should be excluded */
 2755 peter_e@gmx.net          1287                 :CBC      301413 :         excludeFound = false;
 1511 michael@paquier.xyz      1288         [ +  + ]:        2708253 :         for (excludeIdx = 0; excludeFiles[excludeIdx].name != NULL; excludeIdx++)
                               1289                 :                :         {
                               1290                 :        2408001 :             int         cmplen = strlen(excludeFiles[excludeIdx].name);
                               1291                 :                : 
                               1292         [ +  + ]:        2408001 :             if (!excludeFiles[excludeIdx].match_prefix)
                               1293                 :        2106722 :                 cmplen++;
                               1294         [ +  + ]:        2408001 :             if (strncmp(de->d_name, excludeFiles[excludeIdx].name, cmplen) == 0)
                               1295                 :                :             {
 2755 peter_e@gmx.net          1296         [ +  + ]:           1161 :                 elog(DEBUG1, "file \"%s\" excluded from backup", de->d_name);
                               1297                 :           1161 :                 excludeFound = true;
                               1298                 :           1161 :                 break;
                               1299                 :                :             }
                               1300                 :                :         }
                               1301                 :                : 
                               1302         [ +  + ]:         301413 :         if (excludeFound)
 4843 magnus@hagander.net      1303                 :           1161 :             continue;
                               1304                 :                : 
                               1305                 :                :         /*
                               1306                 :                :          * If there could be non-temporary relation files in this directory,
                               1307                 :                :          * try to parse the filename.
                               1308                 :                :          */
  152 rhaas@postgresql.org     1309         [ +  + ]:GNC      300252 :         if (isRelationDir)
                               1310                 :                :             isRelationFile =
                               1311                 :         290319 :                 parse_filename_for_nontemp_relation(de->d_name,
                               1312                 :                :                                                     &relfilenumber,
                               1313                 :                :                                                     &relForkNum, &segno);
                               1314                 :                : 
                               1315                 :                :         /* Exclude all forks for unlogged tables except the init fork */
                               1316   [ +  +  +  + ]:         300252 :         if (isRelationFile && relForkNum != INIT_FORKNUM)
                               1317                 :                :         {
                               1318                 :                :             char        initForkFile[MAXPGPATH];
                               1319                 :                : 
                               1320                 :                :             /*
                               1321                 :                :              * If any other type of fork, check if there is an init fork with
                               1322                 :                :              * the same RelFileNumber. If so, the file can be excluded.
                               1323                 :                :              */
                               1324                 :         287794 :             snprintf(initForkFile, sizeof(initForkFile), "%s/%u_init",
                               1325                 :                :                      path, relfilenumber);
                               1326                 :                : 
                               1327         [ +  + ]:         287794 :             if (lstat(initForkFile, &statbuf) == 0)
                               1328                 :                :             {
                               1329         [ -  + ]:             71 :                 elog(DEBUG2,
                               1330                 :                :                      "unlogged relation file \"%s\" excluded from backup",
                               1331                 :                :                      de->d_name);
                               1332                 :                : 
                               1333                 :             71 :                 continue;
                               1334                 :                :             }
                               1335                 :                :         }
                               1336                 :                : 
                               1337                 :                :         /* Exclude temporary relations */
                               1338   [ +  +  +  + ]:         300181 :         if (OidIsValid(dboid) && looks_like_temp_rel_name(de->d_name))
                               1339                 :                :         {
 2210 teodor@sigaev.ru         1340         [ -  + ]:CBC          40 :             elog(DEBUG2,
                               1341                 :                :                  "temporary relation file \"%s\" excluded from backup",
                               1342                 :                :                  de->d_name);
                               1343                 :                : 
                               1344                 :             40 :             continue;
                               1345                 :                :         }
                               1346                 :                : 
 2560 peter_e@gmx.net          1347                 :         300141 :         snprintf(pathbuf, sizeof(pathbuf), "%s/%s", path, de->d_name);
                               1348                 :                : 
                               1349                 :                :         /* Skip pg_control here to back up it last */
 4463 simon@2ndQuadrant.co     1350         [ +  + ]:         300141 :         if (strcmp(pathbuf, "./global/pg_control") == 0)
                               1351                 :            298 :             continue;
                               1352                 :                : 
 4843 magnus@hagander.net      1353         [ -  + ]:         299843 :         if (lstat(pathbuf, &statbuf) != 0)
                               1354                 :                :         {
 4843 magnus@hagander.net      1355         [ #  # ]:UBC           0 :             if (errno != ENOENT)
                               1356         [ #  # ]:              0 :                 ereport(ERROR,
                               1357                 :                :                         (errcode_for_file_access(),
                               1358                 :                :                          errmsg("could not stat file or directory \"%s\": %m",
                               1359                 :                :                                 pathbuf)));
                               1360                 :                : 
                               1361                 :                :             /* If the file went away while scanning, it's not an error. */
                               1362                 :              0 :             continue;
                               1363                 :                :         }
                               1364                 :                : 
                               1365                 :                :         /* Scan for directories whose contents should be excluded */
 2755 peter_e@gmx.net          1366                 :CBC      299843 :         excludeFound = false;
                               1367         [ +  + ]:        2390540 :         for (excludeIdx = 0; excludeDirContents[excludeIdx] != NULL; excludeIdx++)
                               1368                 :                :         {
                               1369         [ +  + ]:        2092748 :             if (strcmp(de->d_name, excludeDirContents[excludeIdx]) == 0)
                               1370                 :                :             {
                               1371         [ +  + ]:           2051 :                 elog(DEBUG1, "contents of directory \"%s\" excluded from backup", de->d_name);
  915 rhaas@postgresql.org     1372                 :           2051 :                 convert_link_to_directory(pathbuf, &statbuf);
  891                          1373                 :           2051 :                 size += _tarWriteHeader(sink, pathbuf + basepathlen + 1, NULL,
                               1374                 :                :                                         &statbuf, sizeonly);
 2755 peter_e@gmx.net          1375                 :           2051 :                 excludeFound = true;
                               1376                 :           2051 :                 break;
                               1377                 :                :             }
                               1378                 :                :         }
                               1379                 :                : 
                               1380         [ +  + ]:         299843 :         if (excludeFound)
                               1381                 :           2051 :             continue;
                               1382                 :                : 
                               1383                 :                :         /*
                               1384                 :                :          * We can skip pg_wal, the WAL segments need to be fetched from the
                               1385                 :                :          * WAL archive anyway. But include it as an empty directory anyway, so
                               1386                 :                :          * we get permissions right.
                               1387                 :                :          */
 2733 rhaas@postgresql.org     1388         [ +  + ]:         297792 :         if (strcmp(pathbuf, "./pg_wal") == 0)
                               1389                 :                :         {
                               1390                 :                :             /* If pg_wal is a symlink, write it as a directory anyway */
  915                          1391                 :            298 :             convert_link_to_directory(pathbuf, &statbuf);
  891                          1392                 :            298 :             size += _tarWriteHeader(sink, pathbuf + basepathlen + 1, NULL,
                               1393                 :                :                                     &statbuf, sizeonly);
                               1394                 :                : 
                               1395                 :                :             /*
                               1396                 :                :              * Also send archive_status and summaries directories (by
                               1397                 :                :              * hackishly reusing statbuf from above ...).
                               1398                 :                :              */
                               1399                 :            298 :             size += _tarWriteHeader(sink, "./pg_wal/archive_status", NULL,
                               1400                 :                :                                     &statbuf, sizeonly);
  116 rhaas@postgresql.org     1401                 :GNC         298 :             size += _tarWriteHeader(sink, "./pg_wal/summaries", NULL,
                               1402                 :                :                                     &statbuf, sizeonly);
                               1403                 :                : 
 2733 rhaas@postgresql.org     1404                 :CBC         298 :             continue;           /* don't recurse into pg_wal */
                               1405                 :                :         }
                               1406                 :                : 
                               1407                 :                :         /* Allow symbolic links in pg_tblspc only */
  617 tmunro@postgresql.or     1408   [ +  +  +  + ]:         297494 :         if (strcmp(path, "./pg_tblspc") == 0 && S_ISLNK(statbuf.st_mode))
 4843 magnus@hagander.net      1409                 :             31 :         {
                               1410                 :                :             char        linkpath[MAXPGPATH];
                               1411                 :                :             int         rllen;
                               1412                 :                : 
 4512 tgl@sss.pgh.pa.us        1413                 :             31 :             rllen = readlink(pathbuf, linkpath, sizeof(linkpath));
                               1414         [ -  + ]:             31 :             if (rllen < 0)
 4843 magnus@hagander.net      1415         [ #  # ]:UBC           0 :                 ereport(ERROR,
                               1416                 :                :                         (errcode_for_file_access(),
                               1417                 :                :                          errmsg("could not read symbolic link \"%s\": %m",
                               1418                 :                :                                 pathbuf)));
 4512 tgl@sss.pgh.pa.us        1419         [ -  + ]:CBC          31 :             if (rllen >= sizeof(linkpath))
 4512 tgl@sss.pgh.pa.us        1420         [ #  # ]:UBC           0 :                 ereport(ERROR,
                               1421                 :                :                         (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
                               1422                 :                :                          errmsg("symbolic link \"%s\" target is too long",
                               1423                 :                :                                 pathbuf)));
 4512 tgl@sss.pgh.pa.us        1424                 :CBC          31 :             linkpath[rllen] = '\0';
                               1425                 :                : 
  891 rhaas@postgresql.org     1426                 :             31 :             size += _tarWriteHeader(sink, pathbuf + basepathlen + 1, linkpath,
                               1427                 :                :                                     &statbuf, sizeonly);
                               1428                 :                :         }
 4843 magnus@hagander.net      1429         [ +  + ]:         297463 :         else if (S_ISDIR(statbuf.st_mode))
                               1430                 :                :         {
 3750                          1431                 :           4799 :             bool        skip_this_dir = false;
                               1432                 :                :             ListCell   *lc;
                               1433                 :                : 
                               1434                 :                :             /*
                               1435                 :                :              * Store a directory entry in the tar file so we can get the
                               1436                 :                :              * permissions right.
                               1437                 :                :              */
  891 rhaas@postgresql.org     1438                 :           4799 :             size += _tarWriteHeader(sink, pathbuf + basepathlen + 1, NULL, &statbuf,
                               1439                 :                :                                     sizeonly);
                               1440                 :                : 
                               1441                 :                :             /*
                               1442                 :                :              * Call ourselves recursively for a directory, unless it happens
                               1443                 :                :              * to be a separate tablespace located within PGDATA.
                               1444                 :                :              */
 3750 magnus@hagander.net      1445   [ +  +  +  +  :          10547 :             foreach(lc, tablespaces)
                                              +  + ]
                               1446                 :                :             {
                               1447                 :           5776 :                 tablespaceinfo *ti = (tablespaceinfo *) lfirst(lc);
                               1448                 :                : 
                               1449                 :                :                 /*
                               1450                 :                :                  * ti->rpath is the tablespace relative path within PGDATA, or
                               1451                 :                :                  * NULL if the tablespace has been properly located somewhere
                               1452                 :                :                  * else.
                               1453                 :                :                  *
                               1454                 :                :                  * Skip past the leading "./" in pathbuf when comparing.
                               1455                 :                :                  */
                               1456   [ +  +  +  + ]:           5776 :                 if (ti->rpath && strcmp(ti->rpath, pathbuf + 2) == 0)
                               1457                 :                :                 {
                               1458                 :             28 :                     skip_this_dir = true;
                               1459                 :             28 :                     break;
                               1460                 :                :                 }
                               1461                 :                :             }
                               1462                 :                : 
                               1463                 :                :             /*
                               1464                 :                :              * skip sending directories inside pg_tblspc, if not required.
                               1465                 :                :              */
 3260 andrew@dunslane.net      1466   [ +  +  +  + ]:           4799 :             if (strcmp(pathbuf, "./pg_tblspc") == 0 && !sendtblspclinks)
                               1467                 :             24 :                 skip_this_dir = true;
                               1468                 :                : 
 3750 magnus@hagander.net      1469         [ +  + ]:           4799 :             if (!skip_this_dir)
  891 rhaas@postgresql.org     1470                 :           4747 :                 size += sendDir(sink, pathbuf, basepathlen, sizeonly, tablespaces,
                               1471                 :                :                                 sendtblspclinks, manifest, spcoid, ib);
                               1472                 :                :         }
 4843 magnus@hagander.net      1473         [ +  - ]:         292664 :         else if (S_ISREG(statbuf.st_mode))
                               1474                 :                :         {
 3973 bruce@momjian.us         1475                 :         292664 :             bool        sent = false;
  116 rhaas@postgresql.org     1476                 :GNC      292664 :             unsigned    num_blocks_required = 0;
                               1477                 :         292664 :             unsigned    truncation_block_length = 0;
                               1478                 :                :             char        tarfilenamebuf[MAXPGPATH * 2];
                               1479                 :         292664 :             char       *tarfilename = pathbuf + basepathlen + 1;
                               1480                 :         292664 :             FileBackupMethod method = BACK_UP_FILE_FULLY;
                               1481                 :                : 
                               1482   [ +  +  +  + ]:         292664 :             if (ib != NULL && isRelationFile)
                               1483                 :                :             {
                               1484                 :                :                 Oid         relspcoid;
                               1485                 :                :                 char       *lookup_path;
                               1486                 :                : 
                               1487         [ -  + ]:           7255 :                 if (OidIsValid(spcoid))
                               1488                 :                :                 {
  116 rhaas@postgresql.org     1489                 :UNC           0 :                     relspcoid = spcoid;
                               1490                 :              0 :                     lookup_path = psprintf("pg_tblspc/%u/%s", spcoid,
                               1491                 :                :                                            tarfilename);
                               1492                 :                :                 }
                               1493                 :                :                 else
                               1494                 :                :                 {
  116 rhaas@postgresql.org     1495         [ +  + ]:GNC        7255 :                     if (isGlobalDir)
                               1496                 :            420 :                         relspcoid = GLOBALTABLESPACE_OID;
                               1497                 :                :                     else
                               1498                 :           6835 :                         relspcoid = DEFAULTTABLESPACE_OID;
                               1499                 :           7255 :                     lookup_path = pstrdup(tarfilename);
                               1500                 :                :                 }
                               1501                 :                : 
                               1502                 :           7255 :                 method = GetFileBackupMethod(ib, lookup_path, dboid, relspcoid,
                               1503                 :                :                                              relfilenumber, relForkNum,
                               1504                 :           7255 :                                              segno, statbuf.st_size,
                               1505                 :                :                                              &num_blocks_required,
                               1506                 :                :                                              relative_block_numbers,
                               1507                 :                :                                              &truncation_block_length);
                               1508         [ +  + ]:           7255 :                 if (method == BACK_UP_FILE_INCREMENTALLY)
                               1509                 :                :                 {
                               1510                 :           4651 :                     statbuf.st_size =
                               1511                 :           4651 :                         GetIncrementalFileSize(num_blocks_required);
                               1512                 :           4651 :                     snprintf(tarfilenamebuf, sizeof(tarfilenamebuf),
                               1513                 :                :                              "%s/INCREMENTAL.%s",
                               1514                 :           4651 :                              path + basepathlen + 1,
                               1515                 :           4651 :                              de->d_name);
                               1516                 :           4651 :                     tarfilename = tarfilenamebuf;
                               1517                 :                :                 }
                               1518                 :                : 
                               1519                 :           7255 :                 pfree(lookup_path);
                               1520                 :                :             }
                               1521                 :                : 
 4843 magnus@hagander.net      1522         [ +  + ]:CBC      292664 :             if (!sizeonly)
  116 rhaas@postgresql.org     1523         [ +  + ]:GNC      143980 :                 sent = sendFile(sink, pathbuf, tarfilename, &statbuf,
                               1524                 :                :                                 true, dboid, spcoid,
                               1525                 :                :                                 relfilenumber, segno, manifest,
                               1526                 :                :                                 num_blocks_required,
                               1527                 :                :                                 method == BACK_UP_FILE_INCREMENTALLY ? relative_block_numbers : NULL,
                               1528                 :                :                                 truncation_block_length);
                               1529                 :                : 
 4132 heikki.linnakangas@i     1530   [ +  +  +  - ]:CBC      292663 :             if (sent || sizeonly)
                               1531                 :                :             {
                               1532                 :                :                 /* Add size. */
 1451 rhaas@postgresql.org     1533                 :         292663 :                 size += statbuf.st_size;
                               1534                 :                : 
                               1535                 :                :                 /* Pad to a multiple of the tar block size. */
                               1536                 :         292663 :                 size += tarPaddingBytesRequired(statbuf.st_size);
                               1537                 :                : 
                               1538                 :                :                 /* Size of the header for the file. */
                               1539                 :         292663 :                 size += TAR_BLOCK_SIZE;
                               1540                 :                :             }
                               1541                 :                :         }
                               1542                 :                :         else
 4843 magnus@hagander.net      1543         [ #  # ]:UBC           0 :             ereport(WARNING,
                               1544                 :                :                     (errmsg("skipping special file \"%s\"", pathbuf)));
                               1545                 :                :     }
                               1546                 :                : 
  116 rhaas@postgresql.org     1547         [ +  + ]:GNC        5096 :     if (relative_block_numbers != NULL)
                               1548                 :            121 :         pfree(relative_block_numbers);
                               1549                 :                : 
 4843 magnus@hagander.net      1550                 :CBC        5096 :     FreeDir(dir);
                               1551                 :           5096 :     return size;
                               1552                 :                : }
                               1553                 :                : 
                               1554                 :                : /*
                               1555                 :                :  * Given the member, write the TAR header & send the file.
                               1556                 :                :  *
                               1557                 :                :  * If 'missing_ok' is true, will not throw an error if the file is not found.
                               1558                 :                :  *
                               1559                 :                :  * If dboid is anything other than InvalidOid then any checksum failures
                               1560                 :                :  * detected will get reported to the cumulative stats system.
                               1561                 :                :  *
                               1562                 :                :  * If the file is to be sent incrementally, then num_incremental_blocks
                               1563                 :                :  * should be the number of blocks to be sent, and incremental_blocks
                               1564                 :                :  * an array of block numbers relative to the start of the current segment.
                               1565                 :                :  * If the whole file is to be sent, then incremental_blocks should be NULL,
                               1566                 :                :  * and num_incremental_blocks can have any value, as it will be ignored.
                               1567                 :                :  *
                               1568                 :                :  * Returns true if the file was successfully sent, false if 'missing_ok',
                               1569                 :                :  * and the file did not exist.
                               1570                 :                :  */
                               1571                 :                : static bool
  891 rhaas@postgresql.org     1572                 :         144123 : sendFile(bbsink *sink, const char *readfilename, const char *tarfilename,
                               1573                 :                :          struct stat *statbuf, bool missing_ok, Oid dboid, Oid spcoid,
                               1574                 :                :          RelFileNumber relfilenumber, unsigned segno,
                               1575                 :                :          backup_manifest_info *manifest, unsigned num_incremental_blocks,
                               1576                 :                :          BlockNumber *incremental_blocks, unsigned truncation_block_length)
                               1577                 :                : {
                               1578                 :                :     int         fd;
 2203 magnus@hagander.net      1579                 :         144123 :     BlockNumber blkno = 0;
                               1580                 :         144123 :     int         checksum_failures = 0;
                               1581                 :                :     off_t       cnt;
  194 rhaas@postgresql.org     1582                 :GNC      144123 :     pgoff_t     bytes_done = 0;
 2203 magnus@hagander.net      1583                 :CBC      144123 :     bool        verify_checksum = false;
                               1584                 :                :     pg_checksum_context checksum_ctx;
  116 rhaas@postgresql.org     1585                 :GNC      144123 :     int         ibindex = 0;
                               1586                 :                : 
 1229 michael@paquier.xyz      1587         [ -  + ]:CBC      144123 :     if (pg_checksum_init(&checksum_ctx, manifest->checksum_type) < 0)
 1229 michael@paquier.xyz      1588         [ #  # ]:UBC           0 :         elog(ERROR, "could not initialize checksum of file \"%s\"",
                               1589                 :                :              readfilename);
                               1590                 :                : 
 1397 rhaas@postgresql.org     1591                 :CBC      144123 :     fd = OpenTransientFile(readfilename, O_RDONLY | PG_BINARY);
                               1592         [ -  + ]:         144123 :     if (fd < 0)
                               1593                 :                :     {
 4132 heikki.linnakangas@i     1594   [ #  #  #  # ]:UBC           0 :         if (errno == ENOENT && missing_ok)
                               1595                 :              0 :             return false;
 4843 magnus@hagander.net      1596         [ #  # ]:              0 :         ereport(ERROR,
                               1597                 :                :                 (errcode_for_file_access(),
                               1598                 :                :                  errmsg("could not open file \"%s\": %m", readfilename)));
                               1599                 :                :     }
                               1600                 :                : 
  891 rhaas@postgresql.org     1601                 :CBC      144123 :     _tarWriteHeader(sink, tarfilename, NULL, statbuf, false);
                               1602                 :                : 
                               1603                 :                :     /*
                               1604                 :                :      * Checksums are verified in multiples of BLCKSZ, so the buffer length
                               1605                 :                :      * should be a multiple of the block size as well.
                               1606                 :                :      */
  194 rhaas@postgresql.org     1607         [ -  + ]:GNC      144122 :     Assert((sink->bbs_buffer_length % BLCKSZ) == 0);
                               1608                 :                : 
                               1609                 :                :     /*
                               1610                 :                :      * If we weren't told not to verify checksums, and if checksums are
                               1611                 :                :      * enabled for this cluster, and if this is a relation file, then verify
                               1612                 :                :      * the checksum.
                               1613                 :                :      */
  152                          1614   [ +  +  +  +  :         144122 :     if (!noverify_checksums && DataChecksumsEnabled() &&
                                              +  + ]
                               1615                 :                :         RelFileNumberIsValid(relfilenumber))
                               1616                 :          28609 :         verify_checksum = true;
                               1617                 :                : 
                               1618                 :                :     /*
                               1619                 :                :      * If we're sending an incremental file, write the file header.
                               1620                 :                :      */
  116                          1621         [ +  + ]:         144122 :     if (incremental_blocks != NULL)
                               1622                 :                :     {
                               1623                 :           4651 :         unsigned    magic = INCREMENTAL_MAGIC;
                               1624                 :           4651 :         size_t      header_bytes_done = 0;
                               1625                 :                :         char        padding[BLCKSZ];
                               1626                 :                :         size_t      paddinglen;
                               1627                 :                : 
                               1628                 :                :         /* Emit header data. */
                               1629                 :           4651 :         push_to_sink(sink, &checksum_ctx, &header_bytes_done,
                               1630                 :                :                      &magic, sizeof(magic));
                               1631                 :           4651 :         push_to_sink(sink, &checksum_ctx, &header_bytes_done,
                               1632                 :                :                      &num_incremental_blocks, sizeof(num_incremental_blocks));
                               1633                 :           4651 :         push_to_sink(sink, &checksum_ctx, &header_bytes_done,
                               1634                 :                :                      &truncation_block_length, sizeof(truncation_block_length));
                               1635                 :           4651 :         push_to_sink(sink, &checksum_ctx, &header_bytes_done,
                               1636                 :                :                      incremental_blocks,
                               1637                 :                :                      sizeof(BlockNumber) * num_incremental_blocks);
                               1638                 :                : 
                               1639                 :                :         /*
                               1640                 :                :          * Add padding to align header to a multiple of BLCKSZ, but only if
                               1641                 :                :          * the incremental file has some blocks, and the alignment is actually
                               1642                 :                :          * needed (i.e. header is not already a multiple of BLCKSZ). If there
                               1643                 :                :          * are no blocks we don't want to make the file unnecessarily large,
                               1644                 :                :          * as that might make some filesystem optimizations impossible.
                               1645                 :                :          */
    0 tomas.vondra@postgre     1646   [ +  +  +  - ]:           4651 :         if ((num_incremental_blocks > 0) && (header_bytes_done % BLCKSZ != 0))
                               1647                 :                :         {
    9                          1648                 :             23 :             paddinglen = (BLCKSZ - (header_bytes_done % BLCKSZ));
                               1649                 :                : 
                               1650                 :             23 :             memset(padding, 0, paddinglen);
                               1651                 :             23 :             bytes_done += paddinglen;
                               1652                 :                : 
                               1653                 :             23 :             push_to_sink(sink, &checksum_ctx, &header_bytes_done,
                               1654                 :                :                          padding, paddinglen);
                               1655                 :                :         }
                               1656                 :                : 
                               1657                 :                :         /* Flush out any data still in the buffer so it's again empty. */
  116 rhaas@postgresql.org     1658         [ +  - ]:           4651 :         if (header_bytes_done > 0)
                               1659                 :                :         {
                               1660                 :           4651 :             bbsink_archive_contents(sink, header_bytes_done);
                               1661         [ -  + ]:           4651 :             if (pg_checksum_update(&checksum_ctx,
                               1662                 :           4651 :                                    (uint8 *) sink->bbs_buffer,
                               1663                 :                :                                    header_bytes_done) < 0)
  116 rhaas@postgresql.org     1664         [ #  # ]:UNC           0 :                 elog(ERROR, "could not update checksum of base backup");
                               1665                 :                :         }
                               1666                 :                : 
                               1667                 :                :         /* Update our notion of file position. */
  116 rhaas@postgresql.org     1668                 :GNC        4651 :         bytes_done += sizeof(magic);
                               1669                 :           4651 :         bytes_done += sizeof(num_incremental_blocks);
                               1670                 :           4651 :         bytes_done += sizeof(truncation_block_length);
                               1671                 :           4651 :         bytes_done += sizeof(BlockNumber) * num_incremental_blocks;
                               1672                 :                :     }
                               1673                 :                : 
                               1674                 :                :     /*
                               1675                 :                :      * Loop until we read the amount of data the caller told us to expect. The
                               1676                 :                :      * file could be longer, if it was extended while we were sending it, but
                               1677                 :                :      * for a base backup we can ignore such extended data. It will be restored
                               1678                 :                :      * from WAL.
                               1679                 :                :      */
                               1680                 :                :     while (1)
                               1681                 :                :     {
                               1682                 :                :         /*
                               1683                 :                :          * Determine whether we've read all the data that we need, and if not,
                               1684                 :                :          * read some more.
                               1685                 :                :          */
                               1686         [ +  + ]:         312382 :         if (incremental_blocks == NULL)
                               1687                 :                :         {
                               1688                 :         307696 :             size_t      remaining = statbuf->st_size - bytes_done;
                               1689                 :                : 
                               1690                 :                :             /*
                               1691                 :                :              * If we've read the required number of bytes, then it's time to
                               1692                 :                :              * stop.
                               1693                 :                :              */
                               1694         [ +  + ]:         307696 :             if (bytes_done >= statbuf->st_size)
                               1695                 :         139471 :                 break;
                               1696                 :                : 
                               1697                 :                :             /*
                               1698                 :                :              * Read as many bytes as will fit in the buffer, or however many
                               1699                 :                :              * are left to read, whichever is less.
                               1700                 :                :              */
                               1701                 :         168225 :             cnt = read_file_data_into_buffer(sink, readfilename, fd,
                               1702                 :                :                                              bytes_done, remaining,
                               1703                 :         168225 :                                              blkno + segno * RELSEG_SIZE,
                               1704                 :                :                                              verify_checksum,
                               1705                 :                :                                              &checksum_failures);
                               1706                 :                :         }
                               1707                 :                :         else
                               1708                 :                :         {
                               1709                 :                :             BlockNumber relative_blkno;
                               1710                 :                : 
                               1711                 :                :             /*
                               1712                 :                :              * If we've read all the blocks, then it's time to stop.
                               1713                 :                :              */
                               1714         [ +  + ]:           4686 :             if (ibindex >= num_incremental_blocks)
                               1715                 :           4651 :                 break;
                               1716                 :                : 
                               1717                 :                :             /*
                               1718                 :                :              * Read just one block, whichever one is the next that we're
                               1719                 :                :              * supposed to include.
                               1720                 :                :              */
                               1721                 :             35 :             relative_blkno = incremental_blocks[ibindex++];
                               1722                 :             35 :             cnt = read_file_data_into_buffer(sink, readfilename, fd,
                               1723                 :             35 :                                              relative_blkno * BLCKSZ,
                               1724                 :                :                                              BLCKSZ,
                               1725                 :             35 :                                              relative_blkno + segno * RELSEG_SIZE,
                               1726                 :                :                                              verify_checksum,
                               1727                 :                :                                              &checksum_failures);
                               1728                 :                : 
                               1729                 :                :             /*
                               1730                 :                :              * If we get a partial read, that must mean that the relation is
                               1731                 :                :              * being truncated. Ultimately, it should be truncated to a
                               1732                 :                :              * multiple of BLCKSZ, since this path should only be reached for
                               1733                 :                :              * relation files, but we might transiently observe an
                               1734                 :                :              * intermediate value.
                               1735                 :                :              *
                               1736                 :                :              * It should be fine to treat this just as if the entire block had
                               1737                 :                :              * been truncated away - i.e. fill this and all later blocks with
                               1738                 :                :              * zeroes. WAL replay will fix things up.
                               1739                 :                :              */
                               1740         [ -  + ]:             35 :             if (cnt < BLCKSZ)
  116 rhaas@postgresql.org     1741                 :UNC           0 :                 break;
                               1742                 :                :         }
                               1743                 :                : 
                               1744                 :                :         /*
                               1745                 :                :          * If the amount of data we were able to read was not a multiple of
                               1746                 :                :          * BLCKSZ, we cannot verify checksums, which are block-level.
                               1747                 :                :          */
 2203 magnus@hagander.net      1748   [ +  +  -  + ]:CBC      168260 :         if (verify_checksum && (cnt % BLCKSZ != 0))
                               1749                 :                :         {
 2203 magnus@hagander.net      1750         [ #  # ]:UBC           0 :             ereport(WARNING,
                               1751                 :                :                     (errmsg("could not verify checksum in file \"%s\", block "
                               1752                 :                :                             "%u: read buffer size %d and page size %d "
                               1753                 :                :                             "differ",
                               1754                 :                :                             readfilename, blkno, (int) cnt, BLCKSZ)));
                               1755                 :              0 :             verify_checksum = false;
                               1756                 :                :         }
                               1757                 :                : 
                               1758                 :                :         /*
                               1759                 :                :          * If we hit end-of-file, a concurrent truncation must have occurred.
                               1760                 :                :          * That's not an error condition, because WAL replay will fix things
                               1761                 :                :          * up.
                               1762                 :                :          */
  437 rhaas@postgresql.org     1763         [ -  + ]:CBC      168260 :         if (cnt == 0)
  437 rhaas@postgresql.org     1764                 :UBC           0 :             break;
                               1765                 :                : 
                               1766                 :                :         /* Update block number and # of bytes done for next loop iteration. */
  194 rhaas@postgresql.org     1767                 :GNC      168260 :         blkno += cnt / BLCKSZ;
                               1768                 :         168260 :         bytes_done += cnt;
                               1769                 :                : 
                               1770                 :                :         /*
                               1771                 :                :          * Make sure incremental files with block data are properly aligned
                               1772                 :                :          * (header is a multiple of BLCKSZ, blocks are BLCKSZ too).
                               1773                 :                :          */
    9 tomas.vondra@postgre     1774   [ +  +  +  -  :         168260 :         Assert(!((incremental_blocks != NULL && num_incremental_blocks > 0) &&
                                              -  + ]
                               1775                 :                :                  (bytes_done % BLCKSZ != 0)));
                               1776                 :                : 
                               1777                 :                :         /* Archive the data we just read. */
  891 rhaas@postgresql.org     1778                 :CBC      168260 :         bbsink_archive_contents(sink, cnt);
                               1779                 :                : 
                               1780                 :                :         /* Also feed it to the checksum machinery. */
                               1781         [ -  + ]:         168260 :         if (pg_checksum_update(&checksum_ctx,
                               1782                 :         168260 :                                (uint8 *) sink->bbs_buffer, cnt) < 0)
 1229 michael@paquier.xyz      1783         [ #  # ]:UBC           0 :             elog(ERROR, "could not update checksum of base backup");
                               1784                 :                :     }
                               1785                 :                : 
                               1786                 :                :     /* If the file was truncated while we were sending it, pad it with zeros */
  194 rhaas@postgresql.org     1787         [ -  + ]:GNC      144122 :     while (bytes_done < statbuf->st_size)
                               1788                 :                :     {
  194 rhaas@postgresql.org     1789                 :UNC           0 :         size_t      remaining = statbuf->st_size - bytes_done;
  891 rhaas@postgresql.org     1790                 :UBC           0 :         size_t      nbytes = Min(sink->bbs_buffer_length, remaining);
                               1791                 :                : 
                               1792   [ #  #  #  #  :              0 :         MemSet(sink->bbs_buffer, 0, nbytes);
                                     #  #  #  #  #  
                                                 # ]
                               1793         [ #  # ]:              0 :         if (pg_checksum_update(&checksum_ctx,
                               1794                 :              0 :                                (uint8 *) sink->bbs_buffer,
                               1795                 :                :                                nbytes) < 0)
                               1796         [ #  # ]:              0 :             elog(ERROR, "could not update checksum of base backup");
                               1797                 :              0 :         bbsink_archive_contents(sink, nbytes);
  194 rhaas@postgresql.org     1798                 :UNC           0 :         bytes_done += nbytes;
                               1799                 :                :     }
                               1800                 :                : 
                               1801                 :                :     /*
                               1802                 :                :      * Pad to a block boundary, per tar format requirements. (This small piece
                               1803                 :                :      * of data is probably not worth throttling, and is not checksummed
                               1804                 :                :      * because it's not actually part of the file.)
                               1805                 :                :      */
  194 rhaas@postgresql.org     1806                 :GNC      144122 :     _tarWritePadding(sink, bytes_done);
                               1807                 :                : 
 1397 rhaas@postgresql.org     1808                 :CBC      144122 :     CloseTransientFile(fd);
                               1809                 :                : 
 2203 magnus@hagander.net      1810         [ +  + ]:         144122 :     if (checksum_failures > 1)
                               1811                 :                :     {
                               1812         [ +  - ]:              2 :         ereport(WARNING,
                               1813                 :                :                 (errmsg_plural("file \"%s\" has a total of %d checksum verification failure",
                               1814                 :                :                                "file \"%s\" has a total of %d checksum verification failures",
                               1815                 :                :                                checksum_failures,
                               1816                 :                :                                readfilename, checksum_failures)));
                               1817                 :                : 
 1829                          1818                 :              2 :         pgstat_report_checksum_failures_in_db(dboid, checksum_failures);
                               1819                 :                :     }
                               1820                 :                : 
 2203                          1821                 :         144122 :     total_checksum_failures += checksum_failures;
                               1822                 :                : 
 1452 rhaas@postgresql.org     1823                 :         144122 :     AddFileToBackupManifest(manifest, spcoid, tarfilename, statbuf->st_size,
                               1824                 :         144122 :                             (pg_time_t) statbuf->st_mtime, &checksum_ctx);
                               1825                 :                : 
 4132 heikki.linnakangas@i     1826                 :         144122 :     return true;
                               1827                 :                : }
                               1828                 :                : 
                               1829                 :                : /*
                               1830                 :                :  * Read some more data from the file into the bbsink's buffer, verifying
                               1831                 :                :  * checksums as required.
                               1832                 :                :  *
                               1833                 :                :  * 'offset' is the file offset from which we should begin to read, and
                               1834                 :                :  * 'length' is the amount of data that should be read. The actual amount
                               1835                 :                :  * of data read will be less than the requested amount if the bbsink's
                               1836                 :                :  * buffer isn't big enough to hold it all, or if the underlying file has
                               1837                 :                :  * been truncated. The return value is the number of bytes actually read.
                               1838                 :                :  *
                               1839                 :                :  * 'blkno' is the block number of the first page in the bbsink's buffer
                               1840                 :                :  * relative to the start of the relation.
                               1841                 :                :  *
                               1842                 :                :  * 'verify_checksum' indicates whether we should try to verify checksums
                               1843                 :                :  * for the blocks we read. If we do this, we'll update *checksum_failures
                               1844                 :                :  * and issue warnings as appropriate.
                               1845                 :                :  */
                               1846                 :                : static off_t
  194 rhaas@postgresql.org     1847                 :GNC      168260 : read_file_data_into_buffer(bbsink *sink, const char *readfilename, int fd,
                               1848                 :                :                            off_t offset, size_t length, BlockNumber blkno,
                               1849                 :                :                            bool verify_checksum, int *checksum_failures)
                               1850                 :                : {
                               1851                 :                :     off_t       cnt;
                               1852                 :                :     int         i;
                               1853                 :                :     char       *page;
                               1854                 :                : 
                               1855                 :                :     /* Try to read some more data. */
                               1856                 :         168260 :     cnt = basebackup_read_file(fd, sink->bbs_buffer,
                               1857                 :         168260 :                                Min(sink->bbs_buffer_length, length),
                               1858                 :                :                                offset, readfilename, true);
                               1859                 :                : 
                               1860                 :                :     /* Can't verify checksums if read length is not a multiple of BLCKSZ. */
                               1861   [ +  +  -  + ]:         168260 :     if (!verify_checksum || (cnt % BLCKSZ) != 0)
                               1862                 :         133371 :         return cnt;
                               1863                 :                : 
                               1864                 :                :     /* Verify checksum for each block. */
                               1865         [ +  + ]:         121438 :     for (i = 0; i < cnt / BLCKSZ; i++)
                               1866                 :                :     {
                               1867                 :                :         int         reread_cnt;
                               1868                 :                :         uint16      expected_checksum;
                               1869                 :                : 
                               1870                 :          86549 :         page = sink->bbs_buffer + BLCKSZ * i;
                               1871                 :                : 
                               1872                 :                :         /* If the page is OK, go on to the next one. */
                               1873         [ +  + ]:          86549 :         if (verify_page_checksum(page, sink->bbs_state->startptr, blkno + i,
                               1874                 :                :                                  &expected_checksum))
                               1875                 :          86535 :             continue;
                               1876                 :                : 
                               1877                 :                :         /*
                               1878                 :                :          * Retry the block on the first failure.  It's possible that we read
                               1879                 :                :          * the first 4K page of the block just before postgres updated the
                               1880                 :                :          * entire block so it ends up looking torn to us. If, before we retry
                               1881                 :                :          * the read, the concurrent write of the block finishes, the page LSN
                               1882                 :                :          * will be updated and we'll realize that we should ignore this block.
                               1883                 :                :          *
                               1884                 :                :          * There's no guarantee that this will actually happen, though: the
                               1885                 :                :          * torn write could take an arbitrarily long time to complete.
                               1886                 :                :          * Retrying multiple times wouldn't fix this problem, either, though
                               1887                 :                :          * it would reduce the chances of it happening in practice. The only
                               1888                 :                :          * real fix here seems to be to have some kind of interlock that
                               1889                 :                :          * allows us to wait until we can be certain that no write to the
                               1890                 :                :          * block is in progress. Since we don't have any such thing right now,
                               1891                 :                :          * we just do this and hope for the best.
                               1892                 :                :          */
                               1893                 :             14 :         reread_cnt =
                               1894                 :             14 :             basebackup_read_file(fd, sink->bbs_buffer + BLCKSZ * i,
                               1895                 :             14 :                                  BLCKSZ, offset + BLCKSZ * i,
                               1896                 :                :                                  readfilename, false);
                               1897         [ -  + ]:             14 :         if (reread_cnt == 0)
                               1898                 :                :         {
                               1899                 :                :             /*
                               1900                 :                :              * If we hit end-of-file, a concurrent truncation must have
                               1901                 :                :              * occurred, so reduce cnt to reflect only the blocks already
                               1902                 :                :              * processed and break out of this loop.
                               1903                 :                :              */
  194 rhaas@postgresql.org     1904                 :UNC           0 :             cnt = BLCKSZ * i;
                               1905                 :              0 :             break;
                               1906                 :                :         }
                               1907                 :                : 
                               1908                 :                :         /* If the page now looks OK, go on to the next one. */
  194 rhaas@postgresql.org     1909         [ -  + ]:GNC          14 :         if (verify_page_checksum(page, sink->bbs_state->startptr, blkno + i,
                               1910                 :                :                                  &expected_checksum))
  194 rhaas@postgresql.org     1911                 :UNC           0 :             continue;
                               1912                 :                : 
                               1913                 :                :         /* Handle checksum failure. */
  194 rhaas@postgresql.org     1914                 :GNC          14 :         (*checksum_failures)++;
                               1915         [ +  + ]:             14 :         if (*checksum_failures <= 5)
                               1916         [ +  - ]:             12 :             ereport(WARNING,
                               1917                 :                :                     (errmsg("checksum verification failed in "
                               1918                 :                :                             "file \"%s\", block %u: calculated "
                               1919                 :                :                             "%X but expected %X",
                               1920                 :                :                             readfilename, blkno + i, expected_checksum,
                               1921                 :                :                             ((PageHeader) page)->pd_checksum)));
                               1922         [ +  + ]:             14 :         if (*checksum_failures == 5)
                               1923         [ +  - ]:              2 :             ereport(WARNING,
                               1924                 :                :                     (errmsg("further checksum verification "
                               1925                 :                :                             "failures in file \"%s\" will not "
                               1926                 :                :                             "be reported", readfilename)));
                               1927                 :                :     }
                               1928                 :                : 
                               1929                 :          34889 :     return cnt;
                               1930                 :                : }
                               1931                 :                : 
                               1932                 :                : /*
                               1933                 :                :  * Push data into a bbsink.
                               1934                 :                :  *
                               1935                 :                :  * It's better, when possible, to read data directly into the bbsink's buffer,
                               1936                 :                :  * rather than using this function to copy it into the buffer; this function is
                               1937                 :                :  * for cases where that approach is not practical.
                               1938                 :                :  *
                               1939                 :                :  * bytes_done should point to a count of the number of bytes that are
                               1940                 :                :  * currently used in the bbsink's buffer. Upon return, the bytes identified by
                               1941                 :                :  * data and length will have been copied into the bbsink's buffer, flushing
                               1942                 :                :  * as required, and *bytes_done will have been updated accordingly. If the
                               1943                 :                :  * buffer was flushed, the previous contents will also have been fed to
                               1944                 :                :  * checksum_ctx.
                               1945                 :                :  *
                               1946                 :                :  * Note that after one or more calls to this function it is the caller's
                               1947                 :                :  * responsibility to perform any required final flush.
                               1948                 :                :  */
                               1949                 :                : static void
  116                          1950                 :          18627 : push_to_sink(bbsink *sink, pg_checksum_context *checksum_ctx,
                               1951                 :                :              size_t *bytes_done, void *data, size_t length)
                               1952                 :                : {
                               1953         [ +  + ]:          18627 :     while (length > 0)
                               1954                 :                :     {
                               1955                 :                :         size_t      bytes_to_copy;
                               1956                 :                : 
                               1957                 :                :         /*
                               1958                 :                :          * We use < here rather than <= so that if the data exactly fills the
                               1959                 :                :          * remaining buffer space, we trigger a flush now.
                               1960                 :                :          */
                               1961         [ +  - ]:          13999 :         if (length < sink->bbs_buffer_length - *bytes_done)
                               1962                 :                :         {
                               1963                 :                :             /* Append remaining data to buffer. */
                               1964                 :          13999 :             memcpy(sink->bbs_buffer + *bytes_done, data, length);
                               1965                 :          13999 :             *bytes_done += length;
                               1966                 :          13999 :             return;
                               1967                 :                :         }
                               1968                 :                : 
                               1969                 :                :         /* Copy until buffer is full and flush it. */
  116 rhaas@postgresql.org     1970                 :UNC           0 :         bytes_to_copy = sink->bbs_buffer_length - *bytes_done;
                               1971                 :              0 :         memcpy(sink->bbs_buffer + *bytes_done, data, bytes_to_copy);
                               1972                 :              0 :         data = ((char *) data) + bytes_to_copy;
                               1973                 :              0 :         length -= bytes_to_copy;
                               1974                 :              0 :         bbsink_archive_contents(sink, sink->bbs_buffer_length);
                               1975         [ #  # ]:              0 :         if (pg_checksum_update(checksum_ctx, (uint8 *) sink->bbs_buffer,
                               1976                 :                :                                sink->bbs_buffer_length) < 0)
                               1977         [ #  # ]:              0 :             elog(ERROR, "could not update checksum");
                               1978                 :              0 :         *bytes_done = 0;
                               1979                 :                :     }
                               1980                 :                : }
                               1981                 :                : 
                               1982                 :                : /*
                               1983                 :                :  * Try to verify the checksum for the provided page, if it seems appropriate
                               1984                 :                :  * to do so.
                               1985                 :                :  *
                               1986                 :                :  * Returns true if verification succeeds or if we decide not to check it,
                               1987                 :                :  * and false if verification fails. When return false, it also sets
                               1988                 :                :  * *expected_checksum to the computed value.
                               1989                 :                :  */
                               1990                 :                : static bool
  194 rhaas@postgresql.org     1991                 :GNC       86563 : verify_page_checksum(Page page, XLogRecPtr start_lsn, BlockNumber blkno,
                               1992                 :                :                      uint16 *expected_checksum)
                               1993                 :                : {
                               1994                 :                :     PageHeader  phdr;
                               1995                 :                :     uint16      checksum;
                               1996                 :                : 
                               1997                 :                :     /*
                               1998                 :                :      * Only check pages which have not been modified since the start of the
                               1999                 :                :      * base backup. Otherwise, they might have been written only halfway and
                               2000                 :                :      * the checksum would not be valid.  However, replaying WAL would
                               2001                 :                :      * reinstate the correct page in this case. We also skip completely new
                               2002                 :                :      * pages, since they don't have a checksum yet.
                               2003                 :                :      */
                               2004   [ +  +  -  + ]:          86563 :     if (PageIsNew(page) || PageGetLSN(page) >= start_lsn)
                               2005                 :             76 :         return true;
                               2006                 :                : 
                               2007                 :                :     /* Perform the actual checksum calculation. */
                               2008                 :          86487 :     checksum = pg_checksum_page(page, blkno);
                               2009                 :                : 
                               2010                 :                :     /* See whether it matches the value from the page. */
                               2011                 :          86487 :     phdr = (PageHeader) page;
                               2012         [ +  + ]:          86487 :     if (phdr->pd_checksum == checksum)
                               2013                 :          86459 :         return true;
                               2014                 :             28 :     *expected_checksum = checksum;
                               2015                 :             28 :     return false;
                               2016                 :                : }
                               2017                 :                : 
                               2018                 :                : static int64
  891 rhaas@postgresql.org     2019                 :CBC      152173 : _tarWriteHeader(bbsink *sink, const char *filename, const char *linktarget,
                               2020                 :                :                 struct stat *statbuf, bool sizeonly)
                               2021                 :                : {
                               2022                 :                :     enum tarError rc;
                               2023                 :                : 
 2755 peter_e@gmx.net          2024         [ +  + ]:         152173 :     if (!sizeonly)
                               2025                 :                :     {
                               2026                 :                :         /*
                               2027                 :                :          * As of this writing, the smallest supported block size is 1kB, which
                               2028                 :                :          * is twice TAR_BLOCK_SIZE. Since the buffer size is required to be a
                               2029                 :                :          * multiple of BLCKSZ, it should be safe to assume that the buffer is
                               2030                 :                :          * large enough to fit an entire tar block. We double-check by means
                               2031                 :                :          * of these assertions.
                               2032                 :                :          */
                               2033                 :                :         StaticAssertDecl(TAR_BLOCK_SIZE <= BLCKSZ,
                               2034                 :                :                          "BLCKSZ too small for tar block");
  891 rhaas@postgresql.org     2035         [ -  + ]:         148198 :         Assert(sink->bbs_buffer_length >= TAR_BLOCK_SIZE);
                               2036                 :                : 
                               2037                 :         148198 :         rc = tarCreateHeader(sink->bbs_buffer, filename, linktarget,
                               2038                 :                :                              statbuf->st_size, statbuf->st_mode,
                               2039                 :                :                              statbuf->st_uid, statbuf->st_gid,
                               2040                 :                :                              statbuf->st_mtime);
                               2041                 :                : 
 2755 peter_e@gmx.net          2042   [ +  +  -  - ]:         148198 :         switch (rc)
                               2043                 :                :         {
                               2044                 :         148197 :             case TAR_OK:
                               2045                 :         148197 :                 break;
                               2046                 :              1 :             case TAR_NAME_TOO_LONG:
                               2047         [ +  - ]:              1 :                 ereport(ERROR,
                               2048                 :                :                         (errmsg("file name too long for tar format: \"%s\"",
                               2049                 :                :                                 filename)));
                               2050                 :                :                 break;
 2755 peter_e@gmx.net          2051                 :UBC           0 :             case TAR_SYMLINK_TOO_LONG:
                               2052         [ #  # ]:              0 :                 ereport(ERROR,
                               2053                 :                :                         (errmsg("symbolic link target too long for tar format: "
                               2054                 :                :                                 "file name \"%s\", target \"%s\"",
                               2055                 :                :                                 filename, linktarget)));
                               2056                 :                :                 break;
                               2057                 :              0 :             default:
                               2058         [ #  # ]:              0 :                 elog(ERROR, "unrecognized tar error: %d", rc);
                               2059                 :                :         }
                               2060                 :                : 
  891 rhaas@postgresql.org     2061                 :CBC      148197 :         bbsink_archive_contents(sink, TAR_BLOCK_SIZE);
                               2062                 :                :     }
                               2063                 :                : 
                               2064                 :         152172 :     return TAR_BLOCK_SIZE;
                               2065                 :                : }
                               2066                 :                : 
                               2067                 :                : /*
                               2068                 :                :  * Pad with zero bytes out to a multiple of TAR_BLOCK_SIZE.
                               2069                 :                :  */
                               2070                 :                : static void
                               2071                 :         144315 : _tarWritePadding(bbsink *sink, int len)
                               2072                 :                : {
                               2073                 :         144315 :     int         pad = tarPaddingBytesRequired(len);
                               2074                 :                : 
                               2075                 :                :     /*
                               2076                 :                :      * As in _tarWriteHeader, it should be safe to assume that the buffer is
                               2077                 :                :      * large enough that we don't need to do this in multiple chunks.
                               2078                 :                :      */
                               2079         [ -  + ]:         144315 :     Assert(sink->bbs_buffer_length >= TAR_BLOCK_SIZE);
                               2080         [ -  + ]:         144315 :     Assert(pad <= TAR_BLOCK_SIZE);
                               2081                 :                : 
                               2082         [ +  + ]:         144315 :     if (pad > 0)
                               2083                 :                :     {
                               2084   [ +  -  +  +  :          32574 :         MemSet(sink->bbs_buffer, 0, pad);
                                     +  -  +  -  +  
                                                 + ]
                               2085                 :           6727 :         bbsink_archive_contents(sink, pad);
                               2086                 :                :     }
 3699 alvherre@alvh.no-ip.     2087                 :         144315 : }
                               2088                 :                : 
                               2089                 :                : /*
                               2090                 :                :  * If the entry in statbuf is a link, then adjust statbuf to make it look like a
                               2091                 :                :  * directory, so that it will be written that way.
                               2092                 :                :  */
                               2093                 :                : static void
  891 rhaas@postgresql.org     2094                 :           2349 : convert_link_to_directory(const char *pathbuf, struct stat *statbuf)
                               2095                 :                : {
                               2096                 :                :     /* If symlink, write it as a directory anyway */
                               2097         [ +  + ]:           2349 :     if (S_ISLNK(statbuf->st_mode))
                               2098                 :             63 :         statbuf->st_mode = S_IFDIR | pg_dir_create_mode;
 1503 fujii@postgresql.org     2099                 :           2349 : }
                               2100                 :                : 
                               2101                 :                : /*
                               2102                 :                :  * Read some data from a file, setting a wait event and reporting any error
                               2103                 :                :  * encountered.
                               2104                 :                :  *
                               2105                 :                :  * If partial_read_ok is false, also report an error if the number of bytes
                               2106                 :                :  * read is not equal to the number of bytes requested.
                               2107                 :                :  *
                               2108                 :                :  * Returns the number of bytes read.
                               2109                 :                :  */
                               2110                 :                : static ssize_t
 1397 rhaas@postgresql.org     2111                 :         178514 : basebackup_read_file(int fd, char *buf, size_t nbytes, off_t offset,
                               2112                 :                :                      const char *filename, bool partial_read_ok)
                               2113                 :                : {
                               2114                 :                :     ssize_t     rc;
                               2115                 :                : 
                               2116                 :         178514 :     pgstat_report_wait_start(WAIT_EVENT_BASEBACKUP_READ);
  563 tmunro@postgresql.or     2117                 :         178514 :     rc = pg_pread(fd, buf, nbytes, offset);
 1397 rhaas@postgresql.org     2118                 :         178514 :     pgstat_report_wait_end();
                               2119                 :                : 
                               2120         [ -  + ]:         178514 :     if (rc < 0)
 1397 rhaas@postgresql.org     2121         [ #  # ]:UBC           0 :         ereport(ERROR,
                               2122                 :                :                 (errcode_for_file_access(),
                               2123                 :                :                  errmsg("could not read file \"%s\": %m", filename)));
 1397 rhaas@postgresql.org     2124   [ +  +  +  -  :CBC      178514 :     if (!partial_read_ok && rc > 0 && rc != nbytes)
                                              -  + ]
 1397 rhaas@postgresql.org     2125         [ #  # ]:UBC           0 :         ereport(ERROR,
                               2126                 :                :                 (errcode_for_file_access(),
                               2127                 :                :                  errmsg("could not read file \"%s\": read %zd of %zu",
                               2128                 :                :                         filename, rc, nbytes)));
                               2129                 :                : 
 1397 rhaas@postgresql.org     2130                 :CBC      178514 :     return rc;
                               2131                 :                : }
        

Generated by: LCOV version 2.1-beta2-3-g6141622