]> wimlib.net Git - wimlib/blobdiff - src/mount_image.c
Fixes, comments
[wimlib] / src / mount_image.c
index 128e1736dcb21d3b157a6c7854a486618424f971..2d0d56c65c1b96eeb9dbc4ad6f718eef68ecd698 100644 (file)
@@ -98,11 +98,8 @@ struct wimfs_context {
         * filesystem anyway. */
        u64 next_ino;
 
-       /* List of lookup table entries for files in the staging directory */
-       struct list_head staging_list;
-
        /* List of inodes in the mounted image */
-       struct hlist_head *image_inode_list;
+       struct list_head *image_inode_list;
 
        /* Name and message queue descriptors for message queues between the
         * filesystem daemon process and the unmount process.  These are used
@@ -128,7 +125,6 @@ init_wimfs_context(struct wimfs_context *ctx)
        memset(ctx, 0, sizeof(*ctx));
        ctx->unmount_to_daemon_mq = (mqd_t)-1;
        ctx->daemon_to_unmount_mq = (mqd_t)-1;
-       INIT_LIST_HEAD(&ctx->staging_list);
 }
 
 #define WIMFS_CTX(fuse_ctx) ((struct wimfs_context*)(fuse_ctx)->private_data)
@@ -296,7 +292,7 @@ close_wimfs_fd(struct wimfs_fd *fd)
              fd->f_inode->i_ino, fd->f_inode->i_num_opened_fds,
              fd->f_inode->i_num_allocated_fds);
        ret = lte_put_fd(fd->f_lte, fd);
-       if (ret != 0)
+       if (ret)
                return ret;
 
        inode_put_fd(fd->f_inode, fd);
@@ -350,7 +346,7 @@ create_dentry(struct fuse_context *fuse_ctx, const char *path,
                }
        }
        dentry_add_child(parent, new);
-       hlist_add_head(&new->d_inode->i_hlist, wimfs_ctx->image_inode_list);
+       list_add_tail(&new->d_inode->i_list, wimfs_ctx->image_inode_list);
        if (dentry_ret)
                *dentry_ret = new;
        return 0;
@@ -382,7 +378,7 @@ remove_dentry(struct wim_dentry *dentry,
                        lte_decrement_refcnt(lte, lookup_table);
        }
        unlink_dentry(dentry);
-       put_dentry(dentry);
+       free_dentry(dentry);
 }
 
 static mode_t
@@ -651,15 +647,21 @@ extract_resource_to_staging_dir(struct wim_inode *inode,
        new_lte->staging_file_name = staging_file_name;
        new_lte->lte_inode         = inode;
 
-       if (stream_id == 0)
-               inode->i_lte = new_lte;
-       else
-               for (u16 i = 0; i < inode->i_num_ads; i++)
-                       if (inode->i_ads_entries[i].stream_id == stream_id)
-                               inode->i_ads_entries[i].lte = new_lte;
+       struct wim_lookup_table_entry **my_ptr;
 
-       lookup_table_insert_unhashed(ctx->wim->lookup_table, new_lte);
-       list_add_tail(&new_lte->staging_list, &ctx->staging_list);
+       if (stream_id == 0) {
+               my_ptr = &inode->i_lte;
+       } else {
+               for (u16 i = 0; ; i++) {
+                       wimlib_assert(i < inode->i_num_ads);
+                       if (inode->i_ads_entries[i].stream_id == stream_id) {
+                               my_ptr = &inode->i_ads_entries[i].lte;
+                               break;
+                       }
+               }
+       }
+
+       lookup_table_insert_unhashed(ctx->wim->lookup_table, new_lte, my_ptr);
        *lte = new_lte;
        return 0;
 out_revert_fd_changes:
@@ -777,34 +779,6 @@ delete_staging_dir(struct wimfs_context *ctx)
        return ret;
 }
 
-static void
-inode_update_lte_ptr(struct wim_inode *inode,
-                    struct wim_lookup_table_entry *old_lte,
-                    struct wim_lookup_table_entry *new_lte)
-{
-       if (inode->i_lte == old_lte) {
-               inode->i_lte = new_lte;
-       } else {
-               for (unsigned i = 0; i < inode->i_num_ads; i++) {
-                       if (inode->i_ads_entries[i].lte == old_lte) {
-                               inode->i_ads_entries[i].lte = new_lte;
-                               break;
-                       }
-               }
-       }
-}
-
-static void
-free_lte_if_unneeded(struct wim_lookup_table_entry *lte)
-{
-
-       if (wim_resource_size(lte) == 0) {
-               /* Zero-length stream.  No lookup table entry needed. */
-               inode_update_lte_ptr(lte->lte_inode, lte, NULL);
-               free_lookup_table_entry(lte);
-       }
-}
-
 static int
 inode_close_fds(struct wim_inode *inode)
 {
@@ -828,25 +802,28 @@ rebuild_wim(struct wimfs_context *ctx, int write_flags,
            wimlib_progress_func_t progress_func)
 {
        int ret;
-       struct wim_lookup_table_entry *lte, *tmp;
+       struct wim_lookup_table_entry *lte;
        WIMStruct *w = ctx->wim;
+       struct wim_image_metadata *imd = wim_get_current_image_metadata(ctx->wim);
 
        DEBUG("Closing all staging file descriptors.");
-       list_for_each_entry_safe(lte, tmp, &ctx->staging_list, staging_list) {
+       image_for_each_unhashed_stream(lte, imd) {
                ret = inode_close_fds(lte->lte_inode);
-               if (ret != 0)
+               if (ret)
                        return ret;
        }
 
        DEBUG("Freeing entries for zero-length streams");
-       list_for_each_entry_safe(lte, tmp, &ctx->staging_list, staging_list)
-               free_lte_if_unneeded(lte);
+       image_for_each_unhashed_stream(lte, imd) {
+               if (wim_resource_size(lte) == 0) {
+                       *lte->my_ptr = NULL;
+                       free_lookup_table_entry(lte);
+               }
+       }
 
        xml_update_image_info(w, w->current_image);
-       list_splice(&ctx->staging_list,
-                   &w->image_metadata[w->current_image - 1].unhashed_streams);
        ret = wimlib_overwrite(w, write_flags, 0, progress_func);
-       if (ret != 0)
+       if (ret)
                ERROR("Failed to commit changes to mounted WIM image");
        return ret;
 }
@@ -1694,9 +1671,7 @@ wimfs_link(const char *to, const char *from)
        struct wim_dentry *from_dentry, *from_dentry_parent;
        const char *link_name;
        struct wim_inode *inode;
-       struct wim_lookup_table_entry *lte;
        WIMStruct *w = wimfs_get_WIMStruct();
-       u16 i;
        int ret;
 
        inode = wim_pathname_to_inode(w, to);
@@ -1721,15 +1696,10 @@ wimfs_link(const char *to, const char *from)
        if (ret)
                return -ENOMEM;
 
-       inode_add_dentry(from_dentry, inode);
-       from_dentry->d_inode = inode;
        inode->i_nlink++;
-
-       for (i = 0; i <= inode->i_num_ads; i++) {
-               lte = inode_stream_lte_resolved(inode, i);
-               if (lte)
-                       lte->refcnt++;
-       }
+       inode_ref_streams(inode);
+       from_dentry->d_inode = inode;
+       inode_add_dentry(from_dentry, inode);
        dentry_add_child(from_dentry_parent, from_dentry);
        return 0;
 }
@@ -2441,7 +2411,6 @@ wimlib_mount_image(WIMStruct *wim, int image, const char *dir,
        struct wim_lookup_table *joined_tab, *wim_tab_save;
        struct wim_image_metadata *imd;
        struct wimfs_context ctx;
-       struct hlist_node *cur_node;
        struct wim_inode *inode;
 
        DEBUG("Mount: wim = %p, image = %d, dir = %s, flags = %d, ",
@@ -2451,7 +2420,7 @@ wimlib_mount_image(WIMStruct *wim, int image, const char *dir,
                return WIMLIB_ERR_INVALID_PARAM;
 
        ret = verify_swm_set(wim, additional_swms, num_additional_swms);
-       if (ret != 0)
+       if (ret)
                return ret;
 
        if ((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) && (wim->hdr.total_parts != 1)) {
@@ -2463,7 +2432,7 @@ wimlib_mount_image(WIMStruct *wim, int image, const char *dir,
                ret = new_joined_lookup_table(wim, additional_swms,
                                              num_additional_swms,
                                              &joined_tab);
-               if (ret != 0)
+               if (ret)
                        return ret;
                wim_tab_save = wim->lookup_table;
                wim->lookup_table = joined_tab;
@@ -2471,28 +2440,25 @@ wimlib_mount_image(WIMStruct *wim, int image, const char *dir,
 
        if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
                ret = wim_run_full_verifications(wim);
-               if (ret != 0)
+               if (ret)
                        goto out;
        }
 
        ret = select_wim_image(wim, image);
-       if (ret != 0)
+       if (ret)
                goto out;
 
        DEBUG("Selected image %d", image);
 
        imd = wim_get_current_image_metadata(wim);
 
-       if (imd->root_dentry->refcnt != 1) {
+       if (imd->refcnt != 1) {
                ERROR("Cannot mount image that was just exported with "
                      "wimlib_export_image()");
                ret = WIMLIB_ERR_INVALID_PARAM;
                goto out;
        }
 
-       if (imd->inode_list.first) /* May be unneeded? */
-               imd->inode_list.first->pprev = &imd->inode_list.first;
-
        if (imd->modified) {
                ERROR("Cannot mount image that was added "
                      "with wimlib_add_image()");
@@ -2502,7 +2468,7 @@ wimlib_mount_image(WIMStruct *wim, int image, const char *dir,
 
        if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
                ret = lock_wim(wim, wim->fp);
-               if (ret != 0)
+               if (ret)
                        goto out;
        }
 
@@ -2520,12 +2486,13 @@ wimlib_mount_image(WIMStruct *wim, int image, const char *dir,
        ctx.image_inode_list = &imd->inode_list;
        ctx.default_uid = getuid();
        ctx.default_gid = getgid();
+       ctx.wim->lookup_table->unhashed_streams = &imd->unhashed_streams;
        if (mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
                ctx.default_lookup_flags = LOOKUP_FLAG_ADS_OK;
 
        DEBUG("Unlinking message queues in case they already exist");
        ret = set_message_queue_names(&ctx, dir);
-       if (ret != 0)
+       if (ret)
                goto out_unlock;
        unlink_message_queues(&ctx);
 
@@ -2563,7 +2530,7 @@ wimlib_mount_image(WIMStruct *wim, int image, const char *dir,
        if ((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
                /* Read-write mount.  Make the staging directory */
                ret = make_staging_dir(&ctx, staging_dir);
-               if (ret != 0)
+               if (ret)
                        goto out_free_dir_copy;
        } else {
                /* Read-only mount */
@@ -2596,7 +2563,7 @@ wimlib_mount_image(WIMStruct *wim, int image, const char *dir,
         * assign inode numbers */
        DEBUG("Resolving lookup table entries and assigning inode numbers");
        ctx.next_ino = 1;
-       hlist_for_each_entry(inode, cur_node, &imd->inode_list, i_hlist) {
+       image_for_each_inode(inode, imd) {
                inode_resolve_ltes(inode, wim->lookup_table);
                inode->i_ino = ctx.next_ino++;
        }