2 * wim.c - Stuff that doesn't fit into any other file
6 * Copyright (C) 2012, 2013 Eric Biggers
8 * This file is part of wimlib, a library for working with WIM files.
10 * wimlib is free software; you can redistribute it and/or modify it under the
11 * terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 3 of the License, or (at your option)
15 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
16 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 * A PARTICULAR PURPOSE. See the GNU General Public License for more
20 * You should have received a copy of the GNU General Public License
21 * along with wimlib; if not, see http://www.gnu.org/licenses/.
28 #include "wimlib/error.h"
29 #include "wimlib/dentry.h"
30 #include "wimlib/encoding.h"
31 #include "wimlib/file_io.h"
32 #include "wimlib/integrity.h"
33 #include "wimlib/lookup_table.h"
34 #include "wimlib/metadata.h"
36 # include "wimlib/ntfs_3g.h" /* for do_ntfs_umount() */
38 #include "wimlib/security.h"
39 #include "wimlib/wim.h"
40 #include "wimlib/xml.h"
43 # include "wimlib/win32.h" /* for realpath() replacement */
49 # include <langinfo.h>
57 image_print_metadata(WIMStruct *wim)
59 DEBUG("Printing metadata for image %d", wim->current_image);
60 print_wim_security_data(wim_security_data(wim));
61 return for_dentry_in_tree(wim_root_dentry(wim), print_dentry,
69 WIMStruct *wim = CALLOC(1, sizeof(WIMStruct));
73 INIT_LIST_HEAD(&wim->subwims);
79 wim_chunk_size_valid(u32 chunk_size, int ctype)
83 /* Chunk size is meaningless for uncompressed WIMs --- any value is
85 if (ctype == WIMLIB_COMPRESSION_TYPE_NONE)
88 /* Chunk size must be power of 2. */
91 order = bsr32(chunk_size);
92 if (chunk_size != 1U << order)
111 case WIMLIB_COMPRESSION_TYPE_LZX:
112 /* TODO: Allow other chunk sizes when supported by the LZX
113 * compressor and decompressor. */
116 case WIMLIB_COMPRESSION_TYPE_XPRESS:
117 /* WIMGAPI (Windows 7) didn't seem to support XPRESS chunk size
118 * below 32768 bytes, but larger power-of-two sizes appear to be
119 * supported. 67108864 was the largest size that worked.
120 * (Note, however, that the offsets of XPRESS matches are still
121 * limited to 65535 bytes even when a much larger chunk size is
123 return order >= 15 && order <= 26;
129 wim_default_chunk_size(int ctype)
135 * Calls a function on images in the WIM. If @image is WIMLIB_ALL_IMAGES, @visitor
136 * is called on the WIM once for each image, with each image selected as the
137 * current image in turn. If @image is a certain image, @visitor is called on
138 * the WIM only once, with that image selected.
141 for_image(WIMStruct *wim, int image, int (*visitor)(WIMStruct *))
148 if (image == WIMLIB_ALL_IMAGES) {
150 end = wim->hdr.image_count;
151 } else if (image >= 1 && image <= wim->hdr.image_count) {
155 return WIMLIB_ERR_INVALID_IMAGE;
157 for (i = start; i <= end; i++) {
158 ret = select_wim_image(wim, i);
168 /* API function documented in wimlib.h */
170 wimlib_create_new_wim(int ctype, WIMStruct **wim_ret)
173 struct wim_lookup_table *table;
176 wimlib_global_init(WIMLIB_INIT_FLAG_ASSUME_UTF8);
178 DEBUG("Creating new WIM with %"TS" compression.",
179 wimlib_get_compression_type_string(ctype));
181 /* Allocate the WIMStruct. */
182 wim = new_wim_struct();
184 return WIMLIB_ERR_NOMEM;
186 ret = init_wim_header(&wim->hdr, ctype, wim_default_chunk_size(ctype));
190 table = new_lookup_table(9001);
192 ret = WIMLIB_ERR_NOMEM;
195 wim->lookup_table = table;
197 wim->compression_type = ctype;
198 wim->out_compression_type = ctype;
199 wim->chunk_size = wim->hdr.chunk_size;
200 wim->out_chunk_size = wim->hdr.chunk_size;
209 select_wim_image(WIMStruct *wim, int image)
211 struct wim_image_metadata *imd;
214 DEBUG("Selecting image %d", image);
216 if (image == WIMLIB_NO_IMAGE) {
217 ERROR("Invalid image: %d", WIMLIB_NO_IMAGE);
218 return WIMLIB_ERR_INVALID_IMAGE;
221 if (image == wim->current_image)
224 if (image < 1 || image > wim->hdr.image_count) {
225 ERROR("Cannot select image %d: There are only %u images",
226 image, wim->hdr.image_count);
227 return WIMLIB_ERR_INVALID_IMAGE;
230 if (!wim_has_metadata(wim)) {
231 ERROR("\"%"TS"\" does not contain metadata resources!", wim->filename);
232 if (wim->hdr.part_number != 1)
233 ERROR("Specify the first part of the split WIM instead.");
234 return WIMLIB_ERR_METADATA_NOT_FOUND;
237 /* If a valid image is currently selected, it can be freed if it is not
239 if (wim->current_image != WIMLIB_NO_IMAGE) {
240 imd = wim_get_current_image_metadata(wim);
241 if (!imd->modified) {
242 wimlib_assert(list_empty(&imd->unhashed_streams));
243 DEBUG("Freeing image %u", wim->current_image);
244 destroy_image_metadata(imd, NULL, false);
247 wim->current_image = image;
248 imd = wim_get_current_image_metadata(wim);
249 if (imd->root_dentry || imd->modified) {
253 DEBUG("Reading metadata resource specified by the following "
254 "lookup table entry:");
255 print_lookup_table_entry(imd->metadata_lte, stderr);
257 ret = read_metadata_resource(wim, imd);
259 wim->current_image = WIMLIB_NO_IMAGE;
265 /* API function documented in wimlib.h */
266 WIMLIBAPI const tchar *
267 wimlib_get_compression_type_string(int ctype)
270 case WIMLIB_COMPRESSION_TYPE_NONE:
272 case WIMLIB_COMPRESSION_TYPE_LZX:
274 case WIMLIB_COMPRESSION_TYPE_XPRESS:
281 /* API function documented in wimlib.h */
283 wimlib_resolve_image(WIMStruct *wim, const tchar *image_name_or_num)
289 if (!image_name_or_num || !*image_name_or_num)
290 return WIMLIB_NO_IMAGE;
292 if (!tstrcasecmp(image_name_or_num, T("all"))
293 || !tstrcasecmp(image_name_or_num, T("*")))
294 return WIMLIB_ALL_IMAGES;
295 image = tstrtol(image_name_or_num, &p, 10);
296 if (p != image_name_or_num && *p == T('\0') && image > 0) {
297 if (image > wim->hdr.image_count)
298 return WIMLIB_NO_IMAGE;
301 for (i = 1; i <= wim->hdr.image_count; i++) {
302 if (!tstrcmp(image_name_or_num,
303 wimlib_get_image_name(wim, i)))
306 return WIMLIB_NO_IMAGE;
310 /* API function documented in wimlib.h */
312 wimlib_print_available_images(const WIMStruct *wim, int image)
318 if (image == WIMLIB_ALL_IMAGES) {
319 n = tprintf(T("Available Images:\n"));
321 last = wim->hdr.image_count;
322 } else if (image >= 1 && image <= wim->hdr.image_count) {
323 n = tprintf(T("Information for Image %d\n"), image);
327 tprintf(T("wimlib_print_available_images(): Invalid image %d"),
331 for (i = 0; i < n - 1; i++)
334 for (i = first; i <= last; i++)
335 print_image_info(wim->wim_info, i);
339 /* API function documented in wimlib.h */
341 wimlib_print_metadata(WIMStruct *wim, int image)
343 return for_image(wim, image, image_print_metadata);
346 /* API function documented in wimlib.h */
348 wimlib_get_wim_info(WIMStruct *wim, struct wimlib_wim_info *info)
350 memset(info, 0, sizeof(struct wimlib_wim_info));
351 memcpy(info->guid, wim->hdr.guid, WIMLIB_GUID_LEN);
352 info->image_count = wim->hdr.image_count;
353 info->boot_index = wim->hdr.boot_idx;
354 info->wim_version = WIM_VERSION;
355 info->chunk_size = wim->hdr.chunk_size;
356 info->part_number = wim->hdr.part_number;
357 info->total_parts = wim->hdr.total_parts;
358 info->compression_type = wim->compression_type;
359 info->total_bytes = wim_info_get_total_bytes(wim->wim_info);
360 info->has_integrity_table = wim_has_integrity_table(wim);
361 info->opened_from_file = (wim->filename != NULL);
362 info->is_readonly = (wim->hdr.flags & WIM_HDR_FLAG_READONLY) ||
363 (wim->hdr.total_parts != 1) ||
364 (wim->filename && taccess(wim->filename, W_OK));
365 info->has_rpfix = (wim->hdr.flags & WIM_HDR_FLAG_RP_FIX) != 0;
366 info->is_marked_readonly = (wim->hdr.flags & WIM_HDR_FLAG_READONLY) != 0;
367 info->write_in_progress = (wim->hdr.flags & WIM_HDR_FLAG_WRITE_IN_PROGRESS) != 0;
368 info->metadata_only = (wim->hdr.flags & WIM_HDR_FLAG_METADATA_ONLY) != 0;
369 info->resource_only = (wim->hdr.flags & WIM_HDR_FLAG_RESOURCE_ONLY) != 0;
370 info->spanned = (wim->hdr.flags & WIM_HDR_FLAG_SPANNED) != 0;
371 info->pipable = wim_is_pipable(wim);
375 /* API function documented in wimlib.h */
377 wimlib_set_wim_info(WIMStruct *wim, const struct wimlib_wim_info *info, int which)
381 if (which & WIMLIB_CHANGE_READONLY_FLAG) {
382 if (info->is_marked_readonly)
383 wim->hdr.flags |= WIM_HDR_FLAG_READONLY;
385 wim->hdr.flags &= ~WIM_HDR_FLAG_READONLY;
388 if ((which & ~WIMLIB_CHANGE_READONLY_FLAG) == 0)
391 ret = can_modify_wim(wim);
395 if (which & WIMLIB_CHANGE_GUID) {
396 memcpy(wim->hdr.guid, info->guid, WIM_GID_LEN);
397 wim->guid_set_explicitly = 1;
400 if (which & WIMLIB_CHANGE_BOOT_INDEX) {
401 if (info->boot_index > wim->hdr.image_count) {
402 ERROR("%u is not 0 or a valid image in the WIM to mark as bootable",
404 return WIMLIB_ERR_INVALID_IMAGE;
406 wim->hdr.boot_idx = info->boot_index;
409 if (which & WIMLIB_CHANGE_RPFIX_FLAG) {
411 wim->hdr.flags |= WIM_HDR_FLAG_RP_FIX;
413 wim->hdr.flags &= ~WIM_HDR_FLAG_RP_FIX;
418 /* API function documented in wimlib.h */
420 wimlib_set_output_compression_type(WIMStruct *wim, int ctype)
423 case WIMLIB_COMPRESSION_TYPE_INVALID:
425 case WIMLIB_COMPRESSION_TYPE_NONE:
426 case WIMLIB_COMPRESSION_TYPE_LZX:
427 case WIMLIB_COMPRESSION_TYPE_XPRESS:
428 wim->out_compression_type = ctype;
430 /* Reset the chunk size if it's no longer valid. */
431 if (!wim_chunk_size_valid(wim->out_chunk_size,
432 wim->out_compression_type))
433 wim->out_chunk_size = wim_default_chunk_size(wim->out_compression_type);
436 return WIMLIB_ERR_INVALID_PARAM;
439 /* API function documented in wimlib.h */
441 wimlib_set_output_chunk_size(WIMStruct *wim, uint32_t chunk_size)
443 if (!wim_chunk_size_valid(chunk_size, wim->out_compression_type)) {
444 ERROR("Invalid chunk size (%"PRIu32" bytes) "
445 "for compression type %"TS"!",
447 wimlib_get_compression_type_string(wim->out_compression_type));
448 switch (wim->out_compression_type) {
449 case WIMLIB_COMPRESSION_TYPE_XPRESS:
450 ERROR("Valid chunk sizes for XPRESS are 32768, 65536, 131072, ..., 67108864.");
452 case WIMLIB_COMPRESSION_TYPE_LZX:
453 ERROR("Valid chunk sizes for XPRESS are 65536.");
456 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
458 wim->out_chunk_size = chunk_size;
463 do_open_wim(const tchar *filename, struct filedes *fd_ret)
467 raw_fd = topen(filename, O_RDONLY | O_BINARY);
469 ERROR_WITH_ERRNO("Can't open \"%"TS"\" read-only", filename);
470 return WIMLIB_ERR_OPEN;
472 filedes_init(fd_ret, raw_fd);
477 reopen_wim(WIMStruct *wim)
479 wimlib_assert(!filedes_valid(&wim->in_fd));
480 return do_open_wim(wim->filename, &wim->in_fd);
484 close_wim(WIMStruct *wim)
486 if (filedes_valid(&wim->in_fd)) {
487 filedes_close(&wim->in_fd);
488 filedes_invalidate(&wim->in_fd);
494 * Begins the reading of a WIM file; opens the file and reads its header and
495 * lookup table, and optionally checks the integrity.
498 begin_read(WIMStruct *wim, const void *wim_filename_or_fd,
499 int open_flags, wimlib_progress_func_t progress_func)
503 const tchar *wimfile;
505 if (open_flags & WIMLIB_OPEN_FLAG_FROM_PIPE) {
507 filedes_init(&wim->in_fd, *(const int*)wim_filename_or_fd);
508 wim->in_fd.is_pipe = 1;
510 wimfile = wim_filename_or_fd;
511 DEBUG("Reading the WIM file `%"TS"'", wimfile);
512 ret = do_open_wim(wimfile, &wim->in_fd);
516 /* The absolute path to the WIM is requested so that
517 * wimlib_overwrite() still works even if the process changes
518 * its working directory. This actually happens if a WIM is
519 * mounted read-write, since the FUSE thread changes directory
520 * to "/", and it needs to be able to find the WIM file again.
522 * This will break if the full path to the WIM changes in the
523 * intervening time...
525 * Warning: in Windows native builds, realpath() calls the
526 * replacement function in win32_replacements.c.
528 wim->filename = realpath(wimfile, NULL);
529 if (!wim->filename) {
530 ERROR_WITH_ERRNO("Failed to resolve WIM filename");
532 return WIMLIB_ERR_NOMEM;
534 return WIMLIB_ERR_OPEN;
538 ret = read_wim_header(wim->filename, &wim->in_fd, &wim->hdr);
542 if (wim->hdr.flags & WIM_HDR_FLAG_WRITE_IN_PROGRESS) {
543 WARNING("The WIM_HDR_FLAG_WRITE_IN_PROGRESS is set in the header of \"%"TS"\".\n"
544 " It may be being changed by another process, or a process\n"
545 " may have crashed while writing the WIM.", wimfile);
548 if (open_flags & WIMLIB_OPEN_FLAG_WRITE_ACCESS) {
549 ret = can_modify_wim(wim);
554 if ((open_flags & WIMLIB_OPEN_FLAG_ERROR_IF_SPLIT) &&
555 (wim->hdr.total_parts != 1))
556 return WIMLIB_ERR_IS_SPLIT_WIM;
558 DEBUG("According to header, WIM contains %u images", wim->hdr.image_count);
560 /* If the boot index is invalid, print a warning and set it to 0 */
561 if (wim->hdr.boot_idx > wim->hdr.image_count) {
562 WARNING("In `%"TS"', image %u is marked as bootable, "
563 "but there are only %u images in the WIM",
564 wimfile, wim->hdr.boot_idx, wim->hdr.image_count);
565 wim->hdr.boot_idx = 0;
568 /* Check and cache the compression type */
569 if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESSION) {
570 if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESS_LZX) {
571 if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESS_XPRESS) {
572 ERROR("Multiple compression flags are set in \"%"TS"\"",
574 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
576 wim->compression_type = WIMLIB_COMPRESSION_TYPE_LZX;
577 } else if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESS_XPRESS) {
578 wim->compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
580 ERROR("The compression flag is set on \"%"TS"\", but "
581 "neither the XPRESS nor LZX flag is set",
583 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
586 wim->compression_type = WIMLIB_COMPRESSION_TYPE_NONE;
588 wim->out_compression_type = wim->compression_type;
590 /* Check and cache the chunk size. */
591 wim->chunk_size = wim->out_chunk_size = wim->hdr.chunk_size;
592 if (!wim_chunk_size_valid(wim->chunk_size, wim->compression_type)) {
593 ERROR("Invalid chunk size (%"PRIu32" bytes) "
594 "for compression type %"TS"!",
596 wimlib_get_compression_type_string(wim->compression_type));
597 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
600 if (open_flags & WIMLIB_OPEN_FLAG_CHECK_INTEGRITY) {
601 ret = check_wim_integrity(wim, progress_func);
602 if (ret == WIM_INTEGRITY_NONEXISTENT) {
603 WARNING("No integrity information for `%"TS"'; skipping "
604 "integrity check.", wimfile);
605 } else if (ret == WIM_INTEGRITY_NOT_OK) {
606 ERROR("WIM is not intact! (Failed integrity check)");
607 return WIMLIB_ERR_INTEGRITY;
608 } else if (ret != WIM_INTEGRITY_OK) {
613 if (wim->hdr.image_count != 0 && wim->hdr.part_number == 1) {
614 wim->image_metadata = new_image_metadata_array(wim->hdr.image_count);
615 if (!wim->image_metadata)
616 return WIMLIB_ERR_NOMEM;
619 if (open_flags & WIMLIB_OPEN_FLAG_FROM_PIPE) {
620 wim->lookup_table = new_lookup_table(9001);
621 if (!wim->lookup_table)
622 return WIMLIB_ERR_NOMEM;
624 ret = read_wim_lookup_table(wim);
628 ret = read_wim_xml_data(wim);
632 xml_num_images = wim_info_get_num_images(wim->wim_info);
633 if (xml_num_images != wim->hdr.image_count) {
634 ERROR("In the file `%"TS"', there are %u <IMAGE> elements "
635 "in the XML data,", wimfile, xml_num_images);
636 ERROR("but %u images in the WIM! There must be exactly one "
637 "<IMAGE> element per image.", wim->hdr.image_count);
638 return WIMLIB_ERR_IMAGE_COUNT;
640 DEBUG("Done beginning read of WIM file `%"TS"'.", wimfile);
646 open_wim_as_WIMStruct(const void *wim_filename_or_fd, int open_flags,
647 WIMStruct **wim_ret, wimlib_progress_func_t progress_func)
652 wimlib_global_init(WIMLIB_INIT_FLAG_ASSUME_UTF8);
654 ret = WIMLIB_ERR_INVALID_PARAM;
658 ret = WIMLIB_ERR_NOMEM;
659 wim = new_wim_struct();
663 ret = begin_read(wim, wim_filename_or_fd, open_flags, progress_func);
665 goto out_wimlib_free;
676 /* API function documented in wimlib.h */
678 wimlib_open_wim(const tchar *wimfile, int open_flags,
679 WIMStruct **wim_ret, wimlib_progress_func_t progress_func)
681 open_flags &= WIMLIB_OPEN_MASK_PUBLIC;
682 return open_wim_as_WIMStruct(wimfile, open_flags, wim_ret,
687 destroy_image_metadata(struct wim_image_metadata *imd,
688 struct wim_lookup_table *table,
689 bool free_metadata_lte)
691 free_dentry_tree(imd->root_dentry, table);
692 imd->root_dentry = NULL;
693 free_wim_security_data(imd->security_data);
694 imd->security_data = NULL;
696 if (free_metadata_lte) {
697 free_lookup_table_entry(imd->metadata_lte);
698 imd->metadata_lte = NULL;
701 struct wim_lookup_table_entry *lte, *tmp;
702 list_for_each_entry_safe(lte, tmp, &imd->unhashed_streams, unhashed_list)
703 free_lookup_table_entry(lte);
705 INIT_LIST_HEAD(&imd->unhashed_streams);
706 INIT_LIST_HEAD(&imd->inode_list);
709 do_ntfs_umount(imd->ntfs_vol);
710 imd->ntfs_vol = NULL;
716 put_image_metadata(struct wim_image_metadata *imd,
717 struct wim_lookup_table *table)
719 if (imd && --imd->refcnt == 0) {
720 destroy_image_metadata(imd, table, true);
725 /* Appends the specified image metadata structure to the array of image metadata
726 * for a WIM, and increments the image count. */
728 append_image_metadata(WIMStruct *wim, struct wim_image_metadata *imd)
730 struct wim_image_metadata **imd_array;
732 DEBUG("Reallocating image metadata array for image_count = %u",
733 wim->hdr.image_count + 1);
734 imd_array = REALLOC(wim->image_metadata,
735 sizeof(wim->image_metadata[0]) * (wim->hdr.image_count + 1));
738 return WIMLIB_ERR_NOMEM;
739 wim->image_metadata = imd_array;
740 imd_array[wim->hdr.image_count++] = imd;
745 struct wim_image_metadata *
746 new_image_metadata(void)
748 struct wim_image_metadata *imd;
750 imd = CALLOC(1, sizeof(*imd));
753 INIT_LIST_HEAD(&imd->inode_list);
754 INIT_LIST_HEAD(&imd->unhashed_streams);
755 DEBUG("Created new image metadata (refcnt=1)");
757 ERROR_WITH_ERRNO("Failed to allocate new image metadata structure");
762 struct wim_image_metadata **
763 new_image_metadata_array(unsigned num_images)
765 struct wim_image_metadata **imd_array;
767 DEBUG("Creating new image metadata array for %u images",
770 imd_array = CALLOC(num_images, sizeof(imd_array[0]));
773 ERROR("Failed to allocate memory for %u image metadata structures",
777 for (unsigned i = 0; i < num_images; i++) {
778 imd_array[i] = new_image_metadata();
780 for (unsigned j = 0; j < i; j++)
781 put_image_metadata(imd_array[j], NULL);
789 /* Checksum all streams that are unhashed (other than the metadata streams),
790 * merging them into the lookup table as needed. This is a no-op unless the
791 * library has previously used to add or mount an image using the same
794 wim_checksum_unhashed_streams(WIMStruct *wim)
798 if (!wim_has_metadata(wim))
800 for (int i = 0; i < wim->hdr.image_count; i++) {
801 struct wim_lookup_table_entry *lte, *tmp;
802 struct wim_image_metadata *imd = wim->image_metadata[i];
803 image_for_each_unhashed_stream_safe(lte, tmp, imd) {
804 ret = hash_unhashed_stream(lte, wim->lookup_table, NULL);
813 * can_modify_wim - Check if a given WIM is writeable. This is only the case if
814 * it meets the following three conditions:
816 * 1. Write access is allowed to the underlying file (if any) at the filesystem level.
817 * 2. The WIM is not part of a spanned set.
818 * 3. The WIM_HDR_FLAG_READONLY flag is not set in the WIM header.
820 * Return value is 0 if writable; WIMLIB_ERR_WIM_IS_READONLY otherwise.
823 can_modify_wim(WIMStruct *wim)
826 if (taccess(wim->filename, W_OK)) {
827 ERROR_WITH_ERRNO("Can't modify \"%"TS"\"", wim->filename);
828 return WIMLIB_ERR_WIM_IS_READONLY;
831 if (wim->hdr.total_parts != 1) {
832 ERROR("Cannot modify \"%"TS"\": is part of a split WIM",
834 return WIMLIB_ERR_WIM_IS_READONLY;
836 if (wim->hdr.flags & WIM_HDR_FLAG_READONLY) {
837 ERROR("Cannot modify \"%"TS"\": is marked read-only",
839 return WIMLIB_ERR_WIM_IS_READONLY;
845 * can_delete_from_wim - Check if files or images can be deleted from a given
848 * This theoretically should be exactly the same as can_modify_wim(), but
849 * unfortunately, due to bugs in Microsoft's software that generate incorrect
850 * reference counts for some WIM resources, we need to run expensive
851 * verifications to make sure the reference counts are correct on all WIM
852 * resources. Otherwise we might delete a WIM resource whose reference count
853 * has fallen to 0, but is actually still referenced somewhere.
856 can_delete_from_wim(WIMStruct *wim)
860 ret = can_modify_wim(wim);
863 if (!wim->refcnts_ok)
864 wim_recalculate_refcnts(wim);
868 /* API function documented in wimlib.h */
870 wimlib_free(WIMStruct *wim)
875 DEBUG("Freeing WIMStruct (filename=\"%"TS"\", image_count=%u)",
876 wim->filename, wim->hdr.image_count);
878 while (!list_empty(&wim->subwims)) {
881 subwim = list_entry(wim->subwims.next, WIMStruct, subwim_node);
882 list_del(&subwim->subwim_node);
883 DEBUG("Freeing subwim.");
887 if (filedes_valid(&wim->in_fd))
888 filedes_close(&wim->in_fd);
889 if (filedes_valid(&wim->out_fd))
890 filedes_close(&wim->out_fd);
892 wimlib_lzx_free_context(wim->lzx_context);
894 free_lookup_table(wim->lookup_table);
897 free_wim_info(wim->wim_info);
898 if (wim->image_metadata) {
899 for (unsigned i = 0; i < wim->hdr.image_count; i++)
900 put_image_metadata(wim->image_metadata[i], NULL);
901 FREE(wim->image_metadata);
907 test_locale_ctype_utf8(void)
912 char *ctype = nl_langinfo(CODESET);
914 return (!strstr(ctype, "UTF-8") ||
915 !strstr(ctype, "UTF8") ||
916 !strstr(ctype, "utf8") ||
917 !strstr(ctype, "utf-8"));
921 /* API function documented in wimlib.h */
923 wimlib_global_init(int init_flags)
925 static bool already_inited = false;
930 libxml_global_init();
931 if (!(init_flags & WIMLIB_INIT_FLAG_ASSUME_UTF8)) {
932 wimlib_mbs_is_utf8 = test_locale_ctype_utf8();
934 if (!wimlib_mbs_is_utf8)
935 libntfs3g_global_init();
939 ret = win32_global_init(init_flags);
945 already_inited = true;
949 /* API function documented in wimlib.h */
951 wimlib_global_cleanup(void)
953 libxml_global_cleanup();
954 iconv_global_cleanup();
956 win32_global_cleanup();