]> wimlib.net Git - wimlib/blobdiff - src/wim.c
Heuristic sorting of streams for solid compression
[wimlib] / src / wim.c
index 6954dedaa3a0805f2590346f50db3213882b07e3..27708e235138a6ba282a759a713c2e33f9136968 100644 (file)
--- a/src/wim.c
+++ b/src/wim.c
@@ -5,27 +5,35 @@
 /*
  * Copyright (C) 2012, 2013, 2014 Eric Biggers
  *
- * This file is part of wimlib, a library for working with WIM files.
+ * 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
+ * Software Foundation; either version 3 of the License, or (at your option) any
+ * later version.
  *
- * wimlib is free software; you can redistribute it and/or modify it under the
- * terms of the GNU General Public License as published by the Free
- * Software Foundation; either version 3 of the License, or (at your option)
- * any later version.
- *
- * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
- * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
- * A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * This file is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  * details.
  *
- * You should have received a copy of the GNU General Public License
- * along with wimlib; if not, see http://www.gnu.org/licenses/.
+ * 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/.
  */
 
 #ifdef HAVE_CONFIG_H
 #  include "config.h"
 #endif
 
+#include <errno.h>
+#include <fcntl.h>
+#ifndef __WIN32__
+#  include <langinfo.h>
+#endif
+#include <stdlib.h>
+#include <unistd.h>
+
 #include "wimlib.h"
+#include "wimlib/assert.h"
+#include "wimlib/bitops.h"
 #include "wimlib/dentry.h"
 #include "wimlib/encoding.h"
 #include "wimlib/file_io.h"
 #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  */
@@ -77,9 +75,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;
 }
@@ -98,7 +96,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;
 
@@ -205,7 +203,6 @@ wimlib_create_new_wim(int ctype, WIMStruct **wim_ret)
                goto out_free_wim;
        }
        wim->lookup_table = table;
-       wim->refcnts_ok = 1;
        wim->compression_type = ctype;
        wim->out_compression_type = ctype;
        wim->chunk_size = wim->hdr.chunk_size;
@@ -346,25 +343,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_streams));
+               destroy_image_metadata(imd, NULL, false);
+       }
+       wim->current_image = WIMLIB_NO_IMAGE;
+}
 
 /* API function documented in wimlib.h  */
 WIMLIBAPI const tchar *
@@ -474,14 +478,16 @@ wimlib_get_wim_info(WIMStruct *wim, struct wimlib_wim_info *info)
 WIMLIBAPI int
 wimlib_set_wim_info(WIMStruct *wim, const struct wimlib_wim_info *info, int which)
 {
-       int ret;
-
        if (which & ~(WIMLIB_CHANGE_READONLY_FLAG |
                      WIMLIB_CHANGE_GUID |
                      WIMLIB_CHANGE_BOOT_INDEX |
                      WIMLIB_CHANGE_RPFIX_FLAG))
                return WIMLIB_ERR_INVALID_PARAM;
 
+       if ((which & WIMLIB_CHANGE_BOOT_INDEX) &&
+           info->boot_index > wim->hdr.image_count)
+               return WIMLIB_ERR_INVALID_IMAGE;
+
        if (which & WIMLIB_CHANGE_READONLY_FLAG) {
                if (info->is_marked_readonly)
                        wim->hdr.flags |= WIM_HDR_FLAG_READONLY;
@@ -489,21 +495,11 @@ wimlib_set_wim_info(WIMStruct *wim, const struct wimlib_wim_info *info, int whic
                        wim->hdr.flags &= ~WIM_HDR_FLAG_READONLY;
        }
 
-       if ((which & ~WIMLIB_CHANGE_READONLY_FLAG) == 0)
-               return 0;
-
-       ret = can_modify_wim(wim);
-       if (ret)
-               return ret;
-
        if (which & WIMLIB_CHANGE_GUID)
                memcpy(wim->hdr.guid, info->guid, WIM_GUID_LEN);
 
-       if (which & WIMLIB_CHANGE_BOOT_INDEX) {
-               if (info->boot_index > wim->hdr.image_count)
-                       return WIMLIB_ERR_INVALID_IMAGE;
+       if (which & WIMLIB_CHANGE_BOOT_INDEX)
                wim->hdr.boot_idx = info->boot_index;
-       }
 
        if (which & WIMLIB_CHANGE_RPFIX_FLAG) {
                if (info->has_rpfix)
@@ -546,13 +542,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;
 }
 
@@ -586,14 +582,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
@@ -874,33 +870,6 @@ can_modify_wim(WIMStruct *wim)
        return 0;
 }
 
-/*
- * can_delete_from_wim - Check if files or images can be deleted from a given
- * WIM file.
- *
- * This theoretically should be exactly the same as can_modify_wim(), but
- * unfortunately, due to bugs in Microsoft's software that generate incorrect
- * reference counts for some WIM resources, we need to run expensive
- * verifications to make sure the reference counts are correct on all WIM
- * resources.  Otherwise we might delete a WIM resource whose reference count
- * has fallen to 0, but is actually still referenced somewhere.
- */
-int
-can_delete_from_wim(WIMStruct *wim)
-{
-       int ret;
-
-       ret = can_modify_wim(wim);
-       if (ret)
-               return ret;
-       if (!wim->refcnts_ok) {
-               ret = wim_recalculate_refcnts(wim);
-               if (ret)
-                       return ret;
-       }
-       return 0;
-}
-
 /* API function documented in wimlib.h  */
 WIMLIBAPI void
 wimlib_free(WIMStruct *wim)
@@ -954,7 +923,9 @@ 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;