Allow concurrent-safe open() and fopen() in frontend code for Windows
authorMichael Paquier <michael@paquier.xyz>
Fri, 14 Sep 2018 01:04:14 +0000 (10:04 +0900)
committerMichael Paquier <michael@paquier.xyz>
Fri, 14 Sep 2018 01:04:14 +0000 (10:04 +0900)
PostgreSQL uses a custom wrapper for open() and fopen() which is
concurrent-safe, allowing multiple processes to open and work on the
same file.  This has a couple of advantages:
- pg_test_fsync does not handle O_DSYNC correctly otherwise, leading to
false claims that disks are unsafe.
- TAP tests can run into race conditions when a postmaster and pg_ctl
open postmaster.pid, fixing some random failures in the buildfam.

pg_upgrade is one frontend tool using workarounds to bypass file locking
issues with the log files it generates, however the interactions with
pg_ctl are proving to be tedious to get rid of, so this is left for
later.

Author: Laurenz Albe
Reviewed-by: Michael Paquier, Kuntal Ghosh
Discussion: https://postgr.es/m/1527846213.2475.31.camel@cybertec.at
Discussion: https://postgr.es/m/16922.1520722108@sss.pgh.pa.us

src/bin/initdb/initdb.c
src/bin/pg_basebackup/pg_receivewal.c
src/bin/pg_verify_checksums/pg_verify_checksums.c
src/common/file_utils.c
src/include/port.h

index cb8c7450d9bc1d4c53a9bae390de28b2f27ffb34..b53d6eb9ccbabc27294fdb46f2ba63b7b85b2656 100644 (file)
@@ -491,7 +491,15 @@ readfile(const char *path)
    char       *buffer;
    int         c;
 
+#ifdef WIN32
+   /*
+    * On Windows, we have to open the file in text mode so that carriage
+    * returns are stripped.
+    */
+   if ((infile = fopen(path, "rt")) == NULL)
+#else
    if ((infile = fopen(path, "r")) == NULL)
+#endif
    {
        fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
                progname, path, strerror(errno));
index 8be8d48a8ad36aec13780c518c16b2aaddee2469..912aed8d7cf8db882e24ce6a2e144dd28e7091e3 100644 (file)
@@ -288,7 +288,7 @@ FindStreamingStart(uint32 *tli)
 
            snprintf(fullpath, sizeof(fullpath), "%s/%s", basedir, dirent->d_name);
 
-           fd = open(fullpath, O_RDONLY | PG_BINARY);
+           fd = open(fullpath, O_RDONLY | PG_BINARY, 0);
            if (fd < 0)
            {
                fprintf(stderr, _("%s: could not open compressed file \"%s\": %s\n"),
index 589a3cc58988f2716041efa5519a608196824e4f..1bc020ab6cab1359e78c4f734bc76c240ecadd32 100644 (file)
@@ -80,7 +80,7 @@ scan_file(const char *fn, BlockNumber segmentno)
    int         f;
    BlockNumber blockno;
 
-   f = open(fn, O_RDONLY | PG_BINARY);
+   f = open(fn, O_RDONLY | PG_BINARY, 0);
    if (f < 0)
    {
        fprintf(stderr, _("%s: could not open file \"%s\": %s\n"),
index 48876061c384d926d76629b04edd3f7ca1f9e755..d952bc8c88262ebe9a677db2b4ce455b71528b22 100644 (file)
@@ -222,7 +222,7 @@ pre_sync_fname(const char *fname, bool isdir, const char *progname)
 {
    int         fd;
 
-   fd = open(fname, O_RDONLY | PG_BINARY);
+   fd = open(fname, O_RDONLY | PG_BINARY, 0);
 
    if (fd < 0)
    {
@@ -283,7 +283,7 @@ fsync_fname(const char *fname, bool isdir, const char *progname)
     * unsupported operations, e.g. opening a directory under Windows), and
     * logging others.
     */
-   fd = open(fname, flags);
+   fd = open(fname, flags, 0);
    if (fd < 0)
    {
        if (errno == EACCES || (isdir && errno == EISDIR))
index d92756111f2b873c8a2c4c44cb906a19e88475aa..2522ebc35f8b97da09651bb0e3a024c13507170e 100644 (file)
@@ -249,11 +249,8 @@ extern bool rmtree(const char *path, bool rmtopdir);
 #define        O_DIRECT    0x80000000
 extern int pgwin32_open(const char *, int,...);
 extern FILE *pgwin32_fopen(const char *, const char *);
-
-#ifndef FRONTEND
 #define        open(a,b,c) pgwin32_open(a,b,c)
 #define        fopen(a,b) pgwin32_fopen(a,b)
-#endif
 
 /*
  * Mingw-w64 headers #define popen and pclose to _popen and _pclose.  We want