4 * Support for extracting WIM images, or files or directories contained in a WIM
9 * Copyright (C) 2012, 2013, 2014, 2015 Eric Biggers
11 * This file is free software; you can redistribute it and/or modify it under
12 * the terms of the GNU Lesser General Public License as published by the Free
13 * Software Foundation; either version 3 of the License, or (at your option) any
16 * This file is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this file; if not, see http://www.gnu.org/licenses/.
26 * This file provides the API functions wimlib_extract_image(),
27 * wimlib_extract_image_from_pipe(), wimlib_extract_paths(), and
28 * wimlib_extract_pathlist(). Internally, all end up calling
29 * do_wimlib_extract_paths() and extract_trees().
31 * Although wimlib supports multiple extraction modes/backends (NTFS-3g, UNIX,
32 * Win32), this file does not itself have code to extract files or directories
33 * to any specific target; instead, it handles generic functionality and relies
34 * on lower-level callback functions declared in `struct apply_operations' to do
35 * the actual extraction.
47 #include "wimlib/apply.h"
48 #include "wimlib/assert.h"
49 #include "wimlib/blob_table.h"
50 #include "wimlib/dentry.h"
51 #include "wimlib/encoding.h"
52 #include "wimlib/endianness.h"
53 #include "wimlib/error.h"
54 #include "wimlib/metadata.h"
55 #include "wimlib/pathlist.h"
56 #include "wimlib/paths.h"
57 #include "wimlib/pattern.h"
58 #include "wimlib/reparse.h"
59 #include "wimlib/resource.h"
60 #include "wimlib/security.h"
61 #include "wimlib/unix_data.h"
62 #include "wimlib/wim.h"
63 #include "wimlib/win32.h" /* for realpath() equivalent */
64 #include "wimlib/xml.h"
66 #define WIMLIB_EXTRACT_FLAG_FROM_PIPE 0x80000000
67 #define WIMLIB_EXTRACT_FLAG_IMAGEMODE 0x40000000
69 /* Keep in sync with wimlib.h */
70 #define WIMLIB_EXTRACT_MASK_PUBLIC \
71 (WIMLIB_EXTRACT_FLAG_NTFS | \
72 WIMLIB_EXTRACT_FLAG_UNIX_DATA | \
73 WIMLIB_EXTRACT_FLAG_NO_ACLS | \
74 WIMLIB_EXTRACT_FLAG_STRICT_ACLS | \
75 WIMLIB_EXTRACT_FLAG_RPFIX | \
76 WIMLIB_EXTRACT_FLAG_NORPFIX | \
77 WIMLIB_EXTRACT_FLAG_TO_STDOUT | \
78 WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES | \
79 WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS | \
80 WIMLIB_EXTRACT_FLAG_STRICT_TIMESTAMPS | \
81 WIMLIB_EXTRACT_FLAG_STRICT_SHORT_NAMES | \
82 WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS | \
83 WIMLIB_EXTRACT_FLAG_GLOB_PATHS | \
84 WIMLIB_EXTRACT_FLAG_STRICT_GLOB | \
85 WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES | \
86 WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE | \
87 WIMLIB_EXTRACT_FLAG_WIMBOOT)
89 /* Send WIMLIB_PROGRESS_MSG_EXTRACT_FILE_STRUCTURE or
90 * WIMLIB_PROGRESS_MSG_EXTRACT_METADATA. */
92 do_file_extract_progress(struct apply_ctx *ctx, enum wimlib_progress_msg msg)
94 ctx->count_until_file_progress = 500; /* Arbitrary value to limit calls */
95 return extract_progress(ctx, msg);
99 start_file_phase(struct apply_ctx *ctx, u64 end_file_count, enum wimlib_progress_msg msg)
101 ctx->progress.extract.current_file_count = 0;
102 ctx->progress.extract.end_file_count = end_file_count;
103 return do_file_extract_progress(ctx, msg);
107 start_file_structure_phase(struct apply_ctx *ctx, u64 end_file_count)
109 return start_file_phase(ctx, end_file_count, WIMLIB_PROGRESS_MSG_EXTRACT_FILE_STRUCTURE);
113 start_file_metadata_phase(struct apply_ctx *ctx, u64 end_file_count)
115 return start_file_phase(ctx, end_file_count, WIMLIB_PROGRESS_MSG_EXTRACT_METADATA);
119 end_file_phase(struct apply_ctx *ctx, enum wimlib_progress_msg msg)
121 ctx->progress.extract.current_file_count = ctx->progress.extract.end_file_count;
122 return do_file_extract_progress(ctx, msg);
126 end_file_structure_phase(struct apply_ctx *ctx)
128 return end_file_phase(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_FILE_STRUCTURE);
132 end_file_metadata_phase(struct apply_ctx *ctx)
134 return end_file_phase(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_METADATA);
137 #define PWM_FOUND_WIM_HDR (-1)
139 /* Read the header for a blob in a pipable WIM. If @pwm_hdr_ret is not NULL,
140 * also look for a pipable WIM header and return PWM_FOUND_WIM_HDR if found. */
142 read_pwm_blob_header(WIMStruct *pwm, u8 hash_ret[SHA1_HASH_SIZE],
143 struct wim_reshdr *reshdr_ret,
144 struct wim_header_disk *pwm_hdr_ret)
147 struct pwm_blob_hdr blob_hdr;
150 ret = full_read(&pwm->in_fd, &blob_hdr, sizeof(blob_hdr));
154 magic = le64_to_cpu(blob_hdr.magic);
156 if (magic == PWM_MAGIC && pwm_hdr_ret != NULL) {
157 memcpy(pwm_hdr_ret, &blob_hdr, sizeof(blob_hdr));
158 ret = full_read(&pwm->in_fd,
159 (u8 *)pwm_hdr_ret + sizeof(blob_hdr),
160 sizeof(*pwm_hdr_ret) - sizeof(blob_hdr));
163 return PWM_FOUND_WIM_HDR;
166 if (unlikely(magic != PWM_BLOB_MAGIC)) {
167 ERROR("Data read on pipe is invalid (expected blob header)");
168 return WIMLIB_ERR_INVALID_PIPABLE_WIM;
171 copy_hash(hash_ret, blob_hdr.hash);
173 reshdr_ret->size_in_wim = 0; /* Not available */
174 reshdr_ret->flags = le32_to_cpu(blob_hdr.flags);
175 reshdr_ret->offset_in_wim = pwm->in_fd.offset;
176 reshdr_ret->uncompressed_size = le64_to_cpu(blob_hdr.uncompressed_size);
178 if (unlikely(reshdr_ret->uncompressed_size == 0)) {
179 ERROR("Data read on pipe is invalid (resource is of 0 size)");
180 return WIMLIB_ERR_INVALID_PIPABLE_WIM;
186 if (ret == WIMLIB_ERR_UNEXPECTED_END_OF_FILE)
187 ERROR("The pipe ended before all needed data was sent!");
189 ERROR_WITH_ERRNO("Error reading pipable WIM from pipe");
194 read_blobs_from_pipe(struct apply_ctx *ctx, const struct read_blob_callbacks *cbs)
197 u8 hash[SHA1_HASH_SIZE];
198 struct wim_reshdr reshdr;
199 struct wim_header_disk pwm_hdr;
200 struct wim_resource_descriptor rdesc;
201 struct blob_descriptor *blob;
203 copy_guid(ctx->progress.extract.guid, ctx->wim->hdr.guid);
204 ctx->progress.extract.part_number = ctx->wim->hdr.part_number;
205 ctx->progress.extract.total_parts = ctx->wim->hdr.total_parts;
206 ret = extract_progress(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN);
210 while (ctx->num_blobs_remaining) {
212 ret = read_pwm_blob_header(ctx->wim, hash, &reshdr, &pwm_hdr);
214 if (ret == PWM_FOUND_WIM_HDR) {
215 u16 part_number = le16_to_cpu(pwm_hdr.part_number);
216 u16 total_parts = le16_to_cpu(pwm_hdr.total_parts);
218 if (part_number == ctx->progress.extract.part_number &&
219 total_parts == ctx->progress.extract.total_parts &&
220 guids_equal(pwm_hdr.guid, ctx->progress.extract.guid))
223 copy_guid(ctx->progress.extract.guid, pwm_hdr.guid);
224 ctx->progress.extract.part_number = part_number;
225 ctx->progress.extract.total_parts = total_parts;
226 ret = extract_progress(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN);
236 if (!(reshdr.flags & WIM_RESHDR_FLAG_METADATA)
237 && (blob = lookup_blob(ctx->wim->blob_table, hash))
238 && (blob->out_refcnt))
240 wim_reshdr_to_desc_and_blob(&reshdr, ctx->wim, &rdesc, blob);
241 ret = read_blob_with_sha1(blob, cbs);
242 blob_unset_is_located_in_wim_resource(blob);
245 ctx->num_blobs_remaining--;
247 wim_reshdr_to_desc(&reshdr, ctx->wim, &rdesc);
248 ret = skip_wim_resource(&rdesc);
257 /* Creates a temporary file opened for writing. The open file descriptor is
258 * returned in @fd_ret and its name is returned in @name_ret (dynamically
261 create_temporary_file(struct filedes *fd_ret, tchar **name_ret)
268 name = ttempnam(NULL, T("wimlib"));
270 ERROR_WITH_ERRNO("Failed to create temporary filename");
271 return WIMLIB_ERR_NOMEM;
274 open_flags = O_WRONLY | O_CREAT | O_EXCL | O_BINARY;
276 open_flags |= _O_SHORT_LIVED;
278 raw_fd = topen(name, open_flags, 0600);
281 if (errno == EEXIST) {
285 ERROR_WITH_ERRNO("Failed to create temporary file "
288 return WIMLIB_ERR_OPEN;
291 filedes_init(fd_ret, raw_fd);
297 begin_extract_blob_wrapper(struct blob_descriptor *blob, void *_ctx)
299 struct apply_ctx *ctx = _ctx;
301 ctx->cur_blob = blob;
302 ctx->cur_blob_offset = 0;
304 if (unlikely(blob->out_refcnt > MAX_OPEN_FILES))
305 return create_temporary_file(&ctx->tmpfile_fd, &ctx->tmpfile_name);
307 return call_begin_blob(blob, ctx->saved_cbs);
311 extract_chunk_wrapper(const void *chunk, size_t size, void *_ctx)
313 struct apply_ctx *ctx = _ctx;
314 union wimlib_progress_info *progress = &ctx->progress;
317 ctx->cur_blob_offset += size;
319 if (likely(ctx->supported_features.hard_links)) {
320 progress->extract.completed_bytes +=
321 (u64)size * ctx->cur_blob->out_refcnt;
322 if (ctx->cur_blob_offset == ctx->cur_blob->size)
323 progress->extract.completed_streams += ctx->cur_blob->out_refcnt;
325 const struct blob_extraction_target *targets =
326 blob_extraction_targets(ctx->cur_blob);
327 for (u32 i = 0; i < ctx->cur_blob->out_refcnt; i++) {
328 const struct wim_inode *inode = targets[i].inode;
329 const struct wim_dentry *dentry;
331 inode_for_each_extraction_alias(dentry, inode) {
332 progress->extract.completed_bytes += size;
333 if (ctx->cur_blob_offset == ctx->cur_blob->size)
334 progress->extract.completed_streams++;
338 if (progress->extract.completed_bytes >= ctx->next_progress) {
340 ret = extract_progress(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS);
344 set_next_progress(progress->extract.completed_bytes,
345 progress->extract.total_bytes,
346 &ctx->next_progress);
349 if (unlikely(filedes_valid(&ctx->tmpfile_fd))) {
350 /* Just extracting to temporary file for now. */
351 ret = full_write(&ctx->tmpfile_fd, chunk, size);
353 ERROR_WITH_ERRNO("Error writing data to "
354 "temporary file \"%"TS"\"",
360 return call_consume_chunk(chunk, size, ctx->saved_cbs);
363 /* Copy the blob's data from the temporary file to each of its targets.
365 * This is executed only in the very uncommon case that a blob is being
366 * extracted to more than MAX_OPEN_FILES targets! */
368 extract_from_tmpfile(const tchar *tmpfile_name,
369 const struct blob_descriptor *orig_blob,
370 const struct read_blob_callbacks *cbs)
372 struct blob_descriptor tmpfile_blob;
373 const struct blob_extraction_target *targets = blob_extraction_targets(orig_blob);
376 memcpy(&tmpfile_blob, orig_blob, sizeof(struct blob_descriptor));
377 tmpfile_blob.blob_location = BLOB_IN_FILE_ON_DISK;
378 tmpfile_blob.file_on_disk = (tchar *)tmpfile_name;
379 tmpfile_blob.out_refcnt = 1;
381 for (u32 i = 0; i < orig_blob->out_refcnt; i++) {
382 tmpfile_blob.inline_blob_extraction_targets[0] = targets[i];
383 ret = read_blob_with_cbs(&tmpfile_blob, cbs);
391 end_extract_blob_wrapper(struct blob_descriptor *blob, int status, void *_ctx)
393 struct apply_ctx *ctx = _ctx;
395 if (unlikely(filedes_valid(&ctx->tmpfile_fd))) {
396 filedes_close(&ctx->tmpfile_fd);
398 status = extract_from_tmpfile(ctx->tmpfile_name, blob,
400 filedes_invalidate(&ctx->tmpfile_fd);
401 tunlink(ctx->tmpfile_name);
402 FREE(ctx->tmpfile_name);
406 return call_end_blob(blob, status, ctx->saved_cbs);
410 * Read the list of blobs to extract and feed their data into the specified
411 * callback functions.
413 * This handles checksumming each blob.
415 * This also handles sending WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS.
417 * This also works if the WIM is being read from a pipe.
419 * This also will split up blobs that will need to be extracted to more than
420 * MAX_OPEN_FILES locations, as measured by the 'out_refcnt' of each blob.
421 * Therefore, the apply_operations implementation need not worry about running
422 * out of file descriptors, unless it might open more than one file descriptor
423 * per 'blob_extraction_target' (e.g. Win32 currently might because the
424 * destination file system might not support hard links).
427 extract_blob_list(struct apply_ctx *ctx, const struct read_blob_callbacks *cbs)
429 struct read_blob_callbacks wrapper_cbs = {
430 .begin_blob = begin_extract_blob_wrapper,
431 .consume_chunk = extract_chunk_wrapper,
432 .end_blob = end_extract_blob_wrapper,
435 ctx->saved_cbs = cbs;
436 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_FROM_PIPE) {
437 return read_blobs_from_pipe(ctx, &wrapper_cbs);
439 return read_blob_list(&ctx->blob_list,
440 offsetof(struct blob_descriptor,
442 &wrapper_cbs, VERIFY_BLOB_HASHES);
446 /* Extract a WIM dentry to standard output.
448 * This obviously doesn't make sense in all cases. We return an error if the
449 * dentry does not correspond to a regular file. Otherwise we extract the
450 * unnamed data stream only. */
452 extract_dentry_to_stdout(struct wim_dentry *dentry,
453 const struct blob_table *blob_table)
455 struct wim_inode *inode = dentry->d_inode;
456 struct blob_descriptor *blob;
457 struct filedes _stdout;
459 if (inode->i_attributes & (FILE_ATTRIBUTE_REPARSE_POINT |
460 FILE_ATTRIBUTE_DIRECTORY |
461 FILE_ATTRIBUTE_ENCRYPTED))
463 ERROR("\"%"TS"\" is not a regular file and therefore cannot be "
464 "extracted to standard output", dentry_full_path(dentry));
465 return WIMLIB_ERR_NOT_A_REGULAR_FILE;
468 blob = inode_get_blob_for_unnamed_data_stream(inode, blob_table);
470 const u8 *hash = inode_get_hash_of_unnamed_data_stream(inode);
471 if (!is_zero_hash(hash))
472 return blob_not_found_error(inode, hash);
476 filedes_init(&_stdout, STDOUT_FILENO);
477 return extract_blob_to_fd(blob, &_stdout);
481 extract_dentries_to_stdout(struct wim_dentry **dentries, size_t num_dentries,
482 const struct blob_table *blob_table)
484 for (size_t i = 0; i < num_dentries; i++) {
485 int ret = extract_dentry_to_stdout(dentries[i], blob_table);
492 /**********************************************************************/
495 * Removes duplicate dentries from the array.
497 * Returns the new number of dentries, packed at the front of the array.
500 remove_duplicate_trees(struct wim_dentry **trees, size_t num_trees)
503 for (i = 0; i < num_trees; i++) {
504 if (!trees[i]->d_tmp_flag) {
505 /* Found distinct dentry. */
506 trees[i]->d_tmp_flag = 1;
507 trees[j++] = trees[i];
510 for (i = 0; i < j; i++)
511 trees[i]->d_tmp_flag = 0;
516 * Remove dentries that are descendants of other dentries in the array.
518 * Returns the new number of dentries, packed at the front of the array.
521 remove_contained_trees(struct wim_dentry **trees, size_t num_trees)
524 for (i = 0; i < num_trees; i++)
525 trees[i]->d_tmp_flag = 1;
526 for (i = 0; i < num_trees; i++) {
527 struct wim_dentry *d = trees[i];
528 while (!dentry_is_root(d)) {
533 trees[j++] = trees[i];
537 trees[i]->d_tmp_flag = 0;
540 for (i = 0; i < j; i++)
541 trees[i]->d_tmp_flag = 0;
546 dentry_append_to_list(struct wim_dentry *dentry, void *_dentry_list)
548 struct list_head *dentry_list = _dentry_list;
549 list_add_tail(&dentry->d_extraction_list_node, dentry_list);
554 dentry_reset_extraction_list_node(struct wim_dentry *dentry)
556 dentry->d_extraction_list_node = (struct list_head){NULL, NULL};
560 dentry_delete_from_list(struct wim_dentry *dentry, void *_ignore)
562 if (will_extract_dentry(dentry)) {
563 list_del(&dentry->d_extraction_list_node);
564 dentry_reset_extraction_list_node(dentry);
570 * Build the preliminary list of dentries to be extracted.
572 * The list maintains the invariant that if d1 and d2 are in the list and d1 is
573 * an ancestor of d2, then d1 appears before d2 in the list.
576 build_dentry_list(struct list_head *dentry_list, struct wim_dentry **trees,
577 size_t num_trees, bool add_ancestors)
579 INIT_LIST_HEAD(dentry_list);
581 /* Add the trees recursively. */
582 for (size_t i = 0; i < num_trees; i++)
583 for_dentry_in_tree(trees[i], dentry_append_to_list, dentry_list);
585 /* If requested, add ancestors of the trees. */
587 for (size_t i = 0; i < num_trees; i++) {
588 struct wim_dentry *dentry = trees[i];
589 struct wim_dentry *ancestor;
590 struct list_head *place_after;
592 if (dentry_is_root(dentry))
595 place_after = dentry_list;
598 ancestor = ancestor->d_parent;
599 if (will_extract_dentry(ancestor)) {
600 place_after = &ancestor->d_extraction_list_node;
603 } while (!dentry_is_root(ancestor));
607 ancestor = ancestor->d_parent;
608 if (will_extract_dentry(ancestor))
610 list_add(&ancestor->d_extraction_list_node, place_after);
611 } while (!dentry_is_root(ancestor));
617 destroy_dentry_list(struct list_head *dentry_list)
619 struct wim_dentry *dentry, *tmp;
620 struct wim_inode *inode;
622 list_for_each_entry_safe(dentry, tmp, dentry_list, d_extraction_list_node) {
623 inode = dentry->d_inode;
624 dentry_reset_extraction_list_node(dentry);
625 inode->i_visited = 0;
626 inode->i_can_externally_back = 0;
627 if ((void *)dentry->d_extraction_name != (void *)dentry->d_name)
628 FREE(dentry->d_extraction_name);
629 dentry->d_extraction_name = NULL;
630 dentry->d_extraction_name_nchars = 0;
635 destroy_blob_list(struct list_head *blob_list)
637 struct blob_descriptor *blob;
639 list_for_each_entry(blob, blob_list, extraction_list)
640 if (blob->out_refcnt > ARRAY_LEN(blob->inline_blob_extraction_targets))
641 FREE(blob->blob_extraction_targets);
645 static const utf16lechar replacement_char = cpu_to_le16(0xfffd);
647 static const utf16lechar replacement_char = cpu_to_le16('?');
651 file_name_valid(utf16lechar *name, size_t num_chars, bool fix)
657 for (i = 0; i < num_chars; i++) {
658 switch (le16_to_cpu(name[i])) {
660 case '\x01'...'\x1F':
673 name[i] = replacement_char;
683 dentry_calculate_extraction_name(struct wim_dentry *dentry,
684 struct apply_ctx *ctx)
688 if (dentry_is_root(dentry))
692 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
693 dentry->d_extraction_name = dentry->d_name;
694 dentry->d_extraction_name_nchars = dentry->d_name_nbytes /
700 if (!ctx->supported_features.case_sensitive_filenames) {
701 struct wim_dentry *other;
702 list_for_each_entry(other, &dentry->d_ci_conflict_list,
705 if (will_extract_dentry(other)) {
706 if (ctx->extract_flags &
707 WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS) {
708 WARNING("\"%"TS"\" has the same "
709 "case-insensitive name as "
710 "\"%"TS"\"; extracting "
711 "dummy name instead",
712 dentry_full_path(dentry),
713 dentry_full_path(other));
716 WARNING("Not extracting \"%"TS"\": "
717 "has same case-insensitive "
719 dentry_full_path(dentry),
720 dentry_full_path(other));
727 if (file_name_valid(dentry->d_name, dentry->d_name_nbytes / 2, false)) {
729 ret = utf16le_get_tstr(dentry->d_name,
730 dentry->d_name_nbytes,
731 (const tchar **)&dentry->d_extraction_name,
733 dentry->d_extraction_name_nchars = nbytes / sizeof(tchar);
736 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES)
738 WARNING("\"%"TS"\" has an invalid filename "
739 "that is not supported on this platform; "
740 "extracting dummy name instead",
741 dentry_full_path(dentry));
744 WARNING("Not extracting \"%"TS"\": has an invalid filename "
745 "that is not supported on this platform",
746 dentry_full_path(dentry));
753 utf16lechar utf16_name_copy[dentry->d_name_nbytes / 2];
755 memcpy(utf16_name_copy, dentry->d_name, dentry->d_name_nbytes);
756 file_name_valid(utf16_name_copy, dentry->d_name_nbytes / 2, true);
758 const tchar *tchar_name;
761 ret = utf16le_get_tstr(utf16_name_copy,
762 dentry->d_name_nbytes,
763 &tchar_name, &tchar_nchars);
767 tchar_nchars /= sizeof(tchar);
769 size_t fixed_name_num_chars = tchar_nchars;
770 tchar fixed_name[tchar_nchars + 50];
772 tmemcpy(fixed_name, tchar_name, tchar_nchars);
773 fixed_name_num_chars += tsprintf(fixed_name + tchar_nchars,
774 T(" (invalid filename #%lu)"),
775 ++ctx->invalid_sequence);
777 utf16le_put_tstr(tchar_name);
779 dentry->d_extraction_name = TSTRDUP(fixed_name);
780 if (!dentry->d_extraction_name)
781 return WIMLIB_ERR_NOMEM;
782 dentry->d_extraction_name_nchars = fixed_name_num_chars;
787 for_dentry_in_tree(dentry, dentry_delete_from_list, NULL);
792 * Calculate the actual filename component at which each WIM dentry will be
793 * extracted, with special handling for dentries that are unsupported by the
794 * extraction backend or have invalid names.
796 * ctx->supported_features must be filled in.
798 * Possible error codes: WIMLIB_ERR_NOMEM, WIMLIB_ERR_INVALID_UTF16_STRING
801 dentry_list_calculate_extraction_names(struct list_head *dentry_list,
802 struct apply_ctx *ctx)
804 struct list_head *prev, *cur;
806 /* Can't use list_for_each_entry() because a call to
807 * dentry_calculate_extraction_name() may delete the current dentry and
808 * its children from the list. */
812 struct wim_dentry *dentry;
816 if (cur == dentry_list)
819 dentry = list_entry(cur, struct wim_dentry, d_extraction_list_node);
821 ret = dentry_calculate_extraction_name(dentry, ctx);
825 if (prev->next == cur)
828 ; /* Current dentry and its children (which follow in
829 the list) were deleted. prev stays the same. */
835 dentry_resolve_streams(struct wim_dentry *dentry, int extract_flags,
836 struct blob_table *blob_table)
838 struct wim_inode *inode = dentry->d_inode;
839 struct blob_descriptor *blob;
843 /* Special case: when extracting from a pipe, the WIM blob table is
844 * initially empty, so "resolving" an inode's streams is initially not
845 * possible. However, we still need to keep track of which blobs,
846 * identified by SHA-1 message digests, need to be extracted, so we
847 * "resolve" the inode's streams anyway by allocating a 'struct
848 * blob_descriptor' for each one. */
849 if (extract_flags & WIMLIB_EXTRACT_FLAG_FROM_PIPE)
851 ret = inode_resolve_streams(inode, blob_table, force);
854 for (unsigned i = 0; i < inode->i_num_streams; i++) {
855 blob = stream_blob_resolved(&inode->i_streams[i]);
857 blob->out_refcnt = 0;
863 * For each dentry to be extracted, resolve all streams in the corresponding
864 * inode and set 'out_refcnt' in all referenced blob_descriptors to 0.
866 * Possible error codes: WIMLIB_ERR_RESOURCE_NOT_FOUND, WIMLIB_ERR_NOMEM.
869 dentry_list_resolve_streams(struct list_head *dentry_list,
870 struct apply_ctx *ctx)
872 struct wim_dentry *dentry;
875 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
876 ret = dentry_resolve_streams(dentry,
878 ctx->wim->blob_table);
886 ref_stream(struct wim_inode_stream *strm, struct wim_dentry *dentry,
887 struct apply_ctx *ctx)
889 struct wim_inode *inode = dentry->d_inode;
890 struct blob_descriptor *blob = stream_blob_resolved(strm);
891 struct blob_extraction_target *targets;
896 /* Tally the size only for each actual extraction of the stream (not
897 * additional hard links to the inode). */
898 if (inode->i_visited && ctx->supported_features.hard_links)
901 ctx->progress.extract.total_bytes += blob->size;
902 ctx->progress.extract.total_streams++;
904 if (inode->i_visited)
907 /* Add each blob to 'ctx->blob_list' only one time, regardless of how
908 * many extraction targets it will have. */
909 if (blob->out_refcnt == 0) {
910 list_add_tail(&blob->extraction_list, &ctx->blob_list);
911 ctx->num_blobs_remaining++;
914 /* Set this stream as an extraction target of 'blob'. */
916 if (blob->out_refcnt < ARRAY_LEN(blob->inline_blob_extraction_targets)) {
917 targets = blob->inline_blob_extraction_targets;
919 struct blob_extraction_target *prev_targets;
920 size_t alloc_blob_extraction_targets;
922 if (blob->out_refcnt == ARRAY_LEN(blob->inline_blob_extraction_targets)) {
924 alloc_blob_extraction_targets = ARRAY_LEN(blob->inline_blob_extraction_targets);
926 prev_targets = blob->blob_extraction_targets;
927 alloc_blob_extraction_targets = blob->alloc_blob_extraction_targets;
930 if (blob->out_refcnt == alloc_blob_extraction_targets) {
931 alloc_blob_extraction_targets *= 2;
932 targets = REALLOC(prev_targets,
933 alloc_blob_extraction_targets *
936 return WIMLIB_ERR_NOMEM;
939 blob->inline_blob_extraction_targets,
940 sizeof(blob->inline_blob_extraction_targets));
942 blob->blob_extraction_targets = targets;
943 blob->alloc_blob_extraction_targets = alloc_blob_extraction_targets;
945 targets = blob->blob_extraction_targets;
947 targets[blob->out_refcnt].inode = inode;
948 targets[blob->out_refcnt].stream = strm;
954 ref_stream_if_needed(struct wim_dentry *dentry, struct wim_inode *inode,
955 struct wim_inode_stream *strm, struct apply_ctx *ctx)
957 bool need_stream = false;
958 switch (strm->stream_type) {
959 case STREAM_TYPE_DATA:
960 if (stream_is_named(strm)) {
961 /* Named data stream */
962 if (ctx->supported_features.named_data_streams)
964 } else if (!(inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
965 FILE_ATTRIBUTE_ENCRYPTED))
966 && !(inode_is_symlink(inode)
967 && !ctx->supported_features.reparse_points
968 && ctx->supported_features.symlink_reparse_points))
971 * Unnamed data stream. Skip if any of the following is true:
973 * - file is a directory
974 * - file is encrypted
975 * - backend needs to create the file as UNIX symlink
976 * - backend will extract the stream as externally backed
978 if (ctx->apply_ops->will_externally_back) {
979 int ret = (*ctx->apply_ops->will_externally_back)(dentry, ctx);
980 if (ret > 0) /* Error? */
982 if (ret < 0) /* Won't externally back? */
989 case STREAM_TYPE_REPARSE_POINT:
990 wimlib_assert(inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT);
991 if (ctx->supported_features.reparse_points ||
992 (inode_is_symlink(inode) &&
993 ctx->supported_features.symlink_reparse_points))
996 case STREAM_TYPE_EFSRPC_RAW_DATA:
997 wimlib_assert(inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED);
998 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY) {
999 if (ctx->supported_features.encrypted_directories)
1002 if (ctx->supported_features.encrypted_files)
1008 return ref_stream(strm, dentry, ctx);
1013 dentry_ref_streams(struct wim_dentry *dentry, struct apply_ctx *ctx)
1015 struct wim_inode *inode = dentry->d_inode;
1016 for (unsigned i = 0; i < inode->i_num_streams; i++) {
1017 int ret = ref_stream_if_needed(dentry, inode,
1018 &inode->i_streams[i], ctx);
1022 inode->i_visited = 1;
1027 * Given a list of dentries to be extracted, build the list of blobs that need
1028 * to be extracted, and for each blob determine the streams to which that blob
1029 * will be extracted.
1031 * This also initializes the extract progress info with byte and blob
1034 * ctx->supported_features must be filled in.
1037 dentry_list_ref_streams(struct list_head *dentry_list, struct apply_ctx *ctx)
1039 struct wim_dentry *dentry;
1042 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
1043 ret = dentry_ref_streams(dentry, ctx);
1047 list_for_each_entry(dentry, dentry_list, d_extraction_list_node)
1048 dentry->d_inode->i_visited = 0;
1053 dentry_list_build_inode_alias_lists(struct list_head *dentry_list)
1055 struct wim_dentry *dentry;
1057 list_for_each_entry(dentry, dentry_list, d_extraction_list_node)
1058 dentry->d_inode->i_first_extraction_alias = NULL;
1060 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
1061 dentry->d_next_extraction_alias = dentry->d_inode->i_first_extraction_alias;
1062 dentry->d_inode->i_first_extraction_alias = dentry;
1067 inode_tally_features(const struct wim_inode *inode,
1068 struct wim_features *features)
1070 if (inode->i_attributes & FILE_ATTRIBUTE_ARCHIVE)
1071 features->archive_files++;
1072 if (inode->i_attributes & FILE_ATTRIBUTE_HIDDEN)
1073 features->hidden_files++;
1074 if (inode->i_attributes & FILE_ATTRIBUTE_SYSTEM)
1075 features->system_files++;
1076 if (inode->i_attributes & FILE_ATTRIBUTE_COMPRESSED)
1077 features->compressed_files++;
1078 if (inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED) {
1079 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)
1080 features->encrypted_directories++;
1082 features->encrypted_files++;
1084 if (inode->i_attributes & FILE_ATTRIBUTE_NOT_CONTENT_INDEXED)
1085 features->not_context_indexed_files++;
1086 if (inode->i_attributes & FILE_ATTRIBUTE_SPARSE_FILE)
1087 features->sparse_files++;
1088 if (inode_has_named_data_stream(inode))
1089 features->named_data_streams++;
1090 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1091 features->reparse_points++;
1092 if (inode_is_symlink(inode))
1093 features->symlink_reparse_points++;
1095 features->other_reparse_points++;
1097 if (inode_has_security_descriptor(inode))
1098 features->security_descriptors++;
1099 if (inode_has_unix_data(inode))
1100 features->unix_data++;
1103 /* Tally features necessary to extract a dentry and the corresponding inode. */
1105 dentry_tally_features(struct wim_dentry *dentry, struct wim_features *features)
1107 struct wim_inode *inode = dentry->d_inode;
1109 if (dentry_has_short_name(dentry))
1110 features->short_names++;
1112 if (inode->i_visited) {
1113 features->hard_links++;
1115 inode_tally_features(inode, features);
1116 inode->i_visited = 1;
1120 /* Tally the features necessary to extract the specified dentries. */
1122 dentry_list_get_features(struct list_head *dentry_list,
1123 struct wim_features *features)
1125 struct wim_dentry *dentry;
1127 list_for_each_entry(dentry, dentry_list, d_extraction_list_node)
1128 dentry_tally_features(dentry, features);
1130 list_for_each_entry(dentry, dentry_list, d_extraction_list_node)
1131 dentry->d_inode->i_visited = 0;
1135 do_feature_check(const struct wim_features *required_features,
1136 const struct wim_features *supported_features,
1139 /* Encrypted files. */
1140 if (required_features->encrypted_files &&
1141 !supported_features->encrypted_files)
1142 WARNING("Ignoring EFS-encrypted data of %lu files",
1143 required_features->encrypted_files);
1145 /* Named data streams. */
1146 if (required_features->named_data_streams &&
1147 !supported_features->named_data_streams)
1148 WARNING("Ignoring named data streams of %lu files",
1149 required_features->named_data_streams);
1151 /* File attributes. */
1152 if (!(extract_flags & WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES)) {
1153 /* Note: Don't bother the user about FILE_ATTRIBUTE_ARCHIVE.
1154 * We're an archive program, so theoretically we can do what we
1157 if (required_features->hidden_files &&
1158 !supported_features->hidden_files)
1159 WARNING("Ignoring FILE_ATTRIBUTE_HIDDEN of %lu files",
1160 required_features->hidden_files);
1162 if (required_features->system_files &&
1163 !supported_features->system_files)
1164 WARNING("Ignoring FILE_ATTRIBUTE_SYSTEM of %lu files",
1165 required_features->system_files);
1167 if (required_features->compressed_files &&
1168 !supported_features->compressed_files)
1169 WARNING("Ignoring FILE_ATTRIBUTE_COMPRESSED of %lu files",
1170 required_features->compressed_files);
1172 if (required_features->not_context_indexed_files &&
1173 !supported_features->not_context_indexed_files)
1174 WARNING("Ignoring FILE_ATTRIBUTE_NOT_CONTENT_INDEXED of %lu files",
1175 required_features->not_context_indexed_files);
1177 if (required_features->sparse_files &&
1178 !supported_features->sparse_files)
1179 WARNING("Ignoring FILE_ATTRIBUTE_SPARSE_FILE of %lu files",
1180 required_features->sparse_files);
1182 if (required_features->encrypted_directories &&
1183 !supported_features->encrypted_directories)
1184 WARNING("Ignoring FILE_ATTRIBUTE_ENCRYPTED of %lu directories",
1185 required_features->encrypted_directories);
1189 if (required_features->hard_links && !supported_features->hard_links)
1190 WARNING("Extracting %lu hard links as independent files",
1191 required_features->hard_links);
1193 /* Symbolic links and reparse points. */
1194 if ((extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS) &&
1195 required_features->symlink_reparse_points &&
1196 !supported_features->symlink_reparse_points &&
1197 !supported_features->reparse_points)
1199 ERROR("Extraction backend does not support symbolic links!");
1200 return WIMLIB_ERR_UNSUPPORTED;
1202 if (required_features->reparse_points &&
1203 !supported_features->reparse_points)
1205 if (supported_features->symlink_reparse_points) {
1206 if (required_features->other_reparse_points) {
1207 WARNING("Ignoring reparse data of %lu non-symlink/junction files",
1208 required_features->other_reparse_points);
1211 WARNING("Ignoring reparse data of %lu files",
1212 required_features->reparse_points);
1216 /* Security descriptors. */
1217 if (((extract_flags & (WIMLIB_EXTRACT_FLAG_STRICT_ACLS |
1218 WIMLIB_EXTRACT_FLAG_UNIX_DATA))
1219 == WIMLIB_EXTRACT_FLAG_STRICT_ACLS) &&
1220 required_features->security_descriptors &&
1221 !supported_features->security_descriptors)
1223 ERROR("Extraction backend does not support security descriptors!");
1224 return WIMLIB_ERR_UNSUPPORTED;
1226 if (!(extract_flags & WIMLIB_EXTRACT_FLAG_NO_ACLS) &&
1227 required_features->security_descriptors &&
1228 !supported_features->security_descriptors)
1229 WARNING("Ignoring Windows NT security descriptors of %lu files",
1230 required_features->security_descriptors);
1233 if ((extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) &&
1234 required_features->unix_data && !supported_features->unix_data)
1236 ERROR("Extraction backend does not support UNIX data!");
1237 return WIMLIB_ERR_UNSUPPORTED;
1240 if (required_features->unix_data &&
1241 !(extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA))
1243 WARNING("Ignoring UNIX metadata of %lu files",
1244 required_features->unix_data);
1248 if (required_features->short_names &&
1249 !supported_features->short_names)
1251 if (extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_SHORT_NAMES) {
1252 ERROR("Extraction backend does not support DOS names!");
1253 return WIMLIB_ERR_UNSUPPORTED;
1255 WARNING("Ignoring DOS names of %lu files",
1256 required_features->short_names);
1260 if ((extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_TIMESTAMPS) &&
1261 !supported_features->timestamps)
1263 ERROR("Extraction backend does not support timestamps!");
1264 return WIMLIB_ERR_UNSUPPORTED;
1270 static const struct apply_operations *
1271 select_apply_operations(int extract_flags)
1274 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS)
1275 return &ntfs_3g_apply_ops;
1278 return &win32_apply_ops;
1280 return &unix_apply_ops;
1285 extract_trees(WIMStruct *wim, struct wim_dentry **trees, size_t num_trees,
1286 const tchar *target, int extract_flags)
1288 const struct apply_operations *ops;
1289 struct apply_ctx *ctx;
1291 LIST_HEAD(dentry_list);
1293 if (extract_flags & WIMLIB_EXTRACT_FLAG_TO_STDOUT) {
1294 ret = extract_dentries_to_stdout(trees, num_trees,
1299 num_trees = remove_duplicate_trees(trees, num_trees);
1300 num_trees = remove_contained_trees(trees, num_trees);
1302 ops = select_apply_operations(extract_flags);
1304 if (num_trees > 1 && ops->single_tree_only) {
1305 ERROR("Extracting multiple directory trees "
1306 "at once is not supported in %s extraction mode!",
1308 ret = WIMLIB_ERR_UNSUPPORTED;
1312 ctx = CALLOC(1, ops->context_size);
1314 ret = WIMLIB_ERR_NOMEM;
1319 ctx->target = target;
1320 ctx->target_nchars = tstrlen(target);
1321 ctx->extract_flags = extract_flags;
1322 if (ctx->wim->progfunc) {
1323 ctx->progfunc = ctx->wim->progfunc;
1324 ctx->progctx = ctx->wim->progctx;
1325 ctx->progress.extract.image = wim->current_image;
1326 ctx->progress.extract.extract_flags = (extract_flags &
1327 WIMLIB_EXTRACT_MASK_PUBLIC);
1328 ctx->progress.extract.wimfile_name = wim->filename;
1329 ctx->progress.extract.image_name = wimlib_get_image_name(wim,
1330 wim->current_image);
1331 ctx->progress.extract.target = target;
1333 INIT_LIST_HEAD(&ctx->blob_list);
1334 filedes_invalidate(&ctx->tmpfile_fd);
1335 ctx->apply_ops = ops;
1337 ret = (*ops->get_supported_features)(target, &ctx->supported_features);
1341 build_dentry_list(&dentry_list, trees, num_trees,
1343 WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE));
1345 dentry_list_get_features(&dentry_list, &ctx->required_features);
1347 ret = do_feature_check(&ctx->required_features, &ctx->supported_features,
1348 ctx->extract_flags);
1352 ret = dentry_list_calculate_extraction_names(&dentry_list, ctx);
1356 if (unlikely(list_empty(&dentry_list))) {
1357 WARNING("There is nothing to extract!");
1361 ret = dentry_list_resolve_streams(&dentry_list, ctx);
1365 dentry_list_build_inode_alias_lists(&dentry_list);
1367 ret = dentry_list_ref_streams(&dentry_list, ctx);
1371 if (extract_flags & WIMLIB_EXTRACT_FLAG_FROM_PIPE) {
1372 /* When extracting from a pipe, the number of bytes of data to
1373 * extract can't be determined in the normal way (examining the
1374 * blob table), since at this point all we have is a set of
1375 * SHA-1 message digests of blobs that need to be extracted.
1376 * However, we can get a reasonably accurate estimate by taking
1377 * <TOTALBYTES> from the corresponding <IMAGE> in the WIM XML
1378 * data. This does assume that a full image is being extracted,
1379 * but currently there is no API for doing otherwise. (Also,
1380 * subtract <HARDLINKBYTES> from this if hard links are
1381 * supported by the extraction mode.) */
1382 ctx->progress.extract.total_bytes =
1383 wim_info_get_image_total_bytes(wim->wim_info,
1384 wim->current_image);
1385 if (ctx->supported_features.hard_links) {
1386 ctx->progress.extract.total_bytes -=
1387 wim_info_get_image_hard_link_bytes(wim->wim_info,
1388 wim->current_image);
1392 ret = extract_progress(ctx,
1393 ((extract_flags & WIMLIB_EXTRACT_FLAG_IMAGEMODE) ?
1394 WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN :
1395 WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN));
1399 ret = (*ops->extract)(&dentry_list, ctx);
1403 if (ctx->progress.extract.completed_bytes <
1404 ctx->progress.extract.total_bytes)
1406 ctx->progress.extract.completed_bytes =
1407 ctx->progress.extract.total_bytes;
1408 ret = extract_progress(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS);
1413 ret = extract_progress(ctx,
1414 ((extract_flags & WIMLIB_EXTRACT_FLAG_IMAGEMODE) ?
1415 WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END :
1416 WIMLIB_PROGRESS_MSG_EXTRACT_TREE_END));
1418 destroy_blob_list(&ctx->blob_list);
1419 destroy_dentry_list(&dentry_list);
1426 mkdir_if_needed(const tchar *target)
1428 if (!tmkdir(target, 0755))
1431 if (errno == EEXIST)
1435 /* _wmkdir() fails with EACCES if called on a drive root directory. */
1436 if (errno == EACCES)
1440 ERROR_WITH_ERRNO("Failed to create directory \"%"TS"\"", target);
1441 return WIMLIB_ERR_MKDIR;
1444 /* Make sure the extraction flags make sense, and update them if needed. */
1446 check_extract_flags(const WIMStruct *wim, int *extract_flags_p)
1448 int extract_flags = *extract_flags_p;
1450 /* Check for invalid flag combinations */
1452 if ((extract_flags &
1453 (WIMLIB_EXTRACT_FLAG_NO_ACLS |
1454 WIMLIB_EXTRACT_FLAG_STRICT_ACLS)) == (WIMLIB_EXTRACT_FLAG_NO_ACLS |
1455 WIMLIB_EXTRACT_FLAG_STRICT_ACLS))
1456 return WIMLIB_ERR_INVALID_PARAM;
1458 if ((extract_flags &
1459 (WIMLIB_EXTRACT_FLAG_RPFIX |
1460 WIMLIB_EXTRACT_FLAG_NORPFIX)) == (WIMLIB_EXTRACT_FLAG_RPFIX |
1461 WIMLIB_EXTRACT_FLAG_NORPFIX))
1462 return WIMLIB_ERR_INVALID_PARAM;
1464 #ifndef WITH_NTFS_3G
1465 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1466 ERROR("wimlib was compiled without support for NTFS-3g, so\n"
1467 " it cannot apply a WIM image directly to an NTFS volume.");
1468 return WIMLIB_ERR_UNSUPPORTED;
1472 if (extract_flags & WIMLIB_EXTRACT_FLAG_WIMBOOT) {
1475 return WIMLIB_ERR_NO_FILENAME;
1477 ERROR("WIMBoot extraction is only supported on Windows!");
1478 return WIMLIB_ERR_UNSUPPORTED;
1483 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_RPFIX |
1484 WIMLIB_EXTRACT_FLAG_NORPFIX |
1485 WIMLIB_EXTRACT_FLAG_IMAGEMODE)) ==
1486 WIMLIB_EXTRACT_FLAG_IMAGEMODE)
1488 /* For full-image extraction, do reparse point fixups by default
1489 * if the WIM header says they are enabled. */
1490 if (wim->hdr.flags & WIM_HDR_FLAG_RP_FIX)
1491 extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
1494 *extract_flags_p = extract_flags;
1498 struct append_dentry_ctx {
1499 struct wim_dentry **dentries;
1500 size_t num_dentries;
1501 size_t num_alloc_dentries;
1505 append_dentry_cb(struct wim_dentry *dentry, void *_ctx)
1507 struct append_dentry_ctx *ctx = _ctx;
1509 if (ctx->num_dentries == ctx->num_alloc_dentries) {
1510 struct wim_dentry **new_dentries;
1513 new_length = max(ctx->num_alloc_dentries + 8,
1514 ctx->num_alloc_dentries * 3 / 2);
1515 new_dentries = REALLOC(ctx->dentries,
1516 new_length * sizeof(ctx->dentries[0]));
1517 if (new_dentries == NULL)
1518 return WIMLIB_ERR_NOMEM;
1519 ctx->dentries = new_dentries;
1520 ctx->num_alloc_dentries = new_length;
1522 ctx->dentries[ctx->num_dentries++] = dentry;
1526 /* Append dentries matched by a path which can contain wildcard characters. */
1528 append_matched_dentries(WIMStruct *wim, const tchar *orig_pattern,
1529 int extract_flags, struct append_dentry_ctx *ctx)
1531 const size_t count_before = ctx->num_dentries;
1535 pattern = canonicalize_wim_path(orig_pattern);
1537 return WIMLIB_ERR_NOMEM;
1538 ret = expand_path_pattern(wim_get_current_root_dentry(wim), pattern,
1539 append_dentry_cb, ctx);
1541 if (ret || ctx->num_dentries > count_before)
1543 if (extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_GLOB) {
1544 ERROR("No matches for path pattern \"%"TS"\"", orig_pattern);
1545 return WIMLIB_ERR_PATH_DOES_NOT_EXIST;
1547 WARNING("No matches for path pattern \"%"TS"\"", orig_pattern);
1552 do_wimlib_extract_paths(WIMStruct *wim, int image, const tchar *target,
1553 const tchar * const *paths, size_t num_paths,
1557 struct wim_dentry **trees;
1560 if (wim == NULL || target == NULL || target[0] == T('\0') ||
1561 (num_paths != 0 && paths == NULL))
1562 return WIMLIB_ERR_INVALID_PARAM;
1564 ret = check_extract_flags(wim, &extract_flags);
1568 ret = select_wim_image(wim, image);
1572 ret = wim_checksum_unhashed_blobs(wim);
1576 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_NTFS |
1577 WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE)) ==
1578 (WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE))
1580 ret = mkdir_if_needed(target);
1585 if (extract_flags & WIMLIB_EXTRACT_FLAG_GLOB_PATHS) {
1587 struct append_dentry_ctx append_dentry_ctx = {
1590 .num_alloc_dentries = 0,
1593 for (size_t i = 0; i < num_paths; i++) {
1594 ret = append_matched_dentries(wim, paths[i],
1596 &append_dentry_ctx);
1598 trees = append_dentry_ctx.dentries;
1599 goto out_free_trees;
1602 trees = append_dentry_ctx.dentries;
1603 num_trees = append_dentry_ctx.num_dentries;
1605 trees = MALLOC(num_paths * sizeof(trees[0]));
1607 return WIMLIB_ERR_NOMEM;
1609 for (size_t i = 0; i < num_paths; i++) {
1611 tchar *path = canonicalize_wim_path(paths[i]);
1613 ret = WIMLIB_ERR_NOMEM;
1614 goto out_free_trees;
1617 trees[i] = get_dentry(wim, path,
1618 WIMLIB_CASE_PLATFORM_DEFAULT);
1620 if (trees[i] == NULL) {
1621 ERROR("Path \"%"TS"\" does not exist "
1623 paths[i], wim->current_image);
1624 ret = WIMLIB_ERR_PATH_DOES_NOT_EXIST;
1625 goto out_free_trees;
1628 num_trees = num_paths;
1631 if (num_trees == 0) {
1633 goto out_free_trees;
1636 ret = extract_trees(wim, trees, num_trees, target, extract_flags);
1643 extract_single_image(WIMStruct *wim, int image,
1644 const tchar *target, int extract_flags)
1646 const tchar *path = WIMLIB_WIM_ROOT_PATH;
1647 extract_flags |= WIMLIB_EXTRACT_FLAG_IMAGEMODE;
1648 return do_wimlib_extract_paths(wim, image, target, &path, 1, extract_flags);
1651 static const tchar * const filename_forbidden_chars =
1660 /* This function checks if it is okay to use a WIM image's name as a directory
1663 image_name_ok_as_dir(const tchar *image_name)
1665 return image_name && *image_name &&
1666 !tstrpbrk(image_name, filename_forbidden_chars) &&
1667 tstrcmp(image_name, T(".")) &&
1668 tstrcmp(image_name, T(".."));
1671 /* Extracts all images from the WIM to the directory @target, with the images
1672 * placed in subdirectories named by their image names. */
1674 extract_all_images(WIMStruct *wim, const tchar *target, int extract_flags)
1676 size_t image_name_max_len = max(xml_get_max_image_name_len(wim), 20);
1677 size_t output_path_len = tstrlen(target);
1678 tchar buf[output_path_len + 1 + image_name_max_len + 1];
1681 const tchar *image_name;
1683 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1684 ERROR("Cannot extract multiple images in NTFS extraction mode.");
1685 return WIMLIB_ERR_INVALID_PARAM;
1688 ret = mkdir_if_needed(target);
1691 tmemcpy(buf, target, output_path_len);
1692 buf[output_path_len] = OS_PREFERRED_PATH_SEPARATOR;
1693 for (image = 1; image <= wim->hdr.image_count; image++) {
1694 image_name = wimlib_get_image_name(wim, image);
1695 if (image_name_ok_as_dir(image_name)) {
1696 tstrcpy(buf + output_path_len + 1, image_name);
1698 /* Image name is empty or contains forbidden characters.
1699 * Use image number instead. */
1700 tsprintf(buf + output_path_len + 1, T("%d"), image);
1702 ret = extract_single_image(wim, image, buf, extract_flags);
1710 do_wimlib_extract_image(WIMStruct *wim, int image, const tchar *target,
1713 if (extract_flags & (WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE |
1714 WIMLIB_EXTRACT_FLAG_TO_STDOUT |
1715 WIMLIB_EXTRACT_FLAG_GLOB_PATHS))
1716 return WIMLIB_ERR_INVALID_PARAM;
1718 if (image == WIMLIB_ALL_IMAGES)
1719 return extract_all_images(wim, target, extract_flags);
1721 return extract_single_image(wim, image, target, extract_flags);
1725 /****************************************************************************
1727 ****************************************************************************/
1730 wimlib_extract_paths(WIMStruct *wim, int image, const tchar *target,
1731 const tchar * const *paths, size_t num_paths,
1734 if (extract_flags & ~WIMLIB_EXTRACT_MASK_PUBLIC)
1735 return WIMLIB_ERR_INVALID_PARAM;
1737 return do_wimlib_extract_paths(wim, image, target, paths, num_paths,
1742 wimlib_extract_pathlist(WIMStruct *wim, int image, const tchar *target,
1743 const tchar *path_list_file, int extract_flags)
1750 ret = read_path_list_file(path_list_file, &paths, &num_paths, &mem);
1752 ERROR("Failed to read path list file \"%"TS"\"",
1757 ret = wimlib_extract_paths(wim, image, target,
1758 (const tchar * const *)paths, num_paths,
1766 wimlib_extract_image_from_pipe_with_progress(int pipe_fd,
1767 const tchar *image_num_or_name,
1768 const tchar *target,
1770 wimlib_progress_func_t progfunc,
1775 struct filedes *in_fd;
1779 if (extract_flags & ~WIMLIB_EXTRACT_MASK_PUBLIC)
1780 return WIMLIB_ERR_INVALID_PARAM;
1782 /* Read the WIM header from the pipe and get a WIMStruct to represent
1783 * the pipable WIM. Caveats: Unlike getting a WIMStruct with
1784 * wimlib_open_wim(), getting a WIMStruct in this way will result in an
1785 * empty blob table, no XML data read, and no filename set. */
1786 ret = open_wim_as_WIMStruct(&pipe_fd, WIMLIB_OPEN_FLAG_FROM_PIPE, &pwm,
1791 /* Sanity check to make sure this is a pipable WIM. */
1792 if (pwm->hdr.magic != PWM_MAGIC) {
1793 ERROR("The WIM being read from file descriptor %d "
1794 "is not pipable!", pipe_fd);
1795 ret = WIMLIB_ERR_NOT_PIPABLE;
1796 goto out_wimlib_free;
1799 /* Sanity check to make sure the first part of a pipable split WIM is
1800 * sent over the pipe first. */
1801 if (pwm->hdr.part_number != 1) {
1802 ERROR("The first part of the split WIM must be "
1803 "sent over the pipe first.");
1804 ret = WIMLIB_ERR_INVALID_PIPABLE_WIM;
1805 goto out_wimlib_free;
1808 in_fd = &pwm->in_fd;
1809 wimlib_assert(in_fd->offset == WIM_HEADER_DISK_SIZE);
1811 /* As mentioned, the WIMStruct we created from the pipe does not have
1812 * XML data yet. Fix this by reading the extra copy of the XML data
1813 * that directly follows the header in pipable WIMs. (Note: see
1814 * write_pipable_wim() for more details about the format of pipable
1817 u8 hash[SHA1_HASH_SIZE];
1819 ret = read_pwm_blob_header(pwm, hash,
1820 &pwm->hdr.xml_data_reshdr, NULL);
1822 goto out_wimlib_free;
1824 if (!(pwm->hdr.xml_data_reshdr.flags & WIM_RESHDR_FLAG_METADATA)) {
1825 ERROR("Expected XML data, but found non-metadata resource.");
1826 ret = WIMLIB_ERR_INVALID_PIPABLE_WIM;
1827 goto out_wimlib_free;
1830 ret = read_wim_xml_data(pwm);
1832 goto out_wimlib_free;
1834 if (wim_info_get_num_images(pwm->wim_info) != pwm->hdr.image_count) {
1835 ERROR("Image count in XML data is not the same as in WIM header.");
1836 ret = WIMLIB_ERR_IMAGE_COUNT;
1837 goto out_wimlib_free;
1841 /* Get image index (this may use the XML data that was just read to
1842 * resolve an image name). */
1843 if (image_num_or_name) {
1844 image = wimlib_resolve_image(pwm, image_num_or_name);
1845 if (image == WIMLIB_NO_IMAGE) {
1846 ERROR("\"%"TS"\" is not a valid image in the pipable WIM!",
1848 ret = WIMLIB_ERR_INVALID_IMAGE;
1849 goto out_wimlib_free;
1850 } else if (image == WIMLIB_ALL_IMAGES) {
1851 ERROR("Applying all images from a pipe is not supported!");
1852 ret = WIMLIB_ERR_INVALID_IMAGE;
1853 goto out_wimlib_free;
1856 if (pwm->hdr.image_count != 1) {
1857 ERROR("No image was specified, but the pipable WIM "
1858 "did not contain exactly 1 image");
1859 ret = WIMLIB_ERR_INVALID_IMAGE;
1860 goto out_wimlib_free;
1865 /* Load the needed metadata resource. */
1866 for (i = 1; i <= pwm->hdr.image_count; i++) {
1867 struct wim_image_metadata *imd;
1868 struct wim_reshdr reshdr;
1869 struct wim_resource_descriptor *metadata_rdesc;
1871 imd = pwm->image_metadata[i - 1];
1873 ret = WIMLIB_ERR_NOMEM;
1874 imd->metadata_blob = new_blob_descriptor();
1875 if (!imd->metadata_blob)
1876 goto out_wimlib_free;
1878 imd->metadata_blob->is_metadata = 1;
1880 ret = read_pwm_blob_header(pwm, imd->metadata_blob->hash,
1883 goto out_wimlib_free;
1885 if (!(reshdr.flags & WIM_RESHDR_FLAG_METADATA)) {
1886 ERROR("Expected metadata resource, but found "
1887 "non-metadata resource");
1888 ret = WIMLIB_ERR_INVALID_PIPABLE_WIM;
1889 goto out_wimlib_free;
1892 ret = WIMLIB_ERR_NOMEM;
1893 metadata_rdesc = MALLOC(sizeof(struct wim_resource_descriptor));
1894 if (!metadata_rdesc)
1895 goto out_wimlib_free;
1896 wim_reshdr_to_desc_and_blob(&reshdr, pwm, metadata_rdesc,
1897 imd->metadata_blob);
1900 /* Metadata resource is for the image being extracted.
1901 * Parse it and save the metadata in memory. */
1902 ret = read_metadata_resource(imd);
1904 goto out_wimlib_free;
1907 /* Metadata resource is not for the image being
1908 * extracted. Skip over it. */
1909 ret = skip_wim_resource(metadata_rdesc);
1911 goto out_wimlib_free;
1914 /* Extract the image. */
1915 extract_flags |= WIMLIB_EXTRACT_FLAG_FROM_PIPE;
1916 ret = do_wimlib_extract_image(pwm, image, target, extract_flags);
1917 /* Clean up and return. */
1925 wimlib_extract_image_from_pipe(int pipe_fd, const tchar *image_num_or_name,
1926 const tchar *target, int extract_flags)
1928 return wimlib_extract_image_from_pipe_with_progress(pipe_fd,
1937 wimlib_extract_image(WIMStruct *wim, int image, const tchar *target,
1940 if (extract_flags & ~WIMLIB_EXTRACT_MASK_PUBLIC)
1941 return WIMLIB_ERR_INVALID_PARAM;
1942 return do_wimlib_extract_image(wim, image, target, extract_flags);