]> wimlib.net Git - wimlib/blobdiff - src/mount.c
Various minor changes and fixes.
[wimlib] / src / mount.c
index 7f0def11e27f37ec338bc0272c3c03b015ff5509..53a7f364e0989f6eb1e4d56e04a02ff94d2a2527 100644 (file)
@@ -5,28 +5,31 @@
  * Filesystem in Userspace.  FUSE allows a filesystem to be implemented in a
  * userspace process by implementing the filesystem primitives--- read(),
  * write(), readdir(), etc.
- *
+ */
+
+/*
  * Copyright (C) 2012 Eric Biggers
  *
- * wimlib - Library for working with WIM files 
+ * This file is part of wimlib, a library for working with WIM files.
  *
- * This library is free software; you can redistribute it and/or modify it under
- * the terms of the GNU Lesser General Public License as published by the Free
- * Software Foundation; either version 2.1 of the License, or (at your option) any
- * later version.
+ * wimlib is free software; you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation; either version 2.1 of the License, or (at your option)
+ * any later version.
  *
- * This library is distributed in the hope that it will be useful, but WITHOUT ANY
- * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
- * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+ * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+ * details.
  *
- * You should have received a copy of the GNU Lesser General Public License along
- * with this library; if not, write to the Free Software Foundation, Inc., 59
- * Temple Place, Suite 330, Boston, MA 02111-1307 USA 
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with wimlib; if not, see http://www.gnu.org/licenses/.
  */
 
 #include "wimlib_internal.h"
 
 #ifdef WITH_FUSE
+#include "sha1.h"
 #include "lookup_table.h"
 #include "xml.h"
 #include <stdlib.h>
@@ -81,7 +84,7 @@ static void make_staging_dir()
 
        staging_dir_name = MALLOC(staging_dir_name_len + 1);
        if (!staging_dir_name) {
-               ERROR("Out of memory!\n");
+               ERROR("Out of memory");
                return;
        }
 
@@ -93,8 +96,8 @@ static void make_staging_dir()
        staging_dir_name[staging_dir_name_len] = '\0';
 
        if (mkdir(staging_dir_name, 0700) != 0) {
-               ERROR("Failed to create temporary directory `%s': %m\n",
-                               staging_dir_name);
+               ERROR_WITH_ERRNO("Failed to create temporary directory `%s'",
+                                staging_dir_name);
                FREE(staging_dir_name);
                staging_dir_name = NULL;
        }
@@ -191,13 +194,13 @@ static int open_message_queues(bool daemon)
        unmount_to_daemon_mq_name = strcat_dup(slash, mount_dir_basename,
                                                prefix, u2d_suffix);
        if (!unmount_to_daemon_mq_name) {
-               ERROR("Out of memory!\n");
+               ERROR("Out of memory");
                return WIMLIB_ERR_NOMEM;
        }
        daemon_to_unmount_mq_name = strcat_dup(slash, mount_dir_basename,
                                                prefix, d2u_suffix);
        if (!daemon_to_unmount_mq_name) {
-               ERROR("Out of memory!\n");
+               ERROR("Out of memory");
                ret = WIMLIB_ERR_NOMEM;
                goto err1;
        }
@@ -216,7 +219,7 @@ static int open_message_queues(bool daemon)
                                       0700, NULL);
 
        if (unmount_to_daemon_mq == -1) {
-               ERROR("mq_open(): %m\n");
+               ERROR_WITH_ERRNO("mq_open()");
                ret = WIMLIB_ERR_MQUEUE;
                goto err2;
        }
@@ -230,7 +233,7 @@ static int open_message_queues(bool daemon)
                                       0700, NULL);
 
        if (daemon_to_unmount_mq == -1) {
-               ERROR("mq_open(): %m\n");
+               ERROR_WITH_ERRNO("mq_open()");
                ret = WIMLIB_ERR_MQUEUE;
                goto err3;
        }
@@ -255,19 +258,19 @@ static int mq_get_msgsize(mqd_t mq)
        if (mq_getattr(unmount_to_daemon_mq, &attr) == 0) {
                msgsize = attr.mq_msgsize;
        } else {
-               ERROR("mq_getattr(): %m\n");
-               ERROR("Attempting to read %s\n", msgsize_max_file);
+               ERROR_WITH_ERRNO("mq_getattr()");
+               ERROR("Attempting to read %s", msgsize_max_file);
                fp = fopen(msgsize_max_file, "rb");
                if (fp) {
                        if (fscanf(fp, "%d", &msgsize) != 1) {
-                               ERROR("Assuming message size of 8192\n");
+                               ERROR("Assuming message size of 8192");
                                msgsize = 8192;
                        }
                        fclose(fp);
                } else {
-                       ERROR("Failed to open file %s: %m\n", 
-                               msgsize_max_file);
-                       ERROR("Assuming message size of 8192\n");
+                       ERROR_WITH_ERRNO("Failed to open the file `%s'",
+                                        msgsize_max_file);
+                       ERROR("Assuming message size of 8192");
                        msgsize = 8192;
                }
        }
@@ -295,8 +298,8 @@ static int close_staging_file(struct lookup_table_entry *lte, void *ignore)
 {
        if (lte->staging_file_name && lte->staging_num_times_opened) {
                if (close(lte->staging_fd) != 0) {
-                       ERROR("Failed close file `%s': %m\n",
-                                       lte->staging_file_name);
+                       ERROR_WITH_ERRNO("Failed close file `%s'",
+                                        lte->staging_file_name);
                        return WIMLIB_ERR_WRITE;
                }
        }
@@ -320,7 +323,8 @@ static int calculate_sha1sum_for_staging_file(struct dentry *dentry, void *looku
        
        if (lte && lte->staging_file_name) {
 
-               DEBUG("Calculating SHA1 hash for file `%s'\n", dentry->file_name_utf8);
+               DEBUG("Calculating SHA1 hash for file `%s'",
+                     dentry->file_name_utf8);
                ret = sha1sum(lte->staging_file_name, dentry->hash);
                if (ret != 0)
                        return ret;
@@ -329,8 +333,8 @@ static int calculate_sha1sum_for_staging_file(struct dentry *dentry, void *looku
                memcpy(lte->hash, dentry->hash, WIM_HASH_SIZE);
                existing = lookup_resource(table, dentry->hash);
                if (existing) {
-                       DEBUG("Merging duplicate lookup table entries for "
-                               "file `%s'\n", dentry->file_name_utf8);
+                       DEBUG("Merging duplicate lookup table entries for file "
+                             "`%s'", dentry->file_name_utf8);
                        free_lookup_table_entry(lte);
                        existing->refcnt++;
                } else {
@@ -348,22 +352,21 @@ static int rebuild_wim(WIMStruct *w, bool check_integrity)
 
        root = wim_root_dentry(w);
 
-       DEBUG("Closing all staging file descriptors.\n");
+       DEBUG("Closing all staging file descriptors.");
        /* Close all the staging file descriptors. */
-       ret = for_lookup_table_entry(w->lookup_table, 
-                                    close_staging_file, NULL);
+       ret = for_lookup_table_entry(w->lookup_table, close_staging_file, NULL);
        if (ret != 0) {
-               ERROR("Failed to close all staging files!\n");
+               ERROR("Failed to close all staging files");
                return ret;
        }
 
-       DEBUG("Calculating SHA1 checksums for all new staging files.\n");
+       DEBUG("Calculating SHA1 checksums for all new staging files.");
        /* Calculate SHA1 checksums for all staging files, and merge unnecessary
         * lookup table entries. */
        ret = for_dentry_in_tree(root, calculate_sha1sum_for_staging_file,
                                 w->lookup_table);
        if (ret != 0) {
-               ERROR("Failed to calculate new SHA1 checksums!\n");
+               ERROR("Failed to calculate new SHA1 checksums");
                return ret;
        }
 
@@ -371,7 +374,7 @@ static int rebuild_wim(WIMStruct *w, bool check_integrity)
 
        ret = wimlib_overwrite(w, check_integrity);
        if (ret != 0) {
-               ERROR("Failed to commit changes\n");
+               ERROR("Failed to commit changes");
                return ret;
        }
        return ret;
@@ -409,8 +412,8 @@ static void wimfs_destroy(void *p)
        gettimeofday(&now, NULL);
        timeout.tv_sec = now.tv_sec + 3;
        timeout.tv_nsec = now.tv_usec * 1000;
-       DEBUG("Waiting for message telling us whether to commit or not, "
-                       "and whether to include integrity checks.\n");
+       DEBUG("Waiting for message telling us whether to commit or not, and "
+             "whether to include integrity checks.");
 
        bytes_received = mq_timedreceive(unmount_to_daemon_mq, msg, 
                                         msgsize, NULL, &timeout);
@@ -418,36 +421,38 @@ static void wimfs_destroy(void *p)
        check_integrity = msg[1];
        if (bytes_received == -1) {
                if (errno == ETIMEDOUT) {
-                       ERROR("Timed out.\n");
+                       ERROR("Timed out.");
                } else {
-                       ERROR("mq_timedreceive(): %m\n");
+                       ERROR_WITH_ERRNO("mq_timedreceive()");
                }
-               ERROR("Not committing.\n");
+               ERROR("Not committing.");
        } else {
-               DEBUG("Received message: [%d %d]\n", msg[0], msg[1]);
+               DEBUG("Received message: [%d %d]", msg[0], msg[1]);
        }
 
-       if (commit && (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
-               status = chdir(working_directory);
-               if (status != 0) {
-                       ERROR("chdir(): %m\n");
-                       status = WIMLIB_ERR_NOTDIR;
-                       goto done;
+       status = 0;
+       if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
+               if (commit) {
+                       status = chdir(working_directory);
+                       if (status != 0) {
+                               ERROR_WITH_ERRNO("chdir()");
+                               status = WIMLIB_ERR_NOTDIR;
+                               goto done;
+                       }
+                       status = rebuild_wim(w, (check_integrity != 0));
+               }
+               ret = delete_staging_dir();
+               if (ret != 0) {
+                       ERROR_WITH_ERRNO("Failed to delete the staging "
+                                        "directory");
+                       if (status == 0)
+                               status = ret;
                }
-               status = rebuild_wim(w, (check_integrity != 0));
-       } else {
-               status = 0;
-       }
-       ret = delete_staging_dir();
-       if (ret != 0) {
-               ERROR("Failed to delete the staging directory: %m\n");
-               if (status == 0)
-                       status = ret;
        }
 done:
        ret = mq_send(daemon_to_unmount_mq, &status, 1, 1);
        if (ret == -1)
-               ERROR("Failed to send status to unmount process: %m\n");
+               ERROR_WITH_ERRNO("Failed to send status to unmount process");
        close_message_queues();
 }
 
@@ -533,7 +538,7 @@ static int create_staging_file(char **name_ret)
                /* doesn't exist--- ok */
        }
 
-       DEBUG("Creating staging file '%s'\n", name);
+       DEBUG("Creating staging file '%s'", name);
 
        fd = creat(name, 0600); 
        if (fd == -1) {
@@ -627,7 +632,6 @@ static int wimfs_open(const char *path, struct fuse_file_info *fi)
                /* no lookup table entry, so the file must be empty.  Create a
                 * lookup table entry for the file. */
                char *tmpfile_name;
-               int err;
                int fd;
 
                lte = new_lookup_table_entry();
@@ -637,9 +641,9 @@ static int wimfs_open(const char *path, struct fuse_file_info *fi)
                fd = create_staging_file(&tmpfile_name);
 
                if (fd == -1) {
-                       err = errno;
+                       int err = errno;
                        free(lte);
-                       return -errno;
+                       return -err;
                }
                lte->resource_entry.original_size = 0;
                randomize_byte_array(lte->hash, WIM_HASH_SIZE);
@@ -869,7 +873,7 @@ static int extract_resource_to_staging_dir(struct dentry *dentry,
                                           struct lookup_table_entry *lte, 
                                           u64 size)
 {
-       int err, fd;
+       int fd;
        bool ret;
        char *staging_file_name;
        struct lookup_table_entry *new_lte;
@@ -1080,7 +1084,7 @@ WIMLIBAPI int wimlib_mount(WIMStruct *wim, int image, const char *dir,
        mount_dir = dir;
        working_directory = getcwd(NULL, 0);
        if (!working_directory) {
-               ERROR("Could not determine current directory: %m\n");
+               ERROR_WITH_ERRNO("Could not determine current directory");
                return WIMLIB_ERR_NOTDIR;
        }
 
@@ -1159,24 +1163,24 @@ WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
        mount_dir = dir;
        pid = fork();
        if (pid == -1) {
-               ERROR("Failed to fork(): %m\n");
+               ERROR_WITH_ERRNO("Failed to fork()");
                return WIMLIB_ERR_FORK;
        }
        if (pid == 0) {
                execlp("fusermount", "fusermount", "-u", dir, NULL);
-               ERROR("Failed to execute `fusermount': %m\n");
+               ERROR_WITH_ERRNO("Failed to execute `fusermount'");
                return WIMLIB_ERR_FUSERMOUNT;
        }
 
        ret = waitpid(pid, &status, 0);
        if (ret == -1) {
-               ERROR("Failed to wait for fusermount process to "
-                               "terminate: %m\n");
+               ERROR_WITH_ERRNO("Failed to wait for fusermount process to "
+                                "terminate");
                return WIMLIB_ERR_FUSERMOUNT;
        }
 
        if (status != 0) {
-               ERROR("fusermount exited with status %d!\n", status);
+               ERROR("fusermount exited with status %d", status);
                return WIMLIB_ERR_FUSERMOUNT;
        }
 
@@ -1191,13 +1195,13 @@ WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
        msg[0] = (flags & WIMLIB_UNMOUNT_FLAG_COMMIT) ? 1 : 0;
        msg[1] = (flags & WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY) ? 1 : 0;
 
-       DEBUG("Sending message: %s, %s\n", 
+       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!\n");
+               ERROR("Failed to notify filesystem daemon whether we want to "
+                     "commit changes or not");
                close_message_queues();
                return WIMLIB_ERR_MQUEUE;
        }
@@ -1222,28 +1226,27 @@ WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
 
        mailbox[0] = 0;
        DEBUG("Waiting for message telling us whether the unmount was "
-                       "successful or not.\n");
+                       "successful or not.");
        ret = mq_timedreceive(daemon_to_unmount_mq, mailbox, msgsize,
                              NULL, &timeout);
        errno_save = errno;
        close_message_queues();
        if (ret == -1) {
                if (errno_save == ETIMEDOUT) {
-                       ERROR("Timed out- probably the filesystem "
-                                       "daemon crashed and the WIM was not "
-                                       "written successfully.\n");
+                       ERROR("Timed out- probably the filesystem daemon "
+                             "crashed and the WIM was not written "
+                             "successfully.");
                        return WIMLIB_ERR_TIMEOUT;
                } else {
-                       ERROR("mq_receive(): %s\n",
-                                       strerror(errno_save));
+                       ERROR("mq_receive(): %s", strerror(errno_save));
                        return WIMLIB_ERR_MQUEUE;
                }
 
        }
-       DEBUG("Received message: %s\n", (mailbox[0] == 0) ? 
-                                       "Unmount OK" : "Unmount Failed");
+       DEBUG("Received message: %s",
+             (mailbox[0] == 0) ?  "Unmount OK" : "Unmount Failed");
        if (mailbox[0] != 0)
-               ERROR("Unmount failed\n");
+               ERROR("Unmount failed");
        return mailbox[0];
 }
 
@@ -1252,8 +1255,8 @@ WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
 
 static inline int mount_unsupported_error()
 {
-       ERROR("WIMLIB was compiled with --without-fuse, which "
-                       "disables support for mounting WIMs.\n");
+       ERROR("WIMLIB was compiled with --without-fuse, which disables support "
+             "for mounting WIMs.");
        return WIMLIB_ERR_UNSUPPORTED;
 }