4 * Support for extracting WIM images, or files or directories contained in a WIM
9 * Copyright (C) 2012, 2013, 2014 Eric Biggers
11 * This file is part of wimlib, a library for working with WIM files.
13 * wimlib is free software; you can redistribute it and/or modify it under the
14 * terms of the GNU General Public License as published by the Free
15 * Software Foundation; either version 3 of the License, or (at your option)
18 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
19 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
20 * A PARTICULAR PURPOSE. See the GNU General Public License for more
23 * You should have received a copy of the GNU General Public License
24 * along with wimlib; if not, see http://www.gnu.org/licenses/.
28 * This file provides the API functions wimlib_extract_image(),
29 * wimlib_extract_image_from_pipe(), wimlib_extract_paths(), and
30 * wimlib_extract_pathlist(). Internally, all end up calling
31 * do_wimlib_extract_paths() and extract_trees().
33 * Although wimlib supports multiple extraction modes/backends (NTFS-3g, UNIX,
34 * Win32), this file does not itself have code to extract files or directories
35 * to any specific target; instead, it handles generic functionality and relies
36 * on lower-level callback functions declared in `struct apply_operations' to do
37 * the actual extraction.
44 #include "wimlib/apply.h"
45 #include "wimlib/dentry.h"
46 #include "wimlib/encoding.h"
47 #include "wimlib/endianness.h"
48 #include "wimlib/error.h"
49 #include "wimlib/lookup_table.h"
50 #include "wimlib/metadata.h"
51 #include "wimlib/pathlist.h"
52 #include "wimlib/paths.h"
53 #include "wimlib/reparse.h"
54 #include "wimlib/resource.h"
55 #include "wimlib/security.h"
57 # include "wimlib/win32.h" /* for realpath() equivalent */
59 #include "wimlib/xml.h"
60 #include "wimlib/wildcard.h"
61 #include "wimlib/wim.h"
69 #define WIMLIB_EXTRACT_FLAG_MULTI_IMAGE 0x80000000
70 #define WIMLIB_EXTRACT_FLAG_FROM_PIPE 0x40000000
71 #define WIMLIB_EXTRACT_FLAG_IMAGEMODE 0x20000000
73 /* Keep in sync with wimlib.h */
74 #define WIMLIB_EXTRACT_MASK_PUBLIC \
75 (WIMLIB_EXTRACT_FLAG_NTFS | \
76 WIMLIB_EXTRACT_FLAG_UNIX_DATA | \
77 WIMLIB_EXTRACT_FLAG_NO_ACLS | \
78 WIMLIB_EXTRACT_FLAG_STRICT_ACLS | \
79 WIMLIB_EXTRACT_FLAG_RPFIX | \
80 WIMLIB_EXTRACT_FLAG_NORPFIX | \
81 WIMLIB_EXTRACT_FLAG_TO_STDOUT | \
82 WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES | \
83 WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS | \
84 WIMLIB_EXTRACT_FLAG_STRICT_TIMESTAMPS | \
85 WIMLIB_EXTRACT_FLAG_STRICT_SHORT_NAMES | \
86 WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS | \
87 WIMLIB_EXTRACT_FLAG_GLOB_PATHS | \
88 WIMLIB_EXTRACT_FLAG_STRICT_GLOB | \
89 WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES | \
90 WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE | \
91 WIMLIB_EXTRACT_FLAG_WIMBOOT)
93 /* Check whether the extraction of a dentry should be skipped completely. */
95 dentry_is_supported(struct wim_dentry *dentry,
96 const struct wim_features *supported_features)
98 struct wim_inode *inode = dentry->d_inode;
100 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
101 return supported_features->reparse_points ||
102 (inode_is_symlink(inode) &&
103 supported_features->symlink_reparse_points);
105 if (inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED) {
106 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)
107 return supported_features->encrypted_directories != 0;
109 return supported_features->encrypted_files != 0;
115 #define PWM_ALLOW_WIM_HDR 0x00001
116 #define PWM_SILENT_EOF 0x00002
118 /* Read the header from a stream in a pipable WIM. */
120 read_pwm_stream_header(WIMStruct *pwm, struct wim_lookup_table_entry *lte,
121 struct wim_resource_spec *rspec,
122 int flags, struct wim_header_disk *hdr_ret)
125 struct pwm_stream_hdr stream_hdr;
126 struct wim_header_disk pwm_hdr;
128 struct wim_reshdr reshdr;
131 ret = full_read(&pwm->in_fd, &buf.stream_hdr, sizeof(buf.stream_hdr));
135 if ((flags & PWM_ALLOW_WIM_HDR) && buf.stream_hdr.magic == PWM_MAGIC) {
136 BUILD_BUG_ON(sizeof(buf.pwm_hdr) < sizeof(buf.stream_hdr));
137 ret = full_read(&pwm->in_fd, &buf.stream_hdr + 1,
138 sizeof(buf.pwm_hdr) - sizeof(buf.stream_hdr));
142 lte->resource_location = RESOURCE_NONEXISTENT;
143 memcpy(hdr_ret, &buf.pwm_hdr, sizeof(buf.pwm_hdr));
147 if (le64_to_cpu(buf.stream_hdr.magic) != PWM_STREAM_MAGIC) {
148 ERROR("Data read on pipe is invalid (expected stream header).");
149 return WIMLIB_ERR_INVALID_PIPABLE_WIM;
152 copy_hash(lte->hash, buf.stream_hdr.hash);
154 reshdr.size_in_wim = 0;
155 reshdr.flags = le32_to_cpu(buf.stream_hdr.flags);
156 reshdr.offset_in_wim = pwm->in_fd.offset;
157 reshdr.uncompressed_size = le64_to_cpu(buf.stream_hdr.uncompressed_size);
158 wim_res_hdr_to_spec(&reshdr, pwm, rspec);
159 lte_bind_wim_resource_spec(lte, rspec);
160 lte->flags = rspec->flags;
161 lte->size = rspec->uncompressed_size;
162 lte->offset_in_res = 0;
166 if (ret != WIMLIB_ERR_UNEXPECTED_END_OF_FILE || !(flags & PWM_SILENT_EOF))
167 ERROR_WITH_ERRNO("Error reading pipable WIM from pipe");
172 load_streams_from_pipe(struct apply_ctx *ctx,
173 const struct read_stream_list_callbacks *cbs)
175 struct wim_lookup_table_entry *found_lte = NULL;
176 struct wim_resource_spec *rspec = NULL;
177 struct wim_lookup_table *lookup_table;
180 ret = WIMLIB_ERR_NOMEM;
181 found_lte = new_lookup_table_entry();
185 rspec = MALLOC(sizeof(struct wim_resource_spec));
189 lookup_table = ctx->wim->lookup_table;
190 memcpy(ctx->progress.extract.guid, ctx->wim->hdr.guid, WIM_GUID_LEN);
191 ctx->progress.extract.part_number = ctx->wim->hdr.part_number;
192 ctx->progress.extract.total_parts = ctx->wim->hdr.total_parts;
193 ret = extract_progress(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN);
197 while (ctx->num_streams_remaining) {
198 struct wim_header_disk pwm_hdr;
199 struct wim_lookup_table_entry *needed_lte;
201 if (found_lte->resource_location != RESOURCE_NONEXISTENT)
202 lte_unbind_wim_resource_spec(found_lte);
203 ret = read_pwm_stream_header(ctx->wim, found_lte, rspec,
204 PWM_ALLOW_WIM_HDR, &pwm_hdr);
208 if ((found_lte->resource_location != RESOURCE_NONEXISTENT)
209 && !(found_lte->flags & WIM_RESHDR_FLAG_METADATA)
210 && (needed_lte = lookup_stream(lookup_table, found_lte->hash))
211 && (needed_lte->out_refcnt))
213 needed_lte->offset_in_res = found_lte->offset_in_res;
214 needed_lte->flags = found_lte->flags;
215 needed_lte->size = found_lte->size;
217 lte_unbind_wim_resource_spec(found_lte);
218 lte_bind_wim_resource_spec(needed_lte, rspec);
220 ret = (*cbs->begin_stream)(needed_lte, 0,
221 cbs->begin_stream_ctx);
223 lte_unbind_wim_resource_spec(needed_lte);
227 ret = extract_stream(needed_lte, needed_lte->size,
229 cbs->consume_chunk_ctx);
231 ret = (*cbs->end_stream)(needed_lte, ret,
232 cbs->end_stream_ctx);
233 lte_unbind_wim_resource_spec(needed_lte);
236 ctx->num_streams_remaining--;
237 } else if (found_lte->resource_location != RESOURCE_NONEXISTENT) {
238 ret = skip_wim_stream(found_lte);
242 u16 part_number = le16_to_cpu(pwm_hdr.part_number);
243 u16 total_parts = le16_to_cpu(pwm_hdr.total_parts);
245 if (part_number != ctx->progress.extract.part_number ||
246 total_parts != ctx->progress.extract.total_parts ||
247 memcmp(pwm_hdr.guid, ctx->progress.extract.guid,
250 ctx->progress.extract.part_number = part_number;
251 ctx->progress.extract.total_parts = total_parts;
252 memcpy(ctx->progress.extract.guid,
253 pwm_hdr.guid, WIM_GUID_LEN);
254 ret = extract_progress(ctx,
255 WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN);
263 if (found_lte->resource_location != RESOURCE_IN_WIM)
265 free_lookup_table_entry(found_lte);
270 begin_extract_stream_with_progress(struct wim_lookup_table_entry *lte,
271 u32 flags, void *_ctx)
273 struct apply_ctx *ctx = _ctx;
275 ctx->cur_stream = lte;
277 return (*ctx->saved_cbs->begin_stream)(lte, flags,
278 ctx->saved_cbs->begin_stream_ctx);
282 consume_chunk_with_progress(const void *chunk, size_t size, void *_ctx)
284 struct apply_ctx *ctx = _ctx;
285 union wimlib_progress_info *progress = &ctx->progress;
288 if (likely(ctx->supported_features.hard_links)) {
289 progress->extract.completed_bytes +=
290 (u64)size * ctx->cur_stream->out_refcnt;
292 const struct stream_owner *owners = stream_owners(ctx->cur_stream);
293 for (u32 i = 0; i < ctx->cur_stream->out_refcnt; i++) {
294 const struct wim_inode *inode = owners[i].inode;
295 const struct wim_dentry *dentry;
297 list_for_each_entry(dentry,
298 &inode->i_extraction_aliases,
299 d_extraction_alias_node)
301 progress->extract.completed_bytes += size;
305 if (progress->extract.completed_bytes >= ctx->next_progress) {
307 ret = extract_progress(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS);
311 if (progress->extract.completed_bytes >=
312 progress->extract.total_bytes)
314 ctx->next_progress = UINT64_MAX;
316 ctx->next_progress += progress->extract.total_bytes / 128;
317 if (ctx->next_progress > progress->extract.total_bytes)
318 ctx->next_progress = progress->extract.total_bytes;
321 return (*ctx->saved_cbs->consume_chunk)(chunk, size,
322 ctx->saved_cbs->consume_chunk_ctx);
326 * Read the list of single-instance streams to extract and feed their data into
327 * the specified callback functions.
329 * This handles checksumming each stream.
331 * This also handles sending WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS.
333 * This also works if the WIM is being read from a pipe, whereas attempting to
334 * read streams directly (e.g. with read_full_stream_into_buf()) will not.
337 extract_stream_list(struct apply_ctx *ctx,
338 const struct read_stream_list_callbacks *cbs)
340 struct read_stream_list_callbacks wrapper_cbs = {
341 .begin_stream = begin_extract_stream_with_progress,
342 .begin_stream_ctx = ctx,
343 .consume_chunk = consume_chunk_with_progress,
344 .consume_chunk_ctx = ctx,
345 .end_stream = cbs->end_stream,
346 .end_stream_ctx = cbs->end_stream_ctx,
349 ctx->saved_cbs = cbs;
352 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_FROM_PIPE) {
353 return load_streams_from_pipe(ctx, cbs);
355 return read_stream_list(&ctx->stream_list,
356 offsetof(struct wim_lookup_table_entry,
358 cbs, VERIFY_STREAM_HASHES);
362 /* Extract a WIM dentry to standard output.
364 * This obviously doesn't make sense in all cases. We return an error if the
365 * dentry does not correspond to a regular file. Otherwise we extract the
366 * unnamed data stream only. */
368 extract_dentry_to_stdout(struct wim_dentry *dentry,
369 const struct wim_lookup_table *lookup_table)
371 struct wim_inode *inode = dentry->d_inode;
372 struct wim_lookup_table_entry *lte;
373 struct filedes _stdout;
375 if (inode->i_attributes & (FILE_ATTRIBUTE_REPARSE_POINT |
376 FILE_ATTRIBUTE_DIRECTORY))
378 ERROR("\"%"TS"\" is not a regular file and therefore cannot be "
379 "extracted to standard output", dentry_full_path(dentry));
380 return WIMLIB_ERR_NOT_A_REGULAR_FILE;
383 lte = inode_unnamed_lte(inode, lookup_table);
385 const u8 *hash = inode_unnamed_stream_hash(inode);
386 if (!is_zero_hash(hash))
387 return stream_not_found_error(inode, hash);
391 filedes_init(&_stdout, STDOUT_FILENO);
392 return extract_full_stream_to_fd(lte, &_stdout);
396 extract_dentries_to_stdout(struct wim_dentry **dentries, size_t num_dentries,
397 const struct wim_lookup_table *lookup_table)
399 for (size_t i = 0; i < num_dentries; i++) {
400 int ret = extract_dentry_to_stdout(dentries[i], lookup_table);
407 /**********************************************************************/
410 * Removes duplicate dentries from the array.
412 * Returns the new number of dentries, packed at the front of the array.
415 remove_duplicate_trees(struct wim_dentry **trees, size_t num_trees)
418 for (i = 0; i < num_trees; i++) {
419 if (!trees[i]->tmp_flag) {
420 /* Found distinct dentry. */
421 trees[i]->tmp_flag = 1;
422 trees[j++] = trees[i];
425 for (i = 0; i < j; i++)
426 trees[i]->tmp_flag = 0;
431 * Remove dentries that are descendants of other dentries in the array.
433 * Returns the new number of dentries, packed at the front of the array.
436 remove_contained_trees(struct wim_dentry **trees, size_t num_trees)
439 for (i = 0; i < num_trees; i++)
440 trees[i]->tmp_flag = 1;
441 for (i = 0; i < num_trees; i++) {
442 struct wim_dentry *d = trees[i];
443 while (!dentry_is_root(d)) {
448 trees[j++] = trees[i];
452 trees[i]->tmp_flag = 0;
455 for (i = 0; i < j; i++)
456 trees[i]->tmp_flag = 0;
461 dentry_append_to_list(struct wim_dentry *dentry, void *_dentry_list)
463 struct list_head *dentry_list = _dentry_list;
464 list_add_tail(&dentry->d_extraction_list_node, dentry_list);
469 dentry_reset_extraction_list_node(struct wim_dentry *dentry)
471 dentry->d_extraction_list_node = (struct list_head){NULL, NULL};
475 dentry_delete_from_list(struct wim_dentry *dentry, void *_ignore)
477 list_del(&dentry->d_extraction_list_node);
478 dentry_reset_extraction_list_node(dentry);
483 * Build the preliminary list of dentries to be extracted.
485 * The list maintains the invariant that if d1 and d2 are in the list and d1 is
486 * an ancestor of d2, then d1 appears before d2 in the list.
489 build_dentry_list(struct list_head *dentry_list, struct wim_dentry **trees,
490 size_t num_trees, bool add_ancestors)
492 INIT_LIST_HEAD(dentry_list);
494 /* Add the trees recursively. */
495 for (size_t i = 0; i < num_trees; i++)
496 for_dentry_in_tree(trees[i], dentry_append_to_list, dentry_list);
498 /* If requested, add ancestors of the trees. */
500 for (size_t i = 0; i < num_trees; i++) {
501 struct wim_dentry *dentry = trees[i];
502 struct wim_dentry *ancestor;
503 struct list_head *place_after;
505 if (dentry_is_root(dentry))
508 place_after = dentry_list;
511 ancestor = ancestor->parent;
512 if (will_extract_dentry(ancestor)) {
513 place_after = &ancestor->d_extraction_list_node;
516 } while (!dentry_is_root(ancestor));
520 ancestor = ancestor->parent;
521 if (will_extract_dentry(ancestor))
523 list_add(&ancestor->d_extraction_list_node, place_after);
524 } while (!dentry_is_root(ancestor));
530 destroy_dentry_list(struct list_head *dentry_list)
532 struct wim_dentry *dentry, *tmp;
533 struct wim_inode *inode;
535 list_for_each_entry_safe(dentry, tmp, dentry_list, d_extraction_list_node) {
536 inode = dentry->d_inode;
537 dentry_reset_extraction_list_node(dentry);
538 inode->i_visited = 0;
539 if ((void *)dentry->d_extraction_name != (void *)dentry->file_name)
540 FREE(dentry->d_extraction_name);
541 dentry->d_extraction_name = NULL;
542 dentry->d_extraction_name_nchars = 0;
547 destroy_stream_list(struct list_head *stream_list)
549 struct wim_lookup_table_entry *lte;
551 list_for_each_entry(lte, stream_list, extraction_list)
552 if (lte->out_refcnt > ARRAY_LEN(lte->inline_stream_owners))
553 FREE(lte->stream_owners);
557 static const utf16lechar replacement_char = cpu_to_le16(0xfffd);
559 static const utf16lechar replacement_char = cpu_to_le16('?');
563 file_name_valid(utf16lechar *name, size_t num_chars, bool fix)
569 for (i = 0; i < num_chars; i++) {
572 case cpu_to_le16('\\'):
573 case cpu_to_le16(':'):
574 case cpu_to_le16('*'):
575 case cpu_to_le16('?'):
576 case cpu_to_le16('"'):
577 case cpu_to_le16('<'):
578 case cpu_to_le16('>'):
579 case cpu_to_le16('|'):
581 case cpu_to_le16('/'):
582 case cpu_to_le16('\0'):
584 name[i] = replacement_char;
591 if (name[num_chars - 1] == cpu_to_le16(' ') ||
592 name[num_chars - 1] == cpu_to_le16('.'))
595 name[num_chars - 1] = replacement_char;
604 dentry_calculate_extraction_name(struct wim_dentry *dentry,
605 struct apply_ctx *ctx)
609 if (!dentry_is_supported(dentry, &ctx->supported_features))
612 if (dentry_is_root(dentry))
615 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
616 dentry->d_extraction_name = dentry->file_name;
617 dentry->d_extraction_name_nchars = dentry->file_name_nbytes /
622 if (!ctx->supported_features.case_sensitive_filenames) {
623 struct wim_dentry *other;
624 list_for_each_entry(other, &dentry->d_ci_conflict_list,
627 if (will_extract_dentry(other)) {
628 if (ctx->extract_flags &
629 WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS) {
630 WARNING("\"%"TS"\" has the same "
631 "case-insensitive name as "
632 "\"%"TS"\"; extracting "
633 "dummy name instead",
634 dentry_full_path(dentry),
635 dentry_full_path(other));
638 WARNING("Not extracting \"%"TS"\": "
639 "has same case-insensitive "
641 dentry_full_path(dentry),
642 dentry_full_path(other));
649 if (file_name_valid(dentry->file_name, dentry->file_name_nbytes / 2, false)) {
650 ret = utf16le_get_tstr(dentry->file_name,
651 dentry->file_name_nbytes,
652 (const tchar **)&dentry->d_extraction_name,
653 &dentry->d_extraction_name_nchars);
654 dentry->d_extraction_name_nchars /= sizeof(tchar);
657 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES)
659 WARNING("\"%"TS"\" has an invalid filename "
660 "that is not supported on this platform; "
661 "extracting dummy name instead",
662 dentry_full_path(dentry));
665 WARNING("Not extracting \"%"TS"\": has an invalid filename "
666 "that is not supported on this platform",
667 dentry_full_path(dentry));
674 utf16lechar utf16_name_copy[dentry->file_name_nbytes / 2];
676 memcpy(utf16_name_copy, dentry->file_name, dentry->file_name_nbytes);
677 file_name_valid(utf16_name_copy, dentry->file_name_nbytes / 2, true);
679 const tchar *tchar_name;
682 ret = utf16le_get_tstr(utf16_name_copy,
683 dentry->file_name_nbytes,
684 &tchar_name, &tchar_nchars);
688 tchar_nchars /= sizeof(tchar);
690 size_t fixed_name_num_chars = tchar_nchars;
691 tchar fixed_name[tchar_nchars + 50];
693 tmemcpy(fixed_name, tchar_name, tchar_nchars);
694 fixed_name_num_chars += tsprintf(fixed_name + tchar_nchars,
695 T(" (invalid filename #%lu)"),
696 ++ctx->invalid_sequence);
698 utf16le_put_tstr(tchar_name);
700 dentry->d_extraction_name = memdup(fixed_name,
701 2 * fixed_name_num_chars + 2);
702 if (!dentry->d_extraction_name)
703 return WIMLIB_ERR_NOMEM;
704 dentry->d_extraction_name_nchars = fixed_name_num_chars;
709 for_dentry_in_tree(dentry, dentry_delete_from_list, NULL);
714 * Calculate the actual filename component at which each WIM dentry will be
715 * extracted, with special handling for dentries that are unsupported by the
716 * extraction backend or have invalid names.
718 * ctx->supported_features must be filled in.
720 * Possible error codes: WIMLIB_ERR_NOMEM, WIMLIB_ERR_INVALID_UTF16_STRING
723 dentry_list_calculate_extraction_names(struct list_head *dentry_list,
724 struct apply_ctx *ctx)
726 struct list_head *prev, *cur;
728 /* Can't use list_for_each_entry() because a call to
729 * dentry_calculate_extraction_name() may delete the current dentry and
730 * its children from the list. */
734 struct wim_dentry *dentry;
738 if (cur == dentry_list)
741 dentry = list_entry(cur, struct wim_dentry, d_extraction_list_node);
743 ret = dentry_calculate_extraction_name(dentry, ctx);
747 if (prev->next == cur)
750 ; /* Current dentry and its children (which follow in
751 the list) were deleted. prev stays the same. */
757 dentry_resolve_streams(struct wim_dentry *dentry, int extract_flags,
758 struct wim_lookup_table *lookup_table)
760 struct wim_inode *inode = dentry->d_inode;
761 struct wim_lookup_table_entry *lte;
765 /* Special case: when extracting from a pipe, the WIM lookup table is
766 * initially empty, so "resolving" an inode's streams is initially not
767 * possible. However, we still need to keep track of which streams,
768 * identified by SHA1 message digests, need to be extracted, so we
769 * "resolve" the inode's streams anyway by allocating new entries. */
770 if (extract_flags & WIMLIB_EXTRACT_FLAG_FROM_PIPE)
772 ret = inode_resolve_streams(inode, lookup_table, force);
775 for (u32 i = 0; i <= inode->i_num_ads; i++) {
776 lte = inode_stream_lte_resolved(inode, i);
784 * For each dentry to be extracted, resolve all streams in the corresponding
785 * inode and set 'out_refcnt' in each to 0.
787 * Possible error codes: WIMLIB_ERR_RESOURCE_NOT_FOUND, WIMLIB_ERR_NOMEM.
790 dentry_list_resolve_streams(struct list_head *dentry_list,
791 struct apply_ctx *ctx)
793 struct wim_dentry *dentry;
796 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
797 ret = dentry_resolve_streams(dentry,
799 ctx->wim->lookup_table);
807 ref_stream(struct wim_lookup_table_entry *lte, u32 stream_idx,
808 struct wim_dentry *dentry, struct apply_ctx *ctx)
810 struct wim_inode *inode = dentry->d_inode;
811 struct stream_owner *stream_owners;
816 /* Tally the size only for each extraction of the stream (not hard
818 if (inode->i_visited && ctx->supported_features.hard_links)
821 ctx->progress.extract.total_bytes += lte->size;
822 ctx->progress.extract.num_streams++;
824 if (inode->i_visited)
827 /* Add stream to the dentry_list only one time, even if it's going
828 * to be extracted to multiple inodes. */
829 if (lte->out_refcnt == 0) {
830 list_add_tail(<e->extraction_list, &ctx->stream_list);
831 ctx->num_streams_remaining++;
834 /* If inode not yet been visited, append it to the stream_owners array. */
835 if (lte->out_refcnt < ARRAY_LEN(lte->inline_stream_owners)) {
836 stream_owners = lte->inline_stream_owners;
838 struct stream_owner *prev_stream_owners;
839 size_t alloc_stream_owners;
841 if (lte->out_refcnt == ARRAY_LEN(lte->inline_stream_owners)) {
842 prev_stream_owners = NULL;
843 alloc_stream_owners = ARRAY_LEN(lte->inline_stream_owners);
845 prev_stream_owners = lte->stream_owners;
846 alloc_stream_owners = lte->alloc_stream_owners;
849 if (lte->out_refcnt == alloc_stream_owners) {
850 alloc_stream_owners *= 2;
851 stream_owners = REALLOC(prev_stream_owners,
852 alloc_stream_owners *
853 sizeof(stream_owners[0]));
855 return WIMLIB_ERR_NOMEM;
856 if (!prev_stream_owners) {
857 memcpy(stream_owners,
858 lte->inline_stream_owners,
859 sizeof(lte->inline_stream_owners));
861 lte->stream_owners = stream_owners;
862 lte->alloc_stream_owners = alloc_stream_owners;
864 stream_owners = lte->stream_owners;
866 stream_owners[lte->out_refcnt].inode = inode;
867 if (stream_idx == 0) {
868 stream_owners[lte->out_refcnt].stream_name = NULL;
870 stream_owners[lte->out_refcnt].stream_name =
871 inode->i_ads_entries[stream_idx - 1].stream_name;
878 dentry_ref_streams(struct wim_dentry *dentry, struct apply_ctx *ctx)
880 struct wim_inode *inode = dentry->d_inode;
883 /* The unnamed data stream will always be extracted, except in an
885 if (!inode_is_encrypted_directory(inode)) {
887 struct wim_lookup_table_entry *stream;
889 stream = inode_unnamed_stream_resolved(inode, &stream_idx);
890 ret = ref_stream(stream, stream_idx, dentry, ctx);
895 /* Named data streams will be extracted only if supported in the current
896 * extraction mode and volume, and to avoid complications, if not doing
897 * a linked extraction. */
898 if (ctx->supported_features.named_data_streams) {
899 for (u16 i = 0; i < inode->i_num_ads; i++) {
900 if (!ads_entry_is_named_stream(&inode->i_ads_entries[i]))
902 ret = ref_stream(inode->i_ads_entries[i].lte, i + 1,
908 inode->i_visited = 1;
913 * For each dentry to be extracted, iterate through the data streams of the
914 * corresponding inode. For each such stream that is not to be ignored due to
915 * the supported features or extraction flags, add it to the list of streams to
916 * be extracted (ctx->stream_list) if not already done so.
918 * Also builds a mapping from each stream to the inodes referencing it.
920 * This also initializes the extract progress info with byte and stream
923 * ctx->supported_features must be filled in.
925 * Possible error codes: WIMLIB_ERR_NOMEM.
928 dentry_list_ref_streams(struct list_head *dentry_list, struct apply_ctx *ctx)
930 struct wim_dentry *dentry;
933 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
934 ret = dentry_ref_streams(dentry, ctx);
938 list_for_each_entry(dentry, dentry_list, d_extraction_list_node)
939 dentry->d_inode->i_visited = 0;
944 dentry_list_build_inode_alias_lists(struct list_head *dentry_list)
946 struct wim_dentry *dentry;
947 struct wim_inode *inode;
949 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
950 inode = dentry->d_inode;
951 if (!inode->i_visited)
952 INIT_LIST_HEAD(&inode->i_extraction_aliases);
953 list_add_tail(&dentry->d_extraction_alias_node,
954 &inode->i_extraction_aliases);
955 inode->i_visited = 1;
957 list_for_each_entry(dentry, dentry_list, d_extraction_list_node)
958 dentry->d_inode->i_visited = 0;
962 inode_tally_features(const struct wim_inode *inode,
963 struct wim_features *features)
965 if (inode->i_attributes & FILE_ATTRIBUTE_ARCHIVE)
966 features->archive_files++;
967 if (inode->i_attributes & FILE_ATTRIBUTE_HIDDEN)
968 features->hidden_files++;
969 if (inode->i_attributes & FILE_ATTRIBUTE_SYSTEM)
970 features->system_files++;
971 if (inode->i_attributes & FILE_ATTRIBUTE_COMPRESSED)
972 features->compressed_files++;
973 if (inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED) {
974 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)
975 features->encrypted_directories++;
977 features->encrypted_files++;
979 if (inode->i_attributes & FILE_ATTRIBUTE_NOT_CONTENT_INDEXED)
980 features->not_context_indexed_files++;
981 if (inode->i_attributes & FILE_ATTRIBUTE_SPARSE_FILE)
982 features->sparse_files++;
983 if (inode_has_named_stream(inode))
984 features->named_data_streams++;
985 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
986 features->reparse_points++;
987 if (inode_is_symlink(inode))
988 features->symlink_reparse_points++;
990 features->other_reparse_points++;
992 if (inode->i_security_id != -1)
993 features->security_descriptors++;
994 if (inode_has_unix_data(inode))
995 features->unix_data++;
998 /* Tally features necessary to extract a dentry and the corresponding inode. */
1000 dentry_tally_features(struct wim_dentry *dentry, struct wim_features *features)
1002 struct wim_inode *inode = dentry->d_inode;
1004 if (dentry_has_short_name(dentry))
1005 features->short_names++;
1007 if (inode->i_visited) {
1008 features->hard_links++;
1010 inode_tally_features(inode, features);
1011 inode->i_visited = 1;
1015 /* Tally the features necessary to extract the specified dentries. */
1017 dentry_list_get_features(struct list_head *dentry_list,
1018 struct wim_features *features)
1020 struct wim_dentry *dentry;
1022 list_for_each_entry(dentry, dentry_list, d_extraction_list_node)
1023 dentry_tally_features(dentry, features);
1025 list_for_each_entry(dentry, dentry_list, d_extraction_list_node)
1026 dentry->d_inode->i_visited = 0;
1030 do_feature_check(const struct wim_features *required_features,
1031 const struct wim_features *supported_features,
1034 /* File attributes. */
1035 if (!(extract_flags & WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES)) {
1036 /* Note: Don't bother the user about FILE_ATTRIBUTE_ARCHIVE.
1037 * We're an archive program, so theoretically we can do what we
1040 if (required_features->hidden_files &&
1041 !supported_features->hidden_files)
1042 WARNING("Ignoring FILE_ATTRIBUTE_HIDDEN of %lu files",
1043 required_features->hidden_files);
1045 if (required_features->system_files &&
1046 !supported_features->system_files)
1047 WARNING("Ignoring FILE_ATTRIBUTE_SYSTEM of %lu files",
1048 required_features->system_files);
1050 if (required_features->compressed_files &&
1051 !supported_features->compressed_files)
1052 WARNING("Ignoring FILE_ATTRIBUTE_COMPRESSED of %lu files",
1053 required_features->compressed_files);
1055 if (required_features->not_context_indexed_files &&
1056 !supported_features->not_context_indexed_files)
1057 WARNING("Ignoring FILE_ATTRIBUTE_NOT_CONTENT_INDEXED of %lu files",
1058 required_features->not_context_indexed_files);
1060 if (required_features->sparse_files &&
1061 !supported_features->sparse_files)
1062 WARNING("Ignoring FILE_ATTRIBUTE_SPARSE_FILE of %lu files",
1063 required_features->sparse_files);
1065 if (required_features->encrypted_directories &&
1066 !supported_features->encrypted_directories)
1067 WARNING("Ignoring FILE_ATTRIBUTE_ENCRYPTED of %lu directories",
1068 required_features->encrypted_directories);
1071 /* Encrypted files. */
1072 if (required_features->encrypted_files &&
1073 !supported_features->encrypted_files)
1074 WARNING("Ignoring %lu encrypted files",
1075 required_features->encrypted_files);
1077 /* Named data streams. */
1078 if (required_features->named_data_streams &&
1079 (!supported_features->named_data_streams))
1080 WARNING("Ignoring named data streams of %lu files",
1081 required_features->named_data_streams);
1084 if (required_features->hard_links && !supported_features->hard_links)
1085 WARNING("Extracting %lu hard links as independent files",
1086 required_features->hard_links);
1088 /* Symbolic links and reparse points. */
1089 if ((extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS) &&
1090 required_features->symlink_reparse_points &&
1091 !supported_features->symlink_reparse_points &&
1092 !supported_features->reparse_points)
1094 ERROR("Extraction backend does not support symbolic links!");
1095 return WIMLIB_ERR_UNSUPPORTED;
1097 if (required_features->reparse_points &&
1098 !supported_features->reparse_points)
1100 if (supported_features->symlink_reparse_points) {
1101 if (required_features->other_reparse_points) {
1102 WARNING("Ignoring %lu non-symlink/junction "
1103 "reparse point files",
1104 required_features->other_reparse_points);
1107 WARNING("Ignoring %lu reparse point files",
1108 required_features->reparse_points);
1112 /* Security descriptors. */
1113 if (((extract_flags & (WIMLIB_EXTRACT_FLAG_STRICT_ACLS |
1114 WIMLIB_EXTRACT_FLAG_UNIX_DATA))
1115 == WIMLIB_EXTRACT_FLAG_STRICT_ACLS) &&
1116 required_features->security_descriptors &&
1117 !supported_features->security_descriptors)
1119 ERROR("Extraction backend does not support security descriptors!");
1120 return WIMLIB_ERR_UNSUPPORTED;
1122 if (!(extract_flags & WIMLIB_EXTRACT_FLAG_NO_ACLS) &&
1123 required_features->security_descriptors &&
1124 !supported_features->security_descriptors)
1125 WARNING("Ignoring Windows NT security descriptors of %lu files",
1126 required_features->security_descriptors);
1129 if ((extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) &&
1130 required_features->unix_data && !supported_features->unix_data)
1132 ERROR("Extraction backend does not support UNIX data!");
1133 return WIMLIB_ERR_UNSUPPORTED;
1137 if (required_features->short_names &&
1138 !supported_features->short_names)
1140 if (extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_SHORT_NAMES) {
1141 ERROR("Extraction backend does not support DOS names!");
1142 return WIMLIB_ERR_UNSUPPORTED;
1144 WARNING("Ignoring DOS names of %lu files",
1145 required_features->short_names);
1149 if ((extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_TIMESTAMPS) &&
1150 !supported_features->timestamps)
1152 ERROR("Extraction backend does not support timestamps!");
1153 return WIMLIB_ERR_UNSUPPORTED;
1159 static const struct apply_operations *
1160 select_apply_operations(int extract_flags)
1163 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS)
1164 return &ntfs_3g_apply_ops;
1167 return &win32_apply_ops;
1169 return &unix_apply_ops;
1174 extract_trees(WIMStruct *wim, struct wim_dentry **trees, size_t num_trees,
1175 const tchar *target, int extract_flags)
1177 const struct apply_operations *ops;
1178 struct apply_ctx *ctx;
1180 LIST_HEAD(dentry_list);
1182 if (extract_flags & WIMLIB_EXTRACT_FLAG_TO_STDOUT) {
1183 ret = extract_dentries_to_stdout(trees, num_trees,
1188 num_trees = remove_duplicate_trees(trees, num_trees);
1189 num_trees = remove_contained_trees(trees, num_trees);
1191 ops = select_apply_operations(extract_flags);
1193 if (num_trees > 1 && ops->single_tree_only) {
1194 ERROR("Extracting multiple directory trees "
1195 "at once is not supported in %s extraction mode!",
1197 ret = WIMLIB_ERR_UNSUPPORTED;
1201 ctx = CALLOC(1, ops->context_size);
1203 ret = WIMLIB_ERR_NOMEM;
1208 ctx->target = target;
1209 ctx->target_nchars = tstrlen(target);
1210 ctx->extract_flags = extract_flags;
1211 if (ctx->wim->progfunc) {
1212 ctx->progfunc = ctx->wim->progfunc;
1213 ctx->progctx = ctx->wim->progctx;
1214 ctx->progress.extract.image = wim->current_image;
1215 ctx->progress.extract.extract_flags = (extract_flags &
1216 WIMLIB_EXTRACT_MASK_PUBLIC);
1217 ctx->progress.extract.wimfile_name = wim->filename;
1218 ctx->progress.extract.image_name = wimlib_get_image_name(wim,
1219 wim->current_image);
1220 ctx->progress.extract.target = target;
1222 INIT_LIST_HEAD(&ctx->stream_list);
1224 ret = (*ops->get_supported_features)(target, &ctx->supported_features);
1228 build_dentry_list(&dentry_list, trees, num_trees,
1230 WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE));
1232 dentry_list_get_features(&dentry_list, &ctx->required_features);
1234 ret = do_feature_check(&ctx->required_features, &ctx->supported_features,
1235 ctx->extract_flags);
1239 ret = dentry_list_calculate_extraction_names(&dentry_list, ctx);
1243 ret = dentry_list_resolve_streams(&dentry_list, ctx);
1247 ret = dentry_list_ref_streams(&dentry_list, ctx);
1251 dentry_list_build_inode_alias_lists(&dentry_list);
1253 if (extract_flags & WIMLIB_EXTRACT_FLAG_FROM_PIPE) {
1254 /* When extracting from a pipe, the number of bytes of data to
1255 * extract can't be determined in the normal way (examining the
1256 * lookup table), since at this point all we have is a set of
1257 * SHA1 message digests of streams that need to be extracted.
1258 * However, we can get a reasonably accurate estimate by taking
1259 * <TOTALBYTES> from the corresponding <IMAGE> in the WIM XML
1260 * data. This does assume that a full image is being extracted,
1261 * but currently there is no API for doing otherwise. (Also,
1262 * subtract <HARDLINKBYTES> from this if hard links are
1263 * supported by the extraction mode.) */
1264 ctx->progress.extract.total_bytes =
1265 wim_info_get_image_total_bytes(wim->wim_info,
1266 wim->current_image);
1267 if (ctx->supported_features.hard_links) {
1268 ctx->progress.extract.total_bytes -=
1269 wim_info_get_image_hard_link_bytes(wim->wim_info,
1270 wim->current_image);
1274 ret = extract_progress(ctx,
1275 ((extract_flags & WIMLIB_EXTRACT_FLAG_IMAGEMODE) ?
1276 WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN :
1277 WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN));
1281 ret = (*ops->extract)(&dentry_list, ctx);
1285 if (ctx->progress.extract.completed_bytes <
1286 ctx->progress.extract.total_bytes)
1288 ctx->progress.extract.completed_bytes =
1289 ctx->progress.extract.total_bytes;
1290 ret = extract_progress(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS);
1295 ret = extract_progress(ctx,
1296 ((extract_flags & WIMLIB_EXTRACT_FLAG_IMAGEMODE) ?
1297 WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END :
1298 WIMLIB_PROGRESS_MSG_EXTRACT_TREE_END));
1300 destroy_stream_list(&ctx->stream_list);
1301 destroy_dentry_list(&dentry_list);
1308 mkdir_if_needed(const tchar *target)
1311 if (tstat(target, &stbuf)) {
1312 if (errno == ENOENT) {
1313 if (tmkdir(target, 0755)) {
1314 ERROR_WITH_ERRNO("Failed to create directory "
1315 "\"%"TS"\"", target);
1316 return WIMLIB_ERR_MKDIR;
1319 ERROR_WITH_ERRNO("Failed to stat \"%"TS"\"", target);
1320 return WIMLIB_ERR_STAT;
1322 } else if (!S_ISDIR(stbuf.st_mode)) {
1323 ERROR("\"%"TS"\" is not a directory", target);
1324 return WIMLIB_ERR_NOTDIR;
1329 /* Make sure the extraction flags make sense, and update them if needed. */
1331 check_extract_flags(const WIMStruct *wim, int *extract_flags_p)
1333 int extract_flags = *extract_flags_p;
1335 /* Check for invalid flag combinations */
1337 if ((extract_flags &
1338 (WIMLIB_EXTRACT_FLAG_NO_ACLS |
1339 WIMLIB_EXTRACT_FLAG_STRICT_ACLS)) == (WIMLIB_EXTRACT_FLAG_NO_ACLS |
1340 WIMLIB_EXTRACT_FLAG_STRICT_ACLS))
1341 return WIMLIB_ERR_INVALID_PARAM;
1343 if ((extract_flags &
1344 (WIMLIB_EXTRACT_FLAG_RPFIX |
1345 WIMLIB_EXTRACT_FLAG_NORPFIX)) == (WIMLIB_EXTRACT_FLAG_RPFIX |
1346 WIMLIB_EXTRACT_FLAG_NORPFIX))
1347 return WIMLIB_ERR_INVALID_PARAM;
1349 #ifndef WITH_NTFS_3G
1350 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1351 ERROR("wimlib was compiled without support for NTFS-3g, so\n"
1352 " it cannot apply a WIM image directly to an NTFS volume.");
1353 return WIMLIB_ERR_UNSUPPORTED;
1358 if (extract_flags & WIMLIB_EXTRACT_FLAG_WIMBOOT) {
1359 ERROR("WIMBoot extraction is only supported on Windows!");
1360 return WIMLIB_ERR_UNSUPPORTED;
1364 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_RPFIX |
1365 WIMLIB_EXTRACT_FLAG_NORPFIX |
1366 WIMLIB_EXTRACT_FLAG_IMAGEMODE)) ==
1367 WIMLIB_EXTRACT_FLAG_IMAGEMODE)
1369 /* For full-image extraction, do reparse point fixups by default
1370 * if the WIM header says they are enabled. */
1371 if (wim->hdr.flags & WIM_HDR_FLAG_RP_FIX)
1372 extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
1375 *extract_flags_p = extract_flags;
1380 get_wildcard_flags(int extract_flags)
1382 u32 wildcard_flags = 0;
1384 if (extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_GLOB)
1385 wildcard_flags |= WILDCARD_FLAG_ERROR_IF_NO_MATCH;
1387 wildcard_flags |= WILDCARD_FLAG_WARN_IF_NO_MATCH;
1389 if (default_ignore_case)
1390 wildcard_flags |= WILDCARD_FLAG_CASE_INSENSITIVE;
1392 return wildcard_flags;
1395 struct append_dentry_ctx {
1396 struct wim_dentry **dentries;
1397 size_t num_dentries;
1398 size_t num_alloc_dentries;
1402 append_dentry_cb(struct wim_dentry *dentry, void *_ctx)
1404 struct append_dentry_ctx *ctx = _ctx;
1406 if (ctx->num_dentries == ctx->num_alloc_dentries) {
1407 struct wim_dentry **new_dentries;
1410 new_length = max(ctx->num_alloc_dentries + 8,
1411 ctx->num_alloc_dentries * 3 / 2);
1412 new_dentries = REALLOC(ctx->dentries,
1413 new_length * sizeof(ctx->dentries[0]));
1414 if (new_dentries == NULL)
1415 return WIMLIB_ERR_NOMEM;
1416 ctx->dentries = new_dentries;
1417 ctx->num_alloc_dentries = new_length;
1419 ctx->dentries[ctx->num_dentries++] = dentry;
1424 do_wimlib_extract_paths(WIMStruct *wim, int image, const tchar *target,
1425 const tchar * const *paths, size_t num_paths,
1429 struct wim_dentry **trees;
1432 if (wim == NULL || target == NULL || target[0] == T('\0') ||
1433 (num_paths != 0 && paths == NULL))
1434 return WIMLIB_ERR_INVALID_PARAM;
1436 ret = check_extract_flags(wim, &extract_flags);
1440 ret = select_wim_image(wim, image);
1444 ret = wim_checksum_unhashed_streams(wim);
1448 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_NTFS |
1449 WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE)) ==
1450 (WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE))
1452 ret = mkdir_if_needed(target);
1457 if (extract_flags & WIMLIB_EXTRACT_FLAG_GLOB_PATHS) {
1459 struct append_dentry_ctx append_dentry_ctx = {
1462 .num_alloc_dentries = 0,
1465 u32 wildcard_flags = get_wildcard_flags(extract_flags);
1467 for (size_t i = 0; i < num_paths; i++) {
1468 tchar *path = canonicalize_wim_path(paths[i]);
1470 ret = WIMLIB_ERR_NOMEM;
1471 trees = append_dentry_ctx.dentries;
1472 goto out_free_trees;
1474 ret = expand_wildcard(wim, path,
1480 trees = append_dentry_ctx.dentries;
1481 goto out_free_trees;
1484 trees = append_dentry_ctx.dentries;
1485 num_trees = append_dentry_ctx.num_dentries;
1487 trees = MALLOC(num_paths * sizeof(trees[0]));
1489 return WIMLIB_ERR_NOMEM;
1491 for (size_t i = 0; i < num_paths; i++) {
1493 tchar *path = canonicalize_wim_path(paths[i]);
1495 ret = WIMLIB_ERR_NOMEM;
1496 goto out_free_trees;
1499 trees[i] = get_dentry(wim, path,
1500 WIMLIB_CASE_PLATFORM_DEFAULT);
1502 if (trees[i] == NULL) {
1503 ERROR("Path \"%"TS"\" does not exist "
1505 paths[i], wim->current_image);
1506 ret = WIMLIB_ERR_PATH_DOES_NOT_EXIST;
1507 goto out_free_trees;
1510 num_trees = num_paths;
1513 if (num_trees == 0) {
1515 goto out_free_trees;
1518 ret = extract_trees(wim, trees, num_trees, target, extract_flags);
1525 extract_single_image(WIMStruct *wim, int image,
1526 const tchar *target, int extract_flags)
1528 const tchar *path = WIMLIB_WIM_ROOT_PATH;
1529 extract_flags |= WIMLIB_EXTRACT_FLAG_IMAGEMODE;
1530 return do_wimlib_extract_paths(wim, image, target, &path, 1, extract_flags);
1533 static const tchar * const filename_forbidden_chars =
1542 /* This function checks if it is okay to use a WIM image's name as a directory
1545 image_name_ok_as_dir(const tchar *image_name)
1547 return image_name && *image_name &&
1548 !tstrpbrk(image_name, filename_forbidden_chars) &&
1549 tstrcmp(image_name, T(".")) &&
1550 tstrcmp(image_name, T(".."));
1553 /* Extracts all images from the WIM to the directory @target, with the images
1554 * placed in subdirectories named by their image names. */
1556 extract_all_images(WIMStruct *wim, const tchar *target, int extract_flags)
1558 size_t image_name_max_len = max(xml_get_max_image_name_len(wim), 20);
1559 size_t output_path_len = tstrlen(target);
1560 tchar buf[output_path_len + 1 + image_name_max_len + 1];
1563 const tchar *image_name;
1565 extract_flags |= WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
1567 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1568 ERROR("Cannot extract multiple images in NTFS extraction mode.");
1569 return WIMLIB_ERR_INVALID_PARAM;
1572 ret = mkdir_if_needed(target);
1575 tmemcpy(buf, target, output_path_len);
1576 buf[output_path_len] = OS_PREFERRED_PATH_SEPARATOR;
1577 for (image = 1; image <= wim->hdr.image_count; image++) {
1578 image_name = wimlib_get_image_name(wim, image);
1579 if (image_name_ok_as_dir(image_name)) {
1580 tstrcpy(buf + output_path_len + 1, image_name);
1582 /* Image name is empty or contains forbidden characters.
1583 * Use image number instead. */
1584 tsprintf(buf + output_path_len + 1, T("%d"), image);
1586 ret = extract_single_image(wim, image, buf, extract_flags);
1594 do_wimlib_extract_image(WIMStruct *wim, int image, const tchar *target,
1597 if (extract_flags & (WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE |
1598 WIMLIB_EXTRACT_FLAG_TO_STDOUT |
1599 WIMLIB_EXTRACT_FLAG_GLOB_PATHS))
1600 return WIMLIB_ERR_INVALID_PARAM;
1602 if (image == WIMLIB_ALL_IMAGES)
1603 return extract_all_images(wim, target, extract_flags);
1605 return extract_single_image(wim, image, target, extract_flags);
1609 /****************************************************************************
1611 ****************************************************************************/
1614 wimlib_extract_paths(WIMStruct *wim, int image, const tchar *target,
1615 const tchar * const *paths, size_t num_paths,
1618 if (extract_flags & ~WIMLIB_EXTRACT_MASK_PUBLIC)
1619 return WIMLIB_ERR_INVALID_PARAM;
1621 return do_wimlib_extract_paths(wim, image, target, paths, num_paths,
1626 wimlib_extract_pathlist(WIMStruct *wim, int image, const tchar *target,
1627 const tchar *path_list_file, int extract_flags)
1634 ret = read_path_list_file(path_list_file, &paths, &num_paths, &mem);
1636 ERROR("Failed to read path list file \"%"TS"\"",
1641 ret = wimlib_extract_paths(wim, image, target,
1642 (const tchar * const *)paths, num_paths,
1650 wimlib_extract_image_from_pipe_with_progress(int pipe_fd,
1651 const tchar *image_num_or_name,
1652 const tchar *target,
1654 wimlib_progress_func_t progfunc,
1659 struct filedes *in_fd;
1663 if (extract_flags & ~WIMLIB_EXTRACT_MASK_PUBLIC)
1664 return WIMLIB_ERR_INVALID_PARAM;
1666 /* Read the WIM header from the pipe and get a WIMStruct to represent
1667 * the pipable WIM. Caveats: Unlike getting a WIMStruct with
1668 * wimlib_open_wim(), getting a WIMStruct in this way will result in
1669 * an empty lookup table, no XML data read, and no filename set. */
1670 ret = open_wim_as_WIMStruct(&pipe_fd, WIMLIB_OPEN_FLAG_FROM_PIPE, &pwm,
1675 /* Sanity check to make sure this is a pipable WIM. */
1676 if (pwm->hdr.magic != PWM_MAGIC) {
1677 ERROR("The WIM being read from file descriptor %d "
1678 "is not pipable!", pipe_fd);
1679 ret = WIMLIB_ERR_NOT_PIPABLE;
1680 goto out_wimlib_free;
1683 /* Sanity check to make sure the first part of a pipable split WIM is
1684 * sent over the pipe first. */
1685 if (pwm->hdr.part_number != 1) {
1686 ERROR("The first part of the split WIM must be "
1687 "sent over the pipe first.");
1688 ret = WIMLIB_ERR_INVALID_PIPABLE_WIM;
1689 goto out_wimlib_free;
1692 in_fd = &pwm->in_fd;
1693 wimlib_assert(in_fd->offset == WIM_HEADER_DISK_SIZE);
1695 /* As mentioned, the WIMStruct we created from the pipe does not have
1696 * XML data yet. Fix this by reading the extra copy of the XML data
1697 * that directly follows the header in pipable WIMs. (Note: see
1698 * write_pipable_wim() for more details about the format of pipable
1701 struct wim_lookup_table_entry xml_lte;
1702 struct wim_resource_spec xml_rspec;
1703 ret = read_pwm_stream_header(pwm, &xml_lte, &xml_rspec, 0, NULL);
1705 goto out_wimlib_free;
1707 if (!(xml_lte.flags & WIM_RESHDR_FLAG_METADATA))
1709 ERROR("Expected XML data, but found non-metadata "
1711 ret = WIMLIB_ERR_INVALID_PIPABLE_WIM;
1712 goto out_wimlib_free;
1715 wim_res_spec_to_hdr(&xml_rspec, &pwm->hdr.xml_data_reshdr);
1717 ret = read_wim_xml_data(pwm);
1719 goto out_wimlib_free;
1721 if (wim_info_get_num_images(pwm->wim_info) != pwm->hdr.image_count) {
1722 ERROR("Image count in XML data is not the same as in WIM header.");
1723 ret = WIMLIB_ERR_IMAGE_COUNT;
1724 goto out_wimlib_free;
1728 /* Get image index (this may use the XML data that was just read to
1729 * resolve an image name). */
1730 if (image_num_or_name) {
1731 image = wimlib_resolve_image(pwm, image_num_or_name);
1732 if (image == WIMLIB_NO_IMAGE) {
1733 ERROR("\"%"TS"\" is not a valid image in the pipable WIM!",
1735 ret = WIMLIB_ERR_INVALID_IMAGE;
1736 goto out_wimlib_free;
1737 } else if (image == WIMLIB_ALL_IMAGES) {
1738 ERROR("Applying all images from a pipe is not supported!");
1739 ret = WIMLIB_ERR_INVALID_IMAGE;
1740 goto out_wimlib_free;
1743 if (pwm->hdr.image_count != 1) {
1744 ERROR("No image was specified, but the pipable WIM "
1745 "did not contain exactly 1 image");
1746 ret = WIMLIB_ERR_INVALID_IMAGE;
1747 goto out_wimlib_free;
1752 /* Load the needed metadata resource. */
1753 for (i = 1; i <= pwm->hdr.image_count; i++) {
1754 struct wim_lookup_table_entry *metadata_lte;
1755 struct wim_image_metadata *imd;
1756 struct wim_resource_spec *metadata_rspec;
1758 metadata_lte = new_lookup_table_entry();
1759 if (metadata_lte == NULL) {
1760 ret = WIMLIB_ERR_NOMEM;
1761 goto out_wimlib_free;
1763 metadata_rspec = MALLOC(sizeof(struct wim_resource_spec));
1764 if (metadata_rspec == NULL) {
1765 ret = WIMLIB_ERR_NOMEM;
1766 free_lookup_table_entry(metadata_lte);
1767 goto out_wimlib_free;
1770 ret = read_pwm_stream_header(pwm, metadata_lte, metadata_rspec, 0, NULL);
1771 imd = pwm->image_metadata[i - 1];
1772 imd->metadata_lte = metadata_lte;
1774 FREE(metadata_rspec);
1775 goto out_wimlib_free;
1778 if (!(metadata_lte->flags & WIM_RESHDR_FLAG_METADATA)) {
1779 ERROR("Expected metadata resource, but found "
1780 "non-metadata stream.");
1781 ret = WIMLIB_ERR_INVALID_PIPABLE_WIM;
1782 goto out_wimlib_free;
1786 /* Metadata resource is for the image being extracted.
1787 * Parse it and save the metadata in memory. */
1788 ret = read_metadata_resource(pwm, imd);
1790 goto out_wimlib_free;
1793 /* Metadata resource is not for the image being
1794 * extracted. Skip over it. */
1795 ret = skip_wim_stream(metadata_lte);
1797 goto out_wimlib_free;
1800 /* Extract the image. */
1801 extract_flags |= WIMLIB_EXTRACT_FLAG_FROM_PIPE;
1802 ret = do_wimlib_extract_image(pwm, image, target, extract_flags);
1803 /* Clean up and return. */
1811 wimlib_extract_image_from_pipe(int pipe_fd, const tchar *image_num_or_name,
1812 const tchar *target, int extract_flags)
1814 return wimlib_extract_image_from_pipe_with_progress(pipe_fd,
1823 wimlib_extract_image(WIMStruct *wim, int image, const tchar *target,
1826 if (extract_flags & ~WIMLIB_EXTRACT_MASK_PUBLIC)
1827 return WIMLIB_ERR_INVALID_PARAM;
1828 return do_wimlib_extract_image(wim, image, target, extract_flags);