|
16 | 16 |
|
17 | 17 | static void check_data_dir(ClusterInfo *cluster);
|
18 | 18 | static void check_bin_dir(ClusterInfo *cluster);
|
| 19 | +static void get_bin_version(ClusterInfo *cluster); |
19 | 20 | static void validate_exec(const char *dir, const char *cmdName);
|
20 | 21 |
|
21 | 22 | #ifdef WIN32
|
22 | 23 | static int win32_check_directory_write_permissions(void);
|
23 | 24 | #endif
|
24 | 25 |
|
25 | 26 |
|
| 27 | +/* |
| 28 | + * get_bin_version |
| 29 | + * |
| 30 | + * Fetch versions of binaries for cluster. |
| 31 | + */ |
| 32 | +static void |
| 33 | +get_bin_version(ClusterInfo *cluster) |
| 34 | +{ |
| 35 | + char cmd[MAXPGPATH], |
| 36 | + cmd_output[MAX_STRING]; |
| 37 | + FILE *output; |
| 38 | + int pre_dot = 0, |
| 39 | + post_dot = 0; |
| 40 | + |
| 41 | + snprintf(cmd, sizeof(cmd), "\"%s/pg_ctl\" --version", cluster->bindir); |
| 42 | + |
| 43 | + if ((output = popen(cmd, "r")) == NULL || |
| 44 | + fgets(cmd_output, sizeof(cmd_output), output) == NULL) |
| 45 | + pg_fatal("could not get pg_ctl version data using %s: %s\n", |
| 46 | + cmd, strerror(errno)); |
| 47 | + |
| 48 | + pclose(output); |
| 49 | + |
| 50 | + /* Remove trailing newline */ |
| 51 | + if (strchr(cmd_output, '\n') != NULL) |
| 52 | + *strchr(cmd_output, '\n') = '\0'; |
| 53 | + |
| 54 | + if (sscanf(cmd_output, "%*s %*s %d.%d", &pre_dot, &post_dot) < 1) |
| 55 | + pg_fatal("could not get version from %s\n", cmd); |
| 56 | + |
| 57 | + cluster->bin_version = (pre_dot * 100 + post_dot) * 100; |
| 58 | +} |
| 59 | + |
| 60 | + |
26 | 61 | /*
|
27 | 62 | * exec_prog()
|
28 | 63 | * Execute an external program with stdout/stderr redirected, and report
|
@@ -335,7 +370,20 @@ check_bin_dir(ClusterInfo *cluster)
|
335 | 370 |
|
336 | 371 | validate_exec(cluster->bindir, "postgres");
|
337 | 372 | validate_exec(cluster->bindir, "pg_ctl");
|
338 |
| - validate_exec(cluster->bindir, "pg_resetwal"); |
| 373 | + |
| 374 | + /* |
| 375 | + * Fetch the binary versions after checking for the existence of pg_ctl, |
| 376 | + * this gives a correct error if the binary used itself for the version |
| 377 | + * fetching is broken. |
| 378 | + */ |
| 379 | + get_bin_version(&old_cluster); |
| 380 | + get_bin_version(&new_cluster); |
| 381 | + |
| 382 | + /* pg_resetxlog has been renamed to pg_resetwal in version 10 */ |
| 383 | + if (GET_MAJOR_VERSION(cluster->bin_version) < 1000) |
| 384 | + validate_exec(cluster->bindir, "pg_resetxlog"); |
| 385 | + else |
| 386 | + validate_exec(cluster->bindir, "pg_resetwal"); |
339 | 387 | if (cluster == &new_cluster)
|
340 | 388 | {
|
341 | 389 | /* these are only needed in the new cluster */
|
|
0 commit comments