1
0
mirror of https://github.com/postgrespro/pg_probackup.git synced 2025-01-08 13:44:32 +02:00

[Issue #169] review: remove slurpFileFullPath

This commit is contained in:
Grigory Smolkin 2020-03-03 15:59:19 +03:00
parent 251e46c2ab
commit 8110715dbf
3 changed files with 5 additions and 77 deletions

View File

@ -13,67 +13,6 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.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>. * 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 * The file contents are returned in a malloc'd buffer, and *filesize
@ -93,23 +32,15 @@ slurpFile(const char *datadir, const char *path, size_t *filesize, bool safe, fi
struct stat statbuf; struct stat statbuf;
char fullpath[MAXPGPATH]; char fullpath[MAXPGPATH];
int len; int len;
snprintf(fullpath, sizeof(fullpath), "%s/%s", datadir, path);
if (fio_access(fullpath, R_OK, location) != 0) join_path_components(fullpath, datadir, path);
{
if (safe)
return NULL;
else
elog(ERROR, "could not open file \"%s\" for reading: %s",
fullpath, strerror(errno));
}
if ((fd = fio_open(fullpath, O_RDONLY | PG_BINARY, location)) == -1) if ((fd = fio_open(fullpath, O_RDONLY | PG_BINARY, location)) == -1)
{ {
if (safe) if (safe)
return NULL; return NULL;
else else
elog(ERROR, "could not open file \"%s\" for reading: %s", elog(ERROR, "Could not open file \"%s\" for reading: %s",
fullpath, strerror(errno)); fullpath, strerror(errno));
} }
@ -118,7 +49,7 @@ slurpFile(const char *datadir, const char *path, size_t *filesize, bool safe, fi
if (safe) if (safe)
return NULL; return NULL;
else else
elog(ERROR, "could not open file \"%s\" for reading: %s", elog(ERROR, "Could not stat file \"%s\": %s",
fullpath, strerror(errno)); fullpath, strerror(errno));
} }
@ -130,7 +61,7 @@ slurpFile(const char *datadir, const char *path, size_t *filesize, bool safe, fi
if (safe) if (safe)
return NULL; return NULL;
else else
elog(ERROR, "could not read file \"%s\": %s\n", elog(ERROR, "Could not read file \"%s\": %s\n",
fullpath, strerror(errno)); fullpath, strerror(errno));
} }

View File

@ -712,9 +712,6 @@ extern char *slurpFile(const char *datadir,
size_t *filesize, size_t *filesize,
bool safe, bool safe,
fio_location location); fio_location location);
extern char *slurpFileFullPath(const char *from_fullpath,
size_t *filesize, bool safe,
fio_location location);
extern char *fetchFile(PGconn *conn, const char *filename, size_t *filesize); extern char *fetchFile(PGconn *conn, const char *filename, size_t *filesize);
/* in help.c */ /* in help.c */

View File

@ -390,7 +390,7 @@ copy_pgcontrol_file(const char *from_fullpath, fio_location from_location,
char *buffer; char *buffer;
size_t size; size_t size;
buffer = slurpFileFullPath(from_fullpath, &size, false, from_location); buffer = slurpFile(from_fullpath, "", &size, false, from_location);
digestControlFile(&ControlFile, buffer, size); digestControlFile(&ControlFile, buffer, size);