From fff5c6a6f912e2822e07381fa57dc32082475efc Mon Sep 17 00:00:00 2001 From: Amul Sul Date: Thu, 15 Feb 2024 14:33:12 +0530 Subject: [PATCH v7 2/4] Code refactor: get_controlfile() to accept full path of control file The current version get_controlfile() accepts data directory path, and computes the full path for the pg_control file, but it would be better to have a version that accepts the full path of that pg_control file. --- src/backend/utils/misc/pg_controldata.c | 8 ++++---- src/bin/pg_checksums/pg_checksums.c | 2 +- src/bin/pg_controldata/pg_controldata.c | 2 +- src/bin/pg_ctl/pg_ctl.c | 2 +- src/common/controldata_utils.c | 19 ++++++++++++++++--- src/include/common/controldata_utils.h | 3 ++- 6 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/backend/utils/misc/pg_controldata.c b/src/backend/utils/misc/pg_controldata.c index 55435dbcf3a..ea8eb3624d9 100644 --- a/src/backend/utils/misc/pg_controldata.c +++ b/src/backend/utils/misc/pg_controldata.c @@ -44,7 +44,7 @@ pg_control_system(PG_FUNCTION_ARGS) /* read the control file */ LWLockAcquire(ControlFileLock, LW_SHARED); - ControlFile = get_controlfile(DataDir, &crc_ok); + ControlFile = get_dir_controlfile(DataDir, &crc_ok); LWLockRelease(ControlFileLock); if (!crc_ok) ereport(ERROR, @@ -84,7 +84,7 @@ pg_control_checkpoint(PG_FUNCTION_ARGS) /* Read the control file. */ LWLockAcquire(ControlFileLock, LW_SHARED); - ControlFile = get_controlfile(DataDir, &crc_ok); + ControlFile = get_dir_controlfile(DataDir, &crc_ok); LWLockRelease(ControlFileLock); if (!crc_ok) ereport(ERROR, @@ -175,7 +175,7 @@ pg_control_recovery(PG_FUNCTION_ARGS) /* read the control file */ LWLockAcquire(ControlFileLock, LW_SHARED); - ControlFile = get_controlfile(DataDir, &crc_ok); + ControlFile = get_dir_controlfile(DataDir, &crc_ok); LWLockRelease(ControlFileLock); if (!crc_ok) ereport(ERROR, @@ -216,7 +216,7 @@ pg_control_init(PG_FUNCTION_ARGS) /* read the control file */ LWLockAcquire(ControlFileLock, LW_SHARED); - ControlFile = get_controlfile(DataDir, &crc_ok); + ControlFile = get_dir_controlfile(DataDir, &crc_ok); LWLockRelease(ControlFileLock); if (!crc_ok) ereport(ERROR, diff --git a/src/bin/pg_checksums/pg_checksums.c b/src/bin/pg_checksums/pg_checksums.c index 9e6fd435f60..4bdd85f42e0 100644 --- a/src/bin/pg_checksums/pg_checksums.c +++ b/src/bin/pg_checksums/pg_checksums.c @@ -545,7 +545,7 @@ main(int argc, char *argv[]) } /* Read the control file and check compatibility */ - ControlFile = get_controlfile(DataDir, &crc_ok); + ControlFile = get_dir_controlfile(DataDir, &crc_ok); if (!crc_ok) pg_fatal("pg_control CRC value is incorrect"); diff --git a/src/bin/pg_controldata/pg_controldata.c b/src/bin/pg_controldata/pg_controldata.c index 93e0837947c..1e615131612 100644 --- a/src/bin/pg_controldata/pg_controldata.c +++ b/src/bin/pg_controldata/pg_controldata.c @@ -165,7 +165,7 @@ main(int argc, char *argv[]) } /* get a copy of the control file */ - ControlFile = get_controlfile(DataDir, &crc_ok); + ControlFile = get_dir_controlfile(DataDir, &crc_ok); if (!crc_ok) { pg_log_warning("calculated CRC checksum does not match value stored in control file"); diff --git a/src/bin/pg_ctl/pg_ctl.c b/src/bin/pg_ctl/pg_ctl.c index 6900b27675e..1d887c1b9df 100644 --- a/src/bin/pg_ctl/pg_ctl.c +++ b/src/bin/pg_ctl/pg_ctl.c @@ -2174,7 +2174,7 @@ get_control_dbstate(void) { DBState ret; bool crc_ok; - ControlFileData *control_file_data = get_controlfile(pg_data, &crc_ok); + ControlFileData *control_file_data = get_dir_controlfile(pg_data, &crc_ok); if (!crc_ok) { diff --git a/src/common/controldata_utils.c b/src/common/controldata_utils.c index 92e8fed6b2e..43566920fed 100644 --- a/src/common/controldata_utils.c +++ b/src/common/controldata_utils.c @@ -49,11 +49,10 @@ * file data is correct. */ ControlFileData * -get_controlfile(const char *DataDir, bool *crc_ok_p) +get_controlfile(const char *ControlFilePath, bool *crc_ok_p) { ControlFileData *ControlFile; int fd; - char ControlFilePath[MAXPGPATH]; pg_crc32c crc; int r; #ifdef FRONTEND @@ -64,7 +63,6 @@ get_controlfile(const char *DataDir, bool *crc_ok_p) Assert(crc_ok_p); ControlFile = palloc_object(ControlFileData); - snprintf(ControlFilePath, MAXPGPATH, "%s/global/pg_control", DataDir); #ifdef FRONTEND INIT_CRC32C(last_crc); @@ -162,6 +160,21 @@ retry: return ControlFile; } +/* + * get_dir_controlfile() + * + * Get controlfile values of the given data directory. + */ +ControlFileData * +get_dir_controlfile(const char *DataDir, bool *crc_ok_p) +{ + char ControlFilePath[MAXPGPATH]; + + snprintf(ControlFilePath, MAXPGPATH, "%s/global/pg_control", DataDir); + + return get_controlfile(ControlFilePath, crc_ok_p); +} + /* * update_controlfile() * diff --git a/src/include/common/controldata_utils.h b/src/include/common/controldata_utils.h index 04da70e87b2..56528360663 100644 --- a/src/include/common/controldata_utils.h +++ b/src/include/common/controldata_utils.h @@ -12,7 +12,8 @@ #include "catalog/pg_control.h" -extern ControlFileData *get_controlfile(const char *DataDir, bool *crc_ok_p); +extern ControlFileData *get_controlfile(const char *ControlFilePath, bool *crc_ok_p); +extern ControlFileData *get_dir_controlfile(const char *DataDir, bool *crc_ok_p); extern void update_controlfile(const char *DataDir, ControlFileData *ControlFile, bool do_sync); -- 2.18.0