Skip to content

Commit de09685

Browse files
VictorSpiringsmolk
authored andcommitted
Removed basebackup sources dependency.
1 parent a8173e8 commit de09685

20 files changed

Lines changed: 2886 additions & 90 deletions

Makefile

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,15 @@ OBJS += src/archive.o src/backup.o src/catalog.o src/checkdb.o src/configure.o s
3737
src/delete.o src/dir.o src/fetch.o src/help.o src/init.o src/merge.o \
3838
src/parsexlog.o src/ptrack.o src/pg_probackup.o src/restore.o src/show.o src/stream.o \
3939
src/util.o src/validate.o src/datapagemap.o src/catchup.o \
40-
src/compatibility/pg-11.o
40+
src/compatibility/pg-11.o src/utils/simple_prompt.o
41+
OBJS += src/compatibility/file_compat.o src/compatibility/receivelog.o \
42+
src/compatibility/streamutil.o \
43+
src/compatibility/walmethods.o src/compatibility/file_compat10.o
4144

4245
# sources borrowed from postgresql (paths are relative to pg top dir)
43-
BORROWED_H_SRC := \
44-
src/bin/pg_basebackup/receivelog.h \
45-
src/bin/pg_basebackup/streamutil.h \
46-
src/bin/pg_basebackup/walmethods.h
46+
BORROWED_H_SRC :=
4747
BORROWED_C_SRC := \
48-
src/backend/access/transam/xlogreader.c \
49-
src/bin/pg_basebackup/receivelog.c \
50-
src/bin/pg_basebackup/streamutil.c \
51-
src/bin/pg_basebackup/walmethods.c
48+
src/backend/access/transam/xlogreader.c
5249

5350
BORROW_DIR := src/borrowed
5451
BORROWED_H := $(addprefix $(BORROW_DIR)/, $(notdir $(BORROWED_H_SRC)))
@@ -75,17 +72,19 @@ include $(top_srcdir)/contrib/contrib-global.mk
7572
endif
7673

7774
#
78-
PG_CPPFLAGS = -I$(libpq_srcdir) ${PTHREAD_CFLAGS} -I$(top_pbk_srcdir)/src -I$(BORROW_DIR)
75+
#PG_CPPFLAGS = -I$(libpq_srcdir) ${PTHREAD_CFLAGS} -I$(top_pbk_srcdir)/src -I$(BORROW_DIR)
76+
PG_CPPFLAGS = -I$(libpq_srcdir) ${PTHREAD_CFLAGS} -I$(top_pbk_srcdir)/src -I$(BORROW_DIR) -Isrc/compatibility -Isrc/utils
77+
PG_CPPFLAGS += -I$(top_pbk_srcdir)src/fu_util
7978
ifdef VPATH
8079
PG_CPPFLAGS += -Isrc
8180
endif
8281
override CPPFLAGS := -DFRONTEND $(CPPFLAGS) $(PG_CPPFLAGS)
8382
PG_LIBS_INTERNAL = $(libpq_pgport) ${PTHREAD_CFLAGS}
8483

8584
# additional dependencies on borrowed files
86-
src/backup.o src/catchup.o src/pg_probackup.o: $(BORROW_DIR)/streamutil.h
87-
src/stream.o $(BORROW_DIR)/receivelog.o $(BORROW_DIR)/streamutil.o $(BORROW_DIR)/walmethods.o: $(BORROW_DIR)/receivelog.h
88-
$(BORROW_DIR)/receivelog.h: $(BORROW_DIR)/walmethods.h
85+
src/backup.o src/catchup.o src/pg_probackup.o: src/compatibility/streamutil.h
86+
src/stream.o src/compatibility/receivelog.o src/compatibility/streamutil.o src/compatibility/walmethods.o: src/compatibility/receivelog.h
87+
src/compatibility/receivelog.h: src/compatibility/walmethods.h
8988

9089
# generate separate makefile to handle borrowed files
9190
borrowed.mk: $(firstword $(MAKEFILE_LIST))

src/backup.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -689,10 +689,8 @@ do_backup(InstanceState *instanceState, pgSetBackupParams *set_backup_params,
689689
check_system_identifiers(backup_conn, instance_config.pgdata);
690690

691691
/* below perform checks specific for backup command */
692-
#if PG_VERSION_NUM >= 110000
693692
if (!RetrieveWalSegSize(backup_conn))
694693
elog(ERROR, "Failed to retrieve wal_segment_size");
695-
#endif
696694

697695
get_ptrack_version(backup_conn, &nodeInfo);
698696
// elog(WARNING, "ptrack_version_num %d", ptrack_version_num);

src/catchup.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,8 @@ catchup_init_state(PGNodeInfo *source_node_info, const char *source_pgdata, cons
5757
/* Do some compatibility checks and fill basic info about PG instance */
5858
source_conn = pgdata_basic_setup(instance_config.conn_opt, source_node_info);
5959

60-
#if PG_VERSION_NUM >= 110000
6160
if (!RetrieveWalSegSize(source_conn))
6261
elog(ERROR, "Failed to retrieve wal_segment_size");
63-
#endif
6462

6563
get_ptrack_version(source_conn, source_node_info);
6664
if (source_node_info->ptrack_version_num > 0)

src/compatibility/file_compat.c

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
#include <c.h>
2+
#include <fcntl.h>
3+
#include <unistd.h>
4+
5+
#include "logging.h"
6+
#include "file_compat.h"
7+
/*vvs*/
8+
/*
9+
* fsync_fname -- Try to fsync a file or directory
10+
*
11+
* Ignores errors trying to open unreadable files, or trying to fsync
12+
* directories on systems where that isn't allowed/required. All other errors
13+
* are fatal.
14+
*/
15+
int
16+
fsync_fname_compat(const char* fname, bool isdir)
17+
{
18+
int fd;
19+
int flags;
20+
int returncode;
21+
22+
/*
23+
* Some OSs require directories to be opened read-only whereas other
24+
* systems don't allow us to fsync files opened read-only; so we need both
25+
* cases here. Using O_RDWR will cause us to fail to fsync files that are
26+
* not writable by our userid, but we assume that's OK.
27+
*/
28+
flags = PG_BINARY;
29+
if (!isdir)
30+
flags |= O_RDWR;
31+
else
32+
flags |= O_RDONLY;
33+
34+
/*
35+
* Open the file, silently ignoring errors about unreadable files (or
36+
* unsupported operations, e.g. opening a directory under Windows), and
37+
* logging others.
38+
*/
39+
fd = open(fname, flags, 0);
40+
if (fd < 0)
41+
{
42+
if (errno == EACCES || (isdir && errno == EISDIR))
43+
return 0;
44+
pg_log_error("could not open file \"%s\": %m", fname);
45+
return -1;
46+
}
47+
48+
returncode = fsync(fd);
49+
50+
/*
51+
* Some OSes don't allow us to fsync directories at all, so we can ignore
52+
* those errors. Anything else needs to be reported.
53+
*/
54+
if (returncode != 0 && !(isdir && (errno == EBADF || errno == EINVAL)))
55+
{
56+
pg_log_fatal("could not fsync file \"%s\": %m", fname);
57+
(void)close(fd);
58+
exit(EXIT_FAILURE);
59+
}
60+
61+
(void)close(fd);
62+
return 0;
63+
}
64+
65+
/*
66+
* fsync_parent_path -- fsync the parent path of a file or directory
67+
*
68+
* This is aimed at making file operations persistent on disk in case of
69+
* an OS crash or power failure.
70+
*/
71+
int
72+
fsync_parent_path_compat(const char* fname)
73+
{
74+
char parentpath[MAXPGPATH];
75+
76+
strlcpy(parentpath, fname, MAXPGPATH);
77+
get_parent_directory(parentpath);
78+
79+
/*
80+
* get_parent_directory() returns an empty string if the input argument is
81+
* just a file name (see comments in path.c), so handle that as being the
82+
* current directory.
83+
*/
84+
if (strlen(parentpath) == 0)
85+
strlcpy(parentpath, ".", MAXPGPATH);
86+
87+
if (fsync_fname_compat(parentpath, true) != 0)
88+
return -1;
89+
90+
return 0;
91+
}
92+
93+
/*
94+
* durable_rename -- rename(2) wrapper, issuing fsyncs required for durability
95+
*
96+
* Wrapper around rename, similar to the backend version.
97+
*/
98+
int
99+
durable_rename_compat(const char* oldfile, const char* newfile)
100+
{
101+
int fd;
102+
103+
/*
104+
* First fsync the old and target path (if it exists), to ensure that they
105+
* are properly persistent on disk. Syncing the target file is not
106+
* strictly necessary, but it makes it easier to reason about crashes;
107+
* because it's then guaranteed that either source or target file exists
108+
* after a crash.
109+
*/
110+
if (fsync_fname_compat(oldfile, false) != 0)
111+
return -1;
112+
113+
fd = open(newfile, PG_BINARY | O_RDWR, 0);
114+
if (fd < 0)
115+
{
116+
if (errno != ENOENT)
117+
{
118+
pg_log_error("could not open file \"%s\": %m", newfile);
119+
return -1;
120+
}
121+
}
122+
else
123+
{
124+
if (fsync(fd) != 0)
125+
{
126+
pg_log_fatal("could not fsync file \"%s\": %m", newfile);
127+
close(fd);
128+
exit(EXIT_FAILURE);
129+
}
130+
close(fd);
131+
}
132+
133+
/* Time to do the real deal... */
134+
if (rename(oldfile, newfile) != 0)
135+
{
136+
pg_log_error("could not rename file \"%s\" to \"%s\": %m",
137+
oldfile, newfile);
138+
return -1;
139+
}
140+
141+
/*
142+
* To guarantee renaming the file is persistent, fsync the file with its
143+
* new name, and its containing directory.
144+
*/
145+
if (fsync_fname_compat(newfile, false) != 0)
146+
return -1;
147+
148+
if (fsync_parent_path_compat(newfile) != 0)
149+
return -1;
150+
151+
return 0;
152+
}

src/compatibility/file_compat.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ifndef FILE_COMPAT_H
2+
#define FILE_COMPAT_H
3+
4+
#include <sys/stat.h>
5+
#include "datatype/timestamp.h"
6+
7+
8+
#if PG_VERSION_NUM >= 120000
9+
#include "common/logging.h"
10+
#else
11+
#include "logging.h"
12+
#endif
13+
14+
15+
extern int fsync_parent_path_compat(const char* fname);
16+
extern int fsync_fname_compat(const char* fname, bool isdir);
17+
extern int durable_rename_compat(const char* oldfile, const char* newfile);
18+
19+
20+
#if PG_VERSION_NUM < 110000
21+
#include "file_compat10.h"
22+
#else
23+
#include "common/file_perm.h"
24+
#include "access/xlog_internal.h"
25+
#endif
26+
27+
28+
29+
#endif /* FILE_COMPAT_H */
30+
31+
32+

src/compatibility/file_compat10.c

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include <c.h>
2+
3+
#if PG_VERSION_NUM < 110000
4+
5+
#include <fcntl.h>
6+
#include <unistd.h>
7+
#include "logging.h"
8+
9+
#include "file_compat.h"
10+
11+
/* Modes for creating directories and files in the data directory */
12+
int pg_dir_create_mode = PG_DIR_MODE_OWNER;
13+
int pg_file_create_mode = PG_FILE_MODE_OWNER;
14+
/*
15+
* Mode mask to pass to umask(). This is more of a preventative measure since
16+
* all file/directory creates should be performed using the create modes above.
17+
*/
18+
int pg_mode_mask = PG_MODE_MASK_OWNER;
19+
20+
21+
/*
22+
* Set create modes and mask to use when writing to PGDATA based on the data
23+
* directory mode passed. If group read/execute are present in the mode, then
24+
* create modes and mask will be relaxed to allow group read/execute on all
25+
* newly created files and directories.
26+
*/
27+
void
28+
SetDataDirectoryCreatePerm(int dataDirMode)
29+
{
30+
/* If the data directory mode has group access */
31+
if ((PG_DIR_MODE_GROUP & dataDirMode) == PG_DIR_MODE_GROUP)
32+
{
33+
pg_dir_create_mode = PG_DIR_MODE_GROUP;
34+
pg_file_create_mode = PG_FILE_MODE_GROUP;
35+
pg_mode_mask = PG_MODE_MASK_GROUP;
36+
}
37+
/* Else use default permissions */
38+
else
39+
{
40+
pg_dir_create_mode = PG_DIR_MODE_OWNER;
41+
pg_file_create_mode = PG_FILE_MODE_OWNER;
42+
pg_mode_mask = PG_MODE_MASK_OWNER;
43+
}
44+
}
45+
46+
47+
48+
/*
49+
* Get the create modes and mask to use when writing to PGDATA by examining the
50+
* mode of the PGDATA directory and calling SetDataDirectoryCreatePerm().
51+
*
52+
* Errors are not handled here and should be reported by the application when
53+
* false is returned.
54+
*
55+
* Suppress when on Windows, because there may not be proper support for Unix-y
56+
* file permissions.
57+
*/
58+
bool
59+
GetDataDirectoryCreatePerm(const char* dataDir)
60+
{
61+
#if !defined(WIN32) && !defined(__CYGWIN__)
62+
struct stat statBuf;
63+
64+
/*
65+
* If an error occurs getting the mode then return false. The caller is
66+
* responsible for generating an error, if appropriate, indicating that we
67+
* were unable to access the data directory.
68+
*/
69+
if (stat(dataDir, &statBuf) == -1)
70+
return false;
71+
72+
/* Set permissions */
73+
SetDataDirectoryCreatePerm(statBuf.st_mode);
74+
return true;
75+
#else /* !defined(WIN32) && !defined(__CYGWIN__) */
76+
/*
77+
* On Windows, we don't have anything to do here since they don't have
78+
* Unix-y permissions.
79+
*/
80+
return true;
81+
#endif
82+
}
83+
84+
#endif

0 commit comments

Comments
 (0)