2010-05-12 10:19:11 +08:00
|
|
|
/*
|
|
|
|
* file.c
|
|
|
|
*
|
|
|
|
* file system operations
|
2010-07-03 22:23:14 +08:00
|
|
|
*
|
2011-01-02 02:18:15 +08:00
|
|
|
* Copyright (c) 2010-2011, 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
|
|
|
*/
|
|
|
|
|
2011-08-27 09:16:24 +08:00
|
|
|
#include "postgres.h"
|
|
|
|
|
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
|
|
|
|
|
2010-05-14 06:07:43 +08:00
|
|
|
#ifndef HAVE_SCANDIR
|
2010-10-20 05:38:16 +08:00
|
|
|
static int pg_scandir_internal(const char *dirname,
|
2010-05-12 10:19:11 +08:00
|
|
|
struct dirent *** namelist,
|
|
|
|
int (*selector) (const struct dirent *));
|
|
|
|
#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()
|
|
|
|
*
|
|
|
|
* Creates a symbolic link between the given relation files. We use
|
|
|
|
* 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
|
|
|
|
* format of the old cluster, we can simply symlink each relation
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer = (char *) malloc(COPY_BUF_SIZE);
|
|
|
|
|
|
|
|
if (buffer == NULL)
|
|
|
|
{
|
|
|
|
if (src_fd != 0)
|
|
|
|
close(src_fd);
|
|
|
|
|
|
|
|
if (dest_fd != 0)
|
|
|
|
close(dest_fd);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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)
|
|
|
|
{
|
2010-10-20 06:37:04 +08:00
|
|
|
int save_errno = errno;
|
|
|
|
|
2010-05-12 10:19:11 +08:00
|
|
|
if (buffer != NULL)
|
|
|
|
free(buffer);
|
|
|
|
|
|
|
|
if (src_fd != 0)
|
|
|
|
close(src_fd);
|
|
|
|
|
|
|
|
if (dest_fd != 0)
|
|
|
|
close(dest_fd);
|
|
|
|
|
2010-07-10 00:51:23 +08:00
|
|
|
errno = save_errno;
|
2010-05-12 10:19:11 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nbytes == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
|
|
|
|
if (write(dest_fd, buffer, nbytes) != nbytes)
|
|
|
|
{
|
|
|
|
/* if write didn't set errno, assume problem is no disk space */
|
2010-10-20 06:37:04 +08:00
|
|
|
int save_errno = errno ? errno : ENOSPC;
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
if (buffer != NULL)
|
|
|
|
free(buffer);
|
|
|
|
|
|
|
|
if (src_fd != 0)
|
|
|
|
close(src_fd);
|
|
|
|
|
|
|
|
if (dest_fd != 0)
|
|
|
|
close(dest_fd);
|
|
|
|
|
2010-07-10 00:51:23 +08:00
|
|
|
errno = save_errno;
|
2010-05-12 10:19:11 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buffer != NULL)
|
|
|
|
free(buffer);
|
|
|
|
|
|
|
|
if (src_fd != 0)
|
|
|
|
close(src_fd);
|
|
|
|
|
|
|
|
if (dest_fd != 0)
|
|
|
|
close(dest_fd);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2011-04-24 08:27:57 +08:00
|
|
|
#endif
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* pg_scandir()
|
|
|
|
*
|
|
|
|
* Wrapper for portable scandir functionality
|
|
|
|
*/
|
|
|
|
int
|
2010-10-20 05:38:16 +08:00
|
|
|
pg_scandir(const char *dirname,
|
2010-07-07 03:19:02 +08:00
|
|
|
struct dirent *** namelist,
|
2010-05-13 00:50:00 +08:00
|
|
|
int (*selector) (const struct dirent *))
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2010-05-14 06:07:43 +08:00
|
|
|
#ifndef HAVE_SCANDIR
|
2010-10-20 05:38:16 +08:00
|
|
|
return pg_scandir_internal(dirname, namelist, selector);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
/*
|
2010-05-14 06:07:43 +08:00
|
|
|
* scandir() is originally from BSD 4.3, which had the third argument as
|
|
|
|
* non-const. Linux and other C libraries have updated it to use a const.
|
2010-07-07 03:19:02 +08:00
|
|
|
* http://unix.derkeiler.com/Mailing-Lists/FreeBSD/questions/2005-12/msg002
|
|
|
|
* 14.html
|
2010-05-14 06:07:43 +08:00
|
|
|
*
|
2010-05-12 10:19:11 +08:00
|
|
|
* Here we try to guess which libc's need const, and which don't. The net
|
2010-05-14 06:07:43 +08:00
|
|
|
* goal here is to try to suppress a compiler warning due to a prototype
|
2010-05-12 10:19:11 +08:00
|
|
|
* mismatch of const usage. Ideally we would do this via autoconf, but
|
2010-07-07 03:19:02 +08:00
|
|
|
* autoconf doesn't have a suitable builtin test and it seems overkill to
|
|
|
|
* add one just to avoid a warning.
|
2010-05-12 10:19:11 +08:00
|
|
|
*/
|
2010-05-15 18:21:54 +08:00
|
|
|
#elif defined(__FreeBSD__) || defined(__bsdi__) || defined(__darwin__) || defined(__OpenBSD__)
|
2010-05-12 10:19:11 +08:00
|
|
|
/* no const */
|
2010-05-13 00:50:00 +08:00
|
|
|
return scandir(dirname, namelist, (int (*) (struct dirent *)) selector, NULL);
|
2010-05-12 10:19:11 +08:00
|
|
|
#else
|
|
|
|
/* use const */
|
2010-05-13 00:50:00 +08:00
|
|
|
return scandir(dirname, namelist, selector, NULL);
|
2010-05-12 10:19:11 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-14 06:07:43 +08:00
|
|
|
#ifndef HAVE_SCANDIR
|
2010-05-12 10:19:11 +08:00
|
|
|
/*
|
|
|
|
* pg_scandir_internal()
|
|
|
|
*
|
2010-05-14 06:07:43 +08:00
|
|
|
* Implement our own scandir() on platforms that don't have it.
|
2010-05-12 10:19:11 +08:00
|
|
|
*
|
|
|
|
* Returns count of files that meet the selection criteria coded in
|
|
|
|
* the function pointed to by selector. Creates an array of pointers
|
|
|
|
* to dirent structures. Address of array returned in namelist.
|
|
|
|
*
|
|
|
|
* Note that the number of dirent structures needed is dynamically
|
2010-05-14 06:07:43 +08:00
|
|
|
* allocated using realloc. Realloc can be inefficient if invoked a
|
2010-05-12 10:19:11 +08:00
|
|
|
* large number of times. Its use in pg_upgrade is to find filesystem
|
|
|
|
* filenames that have extended beyond the initial segment (file.1,
|
|
|
|
* .2, etc.) and should therefore be invoked a small number of times.
|
|
|
|
*/
|
|
|
|
static int
|
2010-10-20 05:38:16 +08:00
|
|
|
pg_scandir_internal(const char *dirname,
|
2010-05-12 10:19:11 +08:00
|
|
|
struct dirent *** namelist, int (*selector) (const struct dirent *))
|
|
|
|
{
|
|
|
|
DIR *dirdesc;
|
|
|
|
struct dirent *direntry;
|
|
|
|
int count = 0;
|
|
|
|
int name_num = 0;
|
|
|
|
size_t entrysize;
|
|
|
|
|
|
|
|
if ((dirdesc = opendir(dirname)) == NULL)
|
2011-07-08 02:21:57 +08:00
|
|
|
pg_log(PG_FATAL, "could not open directory \"%s\": %s\n", dirname, getErrorText(errno));
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
*namelist = NULL;
|
|
|
|
|
|
|
|
while ((direntry = readdir(dirdesc)) != NULL)
|
|
|
|
{
|
|
|
|
/* Invoke the selector function to see if the direntry matches */
|
|
|
|
if ((*selector) (direntry))
|
|
|
|
{
|
|
|
|
count++;
|
|
|
|
|
|
|
|
*namelist = (struct dirent **) realloc((void *) (*namelist),
|
|
|
|
(size_t) ((name_num + 1) * sizeof(struct dirent *)));
|
|
|
|
|
|
|
|
if (*namelist == NULL)
|
2011-03-09 10:35:42 +08:00
|
|
|
{
|
|
|
|
closedir(dirdesc);
|
2010-05-12 10:19:11 +08:00
|
|
|
return -1;
|
2011-03-09 10:35:42 +08:00
|
|
|
}
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
entrysize = sizeof(struct dirent) - sizeof(direntry->d_name) +
|
|
|
|
strlen(direntry->d_name) + 1;
|
|
|
|
|
|
|
|
(*namelist)[name_num] = (struct dirent *) malloc(entrysize);
|
|
|
|
|
|
|
|
if ((*namelist)[name_num] == NULL)
|
2011-03-09 10:35:42 +08:00
|
|
|
{
|
|
|
|
closedir(dirdesc);
|
2010-05-12 10:19:11 +08:00
|
|
|
return -1;
|
2011-03-09 10:35:42 +08:00
|
|
|
}
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
memcpy((*namelist)[name_num], direntry, entrysize);
|
|
|
|
|
|
|
|
name_num++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
closedir(dirdesc);
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dir_matching_filenames
|
|
|
|
*
|
|
|
|
* Return only matching file names during directory scan
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
dir_matching_filenames(const struct dirent * scan_ent)
|
|
|
|
{
|
|
|
|
/* we only compare for string length because the number suffix varies */
|
|
|
|
if (!strncmp(scandir_file_pattern, scan_ent->d_name, strlen(scandir_file_pattern)))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2011-04-10 23:42:00 +08:00
|
|
|
|
2010-05-12 10:19:11 +08:00
|
|
|
#endif
|