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>
63 /* Returns the number of components of @path. */
65 get_num_path_components(const char *path)
67 unsigned num_components = 0;
73 while (*path && *path != '/')
76 return num_components;
80 path_next_part(const char *path)
82 while (*path && *path != '/')
84 while (*path && *path == '/')
90 extract_regular_file_linked(struct wim_dentry *dentry,
91 const char *output_path,
92 struct apply_args *args,
93 struct wim_lookup_table_entry *lte)
95 /* This mode overrides the normal hard-link extraction and
96 * instead either symlinks or hardlinks *all* identical files in
97 * the WIM, even if they are in a different image (in the case
98 * of a multi-image extraction) */
100 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_HARDLINK) {
101 if (link(lte->extracted_file, output_path) != 0) {
102 ERROR_WITH_ERRNO("Failed to hard link "
104 output_path, lte->extracted_file);
105 return WIMLIB_ERR_LINK;
108 int num_path_components;
109 int num_output_dir_path_components;
110 size_t extracted_file_len;
115 num_path_components = get_num_path_components(dentry->_full_path) - 1;
116 num_output_dir_path_components = get_num_path_components(args->target);
118 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE) {
119 num_path_components++;
120 num_output_dir_path_components--;
122 extracted_file_len = strlen(lte->extracted_file);
124 char buf[extracted_file_len + 3 * num_path_components + 1];
127 for (i = 0; i < num_path_components; i++) {
132 p2 = lte->extracted_file;
135 while (num_output_dir_path_components > 0) {
136 p2 = path_next_part(p2);
137 num_output_dir_path_components--;
140 if (symlink(buf, output_path) != 0) {
141 ERROR_WITH_ERRNO("Failed to symlink `%s' to `%s'",
142 buf, lte->extracted_file);
143 return WIMLIB_ERR_LINK;
150 symlink_apply_unix_data(const char *link,
151 const struct wimlib_unix_data *unix_data)
153 if (lchown(link, unix_data->uid, unix_data->gid)) {
154 if (errno == EPERM) {
156 WARNING_WITH_ERRNO("failed to set symlink UNIX "
157 "owner/group on \"%s\"", link);
159 ERROR_WITH_ERRNO("failed to set symlink UNIX "
160 "owner/group on \"%s\"", link);
161 return WIMLIB_ERR_INVALID_DENTRY;
168 fd_apply_unix_data(int fd, const char *path,
169 const struct wimlib_unix_data *unix_data)
171 if (fchown(fd, unix_data->uid, unix_data->gid)) {
172 if (errno == EPERM) {
173 WARNING_WITH_ERRNO("failed to set file UNIX "
174 "owner/group on \"%s\"", path);
177 ERROR_WITH_ERRNO("failed to set file UNIX "
178 "owner/group on \"%s\"", path);
179 return WIMLIB_ERR_INVALID_DENTRY;
183 if (fchmod(fd, unix_data->mode)) {
184 if (errno == EPERM) {
185 WARNING_WITH_ERRNO("failed to set UNIX file mode "
189 ERROR_WITH_ERRNO("failed to set UNIX file mode "
191 return WIMLIB_ERR_INVALID_DENTRY;
198 dir_apply_unix_data(const char *dir, const struct wimlib_unix_data *unix_data)
200 int dfd = open(dir, O_RDONLY);
203 ret = fd_apply_unix_data(dfd, dir, unix_data);
205 ERROR_WITH_ERRNO("can't close directory `%s'", dir);
206 ret = WIMLIB_ERR_MKDIR;
209 ERROR_WITH_ERRNO("can't open directory `%s'", dir);
210 ret = WIMLIB_ERR_MKDIR;
216 extract_regular_file_unlinked(struct wim_dentry *dentry,
217 struct apply_args *args,
218 const char *output_path,
219 struct wim_lookup_table_entry *lte)
221 /* Normal mode of extraction. Regular files and hard links are
222 * extracted in the way that they appear in the WIM. */
226 struct wim_inode *inode = dentry->d_inode;
228 if (!((args->extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE)
229 && (args->extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
230 WIMLIB_EXTRACT_FLAG_HARDLINK))))
232 /* If the dentry is part of a hard link set of at least 2
233 * dentries and one of the other dentries has already been
234 * extracted, make a hard link to the file corresponding to this
235 * already-extracted directory. Otherwise, extract the file and
236 * set the inode->i_extracted_file field so that other dentries
237 * in the hard link group can link to it. */
238 if (inode->i_nlink > 1) {
239 if (inode->i_extracted_file) {
240 DEBUG("Extracting hard link `%s' => `%s'",
241 output_path, inode->i_extracted_file);
242 if (link(inode->i_extracted_file, output_path) != 0) {
243 ERROR_WITH_ERRNO("Failed to hard link "
246 inode->i_extracted_file);
247 return WIMLIB_ERR_LINK;
251 FREE(inode->i_extracted_file);
252 inode->i_extracted_file = STRDUP(output_path);
253 if (!inode->i_extracted_file) {
254 ERROR("Failed to allocate memory for filename");
255 return WIMLIB_ERR_NOMEM;
260 /* Extract the contents of the file to @output_path. */
262 out_fd = open(output_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
264 ERROR_WITH_ERRNO("Failed to open the file `%s' for writing",
266 return WIMLIB_ERR_OPEN;
270 /* Empty file with no lookup table entry */
271 DEBUG("Empty file `%s'.", output_path);
273 goto out_extract_unix_data;
276 ret = extract_wim_resource_to_fd(lte, out_fd, wim_resource_size(lte));
278 ERROR("Failed to extract resource to `%s'", output_path);
282 out_extract_unix_data:
283 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
284 struct wimlib_unix_data unix_data;
285 ret = inode_get_unix_data(inode, &unix_data, NULL);
291 ret = fd_apply_unix_data(out_fd, output_path, &unix_data);
296 args->progress.extract.completed_bytes += wim_resource_size(lte);
298 if (close(out_fd) != 0) {
299 ERROR_WITH_ERRNO("Failed to close file `%s'", output_path);
301 ret = WIMLIB_ERR_WRITE;
307 extract_regular_file(struct wim_dentry *dentry,
308 struct apply_args *args,
309 const char *output_path)
311 struct wim_lookup_table_entry *lte;
312 const struct wim_inode *inode = dentry->d_inode;
314 lte = inode_unnamed_lte_resolved(inode);
316 if (lte && (args->extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
317 WIMLIB_EXTRACT_FLAG_HARDLINK)))
319 if (lte->extracted_file) {
320 return extract_regular_file_linked(dentry, output_path, args, lte);
322 lte->extracted_file = STRDUP(output_path);
323 if (!lte->extracted_file)
324 return WIMLIB_ERR_NOMEM;
327 return extract_regular_file_unlinked(dentry, args, output_path, lte);
331 extract_symlink(struct wim_dentry *dentry,
332 struct apply_args *args,
333 const char *output_path)
335 char target[4096 + args->target_realpath_len];
337 const struct wim_inode *inode = dentry->d_inode;
339 ssize_t ret = wim_inode_readlink(inode,
340 target + args->target_realpath_len,
341 sizeof(target) - args->target_realpath_len - 1);
342 struct wim_lookup_table_entry *lte;
345 ERROR("Could not read the symbolic link from dentry `%s'",
347 return WIMLIB_ERR_INVALID_DENTRY;
349 target[args->target_realpath_len + ret] = '\0';
350 if (target[args->target_realpath_len] == '/' &&
351 args->extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX)
353 /* Fix absolute symbolic link target to point into the actual
354 * extraction destination */
355 memcpy(target, args->target_realpath,
356 args->target_realpath_len);
357 fixed_target = target;
359 /* Keep same link target */
360 fixed_target = target + args->target_realpath_len;
362 ret = symlink(fixed_target, output_path);
364 ERROR_WITH_ERRNO("Failed to symlink `%s' to `%s'",
365 output_path, fixed_target);
366 return WIMLIB_ERR_LINK;
368 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
369 struct wimlib_unix_data unix_data;
370 ret = inode_get_unix_data(inode, &unix_data, NULL);
376 ret = symlink_apply_unix_data(output_path, &unix_data);
380 lte = inode_unnamed_lte_resolved(inode);
381 wimlib_assert(lte != NULL);
382 args->progress.extract.completed_bytes += wim_resource_size(lte);
386 #endif /* !__WIN32__ */
389 extract_directory(struct wim_dentry *dentry,
390 const tchar *output_path, bool is_root)
395 ret = tstat(output_path, &stbuf);
397 if (S_ISDIR(stbuf.st_mode)) {
399 /*WARNING("`%s' already exists", output_path);*/
402 ERROR("`%"TS"' is not a directory", output_path);
403 return WIMLIB_ERR_MKDIR;
406 if (errno != ENOENT) {
407 ERROR_WITH_ERRNO("Failed to stat `%"TS"'", output_path);
408 return WIMLIB_ERR_STAT;
412 if (tmkdir(output_path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH))
414 ERROR_WITH_ERRNO("Cannot create directory `%"TS"'", output_path);
415 return WIMLIB_ERR_MKDIR;
421 struct wimlib_unix_data unix_data;
422 ret = inode_get_unix_data(dentry->d_inode, &unix_data, NULL);
428 ret = dir_apply_unix_data(output_path, &unix_data);
436 unix_do_apply_dentry(const char *output_path, size_t output_path_len,
437 struct wim_dentry *dentry, struct apply_args *args)
439 const struct wim_inode *inode = dentry->d_inode;
441 if (inode_is_symlink(inode))
442 return extract_symlink(dentry, args, output_path);
443 else if (inode_is_directory(inode))
444 return extract_directory((args->extract_flags &
445 WIMLIB_EXTRACT_FLAG_UNIX_DATA) ? dentry : NULL,
448 return extract_regular_file(dentry, args, output_path);
452 unix_do_apply_dentry_timestamps(const char *output_path,
453 size_t output_path_len,
454 struct wim_dentry *dentry,
455 struct apply_args *args)
458 const struct wim_inode *inode = dentry->d_inode;
460 #ifdef HAVE_UTIMENSAT
461 /* Convert the WIM timestamps, which are accurate to 100 nanoseconds,
462 * into `struct timespec's for passing to utimensat(), which is accurate
463 * to 1 nanosecond. */
465 struct timespec ts[2];
466 ts[0] = wim_timestamp_to_timespec(inode->i_last_access_time);
467 ts[1] = wim_timestamp_to_timespec(inode->i_last_write_time);
468 ret = utimensat(AT_FDCWD, output_path, ts, AT_SYMLINK_NOFOLLOW);
476 /* utimensat() not implemented or not available */
478 /* Convert the WIM timestamps, which are accurate to 100
479 * nanoseconds, into `struct timeval's for passing to lutimes(),
480 * which is accurate to 1 microsecond. */
481 struct timeval tv[2];
482 tv[0] = wim_timestamp_to_timeval(inode->i_last_access_time);
483 tv[1] = wim_timestamp_to_timeval(inode->i_last_write_time);
484 ret = lutimes(output_path, tv);
491 /* utimensat() and lutimes() both not implemented or not
494 /* Convert the WIM timestamps, which are accurate to 100
495 * nanoseconds, into a `struct utimbuf's for passing to
496 * utime(), which is accurate to 1 second. */
498 buf.actime = wim_timestamp_to_unix(inode->i_last_access_time);
499 buf.modtime = wim_timestamp_to_unix(inode->i_last_write_time);
500 ret = utime(output_path, &buf);
503 if (ret && args->num_utime_warnings < 10) {
504 WARNING_WITH_ERRNO("Failed to set timestamp on file `%s'",
506 args->num_utime_warnings++;
510 #endif /* !__WIN32__ */
513 do_apply_op(struct wim_dentry *dentry, struct apply_args *args,
514 int (*apply_dentry_func)(const tchar *, size_t,
515 struct wim_dentry *, struct apply_args *))
518 const tchar *full_path;
519 size_t full_path_nchars;
521 wimlib_assert(dentry->_full_path != NULL);
522 full_path = dentry->_full_path + 1;
523 full_path_nchars = dentry->full_path_nbytes / sizeof(tchar) - 1;
524 tchar output_path[args->target_nchars + 1 +
525 (full_path_nchars - args->wim_source_path_nchars) + 1];
528 /*print_dentry(dentry, NULL);*/
529 /*ERROR("%"TS" %"TS, args->target, dentry->_full_path);*/
532 tmemcpy(p, args->target, args->target_nchars);
533 p += args->target_nchars;
535 if (dentry != args->extract_root) {
537 tmemcpy(p, full_path + args->wim_source_path_nchars,
538 full_path_nchars - args->wim_source_path_nchars);
539 p += full_path_nchars - args->wim_source_path_nchars;
542 return (*apply_dentry_func)(output_path, p - output_path,
547 /* Extracts a file, directory, or symbolic link from the WIM archive. */
549 apply_dentry_normal(struct wim_dentry *dentry, void *arg)
552 return do_apply_op(dentry, arg, win32_do_apply_dentry);
554 return do_apply_op(dentry, arg, unix_do_apply_dentry);
559 /* Apply timestamps to an extracted file or directory */
561 apply_dentry_timestamps_normal(struct wim_dentry *dentry, void *arg)
564 return do_apply_op(dentry, arg, win32_do_apply_dentry_timestamps);
566 return do_apply_op(dentry, arg, unix_do_apply_dentry_timestamps);
571 dentry_is_descendent(const struct wim_dentry *dentry,
572 const struct wim_dentry *ancestor)
575 if (dentry == ancestor)
577 if (dentry_is_root(dentry))
579 dentry = dentry->parent;
583 /* Extract a dentry if it hasn't already been extracted and either
584 * WIMLIB_EXTRACT_FLAG_NO_STREAMS is not specified, or the dentry is a directory
585 * and/or has no unnamed stream. */
587 maybe_apply_dentry(struct wim_dentry *dentry, void *arg)
589 struct apply_args *args = arg;
592 if (dentry->is_extracted)
595 if (!dentry_is_descendent(dentry, args->extract_root))
598 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_NO_STREAMS &&
599 !dentry_is_directory(dentry) &&
600 inode_unnamed_lte_resolved(dentry->d_inode) != NULL)
603 if ((args->extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE) &&
604 args->progress_func) {
605 args->progress.extract.cur_path = dentry->_full_path;
606 args->progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY,
609 ret = args->apply_dentry(dentry, args);
611 dentry->is_extracted = 1;
616 calculate_bytes_to_extract(struct list_head *stream_list,
618 union wimlib_progress_info *progress)
620 struct wim_lookup_table_entry *lte;
624 /* For each stream to be extracted... */
625 list_for_each_entry(lte, stream_list, extraction_list) {
627 (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
629 /* In the symlink or hard link extraction mode, each
630 * stream will be extracted one time regardless of how
631 * many dentries share the stream. */
632 wimlib_assert(!(extract_flags & WIMLIB_EXTRACT_FLAG_NTFS));
633 if (!lte->extracted_file) {
635 total_bytes += wim_resource_size(lte);
638 num_streams += lte->out_refcnt;
639 total_bytes += lte->out_refcnt * wim_resource_size(lte);
642 progress->extract.num_streams = num_streams;
643 progress->extract.total_bytes = total_bytes;
644 progress->extract.completed_bytes = 0;
648 maybe_add_stream_for_extraction(struct wim_lookup_table_entry *lte,
649 struct list_head *stream_list)
651 if (++lte->out_refcnt == 1) {
652 INIT_LIST_HEAD(<e->inode_list);
653 list_add_tail(<e->extraction_list, stream_list);
658 inode_find_streams_for_extraction(struct wim_inode *inode,
659 struct list_head *stream_list,
662 struct wim_lookup_table_entry *lte;
663 bool inode_added = false;
665 lte = inode_unnamed_lte_resolved(inode);
667 maybe_add_stream_for_extraction(lte, stream_list);
668 list_add_tail(&inode->i_lte_inode_list, <e->inode_list);
672 /* Determine whether to include alternate data stream entries or not.
674 * UNIX: Include them if extracting using NTFS-3g.
676 * Windows: Include them undconditionally, although if the filesystem is
677 * not NTFS we won't actually be able to extract them. */
678 #if defined(WITH_NTFS_3G)
679 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS)
680 #elif defined(__WIN32__)
686 for (unsigned i = 0; i < inode->i_num_ads; i++) {
687 if (inode->i_ads_entries[i].stream_name_nbytes != 0) {
688 lte = inode->i_ads_entries[i].lte;
690 maybe_add_stream_for_extraction(lte,
693 list_add_tail(&inode->i_lte_inode_list,
703 struct find_streams_ctx {
704 struct list_head stream_list;
709 dentry_find_streams_to_extract(struct wim_dentry *dentry, void *_ctx)
711 struct find_streams_ctx *ctx = _ctx;
712 struct wim_inode *inode = dentry->d_inode;
714 dentry->is_extracted = 0;
715 if (!inode->i_visited) {
716 inode_find_streams_for_extraction(inode, &ctx->stream_list,
718 inode->i_visited = 1;
724 dentry_resolve_and_zero_lte_refcnt(struct wim_dentry *dentry, void *_lookup_table)
726 struct wim_inode *inode = dentry->d_inode;
727 struct wim_lookup_table *lookup_table = _lookup_table;
728 struct wim_lookup_table_entry *lte;
730 inode_resolve_ltes(inode, lookup_table);
731 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
732 lte = inode_stream_lte_resolved(inode, i);
740 find_streams_for_extraction(struct wim_dentry *root,
741 struct list_head *stream_list,
742 struct wim_lookup_table *lookup_table,
745 struct find_streams_ctx ctx;
747 INIT_LIST_HEAD(&ctx.stream_list);
748 ctx.extract_flags = extract_flags;
749 for_dentry_in_tree(root, dentry_resolve_and_zero_lte_refcnt, lookup_table);
750 for_dentry_in_tree(root, dentry_find_streams_to_extract, &ctx);
751 list_transfer(&ctx.stream_list, stream_list);
755 dentry_mark_inode_unvisited(struct wim_dentry *dentry, void *_ignore)
757 dentry->d_inode->i_visited = 0;
761 struct apply_operations {
762 int (*apply_dentry)(struct wim_dentry *dentry, void *arg);
763 int (*apply_dentry_timestamps)(struct wim_dentry *dentry, void *arg);
766 static const struct apply_operations normal_apply_operations = {
767 .apply_dentry = apply_dentry_normal,
768 .apply_dentry_timestamps = apply_dentry_timestamps_normal,
772 static const struct apply_operations ntfs_apply_operations = {
773 .apply_dentry = apply_dentry_ntfs,
774 .apply_dentry_timestamps = apply_dentry_timestamps_ntfs,
779 apply_stream_list(struct list_head *stream_list,
780 struct apply_args *args,
781 const struct apply_operations *ops,
782 wimlib_progress_func_t progress_func)
784 uint64_t bytes_per_progress = args->progress.extract.total_bytes / 100;
785 uint64_t next_progress = bytes_per_progress;
786 struct wim_lookup_table_entry *lte;
787 struct wim_inode *inode;
788 struct wim_dentry *dentry;
791 /* This complicated loop is essentially looping through the dentries,
792 * although dentries may be visited more than once (if a dentry contains
793 * two different nonempty streams) or not at all (if a dentry contains
794 * no non-empty streams).
796 * The outer loop is over the distinct streams to be extracted so that
797 * sequential reading of the WIM can be implemented. */
799 /* For each distinct stream to be extracted */
800 list_for_each_entry(lte, stream_list, extraction_list) {
801 /* For each inode that contains the stream */
802 list_for_each_entry(inode, <e->inode_list, i_lte_inode_list) {
803 /* For each dentry that points to the inode */
804 inode_for_each_dentry(dentry, inode) {
805 /* Extract the dentry if it was not already
807 ret = maybe_apply_dentry(dentry, args);
811 args->progress.extract.completed_bytes >= next_progress)
813 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS,
815 if (args->progress.extract.completed_bytes >=
816 args->progress.extract.total_bytes)
818 next_progress = ~0ULL;
821 min (args->progress.extract.completed_bytes +
823 args->progress.extract.total_bytes);
833 sort_stream_list_by_wim_position(struct list_head *stream_list)
835 struct list_head *cur;
837 struct wim_lookup_table_entry **array;
842 list_for_each(cur, stream_list)
844 array_size = num_streams * sizeof(array[0]);
845 array = MALLOC(array_size);
847 ERROR("Failed to allocate %zu bytes to sort stream entries",
849 return WIMLIB_ERR_NOMEM;
851 cur = stream_list->next;
852 for (i = 0; i < num_streams; i++) {
853 array[i] = container_of(cur, struct wim_lookup_table_entry, extraction_list);
857 qsort(array, num_streams, sizeof(array[0]), cmp_streams_by_wim_position);
859 INIT_LIST_HEAD(stream_list);
860 for (i = 0; i < num_streams; i++)
861 list_add_tail(&array[i]->extraction_list, stream_list);
867 * extract_tree - Extract a file or directory tree from the currently selected
870 * @wim: WIMStruct for the WIM file, with the desired image selected
871 * (as wim->current_image).
873 * "Canonical" (i.e. no leading or trailing slashes, path
874 * separators forwald slashes) path inside the WIM image to
875 * extract. An empty string means the full image.
877 * Filesystem path to extract the file or directory tree to.
880 * WIMLIB_EXTRACT_FLAG_*. Also, the private flag
881 * WIMLIB_EXTRACT_FLAG_MULTI_IMAGE will be set if this is being
882 * called through wimlib_extract_image() with WIMLIB_ALL_IMAGES as
886 * If non-NULL, progress function for the extraction. The messages
887 * we may in this function are:
889 * WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN or
890 * WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN;
891 * WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN;
892 * WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_END;
893 * WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY;
894 * WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS;
895 * WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS;
896 * WIMLIB_PROGRESS_MSG_EXTRACT_TREE_END or
897 * WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END.
899 * Returns 0 on success; nonzero on failure.
902 extract_tree(WIMStruct *wim, const tchar *wim_source_path, const tchar *target,
903 int extract_flags, wimlib_progress_func_t progress_func)
906 struct list_head stream_list;
907 struct apply_args args;
908 const struct apply_operations *ops;
909 struct wim_dentry *root;
911 memset(&args, 0, sizeof(args));
914 args.target = target;
915 args.extract_flags = extract_flags;
916 args.progress_func = progress_func;
917 args.target_nchars = tstrlen(target);
918 args.wim_source_path_nchars = tstrlen(wim_source_path);
921 args.progress.extract.wimfile_name = wim->filename;
922 args.progress.extract.image = wim->current_image;
923 args.progress.extract.extract_flags = (extract_flags &
924 WIMLIB_EXTRACT_MASK_PUBLIC);
925 args.progress.extract.image_name = wimlib_get_image_name(wim,
927 args.progress.extract.extract_root_wim_source_path = wim_source_path;
928 args.progress.extract.target = target;
932 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
933 args.vol = ntfs_mount(target, 0);
935 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%"TS"'",
937 ret = WIMLIB_ERR_NTFS_3G;
940 ops = &ntfs_apply_operations;
943 ops = &normal_apply_operations;
945 root = get_dentry(wim, wim_source_path);
947 ERROR("Path \"%"TS"\" does not exist in WIM image %d",
948 wim_source_path, wim->current_image);
949 ret = WIMLIB_ERR_PATH_DOES_NOT_EXIST;
950 goto out_ntfs_umount;
952 args.extract_root = root;
954 /* Build a list of the streams that need to be extracted */
955 find_streams_for_extraction(root,
957 wim->lookup_table, extract_flags);
959 /* Calculate the number of bytes of data that will be extracted */
960 calculate_bytes_to_extract(&stream_list, extract_flags,
964 progress_func(*wim_source_path ? WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN :
965 WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN,
969 /* If a sequential extraction was specified, sort the streams to be
970 * extracted by their position in the WIM file, so that the WIM file can
971 * be read sequentially. */
972 if (extract_flags & WIMLIB_EXTRACT_FLAG_SEQUENTIAL) {
973 ret = sort_stream_list_by_wim_position(&stream_list);
975 WARNING("Falling back to non-sequential extraction");
976 extract_flags &= ~WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
981 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN,
985 ret = calculate_dentry_tree_full_paths(root);
987 goto out_mark_inodes_unvisited;
989 /* Make the directory structure and extract empty files */
990 args.extract_flags |= WIMLIB_EXTRACT_FLAG_NO_STREAMS;
991 args.apply_dentry = ops->apply_dentry;
992 ret = for_dentry_in_tree(root, maybe_apply_dentry, &args);
993 args.extract_flags &= ~WIMLIB_EXTRACT_FLAG_NO_STREAMS;
995 goto out_mark_inodes_unvisited;
998 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_END,
1002 if (extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX) {
1003 args.target_realpath = realpath(target, NULL);
1004 if (!args.target_realpath) {
1005 ret = WIMLIB_ERR_NOMEM;
1006 goto out_mark_inodes_unvisited;
1008 args.target_realpath_len = tstrlen(args.target_realpath);
1011 /* Extract non-empty files */
1012 ret = apply_stream_list(&stream_list, &args, ops, progress_func);
1014 goto out_free_target_realpath;
1016 if (progress_func) {
1017 progress_func(WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS,
1021 /* Apply timestamps */
1022 ret = for_dentry_in_tree_depth(root,
1023 ops->apply_dentry_timestamps, &args);
1025 goto out_free_target_realpath;
1027 if (progress_func) {
1028 progress_func(*wim_source_path ? WIMLIB_PROGRESS_MSG_EXTRACT_TREE_END :
1029 WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END,
1032 out_free_target_realpath:
1033 FREE(args.target_realpath);
1034 out_mark_inodes_unvisited:
1035 for_dentry_in_tree(root, dentry_mark_inode_unvisited, NULL);
1038 /* Unmount the NTFS volume */
1039 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1040 if (ntfs_umount(args.vol, FALSE) != 0) {
1041 ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%"TS"'",
1044 ret = WIMLIB_ERR_NTFS_3G;
1052 /* Validates a single wimlib_extract_command, mostly checking to make sure the
1053 * extract flags make sense. */
1055 check_extract_command(struct wimlib_extract_command *cmd, int wim_header_flags)
1058 bool is_entire_image = (cmd->wim_source_path[0] == T('\0'));
1060 /* Empty destination path? */
1061 if (cmd->fs_dest_path[0] == T('\0'))
1062 return WIMLIB_ERR_INVALID_PARAM;
1064 extract_flags = cmd->extract_flags;
1066 /* Specified both symlink and hardlink modes? */
1067 if ((extract_flags &
1068 (WIMLIB_EXTRACT_FLAG_SYMLINK |
1069 WIMLIB_EXTRACT_FLAG_HARDLINK)) == (WIMLIB_EXTRACT_FLAG_SYMLINK |
1070 WIMLIB_EXTRACT_FLAG_HARDLINK))
1071 return WIMLIB_ERR_INVALID_PARAM;
1074 /* Wanted UNIX data on Win32? */
1075 if (extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
1076 ERROR("Extracting UNIX data is not supported on Windows");
1077 return WIMLIB_ERR_INVALID_PARAM;
1079 /* Wanted linked extraction on Windows? (XXX This is possible, just not
1080 * implemented yet.) */
1081 if (extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
1082 WIMLIB_EXTRACT_FLAG_HARDLINK))
1084 ERROR("Linked extraction modes are not supported on Windows");
1085 return WIMLIB_ERR_INVALID_PARAM;
1089 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1090 /* NTFS-3g extraction mode requested */
1092 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
1093 WIMLIB_EXTRACT_FLAG_HARDLINK))) {
1094 ERROR("Cannot specify symlink or hardlink flags when applying\n"
1095 " directly to a NTFS volume");
1096 return WIMLIB_ERR_INVALID_PARAM;
1098 if (!is_entire_image &&
1099 (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS))
1101 ERROR("When applying directly to a NTFS volume you can "
1102 "only extract a full image, not part of one");
1103 return WIMLIB_ERR_INVALID_PARAM;
1105 if (extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
1106 ERROR("Cannot restore UNIX-specific data in "
1107 "the NTFS extraction mode");
1108 return WIMLIB_ERR_INVALID_PARAM;
1111 ERROR("wimlib was compiled without support for NTFS-3g, so");
1112 ERROR("we cannot apply a WIM image directly to a NTFS volume");
1113 return WIMLIB_ERR_UNSUPPORTED;
1117 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_RPFIX |
1118 WIMLIB_EXTRACT_FLAG_NORPFIX)) ==
1119 (WIMLIB_EXTRACT_FLAG_RPFIX | WIMLIB_EXTRACT_FLAG_NORPFIX))
1121 ERROR("Cannot specify RPFIX and NORPFIX flags at the same time!");
1122 return WIMLIB_ERR_INVALID_PARAM;
1125 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_RPFIX |
1126 WIMLIB_EXTRACT_FLAG_NORPFIX)) == 0)
1128 /* Do reparse point fixups by default if the WIM header says
1129 * they are enabled and we are extracting a full image. */
1130 if ((wim_header_flags & WIM_HDR_FLAG_RP_FIX) && is_entire_image)
1131 extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
1134 if (!is_entire_image && (extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX)) {
1135 ERROR("Cannot specify --rpfix when not extracting entire image");
1136 return WIMLIB_ERR_INVALID_PARAM;
1139 cmd->extract_flags = extract_flags;
1144 /* Internal function to execute extraction commands for a WIM image. */
1146 do_wimlib_extract_files(WIMStruct *wim,
1148 struct wimlib_extract_command *cmds,
1150 wimlib_progress_func_t progress_func)
1153 bool found_link_cmd = false;
1154 bool found_nolink_cmd = false;
1156 /* Select the image from which we are extracting files */
1157 ret = select_wim_image(wim, image);
1161 /* Make sure there are no streams in the WIM that have not been
1162 * checksummed yet. */
1163 ret = wim_checksum_unhashed_streams(wim);
1167 /* Check for problems with the extraction commands */
1168 for (size_t i = 0; i < num_cmds; i++) {
1169 ret = check_extract_command(&cmds[i], wim->hdr.flags);
1172 if (cmds[i].extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
1173 WIMLIB_EXTRACT_FLAG_HARDLINK)) {
1174 found_link_cmd = true;
1176 found_nolink_cmd = true;
1178 if (found_link_cmd && found_nolink_cmd) {
1179 ERROR("Symlink or hardlink extraction mode must "
1180 "be set on all extraction commands");
1181 return WIMLIB_ERR_INVALID_PARAM;
1185 /* Execute the extraction commands */
1186 for (size_t i = 0; i < num_cmds; i++) {
1187 ret = extract_tree(wim,
1188 cmds[i].wim_source_path,
1189 cmds[i].fs_dest_path,
1190 cmds[i].extract_flags,
1198 /* Extract files or directories from a WIM image. */
1200 wimlib_extract_files(WIMStruct *wim,
1202 int default_extract_flags,
1203 const struct wimlib_extract_command *cmds,
1205 WIMStruct **additional_swms,
1206 unsigned num_additional_swms,
1207 wimlib_progress_func_t progress_func)
1210 struct wimlib_extract_command *cmds_copy;
1211 struct wim_lookup_table *wim_tab_save, *joined_tab;
1214 default_extract_flags &= WIMLIB_EXTRACT_MASK_PUBLIC;
1216 ret = verify_swm_set(wim, additional_swms, num_additional_swms);
1220 if (num_additional_swms) {
1221 ret = new_joined_lookup_table(wim, additional_swms,
1222 num_additional_swms,
1226 wim_tab_save = wim->lookup_table;
1227 wim->lookup_table = joined_tab;
1230 cmds_copy = CALLOC(num_cmds, sizeof(cmds[0]));
1232 ret = WIMLIB_ERR_NOMEM;
1233 goto out_restore_lookup_table;
1236 for (size_t i = 0; i < num_cmds; i++) {
1237 cmds_copy[i].extract_flags = (default_extract_flags |
1238 cmds[i].extract_flags)
1239 & WIMLIB_EXTRACT_MASK_PUBLIC;
1240 all_flags |= cmds_copy[i].extract_flags;
1242 cmds_copy[i].wim_source_path = canonicalize_wim_path(cmds[i].wim_source_path);
1243 if (!cmds_copy[i].wim_source_path) {
1244 ret = WIMLIB_ERR_NOMEM;
1245 goto out_free_cmds_copy;
1248 cmds_copy[i].fs_dest_path = canonicalize_fs_path(cmds[i].fs_dest_path);
1249 if (!cmds_copy[i].fs_dest_path) {
1250 ret = WIMLIB_ERR_NOMEM;
1251 goto out_free_cmds_copy;
1255 ret = do_wimlib_extract_files(wim, image,
1256 cmds_copy, num_cmds,
1259 if (all_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
1260 WIMLIB_EXTRACT_FLAG_HARDLINK))
1262 for_lookup_table_entry(wim->lookup_table,
1263 lte_free_extracted_file, NULL);
1266 for (size_t i = 0; i < num_cmds; i++) {
1267 FREE(cmds_copy[i].wim_source_path);
1268 FREE(cmds_copy[i].fs_dest_path);
1271 out_restore_lookup_table:
1272 if (num_additional_swms) {
1273 free_lookup_table(wim->lookup_table);
1274 wim->lookup_table = wim_tab_save;
1281 * Extracts an image from a WIM file.
1283 * @wim: WIMStruct for the WIM file.
1285 * @image: Number of the single image to extract.
1287 * @target: Directory or NTFS volume to extract the image to.
1289 * @extract_flags: Bitwise or of WIMLIB_EXTRACT_FLAG_*.
1291 * @progress_func: If non-NULL, a progress function to be called
1294 * Returns 0 on success; nonzero on failure.
1297 extract_single_image(WIMStruct *wim, int image,
1298 const tchar *target, int extract_flags,
1299 wimlib_progress_func_t progress_func)
1302 tchar *target_copy = canonicalize_fs_path(target);
1304 return WIMLIB_ERR_NOMEM;
1305 struct wimlib_extract_command cmd = {
1306 .wim_source_path = T(""),
1307 .fs_dest_path = target_copy,
1308 .extract_flags = extract_flags,
1310 ret = do_wimlib_extract_files(wim, image, &cmd, 1, progress_func);
1315 static const tchar * const filename_forbidden_chars =
1324 /* This function checks if it is okay to use a WIM image's name as a directory
1327 image_name_ok_as_dir(const tchar *image_name)
1329 return image_name && *image_name &&
1330 !tstrpbrk(image_name, filename_forbidden_chars);
1333 /* Extracts all images from the WIM to the directory @target, with the images
1334 * placed in subdirectories named by their image names. */
1336 extract_all_images(WIMStruct *wim,
1337 const tchar *target,
1339 wimlib_progress_func_t progress_func)
1341 size_t image_name_max_len = max(xml_get_max_image_name_len(wim), 20);
1342 size_t output_path_len = tstrlen(target);
1343 tchar buf[output_path_len + 1 + image_name_max_len + 1];
1346 const tchar *image_name;
1348 ret = extract_directory(NULL, target, true);
1352 tmemcpy(buf, target, output_path_len);
1353 buf[output_path_len] = T('/');
1354 for (image = 1; image <= wim->hdr.image_count; image++) {
1355 image_name = wimlib_get_image_name(wim, image);
1356 if (image_name_ok_as_dir(image_name)) {
1357 tstrcpy(buf + output_path_len + 1, image_name);
1359 /* Image name is empty or contains forbidden characters.
1360 * Use image number instead. */
1361 tsprintf(buf + output_path_len + 1, T("%d"), image);
1363 ret = extract_single_image(wim, image, buf, extract_flags,
1371 /* Extracts a single image or all images from a WIM file to a directory or NTFS
1374 wimlib_extract_image(WIMStruct *wim,
1376 const tchar *target,
1378 WIMStruct **additional_swms,
1379 unsigned num_additional_swms,
1380 wimlib_progress_func_t progress_func)
1382 struct wim_lookup_table *joined_tab, *wim_tab_save;
1385 extract_flags &= WIMLIB_EXTRACT_MASK_PUBLIC;
1387 ret = verify_swm_set(wim, additional_swms, num_additional_swms);
1391 if (num_additional_swms) {
1392 ret = new_joined_lookup_table(wim, additional_swms,
1393 num_additional_swms, &joined_tab);
1396 wim_tab_save = wim->lookup_table;
1397 wim->lookup_table = joined_tab;
1400 if (image == WIMLIB_ALL_IMAGES) {
1401 extract_flags |= WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
1402 ret = extract_all_images(wim, target, extract_flags,
1405 extract_flags &= ~WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
1406 ret = extract_single_image(wim, image, target, extract_flags,
1410 if (extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
1411 WIMLIB_EXTRACT_FLAG_HARDLINK))
1413 for_lookup_table_entry(wim->lookup_table,
1414 lte_free_extracted_file,
1417 if (num_additional_swms) {
1418 free_lookup_table(wim->lookup_table);
1419 wim->lookup_table = wim_tab_save;