]> wimlib.net Git - wimlib/blobdiff - src/wim.c
mount_image.c: add fallback definitions of RENAME_* constants
[wimlib] / src / wim.c
index 79a9ba729e08d48b270b7dd538f946b092ee5cf5..3a2a216a4c0db0c682be476484deacca754f438e 100644 (file)
--- a/src/wim.c
+++ b/src/wim.c
@@ -3,7 +3,7 @@
  */
 
 /*
- * Copyright (C) 2012-2016 Eric Biggers
+ * Copyright 2012-2023 Eric Biggers
  *
  * This file is free software; you can redistribute it and/or modify it under
  * the terms of the GNU Lesser General Public License as published by the Free
@@ -16,7 +16,7 @@
  * details.
  *
  * You should have received a copy of the GNU Lesser General Public License
- * along with this file; if not, see http://www.gnu.org/licenses/.
+ * along with this file; if not, see https://www.gnu.org/licenses/.
  */
 
 #ifdef HAVE_CONFIG_H
 
 #include <errno.h>
 #include <fcntl.h>
-#ifndef __WIN32__
-#  include <langinfo.h>
-#endif
-#include <pthread.h>
 #include <stdlib.h>
+#include <sys/stat.h>
 #include <unistd.h>
 
 #include "wimlib.h"
 #include "wimlib/assert.h"
 #include "wimlib/blob_table.h"
+#include "wimlib/cpu_features.h"
 #include "wimlib/dentry.h"
 #include "wimlib/encoding.h"
 #include "wimlib/file_io.h"
 #include "wimlib/integrity.h"
 #include "wimlib/metadata.h"
-#include "wimlib/ntfs_3g.h" /* for libntfs3g_global_init() */
 #include "wimlib/security.h"
+#include "wimlib/threads.h"
 #include "wimlib/wim.h"
 #include "wimlib/xml.h"
 #include "wimlib/win32.h"
@@ -162,7 +160,7 @@ wimlib_create_new_wim(enum wimlib_compression_type ctype, WIMStruct **wim_ret)
        int ret;
        WIMStruct *wim;
 
-       ret = wimlib_global_init(WIMLIB_INIT_FLAG_ASSUME_UTF8);
+       ret = wimlib_global_init(0);
        if (ret)
                return ret;
 
@@ -176,31 +174,25 @@ wimlib_create_new_wim(enum wimlib_compression_type ctype, WIMStruct **wim_ret)
        if (!wim)
                return WIMLIB_ERR_NOMEM;
 
-       wim->xml_info = xml_new_info_struct();
-       wim->blob_table = new_blob_table(64);
-       if (!wim->xml_info || !wim->blob_table) {
-               wimlib_free(wim);
-               return WIMLIB_ERR_NOMEM;
-       }
-
-       /* Fill in wim->hdr with default values  */
+       /* Fill in wim->hdr with default values */
        wim->hdr.magic = WIM_MAGIC;
        wim->hdr.wim_version = WIM_VERSION_DEFAULT;
-       wim->hdr.flags = 0;
-       wim->hdr.chunk_size = 0;
-       generate_guid(wim->hdr.guid);
        wim->hdr.part_number = 1;
        wim->hdr.total_parts = 1;
-       wim->hdr.image_count = 0;
-       wim->hdr.boot_idx = 0;
-
        wim->compression_type = WIMLIB_COMPRESSION_TYPE_NONE;
-       wim->chunk_size = wim->hdr.chunk_size;
 
-       /* Set the output compression type  */
+       /* Set the output compression type */
        wim->out_compression_type = ctype;
        wim->out_chunk_size = wim_default_nonsolid_chunk_size(ctype);
 
+       /* Allocate an empty XML info and blob table */
+       wim->xml_info = xml_new_info_struct();
+       wim->blob_table = new_blob_table(64);
+       if (!wim->xml_info || !wim->blob_table) {
+               wimlib_free(wim);
+               return WIMLIB_ERR_NOMEM;
+       }
+
        *wim_ret = wim;
        return 0;
 }
@@ -653,11 +645,17 @@ begin_read(WIMStruct *wim, const void *wim_filename_or_fd, int open_flags)
                filedes_init(&wim->in_fd, *(const int*)wim_filename_or_fd);
                wim->in_fd.is_pipe = 1;
        } else {
+               struct stat stbuf;
+
                wimfile = wim_filename_or_fd;
                ret = open_wim_file(wimfile, &wim->in_fd);
                if (ret)
                        return ret;
 
+               /* The file size is needed for enforcing some limits later. */
+               if (fstat(wim->in_fd.fd, &stbuf) == 0)
+                       wim->file_size = stbuf.st_size;
+
                /* The absolute path to the WIM is requested so that
                 * wimlib_overwrite() still works even if the process changes
                 * its working directory.  This actually happens if a WIM is
@@ -790,7 +788,7 @@ open_wim_as_WIMStruct(const void *wim_filename_or_fd, int open_flags,
        WIMStruct *wim;
        int ret;
 
-       ret = wimlib_global_init(WIMLIB_INIT_FLAG_ASSUME_UTF8);
+       ret = wimlib_global_init(0);
        if (ret)
                return ret;
 
@@ -936,21 +934,6 @@ wimlib_free(WIMStruct *wim)
        wim_decrement_refcnt(wim);
 }
 
-static bool
-test_locale_ctype_utf8(void)
-{
-#ifdef __WIN32__
-       return false;
-#else
-       char *ctype = nl_langinfo(CODESET);
-
-       return (!strstr(ctype, "UTF-8") ||
-               !strstr(ctype, "UTF8") ||
-               !strstr(ctype, "utf8") ||
-               !strstr(ctype, "utf-8"));
-#endif
-}
-
 /* API function documented in wimlib.h  */
 WIMLIBAPI u32
 wimlib_get_version(void)
@@ -960,8 +943,14 @@ wimlib_get_version(void)
                WIMLIB_PATCH_VERSION;
 }
 
+WIMLIBAPI const tchar *
+wimlib_get_version_string(void)
+{
+       return T(PACKAGE_VERSION);
+}
+
 static bool lib_initialized = false;
-static pthread_mutex_t lib_initialization_mutex = PTHREAD_MUTEX_INITIALIZER;
+static struct mutex lib_initialization_mutex = MUTEX_INITIALIZER;
 
 /* API function documented in wimlib.h  */
 WIMLIBAPI int
@@ -972,15 +961,13 @@ wimlib_global_init(int init_flags)
        if (lib_initialized)
                goto out;
 
-       pthread_mutex_lock(&lib_initialization_mutex);
+       mutex_lock(&lib_initialization_mutex);
 
        if (lib_initialized)
                goto out_unlock;
 
-#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 |
@@ -998,20 +985,12 @@ wimlib_global_init(int init_flags)
                            WIMLIB_INIT_FLAG_DEFAULT_CASE_INSENSITIVE))
                goto out_unlock;
 
-       xml_global_init();
-       if (!(init_flags & WIMLIB_INIT_FLAG_ASSUME_UTF8)) {
-               wimlib_mbs_is_utf8 = test_locale_ctype_utf8();
-       #ifdef WITH_NTFS_3G
-               if (!wimlib_mbs_is_utf8)
-                       libntfs3g_global_init();
-       #endif
-       }
-#ifdef __WIN32__
+       init_cpu_features();
+#ifdef _WIN32
        ret = win32_global_init(init_flags);
        if (ret)
                goto out_unlock;
 #endif
-       iconv_global_init();
        init_upcase();
        if (init_flags & WIMLIB_INIT_FLAG_DEFAULT_CASE_SENSITIVE)
                default_ignore_case = false;
@@ -1020,7 +999,7 @@ wimlib_global_init(int init_flags)
        lib_initialized = true;
        ret = 0;
 out_unlock:
-       pthread_mutex_unlock(&lib_initialization_mutex);
+       mutex_unlock(&lib_initialization_mutex);
 out:
        return ret;
 }
@@ -1032,14 +1011,12 @@ wimlib_global_cleanup(void)
        if (!lib_initialized)
                return;
 
-       pthread_mutex_lock(&lib_initialization_mutex);
+       mutex_lock(&lib_initialization_mutex);
 
        if (!lib_initialized)
                goto out_unlock;
 
-       xml_global_cleanup();
-       iconv_global_cleanup();
-#ifdef __WIN32__
+#ifdef _WIN32
        win32_global_cleanup();
 #endif
 
@@ -1047,5 +1024,5 @@ wimlib_global_cleanup(void)
        lib_initialized = false;
 
 out_unlock:
-       pthread_mutex_unlock(&lib_initialization_mutex);
+       mutex_unlock(&lib_initialization_mutex);
 }