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>
49 #include "lookup_table.h"
50 #include "wimlib_internal.h"
54 # include <ntfs-3g/volume.h>
62 static int extract_regular_file_linked(struct wim_dentry *dentry,
63 const char *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_utf8) - 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 char 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 "
115 buf, lte->extracted_file);
116 return WIMLIB_ERR_LINK;
122 static int symlink_apply_unix_data(const char *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;
137 static int fd_apply_unix_data(int fd, const struct wimlib_unix_data *unix_data)
139 if (fchown(fd, unix_data->uid, unix_data->gid)) {
140 if (errno == EPERM) {
141 WARNING_WITH_ERRNO("failed to set file UNIX owner/group");
144 ERROR_WITH_ERRNO("failed to set file UNIX owner/group");
145 return WIMLIB_ERR_INVALID_DENTRY;
149 if (fchmod(fd, unix_data->mode)) {
150 if (errno == EPERM) {
151 WARNING_WITH_ERRNO("failed to set UNIX file mode");
154 ERROR_WITH_ERRNO("failed to set UNIX file mode");
155 return WIMLIB_ERR_INVALID_DENTRY;
161 static int dir_apply_unix_data(const char *dir,
162 const struct wimlib_unix_data *unix_data)
164 int dfd = open(dir, O_RDONLY);
167 ret = fd_apply_unix_data(dfd, unix_data);
169 ERROR_WITH_ERRNO("can't close directory `%s'", dir);
170 ret = WIMLIB_ERR_MKDIR;
173 ERROR_WITH_ERRNO("can't open directory `%s'", dir);
174 ret = WIMLIB_ERR_MKDIR;
179 static int extract_regular_file_unlinked(struct wim_dentry *dentry,
180 struct apply_args *args,
181 const char *output_path,
182 struct wim_lookup_table_entry *lte)
184 /* Normal mode of extraction. Regular files and hard links are
185 * extracted in the way that they appear in the WIM. */
189 struct wim_inode *inode = dentry->d_inode;
191 if (!((args->extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE)
192 && (args->extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
193 WIMLIB_EXTRACT_FLAG_HARDLINK))))
195 /* If the dentry is part of a hard link set of at least 2
196 * dentries and one of the other dentries has already been
197 * extracted, make a hard link to the file corresponding to this
198 * already-extracted directory. Otherwise, extract the file and
199 * set the inode->i_extracted_file field so that other dentries
200 * in the hard link group can link to it. */
201 if (inode->i_nlink > 1) {
202 if (inode->i_extracted_file) {
203 DEBUG("Extracting hard link `%s' => `%s'",
204 output_path, inode->i_extracted_file);
205 if (link(inode->i_extracted_file, output_path) != 0) {
206 ERROR_WITH_ERRNO("Failed to hard link "
209 inode->i_extracted_file);
210 return WIMLIB_ERR_LINK;
214 FREE(inode->i_extracted_file);
215 inode->i_extracted_file = STRDUP(output_path);
216 if (!inode->i_extracted_file) {
217 ERROR("Failed to allocate memory for filename");
218 return WIMLIB_ERR_NOMEM;
223 /* Extract the contents of the file to @output_path. */
225 out_fd = open(output_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
227 ERROR_WITH_ERRNO("Failed to open the file `%s' for writing",
229 return WIMLIB_ERR_OPEN;
233 /* Empty file with no lookup table entry */
234 DEBUG("Empty file `%s'.", output_path);
236 goto out_extract_unix_data;
239 ret = extract_wim_resource_to_fd(lte, out_fd, wim_resource_size(lte));
241 ERROR("Failed to extract resource to `%s'", output_path);
245 out_extract_unix_data:
246 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
247 struct wimlib_unix_data unix_data;
248 ret = inode_get_unix_data(inode, &unix_data, NULL);
254 ret = fd_apply_unix_data(out_fd, &unix_data);
259 args->progress.extract.completed_bytes += wim_resource_size(lte);
261 if (close(out_fd) != 0) {
262 ERROR_WITH_ERRNO("Failed to close file `%s'", output_path);
264 ret = WIMLIB_ERR_WRITE;
269 static int extract_regular_file(struct wim_dentry *dentry,
270 struct apply_args *args,
271 const char *output_path)
273 struct wim_lookup_table_entry *lte;
274 const struct wim_inode *inode = dentry->d_inode;
276 lte = inode_unnamed_lte_resolved(inode);
278 if (lte && (args->extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
279 WIMLIB_EXTRACT_FLAG_HARDLINK)))
281 if (lte->extracted_file) {
282 return extract_regular_file_linked(dentry, output_path, args, lte);
284 lte->extracted_file = STRDUP(output_path);
285 if (!lte->extracted_file)
286 return WIMLIB_ERR_NOMEM;
289 return extract_regular_file_unlinked(dentry, args, output_path, lte);
292 static int extract_symlink(struct wim_dentry *dentry,
293 struct apply_args *args,
294 const char *output_path)
297 ssize_t ret = inode_readlink(dentry->d_inode, target,
298 sizeof(target), args->w, 0);
299 struct wim_lookup_table_entry *lte;
302 ERROR("Could not read the symbolic link from dentry `%s'",
303 dentry->full_path_utf8);
304 return WIMLIB_ERR_INVALID_DENTRY;
306 ret = symlink(target, output_path);
308 ERROR_WITH_ERRNO("Failed to symlink `%s' to `%s'",
309 output_path, target);
310 return WIMLIB_ERR_LINK;
312 lte = inode_unnamed_lte_resolved(dentry->d_inode);
313 wimlib_assert(lte != NULL);
314 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
315 struct wimlib_unix_data unix_data;
316 ret = inode_get_unix_data(dentry->d_inode, &unix_data, NULL);
322 ret = symlink_apply_unix_data(output_path, &unix_data);
326 args->progress.extract.completed_bytes += wim_resource_size(lte);
330 #endif /* !__WIN32__ */
332 static int extract_directory(struct wim_dentry *dentry,
333 const char *output_path, bool is_root)
338 ret = stat(output_path, &stbuf);
340 if (S_ISDIR(stbuf.st_mode)) {
342 /*WARNING("`%s' already exists", output_path);*/
345 ERROR("`%s' is not a directory", output_path);
346 return WIMLIB_ERR_MKDIR;
349 if (errno != ENOENT) {
350 ERROR_WITH_ERRNO("Failed to stat `%s'", output_path);
351 return WIMLIB_ERR_STAT;
354 if (mkdir(output_path, S_IRWXU | S_IRGRP | S_IXGRP |
355 S_IROTH | S_IXOTH) != 0) {
356 ERROR_WITH_ERRNO("Cannot create directory `%s'",
358 return WIMLIB_ERR_MKDIR;
364 struct wimlib_unix_data unix_data;
365 ret = inode_get_unix_data(dentry->d_inode, &unix_data, NULL);
371 ret = dir_apply_unix_data(output_path, &unix_data);
378 static int unix_do_apply_dentry(const char *output_path,
379 size_t output_path_len,
380 struct wim_dentry *dentry,
381 struct apply_args *args)
383 const struct wim_inode *inode = dentry->d_inode;
385 if (inode_is_symlink(inode))
386 return extract_symlink(dentry, args, output_path);
387 else if (inode_is_directory(inode))
388 return extract_directory((args->extract_flags &
389 WIMLIB_EXTRACT_FLAG_UNIX_DATA) ? dentry : NULL,
392 return extract_regular_file(dentry, args, output_path);
395 static int unix_do_apply_dentry_timestamps(const char *output_path,
396 size_t output_path_len,
397 const struct wim_dentry *dentry,
398 struct apply_args *args)
401 const struct wim_inode *inode = dentry->d_inode;
403 /* Convert the WIM timestamps, which are accurate to 100 nanoseconds,
404 * into struct timeval's. */
405 struct timeval tv[2];
406 wim_timestamp_to_timeval(inode->i_last_access_time, &tv[0]);
407 wim_timestamp_to_timeval(inode->i_last_write_time, &tv[1]);
409 ret = lutimes(output_path, tv);
416 if (errno == ENOSYS) {
418 buf.actime = wim_timestamp_to_unix(inode->i_last_access_time);
419 buf.modtime = wim_timestamp_to_unix(inode->i_last_write_time);
420 if (utime(output_path, &buf) == 0)
424 if (errno != ENOSYS || args->num_lutimes_warnings < 10) {
425 /*WARNING_WITH_ERRNO("Failed to set timestamp on file `%s',*/
427 args->num_lutimes_warnings++;
432 #endif /* !__WIN32__ */
434 /* Extracts a file, directory, or symbolic link from the WIM archive. */
435 static int apply_dentry_normal(struct wim_dentry *dentry, void *arg)
437 struct apply_args *args = arg;
438 struct wim_inode *inode = dentry->d_inode;
442 len = strlen(args->target);
443 if (dentry_is_root(dentry)) {
444 output_path = (char*)args->target;
446 output_path = alloca(len + dentry->full_path_utf8_len + 1);
447 memcpy(output_path, args->target, len);
448 memcpy(output_path + len, dentry->full_path_utf8, dentry->full_path_utf8_len);
449 output_path[len + dentry->full_path_utf8_len] = '\0';
450 len += dentry->full_path_utf8_len;
453 return win32_do_apply_dentry(output_path, len, dentry, args);
455 return unix_do_apply_dentry(output_path, len, dentry, args);
460 /* Apply timestamps to an extracted file or directory */
461 static int apply_dentry_timestamps_normal(struct wim_dentry *dentry, void *arg)
463 struct apply_args *args = arg;
467 len = strlen(args->target);
468 if (dentry_is_root(dentry)) {
469 output_path = (char*)args->target;
471 output_path = alloca(len + dentry->full_path_utf8_len + 1);
472 memcpy(output_path, args->target, len);
473 memcpy(output_path + len, dentry->full_path_utf8, dentry->full_path_utf8_len);
474 output_path[len + dentry->full_path_utf8_len] = '\0';
475 len += dentry->full_path_utf8_len;
478 return win32_do_apply_dentry_timestamps(output_path, len, dentry, args);
480 return unix_do_apply_dentry_timestamps(output_path, len, dentry, args);
484 /* Extract a dentry if it hasn't already been extracted, and either the dentry
485 * has no streams or WIMLIB_EXTRACT_FLAG_NO_STREAMS is not specified. */
486 static int maybe_apply_dentry(struct wim_dentry *dentry, void *arg)
488 struct apply_args *args = arg;
491 if (dentry->is_extracted)
494 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_NO_STREAMS)
495 if (inode_unnamed_lte_resolved(dentry->d_inode))
498 if ((args->extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE) &&
499 args->progress_func) {
500 args->progress.extract.cur_path = dentry->full_path_utf8;
501 args->progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY,
504 ret = args->apply_dentry(dentry, args);
506 dentry->is_extracted = 1;
510 static int cmp_streams_by_wim_position(const void *p1, const void *p2)
512 const struct wim_lookup_table_entry *lte1, *lte2;
513 lte1 = *(const struct wim_lookup_table_entry**)p1;
514 lte2 = *(const struct wim_lookup_table_entry**)p2;
515 if (lte1->resource_entry.offset < lte2->resource_entry.offset)
517 else if (lte1->resource_entry.offset > lte2->resource_entry.offset)
523 static int sort_stream_list_by_wim_position(struct list_head *stream_list)
525 struct list_head *cur;
527 struct wim_lookup_table_entry **array;
532 list_for_each(cur, stream_list)
534 array_size = num_streams * sizeof(array[0]);
535 array = MALLOC(array_size);
537 ERROR("Failed to allocate %zu bytes to sort stream entries",
539 return WIMLIB_ERR_NOMEM;
541 cur = stream_list->next;
542 for (i = 0; i < num_streams; i++) {
543 array[i] = container_of(cur, struct wim_lookup_table_entry, staging_list);
547 qsort(array, num_streams, sizeof(array[0]), cmp_streams_by_wim_position);
549 INIT_LIST_HEAD(stream_list);
550 for (i = 0; i < num_streams; i++)
551 list_add_tail(&array[i]->staging_list, stream_list);
556 static void calculate_bytes_to_extract(struct list_head *stream_list,
558 union wimlib_progress_info *progress)
560 struct wim_lookup_table_entry *lte;
564 /* For each stream to be extracted... */
565 list_for_each_entry(lte, stream_list, staging_list) {
567 (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
569 /* In the symlink or hard link extraction mode, each
570 * stream will be extracted one time regardless of how
571 * many dentries share the stream. */
572 wimlib_assert(!(extract_flags & WIMLIB_EXTRACT_FLAG_NTFS));
573 if (!lte->extracted_file) {
575 total_bytes += wim_resource_size(lte);
578 num_streams += lte->out_refcnt;
579 total_bytes += lte->out_refcnt * wim_resource_size(lte);
582 progress->extract.num_streams = num_streams;
583 progress->extract.total_bytes = total_bytes;
584 progress->extract.completed_bytes = 0;
587 static void maybe_add_stream_for_extraction(struct wim_lookup_table_entry *lte,
588 struct list_head *stream_list)
590 if (++lte->out_refcnt == 1) {
591 INIT_LIST_HEAD(<e->inode_list);
592 list_add_tail(<e->staging_list, stream_list);
596 static void inode_find_streams_for_extraction(struct wim_inode *inode,
597 struct list_head *stream_list,
600 struct wim_lookup_table_entry *lte;
601 bool inode_added = false;
603 lte = inode_unnamed_lte_resolved(inode);
605 maybe_add_stream_for_extraction(lte, stream_list);
606 list_add_tail(&inode->i_lte_inode_list, <e->inode_list);
610 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
611 for (unsigned i = 0; i < inode->i_num_ads; i++) {
612 if (inode->i_ads_entries[i].stream_name_len != 0) {
613 lte = inode->i_ads_entries[i].lte;
615 maybe_add_stream_for_extraction(lte,
618 list_add_tail(&inode->i_lte_inode_list,
629 static void find_streams_for_extraction(struct hlist_head *inode_list,
630 struct list_head *stream_list,
631 struct wim_lookup_table *lookup_table,
634 struct wim_inode *inode;
635 struct hlist_node *cur;
636 struct wim_dentry *dentry;
638 for_lookup_table_entry(lookup_table, lte_zero_out_refcnt, NULL);
639 INIT_LIST_HEAD(stream_list);
640 hlist_for_each_entry(inode, cur, inode_list, i_hlist) {
641 if (!inode->i_resolved)
642 inode_resolve_ltes(inode, lookup_table);
643 inode_for_each_dentry(dentry, inode)
644 dentry->is_extracted = 0;
645 inode_find_streams_for_extraction(inode, stream_list,
650 struct apply_operations {
651 int (*apply_dentry)(struct wim_dentry *dentry, void *arg);
652 int (*apply_dentry_timestamps)(struct wim_dentry *dentry, void *arg);
655 static const struct apply_operations normal_apply_operations = {
656 .apply_dentry = apply_dentry_normal,
657 .apply_dentry_timestamps = apply_dentry_timestamps_normal,
661 static const struct apply_operations ntfs_apply_operations = {
662 .apply_dentry = apply_dentry_ntfs,
663 .apply_dentry_timestamps = apply_dentry_timestamps_ntfs,
667 static int apply_stream_list(struct list_head *stream_list,
668 struct apply_args *args,
669 const struct apply_operations *ops,
670 wimlib_progress_func_t progress_func)
672 uint64_t bytes_per_progress = args->progress.extract.total_bytes / 100;
673 uint64_t next_progress = bytes_per_progress;
674 struct wim_lookup_table_entry *lte;
675 struct wim_inode *inode;
676 struct wim_dentry *dentry;
679 /* This complicated loop is essentially looping through the dentries,
680 * although dentries may be visited more than once (if a dentry contains
681 * two different nonempty streams) or not at all (if a dentry contains
682 * no non-empty streams).
684 * The outer loop is over the distinct streams to be extracted so that
685 * sequential reading of the WIM can be implemented. */
687 /* For each distinct stream to be extracted */
688 list_for_each_entry(lte, stream_list, staging_list) {
689 /* For each inode that contains the stream */
690 list_for_each_entry(inode, <e->inode_list, i_lte_inode_list) {
691 /* For each dentry that points to the inode */
692 inode_for_each_dentry(dentry, inode) {
693 /* Extract the dentry if it was not already
695 ret = maybe_apply_dentry(dentry, args);
699 args->progress.extract.completed_bytes >= next_progress)
701 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS,
703 if (args->progress.extract.completed_bytes >=
704 args->progress.extract.total_bytes)
706 next_progress = ~0ULL;
709 min (args->progress.extract.completed_bytes +
711 args->progress.extract.total_bytes);
720 /* Extracts the image @image from the WIM @w to the directory or NTFS volume
722 static int extract_single_image(WIMStruct *w, int image,
723 const char *target, int extract_flags,
724 wimlib_progress_func_t progress_func)
727 struct list_head stream_list;
728 struct hlist_head *inode_list;
730 struct apply_args args;
731 const struct apply_operations *ops;
734 args.target = target;
735 args.extract_flags = extract_flags;
736 args.num_lutimes_warnings = 0;
737 args.stream_list = &stream_list;
738 args.progress_func = progress_func;
741 args.progress.extract.wimfile_name = w->filename;
742 args.progress.extract.image = image;
743 args.progress.extract.extract_flags = (extract_flags &
744 WIMLIB_EXTRACT_MASK_PUBLIC);
745 args.progress.extract.image_name = wimlib_get_image_name(w, image);
746 args.progress.extract.target = target;
750 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
751 args.vol = ntfs_mount(target, 0);
753 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%s'", target);
754 return WIMLIB_ERR_NTFS_3G;
756 ops = &ntfs_apply_operations;
759 ops = &normal_apply_operations;
761 ret = select_wim_image(w, image);
765 inode_list = &w->image_metadata[image - 1].inode_list;
767 /* Build a list of the streams that need to be extracted */
768 find_streams_for_extraction(inode_list, &stream_list,
769 w->lookup_table, extract_flags);
771 /* Calculate the number of bytes of data that will be extracted */
772 calculate_bytes_to_extract(&stream_list, extract_flags,
776 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN,
780 /* If a sequential extraction was specified, sort the streams to be
781 * extracted by their position in the WIM file, so that the WIM file can
782 * be read sequentially. */
783 if (extract_flags & WIMLIB_EXTRACT_FLAG_SEQUENTIAL) {
784 ret = sort_stream_list_by_wim_position(&stream_list);
786 WARNING("Falling back to non-sequential extraction");
787 extract_flags &= ~WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
792 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN,
796 /* Make the directory structure and extract empty files */
797 args.extract_flags |= WIMLIB_EXTRACT_FLAG_NO_STREAMS;
798 args.apply_dentry = ops->apply_dentry;
799 ret = for_dentry_in_tree(wim_root_dentry(w), maybe_apply_dentry, &args);
800 args.extract_flags &= ~WIMLIB_EXTRACT_FLAG_NO_STREAMS;
805 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_END,
809 /* Extract non-empty files */
810 ret = apply_stream_list(&stream_list, &args, ops, progress_func);
815 progress_func(WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS,
819 /* Apply timestamps */
820 ret = for_dentry_in_tree_depth(wim_root_dentry(w),
821 ops->apply_dentry_timestamps, &args);
826 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END,
831 /* Unmount the NTFS volume */
832 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
833 if (ntfs_umount(args.vol, FALSE) != 0) {
834 ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%s'", args.target);
836 ret = WIMLIB_ERR_NTFS_3G;
844 /* Extracts all images from the WIM to the directory @target, with the images
845 * placed in subdirectories named by their image names. */
846 static int extract_all_images(WIMStruct *w, const char *target,
848 wimlib_progress_func_t progress_func)
850 size_t image_name_max_len = max(xml_get_max_image_name_len(w), 20);
851 size_t output_path_len = strlen(target);
852 char buf[output_path_len + 1 + image_name_max_len + 1];
855 const char *image_name;
857 ret = extract_directory(NULL, target, true);
861 memcpy(buf, target, output_path_len);
862 buf[output_path_len] = '/';
863 for (image = 1; image <= w->hdr.image_count; image++) {
864 image_name = wimlib_get_image_name(w, image);
865 if (image_name && *image_name) {
866 strcpy(buf + output_path_len + 1, image_name);
868 /* Image name is empty. Use image number instead */
869 sprintf(buf + output_path_len + 1, "%d", image);
871 ret = extract_single_image(w, image, buf, extract_flags,
879 /* Extracts a single image or all images from a WIM file to a directory or NTFS
881 WIMLIBAPI int wimlib_extract_image(WIMStruct *w,
885 WIMStruct **additional_swms,
886 unsigned num_additional_swms,
887 wimlib_progress_func_t progress_func)
889 struct wim_lookup_table *joined_tab, *w_tab_save;
893 return WIMLIB_ERR_INVALID_PARAM;
895 extract_flags &= WIMLIB_EXTRACT_MASK_PUBLIC;
897 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
898 == (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
899 return WIMLIB_ERR_INVALID_PARAM;
902 if (extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
903 ERROR("Extracting UNIX data is not supported on Windows");
904 return WIMLIB_ERR_INVALID_PARAM;
906 if (extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK)) {
907 ERROR("Linked extraction modes are not supported on Windows");
908 return WIMLIB_ERR_INVALID_PARAM;
912 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
914 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))) {
915 ERROR("Cannot specify symlink or hardlink flags when applying\n"
916 " directly to a NTFS volume");
917 return WIMLIB_ERR_INVALID_PARAM;
919 if (image == WIMLIB_ALL_IMAGES) {
920 ERROR("Can only apply a single image when applying "
921 "directly to a NTFS volume");
922 return WIMLIB_ERR_INVALID_PARAM;
924 if (extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
925 ERROR("Cannot restore UNIX-specific data in the NTFS extraction mode");
926 return WIMLIB_ERR_INVALID_PARAM;
929 ERROR("wimlib was compiled without support for NTFS-3g, so");
930 ERROR("we cannot apply a WIM image directly to a NTFS volume");
931 return WIMLIB_ERR_UNSUPPORTED;
935 ret = verify_swm_set(w, additional_swms, num_additional_swms);
939 if (num_additional_swms) {
940 ret = new_joined_lookup_table(w, additional_swms,
941 num_additional_swms, &joined_tab);
944 w_tab_save = w->lookup_table;
945 w->lookup_table = joined_tab;
949 win32_acquire_restore_privileges();
951 if (image == WIMLIB_ALL_IMAGES) {
952 extract_flags |= WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
953 ret = extract_all_images(w, target, extract_flags,
956 extract_flags &= ~WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
957 ret = extract_single_image(w, image, target, extract_flags,
961 win32_release_restore_privileges();
964 if (extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
965 WIMLIB_EXTRACT_FLAG_HARDLINK))
967 for_lookup_table_entry(w->lookup_table,
968 lte_free_extracted_file,
972 if (num_additional_swms) {
973 free_lookup_table(w->lookup_table);
974 w->lookup_table = w_tab_save;