From fd48920cf82402a95f658cab93db0cf3786c4d6e Mon Sep 17 00:00:00 2001
From: Bastien Nocera <hadess@hadess.net>
Date: Wed, 25 Jun 2014 17:23:50 +0200
Subject: [PATCH 7/8] Split out fdatasync() usage

---
 update-mime-database.c | 38 ++++++++++++++++++++++++--------------
 1 file changed, 24 insertions(+), 14 deletions(-)

diff --git a/update-mime-database.c b/update-mime-database.c
index c043606..c1a6f9f 100644
--- update-mime-database.c
+++ update-mime-database.c
@@ -936,39 +936,49 @@ set_error_from_errno (GError **error)
 			    g_strerror(errsv));
 }
 
-/* Renames pathname by removing the .new extension */
-static gboolean atomic_update(const gchar *pathname, GError **error)
+static int
+sync_file(const gchar *pathname, GError **error)
 {
-	gboolean ret = FALSE;
-	gchar *new_name = NULL;
-	int len;
 	int fd;
 
-	len = strlen(pathname);
-
-	g_return_val_if_fail(strcmp(pathname + len - 4, ".new") == 0, FALSE);
-
-	new_name = g_strndup(pathname, len - 4);
-
 #ifdef HAVE_FDATASYNC
 	fd = open(pathname, O_RDWR);
 	if (fd == -1)
 	{
 		set_error_from_errno(error);
-		goto out;
+		return -1;
 	}
 	if (fdatasync(fd) == -1)
 	{
 		set_error_from_errno(error);
-		goto out;
+		return -1;
 	}
 	if (close(fd) == -1)
 	{
 		set_error_from_errno(error);
-		goto out;
+		return -1;
 	}
 #endif
 
+	return 0;
+}
+
+/* Renames pathname by removing the .new extension */
+static gboolean atomic_update(const gchar *pathname, GError **error)
+{
+	gboolean ret = FALSE;
+	gchar *new_name = NULL;
+	int len;
+
+	len = strlen(pathname);
+
+	g_return_val_if_fail(strcmp(pathname + len - 4, ".new") == 0, FALSE);
+
+	new_name = g_strndup(pathname, len - 4);
+
+	if (sync_file(pathname, error) == -1)
+		goto out;
+
 #ifdef _WIN32
 	/* we need to remove the old file first! */
 	remove(new_name);
-- 
1.9.3

