Skip to content

Commit 4397c51

Browse files
committed
Use correct path separator for Windows builtin commands.
pg_upgrade produces a platform-specific script to remove the old directory, but on Windows it has not been making sure that the paths it writes as arguments for rmdir and del use the backslash path separator, which will cause these scripts to fail. The fix is backpatched to Release 9.0.
1 parent 1da2a61 commit 4397c51

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

contrib/pg_upgrade/check.c

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,35 @@ static void check_for_isn_and_int8_passing_mismatch(migratorContext *ctx,
1919
static void check_for_reg_data_type_usage(migratorContext *ctx, Cluster whichCluster);
2020

2121

22+
/*
23+
* fix_path_separator
24+
* For non-Windows, just return the argument.
25+
* For Windows convert any forward slash to a backslash
26+
* such as is suitable for arguments to builtin commands
27+
* like RMDIR and DEL.
28+
*/
29+
static char *fix_path_separator(char *path)
30+
{
31+
#ifdef WIN32
32+
33+
char *result;
34+
char *c;
35+
36+
result = pg_strdup(path);
37+
38+
for (c = result; *c != '\0'; c++)
39+
if (*c == '/')
40+
*c = '\\';
41+
42+
return result;
43+
44+
#else
45+
46+
return path;
47+
48+
#endif
49+
}
50+
2251
void
2352
output_check_banner(migratorContext *ctx, bool *live_check)
2453
{
@@ -402,7 +431,7 @@ create_script_for_old_cluster_deletion(migratorContext *ctx,
402431
#endif
403432

404433
/* delete old cluster's default tablespace */
405-
fprintf(script, RMDIR_CMD " %s\n", ctx->old.pgdata);
434+
fprintf(script, RMDIR_CMD " %s\n", fix_path_separator(ctx->old.pgdata));
406435

407436
/* delete old cluster's alternate tablespaces */
408437
for (tblnum = 0; tblnum < ctx->num_tablespaces; tblnum++)
@@ -419,14 +448,17 @@ create_script_for_old_cluster_deletion(migratorContext *ctx,
419448
fprintf(script, "\n");
420449
/* remove PG_VERSION? */
421450
if (GET_MAJOR_VERSION(ctx->old.major_version) <= 804)
422-
fprintf(script, RM_CMD " %s%s/PG_VERSION\n",
423-
ctx->tablespaces[tblnum], ctx->old.tablespace_suffix);
451+
fprintf(script, RM_CMD " %s%s%cPG_VERSION\n",
452+
fix_path_separator(ctx->tablespaces[tblnum]),
453+
fix_path_separator(ctx->old.tablespace_suffix),
454+
PATH_SEPARATOR);
424455

425456
for (dbnum = 0; dbnum < ctx->new.dbarr.ndbs; dbnum++)
426457
{
427-
fprintf(script, RMDIR_CMD " %s%s/%d\n",
428-
ctx->tablespaces[tblnum], ctx->old.tablespace_suffix,
429-
ctx->old.dbarr.dbs[dbnum].db_oid);
458+
fprintf(script, RMDIR_CMD " %s%s%c%d\n",
459+
fix_path_separator(ctx->tablespaces[tblnum]),
460+
fix_path_separator(ctx->old.tablespace_suffix),
461+
PATH_SEPARATOR, ctx->old.dbarr.dbs[dbnum].db_oid);
430462
}
431463
}
432464
else
@@ -436,7 +468,8 @@ create_script_for_old_cluster_deletion(migratorContext *ctx,
436468
* or a version-specific subdirectory.
437469
*/
438470
fprintf(script, RMDIR_CMD " %s%s\n",
439-
ctx->tablespaces[tblnum], ctx->old.tablespace_suffix);
471+
fix_path_separator(ctx->tablespaces[tblnum]),
472+
fix_path_separator(ctx->old.tablespace_suffix));
440473
}
441474

442475
fclose(script);

contrib/pg_upgrade/pg_upgrade.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#define pg_copy_file copy_file
3939
#define pg_mv_file rename
4040
#define pg_link_file link
41+
#define PATH_SEPARATOR '/'
4142
#define RM_CMD "rm -f"
4243
#define RMDIR_CMD "rm -rf"
4344
#define SHELL_EXT "sh"
@@ -46,6 +47,7 @@
4647
#define pg_mv_file pgrename
4748
#define pg_link_file win32_pghardlink
4849
#define sleep(x) Sleep(x * 1000)
50+
#define PATH_SEPARATOR '\\'
4951
#define RM_CMD "DEL /q"
5052
#define RMDIR_CMD "RMDIR /s/q"
5153
#define SHELL_EXT "bat"

0 commit comments

Comments
 (0)