2 * wim.c - Stuff that doesn't fit into any other file
6 * Copyright (C) 2012 Eric Biggers
8 * wimlib - Library for working with WIM files
10 * This file is part of wimlib, a library for working with WIM files.
12 * wimlib is free software; you can redistribute it and/or modify it under the
13 * terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 3 of the License, or (at your option)
17 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
18 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19 * A PARTICULAR PURPOSE. See the GNU General Public License for more
22 * You should have received a copy of the GNU General Public License
23 * along with wimlib; if not, see http://www.gnu.org/licenses/.
38 #include <ntfs-3g/volume.h>
41 #include "wimlib_internal.h"
42 #include "buffer_io.h"
43 #include "lookup_table.h"
46 static int image_print_metadata(WIMStruct *w)
48 DEBUG("Printing metadata for image %d", w->current_image);
49 print_security_data(wim_security_data(w));
50 return for_dentry_in_tree(wim_root_dentry(w), print_dentry,
55 static int image_print_files(WIMStruct *w)
57 return for_dentry_in_tree(wim_root_dentry(w), print_dentry_full_path,
61 static WIMStruct *new_wim_struct()
63 WIMStruct *w = CALLOC(1, sizeof(WIMStruct));
65 if (pthread_mutex_init(&w->fp_tab_mutex, NULL) != 0) {
66 ERROR_WITH_ERRNO("Failed to initialize mutex");
76 * Calls a function on images in the WIM. If @image is WIMLIB_ALL_IMAGES, @visitor
77 * is called on the WIM once for each image, with each image selected as the
78 * current image in turn. If @image is a certain image, @visitor is called on
79 * the WIM only once, with that image selected.
81 int for_image(WIMStruct *w, int image, int (*visitor)(WIMStruct *))
88 if (image == WIMLIB_ALL_IMAGES) {
90 end = w->hdr.image_count;
91 } else if (image >= 1 && image <= w->hdr.image_count) {
95 return WIMLIB_ERR_INVALID_IMAGE;
97 for (i = start; i <= end; i++) {
98 ret = select_wim_image(w, i);
108 static int sort_image_metadata_by_position(const void *p1, const void *p2)
110 const struct wim_image_metadata *imd1 = p1;
111 const struct wim_image_metadata *imd2 = p2;
112 u64 offset1 = imd1->metadata_lte->resource_entry.offset;
113 u64 offset2 = imd2->metadata_lte->resource_entry.offset;
114 if (offset1 < offset2)
116 else if (offset1 > offset2)
123 * If @lte points to a metadata resource, append it to the list of metadata
124 * resources in the WIMStruct. Otherwise, do nothing.
126 static int append_metadata_resource_entry(struct wim_lookup_table_entry *lte,
129 WIMStruct *w = wim_p;
132 if (lte->resource_entry.flags & WIM_RESHDR_FLAG_METADATA) {
133 if (w->current_image == w->hdr.image_count) {
134 ERROR("The WIM header says there are %u images in the WIM,\n"
135 " but we found more metadata resources than this",
137 ret = WIMLIB_ERR_IMAGE_COUNT;
139 DEBUG("Found metadata resource for image %u at "
141 w->current_image + 1,
142 lte->resource_entry.offset);
144 w->current_image++].metadata_lte = lte;
150 /* Returns the compression type given in the flags of a WIM header. */
151 static int wim_hdr_flags_compression_type(int wim_hdr_flags)
153 if (wim_hdr_flags & WIM_HDR_FLAG_COMPRESSION) {
154 if (wim_hdr_flags & WIM_HDR_FLAG_COMPRESS_LZX)
155 return WIMLIB_COMPRESSION_TYPE_LZX;
156 else if (wim_hdr_flags & WIM_HDR_FLAG_COMPRESS_XPRESS)
157 return WIMLIB_COMPRESSION_TYPE_XPRESS;
159 return WIMLIB_COMPRESSION_TYPE_INVALID;
161 return WIMLIB_COMPRESSION_TYPE_NONE;
166 * Creates a WIMStruct for a new WIM file.
168 WIMLIBAPI int wimlib_create_new_wim(int ctype, WIMStruct **w_ret)
171 struct wim_lookup_table *table;
174 DEBUG("Creating new WIM with %s compression.",
175 wimlib_get_compression_type_string(ctype));
177 /* Allocate the WIMStruct. */
178 w = new_wim_struct();
180 return WIMLIB_ERR_NOMEM;
182 ret = init_header(&w->hdr, ctype);
186 table = new_lookup_table(9001);
188 ret = WIMLIB_ERR_NOMEM;
191 w->lookup_table = table;
199 WIMLIBAPI int wimlib_get_num_images(const WIMStruct *w)
201 return w->hdr.image_count;
204 int select_wim_image(WIMStruct *w, int image)
206 struct wim_image_metadata *imd;
209 DEBUG("Selecting image %d", image);
211 if (image == WIMLIB_NO_IMAGE) {
212 ERROR("Invalid image: %d", WIMLIB_NO_IMAGE);
213 return WIMLIB_ERR_INVALID_IMAGE;
216 if (image == w->current_image)
219 if (image < 1 || image > w->hdr.image_count) {
220 ERROR("Cannot select image %d: There are only %u images",
221 image, w->hdr.image_count);
222 return WIMLIB_ERR_INVALID_IMAGE;
225 /* If a valid image is currently selected, it can be freed if it is not
227 if (w->current_image != WIMLIB_NO_IMAGE) {
228 imd = wim_get_current_image_metadata(w);
229 if (!imd->modified) {
230 DEBUG("Freeing image %u", w->current_image);
231 destroy_image_metadata(imd, NULL);
232 imd->root_dentry = NULL;
233 imd->security_data = NULL;
234 INIT_HLIST_HEAD(&imd->inode_list);
237 w->current_image = image;
238 imd = &w->image_metadata[image - 1];
239 if (imd->root_dentry) {
243 DEBUG("Reading metadata resource specified by the following "
244 "lookup table entry:");
245 print_lookup_table_entry(imd->metadata_lte, stdout);
247 ret = read_metadata_resource(w, imd);
249 w->current_image = WIMLIB_NO_IMAGE;
255 /* Returns the compression type of the WIM file. */
256 WIMLIBAPI int wimlib_get_compression_type(const WIMStruct *w)
258 return wim_hdr_flags_compression_type(w->hdr.flags);
261 WIMLIBAPI const char *wimlib_get_compression_type_string(int ctype)
264 case WIMLIB_COMPRESSION_TYPE_NONE:
266 case WIMLIB_COMPRESSION_TYPE_LZX:
268 case WIMLIB_COMPRESSION_TYPE_XPRESS:
276 * Returns the number of an image in the WIM file, given a string that is either
277 * the number of the image, or the name of the image. The images are numbered
280 WIMLIBAPI int wimlib_resolve_image(WIMStruct *w, const char *image_name_or_num)
286 if (!image_name_or_num || !*image_name_or_num)
287 return WIMLIB_NO_IMAGE;
289 if (strcmp(image_name_or_num, "all") == 0
290 || strcmp(image_name_or_num, "*") == 0)
291 return WIMLIB_ALL_IMAGES;
292 image = strtol(image_name_or_num, &p, 10);
293 if (p != image_name_or_num && *p == '\0' && image > 0) {
294 if (image > w->hdr.image_count)
295 return WIMLIB_NO_IMAGE;
298 for (i = 1; i <= w->hdr.image_count; i++) {
299 if (strcmp(image_name_or_num,
300 wimlib_get_image_name(w, i)) == 0)
303 return WIMLIB_NO_IMAGE;
308 /* Prints some basic information about a WIM file. */
309 WIMLIBAPI void wimlib_print_wim_information(const WIMStruct *w)
311 const struct wim_header *hdr;
314 puts("WIM Information:");
315 puts("----------------");
316 printf("Path: %s\n", w->filename);
317 fputs ("GUID: 0x", stdout);
318 print_byte_field(hdr->guid, WIM_GID_LEN);
320 printf("Image Count: %d\n", hdr->image_count);
321 printf("Compression: %s\n", wimlib_get_compression_type_string(
322 wimlib_get_compression_type(w)));
323 printf("Part Number: %d/%d\n", hdr->part_number, hdr->total_parts);
324 printf("Boot Index: %d\n", hdr->boot_idx);
325 printf("Size: %"PRIu64" bytes\n",
326 wim_info_get_total_bytes(w->wim_info));
327 printf("Integrity Info: %s\n", (w->hdr.integrity.offset != 0) ? "yes" : "no");
331 WIMLIBAPI bool wimlib_has_integrity_table(const WIMStruct *w)
333 return w->hdr.integrity.size != 0;
336 WIMLIBAPI void wimlib_print_available_images(const WIMStruct *w, int image)
342 if (image == WIMLIB_ALL_IMAGES) {
343 n = printf("Available Images:\n");
345 last = w->hdr.image_count;
346 } else if (image >= 1 && image <= w->hdr.image_count) {
347 n = printf("Information for Image %d\n", image);
351 printf("wimlib_print_available_images(): Invalid image %d",
355 for (i = 0; i < n - 1; i++)
358 for (i = first; i <= last; i++)
359 print_image_info(w->wim_info, i);
363 /* Prints the metadata for the specified image, which may be WIMLIB_ALL_IMAGES, but
364 * not WIMLIB_NO_IMAGE. */
365 WIMLIBAPI int wimlib_print_metadata(WIMStruct *w, int image)
367 if (w->hdr.part_number != 1) {
368 ERROR("Cannot show the metadata from part %hu of a %hu-part split WIM!",
369 w->hdr.part_number, w->hdr.total_parts);
370 ERROR("Select the first part of the split WIM to see the metadata.");
371 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
373 return for_image(w, image, image_print_metadata);
376 WIMLIBAPI int wimlib_print_files(WIMStruct *w, int image)
378 if (w->hdr.part_number != 1) {
379 ERROR("Cannot list the files from part %hu of a %hu-part split WIM!",
380 w->hdr.part_number, w->hdr.total_parts);
381 ERROR("Select the first part of the split WIM if you'd like to list the files.");
382 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
384 return for_image(w, image, image_print_files);
387 /* Sets the index of the bootable image. */
388 WIMLIBAPI int wimlib_set_boot_idx(WIMStruct *w, int boot_idx)
390 if (w->hdr.total_parts != 1) {
391 ERROR("Cannot modify the boot index of a split WIM!");
392 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
394 if (boot_idx < 0 || boot_idx > w->hdr.image_count)
395 return WIMLIB_ERR_INVALID_IMAGE;
396 w->hdr.boot_idx = boot_idx;
399 memset(&w->hdr.boot_metadata_res_entry, 0,
400 sizeof(struct resource_entry));
402 memcpy(&w->hdr.boot_metadata_res_entry,
404 boot_idx - 1].metadata_lte->resource_entry,
405 sizeof(struct resource_entry));
411 WIMLIBAPI int wimlib_get_part_number(const WIMStruct *w, int *total_parts_ret)
414 *total_parts_ret = w->hdr.total_parts;
415 return w->hdr.part_number;
419 WIMLIBAPI int wimlib_get_boot_idx(const WIMStruct *w)
421 return w->hdr.boot_idx;
425 * Begins the reading of a WIM file; opens the file and reads its header and
426 * lookup table, and optionally checks the integrity.
428 static int begin_read(WIMStruct *w, const char *in_wim_path, int open_flags,
429 wimlib_progress_func_t progress_func)
434 DEBUG("Reading the WIM file `%s'", in_wim_path);
436 w->fp = fopen(in_wim_path, "rb");
438 ERROR_WITH_ERRNO("Failed to open `%s' for reading",
440 return WIMLIB_ERR_OPEN;
443 /* The absolute path to the WIM is requested so that wimlib_overwrite()
444 * still works even if the process changes its working directory. This
445 * actually happens if a WIM is mounted read-write, since the FUSE
446 * thread changes directory to "/", and it needs to be able to find the
449 * This will break if the full path to the WIM changes in the
450 * intervening time...
452 w->filename = realpath(in_wim_path, NULL);
454 ERROR_WITH_ERRNO("Failed to resolve WIM filename");
456 return WIMLIB_ERR_NOMEM;
458 return WIMLIB_ERR_OPEN;
461 ret = read_header(w->fp, &w->hdr, open_flags);
465 DEBUG("According to header, WIM contains %u images", w->hdr.image_count);
467 /* If the boot index is invalid, print a warning and set it to 0 */
468 if (w->hdr.boot_idx > w->hdr.image_count) {
469 WARNING("In `%s', image %u is marked as bootable, "
470 "but there are only %u images in the WIM",
471 in_wim_path, w->hdr.boot_idx, w->hdr.image_count);
475 if (wimlib_get_compression_type(w) == WIMLIB_COMPRESSION_TYPE_INVALID) {
476 ERROR("Invalid compression type (WIM header flags = 0x%x)",
478 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
481 if (open_flags & WIMLIB_OPEN_FLAG_CHECK_INTEGRITY) {
482 ret = check_wim_integrity(w, progress_func);
483 if (ret == WIM_INTEGRITY_NONEXISTENT) {
484 WARNING("No integrity information for `%s'; skipping "
485 "integrity check.", in_wim_path);
486 } else if (ret == WIM_INTEGRITY_NOT_OK) {
487 ERROR("WIM is not intact! (Failed integrity check)");
488 return WIMLIB_ERR_INTEGRITY;
489 } else if (ret != WIM_INTEGRITY_OK) {
494 ret = read_lookup_table(w);
498 if (w->hdr.image_count != 0) {
499 w->image_metadata = CALLOC(w->hdr.image_count,
500 sizeof(struct wim_image_metadata));
502 if (!w->image_metadata) {
503 ERROR("Failed to allocate memory for %u image metadata structures",
505 return WIMLIB_ERR_NOMEM;
508 w->current_image = 0;
510 DEBUG("Looking for metadata resources in the lookup table.");
512 /* Find the images in the WIM by searching the lookup table. */
513 ret = for_lookup_table_entry(w->lookup_table,
514 append_metadata_resource_entry, w);
519 /* Make sure all the expected images were found. (We already have
520 * returned WIMLIB_ERR_IMAGE_COUNT if *extra* images were found) */
521 if (w->current_image != w->hdr.image_count &&
522 w->hdr.part_number == 1)
524 ERROR("Only found %d images in WIM, but expected %u",
525 w->current_image, w->hdr.image_count);
526 return WIMLIB_ERR_IMAGE_COUNT;
529 /* Sort images by the position of their metadata resources. I'm
530 * assuming that is what determines the other of the images in the WIM
531 * file, rather than their order in the lookup table, which is random
532 * because of hashing. */
533 qsort(w->image_metadata, w->current_image,
534 sizeof(struct wim_image_metadata), sort_image_metadata_by_position);
536 w->current_image = WIMLIB_NO_IMAGE;
538 /* Read the XML data. */
539 ret = read_xml_data(w->fp, &w->hdr.xml_res_entry,
540 &w->xml_data, &w->wim_info);
545 xml_num_images = wim_info_get_num_images(w->wim_info);
546 if (xml_num_images != w->hdr.image_count) {
547 ERROR("In the file `%s', there are %u <IMAGE> elements "
548 "in the XML data,", in_wim_path, xml_num_images);
549 ERROR("but %u images in the WIM! There must be exactly one "
550 "<IMAGE> element per image.", w->hdr.image_count);
551 return WIMLIB_ERR_IMAGE_COUNT;
554 DEBUG("Done beginning read of WIM file `%s'.", in_wim_path);
560 * Opens a WIM file and creates a WIMStruct for it.
562 WIMLIBAPI int wimlib_open_wim(const char *wim_file, int open_flags,
564 wimlib_progress_func_t progress_func)
569 if (!wim_file || !w_ret)
570 return WIMLIB_ERR_INVALID_PARAM;
572 w = new_wim_struct();
574 return WIMLIB_ERR_NOMEM;
576 ret = begin_read(w, wim_file, open_flags, progress_func);
584 void destroy_image_metadata(struct wim_image_metadata *imd,
585 struct wim_lookup_table *table)
587 free_dentry_tree(imd->root_dentry, table);
588 free_security_data(imd->security_data);
590 /* Get rid of the lookup table entry for this image's metadata resource
593 lookup_table_unlink(table, imd->metadata_lte);
594 free_lookup_table_entry(imd->metadata_lte);
598 /* Frees the memory for the WIMStruct, including all internal memory; also
599 * closes all files associated with the WIMStruct. */
600 WIMLIBAPI void wimlib_free(WIMStruct *w)
602 DEBUG2("Freeing WIMStruct");
613 for (size_t i = 0; i < w->num_allocated_fps; i++)
615 fclose(w->fp_tab[i]);
618 pthread_mutex_destroy(&w->fp_tab_mutex);
621 free_lookup_table(w->lookup_table);
625 free_wim_info(w->wim_info);
626 if (w->image_metadata) {
627 for (unsigned i = 0; i < w->hdr.image_count; i++)
628 destroy_image_metadata(&w->image_metadata[i], NULL);
629 FREE(w->image_metadata);
633 DEBUG("Unmounting NTFS volume");
634 ntfs_umount(w->ntfs_vol, FALSE);
640 /* Get global memory allocations out of the way. Not strictly necessary in
641 * single-threaded programs like 'imagex'. */
642 WIMLIBAPI int wimlib_global_init()
644 libxml_global_init();
645 return iconv_global_init();
648 /* Free global memory allocations. Not strictly necessary if the process using
649 * wimlib is just about to exit (as is the case for 'imagex'). */
650 WIMLIBAPI void wimlib_global_cleanup()
652 libxml_global_cleanup();
653 iconv_global_cleanup();