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 image_print_metadata(WIMStruct *wim)
61 DEBUG("Printing metadata for image %d", wim->current_image);
62 print_wim_security_data(wim_security_data(wim));
63 return for_dentry_in_tree(wim_root_dentry(wim), print_dentry,
71 WIMStruct *wim = CALLOC(1, sizeof(WIMStruct));
75 INIT_LIST_HEAD(&wim->subwims);
80 /* Determine if the chunk size is valid for the specified compression type. */
82 wim_chunk_size_valid(u32 chunk_size, int ctype)
86 /* Chunk size is meaningless for uncompressed WIMs --- any value is
88 if (ctype == WIMLIB_COMPRESSION_TYPE_NONE)
91 /* Chunk size must be power of 2. */
94 order = bsr32(chunk_size);
95 if (chunk_size != 1U << order)
114 case WIMLIB_COMPRESSION_TYPE_LZX:
115 /* For LZX compression, the chunk size corresponds to the LZX
116 * window size, which according the LZX specification can be any
117 * power of 2 between 2^15 and 2^21, inclusively. All these are
118 * supported by wimlib; however, unfortunately only 2^15 is
119 * supported by WIMGAPI[1] so this value is used by default.
121 * [1] WIMGAPI (Windows 7) attempts to decompress LZX chunk
122 * sizes > 2^15 but seems to have bug(s) that cause it to fail
123 * or crash. (I tried several tweaks to the LZX data but none
124 * resulted in successful decompression.) WIMGAPI (Windows 8)
125 * appears to refuse to open WIMs with chunk size > 2^15
127 return order >= 15 && order <= 21;
129 case WIMLIB_COMPRESSION_TYPE_XPRESS:
130 /* WIMGAPI (Windows 7, Windows 8) doesn't seem to support XPRESS
131 * chunk size below 32768 bytes, but larger power-of-two sizes,
132 * up ta 67108864 bytes, appear to work. (Note, however, that
133 * the offsets of XPRESS matches are still limited to 65535
134 * bytes even when a much larger chunk size is used!) */
135 return order >= 15 && order <= 26;
137 case WIMLIB_COMPRESSION_TYPE_LZMS:
138 return order >= 15 && order <= 26;
143 /* Return the default chunk size to use for the specified compression type.
145 * See notes above in wim_chunk_size_valid(). */
147 wim_default_chunk_size(int ctype)
150 case WIMLIB_COMPRESSION_TYPE_LZMS:
151 return 1U << 17; /* 131072 */
153 return 1U << 15; /* 32768 */
158 wim_default_pack_chunk_size(int ctype) {
160 case WIMLIB_COMPRESSION_TYPE_LZMS:
161 /* Note: WIMGAPI uses 1 << 26, but lower sizes are compatible.
163 return 1U << 25; /* 33554432 */
165 return 1U << 15; /* 32768 */
170 * Calls a function on images in the WIM. If @image is WIMLIB_ALL_IMAGES, @visitor
171 * is called on the WIM once for each image, with each image selected as the
172 * current image in turn. If @image is a certain image, @visitor is called on
173 * the WIM only once, with that image selected.
176 for_image(WIMStruct *wim, int image, int (*visitor)(WIMStruct *))
183 if (image == WIMLIB_ALL_IMAGES) {
185 end = wim->hdr.image_count;
186 } else if (image >= 1 && image <= wim->hdr.image_count) {
190 return WIMLIB_ERR_INVALID_IMAGE;
192 for (i = start; i <= end; i++) {
193 ret = select_wim_image(wim, i);
203 /* API function documented in wimlib.h */
205 wimlib_create_new_wim(int ctype, WIMStruct **wim_ret)
208 struct wim_lookup_table *table;
211 wimlib_global_init(WIMLIB_INIT_FLAG_ASSUME_UTF8);
213 DEBUG("Creating new WIM with %"TS" compression.",
214 wimlib_get_compression_type_string(ctype));
216 /* Allocate the WIMStruct. */
217 wim = new_wim_struct();
219 return WIMLIB_ERR_NOMEM;
221 ret = init_wim_header(&wim->hdr, ctype, wim_default_chunk_size(ctype));
225 table = new_lookup_table(9001);
227 ret = WIMLIB_ERR_NOMEM;
230 wim->lookup_table = table;
232 wim->compression_type = ctype;
233 wim->out_compression_type = ctype;
234 wim->out_pack_compression_type = ctype;
235 wim->chunk_size = wim->hdr.chunk_size;
236 wim->out_chunk_size = wim->hdr.chunk_size;
237 wim->out_pack_chunk_size = wim_default_pack_chunk_size(ctype);
246 select_wim_image(WIMStruct *wim, int image)
248 struct wim_image_metadata *imd;
251 DEBUG("Selecting image %d", image);
253 if (image == WIMLIB_NO_IMAGE) {
254 ERROR("Invalid image: %d", WIMLIB_NO_IMAGE);
255 return WIMLIB_ERR_INVALID_IMAGE;
258 if (image == wim->current_image)
261 if (image < 1 || image > wim->hdr.image_count) {
262 ERROR("Cannot select image %d: There are only %u images",
263 image, wim->hdr.image_count);
264 return WIMLIB_ERR_INVALID_IMAGE;
267 if (!wim_has_metadata(wim)) {
268 ERROR("\"%"TS"\" does not contain metadata resources!", wim->filename);
269 if (wim->hdr.part_number != 1)
270 ERROR("Specify the first part of the split WIM instead.");
271 return WIMLIB_ERR_METADATA_NOT_FOUND;
274 /* If a valid image is currently selected, it can be freed if it is not
276 if (wim->current_image != WIMLIB_NO_IMAGE) {
277 imd = wim_get_current_image_metadata(wim);
278 if (!imd->modified) {
279 wimlib_assert(list_empty(&imd->unhashed_streams));
280 DEBUG("Freeing image %u", wim->current_image);
281 destroy_image_metadata(imd, NULL, false);
284 wim->current_image = image;
285 imd = wim_get_current_image_metadata(wim);
286 if (imd->root_dentry || imd->modified) {
290 DEBUG("Reading metadata resource specified by the following "
291 "lookup table entry:");
292 print_lookup_table_entry(imd->metadata_lte, stderr);
294 ret = read_metadata_resource(wim, imd);
296 wim->current_image = WIMLIB_NO_IMAGE;
302 /* API function documented in wimlib.h */
303 WIMLIBAPI const tchar *
304 wimlib_get_compression_type_string(int ctype)
307 case WIMLIB_COMPRESSION_TYPE_NONE:
309 case WIMLIB_COMPRESSION_TYPE_LZX:
311 case WIMLIB_COMPRESSION_TYPE_XPRESS:
313 case WIMLIB_COMPRESSION_TYPE_LZMS:
320 /* API function documented in wimlib.h */
322 wimlib_resolve_image(WIMStruct *wim, const tchar *image_name_or_num)
328 if (!image_name_or_num || !*image_name_or_num)
329 return WIMLIB_NO_IMAGE;
331 if (!tstrcasecmp(image_name_or_num, T("all"))
332 || !tstrcasecmp(image_name_or_num, T("*")))
333 return WIMLIB_ALL_IMAGES;
334 image = tstrtol(image_name_or_num, &p, 10);
335 if (p != image_name_or_num && *p == T('\0') && image > 0) {
336 if (image > wim->hdr.image_count)
337 return WIMLIB_NO_IMAGE;
340 for (i = 1; i <= wim->hdr.image_count; i++) {
341 if (!tstrcmp(image_name_or_num,
342 wimlib_get_image_name(wim, i)))
345 return WIMLIB_NO_IMAGE;
349 /* API function documented in wimlib.h */
351 wimlib_print_available_images(const WIMStruct *wim, int image)
357 if (image == WIMLIB_ALL_IMAGES) {
358 n = tprintf(T("Available Images:\n"));
360 last = wim->hdr.image_count;
361 } else if (image >= 1 && image <= wim->hdr.image_count) {
362 n = tprintf(T("Information for Image %d\n"), image);
366 tprintf(T("wimlib_print_available_images(): Invalid image %d"),
370 for (i = 0; i < n - 1; i++)
373 for (i = first; i <= last; i++)
374 print_image_info(wim->wim_info, i);
378 /* API function documented in wimlib.h */
380 wimlib_print_metadata(WIMStruct *wim, int image)
382 return for_image(wim, image, image_print_metadata);
385 /* API function documented in wimlib.h */
387 wimlib_get_wim_info(WIMStruct *wim, struct wimlib_wim_info *info)
389 memset(info, 0, sizeof(struct wimlib_wim_info));
390 memcpy(info->guid, wim->hdr.guid, WIMLIB_GUID_LEN);
391 info->image_count = wim->hdr.image_count;
392 info->boot_index = wim->hdr.boot_idx;
393 info->wim_version = wim->hdr.wim_version;
394 info->chunk_size = wim->hdr.chunk_size;
395 info->part_number = wim->hdr.part_number;
396 info->total_parts = wim->hdr.total_parts;
397 info->compression_type = wim->compression_type;
398 info->total_bytes = wim_info_get_total_bytes(wim->wim_info);
399 info->has_integrity_table = wim_has_integrity_table(wim);
400 info->opened_from_file = (wim->filename != NULL);
401 info->is_readonly = (wim->hdr.flags & WIM_HDR_FLAG_READONLY) ||
402 (wim->hdr.total_parts != 1) ||
403 (wim->filename && taccess(wim->filename, W_OK));
404 info->has_rpfix = (wim->hdr.flags & WIM_HDR_FLAG_RP_FIX) != 0;
405 info->is_marked_readonly = (wim->hdr.flags & WIM_HDR_FLAG_READONLY) != 0;
406 info->write_in_progress = (wim->hdr.flags & WIM_HDR_FLAG_WRITE_IN_PROGRESS) != 0;
407 info->metadata_only = (wim->hdr.flags & WIM_HDR_FLAG_METADATA_ONLY) != 0;
408 info->resource_only = (wim->hdr.flags & WIM_HDR_FLAG_RESOURCE_ONLY) != 0;
409 info->spanned = (wim->hdr.flags & WIM_HDR_FLAG_SPANNED) != 0;
410 info->pipable = wim_is_pipable(wim);
414 /* API function documented in wimlib.h */
416 wimlib_set_wim_info(WIMStruct *wim, const struct wimlib_wim_info *info, int which)
420 if (which & WIMLIB_CHANGE_READONLY_FLAG) {
421 if (info->is_marked_readonly)
422 wim->hdr.flags |= WIM_HDR_FLAG_READONLY;
424 wim->hdr.flags &= ~WIM_HDR_FLAG_READONLY;
427 if ((which & ~WIMLIB_CHANGE_READONLY_FLAG) == 0)
430 ret = can_modify_wim(wim);
434 if (which & WIMLIB_CHANGE_GUID) {
435 memcpy(wim->hdr.guid, info->guid, WIM_GID_LEN);
436 wim->guid_set_explicitly = 1;
439 if (which & WIMLIB_CHANGE_BOOT_INDEX) {
440 if (info->boot_index > wim->hdr.image_count) {
441 ERROR("%u is not 0 or a valid image in the WIM to mark as bootable",
443 return WIMLIB_ERR_INVALID_IMAGE;
445 wim->hdr.boot_idx = info->boot_index;
448 if (which & WIMLIB_CHANGE_RPFIX_FLAG) {
450 wim->hdr.flags |= WIM_HDR_FLAG_RP_FIX;
452 wim->hdr.flags &= ~WIM_HDR_FLAG_RP_FIX;
458 set_out_ctype(int ctype, u8 *out_ctype_p)
461 case WIMLIB_COMPRESSION_TYPE_INVALID:
463 case WIMLIB_COMPRESSION_TYPE_NONE:
464 case WIMLIB_COMPRESSION_TYPE_LZX:
465 case WIMLIB_COMPRESSION_TYPE_XPRESS:
466 case WIMLIB_COMPRESSION_TYPE_LZMS:
467 *out_ctype_p = ctype;
470 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
473 /* API function documented in wimlib.h */
475 wimlib_set_output_compression_type(WIMStruct *wim, int ctype)
477 int ret = set_out_ctype(ctype, &wim->out_compression_type);
481 /* Reset the chunk size if it's no longer valid. */
482 if (!wim_chunk_size_valid(ctype, wim->out_chunk_size))
483 wim->out_chunk_size = wim_default_chunk_size(ctype);
487 /* API function documented in wimlib.h */
489 wimlib_set_output_pack_compression_type(WIMStruct *wim, int ctype)
491 int ret = set_out_ctype(ctype, &wim->out_pack_compression_type);
495 /* Reset the chunk size if it's no longer valid. */
496 if (!wim_chunk_size_valid(ctype, wim->out_pack_chunk_size))
497 wim->out_pack_chunk_size = wim_default_pack_chunk_size(ctype);
502 set_out_chunk_size(u32 chunk_size, int ctype, u32 *out_chunk_size_p)
504 if (!wim_chunk_size_valid(chunk_size, ctype)) {
505 ERROR("Invalid chunk size (%"PRIu32" bytes) "
506 "for compression type %"TS"!",
508 wimlib_get_compression_type_string(ctype));
509 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
512 *out_chunk_size_p = chunk_size;
516 /* API function documented in wimlib.h */
518 wimlib_set_output_chunk_size(WIMStruct *wim, uint32_t chunk_size)
520 if (chunk_size == 0) {
521 wim->out_chunk_size =
522 wim_default_chunk_size(wim->out_compression_type);
526 return set_out_chunk_size(chunk_size,
527 wim->out_compression_type,
528 &wim->out_chunk_size);
531 /* API function documented in wimlib.h */
533 wimlib_set_output_pack_chunk_size(WIMStruct *wim, uint32_t chunk_size)
535 if (chunk_size == 0) {
536 wim->out_pack_chunk_size =
537 wim_default_pack_chunk_size(wim->out_pack_compression_type);
541 return set_out_chunk_size(chunk_size,
542 wim->out_pack_compression_type,
543 &wim->out_pack_chunk_size);
547 do_open_wim(const tchar *filename, struct filedes *fd_ret)
551 raw_fd = topen(filename, O_RDONLY | O_BINARY);
553 ERROR_WITH_ERRNO("Can't open \"%"TS"\" read-only", filename);
554 return WIMLIB_ERR_OPEN;
556 filedes_init(fd_ret, raw_fd);
561 reopen_wim(WIMStruct *wim)
563 wimlib_assert(!filedes_valid(&wim->in_fd));
564 return do_open_wim(wim->filename, &wim->in_fd);
568 close_wim(WIMStruct *wim)
570 if (filedes_valid(&wim->in_fd)) {
571 filedes_close(&wim->in_fd);
572 filedes_invalidate(&wim->in_fd);
578 * Begins the reading of a WIM file; opens the file and reads its header and
579 * lookup table, and optionally checks the integrity.
582 begin_read(WIMStruct *wim, const void *wim_filename_or_fd,
583 int open_flags, wimlib_progress_func_t progress_func)
587 const tchar *wimfile;
589 if (open_flags & WIMLIB_OPEN_FLAG_FROM_PIPE) {
591 filedes_init(&wim->in_fd, *(const int*)wim_filename_or_fd);
592 wim->in_fd.is_pipe = 1;
594 wimfile = wim_filename_or_fd;
595 ret = do_open_wim(wimfile, &wim->in_fd);
599 /* The absolute path to the WIM is requested so that
600 * wimlib_overwrite() still works even if the process changes
601 * its working directory. This actually happens if a WIM is
602 * mounted read-write, since the FUSE thread changes directory
603 * to "/", and it needs to be able to find the WIM file again.
605 * This will break if the full path to the WIM changes in the
606 * intervening time...
608 * Warning: in Windows native builds, realpath() calls the
609 * replacement function in win32_replacements.c.
611 wim->filename = realpath(wimfile, NULL);
612 if (wim->filename == NULL) {
613 ERROR_WITH_ERRNO("Failed to resolve WIM filename");
615 return WIMLIB_ERR_NOMEM;
617 return WIMLIB_ERR_OPEN;
621 ret = read_wim_header(wim, &wim->hdr);
625 if (wim->hdr.flags & WIM_HDR_FLAG_WRITE_IN_PROGRESS) {
626 WARNING("The WIM_HDR_FLAG_WRITE_IN_PROGRESS flag is set in the header of\n"
627 " \"%"TS"\". It may be being changed by another process,\n"
628 " or a process may have crashed while writing the WIM.",
632 if (open_flags & WIMLIB_OPEN_FLAG_WRITE_ACCESS) {
633 ret = can_modify_wim(wim);
638 if ((open_flags & WIMLIB_OPEN_FLAG_ERROR_IF_SPLIT) &&
639 (wim->hdr.total_parts != 1))
640 return WIMLIB_ERR_IS_SPLIT_WIM;
642 DEBUG("According to header, WIM contains %u images", wim->hdr.image_count);
644 /* If the boot index is invalid, print a warning and set it to 0 */
645 if (wim->hdr.boot_idx > wim->hdr.image_count) {
646 WARNING("In `%"TS"', image %u is marked as bootable, "
647 "but there are only %u images in the WIM",
648 wimfile, wim->hdr.boot_idx, wim->hdr.image_count);
649 wim->hdr.boot_idx = 0;
652 /* Check and cache the compression type */
653 if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESSION) {
654 if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESS_LZX) {
655 wim->compression_type = WIMLIB_COMPRESSION_TYPE_LZX;
656 } else if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESS_XPRESS) {
657 wim->compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
658 } else if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESS_LZMS) {
659 wim->compression_type = WIMLIB_COMPRESSION_TYPE_LZMS;
661 ERROR("The compression flag is set on \"%"TS"\", but "
662 "a flag for a recognized format is not",
664 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
667 wim->compression_type = WIMLIB_COMPRESSION_TYPE_NONE;
669 wim->out_compression_type = wim->compression_type;
670 wim->out_pack_compression_type = wim->compression_type;
672 /* Check and cache the chunk size. */
673 wim->chunk_size = wim->hdr.chunk_size;
674 wim->out_chunk_size = wim->chunk_size;
675 wim->out_pack_chunk_size = wim_default_pack_chunk_size(wim->out_pack_compression_type);
676 if (!wim_chunk_size_valid(wim->chunk_size, wim->compression_type)) {
677 ERROR("Invalid chunk size (%"PRIu32" bytes) "
678 "for compression type %"TS"!",
680 wimlib_get_compression_type_string(wim->compression_type));
681 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
684 if (open_flags & WIMLIB_OPEN_FLAG_CHECK_INTEGRITY) {
685 ret = check_wim_integrity(wim, progress_func);
686 if (ret == WIM_INTEGRITY_NONEXISTENT) {
687 WARNING("No integrity information for `%"TS"'; skipping "
688 "integrity check.", wimfile);
689 } else if (ret == WIM_INTEGRITY_NOT_OK) {
690 ERROR("WIM is not intact! (Failed integrity check)");
691 return WIMLIB_ERR_INTEGRITY;
692 } else if (ret != WIM_INTEGRITY_OK) {
697 if (wim->hdr.image_count != 0 && wim->hdr.part_number == 1) {
698 wim->image_metadata = new_image_metadata_array(wim->hdr.image_count);
699 if (wim->image_metadata == NULL)
700 return WIMLIB_ERR_NOMEM;
703 if (open_flags & WIMLIB_OPEN_FLAG_FROM_PIPE) {
704 wim->lookup_table = new_lookup_table(9001);
705 if (wim->lookup_table == NULL)
706 return WIMLIB_ERR_NOMEM;
708 ret = read_wim_lookup_table(wim);
712 ret = read_wim_xml_data(wim);
716 xml_num_images = wim_info_get_num_images(wim->wim_info);
717 if (xml_num_images != wim->hdr.image_count) {
718 ERROR("In the file `%"TS"', there are %u <IMAGE> elements "
719 "in the XML data,", wimfile, xml_num_images);
720 ERROR("but %u images in the WIM! There must be exactly one "
721 "<IMAGE> element per image.", wim->hdr.image_count);
722 return WIMLIB_ERR_IMAGE_COUNT;
724 DEBUG("Done beginning read of WIM file `%"TS"'.", wimfile);
730 open_wim_as_WIMStruct(const void *wim_filename_or_fd, int open_flags,
731 WIMStruct **wim_ret, wimlib_progress_func_t progress_func)
736 if (open_flags & WIMLIB_OPEN_FLAG_FROM_PIPE)
737 DEBUG("Opening pipable WIM from file descriptor %d.", *(const int*)wim_filename_or_fd);
739 DEBUG("Opening WIM file \"%"TS"\"", (const tchar*)wim_filename_or_fd);
741 wimlib_global_init(WIMLIB_INIT_FLAG_ASSUME_UTF8);
744 return WIMLIB_ERR_INVALID_PARAM;
746 wim = new_wim_struct();
748 return WIMLIB_ERR_NOMEM;
750 ret = begin_read(wim, wim_filename_or_fd, open_flags, progress_func);
756 DEBUG("Successfully opened WIM and created WIMStruct.");
761 /* API function documented in wimlib.h */
763 wimlib_open_wim(const tchar *wimfile, int open_flags,
764 WIMStruct **wim_ret, wimlib_progress_func_t progress_func)
766 open_flags &= WIMLIB_OPEN_MASK_PUBLIC;
767 return open_wim_as_WIMStruct(wimfile, open_flags, wim_ret,
772 destroy_image_metadata(struct wim_image_metadata *imd,
773 struct wim_lookup_table *table,
774 bool free_metadata_lte)
776 free_dentry_tree(imd->root_dentry, table);
777 imd->root_dentry = NULL;
778 free_wim_security_data(imd->security_data);
779 imd->security_data = NULL;
781 if (free_metadata_lte) {
782 free_lookup_table_entry(imd->metadata_lte);
783 imd->metadata_lte = NULL;
786 struct wim_lookup_table_entry *lte, *tmp;
787 list_for_each_entry_safe(lte, tmp, &imd->unhashed_streams, unhashed_list)
788 free_lookup_table_entry(lte);
790 INIT_LIST_HEAD(&imd->unhashed_streams);
791 INIT_LIST_HEAD(&imd->inode_list);
794 do_ntfs_umount(imd->ntfs_vol);
795 imd->ntfs_vol = NULL;
801 put_image_metadata(struct wim_image_metadata *imd,
802 struct wim_lookup_table *table)
804 if (imd && --imd->refcnt == 0) {
805 destroy_image_metadata(imd, table, true);
810 /* Appends the specified image metadata structure to the array of image metadata
811 * for a WIM, and increments the image count. */
813 append_image_metadata(WIMStruct *wim, struct wim_image_metadata *imd)
815 struct wim_image_metadata **imd_array;
817 DEBUG("Reallocating image metadata array for image_count = %u",
818 wim->hdr.image_count + 1);
819 imd_array = REALLOC(wim->image_metadata,
820 sizeof(wim->image_metadata[0]) * (wim->hdr.image_count + 1));
822 if (imd_array == NULL)
823 return WIMLIB_ERR_NOMEM;
824 wim->image_metadata = imd_array;
825 imd_array[wim->hdr.image_count++] = imd;
830 struct wim_image_metadata *
831 new_image_metadata(void)
833 struct wim_image_metadata *imd;
835 imd = CALLOC(1, sizeof(*imd));
838 INIT_LIST_HEAD(&imd->inode_list);
839 INIT_LIST_HEAD(&imd->unhashed_streams);
840 DEBUG("Created new image metadata (refcnt=1)");
842 ERROR_WITH_ERRNO("Failed to allocate new image metadata structure");
847 struct wim_image_metadata **
848 new_image_metadata_array(unsigned num_images)
850 struct wim_image_metadata **imd_array;
852 DEBUG("Creating new image metadata array for %u images",
855 imd_array = CALLOC(num_images, sizeof(imd_array[0]));
857 if (imd_array == NULL) {
858 ERROR("Failed to allocate memory for %u image metadata structures",
862 for (unsigned i = 0; i < num_images; i++) {
863 imd_array[i] = new_image_metadata();
864 if (imd_array[i] == NULL) {
865 for (unsigned j = 0; j < i; j++)
866 put_image_metadata(imd_array[j], NULL);
874 /* Checksum all streams that are unhashed (other than the metadata streams),
875 * merging them into the lookup table as needed. This is a no-op unless the
876 * library has previously used to add or mount an image using the same
879 wim_checksum_unhashed_streams(WIMStruct *wim)
883 if (!wim_has_metadata(wim))
885 for (int i = 0; i < wim->hdr.image_count; i++) {
886 struct wim_lookup_table_entry *lte, *tmp;
887 struct wim_image_metadata *imd = wim->image_metadata[i];
888 image_for_each_unhashed_stream_safe(lte, tmp, imd) {
889 struct wim_lookup_table_entry *new_lte;
890 ret = hash_unhashed_stream(lte, wim->lookup_table, &new_lte);
894 free_lookup_table_entry(lte);
901 * can_modify_wim - Check if a given WIM is writeable. This is only the case if
902 * it meets the following three conditions:
904 * 1. Write access is allowed to the underlying file (if any) at the filesystem level.
905 * 2. The WIM is not part of a spanned set.
906 * 3. The WIM_HDR_FLAG_READONLY flag is not set in the WIM header.
908 * Return value is 0 if writable; WIMLIB_ERR_WIM_IS_READONLY otherwise.
911 can_modify_wim(WIMStruct *wim)
914 if (taccess(wim->filename, W_OK)) {
915 ERROR_WITH_ERRNO("Can't modify \"%"TS"\"", wim->filename);
916 return WIMLIB_ERR_WIM_IS_READONLY;
919 if (wim->hdr.total_parts != 1) {
920 ERROR("Cannot modify \"%"TS"\": is part of a split WIM",
922 return WIMLIB_ERR_WIM_IS_READONLY;
924 if (wim->hdr.flags & WIM_HDR_FLAG_READONLY) {
925 ERROR("Cannot modify \"%"TS"\": is marked read-only",
927 return WIMLIB_ERR_WIM_IS_READONLY;
933 * can_delete_from_wim - Check if files or images can be deleted from a given
936 * This theoretically should be exactly the same as can_modify_wim(), but
937 * unfortunately, due to bugs in Microsoft's software that generate incorrect
938 * reference counts for some WIM resources, we need to run expensive
939 * verifications to make sure the reference counts are correct on all WIM
940 * resources. Otherwise we might delete a WIM resource whose reference count
941 * has fallen to 0, but is actually still referenced somewhere.
944 can_delete_from_wim(WIMStruct *wim)
948 ret = can_modify_wim(wim);
951 if (!wim->refcnts_ok) {
952 ret = wim_recalculate_refcnts(wim);
959 /* API function documented in wimlib.h */
961 wimlib_free(WIMStruct *wim)
966 DEBUG("Freeing WIMStruct (filename=\"%"TS"\", image_count=%u)",
967 wim->filename, wim->hdr.image_count);
969 while (!list_empty(&wim->subwims)) {
972 subwim = list_entry(wim->subwims.next, WIMStruct, subwim_node);
973 list_del(&subwim->subwim_node);
974 DEBUG("Freeing subwim.");
978 if (filedes_valid(&wim->in_fd))
979 filedes_close(&wim->in_fd);
980 if (filedes_valid(&wim->out_fd))
981 filedes_close(&wim->out_fd);
983 free_lookup_table(wim->lookup_table);
985 wimlib_free_decompressor(wim->decompressor);
988 free_wim_info(wim->wim_info);
989 if (wim->image_metadata) {
990 for (unsigned i = 0; i < wim->hdr.image_count; i++)
991 put_image_metadata(wim->image_metadata[i], NULL);
992 FREE(wim->image_metadata);
998 test_locale_ctype_utf8(void)
1003 char *ctype = nl_langinfo(CODESET);
1005 return (!strstr(ctype, "UTF-8") ||
1006 !strstr(ctype, "UTF8") ||
1007 !strstr(ctype, "utf8") ||
1008 !strstr(ctype, "utf-8"));
1012 /* API function documented in wimlib.h */
1014 wimlib_global_init(int init_flags)
1016 static bool already_inited = false;
1021 libxml_global_init();
1022 if (!(init_flags & WIMLIB_INIT_FLAG_ASSUME_UTF8)) {
1023 wimlib_mbs_is_utf8 = test_locale_ctype_utf8();
1025 if (!wimlib_mbs_is_utf8)
1026 libntfs3g_global_init();
1030 ret = win32_global_init(init_flags);
1036 already_inited = true;
1040 /* API function documented in wimlib.h */
1042 wimlib_global_cleanup(void)
1044 libxml_global_cleanup();
1045 iconv_global_cleanup();
1047 win32_global_cleanup();
1049 cleanup_decompressor_params();
1050 cleanup_compressor_params();