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/.
29 # include <langinfo.h>
36 #include "wimlib/assert.h"
37 #include "wimlib/blob_table.h"
38 #include "wimlib/dentry.h"
39 #include "wimlib/encoding.h"
40 #include "wimlib/file_io.h"
41 #include "wimlib/integrity.h"
42 #include "wimlib/metadata.h"
43 #include "wimlib/security.h"
44 #include "wimlib/wim.h"
45 #include "wimlib/xml.h"
46 #include "wimlib/win32.h"
48 /* Information about the available compression types for the WIM format. */
53 u32 default_nonsolid_chunk_size;
54 u32 default_solid_chunk_size;
55 } wim_ctype_info[] = {
56 [WIMLIB_COMPRESSION_TYPE_NONE] = {
60 .default_nonsolid_chunk_size = 0,
61 .default_solid_chunk_size = 0,
63 [WIMLIB_COMPRESSION_TYPE_XPRESS] = {
65 .min_chunk_size = 4096,
66 .max_chunk_size = 65536,
67 .default_nonsolid_chunk_size = 32768,
68 .default_solid_chunk_size = 32768,
70 [WIMLIB_COMPRESSION_TYPE_LZX] = {
72 .min_chunk_size = 32768,
73 .max_chunk_size = 2097152,
74 .default_nonsolid_chunk_size = 32768,
75 .default_solid_chunk_size = 32768,
77 [WIMLIB_COMPRESSION_TYPE_LZMS] = {
79 .min_chunk_size = 32768,
80 .max_chunk_size = 1073741824,
81 .default_nonsolid_chunk_size = 131072,
82 .default_solid_chunk_size = 67108864,
86 /* Is the specified compression type valid? */
88 wim_compression_type_valid(enum wimlib_compression_type ctype)
90 return (unsigned)ctype < ARRAY_LEN(wim_ctype_info) &&
91 wim_ctype_info[(unsigned)ctype].name != NULL;
94 /* Is the specified chunk size valid for the compression type? */
96 wim_chunk_size_valid(u32 chunk_size, enum wimlib_compression_type ctype)
98 if (!(chunk_size == 0 || is_power_of_2(chunk_size)))
101 return chunk_size >= wim_ctype_info[(unsigned)ctype].min_chunk_size &&
102 chunk_size <= wim_ctype_info[(unsigned)ctype].max_chunk_size;
105 /* Return the default chunk size to use for the specified compression type in
106 * non-solid resources. */
108 wim_default_nonsolid_chunk_size(enum wimlib_compression_type ctype)
110 return wim_ctype_info[(unsigned)ctype].default_nonsolid_chunk_size;
113 /* Return the default chunk size to use for the specified compression type in
114 * solid resources. */
116 wim_default_solid_chunk_size(enum wimlib_compression_type ctype)
118 return wim_ctype_info[(unsigned)ctype].default_solid_chunk_size;
121 /* Return the default compression type to use in solid resources. */
122 static enum wimlib_compression_type
123 wim_default_solid_compression_type(void)
125 return WIMLIB_COMPRESSION_TYPE_LZMS;
129 is_blob_in_solid_resource(struct blob_descriptor *blob, void *_ignore)
131 return blob->blob_location == BLOB_IN_WIM &&
132 (blob->rdesc->flags & WIM_RESHDR_FLAG_SOLID);
136 wim_has_solid_resources(const WIMStruct *wim)
138 return for_blob_in_table(wim->blob_table, is_blob_in_solid_resource, NULL);
144 WIMStruct *wim = CALLOC(1, sizeof(WIMStruct));
149 filedes_invalidate(&wim->in_fd);
150 filedes_invalidate(&wim->out_fd);
151 wim->out_solid_compression_type = wim_default_solid_compression_type();
152 wim->out_solid_chunk_size = wim_default_solid_chunk_size(
153 wim->out_solid_compression_type);
157 /* API function documented in wimlib.h */
159 wimlib_create_new_wim(enum wimlib_compression_type ctype, WIMStruct **wim_ret)
164 ret = wimlib_global_init(0);
169 return WIMLIB_ERR_INVALID_PARAM;
171 if (!wim_compression_type_valid(ctype))
172 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
174 wim = new_wim_struct();
176 return WIMLIB_ERR_NOMEM;
178 wim->xml_info = xml_new_info_struct();
179 wim->blob_table = new_blob_table(64);
180 if (!wim->xml_info || !wim->blob_table) {
182 return WIMLIB_ERR_NOMEM;
185 /* Fill in wim->hdr with default values */
186 wim->hdr.magic = WIM_MAGIC;
187 wim->hdr.wim_version = WIM_VERSION_DEFAULT;
189 wim->hdr.chunk_size = 0;
190 generate_guid(wim->hdr.guid);
191 wim->hdr.part_number = 1;
192 wim->hdr.total_parts = 1;
193 wim->hdr.image_count = 0;
194 wim->hdr.boot_idx = 0;
196 wim->compression_type = WIMLIB_COMPRESSION_TYPE_NONE;
197 wim->chunk_size = wim->hdr.chunk_size;
199 /* Set the output compression type */
200 wim->out_compression_type = ctype;
201 wim->out_chunk_size = wim_default_nonsolid_chunk_size(ctype);
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 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
723 wim->compression_type = WIMLIB_COMPRESSION_TYPE_NONE;
725 wim->out_compression_type = wim->compression_type;
727 /* Check and cache the chunk size. */
728 wim->chunk_size = wim->hdr.chunk_size;
729 wim->out_chunk_size = wim->chunk_size;
730 if (!wim_chunk_size_valid(wim->chunk_size, wim->compression_type)) {
731 ERROR("Invalid chunk size (%"PRIu32" bytes) "
732 "for compression type %"TS"!", wim->chunk_size,
733 wimlib_get_compression_type_string(wim->compression_type));
734 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
737 if (open_flags & WIMLIB_OPEN_FLAG_CHECK_INTEGRITY) {
738 ret = check_wim_integrity(wim);
739 if (ret == WIM_INTEGRITY_NONEXISTENT) {
740 WARNING("\"%"TS"\" does not contain integrity "
741 "information. Skipping integrity check.",
743 } else if (ret == WIM_INTEGRITY_NOT_OK) {
744 return WIMLIB_ERR_INTEGRITY;
745 } else if (ret != WIM_INTEGRITY_OK) {
750 if (wim->hdr.image_count != 0 && wim->hdr.part_number == 1) {
751 wim->image_metadata = CALLOC(wim->hdr.image_count,
752 sizeof(wim->image_metadata[0]));
753 if (!wim->image_metadata)
754 return WIMLIB_ERR_NOMEM;
757 if (open_flags & WIMLIB_OPEN_FLAG_FROM_PIPE) {
758 wim->blob_table = new_blob_table(64);
759 if (!wim->blob_table)
760 return WIMLIB_ERR_NOMEM;
762 if (wim->hdr.blob_table_reshdr.uncompressed_size == 0 &&
763 wim->hdr.xml_data_reshdr.uncompressed_size == 0)
764 return WIMLIB_ERR_WIM_IS_INCOMPLETE;
766 ret = read_wim_xml_data(wim);
770 if (xml_get_image_count(wim->xml_info) != wim->hdr.image_count) {
771 ERROR("The WIM's header is inconsistent with its XML data.\n"
772 " Please submit a bug report if you believe this "
773 "WIM file should be considered valid.");
774 return WIMLIB_ERR_IMAGE_COUNT;
777 ret = read_blob_table(wim);
785 open_wim_as_WIMStruct(const void *wim_filename_or_fd, int open_flags,
787 wimlib_progress_func_t progfunc, void *progctx)
792 ret = wimlib_global_init(0);
796 wim = new_wim_struct();
798 return WIMLIB_ERR_NOMEM;
800 wim->progfunc = progfunc;
801 wim->progctx = progctx;
803 ret = begin_read(wim, wim_filename_or_fd, open_flags);
813 /* API function documented in wimlib.h */
815 wimlib_open_wim_with_progress(const tchar *wimfile, int open_flags,
817 wimlib_progress_func_t progfunc, void *progctx)
819 if (open_flags & ~(WIMLIB_OPEN_FLAG_CHECK_INTEGRITY |
820 WIMLIB_OPEN_FLAG_ERROR_IF_SPLIT |
821 WIMLIB_OPEN_FLAG_WRITE_ACCESS))
822 return WIMLIB_ERR_INVALID_PARAM;
824 if (!wimfile || !*wimfile || !wim_ret)
825 return WIMLIB_ERR_INVALID_PARAM;
827 return open_wim_as_WIMStruct(wimfile, open_flags, wim_ret,
831 /* API function documented in wimlib.h */
833 wimlib_open_wim(const tchar *wimfile, int open_flags, WIMStruct **wim_ret)
835 return wimlib_open_wim_with_progress(wimfile, open_flags, wim_ret,
839 /* Checksum all blobs that are unhashed (other than the metadata blobs), merging
840 * them into the blob table as needed. This is a no-op unless files have been
841 * added to an image in the same WIMStruct. */
843 wim_checksum_unhashed_blobs(WIMStruct *wim)
847 if (!wim_has_metadata(wim))
849 for (int i = 0; i < wim->hdr.image_count; i++) {
850 struct blob_descriptor *blob, *tmp;
851 struct wim_image_metadata *imd = wim->image_metadata[i];
852 image_for_each_unhashed_blob_safe(blob, tmp, imd) {
853 struct blob_descriptor *new_blob;
854 ret = hash_unhashed_blob(blob, wim->blob_table, &new_blob);
857 if (new_blob != blob)
858 free_blob_descriptor(blob);
865 * can_modify_wim - Check if a given WIM is writeable. This is only the case if
866 * it meets the following three conditions:
868 * 1. Write access is allowed to the underlying file (if any) at the filesystem level.
869 * 2. The WIM is not part of a spanned set.
870 * 3. The WIM_HDR_FLAG_READONLY flag is not set in the WIM header.
872 * Return value is 0 if writable; WIMLIB_ERR_WIM_IS_READONLY otherwise.
875 can_modify_wim(WIMStruct *wim)
878 if (taccess(wim->filename, W_OK)) {
879 ERROR_WITH_ERRNO("Can't modify \"%"TS"\"", wim->filename);
880 return WIMLIB_ERR_WIM_IS_READONLY;
883 if (wim->hdr.total_parts != 1) {
884 ERROR("Cannot modify \"%"TS"\": is part of a split WIM",
886 return WIMLIB_ERR_WIM_IS_READONLY;
888 if (wim->hdr.flags & WIM_HDR_FLAG_READONLY) {
889 ERROR("Cannot modify \"%"TS"\": is marked read-only",
891 return WIMLIB_ERR_WIM_IS_READONLY;
896 /* Release a reference to a WIMStruct. If the reference count reaches 0, the
897 * WIMStruct is freed. */
899 wim_decrement_refcnt(WIMStruct *wim)
901 wimlib_assert(wim->refcnt > 0);
902 if (--wim->refcnt != 0)
904 if (filedes_valid(&wim->in_fd))
905 filedes_close(&wim->in_fd);
906 if (filedes_valid(&wim->out_fd))
907 filedes_close(&wim->out_fd);
908 wimlib_free_decompressor(wim->decompressor);
909 xml_free_info_struct(wim->xml_info);
914 /* API function documented in wimlib.h */
916 wimlib_free(WIMStruct *wim)
921 /* The blob table and image metadata are freed immediately, but other
922 * members of the WIMStruct such as the input file descriptor are
923 * retained until no more exported resources reference the WIMStruct. */
925 free_blob_table(wim->blob_table);
926 wim->blob_table = NULL;
927 if (wim->image_metadata != NULL) {
928 deselect_current_wim_image(wim);
929 for (int i = 0; i < wim->hdr.image_count; i++)
930 put_image_metadata(wim->image_metadata[i]);
931 FREE(wim->image_metadata);
932 wim->image_metadata = NULL;
935 wim_decrement_refcnt(wim);
938 /* API function documented in wimlib.h */
940 wimlib_get_version(void)
942 return (WIMLIB_MAJOR_VERSION << 20) |
943 (WIMLIB_MINOR_VERSION << 10) |
944 WIMLIB_PATCH_VERSION;
947 static bool lib_initialized = false;
948 static pthread_mutex_t lib_initialization_mutex = PTHREAD_MUTEX_INITIALIZER;
950 /* API function documented in wimlib.h */
952 wimlib_global_init(int init_flags)
959 pthread_mutex_lock(&lib_initialization_mutex);
964 #ifdef ENABLE_ERROR_MESSAGES
965 if (!wimlib_error_file)
966 wimlib_error_file = stderr;
969 ret = WIMLIB_ERR_INVALID_PARAM;
970 if (init_flags & ~(WIMLIB_INIT_FLAG_ASSUME_UTF8 |
971 WIMLIB_INIT_FLAG_DONT_ACQUIRE_PRIVILEGES |
972 WIMLIB_INIT_FLAG_STRICT_CAPTURE_PRIVILEGES |
973 WIMLIB_INIT_FLAG_STRICT_APPLY_PRIVILEGES |
974 WIMLIB_INIT_FLAG_DEFAULT_CASE_SENSITIVE |
975 WIMLIB_INIT_FLAG_DEFAULT_CASE_INSENSITIVE))
978 ret = WIMLIB_ERR_INVALID_PARAM;
979 if ((init_flags & (WIMLIB_INIT_FLAG_DEFAULT_CASE_SENSITIVE |
980 WIMLIB_INIT_FLAG_DEFAULT_CASE_INSENSITIVE))
981 == (WIMLIB_INIT_FLAG_DEFAULT_CASE_SENSITIVE |
982 WIMLIB_INIT_FLAG_DEFAULT_CASE_INSENSITIVE))
987 ret = win32_global_init(init_flags);
992 if (init_flags & WIMLIB_INIT_FLAG_DEFAULT_CASE_SENSITIVE)
993 default_ignore_case = false;
994 else if (init_flags & WIMLIB_INIT_FLAG_DEFAULT_CASE_INSENSITIVE)
995 default_ignore_case = true;
996 lib_initialized = true;
999 pthread_mutex_unlock(&lib_initialization_mutex);
1004 /* API function documented in wimlib.h */
1006 wimlib_global_cleanup(void)
1008 if (!lib_initialized)
1011 pthread_mutex_lock(&lib_initialization_mutex);
1013 if (!lib_initialized)
1016 xml_global_cleanup();
1018 win32_global_cleanup();
1021 wimlib_set_error_file(NULL);
1022 lib_initialized = false;
1025 pthread_mutex_unlock(&lib_initialization_mutex);