2010-05-12 04:19:11 +02:00
|
|
|
/*
|
|
|
|
* file.c
|
|
|
|
*
|
|
|
|
* file system operations
|
2010-07-03 16:23:14 +02:00
|
|
|
*
|
2016-01-02 19:33:40 +01:00
|
|
|
* Copyright (c) 2010-2016, PostgreSQL Global Development Group
|
2015-03-11 03:33:25 +01:00
|
|
|
* src/bin/pg_upgrade/file.c
|
2010-05-12 04:19:11 +02: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 14:33:40 +01:00
|
|
|
#include "postgres_fe.h"
|
2011-08-27 03:16:24 +02:00
|
|
|
|
pg_upgrade: Convert old visibility map format to new format.
Commit a892234f830e832110f63fc0a2afce2fb21d1584 added a second bit per
page to the visibility map, but pg_upgrade has been unaware of it up
until now. Therefore, a pg_upgrade from an earlier major release of
PostgreSQL to any commit preceding this one and following the one
mentioned above would result in invalid visibility map contents on the
new cluster, very possibly leading to data corruption. This plugs
that hole.
Masahiko Sawada, reviewed by Jeff Janes, Bruce Momjian, Simon Riggs,
Michael Paquier, Andres Freund, me, and others.
2016-03-11 18:28:22 +01:00
|
|
|
#include "access/visibilitymap.h"
|
2010-05-12 04:19:11 +02:00
|
|
|
#include "pg_upgrade.h"
|
pg_upgrade: Convert old visibility map format to new format.
Commit a892234f830e832110f63fc0a2afce2fb21d1584 added a second bit per
page to the visibility map, but pg_upgrade has been unaware of it up
until now. Therefore, a pg_upgrade from an earlier major release of
PostgreSQL to any commit preceding this one and following the one
mentioned above would result in invalid visibility map contents on the
new cluster, very possibly leading to data corruption. This plugs
that hole.
Masahiko Sawada, reviewed by Jeff Janes, Bruce Momjian, Simon Riggs,
Michael Paquier, Andres Freund, me, and others.
2016-03-11 18:28:22 +01:00
|
|
|
#include "storage/bufpage.h"
|
|
|
|
#include "storage/checksum.h"
|
|
|
|
#include "storage/checksum_impl.h"
|
2010-05-12 04:19:11 +02:00
|
|
|
|
pg_upgrade: Convert old visibility map format to new format.
Commit a892234f830e832110f63fc0a2afce2fb21d1584 added a second bit per
page to the visibility map, but pg_upgrade has been unaware of it up
until now. Therefore, a pg_upgrade from an earlier major release of
PostgreSQL to any commit preceding this one and following the one
mentioned above would result in invalid visibility map contents on the
new cluster, very possibly leading to data corruption. This plugs
that hole.
Masahiko Sawada, reviewed by Jeff Janes, Bruce Momjian, Simon Riggs,
Michael Paquier, Andres Freund, me, and others.
2016-03-11 18:28:22 +01:00
|
|
|
#include <sys/stat.h>
|
2010-05-12 04:19:11 +02:00
|
|
|
#include <fcntl.h>
|
|
|
|
|
pg_upgrade: Convert old visibility map format to new format.
Commit a892234f830e832110f63fc0a2afce2fb21d1584 added a second bit per
page to the visibility map, but pg_upgrade has been unaware of it up
until now. Therefore, a pg_upgrade from an earlier major release of
PostgreSQL to any commit preceding this one and following the one
mentioned above would result in invalid visibility map contents on the
new cluster, very possibly leading to data corruption. This plugs
that hole.
Masahiko Sawada, reviewed by Jeff Janes, Bruce Momjian, Simon Riggs,
Michael Paquier, Andres Freund, me, and others.
2016-03-11 18:28:22 +01:00
|
|
|
#define BITS_PER_HEAPBLOCK_OLD 1
|
2010-05-12 04:19:11 +02:00
|
|
|
|
|
|
|
|
2011-04-24 02:27:57 +02:00
|
|
|
#ifndef WIN32
|
2016-06-06 15:51:56 +02:00
|
|
|
static int copy_file(const char *fromfile, const char *tofile);
|
2011-04-24 02:27:57 +02:00
|
|
|
#else
|
2010-05-12 04:19:11 +02:00
|
|
|
static int win32_pghardlink(const char *src, const char *dst);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2016-03-08 14:13:02 +01:00
|
|
|
* copyFile()
|
2010-05-12 04:19:11 +02:00
|
|
|
*
|
2016-03-08 14:13:02 +01:00
|
|
|
* Copies a relation file from src to dst.
|
2010-05-12 04:19:11 +02:00
|
|
|
*/
|
|
|
|
const char *
|
2016-06-06 15:51:56 +02:00
|
|
|
copyFile(const char *src, const char *dst)
|
2010-05-12 04:19:11 +02:00
|
|
|
{
|
2015-11-14 17:47:12 +01:00
|
|
|
#ifndef WIN32
|
2016-06-10 00:02:36 +02:00
|
|
|
if (copy_file(src, dst) == -1)
|
2015-11-14 17:47:12 +01:00
|
|
|
#else
|
2016-06-10 00:02:36 +02:00
|
|
|
if (CopyFile(src, dst, true) == 0)
|
2015-11-14 17:47:12 +01:00
|
|
|
#endif
|
2016-06-10 00:02:36 +02:00
|
|
|
return getErrorText();
|
|
|
|
else
|
|
|
|
return NULL;
|
2010-05-12 04:19:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2016-03-08 14:13:02 +01:00
|
|
|
* linkFile()
|
2010-05-12 04:19:11 +02:00
|
|
|
*
|
2012-08-27 20:20:28 +02:00
|
|
|
* Creates a hard link between the given relation files. We use
|
2010-05-12 04:19:11 +02: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-27 20:20:28 +02:00
|
|
|
* format of the old cluster, we can simply link each relation
|
2010-05-12 04:19:11 +02:00
|
|
|
* instead of copying the data from the old cluster to the new cluster.
|
|
|
|
*/
|
|
|
|
const char *
|
2016-03-08 14:13:02 +01:00
|
|
|
linkFile(const char *src, const char *dst)
|
2010-05-12 04:19:11 +02:00
|
|
|
{
|
|
|
|
if (pg_link_file(src, dst) == -1)
|
2015-11-24 23:18:28 +01:00
|
|
|
return getErrorText();
|
2010-05-12 04:19:11 +02:00
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-04-24 02:27:57 +02:00
|
|
|
#ifndef WIN32
|
2010-05-12 04:19:11 +02:00
|
|
|
static int
|
2016-06-06 15:51:56 +02:00
|
|
|
copy_file(const char *srcfile, const char *dstfile)
|
2010-05-12 04:19:11 +02:00
|
|
|
{
|
|
|
|
#define COPY_BUF_SIZE (50 * BLCKSZ)
|
|
|
|
|
|
|
|
int src_fd;
|
|
|
|
int dest_fd;
|
|
|
|
char *buffer;
|
2012-11-25 04:39:03 +01:00
|
|
|
int ret = 0;
|
2013-05-29 22:58:43 +02:00
|
|
|
int save_errno = 0;
|
2010-05-12 04:19:11 +02:00
|
|
|
|
|
|
|
if ((srcfile == NULL) || (dstfile == NULL))
|
2013-11-19 03:49:40 +01:00
|
|
|
{
|
|
|
|
errno = EINVAL;
|
2010-05-12 04:19:11 +02:00
|
|
|
return -1;
|
2013-11-19 03:49:40 +01:00
|
|
|
}
|
2010-05-12 04:19:11 +02:00
|
|
|
|
|
|
|
if ((src_fd = open(srcfile, O_RDONLY, 0)) < 0)
|
|
|
|
return -1;
|
|
|
|
|
2016-06-06 15:51:56 +02:00
|
|
|
if ((dest_fd = open(dstfile, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR)) < 0)
|
2010-05-12 04:19:11 +02:00
|
|
|
{
|
2013-11-19 03:49:40 +01:00
|
|
|
save_errno = errno;
|
|
|
|
|
2010-05-12 04:19:11 +02:00
|
|
|
if (src_fd != 0)
|
|
|
|
close(src_fd);
|
|
|
|
|
2013-11-19 03:49:40 +01:00
|
|
|
errno = save_errno;
|
2010-05-12 04:19:11 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-11-25 04:12:39 +01:00
|
|
|
buffer = (char *) pg_malloc(COPY_BUF_SIZE);
|
2010-05-12 04:19:11 +02: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 04:39:03 +01:00
|
|
|
save_errno = errno;
|
|
|
|
ret = -1;
|
|
|
|
break;
|
2010-05-12 04:19:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (nbytes == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
|
|
|
|
if (write(dest_fd, buffer, nbytes) != nbytes)
|
|
|
|
{
|
2013-11-19 03:49:40 +01:00
|
|
|
/* if write didn't set errno, assume problem is no disk space */
|
|
|
|
if (errno == 0)
|
|
|
|
errno = ENOSPC;
|
2012-11-25 04:39:03 +01:00
|
|
|
save_errno = errno;
|
|
|
|
ret = -1;
|
|
|
|
break;
|
2010-05-12 04:19:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-25 04:39:03 +01:00
|
|
|
pg_free(buffer);
|
2010-05-12 04:19:11 +02:00
|
|
|
|
|
|
|
if (src_fd != 0)
|
|
|
|
close(src_fd);
|
|
|
|
|
|
|
|
if (dest_fd != 0)
|
|
|
|
close(dest_fd);
|
|
|
|
|
2012-11-25 04:39:03 +01:00
|
|
|
if (save_errno != 0)
|
|
|
|
errno = save_errno;
|
|
|
|
|
|
|
|
return ret;
|
2010-05-12 04:19:11 +02:00
|
|
|
}
|
2011-04-24 02:27:57 +02:00
|
|
|
#endif
|
2010-05-12 04:19:11 +02:00
|
|
|
|
|
|
|
|
pg_upgrade: Convert old visibility map format to new format.
Commit a892234f830e832110f63fc0a2afce2fb21d1584 added a second bit per
page to the visibility map, but pg_upgrade has been unaware of it up
until now. Therefore, a pg_upgrade from an earlier major release of
PostgreSQL to any commit preceding this one and following the one
mentioned above would result in invalid visibility map contents on the
new cluster, very possibly leading to data corruption. This plugs
that hole.
Masahiko Sawada, reviewed by Jeff Janes, Bruce Momjian, Simon Riggs,
Michael Paquier, Andres Freund, me, and others.
2016-03-11 18:28:22 +01:00
|
|
|
/*
|
|
|
|
* rewriteVisibilityMap()
|
|
|
|
*
|
|
|
|
* In versions of PostgreSQL prior to catversion 201603011, PostgreSQL's
|
|
|
|
* visibility map included one bit per heap page; it now includes two.
|
|
|
|
* When upgrading a cluster from before that time to a current PostgreSQL
|
|
|
|
* version, we could refuse to copy visibility maps from the old cluster
|
|
|
|
* to the new cluster; the next VACUUM would recreate them, but at the
|
|
|
|
* price of scanning the entire table. So, instead, we rewrite the old
|
|
|
|
* visibility maps in the new format. That way, the all-visible bit
|
|
|
|
* remains set for the pages for which it was set previously. The
|
|
|
|
* all-frozen bit is never set by this conversion; we leave that to
|
|
|
|
* VACUUM.
|
|
|
|
*/
|
|
|
|
const char *
|
2016-06-06 15:51:56 +02:00
|
|
|
rewriteVisibilityMap(const char *fromfile, const char *tofile)
|
pg_upgrade: Convert old visibility map format to new format.
Commit a892234f830e832110f63fc0a2afce2fb21d1584 added a second bit per
page to the visibility map, but pg_upgrade has been unaware of it up
until now. Therefore, a pg_upgrade from an earlier major release of
PostgreSQL to any commit preceding this one and following the one
mentioned above would result in invalid visibility map contents on the
new cluster, very possibly leading to data corruption. This plugs
that hole.
Masahiko Sawada, reviewed by Jeff Janes, Bruce Momjian, Simon Riggs,
Michael Paquier, Andres Freund, me, and others.
2016-03-11 18:28:22 +01:00
|
|
|
{
|
|
|
|
int src_fd = 0;
|
|
|
|
int dst_fd = 0;
|
|
|
|
char buffer[BLCKSZ];
|
|
|
|
ssize_t bytesRead;
|
2016-06-06 12:14:21 +02:00
|
|
|
ssize_t totalBytesRead = 0;
|
pg_upgrade: Convert old visibility map format to new format.
Commit a892234f830e832110f63fc0a2afce2fb21d1584 added a second bit per
page to the visibility map, but pg_upgrade has been unaware of it up
until now. Therefore, a pg_upgrade from an earlier major release of
PostgreSQL to any commit preceding this one and following the one
mentioned above would result in invalid visibility map contents on the
new cluster, very possibly leading to data corruption. This plugs
that hole.
Masahiko Sawada, reviewed by Jeff Janes, Bruce Momjian, Simon Riggs,
Michael Paquier, Andres Freund, me, and others.
2016-03-11 18:28:22 +01:00
|
|
|
ssize_t src_filesize;
|
|
|
|
int rewriteVmBytesPerPage;
|
|
|
|
BlockNumber new_blkno = 0;
|
|
|
|
struct stat statbuf;
|
|
|
|
|
|
|
|
/* Compute we need how many old page bytes to rewrite a new page */
|
|
|
|
rewriteVmBytesPerPage = (BLCKSZ - SizeOfPageHeaderData) / 2;
|
|
|
|
|
|
|
|
if ((fromfile == NULL) || (tofile == NULL))
|
|
|
|
return "Invalid old file or new file";
|
|
|
|
|
|
|
|
if ((src_fd = open(fromfile, O_RDONLY, 0)) < 0)
|
|
|
|
return getErrorText();
|
|
|
|
|
|
|
|
if (fstat(src_fd, &statbuf) != 0)
|
|
|
|
{
|
|
|
|
close(src_fd);
|
|
|
|
return getErrorText();
|
|
|
|
}
|
|
|
|
|
2016-06-06 15:51:56 +02:00
|
|
|
if ((dst_fd = open(tofile, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR)) < 0)
|
pg_upgrade: Convert old visibility map format to new format.
Commit a892234f830e832110f63fc0a2afce2fb21d1584 added a second bit per
page to the visibility map, but pg_upgrade has been unaware of it up
until now. Therefore, a pg_upgrade from an earlier major release of
PostgreSQL to any commit preceding this one and following the one
mentioned above would result in invalid visibility map contents on the
new cluster, very possibly leading to data corruption. This plugs
that hole.
Masahiko Sawada, reviewed by Jeff Janes, Bruce Momjian, Simon Riggs,
Michael Paquier, Andres Freund, me, and others.
2016-03-11 18:28:22 +01:00
|
|
|
{
|
|
|
|
close(src_fd);
|
|
|
|
return getErrorText();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save old file size */
|
|
|
|
src_filesize = statbuf.st_size;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Turn each visibility map page into 2 pages one by one. Each new page
|
|
|
|
* has the same page header as the old one. If the last section of last
|
|
|
|
* page is empty, we skip it, mostly to avoid turning one-page visibility
|
|
|
|
* maps for small relations into two pages needlessly.
|
|
|
|
*/
|
2016-06-06 12:14:21 +02:00
|
|
|
while (totalBytesRead < src_filesize)
|
pg_upgrade: Convert old visibility map format to new format.
Commit a892234f830e832110f63fc0a2afce2fb21d1584 added a second bit per
page to the visibility map, but pg_upgrade has been unaware of it up
until now. Therefore, a pg_upgrade from an earlier major release of
PostgreSQL to any commit preceding this one and following the one
mentioned above would result in invalid visibility map contents on the
new cluster, very possibly leading to data corruption. This plugs
that hole.
Masahiko Sawada, reviewed by Jeff Janes, Bruce Momjian, Simon Riggs,
Michael Paquier, Andres Freund, me, and others.
2016-03-11 18:28:22 +01:00
|
|
|
{
|
|
|
|
char *old_cur;
|
|
|
|
char *old_break;
|
|
|
|
char *old_blkend;
|
|
|
|
PageHeaderData pageheader;
|
2016-06-06 12:14:21 +02:00
|
|
|
bool old_lastblk;
|
|
|
|
|
|
|
|
if ((bytesRead = read(src_fd, buffer, BLCKSZ)) != BLCKSZ)
|
|
|
|
{
|
|
|
|
close(dst_fd);
|
|
|
|
close(src_fd);
|
|
|
|
return getErrorText();
|
|
|
|
}
|
|
|
|
|
|
|
|
totalBytesRead += BLCKSZ;
|
|
|
|
old_lastblk = (totalBytesRead == src_filesize);
|
pg_upgrade: Convert old visibility map format to new format.
Commit a892234f830e832110f63fc0a2afce2fb21d1584 added a second bit per
page to the visibility map, but pg_upgrade has been unaware of it up
until now. Therefore, a pg_upgrade from an earlier major release of
PostgreSQL to any commit preceding this one and following the one
mentioned above would result in invalid visibility map contents on the
new cluster, very possibly leading to data corruption. This plugs
that hole.
Masahiko Sawada, reviewed by Jeff Janes, Bruce Momjian, Simon Riggs,
Michael Paquier, Andres Freund, me, and others.
2016-03-11 18:28:22 +01:00
|
|
|
|
|
|
|
/* Save the page header data */
|
|
|
|
memcpy(&pageheader, buffer, SizeOfPageHeaderData);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* These old_* variables point to old visibility map page. old_cur
|
|
|
|
* points to current position on old page. old_blkend points to end of
|
|
|
|
* old block. old_break points to old page break position for
|
|
|
|
* rewriting a new page. After wrote a new page, old_break proceeds
|
|
|
|
* rewriteVmBytesPerPage bytes.
|
|
|
|
*/
|
|
|
|
old_cur = buffer + SizeOfPageHeaderData;
|
|
|
|
old_blkend = buffer + bytesRead;
|
|
|
|
old_break = old_cur + rewriteVmBytesPerPage;
|
|
|
|
|
|
|
|
while (old_blkend >= old_break)
|
|
|
|
{
|
|
|
|
char new_vmbuf[BLCKSZ];
|
|
|
|
char *new_cur = new_vmbuf;
|
|
|
|
bool empty = true;
|
|
|
|
bool old_lastpart;
|
|
|
|
|
|
|
|
/* Copy page header in advance */
|
|
|
|
memcpy(new_vmbuf, &pageheader, SizeOfPageHeaderData);
|
|
|
|
|
|
|
|
/* Rewrite the last part of the old page? */
|
|
|
|
old_lastpart = old_lastblk && (old_blkend == old_break);
|
|
|
|
|
|
|
|
new_cur += SizeOfPageHeaderData;
|
|
|
|
|
|
|
|
/* Process old page bytes one by one, and turn it into new page. */
|
|
|
|
while (old_break > old_cur)
|
|
|
|
{
|
|
|
|
uint16 new_vmbits = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Generate new format bits while keeping old information */
|
|
|
|
for (i = 0; i < BITS_PER_BYTE; i++)
|
|
|
|
{
|
|
|
|
uint8 byte = *(uint8 *) old_cur;
|
|
|
|
|
|
|
|
if (byte & (1 << (BITS_PER_HEAPBLOCK_OLD * i)))
|
|
|
|
{
|
|
|
|
empty = false;
|
|
|
|
new_vmbits |= 1 << (BITS_PER_HEAPBLOCK * i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy new visibility map bit to new format page */
|
|
|
|
memcpy(new_cur, &new_vmbits, BITS_PER_HEAPBLOCK);
|
|
|
|
|
|
|
|
old_cur += BITS_PER_HEAPBLOCK_OLD;
|
|
|
|
new_cur += BITS_PER_HEAPBLOCK;
|
|
|
|
}
|
|
|
|
|
2016-06-16 18:43:35 +02:00
|
|
|
/* If the last part of the old page is empty, skip writing it */
|
pg_upgrade: Convert old visibility map format to new format.
Commit a892234f830e832110f63fc0a2afce2fb21d1584 added a second bit per
page to the visibility map, but pg_upgrade has been unaware of it up
until now. Therefore, a pg_upgrade from an earlier major release of
PostgreSQL to any commit preceding this one and following the one
mentioned above would result in invalid visibility map contents on the
new cluster, very possibly leading to data corruption. This plugs
that hole.
Masahiko Sawada, reviewed by Jeff Janes, Bruce Momjian, Simon Riggs,
Michael Paquier, Andres Freund, me, and others.
2016-03-11 18:28:22 +01:00
|
|
|
if (old_lastpart && empty)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Set new checksum for a visibility map page (if enabled) */
|
|
|
|
if (old_cluster.controldata.data_checksum_version != 0 &&
|
|
|
|
new_cluster.controldata.data_checksum_version != 0)
|
|
|
|
((PageHeader) new_vmbuf)->pd_checksum =
|
|
|
|
pg_checksum_page(new_vmbuf, new_blkno);
|
|
|
|
|
|
|
|
if (write(dst_fd, new_vmbuf, BLCKSZ) != BLCKSZ)
|
|
|
|
{
|
|
|
|
close(dst_fd);
|
|
|
|
close(src_fd);
|
|
|
|
return getErrorText();
|
|
|
|
}
|
|
|
|
|
|
|
|
old_break += rewriteVmBytesPerPage;
|
|
|
|
new_blkno++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Close files */
|
|
|
|
close(dst_fd);
|
|
|
|
close(src_fd);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-05-12 04:19:11 +02:00
|
|
|
void
|
2010-10-19 23:38:16 +02:00
|
|
|
check_hard_link(void)
|
2010-05-12 04:19:11 +02:00
|
|
|
{
|
|
|
|
char existing_file[MAXPGPATH];
|
|
|
|
char new_link_file[MAXPGPATH];
|
|
|
|
|
2010-10-19 23:38:16 +02: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 04:19:11 +02:00
|
|
|
unlink(new_link_file); /* might fail */
|
|
|
|
|
|
|
|
if (pg_link_file(existing_file, new_link_file) == -1)
|
|
|
|
{
|
2013-10-02 03:24:56 +02:00
|
|
|
pg_fatal("Could not create hard link between old and new data directories: %s\n"
|
2014-05-06 18:12:18 +02:00
|
|
|
"In link mode the old and new data directories must be on the same file system volume.\n",
|
2015-11-24 23:18:28 +01:00
|
|
|
getErrorText());
|
2010-05-12 04:19:11 +02:00
|
|
|
}
|
|
|
|
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 00:47:54 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* fopen() file with no group/other permissions */
|
|
|
|
FILE *
|
|
|
|
fopen_priv(const char *path, const char *mode)
|
|
|
|
{
|
2012-06-10 21:20:04 +02:00
|
|
|
mode_t old_umask = umask(S_IRWXG | S_IRWXO);
|
|
|
|
FILE *fp;
|
2012-03-13 00:47:54 +01:00
|
|
|
|
|
|
|
fp = fopen(path, mode);
|
|
|
|
umask(old_umask);
|
|
|
|
|
|
|
|
return fp;
|
|
|
|
}
|