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 /* Returns the number of components of @path. */
64 get_num_path_components(const char *path)
66 unsigned num_components = 0;
72 while (*path && *path != '/')
75 return num_components;
79 path_next_part(const char *path)
81 while (*path && *path != '/')
83 while (*path && *path == '/')
89 extract_regular_file_linked(struct wim_dentry *dentry,
90 const char *output_path,
91 struct apply_args *args,
92 struct wim_lookup_table_entry *lte)
94 /* This mode overrides the normal hard-link extraction and
95 * instead either symlinks or hardlinks *all* identical files in
96 * the WIM, even if they are in a different image (in the case
97 * of a multi-image extraction) */
99 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_HARDLINK) {
100 if (link(lte->extracted_file, output_path) != 0) {
101 ERROR_WITH_ERRNO("Failed to hard link "
103 output_path, lte->extracted_file);
104 return WIMLIB_ERR_LINK;
107 int num_path_components;
108 int num_output_dir_path_components;
109 size_t extracted_file_len;
114 num_path_components =
115 get_num_path_components(dentry->full_path) - 1;
116 num_output_dir_path_components =
117 get_num_path_components(args->target);
119 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE) {
120 num_path_components++;
121 num_output_dir_path_components--;
123 extracted_file_len = strlen(lte->extracted_file);
125 char buf[extracted_file_len + 3 * num_path_components + 1];
128 for (i = 0; i < num_path_components; i++) {
133 p2 = lte->extracted_file;
136 while (num_output_dir_path_components > 0) {
137 p2 = path_next_part(p2);
138 num_output_dir_path_components--;
141 if (symlink(buf, output_path) != 0) {
142 ERROR_WITH_ERRNO("Failed to symlink `%s' to `%s'",
143 buf, lte->extracted_file);
144 return WIMLIB_ERR_LINK;
151 symlink_apply_unix_data(const char *link,
152 const struct wimlib_unix_data *unix_data)
154 if (lchown(link, unix_data->uid, unix_data->gid)) {
155 if (errno == EPERM) {
157 WARNING_WITH_ERRNO("failed to set symlink UNIX owner/group");
159 ERROR_WITH_ERRNO("failed to set symlink UNIX owner/group");
160 return WIMLIB_ERR_INVALID_DENTRY;
167 fd_apply_unix_data(int fd, const struct wimlib_unix_data *unix_data)
169 if (fchown(fd, unix_data->uid, unix_data->gid)) {
170 if (errno == EPERM) {
171 WARNING_WITH_ERRNO("failed to set file UNIX owner/group");
174 ERROR_WITH_ERRNO("failed to set file UNIX owner/group");
175 return WIMLIB_ERR_INVALID_DENTRY;
179 if (fchmod(fd, unix_data->mode)) {
180 if (errno == EPERM) {
181 WARNING_WITH_ERRNO("failed to set UNIX file mode");
184 ERROR_WITH_ERRNO("failed to set UNIX file mode");
185 return WIMLIB_ERR_INVALID_DENTRY;
192 dir_apply_unix_data(const char *dir, const struct wimlib_unix_data *unix_data)
194 int dfd = open(dir, O_RDONLY);
197 ret = fd_apply_unix_data(dfd, unix_data);
199 ERROR_WITH_ERRNO("can't close directory `%s'", dir);
200 ret = WIMLIB_ERR_MKDIR;
203 ERROR_WITH_ERRNO("can't open directory `%s'", dir);
204 ret = WIMLIB_ERR_MKDIR;
210 extract_regular_file_unlinked(struct wim_dentry *dentry,
211 struct apply_args *args,
212 const char *output_path,
213 struct wim_lookup_table_entry *lte)
215 /* Normal mode of extraction. Regular files and hard links are
216 * extracted in the way that they appear in the WIM. */
220 struct wim_inode *inode = dentry->d_inode;
222 if (!((args->extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE)
223 && (args->extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
224 WIMLIB_EXTRACT_FLAG_HARDLINK))))
226 /* If the dentry is part of a hard link set of at least 2
227 * dentries and one of the other dentries has already been
228 * extracted, make a hard link to the file corresponding to this
229 * already-extracted directory. Otherwise, extract the file and
230 * set the inode->i_extracted_file field so that other dentries
231 * in the hard link group can link to it. */
232 if (inode->i_nlink > 1) {
233 if (inode->i_extracted_file) {
234 DEBUG("Extracting hard link `%s' => `%s'",
235 output_path, inode->i_extracted_file);
236 if (link(inode->i_extracted_file, output_path) != 0) {
237 ERROR_WITH_ERRNO("Failed to hard link "
240 inode->i_extracted_file);
241 return WIMLIB_ERR_LINK;
245 FREE(inode->i_extracted_file);
246 inode->i_extracted_file = STRDUP(output_path);
247 if (!inode->i_extracted_file) {
248 ERROR("Failed to allocate memory for filename");
249 return WIMLIB_ERR_NOMEM;
254 /* Extract the contents of the file to @output_path. */
256 out_fd = open(output_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
258 ERROR_WITH_ERRNO("Failed to open the file `%s' for writing",
260 return WIMLIB_ERR_OPEN;
264 /* Empty file with no lookup table entry */
265 DEBUG("Empty file `%s'.", output_path);
267 goto out_extract_unix_data;
270 ret = extract_wim_resource_to_fd(lte, out_fd, wim_resource_size(lte));
272 ERROR("Failed to extract resource to `%s'", output_path);
276 out_extract_unix_data:
277 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
278 struct wimlib_unix_data unix_data;
279 ret = inode_get_unix_data(inode, &unix_data, NULL);
285 ret = fd_apply_unix_data(out_fd, &unix_data);
290 args->progress.extract.completed_bytes += wim_resource_size(lte);
292 if (close(out_fd) != 0) {
293 ERROR_WITH_ERRNO("Failed to close file `%s'", output_path);
295 ret = WIMLIB_ERR_WRITE;
301 extract_regular_file(struct wim_dentry *dentry,
302 struct apply_args *args,
303 const char *output_path)
305 struct wim_lookup_table_entry *lte;
306 const struct wim_inode *inode = dentry->d_inode;
308 lte = inode_unnamed_lte_resolved(inode);
310 if (lte && (args->extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
311 WIMLIB_EXTRACT_FLAG_HARDLINK)))
313 if (lte->extracted_file) {
314 return extract_regular_file_linked(dentry, output_path, args, lte);
316 lte->extracted_file = STRDUP(output_path);
317 if (!lte->extracted_file)
318 return WIMLIB_ERR_NOMEM;
321 return extract_regular_file_unlinked(dentry, args, output_path, lte);
325 extract_symlink(struct wim_dentry *dentry,
326 struct apply_args *args,
327 const char *output_path)
330 ssize_t ret = inode_readlink(dentry->d_inode, target,
331 sizeof(target), args->w, 0);
332 struct wim_lookup_table_entry *lte;
335 ERROR("Could not read the symbolic link from dentry `%s'",
337 return WIMLIB_ERR_INVALID_DENTRY;
339 ret = symlink(target, output_path);
341 ERROR_WITH_ERRNO("Failed to symlink `%s' to `%s'",
342 output_path, target);
343 return WIMLIB_ERR_LINK;
345 lte = inode_unnamed_lte_resolved(dentry->d_inode);
346 wimlib_assert(lte != NULL);
347 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
348 struct wimlib_unix_data unix_data;
349 ret = inode_get_unix_data(dentry->d_inode, &unix_data, NULL);
355 ret = symlink_apply_unix_data(output_path, &unix_data);
359 args->progress.extract.completed_bytes += wim_resource_size(lte);
363 #endif /* !__WIN32__ */
366 extract_directory(struct wim_dentry *dentry,
367 const tchar *output_path, bool is_root)
372 ret = tstat(output_path, &stbuf);
374 if (S_ISDIR(stbuf.st_mode)) {
376 /*WARNING("`%s' already exists", output_path);*/
379 ERROR("`%"TS"' is not a directory", output_path);
380 return WIMLIB_ERR_MKDIR;
383 if (errno != ENOENT) {
384 ERROR_WITH_ERRNO("Failed to stat `%"TS"'", output_path);
385 return WIMLIB_ERR_STAT;
389 if (tmkdir(output_path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH))
391 ERROR_WITH_ERRNO("Cannot create directory `%"TS"'", output_path);
392 return WIMLIB_ERR_MKDIR;
398 struct wimlib_unix_data unix_data;
399 ret = inode_get_unix_data(dentry->d_inode, &unix_data, NULL);
405 ret = dir_apply_unix_data(output_path, &unix_data);
412 static int unix_do_apply_dentry(const char *output_path,
413 size_t output_path_len,
414 struct wim_dentry *dentry,
415 struct apply_args *args)
417 const struct wim_inode *inode = dentry->d_inode;
419 if (inode_is_symlink(inode))
420 return extract_symlink(dentry, args, output_path);
421 else if (inode_is_directory(inode))
422 return extract_directory((args->extract_flags &
423 WIMLIB_EXTRACT_FLAG_UNIX_DATA) ? dentry : NULL,
426 return extract_regular_file(dentry, args, output_path);
430 unix_do_apply_dentry_timestamps(const char *output_path,
431 size_t output_path_len,
432 const struct wim_dentry *dentry,
433 struct apply_args *args)
436 const struct wim_inode *inode = dentry->d_inode;
438 #ifdef HAVE_UTIMENSAT
439 /* Convert the WIM timestamps, which are accurate to 100 nanoseconds,
440 * into `struct timespec's for passing to utimensat(), which is accurate
441 * to 1 nanosecond. */
443 struct timespec ts[2];
444 ts[0] = wim_timestamp_to_timespec(inode->i_last_access_time);
445 ts[1] = wim_timestamp_to_timespec(inode->i_last_write_time);
446 ret = utimensat(AT_FDCWD, output_path, ts, AT_SYMLINK_NOFOLLOW);
454 /* utimensat() not implemented or not available */
456 /* Convert the WIM timestamps, which are accurate to 100
457 * nanoseconds, into `struct timeval's for passing to lutimes(),
458 * which is accurate to 1 microsecond. */
459 struct timeval tv[2];
460 tv[0] = wim_timestamp_to_timeval(inode->i_last_access_time);
461 tv[1] = wim_timestamp_to_timeval(inode->i_last_write_time);
462 ret = lutimes(output_path, tv);
469 /* utimensat() and lutimes() both not implemented or not
472 /* Convert the WIM timestamps, which are accurate to 100
473 * nanoseconds, into a `struct utimbuf's for passing to
474 * utime(), which is accurate to 1 second. */
476 buf.actime = wim_timestamp_to_unix(inode->i_last_access_time);
477 buf.modtime = wim_timestamp_to_unix(inode->i_last_write_time);
478 ret = utime(output_path, &buf);
481 if (ret && args->num_utime_warnings < 10) {
482 WARNING_WITH_ERRNO("Failed to set timestamp on file `%s'",
484 args->num_utime_warnings++;
488 #endif /* !__WIN32__ */
490 /* Extracts a file, directory, or symbolic link from the WIM archive. */
492 apply_dentry_normal(struct wim_dentry *dentry, void *arg)
494 struct apply_args *args = arg;
498 len = tstrlen(args->target);
499 if (dentry_is_root(dentry)) {
500 output_path = (tchar*)args->target;
502 output_path = alloca(len * sizeof(tchar) + dentry->full_path_nbytes +
504 memcpy(output_path, args->target, len * sizeof(tchar));
505 memcpy(output_path + len, dentry->full_path, dentry->full_path_nbytes);
506 len += dentry->full_path_nbytes / sizeof(tchar);
507 output_path[len] = T('\0');
510 return win32_do_apply_dentry(output_path, len, dentry, args);
512 return unix_do_apply_dentry(output_path, len, dentry, args);
517 /* Apply timestamps to an extracted file or directory */
519 apply_dentry_timestamps_normal(struct wim_dentry *dentry, void *arg)
521 struct apply_args *args = arg;
525 len = tstrlen(args->target);
526 if (dentry_is_root(dentry)) {
527 output_path = (tchar*)args->target;
529 output_path = alloca(len * sizeof(tchar) + dentry->full_path_nbytes +
531 memcpy(output_path, args->target, len * sizeof(tchar));
532 memcpy(output_path + len, dentry->full_path, dentry->full_path_nbytes);
533 len += dentry->full_path_nbytes / sizeof(tchar);
534 output_path[len] = T('\0');
539 return win32_do_apply_dentry_timestamps(output_path, len, dentry, args);
541 return unix_do_apply_dentry_timestamps(output_path, len, dentry, args);
545 /* Extract a dentry if it hasn't already been extracted, and either the dentry
546 * has no streams or WIMLIB_EXTRACT_FLAG_NO_STREAMS is not specified. */
548 maybe_apply_dentry(struct wim_dentry *dentry, void *arg)
550 struct apply_args *args = arg;
553 if (dentry->is_extracted)
556 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_NO_STREAMS)
557 if (inode_unnamed_lte_resolved(dentry->d_inode))
560 if ((args->extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE) &&
561 args->progress_func) {
562 args->progress.extract.cur_path = dentry->full_path;
563 args->progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY,
566 ret = args->apply_dentry(dentry, args);
568 dentry->is_extracted = 1;
573 cmp_streams_by_wim_position(const void *p1, const void *p2)
575 const struct wim_lookup_table_entry *lte1, *lte2;
576 lte1 = *(const struct wim_lookup_table_entry**)p1;
577 lte2 = *(const struct wim_lookup_table_entry**)p2;
578 if (lte1->resource_entry.offset < lte2->resource_entry.offset)
580 else if (lte1->resource_entry.offset > lte2->resource_entry.offset)
587 sort_stream_list_by_wim_position(struct list_head *stream_list)
589 struct list_head *cur;
591 struct wim_lookup_table_entry **array;
596 list_for_each(cur, stream_list)
598 array_size = num_streams * sizeof(array[0]);
599 array = MALLOC(array_size);
601 ERROR("Failed to allocate %zu bytes to sort stream entries",
603 return WIMLIB_ERR_NOMEM;
605 cur = stream_list->next;
606 for (i = 0; i < num_streams; i++) {
607 array[i] = container_of(cur, struct wim_lookup_table_entry, staging_list);
611 qsort(array, num_streams, sizeof(array[0]), cmp_streams_by_wim_position);
613 INIT_LIST_HEAD(stream_list);
614 for (i = 0; i < num_streams; i++)
615 list_add_tail(&array[i]->staging_list, stream_list);
621 calculate_bytes_to_extract(struct list_head *stream_list,
623 union wimlib_progress_info *progress)
625 struct wim_lookup_table_entry *lte;
629 /* For each stream to be extracted... */
630 list_for_each_entry(lte, stream_list, staging_list) {
632 (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
634 /* In the symlink or hard link extraction mode, each
635 * stream will be extracted one time regardless of how
636 * many dentries share the stream. */
637 wimlib_assert(!(extract_flags & WIMLIB_EXTRACT_FLAG_NTFS));
638 if (!lte->extracted_file) {
640 total_bytes += wim_resource_size(lte);
643 num_streams += lte->out_refcnt;
644 total_bytes += lte->out_refcnt * wim_resource_size(lte);
647 progress->extract.num_streams = num_streams;
648 progress->extract.total_bytes = total_bytes;
649 progress->extract.completed_bytes = 0;
653 maybe_add_stream_for_extraction(struct wim_lookup_table_entry *lte,
654 struct list_head *stream_list)
656 if (++lte->out_refcnt == 1) {
657 INIT_LIST_HEAD(<e->inode_list);
658 list_add_tail(<e->staging_list, stream_list);
663 inode_find_streams_for_extraction(struct wim_inode *inode,
664 struct list_head *stream_list,
667 struct wim_lookup_table_entry *lte;
668 bool inode_added = false;
670 lte = inode_unnamed_lte_resolved(inode);
672 maybe_add_stream_for_extraction(lte, stream_list);
673 list_add_tail(&inode->i_lte_inode_list, <e->inode_list);
677 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
678 for (unsigned i = 0; i < inode->i_num_ads; i++) {
679 if (inode->i_ads_entries[i].stream_name_nbytes != 0) {
680 lte = inode->i_ads_entries[i].lte;
682 maybe_add_stream_for_extraction(lte,
685 list_add_tail(&inode->i_lte_inode_list,
697 find_streams_for_extraction(struct hlist_head *inode_list,
698 struct list_head *stream_list,
699 struct wim_lookup_table *lookup_table,
702 struct wim_inode *inode;
703 struct hlist_node *cur;
704 struct wim_dentry *dentry;
706 for_lookup_table_entry(lookup_table, lte_zero_out_refcnt, NULL);
707 INIT_LIST_HEAD(stream_list);
708 hlist_for_each_entry(inode, cur, inode_list, i_hlist) {
709 if (!inode->i_resolved)
710 inode_resolve_ltes(inode, lookup_table);
711 inode_for_each_dentry(dentry, inode)
712 dentry->is_extracted = 0;
713 inode_find_streams_for_extraction(inode, stream_list,
718 struct apply_operations {
719 int (*apply_dentry)(struct wim_dentry *dentry, void *arg);
720 int (*apply_dentry_timestamps)(struct wim_dentry *dentry, void *arg);
723 static const struct apply_operations normal_apply_operations = {
724 .apply_dentry = apply_dentry_normal,
725 .apply_dentry_timestamps = apply_dentry_timestamps_normal,
729 static const struct apply_operations ntfs_apply_operations = {
730 .apply_dentry = apply_dentry_ntfs,
731 .apply_dentry_timestamps = apply_dentry_timestamps_ntfs,
736 apply_stream_list(struct list_head *stream_list,
737 struct apply_args *args,
738 const struct apply_operations *ops,
739 wimlib_progress_func_t progress_func)
741 uint64_t bytes_per_progress = args->progress.extract.total_bytes / 100;
742 uint64_t next_progress = bytes_per_progress;
743 struct wim_lookup_table_entry *lte;
744 struct wim_inode *inode;
745 struct wim_dentry *dentry;
748 /* This complicated loop is essentially looping through the dentries,
749 * although dentries may be visited more than once (if a dentry contains
750 * two different nonempty streams) or not at all (if a dentry contains
751 * no non-empty streams).
753 * The outer loop is over the distinct streams to be extracted so that
754 * sequential reading of the WIM can be implemented. */
756 /* For each distinct stream to be extracted */
757 list_for_each_entry(lte, stream_list, staging_list) {
758 /* For each inode that contains the stream */
759 list_for_each_entry(inode, <e->inode_list, i_lte_inode_list) {
760 /* For each dentry that points to the inode */
761 inode_for_each_dentry(dentry, inode) {
762 /* Extract the dentry if it was not already
764 ret = maybe_apply_dentry(dentry, args);
768 args->progress.extract.completed_bytes >= next_progress)
770 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS,
772 if (args->progress.extract.completed_bytes >=
773 args->progress.extract.total_bytes)
775 next_progress = ~0ULL;
778 min (args->progress.extract.completed_bytes +
780 args->progress.extract.total_bytes);
789 /* Extracts the image @image from the WIM @w to the directory or NTFS volume
792 extract_single_image(WIMStruct *w, int image,
793 const tchar *target, int extract_flags,
794 wimlib_progress_func_t progress_func)
797 struct list_head stream_list;
798 struct hlist_head *inode_list;
800 struct apply_args args;
801 const struct apply_operations *ops;
804 args.target = target;
805 args.extract_flags = extract_flags;
806 args.num_utime_warnings = 0;
807 args.stream_list = &stream_list;
808 args.progress_func = progress_func;
811 args.progress.extract.wimfile_name = w->filename;
812 args.progress.extract.image = image;
813 args.progress.extract.extract_flags = (extract_flags &
814 WIMLIB_EXTRACT_MASK_PUBLIC);
815 args.progress.extract.image_name = wimlib_get_image_name(w, image);
816 args.progress.extract.target = target;
820 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
821 args.vol = ntfs_mount(target, 0);
823 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%"TS"'",
825 return WIMLIB_ERR_NTFS_3G;
827 ops = &ntfs_apply_operations;
830 ops = &normal_apply_operations;
832 ret = select_wim_image(w, image);
836 inode_list = &w->image_metadata[image - 1].inode_list;
838 /* Build a list of the streams that need to be extracted */
839 find_streams_for_extraction(inode_list, &stream_list,
840 w->lookup_table, extract_flags);
842 /* Calculate the number of bytes of data that will be extracted */
843 calculate_bytes_to_extract(&stream_list, extract_flags,
847 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN,
851 /* If a sequential extraction was specified, sort the streams to be
852 * extracted by their position in the WIM file, so that the WIM file can
853 * be read sequentially. */
854 if (extract_flags & WIMLIB_EXTRACT_FLAG_SEQUENTIAL) {
855 ret = sort_stream_list_by_wim_position(&stream_list);
857 WARNING("Falling back to non-sequential extraction");
858 extract_flags &= ~WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
863 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN,
867 /* Make the directory structure and extract empty files */
868 args.extract_flags |= WIMLIB_EXTRACT_FLAG_NO_STREAMS;
869 args.apply_dentry = ops->apply_dentry;
870 ret = for_dentry_in_tree(wim_root_dentry(w), maybe_apply_dentry, &args);
871 args.extract_flags &= ~WIMLIB_EXTRACT_FLAG_NO_STREAMS;
876 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_END,
880 /* Extract non-empty files */
881 ret = apply_stream_list(&stream_list, &args, ops, progress_func);
886 progress_func(WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS,
890 /* Apply timestamps */
891 ret = for_dentry_in_tree_depth(wim_root_dentry(w),
892 ops->apply_dentry_timestamps, &args);
897 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END,
902 /* Unmount the NTFS volume */
903 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
904 if (ntfs_umount(args.vol, FALSE) != 0) {
905 ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%"TS"'",
908 ret = WIMLIB_ERR_NTFS_3G;
915 static const tchar *filename_forbidden_chars =
924 /* This function checks if it is okay to use a WIM image's name as a directory
927 image_name_ok_as_dir(const tchar *image_name)
929 return image_name && *image_name &&
930 !tstrpbrk(image_name, filename_forbidden_chars);
933 /* Extracts all images from the WIM to the directory @target, with the images
934 * placed in subdirectories named by their image names. */
936 extract_all_images(WIMStruct *w,
939 wimlib_progress_func_t progress_func)
941 size_t image_name_max_len = max(xml_get_max_image_name_len(w), 20);
942 size_t output_path_len = tstrlen(target);
943 tchar buf[output_path_len + 1 + image_name_max_len + 1];
946 const tchar *image_name;
948 ret = extract_directory(NULL, target, true);
952 tmemcpy(buf, target, output_path_len);
953 buf[output_path_len] = T('/');
954 for (image = 1; image <= w->hdr.image_count; image++) {
955 image_name = wimlib_get_image_name(w, image);
956 if (image_name_ok_as_dir(image_name)) {
957 tstrcpy(buf + output_path_len + 1, image_name);
959 /* Image name is empty, or contains forbidden
961 tsprintf(buf + output_path_len + 1, T("%d"), image);
963 ret = extract_single_image(w, image, buf, extract_flags,
971 /* Extracts a single image or all images from a WIM file to a directory or NTFS
974 wimlib_extract_image(WIMStruct *w,
978 WIMStruct **additional_swms,
979 unsigned num_additional_swms,
980 wimlib_progress_func_t progress_func)
982 struct wim_lookup_table *joined_tab, *w_tab_save;
986 return WIMLIB_ERR_INVALID_PARAM;
988 extract_flags &= WIMLIB_EXTRACT_MASK_PUBLIC;
990 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
991 == (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
992 return WIMLIB_ERR_INVALID_PARAM;
995 if (extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
996 ERROR("Extracting UNIX data is not supported on Windows");
997 return WIMLIB_ERR_INVALID_PARAM;
999 if (extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK)) {
1000 ERROR("Linked extraction modes are not supported on Windows");
1001 return WIMLIB_ERR_INVALID_PARAM;
1005 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1007 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))) {
1008 ERROR("Cannot specify symlink or hardlink flags when applying\n"
1009 " directly to a NTFS volume");
1010 return WIMLIB_ERR_INVALID_PARAM;
1012 if (image == WIMLIB_ALL_IMAGES) {
1013 ERROR("Can only apply a single image when applying "
1014 "directly to a NTFS volume");
1015 return WIMLIB_ERR_INVALID_PARAM;
1017 if (extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
1018 ERROR("Cannot restore UNIX-specific data in the NTFS extraction mode");
1019 return WIMLIB_ERR_INVALID_PARAM;
1022 ERROR("wimlib was compiled without support for NTFS-3g, so");
1023 ERROR("we cannot apply a WIM image directly to a NTFS volume");
1024 return WIMLIB_ERR_UNSUPPORTED;
1028 ret = verify_swm_set(w, additional_swms, num_additional_swms);
1032 if (num_additional_swms) {
1033 ret = new_joined_lookup_table(w, additional_swms,
1034 num_additional_swms, &joined_tab);
1037 w_tab_save = w->lookup_table;
1038 w->lookup_table = joined_tab;
1041 if (image == WIMLIB_ALL_IMAGES) {
1042 extract_flags |= WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
1043 ret = extract_all_images(w, target, extract_flags,
1046 extract_flags &= ~WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
1047 ret = extract_single_image(w, image, target, extract_flags,
1051 if (extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
1052 WIMLIB_EXTRACT_FLAG_HARDLINK))
1054 for_lookup_table_entry(w->lookup_table,
1055 lte_free_extracted_file,
1059 if (num_additional_swms) {
1060 free_lookup_table(w->lookup_table);
1061 w->lookup_table = w_tab_save;