#include "utils/rel.h" /* Relation */
#include "funcapi.h" /* SRF */
#include "catalog/pg_type.h" /* TEXTOID for tuple_desc */
+#include "storage/fd.h"
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
tupdesc = BlessTupleDesc(tupdesc);
/* Do the main work */
- /*
+ /*
* Open, fstat file
*/
fd = open(filename, O_RDONLY);
}
/*
- * if file ok
+ * if file ok
* then process
*/
if (st.st_size != 0)
*/
if (writeStat == 11)
{
- FILE *f;
+ char path[MAXPGPATH];
+ FILE *file;
int64 count = 0;
- f = fopen(strcat(filename,"_mincore") , "wb");
- count = fwrite(vec, 1, ((st.st_size+pageSize-1)/pageSize) , f);
+ snprintf(path, sizeof(path), "%s_mincore", filename);
+ file = AllocateFile(path, PG_BINARY_W);
+ count = fwrite(vec, 1, ((st.st_size+pageSize-1)/pageSize) , file);
elog(DEBUG1, "writeStat count : %ld",count);
if (count != ((st.st_size+pageSize-1)/pageSize))
ereport(ERROR,
(errcode_for_file_access(),
- errmsg("could not write file \"%s\"_mincore: %m", filename)));
- fclose(f);
+ errmsg("could not write file \"%s\"_mincore: %m", path)));
+ FreeFile(file);
}
/* handle the results */
}
/*
- * apply relevant function
+ * apply relevant function
*/
switch (action)
{
static int
pgfadv_snapshot(char *filename, int fd, int action)
{
- FILE *f;
+ char path[MAXPGPATH];
+ FILE *file;
int blockNum = 0;
unsigned int c;
unsigned int count = 0;
{
case 21 : /* FADVISE_WILLNEED from mincore file */
/* Open _mincore file */
- f = fopen(strcat(filename,"_mincore") , "rb");
+ snprintf(path, sizeof(path), "%s_mincore", filename);
+ file = AllocateFile(path, PG_BINARY_R);
+ if (file == NULL)
+ {
+ if (errno == ENOENT)
+ return; /* ignore not-found error */
+ goto error;
+ }
/* for each bit we read */
- while ((c = fgetc(f)) != EOF)
+ while ((c = fgetc(file)) != EOF)
{
blockNum++;
if (count)
posix_fadvise(fd, ((blockNum-count)*pageSize), count*pageSize, POSIX_FADV_WILLNEED);
- fclose(f);
- elog(DEBUG1, "pgfadv_snapshot: loading %d blocks from relpath %s", gcount, filename);
+ FreeFile(file);
+ elog(DEBUG1, "pgfadv_snapshot: loading %d blocks from relpath %s", gcount, path);
break;
}
return gcount;
+
+error:
+ ereport(LOG,
+ (errcode_for_file_access(),
+ errmsg("could not read mincore file \"%s\": %m",
+ path)));
+ if (file)
+ FreeFile(file);
+ /* If possible, throw away the bogus file; ignore any error */
+ unlink(path);
}