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,
82 [WIMLIB_COMPRESSION_TYPE_ZSTD] = {
83 .name = T("Zstandard"),
84 .min_chunk_size = 4096,
85 .max_chunk_size = 268435456,
86 .default_nonsolid_chunk_size = 131072,
87 .default_solid_chunk_size = 131072,
92 /* Is the specified compression type valid? */
94 wim_compression_type_valid(enum wimlib_compression_type ctype)
96 return (unsigned)ctype < ARRAY_LEN(wim_ctype_info) &&
97 wim_ctype_info[(unsigned)ctype].name != NULL;
100 /* Is the specified chunk size valid for the compression type? */
102 wim_chunk_size_valid(u32 chunk_size, enum wimlib_compression_type ctype)
104 if (!(chunk_size == 0 || is_power_of_2(chunk_size)))
107 return chunk_size >= wim_ctype_info[(unsigned)ctype].min_chunk_size &&
108 chunk_size <= wim_ctype_info[(unsigned)ctype].max_chunk_size;
111 /* Return the default chunk size to use for the specified compression type in
112 * non-solid resources. */
114 wim_default_nonsolid_chunk_size(enum wimlib_compression_type ctype)
116 return wim_ctype_info[(unsigned)ctype].default_nonsolid_chunk_size;
119 /* Return the default chunk size to use for the specified compression type in
120 * solid resources. */
122 wim_default_solid_chunk_size(enum wimlib_compression_type ctype)
124 return wim_ctype_info[(unsigned)ctype].default_solid_chunk_size;
127 /* Return the default compression type to use in solid resources. */
128 static enum wimlib_compression_type
129 wim_default_solid_compression_type(void)
131 return WIMLIB_COMPRESSION_TYPE_LZMS;
135 is_blob_in_solid_resource(struct blob_descriptor *blob, void *_ignore)
137 return blob->blob_location == BLOB_IN_WIM &&
138 (blob->rdesc->flags & WIM_RESHDR_FLAG_SOLID);
142 wim_has_solid_resources(const WIMStruct *wim)
144 return for_blob_in_table(wim->blob_table, is_blob_in_solid_resource, NULL);
150 WIMStruct *wim = CALLOC(1, sizeof(WIMStruct));
155 filedes_invalidate(&wim->in_fd);
156 filedes_invalidate(&wim->out_fd);
157 wim->out_solid_compression_type = wim_default_solid_compression_type();
158 wim->out_solid_chunk_size = wim_default_solid_chunk_size(
159 wim->out_solid_compression_type);
163 /* API function documented in wimlib.h */
165 wimlib_create_new_wim(enum wimlib_compression_type ctype, WIMStruct **wim_ret)
170 ret = wimlib_global_init(0);
175 return WIMLIB_ERR_INVALID_PARAM;
177 if (!wim_compression_type_valid(ctype))
178 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
180 wim = new_wim_struct();
182 return WIMLIB_ERR_NOMEM;
184 /* Fill in wim->hdr with default values */
185 wim->hdr.magic = WIM_MAGIC;
186 wim->hdr.wim_version = WIM_VERSION_DEFAULT;
187 wim->hdr.part_number = 1;
188 wim->hdr.total_parts = 1;
189 wim->compression_type = WIMLIB_COMPRESSION_TYPE_NONE;
191 /* Set the output compression type */
192 wim->out_compression_type = ctype;
193 wim->out_chunk_size = wim_default_nonsolid_chunk_size(ctype);
195 /* Allocate an empty XML info and blob table */
196 wim->xml_info = xml_new_info_struct();
197 wim->blob_table = new_blob_table(64);
198 if (!wim->xml_info || !wim->blob_table) {
200 return WIMLIB_ERR_NOMEM;
208 unload_image_metadata(struct wim_image_metadata *imd)
210 free_dentry_tree(imd->root_dentry, NULL);
211 imd->root_dentry = NULL;
212 free_wim_security_data(imd->security_data);
213 imd->security_data = NULL;
214 INIT_HLIST_HEAD(&imd->inode_list);
217 /* Release a reference to the specified image metadata. This assumes that no
218 * WIMStruct has the image selected. */
220 put_image_metadata(struct wim_image_metadata *imd)
222 struct blob_descriptor *blob, *tmp;
226 wimlib_assert(imd->refcnt > 0);
227 if (--imd->refcnt != 0)
229 wimlib_assert(imd->selected_refcnt == 0);
230 unload_image_metadata(imd);
231 list_for_each_entry_safe(blob, tmp, &imd->unhashed_blobs, unhashed_list)
232 free_blob_descriptor(blob);
233 free_blob_descriptor(imd->metadata_blob);
237 /* Appends the specified image metadata structure to the array of image metadata
238 * for a WIM, and increments the image count. */
240 append_image_metadata(WIMStruct *wim, struct wim_image_metadata *imd)
242 struct wim_image_metadata **imd_array;
244 if (!wim_has_metadata(wim))
245 return WIMLIB_ERR_METADATA_NOT_FOUND;
247 if (wim->hdr.image_count >= MAX_IMAGES)
248 return WIMLIB_ERR_IMAGE_COUNT;
250 imd_array = REALLOC(wim->image_metadata,
251 sizeof(wim->image_metadata[0]) * (wim->hdr.image_count + 1));
254 return WIMLIB_ERR_NOMEM;
255 wim->image_metadata = imd_array;
256 imd_array[wim->hdr.image_count++] = imd;
260 static struct wim_image_metadata *
261 new_image_metadata(struct blob_descriptor *metadata_blob,
262 struct wim_security_data *security_data)
264 struct wim_image_metadata *imd;
266 imd = CALLOC(1, sizeof(*imd));
270 metadata_blob->is_metadata = 1;
272 imd->selected_refcnt = 0;
273 imd->root_dentry = NULL;
274 imd->security_data = security_data;
275 imd->metadata_blob = metadata_blob;
276 INIT_HLIST_HEAD(&imd->inode_list);
277 INIT_LIST_HEAD(&imd->unhashed_blobs);
278 imd->stats_outdated = false;
282 /* Create an image metadata structure for a new empty image. */
283 struct wim_image_metadata *
284 new_empty_image_metadata(void)
286 struct blob_descriptor *metadata_blob;
287 struct wim_security_data *security_data;
288 struct wim_image_metadata *imd;
290 metadata_blob = new_blob_descriptor();
291 security_data = new_wim_security_data();
292 if (metadata_blob && security_data) {
293 metadata_blob->refcnt = 1;
294 imd = new_image_metadata(metadata_blob, security_data);
298 free_blob_descriptor(metadata_blob);
303 /* Create an image metadata structure that refers to the specified metadata
304 * resource and is initially not loaded. */
305 struct wim_image_metadata *
306 new_unloaded_image_metadata(struct blob_descriptor *metadata_blob)
308 wimlib_assert(metadata_blob->blob_location == BLOB_IN_WIM);
309 return new_image_metadata(metadata_blob, NULL);
313 * Load the metadata for the specified WIM image into memory and set it
314 * as the WIMStruct's currently selected image.
317 * The WIMStruct for the WIM.
319 * The 1-based index of the image in the WIM to select.
321 * On success, 0 will be returned, wim->current_image will be set to
322 * @image, and wim_get_current_image_metadata() can be used to retrieve
323 * metadata information for the image.
325 * On failure, WIMLIB_ERR_INVALID_IMAGE, WIMLIB_ERR_METADATA_NOT_FOUND,
326 * or another error code will be returned.
329 select_wim_image(WIMStruct *wim, int image)
331 struct wim_image_metadata *imd;
334 if (image == WIMLIB_NO_IMAGE)
335 return WIMLIB_ERR_INVALID_IMAGE;
337 if (image == wim->current_image)
340 if (image < 1 || image > wim->hdr.image_count)
341 return WIMLIB_ERR_INVALID_IMAGE;
343 if (!wim_has_metadata(wim))
344 return WIMLIB_ERR_METADATA_NOT_FOUND;
346 deselect_current_wim_image(wim);
348 imd = wim->image_metadata[image - 1];
349 if (!is_image_loaded(imd)) {
350 ret = read_metadata_resource(imd);
354 wim->current_image = image;
355 imd->selected_refcnt++;
360 * Deselect the WIMStruct's currently selected image, if any. To reduce memory
361 * usage, possibly unload the newly deselected image's metadata from memory.
364 deselect_current_wim_image(WIMStruct *wim)
366 struct wim_image_metadata *imd;
368 if (wim->current_image == WIMLIB_NO_IMAGE)
370 imd = wim_get_current_image_metadata(wim);
371 wimlib_assert(imd->selected_refcnt > 0);
372 imd->selected_refcnt--;
373 wim->current_image = WIMLIB_NO_IMAGE;
375 if (can_unload_image(imd)) {
376 wimlib_assert(list_empty(&imd->unhashed_blobs));
377 unload_image_metadata(imd);
382 * Calls a function on images in the WIM. If @image is WIMLIB_ALL_IMAGES,
383 * @visitor is called on the WIM once for each image, with each image selected
384 * as the current image in turn. If @image is a certain image, @visitor is
385 * called on the WIM only once, with that image selected.
388 for_image(WIMStruct *wim, int image, int (*visitor)(WIMStruct *))
395 if (image == WIMLIB_ALL_IMAGES) {
397 end = wim->hdr.image_count;
398 } else if (image >= 1 && image <= wim->hdr.image_count) {
402 return WIMLIB_ERR_INVALID_IMAGE;
404 for (i = start; i <= end; i++) {
405 ret = select_wim_image(wim, i);
415 /* API function documented in wimlib.h */
417 wimlib_resolve_image(WIMStruct *wim, const tchar *image_name_or_num)
423 if (!image_name_or_num || !*image_name_or_num)
424 return WIMLIB_NO_IMAGE;
426 if (!tstrcasecmp(image_name_or_num, T("all"))
427 || !tstrcasecmp(image_name_or_num, T("*")))
428 return WIMLIB_ALL_IMAGES;
429 image = tstrtol(image_name_or_num, &p, 10);
430 if (p != image_name_or_num && *p == T('\0') && image > 0) {
431 if (image > wim->hdr.image_count)
432 return WIMLIB_NO_IMAGE;
435 for (i = 1; i <= wim->hdr.image_count; i++) {
436 if (!tstrcmp(image_name_or_num,
437 wimlib_get_image_name(wim, i)))
440 return WIMLIB_NO_IMAGE;
444 /* API function documented in wimlib.h */
446 wimlib_print_available_images(const WIMStruct *wim, int image)
452 if (image == WIMLIB_ALL_IMAGES) {
453 n = tprintf(T("Available Images:\n"));
455 last = wim->hdr.image_count;
456 } else if (image >= 1 && image <= wim->hdr.image_count) {
457 n = tprintf(T("Information for Image %d\n"), image);
461 tprintf(T("wimlib_print_available_images(): Invalid image %d"),
465 for (i = 0; i < n - 1; i++)
468 for (i = first; i <= last; i++)
469 xml_print_image_info(wim->xml_info, i);
472 /* API function documented in wimlib.h */
474 wimlib_get_wim_info(WIMStruct *wim, struct wimlib_wim_info *info)
476 memset(info, 0, sizeof(struct wimlib_wim_info));
477 copy_guid(info->guid, wim->hdr.guid);
478 info->image_count = wim->hdr.image_count;
479 info->boot_index = wim->hdr.boot_idx;
480 info->wim_version = wim->hdr.wim_version;
481 info->chunk_size = wim->chunk_size;
482 info->part_number = wim->hdr.part_number;
483 info->total_parts = wim->hdr.total_parts;
484 info->compression_type = wim->compression_type;
485 info->total_bytes = xml_get_total_bytes(wim->xml_info);
486 info->has_integrity_table = wim_has_integrity_table(wim);
487 info->opened_from_file = (wim->filename != NULL);
488 info->is_readonly = (wim->hdr.flags & WIM_HDR_FLAG_READONLY) ||
489 (wim->hdr.total_parts != 1) ||
490 (wim->filename && taccess(wim->filename, W_OK));
491 info->has_rpfix = (wim->hdr.flags & WIM_HDR_FLAG_RP_FIX) != 0;
492 info->is_marked_readonly = (wim->hdr.flags & WIM_HDR_FLAG_READONLY) != 0;
493 info->write_in_progress = (wim->hdr.flags & WIM_HDR_FLAG_WRITE_IN_PROGRESS) != 0;
494 info->metadata_only = (wim->hdr.flags & WIM_HDR_FLAG_METADATA_ONLY) != 0;
495 info->resource_only = (wim->hdr.flags & WIM_HDR_FLAG_RESOURCE_ONLY) != 0;
496 info->spanned = (wim->hdr.flags & WIM_HDR_FLAG_SPANNED) != 0;
497 info->pipable = wim_is_pipable(wim);
501 /* API function documented in wimlib.h */
503 wimlib_set_wim_info(WIMStruct *wim, const struct wimlib_wim_info *info, int which)
505 if (which & ~(WIMLIB_CHANGE_READONLY_FLAG |
507 WIMLIB_CHANGE_BOOT_INDEX |
508 WIMLIB_CHANGE_RPFIX_FLAG))
509 return WIMLIB_ERR_INVALID_PARAM;
511 if ((which & WIMLIB_CHANGE_BOOT_INDEX) &&
512 info->boot_index > wim->hdr.image_count)
513 return WIMLIB_ERR_INVALID_IMAGE;
515 if (which & WIMLIB_CHANGE_READONLY_FLAG) {
516 if (info->is_marked_readonly)
517 wim->hdr.flags |= WIM_HDR_FLAG_READONLY;
519 wim->hdr.flags &= ~WIM_HDR_FLAG_READONLY;
522 if (which & WIMLIB_CHANGE_GUID)
523 copy_guid(wim->hdr.guid, info->guid);
525 if (which & WIMLIB_CHANGE_BOOT_INDEX)
526 wim->hdr.boot_idx = info->boot_index;
528 if (which & WIMLIB_CHANGE_RPFIX_FLAG) {
530 wim->hdr.flags |= WIM_HDR_FLAG_RP_FIX;
532 wim->hdr.flags &= ~WIM_HDR_FLAG_RP_FIX;
537 /* API function documented in wimlib.h */
539 wimlib_set_output_compression_type(WIMStruct *wim,
540 enum wimlib_compression_type ctype)
542 if (!wim_compression_type_valid(ctype))
543 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
545 wim->out_compression_type = ctype;
547 /* Reset the chunk size if it's no longer valid. */
548 if (!wim_chunk_size_valid(wim->out_chunk_size, ctype))
549 wim->out_chunk_size = wim_default_nonsolid_chunk_size(ctype);
553 /* API function documented in wimlib.h */
555 wimlib_set_output_pack_compression_type(WIMStruct *wim,
556 enum wimlib_compression_type ctype)
558 if (!wim_compression_type_valid(ctype))
559 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
561 /* Solid resources can't be uncompressed. */
562 if (ctype == WIMLIB_COMPRESSION_TYPE_NONE)
563 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
565 wim->out_solid_compression_type = ctype;
567 /* Reset the chunk size if it's no longer valid. */
568 if (!wim_chunk_size_valid(wim->out_solid_chunk_size, ctype))
569 wim->out_solid_chunk_size = wim_default_solid_chunk_size(ctype);
573 /* API function documented in wimlib.h */
575 wimlib_set_output_chunk_size(WIMStruct *wim, u32 chunk_size)
577 if (chunk_size == 0) {
578 wim->out_chunk_size =
579 wim_default_nonsolid_chunk_size(wim->out_compression_type);
583 if (!wim_chunk_size_valid(chunk_size, wim->out_compression_type))
584 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
586 wim->out_chunk_size = chunk_size;
590 /* API function documented in wimlib.h */
592 wimlib_set_output_pack_chunk_size(WIMStruct *wim, u32 chunk_size)
594 if (chunk_size == 0) {
595 wim->out_solid_chunk_size =
596 wim_default_solid_chunk_size(wim->out_solid_compression_type);
600 if (!wim_chunk_size_valid(chunk_size, wim->out_solid_compression_type))
601 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
603 wim->out_solid_chunk_size = chunk_size;
607 /* API function documented in wimlib.h */
608 WIMLIBAPI const tchar *
609 wimlib_get_compression_type_string(enum wimlib_compression_type ctype)
611 if (!wim_compression_type_valid(ctype))
614 return wim_ctype_info[(unsigned)ctype].name;
618 wimlib_register_progress_function(WIMStruct *wim,
619 wimlib_progress_func_t progfunc,
622 wim->progfunc = progfunc;
623 wim->progctx = progctx;
627 open_wim_file(const tchar *filename, struct filedes *fd_ret)
631 raw_fd = topen(filename, O_RDONLY | O_BINARY);
633 ERROR_WITH_ERRNO("Can't open \"%"TS"\" read-only", filename);
634 return WIMLIB_ERR_OPEN;
636 filedes_init(fd_ret, raw_fd);
641 * Begins the reading of a WIM file; opens the file and reads its header and
642 * blob table, and optionally checks the integrity.
645 begin_read(WIMStruct *wim, const void *wim_filename_or_fd, int open_flags)
648 const tchar *wimfile;
650 if (open_flags & WIMLIB_OPEN_FLAG_FROM_PIPE) {
652 filedes_init(&wim->in_fd, *(const int*)wim_filename_or_fd);
653 wim->in_fd.is_pipe = 1;
655 wimfile = wim_filename_or_fd;
656 ret = open_wim_file(wimfile, &wim->in_fd);
660 /* The absolute path to the WIM is requested so that
661 * wimlib_overwrite() still works even if the process changes
662 * its working directory. This actually happens if a WIM is
663 * mounted read-write, since the FUSE thread changes directory
664 * to "/", and it needs to be able to find the WIM file again.
666 * This will break if the full path to the WIM changes in the
667 * intervening time...
669 * Warning: in Windows native builds, realpath() calls the
670 * replacement function in win32_replacements.c.
672 wim->filename = realpath(wimfile, NULL);
673 if (!wim->filename) {
674 ERROR_WITH_ERRNO("Failed to get full path to file "
675 "\"%"TS"\"", wimfile);
677 return WIMLIB_ERR_NOMEM;
679 return WIMLIB_ERR_NO_FILENAME;
683 ret = read_wim_header(wim, &wim->hdr);
687 if (wim->hdr.flags & WIM_HDR_FLAG_WRITE_IN_PROGRESS) {
688 WARNING("The WIM_HDR_FLAG_WRITE_IN_PROGRESS flag is set in the header of\n"
689 " \"%"TS"\". It may be being changed by another process,\n"
690 " or a process may have crashed while writing the WIM.",
694 if (open_flags & WIMLIB_OPEN_FLAG_WRITE_ACCESS) {
695 ret = can_modify_wim(wim);
700 if ((open_flags & WIMLIB_OPEN_FLAG_ERROR_IF_SPLIT) &&
701 (wim->hdr.total_parts != 1))
702 return WIMLIB_ERR_IS_SPLIT_WIM;
704 /* If the boot index is invalid, print a warning and set it to 0 */
705 if (wim->hdr.boot_idx > wim->hdr.image_count) {
706 WARNING("Ignoring invalid boot index.");
707 wim->hdr.boot_idx = 0;
710 /* Check and cache the compression type */
711 if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESSION) {
712 if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESS_LZX) {
713 wim->compression_type = WIMLIB_COMPRESSION_TYPE_LZX;
714 } else if (wim->hdr.flags & (WIM_HDR_FLAG_COMPRESS_XPRESS |
715 WIM_HDR_FLAG_COMPRESS_XPRESS_2)) {
716 wim->compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
717 } else if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESS_LZMS) {
718 wim->compression_type = WIMLIB_COMPRESSION_TYPE_LZMS;
720 } else if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESS_ZSTD) {
721 wim->compression_type = WIMLIB_COMPRESSION_TYPE_ZSTD;
724 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
727 wim->compression_type = WIMLIB_COMPRESSION_TYPE_NONE;
729 wim->out_compression_type = wim->compression_type;
731 /* Check and cache the chunk size. */
732 wim->chunk_size = wim->hdr.chunk_size;
733 wim->out_chunk_size = wim->chunk_size;
734 if (!wim_chunk_size_valid(wim->chunk_size, wim->compression_type)) {
735 ERROR("Invalid chunk size (%"PRIu32" bytes) "
736 "for compression type %"TS"!", wim->chunk_size,
737 wimlib_get_compression_type_string(wim->compression_type));
738 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
741 if (open_flags & WIMLIB_OPEN_FLAG_CHECK_INTEGRITY) {
742 ret = check_wim_integrity(wim);
743 if (ret == WIM_INTEGRITY_NONEXISTENT) {
744 WARNING("\"%"TS"\" does not contain integrity "
745 "information. Skipping integrity check.",
747 } else if (ret == WIM_INTEGRITY_NOT_OK) {
748 return WIMLIB_ERR_INTEGRITY;
749 } else if (ret != WIM_INTEGRITY_OK) {
754 if (wim->hdr.image_count != 0 && wim->hdr.part_number == 1) {
755 wim->image_metadata = CALLOC(wim->hdr.image_count,
756 sizeof(wim->image_metadata[0]));
757 if (!wim->image_metadata)
758 return WIMLIB_ERR_NOMEM;
761 if (open_flags & WIMLIB_OPEN_FLAG_FROM_PIPE) {
762 wim->blob_table = new_blob_table(64);
763 if (!wim->blob_table)
764 return WIMLIB_ERR_NOMEM;
766 if (wim->hdr.blob_table_reshdr.uncompressed_size == 0 &&
767 wim->hdr.xml_data_reshdr.uncompressed_size == 0)
768 return WIMLIB_ERR_WIM_IS_INCOMPLETE;
770 ret = read_wim_xml_data(wim);
774 if (xml_get_image_count(wim->xml_info) != wim->hdr.image_count) {
775 ERROR("The WIM's header is inconsistent with its XML data.\n"
776 " Please submit a bug report if you believe this "
777 "WIM file should be considered valid.");
778 return WIMLIB_ERR_IMAGE_COUNT;
781 ret = read_blob_table(wim);
789 open_wim_as_WIMStruct(const void *wim_filename_or_fd, int open_flags,
791 wimlib_progress_func_t progfunc, void *progctx)
796 ret = wimlib_global_init(0);
800 wim = new_wim_struct();
802 return WIMLIB_ERR_NOMEM;
804 wim->progfunc = progfunc;
805 wim->progctx = progctx;
807 ret = begin_read(wim, wim_filename_or_fd, open_flags);
817 /* API function documented in wimlib.h */
819 wimlib_open_wim_with_progress(const tchar *wimfile, int open_flags,
821 wimlib_progress_func_t progfunc, void *progctx)
823 if (open_flags & ~(WIMLIB_OPEN_FLAG_CHECK_INTEGRITY |
824 WIMLIB_OPEN_FLAG_ERROR_IF_SPLIT |
825 WIMLIB_OPEN_FLAG_WRITE_ACCESS))
826 return WIMLIB_ERR_INVALID_PARAM;
828 if (!wimfile || !*wimfile || !wim_ret)
829 return WIMLIB_ERR_INVALID_PARAM;
831 return open_wim_as_WIMStruct(wimfile, open_flags, wim_ret,
835 /* API function documented in wimlib.h */
837 wimlib_open_wim(const tchar *wimfile, int open_flags, WIMStruct **wim_ret)
839 return wimlib_open_wim_with_progress(wimfile, open_flags, wim_ret,
843 /* Checksum all blobs that are unhashed (other than the metadata blobs), merging
844 * them into the blob table as needed. This is a no-op unless files have been
845 * added to an image in the same WIMStruct. */
847 wim_checksum_unhashed_blobs(WIMStruct *wim)
851 if (!wim_has_metadata(wim))
853 for (int i = 0; i < wim->hdr.image_count; i++) {
854 struct blob_descriptor *blob, *tmp;
855 struct wim_image_metadata *imd = wim->image_metadata[i];
856 image_for_each_unhashed_blob_safe(blob, tmp, imd) {
857 struct blob_descriptor *new_blob;
858 ret = hash_unhashed_blob(blob, wim->blob_table, &new_blob);
861 if (new_blob != blob)
862 free_blob_descriptor(blob);
869 * can_modify_wim - Check if a given WIM is writeable. This is only the case if
870 * it meets the following three conditions:
872 * 1. Write access is allowed to the underlying file (if any) at the filesystem level.
873 * 2. The WIM is not part of a spanned set.
874 * 3. The WIM_HDR_FLAG_READONLY flag is not set in the WIM header.
876 * Return value is 0 if writable; WIMLIB_ERR_WIM_IS_READONLY otherwise.
879 can_modify_wim(WIMStruct *wim)
882 if (taccess(wim->filename, W_OK)) {
883 ERROR_WITH_ERRNO("Can't modify \"%"TS"\"", wim->filename);
884 return WIMLIB_ERR_WIM_IS_READONLY;
887 if (wim->hdr.total_parts != 1) {
888 ERROR("Cannot modify \"%"TS"\": is part of a split WIM",
890 return WIMLIB_ERR_WIM_IS_READONLY;
892 if (wim->hdr.flags & WIM_HDR_FLAG_READONLY) {
893 ERROR("Cannot modify \"%"TS"\": is marked read-only",
895 return WIMLIB_ERR_WIM_IS_READONLY;
900 /* Release a reference to a WIMStruct. If the reference count reaches 0, the
901 * WIMStruct is freed. */
903 wim_decrement_refcnt(WIMStruct *wim)
905 wimlib_assert(wim->refcnt > 0);
906 if (--wim->refcnt != 0)
908 if (filedes_valid(&wim->in_fd))
909 filedes_close(&wim->in_fd);
910 if (filedes_valid(&wim->out_fd))
911 filedes_close(&wim->out_fd);
912 wimlib_free_decompressor(wim->decompressor);
913 xml_free_info_struct(wim->xml_info);
918 /* API function documented in wimlib.h */
920 wimlib_free(WIMStruct *wim)
925 /* The blob table and image metadata are freed immediately, but other
926 * members of the WIMStruct such as the input file descriptor are
927 * retained until no more exported resources reference the WIMStruct. */
929 free_blob_table(wim->blob_table);
930 wim->blob_table = NULL;
931 if (wim->image_metadata != NULL) {
932 deselect_current_wim_image(wim);
933 for (int i = 0; i < wim->hdr.image_count; i++)
934 put_image_metadata(wim->image_metadata[i]);
935 FREE(wim->image_metadata);
936 wim->image_metadata = NULL;
939 wim_decrement_refcnt(wim);
942 /* API function documented in wimlib.h */
944 wimlib_get_version(void)
946 return (WIMLIB_MAJOR_VERSION << 20) |
947 (WIMLIB_MINOR_VERSION << 10) |
948 WIMLIB_PATCH_VERSION;
951 WIMLIBAPI const tchar *
952 wimlib_get_version_string(void)
954 return T(PACKAGE_VERSION);
957 static bool lib_initialized = false;
958 static pthread_mutex_t lib_initialization_mutex = PTHREAD_MUTEX_INITIALIZER;
960 /* API function documented in wimlib.h */
962 wimlib_global_init(int init_flags)
969 pthread_mutex_lock(&lib_initialization_mutex);
974 #ifdef ENABLE_ERROR_MESSAGES
975 if (!wimlib_error_file)
976 wimlib_error_file = stderr;
979 ret = WIMLIB_ERR_INVALID_PARAM;
980 if (init_flags & ~(WIMLIB_INIT_FLAG_ASSUME_UTF8 |
981 WIMLIB_INIT_FLAG_DONT_ACQUIRE_PRIVILEGES |
982 WIMLIB_INIT_FLAG_STRICT_CAPTURE_PRIVILEGES |
983 WIMLIB_INIT_FLAG_STRICT_APPLY_PRIVILEGES |
984 WIMLIB_INIT_FLAG_DEFAULT_CASE_SENSITIVE |
985 WIMLIB_INIT_FLAG_DEFAULT_CASE_INSENSITIVE))
988 ret = WIMLIB_ERR_INVALID_PARAM;
989 if ((init_flags & (WIMLIB_INIT_FLAG_DEFAULT_CASE_SENSITIVE |
990 WIMLIB_INIT_FLAG_DEFAULT_CASE_INSENSITIVE))
991 == (WIMLIB_INIT_FLAG_DEFAULT_CASE_SENSITIVE |
992 WIMLIB_INIT_FLAG_DEFAULT_CASE_INSENSITIVE))
997 ret = win32_global_init(init_flags);
1002 if (init_flags & WIMLIB_INIT_FLAG_DEFAULT_CASE_SENSITIVE)
1003 default_ignore_case = false;
1004 else if (init_flags & WIMLIB_INIT_FLAG_DEFAULT_CASE_INSENSITIVE)
1005 default_ignore_case = true;
1006 lib_initialized = true;
1009 pthread_mutex_unlock(&lib_initialization_mutex);
1014 /* API function documented in wimlib.h */
1016 wimlib_global_cleanup(void)
1018 if (!lib_initialized)
1021 pthread_mutex_lock(&lib_initialization_mutex);
1023 if (!lib_initialized)
1026 xml_global_cleanup();
1028 win32_global_cleanup();
1031 wimlib_set_error_file(NULL);
1032 lib_initialized = false;
1035 pthread_mutex_unlock(&lib_initialization_mutex);