From 476dd36af55f6835a719bf275ee3b772b7fa652c Mon Sep 17 00:00:00 2001 From: Martin Pitt Date: Tue, 13 Jul 2010 07:58:06 +0000 Subject: 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! --- tools/hal-storage-shared.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'tools/hal-storage-shared.c') diff --git a/tools/hal-storage-shared.c b/tools/hal-storage-shared.c index 422f00e7..e7e28257 100644 --- a/tools/hal-storage-shared.c +++ b/tools/hal-storage-shared.c @@ -47,6 +47,7 @@ #endif #include #include +#include #include #include #include @@ -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"); -- cgit v1.2.3