2 * wim.c - High-level code dealing with WIMStructs and images.
6 * Copyright (C) 2012-2016 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/.
32 #include "wimlib/assert.h"
33 #include "wimlib/blob_table.h"
34 #include "wimlib/cpu_features.h"
35 #include "wimlib/dentry.h"
36 #include "wimlib/encoding.h"
37 #include "wimlib/file_io.h"
38 #include "wimlib/integrity.h"
39 #include "wimlib/metadata.h"
40 #include "wimlib/security.h"
41 #include "wimlib/threads.h"
42 #include "wimlib/wim.h"
43 #include "wimlib/xml.h"
44 #include "wimlib/win32.h"
46 /* Information about the available compression types for the WIM format. */
51 u32 default_nonsolid_chunk_size;
52 u32 default_solid_chunk_size;
53 } wim_ctype_info[] = {
54 [WIMLIB_COMPRESSION_TYPE_NONE] = {
58 .default_nonsolid_chunk_size = 0,
59 .default_solid_chunk_size = 0,
61 [WIMLIB_COMPRESSION_TYPE_XPRESS] = {
63 .min_chunk_size = 4096,
64 .max_chunk_size = 65536,
65 .default_nonsolid_chunk_size = 32768,
66 .default_solid_chunk_size = 32768,
68 [WIMLIB_COMPRESSION_TYPE_LZX] = {
70 .min_chunk_size = 32768,
71 .max_chunk_size = 2097152,
72 .default_nonsolid_chunk_size = 32768,
73 .default_solid_chunk_size = 32768,
75 [WIMLIB_COMPRESSION_TYPE_LZMS] = {
77 .min_chunk_size = 32768,
78 .max_chunk_size = 1073741824,
79 .default_nonsolid_chunk_size = 131072,
80 .default_solid_chunk_size = 67108864,
84 /* Is the specified compression type valid? */
86 wim_compression_type_valid(enum wimlib_compression_type ctype)
88 return (unsigned)ctype < ARRAY_LEN(wim_ctype_info) &&
89 wim_ctype_info[(unsigned)ctype].name != NULL;
92 /* Is the specified chunk size valid for the compression type? */
94 wim_chunk_size_valid(u32 chunk_size, enum wimlib_compression_type ctype)
96 if (!(chunk_size == 0 || is_power_of_2(chunk_size)))
99 return chunk_size >= wim_ctype_info[(unsigned)ctype].min_chunk_size &&
100 chunk_size <= wim_ctype_info[(unsigned)ctype].max_chunk_size;
103 /* Return the default chunk size to use for the specified compression type in
104 * non-solid resources. */
106 wim_default_nonsolid_chunk_size(enum wimlib_compression_type ctype)
108 return wim_ctype_info[(unsigned)ctype].default_nonsolid_chunk_size;
111 /* Return the default chunk size to use for the specified compression type in
112 * solid resources. */
114 wim_default_solid_chunk_size(enum wimlib_compression_type ctype)
116 return wim_ctype_info[(unsigned)ctype].default_solid_chunk_size;
119 /* Return the default compression type to use in solid resources. */
120 static enum wimlib_compression_type
121 wim_default_solid_compression_type(void)
123 return WIMLIB_COMPRESSION_TYPE_LZMS;
127 is_blob_in_solid_resource(struct blob_descriptor *blob, void *_ignore)
129 return blob->blob_location == BLOB_IN_WIM &&
130 (blob->rdesc->flags & WIM_RESHDR_FLAG_SOLID);
134 wim_has_solid_resources(const WIMStruct *wim)
136 return for_blob_in_table(wim->blob_table, is_blob_in_solid_resource, NULL);
142 WIMStruct *wim = CALLOC(1, sizeof(WIMStruct));
147 filedes_invalidate(&wim->in_fd);
148 filedes_invalidate(&wim->out_fd);
149 wim->out_solid_compression_type = wim_default_solid_compression_type();
150 wim->out_solid_chunk_size = wim_default_solid_chunk_size(
151 wim->out_solid_compression_type);
155 /* API function documented in wimlib.h */
157 wimlib_create_new_wim(enum wimlib_compression_type ctype, WIMStruct **wim_ret)
162 ret = wimlib_global_init(0);
167 return WIMLIB_ERR_INVALID_PARAM;
169 if (!wim_compression_type_valid(ctype))
170 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
172 wim = new_wim_struct();
174 return WIMLIB_ERR_NOMEM;
176 /* Fill in wim->hdr with default values */
177 wim->hdr.magic = WIM_MAGIC;
178 wim->hdr.wim_version = WIM_VERSION_DEFAULT;
179 wim->hdr.part_number = 1;
180 wim->hdr.total_parts = 1;
181 wim->compression_type = WIMLIB_COMPRESSION_TYPE_NONE;
183 /* Set the output compression type */
184 wim->out_compression_type = ctype;
185 wim->out_chunk_size = wim_default_nonsolid_chunk_size(ctype);
187 /* Allocate an empty XML info and blob table */
188 wim->xml_info = xml_new_info_struct();
189 wim->blob_table = new_blob_table(64);
190 if (!wim->xml_info || !wim->blob_table) {
192 return WIMLIB_ERR_NOMEM;
200 unload_image_metadata(struct wim_image_metadata *imd)
202 free_dentry_tree(imd->root_dentry, NULL);
203 imd->root_dentry = NULL;
204 free_wim_security_data(imd->security_data);
205 imd->security_data = NULL;
206 INIT_HLIST_HEAD(&imd->inode_list);
209 /* Release a reference to the specified image metadata. This assumes that no
210 * WIMStruct has the image selected. */
212 put_image_metadata(struct wim_image_metadata *imd)
214 struct blob_descriptor *blob, *tmp;
218 wimlib_assert(imd->refcnt > 0);
219 if (--imd->refcnt != 0)
221 wimlib_assert(imd->selected_refcnt == 0);
222 unload_image_metadata(imd);
223 list_for_each_entry_safe(blob, tmp, &imd->unhashed_blobs, unhashed_list)
224 free_blob_descriptor(blob);
225 free_blob_descriptor(imd->metadata_blob);
229 /* Appends the specified image metadata structure to the array of image metadata
230 * for a WIM, and increments the image count. */
232 append_image_metadata(WIMStruct *wim, struct wim_image_metadata *imd)
234 struct wim_image_metadata **imd_array;
236 if (!wim_has_metadata(wim))
237 return WIMLIB_ERR_METADATA_NOT_FOUND;
239 if (wim->hdr.image_count >= MAX_IMAGES)
240 return WIMLIB_ERR_IMAGE_COUNT;
242 imd_array = REALLOC(wim->image_metadata,
243 sizeof(wim->image_metadata[0]) * (wim->hdr.image_count + 1));
246 return WIMLIB_ERR_NOMEM;
247 wim->image_metadata = imd_array;
248 imd_array[wim->hdr.image_count++] = imd;
252 static struct wim_image_metadata *
253 new_image_metadata(struct blob_descriptor *metadata_blob,
254 struct wim_security_data *security_data)
256 struct wim_image_metadata *imd;
258 imd = CALLOC(1, sizeof(*imd));
262 metadata_blob->is_metadata = 1;
264 imd->selected_refcnt = 0;
265 imd->root_dentry = NULL;
266 imd->security_data = security_data;
267 imd->metadata_blob = metadata_blob;
268 INIT_HLIST_HEAD(&imd->inode_list);
269 INIT_LIST_HEAD(&imd->unhashed_blobs);
270 imd->stats_outdated = false;
274 /* Create an image metadata structure for a new empty image. */
275 struct wim_image_metadata *
276 new_empty_image_metadata(void)
278 struct blob_descriptor *metadata_blob;
279 struct wim_security_data *security_data;
280 struct wim_image_metadata *imd;
282 metadata_blob = new_blob_descriptor();
283 security_data = new_wim_security_data();
284 if (metadata_blob && security_data) {
285 metadata_blob->refcnt = 1;
286 imd = new_image_metadata(metadata_blob, security_data);
290 free_blob_descriptor(metadata_blob);
295 /* Create an image metadata structure that refers to the specified metadata
296 * resource and is initially not loaded. */
297 struct wim_image_metadata *
298 new_unloaded_image_metadata(struct blob_descriptor *metadata_blob)
300 wimlib_assert(metadata_blob->blob_location == BLOB_IN_WIM);
301 return new_image_metadata(metadata_blob, NULL);
305 * Load the metadata for the specified WIM image into memory and set it
306 * as the WIMStruct's currently selected image.
309 * The WIMStruct for the WIM.
311 * The 1-based index of the image in the WIM to select.
313 * On success, 0 will be returned, wim->current_image will be set to
314 * @image, and wim_get_current_image_metadata() can be used to retrieve
315 * metadata information for the image.
317 * On failure, WIMLIB_ERR_INVALID_IMAGE, WIMLIB_ERR_METADATA_NOT_FOUND,
318 * or another error code will be returned.
321 select_wim_image(WIMStruct *wim, int image)
323 struct wim_image_metadata *imd;
326 if (image == WIMLIB_NO_IMAGE)
327 return WIMLIB_ERR_INVALID_IMAGE;
329 if (image == wim->current_image)
332 if (image < 1 || image > wim->hdr.image_count)
333 return WIMLIB_ERR_INVALID_IMAGE;
335 if (!wim_has_metadata(wim))
336 return WIMLIB_ERR_METADATA_NOT_FOUND;
338 deselect_current_wim_image(wim);
340 imd = wim->image_metadata[image - 1];
341 if (!is_image_loaded(imd)) {
342 ret = read_metadata_resource(imd);
346 wim->current_image = image;
347 imd->selected_refcnt++;
352 * Deselect the WIMStruct's currently selected image, if any. To reduce memory
353 * usage, possibly unload the newly deselected image's metadata from memory.
356 deselect_current_wim_image(WIMStruct *wim)
358 struct wim_image_metadata *imd;
360 if (wim->current_image == WIMLIB_NO_IMAGE)
362 imd = wim_get_current_image_metadata(wim);
363 wimlib_assert(imd->selected_refcnt > 0);
364 imd->selected_refcnt--;
365 wim->current_image = WIMLIB_NO_IMAGE;
367 if (can_unload_image(imd)) {
368 wimlib_assert(list_empty(&imd->unhashed_blobs));
369 unload_image_metadata(imd);
374 * Calls a function on images in the WIM. If @image is WIMLIB_ALL_IMAGES,
375 * @visitor is called on the WIM once for each image, with each image selected
376 * as the current image in turn. If @image is a certain image, @visitor is
377 * called on the WIM only once, with that image selected.
380 for_image(WIMStruct *wim, int image, int (*visitor)(WIMStruct *))
387 if (image == WIMLIB_ALL_IMAGES) {
389 end = wim->hdr.image_count;
390 } else if (image >= 1 && image <= wim->hdr.image_count) {
394 return WIMLIB_ERR_INVALID_IMAGE;
396 for (i = start; i <= end; i++) {
397 ret = select_wim_image(wim, i);
407 /* API function documented in wimlib.h */
409 wimlib_resolve_image(WIMStruct *wim, const tchar *image_name_or_num)
415 if (!image_name_or_num || !*image_name_or_num)
416 return WIMLIB_NO_IMAGE;
418 if (!tstrcasecmp(image_name_or_num, T("all"))
419 || !tstrcasecmp(image_name_or_num, T("*")))
420 return WIMLIB_ALL_IMAGES;
421 image = tstrtol(image_name_or_num, &p, 10);
422 if (p != image_name_or_num && *p == T('\0') && image > 0) {
423 if (image > wim->hdr.image_count)
424 return WIMLIB_NO_IMAGE;
427 for (i = 1; i <= wim->hdr.image_count; i++) {
428 if (!tstrcmp(image_name_or_num,
429 wimlib_get_image_name(wim, i)))
432 return WIMLIB_NO_IMAGE;
436 /* API function documented in wimlib.h */
438 wimlib_print_available_images(const WIMStruct *wim, int image)
444 if (image == WIMLIB_ALL_IMAGES) {
445 n = tprintf(T("Available Images:\n"));
447 last = wim->hdr.image_count;
448 } else if (image >= 1 && image <= wim->hdr.image_count) {
449 n = tprintf(T("Information for Image %d\n"), image);
453 tprintf(T("wimlib_print_available_images(): Invalid image %d"),
457 for (i = 0; i < n - 1; i++)
460 for (i = first; i <= last; i++)
461 xml_print_image_info(wim->xml_info, i);
464 /* API function documented in wimlib.h */
466 wimlib_get_wim_info(WIMStruct *wim, struct wimlib_wim_info *info)
468 memset(info, 0, sizeof(struct wimlib_wim_info));
469 copy_guid(info->guid, wim->hdr.guid);
470 info->image_count = wim->hdr.image_count;
471 info->boot_index = wim->hdr.boot_idx;
472 info->wim_version = wim->hdr.wim_version;
473 info->chunk_size = wim->chunk_size;
474 info->part_number = wim->hdr.part_number;
475 info->total_parts = wim->hdr.total_parts;
476 info->compression_type = wim->compression_type;
477 info->total_bytes = xml_get_total_bytes(wim->xml_info);
478 info->has_integrity_table = wim_has_integrity_table(wim);
479 info->opened_from_file = (wim->filename != NULL);
480 info->is_readonly = (wim->hdr.flags & WIM_HDR_FLAG_READONLY) ||
481 (wim->hdr.total_parts != 1) ||
482 (wim->filename && taccess(wim->filename, W_OK));
483 info->has_rpfix = (wim->hdr.flags & WIM_HDR_FLAG_RP_FIX) != 0;
484 info->is_marked_readonly = (wim->hdr.flags & WIM_HDR_FLAG_READONLY) != 0;
485 info->write_in_progress = (wim->hdr.flags & WIM_HDR_FLAG_WRITE_IN_PROGRESS) != 0;
486 info->metadata_only = (wim->hdr.flags & WIM_HDR_FLAG_METADATA_ONLY) != 0;
487 info->resource_only = (wim->hdr.flags & WIM_HDR_FLAG_RESOURCE_ONLY) != 0;
488 info->spanned = (wim->hdr.flags & WIM_HDR_FLAG_SPANNED) != 0;
489 info->pipable = wim_is_pipable(wim);
493 /* API function documented in wimlib.h */
495 wimlib_set_wim_info(WIMStruct *wim, const struct wimlib_wim_info *info, int which)
497 if (which & ~(WIMLIB_CHANGE_READONLY_FLAG |
499 WIMLIB_CHANGE_BOOT_INDEX |
500 WIMLIB_CHANGE_RPFIX_FLAG))
501 return WIMLIB_ERR_INVALID_PARAM;
503 if ((which & WIMLIB_CHANGE_BOOT_INDEX) &&
504 info->boot_index > wim->hdr.image_count)
505 return WIMLIB_ERR_INVALID_IMAGE;
507 if (which & WIMLIB_CHANGE_READONLY_FLAG) {
508 if (info->is_marked_readonly)
509 wim->hdr.flags |= WIM_HDR_FLAG_READONLY;
511 wim->hdr.flags &= ~WIM_HDR_FLAG_READONLY;
514 if (which & WIMLIB_CHANGE_GUID)
515 copy_guid(wim->hdr.guid, info->guid);
517 if (which & WIMLIB_CHANGE_BOOT_INDEX)
518 wim->hdr.boot_idx = info->boot_index;
520 if (which & WIMLIB_CHANGE_RPFIX_FLAG) {
522 wim->hdr.flags |= WIM_HDR_FLAG_RP_FIX;
524 wim->hdr.flags &= ~WIM_HDR_FLAG_RP_FIX;
529 /* API function documented in wimlib.h */
531 wimlib_set_output_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 wim->out_compression_type = ctype;
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_nonsolid_chunk_size(ctype);
545 /* API function documented in wimlib.h */
547 wimlib_set_output_pack_compression_type(WIMStruct *wim,
548 enum wimlib_compression_type ctype)
550 if (!wim_compression_type_valid(ctype))
551 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
553 /* Solid resources can't be uncompressed. */
554 if (ctype == WIMLIB_COMPRESSION_TYPE_NONE)
555 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
557 wim->out_solid_compression_type = ctype;
559 /* Reset the chunk size if it's no longer valid. */
560 if (!wim_chunk_size_valid(wim->out_solid_chunk_size, ctype))
561 wim->out_solid_chunk_size = wim_default_solid_chunk_size(ctype);
565 /* API function documented in wimlib.h */
567 wimlib_set_output_chunk_size(WIMStruct *wim, u32 chunk_size)
569 if (chunk_size == 0) {
570 wim->out_chunk_size =
571 wim_default_nonsolid_chunk_size(wim->out_compression_type);
575 if (!wim_chunk_size_valid(chunk_size, wim->out_compression_type))
576 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
578 wim->out_chunk_size = chunk_size;
582 /* API function documented in wimlib.h */
584 wimlib_set_output_pack_chunk_size(WIMStruct *wim, u32 chunk_size)
586 if (chunk_size == 0) {
587 wim->out_solid_chunk_size =
588 wim_default_solid_chunk_size(wim->out_solid_compression_type);
592 if (!wim_chunk_size_valid(chunk_size, wim->out_solid_compression_type))
593 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
595 wim->out_solid_chunk_size = chunk_size;
599 /* API function documented in wimlib.h */
600 WIMLIBAPI const tchar *
601 wimlib_get_compression_type_string(enum wimlib_compression_type ctype)
603 if (!wim_compression_type_valid(ctype))
606 return wim_ctype_info[(unsigned)ctype].name;
610 wimlib_register_progress_function(WIMStruct *wim,
611 wimlib_progress_func_t progfunc,
614 wim->progfunc = progfunc;
615 wim->progctx = progctx;
619 open_wim_file(const tchar *filename, struct filedes *fd_ret)
623 raw_fd = topen(filename, O_RDONLY | O_BINARY);
625 ERROR_WITH_ERRNO("Can't open \"%"TS"\" read-only", filename);
626 return WIMLIB_ERR_OPEN;
628 filedes_init(fd_ret, raw_fd);
633 * Begins the reading of a WIM file; opens the file and reads its header and
634 * blob table, and optionally checks the integrity.
637 begin_read(WIMStruct *wim, const void *wim_filename_or_fd, int open_flags)
640 const tchar *wimfile;
642 if (open_flags & WIMLIB_OPEN_FLAG_FROM_PIPE) {
644 filedes_init(&wim->in_fd, *(const int*)wim_filename_or_fd);
645 wim->in_fd.is_pipe = 1;
647 wimfile = wim_filename_or_fd;
648 ret = open_wim_file(wimfile, &wim->in_fd);
652 /* The absolute path to the WIM is requested so that
653 * wimlib_overwrite() still works even if the process changes
654 * its working directory. This actually happens if a WIM is
655 * mounted read-write, since the FUSE thread changes directory
656 * to "/", and it needs to be able to find the WIM file again.
658 * This will break if the full path to the WIM changes in the
659 * intervening time...
661 * Warning: in Windows native builds, realpath() calls the
662 * replacement function in win32_replacements.c.
664 wim->filename = realpath(wimfile, NULL);
665 if (!wim->filename) {
666 ERROR_WITH_ERRNO("Failed to get full path to file "
667 "\"%"TS"\"", wimfile);
669 return WIMLIB_ERR_NOMEM;
671 return WIMLIB_ERR_NO_FILENAME;
675 ret = read_wim_header(wim, &wim->hdr);
679 if (wim->hdr.flags & WIM_HDR_FLAG_WRITE_IN_PROGRESS) {
680 WARNING("The WIM_HDR_FLAG_WRITE_IN_PROGRESS flag is set in the header of\n"
681 " \"%"TS"\". It may be being changed by another process,\n"
682 " or a process may have crashed while writing the WIM.",
686 if (open_flags & WIMLIB_OPEN_FLAG_WRITE_ACCESS) {
687 ret = can_modify_wim(wim);
692 if ((open_flags & WIMLIB_OPEN_FLAG_ERROR_IF_SPLIT) &&
693 (wim->hdr.total_parts != 1))
694 return WIMLIB_ERR_IS_SPLIT_WIM;
696 /* If the boot index is invalid, print a warning and set it to 0 */
697 if (wim->hdr.boot_idx > wim->hdr.image_count) {
698 WARNING("Ignoring invalid boot index.");
699 wim->hdr.boot_idx = 0;
702 /* Check and cache the compression type */
703 if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESSION) {
704 if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESS_LZX) {
705 wim->compression_type = WIMLIB_COMPRESSION_TYPE_LZX;
706 } else if (wim->hdr.flags & (WIM_HDR_FLAG_COMPRESS_XPRESS |
707 WIM_HDR_FLAG_COMPRESS_XPRESS_2)) {
708 wim->compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
709 } else if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESS_LZMS) {
710 wim->compression_type = WIMLIB_COMPRESSION_TYPE_LZMS;
712 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
715 wim->compression_type = WIMLIB_COMPRESSION_TYPE_NONE;
717 wim->out_compression_type = wim->compression_type;
719 /* Check and cache the chunk size. */
720 wim->chunk_size = wim->hdr.chunk_size;
721 wim->out_chunk_size = wim->chunk_size;
722 if (!wim_chunk_size_valid(wim->chunk_size, wim->compression_type)) {
723 ERROR("Invalid chunk size (%"PRIu32" bytes) "
724 "for compression type %"TS"!", wim->chunk_size,
725 wimlib_get_compression_type_string(wim->compression_type));
726 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
729 if (open_flags & WIMLIB_OPEN_FLAG_CHECK_INTEGRITY) {
730 ret = check_wim_integrity(wim);
731 if (ret == WIM_INTEGRITY_NONEXISTENT) {
732 WARNING("\"%"TS"\" does not contain integrity "
733 "information. Skipping integrity check.",
735 } else if (ret == WIM_INTEGRITY_NOT_OK) {
736 return WIMLIB_ERR_INTEGRITY;
737 } else if (ret != WIM_INTEGRITY_OK) {
742 if (wim->hdr.image_count != 0 && wim->hdr.part_number == 1) {
743 wim->image_metadata = CALLOC(wim->hdr.image_count,
744 sizeof(wim->image_metadata[0]));
745 if (!wim->image_metadata)
746 return WIMLIB_ERR_NOMEM;
749 if (open_flags & WIMLIB_OPEN_FLAG_FROM_PIPE) {
750 wim->blob_table = new_blob_table(64);
751 if (!wim->blob_table)
752 return WIMLIB_ERR_NOMEM;
754 if (wim->hdr.blob_table_reshdr.uncompressed_size == 0 &&
755 wim->hdr.xml_data_reshdr.uncompressed_size == 0)
756 return WIMLIB_ERR_WIM_IS_INCOMPLETE;
758 ret = read_wim_xml_data(wim);
762 if (xml_get_image_count(wim->xml_info) != wim->hdr.image_count) {
763 ERROR("The WIM's header is inconsistent with its XML data.\n"
764 " Please submit a bug report if you believe this "
765 "WIM file should be considered valid.");
766 return WIMLIB_ERR_IMAGE_COUNT;
769 ret = read_blob_table(wim);
777 open_wim_as_WIMStruct(const void *wim_filename_or_fd, int open_flags,
779 wimlib_progress_func_t progfunc, void *progctx)
784 ret = wimlib_global_init(0);
788 wim = new_wim_struct();
790 return WIMLIB_ERR_NOMEM;
792 wim->progfunc = progfunc;
793 wim->progctx = progctx;
795 ret = begin_read(wim, wim_filename_or_fd, open_flags);
805 /* API function documented in wimlib.h */
807 wimlib_open_wim_with_progress(const tchar *wimfile, int open_flags,
809 wimlib_progress_func_t progfunc, void *progctx)
811 if (open_flags & ~(WIMLIB_OPEN_FLAG_CHECK_INTEGRITY |
812 WIMLIB_OPEN_FLAG_ERROR_IF_SPLIT |
813 WIMLIB_OPEN_FLAG_WRITE_ACCESS))
814 return WIMLIB_ERR_INVALID_PARAM;
816 if (!wimfile || !*wimfile || !wim_ret)
817 return WIMLIB_ERR_INVALID_PARAM;
819 return open_wim_as_WIMStruct(wimfile, open_flags, wim_ret,
823 /* API function documented in wimlib.h */
825 wimlib_open_wim(const tchar *wimfile, int open_flags, WIMStruct **wim_ret)
827 return wimlib_open_wim_with_progress(wimfile, open_flags, wim_ret,
831 /* Checksum all blobs that are unhashed (other than the metadata blobs), merging
832 * them into the blob table as needed. This is a no-op unless files have been
833 * added to an image in the same WIMStruct. */
835 wim_checksum_unhashed_blobs(WIMStruct *wim)
839 if (!wim_has_metadata(wim))
841 for (int i = 0; i < wim->hdr.image_count; i++) {
842 struct blob_descriptor *blob, *tmp;
843 struct wim_image_metadata *imd = wim->image_metadata[i];
844 image_for_each_unhashed_blob_safe(blob, tmp, imd) {
845 struct blob_descriptor *new_blob;
846 ret = hash_unhashed_blob(blob, wim->blob_table, &new_blob);
849 if (new_blob != blob)
850 free_blob_descriptor(blob);
857 * can_modify_wim - Check if a given WIM is writeable. This is only the case if
858 * it meets the following three conditions:
860 * 1. Write access is allowed to the underlying file (if any) at the filesystem level.
861 * 2. The WIM is not part of a spanned set.
862 * 3. The WIM_HDR_FLAG_READONLY flag is not set in the WIM header.
864 * Return value is 0 if writable; WIMLIB_ERR_WIM_IS_READONLY otherwise.
867 can_modify_wim(WIMStruct *wim)
870 if (taccess(wim->filename, W_OK)) {
871 ERROR_WITH_ERRNO("Can't modify \"%"TS"\"", wim->filename);
872 return WIMLIB_ERR_WIM_IS_READONLY;
875 if (wim->hdr.total_parts != 1) {
876 ERROR("Cannot modify \"%"TS"\": is part of a split WIM",
878 return WIMLIB_ERR_WIM_IS_READONLY;
880 if (wim->hdr.flags & WIM_HDR_FLAG_READONLY) {
881 ERROR("Cannot modify \"%"TS"\": is marked read-only",
883 return WIMLIB_ERR_WIM_IS_READONLY;
888 /* Release a reference to a WIMStruct. If the reference count reaches 0, the
889 * WIMStruct is freed. */
891 wim_decrement_refcnt(WIMStruct *wim)
893 wimlib_assert(wim->refcnt > 0);
894 if (--wim->refcnt != 0)
896 if (filedes_valid(&wim->in_fd))
897 filedes_close(&wim->in_fd);
898 if (filedes_valid(&wim->out_fd))
899 filedes_close(&wim->out_fd);
900 wimlib_free_decompressor(wim->decompressor);
901 xml_free_info_struct(wim->xml_info);
906 /* API function documented in wimlib.h */
908 wimlib_free(WIMStruct *wim)
913 /* The blob table and image metadata are freed immediately, but other
914 * members of the WIMStruct such as the input file descriptor are
915 * retained until no more exported resources reference the WIMStruct. */
917 free_blob_table(wim->blob_table);
918 wim->blob_table = NULL;
919 if (wim->image_metadata != NULL) {
920 deselect_current_wim_image(wim);
921 for (int i = 0; i < wim->hdr.image_count; i++)
922 put_image_metadata(wim->image_metadata[i]);
923 FREE(wim->image_metadata);
924 wim->image_metadata = NULL;
927 wim_decrement_refcnt(wim);
930 /* API function documented in wimlib.h */
932 wimlib_get_version(void)
934 return (WIMLIB_MAJOR_VERSION << 20) |
935 (WIMLIB_MINOR_VERSION << 10) |
936 WIMLIB_PATCH_VERSION;
939 WIMLIBAPI const tchar *
940 wimlib_get_version_string(void)
942 return T(PACKAGE_VERSION);
945 static bool lib_initialized = false;
946 static struct mutex lib_initialization_mutex = MUTEX_INITIALIZER;
948 /* API function documented in wimlib.h */
950 wimlib_global_init(int init_flags)
957 mutex_lock(&lib_initialization_mutex);
962 if (!wimlib_error_file)
963 wimlib_error_file = stderr;
965 ret = WIMLIB_ERR_INVALID_PARAM;
966 if (init_flags & ~(WIMLIB_INIT_FLAG_ASSUME_UTF8 |
967 WIMLIB_INIT_FLAG_DONT_ACQUIRE_PRIVILEGES |
968 WIMLIB_INIT_FLAG_STRICT_CAPTURE_PRIVILEGES |
969 WIMLIB_INIT_FLAG_STRICT_APPLY_PRIVILEGES |
970 WIMLIB_INIT_FLAG_DEFAULT_CASE_SENSITIVE |
971 WIMLIB_INIT_FLAG_DEFAULT_CASE_INSENSITIVE))
974 ret = WIMLIB_ERR_INVALID_PARAM;
975 if ((init_flags & (WIMLIB_INIT_FLAG_DEFAULT_CASE_SENSITIVE |
976 WIMLIB_INIT_FLAG_DEFAULT_CASE_INSENSITIVE))
977 == (WIMLIB_INIT_FLAG_DEFAULT_CASE_SENSITIVE |
978 WIMLIB_INIT_FLAG_DEFAULT_CASE_INSENSITIVE))
984 ret = win32_global_init(init_flags);
989 if (init_flags & WIMLIB_INIT_FLAG_DEFAULT_CASE_SENSITIVE)
990 default_ignore_case = false;
991 else if (init_flags & WIMLIB_INIT_FLAG_DEFAULT_CASE_INSENSITIVE)
992 default_ignore_case = true;
993 lib_initialized = true;
996 mutex_unlock(&lib_initialization_mutex);
1001 /* API function documented in wimlib.h */
1003 wimlib_global_cleanup(void)
1005 if (!lib_initialized)
1008 mutex_lock(&lib_initialization_mutex);
1010 if (!lib_initialized)
1013 xml_global_cleanup();
1015 win32_global_cleanup();
1018 wimlib_set_error_file(NULL);
1019 lib_initialized = false;
1022 mutex_unlock(&lib_initialization_mutex);