Skip to content

Commit 13b30c1

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 cbd96ef commit 13b30c1

File tree

12 files changed

+28
-31
lines changed

12 files changed

+28
-31
lines changed

src/bin/pg_upgrade/check.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ create_script_for_cluster_analyze(char **analyze_script_file_name)
421421

422422
if ((script = fopen_priv(*analyze_script_file_name, "w")) == NULL)
423423
pg_fatal("Could not open file \"%s\": %s\n",
424-
*analyze_script_file_name, getErrorText(errno));
424+
*analyze_script_file_name, getErrorText());
425425

426426
#ifndef WIN32
427427
/* add shebang header */
@@ -476,7 +476,7 @@ create_script_for_cluster_analyze(char **analyze_script_file_name)
476476
#ifndef WIN32
477477
if (chmod(*analyze_script_file_name, S_IRWXU) != 0)
478478
pg_fatal("Could not add execute permission to file \"%s\": %s\n",
479-
*analyze_script_file_name, getErrorText(errno));
479+
*analyze_script_file_name, getErrorText());
480480
#endif
481481

482482
if (os_info.user_specified)
@@ -532,7 +532,7 @@ create_script_for_old_cluster_deletion(char **deletion_script_file_name)
532532

533533
if ((script = fopen_priv(*deletion_script_file_name, "w")) == NULL)
534534
pg_fatal("Could not open file \"%s\": %s\n",
535-
*deletion_script_file_name, getErrorText(errno));
535+
*deletion_script_file_name, getErrorText());
536536

537537
#ifndef WIN32
538538
/* add shebang header */
@@ -588,7 +588,7 @@ create_script_for_old_cluster_deletion(char **deletion_script_file_name)
588588
#ifndef WIN32
589589
if (chmod(*deletion_script_file_name, S_IRWXU) != 0)
590590
pg_fatal("Could not add execute permission to file \"%s\": %s\n",
591-
*deletion_script_file_name, getErrorText(errno));
591+
*deletion_script_file_name, getErrorText());
592592
#endif
593593

594594
check_ok();
@@ -790,7 +790,7 @@ check_for_isn_and_int8_passing_mismatch(ClusterInfo *cluster)
790790
found = true;
791791
if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
792792
pg_fatal("Could not open file \"%s\": %s\n",
793-
output_path, getErrorText(errno));
793+
output_path, getErrorText());
794794
if (!db_used)
795795
{
796796
fprintf(script, "Database: %s\n", active_db->db_name);
@@ -893,7 +893,7 @@ check_for_reg_data_type_usage(ClusterInfo *cluster)
893893
found = true;
894894
if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
895895
pg_fatal("Could not open file \"%s\": %s\n",
896-
output_path, getErrorText(errno));
896+
output_path, getErrorText());
897897
if (!db_used)
898898
{
899899
fprintf(script, "Database: %s\n", active_db->db_name);
@@ -984,7 +984,7 @@ check_for_jsonb_9_4_usage(ClusterInfo *cluster)
984984
found = true;
985985
if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
986986
pg_fatal("Could not open file \"%s\": %s\n",
987-
output_path, getErrorText(errno));
987+
output_path, getErrorText());
988988
if (!db_used)
989989
{
990990
fprintf(script, "Database: %s\n", active_db->db_name);
@@ -1032,7 +1032,7 @@ get_bin_version(ClusterInfo *cluster)
10321032
if ((output = popen(cmd, "r")) == NULL ||
10331033
fgets(cmd_output, sizeof(cmd_output), output) == NULL)
10341034
pg_fatal("Could not get pg_ctl version data using %s: %s\n",
1035-
cmd, getErrorText(errno));
1035+
cmd, getErrorText());
10361036

10371037
pclose(output);
10381038

src/bin/pg_upgrade/controldata.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ get_control_data(ClusterInfo *cluster, bool live_check)
120120

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

125125
/* Only in <= 9.2 */
126126
if (GET_MAJOR_VERSION(cluster->major_version) <= 902)

src/bin/pg_upgrade/exec.c

Lines changed: 4 additions & 4 deletions
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);

src/bin/pg_upgrade/file.c

Lines changed: 4 additions & 4 deletions
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
}

src/bin/pg_upgrade/function.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ check_loadable_libraries(void)
214214

215215
if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
216216
pg_fatal("Could not open file \"%s\": %s\n",
217-
output_path, getErrorText(errno));
217+
output_path, getErrorText());
218218
fprintf(script, "Could not load library \"%s\"\n%s\n",
219219
lib,
220220
PQerrorMessage(conn));

src/bin/pg_upgrade/option.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ adjust_data_dir(ClusterInfo *cluster)
423423
if ((output = popen(cmd, "r")) == NULL ||
424424
fgets(cmd_output, sizeof(cmd_output), output) == NULL)
425425
pg_fatal("Could not get data directory using %s: %s\n",
426-
cmd, getErrorText(errno));
426+
cmd, getErrorText());
427427

428428
pclose(output);
429429

src/bin/pg_upgrade/pg_upgrade.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ setup(char *argv0, bool *live_check)
224224

225225
/* get path to pg_upgrade executable */
226226
if (find_my_exec(argv0, exec_path) < 0)
227-
pg_fatal("Could not get path name to pg_upgrade: %s\n", getErrorText(errno));
227+
pg_fatal("Could not get path name to pg_upgrade: %s\n", getErrorText());
228228

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

src/bin/pg_upgrade/pg_upgrade.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ void pg_fatal(const char *fmt,...) pg_attribute_printf(1, 2) pg_attribute_noret
460460
void end_progress_output(void);
461461
void prep_status(const char *fmt,...) pg_attribute_printf(1, 2);
462462
void check_ok(void);
463-
const char *getErrorText(int errNum);
463+
const char *getErrorText(void);
464464
unsigned int str2uint(const char *str);
465465
void pg_putenv(const char *var, const char *val);
466466

src/bin/pg_upgrade/relfilenode.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ transfer_relfile(pageCnvCtx *pageConverter, FileNameMap *map,
258258
else
259259
pg_fatal("error while checking for file existence \"%s.%s\" (\"%s\" to \"%s\"): %s\n",
260260
map->nspname, map->relname, old_file, new_file,
261-
getErrorText(errno));
261+
getErrorText());
262262
}
263263
close(fd);
264264
}

src/bin/pg_upgrade/tablespace.c

Lines changed: 1 addition & 1 deletion
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,

src/bin/pg_upgrade/util.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -235,18 +235,15 @@ get_user_info(char **user_name_p)
235235
/*
236236
* getErrorText()
237237
*
238-
* Returns the text of the error message for the given error number
239-
*
240-
* This feature is factored into a separate function because it is
241-
* system-dependent.
238+
* Returns the text of the most recent error
242239
*/
243240
const char *
244-
getErrorText(int errNum)
241+
getErrorText(void)
245242
{
246243
#ifdef WIN32
247244
_dosmaperr(GetLastError());
248245
#endif
249-
return pg_strdup(strerror(errNum));
246+
return pg_strdup(strerror(errno));
250247
}
251248

252249

src/bin/pg_upgrade/version.c

Lines changed: 2 additions & 2 deletions
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);

0 commit comments

Comments
 (0)