2 * wim.c - High-level code dealing with WIMStructs and images.
6 * Copyright (C) 2012, 2013, 2014, 2015 Eric Biggers
8 * This file is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU Lesser General Public License as published by the Free
10 * Software Foundation; either version 3 of the License, or (at your option) any
13 * This file is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this file; if not, see http://www.gnu.org/licenses/.
29 # include <langinfo.h>
36 #include "wimlib/assert.h"
37 #include "wimlib/blob_table.h"
38 #include "wimlib/dentry.h"
39 #include "wimlib/encoding.h"
40 #include "wimlib/file_io.h"
41 #include "wimlib/integrity.h"
42 #include "wimlib/metadata.h"
43 #include "wimlib/ntfs_3g.h" /* for libntfs3g_global_init() */
44 #include "wimlib/security.h"
45 #include "wimlib/wim.h"
46 #include "wimlib/xml.h"
48 # include "wimlib/win32.h" /* for realpath() replacement */
51 /* Information about the available compression types for the WIM format. */
56 u32 default_nonsolid_chunk_size;
57 u32 default_solid_chunk_size;
58 } wim_ctype_info[] = {
59 [WIMLIB_COMPRESSION_TYPE_NONE] = {
63 .default_nonsolid_chunk_size = 0,
64 .default_solid_chunk_size = 0,
66 [WIMLIB_COMPRESSION_TYPE_XPRESS] = {
68 .min_chunk_size = 4096,
69 .max_chunk_size = 65536,
70 .default_nonsolid_chunk_size = 32768,
71 .default_solid_chunk_size = 32768,
73 [WIMLIB_COMPRESSION_TYPE_LZX] = {
75 .min_chunk_size = 32768,
76 .max_chunk_size = 2097152,
77 .default_nonsolid_chunk_size = 32768,
78 .default_solid_chunk_size = 32768,
80 [WIMLIB_COMPRESSION_TYPE_LZMS] = {
82 .min_chunk_size = 32768,
83 .max_chunk_size = 1073741824,
84 .default_nonsolid_chunk_size = 131072,
85 .default_solid_chunk_size = 67108864,
89 /* Is the specified compression type valid? */
91 wim_compression_type_valid(enum wimlib_compression_type ctype)
93 return (unsigned)ctype < ARRAY_LEN(wim_ctype_info) &&
94 wim_ctype_info[(unsigned)ctype].name != NULL;
97 /* Is the specified chunk size valid for the compression type? */
99 wim_chunk_size_valid(u32 chunk_size, enum wimlib_compression_type ctype)
101 if (!(chunk_size == 0 || is_power_of_2(chunk_size)))
104 return chunk_size >= wim_ctype_info[(unsigned)ctype].min_chunk_size &&
105 chunk_size <= wim_ctype_info[(unsigned)ctype].max_chunk_size;
108 /* Return the default chunk size to use for the specified compression type in
109 * non-solid resources. */
111 wim_default_nonsolid_chunk_size(enum wimlib_compression_type ctype)
113 return wim_ctype_info[(unsigned)ctype].default_nonsolid_chunk_size;
116 /* Return the default chunk size to use for the specified compression type in
117 * solid resources. */
119 wim_default_solid_chunk_size(enum wimlib_compression_type ctype)
121 return wim_ctype_info[(unsigned)ctype].default_solid_chunk_size;
124 /* Return the default compression type to use in solid resources. */
125 static enum wimlib_compression_type
126 wim_default_solid_compression_type(void)
128 return WIMLIB_COMPRESSION_TYPE_LZMS;
132 is_blob_in_solid_resource(struct blob_descriptor *blob, void *_ignore)
134 return blob->blob_location == BLOB_IN_WIM &&
135 (blob->rdesc->flags & WIM_RESHDR_FLAG_SOLID);
139 wim_has_solid_resources(const WIMStruct *wim)
141 return for_blob_in_table(wim->blob_table, is_blob_in_solid_resource, NULL);
147 WIMStruct *wim = CALLOC(1, sizeof(WIMStruct));
151 filedes_invalidate(&wim->in_fd);
152 filedes_invalidate(&wim->out_fd);
153 wim->out_solid_compression_type = wim_default_solid_compression_type();
154 wim->out_solid_chunk_size = wim_default_solid_chunk_size(
155 wim->out_solid_compression_type);
156 INIT_LIST_HEAD(&wim->subwims);
160 /* API function documented in wimlib.h */
162 wimlib_create_new_wim(enum wimlib_compression_type ctype, WIMStruct **wim_ret)
167 ret = wimlib_global_init(WIMLIB_INIT_FLAG_ASSUME_UTF8);
172 return WIMLIB_ERR_INVALID_PARAM;
174 if (!wim_compression_type_valid(ctype))
175 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
177 wim = new_wim_struct();
179 return WIMLIB_ERR_NOMEM;
181 wim->blob_table = new_blob_table(9001);
182 if (!wim->blob_table) {
184 return WIMLIB_ERR_NOMEM;
187 /* Fill in wim->hdr with default values */
188 wim->hdr.magic = WIM_MAGIC;
189 wim->hdr.wim_version = WIM_VERSION_DEFAULT;
191 wim->hdr.chunk_size = 0;
192 randomize_byte_array(wim->hdr.guid, WIMLIB_GUID_LEN);
193 wim->hdr.part_number = 1;
194 wim->hdr.total_parts = 1;
195 wim->hdr.image_count = 0;
196 wim->hdr.boot_idx = 0;
198 wim->compression_type = WIMLIB_COMPRESSION_TYPE_NONE;
199 wim->chunk_size = wim->hdr.chunk_size;
201 /* Set the output compression type */
202 wim->out_compression_type = ctype;
203 wim->out_chunk_size = wim_default_nonsolid_chunk_size(ctype);
210 destroy_image_metadata(struct wim_image_metadata *imd,
211 struct blob_table *table,
212 bool free_metadata_blob_descriptor)
214 free_dentry_tree(imd->root_dentry, table);
215 imd->root_dentry = NULL;
216 free_wim_security_data(imd->security_data);
217 imd->security_data = NULL;
219 if (free_metadata_blob_descriptor) {
220 free_blob_descriptor(imd->metadata_blob);
221 imd->metadata_blob = NULL;
224 struct blob_descriptor *blob, *tmp;
225 list_for_each_entry_safe(blob, tmp, &imd->unhashed_blobs, unhashed_list)
226 free_blob_descriptor(blob);
228 INIT_LIST_HEAD(&imd->unhashed_blobs);
229 INIT_HLIST_HEAD(&imd->inode_list);
233 put_image_metadata(struct wim_image_metadata *imd, struct blob_table *table)
235 if (imd && --imd->refcnt == 0) {
236 destroy_image_metadata(imd, table, true);
241 /* Appends the specified image metadata structure to the array of image metadata
242 * for a WIM, and increments the image count. */
244 append_image_metadata(WIMStruct *wim, struct wim_image_metadata *imd)
246 struct wim_image_metadata **imd_array;
248 imd_array = REALLOC(wim->image_metadata,
249 sizeof(wim->image_metadata[0]) * (wim->hdr.image_count + 1));
252 return WIMLIB_ERR_NOMEM;
253 wim->image_metadata = imd_array;
254 imd_array[wim->hdr.image_count++] = imd;
258 struct wim_image_metadata *
259 new_image_metadata(void)
261 struct wim_image_metadata *imd;
263 imd = CALLOC(1, sizeof(*imd));
266 INIT_HLIST_HEAD(&imd->inode_list);
267 INIT_LIST_HEAD(&imd->unhashed_blobs);
272 static struct wim_image_metadata **
273 new_image_metadata_array(unsigned num_images)
275 struct wim_image_metadata **imd_array;
277 imd_array = CALLOC(num_images, sizeof(imd_array[0]));
281 for (unsigned i = 0; i < num_images; i++) {
282 imd_array[i] = new_image_metadata();
283 if (unlikely(!imd_array[i])) {
284 for (unsigned j = 0; j < i; j++)
285 put_image_metadata(imd_array[j], NULL);
295 * Load the metadata for the specified WIM image into memory and set it
296 * as the WIMStruct's currently selected image.
299 * The WIMStruct for the WIM.
301 * The 1-based index of the image in the WIM to select.
303 * On success, 0 will be returned, wim->current_image will be set to
304 * @image, and wim_get_current_image_metadata() can be used to retrieve
305 * metadata information for the image.
307 * On failure, WIMLIB_ERR_INVALID_IMAGE, WIMLIB_ERR_METADATA_NOT_FOUND,
308 * or another error code will be returned.
311 select_wim_image(WIMStruct *wim, int image)
313 struct wim_image_metadata *imd;
316 if (image == WIMLIB_NO_IMAGE)
317 return WIMLIB_ERR_INVALID_IMAGE;
319 if (image == wim->current_image)
322 if (image < 1 || image > wim->hdr.image_count)
323 return WIMLIB_ERR_INVALID_IMAGE;
325 if (!wim_has_metadata(wim))
326 return WIMLIB_ERR_METADATA_NOT_FOUND;
328 /* If a valid image is currently selected, its metadata can be freed if
329 * it has not been modified. */
330 deselect_current_wim_image(wim);
331 wim->current_image = image;
332 imd = wim_get_current_image_metadata(wim);
333 if (imd->root_dentry || imd->modified) {
336 ret = read_metadata_resource(imd);
338 wim->current_image = WIMLIB_NO_IMAGE;
344 deselect_current_wim_image(WIMStruct *wim)
346 struct wim_image_metadata *imd;
347 if (wim->current_image == WIMLIB_NO_IMAGE)
349 imd = wim_get_current_image_metadata(wim);
350 if (!imd->modified) {
351 wimlib_assert(list_empty(&imd->unhashed_blobs));
352 destroy_image_metadata(imd, NULL, false);
354 wim->current_image = WIMLIB_NO_IMAGE;
358 * Calls a function on images in the WIM. If @image is WIMLIB_ALL_IMAGES,
359 * @visitor is called on the WIM once for each image, with each image selected
360 * as the current image in turn. If @image is a certain image, @visitor is
361 * called on the WIM only once, with that image selected.
364 for_image(WIMStruct *wim, int image, int (*visitor)(WIMStruct *))
371 if (image == WIMLIB_ALL_IMAGES) {
373 end = wim->hdr.image_count;
374 } else if (image >= 1 && image <= wim->hdr.image_count) {
378 return WIMLIB_ERR_INVALID_IMAGE;
380 for (i = start; i <= end; i++) {
381 ret = select_wim_image(wim, i);
391 /* API function documented in wimlib.h */
393 wimlib_resolve_image(WIMStruct *wim, const tchar *image_name_or_num)
399 if (!image_name_or_num || !*image_name_or_num)
400 return WIMLIB_NO_IMAGE;
402 if (!tstrcasecmp(image_name_or_num, T("all"))
403 || !tstrcasecmp(image_name_or_num, T("*")))
404 return WIMLIB_ALL_IMAGES;
405 image = tstrtol(image_name_or_num, &p, 10);
406 if (p != image_name_or_num && *p == T('\0') && image > 0) {
407 if (image > wim->hdr.image_count)
408 return WIMLIB_NO_IMAGE;
411 for (i = 1; i <= wim->hdr.image_count; i++) {
412 if (!tstrcmp(image_name_or_num,
413 wimlib_get_image_name(wim, i)))
416 return WIMLIB_NO_IMAGE;
420 /* API function documented in wimlib.h */
422 wimlib_print_available_images(const WIMStruct *wim, int image)
428 if (image == WIMLIB_ALL_IMAGES) {
429 n = tprintf(T("Available Images:\n"));
431 last = wim->hdr.image_count;
432 } else if (image >= 1 && image <= wim->hdr.image_count) {
433 n = tprintf(T("Information for Image %d\n"), image);
437 tprintf(T("wimlib_print_available_images(): Invalid image %d"),
441 for (i = 0; i < n - 1; i++)
444 for (i = first; i <= last; i++)
445 print_image_info(wim->wim_info, i);
448 /* API function documented in wimlib.h */
450 wimlib_get_wim_info(WIMStruct *wim, struct wimlib_wim_info *info)
452 memset(info, 0, sizeof(struct wimlib_wim_info));
453 memcpy(info->guid, wim->hdr.guid, WIMLIB_GUID_LEN);
454 info->image_count = wim->hdr.image_count;
455 info->boot_index = wim->hdr.boot_idx;
456 info->wim_version = wim->hdr.wim_version;
457 info->chunk_size = wim->chunk_size;
458 info->part_number = wim->hdr.part_number;
459 info->total_parts = wim->hdr.total_parts;
460 info->compression_type = wim->compression_type;
461 info->total_bytes = wim_info_get_total_bytes(wim->wim_info);
462 info->has_integrity_table = wim_has_integrity_table(wim);
463 info->opened_from_file = (wim->filename != NULL);
464 info->is_readonly = (wim->hdr.flags & WIM_HDR_FLAG_READONLY) ||
465 (wim->hdr.total_parts != 1) ||
466 (wim->filename && taccess(wim->filename, W_OK));
467 info->has_rpfix = (wim->hdr.flags & WIM_HDR_FLAG_RP_FIX) != 0;
468 info->is_marked_readonly = (wim->hdr.flags & WIM_HDR_FLAG_READONLY) != 0;
469 info->write_in_progress = (wim->hdr.flags & WIM_HDR_FLAG_WRITE_IN_PROGRESS) != 0;
470 info->metadata_only = (wim->hdr.flags & WIM_HDR_FLAG_METADATA_ONLY) != 0;
471 info->resource_only = (wim->hdr.flags & WIM_HDR_FLAG_RESOURCE_ONLY) != 0;
472 info->spanned = (wim->hdr.flags & WIM_HDR_FLAG_SPANNED) != 0;
473 info->pipable = wim_is_pipable(wim);
477 /* API function documented in wimlib.h */
479 wimlib_set_wim_info(WIMStruct *wim, const struct wimlib_wim_info *info, int which)
481 if (which & ~(WIMLIB_CHANGE_READONLY_FLAG |
483 WIMLIB_CHANGE_BOOT_INDEX |
484 WIMLIB_CHANGE_RPFIX_FLAG))
485 return WIMLIB_ERR_INVALID_PARAM;
487 if ((which & WIMLIB_CHANGE_BOOT_INDEX) &&
488 info->boot_index > wim->hdr.image_count)
489 return WIMLIB_ERR_INVALID_IMAGE;
491 if (which & WIMLIB_CHANGE_READONLY_FLAG) {
492 if (info->is_marked_readonly)
493 wim->hdr.flags |= WIM_HDR_FLAG_READONLY;
495 wim->hdr.flags &= ~WIM_HDR_FLAG_READONLY;
498 if (which & WIMLIB_CHANGE_GUID)
499 memcpy(wim->hdr.guid, info->guid, WIM_GUID_LEN);
501 if (which & WIMLIB_CHANGE_BOOT_INDEX)
502 wim->hdr.boot_idx = info->boot_index;
504 if (which & WIMLIB_CHANGE_RPFIX_FLAG) {
506 wim->hdr.flags |= WIM_HDR_FLAG_RP_FIX;
508 wim->hdr.flags &= ~WIM_HDR_FLAG_RP_FIX;
513 /* API function documented in wimlib.h */
515 wimlib_set_output_compression_type(WIMStruct *wim,
516 enum wimlib_compression_type ctype)
518 if (!wim_compression_type_valid(ctype))
519 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
521 wim->out_compression_type = ctype;
523 /* Reset the chunk size if it's no longer valid. */
524 if (!wim_chunk_size_valid(wim->out_chunk_size, ctype))
525 wim->out_chunk_size = wim_default_nonsolid_chunk_size(ctype);
529 /* API function documented in wimlib.h */
531 wimlib_set_output_pack_compression_type(WIMStruct *wim,
532 enum wimlib_compression_type ctype)
534 if (!wim_compression_type_valid(ctype))
535 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
537 /* Solid resources can't be uncompressed. */
538 if (ctype == WIMLIB_COMPRESSION_TYPE_NONE)
539 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
541 wim->out_solid_compression_type = ctype;
543 /* Reset the chunk size if it's no longer valid. */
544 if (!wim_chunk_size_valid(wim->out_solid_chunk_size, ctype))
545 wim->out_solid_chunk_size = wim_default_solid_chunk_size(ctype);
549 /* API function documented in wimlib.h */
551 wimlib_set_output_chunk_size(WIMStruct *wim, u32 chunk_size)
553 if (chunk_size == 0) {
554 wim->out_chunk_size =
555 wim_default_nonsolid_chunk_size(wim->out_compression_type);
559 if (!wim_chunk_size_valid(chunk_size, wim->out_compression_type))
560 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
562 wim->out_chunk_size = chunk_size;
566 /* API function documented in wimlib.h */
568 wimlib_set_output_pack_chunk_size(WIMStruct *wim, u32 chunk_size)
570 if (chunk_size == 0) {
571 wim->out_solid_chunk_size =
572 wim_default_solid_chunk_size(wim->out_solid_compression_type);
576 if (!wim_chunk_size_valid(chunk_size, wim->out_solid_compression_type))
577 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
579 wim->out_solid_chunk_size = chunk_size;
583 /* API function documented in wimlib.h */
584 WIMLIBAPI const tchar *
585 wimlib_get_compression_type_string(enum wimlib_compression_type ctype)
587 if (!wim_compression_type_valid(ctype))
590 return wim_ctype_info[(unsigned)ctype].name;
594 wimlib_register_progress_function(WIMStruct *wim,
595 wimlib_progress_func_t progfunc,
598 wim->progfunc = progfunc;
599 wim->progctx = progctx;
603 open_wim_file(const tchar *filename, struct filedes *fd_ret)
607 raw_fd = topen(filename, O_RDONLY | O_BINARY);
609 ERROR_WITH_ERRNO("Can't open \"%"TS"\" read-only", filename);
610 return WIMLIB_ERR_OPEN;
612 filedes_init(fd_ret, raw_fd);
617 * Begins the reading of a WIM file; opens the file and reads its header and
618 * blob table, and optionally checks the integrity.
621 begin_read(WIMStruct *wim, const void *wim_filename_or_fd, int open_flags)
625 const tchar *wimfile;
627 if (open_flags & WIMLIB_OPEN_FLAG_FROM_PIPE) {
629 filedes_init(&wim->in_fd, *(const int*)wim_filename_or_fd);
630 wim->in_fd.is_pipe = 1;
632 wimfile = wim_filename_or_fd;
633 ret = open_wim_file(wimfile, &wim->in_fd);
637 /* The absolute path to the WIM is requested so that
638 * wimlib_overwrite() still works even if the process changes
639 * its working directory. This actually happens if a WIM is
640 * mounted read-write, since the FUSE thread changes directory
641 * to "/", and it needs to be able to find the WIM file again.
643 * This will break if the full path to the WIM changes in the
644 * intervening time...
646 * Warning: in Windows native builds, realpath() calls the
647 * replacement function in win32_replacements.c.
649 wim->filename = realpath(wimfile, NULL);
650 if (!wim->filename) {
651 ERROR_WITH_ERRNO("Failed to get full path to file "
652 "\"%"TS"\"", wimfile);
654 return WIMLIB_ERR_NOMEM;
656 return WIMLIB_ERR_NO_FILENAME;
660 ret = read_wim_header(wim, &wim->hdr);
664 if (wim->hdr.flags & WIM_HDR_FLAG_WRITE_IN_PROGRESS) {
665 WARNING("The WIM_HDR_FLAG_WRITE_IN_PROGRESS flag is set in the header of\n"
666 " \"%"TS"\". It may be being changed by another process,\n"
667 " or a process may have crashed while writing the WIM.",
671 if (open_flags & WIMLIB_OPEN_FLAG_WRITE_ACCESS) {
672 ret = can_modify_wim(wim);
677 if ((open_flags & WIMLIB_OPEN_FLAG_ERROR_IF_SPLIT) &&
678 (wim->hdr.total_parts != 1))
679 return WIMLIB_ERR_IS_SPLIT_WIM;
681 /* If the boot index is invalid, print a warning and set it to 0 */
682 if (wim->hdr.boot_idx > wim->hdr.image_count) {
683 WARNING("Ignoring invalid boot index.");
684 wim->hdr.boot_idx = 0;
687 /* Check and cache the compression type */
688 if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESSION) {
689 if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESS_LZX) {
690 wim->compression_type = WIMLIB_COMPRESSION_TYPE_LZX;
691 } else if (wim->hdr.flags & (WIM_HDR_FLAG_COMPRESS_XPRESS |
692 WIM_HDR_FLAG_COMPRESS_XPRESS_2)) {
693 wim->compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
694 } else if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESS_LZMS) {
695 wim->compression_type = WIMLIB_COMPRESSION_TYPE_LZMS;
697 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
700 wim->compression_type = WIMLIB_COMPRESSION_TYPE_NONE;
702 wim->out_compression_type = wim->compression_type;
704 /* Check and cache the chunk size. */
705 wim->chunk_size = wim->hdr.chunk_size;
706 wim->out_chunk_size = wim->chunk_size;
707 if (!wim_chunk_size_valid(wim->chunk_size, wim->compression_type)) {
708 ERROR("Invalid chunk size (%"PRIu32" bytes) "
709 "for compression type %"TS"!", wim->chunk_size,
710 wimlib_get_compression_type_string(wim->compression_type));
711 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
714 if (open_flags & WIMLIB_OPEN_FLAG_CHECK_INTEGRITY) {
715 ret = check_wim_integrity(wim);
716 if (ret == WIM_INTEGRITY_NONEXISTENT) {
717 WARNING("\"%"TS"\" does not contain integrity "
718 "information. Skipping integrity check.",
720 } else if (ret == WIM_INTEGRITY_NOT_OK) {
721 return WIMLIB_ERR_INTEGRITY;
722 } else if (ret != WIM_INTEGRITY_OK) {
727 if (wim->hdr.image_count != 0 && wim->hdr.part_number == 1) {
728 wim->image_metadata = new_image_metadata_array(wim->hdr.image_count);
729 if (!wim->image_metadata)
730 return WIMLIB_ERR_NOMEM;
733 if (open_flags & WIMLIB_OPEN_FLAG_FROM_PIPE) {
734 wim->blob_table = new_blob_table(9001);
735 if (!wim->blob_table)
736 return WIMLIB_ERR_NOMEM;
739 ret = read_wim_xml_data(wim);
743 xml_num_images = wim_info_get_num_images(wim->wim_info);
744 if (xml_num_images != wim->hdr.image_count) {
745 ERROR("The WIM's header is inconsistent with its XML data.\n"
746 " Please submit a bug report if you believe this "
747 "WIM file should be considered valid.");
748 return WIMLIB_ERR_IMAGE_COUNT;
751 ret = read_blob_table(wim);
759 open_wim_as_WIMStruct(const void *wim_filename_or_fd, int open_flags,
761 wimlib_progress_func_t progfunc, void *progctx)
766 ret = wimlib_global_init(WIMLIB_INIT_FLAG_ASSUME_UTF8);
770 wim = new_wim_struct();
772 return WIMLIB_ERR_NOMEM;
774 wim->progfunc = progfunc;
775 wim->progctx = progctx;
777 ret = begin_read(wim, wim_filename_or_fd, open_flags);
787 /* API function documented in wimlib.h */
789 wimlib_open_wim_with_progress(const tchar *wimfile, int open_flags,
791 wimlib_progress_func_t progfunc, void *progctx)
793 if (open_flags & ~(WIMLIB_OPEN_FLAG_CHECK_INTEGRITY |
794 WIMLIB_OPEN_FLAG_ERROR_IF_SPLIT |
795 WIMLIB_OPEN_FLAG_WRITE_ACCESS))
796 return WIMLIB_ERR_INVALID_PARAM;
798 if (!wimfile || !*wimfile || !wim_ret)
799 return WIMLIB_ERR_INVALID_PARAM;
801 return open_wim_as_WIMStruct(wimfile, open_flags, wim_ret,
805 /* API function documented in wimlib.h */
807 wimlib_open_wim(const tchar *wimfile, int open_flags, WIMStruct **wim_ret)
809 return wimlib_open_wim_with_progress(wimfile, open_flags, wim_ret,
813 /* Checksum all blobs that are unhashed (other than the metadata blobs), merging
814 * them into the blob table as needed. This is a no-op unless files have been
815 * added to an image in the same WIMStruct. */
817 wim_checksum_unhashed_blobs(WIMStruct *wim)
821 if (!wim_has_metadata(wim))
823 for (int i = 0; i < wim->hdr.image_count; i++) {
824 struct blob_descriptor *blob, *tmp;
825 struct wim_image_metadata *imd = wim->image_metadata[i];
826 image_for_each_unhashed_blob_safe(blob, tmp, imd) {
827 struct blob_descriptor *new_blob;
828 ret = hash_unhashed_blob(blob, wim->blob_table, &new_blob);
831 if (new_blob != blob)
832 free_blob_descriptor(blob);
839 * can_modify_wim - Check if a given WIM is writeable. This is only the case if
840 * it meets the following three conditions:
842 * 1. Write access is allowed to the underlying file (if any) at the filesystem level.
843 * 2. The WIM is not part of a spanned set.
844 * 3. The WIM_HDR_FLAG_READONLY flag is not set in the WIM header.
846 * Return value is 0 if writable; WIMLIB_ERR_WIM_IS_READONLY otherwise.
849 can_modify_wim(WIMStruct *wim)
852 if (taccess(wim->filename, W_OK)) {
853 ERROR_WITH_ERRNO("Can't modify \"%"TS"\"", wim->filename);
854 return WIMLIB_ERR_WIM_IS_READONLY;
857 if (wim->hdr.total_parts != 1) {
858 ERROR("Cannot modify \"%"TS"\": is part of a split WIM",
860 return WIMLIB_ERR_WIM_IS_READONLY;
862 if (wim->hdr.flags & WIM_HDR_FLAG_READONLY) {
863 ERROR("Cannot modify \"%"TS"\": is marked read-only",
865 return WIMLIB_ERR_WIM_IS_READONLY;
870 /* API function documented in wimlib.h */
872 wimlib_free(WIMStruct *wim)
877 while (!list_empty(&wim->subwims)) {
880 subwim = list_entry(wim->subwims.next, WIMStruct, subwim_node);
881 list_del(&subwim->subwim_node);
885 if (filedes_valid(&wim->in_fd))
886 filedes_close(&wim->in_fd);
887 if (filedes_valid(&wim->out_fd))
888 filedes_close(&wim->out_fd);
890 free_blob_table(wim->blob_table);
892 wimlib_free_decompressor(wim->decompressor);
895 free_wim_info(wim->wim_info);
896 if (wim->image_metadata) {
897 for (unsigned i = 0; i < wim->hdr.image_count; i++)
898 put_image_metadata(wim->image_metadata[i], NULL);
899 FREE(wim->image_metadata);
905 test_locale_ctype_utf8(void)
910 char *ctype = nl_langinfo(CODESET);
912 return (!strstr(ctype, "UTF-8") ||
913 !strstr(ctype, "UTF8") ||
914 !strstr(ctype, "utf8") ||
915 !strstr(ctype, "utf-8"));
919 /* API function documented in wimlib.h */
921 wimlib_get_version(void)
923 return (WIMLIB_MAJOR_VERSION << 20) |
924 (WIMLIB_MINOR_VERSION << 10) |
925 WIMLIB_PATCH_VERSION;
928 static bool lib_initialized = false;
929 static pthread_mutex_t lib_initialization_mutex = PTHREAD_MUTEX_INITIALIZER;
931 /* API function documented in wimlib.h */
933 wimlib_global_init(int init_flags)
940 pthread_mutex_lock(&lib_initialization_mutex);
945 #ifdef ENABLE_ERROR_MESSAGES
946 if (!wimlib_error_file)
947 wimlib_error_file = stderr;
950 ret = WIMLIB_ERR_INVALID_PARAM;
951 if (init_flags & ~(WIMLIB_INIT_FLAG_ASSUME_UTF8 |
952 WIMLIB_INIT_FLAG_DONT_ACQUIRE_PRIVILEGES |
953 WIMLIB_INIT_FLAG_STRICT_CAPTURE_PRIVILEGES |
954 WIMLIB_INIT_FLAG_STRICT_APPLY_PRIVILEGES |
955 WIMLIB_INIT_FLAG_DEFAULT_CASE_SENSITIVE |
956 WIMLIB_INIT_FLAG_DEFAULT_CASE_INSENSITIVE))
959 ret = WIMLIB_ERR_INVALID_PARAM;
960 if ((init_flags & (WIMLIB_INIT_FLAG_DEFAULT_CASE_SENSITIVE |
961 WIMLIB_INIT_FLAG_DEFAULT_CASE_INSENSITIVE))
962 == (WIMLIB_INIT_FLAG_DEFAULT_CASE_SENSITIVE |
963 WIMLIB_INIT_FLAG_DEFAULT_CASE_INSENSITIVE))
966 libxml_global_init();
967 if (!(init_flags & WIMLIB_INIT_FLAG_ASSUME_UTF8)) {
968 wimlib_mbs_is_utf8 = test_locale_ctype_utf8();
970 if (!wimlib_mbs_is_utf8)
971 libntfs3g_global_init();
975 ret = win32_global_init(init_flags);
981 if (init_flags & WIMLIB_INIT_FLAG_DEFAULT_CASE_SENSITIVE)
982 default_ignore_case = false;
983 else if (init_flags & WIMLIB_INIT_FLAG_DEFAULT_CASE_INSENSITIVE)
984 default_ignore_case = true;
985 lib_initialized = true;
988 pthread_mutex_unlock(&lib_initialization_mutex);
993 /* API function documented in wimlib.h */
995 wimlib_global_cleanup(void)
997 if (!lib_initialized)
1000 pthread_mutex_lock(&lib_initialization_mutex);
1002 if (!lib_initialized)
1005 libxml_global_cleanup();
1006 iconv_global_cleanup();
1008 win32_global_cleanup();
1011 wimlib_set_error_file(NULL);
1012 lib_initialized = false;
1015 pthread_mutex_unlock(&lib_initialization_mutex);