]> wimlib.net Git - wimlib/blobdiff - src/wim.c
Make WIMStructs reference-counted
[wimlib] / src / wim.c
index 9b5e4b24856d8b70deec0bfc50059158ec0c6e0f..e01131ddffabfdc1504fc3b828d762f880ba526e 100644 (file)
--- a/src/wim.c
+++ b/src/wim.c
@@ -146,12 +146,12 @@ new_wim_struct(void)
        if (!wim)
                return NULL;
 
+       wim->refcnt = 1;
        filedes_invalidate(&wim->in_fd);
        filedes_invalidate(&wim->out_fd);
        wim->out_solid_compression_type = wim_default_solid_compression_type();
        wim->out_solid_chunk_size = wim_default_solid_chunk_size(
                                        wim->out_solid_compression_type);
-       INIT_LIST_HEAD(&wim->subwims);
        return wim;
 }
 
@@ -325,11 +325,11 @@ select_wim_image(WIMStruct *wim, int image)
                return WIMLIB_ERR_METADATA_NOT_FOUND;
 
        /* If a valid image is currently selected, its metadata can be freed if
-        * it has not been modified.  */
+        * it is not dirty and no other WIMStructs may have it selected.  */
        deselect_current_wim_image(wim);
        wim->current_image = image;
        imd = wim_get_current_image_metadata(wim);
-       if (imd->root_dentry || imd->modified) {
+       if (imd->root_dentry || is_image_dirty(imd)) {
                ret = 0;
        } else {
                ret = read_metadata_resource(imd);
@@ -346,7 +346,7 @@ deselect_current_wim_image(WIMStruct *wim)
        if (wim->current_image == WIMLIB_NO_IMAGE)
                return;
        imd = wim_get_current_image_metadata(wim);
-       if (!imd->modified) {
+       if (!is_image_dirty(imd) && imd->refcnt == 1) {
                wimlib_assert(list_empty(&imd->unhashed_blobs));
                destroy_image_metadata(imd, NULL, false);
        }
@@ -733,6 +733,9 @@ begin_read(WIMStruct *wim, const void *wim_filename_or_fd, int open_flags)
                if (!wim->blob_table)
                        return WIMLIB_ERR_NOMEM;
        } else {
+               if (wim->hdr.blob_table_reshdr.uncompressed_size == 0 &&
+                   wim->hdr.xml_data_reshdr.uncompressed_size == 0)
+                       return WIMLIB_ERR_WIM_IS_INCOMPLETE;
 
                ret = read_wim_xml_data(wim);
                if (ret)
@@ -864,6 +867,20 @@ can_modify_wim(WIMStruct *wim)
        return 0;
 }
 
+/* Free a WIMStruct after no more resources reference it.  */
+void
+finalize_wim_struct(WIMStruct *wim)
+{
+       if (filedes_valid(&wim->in_fd))
+               filedes_close(&wim->in_fd);
+       if (filedes_valid(&wim->out_fd))
+               filedes_close(&wim->out_fd);
+       wimlib_free_decompressor(wim->decompressor);
+       xml_free_info_struct(wim->xml_info);
+       FREE(wim->filename);
+       FREE(wim);
+}
+
 /* API function documented in wimlib.h  */
 WIMLIBAPI void
 wimlib_free(WIMStruct *wim)
@@ -871,31 +888,22 @@ wimlib_free(WIMStruct *wim)
        if (!wim)
                return;
 
-       while (!list_empty(&wim->subwims)) {
-               WIMStruct *subwim;
-
-               subwim = list_entry(wim->subwims.next, WIMStruct, subwim_node);
-               list_del(&subwim->subwim_node);
-               wimlib_free(subwim);
-       }
-
-       if (filedes_valid(&wim->in_fd))
-               filedes_close(&wim->in_fd);
-       if (filedes_valid(&wim->out_fd))
-               filedes_close(&wim->out_fd);
+       /* The blob table and image metadata are freed immediately, but other
+        * members of the WIMStruct such as the input file descriptor are
+        * retained until no more exported resources reference the WIMStruct. */
 
        free_blob_table(wim->blob_table);
-
-       wimlib_free_decompressor(wim->decompressor);
-
-       FREE(wim->filename);
-       xml_free_info_struct(wim->xml_info);
-       if (wim->image_metadata) {
-               for (unsigned i = 0; i < wim->hdr.image_count; i++)
+       wim->blob_table = NULL;
+       if (wim->image_metadata != NULL) {
+               for (int i = 0; i < wim->hdr.image_count; i++)
                        put_image_metadata(wim->image_metadata[i], NULL);
                FREE(wim->image_metadata);
+               wim->image_metadata = NULL;
        }
-       FREE(wim);
+
+       wimlib_assert(wim->refcnt > 0);
+       if (--wim->refcnt == 0)
+               finalize_wim_struct(wim);
 }
 
 static bool