PostgreSQL Source Code  git master
archive.c File Reference
#include "postgres.h"
#include "common/archive.h"
#include "lib/stringinfo.h"
Include dependency graph for archive.c:

Go to the source code of this file.

Functions

char * BuildRestoreCommand (const char *restoreCommand, const char *xlogpath, const char *xlogfname, const char *lastRestartPointFname)
 

Function Documentation

◆ BuildRestoreCommand()

char* BuildRestoreCommand ( const char *  restoreCommand,
const char *  xlogpath,
const char *  xlogfname,
const char *  lastRestartPointFname 
)

Definition at line 39 of file archive.c.

References appendStringInfoChar(), appendStringInfoString(), StringInfoData::data, initStringInfo(), make_native_path(), pfree(), and pstrdup().

Referenced by RestoreArchivedFile().

43 {
44  StringInfoData result;
45  const char *sp;
46 
47  /*
48  * Build the command to be executed.
49  */
50  initStringInfo(&result);
51 
52  for (sp = restoreCommand; *sp; sp++)
53  {
54  if (*sp == '%')
55  {
56  switch (sp[1])
57  {
58  case 'p':
59  {
60  char *nativePath;
61 
62  /* %p: relative path of target file */
63  if (xlogpath == NULL)
64  {
65  pfree(result.data);
66  return NULL;
67  }
68  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  nativePath = pstrdup(xlogpath);
76  make_native_path(nativePath);
77  appendStringInfoString(&result,
78  nativePath);
79  pfree(nativePath);
80  break;
81  }
82  case 'f':
83  /* %f: filename of desired file */
84  if (xlogfname == NULL)
85  {
86  pfree(result.data);
87  return NULL;
88  }
89  sp++;
90  appendStringInfoString(&result, xlogfname);
91  break;
92  case 'r':
93  /* %r: filename of last restartpoint */
94  if (lastRestartPointFname == NULL)
95  {
96  pfree(result.data);
97  return NULL;
98  }
99  sp++;
100  appendStringInfoString(&result,
101  lastRestartPointFname);
102  break;
103  case '%':
104  /* convert %% to a single % */
105  sp++;
106  appendStringInfoChar(&result, *sp);
107  break;
108  default:
109  /* otherwise treat the % as not special */
110  appendStringInfoChar(&result, *sp);
111  break;
112  }
113  }
114  else
115  {
116  appendStringInfoChar(&result, *sp);
117  }
118  }
119 
120  return result.data;
121 }
char * pstrdup(const char *in)
Definition: mcxt.c:1186
void make_native_path(char *path)
Definition: path.c:166
void pfree(void *pointer)
Definition: mcxt.c:1056
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
char restoreCommand[MAXPGPATH]
Definition: pg_standby.c:61
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59