From ada5748d2f13400611532eb78359d536ece2c31a Mon Sep 17 00:00:00 2001 From: Andrew Wood Date: Sun, 6 Aug 2023 20:45:46 +0100 Subject: [PATCH] Possible workaround for OS X oddity with fdatasync() - GH#73. --- doc/NEWS.md | 1 + src/pv/transfer.c | 9 ++++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/doc/NEWS.md b/doc/NEWS.md index 5547b39..73b907a 100644 --- a/doc/NEWS.md +++ b/doc/NEWS.md @@ -3,6 +3,7 @@ * cleanup: added a test for terminal width detection to "`make test`" * cleanup: added a test to "`make test`" to ensure that "`make install`" installs everything expected * cleanup: replaced *AC_HEADER_TIOCGWINSZ* with *AC_CHECK_HEADERS(sys/ioctl.h)* for better MacOS compatibility ([GH#74](https://github.com/a-j-wood/pv/issues/74)) + * cleanup: with "`--sync`", call `fsync()` instead of `fdatasync()` on incapable systems ([GH#73](https://github.com/a-j-wood/pv/issues/73)) 1.7.24 - 30 July 2023 diff --git a/src/pv/transfer.c b/src/pv/transfer.c index 02a9c7c..4c5d09f 100644 --- a/src/pv/transfer.c +++ b/src/pv/transfer.c @@ -96,7 +96,8 @@ static ssize_t pv__transfer_read_repeated(int fd, void *buf, size_t count) * see if we can write any more, and keep trying, to make sure we empty the * buffer as much as we can. * - * If "sync_after_write" is true, we call fdatasync() after each write(). + * If "sync_after_write" is true, we call fdatasync() after each write() (or + * fsync() if _POSIX_SYNCHRONIZED_IO is not > 0). * * We stop retrying if the time elapsed since this function was entered * reaches TRANSFER_WRITE_TIMEOUT microseconds. @@ -127,9 +128,15 @@ static ssize_t pv__transfer_write_repeated(int fd, void *buf, size_t count, bool * descriptor), EINVAL (non syncable fd, such as a * pipe), etc - only return an error on EIO. */ +# if defined(_POSIX_SYNCHRONIZED_IO) && _POSIX_SYNCHRONIZED_IO > 0 if ((fdatasync(fd) < 0) && (EIO == errno)) { return -1; } +# else + if ((fsync(fd) < 0) && (EIO == errno)) { + return -1; + } +# endif } #endif /* HAVE_FDATASYNC */