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/.
32 #include <ntfs-3g/volume.h>
35 #include "wimlib_internal.h"
37 #include "lookup_table.h"
41 static int print_metadata(WIMStruct *w)
43 print_security_data(wim_security_data(w));
44 return for_dentry_in_tree(wim_root_dentry(w), print_dentry,
49 static int print_files(WIMStruct *w)
51 return for_dentry_in_tree(wim_root_dentry(w), print_dentry_full_path,
55 WIMStruct *new_wim_struct()
57 return CALLOC(1, sizeof(WIMStruct));
61 * Calls a function on images in the WIM. If @image is WIM_ALL_IMAGES, @visitor
62 * is called on the WIM once for each image, with each image selected as the
63 * current image in turn. If @image is a certain image, @visitor is called on
64 * the WIM only once, with that image selected.
66 int for_image(WIMStruct *w, int image, int (*visitor)(WIMStruct *))
72 if (image == WIM_ALL_IMAGES) {
74 end = w->hdr.image_count;
76 if (image < 1 || image > w->hdr.image_count)
77 return WIMLIB_ERR_INVALID_IMAGE;
81 for (; i <= end; i++) {
82 ret = wimlib_select_image(w, i);
92 static int sort_image_metadata_by_position(const void *p1, const void *p2)
94 struct image_metadata *bmd1 = (struct image_metadata*)p1;
95 struct image_metadata *bmd2 = (struct image_metadata*)p2;
96 u64 offset1 = bmd1->metadata_lte->resource_entry.offset;
97 u64 offset2 = bmd2->metadata_lte->resource_entry.offset;
98 if (offset1 < offset2)
100 else if (offset1 > offset2)
107 * If @lte points to a metadata resource, append it to the list of metadata
108 * resources in the WIMStruct. Otherwise, do nothing.
110 static int append_metadata_resource_entry(struct lookup_table_entry *lte,
113 WIMStruct *w = wim_p;
115 if (lte->resource_entry.flags & WIM_RESHDR_FLAG_METADATA) {
116 /*fprintf(stderr, "found mlte at %u\n", lte->resource_entry.offset);*/
117 if (w->current_image == w->hdr.image_count) {
118 ERROR("Expected only %u images, but found more",
120 return WIMLIB_ERR_IMAGE_COUNT;
122 DEBUG("Found metadata resource for image %u at "
124 w->current_image + 1,
125 lte->resource_entry.offset);
127 w->current_image++].metadata_lte = lte;
133 /* Returns the compression type given in the flags of a WIM header. */
134 int wim_hdr_flags_compression_type(int wim_hdr_flags)
136 if (wim_hdr_flags & WIM_HDR_FLAG_COMPRESSION) {
137 if (wim_hdr_flags & WIM_HDR_FLAG_COMPRESS_LZX)
138 return WIM_COMPRESSION_TYPE_LZX;
139 else if (wim_hdr_flags & WIM_HDR_FLAG_COMPRESS_XPRESS)
140 return WIM_COMPRESSION_TYPE_XPRESS;
142 return WIM_COMPRESSION_TYPE_INVALID;
144 return WIM_COMPRESSION_TYPE_NONE;
149 * Creates a WIMStruct for a new WIM file.
151 WIMLIBAPI int wimlib_create_new_wim(int ctype, WIMStruct **w_ret)
154 struct lookup_table *table;
157 DEBUG("Creating new WIM with %s compression.",
158 wimlib_get_compression_type_string(ctype));
160 /* Allocate the WIMStruct. */
161 w = new_wim_struct();
163 return WIMLIB_ERR_NOMEM;
165 ret = init_header(&w->hdr, ctype);
169 table = new_lookup_table(9001);
171 ret = WIMLIB_ERR_NOMEM;
174 w->lookup_table = table;
182 WIMLIBAPI int wimlib_get_num_images(const WIMStruct *w)
184 return w->hdr.image_count;
187 int wimlib_select_image(WIMStruct *w, int image)
189 struct image_metadata *imd;
191 DEBUG("Selecting image %d", image);
193 if (image == w->current_image)
196 if (image < 1 || image > w->hdr.image_count) {
197 ERROR("Cannot select image %d: There are only %u images",
198 image, w->hdr.image_count);
199 return WIMLIB_ERR_INVALID_IMAGE;
203 /* If a valid image is currently selected, it can be freed if it is not
205 if (w->current_image != WIM_NO_IMAGE) {
206 imd = wim_get_current_image_metadata(w);
207 if (!imd->modified) {
208 DEBUG("Freeing image %u", w->current_image);
209 destroy_image_metadata(imd, NULL);
210 imd->root_dentry = NULL;
211 imd->security_data = NULL;
215 w->current_image = image;
216 imd = wim_get_current_image_metadata(w);
218 if (imd->root_dentry) {
222 DEBUG("Reading metadata resource specified by the following "
223 "lookup table entry:");
224 print_lookup_table_entry(imd->metadata_lte);
226 return read_metadata_resource(w, imd);
231 /* Returns the compression type of the WIM file. */
232 WIMLIBAPI int wimlib_get_compression_type(const WIMStruct *w)
234 return wim_hdr_flags_compression_type(w->hdr.flags);
237 WIMLIBAPI const char *wimlib_get_compression_type_string(int ctype)
240 case WIM_COMPRESSION_TYPE_NONE:
242 case WIM_COMPRESSION_TYPE_LZX:
244 case WIM_COMPRESSION_TYPE_XPRESS:
252 * Returns the number of an image in the WIM file, given a string that is either
253 * the number of the image, or the name of the image. The images are numbered
256 WIMLIBAPI int wimlib_resolve_image(WIMStruct *w, const char *image_name_or_num)
262 if (!image_name_or_num)
265 if (strcmp(image_name_or_num, "all") == 0
266 || strcmp(image_name_or_num, "*") == 0)
267 return WIM_ALL_IMAGES;
268 image = strtol(image_name_or_num, &p, 10);
269 if (p != image_name_or_num && *p == '\0') {
270 if (image < 1 || image > w->hdr.image_count)
274 for (i = 1; i <= w->hdr.image_count; i++) {
275 if (strcmp(image_name_or_num,
276 wimlib_get_image_name(w, i)) == 0)
284 /* Prints some basic information about a WIM file. */
285 WIMLIBAPI void wimlib_print_wim_information(const WIMStruct *w)
287 const struct wim_header *hdr;
290 puts("WIM Information:");
291 puts("----------------");
292 printf("Path: %s\n", w->filename);
293 fputs ("GUID: 0x", stdout);
294 print_byte_field(hdr->guid, WIM_GID_LEN);
296 printf("Image Count: %d\n", hdr->image_count);
297 printf("Compression: %s\n", wimlib_get_compression_type_string(
298 wimlib_get_compression_type(w)));
299 printf("Part Number: %d/%d\n", hdr->part_number, hdr->total_parts);
300 printf("Boot Index: %d\n", hdr->boot_idx);
301 printf("Size: %"PRIu64" bytes\n",
302 wim_info_get_total_bytes(w->wim_info));
303 printf("Integrity Info: %s\n", (w->hdr.integrity.size != 0) ? "yes" : "no");
307 WIMLIBAPI bool wimlib_has_integrity_table(const WIMStruct *w)
309 return w->hdr.integrity.size != 0;
312 WIMLIBAPI void wimlib_print_available_images(const WIMStruct *w, int image)
314 if (image == WIM_ALL_IMAGES) {
315 puts("Available Images:");
316 puts("-----------------");
321 n = printf("Information for Image %d\n", image);
322 for (i = 0; i < n - 1; i++)
327 print_image_info(w->wim_info, image);
331 /* Prints the metadata for the specified image, which may be WIM_ALL_IMAGES, but
332 * not WIM_NO_IMAGE. */
333 WIMLIBAPI int wimlib_print_metadata(WIMStruct *w, int image)
336 return WIMLIB_ERR_INVALID_PARAM;
337 if (w->hdr.part_number != 1) {
338 ERROR("We cannot show the metadata from part %hu of a %hu-part split WIM.",
339 w->hdr.part_number, w->hdr.total_parts);
340 ERROR("Select the first part of the split WIM to see the metadata.");
341 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
343 if (image == WIM_ALL_IMAGES)
344 DEBUG("Printing metadata for all images");
346 DEBUG("Printing metadata for image %d", image);
347 return for_image(w, image, print_metadata);
350 WIMLIBAPI int wimlib_print_files(WIMStruct *w, int image)
353 return WIMLIB_ERR_INVALID_PARAM;
354 if (w->hdr.part_number != 1) {
355 ERROR("We cannot list the files from part %hu of a %hu-part split WIM",
356 w->hdr.part_number, w->hdr.total_parts);
357 ERROR("Select the first part of the split WIM if you'd like to list the files.");
358 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
360 return for_image(w, image, print_files);
363 /* Sets the index of the bootable image. */
364 WIMLIBAPI int wimlib_set_boot_idx(WIMStruct *w, int boot_idx)
367 return WIMLIB_ERR_INVALID_PARAM;
368 if (w->hdr.total_parts != 1) {
369 ERROR("We cannot modify the boot index of a split WIM");
370 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
372 if (boot_idx < 0 || boot_idx > w->hdr.image_count)
373 return WIMLIB_ERR_INVALID_IMAGE;
374 w->hdr.boot_idx = boot_idx;
376 memset(&w->hdr.boot_metadata_res_entry, 0,
377 sizeof(struct resource_entry));
379 memcpy(&w->hdr.boot_metadata_res_entry,
381 boot_idx - 1].metadata_lte->resource_entry,
382 sizeof(struct resource_entry));
387 WIMLIBAPI int wimlib_get_part_number(const WIMStruct *w, int *total_parts_ret)
390 *total_parts_ret = w->hdr.total_parts;
391 return w->hdr.part_number;
395 WIMLIBAPI int wimlib_get_boot_idx(const WIMStruct *w)
397 return w->hdr.boot_idx;
401 * Begins the reading of a WIM file; opens the file and reads its header and
402 * lookup table, and optionally checks the integrity.
404 static int begin_read(WIMStruct *w, const char *in_wim_path, int flags)
409 DEBUG("Reading the WIM file `%s'", in_wim_path);
411 w->filename = STRDUP(in_wim_path);
413 ERROR("Failed to allocate memory for WIM filename");
414 return WIMLIB_ERR_NOMEM;
417 w->fp = fopen(in_wim_path, "rb");
420 ERROR_WITH_ERRNO("Failed to open the file `%s' for reading",
422 return WIMLIB_ERR_OPEN;
425 ret = read_header(w->fp, &w->hdr, flags & WIMLIB_OPEN_FLAG_SPLIT_OK);
429 DEBUG("Wim file contains %u images", w->hdr.image_count);
431 /* If the boot index is invalid, print a warning and set it to 0 */
432 if (w->hdr.boot_idx > w->hdr.image_count) {
433 WARNING("In `%s', image %u is marked as bootable, "
434 "but there are only %u images",
435 in_wim_path, w->hdr.boot_idx, w->hdr.image_count);
439 if (wimlib_get_compression_type(w) == WIM_COMPRESSION_TYPE_INVALID) {
440 ERROR("Invalid compression type (WIM header flags = %x)",
442 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
446 if (flags & WIMLIB_OPEN_FLAG_CHECK_INTEGRITY) {
447 int integrity_status;
448 ret = check_wim_integrity(w,
449 flags & WIMLIB_OPEN_FLAG_SHOW_PROGRESS,
452 ERROR("Error in check_wim_integrity()");
455 if (integrity_status == WIM_INTEGRITY_NONEXISTENT) {
456 WARNING("No integrity information for `%s'; skipping "
457 "integrity check.", w->filename);
458 } else if (integrity_status == WIM_INTEGRITY_NOT_OK) {
459 ERROR("WIM is not intact! (Failed integrity check)");
460 return WIMLIB_ERR_INTEGRITY;
464 if (resource_is_compressed(&w->hdr.lookup_table_res_entry)) {
465 ERROR("Didn't expect a compressed lookup table!");
466 ERROR("Ask the author to implement support for this.");
467 return WIMLIB_ERR_COMPRESSED_LOOKUP_TABLE;
470 ret = read_lookup_table(w);
474 w->image_metadata = CALLOC(w->hdr.image_count,
475 sizeof(struct image_metadata));
477 if (!w->image_metadata) {
478 ERROR("Failed to allocate memory for %u metadata structures",
480 return WIMLIB_ERR_NOMEM;
482 w->current_image = 0;
484 DEBUG("Looking for metadata resources in the lookup table.");
486 /* Find the images in the WIM by searching the lookup table. */
487 ret = for_lookup_table_entry(w->lookup_table,
488 append_metadata_resource_entry, w);
493 /* Make sure all the expected images were found. (We already have
494 * returned false if *extra* images were found) */
495 if (w->current_image != w->hdr.image_count && w->hdr.part_number == 1) {
496 ERROR("Only found %u images in WIM, but expected %u",
497 w->current_image, w->hdr.image_count);
498 return WIMLIB_ERR_IMAGE_COUNT;
502 /* Sort images by the position of their metadata resources. I'm
503 * assuming that is what determines the other of the images in the WIM
504 * file, rather than their order in the lookup table, which is random
505 * because of hashing. */
506 qsort(w->image_metadata, w->current_image,
507 sizeof(struct image_metadata), sort_image_metadata_by_position);
509 w->current_image = WIM_NO_IMAGE;
511 /* Read the XML data. */
512 ret = read_xml_data(w->fp, &w->hdr.xml_res_entry,
513 &w->xml_data, &w->wim_info);
516 ERROR("Missing or invalid XML data");
520 xml_num_images = wim_info_get_num_images(w->wim_info);
521 if (xml_num_images != w->hdr.image_count) {
522 ERROR("In the file `%s', there are %u <IMAGE> elements "
523 "in the XML data,", in_wim_path, xml_num_images);
524 ERROR("but %u images in the WIM! There must be exactly one "
525 "<IMAGE> element per image.", w->hdr.image_count);
526 return WIMLIB_ERR_IMAGE_COUNT;
529 DEBUG("Done beginning read of WIM file `%s'.", in_wim_path);
535 * Opens a WIM file and creates a WIMStruct for it.
537 WIMLIBAPI int wimlib_open_wim(const char *wim_file, int flags,
543 DEBUG("wim_file = `%s', flags = %#x", wim_file, flags);
544 w = new_wim_struct();
546 ERROR("Failed to allocate memory for WIMStruct");
547 return WIMLIB_ERR_NOMEM;
550 ret = begin_read(w, wim_file, flags);
552 DEBUG("Could not begin reading the WIM file `%s'", wim_file);
560 /* Frees the memory for the WIMStruct, including all internal memory; also
561 * closes all files associated with the WIMStruct. */
562 WIMLIBAPI void wimlib_free(WIMStruct *w)
564 DEBUG2("Freeing WIMStruct");
573 free_lookup_table(w->lookup_table);
577 free_wim_info(w->wim_info);
578 if (w->image_metadata) {
579 for (uint i = 0; i < w->hdr.image_count; i++)
580 destroy_image_metadata(&w->image_metadata[i], NULL);
581 FREE(w->image_metadata);
585 DEBUG("Unmounting NTFS volume");
586 ntfs_umount(w->ntfs_vol, FALSE);