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/.
50 #include "lookup_table.h"
51 #include "timestamp.h"
52 #include "wimlib_internal.h"
56 static int extract_regular_file_linked(const struct dentry *dentry,
57 const char *output_dir,
58 const char *output_path,
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) */
66 wimlib_assert(lte->extracted_file);
68 if (extract_flags & WIMLIB_EXTRACT_FLAG_HARDLINK) {
69 if (link(lte->extracted_file, output_path) != 0) {
70 ERROR_WITH_ERRNO("Failed to hard link "
72 output_path, lte->extracted_file);
73 return WIMLIB_ERR_LINK;
76 int num_path_components;
77 int num_output_dir_path_components;
78 size_t extracted_file_len;
83 wimlib_assert(extract_flags & WIMLIB_EXTRACT_FLAG_SYMLINK);
86 get_num_path_components(dentry->full_path_utf8) - 1;
87 num_output_dir_path_components =
88 get_num_path_components(output_dir);
90 if (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;
121 static int extract_regular_file_unlinked(WIMStruct *w,
122 struct dentry *dentry,
123 const char *output_path,
125 struct lookup_table_entry *lte)
127 /* Normal mode of extraction. Regular files and hard links are
128 * extracted in the way that they appear in the WIM. */
132 struct inode *inode = dentry->d_inode;
134 if (!((extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE)
135 && (extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
136 WIMLIB_EXTRACT_FLAG_HARDLINK))))
138 /* If the dentry is one of a hard link set of at least 2
139 * dentries and one of the other dentries has already been
140 * extracted, make a hard link to the file corresponding to this
141 * already-extracted directory. Otherwise, extract the file,
142 * and set the inode->extracted_file field so that other
143 * dentries in the hard link group can link to it. */
144 if (inode->link_count > 1) {
145 if (inode->extracted_file) {
146 DEBUG("Extracting hard link `%s' => `%s'",
147 output_path, inode->extracted_file);
148 if (link(inode->extracted_file, output_path) != 0) {
149 ERROR_WITH_ERRNO("Failed to hard link "
152 inode->extracted_file);
153 return WIMLIB_ERR_LINK;
157 FREE(inode->extracted_file);
158 inode->extracted_file = STRDUP(output_path);
159 if (!inode->extracted_file) {
160 ERROR("Failed to allocate memory for filename");
161 return WIMLIB_ERR_NOMEM;
166 /* Extract the contents of the file to @output_path. */
168 out_fd = open(output_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
170 ERROR_WITH_ERRNO("Failed to open the file `%s' for writing",
172 return WIMLIB_ERR_OPEN;
176 /* Empty file with no lookup table entry */
177 DEBUG("Empty file `%s'.", output_path);
182 ret = extract_full_wim_resource_to_fd(lte, out_fd);
184 ERROR("Failed to extract resource to `%s'", output_path);
189 if (close(out_fd) != 0) {
190 ERROR_WITH_ERRNO("Failed to close file `%s'", output_path);
191 ret = WIMLIB_ERR_WRITE;
197 * Extracts a regular file from the WIM archive.
199 static int extract_regular_file(WIMStruct *w,
200 struct dentry *dentry,
201 const char *output_dir,
202 const char *output_path,
205 struct lookup_table_entry *lte;
206 const struct inode *inode = dentry->d_inode;
208 lte = inode_unnamed_lte(inode, w->lookup_table);
210 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
211 WIMLIB_EXTRACT_FLAG_HARDLINK)) && lte) {
212 if (lte->extracted_file) {
213 return extract_regular_file_linked(dentry, output_dir,
217 lte->extracted_file = STRDUP(output_path);
218 if (!lte->extracted_file)
219 return WIMLIB_ERR_NOMEM;
223 return extract_regular_file_unlinked(w, dentry, output_path,
228 static int extract_symlink(const struct dentry *dentry, const char *output_path,
232 ssize_t ret = inode_readlink(dentry->d_inode, target,
233 sizeof(target), w, 0);
235 ERROR("Could not read the symbolic link from dentry `%s'",
236 dentry->full_path_utf8);
237 return WIMLIB_ERR_INVALID_DENTRY;
239 ret = symlink(target, output_path);
241 ERROR_WITH_ERRNO("Failed to symlink `%s' to `%s'",
242 output_path, target);
243 return WIMLIB_ERR_LINK;
249 * Extracts a directory from the WIM archive.
251 * @dentry: The directory entry for the directory.
252 * @output_path: The path to which the directory is to be extracted to.
253 * @return: True on success, false on failure.
255 static int extract_directory(const char *output_path, bool is_root)
259 ret = stat(output_path, &stbuf);
261 if (S_ISDIR(stbuf.st_mode)) {
263 /*WARNING("`%s' already exists", output_path);*/
266 ERROR("`%s' is not a directory", output_path);
267 return WIMLIB_ERR_MKDIR;
270 if (errno != ENOENT) {
271 ERROR_WITH_ERRNO("Failed to stat `%s'", output_path);
272 return WIMLIB_ERR_STAT;
275 /* Compute the output path directory to the directory. */
276 if (mkdir(output_path, S_IRWXU | S_IRGRP | S_IXGRP |
277 S_IROTH | S_IXOTH) != 0) {
278 ERROR_WITH_ERRNO("Cannot create directory `%s'",
280 return WIMLIB_ERR_MKDIR;
285 struct extract_args {
288 const char *output_dir;
289 unsigned num_lutimes_warnings;
293 * Extracts a file, directory, or symbolic link from the WIM archive. For use
294 * in for_dentry_in_tree().
296 static int extract_dentry(struct dentry *dentry, void *arg)
298 struct extract_args *args = arg;
299 WIMStruct *w = args->w;
300 int extract_flags = args->extract_flags;
301 size_t len = strlen(args->output_dir);
302 char output_path[len + dentry->full_path_utf8_len + 1];
304 if (dentry_is_directory(dentry)) {
305 if (extract_flags & WIMLIB_EXTRACT_FLAG_SKIP_DIRS)
308 if (extract_flags & WIMLIB_EXTRACT_FLAG_DIRS_ONLY)
312 if ((extract_flags & WIMLIB_EXTRACT_FLAG_EMPTY_ONLY)
313 && (!dentry_is_regular_file(dentry) ||
314 inode_unnamed_lte(dentry->d_inode, w->lookup_table) != NULL))
317 if (extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE) {
318 wimlib_assert(dentry->full_path_utf8);
319 puts(dentry->full_path_utf8);
322 memcpy(output_path, args->output_dir, len);
323 memcpy(output_path + len, dentry->full_path_utf8, dentry->full_path_utf8_len);
324 output_path[len + dentry->full_path_utf8_len] = '\0';
327 if (dentry_is_symlink(dentry))
328 return extract_symlink(dentry, output_path, w);
329 else if (dentry_is_directory(dentry))
330 return extract_directory(output_path, dentry_is_root(dentry));
332 return extract_regular_file(w, dentry, args->output_dir,
333 output_path, extract_flags);
336 /* Apply timestamp to extracted file */
337 static int apply_dentry_timestamps(struct dentry *dentry, void *arg)
339 struct extract_args *args = arg;
340 size_t len = strlen(args->output_dir);
341 char output_path[len + dentry->full_path_utf8_len + 1];
342 const struct inode *inode = dentry->d_inode;
345 memcpy(output_path, args->output_dir, len);
346 memcpy(output_path + len, dentry->full_path_utf8, dentry->full_path_utf8_len);
347 output_path[len + dentry->full_path_utf8_len] = '\0';
349 struct timeval tv[2];
350 wim_timestamp_to_timeval(inode->last_access_time, &tv[0]);
351 wim_timestamp_to_timeval(inode->last_write_time, &tv[1]);
353 ret = lutimes(output_path, tv);
360 if (errno == ENOSYS) {
362 buf.actime = wim_timestamp_to_unix(inode->last_access_time);
363 buf.modtime = wim_timestamp_to_unix(inode->last_write_time);
364 if (utime(output_path, &buf) == 0)
368 if (errno != ENOSYS || args->num_lutimes_warnings < 10) {
369 /*WARNING("Failed to set timestamp on file `%s': %s",*/
370 /*output_path, strerror(errno));*/
371 args->num_lutimes_warnings++;
378 static int dentry_add_streams_for_extraction(struct dentry *dentry,
382 struct list_head *stream_list;
383 struct lookup_table_entry *lte;
385 lte = inode_unnamed_lte(dentry->d_inode, w->lookup_table);
387 if (++lte->out_refcnt == 1) {
388 INIT_LIST_HEAD(<e->dentry_list);
389 stream_list = w->private;
390 list_add_tail(<e->staging_list, stream_list);
392 list_add_tail(&dentry->tmp_list, <e->dentry_list);
397 static int cmp_streams_by_wim_position(const void *p1, const void *p2)
399 const struct lookup_table_entry *lte1, *lte2;
400 lte1 = *(const struct lookup_table_entry**)p1;
401 lte2 = *(const struct lookup_table_entry**)p2;
402 if (lte1->resource_entry.offset < lte2->resource_entry.offset)
404 else if (lte1->resource_entry.offset > lte2->resource_entry.offset)
410 static int sort_stream_list_by_wim_position(struct list_head *stream_list)
412 struct list_head *cur;
414 struct lookup_table_entry **array;
418 DEBUG("Sorting stream list by wim position");
421 list_for_each(cur, stream_list)
423 array_size = num_streams * sizeof(array[0]);
425 DEBUG("num_streams = %zu", num_streams);
427 array = MALLOC(array_size);
429 ERROR("Failed to allocate %zu bytes to sort stream entries",
431 return WIMLIB_ERR_NOMEM;
433 cur = stream_list->next;
434 for (i = 0; i < num_streams; i++) {
435 array[i] = container_of(cur, struct lookup_table_entry, staging_list);
439 qsort(array, num_streams, sizeof(array[0]), cmp_streams_by_wim_position);
441 INIT_LIST_HEAD(stream_list);
442 for (i = 0; i < num_streams; i++)
443 list_add(&array[i]->staging_list, stream_list);
448 static int extract_single_image(WIMStruct *w, int image,
449 const char *output_dir, int extract_flags)
454 DEBUG("Extracting image %d", image);
456 ret = select_wim_image(w, image);
460 root = wim_root_dentry(w);
462 struct extract_args args = {
464 .extract_flags = extract_flags,
465 .output_dir = output_dir,
466 .num_lutimes_warnings = 0,
469 if (extract_flags & WIMLIB_EXTRACT_FLAG_SEQUENTIAL)
470 args.extract_flags |= WIMLIB_EXTRACT_FLAG_DIRS_ONLY;
472 ret = for_dentry_in_tree(root, extract_dentry, &args);
476 if (extract_flags & WIMLIB_EXTRACT_FLAG_SEQUENTIAL) {
477 struct list_head stream_list;
478 INIT_LIST_HEAD(&stream_list);
479 w->private = &stream_list;
480 for_dentry_in_tree(root, dentry_add_streams_for_extraction, w);
481 ret = sort_stream_list_by_wim_position(&stream_list);
482 args.extract_flags &= ~WIMLIB_EXTRACT_FLAG_DIRS_ONLY;
484 args.extract_flags |= WIMLIB_EXTRACT_FLAG_SKIP_DIRS;
485 struct lookup_table_entry *lte;
486 struct lookup_table_entry *tmp;
487 struct dentry *dentry;
488 list_for_each_entry_safe(lte, tmp, &stream_list, staging_list) {
489 list_del(<e->staging_list);
490 lte->extracted_file = NULL;
491 list_for_each_entry(dentry, <e->dentry_list, tmp_list) {
492 ret = extract_dentry(dentry, &args);
496 FREE(lte->extracted_file);
498 args.extract_flags |= WIMLIB_EXTRACT_FLAG_EMPTY_ONLY;
499 ret = for_dentry_in_tree(root, extract_dentry, &args);
502 args.extract_flags &= ~(WIMLIB_EXTRACT_FLAG_SKIP_DIRS |
503 WIMLIB_EXTRACT_FLAG_EMPTY_ONLY);
505 WARNING("Falling back to non-sequential image extraction");
506 ret = for_dentry_in_tree(root, extract_dentry, &args);
512 return for_dentry_in_tree_depth(root, apply_dentry_timestamps, &args);
517 /* Extracts all images from the WIM to @output_dir, with the images placed in
518 * subdirectories named by their image names. */
519 static int extract_all_images(WIMStruct *w, const char *output_dir,
522 size_t image_name_max_len = max(xml_get_max_image_name_len(w), 20);
523 size_t output_path_len = strlen(output_dir);
524 char buf[output_path_len + 1 + image_name_max_len + 1];
527 const char *image_name;
529 DEBUG("Attempting to extract all images from `%s' to `%s'",
530 w->filename, output_dir);
532 ret = extract_directory(output_dir, true);
536 memcpy(buf, output_dir, output_path_len);
537 buf[output_path_len] = '/';
538 for (image = 1; image <= w->hdr.image_count; image++) {
540 image_name = wimlib_get_image_name(w, image);
542 strcpy(buf + output_path_len + 1, image_name);
544 /* Image name is empty. Use image number instead */
545 sprintf(buf + output_path_len + 1, "%d", image);
547 ret = extract_single_image(w, image, buf, extract_flags);
554 /* Extracts a single image or all images from a WIM file. */
555 WIMLIBAPI int wimlib_extract_image(WIMStruct *w, int image,
556 const char *output_dir,
558 WIMStruct **additional_swms,
559 unsigned num_additional_swms)
561 struct lookup_table *joined_tab, *w_tab_save;
564 DEBUG("w->filename = %s, image = %d, output_dir = %s, flags = 0x%x, "
565 "num_additional_swms = %u",
566 w->filename, image, output_dir, extract_flags, num_additional_swms);
568 if (!w || !output_dir)
569 return WIMLIB_ERR_INVALID_PARAM;
571 extract_flags &= WIMLIB_EXTRACT_MASK_PUBLIC;
573 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
574 == (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
575 return WIMLIB_ERR_INVALID_PARAM;
577 ret = verify_swm_set(w, additional_swms, num_additional_swms);
581 if (num_additional_swms) {
582 ret = new_joined_lookup_table(w, additional_swms,
583 num_additional_swms, &joined_tab);
586 w_tab_save = w->lookup_table;
587 w->lookup_table = joined_tab;
590 for_lookup_table_entry(w->lookup_table, lte_zero_out_refcnt, NULL);
592 if (!(extract_flags & WIMLIB_EXTRACT_FLAG_SEQUENTIAL)) {
593 for_lookup_table_entry(w->lookup_table,
594 lte_zero_extracted_file,
598 if (image == WIM_ALL_IMAGES) {
599 extract_flags |= WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
600 ret = extract_all_images(w, output_dir, extract_flags);
602 extract_flags &= ~WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
603 ret = extract_single_image(w, image, output_dir, extract_flags);
605 if (num_additional_swms) {
606 free_lookup_table(w->lookup_table);
607 w->lookup_table = w_tab_save;
609 if (!(extract_flags & WIMLIB_EXTRACT_FLAG_SEQUENTIAL)) {
610 for_lookup_table_entry(w->lookup_table,
611 lte_free_extracted_file,