8484#include "access/xlog.h"
8585#include "catalog/catalog.h"
8686#include "catalog/pg_tablespace.h"
87+ #include "common/file_perm.h"
8788#include "pgstat.h"
8889#include "portability/mem.h"
8990#include "storage/fd.h"
124125 */
125126#define FD_MINFREE 10
126127
127- /*
128- * Default mode for created files, unless something else is specified using
129- * the *Perm() function variants.
130- */
131- #define PG_FILE_MODE_DEFAULT (S_IRUSR | S_IWUSR)
132-
133128/*
134129 * A number of platforms allow individual processes to open many more files
135130 * than they can really support when *many* processes do the same thing.
@@ -937,7 +932,7 @@ set_max_safe_fds(void)
937932int
938933BasicOpenFile (const char * fileName , int fileFlags )
939934{
940- return BasicOpenFilePerm (fileName , fileFlags , PG_FILE_MODE_DEFAULT );
935+ return BasicOpenFilePerm (fileName , fileFlags , pg_file_create_mode );
941936}
942937
943938/*
@@ -1356,7 +1351,7 @@ FileInvalidate(File file)
13561351File
13571352PathNameOpenFile (const char * fileName , int fileFlags )
13581353{
1359- return PathNameOpenFilePerm (fileName , fileFlags , PG_FILE_MODE_DEFAULT );
1354+ return PathNameOpenFilePerm (fileName , fileFlags , pg_file_create_mode );
13601355}
13611356
13621357/*
@@ -1434,7 +1429,7 @@ PathNameOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode)
14341429void
14351430PathNameCreateTemporaryDir (const char * basedir , const char * directory )
14361431{
1437- if (mkdir (directory , S_IRWXU ) < 0 )
1432+ if (MakePGDirectory (directory ) < 0 )
14381433 {
14391434 if (errno == EEXIST )
14401435 return ;
@@ -1444,14 +1439,14 @@ PathNameCreateTemporaryDir(const char *basedir, const char *directory)
14441439 * EEXIST to close a race against another process following the same
14451440 * algorithm.
14461441 */
1447- if (mkdir (basedir , S_IRWXU ) < 0 && errno != EEXIST )
1442+ if (MakePGDirectory (basedir ) < 0 && errno != EEXIST )
14481443 ereport (ERROR ,
14491444 (errcode_for_file_access (),
14501445 errmsg ("cannot create temporary directory \"%s\": %m" ,
14511446 basedir )));
14521447
14531448 /* Try again. */
1454- if (mkdir (directory , S_IRWXU ) < 0 && errno != EEXIST )
1449+ if (MakePGDirectory (directory ) < 0 && errno != EEXIST )
14551450 ereport (ERROR ,
14561451 (errcode_for_file_access (),
14571452 errmsg ("cannot create temporary subdirectory \"%s\": %m" ,
@@ -1601,11 +1596,11 @@ OpenTemporaryFileInTablespace(Oid tblspcOid, bool rejectError)
16011596 * We might need to create the tablespace's tempfile directory, if no
16021597 * one has yet done so.
16031598 *
1604- * Don't check for error from mkdir ; it could fail if someone else
1605- * just did the same thing. If it doesn't work then we'll bomb out on
1606- * the second create attempt, instead.
1599+ * Don't check for an error from MakePGDirectory ; it could fail if
1600+ * someone else just did the same thing. If it doesn't work then
1601+ * we'll bomb out on the second create attempt, instead.
16071602 */
1608- mkdir ( tempdirpath , S_IRWXU );
1603+ ( void ) MakePGDirectory ( tempdirpath );
16091604
16101605 file = PathNameOpenFile (tempfilepath ,
16111606 O_RDWR | O_CREAT | O_TRUNC | PG_BINARY );
@@ -2401,7 +2396,7 @@ AllocateFile(const char *name, const char *mode)
24012396int
24022397OpenTransientFile (const char * fileName , int fileFlags )
24032398{
2404- return OpenTransientFilePerm (fileName , fileFlags , PG_FILE_MODE_DEFAULT );
2399+ return OpenTransientFilePerm (fileName , fileFlags , pg_file_create_mode );
24052400}
24062401
24072402/*
@@ -3554,3 +3549,27 @@ fsync_parent_path(const char *fname, int elevel)
35543549
35553550 return 0 ;
35563551}
3552+
3553+ /*
3554+ * Create a PostgreSQL data sub-directory
3555+ *
3556+ * The data directory itself, along with most other directories, are created at
3557+ * initdb-time, but we do have some occations where we create directories from
3558+ * the backend (CREATE TABLESPACE, for example). In those cases, we want to
3559+ * make sure that those directories are created consistently. Today, that means
3560+ * making sure that the created directory has the correct permissions, which is
3561+ * what pg_dir_create_mode tracks for us.
3562+ *
3563+ * Note that we also set the umask() based on what we understand the correct
3564+ * permissions to be (see file_perm.c).
3565+ *
3566+ * For permissions other than the default mkdir() can be used directly, but be
3567+ * sure to consider carefully such cases -- a directory with incorrect
3568+ * permissions in a PostgreSQL data directory could cause backups and other
3569+ * processes to fail.
3570+ */
3571+ int
3572+ MakePGDirectory (const char * directoryName )
3573+ {
3574+ return mkdir (directoryName , pg_dir_create_mode );
3575+ }
0 commit comments