4 * Support for extracting WIM files.
6 * This code does NOT contain any filesystem-specific features. In particular,
7 * security information (i.e. file permissions) and alternate data streams are
8 * ignored, except possibly to read an alternate data stream that contains
13 * Copyright (C) 2012 Eric Biggers
15 * This file is part of wimlib, a library for working with WIM files.
17 * wimlib is free software; you can redistribute it and/or modify it under the
18 * terms of the GNU General Public License as published by the Free
19 * Software Foundation; either version 3 of the License, or (at your option)
22 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
23 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
24 * A PARTICULAR PURPOSE. See the GNU General Public License for more
27 * You should have received a copy of the GNU General Public License
28 * along with wimlib; if not, see http://www.gnu.org/licenses/.
48 #include "lookup_table.h"
49 #include "timestamp.h"
50 #include "wimlib_internal.h"
54 #include <ntfs-3g/volume.h>
57 static int extract_regular_file_linked(struct dentry *dentry,
58 const char *output_path,
59 struct apply_args *args,
60 struct lookup_table_entry *lte)
62 /* This mode overrides the normal hard-link extraction and
63 * instead either symlinks or hardlinks *all* identical files in
64 * the WIM, even if they are in a different image (in the case
65 * of a multi-image extraction) */
67 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_HARDLINK) {
68 if (link(lte->extracted_file, output_path) != 0) {
69 ERROR_WITH_ERRNO("Failed to hard link "
71 output_path, lte->extracted_file);
72 return WIMLIB_ERR_LINK;
75 int num_path_components;
76 int num_output_dir_path_components;
77 size_t extracted_file_len;
83 get_num_path_components(dentry->full_path_utf8) - 1;
84 num_output_dir_path_components =
85 get_num_path_components(args->target);
87 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE) {
88 num_path_components++;
89 num_output_dir_path_components--;
91 extracted_file_len = strlen(lte->extracted_file);
93 char buf[extracted_file_len + 3 * num_path_components + 1];
96 for (i = 0; i < num_path_components; i++) {
101 p2 = lte->extracted_file;
104 while (num_output_dir_path_components--)
105 p2 = path_next_part(p2, NULL);
107 if (symlink(buf, output_path) != 0) {
108 ERROR_WITH_ERRNO("Failed to symlink `%s' to "
110 buf, lte->extracted_file);
111 return WIMLIB_ERR_LINK;
117 static int extract_regular_file_unlinked(struct dentry *dentry,
118 struct apply_args *args,
119 const char *output_path,
120 struct lookup_table_entry *lte)
122 /* Normal mode of extraction. Regular files and hard links are
123 * extracted in the way that they appear in the WIM. */
127 struct inode *inode = dentry->d_inode;
129 if (!((args->extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE)
130 && (args->extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
131 WIMLIB_EXTRACT_FLAG_HARDLINK))))
133 /* If the dentry is one of a hard link set of at least 2
134 * dentries and one of the other dentries has already been
135 * extracted, make a hard link to the file corresponding to this
136 * already-extracted directory. Otherwise, extract the file,
137 * and set the inode->extracted_file field so that other
138 * dentries in the hard link group can link to it. */
139 if (inode->link_count > 1) {
140 if (inode->extracted_file) {
141 DEBUG("Extracting hard link `%s' => `%s'",
142 output_path, inode->extracted_file);
143 if (link(inode->extracted_file, output_path) != 0) {
144 ERROR_WITH_ERRNO("Failed to hard link "
147 inode->extracted_file);
148 return WIMLIB_ERR_LINK;
152 FREE(inode->extracted_file);
153 inode->extracted_file = STRDUP(output_path);
154 if (!inode->extracted_file) {
155 ERROR("Failed to allocate memory for filename");
156 return WIMLIB_ERR_NOMEM;
161 /* Extract the contents of the file to @output_path. */
163 out_fd = open(output_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
165 ERROR_WITH_ERRNO("Failed to open the file `%s' for writing",
167 return WIMLIB_ERR_OPEN;
171 /* Empty file with no lookup table entry */
172 DEBUG("Empty file `%s'.", output_path);
177 ret = extract_wim_resource_to_fd(lte, out_fd, wim_resource_size(lte));
179 ERROR("Failed to extract resource to `%s'", output_path);
182 args->progress.extract.completed_bytes += wim_resource_size(lte);
184 if (close(out_fd) != 0) {
185 ERROR_WITH_ERRNO("Failed to close file `%s'", output_path);
186 ret = WIMLIB_ERR_WRITE;
192 * Extracts a regular file from the WIM archive.
194 static int extract_regular_file(struct dentry *dentry,
195 struct apply_args *args,
196 const char *output_path)
198 struct lookup_table_entry *lte;
199 const struct inode *inode = dentry->d_inode;
201 lte = inode_unnamed_lte_resolved(inode);
203 if (lte && (args->extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
204 WIMLIB_EXTRACT_FLAG_HARDLINK)))
206 if (lte->extracted_file) {
207 return extract_regular_file_linked(dentry, output_path, args, lte);
209 lte->extracted_file = STRDUP(output_path);
210 if (!lte->extracted_file)
211 return WIMLIB_ERR_NOMEM;
214 return extract_regular_file_unlinked(dentry, args, output_path, lte);
217 static int extract_symlink(struct dentry *dentry,
218 struct apply_args *args,
219 const char *output_path)
222 ssize_t ret = inode_readlink(dentry->d_inode, target,
223 sizeof(target), args->w, 0);
224 struct lookup_table_entry *lte;
227 ERROR("Could not read the symbolic link from dentry `%s'",
228 dentry->full_path_utf8);
229 return WIMLIB_ERR_INVALID_DENTRY;
231 ret = symlink(target, output_path);
233 ERROR_WITH_ERRNO("Failed to symlink `%s' to `%s'",
234 output_path, target);
235 return WIMLIB_ERR_LINK;
237 lte = inode_unnamed_lte_resolved(dentry->d_inode);
238 args->progress.extract.completed_bytes += wim_resource_size(lte);
243 * Extracts a directory from the WIM archive.
245 * @dentry: The directory entry for the directory.
246 * @output_path: The path to which the directory is to be extracted to.
247 * @return: True on success, false on failure.
249 static int extract_directory(const char *output_path, bool is_root)
253 ret = stat(output_path, &stbuf);
255 if (S_ISDIR(stbuf.st_mode)) {
257 /*WARNING("`%s' already exists", output_path);*/
260 ERROR("`%s' is not a directory", output_path);
261 return WIMLIB_ERR_MKDIR;
264 if (errno != ENOENT) {
265 ERROR_WITH_ERRNO("Failed to stat `%s'", output_path);
266 return WIMLIB_ERR_STAT;
269 /* Compute the output path directory to the directory. */
270 if (mkdir(output_path, S_IRWXU | S_IRGRP | S_IXGRP |
271 S_IROTH | S_IXOTH) != 0) {
272 ERROR_WITH_ERRNO("Cannot create directory `%s'",
274 return WIMLIB_ERR_MKDIR;
280 * Extracts a file, directory, or symbolic link from the WIM archive. For use
281 * in for_dentry_in_tree().
283 static int apply_dentry_normal(struct dentry *dentry, void *arg)
285 struct apply_args *args = arg;
286 struct inode *inode = dentry->d_inode;
289 len = strlen(args->target);
290 char output_path[len + dentry->full_path_utf8_len + 1];
291 memcpy(output_path, args->target, len);
292 memcpy(output_path + len, dentry->full_path_utf8, dentry->full_path_utf8_len);
293 output_path[len + dentry->full_path_utf8_len] = '\0';
295 if (inode_is_symlink(inode))
296 return extract_symlink(dentry, args, output_path);
297 else if (inode_is_directory(inode))
298 return extract_directory(output_path, false);
300 return extract_regular_file(dentry, args, output_path);
303 /* Apply timestamp to extracted file */
304 static int apply_dentry_timestamps_normal(struct dentry *dentry, void *arg)
306 struct apply_args *args = arg;
307 size_t len = strlen(args->target);
308 char output_path[len + dentry->full_path_utf8_len + 1];
309 const struct inode *inode = dentry->d_inode;
312 memcpy(output_path, args->target, len);
313 memcpy(output_path + len, dentry->full_path_utf8, dentry->full_path_utf8_len);
314 output_path[len + dentry->full_path_utf8_len] = '\0';
316 struct timeval tv[2];
317 wim_timestamp_to_timeval(inode->last_access_time, &tv[0]);
318 wim_timestamp_to_timeval(inode->last_write_time, &tv[1]);
320 ret = lutimes(output_path, tv);
327 if (errno == ENOSYS) {
329 buf.actime = wim_timestamp_to_unix(inode->last_access_time);
330 buf.modtime = wim_timestamp_to_unix(inode->last_write_time);
331 if (utime(output_path, &buf) == 0)
335 if (errno != ENOSYS || args->num_lutimes_warnings < 10) {
336 /*WARNING("Failed to set timestamp on file `%s': %s",*/
337 /*output_path, strerror(errno));*/
338 args->num_lutimes_warnings++;
344 static int maybe_apply_dentry(struct dentry *dentry, void *arg)
346 struct apply_args *args = arg;
349 if (dentry->is_extracted)
352 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_NO_STREAMS)
353 if (inode_unnamed_lte_resolved(dentry->d_inode))
356 if ((args->extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE) &&
357 args->progress_func) {
358 args->progress.extract.cur_path = dentry->full_path_utf8;
359 args->progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY,
362 ret = args->apply_dentry(dentry, args);
364 dentry->is_extracted = 1;
368 static int cmp_streams_by_wim_position(const void *p1, const void *p2)
370 const struct lookup_table_entry *lte1, *lte2;
371 lte1 = *(const struct lookup_table_entry**)p1;
372 lte2 = *(const struct lookup_table_entry**)p2;
373 if (lte1->resource_entry.offset < lte2->resource_entry.offset)
375 else if (lte1->resource_entry.offset > lte2->resource_entry.offset)
381 static int sort_stream_list_by_wim_position(struct list_head *stream_list)
383 struct list_head *cur;
385 struct lookup_table_entry **array;
390 list_for_each(cur, stream_list)
392 array_size = num_streams * sizeof(array[0]);
393 array = MALLOC(array_size);
395 ERROR("Failed to allocate %zu bytes to sort stream entries",
397 return WIMLIB_ERR_NOMEM;
399 cur = stream_list->next;
400 for (i = 0; i < num_streams; i++) {
401 array[i] = container_of(cur, struct lookup_table_entry, staging_list);
405 qsort(array, num_streams, sizeof(array[0]), cmp_streams_by_wim_position);
407 INIT_LIST_HEAD(stream_list);
408 for (i = 0; i < num_streams; i++)
409 list_add_tail(&array[i]->staging_list, stream_list);
414 static void calculate_bytes_to_extract(struct list_head *stream_list,
416 union wimlib_progress_info *progress)
418 struct lookup_table_entry *lte;
422 /* For each stream to be extracted... */
423 list_for_each_entry(lte, stream_list, staging_list) {
425 (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
427 /* In the symlink or hard link extraction mode, each
428 * stream will be extracted one time regardless of how
429 * many dentries share the stream. */
430 wimlib_assert(!(extract_flags & WIMLIB_EXTRACT_FLAG_NTFS));
431 if (!lte->extracted_file) {
433 total_bytes += wim_resource_size(lte);
436 num_streams += lte->out_refcnt;
437 total_bytes += lte->out_refcnt * wim_resource_size(lte);
440 progress->extract.num_streams = num_streams;
441 progress->extract.total_bytes = total_bytes;
442 progress->extract.completed_bytes = 0;
445 static void maybe_add_stream_for_extraction(struct lookup_table_entry *lte,
446 struct list_head *stream_list)
448 if (++lte->out_refcnt == 1) {
449 INIT_LIST_HEAD(<e->inode_list);
450 list_add_tail(<e->staging_list, stream_list);
454 static void inode_find_streams_for_extraction(struct inode *inode,
455 struct list_head *stream_list,
458 struct lookup_table_entry *lte;
459 bool inode_added = false;
461 lte = inode_unnamed_lte_resolved(inode);
464 maybe_add_stream_for_extraction(lte, stream_list);
465 list_add_tail(&inode->lte_inode_list, <e->inode_list);
469 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
470 for (unsigned i = 0; i < inode->num_ads; i++) {
471 if (inode->ads_entries[i].stream_name_len != 0) {
472 lte = inode->ads_entries[i].lte;
474 maybe_add_stream_for_extraction(lte,
477 list_add_tail(&inode->lte_inode_list,
488 static void find_streams_for_extraction(struct hlist_head *inode_list,
489 struct list_head *stream_list,
490 struct lookup_table *lookup_table,
494 struct hlist_node *cur;
495 struct dentry *dentry;
497 for_lookup_table_entry(lookup_table, lte_zero_out_refcnt, NULL);
498 INIT_LIST_HEAD(stream_list);
499 hlist_for_each_entry(inode, cur, inode_list, hlist) {
500 if (!inode->resolved)
501 inode_resolve_ltes(inode, lookup_table);
502 inode_for_each_dentry(dentry, inode)
503 dentry->is_extracted = 0;
504 inode_find_streams_for_extraction(inode, stream_list,
509 struct apply_operations {
510 int (*apply_dentry)(struct dentry *dentry, void *arg);
511 int (*apply_dentry_timestamps)(struct dentry *dentry, void *arg);
514 static const struct apply_operations normal_apply_operations = {
515 .apply_dentry = apply_dentry_normal,
516 .apply_dentry_timestamps = apply_dentry_timestamps_normal,
520 static const struct apply_operations ntfs_apply_operations = {
521 .apply_dentry = apply_dentry_ntfs,
522 .apply_dentry_timestamps = apply_dentry_timestamps_ntfs,
526 static int apply_stream_list(struct list_head *stream_list,
527 struct apply_args *args,
528 const struct apply_operations *ops,
529 wimlib_progress_func_t progress_func)
531 uint64_t bytes_per_progress = args->progress.extract.total_bytes / 100;
532 uint64_t next_progress = bytes_per_progress;
533 struct lookup_table_entry *lte;
535 struct dentry *dentry;
538 /* This complicated loop is essentially looping through the dentries,
539 * although dentries may be visited more than once (if a dentry contains
540 * two different nonempty streams) or not at all (if a dentry contains
541 * no non-empty streams).
543 * The outer loop is over the distinct streams to be extracted so that
544 * sequential reading of the WIM can be implemented. */
546 /* For each distinct stream to be extracted */
547 list_for_each_entry(lte, stream_list, staging_list) {
548 /* For each inode that contains the stream */
549 list_for_each_entry(inode, <e->inode_list, lte_inode_list) {
550 /* For each dentry that points to the inode */
551 inode_for_each_dentry(dentry, inode) {
552 /* Extract the dentry if it was not already
554 ret = maybe_apply_dentry(dentry, args);
558 args->progress.extract.completed_bytes >= next_progress)
560 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS,
562 if (args->progress.extract.completed_bytes >=
563 args->progress.extract.total_bytes)
565 next_progress = ~0ULL;
568 min (args->progress.extract.completed_bytes +
570 args->progress.extract.total_bytes);
579 static int extract_single_image(WIMStruct *w, int image,
580 const char *target, int extract_flags,
581 wimlib_progress_func_t progress_func)
584 struct list_head stream_list;
585 struct hlist_head *inode_list;
587 struct apply_args args;
588 const struct apply_operations *ops;
591 args.target = target;
592 args.extract_flags = extract_flags;
593 args.num_lutimes_warnings = 0;
594 args.target = target;
595 args.stream_list = &stream_list;
596 args.progress_func = progress_func;
599 args.progress.extract.wimfile_name = w->filename;
600 args.progress.extract.image = image;
601 args.progress.extract.extract_flags = (extract_flags &
602 WIMLIB_EXTRACT_MASK_PUBLIC);
603 args.progress.extract.image_name = wimlib_get_image_name(w, image);
604 args.progress.extract.target = target;
608 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
609 args.vol = ntfs_mount(target, 0);
611 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%s'", target);
612 return WIMLIB_ERR_NTFS_3G;
614 ops = &ntfs_apply_operations;
617 ops = &normal_apply_operations;
619 ret = select_wim_image(w, image);
623 inode_list = &w->image_metadata[image - 1].inode_list;
625 find_streams_for_extraction(inode_list, &stream_list,
626 w->lookup_table, extract_flags);
628 calculate_bytes_to_extract(&stream_list, extract_flags,
632 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN,
636 if (extract_flags & WIMLIB_EXTRACT_FLAG_SEQUENTIAL) {
637 ret = sort_stream_list_by_wim_position(&stream_list);
639 WARNING("Falling back to non-sequential extraction");
640 extract_flags &= ~WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
645 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN,
649 args.extract_flags |= WIMLIB_EXTRACT_FLAG_NO_STREAMS;
650 args.apply_dentry = ops->apply_dentry;
651 ret = for_dentry_in_tree(wim_root_dentry(w), maybe_apply_dentry, &args);
652 args.extract_flags &= ~WIMLIB_EXTRACT_FLAG_NO_STREAMS;
657 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_END,
661 ret = apply_stream_list(&stream_list, &args, ops, progress_func);
666 progress_func(WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS,
670 ret = for_dentry_in_tree_depth(wim_root_dentry(w),
671 ops->apply_dentry_timestamps, &args);
676 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END,
681 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
682 if (ntfs_umount(args.vol, FALSE) != 0) {
683 ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%s'", args.target);
685 ret = WIMLIB_ERR_NTFS_3G;
693 /* Extracts all images from the WIM to @output_dir, with the images placed in
694 * subdirectories named by their image names. */
695 static int extract_all_images(WIMStruct *w, const char *target,
697 wimlib_progress_func_t progress_func)
699 size_t image_name_max_len = max(xml_get_max_image_name_len(w), 20);
700 size_t output_path_len = strlen(target);
701 char buf[output_path_len + 1 + image_name_max_len + 1];
704 const char *image_name;
706 ret = extract_directory(target, true);
710 memcpy(buf, target, output_path_len);
711 buf[output_path_len] = '/';
712 for (image = 1; image <= w->hdr.image_count; image++) {
713 image_name = wimlib_get_image_name(w, image);
714 if (image_name && *image_name) {
715 strcpy(buf + output_path_len + 1, image_name);
717 /* Image name is empty. Use image number instead */
718 sprintf(buf + output_path_len + 1, "%d", image);
720 ret = extract_single_image(w, image, buf, extract_flags,
728 /* Extracts a single image or all images from a WIM file. */
729 WIMLIBAPI int wimlib_extract_image(WIMStruct *w, int image,
732 WIMStruct **additional_swms,
733 unsigned num_additional_swms,
734 wimlib_progress_func_t progress_func)
736 struct lookup_table *joined_tab, *w_tab_save;
740 return WIMLIB_ERR_INVALID_PARAM;
742 extract_flags &= WIMLIB_EXTRACT_MASK_PUBLIC;
744 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
745 == (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
746 return WIMLIB_ERR_INVALID_PARAM;
748 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
750 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))) {
751 ERROR("Cannot specify symlink or hardlink flags when applying\n"
752 " directly to a NTFS volume");
753 return WIMLIB_ERR_INVALID_PARAM;
755 if (image == WIMLIB_ALL_IMAGES) {
756 ERROR("Can only apply a single image when applying "
757 "directly to a NTFS volume");
758 return WIMLIB_ERR_INVALID_PARAM;
761 ERROR("wimlib was compiled without support for NTFS-3g, so");
762 ERROR("we cannot apply a WIM image directly to a NTFS volume");
763 return WIMLIB_ERR_UNSUPPORTED;
767 ret = verify_swm_set(w, additional_swms, num_additional_swms);
771 if (num_additional_swms) {
772 ret = new_joined_lookup_table(w, additional_swms,
773 num_additional_swms, &joined_tab);
776 w_tab_save = w->lookup_table;
777 w->lookup_table = joined_tab;
780 if (image == WIMLIB_ALL_IMAGES) {
781 extract_flags |= WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
782 ret = extract_all_images(w, target, extract_flags,
785 extract_flags &= ~WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
786 ret = extract_single_image(w, image, target, extract_flags,
790 if (extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
791 WIMLIB_EXTRACT_FLAG_HARDLINK))
793 for_lookup_table_entry(w->lookup_table,
794 lte_free_extracted_file,
798 if (num_additional_swms) {
799 free_lookup_table(w->lookup_table);
800 w->lookup_table = w_tab_save;