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/.
33 #include "wimlib/assert.h"
34 #include "wimlib/blob_table.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/wim.h"
42 #include "wimlib/xml.h"
43 #include "wimlib/win32.h"
45 /* Information about the available compression types for the WIM format. */
50 u32 default_nonsolid_chunk_size;
51 u32 default_solid_chunk_size;
52 } wim_ctype_info[] = {
53 [WIMLIB_COMPRESSION_TYPE_NONE] = {
57 .default_nonsolid_chunk_size = 0,
58 .default_solid_chunk_size = 0,
60 [WIMLIB_COMPRESSION_TYPE_XPRESS] = {
62 .min_chunk_size = 4096,
63 .max_chunk_size = 65536,
64 .default_nonsolid_chunk_size = 32768,
65 .default_solid_chunk_size = 32768,
67 [WIMLIB_COMPRESSION_TYPE_LZX] = {
69 .min_chunk_size = 32768,
70 .max_chunk_size = 2097152,
71 .default_nonsolid_chunk_size = 32768,
72 .default_solid_chunk_size = 32768,
74 [WIMLIB_COMPRESSION_TYPE_LZMS] = {
76 .min_chunk_size = 32768,
77 .max_chunk_size = 1073741824,
78 .default_nonsolid_chunk_size = 131072,
79 .default_solid_chunk_size = 67108864,
83 /* Is the specified compression type valid? */
85 wim_compression_type_valid(enum wimlib_compression_type ctype)
87 return (unsigned)ctype < ARRAY_LEN(wim_ctype_info) &&
88 wim_ctype_info[(unsigned)ctype].name != NULL;
91 /* Is the specified chunk size valid for the compression type? */
93 wim_chunk_size_valid(u32 chunk_size, enum wimlib_compression_type ctype)
95 if (!(chunk_size == 0 || is_power_of_2(chunk_size)))
98 return chunk_size >= wim_ctype_info[(unsigned)ctype].min_chunk_size &&
99 chunk_size <= wim_ctype_info[(unsigned)ctype].max_chunk_size;
102 /* Return the default chunk size to use for the specified compression type in
103 * non-solid resources. */
105 wim_default_nonsolid_chunk_size(enum wimlib_compression_type ctype)
107 return wim_ctype_info[(unsigned)ctype].default_nonsolid_chunk_size;
110 /* Return the default chunk size to use for the specified compression type in
111 * solid resources. */
113 wim_default_solid_chunk_size(enum wimlib_compression_type ctype)
115 return wim_ctype_info[(unsigned)ctype].default_solid_chunk_size;
118 /* Return the default compression type to use in solid resources. */
119 static enum wimlib_compression_type
120 wim_default_solid_compression_type(void)
122 return WIMLIB_COMPRESSION_TYPE_LZMS;
126 is_blob_in_solid_resource(struct blob_descriptor *blob, void *_ignore)
128 return blob->blob_location == BLOB_IN_WIM &&
129 (blob->rdesc->flags & WIM_RESHDR_FLAG_SOLID);
133 wim_has_solid_resources(const WIMStruct *wim)
135 return for_blob_in_table(wim->blob_table, is_blob_in_solid_resource, NULL);
141 WIMStruct *wim = CALLOC(1, sizeof(WIMStruct));
146 filedes_invalidate(&wim->in_fd);
147 filedes_invalidate(&wim->out_fd);
148 wim->out_solid_compression_type = wim_default_solid_compression_type();
149 wim->out_solid_chunk_size = wim_default_solid_chunk_size(
150 wim->out_solid_compression_type);
154 /* API function documented in wimlib.h */
156 wimlib_create_new_wim(enum wimlib_compression_type ctype, WIMStruct **wim_ret)
161 ret = wimlib_global_init(0);
166 return WIMLIB_ERR_INVALID_PARAM;
168 if (!wim_compression_type_valid(ctype))
169 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
171 wim = new_wim_struct();
173 return WIMLIB_ERR_NOMEM;
175 /* Fill in wim->hdr with default values */
176 wim->hdr.magic = WIM_MAGIC;
177 wim->hdr.wim_version = WIM_VERSION_DEFAULT;
178 wim->hdr.part_number = 1;
179 wim->hdr.total_parts = 1;
180 wim->compression_type = WIMLIB_COMPRESSION_TYPE_NONE;
182 /* Set the output compression type */
183 wim->out_compression_type = ctype;
184 wim->out_chunk_size = wim_default_nonsolid_chunk_size(ctype);
186 /* Allocate an empty XML info and blob table */
187 wim->xml_info = xml_new_info_struct();
188 wim->blob_table = new_blob_table(64);
189 if (!wim->xml_info || !wim->blob_table) {
191 return WIMLIB_ERR_NOMEM;
199 unload_image_metadata(struct wim_image_metadata *imd)
201 free_dentry_tree(imd->root_dentry, NULL);
202 imd->root_dentry = NULL;
203 free_wim_security_data(imd->security_data);
204 imd->security_data = NULL;
205 INIT_HLIST_HEAD(&imd->inode_list);
208 /* Release a reference to the specified image metadata. This assumes that no
209 * WIMStruct has the image selected. */
211 put_image_metadata(struct wim_image_metadata *imd)
213 struct blob_descriptor *blob, *tmp;
217 wimlib_assert(imd->refcnt > 0);
218 if (--imd->refcnt != 0)
220 wimlib_assert(imd->selected_refcnt == 0);
221 unload_image_metadata(imd);
222 list_for_each_entry_safe(blob, tmp, &imd->unhashed_blobs, unhashed_list)
223 free_blob_descriptor(blob);
224 free_blob_descriptor(imd->metadata_blob);
228 /* Appends the specified image metadata structure to the array of image metadata
229 * for a WIM, and increments the image count. */
231 append_image_metadata(WIMStruct *wim, struct wim_image_metadata *imd)
233 struct wim_image_metadata **imd_array;
235 if (!wim_has_metadata(wim))
236 return WIMLIB_ERR_METADATA_NOT_FOUND;
238 if (wim->hdr.image_count >= MAX_IMAGES)
239 return WIMLIB_ERR_IMAGE_COUNT;
241 imd_array = REALLOC(wim->image_metadata,
242 sizeof(wim->image_metadata[0]) * (wim->hdr.image_count + 1));
245 return WIMLIB_ERR_NOMEM;
246 wim->image_metadata = imd_array;
247 imd_array[wim->hdr.image_count++] = imd;
251 static struct wim_image_metadata *
252 new_image_metadata(struct blob_descriptor *metadata_blob,
253 struct wim_security_data *security_data)
255 struct wim_image_metadata *imd;
257 imd = CALLOC(1, sizeof(*imd));
261 metadata_blob->is_metadata = 1;
263 imd->selected_refcnt = 0;
264 imd->root_dentry = NULL;
265 imd->security_data = security_data;
266 imd->metadata_blob = metadata_blob;
267 INIT_HLIST_HEAD(&imd->inode_list);
268 INIT_LIST_HEAD(&imd->unhashed_blobs);
269 imd->stats_outdated = false;
273 /* Create an image metadata structure for a new empty image. */
274 struct wim_image_metadata *
275 new_empty_image_metadata(void)
277 struct blob_descriptor *metadata_blob;
278 struct wim_security_data *security_data;
279 struct wim_image_metadata *imd;
281 metadata_blob = new_blob_descriptor();
282 security_data = new_wim_security_data();
283 if (metadata_blob && security_data) {
284 metadata_blob->refcnt = 1;
285 imd = new_image_metadata(metadata_blob, security_data);
289 free_blob_descriptor(metadata_blob);
294 /* Create an image metadata structure that refers to the specified metadata
295 * resource and is initially not loaded. */
296 struct wim_image_metadata *
297 new_unloaded_image_metadata(struct blob_descriptor *metadata_blob)
299 wimlib_assert(metadata_blob->blob_location == BLOB_IN_WIM);
300 return new_image_metadata(metadata_blob, NULL);
304 * Load the metadata for the specified WIM image into memory and set it
305 * as the WIMStruct's currently selected image.
308 * The WIMStruct for the WIM.
310 * The 1-based index of the image in the WIM to select.
312 * On success, 0 will be returned, wim->current_image will be set to
313 * @image, and wim_get_current_image_metadata() can be used to retrieve
314 * metadata information for the image.
316 * On failure, WIMLIB_ERR_INVALID_IMAGE, WIMLIB_ERR_METADATA_NOT_FOUND,
317 * or another error code will be returned.
320 select_wim_image(WIMStruct *wim, int image)
322 struct wim_image_metadata *imd;
325 if (image == WIMLIB_NO_IMAGE)
326 return WIMLIB_ERR_INVALID_IMAGE;
328 if (image == wim->current_image)
331 if (image < 1 || image > wim->hdr.image_count)
332 return WIMLIB_ERR_INVALID_IMAGE;
334 if (!wim_has_metadata(wim))
335 return WIMLIB_ERR_METADATA_NOT_FOUND;
337 deselect_current_wim_image(wim);
339 imd = wim->image_metadata[image - 1];
340 if (!is_image_loaded(imd)) {
341 ret = read_metadata_resource(imd);
345 wim->current_image = image;
346 imd->selected_refcnt++;
351 * Deselect the WIMStruct's currently selected image, if any. To reduce memory
352 * usage, possibly unload the newly deselected image's metadata from memory.
355 deselect_current_wim_image(WIMStruct *wim)
357 struct wim_image_metadata *imd;
359 if (wim->current_image == WIMLIB_NO_IMAGE)
361 imd = wim_get_current_image_metadata(wim);
362 wimlib_assert(imd->selected_refcnt > 0);
363 imd->selected_refcnt--;
364 wim->current_image = WIMLIB_NO_IMAGE;
366 if (can_unload_image(imd)) {
367 wimlib_assert(list_empty(&imd->unhashed_blobs));
368 unload_image_metadata(imd);
373 * Calls a function on images in the WIM. If @image is WIMLIB_ALL_IMAGES,
374 * @visitor is called on the WIM once for each image, with each image selected
375 * as the current image in turn. If @image is a certain image, @visitor is
376 * called on the WIM only once, with that image selected.
379 for_image(WIMStruct *wim, int image, int (*visitor)(WIMStruct *))
386 if (image == WIMLIB_ALL_IMAGES) {
388 end = wim->hdr.image_count;
389 } else if (image >= 1 && image <= wim->hdr.image_count) {
393 return WIMLIB_ERR_INVALID_IMAGE;
395 for (i = start; i <= end; i++) {
396 ret = select_wim_image(wim, i);
406 /* API function documented in wimlib.h */
408 wimlib_resolve_image(WIMStruct *wim, const tchar *image_name_or_num)
414 if (!image_name_or_num || !*image_name_or_num)
415 return WIMLIB_NO_IMAGE;
417 if (!tstrcasecmp(image_name_or_num, T("all"))
418 || !tstrcasecmp(image_name_or_num, T("*")))
419 return WIMLIB_ALL_IMAGES;
420 image = tstrtol(image_name_or_num, &p, 10);
421 if (p != image_name_or_num && *p == T('\0') && image > 0) {
422 if (image > wim->hdr.image_count)
423 return WIMLIB_NO_IMAGE;
426 for (i = 1; i <= wim->hdr.image_count; i++) {
427 if (!tstrcmp(image_name_or_num,
428 wimlib_get_image_name(wim, i)))
431 return WIMLIB_NO_IMAGE;
435 /* API function documented in wimlib.h */
437 wimlib_print_available_images(const WIMStruct *wim, int image)
443 if (image == WIMLIB_ALL_IMAGES) {
444 n = tprintf(T("Available Images:\n"));
446 last = wim->hdr.image_count;
447 } else if (image >= 1 && image <= wim->hdr.image_count) {
448 n = tprintf(T("Information for Image %d\n"), image);
452 tprintf(T("wimlib_print_available_images(): Invalid image %d"),
456 for (i = 0; i < n - 1; i++)
459 for (i = first; i <= last; i++)
460 xml_print_image_info(wim->xml_info, i);
463 /* API function documented in wimlib.h */
465 wimlib_get_wim_info(WIMStruct *wim, struct wimlib_wim_info *info)
467 memset(info, 0, sizeof(struct wimlib_wim_info));
468 copy_guid(info->guid, wim->hdr.guid);
469 info->image_count = wim->hdr.image_count;
470 info->boot_index = wim->hdr.boot_idx;
471 info->wim_version = wim->hdr.wim_version;
472 info->chunk_size = wim->chunk_size;
473 info->part_number = wim->hdr.part_number;
474 info->total_parts = wim->hdr.total_parts;
475 info->compression_type = wim->compression_type;
476 info->total_bytes = xml_get_total_bytes(wim->xml_info);
477 info->has_integrity_table = wim_has_integrity_table(wim);
478 info->opened_from_file = (wim->filename != NULL);
479 info->is_readonly = (wim->hdr.flags & WIM_HDR_FLAG_READONLY) ||
480 (wim->hdr.total_parts != 1) ||
481 (wim->filename && taccess(wim->filename, W_OK));
482 info->has_rpfix = (wim->hdr.flags & WIM_HDR_FLAG_RP_FIX) != 0;
483 info->is_marked_readonly = (wim->hdr.flags & WIM_HDR_FLAG_READONLY) != 0;
484 info->write_in_progress = (wim->hdr.flags & WIM_HDR_FLAG_WRITE_IN_PROGRESS) != 0;
485 info->metadata_only = (wim->hdr.flags & WIM_HDR_FLAG_METADATA_ONLY) != 0;
486 info->resource_only = (wim->hdr.flags & WIM_HDR_FLAG_RESOURCE_ONLY) != 0;
487 info->spanned = (wim->hdr.flags & WIM_HDR_FLAG_SPANNED) != 0;
488 info->pipable = wim_is_pipable(wim);
492 /* API function documented in wimlib.h */
494 wimlib_set_wim_info(WIMStruct *wim, const struct wimlib_wim_info *info, int which)
496 if (which & ~(WIMLIB_CHANGE_READONLY_FLAG |
498 WIMLIB_CHANGE_BOOT_INDEX |
499 WIMLIB_CHANGE_RPFIX_FLAG))
500 return WIMLIB_ERR_INVALID_PARAM;
502 if ((which & WIMLIB_CHANGE_BOOT_INDEX) &&
503 info->boot_index > wim->hdr.image_count)
504 return WIMLIB_ERR_INVALID_IMAGE;
506 if (which & WIMLIB_CHANGE_READONLY_FLAG) {
507 if (info->is_marked_readonly)
508 wim->hdr.flags |= WIM_HDR_FLAG_READONLY;
510 wim->hdr.flags &= ~WIM_HDR_FLAG_READONLY;
513 if (which & WIMLIB_CHANGE_GUID)
514 copy_guid(wim->hdr.guid, info->guid);
516 if (which & WIMLIB_CHANGE_BOOT_INDEX)
517 wim->hdr.boot_idx = info->boot_index;
519 if (which & WIMLIB_CHANGE_RPFIX_FLAG) {
521 wim->hdr.flags |= WIM_HDR_FLAG_RP_FIX;
523 wim->hdr.flags &= ~WIM_HDR_FLAG_RP_FIX;
528 /* API function documented in wimlib.h */
530 wimlib_set_output_compression_type(WIMStruct *wim,
531 enum wimlib_compression_type ctype)
533 if (!wim_compression_type_valid(ctype))
534 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
536 wim->out_compression_type = ctype;
538 /* Reset the chunk size if it's no longer valid. */
539 if (!wim_chunk_size_valid(wim->out_chunk_size, ctype))
540 wim->out_chunk_size = wim_default_nonsolid_chunk_size(ctype);
544 /* API function documented in wimlib.h */
546 wimlib_set_output_pack_compression_type(WIMStruct *wim,
547 enum wimlib_compression_type ctype)
549 if (!wim_compression_type_valid(ctype))
550 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
552 /* Solid resources can't be uncompressed. */
553 if (ctype == WIMLIB_COMPRESSION_TYPE_NONE)
554 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
556 wim->out_solid_compression_type = ctype;
558 /* Reset the chunk size if it's no longer valid. */
559 if (!wim_chunk_size_valid(wim->out_solid_chunk_size, ctype))
560 wim->out_solid_chunk_size = wim_default_solid_chunk_size(ctype);
564 /* API function documented in wimlib.h */
566 wimlib_set_output_chunk_size(WIMStruct *wim, u32 chunk_size)
568 if (chunk_size == 0) {
569 wim->out_chunk_size =
570 wim_default_nonsolid_chunk_size(wim->out_compression_type);
574 if (!wim_chunk_size_valid(chunk_size, wim->out_compression_type))
575 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
577 wim->out_chunk_size = chunk_size;
581 /* API function documented in wimlib.h */
583 wimlib_set_output_pack_chunk_size(WIMStruct *wim, u32 chunk_size)
585 if (chunk_size == 0) {
586 wim->out_solid_chunk_size =
587 wim_default_solid_chunk_size(wim->out_solid_compression_type);
591 if (!wim_chunk_size_valid(chunk_size, wim->out_solid_compression_type))
592 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
594 wim->out_solid_chunk_size = chunk_size;
598 /* API function documented in wimlib.h */
599 WIMLIBAPI const tchar *
600 wimlib_get_compression_type_string(enum wimlib_compression_type ctype)
602 if (!wim_compression_type_valid(ctype))
605 return wim_ctype_info[(unsigned)ctype].name;
609 wimlib_register_progress_function(WIMStruct *wim,
610 wimlib_progress_func_t progfunc,
613 wim->progfunc = progfunc;
614 wim->progctx = progctx;
618 open_wim_file(const tchar *filename, struct filedes *fd_ret)
622 raw_fd = topen(filename, O_RDONLY | O_BINARY);
624 ERROR_WITH_ERRNO("Can't open \"%"TS"\" read-only", filename);
625 return WIMLIB_ERR_OPEN;
627 filedes_init(fd_ret, raw_fd);
632 * Begins the reading of a WIM file; opens the file and reads its header and
633 * blob table, and optionally checks the integrity.
636 begin_read(WIMStruct *wim, const void *wim_filename_or_fd, int open_flags)
639 const tchar *wimfile;
641 if (open_flags & WIMLIB_OPEN_FLAG_FROM_PIPE) {
643 filedes_init(&wim->in_fd, *(const int*)wim_filename_or_fd);
644 wim->in_fd.is_pipe = 1;
646 wimfile = wim_filename_or_fd;
647 ret = open_wim_file(wimfile, &wim->in_fd);
651 /* The absolute path to the WIM is requested so that
652 * wimlib_overwrite() still works even if the process changes
653 * its working directory. This actually happens if a WIM is
654 * mounted read-write, since the FUSE thread changes directory
655 * to "/", and it needs to be able to find the WIM file again.
657 * This will break if the full path to the WIM changes in the
658 * intervening time...
660 * Warning: in Windows native builds, realpath() calls the
661 * replacement function in win32_replacements.c.
663 wim->filename = realpath(wimfile, NULL);
664 if (!wim->filename) {
665 ERROR_WITH_ERRNO("Failed to get full path to file "
666 "\"%"TS"\"", wimfile);
668 return WIMLIB_ERR_NOMEM;
670 return WIMLIB_ERR_NO_FILENAME;
674 ret = read_wim_header(wim, &wim->hdr);
678 if (wim->hdr.flags & WIM_HDR_FLAG_WRITE_IN_PROGRESS) {
679 WARNING("The WIM_HDR_FLAG_WRITE_IN_PROGRESS flag is set in the header of\n"
680 " \"%"TS"\". It may be being changed by another process,\n"
681 " or a process may have crashed while writing the WIM.",
685 if (open_flags & WIMLIB_OPEN_FLAG_WRITE_ACCESS) {
686 ret = can_modify_wim(wim);
691 if ((open_flags & WIMLIB_OPEN_FLAG_ERROR_IF_SPLIT) &&
692 (wim->hdr.total_parts != 1))
693 return WIMLIB_ERR_IS_SPLIT_WIM;
695 /* If the boot index is invalid, print a warning and set it to 0 */
696 if (wim->hdr.boot_idx > wim->hdr.image_count) {
697 WARNING("Ignoring invalid boot index.");
698 wim->hdr.boot_idx = 0;
701 /* Check and cache the compression type */
702 if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESSION) {
703 if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESS_LZX) {
704 wim->compression_type = WIMLIB_COMPRESSION_TYPE_LZX;
705 } else if (wim->hdr.flags & (WIM_HDR_FLAG_COMPRESS_XPRESS |
706 WIM_HDR_FLAG_COMPRESS_XPRESS_2)) {
707 wim->compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
708 } else if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESS_LZMS) {
709 wim->compression_type = WIMLIB_COMPRESSION_TYPE_LZMS;
711 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
714 wim->compression_type = WIMLIB_COMPRESSION_TYPE_NONE;
716 wim->out_compression_type = wim->compression_type;
718 /* Check and cache the chunk size. */
719 wim->chunk_size = wim->hdr.chunk_size;
720 wim->out_chunk_size = wim->chunk_size;
721 if (!wim_chunk_size_valid(wim->chunk_size, wim->compression_type)) {
722 ERROR("Invalid chunk size (%"PRIu32" bytes) "
723 "for compression type %"TS"!", wim->chunk_size,
724 wimlib_get_compression_type_string(wim->compression_type));
725 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
728 if (open_flags & WIMLIB_OPEN_FLAG_CHECK_INTEGRITY) {
729 ret = check_wim_integrity(wim);
730 if (ret == WIM_INTEGRITY_NONEXISTENT) {
731 WARNING("\"%"TS"\" does not contain integrity "
732 "information. Skipping integrity check.",
734 } else if (ret == WIM_INTEGRITY_NOT_OK) {
735 return WIMLIB_ERR_INTEGRITY;
736 } else if (ret != WIM_INTEGRITY_OK) {
741 if (wim->hdr.image_count != 0 && wim->hdr.part_number == 1) {
742 wim->image_metadata = CALLOC(wim->hdr.image_count,
743 sizeof(wim->image_metadata[0]));
744 if (!wim->image_metadata)
745 return WIMLIB_ERR_NOMEM;
748 if (open_flags & WIMLIB_OPEN_FLAG_FROM_PIPE) {
749 wim->blob_table = new_blob_table(64);
750 if (!wim->blob_table)
751 return WIMLIB_ERR_NOMEM;
753 if (wim->hdr.blob_table_reshdr.uncompressed_size == 0 &&
754 wim->hdr.xml_data_reshdr.uncompressed_size == 0)
755 return WIMLIB_ERR_WIM_IS_INCOMPLETE;
757 ret = read_wim_xml_data(wim);
761 if (xml_get_image_count(wim->xml_info) != wim->hdr.image_count) {
762 ERROR("The WIM's header is inconsistent with its XML data.\n"
763 " Please submit a bug report if you believe this "
764 "WIM file should be considered valid.");
765 return WIMLIB_ERR_IMAGE_COUNT;
768 ret = read_blob_table(wim);
776 open_wim_as_WIMStruct(const void *wim_filename_or_fd, int open_flags,
778 wimlib_progress_func_t progfunc, void *progctx)
783 ret = wimlib_global_init(0);
787 wim = new_wim_struct();
789 return WIMLIB_ERR_NOMEM;
791 wim->progfunc = progfunc;
792 wim->progctx = progctx;
794 ret = begin_read(wim, wim_filename_or_fd, open_flags);
804 /* API function documented in wimlib.h */
806 wimlib_open_wim_with_progress(const tchar *wimfile, int open_flags,
808 wimlib_progress_func_t progfunc, void *progctx)
810 if (open_flags & ~(WIMLIB_OPEN_FLAG_CHECK_INTEGRITY |
811 WIMLIB_OPEN_FLAG_ERROR_IF_SPLIT |
812 WIMLIB_OPEN_FLAG_WRITE_ACCESS))
813 return WIMLIB_ERR_INVALID_PARAM;
815 if (!wimfile || !*wimfile || !wim_ret)
816 return WIMLIB_ERR_INVALID_PARAM;
818 return open_wim_as_WIMStruct(wimfile, open_flags, wim_ret,
822 /* API function documented in wimlib.h */
824 wimlib_open_wim(const tchar *wimfile, int open_flags, WIMStruct **wim_ret)
826 return wimlib_open_wim_with_progress(wimfile, open_flags, wim_ret,
830 /* Checksum all blobs that are unhashed (other than the metadata blobs), merging
831 * them into the blob table as needed. This is a no-op unless files have been
832 * added to an image in the same WIMStruct. */
834 wim_checksum_unhashed_blobs(WIMStruct *wim)
838 if (!wim_has_metadata(wim))
840 for (int i = 0; i < wim->hdr.image_count; i++) {
841 struct blob_descriptor *blob, *tmp;
842 struct wim_image_metadata *imd = wim->image_metadata[i];
843 image_for_each_unhashed_blob_safe(blob, tmp, imd) {
844 struct blob_descriptor *new_blob;
845 ret = hash_unhashed_blob(blob, wim->blob_table, &new_blob);
848 if (new_blob != blob)
849 free_blob_descriptor(blob);
856 * can_modify_wim - Check if a given WIM is writeable. This is only the case if
857 * it meets the following three conditions:
859 * 1. Write access is allowed to the underlying file (if any) at the filesystem level.
860 * 2. The WIM is not part of a spanned set.
861 * 3. The WIM_HDR_FLAG_READONLY flag is not set in the WIM header.
863 * Return value is 0 if writable; WIMLIB_ERR_WIM_IS_READONLY otherwise.
866 can_modify_wim(WIMStruct *wim)
869 if (taccess(wim->filename, W_OK)) {
870 ERROR_WITH_ERRNO("Can't modify \"%"TS"\"", wim->filename);
871 return WIMLIB_ERR_WIM_IS_READONLY;
874 if (wim->hdr.total_parts != 1) {
875 ERROR("Cannot modify \"%"TS"\": is part of a split WIM",
877 return WIMLIB_ERR_WIM_IS_READONLY;
879 if (wim->hdr.flags & WIM_HDR_FLAG_READONLY) {
880 ERROR("Cannot modify \"%"TS"\": is marked read-only",
882 return WIMLIB_ERR_WIM_IS_READONLY;
887 /* Release a reference to a WIMStruct. If the reference count reaches 0, the
888 * WIMStruct is freed. */
890 wim_decrement_refcnt(WIMStruct *wim)
892 wimlib_assert(wim->refcnt > 0);
893 if (--wim->refcnt != 0)
895 if (filedes_valid(&wim->in_fd))
896 filedes_close(&wim->in_fd);
897 if (filedes_valid(&wim->out_fd))
898 filedes_close(&wim->out_fd);
899 wimlib_free_decompressor(wim->decompressor);
900 xml_free_info_struct(wim->xml_info);
905 /* API function documented in wimlib.h */
907 wimlib_free(WIMStruct *wim)
912 /* The blob table and image metadata are freed immediately, but other
913 * members of the WIMStruct such as the input file descriptor are
914 * retained until no more exported resources reference the WIMStruct. */
916 free_blob_table(wim->blob_table);
917 wim->blob_table = NULL;
918 if (wim->image_metadata != NULL) {
919 deselect_current_wim_image(wim);
920 for (int i = 0; i < wim->hdr.image_count; i++)
921 put_image_metadata(wim->image_metadata[i]);
922 FREE(wim->image_metadata);
923 wim->image_metadata = NULL;
926 wim_decrement_refcnt(wim);
929 /* API function documented in wimlib.h */
931 wimlib_get_version(void)
933 return (WIMLIB_MAJOR_VERSION << 20) |
934 (WIMLIB_MINOR_VERSION << 10) |
935 WIMLIB_PATCH_VERSION;
938 WIMLIBAPI const tchar *
939 wimlib_get_version_string(void)
941 return T(PACKAGE_VERSION);
944 static bool lib_initialized = false;
945 static pthread_mutex_t lib_initialization_mutex = PTHREAD_MUTEX_INITIALIZER;
947 /* API function documented in wimlib.h */
949 wimlib_global_init(int init_flags)
956 pthread_mutex_lock(&lib_initialization_mutex);
961 #ifdef ENABLE_ERROR_MESSAGES
962 if (!wimlib_error_file)
963 wimlib_error_file = stderr;
966 ret = WIMLIB_ERR_INVALID_PARAM;
967 if (init_flags & ~(WIMLIB_INIT_FLAG_ASSUME_UTF8 |
968 WIMLIB_INIT_FLAG_DONT_ACQUIRE_PRIVILEGES |
969 WIMLIB_INIT_FLAG_STRICT_CAPTURE_PRIVILEGES |
970 WIMLIB_INIT_FLAG_STRICT_APPLY_PRIVILEGES |
971 WIMLIB_INIT_FLAG_DEFAULT_CASE_SENSITIVE |
972 WIMLIB_INIT_FLAG_DEFAULT_CASE_INSENSITIVE))
975 ret = WIMLIB_ERR_INVALID_PARAM;
976 if ((init_flags & (WIMLIB_INIT_FLAG_DEFAULT_CASE_SENSITIVE |
977 WIMLIB_INIT_FLAG_DEFAULT_CASE_INSENSITIVE))
978 == (WIMLIB_INIT_FLAG_DEFAULT_CASE_SENSITIVE |
979 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 pthread_mutex_unlock(&lib_initialization_mutex);
1001 /* API function documented in wimlib.h */
1003 wimlib_global_cleanup(void)
1005 if (!lib_initialized)
1008 pthread_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 pthread_mutex_unlock(&lib_initialization_mutex);