6 * Copyright (C) 2010 Carl Thijssen
7 * Copyright (C) 2012 Eric Biggers
9 * wimlib - Library for working with WIM files
11 * This file is part of wimlib, a library for working with WIM files.
13 * wimlib is free software; you can redistribute it and/or modify it under the
14 * terms of the GNU General Public License as published by the Free
15 * Software Foundation; either version 3 of the License, or (at your option)
18 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
19 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
20 * A PARTICULAR PURPOSE. See the GNU General Public License for more
23 * You should have received a copy of the GNU General Public License
24 * along with wimlib; if not, see http://www.gnu.org/licenses/.
35 #include <ntfs-3g/volume.h>
38 #include "wimlib_internal.h"
40 #include "lookup_table.h"
44 static int print_metadata(WIMStruct *w)
46 DEBUG("Printing metadata for image %d", w->current_image);
47 print_security_data(wim_security_data(w));
48 return for_dentry_in_tree(wim_root_dentry(w), print_dentry,
53 static int print_files(WIMStruct *w)
55 return for_dentry_in_tree(wim_root_dentry(w), print_dentry_full_path,
59 WIMStruct *new_wim_struct()
61 WIMStruct *w = CALLOC(1, sizeof(WIMStruct));
62 if (pthread_mutex_init(&w->fp_tab_mutex, NULL) != 0) {
63 ERROR_WITH_ERRNO("Failed to initialize mutex");
72 * Calls a function on images in the WIM. If @image is WIM_ALL_IMAGES, @visitor
73 * is called on the WIM once for each image, with each image selected as the
74 * current image in turn. If @image is a certain image, @visitor is called on
75 * the WIM only once, with that image selected.
77 int for_image(WIMStruct *w, int image, int (*visitor)(WIMStruct *))
84 if (image == WIM_ALL_IMAGES) {
86 end = w->hdr.image_count;
87 } else if (image >= 1 && image <= w->hdr.image_count) {
91 return WIMLIB_ERR_INVALID_IMAGE;
93 for (i = start; i <= end; i++) {
94 ret = select_wim_image(w, i);
104 static int sort_image_metadata_by_position(const void *p1, const void *p2)
106 const struct image_metadata *imd1 = p1;
107 const struct image_metadata *imd2 = p2;
108 u64 offset1 = imd1->metadata_lte->resource_entry.offset;
109 u64 offset2 = imd2->metadata_lte->resource_entry.offset;
110 if (offset1 < offset2)
112 else if (offset1 > offset2)
119 * If @lte points to a metadata resource, append it to the list of metadata
120 * resources in the WIMStruct. Otherwise, do nothing.
122 static int append_metadata_resource_entry(struct lookup_table_entry *lte,
125 WIMStruct *w = wim_p;
128 if (lte->resource_entry.flags & WIM_RESHDR_FLAG_METADATA) {
129 if (w->current_image == w->hdr.image_count) {
130 ERROR("Expected only %u images, but found more",
132 ret = WIMLIB_ERR_IMAGE_COUNT;
134 DEBUG("Found metadata resource for image %u at "
136 w->current_image + 1,
137 lte->resource_entry.offset);
139 w->current_image++].metadata_lte = lte;
145 /* Returns the compression type given in the flags of a WIM header. */
146 int wim_hdr_flags_compression_type(int wim_hdr_flags)
148 if (wim_hdr_flags & WIM_HDR_FLAG_COMPRESSION) {
149 if (wim_hdr_flags & WIM_HDR_FLAG_COMPRESS_LZX)
150 return WIM_COMPRESSION_TYPE_LZX;
151 else if (wim_hdr_flags & WIM_HDR_FLAG_COMPRESS_XPRESS)
152 return WIM_COMPRESSION_TYPE_XPRESS;
154 return WIM_COMPRESSION_TYPE_INVALID;
156 return WIM_COMPRESSION_TYPE_NONE;
161 * Creates a WIMStruct for a new WIM file.
163 WIMLIBAPI int wimlib_create_new_wim(int ctype, WIMStruct **w_ret)
166 struct lookup_table *table;
169 DEBUG("Creating new WIM with %s compression.",
170 wimlib_get_compression_type_string(ctype));
172 /* Allocate the WIMStruct. */
173 w = new_wim_struct();
175 return WIMLIB_ERR_NOMEM;
177 ret = init_header(&w->hdr, ctype);
181 table = new_lookup_table(9001);
183 ret = WIMLIB_ERR_NOMEM;
186 w->lookup_table = table;
194 WIMLIBAPI int wimlib_get_num_images(const WIMStruct *w)
196 return w->hdr.image_count;
199 int select_wim_image(WIMStruct *w, int image)
201 struct image_metadata *imd;
203 DEBUG("Selecting image %d", image);
205 if (image == w->current_image)
208 if (image < 1 || image > w->hdr.image_count) {
209 ERROR("Cannot select image %d: There are only %u images",
210 image, w->hdr.image_count);
211 return WIMLIB_ERR_INVALID_IMAGE;
215 /* If a valid image is currently selected, it can be freed if it is not
217 if (w->current_image != WIM_NO_IMAGE) {
218 imd = wim_get_current_image_metadata(w);
219 if (!imd->modified) {
220 DEBUG("Freeing image %u", w->current_image);
221 destroy_image_metadata(imd, NULL);
222 imd->root_dentry = NULL;
223 imd->security_data = NULL;
227 w->current_image = image;
228 imd = wim_get_current_image_metadata(w);
230 if (imd->root_dentry) {
234 DEBUG("Reading metadata resource specified by the following "
235 "lookup table entry:");
236 print_lookup_table_entry(imd->metadata_lte);
238 return read_metadata_resource(w, imd);
243 /* Returns the compression type of the WIM file. */
244 WIMLIBAPI int wimlib_get_compression_type(const WIMStruct *w)
246 return wim_hdr_flags_compression_type(w->hdr.flags);
249 WIMLIBAPI const char *wimlib_get_compression_type_string(int ctype)
252 case WIM_COMPRESSION_TYPE_NONE:
254 case WIM_COMPRESSION_TYPE_LZX:
256 case WIM_COMPRESSION_TYPE_XPRESS:
264 * Returns the number of an image in the WIM file, given a string that is either
265 * the number of the image, or the name of the image. The images are numbered
268 WIMLIBAPI int wimlib_resolve_image(WIMStruct *w, const char *image_name_or_num)
274 if (!image_name_or_num)
277 if (strcmp(image_name_or_num, "all") == 0
278 || strcmp(image_name_or_num, "*") == 0)
279 return WIM_ALL_IMAGES;
280 image = strtol(image_name_or_num, &p, 10);
281 if (p != image_name_or_num && *p == '\0') {
282 if (image < 1 || image > w->hdr.image_count)
286 for (i = 1; i <= w->hdr.image_count; i++) {
287 if (strcmp(image_name_or_num,
288 wimlib_get_image_name(w, i)) == 0)
296 /* Prints some basic information about a WIM file. */
297 WIMLIBAPI void wimlib_print_wim_information(const WIMStruct *w)
299 const struct wim_header *hdr;
302 puts("WIM Information:");
303 puts("----------------");
304 printf("Path: %s\n", w->filename);
305 fputs ("GUID: 0x", stdout);
306 print_byte_field(hdr->guid, WIM_GID_LEN);
308 printf("Image Count: %d\n", hdr->image_count);
309 printf("Compression: %s\n", wimlib_get_compression_type_string(
310 wimlib_get_compression_type(w)));
311 printf("Part Number: %d/%d\n", hdr->part_number, hdr->total_parts);
312 printf("Boot Index: %d\n", hdr->boot_idx);
313 printf("Size: %"PRIu64" bytes\n",
314 wim_info_get_total_bytes(w->wim_info));
315 printf("Integrity Info: %s\n", (w->hdr.integrity.size != 0) ? "yes" : "no");
319 WIMLIBAPI bool wimlib_has_integrity_table(const WIMStruct *w)
321 return w->hdr.integrity.size != 0;
324 WIMLIBAPI void wimlib_print_available_images(const WIMStruct *w, int image)
330 if (image == WIM_ALL_IMAGES) {
331 n = printf("Available Images:\n");
333 last = w->hdr.image_count;
334 } else if (image >= 1 && image <= w->hdr.image_count) {
335 n = printf("Information for Image %d\n", image);
339 printf("wimlib_print_available_images(): Invalid image %d",
343 for (i = 0; i < n - 1; i++)
346 for (i = first; i <= last; i++)
347 print_image_info(w->wim_info, i);
351 /* Prints the metadata for the specified image, which may be WIM_ALL_IMAGES, but
352 * not WIM_NO_IMAGE. */
353 WIMLIBAPI int wimlib_print_metadata(WIMStruct *w, int image)
356 return WIMLIB_ERR_INVALID_PARAM;
357 if (w->hdr.part_number != 1) {
358 ERROR("Cannot show the metadata from part %hu of a %hu-part split WIM!",
359 w->hdr.part_number, w->hdr.total_parts);
360 ERROR("Select the first part of the split WIM to see the metadata.");
361 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
363 return for_image(w, image, print_metadata);
366 WIMLIBAPI int wimlib_print_files(WIMStruct *w, int image)
369 return WIMLIB_ERR_INVALID_PARAM;
370 if (w->hdr.part_number != 1) {
371 ERROR("Cannot list the files from part %hu of a %hu-part split WIM!",
372 w->hdr.part_number, w->hdr.total_parts);
373 ERROR("Select the first part of the split WIM if you'd like to list the files.");
374 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
376 return for_image(w, image, print_files);
379 /* Sets the index of the bootable image. */
380 WIMLIBAPI int wimlib_set_boot_idx(WIMStruct *w, int boot_idx)
383 return WIMLIB_ERR_INVALID_PARAM;
384 if (w->hdr.total_parts != 1) {
385 ERROR("Cannot modify the boot index of a split WIM!");
386 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
388 if (boot_idx < 0 || boot_idx > w->hdr.image_count)
389 return WIMLIB_ERR_INVALID_IMAGE;
390 w->hdr.boot_idx = boot_idx;
394 WIMLIBAPI int wimlib_get_part_number(const WIMStruct *w, int *total_parts_ret)
397 *total_parts_ret = w->hdr.total_parts;
398 return w->hdr.part_number;
402 WIMLIBAPI int wimlib_get_boot_idx(const WIMStruct *w)
404 return w->hdr.boot_idx;
408 * Begins the reading of a WIM file; opens the file and reads its header and
409 * lookup table, and optionally checks the integrity.
411 static int begin_read(WIMStruct *w, const char *in_wim_path, int open_flags)
416 DEBUG("Reading the WIM file `%s'", in_wim_path);
418 w->filename = realpath(in_wim_path, NULL);
420 ERROR("Failed to allocate memory for WIM filename");
421 return WIMLIB_ERR_NOMEM;
424 w->fp = fopen(in_wim_path, "rb");
427 ERROR_WITH_ERRNO("Failed to open the file `%s' for reading",
429 return WIMLIB_ERR_OPEN;
432 ret = read_header(w->fp, &w->hdr, open_flags);
436 DEBUG("Wim file contains %u images", w->hdr.image_count);
438 /* If the boot index is invalid, print a warning and set it to 0 */
439 if (w->hdr.boot_idx > w->hdr.image_count) {
440 WARNING("In `%s', image %u is marked as bootable, "
441 "but there are only %u images in the WIM",
442 in_wim_path, w->hdr.boot_idx, w->hdr.image_count);
446 if (wimlib_get_compression_type(w) == WIM_COMPRESSION_TYPE_INVALID) {
447 ERROR("Invalid compression type (WIM header flags = %x)",
449 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
453 if (open_flags & WIMLIB_OPEN_FLAG_CHECK_INTEGRITY) {
454 int integrity_status;
455 ret = check_wim_integrity(w,
456 open_flags & WIMLIB_OPEN_FLAG_SHOW_PROGRESS,
460 if (integrity_status == WIM_INTEGRITY_NONEXISTENT) {
461 WARNING("No integrity information for `%s'; skipping "
462 "integrity check.", w->filename);
463 } else if (integrity_status == WIM_INTEGRITY_NOT_OK) {
464 ERROR("WIM is not intact! (Failed integrity check)");
465 return WIMLIB_ERR_INTEGRITY;
469 if (resource_is_compressed(&w->hdr.lookup_table_res_entry)) {
470 ERROR("Didn't expect a compressed lookup table!");
471 ERROR("Ask the author to implement support for this.");
472 return WIMLIB_ERR_COMPRESSED_LOOKUP_TABLE;
475 ret = read_lookup_table(w);
479 w->image_metadata = CALLOC(w->hdr.image_count,
480 sizeof(struct image_metadata));
482 if (!w->image_metadata) {
483 ERROR("Failed to allocate memory for %u metadata structures",
485 return WIMLIB_ERR_NOMEM;
487 w->current_image = 0;
489 DEBUG("Looking for metadata resources in the lookup table.");
491 /* Find the images in the WIM by searching the lookup table. */
492 ret = for_lookup_table_entry(w->lookup_table,
493 append_metadata_resource_entry, w);
498 /* Make sure all the expected images were found. (We already have
499 * returned false if *extra* images were found) */
500 if (w->current_image != w->hdr.image_count &&
501 w->hdr.part_number == 1)
503 ERROR("Only found %u images in WIM, but expected %u",
504 w->current_image, w->hdr.image_count);
505 return WIMLIB_ERR_IMAGE_COUNT;
509 /* Sort images by the position of their metadata resources. I'm
510 * assuming that is what determines the other of the images in the WIM
511 * file, rather than their order in the lookup table, which is random
512 * because of hashing. */
513 qsort(w->image_metadata, w->current_image,
514 sizeof(struct image_metadata), sort_image_metadata_by_position);
516 w->current_image = WIM_NO_IMAGE;
518 /* Read the XML data. */
519 ret = read_xml_data(w->fp, &w->hdr.xml_res_entry,
520 &w->xml_data, &w->wim_info);
523 ERROR("Missing or invalid XML data");
527 xml_num_images = wim_info_get_num_images(w->wim_info);
528 if (xml_num_images != w->hdr.image_count) {
529 ERROR("In the file `%s', there are %u <IMAGE> elements "
530 "in the XML data,", in_wim_path, xml_num_images);
531 ERROR("but %u images in the WIM! There must be exactly one "
532 "<IMAGE> element per image.", w->hdr.image_count);
533 return WIMLIB_ERR_IMAGE_COUNT;
536 DEBUG("Done beginning read of WIM file `%s'.", in_wim_path);
542 * Opens a WIM file and creates a WIMStruct for it.
544 WIMLIBAPI int wimlib_open_wim(const char *wim_file, int open_flags,
550 DEBUG("wim_file = `%s', open_flags = %#x", wim_file, open_flags);
551 w = new_wim_struct();
553 ERROR("Failed to allocate memory for WIMStruct");
554 return WIMLIB_ERR_NOMEM;
557 ret = begin_read(w, wim_file, open_flags);
561 DEBUG("Could not begin reading the WIM file `%s'", wim_file);
567 /* Frees the memory for the WIMStruct, including all internal memory; also
568 * closes all files associated with the WIMStruct. */
569 WIMLIBAPI void wimlib_free(WIMStruct *w)
571 DEBUG2("Freeing WIMStruct");
581 for (size_t i = 0; i < w->num_allocated_fps; i++)
583 fclose(w->fp_tab[i]);
586 pthread_mutex_destroy(&w->fp_tab_mutex);
588 free_lookup_table(w->lookup_table);
592 free_wim_info(w->wim_info);
593 if (w->image_metadata) {
594 for (uint i = 0; i < w->hdr.image_count; i++)
595 destroy_image_metadata(&w->image_metadata[i], NULL);
596 FREE(w->image_metadata);
600 DEBUG("Unmounting NTFS volume");
601 ntfs_umount(w->ntfs_vol, FALSE);