2013-12-12 21:55:39 +03:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* fetch.c
|
|
|
|
* Functions for fetching files from PostgreSQL data directory
|
|
|
|
*
|
2017-02-12 22:42:10 +02:00
|
|
|
* Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
|
2013-12-12 21:55:39 +03:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2018-10-18 14:43:30 +02:00
|
|
|
#include "pg_probackup.h"
|
|
|
|
|
2013-12-12 21:55:39 +03:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.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 *
|
2016-03-02 19:50:33 +02:00
|
|
|
slurpFile(const char *datadir, const char *path, size_t *filesize, bool safe)
|
2013-12-12 21:55:39 +03:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
char *buffer;
|
|
|
|
struct stat statbuf;
|
|
|
|
char fullpath[MAXPGPATH];
|
|
|
|
int len;
|
|
|
|
snprintf(fullpath, sizeof(fullpath), "%s/%s", datadir, path);
|
|
|
|
|
2018-11-04 09:02:26 +02:00
|
|
|
if ((fd = fio_open(fullpath, O_RDONLY | PG_BINARY, FIO_DB_HOST)) == -1)
|
2016-03-02 19:50:33 +02:00
|
|
|
{
|
|
|
|
if (safe)
|
|
|
|
return NULL;
|
|
|
|
else
|
|
|
|
elog(ERROR, "could not open file \"%s\" for reading: %s",
|
|
|
|
fullpath, strerror(errno));
|
|
|
|
}
|
2013-12-12 21:55:39 +03:00
|
|
|
|
2018-11-04 09:02:26 +02:00
|
|
|
if (fio_stat(fd, &statbuf) < 0)
|
2016-03-02 19:50:33 +02:00
|
|
|
{
|
|
|
|
if (safe)
|
|
|
|
return NULL;
|
|
|
|
else
|
|
|
|
elog(ERROR, "could not open file \"%s\" for reading: %s",
|
|
|
|
fullpath, strerror(errno));
|
|
|
|
}
|
2013-12-12 21:55:39 +03:00
|
|
|
|
|
|
|
len = statbuf.st_size;
|
|
|
|
buffer = pg_malloc(len + 1);
|
|
|
|
|
2018-11-04 09:02:26 +02:00
|
|
|
if (fio_read(fd, buffer, len) != len)
|
2016-03-02 19:50:33 +02:00
|
|
|
{
|
|
|
|
if (safe)
|
|
|
|
return NULL;
|
|
|
|
else
|
|
|
|
elog(ERROR, "could not read file \"%s\": %s\n",
|
|
|
|
fullpath, strerror(errno));
|
|
|
|
}
|
2013-12-12 21:55:39 +03:00
|
|
|
|
2018-11-04 09:02:26 +02:00
|
|
|
fio_close(fd);
|
2013-12-12 21:55:39 +03:00
|
|
|
|
|
|
|
/* Zero-terminate the buffer. */
|
|
|
|
buffer[len] = '\0';
|
|
|
|
|
|
|
|
if (filesize)
|
|
|
|
*filesize = len;
|
|
|
|
return buffer;
|
|
|
|
}
|
2017-12-11 14:44:54 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Receive a single file as a malloc'd buffer.
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
fetchFile(PGconn *conn, const char *filename, size_t *filesize)
|
|
|
|
{
|
|
|
|
PGresult *res;
|
|
|
|
char *result;
|
|
|
|
const char *params[1];
|
|
|
|
int len;
|
|
|
|
|
|
|
|
params[0] = filename;
|
2018-05-16 09:00:49 +02:00
|
|
|
res = pgut_execute_extended(conn, "SELECT pg_catalog.pg_read_binary_file($1)",
|
|
|
|
1, params, false, false);
|
2017-12-11 14:44:54 +02:00
|
|
|
|
|
|
|
/* sanity check the result set */
|
|
|
|
if (PQntuples(res) != 1 || PQgetisnull(res, 0, 0))
|
|
|
|
elog(ERROR, "unexpected result set while fetching remote file \"%s\"",
|
|
|
|
filename);
|
|
|
|
|
|
|
|
/* Read result to local variables */
|
|
|
|
len = PQgetlength(res, 0, 0);
|
|
|
|
result = pg_malloc(len + 1);
|
|
|
|
memcpy(result, PQgetvalue(res, 0, 0), len);
|
|
|
|
result[len] = '\0';
|
|
|
|
|
|
|
|
PQclear(res);
|
|
|
|
*filesize = len;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|