LCOV - code coverage report
Current view: top level - src/bin/pg_basebackup - pg_basebackup.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 0.0 % 1199 0
Test Date: 2026-01-26 10:56:24 Functions: 0.0 % 32 0
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /*-------------------------------------------------------------------------
       2              :  *
       3              :  * pg_basebackup.c - receive a base backup using streaming replication protocol
       4              :  *
       5              :  * Author: Magnus Hagander <magnus@hagander.net>
       6              :  *
       7              :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
       8              :  *
       9              :  * IDENTIFICATION
      10              :  *                src/bin/pg_basebackup/pg_basebackup.c
      11              :  *-------------------------------------------------------------------------
      12              :  */
      13              : 
      14              : #include "postgres_fe.h"
      15              : 
      16              : #include <unistd.h>
      17              : #include <dirent.h>
      18              : #include <limits.h>
      19              : #include <sys/select.h>
      20              : #include <sys/stat.h>
      21              : #include <sys/wait.h>
      22              : #include <signal.h>
      23              : #include <time.h>
      24              : #ifdef HAVE_LIBZ
      25              : #include <zlib.h>
      26              : #endif
      27              : 
      28              : #include "access/xlog_internal.h"
      29              : #include "astreamer_inject.h"
      30              : #include "backup/basebackup.h"
      31              : #include "common/compression.h"
      32              : #include "common/file_perm.h"
      33              : #include "common/file_utils.h"
      34              : #include "common/logging.h"
      35              : #include "fe_utils/option_utils.h"
      36              : #include "fe_utils/recovery_gen.h"
      37              : #include "getopt_long.h"
      38              : #include "libpq/protocol.h"
      39              : #include "receivelog.h"
      40              : #include "streamutil.h"
      41              : 
      42              : #define ERRCODE_DATA_CORRUPTED  "XX001"
      43              : 
      44              : typedef struct TablespaceListCell
      45              : {
      46              :         struct TablespaceListCell *next;
      47              :         char            old_dir[MAXPGPATH];
      48              :         char            new_dir[MAXPGPATH];
      49              : } TablespaceListCell;
      50              : 
      51              : typedef struct TablespaceList
      52              : {
      53              :         TablespaceListCell *head;
      54              :         TablespaceListCell *tail;
      55              : } TablespaceList;
      56              : 
      57              : typedef struct ArchiveStreamState
      58              : {
      59              :         int                     tablespacenum;
      60              :         pg_compress_specification *compress;
      61              :         astreamer  *streamer;
      62              :         astreamer  *manifest_inject_streamer;
      63              :         PQExpBuffer manifest_buffer;
      64              :         char            manifest_filename[MAXPGPATH];
      65              :         FILE       *manifest_file;
      66              : } ArchiveStreamState;
      67              : 
      68              : typedef struct WriteTarState
      69              : {
      70              :         int                     tablespacenum;
      71              :         astreamer  *streamer;
      72              : } WriteTarState;
      73              : 
      74              : typedef struct WriteManifestState
      75              : {
      76              :         char            filename[MAXPGPATH];
      77              :         FILE       *file;
      78              : } WriteManifestState;
      79              : 
      80              : typedef void (*WriteDataCallback) (size_t nbytes, char *buf,
      81              :                                                                    void *callback_data);
      82              : 
      83              : /*
      84              :  * pg_xlog has been renamed to pg_wal in version 10.  This version number
      85              :  * should be compared with PQserverVersion().
      86              :  */
      87              : #define MINIMUM_VERSION_FOR_PG_WAL      100000
      88              : 
      89              : /*
      90              :  * Temporary replication slots are supported from version 10.
      91              :  */
      92              : #define MINIMUM_VERSION_FOR_TEMP_SLOTS 100000
      93              : 
      94              : /*
      95              :  * Backup manifests are supported from version 13.
      96              :  */
      97              : #define MINIMUM_VERSION_FOR_MANIFESTS   130000
      98              : 
      99              : /*
     100              :  * Before v15, tar files received from the server will be improperly
     101              :  * terminated.
     102              :  */
     103              : #define MINIMUM_VERSION_FOR_TERMINATED_TARFILE 150000
     104              : 
     105              : /*
     106              :  * pg_wal/summaries exists beginning with version 17.
     107              :  */
     108              : #define MINIMUM_VERSION_FOR_WAL_SUMMARIES 170000
     109              : 
     110              : /*
     111              :  * Different ways to include WAL
     112              :  */
     113              : typedef enum
     114              : {
     115              :         NO_WAL,
     116              :         FETCH_WAL,
     117              :         STREAM_WAL,
     118              : } IncludeWal;
     119              : 
     120              : /*
     121              :  * Different places to perform compression
     122              :  */
     123              : typedef enum
     124              : {
     125              :         COMPRESS_LOCATION_UNSPECIFIED,
     126              :         COMPRESS_LOCATION_CLIENT,
     127              :         COMPRESS_LOCATION_SERVER,
     128              : } CompressionLocation;
     129              : 
     130              : /* Global options */
     131              : static char *basedir = NULL;
     132              : static TablespaceList tablespace_dirs = {NULL, NULL};
     133              : static char *xlog_dir = NULL;
     134              : static char format = '\0';              /* p(lain)/t(ar) */
     135              : static char *label = "pg_basebackup base backup";
     136              : static bool noclean = false;
     137              : static bool checksum_failure = false;
     138              : static bool showprogress = false;
     139              : static bool estimatesize = true;
     140              : static int      verbose = 0;
     141              : static IncludeWal includewal = STREAM_WAL;
     142              : static bool fastcheckpoint = false;
     143              : static bool writerecoveryconf = false;
     144              : static bool do_sync = true;
     145              : static int      standby_message_timeout = 10 * 1000;    /* 10 sec = default */
     146              : static pg_time_t last_progress_report = 0;
     147              : static int32 maxrate = 0;               /* no limit by default */
     148              : static char *replication_slot = NULL;
     149              : static bool temp_replication_slot = true;
     150              : static char *backup_target = NULL;
     151              : static bool create_slot = false;
     152              : static bool no_slot = false;
     153              : static bool verify_checksums = true;
     154              : static bool manifest = true;
     155              : static bool manifest_force_encode = false;
     156              : static char *manifest_checksums = NULL;
     157              : static DataDirSyncMethod sync_method = DATA_DIR_SYNC_METHOD_FSYNC;
     158              : 
     159              : static bool success = false;
     160              : static bool made_new_pgdata = false;
     161              : static bool found_existing_pgdata = false;
     162              : static bool made_new_xlogdir = false;
     163              : static bool found_existing_xlogdir = false;
     164              : static bool made_tablespace_dirs = false;
     165              : static bool found_tablespace_dirs = false;
     166              : 
     167              : /* Progress indicators */
     168              : static uint64 totalsize_kb;
     169              : static uint64 totaldone;
     170              : static int      tablespacecount;
     171              : static char *progress_filename = NULL;
     172              : 
     173              : /* Pipe to communicate with background wal receiver process */
     174              : #ifndef WIN32
     175              : static int      bgpipe[2] = {-1, -1};
     176              : #endif
     177              : 
     178              : /* Handle to child process */
     179              : static pid_t bgchild = -1;
     180              : static bool in_log_streamer = false;
     181              : 
     182              : /* Flag to indicate if child process exited unexpectedly */
     183              : static volatile sig_atomic_t bgchild_exited = false;
     184              : 
     185              : /* End position for xlog streaming, empty string if unknown yet */
     186              : static XLogRecPtr xlogendptr;
     187              : 
     188              : #ifndef WIN32
     189              : static int      has_xlogendptr = 0;
     190              : #else
     191              : static volatile LONG has_xlogendptr = 0;
     192              : #endif
     193              : 
     194              : /* Contents of configuration file to be generated */
     195              : static PQExpBuffer recoveryconfcontents = NULL;
     196              : 
     197              : /* Function headers */
     198              : static void usage(void);
     199              : static void verify_dir_is_empty_or_create(char *dirname, bool *created, bool *found);
     200              : static void progress_update_filename(const char *filename);
     201              : static void progress_report(int tablespacenum, bool force, bool finished);
     202              : 
     203              : static astreamer *CreateBackupStreamer(char *archive_name, char *spclocation,
     204              :                                                                            astreamer **manifest_inject_streamer_p,
     205              :                                                                            bool is_recovery_guc_supported,
     206              :                                                                            bool expect_unterminated_tarfile,
     207              :                                                                            pg_compress_specification *compress);
     208              : static void ReceiveArchiveStreamChunk(size_t r, char *copybuf,
     209              :                                                                           void *callback_data);
     210              : static char GetCopyDataByte(size_t r, char *copybuf, size_t *cursor);
     211              : static char *GetCopyDataString(size_t r, char *copybuf, size_t *cursor);
     212              : static uint64 GetCopyDataUInt64(size_t r, char *copybuf, size_t *cursor);
     213              : static void GetCopyDataEnd(size_t r, char *copybuf, size_t cursor);
     214              : static void ReportCopyDataParseError(size_t r, char *copybuf);
     215              : static void ReceiveTarFile(PGconn *conn, char *archive_name, char *spclocation,
     216              :                                                    bool tablespacenum, pg_compress_specification *compress);
     217              : static void ReceiveTarCopyChunk(size_t r, char *copybuf, void *callback_data);
     218              : static void ReceiveBackupManifest(PGconn *conn);
     219              : static void ReceiveBackupManifestChunk(size_t r, char *copybuf,
     220              :                                                                            void *callback_data);
     221              : static void ReceiveBackupManifestInMemory(PGconn *conn, PQExpBuffer buf);
     222              : static void ReceiveBackupManifestInMemoryChunk(size_t r, char *copybuf,
     223              :                                                                                            void *callback_data);
     224              : static void BaseBackup(char *compression_algorithm, char *compression_detail,
     225              :                                            CompressionLocation compressloc,
     226              :                                            pg_compress_specification *client_compress,
     227              :                                            char *incremental_manifest);
     228              : 
     229              : static bool reached_end_position(XLogRecPtr segendpos, uint32 timeline,
     230              :                                                                  bool segment_finished);
     231              : 
     232              : static const char *get_tablespace_mapping(const char *dir);
     233              : static void tablespace_list_append(const char *arg);
     234              : 
     235              : 
     236              : static void
     237            0 : cleanup_directories_atexit(void)
     238              : {
     239            0 :         if (success || in_log_streamer)
     240            0 :                 return;
     241              : 
     242            0 :         if (!noclean && !checksum_failure)
     243              :         {
     244            0 :                 if (made_new_pgdata)
     245              :                 {
     246            0 :                         pg_log_info("removing data directory \"%s\"", basedir);
     247            0 :                         if (!rmtree(basedir, true))
     248            0 :                                 pg_log_error("failed to remove data directory");
     249            0 :                 }
     250            0 :                 else if (found_existing_pgdata)
     251              :                 {
     252            0 :                         pg_log_info("removing contents of data directory \"%s\"", basedir);
     253            0 :                         if (!rmtree(basedir, false))
     254            0 :                                 pg_log_error("failed to remove contents of data directory");
     255            0 :                 }
     256              : 
     257            0 :                 if (made_new_xlogdir)
     258              :                 {
     259            0 :                         pg_log_info("removing WAL directory \"%s\"", xlog_dir);
     260            0 :                         if (!rmtree(xlog_dir, true))
     261            0 :                                 pg_log_error("failed to remove WAL directory");
     262            0 :                 }
     263            0 :                 else if (found_existing_xlogdir)
     264              :                 {
     265            0 :                         pg_log_info("removing contents of WAL directory \"%s\"", xlog_dir);
     266            0 :                         if (!rmtree(xlog_dir, false))
     267            0 :                                 pg_log_error("failed to remove contents of WAL directory");
     268            0 :                 }
     269            0 :         }
     270              :         else
     271              :         {
     272            0 :                 if ((made_new_pgdata || found_existing_pgdata) && !checksum_failure)
     273            0 :                         pg_log_info("data directory \"%s\" not removed at user's request", basedir);
     274              : 
     275            0 :                 if (made_new_xlogdir || found_existing_xlogdir)
     276            0 :                         pg_log_info("WAL directory \"%s\" not removed at user's request", xlog_dir);
     277              :         }
     278              : 
     279            0 :         if ((made_tablespace_dirs || found_tablespace_dirs) && !checksum_failure)
     280            0 :                 pg_log_info("changes to tablespace directories will not be undone");
     281            0 : }
     282              : 
     283              : static void
     284            0 : disconnect_atexit(void)
     285              : {
     286            0 :         if (conn != NULL)
     287            0 :                 PQfinish(conn);
     288            0 : }
     289              : 
     290              : #ifndef WIN32
     291              : /*
     292              :  * If the bgchild exits prematurely and raises a SIGCHLD signal, we can abort
     293              :  * processing rather than wait until the backup has finished and error out at
     294              :  * that time. On Windows, we use a background thread which can communicate
     295              :  * without the need for a signal handler.
     296              :  */
     297              : static void
     298            0 : sigchld_handler(SIGNAL_ARGS)
     299              : {
     300            0 :         bgchild_exited = true;
     301            0 : }
     302              : 
     303              : /*
     304              :  * On windows, our background thread dies along with the process. But on
     305              :  * Unix, if we have started a subprocess, we want to kill it off so it
     306              :  * doesn't remain running trying to stream data.
     307              :  */
     308              : static void
     309            0 : kill_bgchild_atexit(void)
     310              : {
     311            0 :         if (bgchild > 0 && !bgchild_exited)
     312            0 :                 kill(bgchild, SIGTERM);
     313            0 : }
     314              : #endif
     315              : 
     316              : /*
     317              :  * Split argument into old_dir and new_dir and append to tablespace mapping
     318              :  * list.
     319              :  */
     320              : static void
     321            0 : tablespace_list_append(const char *arg)
     322              : {
     323            0 :         TablespaceListCell *cell = (TablespaceListCell *) pg_malloc0(sizeof(TablespaceListCell));
     324            0 :         char       *dst;
     325            0 :         char       *dst_ptr;
     326            0 :         const char *arg_ptr;
     327              : 
     328            0 :         dst_ptr = dst = cell->old_dir;
     329            0 :         for (arg_ptr = arg; *arg_ptr; arg_ptr++)
     330              :         {
     331            0 :                 if (dst_ptr - dst >= MAXPGPATH)
     332            0 :                         pg_fatal("directory name too long");
     333              : 
     334            0 :                 if (*arg_ptr == '\\' && *(arg_ptr + 1) == '=')
     335              :                         ;                                       /* skip backslash escaping = */
     336            0 :                 else if (*arg_ptr == '=' && (arg_ptr == arg || *(arg_ptr - 1) != '\\'))
     337              :                 {
     338            0 :                         if (*cell->new_dir)
     339            0 :                                 pg_fatal("multiple \"=\" signs in tablespace mapping");
     340              :                         else
     341            0 :                                 dst = dst_ptr = cell->new_dir;
     342            0 :                 }
     343              :                 else
     344            0 :                         *dst_ptr++ = *arg_ptr;
     345            0 :         }
     346              : 
     347            0 :         if (!*cell->old_dir || !*cell->new_dir)
     348            0 :                 pg_fatal("invalid tablespace mapping format \"%s\", must be \"OLDDIR=NEWDIR\"", arg);
     349              : 
     350              :         /*
     351              :          * All tablespaces are created with absolute directories, so specifying a
     352              :          * non-absolute path here would just never match, possibly confusing
     353              :          * users. Since we don't know whether the remote side is Windows or not,
     354              :          * and it might be different than the local side, permit any path that
     355              :          * could be absolute under either set of rules.
     356              :          *
     357              :          * (There is little practical risk of confusion here, because someone
     358              :          * running entirely on Linux isn't likely to have a relative path that
     359              :          * begins with a backslash or something that looks like a drive
     360              :          * specification. If they do, and they also incorrectly believe that a
     361              :          * relative path is acceptable here, we'll silently fail to warn them of
     362              :          * their mistake, and the -T option will just not get applied, same as if
     363              :          * they'd specified -T for a nonexistent tablespace.)
     364              :          */
     365            0 :         if (!is_nonwindows_absolute_path(cell->old_dir) &&
     366            0 :                 !is_windows_absolute_path(cell->old_dir))
     367            0 :                 pg_fatal("old directory is not an absolute path in tablespace mapping: %s",
     368              :                                  cell->old_dir);
     369              : 
     370            0 :         if (!is_absolute_path(cell->new_dir))
     371            0 :                 pg_fatal("new directory is not an absolute path in tablespace mapping: %s",
     372              :                                  cell->new_dir);
     373              : 
     374              :         /*
     375              :          * Comparisons done with these values should involve similarly
     376              :          * canonicalized path values.  This is particularly sensitive on Windows
     377              :          * where path values may not necessarily use Unix slashes.
     378              :          */
     379            0 :         canonicalize_path(cell->old_dir);
     380            0 :         canonicalize_path(cell->new_dir);
     381              : 
     382            0 :         if (tablespace_dirs.tail)
     383            0 :                 tablespace_dirs.tail->next = cell;
     384              :         else
     385            0 :                 tablespace_dirs.head = cell;
     386            0 :         tablespace_dirs.tail = cell;
     387            0 : }
     388              : 
     389              : 
     390              : static void
     391            0 : usage(void)
     392              : {
     393            0 :         printf(_("%s takes a base backup of a running PostgreSQL server.\n\n"),
     394              :                    progname);
     395            0 :         printf(_("Usage:\n"));
     396            0 :         printf(_("  %s [OPTION]...\n"), progname);
     397            0 :         printf(_("\nOptions controlling the output:\n"));
     398            0 :         printf(_("  -D, --pgdata=DIRECTORY receive base backup into directory\n"));
     399            0 :         printf(_("  -F, --format=p|t       output format (plain (default), tar)\n"));
     400            0 :         printf(_("  -i, --incremental=OLDMANIFEST\n"
     401              :                          "                         take incremental backup\n"));
     402            0 :         printf(_("  -r, --max-rate=RATE    maximum transfer rate to transfer data directory\n"
     403              :                          "                         (in kB/s, or use suffix \"k\" or \"M\")\n"));
     404            0 :         printf(_("  -R, --write-recovery-conf\n"
     405              :                          "                         write configuration for replication\n"));
     406            0 :         printf(_("  -t, --target=TARGET[:DETAIL]\n"
     407              :                          "                         backup target (if other than client)\n"));
     408            0 :         printf(_("  -T, --tablespace-mapping=OLDDIR=NEWDIR\n"
     409              :                          "                         relocate tablespace in OLDDIR to NEWDIR\n"));
     410            0 :         printf(_("      --waldir=WALDIR    location for the write-ahead log directory\n"));
     411            0 :         printf(_("  -X, --wal-method=none|fetch|stream\n"
     412              :                          "                         include required WAL files with specified method\n"));
     413            0 :         printf(_("  -z, --gzip             compress tar output\n"));
     414            0 :         printf(_("  -Z, --compress=[{client|server}-]METHOD[:DETAIL]\n"
     415              :                          "                         compress on client or server as specified\n"));
     416            0 :         printf(_("  -Z, --compress=none    do not compress tar output\n"));
     417            0 :         printf(_("\nGeneral options:\n"));
     418            0 :         printf(_("  -c, --checkpoint=fast|spread\n"
     419              :                          "                         set fast or spread (default) checkpointing\n"));
     420            0 :         printf(_("  -C, --create-slot      create replication slot\n"));
     421            0 :         printf(_("  -l, --label=LABEL      set backup label\n"));
     422            0 :         printf(_("  -n, --no-clean         do not clean up after errors\n"));
     423            0 :         printf(_("  -N, --no-sync          do not wait for changes to be written safely to disk\n"));
     424            0 :         printf(_("  -P, --progress         show progress information\n"));
     425            0 :         printf(_("  -S, --slot=SLOTNAME    replication slot to use\n"));
     426            0 :         printf(_("  -v, --verbose          output verbose messages\n"));
     427            0 :         printf(_("  -V, --version          output version information, then exit\n"));
     428            0 :         printf(_("      --manifest-checksums=SHA{224,256,384,512}|CRC32C|NONE\n"
     429              :                          "                         use algorithm for manifest checksums\n"));
     430            0 :         printf(_("      --manifest-force-encode\n"
     431              :                          "                         hex encode all file names in manifest\n"));
     432            0 :         printf(_("      --no-estimate-size do not estimate backup size in server side\n"));
     433            0 :         printf(_("      --no-manifest      suppress generation of backup manifest\n"));
     434            0 :         printf(_("      --no-slot          prevent creation of temporary replication slot\n"));
     435            0 :         printf(_("      --no-verify-checksums\n"
     436              :                          "                         do not verify checksums\n"));
     437            0 :         printf(_("      --sync-method=METHOD\n"
     438              :                          "                         set method for syncing files to disk\n"));
     439            0 :         printf(_("  -?, --help             show this help, then exit\n"));
     440            0 :         printf(_("\nConnection options:\n"));
     441            0 :         printf(_("  -d, --dbname=CONNSTR   connection string\n"));
     442            0 :         printf(_("  -h, --host=HOSTNAME    database server host or socket directory\n"));
     443            0 :         printf(_("  -p, --port=PORT        database server port number\n"));
     444            0 :         printf(_("  -s, --status-interval=INTERVAL\n"
     445              :                          "                         time between status packets sent to server (in seconds)\n"));
     446            0 :         printf(_("  -U, --username=NAME    connect as specified database user\n"));
     447            0 :         printf(_("  -w, --no-password      never prompt for password\n"));
     448            0 :         printf(_("  -W, --password         force password prompt (should happen automatically)\n"));
     449            0 :         printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
     450            0 :         printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
     451            0 : }
     452              : 
     453              : 
     454              : /*
     455              :  * Called in the background process every time data is received.
     456              :  * On Unix, we check to see if there is any data on our pipe
     457              :  * (which would mean we have a stop position), and if it is, check if
     458              :  * it is time to stop.
     459              :  * On Windows, we are in a single process, so we can just check if it's
     460              :  * time to stop.
     461              :  */
     462              : static bool
     463            0 : reached_end_position(XLogRecPtr segendpos, uint32 timeline,
     464              :                                          bool segment_finished)
     465              : {
     466            0 :         if (!has_xlogendptr)
     467              :         {
     468              : #ifndef WIN32
     469            0 :                 fd_set          fds;
     470            0 :                 struct timeval tv = {0};
     471            0 :                 int                     r;
     472              : 
     473              :                 /*
     474              :                  * Don't have the end pointer yet - check our pipe to see if it has
     475              :                  * been sent yet.
     476              :                  */
     477            0 :                 FD_ZERO(&fds);
     478            0 :                 FD_SET(bgpipe[0], &fds);
     479              : 
     480            0 :                 r = select(bgpipe[0] + 1, &fds, NULL, NULL, &tv);
     481            0 :                 if (r == 1)
     482              :                 {
     483            0 :                         char            xlogend[64] = {0};
     484            0 :                         uint32          hi,
     485              :                                                 lo;
     486              : 
     487            0 :                         r = read(bgpipe[0], xlogend, sizeof(xlogend) - 1);
     488            0 :                         if (r < 0)
     489            0 :                                 pg_fatal("could not read from ready pipe: %m");
     490              : 
     491            0 :                         if (sscanf(xlogend, "%X/%08X", &hi, &lo) != 2)
     492            0 :                                 pg_fatal("could not parse write-ahead log location \"%s\"",
     493              :                                                  xlogend);
     494            0 :                         xlogendptr = ((uint64) hi) << 32 | lo;
     495            0 :                         has_xlogendptr = 1;
     496              : 
     497              :                         /*
     498              :                          * Fall through to check if we've reached the point further
     499              :                          * already.
     500              :                          */
     501            0 :                 }
     502              :                 else
     503              :                 {
     504              :                         /*
     505              :                          * No data received on the pipe means we don't know the end
     506              :                          * position yet - so just say it's not time to stop yet.
     507              :                          */
     508            0 :                         return false;
     509              :                 }
     510              : #else
     511              : 
     512              :                 /*
     513              :                  * On win32, has_xlogendptr is set by the main thread, so if it's not
     514              :                  * set here, we just go back and wait until it shows up.
     515              :                  */
     516              :                 return false;
     517              : #endif
     518            0 :         }
     519              : 
     520              :         /*
     521              :          * At this point we have an end pointer, so compare it to the current
     522              :          * position to figure out if it's time to stop.
     523              :          */
     524            0 :         if (segendpos >= xlogendptr)
     525            0 :                 return true;
     526              : 
     527              :         /*
     528              :          * Have end pointer, but haven't reached it yet - so tell the caller to
     529              :          * keep streaming.
     530              :          */
     531            0 :         return false;
     532            0 : }
     533              : 
     534              : typedef struct
     535              : {
     536              :         PGconn     *bgconn;
     537              :         XLogRecPtr      startptr;
     538              :         char            xlog[MAXPGPATH];        /* directory or tarfile depending on mode */
     539              :         char       *sysidentifier;
     540              :         int                     timeline;
     541              :         pg_compress_algorithm wal_compress_algorithm;
     542              :         int                     wal_compress_level;
     543              : } logstreamer_param;
     544              : 
     545              : static int
     546            0 : LogStreamerMain(logstreamer_param *param)
     547              : {
     548            0 :         StreamCtl       stream = {0};
     549              : 
     550            0 :         in_log_streamer = true;
     551              : 
     552            0 :         stream.startpos = param->startptr;
     553            0 :         stream.timeline = param->timeline;
     554            0 :         stream.sysidentifier = param->sysidentifier;
     555            0 :         stream.stream_stop = reached_end_position;
     556              : #ifndef WIN32
     557            0 :         stream.stop_socket = bgpipe[0];
     558              : #else
     559              :         stream.stop_socket = PGINVALID_SOCKET;
     560              : #endif
     561            0 :         stream.standby_message_timeout = standby_message_timeout;
     562            0 :         stream.synchronous = false;
     563              :         /* fsync happens at the end of pg_basebackup for all data */
     564            0 :         stream.do_sync = false;
     565            0 :         stream.mark_done = true;
     566            0 :         stream.partial_suffix = NULL;
     567            0 :         stream.replication_slot = replication_slot;
     568            0 :         if (format == 'p')
     569            0 :                 stream.walmethod = CreateWalDirectoryMethod(param->xlog,
     570              :                                                                                                         PG_COMPRESSION_NONE, 0,
     571            0 :                                                                                                         stream.do_sync);
     572              :         else
     573            0 :                 stream.walmethod = CreateWalTarMethod(param->xlog,
     574            0 :                                                                                           param->wal_compress_algorithm,
     575            0 :                                                                                           param->wal_compress_level,
     576            0 :                                                                                           stream.do_sync);
     577              : 
     578            0 :         if (!ReceiveXlogStream(param->bgconn, &stream))
     579              :         {
     580              :                 /*
     581              :                  * Any errors will already have been reported in the function process,
     582              :                  * but we need to tell the parent that we didn't shutdown in a nice
     583              :                  * way.
     584              :                  */
     585              : #ifdef WIN32
     586              :                 /*
     587              :                  * In order to signal the main thread of an ungraceful exit we set the
     588              :                  * same flag that we use on Unix to signal SIGCHLD.
     589              :                  */
     590              :                 bgchild_exited = true;
     591              : #endif
     592            0 :                 return 1;
     593              :         }
     594              : 
     595            0 :         if (!stream.walmethod->ops->finish(stream.walmethod))
     596              :         {
     597            0 :                 pg_log_error("could not finish writing WAL files: %m");
     598              : #ifdef WIN32
     599              :                 bgchild_exited = true;
     600              : #endif
     601            0 :                 return 1;
     602              :         }
     603              : 
     604            0 :         PQfinish(param->bgconn);
     605              : 
     606            0 :         stream.walmethod->ops->free(stream.walmethod);
     607              : 
     608            0 :         return 0;
     609            0 : }
     610              : 
     611              : /*
     612              :  * Initiate background process for receiving xlog during the backup.
     613              :  * The background stream will use its own database connection so we can
     614              :  * stream the logfile in parallel with the backups.
     615              :  */
     616              : static void
     617            0 : StartLogStreamer(char *startpos, uint32 timeline, char *sysidentifier,
     618              :                                  pg_compress_algorithm wal_compress_algorithm,
     619              :                                  int wal_compress_level)
     620              : {
     621            0 :         logstreamer_param *param;
     622            0 :         uint32          hi,
     623              :                                 lo;
     624            0 :         char            statusdir[MAXPGPATH];
     625              : 
     626            0 :         param = pg_malloc0(sizeof(logstreamer_param));
     627            0 :         param->timeline = timeline;
     628            0 :         param->sysidentifier = sysidentifier;
     629            0 :         param->wal_compress_algorithm = wal_compress_algorithm;
     630            0 :         param->wal_compress_level = wal_compress_level;
     631              : 
     632              :         /* Convert the starting position */
     633            0 :         if (sscanf(startpos, "%X/%08X", &hi, &lo) != 2)
     634            0 :                 pg_fatal("could not parse write-ahead log location \"%s\"",
     635              :                                  startpos);
     636            0 :         param->startptr = ((uint64) hi) << 32 | lo;
     637              :         /* Round off to even segment position */
     638            0 :         param->startptr -= XLogSegmentOffset(param->startptr, WalSegSz);
     639              : 
     640              : #ifndef WIN32
     641              :         /* Create our background pipe */
     642            0 :         if (pipe(bgpipe) < 0)
     643            0 :                 pg_fatal("could not create pipe for background process: %m");
     644              : #endif
     645              : 
     646              :         /* Get a second connection */
     647            0 :         param->bgconn = GetConnection();
     648            0 :         if (!param->bgconn)
     649              :                 /* Error message already written in GetConnection() */
     650            0 :                 exit(1);
     651              : 
     652              :         /* In post-10 cluster, pg_xlog has been renamed to pg_wal */
     653            0 :         snprintf(param->xlog, sizeof(param->xlog), "%s/%s",
     654            0 :                          basedir,
     655            0 :                          PQserverVersion(conn) < MINIMUM_VERSION_FOR_PG_WAL ?
     656              :                          "pg_xlog" : "pg_wal");
     657              : 
     658              :         /* Temporary replication slots are only supported in 10 and newer */
     659            0 :         if (PQserverVersion(conn) < MINIMUM_VERSION_FOR_TEMP_SLOTS)
     660            0 :                 temp_replication_slot = false;
     661              : 
     662              :         /*
     663              :          * Create replication slot if requested
     664              :          */
     665            0 :         if (temp_replication_slot && !replication_slot)
     666            0 :                 replication_slot = psprintf("pg_basebackup_%u",
     667            0 :                                                                         (unsigned int) PQbackendPID(param->bgconn));
     668            0 :         if (temp_replication_slot || create_slot)
     669              :         {
     670            0 :                 if (!CreateReplicationSlot(param->bgconn, replication_slot, NULL,
     671            0 :                                                                    temp_replication_slot, true, true, false,
     672              :                                                                    false, false))
     673            0 :                         exit(1);
     674              : 
     675            0 :                 if (verbose)
     676              :                 {
     677            0 :                         if (temp_replication_slot)
     678            0 :                                 pg_log_info("created temporary replication slot \"%s\"",
     679              :                                                         replication_slot);
     680              :                         else
     681            0 :                                 pg_log_info("created replication slot \"%s\"",
     682              :                                                         replication_slot);
     683            0 :                 }
     684            0 :         }
     685              : 
     686            0 :         if (format == 'p')
     687              :         {
     688              :                 /*
     689              :                  * Create pg_wal/archive_status or pg_xlog/archive_status (and thus
     690              :                  * pg_wal or pg_xlog) depending on the target server so we can write
     691              :                  * to basedir/pg_wal or basedir/pg_xlog as the directory entry in the
     692              :                  * tar file may arrive later.
     693              :                  */
     694            0 :                 snprintf(statusdir, sizeof(statusdir), "%s/%s/archive_status",
     695            0 :                                  basedir,
     696            0 :                                  PQserverVersion(conn) < MINIMUM_VERSION_FOR_PG_WAL ?
     697              :                                  "pg_xlog" : "pg_wal");
     698              : 
     699            0 :                 if (pg_mkdir_p(statusdir, pg_dir_create_mode) != 0 && errno != EEXIST)
     700            0 :                         pg_fatal("could not create directory \"%s\": %m", statusdir);
     701              : 
     702              :                 /*
     703              :                  * For newer server versions, likewise create pg_wal/summaries
     704              :                  */
     705            0 :                 if (PQserverVersion(conn) >= MINIMUM_VERSION_FOR_WAL_SUMMARIES)
     706              :                 {
     707            0 :                         char            summarydir[MAXPGPATH];
     708              : 
     709            0 :                         snprintf(summarydir, sizeof(summarydir), "%s/%s/summaries",
     710            0 :                                          basedir, "pg_wal");
     711              : 
     712            0 :                         if (pg_mkdir_p(summarydir, pg_dir_create_mode) != 0 &&
     713            0 :                                 errno != EEXIST)
     714            0 :                                 pg_fatal("could not create directory \"%s\": %m", summarydir);
     715            0 :                 }
     716            0 :         }
     717              : 
     718              :         /*
     719              :          * Start a child process and tell it to start streaming. On Unix, this is
     720              :          * a fork(). On Windows, we create a thread.
     721              :          */
     722              : #ifndef WIN32
     723            0 :         bgchild = fork();
     724            0 :         if (bgchild == 0)
     725              :         {
     726              :                 /* in child process */
     727            0 :                 exit(LogStreamerMain(param));
     728              :         }
     729            0 :         else if (bgchild < 0)
     730            0 :                 pg_fatal("could not create background process: %m");
     731              : 
     732              :         /*
     733              :          * Else we are in the parent process and all is well.
     734              :          */
     735            0 :         atexit(kill_bgchild_atexit);
     736              : #else                                                   /* WIN32 */
     737              :         bgchild = _beginthreadex(NULL, 0, (void *) LogStreamerMain, param, 0, NULL);
     738              :         if (bgchild == 0)
     739              :                 pg_fatal("could not create background thread: %m");
     740              : #endif
     741            0 : }
     742              : 
     743              : /*
     744              :  * Verify that the given directory exists and is empty. If it does not
     745              :  * exist, it is created. If it exists but is not empty, an error will
     746              :  * be given and the process ended.
     747              :  */
     748              : static void
     749            0 : verify_dir_is_empty_or_create(char *dirname, bool *created, bool *found)
     750              : {
     751            0 :         switch (pg_check_dir(dirname))
     752              :         {
     753              :                 case 0:
     754              : 
     755              :                         /*
     756              :                          * Does not exist, so create
     757              :                          */
     758            0 :                         if (pg_mkdir_p(dirname, pg_dir_create_mode) == -1)
     759            0 :                                 pg_fatal("could not create directory \"%s\": %m", dirname);
     760            0 :                         if (created)
     761            0 :                                 *created = true;
     762            0 :                         return;
     763              :                 case 1:
     764              : 
     765              :                         /*
     766              :                          * Exists, empty
     767              :                          */
     768            0 :                         if (found)
     769            0 :                                 *found = true;
     770            0 :                         return;
     771              :                 case 2:
     772              :                 case 3:
     773              :                 case 4:
     774              : 
     775              :                         /*
     776              :                          * Exists, not empty
     777              :                          */
     778            0 :                         pg_fatal("directory \"%s\" exists but is not empty", dirname);
     779              :                 case -1:
     780              : 
     781              :                         /*
     782              :                          * Access problem
     783              :                          */
     784            0 :                         pg_fatal("could not access directory \"%s\": %m", dirname);
     785            0 :         }
     786            0 : }
     787              : 
     788              : /*
     789              :  * Callback to update our notion of the current filename.
     790              :  *
     791              :  * No other code should modify progress_filename!
     792              :  */
     793              : static void
     794            0 : progress_update_filename(const char *filename)
     795              : {
     796              :         /* We needn't maintain this variable if not doing verbose reports. */
     797            0 :         if (showprogress && verbose)
     798              :         {
     799            0 :                 free(progress_filename);
     800            0 :                 if (filename)
     801            0 :                         progress_filename = pg_strdup(filename);
     802              :                 else
     803            0 :                         progress_filename = NULL;
     804            0 :         }
     805            0 : }
     806              : 
     807              : /*
     808              :  * Print a progress report based on the global variables. If verbose output
     809              :  * is enabled, also print the current file name.
     810              :  *
     811              :  * Progress report is written at maximum once per second, unless the force
     812              :  * parameter is set to true.
     813              :  *
     814              :  * If finished is set to true, this is the last progress report. The cursor
     815              :  * is moved to the next line.
     816              :  */
     817              : static void
     818            0 : progress_report(int tablespacenum, bool force, bool finished)
     819              : {
     820            0 :         int                     percent;
     821            0 :         char            totaldone_str[32];
     822            0 :         char            totalsize_str[32];
     823            0 :         pg_time_t       now;
     824              : 
     825            0 :         if (!showprogress)
     826            0 :                 return;
     827              : 
     828            0 :         now = time(NULL);
     829            0 :         if (now == last_progress_report && !force && !finished)
     830            0 :                 return;                                 /* Max once per second */
     831              : 
     832            0 :         last_progress_report = now;
     833            0 :         percent = totalsize_kb ? (int) ((totaldone / 1024) * 100 / totalsize_kb) : 0;
     834              : 
     835              :         /*
     836              :          * Avoid overflowing past 100% or the full size. This may make the total
     837              :          * size number change as we approach the end of the backup (the estimate
     838              :          * will always be wrong if WAL is included), but that's better than having
     839              :          * the done column be bigger than the total.
     840              :          */
     841            0 :         if (percent > 100)
     842            0 :                 percent = 100;
     843            0 :         if (totaldone / 1024 > totalsize_kb)
     844            0 :                 totalsize_kb = totaldone / 1024;
     845              : 
     846            0 :         snprintf(totaldone_str, sizeof(totaldone_str), UINT64_FORMAT,
     847            0 :                          totaldone / 1024);
     848            0 :         snprintf(totalsize_str, sizeof(totalsize_str), UINT64_FORMAT, totalsize_kb);
     849              : 
     850              : #define VERBOSE_FILENAME_LENGTH 35
     851            0 :         if (verbose)
     852              :         {
     853            0 :                 if (!progress_filename)
     854              : 
     855              :                         /*
     856              :                          * No filename given, so clear the status line (used for last
     857              :                          * call)
     858              :                          */
     859            0 :                         fprintf(stderr,
     860            0 :                                         ngettext("%*s/%s kB (100%%), %d/%d tablespace %*s",
     861              :                                                          "%*s/%s kB (100%%), %d/%d tablespaces %*s",
     862            0 :                                                          tablespacecount),
     863            0 :                                         (int) strlen(totalsize_str),
     864            0 :                                         totaldone_str, totalsize_str,
     865            0 :                                         tablespacenum, tablespacecount,
     866              :                                         VERBOSE_FILENAME_LENGTH + 5, "");
     867              :                 else
     868              :                 {
     869            0 :                         bool            truncate = (strlen(progress_filename) > VERBOSE_FILENAME_LENGTH);
     870              : 
     871            0 :                         fprintf(stderr,
     872            0 :                                         ngettext("%*s/%s kB (%d%%), %d/%d tablespace (%s%-*.*s)",
     873              :                                                          "%*s/%s kB (%d%%), %d/%d tablespaces (%s%-*.*s)",
     874            0 :                                                          tablespacecount),
     875            0 :                                         (int) strlen(totalsize_str),
     876            0 :                                         totaldone_str, totalsize_str, percent,
     877            0 :                                         tablespacenum, tablespacecount,
     878              :                         /* Prefix with "..." if we do leading truncation */
     879            0 :                                         truncate ? "..." : "",
     880            0 :                                         truncate ? VERBOSE_FILENAME_LENGTH - 3 : VERBOSE_FILENAME_LENGTH,
     881            0 :                                         truncate ? VERBOSE_FILENAME_LENGTH - 3 : VERBOSE_FILENAME_LENGTH,
     882              :                         /* Truncate filename at beginning if it's too long */
     883            0 :                                         truncate ? progress_filename + strlen(progress_filename) - VERBOSE_FILENAME_LENGTH + 3 : progress_filename);
     884            0 :                 }
     885            0 :         }
     886              :         else
     887            0 :                 fprintf(stderr,
     888            0 :                                 ngettext("%*s/%s kB (%d%%), %d/%d tablespace",
     889              :                                                  "%*s/%s kB (%d%%), %d/%d tablespaces",
     890            0 :                                                  tablespacecount),
     891            0 :                                 (int) strlen(totalsize_str),
     892            0 :                                 totaldone_str, totalsize_str, percent,
     893            0 :                                 tablespacenum, tablespacecount);
     894              : 
     895              :         /*
     896              :          * Stay on the same line if reporting to a terminal and we're not done
     897              :          * yet.
     898              :          */
     899            0 :         fputc((!finished && isatty(fileno(stderr))) ? '\r' : '\n', stderr);
     900            0 : }
     901              : 
     902              : static int32
     903            0 : parse_max_rate(char *src)
     904              : {
     905            0 :         double          result;
     906            0 :         char       *after_num;
     907            0 :         char       *suffix = NULL;
     908              : 
     909            0 :         errno = 0;
     910            0 :         result = strtod(src, &after_num);
     911            0 :         if (src == after_num)
     912            0 :                 pg_fatal("transfer rate \"%s\" is not a valid value", src);
     913            0 :         if (errno != 0)
     914            0 :                 pg_fatal("invalid transfer rate \"%s\": %m", src);
     915              : 
     916            0 :         if (result <= 0)
     917              :         {
     918              :                 /*
     919              :                  * Reject obviously wrong values here.
     920              :                  */
     921            0 :                 pg_fatal("transfer rate must be greater than zero");
     922            0 :         }
     923              : 
     924              :         /*
     925              :          * Evaluate suffix, after skipping over possible whitespace. Lack of
     926              :          * suffix means kilobytes.
     927              :          */
     928            0 :         while (*after_num != '\0' && isspace((unsigned char) *after_num))
     929            0 :                 after_num++;
     930              : 
     931            0 :         if (*after_num != '\0')
     932              :         {
     933            0 :                 suffix = after_num;
     934            0 :                 if (*after_num == 'k')
     935              :                 {
     936              :                         /* kilobyte is the expected unit. */
     937            0 :                         after_num++;
     938            0 :                 }
     939            0 :                 else if (*after_num == 'M')
     940              :                 {
     941            0 :                         after_num++;
     942            0 :                         result *= 1024.0;
     943            0 :                 }
     944            0 :         }
     945              : 
     946              :         /* The rest can only consist of white space. */
     947            0 :         while (*after_num != '\0' && isspace((unsigned char) *after_num))
     948            0 :                 after_num++;
     949              : 
     950            0 :         if (*after_num != '\0')
     951            0 :                 pg_fatal("invalid --max-rate unit: \"%s\"", suffix);
     952              : 
     953              :         /* Valid integer? */
     954            0 :         if ((uint64) result != (uint64) ((uint32) result))
     955            0 :                 pg_fatal("transfer rate \"%s\" exceeds integer range", src);
     956              : 
     957              :         /*
     958              :          * The range is checked on the server side too, but avoid the server
     959              :          * connection if a nonsensical value was passed.
     960              :          */
     961            0 :         if (result < MAX_RATE_LOWER || result > MAX_RATE_UPPER)
     962            0 :                 pg_fatal("transfer rate \"%s\" is out of range", src);
     963              : 
     964            0 :         return (int32) result;
     965            0 : }
     966              : 
     967              : /*
     968              :  * Basic parsing of a value specified for -Z/--compress.
     969              :  *
     970              :  * We're not concerned here with understanding exactly what behavior the
     971              :  * user wants, but we do need to know whether the user is requesting client
     972              :  * or server side compression or leaving it unspecified, and we need to
     973              :  * separate the name of the compression algorithm from the detail string.
     974              :  *
     975              :  * For instance, if the user writes --compress client-lz4:6, we want to
     976              :  * separate that into (a) client-side compression, (b) algorithm "lz4",
     977              :  * and (c) detail "6". Note, however, that all the client/server prefix is
     978              :  * optional, and so is the detail. The algorithm name is required, unless
     979              :  * the whole string is an integer, in which case we assume "gzip" as the
     980              :  * algorithm and use the integer as the detail.
     981              :  *
     982              :  * We're not concerned with validation at this stage, so if the user writes
     983              :  * --compress client-turkey:sandwich, the requested algorithm is "turkey"
     984              :  * and the detail string is "sandwich". We'll sort out whether that's legal
     985              :  * at a later stage.
     986              :  */
     987              : static void
     988            0 : backup_parse_compress_options(char *option, char **algorithm, char **detail,
     989              :                                                           CompressionLocation *locationres)
     990              : {
     991              :         /*
     992              :          * Strip off any "client-" or "server-" prefix, calculating the location.
     993              :          */
     994            0 :         if (strncmp(option, "server-", 7) == 0)
     995              :         {
     996            0 :                 *locationres = COMPRESS_LOCATION_SERVER;
     997            0 :                 option += 7;
     998            0 :         }
     999            0 :         else if (strncmp(option, "client-", 7) == 0)
    1000              :         {
    1001            0 :                 *locationres = COMPRESS_LOCATION_CLIENT;
    1002            0 :                 option += 7;
    1003            0 :         }
    1004              :         else
    1005            0 :                 *locationres = COMPRESS_LOCATION_UNSPECIFIED;
    1006              : 
    1007              :         /* fallback to the common parsing for the algorithm and detail */
    1008            0 :         parse_compress_options(option, algorithm, detail);
    1009            0 : }
    1010              : 
    1011              : /*
    1012              :  * Read a stream of COPY data and invoke the provided callback for each
    1013              :  * chunk.
    1014              :  */
    1015              : static void
    1016            0 : ReceiveCopyData(PGconn *conn, WriteDataCallback callback,
    1017              :                                 void *callback_data)
    1018              : {
    1019            0 :         PGresult   *res;
    1020              : 
    1021              :         /* Get the COPY data stream. */
    1022            0 :         res = PQgetResult(conn);
    1023            0 :         if (PQresultStatus(res) != PGRES_COPY_OUT)
    1024            0 :                 pg_fatal("could not get COPY data stream: %s",
    1025              :                                  PQerrorMessage(conn));
    1026            0 :         PQclear(res);
    1027              : 
    1028              :         /* Loop over chunks until done. */
    1029            0 :         while (1)
    1030              :         {
    1031            0 :                 int                     r;
    1032            0 :                 char       *copybuf;
    1033              : 
    1034            0 :                 r = PQgetCopyData(conn, &copybuf, 0);
    1035            0 :                 if (r == -1)
    1036              :                 {
    1037              :                         /* End of chunk. */
    1038            0 :                         break;
    1039              :                 }
    1040            0 :                 else if (r == -2)
    1041            0 :                         pg_fatal("could not read COPY data: %s",
    1042              :                                          PQerrorMessage(conn));
    1043              : 
    1044            0 :                 if (bgchild_exited)
    1045            0 :                         pg_fatal("background process terminated unexpectedly");
    1046              : 
    1047            0 :                 (*callback) (r, copybuf, callback_data);
    1048              : 
    1049            0 :                 PQfreemem(copybuf);
    1050            0 :         }
    1051            0 : }
    1052              : 
    1053              : /*
    1054              :  * Figure out what to do with an archive received from the server based on
    1055              :  * the options selected by the user.  We may just write the results directly
    1056              :  * to a file, or we might compress first, or we might extract the tar file
    1057              :  * and write each member separately. This function doesn't do any of that
    1058              :  * directly, but it works out what kind of astreamer we need to create so
    1059              :  * that the right stuff happens when, down the road, we actually receive
    1060              :  * the data.
    1061              :  */
    1062              : static astreamer *
    1063            0 : CreateBackupStreamer(char *archive_name, char *spclocation,
    1064              :                                          astreamer **manifest_inject_streamer_p,
    1065              :                                          bool is_recovery_guc_supported,
    1066              :                                          bool expect_unterminated_tarfile,
    1067              :                                          pg_compress_specification *compress)
    1068              : {
    1069            0 :         astreamer  *streamer = NULL;
    1070            0 :         astreamer  *manifest_inject_streamer = NULL;
    1071            0 :         bool            inject_manifest;
    1072            0 :         bool            is_tar,
    1073              :                                 is_tar_gz,
    1074              :                                 is_tar_lz4,
    1075              :                                 is_tar_zstd,
    1076              :                                 is_compressed_tar;
    1077            0 :         bool            must_parse_archive;
    1078            0 :         int                     archive_name_len = strlen(archive_name);
    1079              : 
    1080              :         /*
    1081              :          * Normally, we emit the backup manifest as a separate file, but when
    1082              :          * we're writing a tarfile to stdout, we don't have that option, so
    1083              :          * include it in the one tarfile we've got.
    1084              :          */
    1085            0 :         inject_manifest = (format == 't' && strcmp(basedir, "-") == 0 && manifest);
    1086              : 
    1087              :         /* Is this a tar archive? */
    1088            0 :         is_tar = (archive_name_len > 4 &&
    1089            0 :                           strcmp(archive_name + archive_name_len - 4, ".tar") == 0);
    1090              : 
    1091              :         /* Is this a .tar.gz archive? */
    1092            0 :         is_tar_gz = (archive_name_len > 7 &&
    1093            0 :                                  strcmp(archive_name + archive_name_len - 7, ".tar.gz") == 0);
    1094              : 
    1095              :         /* Is this a .tar.lz4 archive? */
    1096            0 :         is_tar_lz4 = (archive_name_len > 8 &&
    1097            0 :                                   strcmp(archive_name + archive_name_len - 8, ".tar.lz4") == 0);
    1098              : 
    1099              :         /* Is this a .tar.zst archive? */
    1100            0 :         is_tar_zstd = (archive_name_len > 8 &&
    1101            0 :                                    strcmp(archive_name + archive_name_len - 8, ".tar.zst") == 0);
    1102              : 
    1103              :         /* Is this any kind of compressed tar? */
    1104            0 :         is_compressed_tar = is_tar_gz || is_tar_lz4 || is_tar_zstd;
    1105              : 
    1106              :         /*
    1107              :          * Injecting the manifest into a compressed tar file would be possible if
    1108              :          * we decompressed it, parsed the tarfile, generated a new tarfile, and
    1109              :          * recompressed it, but compressing and decompressing multiple times just
    1110              :          * to inject the manifest seems inefficient enough that it's probably not
    1111              :          * what the user wants. So, instead, reject the request and tell the user
    1112              :          * to specify something more reasonable.
    1113              :          */
    1114            0 :         if (inject_manifest && is_compressed_tar)
    1115              :         {
    1116            0 :                 pg_log_error("cannot inject manifest into a compressed tar file");
    1117            0 :                 pg_log_error_hint("Use client-side compression, send the output to a directory rather than standard output, or use %s.",
    1118              :                                                   "--no-manifest");
    1119            0 :                 exit(1);
    1120              :         }
    1121              : 
    1122              :         /*
    1123              :          * We have to parse the archive if (1) we're suppose to extract it, or if
    1124              :          * (2) we need to inject backup_manifest or recovery configuration into
    1125              :          * it. However, we only know how to parse tar archives.
    1126              :          */
    1127            0 :         must_parse_archive = (format == 'p' || inject_manifest ||
    1128            0 :                                                   (spclocation == NULL && writerecoveryconf));
    1129              : 
    1130              :         /* At present, we only know how to parse tar archives. */
    1131            0 :         if (must_parse_archive && !is_tar && !is_compressed_tar)
    1132              :         {
    1133            0 :                 pg_log_error("cannot parse archive \"%s\"", archive_name);
    1134            0 :                 pg_log_error_detail("Only tar archives can be parsed.");
    1135            0 :                 if (format == 'p')
    1136            0 :                         pg_log_error_detail("Plain format requires pg_basebackup to parse the archive.");
    1137            0 :                 if (inject_manifest)
    1138            0 :                         pg_log_error_detail("Using - as the output directory requires pg_basebackup to parse the archive.");
    1139            0 :                 if (writerecoveryconf)
    1140            0 :                         pg_log_error_detail("The -R option requires pg_basebackup to parse the archive.");
    1141            0 :                 exit(1);
    1142              :         }
    1143              : 
    1144            0 :         if (format == 'p')
    1145              :         {
    1146            0 :                 const char *directory;
    1147              : 
    1148              :                 /*
    1149              :                  * In plain format, we must extract the archive. The data for the main
    1150              :                  * tablespace will be written to the base directory, and the data for
    1151              :                  * other tablespaces will be written to the directory where they're
    1152              :                  * located on the server, after applying any user-specified tablespace
    1153              :                  * mappings.
    1154              :                  *
    1155              :                  * In the case of an in-place tablespace, spclocation will be a
    1156              :                  * relative path. We just convert it to an absolute path by prepending
    1157              :                  * basedir.
    1158              :                  */
    1159            0 :                 if (spclocation == NULL)
    1160            0 :                         directory = basedir;
    1161            0 :                 else if (!is_absolute_path(spclocation))
    1162            0 :                         directory = psprintf("%s/%s", basedir, spclocation);
    1163              :                 else
    1164            0 :                         directory = get_tablespace_mapping(spclocation);
    1165            0 :                 streamer = astreamer_extractor_new(directory,
    1166              :                                                                                    get_tablespace_mapping,
    1167              :                                                                                    progress_update_filename);
    1168            0 :         }
    1169              :         else
    1170              :         {
    1171            0 :                 FILE       *archive_file;
    1172            0 :                 char            archive_filename[MAXPGPATH];
    1173              : 
    1174              :                 /*
    1175              :                  * In tar format, we just write the archive without extracting it.
    1176              :                  * Normally, we write it to the archive name provided by the caller,
    1177              :                  * but when the base directory is "-" that means we need to write to
    1178              :                  * standard output.
    1179              :                  */
    1180            0 :                 if (strcmp(basedir, "-") == 0)
    1181              :                 {
    1182            0 :                         snprintf(archive_filename, sizeof(archive_filename), "-");
    1183            0 :                         archive_file = stdout;
    1184            0 :                 }
    1185              :                 else
    1186              :                 {
    1187            0 :                         snprintf(archive_filename, sizeof(archive_filename),
    1188            0 :                                          "%s/%s", basedir, archive_name);
    1189            0 :                         archive_file = NULL;
    1190              :                 }
    1191              : 
    1192            0 :                 if (compress->algorithm == PG_COMPRESSION_NONE)
    1193            0 :                         streamer = astreamer_plain_writer_new(archive_filename,
    1194            0 :                                                                                                   archive_file);
    1195            0 :                 else if (compress->algorithm == PG_COMPRESSION_GZIP)
    1196              :                 {
    1197            0 :                         strlcat(archive_filename, ".gz", sizeof(archive_filename));
    1198            0 :                         streamer = astreamer_gzip_writer_new(archive_filename,
    1199            0 :                                                                                                  archive_file, compress);
    1200            0 :                 }
    1201            0 :                 else if (compress->algorithm == PG_COMPRESSION_LZ4)
    1202              :                 {
    1203            0 :                         strlcat(archive_filename, ".lz4", sizeof(archive_filename));
    1204            0 :                         streamer = astreamer_plain_writer_new(archive_filename,
    1205            0 :                                                                                                   archive_file);
    1206            0 :                         streamer = astreamer_lz4_compressor_new(streamer, compress);
    1207            0 :                 }
    1208            0 :                 else if (compress->algorithm == PG_COMPRESSION_ZSTD)
    1209              :                 {
    1210            0 :                         strlcat(archive_filename, ".zst", sizeof(archive_filename));
    1211            0 :                         streamer = astreamer_plain_writer_new(archive_filename,
    1212            0 :                                                                                                   archive_file);
    1213            0 :                         streamer = astreamer_zstd_compressor_new(streamer, compress);
    1214            0 :                 }
    1215              :                 else
    1216              :                 {
    1217            0 :                         Assert(false);          /* not reachable */
    1218              :                 }
    1219              : 
    1220              :                 /*
    1221              :                  * If we need to parse the archive for whatever reason, then we'll
    1222              :                  * also need to re-archive, because, if the output format is tar, the
    1223              :                  * only point of parsing the archive is to be able to inject stuff
    1224              :                  * into it.
    1225              :                  */
    1226            0 :                 if (must_parse_archive)
    1227            0 :                         streamer = astreamer_tar_archiver_new(streamer);
    1228            0 :                 progress_update_filename(archive_filename);
    1229            0 :         }
    1230              : 
    1231              :         /*
    1232              :          * If we're supposed to inject the backup manifest into the results, it
    1233              :          * should be done here, so that the file content can be injected directly,
    1234              :          * without worrying about the details of the tar format.
    1235              :          */
    1236            0 :         if (inject_manifest)
    1237            0 :                 manifest_inject_streamer = streamer;
    1238              : 
    1239              :         /*
    1240              :          * If this is the main tablespace and we're supposed to write recovery
    1241              :          * information, arrange to do that.
    1242              :          */
    1243            0 :         if (spclocation == NULL && writerecoveryconf)
    1244              :         {
    1245            0 :                 Assert(must_parse_archive);
    1246            0 :                 streamer = astreamer_recovery_injector_new(streamer,
    1247            0 :                                                                                                    is_recovery_guc_supported,
    1248            0 :                                                                                                    recoveryconfcontents);
    1249            0 :         }
    1250              : 
    1251              :         /*
    1252              :          * If we're doing anything that involves understanding the contents of the
    1253              :          * archive, we'll need to parse it. If not, we can skip parsing it, but
    1254              :          * old versions of the server send improperly terminated tarfiles, so if
    1255              :          * we're talking to such a server we'll need to add the terminator here.
    1256              :          */
    1257            0 :         if (must_parse_archive)
    1258            0 :                 streamer = astreamer_tar_parser_new(streamer);
    1259            0 :         else if (expect_unterminated_tarfile)
    1260            0 :                 streamer = astreamer_tar_terminator_new(streamer);
    1261              : 
    1262              :         /*
    1263              :          * If the user has requested a server compressed archive along with
    1264              :          * archive extraction at client then we need to decompress it.
    1265              :          */
    1266            0 :         if (format == 'p')
    1267              :         {
    1268            0 :                 if (is_tar_gz)
    1269            0 :                         streamer = astreamer_gzip_decompressor_new(streamer);
    1270            0 :                 else if (is_tar_lz4)
    1271            0 :                         streamer = astreamer_lz4_decompressor_new(streamer);
    1272            0 :                 else if (is_tar_zstd)
    1273            0 :                         streamer = astreamer_zstd_decompressor_new(streamer);
    1274            0 :         }
    1275              : 
    1276              :         /* Return the results. */
    1277            0 :         *manifest_inject_streamer_p = manifest_inject_streamer;
    1278            0 :         return streamer;
    1279            0 : }
    1280              : 
    1281              : /*
    1282              :  * Receive all of the archives the server wants to send - and the backup
    1283              :  * manifest if present - as a single COPY stream.
    1284              :  */
    1285              : static void
    1286            0 : ReceiveArchiveStream(PGconn *conn, pg_compress_specification *compress)
    1287              : {
    1288            0 :         ArchiveStreamState state;
    1289              : 
    1290              :         /* Set up initial state. */
    1291            0 :         memset(&state, 0, sizeof(state));
    1292            0 :         state.tablespacenum = -1;
    1293            0 :         state.compress = compress;
    1294              : 
    1295              :         /* All the real work happens in ReceiveArchiveStreamChunk. */
    1296            0 :         ReceiveCopyData(conn, ReceiveArchiveStreamChunk, &state);
    1297              : 
    1298              :         /* If we wrote the backup manifest to a file, close the file. */
    1299            0 :         if (state.manifest_file !=NULL)
    1300              :         {
    1301            0 :                 fclose(state.manifest_file);
    1302            0 :                 state.manifest_file = NULL;
    1303            0 :         }
    1304              : 
    1305              :         /*
    1306              :          * If we buffered the backup manifest in order to inject it into the
    1307              :          * output tarfile, do that now.
    1308              :          */
    1309            0 :         if (state.manifest_inject_streamer != NULL &&
    1310            0 :                 state.manifest_buffer != NULL)
    1311              :         {
    1312            0 :                 astreamer_inject_file(state.manifest_inject_streamer,
    1313              :                                                           "backup_manifest",
    1314            0 :                                                           state.manifest_buffer->data,
    1315            0 :                                                           state.manifest_buffer->len);
    1316            0 :                 destroyPQExpBuffer(state.manifest_buffer);
    1317            0 :                 state.manifest_buffer = NULL;
    1318            0 :         }
    1319              : 
    1320              :         /* If there's still an archive in progress, end processing. */
    1321            0 :         if (state.streamer != NULL)
    1322              :         {
    1323            0 :                 astreamer_finalize(state.streamer);
    1324            0 :                 astreamer_free(state.streamer);
    1325            0 :                 state.streamer = NULL;
    1326            0 :         }
    1327            0 : }
    1328              : 
    1329              : /*
    1330              :  * Receive one chunk of data sent by the server as part of a single COPY
    1331              :  * stream that includes all archives and the manifest.
    1332              :  */
    1333              : static void
    1334            0 : ReceiveArchiveStreamChunk(size_t r, char *copybuf, void *callback_data)
    1335              : {
    1336            0 :         ArchiveStreamState *state = callback_data;
    1337            0 :         size_t          cursor = 0;
    1338              : 
    1339              :         /* Each CopyData message begins with a type byte. */
    1340            0 :         switch (GetCopyDataByte(r, copybuf, &cursor))
    1341              :         {
    1342              :                 case PqBackupMsg_NewArchive:
    1343              :                         {
    1344              :                                 /* New archive. */
    1345            0 :                                 char       *archive_name;
    1346            0 :                                 char       *spclocation;
    1347              : 
    1348              :                                 /*
    1349              :                                  * We force a progress report at the end of each tablespace. A
    1350              :                                  * new tablespace starts when the previous one ends, except in
    1351              :                                  * the case of the very first one.
    1352              :                                  */
    1353            0 :                                 if (++state->tablespacenum > 0)
    1354            0 :                                         progress_report(state->tablespacenum, true, false);
    1355              : 
    1356              :                                 /* Sanity check. */
    1357            0 :                                 if (state->manifest_buffer != NULL ||
    1358            0 :                                         state->manifest_file !=NULL)
    1359            0 :                                         pg_fatal("archives must precede manifest");
    1360              : 
    1361              :                                 /* Parse the rest of the CopyData message. */
    1362            0 :                                 archive_name = GetCopyDataString(r, copybuf, &cursor);
    1363            0 :                                 spclocation = GetCopyDataString(r, copybuf, &cursor);
    1364            0 :                                 GetCopyDataEnd(r, copybuf, cursor);
    1365              : 
    1366              :                                 /*
    1367              :                                  * Basic sanity checks on the archive name: it shouldn't be
    1368              :                                  * empty, it shouldn't start with a dot, and it shouldn't
    1369              :                                  * contain a path separator.
    1370              :                                  */
    1371            0 :                                 if (archive_name[0] == '\0' || archive_name[0] == '.' ||
    1372            0 :                                         strchr(archive_name, '/') != NULL ||
    1373            0 :                                         strchr(archive_name, '\\') != NULL)
    1374            0 :                                         pg_fatal("invalid archive name: \"%s\"",
    1375              :                                                          archive_name);
    1376              : 
    1377              :                                 /*
    1378              :                                  * An empty spclocation is treated as NULL. We expect this
    1379              :                                  * case to occur for the data directory itself, but not for
    1380              :                                  * any archives that correspond to tablespaces.
    1381              :                                  */
    1382            0 :                                 if (spclocation[0] == '\0')
    1383            0 :                                         spclocation = NULL;
    1384              : 
    1385              :                                 /* End processing of any prior archive. */
    1386            0 :                                 if (state->streamer != NULL)
    1387              :                                 {
    1388            0 :                                         astreamer_finalize(state->streamer);
    1389            0 :                                         astreamer_free(state->streamer);
    1390            0 :                                         state->streamer = NULL;
    1391            0 :                                 }
    1392              : 
    1393              :                                 /*
    1394              :                                  * Create an appropriate backup streamer, unless a backup
    1395              :                                  * target was specified. In that case, it's up to the server
    1396              :                                  * to put the backup wherever it needs to go.
    1397              :                                  */
    1398            0 :                                 if (backup_target == NULL)
    1399              :                                 {
    1400              :                                         /*
    1401              :                                          * We know that recovery GUCs are supported, because this
    1402              :                                          * protocol can only be used on v15+.
    1403              :                                          */
    1404            0 :                                         state->streamer =
    1405            0 :                                                 CreateBackupStreamer(archive_name,
    1406            0 :                                                                                          spclocation,
    1407            0 :                                                                                          &state->manifest_inject_streamer,
    1408              :                                                                                          true, false,
    1409            0 :                                                                                          state->compress);
    1410            0 :                                 }
    1411              :                                 break;
    1412            0 :                         }
    1413              : 
    1414              :                 case PqMsg_CopyData:
    1415              :                         {
    1416              :                                 /* Archive or manifest data. */
    1417            0 :                                 if (state->manifest_buffer != NULL)
    1418              :                                 {
    1419              :                                         /* Manifest data, buffer in memory. */
    1420            0 :                                         appendPQExpBuffer(state->manifest_buffer, copybuf + 1,
    1421            0 :                                                                           r - 1);
    1422            0 :                                 }
    1423            0 :                                 else if (state->manifest_file !=NULL)
    1424              :                                 {
    1425              :                                         /* Manifest data, write to disk. */
    1426            0 :                                         if (fwrite(copybuf + 1, r - 1, 1,
    1427            0 :                                                            state->manifest_file) != 1)
    1428              :                                         {
    1429              :                                                 /*
    1430              :                                                  * If fwrite() didn't set errno, assume that the
    1431              :                                                  * problem is that we're out of disk space.
    1432              :                                                  */
    1433            0 :                                                 if (errno == 0)
    1434            0 :                                                         errno = ENOSPC;
    1435            0 :                                                 pg_fatal("could not write to file \"%s\": %m",
    1436              :                                                                  state->manifest_filename);
    1437            0 :                                         }
    1438            0 :                                 }
    1439            0 :                                 else if (state->streamer != NULL)
    1440              :                                 {
    1441              :                                         /* Archive data. */
    1442            0 :                                         astreamer_content(state->streamer, NULL, copybuf + 1,
    1443            0 :                                                                           r - 1, ASTREAMER_UNKNOWN);
    1444            0 :                                 }
    1445              :                                 else
    1446            0 :                                         pg_fatal("unexpected payload data");
    1447            0 :                                 break;
    1448              :                         }
    1449              : 
    1450              :                 case PqBackupMsg_ProgressReport:
    1451              :                         {
    1452              :                                 /*
    1453              :                                  * Progress report.
    1454              :                                  *
    1455              :                                  * The remainder of the message is expected to be an 8-byte
    1456              :                                  * count of bytes completed.
    1457              :                                  */
    1458            0 :                                 totaldone = GetCopyDataUInt64(r, copybuf, &cursor);
    1459            0 :                                 GetCopyDataEnd(r, copybuf, cursor);
    1460              : 
    1461              :                                 /*
    1462              :                                  * The server shouldn't send progress report messages too
    1463              :                                  * often, so we force an update each time we receive one.
    1464              :                                  */
    1465            0 :                                 progress_report(state->tablespacenum, true, false);
    1466            0 :                                 break;
    1467              :                         }
    1468              : 
    1469              :                 case PqBackupMsg_Manifest:
    1470              :                         {
    1471              :                                 /*
    1472              :                                  * Manifest data will be sent next. This message is not
    1473              :                                  * expected to have any further payload data.
    1474              :                                  */
    1475            0 :                                 GetCopyDataEnd(r, copybuf, cursor);
    1476              : 
    1477              :                                 /*
    1478              :                                  * If a backup target was specified, figuring out where to put
    1479              :                                  * the manifest is the server's problem. Otherwise, we need to
    1480              :                                  * deal with it.
    1481              :                                  */
    1482            0 :                                 if (backup_target == NULL)
    1483              :                                 {
    1484              :                                         /*
    1485              :                                          * If we're supposed inject the manifest into the archive,
    1486              :                                          * we prepare to buffer it in memory; otherwise, we
    1487              :                                          * prepare to write it to a temporary file.
    1488              :                                          */
    1489            0 :                                         if (state->manifest_inject_streamer != NULL)
    1490            0 :                                                 state->manifest_buffer = createPQExpBuffer();
    1491              :                                         else
    1492              :                                         {
    1493            0 :                                                 snprintf(state->manifest_filename,
    1494              :                                                                  sizeof(state->manifest_filename),
    1495            0 :                                                                  "%s/backup_manifest.tmp", basedir);
    1496            0 :                                                 state->manifest_file =
    1497            0 :                                                         fopen(state->manifest_filename, "wb");
    1498            0 :                                                 if (state->manifest_file == NULL)
    1499            0 :                                                         pg_fatal("could not create file \"%s\": %m",
    1500              :                                                                          state->manifest_filename);
    1501              :                                         }
    1502            0 :                                 }
    1503            0 :                                 break;
    1504              :                         }
    1505              : 
    1506              :                 default:
    1507            0 :                         ReportCopyDataParseError(r, copybuf);
    1508            0 :                         break;
    1509              :         }
    1510            0 : }
    1511              : 
    1512              : /*
    1513              :  * Get a single byte from a CopyData message.
    1514              :  *
    1515              :  * Bail out if none remain.
    1516              :  */
    1517              : static char
    1518            0 : GetCopyDataByte(size_t r, char *copybuf, size_t *cursor)
    1519              : {
    1520            0 :         if (*cursor >= r)
    1521            0 :                 ReportCopyDataParseError(r, copybuf);
    1522              : 
    1523            0 :         return copybuf[(*cursor)++];
    1524              : }
    1525              : 
    1526              : /*
    1527              :  * Get a NUL-terminated string from a CopyData message.
    1528              :  *
    1529              :  * Bail out if the terminating NUL cannot be found.
    1530              :  */
    1531              : static char *
    1532            0 : GetCopyDataString(size_t r, char *copybuf, size_t *cursor)
    1533              : {
    1534            0 :         size_t          startpos = *cursor;
    1535            0 :         size_t          endpos = startpos;
    1536              : 
    1537            0 :         while (1)
    1538              :         {
    1539            0 :                 if (endpos >= r)
    1540            0 :                         ReportCopyDataParseError(r, copybuf);
    1541            0 :                 if (copybuf[endpos] == '\0')
    1542            0 :                         break;
    1543            0 :                 ++endpos;
    1544              :         }
    1545              : 
    1546            0 :         *cursor = endpos + 1;
    1547            0 :         return &copybuf[startpos];
    1548            0 : }
    1549              : 
    1550              : /*
    1551              :  * Get an unsigned 64-bit integer from a CopyData message.
    1552              :  *
    1553              :  * Bail out if there are not at least 8 bytes remaining.
    1554              :  */
    1555              : static uint64
    1556            0 : GetCopyDataUInt64(size_t r, char *copybuf, size_t *cursor)
    1557              : {
    1558            0 :         uint64          result;
    1559              : 
    1560            0 :         if (*cursor + sizeof(uint64) > r)
    1561            0 :                 ReportCopyDataParseError(r, copybuf);
    1562            0 :         memcpy(&result, &copybuf[*cursor], sizeof(uint64));
    1563            0 :         *cursor += sizeof(uint64);
    1564            0 :         return pg_ntoh64(result);
    1565            0 : }
    1566              : 
    1567              : /*
    1568              :  * Bail out if we didn't parse the whole message.
    1569              :  */
    1570              : static void
    1571            0 : GetCopyDataEnd(size_t r, char *copybuf, size_t cursor)
    1572              : {
    1573            0 :         if (r != cursor)
    1574            0 :                 ReportCopyDataParseError(r, copybuf);
    1575            0 : }
    1576              : 
    1577              : /*
    1578              :  * Report failure to parse a CopyData message from the server. Then exit.
    1579              :  *
    1580              :  * As a debugging aid, we try to give some hint about what kind of message
    1581              :  * provoked the failure. Perhaps this is not detailed enough, but it's not
    1582              :  * clear that it's worth expending any more code on what should be a
    1583              :  * can't-happen case.
    1584              :  */
    1585              : static void
    1586            0 : ReportCopyDataParseError(size_t r, char *copybuf)
    1587              : {
    1588            0 :         if (r == 0)
    1589            0 :                 pg_fatal("empty COPY message");
    1590              :         else
    1591            0 :                 pg_fatal("malformed COPY message of type %d, length %zu",
    1592              :                                  copybuf[0], r);
    1593            0 : }
    1594              : 
    1595              : /*
    1596              :  * Receive raw tar data from the server, and stream it to the appropriate
    1597              :  * location. If we're writing a single tarfile to standard output, also
    1598              :  * receive the backup manifest and inject it into that tarfile.
    1599              :  */
    1600              : static void
    1601            0 : ReceiveTarFile(PGconn *conn, char *archive_name, char *spclocation,
    1602              :                            bool tablespacenum, pg_compress_specification *compress)
    1603              : {
    1604            0 :         WriteTarState state;
    1605            0 :         astreamer  *manifest_inject_streamer;
    1606            0 :         bool            is_recovery_guc_supported;
    1607            0 :         bool            expect_unterminated_tarfile;
    1608              : 
    1609              :         /* Pass all COPY data through to the backup streamer. */
    1610            0 :         memset(&state, 0, sizeof(state));
    1611            0 :         is_recovery_guc_supported =
    1612            0 :                 PQserverVersion(conn) >= MINIMUM_VERSION_FOR_RECOVERY_GUC;
    1613            0 :         expect_unterminated_tarfile =
    1614            0 :                 PQserverVersion(conn) < MINIMUM_VERSION_FOR_TERMINATED_TARFILE;
    1615            0 :         state.streamer = CreateBackupStreamer(archive_name, spclocation,
    1616              :                                                                                   &manifest_inject_streamer,
    1617            0 :                                                                                   is_recovery_guc_supported,
    1618            0 :                                                                                   expect_unterminated_tarfile,
    1619            0 :                                                                                   compress);
    1620            0 :         state.tablespacenum = tablespacenum;
    1621            0 :         ReceiveCopyData(conn, ReceiveTarCopyChunk, &state);
    1622            0 :         progress_update_filename(NULL);
    1623              : 
    1624              :         /*
    1625              :          * The decision as to whether we need to inject the backup manifest into
    1626              :          * the output at this stage is made by CreateBackupStreamer; if that is
    1627              :          * needed, manifest_inject_streamer will be non-NULL; otherwise, it will
    1628              :          * be NULL.
    1629              :          */
    1630            0 :         if (manifest_inject_streamer != NULL)
    1631              :         {
    1632            0 :                 PQExpBufferData buf;
    1633              : 
    1634              :                 /* Slurp the entire backup manifest into a buffer. */
    1635            0 :                 initPQExpBuffer(&buf);
    1636            0 :                 ReceiveBackupManifestInMemory(conn, &buf);
    1637            0 :                 if (PQExpBufferDataBroken(buf))
    1638            0 :                         pg_fatal("out of memory");
    1639              : 
    1640              :                 /* Inject it into the output tarfile. */
    1641            0 :                 astreamer_inject_file(manifest_inject_streamer, "backup_manifest",
    1642            0 :                                                           buf.data, buf.len);
    1643              : 
    1644              :                 /* Free memory. */
    1645            0 :                 termPQExpBuffer(&buf);
    1646            0 :         }
    1647              : 
    1648              :         /* Cleanup. */
    1649            0 :         astreamer_finalize(state.streamer);
    1650            0 :         astreamer_free(state.streamer);
    1651              : 
    1652            0 :         progress_report(tablespacenum, true, false);
    1653              : 
    1654              :         /*
    1655              :          * Do not sync the resulting tar file yet, all files are synced once at
    1656              :          * the end.
    1657              :          */
    1658            0 : }
    1659              : 
    1660              : /*
    1661              :  * Receive one chunk of tar-format data from the server.
    1662              :  */
    1663              : static void
    1664            0 : ReceiveTarCopyChunk(size_t r, char *copybuf, void *callback_data)
    1665              : {
    1666            0 :         WriteTarState *state = callback_data;
    1667              : 
    1668            0 :         astreamer_content(state->streamer, NULL, copybuf, r, ASTREAMER_UNKNOWN);
    1669              : 
    1670            0 :         totaldone += r;
    1671            0 :         progress_report(state->tablespacenum, false, false);
    1672            0 : }
    1673              : 
    1674              : 
    1675              : /*
    1676              :  * Retrieve tablespace path, either relocated or original depending on whether
    1677              :  * -T was passed or not.
    1678              :  */
    1679              : static const char *
    1680            0 : get_tablespace_mapping(const char *dir)
    1681              : {
    1682            0 :         TablespaceListCell *cell;
    1683            0 :         char            canon_dir[MAXPGPATH];
    1684              : 
    1685              :         /* Canonicalize path for comparison consistency */
    1686            0 :         strlcpy(canon_dir, dir, sizeof(canon_dir));
    1687            0 :         canonicalize_path(canon_dir);
    1688              : 
    1689            0 :         for (cell = tablespace_dirs.head; cell; cell = cell->next)
    1690            0 :                 if (strcmp(canon_dir, cell->old_dir) == 0)
    1691            0 :                         return cell->new_dir;
    1692              : 
    1693            0 :         return dir;
    1694            0 : }
    1695              : 
    1696              : /*
    1697              :  * Receive the backup manifest file and write it out to a file.
    1698              :  */
    1699              : static void
    1700            0 : ReceiveBackupManifest(PGconn *conn)
    1701              : {
    1702            0 :         WriteManifestState state;
    1703              : 
    1704            0 :         snprintf(state.filename, sizeof(state.filename),
    1705            0 :                          "%s/backup_manifest.tmp", basedir);
    1706            0 :         state.file = fopen(state.filename, "wb");
    1707            0 :         if (state.file == NULL)
    1708            0 :                 pg_fatal("could not create file \"%s\": %m", state.filename);
    1709              : 
    1710            0 :         ReceiveCopyData(conn, ReceiveBackupManifestChunk, &state);
    1711              : 
    1712            0 :         fclose(state.file);
    1713            0 : }
    1714              : 
    1715              : /*
    1716              :  * Receive one chunk of the backup manifest file and write it out to a file.
    1717              :  */
    1718              : static void
    1719            0 : ReceiveBackupManifestChunk(size_t r, char *copybuf, void *callback_data)
    1720              : {
    1721            0 :         WriteManifestState *state = callback_data;
    1722              : 
    1723            0 :         errno = 0;
    1724            0 :         if (fwrite(copybuf, r, 1, state->file) != 1)
    1725              :         {
    1726              :                 /* if write didn't set errno, assume problem is no disk space */
    1727            0 :                 if (errno == 0)
    1728            0 :                         errno = ENOSPC;
    1729            0 :                 pg_fatal("could not write to file \"%s\": %m", state->filename);
    1730            0 :         }
    1731            0 : }
    1732              : 
    1733              : /*
    1734              :  * Receive the backup manifest file and write it out to a file.
    1735              :  */
    1736              : static void
    1737            0 : ReceiveBackupManifestInMemory(PGconn *conn, PQExpBuffer buf)
    1738              : {
    1739            0 :         ReceiveCopyData(conn, ReceiveBackupManifestInMemoryChunk, buf);
    1740            0 : }
    1741              : 
    1742              : /*
    1743              :  * Receive one chunk of the backup manifest file and write it out to a file.
    1744              :  */
    1745              : static void
    1746            0 : ReceiveBackupManifestInMemoryChunk(size_t r, char *copybuf,
    1747              :                                                                    void *callback_data)
    1748              : {
    1749            0 :         PQExpBuffer buf = callback_data;
    1750              : 
    1751            0 :         appendPQExpBuffer(buf, copybuf, r);
    1752            0 : }
    1753              : 
    1754              : static void
    1755            0 : BaseBackup(char *compression_algorithm, char *compression_detail,
    1756              :                    CompressionLocation compressloc,
    1757              :                    pg_compress_specification *client_compress,
    1758              :                    char *incremental_manifest)
    1759              : {
    1760            0 :         PGresult   *res;
    1761            0 :         char       *sysidentifier;
    1762            0 :         TimeLineID      latesttli;
    1763            0 :         TimeLineID      starttli;
    1764            0 :         char       *basebkp;
    1765            0 :         int                     i;
    1766            0 :         char            xlogstart[64];
    1767            0 :         char            xlogend[64] = {0};
    1768            0 :         int                     minServerMajor,
    1769              :                                 maxServerMajor;
    1770            0 :         int                     serverVersion,
    1771              :                                 serverMajor;
    1772            0 :         int                     writing_to_stdout;
    1773            0 :         bool            use_new_option_syntax = false;
    1774            0 :         PQExpBufferData buf;
    1775              : 
    1776            0 :         Assert(conn != NULL);
    1777            0 :         initPQExpBuffer(&buf);
    1778              : 
    1779              :         /*
    1780              :          * Check server version. BASE_BACKUP command was introduced in 9.1, so we
    1781              :          * can't work with servers older than 9.1.
    1782              :          */
    1783            0 :         minServerMajor = 901;
    1784            0 :         maxServerMajor = PG_VERSION_NUM / 100;
    1785            0 :         serverVersion = PQserverVersion(conn);
    1786            0 :         serverMajor = serverVersion / 100;
    1787            0 :         if (serverMajor < minServerMajor || serverMajor > maxServerMajor)
    1788              :         {
    1789            0 :                 const char *serverver = PQparameterStatus(conn, "server_version");
    1790              : 
    1791            0 :                 pg_fatal("incompatible server version %s",
    1792              :                                  serverver ? serverver : "'unknown'");
    1793            0 :         }
    1794            0 :         if (serverMajor >= 1500)
    1795            0 :                 use_new_option_syntax = true;
    1796              : 
    1797              :         /*
    1798              :          * If WAL streaming was requested, also check that the server is new
    1799              :          * enough for that.
    1800              :          */
    1801            0 :         if (includewal == STREAM_WAL && !CheckServerVersionForStreaming(conn))
    1802              :         {
    1803              :                 /*
    1804              :                  * Error message already written in CheckServerVersionForStreaming(),
    1805              :                  * but add a hint about using -X none.
    1806              :                  */
    1807            0 :                 pg_log_error_hint("Use -X none or -X fetch to disable log streaming.");
    1808            0 :                 exit(1);
    1809              :         }
    1810              : 
    1811              :         /*
    1812              :          * Build contents of configuration file if requested.
    1813              :          *
    1814              :          * Note that we don't use the dbname from key-value pair in conn as that
    1815              :          * would have been filled by the default dbname (dbname=replication) in
    1816              :          * case the user didn't specify the one. The dbname written in the config
    1817              :          * file as part of primary_conninfo would be used by slotsync worker which
    1818              :          * doesn't use a replication connection so the default won't work for it.
    1819              :          */
    1820            0 :         if (writerecoveryconf)
    1821            0 :                 recoveryconfcontents = GenerateRecoveryConfig(conn,
    1822            0 :                                                                                                           replication_slot,
    1823            0 :                                                                                                           GetDbnameFromConnectionOptions(connection_string));
    1824              : 
    1825              :         /*
    1826              :          * Run IDENTIFY_SYSTEM so we can get the timeline
    1827              :          */
    1828            0 :         if (!RunIdentifySystem(conn, &sysidentifier, &latesttli, NULL, NULL))
    1829            0 :                 exit(1);
    1830              : 
    1831              :         /*
    1832              :          * If the user wants an incremental backup, we must upload the manifest
    1833              :          * for the previous backup upon which it is to be based.
    1834              :          */
    1835            0 :         if (incremental_manifest != NULL)
    1836              :         {
    1837            0 :                 int                     fd;
    1838            0 :                 char            mbuf[65536];
    1839            0 :                 int                     nbytes;
    1840              : 
    1841              :                 /* Reject if server is too old. */
    1842            0 :                 if (serverVersion < MINIMUM_VERSION_FOR_WAL_SUMMARIES)
    1843            0 :                         pg_fatal("server does not support incremental backup");
    1844              : 
    1845              :                 /* Open the file. */
    1846            0 :                 fd = open(incremental_manifest, O_RDONLY | PG_BINARY, 0);
    1847            0 :                 if (fd < 0)
    1848            0 :                         pg_fatal("could not open file \"%s\": %m", incremental_manifest);
    1849              : 
    1850              :                 /* Tell the server what we want to do. */
    1851            0 :                 if (PQsendQuery(conn, "UPLOAD_MANIFEST") == 0)
    1852            0 :                         pg_fatal("could not send replication command \"%s\": %s",
    1853              :                                          "UPLOAD_MANIFEST", PQerrorMessage(conn));
    1854            0 :                 res = PQgetResult(conn);
    1855            0 :                 if (PQresultStatus(res) != PGRES_COPY_IN)
    1856              :                 {
    1857            0 :                         if (PQresultStatus(res) == PGRES_FATAL_ERROR)
    1858            0 :                                 pg_fatal("could not upload manifest: %s",
    1859              :                                                  PQerrorMessage(conn));
    1860              :                         else
    1861            0 :                                 pg_fatal("could not upload manifest: unexpected status %s",
    1862              :                                                  PQresStatus(PQresultStatus(res)));
    1863            0 :                 }
    1864              : 
    1865              :                 /* Loop, reading from the file and sending the data to the server. */
    1866            0 :                 while ((nbytes = read(fd, mbuf, sizeof mbuf)) > 0)
    1867              :                 {
    1868            0 :                         if (PQputCopyData(conn, mbuf, nbytes) < 0)
    1869            0 :                                 pg_fatal("could not send COPY data: %s",
    1870              :                                                  PQerrorMessage(conn));
    1871              :                 }
    1872              : 
    1873              :                 /* Bail out if we exited the loop due to an error. */
    1874            0 :                 if (nbytes < 0)
    1875            0 :                         pg_fatal("could not read file \"%s\": %m", incremental_manifest);
    1876              : 
    1877              :                 /* End the COPY operation. */
    1878            0 :                 if (PQputCopyEnd(conn, NULL) < 0)
    1879            0 :                         pg_fatal("could not send end-of-COPY: %s",
    1880              :                                          PQerrorMessage(conn));
    1881              : 
    1882              :                 /* See whether the server is happy with what we sent. */
    1883            0 :                 res = PQgetResult(conn);
    1884            0 :                 if (PQresultStatus(res) == PGRES_FATAL_ERROR)
    1885            0 :                         pg_fatal("could not upload manifest: %s",
    1886              :                                          PQerrorMessage(conn));
    1887            0 :                 else if (PQresultStatus(res) != PGRES_COMMAND_OK)
    1888            0 :                         pg_fatal("could not upload manifest: unexpected status %s",
    1889              :                                          PQresStatus(PQresultStatus(res)));
    1890              : 
    1891              :                 /* Consume ReadyForQuery message from server. */
    1892            0 :                 res = PQgetResult(conn);
    1893            0 :                 if (res != NULL)
    1894            0 :                         pg_fatal("unexpected extra result while sending manifest");
    1895              : 
    1896              :                 /* Add INCREMENTAL option to BASE_BACKUP command. */
    1897            0 :                 AppendPlainCommandOption(&buf, use_new_option_syntax, "INCREMENTAL");
    1898            0 :         }
    1899              : 
    1900              :         /*
    1901              :          * Continue building up the options list for the BASE_BACKUP command.
    1902              :          */
    1903            0 :         AppendStringCommandOption(&buf, use_new_option_syntax, "LABEL", label);
    1904            0 :         if (estimatesize)
    1905            0 :                 AppendPlainCommandOption(&buf, use_new_option_syntax, "PROGRESS");
    1906            0 :         if (includewal == FETCH_WAL)
    1907            0 :                 AppendPlainCommandOption(&buf, use_new_option_syntax, "WAL");
    1908            0 :         if (fastcheckpoint)
    1909              :         {
    1910            0 :                 if (use_new_option_syntax)
    1911            0 :                         AppendStringCommandOption(&buf, use_new_option_syntax,
    1912              :                                                                           "CHECKPOINT", "fast");
    1913              :                 else
    1914            0 :                         AppendPlainCommandOption(&buf, use_new_option_syntax, "FAST");
    1915            0 :         }
    1916            0 :         if (includewal != NO_WAL)
    1917              :         {
    1918            0 :                 if (use_new_option_syntax)
    1919            0 :                         AppendIntegerCommandOption(&buf, use_new_option_syntax, "WAIT", 0);
    1920              :                 else
    1921            0 :                         AppendPlainCommandOption(&buf, use_new_option_syntax, "NOWAIT");
    1922            0 :         }
    1923            0 :         if (maxrate > 0)
    1924            0 :                 AppendIntegerCommandOption(&buf, use_new_option_syntax, "MAX_RATE",
    1925            0 :                                                                    maxrate);
    1926            0 :         if (format == 't')
    1927            0 :                 AppendPlainCommandOption(&buf, use_new_option_syntax, "TABLESPACE_MAP");
    1928            0 :         if (!verify_checksums)
    1929              :         {
    1930            0 :                 if (use_new_option_syntax)
    1931            0 :                         AppendIntegerCommandOption(&buf, use_new_option_syntax,
    1932              :                                                                            "VERIFY_CHECKSUMS", 0);
    1933              :                 else
    1934            0 :                         AppendPlainCommandOption(&buf, use_new_option_syntax,
    1935              :                                                                          "NOVERIFY_CHECKSUMS");
    1936            0 :         }
    1937              : 
    1938            0 :         if (manifest)
    1939              :         {
    1940            0 :                 AppendStringCommandOption(&buf, use_new_option_syntax, "MANIFEST",
    1941            0 :                                                                   manifest_force_encode ? "force-encode" : "yes");
    1942            0 :                 if (manifest_checksums != NULL)
    1943            0 :                         AppendStringCommandOption(&buf, use_new_option_syntax,
    1944            0 :                                                                           "MANIFEST_CHECKSUMS", manifest_checksums);
    1945            0 :         }
    1946              : 
    1947            0 :         if (backup_target != NULL)
    1948              :         {
    1949            0 :                 char       *colon;
    1950              : 
    1951            0 :                 if (serverMajor < 1500)
    1952            0 :                         pg_fatal("backup targets are not supported by this server version");
    1953              : 
    1954            0 :                 if (writerecoveryconf)
    1955            0 :                         pg_fatal("recovery configuration cannot be written when a backup target is used");
    1956              : 
    1957            0 :                 AppendPlainCommandOption(&buf, use_new_option_syntax, "TABLESPACE_MAP");
    1958              : 
    1959            0 :                 if ((colon = strchr(backup_target, ':')) == NULL)
    1960              :                 {
    1961            0 :                         AppendStringCommandOption(&buf, use_new_option_syntax,
    1962            0 :                                                                           "TARGET", backup_target);
    1963            0 :                 }
    1964              :                 else
    1965              :                 {
    1966            0 :                         char       *target;
    1967              : 
    1968            0 :                         target = pnstrdup(backup_target, colon - backup_target);
    1969            0 :                         AppendStringCommandOption(&buf, use_new_option_syntax,
    1970            0 :                                                                           "TARGET", target);
    1971            0 :                         AppendStringCommandOption(&buf, use_new_option_syntax,
    1972            0 :                                                                           "TARGET_DETAIL", colon + 1);
    1973            0 :                 }
    1974            0 :         }
    1975            0 :         else if (serverMajor >= 1500)
    1976            0 :                 AppendStringCommandOption(&buf, use_new_option_syntax,
    1977              :                                                                   "TARGET", "client");
    1978              : 
    1979            0 :         if (compressloc == COMPRESS_LOCATION_SERVER)
    1980              :         {
    1981            0 :                 if (!use_new_option_syntax)
    1982            0 :                         pg_fatal("server does not support server-side compression");
    1983            0 :                 AppendStringCommandOption(&buf, use_new_option_syntax,
    1984            0 :                                                                   "COMPRESSION", compression_algorithm);
    1985            0 :                 if (compression_detail != NULL)
    1986            0 :                         AppendStringCommandOption(&buf, use_new_option_syntax,
    1987              :                                                                           "COMPRESSION_DETAIL",
    1988            0 :                                                                           compression_detail);
    1989            0 :         }
    1990              : 
    1991            0 :         if (verbose)
    1992            0 :                 pg_log_info("initiating base backup, waiting for checkpoint to complete");
    1993              : 
    1994            0 :         if (showprogress && !verbose)
    1995              :         {
    1996            0 :                 fprintf(stderr, _("waiting for checkpoint"));
    1997            0 :                 if (isatty(fileno(stderr)))
    1998            0 :                         fprintf(stderr, "\r");
    1999              :                 else
    2000            0 :                         fprintf(stderr, "\n");
    2001            0 :         }
    2002              : 
    2003            0 :         if (use_new_option_syntax && buf.len > 0)
    2004            0 :                 basebkp = psprintf("BASE_BACKUP (%s)", buf.data);
    2005              :         else
    2006            0 :                 basebkp = psprintf("BASE_BACKUP %s", buf.data);
    2007              : 
    2008              :         /* OK, try to start the backup. */
    2009            0 :         if (PQsendQuery(conn, basebkp) == 0)
    2010            0 :                 pg_fatal("could not send replication command \"%s\": %s",
    2011              :                                  "BASE_BACKUP", PQerrorMessage(conn));
    2012              : 
    2013              :         /*
    2014              :          * Get the starting WAL location
    2015              :          */
    2016            0 :         res = PQgetResult(conn);
    2017            0 :         if (PQresultStatus(res) != PGRES_TUPLES_OK)
    2018            0 :                 pg_fatal("could not initiate base backup: %s",
    2019              :                                  PQerrorMessage(conn));
    2020            0 :         if (PQntuples(res) != 1)
    2021            0 :                 pg_fatal("server returned unexpected response to BASE_BACKUP command; got %d rows and %d fields, expected %d rows and %d fields",
    2022              :                                  PQntuples(res), PQnfields(res), 1, 2);
    2023              : 
    2024            0 :         strlcpy(xlogstart, PQgetvalue(res, 0, 0), sizeof(xlogstart));
    2025              : 
    2026            0 :         if (verbose)
    2027            0 :                 pg_log_info("checkpoint completed");
    2028              : 
    2029              :         /*
    2030              :          * 9.3 and later sends the TLI of the starting point. With older servers,
    2031              :          * assume it's the same as the latest timeline reported by
    2032              :          * IDENTIFY_SYSTEM.
    2033              :          */
    2034            0 :         if (PQnfields(res) >= 2)
    2035            0 :                 starttli = atoi(PQgetvalue(res, 0, 1));
    2036              :         else
    2037            0 :                 starttli = latesttli;
    2038            0 :         PQclear(res);
    2039              : 
    2040            0 :         if (verbose && includewal != NO_WAL)
    2041            0 :                 pg_log_info("write-ahead log start point: %s on timeline %u",
    2042              :                                         xlogstart, starttli);
    2043              : 
    2044              :         /*
    2045              :          * Get the header
    2046              :          */
    2047            0 :         res = PQgetResult(conn);
    2048            0 :         if (PQresultStatus(res) != PGRES_TUPLES_OK)
    2049            0 :                 pg_fatal("could not get backup header: %s",
    2050              :                                  PQerrorMessage(conn));
    2051            0 :         if (PQntuples(res) < 1)
    2052            0 :                 pg_fatal("no data returned from server");
    2053              : 
    2054              :         /*
    2055              :          * Sum up the total size, for progress reporting
    2056              :          */
    2057            0 :         totalsize_kb = totaldone = 0;
    2058            0 :         tablespacecount = PQntuples(res);
    2059            0 :         for (i = 0; i < PQntuples(res); i++)
    2060              :         {
    2061            0 :                 totalsize_kb += atoll(PQgetvalue(res, i, 2));
    2062              : 
    2063              :                 /*
    2064              :                  * Verify tablespace directories are empty. Don't bother with the
    2065              :                  * first once since it can be relocated, and it will be checked before
    2066              :                  * we do anything anyway.
    2067              :                  *
    2068              :                  * Note that this is skipped for tar format backups and backups that
    2069              :                  * the server is storing to a target location, since in that case we
    2070              :                  * won't be storing anything into these directories and thus should
    2071              :                  * not create them.
    2072              :                  */
    2073            0 :                 if (backup_target == NULL && format == 'p' && !PQgetisnull(res, i, 1))
    2074              :                 {
    2075            0 :                         char       *path = PQgetvalue(res, i, 1);
    2076              : 
    2077            0 :                         if (is_absolute_path(path))
    2078            0 :                                 path = unconstify(char *, get_tablespace_mapping(path));
    2079              :                         else
    2080              :                         {
    2081              :                                 /* This is an in-place tablespace, so prepend basedir. */
    2082            0 :                                 path = psprintf("%s/%s", basedir, path);
    2083              :                         }
    2084              : 
    2085            0 :                         verify_dir_is_empty_or_create(path, &made_tablespace_dirs, &found_tablespace_dirs);
    2086            0 :                 }
    2087            0 :         }
    2088              : 
    2089              :         /*
    2090              :          * When writing to stdout, require a single tablespace
    2091              :          */
    2092            0 :         writing_to_stdout = format == 't' && basedir != NULL &&
    2093            0 :                 strcmp(basedir, "-") == 0;
    2094            0 :         if (writing_to_stdout && PQntuples(res) > 1)
    2095            0 :                 pg_fatal("can only write single tablespace to stdout, database has %d",
    2096              :                                  PQntuples(res));
    2097              : 
    2098              :         /*
    2099              :          * If we're streaming WAL, start the streaming session before we start
    2100              :          * receiving the actual data chunks.
    2101              :          */
    2102            0 :         if (includewal == STREAM_WAL)
    2103              :         {
    2104            0 :                 pg_compress_algorithm wal_compress_algorithm;
    2105            0 :                 int                     wal_compress_level;
    2106              : 
    2107            0 :                 if (verbose)
    2108            0 :                         pg_log_info("starting background WAL receiver");
    2109              : 
    2110            0 :                 if (client_compress->algorithm == PG_COMPRESSION_GZIP)
    2111              :                 {
    2112            0 :                         wal_compress_algorithm = PG_COMPRESSION_GZIP;
    2113            0 :                         wal_compress_level = client_compress->level;
    2114            0 :                 }
    2115              :                 else
    2116              :                 {
    2117            0 :                         wal_compress_algorithm = PG_COMPRESSION_NONE;
    2118            0 :                         wal_compress_level = 0;
    2119              :                 }
    2120              : 
    2121            0 :                 StartLogStreamer(xlogstart, starttli, sysidentifier,
    2122            0 :                                                  wal_compress_algorithm,
    2123            0 :                                                  wal_compress_level);
    2124            0 :         }
    2125              : 
    2126            0 :         if (serverMajor >= 1500)
    2127              :         {
    2128              :                 /* Receive a single tar stream with everything. */
    2129            0 :                 ReceiveArchiveStream(conn, client_compress);
    2130            0 :         }
    2131              :         else
    2132              :         {
    2133              :                 /* Receive a tar file for each tablespace in turn */
    2134            0 :                 for (i = 0; i < PQntuples(res); i++)
    2135              :                 {
    2136            0 :                         char            archive_name[MAXPGPATH];
    2137            0 :                         char       *spclocation;
    2138              : 
    2139              :                         /*
    2140              :                          * If we write the data out to a tar file, it will be named
    2141              :                          * base.tar if it's the main data directory or <tablespaceoid>.tar
    2142              :                          * if it's for another tablespace. CreateBackupStreamer() will
    2143              :                          * arrange to add an extension to the archive name if
    2144              :                          * pg_basebackup is performing compression, depending on the
    2145              :                          * compression type.
    2146              :                          */
    2147            0 :                         if (PQgetisnull(res, i, 0))
    2148              :                         {
    2149            0 :                                 strlcpy(archive_name, "base.tar", sizeof(archive_name));
    2150            0 :                                 spclocation = NULL;
    2151            0 :                         }
    2152              :                         else
    2153              :                         {
    2154            0 :                                 snprintf(archive_name, sizeof(archive_name),
    2155            0 :                                                  "%s.tar", PQgetvalue(res, i, 0));
    2156            0 :                                 spclocation = PQgetvalue(res, i, 1);
    2157              :                         }
    2158              : 
    2159            0 :                         ReceiveTarFile(conn, archive_name, spclocation, i,
    2160            0 :                                                    client_compress);
    2161            0 :                 }
    2162              : 
    2163              :                 /*
    2164              :                  * Now receive backup manifest, if appropriate.
    2165              :                  *
    2166              :                  * If we're writing a tarfile to stdout, ReceiveTarFile will have
    2167              :                  * already processed the backup manifest and included it in the output
    2168              :                  * tarfile.  Such a configuration doesn't allow for writing multiple
    2169              :                  * files.
    2170              :                  *
    2171              :                  * If we're talking to an older server, it won't send a backup
    2172              :                  * manifest, so don't try to receive one.
    2173              :                  */
    2174            0 :                 if (!writing_to_stdout && manifest)
    2175            0 :                         ReceiveBackupManifest(conn);
    2176              :         }
    2177              : 
    2178            0 :         if (showprogress)
    2179              :         {
    2180            0 :                 progress_update_filename(NULL);
    2181            0 :                 progress_report(PQntuples(res), true, true);
    2182            0 :         }
    2183              : 
    2184            0 :         PQclear(res);
    2185              : 
    2186              :         /*
    2187              :          * Get the stop position
    2188              :          */
    2189            0 :         res = PQgetResult(conn);
    2190            0 :         if (PQresultStatus(res) != PGRES_TUPLES_OK)
    2191            0 :                 pg_fatal("backup failed: %s",
    2192              :                                  PQerrorMessage(conn));
    2193            0 :         if (PQntuples(res) != 1)
    2194            0 :                 pg_fatal("no write-ahead log end position returned from server");
    2195            0 :         strlcpy(xlogend, PQgetvalue(res, 0, 0), sizeof(xlogend));
    2196            0 :         if (verbose && includewal != NO_WAL)
    2197            0 :                 pg_log_info("write-ahead log end point: %s", xlogend);
    2198            0 :         PQclear(res);
    2199              : 
    2200            0 :         res = PQgetResult(conn);
    2201            0 :         if (PQresultStatus(res) != PGRES_COMMAND_OK)
    2202              :         {
    2203            0 :                 const char *sqlstate = PQresultErrorField(res, PG_DIAG_SQLSTATE);
    2204              : 
    2205            0 :                 if (sqlstate &&
    2206            0 :                         strcmp(sqlstate, ERRCODE_DATA_CORRUPTED) == 0)
    2207              :                 {
    2208            0 :                         pg_log_error("checksum error occurred");
    2209            0 :                         checksum_failure = true;
    2210            0 :                 }
    2211              :                 else
    2212              :                 {
    2213            0 :                         pg_log_error("final receive failed: %s",
    2214              :                                                  PQerrorMessage(conn));
    2215              :                 }
    2216            0 :                 exit(1);
    2217              :         }
    2218              : 
    2219            0 :         if (bgchild > 0)
    2220              :         {
    2221              : #ifndef WIN32
    2222            0 :                 int                     status;
    2223            0 :                 pid_t           r;
    2224              : #else
    2225              :                 DWORD           status;
    2226              : 
    2227              :                 /*
    2228              :                  * get a pointer sized version of bgchild to avoid warnings about
    2229              :                  * casting to a different size on WIN64.
    2230              :                  */
    2231              :                 intptr_t        bgchild_handle = bgchild;
    2232              :                 uint32          hi,
    2233              :                                         lo;
    2234              : #endif
    2235              : 
    2236            0 :                 if (verbose)
    2237            0 :                         pg_log_info("waiting for background process to finish streaming ...");
    2238              : 
    2239              : #ifndef WIN32
    2240            0 :                 if (write(bgpipe[1], xlogend, strlen(xlogend)) != strlen(xlogend))
    2241            0 :                         pg_fatal("could not send command to background pipe: %m");
    2242              : 
    2243              :                 /* Just wait for the background process to exit */
    2244            0 :                 r = waitpid(bgchild, &status, 0);
    2245            0 :                 if (r == (pid_t) -1)
    2246            0 :                         pg_fatal("could not wait for child process: %m");
    2247            0 :                 if (r != bgchild)
    2248            0 :                         pg_fatal("child %d died, expected %d", (int) r, (int) bgchild);
    2249            0 :                 if (status != 0)
    2250            0 :                         pg_fatal("%s", wait_result_to_str(status));
    2251              :                 /* Exited normally, we're happy! */
    2252              : #else                                                   /* WIN32 */
    2253              : 
    2254              :                 /*
    2255              :                  * On Windows, since we are in the same process, we can just store the
    2256              :                  * value directly in the variable, and then set the flag that says
    2257              :                  * it's there.
    2258              :                  */
    2259              :                 if (sscanf(xlogend, "%X/%08X", &hi, &lo) != 2)
    2260              :                         pg_fatal("could not parse write-ahead log location \"%s\"",
    2261              :                                          xlogend);
    2262              :                 xlogendptr = ((uint64) hi) << 32 | lo;
    2263              :                 InterlockedIncrement(&has_xlogendptr);
    2264              : 
    2265              :                 /* First wait for the thread to exit */
    2266              :                 if (WaitForSingleObjectEx((HANDLE) bgchild_handle, INFINITE, FALSE) !=
    2267              :                         WAIT_OBJECT_0)
    2268              :                 {
    2269              :                         _dosmaperr(GetLastError());
    2270              :                         pg_fatal("could not wait for child thread: %m");
    2271              :                 }
    2272              :                 if (GetExitCodeThread((HANDLE) bgchild_handle, &status) == 0)
    2273              :                 {
    2274              :                         _dosmaperr(GetLastError());
    2275              :                         pg_fatal("could not get child thread exit status: %m");
    2276              :                 }
    2277              :                 if (status != 0)
    2278              :                         pg_fatal("child thread exited with error %u",
    2279              :                                          (unsigned int) status);
    2280              :                 /* Exited normally, we're happy */
    2281              : #endif
    2282            0 :         }
    2283              : 
    2284              :         /* Free the configuration file contents */
    2285            0 :         destroyPQExpBuffer(recoveryconfcontents);
    2286              : 
    2287              :         /*
    2288              :          * End of copy data. Final result is already checked inside the loop.
    2289              :          */
    2290            0 :         PQclear(res);
    2291            0 :         PQfinish(conn);
    2292            0 :         conn = NULL;
    2293              : 
    2294              :         /*
    2295              :          * Make data persistent on disk once backup is completed. For tar format
    2296              :          * sync the parent directory and all its contents as each tar file was not
    2297              :          * synced after being completed.  In plain format, all the data of the
    2298              :          * base directory is synced, taking into account all the tablespaces.
    2299              :          * Errors are not considered fatal.
    2300              :          *
    2301              :          * If, however, there's a backup target, we're not writing anything
    2302              :          * locally, so in that case we skip this step.
    2303              :          */
    2304            0 :         if (do_sync && backup_target == NULL)
    2305              :         {
    2306            0 :                 if (verbose)
    2307            0 :                         pg_log_info("syncing data to disk ...");
    2308            0 :                 if (format == 't')
    2309              :                 {
    2310            0 :                         if (strcmp(basedir, "-") != 0)
    2311            0 :                                 (void) sync_dir_recurse(basedir, sync_method);
    2312            0 :                 }
    2313              :                 else
    2314              :                 {
    2315            0 :                         (void) sync_pgdata(basedir, serverVersion, sync_method, true);
    2316              :                 }
    2317            0 :         }
    2318              : 
    2319              :         /*
    2320              :          * After synchronizing data to disk, perform a durable rename of
    2321              :          * backup_manifest.tmp to backup_manifest, if we wrote such a file. This
    2322              :          * way, a failure or system crash before we reach this point will leave us
    2323              :          * without a backup_manifest file, decreasing the chances that a directory
    2324              :          * we leave behind will be mistaken for a valid backup.
    2325              :          */
    2326            0 :         if (!writing_to_stdout && manifest && backup_target == NULL)
    2327              :         {
    2328            0 :                 char            tmp_filename[MAXPGPATH];
    2329            0 :                 char            filename[MAXPGPATH];
    2330              : 
    2331            0 :                 if (verbose)
    2332            0 :                         pg_log_info("renaming backup_manifest.tmp to backup_manifest");
    2333              : 
    2334            0 :                 snprintf(tmp_filename, MAXPGPATH, "%s/backup_manifest.tmp", basedir);
    2335            0 :                 snprintf(filename, MAXPGPATH, "%s/backup_manifest", basedir);
    2336              : 
    2337            0 :                 if (do_sync)
    2338              :                 {
    2339              :                         /* durable_rename emits its own log message in case of failure */
    2340            0 :                         if (durable_rename(tmp_filename, filename) != 0)
    2341            0 :                                 exit(1);
    2342            0 :                 }
    2343              :                 else
    2344              :                 {
    2345            0 :                         if (rename(tmp_filename, filename) != 0)
    2346            0 :                                 pg_fatal("could not rename file \"%s\" to \"%s\": %m",
    2347              :                                                  tmp_filename, filename);
    2348              :                 }
    2349            0 :         }
    2350              : 
    2351            0 :         if (verbose)
    2352            0 :                 pg_log_info("base backup completed");
    2353            0 : }
    2354              : 
    2355              : 
    2356              : int
    2357            0 : main(int argc, char **argv)
    2358              : {
    2359              :         static struct option long_options[] = {
    2360              :                 {"help", no_argument, NULL, '?'},
    2361              :                 {"version", no_argument, NULL, 'V'},
    2362              :                 {"pgdata", required_argument, NULL, 'D'},
    2363              :                 {"format", required_argument, NULL, 'F'},
    2364              :                 {"incremental", required_argument, NULL, 'i'},
    2365              :                 {"checkpoint", required_argument, NULL, 'c'},
    2366              :                 {"create-slot", no_argument, NULL, 'C'},
    2367              :                 {"max-rate", required_argument, NULL, 'r'},
    2368              :                 {"write-recovery-conf", no_argument, NULL, 'R'},
    2369              :                 {"slot", required_argument, NULL, 'S'},
    2370              :                 {"target", required_argument, NULL, 't'},
    2371              :                 {"tablespace-mapping", required_argument, NULL, 'T'},
    2372              :                 {"wal-method", required_argument, NULL, 'X'},
    2373              :                 {"gzip", no_argument, NULL, 'z'},
    2374              :                 {"compress", required_argument, NULL, 'Z'},
    2375              :                 {"label", required_argument, NULL, 'l'},
    2376              :                 {"no-clean", no_argument, NULL, 'n'},
    2377              :                 {"no-sync", no_argument, NULL, 'N'},
    2378              :                 {"dbname", required_argument, NULL, 'd'},
    2379              :                 {"host", required_argument, NULL, 'h'},
    2380              :                 {"port", required_argument, NULL, 'p'},
    2381              :                 {"username", required_argument, NULL, 'U'},
    2382              :                 {"no-password", no_argument, NULL, 'w'},
    2383              :                 {"password", no_argument, NULL, 'W'},
    2384              :                 {"status-interval", required_argument, NULL, 's'},
    2385              :                 {"verbose", no_argument, NULL, 'v'},
    2386              :                 {"progress", no_argument, NULL, 'P'},
    2387              :                 {"waldir", required_argument, NULL, 1},
    2388              :                 {"no-slot", no_argument, NULL, 2},
    2389              :                 {"no-verify-checksums", no_argument, NULL, 3},
    2390              :                 {"no-estimate-size", no_argument, NULL, 4},
    2391              :                 {"no-manifest", no_argument, NULL, 5},
    2392              :                 {"manifest-force-encode", no_argument, NULL, 6},
    2393              :                 {"manifest-checksums", required_argument, NULL, 7},
    2394              :                 {"sync-method", required_argument, NULL, 8},
    2395              :                 {NULL, 0, NULL, 0}
    2396              :         };
    2397            0 :         int                     c;
    2398              : 
    2399            0 :         int                     option_index;
    2400            0 :         char       *compression_algorithm = "none";
    2401            0 :         char       *compression_detail = NULL;
    2402            0 :         char       *incremental_manifest = NULL;
    2403            0 :         CompressionLocation compressloc = COMPRESS_LOCATION_UNSPECIFIED;
    2404            0 :         pg_compress_specification client_compress;
    2405              : 
    2406            0 :         pg_logging_init(argv[0]);
    2407            0 :         progname = get_progname(argv[0]);
    2408            0 :         set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_basebackup"));
    2409              : 
    2410            0 :         if (argc > 1)
    2411              :         {
    2412            0 :                 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
    2413              :                 {
    2414            0 :                         usage();
    2415            0 :                         exit(0);
    2416              :                 }
    2417            0 :                 else if (strcmp(argv[1], "-V") == 0
    2418            0 :                                  || strcmp(argv[1], "--version") == 0)
    2419              :                 {
    2420            0 :                         puts("pg_basebackup (PostgreSQL) " PG_VERSION);
    2421            0 :                         exit(0);
    2422              :                 }
    2423            0 :         }
    2424              : 
    2425            0 :         atexit(cleanup_directories_atexit);
    2426              : 
    2427            0 :         while ((c = getopt_long(argc, argv, "c:Cd:D:F:h:i:l:nNp:Pr:Rs:S:t:T:U:vwWX:zZ:",
    2428            0 :                                                         long_options, &option_index)) != -1)
    2429              :         {
    2430            0 :                 switch (c)
    2431              :                 {
    2432              :                         case 'c':
    2433            0 :                                 if (pg_strcasecmp(optarg, "fast") == 0)
    2434            0 :                                         fastcheckpoint = true;
    2435            0 :                                 else if (pg_strcasecmp(optarg, "spread") == 0)
    2436            0 :                                         fastcheckpoint = false;
    2437              :                                 else
    2438            0 :                                         pg_fatal("invalid checkpoint argument \"%s\", must be \"fast\" or \"spread\"",
    2439              :                                                          optarg);
    2440            0 :                                 break;
    2441              :                         case 'C':
    2442            0 :                                 create_slot = true;
    2443            0 :                                 break;
    2444              :                         case 'd':
    2445            0 :                                 connection_string = pg_strdup(optarg);
    2446            0 :                                 break;
    2447              :                         case 'D':
    2448            0 :                                 basedir = pg_strdup(optarg);
    2449            0 :                                 break;
    2450              :                         case 'F':
    2451            0 :                                 if (strcmp(optarg, "p") == 0 || strcmp(optarg, "plain") == 0)
    2452            0 :                                         format = 'p';
    2453            0 :                                 else if (strcmp(optarg, "t") == 0 || strcmp(optarg, "tar") == 0)
    2454            0 :                                         format = 't';
    2455              :                                 else
    2456            0 :                                         pg_fatal("invalid output format \"%s\", must be \"plain\" or \"tar\"",
    2457              :                                                          optarg);
    2458            0 :                                 break;
    2459              :                         case 'h':
    2460            0 :                                 dbhost = pg_strdup(optarg);
    2461            0 :                                 break;
    2462              :                         case 'i':
    2463            0 :                                 incremental_manifest = pg_strdup(optarg);
    2464            0 :                                 break;
    2465              :                         case 'l':
    2466            0 :                                 label = pg_strdup(optarg);
    2467            0 :                                 break;
    2468              :                         case 'n':
    2469            0 :                                 noclean = true;
    2470            0 :                                 break;
    2471              :                         case 'N':
    2472            0 :                                 do_sync = false;
    2473            0 :                                 break;
    2474              :                         case 'p':
    2475            0 :                                 dbport = pg_strdup(optarg);
    2476            0 :                                 break;
    2477              :                         case 'P':
    2478            0 :                                 showprogress = true;
    2479            0 :                                 break;
    2480              :                         case 'r':
    2481            0 :                                 maxrate = parse_max_rate(optarg);
    2482            0 :                                 break;
    2483              :                         case 'R':
    2484            0 :                                 writerecoveryconf = true;
    2485            0 :                                 break;
    2486              :                         case 's':
    2487            0 :                                 if (!option_parse_int(optarg, "-s/--status-interval", 0,
    2488              :                                                                           INT_MAX / 1000,
    2489              :                                                                           &standby_message_timeout))
    2490            0 :                                         exit(1);
    2491            0 :                                 standby_message_timeout *= 1000;
    2492            0 :                                 break;
    2493              :                         case 'S':
    2494              : 
    2495              :                                 /*
    2496              :                                  * When specifying replication slot name, use a permanent
    2497              :                                  * slot.
    2498              :                                  */
    2499            0 :                                 replication_slot = pg_strdup(optarg);
    2500            0 :                                 temp_replication_slot = false;
    2501            0 :                                 break;
    2502              :                         case 't':
    2503            0 :                                 backup_target = pg_strdup(optarg);
    2504            0 :                                 break;
    2505              :                         case 'T':
    2506            0 :                                 tablespace_list_append(optarg);
    2507            0 :                                 break;
    2508              :                         case 'U':
    2509            0 :                                 dbuser = pg_strdup(optarg);
    2510            0 :                                 break;
    2511              :                         case 'v':
    2512            0 :                                 verbose++;
    2513            0 :                                 break;
    2514              :                         case 'w':
    2515            0 :                                 dbgetpassword = -1;
    2516            0 :                                 break;
    2517              :                         case 'W':
    2518            0 :                                 dbgetpassword = 1;
    2519            0 :                                 break;
    2520              :                         case 'X':
    2521            0 :                                 if (strcmp(optarg, "n") == 0 ||
    2522            0 :                                         strcmp(optarg, "none") == 0)
    2523              :                                 {
    2524            0 :                                         includewal = NO_WAL;
    2525            0 :                                 }
    2526            0 :                                 else if (strcmp(optarg, "f") == 0 ||
    2527            0 :                                                  strcmp(optarg, "fetch") == 0)
    2528              :                                 {
    2529            0 :                                         includewal = FETCH_WAL;
    2530            0 :                                 }
    2531            0 :                                 else if (strcmp(optarg, "s") == 0 ||
    2532            0 :                                                  strcmp(optarg, "stream") == 0)
    2533              :                                 {
    2534            0 :                                         includewal = STREAM_WAL;
    2535            0 :                                 }
    2536              :                                 else
    2537            0 :                                         pg_fatal("invalid wal-method option \"%s\", must be \"fetch\", \"stream\", or \"none\"",
    2538              :                                                          optarg);
    2539            0 :                                 break;
    2540              :                         case 'z':
    2541            0 :                                 compression_algorithm = "gzip";
    2542            0 :                                 compression_detail = NULL;
    2543            0 :                                 compressloc = COMPRESS_LOCATION_UNSPECIFIED;
    2544            0 :                                 break;
    2545              :                         case 'Z':
    2546            0 :                                 backup_parse_compress_options(optarg, &compression_algorithm,
    2547              :                                                                                           &compression_detail, &compressloc);
    2548            0 :                                 break;
    2549              :                         case 1:
    2550            0 :                                 xlog_dir = pg_strdup(optarg);
    2551            0 :                                 break;
    2552              :                         case 2:
    2553            0 :                                 no_slot = true;
    2554            0 :                                 break;
    2555              :                         case 3:
    2556            0 :                                 verify_checksums = false;
    2557            0 :                                 break;
    2558              :                         case 4:
    2559            0 :                                 estimatesize = false;
    2560            0 :                                 break;
    2561              :                         case 5:
    2562            0 :                                 manifest = false;
    2563            0 :                                 break;
    2564              :                         case 6:
    2565            0 :                                 manifest_force_encode = true;
    2566            0 :                                 break;
    2567              :                         case 7:
    2568            0 :                                 manifest_checksums = pg_strdup(optarg);
    2569            0 :                                 break;
    2570              :                         case 8:
    2571            0 :                                 if (!parse_sync_method(optarg, &sync_method))
    2572            0 :                                         exit(1);
    2573            0 :                                 break;
    2574              :                         default:
    2575              :                                 /* getopt_long already emitted a complaint */
    2576            0 :                                 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
    2577            0 :                                 exit(1);
    2578              :                 }
    2579              :         }
    2580              : 
    2581              :         /*
    2582              :          * Any non-option arguments?
    2583              :          */
    2584            0 :         if (optind < argc)
    2585              :         {
    2586            0 :                 pg_log_error("too many command-line arguments (first is \"%s\")",
    2587              :                                          argv[optind]);
    2588            0 :                 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
    2589            0 :                 exit(1);
    2590              :         }
    2591              : 
    2592              :         /*
    2593              :          * Setting the backup target to 'client' is equivalent to leaving out the
    2594              :          * option. This logic allows us to assume elsewhere that the backup is
    2595              :          * being stored locally if and only if backup_target == NULL.
    2596              :          */
    2597            0 :         if (backup_target != NULL && strcmp(backup_target, "client") == 0)
    2598              :         {
    2599            0 :                 pg_free(backup_target);
    2600            0 :                 backup_target = NULL;
    2601            0 :         }
    2602              : 
    2603              :         /*
    2604              :          * Can't use --format with --target. Without --target, default format is
    2605              :          * tar.
    2606              :          */
    2607            0 :         if (backup_target != NULL && format != '\0')
    2608              :         {
    2609            0 :                 pg_log_error("cannot specify both format and backup target");
    2610            0 :                 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
    2611            0 :                 exit(1);
    2612              :         }
    2613            0 :         if (format == '\0')
    2614            0 :                 format = 'p';
    2615              : 
    2616              :         /*
    2617              :          * Either directory or backup target should be specified, but not both
    2618              :          */
    2619            0 :         if (basedir == NULL && backup_target == NULL)
    2620              :         {
    2621            0 :                 pg_log_error("must specify output directory or backup target");
    2622            0 :                 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
    2623            0 :                 exit(1);
    2624              :         }
    2625            0 :         if (basedir != NULL && backup_target != NULL)
    2626              :         {
    2627            0 :                 pg_log_error("cannot specify both output directory and backup target");
    2628            0 :                 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
    2629            0 :                 exit(1);
    2630              :         }
    2631              : 
    2632              :         /*
    2633              :          * If the user has not specified where to perform backup compression,
    2634              :          * default to the client, unless the user specified --target, in which
    2635              :          * case the server is the only choice.
    2636              :          */
    2637            0 :         if (compressloc == COMPRESS_LOCATION_UNSPECIFIED)
    2638              :         {
    2639            0 :                 if (backup_target == NULL)
    2640            0 :                         compressloc = COMPRESS_LOCATION_CLIENT;
    2641              :                 else
    2642            0 :                         compressloc = COMPRESS_LOCATION_SERVER;
    2643            0 :         }
    2644              : 
    2645              :         /*
    2646              :          * If any compression that we're doing is happening on the client side, we
    2647              :          * must try to parse the compression algorithm and detail, but if it's all
    2648              :          * on the server side, then we're just going to pass through whatever was
    2649              :          * requested and let the server decide what to do.
    2650              :          */
    2651            0 :         if (compressloc == COMPRESS_LOCATION_CLIENT)
    2652              :         {
    2653            0 :                 pg_compress_algorithm alg;
    2654            0 :                 char       *error_detail;
    2655              : 
    2656            0 :                 if (!parse_compress_algorithm(compression_algorithm, &alg))
    2657            0 :                         pg_fatal("unrecognized compression algorithm: \"%s\"",
    2658              :                                          compression_algorithm);
    2659              : 
    2660            0 :                 parse_compress_specification(alg, compression_detail, &client_compress);
    2661            0 :                 error_detail = validate_compress_specification(&client_compress);
    2662            0 :                 if (error_detail != NULL)
    2663            0 :                         pg_fatal("invalid compression specification: %s",
    2664              :                                          error_detail);
    2665            0 :         }
    2666              :         else
    2667              :         {
    2668            0 :                 Assert(compressloc == COMPRESS_LOCATION_SERVER);
    2669            0 :                 client_compress.algorithm = PG_COMPRESSION_NONE;
    2670            0 :                 client_compress.options = 0;
    2671              :         }
    2672              : 
    2673              :         /*
    2674              :          * Can't perform client-side compression if the backup is not being sent
    2675              :          * to the client.
    2676              :          */
    2677            0 :         if (backup_target != NULL && compressloc == COMPRESS_LOCATION_CLIENT)
    2678              :         {
    2679            0 :                 pg_log_error("client-side compression is not possible when a backup target is specified");
    2680            0 :                 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
    2681            0 :                 exit(1);
    2682              :         }
    2683              : 
    2684              :         /*
    2685              :          * Client-side compression doesn't make sense unless tar format is in use.
    2686              :          */
    2687            0 :         if (format == 'p' && compressloc == COMPRESS_LOCATION_CLIENT &&
    2688            0 :                 client_compress.algorithm != PG_COMPRESSION_NONE)
    2689              :         {
    2690            0 :                 pg_log_error("only tar mode backups can be compressed");
    2691            0 :                 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
    2692            0 :                 exit(1);
    2693              :         }
    2694              : 
    2695              :         /*
    2696              :          * Sanity checks for WAL method.
    2697              :          */
    2698            0 :         if (backup_target != NULL && includewal == STREAM_WAL)
    2699              :         {
    2700            0 :                 pg_log_error("WAL cannot be streamed when a backup target is specified");
    2701            0 :                 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
    2702            0 :                 exit(1);
    2703              :         }
    2704            0 :         if (format == 't' && includewal == STREAM_WAL && strcmp(basedir, "-") == 0)
    2705              :         {
    2706            0 :                 pg_log_error("cannot stream write-ahead logs in tar mode to stdout");
    2707            0 :                 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
    2708            0 :                 exit(1);
    2709              :         }
    2710              : 
    2711            0 :         if (replication_slot && includewal != STREAM_WAL)
    2712              :         {
    2713            0 :                 pg_log_error("replication slots can only be used with WAL streaming");
    2714            0 :                 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
    2715            0 :                 exit(1);
    2716              :         }
    2717              : 
    2718              :         /*
    2719              :          * Sanity checks for replication slot options.
    2720              :          */
    2721            0 :         if (no_slot)
    2722              :         {
    2723            0 :                 if (replication_slot)
    2724              :                 {
    2725            0 :                         pg_log_error("--no-slot cannot be used with slot name");
    2726            0 :                         pg_log_error_hint("Try \"%s --help\" for more information.", progname);
    2727            0 :                         exit(1);
    2728              :                 }
    2729            0 :                 temp_replication_slot = false;
    2730            0 :         }
    2731              : 
    2732            0 :         if (create_slot)
    2733              :         {
    2734            0 :                 if (!replication_slot)
    2735              :                 {
    2736            0 :                         pg_log_error("%s needs a slot to be specified using --slot",
    2737              :                                                  "--create-slot");
    2738            0 :                         pg_log_error_hint("Try \"%s --help\" for more information.", progname);
    2739            0 :                         exit(1);
    2740              :                 }
    2741              : 
    2742            0 :                 if (no_slot)
    2743              :                 {
    2744            0 :                         pg_log_error("%s and %s are incompatible options",
    2745              :                                                  "--create-slot", "--no-slot");
    2746            0 :                         pg_log_error_hint("Try \"%s --help\" for more information.", progname);
    2747            0 :                         exit(1);
    2748              :                 }
    2749            0 :         }
    2750              : 
    2751              :         /*
    2752              :          * Sanity checks on WAL directory.
    2753              :          */
    2754            0 :         if (xlog_dir)
    2755              :         {
    2756            0 :                 if (backup_target != NULL)
    2757              :                 {
    2758            0 :                         pg_log_error("WAL directory location cannot be specified along with a backup target");
    2759            0 :                         pg_log_error_hint("Try \"%s --help\" for more information.", progname);
    2760            0 :                         exit(1);
    2761              :                 }
    2762            0 :                 if (format != 'p')
    2763              :                 {
    2764            0 :                         pg_log_error("WAL directory location can only be specified in plain mode");
    2765            0 :                         pg_log_error_hint("Try \"%s --help\" for more information.", progname);
    2766            0 :                         exit(1);
    2767              :                 }
    2768              : 
    2769              :                 /* clean up xlog directory name, check it's absolute */
    2770            0 :                 canonicalize_path(xlog_dir);
    2771            0 :                 if (!is_absolute_path(xlog_dir))
    2772              :                 {
    2773            0 :                         pg_log_error("WAL directory location must be an absolute path");
    2774            0 :                         pg_log_error_hint("Try \"%s --help\" for more information.", progname);
    2775            0 :                         exit(1);
    2776              :                 }
    2777            0 :         }
    2778              : 
    2779              :         /*
    2780              :          * Sanity checks for progress reporting options.
    2781              :          */
    2782            0 :         if (showprogress && !estimatesize)
    2783              :         {
    2784            0 :                 pg_log_error("%s and %s are incompatible options",
    2785              :                                          "--progress", "--no-estimate-size");
    2786            0 :                 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
    2787            0 :                 exit(1);
    2788              :         }
    2789              : 
    2790              :         /*
    2791              :          * Sanity checks for backup manifest options.
    2792              :          */
    2793            0 :         if (!manifest && manifest_checksums != NULL)
    2794              :         {
    2795            0 :                 pg_log_error("%s and %s are incompatible options",
    2796              :                                          "--no-manifest", "--manifest-checksums");
    2797            0 :                 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
    2798            0 :                 exit(1);
    2799              :         }
    2800              : 
    2801            0 :         if (!manifest && manifest_force_encode)
    2802              :         {
    2803            0 :                 pg_log_error("%s and %s are incompatible options",
    2804              :                                          "--no-manifest", "--manifest-force-encode");
    2805            0 :                 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
    2806            0 :                 exit(1);
    2807              :         }
    2808              : 
    2809              :         /* connection in replication mode to server */
    2810            0 :         conn = GetConnection();
    2811            0 :         if (!conn)
    2812              :         {
    2813              :                 /* Error message already written in GetConnection() */
    2814            0 :                 exit(1);
    2815              :         }
    2816            0 :         atexit(disconnect_atexit);
    2817              : 
    2818              : #ifndef WIN32
    2819              : 
    2820              :         /*
    2821              :          * Trap SIGCHLD to be able to handle the WAL stream process exiting. There
    2822              :          * is no SIGCHLD on Windows, there we rely on the background thread
    2823              :          * setting the signal variable on unexpected but graceful exit. If the WAL
    2824              :          * stream thread crashes on Windows it will bring down the entire process
    2825              :          * as it's a thread, so there is nothing to catch should that happen. A
    2826              :          * crash on UNIX will be caught by the signal handler.
    2827              :          */
    2828            0 :         pqsignal(SIGCHLD, sigchld_handler);
    2829              : #endif
    2830              : 
    2831              :         /*
    2832              :          * Set umask so that directories/files are created with the same
    2833              :          * permissions as directories/files in the source data directory.
    2834              :          *
    2835              :          * pg_mode_mask is set to owner-only by default and then updated in
    2836              :          * GetConnection() where we get the mode from the server-side with
    2837              :          * RetrieveDataDirCreatePerm() and then call SetDataDirectoryCreatePerm().
    2838              :          */
    2839            0 :         umask(pg_mode_mask);
    2840              : 
    2841              :         /* Backup manifests are supported in 13 and newer versions */
    2842            0 :         if (PQserverVersion(conn) < MINIMUM_VERSION_FOR_MANIFESTS)
    2843            0 :                 manifest = false;
    2844              : 
    2845              :         /*
    2846              :          * If an output directory was specified, verify that it exists, or create
    2847              :          * it. Note that for a tar backup, an output directory of "-" means we are
    2848              :          * writing to stdout, so do nothing in that case.
    2849              :          */
    2850            0 :         if (basedir != NULL && (format == 'p' || strcmp(basedir, "-") != 0))
    2851            0 :                 verify_dir_is_empty_or_create(basedir, &made_new_pgdata, &found_existing_pgdata);
    2852              : 
    2853              :         /* determine remote server's xlog segment size */
    2854            0 :         if (!RetrieveWalSegSize(conn))
    2855            0 :                 exit(1);
    2856              : 
    2857              :         /* Create pg_wal symlink, if required */
    2858            0 :         if (xlog_dir)
    2859              :         {
    2860            0 :                 char       *linkloc;
    2861              : 
    2862            0 :                 verify_dir_is_empty_or_create(xlog_dir, &made_new_xlogdir, &found_existing_xlogdir);
    2863              : 
    2864              :                 /*
    2865              :                  * Form name of the place where the symlink must go. pg_xlog has been
    2866              :                  * renamed to pg_wal in post-10 clusters.
    2867              :                  */
    2868            0 :                 linkloc = psprintf("%s/%s", basedir,
    2869            0 :                                                    PQserverVersion(conn) < MINIMUM_VERSION_FOR_PG_WAL ?
    2870              :                                                    "pg_xlog" : "pg_wal");
    2871              : 
    2872            0 :                 if (symlink(xlog_dir, linkloc) != 0)
    2873            0 :                         pg_fatal("could not create symbolic link \"%s\": %m", linkloc);
    2874            0 :                 free(linkloc);
    2875            0 :         }
    2876              : 
    2877            0 :         BaseBackup(compression_algorithm, compression_detail, compressloc,
    2878            0 :                            &client_compress, incremental_manifest);
    2879              : 
    2880            0 :         success = true;
    2881            0 :         return 0;
    2882            0 : }
        

Generated by: LCOV version 2.3.2-1