2 * wim.c - Stuff that doesn't fit into any other file
6 * Copyright (C) 2012, 2013 Eric Biggers
8 * wimlib - Library for working with WIM files
10 * This file is part of wimlib, a library for working with WIM files.
12 * wimlib is free software; you can redistribute it and/or modify it under the
13 * terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 3 of the License, or (at your option)
17 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
18 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19 * A PARTICULAR PURPOSE. See the GNU General Public License for more
22 * You should have received a copy of the GNU General Public License
23 * along with wimlib; if not, see http://www.gnu.org/licenses/.
46 #include <ntfs-3g/volume.h>
49 #include "wimlib_internal.h"
50 #include "buffer_io.h"
51 #include "lookup_table.h"
55 static char *realpath(const char *path, char *resolved_path)
58 wimlib_assert(resolved_path == NULL);
60 ret = GetFullPathNameA(path, 0, NULL, NULL);
64 resolved_path = MALLOC(ret + 1);
67 ret = GetFullPathNameA(path, ret, resolved_path, NULL);
74 win32_error(GetLastError());
80 static int image_print_metadata(WIMStruct *w)
82 DEBUG("Printing metadata for image %d", w->current_image);
83 print_security_data(wim_security_data(w));
84 return for_dentry_in_tree(wim_root_dentry(w), print_dentry,
89 static int image_print_files(WIMStruct *w)
91 return for_dentry_in_tree(wim_root_dentry(w), print_dentry_full_path,
95 static WIMStruct *new_wim_struct()
97 WIMStruct *w = CALLOC(1, sizeof(WIMStruct));
99 if (pthread_mutex_init(&w->fp_tab_mutex, NULL) != 0) {
100 ERROR_WITH_ERRNO("Failed to initialize mutex");
110 * Calls a function on images in the WIM. If @image is WIMLIB_ALL_IMAGES, @visitor
111 * is called on the WIM once for each image, with each image selected as the
112 * current image in turn. If @image is a certain image, @visitor is called on
113 * the WIM only once, with that image selected.
115 int for_image(WIMStruct *w, int image, int (*visitor)(WIMStruct *))
122 if (image == WIMLIB_ALL_IMAGES) {
124 end = w->hdr.image_count;
125 } else if (image >= 1 && image <= w->hdr.image_count) {
129 return WIMLIB_ERR_INVALID_IMAGE;
131 for (i = start; i <= end; i++) {
132 ret = select_wim_image(w, i);
142 static int sort_image_metadata_by_position(const void *p1, const void *p2)
144 const struct wim_image_metadata *imd1 = p1;
145 const struct wim_image_metadata *imd2 = p2;
146 u64 offset1 = imd1->metadata_lte->resource_entry.offset;
147 u64 offset2 = imd2->metadata_lte->resource_entry.offset;
148 if (offset1 < offset2)
150 else if (offset1 > offset2)
157 * If @lte points to a metadata resource, append it to the list of metadata
158 * resources in the WIMStruct. Otherwise, do nothing.
160 static int append_metadata_resource_entry(struct wim_lookup_table_entry *lte,
163 WIMStruct *w = wim_p;
166 if (lte->resource_entry.flags & WIM_RESHDR_FLAG_METADATA) {
167 if (w->current_image == w->hdr.image_count) {
168 ERROR("The WIM header says there are %u images in the WIM,\n"
169 " but we found more metadata resources than this",
171 ret = WIMLIB_ERR_IMAGE_COUNT;
173 DEBUG("Found metadata resource for image %u at "
175 w->current_image + 1,
176 lte->resource_entry.offset);
178 w->current_image++].metadata_lte = lte;
184 /* Returns the compression type given in the flags of a WIM header. */
185 static int wim_hdr_flags_compression_type(int wim_hdr_flags)
187 if (wim_hdr_flags & WIM_HDR_FLAG_COMPRESSION) {
188 if (wim_hdr_flags & WIM_HDR_FLAG_COMPRESS_LZX)
189 return WIMLIB_COMPRESSION_TYPE_LZX;
190 else if (wim_hdr_flags & WIM_HDR_FLAG_COMPRESS_XPRESS)
191 return WIMLIB_COMPRESSION_TYPE_XPRESS;
193 return WIMLIB_COMPRESSION_TYPE_INVALID;
195 return WIMLIB_COMPRESSION_TYPE_NONE;
200 * Creates a WIMStruct for a new WIM file.
202 WIMLIBAPI int wimlib_create_new_wim(int ctype, WIMStruct **w_ret)
205 struct wim_lookup_table *table;
208 DEBUG("Creating new WIM with %s compression.",
209 wimlib_get_compression_type_string(ctype));
211 /* Allocate the WIMStruct. */
212 w = new_wim_struct();
214 return WIMLIB_ERR_NOMEM;
216 ret = init_header(&w->hdr, ctype);
220 table = new_lookup_table(9001);
222 ret = WIMLIB_ERR_NOMEM;
225 w->lookup_table = table;
233 WIMLIBAPI int wimlib_get_num_images(const WIMStruct *w)
235 return w->hdr.image_count;
238 int select_wim_image(WIMStruct *w, int image)
240 struct wim_image_metadata *imd;
243 DEBUG("Selecting image %d", image);
245 if (image == WIMLIB_NO_IMAGE) {
246 ERROR("Invalid image: %d", WIMLIB_NO_IMAGE);
247 return WIMLIB_ERR_INVALID_IMAGE;
250 if (image == w->current_image)
253 if (image < 1 || image > w->hdr.image_count) {
254 ERROR("Cannot select image %d: There are only %u images",
255 image, w->hdr.image_count);
256 return WIMLIB_ERR_INVALID_IMAGE;
259 /* If a valid image is currently selected, it can be freed if it is not
261 if (w->current_image != WIMLIB_NO_IMAGE) {
262 imd = wim_get_current_image_metadata(w);
263 if (!imd->modified) {
264 DEBUG("Freeing image %u", w->current_image);
265 destroy_image_metadata(imd, NULL);
266 imd->root_dentry = NULL;
267 imd->security_data = NULL;
268 INIT_HLIST_HEAD(&imd->inode_list);
271 w->current_image = image;
272 imd = &w->image_metadata[image - 1];
273 if (imd->root_dentry) {
277 DEBUG("Reading metadata resource specified by the following "
278 "lookup table entry:");
279 print_lookup_table_entry(imd->metadata_lte, stdout);
281 ret = read_metadata_resource(w, imd);
283 w->current_image = WIMLIB_NO_IMAGE;
289 /* Returns the compression type of the WIM file. */
290 WIMLIBAPI int wimlib_get_compression_type(const WIMStruct *w)
292 return wim_hdr_flags_compression_type(w->hdr.flags);
295 WIMLIBAPI const char *wimlib_get_compression_type_string(int ctype)
298 case WIMLIB_COMPRESSION_TYPE_NONE:
300 case WIMLIB_COMPRESSION_TYPE_LZX:
302 case WIMLIB_COMPRESSION_TYPE_XPRESS:
310 * Returns the number of an image in the WIM file, given a string that is either
311 * the number of the image, or the name of the image. The images are numbered
314 WIMLIBAPI int wimlib_resolve_image(WIMStruct *w, const char *image_name_or_num)
320 if (!image_name_or_num || !*image_name_or_num)
321 return WIMLIB_NO_IMAGE;
323 if (strcmp(image_name_or_num, "all") == 0
324 || strcmp(image_name_or_num, "*") == 0)
325 return WIMLIB_ALL_IMAGES;
326 image = strtol(image_name_or_num, &p, 10);
327 if (p != image_name_or_num && *p == '\0' && image > 0) {
328 if (image > w->hdr.image_count)
329 return WIMLIB_NO_IMAGE;
332 for (i = 1; i <= w->hdr.image_count; i++) {
333 if (strcmp(image_name_or_num,
334 wimlib_get_image_name(w, i)) == 0)
337 return WIMLIB_NO_IMAGE;
342 /* Prints some basic information about a WIM file. */
343 WIMLIBAPI void wimlib_print_wim_information(const WIMStruct *w)
345 const struct wim_header *hdr;
348 puts("WIM Information:");
349 puts("----------------");
350 printf("Path: %s\n", w->filename);
351 fputs ("GUID: 0x", stdout);
352 print_byte_field(hdr->guid, WIM_GID_LEN);
354 printf("Image Count: %d\n", hdr->image_count);
355 printf("Compression: %s\n", wimlib_get_compression_type_string(
356 wimlib_get_compression_type(w)));
357 printf("Part Number: %d/%d\n", hdr->part_number, hdr->total_parts);
358 printf("Boot Index: %d\n", hdr->boot_idx);
359 printf("Size: %"PRIu64" bytes\n",
360 wim_info_get_total_bytes(w->wim_info));
361 printf("Integrity Info: %s\n", (w->hdr.integrity.offset != 0) ? "yes" : "no");
365 WIMLIBAPI bool wimlib_has_integrity_table(const WIMStruct *w)
367 return w->hdr.integrity.size != 0;
370 WIMLIBAPI void wimlib_print_available_images(const WIMStruct *w, int image)
376 if (image == WIMLIB_ALL_IMAGES) {
377 n = printf("Available Images:\n");
379 last = w->hdr.image_count;
380 } else if (image >= 1 && image <= w->hdr.image_count) {
381 n = printf("Information for Image %d\n", image);
385 printf("wimlib_print_available_images(): Invalid image %d",
389 for (i = 0; i < n - 1; i++)
392 for (i = first; i <= last; i++)
393 print_image_info(w->wim_info, i);
397 /* Prints the metadata for the specified image, which may be WIMLIB_ALL_IMAGES, but
398 * not WIMLIB_NO_IMAGE. */
399 WIMLIBAPI int wimlib_print_metadata(WIMStruct *w, int image)
401 if (w->hdr.part_number != 1) {
402 ERROR("Cannot show the metadata from part %hu of a %hu-part split WIM!",
403 w->hdr.part_number, w->hdr.total_parts);
404 ERROR("Select the first part of the split WIM to see the metadata.");
405 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
407 return for_image(w, image, image_print_metadata);
410 WIMLIBAPI int wimlib_print_files(WIMStruct *w, int image)
412 if (w->hdr.part_number != 1) {
413 ERROR("Cannot list the files from part %hu of a %hu-part split WIM!",
414 w->hdr.part_number, w->hdr.total_parts);
415 ERROR("Select the first part of the split WIM if you'd like to list the files.");
416 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
418 return for_image(w, image, image_print_files);
421 /* Sets the index of the bootable image. */
422 WIMLIBAPI int wimlib_set_boot_idx(WIMStruct *w, int boot_idx)
424 if (w->hdr.total_parts != 1) {
425 ERROR("Cannot modify the boot index of a split WIM!");
426 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
428 if (boot_idx < 0 || boot_idx > w->hdr.image_count)
429 return WIMLIB_ERR_INVALID_IMAGE;
430 w->hdr.boot_idx = boot_idx;
433 memset(&w->hdr.boot_metadata_res_entry, 0,
434 sizeof(struct resource_entry));
436 memcpy(&w->hdr.boot_metadata_res_entry,
438 boot_idx - 1].metadata_lte->resource_entry,
439 sizeof(struct resource_entry));
445 WIMLIBAPI int wimlib_get_part_number(const WIMStruct *w, int *total_parts_ret)
448 *total_parts_ret = w->hdr.total_parts;
449 return w->hdr.part_number;
453 WIMLIBAPI int wimlib_get_boot_idx(const WIMStruct *w)
455 return w->hdr.boot_idx;
459 * Begins the reading of a WIM file; opens the file and reads its header and
460 * lookup table, and optionally checks the integrity.
462 static int begin_read(WIMStruct *w, const char *in_wim_path, int open_flags,
463 wimlib_progress_func_t progress_func)
468 DEBUG("Reading the WIM file `%s'", in_wim_path);
470 w->fp = fopen(in_wim_path, "rb");
472 ERROR_WITH_ERRNO("Failed to open `%s' for reading",
474 return WIMLIB_ERR_OPEN;
477 /* The absolute path to the WIM is requested so that wimlib_overwrite()
478 * still works even if the process changes its working directory. This
479 * actually happens if a WIM is mounted read-write, since the FUSE
480 * thread changes directory to "/", and it needs to be able to find the
483 * This will break if the full path to the WIM changes in the
484 * intervening time...
486 w->filename = realpath(in_wim_path, NULL);
488 ERROR_WITH_ERRNO("Failed to resolve WIM filename");
490 return WIMLIB_ERR_NOMEM;
492 return WIMLIB_ERR_OPEN;
495 ret = read_header(w->fp, &w->hdr, open_flags);
499 DEBUG("According to header, WIM contains %u images", w->hdr.image_count);
501 /* If the boot index is invalid, print a warning and set it to 0 */
502 if (w->hdr.boot_idx > w->hdr.image_count) {
503 WARNING("In `%s', image %u is marked as bootable, "
504 "but there are only %u images in the WIM",
505 in_wim_path, w->hdr.boot_idx, w->hdr.image_count);
509 if (wimlib_get_compression_type(w) == WIMLIB_COMPRESSION_TYPE_INVALID) {
510 ERROR("Invalid compression type (WIM header flags = 0x%x)",
512 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
515 if (open_flags & WIMLIB_OPEN_FLAG_CHECK_INTEGRITY) {
516 ret = check_wim_integrity(w, progress_func);
517 if (ret == WIM_INTEGRITY_NONEXISTENT) {
518 WARNING("No integrity information for `%s'; skipping "
519 "integrity check.", in_wim_path);
520 } else if (ret == WIM_INTEGRITY_NOT_OK) {
521 ERROR("WIM is not intact! (Failed integrity check)");
522 return WIMLIB_ERR_INTEGRITY;
523 } else if (ret != WIM_INTEGRITY_OK) {
528 ret = read_lookup_table(w);
532 if (w->hdr.image_count != 0) {
533 w->image_metadata = CALLOC(w->hdr.image_count,
534 sizeof(struct wim_image_metadata));
536 if (!w->image_metadata) {
537 ERROR("Failed to allocate memory for %u image metadata structures",
539 return WIMLIB_ERR_NOMEM;
542 w->current_image = 0;
544 DEBUG("Looking for metadata resources in the lookup table.");
546 /* Find the images in the WIM by searching the lookup table. */
547 ret = for_lookup_table_entry(w->lookup_table,
548 append_metadata_resource_entry, w);
553 /* Make sure all the expected images were found. (We already have
554 * returned WIMLIB_ERR_IMAGE_COUNT if *extra* images were found) */
555 if (w->current_image != w->hdr.image_count &&
556 w->hdr.part_number == 1)
558 ERROR("Only found %d images in WIM, but expected %u",
559 w->current_image, w->hdr.image_count);
560 return WIMLIB_ERR_IMAGE_COUNT;
563 /* Sort images by the position of their metadata resources. I'm
564 * assuming that is what determines the other of the images in the WIM
565 * file, rather than their order in the lookup table, which is random
566 * because of hashing. */
567 qsort(w->image_metadata, w->current_image,
568 sizeof(struct wim_image_metadata), sort_image_metadata_by_position);
570 w->current_image = WIMLIB_NO_IMAGE;
572 /* Read the XML data. */
573 ret = read_xml_data(w->fp, &w->hdr.xml_res_entry,
574 &w->xml_data, &w->wim_info);
579 xml_num_images = wim_info_get_num_images(w->wim_info);
580 if (xml_num_images != w->hdr.image_count) {
581 ERROR("In the file `%s', there are %u <IMAGE> elements "
582 "in the XML data,", in_wim_path, xml_num_images);
583 ERROR("but %u images in the WIM! There must be exactly one "
584 "<IMAGE> element per image.", w->hdr.image_count);
585 return WIMLIB_ERR_IMAGE_COUNT;
588 DEBUG("Done beginning read of WIM file `%s'.", in_wim_path);
594 * Opens a WIM file and creates a WIMStruct for it.
596 WIMLIBAPI int wimlib_open_wim(const char *wim_file, int open_flags,
598 wimlib_progress_func_t progress_func)
603 if (!wim_file || !w_ret)
604 return WIMLIB_ERR_INVALID_PARAM;
606 w = new_wim_struct();
608 return WIMLIB_ERR_NOMEM;
610 ret = begin_read(w, wim_file, open_flags, progress_func);
618 void destroy_image_metadata(struct wim_image_metadata *imd,
619 struct wim_lookup_table *table)
621 free_dentry_tree(imd->root_dentry, table);
622 free_security_data(imd->security_data);
624 /* Get rid of the lookup table entry for this image's metadata resource
627 lookup_table_unlink(table, imd->metadata_lte);
628 free_lookup_table_entry(imd->metadata_lte);
632 /* Frees the memory for the WIMStruct, including all internal memory; also
633 * closes all files associated with the WIMStruct. */
634 WIMLIBAPI void wimlib_free(WIMStruct *w)
636 DEBUG("Freeing WIMStruct");
647 for (size_t i = 0; i < w->num_allocated_fps; i++)
649 fclose(w->fp_tab[i]);
652 pthread_mutex_destroy(&w->fp_tab_mutex);
655 free_lookup_table(w->lookup_table);
659 free_wim_info(w->wim_info);
660 if (w->image_metadata) {
661 for (unsigned i = 0; i < w->hdr.image_count; i++)
662 destroy_image_metadata(&w->image_metadata[i], NULL);
663 FREE(w->image_metadata);
667 DEBUG("Unmounting NTFS volume");
668 ntfs_umount(w->ntfs_vol, FALSE);
672 DEBUG("Freed WIMStruct");
675 /* Get global memory allocations out of the way. Not strictly necessary in
676 * single-threaded programs like 'imagex'. */
677 WIMLIBAPI int wimlib_global_init()
679 libxml_global_init();
680 return iconv_global_init();
683 /* Free global memory allocations. Not strictly necessary if the process using
684 * wimlib is just about to exit (as is the case for 'imagex'). */
685 WIMLIBAPI void wimlib_global_cleanup()
687 libxml_global_cleanup();
688 iconv_global_cleanup();