From a9d42cf1d60a94b5b13e888ff597614255eaa0fe Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Mon, 25 Feb 2013 02:21:54 -0500 Subject: [PATCH 3/3] Call fdatasync() before renaming files into place In order to implement fully atomic upgrades, the OSTree system expects that "triggers" such as this tool ensure that any data they generate are on durable storage. Thus, we use fdatasync() before calling rename(). --- configure.in | 2 ++ update-mime-database.c | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 0 deletions(-) diff --git a/configure.in b/configure.in index 3feb045..9ddbd1a 100644 --- a/configure.in +++ b/configure.in @@ -20,6 +20,8 @@ GETTEXT_PACKAGE=shared-mime-info AC_SUBST(GETTEXT_PACKAGE) AM_GLIB_GNU_GETTEXT +AC_CHECK_FUNCS(fdatasync) + dnl Check for cross compiling AM_CONDITIONAL(CROSS_COMPILING, test $cross_compiling = yes) diff --git a/update-mime-database.c b/update-mime-database.c index 959680e..82a2ef8 100644 --- a/update-mime-database.c +++ b/update-mime-database.c @@ -16,6 +16,7 @@ #include #include #include +#include #define XML_NS XML_XML_NAMESPACE #define XMLNS_NS "http://www.w3.org/2000/xmlns/" @@ -927,6 +928,7 @@ static gboolean atomic_update(const gchar *pathname, GError **error) gboolean ret = FALSE; gchar *new_name = NULL; int len; + int fd; len = strlen(pathname); @@ -934,6 +936,25 @@ static gboolean atomic_update(const gchar *pathname, GError **error) new_name = g_strndup(pathname, len - 4); +#ifdef HAVE_FDATASYNC + fd = open(pathname, O_RDONLY); + if (fd == -1) + { + set_error_from_errno(error); + goto out; + } + if (fdatasync(fd) == -1) + { + set_error_from_errno(error); + goto out; + } + if (close(fd) == -1) + { + set_error_from_errno(error); + goto out; + } +#endif + #ifdef _WIN32 /* we need to remove the old file first! */ remove(new_name); -- 1.7.1