From 9da091497669de1921d32c0f8ae5ce5ac6dc2fa9 Mon Sep 17 00:00:00 2001 From: Ian Barwick Date: Thu, 22 Dec 2016 12:11:54 +0900 Subject: [PATCH] Consolidate various backported functions --- HISTORY | 1 + Makefile | 2 +- escape.c => compat.c | 38 +++++++++++++++++++++++++++++++++++--- escape.h => compat.h | 9 ++++++--- repmgr.c | 2 +- strutil.c | 31 ------------------------------- strutil.h | 3 --- 7 files changed, 44 insertions(+), 42 deletions(-) rename escape.c => compat.c (74%) rename escape.h => compat.h (86%) diff --git a/HISTORY b/HISTORY index 38a455fd..50746d0b 100644 --- a/HISTORY +++ b/HISTORY @@ -10,6 +10,7 @@ with cascaded downstream node records (Ian) repmgr: add option `--no-conninfo-password` (Abhijit, Ian) repmgr: add initial support for PostgreSQL 10.0 (Ian) + repmgr: escape values in primary_conninfo if needed (Ian) 3.2.1 2016-10-24 repmgr: require a valid repmgr cluster name unless -F/--force diff --git a/Makefile b/Makefile index fdd1da0f..f8d26fb4 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ HEADERS = $(wildcard *.h) repmgrd_OBJS = dbutils.o config.o repmgrd.o log.o strutil.o -repmgr_OBJS = dbutils.o check_dir.o config.o repmgr.o log.o strutil.o dirmod.o escape.o +repmgr_OBJS = dbutils.o check_dir.o config.o repmgr.o log.o strutil.o dirmod.o compat.o DATA = repmgr.sql uninstall_repmgr.sql diff --git a/escape.c b/compat.c similarity index 74% rename from escape.c rename to compat.c index 6295a552..65fa0c1f 100644 --- a/escape.c +++ b/compat.c @@ -1,7 +1,8 @@ /* * - * escape.c - * parameter escaping functions + * compat.c + * Provide backports of various functions not publicly + * exposed before PostgreSQL 9.6 * * Copyright (C) 2ndQuadrant, 2010-2016 * @@ -26,7 +27,7 @@ #if (PG_VERSION_NUM < 90600) #include "repmgr.h" -#include "escape.h" +#include "compat.h" /* * Append the given string to the buffer, with suitable quoting for passing @@ -76,4 +77,35 @@ appendConnStrVal(PQExpBuffer buf, const char *str) appendPQExpBufferStr(buf, str); } +/* + * Adapted from: src/fe_utils/string_utils.c + * + * Function not publicly available before PostgreSQL 9.6. + */ +void +appendShellString(PQExpBuffer buf, const char *str) +{ + const char *p; + + appendPQExpBufferChar(buf, '\''); + for (p = str; *p; p++) + { + if (*p == '\n' || *p == '\r') + { + fprintf(stderr, + _("shell command argument contains a newline or carriage return: \"%s\"\n"), + str); + exit(ERR_BAD_CONFIG); + } + + if (*p == '\'') + appendPQExpBufferStr(buf, "'\"'\"'"); + else + appendPQExpBufferChar(buf, *p); + } + + appendPQExpBufferChar(buf, '\''); +} + + #endif diff --git a/escape.h b/compat.h similarity index 86% rename from escape.h rename to compat.h index f3dd3e85..b5350ad0 100644 --- a/escape.h +++ b/compat.h @@ -1,5 +1,5 @@ /* - * escape.h + * compat.h * Copyright (c) 2ndQuadrant, 2010-2016 * * This program is free software: you can redistribute it and/or modify @@ -17,10 +17,13 @@ * */ -#ifndef _ESCAPE_H_ -#define _ESCAPE_H_ +#ifndef _COMPAT_H_ +#define _COMPAT_H_ extern void appendConnStrVal(PQExpBuffer buf, const char *str); +extern void +appendShellString(PQExpBuffer buf, const char *str); + #endif diff --git a/repmgr.c b/repmgr.c index acc66034..8969de3a 100644 --- a/repmgr.c +++ b/repmgr.c @@ -64,7 +64,7 @@ #include "version.h" #if (PG_VERSION_NUM < 90600) -#include "escape.h" +#include "compat.h" #else #include "fe_utils/string_utils.h" #include "postgres_fe.h" diff --git a/strutil.c b/strutil.c index db6c024e..1d69e33d 100644 --- a/strutil.c +++ b/strutil.c @@ -89,37 +89,6 @@ maxlen_snprintf(char *str, const char *format,...) } -/* - * Adapted from: src/fe_utils/string_utils.c - * - * Function not publicly available before PostgreSQL 9.6. - */ -void -appendShellString(PQExpBuffer buf, const char *str) -{ - const char *p; - - appendPQExpBufferChar(buf, '\''); - for (p = str; *p; p++) - { - if (*p == '\n' || *p == '\r') - { - fprintf(stderr, - _("shell command argument contains a newline or carriage return: \"%s\"\n"), - str); - exit(ERR_BAD_CONFIG); - } - - if (*p == '\'') - appendPQExpBufferStr(buf, "'\"'\"'"); - else - appendPQExpBufferChar(buf, *p); - } - - appendPQExpBufferChar(buf, '\''); -} - - /* * Escape a string for use as a parameter in recovery.conf * Caller must free returned value diff --git a/strutil.h b/strutil.h index da436b3b..37a9871d 100644 --- a/strutil.h +++ b/strutil.h @@ -49,9 +49,6 @@ extern int maxlen_snprintf(char *str, const char *format,...) __attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3))); -extern void -appendShellString(PQExpBuffer buf, const char *str); - extern char * escape_recovery_conf_value(const char *src); #endif /* _STRUTIL_H_ */