Bug 17352: synchronize the file before renaming
authorArtem Bityutskiy <Artem.Bityutskiy@nokia.com>
Fri, 29 Aug 2008 14:13:15 +0000 (17:13 +0300)
committerColin Walters <walters@verbum.org>
Wed, 1 Oct 2008 18:59:51 +0000 (14:59 -0400)
Dbus is doing atomic file updates by copying them, changing
the copy, and re-naming them. However, it does not synchronize
the file before re-naming, which results in corruption in
case of unclean reboots. The reason for this is that file-systems
have write-back cache and they postpone writing data to the media.

This patch adds the missed fsync() for the Unix part. I do
not have windows so cannot provide a windows port fix.

Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>
Signed-off-by: Colin Walters <walters@verbum.org>
dbus/dbus-sysdeps-unix.c

index 1e45649..18b4967 100644 (file)
@@ -2224,6 +2224,15 @@ _dbus_string_save_to_file (const DBusString *str,
       total += bytes_written;
     }
 
+  if (fsync(fd))
+    {
+      dbus_set_error (error, _dbus_error_from_errno (errno),
+                      "Could not synchronize file %s: %s",
+                      tmp_filename_c, _dbus_strerror (errno));
+
+      goto out;
+  }
+
   if (!_dbus_close (fd, NULL))
     {
       dbus_set_error (error, _dbus_error_from_errno (errno),