]> granicus.if.org Git - postgresql/commitdiff
pg_upgrade: fix CopyFile() on Windows to fail on file existence
authorBruce Momjian <bruce@momjian.us>
Tue, 24 Nov 2015 22:18:28 +0000 (17:18 -0500)
committerBruce Momjian <bruce@momjian.us>
Tue, 24 Nov 2015 22:18:28 +0000 (17:18 -0500)
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

13 files changed:
contrib/pg_upgrade/check.c
contrib/pg_upgrade/controldata.c
contrib/pg_upgrade/exec.c
contrib/pg_upgrade/file.c
contrib/pg_upgrade/function.c
contrib/pg_upgrade/option.c
contrib/pg_upgrade/pg_upgrade.c
contrib/pg_upgrade/pg_upgrade.h
contrib/pg_upgrade/relfilenode.c
contrib/pg_upgrade/tablespace.c
contrib/pg_upgrade/util.c
contrib/pg_upgrade/version.c
contrib/pg_upgrade/version_old_8_3.c

index e29bea335bf96e85a276302bcb106f3764cc7d43..d6afb53e2a2503bc512570889807d337c0eb012d 100644 (file)
@@ -529,7 +529,7 @@ create_script_for_cluster_analyze(char **analyze_script_file_name)
 
        if ((script = fopen_priv(*analyze_script_file_name, "w")) == NULL)
                pg_fatal("Could not open file \"%s\": %s\n",
-                                *analyze_script_file_name, getErrorText(errno));
+                                *analyze_script_file_name, getErrorText());
 
 #ifndef WIN32
        /* add shebang header */
@@ -584,7 +584,7 @@ create_script_for_cluster_analyze(char **analyze_script_file_name)
 #ifndef WIN32
        if (chmod(*analyze_script_file_name, S_IRWXU) != 0)
                pg_fatal("Could not add execute permission to file \"%s\": %s\n",
-                                *analyze_script_file_name, getErrorText(errno));
+                                *analyze_script_file_name, getErrorText());
 #endif
 
        if (os_info.user_specified)
@@ -687,7 +687,7 @@ create_script_for_old_cluster_deletion(char **deletion_script_file_name)
 
        if ((script = fopen_priv(*deletion_script_file_name, "w")) == NULL)
                pg_fatal("Could not open file \"%s\": %s\n",
-                                *deletion_script_file_name, getErrorText(errno));
+                                *deletion_script_file_name, getErrorText());
 
 #ifndef WIN32
        /* add shebang header */
@@ -741,7 +741,7 @@ create_script_for_old_cluster_deletion(char **deletion_script_file_name)
 #ifndef WIN32
        if (chmod(*deletion_script_file_name, S_IRWXU) != 0)
                pg_fatal("Could not add execute permission to file \"%s\": %s\n",
-                                *deletion_script_file_name, getErrorText(errno));
+                                *deletion_script_file_name, getErrorText());
 #endif
 
        check_ok();
@@ -877,7 +877,7 @@ check_for_isn_and_int8_passing_mismatch(ClusterInfo *cluster)
                        found = true;
                        if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
                                pg_fatal("Could not open file \"%s\": %s\n",
-                                                output_path, getErrorText(errno));
+                                                output_path, getErrorText());
                        if (!db_used)
                        {
                                fprintf(script, "Database: %s\n", active_db->db_name);
@@ -980,7 +980,7 @@ check_for_reg_data_type_usage(ClusterInfo *cluster)
                        found = true;
                        if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
                                pg_fatal("Could not open file \"%s\": %s\n",
-                                                output_path, getErrorText(errno));
+                                                output_path, getErrorText());
                        if (!db_used)
                        {
                                fprintf(script, "Database: %s\n", active_db->db_name);
@@ -1071,7 +1071,7 @@ check_for_jsonb_9_4_usage(ClusterInfo *cluster)
                        found = true;
                        if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
                                pg_fatal("Could not open file \"%s\": %s\n",
-                                                output_path, getErrorText(errno));
+                                                output_path, getErrorText());
                        if (!db_used)
                        {
                                fprintf(script, "Database: %s\n", active_db->db_name);
@@ -1119,7 +1119,7 @@ get_bin_version(ClusterInfo *cluster)
        if ((output = popen(cmd, "r")) == NULL ||
                fgets(cmd_output, sizeof(cmd_output), output) == NULL)
                pg_fatal("Could not get pg_ctl version data using %s: %s\n",
-                                cmd, getErrorText(errno));
+                                cmd, getErrorText());
 
        pclose(output);
 
index df02816ca7ac8c514ff04dfb10d5684f530c559b..5f5a19b2658ea991c1838df7b6287f2213957038 100644 (file)
@@ -119,7 +119,7 @@ get_control_data(ClusterInfo *cluster, bool live_check)
 
        if ((output = popen(cmd, "r")) == NULL)
                pg_fatal("Could not get control data using %s: %s\n",
-                                cmd, getErrorText(errno));
+                                cmd, getErrorText());
 
        /* Only pre-8.4 has these so if they are not set below we will check later */
        cluster->controldata.lc_collate = NULL;
index c177288255681ecd0000d2c6188c4022ed95e96c..b38d801b5c17be51346c6baa45e7e628693afd78 100644 (file)
@@ -191,7 +191,7 @@ pid_lock_file_exists(const char *datadir)
                /* ENOTDIR means we will throw a more useful error later */
                if (errno != ENOENT && errno != ENOTDIR)
                        pg_fatal("could not open file \"%s\" for reading: %s\n",
-                                        path, getErrorText(errno));
+                                        path, getErrorText());
 
                return false;
        }
@@ -285,7 +285,7 @@ check_data_dir(const char *pg_data)
 
                if (stat(subDirName, &statBuf) != 0)
                        report_status(PG_FATAL, "check for \"%s\" failed: %s\n",
-                                                 subDirName, getErrorText(errno));
+                                                 subDirName, getErrorText());
                else if (!S_ISDIR(statBuf.st_mode))
                        report_status(PG_FATAL, "%s is not a directory\n",
                                                  subDirName);
@@ -309,7 +309,7 @@ check_bin_dir(ClusterInfo *cluster)
        /* check bindir */
        if (stat(cluster->bindir, &statBuf) != 0)
                report_status(PG_FATAL, "check for \"%s\" failed: %s\n",
-                                         cluster->bindir, getErrorText(errno));
+                                         cluster->bindir, getErrorText());
        else if (!S_ISDIR(statBuf.st_mode))
                report_status(PG_FATAL, "%s is not a directory\n",
                                          cluster->bindir);
@@ -352,7 +352,7 @@ validate_exec(const char *dir, const char *cmdName)
         */
        if (stat(path, &buf) < 0)
                pg_fatal("check for \"%s\" failed: %s\n",
-                                path, getErrorText(errno));
+                                path, getErrorText());
        else if (!S_ISREG(buf.st_mode))
                pg_fatal("check for \"%s\" failed: not an executable file\n",
                                 path);
index 4d2c407084c7a3ad65dc4b068f62c88c14f87c50..e2f8f3d2d1f5ae7a0bc51f16e87a79bfe4b6ef9f 100644 (file)
@@ -37,9 +37,9 @@ copyAndUpdateFile(pageCnvCtx *pageConverter,
 #ifndef WIN32
                if (copy_file(src, dst, force) == -1)
 #else
-               if (CopyFile(src, dst, force) == 0)
+               if (CopyFile(src, dst, !force) == 0)
 #endif
-                       return getErrorText(errno);
+                       return getErrorText();
                else
                        return NULL;
        }
@@ -121,7 +121,7 @@ linkAndUpdateFile(pageCnvCtx *pageConverter,
                return "Cannot in-place update this cluster, page-by-page conversion is required";
 
        if (pg_link_file(src, dst) == -1)
-               return getErrorText(errno);
+               return getErrorText();
        else
                return NULL;
 }
@@ -219,7 +219,7 @@ check_hard_link(void)
        {
                pg_fatal("Could not create hard link between old and new data directories: %s\n"
                                 "In link mode the old and new data directories must be on the same file system volume.\n",
-                                getErrorText(errno));
+                                getErrorText());
        }
        unlink(new_link_file);
 }
index a4dec6ea88ac3a3e6933c2486aa71d49b95eb4c3..f6e11a069953b5d9a1843fe64fb270dd99d63188 100644 (file)
@@ -327,7 +327,7 @@ check_loadable_libraries(void)
 
                        if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
                                pg_fatal("Could not open file \"%s\": %s\n",
-                                                output_path, getErrorText(errno));
+                                                output_path, getErrorText());
                        fprintf(script, "Could not load library \"%s\"\n%s\n",
                                        lib,
                                        PQerrorMessage(conn));
index e0a3c6d71146ca254454611346aacd2660ad8611..a608036cf446e04b0eb83a2e695bfc063cbcec81 100644 (file)
@@ -383,7 +383,7 @@ adjust_data_dir(ClusterInfo *cluster)
        if ((output = popen(cmd, "r")) == NULL ||
                fgets(cmd_output, sizeof(cmd_output), output) == NULL)
                pg_fatal("Could not get data directory using %s: %s\n",
-                                cmd, getErrorText(errno));
+                                cmd, getErrorText());
 
        pclose(output);
 
index 96f902f77233f3811e2f47eba497cd3c57bf18ab..9d5ac03a2277e6ac493455027e794a6e23b1d189 100644 (file)
@@ -389,7 +389,7 @@ setup(char *argv0, bool *live_check)
 
        /* get path to pg_upgrade executable */
        if (find_my_exec(argv0, exec_path) < 0)
-               pg_fatal("Could not get path name to pg_upgrade: %s\n", getErrorText(errno));
+               pg_fatal("Could not get path name to pg_upgrade: %s\n", getErrorText());
 
        /* Trim off program name and keep just path */
        *last_dir_separator(exec_path) = '\0';
index 5568c2ebbab911c720dd84b782a649b0b5ecc424..e7c0f637c4a0435c0566d01b3f1f238c711cbb97 100644 (file)
@@ -465,7 +465,7 @@ void
 prep_status(const char *fmt,...)
 __attribute__((format(PG_PRINTF_ATTRIBUTE, 1, 2)));
 void           check_ok(void);
-const char *getErrorText(int errNum);
+const char *getErrorText(void);
 unsigned int str2uint(const char *str);
 void           pg_putenv(const char *var, const char *val);
 
index aa6aafde5e97d70ff1673599a56197fed388c038..05c375e0c38c49e170886dfb64e9039df31f8f12 100644 (file)
@@ -260,7 +260,7 @@ transfer_relfile(pageCnvCtx *pageConverter, FileNameMap *map,
                                else
                                        pg_fatal("error while checking for file existence \"%s.%s\" (\"%s\" to \"%s\"): %s\n",
                                                         map->nspname, map->relname, old_file, new_file,
-                                                        getErrorText(errno));
+                                                        getErrorText());
                        }
                        close(fd);
                }
index 68e9cb241c75dbc0bb42a976733238d7e77de717..4086e53cf56dacf7ac73a80f56a03663a60129b2 100644 (file)
@@ -91,7 +91,7 @@ get_tablespace_paths(void)
                        else
                                report_status(PG_FATAL,
                                                   "cannot stat() tablespace directory \"%s\": %s\n",
-                                          os_info.old_tablespaces[tblnum], getErrorText(errno));
+                                          os_info.old_tablespaces[tblnum], getErrorText());
                }
                if (!S_ISDIR(statBuf.st_mode))
                        report_status(PG_FATAL,
index 3b94057696d0ad7cae3ead2521af10c39e25c2b1..84a02ecaf465c791de86d117468ce678a1fe34f2 100644 (file)
@@ -234,18 +234,15 @@ get_user_info(char **user_name_p)
 /*
  * getErrorText()
  *
- *     Returns the text of the error message for the given error number
- *
- *     This feature is factored into a separate function because it is
- *     system-dependent.
+ *     Returns the text of the most recent error
  */
 const char *
-getErrorText(int errNum)
+getErrorText(void)
 {
 #ifdef WIN32
        _dosmaperr(GetLastError());
 #endif
-       return pg_strdup(strerror(errNum));
+       return pg_strdup(strerror(errno));
 }
 
 
index 0f9dc079b21e699ab1ba8dce3cd2e3275b8b9d81..339241b6e37e736637a0976eee8f7fd3b3b0c77f 100644 (file)
@@ -49,7 +49,7 @@ new_9_0_populate_pg_largeobject_metadata(ClusterInfo *cluster, bool check_mode)
                        if (!check_mode)
                        {
                                if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
-                                       pg_fatal("could not open file \"%s\": %s\n", output_path, getErrorText(errno));
+                                       pg_fatal("could not open file \"%s\": %s\n", output_path, getErrorText());
                                fprintf(script, "\\connect %s\n",
                                                quote_identifier(active_db->db_name));
                                fprintf(script,
@@ -143,7 +143,7 @@ old_9_3_check_for_line_data_type_usage(ClusterInfo *cluster)
                {
                        found = true;
                        if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
-                               pg_fatal("could not open file \"%s\": %s\n", output_path, getErrorText(errno));
+                               pg_fatal("could not open file \"%s\": %s\n", output_path, getErrorText());
                        if (!db_used)
                        {
                                fprintf(script, "Database: %s\n", active_db->db_name);
index 07e79bd609a5f613e93bf94d3c2d9a3e16c5cd6c..40d86871e0905e40cf61a94c6ee9119d525f62e4 100644 (file)
@@ -73,7 +73,7 @@ old_8_3_check_for_name_data_type_usage(ClusterInfo *cluster)
                {
                        found = true;
                        if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
-                               pg_fatal("could not open file \"%s\": %s\n", output_path, getErrorText(errno));
+                               pg_fatal("could not open file \"%s\": %s\n", output_path, getErrorText());
                        if (!db_used)
                        {
                                fprintf(script, "Database: %s\n", active_db->db_name);
@@ -163,7 +163,7 @@ old_8_3_check_for_tsquery_usage(ClusterInfo *cluster)
                {
                        found = true;
                        if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
-                               pg_fatal("could not open file \"%s\": %s\n", output_path, getErrorText(errno));
+                               pg_fatal("could not open file \"%s\": %s\n", output_path, getErrorText());
                        if (!db_used)
                        {
                                fprintf(script, "Database: %s\n", active_db->db_name);
@@ -242,7 +242,7 @@ old_8_3_check_ltree_usage(ClusterInfo *cluster)
                        found = true;
                        if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
                                pg_fatal("Could not open file \"%s\": %s\n",
-                                                output_path, getErrorText(errno));
+                                                output_path, getErrorText());
                        if (!db_used)
                        {
                                fprintf(script, "Database: %s\n", active_db->db_name);
@@ -365,7 +365,7 @@ old_8_3_rebuild_tsvector_tables(ClusterInfo *cluster, bool check_mode)
                        if (!check_mode)
                        {
                                if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
-                                       pg_fatal("could not open file \"%s\": %s\n", output_path, getErrorText(errno));
+                                       pg_fatal("could not open file \"%s\": %s\n", output_path, getErrorText());
                                if (!db_used)
                                {
                                        fprintf(script, "\\connect %s\n\n",
@@ -481,7 +481,7 @@ old_8_3_invalidate_hash_gin_indexes(ClusterInfo *cluster, bool check_mode)
                        if (!check_mode)
                        {
                                if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
-                                       pg_fatal("could not open file \"%s\": %s\n", output_path, getErrorText(errno));
+                                       pg_fatal("could not open file \"%s\": %s\n", output_path, getErrorText());
                                if (!db_used)
                                {
                                        fprintf(script, "\\connect %s\n",
@@ -600,7 +600,7 @@ old_8_3_invalidate_bpchar_pattern_ops_indexes(ClusterInfo *cluster,
                        if (!check_mode)
                        {
                                if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
-                                       pg_fatal("could not open file \"%s\": %s\n", output_path, getErrorText(errno));
+                                       pg_fatal("could not open file \"%s\": %s\n", output_path, getErrorText());
                                if (!db_used)
                                {
                                        fprintf(script, "\\connect %s\n",
@@ -722,7 +722,7 @@ old_8_3_create_sequence_script(ClusterInfo *cluster)
                        found = true;
 
                        if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
-                               pg_fatal("could not open file \"%s\": %s\n", output_path, getErrorText(errno));
+                               pg_fatal("could not open file \"%s\": %s\n", output_path, getErrorText());
                        if (!db_used)
                        {
                                fprintf(script, "\\connect %s\n\n",