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/.
36 # include "timestamp.h"
37 # include <sys/time.h>
48 #include "lookup_table.h"
49 #include "wimlib_internal.h"
53 # include <ntfs-3g/volume.h>
62 extract_regular_file_linked(struct wim_dentry *dentry,
63 const mbchar *output_path,
64 struct apply_args *args,
65 struct wim_lookup_table_entry *lte)
67 /* This mode overrides the normal hard-link extraction and
68 * instead either symlinks or hardlinks *all* identical files in
69 * the WIM, even if they are in a different image (in the case
70 * of a multi-image extraction) */
72 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_HARDLINK) {
73 if (link(lte->extracted_file, output_path) != 0) {
74 ERROR_WITH_ERRNO("Failed to hard link "
76 output_path, lte->extracted_file);
77 return WIMLIB_ERR_LINK;
80 int num_path_components;
81 int num_output_dir_path_components;
82 size_t extracted_file_len;
88 get_num_path_components(dentry->full_path) - 1;
89 num_output_dir_path_components =
90 get_num_path_components(args->target);
92 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE) {
93 num_path_components++;
94 num_output_dir_path_components--;
96 extracted_file_len = strlen(lte->extracted_file);
98 mbchar buf[extracted_file_len + 3 * num_path_components + 1];
101 for (i = 0; i < num_path_components; i++) {
106 p2 = lte->extracted_file;
109 while (num_output_dir_path_components--)
110 p2 = path_next_part(p2, NULL);
112 if (symlink(buf, output_path) != 0) {
113 ERROR_WITH_ERRNO("Failed to symlink `%s' to `%s'",
114 buf, lte->extracted_file);
115 return WIMLIB_ERR_LINK;
122 symlink_apply_unix_data(const mbchar *link,
123 const struct wimlib_unix_data *unix_data)
125 if (lchown(link, unix_data->uid, unix_data->gid)) {
126 if (errno == EPERM) {
128 WARNING_WITH_ERRNO("failed to set symlink UNIX owner/group");
130 ERROR_WITH_ERRNO("failed to set symlink UNIX owner/group");
131 return WIMLIB_ERR_INVALID_DENTRY;
138 fd_apply_unix_data(int fd, const struct wimlib_unix_data *unix_data)
140 if (fchown(fd, unix_data->uid, unix_data->gid)) {
141 if (errno == EPERM) {
142 WARNING_WITH_ERRNO("failed to set file UNIX owner/group");
145 ERROR_WITH_ERRNO("failed to set file UNIX owner/group");
146 return WIMLIB_ERR_INVALID_DENTRY;
150 if (fchmod(fd, unix_data->mode)) {
151 if (errno == EPERM) {
152 WARNING_WITH_ERRNO("failed to set UNIX file mode");
155 ERROR_WITH_ERRNO("failed to set UNIX file mode");
156 return WIMLIB_ERR_INVALID_DENTRY;
163 dir_apply_unix_data(const mbchar *dir, const struct wimlib_unix_data *unix_data)
165 int dfd = open(dir, O_RDONLY);
168 ret = fd_apply_unix_data(dfd, unix_data);
170 ERROR_WITH_ERRNO("can't close directory `%s'", dir);
171 ret = WIMLIB_ERR_MKDIR;
174 ERROR_WITH_ERRNO("can't open directory `%s'", dir);
175 ret = WIMLIB_ERR_MKDIR;
181 extract_regular_file_unlinked(struct wim_dentry *dentry,
182 struct apply_args *args,
183 const mbchar *output_path,
184 struct wim_lookup_table_entry *lte)
186 /* Normal mode of extraction. Regular files and hard links are
187 * extracted in the way that they appear in the WIM. */
191 struct wim_inode *inode = dentry->d_inode;
193 if (!((args->extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE)
194 && (args->extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
195 WIMLIB_EXTRACT_FLAG_HARDLINK))))
197 /* If the dentry is part of a hard link set of at least 2
198 * dentries and one of the other dentries has already been
199 * extracted, make a hard link to the file corresponding to this
200 * already-extracted directory. Otherwise, extract the file and
201 * set the inode->i_extracted_file field so that other dentries
202 * in the hard link group can link to it. */
203 if (inode->i_nlink > 1) {
204 if (inode->i_extracted_file) {
205 DEBUG("Extracting hard link `%s' => `%s'",
206 output_path, inode->i_extracted_file);
207 if (link(inode->i_extracted_file, output_path) != 0) {
208 ERROR_WITH_ERRNO("Failed to hard link "
211 inode->i_extracted_file);
212 return WIMLIB_ERR_LINK;
216 FREE(inode->i_extracted_file);
217 inode->i_extracted_file = STRDUP(output_path);
218 if (!inode->i_extracted_file) {
219 ERROR("Failed to allocate memory for filename");
220 return WIMLIB_ERR_NOMEM;
225 /* Extract the contents of the file to @output_path. */
227 out_fd = open(output_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
229 ERROR_WITH_ERRNO("Failed to open the file `%s' for writing",
231 return WIMLIB_ERR_OPEN;
235 /* Empty file with no lookup table entry */
236 DEBUG("Empty file `%s'.", output_path);
238 goto out_extract_unix_data;
241 ret = extract_wim_resource_to_fd(lte, out_fd, wim_resource_size(lte));
243 ERROR("Failed to extract resource to `%s'", output_path);
247 out_extract_unix_data:
248 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
249 struct wimlib_unix_data unix_data;
250 ret = inode_get_unix_data(inode, &unix_data, NULL);
256 ret = fd_apply_unix_data(out_fd, &unix_data);
261 args->progress.extract.completed_bytes += wim_resource_size(lte);
263 if (close(out_fd) != 0) {
264 ERROR_WITH_ERRNO("Failed to close file `%s'", output_path);
266 ret = WIMLIB_ERR_WRITE;
272 extract_regular_file(struct wim_dentry *dentry,
273 struct apply_args *args,
274 const mbchar *output_path)
276 struct wim_lookup_table_entry *lte;
277 const struct wim_inode *inode = dentry->d_inode;
279 lte = inode_unnamed_lte_resolved(inode);
281 if (lte && (args->extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
282 WIMLIB_EXTRACT_FLAG_HARDLINK)))
284 if (lte->extracted_file) {
285 return extract_regular_file_linked(dentry, output_path, args, lte);
287 lte->extracted_file = STRDUP(output_path);
288 if (!lte->extracted_file)
289 return WIMLIB_ERR_NOMEM;
292 return extract_regular_file_unlinked(dentry, args, output_path, lte);
296 extract_symlink(struct wim_dentry *dentry,
297 struct apply_args *args,
298 const mbchar *output_path)
301 ssize_t ret = inode_readlink(dentry->d_inode, target,
302 sizeof(target), args->w, 0);
303 struct wim_lookup_table_entry *lte;
306 ERROR("Could not read the symbolic link from dentry `%s'",
308 return WIMLIB_ERR_INVALID_DENTRY;
310 ret = symlink(target, output_path);
312 ERROR_WITH_ERRNO("Failed to symlink `%s' to `%s'",
313 output_path, target);
314 return WIMLIB_ERR_LINK;
316 lte = inode_unnamed_lte_resolved(dentry->d_inode);
317 wimlib_assert(lte != NULL);
318 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
319 struct wimlib_unix_data unix_data;
320 ret = inode_get_unix_data(dentry->d_inode, &unix_data, NULL);
326 ret = symlink_apply_unix_data(output_path, &unix_data);
330 args->progress.extract.completed_bytes += wim_resource_size(lte);
334 #endif /* !__WIN32__ */
337 extract_directory(struct wim_dentry *dentry,
338 const mbchar *output_path, bool is_root)
343 ret = stat(output_path, &stbuf);
345 if (S_ISDIR(stbuf.st_mode)) {
347 /*WARNING("`%s' already exists", output_path);*/
350 ERROR("`%s' is not a directory", output_path);
351 return WIMLIB_ERR_MKDIR;
354 if (errno != ENOENT) {
355 ERROR_WITH_ERRNO("Failed to stat `%s'", output_path);
356 return WIMLIB_ERR_STAT;
360 if (mkdir(output_path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH))
362 ERROR_WITH_ERRNO("Cannot create directory `%s'", output_path);
363 return WIMLIB_ERR_MKDIR;
369 struct wimlib_unix_data unix_data;
370 ret = inode_get_unix_data(dentry->d_inode, &unix_data, NULL);
376 ret = dir_apply_unix_data(output_path, &unix_data);
383 static int unix_do_apply_dentry(const mbchar *output_path,
384 size_t output_path_len,
385 struct wim_dentry *dentry,
386 struct apply_args *args)
388 const struct wim_inode *inode = dentry->d_inode;
390 if (inode_is_symlink(inode))
391 return extract_symlink(dentry, args, output_path);
392 else if (inode_is_directory(inode))
393 return extract_directory((args->extract_flags &
394 WIMLIB_EXTRACT_FLAG_UNIX_DATA) ? dentry : NULL,
397 return extract_regular_file(dentry, args, output_path);
401 unix_do_apply_dentry_timestamps(const mbchar *output_path,
402 size_t output_path_len,
403 const struct wim_dentry *dentry,
404 struct apply_args *args)
407 const struct wim_inode *inode = dentry->d_inode;
409 #ifdef HAVE_UTIMENSAT
410 /* Convert the WIM timestamps, which are accurate to 100 nanoseconds,
411 * into `struct timespec's for passing to utimensat(), which is accurate
412 * to 1 nanosecond. */
414 struct timespec ts[2];
415 ts[0] = wim_timestamp_to_timespec(inode->i_last_access_time);
416 ts[1] = wim_timestamp_to_timespec(inode->i_last_write_time);
417 ret = utimensat(AT_FDCWD, output_path, ts, AT_SYMLINK_NOFOLLOW);
425 /* utimensat() not implemented or not available */
427 /* Convert the WIM timestamps, which are accurate to 100
428 * nanoseconds, into `struct timeval's for passing to lutimes(),
429 * which is accurate to 1 microsecond. */
430 struct timeval tv[2];
431 tv[0] = wim_timestamp_to_timeval(inode->i_last_access_time);
432 tv[1] = wim_timestamp_to_timeval(inode->i_last_write_time);
433 ret = lutimes(output_path, tv);
440 /* utimensat() and lutimes() both not implemented or not
443 /* Convert the WIM timestamps, which are accurate to 100
444 * nanoseconds, into a `struct utimbuf's for passing to
445 * utime(), which is accurate to 1 second. */
447 buf.actime = wim_timestamp_to_unix(inode->i_last_access_time);
448 buf.modtime = wim_timestamp_to_unix(inode->i_last_write_time);
449 ret = utime(output_path, &buf);
452 if (ret && args->num_utime_warnings < 10) {
453 WARNING_WITH_ERRNO("Failed to set timestamp on file `%s'",
455 args->num_utime_warnings++;
459 #endif /* !__WIN32__ */
461 /* Extracts a file, directory, or symbolic link from the WIM archive. */
463 apply_dentry_normal(struct wim_dentry *dentry, void *arg)
465 struct apply_args *args = arg;
469 len = strlen(args->target);
470 if (dentry_is_root(dentry)) {
471 output_path = (mbchar*)args->target;
473 output_path = alloca(len + dentry->full_path_nbytes + 1);
474 memcpy(output_path, args->target, len);
475 memcpy(output_path + len, dentry->full_path, dentry->full_path_nbytes);
476 output_path[len + dentry->full_path_nbytes] = '\0';
477 len += dentry->full_path_nbytes;
480 return win32_do_apply_dentry(output_path, len, dentry, args);
482 return unix_do_apply_dentry(output_path, len, dentry, args);
487 /* Apply timestamps to an extracted file or directory */
489 apply_dentry_timestamps_normal(struct wim_dentry *dentry, void *arg)
491 struct apply_args *args = arg;
495 len = strlen(args->target);
496 if (dentry_is_root(dentry)) {
497 output_path = (mbchar*)args->target;
499 output_path = alloca(len + dentry->full_path_nbytes + 1);
500 memcpy(output_path, args->target, len);
501 memcpy(output_path + len, dentry->full_path, dentry->full_path_nbytes);
502 output_path[len + dentry->full_path_nbytes] = '\0';
503 len += dentry->full_path_nbytes;
507 return win32_do_apply_dentry_timestamps(output_path, len, dentry, args);
509 return unix_do_apply_dentry_timestamps(output_path, len, dentry, args);
513 /* Extract a dentry if it hasn't already been extracted, and either the dentry
514 * has no streams or WIMLIB_EXTRACT_FLAG_NO_STREAMS is not specified. */
516 maybe_apply_dentry(struct wim_dentry *dentry, void *arg)
518 struct apply_args *args = arg;
521 if (dentry->is_extracted)
524 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_NO_STREAMS)
525 if (inode_unnamed_lte_resolved(dentry->d_inode))
528 if ((args->extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE) &&
529 args->progress_func) {
530 args->progress.extract.cur_path = dentry->full_path;
531 args->progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY,
534 ret = args->apply_dentry(dentry, args);
536 dentry->is_extracted = 1;
541 cmp_streams_by_wim_position(const void *p1, const void *p2)
543 const struct wim_lookup_table_entry *lte1, *lte2;
544 lte1 = *(const struct wim_lookup_table_entry**)p1;
545 lte2 = *(const struct wim_lookup_table_entry**)p2;
546 if (lte1->resource_entry.offset < lte2->resource_entry.offset)
548 else if (lte1->resource_entry.offset > lte2->resource_entry.offset)
555 sort_stream_list_by_wim_position(struct list_head *stream_list)
557 struct list_head *cur;
559 struct wim_lookup_table_entry **array;
564 list_for_each(cur, stream_list)
566 array_size = num_streams * sizeof(array[0]);
567 array = MALLOC(array_size);
569 ERROR("Failed to allocate %zu bytes to sort stream entries",
571 return WIMLIB_ERR_NOMEM;
573 cur = stream_list->next;
574 for (i = 0; i < num_streams; i++) {
575 array[i] = container_of(cur, struct wim_lookup_table_entry, staging_list);
579 qsort(array, num_streams, sizeof(array[0]), cmp_streams_by_wim_position);
581 INIT_LIST_HEAD(stream_list);
582 for (i = 0; i < num_streams; i++)
583 list_add_tail(&array[i]->staging_list, stream_list);
589 calculate_bytes_to_extract(struct list_head *stream_list,
591 union wimlib_progress_info *progress)
593 struct wim_lookup_table_entry *lte;
597 /* For each stream to be extracted... */
598 list_for_each_entry(lte, stream_list, staging_list) {
600 (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
602 /* In the symlink or hard link extraction mode, each
603 * stream will be extracted one time regardless of how
604 * many dentries share the stream. */
605 wimlib_assert(!(extract_flags & WIMLIB_EXTRACT_FLAG_NTFS));
606 if (!lte->extracted_file) {
608 total_bytes += wim_resource_size(lte);
611 num_streams += lte->out_refcnt;
612 total_bytes += lte->out_refcnt * wim_resource_size(lte);
615 progress->extract.num_streams = num_streams;
616 progress->extract.total_bytes = total_bytes;
617 progress->extract.completed_bytes = 0;
621 maybe_add_stream_for_extraction(struct wim_lookup_table_entry *lte,
622 struct list_head *stream_list)
624 if (++lte->out_refcnt == 1) {
625 INIT_LIST_HEAD(<e->inode_list);
626 list_add_tail(<e->staging_list, stream_list);
631 inode_find_streams_for_extraction(struct wim_inode *inode,
632 struct list_head *stream_list,
635 struct wim_lookup_table_entry *lte;
636 bool inode_added = false;
638 lte = inode_unnamed_lte_resolved(inode);
640 maybe_add_stream_for_extraction(lte, stream_list);
641 list_add_tail(&inode->i_lte_inode_list, <e->inode_list);
645 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
646 for (unsigned i = 0; i < inode->i_num_ads; i++) {
647 if (inode->i_ads_entries[i].stream_name_nbytes != 0) {
648 lte = inode->i_ads_entries[i].lte;
650 maybe_add_stream_for_extraction(lte,
653 list_add_tail(&inode->i_lte_inode_list,
665 find_streams_for_extraction(struct hlist_head *inode_list,
666 struct list_head *stream_list,
667 struct wim_lookup_table *lookup_table,
670 struct wim_inode *inode;
671 struct hlist_node *cur;
672 struct wim_dentry *dentry;
674 for_lookup_table_entry(lookup_table, lte_zero_out_refcnt, NULL);
675 INIT_LIST_HEAD(stream_list);
676 hlist_for_each_entry(inode, cur, inode_list, i_hlist) {
677 if (!inode->i_resolved)
678 inode_resolve_ltes(inode, lookup_table);
679 inode_for_each_dentry(dentry, inode)
680 dentry->is_extracted = 0;
681 inode_find_streams_for_extraction(inode, stream_list,
686 struct apply_operations {
687 int (*apply_dentry)(struct wim_dentry *dentry, void *arg);
688 int (*apply_dentry_timestamps)(struct wim_dentry *dentry, void *arg);
691 static const struct apply_operations normal_apply_operations = {
692 .apply_dentry = apply_dentry_normal,
693 .apply_dentry_timestamps = apply_dentry_timestamps_normal,
697 static const struct apply_operations ntfs_apply_operations = {
698 .apply_dentry = apply_dentry_ntfs,
699 .apply_dentry_timestamps = apply_dentry_timestamps_ntfs,
704 apply_stream_list(struct list_head *stream_list,
705 struct apply_args *args,
706 const struct apply_operations *ops,
707 wimlib_progress_func_t progress_func)
709 uint64_t bytes_per_progress = args->progress.extract.total_bytes / 100;
710 uint64_t next_progress = bytes_per_progress;
711 struct wim_lookup_table_entry *lte;
712 struct wim_inode *inode;
713 struct wim_dentry *dentry;
716 /* This complicated loop is essentially looping through the dentries,
717 * although dentries may be visited more than once (if a dentry contains
718 * two different nonempty streams) or not at all (if a dentry contains
719 * no non-empty streams).
721 * The outer loop is over the distinct streams to be extracted so that
722 * sequential reading of the WIM can be implemented. */
724 /* For each distinct stream to be extracted */
725 list_for_each_entry(lte, stream_list, staging_list) {
726 /* For each inode that contains the stream */
727 list_for_each_entry(inode, <e->inode_list, i_lte_inode_list) {
728 /* For each dentry that points to the inode */
729 inode_for_each_dentry(dentry, inode) {
730 /* Extract the dentry if it was not already
732 ret = maybe_apply_dentry(dentry, args);
736 args->progress.extract.completed_bytes >= next_progress)
738 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS,
740 if (args->progress.extract.completed_bytes >=
741 args->progress.extract.total_bytes)
743 next_progress = ~0ULL;
746 min (args->progress.extract.completed_bytes +
748 args->progress.extract.total_bytes);
757 /* Extracts the image @image from the WIM @w to the directory or NTFS volume
760 extract_single_image(WIMStruct *w, int image,
761 const mbchar *target, int extract_flags,
762 wimlib_progress_func_t progress_func)
765 struct list_head stream_list;
766 struct hlist_head *inode_list;
768 struct apply_args args;
769 const struct apply_operations *ops;
772 args.target = target;
773 args.extract_flags = extract_flags;
774 args.num_utime_warnings = 0;
775 args.stream_list = &stream_list;
776 args.progress_func = progress_func;
779 args.progress.extract.wimfile_name = w->filename;
780 args.progress.extract.image = image;
781 args.progress.extract.extract_flags = (extract_flags &
782 WIMLIB_EXTRACT_MASK_PUBLIC);
783 args.progress.extract.image_name = wimlib_get_image_name(w, image);
784 args.progress.extract.target = target;
788 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
789 args.vol = ntfs_mount(target, 0);
791 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%s'", target);
792 return WIMLIB_ERR_NTFS_3G;
794 ops = &ntfs_apply_operations;
797 ops = &normal_apply_operations;
799 ret = select_wim_image(w, image);
803 inode_list = &w->image_metadata[image - 1].inode_list;
805 /* Build a list of the streams that need to be extracted */
806 find_streams_for_extraction(inode_list, &stream_list,
807 w->lookup_table, extract_flags);
809 /* Calculate the number of bytes of data that will be extracted */
810 calculate_bytes_to_extract(&stream_list, extract_flags,
814 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN,
818 /* If a sequential extraction was specified, sort the streams to be
819 * extracted by their position in the WIM file, so that the WIM file can
820 * be read sequentially. */
821 if (extract_flags & WIMLIB_EXTRACT_FLAG_SEQUENTIAL) {
822 ret = sort_stream_list_by_wim_position(&stream_list);
824 WARNING("Falling back to non-sequential extraction");
825 extract_flags &= ~WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
830 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN,
834 /* Make the directory structure and extract empty files */
835 args.extract_flags |= WIMLIB_EXTRACT_FLAG_NO_STREAMS;
836 args.apply_dentry = ops->apply_dentry;
837 ret = for_dentry_in_tree(wim_root_dentry(w), maybe_apply_dentry, &args);
838 args.extract_flags &= ~WIMLIB_EXTRACT_FLAG_NO_STREAMS;
843 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_END,
847 /* Extract non-empty files */
848 ret = apply_stream_list(&stream_list, &args, ops, progress_func);
853 progress_func(WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS,
857 /* Apply timestamps */
858 ret = for_dentry_in_tree_depth(wim_root_dentry(w),
859 ops->apply_dentry_timestamps, &args);
864 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END,
869 /* Unmount the NTFS volume */
870 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
871 if (ntfs_umount(args.vol, FALSE) != 0) {
872 ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%s'", args.target);
874 ret = WIMLIB_ERR_NTFS_3G;
882 /* Extracts all images from the WIM to the directory @target, with the images
883 * placed in subdirectories named by their image names. */
885 extract_all_images(WIMStruct *w, const mbchar *target,
887 wimlib_progress_func_t progress_func)
889 size_t image_name_max_len = max(xml_get_max_image_name_len(w), 20);
890 size_t output_path_len = strlen(target);
891 mbchar buf[output_path_len + 1 + image_name_max_len + 1];
894 const utf8char *image_name;
896 ret = extract_directory(NULL, target, true);
900 memcpy(buf, target, output_path_len);
901 buf[output_path_len] = '/';
902 for (image = 1; image <= w->hdr.image_count; image++) {
903 image_name = wimlib_get_image_name(w, image);
904 if (image_name && *image_name &&
905 (wimlib_mbs_is_utf8 || !utf8_str_contains_nonascii_chars(image_name))
906 && strchr(image_name, '/') == NULL)
908 strcpy(buf + output_path_len + 1, image_name);
910 /* Image name is empty, or may not be representable in
911 * the current locale, or contains path separators. Use
912 * the image number instead. */
913 sprintf(buf + output_path_len + 1, "%d", image);
915 ret = extract_single_image(w, image, buf, extract_flags,
923 /* Extracts a single image or all images from a WIM file to a directory or NTFS
926 wimlib_extract_image(WIMStruct *w,
928 const mbchar *target,
930 WIMStruct **additional_swms,
931 unsigned num_additional_swms,
932 wimlib_progress_func_t progress_func)
934 struct wim_lookup_table *joined_tab, *w_tab_save;
938 return WIMLIB_ERR_INVALID_PARAM;
940 extract_flags &= WIMLIB_EXTRACT_MASK_PUBLIC;
942 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
943 == (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
944 return WIMLIB_ERR_INVALID_PARAM;
947 if (extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
948 ERROR("Extracting UNIX data is not supported on Windows");
949 return WIMLIB_ERR_INVALID_PARAM;
951 if (extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK)) {
952 ERROR("Linked extraction modes are not supported on Windows");
953 return WIMLIB_ERR_INVALID_PARAM;
957 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
959 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))) {
960 ERROR("Cannot specify symlink or hardlink flags when applying\n"
961 " directly to a NTFS volume");
962 return WIMLIB_ERR_INVALID_PARAM;
964 if (image == WIMLIB_ALL_IMAGES) {
965 ERROR("Can only apply a single image when applying "
966 "directly to a NTFS volume");
967 return WIMLIB_ERR_INVALID_PARAM;
969 if (extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
970 ERROR("Cannot restore UNIX-specific data in the NTFS extraction mode");
971 return WIMLIB_ERR_INVALID_PARAM;
974 ERROR("wimlib was compiled without support for NTFS-3g, so");
975 ERROR("we cannot apply a WIM image directly to a NTFS volume");
976 return WIMLIB_ERR_UNSUPPORTED;
980 ret = verify_swm_set(w, additional_swms, num_additional_swms);
984 if (num_additional_swms) {
985 ret = new_joined_lookup_table(w, additional_swms,
986 num_additional_swms, &joined_tab);
989 w_tab_save = w->lookup_table;
990 w->lookup_table = joined_tab;
993 if (image == WIMLIB_ALL_IMAGES) {
994 extract_flags |= WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
995 ret = extract_all_images(w, target, extract_flags,
998 extract_flags &= ~WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
999 ret = extract_single_image(w, image, target, extract_flags,
1003 if (extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
1004 WIMLIB_EXTRACT_FLAG_HARDLINK))
1006 for_lookup_table_entry(w->lookup_table,
1007 lte_free_extracted_file,
1011 if (num_additional_swms) {
1012 free_lookup_table(w->lookup_table);
1013 w->lookup_table = w_tab_save;