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 wim->xml_info = xml_new_info_struct();
176 wim->blob_table = new_blob_table(64);
177 if (!wim->xml_info || !wim->blob_table) {
179 return WIMLIB_ERR_NOMEM;
182 /* Fill in wim->hdr with default values */
183 wim->hdr.magic = WIM_MAGIC;
184 wim->hdr.wim_version = WIM_VERSION_DEFAULT;
186 wim->hdr.chunk_size = 0;
187 generate_guid(wim->hdr.guid);
188 wim->hdr.part_number = 1;
189 wim->hdr.total_parts = 1;
190 wim->hdr.image_count = 0;
191 wim->hdr.boot_idx = 0;
193 wim->compression_type = WIMLIB_COMPRESSION_TYPE_NONE;
194 wim->chunk_size = wim->hdr.chunk_size;
196 /* Set the output compression type */
197 wim->out_compression_type = ctype;
198 wim->out_chunk_size = wim_default_nonsolid_chunk_size(ctype);
205 unload_image_metadata(struct wim_image_metadata *imd)
207 free_dentry_tree(imd->root_dentry, NULL);
208 imd->root_dentry = NULL;
209 free_wim_security_data(imd->security_data);
210 imd->security_data = NULL;
211 INIT_HLIST_HEAD(&imd->inode_list);
214 /* Release a reference to the specified image metadata. This assumes that no
215 * WIMStruct has the image selected. */
217 put_image_metadata(struct wim_image_metadata *imd)
219 struct blob_descriptor *blob, *tmp;
223 wimlib_assert(imd->refcnt > 0);
224 if (--imd->refcnt != 0)
226 wimlib_assert(imd->selected_refcnt == 0);
227 unload_image_metadata(imd);
228 list_for_each_entry_safe(blob, tmp, &imd->unhashed_blobs, unhashed_list)
229 free_blob_descriptor(blob);
230 free_blob_descriptor(imd->metadata_blob);
234 /* Appends the specified image metadata structure to the array of image metadata
235 * for a WIM, and increments the image count. */
237 append_image_metadata(WIMStruct *wim, struct wim_image_metadata *imd)
239 struct wim_image_metadata **imd_array;
241 if (!wim_has_metadata(wim))
242 return WIMLIB_ERR_METADATA_NOT_FOUND;
244 if (wim->hdr.image_count >= MAX_IMAGES)
245 return WIMLIB_ERR_IMAGE_COUNT;
247 imd_array = REALLOC(wim->image_metadata,
248 sizeof(wim->image_metadata[0]) * (wim->hdr.image_count + 1));
251 return WIMLIB_ERR_NOMEM;
252 wim->image_metadata = imd_array;
253 imd_array[wim->hdr.image_count++] = imd;
257 static struct wim_image_metadata *
258 new_image_metadata(struct blob_descriptor *metadata_blob,
259 struct wim_security_data *security_data)
261 struct wim_image_metadata *imd;
263 imd = CALLOC(1, sizeof(*imd));
267 metadata_blob->is_metadata = 1;
269 imd->selected_refcnt = 0;
270 imd->root_dentry = NULL;
271 imd->security_data = security_data;
272 imd->metadata_blob = metadata_blob;
273 INIT_HLIST_HEAD(&imd->inode_list);
274 INIT_LIST_HEAD(&imd->unhashed_blobs);
275 imd->stats_outdated = false;
279 /* Create an image metadata structure for a new empty image. */
280 struct wim_image_metadata *
281 new_empty_image_metadata(void)
283 struct blob_descriptor *metadata_blob;
284 struct wim_security_data *security_data;
285 struct wim_image_metadata *imd;
287 metadata_blob = new_blob_descriptor();
288 security_data = new_wim_security_data();
289 if (metadata_blob && security_data) {
290 metadata_blob->refcnt = 1;
291 imd = new_image_metadata(metadata_blob, security_data);
295 free_blob_descriptor(metadata_blob);
300 /* Create an image metadata structure that refers to the specified metadata
301 * resource and is initially not loaded. */
302 struct wim_image_metadata *
303 new_unloaded_image_metadata(struct blob_descriptor *metadata_blob)
305 wimlib_assert(metadata_blob->blob_location == BLOB_IN_WIM);
306 return new_image_metadata(metadata_blob, NULL);
310 * Load the metadata for the specified WIM image into memory and set it
311 * as the WIMStruct's currently selected image.
314 * The WIMStruct for the WIM.
316 * The 1-based index of the image in the WIM to select.
318 * On success, 0 will be returned, wim->current_image will be set to
319 * @image, and wim_get_current_image_metadata() can be used to retrieve
320 * metadata information for the image.
322 * On failure, WIMLIB_ERR_INVALID_IMAGE, WIMLIB_ERR_METADATA_NOT_FOUND,
323 * or another error code will be returned.
326 select_wim_image(WIMStruct *wim, int image)
328 struct wim_image_metadata *imd;
331 if (image == WIMLIB_NO_IMAGE)
332 return WIMLIB_ERR_INVALID_IMAGE;
334 if (image == wim->current_image)
337 if (image < 1 || image > wim->hdr.image_count)
338 return WIMLIB_ERR_INVALID_IMAGE;
340 if (!wim_has_metadata(wim))
341 return WIMLIB_ERR_METADATA_NOT_FOUND;
343 deselect_current_wim_image(wim);
345 imd = wim->image_metadata[image - 1];
346 if (!is_image_loaded(imd)) {
347 ret = read_metadata_resource(imd);
351 wim->current_image = image;
352 imd->selected_refcnt++;
357 * Deselect the WIMStruct's currently selected image, if any. To reduce memory
358 * usage, possibly unload the newly deselected image's metadata from memory.
361 deselect_current_wim_image(WIMStruct *wim)
363 struct wim_image_metadata *imd;
365 if (wim->current_image == WIMLIB_NO_IMAGE)
367 imd = wim_get_current_image_metadata(wim);
368 wimlib_assert(imd->selected_refcnt > 0);
369 imd->selected_refcnt--;
370 wim->current_image = WIMLIB_NO_IMAGE;
372 if (can_unload_image(imd)) {
373 wimlib_assert(list_empty(&imd->unhashed_blobs));
374 unload_image_metadata(imd);
379 * Calls a function on images in the WIM. If @image is WIMLIB_ALL_IMAGES,
380 * @visitor is called on the WIM once for each image, with each image selected
381 * as the current image in turn. If @image is a certain image, @visitor is
382 * called on the WIM only once, with that image selected.
385 for_image(WIMStruct *wim, int image, int (*visitor)(WIMStruct *))
392 if (image == WIMLIB_ALL_IMAGES) {
394 end = wim->hdr.image_count;
395 } else if (image >= 1 && image <= wim->hdr.image_count) {
399 return WIMLIB_ERR_INVALID_IMAGE;
401 for (i = start; i <= end; i++) {
402 ret = select_wim_image(wim, i);
412 /* API function documented in wimlib.h */
414 wimlib_resolve_image(WIMStruct *wim, const tchar *image_name_or_num)
420 if (!image_name_or_num || !*image_name_or_num)
421 return WIMLIB_NO_IMAGE;
423 if (!tstrcasecmp(image_name_or_num, T("all"))
424 || !tstrcasecmp(image_name_or_num, T("*")))
425 return WIMLIB_ALL_IMAGES;
426 image = tstrtol(image_name_or_num, &p, 10);
427 if (p != image_name_or_num && *p == T('\0') && image > 0) {
428 if (image > wim->hdr.image_count)
429 return WIMLIB_NO_IMAGE;
432 for (i = 1; i <= wim->hdr.image_count; i++) {
433 if (!tstrcmp(image_name_or_num,
434 wimlib_get_image_name(wim, i)))
437 return WIMLIB_NO_IMAGE;
441 /* API function documented in wimlib.h */
443 wimlib_print_available_images(const WIMStruct *wim, int image)
449 if (image == WIMLIB_ALL_IMAGES) {
450 n = tprintf(T("Available Images:\n"));
452 last = wim->hdr.image_count;
453 } else if (image >= 1 && image <= wim->hdr.image_count) {
454 n = tprintf(T("Information for Image %d\n"), image);
458 tprintf(T("wimlib_print_available_images(): Invalid image %d"),
462 for (i = 0; i < n - 1; i++)
465 for (i = first; i <= last; i++)
466 xml_print_image_info(wim->xml_info, i);
469 /* API function documented in wimlib.h */
471 wimlib_get_wim_info(WIMStruct *wim, struct wimlib_wim_info *info)
473 memset(info, 0, sizeof(struct wimlib_wim_info));
474 copy_guid(info->guid, wim->hdr.guid);
475 info->image_count = wim->hdr.image_count;
476 info->boot_index = wim->hdr.boot_idx;
477 info->wim_version = wim->hdr.wim_version;
478 info->chunk_size = wim->chunk_size;
479 info->part_number = wim->hdr.part_number;
480 info->total_parts = wim->hdr.total_parts;
481 info->compression_type = wim->compression_type;
482 info->total_bytes = xml_get_total_bytes(wim->xml_info);
483 info->has_integrity_table = wim_has_integrity_table(wim);
484 info->opened_from_file = (wim->filename != NULL);
485 info->is_readonly = (wim->hdr.flags & WIM_HDR_FLAG_READONLY) ||
486 (wim->hdr.total_parts != 1) ||
487 (wim->filename && taccess(wim->filename, W_OK));
488 info->has_rpfix = (wim->hdr.flags & WIM_HDR_FLAG_RP_FIX) != 0;
489 info->is_marked_readonly = (wim->hdr.flags & WIM_HDR_FLAG_READONLY) != 0;
490 info->write_in_progress = (wim->hdr.flags & WIM_HDR_FLAG_WRITE_IN_PROGRESS) != 0;
491 info->metadata_only = (wim->hdr.flags & WIM_HDR_FLAG_METADATA_ONLY) != 0;
492 info->resource_only = (wim->hdr.flags & WIM_HDR_FLAG_RESOURCE_ONLY) != 0;
493 info->spanned = (wim->hdr.flags & WIM_HDR_FLAG_SPANNED) != 0;
494 info->pipable = wim_is_pipable(wim);
498 /* API function documented in wimlib.h */
500 wimlib_set_wim_info(WIMStruct *wim, const struct wimlib_wim_info *info, int which)
502 if (which & ~(WIMLIB_CHANGE_READONLY_FLAG |
504 WIMLIB_CHANGE_BOOT_INDEX |
505 WIMLIB_CHANGE_RPFIX_FLAG))
506 return WIMLIB_ERR_INVALID_PARAM;
508 if ((which & WIMLIB_CHANGE_BOOT_INDEX) &&
509 info->boot_index > wim->hdr.image_count)
510 return WIMLIB_ERR_INVALID_IMAGE;
512 if (which & WIMLIB_CHANGE_READONLY_FLAG) {
513 if (info->is_marked_readonly)
514 wim->hdr.flags |= WIM_HDR_FLAG_READONLY;
516 wim->hdr.flags &= ~WIM_HDR_FLAG_READONLY;
519 if (which & WIMLIB_CHANGE_GUID)
520 copy_guid(wim->hdr.guid, info->guid);
522 if (which & WIMLIB_CHANGE_BOOT_INDEX)
523 wim->hdr.boot_idx = info->boot_index;
525 if (which & WIMLIB_CHANGE_RPFIX_FLAG) {
527 wim->hdr.flags |= WIM_HDR_FLAG_RP_FIX;
529 wim->hdr.flags &= ~WIM_HDR_FLAG_RP_FIX;
534 /* API function documented in wimlib.h */
536 wimlib_set_output_compression_type(WIMStruct *wim,
537 enum wimlib_compression_type ctype)
539 if (!wim_compression_type_valid(ctype))
540 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
542 wim->out_compression_type = ctype;
544 /* Reset the chunk size if it's no longer valid. */
545 if (!wim_chunk_size_valid(wim->out_chunk_size, ctype))
546 wim->out_chunk_size = wim_default_nonsolid_chunk_size(ctype);
550 /* API function documented in wimlib.h */
552 wimlib_set_output_pack_compression_type(WIMStruct *wim,
553 enum wimlib_compression_type ctype)
555 if (!wim_compression_type_valid(ctype))
556 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
558 /* Solid resources can't be uncompressed. */
559 if (ctype == WIMLIB_COMPRESSION_TYPE_NONE)
560 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
562 wim->out_solid_compression_type = ctype;
564 /* Reset the chunk size if it's no longer valid. */
565 if (!wim_chunk_size_valid(wim->out_solid_chunk_size, ctype))
566 wim->out_solid_chunk_size = wim_default_solid_chunk_size(ctype);
570 /* API function documented in wimlib.h */
572 wimlib_set_output_chunk_size(WIMStruct *wim, u32 chunk_size)
574 if (chunk_size == 0) {
575 wim->out_chunk_size =
576 wim_default_nonsolid_chunk_size(wim->out_compression_type);
580 if (!wim_chunk_size_valid(chunk_size, wim->out_compression_type))
581 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
583 wim->out_chunk_size = chunk_size;
587 /* API function documented in wimlib.h */
589 wimlib_set_output_pack_chunk_size(WIMStruct *wim, u32 chunk_size)
591 if (chunk_size == 0) {
592 wim->out_solid_chunk_size =
593 wim_default_solid_chunk_size(wim->out_solid_compression_type);
597 if (!wim_chunk_size_valid(chunk_size, wim->out_solid_compression_type))
598 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
600 wim->out_solid_chunk_size = chunk_size;
604 /* API function documented in wimlib.h */
605 WIMLIBAPI const tchar *
606 wimlib_get_compression_type_string(enum wimlib_compression_type ctype)
608 if (!wim_compression_type_valid(ctype))
611 return wim_ctype_info[(unsigned)ctype].name;
615 wimlib_register_progress_function(WIMStruct *wim,
616 wimlib_progress_func_t progfunc,
619 wim->progfunc = progfunc;
620 wim->progctx = progctx;
624 open_wim_file(const tchar *filename, struct filedes *fd_ret)
628 raw_fd = topen(filename, O_RDONLY | O_BINARY);
630 ERROR_WITH_ERRNO("Can't open \"%"TS"\" read-only", filename);
631 return WIMLIB_ERR_OPEN;
633 filedes_init(fd_ret, raw_fd);
638 * Begins the reading of a WIM file; opens the file and reads its header and
639 * blob table, and optionally checks the integrity.
642 begin_read(WIMStruct *wim, const void *wim_filename_or_fd, int open_flags)
645 const tchar *wimfile;
647 if (open_flags & WIMLIB_OPEN_FLAG_FROM_PIPE) {
649 filedes_init(&wim->in_fd, *(const int*)wim_filename_or_fd);
650 wim->in_fd.is_pipe = 1;
652 wimfile = wim_filename_or_fd;
653 ret = open_wim_file(wimfile, &wim->in_fd);
657 /* The absolute path to the WIM is requested so that
658 * wimlib_overwrite() still works even if the process changes
659 * its working directory. This actually happens if a WIM is
660 * mounted read-write, since the FUSE thread changes directory
661 * to "/", and it needs to be able to find the WIM file again.
663 * This will break if the full path to the WIM changes in the
664 * intervening time...
666 * Warning: in Windows native builds, realpath() calls the
667 * replacement function in win32_replacements.c.
669 wim->filename = realpath(wimfile, NULL);
670 if (!wim->filename) {
671 ERROR_WITH_ERRNO("Failed to get full path to file "
672 "\"%"TS"\"", wimfile);
674 return WIMLIB_ERR_NOMEM;
676 return WIMLIB_ERR_NO_FILENAME;
680 ret = read_wim_header(wim, &wim->hdr);
684 if (wim->hdr.flags & WIM_HDR_FLAG_WRITE_IN_PROGRESS) {
685 WARNING("The WIM_HDR_FLAG_WRITE_IN_PROGRESS flag is set in the header of\n"
686 " \"%"TS"\". It may be being changed by another process,\n"
687 " or a process may have crashed while writing the WIM.",
691 if (open_flags & WIMLIB_OPEN_FLAG_WRITE_ACCESS) {
692 ret = can_modify_wim(wim);
697 if ((open_flags & WIMLIB_OPEN_FLAG_ERROR_IF_SPLIT) &&
698 (wim->hdr.total_parts != 1))
699 return WIMLIB_ERR_IS_SPLIT_WIM;
701 /* If the boot index is invalid, print a warning and set it to 0 */
702 if (wim->hdr.boot_idx > wim->hdr.image_count) {
703 WARNING("Ignoring invalid boot index.");
704 wim->hdr.boot_idx = 0;
707 /* Check and cache the compression type */
708 if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESSION) {
709 if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESS_LZX) {
710 wim->compression_type = WIMLIB_COMPRESSION_TYPE_LZX;
711 } else if (wim->hdr.flags & (WIM_HDR_FLAG_COMPRESS_XPRESS |
712 WIM_HDR_FLAG_COMPRESS_XPRESS_2)) {
713 wim->compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
714 } else if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESS_LZMS) {
715 wim->compression_type = WIMLIB_COMPRESSION_TYPE_LZMS;
717 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
720 wim->compression_type = WIMLIB_COMPRESSION_TYPE_NONE;
722 wim->out_compression_type = wim->compression_type;
724 /* Check and cache the chunk size. */
725 wim->chunk_size = wim->hdr.chunk_size;
726 wim->out_chunk_size = wim->chunk_size;
727 if (!wim_chunk_size_valid(wim->chunk_size, wim->compression_type)) {
728 ERROR("Invalid chunk size (%"PRIu32" bytes) "
729 "for compression type %"TS"!", wim->chunk_size,
730 wimlib_get_compression_type_string(wim->compression_type));
731 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
734 if (open_flags & WIMLIB_OPEN_FLAG_CHECK_INTEGRITY) {
735 ret = check_wim_integrity(wim);
736 if (ret == WIM_INTEGRITY_NONEXISTENT) {
737 WARNING("\"%"TS"\" does not contain integrity "
738 "information. Skipping integrity check.",
740 } else if (ret == WIM_INTEGRITY_NOT_OK) {
741 return WIMLIB_ERR_INTEGRITY;
742 } else if (ret != WIM_INTEGRITY_OK) {
747 if (wim->hdr.image_count != 0 && wim->hdr.part_number == 1) {
748 wim->image_metadata = CALLOC(wim->hdr.image_count,
749 sizeof(wim->image_metadata[0]));
750 if (!wim->image_metadata)
751 return WIMLIB_ERR_NOMEM;
754 if (open_flags & WIMLIB_OPEN_FLAG_FROM_PIPE) {
755 wim->blob_table = new_blob_table(64);
756 if (!wim->blob_table)
757 return WIMLIB_ERR_NOMEM;
759 if (wim->hdr.blob_table_reshdr.uncompressed_size == 0 &&
760 wim->hdr.xml_data_reshdr.uncompressed_size == 0)
761 return WIMLIB_ERR_WIM_IS_INCOMPLETE;
763 ret = read_wim_xml_data(wim);
767 if (xml_get_image_count(wim->xml_info) != wim->hdr.image_count) {
768 ERROR("The WIM's header is inconsistent with its XML data.\n"
769 " Please submit a bug report if you believe this "
770 "WIM file should be considered valid.");
771 return WIMLIB_ERR_IMAGE_COUNT;
774 ret = read_blob_table(wim);
782 open_wim_as_WIMStruct(const void *wim_filename_or_fd, int open_flags,
784 wimlib_progress_func_t progfunc, void *progctx)
789 ret = wimlib_global_init(0);
793 wim = new_wim_struct();
795 return WIMLIB_ERR_NOMEM;
797 wim->progfunc = progfunc;
798 wim->progctx = progctx;
800 ret = begin_read(wim, wim_filename_or_fd, open_flags);
810 /* API function documented in wimlib.h */
812 wimlib_open_wim_with_progress(const tchar *wimfile, int open_flags,
814 wimlib_progress_func_t progfunc, void *progctx)
816 if (open_flags & ~(WIMLIB_OPEN_FLAG_CHECK_INTEGRITY |
817 WIMLIB_OPEN_FLAG_ERROR_IF_SPLIT |
818 WIMLIB_OPEN_FLAG_WRITE_ACCESS))
819 return WIMLIB_ERR_INVALID_PARAM;
821 if (!wimfile || !*wimfile || !wim_ret)
822 return WIMLIB_ERR_INVALID_PARAM;
824 return open_wim_as_WIMStruct(wimfile, open_flags, wim_ret,
828 /* API function documented in wimlib.h */
830 wimlib_open_wim(const tchar *wimfile, int open_flags, WIMStruct **wim_ret)
832 return wimlib_open_wim_with_progress(wimfile, open_flags, wim_ret,
836 /* Checksum all blobs that are unhashed (other than the metadata blobs), merging
837 * them into the blob table as needed. This is a no-op unless files have been
838 * added to an image in the same WIMStruct. */
840 wim_checksum_unhashed_blobs(WIMStruct *wim)
844 if (!wim_has_metadata(wim))
846 for (int i = 0; i < wim->hdr.image_count; i++) {
847 struct blob_descriptor *blob, *tmp;
848 struct wim_image_metadata *imd = wim->image_metadata[i];
849 image_for_each_unhashed_blob_safe(blob, tmp, imd) {
850 struct blob_descriptor *new_blob;
851 ret = hash_unhashed_blob(blob, wim->blob_table, &new_blob);
854 if (new_blob != blob)
855 free_blob_descriptor(blob);
862 * can_modify_wim - Check if a given WIM is writeable. This is only the case if
863 * it meets the following three conditions:
865 * 1. Write access is allowed to the underlying file (if any) at the filesystem level.
866 * 2. The WIM is not part of a spanned set.
867 * 3. The WIM_HDR_FLAG_READONLY flag is not set in the WIM header.
869 * Return value is 0 if writable; WIMLIB_ERR_WIM_IS_READONLY otherwise.
872 can_modify_wim(WIMStruct *wim)
875 if (taccess(wim->filename, W_OK)) {
876 ERROR_WITH_ERRNO("Can't modify \"%"TS"\"", wim->filename);
877 return WIMLIB_ERR_WIM_IS_READONLY;
880 if (wim->hdr.total_parts != 1) {
881 ERROR("Cannot modify \"%"TS"\": is part of a split WIM",
883 return WIMLIB_ERR_WIM_IS_READONLY;
885 if (wim->hdr.flags & WIM_HDR_FLAG_READONLY) {
886 ERROR("Cannot modify \"%"TS"\": is marked read-only",
888 return WIMLIB_ERR_WIM_IS_READONLY;
893 /* Release a reference to a WIMStruct. If the reference count reaches 0, the
894 * WIMStruct is freed. */
896 wim_decrement_refcnt(WIMStruct *wim)
898 wimlib_assert(wim->refcnt > 0);
899 if (--wim->refcnt != 0)
901 if (filedes_valid(&wim->in_fd))
902 filedes_close(&wim->in_fd);
903 if (filedes_valid(&wim->out_fd))
904 filedes_close(&wim->out_fd);
905 wimlib_free_decompressor(wim->decompressor);
906 xml_free_info_struct(wim->xml_info);
911 /* API function documented in wimlib.h */
913 wimlib_free(WIMStruct *wim)
918 /* The blob table and image metadata are freed immediately, but other
919 * members of the WIMStruct such as the input file descriptor are
920 * retained until no more exported resources reference the WIMStruct. */
922 free_blob_table(wim->blob_table);
923 wim->blob_table = NULL;
924 if (wim->image_metadata != NULL) {
925 deselect_current_wim_image(wim);
926 for (int i = 0; i < wim->hdr.image_count; i++)
927 put_image_metadata(wim->image_metadata[i]);
928 FREE(wim->image_metadata);
929 wim->image_metadata = NULL;
932 wim_decrement_refcnt(wim);
935 /* API function documented in wimlib.h */
937 wimlib_get_version(void)
939 return (WIMLIB_MAJOR_VERSION << 20) |
940 (WIMLIB_MINOR_VERSION << 10) |
941 WIMLIB_PATCH_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);