2 * wim.c - High-level code dealing with WIMStructs and images.
6 * Copyright (C) 2012, 2013, 2014, 2015 Eric Biggers
8 * This file is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU Lesser General Public License as published by the Free
10 * Software Foundation; either version 3 of the License, or (at your option) any
13 * This file is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this file; if not, see http://www.gnu.org/licenses/.
29 # include <langinfo.h>
36 #include "wimlib/assert.h"
37 #include "wimlib/blob_table.h"
38 #include "wimlib/dentry.h"
39 #include "wimlib/encoding.h"
40 #include "wimlib/file_io.h"
41 #include "wimlib/integrity.h"
42 #include "wimlib/metadata.h"
43 #include "wimlib/ntfs_3g.h" /* for libntfs3g_global_init() */
44 #include "wimlib/security.h"
45 #include "wimlib/wim.h"
46 #include "wimlib/xml.h"
48 # include "wimlib/win32.h" /* for realpath() replacement */
51 /* Information about the available compression types for the WIM format. */
56 u32 default_nonsolid_chunk_size;
57 u32 default_solid_chunk_size;
58 } wim_ctype_info[] = {
59 [WIMLIB_COMPRESSION_TYPE_NONE] = {
63 .default_nonsolid_chunk_size = 0,
64 .default_solid_chunk_size = 0,
66 [WIMLIB_COMPRESSION_TYPE_XPRESS] = {
68 .min_chunk_size = 4096,
69 .max_chunk_size = 65536,
70 .default_nonsolid_chunk_size = 32768,
71 .default_solid_chunk_size = 32768,
73 [WIMLIB_COMPRESSION_TYPE_LZX] = {
75 .min_chunk_size = 32768,
76 .max_chunk_size = 2097152,
77 .default_nonsolid_chunk_size = 32768,
78 .default_solid_chunk_size = 32768,
80 [WIMLIB_COMPRESSION_TYPE_LZMS] = {
82 .min_chunk_size = 32768,
83 .max_chunk_size = 1073741824,
84 .default_nonsolid_chunk_size = 131072,
85 .default_solid_chunk_size = 67108864,
89 /* Is the specified compression type valid? */
91 wim_compression_type_valid(enum wimlib_compression_type ctype)
93 return (unsigned)ctype < ARRAY_LEN(wim_ctype_info) &&
94 wim_ctype_info[(unsigned)ctype].name != NULL;
97 /* Is the specified chunk size valid for the compression type? */
99 wim_chunk_size_valid(u32 chunk_size, enum wimlib_compression_type ctype)
101 if (!(chunk_size == 0 || is_power_of_2(chunk_size)))
104 return chunk_size >= wim_ctype_info[(unsigned)ctype].min_chunk_size &&
105 chunk_size <= wim_ctype_info[(unsigned)ctype].max_chunk_size;
108 /* Return the default chunk size to use for the specified compression type in
109 * non-solid resources. */
111 wim_default_nonsolid_chunk_size(enum wimlib_compression_type ctype)
113 return wim_ctype_info[(unsigned)ctype].default_nonsolid_chunk_size;
116 /* Return the default chunk size to use for the specified compression type in
117 * solid resources. */
119 wim_default_solid_chunk_size(enum wimlib_compression_type ctype)
121 return wim_ctype_info[(unsigned)ctype].default_solid_chunk_size;
124 /* Return the default compression type to use in solid resources. */
125 static enum wimlib_compression_type
126 wim_default_solid_compression_type(void)
128 return WIMLIB_COMPRESSION_TYPE_LZMS;
132 is_blob_in_solid_resource(struct blob_descriptor *blob, void *_ignore)
134 return blob->blob_location == BLOB_IN_WIM &&
135 (blob->rdesc->flags & WIM_RESHDR_FLAG_SOLID);
139 wim_has_solid_resources(const WIMStruct *wim)
141 return for_blob_in_table(wim->blob_table, is_blob_in_solid_resource, NULL);
147 WIMStruct *wim = CALLOC(1, sizeof(WIMStruct));
151 filedes_invalidate(&wim->in_fd);
152 filedes_invalidate(&wim->out_fd);
153 wim->out_solid_compression_type = wim_default_solid_compression_type();
154 wim->out_solid_chunk_size = wim_default_solid_chunk_size(
155 wim->out_solid_compression_type);
156 INIT_LIST_HEAD(&wim->subwims);
160 /* API function documented in wimlib.h */
162 wimlib_create_new_wim(enum wimlib_compression_type ctype, WIMStruct **wim_ret)
167 ret = wimlib_global_init(WIMLIB_INIT_FLAG_ASSUME_UTF8);
172 return WIMLIB_ERR_INVALID_PARAM;
174 if (!wim_compression_type_valid(ctype))
175 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
177 wim = new_wim_struct();
179 return WIMLIB_ERR_NOMEM;
181 wim->blob_table = new_blob_table(9001);
182 if (!wim->blob_table) {
184 return WIMLIB_ERR_NOMEM;
187 init_wim_header(&wim->hdr, ctype,
188 wim_default_nonsolid_chunk_size(ctype));
189 wim->compression_type = ctype;
190 wim->out_compression_type = ctype;
191 wim->chunk_size = wim->hdr.chunk_size;
192 wim->out_chunk_size = wim->hdr.chunk_size;
199 destroy_image_metadata(struct wim_image_metadata *imd,
200 struct blob_table *table,
201 bool free_metadata_blob_descriptor)
203 free_dentry_tree(imd->root_dentry, table);
204 imd->root_dentry = NULL;
205 free_wim_security_data(imd->security_data);
206 imd->security_data = NULL;
208 if (free_metadata_blob_descriptor) {
209 free_blob_descriptor(imd->metadata_blob);
210 imd->metadata_blob = NULL;
213 struct blob_descriptor *blob, *tmp;
214 list_for_each_entry_safe(blob, tmp, &imd->unhashed_blobs, unhashed_list)
215 free_blob_descriptor(blob);
217 INIT_LIST_HEAD(&imd->unhashed_blobs);
218 INIT_HLIST_HEAD(&imd->inode_list);
222 put_image_metadata(struct wim_image_metadata *imd, struct blob_table *table)
224 if (imd && --imd->refcnt == 0) {
225 destroy_image_metadata(imd, table, true);
230 /* Appends the specified image metadata structure to the array of image metadata
231 * for a WIM, and increments the image count. */
233 append_image_metadata(WIMStruct *wim, struct wim_image_metadata *imd)
235 struct wim_image_metadata **imd_array;
237 imd_array = REALLOC(wim->image_metadata,
238 sizeof(wim->image_metadata[0]) * (wim->hdr.image_count + 1));
241 return WIMLIB_ERR_NOMEM;
242 wim->image_metadata = imd_array;
243 imd_array[wim->hdr.image_count++] = imd;
247 struct wim_image_metadata *
248 new_image_metadata(void)
250 struct wim_image_metadata *imd;
252 imd = CALLOC(1, sizeof(*imd));
255 INIT_HLIST_HEAD(&imd->inode_list);
256 INIT_LIST_HEAD(&imd->unhashed_blobs);
261 static struct wim_image_metadata **
262 new_image_metadata_array(unsigned num_images)
264 struct wim_image_metadata **imd_array;
266 imd_array = CALLOC(num_images, sizeof(imd_array[0]));
270 for (unsigned i = 0; i < num_images; i++) {
271 imd_array[i] = new_image_metadata();
272 if (unlikely(!imd_array[i])) {
273 for (unsigned j = 0; j < i; j++)
274 put_image_metadata(imd_array[j], NULL);
284 * Load the metadata for the specified WIM image into memory and set it
285 * as the WIMStruct's currently selected image.
288 * The WIMStruct for the WIM.
290 * The 1-based index of the image in the WIM to select.
292 * On success, 0 will be returned, wim->current_image will be set to
293 * @image, and wim_get_current_image_metadata() can be used to retrieve
294 * metadata information for the image.
296 * On failure, WIMLIB_ERR_INVALID_IMAGE, WIMLIB_ERR_METADATA_NOT_FOUND,
297 * or another error code will be returned.
300 select_wim_image(WIMStruct *wim, int image)
302 struct wim_image_metadata *imd;
305 if (image == WIMLIB_NO_IMAGE)
306 return WIMLIB_ERR_INVALID_IMAGE;
308 if (image == wim->current_image)
311 if (image < 1 || image > wim->hdr.image_count)
312 return WIMLIB_ERR_INVALID_IMAGE;
314 if (!wim_has_metadata(wim))
315 return WIMLIB_ERR_METADATA_NOT_FOUND;
317 /* If a valid image is currently selected, its metadata can be freed if
318 * it has not been modified. */
319 deselect_current_wim_image(wim);
320 wim->current_image = image;
321 imd = wim_get_current_image_metadata(wim);
322 if (imd->root_dentry || imd->modified) {
325 ret = read_metadata_resource(imd);
327 wim->current_image = WIMLIB_NO_IMAGE;
333 deselect_current_wim_image(WIMStruct *wim)
335 struct wim_image_metadata *imd;
336 if (wim->current_image == WIMLIB_NO_IMAGE)
338 imd = wim_get_current_image_metadata(wim);
339 if (!imd->modified) {
340 wimlib_assert(list_empty(&imd->unhashed_blobs));
341 destroy_image_metadata(imd, NULL, false);
343 wim->current_image = WIMLIB_NO_IMAGE;
347 * Calls a function on images in the WIM. If @image is WIMLIB_ALL_IMAGES,
348 * @visitor is called on the WIM once for each image, with each image selected
349 * as the current image in turn. If @image is a certain image, @visitor is
350 * called on the WIM only once, with that image selected.
353 for_image(WIMStruct *wim, int image, int (*visitor)(WIMStruct *))
360 if (image == WIMLIB_ALL_IMAGES) {
362 end = wim->hdr.image_count;
363 } else if (image >= 1 && image <= wim->hdr.image_count) {
367 return WIMLIB_ERR_INVALID_IMAGE;
369 for (i = start; i <= end; i++) {
370 ret = select_wim_image(wim, i);
380 /* API function documented in wimlib.h */
382 wimlib_resolve_image(WIMStruct *wim, const tchar *image_name_or_num)
388 if (!image_name_or_num || !*image_name_or_num)
389 return WIMLIB_NO_IMAGE;
391 if (!tstrcasecmp(image_name_or_num, T("all"))
392 || !tstrcasecmp(image_name_or_num, T("*")))
393 return WIMLIB_ALL_IMAGES;
394 image = tstrtol(image_name_or_num, &p, 10);
395 if (p != image_name_or_num && *p == T('\0') && image > 0) {
396 if (image > wim->hdr.image_count)
397 return WIMLIB_NO_IMAGE;
400 for (i = 1; i <= wim->hdr.image_count; i++) {
401 if (!tstrcmp(image_name_or_num,
402 wimlib_get_image_name(wim, i)))
405 return WIMLIB_NO_IMAGE;
409 /* API function documented in wimlib.h */
411 wimlib_print_available_images(const WIMStruct *wim, int image)
417 if (image == WIMLIB_ALL_IMAGES) {
418 n = tprintf(T("Available Images:\n"));
420 last = wim->hdr.image_count;
421 } else if (image >= 1 && image <= wim->hdr.image_count) {
422 n = tprintf(T("Information for Image %d\n"), image);
426 tprintf(T("wimlib_print_available_images(): Invalid image %d"),
430 for (i = 0; i < n - 1; i++)
433 for (i = first; i <= last; i++)
434 print_image_info(wim->wim_info, i);
437 /* API function documented in wimlib.h */
439 wimlib_get_wim_info(WIMStruct *wim, struct wimlib_wim_info *info)
441 memset(info, 0, sizeof(struct wimlib_wim_info));
442 memcpy(info->guid, wim->hdr.guid, WIMLIB_GUID_LEN);
443 info->image_count = wim->hdr.image_count;
444 info->boot_index = wim->hdr.boot_idx;
445 info->wim_version = wim->hdr.wim_version;
446 info->chunk_size = wim->chunk_size;
447 info->part_number = wim->hdr.part_number;
448 info->total_parts = wim->hdr.total_parts;
449 info->compression_type = wim->compression_type;
450 info->total_bytes = wim_info_get_total_bytes(wim->wim_info);
451 info->has_integrity_table = wim_has_integrity_table(wim);
452 info->opened_from_file = (wim->filename != NULL);
453 info->is_readonly = (wim->hdr.flags & WIM_HDR_FLAG_READONLY) ||
454 (wim->hdr.total_parts != 1) ||
455 (wim->filename && taccess(wim->filename, W_OK));
456 info->has_rpfix = (wim->hdr.flags & WIM_HDR_FLAG_RP_FIX) != 0;
457 info->is_marked_readonly = (wim->hdr.flags & WIM_HDR_FLAG_READONLY) != 0;
458 info->write_in_progress = (wim->hdr.flags & WIM_HDR_FLAG_WRITE_IN_PROGRESS) != 0;
459 info->metadata_only = (wim->hdr.flags & WIM_HDR_FLAG_METADATA_ONLY) != 0;
460 info->resource_only = (wim->hdr.flags & WIM_HDR_FLAG_RESOURCE_ONLY) != 0;
461 info->spanned = (wim->hdr.flags & WIM_HDR_FLAG_SPANNED) != 0;
462 info->pipable = wim_is_pipable(wim);
466 /* API function documented in wimlib.h */
468 wimlib_set_wim_info(WIMStruct *wim, const struct wimlib_wim_info *info, int which)
470 if (which & ~(WIMLIB_CHANGE_READONLY_FLAG |
472 WIMLIB_CHANGE_BOOT_INDEX |
473 WIMLIB_CHANGE_RPFIX_FLAG))
474 return WIMLIB_ERR_INVALID_PARAM;
476 if ((which & WIMLIB_CHANGE_BOOT_INDEX) &&
477 info->boot_index > wim->hdr.image_count)
478 return WIMLIB_ERR_INVALID_IMAGE;
480 if (which & WIMLIB_CHANGE_READONLY_FLAG) {
481 if (info->is_marked_readonly)
482 wim->hdr.flags |= WIM_HDR_FLAG_READONLY;
484 wim->hdr.flags &= ~WIM_HDR_FLAG_READONLY;
487 if (which & WIMLIB_CHANGE_GUID)
488 memcpy(wim->hdr.guid, info->guid, WIM_GUID_LEN);
490 if (which & WIMLIB_CHANGE_BOOT_INDEX)
491 wim->hdr.boot_idx = info->boot_index;
493 if (which & WIMLIB_CHANGE_RPFIX_FLAG) {
495 wim->hdr.flags |= WIM_HDR_FLAG_RP_FIX;
497 wim->hdr.flags &= ~WIM_HDR_FLAG_RP_FIX;
502 /* API function documented in wimlib.h */
504 wimlib_set_output_compression_type(WIMStruct *wim,
505 enum wimlib_compression_type ctype)
507 if (!wim_compression_type_valid(ctype))
508 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
510 wim->out_compression_type = ctype;
512 /* Reset the chunk size if it's no longer valid. */
513 if (!wim_chunk_size_valid(wim->out_chunk_size, ctype))
514 wim->out_chunk_size = wim_default_nonsolid_chunk_size(ctype);
518 /* API function documented in wimlib.h */
520 wimlib_set_output_pack_compression_type(WIMStruct *wim,
521 enum wimlib_compression_type ctype)
523 if (!wim_compression_type_valid(ctype))
524 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
526 /* Solid resources can't be uncompressed. */
527 if (ctype == WIMLIB_COMPRESSION_TYPE_NONE)
528 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
530 wim->out_solid_compression_type = ctype;
532 /* Reset the chunk size if it's no longer valid. */
533 if (!wim_chunk_size_valid(wim->out_solid_chunk_size, ctype))
534 wim->out_solid_chunk_size = wim_default_solid_chunk_size(ctype);
538 /* API function documented in wimlib.h */
540 wimlib_set_output_chunk_size(WIMStruct *wim, u32 chunk_size)
542 if (chunk_size == 0) {
543 wim->out_chunk_size =
544 wim_default_nonsolid_chunk_size(wim->out_compression_type);
548 if (!wim_chunk_size_valid(chunk_size, wim->out_compression_type))
549 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
551 wim->out_chunk_size = chunk_size;
555 /* API function documented in wimlib.h */
557 wimlib_set_output_pack_chunk_size(WIMStruct *wim, u32 chunk_size)
559 if (chunk_size == 0) {
560 wim->out_solid_chunk_size =
561 wim_default_solid_chunk_size(wim->out_solid_compression_type);
565 if (!wim_chunk_size_valid(chunk_size, wim->out_solid_compression_type))
566 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
568 wim->out_solid_chunk_size = chunk_size;
572 /* API function documented in wimlib.h */
573 WIMLIBAPI const tchar *
574 wimlib_get_compression_type_string(enum wimlib_compression_type ctype)
576 if (!wim_compression_type_valid(ctype))
579 return wim_ctype_info[(unsigned)ctype].name;
583 wimlib_register_progress_function(WIMStruct *wim,
584 wimlib_progress_func_t progfunc,
587 wim->progfunc = progfunc;
588 wim->progctx = progctx;
592 open_wim_file(const tchar *filename, struct filedes *fd_ret)
596 raw_fd = topen(filename, O_RDONLY | O_BINARY);
598 ERROR_WITH_ERRNO("Can't open \"%"TS"\" read-only", filename);
599 return WIMLIB_ERR_OPEN;
601 filedes_init(fd_ret, raw_fd);
606 * Begins the reading of a WIM file; opens the file and reads its header and
607 * blob table, and optionally checks the integrity.
610 begin_read(WIMStruct *wim, const void *wim_filename_or_fd, int open_flags)
614 const tchar *wimfile;
616 if (open_flags & WIMLIB_OPEN_FLAG_FROM_PIPE) {
618 filedes_init(&wim->in_fd, *(const int*)wim_filename_or_fd);
619 wim->in_fd.is_pipe = 1;
621 wimfile = wim_filename_or_fd;
622 ret = open_wim_file(wimfile, &wim->in_fd);
626 /* The absolute path to the WIM is requested so that
627 * wimlib_overwrite() still works even if the process changes
628 * its working directory. This actually happens if a WIM is
629 * mounted read-write, since the FUSE thread changes directory
630 * to "/", and it needs to be able to find the WIM file again.
632 * This will break if the full path to the WIM changes in the
633 * intervening time...
635 * Warning: in Windows native builds, realpath() calls the
636 * replacement function in win32_replacements.c.
638 wim->filename = realpath(wimfile, NULL);
639 if (!wim->filename) {
640 ERROR_WITH_ERRNO("Failed to get full path to file "
641 "\"%"TS"\"", wimfile);
643 return WIMLIB_ERR_NOMEM;
645 return WIMLIB_ERR_NO_FILENAME;
649 ret = read_wim_header(wim, &wim->hdr);
653 if (wim->hdr.flags & WIM_HDR_FLAG_WRITE_IN_PROGRESS) {
654 WARNING("The WIM_HDR_FLAG_WRITE_IN_PROGRESS flag is set in the header of\n"
655 " \"%"TS"\". It may be being changed by another process,\n"
656 " or a process may have crashed while writing the WIM.",
660 if (open_flags & WIMLIB_OPEN_FLAG_WRITE_ACCESS) {
661 ret = can_modify_wim(wim);
666 if ((open_flags & WIMLIB_OPEN_FLAG_ERROR_IF_SPLIT) &&
667 (wim->hdr.total_parts != 1))
668 return WIMLIB_ERR_IS_SPLIT_WIM;
670 /* If the boot index is invalid, print a warning and set it to 0 */
671 if (wim->hdr.boot_idx > wim->hdr.image_count) {
672 WARNING("Ignoring invalid boot index.");
673 wim->hdr.boot_idx = 0;
676 /* Check and cache the compression type */
677 if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESSION) {
678 if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESS_LZX) {
679 wim->compression_type = WIMLIB_COMPRESSION_TYPE_LZX;
680 } else if (wim->hdr.flags & (WIM_HDR_FLAG_COMPRESS_XPRESS |
681 WIM_HDR_FLAG_COMPRESS_XPRESS_2)) {
682 wim->compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
683 } else if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESS_LZMS) {
684 wim->compression_type = WIMLIB_COMPRESSION_TYPE_LZMS;
686 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
689 wim->compression_type = WIMLIB_COMPRESSION_TYPE_NONE;
691 wim->out_compression_type = wim->compression_type;
693 /* Check and cache the chunk size. */
694 wim->chunk_size = wim->hdr.chunk_size;
695 wim->out_chunk_size = wim->chunk_size;
696 if (!wim_chunk_size_valid(wim->chunk_size, wim->compression_type)) {
697 ERROR("Invalid chunk size (%"PRIu32" bytes) "
698 "for compression type %"TS"!", wim->chunk_size,
699 wimlib_get_compression_type_string(wim->compression_type));
700 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
703 if (open_flags & WIMLIB_OPEN_FLAG_CHECK_INTEGRITY) {
704 ret = check_wim_integrity(wim);
705 if (ret == WIM_INTEGRITY_NONEXISTENT) {
706 WARNING("\"%"TS"\" does not contain integrity "
707 "information. Skipping integrity check.",
709 } else if (ret == WIM_INTEGRITY_NOT_OK) {
710 return WIMLIB_ERR_INTEGRITY;
711 } else if (ret != WIM_INTEGRITY_OK) {
716 if (wim->hdr.image_count != 0 && wim->hdr.part_number == 1) {
717 wim->image_metadata = new_image_metadata_array(wim->hdr.image_count);
718 if (!wim->image_metadata)
719 return WIMLIB_ERR_NOMEM;
722 if (open_flags & WIMLIB_OPEN_FLAG_FROM_PIPE) {
723 wim->blob_table = new_blob_table(9001);
724 if (!wim->blob_table)
725 return WIMLIB_ERR_NOMEM;
728 ret = read_wim_xml_data(wim);
732 xml_num_images = wim_info_get_num_images(wim->wim_info);
733 if (xml_num_images != wim->hdr.image_count) {
734 ERROR("The WIM's header is inconsistent with its XML data.\n"
735 " Please submit a bug report if you believe this "
736 "WIM file should be considered valid.");
737 return WIMLIB_ERR_IMAGE_COUNT;
740 ret = read_blob_table(wim);
748 open_wim_as_WIMStruct(const void *wim_filename_or_fd, int open_flags,
750 wimlib_progress_func_t progfunc, void *progctx)
755 ret = wimlib_global_init(WIMLIB_INIT_FLAG_ASSUME_UTF8);
759 wim = new_wim_struct();
761 return WIMLIB_ERR_NOMEM;
763 wim->progfunc = progfunc;
764 wim->progctx = progctx;
766 ret = begin_read(wim, wim_filename_or_fd, open_flags);
776 /* API function documented in wimlib.h */
778 wimlib_open_wim_with_progress(const tchar *wimfile, int open_flags,
780 wimlib_progress_func_t progfunc, void *progctx)
782 if (open_flags & ~(WIMLIB_OPEN_FLAG_CHECK_INTEGRITY |
783 WIMLIB_OPEN_FLAG_ERROR_IF_SPLIT |
784 WIMLIB_OPEN_FLAG_WRITE_ACCESS))
785 return WIMLIB_ERR_INVALID_PARAM;
787 if (!wimfile || !*wimfile || !wim_ret)
788 return WIMLIB_ERR_INVALID_PARAM;
790 return open_wim_as_WIMStruct(wimfile, open_flags, wim_ret,
794 /* API function documented in wimlib.h */
796 wimlib_open_wim(const tchar *wimfile, int open_flags, WIMStruct **wim_ret)
798 return wimlib_open_wim_with_progress(wimfile, open_flags, wim_ret,
802 /* Checksum all blobs that are unhashed (other than the metadata blobs), merging
803 * them into the blob table as needed. This is a no-op unless files have been
804 * added to an image in the same WIMStruct. */
806 wim_checksum_unhashed_blobs(WIMStruct *wim)
810 if (!wim_has_metadata(wim))
812 for (int i = 0; i < wim->hdr.image_count; i++) {
813 struct blob_descriptor *blob, *tmp;
814 struct wim_image_metadata *imd = wim->image_metadata[i];
815 image_for_each_unhashed_blob_safe(blob, tmp, imd) {
816 struct blob_descriptor *new_blob;
817 ret = hash_unhashed_blob(blob, wim->blob_table, &new_blob);
820 if (new_blob != blob)
821 free_blob_descriptor(blob);
828 * can_modify_wim - Check if a given WIM is writeable. This is only the case if
829 * it meets the following three conditions:
831 * 1. Write access is allowed to the underlying file (if any) at the filesystem level.
832 * 2. The WIM is not part of a spanned set.
833 * 3. The WIM_HDR_FLAG_READONLY flag is not set in the WIM header.
835 * Return value is 0 if writable; WIMLIB_ERR_WIM_IS_READONLY otherwise.
838 can_modify_wim(WIMStruct *wim)
841 if (taccess(wim->filename, W_OK)) {
842 ERROR_WITH_ERRNO("Can't modify \"%"TS"\"", wim->filename);
843 return WIMLIB_ERR_WIM_IS_READONLY;
846 if (wim->hdr.total_parts != 1) {
847 ERROR("Cannot modify \"%"TS"\": is part of a split WIM",
849 return WIMLIB_ERR_WIM_IS_READONLY;
851 if (wim->hdr.flags & WIM_HDR_FLAG_READONLY) {
852 ERROR("Cannot modify \"%"TS"\": is marked read-only",
854 return WIMLIB_ERR_WIM_IS_READONLY;
859 /* API function documented in wimlib.h */
861 wimlib_free(WIMStruct *wim)
866 while (!list_empty(&wim->subwims)) {
869 subwim = list_entry(wim->subwims.next, WIMStruct, subwim_node);
870 list_del(&subwim->subwim_node);
874 if (filedes_valid(&wim->in_fd))
875 filedes_close(&wim->in_fd);
876 if (filedes_valid(&wim->out_fd))
877 filedes_close(&wim->out_fd);
879 free_blob_table(wim->blob_table);
881 wimlib_free_decompressor(wim->decompressor);
884 free_wim_info(wim->wim_info);
885 if (wim->image_metadata) {
886 for (unsigned i = 0; i < wim->hdr.image_count; i++)
887 put_image_metadata(wim->image_metadata[i], NULL);
888 FREE(wim->image_metadata);
894 test_locale_ctype_utf8(void)
899 char *ctype = nl_langinfo(CODESET);
901 return (!strstr(ctype, "UTF-8") ||
902 !strstr(ctype, "UTF8") ||
903 !strstr(ctype, "utf8") ||
904 !strstr(ctype, "utf-8"));
908 /* API function documented in wimlib.h */
910 wimlib_get_version(void)
912 return (WIMLIB_MAJOR_VERSION << 20) |
913 (WIMLIB_MINOR_VERSION << 10) |
914 WIMLIB_PATCH_VERSION;
917 static bool lib_initialized = false;
918 static pthread_mutex_t lib_initialization_mutex = PTHREAD_MUTEX_INITIALIZER;
920 /* API function documented in wimlib.h */
922 wimlib_global_init(int init_flags)
929 pthread_mutex_lock(&lib_initialization_mutex);
934 #ifdef ENABLE_ERROR_MESSAGES
935 if (!wimlib_error_file)
936 wimlib_error_file = stderr;
939 ret = WIMLIB_ERR_INVALID_PARAM;
940 if (init_flags & ~(WIMLIB_INIT_FLAG_ASSUME_UTF8 |
941 WIMLIB_INIT_FLAG_DONT_ACQUIRE_PRIVILEGES |
942 WIMLIB_INIT_FLAG_STRICT_CAPTURE_PRIVILEGES |
943 WIMLIB_INIT_FLAG_STRICT_APPLY_PRIVILEGES |
944 WIMLIB_INIT_FLAG_DEFAULT_CASE_SENSITIVE |
945 WIMLIB_INIT_FLAG_DEFAULT_CASE_INSENSITIVE))
948 ret = WIMLIB_ERR_INVALID_PARAM;
949 if ((init_flags & (WIMLIB_INIT_FLAG_DEFAULT_CASE_SENSITIVE |
950 WIMLIB_INIT_FLAG_DEFAULT_CASE_INSENSITIVE))
951 == (WIMLIB_INIT_FLAG_DEFAULT_CASE_SENSITIVE |
952 WIMLIB_INIT_FLAG_DEFAULT_CASE_INSENSITIVE))
955 libxml_global_init();
956 if (!(init_flags & WIMLIB_INIT_FLAG_ASSUME_UTF8)) {
957 wimlib_mbs_is_utf8 = test_locale_ctype_utf8();
959 if (!wimlib_mbs_is_utf8)
960 libntfs3g_global_init();
964 ret = win32_global_init(init_flags);
970 if (init_flags & WIMLIB_INIT_FLAG_DEFAULT_CASE_SENSITIVE)
971 default_ignore_case = false;
972 else if (init_flags & WIMLIB_INIT_FLAG_DEFAULT_CASE_INSENSITIVE)
973 default_ignore_case = true;
974 lib_initialized = true;
977 pthread_mutex_unlock(&lib_initialization_mutex);
982 /* API function documented in wimlib.h */
984 wimlib_global_cleanup(void)
986 if (!lib_initialized)
989 pthread_mutex_lock(&lib_initialization_mutex);
991 if (!lib_initialized)
994 libxml_global_cleanup();
995 iconv_global_cleanup();
997 win32_global_cleanup();
1000 wimlib_set_error_file(NULL);
1001 lib_initialized = false;
1004 pthread_mutex_unlock(&lib_initialization_mutex);