Hi, hackers

I found we defined PG_BINARY_R/W/A macros for opening files, however,
there are some places use the constant strings.  IMO we should use
those macros instead of constant strings.  Here is a patch for it.
Any thoughts?

-- 
Regrads,
Japin Li.
ChengDu WenWu Information Technology Co.,Ltd.

>From 1ab1b67edf4e6922af4c65674209b245a8a52369 Mon Sep 17 00:00:00 2001
From: Japin Li <jianping...@ww-it.cn>
Date: Mon, 18 Apr 2022 15:49:47 +0800
Subject: [PATCH] Replace open mode with macros

---
 contrib/adminpack/adminpack.c             | 4 ++--
 contrib/pg_prewarm/autoprewarm.c          | 4 ++--
 src/backend/access/transam/timeline.c     | 4 ++--
 src/backend/access/transam/xlog.c         | 2 +-
 src/backend/access/transam/xlogarchive.c  | 4 ++--
 src/backend/access/transam/xlogfuncs.c    | 2 +-
 src/backend/access/transam/xlogrecovery.c | 4 ++--
 src/backend/commands/extension.c          | 2 +-
 src/backend/libpq/be-secure-openssl.c     | 2 +-
 src/backend/libpq/hba.c                   | 6 +++---
 src/backend/port/sysv_shmem.c             | 2 +-
 src/backend/tsearch/ts_locale.c           | 2 +-
 src/backend/utils/adt/hbafuncs.c          | 4 ++--
 src/backend/utils/adt/misc.c              | 2 +-
 src/backend/utils/init/miscinit.c         | 2 +-
 src/backend/utils/misc/guc-file.l         | 2 +-
 src/backend/utils/misc/guc.c              | 6 +++---
 src/backend/utils/misc/tzparser.c         | 2 +-
 18 files changed, 28 insertions(+), 28 deletions(-)

diff --git a/contrib/adminpack/adminpack.c b/contrib/adminpack/adminpack.c
index 03addf1dc5..ede09e8297 100644
--- a/contrib/adminpack/adminpack.c
+++ b/contrib/adminpack/adminpack.c
@@ -184,10 +184,10 @@ pg_file_write_internal(text *file, text *data, bool replace)
 					(errcode(ERRCODE_DUPLICATE_FILE),
 					 errmsg("file \"%s\" exists", filename)));
 
-		f = AllocateFile(filename, "wb");
+		f = AllocateFile(filename, PG_BINARY_W);
 	}
 	else
-		f = AllocateFile(filename, "ab");
+		f = AllocateFile(filename, PG_BINARY_A);
 
 	if (!f)
 		ereport(ERROR,
diff --git a/contrib/pg_prewarm/autoprewarm.c b/contrib/pg_prewarm/autoprewarm.c
index 45e012a63a..5f6295010e 100644
--- a/contrib/pg_prewarm/autoprewarm.c
+++ b/contrib/pg_prewarm/autoprewarm.c
@@ -299,7 +299,7 @@ apw_load_buffers(void)
 	 * Open the block dump file.  Exit quietly if it doesn't exist, but report
 	 * any other error.
 	 */
-	file = AllocateFile(AUTOPREWARM_FILE, "r");
+	file = AllocateFile(AUTOPREWARM_FILE, PG_BINARY_R);
 	if (!file)
 	{
 		if (errno == ENOENT)
@@ -628,7 +628,7 @@ apw_dump_now(bool is_bgworker, bool dump_unlogged)
 	}
 
 	snprintf(transient_dump_file_path, MAXPGPATH, "%s.tmp", AUTOPREWARM_FILE);
-	file = AllocateFile(transient_dump_file_path, "w");
+	file = AllocateFile(transient_dump_file_path, PG_BINARY_W);
 	if (!file)
 		ereport(ERROR,
 				(errcode_for_file_access(),
diff --git a/src/backend/access/transam/timeline.c b/src/backend/access/transam/timeline.c
index be21968293..db165f2d10 100644
--- a/src/backend/access/transam/timeline.c
+++ b/src/backend/access/transam/timeline.c
@@ -102,7 +102,7 @@ readTimeLineHistory(TimeLineID targetTLI)
 	else
 		TLHistoryFilePath(path, targetTLI);
 
-	fd = AllocateFile(path, "r");
+	fd = AllocateFile(path, PG_BINARY_R);
 	if (fd == NULL)
 	{
 		if (errno != ENOENT)
@@ -237,7 +237,7 @@ existsTimeLineHistory(TimeLineID probeTLI)
 	else
 		TLHistoryFilePath(path, probeTLI);
 
-	fd = AllocateFile(path, "r");
+	fd = AllocateFile(path, PG_BINARY_R);
 	if (fd != NULL)
 	{
 		FreeFile(fd);
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index 5eabd32cf6..dd05ae86e6 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -8616,7 +8616,7 @@ do_pg_backup_stop(char *labelfile, bool waitforarchive, TimeLineID *stoptli_p)
 		XLByteToSeg(startpoint, _logSegNo, wal_segment_size);
 		BackupHistoryFilePath(histfilepath, stoptli, _logSegNo,
 							  startpoint, wal_segment_size);
-		fp = AllocateFile(histfilepath, "w");
+		fp = AllocateFile(histfilepath, PG_BINARY_W);
 		if (!fp)
 			ereport(ERROR,
 					(errcode_for_file_access(),
diff --git a/src/backend/access/transam/xlogarchive.c b/src/backend/access/transam/xlogarchive.c
index a2657a2005..e2699a0b71 100644
--- a/src/backend/access/transam/xlogarchive.c
+++ b/src/backend/access/transam/xlogarchive.c
@@ -478,7 +478,7 @@ XLogArchiveNotify(const char *xlog)
 
 	/* insert an otherwise empty file called <XLOG>.ready */
 	StatusFilePath(archiveStatusPath, xlog, ".ready");
-	fd = AllocateFile(archiveStatusPath, "w");
+	fd = AllocateFile(archiveStatusPath, PG_BINARY_W);
 	if (fd == NULL)
 	{
 		ereport(LOG,
@@ -558,7 +558,7 @@ XLogArchiveForceDone(const char *xlog)
 	}
 
 	/* insert an otherwise empty file called <XLOG>.done */
-	fd = AllocateFile(archiveDone, "w");
+	fd = AllocateFile(archiveDone, PG_BINARY_W);
 	if (fd == NULL)
 	{
 		ereport(LOG,
diff --git a/src/backend/access/transam/xlogfuncs.c b/src/backend/access/transam/xlogfuncs.c
index b61ae6c0b4..39f879a82b 100644
--- a/src/backend/access/transam/xlogfuncs.c
+++ b/src/backend/access/transam/xlogfuncs.c
@@ -587,7 +587,7 @@ pg_promote(PG_FUNCTION_ARGS)
 				 errmsg("\"wait_seconds\" must not be negative or zero")));
 
 	/* create the promote signal file */
-	promote_file = AllocateFile(PROMOTE_SIGNAL_FILE, "w");
+	promote_file = AllocateFile(PROMOTE_SIGNAL_FILE, PG_BINARY_W);
 	if (!promote_file)
 		ereport(ERROR,
 				(errcode_for_file_access(),
diff --git a/src/backend/access/transam/xlogrecovery.c b/src/backend/access/transam/xlogrecovery.c
index 39ef865ed9..b2d902a19a 100644
--- a/src/backend/access/transam/xlogrecovery.c
+++ b/src/backend/access/transam/xlogrecovery.c
@@ -1168,7 +1168,7 @@ read_backup_label(XLogRecPtr *checkPointLoc, TimeLineID *backupLabelTLI,
 	/*
 	 * See if label file is present
 	 */
-	lfp = AllocateFile(BACKUP_LABEL_FILE, "r");
+	lfp = AllocateFile(BACKUP_LABEL_FILE, PG_BINARY_R);
 	if (!lfp)
 	{
 		if (errno != ENOENT)
@@ -1297,7 +1297,7 @@ read_tablespace_map(List **tablespaces)
 	/*
 	 * See if tablespace_map file is present
 	 */
-	lfp = AllocateFile(TABLESPACE_MAP, "r");
+	lfp = AllocateFile(TABLESPACE_MAP, PG_BINARY_R);
 	if (!lfp)
 	{
 		if (errno != ENOENT)
diff --git a/src/backend/commands/extension.c b/src/backend/commands/extension.c
index 1013790dbb..0d6a2e3c29 100644
--- a/src/backend/commands/extension.c
+++ b/src/backend/commands/extension.c
@@ -485,7 +485,7 @@ parse_extension_control_file(ExtensionControlFile *control,
 	else
 		filename = get_extension_control_filename(control->name);
 
-	if ((file = AllocateFile(filename, "r")) == NULL)
+	if ((file = AllocateFile(filename, PG_BINARY_R)) == NULL)
 	{
 		if (errno == ENOENT)
 		{
diff --git a/src/backend/libpq/be-secure-openssl.c b/src/backend/libpq/be-secure-openssl.c
index 3d0168a369..dbf81afa55 100644
--- a/src/backend/libpq/be-secure-openssl.c
+++ b/src/backend/libpq/be-secure-openssl.c
@@ -964,7 +964,7 @@ load_dh_file(char *filename, bool isServerStart)
 	int			codes;
 
 	/* attempt to open file.  It's not an error if it doesn't exist. */
-	if ((fp = AllocateFile(filename, "r")) == NULL)
+	if ((fp = AllocateFile(filename, PG_BINARY_R)) == NULL)
 	{
 		ereport(isServerStart ? FATAL : LOG,
 				(errcode_for_file_access(),
diff --git a/src/backend/libpq/hba.c b/src/backend/libpq/hba.c
index 327a4b42af..2c82ad7ea9 100644
--- a/src/backend/libpq/hba.c
+++ b/src/backend/libpq/hba.c
@@ -371,7 +371,7 @@ tokenize_inc_file(List *tokens,
 		canonicalize_path(inc_fullname);
 	}
 
-	inc_file = AllocateFile(inc_fullname, "r");
+	inc_file = AllocateFile(inc_fullname, PG_BINARY_R);
 	if (inc_file == NULL)
 	{
 		int			save_errno = errno;
@@ -2215,7 +2215,7 @@ load_hba(void)
 	MemoryContext oldcxt;
 	MemoryContext hbacxt;
 
-	file = AllocateFile(HbaFileName, "r");
+	file = AllocateFile(HbaFileName, PG_BINARY_R);
 	if (file == NULL)
 	{
 		ereport(LOG,
@@ -2596,7 +2596,7 @@ load_ident(void)
 	MemoryContext ident_context;
 	IdentLine  *newline;
 
-	file = AllocateFile(IdentFileName, "r");
+	file = AllocateFile(IdentFileName, PG_BINARY_R);
 	if (file == NULL)
 	{
 		/* not fatal ... we just won't do any special ident maps */
diff --git a/src/backend/port/sysv_shmem.c b/src/backend/port/sysv_shmem.c
index ea287c733d..3184d234d6 100644
--- a/src/backend/port/sysv_shmem.c
+++ b/src/backend/port/sysv_shmem.c
@@ -496,7 +496,7 @@ GetHugePageSize(Size *hugepagesize, int *mmap_flags)
 #ifdef __linux__
 
 	{
-		FILE	   *fp = AllocateFile("/proc/meminfo", "r");
+		FILE	   *fp = AllocateFile("/proc/meminfo", PG_BINARY_R);
 		char		buf[128];
 		unsigned int sz;
 		char		ch;
diff --git a/src/backend/tsearch/ts_locale.c b/src/backend/tsearch/ts_locale.c
index e0aa570bf5..9657a60220 100644
--- a/src/backend/tsearch/ts_locale.c
+++ b/src/backend/tsearch/ts_locale.c
@@ -125,7 +125,7 @@ bool
 tsearch_readline_begin(tsearch_readline_state *stp,
 					   const char *filename)
 {
-	if ((stp->fp = AllocateFile(filename, "r")) == NULL)
+	if ((stp->fp = AllocateFile(filename, PG_BINARY_R)) == NULL)
 		return false;
 	stp->filename = filename;
 	stp->lineno = 0;
diff --git a/src/backend/utils/adt/hbafuncs.c b/src/backend/utils/adt/hbafuncs.c
index 9fe7b62c9a..c301910c25 100644
--- a/src/backend/utils/adt/hbafuncs.c
+++ b/src/backend/utils/adt/hbafuncs.c
@@ -369,7 +369,7 @@ fill_hba_view(Tuplestorestate *tuple_store, TupleDesc tupdesc)
 	 * (Most other error conditions should result in a message in a view
 	 * entry.)
 	 */
-	file = AllocateFile(HbaFileName, "r");
+	file = AllocateFile(HbaFileName, PG_BINARY_R);
 	if (file == NULL)
 		ereport(ERROR,
 				(errcode_for_file_access(),
@@ -505,7 +505,7 @@ fill_ident_view(Tuplestorestate *tuple_store, TupleDesc tupdesc)
 	 * (Most other error conditions should result in a message in a view
 	 * entry.)
 	 */
-	file = AllocateFile(IdentFileName, "r");
+	file = AllocateFile(IdentFileName, PG_BINARY_R);
 	if (file == NULL)
 		ereport(ERROR,
 				(errcode_for_file_access(),
diff --git a/src/backend/utils/adt/misc.c b/src/backend/utils/adt/misc.c
index 89690be2ed..989a2db12c 100644
--- a/src/backend/utils/adt/misc.c
+++ b/src/backend/utils/adt/misc.c
@@ -854,7 +854,7 @@ pg_current_logfile(PG_FUNCTION_ARGS)
 					 errhint("The supported log formats are \"stderr\", \"csvlog\", and \"jsonlog\".")));
 	}
 
-	fd = AllocateFile(LOG_METAINFO_DATAFILE, "r");
+	fd = AllocateFile(LOG_METAINFO_DATAFILE, PG_BINARY_R);
 	if (fd == NULL)
 	{
 		if (errno != ENOENT)
diff --git a/src/backend/utils/init/miscinit.c b/src/backend/utils/init/miscinit.c
index 30f0f19dd5..5182478b1e 100644
--- a/src/backend/utils/init/miscinit.c
+++ b/src/backend/utils/init/miscinit.c
@@ -1556,7 +1556,7 @@ ValidatePgVersion(const char *path)
 
 	snprintf(full_path, sizeof(full_path), "%s/PG_VERSION", path);
 
-	file = AllocateFile(full_path, "r");
+	file = AllocateFile(full_path, PG_BINARY_R);
 	if (!file)
 	{
 		if (errno == ENOENT)
diff --git a/src/backend/utils/misc/guc-file.l b/src/backend/utils/misc/guc-file.l
index c70543fa74..25ac17f086 100644
--- a/src/backend/utils/misc/guc-file.l
+++ b/src/backend/utils/misc/guc-file.l
@@ -622,7 +622,7 @@ ParseConfigFile(const char *config_file, bool strict,
 		return false;
 	}
 
-	fp = AllocateFile(abs_path, "r");
+	fp = AllocateFile(abs_path, PG_BINARY_R);
 	if (!fp)
 	{
 		if (strict)
diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c
index 8e9b71375c..393165cacb 100644
--- a/src/backend/utils/misc/guc.c
+++ b/src/backend/utils/misc/guc.c
@@ -8916,7 +8916,7 @@ AlterSystemSetConfigFile(AlterSystemStmt *altersysstmt)
 			/* open old file PG_AUTOCONF_FILENAME */
 			FILE	   *infile;
 
-			infile = AllocateFile(AutoConfFileName, "r");
+			infile = AllocateFile(AutoConfFileName, PG_BINARY_R);
 			if (infile == NULL)
 				ereport(ERROR,
 						(errcode_for_file_access(),
@@ -10647,7 +10647,7 @@ write_nondefault_variables(GucContext context)
 	/*
 	 * Open file
 	 */
-	fp = AllocateFile(CONFIG_EXEC_PARAMS_NEW, "w");
+	fp = AllocateFile(CONFIG_EXEC_PARAMS_NEW, PG_BINARY_W);
 	if (!fp)
 	{
 		ereport(elevel,
@@ -10730,7 +10730,7 @@ read_nondefault_variables(void)
 	/*
 	 * Open file
 	 */
-	fp = AllocateFile(CONFIG_EXEC_PARAMS, "r");
+	fp = AllocateFile(CONFIG_EXEC_PARAMS, PG_BINARY_R);
 	if (!fp)
 	{
 		/* File not found is fine */
diff --git a/src/backend/utils/misc/tzparser.c b/src/backend/utils/misc/tzparser.c
index a69cb2d268..39bc0a687a 100644
--- a/src/backend/utils/misc/tzparser.c
+++ b/src/backend/utils/misc/tzparser.c
@@ -318,7 +318,7 @@ ParseTzFile(const char *filename, int depth,
 	get_share_path(my_exec_path, share_path);
 	snprintf(file_path, sizeof(file_path), "%s/timezonesets/%s",
 			 share_path, filename);
-	tzFile = AllocateFile(file_path, "r");
+	tzFile = AllocateFile(file_path, PG_BINARY_R);
 	if (!tzFile)
 	{
 		/*
-- 
2.17.1

Reply via email to