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 /*print_dentry(dentry, NULL);*/
63 /*ERROR("%"TS" %"TS, args->target, dentry->_full_path);*/
66 tmemcpy(p, args->target, args->target_nchars);
67 p += args->target_nchars;
69 if (dentry != args->extract_root) {
71 tmemcpy(p, full_path + args->wim_source_path_nchars,
72 full_path_nchars - args->wim_source_path_nchars);
73 p += full_path_nchars - args->wim_source_path_nchars;
76 return (*apply_dentry_func)(output_path, p - output_path,
81 /* Extracts a file, directory, or symbolic link from the WIM archive. */
83 apply_dentry_normal(struct wim_dentry *dentry, void *arg)
86 return do_apply_op(dentry, arg, win32_do_apply_dentry);
88 return do_apply_op(dentry, arg, unix_do_apply_dentry);
93 /* Apply timestamps to an extracted file or directory */
95 apply_dentry_timestamps_normal(struct wim_dentry *dentry, void *arg)
98 return do_apply_op(dentry, arg, win32_do_apply_dentry_timestamps);
100 return do_apply_op(dentry, arg, unix_do_apply_dentry_timestamps);
104 /* Extract a dentry if it hasn't already been extracted and either
105 * WIMLIB_EXTRACT_FLAG_NO_STREAMS is not specified, or the dentry is a directory
106 * and/or has no unnamed stream. */
108 maybe_apply_dentry(struct wim_dentry *dentry, void *arg)
110 struct apply_args *args = arg;
113 if (!dentry->needs_extraction)
116 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_NO_STREAMS &&
117 !dentry_is_directory(dentry) &&
118 inode_unnamed_lte_resolved(dentry->d_inode) != NULL)
121 if ((args->extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE) &&
122 args->progress_func) {
123 args->progress.extract.cur_path = dentry->_full_path;
124 args->progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY,
127 ret = args->apply_dentry(dentry, args);
129 dentry->needs_extraction = 0;
134 calculate_bytes_to_extract(struct list_head *stream_list,
136 union wimlib_progress_info *progress)
138 struct wim_lookup_table_entry *lte;
142 /* For each stream to be extracted... */
143 list_for_each_entry(lte, stream_list, extraction_list) {
145 (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
147 /* In the symlink or hard link extraction mode, each
148 * stream will be extracted one time regardless of how
149 * many dentries share the stream. */
150 wimlib_assert(!(extract_flags & WIMLIB_EXTRACT_FLAG_NTFS));
151 if (!lte->extracted_file) {
153 total_bytes += wim_resource_size(lte);
156 num_streams += lte->out_refcnt;
157 total_bytes += lte->out_refcnt * wim_resource_size(lte);
160 progress->extract.num_streams = num_streams;
161 progress->extract.total_bytes = total_bytes;
162 progress->extract.completed_bytes = 0;
166 maybe_add_stream_for_extraction(struct wim_lookup_table_entry *lte,
167 struct list_head *stream_list)
169 if (++lte->out_refcnt == 1) {
170 INIT_LIST_HEAD(<e->lte_dentry_list);
171 list_add_tail(<e->extraction_list, stream_list);
175 struct find_streams_ctx {
176 struct list_head stream_list;
181 dentry_find_streams_to_extract(struct wim_dentry *dentry, void *_ctx)
183 struct find_streams_ctx *ctx = _ctx;
184 struct wim_inode *inode = dentry->d_inode;
185 struct wim_lookup_table_entry *lte;
186 bool dentry_added = false;
187 struct list_head *stream_list = &ctx->stream_list;
188 int extract_flags = ctx->extract_flags;
190 dentry->needs_extraction = 1;
192 lte = inode_unnamed_lte_resolved(inode);
194 maybe_add_stream_for_extraction(lte, stream_list);
195 list_add_tail(&dentry->tmp_list, <e->lte_dentry_list);
199 /* Determine whether to include alternate data stream entries or not.
201 * UNIX: Include them if extracting using NTFS-3g.
203 * Windows: Include them undconditionally, although if the filesystem is
204 * not NTFS we won't actually be able to extract them. */
205 #if defined(WITH_NTFS_3G)
206 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS)
207 #elif defined(__WIN32__)
213 for (unsigned i = 0; i < inode->i_num_ads; i++) {
214 if (inode->i_ads_entries[i].stream_name_nbytes != 0) {
215 lte = inode->i_ads_entries[i].lte;
217 maybe_add_stream_for_extraction(lte,
220 list_add_tail(&dentry->tmp_list,
221 <e->lte_dentry_list);
232 dentry_resolve_and_zero_lte_refcnt(struct wim_dentry *dentry, void *_lookup_table)
234 struct wim_inode *inode = dentry->d_inode;
235 struct wim_lookup_table *lookup_table = _lookup_table;
236 struct wim_lookup_table_entry *lte;
238 inode_resolve_ltes(inode, lookup_table);
239 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
240 lte = inode_stream_lte_resolved(inode, i);
248 find_streams_for_extraction(struct wim_dentry *root,
249 struct list_head *stream_list,
250 struct wim_lookup_table *lookup_table,
253 struct find_streams_ctx ctx;
255 INIT_LIST_HEAD(&ctx.stream_list);
256 ctx.extract_flags = extract_flags;
257 for_dentry_in_tree(root, dentry_resolve_and_zero_lte_refcnt, lookup_table);
258 for_dentry_in_tree(root, dentry_find_streams_to_extract, &ctx);
259 list_transfer(&ctx.stream_list, stream_list);
263 dentry_reset_needs_extraction(struct wim_dentry *dentry, void *_ignore)
265 dentry->needs_extraction = 0;
269 struct apply_operations {
270 int (*apply_dentry)(struct wim_dentry *dentry, void *arg);
271 int (*apply_dentry_timestamps)(struct wim_dentry *dentry, void *arg);
274 static const struct apply_operations normal_apply_operations = {
275 .apply_dentry = apply_dentry_normal,
276 .apply_dentry_timestamps = apply_dentry_timestamps_normal,
280 static const struct apply_operations ntfs_apply_operations = {
281 .apply_dentry = apply_dentry_ntfs,
282 .apply_dentry_timestamps = apply_dentry_timestamps_ntfs,
287 apply_stream_list(struct list_head *stream_list,
288 struct apply_args *args,
289 const struct apply_operations *ops,
290 wimlib_progress_func_t progress_func)
292 uint64_t bytes_per_progress = args->progress.extract.total_bytes / 100;
293 uint64_t next_progress = bytes_per_progress;
294 struct wim_lookup_table_entry *lte;
295 struct wim_dentry *dentry;
298 /* This complicated loop is essentially looping through the dentries,
299 * although dentries may be visited more than once (if a dentry contains
300 * two different nonempty streams) or not at all (if a dentry contains
301 * no non-empty streams).
303 * The outer loop is over the distinct streams to be extracted so that
304 * sequential reading of the WIM can be implemented. */
306 /* For each distinct stream to be extracted */
307 list_for_each_entry(lte, stream_list, extraction_list) {
308 /* For each dentry to be extracted that is a name for an inode
309 * containing the stream */
310 list_for_each_entry(dentry, <e->lte_dentry_list, tmp_list) {
311 /* Extract the dentry if it was not already
313 ret = maybe_apply_dentry(dentry, args);
317 args->progress.extract.completed_bytes >= next_progress)
319 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS,
321 if (args->progress.extract.completed_bytes >=
322 args->progress.extract.total_bytes)
324 next_progress = ~0ULL;
327 min (args->progress.extract.completed_bytes +
329 args->progress.extract.total_bytes);
338 sort_stream_list_by_wim_position(struct list_head *stream_list)
340 struct list_head *cur;
342 struct wim_lookup_table_entry **array;
347 list_for_each(cur, stream_list)
349 array_size = num_streams * sizeof(array[0]);
350 array = MALLOC(array_size);
352 ERROR("Failed to allocate %zu bytes to sort stream entries",
354 return WIMLIB_ERR_NOMEM;
356 cur = stream_list->next;
357 for (i = 0; i < num_streams; i++) {
358 array[i] = container_of(cur, struct wim_lookup_table_entry, extraction_list);
362 qsort(array, num_streams, sizeof(array[0]), cmp_streams_by_wim_position);
364 INIT_LIST_HEAD(stream_list);
365 for (i = 0; i < num_streams; i++)
366 list_add_tail(&array[i]->extraction_list, stream_list);
372 * Extract a dentry to standard output.
374 * This obviously doesn't make sense in all cases. We return an error if the
375 * dentry does not correspond to a regular file. Otherwise we extract the
376 * unnamed data stream only.
379 extract_dentry_to_stdout(struct wim_dentry *dentry)
382 if (!dentry_is_regular_file(dentry)) {
383 ERROR("\"%"TS"\" is not a regular file and therefore cannot be "
384 "extracted to standard output", dentry->_full_path);
385 ret = WIMLIB_ERR_NOT_A_REGULAR_FILE;
387 struct wim_lookup_table_entry *lte;
389 lte = inode_unnamed_lte_resolved(dentry->d_inode);
391 ret = extract_wim_resource_to_fd(lte, STDOUT_FILENO,
392 wim_resource_size(lte));
399 * extract_tree - Extract a file or directory tree from the currently selected
402 * @wim: WIMStruct for the WIM file, with the desired image selected
403 * (as wim->current_image).
405 * "Canonical" (i.e. no leading or trailing slashes, path
406 * separators forwald slashes) path inside the WIM image to
407 * extract. An empty string means the full image.
409 * Filesystem path to extract the file or directory tree to.
412 * WIMLIB_EXTRACT_FLAG_*. Also, the private flag
413 * WIMLIB_EXTRACT_FLAG_MULTI_IMAGE will be set if this is being
414 * called through wimlib_extract_image() with WIMLIB_ALL_IMAGES as
418 * If non-NULL, progress function for the extraction. The messages
419 * we may in this function are:
421 * WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN or
422 * WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN;
423 * WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN;
424 * WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_END;
425 * WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY;
426 * WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS;
427 * WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS;
428 * WIMLIB_PROGRESS_MSG_EXTRACT_TREE_END or
429 * WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END.
431 * Returns 0 on success; nonzero on failure.
434 extract_tree(WIMStruct *wim, const tchar *wim_source_path, const tchar *target,
435 int extract_flags, wimlib_progress_func_t progress_func)
438 struct list_head stream_list;
439 struct apply_args args;
440 const struct apply_operations *ops;
441 struct wim_dentry *root;
443 memset(&args, 0, sizeof(args));
446 args.target = target;
447 args.extract_flags = extract_flags;
448 args.progress_func = progress_func;
449 args.target_nchars = tstrlen(target);
450 args.wim_source_path_nchars = tstrlen(wim_source_path);
453 args.progress.extract.wimfile_name = wim->filename;
454 args.progress.extract.image = wim->current_image;
455 args.progress.extract.extract_flags = (extract_flags &
456 WIMLIB_EXTRACT_MASK_PUBLIC);
457 args.progress.extract.image_name = wimlib_get_image_name(wim,
459 args.progress.extract.extract_root_wim_source_path = wim_source_path;
460 args.progress.extract.target = target;
464 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
465 args.vol = ntfs_mount(target, 0);
467 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%"TS"'",
469 ret = WIMLIB_ERR_NTFS_3G;
472 ops = &ntfs_apply_operations;
475 ops = &normal_apply_operations;
477 root = get_dentry(wim, wim_source_path);
479 ERROR("Path \"%"TS"\" does not exist in WIM image %d",
480 wim_source_path, wim->current_image);
481 ret = WIMLIB_ERR_PATH_DOES_NOT_EXIST;
482 goto out_ntfs_umount;
484 args.extract_root = root;
486 ret = calculate_dentry_tree_full_paths(root);
488 goto out_ntfs_umount;
490 /* Build a list of the streams that need to be extracted */
491 find_streams_for_extraction(root,
493 wim->lookup_table, extract_flags);
495 /* Calculate the number of bytes of data that will be extracted */
496 calculate_bytes_to_extract(&stream_list, extract_flags,
499 if (extract_flags & WIMLIB_EXTRACT_FLAG_TO_STDOUT) {
500 ret = extract_dentry_to_stdout(root);
501 goto out_dentry_reset_needs_extraction;
505 progress_func(*wim_source_path ? WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN :
506 WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN,
510 /* If a sequential extraction was specified, sort the streams to be
511 * extracted by their position in the WIM file, so that the WIM file can
512 * be read sequentially. */
513 if (extract_flags & WIMLIB_EXTRACT_FLAG_SEQUENTIAL) {
514 ret = sort_stream_list_by_wim_position(&stream_list);
516 WARNING("Falling back to non-sequential extraction");
517 extract_flags &= ~WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
522 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN,
526 /* Make the directory structure and extract empty files */
527 args.extract_flags |= WIMLIB_EXTRACT_FLAG_NO_STREAMS;
528 args.apply_dentry = ops->apply_dentry;
529 ret = for_dentry_in_tree(root, maybe_apply_dentry, &args);
530 args.extract_flags &= ~WIMLIB_EXTRACT_FLAG_NO_STREAMS;
532 goto out_dentry_reset_needs_extraction;
535 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_END,
539 if (extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX) {
540 args.target_realpath = realpath(target, NULL);
541 if (!args.target_realpath) {
542 ret = WIMLIB_ERR_NOMEM;
543 goto out_dentry_reset_needs_extraction;
545 args.target_realpath_len = tstrlen(args.target_realpath);
548 /* Extract non-empty files */
549 ret = apply_stream_list(&stream_list, &args, ops, progress_func);
551 goto out_free_target_realpath;
554 progress_func(WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS,
558 /* Apply timestamps */
559 ret = for_dentry_in_tree_depth(root,
560 ops->apply_dentry_timestamps, &args);
562 goto out_free_target_realpath;
565 progress_func(*wim_source_path ? WIMLIB_PROGRESS_MSG_EXTRACT_TREE_END :
566 WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END,
569 out_free_target_realpath:
570 FREE(args.target_realpath);
571 out_dentry_reset_needs_extraction:
572 for_dentry_in_tree(root, dentry_reset_needs_extraction, NULL);
575 /* Unmount the NTFS volume */
576 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
577 if (ntfs_umount(args.vol, FALSE) != 0) {
578 ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%"TS"'",
581 ret = WIMLIB_ERR_NTFS_3G;
589 /* Validates a single wimlib_extract_command, mostly checking to make sure the
590 * extract flags make sense. */
592 check_extract_command(struct wimlib_extract_command *cmd, int wim_header_flags)
595 bool is_entire_image = (cmd->wim_source_path[0] == T('\0'));
597 /* Empty destination path? */
598 if (cmd->fs_dest_path[0] == T('\0'))
599 return WIMLIB_ERR_INVALID_PARAM;
601 extract_flags = cmd->extract_flags;
603 /* Specified both symlink and hardlink modes? */
605 (WIMLIB_EXTRACT_FLAG_SYMLINK |
606 WIMLIB_EXTRACT_FLAG_HARDLINK)) == (WIMLIB_EXTRACT_FLAG_SYMLINK |
607 WIMLIB_EXTRACT_FLAG_HARDLINK))
608 return WIMLIB_ERR_INVALID_PARAM;
611 /* Wanted UNIX data on Windows? */
612 if (extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
613 ERROR("Extracting UNIX data is not supported on Windows");
614 return WIMLIB_ERR_INVALID_PARAM;
616 /* Wanted linked extraction on Windows? (XXX This is possible, just not
617 * implemented yet.) */
618 if (extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
619 WIMLIB_EXTRACT_FLAG_HARDLINK))
621 ERROR("Linked extraction modes are not supported on Windows");
622 return WIMLIB_ERR_INVALID_PARAM;
626 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
627 /* NTFS-3g extraction mode requested */
629 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
630 WIMLIB_EXTRACT_FLAG_HARDLINK))) {
631 ERROR("Cannot specify symlink or hardlink flags when applying\n"
632 " directly to a NTFS volume");
633 return WIMLIB_ERR_INVALID_PARAM;
635 if (!is_entire_image &&
636 (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS))
638 ERROR("When applying directly to a NTFS volume you can "
639 "only extract a full image, not part of one");
640 return WIMLIB_ERR_INVALID_PARAM;
642 if (extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
643 ERROR("Cannot restore UNIX-specific data in "
644 "the NTFS extraction mode");
645 return WIMLIB_ERR_INVALID_PARAM;
648 ERROR("wimlib was compiled without support for NTFS-3g, so");
649 ERROR("we cannot apply a WIM image directly to a NTFS volume");
650 return WIMLIB_ERR_UNSUPPORTED;
654 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_RPFIX |
655 WIMLIB_EXTRACT_FLAG_NORPFIX)) ==
656 (WIMLIB_EXTRACT_FLAG_RPFIX | WIMLIB_EXTRACT_FLAG_NORPFIX))
658 ERROR("Cannot specify RPFIX and NORPFIX flags at the same time!");
659 return WIMLIB_ERR_INVALID_PARAM;
662 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_RPFIX |
663 WIMLIB_EXTRACT_FLAG_NORPFIX)) == 0)
665 /* Do reparse point fixups by default if the WIM header says
666 * they are enabled and we are extracting a full image. */
667 if ((wim_header_flags & WIM_HDR_FLAG_RP_FIX) && is_entire_image)
668 extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
671 if (!is_entire_image && (extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX)) {
672 ERROR("Cannot specify --rpfix when not extracting entire image");
673 return WIMLIB_ERR_INVALID_PARAM;
676 cmd->extract_flags = extract_flags;
681 /* Internal function to execute extraction commands for a WIM image. */
683 do_wimlib_extract_files(WIMStruct *wim,
685 struct wimlib_extract_command *cmds,
687 wimlib_progress_func_t progress_func)
690 bool found_link_cmd = false;
691 bool found_nolink_cmd = false;
693 /* Select the image from which we are extracting files */
694 ret = select_wim_image(wim, image);
698 /* Make sure there are no streams in the WIM that have not been
699 * checksummed yet. */
700 ret = wim_checksum_unhashed_streams(wim);
704 /* Check for problems with the extraction commands */
705 for (size_t i = 0; i < num_cmds; i++) {
706 ret = check_extract_command(&cmds[i], wim->hdr.flags);
709 if (cmds[i].extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
710 WIMLIB_EXTRACT_FLAG_HARDLINK)) {
711 found_link_cmd = true;
713 found_nolink_cmd = true;
715 if (found_link_cmd && found_nolink_cmd) {
716 ERROR("Symlink or hardlink extraction mode must "
717 "be set on all extraction commands");
718 return WIMLIB_ERR_INVALID_PARAM;
722 /* Execute the extraction commands */
723 for (size_t i = 0; i < num_cmds; i++) {
724 ret = extract_tree(wim,
725 cmds[i].wim_source_path,
726 cmds[i].fs_dest_path,
727 cmds[i].extract_flags,
735 /* Extract files or directories from a WIM image. */
737 wimlib_extract_files(WIMStruct *wim,
739 const struct wimlib_extract_command *cmds,
741 int default_extract_flags,
742 WIMStruct **additional_swms,
743 unsigned num_additional_swms,
744 wimlib_progress_func_t progress_func)
747 struct wimlib_extract_command *cmds_copy;
748 struct wim_lookup_table *wim_tab_save, *joined_tab;
751 default_extract_flags &= WIMLIB_EXTRACT_MASK_PUBLIC;
753 ret = verify_swm_set(wim, additional_swms, num_additional_swms);
760 if (num_additional_swms) {
761 ret = new_joined_lookup_table(wim, additional_swms,
766 wim_tab_save = wim->lookup_table;
767 wim->lookup_table = joined_tab;
770 cmds_copy = CALLOC(num_cmds, sizeof(cmds[0]));
772 ret = WIMLIB_ERR_NOMEM;
773 goto out_restore_lookup_table;
776 for (size_t i = 0; i < num_cmds; i++) {
777 cmds_copy[i].extract_flags = (default_extract_flags |
778 cmds[i].extract_flags)
779 & WIMLIB_EXTRACT_MASK_PUBLIC;
780 all_flags |= cmds_copy[i].extract_flags;
782 cmds_copy[i].wim_source_path = canonicalize_wim_path(cmds[i].wim_source_path);
783 if (!cmds_copy[i].wim_source_path) {
784 ret = WIMLIB_ERR_NOMEM;
785 goto out_free_cmds_copy;
788 cmds_copy[i].fs_dest_path = canonicalize_fs_path(cmds[i].fs_dest_path);
789 if (!cmds_copy[i].fs_dest_path) {
790 ret = WIMLIB_ERR_NOMEM;
791 goto out_free_cmds_copy;
795 ret = do_wimlib_extract_files(wim, image,
799 if (all_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
800 WIMLIB_EXTRACT_FLAG_HARDLINK))
802 for_lookup_table_entry(wim->lookup_table,
803 lte_free_extracted_file, NULL);
806 for (size_t i = 0; i < num_cmds; i++) {
807 FREE(cmds_copy[i].wim_source_path);
808 FREE(cmds_copy[i].fs_dest_path);
811 out_restore_lookup_table:
812 if (num_additional_swms) {
813 free_lookup_table(wim->lookup_table);
814 wim->lookup_table = wim_tab_save;
821 * Extracts an image from a WIM file.
823 * @wim: WIMStruct for the WIM file.
825 * @image: Number of the single image to extract.
827 * @target: Directory or NTFS volume to extract the image to.
829 * @extract_flags: Bitwise or of WIMLIB_EXTRACT_FLAG_*.
831 * @progress_func: If non-NULL, a progress function to be called
834 * Returns 0 on success; nonzero on failure.
837 extract_single_image(WIMStruct *wim, int image,
838 const tchar *target, int extract_flags,
839 wimlib_progress_func_t progress_func)
842 tchar *target_copy = canonicalize_fs_path(target);
844 return WIMLIB_ERR_NOMEM;
845 struct wimlib_extract_command cmd = {
846 .wim_source_path = T(""),
847 .fs_dest_path = target_copy,
848 .extract_flags = extract_flags,
850 ret = do_wimlib_extract_files(wim, image, &cmd, 1, progress_func);
855 static const tchar * const filename_forbidden_chars =
864 /* This function checks if it is okay to use a WIM image's name as a directory
867 image_name_ok_as_dir(const tchar *image_name)
869 return image_name && *image_name &&
870 !tstrpbrk(image_name, filename_forbidden_chars) &&
871 tstrcmp(image_name, T(".")) &&
872 tstrcmp(image_name, T(".."));
875 /* Extracts all images from the WIM to the directory @target, with the images
876 * placed in subdirectories named by their image names. */
878 extract_all_images(WIMStruct *wim,
881 wimlib_progress_func_t progress_func)
883 size_t image_name_max_len = max(xml_get_max_image_name_len(wim), 20);
884 size_t output_path_len = tstrlen(target);
885 tchar buf[output_path_len + 1 + image_name_max_len + 1];
888 const tchar *image_name;
891 if (tstat(target, &stbuf)) {
894 if (tmkdir(target, S_IRWXU | S_IRGRP | S_IXGRP |
897 ERROR_WITH_ERRNO("Failed to create directory \"%"TS"\"", target);
898 return WIMLIB_ERR_MKDIR;
901 ERROR_WITH_ERRNO("Failed to stat \"%"TS"\"", target);
902 return WIMLIB_ERR_STAT;
904 } else if (!S_ISDIR(stbuf.st_mode)) {
905 ERROR("\"%"TS"\" is not a directory", target);
906 return WIMLIB_ERR_NOTDIR;
909 tmemcpy(buf, target, output_path_len);
910 buf[output_path_len] = T('/');
911 for (image = 1; image <= wim->hdr.image_count; image++) {
912 image_name = wimlib_get_image_name(wim, image);
913 if (image_name_ok_as_dir(image_name)) {
914 tstrcpy(buf + output_path_len + 1, image_name);
916 /* Image name is empty or contains forbidden characters.
917 * Use image number instead. */
918 tsprintf(buf + output_path_len + 1, T("%d"), image);
920 ret = extract_single_image(wim, image, buf, extract_flags,
928 /* Extracts a single image or all images from a WIM file to a directory or NTFS
931 wimlib_extract_image(WIMStruct *wim,
935 WIMStruct **additional_swms,
936 unsigned num_additional_swms,
937 wimlib_progress_func_t progress_func)
939 struct wim_lookup_table *joined_tab, *wim_tab_save;
942 extract_flags &= WIMLIB_EXTRACT_MASK_PUBLIC;
944 ret = verify_swm_set(wim, additional_swms, num_additional_swms);
948 if (num_additional_swms) {
949 ret = new_joined_lookup_table(wim, additional_swms,
950 num_additional_swms, &joined_tab);
953 wim_tab_save = wim->lookup_table;
954 wim->lookup_table = joined_tab;
957 if (image == WIMLIB_ALL_IMAGES) {
958 ret = extract_all_images(wim, target,
959 extract_flags | WIMLIB_EXTRACT_FLAG_MULTI_IMAGE,
962 ret = extract_single_image(wim, image, target, extract_flags,
966 if (extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
967 WIMLIB_EXTRACT_FLAG_HARDLINK))
969 for_lookup_table_entry(wim->lookup_table,
970 lte_free_extracted_file,
973 if (num_additional_swms) {
974 free_lookup_table(wim->lookup_table);
975 wim->lookup_table = wim_tab_save;