mirror of
https://github.com/postgrespro/pg_probackup.git
synced 2025-01-07 13:40:17 +02:00
[Issue #169] review: remove slurpFileFullPath
This commit is contained in:
parent
251e46c2ab
commit
8110715dbf
77
src/fetch.c
77
src/fetch.c
@ -13,67 +13,6 @@
|
||||
#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
|
||||
@ -93,23 +32,15 @@ slurpFile(const char *datadir, const char *path, size_t *filesize, bool safe, fi
|
||||
struct stat statbuf;
|
||||
char fullpath[MAXPGPATH];
|
||||
int len;
|
||||
snprintf(fullpath, sizeof(fullpath), "%s/%s", datadir, path);
|
||||
|
||||
if (fio_access(fullpath, R_OK, location) != 0)
|
||||
{
|
||||
if (safe)
|
||||
return NULL;
|
||||
else
|
||||
elog(ERROR, "could not open file \"%s\" for reading: %s",
|
||||
fullpath, strerror(errno));
|
||||
}
|
||||
join_path_components(fullpath, datadir, path);
|
||||
|
||||
if ((fd = fio_open(fullpath, O_RDONLY | PG_BINARY, location)) == -1)
|
||||
{
|
||||
if (safe)
|
||||
return NULL;
|
||||
else
|
||||
elog(ERROR, "could not open file \"%s\" for reading: %s",
|
||||
elog(ERROR, "Could not open file \"%s\" for reading: %s",
|
||||
fullpath, strerror(errno));
|
||||
}
|
||||
|
||||
@ -118,7 +49,7 @@ slurpFile(const char *datadir, const char *path, size_t *filesize, bool safe, fi
|
||||
if (safe)
|
||||
return NULL;
|
||||
else
|
||||
elog(ERROR, "could not open file \"%s\" for reading: %s",
|
||||
elog(ERROR, "Could not stat file \"%s\": %s",
|
||||
fullpath, strerror(errno));
|
||||
}
|
||||
|
||||
@ -130,7 +61,7 @@ slurpFile(const char *datadir, const char *path, size_t *filesize, bool safe, fi
|
||||
if (safe)
|
||||
return NULL;
|
||||
else
|
||||
elog(ERROR, "could not read file \"%s\": %s\n",
|
||||
elog(ERROR, "Could not read file \"%s\": %s\n",
|
||||
fullpath, strerror(errno));
|
||||
}
|
||||
|
||||
|
@ -712,9 +712,6 @@ extern char *slurpFile(const char *datadir,
|
||||
size_t *filesize,
|
||||
bool safe,
|
||||
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);
|
||||
|
||||
/* in help.c */
|
||||
|
@ -390,7 +390,7 @@ copy_pgcontrol_file(const char *from_fullpath, fio_location from_location,
|
||||
char *buffer;
|
||||
size_t size;
|
||||
|
||||
buffer = slurpFileFullPath(from_fullpath, &size, false, from_location);
|
||||
buffer = slurpFile(from_fullpath, "", &size, false, from_location);
|
||||
|
||||
digestControlFile(&ControlFile, buffer, size);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user