]> wimlib.net Git - wimlib/blobdiff - src/wim.c
Serialize wimlib_global_init() and wimlib_global_cleanup()
[wimlib] / src / wim.c
index 7b1347dd93379ccc29726ad57d9d4c7f8a3fa689..a66d5cce1c16462670515398af70de3fd4993df3 100644 (file)
--- a/src/wim.c
+++ b/src/wim.c
 #  include "config.h"
 #endif
 
+#include <errno.h>
+#include <fcntl.h>
+#ifndef __WIN32__
+#  include <langinfo.h>
+#endif
+#include <pthread.h>
+#include <stdlib.h>
+#include <unistd.h>
+
 #include "wimlib.h"
+#include "wimlib/assert.h"
+#include "wimlib/blob_table.h"
+#include "wimlib/bitops.h"
 #include "wimlib/dentry.h"
 #include "wimlib/encoding.h"
 #include "wimlib/file_io.h"
 #include "wimlib/integrity.h"
-#include "wimlib/lookup_table.h"
 #include "wimlib/metadata.h"
 #ifdef WITH_NTFS_3G
 #  include "wimlib/ntfs_3g.h" /* for do_ntfs_umount() */
 #include "wimlib/security.h"
 #include "wimlib/wim.h"
 #include "wimlib/xml.h"
-#include "wimlib/version.h"
-
 #ifdef __WIN32__
 #  include "wimlib/win32.h" /* for realpath() replacement */
 #endif
 
-#include <errno.h>
-#include <fcntl.h>
-#ifndef __WIN32__
-#  include <langinfo.h>
-#endif
-#include <stdlib.h>
-#include <unistd.h>
-
 static int
-wim_default_pack_compression_type(void)
+wim_default_solid_compression_type(void)
 {
        return WIMLIB_COMPRESSION_TYPE_LZMS;
 }
 
 static u32
-wim_default_pack_chunk_size(int ctype) {
+wim_default_solid_chunk_size(int ctype) {
        switch (ctype) {
        case WIMLIB_COMPRESSION_TYPE_LZMS:
-               return 1U << 25; /* 33554432  */
+               return (u32)1 << 26; /* 67108864  */
        default:
-               return 1U << 15; /* 32768     */
+               return (u32)1 << 15; /* 32768     */
        }
 }
 
@@ -75,9 +76,9 @@ new_wim_struct(void)
 
        filedes_invalidate(&wim->in_fd);
        filedes_invalidate(&wim->out_fd);
-       wim->out_pack_compression_type = wim_default_pack_compression_type();
-       wim->out_pack_chunk_size = wim_default_pack_chunk_size(
-                                       wim->out_pack_compression_type);
+       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;
 }
@@ -96,7 +97,7 @@ wim_chunk_size_valid(u32 chunk_size, int ctype)
        /* Chunk size must be power of 2.  */
        if (chunk_size == 0)
                return false;
-       order = bsr32(chunk_size);
+       order = fls32(chunk_size);
        if (chunk_size != 1U << order)
                return false;
 
@@ -143,6 +144,19 @@ wim_default_chunk_size(int ctype)
        }
 }
 
+static int
+is_blob_in_solid_resource(struct blob_descriptor *blob, void *_ignore)
+{
+       return blob->blob_location == BLOB_IN_WIM &&
+               (blob->rdesc->flags & WIM_RESHDR_FLAG_SOLID);
+}
+
+bool
+wim_has_solid_resources(const WIMStruct *wim)
+{
+       return for_blob_in_table(wim->blob_table, is_blob_in_solid_resource, NULL);
+}
+
 /*
  * Calls a function on images in the WIM.  If @image is WIMLIB_ALL_IMAGES,
  * @visitor is called on the WIM once for each image, with each image selected
@@ -182,7 +196,6 @@ WIMLIBAPI int
 wimlib_create_new_wim(int ctype, WIMStruct **wim_ret)
 {
        WIMStruct *wim;
-       struct wim_lookup_table *table;
        int ret;
 
        ret = wimlib_global_init(WIMLIB_INIT_FLAG_ASSUME_UTF8);
@@ -197,12 +210,11 @@ wimlib_create_new_wim(int ctype, WIMStruct **wim_ret)
        if (ret)
                goto out_free_wim;
 
-       table = new_lookup_table(9001);
-       if (!table) {
+       wim->blob_table = new_blob_table(9001);
+       if (!wim->blob_table) {
                ret = WIMLIB_ERR_NOMEM;
                goto out_free_wim;
        }
-       wim->lookup_table = table;
        wim->compression_type = ctype;
        wim->out_compression_type = ctype;
        wim->chunk_size = wim->hdr.chunk_size;
@@ -217,24 +229,24 @@ out_free_wim:
 
 static void
 destroy_image_metadata(struct wim_image_metadata *imd,
-                      struct wim_lookup_table *table,
-                      bool free_metadata_lte)
+                      struct blob_table *table,
+                      bool free_metadata_blob_descriptor)
 {
        free_dentry_tree(imd->root_dentry, table);
        imd->root_dentry = NULL;
        free_wim_security_data(imd->security_data);
        imd->security_data = NULL;
 
-       if (free_metadata_lte) {
-               free_lookup_table_entry(imd->metadata_lte);
-               imd->metadata_lte = NULL;
+       if (free_metadata_blob_descriptor) {
+               free_blob_descriptor(imd->metadata_blob);
+               imd->metadata_blob = NULL;
        }
        if (!table) {
-               struct wim_lookup_table_entry *lte, *tmp;
-               list_for_each_entry_safe(lte, tmp, &imd->unhashed_streams, unhashed_list)
-                       free_lookup_table_entry(lte);
+               struct blob_descriptor *blob, *tmp;
+               list_for_each_entry_safe(blob, tmp, &imd->unhashed_blobs, unhashed_list)
+                       free_blob_descriptor(blob);
        }
-       INIT_LIST_HEAD(&imd->unhashed_streams);
+       INIT_LIST_HEAD(&imd->unhashed_blobs);
        INIT_LIST_HEAD(&imd->inode_list);
 #ifdef WITH_NTFS_3G
        if (imd->ntfs_vol) {
@@ -245,8 +257,7 @@ destroy_image_metadata(struct wim_image_metadata *imd,
 }
 
 void
-put_image_metadata(struct wim_image_metadata *imd,
-                  struct wim_lookup_table *table)
+put_image_metadata(struct wim_image_metadata *imd, struct blob_table *table)
 {
        if (imd && --imd->refcnt == 0) {
                destroy_image_metadata(imd, table, true);
@@ -280,7 +291,7 @@ new_image_metadata(void)
        if (imd) {
                imd->refcnt = 1;
                INIT_LIST_HEAD(&imd->inode_list);
-               INIT_LIST_HEAD(&imd->unhashed_streams);
+               INIT_LIST_HEAD(&imd->unhashed_blobs);
        }
        return imd;
 }
@@ -343,25 +354,32 @@ select_wim_image(WIMStruct *wim, int image)
 
        /* If a valid image is currently selected, its metadata can be freed if
         * it has not been modified.  */
-       if (wim->current_image != WIMLIB_NO_IMAGE) {
-               imd = wim_get_current_image_metadata(wim);
-               if (!imd->modified) {
-                       wimlib_assert(list_empty(&imd->unhashed_streams));
-                       destroy_image_metadata(imd, NULL, false);
-               }
-       }
+       deselect_current_wim_image(wim);
        wim->current_image = image;
        imd = wim_get_current_image_metadata(wim);
        if (imd->root_dentry || imd->modified) {
                ret = 0;
        } else {
-               ret = read_metadata_resource(wim, imd);
+               ret = read_metadata_resource(imd);
                if (ret)
                        wim->current_image = WIMLIB_NO_IMAGE;
        }
        return ret;
 }
 
+void
+deselect_current_wim_image(WIMStruct *wim)
+{
+       struct wim_image_metadata *imd;
+       if (wim->current_image == WIMLIB_NO_IMAGE)
+               return;
+       imd = wim_get_current_image_metadata(wim);
+       if (!imd->modified) {
+               wimlib_assert(list_empty(&imd->unhashed_blobs));
+               destroy_image_metadata(imd, NULL, false);
+       }
+       wim->current_image = WIMLIB_NO_IMAGE;
+}
 
 /* API function documented in wimlib.h  */
 WIMLIBAPI const tchar *
@@ -535,13 +553,13 @@ wimlib_set_output_compression_type(WIMStruct *wim, int ctype)
 WIMLIBAPI int
 wimlib_set_output_pack_compression_type(WIMStruct *wim, int ctype)
 {
-       int ret = set_out_ctype(ctype, &wim->out_pack_compression_type);
+       int ret = set_out_ctype(ctype, &wim->out_solid_compression_type);
        if (ret)
                return ret;
 
        /* Reset the chunk size if it's no longer valid.  */
-       if (!wim_chunk_size_valid(wim->out_pack_chunk_size, ctype))
-               wim->out_pack_chunk_size = wim_default_pack_chunk_size(ctype);
+       if (!wim_chunk_size_valid(wim->out_solid_chunk_size, ctype))
+               wim->out_solid_chunk_size = wim_default_solid_chunk_size(ctype);
        return 0;
 }
 
@@ -575,14 +593,14 @@ WIMLIBAPI int
 wimlib_set_output_pack_chunk_size(WIMStruct *wim, uint32_t chunk_size)
 {
        if (chunk_size == 0) {
-               wim->out_pack_chunk_size =
-                       wim_default_pack_chunk_size(wim->out_pack_compression_type);
+               wim->out_solid_chunk_size =
+                       wim_default_solid_chunk_size(wim->out_solid_compression_type);
                return 0;
        }
 
        return set_out_chunk_size(chunk_size,
-                                 wim->out_pack_compression_type,
-                                 &wim->out_pack_chunk_size);
+                                 wim->out_solid_compression_type,
+                                 &wim->out_solid_chunk_size);
 }
 
 WIMLIBAPI void
@@ -610,7 +628,7 @@ open_wim_file(const tchar *filename, struct filedes *fd_ret)
 
 /*
  * Begins the reading of a WIM file; opens the file and reads its header and
- * lookup table, and optionally checks the integrity.
+ * blob table, and optionally checks the integrity.
  */
 static int
 begin_read(WIMStruct *wim, const void *wim_filename_or_fd, int open_flags)
@@ -726,8 +744,8 @@ begin_read(WIMStruct *wim, const void *wim_filename_or_fd, int open_flags)
        }
 
        if (open_flags & WIMLIB_OPEN_FLAG_FROM_PIPE) {
-               wim->lookup_table = new_lookup_table(9001);
-               if (!wim->lookup_table)
+               wim->blob_table = new_blob_table(9001);
+               if (!wim->blob_table)
                        return WIMLIB_ERR_NOMEM;
        } else {
 
@@ -743,7 +761,7 @@ begin_read(WIMStruct *wim, const void *wim_filename_or_fd, int open_flags)
                        return WIMLIB_ERR_IMAGE_COUNT;
                }
 
-               ret = read_wim_lookup_table(wim);
+               ret = read_blob_table(wim);
                if (ret)
                        return ret;
        }
@@ -805,27 +823,26 @@ wimlib_open_wim(const tchar *wimfile, int open_flags, WIMStruct **wim_ret)
                                             NULL, NULL);
 }
 
-/* Checksum all streams that are unhashed (other than the metadata streams),
- * merging them into the lookup table as needed.  This is a no-op unless the
- * library has previously used to add or mount an image using the same
- * WIMStruct. */
+/* Checksum all blobs that are unhashed (other than the metadata blobs), merging
+ * them into the blob table as needed.  This is a no-op unless files have been
+ * added to an image in the same WIMStruct.  */
 int
-wim_checksum_unhashed_streams(WIMStruct *wim)
+wim_checksum_unhashed_blobs(WIMStruct *wim)
 {
        int ret;
 
        if (!wim_has_metadata(wim))
                return 0;
        for (int i = 0; i < wim->hdr.image_count; i++) {
-               struct wim_lookup_table_entry *lte, *tmp;
+               struct blob_descriptor *blob, *tmp;
                struct wim_image_metadata *imd = wim->image_metadata[i];
-               image_for_each_unhashed_stream_safe(lte, tmp, imd) {
-                       struct wim_lookup_table_entry *new_lte;
-                       ret = hash_unhashed_stream(lte, wim->lookup_table, &new_lte);
+               image_for_each_unhashed_blob_safe(blob, tmp, imd) {
+                       struct blob_descriptor *new_blob;
+                       ret = hash_unhashed_blob(blob, wim->blob_table, &new_blob);
                        if (ret)
                                return ret;
-                       if (new_lte != lte)
-                               free_lookup_table_entry(lte);
+                       if (new_blob != blob)
+                               free_blob_descriptor(blob);
                }
        }
        return 0;
@@ -883,7 +900,7 @@ wimlib_free(WIMStruct *wim)
        if (filedes_valid(&wim->out_fd))
                filedes_close(&wim->out_fd);
 
-       free_lookup_table(wim->lookup_table);
+       free_blob_table(wim->blob_table);
 
        wimlib_free_decompressor(wim->decompressor);
 
@@ -916,30 +933,42 @@ test_locale_ctype_utf8(void)
 WIMLIBAPI u32
 wimlib_get_version(void)
 {
-       return WIMLIB_VERSION_CODE;
+       return (WIMLIB_MAJOR_VERSION << 20) |
+              (WIMLIB_MINOR_VERSION << 10) |
+               WIMLIB_PATCH_VERSION;
 }
 
 static bool lib_initialized = false;
+static pthread_mutex_t lib_initialization_mutex = PTHREAD_MUTEX_INITIALIZER;
 
 /* API function documented in wimlib.h  */
 WIMLIBAPI int
 wimlib_global_init(int init_flags)
 {
+       int ret;
+
        if (lib_initialized)
                return 0;
 
+       pthread_mutex_lock(&lib_initialization_mutex);
+
+       ret = 0;
+       if (lib_initialized)
+               goto out;
+
 #ifdef ENABLE_ERROR_MESSAGES
        if (!wimlib_error_file)
                wimlib_error_file = stderr;
 #endif
 
+       ret = WIMLIB_ERR_INVALID_PARAM;
        if (init_flags & ~(WIMLIB_INIT_FLAG_ASSUME_UTF8 |
                           WIMLIB_INIT_FLAG_DONT_ACQUIRE_PRIVILEGES |
                           WIMLIB_INIT_FLAG_STRICT_CAPTURE_PRIVILEGES |
                           WIMLIB_INIT_FLAG_STRICT_APPLY_PRIVILEGES |
                           WIMLIB_INIT_FLAG_DEFAULT_CASE_SENSITIVE |
                           WIMLIB_INIT_FLAG_DEFAULT_CASE_INSENSITIVE))
-               return WIMLIB_ERR_INVALID_PARAM;
+               goto out;
 
        libxml_global_init();
        if (!(init_flags & WIMLIB_INIT_FLAG_ASSUME_UTF8)) {
@@ -950,11 +979,9 @@ wimlib_global_init(int init_flags)
        #endif
        }
 #ifdef __WIN32__
-       {
-               int ret = win32_global_init(init_flags);
-               if (ret)
-                       return ret;
-       }
+       ret = win32_global_init(init_flags);
+       if (ret)
+               goto out;
 #endif
        iconv_global_init();
        init_upcase();
@@ -963,7 +990,10 @@ wimlib_global_init(int init_flags)
        else if (init_flags & WIMLIB_INIT_FLAG_DEFAULT_CASE_INSENSITIVE)
                default_ignore_case = true;
        lib_initialized = true;
-       return 0;
+       ret = 0;
+out:
+       pthread_mutex_unlock(&lib_initialization_mutex);
+       return ret;
 }
 
 /* API function documented in wimlib.h  */
@@ -972,6 +1002,12 @@ wimlib_global_cleanup(void)
 {
        if (!lib_initialized)
                return;
+
+       pthread_mutex_lock(&lib_initialization_mutex);
+
+       if (!lib_initialized)
+               goto out;
+
        libxml_global_cleanup();
        iconv_global_cleanup();
 #ifdef __WIN32__
@@ -980,4 +1016,7 @@ wimlib_global_cleanup(void)
 
        wimlib_set_error_file(NULL);
        lib_initialized = false;
+
+out:
+       pthread_mutex_unlock(&lib_initialization_mutex);
 }