]> wimlib.net Git - wimlib/blobdiff - src/mount.c
Make WIM mounting work on FreeBSD
[wimlib] / src / mount.c
index f152fcd726d4d7e7f1e7dc287f566cf66fdc17ce..6efe84c6a375468654f246b3313b055d5782f682 100644 (file)
@@ -29,6 +29,7 @@
 #include "wimlib_internal.h"
 
 #ifdef WITH_FUSE
+
 #include "sha1.h"
 #include "lookup_table.h"
 #include "xml.h"
@@ -2030,6 +2031,30 @@ WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
        int msgsize;
        int errno_save;
 
+       mount_dir = dir;
+
+       /* Open message queues between the unmount process and the
+        * filesystem daemon. */
+       ret = open_message_queues(false);
+       if (ret != 0)
+               return ret;
+
+       /* Send a message to the filesystem saying whether to commit or
+        * not. */
+       msg[0] = (flags & WIMLIB_UNMOUNT_FLAG_COMMIT) ? 1 : 0;
+       msg[1] = (flags & WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY) ? 1 : 0;
+
+       DEBUG("Sending message: %s, %s", 
+                       (msg[0] == 0) ? "don't commit" : "commit",
+                       (msg[1] == 0) ? "don't check"  : "check");
+       ret = mq_send(unmount_to_daemon_mq, msg, 2, 1);
+       if (ret == -1) {
+               ERROR("Failed to notify filesystem daemon whether we want to "
+                     "commit changes or not");
+               close_message_queues();
+               return WIMLIB_ERR_MQUEUE;
+       }
+
        /* Execute `fusermount -u', which is installed setuid root, to unmount
         * the WIM.
         *
@@ -2042,7 +2067,6 @@ WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
         * the WIM file. 
         */
 
-       mount_dir = dir;
 
        pid = fork();
        if (pid == -1) {
@@ -2052,10 +2076,10 @@ WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
        if (pid == 0) {
                execlp("fusermount", "fusermount", "-u", dir, NULL);
                ERROR_WITH_ERRNO("Failed to execute `fusermount'");
-               return WIMLIB_ERR_FUSERMOUNT;
+               exit(WIMLIB_ERR_FUSERMOUNT);
        }
 
-       ret = waitpid(pid, &status, 0);
+       ret = wait(&status);
        if (ret == -1) {
                ERROR_WITH_ERRNO("Failed to wait for fusermount process to "
                                 "terminate");
@@ -2064,31 +2088,35 @@ WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
 
        if (status != 0) {
                ERROR("fusermount exited with status %d", status);
-               return WIMLIB_ERR_FUSERMOUNT;
-       }
 
-       /* Open message queues between the unmount process and the
-        * filesystem daemon. */
-       ret = open_message_queues(false);
-       if (ret != 0)
-               return ret;
+               /* Try again, but with the `umount' program.  This is required
+                * on other FUSE implementations such as FreeBSD's that do not
+                * have a `fusermount' program. */
 
-       /* Send a message to the filesystem saying whether to commit or
-        * not. */
-       msg[0] = (flags & WIMLIB_UNMOUNT_FLAG_COMMIT) ? 1 : 0;
-       msg[1] = (flags & WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY) ? 1 : 0;
+               pid = fork();
+               if (pid == -1) {
+                       ERROR_WITH_ERRNO("Failed to fork()");
+                       return WIMLIB_ERR_FORK;
+               }
+               if (pid == 0) {
+                       execlp("umount", "umount", dir, NULL);
+                       ERROR_WITH_ERRNO("Failed to execute `umount'");
+                       exit(WIMLIB_ERR_FUSERMOUNT);
+               }
 
-       DEBUG("Sending message: %s, %s", 
-                       (msg[0] == 0) ? "don't commit" : "commit",
-                       (msg[1] == 0) ? "don't check"  : "check");
-       ret = mq_send(unmount_to_daemon_mq, msg, 2, 1);
-       if (ret == -1) {
-               ERROR("Failed to notify filesystem daemon whether we want to "
-                     "commit changes or not");
-               close_message_queues();
-               return WIMLIB_ERR_MQUEUE;
+               ret = wait(&status);
+               if (ret == -1) {
+                       ERROR_WITH_ERRNO("Failed to wait for `umount' process to "
+                                        "terminate");
+                       return WIMLIB_ERR_FUSERMOUNT;
+               }
+               if (status != 0) {
+                       ERROR("`umount' exited with failure status");
+                       return WIMLIB_ERR_FUSERMOUNT;
+               }
        }
 
+
        /* Wait for a message from the filesytem daemon indicating whether  the
         * filesystem was unmounted successfully (0) or an error occurred (1).
         * This may take a long time if a big WIM file needs to be rewritten. */