Add a wrapper for fsync() function

Closes issue #35
This commit is contained in:
Todd Goyen 2019-09-13 19:53:15 +02:00 committed by Emmanuel Fleury
parent 96c25ceba6
commit 3636bb5fe1
4 changed files with 31 additions and 0 deletions

View File

@ -1626,6 +1626,7 @@ g_remove
g_rmdir g_rmdir
g_fopen g_fopen
g_freopen g_freopen
g_fsync
g_chmod g_chmod
g_access g_access
g_creat g_creat

View File

@ -1641,6 +1641,31 @@ g_freopen (const gchar *filename,
#endif #endif
} }
/**
* g_fsync:
* @fd: a file descriptor
*
* A wrapper for the POSIX fsync() function (_commit() on Windows).
* The fsync() function is used to synchronize a file's in-core
* state with that of the disk.
*
* See the C library manual for more details about fsync().
*
* Returns: 0 on success, or -1 if an error occurred.
* The return value can be used exactly like the return value from fsync().
*
* Since: 2.64
*/
gint
g_fsync (gint fd)
{
#ifdef G_OS_WIN32
return _commit (fd);
#else
return fsync (fd);
#endif
}
/** /**
* g_utime: * g_utime:
* @filename: (type filename): a pathname in the GLib file name encoding * @filename: (type filename): a pathname in the GLib file name encoding

View File

@ -76,6 +76,7 @@ typedef struct stat GStatBuf;
#define g_remove remove #define g_remove remove
#define g_fopen fopen #define g_fopen fopen
#define g_freopen freopen #define g_freopen freopen
#define g_fsync fsync
#define g_utime utime #define g_utime utime
#endif #endif
@ -158,6 +159,9 @@ FILE *g_freopen (const gchar *filename,
const gchar *mode, const gchar *mode,
FILE *stream); FILE *stream);
GLIB_AVAILABLE_IN_2_64
gint g_fsync (gint fd);
struct utimbuf; /* Don't need the real definition of struct utimbuf when just struct utimbuf; /* Don't need the real definition of struct utimbuf when just
* including this header. * including this header.
*/ */

View File

@ -866,6 +866,7 @@ test_set_contents (void)
fd = g_file_open_tmp (NULL, &name, &error); fd = g_file_open_tmp (NULL, &name, &error);
g_assert_no_error (error); g_assert_no_error (error);
write (fd, "a", 1); write (fd, "a", 1);
g_assert_cmpint (g_fsync (fd), ==, 0);
close (fd); close (fd);
ret = g_file_get_contents (name, &buf, &len, &error); ret = g_file_get_contents (name, &buf, &len, &error);