Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/bin/pg_upgrade/exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
43 changes: 43 additions & 0 deletions src/bin/pg_upgrade/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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)
Expand All @@ -107,18 +121,34 @@ 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;

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);
}
Expand Down Expand Up @@ -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);

Expand Down