2 * wim.c - Stuff that doesn't fit into any other file
6 * Copyright (C) 2012, 2013 Eric Biggers
8 * This file is part of wimlib, a library for working with WIM files.
10 * wimlib is free software; you can redistribute it and/or modify it under the
11 * terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 3 of the License, or (at your option)
15 * wimlib 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
17 * A PARTICULAR PURPOSE. See the GNU General Public License for more
20 * You should have received a copy of the GNU General Public License
21 * along with wimlib; if not, see http://www.gnu.org/licenses/.
28 #include "wimlib/error.h"
29 #include "wimlib/dentry.h"
30 #include "wimlib/encoding.h"
31 #include "wimlib/file_io.h"
32 #include "wimlib/integrity.h"
33 #include "wimlib/lookup_table.h"
34 #include "wimlib/metadata.h"
36 # include "wimlib/ntfs_3g.h" /* for do_ntfs_umount() */
38 #include "wimlib/security.h"
39 #include "wimlib/wim.h"
40 #include "wimlib/xml.h"
43 # include "wimlib/win32.h" /* for realpath() replacement */
49 # include <langinfo.h>
57 image_print_metadata(WIMStruct *w)
59 DEBUG("Printing metadata for image %d", w->current_image);
60 print_wim_security_data(wim_security_data(w));
61 return for_dentry_in_tree(wim_root_dentry(w), print_dentry,
67 image_print_files(WIMStruct *w)
69 return for_dentry_in_tree(wim_root_dentry(w), print_dentry_full_path,
76 WIMStruct *w = CALLOC(1, sizeof(WIMStruct));
85 * Calls a function on images in the WIM. If @image is WIMLIB_ALL_IMAGES, @visitor
86 * is called on the WIM once for each image, with each image selected as the
87 * current image in turn. If @image is a certain image, @visitor is called on
88 * the WIM only once, with that image selected.
91 for_image(WIMStruct *w, int image, int (*visitor)(WIMStruct *))
98 if (image == WIMLIB_ALL_IMAGES) {
100 end = w->hdr.image_count;
101 } else if (image >= 1 && image <= w->hdr.image_count) {
105 return WIMLIB_ERR_INVALID_IMAGE;
107 for (i = start; i <= end; i++) {
108 ret = select_wim_image(w, i);
118 /* Returns the compression type given in the flags of a WIM header. */
120 wim_hdr_flags_compression_type(int wim_hdr_flags)
122 if (wim_hdr_flags & WIM_HDR_FLAG_COMPRESSION) {
123 if (wim_hdr_flags & WIM_HDR_FLAG_COMPRESS_LZX)
124 return WIMLIB_COMPRESSION_TYPE_LZX;
125 else if (wim_hdr_flags & WIM_HDR_FLAG_COMPRESS_XPRESS)
126 return WIMLIB_COMPRESSION_TYPE_XPRESS;
128 return WIMLIB_COMPRESSION_TYPE_INVALID;
130 return WIMLIB_COMPRESSION_TYPE_NONE;
135 * Creates a WIMStruct for a new WIM file.
138 wimlib_create_new_wim(int ctype, WIMStruct **w_ret)
141 struct wim_lookup_table *table;
144 DEBUG("Creating new WIM with %"TS" compression.",
145 wimlib_get_compression_type_string(ctype));
147 /* Allocate the WIMStruct. */
148 w = new_wim_struct();
150 return WIMLIB_ERR_NOMEM;
152 ret = init_header(&w->hdr, ctype);
156 table = new_lookup_table(9001);
158 ret = WIMLIB_ERR_NOMEM;
161 w->lookup_table = table;
170 wimlib_get_num_images(const WIMStruct *w)
172 return w->hdr.image_count;
176 select_wim_image(WIMStruct *w, int image)
178 struct wim_image_metadata *imd;
181 DEBUG("Selecting image %d", image);
183 if (image == WIMLIB_NO_IMAGE) {
184 ERROR("Invalid image: %d", WIMLIB_NO_IMAGE);
185 return WIMLIB_ERR_INVALID_IMAGE;
188 if (image == w->current_image)
191 if (image < 1 || image > w->hdr.image_count) {
192 ERROR("Cannot select image %d: There are only %u images",
193 image, w->hdr.image_count);
194 return WIMLIB_ERR_INVALID_IMAGE;
197 /* If a valid image is currently selected, it can be freed if it is not
199 if (w->current_image != WIMLIB_NO_IMAGE) {
200 imd = wim_get_current_image_metadata(w);
201 if (!imd->modified) {
202 wimlib_assert(list_empty(&imd->unhashed_streams));
203 DEBUG("Freeing image %u", w->current_image);
204 destroy_image_metadata(imd, NULL, false);
207 w->current_image = image;
208 imd = wim_get_current_image_metadata(w);
209 if (imd->root_dentry || imd->modified) {
213 DEBUG("Reading metadata resource specified by the following "
214 "lookup table entry:");
215 print_lookup_table_entry(imd->metadata_lte, stderr);
217 ret = read_metadata_resource(w, imd);
219 w->current_image = WIMLIB_NO_IMAGE;
225 /* Returns the compression type of the WIM file. */
227 wimlib_get_compression_type(const WIMStruct *w)
229 return wim_hdr_flags_compression_type(w->hdr.flags);
232 WIMLIBAPI const tchar *
233 wimlib_get_compression_type_string(int ctype)
236 case WIMLIB_COMPRESSION_TYPE_NONE:
238 case WIMLIB_COMPRESSION_TYPE_LZX:
240 case WIMLIB_COMPRESSION_TYPE_XPRESS:
248 * Returns the number of an image in the WIM file, given a string that is either
249 * the number of the image, or the name of the image. The images are numbered
253 wimlib_resolve_image(WIMStruct *w, const tchar *image_name_or_num)
259 if (!image_name_or_num || !*image_name_or_num)
260 return WIMLIB_NO_IMAGE;
262 if (!tstrcasecmp(image_name_or_num, T("all"))
263 || !tstrcasecmp(image_name_or_num, T("*")))
264 return WIMLIB_ALL_IMAGES;
265 image = tstrtol(image_name_or_num, &p, 10);
266 if (p != image_name_or_num && *p == T('\0') && image > 0) {
267 if (image > w->hdr.image_count)
268 return WIMLIB_NO_IMAGE;
271 for (i = 1; i <= w->hdr.image_count; i++) {
272 if (!tstrcmp(image_name_or_num,
273 wimlib_get_image_name(w, i)))
276 return WIMLIB_NO_IMAGE;
280 /* Prints some basic information about a WIM file. */
282 wimlib_print_wim_information(const WIMStruct *w)
284 const struct wim_header *hdr;
287 tputs(T("WIM Information:"));
288 tputs(T("----------------"));
289 tprintf(T("Path: %"TS"\n"), w->filename);
290 tfputs(T("GUID: 0x"), stdout);
291 print_byte_field(hdr->guid, WIM_GID_LEN, stdout);
293 tprintf(T("Image Count: %d\n"), hdr->image_count);
294 tprintf(T("Compression: %"TS"\n"),
295 wimlib_get_compression_type_string(wimlib_get_compression_type(w)));
296 tprintf(T("Part Number: %d/%d\n"), hdr->part_number, hdr->total_parts);
297 tprintf(T("Boot Index: %d\n"), hdr->boot_idx);
298 tprintf(T("Size: %"PRIu64" bytes\n"),
299 wim_info_get_total_bytes(w->wim_info));
300 tprintf(T("Integrity Info: %"TS"\n"),
301 (w->hdr.integrity.offset != 0) ? T("yes") : T("no"));
302 tprintf(T("Relative path junction: %"TS"\n"),
303 (hdr->flags & WIM_HDR_FLAG_RP_FIX) ? T("yes") : T("no"));
308 wimlib_has_integrity_table(const WIMStruct *w)
310 return w->hdr.integrity.size != 0;
314 wimlib_print_available_images(const WIMStruct *w, int image)
320 if (image == WIMLIB_ALL_IMAGES) {
321 n = tprintf(T("Available Images:\n"));
323 last = w->hdr.image_count;
324 } else if (image >= 1 && image <= w->hdr.image_count) {
325 n = tprintf(T("Information for Image %d\n"), image);
329 tprintf(T("wimlib_print_available_images(): Invalid image %d"),
333 for (i = 0; i < n - 1; i++)
336 for (i = first; i <= last; i++)
337 print_image_info(w->wim_info, i);
341 /* Prints the metadata for the specified image, which may be WIMLIB_ALL_IMAGES, but
342 * not WIMLIB_NO_IMAGE. */
344 wimlib_print_metadata(WIMStruct *w, int image)
346 if (w->hdr.part_number != 1) {
347 ERROR("Cannot show the metadata from part %hu of a %hu-part split WIM!",
348 w->hdr.part_number, w->hdr.total_parts);
349 ERROR("Select the first part of the split WIM to see the metadata.");
350 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
352 return for_image(w, image, image_print_metadata);
356 wimlib_print_files(WIMStruct *w, int image)
358 if (w->hdr.part_number != 1) {
359 ERROR("Cannot list the files 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 if you'd like to list the files.");
362 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
364 return for_image(w, image, image_print_files);
367 /* Sets the index of the bootable image. */
369 wimlib_set_boot_idx(WIMStruct *wim, int boot_idx)
373 ret = can_modify_wim(wim);
376 if (boot_idx < 0 || boot_idx > wim->hdr.image_count)
377 return WIMLIB_ERR_INVALID_IMAGE;
378 wim->hdr.boot_idx = boot_idx;
383 wimlib_get_part_number(const WIMStruct *w, int *total_parts_ret)
386 *total_parts_ret = w->hdr.total_parts;
387 return w->hdr.part_number;
392 wimlib_get_boot_idx(const WIMStruct *w)
394 return w->hdr.boot_idx;
398 do_open_wim(const tchar *filename, int *fd_ret)
402 fd = topen(filename, O_RDONLY | O_BINARY);
404 ERROR_WITH_ERRNO("Can't open \"%"TS"\" read-only", filename);
405 return WIMLIB_ERR_OPEN;
412 reopen_wim(WIMStruct *w)
414 wimlib_assert(w->in_fd == -1);
415 return do_open_wim(w->filename, &w->in_fd);
419 close_wim(WIMStruct *w)
421 if (w->in_fd != -1) {
429 * Begins the reading of a WIM file; opens the file and reads its header and
430 * lookup table, and optionally checks the integrity.
433 begin_read(WIMStruct *w, const tchar *in_wim_path, int open_flags,
434 wimlib_progress_func_t progress_func)
439 DEBUG("Reading the WIM file `%"TS"'", in_wim_path);
441 ret = do_open_wim(in_wim_path, &w->in_fd);
445 /* The absolute path to the WIM is requested so that wimlib_overwrite()
446 * still works even if the process changes its working directory. This
447 * actually happens if a WIM is mounted read-write, since the FUSE
448 * thread changes directory to "/", and it needs to be able to find the
451 * This will break if the full path to the WIM changes in the
452 * intervening time...
454 * Warning: in Windows native builds, realpath() calls the replacement
455 * function in win32.c.
457 w->filename = realpath(in_wim_path, NULL);
459 ERROR_WITH_ERRNO("Failed to resolve WIM filename");
461 return WIMLIB_ERR_NOMEM;
463 return WIMLIB_ERR_OPEN;
466 ret = read_header(w->filename, w->in_fd, &w->hdr, open_flags);
470 DEBUG("According to header, WIM contains %u images", w->hdr.image_count);
472 /* If the boot index is invalid, print a warning and set it to 0 */
473 if (w->hdr.boot_idx > w->hdr.image_count) {
474 WARNING("In `%"TS"', image %u is marked as bootable, "
475 "but there are only %u images in the WIM",
476 in_wim_path, w->hdr.boot_idx, w->hdr.image_count);
480 if (wimlib_get_compression_type(w) == WIMLIB_COMPRESSION_TYPE_INVALID) {
481 ERROR("Invalid compression type (WIM header flags = 0x%x)",
483 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
486 if (open_flags & WIMLIB_OPEN_FLAG_CHECK_INTEGRITY) {
487 ret = check_wim_integrity(w, progress_func);
488 if (ret == WIM_INTEGRITY_NONEXISTENT) {
489 WARNING("No integrity information for `%"TS"'; skipping "
490 "integrity check.", in_wim_path);
491 } else if (ret == WIM_INTEGRITY_NOT_OK) {
492 ERROR("WIM is not intact! (Failed integrity check)");
493 return WIMLIB_ERR_INTEGRITY;
494 } else if (ret != WIM_INTEGRITY_OK) {
499 if (w->hdr.image_count != 0 && w->hdr.part_number == 1) {
500 w->image_metadata = new_image_metadata_array(w->hdr.image_count);
501 if (!w->image_metadata)
502 return WIMLIB_ERR_NOMEM;
505 ret = read_lookup_table(w);
509 ret = read_xml_data(w->in_fd, &w->hdr.xml_res_entry, &w->wim_info);
513 xml_num_images = wim_info_get_num_images(w->wim_info);
514 if (xml_num_images != w->hdr.image_count) {
515 ERROR("In the file `%"TS"', there are %u <IMAGE> elements "
516 "in the XML data,", in_wim_path, xml_num_images);
517 ERROR("but %u images in the WIM! There must be exactly one "
518 "<IMAGE> element per image.", w->hdr.image_count);
519 return WIMLIB_ERR_IMAGE_COUNT;
522 DEBUG("Done beginning read of WIM file `%"TS"'.", in_wim_path);
527 * Opens a WIM file and creates a WIMStruct for it.
530 wimlib_open_wim(const tchar *wim_file, int open_flags,
532 wimlib_progress_func_t progress_func)
537 if (!wim_file || !w_ret)
538 return WIMLIB_ERR_INVALID_PARAM;
540 w = new_wim_struct();
542 return WIMLIB_ERR_NOMEM;
544 ret = begin_read(w, wim_file, open_flags, progress_func);
553 destroy_image_metadata(struct wim_image_metadata *imd,
554 struct wim_lookup_table *table,
555 bool free_metadata_lte)
557 free_dentry_tree(imd->root_dentry, table);
558 imd->root_dentry = NULL;
559 free_wim_security_data(imd->security_data);
560 imd->security_data = NULL;
562 if (free_metadata_lte) {
563 free_lookup_table_entry(imd->metadata_lte);
564 imd->metadata_lte = NULL;
567 struct wim_lookup_table_entry *lte, *tmp;
568 list_for_each_entry_safe(lte, tmp, &imd->unhashed_streams, unhashed_list)
569 free_lookup_table_entry(lte);
571 INIT_LIST_HEAD(&imd->unhashed_streams);
572 INIT_LIST_HEAD(&imd->inode_list);
575 do_ntfs_umount(imd->ntfs_vol);
576 imd->ntfs_vol = NULL;
582 put_image_metadata(struct wim_image_metadata *imd,
583 struct wim_lookup_table *table)
585 if (imd && --imd->refcnt == 0) {
586 destroy_image_metadata(imd, table, true);
591 /* Appends the specified image metadata structure to the array of image metadata
592 * for a WIM, and increments the image count. */
594 append_image_metadata(WIMStruct *w, struct wim_image_metadata *imd)
596 struct wim_image_metadata **imd_array;
598 DEBUG("Reallocating image metadata array for image_count = %u",
599 w->hdr.image_count + 1);
600 imd_array = REALLOC(w->image_metadata,
601 sizeof(w->image_metadata[0]) * (w->hdr.image_count + 1));
604 return WIMLIB_ERR_NOMEM;
605 w->image_metadata = imd_array;
606 imd_array[w->hdr.image_count++] = imd;
611 struct wim_image_metadata *
612 new_image_metadata(void)
614 struct wim_image_metadata *imd;
616 imd = CALLOC(1, sizeof(*imd));
619 INIT_LIST_HEAD(&imd->inode_list);
620 INIT_LIST_HEAD(&imd->unhashed_streams);
621 DEBUG("Created new image metadata (refcnt=1)");
623 ERROR_WITH_ERRNO("Failed to allocate new image metadata structure");
628 struct wim_image_metadata **
629 new_image_metadata_array(unsigned num_images)
631 struct wim_image_metadata **imd_array;
633 DEBUG("Creating new image metadata array for %u images",
636 imd_array = CALLOC(num_images, sizeof(imd_array[0]));
639 ERROR("Failed to allocate memory for %u image metadata structures",
643 for (unsigned i = 0; i < num_images; i++) {
644 imd_array[i] = new_image_metadata();
646 for (unsigned j = 0; j < i; j++)
647 put_image_metadata(imd_array[j], NULL);
655 /* Checksum all streams that are unhashed (other than the metadata streams),
656 * merging them into the lookup table as needed. This is a no-op unless the
657 * library has previously used to add or mount an image using the same
660 wim_checksum_unhashed_streams(WIMStruct *w)
663 for (int i = 0; i < w->hdr.image_count; i++) {
664 struct wim_lookup_table_entry *lte, *tmp;
665 struct wim_image_metadata *imd = w->image_metadata[i];
666 image_for_each_unhashed_stream_safe(lte, tmp, imd) {
667 ret = hash_unhashed_stream(lte, w->lookup_table, NULL);
676 can_modify_wim(WIMStruct *wim)
678 if (wim->hdr.total_parts != 1) {
679 ERROR("Cannot modify \"%"TS"\": is part of a spanned set",
681 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
683 if (wim->hdr.flags & WIM_HDR_FLAG_READONLY) {
684 ERROR("Cannot modify \"%"TS"\": is marked read-only",
686 return WIMLIB_ERR_WIM_IS_MARKED_READONLY;
692 can_delete_from_wim(WIMStruct *wim)
696 ret = can_modify_wim(wim);
697 if (ret == 0 && !wim->all_images_verified)
698 ret = wim_run_full_verifications(wim);
702 /* Frees the memory for the WIMStruct, including all internal memory; also
703 * closes all files associated with the WIMStruct. */
705 wimlib_free(WIMStruct *w)
707 DEBUG("Freeing WIMStruct");
716 free_lookup_table(w->lookup_table);
719 free_wim_info(w->wim_info);
720 if (w->image_metadata) {
721 for (unsigned i = 0; i < w->hdr.image_count; i++)
722 put_image_metadata(w->image_metadata[i], NULL);
723 FREE(w->image_metadata);
726 DEBUG("Freed WIMStruct");
730 test_locale_ctype_utf8(void)
735 char *ctype = nl_langinfo(CODESET);
737 return (!strstr(ctype, "UTF-8") ||
738 !strstr(ctype, "UTF8") ||
739 !strstr(ctype, "utf8") ||
740 !strstr(ctype, "utf-8"));
745 wimlib_global_init(int init_flags)
747 libxml_global_init();
748 if (!(init_flags & WIMLIB_INIT_FLAG_ASSUME_UTF8)) {
749 wimlib_mbs_is_utf8 = test_locale_ctype_utf8();
751 if (!wimlib_mbs_is_utf8)
752 libntfs3g_global_init();
761 /* Free global memory allocations. Not strictly necessary if the process using
762 * wimlib is just about to exit (as is the case for 'imagex'). */
764 wimlib_global_cleanup(void)
766 libxml_global_cleanup();
767 iconv_global_cleanup();
769 win32_global_cleanup();