2010-05-12 10:19:11 +08:00
|
|
|
/*
|
|
|
|
* file.c
|
|
|
|
*
|
|
|
|
* file system operations
|
2010-07-03 22:23:14 +08:00
|
|
|
*
|
2013-01-02 06:15:01 +08:00
|
|
|
* Copyright (c) 2010-2013, PostgreSQL Global Development Group
|
2010-09-21 04:08:53 +08:00
|
|
|
* contrib/pg_upgrade/file.c
|
2010-05-12 10:19:11 +08:00
|
|
|
*/
|
|
|
|
|
Create libpgcommon, and move pg_malloc et al to it
libpgcommon is a new static library to allow sharing code among the
various frontend programs and backend; this lets us eliminate duplicate
implementations of common routines. We avoid libpgport, because that's
intended as a place for porting issues; per discussion, it seems better
to keep them separate.
The first use case, and the only implemented by this patch, is pg_malloc
and friends, which many frontend programs were already using.
At the same time, we can use this to provide palloc emulation functions
for the frontend; this way, some palloc-using files in the backend can
also be used by the frontend cleanly. To do this, we change palloc() in
the backend to be a function instead of a macro on top of
MemoryContextAlloc(). This was previously believed to cause loss of
performance, but this implementation has been tweaked by Tom and Andres
so that on modern compilers it provides a slight improvement over the
previous one.
This lets us clean up some places that were already with
localized hacks.
Most of the pg_malloc/palloc changes in this patch were authored by
Andres Freund. Zoltán Böszörményi also independently provided a form of
that. libpgcommon infrastructure was authored by Álvaro.
2013-02-12 21:33:40 +08:00
|
|
|
#include "postgres_fe.h"
|
2011-08-27 09:16:24 +08:00
|
|
|
|
2010-05-12 10:19:11 +08:00
|
|
|
#include "pg_upgrade.h"
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-04-24 08:27:57 +08:00
|
|
|
#ifndef WIN32
|
|
|
|
static int copy_file(const char *fromfile, const char *tofile, bool force);
|
|
|
|
#else
|
2010-05-12 10:19:11 +08:00
|
|
|
static int win32_pghardlink(const char *src, const char *dst);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* copyAndUpdateFile()
|
|
|
|
*
|
|
|
|
* Copies a relation file from src to dst. If pageConverter is non-NULL, this function
|
|
|
|
* uses that pageConverter to do a page-by-page conversion.
|
|
|
|
*/
|
|
|
|
const char *
|
2010-10-20 05:38:16 +08:00
|
|
|
copyAndUpdateFile(pageCnvCtx *pageConverter,
|
2010-05-12 10:19:11 +08:00
|
|
|
const char *src, const char *dst, bool force)
|
|
|
|
{
|
|
|
|
if (pageConverter == NULL)
|
|
|
|
{
|
|
|
|
if (pg_copy_file(src, dst, force) == -1)
|
|
|
|
return getErrorText(errno);
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* We have a pageConverter object - that implies that the
|
|
|
|
* PageLayoutVersion differs between the two clusters so we have to
|
|
|
|
* perform a page-by-page conversion.
|
|
|
|
*
|
|
|
|
* If the pageConverter can convert the entire file at once, invoke
|
|
|
|
* that plugin function, otherwise, read each page in the relation
|
|
|
|
* file and call the convertPage plugin function.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef PAGE_CONVERSION
|
|
|
|
if (pageConverter->convertFile)
|
|
|
|
return pageConverter->convertFile(pageConverter->pluginData,
|
|
|
|
dst, src);
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
int src_fd;
|
|
|
|
int dstfd;
|
|
|
|
char buf[BLCKSZ];
|
|
|
|
ssize_t bytesRead;
|
|
|
|
const char *msg = NULL;
|
|
|
|
|
|
|
|
if ((src_fd = open(src, O_RDONLY, 0)) < 0)
|
2011-07-12 12:13:51 +08:00
|
|
|
return "could not open source file";
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
if ((dstfd = open(dst, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR)) < 0)
|
2010-09-29 03:25:12 +08:00
|
|
|
{
|
2010-09-29 05:27:32 +08:00
|
|
|
close(src_fd);
|
2011-07-12 12:13:51 +08:00
|
|
|
return "could not create destination file";
|
2010-09-29 03:25:12 +08:00
|
|
|
}
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
while ((bytesRead = read(src_fd, buf, BLCKSZ)) == BLCKSZ)
|
|
|
|
{
|
|
|
|
#ifdef PAGE_CONVERSION
|
|
|
|
if ((msg = pageConverter->convertPage(pageConverter->pluginData, buf, buf)) != NULL)
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
if (write(dstfd, buf, BLCKSZ) != BLCKSZ)
|
|
|
|
{
|
2011-07-12 12:13:51 +08:00
|
|
|
msg = "could not write new page to destination";
|
2010-05-12 10:19:11 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close(src_fd);
|
|
|
|
close(dstfd);
|
|
|
|
|
|
|
|
if (msg)
|
|
|
|
return msg;
|
|
|
|
else if (bytesRead != 0)
|
|
|
|
return "found partial page in source file";
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* linkAndUpdateFile()
|
|
|
|
*
|
2012-08-28 02:20:28 +08:00
|
|
|
* Creates a hard link between the given relation files. We use
|
2010-05-12 10:19:11 +08:00
|
|
|
* this function to perform a true in-place update. If the on-disk
|
|
|
|
* format of the new cluster is bit-for-bit compatible with the on-disk
|
2012-08-28 02:20:28 +08:00
|
|
|
* format of the old cluster, we can simply link each relation
|
2010-05-12 10:19:11 +08:00
|
|
|
* instead of copying the data from the old cluster to the new cluster.
|
|
|
|
*/
|
|
|
|
const char *
|
2010-10-20 05:38:16 +08:00
|
|
|
linkAndUpdateFile(pageCnvCtx *pageConverter,
|
2010-05-12 10:19:11 +08:00
|
|
|
const char *src, const char *dst)
|
|
|
|
{
|
|
|
|
if (pageConverter != NULL)
|
2011-07-12 12:13:51 +08:00
|
|
|
return "Cannot in-place update this cluster, page-by-page conversion is required";
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
if (pg_link_file(src, dst) == -1)
|
|
|
|
return getErrorText(errno);
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-04-24 08:27:57 +08:00
|
|
|
#ifndef WIN32
|
2010-05-12 10:19:11 +08:00
|
|
|
static int
|
|
|
|
copy_file(const char *srcfile, const char *dstfile, bool force)
|
|
|
|
{
|
|
|
|
#define COPY_BUF_SIZE (50 * BLCKSZ)
|
|
|
|
|
|
|
|
int src_fd;
|
|
|
|
int dest_fd;
|
|
|
|
char *buffer;
|
2012-11-25 11:39:03 +08:00
|
|
|
int ret = 0;
|
2013-05-30 04:58:43 +08:00
|
|
|
int save_errno = 0;
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
if ((srcfile == NULL) || (dstfile == NULL))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if ((src_fd = open(srcfile, O_RDONLY, 0)) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if ((dest_fd = open(dstfile, O_RDWR | O_CREAT | (force ? 0 : O_EXCL), S_IRUSR | S_IWUSR)) < 0)
|
|
|
|
{
|
|
|
|
if (src_fd != 0)
|
|
|
|
close(src_fd);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-11-25 11:12:39 +08:00
|
|
|
buffer = (char *) pg_malloc(COPY_BUF_SIZE);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
/* perform data copying i.e read src source, write to destination */
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
ssize_t nbytes = read(src_fd, buffer, COPY_BUF_SIZE);
|
|
|
|
|
|
|
|
if (nbytes < 0)
|
|
|
|
{
|
2012-11-25 11:39:03 +08:00
|
|
|
save_errno = errno;
|
|
|
|
ret = -1;
|
|
|
|
break;
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (nbytes == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
|
|
|
|
if (write(dest_fd, buffer, nbytes) != nbytes)
|
|
|
|
{
|
2012-11-25 11:39:03 +08:00
|
|
|
save_errno = errno;
|
|
|
|
ret = -1;
|
|
|
|
break;
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-25 11:39:03 +08:00
|
|
|
pg_free(buffer);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
if (src_fd != 0)
|
|
|
|
close(src_fd);
|
|
|
|
|
|
|
|
if (dest_fd != 0)
|
|
|
|
close(dest_fd);
|
|
|
|
|
2012-11-25 11:39:03 +08:00
|
|
|
if (save_errno != 0)
|
|
|
|
errno = save_errno;
|
|
|
|
|
|
|
|
return ret;
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
2011-04-24 08:27:57 +08:00
|
|
|
#endif
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
void
|
2010-10-20 05:38:16 +08:00
|
|
|
check_hard_link(void)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
|
|
|
char existing_file[MAXPGPATH];
|
|
|
|
char new_link_file[MAXPGPATH];
|
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
snprintf(existing_file, sizeof(existing_file), "%s/PG_VERSION", old_cluster.pgdata);
|
|
|
|
snprintf(new_link_file, sizeof(new_link_file), "%s/PG_VERSION.linktest", new_cluster.pgdata);
|
2010-05-12 10:19:11 +08:00
|
|
|
unlink(new_link_file); /* might fail */
|
|
|
|
|
|
|
|
if (pg_link_file(existing_file, new_link_file) == -1)
|
|
|
|
{
|
2010-10-20 05:38:16 +08:00
|
|
|
pg_log(PG_FATAL,
|
2011-07-12 12:13:51 +08:00
|
|
|
"Could not create hard link between old and new data directories: %s\n"
|
2010-05-12 10:19:11 +08:00
|
|
|
"In link mode the old and new data directories must be on the same file system volume.\n",
|
|
|
|
getErrorText(errno));
|
|
|
|
}
|
|
|
|
unlink(new_link_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
static int
|
|
|
|
win32_pghardlink(const char *src, const char *dst)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* CreateHardLinkA returns zero for failure
|
|
|
|
* http://msdn.microsoft.com/en-us/library/aa363860(VS.85).aspx
|
|
|
|
*/
|
|
|
|
if (CreateHardLinkA(dst, src, NULL) == 0)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
2012-03-13 07:47:54 +08:00
|
|
|
|
|
|
|
|
|
|
|
/* fopen() file with no group/other permissions */
|
|
|
|
FILE *
|
|
|
|
fopen_priv(const char *path, const char *mode)
|
|
|
|
{
|
2012-06-11 03:20:04 +08:00
|
|
|
mode_t old_umask = umask(S_IRWXG | S_IRWXO);
|
|
|
|
FILE *fp;
|
2012-03-13 07:47:54 +08:00
|
|
|
|
|
|
|
fp = fopen(path, mode);
|
|
|
|
umask(old_umask);
|
|
|
|
|
|
|
|
return fp;
|
|
|
|
}
|