8000 Make safeguard against incorrect flags for fsync more portable. · postgres/postgres@45c5276 · GitHub
[go: up one dir, main page]

Skip to content

Commit 45c5276

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 07448b3 commit 45c5276

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
@@ -400,25 +400,22 @@ pg_fsync(int fd)
400400
* portable, even if it runs ok on the current system.
401401
*
402402
* We assert here that a descriptor for a file was opened with write
403-
* permissions (either O_RDWR or O_WRONLY) and for a directory without
404-
* write permissions (O_RDONLY).
403+
* permissions (i.e., not O_RDONLY) and for a directory without write
404+
* permissions (O_RDONLY). Notice that the assertion check is made even
405+
* if fsync() is disabled.
405406
*
406-
* Ignore any fstat errors and let the follow-up fsync() do its work.
407-
* Doing this sanity check here counts for the case where fsync() is
408-
* disabled.
407+
* If fstat() fails, ignore it and let the follow-up fsync() complain.
409408
*/
410409
if (fstat(fd, &st) == 0)
411410
{
412411
int desc_flags = fcntl(fd, F_GETFL);
413412

414-
/*
415-
* O_RDONLY is historically 0, so just make sure that for directories
416-
* no write flags are used.
417-
*/
413+
desc_flags &= O_ACCMODE;
414+
418415
if (S_ISDIR(st.st_mode))
419-
Assert((desc_flags & (O_RDWR | O_WRONLY)) == 0);
416+
Assert(desc_flags == O_RDONLY);
420417
else
421-
Assert((desc_flags & (O_RDWR | O_WRONLY)) != 0);
418+
Assert(desc_flags != O_RDONLY);
422419
}
423420
errno = 0;
424421
#endif

0 commit comments

Comments
 (0)
0