4 * Support for extracting WIM files.
8 * Copyright (C) 2012, 2013 Eric Biggers
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/.
37 #include "wimlib_internal.h"
39 #include "lookup_table.h"
43 # include <ntfs-3g/volume.h>
47 do_apply_op(struct wim_dentry *dentry, struct apply_args *args,
48 int (*apply_dentry_func)(const tchar *, size_t,
49 struct wim_dentry *, struct apply_args *))
52 const tchar *full_path;
53 size_t full_path_nchars;
55 wimlib_assert(dentry->_full_path != NULL);
56 full_path = dentry->_full_path + 1;
57 full_path_nchars = dentry->full_path_nbytes / sizeof(tchar) - 1;
58 tchar output_path[args->target_nchars + 1 +
59 (full_path_nchars - args->wim_source_path_nchars) + 1];
62 tmemcpy(p, args->target, args->target_nchars);
63 p += args->target_nchars;
65 if (dentry != args->extract_root) {
67 tmemcpy(p, full_path + args->wim_source_path_nchars,
68 full_path_nchars - args->wim_source_path_nchars);
69 p += full_path_nchars - args->wim_source_path_nchars;
72 return (*apply_dentry_func)(output_path, p - output_path,
77 /* Extracts a file, directory, or symbolic link from the WIM archive. */
79 apply_dentry_normal(struct wim_dentry *dentry, void *arg)
82 return do_apply_op(dentry, arg, win32_do_apply_dentry);
84 return do_apply_op(dentry, arg, unix_do_apply_dentry);
89 /* Apply timestamps to an extracted file or directory */
91 apply_dentry_timestamps_normal(struct wim_dentry *dentry, void *arg)
94 return do_apply_op(dentry, arg, win32_do_apply_dentry_timestamps);
96 return do_apply_op(dentry, arg, unix_do_apply_dentry_timestamps);
100 /* Extract a dentry if it hasn't already been extracted and either
101 * WIMLIB_EXTRACT_FLAG_NO_STREAMS is not specified, or the dentry is a directory
102 * and/or has no unnamed stream. */
104 maybe_apply_dentry(struct wim_dentry *dentry, void *arg)
106 struct apply_args *args = arg;
109 if (!dentry->needs_extraction)
112 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_NO_STREAMS &&
113 !dentry_is_directory(dentry) &&
114 inode_unnamed_lte_resolved(dentry->d_inode) != NULL)
117 if ((args->extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE) &&
118 args->progress_func) {
119 args->progress.extract.cur_path = dentry->_full_path;
120 args->progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY,
123 ret = args->apply_dentry(dentry, args);
125 dentry->needs_extraction = 0;
130 calculate_bytes_to_extract(struct list_head *stream_list,
132 union wimlib_progress_info *progress)
134 struct wim_lookup_table_entry *lte;
138 /* For each stream to be extracted... */
139 list_for_each_entry(lte, stream_list, extraction_list) {
141 (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
143 /* In the symlink or hard link extraction mode, each
144 * stream will be extracted one time regardless of how
145 * many dentries share the stream. */
146 wimlib_assert(!(extract_flags & WIMLIB_EXTRACT_FLAG_NTFS));
147 if (!lte->extracted_file) {
149 total_bytes += wim_resource_size(lte);
152 num_streams += lte->out_refcnt;
153 total_bytes += lte->out_refcnt * wim_resource_size(lte);
156 progress->extract.num_streams = num_streams;
157 progress->extract.total_bytes = total_bytes;
158 progress->extract.completed_bytes = 0;
162 maybe_add_stream_for_extraction(struct wim_lookup_table_entry *lte,
163 struct list_head *stream_list)
165 if (++lte->out_refcnt == 1) {
166 INIT_LIST_HEAD(<e->lte_dentry_list);
167 list_add_tail(<e->extraction_list, stream_list);
171 struct find_streams_ctx {
172 struct list_head stream_list;
177 dentry_find_streams_to_extract(struct wim_dentry *dentry, void *_ctx)
179 struct find_streams_ctx *ctx = _ctx;
180 struct wim_inode *inode = dentry->d_inode;
181 struct wim_lookup_table_entry *lte;
182 bool dentry_added = false;
183 struct list_head *stream_list = &ctx->stream_list;
184 int extract_flags = ctx->extract_flags;
186 dentry->needs_extraction = 1;
188 lte = inode_unnamed_lte_resolved(inode);
190 maybe_add_stream_for_extraction(lte, stream_list);
191 list_add_tail(&dentry->tmp_list, <e->lte_dentry_list);
195 /* Determine whether to include alternate data stream entries or not.
197 * UNIX: Include them if extracting using NTFS-3g.
199 * Windows: Include them undconditionally, although if the filesystem is
200 * not NTFS we won't actually be able to extract them. */
201 #if defined(WITH_NTFS_3G)
202 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS)
203 #elif defined(__WIN32__)
209 for (unsigned i = 0; i < inode->i_num_ads; i++) {
210 if (inode->i_ads_entries[i].stream_name_nbytes != 0) {
211 lte = inode->i_ads_entries[i].lte;
213 maybe_add_stream_for_extraction(lte,
216 list_add_tail(&dentry->tmp_list,
217 <e->lte_dentry_list);
228 dentry_resolve_and_zero_lte_refcnt(struct wim_dentry *dentry, void *_lookup_table)
230 struct wim_inode *inode = dentry->d_inode;
231 struct wim_lookup_table *lookup_table = _lookup_table;
232 struct wim_lookup_table_entry *lte;
234 inode_resolve_ltes(inode, lookup_table);
235 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
236 lte = inode_stream_lte_resolved(inode, i);
244 find_streams_for_extraction(struct wim_dentry *root,
245 struct list_head *stream_list,
246 struct wim_lookup_table *lookup_table,
249 struct find_streams_ctx ctx;
251 INIT_LIST_HEAD(&ctx.stream_list);
252 ctx.extract_flags = extract_flags;
253 for_dentry_in_tree(root, dentry_resolve_and_zero_lte_refcnt, lookup_table);
254 for_dentry_in_tree(root, dentry_find_streams_to_extract, &ctx);
255 list_transfer(&ctx.stream_list, stream_list);
259 dentry_reset_needs_extraction(struct wim_dentry *dentry, void *_ignore)
261 dentry->needs_extraction = 0;
265 struct apply_operations {
266 int (*apply_dentry)(struct wim_dentry *dentry, void *arg);
267 int (*apply_dentry_timestamps)(struct wim_dentry *dentry, void *arg);
270 static const struct apply_operations normal_apply_operations = {
271 .apply_dentry = apply_dentry_normal,
272 .apply_dentry_timestamps = apply_dentry_timestamps_normal,
276 static const struct apply_operations ntfs_apply_operations = {
277 .apply_dentry = apply_dentry_ntfs,
278 .apply_dentry_timestamps = apply_dentry_timestamps_ntfs,
283 apply_stream_list(struct list_head *stream_list,
284 struct apply_args *args,
285 const struct apply_operations *ops,
286 wimlib_progress_func_t progress_func)
288 uint64_t bytes_per_progress = args->progress.extract.total_bytes / 100;
289 uint64_t next_progress = bytes_per_progress;
290 struct wim_lookup_table_entry *lte;
291 struct wim_dentry *dentry;
294 /* This complicated loop is essentially looping through the dentries,
295 * although dentries may be visited more than once (if a dentry contains
296 * two different nonempty streams) or not at all (if a dentry contains
297 * no non-empty streams).
299 * The outer loop is over the distinct streams to be extracted so that
300 * sequential reading of the WIM can be implemented. */
302 /* For each distinct stream to be extracted */
303 list_for_each_entry(lte, stream_list, extraction_list) {
304 /* For each dentry to be extracted that is a name for an inode
305 * containing the stream */
306 list_for_each_entry(dentry, <e->lte_dentry_list, tmp_list) {
307 /* Extract the dentry if it was not already
309 ret = maybe_apply_dentry(dentry, args);
313 args->progress.extract.completed_bytes >= next_progress)
315 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS,
317 if (args->progress.extract.completed_bytes >=
318 args->progress.extract.total_bytes)
320 next_progress = ~0ULL;
323 min (args->progress.extract.completed_bytes +
325 args->progress.extract.total_bytes);
334 sort_stream_list_by_wim_position(struct list_head *stream_list)
336 struct list_head *cur;
338 struct wim_lookup_table_entry **array;
343 list_for_each(cur, stream_list)
345 array_size = num_streams * sizeof(array[0]);
346 array = MALLOC(array_size);
348 ERROR("Failed to allocate %zu bytes to sort stream entries",
350 return WIMLIB_ERR_NOMEM;
352 cur = stream_list->next;
353 for (i = 0; i < num_streams; i++) {
354 array[i] = container_of(cur, struct wim_lookup_table_entry, extraction_list);
358 qsort(array, num_streams, sizeof(array[0]), cmp_streams_by_wim_position);
360 INIT_LIST_HEAD(stream_list);
361 for (i = 0; i < num_streams; i++)
362 list_add_tail(&array[i]->extraction_list, stream_list);
368 * Extract a dentry to standard output.
370 * This obviously doesn't make sense in all cases. We return an error if the
371 * dentry does not correspond to a regular file. Otherwise we extract the
372 * unnamed data stream only.
375 extract_dentry_to_stdout(struct wim_dentry *dentry)
378 if (!dentry_is_regular_file(dentry)) {
379 ERROR("\"%"TS"\" is not a regular file and therefore cannot be "
380 "extracted to standard output", dentry->_full_path);
381 ret = WIMLIB_ERR_NOT_A_REGULAR_FILE;
383 struct wim_lookup_table_entry *lte;
385 lte = inode_unnamed_lte_resolved(dentry->d_inode);
387 ret = extract_wim_resource_to_fd(lte, STDOUT_FILENO,
388 wim_resource_size(lte));
395 * extract_tree - Extract a file or directory tree from the currently selected
398 * @wim: WIMStruct for the WIM file, with the desired image selected
399 * (as wim->current_image).
401 * "Canonical" (i.e. no leading or trailing slashes, path
402 * separators forwald slashes) path inside the WIM image to
403 * extract. An empty string means the full image.
405 * Filesystem path to extract the file or directory tree to.
408 * WIMLIB_EXTRACT_FLAG_*. Also, the private flag
409 * WIMLIB_EXTRACT_FLAG_MULTI_IMAGE will be set if this is being
410 * called through wimlib_extract_image() with WIMLIB_ALL_IMAGES as
414 * If non-NULL, progress function for the extraction. The messages
415 * we may in this function are:
417 * WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN or
418 * WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN;
419 * WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN;
420 * WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_END;
421 * WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY;
422 * WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS;
423 * WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS;
424 * WIMLIB_PROGRESS_MSG_EXTRACT_TREE_END or
425 * WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END.
427 * Returns 0 on success; nonzero on failure.
430 extract_tree(WIMStruct *wim, const tchar *wim_source_path, const tchar *target,
431 int extract_flags, wimlib_progress_func_t progress_func)
434 struct list_head stream_list;
435 struct apply_args args;
436 const struct apply_operations *ops;
437 struct wim_dentry *root;
439 memset(&args, 0, sizeof(args));
442 args.target = target;
443 args.extract_flags = extract_flags;
444 args.progress_func = progress_func;
445 args.target_nchars = tstrlen(target);
446 args.wim_source_path_nchars = tstrlen(wim_source_path);
449 args.progress.extract.wimfile_name = wim->filename;
450 args.progress.extract.image = wim->current_image;
451 args.progress.extract.extract_flags = (extract_flags &
452 WIMLIB_EXTRACT_MASK_PUBLIC);
453 args.progress.extract.image_name = wimlib_get_image_name(wim,
455 args.progress.extract.extract_root_wim_source_path = wim_source_path;
456 args.progress.extract.target = target;
460 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
461 args.vol = ntfs_mount(target, 0);
463 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%"TS"'",
465 ret = WIMLIB_ERR_NTFS_3G;
468 ops = &ntfs_apply_operations;
471 ops = &normal_apply_operations;
473 root = get_dentry(wim, wim_source_path);
475 ERROR("Path \"%"TS"\" does not exist in WIM image %d",
476 wim_source_path, wim->current_image);
477 ret = WIMLIB_ERR_PATH_DOES_NOT_EXIST;
478 goto out_ntfs_umount;
480 args.extract_root = root;
482 ret = calculate_dentry_tree_full_paths(root);
484 goto out_ntfs_umount;
486 /* Build a list of the streams that need to be extracted */
487 find_streams_for_extraction(root,
489 wim->lookup_table, extract_flags);
491 /* Calculate the number of bytes of data that will be extracted */
492 calculate_bytes_to_extract(&stream_list, extract_flags,
495 if (extract_flags & WIMLIB_EXTRACT_FLAG_TO_STDOUT) {
496 ret = extract_dentry_to_stdout(root);
497 goto out_dentry_reset_needs_extraction;
501 progress_func(*wim_source_path ? WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN :
502 WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN,
506 /* If a sequential extraction was specified, sort the streams to be
507 * extracted by their position in the WIM file, so that the WIM file can
508 * be read sequentially. */
509 if (extract_flags & WIMLIB_EXTRACT_FLAG_SEQUENTIAL) {
510 ret = sort_stream_list_by_wim_position(&stream_list);
512 WARNING("Falling back to non-sequential extraction");
513 extract_flags &= ~WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
518 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN,
522 /* Make the directory structure and extract empty files */
523 args.extract_flags |= WIMLIB_EXTRACT_FLAG_NO_STREAMS;
524 args.apply_dentry = ops->apply_dentry;
525 ret = for_dentry_in_tree(root, maybe_apply_dentry, &args);
526 args.extract_flags &= ~WIMLIB_EXTRACT_FLAG_NO_STREAMS;
528 goto out_dentry_reset_needs_extraction;
531 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_END,
535 if (extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX) {
536 args.target_realpath = realpath(target, NULL);
537 if (!args.target_realpath) {
538 ret = WIMLIB_ERR_NOMEM;
539 goto out_dentry_reset_needs_extraction;
541 args.target_realpath_len = tstrlen(args.target_realpath);
544 /* Extract non-empty files */
545 ret = apply_stream_list(&stream_list, &args, ops, progress_func);
547 goto out_free_target_realpath;
550 progress_func(WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS,
554 /* Apply timestamps */
555 ret = for_dentry_in_tree_depth(root,
556 ops->apply_dentry_timestamps, &args);
558 goto out_free_target_realpath;
561 progress_func(*wim_source_path ? WIMLIB_PROGRESS_MSG_EXTRACT_TREE_END :
562 WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END,
565 out_free_target_realpath:
566 FREE(args.target_realpath);
567 out_dentry_reset_needs_extraction:
568 for_dentry_in_tree(root, dentry_reset_needs_extraction, NULL);
571 /* Unmount the NTFS volume */
572 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
573 if (ntfs_umount(args.vol, FALSE) != 0) {
574 ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%"TS"'",
577 ret = WIMLIB_ERR_NTFS_3G;
585 /* Validates a single wimlib_extract_command, mostly checking to make sure the
586 * extract flags make sense. */
588 check_extract_command(struct wimlib_extract_command *cmd, int wim_header_flags)
591 bool is_entire_image = (cmd->wim_source_path[0] == T('\0'));
593 /* Empty destination path? */
594 if (cmd->fs_dest_path[0] == T('\0'))
595 return WIMLIB_ERR_INVALID_PARAM;
597 extract_flags = cmd->extract_flags;
599 /* Specified both symlink and hardlink modes? */
601 (WIMLIB_EXTRACT_FLAG_SYMLINK |
602 WIMLIB_EXTRACT_FLAG_HARDLINK)) == (WIMLIB_EXTRACT_FLAG_SYMLINK |
603 WIMLIB_EXTRACT_FLAG_HARDLINK))
604 return WIMLIB_ERR_INVALID_PARAM;
607 /* Wanted UNIX data on Windows? */
608 if (extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
609 ERROR("Extracting UNIX data is not supported on Windows");
610 return WIMLIB_ERR_INVALID_PARAM;
612 /* Wanted linked extraction on Windows? (XXX This is possible, just not
613 * implemented yet.) */
614 if (extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
615 WIMLIB_EXTRACT_FLAG_HARDLINK))
617 ERROR("Linked extraction modes are not supported on Windows");
618 return WIMLIB_ERR_INVALID_PARAM;
622 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
623 /* NTFS-3g extraction mode requested */
625 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
626 WIMLIB_EXTRACT_FLAG_HARDLINK))) {
627 ERROR("Cannot specify symlink or hardlink flags when applying\n"
628 " directly to a NTFS volume");
629 return WIMLIB_ERR_INVALID_PARAM;
631 if (!is_entire_image &&
632 (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS))
634 ERROR("When applying directly to a NTFS volume you can "
635 "only extract a full image, not part of one");
636 return WIMLIB_ERR_INVALID_PARAM;
638 if (extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
639 ERROR("Cannot restore UNIX-specific data in "
640 "the NTFS extraction mode");
641 return WIMLIB_ERR_INVALID_PARAM;
644 ERROR("wimlib was compiled without support for NTFS-3g, so");
645 ERROR("we cannot apply a WIM image directly to a NTFS volume");
646 return WIMLIB_ERR_UNSUPPORTED;
650 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_RPFIX |
651 WIMLIB_EXTRACT_FLAG_NORPFIX)) ==
652 (WIMLIB_EXTRACT_FLAG_RPFIX | WIMLIB_EXTRACT_FLAG_NORPFIX))
654 ERROR("Cannot specify RPFIX and NORPFIX flags at the same time!");
655 return WIMLIB_ERR_INVALID_PARAM;
658 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_RPFIX |
659 WIMLIB_EXTRACT_FLAG_NORPFIX)) == 0)
661 /* Do reparse point fixups by default if the WIM header says
662 * they are enabled and we are extracting a full image. */
663 if ((wim_header_flags & WIM_HDR_FLAG_RP_FIX) && is_entire_image)
664 extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
667 if (!is_entire_image && (extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX)) {
668 ERROR("Cannot specify --rpfix when not extracting entire image");
669 return WIMLIB_ERR_INVALID_PARAM;
672 cmd->extract_flags = extract_flags;
677 /* Internal function to execute extraction commands for a WIM image. */
679 do_wimlib_extract_files(WIMStruct *wim,
681 struct wimlib_extract_command *cmds,
683 wimlib_progress_func_t progress_func)
686 bool found_link_cmd = false;
687 bool found_nolink_cmd = false;
689 /* Select the image from which we are extracting files */
690 ret = select_wim_image(wim, image);
694 /* Make sure there are no streams in the WIM that have not been
695 * checksummed yet. */
696 ret = wim_checksum_unhashed_streams(wim);
700 /* Check for problems with the extraction commands */
701 for (size_t i = 0; i < num_cmds; i++) {
702 ret = check_extract_command(&cmds[i], wim->hdr.flags);
705 if (cmds[i].extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
706 WIMLIB_EXTRACT_FLAG_HARDLINK)) {
707 found_link_cmd = true;
709 found_nolink_cmd = true;
711 if (found_link_cmd && found_nolink_cmd) {
712 ERROR("Symlink or hardlink extraction mode must "
713 "be set on all extraction commands");
714 return WIMLIB_ERR_INVALID_PARAM;
718 /* Execute the extraction commands */
719 for (size_t i = 0; i < num_cmds; i++) {
720 ret = extract_tree(wim,
721 cmds[i].wim_source_path,
722 cmds[i].fs_dest_path,
723 cmds[i].extract_flags,
731 /* Extract files or directories from a WIM image. */
733 wimlib_extract_files(WIMStruct *wim,
735 const struct wimlib_extract_command *cmds,
737 int default_extract_flags,
738 WIMStruct **additional_swms,
739 unsigned num_additional_swms,
740 wimlib_progress_func_t progress_func)
743 struct wimlib_extract_command *cmds_copy;
744 struct wim_lookup_table *wim_tab_save, *joined_tab;
747 default_extract_flags &= WIMLIB_EXTRACT_MASK_PUBLIC;
749 ret = verify_swm_set(wim, additional_swms, num_additional_swms);
756 if (num_additional_swms) {
757 ret = new_joined_lookup_table(wim, additional_swms,
762 wim_tab_save = wim->lookup_table;
763 wim->lookup_table = joined_tab;
766 cmds_copy = CALLOC(num_cmds, sizeof(cmds[0]));
768 ret = WIMLIB_ERR_NOMEM;
769 goto out_restore_lookup_table;
772 for (size_t i = 0; i < num_cmds; i++) {
773 cmds_copy[i].extract_flags = (default_extract_flags |
774 cmds[i].extract_flags)
775 & WIMLIB_EXTRACT_MASK_PUBLIC;
776 all_flags |= cmds_copy[i].extract_flags;
778 cmds_copy[i].wim_source_path = canonicalize_wim_path(cmds[i].wim_source_path);
779 if (!cmds_copy[i].wim_source_path) {
780 ret = WIMLIB_ERR_NOMEM;
781 goto out_free_cmds_copy;
784 cmds_copy[i].fs_dest_path = canonicalize_fs_path(cmds[i].fs_dest_path);
785 if (!cmds_copy[i].fs_dest_path) {
786 ret = WIMLIB_ERR_NOMEM;
787 goto out_free_cmds_copy;
791 ret = do_wimlib_extract_files(wim, image,
795 if (all_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
796 WIMLIB_EXTRACT_FLAG_HARDLINK))
798 for_lookup_table_entry(wim->lookup_table,
799 lte_free_extracted_file, NULL);
802 for (size_t i = 0; i < num_cmds; i++) {
803 FREE(cmds_copy[i].wim_source_path);
804 FREE(cmds_copy[i].fs_dest_path);
807 out_restore_lookup_table:
808 if (num_additional_swms) {
809 free_lookup_table(wim->lookup_table);
810 wim->lookup_table = wim_tab_save;
817 * Extracts an image from a WIM file.
819 * @wim: WIMStruct for the WIM file.
821 * @image: Number of the single image to extract.
823 * @target: Directory or NTFS volume to extract the image to.
825 * @extract_flags: Bitwise or of WIMLIB_EXTRACT_FLAG_*.
827 * @progress_func: If non-NULL, a progress function to be called
830 * Returns 0 on success; nonzero on failure.
833 extract_single_image(WIMStruct *wim, int image,
834 const tchar *target, int extract_flags,
835 wimlib_progress_func_t progress_func)
838 tchar *target_copy = canonicalize_fs_path(target);
840 return WIMLIB_ERR_NOMEM;
841 struct wimlib_extract_command cmd = {
842 .wim_source_path = T(""),
843 .fs_dest_path = target_copy,
844 .extract_flags = extract_flags,
846 ret = do_wimlib_extract_files(wim, image, &cmd, 1, progress_func);
851 static const tchar * const filename_forbidden_chars =
860 /* This function checks if it is okay to use a WIM image's name as a directory
863 image_name_ok_as_dir(const tchar *image_name)
865 return image_name && *image_name &&
866 !tstrpbrk(image_name, filename_forbidden_chars) &&
867 tstrcmp(image_name, T(".")) &&
868 tstrcmp(image_name, T(".."));
871 /* Extracts all images from the WIM to the directory @target, with the images
872 * placed in subdirectories named by their image names. */
874 extract_all_images(WIMStruct *wim,
877 wimlib_progress_func_t progress_func)
879 size_t image_name_max_len = max(xml_get_max_image_name_len(wim), 20);
880 size_t output_path_len = tstrlen(target);
881 tchar buf[output_path_len + 1 + image_name_max_len + 1];
884 const tchar *image_name;
887 if (tstat(target, &stbuf)) {
890 if (tmkdir(target, S_IRWXU | S_IRGRP | S_IXGRP |
893 ERROR_WITH_ERRNO("Failed to create directory \"%"TS"\"", target);
894 return WIMLIB_ERR_MKDIR;
897 ERROR_WITH_ERRNO("Failed to stat \"%"TS"\"", target);
898 return WIMLIB_ERR_STAT;
900 } else if (!S_ISDIR(stbuf.st_mode)) {
901 ERROR("\"%"TS"\" is not a directory", target);
902 return WIMLIB_ERR_NOTDIR;
905 tmemcpy(buf, target, output_path_len);
906 buf[output_path_len] = T('/');
907 for (image = 1; image <= wim->hdr.image_count; image++) {
908 image_name = wimlib_get_image_name(wim, image);
909 if (image_name_ok_as_dir(image_name)) {
910 tstrcpy(buf + output_path_len + 1, image_name);
912 /* Image name is empty or contains forbidden characters.
913 * Use image number instead. */
914 tsprintf(buf + output_path_len + 1, T("%d"), image);
916 ret = extract_single_image(wim, image, buf, extract_flags,
924 /* Extracts a single image or all images from a WIM file to a directory or NTFS
927 wimlib_extract_image(WIMStruct *wim,
931 WIMStruct **additional_swms,
932 unsigned num_additional_swms,
933 wimlib_progress_func_t progress_func)
935 struct wim_lookup_table *joined_tab, *wim_tab_save;
938 extract_flags &= WIMLIB_EXTRACT_MASK_PUBLIC;
940 ret = verify_swm_set(wim, additional_swms, num_additional_swms);
944 if (num_additional_swms) {
945 ret = new_joined_lookup_table(wim, additional_swms,
946 num_additional_swms, &joined_tab);
949 wim_tab_save = wim->lookup_table;
950 wim->lookup_table = joined_tab;
953 if (image == WIMLIB_ALL_IMAGES) {
954 ret = extract_all_images(wim, target,
955 extract_flags | WIMLIB_EXTRACT_FLAG_MULTI_IMAGE,
958 ret = extract_single_image(wim, image, target, extract_flags,
962 if (extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
963 WIMLIB_EXTRACT_FLAG_HARDLINK))
965 for_lookup_table_entry(wim->lookup_table,
966 lte_free_extracted_file,
969 if (num_additional_swms) {
970 free_lookup_table(wim->lookup_table);
971 wim->lookup_table = wim_tab_save;