Skip to content

Commit 71d71ac

Browse files
committed
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
1 parent 326f2aa commit 71d71ac

File tree

1 file changed

+8
-11
lines changed
  • src/backend/storage/file

1 file changed

+8
-11
lines changed

src/backend/storage/file/fd.c

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -366,25 +366,22 @@ pg_fsync(int fd)
366366
* portable, even if it runs ok on the current system.
367367
*
368368
* We assert here that a descriptor for a file was opened with write
369-
* permissions (either O_RDWR or O_WRONLY) and for a directory without
370-
* write permissions (O_RDONLY).
369+
* permissions (i.e., not O_RDONLY) and for a directory without write
370+
* permissions (O_RDONLY). Notice that the assertion check is made even
371+
* if fsync() is disabled.
371372
*
372-
* Ignore any fstat errors and let the follow-up fsync() do its work.
373-
* Doing this sanity check here counts for the case where fsync() is
374-
* disabled.
373+
* If fstat() fails, ignore it and let the follow-up fsync() complain.
375374
*/
376375
if (fstat(fd, &st) == 0)
377376
{
378377
int desc_flags = fcntl(fd, F_GETFL);
379378

380-
/*
381-
* O_RDONLY is historically 0, so just make sure that for directories
382-
* no write flags are used.
383-
*/
379+
desc_flags &= O_ACCMODE;
380+
384381
if (S_ISDIR(st.st_mode))
385-
Assert((desc_flags & (O_RDWR | O_WRONLY)) == 0);
382+
Assert(desc_flags == O_RDONLY);
386383
else
387-
Assert((desc_flags & (O_RDWR | O_WRONLY)) != 0);
384+
Assert(desc_flags != O_RDONLY);
388385
}
389386
errno = 0;
390387
#endif

0 commit comments

Comments
 (0)