Fix pg_basebackup output to stdout on Windows.
authorHeikki Linnakangas
Fri, 14 Jul 2017 13:02:53 +0000 (16:02 +0300)
committerHeikki Linnakangas
Fri, 14 Jul 2017 13:03:12 +0000 (16:03 +0300)
When writing a backup to stdout with pg_basebackup on Windows, put stdout
to binary mode. Any CR bytes in the output will otherwise be output
incorrectly as CR+LF.

In the passing, standardize on using "_setmode" instead of "setmode", for
the sake of consistency. They both do the same thing, but according to
MSDN documentation, setmode is deprecated.

Fixes bug #14634, reported by Henry Boehlert. Patch by Haribabu Kommi.
Backpatch to all supported versions.

Discussion: https://www.postgresql.org/message-id/20170428082818[email protected]

src/bin/pg_basebackup/pg_basebackup.c
src/bin/pg_dump/pg_backup_archiver.c

index 60ead91a9da4734138d837c6f47baa1bafac9908..17b8a8bbe12babc8656f56e8297701e22ba45d3c 100644 (file)
@@ -795,6 +795,10 @@ ReceiveTarFile(PGconn *conn, PGresult *res, int rownum)
         */
        if (strcmp(basedir, "-") == 0)
        {
+#ifdef WIN32
+           _setmode(fileno(stdout), _O_BINARY);
+#endif
+
 #ifdef HAVE_LIBZ
            if (compresslevel != 0)
            {
index 78e243c12c0b7db45a8d72d0671e411bc1e0c646..e7aeff0e0966b63b4f3017edc1ee923c676cac6c 100644 (file)
@@ -2324,9 +2324,9 @@ _allocAH(const char *FileSpec, const ArchiveFormat fmt,
        (AH->fSpec == NULL || strcmp(AH->fSpec, "") == 0))
    {
        if (mode == archModeWrite)
-           setmode(fileno(stdout), O_BINARY);
+           _setmode(fileno(stdout), O_BINARY);
        else
-           setmode(fileno(stdin), O_BINARY);
+           _setmode(fileno(stdin), O_BINARY);
    }
 #endif