diff --git a/src/bin/pg_upgrade/exec.c b/src/bin/pg_upgrade/exec.c index e1de61f36eee1..7c5ace9da4ec2 100644 --- a/src/bin/pg_upgrade/exec.c +++ b/src/bin/pg_upgrade/exec.c @@ -43,10 +43,18 @@ get_bin_version(ClusterInfo *cluster) snprintf(cmd, sizeof(cmd), "\"%s/pg_ctl\" --version", cluster->bindir); fflush(NULL); - if ((output = popen(cmd, "r")) == NULL || - fgets(cmd_output, sizeof(cmd_output), output) == NULL) + if ((output = popen(cmd, "r")) == NULL) pg_fatal("could not get pg_ctl version data using %s: %m", cmd); + if (fgets(cmd_output, sizeof(cmd_output), output) == NULL) + { + int save_errno = errno; + + pclose(output); + errno = save_errno; + pg_fatal("could not get pg_ctl version data using %s: %m", cmd); + } + rc = pclose(output); if (rc != 0) pg_fatal("could not get pg_ctl version data using %s: %s", diff --git a/src/bin/pg_upgrade/file.c b/src/bin/pg_upgrade/file.c index 4692e896326d0..2c55c081ba766 100644 --- a/src/bin/pg_upgrade/file.c +++ b/src/bin/pg_upgrade/file.c @@ -53,13 +53,21 @@ cloneFile(const char *src, const char *dst, if ((dest_fd = open(dst, O_RDWR | O_CREAT | O_EXCL | PG_BINARY, pg_file_create_mode)) < 0) + { + int save_errno = errno; + + close(src_fd); + errno = save_errno; pg_fatal("error while cloning relation \"%s.%s\": could not create file \"%s\": %m", schemaName, relName, dst); + } if (ioctl(dest_fd, FICLONE, src_fd) < 0) { int save_errno = errno; + close(src_fd); + close(dest_fd); unlink(dst); pg_fatal("error while cloning relation \"%s.%s\" (\"%s\" to \"%s\"): %s", @@ -93,8 +101,14 @@ copyFile(const char *src, const char *dst, if ((dest_fd = open(dst, O_RDWR | O_CREAT | O_EXCL | PG_BINARY, pg_file_create_mode)) < 0) + { + int save_errno = errno; + + close(src_fd); + errno = save_errno; pg_fatal("error while copying relation \"%s.%s\": could not create file \"%s\": %m", schemaName, relName, dst); + } /* copy in fairly large chunks for best efficiency */ #define COPY_BUF_SIZE (50 * BLCKSZ) @@ -107,8 +121,16 @@ copyFile(const char *src, const char *dst, ssize_t nbytes = read(src_fd, buffer, COPY_BUF_SIZE); if (nbytes < 0) + { + int save_errno = errno; + + pg_free(buffer); + close(src_fd); + close(dest_fd); + errno = save_errno; pg_fatal("error while copying relation \"%s.%s\": could not read file \"%s\": %m", schemaName, relName, src); + } if (nbytes == 0) break; @@ -116,9 +138,17 @@ copyFile(const char *src, const char *dst, errno = 0; if (write(dest_fd, buffer, nbytes) != nbytes) { + int save_errno; + /* if write didn't set errno, assume problem is no disk space */ if (errno == 0) errno = ENOSPC; + + save_errno = errno; + pg_free(buffer); + close(src_fd); + close(dest_fd); + errno = save_errno; pg_fatal("error while copying relation \"%s.%s\": could not write file \"%s\": %m", schemaName, relName, dst); } @@ -162,15 +192,28 @@ copyFileByRange(const char *src, const char *dst, if ((dest_fd = open(dst, O_RDWR | O_CREAT | O_EXCL | PG_BINARY, pg_file_create_mode)) < 0) + { + int save_errno = errno; + + close(src_fd); + errno = save_errno; pg_fatal("error while copying relation \"%s.%s\": could not create file \"%s\": %m", schemaName, relName, dst); + } do { nbytes = copy_file_range(src_fd, NULL, dest_fd, NULL, SSIZE_MAX, 0); if (nbytes < 0) + { + int save_errno = errno; + + close(src_fd); + close(dest_fd); + errno = save_errno; pg_fatal("error while copying relation \"%s.%s\": could not copy file range from \"%s\" to \"%s\": %m", schemaName, relName, src, dst); + } } while (nbytes > 0);