4 * Support for extracting WIM images, or files or directories contained in a WIM
9 * Copyright (C) 2012-2016 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 | \
88 WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS4K | \
89 WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS8K | \
90 WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS16K | \
91 WIMLIB_EXTRACT_FLAG_COMPACT_LZX \
94 /* Send WIMLIB_PROGRESS_MSG_EXTRACT_FILE_STRUCTURE or
95 * WIMLIB_PROGRESS_MSG_EXTRACT_METADATA. */
97 do_file_extract_progress(struct apply_ctx *ctx, enum wimlib_progress_msg msg)
99 ctx->count_until_file_progress = 500; /* Arbitrary value to limit calls */
100 return extract_progress(ctx, msg);
104 start_file_phase(struct apply_ctx *ctx, u64 end_file_count, enum wimlib_progress_msg msg)
106 ctx->progress.extract.current_file_count = 0;
107 ctx->progress.extract.end_file_count = end_file_count;
108 return do_file_extract_progress(ctx, msg);
112 start_file_structure_phase(struct apply_ctx *ctx, u64 end_file_count)
114 return start_file_phase(ctx, end_file_count, WIMLIB_PROGRESS_MSG_EXTRACT_FILE_STRUCTURE);
118 start_file_metadata_phase(struct apply_ctx *ctx, u64 end_file_count)
120 return start_file_phase(ctx, end_file_count, WIMLIB_PROGRESS_MSG_EXTRACT_METADATA);
124 end_file_phase(struct apply_ctx *ctx, enum wimlib_progress_msg msg)
126 ctx->progress.extract.current_file_count = ctx->progress.extract.end_file_count;
127 return do_file_extract_progress(ctx, msg);
131 end_file_structure_phase(struct apply_ctx *ctx)
133 return end_file_phase(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_FILE_STRUCTURE);
137 end_file_metadata_phase(struct apply_ctx *ctx)
139 return end_file_phase(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_METADATA);
142 #define PWM_FOUND_WIM_HDR (-1)
144 /* Read the header for a blob in a pipable WIM. If @pwm_hdr_ret is not NULL,
145 * also look for a pipable WIM header and return PWM_FOUND_WIM_HDR if found. */
147 read_pwm_blob_header(WIMStruct *pwm, u8 hash_ret[SHA1_HASH_SIZE],
148 struct wim_reshdr *reshdr_ret,
149 struct wim_header_disk *pwm_hdr_ret)
152 struct pwm_blob_hdr blob_hdr;
155 ret = full_read(&pwm->in_fd, &blob_hdr, sizeof(blob_hdr));
159 magic = le64_to_cpu(blob_hdr.magic);
161 if (magic == PWM_MAGIC && pwm_hdr_ret != NULL) {
162 memcpy(pwm_hdr_ret, &blob_hdr, sizeof(blob_hdr));
163 ret = full_read(&pwm->in_fd,
164 (u8 *)pwm_hdr_ret + sizeof(blob_hdr),
165 sizeof(*pwm_hdr_ret) - sizeof(blob_hdr));
168 return PWM_FOUND_WIM_HDR;
171 if (unlikely(magic != PWM_BLOB_MAGIC)) {
172 ERROR("Data read on pipe is invalid (expected blob header)");
173 return WIMLIB_ERR_INVALID_PIPABLE_WIM;
176 copy_hash(hash_ret, blob_hdr.hash);
178 reshdr_ret->size_in_wim = 0; /* Not available */
179 reshdr_ret->flags = le32_to_cpu(blob_hdr.flags);
180 reshdr_ret->offset_in_wim = pwm->in_fd.offset;
181 reshdr_ret->uncompressed_size = le64_to_cpu(blob_hdr.uncompressed_size);
183 if (unlikely(reshdr_ret->uncompressed_size == 0)) {
184 ERROR("Data read on pipe is invalid (resource is of 0 size)");
185 return WIMLIB_ERR_INVALID_PIPABLE_WIM;
191 if (ret == WIMLIB_ERR_UNEXPECTED_END_OF_FILE)
192 ERROR("The pipe ended before all needed data was sent!");
194 ERROR_WITH_ERRNO("Error reading pipable WIM from pipe");
199 read_blobs_from_pipe(struct apply_ctx *ctx, const struct read_blob_callbacks *cbs)
202 u8 hash[SHA1_HASH_SIZE];
203 struct wim_reshdr reshdr;
204 struct wim_header_disk pwm_hdr;
205 struct wim_resource_descriptor rdesc;
206 struct blob_descriptor *blob;
208 copy_guid(ctx->progress.extract.guid, ctx->wim->hdr.guid);
209 ctx->progress.extract.part_number = ctx->wim->hdr.part_number;
210 ctx->progress.extract.total_parts = ctx->wim->hdr.total_parts;
211 ret = extract_progress(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN);
215 while (ctx->num_blobs_remaining) {
217 ret = read_pwm_blob_header(ctx->wim, hash, &reshdr, &pwm_hdr);
219 if (ret == PWM_FOUND_WIM_HDR) {
220 u16 part_number = le16_to_cpu(pwm_hdr.part_number);
221 u16 total_parts = le16_to_cpu(pwm_hdr.total_parts);
223 if (part_number == ctx->progress.extract.part_number &&
224 total_parts == ctx->progress.extract.total_parts &&
225 guids_equal(pwm_hdr.guid, ctx->progress.extract.guid))
228 copy_guid(ctx->progress.extract.guid, pwm_hdr.guid);
229 ctx->progress.extract.part_number = part_number;
230 ctx->progress.extract.total_parts = total_parts;
231 ret = extract_progress(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN);
241 if (!(reshdr.flags & WIM_RESHDR_FLAG_METADATA)
242 && (blob = lookup_blob(ctx->wim->blob_table, hash))
243 && (blob->out_refcnt))
245 wim_reshdr_to_desc_and_blob(&reshdr, ctx->wim, &rdesc, blob);
246 ret = read_blob_with_sha1(blob, cbs);
247 blob_unset_is_located_in_wim_resource(blob);
250 ctx->num_blobs_remaining--;
252 wim_reshdr_to_desc(&reshdr, ctx->wim, &rdesc);
253 ret = skip_wim_resource(&rdesc);
263 handle_pwm_metadata_resource(WIMStruct *pwm, int image, bool is_needed)
265 struct blob_descriptor *blob;
266 struct wim_reshdr reshdr;
267 struct wim_resource_descriptor *rdesc;
270 ret = WIMLIB_ERR_NOMEM;
271 blob = new_blob_descriptor();
275 ret = read_pwm_blob_header(pwm, blob->hash, &reshdr, NULL);
279 ret = WIMLIB_ERR_INVALID_PIPABLE_WIM;
280 if (!(reshdr.flags & WIM_RESHDR_FLAG_METADATA)) {
281 ERROR("Expected metadata resource, but found non-metadata "
286 ret = WIMLIB_ERR_NOMEM;
287 rdesc = MALLOC(sizeof(*rdesc));
291 wim_reshdr_to_desc_and_blob(&reshdr, pwm, rdesc, blob);
294 ret = WIMLIB_ERR_NOMEM;
295 pwm->image_metadata[image - 1] = new_unloaded_image_metadata(blob);
296 if (!pwm->image_metadata[image - 1])
300 /* If the metadata resource is for the image being extracted, then parse
301 * it and save the metadata in memory. Otherwise, skip over it. */
303 ret = select_wim_image(pwm, image);
305 ret = skip_wim_resource(rdesc);
307 free_blob_descriptor(blob);
311 /* Creates a temporary file opened for writing. The open file descriptor is
312 * returned in @fd_ret and its name is returned in @name_ret (dynamically
315 create_temporary_file(struct filedes *fd_ret, tchar **name_ret)
322 name = ttempnam(NULL, T("wimlib"));
324 ERROR_WITH_ERRNO("Failed to create temporary filename");
325 return WIMLIB_ERR_NOMEM;
328 open_flags = O_WRONLY | O_CREAT | O_EXCL | O_BINARY;
330 open_flags |= _O_SHORT_LIVED;
332 raw_fd = topen(name, open_flags, 0600);
335 if (errno == EEXIST) {
339 ERROR_WITH_ERRNO("Failed to create temporary file "
342 return WIMLIB_ERR_OPEN;
345 filedes_init(fd_ret, raw_fd);
351 begin_extract_blob_wrapper(struct blob_descriptor *blob, void *_ctx)
353 struct apply_ctx *ctx = _ctx;
355 ctx->cur_blob = blob;
356 ctx->cur_blob_offset = 0;
358 if (unlikely(blob->out_refcnt > MAX_OPEN_FILES))
359 return create_temporary_file(&ctx->tmpfile_fd, &ctx->tmpfile_name);
361 return call_begin_blob(blob, ctx->saved_cbs);
365 extract_chunk_wrapper(const void *chunk, size_t size, void *_ctx)
367 struct apply_ctx *ctx = _ctx;
368 union wimlib_progress_info *progress = &ctx->progress;
371 ctx->cur_blob_offset += size;
373 if (likely(ctx->supported_features.hard_links)) {
374 progress->extract.completed_bytes +=
375 (u64)size * ctx->cur_blob->out_refcnt;
376 if (ctx->cur_blob_offset == ctx->cur_blob->size)
377 progress->extract.completed_streams += ctx->cur_blob->out_refcnt;
379 const struct blob_extraction_target *targets =
380 blob_extraction_targets(ctx->cur_blob);
381 for (u32 i = 0; i < ctx->cur_blob->out_refcnt; i++) {
382 const struct wim_inode *inode = targets[i].inode;
383 const struct wim_dentry *dentry;
385 inode_for_each_extraction_alias(dentry, inode) {
386 progress->extract.completed_bytes += size;
387 if (ctx->cur_blob_offset == ctx->cur_blob->size)
388 progress->extract.completed_streams++;
392 if (progress->extract.completed_bytes >= ctx->next_progress) {
394 ret = extract_progress(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS);
398 set_next_progress(progress->extract.completed_bytes,
399 progress->extract.total_bytes,
400 &ctx->next_progress);
403 if (unlikely(filedes_valid(&ctx->tmpfile_fd))) {
404 /* Just extracting to temporary file for now. */
405 ret = full_write(&ctx->tmpfile_fd, chunk, size);
407 ERROR_WITH_ERRNO("Error writing data to "
408 "temporary file \"%"TS"\"",
414 return call_consume_chunk(chunk, size, ctx->saved_cbs);
417 /* Copy the blob's data from the temporary file to each of its targets.
419 * This is executed only in the very uncommon case that a blob is being
420 * extracted to more than MAX_OPEN_FILES targets! */
422 extract_from_tmpfile(const tchar *tmpfile_name,
423 const struct blob_descriptor *orig_blob,
424 const struct read_blob_callbacks *cbs)
426 struct blob_descriptor tmpfile_blob;
427 const struct blob_extraction_target *targets = blob_extraction_targets(orig_blob);
430 memcpy(&tmpfile_blob, orig_blob, sizeof(struct blob_descriptor));
431 tmpfile_blob.blob_location = BLOB_IN_FILE_ON_DISK;
432 tmpfile_blob.file_on_disk = (tchar *)tmpfile_name;
433 tmpfile_blob.out_refcnt = 1;
435 for (u32 i = 0; i < orig_blob->out_refcnt; i++) {
436 tmpfile_blob.inline_blob_extraction_targets[0] = targets[i];
437 ret = read_blob_with_cbs(&tmpfile_blob, cbs);
445 end_extract_blob_wrapper(struct blob_descriptor *blob, int status, void *_ctx)
447 struct apply_ctx *ctx = _ctx;
449 if (unlikely(filedes_valid(&ctx->tmpfile_fd))) {
450 filedes_close(&ctx->tmpfile_fd);
452 status = extract_from_tmpfile(ctx->tmpfile_name, blob,
454 filedes_invalidate(&ctx->tmpfile_fd);
455 tunlink(ctx->tmpfile_name);
456 FREE(ctx->tmpfile_name);
460 return call_end_blob(blob, status, ctx->saved_cbs);
464 * Read the list of blobs to extract and feed their data into the specified
465 * callback functions.
467 * This handles checksumming each blob.
469 * This also handles sending WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS.
471 * This also works if the WIM is being read from a pipe.
473 * This also will split up blobs that will need to be extracted to more than
474 * MAX_OPEN_FILES locations, as measured by the 'out_refcnt' of each blob.
475 * Therefore, the apply_operations implementation need not worry about running
476 * out of file descriptors, unless it might open more than one file descriptor
477 * per 'blob_extraction_target' (e.g. Win32 currently might because the
478 * destination file system might not support hard links).
481 extract_blob_list(struct apply_ctx *ctx, const struct read_blob_callbacks *cbs)
483 struct read_blob_callbacks wrapper_cbs = {
484 .begin_blob = begin_extract_blob_wrapper,
485 .consume_chunk = extract_chunk_wrapper,
486 .end_blob = end_extract_blob_wrapper,
489 ctx->saved_cbs = cbs;
490 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_FROM_PIPE) {
491 return read_blobs_from_pipe(ctx, &wrapper_cbs);
493 return read_blob_list(&ctx->blob_list,
494 offsetof(struct blob_descriptor,
496 &wrapper_cbs, VERIFY_BLOB_HASHES);
500 /* Extract a WIM dentry to standard output.
502 * This obviously doesn't make sense in all cases. We return an error if the
503 * dentry does not correspond to a regular file. Otherwise we extract the
504 * unnamed data stream only. */
506 extract_dentry_to_stdout(struct wim_dentry *dentry,
507 const struct blob_table *blob_table)
509 struct wim_inode *inode = dentry->d_inode;
510 struct blob_descriptor *blob;
511 struct filedes _stdout;
513 if (inode->i_attributes & (FILE_ATTRIBUTE_REPARSE_POINT |
514 FILE_ATTRIBUTE_DIRECTORY |
515 FILE_ATTRIBUTE_ENCRYPTED))
517 ERROR("\"%"TS"\" is not a regular file and therefore cannot be "
518 "extracted to standard output", dentry_full_path(dentry));
519 return WIMLIB_ERR_NOT_A_REGULAR_FILE;
522 blob = inode_get_blob_for_unnamed_data_stream(inode, blob_table);
524 const u8 *hash = inode_get_hash_of_unnamed_data_stream(inode);
525 if (!is_zero_hash(hash))
526 return blob_not_found_error(inode, hash);
530 filedes_init(&_stdout, STDOUT_FILENO);
531 return extract_blob_to_fd(blob, &_stdout);
535 extract_dentries_to_stdout(struct wim_dentry **dentries, size_t num_dentries,
536 const struct blob_table *blob_table)
538 for (size_t i = 0; i < num_dentries; i++) {
539 int ret = extract_dentry_to_stdout(dentries[i], blob_table);
546 /**********************************************************************/
549 * Removes duplicate dentries from the array.
551 * Returns the new number of dentries, packed at the front of the array.
554 remove_duplicate_trees(struct wim_dentry **trees, size_t num_trees)
557 for (i = 0; i < num_trees; i++) {
558 if (!trees[i]->d_tmp_flag) {
559 /* Found distinct dentry. */
560 trees[i]->d_tmp_flag = 1;
561 trees[j++] = trees[i];
564 for (i = 0; i < j; i++)
565 trees[i]->d_tmp_flag = 0;
570 * Remove dentries that are descendants of other dentries in the array.
572 * Returns the new number of dentries, packed at the front of the array.
575 remove_contained_trees(struct wim_dentry **trees, size_t num_trees)
578 for (i = 0; i < num_trees; i++)
579 trees[i]->d_tmp_flag = 1;
580 for (i = 0; i < num_trees; i++) {
581 struct wim_dentry *d = trees[i];
582 while (!dentry_is_root(d)) {
587 trees[j++] = trees[i];
591 trees[i]->d_tmp_flag = 0;
594 for (i = 0; i < j; i++)
595 trees[i]->d_tmp_flag = 0;
600 dentry_append_to_list(struct wim_dentry *dentry, void *_dentry_list)
602 struct list_head *dentry_list = _dentry_list;
603 list_add_tail(&dentry->d_extraction_list_node, dentry_list);
608 dentry_reset_extraction_list_node(struct wim_dentry *dentry)
610 dentry->d_extraction_list_node = (struct list_head){NULL, NULL};
614 dentry_delete_from_list(struct wim_dentry *dentry, void *_ignore)
616 if (will_extract_dentry(dentry)) {
617 list_del(&dentry->d_extraction_list_node);
618 dentry_reset_extraction_list_node(dentry);
624 * Build the preliminary list of dentries to be extracted.
626 * The list maintains the invariant that if d1 and d2 are in the list and d1 is
627 * an ancestor of d2, then d1 appears before d2 in the list.
630 build_dentry_list(struct list_head *dentry_list, struct wim_dentry **trees,
631 size_t num_trees, bool add_ancestors)
633 INIT_LIST_HEAD(dentry_list);
635 /* Add the trees recursively. */
636 for (size_t i = 0; i < num_trees; i++)
637 for_dentry_in_tree(trees[i], dentry_append_to_list, dentry_list);
639 /* If requested, add ancestors of the trees. */
641 for (size_t i = 0; i < num_trees; i++) {
642 struct wim_dentry *dentry = trees[i];
643 struct wim_dentry *ancestor;
644 struct list_head *place_after;
646 if (dentry_is_root(dentry))
649 place_after = dentry_list;
652 ancestor = ancestor->d_parent;
653 if (will_extract_dentry(ancestor)) {
654 place_after = &ancestor->d_extraction_list_node;
657 } while (!dentry_is_root(ancestor));
661 ancestor = ancestor->d_parent;
662 if (will_extract_dentry(ancestor))
664 list_add(&ancestor->d_extraction_list_node, place_after);
665 } while (!dentry_is_root(ancestor));
671 destroy_dentry_list(struct list_head *dentry_list)
673 struct wim_dentry *dentry, *tmp;
674 struct wim_inode *inode;
676 list_for_each_entry_safe(dentry, tmp, dentry_list, d_extraction_list_node) {
677 inode = dentry->d_inode;
678 dentry_reset_extraction_list_node(dentry);
679 inode->i_visited = 0;
680 inode->i_can_externally_back = 0;
681 if ((void *)dentry->d_extraction_name != (void *)dentry->d_name)
682 FREE(dentry->d_extraction_name);
683 dentry->d_extraction_name = NULL;
684 dentry->d_extraction_name_nchars = 0;
689 destroy_blob_list(struct list_head *blob_list)
691 struct blob_descriptor *blob;
693 list_for_each_entry(blob, blob_list, extraction_list)
694 if (blob->out_refcnt > ARRAY_LEN(blob->inline_blob_extraction_targets))
695 FREE(blob->blob_extraction_targets);
699 static const utf16lechar replacement_char = cpu_to_le16(0xfffd);
701 static const utf16lechar replacement_char = cpu_to_le16('?');
705 file_name_valid(utf16lechar *name, size_t num_chars, bool fix)
711 for (i = 0; i < num_chars; i++) {
712 switch (le16_to_cpu(name[i])) {
714 case '\x01'...'\x1F':
727 name[i] = replacement_char;
737 dentry_calculate_extraction_name(struct wim_dentry *dentry,
738 struct apply_ctx *ctx)
742 if (dentry_is_root(dentry))
746 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
747 dentry->d_extraction_name = dentry->d_name;
748 dentry->d_extraction_name_nchars = dentry->d_name_nbytes /
754 if (!ctx->supported_features.case_sensitive_filenames) {
755 struct wim_dentry *other;
756 list_for_each_entry(other, &dentry->d_ci_conflict_list,
759 if (will_extract_dentry(other)) {
760 if (ctx->extract_flags &
761 WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS) {
762 WARNING("\"%"TS"\" has the same "
763 "case-insensitive name as "
764 "\"%"TS"\"; extracting "
765 "dummy name instead",
766 dentry_full_path(dentry),
767 dentry_full_path(other));
770 WARNING("Not extracting \"%"TS"\": "
771 "has same case-insensitive "
773 dentry_full_path(dentry),
774 dentry_full_path(other));
781 if (file_name_valid(dentry->d_name, dentry->d_name_nbytes / 2, false)) {
783 ret = utf16le_get_tstr(dentry->d_name,
784 dentry->d_name_nbytes,
785 (const tchar **)&dentry->d_extraction_name,
787 dentry->d_extraction_name_nchars = nbytes / sizeof(tchar);
790 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES)
792 WARNING("\"%"TS"\" has an invalid filename "
793 "that is not supported on this platform; "
794 "extracting dummy name instead",
795 dentry_full_path(dentry));
798 WARNING("Not extracting \"%"TS"\": has an invalid filename "
799 "that is not supported on this platform",
800 dentry_full_path(dentry));
807 utf16lechar utf16_name_copy[dentry->d_name_nbytes / 2];
809 memcpy(utf16_name_copy, dentry->d_name, dentry->d_name_nbytes);
810 file_name_valid(utf16_name_copy, dentry->d_name_nbytes / 2, true);
812 const tchar *tchar_name;
815 ret = utf16le_get_tstr(utf16_name_copy,
816 dentry->d_name_nbytes,
817 &tchar_name, &tchar_nchars);
821 tchar_nchars /= sizeof(tchar);
823 size_t fixed_name_num_chars = tchar_nchars;
824 tchar fixed_name[tchar_nchars + 50];
826 tmemcpy(fixed_name, tchar_name, tchar_nchars);
827 fixed_name_num_chars += tsprintf(fixed_name + tchar_nchars,
828 T(" (invalid filename #%lu)"),
829 ++ctx->invalid_sequence);
831 utf16le_put_tstr(tchar_name);
833 dentry->d_extraction_name = TSTRDUP(fixed_name);
834 if (!dentry->d_extraction_name)
835 return WIMLIB_ERR_NOMEM;
836 dentry->d_extraction_name_nchars = fixed_name_num_chars;
841 for_dentry_in_tree(dentry, dentry_delete_from_list, NULL);
846 * Calculate the actual filename component at which each WIM dentry will be
847 * extracted, with special handling for dentries that are unsupported by the
848 * extraction backend or have invalid names.
850 * ctx->supported_features must be filled in.
852 * Possible error codes: WIMLIB_ERR_NOMEM, WIMLIB_ERR_INVALID_UTF16_STRING
855 dentry_list_calculate_extraction_names(struct list_head *dentry_list,
856 struct apply_ctx *ctx)
858 struct list_head *prev, *cur;
860 /* Can't use list_for_each_entry() because a call to
861 * dentry_calculate_extraction_name() may delete the current dentry and
862 * its children from the list. */
866 struct wim_dentry *dentry;
870 if (cur == dentry_list)
873 dentry = list_entry(cur, struct wim_dentry, d_extraction_list_node);
875 ret = dentry_calculate_extraction_name(dentry, ctx);
879 if (prev->next == cur)
882 ; /* Current dentry and its children (which follow in
883 the list) were deleted. prev stays the same. */
889 dentry_resolve_streams(struct wim_dentry *dentry, int extract_flags,
890 struct blob_table *blob_table)
892 struct wim_inode *inode = dentry->d_inode;
893 struct blob_descriptor *blob;
897 /* Special case: when extracting from a pipe, the WIM blob table is
898 * initially empty, so "resolving" an inode's streams is initially not
899 * possible. However, we still need to keep track of which blobs,
900 * identified by SHA-1 message digests, need to be extracted, so we
901 * "resolve" the inode's streams anyway by allocating a 'struct
902 * blob_descriptor' for each one. */
903 if (extract_flags & WIMLIB_EXTRACT_FLAG_FROM_PIPE)
905 ret = inode_resolve_streams(inode, blob_table, force);
908 for (unsigned i = 0; i < inode->i_num_streams; i++) {
909 blob = stream_blob_resolved(&inode->i_streams[i]);
911 blob->out_refcnt = 0;
917 * For each dentry to be extracted, resolve all streams in the corresponding
918 * inode and set 'out_refcnt' in all referenced blob_descriptors to 0.
920 * Possible error codes: WIMLIB_ERR_RESOURCE_NOT_FOUND, WIMLIB_ERR_NOMEM.
923 dentry_list_resolve_streams(struct list_head *dentry_list,
924 struct apply_ctx *ctx)
926 struct wim_dentry *dentry;
929 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
930 ret = dentry_resolve_streams(dentry,
932 ctx->wim->blob_table);
940 ref_stream(struct wim_inode_stream *strm, struct wim_dentry *dentry,
941 struct apply_ctx *ctx)
943 struct wim_inode *inode = dentry->d_inode;
944 struct blob_descriptor *blob = stream_blob_resolved(strm);
945 struct blob_extraction_target *targets;
950 /* Tally the size only for each actual extraction of the stream (not
951 * additional hard links to the inode). */
952 if (inode->i_visited && ctx->supported_features.hard_links)
955 ctx->progress.extract.total_bytes += blob->size;
956 ctx->progress.extract.total_streams++;
958 if (inode->i_visited)
961 /* Add each blob to 'ctx->blob_list' only one time, regardless of how
962 * many extraction targets it will have. */
963 if (blob->out_refcnt == 0) {
964 list_add_tail(&blob->extraction_list, &ctx->blob_list);
965 ctx->num_blobs_remaining++;
968 /* Set this stream as an extraction target of 'blob'. */
970 if (blob->out_refcnt < ARRAY_LEN(blob->inline_blob_extraction_targets)) {
971 targets = blob->inline_blob_extraction_targets;
973 struct blob_extraction_target *prev_targets;
974 size_t alloc_blob_extraction_targets;
976 if (blob->out_refcnt == ARRAY_LEN(blob->inline_blob_extraction_targets)) {
978 alloc_blob_extraction_targets = ARRAY_LEN(blob->inline_blob_extraction_targets);
980 prev_targets = blob->blob_extraction_targets;
981 alloc_blob_extraction_targets = blob->alloc_blob_extraction_targets;
984 if (blob->out_refcnt == alloc_blob_extraction_targets) {
985 alloc_blob_extraction_targets *= 2;
986 targets = REALLOC(prev_targets,
987 alloc_blob_extraction_targets *
990 return WIMLIB_ERR_NOMEM;
993 blob->inline_blob_extraction_targets,
994 sizeof(blob->inline_blob_extraction_targets));
996 blob->blob_extraction_targets = targets;
997 blob->alloc_blob_extraction_targets = alloc_blob_extraction_targets;
999 targets = blob->blob_extraction_targets;
1001 targets[blob->out_refcnt].inode = inode;
1002 targets[blob->out_refcnt].stream = strm;
1008 ref_stream_if_needed(struct wim_dentry *dentry, struct wim_inode *inode,
1009 struct wim_inode_stream *strm, struct apply_ctx *ctx)
1011 bool need_stream = false;
1012 switch (strm->stream_type) {
1013 case STREAM_TYPE_DATA:
1014 if (stream_is_named(strm)) {
1015 /* Named data stream */
1016 if (ctx->supported_features.named_data_streams)
1018 } else if (!(inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
1019 FILE_ATTRIBUTE_ENCRYPTED))
1020 && !(inode_is_symlink(inode)
1021 && !ctx->supported_features.reparse_points
1022 && ctx->supported_features.symlink_reparse_points))
1025 * Unnamed data stream. Skip if any of the following is true:
1027 * - file is a directory
1028 * - file is encrypted
1029 * - backend needs to create the file as UNIX symlink
1030 * - backend will extract the stream as externally
1031 * backed from the WIM archive itself
1033 if (ctx->apply_ops->will_back_from_wim) {
1034 int ret = (*ctx->apply_ops->will_back_from_wim)(dentry, ctx);
1035 if (ret > 0) /* Error? */
1037 if (ret < 0) /* Won't externally back? */
1044 case STREAM_TYPE_REPARSE_POINT:
1045 wimlib_assert(inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT);
1046 if (ctx->supported_features.reparse_points ||
1047 (inode_is_symlink(inode) &&
1048 ctx->supported_features.symlink_reparse_points))
1051 case STREAM_TYPE_EFSRPC_RAW_DATA:
1052 wimlib_assert(inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED);
1053 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY) {
1054 if (ctx->supported_features.encrypted_directories)
1057 if (ctx->supported_features.encrypted_files)
1063 return ref_stream(strm, dentry, ctx);
1068 dentry_ref_streams(struct wim_dentry *dentry, struct apply_ctx *ctx)
1070 struct wim_inode *inode = dentry->d_inode;
1071 for (unsigned i = 0; i < inode->i_num_streams; i++) {
1072 int ret = ref_stream_if_needed(dentry, inode,
1073 &inode->i_streams[i], ctx);
1077 inode->i_visited = 1;
1082 * Given a list of dentries to be extracted, build the list of blobs that need
1083 * to be extracted, and for each blob determine the streams to which that blob
1084 * will be extracted.
1086 * This also initializes the extract progress info with byte and blob
1089 * ctx->supported_features must be filled in.
1092 dentry_list_ref_streams(struct list_head *dentry_list, struct apply_ctx *ctx)
1094 struct wim_dentry *dentry;
1097 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
1098 ret = dentry_ref_streams(dentry, ctx);
1102 list_for_each_entry(dentry, dentry_list, d_extraction_list_node)
1103 dentry->d_inode->i_visited = 0;
1108 dentry_list_build_inode_alias_lists(struct list_head *dentry_list)
1110 struct wim_dentry *dentry;
1112 list_for_each_entry(dentry, dentry_list, d_extraction_list_node)
1113 dentry->d_inode->i_first_extraction_alias = NULL;
1115 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
1116 dentry->d_next_extraction_alias = dentry->d_inode->i_first_extraction_alias;
1117 dentry->d_inode->i_first_extraction_alias = dentry;
1122 inode_tally_features(const struct wim_inode *inode,
1123 struct wim_features *features)
1125 if (inode->i_attributes & FILE_ATTRIBUTE_READONLY)
1126 features->readonly_files++;
1127 if (inode->i_attributes & FILE_ATTRIBUTE_HIDDEN)
1128 features->hidden_files++;
1129 if (inode->i_attributes & FILE_ATTRIBUTE_SYSTEM)
1130 features->system_files++;
1131 if (inode->i_attributes & FILE_ATTRIBUTE_ARCHIVE)
1132 features->archive_files++;
1133 if (inode->i_attributes & FILE_ATTRIBUTE_COMPRESSED)
1134 features->compressed_files++;
1135 if (inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED) {
1136 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)
1137 features->encrypted_directories++;
1139 features->encrypted_files++;
1141 if (inode->i_attributes & FILE_ATTRIBUTE_NOT_CONTENT_INDEXED)
1142 features->not_context_indexed_files++;
1143 if (inode->i_attributes & FILE_ATTRIBUTE_SPARSE_FILE)
1144 features->sparse_files++;
1145 if (inode_has_named_data_stream(inode))
1146 features->named_data_streams++;
1147 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1148 features->reparse_points++;
1149 if (inode_is_symlink(inode))
1150 features->symlink_reparse_points++;
1152 features->other_reparse_points++;
1154 if (inode_has_security_descriptor(inode))
1155 features->security_descriptors++;
1156 if (inode_has_unix_data(inode))
1157 features->unix_data++;
1160 /* Tally features necessary to extract a dentry and the corresponding inode. */
1162 dentry_tally_features(struct wim_dentry *dentry, struct wim_features *features)
1164 struct wim_inode *inode = dentry->d_inode;
1166 if (dentry_has_short_name(dentry))
1167 features->short_names++;
1169 if (inode->i_visited) {
1170 features->hard_links++;
1172 inode_tally_features(inode, features);
1173 inode->i_visited = 1;
1177 /* Tally the features necessary to extract the specified dentries. */
1179 dentry_list_get_features(struct list_head *dentry_list,
1180 struct wim_features *features)
1182 struct wim_dentry *dentry;
1184 list_for_each_entry(dentry, dentry_list, d_extraction_list_node)
1185 dentry_tally_features(dentry, features);
1187 list_for_each_entry(dentry, dentry_list, d_extraction_list_node)
1188 dentry->d_inode->i_visited = 0;
1192 do_feature_check(const struct wim_features *required_features,
1193 const struct wim_features *supported_features,
1196 /* Encrypted files. */
1197 if (required_features->encrypted_files &&
1198 !supported_features->encrypted_files)
1199 WARNING("Ignoring EFS-encrypted data of %lu files",
1200 required_features->encrypted_files);
1202 /* Named data streams. */
1203 if (required_features->named_data_streams &&
1204 !supported_features->named_data_streams)
1205 WARNING("Ignoring named data streams of %lu files",
1206 required_features->named_data_streams);
1208 /* File attributes. */
1209 if (!(extract_flags & WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES)) {
1211 if (required_features->readonly_files &&
1212 !supported_features->readonly_files)
1213 WARNING("Ignoring FILE_ATTRIBUTE_READONLY of %lu files",
1214 required_features->readonly_files);
1216 if (required_features->hidden_files &&
1217 !supported_features->hidden_files)
1218 WARNING("Ignoring FILE_ATTRIBUTE_HIDDEN of %lu files",
1219 required_features->hidden_files);
1221 if (required_features->system_files &&
1222 !supported_features->system_files)
1223 WARNING("Ignoring FILE_ATTRIBUTE_SYSTEM of %lu files",
1224 required_features->system_files);
1226 /* Note: Don't bother the user about FILE_ATTRIBUTE_ARCHIVE.
1227 * We're an archive program, so theoretically we can do what we
1230 if (required_features->compressed_files &&
1231 !supported_features->compressed_files)
1232 WARNING("Ignoring FILE_ATTRIBUTE_COMPRESSED of %lu files",
1233 required_features->compressed_files);
1235 if (required_features->not_context_indexed_files &&
1236 !supported_features->not_context_indexed_files)
1237 WARNING("Ignoring FILE_ATTRIBUTE_NOT_CONTENT_INDEXED of %lu files",
1238 required_features->not_context_indexed_files);
1240 if (required_features->sparse_files &&
1241 !supported_features->sparse_files)
1242 WARNING("Ignoring FILE_ATTRIBUTE_SPARSE_FILE of %lu files",
1243 required_features->sparse_files);
1245 if (required_features->encrypted_directories &&
1246 !supported_features->encrypted_directories)
1247 WARNING("Ignoring FILE_ATTRIBUTE_ENCRYPTED of %lu directories",
1248 required_features->encrypted_directories);
1252 if (required_features->hard_links && !supported_features->hard_links)
1253 WARNING("Extracting %lu hard links as independent files",
1254 required_features->hard_links);
1256 /* Symbolic links and reparse points. */
1257 if ((extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS) &&
1258 required_features->symlink_reparse_points &&
1259 !supported_features->symlink_reparse_points &&
1260 !supported_features->reparse_points)
1262 ERROR("Extraction backend does not support symbolic links!");
1263 return WIMLIB_ERR_UNSUPPORTED;
1265 if (required_features->reparse_points &&
1266 !supported_features->reparse_points)
1268 if (supported_features->symlink_reparse_points) {
1269 if (required_features->other_reparse_points) {
1270 WARNING("Ignoring reparse data of %lu non-symlink/junction files",
1271 required_features->other_reparse_points);
1274 WARNING("Ignoring reparse data of %lu files",
1275 required_features->reparse_points);
1279 /* Security descriptors. */
1280 if (((extract_flags & (WIMLIB_EXTRACT_FLAG_STRICT_ACLS |
1281 WIMLIB_EXTRACT_FLAG_UNIX_DATA))
1282 == WIMLIB_EXTRACT_FLAG_STRICT_ACLS) &&
1283 required_features->security_descriptors &&
1284 !supported_features->security_descriptors)
1286 ERROR("Extraction backend does not support security descriptors!");
1287 return WIMLIB_ERR_UNSUPPORTED;
1289 if (!(extract_flags & WIMLIB_EXTRACT_FLAG_NO_ACLS) &&
1290 required_features->security_descriptors &&
1291 !supported_features->security_descriptors)
1292 WARNING("Ignoring Windows NT security descriptors of %lu files",
1293 required_features->security_descriptors);
1296 if ((extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) &&
1297 required_features->unix_data && !supported_features->unix_data)
1299 ERROR("Extraction backend does not support UNIX data!");
1300 return WIMLIB_ERR_UNSUPPORTED;
1303 if (required_features->unix_data &&
1304 !(extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA))
1306 WARNING("Ignoring UNIX metadata of %lu files",
1307 required_features->unix_data);
1311 if (required_features->short_names &&
1312 !supported_features->short_names)
1314 if (extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_SHORT_NAMES) {
1315 ERROR("Extraction backend does not support DOS names!");
1316 return WIMLIB_ERR_UNSUPPORTED;
1318 WARNING("Ignoring DOS names of %lu files",
1319 required_features->short_names);
1323 if ((extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_TIMESTAMPS) &&
1324 !supported_features->timestamps)
1326 ERROR("Extraction backend does not support timestamps!");
1327 return WIMLIB_ERR_UNSUPPORTED;
1333 static const struct apply_operations *
1334 select_apply_operations(int extract_flags)
1337 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS)
1338 return &ntfs_3g_apply_ops;
1341 return &win32_apply_ops;
1343 return &unix_apply_ops;
1348 extract_trees(WIMStruct *wim, struct wim_dentry **trees, size_t num_trees,
1349 const tchar *target, int extract_flags)
1351 const struct apply_operations *ops;
1352 struct apply_ctx *ctx;
1354 LIST_HEAD(dentry_list);
1356 if (extract_flags & WIMLIB_EXTRACT_FLAG_TO_STDOUT) {
1357 ret = extract_dentries_to_stdout(trees, num_trees,
1362 num_trees = remove_duplicate_trees(trees, num_trees);
1363 num_trees = remove_contained_trees(trees, num_trees);
1365 ops = select_apply_operations(extract_flags);
1367 if (num_trees > 1 && ops->single_tree_only) {
1368 ERROR("Extracting multiple directory trees "
1369 "at once is not supported in %s extraction mode!",
1371 ret = WIMLIB_ERR_UNSUPPORTED;
1375 ctx = CALLOC(1, ops->context_size);
1377 ret = WIMLIB_ERR_NOMEM;
1382 ctx->target = target;
1383 ctx->target_nchars = tstrlen(target);
1384 ctx->extract_flags = extract_flags;
1385 if (ctx->wim->progfunc) {
1386 ctx->progfunc = ctx->wim->progfunc;
1387 ctx->progctx = ctx->wim->progctx;
1388 ctx->progress.extract.image = wim->current_image;
1389 ctx->progress.extract.extract_flags = (extract_flags &
1390 WIMLIB_EXTRACT_MASK_PUBLIC);
1391 ctx->progress.extract.wimfile_name = wim->filename;
1392 ctx->progress.extract.image_name = wimlib_get_image_name(wim,
1393 wim->current_image);
1394 ctx->progress.extract.target = target;
1396 INIT_LIST_HEAD(&ctx->blob_list);
1397 filedes_invalidate(&ctx->tmpfile_fd);
1398 ctx->apply_ops = ops;
1400 ret = (*ops->get_supported_features)(target, &ctx->supported_features);
1404 build_dentry_list(&dentry_list, trees, num_trees,
1406 WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE));
1408 dentry_list_get_features(&dentry_list, &ctx->required_features);
1410 ret = do_feature_check(&ctx->required_features, &ctx->supported_features,
1411 ctx->extract_flags);
1415 ret = dentry_list_calculate_extraction_names(&dentry_list, ctx);
1419 if (unlikely(list_empty(&dentry_list))) {
1420 WARNING("There is nothing to extract!");
1424 ret = dentry_list_resolve_streams(&dentry_list, ctx);
1428 dentry_list_build_inode_alias_lists(&dentry_list);
1430 ret = dentry_list_ref_streams(&dentry_list, ctx);
1434 if (extract_flags & WIMLIB_EXTRACT_FLAG_FROM_PIPE) {
1435 /* When extracting from a pipe, the number of bytes of data to
1436 * extract can't be determined in the normal way (examining the
1437 * blob table), since at this point all we have is a set of
1438 * SHA-1 message digests of blobs that need to be extracted.
1439 * However, we can get a reasonably accurate estimate by taking
1440 * <TOTALBYTES> from the corresponding <IMAGE> in the WIM XML
1441 * data. This does assume that a full image is being extracted,
1442 * but currently there is no API for doing otherwise. (Also,
1443 * subtract <HARDLINKBYTES> from this if hard links are
1444 * supported by the extraction mode.) */
1445 ctx->progress.extract.total_bytes =
1446 xml_get_image_total_bytes(wim->xml_info,
1447 wim->current_image);
1448 if (ctx->supported_features.hard_links) {
1449 ctx->progress.extract.total_bytes -=
1450 xml_get_image_hard_link_bytes(wim->xml_info,
1451 wim->current_image);
1455 ret = extract_progress(ctx,
1456 ((extract_flags & WIMLIB_EXTRACT_FLAG_IMAGEMODE) ?
1457 WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN :
1458 WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN));
1462 ret = (*ops->extract)(&dentry_list, ctx);
1466 if (ctx->progress.extract.completed_bytes <
1467 ctx->progress.extract.total_bytes)
1469 ctx->progress.extract.completed_bytes =
1470 ctx->progress.extract.total_bytes;
1471 ret = extract_progress(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS);
1476 ret = extract_progress(ctx,
1477 ((extract_flags & WIMLIB_EXTRACT_FLAG_IMAGEMODE) ?
1478 WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END :
1479 WIMLIB_PROGRESS_MSG_EXTRACT_TREE_END));
1481 destroy_blob_list(&ctx->blob_list);
1482 destroy_dentry_list(&dentry_list);
1489 mkdir_if_needed(const tchar *target)
1491 if (!tmkdir(target, 0755))
1494 if (errno == EEXIST)
1498 /* _wmkdir() fails with EACCES if called on a drive root directory. */
1499 if (errno == EACCES)
1503 ERROR_WITH_ERRNO("Failed to create directory \"%"TS"\"", target);
1504 return WIMLIB_ERR_MKDIR;
1507 /* Make sure the extraction flags make sense, and update them if needed. */
1509 check_extract_flags(const WIMStruct *wim, int *extract_flags_p)
1511 int extract_flags = *extract_flags_p;
1513 /* Check for invalid flag combinations */
1515 if ((extract_flags &
1516 (WIMLIB_EXTRACT_FLAG_NO_ACLS |
1517 WIMLIB_EXTRACT_FLAG_STRICT_ACLS)) == (WIMLIB_EXTRACT_FLAG_NO_ACLS |
1518 WIMLIB_EXTRACT_FLAG_STRICT_ACLS))
1519 return WIMLIB_ERR_INVALID_PARAM;
1521 if ((extract_flags &
1522 (WIMLIB_EXTRACT_FLAG_RPFIX |
1523 WIMLIB_EXTRACT_FLAG_NORPFIX)) == (WIMLIB_EXTRACT_FLAG_RPFIX |
1524 WIMLIB_EXTRACT_FLAG_NORPFIX))
1525 return WIMLIB_ERR_INVALID_PARAM;
1527 #ifndef WITH_NTFS_3G
1528 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1529 ERROR("wimlib was compiled without support for NTFS-3g, so\n"
1530 " it cannot apply a WIM image directly to an NTFS volume.");
1531 return WIMLIB_ERR_UNSUPPORTED;
1535 if (extract_flags & WIMLIB_EXTRACT_FLAG_WIMBOOT) {
1538 return WIMLIB_ERR_NO_FILENAME;
1540 ERROR("WIMBoot extraction is only supported on Windows!");
1541 return WIMLIB_ERR_UNSUPPORTED;
1545 if (extract_flags & (WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS4K |
1546 WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS8K |
1547 WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS16K |
1548 WIMLIB_EXTRACT_FLAG_COMPACT_LZX))
1552 count += ((extract_flags & WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS4K) != 0);
1553 count += ((extract_flags & WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS8K) != 0);
1554 count += ((extract_flags & WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS16K) != 0);
1555 count += ((extract_flags & WIMLIB_EXTRACT_FLAG_COMPACT_LZX) != 0);
1557 ERROR("Only one compression format can be specified "
1558 "for compact-mode extraction!");
1559 return WIMLIB_ERR_INVALID_PARAM;
1561 if (extract_flags & WIMLIB_EXTRACT_FLAG_WIMBOOT) {
1562 ERROR("Compact-mode extraction and WIMBoot-mode "
1563 "extraction are mutually exclusive!");
1564 return WIMLIB_ERR_INVALID_PARAM;
1567 ERROR("Compact-mode extraction (System Compression) "
1568 "is only supported on Windows!");
1569 return WIMLIB_ERR_UNSUPPORTED;
1574 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_RPFIX |
1575 WIMLIB_EXTRACT_FLAG_NORPFIX |
1576 WIMLIB_EXTRACT_FLAG_IMAGEMODE)) ==
1577 WIMLIB_EXTRACT_FLAG_IMAGEMODE)
1579 /* For full-image extraction, do reparse point fixups by default
1580 * if the WIM header says they are enabled. */
1581 if (wim->hdr.flags & WIM_HDR_FLAG_RP_FIX)
1582 extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
1585 *extract_flags_p = extract_flags;
1589 struct append_dentry_ctx {
1590 struct wim_dentry **dentries;
1591 size_t num_dentries;
1592 size_t num_alloc_dentries;
1596 append_dentry_cb(struct wim_dentry *dentry, void *_ctx)
1598 struct append_dentry_ctx *ctx = _ctx;
1600 if (ctx->num_dentries == ctx->num_alloc_dentries) {
1601 struct wim_dentry **new_dentries;
1604 new_length = max(ctx->num_alloc_dentries + 8,
1605 ctx->num_alloc_dentries * 3 / 2);
1606 new_dentries = REALLOC(ctx->dentries,
1607 new_length * sizeof(ctx->dentries[0]));
1608 if (new_dentries == NULL)
1609 return WIMLIB_ERR_NOMEM;
1610 ctx->dentries = new_dentries;
1611 ctx->num_alloc_dentries = new_length;
1613 ctx->dentries[ctx->num_dentries++] = dentry;
1617 /* Append dentries matched by a path which can contain wildcard characters. */
1619 append_matched_dentries(WIMStruct *wim, const tchar *orig_pattern,
1620 int extract_flags, struct append_dentry_ctx *ctx)
1622 const size_t count_before = ctx->num_dentries;
1626 pattern = canonicalize_wim_path(orig_pattern);
1628 return WIMLIB_ERR_NOMEM;
1629 ret = expand_path_pattern(wim_get_current_root_dentry(wim), pattern,
1630 append_dentry_cb, ctx);
1632 if (ret || ctx->num_dentries > count_before)
1634 if (extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_GLOB) {
1635 ERROR("No matches for path pattern \"%"TS"\"", orig_pattern);
1636 return WIMLIB_ERR_PATH_DOES_NOT_EXIST;
1638 WARNING("No matches for path pattern \"%"TS"\"", orig_pattern);
1643 do_wimlib_extract_paths(WIMStruct *wim, int image, const tchar *target,
1644 const tchar * const *paths, size_t num_paths,
1648 struct wim_dentry **trees;
1651 if (wim == NULL || target == NULL || target[0] == T('\0') ||
1652 (num_paths != 0 && paths == NULL))
1653 return WIMLIB_ERR_INVALID_PARAM;
1655 ret = check_extract_flags(wim, &extract_flags);
1659 ret = select_wim_image(wim, image);
1663 ret = wim_checksum_unhashed_blobs(wim);
1667 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_NTFS |
1668 WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE)) ==
1669 (WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE))
1671 ret = mkdir_if_needed(target);
1676 if (extract_flags & WIMLIB_EXTRACT_FLAG_GLOB_PATHS) {
1678 struct append_dentry_ctx append_dentry_ctx = {
1681 .num_alloc_dentries = 0,
1684 for (size_t i = 0; i < num_paths; i++) {
1685 ret = append_matched_dentries(wim, paths[i],
1687 &append_dentry_ctx);
1689 trees = append_dentry_ctx.dentries;
1690 goto out_free_trees;
1693 trees = append_dentry_ctx.dentries;
1694 num_trees = append_dentry_ctx.num_dentries;
1696 trees = MALLOC(num_paths * sizeof(trees[0]));
1698 return WIMLIB_ERR_NOMEM;
1700 for (size_t i = 0; i < num_paths; i++) {
1702 tchar *path = canonicalize_wim_path(paths[i]);
1704 ret = WIMLIB_ERR_NOMEM;
1705 goto out_free_trees;
1708 trees[i] = get_dentry(wim, path,
1709 WIMLIB_CASE_PLATFORM_DEFAULT);
1711 if (trees[i] == NULL) {
1712 ERROR("Path \"%"TS"\" does not exist "
1714 paths[i], wim->current_image);
1715 ret = WIMLIB_ERR_PATH_DOES_NOT_EXIST;
1716 goto out_free_trees;
1719 num_trees = num_paths;
1722 if (num_trees == 0) {
1724 goto out_free_trees;
1727 ret = extract_trees(wim, trees, num_trees, target, extract_flags);
1734 extract_single_image(WIMStruct *wim, int image,
1735 const tchar *target, int extract_flags)
1737 const tchar *path = WIMLIB_WIM_ROOT_PATH;
1738 extract_flags |= WIMLIB_EXTRACT_FLAG_IMAGEMODE;
1739 return do_wimlib_extract_paths(wim, image, target, &path, 1, extract_flags);
1742 static const tchar * const filename_forbidden_chars =
1751 /* This function checks if it is okay to use a WIM image's name as a directory
1754 image_name_ok_as_dir(const tchar *image_name)
1756 return image_name && *image_name &&
1757 !tstrpbrk(image_name, filename_forbidden_chars) &&
1758 tstrcmp(image_name, T(".")) &&
1759 tstrcmp(image_name, T("..")) &&
1760 tstrlen(image_name) <= 128;
1763 /* Extracts all images from the WIM to the directory @target, with the images
1764 * placed in subdirectories named by their image names. */
1766 extract_all_images(WIMStruct *wim, const tchar *target, int extract_flags)
1768 size_t output_path_len = tstrlen(target);
1769 tchar buf[output_path_len + 1 + 128 + 1];
1772 const tchar *image_name;
1774 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1775 ERROR("Cannot extract multiple images in NTFS extraction mode.");
1776 return WIMLIB_ERR_INVALID_PARAM;
1779 ret = mkdir_if_needed(target);
1782 tmemcpy(buf, target, output_path_len);
1783 buf[output_path_len] = OS_PREFERRED_PATH_SEPARATOR;
1784 for (image = 1; image <= wim->hdr.image_count; image++) {
1785 image_name = wimlib_get_image_name(wim, image);
1786 if (image_name_ok_as_dir(image_name)) {
1787 tstrcpy(buf + output_path_len + 1, image_name);
1789 /* Image name is empty or contains forbidden characters.
1790 * Use image number instead. */
1791 tsprintf(buf + output_path_len + 1, T("%d"), image);
1793 ret = extract_single_image(wim, image, buf, extract_flags);
1801 do_wimlib_extract_image(WIMStruct *wim, int image, const tchar *target,
1804 if (extract_flags & (WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE |
1805 WIMLIB_EXTRACT_FLAG_TO_STDOUT |
1806 WIMLIB_EXTRACT_FLAG_GLOB_PATHS))
1807 return WIMLIB_ERR_INVALID_PARAM;
1809 if (image == WIMLIB_ALL_IMAGES)
1810 return extract_all_images(wim, target, extract_flags);
1812 return extract_single_image(wim, image, target, extract_flags);
1816 /****************************************************************************
1818 ****************************************************************************/
1821 wimlib_extract_paths(WIMStruct *wim, int image, const tchar *target,
1822 const tchar * const *paths, size_t num_paths,
1825 if (extract_flags & ~WIMLIB_EXTRACT_MASK_PUBLIC)
1826 return WIMLIB_ERR_INVALID_PARAM;
1828 return do_wimlib_extract_paths(wim, image, target, paths, num_paths,
1833 wimlib_extract_pathlist(WIMStruct *wim, int image, const tchar *target,
1834 const tchar *path_list_file, int extract_flags)
1841 ret = read_path_list_file(path_list_file, &paths, &num_paths, &mem);
1843 ERROR("Failed to read path list file \"%"TS"\"",
1848 ret = wimlib_extract_paths(wim, image, target,
1849 (const tchar * const *)paths, num_paths,
1857 wimlib_extract_image_from_pipe_with_progress(int pipe_fd,
1858 const tchar *image_num_or_name,
1859 const tchar *target,
1861 wimlib_progress_func_t progfunc,
1866 struct filedes *in_fd;
1870 if (extract_flags & ~WIMLIB_EXTRACT_MASK_PUBLIC)
1871 return WIMLIB_ERR_INVALID_PARAM;
1873 /* Read the WIM header from the pipe and get a WIMStruct to represent
1874 * the pipable WIM. Caveats: Unlike getting a WIMStruct with
1875 * wimlib_open_wim(), getting a WIMStruct in this way will result in an
1876 * empty blob table, no XML data read, and no filename set. */
1877 ret = open_wim_as_WIMStruct(&pipe_fd, WIMLIB_OPEN_FLAG_FROM_PIPE, &pwm,
1882 /* Sanity check to make sure this is a pipable WIM. */
1883 if (pwm->hdr.magic != PWM_MAGIC) {
1884 ERROR("The WIM being read from file descriptor %d "
1885 "is not pipable!", pipe_fd);
1886 ret = WIMLIB_ERR_NOT_PIPABLE;
1887 goto out_wimlib_free;
1890 /* Sanity check to make sure the first part of a pipable split WIM is
1891 * sent over the pipe first. */
1892 if (pwm->hdr.part_number != 1) {
1893 ERROR("The first part of the split WIM must be "
1894 "sent over the pipe first.");
1895 ret = WIMLIB_ERR_INVALID_PIPABLE_WIM;
1896 goto out_wimlib_free;
1899 in_fd = &pwm->in_fd;
1900 wimlib_assert(in_fd->offset == WIM_HEADER_DISK_SIZE);
1902 /* As mentioned, the WIMStruct we created from the pipe does not have
1903 * XML data yet. Fix this by reading the extra copy of the XML data
1904 * that directly follows the header in pipable WIMs. (Note: see
1905 * write_pipable_wim() for more details about the format of pipable
1908 u8 hash[SHA1_HASH_SIZE];
1910 ret = read_pwm_blob_header(pwm, hash,
1911 &pwm->hdr.xml_data_reshdr, NULL);
1913 goto out_wimlib_free;
1915 if (!(pwm->hdr.xml_data_reshdr.flags & WIM_RESHDR_FLAG_METADATA)) {
1916 ERROR("Expected XML data, but found non-metadata resource.");
1917 ret = WIMLIB_ERR_INVALID_PIPABLE_WIM;
1918 goto out_wimlib_free;
1921 ret = read_wim_xml_data(pwm);
1923 goto out_wimlib_free;
1925 if (xml_get_image_count(pwm->xml_info) != pwm->hdr.image_count) {
1926 ERROR("Image count in XML data is not the same as in WIM header.");
1927 ret = WIMLIB_ERR_IMAGE_COUNT;
1928 goto out_wimlib_free;
1932 /* Get image index (this may use the XML data that was just read to
1933 * resolve an image name). */
1934 if (image_num_or_name) {
1935 image = wimlib_resolve_image(pwm, image_num_or_name);
1936 if (image == WIMLIB_NO_IMAGE) {
1937 ERROR("\"%"TS"\" is not a valid image in the pipable WIM!",
1939 ret = WIMLIB_ERR_INVALID_IMAGE;
1940 goto out_wimlib_free;
1941 } else if (image == WIMLIB_ALL_IMAGES) {
1942 ERROR("Applying all images from a pipe is not supported!");
1943 ret = WIMLIB_ERR_INVALID_IMAGE;
1944 goto out_wimlib_free;
1947 if (pwm->hdr.image_count != 1) {
1948 ERROR("No image was specified, but the pipable WIM "
1949 "did not contain exactly 1 image");
1950 ret = WIMLIB_ERR_INVALID_IMAGE;
1951 goto out_wimlib_free;
1956 /* Load the needed metadata resource. */
1957 for (i = 1; i <= pwm->hdr.image_count; i++) {
1958 ret = handle_pwm_metadata_resource(pwm, i, i == image);
1960 goto out_wimlib_free;
1962 /* Extract the image. */
1963 extract_flags |= WIMLIB_EXTRACT_FLAG_FROM_PIPE;
1964 ret = do_wimlib_extract_image(pwm, image, target, extract_flags);
1965 /* Clean up and return. */
1973 wimlib_extract_image_from_pipe(int pipe_fd, const tchar *image_num_or_name,
1974 const tchar *target, int extract_flags)
1976 return wimlib_extract_image_from_pipe_with_progress(pipe_fd,
1985 wimlib_extract_image(WIMStruct *wim, int image, const tchar *target,
1988 if (extract_flags & ~WIMLIB_EXTRACT_MASK_PUBLIC)
1989 return WIMLIB_ERR_INVALID_PARAM;
1990 return do_wimlib_extract_image(wim, image, target, extract_flags);