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/ntfs_3g.h" /* for libntfs3g_global_init() */
44 #include "wimlib/security.h"
45 #include "wimlib/wim.h"
46 #include "wimlib/xml.h"
47 #include "wimlib/win32.h"
49 /* Information about the available compression types for the WIM format. */
54 u32 default_nonsolid_chunk_size;
55 u32 default_solid_chunk_size;
56 } wim_ctype_info[] = {
57 [WIMLIB_COMPRESSION_TYPE_NONE] = {
61 .default_nonsolid_chunk_size = 0,
62 .default_solid_chunk_size = 0,
64 [WIMLIB_COMPRESSION_TYPE_XPRESS] = {
66 .min_chunk_size = 4096,
67 .max_chunk_size = 65536,
68 .default_nonsolid_chunk_size = 32768,
69 .default_solid_chunk_size = 32768,
71 [WIMLIB_COMPRESSION_TYPE_LZX] = {
73 .min_chunk_size = 32768,
74 .max_chunk_size = 2097152,
75 .default_nonsolid_chunk_size = 32768,
76 .default_solid_chunk_size = 32768,
78 [WIMLIB_COMPRESSION_TYPE_LZMS] = {
80 .min_chunk_size = 32768,
81 .max_chunk_size = 1073741824,
82 .default_nonsolid_chunk_size = 131072,
83 .default_solid_chunk_size = 67108864,
87 /* Is the specified compression type valid? */
89 wim_compression_type_valid(enum wimlib_compression_type ctype)
91 return (unsigned)ctype < ARRAY_LEN(wim_ctype_info) &&
92 wim_ctype_info[(unsigned)ctype].name != NULL;
95 /* Is the specified chunk size valid for the compression type? */
97 wim_chunk_size_valid(u32 chunk_size, enum wimlib_compression_type ctype)
99 if (!(chunk_size == 0 || is_power_of_2(chunk_size)))
102 return chunk_size >= wim_ctype_info[(unsigned)ctype].min_chunk_size &&
103 chunk_size <= wim_ctype_info[(unsigned)ctype].max_chunk_size;
106 /* Return the default chunk size to use for the specified compression type in
107 * non-solid resources. */
109 wim_default_nonsolid_chunk_size(enum wimlib_compression_type ctype)
111 return wim_ctype_info[(unsigned)ctype].default_nonsolid_chunk_size;
114 /* Return the default chunk size to use for the specified compression type in
115 * solid resources. */
117 wim_default_solid_chunk_size(enum wimlib_compression_type ctype)
119 return wim_ctype_info[(unsigned)ctype].default_solid_chunk_size;
122 /* Return the default compression type to use in solid resources. */
123 static enum wimlib_compression_type
124 wim_default_solid_compression_type(void)
126 return WIMLIB_COMPRESSION_TYPE_LZMS;
130 is_blob_in_solid_resource(struct blob_descriptor *blob, void *_ignore)
132 return blob->blob_location == BLOB_IN_WIM &&
133 (blob->rdesc->flags & WIM_RESHDR_FLAG_SOLID);
137 wim_has_solid_resources(const WIMStruct *wim)
139 return for_blob_in_table(wim->blob_table, is_blob_in_solid_resource, NULL);
145 WIMStruct *wim = CALLOC(1, sizeof(WIMStruct));
150 filedes_invalidate(&wim->in_fd);
151 filedes_invalidate(&wim->out_fd);
152 wim->out_solid_compression_type = wim_default_solid_compression_type();
153 wim->out_solid_chunk_size = wim_default_solid_chunk_size(
154 wim->out_solid_compression_type);
158 /* API function documented in wimlib.h */
160 wimlib_create_new_wim(enum wimlib_compression_type ctype, WIMStruct **wim_ret)
165 ret = wimlib_global_init(WIMLIB_INIT_FLAG_ASSUME_UTF8);
170 return WIMLIB_ERR_INVALID_PARAM;
172 if (!wim_compression_type_valid(ctype))
173 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
175 wim = new_wim_struct();
177 return WIMLIB_ERR_NOMEM;
179 wim->xml_info = xml_new_info_struct();
180 wim->blob_table = new_blob_table(64);
181 if (!wim->xml_info || !wim->blob_table) {
183 return WIMLIB_ERR_NOMEM;
186 /* Fill in wim->hdr with default values */
187 wim->hdr.magic = WIM_MAGIC;
188 wim->hdr.wim_version = WIM_VERSION_DEFAULT;
190 wim->hdr.chunk_size = 0;
191 generate_guid(wim->hdr.guid);
192 wim->hdr.part_number = 1;
193 wim->hdr.total_parts = 1;
194 wim->hdr.image_count = 0;
195 wim->hdr.boot_idx = 0;
197 wim->compression_type = WIMLIB_COMPRESSION_TYPE_NONE;
198 wim->chunk_size = wim->hdr.chunk_size;
200 /* Set the output compression type */
201 wim->out_compression_type = ctype;
202 wim->out_chunk_size = wim_default_nonsolid_chunk_size(ctype);
209 unload_image_metadata(struct wim_image_metadata *imd)
211 free_dentry_tree(imd->root_dentry, NULL);
212 imd->root_dentry = NULL;
213 free_wim_security_data(imd->security_data);
214 imd->security_data = NULL;
215 INIT_HLIST_HEAD(&imd->inode_list);
218 /* Release a reference to the specified image metadata. This assumes that no
219 * WIMStruct has the image selected. */
221 put_image_metadata(struct wim_image_metadata *imd)
223 struct blob_descriptor *blob, *tmp;
227 wimlib_assert(imd->refcnt > 0);
228 if (--imd->refcnt != 0)
230 wimlib_assert(imd->selected_refcnt == 0);
231 unload_image_metadata(imd);
232 list_for_each_entry_safe(blob, tmp, &imd->unhashed_blobs, unhashed_list)
233 free_blob_descriptor(blob);
234 free_blob_descriptor(imd->metadata_blob);
238 /* Appends the specified image metadata structure to the array of image metadata
239 * for a WIM, and increments the image count. */
241 append_image_metadata(WIMStruct *wim, struct wim_image_metadata *imd)
243 struct wim_image_metadata **imd_array;
245 if (!wim_has_metadata(wim))
246 return WIMLIB_ERR_METADATA_NOT_FOUND;
248 if (wim->hdr.image_count >= MAX_IMAGES)
249 return WIMLIB_ERR_IMAGE_COUNT;
251 imd_array = REALLOC(wim->image_metadata,
252 sizeof(wim->image_metadata[0]) * (wim->hdr.image_count + 1));
255 return WIMLIB_ERR_NOMEM;
256 wim->image_metadata = imd_array;
257 imd_array[wim->hdr.image_count++] = imd;
261 static struct wim_image_metadata *
262 new_image_metadata(struct blob_descriptor *metadata_blob,
263 struct wim_security_data *security_data)
265 struct wim_image_metadata *imd;
267 imd = CALLOC(1, sizeof(*imd));
271 metadata_blob->is_metadata = 1;
273 imd->selected_refcnt = 0;
274 imd->root_dentry = NULL;
275 imd->security_data = security_data;
276 imd->metadata_blob = metadata_blob;
277 INIT_HLIST_HEAD(&imd->inode_list);
278 INIT_LIST_HEAD(&imd->unhashed_blobs);
279 imd->stats_outdated = false;
283 /* Create an image metadata structure for a new empty image. */
284 struct wim_image_metadata *
285 new_empty_image_metadata(void)
287 struct blob_descriptor *metadata_blob;
288 struct wim_security_data *security_data;
289 struct wim_image_metadata *imd;
291 metadata_blob = new_blob_descriptor();
292 security_data = new_wim_security_data();
293 if (metadata_blob && security_data) {
294 metadata_blob->refcnt = 1;
295 imd = new_image_metadata(metadata_blob, security_data);
299 free_blob_descriptor(metadata_blob);
304 /* Create an image metadata structure that refers to the specified metadata
305 * resource and is initially not loaded. */
306 struct wim_image_metadata *
307 new_unloaded_image_metadata(struct blob_descriptor *metadata_blob)
309 wimlib_assert(metadata_blob->blob_location == BLOB_IN_WIM);
310 return new_image_metadata(metadata_blob, NULL);
314 * Load the metadata for the specified WIM image into memory and set it
315 * as the WIMStruct's currently selected image.
318 * The WIMStruct for the WIM.
320 * The 1-based index of the image in the WIM to select.
322 * On success, 0 will be returned, wim->current_image will be set to
323 * @image, and wim_get_current_image_metadata() can be used to retrieve
324 * metadata information for the image.
326 * On failure, WIMLIB_ERR_INVALID_IMAGE, WIMLIB_ERR_METADATA_NOT_FOUND,
327 * or another error code will be returned.
330 select_wim_image(WIMStruct *wim, int image)
332 struct wim_image_metadata *imd;
335 if (image == WIMLIB_NO_IMAGE)
336 return WIMLIB_ERR_INVALID_IMAGE;
338 if (image == wim->current_image)
341 if (image < 1 || image > wim->hdr.image_count)
342 return WIMLIB_ERR_INVALID_IMAGE;
344 if (!wim_has_metadata(wim))
345 return WIMLIB_ERR_METADATA_NOT_FOUND;
347 deselect_current_wim_image(wim);
349 imd = wim->image_metadata[image - 1];
350 if (!is_image_loaded(imd)) {
351 ret = read_metadata_resource(imd);
355 wim->current_image = image;
356 imd->selected_refcnt++;
361 * Deselect the WIMStruct's currently selected image, if any. To reduce memory
362 * usage, possibly unload the newly deselected image's metadata from memory.
365 deselect_current_wim_image(WIMStruct *wim)
367 struct wim_image_metadata *imd;
369 if (wim->current_image == WIMLIB_NO_IMAGE)
371 imd = wim_get_current_image_metadata(wim);
372 wimlib_assert(imd->selected_refcnt > 0);
373 imd->selected_refcnt--;
374 wim->current_image = WIMLIB_NO_IMAGE;
376 if (can_unload_image(imd)) {
377 wimlib_assert(list_empty(&imd->unhashed_blobs));
378 unload_image_metadata(imd);
383 * Calls a function on images in the WIM. If @image is WIMLIB_ALL_IMAGES,
384 * @visitor is called on the WIM once for each image, with each image selected
385 * as the current image in turn. If @image is a certain image, @visitor is
386 * called on the WIM only once, with that image selected.
389 for_image(WIMStruct *wim, int image, int (*visitor)(WIMStruct *))
396 if (image == WIMLIB_ALL_IMAGES) {
398 end = wim->hdr.image_count;
399 } else if (image >= 1 && image <= wim->hdr.image_count) {
403 return WIMLIB_ERR_INVALID_IMAGE;
405 for (i = start; i <= end; i++) {
406 ret = select_wim_image(wim, i);
416 /* API function documented in wimlib.h */
418 wimlib_resolve_image(WIMStruct *wim, const tchar *image_name_or_num)
424 if (!image_name_or_num || !*image_name_or_num)
425 return WIMLIB_NO_IMAGE;
427 if (!tstrcasecmp(image_name_or_num, T("all"))
428 || !tstrcasecmp(image_name_or_num, T("*")))
429 return WIMLIB_ALL_IMAGES;
430 image = tstrtol(image_name_or_num, &p, 10);
431 if (p != image_name_or_num && *p == T('\0') && image > 0) {
432 if (image > wim->hdr.image_count)
433 return WIMLIB_NO_IMAGE;
436 for (i = 1; i <= wim->hdr.image_count; i++) {
437 if (!tstrcmp(image_name_or_num,
438 wimlib_get_image_name(wim, i)))
441 return WIMLIB_NO_IMAGE;
445 /* API function documented in wimlib.h */
447 wimlib_print_available_images(const WIMStruct *wim, int image)
453 if (image == WIMLIB_ALL_IMAGES) {
454 n = tprintf(T("Available Images:\n"));
456 last = wim->hdr.image_count;
457 } else if (image >= 1 && image <= wim->hdr.image_count) {
458 n = tprintf(T("Information for Image %d\n"), image);
462 tprintf(T("wimlib_print_available_images(): Invalid image %d"),
466 for (i = 0; i < n - 1; i++)
469 for (i = first; i <= last; i++)
470 xml_print_image_info(wim->xml_info, i);
473 /* API function documented in wimlib.h */
475 wimlib_get_wim_info(WIMStruct *wim, struct wimlib_wim_info *info)
477 memset(info, 0, sizeof(struct wimlib_wim_info));
478 copy_guid(info->guid, wim->hdr.guid);
479 info->image_count = wim->hdr.image_count;
480 info->boot_index = wim->hdr.boot_idx;
481 info->wim_version = wim->hdr.wim_version;
482 info->chunk_size = wim->chunk_size;
483 info->part_number = wim->hdr.part_number;
484 info->total_parts = wim->hdr.total_parts;
485 info->compression_type = wim->compression_type;
486 info->total_bytes = xml_get_total_bytes(wim->xml_info);
487 info->has_integrity_table = wim_has_integrity_table(wim);
488 info->opened_from_file = (wim->filename != NULL);
489 info->is_readonly = (wim->hdr.flags & WIM_HDR_FLAG_READONLY) ||
490 (wim->hdr.total_parts != 1) ||
491 (wim->filename && taccess(wim->filename, W_OK));
492 info->has_rpfix = (wim->hdr.flags & WIM_HDR_FLAG_RP_FIX) != 0;
493 info->is_marked_readonly = (wim->hdr.flags & WIM_HDR_FLAG_READONLY) != 0;
494 info->write_in_progress = (wim->hdr.flags & WIM_HDR_FLAG_WRITE_IN_PROGRESS) != 0;
495 info->metadata_only = (wim->hdr.flags & WIM_HDR_FLAG_METADATA_ONLY) != 0;
496 info->resource_only = (wim->hdr.flags & WIM_HDR_FLAG_RESOURCE_ONLY) != 0;
497 info->spanned = (wim->hdr.flags & WIM_HDR_FLAG_SPANNED) != 0;
498 info->pipable = wim_is_pipable(wim);
502 /* API function documented in wimlib.h */
504 wimlib_set_wim_info(WIMStruct *wim, const struct wimlib_wim_info *info, int which)
506 if (which & ~(WIMLIB_CHANGE_READONLY_FLAG |
508 WIMLIB_CHANGE_BOOT_INDEX |
509 WIMLIB_CHANGE_RPFIX_FLAG))
510 return WIMLIB_ERR_INVALID_PARAM;
512 if ((which & WIMLIB_CHANGE_BOOT_INDEX) &&
513 info->boot_index > wim->hdr.image_count)
514 return WIMLIB_ERR_INVALID_IMAGE;
516 if (which & WIMLIB_CHANGE_READONLY_FLAG) {
517 if (info->is_marked_readonly)
518 wim->hdr.flags |= WIM_HDR_FLAG_READONLY;
520 wim->hdr.flags &= ~WIM_HDR_FLAG_READONLY;
523 if (which & WIMLIB_CHANGE_GUID)
524 copy_guid(wim->hdr.guid, info->guid);
526 if (which & WIMLIB_CHANGE_BOOT_INDEX)
527 wim->hdr.boot_idx = info->boot_index;
529 if (which & WIMLIB_CHANGE_RPFIX_FLAG) {
531 wim->hdr.flags |= WIM_HDR_FLAG_RP_FIX;
533 wim->hdr.flags &= ~WIM_HDR_FLAG_RP_FIX;
538 /* API function documented in wimlib.h */
540 wimlib_set_output_compression_type(WIMStruct *wim,
541 enum wimlib_compression_type ctype)
543 if (!wim_compression_type_valid(ctype))
544 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
546 wim->out_compression_type = ctype;
548 /* Reset the chunk size if it's no longer valid. */
549 if (!wim_chunk_size_valid(wim->out_chunk_size, ctype))
550 wim->out_chunk_size = wim_default_nonsolid_chunk_size(ctype);
554 /* API function documented in wimlib.h */
556 wimlib_set_output_pack_compression_type(WIMStruct *wim,
557 enum wimlib_compression_type ctype)
559 if (!wim_compression_type_valid(ctype))
560 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
562 /* Solid resources can't be uncompressed. */
563 if (ctype == WIMLIB_COMPRESSION_TYPE_NONE)
564 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
566 wim->out_solid_compression_type = ctype;
568 /* Reset the chunk size if it's no longer valid. */
569 if (!wim_chunk_size_valid(wim->out_solid_chunk_size, ctype))
570 wim->out_solid_chunk_size = wim_default_solid_chunk_size(ctype);
574 /* API function documented in wimlib.h */
576 wimlib_set_output_chunk_size(WIMStruct *wim, u32 chunk_size)
578 if (chunk_size == 0) {
579 wim->out_chunk_size =
580 wim_default_nonsolid_chunk_size(wim->out_compression_type);
584 if (!wim_chunk_size_valid(chunk_size, wim->out_compression_type))
585 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
587 wim->out_chunk_size = chunk_size;
591 /* API function documented in wimlib.h */
593 wimlib_set_output_pack_chunk_size(WIMStruct *wim, u32 chunk_size)
595 if (chunk_size == 0) {
596 wim->out_solid_chunk_size =
597 wim_default_solid_chunk_size(wim->out_solid_compression_type);
601 if (!wim_chunk_size_valid(chunk_size, wim->out_solid_compression_type))
602 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
604 wim->out_solid_chunk_size = chunk_size;
608 /* API function documented in wimlib.h */
609 WIMLIBAPI const tchar *
610 wimlib_get_compression_type_string(enum wimlib_compression_type ctype)
612 if (!wim_compression_type_valid(ctype))
615 return wim_ctype_info[(unsigned)ctype].name;
619 wimlib_register_progress_function(WIMStruct *wim,
620 wimlib_progress_func_t progfunc,
623 wim->progfunc = progfunc;
624 wim->progctx = progctx;
628 open_wim_file(const tchar *filename, struct filedes *fd_ret)
632 raw_fd = topen(filename, O_RDONLY | O_BINARY);
634 ERROR_WITH_ERRNO("Can't open \"%"TS"\" read-only", filename);
635 return WIMLIB_ERR_OPEN;
637 filedes_init(fd_ret, raw_fd);
642 * Begins the reading of a WIM file; opens the file and reads its header and
643 * blob table, and optionally checks the integrity.
646 begin_read(WIMStruct *wim, const void *wim_filename_or_fd, int open_flags)
649 const tchar *wimfile;
651 if (open_flags & WIMLIB_OPEN_FLAG_FROM_PIPE) {
653 filedes_init(&wim->in_fd, *(const int*)wim_filename_or_fd);
654 wim->in_fd.is_pipe = 1;
656 wimfile = wim_filename_or_fd;
657 ret = open_wim_file(wimfile, &wim->in_fd);
661 /* The absolute path to the WIM is requested so that
662 * wimlib_overwrite() still works even if the process changes
663 * its working directory. This actually happens if a WIM is
664 * mounted read-write, since the FUSE thread changes directory
665 * to "/", and it needs to be able to find the WIM file again.
667 * This will break if the full path to the WIM changes in the
668 * intervening time...
670 * Warning: in Windows native builds, realpath() calls the
671 * replacement function in win32_replacements.c.
673 wim->filename = realpath(wimfile, NULL);
674 if (!wim->filename) {
675 ERROR_WITH_ERRNO("Failed to get full path to file "
676 "\"%"TS"\"", wimfile);
678 return WIMLIB_ERR_NOMEM;
680 return WIMLIB_ERR_NO_FILENAME;
684 ret = read_wim_header(wim, &wim->hdr);
688 if (wim->hdr.flags & WIM_HDR_FLAG_WRITE_IN_PROGRESS) {
689 WARNING("The WIM_HDR_FLAG_WRITE_IN_PROGRESS flag is set in the header of\n"
690 " \"%"TS"\". It may be being changed by another process,\n"
691 " or a process may have crashed while writing the WIM.",
695 if (open_flags & WIMLIB_OPEN_FLAG_WRITE_ACCESS) {
696 ret = can_modify_wim(wim);
701 if ((open_flags & WIMLIB_OPEN_FLAG_ERROR_IF_SPLIT) &&
702 (wim->hdr.total_parts != 1))
703 return WIMLIB_ERR_IS_SPLIT_WIM;
705 /* If the boot index is invalid, print a warning and set it to 0 */
706 if (wim->hdr.boot_idx > wim->hdr.image_count) {
707 WARNING("Ignoring invalid boot index.");
708 wim->hdr.boot_idx = 0;
711 /* Check and cache the compression type */
712 if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESSION) {
713 if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESS_LZX) {
714 wim->compression_type = WIMLIB_COMPRESSION_TYPE_LZX;
715 } else if (wim->hdr.flags & (WIM_HDR_FLAG_COMPRESS_XPRESS |
716 WIM_HDR_FLAG_COMPRESS_XPRESS_2)) {
717 wim->compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
718 } else if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESS_LZMS) {
719 wim->compression_type = WIMLIB_COMPRESSION_TYPE_LZMS;
721 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
724 wim->compression_type = WIMLIB_COMPRESSION_TYPE_NONE;
726 wim->out_compression_type = wim->compression_type;
728 /* Check and cache the chunk size. */
729 wim->chunk_size = wim->hdr.chunk_size;
730 wim->out_chunk_size = wim->chunk_size;
731 if (!wim_chunk_size_valid(wim->chunk_size, wim->compression_type)) {
732 ERROR("Invalid chunk size (%"PRIu32" bytes) "
733 "for compression type %"TS"!", wim->chunk_size,
734 wimlib_get_compression_type_string(wim->compression_type));
735 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
738 if (open_flags & WIMLIB_OPEN_FLAG_CHECK_INTEGRITY) {
739 ret = check_wim_integrity(wim);
740 if (ret == WIM_INTEGRITY_NONEXISTENT) {
741 WARNING("\"%"TS"\" does not contain integrity "
742 "information. Skipping integrity check.",
744 } else if (ret == WIM_INTEGRITY_NOT_OK) {
745 return WIMLIB_ERR_INTEGRITY;
746 } else if (ret != WIM_INTEGRITY_OK) {
751 if (wim->hdr.image_count != 0 && wim->hdr.part_number == 1) {
752 wim->image_metadata = CALLOC(wim->hdr.image_count,
753 sizeof(wim->image_metadata[0]));
754 if (!wim->image_metadata)
755 return WIMLIB_ERR_NOMEM;
758 if (open_flags & WIMLIB_OPEN_FLAG_FROM_PIPE) {
759 wim->blob_table = new_blob_table(64);
760 if (!wim->blob_table)
761 return WIMLIB_ERR_NOMEM;
763 if (wim->hdr.blob_table_reshdr.uncompressed_size == 0 &&
764 wim->hdr.xml_data_reshdr.uncompressed_size == 0)
765 return WIMLIB_ERR_WIM_IS_INCOMPLETE;
767 ret = read_wim_xml_data(wim);
771 if (xml_get_image_count(wim->xml_info) != wim->hdr.image_count) {
772 ERROR("The WIM's header is inconsistent with its XML data.\n"
773 " Please submit a bug report if you believe this "
774 "WIM file should be considered valid.");
775 return WIMLIB_ERR_IMAGE_COUNT;
778 ret = read_blob_table(wim);
786 open_wim_as_WIMStruct(const void *wim_filename_or_fd, int open_flags,
788 wimlib_progress_func_t progfunc, void *progctx)
793 ret = wimlib_global_init(WIMLIB_INIT_FLAG_ASSUME_UTF8);
797 wim = new_wim_struct();
799 return WIMLIB_ERR_NOMEM;
801 wim->progfunc = progfunc;
802 wim->progctx = progctx;
804 ret = begin_read(wim, wim_filename_or_fd, open_flags);
814 /* API function documented in wimlib.h */
816 wimlib_open_wim_with_progress(const tchar *wimfile, int open_flags,
818 wimlib_progress_func_t progfunc, void *progctx)
820 if (open_flags & ~(WIMLIB_OPEN_FLAG_CHECK_INTEGRITY |
821 WIMLIB_OPEN_FLAG_ERROR_IF_SPLIT |
822 WIMLIB_OPEN_FLAG_WRITE_ACCESS))
823 return WIMLIB_ERR_INVALID_PARAM;
825 if (!wimfile || !*wimfile || !wim_ret)
826 return WIMLIB_ERR_INVALID_PARAM;
828 return open_wim_as_WIMStruct(wimfile, open_flags, wim_ret,
832 /* API function documented in wimlib.h */
834 wimlib_open_wim(const tchar *wimfile, int open_flags, WIMStruct **wim_ret)
836 return wimlib_open_wim_with_progress(wimfile, open_flags, wim_ret,
840 /* Checksum all blobs that are unhashed (other than the metadata blobs), merging
841 * them into the blob table as needed. This is a no-op unless files have been
842 * added to an image in the same WIMStruct. */
844 wim_checksum_unhashed_blobs(WIMStruct *wim)
848 if (!wim_has_metadata(wim))
850 for (int i = 0; i < wim->hdr.image_count; i++) {
851 struct blob_descriptor *blob, *tmp;
852 struct wim_image_metadata *imd = wim->image_metadata[i];
853 image_for_each_unhashed_blob_safe(blob, tmp, imd) {
854 struct blob_descriptor *new_blob;
855 ret = hash_unhashed_blob(blob, wim->blob_table, &new_blob);
858 if (new_blob != blob)
859 free_blob_descriptor(blob);
866 * can_modify_wim - Check if a given WIM is writeable. This is only the case if
867 * it meets the following three conditions:
869 * 1. Write access is allowed to the underlying file (if any) at the filesystem level.
870 * 2. The WIM is not part of a spanned set.
871 * 3. The WIM_HDR_FLAG_READONLY flag is not set in the WIM header.
873 * Return value is 0 if writable; WIMLIB_ERR_WIM_IS_READONLY otherwise.
876 can_modify_wim(WIMStruct *wim)
879 if (taccess(wim->filename, W_OK)) {
880 ERROR_WITH_ERRNO("Can't modify \"%"TS"\"", wim->filename);
881 return WIMLIB_ERR_WIM_IS_READONLY;
884 if (wim->hdr.total_parts != 1) {
885 ERROR("Cannot modify \"%"TS"\": is part of a split WIM",
887 return WIMLIB_ERR_WIM_IS_READONLY;
889 if (wim->hdr.flags & WIM_HDR_FLAG_READONLY) {
890 ERROR("Cannot modify \"%"TS"\": is marked read-only",
892 return WIMLIB_ERR_WIM_IS_READONLY;
897 /* Release a reference to a WIMStruct. If the reference count reaches 0, the
898 * WIMStruct is freed. */
900 wim_decrement_refcnt(WIMStruct *wim)
902 wimlib_assert(wim->refcnt > 0);
903 if (--wim->refcnt != 0)
905 if (filedes_valid(&wim->in_fd))
906 filedes_close(&wim->in_fd);
907 if (filedes_valid(&wim->out_fd))
908 filedes_close(&wim->out_fd);
909 wimlib_free_decompressor(wim->decompressor);
910 xml_free_info_struct(wim->xml_info);
915 /* API function documented in wimlib.h */
917 wimlib_free(WIMStruct *wim)
922 /* The blob table and image metadata are freed immediately, but other
923 * members of the WIMStruct such as the input file descriptor are
924 * retained until no more exported resources reference the WIMStruct. */
926 free_blob_table(wim->blob_table);
927 wim->blob_table = NULL;
928 if (wim->image_metadata != NULL) {
929 deselect_current_wim_image(wim);
930 for (int i = 0; i < wim->hdr.image_count; i++)
931 put_image_metadata(wim->image_metadata[i]);
932 FREE(wim->image_metadata);
933 wim->image_metadata = NULL;
936 wim_decrement_refcnt(wim);
940 test_locale_ctype_utf8(void)
945 char *ctype = nl_langinfo(CODESET);
947 return (strstr(ctype, "UTF-8") ||
948 strstr(ctype, "UTF8") ||
949 strstr(ctype, "utf8") ||
950 strstr(ctype, "utf-8"));
954 /* API function documented in wimlib.h */
956 wimlib_get_version(void)
958 return (WIMLIB_MAJOR_VERSION << 20) |
959 (WIMLIB_MINOR_VERSION << 10) |
960 WIMLIB_PATCH_VERSION;
963 static bool lib_initialized = false;
964 static pthread_mutex_t lib_initialization_mutex = PTHREAD_MUTEX_INITIALIZER;
966 /* API function documented in wimlib.h */
968 wimlib_global_init(int init_flags)
975 pthread_mutex_lock(&lib_initialization_mutex);
980 #ifdef ENABLE_ERROR_MESSAGES
981 if (!wimlib_error_file)
982 wimlib_error_file = stderr;
985 ret = WIMLIB_ERR_INVALID_PARAM;
986 if (init_flags & ~(WIMLIB_INIT_FLAG_ASSUME_UTF8 |
987 WIMLIB_INIT_FLAG_DONT_ACQUIRE_PRIVILEGES |
988 WIMLIB_INIT_FLAG_STRICT_CAPTURE_PRIVILEGES |
989 WIMLIB_INIT_FLAG_STRICT_APPLY_PRIVILEGES |
990 WIMLIB_INIT_FLAG_DEFAULT_CASE_SENSITIVE |
991 WIMLIB_INIT_FLAG_DEFAULT_CASE_INSENSITIVE))
994 ret = WIMLIB_ERR_INVALID_PARAM;
995 if ((init_flags & (WIMLIB_INIT_FLAG_DEFAULT_CASE_SENSITIVE |
996 WIMLIB_INIT_FLAG_DEFAULT_CASE_INSENSITIVE))
997 == (WIMLIB_INIT_FLAG_DEFAULT_CASE_SENSITIVE |
998 WIMLIB_INIT_FLAG_DEFAULT_CASE_INSENSITIVE))
1002 if (!(init_flags & WIMLIB_INIT_FLAG_ASSUME_UTF8)) {
1003 wimlib_mbs_is_utf8 = test_locale_ctype_utf8();
1005 if (!wimlib_mbs_is_utf8)
1006 libntfs3g_global_init();
1010 ret = win32_global_init(init_flags);
1014 iconv_global_init();
1016 if (init_flags & WIMLIB_INIT_FLAG_DEFAULT_CASE_SENSITIVE)
1017 default_ignore_case = false;
1018 else if (init_flags & WIMLIB_INIT_FLAG_DEFAULT_CASE_INSENSITIVE)
1019 default_ignore_case = true;
1020 lib_initialized = true;
1023 pthread_mutex_unlock(&lib_initialization_mutex);
1028 /* API function documented in wimlib.h */
1030 wimlib_global_cleanup(void)
1032 if (!lib_initialized)
1035 pthread_mutex_lock(&lib_initialization_mutex);
1037 if (!lib_initialized)
1040 xml_global_cleanup();
1041 iconv_global_cleanup();
1043 win32_global_cleanup();
1046 wimlib_set_error_file(NULL);
1047 lib_initialized = false;
1050 pthread_mutex_unlock(&lib_initialization_mutex);