2 * wim.c - High-level code dealing with WIMStructs and images.
6 * Copyright (C) 2012, 2013, 2014 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/.
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/version.h"
44 # include "wimlib/win32.h" /* for realpath() replacement */
50 # include <langinfo.h>
56 wim_default_pack_compression_type(void)
58 return WIMLIB_COMPRESSION_TYPE_LZMS;
62 wim_default_pack_chunk_size(int ctype) {
64 case WIMLIB_COMPRESSION_TYPE_LZMS:
65 return 1U << 25; /* 33554432 */
67 return 1U << 15; /* 32768 */
74 WIMStruct *wim = CALLOC(1, sizeof(WIMStruct));
78 filedes_invalidate(&wim->in_fd);
79 filedes_invalidate(&wim->out_fd);
80 wim->out_pack_compression_type = wim_default_pack_compression_type();
81 wim->out_pack_chunk_size = wim_default_pack_chunk_size(
82 wim->out_pack_compression_type);
83 INIT_LIST_HEAD(&wim->subwims);
87 /* Determine if the chunk size is valid for the specified compression type. */
89 wim_chunk_size_valid(u32 chunk_size, int ctype)
93 /* Chunk size is meaningless for uncompressed WIMs --- any value is
95 if (ctype == WIMLIB_COMPRESSION_TYPE_NONE)
98 /* Chunk size must be power of 2. */
101 order = bsr32(chunk_size);
102 if (chunk_size != 1U << order)
121 /* See the documentation for the --chunk-size option of `wimlib-imagex
122 * capture' for information about allowed chunk sizes. */
124 case WIMLIB_COMPRESSION_TYPE_LZX:
125 return order >= 15 && order <= 21;
126 case WIMLIB_COMPRESSION_TYPE_XPRESS:
127 return order >= 12 && order <= 16;
128 case WIMLIB_COMPRESSION_TYPE_LZMS:
129 return order >= 15 && order <= 30;
134 /* Return the default chunk size to use for the specified compression type.
136 * See notes above in wim_chunk_size_valid(). */
138 wim_default_chunk_size(int ctype)
141 case WIMLIB_COMPRESSION_TYPE_LZMS:
142 return 1U << 17; /* 131072 */
144 return 1U << 15; /* 32768 */
149 * Calls a function on images in the WIM. If @image is WIMLIB_ALL_IMAGES,
150 * @visitor is called on the WIM once for each image, with each image selected
151 * as the current image in turn. If @image is a certain image, @visitor is
152 * called on the WIM only once, with that image selected.
155 for_image(WIMStruct *wim, int image, int (*visitor)(WIMStruct *))
162 if (image == WIMLIB_ALL_IMAGES) {
164 end = wim->hdr.image_count;
165 } else if (image >= 1 && image <= wim->hdr.image_count) {
169 return WIMLIB_ERR_INVALID_IMAGE;
171 for (i = start; i <= end; i++) {
172 ret = select_wim_image(wim, i);
182 /* API function documented in wimlib.h */
184 wimlib_create_new_wim(int ctype, WIMStruct **wim_ret)
187 struct wim_lookup_table *table;
190 ret = wimlib_global_init(WIMLIB_INIT_FLAG_ASSUME_UTF8);
194 wim = new_wim_struct();
196 return WIMLIB_ERR_NOMEM;
198 ret = init_wim_header(&wim->hdr, ctype, wim_default_chunk_size(ctype));
202 table = new_lookup_table(9001);
204 ret = WIMLIB_ERR_NOMEM;
207 wim->lookup_table = table;
209 wim->compression_type = ctype;
210 wim->out_compression_type = ctype;
211 wim->chunk_size = wim->hdr.chunk_size;
212 wim->out_chunk_size = wim->hdr.chunk_size;
222 destroy_image_metadata(struct wim_image_metadata *imd,
223 struct wim_lookup_table *table,
224 bool free_metadata_lte)
226 free_dentry_tree(imd->root_dentry, table);
227 imd->root_dentry = NULL;
228 free_wim_security_data(imd->security_data);
229 imd->security_data = NULL;
231 if (free_metadata_lte) {
232 free_lookup_table_entry(imd->metadata_lte);
233 imd->metadata_lte = NULL;
236 struct wim_lookup_table_entry *lte, *tmp;
237 list_for_each_entry_safe(lte, tmp, &imd->unhashed_streams, unhashed_list)
238 free_lookup_table_entry(lte);
240 INIT_LIST_HEAD(&imd->unhashed_streams);
241 INIT_LIST_HEAD(&imd->inode_list);
244 do_ntfs_umount(imd->ntfs_vol);
245 imd->ntfs_vol = NULL;
251 put_image_metadata(struct wim_image_metadata *imd,
252 struct wim_lookup_table *table)
254 if (imd && --imd->refcnt == 0) {
255 destroy_image_metadata(imd, table, true);
260 /* Appends the specified image metadata structure to the array of image metadata
261 * for a WIM, and increments the image count. */
263 append_image_metadata(WIMStruct *wim, struct wim_image_metadata *imd)
265 struct wim_image_metadata **imd_array;
267 imd_array = REALLOC(wim->image_metadata,
268 sizeof(wim->image_metadata[0]) * (wim->hdr.image_count + 1));
271 return WIMLIB_ERR_NOMEM;
272 wim->image_metadata = imd_array;
273 imd_array[wim->hdr.image_count++] = imd;
277 struct wim_image_metadata *
278 new_image_metadata(void)
280 struct wim_image_metadata *imd;
282 imd = CALLOC(1, sizeof(*imd));
285 INIT_LIST_HEAD(&imd->inode_list);
286 INIT_LIST_HEAD(&imd->unhashed_streams);
291 static struct wim_image_metadata **
292 new_image_metadata_array(unsigned num_images)
294 struct wim_image_metadata **imd_array;
296 imd_array = CALLOC(num_images, sizeof(imd_array[0]));
300 for (unsigned i = 0; i < num_images; i++) {
301 imd_array[i] = new_image_metadata();
302 if (unlikely(!imd_array[i])) {
303 for (unsigned j = 0; j < i; j++)
304 put_image_metadata(imd_array[j], NULL);
314 * Load the metadata for the specified WIM image into memory and set it
315 * as the WIMStruct's currently selected image.
318 * The WIMStruct for the WIM.
320 * The 1-based index of the image in the WIM to select.
322 * On success, 0 will be returned, wim->current_image will be set to
323 * @image, and wim_get_current_image_metadata() can be used to retrieve
324 * metadata information for the image.
326 * On failure, WIMLIB_ERR_INVALID_IMAGE, WIMLIB_ERR_METADATA_NOT_FOUND,
327 * or another error code will be returned.
330 select_wim_image(WIMStruct *wim, int image)
332 struct wim_image_metadata *imd;
335 if (image == WIMLIB_NO_IMAGE)
336 return WIMLIB_ERR_INVALID_IMAGE;
338 if (image == wim->current_image)
341 if (image < 1 || image > wim->hdr.image_count)
342 return WIMLIB_ERR_INVALID_IMAGE;
344 if (!wim_has_metadata(wim))
345 return WIMLIB_ERR_METADATA_NOT_FOUND;
347 /* If a valid image is currently selected, its metadata can be freed if
348 * it has not been modified. */
349 if (wim->current_image != WIMLIB_NO_IMAGE) {
350 imd = wim_get_current_image_metadata(wim);
351 if (!imd->modified) {
352 wimlib_assert(list_empty(&imd->unhashed_streams));
353 destroy_image_metadata(imd, NULL, false);
356 wim->current_image = image;
357 imd = wim_get_current_image_metadata(wim);
358 if (imd->root_dentry || imd->modified) {
361 ret = read_metadata_resource(wim, imd);
363 wim->current_image = WIMLIB_NO_IMAGE;
369 /* API function documented in wimlib.h */
370 WIMLIBAPI const tchar *
371 wimlib_get_compression_type_string(int ctype)
374 case WIMLIB_COMPRESSION_TYPE_NONE:
376 case WIMLIB_COMPRESSION_TYPE_XPRESS:
378 case WIMLIB_COMPRESSION_TYPE_LZX:
380 case WIMLIB_COMPRESSION_TYPE_LZMS:
387 /* API function documented in wimlib.h */
389 wimlib_resolve_image(WIMStruct *wim, const tchar *image_name_or_num)
395 if (!image_name_or_num || !*image_name_or_num)
396 return WIMLIB_NO_IMAGE;
398 if (!tstrcasecmp(image_name_or_num, T("all"))
399 || !tstrcasecmp(image_name_or_num, T("*")))
400 return WIMLIB_ALL_IMAGES;
401 image = tstrtol(image_name_or_num, &p, 10);
402 if (p != image_name_or_num && *p == T('\0') && image > 0) {
403 if (image > wim->hdr.image_count)
404 return WIMLIB_NO_IMAGE;
407 for (i = 1; i <= wim->hdr.image_count; i++) {
408 if (!tstrcmp(image_name_or_num,
409 wimlib_get_image_name(wim, i)))
412 return WIMLIB_NO_IMAGE;
416 /* API function documented in wimlib.h */
418 wimlib_print_available_images(const WIMStruct *wim, int image)
424 if (image == WIMLIB_ALL_IMAGES) {
425 n = tprintf(T("Available Images:\n"));
427 last = wim->hdr.image_count;
428 } else if (image >= 1 && image <= wim->hdr.image_count) {
429 n = tprintf(T("Information for Image %d\n"), image);
433 tprintf(T("wimlib_print_available_images(): Invalid image %d"),
437 for (i = 0; i < n - 1; i++)
440 for (i = first; i <= last; i++)
441 print_image_info(wim->wim_info, i);
444 /* API function documented in wimlib.h */
446 wimlib_get_wim_info(WIMStruct *wim, struct wimlib_wim_info *info)
448 memset(info, 0, sizeof(struct wimlib_wim_info));
449 memcpy(info->guid, wim->hdr.guid, WIMLIB_GUID_LEN);
450 info->image_count = wim->hdr.image_count;
451 info->boot_index = wim->hdr.boot_idx;
452 info->wim_version = wim->hdr.wim_version;
453 info->chunk_size = wim->chunk_size;
454 info->part_number = wim->hdr.part_number;
455 info->total_parts = wim->hdr.total_parts;
456 info->compression_type = wim->compression_type;
457 info->total_bytes = wim_info_get_total_bytes(wim->wim_info);
458 info->has_integrity_table = wim_has_integrity_table(wim);
459 info->opened_from_file = (wim->filename != NULL);
460 info->is_readonly = (wim->hdr.flags & WIM_HDR_FLAG_READONLY) ||
461 (wim->hdr.total_parts != 1) ||
462 (wim->filename && taccess(wim->filename, W_OK));
463 info->has_rpfix = (wim->hdr.flags & WIM_HDR_FLAG_RP_FIX) != 0;
464 info->is_marked_readonly = (wim->hdr.flags & WIM_HDR_FLAG_READONLY) != 0;
465 info->write_in_progress = (wim->hdr.flags & WIM_HDR_FLAG_WRITE_IN_PROGRESS) != 0;
466 info->metadata_only = (wim->hdr.flags & WIM_HDR_FLAG_METADATA_ONLY) != 0;
467 info->resource_only = (wim->hdr.flags & WIM_HDR_FLAG_RESOURCE_ONLY) != 0;
468 info->spanned = (wim->hdr.flags & WIM_HDR_FLAG_SPANNED) != 0;
469 info->pipable = wim_is_pipable(wim);
473 /* API function documented in wimlib.h */
475 wimlib_set_wim_info(WIMStruct *wim, const struct wimlib_wim_info *info, int which)
479 if (which & ~(WIMLIB_CHANGE_READONLY_FLAG |
481 WIMLIB_CHANGE_BOOT_INDEX |
482 WIMLIB_CHANGE_RPFIX_FLAG))
483 return WIMLIB_ERR_INVALID_PARAM;
485 if (which & WIMLIB_CHANGE_READONLY_FLAG) {
486 if (info->is_marked_readonly)
487 wim->hdr.flags |= WIM_HDR_FLAG_READONLY;
489 wim->hdr.flags &= ~WIM_HDR_FLAG_READONLY;
492 if ((which & ~WIMLIB_CHANGE_READONLY_FLAG) == 0)
495 ret = can_modify_wim(wim);
499 if (which & WIMLIB_CHANGE_GUID)
500 memcpy(wim->hdr.guid, info->guid, WIM_GUID_LEN);
502 if (which & WIMLIB_CHANGE_BOOT_INDEX) {
503 if (info->boot_index > wim->hdr.image_count)
504 return WIMLIB_ERR_INVALID_IMAGE;
505 wim->hdr.boot_idx = info->boot_index;
508 if (which & WIMLIB_CHANGE_RPFIX_FLAG) {
510 wim->hdr.flags |= WIM_HDR_FLAG_RP_FIX;
512 wim->hdr.flags &= ~WIM_HDR_FLAG_RP_FIX;
518 set_out_ctype(int ctype, u8 *out_ctype_p)
521 case WIMLIB_COMPRESSION_TYPE_NONE:
522 case WIMLIB_COMPRESSION_TYPE_LZX:
523 case WIMLIB_COMPRESSION_TYPE_XPRESS:
524 case WIMLIB_COMPRESSION_TYPE_LZMS:
525 *out_ctype_p = ctype;
528 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
531 /* API function documented in wimlib.h */
533 wimlib_set_output_compression_type(WIMStruct *wim, int ctype)
535 int ret = set_out_ctype(ctype, &wim->out_compression_type);
539 /* Reset the chunk size if it's no longer valid. */
540 if (!wim_chunk_size_valid(wim->out_chunk_size, ctype))
541 wim->out_chunk_size = wim_default_chunk_size(ctype);
545 /* API function documented in wimlib.h */
547 wimlib_set_output_pack_compression_type(WIMStruct *wim, int ctype)
549 int ret = set_out_ctype(ctype, &wim->out_pack_compression_type);
553 /* Reset the chunk size if it's no longer valid. */
554 if (!wim_chunk_size_valid(wim->out_pack_chunk_size, ctype))
555 wim->out_pack_chunk_size = wim_default_pack_chunk_size(ctype);
560 set_out_chunk_size(u32 chunk_size, int ctype, u32 *out_chunk_size_p)
562 if (!wim_chunk_size_valid(chunk_size, ctype))
563 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
565 *out_chunk_size_p = chunk_size;
569 /* API function documented in wimlib.h */
571 wimlib_set_output_chunk_size(WIMStruct *wim, uint32_t chunk_size)
573 if (chunk_size == 0) {
574 wim->out_chunk_size =
575 wim_default_chunk_size(wim->out_compression_type);
579 return set_out_chunk_size(chunk_size,
580 wim->out_compression_type,
581 &wim->out_chunk_size);
584 /* API function documented in wimlib.h */
586 wimlib_set_output_pack_chunk_size(WIMStruct *wim, uint32_t chunk_size)
588 if (chunk_size == 0) {
589 wim->out_pack_chunk_size =
590 wim_default_pack_chunk_size(wim->out_pack_compression_type);
594 return set_out_chunk_size(chunk_size,
595 wim->out_pack_compression_type,
596 &wim->out_pack_chunk_size);
600 wimlib_register_progress_function(WIMStruct *wim,
601 wimlib_progress_func_t progfunc,
604 wim->progfunc = progfunc;
605 wim->progctx = progctx;
609 open_wim_file(const tchar *filename, struct filedes *fd_ret)
613 raw_fd = topen(filename, O_RDONLY | O_BINARY);
615 ERROR_WITH_ERRNO("Can't open \"%"TS"\" read-only", filename);
616 return WIMLIB_ERR_OPEN;
618 filedes_init(fd_ret, raw_fd);
623 * Begins the reading of a WIM file; opens the file and reads its header and
624 * lookup table, and optionally checks the integrity.
627 begin_read(WIMStruct *wim, const void *wim_filename_or_fd, int open_flags)
631 const tchar *wimfile;
633 if (open_flags & WIMLIB_OPEN_FLAG_FROM_PIPE) {
635 filedes_init(&wim->in_fd, *(const int*)wim_filename_or_fd);
636 wim->in_fd.is_pipe = 1;
638 wimfile = wim_filename_or_fd;
639 ret = open_wim_file(wimfile, &wim->in_fd);
643 /* The absolute path to the WIM is requested so that
644 * wimlib_overwrite() still works even if the process changes
645 * its working directory. This actually happens if a WIM is
646 * mounted read-write, since the FUSE thread changes directory
647 * to "/", and it needs to be able to find the WIM file again.
649 * This will break if the full path to the WIM changes in the
650 * intervening time...
652 * Warning: in Windows native builds, realpath() calls the
653 * replacement function in win32_replacements.c.
655 wim->filename = realpath(wimfile, NULL);
656 if (!wim->filename) {
657 ERROR_WITH_ERRNO("Failed to get full path to file "
658 "\"%"TS"\"", wimfile);
660 return WIMLIB_ERR_NOMEM;
662 return WIMLIB_ERR_NO_FILENAME;
666 ret = read_wim_header(wim, &wim->hdr);
670 if (wim->hdr.flags & WIM_HDR_FLAG_WRITE_IN_PROGRESS) {
671 WARNING("The WIM_HDR_FLAG_WRITE_IN_PROGRESS flag is set in the header of\n"
672 " \"%"TS"\". It may be being changed by another process,\n"
673 " or a process may have crashed while writing the WIM.",
677 if (open_flags & WIMLIB_OPEN_FLAG_WRITE_ACCESS) {
678 ret = can_modify_wim(wim);
683 if ((open_flags & WIMLIB_OPEN_FLAG_ERROR_IF_SPLIT) &&
684 (wim->hdr.total_parts != 1))
685 return WIMLIB_ERR_IS_SPLIT_WIM;
687 /* If the boot index is invalid, print a warning and set it to 0 */
688 if (wim->hdr.boot_idx > wim->hdr.image_count) {
689 WARNING("Ignoring invalid boot index.");
690 wim->hdr.boot_idx = 0;
693 /* Check and cache the compression type */
694 if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESSION) {
695 if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESS_LZX) {
696 wim->compression_type = WIMLIB_COMPRESSION_TYPE_LZX;
697 } else if (wim->hdr.flags & (WIM_HDR_FLAG_COMPRESS_XPRESS |
698 WIM_HDR_FLAG_COMPRESS_XPRESS_2)) {
699 wim->compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
700 } else if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESS_LZMS) {
701 wim->compression_type = WIMLIB_COMPRESSION_TYPE_LZMS;
703 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
706 wim->compression_type = WIMLIB_COMPRESSION_TYPE_NONE;
708 wim->out_compression_type = wim->compression_type;
710 /* Check and cache the chunk size. */
711 wim->chunk_size = wim->hdr.chunk_size;
712 wim->out_chunk_size = wim->chunk_size;
713 if (!wim_chunk_size_valid(wim->chunk_size, wim->compression_type)) {
714 ERROR("Invalid chunk size (%"PRIu32" bytes) "
715 "for compression type %"TS"!", wim->chunk_size,
716 wimlib_get_compression_type_string(wim->compression_type));
717 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
720 if (open_flags & WIMLIB_OPEN_FLAG_CHECK_INTEGRITY) {
721 ret = check_wim_integrity(wim);
722 if (ret == WIM_INTEGRITY_NONEXISTENT) {
723 WARNING("\"%"TS"\" does not contain integrity "
724 "information. Skipping integrity check.",
726 } else if (ret == WIM_INTEGRITY_NOT_OK) {
727 return WIMLIB_ERR_INTEGRITY;
728 } else if (ret != WIM_INTEGRITY_OK) {
733 if (wim->hdr.image_count != 0 && wim->hdr.part_number == 1) {
734 wim->image_metadata = new_image_metadata_array(wim->hdr.image_count);
735 if (!wim->image_metadata)
736 return WIMLIB_ERR_NOMEM;
739 if (open_flags & WIMLIB_OPEN_FLAG_FROM_PIPE) {
740 wim->lookup_table = new_lookup_table(9001);
741 if (!wim->lookup_table)
742 return WIMLIB_ERR_NOMEM;
745 ret = read_wim_xml_data(wim);
749 xml_num_images = wim_info_get_num_images(wim->wim_info);
750 if (xml_num_images != wim->hdr.image_count) {
751 ERROR("The WIM's header is inconsistent with its XML data.\n"
752 " Please submit a bug report if you believe this "
753 "WIM file should be considered valid.");
754 return WIMLIB_ERR_IMAGE_COUNT;
757 ret = read_wim_lookup_table(wim);
765 open_wim_as_WIMStruct(const void *wim_filename_or_fd, int open_flags,
767 wimlib_progress_func_t progfunc, void *progctx)
772 ret = wimlib_global_init(WIMLIB_INIT_FLAG_ASSUME_UTF8);
776 wim = new_wim_struct();
778 return WIMLIB_ERR_NOMEM;
780 wim->progfunc = progfunc;
781 wim->progctx = progctx;
783 ret = begin_read(wim, wim_filename_or_fd, open_flags);
793 /* API function documented in wimlib.h */
795 wimlib_open_wim_with_progress(const tchar *wimfile, int open_flags,
797 wimlib_progress_func_t progfunc, void *progctx)
799 if (open_flags & ~(WIMLIB_OPEN_FLAG_CHECK_INTEGRITY |
800 WIMLIB_OPEN_FLAG_ERROR_IF_SPLIT |
801 WIMLIB_OPEN_FLAG_WRITE_ACCESS))
802 return WIMLIB_ERR_INVALID_PARAM;
804 if (!wimfile || !*wimfile || !wim_ret)
805 return WIMLIB_ERR_INVALID_PARAM;
807 return open_wim_as_WIMStruct(wimfile, open_flags, wim_ret,
811 /* API function documented in wimlib.h */
813 wimlib_open_wim(const tchar *wimfile, int open_flags, WIMStruct **wim_ret)
815 return wimlib_open_wim_with_progress(wimfile, open_flags, wim_ret,
819 /* Checksum all streams that are unhashed (other than the metadata streams),
820 * merging them into the lookup table as needed. This is a no-op unless the
821 * library has previously used to add or mount an image using the same
824 wim_checksum_unhashed_streams(WIMStruct *wim)
828 if (!wim_has_metadata(wim))
830 for (int i = 0; i < wim->hdr.image_count; i++) {
831 struct wim_lookup_table_entry *lte, *tmp;
832 struct wim_image_metadata *imd = wim->image_metadata[i];
833 image_for_each_unhashed_stream_safe(lte, tmp, imd) {
834 struct wim_lookup_table_entry *new_lte;
835 ret = hash_unhashed_stream(lte, wim->lookup_table, &new_lte);
839 free_lookup_table_entry(lte);
846 * can_modify_wim - Check if a given WIM is writeable. This is only the case if
847 * it meets the following three conditions:
849 * 1. Write access is allowed to the underlying file (if any) at the filesystem level.
850 * 2. The WIM is not part of a spanned set.
851 * 3. The WIM_HDR_FLAG_READONLY flag is not set in the WIM header.
853 * Return value is 0 if writable; WIMLIB_ERR_WIM_IS_READONLY otherwise.
856 can_modify_wim(WIMStruct *wim)
859 if (taccess(wim->filename, W_OK)) {
860 ERROR_WITH_ERRNO("Can't modify \"%"TS"\"", wim->filename);
861 return WIMLIB_ERR_WIM_IS_READONLY;
864 if (wim->hdr.total_parts != 1) {
865 ERROR("Cannot modify \"%"TS"\": is part of a split WIM",
867 return WIMLIB_ERR_WIM_IS_READONLY;
869 if (wim->hdr.flags & WIM_HDR_FLAG_READONLY) {
870 ERROR("Cannot modify \"%"TS"\": is marked read-only",
872 return WIMLIB_ERR_WIM_IS_READONLY;
878 * can_delete_from_wim - Check if files or images can be deleted from a given
881 * This theoretically should be exactly the same as can_modify_wim(), but
882 * unfortunately, due to bugs in Microsoft's software that generate incorrect
883 * reference counts for some WIM resources, we need to run expensive
884 * verifications to make sure the reference counts are correct on all WIM
885 * resources. Otherwise we might delete a WIM resource whose reference count
886 * has fallen to 0, but is actually still referenced somewhere.
889 can_delete_from_wim(WIMStruct *wim)
893 ret = can_modify_wim(wim);
896 if (!wim->refcnts_ok) {
897 ret = wim_recalculate_refcnts(wim);
904 /* API function documented in wimlib.h */
906 wimlib_free(WIMStruct *wim)
911 while (!list_empty(&wim->subwims)) {
914 subwim = list_entry(wim->subwims.next, WIMStruct, subwim_node);
915 list_del(&subwim->subwim_node);
919 if (filedes_valid(&wim->in_fd))
920 filedes_close(&wim->in_fd);
921 if (filedes_valid(&wim->out_fd))
922 filedes_close(&wim->out_fd);
924 free_lookup_table(wim->lookup_table);
926 wimlib_free_decompressor(wim->decompressor);
929 free_wim_info(wim->wim_info);
930 if (wim->image_metadata) {
931 for (unsigned i = 0; i < wim->hdr.image_count; i++)
932 put_image_metadata(wim->image_metadata[i], NULL);
933 FREE(wim->image_metadata);
939 test_locale_ctype_utf8(void)
944 char *ctype = nl_langinfo(CODESET);
946 return (!strstr(ctype, "UTF-8") ||
947 !strstr(ctype, "UTF8") ||
948 !strstr(ctype, "utf8") ||
949 !strstr(ctype, "utf-8"));
953 /* API function documented in wimlib.h */
955 wimlib_get_version(void)
957 return WIMLIB_VERSION_CODE;
960 static bool lib_initialized = false;
962 /* API function documented in wimlib.h */
964 wimlib_global_init(int init_flags)
969 #ifdef ENABLE_ERROR_MESSAGES
970 if (!wimlib_error_file)
971 wimlib_error_file = stderr;
974 if (init_flags & ~(WIMLIB_INIT_FLAG_ASSUME_UTF8 |
975 WIMLIB_INIT_FLAG_DONT_ACQUIRE_PRIVILEGES |
976 WIMLIB_INIT_FLAG_STRICT_CAPTURE_PRIVILEGES |
977 WIMLIB_INIT_FLAG_STRICT_APPLY_PRIVILEGES |
978 WIMLIB_INIT_FLAG_DEFAULT_CASE_SENSITIVE |
979 WIMLIB_INIT_FLAG_DEFAULT_CASE_INSENSITIVE))
980 return WIMLIB_ERR_INVALID_PARAM;
982 libxml_global_init();
983 if (!(init_flags & WIMLIB_INIT_FLAG_ASSUME_UTF8)) {
984 wimlib_mbs_is_utf8 = test_locale_ctype_utf8();
986 if (!wimlib_mbs_is_utf8)
987 libntfs3g_global_init();
992 int ret = win32_global_init(init_flags);
999 if (init_flags & WIMLIB_INIT_FLAG_DEFAULT_CASE_SENSITIVE)
1000 default_ignore_case = false;
1001 else if (init_flags & WIMLIB_INIT_FLAG_DEFAULT_CASE_INSENSITIVE)
1002 default_ignore_case = true;
1003 lib_initialized = true;
1007 /* API function documented in wimlib.h */
1009 wimlib_global_cleanup(void)
1011 if (!lib_initialized)
1013 libxml_global_cleanup();
1014 iconv_global_cleanup();
1016 win32_global_cleanup();
1019 wimlib_set_error_file(NULL);
1020 lib_initialized = false;