hal: Branch 'master' - 2 commits

Martin Pitt martin at kemper.freedesktop.org
Tue Jul 13 01:04:45 PDT 2010


 0 files changed

New commits:
commit e2c6b59d5160f632bd6976c40a90af5c8358c153
Merge: f4e911a... 83ef2fc...
Author: Martin Pitt <martin.pitt at ubuntu.com>
Date:   Tue Jul 13 10:04:20 2010 +0200

    Merge branch 'master' of ssh://git.freedesktop.org/git/hal

commit f4e911a3b752b92e826e941b31b2e2bd104edd7a
Author: Martin Pitt <martin.pitt at ubuntu.com>
Date:   Tue Jul 13 09:58:06 2010 +0200

    fsync /media/.hal-mtab after changes
    
    Some file systems (reported on ubifs) are rather lazy with writing changes to
    disk. An automount, followed by a power failure, then results in a stale
    /media/.hal-mtab and also a stale mount point directory.
    
    Use fsync() to make hal-mtab changes much more reliable.
    
    Thanks to Fujii Takafumi for debugging this!

diff --git a/tools/hal-storage-mount.c b/tools/hal-storage-mount.c
index 9471706..c543707 100644
--- a/tools/hal-storage-mount.c
+++ b/tools/hal-storage-mount.c
@@ -953,6 +953,13 @@ handle_mount (LibHalContext *hal_ctx,
 		if (fwrite (hal_mtab_buf, 1, strlen (hal_mtab_buf), hal_mtab) != strlen (hal_mtab_buf)) {
 			unknown_error ("Cannot write to /media/.hal-mtab~");
 		}
+		if (fsync (fileno (hal_mtab)) < 0) {
+			printf ("WARNING! syncing /media/.hal-mtab~ failed: %s\n", strerror (errno));
+		}
+#ifdef DEBUG
+		else
+			printf ("fsync /media/.hal-mtab~: success\n");
+#endif
 		fclose (hal_mtab);
 		g_free (hal_mtab_buf);
 #ifdef DEBUG
@@ -1012,6 +1019,8 @@ handle_mount (LibHalContext *hal_ctx,
 #endif
 			unknown_error ("Cannot rename /media/.hal-mtab~ to /media/.hal-mtab");
 		}
+
+		fsync_dir("/media");
 #ifdef DEBUG
 		printf ("%d: XYA done renaming /media/.hal-mtab~ to /media/.hal-mtab\n", getpid ());
 #endif
diff --git a/tools/hal-storage-shared.c b/tools/hal-storage-shared.c
index 422f00e..e7e2825 100644
--- a/tools/hal-storage-shared.c
+++ b/tools/hal-storage-shared.c
@@ -47,6 +47,7 @@
 #endif
 #include <sys/types.h>
 #include <unistd.h>
+#include <dirent.h>
 #include <sys/file.h>
 #include <errno.h>
 #include <syslog.h>
@@ -195,6 +196,28 @@ fstab_close (gpointer handle)
 #endif
 }
 
+/* fsync() a directory */
+void
+fsync_dir (char *path)
+{
+	DIR* d;
+
+	d = opendir (path);
+	if (d == NULL) {
+		printf ("fsync_dir (%s): failed to opendir(): %s\n", path, strerror (errno));
+		return;
+	}
+
+	if (fsync (dirfd (d)) < 0)
+		printf ("fsync_dir (%s): failed to fsync(): %s\n", path, strerror (errno));
+
+	closedir (d);
+
+#ifdef DEBUG
+	printf ("fsync_dir (%s): success\n", path);
+#endif
+}
+
 #ifdef __FreeBSD__
 #define UMOUNT		"/sbin/umount"
 #elif sun
@@ -460,6 +483,14 @@ line_found:
 		}
 
 	}
+       if (fsync (fileno (hal_mtab_new)) < 0) {
+	       printf ("WARNING! syncing /media/.hal-mtab~ failed: %s\n", strerror (errno));
+       }
+#ifdef DEBUG
+       else
+	       printf ("fsync /media/.hal-mtab~: success\n");
+#endif
+
 	fclose (hal_mtab_new);
 
 	g_strfreev (lines);
@@ -521,6 +552,7 @@ line_found:
 		unlink ("/media/.hal-mtab~");
 		unknown_error ("Cannot rename /media/.hal-mtab~ to /media/.hal-mtab");
 	}
+	fsync_dir("/media");
 
 #ifdef DEBUG
 	printf ("done unmounting\n");
diff --git a/tools/hal-storage-shared.h b/tools/hal-storage-shared.h
index dcf7a0b..fc7d63e 100644
--- a/tools/hal-storage-shared.h
+++ b/tools/hal-storage-shared.h
@@ -41,6 +41,8 @@ void fstab_close (gpointer handle);
 gboolean lock_hal_mtab (void);
 void unlock_hal_mtab (void);
 
+void fsync_dir (char *path);
+
 void unknown_error (const char *detail);
 
 void bailout_if_drive_is_locked (LibHalContext *hal_ctx, LibHalDrive *drive, const char *invoked_by_syscon_name);


More information about the hal-commit mailing list