1313#include <sys/stat.h>
1414#include <unistd.h>
1515
16- /*
17- * Read a file into memory.
18- * The file contents are returned in a malloc'd buffer, and *filesize
19- * is set to the length of the file.
20- *
21- * The returned buffer is always zero-terminated; the size of the returned
22- * buffer is actually *filesize + 1. That's handy when reading a text file.
23- * This function can be used to read binary files as well, you can just
24- * ignore the zero-terminator in that case.
25- *
26- */
27- char *
28- slurpFileFullPath (const char * from_fullpath , size_t * filesize , bool safe , fio_location location )
29- {
30- int fd ;
31- char * buffer ;
32- int len ;
33- struct stat statbuf ;
34-
35- if ((fd = fio_open (from_fullpath , O_RDONLY | PG_BINARY , location )) == -1 )
36- {
37- if (safe )
38- return NULL ;
39- else
40- elog (ERROR , "Could not open file \"%s\" for reading: %s" ,
41- from_fullpath , strerror (errno ));
42- }
43-
44- if (fio_fstat (fd , & statbuf ) < 0 )
45- {
46- if (safe )
47- return NULL ;
48- else
49- elog (ERROR , "Could not stat file \"%s\": %s" ,
50- from_fullpath , strerror (errno ));
51- }
52-
53- len = statbuf .st_size ;
54-
55- buffer = pg_malloc (len + 1 );
56-
57- if (fio_read (fd , buffer , len ) != len )
58- {
59- if (safe )
60- return NULL ;
61- else
62- elog (ERROR , "Could not read file \"%s\": %s\n" ,
63- from_fullpath , strerror (errno ));
64- }
65-
66- fio_close (fd );
67-
68- /* Zero-terminate the buffer. */
69- buffer [len ] = '\0' ;
70-
71- if (filesize )
72- * filesize = len ;
73-
74- return buffer ;
75- }
76-
7716/*
7817 * Read a file into memory. The file to be read is <datadir>/<path>.
7918 * 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
9332 struct stat statbuf ;
9433 char fullpath [MAXPGPATH ];
9534 int len ;
96- snprintf (fullpath , sizeof (fullpath ), "%s/%s" , datadir , path );
9735
98- if (fio_access (fullpath , R_OK , location ) != 0 )
99- {
100- if (safe )
101- return NULL ;
102- else
103- elog (ERROR , "could not open file \"%s\" for reading: %s" ,
104- fullpath , strerror (errno ));
105- }
36+ join_path_components (fullpath , datadir , path );
10637
10738 if ((fd = fio_open (fullpath , O_RDONLY | PG_BINARY , location )) == -1 )
10839 {
10940 if (safe )
11041 return NULL ;
11142 else
112- elog (ERROR , "could not open file \"%s\" for reading: %s" ,
43+ elog (ERROR , "Could not open file \"%s\" for reading: %s" ,
11344 fullpath , strerror (errno ));
11445 }
11546
@@ -118,7 +49,7 @@ slurpFile(const char *datadir, const char *path, size_t *filesize, bool safe, fi
11849 if (safe )
11950 return NULL ;
12051 else
121- elog (ERROR , "could not open file \"%s\" for reading : %s" ,
52+ elog (ERROR , "Could not stat file \"%s\": %s" ,
12253 fullpath , strerror (errno ));
12354 }
12455
@@ -130,7 +61,7 @@ slurpFile(const char *datadir, const char *path, size_t *filesize, bool safe, fi
13061 if (safe )
13162 return NULL ;
13263 else
133- elog (ERROR , "could not read file \"%s\": %s\n" ,
64+ elog (ERROR , "Could not read file \"%s\": %s\n" ,
13465 fullpath , strerror (errno ));
13566 }
13667
0 commit comments