1
0
mirror of https://github.com/postgres/postgres.git synced 2025-09-02 04:21:28 +03:00

Common function for percent placeholder replacement

There are a number of places where a shell command is constructed with
percent-placeholders (like %x).  It's cumbersome to have to open-code
this several times.  This factors out this logic into a separate
function.  This also allows us to ensure consistency for and document
some subtle behaviors, such as what to do with unrecognized
placeholders.

The unified handling is now that incorrect and unknown placeholders
are an error, where previously in most cases they were skipped or
ignored.  This affects the following settings:

- archive_cleanup_command
- archive_command
- recovery_end_command
- restore_command
- ssl_passphrase_command

The following settings are part of this refactoring but already had
stricter error handling and should be unchanged in their behavior:

- basebackup_to_shell.command

Reviewed-by: Nathan Bossart <nathandbossart@gmail.com>
Discussion: https://www.postgresql.org/message-id/flat/5238bbed-0b01-83a6-d4b2-7eb0562a054e%40enterprisedb.com
This commit is contained in:
Peter Eisentraut
2023-01-11 07:22:51 +01:00
parent 5f6401f81c
commit c96de2ce17
10 changed files with 199 additions and 247 deletions

View File

@@ -65,6 +65,7 @@ OBJS_COMMON = \
kwlookup.o \
link-canary.o \
md5_common.o \
percentrepl.o \
pg_get_line.o \
pg_lzcompress.o \
pg_prng.o \

View File

@@ -20,7 +20,7 @@
#endif
#include "common/archive.h"
#include "lib/stringinfo.h"
#include "common/percentrepl.h"
/*
* BuildRestoreCommand
@@ -41,81 +41,20 @@ BuildRestoreCommand(const char *restoreCommand,
const char *xlogfname,
const char *lastRestartPointFname)
{
StringInfoData result;
const char *sp;
char *nativePath = NULL;
char *result;
/*
* Build the command to be executed.
*/
initStringInfo(&result);
for (sp = restoreCommand; *sp; sp++)
if (xlogpath)
{
if (*sp == '%')
{
switch (sp[1])
{
case 'p':
{
char *nativePath;
/* %p: relative path of target file */
if (xlogpath == NULL)
{
pfree(result.data);
return NULL;
}
sp++;
/*
* This needs to use a placeholder to not modify the
* input with the conversion done via
* make_native_path().
*/
nativePath = pstrdup(xlogpath);
make_native_path(nativePath);
appendStringInfoString(&result,
nativePath);
pfree(nativePath);
break;
}
case 'f':
/* %f: filename of desired file */
if (xlogfname == NULL)
{
pfree(result.data);
return NULL;
}
sp++;
appendStringInfoString(&result, xlogfname);
break;
case 'r':
/* %r: filename of last restartpoint */
if (lastRestartPointFname == NULL)
{
pfree(result.data);
return NULL;
}
sp++;
appendStringInfoString(&result,
lastRestartPointFname);
break;
case '%':
/* convert %% to a single % */
sp++;
appendStringInfoChar(&result, *sp);
break;
default:
/* otherwise treat the % as not special */
appendStringInfoChar(&result, *sp);
break;
}
}
else
{
appendStringInfoChar(&result, *sp);
}
nativePath = pstrdup(xlogpath);
make_native_path(nativePath);
}
return result.data;
result = replace_percent_placeholders(restoreCommand, "restore_command", "frp",
xlogfname, lastRestartPointFname, nativePath);
if (nativePath)
pfree(nativePath);
return result;
}

View File

@@ -17,6 +17,7 @@ common_sources = files(
'kwlookup.c',
'link-canary.c',
'md5_common.c',
'percentrepl.c',
'pg_get_line.c',
'pg_lzcompress.c',
'pg_prng.c',

137
src/common/percentrepl.c Normal file
View File

@@ -0,0 +1,137 @@
/*-------------------------------------------------------------------------
*
* percentrepl.c
* Common routines to replace percent placeholders in strings
*
* Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* src/common/percentrepl.c
*
*-------------------------------------------------------------------------
*/
#ifndef FRONTEND
#include "postgres.h"
#else
#include "postgres_fe.h"
#include "common/logging.h"
#endif
#include "common/percentrepl.h"
#include "lib/stringinfo.h"
/*
* replace_percent_placeholders
*
* Replace percent-letter placeholders in input string with the supplied
* values. For example, to replace %f with foo and %b with bar, call
*
* replace_percent_placeholders(instr, "param_name", "bf", bar, foo);
*
* The return value is palloc'd.
*
* "%%" is replaced by a single "%".
*
* This throws an error for an unsupported placeholder or a "%" at the end of
* the input string.
*
* A value may be NULL. If the corresponding placeholder is found in the
* input string, it will be treated as if an unsupported placeholder was used.
* This allows callers to share a "letters" specification but vary the
* actually supported placeholders at run time.
*
* This functions is meant for cases where all the values are readily
* available or cheap to compute and most invocations will use most values
* (for example for archive_command). Also, it requires that all values are
* strings. It won't be a good match for things like log prefixes or prompts
* that use a mix of data types and any invocation will only use a few of the
* possible values.
*
* param_name is the name of the underlying GUC parameter, for error
* reporting. At the moment, this function is only used for GUC parameters.
* If other kinds of uses were added, the error reporting would need to be
* revised.
*/
char *
replace_percent_placeholders(const char *instr, const char *param_name, const char *letters,...)
{
StringInfoData result;
initStringInfo(&result);
for (const char *sp = instr; *sp; sp++)
{
if (*sp == '%')
{
if (sp[1] == '%')
{
/* Convert %% to a single % */
sp++;
appendStringInfoChar(&result, *sp);
}
else if (sp[1] == '\0')
{
/* Incomplete escape sequence, expected a character afterward */
#ifdef FRONTEND
pg_log_error("invalid value for parameter \"%s\": \"%s\"", param_name, instr);
pg_log_error_detail("String ends unexpectedly after escape character \"%%\".");
exit(1);
#else
ereport(ERROR,
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("invalid value for parameter \"%s\": \"%s\"", param_name, instr),
errdetail("String ends unexpectedly after escape character \"%%\"."));
#endif
}
else
{
/* Look up placeholder character */
bool found = false;
va_list ap;
sp++;
va_start(ap, letters);
for (const char *lp = letters; *lp; lp++)
{
char *val = va_arg(ap, char *);
if (*sp == *lp)
{
if (val)
{
appendStringInfoString(&result, val);
found = true;
}
/* If val is NULL, we will report an error. */
break;
}
}
va_end(ap);
if (!found)
{
/* Unknown escape sequence */
#ifdef FRONTEND
pg_log_error("invalid value for parameter \"%s\": \"%s\"", param_name, instr);
pg_log_error_detail("String contains unexpected escape sequence \"%c\".", *sp);
exit(1);
#else
ereport(ERROR,
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("invalid value for parameter \"%s\": \"%s\"", param_name, instr),
errdetail("String contains unexpected escape sequence \"%c\".", *sp));
#endif
}
}
}
else
{
appendStringInfoChar(&result, *sp);
}
}
return result.data;
}