Skip to content

Commit a730183

Browse files
committed
Move relpath() to libpgcommon
This enables non-backend code, such as pg_xlogdump, to use it easily. The previous location, in src/backend/catalog/catalog.c, made that essentially impossible because that file depends on many backend-only facilities; so this needs to live separately.
1 parent 6e3fd96 commit a730183

File tree

19 files changed

+237
-168
lines changed

19 files changed

+237
-168
lines changed

src/backend/Makefile

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@ LOCALOBJS += utils/probes.o
3535
endif
3636
endif
3737

38-
OBJS = $(SUBDIROBJS) $(LOCALOBJS) $(top_builddir)/src/port/libpgport_srv.a
38+
OBJS = $(SUBDIROBJS) $(LOCALOBJS) $(top_builddir)/src/port/libpgport_srv.a \
39+
$(top_builddir)/src/common/libpgcommon_srv.a
3940

40-
# We put libpgport into OBJS, so remove it from LIBS; also add libldap
41-
LIBS := $(filter-out -lpgport, $(LIBS)) $(LDAP_LIBS_BE)
41+
# We put libpgport and libpgcommon into OBJS, so remove it from LIBS; also add
42+
# libldap
43+
LIBS := $(filter-out -lpgport -lpgcommon, $(LIBS)) $(LDAP_LIBS_BE)
4244

4345
# The backend doesn't need everything that's in LIBS, however
4446
LIBS := $(filter-out -lz -lreadline -ledit -ltermcap -lncurses -lcurses, $(LIBS))

src/backend/access/rmgrdesc/smgrdesc.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "catalog/catalog.h"
1818
#include "catalog/storage_xlog.h"
19+
#include "common/relpath.h"
1920

2021

2122
void

src/backend/access/rmgrdesc/xactdesc.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "access/xact.h"
1818
#include "catalog/catalog.h"
19+
#include "common/relpath.h"
1920
#include "storage/sinval.h"
2021
#include "utils/timestamp.h"
2122

src/backend/access/transam/xlogutils.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "access/xlog.h"
2121
#include "access/xlogutils.h"
2222
#include "catalog/catalog.h"
23+
#include "common/relpath.h"
2324
#include "storage/smgr.h"
2425
#include "utils/guc.h"
2526
#include "utils/hsearch.h"

src/backend/catalog/catalog.c

Lines changed: 1 addition & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -37,28 +37,14 @@
3737
#include "catalog/pg_shseclabel.h"
3838
#include "catalog/pg_tablespace.h"
3939
#include "catalog/toasting.h"
40+
#include "common/relpath.h"
4041
#include "miscadmin.h"
4142
#include "storage/fd.h"
4243
#include "utils/fmgroids.h"
4344
#include "utils/rel.h"
4445
#include "utils/tqual.h"
4546

4647

47-
#define FORKNAMECHARS 4 /* max chars for a fork name */
48-
49-
/*
50-
* Lookup table of fork name by fork number.
51-
*
52-
* If you add a new entry, remember to update the errhint below, and the
53-
* documentation for pg_relation_size(). Also keep FORKNAMECHARS above
54-
* up-to-date.
55-
*/
56-
const char *forkNames[] = {
57-
"main", /* MAIN_FORKNUM */
58-
"fsm", /* FSM_FORKNUM */
59-
"vm", /* VISIBILITYMAP_FORKNUM */
60-
"init" /* INIT_FORKNUM */
61-
};
6248

6349
/*
6450
* forkname_to_number - look up fork number by name
@@ -79,130 +65,6 @@ forkname_to_number(char *forkName)
7965
return InvalidForkNumber; /* keep compiler quiet */
8066
}
8167

82-
/*
83-
* forkname_chars
84-
* We use this to figure out whether a filename could be a relation
85-
* fork (as opposed to an oddly named stray file that somehow ended
86-
* up in the database directory). If the passed string begins with
87-
* a fork name (other than the main fork name), we return its length,
88-
* and set *fork (if not NULL) to the fork number. If not, we return 0.
89-
*
90-
* Note that the present coding assumes that there are no fork names which
91-
* are prefixes of other fork names.
92-
*/
93-
int
94-
forkname_chars(const char *str, ForkNumber *fork)
95-
{
96-
ForkNumber forkNum;
97-
98-
for (forkNum = 1; forkNum <= MAX_FORKNUM; forkNum++)
99-
{
100-
int len = strlen(forkNames[forkNum]);
101-
102-
if (strncmp(forkNames[forkNum], str, len) == 0)
103-
{
104-
if (fork)
105-
*fork = forkNum;
106-
return len;
107-
}
108-
}
109-
return 0;
110-
}
111-
112-
/*
113-
* relpathbackend - construct path to a relation's file
114-
*
115-
* Result is a palloc'd string.
116-
*/
117-
char *
118-
relpathbackend(RelFileNode rnode, BackendId backend, ForkNumber forknum)
119-
{
120-
int pathlen;
121-
char *path;
122-
123-
if (rnode.spcNode == GLOBALTABLESPACE_OID)
124-
{
125-
/* Shared system relations live in {datadir}/global */
126-
Assert(rnode.dbNode == 0);
127-
Assert(backend == InvalidBackendId);
128-
pathlen = 7 + OIDCHARS + 1 + FORKNAMECHARS + 1;
129-
path = (char *) palloc(pathlen);
130-
if (forknum != MAIN_FORKNUM)
131-
snprintf(path, pathlen, "global/%u_%s",
132-
rnode.relNode, forkNames[forknum]);
133-
else
134-
snprintf(path, pathlen, "global/%u", rnode.relNode);
135-
}
136-
else if (rnode.spcNode == DEFAULTTABLESPACE_OID)
137-
{
138-
/* The default tablespace is {datadir}/base */
139-
if (backend == InvalidBackendId)
140-
{
141-
pathlen = 5 + OIDCHARS + 1 + OIDCHARS + 1 + FORKNAMECHARS + 1;
142-
path = (char *) palloc(pathlen);
143-
if (forknum != MAIN_FORKNUM)
144-
snprintf(path, pathlen, "base/%u/%u_%s",
145-
rnode.dbNode, rnode.relNode,
146-
forkNames[forknum]);
147-
else
148-
snprintf(path, pathlen, "base/%u/%u",
149-
rnode.dbNode, rnode.relNode);
150-
}
151-
else
152-
{
153-
/* OIDCHARS will suffice for an integer, too */
154-
pathlen = 5 + OIDCHARS + 2 + OIDCHARS + 1 + OIDCHARS + 1
155-
+ FORKNAMECHARS + 1;
156-
path = (char *) palloc(pathlen);
157-
if (forknum != MAIN_FORKNUM)
158-
snprintf(path, pathlen, "base/%u/t%d_%u_%s",
159-
rnode.dbNode, backend, rnode.relNode,
160-
forkNames[forknum]);
161-
else
162-
snprintf(path, pathlen, "base/%u/t%d_%u",
163-
rnode.dbNode, backend, rnode.relNode);
164-
}
165-
}
166-
else
167-
{
168-
/* All other tablespaces are accessed via symlinks */
169-
if (backend == InvalidBackendId)
170-
{
171-
pathlen = 9 + 1 + OIDCHARS + 1
172-
+ strlen(TABLESPACE_VERSION_DIRECTORY) + 1 + OIDCHARS + 1
173-
+ OIDCHARS + 1 + FORKNAMECHARS + 1;
174-
path = (char *) palloc(pathlen);
175-
if (forknum != MAIN_FORKNUM)
176-
snprintf(path, pathlen, "pg_tblspc/%u/%s/%u/%u_%s",
177-
rnode.spcNode, TABLESPACE_VERSION_DIRECTORY,
178-
rnode.dbNode, rnode.relNode,
179-
forkNames[forknum]);
180-
else
181-
snprintf(path, pathlen, "pg_tblspc/%u/%s/%u/%u",
182-
rnode.spcNode, TABLESPACE_VERSION_DIRECTORY,
183-
rnode.dbNode, rnode.relNode);
184-
}
185-
else
186-
{
187-
/* OIDCHARS will suffice for an integer, too */
188-
pathlen = 9 + 1 + OIDCHARS + 1
189-
+ strlen(TABLESPACE_VERSION_DIRECTORY) + 1 + OIDCHARS + 2
190-
+ OIDCHARS + 1 + OIDCHARS + 1 + FORKNAMECHARS + 1;
191-
path = (char *) palloc(pathlen);
192-
if (forknum != MAIN_FORKNUM)
193-
snprintf(path, pathlen, "pg_tblspc/%u/%s/%u/t%d_%u_%s",
194-
rnode.spcNode, TABLESPACE_VERSION_DIRECTORY,
195-
rnode.dbNode, backend, rnode.relNode,
196-
forkNames[forknum]);
197-
else
198-
snprintf(path, pathlen, "pg_tblspc/%u/%s/%u/t%d_%u",
199-
rnode.spcNode, TABLESPACE_VERSION_DIRECTORY,
200-
rnode.dbNode, backend, rnode.relNode);
201-
}
202-
}
203-
return path;
204-
}
205-
20668
/*
20769
* GetDatabasePath - construct path to a database dir
20870
*

src/backend/commands/tablespace.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
#include "commands/comment.h"
6565
#include "commands/seclabel.h"
6666
#include "commands/tablespace.h"
67+
#include "common/relpath.h"
6768
#include "miscadmin.h"
6869
#include "postmaster/bgwriter.h"
6970
#include "storage/fd.h"

src/backend/storage/buffer/bufmgr.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <unistd.h>
3535

3636
#include "catalog/catalog.h"
37+
#include "common/relpath.h"
3738
#include "executor/instrument.h"
3839
#include "miscadmin.h"
3940
#include "pg_trace.h"

src/backend/storage/buffer/localbuf.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "postgres.h"
1717

1818
#include "catalog/catalog.h"
19+
#include "common/relpath.h"
1920
#include "executor/instrument.h"
2021
#include "storage/buf_internals.h"
2122
#include "storage/bufmgr.h"

src/backend/storage/file/fd.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
#include "access/xact.h"
7272
#include "catalog/catalog.h"
7373
#include "catalog/pg_tablespace.h"
74+
#include "common/relpath.h"
7475
#include "pgstat.h"
7576
#include "storage/fd.h"
7677
#include "storage/ipc.h"

src/backend/storage/file/reinit.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <unistd.h>
1818

1919
#include "catalog/catalog.h"
20+
#include "common/relpath.h"
2021
#include "storage/copydir.h"
2122
#include "storage/fd.h"
2223
#include "storage/reinit.h"

src/backend/storage/smgr/md.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "miscadmin.h"
2222
#include "access/xlog.h"
2323
#include "catalog/catalog.h"
24+
#include "common/relpath.h"
2425
#include "portability/instr_time.h"
2526
#include "postmaster/bgwriter.h"
2627
#include "storage/fd.h"

src/backend/utils/adt/dbsize.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "catalog/pg_tablespace.h"
2222
#include "commands/dbcommands.h"
2323
#include "commands/tablespace.h"
24+
#include "common/relpath.h"
2425
#include "miscadmin.h"
2526
#include "storage/fd.h"
2627
#include "utils/acl.h"

src/backend/utils/adt/misc.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "catalog/pg_tablespace.h"
2525
#include "catalog/pg_type.h"
2626
#include "commands/dbcommands.h"
27+
#include "common/relpath.h"
2728
#include "funcapi.h"
2829
#include "miscadmin.h"
2930
#include "parser/keywords.h"

src/backend/utils/cache/relcache.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
#include "catalog/schemapg.h"
5757
#include "catalog/storage.h"
5858
#include "commands/trigger.h"
59+
#include "common/relpath.h"
5960
#include "miscadmin.h"
6061
#include "optimizer/clauses.h"
6162
#include "optimizer/planmain.h"

src/common/Makefile

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ include $(top_builddir)/src/Makefile.global
2323
override CPPFLAGS := -DFRONTEND $(CPPFLAGS)
2424
LIBS += $(PTHREAD_LIBS)
2525

26-
OBJS_COMMON =
26+
OBJS_COMMON = relpath.o
2727

2828
OBJS_FRONTEND = $(OBJS_COMMON) fe_memutils.o
2929

3030
OBJS_SRV = $(OBJS_COMMON:%.o=%_srv.o)
3131

32-
all: libpgcommon.a
32+
all: libpgcommon.a libpgcommon_srv.a
3333

3434
# libpgcommon is needed by some contrib
3535
install: all installdirs
@@ -60,5 +60,12 @@ libpgcommon_srv.a: $(OBJS_SRV)
6060
%_srv.o: %.c %.o
6161
$(CC) $(CFLAGS) $(subst -DFRONTEND,, $(CPPFLAGS)) -c $< -o $@
6262

63+
$(OBJS_SRV): | submake-errcodes
64+
65+
.PHONY: submake-errcodes
66+
67+
submake-errcodes:
68+
$(MAKE) -C ../backend submake-errcodes
69+
6370
clean distclean maintainer-clean:
6471
rm -f libpgcommon.a libpgcommon_srv.a $(OBJS_FRONTEND) $(OBJS_SRV)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy