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 Lesser General Public License as published by the Free
15 * Software Foundation; either version 2.1 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 Lesser General Public License for more
23 * You should have received a copy of the GNU Lesser General Public License
24 * along with wimlib; if not, see http://www.gnu.org/licenses/.
27 #include "wimlib_internal.h"
29 #include "lookup_table.h"
33 static int print_metadata(WIMStruct *w)
35 print_security_data(wim_security_data(w));
36 return for_dentry_in_tree(wim_root_dentry(w), print_dentry,
41 static int print_files(WIMStruct *w)
43 return for_dentry_in_tree(wim_root_dentry(w), print_dentry_full_path,
47 WIMStruct *new_wim_struct()
49 return CALLOC(1, sizeof(WIMStruct));
53 * Calls a function on images in the WIM. If @image is WIM_ALL_IMAGES, @visitor
54 * is called on the WIM once for each image, with each image selected as the
55 * current image in turn. If @image is a certain image, @visitor is called on
56 * the WIM only once, with that image selected.
58 int for_image(WIMStruct *w, int image, int (*visitor)(WIMStruct *))
64 DEBUG("for_image(w = %p, image = %d, visitor = %p)", w, image, visitor);
66 if (image == WIM_ALL_IMAGES) {
68 end = w->hdr.image_count;
70 if (image < 1 || image > w->hdr.image_count)
71 return WIMLIB_ERR_INVALID_IMAGE;
75 for (; i <= end; i++) {
76 ret = wimlib_select_image(w, i);
86 static int sort_image_metadata_by_position(const void *p1, const void *p2)
88 struct image_metadata *bmd1 = (struct image_metadata*)p1;
89 struct image_metadata *bmd2 = (struct image_metadata*)p2;
90 u64 offset1 = bmd1->metadata_lte->resource_entry.offset;
91 u64 offset2 = bmd2->metadata_lte->resource_entry.offset;
92 if (offset1 < offset2)
94 else if (offset1 > offset2)
101 * If @lte points to a metadata resource, append it to the list of metadata
102 * resources in the WIMStruct. Otherwise, do nothing.
104 static int append_metadata_resource_entry(struct lookup_table_entry *lte,
107 WIMStruct *w = wim_p;
109 if (lte->resource_entry.flags & WIM_RESHDR_FLAG_METADATA) {
110 /*fprintf(stderr, "found mlte at %u\n", lte->resource_entry.offset);*/
111 if (w->current_image == w->hdr.image_count) {
112 ERROR("Expected only %u images, but found more",
114 return WIMLIB_ERR_IMAGE_COUNT;
116 DEBUG("Found metadata resource for image %u at "
118 w->current_image + 1,
119 lte->resource_entry.offset);
121 w->current_image++].metadata_lte = lte;
127 /* Returns the compression type given in the flags of a WIM header. */
128 int wim_hdr_flags_compression_type(int wim_hdr_flags)
130 if (wim_hdr_flags & WIM_HDR_FLAG_COMPRESSION) {
131 if (wim_hdr_flags & WIM_HDR_FLAG_COMPRESS_LZX)
132 return WIM_COMPRESSION_TYPE_LZX;
133 else if (wim_hdr_flags & WIM_HDR_FLAG_COMPRESS_XPRESS)
134 return WIM_COMPRESSION_TYPE_XPRESS;
136 return WIM_COMPRESSION_TYPE_INVALID;
138 return WIM_COMPRESSION_TYPE_NONE;
143 * Creates a WIMStruct for a new WIM file.
145 WIMLIBAPI int wimlib_create_new_wim(int ctype, WIMStruct **w_ret)
148 struct lookup_table *table;
151 DEBUG("Creating new WIM with %s compression.",
152 wimlib_get_compression_type_string(ctype));
154 /* Allocate the WIMStruct. */
155 w = new_wim_struct();
157 return WIMLIB_ERR_NOMEM;
159 ret = init_header(&w->hdr, ctype);
163 table = new_lookup_table(9001);
165 ret = WIMLIB_ERR_NOMEM;
168 w->lookup_table = table;
176 WIMLIBAPI int wimlib_get_num_images(const WIMStruct *w)
178 return w->hdr.image_count;
181 int wimlib_select_image(WIMStruct *w, int image)
183 struct image_metadata *imd;
185 DEBUG("Selecting image %d", image);
187 if (image == w->current_image)
190 if (image < 1 || image > w->hdr.image_count) {
191 ERROR("Cannot select image %d: There are only %u images",
192 image, w->hdr.image_count);
193 return WIMLIB_ERR_INVALID_IMAGE;
197 /* If a valid image is currently selected, it can be freed if it is not
199 if (w->current_image != WIM_NO_IMAGE) {
200 imd = wim_get_current_image_metadata(w);
201 if (!imd->modified) {
202 DEBUG("Freeing image %u", w->current_image);
203 destroy_image_metadata(imd, NULL);
204 imd->root_dentry = NULL;
205 imd->security_data = NULL;
210 w->current_image = image;
211 imd = wim_get_current_image_metadata(w);
213 if (imd->root_dentry) {
217 DEBUG("Reading metadata resource specified by the following "
218 "lookup table entry:");
219 print_lookup_table_entry(imd->metadata_lte);
221 return read_metadata_resource(w->fp,
222 wimlib_get_compression_type(w),
228 /* Returns the compression type of the WIM file. */
229 WIMLIBAPI int wimlib_get_compression_type(const WIMStruct *w)
231 return wim_hdr_flags_compression_type(w->hdr.flags);
234 WIMLIBAPI const char *wimlib_get_compression_type_string(int ctype)
237 case WIM_COMPRESSION_TYPE_NONE:
239 case WIM_COMPRESSION_TYPE_LZX:
241 case WIM_COMPRESSION_TYPE_XPRESS:
249 * Returns the number of an image in the WIM file, given a string that is either
250 * the number of the image, or the name of the image. The images are numbered
253 WIMLIBAPI int wimlib_resolve_image(WIMStruct *w, const char *image_name_or_num)
259 if (!image_name_or_num)
262 if (strcmp(image_name_or_num, "all") == 0)
263 return WIM_ALL_IMAGES;
264 image = strtol(image_name_or_num, &p, 10);
265 if (p != image_name_or_num && *p == '\0') {
266 if (image < 1 || image > w->hdr.image_count)
270 for (i = 1; i <= w->hdr.image_count; i++) {
271 if (strcmp(image_name_or_num,
272 wimlib_get_image_name(w, i)) == 0)
280 /* Prints some basic information about a WIM file. */
281 WIMLIBAPI void wimlib_print_wim_information(const WIMStruct *w)
283 const struct wim_header *hdr;
286 puts("WIM Information:");
287 puts("----------------");
288 printf("Path: %s\n", w->filename);
289 fputs ("GUID: 0x", stdout);
290 print_byte_field(hdr->guid, WIM_GID_LEN);
292 printf("Image Count: %d\n", hdr->image_count);
293 printf("Compression: %s\n", wimlib_get_compression_type_string(
294 wimlib_get_compression_type(w)));
295 printf("Part Number: %d/%d\n", hdr->part_number, hdr->total_parts);
296 printf("Boot Index: %d\n", hdr->boot_idx);
297 printf("Size: %"PRIu64" bytes\n",
298 wim_info_get_total_bytes(w->wim_info));
299 printf("Integrity Info: %s\n", (w->hdr.integrity.size != 0) ? "yes" : "no");
303 WIMLIBAPI bool wimlib_has_integrity_table(const WIMStruct *w)
305 return w->hdr.integrity.size != 0;
308 WIMLIBAPI void wimlib_print_available_images(const WIMStruct *w, int image)
310 if (image == WIM_ALL_IMAGES) {
311 puts("Available Images:");
312 puts("-----------------");
317 n = printf("Information for Image %d\n", image);
318 for (i = 0; i < n - 1; i++)
323 print_image_info(w->wim_info, image);
327 /* Prints the metadata for the specified image, which may be WIM_ALL_IMAGES, but
328 * not WIM_NO_IMAGE. */
329 WIMLIBAPI int wimlib_print_metadata(WIMStruct *w, int image)
331 return for_image(w, image, print_metadata);
334 WIMLIBAPI int wimlib_print_files(WIMStruct *w, int image)
336 return for_image(w, image, print_files);
339 /* Sets the index of the bootable image. */
340 WIMLIBAPI int wimlib_set_boot_idx(WIMStruct *w, int boot_idx)
342 if (boot_idx < 0 || boot_idx > w->hdr.image_count)
343 return WIMLIB_ERR_INVALID_IMAGE;
344 w->hdr.boot_idx = boot_idx;
346 memset(&w->hdr.boot_metadata_res_entry, 0,
347 sizeof(struct resource_entry));
349 memcpy(&w->hdr.boot_metadata_res_entry,
351 boot_idx - 1].metadata_lte->resource_entry,
352 sizeof(struct resource_entry));
357 WIMLIBAPI int wimlib_get_part_number(const WIMStruct *w, int *total_parts_ret)
360 *total_parts_ret = w->hdr.total_parts;
361 return w->hdr.part_number;
365 WIMLIBAPI int wimlib_get_boot_idx(const WIMStruct *w)
367 return w->hdr.boot_idx;
371 * Begins the reading of a WIM file; opens the file and reads its header and
372 * lookup table, and optionally checks the integrity.
374 static int begin_read(WIMStruct *w, const char *in_wim_path, int flags)
379 DEBUG("Reading the WIM file `%s'", in_wim_path);
381 w->filename = STRDUP(in_wim_path);
383 ERROR("Failed to allocate memory for WIM filename");
384 return WIMLIB_ERR_NOMEM;
387 w->fp = fopen(in_wim_path, "rb");
390 ERROR_WITH_ERRNO("Failed to open the file `%s' for reading",
392 return WIMLIB_ERR_OPEN;
395 ret = read_header(w->fp, &w->hdr, flags & WIMLIB_OPEN_FLAG_SPLIT_OK);
399 DEBUG("Wim file contains %u images", w->hdr.image_count);
401 /* If the boot index is invalid, print a warning and set it to 0 */
402 if (w->hdr.boot_idx > w->hdr.image_count) {
403 WARNING("In `%s', image %u is marked as bootable, "
404 "but there are only %u images",
405 in_wim_path, w->hdr.boot_idx, w->hdr.image_count);
409 if (wimlib_get_compression_type(w) == WIM_COMPRESSION_TYPE_INVALID) {
410 ERROR("Invalid compression type (WIM header flags = %x)",
412 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
416 if (flags & WIMLIB_OPEN_FLAG_CHECK_INTEGRITY) {
417 int integrity_status;
418 ret = check_wim_integrity(w,
419 flags & WIMLIB_OPEN_FLAG_SHOW_PROGRESS,
422 ERROR("Error in check_wim_integrity()");
425 if (integrity_status == WIM_INTEGRITY_NONEXISTENT) {
426 WARNING("No integrity information for `%s'; skipping "
427 "integrity check.", w->filename);
428 } else if (integrity_status == WIM_INTEGRITY_NOT_OK) {
429 ERROR("WIM is not intact! (Failed integrity check)");
430 return WIMLIB_ERR_INTEGRITY;
434 if (resource_is_compressed(&w->hdr.lookup_table_res_entry)) {
435 ERROR("Didn't expect a compressed lookup table!");
436 ERROR("Ask the author to implement support for this.");
437 return WIMLIB_ERR_COMPRESSED_LOOKUP_TABLE;
440 ret = read_lookup_table(w);
444 w->image_metadata = CALLOC(w->hdr.image_count,
445 sizeof(struct image_metadata));
447 if (!w->image_metadata) {
448 ERROR("Failed to allocate memory for %u metadata structures",
450 return WIMLIB_ERR_NOMEM;
452 w->current_image = 0;
454 DEBUG("Looking for metadata resources in the lookup table.");
456 /* Find the images in the WIM by searching the lookup table. */
457 ret = for_lookup_table_entry(w->lookup_table,
458 append_metadata_resource_entry, w);
463 /* Make sure all the expected images were found. (We already have
464 * returned false if *extra* images were found) */
465 if (w->current_image != w->hdr.image_count && w->hdr.part_number == 1) {
466 ERROR("Only found %u images in WIM, but expected %u",
467 w->current_image, w->hdr.image_count);
468 return WIMLIB_ERR_IMAGE_COUNT;
472 /* Sort images by the position of their metadata resources. I'm
473 * assuming that is what determines the other of the images in the WIM
474 * file, rather than their order in the lookup table, which is random
475 * because of hashing. */
476 qsort(w->image_metadata, w->current_image,
477 sizeof(struct image_metadata), sort_image_metadata_by_position);
479 w->current_image = WIM_NO_IMAGE;
481 /* Read the XML data. */
482 ret = read_xml_data(w->fp, &w->hdr.xml_res_entry,
483 &w->xml_data, &w->wim_info);
486 ERROR("Missing or invalid XML data");
490 xml_num_images = wim_info_get_num_images(w->wim_info);
491 if (xml_num_images != w->hdr.image_count) {
492 ERROR("In the file `%s', there are %u <IMAGE> elements "
493 "in the XML data,", in_wim_path, xml_num_images);
494 ERROR("but %u images in the WIM! There must be exactly one "
495 "<IMAGE> element per image.", w->hdr.image_count);
496 return WIMLIB_ERR_IMAGE_COUNT;
499 DEBUG("Done beginning read of WIM file `%s'.", in_wim_path);
505 * Opens a WIM file and creates a WIMStruct for it.
507 WIMLIBAPI int wimlib_open_wim(const char *wim_file, int flags,
513 DEBUG("wim_file = `%s', flags = %#x", wim_file, flags);
514 w = new_wim_struct();
516 ERROR("Failed to allocate memory for WIMStruct");
517 return WIMLIB_ERR_NOMEM;
520 ret = begin_read(w, wim_file, flags);
522 DEBUG("Could not begin reading the WIM file `%s'", wim_file);
530 /* Frees the memory for the WIMStruct, including all internal memory; also
531 * closes all files associated with the WIMStruct. */
532 WIMLIBAPI void wimlib_free(WIMStruct *w)
534 DEBUG("Freeing WIMStruct");
543 free_lookup_table(w->lookup_table);
547 free_wim_info(w->wim_info);
548 if (w->image_metadata) {
549 for (uint i = 0; i < w->hdr.image_count; i++)
550 destroy_image_metadata(&w->image_metadata[i], NULL);
551 FREE(w->image_metadata);