4 * Support for extracting WIM images, or files or directories contained in a WIM
9 * Copyright (C) 2012, 2013 Eric Biggers
11 * This file is part of wimlib, a library for working with WIM files.
13 * wimlib is free software; you can redistribute it and/or modify it under the
14 * terms of the GNU General Public License as published by the Free
15 * Software Foundation; either version 3 of the License, or (at your option)
18 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
19 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
20 * A PARTICULAR PURPOSE. See the GNU General Public License for more
23 * You should have received a copy of the GNU General Public License
24 * along with wimlib; if not, see http://www.gnu.org/licenses/.
31 #include "wimlib/apply.h"
32 #include "wimlib/dentry.h"
33 #include "wimlib/error.h"
34 #include "wimlib/lookup_table.h"
35 #include "wimlib/paths.h"
36 #include "wimlib/resource.h"
37 #include "wimlib/swm.h"
39 # include "wimlib/win32.h" /* for realpath() equivalent */
41 #include "wimlib/xml.h"
46 # include <ntfs-3g/volume.h> /* for ntfs_mount(), ntfs_umount() */
53 do_apply_op(struct wim_dentry *dentry, struct apply_args *args,
54 int (*apply_dentry_func)(const tchar *, size_t,
55 struct wim_dentry *, struct apply_args *))
58 const tchar *full_path;
59 size_t full_path_nchars;
61 wimlib_assert(dentry->_full_path != NULL);
62 full_path = dentry->_full_path + 1;
63 full_path_nchars = dentry->full_path_nbytes / sizeof(tchar) - 1;
64 tchar output_path[args->target_nchars + 1 +
65 (full_path_nchars - args->wim_source_path_nchars) + 1];
68 tmemcpy(p, args->target, args->target_nchars);
69 p += args->target_nchars;
71 if (dentry != args->extract_root) {
73 tmemcpy(p, full_path + args->wim_source_path_nchars,
74 full_path_nchars - args->wim_source_path_nchars);
75 p += full_path_nchars - args->wim_source_path_nchars;
78 return (*apply_dentry_func)(output_path, p - output_path,
83 /* Extracts a file, directory, or symbolic link from the WIM archive. */
85 apply_dentry_normal(struct wim_dentry *dentry, void *arg)
88 return do_apply_op(dentry, arg, win32_do_apply_dentry);
90 return do_apply_op(dentry, arg, unix_do_apply_dentry);
95 /* Apply timestamps to an extracted file or directory */
97 apply_dentry_timestamps_normal(struct wim_dentry *dentry, void *arg)
100 return do_apply_op(dentry, arg, win32_do_apply_dentry_timestamps);
102 return do_apply_op(dentry, arg, unix_do_apply_dentry_timestamps);
106 /* Extract a dentry if it hasn't already been extracted and either
107 * WIMLIB_EXTRACT_FLAG_NO_STREAMS is not specified, or the dentry is a directory
108 * and/or has no unnamed stream. */
110 maybe_apply_dentry(struct wim_dentry *dentry, void *arg)
112 struct apply_args *args = arg;
115 if (!dentry->needs_extraction)
118 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_NO_STREAMS &&
119 !dentry_is_directory(dentry) &&
120 inode_unnamed_lte_resolved(dentry->d_inode) != NULL)
123 if ((args->extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE) &&
124 args->progress_func) {
125 args->progress.extract.cur_path = dentry->_full_path;
126 args->progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY,
129 ret = args->apply_dentry(dentry, args);
131 dentry->needs_extraction = 0;
136 calculate_bytes_to_extract(struct list_head *stream_list,
138 union wimlib_progress_info *progress)
140 struct wim_lookup_table_entry *lte;
144 /* For each stream to be extracted... */
145 list_for_each_entry(lte, stream_list, extraction_list) {
147 (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
149 /* In the symlink or hard link extraction mode, each
150 * stream will be extracted one time regardless of how
151 * many dentries share the stream. */
152 wimlib_assert(!(extract_flags & WIMLIB_EXTRACT_FLAG_NTFS));
153 if (!lte->extracted_file) {
155 total_bytes += wim_resource_size(lte);
158 num_streams += lte->out_refcnt;
159 total_bytes += lte->out_refcnt * wim_resource_size(lte);
162 progress->extract.num_streams = num_streams;
163 progress->extract.total_bytes = total_bytes;
164 progress->extract.completed_bytes = 0;
168 maybe_add_stream_for_extraction(struct wim_lookup_table_entry *lte,
169 struct list_head *stream_list)
171 if (++lte->out_refcnt == 1) {
172 INIT_LIST_HEAD(<e->lte_dentry_list);
173 list_add_tail(<e->extraction_list, stream_list);
177 struct find_streams_ctx {
178 struct list_head stream_list;
183 dentry_find_streams_to_extract(struct wim_dentry *dentry, void *_ctx)
185 struct find_streams_ctx *ctx = _ctx;
186 struct wim_inode *inode = dentry->d_inode;
187 struct wim_lookup_table_entry *lte;
188 bool dentry_added = false;
189 struct list_head *stream_list = &ctx->stream_list;
190 int extract_flags = ctx->extract_flags;
192 dentry->needs_extraction = 1;
194 lte = inode_unnamed_lte_resolved(inode);
196 if (!inode->i_visited)
197 maybe_add_stream_for_extraction(lte, stream_list);
198 list_add_tail(&dentry->tmp_list, <e->lte_dentry_list);
202 /* Determine whether to include alternate data stream entries or not.
204 * UNIX: Include them if extracting using NTFS-3g.
206 * Windows: Include them undconditionally, although if the filesystem is
207 * not NTFS we won't actually be able to extract them. */
208 #if defined(WITH_NTFS_3G)
209 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS)
210 #elif defined(__WIN32__)
216 for (unsigned i = 0; i < inode->i_num_ads; i++) {
217 if (inode->i_ads_entries[i].stream_name_nbytes != 0) {
218 lte = inode->i_ads_entries[i].lte;
220 if (!inode->i_visited) {
221 maybe_add_stream_for_extraction(lte,
225 list_add_tail(&dentry->tmp_list,
226 <e->lte_dentry_list);
233 inode->i_visited = 1;
238 dentry_resolve_and_zero_lte_refcnt(struct wim_dentry *dentry, void *_lookup_table)
240 struct wim_inode *inode = dentry->d_inode;
241 struct wim_lookup_table *lookup_table = _lookup_table;
242 struct wim_lookup_table_entry *lte;
244 inode_resolve_ltes(inode, lookup_table);
245 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
246 lte = inode_stream_lte_resolved(inode, i);
254 find_streams_for_extraction(struct wim_dentry *root,
255 struct list_head *stream_list,
256 struct wim_lookup_table *lookup_table,
259 struct find_streams_ctx ctx;
261 INIT_LIST_HEAD(&ctx.stream_list);
262 ctx.extract_flags = extract_flags;
263 for_dentry_in_tree(root, dentry_resolve_and_zero_lte_refcnt, lookup_table);
264 for_dentry_in_tree(root, dentry_find_streams_to_extract, &ctx);
265 list_transfer(&ctx.stream_list, stream_list);
269 dentry_reset_needs_extraction(struct wim_dentry *dentry, void *_ignore)
271 dentry->needs_extraction = 0;
272 dentry->d_inode->i_visited = 0;
276 struct apply_operations {
277 int (*apply_dentry)(struct wim_dentry *dentry, void *arg);
278 int (*apply_dentry_timestamps)(struct wim_dentry *dentry, void *arg);
281 static const struct apply_operations normal_apply_operations = {
282 .apply_dentry = apply_dentry_normal,
283 .apply_dentry_timestamps = apply_dentry_timestamps_normal,
287 static const struct apply_operations ntfs_apply_operations = {
288 .apply_dentry = apply_dentry_ntfs,
289 .apply_dentry_timestamps = apply_dentry_timestamps_ntfs,
294 apply_stream_list(struct list_head *stream_list,
295 struct apply_args *args,
296 const struct apply_operations *ops,
297 wimlib_progress_func_t progress_func)
299 uint64_t bytes_per_progress = args->progress.extract.total_bytes / 100;
300 uint64_t next_progress = bytes_per_progress;
301 struct wim_lookup_table_entry *lte;
302 struct wim_dentry *dentry;
305 /* This complicated loop is essentially looping through the dentries,
306 * although dentries may be visited more than once (if a dentry contains
307 * two different nonempty streams) or not at all (if a dentry contains
308 * no non-empty streams).
310 * The outer loop is over the distinct streams to be extracted so that
311 * sequential reading of the WIM can be implemented. */
313 /* For each distinct stream to be extracted */
314 list_for_each_entry(lte, stream_list, extraction_list) {
315 /* For each dentry to be extracted that is a name for an inode
316 * containing the stream */
317 list_for_each_entry(dentry, <e->lte_dentry_list, tmp_list) {
318 /* Extract the dentry if it was not already
320 ret = maybe_apply_dentry(dentry, args);
324 args->progress.extract.completed_bytes >= next_progress)
326 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS,
328 if (args->progress.extract.completed_bytes >=
329 args->progress.extract.total_bytes)
331 next_progress = ~0ULL;
334 min (args->progress.extract.completed_bytes +
336 args->progress.extract.total_bytes);
345 sort_stream_list_by_wim_position(struct list_head *stream_list)
347 struct list_head *cur;
349 struct wim_lookup_table_entry **array;
354 list_for_each(cur, stream_list)
356 array_size = num_streams * sizeof(array[0]);
357 array = MALLOC(array_size);
359 ERROR("Failed to allocate %zu bytes to sort stream entries",
361 return WIMLIB_ERR_NOMEM;
363 cur = stream_list->next;
364 for (i = 0; i < num_streams; i++) {
365 array[i] = container_of(cur, struct wim_lookup_table_entry, extraction_list);
369 qsort(array, num_streams, sizeof(array[0]), cmp_streams_by_wim_position);
371 INIT_LIST_HEAD(stream_list);
372 for (i = 0; i < num_streams; i++)
373 list_add_tail(&array[i]->extraction_list, stream_list);
379 * Extract a dentry to standard output.
381 * This obviously doesn't make sense in all cases. We return an error if the
382 * dentry does not correspond to a regular file. Otherwise we extract the
383 * unnamed data stream only.
386 extract_dentry_to_stdout(struct wim_dentry *dentry)
389 if (!dentry_is_regular_file(dentry)) {
390 ERROR("\"%"TS"\" is not a regular file and therefore cannot be "
391 "extracted to standard output", dentry->_full_path);
392 ret = WIMLIB_ERR_NOT_A_REGULAR_FILE;
394 struct wim_lookup_table_entry *lte;
396 lte = inode_unnamed_lte_resolved(dentry->d_inode);
398 ret = extract_wim_resource_to_fd(lte, STDOUT_FILENO,
399 wim_resource_size(lte));
406 * extract_tree - Extract a file or directory tree from the currently selected
409 * @wim: WIMStruct for the WIM file, with the desired image selected
410 * (as wim->current_image).
412 * "Canonical" (i.e. no leading or trailing slashes, path
413 * separators forwald slashes) path inside the WIM image to
414 * extract. An empty string means the full image.
416 * Filesystem path to extract the file or directory tree to.
419 * WIMLIB_EXTRACT_FLAG_*. Also, the private flag
420 * WIMLIB_EXTRACT_FLAG_MULTI_IMAGE will be set if this is being
421 * called through wimlib_extract_image() with WIMLIB_ALL_IMAGES as
425 * If non-NULL, progress function for the extraction. The messages
426 * we may in this function are:
428 * WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN or
429 * WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN;
430 * WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN;
431 * WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_END;
432 * WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY;
433 * WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS;
434 * WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS;
435 * WIMLIB_PROGRESS_MSG_EXTRACT_TREE_END or
436 * WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END.
438 * Returns 0 on success; nonzero on failure.
441 extract_tree(WIMStruct *wim, const tchar *wim_source_path, const tchar *target,
442 int extract_flags, wimlib_progress_func_t progress_func)
445 struct list_head stream_list;
446 struct apply_args args;
447 const struct apply_operations *ops;
448 struct wim_dentry *root;
450 memset(&args, 0, sizeof(args));
453 args.target = target;
454 args.extract_flags = extract_flags;
455 args.progress_func = progress_func;
456 args.target_nchars = tstrlen(target);
457 args.wim_source_path_nchars = tstrlen(wim_source_path);
460 args.progress.extract.wimfile_name = wim->filename;
461 args.progress.extract.image = wim->current_image;
462 args.progress.extract.extract_flags = (extract_flags &
463 WIMLIB_EXTRACT_MASK_PUBLIC);
464 args.progress.extract.image_name = wimlib_get_image_name(wim,
466 args.progress.extract.extract_root_wim_source_path = wim_source_path;
467 args.progress.extract.target = target;
471 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
472 args.vol = ntfs_mount(target, 0);
474 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%"TS"'",
476 ret = WIMLIB_ERR_NTFS_3G;
479 ops = &ntfs_apply_operations;
482 ops = &normal_apply_operations;
484 root = get_dentry(wim, wim_source_path);
486 ERROR("Path \"%"TS"\" does not exist in WIM image %d",
487 wim_source_path, wim->current_image);
488 ret = WIMLIB_ERR_PATH_DOES_NOT_EXIST;
489 goto out_ntfs_umount;
491 args.extract_root = root;
493 ret = calculate_dentry_tree_full_paths(root);
495 goto out_ntfs_umount;
497 /* Build a list of the streams that need to be extracted */
498 find_streams_for_extraction(root,
500 wim->lookup_table, extract_flags);
502 /* Calculate the number of bytes of data that will be extracted */
503 calculate_bytes_to_extract(&stream_list, extract_flags,
506 if (extract_flags & WIMLIB_EXTRACT_FLAG_TO_STDOUT) {
507 ret = extract_dentry_to_stdout(root);
508 goto out_dentry_reset_needs_extraction;
512 progress_func(*wim_source_path ? WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN :
513 WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN,
517 /* If a sequential extraction was specified, sort the streams to be
518 * extracted by their position in the WIM file, so that the WIM file can
519 * be read sequentially. */
520 if (extract_flags & WIMLIB_EXTRACT_FLAG_SEQUENTIAL) {
521 ret = sort_stream_list_by_wim_position(&stream_list);
523 WARNING("Falling back to non-sequential extraction");
524 extract_flags &= ~WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
529 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN,
533 /* Make the directory structure and extract empty files */
534 args.extract_flags |= WIMLIB_EXTRACT_FLAG_NO_STREAMS;
535 args.apply_dentry = ops->apply_dentry;
536 ret = for_dentry_in_tree(root, maybe_apply_dentry, &args);
537 args.extract_flags &= ~WIMLIB_EXTRACT_FLAG_NO_STREAMS;
539 goto out_dentry_reset_needs_extraction;
542 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_END,
546 if (extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX) {
547 args.target_realpath = realpath(target, NULL);
548 if (!args.target_realpath) {
549 ret = WIMLIB_ERR_NOMEM;
550 goto out_dentry_reset_needs_extraction;
552 args.target_realpath_len = tstrlen(args.target_realpath);
555 /* Extract non-empty files */
556 ret = apply_stream_list(&stream_list, &args, ops, progress_func);
558 goto out_free_target_realpath;
561 progress_func(WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS,
565 /* Apply timestamps */
566 ret = for_dentry_in_tree_depth(root,
567 ops->apply_dentry_timestamps, &args);
569 goto out_free_target_realpath;
572 progress_func(*wim_source_path ? WIMLIB_PROGRESS_MSG_EXTRACT_TREE_END :
573 WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END,
576 out_free_target_realpath:
577 FREE(args.target_realpath);
578 out_dentry_reset_needs_extraction:
579 for_dentry_in_tree(root, dentry_reset_needs_extraction, NULL);
582 /* Unmount the NTFS volume */
583 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
584 if (ntfs_umount(args.vol, FALSE) != 0) {
585 ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%"TS"'",
588 ret = WIMLIB_ERR_NTFS_3G;
596 /* Validates a single wimlib_extract_command, mostly checking to make sure the
597 * extract flags make sense. */
599 check_extract_command(struct wimlib_extract_command *cmd, int wim_header_flags)
602 bool is_entire_image = (cmd->wim_source_path[0] == T('\0'));
604 /* Empty destination path? */
605 if (cmd->fs_dest_path[0] == T('\0'))
606 return WIMLIB_ERR_INVALID_PARAM;
608 extract_flags = cmd->extract_flags;
610 /* Specified both symlink and hardlink modes? */
612 (WIMLIB_EXTRACT_FLAG_SYMLINK |
613 WIMLIB_EXTRACT_FLAG_HARDLINK)) == (WIMLIB_EXTRACT_FLAG_SYMLINK |
614 WIMLIB_EXTRACT_FLAG_HARDLINK))
615 return WIMLIB_ERR_INVALID_PARAM;
618 /* Wanted UNIX data on Windows? */
619 if (extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
620 ERROR("Extracting UNIX data is not supported on Windows");
621 return WIMLIB_ERR_INVALID_PARAM;
623 /* Wanted linked extraction on Windows? (XXX This is possible, just not
624 * implemented yet.) */
625 if (extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
626 WIMLIB_EXTRACT_FLAG_HARDLINK))
628 ERROR("Linked extraction modes are not supported on Windows");
629 return WIMLIB_ERR_INVALID_PARAM;
633 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
634 /* NTFS-3g extraction mode requested */
636 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
637 WIMLIB_EXTRACT_FLAG_HARDLINK))) {
638 ERROR("Cannot specify symlink or hardlink flags when applying\n"
639 " directly to a NTFS volume");
640 return WIMLIB_ERR_INVALID_PARAM;
642 if (!is_entire_image &&
643 (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS))
645 ERROR("When applying directly to a NTFS volume you can "
646 "only extract a full image, not part of one");
647 return WIMLIB_ERR_INVALID_PARAM;
649 if (extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
650 ERROR("Cannot restore UNIX-specific data in "
651 "the NTFS extraction mode");
652 return WIMLIB_ERR_INVALID_PARAM;
655 ERROR("wimlib was compiled without support for NTFS-3g, so");
656 ERROR("we cannot apply a WIM image directly to a NTFS volume");
657 return WIMLIB_ERR_UNSUPPORTED;
661 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_RPFIX |
662 WIMLIB_EXTRACT_FLAG_NORPFIX)) ==
663 (WIMLIB_EXTRACT_FLAG_RPFIX | WIMLIB_EXTRACT_FLAG_NORPFIX))
665 ERROR("Cannot specify RPFIX and NORPFIX flags at the same time!");
666 return WIMLIB_ERR_INVALID_PARAM;
669 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_RPFIX |
670 WIMLIB_EXTRACT_FLAG_NORPFIX)) == 0)
672 /* Do reparse point fixups by default if the WIM header says
673 * they are enabled and we are extracting a full image. */
674 if ((wim_header_flags & WIM_HDR_FLAG_RP_FIX) && is_entire_image)
675 extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
678 if (!is_entire_image && (extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX)) {
679 ERROR("Cannot specify --rpfix when not extracting entire image");
680 return WIMLIB_ERR_INVALID_PARAM;
683 cmd->extract_flags = extract_flags;
688 /* Internal function to execute extraction commands for a WIM image. */
690 do_wimlib_extract_files(WIMStruct *wim,
692 struct wimlib_extract_command *cmds,
694 wimlib_progress_func_t progress_func)
697 bool found_link_cmd = false;
698 bool found_nolink_cmd = false;
700 /* Select the image from which we are extracting files */
701 ret = select_wim_image(wim, image);
705 /* Make sure there are no streams in the WIM that have not been
706 * checksummed yet. */
707 ret = wim_checksum_unhashed_streams(wim);
711 /* Check for problems with the extraction commands */
712 for (size_t i = 0; i < num_cmds; i++) {
713 ret = check_extract_command(&cmds[i], wim->hdr.flags);
716 if (cmds[i].extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
717 WIMLIB_EXTRACT_FLAG_HARDLINK)) {
718 found_link_cmd = true;
720 found_nolink_cmd = true;
722 if (found_link_cmd && found_nolink_cmd) {
723 ERROR("Symlink or hardlink extraction mode must "
724 "be set on all extraction commands");
725 return WIMLIB_ERR_INVALID_PARAM;
729 /* Execute the extraction commands */
730 for (size_t i = 0; i < num_cmds; i++) {
731 ret = extract_tree(wim,
732 cmds[i].wim_source_path,
733 cmds[i].fs_dest_path,
734 cmds[i].extract_flags,
742 /* Extract files or directories from a WIM image. */
744 wimlib_extract_files(WIMStruct *wim,
746 const struct wimlib_extract_command *cmds,
748 int default_extract_flags,
749 WIMStruct **additional_swms,
750 unsigned num_additional_swms,
751 wimlib_progress_func_t progress_func)
754 struct wimlib_extract_command *cmds_copy;
757 default_extract_flags &= WIMLIB_EXTRACT_MASK_PUBLIC;
759 ret = verify_swm_set(wim, additional_swms, num_additional_swms);
766 if (num_additional_swms)
767 merge_lookup_tables(wim, additional_swms, num_additional_swms);
769 cmds_copy = CALLOC(num_cmds, sizeof(cmds[0]));
771 ret = WIMLIB_ERR_NOMEM;
772 goto out_restore_lookup_table;
775 for (size_t i = 0; i < num_cmds; i++) {
776 cmds_copy[i].extract_flags = (default_extract_flags |
777 cmds[i].extract_flags)
778 & WIMLIB_EXTRACT_MASK_PUBLIC;
779 all_flags |= cmds_copy[i].extract_flags;
781 cmds_copy[i].wim_source_path = canonicalize_wim_path(cmds[i].wim_source_path);
782 if (!cmds_copy[i].wim_source_path) {
783 ret = WIMLIB_ERR_NOMEM;
784 goto out_free_cmds_copy;
787 cmds_copy[i].fs_dest_path = canonicalize_fs_path(cmds[i].fs_dest_path);
788 if (!cmds_copy[i].fs_dest_path) {
789 ret = WIMLIB_ERR_NOMEM;
790 goto out_free_cmds_copy;
794 ret = do_wimlib_extract_files(wim, image,
798 if (all_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
799 WIMLIB_EXTRACT_FLAG_HARDLINK))
801 for_lookup_table_entry(wim->lookup_table,
802 lte_free_extracted_file, NULL);
805 for (size_t i = 0; i < num_cmds; i++) {
806 FREE(cmds_copy[i].wim_source_path);
807 FREE(cmds_copy[i].fs_dest_path);
810 out_restore_lookup_table:
811 if (num_additional_swms)
812 unmerge_lookup_table(wim);
818 * Extracts an image from a WIM file.
820 * @wim: WIMStruct for the WIM file.
822 * @image: Number of the single image to extract.
824 * @target: Directory or NTFS volume to extract the image to.
826 * @extract_flags: Bitwise or of WIMLIB_EXTRACT_FLAG_*.
828 * @progress_func: If non-NULL, a progress function to be called
831 * Returns 0 on success; nonzero on failure.
834 extract_single_image(WIMStruct *wim, int image,
835 const tchar *target, int extract_flags,
836 wimlib_progress_func_t progress_func)
839 tchar *target_copy = canonicalize_fs_path(target);
841 return WIMLIB_ERR_NOMEM;
842 struct wimlib_extract_command cmd = {
843 .wim_source_path = T(""),
844 .fs_dest_path = target_copy,
845 .extract_flags = extract_flags,
847 ret = do_wimlib_extract_files(wim, image, &cmd, 1, progress_func);
852 static const tchar * const filename_forbidden_chars =
861 /* This function checks if it is okay to use a WIM image's name as a directory
864 image_name_ok_as_dir(const tchar *image_name)
866 return image_name && *image_name &&
867 !tstrpbrk(image_name, filename_forbidden_chars) &&
868 tstrcmp(image_name, T(".")) &&
869 tstrcmp(image_name, T(".."));
872 /* Extracts all images from the WIM to the directory @target, with the images
873 * placed in subdirectories named by their image names. */
875 extract_all_images(WIMStruct *wim,
878 wimlib_progress_func_t progress_func)
880 size_t image_name_max_len = max(xml_get_max_image_name_len(wim), 20);
881 size_t output_path_len = tstrlen(target);
882 tchar buf[output_path_len + 1 + image_name_max_len + 1];
885 const tchar *image_name;
888 if (tstat(target, &stbuf)) {
891 if (tmkdir(target, S_IRWXU | S_IRGRP | S_IXGRP |
894 ERROR_WITH_ERRNO("Failed to create directory \"%"TS"\"", target);
895 return WIMLIB_ERR_MKDIR;
898 ERROR_WITH_ERRNO("Failed to stat \"%"TS"\"", target);
899 return WIMLIB_ERR_STAT;
901 } else if (!S_ISDIR(stbuf.st_mode)) {
902 ERROR("\"%"TS"\" is not a directory", target);
903 return WIMLIB_ERR_NOTDIR;
906 tmemcpy(buf, target, output_path_len);
907 buf[output_path_len] = T('/');
908 for (image = 1; image <= wim->hdr.image_count; image++) {
909 image_name = wimlib_get_image_name(wim, image);
910 if (image_name_ok_as_dir(image_name)) {
911 tstrcpy(buf + output_path_len + 1, image_name);
913 /* Image name is empty or contains forbidden characters.
914 * Use image number instead. */
915 tsprintf(buf + output_path_len + 1, T("%d"), image);
917 ret = extract_single_image(wim, image, buf, extract_flags,
925 /* Extracts a single image or all images from a WIM file to a directory or NTFS
928 wimlib_extract_image(WIMStruct *wim,
932 WIMStruct **additional_swms,
933 unsigned num_additional_swms,
934 wimlib_progress_func_t progress_func)
938 extract_flags &= WIMLIB_EXTRACT_MASK_PUBLIC;
940 ret = verify_swm_set(wim, additional_swms, num_additional_swms);
944 if (num_additional_swms)
945 merge_lookup_tables(wim, additional_swms, num_additional_swms);
947 if (image == WIMLIB_ALL_IMAGES) {
948 ret = extract_all_images(wim, target,
949 extract_flags | WIMLIB_EXTRACT_FLAG_MULTI_IMAGE,
952 ret = extract_single_image(wim, image, target, extract_flags,
956 if (extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
957 WIMLIB_EXTRACT_FLAG_HARDLINK))
959 for_lookup_table_entry(wim->lookup_table,
960 lte_free_extracted_file,
963 if (num_additional_swms)
964 unmerge_lookup_table(wim);