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/.
36 #include <ntfs-3g/volume.h>
39 #include "wimlib_internal.h"
41 #include "lookup_table.h"
45 static int print_metadata(WIMStruct *w)
47 DEBUG("Printing metadata for image %d", w->current_image);
48 print_security_data(wim_security_data(w));
49 return for_dentry_in_tree(wim_root_dentry(w), print_dentry,
54 static int print_files(WIMStruct *w)
56 return for_dentry_in_tree(wim_root_dentry(w), print_dentry_full_path,
60 WIMStruct *new_wim_struct()
62 WIMStruct *w = CALLOC(1, sizeof(WIMStruct));
63 if (pthread_mutex_init(&w->fp_tab_mutex, NULL) != 0) {
64 ERROR_WITH_ERRNO("Failed to initialize mutex");
73 * Calls a function on images in the WIM. If @image is WIM_ALL_IMAGES, @visitor
74 * is called on the WIM once for each image, with each image selected as the
75 * current image in turn. If @image is a certain image, @visitor is called on
76 * the WIM only once, with that image selected.
78 int for_image(WIMStruct *w, int image, int (*visitor)(WIMStruct *))
85 if (image == WIM_ALL_IMAGES) {
87 end = w->hdr.image_count;
88 } else if (image >= 1 && image <= w->hdr.image_count) {
92 return WIMLIB_ERR_INVALID_IMAGE;
94 for (i = start; i <= end; i++) {
95 ret = select_wim_image(w, i);
105 static int sort_image_metadata_by_position(const void *p1, const void *p2)
107 const struct image_metadata *imd1 = p1;
108 const struct image_metadata *imd2 = p2;
109 u64 offset1 = imd1->metadata_lte->resource_entry.offset;
110 u64 offset2 = imd2->metadata_lte->resource_entry.offset;
111 if (offset1 < offset2)
113 else if (offset1 > offset2)
120 * If @lte points to a metadata resource, append it to the list of metadata
121 * resources in the WIMStruct. Otherwise, do nothing.
123 static int append_metadata_resource_entry(struct lookup_table_entry *lte,
126 WIMStruct *w = wim_p;
129 if (lte->resource_entry.flags & WIM_RESHDR_FLAG_METADATA) {
130 if (w->current_image == w->hdr.image_count) {
131 ERROR("Expected only %u images, but found more",
133 ret = WIMLIB_ERR_IMAGE_COUNT;
135 DEBUG("Found metadata resource for image %u at "
137 w->current_image + 1,
138 lte->resource_entry.offset);
140 w->current_image++].metadata_lte = lte;
146 /* Returns the compression type given in the flags of a WIM header. */
147 int wim_hdr_flags_compression_type(int wim_hdr_flags)
149 if (wim_hdr_flags & WIM_HDR_FLAG_COMPRESSION) {
150 if (wim_hdr_flags & WIM_HDR_FLAG_COMPRESS_LZX)
151 return WIM_COMPRESSION_TYPE_LZX;
152 else if (wim_hdr_flags & WIM_HDR_FLAG_COMPRESS_XPRESS)
153 return WIM_COMPRESSION_TYPE_XPRESS;
155 return WIM_COMPRESSION_TYPE_INVALID;
157 return WIM_COMPRESSION_TYPE_NONE;
162 * Creates a WIMStruct for a new WIM file.
164 WIMLIBAPI int wimlib_create_new_wim(int ctype, WIMStruct **w_ret)
167 struct lookup_table *table;
170 DEBUG("Creating new WIM with %s compression.",
171 wimlib_get_compression_type_string(ctype));
173 /* Allocate the WIMStruct. */
174 w = new_wim_struct();
176 return WIMLIB_ERR_NOMEM;
178 ret = init_header(&w->hdr, ctype);
182 table = new_lookup_table(9001);
184 ret = WIMLIB_ERR_NOMEM;
187 w->lookup_table = table;
195 WIMLIBAPI int wimlib_get_num_images(const WIMStruct *w)
197 return w->hdr.image_count;
200 int select_wim_image(WIMStruct *w, int image)
202 struct image_metadata *imd;
204 DEBUG("Selecting image %d", image);
206 if (image == w->current_image)
209 if (image < 1 || image > w->hdr.image_count) {
210 ERROR("Cannot select image %d: There are only %u images",
211 image, w->hdr.image_count);
212 return WIMLIB_ERR_INVALID_IMAGE;
216 /* If a valid image is currently selected, it can be freed if it is not
218 if (w->current_image != WIM_NO_IMAGE) {
219 imd = wim_get_current_image_metadata(w);
220 if (!imd->modified) {
221 DEBUG("Freeing image %u", w->current_image);
222 destroy_image_metadata(imd, NULL);
223 imd->root_dentry = NULL;
224 imd->security_data = NULL;
228 w->current_image = image;
229 imd = wim_get_current_image_metadata(w);
231 if (imd->root_dentry) {
235 DEBUG("Reading metadata resource specified by the following "
236 "lookup table entry:");
237 print_lookup_table_entry(imd->metadata_lte);
239 return read_metadata_resource(w, imd);
244 /* Returns the compression type of the WIM file. */
245 WIMLIBAPI int wimlib_get_compression_type(const WIMStruct *w)
247 return wim_hdr_flags_compression_type(w->hdr.flags);
250 WIMLIBAPI const char *wimlib_get_compression_type_string(int ctype)
253 case WIM_COMPRESSION_TYPE_NONE:
255 case WIM_COMPRESSION_TYPE_LZX:
257 case WIM_COMPRESSION_TYPE_XPRESS:
265 * Returns the number of an image in the WIM file, given a string that is either
266 * the number of the image, or the name of the image. The images are numbered
269 WIMLIBAPI int wimlib_resolve_image(WIMStruct *w, const char *image_name_or_num)
275 if (!image_name_or_num)
278 if (strcmp(image_name_or_num, "all") == 0
279 || strcmp(image_name_or_num, "*") == 0)
280 return WIM_ALL_IMAGES;
281 image = strtol(image_name_or_num, &p, 10);
282 if (p != image_name_or_num && *p == '\0') {
283 if (image < 1 || image > w->hdr.image_count)
287 for (i = 1; i <= w->hdr.image_count; i++) {
288 if (strcmp(image_name_or_num,
289 wimlib_get_image_name(w, i)) == 0)
297 /* Prints some basic information about a WIM file. */
298 WIMLIBAPI void wimlib_print_wim_information(const WIMStruct *w)
300 const struct wim_header *hdr;
303 puts("WIM Information:");
304 puts("----------------");
305 printf("Path: %s\n", w->filename);
306 fputs ("GUID: 0x", stdout);
307 print_byte_field(hdr->guid, WIM_GID_LEN);
309 printf("Image Count: %d\n", hdr->image_count);
310 printf("Compression: %s\n", wimlib_get_compression_type_string(
311 wimlib_get_compression_type(w)));
312 printf("Part Number: %d/%d\n", hdr->part_number, hdr->total_parts);
313 printf("Boot Index: %d\n", hdr->boot_idx);
314 printf("Size: %"PRIu64" bytes\n",
315 wim_info_get_total_bytes(w->wim_info));
316 printf("Integrity Info: %s\n", (w->hdr.integrity.size != 0) ? "yes" : "no");
320 WIMLIBAPI bool wimlib_has_integrity_table(const WIMStruct *w)
322 return w->hdr.integrity.size != 0;
325 WIMLIBAPI void wimlib_print_available_images(const WIMStruct *w, int image)
331 if (image == WIM_ALL_IMAGES) {
332 n = printf("Available Images:\n");
334 last = w->hdr.image_count;
335 } else if (image >= 1 && image <= w->hdr.image_count) {
336 n = printf("Information for Image %d\n", image);
340 printf("wimlib_print_available_images(): Invalid image %d",
344 for (i = 0; i < n - 1; i++)
347 for (i = first; i <= last; i++)
348 print_image_info(w->wim_info, i);
352 /* Prints the metadata for the specified image, which may be WIM_ALL_IMAGES, but
353 * not WIM_NO_IMAGE. */
354 WIMLIBAPI int wimlib_print_metadata(WIMStruct *w, int image)
357 return WIMLIB_ERR_INVALID_PARAM;
358 if (w->hdr.part_number != 1) {
359 ERROR("Cannot show the metadata from part %hu of a %hu-part split WIM!",
360 w->hdr.part_number, w->hdr.total_parts);
361 ERROR("Select the first part of the split WIM to see the metadata.");
362 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
364 return for_image(w, image, print_metadata);
367 WIMLIBAPI int wimlib_print_files(WIMStruct *w, int image)
370 return WIMLIB_ERR_INVALID_PARAM;
371 if (w->hdr.part_number != 1) {
372 ERROR("Cannot list the files from part %hu of a %hu-part split WIM!",
373 w->hdr.part_number, w->hdr.total_parts);
374 ERROR("Select the first part of the split WIM if you'd like to list the files.");
375 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
377 return for_image(w, image, print_files);
380 /* Sets the index of the bootable image. */
381 WIMLIBAPI int wimlib_set_boot_idx(WIMStruct *w, int boot_idx)
384 return WIMLIB_ERR_INVALID_PARAM;
385 if (w->hdr.total_parts != 1) {
386 ERROR("Cannot modify the boot index of a split WIM!");
387 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
389 if (boot_idx < 0 || boot_idx > w->hdr.image_count)
390 return WIMLIB_ERR_INVALID_IMAGE;
391 w->hdr.boot_idx = boot_idx;
395 WIMLIBAPI int wimlib_get_part_number(const WIMStruct *w, int *total_parts_ret)
398 *total_parts_ret = w->hdr.total_parts;
399 return w->hdr.part_number;
403 WIMLIBAPI int wimlib_get_boot_idx(const WIMStruct *w)
405 return w->hdr.boot_idx;
409 * Begins the reading of a WIM file; opens the file and reads its header and
410 * lookup table, and optionally checks the integrity.
412 static int begin_read(WIMStruct *w, const char *in_wim_path, int open_flags)
417 DEBUG("Reading the WIM file `%s'", in_wim_path);
419 w->filename = realpath(in_wim_path, NULL);
421 ERROR("Failed to allocate memory for WIM filename");
422 return WIMLIB_ERR_NOMEM;
425 w->fp = fopen(in_wim_path, "rb");
428 ERROR_WITH_ERRNO("Failed to open the file `%s' for reading",
430 return WIMLIB_ERR_OPEN;
433 ret = read_header(w->fp, &w->hdr, open_flags);
437 DEBUG("Wim file contains %u images", w->hdr.image_count);
439 /* If the boot index is invalid, print a warning and set it to 0 */
440 if (w->hdr.boot_idx > w->hdr.image_count) {
441 WARNING("In `%s', image %u is marked as bootable, "
442 "but there are only %u images in the WIM",
443 in_wim_path, w->hdr.boot_idx, w->hdr.image_count);
447 if (wimlib_get_compression_type(w) == WIM_COMPRESSION_TYPE_INVALID) {
448 ERROR("Invalid compression type (WIM header flags = %x)",
450 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
454 if (open_flags & WIMLIB_OPEN_FLAG_CHECK_INTEGRITY) {
455 int integrity_status;
456 ret = check_wim_integrity(w,
457 open_flags & WIMLIB_OPEN_FLAG_SHOW_PROGRESS,
461 if (integrity_status == WIM_INTEGRITY_NONEXISTENT) {
462 WARNING("No integrity information for `%s'; skipping "
463 "integrity check.", w->filename);
464 } else if (integrity_status == WIM_INTEGRITY_NOT_OK) {
465 ERROR("WIM is not intact! (Failed integrity check)");
466 return WIMLIB_ERR_INTEGRITY;
470 if (resource_is_compressed(&w->hdr.lookup_table_res_entry)) {
471 ERROR("Didn't expect a compressed lookup table!");
472 ERROR("Ask the author to implement support for this.");
473 return WIMLIB_ERR_COMPRESSED_LOOKUP_TABLE;
476 ret = read_lookup_table(w);
480 w->image_metadata = CALLOC(w->hdr.image_count,
481 sizeof(struct image_metadata));
483 if (!w->image_metadata) {
484 ERROR("Failed to allocate memory for %u metadata structures",
486 return WIMLIB_ERR_NOMEM;
488 w->current_image = 0;
490 DEBUG("Looking for metadata resources in the lookup table.");
492 /* Find the images in the WIM by searching the lookup table. */
493 ret = for_lookup_table_entry(w->lookup_table,
494 append_metadata_resource_entry, w);
499 /* Make sure all the expected images were found. (We already have
500 * returned false if *extra* images were found) */
501 if (w->current_image != w->hdr.image_count &&
502 w->hdr.part_number == 1)
504 ERROR("Only found %u images in WIM, but expected %u",
505 w->current_image, w->hdr.image_count);
506 return WIMLIB_ERR_IMAGE_COUNT;
510 /* Sort images by the position of their metadata resources. I'm
511 * assuming that is what determines the other of the images in the WIM
512 * file, rather than their order in the lookup table, which is random
513 * because of hashing. */
514 qsort(w->image_metadata, w->current_image,
515 sizeof(struct image_metadata), sort_image_metadata_by_position);
517 w->current_image = WIM_NO_IMAGE;
519 /* Read the XML data. */
520 ret = read_xml_data(w->fp, &w->hdr.xml_res_entry,
521 &w->xml_data, &w->wim_info);
524 ERROR("Missing or invalid XML data");
528 xml_num_images = wim_info_get_num_images(w->wim_info);
529 if (xml_num_images != w->hdr.image_count) {
530 ERROR("In the file `%s', there are %u <IMAGE> elements "
531 "in the XML data,", in_wim_path, xml_num_images);
532 ERROR("but %u images in the WIM! There must be exactly one "
533 "<IMAGE> element per image.", w->hdr.image_count);
534 return WIMLIB_ERR_IMAGE_COUNT;
537 DEBUG("Done beginning read of WIM file `%s'.", in_wim_path);
543 * Opens a WIM file and creates a WIMStruct for it.
545 WIMLIBAPI int wimlib_open_wim(const char *wim_file, int open_flags,
551 DEBUG("wim_file = `%s', open_flags = %#x", wim_file, open_flags);
552 w = new_wim_struct();
554 ERROR("Failed to allocate memory for WIMStruct");
555 return WIMLIB_ERR_NOMEM;
558 ret = begin_read(w, wim_file, open_flags);
562 DEBUG("Could not begin reading the WIM file `%s'", wim_file);
568 /* Frees the memory for the WIMStruct, including all internal memory; also
569 * closes all files associated with the WIMStruct. */
570 WIMLIBAPI void wimlib_free(WIMStruct *w)
572 DEBUG2("Freeing WIMStruct");
582 for (size_t i = 0; i < w->num_allocated_fps; i++)
584 fclose(w->fp_tab[i]);
587 pthread_mutex_destroy(&w->fp_tab_mutex);
589 free_lookup_table(w->lookup_table);
593 free_wim_info(w->wim_info);
594 if (w->image_metadata) {
595 for (uint i = 0; i < w->hdr.image_count; i++)
596 destroy_image_metadata(&w->image_metadata[i], NULL);
597 FREE(w->image_metadata);
601 DEBUG("Unmounting NTFS volume");
602 ntfs_umount(w->ntfs_vol, FALSE);