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"
41 #include "wimlib/compressor_ops.h"
42 #include "wimlib/decompressor_ops.h"
45 # include "wimlib/win32.h" /* for realpath() replacement */
51 # include <langinfo.h>
59 wim_default_pack_compression_type(void)
61 return WIMLIB_COMPRESSION_TYPE_LZMS;
65 wim_default_pack_chunk_size(int ctype) {
67 case WIMLIB_COMPRESSION_TYPE_LZMS:
68 /* Note: WIMGAPI uses 1 << 26, but lower sizes are compatible.
70 return 1U << 25; /* 33554432 */
72 return 1U << 15; /* 32768 */
79 WIMStruct *wim = CALLOC(1, sizeof(WIMStruct));
81 filedes_invalidate(&wim->in_fd);
82 filedes_invalidate(&wim->out_fd);
83 wim->out_pack_compression_type = wim_default_pack_compression_type();
84 wim->out_pack_chunk_size = wim_default_pack_chunk_size(
85 wim->out_pack_compression_type);
86 INIT_LIST_HEAD(&wim->subwims);
91 /* Determine if the chunk size is valid for the specified compression type. */
93 wim_chunk_size_valid(u32 chunk_size, int ctype)
97 /* Chunk size is meaningless for uncompressed WIMs --- any value is
99 if (ctype == WIMLIB_COMPRESSION_TYPE_NONE)
102 /* Chunk size must be power of 2. */
105 order = bsr32(chunk_size);
106 if (chunk_size != 1U << order)
125 /* See the documentation for the --chunk-size option of `wimlib-imagex
126 * capture' for information about allowed chunk sizes. */
128 case WIMLIB_COMPRESSION_TYPE_LZX:
129 return order >= 15 && order <= 21;
131 case WIMLIB_COMPRESSION_TYPE_XPRESS:
132 case WIMLIB_COMPRESSION_TYPE_LZMS:
133 return order >= 15 && order <= 26;
138 /* Return the default chunk size to use for the specified compression type.
140 * See notes above in wim_chunk_size_valid(). */
142 wim_default_chunk_size(int ctype)
145 case WIMLIB_COMPRESSION_TYPE_LZMS:
146 return 1U << 17; /* 131072 */
148 return 1U << 15; /* 32768 */
153 * Calls a function on images in the WIM. If @image is WIMLIB_ALL_IMAGES,
154 * @visitor is called on the WIM once for each image, with each image selected
155 * as the current image in turn. If @image is a certain image, @visitor is
156 * called on the WIM only once, with that image selected.
159 for_image(WIMStruct *wim, int image, int (*visitor)(WIMStruct *))
166 if (image == WIMLIB_ALL_IMAGES) {
168 end = wim->hdr.image_count;
169 } else if (image >= 1 && image <= wim->hdr.image_count) {
173 return WIMLIB_ERR_INVALID_IMAGE;
175 for (i = start; i <= end; i++) {
176 ret = select_wim_image(wim, i);
186 /* API function documented in wimlib.h */
188 wimlib_create_new_wim(int ctype, WIMStruct **wim_ret)
191 struct wim_lookup_table *table;
194 wimlib_global_init(WIMLIB_INIT_FLAG_ASSUME_UTF8);
196 DEBUG("Creating new WIM with %"TS" compression.",
197 wimlib_get_compression_type_string(ctype));
199 /* Allocate the WIMStruct. */
200 wim = new_wim_struct();
202 return WIMLIB_ERR_NOMEM;
204 ret = init_wim_header(&wim->hdr, ctype, wim_default_chunk_size(ctype));
208 table = new_lookup_table(9001);
210 ret = WIMLIB_ERR_NOMEM;
213 wim->lookup_table = table;
215 wim->compression_type = ctype;
216 wim->out_compression_type = ctype;
217 wim->chunk_size = wim->hdr.chunk_size;
218 wim->out_chunk_size = wim->hdr.chunk_size;
227 select_wim_image(WIMStruct *wim, int image)
229 struct wim_image_metadata *imd;
232 DEBUG("Selecting image %d", image);
234 if (image == WIMLIB_NO_IMAGE) {
235 ERROR("Invalid image: %d", WIMLIB_NO_IMAGE);
236 return WIMLIB_ERR_INVALID_IMAGE;
239 if (image == wim->current_image)
242 if (image < 1 || image > wim->hdr.image_count) {
243 ERROR("Cannot select image %d: There are only %u images",
244 image, wim->hdr.image_count);
245 return WIMLIB_ERR_INVALID_IMAGE;
248 if (!wim_has_metadata(wim)) {
249 ERROR("\"%"TS"\" does not contain metadata resources!", wim->filename);
250 if (wim->hdr.part_number != 1)
251 ERROR("Specify the first part of the split WIM instead.");
252 return WIMLIB_ERR_METADATA_NOT_FOUND;
255 /* If a valid image is currently selected, it can be freed if it is not
257 if (wim->current_image != WIMLIB_NO_IMAGE) {
258 imd = wim_get_current_image_metadata(wim);
259 if (!imd->modified) {
260 wimlib_assert(list_empty(&imd->unhashed_streams));
261 DEBUG("Freeing image %u", wim->current_image);
262 destroy_image_metadata(imd, NULL, false);
265 wim->current_image = image;
266 imd = wim_get_current_image_metadata(wim);
267 if (imd->root_dentry || imd->modified) {
270 ret = read_metadata_resource(wim, imd);
272 wim->current_image = WIMLIB_NO_IMAGE;
278 /* API function documented in wimlib.h */
279 WIMLIBAPI const tchar *
280 wimlib_get_compression_type_string(int ctype)
283 case WIMLIB_COMPRESSION_TYPE_NONE:
285 case WIMLIB_COMPRESSION_TYPE_LZX:
287 case WIMLIB_COMPRESSION_TYPE_XPRESS:
289 case WIMLIB_COMPRESSION_TYPE_LZMS:
296 /* API function documented in wimlib.h */
298 wimlib_resolve_image(WIMStruct *wim, const tchar *image_name_or_num)
304 if (!image_name_or_num || !*image_name_or_num)
305 return WIMLIB_NO_IMAGE;
307 if (!tstrcasecmp(image_name_or_num, T("all"))
308 || !tstrcasecmp(image_name_or_num, T("*")))
309 return WIMLIB_ALL_IMAGES;
310 image = tstrtol(image_name_or_num, &p, 10);
311 if (p != image_name_or_num && *p == T('\0') && image > 0) {
312 if (image > wim->hdr.image_count)
313 return WIMLIB_NO_IMAGE;
316 for (i = 1; i <= wim->hdr.image_count; i++) {
317 if (!tstrcmp(image_name_or_num,
318 wimlib_get_image_name(wim, i)))
321 return WIMLIB_NO_IMAGE;
325 /* API function documented in wimlib.h */
327 wimlib_print_available_images(const WIMStruct *wim, int image)
333 if (image == WIMLIB_ALL_IMAGES) {
334 n = tprintf(T("Available Images:\n"));
336 last = wim->hdr.image_count;
337 } else if (image >= 1 && image <= wim->hdr.image_count) {
338 n = tprintf(T("Information for Image %d\n"), image);
342 tprintf(T("wimlib_print_available_images(): Invalid image %d"),
346 for (i = 0; i < n - 1; i++)
349 for (i = first; i <= last; i++)
350 print_image_info(wim->wim_info, i);
353 /* TODO: Deprecated; remove this. */
355 wimlib_print_metadata(WIMStruct *wim, int image)
357 return WIMLIB_ERR_UNSUPPORTED;
360 /* API function documented in wimlib.h */
362 wimlib_get_wim_info(WIMStruct *wim, struct wimlib_wim_info *info)
364 memset(info, 0, sizeof(struct wimlib_wim_info));
365 memcpy(info->guid, wim->hdr.guid, WIMLIB_GUID_LEN);
366 info->image_count = wim->hdr.image_count;
367 info->boot_index = wim->hdr.boot_idx;
368 info->wim_version = wim->hdr.wim_version;
369 info->chunk_size = wim->chunk_size;
370 info->part_number = wim->hdr.part_number;
371 info->total_parts = wim->hdr.total_parts;
372 info->compression_type = wim->compression_type;
373 info->total_bytes = wim_info_get_total_bytes(wim->wim_info);
374 info->has_integrity_table = wim_has_integrity_table(wim);
375 info->opened_from_file = (wim->filename != NULL);
376 info->is_readonly = (wim->hdr.flags & WIM_HDR_FLAG_READONLY) ||
377 (wim->hdr.total_parts != 1) ||
378 (wim->filename && taccess(wim->filename, W_OK));
379 info->has_rpfix = (wim->hdr.flags & WIM_HDR_FLAG_RP_FIX) != 0;
380 info->is_marked_readonly = (wim->hdr.flags & WIM_HDR_FLAG_READONLY) != 0;
381 info->write_in_progress = (wim->hdr.flags & WIM_HDR_FLAG_WRITE_IN_PROGRESS) != 0;
382 info->metadata_only = (wim->hdr.flags & WIM_HDR_FLAG_METADATA_ONLY) != 0;
383 info->resource_only = (wim->hdr.flags & WIM_HDR_FLAG_RESOURCE_ONLY) != 0;
384 info->spanned = (wim->hdr.flags & WIM_HDR_FLAG_SPANNED) != 0;
385 info->pipable = wim_is_pipable(wim);
389 /* API function documented in wimlib.h */
391 wimlib_set_wim_info(WIMStruct *wim, const struct wimlib_wim_info *info, int which)
395 if (which & WIMLIB_CHANGE_READONLY_FLAG) {
396 if (info->is_marked_readonly)
397 wim->hdr.flags |= WIM_HDR_FLAG_READONLY;
399 wim->hdr.flags &= ~WIM_HDR_FLAG_READONLY;
402 if ((which & ~WIMLIB_CHANGE_READONLY_FLAG) == 0)
405 ret = can_modify_wim(wim);
409 if (which & WIMLIB_CHANGE_GUID) {
410 memcpy(wim->hdr.guid, info->guid, WIM_GID_LEN);
411 wim->guid_set_explicitly = 1;
414 if (which & WIMLIB_CHANGE_BOOT_INDEX) {
415 if (info->boot_index > wim->hdr.image_count) {
416 ERROR("%u is not 0 or a valid image in the WIM to mark as bootable",
418 return WIMLIB_ERR_INVALID_IMAGE;
420 wim->hdr.boot_idx = info->boot_index;
423 if (which & WIMLIB_CHANGE_RPFIX_FLAG) {
425 wim->hdr.flags |= WIM_HDR_FLAG_RP_FIX;
427 wim->hdr.flags &= ~WIM_HDR_FLAG_RP_FIX;
433 set_out_ctype(int ctype, u8 *out_ctype_p)
436 case WIMLIB_COMPRESSION_TYPE_INVALID:
438 case WIMLIB_COMPRESSION_TYPE_NONE:
439 case WIMLIB_COMPRESSION_TYPE_LZX:
440 case WIMLIB_COMPRESSION_TYPE_XPRESS:
441 case WIMLIB_COMPRESSION_TYPE_LZMS:
442 *out_ctype_p = ctype;
445 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
448 /* API function documented in wimlib.h */
450 wimlib_set_output_compression_type(WIMStruct *wim, int ctype)
452 int ret = set_out_ctype(ctype, &wim->out_compression_type);
456 /* Reset the chunk size if it's no longer valid. */
457 if (!wim_chunk_size_valid(wim->out_chunk_size, ctype))
458 wim->out_chunk_size = wim_default_chunk_size(ctype);
462 /* API function documented in wimlib.h */
464 wimlib_set_output_pack_compression_type(WIMStruct *wim, int ctype)
466 int ret = set_out_ctype(ctype, &wim->out_pack_compression_type);
470 /* Reset the chunk size if it's no longer valid. */
471 if (!wim_chunk_size_valid(wim->out_pack_chunk_size, ctype))
472 wim->out_pack_chunk_size = wim_default_pack_chunk_size(ctype);
477 set_out_chunk_size(u32 chunk_size, int ctype, u32 *out_chunk_size_p)
479 if (!wim_chunk_size_valid(chunk_size, ctype)) {
480 ERROR("Invalid chunk size (%"PRIu32" bytes) "
481 "for compression type %"TS"!",
483 wimlib_get_compression_type_string(ctype));
484 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
487 *out_chunk_size_p = chunk_size;
491 /* API function documented in wimlib.h */
493 wimlib_set_output_chunk_size(WIMStruct *wim, uint32_t chunk_size)
495 if (chunk_size == 0) {
496 wim->out_chunk_size =
497 wim_default_chunk_size(wim->out_compression_type);
501 return set_out_chunk_size(chunk_size,
502 wim->out_compression_type,
503 &wim->out_chunk_size);
506 /* API function documented in wimlib.h */
508 wimlib_set_output_pack_chunk_size(WIMStruct *wim, uint32_t chunk_size)
510 if (chunk_size == 0) {
511 wim->out_pack_chunk_size =
512 wim_default_pack_chunk_size(wim->out_pack_compression_type);
516 return set_out_chunk_size(chunk_size,
517 wim->out_pack_compression_type,
518 &wim->out_pack_chunk_size);
522 open_wim_file(const tchar *filename, struct filedes *fd_ret)
526 raw_fd = topen(filename, O_RDONLY | O_BINARY);
528 ERROR_WITH_ERRNO("Can't open \"%"TS"\" read-only", filename);
529 return WIMLIB_ERR_OPEN;
531 filedes_init(fd_ret, raw_fd);
536 * Begins the reading of a WIM file; opens the file and reads its header and
537 * lookup table, and optionally checks the integrity.
540 begin_read(WIMStruct *wim, const void *wim_filename_or_fd,
541 int open_flags, wimlib_progress_func_t progress_func)
545 const tchar *wimfile;
547 if (open_flags & WIMLIB_OPEN_FLAG_FROM_PIPE) {
549 filedes_init(&wim->in_fd, *(const int*)wim_filename_or_fd);
550 wim->in_fd.is_pipe = 1;
552 wimfile = wim_filename_or_fd;
553 ret = open_wim_file(wimfile, &wim->in_fd);
557 /* The absolute path to the WIM is requested so that
558 * wimlib_overwrite() still works even if the process changes
559 * its working directory. This actually happens if a WIM is
560 * mounted read-write, since the FUSE thread changes directory
561 * to "/", and it needs to be able to find the WIM file again.
563 * This will break if the full path to the WIM changes in the
564 * intervening time...
566 * Warning: in Windows native builds, realpath() calls the
567 * replacement function in win32_replacements.c.
569 wim->filename = realpath(wimfile, NULL);
570 if (wim->filename == NULL) {
571 ERROR_WITH_ERRNO("Failed to resolve WIM filename");
573 return WIMLIB_ERR_NOMEM;
575 return WIMLIB_ERR_OPEN;
579 ret = read_wim_header(wim, &wim->hdr);
583 if (wim->hdr.flags & WIM_HDR_FLAG_WRITE_IN_PROGRESS) {
584 WARNING("The WIM_HDR_FLAG_WRITE_IN_PROGRESS flag is set in the header of\n"
585 " \"%"TS"\". It may be being changed by another process,\n"
586 " or a process may have crashed while writing the WIM.",
590 if (open_flags & WIMLIB_OPEN_FLAG_WRITE_ACCESS) {
591 ret = can_modify_wim(wim);
596 if ((open_flags & WIMLIB_OPEN_FLAG_ERROR_IF_SPLIT) &&
597 (wim->hdr.total_parts != 1))
598 return WIMLIB_ERR_IS_SPLIT_WIM;
600 DEBUG("According to header, WIM contains %u images", wim->hdr.image_count);
602 /* If the boot index is invalid, print a warning and set it to 0 */
603 if (wim->hdr.boot_idx > wim->hdr.image_count) {
604 WARNING("In `%"TS"', image %u is marked as bootable, "
605 "but there are only %u images in the WIM",
606 wimfile, wim->hdr.boot_idx, wim->hdr.image_count);
607 wim->hdr.boot_idx = 0;
610 /* Check and cache the compression type */
611 if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESSION) {
612 if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESS_LZX) {
613 wim->compression_type = WIMLIB_COMPRESSION_TYPE_LZX;
614 } else if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESS_XPRESS) {
615 wim->compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
616 } else if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESS_LZMS) {
617 wim->compression_type = WIMLIB_COMPRESSION_TYPE_LZMS;
619 ERROR("The compression flag is set on \"%"TS"\", but "
620 "a flag for a recognized format is not",
622 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
625 wim->compression_type = WIMLIB_COMPRESSION_TYPE_NONE;
627 wim->out_compression_type = wim->compression_type;
629 /* Check and cache the chunk size. */
630 wim->chunk_size = wim->hdr.chunk_size;
631 wim->out_chunk_size = wim->chunk_size;
632 if (!wim_chunk_size_valid(wim->chunk_size, wim->compression_type)) {
633 ERROR("Invalid chunk size (%"PRIu32" bytes) "
634 "for compression type %"TS"!",
636 wimlib_get_compression_type_string(wim->compression_type));
637 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
640 if (open_flags & WIMLIB_OPEN_FLAG_CHECK_INTEGRITY) {
641 ret = check_wim_integrity(wim, progress_func);
642 if (ret == WIM_INTEGRITY_NONEXISTENT) {
643 WARNING("No integrity information for `%"TS"'; skipping "
644 "integrity check.", wimfile);
645 } else if (ret == WIM_INTEGRITY_NOT_OK) {
646 ERROR("WIM is not intact! (Failed integrity check)");
647 return WIMLIB_ERR_INTEGRITY;
648 } else if (ret != WIM_INTEGRITY_OK) {
653 if (wim->hdr.image_count != 0 && wim->hdr.part_number == 1) {
654 wim->image_metadata = new_image_metadata_array(wim->hdr.image_count);
655 if (wim->image_metadata == NULL)
656 return WIMLIB_ERR_NOMEM;
659 if (open_flags & WIMLIB_OPEN_FLAG_FROM_PIPE) {
660 wim->lookup_table = new_lookup_table(9001);
661 if (wim->lookup_table == NULL)
662 return WIMLIB_ERR_NOMEM;
665 ret = read_wim_xml_data(wim);
669 xml_num_images = wim_info_get_num_images(wim->wim_info);
670 if (xml_num_images != wim->hdr.image_count) {
671 ERROR("In the file `%"TS"', there are %u <IMAGE> elements "
672 "in the XML data,", wimfile, xml_num_images);
673 ERROR("but %u images in the WIM! There must be exactly one "
674 "<IMAGE> element per image.", wim->hdr.image_count);
675 return WIMLIB_ERR_IMAGE_COUNT;
678 ret = read_wim_lookup_table(wim);
682 DEBUG("Done beginning read of WIM file `%"TS"'.", wimfile);
688 open_wim_as_WIMStruct(const void *wim_filename_or_fd, int open_flags,
689 WIMStruct **wim_ret, wimlib_progress_func_t progress_func)
694 if (open_flags & WIMLIB_OPEN_FLAG_FROM_PIPE)
695 DEBUG("Opening pipable WIM from file descriptor %d.", *(const int*)wim_filename_or_fd);
697 DEBUG("Opening WIM file \"%"TS"\"", (const tchar*)wim_filename_or_fd);
699 wimlib_global_init(WIMLIB_INIT_FLAG_ASSUME_UTF8);
702 return WIMLIB_ERR_INVALID_PARAM;
704 wim = new_wim_struct();
706 return WIMLIB_ERR_NOMEM;
708 ret = begin_read(wim, wim_filename_or_fd, open_flags, progress_func);
714 DEBUG("Successfully opened WIM and created WIMStruct.");
719 /* API function documented in wimlib.h */
721 wimlib_open_wim(const tchar *wimfile, int open_flags,
722 WIMStruct **wim_ret, wimlib_progress_func_t progress_func)
724 open_flags &= WIMLIB_OPEN_MASK_PUBLIC;
725 return open_wim_as_WIMStruct(wimfile, open_flags, wim_ret,
730 destroy_image_metadata(struct wim_image_metadata *imd,
731 struct wim_lookup_table *table,
732 bool free_metadata_lte)
734 free_dentry_tree(imd->root_dentry, table);
735 imd->root_dentry = NULL;
736 free_wim_security_data(imd->security_data);
737 imd->security_data = NULL;
739 if (free_metadata_lte) {
740 free_lookup_table_entry(imd->metadata_lte);
741 imd->metadata_lte = NULL;
744 struct wim_lookup_table_entry *lte, *tmp;
745 list_for_each_entry_safe(lte, tmp, &imd->unhashed_streams, unhashed_list)
746 free_lookup_table_entry(lte);
748 INIT_LIST_HEAD(&imd->unhashed_streams);
749 INIT_LIST_HEAD(&imd->inode_list);
752 do_ntfs_umount(imd->ntfs_vol);
753 imd->ntfs_vol = NULL;
759 put_image_metadata(struct wim_image_metadata *imd,
760 struct wim_lookup_table *table)
762 if (imd && --imd->refcnt == 0) {
763 destroy_image_metadata(imd, table, true);
768 /* Appends the specified image metadata structure to the array of image metadata
769 * for a WIM, and increments the image count. */
771 append_image_metadata(WIMStruct *wim, struct wim_image_metadata *imd)
773 struct wim_image_metadata **imd_array;
775 DEBUG("Reallocating image metadata array for image_count = %u",
776 wim->hdr.image_count + 1);
777 imd_array = REALLOC(wim->image_metadata,
778 sizeof(wim->image_metadata[0]) * (wim->hdr.image_count + 1));
780 if (imd_array == NULL)
781 return WIMLIB_ERR_NOMEM;
782 wim->image_metadata = imd_array;
783 imd_array[wim->hdr.image_count++] = imd;
788 struct wim_image_metadata *
789 new_image_metadata(void)
791 struct wim_image_metadata *imd;
793 imd = CALLOC(1, sizeof(*imd));
796 INIT_LIST_HEAD(&imd->inode_list);
797 INIT_LIST_HEAD(&imd->unhashed_streams);
798 DEBUG("Created new image metadata (refcnt=1)");
800 ERROR_WITH_ERRNO("Failed to allocate new image metadata structure");
805 struct wim_image_metadata **
806 new_image_metadata_array(unsigned num_images)
808 struct wim_image_metadata **imd_array;
810 DEBUG("Creating new image metadata array for %u images",
813 imd_array = CALLOC(num_images, sizeof(imd_array[0]));
815 if (imd_array == NULL) {
816 ERROR("Failed to allocate memory for %u image metadata structures",
820 for (unsigned i = 0; i < num_images; i++) {
821 imd_array[i] = new_image_metadata();
822 if (imd_array[i] == NULL) {
823 for (unsigned j = 0; j < i; j++)
824 put_image_metadata(imd_array[j], NULL);
832 /* Checksum all streams that are unhashed (other than the metadata streams),
833 * merging them into the lookup table as needed. This is a no-op unless the
834 * library has previously used to add or mount an image using the same
837 wim_checksum_unhashed_streams(WIMStruct *wim)
841 if (!wim_has_metadata(wim))
843 for (int i = 0; i < wim->hdr.image_count; i++) {
844 struct wim_lookup_table_entry *lte, *tmp;
845 struct wim_image_metadata *imd = wim->image_metadata[i];
846 image_for_each_unhashed_stream_safe(lte, tmp, imd) {
847 struct wim_lookup_table_entry *new_lte;
848 ret = hash_unhashed_stream(lte, wim->lookup_table, &new_lte);
852 free_lookup_table_entry(lte);
859 * can_modify_wim - Check if a given WIM is writeable. This is only the case if
860 * it meets the following three conditions:
862 * 1. Write access is allowed to the underlying file (if any) at the filesystem level.
863 * 2. The WIM is not part of a spanned set.
864 * 3. The WIM_HDR_FLAG_READONLY flag is not set in the WIM header.
866 * Return value is 0 if writable; WIMLIB_ERR_WIM_IS_READONLY otherwise.
869 can_modify_wim(WIMStruct *wim)
872 if (taccess(wim->filename, W_OK)) {
873 ERROR_WITH_ERRNO("Can't modify \"%"TS"\"", wim->filename);
874 return WIMLIB_ERR_WIM_IS_READONLY;
877 if (wim->hdr.total_parts != 1) {
878 ERROR("Cannot modify \"%"TS"\": is part of a split WIM",
880 return WIMLIB_ERR_WIM_IS_READONLY;
882 if (wim->hdr.flags & WIM_HDR_FLAG_READONLY) {
883 ERROR("Cannot modify \"%"TS"\": is marked read-only",
885 return WIMLIB_ERR_WIM_IS_READONLY;
891 * can_delete_from_wim - Check if files or images can be deleted from a given
894 * This theoretically should be exactly the same as can_modify_wim(), but
895 * unfortunately, due to bugs in Microsoft's software that generate incorrect
896 * reference counts for some WIM resources, we need to run expensive
897 * verifications to make sure the reference counts are correct on all WIM
898 * resources. Otherwise we might delete a WIM resource whose reference count
899 * has fallen to 0, but is actually still referenced somewhere.
902 can_delete_from_wim(WIMStruct *wim)
906 ret = can_modify_wim(wim);
909 if (!wim->refcnts_ok) {
910 ret = wim_recalculate_refcnts(wim);
917 /* API function documented in wimlib.h */
919 wimlib_free(WIMStruct *wim)
924 DEBUG("Freeing WIMStruct (filename=\"%"TS"\", image_count=%u)",
925 wim->filename, wim->hdr.image_count);
927 while (!list_empty(&wim->subwims)) {
930 subwim = list_entry(wim->subwims.next, WIMStruct, subwim_node);
931 list_del(&subwim->subwim_node);
932 DEBUG("Freeing subwim.");
936 if (filedes_valid(&wim->in_fd))
937 filedes_close(&wim->in_fd);
938 if (filedes_valid(&wim->out_fd))
939 filedes_close(&wim->out_fd);
941 free_lookup_table(wim->lookup_table);
943 wimlib_free_decompressor(wim->decompressor);
946 free_wim_info(wim->wim_info);
947 if (wim->image_metadata) {
948 for (unsigned i = 0; i < wim->hdr.image_count; i++)
949 put_image_metadata(wim->image_metadata[i], NULL);
950 FREE(wim->image_metadata);
956 test_locale_ctype_utf8(void)
961 char *ctype = nl_langinfo(CODESET);
963 return (!strstr(ctype, "UTF-8") ||
964 !strstr(ctype, "UTF8") ||
965 !strstr(ctype, "utf8") ||
966 !strstr(ctype, "utf-8"));
970 /* API function documented in wimlib.h */
972 wimlib_global_init(int init_flags)
974 static bool already_inited = false;
978 libxml_global_init();
979 if (!(init_flags & WIMLIB_INIT_FLAG_ASSUME_UTF8)) {
980 wimlib_mbs_is_utf8 = test_locale_ctype_utf8();
982 if (!wimlib_mbs_is_utf8)
983 libntfs3g_global_init();
988 int ret = win32_global_init(init_flags);
994 if (init_flags & WIMLIB_INIT_FLAG_DEFAULT_CASE_SENSITIVE)
995 default_ignore_case = false;
996 else if (init_flags & WIMLIB_INIT_FLAG_DEFAULT_CASE_INSENSITIVE)
997 default_ignore_case = true;
998 already_inited = true;
1002 /* API function documented in wimlib.h */
1004 wimlib_global_cleanup(void)
1006 libxml_global_cleanup();
1007 iconv_global_cleanup();
1009 win32_global_cleanup();
1011 cleanup_decompressor_params();
1012 cleanup_compressor_params();