2 * wim.c - Stuff that doesn't fit into any other file
6 * Copyright (C) 2012, 2013 Eric Biggers
8 * This file is part of wimlib, a library for working with WIM files.
10 * wimlib is free software; you can redistribute it and/or modify it under the
11 * terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 3 of the License, or (at your option)
15 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
16 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 * A PARTICULAR PURPOSE. See the GNU General Public License for more
20 * You should have received a copy of the GNU General Public License
21 * along with wimlib; if not, see http://www.gnu.org/licenses/.
28 #include "wimlib/error.h"
29 #include "wimlib/dentry.h"
30 #include "wimlib/encoding.h"
31 #include "wimlib/file_io.h"
32 #include "wimlib/integrity.h"
33 #include "wimlib/lookup_table.h"
34 #include "wimlib/metadata.h"
36 # include "wimlib/ntfs_3g.h" /* for do_ntfs_umount() */
38 #include "wimlib/security.h"
39 #include "wimlib/wim.h"
40 #include "wimlib/xml.h"
43 # include "wimlib/win32.h" /* for realpath() replacement */
49 # include <langinfo.h>
57 image_print_metadata(WIMStruct *wim)
59 DEBUG("Printing metadata for image %d", wim->current_image);
60 print_wim_security_data(wim_security_data(wim));
61 return for_dentry_in_tree(wim_root_dentry(wim), print_dentry,
69 WIMStruct *wim = CALLOC(1, sizeof(WIMStruct));
78 * Calls a function on images in the WIM. If @image is WIMLIB_ALL_IMAGES, @visitor
79 * is called on the WIM once for each image, with each image selected as the
80 * current image in turn. If @image is a certain image, @visitor is called on
81 * the WIM only once, with that image selected.
84 for_image(WIMStruct *wim, int image, int (*visitor)(WIMStruct *))
91 if (image == WIMLIB_ALL_IMAGES) {
93 end = wim->hdr.image_count;
94 } else if (image >= 1 && image <= wim->hdr.image_count) {
98 return WIMLIB_ERR_INVALID_IMAGE;
100 for (i = start; i <= end; i++) {
101 ret = select_wim_image(wim, i);
111 /* API function documented in wimlib.h */
113 wimlib_create_new_wim(int ctype, WIMStruct **wim_ret)
116 struct wim_lookup_table *table;
119 wimlib_global_init(WIMLIB_INIT_FLAG_ASSUME_UTF8);
121 DEBUG("Creating new WIM with %"TS" compression.",
122 wimlib_get_compression_type_string(ctype));
124 /* Allocate the WIMStruct. */
125 wim = new_wim_struct();
127 return WIMLIB_ERR_NOMEM;
129 ret = init_wim_header(&wim->hdr, ctype);
133 table = new_lookup_table(9001);
135 ret = WIMLIB_ERR_NOMEM;
138 wim->lookup_table = table;
140 wim->compression_type = ctype;
149 select_wim_image(WIMStruct *wim, int image)
151 struct wim_image_metadata *imd;
154 DEBUG("Selecting image %d", image);
156 if (image == WIMLIB_NO_IMAGE) {
157 ERROR("Invalid image: %d", WIMLIB_NO_IMAGE);
158 return WIMLIB_ERR_INVALID_IMAGE;
161 if (image == wim->current_image)
164 if (image < 1 || image > wim->hdr.image_count) {
165 ERROR("Cannot select image %d: There are only %u images",
166 image, wim->hdr.image_count);
167 return WIMLIB_ERR_INVALID_IMAGE;
170 if (wim->hdr.part_number != 1) {
171 ERROR("Cannot select an image from a non-first part of a split WIM");
172 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
175 /* If a valid image is currently selected, it can be freed if it is not
177 if (wim->current_image != WIMLIB_NO_IMAGE) {
178 imd = wim_get_current_image_metadata(wim);
179 if (!imd->modified) {
180 wimlib_assert(list_empty(&imd->unhashed_streams));
181 DEBUG("Freeing image %u", wim->current_image);
182 destroy_image_metadata(imd, NULL, false);
185 wim->current_image = image;
186 imd = wim_get_current_image_metadata(wim);
187 if (imd->root_dentry || imd->modified) {
191 DEBUG("Reading metadata resource specified by the following "
192 "lookup table entry:");
193 print_lookup_table_entry(imd->metadata_lte, stderr);
195 ret = read_metadata_resource(wim, imd);
197 wim->current_image = WIMLIB_NO_IMAGE;
203 /* API function documented in wimlib.h */
204 WIMLIBAPI const tchar *
205 wimlib_get_compression_type_string(int ctype)
208 case WIMLIB_COMPRESSION_TYPE_NONE:
210 case WIMLIB_COMPRESSION_TYPE_LZX:
212 case WIMLIB_COMPRESSION_TYPE_XPRESS:
219 /* API function documented in wimlib.h */
221 wimlib_resolve_image(WIMStruct *wim, const tchar *image_name_or_num)
227 if (!image_name_or_num || !*image_name_or_num)
228 return WIMLIB_NO_IMAGE;
230 if (!tstrcasecmp(image_name_or_num, T("all"))
231 || !tstrcasecmp(image_name_or_num, T("*")))
232 return WIMLIB_ALL_IMAGES;
233 image = tstrtol(image_name_or_num, &p, 10);
234 if (p != image_name_or_num && *p == T('\0') && image > 0) {
235 if (image > wim->hdr.image_count)
236 return WIMLIB_NO_IMAGE;
239 for (i = 1; i <= wim->hdr.image_count; i++) {
240 if (!tstrcmp(image_name_or_num,
241 wimlib_get_image_name(wim, i)))
244 return WIMLIB_NO_IMAGE;
248 /* API function documented in wimlib.h */
250 wimlib_print_available_images(const WIMStruct *wim, int image)
256 if (image == WIMLIB_ALL_IMAGES) {
257 n = tprintf(T("Available Images:\n"));
259 last = wim->hdr.image_count;
260 } else if (image >= 1 && image <= wim->hdr.image_count) {
261 n = tprintf(T("Information for Image %d\n"), image);
265 tprintf(T("wimlib_print_available_images(): Invalid image %d"),
269 for (i = 0; i < n - 1; i++)
272 for (i = first; i <= last; i++)
273 print_image_info(wim->wim_info, i);
277 /* API function documented in wimlib.h */
279 wimlib_print_metadata(WIMStruct *wim, int image)
281 if (wim->hdr.part_number != 1) {
282 ERROR("Cannot show the metadata from part %hu of a %hu-part split WIM!",
283 wim->hdr.part_number, wim->hdr.total_parts);
284 ERROR("Select the first part of the split WIM to see the metadata.");
285 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
287 return for_image(wim, image, image_print_metadata);
290 /* API function documented in wimlib.h */
292 wimlib_get_wim_info(WIMStruct *wim, struct wimlib_wim_info *info)
294 memset(info, 0, sizeof(struct wimlib_wim_info));
295 memcpy(info->guid, wim->hdr.guid, WIMLIB_GUID_LEN);
296 info->image_count = wim->hdr.image_count;
297 info->boot_index = wim->hdr.boot_idx;
298 info->wim_version = WIM_VERSION;
299 info->chunk_size = WIM_CHUNK_SIZE;
300 info->part_number = wim->hdr.part_number;
301 info->total_parts = wim->hdr.total_parts;
302 info->compression_type = wim->compression_type;
303 info->total_bytes = wim_info_get_total_bytes(wim->wim_info);
304 info->has_integrity_table = wim_has_integrity_table(wim);
305 info->opened_from_file = (wim->filename != NULL);
306 info->is_readonly = (wim->hdr.flags & WIM_HDR_FLAG_READONLY) ||
307 (wim->hdr.total_parts != 1) ||
308 (wim->filename && taccess(wim->filename, W_OK));
309 info->has_rpfix = (wim->hdr.flags & WIM_HDR_FLAG_RP_FIX) != 0;
310 info->is_marked_readonly = (wim->hdr.flags & WIM_HDR_FLAG_READONLY) != 0;
311 info->write_in_progress = (wim->hdr.flags & WIM_HDR_FLAG_WRITE_IN_PROGRESS) != 0;
312 info->metadata_only = (wim->hdr.flags & WIM_HDR_FLAG_METADATA_ONLY) != 0;
313 info->resource_only = (wim->hdr.flags & WIM_HDR_FLAG_RESOURCE_ONLY) != 0;
314 info->spanned = (wim->hdr.flags & WIM_HDR_FLAG_SPANNED) != 0;
315 info->pipable = wim_is_pipable(wim);
319 /* API function documented in wimlib.h */
321 wimlib_set_wim_info(WIMStruct *wim, const struct wimlib_wim_info *info, int which)
325 if (which & WIMLIB_CHANGE_READONLY_FLAG) {
326 if (info->is_marked_readonly)
327 wim->hdr.flags |= WIM_HDR_FLAG_READONLY;
329 wim->hdr.flags &= ~WIM_HDR_FLAG_READONLY;
332 if ((which & ~WIMLIB_CHANGE_READONLY_FLAG) == 0)
335 ret = can_modify_wim(wim);
339 if (which & WIMLIB_CHANGE_GUID)
340 memcpy(wim->hdr.guid, info->guid, WIM_GID_LEN);
342 if (which & WIMLIB_CHANGE_BOOT_INDEX) {
343 if (info->boot_index > wim->hdr.image_count) {
344 ERROR("%u is not 0 or a valid image in the WIM to mark as bootable",
346 return WIMLIB_ERR_INVALID_IMAGE;
348 wim->hdr.boot_idx = info->boot_index;
351 if (which & WIMLIB_CHANGE_RPFIX_FLAG) {
353 wim->hdr.flags |= WIM_HDR_FLAG_RP_FIX;
355 wim->hdr.flags &= ~WIM_HDR_FLAG_RP_FIX;
361 do_open_wim(const tchar *filename, struct filedes *fd_ret)
365 raw_fd = topen(filename, O_RDONLY | O_BINARY);
367 ERROR_WITH_ERRNO("Can't open \"%"TS"\" read-only", filename);
368 return WIMLIB_ERR_OPEN;
370 filedes_init(fd_ret, raw_fd);
375 reopen_wim(WIMStruct *wim)
377 wimlib_assert(!filedes_valid(&wim->in_fd));
378 return do_open_wim(wim->filename, &wim->in_fd);
382 close_wim(WIMStruct *wim)
384 if (filedes_valid(&wim->in_fd)) {
385 filedes_close(&wim->in_fd);
386 filedes_invalidate(&wim->in_fd);
392 * Begins the reading of a WIM file; opens the file and reads its header and
393 * lookup table, and optionally checks the integrity.
396 begin_read(WIMStruct *wim, const void *wim_filename_or_fd,
397 int open_flags, wimlib_progress_func_t progress_func)
401 const tchar *wimfile;
403 if (open_flags & WIMLIB_OPEN_FLAG_FROM_PIPE) {
405 filedes_init(&wim->in_fd, *(const int*)wim_filename_or_fd);
406 wim->in_fd.is_pipe = 1;
408 wimfile = wim_filename_or_fd;
409 DEBUG("Reading the WIM file `%"TS"'", wimfile);
410 ret = do_open_wim(wimfile, &wim->in_fd);
414 /* The absolute path to the WIM is requested so that
415 * wimlib_overwrite() still works even if the process changes
416 * its working directory. This actually happens if a WIM is
417 * mounted read-write, since the FUSE thread changes directory
418 * to "/", and it needs to be able to find the WIM file again.
420 * This will break if the full path to the WIM changes in the
421 * intervening time...
423 * Warning: in Windows native builds, realpath() calls the
424 * replacement function in win32.c.
426 wim->filename = realpath(wimfile, NULL);
427 if (!wim->filename) {
428 ERROR_WITH_ERRNO("Failed to resolve WIM filename");
430 return WIMLIB_ERR_NOMEM;
432 return WIMLIB_ERR_OPEN;
436 ret = read_wim_header(wim->filename, &wim->in_fd, &wim->hdr);
440 if (wim->hdr.flags & WIM_HDR_FLAG_WRITE_IN_PROGRESS) {
441 WARNING("The WIM_HDR_FLAG_WRITE_IN_PROGRESS is set in the header of \"%"TS"\".\n"
442 " It may be being changed by another process, or a process\n"
443 " may have crashed while writing the WIM.", wimfile);
446 if (open_flags & WIMLIB_OPEN_FLAG_WRITE_ACCESS) {
447 ret = can_modify_wim(wim);
452 if (wim->hdr.total_parts != 1 && !(open_flags & WIMLIB_OPEN_FLAG_SPLIT_OK)) {
453 ERROR("\"%"TS"\": This WIM is part %u of a %u-part WIM",
454 wimfile, wim->hdr.part_number, wim->hdr.total_parts);
455 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
458 DEBUG("According to header, WIM contains %u images", wim->hdr.image_count);
460 /* If the boot index is invalid, print a warning and set it to 0 */
461 if (wim->hdr.boot_idx > wim->hdr.image_count) {
462 WARNING("In `%"TS"', image %u is marked as bootable, "
463 "but there are only %u images in the WIM",
464 wimfile, wim->hdr.boot_idx, wim->hdr.image_count);
465 wim->hdr.boot_idx = 0;
468 /* Check and cache the compression type */
469 if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESSION) {
470 if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESS_LZX) {
471 if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESS_XPRESS) {
472 ERROR("Multiple compression flags are set in \"%"TS"\"",
474 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
476 wim->compression_type = WIMLIB_COMPRESSION_TYPE_LZX;
477 } else if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESS_XPRESS) {
478 wim->compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
480 ERROR("The compression flag is set on \"%"TS"\", but "
481 "neither the XPRESS nor LZX flag is set",
483 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
486 wim->compression_type = WIMLIB_COMPRESSION_TYPE_NONE;
489 if (open_flags & WIMLIB_OPEN_FLAG_CHECK_INTEGRITY) {
490 ret = check_wim_integrity(wim, progress_func);
491 if (ret == WIM_INTEGRITY_NONEXISTENT) {
492 WARNING("No integrity information for `%"TS"'; skipping "
493 "integrity check.", wimfile);
494 } else if (ret == WIM_INTEGRITY_NOT_OK) {
495 ERROR("WIM is not intact! (Failed integrity check)");
496 return WIMLIB_ERR_INTEGRITY;
497 } else if (ret != WIM_INTEGRITY_OK) {
502 if (wim->hdr.image_count != 0 && wim->hdr.part_number == 1) {
503 wim->image_metadata = new_image_metadata_array(wim->hdr.image_count);
504 if (!wim->image_metadata)
505 return WIMLIB_ERR_NOMEM;
508 if (open_flags & WIMLIB_OPEN_FLAG_FROM_PIPE) {
509 wim->lookup_table = new_lookup_table(9001);
510 if (!wim->lookup_table)
511 return WIMLIB_ERR_NOMEM;
513 ret = read_wim_lookup_table(wim);
517 ret = read_wim_xml_data(wim);
521 xml_num_images = wim_info_get_num_images(wim->wim_info);
522 if (xml_num_images != wim->hdr.image_count) {
523 ERROR("In the file `%"TS"', there are %u <IMAGE> elements "
524 "in the XML data,", wimfile, xml_num_images);
525 ERROR("but %u images in the WIM! There must be exactly one "
526 "<IMAGE> element per image.", wim->hdr.image_count);
527 return WIMLIB_ERR_IMAGE_COUNT;
529 DEBUG("Done beginning read of WIM file `%"TS"'.", wimfile);
535 open_wim_as_WIMStruct(const void *wim_filename_or_fd, int open_flags,
536 WIMStruct **wim_ret, wimlib_progress_func_t progress_func)
541 wimlib_global_init(WIMLIB_INIT_FLAG_ASSUME_UTF8);
543 ret = WIMLIB_ERR_INVALID_PARAM;
547 ret = WIMLIB_ERR_NOMEM;
548 wim = new_wim_struct();
552 ret = begin_read(wim, wim_filename_or_fd, open_flags, progress_func);
554 goto out_wimlib_free;
565 /* API function documented in wimlib.h */
567 wimlib_open_wim(const tchar *wimfile, int open_flags,
568 WIMStruct **wim_ret, wimlib_progress_func_t progress_func)
570 open_flags &= WIMLIB_OPEN_MASK_PUBLIC;
571 return open_wim_as_WIMStruct(wimfile, open_flags, wim_ret,
576 destroy_image_metadata(struct wim_image_metadata *imd,
577 struct wim_lookup_table *table,
578 bool free_metadata_lte)
580 free_dentry_tree(imd->root_dentry, table);
581 imd->root_dentry = NULL;
582 free_wim_security_data(imd->security_data);
583 imd->security_data = NULL;
585 if (free_metadata_lte) {
586 free_lookup_table_entry(imd->metadata_lte);
587 imd->metadata_lte = NULL;
590 struct wim_lookup_table_entry *lte, *tmp;
591 list_for_each_entry_safe(lte, tmp, &imd->unhashed_streams, unhashed_list)
592 free_lookup_table_entry(lte);
594 INIT_LIST_HEAD(&imd->unhashed_streams);
595 INIT_LIST_HEAD(&imd->inode_list);
598 do_ntfs_umount(imd->ntfs_vol);
599 imd->ntfs_vol = NULL;
605 put_image_metadata(struct wim_image_metadata *imd,
606 struct wim_lookup_table *table)
608 if (imd && --imd->refcnt == 0) {
609 destroy_image_metadata(imd, table, true);
614 /* Appends the specified image metadata structure to the array of image metadata
615 * for a WIM, and increments the image count. */
617 append_image_metadata(WIMStruct *wim, struct wim_image_metadata *imd)
619 struct wim_image_metadata **imd_array;
621 DEBUG("Reallocating image metadata array for image_count = %u",
622 wim->hdr.image_count + 1);
623 imd_array = REALLOC(wim->image_metadata,
624 sizeof(wim->image_metadata[0]) * (wim->hdr.image_count + 1));
627 return WIMLIB_ERR_NOMEM;
628 wim->image_metadata = imd_array;
629 imd_array[wim->hdr.image_count++] = imd;
634 struct wim_image_metadata *
635 new_image_metadata(void)
637 struct wim_image_metadata *imd;
639 imd = CALLOC(1, sizeof(*imd));
642 INIT_LIST_HEAD(&imd->inode_list);
643 INIT_LIST_HEAD(&imd->unhashed_streams);
644 DEBUG("Created new image metadata (refcnt=1)");
646 ERROR_WITH_ERRNO("Failed to allocate new image metadata structure");
651 struct wim_image_metadata **
652 new_image_metadata_array(unsigned num_images)
654 struct wim_image_metadata **imd_array;
656 DEBUG("Creating new image metadata array for %u images",
659 imd_array = CALLOC(num_images, sizeof(imd_array[0]));
662 ERROR("Failed to allocate memory for %u image metadata structures",
666 for (unsigned i = 0; i < num_images; i++) {
667 imd_array[i] = new_image_metadata();
669 for (unsigned j = 0; j < i; j++)
670 put_image_metadata(imd_array[j], NULL);
678 /* Checksum all streams that are unhashed (other than the metadata streams),
679 * merging them into the lookup table as needed. This is a no-op unless the
680 * library has previously used to add or mount an image using the same
683 wim_checksum_unhashed_streams(WIMStruct *wim)
686 for (int i = 0; i < wim->hdr.image_count; i++) {
687 struct wim_lookup_table_entry *lte, *tmp;
688 struct wim_image_metadata *imd = wim->image_metadata[i];
689 image_for_each_unhashed_stream_safe(lte, tmp, imd) {
690 ret = hash_unhashed_stream(lte, wim->lookup_table, NULL);
699 * can_modify_wim - Check if a given WIM is writeable. This is only the case if
700 * it meets the following three conditions:
702 * 1. Write access is allowed to the underlying file (if any) at the filesystem level.
703 * 2. The WIM is not part of a spanned set.
704 * 3. The WIM_HDR_FLAG_READONLY flag is not set in the WIM header.
706 * Return value is 0 if writable; WIMLIB_ERR_WIM_IS_READONLY otherwise.
709 can_modify_wim(WIMStruct *wim)
712 if (taccess(wim->filename, W_OK)) {
713 ERROR_WITH_ERRNO("Can't modify \"%"TS"\"", wim->filename);
714 return WIMLIB_ERR_WIM_IS_READONLY;
717 if (wim->hdr.total_parts != 1) {
718 ERROR("Cannot modify \"%"TS"\": is part of a spanned set",
720 return WIMLIB_ERR_WIM_IS_READONLY;
722 if (wim->hdr.flags & WIM_HDR_FLAG_READONLY) {
723 ERROR("Cannot modify \"%"TS"\": is marked read-only",
725 return WIMLIB_ERR_WIM_IS_READONLY;
731 * can_delete_from_wim - Check if files or images can be deleted from a given
734 * This theoretically should be exactly the same as can_modify_wim(), but
735 * unfortunately, due to bugs in Microsoft's software that generate incorrect
736 * reference counts for some WIM resources, we need to run expensive
737 * verifications to make sure the reference counts are correct on all WIM
738 * resources. Otherwise we might delete a WIM resource whose reference count
739 * has fallen to 0, but is actually still referenced somewhere.
742 can_delete_from_wim(WIMStruct *wim)
746 ret = can_modify_wim(wim);
749 if (!wim->refcnts_ok)
750 wim_recalculate_refcnts(wim);
754 /* API function documented in wimlib.h */
756 wimlib_free(WIMStruct *wim)
758 DEBUG("Freeing WIMStruct");
762 if (filedes_valid(&wim->in_fd))
763 filedes_close(&wim->in_fd);
764 if (filedes_valid(&wim->out_fd))
765 filedes_close(&wim->out_fd);
767 free_lookup_table(wim->lookup_table);
770 free_wim_info(wim->wim_info);
771 if (wim->image_metadata) {
772 for (unsigned i = 0; i < wim->hdr.image_count; i++)
773 put_image_metadata(wim->image_metadata[i], NULL);
774 FREE(wim->image_metadata);
777 DEBUG("Freed WIMStruct");
781 test_locale_ctype_utf8(void)
786 char *ctype = nl_langinfo(CODESET);
788 return (!strstr(ctype, "UTF-8") ||
789 !strstr(ctype, "UTF8") ||
790 !strstr(ctype, "utf8") ||
791 !strstr(ctype, "utf-8"));
795 /* API function documented in wimlib.h */
797 wimlib_global_init(int init_flags)
799 static bool already_inited = false;
803 libxml_global_init();
804 if (!(init_flags & WIMLIB_INIT_FLAG_ASSUME_UTF8)) {
805 wimlib_mbs_is_utf8 = test_locale_ctype_utf8();
807 if (!wimlib_mbs_is_utf8)
808 libntfs3g_global_init();
812 win32_global_init(init_flags);
814 already_inited = true;
818 /* API function documented in wimlib.h */
820 wimlib_global_cleanup(void)
822 libxml_global_cleanup();
823 iconv_global_cleanup();
825 win32_global_cleanup();