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"
34 #include <ntfs-3g/volume.h>
37 static int print_metadata(WIMStruct *w)
39 print_security_data(wim_security_data(w));
40 return for_dentry_in_tree(wim_root_dentry(w), print_dentry,
45 static int print_files(WIMStruct *w)
47 return for_dentry_in_tree(wim_root_dentry(w), print_dentry_full_path,
51 WIMStruct *new_wim_struct()
53 return CALLOC(1, sizeof(WIMStruct));
57 * Calls a function on images in the WIM. If @image is WIM_ALL_IMAGES, @visitor
58 * is called on the WIM once for each image, with each image selected as the
59 * current image in turn. If @image is a certain image, @visitor is called on
60 * the WIM only once, with that image selected.
62 int for_image(WIMStruct *w, int image, int (*visitor)(WIMStruct *))
68 DEBUG("for_image(w = %p, image = %d, visitor = %p)", w, image, visitor);
70 if (image == WIM_ALL_IMAGES) {
72 end = w->hdr.image_count;
74 if (image < 1 || image > w->hdr.image_count)
75 return WIMLIB_ERR_INVALID_IMAGE;
79 for (; i <= end; i++) {
80 ret = wimlib_select_image(w, i);
90 static int sort_image_metadata_by_position(const void *p1, const void *p2)
92 struct image_metadata *bmd1 = (struct image_metadata*)p1;
93 struct image_metadata *bmd2 = (struct image_metadata*)p2;
94 u64 offset1 = bmd1->metadata_lte->resource_entry.offset;
95 u64 offset2 = bmd2->metadata_lte->resource_entry.offset;
96 if (offset1 < offset2)
98 else if (offset1 > offset2)
105 * If @lte points to a metadata resource, append it to the list of metadata
106 * resources in the WIMStruct. Otherwise, do nothing.
108 static int append_metadata_resource_entry(struct lookup_table_entry *lte,
111 WIMStruct *w = wim_p;
113 if (lte->resource_entry.flags & WIM_RESHDR_FLAG_METADATA) {
114 /*fprintf(stderr, "found mlte at %u\n", lte->resource_entry.offset);*/
115 if (w->current_image == w->hdr.image_count) {
116 ERROR("Expected only %u images, but found more",
118 return WIMLIB_ERR_IMAGE_COUNT;
120 DEBUG("Found metadata resource for image %u at "
122 w->current_image + 1,
123 lte->resource_entry.offset);
125 w->current_image++].metadata_lte = lte;
131 /* Returns the compression type given in the flags of a WIM header. */
132 int wim_hdr_flags_compression_type(int wim_hdr_flags)
134 if (wim_hdr_flags & WIM_HDR_FLAG_COMPRESSION) {
135 if (wim_hdr_flags & WIM_HDR_FLAG_COMPRESS_LZX)
136 return WIM_COMPRESSION_TYPE_LZX;
137 else if (wim_hdr_flags & WIM_HDR_FLAG_COMPRESS_XPRESS)
138 return WIM_COMPRESSION_TYPE_XPRESS;
140 return WIM_COMPRESSION_TYPE_INVALID;
142 return WIM_COMPRESSION_TYPE_NONE;
147 * Creates a WIMStruct for a new WIM file.
149 WIMLIBAPI int wimlib_create_new_wim(int ctype, WIMStruct **w_ret)
152 struct lookup_table *table;
155 DEBUG("Creating new WIM with %s compression.",
156 wimlib_get_compression_type_string(ctype));
158 /* Allocate the WIMStruct. */
159 w = new_wim_struct();
161 return WIMLIB_ERR_NOMEM;
163 ret = init_header(&w->hdr, ctype);
167 table = new_lookup_table(9001);
169 ret = WIMLIB_ERR_NOMEM;
172 w->lookup_table = table;
180 WIMLIBAPI int wimlib_get_num_images(const WIMStruct *w)
182 return w->hdr.image_count;
185 int wimlib_select_image(WIMStruct *w, int image)
187 struct image_metadata *imd;
189 DEBUG("Selecting image %d", image);
191 if (image == w->current_image)
194 if (image < 1 || image > w->hdr.image_count) {
195 ERROR("Cannot select image %d: There are only %u images",
196 image, w->hdr.image_count);
197 return WIMLIB_ERR_INVALID_IMAGE;
201 /* If a valid image is currently selected, it can be freed if it is not
203 if (w->current_image != WIM_NO_IMAGE) {
204 imd = wim_get_current_image_metadata(w);
205 if (!imd->modified) {
206 DEBUG("Freeing image %u", w->current_image);
207 destroy_image_metadata(imd, NULL);
208 imd->root_dentry = NULL;
209 imd->security_data = NULL;
214 w->current_image = image;
215 imd = wim_get_current_image_metadata(w);
217 if (imd->root_dentry) {
221 DEBUG("Reading metadata resource specified by the following "
222 "lookup table entry:");
223 print_lookup_table_entry(imd->metadata_lte);
225 return read_metadata_resource(w, imd);
230 /* Returns the compression type of the WIM file. */
231 WIMLIBAPI int wimlib_get_compression_type(const WIMStruct *w)
233 return wim_hdr_flags_compression_type(w->hdr.flags);
236 WIMLIBAPI const char *wimlib_get_compression_type_string(int ctype)
239 case WIM_COMPRESSION_TYPE_NONE:
241 case WIM_COMPRESSION_TYPE_LZX:
243 case WIM_COMPRESSION_TYPE_XPRESS:
251 * Returns the number of an image in the WIM file, given a string that is either
252 * the number of the image, or the name of the image. The images are numbered
255 WIMLIBAPI int wimlib_resolve_image(WIMStruct *w, const char *image_name_or_num)
261 if (!image_name_or_num)
264 if (strcmp(image_name_or_num, "all") == 0)
265 return WIM_ALL_IMAGES;
266 image = strtol(image_name_or_num, &p, 10);
267 if (p != image_name_or_num && *p == '\0') {
268 if (image < 1 || image > w->hdr.image_count)
272 for (i = 1; i <= w->hdr.image_count; i++) {
273 if (strcmp(image_name_or_num,
274 wimlib_get_image_name(w, i)) == 0)
282 /* Prints some basic information about a WIM file. */
283 WIMLIBAPI void wimlib_print_wim_information(const WIMStruct *w)
285 const struct wim_header *hdr;
288 puts("WIM Information:");
289 puts("----------------");
290 printf("Path: %s\n", w->filename);
291 fputs ("GUID: 0x", stdout);
292 print_byte_field(hdr->guid, WIM_GID_LEN);
294 printf("Image Count: %d\n", hdr->image_count);
295 printf("Compression: %s\n", wimlib_get_compression_type_string(
296 wimlib_get_compression_type(w)));
297 printf("Part Number: %d/%d\n", hdr->part_number, hdr->total_parts);
298 printf("Boot Index: %d\n", hdr->boot_idx);
299 printf("Size: %"PRIu64" bytes\n",
300 wim_info_get_total_bytes(w->wim_info));
301 printf("Integrity Info: %s\n", (w->hdr.integrity.size != 0) ? "yes" : "no");
305 WIMLIBAPI bool wimlib_has_integrity_table(const WIMStruct *w)
307 return w->hdr.integrity.size != 0;
310 WIMLIBAPI void wimlib_print_available_images(const WIMStruct *w, int image)
312 if (image == WIM_ALL_IMAGES) {
313 puts("Available Images:");
314 puts("-----------------");
319 n = printf("Information for Image %d\n", image);
320 for (i = 0; i < n - 1; i++)
325 print_image_info(w->wim_info, image);
329 /* Prints the metadata for the specified image, which may be WIM_ALL_IMAGES, but
330 * not WIM_NO_IMAGE. */
331 WIMLIBAPI int wimlib_print_metadata(WIMStruct *w, int image)
333 return for_image(w, image, print_metadata);
336 WIMLIBAPI int wimlib_print_files(WIMStruct *w, int image)
338 return for_image(w, image, print_files);
341 /* Sets the index of the bootable image. */
342 WIMLIBAPI int wimlib_set_boot_idx(WIMStruct *w, int boot_idx)
344 if (boot_idx < 0 || boot_idx > w->hdr.image_count)
345 return WIMLIB_ERR_INVALID_IMAGE;
346 w->hdr.boot_idx = boot_idx;
348 memset(&w->hdr.boot_metadata_res_entry, 0,
349 sizeof(struct resource_entry));
351 memcpy(&w->hdr.boot_metadata_res_entry,
353 boot_idx - 1].metadata_lte->resource_entry,
354 sizeof(struct resource_entry));
359 WIMLIBAPI int wimlib_get_part_number(const WIMStruct *w, int *total_parts_ret)
362 *total_parts_ret = w->hdr.total_parts;
363 return w->hdr.part_number;
367 WIMLIBAPI int wimlib_get_boot_idx(const WIMStruct *w)
369 return w->hdr.boot_idx;
373 * Begins the reading of a WIM file; opens the file and reads its header and
374 * lookup table, and optionally checks the integrity.
376 static int begin_read(WIMStruct *w, const char *in_wim_path, int flags)
381 DEBUG("Reading the WIM file `%s'", in_wim_path);
383 w->filename = STRDUP(in_wim_path);
385 ERROR("Failed to allocate memory for WIM filename");
386 return WIMLIB_ERR_NOMEM;
389 w->fp = fopen(in_wim_path, "rb");
392 ERROR_WITH_ERRNO("Failed to open the file `%s' for reading",
394 return WIMLIB_ERR_OPEN;
397 ret = read_header(w->fp, &w->hdr, flags & WIMLIB_OPEN_FLAG_SPLIT_OK);
401 DEBUG("Wim file contains %u images", w->hdr.image_count);
403 /* If the boot index is invalid, print a warning and set it to 0 */
404 if (w->hdr.boot_idx > w->hdr.image_count) {
405 WARNING("In `%s', image %u is marked as bootable, "
406 "but there are only %u images",
407 in_wim_path, w->hdr.boot_idx, w->hdr.image_count);
411 if (wimlib_get_compression_type(w) == WIM_COMPRESSION_TYPE_INVALID) {
412 ERROR("Invalid compression type (WIM header flags = %x)",
414 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
418 if (flags & WIMLIB_OPEN_FLAG_CHECK_INTEGRITY) {
419 int integrity_status;
420 ret = check_wim_integrity(w,
421 flags & WIMLIB_OPEN_FLAG_SHOW_PROGRESS,
424 ERROR("Error in check_wim_integrity()");
427 if (integrity_status == WIM_INTEGRITY_NONEXISTENT) {
428 WARNING("No integrity information for `%s'; skipping "
429 "integrity check.", w->filename);
430 } else if (integrity_status == WIM_INTEGRITY_NOT_OK) {
431 ERROR("WIM is not intact! (Failed integrity check)");
432 return WIMLIB_ERR_INTEGRITY;
436 if (resource_is_compressed(&w->hdr.lookup_table_res_entry)) {
437 ERROR("Didn't expect a compressed lookup table!");
438 ERROR("Ask the author to implement support for this.");
439 return WIMLIB_ERR_COMPRESSED_LOOKUP_TABLE;
442 ret = read_lookup_table(w);
446 w->image_metadata = CALLOC(w->hdr.image_count,
447 sizeof(struct image_metadata));
449 if (!w->image_metadata) {
450 ERROR("Failed to allocate memory for %u metadata structures",
452 return WIMLIB_ERR_NOMEM;
454 w->current_image = 0;
456 DEBUG("Looking for metadata resources in the lookup table.");
458 /* Find the images in the WIM by searching the lookup table. */
459 ret = for_lookup_table_entry(w->lookup_table,
460 append_metadata_resource_entry, w);
465 /* Make sure all the expected images were found. (We already have
466 * returned false if *extra* images were found) */
467 if (w->current_image != w->hdr.image_count && w->hdr.part_number == 1) {
468 ERROR("Only found %u images in WIM, but expected %u",
469 w->current_image, w->hdr.image_count);
470 return WIMLIB_ERR_IMAGE_COUNT;
474 /* Sort images by the position of their metadata resources. I'm
475 * assuming that is what determines the other of the images in the WIM
476 * file, rather than their order in the lookup table, which is random
477 * because of hashing. */
478 qsort(w->image_metadata, w->current_image,
479 sizeof(struct image_metadata), sort_image_metadata_by_position);
481 w->current_image = WIM_NO_IMAGE;
483 /* Read the XML data. */
484 ret = read_xml_data(w->fp, &w->hdr.xml_res_entry,
485 &w->xml_data, &w->wim_info);
488 ERROR("Missing or invalid XML data");
492 xml_num_images = wim_info_get_num_images(w->wim_info);
493 if (xml_num_images != w->hdr.image_count) {
494 ERROR("In the file `%s', there are %u <IMAGE> elements "
495 "in the XML data,", in_wim_path, xml_num_images);
496 ERROR("but %u images in the WIM! There must be exactly one "
497 "<IMAGE> element per image.", w->hdr.image_count);
498 return WIMLIB_ERR_IMAGE_COUNT;
501 DEBUG("Done beginning read of WIM file `%s'.", in_wim_path);
507 * Opens a WIM file and creates a WIMStruct for it.
509 WIMLIBAPI int wimlib_open_wim(const char *wim_file, int flags,
515 DEBUG("wim_file = `%s', flags = %#x", wim_file, flags);
516 w = new_wim_struct();
518 ERROR("Failed to allocate memory for WIMStruct");
519 return WIMLIB_ERR_NOMEM;
522 ret = begin_read(w, wim_file, flags);
524 DEBUG("Could not begin reading the WIM file `%s'", wim_file);
532 /* Frees the memory for the WIMStruct, including all internal memory; also
533 * closes all files associated with the WIMStruct. */
534 WIMLIBAPI void wimlib_free(WIMStruct *w)
536 DEBUG("Freeing WIMStruct");
545 free_lookup_table(w->lookup_table);
549 free_wim_info(w->wim_info);
550 if (w->image_metadata) {
551 for (uint i = 0; i < w->hdr.image_count; i++)
552 destroy_image_metadata(&w->image_metadata[i], NULL);
553 FREE(w->image_metadata);
557 DEBUG("Unmounting NTFS volume");
558 ntfs_umount(w->ntfs_vol, FALSE);