Use AllocateFile
authorCΓ©dric Villemain <cedric@2ndquadrant.fr>
Fri, 26 Oct 2012 09:37:12 +0000 (11:37 +0200)
committerCΓ©dric Villemain <cedric@2ndquadrant.fr>
Fri, 26 Oct 2012 09:41:39 +0000 (11:41 +0200)
So that we follow the PostgreSQL policy.

ChangeLog
README.rst
pgfincore.c

index df67632c4e1dd9aa2d14834a6dcc3bcc815140e2..285b5be930abc4cbb100e136faec3979e713216a 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,4 +1,7 @@
-09//2011 CΓ©dric Villemain <cedric@2ndQuadrant.fr>
+06/21/2012 CΓ©dric Villemain <cedric@2ndQuadrant.fr>
+  * 1.1.2 - Change the open() call to use AllocateFile, and FreeFile
+
+12/06/2011 CΓ©dric Villemain <cedric@2ndQuadrant.fr>
   * 1.1.1 - Fix Makefile again, as well as debian scripts (VPATH)
                  - Add checks (make installcheck)
                  - Improve .gitignore
index ca7bcedc162cc08424a858b8ca76f685e420f514..ed4a95213f40d968cfaf2d176202ca1383ba5571 100644 (file)
@@ -371,6 +371,8 @@ LIMITATIONS
 
  * PgFincore needs PostgreSQL >= 8.3
 
+ * PgFincore does not work on windows.
+
 SEE ALSO
 ========
 
index 4e074cdce82698678e47acad20d7cfa270e823f7..1dd1983a6e529367d9b3fd1bf3ff918bdd656473 100644 (file)
@@ -233,15 +233,17 @@ pgsysconf(PG_FUNCTION_ARGS)
  * pgfadvise_file
  */
 static int
-pgfadvise_file(char *filename, int advice, pgfadviseStruct     *pgfdv)
+pgfadvise_file(char *filename, int advice, pgfadviseStruct *pgfdv)
 {
        /*
-        * We work directly with the file
-        * we don't use the postgresql file handler
+        * We use the AllocateFile(2) provided by PostgreSQL.  We're going to
+        * close it ourselves even if PostgreSQL close it anyway at transaction
+        * end.
         */
-       struct stat     st;
-       int                     fd;
-       int                     adviceFlag;
+       FILE    *fp;
+       int     fd;
+       struct stat st;
+       int         adviceFlag;
 
        /*
         * OS Page size and Free pages
@@ -249,16 +251,18 @@ pgfadvise_file(char *filename, int advice, pgfadviseStruct        *pgfdv)
        pgfdv->pageSize = sysconf(_SC_PAGESIZE);
 
        /*
-        * Open and fstat file
+        * Fopen and fstat file
         * fd will be provided to posix_fadvise
         * if there is no file, just return 1, it is expected to leave the SRF
         */
-       fd = open(filename, O_RDONLY);
-       if (fd == -1)
-               return 1;
+       fp = AllocateFile(filename, "rb");
+       if (fp == NULL)
+                return 1;
+
+       fd = fileno(fp);
        if (fstat(fd, &st) == -1)
        {
-               close(fd);
+               FreeFile(fp);
                elog(ERROR, "pgfadvise: Can not stat object file : %s", filename);
                return 2;
        }
@@ -317,7 +321,7 @@ pgfadvise_file(char *filename, int advice, pgfadviseStruct  *pgfdv)
        posix_fadvise(fd, 0, 0, adviceFlag);
 
        /* close the file */
-       close(fd);
+       FreeFile(fp);
 
        /*
         * OS things : Pages free
@@ -496,11 +500,13 @@ pgfadvise_loader_file(char *filename,
        int             i, k;
 
        /*
-        * We work directly with the file
-        * we don't use the postgresql file handler
+        * We use the AllocateFile(2) provided by PostgreSQL.  We're going to
+        * close it ourselves even if PostgreSQL close it anyway at transaction
+        * end.
         */
-       struct stat     st;
-       int                     fd;
+       FILE    *fp;
+       int     fd;
+       struct stat st;
 
        /*
         * OS things : Page size
@@ -516,16 +522,18 @@ pgfadvise_loader_file(char *filename,
        pgfloader->pagesUnloaded        = 0;
 
        /*
-        * Open and fstat file
+        * Fopen and fstat file
         * fd will be provided to posix_fadvise
         * if there is no file, just return 1, it is expected to leave the SRF
         */
-       fd = open(filename, O_RDONLY);
-       if (fd == -1)
-               return 1;
+       fp = AllocateFile(filename, "rb");
+       if (fp == NULL)
+                return 1;
+
+       fd = fileno(fp);
        if (fstat(fd, &st) == -1)
        {
-               close(fd);
+               FreeFile(fp);
                elog(ERROR, "pgfadvise_loader: Can not stat object file: %s", filename);
                return 2;
        }
@@ -594,7 +602,7 @@ pgfadvise_loader_file(char *filename,
                        x <<= 1;
                }
        }
-       close(fd);
+       FreeFile(fp);
 
        /*
         * OS things : Pages free
@@ -724,12 +732,16 @@ pgfincore_file(char *filename, pgfincoreStruct *pgfncr)
        bits8   x = 0;
        register int64 pageIndex;
 
+
        /*
-        * We work directly with the file
-        * we don't use the postgresql file handler
+        * We use the AllocateFile(2) provided by PostgreSQL.  We're going to
+        * close it ourselves even if PostgreSQL close it anyway at transaction
+        * end.
         */
-       struct stat       st;
-       int                       fd;
+       FILE    *fp;
+       int     fd;
+       struct stat st;
+
        void              *pa  = (char *) 0;
        unsigned char *vec = (unsigned char *) 0;
 
@@ -746,15 +758,19 @@ pgfincore_file(char *filename, pgfincoreStruct *pgfncr)
        pgfncr->rel_os_pages    = 0;
 
        /*
-       * Open, fstat file
-       */
-       fd = open(filename, O_RDONLY);
-       if (fd == -1)
-               return 1;
+        * Fopen and fstat file
+        * fd will be provided to posix_fadvise
+        * if there is no file, just return 1, it is expected to leave the SRF
+        */
+       fp = AllocateFile(filename, "rb");
+       if (fp == NULL)
+                return 1;
+
+       fd = fileno(fp);
 
        if (fstat(fd, &st) == -1)
        {
-               close(fd);
+               FreeFile(fp);
                elog(ERROR, "Can not stat object file : %s",
                     filename);
                return 2;
@@ -773,7 +789,7 @@ pgfincore_file(char *filename, pgfincoreStruct *pgfncr)
                pa = mmap(NULL, st.st_size, PROT_NONE, MAP_SHARED, fd, 0);
                if (pa == MAP_FAILED)
                {
-                       close(fd);
+                       FreeFile(fp);
                        elog(ERROR, "Can not mmap object file : %s, errno = %i,%s\nThis error can happen if there is not enought space in memory to do the projection. Please mail cedric@villemain.org with '[pgfincore] ENOMEM' as subject.",
                             filename, errno, strerror(errno));
                        return 3;
@@ -784,7 +800,7 @@ pgfincore_file(char *filename, pgfincoreStruct *pgfncr)
                if ((void *)0 == vec)
                {
                        munmap(pa, st.st_size);
-                       close(fd);
+                       FreeFile(fp);
                        elog(ERROR, "Can not calloc object file : %s",
                             filename);
                        return 4;
@@ -795,7 +811,7 @@ pgfincore_file(char *filename, pgfincoreStruct *pgfncr)
                {
                        free(vec);
                        munmap(pa, st.st_size);
-                       close(fd);
+                       FreeFile(fp);
                        elog(ERROR, "mincore(%p, %lld, %p): %s\n",
                             pa, (long long int)st.st_size, vec, strerror(errno));
                        return 5;
@@ -852,7 +868,7 @@ pgfincore_file(char *filename, pgfincoreStruct *pgfncr)
         */
        free(vec);
        munmap(pa, st.st_size);
-       close(fd);
+       FreeFile(fp);
 
        /*
         * OS things : Pages free