Skip to content

Commit 0fb496c

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 8f45663 commit 0fb496c

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
@@ -373,25 +373,22 @@ pg_fsync(int fd)
373373
* portable, even if it runs ok on the current system.
374374
*
375375
* We assert here that a descriptor for a file was opened with write
376-
* permissions (either O_RDWR or O_WRONLY) and for a directory without
377-
* write permissions (O_RDONLY).
376+
* permissions (i.e., not O_RDONLY) and for a directory without write
377+
* permissions (O_RDONLY). Notice that the assertion check is made even
378+
* if fsync() is disabled.
378379
*
379-
* Ignore any fstat errors and let the follow-up fsync() do its work.
380-
* Doing this sanity check here counts for the case where fsync() is
381-
* disabled.
380+
* If fstat() fails, ignore it and let the follow-up fsync() complain.
382381
*/
383382
if (fstat(fd, &st) == 0)
384383
{
385384
int desc_flags = fcntl(fd, F_GETFL);
386385

387-
/*
388-
* O_RDONLY is historically 0, so just make sure that for directories
389-
* no write flags are used.
390-
*/
386+
desc_flags &= O_ACCMODE;
387+
391388
if (S_ISDIR(st.st_mode))
392-
Assert((desc_flags & (O_RDWR | O_WRONLY)) == 0);
389+
Assert(desc_flags == O_RDONLY);
393390
else
394-
Assert((desc_flags & (O_RDWR | O_WRONLY)) != 0);
391+
Assert(desc_flags != O_RDONLY);
395392
}
396393
errno = 0;
397394
#endif

0 commit comments

Comments
 (0)