]> wimlib.net Git - wimlib/blobdiff - src/wim.c
Improve char encoding support (IN PROGRESS)
[wimlib] / src / wim.c
index 23858d69b03c538261c64fee25e0db49030442f6..6df2c71e79bdb9b39e349141be1f752d6c0b6029 100644 (file)
--- a/src/wim.c
+++ b/src/wim.c
@@ -3,7 +3,7 @@
  */
 
 /*
- * Copyright (C) 2012 Eric Biggers
+ * Copyright (C) 2012, 2013 Eric Biggers
  *
  * wimlib - Library for working with WIM files
  *
  */
 
 #include "config.h"
+
+#include <errno.h>
+#include <fcntl.h>
+#include <langinfo.h>
 #include <limits.h>
-#include <stdlib.h>
 #include <stdarg.h>
-#include <errno.h>
-
-#include "dentry.h"
+#include <stdlib.h>
 #include <unistd.h>
-#include <fcntl.h>
 
 #ifdef WITH_NTFS_3G
-#include <time.h>
-#include <ntfs-3g/volume.h>
+#  include <time.h>
+#  include <ntfs-3g/volume.h>
+#endif
+
+#ifdef __WIN32__
+#  include "win32.h"
 #endif
 
-#include "wimlib_internal.h"
 #include "buffer_io.h"
+#include "dentry.h"
 #include "lookup_table.h"
+#include "wimlib_internal.h"
 #include "xml.h"
 
 static int image_print_metadata(WIMStruct *w)
@@ -204,6 +209,7 @@ WIMLIBAPI int wimlib_get_num_images(const WIMStruct *w)
 int select_wim_image(WIMStruct *w, int image)
 {
        struct wim_image_metadata *imd;
+       int ret;
 
        DEBUG("Selecting image %d", image);
 
@@ -233,20 +239,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;
 }
 
 
@@ -446,6 +453,9 @@ static int begin_read(WIMStruct *w, const char *in_wim_path, int open_flags,
         *
         * This will break if the full path to the WIM changes in the
         * intervening time...
+        *
+        * Warning: in Windows native builds, realpath() calls the replacement
+        * function in win32.c.
         */
        w->filename = realpath(in_wim_path, NULL);
        if (!w->filename) {
@@ -597,7 +607,7 @@ void destroy_image_metadata(struct wim_image_metadata *imd,
  * closes all files associated with the WIMStruct.  */
 WIMLIBAPI void wimlib_free(WIMStruct *w)
 {
-       DEBUG2("Freeing WIMStruct");
+       DEBUG("Freeing WIMStruct");
 
        if (!w)
                return;
@@ -633,4 +643,26 @@ WIMLIBAPI void wimlib_free(WIMStruct *w)
        }
 #endif
        FREE(w);
+       DEBUG("Freed WIMStruct");
+}
+
+bool wimlib_mbs_is_utf8;
+
+/* Get global memory allocations out of the way.  Not strictly necessary in
+ * single-threaded programs like 'imagex'. */
+WIMLIBAPI int wimlib_global_init()
+{
+       char *encoding;
+
+       libxml_global_init();
+       wimlib_mbs_is_utf8 = (strcmp(nl_langinfo(CODESET), "UTF-8") == 0);
+       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();
 }