From a3b2bf1fe7ce7cf88af6af2c100c6ed61c976780 Mon Sep 17 00:00:00 2001 From: Michael Paquier Date: Thu, 11 Jun 2020 15:48:46 +0900 Subject: Move frontend-side archive APIs from src/common/ to src/fe_utils/ fe_archive.c was compiled only for the frontend in src/common/, but as it will never share anything with the backend, it makes most sense to move this file to src/fe_utils/. Reported-by: Peter Eisentraut Discussion: https://postgr.es/m/e9766d71-8655-ac86-bdf6-77e0e7169977@2ndquadrant.com Backpatch-through: 13 --- src/bin/pg_rewind/parsexlog.c | 2 +- src/common/Makefile | 1 - src/common/fe_archive.c | 128 ---------------------------------------- src/fe_utils/Makefile | 1 + src/fe_utils/archive.c | 124 ++++++++++++++++++++++++++++++++++++++ src/include/common/fe_archive.h | 21 ------- src/include/fe_utils/archive.h | 21 +++++++ src/tools/msvc/Mkvcbuild.pm | 8 +-- 8 files changed, 151 insertions(+), 155 deletions(-) delete mode 100644 src/common/fe_archive.c create mode 100644 src/fe_utils/archive.c delete mode 100644 src/include/common/fe_archive.h create mode 100644 src/include/fe_utils/archive.h diff --git a/src/bin/pg_rewind/parsexlog.c b/src/bin/pg_rewind/parsexlog.c index d637f5eb771..bc6f9769941 100644 --- a/src/bin/pg_rewind/parsexlog.c +++ b/src/bin/pg_rewind/parsexlog.c @@ -19,7 +19,7 @@ #include "catalog/pg_control.h" #include "catalog/storage_xlog.h" #include "commands/dbcommands_xlog.h" -#include "common/fe_archive.h" +#include "fe_utils/archive.h" #include "filemap.h" #include "pg_rewind.h" diff --git a/src/common/Makefile b/src/common/Makefile index d0be882cca4..16619e4ba88 100644 --- a/src/common/Makefile +++ b/src/common/Makefile @@ -89,7 +89,6 @@ endif # (Mkvcbuild.pm has a copy of this list, too) OBJS_FRONTEND = \ $(OBJS_COMMON) \ - fe_archive.o \ fe_memutils.o \ file_utils.o \ logging.o \ diff --git a/src/common/fe_archive.c b/src/common/fe_archive.c deleted file mode 100644 index b0d68870db8..00000000000 --- a/src/common/fe_archive.c +++ /dev/null @@ -1,128 +0,0 @@ -/*------------------------------------------------------------------------- - * - * fe_archive.c - * Routines to access WAL archives from frontend - * - * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group - * Portions Copyright (c) 1994, Regents of the University of California - * - * - * IDENTIFICATION - * src/common/fe_archive.c - * - *------------------------------------------------------------------------- - */ - -#ifndef FRONTEND -#error "This file is not expected to be compiled for backend code" -#endif - -#include "postgres_fe.h" - -#include -#include - -#include "access/xlog_internal.h" -#include "common/archive.h" -#include "common/fe_archive.h" -#include "common/logging.h" - - -/* - * RestoreArchivedFile - * - * Attempt to retrieve the specified file from off-line archival storage. - * If successful, return a file descriptor of the restored file, else - * return -1. - * - * For fixed-size files, the caller may pass the expected size as an - * additional crosscheck on successful recovery. If the file size is not - * known, set expectedSize = 0. - */ -int -RestoreArchivedFile(const char *path, const char *xlogfname, - off_t expectedSize, const char *restoreCommand) -{ - char xlogpath[MAXPGPATH]; - char *xlogRestoreCmd; - int rc; - struct stat stat_buf; - - snprintf(xlogpath, MAXPGPATH, "%s/" XLOGDIR "/%s", path, xlogfname); - - xlogRestoreCmd = BuildRestoreCommand(restoreCommand, xlogpath, - xlogfname, NULL); - if (xlogRestoreCmd == NULL) - { - pg_log_fatal("could not use restore_command with %%r alias"); - exit(1); - } - - /* - * Execute restore_command, which should copy the missing file from - * archival storage. - */ - rc = system(xlogRestoreCmd); - pfree(xlogRestoreCmd); - - if (rc == 0) - { - /* - * Command apparently succeeded, but let's make sure the file is - * really there now and has the correct size. - */ - if (stat(xlogpath, &stat_buf) == 0) - { - if (expectedSize > 0 && stat_buf.st_size != expectedSize) - { - pg_log_fatal("unexpected file size for \"%s\": %lu instead of %lu", - xlogfname, (unsigned long) stat_buf.st_size, - (unsigned long) expectedSize); - exit(1); - } - else - { - int xlogfd = open(xlogpath, O_RDONLY | PG_BINARY, 0); - - if (xlogfd < 0) - { - pg_log_fatal("could not open file \"%s\" restored from archive: %m", - xlogpath); - exit(1); - } - else - return xlogfd; - } - } - else - { - if (errno != ENOENT) - { - pg_log_fatal("could not stat file \"%s\": %m", - xlogpath); - exit(1); - } - } - } - - /* - * If the failure was due to a signal, then it would be misleading to - * return with a failure at restoring the file. So just bail out and - * exit. Hard shell errors such as "command not found" are treated as - * fatal too. - */ - if (wait_result_is_any_signal(rc, true)) - { - pg_log_fatal("restore_command failed due to the signal: %s", - wait_result_to_str(rc)); - exit(1); - } - - /* - * The file is not available, so just let the caller decide what to do - * next. - */ - pg_log_error("could not restore file \"%s\" from archive", - xlogfname); - return -1; -} diff --git a/src/fe_utils/Makefile b/src/fe_utils/Makefile index 9eb4417690b..dd206636047 100644 --- a/src/fe_utils/Makefile +++ b/src/fe_utils/Makefile @@ -20,6 +20,7 @@ include $(top_builddir)/src/Makefile.global override CPPFLAGS := -DFRONTEND -I$(libpq_srcdir) $(CPPFLAGS) OBJS = \ + archive.o \ cancel.o \ conditional.o \ mbprint.o \ diff --git a/src/fe_utils/archive.c b/src/fe_utils/archive.c new file mode 100644 index 00000000000..c4cb2131985 --- /dev/null +++ b/src/fe_utils/archive.c @@ -0,0 +1,124 @@ +/*------------------------------------------------------------------------- + * + * archive.c + * Routines to access WAL archives from frontend + * + * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * + * IDENTIFICATION + * src/fe_utils/archive.c + * + *------------------------------------------------------------------------- + */ + +#include "postgres_fe.h" + +#include +#include + +#include "access/xlog_internal.h" +#include "common/archive.h" +#include "common/logging.h" +#include "fe_utils/archive.h" + + +/* + * RestoreArchivedFile + * + * Attempt to retrieve the specified file from off-line archival storage. + * If successful, return a file descriptor of the restored file, else + * return -1. + * + * For fixed-size files, the caller may pass the expected size as an + * additional crosscheck on successful recovery. If the file size is not + * known, set expectedSize = 0. + */ +int +RestoreArchivedFile(const char *path, const char *xlogfname, + off_t expectedSize, const char *restoreCommand) +{ + char xlogpath[MAXPGPATH]; + char *xlogRestoreCmd; + int rc; + struct stat stat_buf; + + snprintf(xlogpath, MAXPGPATH, "%s/" XLOGDIR "/%s", path, xlogfname); + + xlogRestoreCmd = BuildRestoreCommand(restoreCommand, xlogpath, + xlogfname, NULL); + if (xlogRestoreCmd == NULL) + { + pg_log_fatal("could not use restore_command with %%r alias"); + exit(1); + } + + /* + * Execute restore_command, which should copy the missing file from + * archival storage. + */ + rc = system(xlogRestoreCmd); + pfree(xlogRestoreCmd); + + if (rc == 0) + { + /* + * Command apparently succeeded, but let's make sure the file is + * really there now and has the correct size. + */ + if (stat(xlogpath, &stat_buf) == 0) + { + if (expectedSize > 0 && stat_buf.st_size != expectedSize) + { + pg_log_fatal("unexpected file size for \"%s\": %lu instead of %lu", + xlogfname, (unsigned long) stat_buf.st_size, + (unsigned long) expectedSize); + exit(1); + } + else + { + int xlogfd = open(xlogpath, O_RDONLY | PG_BINARY, 0); + + if (xlogfd < 0) + { + pg_log_fatal("could not open file \"%s\" restored from archive: %m", + xlogpath); + exit(1); + } + else + return xlogfd; + } + } + else + { + if (errno != ENOENT) + { + pg_log_fatal("could not stat file \"%s\": %m", + xlogpath); + exit(1); + } + } + } + + /* + * If the failure was due to a signal, then it would be misleading to + * return with a failure at restoring the file. So just bail out and + * exit. Hard shell errors such as "command not found" are treated as + * fatal too. + */ + if (wait_result_is_any_signal(rc, true)) + { + pg_log_fatal("restore_command failed due to the signal: %s", + wait_result_to_str(rc)); + exit(1); + } + + /* + * The file is not available, so just let the caller decide what to do + * next. + */ + pg_log_error("could not restore file \"%s\" from archive", + xlogfname); + return -1; +} diff --git a/src/include/common/fe_archive.h b/src/include/common/fe_archive.h deleted file mode 100644 index 495b560d245..00000000000 --- a/src/include/common/fe_archive.h +++ /dev/null @@ -1,21 +0,0 @@ -/*------------------------------------------------------------------------- - * - * fe_archive.h - * Routines to access WAL archives from frontend - * - * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group - * Portions Copyright (c) 1994, Regents of the University of California - * - * src/include/common/fe_archive.h - * - *------------------------------------------------------------------------- - */ -#ifndef FE_ARCHIVE_H -#define FE_ARCHIVE_H - -extern int RestoreArchivedFile(const char *path, - const char *xlogfname, - off_t expectedSize, - const char *restoreCommand); - -#endif /* FE_ARCHIVE_H */ diff --git a/src/include/fe_utils/archive.h b/src/include/fe_utils/archive.h new file mode 100644 index 00000000000..a6beaf04ea7 --- /dev/null +++ b/src/include/fe_utils/archive.h @@ -0,0 +1,21 @@ +/*------------------------------------------------------------------------- + * + * archive.h + * Routines to access WAL archives from frontend + * + * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * src/include/fe_utils/archive.h + * + *------------------------------------------------------------------------- + */ +#ifndef FE_ARCHIVE_H +#define FE_ARCHIVE_H + +extern int RestoreArchivedFile(const char *path, + const char *xlogfname, + off_t expectedSize, + const char *restoreCommand); + +#endif /* FE_ARCHIVE_H */ diff --git a/src/tools/msvc/Mkvcbuild.pm b/src/tools/msvc/Mkvcbuild.pm index c21c94dc1f7..20da7985c10 100644 --- a/src/tools/msvc/Mkvcbuild.pm +++ b/src/tools/msvc/Mkvcbuild.pm @@ -138,14 +138,14 @@ sub mkvcbuild } our @pgcommonfrontendfiles = ( - @pgcommonallfiles, qw(fe_archive.c fe_memutils.c - file_utils.c logging.c restricted_token.c)); + @pgcommonallfiles, qw(fe_memutils.c file_utils.c + logging.c restricted_token.c)); our @pgcommonbkndfiles = @pgcommonallfiles; our @pgfeutilsfiles = qw( - cancel.c conditional.c mbprint.c print.c psqlscan.l psqlscan.c - simple_list.c string_utils.c recovery_gen.c); + archive.c cancel.c conditional.c mbprint.c print.c psqlscan.l + psqlscan.c simple_list.c string_utils.c recovery_gen.c); $libpgport = $solution->AddProject('libpgport', 'lib', 'misc'); $libpgport->AddDefine('FRONTEND'); -- cgit v1.2.3