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 "
156 "owner/group on \"%s\"", link);
158 ERROR_WITH_ERRNO("failed to set symlink UNIX "
159 "owner/group on \"%s\"", link);
160 return WIMLIB_ERR_INVALID_DENTRY;
167 fd_apply_unix_data(int fd, const char *path,
168 const struct wimlib_unix_data *unix_data)
170 if (fchown(fd, unix_data->uid, unix_data->gid)) {
171 if (errno == EPERM) {
172 WARNING_WITH_ERRNO("failed to set file UNIX "
173 "owner/group on \"%s\"", path);
176 ERROR_WITH_ERRNO("failed to set file UNIX "
177 "owner/group on \"%s\"", path);
178 return WIMLIB_ERR_INVALID_DENTRY;
182 if (fchmod(fd, unix_data->mode)) {
183 if (errno == EPERM) {
184 WARNING_WITH_ERRNO("failed to set UNIX file mode "
188 ERROR_WITH_ERRNO("failed to set UNIX file mode "
190 return WIMLIB_ERR_INVALID_DENTRY;
197 dir_apply_unix_data(const char *dir, const struct wimlib_unix_data *unix_data)
199 int dfd = open(dir, O_RDONLY);
202 ret = fd_apply_unix_data(dfd, dir, unix_data);
204 ERROR_WITH_ERRNO("can't close directory `%s'", dir);
205 ret = WIMLIB_ERR_MKDIR;
208 ERROR_WITH_ERRNO("can't open directory `%s'", dir);
209 ret = WIMLIB_ERR_MKDIR;
215 extract_regular_file_unlinked(struct wim_dentry *dentry,
216 struct apply_args *args,
217 const char *output_path,
218 struct wim_lookup_table_entry *lte)
220 /* Normal mode of extraction. Regular files and hard links are
221 * extracted in the way that they appear in the WIM. */
225 struct wim_inode *inode = dentry->d_inode;
227 if (!((args->extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE)
228 && (args->extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
229 WIMLIB_EXTRACT_FLAG_HARDLINK))))
231 /* If the dentry is part of a hard link set of at least 2
232 * dentries and one of the other dentries has already been
233 * extracted, make a hard link to the file corresponding to this
234 * already-extracted directory. Otherwise, extract the file and
235 * set the inode->i_extracted_file field so that other dentries
236 * in the hard link group can link to it. */
237 if (inode->i_nlink > 1) {
238 if (inode->i_extracted_file) {
239 DEBUG("Extracting hard link `%s' => `%s'",
240 output_path, inode->i_extracted_file);
241 if (link(inode->i_extracted_file, output_path) != 0) {
242 ERROR_WITH_ERRNO("Failed to hard link "
245 inode->i_extracted_file);
246 return WIMLIB_ERR_LINK;
250 FREE(inode->i_extracted_file);
251 inode->i_extracted_file = STRDUP(output_path);
252 if (!inode->i_extracted_file) {
253 ERROR("Failed to allocate memory for filename");
254 return WIMLIB_ERR_NOMEM;
259 /* Extract the contents of the file to @output_path. */
261 out_fd = open(output_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
263 ERROR_WITH_ERRNO("Failed to open the file `%s' for writing",
265 return WIMLIB_ERR_OPEN;
269 /* Empty file with no lookup table entry */
270 DEBUG("Empty file `%s'.", output_path);
272 goto out_extract_unix_data;
275 ret = extract_wim_resource_to_fd(lte, out_fd, wim_resource_size(lte));
277 ERROR("Failed to extract resource to `%s'", output_path);
281 out_extract_unix_data:
282 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
283 struct wimlib_unix_data unix_data;
284 ret = inode_get_unix_data(inode, &unix_data, NULL);
290 ret = fd_apply_unix_data(out_fd, output_path, &unix_data);
295 args->progress.extract.completed_bytes += wim_resource_size(lte);
297 if (close(out_fd) != 0) {
298 ERROR_WITH_ERRNO("Failed to close file `%s'", output_path);
300 ret = WIMLIB_ERR_WRITE;
306 extract_regular_file(struct wim_dentry *dentry,
307 struct apply_args *args,
308 const char *output_path)
310 struct wim_lookup_table_entry *lte;
311 const struct wim_inode *inode = dentry->d_inode;
313 lte = inode_unnamed_lte_resolved(inode);
315 if (lte && (args->extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
316 WIMLIB_EXTRACT_FLAG_HARDLINK)))
318 if (lte->extracted_file) {
319 return extract_regular_file_linked(dentry, output_path, args, lte);
321 lte->extracted_file = STRDUP(output_path);
322 if (!lte->extracted_file)
323 return WIMLIB_ERR_NOMEM;
326 return extract_regular_file_unlinked(dentry, args, output_path, lte);
330 extract_symlink(struct wim_dentry *dentry,
331 struct apply_args *args,
332 const char *output_path)
334 char target[4096 + args->target_realpath_len];
336 const struct wim_inode *inode = dentry->d_inode;
338 ssize_t ret = wim_inode_readlink(inode,
339 target + args->target_realpath_len,
340 sizeof(target) - args->target_realpath_len - 1);
341 struct wim_lookup_table_entry *lte;
344 ERROR("Could not read the symbolic link from dentry `%s'",
346 return WIMLIB_ERR_INVALID_DENTRY;
348 target[args->target_realpath_len + ret] = '\0';
349 if (target[args->target_realpath_len] == '/' &&
350 args->extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX)
352 /* Fix absolute symbolic link target to point into the actual
353 * extraction destination */
354 memcpy(target, args->target_realpath,
355 args->target_realpath_len);
356 fixed_target = target;
358 /* Keep same link target */
359 fixed_target = target + args->target_realpath_len;
361 ret = symlink(fixed_target, output_path);
363 ERROR_WITH_ERRNO("Failed to symlink `%s' to `%s'",
364 output_path, fixed_target);
365 return WIMLIB_ERR_LINK;
367 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
368 struct wimlib_unix_data unix_data;
369 ret = inode_get_unix_data(inode, &unix_data, NULL);
375 ret = symlink_apply_unix_data(output_path, &unix_data);
379 lte = inode_unnamed_lte_resolved(inode);
380 wimlib_assert(lte != NULL);
381 args->progress.extract.completed_bytes += wim_resource_size(lte);
385 #endif /* !__WIN32__ */
388 extract_directory(struct wim_dentry *dentry,
389 const tchar *output_path, bool is_root)
394 ret = tstat(output_path, &stbuf);
396 if (S_ISDIR(stbuf.st_mode)) {
398 /*WARNING("`%s' already exists", output_path);*/
401 ERROR("`%"TS"' is not a directory", output_path);
402 return WIMLIB_ERR_MKDIR;
405 if (errno != ENOENT) {
406 ERROR_WITH_ERRNO("Failed to stat `%"TS"'", output_path);
407 return WIMLIB_ERR_STAT;
411 if (tmkdir(output_path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH))
413 ERROR_WITH_ERRNO("Cannot create directory `%"TS"'", output_path);
414 return WIMLIB_ERR_MKDIR;
420 struct wimlib_unix_data unix_data;
421 ret = inode_get_unix_data(dentry->d_inode, &unix_data, NULL);
427 ret = dir_apply_unix_data(output_path, &unix_data);
435 unix_do_apply_dentry(const char *output_path, size_t output_path_len,
436 struct wim_dentry *dentry, struct apply_args *args)
438 const struct wim_inode *inode = dentry->d_inode;
440 if (inode_is_symlink(inode))
441 return extract_symlink(dentry, args, output_path);
442 else if (inode_is_directory(inode))
443 return extract_directory((args->extract_flags &
444 WIMLIB_EXTRACT_FLAG_UNIX_DATA) ? dentry : NULL,
447 return extract_regular_file(dentry, args, output_path);
451 unix_do_apply_dentry_timestamps(const char *output_path,
452 size_t output_path_len,
453 const struct wim_dentry *dentry,
454 struct apply_args *args)
457 const struct wim_inode *inode = dentry->d_inode;
459 #ifdef HAVE_UTIMENSAT
460 /* Convert the WIM timestamps, which are accurate to 100 nanoseconds,
461 * into `struct timespec's for passing to utimensat(), which is accurate
462 * to 1 nanosecond. */
464 struct timespec ts[2];
465 ts[0] = wim_timestamp_to_timespec(inode->i_last_access_time);
466 ts[1] = wim_timestamp_to_timespec(inode->i_last_write_time);
467 ret = utimensat(AT_FDCWD, output_path, ts, AT_SYMLINK_NOFOLLOW);
475 /* utimensat() not implemented or not available */
477 /* Convert the WIM timestamps, which are accurate to 100
478 * nanoseconds, into `struct timeval's for passing to lutimes(),
479 * which is accurate to 1 microsecond. */
480 struct timeval tv[2];
481 tv[0] = wim_timestamp_to_timeval(inode->i_last_access_time);
482 tv[1] = wim_timestamp_to_timeval(inode->i_last_write_time);
483 ret = lutimes(output_path, tv);
490 /* utimensat() and lutimes() both not implemented or not
493 /* Convert the WIM timestamps, which are accurate to 100
494 * nanoseconds, into a `struct utimbuf's for passing to
495 * utime(), which is accurate to 1 second. */
497 buf.actime = wim_timestamp_to_unix(inode->i_last_access_time);
498 buf.modtime = wim_timestamp_to_unix(inode->i_last_write_time);
499 ret = utime(output_path, &buf);
502 if (ret && args->num_utime_warnings < 10) {
503 WARNING_WITH_ERRNO("Failed to set timestamp on file `%s'",
505 args->num_utime_warnings++;
509 #endif /* !__WIN32__ */
511 /* Extracts a file, directory, or symbolic link from the WIM archive. */
513 apply_dentry_normal(struct wim_dentry *dentry, void *arg)
515 struct apply_args *args = arg;
519 len = tstrlen(args->target);
520 if (dentry_is_root(dentry)) {
521 output_path = (tchar*)args->target;
523 output_path = alloca(len * sizeof(tchar) + dentry->full_path_nbytes +
525 memcpy(output_path, args->target, len * sizeof(tchar));
526 memcpy(output_path + len, dentry->_full_path, dentry->full_path_nbytes);
527 len += dentry->full_path_nbytes / sizeof(tchar);
528 output_path[len] = T('\0');
531 return win32_do_apply_dentry(output_path, len, dentry, args);
533 return unix_do_apply_dentry(output_path, len, dentry, args);
538 /* Apply timestamps to an extracted file or directory */
540 apply_dentry_timestamps_normal(struct wim_dentry *dentry, void *arg)
542 struct apply_args *args = arg;
546 len = tstrlen(args->target);
547 if (dentry_is_root(dentry)) {
548 output_path = (tchar*)args->target;
550 output_path = alloca(len * sizeof(tchar) + dentry->full_path_nbytes +
552 memcpy(output_path, args->target, len * sizeof(tchar));
553 memcpy(output_path + len, dentry->_full_path, dentry->full_path_nbytes);
554 len += dentry->full_path_nbytes / sizeof(tchar);
555 output_path[len] = T('\0');
559 return win32_do_apply_dentry_timestamps(output_path, len, dentry, args);
561 return unix_do_apply_dentry_timestamps(output_path, len, dentry, args);
565 /* Extract a dentry if it hasn't already been extracted and either
566 * WIMLIB_EXTRACT_FLAG_NO_STREAMS is not specified, or the dentry is a directory
567 * and/or has no unnamed stream. */
569 maybe_apply_dentry(struct wim_dentry *dentry, void *arg)
571 struct apply_args *args = arg;
574 if (dentry->is_extracted)
577 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_NO_STREAMS &&
578 !dentry_is_directory(dentry) &&
579 inode_unnamed_lte_resolved(dentry->d_inode) != NULL)
582 if ((args->extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE) &&
583 args->progress_func) {
584 args->progress.extract.cur_path = dentry->_full_path;
585 args->progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY,
588 ret = args->apply_dentry(dentry, args);
590 dentry->is_extracted = 1;
595 calculate_bytes_to_extract(struct list_head *stream_list,
597 union wimlib_progress_info *progress)
599 struct wim_lookup_table_entry *lte;
603 /* For each stream to be extracted... */
604 list_for_each_entry(lte, stream_list, extraction_list) {
606 (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
608 /* In the symlink or hard link extraction mode, each
609 * stream will be extracted one time regardless of how
610 * many dentries share the stream. */
611 wimlib_assert(!(extract_flags & WIMLIB_EXTRACT_FLAG_NTFS));
612 if (!lte->extracted_file) {
614 total_bytes += wim_resource_size(lte);
617 num_streams += lte->out_refcnt;
618 total_bytes += lte->out_refcnt * wim_resource_size(lte);
621 progress->extract.num_streams = num_streams;
622 progress->extract.total_bytes = total_bytes;
623 progress->extract.completed_bytes = 0;
627 maybe_add_stream_for_extraction(struct wim_lookup_table_entry *lte,
628 struct list_head *stream_list)
630 if (++lte->out_refcnt == 1) {
631 INIT_LIST_HEAD(<e->inode_list);
632 list_add_tail(<e->extraction_list, stream_list);
637 inode_find_streams_for_extraction(struct wim_inode *inode,
638 struct list_head *stream_list,
641 struct wim_lookup_table_entry *lte;
642 bool inode_added = false;
644 lte = inode_unnamed_lte_resolved(inode);
646 maybe_add_stream_for_extraction(lte, stream_list);
647 list_add_tail(&inode->i_lte_inode_list, <e->inode_list);
651 /* Determine whether to include alternate data stream entries or not.
653 * UNIX: Include them if extracting using NTFS-3g.
655 * Windows: Include them undconditionally, although if the filesystem is
656 * not NTFS we won't actually be able to extract them. */
657 #if defined(WITH_NTFS_3G)
658 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS)
659 #elif defined(__WIN32__)
665 for (unsigned i = 0; i < inode->i_num_ads; i++) {
666 if (inode->i_ads_entries[i].stream_name_nbytes != 0) {
667 lte = inode->i_ads_entries[i].lte;
669 maybe_add_stream_for_extraction(lte,
672 list_add_tail(&inode->i_lte_inode_list,
683 find_streams_for_extraction(struct wim_image_metadata *imd,
684 struct list_head *stream_list,
685 struct wim_lookup_table *lookup_table,
688 struct wim_inode *inode;
689 struct wim_dentry *dentry;
691 for_lookup_table_entry(lookup_table, lte_zero_out_refcnt, NULL);
692 INIT_LIST_HEAD(stream_list);
693 image_for_each_inode(inode, imd) {
694 if (!inode->i_resolved)
695 inode_resolve_ltes(inode, lookup_table);
696 inode_for_each_dentry(dentry, inode)
697 dentry->is_extracted = 0;
698 inode_find_streams_for_extraction(inode, stream_list,
703 struct apply_operations {
704 int (*apply_dentry)(struct wim_dentry *dentry, void *arg);
705 int (*apply_dentry_timestamps)(struct wim_dentry *dentry, void *arg);
708 static const struct apply_operations normal_apply_operations = {
709 .apply_dentry = apply_dentry_normal,
710 .apply_dentry_timestamps = apply_dentry_timestamps_normal,
714 static const struct apply_operations ntfs_apply_operations = {
715 .apply_dentry = apply_dentry_ntfs,
716 .apply_dentry_timestamps = apply_dentry_timestamps_ntfs,
721 apply_stream_list(struct list_head *stream_list,
722 struct apply_args *args,
723 const struct apply_operations *ops,
724 wimlib_progress_func_t progress_func)
726 uint64_t bytes_per_progress = args->progress.extract.total_bytes / 100;
727 uint64_t next_progress = bytes_per_progress;
728 struct wim_lookup_table_entry *lte;
729 struct wim_inode *inode;
730 struct wim_dentry *dentry;
733 /* This complicated loop is essentially looping through the dentries,
734 * although dentries may be visited more than once (if a dentry contains
735 * two different nonempty streams) or not at all (if a dentry contains
736 * no non-empty streams).
738 * The outer loop is over the distinct streams to be extracted so that
739 * sequential reading of the WIM can be implemented. */
741 /* For each distinct stream to be extracted */
742 list_for_each_entry(lte, stream_list, extraction_list) {
743 /* For each inode that contains the stream */
744 list_for_each_entry(inode, <e->inode_list, i_lte_inode_list) {
745 /* For each dentry that points to the inode */
746 inode_for_each_dentry(dentry, inode) {
747 /* Extract the dentry if it was not already
749 ret = maybe_apply_dentry(dentry, args);
753 args->progress.extract.completed_bytes >= next_progress)
755 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS,
757 if (args->progress.extract.completed_bytes >=
758 args->progress.extract.total_bytes)
760 next_progress = ~0ULL;
763 min (args->progress.extract.completed_bytes +
765 args->progress.extract.total_bytes);
775 sort_stream_list_by_wim_position(struct list_head *stream_list)
777 struct list_head *cur;
779 struct wim_lookup_table_entry **array;
784 list_for_each(cur, stream_list)
786 array_size = num_streams * sizeof(array[0]);
787 array = MALLOC(array_size);
789 ERROR("Failed to allocate %zu bytes to sort stream entries",
791 return WIMLIB_ERR_NOMEM;
793 cur = stream_list->next;
794 for (i = 0; i < num_streams; i++) {
795 array[i] = container_of(cur, struct wim_lookup_table_entry, extraction_list);
799 qsort(array, num_streams, sizeof(array[0]), cmp_streams_by_wim_position);
801 INIT_LIST_HEAD(stream_list);
802 for (i = 0; i < num_streams; i++)
803 list_add_tail(&array[i]->extraction_list, stream_list);
809 /* Extracts the image @image from the WIM @w to the directory or NTFS volume
812 extract_single_image(WIMStruct *w, int image,
813 const tchar *target, int extract_flags,
814 wimlib_progress_func_t progress_func)
817 struct list_head stream_list;
819 struct apply_args args;
820 const struct apply_operations *ops;
822 memset(&args, 0, sizeof(args));
825 args.target = target;
826 args.extract_flags = extract_flags;
827 args.progress_func = progress_func;
830 args.progress.extract.wimfile_name = w->filename;
831 args.progress.extract.image = image;
832 args.progress.extract.extract_flags = (extract_flags &
833 WIMLIB_EXTRACT_MASK_PUBLIC);
834 args.progress.extract.image_name = wimlib_get_image_name(w, image);
835 args.progress.extract.target = target;
839 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
840 args.vol = ntfs_mount(target, 0);
842 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%"TS"'",
844 return WIMLIB_ERR_NTFS_3G;
846 ops = &ntfs_apply_operations;
849 ops = &normal_apply_operations;
851 ret = select_wim_image(w, image);
855 /* Build a list of the streams that need to be extracted */
856 find_streams_for_extraction(wim_get_current_image_metadata(w),
858 w->lookup_table, extract_flags);
860 /* Calculate the number of bytes of data that will be extracted */
861 calculate_bytes_to_extract(&stream_list, extract_flags,
865 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN,
869 /* If a sequential extraction was specified, sort the streams to be
870 * extracted by their position in the WIM file, so that the WIM file can
871 * be read sequentially. */
872 if (extract_flags & WIMLIB_EXTRACT_FLAG_SEQUENTIAL) {
873 ret = sort_stream_list_by_wim_position(&stream_list);
875 WARNING("Falling back to non-sequential extraction");
876 extract_flags &= ~WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
881 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN,
885 ret = calculate_dentry_tree_full_paths(wim_root_dentry(w));
889 /* Make the directory structure and extract empty files */
890 args.extract_flags |= WIMLIB_EXTRACT_FLAG_NO_STREAMS;
891 args.apply_dentry = ops->apply_dentry;
892 ret = for_dentry_in_tree(wim_root_dentry(w), maybe_apply_dentry, &args);
893 args.extract_flags &= ~WIMLIB_EXTRACT_FLAG_NO_STREAMS;
898 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_END,
902 if (extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX) {
903 args.target_realpath = realpath(target, NULL);
904 if (!args.target_realpath)
905 return WIMLIB_ERR_NOMEM;
906 args.target_realpath_len = tstrlen(args.target_realpath);
909 /* Extract non-empty files */
910 ret = apply_stream_list(&stream_list, &args, ops, progress_func);
912 goto out_free_target_realpath;
915 progress_func(WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS,
919 /* Apply timestamps */
920 ret = for_dentry_in_tree_depth(wim_root_dentry(w),
921 ops->apply_dentry_timestamps, &args);
923 goto out_free_target_realpath;
926 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END,
929 out_free_target_realpath:
930 FREE(args.target_realpath);
933 /* Unmount the NTFS volume */
934 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
935 if (ntfs_umount(args.vol, FALSE) != 0) {
936 ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%"TS"'",
939 ret = WIMLIB_ERR_NTFS_3G;
946 static const tchar * const filename_forbidden_chars =
955 /* This function checks if it is okay to use a WIM image's name as a directory
958 image_name_ok_as_dir(const tchar *image_name)
960 return image_name && *image_name &&
961 !tstrpbrk(image_name, filename_forbidden_chars);
964 /* Extracts all images from the WIM to the directory @target, with the images
965 * placed in subdirectories named by their image names. */
967 extract_all_images(WIMStruct *w,
970 wimlib_progress_func_t progress_func)
972 size_t image_name_max_len = max(xml_get_max_image_name_len(w), 20);
973 size_t output_path_len = tstrlen(target);
974 tchar buf[output_path_len + 1 + image_name_max_len + 1];
977 const tchar *image_name;
979 ret = extract_directory(NULL, target, true);
983 tmemcpy(buf, target, output_path_len);
984 buf[output_path_len] = T('/');
985 for (image = 1; image <= w->hdr.image_count; image++) {
986 image_name = wimlib_get_image_name(w, image);
987 if (image_name_ok_as_dir(image_name)) {
988 tstrcpy(buf + output_path_len + 1, image_name);
990 /* Image name is empty, or contains forbidden
992 tsprintf(buf + output_path_len + 1, T("%d"), image);
994 ret = extract_single_image(w, image, buf, extract_flags,
1002 /* Extracts a single image or all images from a WIM file to a directory or NTFS
1005 wimlib_extract_image(WIMStruct *w,
1007 const tchar *target,
1009 WIMStruct **additional_swms,
1010 unsigned num_additional_swms,
1011 wimlib_progress_func_t progress_func)
1013 struct wim_lookup_table *joined_tab, *w_tab_save;
1017 return WIMLIB_ERR_INVALID_PARAM;
1019 extract_flags &= WIMLIB_EXTRACT_MASK_PUBLIC;
1021 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
1022 == (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
1023 return WIMLIB_ERR_INVALID_PARAM;
1026 if (extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
1027 ERROR("Extracting UNIX data is not supported on Windows");
1028 return WIMLIB_ERR_INVALID_PARAM;
1030 if (extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK)) {
1031 ERROR("Linked extraction modes are not supported on Windows");
1032 return WIMLIB_ERR_INVALID_PARAM;
1036 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1038 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))) {
1039 ERROR("Cannot specify symlink or hardlink flags when applying\n"
1040 " directly to a NTFS volume");
1041 return WIMLIB_ERR_INVALID_PARAM;
1043 if (image == WIMLIB_ALL_IMAGES) {
1044 ERROR("Can only apply a single image when applying "
1045 "directly to a NTFS volume");
1046 return WIMLIB_ERR_INVALID_PARAM;
1048 if (extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
1049 ERROR("Cannot restore UNIX-specific data in the NTFS extraction mode");
1050 return WIMLIB_ERR_INVALID_PARAM;
1053 ERROR("wimlib was compiled without support for NTFS-3g, so");
1054 ERROR("we cannot apply a WIM image directly to a NTFS volume");
1055 return WIMLIB_ERR_UNSUPPORTED;
1059 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_RPFIX |
1060 WIMLIB_EXTRACT_FLAG_RPFIX)) ==
1061 (WIMLIB_EXTRACT_FLAG_RPFIX | WIMLIB_EXTRACT_FLAG_NORPFIX))
1063 ERROR("Cannot specify RPFIX and NORPFIX flags at the same time!");
1064 return WIMLIB_ERR_INVALID_PARAM;
1067 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_RPFIX |
1068 WIMLIB_EXTRACT_FLAG_NORPFIX)) == 0)
1069 if (w->hdr.flags & WIM_HDR_FLAG_RP_FIX)
1070 extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
1072 ret = verify_swm_set(w, additional_swms, num_additional_swms);
1076 ret = wim_checksum_unhashed_streams(w);
1080 if (num_additional_swms) {
1081 ret = new_joined_lookup_table(w, additional_swms,
1082 num_additional_swms, &joined_tab);
1085 w_tab_save = w->lookup_table;
1086 w->lookup_table = joined_tab;
1089 if (image == WIMLIB_ALL_IMAGES) {
1090 extract_flags |= WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
1091 ret = extract_all_images(w, target, extract_flags,
1094 extract_flags &= ~WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
1095 ret = extract_single_image(w, image, target, extract_flags,
1099 if (extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
1100 WIMLIB_EXTRACT_FLAG_HARDLINK))
1102 for_lookup_table_entry(w->lookup_table,
1103 lte_free_extracted_file,
1107 if (num_additional_swms) {
1108 free_lookup_table(w->lookup_table);
1109 w->lookup_table = w_tab_save;