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

Skip to content

Commit d0a695c

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 4bcca5f commit d0a695c

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
@@ -359,25 +359,22 @@ pg_fsync(int fd)
359359
* portable, even if it runs ok on the current system.
360360
*
361361
* We assert here that a descriptor for a file was opened with write
362-
* permissions (either O_RDWR or O_WRONLY) and for a directory without
363-
* write permissions (O_RDONLY).
362+
* permissions (i.e., not O_RDONLY) and for a directory without write
363+
* permissions (O_RDONLY). Notice that the assertion check is made even
364+
* if fsync() is disabled.
364365
*
365-
* Ignore any fstat errors and let the follow-up fsync() do its work.
366-
* Doing this sanity check here counts for the case where fsync() is
367-
* disabled.
366+
* If fstat() fails, ignore it and let the follow-up fsync() complain.
368367
*/
369368
if (fstat(fd, &st) == 0)
370369
{
371370
int desc_flags = fcntl(fd, F_GETFL);
372371

373-
/*
374-
* O_RDONLY is historically 0, so just make sure that for directories
375-
* no write flags are used.
376-
*/
372+
desc_flags &= O_ACCMODE;
373+
377374
if (S_ISDIR(st.st_mode))
378-
Assert((desc_flags & (O_RDWR | O_WRONLY)) == 0);
375+
Assert(desc_flags == O_RDONLY);
379376
el 5356 se
380-
Assert((desc_flags & (O_RDWR | O_WRONLY)) != 0);
377+
Assert(desc_flags != O_RDONLY);
381378
}
382379
errno = 0;
383380
#endif

0 commit comments

Comments
 (0)
0