2 * wim.c - High-level code dealing with WIMStructs and images.
6 * Copyright (C) 2012, 2013, 2014 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>
35 #include "wimlib/assert.h"
36 #include "wimlib/bitops.h"
37 #include "wimlib/dentry.h"
38 #include "wimlib/encoding.h"
39 #include "wimlib/file_io.h"
40 #include "wimlib/integrity.h"
41 #include "wimlib/lookup_table.h"
42 #include "wimlib/metadata.h"
44 # include "wimlib/ntfs_3g.h" /* for do_ntfs_umount() */
46 #include "wimlib/security.h"
47 #include "wimlib/wim.h"
48 #include "wimlib/xml.h"
50 # include "wimlib/win32.h" /* for realpath() replacement */
54 wim_default_solid_compression_type(void)
56 return WIMLIB_COMPRESSION_TYPE_LZMS;
60 wim_default_solid_chunk_size(int ctype) {
62 case WIMLIB_COMPRESSION_TYPE_LZMS:
63 return (u32)1 << 26; /* 67108864 */
65 return (u32)1 << 15; /* 32768 */
72 WIMStruct *wim = CALLOC(1, sizeof(WIMStruct));
76 filedes_invalidate(&wim->in_fd);
77 filedes_invalidate(&wim->out_fd);
78 wim->out_solid_compression_type = wim_default_solid_compression_type();
79 wim->out_solid_chunk_size = wim_default_solid_chunk_size(
80 wim->out_solid_compression_type);
81 INIT_LIST_HEAD(&wim->subwims);
85 /* Determine if the chunk size is valid for the specified compression type. */
87 wim_chunk_size_valid(u32 chunk_size, int ctype)
91 /* Chunk size is meaningless for uncompressed WIMs --- any value is
93 if (ctype == WIMLIB_COMPRESSION_TYPE_NONE)
96 /* Chunk size must be power of 2. */
99 order = fls32(chunk_size);
100 if (chunk_size != 1U << order)
119 /* See the documentation for the --chunk-size option of `wimlib-imagex
120 * capture' for information about allowed chunk sizes. */
122 case WIMLIB_COMPRESSION_TYPE_LZX:
123 return order >= 15 && order <= 21;
124 case WIMLIB_COMPRESSION_TYPE_XPRESS:
125 return order >= 12 && order <= 16;
126 case WIMLIB_COMPRESSION_TYPE_LZMS:
127 return order >= 15 && order <= 30;
132 /* Return the default chunk size to use for the specified compression type.
134 * See notes above in wim_chunk_size_valid(). */
136 wim_default_chunk_size(int ctype)
139 case WIMLIB_COMPRESSION_TYPE_LZMS:
140 return 1U << 17; /* 131072 */
142 return 1U << 15; /* 32768 */
147 * Calls a function on images in the WIM. If @image is WIMLIB_ALL_IMAGES,
148 * @visitor is called on the WIM once for each image, with each image selected
149 * as the current image in turn. If @image is a certain image, @visitor is
150 * called on the WIM only once, with that image selected.
153 for_image(WIMStruct *wim, int image, int (*visitor)(WIMStruct *))
160 if (image == WIMLIB_ALL_IMAGES) {
162 end = wim->hdr.image_count;
163 } else if (image >= 1 && image <= wim->hdr.image_count) {
167 return WIMLIB_ERR_INVALID_IMAGE;
169 for (i = start; i <= end; i++) {
170 ret = select_wim_image(wim, i);
180 /* API function documented in wimlib.h */
182 wimlib_create_new_wim(int ctype, WIMStruct **wim_ret)
185 struct wim_lookup_table *table;
188 ret = wimlib_global_init(WIMLIB_INIT_FLAG_ASSUME_UTF8);
192 wim = new_wim_struct();
194 return WIMLIB_ERR_NOMEM;
196 ret = init_wim_header(&wim->hdr, ctype, wim_default_chunk_size(ctype));
200 table = new_lookup_table(9001);
202 ret = WIMLIB_ERR_NOMEM;
205 wim->lookup_table = table;
206 wim->compression_type = ctype;
207 wim->out_compression_type = ctype;
208 wim->chunk_size = wim->hdr.chunk_size;
209 wim->out_chunk_size = wim->hdr.chunk_size;
219 destroy_image_metadata(struct wim_image_metadata *imd,
220 struct wim_lookup_table *table,
221 bool free_metadata_lte)
223 free_dentry_tree(imd->root_dentry, table);
224 imd->root_dentry = NULL;
225 free_wim_security_data(imd->security_data);
226 imd->security_data = NULL;
228 if (free_metadata_lte) {
229 free_lookup_table_entry(imd->metadata_lte);
230 imd->metadata_lte = NULL;
233 struct wim_lookup_table_entry *lte, *tmp;
234 list_for_each_entry_safe(lte, tmp, &imd->unhashed_streams, unhashed_list)
235 free_lookup_table_entry(lte);
237 INIT_LIST_HEAD(&imd->unhashed_streams);
238 INIT_LIST_HEAD(&imd->inode_list);
241 do_ntfs_umount(imd->ntfs_vol);
242 imd->ntfs_vol = NULL;
248 put_image_metadata(struct wim_image_metadata *imd,
249 struct wim_lookup_table *table)
251 if (imd && --imd->refcnt == 0) {
252 destroy_image_metadata(imd, table, true);
257 /* Appends the specified image metadata structure to the array of image metadata
258 * for a WIM, and increments the image count. */
260 append_image_metadata(WIMStruct *wim, struct wim_image_metadata *imd)
262 struct wim_image_metadata **imd_array;
264 imd_array = REALLOC(wim->image_metadata,
265 sizeof(wim->image_metadata[0]) * (wim->hdr.image_count + 1));
268 return WIMLIB_ERR_NOMEM;
269 wim->image_metadata = imd_array;
270 imd_array[wim->hdr.image_count++] = imd;
274 struct wim_image_metadata *
275 new_image_metadata(void)
277 struct wim_image_metadata *imd;
279 imd = CALLOC(1, sizeof(*imd));
282 INIT_LIST_HEAD(&imd->inode_list);
283 INIT_LIST_HEAD(&imd->unhashed_streams);
288 static struct wim_image_metadata **
289 new_image_metadata_array(unsigned num_images)
291 struct wim_image_metadata **imd_array;
293 imd_array = CALLOC(num_images, sizeof(imd_array[0]));
297 for (unsigned i = 0; i < num_images; i++) {
298 imd_array[i] = new_image_metadata();
299 if (unlikely(!imd_array[i])) {
300 for (unsigned j = 0; j < i; j++)
301 put_image_metadata(imd_array[j], NULL);
311 * Load the metadata for the specified WIM image into memory and set it
312 * as the WIMStruct's currently selected image.
315 * The WIMStruct for the WIM.
317 * The 1-based index of the image in the WIM to select.
319 * On success, 0 will be returned, wim->current_image will be set to
320 * @image, and wim_get_current_image_metadata() can be used to retrieve
321 * metadata information for the image.
323 * On failure, WIMLIB_ERR_INVALID_IMAGE, WIMLIB_ERR_METADATA_NOT_FOUND,
324 * or another error code will be returned.
327 select_wim_image(WIMStruct *wim, int image)
329 struct wim_image_metadata *imd;
332 if (image == WIMLIB_NO_IMAGE)
333 return WIMLIB_ERR_INVALID_IMAGE;
335 if (image == wim->current_image)
338 if (image < 1 || image > wim->hdr.image_count)
339 return WIMLIB_ERR_INVALID_IMAGE;
341 if (!wim_has_metadata(wim))
342 return WIMLIB_ERR_METADATA_NOT_FOUND;
344 /* If a valid image is currently selected, its metadata can be freed if
345 * it has not been modified. */
346 deselect_current_wim_image(wim);
347 wim->current_image = image;
348 imd = wim_get_current_image_metadata(wim);
349 if (imd->root_dentry || imd->modified) {
352 ret = read_metadata_resource(imd);
354 wim->current_image = WIMLIB_NO_IMAGE;
360 deselect_current_wim_image(WIMStruct *wim)
362 struct wim_image_metadata *imd;
363 if (wim->current_image == WIMLIB_NO_IMAGE)
365 imd = wim_get_current_image_metadata(wim);
366 if (!imd->modified) {
367 wimlib_assert(list_empty(&imd->unhashed_streams));
368 destroy_image_metadata(imd, NULL, false);
370 wim->current_image = WIMLIB_NO_IMAGE;
373 /* API function documented in wimlib.h */
374 WIMLIBAPI const tchar *
375 wimlib_get_compression_type_string(int ctype)
378 case WIMLIB_COMPRESSION_TYPE_NONE:
380 case WIMLIB_COMPRESSION_TYPE_XPRESS:
382 case WIMLIB_COMPRESSION_TYPE_LZX:
384 case WIMLIB_COMPRESSION_TYPE_LZMS:
391 /* API function documented in wimlib.h */
393 wimlib_resolve_image(WIMStruct *wim, const tchar *image_name_or_num)
399 if (!image_name_or_num || !*image_name_or_num)
400 return WIMLIB_NO_IMAGE;
402 if (!tstrcasecmp(image_name_or_num, T("all"))
403 || !tstrcasecmp(image_name_or_num, T("*")))
404 return WIMLIB_ALL_IMAGES;
405 image = tstrtol(image_name_or_num, &p, 10);
406 if (p != image_name_or_num && *p == T('\0') && image > 0) {
407 if (image > wim->hdr.image_count)
408 return WIMLIB_NO_IMAGE;
411 for (i = 1; i <= wim->hdr.image_count; i++) {
412 if (!tstrcmp(image_name_or_num,
413 wimlib_get_image_name(wim, i)))
416 return WIMLIB_NO_IMAGE;
420 /* API function documented in wimlib.h */
422 wimlib_print_available_images(const WIMStruct *wim, int image)
428 if (image == WIMLIB_ALL_IMAGES) {
429 n = tprintf(T("Available Images:\n"));
431 last = wim->hdr.image_count;
432 } else if (image >= 1 && image <= wim->hdr.image_count) {
433 n = tprintf(T("Information for Image %d\n"), image);
437 tprintf(T("wimlib_print_available_images(): Invalid image %d"),
441 for (i = 0; i < n - 1; i++)
444 for (i = first; i <= last; i++)
445 print_image_info(wim->wim_info, i);
448 /* API function documented in wimlib.h */
450 wimlib_get_wim_info(WIMStruct *wim, struct wimlib_wim_info *info)
452 memset(info, 0, sizeof(struct wimlib_wim_info));
453 memcpy(info->guid, wim->hdr.guid, WIMLIB_GUID_LEN);
454 info->image_count = wim->hdr.image_count;
455 info->boot_index = wim->hdr.boot_idx;
456 info->wim_version = wim->hdr.wim_version;
457 info->chunk_size = wim->chunk_size;
458 info->part_number = wim->hdr.part_number;
459 info->total_parts = wim->hdr.total_parts;
460 info->compression_type = wim->compression_type;
461 info->total_bytes = wim_info_get_total_bytes(wim->wim_info);
462 info->has_integrity_table = wim_has_integrity_table(wim);
463 info->opened_from_file = (wim->filename != NULL);
464 info->is_readonly = (wim->hdr.flags & WIM_HDR_FLAG_READONLY) ||
465 (wim->hdr.total_parts != 1) ||
466 (wim->filename && taccess(wim->filename, W_OK));
467 info->has_rpfix = (wim->hdr.flags & WIM_HDR_FLAG_RP_FIX) != 0;
468 info->is_marked_readonly = (wim->hdr.flags & WIM_HDR_FLAG_READONLY) != 0;
469 info->write_in_progress = (wim->hdr.flags & WIM_HDR_FLAG_WRITE_IN_PROGRESS) != 0;
470 info->metadata_only = (wim->hdr.flags & WIM_HDR_FLAG_METADATA_ONLY) != 0;
471 info->resource_only = (wim->hdr.flags & WIM_HDR_FLAG_RESOURCE_ONLY) != 0;
472 info->spanned = (wim->hdr.flags & WIM_HDR_FLAG_SPANNED) != 0;
473 info->pipable = wim_is_pipable(wim);
477 /* API function documented in wimlib.h */
479 wimlib_set_wim_info(WIMStruct *wim, const struct wimlib_wim_info *info, int which)
481 if (which & ~(WIMLIB_CHANGE_READONLY_FLAG |
483 WIMLIB_CHANGE_BOOT_INDEX |
484 WIMLIB_CHANGE_RPFIX_FLAG))
485 return WIMLIB_ERR_INVALID_PARAM;
487 if ((which & WIMLIB_CHANGE_BOOT_INDEX) &&
488 info->boot_index > wim->hdr.image_count)
489 return WIMLIB_ERR_INVALID_IMAGE;
491 if (which & WIMLIB_CHANGE_READONLY_FLAG) {
492 if (info->is_marked_readonly)
493 wim->hdr.flags |= WIM_HDR_FLAG_READONLY;
495 wim->hdr.flags &= ~WIM_HDR_FLAG_READONLY;
498 if (which & WIMLIB_CHANGE_GUID)
499 memcpy(wim->hdr.guid, info->guid, WIM_GUID_LEN);
501 if (which & WIMLIB_CHANGE_BOOT_INDEX)
502 wim->hdr.boot_idx = info->boot_index;
504 if (which & WIMLIB_CHANGE_RPFIX_FLAG) {
506 wim->hdr.flags |= WIM_HDR_FLAG_RP_FIX;
508 wim->hdr.flags &= ~WIM_HDR_FLAG_RP_FIX;
514 set_out_ctype(int ctype, u8 *out_ctype_p)
517 case WIMLIB_COMPRESSION_TYPE_NONE:
518 case WIMLIB_COMPRESSION_TYPE_LZX:
519 case WIMLIB_COMPRESSION_TYPE_XPRESS:
520 case WIMLIB_COMPRESSION_TYPE_LZMS:
521 *out_ctype_p = ctype;
524 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
527 /* API function documented in wimlib.h */
529 wimlib_set_output_compression_type(WIMStruct *wim, int ctype)
531 int ret = set_out_ctype(ctype, &wim->out_compression_type);
535 /* Reset the chunk size if it's no longer valid. */
536 if (!wim_chunk_size_valid(wim->out_chunk_size, ctype))
537 wim->out_chunk_size = wim_default_chunk_size(ctype);
541 /* API function documented in wimlib.h */
543 wimlib_set_output_pack_compression_type(WIMStruct *wim, int ctype)
545 int ret = set_out_ctype(ctype, &wim->out_solid_compression_type);
549 /* Reset the chunk size if it's no longer valid. */
550 if (!wim_chunk_size_valid(wim->out_solid_chunk_size, ctype))
551 wim->out_solid_chunk_size = wim_default_solid_chunk_size(ctype);
556 set_out_chunk_size(u32 chunk_size, int ctype, u32 *out_chunk_size_p)
558 if (!wim_chunk_size_valid(chunk_size, ctype))
559 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
561 *out_chunk_size_p = chunk_size;
565 /* API function documented in wimlib.h */
567 wimlib_set_output_chunk_size(WIMStruct *wim, uint32_t chunk_size)
569 if (chunk_size == 0) {
570 wim->out_chunk_size =
571 wim_default_chunk_size(wim->out_compression_type);
575 return set_out_chunk_size(chunk_size,
576 wim->out_compression_type,
577 &wim->out_chunk_size);
580 /* API function documented in wimlib.h */
582 wimlib_set_output_pack_chunk_size(WIMStruct *wim, uint32_t chunk_size)
584 if (chunk_size == 0) {
585 wim->out_solid_chunk_size =
586 wim_default_solid_chunk_size(wim->out_solid_compression_type);
590 return set_out_chunk_size(chunk_size,
591 wim->out_solid_compression_type,
592 &wim->out_solid_chunk_size);
596 wimlib_register_progress_function(WIMStruct *wim,
597 wimlib_progress_func_t progfunc,
600 wim->progfunc = progfunc;
601 wim->progctx = progctx;
605 open_wim_file(const tchar *filename, struct filedes *fd_ret)
609 raw_fd = topen(filename, O_RDONLY | O_BINARY);
611 ERROR_WITH_ERRNO("Can't open \"%"TS"\" read-only", filename);
612 return WIMLIB_ERR_OPEN;
614 filedes_init(fd_ret, raw_fd);
619 * Begins the reading of a WIM file; opens the file and reads its header and
620 * lookup table, and optionally checks the integrity.
623 begin_read(WIMStruct *wim, const void *wim_filename_or_fd, int open_flags)
627 const tchar *wimfile;
629 if (open_flags & WIMLIB_OPEN_FLAG_FROM_PIPE) {
631 filedes_init(&wim->in_fd, *(const int*)wim_filename_or_fd);
632 wim->in_fd.is_pipe = 1;
634 wimfile = wim_filename_or_fd;
635 ret = open_wim_file(wimfile, &wim->in_fd);
639 /* The absolute path to the WIM is requested so that
640 * wimlib_overwrite() still works even if the process changes
641 * its working directory. This actually happens if a WIM is
642 * mounted read-write, since the FUSE thread changes directory
643 * to "/", and it needs to be able to find the WIM file again.
645 * This will break if the full path to the WIM changes in the
646 * intervening time...
648 * Warning: in Windows native builds, realpath() calls the
649 * replacement function in win32_replacements.c.
651 wim->filename = realpath(wimfile, NULL);
652 if (!wim->filename) {
653 ERROR_WITH_ERRNO("Failed to get full path to file "
654 "\"%"TS"\"", wimfile);
656 return WIMLIB_ERR_NOMEM;
658 return WIMLIB_ERR_NO_FILENAME;
662 ret = read_wim_header(wim, &wim->hdr);
666 if (wim->hdr.flags & WIM_HDR_FLAG_WRITE_IN_PROGRESS) {
667 WARNING("The WIM_HDR_FLAG_WRITE_IN_PROGRESS flag is set in the header of\n"
668 " \"%"TS"\". It may be being changed by another process,\n"
669 " or a process may have crashed while writing the WIM.",
673 if (open_flags & WIMLIB_OPEN_FLAG_WRITE_ACCESS) {
674 ret = can_modify_wim(wim);
679 if ((open_flags & WIMLIB_OPEN_FLAG_ERROR_IF_SPLIT) &&
680 (wim->hdr.total_parts != 1))
681 return WIMLIB_ERR_IS_SPLIT_WIM;
683 /* If the boot index is invalid, print a warning and set it to 0 */
684 if (wim->hdr.boot_idx > wim->hdr.image_count) {
685 WARNING("Ignoring invalid boot index.");
686 wim->hdr.boot_idx = 0;
689 /* Check and cache the compression type */
690 if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESSION) {
691 if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESS_LZX) {
692 wim->compression_type = WIMLIB_COMPRESSION_TYPE_LZX;
693 } else if (wim->hdr.flags & (WIM_HDR_FLAG_COMPRESS_XPRESS |
694 WIM_HDR_FLAG_COMPRESS_XPRESS_2)) {
695 wim->compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
696 } else if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESS_LZMS) {
697 wim->compression_type = WIMLIB_COMPRESSION_TYPE_LZMS;
699 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
702 wim->compression_type = WIMLIB_COMPRESSION_TYPE_NONE;
704 wim->out_compression_type = wim->compression_type;
706 /* Check and cache the chunk size. */
707 wim->chunk_size = wim->hdr.chunk_size;
708 wim->out_chunk_size = wim->chunk_size;
709 if (!wim_chunk_size_valid(wim->chunk_size, wim->compression_type)) {
710 ERROR("Invalid chunk size (%"PRIu32" bytes) "
711 "for compression type %"TS"!", wim->chunk_size,
712 wimlib_get_compression_type_string(wim->compression_type));
713 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
716 if (open_flags & WIMLIB_OPEN_FLAG_CHECK_INTEGRITY) {
717 ret = check_wim_integrity(wim);
718 if (ret == WIM_INTEGRITY_NONEXISTENT) {
719 WARNING("\"%"TS"\" does not contain integrity "
720 "information. Skipping integrity check.",
722 } else if (ret == WIM_INTEGRITY_NOT_OK) {
723 return WIMLIB_ERR_INTEGRITY;
724 } else if (ret != WIM_INTEGRITY_OK) {
729 if (wim->hdr.image_count != 0 && wim->hdr.part_number == 1) {
730 wim->image_metadata = new_image_metadata_array(wim->hdr.image_count);
731 if (!wim->image_metadata)
732 return WIMLIB_ERR_NOMEM;
735 if (open_flags & WIMLIB_OPEN_FLAG_FROM_PIPE) {
736 wim->lookup_table = new_lookup_table(9001);
737 if (!wim->lookup_table)
738 return WIMLIB_ERR_NOMEM;
741 ret = read_wim_xml_data(wim);
745 xml_num_images = wim_info_get_num_images(wim->wim_info);
746 if (xml_num_images != wim->hdr.image_count) {
747 ERROR("The WIM's header is inconsistent with its XML data.\n"
748 " Please submit a bug report if you believe this "
749 "WIM file should be considered valid.");
750 return WIMLIB_ERR_IMAGE_COUNT;
753 ret = read_wim_lookup_table(wim);
761 open_wim_as_WIMStruct(const void *wim_filename_or_fd, int open_flags,
763 wimlib_progress_func_t progfunc, void *progctx)
768 ret = wimlib_global_init(WIMLIB_INIT_FLAG_ASSUME_UTF8);
772 wim = new_wim_struct();
774 return WIMLIB_ERR_NOMEM;
776 wim->progfunc = progfunc;
777 wim->progctx = progctx;
779 ret = begin_read(wim, wim_filename_or_fd, open_flags);
789 /* API function documented in wimlib.h */
791 wimlib_open_wim_with_progress(const tchar *wimfile, int open_flags,
793 wimlib_progress_func_t progfunc, void *progctx)
795 if (open_flags & ~(WIMLIB_OPEN_FLAG_CHECK_INTEGRITY |
796 WIMLIB_OPEN_FLAG_ERROR_IF_SPLIT |
797 WIMLIB_OPEN_FLAG_WRITE_ACCESS))
798 return WIMLIB_ERR_INVALID_PARAM;
800 if (!wimfile || !*wimfile || !wim_ret)
801 return WIMLIB_ERR_INVALID_PARAM;
803 return open_wim_as_WIMStruct(wimfile, open_flags, wim_ret,
807 /* API function documented in wimlib.h */
809 wimlib_open_wim(const tchar *wimfile, int open_flags, WIMStruct **wim_ret)
811 return wimlib_open_wim_with_progress(wimfile, open_flags, wim_ret,
815 /* Checksum all streams that are unhashed (other than the metadata streams),
816 * merging them into the lookup table as needed. This is a no-op unless the
817 * library has previously used to add or mount an image using the same
820 wim_checksum_unhashed_streams(WIMStruct *wim)
824 if (!wim_has_metadata(wim))
826 for (int i = 0; i < wim->hdr.image_count; i++) {
827 struct wim_lookup_table_entry *lte, *tmp;
828 struct wim_image_metadata *imd = wim->image_metadata[i];
829 image_for_each_unhashed_stream_safe(lte, tmp, imd) {
830 struct wim_lookup_table_entry *new_lte;
831 ret = hash_unhashed_stream(lte, wim->lookup_table, &new_lte);
835 free_lookup_table_entry(lte);
842 * can_modify_wim - Check if a given WIM is writeable. This is only the case if
843 * it meets the following three conditions:
845 * 1. Write access is allowed to the underlying file (if any) at the filesystem level.
846 * 2. The WIM is not part of a spanned set.
847 * 3. The WIM_HDR_FLAG_READONLY flag is not set in the WIM header.
849 * Return value is 0 if writable; WIMLIB_ERR_WIM_IS_READONLY otherwise.
852 can_modify_wim(WIMStruct *wim)
855 if (taccess(wim->filename, W_OK)) {
856 ERROR_WITH_ERRNO("Can't modify \"%"TS"\"", wim->filename);
857 return WIMLIB_ERR_WIM_IS_READONLY;
860 if (wim->hdr.total_parts != 1) {
861 ERROR("Cannot modify \"%"TS"\": is part of a split WIM",
863 return WIMLIB_ERR_WIM_IS_READONLY;
865 if (wim->hdr.flags & WIM_HDR_FLAG_READONLY) {
866 ERROR("Cannot modify \"%"TS"\": is marked read-only",
868 return WIMLIB_ERR_WIM_IS_READONLY;
873 /* API function documented in wimlib.h */
875 wimlib_free(WIMStruct *wim)
880 while (!list_empty(&wim->subwims)) {
883 subwim = list_entry(wim->subwims.next, WIMStruct, subwim_node);
884 list_del(&subwim->subwim_node);
888 if (filedes_valid(&wim->in_fd))
889 filedes_close(&wim->in_fd);
890 if (filedes_valid(&wim->out_fd))
891 filedes_close(&wim->out_fd);
893 free_lookup_table(wim->lookup_table);
895 wimlib_free_decompressor(wim->decompressor);
898 free_wim_info(wim->wim_info);
899 if (wim->image_metadata) {
900 for (unsigned i = 0; i < wim->hdr.image_count; i++)
901 put_image_metadata(wim->image_metadata[i], NULL);
902 FREE(wim->image_metadata);
908 test_locale_ctype_utf8(void)
913 char *ctype = nl_langinfo(CODESET);
915 return (!strstr(ctype, "UTF-8") ||
916 !strstr(ctype, "UTF8") ||
917 !strstr(ctype, "utf8") ||
918 !strstr(ctype, "utf-8"));
922 /* API function documented in wimlib.h */
924 wimlib_get_version(void)
926 return (WIMLIB_MAJOR_VERSION << 20) |
927 (WIMLIB_MINOR_VERSION << 10) |
928 WIMLIB_PATCH_VERSION;
931 static bool lib_initialized = false;
933 /* API function documented in wimlib.h */
935 wimlib_global_init(int init_flags)
940 #ifdef ENABLE_ERROR_MESSAGES
941 if (!wimlib_error_file)
942 wimlib_error_file = stderr;
945 if (init_flags & ~(WIMLIB_INIT_FLAG_ASSUME_UTF8 |
946 WIMLIB_INIT_FLAG_DONT_ACQUIRE_PRIVILEGES |
947 WIMLIB_INIT_FLAG_STRICT_CAPTURE_PRIVILEGES |
948 WIMLIB_INIT_FLAG_STRICT_APPLY_PRIVILEGES |
949 WIMLIB_INIT_FLAG_DEFAULT_CASE_SENSITIVE |
950 WIMLIB_INIT_FLAG_DEFAULT_CASE_INSENSITIVE))
951 return WIMLIB_ERR_INVALID_PARAM;
953 libxml_global_init();
954 if (!(init_flags & WIMLIB_INIT_FLAG_ASSUME_UTF8)) {
955 wimlib_mbs_is_utf8 = test_locale_ctype_utf8();
957 if (!wimlib_mbs_is_utf8)
958 libntfs3g_global_init();
963 int ret = win32_global_init(init_flags);
970 if (init_flags & WIMLIB_INIT_FLAG_DEFAULT_CASE_SENSITIVE)
971 default_ignore_case = false;
972 else if (init_flags & WIMLIB_INIT_FLAG_DEFAULT_CASE_INSENSITIVE)
973 default_ignore_case = true;
974 lib_initialized = true;
978 /* API function documented in wimlib.h */
980 wimlib_global_cleanup(void)
982 if (!lib_initialized)
984 libxml_global_cleanup();
985 iconv_global_cleanup();
987 win32_global_cleanup();
990 wimlib_set_error_file(NULL);
991 lib_initialized = false;