5 * Copyright (C) 2010 Carl Thijssen
6 * Copyright (C) 2012 Eric Biggers
8 * wimlib - Library for working with WIM files
10 * This library is free software; you can redistribute it and/or modify it under
11 * the terms of the GNU Lesser General Public License as published by the Free
12 * Software Foundation; either version 2.1 of the License, or (at your option) any
15 * This library is distributed in the hope that it will be useful, but WITHOUT ANY
16 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
17 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License along
20 * with this library; if not, write to the Free Software Foundation, Inc., 59
21 * Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "wimlib_internal.h"
26 #include "lookup_table.h"
30 static int print_metadata(WIMStruct *w)
33 print_security_data(wim_security_data(w));
35 return for_dentry_in_tree(wim_root_dentry(w), print_dentry,
40 static int print_files(WIMStruct *w)
42 return for_dentry_in_tree(wim_root_dentry(w), print_dentry_full_path,
46 WIMStruct *new_wim_struct()
50 w = CALLOC(1, sizeof(WIMStruct));
53 w->link_type = WIM_LINK_TYPE_HARD;
54 w->current_image = WIM_NO_IMAGE;
59 * Calls a function on images in the WIM. If @image is WIM_ALL_IMAGES, @visitor
60 * is called on the WIM once for each image, with each image selected as the
61 * current image in turn. If @image is a certain image, @visitor is called on
62 * the WIM only once, with that image selected.
64 int for_image(WIMStruct *w, int image, int (*visitor)(WIMStruct *))
70 DEBUG("for_image(w = %p, image = %d, visitor = %p)\n",
73 if (image == WIM_ALL_IMAGES) {
74 image_count = w->hdr.image_count;
75 for (i = 1; i <= image_count; i++) {
76 ret = wimlib_select_image(w, i);
84 ret = wimlib_select_image(w, image);
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->lookup_table_entry->resource_entry.offset;
97 u64 offset2 = bmd2->lookup_table_entry->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.
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 if (w->current_image == w->hdr.image_count) {
117 ERROR("Expected only %u images, but found more!\n",
119 return WIMLIB_ERR_IMAGE_COUNT;
121 DEBUG("Found metadata resource for image %u at "
122 "offset %"PRIu64"\n", w->current_image + 1,
123 lte->resource_entry.offset);
125 w->current_image++].lookup_table_entry = lte;
129 /* Do nothing if not a metadata resource. */
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;
148 int wim_resource_compression_type(const WIMStruct *w,
149 const struct resource_entry *entry)
151 int wim_ctype = wimlib_get_compression_type(w);
152 return resource_compression_type(wim_ctype, entry->flags);
155 WIMLIBAPI void wimlib_set_verbose(WIMStruct *w, bool verbose)
157 w->verbose = verbose;
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\n",
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 wimlib_select_image(WIMStruct *w, int image)
201 DEBUG("Selecting image %u\n", image);
203 if (image == w->current_image)
206 if (image > w->hdr.image_count || image < 1) {
207 ERROR("Cannot select image %u: There are only %u images!\n",
208 image, w->hdr.image_count);
209 return WIMLIB_ERR_INVALID_IMAGE;
213 /* If a valid image is selected, it can be freed if it is not modified.
215 if (w->current_image != WIM_NO_IMAGE &&
216 !wim_current_image_is_modified(w)) {
218 struct image_metadata *imd;
220 DEBUG("Freeing image %u\n", w->current_image);
221 imd = wim_get_current_image_metadata(w);
222 free_dentry_tree(imd->root_dentry, NULL, false);
224 destroy_security_data(&imd->security_data);
226 imd->root_dentry = NULL;
229 w->current_image = image;
231 if (wim_root_dentry(w)) {
235 DEBUG("Reading metadata resource specified by the following "
236 "lookup table entry:\n");
237 print_lookup_table_entry(wim_metadata_lookup_table_entry(w), NULL);
239 return read_metadata_resource(w->fp,
240 wim_metadata_resource_entry(w),
241 wimlib_get_compression_type(w),
242 /*wim_security_data(w), */
243 wim_root_dentry_p(w));
248 /* Returns the compression type of the WIM file. */
249 WIMLIBAPI int wimlib_get_compression_type(const WIMStruct *w)
251 return wim_hdr_flags_compression_type(w->hdr.flags);
254 WIMLIBAPI const char *wimlib_get_compression_type_string(int ctype)
257 case WIM_COMPRESSION_TYPE_NONE:
259 case WIM_COMPRESSION_TYPE_LZX:
261 case WIM_COMPRESSION_TYPE_XPRESS:
269 * Returns the number of an image in the WIM file, given a string that is either
270 * the number of the image, or the name of the image. The images are numbered
273 WIMLIBAPI int wimlib_resolve_image(WIMStruct *w, const char *image_name_or_num)
279 if (!image_name_or_num)
282 if (strcmp(image_name_or_num, "all") == 0)
283 return WIM_ALL_IMAGES;
284 image = strtoul(image_name_or_num, &p, 10);
286 if (image < 1 || image > w->hdr.image_count)
290 for (i = 1; i <= w->hdr.image_count; i++) {
291 if (strcmp(image_name_or_num,
292 wimlib_get_image_name(w, i)) == 0)
300 /* Prints some basic information about a WIM file. */
301 WIMLIBAPI void wimlib_print_wim_information(const WIMStruct *w)
303 const struct wim_header *hdr;
306 puts("WIM Information:");
307 puts("----------------");
308 printf("Path: %s\n", w->filename);
309 fputs ("GUID: 0x", stdout);
310 print_byte_field(hdr->guid, WIM_GID_LEN);
312 printf("Image Count: %d\n", hdr->image_count);
313 printf("Compression: %s\n", wimlib_get_compression_type_string(
314 wimlib_get_compression_type(w)));
315 printf("Part Number: %d/%d\n", hdr->part_number, hdr->total_parts);
316 printf("Boot Index: %d\n", hdr->boot_idx);
317 printf("Size: %"PRIu64" bytes\n",
318 wim_info_get_total_bytes(w->wim_info));
319 printf("Integrity Info: %s\n", (w->hdr.integrity.size != 0) ? "yes" : "no");
323 WIMLIBAPI bool wimlib_has_integrity_table(const WIMStruct *w)
325 return w->hdr.integrity.size != 0;
329 WIMLIBAPI void wimlib_print_available_images(const WIMStruct *w, int image)
331 if (image == WIM_ALL_IMAGES) {
332 puts("Available Images:");
333 puts("-----------------");
338 n = printf("Information for Image %d\n", image);
339 for (i = 0; i < n - 1; i++)
344 print_image_info(w->wim_info, image);
348 /* Prints the metadata for the specified image, which may be WIM_ALL_IMAGES, but
349 * not WIM_NO_IMAGE. */
350 WIMLIBAPI int wimlib_print_metadata(WIMStruct *w, int image)
352 return for_image(w, image, print_metadata);
355 WIMLIBAPI int wimlib_print_files(WIMStruct *w, int image)
357 return for_image(w, image, print_files);
360 /* Sets the index of the bootable image. */
361 WIMLIBAPI int wimlib_set_boot_idx(WIMStruct *w, int boot_idx)
363 if (boot_idx < 0 || boot_idx > w->hdr.image_count)
364 return WIMLIB_ERR_INVALID_IMAGE;
365 w->hdr.boot_idx = boot_idx;
367 memset(&w->hdr.boot_metadata_res_entry, 0,
368 sizeof(struct resource_entry));
370 memcpy(&w->hdr.boot_metadata_res_entry,
371 &w->image_metadata[boot_idx - 1].lookup_table_entry->
373 sizeof(struct resource_entry));
378 WIMLIBAPI int wimlib_get_part_number(const WIMStruct *w, int *total_parts_ret)
381 *total_parts_ret = w->hdr.total_parts;
382 return w->hdr.part_number;
386 WIMLIBAPI int wimlib_get_boot_idx(const WIMStruct *w)
388 return w->hdr.boot_idx;
392 * Begins the reading of a WIM file; opens the file and reads its header and
393 * lookup table, and optionally checks the integrity.
395 static int begin_read(WIMStruct *w, const char *in_wim_path, int flags)
399 int integrity_status;
401 DEBUG("Reading the WIM file `%s'\n", in_wim_path);
403 w->filename = STRDUP(in_wim_path);
405 ERROR("Failed to allocate memory for WIM filename.\n");
406 return WIMLIB_ERR_NOMEM;
409 w->fp = fopen(in_wim_path, "rb");
412 ERROR("Failed to open the file \"%s\" for reading: %m\n",
414 ret = WIMLIB_ERR_OPEN;
418 ret = read_header(w->fp, &w->hdr, flags & WIMLIB_OPEN_FLAG_SPLIT_OK);
422 DEBUG("Wim file contains %u images\n", w->hdr.image_count);
424 /* If the boot index is invalid, print a warning and set it to 0 */
425 if (w->hdr.boot_idx > w->hdr.image_count) {
426 WARNING("In `%s', image %u is marked as bootable,\n"
427 "\tbut there are only %u images!\n",
428 in_wim_path, w->hdr.boot_idx, w->hdr.image_count);
432 if (wimlib_get_compression_type(w) == WIM_COMPRESSION_TYPE_INVALID) {
433 ERROR("Invalid compression type (WIM header flags "
434 "= %x)\n", w->hdr.flags);
435 ret = WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
440 if (flags & WIMLIB_OPEN_FLAG_CHECK_INTEGRITY) {
441 ret = check_wim_integrity(w,
442 flags & WIMLIB_OPEN_FLAG_SHOW_PROGRESS,
445 ERROR("Error in check_wim_integrity()\n");
448 if (integrity_status == WIM_INTEGRITY_NONEXISTENT) {
449 WARNING("No integrity information for `%s'; skipping "
450 "integrity check.\n", w->filename);
451 } else if (integrity_status == WIM_INTEGRITY_NOT_OK) {
452 ERROR("WIM is not intact! (Failed integrity check)\n");
453 ret = WIMLIB_ERR_INTEGRITY;
458 if (resource_is_compressed(&w->hdr.lookup_table_res_entry)) {
459 ERROR("Didn't expect a compressed lookup table!\n");
460 ERROR("Ask the author to implement support for this.\n");
461 ret = WIMLIB_ERR_COMPRESSED_LOOKUP_TABLE;
465 ret = read_lookup_table(w->fp, w->hdr.lookup_table_res_entry.offset,
466 w->hdr.lookup_table_res_entry.size,
472 w->image_metadata = CALLOC(w->hdr.image_count,
473 sizeof(struct image_metadata));
475 if (!w->image_metadata) {
476 ERROR("Failed to allocate memory for %u metadata structures\n",
480 w->current_image = 0;
482 DEBUG("Looking for metadata resources in the lookup table.\n");
484 /* Find the images in the WIM by searching the lookup table. */
485 ret = for_lookup_table_entry(w->lookup_table,
486 append_metadata_resource_entry, w);
488 if (ret != 0 && w->hdr.part_number == 1)
491 /* Make sure all the expected images were found. (We already have
492 * returned false if *extra* images were found) */
493 if (w->current_image != w->hdr.image_count && w->hdr.part_number == 1) {
494 ERROR("Only found %u images in WIM, but expected %u!\n",
495 w->current_image, w->hdr.image_count);
496 ret = WIMLIB_ERR_IMAGE_COUNT;
501 /* Sort images by the position of their metadata resources. I'm
502 * assuming that is what determines the other of the images in the WIM
503 * file, rather than their order in the lookup table, which may be
504 * random because of hashing. */
505 qsort(w->image_metadata, w->current_image,
506 sizeof(struct image_metadata), sort_image_metadata_by_position);
508 w->current_image = WIM_NO_IMAGE;
510 /* Read the XML data. */
511 ret = read_xml_data(w->fp, &w->hdr.xml_res_entry,
512 &w->xml_data, &w->wim_info);
515 ERROR("Missing or invalid XML data\n");
519 xml_num_images = wim_info_get_num_images(w->wim_info);
520 if (xml_num_images != w->hdr.image_count) {
521 ERROR("In the file `%s', there are %u <IMAGE> elements "
522 "in the XML data,\n", in_wim_path,
524 ERROR("but %u images in the WIM! There must be "
525 "exactly one <IMAGE> element per image.\n",
527 ret = WIMLIB_ERR_IMAGE_COUNT;
531 DEBUG("Done beginning read of WIM file.\n");
539 * Opens a WIM file and creates a WIMStruct for it.
541 WIMLIBAPI int wimlib_open_wim(const char *wim_file, int flags,
547 DEBUG("wim_file = `%s', flags = %d\n", wim_file, flags);
548 w = new_wim_struct();
550 return WIMLIB_ERR_NOMEM;
552 ret = begin_read(w, wim_file, flags);
554 ERROR("Could not begin reading the WIM file `%s'\n", wim_file);
562 /* Frees the memory for the WIMStruct, including all internal memory; also
563 * closes all files associated with the WIMStruct. */
564 WIMLIBAPI void wimlib_free(WIMStruct *w)
575 free_lookup_table(w->lookup_table);
580 free_wim_info(w->wim_info);
581 if (w->image_metadata) {
582 for (i = 0; i < w->hdr.image_count; i++)
583 free_dentry_tree(w->image_metadata[i].root_dentry,
585 FREE(w->image_metadata);