LCOV - code coverage report
Current view: top level - src/bin/pg_basebackup - pg_basebackup.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13beta1 Lines: 547 932 58.7 %
Date: 2020-06-01 08:06:25 Functions: 21 25 84.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-2020, 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 <sys/stat.h>
      19             : #include <sys/wait.h>
      20             : #include <signal.h>
      21             : #include <time.h>
      22             : #ifdef HAVE_SYS_SELECT_H
      23             : #include <sys/select.h>
      24             : #endif
      25             : #ifdef HAVE_LIBZ
      26             : #include <zlib.h>
      27             : #endif
      28             : 
      29             : #include "access/xlog_internal.h"
      30             : #include "common/file_perm.h"
      31             : #include "common/file_utils.h"
      32             : #include "common/logging.h"
      33             : #include "common/string.h"
      34             : #include "fe_utils/recovery_gen.h"
      35             : #include "fe_utils/string_utils.h"
      36             : #include "getopt_long.h"
      37             : #include "libpq-fe.h"
      38             : #include "pgtar.h"
      39             : #include "pgtime.h"
      40             : #include "pqexpbuffer.h"
      41             : #include "receivelog.h"
      42             : #include "replication/basebackup.h"
      43             : #include "streamutil.h"
      44             : 
      45             : #define ERRCODE_DATA_CORRUPTED  "XX001"
      46             : 
      47             : typedef struct TablespaceListCell
      48             : {
      49             :     struct TablespaceListCell *next;
      50             :     char        old_dir[MAXPGPATH];
      51             :     char        new_dir[MAXPGPATH];
      52             : } TablespaceListCell;
      53             : 
      54             : typedef struct TablespaceList
      55             : {
      56             :     TablespaceListCell *head;
      57             :     TablespaceListCell *tail;
      58             : } TablespaceList;
      59             : 
      60             : typedef struct WriteTarState
      61             : {
      62             :     int         tablespacenum;
      63             :     char        filename[MAXPGPATH];
      64             :     FILE       *tarfile;
      65             :     char        tarhdr[512];
      66             :     bool        basetablespace;
      67             :     bool        in_tarhdr;
      68             :     bool        skip_file;
      69             :     bool        is_recovery_guc_supported;
      70             :     bool        is_postgresql_auto_conf;
      71             :     bool        found_postgresql_auto_conf;
      72             :     int         file_padding_len;
      73             :     size_t      tarhdrsz;
      74             :     pgoff_t     filesz;
      75             : #ifdef HAVE_LIBZ
      76             :     gzFile      ztarfile;
      77             : #endif
      78             : } WriteTarState;
      79             : 
      80             : typedef struct UnpackTarState
      81             : {
      82             :     int         tablespacenum;
      83             :     char        current_path[MAXPGPATH];
      84             :     char        filename[MAXPGPATH];
      85             :     const char *mapped_tblspc_path;
      86             :     pgoff_t     current_len_left;
      87             :     int         current_padding;
      88             :     FILE       *file;
      89             : } UnpackTarState;
      90             : 
      91             : typedef struct WriteManifestState
      92             : {
      93             :     char        filename[MAXPGPATH];
      94             :     FILE       *file;
      95             : } WriteManifestState;
      96             : 
      97             : typedef void (*WriteDataCallback) (size_t nbytes, char *buf,
      98             :                                    void *callback_data);
      99             : 
     100             : /*
     101             :  * pg_xlog has been renamed to pg_wal in version 10.  This version number
     102             :  * should be compared with PQserverVersion().
     103             :  */
     104             : #define MINIMUM_VERSION_FOR_PG_WAL  100000
     105             : 
     106             : /*
     107             :  * Temporary replication slots are supported from version 10.
     108             :  */
     109             : #define MINIMUM_VERSION_FOR_TEMP_SLOTS 100000
     110             : 
     111             : /*
     112             :  * Backup manifests are supported from version 13.
     113             :  */
     114             : #define MINIMUM_VERSION_FOR_MANIFESTS   130000
     115             : 
     116             : /*
     117             :  * Different ways to include WAL
     118             :  */
     119             : typedef enum
     120             : {
     121             :     NO_WAL,
     122             :     FETCH_WAL,
     123             :     STREAM_WAL
     124             : } IncludeWal;
     125             : 
     126             : /* Global options */
     127             : static char *basedir = NULL;
     128             : static TablespaceList tablespace_dirs = {NULL, NULL};
     129             : static char *xlog_dir = NULL;
     130             : static char format = 'p';       /* p(lain)/t(ar) */
     131             : static char *label = "pg_basebackup base backup";
     132             : static bool noclean = false;
     133             : static bool checksum_failure = false;
     134             : static bool showprogress = false;
     135             : static bool estimatesize = true;
     136             : static int  verbose = 0;
     137             : static int  compresslevel = 0;
     138             : static IncludeWal includewal = STREAM_WAL;
     139             : static bool fastcheckpoint = false;
     140             : static bool writerecoveryconf = false;
     141             : static bool do_sync = true;
     142             : static int  standby_message_timeout = 10 * 1000;    /* 10 sec = default */
     143             : static pg_time_t last_progress_report = 0;
     144             : static int32 maxrate = 0;       /* no limit by default */
     145             : static char *replication_slot = NULL;
     146             : static bool temp_replication_slot = true;
     147             : static bool create_slot = false;
     148             : static bool no_slot = false;
     149             : static bool verify_checksums = true;
     150             : static bool manifest = true;
     151             : static bool manifest_force_encode = false;
     152             : static char *manifest_checksums = NULL;
     153             : 
     154             : static bool success = false;
     155             : static bool made_new_pgdata = false;
     156             : static bool found_existing_pgdata = false;
     157             : static bool made_new_xlogdir = false;
     158             : static bool found_existing_xlogdir = false;
     159             : static bool made_tablespace_dirs = false;
     160             : static bool found_tablespace_dirs = false;
     161             : 
     162             : /* Progress counters */
     163             : static uint64 totalsize_kb;
     164             : static uint64 totaldone;
     165             : static int  tablespacecount;
     166             : 
     167             : /* Pipe to communicate with background wal receiver process */
     168             : #ifndef WIN32
     169             : static int  bgpipe[2] = {-1, -1};
     170             : #endif
     171             : 
     172             : /* Handle to child process */
     173             : static pid_t bgchild = -1;
     174             : static bool in_log_streamer = false;
     175             : 
     176             : /* End position for xlog streaming, empty string if unknown yet */
     177             : static XLogRecPtr xlogendptr;
     178             : 
     179             : #ifndef WIN32
     180             : static int  has_xlogendptr = 0;
     181             : #else
     182             : static volatile LONG has_xlogendptr = 0;
     183             : #endif
     184             : 
     185             : /* Contents of configuration file to be generated */
     186             : static PQExpBuffer recoveryconfcontents = NULL;
     187             : 
     188             : /* Function headers */
     189             : static void usage(void);
     190             : static void verify_dir_is_empty_or_create(char *dirname, bool *created, bool *found);
     191             : static void progress_report(int tablespacenum, const char *filename, bool force);
     192             : 
     193             : static void ReceiveTarFile(PGconn *conn, PGresult *res, int rownum);
     194             : static void ReceiveTarCopyChunk(size_t r, char *copybuf, void *callback_data);
     195             : static void ReceiveAndUnpackTarFile(PGconn *conn, PGresult *res, int rownum);
     196             : static void ReceiveTarAndUnpackCopyChunk(size_t r, char *copybuf,
     197             :                                          void *callback_data);
     198             : static void ReceiveBackupManifest(PGconn *conn);
     199             : static void ReceiveBackupManifestChunk(size_t r, char *copybuf,
     200             :                                        void *callback_data);
     201             : static void ReceiveBackupManifestInMemory(PGconn *conn, PQExpBuffer buf);
     202             : static void ReceiveBackupManifestInMemoryChunk(size_t r, char *copybuf,
     203             :                                                void *callback_data);
     204             : static void BaseBackup(void);
     205             : 
     206             : static bool reached_end_position(XLogRecPtr segendpos, uint32 timeline,
     207             :                                  bool segment_finished);
     208             : 
     209             : static const char *get_tablespace_mapping(const char *dir);
     210             : static void tablespace_list_append(const char *arg);
     211             : 
     212             : 
     213             : static void
     214         300 : cleanup_directories_atexit(void)
     215             : {
     216         300 :     if (success || in_log_streamer)
     217         258 :         return;
     218             : 
     219          42 :     if (!noclean && !checksum_failure)
     220             :     {
     221          34 :         if (made_new_pgdata)
     222             :         {
     223          10 :             pg_log_info("removing data directory \"%s\"", basedir);
     224          10 :             if (!rmtree(basedir, true))
     225           0 :                 pg_log_error("failed to remove data directory");
     226             :         }
     227          24 :         else if (found_existing_pgdata)
     228             :         {
     229           0 :             pg_log_info("removing contents of data directory \"%s\"", basedir);
     230           0 :             if (!rmtree(basedir, false))
     231           0 :                 pg_log_error("failed to remove contents of data directory");
     232             :         }
     233             : 
     234          68 :         if (made_new_xlogdir)
     235             :         {
     236           0 :             pg_log_info("removing WAL directory \"%s\"", xlog_dir);
     237           0 :             if (!rmtree(xlog_dir, true))
     238           0 :                 pg_log_error("failed to remove WAL directory");
     239             :         }
     240          34 :         else if (found_existing_xlogdir)
     241             :         {
     242           0 :             pg_log_info("removing contents of WAL directory \"%s\"", xlog_dir);
     243           0 :             if (!rmtree(xlog_dir, false))
     244           0 :                 pg_log_error("failed to remove contents of WAL directory");
     245             :         }
     246             :     }
     247             :     else
     248             :     {
     249           8 :         if ((made_new_pgdata || found_existing_pgdata) && !checksum_failure)
     250           0 :             pg_log_info("data directory \"%s\" not removed at user's request", basedir);
     251             : 
     252           8 :         if (made_new_xlogdir || found_existing_xlogdir)
     253           0 :             pg_log_info("WAL directory \"%s\" not removed at user's request", xlog_dir);
     254             :     }
     255             : 
     256          42 :     if ((made_tablespace_dirs || found_tablespace_dirs) && !checksum_failure)
     257           0 :         pg_log_info("changes to tablespace directories will not be undone");
     258             : }
     259             : 
     260             : static void
     261         274 : disconnect_atexit(void)
     262             : {
     263         274 :     if (conn != NULL)
     264         144 :         PQfinish(conn);
     265         274 : }
     266             : 
     267             : #ifndef WIN32
     268             : /*
     269             :  * On windows, our background thread dies along with the process. But on
     270             :  * Unix, if we have started a subprocess, we want to kill it off so it
     271             :  * doesn't remain running trying to stream data.
     272             :  */
     273             : static void
     274         136 : kill_bgchild_atexit(void)
     275             : {
     276         136 :     if (bgchild > 0)
     277         136 :         kill(bgchild, SIGTERM);
     278         136 : }
     279             : #endif
     280             : 
     281             : /*
     282             :  * Split argument into old_dir and new_dir and append to tablespace mapping
     283             :  * list.
     284             :  */
     285             : static void
     286          38 : tablespace_list_append(const char *arg)
     287             : {
     288          38 :     TablespaceListCell *cell = (TablespaceListCell *) pg_malloc0(sizeof(TablespaceListCell));
     289             :     char       *dst;
     290             :     char       *dst_ptr;
     291             :     const char *arg_ptr;
     292             : 
     293          38 :     dst_ptr = dst = cell->old_dir;
     294        1284 :     for (arg_ptr = arg; *arg_ptr; arg_ptr++)
     295             :     {
     296        1248 :         if (dst_ptr - dst >= MAXPGPATH)
     297             :         {
     298           0 :             pg_log_error("directory name too long");
     299           0 :             exit(1);
     300             :         }
     301             : 
     302        1248 :         if (*arg_ptr == '\\' && *(arg_ptr + 1) == '=')
     303             :             ;                   /* skip backslash escaping = */
     304        1244 :         else if (*arg_ptr == '=' && (arg_ptr == arg || *(arg_ptr - 1) != '\\'))
     305             :         {
     306          38 :             if (*cell->new_dir)
     307             :             {
     308           2 :                 pg_log_error("multiple \"=\" signs in tablespace mapping");
     309           2 :                 exit(1);
     310             :             }
     311             :             else
     312          36 :                 dst = dst_ptr = cell->new_dir;
     313             :         }
     314             :         else
     315        1206 :             *dst_ptr++ = *arg_ptr;
     316             :     }
     317             : 
     318          36 :     if (!*cell->old_dir || !*cell->new_dir)
     319             :     {
     320           6 :         pg_log_error("invalid tablespace mapping format \"%s\", must be \"OLDDIR=NEWDIR\"", arg);
     321           6 :         exit(1);
     322             :     }
     323             : 
     324             :     /*
     325             :      * This check isn't absolutely necessary.  But all tablespaces are created
     326             :      * with absolute directories, so specifying a non-absolute path here would
     327             :      * just never match, possibly confusing users.  It's also good to be
     328             :      * consistent with the new_dir check.
     329             :      */
     330          30 :     if (!is_absolute_path(cell->old_dir))
     331             :     {
     332           2 :         pg_log_error("old directory is not an absolute path in tablespace mapping: %s",
     333             :                      cell->old_dir);
     334           2 :         exit(1);
     335             :     }
     336             : 
     337          28 :     if (!is_absolute_path(cell->new_dir))
     338             :     {
     339           2 :         pg_log_error("new directory is not an absolute path in tablespace mapping: %s",
     340             :                      cell->new_dir);
     341           2 :         exit(1);
     342             :     }
     343             : 
     344             :     /*
     345             :      * Comparisons done with these values should involve similarly
     346             :      * canonicalized path values.  This is particularly sensitive on Windows
     347             :      * where path values may not necessarily use Unix slashes.
     348             :      */
     349          26 :     canonicalize_path(cell->old_dir);
     350          26 :     canonicalize_path(cell->new_dir);
     351             : 
     352          26 :     if (tablespace_dirs.tail)
     353           0 :         tablespace_dirs.tail->next = cell;
     354             :     else
     355          26 :         tablespace_dirs.head = cell;
     356          26 :     tablespace_dirs.tail = cell;
     357          26 : }
     358             : 
     359             : 
     360             : #ifdef HAVE_LIBZ
     361             : static const char *
     362           0 : get_gz_error(gzFile gzf)
     363             : {
     364             :     int         errnum;
     365             :     const char *errmsg;
     366             : 
     367           0 :     errmsg = gzerror(gzf, &errnum);
     368           0 :     if (errnum == Z_ERRNO)
     369           0 :         return strerror(errno);
     370             :     else
     371           0 :         return errmsg;
     372             : }
     373             : #endif
     374             : 
     375             : static void
     376           2 : usage(void)
     377             : {
     378           2 :     printf(_("%s takes a base backup of a running PostgreSQL server.\n\n"),
     379             :            progname);
     380           2 :     printf(_("Usage:\n"));
     381           2 :     printf(_("  %s [OPTION]...\n"), progname);
     382           2 :     printf(_("\nOptions controlling the output:\n"));
     383           2 :     printf(_("  -D, --pgdata=DIRECTORY receive base backup into directory\n"));
     384           2 :     printf(_("  -F, --format=p|t       output format (plain (default), tar)\n"));
     385           2 :     printf(_("  -r, --max-rate=RATE    maximum transfer rate to transfer data directory\n"
     386             :              "                         (in kB/s, or use suffix \"k\" or \"M\")\n"));
     387           2 :     printf(_("  -R, --write-recovery-conf\n"
     388             :              "                         write configuration for replication\n"));
     389           2 :     printf(_("  -T, --tablespace-mapping=OLDDIR=NEWDIR\n"
     390             :              "                         relocate tablespace in OLDDIR to NEWDIR\n"));
     391           2 :     printf(_("      --waldir=WALDIR    location for the write-ahead log directory\n"));
     392           2 :     printf(_("  -X, --wal-method=none|fetch|stream\n"
     393             :              "                         include required WAL files with specified method\n"));
     394           2 :     printf(_("  -z, --gzip             compress tar output\n"));
     395           2 :     printf(_("  -Z, --compress=0-9     compress tar output with given compression level\n"));
     396           2 :     printf(_("\nGeneral options:\n"));
     397           2 :     printf(_("  -c, --checkpoint=fast|spread\n"
     398             :              "                         set fast or spread checkpointing\n"));
     399           2 :     printf(_("  -C, --create-slot      create replication slot\n"));
     400           2 :     printf(_("  -l, --label=LABEL      set backup label\n"));
     401           2 :     printf(_("  -n, --no-clean         do not clean up after errors\n"));
     402           2 :     printf(_("  -N, --no-sync          do not wait for changes to be written safely to disk\n"));
     403           2 :     printf(_("  -P, --progress         show progress information\n"));
     404           2 :     printf(_("  -S, --slot=SLOTNAME    replication slot to use\n"));
     405           2 :     printf(_("  -v, --verbose          output verbose messages\n"));
     406           2 :     printf(_("  -V, --version          output version information, then exit\n"));
     407           2 :     printf(_("      --manifest-checksums=SHA{224,256,384,512}|CRC32C|NONE\n"
     408             :              "                         use algorithm for manifest checksums\n"));
     409           2 :     printf(_("      --manifest-force-encode\n"
     410             :              "                         hex encode all file names in manifest\n"));
     411           2 :     printf(_("      --no-estimate-size do not estimate backup size in server side\n"));
     412           2 :     printf(_("      --no-manifest      suppress generation of backup manifest\n"));
     413           2 :     printf(_("      --no-slot          prevent creation of temporary replication slot\n"));
     414           2 :     printf(_("      --no-verify-checksums\n"
     415             :              "                         do not verify checksums\n"));
     416           2 :     printf(_("  -?, --help             show this help, then exit\n"));
     417           2 :     printf(_("\nConnection options:\n"));
     418           2 :     printf(_("  -d, --dbname=CONNSTR   connection string\n"));
     419           2 :     printf(_("  -h, --host=HOSTNAME    database server host or socket directory\n"));
     420           2 :     printf(_("  -p, --port=PORT        database server port number\n"));
     421           2 :     printf(_("  -s, --status-interval=INTERVAL\n"
     422             :              "                         time between status packets sent to server (in seconds)\n"));
     423           2 :     printf(_("  -U, --username=NAME    connect as specified database user\n"));
     424           2 :     printf(_("  -w, --no-password      never prompt for password\n"));
     425           2 :     printf(_("  -W, --password         force password prompt (should happen automatically)\n"));
     426           2 :     printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
     427           2 :     printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
     428           2 : }
     429             : 
     430             : 
     431             : /*
     432             :  * Called in the background process every time data is received.
     433             :  * On Unix, we check to see if there is any data on our pipe
     434             :  * (which would mean we have a stop position), and if it is, check if
     435             :  * it is time to stop.
     436             :  * On Windows, we are in a single process, so we can just check if it's
     437             :  * time to stop.
     438             :  */
     439             : static bool
     440        6776 : reached_end_position(XLogRecPtr segendpos, uint32 timeline,
     441             :                      bool segment_finished)
     442             : {
     443        6776 :     if (!has_xlogendptr)
     444             :     {
     445             : #ifndef WIN32
     446             :         fd_set      fds;
     447             :         struct timeval tv;
     448             :         int         r;
     449             : 
     450             :         /*
     451             :          * Don't have the end pointer yet - check our pipe to see if it has
     452             :          * been sent yet.
     453             :          */
     454        6636 :         FD_ZERO(&fds);
     455        6636 :         FD_SET(bgpipe[0], &fds);
     456             : 
     457       19908 :         MemSet(&tv, 0, sizeof(tv));
     458             : 
     459        6636 :         r = select(bgpipe[0] + 1, &fds, NULL, NULL, &tv);
     460        6636 :         if (r == 1)
     461             :         {
     462             :             char        xlogend[64];
     463             :             uint32      hi,
     464             :                         lo;
     465             : 
     466        1134 :             MemSet(xlogend, 0, sizeof(xlogend));
     467         126 :             r = read(bgpipe[0], xlogend, sizeof(xlogend) - 1);
     468         126 :             if (r < 0)
     469             :             {
     470           0 :                 pg_log_error("could not read from ready pipe: %m");
     471           0 :                 exit(1);
     472             :             }
     473             : 
     474         126 :             if (sscanf(xlogend, "%X/%X", &hi, &lo) != 2)
     475             :             {
     476           0 :                 pg_log_error("could not parse write-ahead log location \"%s\"",
     477             :                              xlogend);
     478           0 :                 exit(1);
     479             :             }
     480         126 :             xlogendptr = ((uint64) hi) << 32 | lo;
     481         126 :             has_xlogendptr = 1;
     482             : 
     483             :             /*
     484             :              * Fall through to check if we've reached the point further
     485             :              * already.
     486             :              */
     487             :         }
     488             :         else
     489             :         {
     490             :             /*
     491             :              * No data received on the pipe means we don't know the end
     492             :              * position yet - so just say it's not time to stop yet.
     493             :              */
     494        6510 :             return false;
     495             :         }
     496             : #else
     497             : 
     498             :         /*
     499             :          * On win32, has_xlogendptr is set by the main thread, so if it's not
     500             :          * set here, we just go back and wait until it shows up.
     501             :          */
     502             :         return false;
     503             : #endif
     504             :     }
     505             : 
     506             :     /*
     507             :      * At this point we have an end pointer, so compare it to the current
     508             :      * position to figure out if it's time to stop.
     509             :      */
     510         266 :     if (segendpos >= xlogendptr)
     511         252 :         return true;
     512             : 
     513             :     /*
     514             :      * Have end pointer, but haven't reached it yet - so tell the caller to
     515             :      * keep streaming.
     516             :      */
     517          14 :     return false;
     518             : }
     519             : 
     520             : typedef struct
     521             : {
     522             :     PGconn     *bgconn;
     523             :     XLogRecPtr  startptr;
     524             :     char        xlog[MAXPGPATH];    /* directory or tarfile depending on mode */
     525             :     char       *sysidentifier;
     526             :     int         timeline;
     527             : } logstreamer_param;
     528             : 
     529             : static int
     530         128 : LogStreamerMain(logstreamer_param *param)
     531             : {
     532             :     StreamCtl   stream;
     533             : 
     534         128 :     in_log_streamer = true;
     535             : 
     536        1280 :     MemSet(&stream, 0, sizeof(stream));
     537         128 :     stream.startpos = param->startptr;
     538         128 :     stream.timeline = param->timeline;
     539         128 :     stream.sysidentifier = param->sysidentifier;
     540         128 :     stream.stream_stop = reached_end_position;
     541             : #ifndef WIN32
     542         128 :     stream.stop_socket = bgpipe[0];
     543             : #else
     544             :     stream.stop_socket = PGINVALID_SOCKET;
     545             : #endif
     546         128 :     stream.standby_message_timeout = standby_message_timeout;
     547         128 :     stream.synchronous = false;
     548             :     /* fsync happens at the end of pg_basebackup for all data */
     549         128 :     stream.do_sync = false;
     550         128 :     stream.mark_done = true;
     551         128 :     stream.partial_suffix = NULL;
     552         128 :     stream.replication_slot = replication_slot;
     553             : 
     554         128 :     if (format == 'p')
     555         120 :         stream.walmethod = CreateWalDirectoryMethod(param->xlog, 0,
     556         120 :                                                     stream.do_sync);
     557             :     else
     558           8 :         stream.walmethod = CreateWalTarMethod(param->xlog, compresslevel,
     559           8 :                                               stream.do_sync);
     560             : 
     561         128 :     if (!ReceiveXlogStream(param->bgconn, &stream))
     562             : 
     563             :         /*
     564             :          * Any errors will already have been reported in the function process,
     565             :          * but we need to tell the parent that we didn't shutdown in a nice
     566             :          * way.
     567             :          */
     568           2 :         return 1;
     569             : 
     570         126 :     if (!stream.walmethod->finish())
     571             :     {
     572           0 :         pg_log_error("could not finish writing WAL files: %m");
     573           0 :         return 1;
     574             :     }
     575             : 
     576         126 :     PQfinish(param->bgconn);
     577             : 
     578         126 :     if (format == 'p')
     579         118 :         FreeWalDirectoryMethod();
     580             :     else
     581           8 :         FreeWalTarMethod();
     582         126 :     pg_free(stream.walmethod);
     583             : 
     584         126 :     return 0;
     585             : }
     586             : 
     587             : /*
     588             :  * Initiate background process for receiving xlog during the backup.
     589             :  * The background stream will use its own database connection so we can
     590             :  * stream the logfile in parallel with the backups.
     591             :  */
     592             : static void
     593         138 : StartLogStreamer(char *startpos, uint32 timeline, char *sysidentifier)
     594             : {
     595             :     logstreamer_param *param;
     596             :     uint32      hi,
     597             :                 lo;
     598             :     char        statusdir[MAXPGPATH];
     599             : 
     600         138 :     param = pg_malloc0(sizeof(logstreamer_param));
     601         138 :     param->timeline = timeline;
     602         138 :     param->sysidentifier = sysidentifier;
     603             : 
     604             :     /* Convert the starting position */
     605         138 :     if (sscanf(startpos, "%X/%X", &hi, &lo) != 2)
     606             :     {
     607           0 :         pg_log_error("could not parse write-ahead log location \"%s\"",
     608             :                      startpos);
     609           0 :         exit(1);
     610             :     }
     611         138 :     param->startptr = ((uint64) hi) << 32 | lo;
     612             :     /* Round off to even segment position */
     613         138 :     param->startptr -= XLogSegmentOffset(param->startptr, WalSegSz);
     614             : 
     615             : #ifndef WIN32
     616             :     /* Create our background pipe */
     617         138 :     if (pipe(bgpipe) < 0)
     618             :     {
     619           0 :         pg_log_error("could not create pipe for background process: %m");
     620           0 :         exit(1);
     621             :     }
     622             : #endif
     623             : 
     624             :     /* Get a second connection */
     625         138 :     param->bgconn = GetConnection();
     626         138 :     if (!param->bgconn)
     627             :         /* Error message already written in GetConnection() */
     628           0 :         exit(1);
     629             : 
     630             :     /* In post-10 cluster, pg_xlog has been renamed to pg_wal */
     631         138 :     snprintf(param->xlog, sizeof(param->xlog), "%s/%s",
     632             :              basedir,
     633         138 :              PQserverVersion(conn) < MINIMUM_VERSION_FOR_PG_WAL ?
     634             :              "pg_xlog" : "pg_wal");
     635             : 
     636             :     /* Temporary replication slots are only supported in 10 and newer */
     637         138 :     if (PQserverVersion(conn) < MINIMUM_VERSION_FOR_TEMP_SLOTS)
     638           0 :         temp_replication_slot = false;
     639             : 
     640             :     /*
     641             :      * Create replication slot if requested
     642             :      */
     643         138 :     if (temp_replication_slot && !replication_slot)
     644         126 :         replication_slot = psprintf("pg_basebackup_%d", (int) PQbackendPID(param->bgconn));
     645         138 :     if (temp_replication_slot || create_slot)
     646             :     {
     647         130 :         if (!CreateReplicationSlot(param->bgconn, replication_slot, NULL,
     648             :                                    temp_replication_slot, true, true, false))
     649           2 :             exit(1);
     650             : 
     651         128 :         if (verbose)
     652             :         {
     653           0 :             if (temp_replication_slot)
     654           0 :                 pg_log_info("created temporary replication slot \"%s\"",
     655             :                             replication_slot);
     656             :             else
     657           0 :                 pg_log_info("created replication slot \"%s\"",
     658             :                             replication_slot);
     659             :         }
     660             :     }
     661             : 
     662         136 :     if (format == 'p')
     663             :     {
     664             :         /*
     665             :          * Create pg_wal/archive_status or pg_xlog/archive_status (and thus
     666             :          * pg_wal or pg_xlog) depending on the target server so we can write
     667             :          * to basedir/pg_wal or basedir/pg_xlog as the directory entry in the
     668             :          * tar file may arrive later.
     669             :          */
     670         126 :         snprintf(statusdir, sizeof(statusdir), "%s/%s/archive_status",
     671             :                  basedir,
     672         126 :                  PQserverVersion(conn) < MINIMUM_VERSION_FOR_PG_WAL ?
     673             :                  "pg_xlog" : "pg_wal");
     674             : 
     675         126 :         if (pg_mkdir_p(statusdir, pg_dir_create_mode) != 0 && errno != EEXIST)
     676             :         {
     677           0 :             pg_log_error("could not create directory \"%s\": %m", statusdir);
     678           0 :             exit(1);
     679             :         }
     680             :     }
     681             : 
     682             :     /*
     683             :      * Start a child process and tell it to start streaming. On Unix, this is
     684             :      * a fork(). On Windows, we create a thread.
     685             :      */
     686             : #ifndef WIN32
     687         136 :     bgchild = fork();
     688         264 :     if (bgchild == 0)
     689             :     {
     690             :         /* in child process */
     691         128 :         exit(LogStreamerMain(param));
     692             :     }
     693         136 :     else if (bgchild < 0)
     694             :     {
     695           0 :         pg_log_error("could not create background process: %m");
     696           0 :         exit(1);
     697             :     }
     698             : 
     699             :     /*
     700             :      * Else we are in the parent process and all is well.
     701             :      */
     702         136 :     atexit(kill_bgchild_atexit);
     703             : #else                           /* WIN32 */
     704             :     bgchild = _beginthreadex(NULL, 0, (void *) LogStreamerMain, param, 0, NULL);
     705             :     if (bgchild == 0)
     706             :     {
     707             :         pg_log_error("could not create background thread: %m");
     708             :         exit(1);
     709             :     }
     710             : #endif
     711         136 : }
     712             : 
     713             : /*
     714             :  * Verify that the given directory exists and is empty. If it does not
     715             :  * exist, it is created. If it exists but is not empty, an error will
     716             :  * be given and the process ended.
     717             :  */
     718             : static void
     719         176 : verify_dir_is_empty_or_create(char *dirname, bool *created, bool *found)
     720             : {
     721         176 :     switch (pg_check_dir(dirname))
     722             :     {
     723         152 :         case 0:
     724             : 
     725             :             /*
     726             :              * Does not exist, so create
     727             :              */
     728         152 :             if (pg_mkdir_p(dirname, pg_dir_create_mode) == -1)
     729             :             {
     730           0 :                 pg_log_error("could not create directory \"%s\": %m", dirname);
     731           0 :                 exit(1);
     732             :             }
     733         152 :             if (created)
     734         152 :                 *created = true;
     735         152 :             return;
     736          22 :         case 1:
     737             : 
     738             :             /*
     739             :              * Exists, empty
     740             :              */
     741          22 :             if (found)
     742          22 :                 *found = true;
     743          22 :             return;
     744           2 :         case 2:
     745             :         case 3:
     746             :         case 4:
     747             : 
     748             :             /*
     749             :              * Exists, not empty
     750             :              */
     751           2 :             pg_log_error("directory \"%s\" exists but is not empty", dirname);
     752           2 :             exit(1);
     753           0 :         case -1:
     754             : 
     755             :             /*
     756             :              * Access problem
     757             :              */
     758           0 :             pg_log_error("could not access directory \"%s\": %m", dirname);
     759           0 :             exit(1);
     760             :     }
     761             : }
     762             : 
     763             : 
     764             : /*
     765             :  * Print a progress report based on the global variables. If verbose output
     766             :  * is enabled, also print the current file name.
     767             :  *
     768             :  * Progress report is written at maximum once per second, unless the
     769             :  * force parameter is set to true.
     770             :  */
     771             : static void
     772      170748 : progress_report(int tablespacenum, const char *filename, bool force)
     773             : {
     774             :     int         percent;
     775             :     char        totaldone_str[32];
     776             :     char        totalsize_str[32];
     777             :     pg_time_t   now;
     778             : 
     779      170748 :     if (!showprogress)
     780      170748 :         return;
     781             : 
     782           0 :     now = time(NULL);
     783           0 :     if (now == last_progress_report && !force)
     784           0 :         return;                 /* Max once per second */
     785             : 
     786           0 :     last_progress_report = now;
     787           0 :     percent = totalsize_kb ? (int) ((totaldone / 1024) * 100 / totalsize_kb) : 0;
     788             : 
     789             :     /*
     790             :      * Avoid overflowing past 100% or the full size. This may make the total
     791             :      * size number change as we approach the end of the backup (the estimate
     792             :      * will always be wrong if WAL is included), but that's better than having
     793             :      * the done column be bigger than the total.
     794             :      */
     795           0 :     if (percent > 100)
     796           0 :         percent = 100;
     797           0 :     if (totaldone / 1024 > totalsize_kb)
     798           0 :         totalsize_kb = totaldone / 1024;
     799             : 
     800             :     /*
     801             :      * Separate step to keep platform-dependent format code out of
     802             :      * translatable strings.  And we only test for INT64_FORMAT availability
     803             :      * in snprintf, not fprintf.
     804             :      */
     805           0 :     snprintf(totaldone_str, sizeof(totaldone_str), INT64_FORMAT,
     806             :              totaldone / 1024);
     807           0 :     snprintf(totalsize_str, sizeof(totalsize_str), INT64_FORMAT, totalsize_kb);
     808             : 
     809             : #define VERBOSE_FILENAME_LENGTH 35
     810           0 :     if (verbose)
     811             :     {
     812           0 :         if (!filename)
     813             : 
     814             :             /*
     815             :              * No filename given, so clear the status line (used for last
     816             :              * call)
     817             :              */
     818           0 :             fprintf(stderr,
     819           0 :                     ngettext("%*s/%s kB (100%%), %d/%d tablespace %*s",
     820             :                              "%*s/%s kB (100%%), %d/%d tablespaces %*s",
     821             :                              tablespacecount),
     822           0 :                     (int) strlen(totalsize_str),
     823             :                     totaldone_str, totalsize_str,
     824             :                     tablespacenum, tablespacecount,
     825             :                     VERBOSE_FILENAME_LENGTH + 5, "");
     826             :         else
     827             :         {
     828           0 :             bool        truncate = (strlen(filename) > VERBOSE_FILENAME_LENGTH);
     829             : 
     830           0 :             fprintf(stderr,
     831           0 :                     ngettext("%*s/%s kB (%d%%), %d/%d tablespace (%s%-*.*s)",
     832             :                              "%*s/%s kB (%d%%), %d/%d tablespaces (%s%-*.*s)",
     833             :                              tablespacecount),
     834           0 :                     (int) strlen(totalsize_str),
     835             :                     totaldone_str, totalsize_str, percent,
     836             :                     tablespacenum, tablespacecount,
     837             :             /* Prefix with "..." if we do leading truncation */
     838             :                     truncate ? "..." : "",
     839             :                     truncate ? VERBOSE_FILENAME_LENGTH - 3 : VERBOSE_FILENAME_LENGTH,
     840             :                     truncate ? VERBOSE_FILENAME_LENGTH - 3 : VERBOSE_FILENAME_LENGTH,
     841             :             /* Truncate filename at beginning if it's too long */
     842           0 :                     truncate ? filename + strlen(filename) - VERBOSE_FILENAME_LENGTH + 3 : filename);
     843             :         }
     844             :     }
     845             :     else
     846           0 :         fprintf(stderr,
     847           0 :                 ngettext("%*s/%s kB (%d%%), %d/%d tablespace",
     848             :                          "%*s/%s kB (%d%%), %d/%d tablespaces",
     849             :                          tablespacecount),
     850           0 :                 (int) strlen(totalsize_str),
     851             :                 totaldone_str, totalsize_str, percent,
     852             :                 tablespacenum, tablespacecount);
     853             : 
     854           0 :     if (isatty(fileno(stderr)))
     855           0 :         fprintf(stderr, "\r");
     856             :     else
     857           0 :         fprintf(stderr, "\n");
     858             : }
     859             : 
     860             : static int32
     861           0 : parse_max_rate(char *src)
     862             : {
     863             :     double      result;
     864             :     char       *after_num;
     865           0 :     char       *suffix = NULL;
     866             : 
     867           0 :     errno = 0;
     868           0 :     result = strtod(src, &after_num);
     869           0 :     if (src == after_num)
     870             :     {
     871           0 :         pg_log_error("transfer rate \"%s\" is not a valid value", src);
     872           0 :         exit(1);
     873             :     }
     874           0 :     if (errno != 0)
     875             :     {
     876           0 :         pg_log_error("invalid transfer rate \"%s\": %m", src);
     877           0 :         exit(1);
     878             :     }
     879             : 
     880           0 :     if (result <= 0)
     881             :     {
     882             :         /*
     883             :          * Reject obviously wrong values here.
     884             :          */
     885           0 :         pg_log_error("transfer rate must be greater than zero");
     886           0 :         exit(1);
     887             :     }
     888             : 
     889             :     /*
     890             :      * Evaluate suffix, after skipping over possible whitespace. Lack of
     891             :      * suffix means kilobytes.
     892             :      */
     893           0 :     while (*after_num != '\0' && isspace((unsigned char) *after_num))
     894           0 :         after_num++;
     895             : 
     896           0 :     if (*after_num != '\0')
     897             :     {
     898           0 :         suffix = after_num;
     899           0 :         if (*after_num == 'k')
     900             :         {
     901             :             /* kilobyte is the expected unit. */
     902           0 :             after_num++;
     903             :         }
     904           0 :         else if (*after_num == 'M')
     905             :         {
     906           0 :             after_num++;
     907           0 :             result *= 1024.0;
     908             :         }
     909             :     }
     910             : 
     911             :     /* The rest can only consist of white space. */
     912           0 :     while (*after_num != '\0' && isspace((unsigned char) *after_num))
     913           0 :         after_num++;
     914             : 
     915           0 :     if (*after_num != '\0')
     916             :     {
     917           0 :         pg_log_error("invalid --max-rate unit: \"%s\"", suffix);
     918           0 :         exit(1);
     919             :     }
     920             : 
     921             :     /* Valid integer? */
     922           0 :     if ((uint64) result != (uint64) ((uint32) result))
     923             :     {
     924           0 :         pg_log_error("transfer rate \"%s\" exceeds integer range", src);
     925           0 :         exit(1);
     926             :     }
     927             : 
     928             :     /*
     929             :      * The range is checked on the server side too, but avoid the server
     930             :      * connection if a nonsensical value was passed.
     931             :      */
     932           0 :     if (result < MAX_RATE_LOWER || result > MAX_RATE_UPPER)
     933             :     {
     934           0 :         pg_log_error("transfer rate \"%s\" is out of range", src);
     935           0 :         exit(1);
     936             :     }
     937             : 
     938           0 :     return (int32) result;
     939             : }
     940             : 
     941             : /*
     942             :  * Read a stream of COPY data and invoke the provided callback for each
     943             :  * chunk.
     944             :  */
     945             : static void
     946         308 : ReceiveCopyData(PGconn *conn, WriteDataCallback callback,
     947             :                 void *callback_data)
     948             : {
     949             :     PGresult   *res;
     950             : 
     951             :     /* Get the COPY data stream. */
     952         308 :     res = PQgetResult(conn);
     953         308 :     if (PQresultStatus(res) != PGRES_COPY_OUT)
     954             :     {
     955           2 :         pg_log_error("could not get COPY data stream: %s",
     956             :                      PQerrorMessage(conn));
     957           2 :         exit(1);
     958             :     }
     959         306 :     PQclear(res);
     960             : 
     961             :     /* Loop over chunks until done. */
     962             :     while (1)
     963      300532 :     {
     964             :         int         r;
     965             :         char       *copybuf;
     966             : 
     967      300838 :         r = PQgetCopyData(conn, &copybuf, 0);
     968      300838 :         if (r == -1)
     969             :         {
     970             :             /* End of chunk. */
     971         306 :             break;
     972             :         }
     973      300532 :         else if (r == -2)
     974             :         {
     975           0 :             pg_log_error("could not read COPY data: %s",
     976             :                          PQerrorMessage(conn));
     977           0 :             exit(1);
     978             :         }
     979             : 
     980      300532 :         (*callback) (r, copybuf, callback_data);
     981             : 
     982      300532 :         PQfreemem(copybuf);
     983             :     }
     984         306 : }
     985             : 
     986             : /*
     987             :  * Write a piece of tar data
     988             :  */
     989             : static void
     990       17166 : writeTarData(WriteTarState *state, char *buf, int r)
     991             : {
     992             : #ifdef HAVE_LIBZ
     993       17166 :     if (state->ztarfile != NULL)
     994             :     {
     995           0 :         if (gzwrite(state->ztarfile, buf, r) != r)
     996             :         {
     997           0 :             pg_log_error("could not write to compressed file \"%s\": %s",
     998             :                          state->filename, get_gz_error(state->ztarfile));
     999           0 :             exit(1);
    1000             :         }
    1001             :     }
    1002             :     else
    1003             : #endif
    1004             :     {
    1005       17166 :         if (fwrite(buf, r, 1, state->tarfile) != 1)
    1006             :         {
    1007           0 :             pg_log_error("could not write to file \"%s\": %m",
    1008             :                          state->filename);
    1009           0 :             exit(1);
    1010             :         }
    1011             :     }
    1012       17166 : }
    1013             : 
    1014             : /*
    1015             :  * Receive a tar format file from the connection to the server, and write
    1016             :  * the data from this file directly into a tar file. If compression is
    1017             :  * enabled, the data will be compressed while written to the file.
    1018             :  *
    1019             :  * The file will be named base.tar[.gz] if it's for the main data directory
    1020             :  * or <tablespaceoid>.tar[.gz] if it's for another tablespace.
    1021             :  *
    1022             :  * No attempt to inspect or validate the contents of the file is done.
    1023             :  */
    1024             : static void
    1025          14 : ReceiveTarFile(PGconn *conn, PGresult *res, int rownum)
    1026             : {
    1027             :     char        zerobuf[1024];
    1028             :     WriteTarState state;
    1029             : 
    1030          14 :     memset(&state, 0, sizeof(state));
    1031          14 :     state.tablespacenum = rownum;
    1032          14 :     state.basetablespace = PQgetisnull(res, rownum, 0);
    1033          14 :     state.in_tarhdr = true;
    1034             : 
    1035             :     /* recovery.conf is integrated into postgresql.conf in 12 and newer */
    1036          14 :     if (PQserverVersion(conn) >= MINIMUM_VERSION_FOR_RECOVERY_GUC)
    1037          14 :         state.is_recovery_guc_supported = true;
    1038             : 
    1039          14 :     if (state.basetablespace)
    1040             :     {
    1041             :         /*
    1042             :          * Base tablespaces
    1043             :          */
    1044          10 :         if (strcmp(basedir, "-") == 0)
    1045             :         {
    1046             : #ifdef WIN32
    1047             :             _setmode(fileno(stdout), _O_BINARY);
    1048             : #endif
    1049             : 
    1050             : #ifdef HAVE_LIBZ
    1051           0 :             if (compresslevel != 0)
    1052             :             {
    1053           0 :                 int         fd = dup(fileno(stdout));
    1054             : 
    1055           0 :                 if (fd < 0)
    1056             :                 {
    1057           0 :                     pg_log_error("could not duplicate stdout: %m");
    1058           0 :                     exit(1);
    1059             :                 }
    1060             : 
    1061           0 :                 state.ztarfile = gzdopen(fd, "wb");
    1062           0 :                 if (state.ztarfile == NULL)
    1063             :                 {
    1064           0 :                     pg_log_error("could not open output file: %m");
    1065           0 :                     exit(1);
    1066             :                 }
    1067             : 
    1068           0 :                 if (gzsetparams(state.ztarfile, compresslevel,
    1069             :                                 Z_DEFAULT_STRATEGY) != Z_OK)
    1070             :                 {
    1071           0 :                     pg_log_error("could not set compression level %d: %s",
    1072             :                                  compresslevel, get_gz_error(state.ztarfile));
    1073           0 :                     exit(1);
    1074             :                 }
    1075             :             }
    1076             :             else
    1077             : #endif
    1078           0 :                 state.tarfile = stdout;
    1079           0 :             strcpy(state.filename, "-");
    1080             :         }
    1081             :         else
    1082             :         {
    1083             : #ifdef HAVE_LIBZ
    1084          10 :             if (compresslevel != 0)
    1085             :             {
    1086           0 :                 snprintf(state.filename, sizeof(state.filename),
    1087             :                          "%s/base.tar.gz", basedir);
    1088           0 :                 state.ztarfile = gzopen(state.filename, "wb");
    1089           0 :                 if (gzsetparams(state.ztarfile, compresslevel,
    1090             :                                 Z_DEFAULT_STRATEGY) != Z_OK)
    1091             :                 {
    1092           0 :                     pg_log_error("could not set compression level %d: %s",
    1093             :                                  compresslevel, get_gz_error(state.ztarfile));
    1094           0 :                     exit(1);
    1095             :                 }
    1096             :             }
    1097             :             else
    1098             : #endif
    1099             :             {
    1100          10 :                 snprintf(state.filename, sizeof(state.filename),
    1101             :                          "%s/base.tar", basedir);
    1102          10 :                 state.tarfile = fopen(state.filename, "wb");
    1103             :             }
    1104             :         }
    1105             :     }
    1106             :     else
    1107             :     {
    1108             :         /*
    1109             :          * Specific tablespace
    1110             :          */
    1111             : #ifdef HAVE_LIBZ
    1112           4 :         if (compresslevel != 0)
    1113             :         {
    1114           0 :             snprintf(state.filename, sizeof(state.filename),
    1115             :                      "%s/%s.tar.gz",
    1116             :                      basedir, PQgetvalue(res, rownum, 0));
    1117           0 :             state.ztarfile = gzopen(state.filename, "wb");
    1118           0 :             if (gzsetparams(state.ztarfile, compresslevel,
    1119             :                             Z_DEFAULT_STRATEGY) != Z_OK)
    1120             :             {
    1121           0 :                 pg_log_error("could not set compression level %d: %s",
    1122             :                              compresslevel, get_gz_error(state.ztarfile));
    1123           0 :                 exit(1);
    1124             :             }
    1125             :         }
    1126             :         else
    1127             : #endif
    1128             :         {
    1129           4 :             snprintf(state.filename, sizeof(state.filename), "%s/%s.tar",
    1130             :                      basedir, PQgetvalue(res, rownum, 0));
    1131           4 :             state.tarfile = fopen(state.filename, "wb");
    1132             :         }
    1133             :     }
    1134             : 
    1135             : #ifdef HAVE_LIBZ
    1136          14 :     if (compresslevel != 0)
    1137             :     {
    1138           0 :         if (!state.ztarfile)
    1139             :         {
    1140             :             /* Compression is in use */
    1141           0 :             pg_log_error("could not create compressed file \"%s\": %s",
    1142             :                          state.filename, get_gz_error(state.ztarfile));
    1143           0 :             exit(1);
    1144             :         }
    1145             :     }
    1146             :     else
    1147             : #endif
    1148             :     {
    1149             :         /* Either no zlib support, or zlib support but compresslevel = 0 */
    1150          14 :         if (!state.tarfile)
    1151             :         {
    1152           0 :             pg_log_error("could not create file \"%s\": %m", state.filename);
    1153           0 :             exit(1);
    1154             :         }
    1155             :     }
    1156             : 
    1157          14 :     ReceiveCopyData(conn, ReceiveTarCopyChunk, &state);
    1158             : 
    1159             :     /*
    1160             :      * End of copy data. If requested, and this is the base tablespace, write
    1161             :      * configuration file into the tarfile. When done, close the file (but not
    1162             :      * stdout).
    1163             :      *
    1164             :      * Also, write two completely empty blocks at the end of the tar file, as
    1165             :      * required by some tar programs.
    1166             :      */
    1167             : 
    1168        1806 :     MemSet(zerobuf, 0, sizeof(zerobuf));
    1169             : 
    1170          14 :     if (state.basetablespace && writerecoveryconf)
    1171             :     {
    1172             :         char        header[512];
    1173             : 
    1174             :         /*
    1175             :          * If postgresql.auto.conf has not been found in the streamed data,
    1176             :          * add recovery configuration to postgresql.auto.conf if recovery
    1177             :          * parameters are GUCs.  If the instance connected to is older than
    1178             :          * 12, create recovery.conf with this data otherwise.
    1179             :          */
    1180           0 :         if (!state.found_postgresql_auto_conf || !state.is_recovery_guc_supported)
    1181             :         {
    1182             :             int         padding;
    1183             : 
    1184           0 :             tarCreateHeader(header,
    1185           0 :                             state.is_recovery_guc_supported ? "postgresql.auto.conf" : "recovery.conf",
    1186             :                             NULL,
    1187           0 :                             recoveryconfcontents->len,
    1188             :                             pg_file_create_mode, 04000, 02000,
    1189             :                             time(NULL));
    1190             : 
    1191           0 :             padding = ((recoveryconfcontents->len + 511) & ~511) - recoveryconfcontents->len;
    1192             : 
    1193           0 :             writeTarData(&state, header, sizeof(header));
    1194           0 :             writeTarData(&state, recoveryconfcontents->data,
    1195           0 :                          recoveryconfcontents->len);
    1196           0 :             if (padding)
    1197           0 :                 writeTarData(&state, zerobuf, padding);
    1198             :         }
    1199             : 
    1200             :         /*
    1201             :          * standby.signal is supported only if recovery parameters are GUCs.
    1202             :          */
    1203           0 :         if (state.is_recovery_guc_supported)
    1204             :         {
    1205           0 :             tarCreateHeader(header, "standby.signal", NULL,
    1206             :                             0,  /* zero-length file */
    1207             :                             pg_file_create_mode, 04000, 02000,
    1208             :                             time(NULL));
    1209             : 
    1210           0 :             writeTarData(&state, header, sizeof(header));
    1211             : 
    1212             :             /*
    1213             :              * we don't need to pad out to a multiple of the tar block size
    1214             :              * here, because the file is zero length, which is a multiple of
    1215             :              * any block size.
    1216             :              */
    1217             :         }
    1218             :     }
    1219             : 
    1220             :     /*
    1221             :      * Normally, we emit the backup manifest as a separate file, but when
    1222             :      * we're writing a tarfile to stdout, we don't have that option, so
    1223             :      * include it in the one tarfile we've got.
    1224             :      */
    1225          14 :     if (strcmp(basedir, "-") == 0 && manifest)
    1226             :     {
    1227             :         char        header[512];
    1228             :         PQExpBufferData buf;
    1229             : 
    1230           0 :         initPQExpBuffer(&buf);
    1231           0 :         ReceiveBackupManifestInMemory(conn, &buf);
    1232           0 :         if (PQExpBufferDataBroken(buf))
    1233             :         {
    1234           0 :             pg_log_error("out of memory");
    1235           0 :             exit(1);
    1236             :         }
    1237           0 :         tarCreateHeader(header, "backup_manifest", NULL, buf.len,
    1238             :                         pg_file_create_mode, 04000, 02000,
    1239             :                         time(NULL));
    1240           0 :         writeTarData(&state, header, sizeof(header));
    1241           0 :         writeTarData(&state, buf.data, buf.len);
    1242           0 :         termPQExpBuffer(&buf);
    1243             :     }
    1244             : 
    1245             :     /* 2 * 512 bytes empty data at end of file */
    1246          14 :     writeTarData(&state, zerobuf, sizeof(zerobuf));
    1247             : 
    1248             : #ifdef HAVE_LIBZ
    1249          14 :     if (state.ztarfile != NULL)
    1250             :     {
    1251           0 :         if (gzclose(state.ztarfile) != 0)
    1252             :         {
    1253           0 :             pg_log_error("could not close compressed file \"%s\": %s",
    1254             :                          state.filename, get_gz_error(state.ztarfile));
    1255           0 :             exit(1);
    1256             :         }
    1257             :     }
    1258             :     else
    1259             : #endif
    1260             :     {
    1261          14 :         if (strcmp(basedir, "-") != 0)
    1262             :         {
    1263          14 :             if (fclose(state.tarfile) != 0)
    1264             :             {
    1265           0 :                 pg_log_error("could not close file \"%s\": %m",
    1266             :                              state.filename);
    1267           0 :                 exit(1);
    1268             :             }
    1269             :         }
    1270             :     }
    1271             : 
    1272          14 :     progress_report(rownum, state.filename, true);
    1273             : 
    1274             :     /*
    1275             :      * Do not sync the resulting tar file yet, all files are synced once at
    1276             :      * the end.
    1277             :      */
    1278          14 : }
    1279             : 
    1280             : /*
    1281             :  * Receive one chunk of tar-format data from the server.
    1282             :  */
    1283             : static void
    1284       17152 : ReceiveTarCopyChunk(size_t r, char *copybuf, void *callback_data)
    1285             : {
    1286       17152 :     WriteTarState *state = callback_data;
    1287             : 
    1288       17152 :     if (!writerecoveryconf || !state->basetablespace)
    1289             :     {
    1290             :         /*
    1291             :          * When not writing config file, or when not working on the base
    1292             :          * tablespace, we never have to look for an existing configuration
    1293             :          * file in the stream.
    1294             :          */
    1295       17152 :         writeTarData(state, copybuf, r);
    1296             :     }
    1297             :     else
    1298             :     {
    1299             :         /*
    1300             :          * Look for a config file in the existing tar stream. If it's there,
    1301             :          * we must skip it so we can later overwrite it with our own version
    1302             :          * of the file.
    1303             :          *
    1304             :          * To do this, we have to process the individual files inside the TAR
    1305             :          * stream. The stream consists of a header and zero or more chunks,
    1306             :          * all 512 bytes long. The stream from the server is broken up into
    1307             :          * smaller pieces, so we have to track the size of the files to find
    1308             :          * the next header structure.
    1309             :          */
    1310           0 :         int         rr = r;
    1311           0 :         int         pos = 0;
    1312             : 
    1313           0 :         while (rr > 0)
    1314             :         {
    1315           0 :             if (state->in_tarhdr)
    1316             :             {
    1317             :                 /*
    1318             :                  * We're currently reading a header structure inside the TAR
    1319             :                  * stream, i.e. the file metadata.
    1320             :                  */
    1321           0 :                 if (state->tarhdrsz < 512)
    1322             :                 {
    1323             :                     /*
    1324             :                      * Copy the header structure into tarhdr in case the
    1325             :                      * header is not aligned to 512 bytes or it's not returned
    1326             :                      * in whole by the last PQgetCopyData call.
    1327             :                      */
    1328             :                     int         hdrleft;
    1329             :                     int         bytes2copy;
    1330             : 
    1331           0 :                     hdrleft = 512 - state->tarhdrsz;
    1332           0 :                     bytes2copy = (rr > hdrleft ? hdrleft : rr);
    1333             : 
    1334           0 :                     memcpy(&state->tarhdr[state->tarhdrsz], copybuf + pos,
    1335             :                            bytes2copy);
    1336             : 
    1337           0 :                     rr -= bytes2copy;
    1338           0 :                     pos += bytes2copy;
    1339           0 :                     state->tarhdrsz += bytes2copy;
    1340             :                 }
    1341             :                 else
    1342             :                 {
    1343             :                     /*
    1344             :                      * We have the complete header structure in tarhdr, look
    1345             :                      * at the file metadata: we may want append recovery info
    1346             :                      * into postgresql.auto.conf and skip standby.signal file
    1347             :                      * if recovery parameters are integrated as GUCs, and
    1348             :                      * recovery.conf otherwise. In both cases we must
    1349             :                      * calculate tar padding.
    1350             :                      */
    1351           0 :                     if (state->is_recovery_guc_supported)
    1352             :                     {
    1353           0 :                         state->skip_file =
    1354           0 :                             (strcmp(&state->tarhdr[0], "standby.signal") == 0);
    1355           0 :                         state->is_postgresql_auto_conf =
    1356           0 :                             (strcmp(&state->tarhdr[0], "postgresql.auto.conf") == 0);
    1357             :                     }
    1358             :                     else
    1359           0 :                         state->skip_file =
    1360           0 :                             (strcmp(&state->tarhdr[0], "recovery.conf") == 0);
    1361             : 
    1362           0 :                     state->filesz = read_tar_number(&state->tarhdr[124], 12);
    1363           0 :                     state->file_padding_len =
    1364           0 :                         ((state->filesz + 511) & ~511) - state->filesz;
    1365             : 
    1366           0 :                     if (state->is_recovery_guc_supported &&
    1367           0 :                         state->is_postgresql_auto_conf &&
    1368             :                         writerecoveryconf)
    1369           0 :                     {
    1370             :                         /* replace tar header */
    1371             :                         char        header[512];
    1372             : 
    1373           0 :                         tarCreateHeader(header, "postgresql.auto.conf", NULL,
    1374           0 :                                         state->filesz + recoveryconfcontents->len,
    1375             :                                         pg_file_create_mode, 04000, 02000,
    1376             :                                         time(NULL));
    1377             : 
    1378           0 :                         writeTarData(state, header, sizeof(header));
    1379             :                     }
    1380             :                     else
    1381             :                     {
    1382             :                         /* copy stream with padding */
    1383           0 :                         state->filesz += state->file_padding_len;
    1384             : 
    1385           0 :                         if (!state->skip_file)
    1386             :                         {
    1387             :                             /*
    1388             :                              * If we're not skipping the file, write the tar
    1389             :                              * header unmodified.
    1390             :                              */
    1391           0 :                             writeTarData(state, state->tarhdr, 512);
    1392             :                         }
    1393             :                     }
    1394             : 
    1395             :                     /* Next part is the file, not the header */
    1396           0 :                     state->in_tarhdr = false;
    1397             :                 }
    1398             :             }
    1399             :             else
    1400             :             {
    1401             :                 /*
    1402             :                  * We're processing a file's contents.
    1403             :                  */
    1404           0 :                 if (state->filesz > 0)
    1405             :                 {
    1406             :                     /*
    1407             :                      * We still have data to read (and possibly write).
    1408             :                      */
    1409             :                     int         bytes2write;
    1410             : 
    1411           0 :                     bytes2write = (state->filesz > rr ? rr : state->filesz);
    1412             : 
    1413           0 :                     if (!state->skip_file)
    1414           0 :                         writeTarData(state, copybuf + pos, bytes2write);
    1415             : 
    1416           0 :                     rr -= bytes2write;
    1417           0 :                     pos += bytes2write;
    1418           0 :                     state->filesz -= bytes2write;
    1419             :                 }
    1420           0 :                 else if (state->is_recovery_guc_supported &&
    1421           0 :                          state->is_postgresql_auto_conf &&
    1422             :                          writerecoveryconf)
    1423           0 :                 {
    1424             :                     /* append recovery config to postgresql.auto.conf */
    1425             :                     int         padding;
    1426             :                     int         tailsize;
    1427             : 
    1428           0 :                     tailsize = (512 - state->file_padding_len) + recoveryconfcontents->len;
    1429           0 :                     padding = ((tailsize + 511) & ~511) - tailsize;
    1430             : 
    1431           0 :                     writeTarData(state, recoveryconfcontents->data,
    1432           0 :                                  recoveryconfcontents->len);
    1433             : 
    1434           0 :                     if (padding)
    1435             :                     {
    1436             :                         char        zerobuf[512];
    1437             : 
    1438           0 :                         MemSet(zerobuf, 0, sizeof(zerobuf));
    1439           0 :                         writeTarData(state, zerobuf, padding);
    1440             :                     }
    1441             : 
    1442             :                     /* skip original file padding */
    1443           0 :                     state->is_postgresql_auto_conf = false;
    1444           0 :                     state->skip_file = true;
    1445           0 :                     state->filesz += state->file_padding_len;
    1446             : 
    1447           0 :                     state->found_postgresql_auto_conf = true;
    1448             :                 }
    1449             :                 else
    1450             :                 {
    1451             :                     /*
    1452             :                      * No more data in the current file, the next piece of
    1453             :                      * data (if any) will be a new file header structure.
    1454             :                      */
    1455           0 :                     state->in_tarhdr = true;
    1456           0 :                     state->skip_file = false;
    1457           0 :                     state->is_postgresql_auto_conf = false;
    1458           0 :                     state->tarhdrsz = 0;
    1459           0 :                     state->filesz = 0;
    1460             :                 }
    1461             :             }
    1462             :         }
    1463             :     }
    1464       17152 :     totaldone += r;
    1465       17152 :     progress_report(state->tablespacenum, state->filename, false);
    1466       17152 : }
    1467             : 
    1468             : 
    1469             : /*
    1470             :  * Retrieve tablespace path, either relocated or original depending on whether
    1471             :  * -T was passed or not.
    1472             :  */
    1473             : static const char *
    1474          80 : get_tablespace_mapping(const char *dir)
    1475             : {
    1476             :     TablespaceListCell *cell;
    1477             :     char        canon_dir[MAXPGPATH];
    1478             : 
    1479             :     /* Canonicalize path for comparison consistency */
    1480          80 :     strlcpy(canon_dir, dir, sizeof(canon_dir));
    1481          80 :     canonicalize_path(canon_dir);
    1482             : 
    1483          80 :     for (cell = tablespace_dirs.head; cell; cell = cell->next)
    1484          78 :         if (strcmp(canon_dir, cell->old_dir) == 0)
    1485          78 :             return cell->new_dir;
    1486             : 
    1487           2 :     return dir;
    1488             : }
    1489             : 
    1490             : 
    1491             : /*
    1492             :  * Receive a tar format stream from the connection to the server, and unpack
    1493             :  * the contents of it into a directory. Only files, directories and
    1494             :  * symlinks are supported, no other kinds of special files.
    1495             :  *
    1496             :  * If the data is for the main data directory, it will be restored in the
    1497             :  * specified directory. If it's for another tablespace, it will be restored
    1498             :  * in the original or mapped directory.
    1499             :  */
    1500             : static void
    1501         156 : ReceiveAndUnpackTarFile(PGconn *conn, PGresult *res, int rownum)
    1502             : {
    1503             :     UnpackTarState state;
    1504             :     bool        basetablespace;
    1505             : 
    1506         156 :     memset(&state, 0, sizeof(state));
    1507         156 :     state.tablespacenum = rownum;
    1508             : 
    1509         156 :     basetablespace = PQgetisnull(res, rownum, 0);
    1510         156 :     if (basetablespace)
    1511         130 :         strlcpy(state.current_path, basedir, sizeof(state.current_path));
    1512             :     else
    1513          26 :         strlcpy(state.current_path,
    1514          26 :                 get_tablespace_mapping(PQgetvalue(res, rownum, 1)),
    1515             :                 sizeof(state.current_path));
    1516             : 
    1517         156 :     ReceiveCopyData(conn, ReceiveTarAndUnpackCopyChunk, &state);
    1518             : 
    1519             : 
    1520         156 :     if (state.file)
    1521           0 :         fclose(state.file);
    1522             : 
    1523         156 :     progress_report(rownum, state.filename, true);
    1524             : 
    1525         156 :     if (state.file != NULL)
    1526             :     {
    1527           0 :         pg_log_error("COPY stream ended before last file was finished");
    1528           0 :         exit(1);
    1529             :     }
    1530             : 
    1531         156 :     if (basetablespace && writerecoveryconf)
    1532           4 :         WriteRecoveryConfig(conn, basedir, recoveryconfcontents);
    1533             : 
    1534             :     /*
    1535             :      * No data is synced here, everything is done for all tablespaces at the
    1536             :      * end.
    1537             :      */
    1538         156 : }
    1539             : 
    1540             : static void
    1541      280984 : ReceiveTarAndUnpackCopyChunk(size_t r, char *copybuf, void *callback_data)
    1542             : {
    1543      280984 :     UnpackTarState *state = callback_data;
    1544             : 
    1545      280984 :     if (state->file == NULL)
    1546             :     {
    1547             : #ifndef WIN32
    1548             :         int         filemode;
    1549             : #endif
    1550             : 
    1551             :         /*
    1552             :          * No current file, so this must be the header for a new file
    1553             :          */
    1554      126174 :         if (r != 512)
    1555             :         {
    1556           0 :             pg_log_error("invalid tar block header size: %zu", r);
    1557           0 :             exit(1);
    1558             :         }
    1559      126174 :         totaldone += 512;
    1560             : 
    1561      126174 :         state->current_len_left = read_tar_number(&copybuf[124], 12);
    1562             : 
    1563             : #ifndef WIN32
    1564             :         /* Set permissions on the file */
    1565      126174 :         filemode = read_tar_number(&copybuf[100], 8);
    1566             : #endif
    1567             : 
    1568             :         /*
    1569             :          * All files are padded up to 512 bytes
    1570             :          */
    1571      126174 :         state->current_padding =
    1572      126174 :             ((state->current_len_left + 511) & ~511) - state->current_len_left;
    1573             : 
    1574             :         /*
    1575             :          * First part of header is zero terminated filename
    1576             :          */
    1577      126174 :         snprintf(state->filename, sizeof(state->filename),
    1578      126174 :                  "%s/%s", state->current_path, copybuf);
    1579      126174 :         if (state->filename[strlen(state->filename) - 1] == '/')
    1580             :         {
    1581             :             /*
    1582             :              * Ends in a slash means directory or symlink to directory
    1583             :              */
    1584        3338 :             if (copybuf[156] == '5')
    1585             :             {
    1586             :                 /*
    1587             :                  * Directory. Remove trailing slash first.
    1588             :                  */
    1589        3312 :                 state->filename[strlen(state->filename) - 1] = '\0';
    1590        3312 :                 if (mkdir(state->filename, pg_dir_create_mode) != 0)
    1591             :                 {
    1592             :                     /*
    1593             :                      * When streaming WAL, pg_wal (or pg_xlog for pre-9.6
    1594             :                      * clusters) will have been created by the wal receiver
    1595             :                      * process. Also, when the WAL directory location was
    1596             :                      * specified, pg_wal (or pg_xlog) has already been created
    1597             :                      * as a symbolic link before starting the actual backup.
    1598             :                      * So just ignore creation failures on related
    1599             :                      * directories.
    1600             :                      */
    1601         378 :                     if (!((pg_str_endswith(state->filename, "/pg_wal") ||
    1602         252 :                            pg_str_endswith(state->filename, "/pg_xlog") ||
    1603         126 :                            pg_str_endswith(state->filename, "/archive_status")) &&
    1604         252 :                           errno == EEXIST))
    1605             :                     {
    1606           0 :                         pg_log_error("could not create directory \"%s\": %m",
    1607             :                                      state->filename);
    1608           0 :                         exit(1);
    1609             :                     }
    1610             :                 }
    1611             : #ifndef WIN32
    1612        3312 :                 if (chmod(state->filename, (mode_t) filemode))
    1613           0 :                     pg_log_error("could not set permissions on directory \"%s\": %m",
    1614             :                                  state->filename);
    1615             : #endif
    1616             :             }
    1617          26 :             else if (copybuf[156] == '2')
    1618             :             {
    1619             :                 /*
    1620             :                  * Symbolic link
    1621             :                  *
    1622             :                  * It's most likely a link in pg_tblspc directory, to the
    1623             :                  * location of a tablespace. Apply any tablespace mapping
    1624             :                  * given on the command line (--tablespace-mapping). (We
    1625             :                  * blindly apply the mapping without checking that the link
    1626             :                  * really is inside pg_tblspc. We don't expect there to be
    1627             :                  * other symlinks in a data directory, but if there are, you
    1628             :                  * can call it an undocumented feature that you can map them
    1629             :                  * too.)
    1630             :                  */
    1631          26 :                 state->filename[strlen(state->filename) - 1] = '\0';  /* Remove trailing slash */
    1632             : 
    1633          26 :                 state->mapped_tblspc_path =
    1634          26 :                     get_tablespace_mapping(&copybuf[157]);
    1635          26 :                 if (symlink(state->mapped_tblspc_path, state->filename) != 0)
    1636             :                 {
    1637           0 :                     pg_log_error("could not create symbolic link from \"%s\" to \"%s\": %m",
    1638             :                                  state->filename, state->mapped_tblspc_path);
    1639           0 :                     exit(1);
    1640             :                 }
    1641             :             }
    1642             :             else
    1643             :             {
    1644           0 :                 pg_log_error("unrecognized link indicator \"%c\"",
    1645             :                              copybuf[156]);
    1646           0 :                 exit(1);
    1647             :             }
    1648        3338 :             return;             /* directory or link handled */
    1649             :         }
    1650             : 
    1651             :         /*
    1652             :          * regular file
    1653             :          */
    1654      122836 :         state->file = fopen(state->filename, "wb");
    1655      122836 :         if (!state->file)
    1656             :         {
    1657           0 :             pg_log_error("could not create file \"%s\": %m", state->filename);
    1658           0 :             exit(1);
    1659             :         }
    1660             : 
    1661             : #ifndef WIN32
    1662      122836 :         if (chmod(state->filename, (mode_t) filemode))
    1663           0 :             pg_log_error("could not set permissions on file \"%s\": %m",
    1664             :                          state->filename);
    1665             : #endif
    1666             : 
    1667      122836 :         if (state->current_len_left == 0)
    1668             :         {
    1669             :             /*
    1670             :              * Done with this file, next one will be a new tar header
    1671             :              */
    1672       21348 :             fclose(state->file);
    1673       21348 :             state->file = NULL;
    1674       21348 :             return;
    1675             :         }
    1676             :     }                           /* new file */
    1677             :     else
    1678             :     {
    1679             :         /*
    1680             :          * Continuing blocks in existing file
    1681             :          */
    1682      154810 :         if (state->current_len_left == 0 && r == state->current_padding)
    1683             :         {
    1684             :             /*
    1685             :              * Received the padding block for this file, ignore it and close
    1686             :              * the file, then move on to the next tar header.
    1687             :              */
    1688        1384 :             fclose(state->file);
    1689        1384 :             state->file = NULL;
    1690        1384 :             totaldone += r;
    1691        1384 :             return;
    1692             :         }
    1693             : 
    1694      153426 :         if (fwrite(copybuf, r, 1, state->file) != 1)
    1695             :         {
    1696           0 :             pg_log_error("could not write to file \"%s\": %m", state->filename);
    1697           0 :             exit(1);
    1698             :         }
    1699      153426 :         totaldone += r;
    1700      153426 :         progress_report(state->tablespacenum, state->filename, false);
    1701             : 
    1702      153426 :         state->current_len_left -= r;
    1703      153426 :         if (state->current_len_left == 0 && state->current_padding == 0)
    1704             :         {
    1705             :             /*
    1706             :              * Received the last block, and there is no padding to be
    1707             :              * expected. Close the file and move on to the next tar header.
    1708             :              */
    1709      100104 :             fclose(state->file);
    1710      100104 :             state->file = NULL;
    1711      100104 :             return;
    1712             :         }
    1713             :     }                           /* continuing data in existing file */
    1714             : }
    1715             : 
    1716             : /*
    1717             :  * Receive the backup manifest file and write it out to a file.
    1718             :  */
    1719             : static void
    1720         138 : ReceiveBackupManifest(PGconn *conn)
    1721             : {
    1722             :     WriteManifestState state;
    1723             : 
    1724         138 :     snprintf(state.filename, sizeof(state.filename),
    1725             :              "%s/backup_manifest.tmp", basedir);
    1726         138 :     state.file = fopen(state.filename, "wb");
    1727         138 :     if (state.file == NULL)
    1728             :     {
    1729           0 :         pg_log_error("could not create file \"%s\": %m", state.filename);
    1730           0 :         exit(1);
    1731             :     }
    1732             : 
    1733         138 :     ReceiveCopyData(conn, ReceiveBackupManifestChunk, &state);
    1734             : 
    1735         136 :     fclose(state.file);
    1736         136 : }
    1737             : 
    1738             : /*
    1739             :  * Receive one chunk of the backup manifest file and write it out to a file.
    1740             :  */
    1741             : static void
    1742        2396 : ReceiveBackupManifestChunk(size_t r, char *copybuf, void *callback_data)
    1743             : {
    1744        2396 :     WriteManifestState *state = callback_data;
    1745             : 
    1746        2396 :     if (fwrite(copybuf, r, 1, state->file) != 1)
    1747             :     {
    1748           0 :         pg_log_error("could not write to file \"%s\": %m", state->filename);
    1749           0 :         exit(1);
    1750             :     }
    1751        2396 : }
    1752             : 
    1753             : /*
    1754             :  * Receive the backup manifest file and write it out to a file.
    1755             :  */
    1756             : static void
    1757           0 : ReceiveBackupManifestInMemory(PGconn *conn, PQExpBuffer buf)
    1758             : {
    1759           0 :     ReceiveCopyData(conn, ReceiveBackupManifestInMemoryChunk, buf);
    1760           0 : }
    1761             : 
    1762             : /*
    1763             :  * Receive one chunk of the backup manifest file and write it out to a file.
    1764             :  */
    1765             : static void
    1766           0 : ReceiveBackupManifestInMemoryChunk(size_t r, char *copybuf,
    1767             :                                    void *callback_data)
    1768             : {
    1769           0 :     PQExpBuffer buf = callback_data;
    1770             : 
    1771           0 :     appendPQExpBuffer(buf, copybuf, r);
    1772           0 : }
    1773             : 
    1774             : static void
    1775         146 : BaseBackup(void)
    1776             : {
    1777             :     PGresult   *res;
    1778             :     char       *sysidentifier;
    1779             :     TimeLineID  latesttli;
    1780             :     TimeLineID  starttli;
    1781             :     char       *basebkp;
    1782             :     char        escaped_label[MAXPGPATH];
    1783         146 :     char       *maxrate_clause = NULL;
    1784         146 :     char       *manifest_clause = NULL;
    1785         146 :     char       *manifest_checksums_clause = "";
    1786             :     int         i;
    1787             :     char        xlogstart[64];
    1788             :     char        xlogend[64];
    1789             :     int         minServerMajor,
    1790             :                 maxServerMajor;
    1791             :     int         serverVersion,
    1792             :                 serverMajor;
    1793             :     int         writing_to_stdout;
    1794             : 
    1795             :     Assert(conn != NULL);
    1796             : 
    1797             :     /*
    1798             :      * Check server version. BASE_BACKUP command was introduced in 9.1, so we
    1799             :      * can't work with servers older than 9.1.
    1800             :      */
    1801         146 :     minServerMajor = 901;
    1802         146 :     maxServerMajor = PG_VERSION_NUM / 100;
    1803         146 :     serverVersion = PQserverVersion(conn);
    1804         146 :     serverMajor = serverVersion / 100;
    1805         146 :     if (serverMajor < minServerMajor || serverMajor > maxServerMajor)
    1806             :     {
    1807           0 :         const char *serverver = PQparameterStatus(conn, "server_version");
    1808             : 
    1809           0 :         pg_log_error("incompatible server version %s",
    1810             :                      serverver ? serverver : "'unknown'");
    1811           0 :         exit(1);
    1812             :     }
    1813             : 
    1814             :     /*
    1815             :      * If WAL streaming was requested, also check that the server is new
    1816             :      * enough for that.
    1817             :      */
    1818         146 :     if (includewal == STREAM_WAL && !CheckServerVersionForStreaming(conn))
    1819             :     {
    1820             :         /*
    1821             :          * Error message already written in CheckServerVersionForStreaming(),
    1822             :          * but add a hint about using -X none.
    1823             :          */
    1824           0 :         pg_log_info("HINT: use -X none or -X fetch to disable log streaming");
    1825           0 :         exit(1);
    1826             :     }
    1827             : 
    1828             :     /*
    1829             :      * Build contents of configuration file if requested
    1830             :      */
    1831         146 :     if (writerecoveryconf)
    1832           4 :         recoveryconfcontents = GenerateRecoveryConfig(conn, replication_slot);
    1833             : 
    1834             :     /*
    1835             :      * Run IDENTIFY_SYSTEM so we can get the timeline
    1836             :      */
    1837         146 :     if (!RunIdentifySystem(conn, &sysidentifier, &latesttli, NULL, NULL))
    1838           0 :         exit(1);
    1839             : 
    1840             :     /*
    1841             :      * Start the actual backup
    1842             :      */
    1843         146 :     PQescapeStringConn(conn, escaped_label, label, sizeof(escaped_label), &i);
    1844             : 
    1845         146 :     if (maxrate > 0)
    1846           0 :         maxrate_clause = psprintf("MAX_RATE %u", maxrate);
    1847             : 
    1848         146 :     if (manifest)
    1849             :     {
    1850         144 :         if (manifest_force_encode)
    1851           2 :             manifest_clause = "MANIFEST 'force-encode'";
    1852             :         else
    1853         142 :             manifest_clause = "MANIFEST 'yes'";
    1854         144 :         if (manifest_checksums != NULL)
    1855          14 :             manifest_checksums_clause = psprintf("MANIFEST_CHECKSUMS '%s'",
    1856             :                                                  manifest_checksums);
    1857             :     }
    1858             : 
    1859         146 :     if (verbose)
    1860           0 :         pg_log_info("initiating base backup, waiting for checkpoint to complete");
    1861             : 
    1862         146 :     if (showprogress && !verbose)
    1863             :     {
    1864           0 :         fprintf(stderr, "waiting for checkpoint");
    1865           0 :         if (isatty(fileno(stderr)))
    1866           0 :             fprintf(stderr, "\r");
    1867             :         else
    1868           0 :             fprintf(stderr, "\n");
    1869             :     }
    1870             : 
    1871             :     basebkp =
    1872        1022 :         psprintf("BASE_BACKUP LABEL '%s' %s %s %s %s %s %s %s %s %s",
    1873             :                  escaped_label,
    1874         146 :                  estimatesize ? "PROGRESS" : "",
    1875         146 :                  includewal == FETCH_WAL ? "WAL" : "",
    1876         146 :                  fastcheckpoint ? "FAST" : "",
    1877         146 :                  includewal == NO_WAL ? "" : "NOWAIT",
    1878             :                  maxrate_clause ? maxrate_clause : "",
    1879         146 :                  format == 't' ? "TABLESPACE_MAP" : "",
    1880         146 :                  verify_checksums ? "" : "NOVERIFY_CHECKSUMS",
    1881             :                  manifest_clause ? manifest_clause : "",
    1882             :                  manifest_checksums_clause);
    1883             : 
    1884         146 :     if (PQsendQuery(conn, basebkp) == 0)
    1885             :     {
    1886           0 :         pg_log_error("could not send replication command \"%s\": %s",
    1887             :                      "BASE_BACKUP", PQerrorMessage(conn));
    1888           0 :         exit(1);
    1889             :     }
    1890             : 
    1891             :     /*
    1892             :      * Get the starting WAL location
    1893             :      */
    1894         146 :     res = PQgetResult(conn);
    1895         146 :     if (PQresultStatus(res) != PGRES_TUPLES_OK)
    1896             :     {
    1897           2 :         pg_log_error("could not initiate base backup: %s",
    1898             :                      PQerrorMessage(conn));
    1899           2 :         exit(1);
    1900             :     }
    1901         144 :     if (PQntuples(res) != 1)
    1902             :     {
    1903           0 :         pg_log_error("server returned unexpected response to BASE_BACKUP command; got %d rows and %d fields, expected %d rows and %d fields",
    1904             :                      PQntuples(res), PQnfields(res), 1, 2);
    1905           0 :         exit(1);
    1906             :     }
    1907             : 
    1908         144 :     strlcpy(xlogstart, PQgetvalue(res, 0, 0), sizeof(xlogstart));
    1909             : 
    1910         144 :     if (verbose)
    1911           0 :         pg_log_info("checkpoint completed");
    1912             : 
    1913             :     /*
    1914             :      * 9.3 and later sends the TLI of the starting point. With older servers,
    1915             :      * assume it's the same as the latest timeline reported by
    1916             :      * IDENTIFY_SYSTEM.
    1917             :      */
    1918         144 :     if (PQnfields(res) >= 2)
    1919         144 :         starttli = atoi(PQgetvalue(res, 0, 1));
    1920             :     else
    1921           0 :         starttli = latesttli;
    1922         144 :     PQclear(res);
    1923        1296 :     MemSet(xlogend, 0, sizeof(xlogend));
    1924             : 
    1925         144 :     if (verbose && includewal != NO_WAL)
    1926           0 :         pg_log_info("write-ahead log start point: %s on timeline %u",
    1927             :                     xlogstart, starttli);
    1928             : 
    1929             :     /*
    1930             :      * Get the header
    1931             :      */
    1932         144 :     res = PQgetResult(conn);
    1933         144 :     if (PQresultStatus(res) != PGRES_TUPLES_OK)
    1934             :     {
    1935           0 :         pg_log_error("could not get backup header: %s",
    1936             :                      PQerrorMessage(conn));
    1937           0 :         exit(1);
    1938             :     }
    1939         144 :     if (PQntuples(res) < 1)
    1940             :     {
    1941           0 :         pg_log_error("no data returned from server");
    1942           0 :         exit(1);
    1943             :     }
    1944             : 
    1945             :     /*
    1946             :      * Sum up the total size, for progress reporting
    1947             :      */
    1948         144 :     totalsize_kb = totaldone = 0;
    1949         144 :     tablespacecount = PQntuples(res);
    1950         316 :     for (i = 0; i < PQntuples(res); i++)
    1951             :     {
    1952         174 :         totalsize_kb += atol(PQgetvalue(res, i, 2));
    1953             : 
    1954             :         /*
    1955             :          * Verify tablespace directories are empty. Don't bother with the
    1956             :          * first once since it can be relocated, and it will be checked before
    1957             :          * we do anything anyway.
    1958             :          */
    1959         174 :         if (format == 'p' && !PQgetisnull(res, i, 1))
    1960             :         {
    1961          28 :             char       *path = unconstify(char *, get_tablespace_mapping(PQgetvalue(res, i, 1)));
    1962             : 
    1963          28 :             verify_dir_is_empty_or_create(path, &made_tablespace_dirs, &found_tablespace_dirs);
    1964             :         }
    1965             :     }
    1966             : 
    1967             :     /*
    1968             :      * When writing to stdout, require a single tablespace
    1969             :      */
    1970         142 :     writing_to_stdout = format == 't' && strcmp(basedir, "-") == 0;
    1971         142 :     if (writing_to_stdout && PQntuples(res) > 1)
    1972             :     {
    1973           0 :         pg_log_error("can only write single tablespace to stdout, database has %d",
    1974             :                      PQntuples(res));
    1975           0 :         exit(1);
    1976             :     }
    1977             : 
    1978             :     /*
    1979             :      * If we're streaming WAL, start the streaming session before we start
    1980             :      * receiving the actual data chunks.
    1981             :      */
    1982         142 :     if (includewal == STREAM_WAL)
    1983             :     {
    1984         138 :         if (verbose)
    1985           0 :             pg_log_info("starting background WAL receiver");
    1986         138 :         StartLogStreamer(xlogstart, starttli, sysidentifier);
    1987             :     }
    1988             : 
    1989             :     /*
    1990             :      * Start receiving chunks
    1991             :      */
    1992         310 :     for (i = 0; i < PQntuples(res); i++)
    1993             :     {
    1994         170 :         if (format == 't')
    1995          14 :             ReceiveTarFile(conn, res, i);
    1996             :         else
    1997         156 :             ReceiveAndUnpackTarFile(conn, res, i);
    1998             :     }                           /* Loop over all tablespaces */
    1999             : 
    2000             :     /*
    2001             :      * Now receive backup manifest, if appropriate.
    2002             :      *
    2003             :      * If we're writing a tarfile to stdout, ReceiveTarFile will have already
    2004             :      * processed the backup manifest and included it in the output tarfile.
    2005             :      * Such a configuration doesn't allow for writing multiple files.
    2006             :      *
    2007             :      * If we're talking to an older server, it won't send a backup manifest,
    2008             :      * so don't try to receive one.
    2009             :      */
    2010         140 :     if (!writing_to_stdout && manifest)
    2011         138 :         ReceiveBackupManifest(conn);
    2012             : 
    2013         138 :     if (showprogress)
    2014             :     {
    2015           0 :         progress_report(PQntuples(res), NULL, true);
    2016           0 :         if (isatty(fileno(stderr)))
    2017           0 :             fprintf(stderr, "\n");    /* Need to move to next line */
    2018             :     }
    2019             : 
    2020         138 :     PQclear(res);
    2021             : 
    2022             :     /*
    2023             :      * Get the stop position
    2024             :      */
    2025         138 :     res = PQgetResult(conn);
    2026         138 :     if (PQresultStatus(res) != PGRES_TUPLES_OK)
    2027             :     {
    2028           0 :         pg_log_error("could not get write-ahead log end position from server: %s",
    2029             :                      PQerrorMessage(conn));
    2030           0 :         exit(1);
    2031             :     }
    2032         138 :     if (PQntuples(res) != 1)
    2033             :     {
    2034           0 :         pg_log_error("no write-ahead log end position returned from server");
    2035           0 :         exit(1);
    2036             :     }
    2037         138 :     strlcpy(xlogend, PQgetvalue(res, 0, 0), sizeof(xlogend));
    2038         138 :     if (verbose && includewal != NO_WAL)
    2039           0 :         pg_log_info("write-ahead log end point: %s", xlogend);
    2040         138 :     PQclear(res);
    2041             : 
    2042         138 :     res = PQgetResult(conn);
    2043         138 :     if (PQresultStatus(res) != PGRES_COMMAND_OK)
    2044             :     {
    2045           6 :         const char *sqlstate = PQresultErrorField(res, PG_DIAG_SQLSTATE);
    2046             : 
    2047           6 :         if (sqlstate &&
    2048           6 :             strcmp(sqlstate, ERRCODE_DATA_CORRUPTED) == 0)
    2049             :         {
    2050           6 :             pg_log_error("checksum error occurred");
    2051           6 :             checksum_failure = true;
    2052             :         }
    2053             :         else
    2054             :         {
    2055           0 :             pg_log_error("final receive failed: %s",
    2056             :                          PQerrorMessage(conn));
    2057             :         }
    2058           6 :         exit(1);
    2059             :     }
    2060             : 
    2061         132 :     if (bgchild > 0)
    2062             :     {
    2063             : #ifndef WIN32
    2064             :         int         status;
    2065             :         pid_t       r;
    2066             : #else
    2067             :         DWORD       status;
    2068             : 
    2069             :         /*
    2070             :          * get a pointer sized version of bgchild to avoid warnings about
    2071             :          * casting to a different size on WIN64.
    2072             :          */
    2073             :         intptr_t    bgchild_handle = bgchild;
    2074             :         uint32      hi,
    2075             :                     lo;
    2076             : #endif
    2077             : 
    2078         128 :         if (verbose)
    2079           0 :             pg_log_info("waiting for background process to finish streaming ...");
    2080             : 
    2081             : #ifndef WIN32
    2082         128 :         if (write(bgpipe[1], xlogend, strlen(xlogend)) != strlen(xlogend))
    2083             :         {
    2084           0 :             pg_log_info("could not send command to background pipe: %m");
    2085           0 :             exit(1);
    2086             :         }
    2087             : 
    2088             :         /* Just wait for the background process to exit */
    2089         128 :         r = waitpid(bgchild, &status, 0);
    2090         128 :         if (r == (pid_t) -1)
    2091             :         {
    2092           0 :             pg_log_error("could not wait for child process: %m");
    2093           0 :             exit(1);
    2094             :         }
    2095         128 :         if (r != bgchild)
    2096             :         {
    2097           0 :             pg_log_error("child %d died, expected %d", (int) r, (int) bgchild);
    2098           0 :             exit(1);
    2099             :         }
    2100         128 :         if (status != 0)
    2101             :         {
    2102           2 :             pg_log_error("%s", wait_result_to_str(status));
    2103           2 :             exit(1);
    2104             :         }
    2105             :         /* Exited normally, we're happy! */
    2106             : #else                           /* WIN32 */
    2107             : 
    2108             :         /*
    2109             :          * On Windows, since we are in the same process, we can just store the
    2110             :          * value directly in the variable, and then set the flag that says
    2111             :          * it's there.
    2112             :          */
    2113             :         if (sscanf(xlogend, "%X/%X", &hi, &lo) != 2)
    2114             :         {
    2115             :             pg_log_error("could not parse write-ahead log location \"%s\"",
    2116             :                          xlogend);
    2117             :             exit(1);
    2118             :         }
    2119             :         xlogendptr = ((uint64) hi) << 32 | lo;
    2120             :         InterlockedIncrement(&has_xlogendptr);
    2121             : 
    2122             :         /* First wait for the thread to exit */
    2123             :         if (WaitForSingleObjectEx((HANDLE) bgchild_handle, INFINITE, FALSE) !=
    2124             :             WAIT_OBJECT_0)
    2125             :         {
    2126             :             _dosmaperr(GetLastError());
    2127             :             pg_log_error("could not wait for child thread: %m");
    2128             :             exit(1);
    2129             :         }
    2130             :         if (GetExitCodeThread((HANDLE) bgchild_handle, &status) == 0)
    2131             :         {
    2132             :             _dosmaperr(GetLastError());
    2133             :             pg_log_error("could not get child thread exit status: %m");
    2134             :             exit(1);
    2135             :         }
    2136             :         if (status != 0)
    2137             :         {
    2138             :             pg_log_error("child thread exited with error %u",
    2139             :                          (unsigned int) status);
    2140             :             exit(1);
    2141             :         }
    2142             :         /* Exited normally, we're happy */
    2143             : #endif
    2144             :     }
    2145             : 
    2146             :     /* Free the configuration file contents */
    2147         130 :     destroyPQExpBuffer(recoveryconfcontents);
    2148             : 
    2149             :     /*
    2150             :      * End of copy data. Final result is already checked inside the loop.
    2151             :      */
    2152         130 :     PQclear(res);
    2153         130 :     PQfinish(conn);
    2154         130 :     conn = NULL;
    2155             : 
    2156             :     /*
    2157             :      * Make data persistent on disk once backup is completed. For tar format
    2158             :      * sync the parent directory and all its contents as each tar file was not
    2159             :      * synced after being completed.  In plain format, all the data of the
    2160             :      * base directory is synced, taking into account all the tablespaces.
    2161             :      * Errors are not considered fatal.
    2162             :      */
    2163         130 :     if (do_sync)
    2164             :     {
    2165          34 :         if (verbose)
    2166           0 :             pg_log_info("syncing data to disk ...");
    2167          34 :         if (format == 't')
    2168             :         {
    2169           8 :             if (strcmp(basedir, "-") != 0)
    2170           8 :                 (void) fsync_dir_recurse(basedir);
    2171             :         }
    2172             :         else
    2173             :         {
    2174          26 :             (void) fsync_pgdata(basedir, serverVersion);
    2175             :         }
    2176             :     }
    2177             : 
    2178             :     /*
    2179             :      * After synchronizing data to disk, perform a durable rename of
    2180             :      * backup_manifest.tmp to backup_manifest, if we wrote such a file. This
    2181             :      * way, a failure or system crash before we reach this point will leave us
    2182             :      * without a backup_manifest file, decreasing the chances that a directory
    2183             :      * we leave behind will be mistaken for a valid backup.
    2184             :      */
    2185         130 :     if (!writing_to_stdout && manifest)
    2186             :     {
    2187             :         char        tmp_filename[MAXPGPATH];
    2188             :         char        filename[MAXPGPATH];
    2189             : 
    2190         128 :         if (verbose)
    2191           0 :             pg_log_info("renaming backup_manifest.tmp to backup_manifest");
    2192             : 
    2193         128 :         snprintf(tmp_filename, MAXPGPATH, "%s/backup_manifest.tmp", basedir);
    2194         128 :         snprintf(filename, MAXPGPATH, "%s/backup_manifest", basedir);
    2195             : 
    2196             :         /* durable_rename emits its own log message in case of failure */
    2197         128 :         if (durable_rename(tmp_filename, filename) != 0)
    2198           0 :             exit(1);
    2199             :     }
    2200             : 
    2201         130 :     if (verbose)
    2202           0 :         pg_log_info("base backup completed");
    2203         130 : }
    2204             : 
    2205             : 
    2206             : int
    2207         176 : main(int argc, char **argv)
    2208             : {
    2209             :     static struct option long_options[] = {
    2210             :         {"help", no_argument, NULL, '?'},
    2211             :         {"version", no_argument, NULL, 'V'},
    2212             :         {"pgdata", required_argument, NULL, 'D'},
    2213             :         {"format", required_argument, NULL, 'F'},
    2214             :         {"checkpoint", required_argument, NULL, 'c'},
    2215             :         {"create-slot", no_argument, NULL, 'C'},
    2216             :         {"max-rate", required_argument, NULL, 'r'},
    2217             :         {"write-recovery-conf", no_argument, NULL, 'R'},
    2218             :         {"slot", required_argument, NULL, 'S'},
    2219             :         {"tablespace-mapping", required_argument, NULL, 'T'},
    2220             :         {"wal-method", required_argument, NULL, 'X'},
    2221             :         {"gzip", no_argument, NULL, 'z'},
    2222             :         {"compress", required_argument, NULL, 'Z'},
    2223             :         {"label", required_argument, NULL, 'l'},
    2224             :         {"no-clean", no_argument, NULL, 'n'},
    2225             :         {"no-sync", no_argument, NULL, 'N'},
    2226             :         {"dbname", required_argument, NULL, 'd'},
    2227             :         {"host", required_argument, NULL, 'h'},
    2228             :         {"port", required_argument, NULL, 'p'},
    2229             :         {"username", required_argument, NULL, 'U'},
    2230             :         {"no-password", no_argument, NULL, 'w'},
    2231             :         {"password", no_argument, NULL, 'W'},
    2232             :         {"status-interval", required_argument, NULL, 's'},
    2233             :         {"verbose", no_argument, NULL, 'v'},
    2234             :         {"progress", no_argument, NULL, 'P'},
    2235             :         {"waldir", required_argument, NULL, 1},
    2236             :         {"no-slot", no_argument, NULL, 2},
    2237             :         {"no-verify-checksums", no_argument, NULL, 3},
    2238             :         {"no-estimate-size", no_argument, NULL, 4},
    2239             :         {"no-manifest", no_argument, NULL, 5},
    2240             :         {"manifest-force-encode", no_argument, NULL, 6},
    2241             :         {"manifest-checksums", required_argument, NULL, 7},
    2242             :         {NULL, 0, NULL, 0}
    2243             :     };
    2244             :     int         c;
    2245             : 
    2246             :     int         option_index;
    2247             : 
    2248         176 :     pg_logging_init(argv[0]);
    2249         176 :     progname = get_progname(argv[0]);
    2250         176 :     set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_basebackup"));
    2251             : 
    2252         176 :     if (argc > 1)
    2253             :     {
    2254         174 :         if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
    2255             :         {
    2256           2 :             usage();
    2257           2 :             exit(0);
    2258             :         }
    2259         172 :         else if (strcmp(argv[1], "-V") == 0
    2260         172 :                  || strcmp(argv[1], "--version") == 0)
    2261             :         {
    2262           2 :             puts("pg_basebackup (PostgreSQL) " PG_VERSION);
    2263           2 :             exit(0);
    2264             :         }
    2265             :     }
    2266             : 
    2267         172 :     atexit(cleanup_directories_atexit);
    2268             : 
    2269         676 :     while ((c = getopt_long(argc, argv, "CD:F:r:RS:T:X:l:nNzZ:d:c:h:p:U:s:wWkvP",
    2270             :                             long_options, &option_index)) != -1)
    2271             :     {
    2272         518 :         switch (c)
    2273             :         {
    2274           8 :             case 'C':
    2275           8 :                 create_slot = true;
    2276           8 :                 break;
    2277         168 :             case 'D':
    2278         168 :                 basedir = pg_strdup(optarg);
    2279         168 :                 break;
    2280          28 :             case 'F':
    2281          28 :                 if (strcmp(optarg, "p") == 0 || strcmp(optarg, "plain") == 0)
    2282          18 :                     format = 'p';
    2283          10 :                 else if (strcmp(optarg, "t") == 0 || strcmp(optarg, "tar") == 0)
    2284          10 :                     format = 't';
    2285             :                 else
    2286             :                 {
    2287           0 :                     pg_log_error("invalid output format \"%s\", must be \"plain\" or \"tar\"",
    2288             :                                  optarg);
    2289           0 :                     exit(1);
    2290             :                 }
    2291          28 :                 break;
    2292           0 :             case 'r':
    2293           0 :                 maxrate = parse_max_rate(optarg);
    2294           0 :                 break;
    2295           4 :             case 'R':
    2296           4 :                 writerecoveryconf = true;
    2297           4 :                 break;
    2298          14 :             case 'S':
    2299             : 
    2300             :                 /*
    2301             :                  * When specifying replication slot name, use a permanent
    2302             :                  * slot.
    2303             :                  */
    2304          14 :                 replication_slot = pg_strdup(optarg);
    2305          14 :                 temp_replication_slot = false;
    2306          14 :                 break;
    2307           4 :             case 2:
    2308           4 :                 no_slot = true;
    2309           4 :                 break;
    2310          38 :             case 'T':
    2311          38 :                 tablespace_list_append(optarg);
    2312          26 :                 break;
    2313          18 :             case 'X':
    2314          18 :                 if (strcmp(optarg, "n") == 0 ||
    2315          18 :                     strcmp(optarg, "none") == 0)
    2316             :                 {
    2317           4 :                     includewal = NO_WAL;
    2318             :                 }
    2319          14 :                 else if (strcmp(optarg, "f") == 0 ||
    2320          14 :                          strcmp(optarg, "fetch") == 0)
    2321             :                 {
    2322           2 :                     includewal = FETCH_WAL;
    2323             :                 }
    2324          12 :                 else if (strcmp(optarg, "s") == 0 ||
    2325          12 :                          strcmp(optarg, "stream") == 0)
    2326             :                 {
    2327          12 :                     includewal = STREAM_WAL;
    2328             :                 }
    2329             :                 else
    2330             :                 {
    2331           0 :                     pg_log_error("invalid wal-method option \"%s\", must be \"fetch\", \"stream\", or \"none\"",
    2332             :                                  optarg);
    2333           0 :                     exit(1);
    2334             :                 }
    2335          18 :                 break;
    2336           2 :             case 1:
    2337           2 :                 xlog_dir = pg_strdup(optarg);
    2338           2 :                 break;
    2339           0 :             case 'l':
    2340           0 :                 label = pg_strdup(optarg);
    2341           0 :                 break;
    2342           2 :             case 'n':
    2343           2 :                 noclean = true;
    2344           2 :                 break;
    2345          98 :             case 'N':
    2346          98 :                 do_sync = false;
    2347          98 :                 break;
    2348           0 :             case 'z':
    2349             : #ifdef HAVE_LIBZ
    2350           0 :                 compresslevel = Z_DEFAULT_COMPRESSION;
    2351             : #else
    2352             :                 compresslevel = 1;  /* will be rejected below */
    2353             : #endif
    2354           0 :                 break;
    2355           0 :             case 'Z':
    2356           0 :                 compresslevel = atoi(optarg);
    2357           0 :                 if (compresslevel < 0 || compresslevel > 9)
    2358             :                 {
    2359           0 :                     pg_log_error("invalid compression level \"%s\"", optarg);
    2360           0 :                     exit(1);
    2361             :                 }
    2362           0 :                 break;
    2363           0 :             case 'c':
    2364           0 :                 if (pg_strcasecmp(optarg, "fast") == 0)
    2365           0 :                     fastcheckpoint = true;
    2366           0 :                 else if (pg_strcasecmp(optarg, "spread") == 0)
    2367           0 :                     fastcheckpoint = false;
    2368             :                 else
    2369             :                 {
    2370           0 :                     pg_log_error("invalid checkpoint argument \"%s\", must be \"fast\" or \"spread\"",
    2371             :                                  optarg);
    2372           0 :                     exit(1);
    2373             :                 }
    2374           0 :                 break;
    2375           0 :             case 'd':
    2376           0 :                 connection_string = pg_strdup(optarg);
    2377           0 :                 break;
    2378          56 :             case 'h':
    2379          56 :                 dbhost = pg_strdup(optarg);
    2380          56 :                 break;
    2381          56 :             case 'p':
    2382          56 :                 dbport = pg_strdup(optarg);
    2383          56 :                 break;
    2384           0 :             case 'U':
    2385           0 :                 dbuser = pg_strdup(optarg);
    2386           0 :                 break;
    2387           0 :             case 'w':
    2388           0 :                 dbgetpassword = -1;
    2389           0 :                 break;
    2390           0 :             case 'W':
    2391           0 :                 dbgetpassword = 1;
    2392           0 :                 break;
    2393           0 :             case 's':
    2394           0 :                 standby_message_timeout = atoi(optarg) * 1000;
    2395           0 :                 if (standby_message_timeout < 0)
    2396             :                 {
    2397           0 :                     pg_log_error("invalid status interval \"%s\"", optarg);
    2398           0 :                     exit(1);
    2399             :                 }
    2400           0 :                 break;
    2401           0 :             case 'v':
    2402           0 :                 verbose++;
    2403           0 :                 break;
    2404           0 :             case 'P':
    2405           0 :                 showprogress = true;
    2406           0 :                 break;
    2407           2 :             case 3:
    2408           2 :                 verify_checksums = false;
    2409           2 :                 break;
    2410           0 :             case 4:
    2411           0 :                 estimatesize = false;
    2412           0 :                 break;
    2413           2 :             case 5:
    2414           2 :                 manifest = false;
    2415           2 :                 break;
    2416           2 :             case 6:
    2417           2 :                 manifest_force_encode = true;
    2418           2 :                 break;
    2419          14 :             case 7:
    2420          14 :                 manifest_checksums = pg_strdup(optarg);
    2421          14 :                 break;
    2422           2 :             default:
    2423             : 
    2424             :                 /*
    2425             :                  * getopt_long already emitted a complaint
    2426             :                  */
    2427           2 :                 fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
    2428             :                         progname);
    2429           2 :                 exit(1);
    2430             :         }
    2431             :     }
    2432             : 
    2433             :     /*
    2434             :      * Any non-option arguments?
    2435             :      */
    2436         158 :     if (optind < argc)
    2437             :     {
    2438           0 :         pg_log_error("too many command-line arguments (first is \"%s\")",
    2439             :                      argv[optind]);
    2440           0 :         fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
    2441             :                 progname);
    2442           0 :         exit(1);
    2443             :     }
    2444             : 
    2445             :     /*
    2446             :      * Required arguments
    2447             :      */
    2448         158 :     if (basedir == NULL)
    2449             :     {
    2450           2 :         pg_log_error("no target directory specified");
    2451           2 :         fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
    2452             :                 progname);
    2453           2 :         exit(1);
    2454             :     }
    2455             : 
    2456             :     /*
    2457             :      * Mutually exclusive arguments
    2458             :      */
    2459         156 :     if (format == 'p' && compresslevel != 0)
    2460             :     {
    2461           0 :         pg_log_error("only tar mode backups can be compressed");
    2462           0 :         fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
    2463             :                 progname);
    2464           0 :         exit(1);
    2465             :     }
    2466             : 
    2467         156 :     if (format == 't' && includewal == STREAM_WAL && strcmp(basedir, "-") == 0)
    2468             :     {
    2469           0 :         pg_log_error("cannot stream write-ahead logs in tar mode to stdout");
    2470           0 :         fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
    2471             :                 progname);
    2472           0 :         exit(1);
    2473             :     }
    2474             : 
    2475         156 :     if (replication_slot && includewal != STREAM_WAL)
    2476             :     {
    2477           2 :         pg_log_error("replication slots can only be used with WAL streaming");
    2478           2 :         fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
    2479             :                 progname);
    2480           2 :         exit(1);
    2481             :     }
    2482             : 
    2483         154 :     if (no_slot)
    2484             :     {
    2485           4 :         if (replication_slot)
    2486             :         {
    2487           2 :             pg_log_error("--no-slot cannot be used with slot name");
    2488           2 :             fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
    2489             :                     progname);
    2490           2 :             exit(1);
    2491             :         }
    2492           2 :         temp_replication_slot = false;
    2493             :     }
    2494             : 
    2495         152 :     if (create_slot)
    2496             :     {
    2497           6 :         if (!replication_slot)
    2498             :         {
    2499           2 :             pg_log_error("%s needs a slot to be specified using --slot",
    2500             :                          "--create-slot");
    2501           2 :             fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
    2502             :                     progname);
    2503           2 :             exit(1);
    2504             :         }
    2505             : 
    2506           4 :         if (no_slot)
    2507             :         {
    2508           0 :             pg_log_error("--create-slot and --no-slot are incompatible options");
    2509           0 :             fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
    2510             :                     progname);
    2511           0 :             exit(1);
    2512             :         }
    2513             :     }
    2514             : 
    2515         150 :     if (xlog_dir)
    2516             :     {
    2517           2 :         if (format != 'p')
    2518             :         {
    2519           0 :             pg_log_error("WAL directory location can only be specified in plain mode");
    2520           0 :             fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
    2521             :                     progname);
    2522           0 :             exit(1);
    2523             :         }
    2524             : 
    2525             :         /* clean up xlog directory name, check it's absolute */
    2526           2 :         canonicalize_path(xlog_dir);
    2527           2 :         if (!is_absolute_path(xlog_dir))
    2528             :         {
    2529           0 :             pg_log_error("WAL directory location must be an absolute path");
    2530           0 :             fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
    2531             :                     progname);
    2532           0 :             exit(1);
    2533             :         }
    2534             :     }
    2535             : 
    2536             : #ifndef HAVE_LIBZ
    2537             :     if (compresslevel != 0)
    2538             :     {
    2539             :         pg_log_error("this build does not support compression");
    2540             :         exit(1);
    2541             :     }
    2542             : #endif
    2543             : 
    2544         150 :     if (showprogress && !estimatesize)
    2545             :     {
    2546           0 :         pg_log_error("--progress and --no-estimate-size are incompatible options");
    2547           0 :         fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
    2548             :                 progname);
    2549           0 :         exit(1);
    2550             :     }
    2551             : 
    2552         150 :     if (!manifest && manifest_checksums != NULL)
    2553             :     {
    2554           0 :         pg_log_error("--no-manifest and --manifest-checksums are incompatible options");
    2555           0 :         fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
    2556             :                 progname);
    2557           0 :         exit(1);
    2558             :     }
    2559             : 
    2560         150 :     if (!manifest && manifest_force_encode)
    2561             :     {
    2562           0 :         pg_log_error("--no-manifest and --manifest-force-encode are incompatible options");
    2563           0 :         fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
    2564             :                 progname);
    2565           0 :         exit(1);
    2566             :     }
    2567             : 
    2568             :     /* connection in replication mode to server */
    2569         150 :     conn = GetConnection();
    2570         150 :     if (!conn)
    2571             :     {
    2572             :         /* Error message already written in GetConnection() */
    2573           4 :         exit(1);
    2574             :     }
    2575         146 :     atexit(disconnect_atexit);
    2576             : 
    2577             :     /*
    2578             :      * Set umask so that directories/files are created with the same
    2579             :      * permissions as directories/files in the source data directory.
    2580             :      *
    2581             :      * pg_mode_mask is set to owner-only by default and then updated in
    2582             :      * GetConnection() where we get the mode from the server-side with
    2583             :      * RetrieveDataDirCreatePerm() and then call SetDataDirectoryCreatePerm().
    2584             :      */
    2585         146 :     umask(pg_mode_mask);
    2586             : 
    2587             :     /* Backup manifests are supported in 13 and newer versions */
    2588         146 :     if (PQserverVersion(conn) < MINIMUM_VERSION_FOR_MANIFESTS)
    2589           0 :         manifest = false;
    2590             : 
    2591             :     /*
    2592             :      * Verify that the target directory exists, or create it. For plaintext
    2593             :      * backups, always require the directory. For tar backups, require it
    2594             :      * unless we are writing to stdout.
    2595             :      */
    2596         146 :     if (format == 'p' || strcmp(basedir, "-") != 0)
    2597         146 :         verify_dir_is_empty_or_create(basedir, &made_new_pgdata, &found_existing_pgdata);
    2598             : 
    2599             :     /* determine remote server's xlog segment size */
    2600         146 :     if (!RetrieveWalSegSize(conn))
    2601           0 :         exit(1);
    2602             : 
    2603             :     /* Create pg_wal symlink, if required */
    2604         146 :     if (xlog_dir)
    2605             :     {
    2606             :         char       *linkloc;
    2607             : 
    2608           2 :         verify_dir_is_empty_or_create(xlog_dir, &made_new_xlogdir, &found_existing_xlogdir);
    2609             : 
    2610             :         /*
    2611             :          * Form name of the place where the symlink must go. pg_xlog has been
    2612             :          * renamed to pg_wal in post-10 clusters.
    2613             :          */
    2614           2 :         linkloc = psprintf("%s/%s", basedir,
    2615           2 :                            PQserverVersion(conn) < MINIMUM_VERSION_FOR_PG_WAL ?
    2616             :                            "pg_xlog" : "pg_wal");
    2617             : 
    2618             : #ifdef HAVE_SYMLINK
    2619           2 :         if (symlink(xlog_dir, linkloc) != 0)
    2620             :         {
    2621           0 :             pg_log_error("could not create symbolic link \"%s\": %m", linkloc);
    2622           0 :             exit(1);
    2623             :         }
    2624             : #else
    2625             :         pg_log_error("symlinks are not supported on this platform");
    2626             :         exit(1);
    2627             : #endif
    2628           2 :         free(linkloc);
    2629             :     }
    2630             : 
    2631         146 :     BaseBackup();
    2632             : 
    2633         130 :     success = true;
    2634         130 :     return 0;
    2635             : }

Generated by: LCOV version 1.13