Centralize single quote escaping in src/port/quotes.c
For code-reuse in upcoming functionality in pg_basebackup. Zoltan Boszormenyi
This commit is contained in:
parent
fc8745070a
commit
940d136661
@ -354,6 +354,18 @@ pg_strdup(const char *s)
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static char *
|
||||||
|
escape_quotes(const char *src)
|
||||||
|
{
|
||||||
|
char *result = escape_single_quotes_ascii(src);
|
||||||
|
if (!result)
|
||||||
|
{
|
||||||
|
fprintf(stderr, _("%s: out of memory\n"), progname);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* make a copy of the array of lines, with token replaced by replacement
|
* make a copy of the array of lines, with token replaced by replacement
|
||||||
* the first time it occurs on each line.
|
* the first time it occurs on each line.
|
||||||
@ -2415,35 +2427,6 @@ check_ok(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* Escape (by doubling) any single quotes or backslashes in given string
|
|
||||||
*
|
|
||||||
* Note: this is used to process both postgresql.conf entries and SQL
|
|
||||||
* string literals. Since postgresql.conf strings are defined to treat
|
|
||||||
* backslashes as escapes, we have to double backslashes here. Hence,
|
|
||||||
* when using this for a SQL string literal, use E'' syntax.
|
|
||||||
*
|
|
||||||
* We do not need to worry about encoding considerations because all
|
|
||||||
* valid backend encodings are ASCII-safe.
|
|
||||||
*/
|
|
||||||
static char *
|
|
||||||
escape_quotes(const char *src)
|
|
||||||
{
|
|
||||||
int len = strlen(src),
|
|
||||||
i,
|
|
||||||
j;
|
|
||||||
char *result = pg_malloc(len * 2 + 1);
|
|
||||||
|
|
||||||
for (i = 0, j = 0; i < len; i++)
|
|
||||||
{
|
|
||||||
if (SQL_STR_DOUBLE(src[i], true))
|
|
||||||
result[j++] = src[i];
|
|
||||||
result[j++] = src[i];
|
|
||||||
}
|
|
||||||
result[j] = '\0';
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Hack to suppress a warning about %x from some versions of gcc */
|
/* Hack to suppress a warning about %x from some versions of gcc */
|
||||||
static inline size_t
|
static inline size_t
|
||||||
my_strftime(char *s, size_t max, const char *fmt, const struct tm * tm)
|
my_strftime(char *s, size_t max, const char *fmt, const struct tm * tm)
|
||||||
|
@ -465,4 +465,7 @@ extern int pg_check_dir(const char *dir);
|
|||||||
/* port/pgmkdirp.c */
|
/* port/pgmkdirp.c */
|
||||||
extern int pg_mkdir_p(char *path, int omode);
|
extern int pg_mkdir_p(char *path, int omode);
|
||||||
|
|
||||||
|
/* port/quotes.c */
|
||||||
|
extern char *escape_single_quotes_ascii(const char *src);
|
||||||
|
|
||||||
#endif /* PG_PORT_H */
|
#endif /* PG_PORT_H */
|
||||||
|
@ -32,7 +32,7 @@ LIBS += $(PTHREAD_LIBS)
|
|||||||
|
|
||||||
OBJS = $(LIBOBJS) chklocale.o dirmod.o erand48.o exec.o fls.o inet_net_ntop.o \
|
OBJS = $(LIBOBJS) chklocale.o dirmod.o erand48.o exec.o fls.o inet_net_ntop.o \
|
||||||
noblock.o path.o pgcheckdir.o pg_crc.o pgmkdirp.o pgsleep.o \
|
noblock.o path.o pgcheckdir.o pg_crc.o pgmkdirp.o pgsleep.o \
|
||||||
pgstrcasecmp.o qsort.o qsort_arg.o sprompt.o tar.o thread.o
|
pgstrcasecmp.o qsort.o qsort_arg.o quotes.o sprompt.o tar.o thread.o
|
||||||
|
|
||||||
# foo_srv.o and foo.o are both built from foo.c, but only foo.o has -DFRONTEND
|
# foo_srv.o and foo.o are both built from foo.c, but only foo.o has -DFRONTEND
|
||||||
OBJS_SRV = $(OBJS:%.o=%_srv.o)
|
OBJS_SRV = $(OBJS:%.o=%_srv.o)
|
||||||
|
36
src/port/quotes.c
Normal file
36
src/port/quotes.c
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
#include "c.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Escape (by doubling) any single quotes or backslashes in given string
|
||||||
|
*
|
||||||
|
* Note: this is used to process postgresql.conf entries and to quote
|
||||||
|
* string literals in pg_basebackup for creating recovery.conf.
|
||||||
|
* Since postgresql.conf strings are defined to treat backslashes as escapes,
|
||||||
|
* we have to double backslashes here.
|
||||||
|
*
|
||||||
|
* Since this function is only used for parsing or creating configuration
|
||||||
|
* files, we do not care about encoding considerations.
|
||||||
|
*
|
||||||
|
* Returns a malloced() string that it's the responsibility of the caller
|
||||||
|
* to free.
|
||||||
|
*/
|
||||||
|
char *
|
||||||
|
escape_single_quotes_ascii(const char *src)
|
||||||
|
{
|
||||||
|
int len = strlen(src),
|
||||||
|
i,
|
||||||
|
j;
|
||||||
|
char *result = malloc(len * 2 + 1);
|
||||||
|
|
||||||
|
if (!result)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
for (i = 0, j = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
if (SQL_STR_DOUBLE(src[i], true))
|
||||||
|
result[j++] = src[i];
|
||||||
|
result[j++] = src[i];
|
||||||
|
}
|
||||||
|
result[j] = '\0';
|
||||||
|
return result;
|
||||||
|
}
|
@ -59,7 +59,7 @@ sub mkvcbuild
|
|||||||
chklocale.c crypt.c fls.c fseeko.c getrusage.c inet_aton.c random.c
|
chklocale.c crypt.c fls.c fseeko.c getrusage.c inet_aton.c random.c
|
||||||
srandom.c getaddrinfo.c gettimeofday.c inet_net_ntop.c kill.c open.c
|
srandom.c getaddrinfo.c gettimeofday.c inet_net_ntop.c kill.c open.c
|
||||||
erand48.c snprintf.c strlcat.c strlcpy.c dirmod.c exec.c noblock.c path.c
|
erand48.c snprintf.c strlcat.c strlcpy.c dirmod.c exec.c noblock.c path.c
|
||||||
pgcheckdir.c pg_crc.c pgmkdirp.c pgsleep.c pgstrcasecmp.c qsort.c qsort_arg.c
|
pgcheckdir.c pg_crc.c pgmkdirp.c pgsleep.c pgstrcasecmp.c qsort.c qsort_arg.c quotes.c
|
||||||
sprompt.c tar.c thread.c getopt.c getopt_long.c dirent.c rint.c win32env.c
|
sprompt.c tar.c thread.c getopt.c getopt_long.c dirent.c rint.c win32env.c
|
||||||
win32error.c win32setlocale.c);
|
win32error.c win32setlocale.c);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user