From 04f4e10cfc158239ca00a6ed6a84428c7acb1e6d Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Fri, 10 Dec 2010 17:35:33 -0500
Subject: [PATCH] Use symbolic names not octal constants for file permission
 flags.

Purely cosmetic patch to make our coding standards more consistent ---
we were doing symbolic some places and octal other places.  This patch
fixes all C-coded uses of mkdir, chmod, and umask.  There might be some
other calls I missed.  Inconsistency noted while researching tablespace
directory permissions issue.
---
 src/backend/access/transam/xlog.c   |  2 +-
 src/backend/commands/copy.c         |  2 +-
 src/backend/commands/tablespace.c   |  2 +-
 src/backend/libpq/be-fsstubs.c      |  7 ++++---
 src/backend/postmaster/postmaster.c |  4 ++--
 src/backend/postmaster/syslogger.c  |  6 +++---
 src/backend/storage/file/copydir.c  |  2 +-
 src/backend/storage/ipc/ipc.c       |  4 ++--
 src/bin/initdb/initdb.c             | 16 ++++++++--------
 src/bin/pg_ctl/pg_ctl.c             |  2 +-
 10 files changed, 24 insertions(+), 23 deletions(-)

diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index 5288b7fb3d4..49764581049 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -3451,7 +3451,7 @@ ValidateXLOGDirectoryStructure(void)
 	{
 		ereport(LOG,
 				(errmsg("creating missing WAL directory \"%s\"", path)));
-		if (mkdir(path, 0700) < 0)
+		if (mkdir(path, S_IRWXU) < 0)
 			ereport(FATAL,
 					(errmsg("could not create missing directory \"%s\": %m",
 							path)));
diff --git a/src/backend/commands/copy.c b/src/backend/commands/copy.c
index 40632b0e1cd..7b8bee8f30e 100644
--- a/src/backend/commands/copy.c
+++ b/src/backend/commands/copy.c
@@ -1269,7 +1269,7 @@ DoCopyTo(CopyState cstate)
 					(errcode(ERRCODE_INVALID_NAME),
 					 errmsg("relative path not allowed for COPY to file")));
 
-		oumask = umask((mode_t) 022);
+		oumask = umask(S_IWGRP | S_IWOTH);
 		cstate->copy_file = AllocateFile(cstate->filename, PG_BINARY_W);
 		umask(oumask);
 
diff --git a/src/backend/commands/tablespace.c b/src/backend/commands/tablespace.c
index 5ba0f1ca9da..cd80c811a92 100644
--- a/src/backend/commands/tablespace.c
+++ b/src/backend/commands/tablespace.c
@@ -552,7 +552,7 @@ create_tablespace_directories(const char *location, const Oid tablespaceoid)
 	 * Attempt to coerce target directory to safe permissions.	If this fails,
 	 * it doesn't exist or has the wrong owner.
 	 */
-	if (chmod(location, 0700) != 0)
+	if (chmod(location, S_IRWXU) != 0)
 	{
 		if (errno == ENOENT)
 			ereport(ERROR,
diff --git a/src/backend/libpq/be-fsstubs.c b/src/backend/libpq/be-fsstubs.c
index ac6f56155f2..c74d829e1ac 100644
--- a/src/backend/libpq/be-fsstubs.c
+++ b/src/backend/libpq/be-fsstubs.c
@@ -399,7 +399,7 @@ lo_import_internal(text *filename, Oid lobjOid)
 	 * open the file to be read in
 	 */
 	text_to_cstring_buffer(filename, fnamebuf, sizeof(fnamebuf));
-	fd = PathNameOpenFile(fnamebuf, O_RDONLY | PG_BINARY, 0666);
+	fd = PathNameOpenFile(fnamebuf, O_RDONLY | PG_BINARY, S_IRWXU);
 	if (fd < 0)
 		ereport(ERROR,
 				(errcode_for_file_access(),
@@ -474,8 +474,9 @@ lo_export(PG_FUNCTION_ARGS)
 	 * world-writable export files doesn't seem wise.
 	 */
 	text_to_cstring_buffer(filename, fnamebuf, sizeof(fnamebuf));
-	oumask = umask((mode_t) 0022);
-	fd = PathNameOpenFile(fnamebuf, O_CREAT | O_WRONLY | O_TRUNC | PG_BINARY, 0666);
+	oumask = umask(S_IWGRP | S_IWOTH);
+	fd = PathNameOpenFile(fnamebuf, O_CREAT | O_WRONLY | O_TRUNC | PG_BINARY,
+						  S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
 	umask(oumask);
 	if (fd < 0)
 		ereport(ERROR,
diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index 65278b510dd..90854f44d79 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -494,7 +494,7 @@ PostmasterMain(int argc, char *argv[])
 	/*
 	 * for security, no dir or file created can be group or other accessible
 	 */
-	umask((mode_t) 0077);
+	umask(S_IRWXG | S_IRWXO);
 
 	/*
 	 * Fire up essential subsystems: memory management
@@ -1274,7 +1274,7 @@ pmdaemonize(void)
 					 progname, DEVNULL, strerror(errno));
 		ExitPostmaster(1);
 	}
-	pmlog = open(pmlogname, O_CREAT | O_WRONLY | O_APPEND, 0600);
+	pmlog = open(pmlogname, O_CREAT | O_WRONLY | O_APPEND, S_IRUSR | S_IWUSR);
 	if (pmlog < 0)
 	{
 		write_stderr("%s: could not open log file \"%s/%s\": %s\n",
diff --git a/src/backend/postmaster/syslogger.c b/src/backend/postmaster/syslogger.c
index 00ab343d7de..7541f88cc32 100644
--- a/src/backend/postmaster/syslogger.c
+++ b/src/backend/postmaster/syslogger.c
@@ -73,7 +73,7 @@ int			Log_RotationSize = 10 * 1024;
 char	   *Log_directory = NULL;
 char	   *Log_filename = NULL;
 bool		Log_truncate_on_rotation = false;
-int			Log_file_mode = 0600;
+int			Log_file_mode = S_IRUSR | S_IWUSR;
 
 /*
  * Globally visible state (used by elog.c)
@@ -511,7 +511,7 @@ SysLogger_Start(void)
 	/*
 	 * Create log directory if not present; ignore errors
 	 */
-	mkdir(Log_directory, 0700);
+	mkdir(Log_directory, S_IRWXU);
 
 	/*
 	 * The initial logfile is created right in the postmaster, to verify that
@@ -1020,7 +1020,7 @@ logfile_open(const char *filename, const char *mode, bool allow_errors)
 	 * Note we do not let Log_file_mode disable IWUSR, since we certainly
 	 * want to be able to write the files ourselves.
 	 */
-	oumask = umask((mode_t) ((~(Log_file_mode | S_IWUSR)) & 0777));
+	oumask = umask((mode_t) ((~(Log_file_mode | S_IWUSR)) & (S_IRWXU | S_IRWXG | S_IRWXO)));
 	fh = fopen(filename, mode);
 	umask(oumask);
 
diff --git a/src/backend/storage/file/copydir.c b/src/backend/storage/file/copydir.c
index 4a10563ef83..f7dc509b500 100644
--- a/src/backend/storage/file/copydir.c
+++ b/src/backend/storage/file/copydir.c
@@ -56,7 +56,7 @@ copydir(char *fromdir, char *todir, bool recurse)
 	char		fromfile[MAXPGPATH];
 	char		tofile[MAXPGPATH];
 
-	if (mkdir(todir, S_IRUSR | S_IWUSR | S_IXUSR) != 0)
+	if (mkdir(todir, S_IRWXU) != 0)
 		ereport(ERROR,
 				(errcode_for_file_access(),
 				 errmsg("could not create directory \"%s\": %m", todir)));
diff --git a/src/backend/storage/ipc/ipc.c b/src/backend/storage/ipc/ipc.c
index 9d15d11e636..27b46954e07 100644
--- a/src/backend/storage/ipc/ipc.c
+++ b/src/backend/storage/ipc/ipc.c
@@ -126,8 +126,8 @@ proc_exit(int code)
 		else
 			snprintf(gprofDirName, 32, "gprof/%d", (int) getpid());
 
-		mkdir("gprof", 0777);
-		mkdir(gprofDirName, 0777);
+		mkdir("gprof", S_IRWXU | S_IRWXG | S_IRWXO);
+		mkdir(gprofDirName, S_IRWXU | S_IRWXG | S_IRWXO);
 		chdir(gprofDirName);
 	}
 #endif
diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c
index 71c6324a3be..19033ed54a5 100644
--- a/src/bin/initdb/initdb.c
+++ b/src/bin/initdb/initdb.c
@@ -870,7 +870,7 @@ mkdatadir(const char *subdir)
 	else
 		strcpy(path, pg_data);
 
-	if (mkdir_p(path, 0700) == 0)
+	if (mkdir_p(path, S_IRWXU) == 0)
 		return true;
 
 	fprintf(stderr, _("%s: could not create directory \"%s\": %s\n"),
@@ -1166,7 +1166,7 @@ setup_config(void)
 	snprintf(path, sizeof(path), "%s/postgresql.conf", pg_data);
 
 	writefile(path, conflines);
-	chmod(path, 0600);
+	chmod(path, S_IRUSR | S_IWUSR);
 
 	free(conflines);
 
@@ -1237,7 +1237,7 @@ setup_config(void)
 	snprintf(path, sizeof(path), "%s/pg_hba.conf", pg_data);
 
 	writefile(path, conflines);
-	chmod(path, 0600);
+	chmod(path, S_IRUSR | S_IWUSR);
 
 	free(conflines);
 
@@ -1248,7 +1248,7 @@ setup_config(void)
 	snprintf(path, sizeof(path), "%s/pg_ident.conf", pg_data);
 
 	writefile(path, conflines);
-	chmod(path, 0600);
+	chmod(path, S_IRUSR | S_IWUSR);
 
 	free(conflines);
 
@@ -2904,7 +2904,7 @@ main(int argc, char *argv[])
 
 	printf("\n");
 
-	umask(077);
+	umask(S_IRWXG | S_IRWXO);
 
 	/*
 	 * now we are starting to do real work, trap signals so we can clean up
@@ -2951,7 +2951,7 @@ main(int argc, char *argv[])
 				   pg_data);
 			fflush(stdout);
 
-			if (chmod(pg_data, 0700) != 0)
+			if (chmod(pg_data, S_IRWXU) != 0)
 			{
 				fprintf(stderr, _("%s: could not change permissions of directory \"%s\": %s\n"),
 						progname, pg_data, strerror(errno));
@@ -3004,7 +3004,7 @@ main(int argc, char *argv[])
 					   xlog_dir);
 				fflush(stdout);
 
-				if (mkdir_p(xlog_dir, 0700) != 0)
+				if (mkdir_p(xlog_dir, S_IRWXU) != 0)
 				{
 					fprintf(stderr, _("%s: could not create directory \"%s\": %s\n"),
 							progname, xlog_dir, strerror(errno));
@@ -3021,7 +3021,7 @@ main(int argc, char *argv[])
 					   xlog_dir);
 				fflush(stdout);
 
-				if (chmod(xlog_dir, 0700) != 0)
+				if (chmod(xlog_dir, S_IRWXU) != 0)
 				{
 					fprintf(stderr, _("%s: could not change permissions of directory \"%s\": %s\n"),
 							progname, xlog_dir, strerror(errno));
diff --git a/src/bin/pg_ctl/pg_ctl.c b/src/bin/pg_ctl/pg_ctl.c
index 3cf2afcc27f..c5f855e063f 100644
--- a/src/bin/pg_ctl/pg_ctl.c
+++ b/src/bin/pg_ctl/pg_ctl.c
@@ -1769,7 +1769,7 @@ main(int argc, char **argv)
 	 */
 	argv0 = argv[0];
 
-	umask(077);
+	umask(S_IRWXG | S_IRWXO);
 
 	/* support --help and --version even if invoked as root */
 	if (argc > 1)
-- 
GitLab