LCOV - code coverage report
Current view: top level - src/common - archive.c (source / functions) Hit Total Coverage
Test: PostgreSQL 14devel Lines: 20 38 52.6 %
Date: 2020-12-05 18:06:08 Functions: 1 1 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * archive.c
       4             :  *    Common WAL archive routines
       5             :  *
       6             :  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
       7             :  * Portions Copyright (c) 1994, Regents of the University of California
       8             :  *
       9             :  *
      10             :  * IDENTIFICATION
      11             :  *    src/common/archive.c
      12             :  *
      13             :  *-------------------------------------------------------------------------
      14             :  */
      15             : 
      16             : #ifndef FRONTEND
      17             : #include "postgres.h"
      18             : #else
      19             : #include "postgres_fe.h"
      20             : #endif
      21             : 
      22             : #include "common/archive.h"
      23             : #include "lib/stringinfo.h"
      24             : 
      25             : /*
      26             :  * BuildRestoreCommand
      27             :  *
      28             :  * Builds a restore command to retrieve a file from WAL archives, replacing
      29             :  * the supported aliases with values supplied by the caller as defined by
      30             :  * the GUC parameter restore_command: xlogpath for %p, xlogfname for %f and
      31             :  * lastRestartPointFname for %r.
      32             :  *
      33             :  * The result is a palloc'd string for the restore command built.  The
      34             :  * caller is responsible for freeing it.  If any of the required arguments
      35             :  * is NULL and that the corresponding alias is found in the command given
      36             :  * by the caller, then NULL is returned.
      37             :  */
      38             : char *
      39         118 : BuildRestoreCommand(const char *restoreCommand,
      40             :                     const char *xlogpath,
      41             :                     const char *xlogfname,
      42             :                     const char *lastRestartPointFname)
      43             : {
      44             :     StringInfoData result;
      45             :     const char *sp;
      46             : 
      47             :     /*
      48             :      * Build the command to be executed.
      49             :      */
      50         118 :     initStringInfo(&result);
      51             : 
      52       12676 :     for (sp = restoreCommand; *sp; sp++)
      53             :     {
      54       12558 :         if (*sp == '%')
      55             :         {
      56         236 :             switch (sp[1])
      57             :             {
      58         118 :                 case 'p':
      59             :                     {
      60             :                         char       *nativePath;
      61             : 
      62             :                         /* %p: relative path of target file */
      63         118 :                         if (xlogpath == NULL)
      64             :                         {
      65           0 :                             pfree(result.data);
      66           0 :                             return NULL;
      67             :                         }
      68         118 :                         sp++;
      69             : 
      70             :                         /*
      71             :                          * This needs to use a placeholder to not modify the
      72             :                          * input with the conversion done via
      73             :                          * make_native_path().
      74             :                          */
      75         118 :                         nativePath = pstrdup(xlogpath);
      76         118 :                         make_native_path(nativePath);
      77         118 :                         appendStringInfoString(&result,
      78             :                                                nativePath);
      79         118 :                         pfree(nativePath);
      80         118 :                         break;
      81             :                     }
      82         118 :                 case 'f':
      83             :                     /* %f: filename of desired file */
      84         118 :                     if (xlogfname == NULL)
      85             :                     {
      86           0 :                         pfree(result.data);
      87           0 :                         return NULL;
      88             :                     }
      89         118 :                     sp++;
      90         118 :                     appendStringInfoString(&result, xlogfname);
      91         118 :                     break;
      92           0 :                 case 'r':
      93             :                     /* %r: filename of last restartpoint */
      94           0 :                     if (lastRestartPointFname == NULL)
      95             :                     {
      96           0 :                         pfree(result.data);
      97           0 :                         return NULL;
      98             :                     }
      99           0 :                     sp++;
     100           0 :                     appendStringInfoString(&result,
     101             :                                            lastRestartPointFname);
     102           0 :                     break;
     103           0 :                 case '%':
     104             :                     /* convert %% to a single % */
     105           0 :                     sp++;
     106           0 :                     appendStringInfoChar(&result, *sp);
     107           0 :                     break;
     108           0 :                 default:
     109             :                     /* otherwise treat the % as not special */
     110           0 :                     appendStringInfoChar(&result, *sp);
     111           0 :                     break;
     112             :             }
     113             :         }
     114             :         else
     115             :         {
     116       12322 :             appendStringInfoChar(&result, *sp);
     117             :         }
     118             :     }
     119             : 
     120         118 :     return result.data;
     121             : }

Generated by: LCOV version 1.13