Replace open mode with PG_BINARY_R/W/A macros
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.
Attachments:
0001-Replace-open-mode-with-macros.patchtext/x-patchDownload
From 1ab1b67edf4e6922af4c65674209b245a8a52369 Mon Sep 17 00:00:00 2001
From: Japin Li <jianping.li@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
Japin Li <japinli@hotmail.com> writes:
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?
A lot of these changes look wrong to me: they are substituting "rb" for
"r", etc, in places that mean to read text files. You have to think
about the Windows semantics.
If you think any of those changes are correct, then they are bug fixes
that need to be considered separately from cosmetic tidying.
regards, tom lane
On Mon, 18 Apr 2022 at 22:41, Tom Lane <tgl@sss.pgh.pa.us> wrote:
Japin Li <japinli@hotmail.com> writes:
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?A lot of these changes look wrong to me: they are substituting "rb" for
"r", etc, in places that mean to read text files. You have to think
about the Windows semantics.
I do this substituting, since the comment says it can be used for opening
text files. Maybe I misunderstand the comment.
/*
* NOTE: this is also used for opening text files.
* WIN32 treats Control-Z as EOF in files opened in text mode.
* Therefore, we open files in binary mode on Win32 so we can read
* literal control-Z. The other affect is that we see CRLF, but
* that is OK because we can already handle those cleanly.
*/
#if defined(WIN32) || defined(__CYGWIN__)
#define PG_BINARY O_BINARY
#define PG_BINARY_A "ab"
#define PG_BINARY_R "rb"
#define PG_BINARY_W "wb"
#else
#define PG_BINARY 0
#define PG_BINARY_A "a"
#define PG_BINARY_R "r"
#define PG_BINARY_W "w"
#endif
--
Regrads,
Japin Li.
ChengDu WenWu Information Technology Co.,Ltd.
On Tue, Apr 19, 2022 at 01:29:18PM +0800, Japin Li wrote:
On Mon, 18 Apr 2022 at 22:41, Tom Lane <tgl@sss.pgh.pa.us> wrote:
Japin Li <japinli@hotmail.com> writes:
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?A lot of these changes look wrong to me: they are substituting "rb" for
"r", etc, in places that mean to read text files. You have to think
about the Windows semantics.
This reminded me of the business from a couple of years ago in
pgwin32_open() to enforce the text mode in the frontend if O_BINARY is
not specified.
I do this substituting, since the comment says it can be used for opening
text files. Maybe I misunderstand the comment.
'b' is normally ignored on POSIX platforms (per the Linux man page for
fopen), but your patch has as effect to silently switch to binary mode
on Windows all those code paths. See _setmode() in pgwin32_open(),
that changes the behavior of CRLF when reading or writing such files,
as described here:
https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/setmode?view=msvc-170
The change in adminpack.c would be actually as 'b' should be ignored
on non-WIN32, but Tom's point is to not take lightly all the others.
--
Michael
Japin Li <japinli@hotmail.com> writes:
On Mon, 18 Apr 2022 at 22:41, Tom Lane <tgl@sss.pgh.pa.us> wrote:
A lot of these changes look wrong to me: they are substituting "rb" for
"r", etc, in places that mean to read text files. You have to think
about the Windows semantics.
I do this substituting, since the comment says it can be used for opening
text files. Maybe I misunderstand the comment.
I think the comment's at best misleading. See e.g. 66f8687a8.
It might be okay to use "rb" to read a text file when there
is actually \r-stripping logic present, but you need to check
that. Using "wb" to write a text file is flat wrong.
regards, tom lane
On Tue, 19 Apr 2022 at 14:14, Michael Paquier <michael@paquier.xyz> wrote:
On Tue, Apr 19, 2022 at 01:29:18PM +0800, Japin Li wrote:
On Mon, 18 Apr 2022 at 22:41, Tom Lane <tgl@sss.pgh.pa.us> wrote:
Japin Li <japinli@hotmail.com> writes:
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?A lot of these changes look wrong to me: they are substituting "rb" for
"r", etc, in places that mean to read text files. You have to think
about the Windows semantics.This reminded me of the business from a couple of years ago in
pgwin32_open() to enforce the text mode in the frontend if O_BINARY is
not specified.I do this substituting, since the comment says it can be used for opening
text files. Maybe I misunderstand the comment.'b' is normally ignored on POSIX platforms (per the Linux man page for
fopen), but your patch has as effect to silently switch to binary mode
on Windows all those code paths. See _setmode() in pgwin32_open(),
that changes the behavior of CRLF when reading or writing such files,
as described here:
https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/setmode?view=msvc-170The change in adminpack.c would be actually as 'b' should be ignored
on non-WIN32, but Tom's point is to not take lightly all the others.
Oh, I understand your points. Thanks for the explanation.
--
Regrads,
Japin Li.
ChengDu WenWu Information Technology Co.,Ltd.
On Tue, 19 Apr 2022 at 14:20, Tom Lane <tgl@sss.pgh.pa.us> wrote:
Japin Li <japinli@hotmail.com> writes:
On Mon, 18 Apr 2022 at 22:41, Tom Lane <tgl@sss.pgh.pa.us> wrote:
A lot of these changes look wrong to me: they are substituting "rb" for
"r", etc, in places that mean to read text files. You have to think
about the Windows semantics.I do this substituting, since the comment says it can be used for opening
text files. Maybe I misunderstand the comment.I think the comment's at best misleading. See e.g. 66f8687a8.
It might be okay to use "rb" to read a text file when there
is actually \r-stripping logic present, but you need to check
that. Using "wb" to write a text file is flat wrong.
Thanks for the detail explanation. Should we remove the misleading comment?
--
Regrads,
Japin Li.
ChengDu WenWu Information Technology Co.,Ltd.
Japin Li <japinli@hotmail.com> writes:
On Tue, 19 Apr 2022 at 14:20, Tom Lane <tgl@sss.pgh.pa.us> wrote:
I think the comment's at best misleading. See e.g. 66f8687a8.
It might be okay to use "rb" to read a text file when there
is actually \r-stripping logic present, but you need to check
that. Using "wb" to write a text file is flat wrong.
Thanks for the detail explanation. Should we remove the misleading comment?
We should rewrite it, not just remove it. But I'm not 100% sure
what to say instead. I wonder whether the comment's claims about
control-Z processing still apply on modern Windows.
Another question is whether we actually like the current shape of
the code. I can see at least two different directions we might
prefer to the status quo:
* Invent '#define PG_TEXT_R "r"' and so on, and use those in the
calls that currently use plain "r" etc, establishing a project
policy that you should use one of these six macros and never the
underlying strings directly. This perhaps has some advantages
in greppability and clarity of intent, but I can't help wondering
if it's mostly obsessive-compulsiveness.
* In the other direction, decide that the PG_BINARY_X macros are
offering no benefit at all and just rip 'em out, writing "rb" and
so on in their place. POSIX specifies that the character "b" has
no effect on Unix-oid systems, and it has said that for thirty years
now, so we do not really need the platform dependency that presently
exists in the macro definitions. The presence or absence of "b"
would serve fine as an indicator of intent, and there would be one
less PG-specific coding convention to remember.
Or maybe it's fine as-is. Any sort of wide-ranging change like this
creates hazards for back-patching, so we shouldn't do it lightly.
regards, tom lane
On Tue, 19 Apr 2022 at 22:21, Tom Lane <tgl@sss.pgh.pa.us> wrote:
Japin Li <japinli@hotmail.com> writes:
On Tue, 19 Apr 2022 at 14:20, Tom Lane <tgl@sss.pgh.pa.us> wrote:
I think the comment's at best misleading. See e.g. 66f8687a8.
It might be okay to use "rb" to read a text file when there
is actually \r-stripping logic present, but you need to check
that. Using "wb" to write a text file is flat wrong.Thanks for the detail explanation. Should we remove the misleading comment?
We should rewrite it, not just remove it. But I'm not 100% sure
what to say instead. I wonder whether the comment's claims about
control-Z processing still apply on modern Windows.
It might be true [1]https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/fopen-wfopen?view=msvc-170.
[1]: https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/fopen-wfopen?view=msvc-170
Another question is whether we actually like the current shape of
the code. I can see at least two different directions we might
prefer to the status quo:* Invent '#define PG_TEXT_R "r"' and so on, and use those in the
calls that currently use plain "r" etc, establishing a project
policy that you should use one of these six macros and never the
underlying strings directly. This perhaps has some advantages
in greppability and clarity of intent, but I can't help wondering
if it's mostly obsessive-compulsiveness.* In the other direction, decide that the PG_BINARY_X macros are
offering no benefit at all and just rip 'em out, writing "rb" and
so on in their place. POSIX specifies that the character "b" has
no effect on Unix-oid systems, and it has said that for thirty years
now, so we do not really need the platform dependency that presently
exists in the macro definitions. The presence or absence of "b"
would serve fine as an indicator of intent, and there would be one
less PG-specific coding convention to remember.
I'm incline the second direction if we need to change this.
Or maybe it's fine as-is. Any sort of wide-ranging change like this
creates hazards for back-patching, so we shouldn't do it lightly.
Agreed. Thanks again for the explanation.
--
Regrads,
Japin Li.
ChengDu WenWu Information Technology Co.,Ltd.
On 19.04.22 16:21, Tom Lane wrote:
* In the other direction, decide that the PG_BINARY_X macros are
offering no benefit at all and just rip 'em out, writing "rb" and
so on in their place. POSIX specifies that the character "b" has
no effect on Unix-oid systems, and it has said that for thirty years
now, so we do not really need the platform dependency that presently
exists in the macro definitions. The presence or absence of "b"
would serve fine as an indicator of intent, and there would be one
less PG-specific coding convention to remember.
I can only imagine that there must have been some Unix systems that did
not understand the "binary" APIs required for Windows. (For example,
neither the Linux nor the macOS open(2) man page mentions O_BINARY.)
Otherwise, these macros don't make any sense, because then you could
just write the thing directly on all platforms.
Peter Eisentraut <peter.eisentraut@enterprisedb.com> writes:
On 19.04.22 16:21, Tom Lane wrote:
* In the other direction, decide that the PG_BINARY_X macros are
offering no benefit at all and just rip 'em out, writing "rb" and
so on in their place. POSIX specifies that the character "b" has
no effect on Unix-oid systems, and it has said that for thirty years
now, so we do not really need the platform dependency that presently
exists in the macro definitions. The presence or absence of "b"
would serve fine as an indicator of intent, and there would be one
less PG-specific coding convention to remember.
I can only imagine that there must have been some Unix systems that did
not understand the "binary" APIs required for Windows. (For example,
neither the Linux nor the macOS open(2) man page mentions O_BINARY.)
Otherwise, these macros don't make any sense, because then you could
just write the thing directly on all platforms.
PG_BINARY is useful for open(). It's the PG_BINARY_R/W/A macros for
fopen() that are redundant per POSIX. Possibly someone generalized
inappropriately; or maybe long ago we supported some platform that
rejected the "b" option?
regards, tom lane
On 20.04.22 22:29, Tom Lane wrote:
PG_BINARY is useful for open(). It's the PG_BINARY_R/W/A macros for
fopen() that are redundant per POSIX. Possibly someone generalized
inappropriately; or maybe long ago we supported some platform that
rejected the "b" option?
I think the latter was the case. I doubt it's still a problem.
I see some of the new code in pg_basebackup uses "wb" directly. It
would probably be good to fix that to be consistent one way or the
other. I vote for getting rid of the macros.
Peter Eisentraut <peter.eisentraut@enterprisedb.com> writes:
On 20.04.22 22:29, Tom Lane wrote:
PG_BINARY is useful for open(). It's the PG_BINARY_R/W/A macros for
fopen() that are redundant per POSIX. Possibly someone generalized
inappropriately; or maybe long ago we supported some platform that
rejected the "b" option?
I think the latter was the case. I doubt it's still a problem.
We could find that out with little effort, at least for machines in the
buildfarm, by modifying c.h to use the form with "b" always.
I see some of the new code in pg_basebackup uses "wb" directly. It
would probably be good to fix that to be consistent one way or the
other. I vote for getting rid of the macros.
Yeah, I suspect there have been other inconsistencies for years :-(
regards, tom lane
On Fri, 22 Apr 2022 at 04:38, Tom Lane <tgl@sss.pgh.pa.us> wrote:
Peter Eisentraut <peter.eisentraut@enterprisedb.com> writes:
On 20.04.22 22:29, Tom Lane wrote:
PG_BINARY is useful for open(). It's the PG_BINARY_R/W/A macros for
fopen() that are redundant per POSIX. Possibly someone generalized
inappropriately; or maybe long ago we supported some platform that
rejected the "b" option?I think the latter was the case. I doubt it's still a problem.
We could find that out with little effort, at least for machines in the
buildfarm, by modifying c.h to use the form with "b" always.
I think we should also consider the popen() (see: OpenPipeStream() function),
on the Windows, it can use "b", however, for linux, it might be not right.
So, modifying c.h to use the form with "b" isn't always right.
[1]: https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/popen-wpopen?view=msvc-170
--
Regrads,
Japin Li.
ChengDu WenWu Information Technology Co.,Ltd.
Japin Li <japinli@hotmail.com> writes:
I think we should also consider the popen() (see: OpenPipeStream() function),
on the Windows, it can use "b", however, for linux, it might be not right.
Oh, ugh ... POSIX says for popen():
The behavior of popen() is specified for values of mode of r and
w. Other modes such as rb and wb might be supported by specific
implementations, but these would not be portable features. Note
that historical implementations of popen() only check to see if
the first character of mode is r. Thus, a mode of robert the robot
would be treated as mode r, and a mode of anything else would be
treated as mode w.
Maybe it's best to leave well enough alone here.
regards, tom lane