mirror of
https://github.com/postgrespro/pg_probackup.git
synced 2024-12-01 09:51:43 +02:00
308d00b80c
The system function used up to now was pg_xlogfile_name_offset, which cannot be used on a node in recovery, and it was the only way present to fetch the timeline ID of a backup, either incremental or full. So instead scan the control file of server and fetch the timeline from that. This also removes the restriction on which a backup could not be taken on a standby node. The next step being to have the possibility to take backups from streams.
70 lines
1.8 KiB
C
70 lines
1.8 KiB
C
/*-------------------------------------------------------------------------
|
|
*
|
|
* fetch.c
|
|
* Functions for fetching files from PostgreSQL data directory
|
|
*
|
|
* Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
|
|
*
|
|
*-------------------------------------------------------------------------
|
|
*/
|
|
|
|
#include "postgres_fe.h"
|
|
|
|
#include "catalog/catalog.h"
|
|
|
|
#include <sys/types.h>
|
|
#include <sys/stat.h>
|
|
#include <dirent.h>
|
|
#include <fcntl.h>
|
|
#include <unistd.h>
|
|
#include <string.h>
|
|
|
|
#include "pg_rman.h"
|
|
|
|
/*
|
|
* Read a file into memory. The file to be read is <datadir>/<path>.
|
|
* The file contents are returned in a malloc'd buffer, and *filesize
|
|
* is set to the length of the file.
|
|
*
|
|
* The returned buffer is always zero-terminated; the size of the returned
|
|
* buffer is actually *filesize + 1. That's handy when reading a text file.
|
|
* This function can be used to read binary files as well, you can just
|
|
* ignore the zero-terminator in that case.
|
|
*
|
|
*/
|
|
char *
|
|
slurpFile(const char *datadir, const char *path, size_t *filesize)
|
|
{
|
|
int fd;
|
|
char *buffer;
|
|
struct stat statbuf;
|
|
char fullpath[MAXPGPATH];
|
|
int len;
|
|
snprintf(fullpath, sizeof(fullpath), "%s/%s", datadir, path);
|
|
|
|
if ((fd = open(fullpath, O_RDONLY | PG_BINARY, 0)) == -1)
|
|
elog(ERROR_CORRUPTED, _("could not open file \"%s\" for reading: %s\n"),
|
|
fullpath, strerror(errno));
|
|
|
|
if (fstat(fd, &statbuf) < 0)
|
|
elog(ERROR_CORRUPTED, _("could not open file \"%s\" for reading: %s\n"),
|
|
fullpath, strerror(errno));
|
|
|
|
len = statbuf.st_size;
|
|
|
|
buffer = pg_malloc(len + 1);
|
|
|
|
if (read(fd, buffer, len) != len)
|
|
elog(ERROR_CORRUPTED, _("could not read file \"%s\": %s\n"),
|
|
fullpath, strerror(errno));
|
|
|
|
close(fd);
|
|
|
|
/* Zero-terminate the buffer. */
|
|
buffer[len] = '\0';
|
|
|
|
if (filesize)
|
|
*filesize = len;
|
|
return buffer;
|
|
}
|