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 static 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))
234 return read_metadata_resource(w->fp,
235 wim_metadata_resource_entry(w),
236 wimlib_get_compression_type(w),
237 /*wim_security_data(w), */
238 wim_root_dentry_p(w));
242 /* Returns the compression type of the WIM file. */
243 WIMLIBAPI int wimlib_get_compression_type(const WIMStruct *w)
245 return wim_hdr_flags_compression_type(w->hdr.flags);
248 WIMLIBAPI const char *wimlib_get_compression_type_string(int ctype)
251 case WIM_COMPRESSION_TYPE_NONE:
253 case WIM_COMPRESSION_TYPE_LZX:
255 case WIM_COMPRESSION_TYPE_XPRESS:
263 * Returns the number of an image in the WIM file, given a string that is either
264 * the number of the image, or the name of the image. The images are numbered
267 WIMLIBAPI int wimlib_resolve_image(WIMStruct *w, const char *image_name_or_num)
273 if (!image_name_or_num)
276 if (strcmp(image_name_or_num, "all") == 0)
277 return WIM_ALL_IMAGES;
278 image = strtoul(image_name_or_num, &p, 10);
280 if (image < 1 || image > w->hdr.image_count)
284 for (i = 1; i <= w->hdr.image_count; i++) {
285 if (strcmp(image_name_or_num,
286 wimlib_get_image_name(w, i)) == 0)
294 /* Prints some basic information about a WIM file. */
295 WIMLIBAPI void wimlib_print_wim_information(const WIMStruct *w)
297 const struct wim_header *hdr;
300 puts("WIM Information:");
301 puts("----------------");
302 printf("Path: %s\n", w->filename);
303 fputs ("GUID: 0x", stdout);
304 print_byte_field(hdr->guid, WIM_GID_LEN);
306 printf("Image Count: %d\n", hdr->image_count);
307 printf("Compression: %s\n", wimlib_get_compression_type_string(
308 wimlib_get_compression_type(w)));
309 printf("Part Number: %d/%d\n", 1, 1);
310 printf("Boot Index: %d\n", hdr->boot_idx);
311 printf("Size: %"PRIu64" bytes\n",
312 wim_info_get_total_bytes(w->wim_info));
313 printf("Integrity Info: %s\n", (w->hdr.integrity.size != 0) ? "yes" : "no");
317 WIMLIBAPI bool wimlib_has_integrity_table(const WIMStruct *w)
319 return w->hdr.integrity.size != 0;
323 WIMLIBAPI void wimlib_print_available_images(const WIMStruct *w, int image)
325 if (image == WIM_ALL_IMAGES) {
326 puts("Available Images:");
327 puts("-----------------");
332 n = printf("Information for Image %d\n", image);
333 for (i = 0; i < n - 1; i++)
338 print_image_info(w->wim_info, image);
342 /* Prints the metadata for the specified image, which may be WIM_ALL_IMAGES, but
343 * not WIM_NO_IMAGE. */
344 WIMLIBAPI int wimlib_print_metadata(WIMStruct *w, int image)
346 return for_image(w, image, print_metadata);
349 WIMLIBAPI int wimlib_print_files(WIMStruct *w, int image)
351 return for_image(w, image, print_files);
354 /* Sets the index of the bootable image. */
355 WIMLIBAPI int wimlib_set_boot_idx(WIMStruct *w, int boot_idx)
357 if (boot_idx < 0 || boot_idx > w->hdr.image_count)
358 return WIMLIB_ERR_INVALID_IMAGE;
359 w->hdr.boot_idx = boot_idx;
361 memset(&w->hdr.boot_metadata_res_entry, 0,
362 sizeof(struct resource_entry));
364 memcpy(&w->hdr.boot_metadata_res_entry,
365 &w->image_metadata[boot_idx - 1].lookup_table_entry->
367 sizeof(struct resource_entry));
372 WIMLIBAPI int wimlib_get_boot_idx(const WIMStruct *w)
374 return w->hdr.boot_idx;
378 * Begins the reading of a WIM file; opens the file and reads its header and
379 * lookup table, and optionally checks the integrity.
381 static int wim_begin_read(WIMStruct *w, const char *in_wim_path, int flags)
385 int integrity_status;
387 DEBUG("Reading the WIM file `%s'\n", in_wim_path);
389 w->filename = STRDUP(in_wim_path);
391 ERROR("Failed to allocate memory for WIM filename.\n");
392 return WIMLIB_ERR_NOMEM;
395 w->fp = fopen(in_wim_path, "rb");
398 ERROR("Failed to open the file \"%s\" for reading: %m\n",
400 ret = WIMLIB_ERR_OPEN;
404 ret = read_header(w->fp, &w->hdr);
408 DEBUG("Wim file contains %u images\n", w->hdr.image_count);
410 /* If the boot index is invalid, print a warning and set it to 0 */
411 if (w->hdr.boot_idx > w->hdr.image_count) {
412 ERROR("WARNING: In `%s', image %u is marked as bootable,\n",
413 in_wim_path, w->hdr.boot_idx);
414 ERROR(" but there are only %u images!\n",
419 if (wimlib_get_compression_type(w) == WIM_COMPRESSION_TYPE_INVALID) {
420 ERROR("Invalid compression type (WIM header flags "
421 "= %x)\n", w->hdr.flags);
422 ret = WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
427 if (flags & WIMLIB_OPEN_FLAG_CHECK_INTEGRITY) {
428 ret = check_wim_integrity(w,
429 flags & WIMLIB_OPEN_FLAG_SHOW_PROGRESS,
432 ERROR("Error in check_wim_integrity()\n");
435 if (integrity_status == WIM_INTEGRITY_NONEXISTENT) {
436 DEBUG("WARNING: No integrity information; skipping "
437 "integrity check.\n");
438 } else if (integrity_status == WIM_INTEGRITY_NOT_OK) {
439 ERROR("WIM is not intact! (Failed integrity check)\n");
440 ret = WIMLIB_ERR_INTEGRITY;
445 if (resource_is_compressed(&w->hdr.lookup_table_res_entry)) {
446 ERROR("Didn't expect a compressed lookup table!\n");
447 ERROR("Ask the author to implement support for this.\n");
448 ret = WIMLIB_ERR_COMPRESSED_LOOKUP_TABLE;
452 ret = read_lookup_table(w->fp, w->hdr.lookup_table_res_entry.offset,
453 w->hdr.lookup_table_res_entry.size,
459 w->image_metadata = CALLOC(w->hdr.image_count,
460 sizeof(struct image_metadata));
462 if (!w->image_metadata) {
463 ERROR("Failed to allocate memory for %u metadata structures\n",
467 w->current_image = 0;
469 DEBUG("Looking for metadata resources in the lookup table.\n");
471 /* Find the images in the WIM by searching the lookup table. */
472 ret = for_lookup_table_entry(w->lookup_table,
473 append_metadata_resource_entry, w);
478 /* Make sure all the expected images were found. (We already have
479 * returned false if *extra* images were found) */
480 if (w->current_image != w->hdr.image_count) {
481 ERROR("Only found %u images in WIM, but expected %u!\n",
482 w->current_image, w->hdr.image_count);
483 ret = WIMLIB_ERR_IMAGE_COUNT;
487 w->current_image = WIM_NO_IMAGE;
489 /* Sort images by the position of their metadata resources. I'm
490 * assuming that is what determines the other of the images in the WIM
491 * file, rather than their order in the lookup table, which may be
492 * random because of hashing. */
493 qsort(w->image_metadata, w->hdr.image_count,
494 sizeof(struct image_metadata), sort_image_metadata_by_position);
496 /* Read the XML data. */
497 ret = read_xml_data(w->fp, &w->hdr.xml_res_entry,
498 &w->xml_data, &w->wim_info);
501 ERROR("Missing or invalid XML data\n");
505 xml_num_images = wim_info_get_num_images(w->wim_info);
506 if (xml_num_images != w->hdr.image_count) {
507 ERROR("In the file `%s', there are %u <IMAGE> elements "
508 "in the XML data,\n", in_wim_path,
510 ERROR("but %u images in the WIM! There must be "
511 "exactly one <IMAGE> element per image.\n",
513 ret = WIMLIB_ERR_IMAGE_COUNT;
517 DEBUG("Done beginning read of WIM file.\n");
525 * Opens a WIM file and creates a WIMStruct for it.
527 WIMLIBAPI int wimlib_open_wim(const char *wim_file, int flags,
533 DEBUG("wim_file = `%s', flags = %d\n", wim_file, flags);
534 w = new_wim_struct();
536 return WIMLIB_ERR_NOMEM;
538 ret = wim_begin_read(w, wim_file, flags);
540 ERROR("Could not begin reading the WIM file `%s'\n", wim_file);
549 * Frees internal memory allocated by WIMLIB for a WIM file, and closes it if it
552 void wimlib_destroy(WIMStruct *w)
557 /* Frees the memory for the WIMStruct, including all internal memory; also
558 * closes all files associated with the WIMStruct. */
559 WIMLIBAPI void wimlib_free(WIMStruct *w)
570 free_lookup_table(w->lookup_table);
575 free_wim_info(w->wim_info);
576 if (w->image_metadata) {
577 for (i = 0; i < w->hdr.image_count; i++)
578 free_dentry_tree(w->image_metadata[i].root_dentry,
580 FREE(w->image_metadata);