Skip to content

Commit b17dbf2

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 b542d94 commit b17dbf2

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
@@ -415,7 +415,7 @@ create_script_for_cluster_analyze(char **analyze_script_file_name)
415415

416416
if ((script = fopen_priv(*analyze_script_file_name, "w")) == NULL)
417417
pg_fatal("Could not open file \"%s\": %s\n",
418-
*analyze_script_file_name, getErrorText(errno));
418+
*analyze_script_file_name, getErrorText());
419419

420420
#ifndef WIN32
421421
/* add shebang header */
@@ -470,7 +470,7 @@ create_script_for_cluster_analyze(char **analyze_script_file_name)
470470
#ifndef WIN32
471471
if (chmod(*analyze_script_file_name, S_IRWXU) != 0)
472472
pg_fatal("Could not add execute permission to file \"%s\": %s\n",
473-
*analyze_script_file_name, getErrorText(errno));
473+
*analyze_script_file_name, getErrorText());
474474
#endif
475475

476476
if (os_info.user_specified)
@@ -526,7 +526,7 @@ create_script_for_old_cluster_deletion(char **deletion_script_file_name)
526526

527527
if ((script = fopen_priv(*deletion_script_file_name, "w")) == NULL)
528528
pg_fatal("Could not open file \"%s\": %s\n",
529-
*deletion_script_file_name, getErrorText(errno));
529+
*deletion_script_file_name, getErrorText());
530530

531531
#ifndef WIN32
532532
/* add shebang header */
@@ -582,7 +582,7 @@ create_script_for_old_cluster_deletion(char **deletion_script_file_name)
582582
#ifndef WIN32
583583
if (chmod(*deletion_script_file_name, S_IRWXU) != 0)
584584
pg_fatal("Could not add execute permission to file \"%s\": %s\n",
585-
*deletion_script_file_name, getErrorText(errno));
585+
*deletion_script_file_name, getErrorText());
586586
#endif
587587

588588
check_ok();
@@ -784,7 +784,7 @@ check_for_isn_and_int8_passing_mismatch(ClusterInfo *cluster)
784784
found = true;
785785
if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
786786
pg_fatal("Could not open file \"%s\": %s\n",
787-
output_path, getErrorText(errno));
787+
output_path, getErrorText());
788788
if (!db_used)
789789
{
790790
fprintf(script, "Database: %s\n", active_db->db_name);
@@ -887,7 +887,7 @@ check_for_reg_data_type_usage(ClusterInfo *cluster)
887887
found = true;
888888
if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
889889
pg_fatal("Could not open file \"%s\": %s\n",
890-
output_path, getErrorText(errno));
890+
output_path, getErrorText());
891891
if (!db_used)
892892
{
893893
fprintf(script, "Database: %s\n", active_db->db_name);
@@ -978,7 +978,7 @@ check_for_jsonb_9_4_usage(ClusterInfo *cluster)
978978
found = true;
979979
if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
980980
pg_fatal("Could not open file \"%s\": %s\n",
981-
output_path, getErrorText(errno));
981+
output_path, getErrorText());
982982
if (!db_used)
983983
{
984984
fprintf(script, "Database: %s\n", active_db->db_name);
@@ -1026,7 +1026,7 @@ get_bin_version(ClusterInfo *cluster)
10261026
if ((output = popen(cmd, "r")) == NULL ||
10271027
fgets(cmd_output, sizeof(cmd_output), output) == NULL)
10281028
pg_fatal("Could not get pg_ctl version data using %s: %s\n",
1029-
cmd, getErrorText(errno));
1029+
cmd, getErrorText());
10301030

10311031
pclose(output);
10321032

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)