Skip to content

Commit 2284c2b

Browse files
committed
Refactoring. Introduce catalogState structure. Update code in init.c
1 parent 7ef802d commit 2284c2b

File tree

3 files changed

+53
-29
lines changed

3 files changed

+53
-29
lines changed

src/init.c

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,42 +17,37 @@
1717
* Initialize backup catalog.
1818
*/
1919
int
20-
do_init(char *backup_catalog_path)
20+
do_init(CatalogState *catalogState)
2121
{
22-
char path[MAXPGPATH];
23-
char arclog_path_dir[MAXPGPATH];
2422
int results;
2523

26-
results = pg_check_dir(backup_catalog_path);
24+
results = pg_check_dir(catalogState->catalog_path);
25+
2726
if (results == 4) /* exists and not empty*/
2827
elog(ERROR, "backup catalog already exist and it's not empty");
2928
else if (results == -1) /*trouble accessing directory*/
3029
{
3130
int errno_tmp = errno;
3231
elog(ERROR, "cannot open backup catalog directory \"%s\": %s",
33-
backup_catalog_path, strerror(errno_tmp));
32+
catalogState->catalog_path, strerror(errno_tmp));
3433
}
3534

3635
/* create backup catalog root directory */
37-
dir_create_dir(backup_catalog_path, DIR_PERMISSION, false);
36+
dir_create_dir(catalogState->catalog_path, DIR_PERMISSION, false);
3837

3938
/* create backup catalog data directory */
40-
join_path_components(path, backup_catalog_path, BACKUPS_DIR);
41-
dir_create_dir(path, DIR_PERMISSION, false);
39+
dir_create_dir(catalogState->backup_subdir_path, DIR_PERMISSION, false);
4240

4341
/* create backup catalog wal directory */
44-
join_path_components(arclog_path_dir, backup_catalog_path, "wal");
45-
dir_create_dir(arclog_path_dir, DIR_PERMISSION, false);
42+
dir_create_dir(catalogState->wal_subdir_path, DIR_PERMISSION, false);
4643

47-
elog(INFO, "Backup catalog '%s' successfully inited", backup_catalog_path);
44+
elog(INFO, "Backup catalog '%s' successfully inited", catalogState->catalog_path);
4845
return 0;
4946
}
5047

5148
int
52-
do_add_instance(char *backup_catalog_path, InstanceConfig *instance)
49+
do_add_instance(CatalogState *catalogState, InstanceConfig *instance)
5350
{
54-
char path[MAXPGPATH];
55-
char arclog_path_dir[MAXPGPATH];
5651
struct stat st;
5752

5853
/* PGDATA is always required */
@@ -66,16 +61,15 @@ do_add_instance(char *backup_catalog_path, InstanceConfig *instance)
6661
instance->xlog_seg_size = get_xlog_seg_size(instance->pgdata);
6762

6863
/* Ensure that all root directories already exist */
69-
if (access(backup_catalog_path, F_OK) != 0)
70-
elog(ERROR, "Directory does not exist: '%s'", backup_catalog_path);
64+
/* TODO maybe call do_init() here instead of error?*/
65+
if (access(catalogState->catalog_path, F_OK) != 0)
66+
elog(ERROR, "Directory does not exist: '%s'", catalogState->catalog_path);
7167

72-
join_path_components(path, backup_catalog_path, BACKUPS_DIR);
73-
if (access(path, F_OK) != 0)
74-
elog(ERROR, "Directory does not exist: '%s'", path);
68+
if (access(catalogState->backup_subdir_path, F_OK) != 0)
69+
elog(ERROR, "Directory does not exist: '%s'", catalogState->backup_subdir_path);
7570

76-
join_path_components(arclog_path_dir, backup_catalog_path, "wal");
77-
if (access(arclog_path_dir, F_OK) != 0)
78-
elog(ERROR, "Directory does not exist: '%s'", arclog_path_dir);
71+
if (access(catalogState->wal_subdir_path, F_OK) != 0)
72+
elog(ERROR, "Directory does not exist: '%s'", catalogState->wal_subdir_path);
7973

8074
if (stat(instance->backup_instance_path, &st) == 0 && S_ISDIR(st.st_mode))
8175
elog(ERROR, "Instance '%s' backup directory already exists: '%s'",

src/pg_probackup.c

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ char backup_instance_path[MAXPGPATH];
7171
*/
7272
char arclog_path[MAXPGPATH] = "";
7373

74-
/* ================ catalogState (END) =========== */
75-
7674

75+
static CatalogState *catalogState = NULL;
76+
/* ================ catalogState (END) =========== */
7777

7878
/* colon separated external directories list ("/path1:/path2") */
7979
char *externaldir = NULL;
@@ -424,6 +424,7 @@ main(int argc, char *argv[])
424424
/* set location based on cmdline options only */
425425
setMyLocation(backup_subcmd);
426426

427+
/* ===== catalogState ======*/
427428
if (backup_path == NULL)
428429
{
429430
/*
@@ -440,11 +441,24 @@ main(int argc, char *argv[])
440441
/* Ensure that backup_path is an absolute path */
441442
if (!is_absolute_path(backup_path))
442443
elog(ERROR, "-B, --backup-path must be an absolute path");
444+
445+
catalogState = pgut_new(CatalogState);
446+
strncpy(catalogState->catalog_path, backup_path, MAXPGPATH);
447+
join_path_components(catalogState->backup_subdir_path,
448+
catalogState->catalog_path, BACKUPS_DIR);
449+
join_path_components(catalogState->wal_subdir_path,
450+
catalogState->catalog_path, WAL_SUBDIR);
443451
}
452+
444453
/* backup_path is required for all pg_probackup commands except help, version and checkdb */
445-
if (backup_path == NULL && backup_subcmd != CHECKDB_CMD && backup_subcmd != HELP_CMD && backup_subcmd != VERSION_CMD)
454+
if (backup_path == NULL &&
455+
backup_subcmd != CHECKDB_CMD &&
456+
backup_subcmd != HELP_CMD &&
457+
backup_subcmd != VERSION_CMD)
446458
elog(ERROR, "required parameter not specified: BACKUP_PATH (-B, --backup-path)");
447459

460+
/* ===== catalogState (END) ======*/
461+
448462
/*
449463
* Option --instance is required for all commands except
450464
* init, show, checkdb and validate
@@ -772,11 +786,11 @@ main(int argc, char *argv[])
772786
wal_file_path, wal_file_name, batch_size, !no_validate_wal);
773787
break;
774788
case ADD_INSTANCE_CMD:
775-
return do_add_instance(backup_path, &instance_config);
789+
return do_add_instance(catalogState, &instance_config);
776790
case DELETE_INSTANCE_CMD:
777791
return do_delete_instance();
778792
case INIT_CMD:
779-
return do_init(backup_path);
793+
return do_init(catalogState);
780794
case BACKUP_CMD:
781795
{
782796
current.stream = stream_wal;

src/pg_probackup.h

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ extern const char *PROGRAM_EMAIL;
5555
/* Directory/File names */
5656
#define DATABASE_DIR "database"
5757
#define BACKUPS_DIR "backups"
58+
#define WAL_SUBDIR "wal"
5859
#if PG_VERSION_NUM >= 100000
5960
#define PG_XLOG_DIR "pg_wal"
6061
#define PG_LOG_DIR "log"
@@ -129,6 +130,7 @@ extern const char *PROGRAM_EMAIL;
129130
#define TC_CYAN_BOLD "\033[1;36m"
130131
#define TC_RESET "\033[0m"
131132

133+
132134
typedef struct RedoParams
133135
{
134136
TimeLineID tli;
@@ -746,11 +748,25 @@ typedef struct BackupPageHeader2
746748

747749
#define IsSshProtocol() (instance_config.remote.host && strcmp(instance_config.remote.proto, "ssh") == 0)
748750

751+
/* ====== CatalogState ======= */
752+
749753
/* directory options */
750754
extern char *backup_path;
751755
extern char backup_instance_path[MAXPGPATH];
752756
extern char arclog_path[MAXPGPATH];
753757

758+
typedef struct CatalogState
759+
{
760+
/* $BACKUP_PATH */
761+
char catalog_path[MAXPGPATH]; //previously global var backup_path
762+
/* $BACKUP_PATH/backups */
763+
char backup_subdir_path[MAXPGPATH];
764+
/* $BACKUP_PATH/wal */
765+
char wal_subdir_path[MAXPGPATH]; // previously global var arclog_path
766+
} CatalogState;
767+
768+
/* ====== CatalogState (END) ======= */
769+
754770
/* common options */
755771
extern pid_t my_pid;
756772
extern __thread int my_thread_num;
@@ -839,8 +855,8 @@ extern void merge_chain(parray *parent_chain,
839855
extern parray *read_database_map(pgBackup *backup);
840856

841857
/* in init.c */
842-
extern int do_init(char *backup_catalog_path);
843-
extern int do_add_instance(char *backup_catalog_path, InstanceConfig *instance);
858+
extern int do_init(CatalogState *catalogState);
859+
extern int do_add_instance(CatalogState *catalogState, InstanceConfig *instance);
844860

845861
/* in archive.c */
846862
extern void do_archive_push(InstanceConfig *instance, char *wal_file_path,

0 commit comments

Comments
 (0)