gstdio: Add macOS support to g_fsync()

Apparently, `fsync()` doesn’t actually sync to the spinning disk on
macOS. You need an `fcntl()` for that.

See: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/fsync.2.html

Spotted by Christoph Reiter in a comment on !369.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
This commit is contained in:
Philip Withnall 2020-05-27 18:54:12 +01:00
parent 926dfe5925
commit cd02eac2d4
2 changed files with 17 additions and 2 deletions

View File

@ -1661,7 +1661,7 @@ g_freopen (const gchar *filename,
* @fd: a file descriptor
*
* A wrapper for the POSIX `fsync()` function. On Windows, `_commit()` will be
* used.
* used. On macOS, `fcntl(F_FULLFSYNC)` will be used.
* The `fsync()` function is used to synchronize a file's in-core
* state with that of the disk.
*
@ -1679,10 +1679,14 @@ g_fsync (gint fd)
{
#ifdef G_OS_WIN32
return _commit (fd);
#elif defined(HAVE_FSYNC)
#elif defined(HAVE_FSYNC) || defined(HAVE_FCNTL_F_FULLFSYNC)
int retval;
do
#ifdef HAVE_FCNTL_F_FULLFSYNC
retval = fcntl (fd, F_FULLFSYNC, 0);
#else
retval = fsync (fd);
#endif
while (G_UNLIKELY (retval < 0 && errno == EINTR));
return retval;
#else

View File

@ -875,6 +875,17 @@ if cc.compiles('''#include <fcntl.h>
glib_conf.set('HAVE_OPEN_O_DIRECTORY', 1)
endif
# fcntl takes F_FULLFSYNC as an option
# See https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/fsync.2.html
if cc.compiles('''#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
void some_func (void) {
fcntl(0, F_FULLFSYNC, 0);
}''', name : 'fcntl() option F_FULLFSYNC')
glib_conf.set('HAVE_FCNTL_F_FULLFSYNC', 1)
endif
# Check whether there is a vsnprintf() function with C99 semantics installed.
# (similar tests to AC_FUNC_VSNPRINTF_C99)
# Check whether there is a snprintf() function with C99 semantics installed.