Skip to content

Commit f91c4e3

Browse files
committed
pg_upgrade: fix CopyFile() on Windows to fail on file existence
Also fix getErrorText() to return the right error string on failure. This behavior now matches that of other operating systems. Report by Noah Misch Backpatch through 9.1
1 parent 7acad95 commit f91c4e3

13 files changed

+35
-38
lines changed

contrib/pg_upgrade/check.c

+8-8
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ create_script_for_cluster_analyze(char **analyze_script_file_name)
529529

530530
if ((script = fopen_priv(*analyze_script_file_name, "w")) == NULL)
531531
pg_fatal("Could not open file \"%s\": %s\n",
532-
*analyze_script_file_name, getErrorText(errno));
532+
*analyze_script_file_name, getErrorText());
533533

534534
#ifndef WIN32
535535
/* add shebang header */
@@ -584,7 +584,7 @@ create_script_for_cluster_analyze(char **analyze_script_file_name)
584584
#ifndef WIN32
585585
if (chmod(*analyze_script_file_name, S_IRWXU) != 0)
586586
pg_fatal("Could not add execute permission to file \"%s\": %s\n",
587-
*analyze_script_file_name, getErrorText(errno));
587+
*analyze_script_file_name, getErrorText());
588588
#endif
589589

590590
if (os_info.user_specified)
@@ -687,7 +687,7 @@ create_script_for_old_cluster_deletion(char **deletion_script_file_name)
687687

688688
if ((script = fopen_priv(*deletion_script_file_name, "w")) == NULL)
689689
pg_fatal("Could not open file \"%s\": %s\n",
690-
*deletion_script_file_name, getErrorText(errno));
690+
*deletion_script_file_name, getErrorText());
691691

692692
#ifndef WIN32
693693
/* add shebang header */
@@ -741,7 +741,7 @@ create_script_for_old_cluster_deletion(char **deletion_script_file_name)
741741
#ifndef WIN32
742742
if (chmod(*deletion_script_file_name, S_IRWXU) != 0)
743743
pg_fatal("Could not add execute permission to file \"%s\": %s\n",
744-
*deletion_script_file_name, getErrorText(errno));
744+
*deletion_script_file_name, getErrorText());
745745
#endif
746746

747747
check_ok();
@@ -877,7 +877,7 @@ check_for_isn_and_int8_passing_mismatch(ClusterInfo *cluster)
877877
found = true;
878878
if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
879879
pg_fatal("Could not open file \"%s\": %s\n",
880-
output_path, getErrorText(errno));
880+
output_path, getErrorText());
881881
if (!db_used)
882882
{
883883
fprintf(script, "Database: %s\n", active_db->db_name);
@@ -980,7 +980,7 @@ check_for_reg_data_type_usage(ClusterInfo *cluster)
980980
found = true;
981981
if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
982982
pg_fatal("Could not open file \"%s\": %s\n",
983-
output_path, getErrorText(errno));
983+
output_path, getErrorText());
984984
if (!db_used)
985985
{
986986
fprintf(script, "Database: %s\n", active_db->db_name);
@@ -1071,7 +1071,7 @@ check_for_jsonb_9_4_usage(ClusterInfo *cluster)
10711071
found = true;
10721072
if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
10731073
pg_fatal("Could not open file \"%s\": %s\n",
1074-
output_path, getErrorText(errno));
1074+
output_path, getErrorText());
10751075
if (!db_used)
10761076
{
10771077
fprintf(script, "Database: %s\n", active_db->db_name);
@@ -1119,7 +1119,7 @@ get_bin_version(ClusterInfo *cluster)
11191119
if ((output = popen(cmd, "r")) == NULL ||
11201120
fgets(cmd_output, sizeof(cmd_output), output) == NULL)
11211121
pg_fatal("Could not get pg_ctl version data using %s: %s\n",
1122-
cmd, getErrorText(errno));
1122+
cmd, getErrorText());
11231123

11241124
pclose(output);
11251125

contrib/pg_upgrade/controldata.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ get_control_data(ClusterInfo *cluster, bool live_check)
119119

120120
if ((output = popen(cmd, "r")) == NULL)
121121
pg_fatal("Could not get control data using %s: %s\n",
122-
cmd, getErrorText(errno));
122+
cmd, getErrorText());
123123

124124
/* Only pre-8.4 has these so if they are not set below we will check later */
125125
cluster->controldata.lc_collate = NULL;

contrib/pg_upgrade/exec.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ pid_lock_file_exists(const char *datadir)
191191
/* ENOTDIR means we will throw a more useful error later */
192192
if (errno != ENOENT && errno != ENOTDIR)
193193
pg_fatal("could not open file \"%s\" for reading: %s\n",
194-
path, getErrorText(errno));
194+
path, getErrorText());
195195

196196
return false;
197197
}
@@ -285,7 +285,7 @@ check_data_dir(const char *pg_data)
285285

286286
if (stat(subDirName, &statBuf) != 0)
287287
report_status(PG_FATAL, "check for \"%s\" failed: %s\n",
288-
subDirName, getErrorText(errno));
288+
subDirName, getErrorText());
289289
else if (!S_ISDIR(statBuf.st_mode))
290290
report_status(PG_FATAL, "%s is not a directory\n",
291291
subDirName);
@@ -309,7 +309,7 @@ check_bin_dir(ClusterInfo *cluster)
309309
/* check bindir */
310310
if (stat(cluster->bindir, &statBuf) != 0)
311311
report_status(PG_FATAL, "check for \"%s\" failed: %s\n",
312-
cluster->bindir, getErrorText(errno));
312+
cluster->bindir, getErrorText());
313313
else if (!S_ISDIR(statBuf.st_mode))
314314
report_status(PG_FATAL, "%s is not a directory\n",
315315
cluster->bindir);
@@ -352,7 +352,7 @@ validate_exec(const char *dir, const char *cmdName)
352352
*/
353353
if (stat(path, &buf) < 0)
354354
pg_fatal("check for \"%s\" failed: %s\n",
355-
path, getErrorText(errno));
355+
path, getErrorText());
356356
else if (!S_ISREG(buf.st_mode))
357357
pg_fatal("check for \"%s\" failed: not an executable file\n",
358358
path);

contrib/pg_upgrade/file.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ copyAndUpdateFile(pageCnvCtx *pageConverter,
3737
#ifndef WIN32
3838
if (copy_file(src, dst, force) == -1)
3939
#else
40-
if (CopyFile(src, dst, force) == 0)
40+
if (CopyFile(src, dst, !force) == 0)
4141
#endif
42-
return getErrorText(errno);
42+
return getErrorText();
4343
else
4444
return NULL;
4545
}
@@ -121,7 +121,7 @@ linkAndUpdateFile(pageCnvCtx *pageConverter,
121121
return "Cannot in-place update this cluster, page-by-page conversion is required";
122122

123123
if (pg_link_file(src, dst) == -1)
124-
return getErrorText(errno);
124+
return getErrorText();
125125
else
126126
return NULL;
127127
}
@@ -219,7 +219,7 @@ check_hard_link(void)
219219
{
220220
pg_fatal("Could not create hard link between old and new data directories: %s\n"
221221
"In link mode the old and new data directories must be on the same file system volume.\n",
222-
getErrorText(errno));
222+
getErrorText());
223223
}
224224
unlink(new_link_file);
225225
}

contrib/pg_upgrade/function.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ check_loadable_libraries(void)
327327

328328
if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
329329
pg_fatal("Could not open file \"%s\": %s\n",
330-
output_path, getErrorText(errno));
330+
output_path, getErrorText());
331331
fprintf(script, "Could not load library \"%s\"\n%s\n",
332332
lib,
333333
PQerrorMessage(conn));

contrib/pg_upgrade/option.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ adjust_data_dir(ClusterInfo *cluster)
383383
if ((output = popen(cmd, "r")) == NULL ||
384384
fgets(cmd_output, sizeof(cmd_output), output) == NULL)
385385
pg_fatal("Could not get data directory using %s: %s\n",
386-
cmd, getErrorText(errno));
386+
cmd, getErrorText());
387387

388388
pclose(output);
389389

contrib/pg_upgrade/pg_upgrade.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ setup(char *argv0, bool *live_check)
389389

390390
/* get path to pg_upgrade executable */
391391
if (find_my_exec(argv0, exec_path) < 0)
392-
pg_fatal("Could not get path name to pg_upgrade: %s\n", getErrorText(errno));
392+
pg_fatal("Could not get path name to pg_upgrade: %s\n", getErrorText());
393393

394394
/* Trim off program name and keep just path */
395395
*last_dir_separator(exec_path) = '\0';

contrib/pg_upgrade/pg_upgrade.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ void
465465
prep_status(const char *fmt,...)
466466
__attribute__((format(PG_PRINTF_ATTRIBUTE, 1, 2)));
467467
void check_ok(void);
468-
const char *getErrorText(int errNum);
468+
const char *getErrorText(void);
469469
unsigned int str2uint(const char *str);
470470
void pg_putenv(const char *var, const char *val);
471471

contrib/pg_upgrade/relfilenode.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ transfer_relfile(pageCnvCtx *pageConverter, FileNameMap *map,
260260
else
261261
pg_fatal("error while checking for file existence \"%s.%s\" (\"%s\" to \"%s\"): %s\n",
262262
map->nspname, map->relname, old_file, new_file,
263-
getErrorText(errno));
263+
getErrorText());
264264
}
265265
close(fd);
266266
}

contrib/pg_upgrade/tablespace.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ get_tablespace_paths(void)
9191
else
9292
report_status(PG_FATAL,
9393
"cannot stat() tablespace directory \"%s\": %s\n",
94-
os_info.old_tablespaces[tblnum], getErrorText(errno));
94+
os_info.old_tablespaces[tblnum], getErrorText());
9595
}
9696
if (!S_ISDIR(statBuf.st_mode))
9797
report_status(PG_FATAL,

contrib/pg_upgrade/util.c

+3-6
Original file line numberDiff line numberDiff line change
@@ -234,18 +234,15 @@ get_user_info(char **user_name_p)
234234
/*
235235
* getErrorText()
236236
*
237-
* Returns the text of the error message for the given error number
238-
*
239-
* This feature is factored into a separate function because it is
240-
* system-dependent.
237+
* Returns the text of the most recent error
241238
*/
242239
const char *
243-
getErrorText(int errNum)
240+
getErrorText(void)
244241
{
245242
#ifdef WIN32
246243
_dosmaperr(GetLastError());
247244
#endif
248-
return pg_strdup(strerror(errNum));
245+
return pg_strdup(strerror(errno));
249246
}
250247

251248

contrib/pg_upgrade/version.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ new_9_0_populate_pg_largeobject_metadata(ClusterInfo *cluster, bool check_mode)
4949
if (!check_mode)
5050
{
5151
if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
52-
pg_fatal("could not open file \"%s\": %s\n", output_path, getErrorText(errno));
52+
pg_fatal("could not open file \"%s\": %s\n", output_path, getErrorText());
5353
fprintf(script, "\\connect %s\n",
5454
quote_identifier(active_db->db_name));
5555
fprintf(script,
@@ -143,7 +143,7 @@ old_9_3_check_for_line_data_type_usage(ClusterInfo *cluster)
143143
{
144144
found = true;
145145
if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
146-
pg_fatal("could not open file \"%s\": %s\n", output_path, getErrorText(errno));
146+
pg_fatal("could not open file \"%s\": %s\n", output_path, getErrorText());
147147
if (!db_used)
148148
{
149149
fprintf(script, "Database: %s\n", active_db->db_name);

contrib/pg_upgrade/version_old_8_3.c

+7-7
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ old_8_3_check_for_name_data_type_usage(ClusterInfo *cluster)
7373
{
7474
found = true;
7575
if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
76-
pg_fatal("could not open file \"%s\": %s\n", output_path, getErrorText(errno));
76+
pg_fatal("could not open file \"%s\": %s\n", output_path, getErrorText());
7777
if (!db_used)
7878
{
7979
fprintf(script, "Database: %s\n", active_db->db_name);
@@ -163,7 +163,7 @@ old_8_3_check_for_tsquery_usage(ClusterInfo *cluster)
163163
{
164164
found = true;
165165
if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
166-
pg_fatal("could not open file \"%s\": %s\n", output_path, getErrorText(errno));
166+
pg_fatal("could not open file \"%s\": %s\n", output_path, getErrorText());
167167
if (!db_used)
168168
{
169169
fprintf(script, "Database: %s\n", active_db->db_name);
@@ -242,7 +242,7 @@ old_8_3_check_ltree_usage(ClusterInfo *cluster)
242242
found = true;
243243
if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
244244
pg_fatal("Could not open file \"%s\": %s\n",
245-
output_path, getErrorText(errno));
245+
output_path, getErrorText());
246246
if (!db_used)
247247
{
248248
fprintf(script, "Database: %s\n", active_db->db_name);
@@ -365,7 +365,7 @@ old_8_3_rebuild_tsvector_tables(ClusterInfo *cluster, bool check_mode)
365365
if (!check_mode)
366366
{
367367
if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
368-
pg_fatal("could not open file \"%s\": %s\n", output_path, getErrorText(errno));
368+
pg_fatal("could not open file \"%s\": %s\n", output_path, getErrorText());
369369
if (!db_used)
370370
{
371371
fprintf(script, "\\connect %s\n\n",
@@ -481,7 +481,7 @@ old_8_3_invalidate_hash_gin_indexes(ClusterInfo *cluster, bool check_mode)
481481
if (!check_mode)
482482
{
483483
if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
484-
pg_fatal("could not open file \"%s\": %s\n", output_path, getErrorText(errno));
484+
pg_fatal("could not open file \"%s\": %s\n", output_path, getErrorText());
485485
if (!db_used)
486486
{
487487
fprintf(script, "\\connect %s\n",
@@ -600,7 +600,7 @@ old_8_3_invalidate_bpchar_pattern_ops_indexes(ClusterInfo *cluster,
600600
if (!check_mode)
601601
{
602602
if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
603-
pg_fatal("could not open file \"%s\": %s\n", output_path, getErrorText(errno));
603+
pg_fatal("could not open file \"%s\": %s\n", output_path, getErrorText());
604604
if (!db_used)
605605
{
606606
fprintf(script, "\\connect %s\n",
@@ -722,7 +722,7 @@ old_8_3_create_sequence_script(ClusterInfo *cluster)
722722
found = true;
723723

724724
if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
725-
pg_fatal("could not open file \"%s\": %s\n", output_path, getErrorText(errno));
725+
pg_fatal("could not open file \"%s\": %s\n", output_path, getErrorText());
726726
if (!db_used)
727727
{
728728
fprintf(script, "\\connect %s\n\n",

0 commit comments

Comments
 (0)