Merge branch 'add_g_fsync_function' into 'master'

Add a wrapper for fsync() function

Closes #35

See merge request GNOME/glib!1104
This commit is contained in:
Nirbheek Chauhan 2019-09-23 11:22:25 +00:00
commit e3b87b6ffb
4 changed files with 31 additions and 0 deletions

View File

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

View File

@ -1641,6 +1641,31 @@ g_freopen (const gchar *filename,
#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:
* @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_fopen fopen
#define g_freopen freopen
#define g_fsync fsync
#define g_utime utime
#endif
@ -158,6 +159,9 @@ FILE *g_freopen (const gchar *filename,
const gchar *mode,
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
* including this header.
*/

View File

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