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>
66 extract_regular_file_linked(struct wim_dentry *dentry,
67 const char *output_path,
68 struct apply_args *args,
69 struct wim_lookup_table_entry *lte)
71 /* This mode overrides the normal hard-link extraction and
72 * instead either symlinks or hardlinks *all* identical files in
73 * the WIM, even if they are in a different image (in the case
74 * of a multi-image extraction) */
76 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_HARDLINK) {
77 if (link(lte->extracted_file, output_path) != 0) {
78 ERROR_WITH_ERRNO("Failed to hard link "
80 output_path, lte->extracted_file);
81 return WIMLIB_ERR_LINK;
84 int num_path_components;
85 int num_output_dir_path_components;
86 size_t extracted_file_len;
92 get_num_path_components(dentry->full_path) - 1;
93 num_output_dir_path_components =
94 get_num_path_components(args->target);
96 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE) {
97 num_path_components++;
98 num_output_dir_path_components--;
100 extracted_file_len = strlen(lte->extracted_file);
102 char buf[extracted_file_len + 3 * num_path_components + 1];
105 for (i = 0; i < num_path_components; i++) {
110 p2 = lte->extracted_file;
113 while (num_output_dir_path_components--)
114 p2 = path_next_part(p2, NULL);
116 if (symlink(buf, output_path) != 0) {
117 ERROR_WITH_ERRNO("Failed to symlink `%s' to `%s'",
118 buf, lte->extracted_file);
119 return WIMLIB_ERR_LINK;
126 symlink_apply_unix_data(const mbchar *link,
127 const struct wimlib_unix_data *unix_data)
129 if (lchown(link, unix_data->uid, unix_data->gid)) {
130 if (errno == EPERM) {
132 WARNING_WITH_ERRNO("failed to set symlink UNIX owner/group");
134 ERROR_WITH_ERRNO("failed to set symlink UNIX owner/group");
135 return WIMLIB_ERR_INVALID_DENTRY;
142 fd_apply_unix_data(int fd, const struct wimlib_unix_data *unix_data)
144 if (fchown(fd, unix_data->uid, unix_data->gid)) {
145 if (errno == EPERM) {
146 WARNING_WITH_ERRNO("failed to set file UNIX owner/group");
149 ERROR_WITH_ERRNO("failed to set file UNIX owner/group");
150 return WIMLIB_ERR_INVALID_DENTRY;
154 if (fchmod(fd, unix_data->mode)) {
155 if (errno == EPERM) {
156 WARNING_WITH_ERRNO("failed to set UNIX file mode");
159 ERROR_WITH_ERRNO("failed to set UNIX file mode");
160 return WIMLIB_ERR_INVALID_DENTRY;
167 dir_apply_unix_data(const char *dir, const struct wimlib_unix_data *unix_data)
169 int dfd = open(dir, O_RDONLY);
172 ret = fd_apply_unix_data(dfd, unix_data);
174 ERROR_WITH_ERRNO("can't close directory `%s'", dir);
175 ret = WIMLIB_ERR_MKDIR;
178 ERROR_WITH_ERRNO("can't open directory `%s'", dir);
179 ret = WIMLIB_ERR_MKDIR;
185 extract_regular_file_unlinked(struct wim_dentry *dentry,
186 struct apply_args *args,
187 const char *output_path,
188 struct wim_lookup_table_entry *lte)
190 /* Normal mode of extraction. Regular files and hard links are
191 * extracted in the way that they appear in the WIM. */
195 struct wim_inode *inode = dentry->d_inode;
197 if (!((args->extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE)
198 && (args->extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
199 WIMLIB_EXTRACT_FLAG_HARDLINK))))
201 /* If the dentry is part of a hard link set of at least 2
202 * dentries and one of the other dentries has already been
203 * extracted, make a hard link to the file corresponding to this
204 * already-extracted directory. Otherwise, extract the file and
205 * set the inode->i_extracted_file field so that other dentries
206 * in the hard link group can link to it. */
207 if (inode->i_nlink > 1) {
208 if (inode->i_extracted_file) {
209 DEBUG("Extracting hard link `%s' => `%s'",
210 output_path, inode->i_extracted_file);
211 if (link(inode->i_extracted_file, output_path) != 0) {
212 ERROR_WITH_ERRNO("Failed to hard link "
215 inode->i_extracted_file);
216 return WIMLIB_ERR_LINK;
220 FREE(inode->i_extracted_file);
221 inode->i_extracted_file = STRDUP(output_path);
222 if (!inode->i_extracted_file) {
223 ERROR("Failed to allocate memory for filename");
224 return WIMLIB_ERR_NOMEM;
229 /* Extract the contents of the file to @output_path. */
231 out_fd = open(output_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
233 ERROR_WITH_ERRNO("Failed to open the file `%s' for writing",
235 return WIMLIB_ERR_OPEN;
239 /* Empty file with no lookup table entry */
240 DEBUG("Empty file `%s'.", output_path);
242 goto out_extract_unix_data;
245 ret = extract_wim_resource_to_fd(lte, out_fd, wim_resource_size(lte));
247 ERROR("Failed to extract resource to `%s'", output_path);
251 out_extract_unix_data:
252 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
253 struct wimlib_unix_data unix_data;
254 ret = inode_get_unix_data(inode, &unix_data, NULL);
260 ret = fd_apply_unix_data(out_fd, &unix_data);
265 args->progress.extract.completed_bytes += wim_resource_size(lte);
267 if (close(out_fd) != 0) {
268 ERROR_WITH_ERRNO("Failed to close file `%s'", output_path);
270 ret = WIMLIB_ERR_WRITE;
276 extract_regular_file(struct wim_dentry *dentry,
277 struct apply_args *args,
278 const char *output_path)
280 struct wim_lookup_table_entry *lte;
281 const struct wim_inode *inode = dentry->d_inode;
283 lte = inode_unnamed_lte_resolved(inode);
285 if (lte && (args->extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
286 WIMLIB_EXTRACT_FLAG_HARDLINK)))
288 if (lte->extracted_file) {
289 return extract_regular_file_linked(dentry, output_path, args, lte);
291 lte->extracted_file = STRDUP(output_path);
292 if (!lte->extracted_file)
293 return WIMLIB_ERR_NOMEM;
296 return extract_regular_file_unlinked(dentry, args, output_path, lte);
300 extract_symlink(struct wim_dentry *dentry,
301 struct apply_args *args,
302 const char *output_path)
305 ssize_t ret = inode_readlink(dentry->d_inode, target,
306 sizeof(target), args->w, 0);
307 struct wim_lookup_table_entry *lte;
310 ERROR("Could not read the symbolic link from dentry `%s'",
312 return WIMLIB_ERR_INVALID_DENTRY;
314 ret = symlink(target, output_path);
316 ERROR_WITH_ERRNO("Failed to symlink `%s' to `%s'",
317 output_path, target);
318 return WIMLIB_ERR_LINK;
320 lte = inode_unnamed_lte_resolved(dentry->d_inode);
321 wimlib_assert(lte != NULL);
322 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
323 struct wimlib_unix_data unix_data;
324 ret = inode_get_unix_data(dentry->d_inode, &unix_data, NULL);
330 ret = symlink_apply_unix_data(output_path, &unix_data);
334 args->progress.extract.completed_bytes += wim_resource_size(lte);
338 #endif /* !__WIN32__ */
341 extract_directory(struct wim_dentry *dentry,
342 const tchar *output_path, bool is_root)
347 ret = tstat(output_path, &stbuf);
349 if (S_ISDIR(stbuf.st_mode)) {
351 /*WARNING("`%s' already exists", output_path);*/
354 ERROR("`%"TS"' is not a directory", output_path);
355 return WIMLIB_ERR_MKDIR;
358 if (errno != ENOENT) {
359 ERROR_WITH_ERRNO("Failed to stat `%"TS"'", output_path);
360 return WIMLIB_ERR_STAT;
364 if (tmkdir(output_path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH))
366 ERROR_WITH_ERRNO("Cannot create directory `%"TS"'", output_path);
367 return WIMLIB_ERR_MKDIR;
373 struct wimlib_unix_data unix_data;
374 ret = inode_get_unix_data(dentry->d_inode, &unix_data, NULL);
380 ret = dir_apply_unix_data(output_path, &unix_data);
387 static int unix_do_apply_dentry(const char *output_path,
388 size_t output_path_len,
389 struct wim_dentry *dentry,
390 struct apply_args *args)
392 const struct wim_inode *inode = dentry->d_inode;
394 if (inode_is_symlink(inode))
395 return extract_symlink(dentry, args, output_path);
396 else if (inode_is_directory(inode))
397 return extract_directory((args->extract_flags &
398 WIMLIB_EXTRACT_FLAG_UNIX_DATA) ? dentry : NULL,
401 return extract_regular_file(dentry, args, output_path);
405 unix_do_apply_dentry_timestamps(const char *output_path,
406 size_t output_path_len,
407 const struct wim_dentry *dentry,
408 struct apply_args *args)
411 const struct wim_inode *inode = dentry->d_inode;
413 #ifdef HAVE_UTIMENSAT
414 /* Convert the WIM timestamps, which are accurate to 100 nanoseconds,
415 * into `struct timespec's for passing to utimensat(), which is accurate
416 * to 1 nanosecond. */
418 struct timespec ts[2];
419 ts[0] = wim_timestamp_to_timespec(inode->i_last_access_time);
420 ts[1] = wim_timestamp_to_timespec(inode->i_last_write_time);
421 ret = utimensat(AT_FDCWD, output_path, ts, AT_SYMLINK_NOFOLLOW);
429 /* utimensat() not implemented or not available */
431 /* Convert the WIM timestamps, which are accurate to 100
432 * nanoseconds, into `struct timeval's for passing to lutimes(),
433 * which is accurate to 1 microsecond. */
434 struct timeval tv[2];
435 tv[0] = wim_timestamp_to_timeval(inode->i_last_access_time);
436 tv[1] = wim_timestamp_to_timeval(inode->i_last_write_time);
437 ret = lutimes(output_path, tv);
444 /* utimensat() and lutimes() both not implemented or not
447 /* Convert the WIM timestamps, which are accurate to 100
448 * nanoseconds, into a `struct utimbuf's for passing to
449 * utime(), which is accurate to 1 second. */
451 buf.actime = wim_timestamp_to_unix(inode->i_last_access_time);
452 buf.modtime = wim_timestamp_to_unix(inode->i_last_write_time);
453 ret = utime(output_path, &buf);
456 if (ret && args->num_utime_warnings < 10) {
457 WARNING_WITH_ERRNO("Failed to set timestamp on file `%s'",
459 args->num_utime_warnings++;
463 #endif /* !__WIN32__ */
465 /* Extracts a file, directory, or symbolic link from the WIM archive. */
467 apply_dentry_normal(struct wim_dentry *dentry, void *arg)
469 struct apply_args *args = arg;
473 len = tstrlen(args->target);
474 if (dentry_is_root(dentry)) {
475 output_path = (tchar*)args->target;
477 output_path = alloca(len * sizeof(tchar) + dentry->full_path_nbytes +
479 memcpy(output_path, args->target, len * sizeof(tchar));
480 memcpy(output_path + len, dentry->full_path, dentry->full_path_nbytes);
481 len += dentry->full_path_nbytes / sizeof(tchar);
482 output_path[len] = T('\0');
485 return win32_do_apply_dentry(output_path, len, dentry, args);
487 return unix_do_apply_dentry(output_path, len, dentry, args);
492 /* Apply timestamps to an extracted file or directory */
494 apply_dentry_timestamps_normal(struct wim_dentry *dentry, void *arg)
496 struct apply_args *args = arg;
500 len = tstrlen(args->target);
501 if (dentry_is_root(dentry)) {
502 output_path = (tchar*)args->target;
504 output_path = alloca(len * sizeof(tchar) + dentry->full_path_nbytes +
506 memcpy(output_path, args->target, len * sizeof(tchar));
507 memcpy(output_path + len, dentry->full_path, dentry->full_path_nbytes);
508 len += dentry->full_path_nbytes / sizeof(tchar);
509 output_path[len] = T('\0');
514 return win32_do_apply_dentry_timestamps(output_path, len, dentry, args);
516 return unix_do_apply_dentry_timestamps(output_path, len, dentry, args);
520 /* Extract a dentry if it hasn't already been extracted, and either the dentry
521 * has no streams or WIMLIB_EXTRACT_FLAG_NO_STREAMS is not specified. */
523 maybe_apply_dentry(struct wim_dentry *dentry, void *arg)
525 struct apply_args *args = arg;
528 if (dentry->is_extracted)
531 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_NO_STREAMS)
532 if (inode_unnamed_lte_resolved(dentry->d_inode))
535 if ((args->extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE) &&
536 args->progress_func) {
537 args->progress.extract.cur_path = dentry->full_path;
538 args->progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY,
541 ret = args->apply_dentry(dentry, args);
543 dentry->is_extracted = 1;
548 cmp_streams_by_wim_position(const void *p1, const void *p2)
550 const struct wim_lookup_table_entry *lte1, *lte2;
551 lte1 = *(const struct wim_lookup_table_entry**)p1;
552 lte2 = *(const struct wim_lookup_table_entry**)p2;
553 if (lte1->resource_entry.offset < lte2->resource_entry.offset)
555 else if (lte1->resource_entry.offset > lte2->resource_entry.offset)
562 sort_stream_list_by_wim_position(struct list_head *stream_list)
564 struct list_head *cur;
566 struct wim_lookup_table_entry **array;
571 list_for_each(cur, stream_list)
573 array_size = num_streams * sizeof(array[0]);
574 array = MALLOC(array_size);
576 ERROR("Failed to allocate %zu bytes to sort stream entries",
578 return WIMLIB_ERR_NOMEM;
580 cur = stream_list->next;
581 for (i = 0; i < num_streams; i++) {
582 array[i] = container_of(cur, struct wim_lookup_table_entry, staging_list);
586 qsort(array, num_streams, sizeof(array[0]), cmp_streams_by_wim_position);
588 INIT_LIST_HEAD(stream_list);
589 for (i = 0; i < num_streams; i++)
590 list_add_tail(&array[i]->staging_list, stream_list);
596 calculate_bytes_to_extract(struct list_head *stream_list,
598 union wimlib_progress_info *progress)
600 struct wim_lookup_table_entry *lte;
604 /* For each stream to be extracted... */
605 list_for_each_entry(lte, stream_list, staging_list) {
607 (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
609 /* In the symlink or hard link extraction mode, each
610 * stream will be extracted one time regardless of how
611 * many dentries share the stream. */
612 wimlib_assert(!(extract_flags & WIMLIB_EXTRACT_FLAG_NTFS));
613 if (!lte->extracted_file) {
615 total_bytes += wim_resource_size(lte);
618 num_streams += lte->out_refcnt;
619 total_bytes += lte->out_refcnt * wim_resource_size(lte);
622 progress->extract.num_streams = num_streams;
623 progress->extract.total_bytes = total_bytes;
624 progress->extract.completed_bytes = 0;
628 maybe_add_stream_for_extraction(struct wim_lookup_table_entry *lte,
629 struct list_head *stream_list)
631 if (++lte->out_refcnt == 1) {
632 INIT_LIST_HEAD(<e->inode_list);
633 list_add_tail(<e->staging_list, stream_list);
638 inode_find_streams_for_extraction(struct wim_inode *inode,
639 struct list_head *stream_list,
642 struct wim_lookup_table_entry *lte;
643 bool inode_added = false;
645 lte = inode_unnamed_lte_resolved(inode);
647 maybe_add_stream_for_extraction(lte, stream_list);
648 list_add_tail(&inode->i_lte_inode_list, <e->inode_list);
652 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
653 for (unsigned i = 0; i < inode->i_num_ads; i++) {
654 if (inode->i_ads_entries[i].stream_name_nbytes != 0) {
655 lte = inode->i_ads_entries[i].lte;
657 maybe_add_stream_for_extraction(lte,
660 list_add_tail(&inode->i_lte_inode_list,
672 find_streams_for_extraction(struct hlist_head *inode_list,
673 struct list_head *stream_list,
674 struct wim_lookup_table *lookup_table,
677 struct wim_inode *inode;
678 struct hlist_node *cur;
679 struct wim_dentry *dentry;
681 for_lookup_table_entry(lookup_table, lte_zero_out_refcnt, NULL);
682 INIT_LIST_HEAD(stream_list);
683 hlist_for_each_entry(inode, cur, inode_list, i_hlist) {
684 if (!inode->i_resolved)
685 inode_resolve_ltes(inode, lookup_table);
686 inode_for_each_dentry(dentry, inode)
687 dentry->is_extracted = 0;
688 inode_find_streams_for_extraction(inode, stream_list,
693 struct apply_operations {
694 int (*apply_dentry)(struct wim_dentry *dentry, void *arg);
695 int (*apply_dentry_timestamps)(struct wim_dentry *dentry, void *arg);
698 static const struct apply_operations normal_apply_operations = {
699 .apply_dentry = apply_dentry_normal,
700 .apply_dentry_timestamps = apply_dentry_timestamps_normal,
704 static const struct apply_operations ntfs_apply_operations = {
705 .apply_dentry = apply_dentry_ntfs,
706 .apply_dentry_timestamps = apply_dentry_timestamps_ntfs,
711 apply_stream_list(struct list_head *stream_list,
712 struct apply_args *args,
713 const struct apply_operations *ops,
714 wimlib_progress_func_t progress_func)
716 uint64_t bytes_per_progress = args->progress.extract.total_bytes / 100;
717 uint64_t next_progress = bytes_per_progress;
718 struct wim_lookup_table_entry *lte;
719 struct wim_inode *inode;
720 struct wim_dentry *dentry;
723 /* This complicated loop is essentially looping through the dentries,
724 * although dentries may be visited more than once (if a dentry contains
725 * two different nonempty streams) or not at all (if a dentry contains
726 * no non-empty streams).
728 * The outer loop is over the distinct streams to be extracted so that
729 * sequential reading of the WIM can be implemented. */
731 /* For each distinct stream to be extracted */
732 list_for_each_entry(lte, stream_list, staging_list) {
733 /* For each inode that contains the stream */
734 list_for_each_entry(inode, <e->inode_list, i_lte_inode_list) {
735 /* For each dentry that points to the inode */
736 inode_for_each_dentry(dentry, inode) {
737 /* Extract the dentry if it was not already
739 ret = maybe_apply_dentry(dentry, args);
743 args->progress.extract.completed_bytes >= next_progress)
745 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS,
747 if (args->progress.extract.completed_bytes >=
748 args->progress.extract.total_bytes)
750 next_progress = ~0ULL;
753 min (args->progress.extract.completed_bytes +
755 args->progress.extract.total_bytes);
764 /* Extracts the image @image from the WIM @w to the directory or NTFS volume
767 extract_single_image(WIMStruct *w, int image,
768 const tchar *target, int extract_flags,
769 wimlib_progress_func_t progress_func)
772 struct list_head stream_list;
773 struct hlist_head *inode_list;
775 struct apply_args args;
776 const struct apply_operations *ops;
779 args.target = target;
780 args.extract_flags = extract_flags;
781 args.num_utime_warnings = 0;
782 args.stream_list = &stream_list;
783 args.progress_func = progress_func;
786 args.progress.extract.wimfile_name = w->filename;
787 args.progress.extract.image = image;
788 args.progress.extract.extract_flags = (extract_flags &
789 WIMLIB_EXTRACT_MASK_PUBLIC);
790 args.progress.extract.image_name = wimlib_get_image_name(w, image);
791 args.progress.extract.target = target;
795 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
796 args.vol = ntfs_mount(target, 0);
798 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%"TS"'",
800 return WIMLIB_ERR_NTFS_3G;
802 ops = &ntfs_apply_operations;
805 ops = &normal_apply_operations;
807 ret = select_wim_image(w, image);
811 inode_list = &w->image_metadata[image - 1].inode_list;
813 /* Build a list of the streams that need to be extracted */
814 find_streams_for_extraction(inode_list, &stream_list,
815 w->lookup_table, extract_flags);
817 /* Calculate the number of bytes of data that will be extracted */
818 calculate_bytes_to_extract(&stream_list, extract_flags,
822 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN,
826 /* If a sequential extraction was specified, sort the streams to be
827 * extracted by their position in the WIM file, so that the WIM file can
828 * be read sequentially. */
829 if (extract_flags & WIMLIB_EXTRACT_FLAG_SEQUENTIAL) {
830 ret = sort_stream_list_by_wim_position(&stream_list);
832 WARNING("Falling back to non-sequential extraction");
833 extract_flags &= ~WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
838 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN,
842 /* Make the directory structure and extract empty files */
843 args.extract_flags |= WIMLIB_EXTRACT_FLAG_NO_STREAMS;
844 args.apply_dentry = ops->apply_dentry;
845 ret = for_dentry_in_tree(wim_root_dentry(w), maybe_apply_dentry, &args);
846 args.extract_flags &= ~WIMLIB_EXTRACT_FLAG_NO_STREAMS;
851 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_END,
855 /* Extract non-empty files */
856 ret = apply_stream_list(&stream_list, &args, ops, progress_func);
861 progress_func(WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS,
865 /* Apply timestamps */
866 ret = for_dentry_in_tree_depth(wim_root_dentry(w),
867 ops->apply_dentry_timestamps, &args);
872 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END,
877 /* Unmount the NTFS volume */
878 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
879 if (ntfs_umount(args.vol, FALSE) != 0) {
880 ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%"TS"'",
883 ret = WIMLIB_ERR_NTFS_3G;
892 image_name_ok_as_dir(const tchar *image_name)
894 if (image_name == NULL)
896 if (image_name[0] == T('\0'))
898 if (tstrchr(image_name, T('/')))
900 if (tstrchr(image_name, T('\\')))
905 /* Extracts all images from the WIM to the directory @target, with the images
906 * placed in subdirectories named by their image names. */
908 extract_all_images(WIMStruct *w,
911 wimlib_progress_func_t progress_func)
913 size_t image_name_max_len = max(xml_get_max_image_name_len(w), 20);
914 size_t output_path_len = tstrlen(target);
915 tchar buf[output_path_len + 1 + image_name_max_len + 1];
918 const tchar *image_name;
920 ret = extract_directory(NULL, target, true);
924 wmemcpy(buf, target, output_path_len);
925 buf[output_path_len] = T('/');
926 for (image = 1; image <= w->hdr.image_count; image++) {
927 image_name = wimlib_get_image_name(w, image);
928 if (image_name_ok_as_dir(image_name)) {
929 tstrcpy(buf + output_path_len + 1, image_name);
931 /* Image name is empty, or may not be representable in
932 * the current locale, or contains path separators. Use
933 * the image number instead. */
934 tsprintf(buf + output_path_len + 1, T("%d"), image);
936 ret = extract_single_image(w, image, buf, extract_flags,
944 /* Extracts a single image or all images from a WIM file to a directory or NTFS
947 wimlib_extract_image(WIMStruct *w,
951 WIMStruct **additional_swms,
952 unsigned num_additional_swms,
953 wimlib_progress_func_t progress_func)
955 struct wim_lookup_table *joined_tab, *w_tab_save;
959 return WIMLIB_ERR_INVALID_PARAM;
961 extract_flags &= WIMLIB_EXTRACT_MASK_PUBLIC;
963 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
964 == (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
965 return WIMLIB_ERR_INVALID_PARAM;
968 if (extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
969 ERROR("Extracting UNIX data is not supported on Windows");
970 return WIMLIB_ERR_INVALID_PARAM;
972 if (extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK)) {
973 ERROR("Linked extraction modes are not supported on Windows");
974 return WIMLIB_ERR_INVALID_PARAM;
978 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
980 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))) {
981 ERROR("Cannot specify symlink or hardlink flags when applying\n"
982 " directly to a NTFS volume");
983 return WIMLIB_ERR_INVALID_PARAM;
985 if (image == WIMLIB_ALL_IMAGES) {
986 ERROR("Can only apply a single image when applying "
987 "directly to a NTFS volume");
988 return WIMLIB_ERR_INVALID_PARAM;
990 if (extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
991 ERROR("Cannot restore UNIX-specific data in the NTFS extraction mode");
992 return WIMLIB_ERR_INVALID_PARAM;
995 ERROR("wimlib was compiled without support for NTFS-3g, so");
996 ERROR("we cannot apply a WIM image directly to a NTFS volume");
997 return WIMLIB_ERR_UNSUPPORTED;
1001 ret = verify_swm_set(w, additional_swms, num_additional_swms);
1005 if (num_additional_swms) {
1006 ret = new_joined_lookup_table(w, additional_swms,
1007 num_additional_swms, &joined_tab);
1010 w_tab_save = w->lookup_table;
1011 w->lookup_table = joined_tab;
1014 if (image == WIMLIB_ALL_IMAGES) {
1015 extract_flags |= WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
1016 ret = extract_all_images(w, target, extract_flags,
1019 extract_flags &= ~WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
1020 ret = extract_single_image(w, image, target, extract_flags,
1024 if (extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
1025 WIMLIB_EXTRACT_FLAG_HARDLINK))
1027 for_lookup_table_entry(w->lookup_table,
1028 lte_free_extracted_file,
1032 if (num_additional_swms) {
1033 free_lookup_table(w->lookup_table);
1034 w->lookup_table = w_tab_save;