1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-12 21:01:52 +03:00

Make safeguard against incorrect flags for fsync more portable.

The existing code assumed that O_RDONLY is defined as 0, but this is
not required by POSIX and is not true on GNU Hurd.  We can avoid
the assumption by relying on O_ACCMODE to mask the fcntl() result.
(Hopefully, all supported platforms define that.)

Author: Michael Banck <mbanck@gmx.net>
Co-authored-by: Samuel Thibault
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/6862e8d1.050a0220.194b8d.76fa@mx.google.com
Discussion: https://postgr.es/m/68480868.5d0a0220.1e214d.68a6@mx.google.com
Backpatch-through: 13
This commit is contained in:
Tom Lane
2025-07-01 12:08:20 -04:00
parent 8f456630ce
commit 0fb496c704

View File

@ -373,25 +373,22 @@ pg_fsync(int fd)
* portable, even if it runs ok on the current system. * portable, even if it runs ok on the current system.
* *
* We assert here that a descriptor for a file was opened with write * We assert here that a descriptor for a file was opened with write
* permissions (either O_RDWR or O_WRONLY) and for a directory without * permissions (i.e., not O_RDONLY) and for a directory without write
* write permissions (O_RDONLY). * permissions (O_RDONLY). Notice that the assertion check is made even
* if fsync() is disabled.
* *
* Ignore any fstat errors and let the follow-up fsync() do its work. * If fstat() fails, ignore it and let the follow-up fsync() complain.
* Doing this sanity check here counts for the case where fsync() is
* disabled.
*/ */
if (fstat(fd, &st) == 0) if (fstat(fd, &st) == 0)
{ {
int desc_flags = fcntl(fd, F_GETFL); int desc_flags = fcntl(fd, F_GETFL);
/* desc_flags &= O_ACCMODE;
* O_RDONLY is historically 0, so just make sure that for directories
* no write flags are used.
*/
if (S_ISDIR(st.st_mode)) if (S_ISDIR(st.st_mode))
Assert((desc_flags & (O_RDWR | O_WRONLY)) == 0); Assert(desc_flags == O_RDONLY);
else else
Assert((desc_flags & (O_RDWR | O_WRONLY)) != 0); Assert(desc_flags != O_RDONLY);
} }
errno = 0; errno = 0;
#endif #endif