From 70e36adb0d4b7f4656a06a5396a4cf65fc863323 Mon Sep 17 00:00:00 2001 From: Andres Freund Date: Sat, 3 Jan 2015 20:51:52 +0100 Subject: [PATCH] Add pg_string_endswith as the start of a string helper library in src/common. Backpatch to 9.3 where src/common was introduce, because a bugfix that needs to be backpatched, requires the function. Earlier branches will have to duplicate the code. --- src/backend/replication/slot.c | 21 ++--------------- src/common/Makefile | 2 +- src/common/string.c | 43 ++++++++++++++++++++++++++++++++++ src/include/common/string.h | 15 ++++++++++++ src/tools/msvc/Mkvcbuild.pm | 2 +- 5 files changed, 62 insertions(+), 21 deletions(-) create mode 100644 src/common/string.c create mode 100644 src/include/common/string.h diff --git a/src/backend/replication/slot.c b/src/backend/replication/slot.c index 29191ffb95..7b5e0a89bf 100644 --- a/src/backend/replication/slot.c +++ b/src/backend/replication/slot.c @@ -40,6 +40,7 @@ #include #include "access/transam.h" +#include "common/string.h" #include "miscadmin.h" #include "replication/slot.h" #include "storage/fd.h" @@ -779,24 +780,6 @@ CheckSlotRequirements(void) errmsg("replication slots can only be used if wal_level >= archive"))); } -/* - * Returns whether the string `str' has the postfix `end'. - */ -static bool -string_endswith(const char *str, const char *end) -{ - size_t slen = strlen(str); - size_t elen = strlen(end); - - /* can't be a postfix if longer */ - if (elen > slen) - return false; - - /* compare the end of the strings */ - str += slen - elen; - return strcmp(str, end) == 0; -} - /* * Flush all replication slots to disk. * @@ -864,7 +847,7 @@ StartupReplicationSlots(void) continue; /* we crashed while a slot was being setup or deleted, clean up */ - if (string_endswith(replication_de->d_name, ".tmp")) + if (pg_str_endswith(replication_de->d_name, ".tmp")) { if (!rmtree(path, true)) { diff --git a/src/common/Makefile b/src/common/Makefile index 7edbaaa2c1..e5c345d7de 100644 --- a/src/common/Makefile +++ b/src/common/Makefile @@ -23,7 +23,7 @@ include $(top_builddir)/src/Makefile.global override CPPFLAGS := -DFRONTEND $(CPPFLAGS) LIBS += $(PTHREAD_LIBS) -OBJS_COMMON = exec.o pgfnames.o psprintf.o relpath.o rmtree.o username.o wait_error.o +OBJS_COMMON = exec.o pgfnames.o psprintf.o relpath.o rmtree.o string.o username.o wait_error.o OBJS_FRONTEND = $(OBJS_COMMON) fe_memutils.o diff --git a/src/common/string.c b/src/common/string.c new file mode 100644 index 0000000000..27e074313d --- /dev/null +++ b/src/common/string.c @@ -0,0 +1,43 @@ +/*------------------------------------------------------------------------- + * + * string.c + * string handling helpers + * + * + * Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * + * IDENTIFICATION + * src/common/string.c + * + *------------------------------------------------------------------------- + */ + + +#ifndef FRONTEND +#include "postgres.h" +#else +#include "postgres_fe.h" +#endif + +#include "common/string.h" + + +/* + * Returns whether the string `str' has the postfix `end'. + */ +bool +pg_str_endswith(const char *str, const char *end) +{ + size_t slen = strlen(str); + size_t elen = strlen(end); + + /* can't be a postfix if longer */ + if (elen > slen) + return false; + + /* compare the end of the strings */ + str += slen - elen; + return strcmp(str, end) == 0; +} diff --git a/src/include/common/string.h b/src/include/common/string.h new file mode 100644 index 0000000000..023385856f --- /dev/null +++ b/src/include/common/string.h @@ -0,0 +1,15 @@ +/* + * string.h + * string handling helpers + * + * Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * src/include/common/string.h + */ +#ifndef COMMON_STRING_H +#define COMMON_STRING_H + +extern bool pg_str_endswith(const char *str, const char *end); + +#endif /* COMMON_STRING_H */ diff --git a/src/tools/msvc/Mkvcbuild.pm b/src/tools/msvc/Mkvcbuild.pm index 0532a90b7c..ca0876868d 100644 --- a/src/tools/msvc/Mkvcbuild.pm +++ b/src/tools/msvc/Mkvcbuild.pm @@ -76,7 +76,7 @@ sub mkvcbuild push(@pgportfiles, 'rint.c') if ($vsVersion < '12.00'); our @pgcommonallfiles = qw( - exec.c pgfnames.c psprintf.c relpath.c rmtree.c username.c wait_error.c); + exec.c pgfnames.c psprintf.c relpath.c rmtree.c string.c username.c wait_error.c); our @pgcommonfrontendfiles = (@pgcommonallfiles, qw(fe_memutils.c)); -- 2.40.0