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,
67 image_print_files(WIMStruct *wim)
69 return for_dentry_in_tree(wim_root_dentry(wim), print_dentry_full_path,
76 WIMStruct *wim = CALLOC(1, sizeof(WIMStruct));
85 * Calls a function on images in the WIM. If @image is WIMLIB_ALL_IMAGES, @visitor
86 * is called on the WIM once for each image, with each image selected as the
87 * current image in turn. If @image is a certain image, @visitor is called on
88 * the WIM only once, with that image selected.
91 for_image(WIMStruct *wim, int image, int (*visitor)(WIMStruct *))
98 if (image == WIMLIB_ALL_IMAGES) {
100 end = wim->hdr.image_count;
101 } else if (image >= 1 && image <= wim->hdr.image_count) {
105 return WIMLIB_ERR_INVALID_IMAGE;
107 for (i = start; i <= end; i++) {
108 ret = select_wim_image(wim, i);
118 /* API function documented in wimlib.h */
120 wimlib_create_new_wim(int ctype, WIMStruct **wim_ret)
123 struct wim_lookup_table *table;
126 wimlib_global_init(WIMLIB_INIT_FLAG_ASSUME_UTF8);
128 DEBUG("Creating new WIM with %"TS" compression.",
129 wimlib_get_compression_type_string(ctype));
131 /* Allocate the WIMStruct. */
132 wim = new_wim_struct();
134 return WIMLIB_ERR_NOMEM;
136 ret = init_wim_header(&wim->hdr, ctype);
140 table = new_lookup_table(9001);
142 ret = WIMLIB_ERR_NOMEM;
145 wim->lookup_table = table;
147 wim->compression_type = ctype;
156 select_wim_image(WIMStruct *wim, int image)
158 struct wim_image_metadata *imd;
161 DEBUG("Selecting image %d", image);
163 if (image == WIMLIB_NO_IMAGE) {
164 ERROR("Invalid image: %d", WIMLIB_NO_IMAGE);
165 return WIMLIB_ERR_INVALID_IMAGE;
168 if (image == wim->current_image)
171 if (image < 1 || image > wim->hdr.image_count) {
172 ERROR("Cannot select image %d: There are only %u images",
173 image, wim->hdr.image_count);
174 return WIMLIB_ERR_INVALID_IMAGE;
177 if (wim->hdr.part_number != 1) {
178 ERROR("Cannot select an image from a non-first part of a split WIM");
179 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
182 /* If a valid image is currently selected, it can be freed if it is not
184 if (wim->current_image != WIMLIB_NO_IMAGE) {
185 imd = wim_get_current_image_metadata(wim);
186 if (!imd->modified) {
187 wimlib_assert(list_empty(&imd->unhashed_streams));
188 DEBUG("Freeing image %u", wim->current_image);
189 destroy_image_metadata(imd, NULL, false);
192 wim->current_image = image;
193 imd = wim_get_current_image_metadata(wim);
194 if (imd->root_dentry || imd->modified) {
198 DEBUG("Reading metadata resource specified by the following "
199 "lookup table entry:");
200 print_lookup_table_entry(imd->metadata_lte, stderr);
202 ret = read_metadata_resource(wim, imd);
204 wim->current_image = WIMLIB_NO_IMAGE;
210 /* API function documented in wimlib.h */
211 WIMLIBAPI const tchar *
212 wimlib_get_compression_type_string(int ctype)
215 case WIMLIB_COMPRESSION_TYPE_NONE:
217 case WIMLIB_COMPRESSION_TYPE_LZX:
219 case WIMLIB_COMPRESSION_TYPE_XPRESS:
226 /* API function documented in wimlib.h */
228 wimlib_resolve_image(WIMStruct *wim, const tchar *image_name_or_num)
234 if (!image_name_or_num || !*image_name_or_num)
235 return WIMLIB_NO_IMAGE;
237 if (!tstrcasecmp(image_name_or_num, T("all"))
238 || !tstrcasecmp(image_name_or_num, T("*")))
239 return WIMLIB_ALL_IMAGES;
240 image = tstrtol(image_name_or_num, &p, 10);
241 if (p != image_name_or_num && *p == T('\0') && image > 0) {
242 if (image > wim->hdr.image_count)
243 return WIMLIB_NO_IMAGE;
246 for (i = 1; i <= wim->hdr.image_count; i++) {
247 if (!tstrcmp(image_name_or_num,
248 wimlib_get_image_name(wim, i)))
251 return WIMLIB_NO_IMAGE;
255 /* API function documented in wimlib.h */
257 wimlib_print_wim_information(const WIMStruct *wim)
259 struct wimlib_wim_info info;
261 wimlib_get_wim_info((WIMStruct*)wim, &info);
263 tputs(T("WIM Information:"));
264 tputs(T("----------------"));
265 tprintf(T("Path: %"TS"\n"), wim->filename);
266 tfputs(T("GUID: 0x"), stdout);
267 print_byte_field(info.guid, WIM_GID_LEN, stdout);
269 tprintf(T("Image Count: %d\n"), info.image_count);
270 tprintf(T("Compression: %"TS"\n"),
271 wimlib_get_compression_type_string(info.compression_type));
272 tprintf(T("Part Number: %d/%d\n"), info.part_number, info.total_parts);
273 tprintf(T("Boot Index: %d\n"), info.boot_index);
274 tprintf(T("Size: %"PRIu64" bytes\n"), info.total_bytes);
275 tprintf(T("Integrity Info: %"TS"\n"),
276 info.has_integrity_table ? T("yes") : T("no"));
277 tprintf(T("Relative path junction: %"TS"\n"),
278 info.has_rpfix ? T("yes") : T("no"));
282 /* API function documented in wimlib.h */
284 wimlib_print_available_images(const WIMStruct *wim, int image)
290 if (image == WIMLIB_ALL_IMAGES) {
291 n = tprintf(T("Available Images:\n"));
293 last = wim->hdr.image_count;
294 } else if (image >= 1 && image <= wim->hdr.image_count) {
295 n = tprintf(T("Information for Image %d\n"), image);
299 tprintf(T("wimlib_print_available_images(): Invalid image %d"),
303 for (i = 0; i < n - 1; i++)
306 for (i = first; i <= last; i++)
307 print_image_info(wim->wim_info, i);
311 /* API function documented in wimlib.h */
313 wimlib_print_metadata(WIMStruct *wim, int image)
315 if (wim->hdr.part_number != 1) {
316 ERROR("Cannot show the metadata from part %hu of a %hu-part split WIM!",
317 wim->hdr.part_number, wim->hdr.total_parts);
318 ERROR("Select the first part of the split WIM to see the metadata.");
319 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
321 return for_image(wim, image, image_print_metadata);
324 /* API function documented in wimlib.h */
326 wimlib_print_files(WIMStruct *wim, int image)
328 if (wim->hdr.part_number != 1) {
329 ERROR("Cannot list the files from part %hu of a %hu-part split WIM!",
330 wim->hdr.part_number, wim->hdr.total_parts);
331 ERROR("Select the first part of the split WIM if you'd like to list the files.");
332 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
334 return for_image(wim, image, image_print_files);
337 /* API function documented in wimlib.h */
339 wimlib_get_wim_info(WIMStruct *wim, struct wimlib_wim_info *info)
341 memset(info, 0, sizeof(struct wimlib_wim_info));
342 memcpy(info->guid, wim->hdr.guid, WIMLIB_GUID_LEN);
343 info->image_count = wim->hdr.image_count;
344 info->boot_index = wim->hdr.boot_idx;
345 info->wim_version = WIM_VERSION;
346 info->chunk_size = WIM_CHUNK_SIZE;
347 info->part_number = wim->hdr.part_number;
348 info->total_parts = wim->hdr.total_parts;
349 info->compression_type = wim->compression_type;
350 info->total_bytes = wim_info_get_total_bytes(wim->wim_info);
351 info->has_integrity_table = wim_has_integrity_table(wim);
352 info->opened_from_file = (wim->filename != NULL);
353 info->is_readonly = (wim->hdr.flags & WIM_HDR_FLAG_READONLY) ||
354 (wim->hdr.total_parts != 1) ||
355 (wim->filename && taccess(wim->filename, W_OK));
356 info->has_rpfix = (wim->hdr.flags & WIM_HDR_FLAG_RP_FIX) != 0;
357 info->is_marked_readonly = (wim->hdr.flags & WIM_HDR_FLAG_READONLY) != 0;
358 info->write_in_progress = (wim->hdr.flags & WIM_HDR_FLAG_WRITE_IN_PROGRESS) != 0;
359 info->metadata_only = (wim->hdr.flags & WIM_HDR_FLAG_METADATA_ONLY) != 0;
360 info->resource_only = (wim->hdr.flags & WIM_HDR_FLAG_RESOURCE_ONLY) != 0;
361 info->spanned = (wim->hdr.flags & WIM_HDR_FLAG_SPANNED) != 0;
362 info->pipable = wim_is_pipable(wim);
367 /* API function documented in wimlib.h */
369 wimlib_get_boot_idx(const WIMStruct *wim)
371 struct wimlib_wim_info info;
373 wimlib_get_wim_info((WIMStruct*)wim, &info);
374 return info.boot_index;
377 /* API function documented in wimlib.h */
379 wimlib_get_compression_type(const WIMStruct *wim)
381 struct wimlib_wim_info info;
383 wimlib_get_wim_info((WIMStruct*)wim, &info);
384 return info.compression_type;
387 /* API function documented in wimlib.h */
389 wimlib_get_num_images(const WIMStruct *wim)
391 struct wimlib_wim_info info;
393 wimlib_get_wim_info((WIMStruct*)wim, &info);
394 return info.image_count;
397 /* API function documented in wimlib.h */
399 wimlib_get_part_number(const WIMStruct *wim, int *total_parts_ret)
401 struct wimlib_wim_info info;
403 wimlib_get_wim_info((WIMStruct*)wim, &info);
405 *total_parts_ret = info.total_parts;
406 return info.part_number;
409 /* API function documented in wimlib.h */
411 wimlib_has_integrity_table(const WIMStruct *wim)
413 struct wimlib_wim_info info;
415 wimlib_get_wim_info((WIMStruct*)wim, &info);
416 return info.has_integrity_table;
419 /* API function documented in wimlib.h */
421 wimlib_set_wim_info(WIMStruct *wim, const struct wimlib_wim_info *info, int which)
425 if (which & WIMLIB_CHANGE_READONLY_FLAG) {
426 if (info->is_marked_readonly)
427 wim->hdr.flags |= WIM_HDR_FLAG_READONLY;
429 wim->hdr.flags &= ~WIM_HDR_FLAG_READONLY;
432 if ((which & ~WIMLIB_CHANGE_READONLY_FLAG) == 0)
435 ret = can_modify_wim(wim);
439 if (which & WIMLIB_CHANGE_GUID)
440 memcpy(wim->hdr.guid, info->guid, WIM_GID_LEN);
442 if (which & WIMLIB_CHANGE_BOOT_INDEX) {
443 if (info->boot_index > wim->hdr.image_count) {
444 ERROR("%u is not 0 or a valid image in the WIM to mark as bootable",
446 return WIMLIB_ERR_INVALID_IMAGE;
448 wim->hdr.boot_idx = info->boot_index;
451 if (which & WIMLIB_CHANGE_RPFIX_FLAG) {
453 wim->hdr.flags |= WIM_HDR_FLAG_RP_FIX;
455 wim->hdr.flags &= ~WIM_HDR_FLAG_RP_FIX;
460 /* API function documented in wimlib.h */
462 wimlib_set_boot_idx(WIMStruct *wim, int boot_idx)
464 struct wimlib_wim_info info;
466 info.boot_index = boot_idx;
467 return wimlib_set_wim_info(wim, &info, WIMLIB_CHANGE_BOOT_INDEX);
471 do_open_wim(const tchar *filename, struct filedes *fd_ret)
475 raw_fd = topen(filename, O_RDONLY | O_BINARY);
477 ERROR_WITH_ERRNO("Can't open \"%"TS"\" read-only", filename);
478 return WIMLIB_ERR_OPEN;
480 filedes_init(fd_ret, raw_fd);
485 reopen_wim(WIMStruct *wim)
487 wimlib_assert(!filedes_valid(&wim->in_fd));
488 return do_open_wim(wim->filename, &wim->in_fd);
492 close_wim(WIMStruct *wim)
494 if (filedes_valid(&wim->in_fd)) {
495 filedes_close(&wim->in_fd);
496 filedes_invalidate(&wim->in_fd);
502 * Begins the reading of a WIM file; opens the file and reads its header and
503 * lookup table, and optionally checks the integrity.
506 begin_read(WIMStruct *wim, const void *wim_filename_or_fd,
507 int open_flags, wimlib_progress_func_t progress_func)
511 const tchar *wimfile;
513 if (open_flags & WIMLIB_OPEN_FLAG_FROM_PIPE) {
515 filedes_init(&wim->in_fd, *(const int*)wim_filename_or_fd);
516 wim->in_fd.is_pipe = 1;
518 wimfile = wim_filename_or_fd;
519 DEBUG("Reading the WIM file `%"TS"'", wimfile);
520 ret = do_open_wim(wimfile, &wim->in_fd);
524 /* The absolute path to the WIM is requested so that
525 * wimlib_overwrite() still works even if the process changes
526 * its working directory. This actually happens if a WIM is
527 * mounted read-write, since the FUSE thread changes directory
528 * to "/", and it needs to be able to find the WIM file again.
530 * This will break if the full path to the WIM changes in the
531 * intervening time...
533 * Warning: in Windows native builds, realpath() calls the
534 * replacement function in win32.c.
536 wim->filename = realpath(wimfile, NULL);
537 if (!wim->filename) {
538 ERROR_WITH_ERRNO("Failed to resolve WIM filename");
540 return WIMLIB_ERR_NOMEM;
542 return WIMLIB_ERR_OPEN;
546 ret = read_wim_header(wim->filename, &wim->in_fd, &wim->hdr);
550 if (wim->hdr.flags & WIM_HDR_FLAG_WRITE_IN_PROGRESS) {
551 WARNING("The WIM_HDR_FLAG_WRITE_IN_PROGRESS is set in the header of \"%"TS"\".\n"
552 " It may be being changed by another process, or a process\n"
553 " may have crashed while writing the WIM.", wimfile);
556 if (open_flags & WIMLIB_OPEN_FLAG_WRITE_ACCESS) {
557 ret = can_modify_wim(wim);
562 if (wim->hdr.total_parts != 1 && !(open_flags & WIMLIB_OPEN_FLAG_SPLIT_OK)) {
563 ERROR("\"%"TS"\": This WIM is part %u of a %u-part WIM",
564 wimfile, wim->hdr.part_number, wim->hdr.total_parts);
565 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
568 DEBUG("According to header, WIM contains %u images", wim->hdr.image_count);
570 /* If the boot index is invalid, print a warning and set it to 0 */
571 if (wim->hdr.boot_idx > wim->hdr.image_count) {
572 WARNING("In `%"TS"', image %u is marked as bootable, "
573 "but there are only %u images in the WIM",
574 wimfile, wim->hdr.boot_idx, wim->hdr.image_count);
575 wim->hdr.boot_idx = 0;
578 /* Check and cache the compression type */
579 if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESSION) {
580 if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESS_LZX) {
581 if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESS_XPRESS) {
582 ERROR("Multiple compression flags are set in \"%"TS"\"",
584 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
586 wim->compression_type = WIMLIB_COMPRESSION_TYPE_LZX;
587 } else if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESS_XPRESS) {
588 wim->compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
590 ERROR("The compression flag is set on \"%"TS"\", but "
591 "neither the XPRESS nor LZX flag is set",
593 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
596 wim->compression_type = WIMLIB_COMPRESSION_TYPE_NONE;
599 if (open_flags & WIMLIB_OPEN_FLAG_CHECK_INTEGRITY) {
600 ret = check_wim_integrity(wim, progress_func);
601 if (ret == WIM_INTEGRITY_NONEXISTENT) {
602 WARNING("No integrity information for `%"TS"'; skipping "
603 "integrity check.", wimfile);
604 } else if (ret == WIM_INTEGRITY_NOT_OK) {
605 ERROR("WIM is not intact! (Failed integrity check)");
606 return WIMLIB_ERR_INTEGRITY;
607 } else if (ret != WIM_INTEGRITY_OK) {
612 if (wim->hdr.image_count != 0 && wim->hdr.part_number == 1) {
613 wim->image_metadata = new_image_metadata_array(wim->hdr.image_count);
614 if (!wim->image_metadata)
615 return WIMLIB_ERR_NOMEM;
618 if (open_flags & WIMLIB_OPEN_FLAG_FROM_PIPE) {
619 wim->lookup_table = new_lookup_table(9001);
620 if (!wim->lookup_table)
621 return WIMLIB_ERR_NOMEM;
623 ret = read_wim_lookup_table(wim);
627 ret = read_wim_xml_data(wim);
631 xml_num_images = wim_info_get_num_images(wim->wim_info);
632 if (xml_num_images != wim->hdr.image_count) {
633 ERROR("In the file `%"TS"', there are %u <IMAGE> elements "
634 "in the XML data,", wimfile, xml_num_images);
635 ERROR("but %u images in the WIM! There must be exactly one "
636 "<IMAGE> element per image.", wim->hdr.image_count);
637 return WIMLIB_ERR_IMAGE_COUNT;
639 DEBUG("Done beginning read of WIM file `%"TS"'.", wimfile);
645 open_wim_as_WIMStruct(const void *wim_filename_or_fd, int open_flags,
646 WIMStruct **wim_ret, wimlib_progress_func_t progress_func)
651 wimlib_global_init(WIMLIB_INIT_FLAG_ASSUME_UTF8);
653 ret = WIMLIB_ERR_INVALID_PARAM;
657 ret = WIMLIB_ERR_NOMEM;
658 wim = new_wim_struct();
662 ret = begin_read(wim, wim_filename_or_fd, open_flags, progress_func);
664 goto out_wimlib_free;
675 /* API function documented in wimlib.h */
677 wimlib_open_wim(const tchar *wimfile, int open_flags,
678 WIMStruct **wim_ret, wimlib_progress_func_t progress_func)
680 open_flags &= WIMLIB_OPEN_MASK_PUBLIC;
681 return open_wim_as_WIMStruct(wimfile, open_flags, wim_ret,
686 destroy_image_metadata(struct wim_image_metadata *imd,
687 struct wim_lookup_table *table,
688 bool free_metadata_lte)
690 free_dentry_tree(imd->root_dentry, table);
691 imd->root_dentry = NULL;
692 free_wim_security_data(imd->security_data);
693 imd->security_data = NULL;
695 if (free_metadata_lte) {
696 free_lookup_table_entry(imd->metadata_lte);
697 imd->metadata_lte = NULL;
700 struct wim_lookup_table_entry *lte, *tmp;
701 list_for_each_entry_safe(lte, tmp, &imd->unhashed_streams, unhashed_list)
702 free_lookup_table_entry(lte);
704 INIT_LIST_HEAD(&imd->unhashed_streams);
705 INIT_LIST_HEAD(&imd->inode_list);
708 do_ntfs_umount(imd->ntfs_vol);
709 imd->ntfs_vol = NULL;
715 put_image_metadata(struct wim_image_metadata *imd,
716 struct wim_lookup_table *table)
718 if (imd && --imd->refcnt == 0) {
719 destroy_image_metadata(imd, table, true);
724 /* Appends the specified image metadata structure to the array of image metadata
725 * for a WIM, and increments the image count. */
727 append_image_metadata(WIMStruct *wim, struct wim_image_metadata *imd)
729 struct wim_image_metadata **imd_array;
731 DEBUG("Reallocating image metadata array for image_count = %u",
732 wim->hdr.image_count + 1);
733 imd_array = REALLOC(wim->image_metadata,
734 sizeof(wim->image_metadata[0]) * (wim->hdr.image_count + 1));
737 return WIMLIB_ERR_NOMEM;
738 wim->image_metadata = imd_array;
739 imd_array[wim->hdr.image_count++] = imd;
744 struct wim_image_metadata *
745 new_image_metadata(void)
747 struct wim_image_metadata *imd;
749 imd = CALLOC(1, sizeof(*imd));
752 INIT_LIST_HEAD(&imd->inode_list);
753 INIT_LIST_HEAD(&imd->unhashed_streams);
754 DEBUG("Created new image metadata (refcnt=1)");
756 ERROR_WITH_ERRNO("Failed to allocate new image metadata structure");
761 struct wim_image_metadata **
762 new_image_metadata_array(unsigned num_images)
764 struct wim_image_metadata **imd_array;
766 DEBUG("Creating new image metadata array for %u images",
769 imd_array = CALLOC(num_images, sizeof(imd_array[0]));
772 ERROR("Failed to allocate memory for %u image metadata structures",
776 for (unsigned i = 0; i < num_images; i++) {
777 imd_array[i] = new_image_metadata();
779 for (unsigned j = 0; j < i; j++)
780 put_image_metadata(imd_array[j], NULL);
788 /* Checksum all streams that are unhashed (other than the metadata streams),
789 * merging them into the lookup table as needed. This is a no-op unless the
790 * library has previously used to add or mount an image using the same
793 wim_checksum_unhashed_streams(WIMStruct *wim)
796 for (int i = 0; i < wim->hdr.image_count; i++) {
797 struct wim_lookup_table_entry *lte, *tmp;
798 struct wim_image_metadata *imd = wim->image_metadata[i];
799 image_for_each_unhashed_stream_safe(lte, tmp, imd) {
800 ret = hash_unhashed_stream(lte, wim->lookup_table, NULL);
809 * can_modify_wim - Check if a given WIM is writeable. This is only the case if
810 * it meets the following three conditions:
812 * 1. Write access is allowed to the underlying file (if any) at the filesystem level.
813 * 2. The WIM is not part of a spanned set.
814 * 3. The WIM_HDR_FLAG_READONLY flag is not set in the WIM header.
816 * Return value is 0 if writable; WIMLIB_ERR_WIM_IS_READONLY otherwise.
819 can_modify_wim(WIMStruct *wim)
822 if (taccess(wim->filename, W_OK)) {
823 ERROR_WITH_ERRNO("Can't modify \"%"TS"\"", wim->filename);
824 return WIMLIB_ERR_WIM_IS_READONLY;
827 if (wim->hdr.total_parts != 1) {
828 ERROR("Cannot modify \"%"TS"\": is part of a spanned set",
830 return WIMLIB_ERR_WIM_IS_READONLY;
832 if (wim->hdr.flags & WIM_HDR_FLAG_READONLY) {
833 ERROR("Cannot modify \"%"TS"\": is marked read-only",
835 return WIMLIB_ERR_WIM_IS_READONLY;
841 * can_delete_from_wim - Check if files or images can be deleted from a given
844 * This theoretically should be exactly the same as can_modify_wim(), but
845 * unfortunately, due to bugs in Microsoft's software that generate incorrect
846 * reference counts for some WIM resources, we need to run expensive
847 * verifications to make sure the reference counts are correct on all WIM
848 * resources. Otherwise we might delete a WIM resource whose reference count
849 * has fallen to 0, but is actually still referenced somewhere.
852 can_delete_from_wim(WIMStruct *wim)
856 ret = can_modify_wim(wim);
859 if (!wim->refcnts_ok)
860 wim_recalculate_refcnts(wim);
864 /* API function documented in wimlib.h */
866 wimlib_free(WIMStruct *wim)
868 DEBUG("Freeing WIMStruct");
872 if (wim->in_fd.fd != -1)
873 close(wim->in_fd.fd);
874 if (wim->out_fd.fd != -1)
875 close(wim->out_fd.fd);
877 free_lookup_table(wim->lookup_table);
880 free_wim_info(wim->wim_info);
881 if (wim->image_metadata) {
882 for (unsigned i = 0; i < wim->hdr.image_count; i++)
883 put_image_metadata(wim->image_metadata[i], NULL);
884 FREE(wim->image_metadata);
887 DEBUG("Freed WIMStruct");
891 test_locale_ctype_utf8(void)
896 char *ctype = nl_langinfo(CODESET);
898 return (!strstr(ctype, "UTF-8") ||
899 !strstr(ctype, "UTF8") ||
900 !strstr(ctype, "utf8") ||
901 !strstr(ctype, "utf-8"));
905 /* API function documented in wimlib.h */
907 wimlib_global_init(int init_flags)
909 static bool already_inited = false;
913 libxml_global_init();
914 if (!(init_flags & WIMLIB_INIT_FLAG_ASSUME_UTF8)) {
915 wimlib_mbs_is_utf8 = test_locale_ctype_utf8();
917 if (!wimlib_mbs_is_utf8)
918 libntfs3g_global_init();
924 already_inited = true;
928 /* API function documented in wimlib.h */
930 wimlib_global_cleanup(void)
932 libxml_global_cleanup();
933 iconv_global_cleanup();
935 win32_global_cleanup();