LCOV - code coverage report
Current view: top level - src/backend/backup - basebackup.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 0.0 % 868 0
Test Date: 2026-01-26 10:56:24 Functions: 0.0 % 15 0
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 0.0 % 797 0

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

Generated by: LCOV version 2.3.2-1