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"
43 #include "wimlib/version.h"
46 # include "wimlib/win32.h" /* for realpath() replacement */
52 # include <langinfo.h>
60 wim_default_pack_compression_type(void)
62 return WIMLIB_COMPRESSION_TYPE_LZMS;
66 wim_default_pack_chunk_size(int ctype) {
68 case WIMLIB_COMPRESSION_TYPE_LZMS:
69 /* Note: WIMGAPI uses 1 << 26, but lower sizes are compatible.
71 return 1U << 25; /* 33554432 */
73 return 1U << 15; /* 32768 */
80 WIMStruct *wim = CALLOC(1, sizeof(WIMStruct));
82 filedes_invalidate(&wim->in_fd);
83 filedes_invalidate(&wim->out_fd);
84 wim->out_pack_compression_type = wim_default_pack_compression_type();
85 wim->out_pack_chunk_size = wim_default_pack_chunk_size(
86 wim->out_pack_compression_type);
87 INIT_LIST_HEAD(&wim->subwims);
92 /* Determine if the chunk size is valid for the specified compression type. */
94 wim_chunk_size_valid(u32 chunk_size, int ctype)
98 /* Chunk size is meaningless for uncompressed WIMs --- any value is
100 if (ctype == WIMLIB_COMPRESSION_TYPE_NONE)
103 /* Chunk size must be power of 2. */
106 order = bsr32(chunk_size);
107 if (chunk_size != 1U << order)
126 /* See the documentation for the --chunk-size option of `wimlib-imagex
127 * capture' for information about allowed chunk sizes. */
129 case WIMLIB_COMPRESSION_TYPE_LZX:
130 return order >= 15 && order <= 21;
132 case WIMLIB_COMPRESSION_TYPE_XPRESS:
133 return order >= 12 && order <= 26;
134 case WIMLIB_COMPRESSION_TYPE_LZMS:
135 return order >= 15 && order <= 30;
140 /* Return the default chunk size to use for the specified compression type.
142 * See notes above in wim_chunk_size_valid(). */
144 wim_default_chunk_size(int ctype)
147 case WIMLIB_COMPRESSION_TYPE_LZMS:
148 return 1U << 17; /* 131072 */
150 return 1U << 15; /* 32768 */
155 * Calls a function on images in the WIM. If @image is WIMLIB_ALL_IMAGES,
156 * @visitor is called on the WIM once for each image, with each image selected
157 * as the current image in turn. If @image is a certain image, @visitor is
158 * called on the WIM only once, with that image selected.
161 for_image(WIMStruct *wim, int image, int (*visitor)(WIMStruct *))
168 if (image == WIMLIB_ALL_IMAGES) {
170 end = wim->hdr.image_count;
171 } else if (image >= 1 && image <= wim->hdr.image_count) {
175 return WIMLIB_ERR_INVALID_IMAGE;
177 for (i = start; i <= end; i++) {
178 ret = select_wim_image(wim, i);
188 /* API function documented in wimlib.h */
190 wimlib_create_new_wim(int ctype, WIMStruct **wim_ret)
193 struct wim_lookup_table *table;
196 wimlib_global_init(WIMLIB_INIT_FLAG_ASSUME_UTF8);
198 DEBUG("Creating new WIM with %"TS" compression.",
199 wimlib_get_compression_type_string(ctype));
201 /* Allocate the WIMStruct. */
202 wim = new_wim_struct();
204 return WIMLIB_ERR_NOMEM;
206 ret = init_wim_header(&wim->hdr, ctype, wim_default_chunk_size(ctype));
210 table = new_lookup_table(9001);
212 ret = WIMLIB_ERR_NOMEM;
215 wim->lookup_table = table;
217 wim->compression_type = ctype;
218 wim->out_compression_type = ctype;
219 wim->chunk_size = wim->hdr.chunk_size;
220 wim->out_chunk_size = wim->hdr.chunk_size;
229 destroy_image_metadata(struct wim_image_metadata *imd,
230 struct wim_lookup_table *table,
231 bool free_metadata_lte)
233 free_dentry_tree(imd->root_dentry, table);
234 imd->root_dentry = NULL;
235 free_wim_security_data(imd->security_data);
236 imd->security_data = NULL;
238 if (free_metadata_lte) {
239 free_lookup_table_entry(imd->metadata_lte);
240 imd->metadata_lte = NULL;
243 struct wim_lookup_table_entry *lte, *tmp;
244 list_for_each_entry_safe(lte, tmp, &imd->unhashed_streams, unhashed_list)
245 free_lookup_table_entry(lte);
247 INIT_LIST_HEAD(&imd->unhashed_streams);
248 INIT_LIST_HEAD(&imd->inode_list);
251 do_ntfs_umount(imd->ntfs_vol);
252 imd->ntfs_vol = NULL;
258 put_image_metadata(struct wim_image_metadata *imd,
259 struct wim_lookup_table *table)
261 if (imd && --imd->refcnt == 0) {
262 destroy_image_metadata(imd, table, true);
267 /* Appends the specified image metadata structure to the array of image metadata
268 * for a WIM, and increments the image count. */
270 append_image_metadata(WIMStruct *wim, struct wim_image_metadata *imd)
272 struct wim_image_metadata **imd_array;
274 DEBUG("Reallocating image metadata array for image_count = %u",
275 wim->hdr.image_count + 1);
276 imd_array = REALLOC(wim->image_metadata,
277 sizeof(wim->image_metadata[0]) * (wim->hdr.image_count + 1));
279 if (imd_array == NULL)
280 return WIMLIB_ERR_NOMEM;
281 wim->image_metadata = imd_array;
282 imd_array[wim->hdr.image_count++] = imd;
286 struct wim_image_metadata *
287 new_image_metadata(void)
289 struct wim_image_metadata *imd;
291 imd = CALLOC(1, sizeof(*imd));
294 INIT_LIST_HEAD(&imd->inode_list);
295 INIT_LIST_HEAD(&imd->unhashed_streams);
296 DEBUG("Created new image metadata (refcnt=1)");
298 ERROR_WITH_ERRNO("Failed to allocate new image metadata structure");
303 static struct wim_image_metadata **
304 new_image_metadata_array(unsigned num_images)
306 struct wim_image_metadata **imd_array;
308 DEBUG("Creating new image metadata array for %u images",
311 imd_array = CALLOC(num_images, sizeof(imd_array[0]));
313 if (imd_array == NULL) {
314 ERROR("Failed to allocate memory for %u image metadata structures",
318 for (unsigned i = 0; i < num_images; i++) {
319 imd_array[i] = new_image_metadata();
320 if (imd_array[i] == NULL) {
321 for (unsigned j = 0; j < i; j++)
322 put_image_metadata(imd_array[j], NULL);
331 /* Load the metadata for the specified WIM image into memory and set it as the
332 * WIMStruct's currently selected WIM image. */
334 select_wim_image(WIMStruct *wim, int image)
336 struct wim_image_metadata *imd;
339 DEBUG("Selecting image %d", image);
341 if (image == WIMLIB_NO_IMAGE) {
342 ERROR("Invalid image: %d", WIMLIB_NO_IMAGE);
343 return WIMLIB_ERR_INVALID_IMAGE;
346 if (image == wim->current_image)
349 if (image < 1 || image > wim->hdr.image_count) {
350 ERROR("Cannot select image %d: There are only %u images",
351 image, wim->hdr.image_count);
352 return WIMLIB_ERR_INVALID_IMAGE;
355 if (!wim_has_metadata(wim)) {
356 ERROR("\"%"TS"\" does not contain metadata resources!", wim->filename);
357 if (wim->hdr.part_number != 1)
358 ERROR("Specify the first part of the split WIM instead.");
359 return WIMLIB_ERR_METADATA_NOT_FOUND;
362 /* If a valid image is currently selected, its metadata can be freed if
363 * it has not been modified. */
364 if (wim->current_image != WIMLIB_NO_IMAGE) {
365 imd = wim_get_current_image_metadata(wim);
366 if (!imd->modified) {
367 wimlib_assert(list_empty(&imd->unhashed_streams));
368 DEBUG("Freeing image %u", wim->current_image);
369 destroy_image_metadata(imd, NULL, false);
372 wim->current_image = image;
373 imd = wim_get_current_image_metadata(wim);
374 if (imd->root_dentry || imd->modified) {
377 ret = read_metadata_resource(wim, imd);
379 wim->current_image = WIMLIB_NO_IMAGE;
385 /* API function documented in wimlib.h */
386 WIMLIBAPI const tchar *
387 wimlib_get_compression_type_string(int ctype)
390 case WIMLIB_COMPRESSION_TYPE_NONE:
392 case WIMLIB_COMPRESSION_TYPE_LZX:
394 case WIMLIB_COMPRESSION_TYPE_XPRESS:
396 case WIMLIB_COMPRESSION_TYPE_LZMS:
403 /* API function documented in wimlib.h */
405 wimlib_resolve_image(WIMStruct *wim, const tchar *image_name_or_num)
411 if (!image_name_or_num || !*image_name_or_num)
412 return WIMLIB_NO_IMAGE;
414 if (!tstrcasecmp(image_name_or_num, T("all"))
415 || !tstrcasecmp(image_name_or_num, T("*")))
416 return WIMLIB_ALL_IMAGES;
417 image = tstrtol(image_name_or_num, &p, 10);
418 if (p != image_name_or_num && *p == T('\0') && image > 0) {
419 if (image > wim->hdr.image_count)
420 return WIMLIB_NO_IMAGE;
423 for (i = 1; i <= wim->hdr.image_count; i++) {
424 if (!tstrcmp(image_name_or_num,
425 wimlib_get_image_name(wim, i)))
428 return WIMLIB_NO_IMAGE;
432 /* API function documented in wimlib.h */
434 wimlib_print_available_images(const WIMStruct *wim, int image)
440 if (image == WIMLIB_ALL_IMAGES) {
441 n = tprintf(T("Available Images:\n"));
443 last = wim->hdr.image_count;
444 } else if (image >= 1 && image <= wim->hdr.image_count) {
445 n = tprintf(T("Information for Image %d\n"), image);
449 tprintf(T("wimlib_print_available_images(): Invalid image %d"),
453 for (i = 0; i < n - 1; i++)
456 for (i = first; i <= last; i++)
457 print_image_info(wim->wim_info, i);
460 /* API function documented in wimlib.h */
462 wimlib_get_wim_info(WIMStruct *wim, struct wimlib_wim_info *info)
464 memset(info, 0, sizeof(struct wimlib_wim_info));
465 memcpy(info->guid, wim->hdr.guid, WIMLIB_GUID_LEN);
466 info->image_count = wim->hdr.image_count;
467 info->boot_index = wim->hdr.boot_idx;
468 info->wim_version = wim->hdr.wim_version;
469 info->chunk_size = wim->chunk_size;
470 info->part_number = wim->hdr.part_number;
471 info->total_parts = wim->hdr.total_parts;
472 info->compression_type = wim->compression_type;
473 info->total_bytes = wim_info_get_total_bytes(wim->wim_info);
474 info->has_integrity_table = wim_has_integrity_table(wim);
475 info->opened_from_file = (wim->filename != NULL);
476 info->is_readonly = (wim->hdr.flags & WIM_HDR_FLAG_READONLY) ||
477 (wim->hdr.total_parts != 1) ||
478 (wim->filename && taccess(wim->filename, W_OK));
479 info->has_rpfix = (wim->hdr.flags & WIM_HDR_FLAG_RP_FIX) != 0;
480 info->is_marked_readonly = (wim->hdr.flags & WIM_HDR_FLAG_READONLY) != 0;
481 info->write_in_progress = (wim->hdr.flags & WIM_HDR_FLAG_WRITE_IN_PROGRESS) != 0;
482 info->metadata_only = (wim->hdr.flags & WIM_HDR_FLAG_METADATA_ONLY) != 0;
483 info->resource_only = (wim->hdr.flags & WIM_HDR_FLAG_RESOURCE_ONLY) != 0;
484 info->spanned = (wim->hdr.flags & WIM_HDR_FLAG_SPANNED) != 0;
485 info->pipable = wim_is_pipable(wim);
489 /* API function documented in wimlib.h */
491 wimlib_set_wim_info(WIMStruct *wim, const struct wimlib_wim_info *info, int which)
495 if (which & ~(WIMLIB_CHANGE_READONLY_FLAG |
497 WIMLIB_CHANGE_BOOT_INDEX |
498 WIMLIB_CHANGE_RPFIX_FLAG))
499 return WIMLIB_ERR_INVALID_PARAM;
501 if (which & WIMLIB_CHANGE_READONLY_FLAG) {
502 if (info->is_marked_readonly)
503 wim->hdr.flags |= WIM_HDR_FLAG_READONLY;
505 wim->hdr.flags &= ~WIM_HDR_FLAG_READONLY;
508 if ((which & ~WIMLIB_CHANGE_READONLY_FLAG) == 0)
511 ret = can_modify_wim(wim);
515 if (which & WIMLIB_CHANGE_GUID)
516 memcpy(wim->hdr.guid, info->guid, WIM_GUID_LEN);
518 if (which & WIMLIB_CHANGE_BOOT_INDEX) {
519 if (info->boot_index > wim->hdr.image_count) {
520 ERROR("%u is not 0 or a valid image in the WIM to mark as bootable",
522 return WIMLIB_ERR_INVALID_IMAGE;
524 wim->hdr.boot_idx = info->boot_index;
527 if (which & WIMLIB_CHANGE_RPFIX_FLAG) {
529 wim->hdr.flags |= WIM_HDR_FLAG_RP_FIX;
531 wim->hdr.flags &= ~WIM_HDR_FLAG_RP_FIX;
537 set_out_ctype(int ctype, u8 *out_ctype_p)
540 case WIMLIB_COMPRESSION_TYPE_INVALID:
542 case WIMLIB_COMPRESSION_TYPE_NONE:
543 case WIMLIB_COMPRESSION_TYPE_LZX:
544 case WIMLIB_COMPRESSION_TYPE_XPRESS:
545 case WIMLIB_COMPRESSION_TYPE_LZMS:
546 *out_ctype_p = ctype;
549 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
552 /* API function documented in wimlib.h */
554 wimlib_set_output_compression_type(WIMStruct *wim, int ctype)
556 int ret = set_out_ctype(ctype, &wim->out_compression_type);
560 /* Reset the chunk size if it's no longer valid. */
561 if (!wim_chunk_size_valid(wim->out_chunk_size, ctype))
562 wim->out_chunk_size = wim_default_chunk_size(ctype);
566 /* API function documented in wimlib.h */
568 wimlib_set_output_pack_compression_type(WIMStruct *wim, int ctype)
570 int ret = set_out_ctype(ctype, &wim->out_pack_compression_type);
574 /* Reset the chunk size if it's no longer valid. */
575 if (!wim_chunk_size_valid(wim->out_pack_chunk_size, ctype))
576 wim->out_pack_chunk_size = wim_default_pack_chunk_size(ctype);
581 set_out_chunk_size(u32 chunk_size, int ctype, u32 *out_chunk_size_p)
583 if (!wim_chunk_size_valid(chunk_size, ctype)) {
584 ERROR("Invalid chunk size (%"PRIu32" bytes) "
585 "for compression type %"TS"!",
587 wimlib_get_compression_type_string(ctype));
588 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
591 *out_chunk_size_p = chunk_size;
595 /* API function documented in wimlib.h */
597 wimlib_set_output_chunk_size(WIMStruct *wim, uint32_t chunk_size)
599 if (chunk_size == 0) {
600 wim->out_chunk_size =
601 wim_default_chunk_size(wim->out_compression_type);
605 return set_out_chunk_size(chunk_size,
606 wim->out_compression_type,
607 &wim->out_chunk_size);
610 /* API function documented in wimlib.h */
612 wimlib_set_output_pack_chunk_size(WIMStruct *wim, uint32_t chunk_size)
614 if (chunk_size == 0) {
615 wim->out_pack_chunk_size =
616 wim_default_pack_chunk_size(wim->out_pack_compression_type);
620 return set_out_chunk_size(chunk_size,
621 wim->out_pack_compression_type,
622 &wim->out_pack_chunk_size);
626 wimlib_register_progress_function(WIMStruct *wim,
627 wimlib_progress_func_t progfunc,
630 wim->progfunc = progfunc;
631 wim->progctx = progctx;
635 open_wim_file(const tchar *filename, struct filedes *fd_ret)
639 raw_fd = topen(filename, O_RDONLY | O_BINARY);
641 ERROR_WITH_ERRNO("Can't open \"%"TS"\" read-only", filename);
642 return WIMLIB_ERR_OPEN;
644 filedes_init(fd_ret, raw_fd);
649 * Begins the reading of a WIM file; opens the file and reads its header and
650 * lookup table, and optionally checks the integrity.
653 begin_read(WIMStruct *wim, const void *wim_filename_or_fd, int open_flags)
657 const tchar *wimfile;
659 if (open_flags & WIMLIB_OPEN_FLAG_FROM_PIPE) {
661 filedes_init(&wim->in_fd, *(const int*)wim_filename_or_fd);
662 wim->in_fd.is_pipe = 1;
664 wimfile = wim_filename_or_fd;
665 ret = open_wim_file(wimfile, &wim->in_fd);
669 /* The absolute path to the WIM is requested so that
670 * wimlib_overwrite() still works even if the process changes
671 * its working directory. This actually happens if a WIM is
672 * mounted read-write, since the FUSE thread changes directory
673 * to "/", and it needs to be able to find the WIM file again.
675 * This will break if the full path to the WIM changes in the
676 * intervening time...
678 * Warning: in Windows native builds, realpath() calls the
679 * replacement function in win32_replacements.c.
681 wim->filename = realpath(wimfile, NULL);
682 if (wim->filename == NULL) {
683 ERROR_WITH_ERRNO("Failed to resolve WIM filename");
685 return WIMLIB_ERR_NOMEM;
687 return WIMLIB_ERR_OPEN;
691 ret = read_wim_header(wim, &wim->hdr);
695 if (wim->hdr.flags & WIM_HDR_FLAG_WRITE_IN_PROGRESS) {
696 WARNING("The WIM_HDR_FLAG_WRITE_IN_PROGRESS flag is set in the header of\n"
697 " \"%"TS"\". It may be being changed by another process,\n"
698 " or a process may have crashed while writing the WIM.",
702 if (open_flags & WIMLIB_OPEN_FLAG_WRITE_ACCESS) {
703 ret = can_modify_wim(wim);
708 if ((open_flags & WIMLIB_OPEN_FLAG_ERROR_IF_SPLIT) &&
709 (wim->hdr.total_parts != 1))
710 return WIMLIB_ERR_IS_SPLIT_WIM;
712 DEBUG("According to header, WIM contains %u images", wim->hdr.image_count);
714 /* If the boot index is invalid, print a warning and set it to 0 */
715 if (wim->hdr.boot_idx > wim->hdr.image_count) {
716 WARNING("In `%"TS"', image %u is marked as bootable, "
717 "but there are only %u images in the WIM",
718 wimfile, wim->hdr.boot_idx, wim->hdr.image_count);
719 wim->hdr.boot_idx = 0;
722 /* Check and cache the compression type */
723 if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESSION) {
724 if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESS_LZX) {
725 wim->compression_type = WIMLIB_COMPRESSION_TYPE_LZX;
726 } else if (wim->hdr.flags & (WIM_HDR_FLAG_COMPRESS_XPRESS |
727 WIM_HDR_FLAG_COMPRESS_XPRESS_2)) {
728 wim->compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
729 } else if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESS_LZMS) {
730 wim->compression_type = WIMLIB_COMPRESSION_TYPE_LZMS;
732 ERROR("The compression flag is set on \"%"TS"\", but "
733 "a flag for a recognized format is not",
735 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
738 wim->compression_type = WIMLIB_COMPRESSION_TYPE_NONE;
740 wim->out_compression_type = wim->compression_type;
742 /* Check and cache the chunk size. */
743 wim->chunk_size = wim->hdr.chunk_size;
744 wim->out_chunk_size = wim->chunk_size;
745 if (!wim_chunk_size_valid(wim->chunk_size, wim->compression_type)) {
746 ERROR("Invalid chunk size (%"PRIu32" bytes) "
747 "for compression type %"TS"!",
749 wimlib_get_compression_type_string(wim->compression_type));
750 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
753 if (open_flags & WIMLIB_OPEN_FLAG_CHECK_INTEGRITY) {
754 ret = check_wim_integrity(wim);
755 if (ret == WIM_INTEGRITY_NONEXISTENT) {
756 WARNING("No integrity information for `%"TS"'; skipping "
757 "integrity check.", wimfile);
758 } else if (ret == WIM_INTEGRITY_NOT_OK) {
759 ERROR("WIM is not intact! (Failed integrity check)");
760 return WIMLIB_ERR_INTEGRITY;
761 } else if (ret != WIM_INTEGRITY_OK) {
766 if (wim->hdr.image_count != 0 && wim->hdr.part_number == 1) {
767 wim->image_metadata = new_image_metadata_array(wim->hdr.image_count);
768 if (wim->image_metadata == NULL)
769 return WIMLIB_ERR_NOMEM;
772 if (open_flags & WIMLIB_OPEN_FLAG_FROM_PIPE) {
773 wim->lookup_table = new_lookup_table(9001);
774 if (wim->lookup_table == NULL)
775 return WIMLIB_ERR_NOMEM;
778 ret = read_wim_xml_data(wim);
782 xml_num_images = wim_info_get_num_images(wim->wim_info);
783 if (xml_num_images != wim->hdr.image_count) {
784 ERROR("In the file `%"TS"', there are %u <IMAGE> elements "
785 "in the XML data,", wimfile, xml_num_images);
786 ERROR("but %u images in the WIM! There must be exactly one "
787 "<IMAGE> element per image.", wim->hdr.image_count);
788 return WIMLIB_ERR_IMAGE_COUNT;
791 ret = read_wim_lookup_table(wim);
795 DEBUG("Done beginning read of WIM file `%"TS"'.", wimfile);
801 open_wim_as_WIMStruct(const void *wim_filename_or_fd, int open_flags,
803 wimlib_progress_func_t progfunc, void *progctx)
808 if (open_flags & WIMLIB_OPEN_FLAG_FROM_PIPE)
809 DEBUG("Opening pipable WIM from file descriptor %d.", *(const int*)wim_filename_or_fd);
811 DEBUG("Opening WIM file \"%"TS"\"", (const tchar*)wim_filename_or_fd);
813 wimlib_global_init(WIMLIB_INIT_FLAG_ASSUME_UTF8);
816 return WIMLIB_ERR_INVALID_PARAM;
818 wim = new_wim_struct();
820 return WIMLIB_ERR_NOMEM;
822 wim->progfunc = progfunc;
823 wim->progctx = progctx;
825 ret = begin_read(wim, wim_filename_or_fd, open_flags);
831 DEBUG("Successfully opened WIM and created WIMStruct.");
836 /* API function documented in wimlib.h */
838 wimlib_open_wim_with_progress(const tchar *wimfile, int open_flags,
840 wimlib_progress_func_t progfunc, void *progctx)
842 if (open_flags & ~(WIMLIB_OPEN_FLAG_CHECK_INTEGRITY |
843 WIMLIB_OPEN_FLAG_ERROR_IF_SPLIT |
844 WIMLIB_OPEN_FLAG_WRITE_ACCESS))
845 return WIMLIB_ERR_INVALID_PARAM;
847 return open_wim_as_WIMStruct(wimfile, open_flags, wim_ret,
851 /* API function documented in wimlib.h */
853 wimlib_open_wim(const tchar *wimfile, int open_flags, WIMStruct **wim_ret)
855 return wimlib_open_wim_with_progress(wimfile, open_flags, wim_ret,
859 /* Checksum all streams that are unhashed (other than the metadata streams),
860 * merging them into the lookup table as needed. This is a no-op unless the
861 * library has previously used to add or mount an image using the same
864 wim_checksum_unhashed_streams(WIMStruct *wim)
868 if (!wim_has_metadata(wim))
870 for (int i = 0; i < wim->hdr.image_count; i++) {
871 struct wim_lookup_table_entry *lte, *tmp;
872 struct wim_image_metadata *imd = wim->image_metadata[i];
873 image_for_each_unhashed_stream_safe(lte, tmp, imd) {
874 struct wim_lookup_table_entry *new_lte;
875 ret = hash_unhashed_stream(lte, wim->lookup_table, &new_lte);
879 free_lookup_table_entry(lte);
886 * can_modify_wim - Check if a given WIM is writeable. This is only the case if
887 * it meets the following three conditions:
889 * 1. Write access is allowed to the underlying file (if any) at the filesystem level.
890 * 2. The WIM is not part of a spanned set.
891 * 3. The WIM_HDR_FLAG_READONLY flag is not set in the WIM header.
893 * Return value is 0 if writable; WIMLIB_ERR_WIM_IS_READONLY otherwise.
896 can_modify_wim(WIMStruct *wim)
899 if (taccess(wim->filename, W_OK)) {
900 ERROR_WITH_ERRNO("Can't modify \"%"TS"\"", wim->filename);
901 return WIMLIB_ERR_WIM_IS_READONLY;
904 if (wim->hdr.total_parts != 1) {
905 ERROR("Cannot modify \"%"TS"\": is part of a split WIM",
907 return WIMLIB_ERR_WIM_IS_READONLY;
909 if (wim->hdr.flags & WIM_HDR_FLAG_READONLY) {
910 ERROR("Cannot modify \"%"TS"\": is marked read-only",
912 return WIMLIB_ERR_WIM_IS_READONLY;
918 * can_delete_from_wim - Check if files or images can be deleted from a given
921 * This theoretically should be exactly the same as can_modify_wim(), but
922 * unfortunately, due to bugs in Microsoft's software that generate incorrect
923 * reference counts for some WIM resources, we need to run expensive
924 * verifications to make sure the reference counts are correct on all WIM
925 * resources. Otherwise we might delete a WIM resource whose reference count
926 * has fallen to 0, but is actually still referenced somewhere.
929 can_delete_from_wim(WIMStruct *wim)
933 ret = can_modify_wim(wim);
936 if (!wim->refcnts_ok) {
937 ret = wim_recalculate_refcnts(wim);
944 /* API function documented in wimlib.h */
946 wimlib_free(WIMStruct *wim)
951 DEBUG("Freeing WIMStruct (filename=\"%"TS"\", image_count=%u)",
952 wim->filename, wim->hdr.image_count);
954 while (!list_empty(&wim->subwims)) {
957 subwim = list_entry(wim->subwims.next, WIMStruct, subwim_node);
958 list_del(&subwim->subwim_node);
959 DEBUG("Freeing subwim.");
963 if (filedes_valid(&wim->in_fd))
964 filedes_close(&wim->in_fd);
965 if (filedes_valid(&wim->out_fd))
966 filedes_close(&wim->out_fd);
968 free_lookup_table(wim->lookup_table);
970 wimlib_free_decompressor(wim->decompressor);
973 free_wim_info(wim->wim_info);
974 if (wim->image_metadata) {
975 for (unsigned i = 0; i < wim->hdr.image_count; i++)
976 put_image_metadata(wim->image_metadata[i], NULL);
977 FREE(wim->image_metadata);
983 test_locale_ctype_utf8(void)
988 char *ctype = nl_langinfo(CODESET);
990 return (!strstr(ctype, "UTF-8") ||
991 !strstr(ctype, "UTF8") ||
992 !strstr(ctype, "utf8") ||
993 !strstr(ctype, "utf-8"));
997 /* API function documented in wimlib.h */
999 wimlib_get_version(void)
1001 return WIMLIB_VERSION_CODE;
1004 /* API function documented in wimlib.h */
1006 wimlib_global_init(int init_flags)
1008 static bool already_inited = false;
1013 if (init_flags & ~(WIMLIB_INIT_FLAG_ASSUME_UTF8 |
1014 WIMLIB_INIT_FLAG_DONT_ACQUIRE_PRIVILEGES |
1015 WIMLIB_INIT_FLAG_STRICT_CAPTURE_PRIVILEGES |
1016 WIMLIB_INIT_FLAG_STRICT_APPLY_PRIVILEGES |
1017 WIMLIB_INIT_FLAG_DEFAULT_CASE_SENSITIVE |
1018 WIMLIB_INIT_FLAG_DEFAULT_CASE_INSENSITIVE))
1019 return WIMLIB_ERR_INVALID_PARAM;
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();
1031 int ret = win32_global_init(init_flags);
1037 if (init_flags & WIMLIB_INIT_FLAG_DEFAULT_CASE_SENSITIVE)
1038 default_ignore_case = false;
1039 else if (init_flags & WIMLIB_INIT_FLAG_DEFAULT_CASE_INSENSITIVE)
1040 default_ignore_case = true;
1041 already_inited = true;
1045 /* API function documented in wimlib.h */
1047 wimlib_global_cleanup(void)
1049 libxml_global_cleanup();
1050 iconv_global_cleanup();
1052 win32_global_cleanup();
1054 cleanup_decompressor_params();
1055 cleanup_compressor_params();