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) 2010 Carl Thijssen
14 * Copyright (C) 2012 Eric Biggers
16 * This file is part of wimlib, a library for working with WIM files.
18 * wimlib is free software; you can redistribute it and/or modify it under the
19 * terms of the GNU General Public License as published by the Free
20 * Software Foundation; either version 3 of the License, or (at your option)
23 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
24 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
25 * A PARTICULAR PURPOSE. See the GNU General Public License for more
28 * You should have received a copy of the GNU General Public License
29 * along with wimlib; if not, see http://www.gnu.org/licenses/.
51 #include "lookup_table.h"
52 #include "timestamp.h"
53 #include "wimlib_internal.h"
57 #include <ntfs-3g/volume.h>
60 static int extract_regular_file_linked(struct dentry *dentry,
61 const char *output_path,
62 struct apply_args *args,
63 struct lookup_table_entry *lte)
65 /* This mode overrides the normal hard-link extraction and
66 * instead either symlinks or hardlinks *all* identical files in
67 * the WIM, even if they are in a different image (in the case
68 * of a multi-image extraction) */
70 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_HARDLINK) {
71 if (link(lte->extracted_file, output_path) != 0) {
72 ERROR_WITH_ERRNO("Failed to hard link "
74 output_path, lte->extracted_file);
75 return WIMLIB_ERR_LINK;
78 int num_path_components;
79 int num_output_dir_path_components;
80 size_t extracted_file_len;
86 get_num_path_components(dentry->full_path_utf8) - 1;
87 num_output_dir_path_components =
88 get_num_path_components(args->target);
90 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE) {
91 num_path_components++;
92 num_output_dir_path_components--;
94 extracted_file_len = strlen(lte->extracted_file);
96 char buf[extracted_file_len + 3 * num_path_components + 1];
99 for (i = 0; i < num_path_components; i++) {
104 p2 = lte->extracted_file;
107 while (num_output_dir_path_components--)
108 p2 = path_next_part(p2, NULL);
110 if (symlink(buf, output_path) != 0) {
111 ERROR_WITH_ERRNO("Failed to symlink `%s' to "
113 buf, lte->extracted_file);
114 return WIMLIB_ERR_LINK;
120 static int extract_regular_file_unlinked(struct dentry *dentry,
121 struct apply_args *args,
122 const char *output_path,
123 struct lookup_table_entry *lte)
125 /* Normal mode of extraction. Regular files and hard links are
126 * extracted in the way that they appear in the WIM. */
130 struct inode *inode = dentry->d_inode;
132 if (!((args->extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE)
133 && (args->extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
134 WIMLIB_EXTRACT_FLAG_HARDLINK))))
136 /* If the dentry is one of a hard link set of at least 2
137 * dentries and one of the other dentries has already been
138 * extracted, make a hard link to the file corresponding to this
139 * already-extracted directory. Otherwise, extract the file,
140 * and set the inode->extracted_file field so that other
141 * dentries in the hard link group can link to it. */
142 if (inode->link_count > 1) {
143 if (inode->extracted_file) {
144 DEBUG("Extracting hard link `%s' => `%s'",
145 output_path, inode->extracted_file);
146 if (link(inode->extracted_file, output_path) != 0) {
147 ERROR_WITH_ERRNO("Failed to hard link "
150 inode->extracted_file);
151 return WIMLIB_ERR_LINK;
155 FREE(inode->extracted_file);
156 inode->extracted_file = STRDUP(output_path);
157 if (!inode->extracted_file) {
158 ERROR("Failed to allocate memory for filename");
159 return WIMLIB_ERR_NOMEM;
164 /* Extract the contents of the file to @output_path. */
166 out_fd = open(output_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
168 ERROR_WITH_ERRNO("Failed to open the file `%s' for writing",
170 return WIMLIB_ERR_OPEN;
174 /* Empty file with no lookup table entry */
175 DEBUG("Empty file `%s'.", output_path);
180 ret = extract_full_wim_resource_to_fd(lte, out_fd);
182 ERROR("Failed to extract resource to `%s'", output_path);
185 args->progress.extract.completed_bytes += wim_resource_size(lte);
187 if (close(out_fd) != 0) {
188 ERROR_WITH_ERRNO("Failed to close file `%s'", output_path);
189 ret = WIMLIB_ERR_WRITE;
195 * Extracts a regular file from the WIM archive.
197 static int extract_regular_file(struct dentry *dentry,
198 struct apply_args *args,
199 const char *output_path)
201 struct lookup_table_entry *lte;
202 const struct inode *inode = dentry->d_inode;
204 lte = inode_unnamed_lte_resolved(inode);
206 if (lte && (args->extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
207 WIMLIB_EXTRACT_FLAG_HARDLINK)))
209 if (lte->extracted_file) {
210 return extract_regular_file_linked(dentry, output_path, args, lte);
212 lte->extracted_file = STRDUP(output_path);
213 if (!lte->extracted_file)
214 return WIMLIB_ERR_NOMEM;
217 return extract_regular_file_unlinked(dentry, args, output_path, lte);
220 static int extract_symlink(struct dentry *dentry,
221 struct apply_args *args,
222 const char *output_path)
225 ssize_t ret = inode_readlink(dentry->d_inode, target,
226 sizeof(target), args->w, 0);
227 struct lookup_table_entry *lte;
230 ERROR("Could not read the symbolic link from dentry `%s'",
231 dentry->full_path_utf8);
232 return WIMLIB_ERR_INVALID_DENTRY;
234 ret = symlink(target, output_path);
236 ERROR_WITH_ERRNO("Failed to symlink `%s' to `%s'",
237 output_path, target);
238 return WIMLIB_ERR_LINK;
240 lte = inode_unnamed_lte_resolved(dentry->d_inode);
241 args->progress.extract.completed_bytes += wim_resource_size(lte);
246 * Extracts a directory from the WIM archive.
248 * @dentry: The directory entry for the directory.
249 * @output_path: The path to which the directory is to be extracted to.
250 * @return: True on success, false on failure.
252 static int extract_directory(const char *output_path, bool is_root)
256 ret = stat(output_path, &stbuf);
258 if (S_ISDIR(stbuf.st_mode)) {
260 /*WARNING("`%s' already exists", output_path);*/
263 ERROR("`%s' is not a directory", output_path);
264 return WIMLIB_ERR_MKDIR;
267 if (errno != ENOENT) {
268 ERROR_WITH_ERRNO("Failed to stat `%s'", output_path);
269 return WIMLIB_ERR_STAT;
272 /* Compute the output path directory to the directory. */
273 if (mkdir(output_path, S_IRWXU | S_IRGRP | S_IXGRP |
274 S_IROTH | S_IXOTH) != 0) {
275 ERROR_WITH_ERRNO("Cannot create directory `%s'",
277 return WIMLIB_ERR_MKDIR;
283 * Extracts a file, directory, or symbolic link from the WIM archive. For use
284 * in for_dentry_in_tree().
286 static int apply_dentry_normal(struct dentry *dentry, void *arg)
288 struct apply_args *args = arg;
289 int extract_flags = args->extract_flags;
290 struct inode *inode = dentry->d_inode;
294 if (dentry->is_extracted)
297 if (extract_flags & WIMLIB_EXTRACT_FLAG_NO_STREAMS)
298 if (inode_unnamed_lte_resolved(inode))
301 if ((extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE) &&
304 args->progress.extract.cur_path = dentry->full_path_utf8;
305 args->progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY,
309 len = strlen(args->target);
310 char output_path[len + dentry->full_path_utf8_len + 1];
311 memcpy(output_path, args->target, len);
312 memcpy(output_path + len, dentry->full_path_utf8, dentry->full_path_utf8_len);
313 output_path[len + dentry->full_path_utf8_len] = '\0';
315 if (inode_is_symlink(inode))
316 ret = extract_symlink(dentry, args, output_path);
317 else if (inode_is_directory(inode))
318 ret = extract_directory(output_path, false);
320 ret = extract_regular_file(dentry, args, output_path);
322 dentry->is_extracted = 1;
326 /* Apply timestamp to extracted file */
327 static int apply_dentry_timestamps_normal(struct dentry *dentry, void *arg)
329 struct apply_args *args = arg;
330 size_t len = strlen(args->target);
331 char output_path[len + dentry->full_path_utf8_len + 1];
332 const struct inode *inode = dentry->d_inode;
335 memcpy(output_path, args->target, len);
336 memcpy(output_path + len, dentry->full_path_utf8, dentry->full_path_utf8_len);
337 output_path[len + dentry->full_path_utf8_len] = '\0';
339 struct timeval tv[2];
340 wim_timestamp_to_timeval(inode->last_access_time, &tv[0]);
341 wim_timestamp_to_timeval(inode->last_write_time, &tv[1]);
343 ret = lutimes(output_path, tv);
350 if (errno == ENOSYS) {
352 buf.actime = wim_timestamp_to_unix(inode->last_access_time);
353 buf.modtime = wim_timestamp_to_unix(inode->last_write_time);
354 if (utime(output_path, &buf) == 0)
358 if (errno != ENOSYS || args->num_lutimes_warnings < 10) {
359 /*WARNING("Failed to set timestamp on file `%s': %s",*/
360 /*output_path, strerror(errno));*/
361 args->num_lutimes_warnings++;
367 static int cmp_streams_by_wim_position(const void *p1, const void *p2)
369 const struct lookup_table_entry *lte1, *lte2;
370 lte1 = *(const struct lookup_table_entry**)p1;
371 lte2 = *(const struct lookup_table_entry**)p2;
372 if (lte1->resource_entry.offset < lte2->resource_entry.offset)
374 else if (lte1->resource_entry.offset > lte2->resource_entry.offset)
380 static int sort_stream_list_by_wim_position(struct list_head *stream_list)
382 struct list_head *cur;
384 struct lookup_table_entry **array;
389 list_for_each(cur, stream_list)
391 array_size = num_streams * sizeof(array[0]);
392 array = MALLOC(array_size);
394 ERROR("Failed to allocate %zu bytes to sort stream entries",
396 return WIMLIB_ERR_NOMEM;
398 cur = stream_list->next;
399 for (i = 0; i < num_streams; i++) {
400 array[i] = container_of(cur, struct lookup_table_entry, staging_list);
404 qsort(array, num_streams, sizeof(array[0]), cmp_streams_by_wim_position);
406 INIT_LIST_HEAD(stream_list);
407 for (i = 0; i < num_streams; i++)
408 list_add_tail(&array[i]->staging_list, stream_list);
413 static void calculate_bytes_to_extract(struct list_head *stream_list,
415 union wimlib_progress_info *progress)
417 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 list_for_each_entry(inode, <e->inode_list,
440 total_bytes += wim_resource_size(lte);
444 progress->extract.num_streams = num_streams;
445 progress->extract.total_bytes = total_bytes;
446 progress->extract.completed_bytes = 0;
449 static void maybe_add_stream_for_extraction(struct lookup_table_entry *lte,
450 struct list_head *stream_list)
452 if (lte->out_refcnt == 0) {
454 INIT_LIST_HEAD(<e->inode_list);
455 list_add_tail(<e->staging_list, stream_list);
459 static void inode_find_streams_for_extraction(struct inode *inode,
460 struct list_head *stream_list,
463 struct lookup_table_entry *lte;
464 bool inode_added = false;
466 lte = inode_unnamed_lte_resolved(inode);
469 maybe_add_stream_for_extraction(lte, stream_list);
470 list_add_tail(&inode->lte_inode_list, <e->inode_list);
474 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
475 for (unsigned i = 0; i < inode->num_ads; i++) {
476 if (inode->ads_entries[i].stream_name_len != 0) {
477 lte = inode_stream_lte_resolved(inode, i + 1);
479 maybe_add_stream_for_extraction(lte,
482 list_add_tail(&inode->lte_inode_list,
493 static void find_streams_for_extraction(struct hlist_head *inode_list,
494 struct list_head *stream_list,
495 struct lookup_table *lookup_table,
499 struct hlist_node *cur;
500 struct dentry *dentry;
502 for_lookup_table_entry(lookup_table, lte_zero_out_refcnt, NULL);
503 INIT_LIST_HEAD(stream_list);
504 hlist_for_each_entry(inode, cur, inode_list, hlist) {
505 if (!inode->resolved)
506 inode_resolve_ltes(inode, lookup_table);
507 inode_for_each_dentry(dentry, inode)
508 dentry->is_extracted = 0;
509 inode_find_streams_for_extraction(inode, stream_list,
514 struct apply_operations {
515 int (*apply_dentry)(struct dentry *dentry, void *arg);
516 int (*apply_dentry_timestamps)(struct dentry *dentry, void *arg);
519 static const struct apply_operations normal_apply_operations = {
520 .apply_dentry = apply_dentry_normal,
521 .apply_dentry_timestamps = apply_dentry_timestamps_normal,
525 static const struct apply_operations ntfs_apply_operations = {
526 .apply_dentry = wim_apply_dentry_ntfs,
527 .apply_dentry_timestamps = wim_apply_dentry_timestamps,
531 static int apply_stream_list(struct list_head *stream_list,
532 struct apply_args *args,
533 const struct apply_operations *ops,
534 wimlib_progress_func_t progress_func)
536 uint64_t bytes_per_progress = args->progress.extract.total_bytes / 100;
537 uint64_t next_progress = bytes_per_progress;
538 struct lookup_table_entry *lte;
540 struct dentry *dentry;
542 list_for_each_entry(lte, stream_list, staging_list) {
543 list_for_each_entry(inode, <e->inode_list, lte_inode_list) {
544 inode_for_each_dentry(dentry, inode) {
545 ret = ops->apply_dentry(dentry, args);
548 if (args->progress.extract.completed_bytes >= next_progress) {
549 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS,
551 next_progress += bytes_per_progress;
560 static int extract_single_image(WIMStruct *w, int image,
561 const char *target, int extract_flags,
562 wimlib_progress_func_t progress_func)
565 struct list_head stream_list;
566 struct hlist_head *inode_list;
568 struct apply_args args;
569 const struct apply_operations *ops;
572 args.target = target;
573 args.extract_flags = extract_flags;
574 args.num_lutimes_warnings = 0;
575 args.target = target;
576 args.stream_list = &stream_list;
577 args.progress_func = progress_func;
580 args.progress.extract.image = image;
581 args.progress.extract.image_name = wimlib_get_image_name(w, image);
582 args.progress.extract.target = target;
586 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
587 args.vol = ntfs_mount(target, 0);
589 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%s'", target);
590 return WIMLIB_ERR_NTFS_3G;
596 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS)
597 ops = &ntfs_apply_operations;
600 ops = &normal_apply_operations;
602 ret = select_wim_image(w, image);
606 inode_list = &w->image_metadata[image - 1].inode_list;
607 find_streams_for_extraction(inode_list,
612 calculate_bytes_to_extract(&stream_list, extract_flags,
616 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN,
620 if (extract_flags & WIMLIB_EXTRACT_FLAG_SEQUENTIAL) {
621 ret = sort_stream_list_by_wim_position(&stream_list);
623 WARNING("Falling back to non-sequential extraction");
624 extract_flags &= ~WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
629 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN,
634 args.extract_flags |= WIMLIB_EXTRACT_FLAG_NO_STREAMS;
635 ret = for_dentry_in_tree(wim_root_dentry(w), ops->apply_dentry, &args);
636 args.extract_flags &= ~WIMLIB_EXTRACT_FLAG_NO_STREAMS;
641 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_END,
645 ret = apply_stream_list(&stream_list, &args, ops, progress_func);
650 progress_func(WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS, NULL);
652 ret = for_dentry_in_tree_depth(wim_root_dentry(w),
653 ops->apply_dentry_timestamps, &args);
658 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END,
663 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
664 if (ntfs_umount(args.vol, FALSE) != 0) {
665 ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%s'", args.target);
667 ret = WIMLIB_ERR_NTFS_3G;
675 /* Extracts all images from the WIM to @output_dir, with the images placed in
676 * subdirectories named by their image names. */
677 static int extract_all_images(WIMStruct *w, const char *target,
679 wimlib_progress_func_t progress_func)
681 size_t image_name_max_len = max(xml_get_max_image_name_len(w), 20);
682 size_t output_path_len = strlen(target);
683 char buf[output_path_len + 1 + image_name_max_len + 1];
686 const char *image_name;
688 ret = extract_directory(target, true);
692 memcpy(buf, target, output_path_len);
693 buf[output_path_len] = '/';
694 for (image = 1; image <= w->hdr.image_count; image++) {
695 image_name = wimlib_get_image_name(w, image);
696 if (image_name && *image_name) {
697 strcpy(buf + output_path_len + 1, image_name);
699 /* Image name is empty. Use image number instead */
700 sprintf(buf + output_path_len + 1, "%d", image);
702 ret = extract_single_image(w, image, buf, extract_flags,
710 /* Extracts a single image or all images from a WIM file. */
711 WIMLIBAPI int wimlib_extract_image(WIMStruct *w, int image,
714 WIMStruct **additional_swms,
715 unsigned num_additional_swms,
716 wimlib_progress_func_t progress_func)
718 struct lookup_table *joined_tab, *w_tab_save;
722 return WIMLIB_ERR_INVALID_PARAM;
724 extract_flags &= WIMLIB_EXTRACT_MASK_PUBLIC;
726 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
727 == (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
728 return WIMLIB_ERR_INVALID_PARAM;
730 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
732 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))) {
733 ERROR("Cannot specify symlink or hardlink flags when applying ");
734 ERROR("directly to a NTFS volume");
735 return WIMLIB_ERR_INVALID_PARAM;
737 if (image == WIMLIB_ALL_IMAGES) {
738 ERROR("Can only apply a single image when applying "
739 "directly to a NTFS volume");
740 return WIMLIB_ERR_INVALID_PARAM;
743 ERROR("wimlib was compiled without support for NTFS-3g, so");
744 ERROR("we cannot apply a WIM image directly to a NTFS volume");
745 return WIMLIB_ERR_UNSUPPORTED;
749 ret = verify_swm_set(w, additional_swms, num_additional_swms);
753 if (num_additional_swms) {
754 ret = new_joined_lookup_table(w, additional_swms,
755 num_additional_swms, &joined_tab);
758 w_tab_save = w->lookup_table;
759 w->lookup_table = joined_tab;
762 if (image == WIMLIB_ALL_IMAGES) {
763 extract_flags |= WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
764 ret = extract_all_images(w, target, extract_flags,
767 extract_flags &= ~WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
768 ret = extract_single_image(w, image, target, extract_flags,
772 if (extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
773 WIMLIB_EXTRACT_FLAG_HARDLINK))
775 for_lookup_table_entry(w->lookup_table,
776 lte_free_extracted_file,
780 if (num_additional_swms) {
781 free_lookup_table(w->lookup_table);
782 w->lookup_table = w_tab_save;