]> wimlib.net Git - wimlib/blobdiff - src/wim.c
select_wim_image(): Fix last commit
[wimlib] / src / wim.c
index c76be36f65176bd02967dd1ec390770a134ba9ce..af4580285e54593e719397eae1f4a29afd1bdcf8 100644 (file)
--- a/src/wim.c
+++ b/src/wim.c
@@ -1,9 +1,8 @@
 /*
- * wim.c
+ * wim.c - Stuff that doesn't fit into any other file
  */
 
 /*
- * Copyright (C) 2010 Carl Thijssen
  * Copyright (C) 2012 Eric Biggers
  *
  * wimlib - Library for working with WIM files
 #endif
 
 #include "wimlib_internal.h"
-#include "io.h"
+#include "buffer_io.h"
 #include "lookup_table.h"
 #include "xml.h"
 
-static int print_metadata(WIMStruct *w)
+static int image_print_metadata(WIMStruct *w)
 {
        DEBUG("Printing metadata for image %d", w->current_image);
        print_security_data(wim_security_data(w));
@@ -53,13 +52,13 @@ static int print_metadata(WIMStruct *w)
 }
 
 
-static int print_files(WIMStruct *w)
+static int image_print_files(WIMStruct *w)
 {
        return for_dentry_in_tree(wim_root_dentry(w), print_dentry_full_path,
                                  NULL);
 }
 
-WIMStruct *new_wim_struct()
+static WIMStruct *new_wim_struct()
 {
        WIMStruct *w = CALLOC(1, sizeof(WIMStruct));
 #ifdef WITH_FUSE
@@ -108,8 +107,8 @@ int for_image(WIMStruct *w, int image, int (*visitor)(WIMStruct *))
 
 static int sort_image_metadata_by_position(const void *p1, const void *p2)
 {
-       const struct image_metadata *imd1 = p1;
-       const struct image_metadata *imd2 = p2;
+       const struct wim_image_metadata *imd1 = p1;
+       const struct wim_image_metadata *imd2 = p2;
        u64 offset1 = imd1->metadata_lte->resource_entry.offset;
        u64 offset2 = imd2->metadata_lte->resource_entry.offset;
        if (offset1 < offset2)
@@ -124,7 +123,7 @@ static int sort_image_metadata_by_position(const void *p1, const void *p2)
  * If @lte points to a metadata resource, append it to the list of metadata
  * resources in the WIMStruct.  Otherwise, do nothing.
  */
-static int append_metadata_resource_entry(struct lookup_table_entry *lte,
+static int append_metadata_resource_entry(struct wim_lookup_table_entry *lte,
                                          void *wim_p)
 {
        WIMStruct *w = wim_p;
@@ -149,7 +148,7 @@ static int append_metadata_resource_entry(struct lookup_table_entry *lte,
 }
 
 /* Returns the compression type given in the flags of a WIM header. */
-int wim_hdr_flags_compression_type(int wim_hdr_flags)
+static int wim_hdr_flags_compression_type(int wim_hdr_flags)
 {
        if (wim_hdr_flags & WIM_HDR_FLAG_COMPRESSION) {
                if (wim_hdr_flags & WIM_HDR_FLAG_COMPRESS_LZX)
@@ -169,7 +168,7 @@ int wim_hdr_flags_compression_type(int wim_hdr_flags)
 WIMLIBAPI int wimlib_create_new_wim(int ctype, WIMStruct **w_ret)
 {
        WIMStruct *w;
-       struct lookup_table *table;
+       struct wim_lookup_table *table;
        int ret;
 
        DEBUG("Creating new WIM with %s compression.",
@@ -204,7 +203,8 @@ WIMLIBAPI int wimlib_get_num_images(const WIMStruct *w)
 
 int select_wim_image(WIMStruct *w, int image)
 {
-       struct image_metadata *imd;
+       struct wim_image_metadata *imd;
+       int ret;
 
        DEBUG("Selecting image %d", image);
 
@@ -234,20 +234,21 @@ int select_wim_image(WIMStruct *w, int image)
                        INIT_HLIST_HEAD(&imd->inode_list);
                }
        }
-
        w->current_image = image;
-       imd = wim_get_current_image_metadata(w);
-
+       imd = &w->image_metadata[image - 1];
        if (imd->root_dentry) {
-               return 0;
+               ret = 0;
        } else {
                #ifdef ENABLE_DEBUG
                DEBUG("Reading metadata resource specified by the following "
                      "lookup table entry:");
-               print_lookup_table_entry(imd->metadata_lte);
+               print_lookup_table_entry(imd->metadata_lte, stdout);
                #endif
-               return read_metadata_resource(w, imd);
+               ret = read_metadata_resource(w, imd);
+               if (ret)
+                       w->current_image = WIMLIB_NO_IMAGE;
        }
+       return ret;
 }
 
 
@@ -369,7 +370,7 @@ WIMLIBAPI int wimlib_print_metadata(WIMStruct *w, int image)
                ERROR("Select the first part of the split WIM to see the metadata.");
                return WIMLIB_ERR_SPLIT_UNSUPPORTED;
        }
-       return for_image(w, image, print_metadata);
+       return for_image(w, image, image_print_metadata);
 }
 
 WIMLIBAPI int wimlib_print_files(WIMStruct *w, int image)
@@ -380,7 +381,7 @@ WIMLIBAPI int wimlib_print_files(WIMStruct *w, int image)
                ERROR("Select the first part of the split WIM if you'd like to list the files.");
                return WIMLIB_ERR_SPLIT_UNSUPPORTED;
        }
-       return for_image(w, image, print_files);
+       return for_image(w, image, image_print_files);
 }
 
 /* Sets the index of the bootable image. */
@@ -496,7 +497,7 @@ static int begin_read(WIMStruct *w, const char *in_wim_path, int open_flags,
 
        if (w->hdr.image_count != 0) {
                w->image_metadata = CALLOC(w->hdr.image_count,
-                                          sizeof(struct image_metadata));
+                                          sizeof(struct wim_image_metadata));
 
                if (!w->image_metadata) {
                        ERROR("Failed to allocate memory for %u image metadata structures",
@@ -516,7 +517,7 @@ static int begin_read(WIMStruct *w, const char *in_wim_path, int open_flags,
                return ret;
 
        /* Make sure all the expected images were found.  (We already have
-        * returned false if *extra* images were found) */
+        * returned WIMLIB_ERR_IMAGE_COUNT if *extra* images were found) */
        if (w->current_image != w->hdr.image_count &&
            w->hdr.part_number == 1)
        {
@@ -530,7 +531,7 @@ static int begin_read(WIMStruct *w, const char *in_wim_path, int open_flags,
         * file, rather than their order in the lookup table, which is random
         * because of hashing. */
        qsort(w->image_metadata, w->current_image,
-             sizeof(struct image_metadata), sort_image_metadata_by_position);
+             sizeof(struct wim_image_metadata), sort_image_metadata_by_position);
 
        w->current_image = WIMLIB_NO_IMAGE;
 
@@ -580,6 +581,20 @@ WIMLIBAPI int wimlib_open_wim(const char *wim_file, int open_flags,
        return ret;
 }
 
+void destroy_image_metadata(struct wim_image_metadata *imd,
+                           struct wim_lookup_table *table)
+{
+       free_dentry_tree(imd->root_dentry, table);
+       free_security_data(imd->security_data);
+
+       /* Get rid of the lookup table entry for this image's metadata resource
+        * */
+       if (table) {
+               lookup_table_unlink(table, imd->metadata_lte);
+               free_lookup_table_entry(imd->metadata_lte);
+       }
+}
+
 /* Frees the memory for the WIMStruct, including all internal memory; also
  * closes all files associated with the WIMStruct.  */
 WIMLIBAPI void wimlib_free(WIMStruct *w)
@@ -621,3 +636,19 @@ WIMLIBAPI void wimlib_free(WIMStruct *w)
 #endif
        FREE(w);
 }
+
+/* Get global memory allocations out of the way.  Not strictly necessary in
+ * single-threaded programs like 'imagex'. */
+WIMLIBAPI int wimlib_global_init()
+{
+       libxml_global_init();
+       return iconv_global_init();
+}
+
+/* Free global memory allocations.  Not strictly necessary if the process using
+ * wimlib is just about to exit (as is the case for 'imagex'). */
+WIMLIBAPI void wimlib_global_cleanup()
+{
+       libxml_global_cleanup();
+       iconv_global_cleanup();
+}