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 = get_num_path_components(dentry->_full_path) - 1;
115 num_output_dir_path_components = get_num_path_components(args->target);
117 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE) {
118 num_path_components++;
119 num_output_dir_path_components--;
121 extracted_file_len = strlen(lte->extracted_file);
123 char buf[extracted_file_len + 3 * num_path_components + 1];
126 for (i = 0; i < num_path_components; i++) {
131 p2 = lte->extracted_file;
134 while (num_output_dir_path_components > 0) {
135 p2 = path_next_part(p2);
136 num_output_dir_path_components--;
139 if (symlink(buf, output_path) != 0) {
140 ERROR_WITH_ERRNO("Failed to symlink `%s' to `%s'",
141 buf, lte->extracted_file);
142 return WIMLIB_ERR_LINK;
149 symlink_apply_unix_data(const char *link,
150 const struct wimlib_unix_data *unix_data)
152 if (lchown(link, unix_data->uid, unix_data->gid)) {
153 if (errno == EPERM) {
155 WARNING_WITH_ERRNO("failed to set symlink UNIX owner/group");
157 ERROR_WITH_ERRNO("failed to set symlink UNIX owner/group");
158 return WIMLIB_ERR_INVALID_DENTRY;
165 fd_apply_unix_data(int fd, const struct wimlib_unix_data *unix_data)
167 if (fchown(fd, unix_data->uid, unix_data->gid)) {
168 if (errno == EPERM) {
169 WARNING_WITH_ERRNO("failed to set file UNIX owner/group");
172 ERROR_WITH_ERRNO("failed to set file UNIX owner/group");
173 return WIMLIB_ERR_INVALID_DENTRY;
177 if (fchmod(fd, unix_data->mode)) {
178 if (errno == EPERM) {
179 WARNING_WITH_ERRNO("failed to set UNIX file mode");
182 ERROR_WITH_ERRNO("failed to set UNIX file mode");
183 return WIMLIB_ERR_INVALID_DENTRY;
190 dir_apply_unix_data(const char *dir, const struct wimlib_unix_data *unix_data)
192 int dfd = open(dir, O_RDONLY);
195 ret = fd_apply_unix_data(dfd, unix_data);
197 ERROR_WITH_ERRNO("can't close directory `%s'", dir);
198 ret = WIMLIB_ERR_MKDIR;
201 ERROR_WITH_ERRNO("can't open directory `%s'", dir);
202 ret = WIMLIB_ERR_MKDIR;
208 extract_regular_file_unlinked(struct wim_dentry *dentry,
209 struct apply_args *args,
210 const char *output_path,
211 struct wim_lookup_table_entry *lte)
213 /* Normal mode of extraction. Regular files and hard links are
214 * extracted in the way that they appear in the WIM. */
218 struct wim_inode *inode = dentry->d_inode;
220 if (!((args->extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE)
221 && (args->extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
222 WIMLIB_EXTRACT_FLAG_HARDLINK))))
224 /* If the dentry is part of a hard link set of at least 2
225 * dentries and one of the other dentries has already been
226 * extracted, make a hard link to the file corresponding to this
227 * already-extracted directory. Otherwise, extract the file and
228 * set the inode->i_extracted_file field so that other dentries
229 * in the hard link group can link to it. */
230 if (inode->i_nlink > 1) {
231 if (inode->i_extracted_file) {
232 DEBUG("Extracting hard link `%s' => `%s'",
233 output_path, inode->i_extracted_file);
234 if (link(inode->i_extracted_file, output_path) != 0) {
235 ERROR_WITH_ERRNO("Failed to hard link "
238 inode->i_extracted_file);
239 return WIMLIB_ERR_LINK;
243 FREE(inode->i_extracted_file);
244 inode->i_extracted_file = STRDUP(output_path);
245 if (!inode->i_extracted_file) {
246 ERROR("Failed to allocate memory for filename");
247 return WIMLIB_ERR_NOMEM;
252 /* Extract the contents of the file to @output_path. */
254 out_fd = open(output_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
256 ERROR_WITH_ERRNO("Failed to open the file `%s' for writing",
258 return WIMLIB_ERR_OPEN;
262 /* Empty file with no lookup table entry */
263 DEBUG("Empty file `%s'.", output_path);
265 goto out_extract_unix_data;
268 ret = extract_wim_resource_to_fd(lte, out_fd, wim_resource_size(lte));
270 ERROR("Failed to extract resource to `%s'", output_path);
274 out_extract_unix_data:
275 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
276 struct wimlib_unix_data unix_data;
277 ret = inode_get_unix_data(inode, &unix_data, NULL);
283 ret = fd_apply_unix_data(out_fd, &unix_data);
288 args->progress.extract.completed_bytes += wim_resource_size(lte);
290 if (close(out_fd) != 0) {
291 ERROR_WITH_ERRNO("Failed to close file `%s'", output_path);
293 ret = WIMLIB_ERR_WRITE;
299 extract_regular_file(struct wim_dentry *dentry,
300 struct apply_args *args,
301 const char *output_path)
303 struct wim_lookup_table_entry *lte;
304 const struct wim_inode *inode = dentry->d_inode;
306 lte = inode_unnamed_lte_resolved(inode);
308 if (lte && (args->extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
309 WIMLIB_EXTRACT_FLAG_HARDLINK)))
311 if (lte->extracted_file) {
312 return extract_regular_file_linked(dentry, output_path, args, lte);
314 lte->extracted_file = STRDUP(output_path);
315 if (!lte->extracted_file)
316 return WIMLIB_ERR_NOMEM;
319 return extract_regular_file_unlinked(dentry, args, output_path, lte);
323 extract_symlink(struct wim_dentry *dentry,
324 struct apply_args *args,
325 const char *output_path)
327 char target[4096 + args->target_realpath_len];
329 const struct wim_inode *inode = dentry->d_inode;
331 ssize_t ret = wim_inode_readlink(inode,
332 target + args->target_realpath_len,
333 sizeof(target) - args->target_realpath_len - 1);
334 struct wim_lookup_table_entry *lte;
337 ERROR("Could not read the symbolic link from dentry `%s'",
339 return WIMLIB_ERR_INVALID_DENTRY;
341 target[args->target_realpath_len + ret] = '\0';
342 if (target[args->target_realpath_len] == '/' &&
343 args->extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX)
345 /* Fix absolute symbolic link target to point into the actual
346 * extraction destination */
347 memcpy(target, args->target_realpath,
348 args->target_realpath_len);
349 fixed_target = target;
351 /* Keep same link target */
352 fixed_target = target + args->target_realpath_len;
354 ret = symlink(fixed_target, output_path);
356 ERROR_WITH_ERRNO("Failed to symlink `%s' to `%s'",
357 output_path, fixed_target);
358 return WIMLIB_ERR_LINK;
360 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
361 struct wimlib_unix_data unix_data;
362 ret = inode_get_unix_data(inode, &unix_data, NULL);
368 ret = symlink_apply_unix_data(output_path, &unix_data);
372 lte = inode_unnamed_lte_resolved(inode);
373 wimlib_assert(lte != NULL);
374 args->progress.extract.completed_bytes += wim_resource_size(lte);
378 #endif /* !__WIN32__ */
381 extract_directory(struct wim_dentry *dentry,
382 const tchar *output_path, bool is_root)
387 ret = tstat(output_path, &stbuf);
389 if (S_ISDIR(stbuf.st_mode)) {
391 /*WARNING("`%s' already exists", output_path);*/
394 ERROR("`%"TS"' is not a directory", output_path);
395 return WIMLIB_ERR_MKDIR;
398 if (errno != ENOENT) {
399 ERROR_WITH_ERRNO("Failed to stat `%"TS"'", output_path);
400 return WIMLIB_ERR_STAT;
404 if (tmkdir(output_path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH))
406 ERROR_WITH_ERRNO("Cannot create directory `%"TS"'", output_path);
407 return WIMLIB_ERR_MKDIR;
413 struct wimlib_unix_data unix_data;
414 ret = inode_get_unix_data(dentry->d_inode, &unix_data, NULL);
420 ret = dir_apply_unix_data(output_path, &unix_data);
428 unix_do_apply_dentry(const char *output_path, size_t output_path_len,
429 struct wim_dentry *dentry, struct apply_args *args)
431 const struct wim_inode *inode = dentry->d_inode;
433 if (inode_is_symlink(inode))
434 return extract_symlink(dentry, args, output_path);
435 else if (inode_is_directory(inode))
436 return extract_directory((args->extract_flags &
437 WIMLIB_EXTRACT_FLAG_UNIX_DATA) ? dentry : NULL,
440 return extract_regular_file(dentry, args, output_path);
444 unix_do_apply_dentry_timestamps(const char *output_path,
445 size_t output_path_len,
446 const struct wim_dentry *dentry,
447 struct apply_args *args)
450 const struct wim_inode *inode = dentry->d_inode;
452 #ifdef HAVE_UTIMENSAT
453 /* Convert the WIM timestamps, which are accurate to 100 nanoseconds,
454 * into `struct timespec's for passing to utimensat(), which is accurate
455 * to 1 nanosecond. */
457 struct timespec ts[2];
458 ts[0] = wim_timestamp_to_timespec(inode->i_last_access_time);
459 ts[1] = wim_timestamp_to_timespec(inode->i_last_write_time);
460 ret = utimensat(AT_FDCWD, output_path, ts, AT_SYMLINK_NOFOLLOW);
468 /* utimensat() not implemented or not available */
470 /* Convert the WIM timestamps, which are accurate to 100
471 * nanoseconds, into `struct timeval's for passing to lutimes(),
472 * which is accurate to 1 microsecond. */
473 struct timeval tv[2];
474 tv[0] = wim_timestamp_to_timeval(inode->i_last_access_time);
475 tv[1] = wim_timestamp_to_timeval(inode->i_last_write_time);
476 ret = lutimes(output_path, tv);
483 /* utimensat() and lutimes() both not implemented or not
486 /* Convert the WIM timestamps, which are accurate to 100
487 * nanoseconds, into a `struct utimbuf's for passing to
488 * utime(), which is accurate to 1 second. */
490 buf.actime = wim_timestamp_to_unix(inode->i_last_access_time);
491 buf.modtime = wim_timestamp_to_unix(inode->i_last_write_time);
492 ret = utime(output_path, &buf);
495 if (ret && args->num_utime_warnings < 10) {
496 WARNING_WITH_ERRNO("Failed to set timestamp on file `%s'",
498 args->num_utime_warnings++;
502 #endif /* !__WIN32__ */
504 /* Extracts a file, directory, or symbolic link from the WIM archive. */
506 apply_dentry_normal(struct wim_dentry *dentry, void *arg)
508 struct apply_args *args = arg;
512 len = tstrlen(args->target);
513 if (dentry_is_root(dentry)) {
514 output_path = (tchar*)args->target;
516 output_path = alloca(len * sizeof(tchar) + dentry->full_path_nbytes +
518 memcpy(output_path, args->target, len * sizeof(tchar));
519 memcpy(output_path + len, dentry->_full_path, dentry->full_path_nbytes);
520 len += dentry->full_path_nbytes / sizeof(tchar);
521 output_path[len] = T('\0');
524 return win32_do_apply_dentry(output_path, len, dentry, args);
526 return unix_do_apply_dentry(output_path, len, dentry, args);
531 /* Apply timestamps to an extracted file or directory */
533 apply_dentry_timestamps_normal(struct wim_dentry *dentry, void *arg)
535 struct apply_args *args = arg;
539 len = tstrlen(args->target);
540 if (dentry_is_root(dentry)) {
541 output_path = (tchar*)args->target;
543 output_path = alloca(len * sizeof(tchar) + dentry->full_path_nbytes +
545 memcpy(output_path, args->target, len * sizeof(tchar));
546 memcpy(output_path + len, dentry->_full_path, dentry->full_path_nbytes);
547 len += dentry->full_path_nbytes / sizeof(tchar);
548 output_path[len] = T('\0');
552 return win32_do_apply_dentry_timestamps(output_path, len, dentry, args);
554 return unix_do_apply_dentry_timestamps(output_path, len, dentry, args);
558 /* Extract a dentry if it hasn't already been extracted, and either the dentry
559 * has no streams or WIMLIB_EXTRACT_FLAG_NO_STREAMS is not specified. */
561 maybe_apply_dentry(struct wim_dentry *dentry, void *arg)
563 struct apply_args *args = arg;
566 if (dentry->is_extracted)
569 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_NO_STREAMS)
570 if (inode_unnamed_lte_resolved(dentry->d_inode))
573 if ((args->extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE) &&
574 args->progress_func) {
575 args->progress.extract.cur_path = dentry->_full_path;
576 args->progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY,
579 ret = args->apply_dentry(dentry, args);
581 dentry->is_extracted = 1;
586 calculate_bytes_to_extract(struct list_head *stream_list,
588 union wimlib_progress_info *progress)
590 struct wim_lookup_table_entry *lte;
594 /* For each stream to be extracted... */
595 list_for_each_entry(lte, stream_list, extraction_list) {
597 (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
599 /* In the symlink or hard link extraction mode, each
600 * stream will be extracted one time regardless of how
601 * many dentries share the stream. */
602 wimlib_assert(!(extract_flags & WIMLIB_EXTRACT_FLAG_NTFS));
603 if (!lte->extracted_file) {
605 total_bytes += wim_resource_size(lte);
608 num_streams += lte->out_refcnt;
609 total_bytes += lte->out_refcnt * wim_resource_size(lte);
612 progress->extract.num_streams = num_streams;
613 progress->extract.total_bytes = total_bytes;
614 progress->extract.completed_bytes = 0;
618 maybe_add_stream_for_extraction(struct wim_lookup_table_entry *lte,
619 struct list_head *stream_list)
621 if (++lte->out_refcnt == 1) {
622 INIT_LIST_HEAD(<e->inode_list);
623 list_add_tail(<e->extraction_list, stream_list);
628 inode_find_streams_for_extraction(struct wim_inode *inode,
629 struct list_head *stream_list,
632 struct wim_lookup_table_entry *lte;
633 bool inode_added = false;
635 lte = inode_unnamed_lte_resolved(inode);
637 maybe_add_stream_for_extraction(lte, stream_list);
638 list_add_tail(&inode->i_lte_inode_list, <e->inode_list);
642 /* Determine whether to include alternate data stream entries or not.
644 * UNIX: Include them if extracting using NTFS-3g.
646 * Windows: Include them undconditionally, although if the filesystem is
647 * not NTFS we won't actually be able to extract them. */
648 #if defined(WITH_NTFS_3G)
649 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS)
650 #elif defined(__WIN32__)
656 for (unsigned i = 0; i < inode->i_num_ads; i++) {
657 if (inode->i_ads_entries[i].stream_name_nbytes != 0) {
658 lte = inode->i_ads_entries[i].lte;
660 maybe_add_stream_for_extraction(lte,
663 list_add_tail(&inode->i_lte_inode_list,
674 find_streams_for_extraction(struct wim_image_metadata *imd,
675 struct list_head *stream_list,
676 struct wim_lookup_table *lookup_table,
679 struct wim_inode *inode;
680 struct wim_dentry *dentry;
682 for_lookup_table_entry(lookup_table, lte_zero_out_refcnt, NULL);
683 INIT_LIST_HEAD(stream_list);
684 image_for_each_inode(inode, imd) {
685 if (!inode->i_resolved)
686 inode_resolve_ltes(inode, lookup_table);
687 inode_for_each_dentry(dentry, inode)
688 dentry->is_extracted = 0;
689 inode_find_streams_for_extraction(inode, stream_list,
694 struct apply_operations {
695 int (*apply_dentry)(struct wim_dentry *dentry, void *arg);
696 int (*apply_dentry_timestamps)(struct wim_dentry *dentry, void *arg);
699 static const struct apply_operations normal_apply_operations = {
700 .apply_dentry = apply_dentry_normal,
701 .apply_dentry_timestamps = apply_dentry_timestamps_normal,
705 static const struct apply_operations ntfs_apply_operations = {
706 .apply_dentry = apply_dentry_ntfs,
707 .apply_dentry_timestamps = apply_dentry_timestamps_ntfs,
712 apply_stream_list(struct list_head *stream_list,
713 struct apply_args *args,
714 const struct apply_operations *ops,
715 wimlib_progress_func_t progress_func)
717 uint64_t bytes_per_progress = args->progress.extract.total_bytes / 100;
718 uint64_t next_progress = bytes_per_progress;
719 struct wim_lookup_table_entry *lte;
720 struct wim_inode *inode;
721 struct wim_dentry *dentry;
724 /* This complicated loop is essentially looping through the dentries,
725 * although dentries may be visited more than once (if a dentry contains
726 * two different nonempty streams) or not at all (if a dentry contains
727 * no non-empty streams).
729 * The outer loop is over the distinct streams to be extracted so that
730 * sequential reading of the WIM can be implemented. */
732 /* For each distinct stream to be extracted */
733 list_for_each_entry(lte, stream_list, extraction_list) {
734 /* For each inode that contains the stream */
735 list_for_each_entry(inode, <e->inode_list, i_lte_inode_list) {
736 /* For each dentry that points to the inode */
737 inode_for_each_dentry(dentry, inode) {
738 /* Extract the dentry if it was not already
740 ret = maybe_apply_dentry(dentry, args);
744 args->progress.extract.completed_bytes >= next_progress)
746 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS,
748 if (args->progress.extract.completed_bytes >=
749 args->progress.extract.total_bytes)
751 next_progress = ~0ULL;
754 min (args->progress.extract.completed_bytes +
756 args->progress.extract.total_bytes);
766 sort_stream_list_by_wim_position(struct list_head *stream_list)
768 struct list_head *cur;
770 struct wim_lookup_table_entry **array;
775 list_for_each(cur, stream_list)
777 array_size = num_streams * sizeof(array[0]);
778 array = MALLOC(array_size);
780 ERROR("Failed to allocate %zu bytes to sort stream entries",
782 return WIMLIB_ERR_NOMEM;
784 cur = stream_list->next;
785 for (i = 0; i < num_streams; i++) {
786 array[i] = container_of(cur, struct wim_lookup_table_entry, extraction_list);
790 qsort(array, num_streams, sizeof(array[0]), cmp_streams_by_wim_position);
792 INIT_LIST_HEAD(stream_list);
793 for (i = 0; i < num_streams; i++)
794 list_add_tail(&array[i]->extraction_list, stream_list);
800 /* Extracts the image @image from the WIM @w to the directory or NTFS volume
803 extract_single_image(WIMStruct *w, int image,
804 const tchar *target, int extract_flags,
805 wimlib_progress_func_t progress_func)
808 struct list_head stream_list;
810 struct apply_args args;
811 const struct apply_operations *ops;
813 memset(&args, 0, sizeof(args));
816 args.target = target;
817 args.extract_flags = extract_flags;
818 args.progress_func = progress_func;
821 args.progress.extract.wimfile_name = w->filename;
822 args.progress.extract.image = image;
823 args.progress.extract.extract_flags = (extract_flags &
824 WIMLIB_EXTRACT_MASK_PUBLIC);
825 args.progress.extract.image_name = wimlib_get_image_name(w, image);
826 args.progress.extract.target = target;
830 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
831 args.vol = ntfs_mount(target, 0);
833 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%"TS"'",
835 return WIMLIB_ERR_NTFS_3G;
837 ops = &ntfs_apply_operations;
840 ops = &normal_apply_operations;
842 ret = select_wim_image(w, image);
846 /* Build a list of the streams that need to be extracted */
847 find_streams_for_extraction(wim_get_current_image_metadata(w),
849 w->lookup_table, extract_flags);
851 /* Calculate the number of bytes of data that will be extracted */
852 calculate_bytes_to_extract(&stream_list, extract_flags,
856 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN,
860 /* If a sequential extraction was specified, sort the streams to be
861 * extracted by their position in the WIM file, so that the WIM file can
862 * be read sequentially. */
863 if (extract_flags & WIMLIB_EXTRACT_FLAG_SEQUENTIAL) {
864 ret = sort_stream_list_by_wim_position(&stream_list);
866 WARNING("Falling back to non-sequential extraction");
867 extract_flags &= ~WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
872 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN,
876 ret = calculate_dentry_tree_full_paths(wim_root_dentry(w));
880 /* Make the directory structure and extract empty files */
881 args.extract_flags |= WIMLIB_EXTRACT_FLAG_NO_STREAMS;
882 args.apply_dentry = ops->apply_dentry;
883 ret = for_dentry_in_tree(wim_root_dentry(w), maybe_apply_dentry, &args);
884 args.extract_flags &= ~WIMLIB_EXTRACT_FLAG_NO_STREAMS;
889 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_END,
893 if (extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX) {
894 args.target_realpath = realpath(target, NULL);
895 if (!args.target_realpath)
896 return WIMLIB_ERR_NOMEM;
897 args.target_realpath_len = tstrlen(args.target_realpath);
900 /* Extract non-empty files */
901 ret = apply_stream_list(&stream_list, &args, ops, progress_func);
903 goto out_free_target_realpath;
906 progress_func(WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS,
910 /* Apply timestamps */
911 ret = for_dentry_in_tree_depth(wim_root_dentry(w),
912 ops->apply_dentry_timestamps, &args);
914 goto out_free_target_realpath;
917 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END,
920 out_free_target_realpath:
921 FREE(args.target_realpath);
924 /* Unmount the NTFS volume */
925 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
926 if (ntfs_umount(args.vol, FALSE) != 0) {
927 ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%"TS"'",
930 ret = WIMLIB_ERR_NTFS_3G;
937 static const tchar * const filename_forbidden_chars =
946 /* This function checks if it is okay to use a WIM image's name as a directory
949 image_name_ok_as_dir(const tchar *image_name)
951 return image_name && *image_name &&
952 !tstrpbrk(image_name, filename_forbidden_chars);
955 /* Extracts all images from the WIM to the directory @target, with the images
956 * placed in subdirectories named by their image names. */
958 extract_all_images(WIMStruct *w,
961 wimlib_progress_func_t progress_func)
963 size_t image_name_max_len = max(xml_get_max_image_name_len(w), 20);
964 size_t output_path_len = tstrlen(target);
965 tchar buf[output_path_len + 1 + image_name_max_len + 1];
968 const tchar *image_name;
970 ret = extract_directory(NULL, target, true);
974 tmemcpy(buf, target, output_path_len);
975 buf[output_path_len] = T('/');
976 for (image = 1; image <= w->hdr.image_count; image++) {
977 image_name = wimlib_get_image_name(w, image);
978 if (image_name_ok_as_dir(image_name)) {
979 tstrcpy(buf + output_path_len + 1, image_name);
981 /* Image name is empty, or contains forbidden
983 tsprintf(buf + output_path_len + 1, T("%d"), image);
985 ret = extract_single_image(w, image, buf, extract_flags,
993 /* Extracts a single image or all images from a WIM file to a directory or NTFS
996 wimlib_extract_image(WIMStruct *w,
1000 WIMStruct **additional_swms,
1001 unsigned num_additional_swms,
1002 wimlib_progress_func_t progress_func)
1004 struct wim_lookup_table *joined_tab, *w_tab_save;
1008 return WIMLIB_ERR_INVALID_PARAM;
1010 extract_flags &= WIMLIB_EXTRACT_MASK_PUBLIC;
1012 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
1013 == (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
1014 return WIMLIB_ERR_INVALID_PARAM;
1017 if (extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
1018 ERROR("Extracting UNIX data is not supported on Windows");
1019 return WIMLIB_ERR_INVALID_PARAM;
1021 if (extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK)) {
1022 ERROR("Linked extraction modes are not supported on Windows");
1023 return WIMLIB_ERR_INVALID_PARAM;
1027 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1029 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))) {
1030 ERROR("Cannot specify symlink or hardlink flags when applying\n"
1031 " directly to a NTFS volume");
1032 return WIMLIB_ERR_INVALID_PARAM;
1034 if (image == WIMLIB_ALL_IMAGES) {
1035 ERROR("Can only apply a single image when applying "
1036 "directly to a NTFS volume");
1037 return WIMLIB_ERR_INVALID_PARAM;
1039 if (extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
1040 ERROR("Cannot restore UNIX-specific data in the NTFS extraction mode");
1041 return WIMLIB_ERR_INVALID_PARAM;
1044 ERROR("wimlib was compiled without support for NTFS-3g, so");
1045 ERROR("we cannot apply a WIM image directly to a NTFS volume");
1046 return WIMLIB_ERR_UNSUPPORTED;
1050 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_RPFIX |
1051 WIMLIB_EXTRACT_FLAG_RPFIX)) ==
1052 (WIMLIB_EXTRACT_FLAG_RPFIX | WIMLIB_EXTRACT_FLAG_NORPFIX))
1054 ERROR("Cannot specify RPFIX and NORPFIX flags at the same time!");
1055 return WIMLIB_ERR_INVALID_PARAM;
1058 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_RPFIX |
1059 WIMLIB_EXTRACT_FLAG_NORPFIX)) == 0)
1060 if (w->hdr.flags & WIM_HDR_FLAG_RP_FIX)
1061 extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
1063 ret = verify_swm_set(w, additional_swms, num_additional_swms);
1067 ret = wim_checksum_unhashed_streams(w);
1071 if (num_additional_swms) {
1072 ret = new_joined_lookup_table(w, additional_swms,
1073 num_additional_swms, &joined_tab);
1076 w_tab_save = w->lookup_table;
1077 w->lookup_table = joined_tab;
1080 if (image == WIMLIB_ALL_IMAGES) {
1081 extract_flags |= WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
1082 ret = extract_all_images(w, target, extract_flags,
1085 extract_flags &= ~WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
1086 ret = extract_single_image(w, image, target, extract_flags,
1090 if (extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
1091 WIMLIB_EXTRACT_FLAG_HARDLINK))
1093 for_lookup_table_entry(w->lookup_table,
1094 lte_free_extracted_file,
1098 if (num_additional_swms) {
1099 free_lookup_table(w->lookup_table);
1100 w->lookup_table = w_tab_save;