From fdf7d09680595ac5cdacb8804ed9877047112b5c Mon Sep 17 00:00:00 2001 From: Heikki Linnakangas Date: Fri, 9 Jan 2015 11:08:23 +0200 Subject: [PATCH] Move the only function defined in util.c/util.h to where it's used. --- contrib/pg_rewind/Makefile | 2 +- contrib/pg_rewind/copy_fetch.c | 1 - contrib/pg_rewind/datapagemap.c | 1 - contrib/pg_rewind/filemap.c | 65 +++++++++++++++++++-------------- contrib/pg_rewind/pg_rewind.h | 1 - contrib/pg_rewind/util.c | 32 ---------------- contrib/pg_rewind/util.h | 15 -------- 7 files changed, 39 insertions(+), 78 deletions(-) delete mode 100644 contrib/pg_rewind/util.c delete mode 100644 contrib/pg_rewind/util.h diff --git a/contrib/pg_rewind/Makefile b/contrib/pg_rewind/Makefile index 48a36929b2..0c8b218015 100644 --- a/contrib/pg_rewind/Makefile +++ b/contrib/pg_rewind/Makefile @@ -7,7 +7,7 @@ PGFILEDESC = "pg_rewind - repurpose an old master server as standby" PGAPPICON = win32 PROGRAM = pg_rewind -OBJS = pg_rewind.o parsexlog.o xlogreader.o util.o datapagemap.o timeline.o \ +OBJS = pg_rewind.o parsexlog.o xlogreader.o datapagemap.o timeline.o \ fetch.o copy_fetch.o libpq_fetch.o filemap.o $(WIN32RES) PG_CPPFLAGS = -I$(libpq_srcdir) diff --git a/contrib/pg_rewind/copy_fetch.c b/contrib/pg_rewind/copy_fetch.c index 6b0ccc196b..4695d0ecac 100644 --- a/contrib/pg_rewind/copy_fetch.c +++ b/contrib/pg_rewind/copy_fetch.c @@ -22,7 +22,6 @@ #include "fetch.h" #include "filemap.h" #include "datapagemap.h" -#include "util.h" static void recurse_dir(const char *datadir, const char *path, process_file_callback_t callback); diff --git a/contrib/pg_rewind/datapagemap.c b/contrib/pg_rewind/datapagemap.c index 235a881a1e..6d76b3ad7d 100644 --- a/contrib/pg_rewind/datapagemap.c +++ b/contrib/pg_rewind/datapagemap.c @@ -13,7 +13,6 @@ #include "postgres_fe.h" #include "datapagemap.h" -#include "util.h" struct datapagemap_iterator { diff --git a/contrib/pg_rewind/filemap.c b/contrib/pg_rewind/filemap.c index 5fac54b3fc..41d3c9ef92 100644 --- a/contrib/pg_rewind/filemap.c +++ b/contrib/pg_rewind/filemap.c @@ -16,7 +16,6 @@ #include "datapagemap.h" #include "filemap.h" -#include "util.h" #include "pg_rewind.h" #include "catalog/pg_tablespace.h" #include "storage/fd.h" @@ -24,6 +23,8 @@ filemap_t *filemap = NULL; static bool isRelDataFile(const char *path); +static char *datasegpath(RelFileNode rnode, ForkNumber forknum, + BlockNumber segno); static int path_cmp(const void *a, const void *b); static int final_filemap_cmp(const void *a, const void *b); static void filemap_list_to_array(void); @@ -497,9 +498,7 @@ static bool isRelDataFile(const char *path) { char buf[20 + 1]; - Oid spcNode; - Oid dbNode; - Oid relNode; + RelFileNode rnode; unsigned int segNo; int nmatch; bool matched; @@ -520,33 +519,34 @@ isRelDataFile(const char *path) * And the relation data files themselves have a filename like: * * . - * */ - spcNode = InvalidOid; - dbNode = InvalidOid; - relNode = InvalidOid; + rnode.spcNode = InvalidOid; + rnode.dbNode = InvalidOid; + rnode.relNode = InvalidOid; segNo = 0; matched = false; - nmatch = sscanf(path, "global/%u.%u", &relNode, &segNo); + nmatch = sscanf(path, "global/%u.%u", &rnode.relNode, &segNo); if (nmatch == 1 || nmatch == 2) { - spcNode = GLOBALTABLESPACE_OID; - dbNode = 0; + rnode.spcNode = GLOBALTABLESPACE_OID; + rnode.dbNode = 0; matched = true; } else { - nmatch = sscanf(path, "base/%u/%u.%u", &dbNode, &relNode, &segNo); + nmatch = sscanf(path, "base/%u/%u.%u", + &rnode.dbNode, &rnode.relNode, &segNo); if (nmatch == 2 || nmatch == 3) { - spcNode = DEFAULTTABLESPACE_OID; + rnode.spcNode = DEFAULTTABLESPACE_OID; matched = true; } else { nmatch = sscanf(path, "pg_tblspc/%u/PG_%20s/%u/%u.%u", - &spcNode, buf, &dbNode, &relNode, &segNo); + &rnode.spcNode, buf, &rnode.dbNode, &rnode.relNode, + &segNo); if (nmatch == 4 || nmatch == 5) matched = true; } @@ -562,26 +562,37 @@ isRelDataFile(const char *path) */ if (matched) { - char *check_path; - char *check_path_with_seg; - - check_path = GetRelationPath(dbNode, spcNode, relNode, InvalidBackendId, - MAIN_FORKNUM); - if (segNo != 0) - { - check_path_with_seg = psprintf("%s.%u", check_path, segNo); - pfree(check_path); - } - else - check_path_with_seg = check_path; + char *check_path = datasegpath(rnode, MAIN_FORKNUM, segNo); - if (strcmp(check_path_with_seg, path) != 0) + if (strcmp(check_path, path) != 0) matched = false; + + pfree(check_path); } return matched; } +/* + * A helper function to create the path of a relation file and segment. + * + * The returned path is palloc'd + */ +static char * +datasegpath(RelFileNode rnode, ForkNumber forknum, BlockNumber segno) +{ + char *path = relpathperm(rnode, forknum); + + if (segno > 0) + { + char *segpath = psprintf("%s.%u", path, segno); + pfree(path); + return segpath; + } + else + return path; +} + static int path_cmp(const void *a, const void *b) { diff --git a/contrib/pg_rewind/pg_rewind.h b/contrib/pg_rewind/pg_rewind.h index d092902632..5a6dbce053 100644 --- a/contrib/pg_rewind/pg_rewind.h +++ b/contrib/pg_rewind/pg_rewind.h @@ -14,7 +14,6 @@ #include "c.h" #include "datapagemap.h" -#include "util.h" #include "access/timeline.h" #include "storage/block.h" diff --git a/contrib/pg_rewind/util.c b/contrib/pg_rewind/util.c deleted file mode 100644 index bb03319389..0000000000 --- a/contrib/pg_rewind/util.c +++ /dev/null @@ -1,32 +0,0 @@ -/*------------------------------------------------------------------------- - * - * util.c - * Misc utility functions - * - * Copyright (c) 2013-2015, PostgreSQL Global Development Group - * - *------------------------------------------------------------------------- - */ -#include "postgres_fe.h" - -#include "common/relpath.h" -#include "catalog/catalog.h" -#include "catalog/pg_tablespace.h" - -#include "pg_rewind.h" - -char * -datasegpath(RelFileNode rnode, ForkNumber forknum, BlockNumber segno) -{ - char *path = relpathperm(rnode, forknum); - - if (segno > 0) - { - char *segpath = pg_malloc(strlen(path) + 13); - sprintf(segpath, "%s.%u", path, segno); - pg_free(path); - return segpath; - } - else - return path; -} diff --git a/contrib/pg_rewind/util.h b/contrib/pg_rewind/util.h deleted file mode 100644 index e3d69f8d25..0000000000 --- a/contrib/pg_rewind/util.h +++ /dev/null @@ -1,15 +0,0 @@ -/*------------------------------------------------------------------------- - * - * util.h - * Prototypes for functions in util.c - * - * Copyright (c) 2013-2015, PostgreSQL Global Development Group - *------------------------------------------------------------------------- - */ -#ifndef UTIL_H -#define UTIL_H - -extern char *datasegpath(RelFileNode rnode, ForkNumber forknum, - BlockNumber segno); - -#endif /* UTIL_H */ -- 2.39.5