Commit 
c37b3d08c dropped its added GetDataDirectoryCreatePerm call into
the wrong place in pg_resetwal.c, namely after the chdir to DataDir.
That broke invocations using a relative path, as reported by Tushar Ahuja.
We could have left it where it was and changed the argument to be ".",
but that'd result in a rather confusing error message in event of a
failure, so re-ordering seems like a better solution.
Similarly reorder operations in pg_rewind.c.  The issue there is that
it doesn't seem like a good idea to do any actual operations before the
not-root check (on Unix) or the restricted token acquisition (on Windows).
I don't know that this is an actual bug, but I'm definitely not convinced
that it isn't, either.
Assorted other code review for 
c37b3d08c and 
da9b580d8: fix some
misspelled or otherwise badly worded comments, put the #include for
<sys/stat.h> where it actually belongs, etc.
Discussion: https://postgr.es/m/
aeb9c3a7-3c3f-a57f-1a18-
c8d4fcdc2a1f@enterprisedb.com
 /*
  * Create a PostgreSQL data sub-directory
  *
- * The data directory itself, along with most other directories, are created at
- * initdb-time, but we do have some occations where we create directories from
+ * The data directory itself, and most of its sub-directories, are created at
+ * initdb time, but we do have some occasions when we create directories in
  * the backend (CREATE TABLESPACE, for example).  In those cases, we want to
  * make sure that those directories are created consistently.  Today, that means
  * making sure that the created directory has the correct permissions, which is
  * Note that we also set the umask() based on what we understand the correct
  * permissions to be (see file_perm.c).
  *
- * For permissions other than the default mkdir() can be used directly, but be
- * sure to consider carefully such cases -- a directory with incorrect
+ * For permissions other than the default, mkdir() can be used directly, but
+ * be sure to consider carefully such cases -- a sub-directory with incorrect
  * permissions in a PostgreSQL data directory could cause backups and other
  * processes to fail.
  */
 
  */
 #include "postgres.h"
 
-#include <sys/stat.h>
-
 #include "common/file_perm.h"
 #include "libpq/libpq-be.h"
 #include "libpq/pqcomm.h"
 char      *DataDir = NULL;
 
 /*
- * Mode of the data directory.  The default is 0700 but may it be changed in
+ * Mode of the data directory.  The default is 0700 but it may be changed in
  * checkDataDir() to 0750 if the data directory actually has that mode.
  */
 int            data_directory_mode = PG_DIR_MODE_OWNER;
 
 
    get_restricted_token(progname);
 
-   if (chdir(DataDir) < 0)
-   {
-       fprintf(stderr, _("%s: could not change directory to \"%s\": %s\n"),
-               progname, DataDir, strerror(errno));
-       exit(1);
-   }
-
    /* Set mask based on PGDATA permissions */
    if (!GetDataDirectoryCreatePerm(DataDir))
    {
 
    umask(pg_mode_mask);
 
+   if (chdir(DataDir) < 0)
+   {
+       fprintf(stderr, _("%s: could not change directory to \"%s\": %s\n"),
+               progname, DataDir, strerror(errno));
+       exit(1);
+   }
+
    /* Check that data directory matches our server version */
    CheckDataVersion();
 
 
        exit(1);
    }
 
-   /* Set mask based on PGDATA permissions */
-   if (!GetDataDirectoryCreatePerm(datadir_target))
-   {
-       fprintf(stderr, _("%s: could not read permissions of directory \"%s\": %s\n"),
-               progname, datadir_target, strerror(errno));
-       exit(1);
-   }
-
-   umask(pg_mode_mask);
-
    /*
     * Don't allow pg_rewind to be run as root, to avoid overwriting the
     * ownership of files in the data directory. We need only check for root
 
    get_restricted_token(progname);
 
+   /* Set mask based on PGDATA permissions */
+   if (!GetDataDirectoryCreatePerm(datadir_target))
+   {
+       fprintf(stderr, _("%s: could not read permissions of directory \"%s\": %s\n"),
+               progname, datadir_target, strerror(errno));
+       exit(1);
+   }
+
+   umask(pg_mode_mask);
+
    /* Connect to remote server */
    if (connstr_source)
        libpqConnect(connstr_source);
 
  *
  *-------------------------------------------------------------------------
  */
-#include <sys/stat.h>
-
 #include "c.h"
+
 #include "common/file_perm.h"
 
 /* Modes for creating directories and files in the data directory */
 
 /*-------------------------------------------------------------------------
  *
- * File and directory permission constants
+ * File and directory permission definitions
  *
  *
  * Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
 #ifndef FILE_PERM_H
 #define FILE_PERM_H
 
+#include <sys/stat.h>
+
 /*
  * Mode mask for data directory permissions that only allows the owner to
  * read/write directories and files.