+static int
+inode_close_fds(struct wim_inode *inode)
+{
+ u16 num_opened_fds = inode->i_num_opened_fds;
+ for (u16 i = 0, j = 0; j < num_opened_fds; i++) {
+ struct wimfs_fd *fd = inode->i_fds[i];
+ if (fd) {
+ wimlib_assert(fd->f_inode == inode);
+ int ret = close_wimfs_fd(fd);
+ if (ret != 0)
+ return ret;
+ j++;
+ }
+ }
+ return 0;
+}
+
+/* Overwrites the WIM file, with changes saved. */
+static int
+rebuild_wim(struct wimfs_context *ctx, int write_flags,
+ wimlib_progress_func_t progress_func)
+{
+ int ret;
+ struct wim_lookup_table_entry *lte, *tmp;
+ WIMStruct *w = ctx->wim;
+ struct wim_image_metadata *imd = wim_get_current_image_metadata(ctx->wim);
+
+ DEBUG("Closing all staging file descriptors.");
+ image_for_each_unhashed_stream_safe(lte, tmp, imd) {
+ ret = inode_close_fds(lte->back_inode);
+ if (ret)
+ return ret;
+ }
+
+ DEBUG("Freeing entries for zero-length streams");
+ image_for_each_unhashed_stream_safe(lte, tmp, imd) {
+ wimlib_assert(lte->unhashed);
+ if (wim_resource_size(lte) == 0) {
+ print_lookup_table_entry(lte, stderr);
+ struct wim_lookup_table_entry **back_ptr;
+ back_ptr = retrieve_lte_pointer(lte);
+ *back_ptr = NULL;
+ list_del(<e->unhashed_list);
+ free_lookup_table_entry(lte);
+ }
+ }
+
+ xml_update_image_info(w, w->current_image);
+ ret = wimlib_overwrite(w, write_flags, 0, progress_func);
+ if (ret)
+ ERROR("Failed to commit changes to mounted WIM image");
+ return ret;
+}