2 * wim.c - Stuff that doesn't fit into any other file
6 * Copyright (C) 2012, 2013 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 <langinfo.h>
41 #include "buffer_io.h"
43 #include "lookup_table.h"
44 #include "wimlib_internal.h"
48 image_print_metadata(WIMStruct *w)
50 DEBUG("Printing metadata for image %d", w->current_image);
51 print_security_data(wim_security_data(w));
52 return for_dentry_in_tree(wim_root_dentry(w), print_dentry,
58 image_print_files(WIMStruct *w)
60 return for_dentry_in_tree(wim_root_dentry(w), print_dentry_full_path,
67 WIMStruct *w = CALLOC(1, sizeof(WIMStruct));
69 w->in_fd = INVALID_FILEDES;
70 w->out_fd = INVALID_FILEDES;
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.
82 for_image(WIMStruct *w, int image, int (*visitor)(WIMStruct *))
89 if (image == WIMLIB_ALL_IMAGES) {
91 end = w->hdr.image_count;
92 } else if (image >= 1 && image <= w->hdr.image_count) {
96 return WIMLIB_ERR_INVALID_IMAGE;
98 for (i = start; i <= end; i++) {
99 ret = select_wim_image(w, i);
109 /* Returns the compression type given in the flags of a WIM header. */
111 wim_hdr_flags_compression_type(int wim_hdr_flags)
113 if (wim_hdr_flags & WIM_HDR_FLAG_COMPRESSION) {
114 if (wim_hdr_flags & WIM_HDR_FLAG_COMPRESS_LZX)
115 return WIMLIB_COMPRESSION_TYPE_LZX;
116 else if (wim_hdr_flags & WIM_HDR_FLAG_COMPRESS_XPRESS)
117 return WIMLIB_COMPRESSION_TYPE_XPRESS;
119 return WIMLIB_COMPRESSION_TYPE_INVALID;
121 return WIMLIB_COMPRESSION_TYPE_NONE;
126 * Creates a WIMStruct for a new WIM file.
129 wimlib_create_new_wim(int ctype, WIMStruct **w_ret)
132 struct wim_lookup_table *table;
135 DEBUG("Creating new WIM with %"TS" compression.",
136 wimlib_get_compression_type_string(ctype));
138 /* Allocate the WIMStruct. */
139 w = new_wim_struct();
141 return WIMLIB_ERR_NOMEM;
143 ret = init_header(&w->hdr, ctype);
147 table = new_lookup_table(9001);
149 ret = WIMLIB_ERR_NOMEM;
152 w->lookup_table = table;
161 wimlib_get_num_images(const WIMStruct *w)
163 return w->hdr.image_count;
167 select_wim_image(WIMStruct *w, int image)
169 struct wim_image_metadata *imd;
172 DEBUG("Selecting image %d", image);
174 if (image == WIMLIB_NO_IMAGE) {
175 ERROR("Invalid image: %d", WIMLIB_NO_IMAGE);
176 return WIMLIB_ERR_INVALID_IMAGE;
179 if (image == w->current_image)
182 if (image < 1 || image > w->hdr.image_count) {
183 ERROR("Cannot select image %d: There are only %u images",
184 image, w->hdr.image_count);
185 return WIMLIB_ERR_INVALID_IMAGE;
188 /* If a valid image is currently selected, it can be freed if it is not
190 if (w->current_image != WIMLIB_NO_IMAGE) {
191 imd = wim_get_current_image_metadata(w);
192 if (!imd->modified) {
193 wimlib_assert(list_empty(&imd->unhashed_streams));
194 DEBUG("Freeing image %u", w->current_image);
195 destroy_image_metadata(imd, NULL, false);
198 w->current_image = image;
199 imd = wim_get_current_image_metadata(w);
200 if (imd->root_dentry) {
204 DEBUG("Reading metadata resource specified by the following "
205 "lookup table entry:");
206 print_lookup_table_entry(imd->metadata_lte, stderr);
208 ret = read_metadata_resource(w, imd);
210 w->current_image = WIMLIB_NO_IMAGE;
216 /* Returns the compression type of the WIM file. */
218 wimlib_get_compression_type(const WIMStruct *w)
220 return wim_hdr_flags_compression_type(w->hdr.flags);
223 WIMLIBAPI const tchar *
224 wimlib_get_compression_type_string(int ctype)
227 case WIMLIB_COMPRESSION_TYPE_NONE:
229 case WIMLIB_COMPRESSION_TYPE_LZX:
231 case WIMLIB_COMPRESSION_TYPE_XPRESS:
239 * Returns the number of an image in the WIM file, given a string that is either
240 * the number of the image, or the name of the image. The images are numbered
244 wimlib_resolve_image(WIMStruct *w, const tchar *image_name_or_num)
250 if (!image_name_or_num || !*image_name_or_num)
251 return WIMLIB_NO_IMAGE;
253 if (!tstrcasecmp(image_name_or_num, T("all"))
254 || !tstrcasecmp(image_name_or_num, T("*")))
255 return WIMLIB_ALL_IMAGES;
256 image = tstrtol(image_name_or_num, &p, 10);
257 if (p != image_name_or_num && *p == T('\0') && image > 0) {
258 if (image > w->hdr.image_count)
259 return WIMLIB_NO_IMAGE;
262 for (i = 1; i <= w->hdr.image_count; i++) {
263 if (!tstrcmp(image_name_or_num,
264 wimlib_get_image_name(w, i)))
267 return WIMLIB_NO_IMAGE;
271 /* Prints some basic information about a WIM file. */
273 wimlib_print_wim_information(const WIMStruct *w)
275 const struct wim_header *hdr;
278 tputs(T("WIM Information:"));
279 tputs(T("----------------"));
280 tprintf(T("Path: %"TS"\n"), w->filename);
281 tfputs(T("GUID: 0x"), stdout);
282 print_byte_field(hdr->guid, WIM_GID_LEN, stdout);
284 tprintf(T("Image Count: %d\n"), hdr->image_count);
285 tprintf(T("Compression: %"TS"\n"),
286 wimlib_get_compression_type_string(wimlib_get_compression_type(w)));
287 tprintf(T("Part Number: %d/%d\n"), hdr->part_number, hdr->total_parts);
288 tprintf(T("Boot Index: %d\n"), hdr->boot_idx);
289 tprintf(T("Size: %"PRIu64" bytes\n"),
290 wim_info_get_total_bytes(w->wim_info));
291 tprintf(T("Integrity Info: %"TS"\n"),
292 (w->hdr.integrity.offset != 0) ? T("yes") : T("no"));
293 tprintf(T("Relative path junction: %"TS"\n"),
294 (hdr->flags & WIM_HDR_FLAG_RP_FIX) ? T("yes") : T("no"));
299 wimlib_has_integrity_table(const WIMStruct *w)
301 return w->hdr.integrity.size != 0;
305 wimlib_print_available_images(const WIMStruct *w, int image)
311 if (image == WIMLIB_ALL_IMAGES) {
312 n = tprintf(T("Available Images:\n"));
314 last = w->hdr.image_count;
315 } else if (image >= 1 && image <= w->hdr.image_count) {
316 n = tprintf(T("Information for Image %d\n"), image);
320 tprintf(T("wimlib_print_available_images(): Invalid image %d"),
324 for (i = 0; i < n - 1; i++)
327 for (i = first; i <= last; i++)
328 print_image_info(w->wim_info, i);
332 /* Prints the metadata for the specified image, which may be WIMLIB_ALL_IMAGES, but
333 * not WIMLIB_NO_IMAGE. */
335 wimlib_print_metadata(WIMStruct *w, int image)
337 if (w->hdr.part_number != 1) {
338 ERROR("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 return for_image(w, image, image_print_metadata);
347 wimlib_print_files(WIMStruct *w, int image)
349 if (w->hdr.part_number != 1) {
350 ERROR("Cannot list the files from part %hu of a %hu-part split WIM!",
351 w->hdr.part_number, w->hdr.total_parts);
352 ERROR("Select the first part of the split WIM if you'd like to list the files.");
353 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
355 return for_image(w, image, image_print_files);
358 /* Sets the index of the bootable image. */
360 wimlib_set_boot_idx(WIMStruct *w, int boot_idx)
362 if (w->hdr.total_parts != 1) {
363 ERROR("Cannot modify the boot index of a split WIM!");
364 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
366 if (boot_idx < 0 || boot_idx > w->hdr.image_count)
367 return WIMLIB_ERR_INVALID_IMAGE;
368 w->hdr.boot_idx = boot_idx;
373 wimlib_get_part_number(const WIMStruct *w, int *total_parts_ret)
376 *total_parts_ret = w->hdr.total_parts;
377 return w->hdr.part_number;
382 wimlib_get_boot_idx(const WIMStruct *w)
384 return w->hdr.boot_idx;
388 do_open_wim(const tchar *filename, filedes_t *fd_ret)
392 fd = topen(filename, O_RDONLY | O_BINARY);
393 if (fd == INVALID_FILEDES) {
394 ERROR_WITH_ERRNO("Can't open \"%"TS"\" read-only", filename);
395 return WIMLIB_ERR_OPEN;
402 reopen_wim(WIMStruct *w)
404 wimlib_assert(w->in_fd == INVALID_FILEDES);
405 return do_open_wim(w->filename, &w->in_fd);
409 close_wim(WIMStruct *w)
411 if (w->in_fd != INVALID_FILEDES) {
413 w->in_fd = INVALID_FILEDES;
419 * Begins the reading of a WIM file; opens the file and reads its header and
420 * lookup table, and optionally checks the integrity.
423 begin_read(WIMStruct *w, const tchar *in_wim_path, int open_flags,
424 wimlib_progress_func_t progress_func)
429 DEBUG("Reading the WIM file `%"TS"'", in_wim_path);
431 ret = do_open_wim(in_wim_path, &w->in_fd);
435 /* The absolute path to the WIM is requested so that wimlib_overwrite()
436 * still works even if the process changes its working directory. This
437 * actually happens if a WIM is mounted read-write, since the FUSE
438 * thread changes directory to "/", and it needs to be able to find the
441 * This will break if the full path to the WIM changes in the
442 * intervening time...
444 * Warning: in Windows native builds, realpath() calls the replacement
445 * function in win32.c.
447 w->filename = realpath(in_wim_path, NULL);
449 ERROR_WITH_ERRNO("Failed to resolve WIM filename");
451 return WIMLIB_ERR_NOMEM;
453 return WIMLIB_ERR_OPEN;
456 ret = read_header(w->in_fd, &w->hdr, open_flags);
460 DEBUG("According to header, WIM contains %u images", w->hdr.image_count);
462 /* If the boot index is invalid, print a warning and set it to 0 */
463 if (w->hdr.boot_idx > w->hdr.image_count) {
464 WARNING("In `%"TS"', image %u is marked as bootable, "
465 "but there are only %u images in the WIM",
466 in_wim_path, w->hdr.boot_idx, w->hdr.image_count);
470 if (wimlib_get_compression_type(w) == WIMLIB_COMPRESSION_TYPE_INVALID) {
471 ERROR("Invalid compression type (WIM header flags = 0x%x)",
473 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
476 if (open_flags & WIMLIB_OPEN_FLAG_CHECK_INTEGRITY) {
477 ret = check_wim_integrity(w, progress_func);
478 if (ret == WIM_INTEGRITY_NONEXISTENT) {
479 WARNING("No integrity information for `%"TS"'; skipping "
480 "integrity check.", in_wim_path);
481 } else if (ret == WIM_INTEGRITY_NOT_OK) {
482 ERROR("WIM is not intact! (Failed integrity check)");
483 return WIMLIB_ERR_INTEGRITY;
484 } else if (ret != WIM_INTEGRITY_OK) {
489 if (w->hdr.image_count != 0 && w->hdr.part_number == 1) {
490 w->image_metadata = new_image_metadata_array(w->hdr.image_count);
491 if (!w->image_metadata)
492 return WIMLIB_ERR_NOMEM;
495 ret = read_lookup_table(w);
499 ret = read_xml_data(w->in_fd, &w->hdr.xml_res_entry, &w->wim_info);
503 xml_num_images = wim_info_get_num_images(w->wim_info);
504 if (xml_num_images != w->hdr.image_count) {
505 ERROR("In the file `%"TS"', there are %u <IMAGE> elements "
506 "in the XML data,", in_wim_path, xml_num_images);
507 ERROR("but %u images in the WIM! There must be exactly one "
508 "<IMAGE> element per image.", w->hdr.image_count);
509 return WIMLIB_ERR_IMAGE_COUNT;
512 DEBUG("Done beginning read of WIM file `%"TS"'.", in_wim_path);
517 * Opens a WIM file and creates a WIMStruct for it.
520 wimlib_open_wim(const tchar *wim_file, int open_flags,
522 wimlib_progress_func_t progress_func)
527 if (!wim_file || !w_ret)
528 return WIMLIB_ERR_INVALID_PARAM;
530 w = new_wim_struct();
532 return WIMLIB_ERR_NOMEM;
534 ret = begin_read(w, wim_file, open_flags, progress_func);
543 destroy_image_metadata(struct wim_image_metadata *imd,
544 struct wim_lookup_table *table,
545 bool free_metadata_lte)
547 free_dentry_tree(imd->root_dentry, table);
548 imd->root_dentry = NULL;
549 free_security_data(imd->security_data);
550 imd->security_data = NULL;
552 if (free_metadata_lte) {
553 free_lookup_table_entry(imd->metadata_lte);
554 imd->metadata_lte = NULL;
557 struct wim_lookup_table_entry *lte, *tmp;
558 list_for_each_entry_safe(lte, tmp, &imd->unhashed_streams, unhashed_list)
559 free_lookup_table_entry(lte);
561 INIT_LIST_HEAD(&imd->unhashed_streams);
562 INIT_LIST_HEAD(&imd->inode_list);
565 do_ntfs_umount(imd->ntfs_vol);
566 imd->ntfs_vol = NULL;
572 put_image_metadata(struct wim_image_metadata *imd,
573 struct wim_lookup_table *table)
575 if (imd && --imd->refcnt == 0) {
576 destroy_image_metadata(imd, table, true);
581 /* Appends the specified image metadata structure to the array of image metadata
582 * for a WIM, and increments the image count. */
584 append_image_metadata(WIMStruct *w, struct wim_image_metadata *imd)
586 struct wim_image_metadata **imd_array;
588 DEBUG("Reallocating image metadata array for image_count = %u",
589 w->hdr.image_count + 1);
590 imd_array = REALLOC(w->image_metadata,
591 sizeof(w->image_metadata[0]) * (w->hdr.image_count + 1));
594 return WIMLIB_ERR_NOMEM;
595 w->image_metadata = imd_array;
596 imd_array[w->hdr.image_count++] = imd;
601 struct wim_image_metadata *
604 struct wim_image_metadata *imd;
606 imd = CALLOC(1, sizeof(*imd));
609 INIT_LIST_HEAD(&imd->inode_list);
610 INIT_LIST_HEAD(&imd->unhashed_streams);
611 DEBUG("Created new image metadata (refcnt=1)");
613 ERROR_WITH_ERRNO("Failed to allocate new image metadata structure");
618 struct wim_image_metadata **
619 new_image_metadata_array(unsigned num_images)
621 struct wim_image_metadata **imd_array;
623 DEBUG("Creating new image metadata array for %u images",
626 imd_array = CALLOC(num_images, sizeof(imd_array[0]));
629 ERROR("Failed to allocate memory for %u image metadata structures",
633 for (unsigned i = 0; i < num_images; i++) {
634 imd_array[i] = new_image_metadata();
636 for (unsigned j = 0; j < i; j++)
637 put_image_metadata(imd_array[j], NULL);
645 /* Checksum all streams that are unhashed (other than the metadata streams),
646 * merging them into the lookup table as needed. This is a no-op unless the
647 * library has previously used to add or mount an image using the same
650 wim_checksum_unhashed_streams(WIMStruct *w)
653 for (int i = 0; i < w->hdr.image_count; i++) {
654 struct wim_lookup_table_entry *lte, *tmp;
655 struct wim_image_metadata *imd = w->image_metadata[i];
656 image_for_each_unhashed_stream_safe(lte, tmp, imd) {
657 ret = hash_unhashed_stream(lte, w->lookup_table, NULL);
665 /* Frees the memory for the WIMStruct, including all internal memory; also
666 * closes all files associated with the WIMStruct. */
668 wimlib_free(WIMStruct *w)
670 DEBUG("Freeing WIMStruct");
674 if (w->in_fd != INVALID_FILEDES)
676 if (w->out_fd != INVALID_FILEDES)
679 free_lookup_table(w->lookup_table);
682 free_wim_info(w->wim_info);
683 if (w->image_metadata) {
684 for (unsigned i = 0; i < w->hdr.image_count; i++)
685 put_image_metadata(w->image_metadata[i], NULL);
686 FREE(w->image_metadata);
689 DEBUG("Freed WIMStruct");
693 test_locale_ctype_utf8()
698 char *ctype = nl_langinfo(CODESET);
700 return (!strstr(ctype, "UTF-8") ||
701 !strstr(ctype, "UTF8") ||
702 !strstr(ctype, "utf8") ||
703 !strstr(ctype, "utf-8"));
708 wimlib_global_init(int init_flags)
710 libxml_global_init();
711 if (!(init_flags & WIMLIB_INIT_FLAG_ASSUME_UTF8)) {
712 wimlib_mbs_is_utf8 = test_locale_ctype_utf8();
714 if (!wimlib_mbs_is_utf8)
715 libntfs3g_global_init();
724 /* Free global memory allocations. Not strictly necessary if the process using
725 * wimlib is just about to exit (as is the case for 'imagex'). */
727 wimlib_global_cleanup()
729 libxml_global_cleanup();
730 iconv_global_cleanup();
732 win32_global_cleanup();