mirror of
https://github.com/postgrespro/pg_probackup.git
synced 2025-02-07 14:18:17 +02:00
[Issue #169] added function slurpFileFullPath()
This commit is contained in:
parent
a1e005efa2
commit
2ad2af98f1
61
src/fetch.c
61
src/fetch.c
@ -13,6 +13,67 @@
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/*
|
||||
* Read a file into memory.
|
||||
* 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 *
|
||||
slurpFileFullPath(const char *from_fullpath, size_t *filesize, bool safe, fio_location location)
|
||||
{
|
||||
int fd;
|
||||
char *buffer;
|
||||
int len;
|
||||
struct stat statbuf;
|
||||
|
||||
if ((fd = fio_open(from_fullpath, O_RDONLY | PG_BINARY, location)) == -1)
|
||||
{
|
||||
if (safe)
|
||||
return NULL;
|
||||
else
|
||||
elog(ERROR, "Could not open file \"%s\" for reading: %s",
|
||||
from_fullpath, strerror(errno));
|
||||
}
|
||||
|
||||
if (fio_fstat(fd, &statbuf) < 0)
|
||||
{
|
||||
if (safe)
|
||||
return NULL;
|
||||
else
|
||||
elog(ERROR, "Could not stat file \"%s\": %s",
|
||||
from_fullpath, strerror(errno));
|
||||
}
|
||||
|
||||
len = statbuf.st_size;
|
||||
|
||||
buffer = pg_malloc(len + 1);
|
||||
|
||||
if (fio_read(fd, buffer, len) != len)
|
||||
{
|
||||
if (safe)
|
||||
return NULL;
|
||||
else
|
||||
elog(ERROR, "Could not read file \"%s\": %s\n",
|
||||
from_fullpath, strerror(errno));
|
||||
}
|
||||
|
||||
fio_close(fd);
|
||||
|
||||
/* Zero-terminate the buffer. */
|
||||
buffer[len] = '\0';
|
||||
|
||||
if (filesize)
|
||||
*filesize = len;
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
/*
|
||||
* 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
|
||||
|
Loading…
x
Reference in New Issue
Block a user