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.
48 #include "wimlib/apply.h"
49 #include "wimlib/assert.h"
50 #include "wimlib/blob_table.h"
51 #include "wimlib/dentry.h"
52 #include "wimlib/encoding.h"
53 #include "wimlib/endianness.h"
54 #include "wimlib/error.h"
55 #include "wimlib/metadata.h"
56 #include "wimlib/object_id.h"
57 #include "wimlib/pathlist.h"
58 #include "wimlib/paths.h"
59 #include "wimlib/pattern.h"
60 #include "wimlib/reparse.h"
61 #include "wimlib/resource.h"
62 #include "wimlib/security.h"
63 #include "wimlib/unix_data.h"
64 #include "wimlib/wim.h"
65 #include "wimlib/win32.h" /* for realpath() equivalent */
66 #include "wimlib/xml.h"
68 #define WIMLIB_EXTRACT_FLAG_FROM_PIPE 0x80000000
69 #define WIMLIB_EXTRACT_FLAG_IMAGEMODE 0x40000000
71 /* Keep in sync with wimlib.h */
72 #define WIMLIB_EXTRACT_MASK_PUBLIC \
73 (WIMLIB_EXTRACT_FLAG_NTFS | \
74 WIMLIB_EXTRACT_FLAG_UNIX_DATA | \
75 WIMLIB_EXTRACT_FLAG_NO_ACLS | \
76 WIMLIB_EXTRACT_FLAG_STRICT_ACLS | \
77 WIMLIB_EXTRACT_FLAG_RPFIX | \
78 WIMLIB_EXTRACT_FLAG_NORPFIX | \
79 WIMLIB_EXTRACT_FLAG_TO_STDOUT | \
80 WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES | \
81 WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS | \
82 WIMLIB_EXTRACT_FLAG_STRICT_TIMESTAMPS | \
83 WIMLIB_EXTRACT_FLAG_STRICT_SHORT_NAMES | \
84 WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS | \
85 WIMLIB_EXTRACT_FLAG_GLOB_PATHS | \
86 WIMLIB_EXTRACT_FLAG_STRICT_GLOB | \
87 WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES | \
88 WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE | \
89 WIMLIB_EXTRACT_FLAG_WIMBOOT | \
90 WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS4K | \
91 WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS8K | \
92 WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS16K | \
93 WIMLIB_EXTRACT_FLAG_COMPACT_LZX \
96 /* Send WIMLIB_PROGRESS_MSG_EXTRACT_FILE_STRUCTURE or
97 * WIMLIB_PROGRESS_MSG_EXTRACT_METADATA. */
99 do_file_extract_progress(struct apply_ctx *ctx, enum wimlib_progress_msg msg)
101 ctx->count_until_file_progress = 500; /* Arbitrary value to limit calls */
102 return extract_progress(ctx, msg);
106 start_file_phase(struct apply_ctx *ctx, u64 end_file_count, enum wimlib_progress_msg msg)
108 ctx->progress.extract.current_file_count = 0;
109 ctx->progress.extract.end_file_count = end_file_count;
110 return do_file_extract_progress(ctx, msg);
114 start_file_structure_phase(struct apply_ctx *ctx, u64 end_file_count)
116 return start_file_phase(ctx, end_file_count, WIMLIB_PROGRESS_MSG_EXTRACT_FILE_STRUCTURE);
120 start_file_metadata_phase(struct apply_ctx *ctx, u64 end_file_count)
122 return start_file_phase(ctx, end_file_count, WIMLIB_PROGRESS_MSG_EXTRACT_METADATA);
126 end_file_phase(struct apply_ctx *ctx, enum wimlib_progress_msg msg)
128 ctx->progress.extract.current_file_count = ctx->progress.extract.end_file_count;
129 return do_file_extract_progress(ctx, msg);
133 end_file_structure_phase(struct apply_ctx *ctx)
135 return end_file_phase(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_FILE_STRUCTURE);
139 end_file_metadata_phase(struct apply_ctx *ctx)
141 return end_file_phase(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_METADATA);
144 #define PWM_FOUND_WIM_HDR (-1)
146 /* Read the header for a blob in a pipable WIM. If @pwm_hdr_ret is not NULL,
147 * also look for a pipable WIM header and return PWM_FOUND_WIM_HDR if found. */
149 read_pwm_blob_header(WIMStruct *pwm, u8 hash_ret[SHA1_HASH_SIZE],
150 struct wim_reshdr *reshdr_ret,
151 struct wim_header_disk *pwm_hdr_ret)
154 struct pwm_blob_hdr blob_hdr;
157 ret = full_read(&pwm->in_fd, &blob_hdr, sizeof(blob_hdr));
161 magic = le64_to_cpu(blob_hdr.magic);
163 if (magic == PWM_MAGIC && pwm_hdr_ret != NULL) {
164 memcpy(pwm_hdr_ret, &blob_hdr, sizeof(blob_hdr));
165 ret = full_read(&pwm->in_fd,
166 (u8 *)pwm_hdr_ret + sizeof(blob_hdr),
167 sizeof(*pwm_hdr_ret) - sizeof(blob_hdr));
170 return PWM_FOUND_WIM_HDR;
173 if (unlikely(magic != PWM_BLOB_MAGIC)) {
174 ERROR("Data read on pipe is invalid (expected blob header)");
175 return WIMLIB_ERR_INVALID_PIPABLE_WIM;
178 copy_hash(hash_ret, blob_hdr.hash);
180 reshdr_ret->size_in_wim = 0; /* Not available */
181 reshdr_ret->flags = le32_to_cpu(blob_hdr.flags);
182 reshdr_ret->offset_in_wim = pwm->in_fd.offset;
183 reshdr_ret->uncompressed_size = le64_to_cpu(blob_hdr.uncompressed_size);
185 if (unlikely(reshdr_ret->uncompressed_size == 0)) {
186 ERROR("Data read on pipe is invalid (resource is of 0 size)");
187 return WIMLIB_ERR_INVALID_PIPABLE_WIM;
193 if (ret == WIMLIB_ERR_UNEXPECTED_END_OF_FILE)
194 ERROR("The pipe ended before all needed data was sent!");
196 ERROR_WITH_ERRNO("Error reading pipable WIM from pipe");
201 read_blobs_from_pipe(struct apply_ctx *ctx, const struct read_blob_callbacks *cbs)
204 u8 hash[SHA1_HASH_SIZE];
205 struct wim_reshdr reshdr;
206 struct wim_header_disk pwm_hdr;
207 struct wim_resource_descriptor rdesc;
208 struct blob_descriptor *blob;
210 copy_guid(ctx->progress.extract.guid, ctx->wim->hdr.guid);
211 ctx->progress.extract.part_number = ctx->wim->hdr.part_number;
212 ctx->progress.extract.total_parts = ctx->wim->hdr.total_parts;
213 ret = extract_progress(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN);
217 while (ctx->num_blobs_remaining) {
219 ret = read_pwm_blob_header(ctx->wim, hash, &reshdr, &pwm_hdr);
221 if (ret == PWM_FOUND_WIM_HDR) {
222 u16 part_number = le16_to_cpu(pwm_hdr.part_number);
223 u16 total_parts = le16_to_cpu(pwm_hdr.total_parts);
225 if (part_number == ctx->progress.extract.part_number &&
226 total_parts == ctx->progress.extract.total_parts &&
227 guids_equal(pwm_hdr.guid, ctx->progress.extract.guid))
230 copy_guid(ctx->progress.extract.guid, pwm_hdr.guid);
231 ctx->progress.extract.part_number = part_number;
232 ctx->progress.extract.total_parts = total_parts;
233 ret = extract_progress(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN);
243 if (!(reshdr.flags & WIM_RESHDR_FLAG_METADATA)
244 && (blob = lookup_blob(ctx->wim->blob_table, hash))
245 && (blob->out_refcnt))
247 wim_reshdr_to_desc_and_blob(&reshdr, ctx->wim, &rdesc, blob);
248 ret = read_blob_with_sha1(blob, cbs);
249 blob_unset_is_located_in_wim_resource(blob);
252 ctx->num_blobs_remaining--;
254 wim_reshdr_to_desc(&reshdr, ctx->wim, &rdesc);
255 ret = skip_wim_resource(&rdesc);
265 handle_pwm_metadata_resource(WIMStruct *pwm, int image, bool is_needed)
267 struct blob_descriptor *blob;
268 struct wim_reshdr reshdr;
269 struct wim_resource_descriptor *rdesc;
272 ret = WIMLIB_ERR_NOMEM;
273 blob = new_blob_descriptor();
277 ret = read_pwm_blob_header(pwm, blob->hash, &reshdr, NULL);
281 ret = WIMLIB_ERR_INVALID_PIPABLE_WIM;
282 if (!(reshdr.flags & WIM_RESHDR_FLAG_METADATA)) {
283 ERROR("Expected metadata resource, but found non-metadata "
288 ret = WIMLIB_ERR_NOMEM;
289 rdesc = MALLOC(sizeof(*rdesc));
293 wim_reshdr_to_desc_and_blob(&reshdr, pwm, rdesc, blob);
296 ret = WIMLIB_ERR_NOMEM;
297 pwm->image_metadata[image - 1] = new_unloaded_image_metadata(blob);
298 if (!pwm->image_metadata[image - 1])
302 /* If the metadata resource is for the image being extracted, then parse
303 * it and save the metadata in memory. Otherwise, skip over it. */
305 ret = select_wim_image(pwm, image);
307 ret = skip_wim_resource(rdesc);
309 free_blob_descriptor(blob);
313 /* Creates a temporary file opened for writing. The open file descriptor is
314 * returned in @fd_ret and its name is returned in @name_ret (dynamically
317 create_temporary_file(struct filedes *fd_ret, tchar **name_ret)
324 name = _wtempnam(NULL, L"wimlib");
326 ERROR_WITH_ERRNO("Failed to create temporary filename");
327 return WIMLIB_ERR_NOMEM;
329 raw_fd = _wopen(name, O_WRONLY | O_CREAT | O_EXCL | O_BINARY |
330 _O_SHORT_LIVED, 0600);
331 if (raw_fd < 0 && errno == EEXIST) {
335 #else /* __WIN32__ */
336 const char *tmpdir = getenv("TMPDIR");
339 name = MALLOC(strlen(tmpdir) + 1 + 6 + 6 + 1);
341 return WIMLIB_ERR_NOMEM;
342 sprintf(name, "%s/wimlibXXXXXX", tmpdir);
343 raw_fd = mkstemp(name);
344 #endif /* !__WIN32__ */
347 ERROR_WITH_ERRNO("Failed to create temporary file "
350 return WIMLIB_ERR_OPEN;
353 filedes_init(fd_ret, raw_fd);
359 begin_extract_blob_wrapper(struct blob_descriptor *blob, void *_ctx)
361 struct apply_ctx *ctx = _ctx;
363 ctx->cur_blob = blob;
364 ctx->cur_blob_offset = 0;
366 if (unlikely(blob->out_refcnt > MAX_OPEN_FILES))
367 return create_temporary_file(&ctx->tmpfile_fd, &ctx->tmpfile_name);
369 return call_begin_blob(blob, ctx->saved_cbs);
373 extract_chunk_wrapper(const void *chunk, size_t size, void *_ctx)
375 struct apply_ctx *ctx = _ctx;
376 union wimlib_progress_info *progress = &ctx->progress;
379 ctx->cur_blob_offset += size;
381 if (likely(ctx->supported_features.hard_links)) {
382 progress->extract.completed_bytes +=
383 (u64)size * ctx->cur_blob->out_refcnt;
384 if (ctx->cur_blob_offset == ctx->cur_blob->size)
385 progress->extract.completed_streams += ctx->cur_blob->out_refcnt;
387 const struct blob_extraction_target *targets =
388 blob_extraction_targets(ctx->cur_blob);
389 for (u32 i = 0; i < ctx->cur_blob->out_refcnt; i++) {
390 const struct wim_inode *inode = targets[i].inode;
391 const struct wim_dentry *dentry;
393 inode_for_each_extraction_alias(dentry, inode) {
394 progress->extract.completed_bytes += size;
395 if (ctx->cur_blob_offset == ctx->cur_blob->size)
396 progress->extract.completed_streams++;
400 if (progress->extract.completed_bytes >= ctx->next_progress) {
402 ret = extract_progress(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS);
406 set_next_progress(progress->extract.completed_bytes,
407 progress->extract.total_bytes,
408 &ctx->next_progress);
411 if (unlikely(filedes_valid(&ctx->tmpfile_fd))) {
412 /* Just extracting to temporary file for now. */
413 ret = full_write(&ctx->tmpfile_fd, chunk, size);
415 ERROR_WITH_ERRNO("Error writing data to "
416 "temporary file \"%"TS"\"",
422 return call_consume_chunk(chunk, size, ctx->saved_cbs);
425 /* Copy the blob's data from the temporary file to each of its targets.
427 * This is executed only in the very uncommon case that a blob is being
428 * extracted to more than MAX_OPEN_FILES targets! */
430 extract_from_tmpfile(const tchar *tmpfile_name,
431 const struct blob_descriptor *orig_blob,
432 const struct read_blob_callbacks *cbs)
434 struct blob_descriptor tmpfile_blob;
435 const struct blob_extraction_target *targets = blob_extraction_targets(orig_blob);
438 memcpy(&tmpfile_blob, orig_blob, sizeof(struct blob_descriptor));
439 tmpfile_blob.blob_location = BLOB_IN_FILE_ON_DISK;
440 tmpfile_blob.file_on_disk = (tchar *)tmpfile_name;
441 tmpfile_blob.out_refcnt = 1;
443 for (u32 i = 0; i < orig_blob->out_refcnt; i++) {
444 tmpfile_blob.inline_blob_extraction_targets[0] = targets[i];
445 ret = read_blob_with_cbs(&tmpfile_blob, cbs);
453 end_extract_blob_wrapper(struct blob_descriptor *blob, int status, void *_ctx)
455 struct apply_ctx *ctx = _ctx;
457 if (unlikely(filedes_valid(&ctx->tmpfile_fd))) {
458 filedes_close(&ctx->tmpfile_fd);
460 status = extract_from_tmpfile(ctx->tmpfile_name, blob,
462 filedes_invalidate(&ctx->tmpfile_fd);
463 tunlink(ctx->tmpfile_name);
464 FREE(ctx->tmpfile_name);
468 return call_end_blob(blob, status, ctx->saved_cbs);
472 * Read the list of blobs to extract and feed their data into the specified
473 * callback functions.
475 * This handles checksumming each blob.
477 * This also handles sending WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS.
479 * This also works if the WIM is being read from a pipe.
481 * This also will split up blobs that will need to be extracted to more than
482 * MAX_OPEN_FILES locations, as measured by the 'out_refcnt' of each blob.
483 * Therefore, the apply_operations implementation need not worry about running
484 * out of file descriptors, unless it might open more than one file descriptor
485 * per 'blob_extraction_target' (e.g. Win32 currently might because the
486 * destination file system might not support hard links).
489 extract_blob_list(struct apply_ctx *ctx, const struct read_blob_callbacks *cbs)
491 struct read_blob_callbacks wrapper_cbs = {
492 .begin_blob = begin_extract_blob_wrapper,
493 .consume_chunk = extract_chunk_wrapper,
494 .end_blob = end_extract_blob_wrapper,
497 ctx->saved_cbs = cbs;
498 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_FROM_PIPE) {
499 return read_blobs_from_pipe(ctx, &wrapper_cbs);
501 return read_blob_list(&ctx->blob_list,
502 offsetof(struct blob_descriptor,
504 &wrapper_cbs, VERIFY_BLOB_HASHES);
508 /* Extract a WIM dentry to standard output.
510 * This obviously doesn't make sense in all cases. We return an error if the
511 * dentry does not correspond to a regular file. Otherwise we extract the
512 * unnamed data stream only. */
514 extract_dentry_to_stdout(struct wim_dentry *dentry,
515 const struct blob_table *blob_table)
517 struct wim_inode *inode = dentry->d_inode;
518 struct blob_descriptor *blob;
519 struct filedes _stdout;
521 if (inode->i_attributes & (FILE_ATTRIBUTE_REPARSE_POINT |
522 FILE_ATTRIBUTE_DIRECTORY |
523 FILE_ATTRIBUTE_ENCRYPTED))
525 ERROR("\"%"TS"\" is not a regular file and therefore cannot be "
526 "extracted to standard output", dentry_full_path(dentry));
527 return WIMLIB_ERR_NOT_A_REGULAR_FILE;
530 blob = inode_get_blob_for_unnamed_data_stream(inode, blob_table);
532 const u8 *hash = inode_get_hash_of_unnamed_data_stream(inode);
533 if (!is_zero_hash(hash))
534 return blob_not_found_error(inode, hash);
538 filedes_init(&_stdout, STDOUT_FILENO);
539 return extract_blob_to_fd(blob, &_stdout);
543 extract_dentries_to_stdout(struct wim_dentry **dentries, size_t num_dentries,
544 const struct blob_table *blob_table)
546 for (size_t i = 0; i < num_dentries; i++) {
547 int ret = extract_dentry_to_stdout(dentries[i], blob_table);
554 /**********************************************************************/
557 * Removes duplicate dentries from the array.
559 * Returns the new number of dentries, packed at the front of the array.
562 remove_duplicate_trees(struct wim_dentry **trees, size_t num_trees)
565 for (i = 0; i < num_trees; i++) {
566 if (!trees[i]->d_tmp_flag) {
567 /* Found distinct dentry. */
568 trees[i]->d_tmp_flag = 1;
569 trees[j++] = trees[i];
572 for (i = 0; i < j; i++)
573 trees[i]->d_tmp_flag = 0;
578 * Remove dentries that are descendants of other dentries in the array.
580 * Returns the new number of dentries, packed at the front of the array.
583 remove_contained_trees(struct wim_dentry **trees, size_t num_trees)
586 for (i = 0; i < num_trees; i++)
587 trees[i]->d_tmp_flag = 1;
588 for (i = 0; i < num_trees; i++) {
589 struct wim_dentry *d = trees[i];
590 while (!dentry_is_root(d)) {
595 trees[j++] = trees[i];
599 trees[i]->d_tmp_flag = 0;
602 for (i = 0; i < j; i++)
603 trees[i]->d_tmp_flag = 0;
608 dentry_append_to_list(struct wim_dentry *dentry, void *_dentry_list)
610 struct list_head *dentry_list = _dentry_list;
611 list_add_tail(&dentry->d_extraction_list_node, dentry_list);
616 dentry_reset_extraction_list_node(struct wim_dentry *dentry)
618 dentry->d_extraction_list_node = (struct list_head){NULL, NULL};
622 dentry_delete_from_list(struct wim_dentry *dentry, void *_ignore)
624 if (will_extract_dentry(dentry)) {
625 list_del(&dentry->d_extraction_list_node);
626 dentry_reset_extraction_list_node(dentry);
632 * Build the preliminary list of dentries to be extracted.
634 * The list maintains the invariant that if d1 and d2 are in the list and d1 is
635 * an ancestor of d2, then d1 appears before d2 in the list.
638 build_dentry_list(struct list_head *dentry_list, struct wim_dentry **trees,
639 size_t num_trees, bool add_ancestors)
641 INIT_LIST_HEAD(dentry_list);
643 /* Add the trees recursively. */
644 for (size_t i = 0; i < num_trees; i++)
645 for_dentry_in_tree(trees[i], dentry_append_to_list, dentry_list);
647 /* If requested, add ancestors of the trees. */
649 for (size_t i = 0; i < num_trees; i++) {
650 struct wim_dentry *dentry = trees[i];
651 struct wim_dentry *ancestor;
652 struct list_head *place_after;
654 if (dentry_is_root(dentry))
657 place_after = dentry_list;
660 ancestor = ancestor->d_parent;
661 if (will_extract_dentry(ancestor)) {
662 place_after = &ancestor->d_extraction_list_node;
665 } while (!dentry_is_root(ancestor));
669 ancestor = ancestor->d_parent;
670 if (will_extract_dentry(ancestor))
672 list_add(&ancestor->d_extraction_list_node, place_after);
673 } while (!dentry_is_root(ancestor));
679 destroy_dentry_list(struct list_head *dentry_list)
681 struct wim_dentry *dentry, *tmp;
682 struct wim_inode *inode;
684 list_for_each_entry_safe(dentry, tmp, dentry_list, d_extraction_list_node) {
685 inode = dentry->d_inode;
686 dentry_reset_extraction_list_node(dentry);
687 inode->i_visited = 0;
688 inode->i_can_externally_back = 0;
689 if ((void *)dentry->d_extraction_name != (void *)dentry->d_name)
690 FREE(dentry->d_extraction_name);
691 dentry->d_extraction_name = NULL;
692 dentry->d_extraction_name_nchars = 0;
697 destroy_blob_list(struct list_head *blob_list)
699 struct blob_descriptor *blob;
701 list_for_each_entry(blob, blob_list, extraction_list)
702 if (blob->out_refcnt > ARRAY_LEN(blob->inline_blob_extraction_targets))
703 FREE(blob->blob_extraction_targets);
707 static const utf16lechar replacement_char = cpu_to_le16(0xfffd);
709 static const utf16lechar replacement_char = cpu_to_le16('?');
713 file_name_valid(utf16lechar *name, size_t num_chars, bool fix)
719 for (i = 0; i < num_chars; i++) {
720 switch (le16_to_cpu(name[i])) {
722 case '\x01'...'\x1F':
735 name[i] = replacement_char;
745 dentry_calculate_extraction_name(struct wim_dentry *dentry,
746 struct apply_ctx *ctx)
750 if (dentry_is_root(dentry))
754 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
755 dentry->d_extraction_name = dentry->d_name;
756 dentry->d_extraction_name_nchars = dentry->d_name_nbytes /
762 if (!ctx->supported_features.case_sensitive_filenames) {
763 struct wim_dentry *other;
764 dentry_for_each_ci_match(other, dentry) {
765 if (will_extract_dentry(other)) {
766 if (ctx->extract_flags &
767 WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS) {
768 WARNING("\"%"TS"\" has the same "
769 "case-insensitive name as "
770 "\"%"TS"\"; extracting "
771 "dummy name instead",
772 dentry_full_path(dentry),
773 dentry_full_path(other));
776 WARNING("Not extracting \"%"TS"\": "
777 "has same case-insensitive "
779 dentry_full_path(dentry),
780 dentry_full_path(other));
787 if (file_name_valid(dentry->d_name, dentry->d_name_nbytes / 2, false)) {
789 ret = utf16le_get_tstr(dentry->d_name,
790 dentry->d_name_nbytes,
791 (const tchar **)&dentry->d_extraction_name,
793 dentry->d_extraction_name_nchars = nbytes / sizeof(tchar);
796 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES)
798 WARNING("\"%"TS"\" has an invalid filename "
799 "that is not supported on this platform; "
800 "extracting dummy name instead",
801 dentry_full_path(dentry));
804 WARNING("Not extracting \"%"TS"\": has an invalid filename "
805 "that is not supported on this platform",
806 dentry_full_path(dentry));
813 utf16lechar utf16_name_copy[dentry->d_name_nbytes / 2];
815 memcpy(utf16_name_copy, dentry->d_name, dentry->d_name_nbytes);
816 file_name_valid(utf16_name_copy, dentry->d_name_nbytes / 2, true);
818 const tchar *tchar_name;
821 ret = utf16le_get_tstr(utf16_name_copy,
822 dentry->d_name_nbytes,
823 &tchar_name, &tchar_nchars);
827 tchar_nchars /= sizeof(tchar);
829 size_t fixed_name_num_chars = tchar_nchars;
830 tchar fixed_name[tchar_nchars + 50];
832 tmemcpy(fixed_name, tchar_name, tchar_nchars);
833 fixed_name_num_chars += tsprintf(fixed_name + tchar_nchars,
834 T(" (invalid filename #%lu)"),
835 ++ctx->invalid_sequence);
837 utf16le_put_tstr(tchar_name);
839 dentry->d_extraction_name = TSTRDUP(fixed_name);
840 if (!dentry->d_extraction_name)
841 return WIMLIB_ERR_NOMEM;
842 dentry->d_extraction_name_nchars = fixed_name_num_chars;
847 for_dentry_in_tree(dentry, dentry_delete_from_list, NULL);
852 * Calculate the actual filename component at which each WIM dentry will be
853 * extracted, with special handling for dentries that are unsupported by the
854 * extraction backend or have invalid names.
856 * ctx->supported_features must be filled in.
858 * Possible error codes: WIMLIB_ERR_NOMEM, WIMLIB_ERR_INVALID_UTF16_STRING
861 dentry_list_calculate_extraction_names(struct list_head *dentry_list,
862 struct apply_ctx *ctx)
864 struct list_head *prev, *cur;
866 /* Can't use list_for_each_entry() because a call to
867 * dentry_calculate_extraction_name() may delete the current dentry and
868 * its children from the list. */
872 struct wim_dentry *dentry;
876 if (cur == dentry_list)
879 dentry = list_entry(cur, struct wim_dentry, d_extraction_list_node);
881 ret = dentry_calculate_extraction_name(dentry, ctx);
885 if (prev->next == cur)
888 ; /* Current dentry and its children (which follow in
889 the list) were deleted. prev stays the same. */
895 dentry_resolve_streams(struct wim_dentry *dentry, int extract_flags,
896 struct blob_table *blob_table)
898 struct wim_inode *inode = dentry->d_inode;
899 struct blob_descriptor *blob;
903 /* Special case: when extracting from a pipe, the WIM blob table is
904 * initially empty, so "resolving" an inode's streams is initially not
905 * possible. However, we still need to keep track of which blobs,
906 * identified by SHA-1 message digests, need to be extracted, so we
907 * "resolve" the inode's streams anyway by allocating a 'struct
908 * blob_descriptor' for each one. */
909 if (extract_flags & WIMLIB_EXTRACT_FLAG_FROM_PIPE)
911 ret = inode_resolve_streams(inode, blob_table, force);
914 for (unsigned i = 0; i < inode->i_num_streams; i++) {
915 blob = stream_blob_resolved(&inode->i_streams[i]);
917 blob->out_refcnt = 0;
923 * For each dentry to be extracted, resolve all streams in the corresponding
924 * inode and set 'out_refcnt' in all referenced blob_descriptors to 0.
926 * Possible error codes: WIMLIB_ERR_RESOURCE_NOT_FOUND, WIMLIB_ERR_NOMEM.
929 dentry_list_resolve_streams(struct list_head *dentry_list,
930 struct apply_ctx *ctx)
932 struct wim_dentry *dentry;
935 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
936 ret = dentry_resolve_streams(dentry,
938 ctx->wim->blob_table);
946 ref_stream(struct wim_inode_stream *strm, struct wim_dentry *dentry,
947 struct apply_ctx *ctx)
949 struct wim_inode *inode = dentry->d_inode;
950 struct blob_descriptor *blob = stream_blob_resolved(strm);
951 struct blob_extraction_target *targets;
956 /* Tally the size only for each actual extraction of the stream (not
957 * additional hard links to the inode). */
958 if (inode->i_visited && ctx->supported_features.hard_links)
961 ctx->progress.extract.total_bytes += blob->size;
962 ctx->progress.extract.total_streams++;
964 if (inode->i_visited)
967 /* Add each blob to 'ctx->blob_list' only one time, regardless of how
968 * many extraction targets it will have. */
969 if (blob->out_refcnt == 0) {
970 list_add_tail(&blob->extraction_list, &ctx->blob_list);
971 ctx->num_blobs_remaining++;
974 /* Set this stream as an extraction target of 'blob'. */
976 if (blob->out_refcnt < ARRAY_LEN(blob->inline_blob_extraction_targets)) {
977 targets = blob->inline_blob_extraction_targets;
979 struct blob_extraction_target *prev_targets;
980 size_t alloc_blob_extraction_targets;
982 if (blob->out_refcnt == ARRAY_LEN(blob->inline_blob_extraction_targets)) {
984 alloc_blob_extraction_targets = ARRAY_LEN(blob->inline_blob_extraction_targets);
986 prev_targets = blob->blob_extraction_targets;
987 alloc_blob_extraction_targets = blob->alloc_blob_extraction_targets;
990 if (blob->out_refcnt == alloc_blob_extraction_targets) {
991 alloc_blob_extraction_targets *= 2;
992 targets = REALLOC(prev_targets,
993 alloc_blob_extraction_targets *
996 return WIMLIB_ERR_NOMEM;
999 blob->inline_blob_extraction_targets,
1000 sizeof(blob->inline_blob_extraction_targets));
1002 blob->blob_extraction_targets = targets;
1003 blob->alloc_blob_extraction_targets = alloc_blob_extraction_targets;
1005 targets = blob->blob_extraction_targets;
1007 targets[blob->out_refcnt].inode = inode;
1008 targets[blob->out_refcnt].stream = strm;
1014 ref_stream_if_needed(struct wim_dentry *dentry, struct wim_inode *inode,
1015 struct wim_inode_stream *strm, struct apply_ctx *ctx)
1017 bool need_stream = false;
1018 switch (strm->stream_type) {
1019 case STREAM_TYPE_DATA:
1020 if (stream_is_named(strm)) {
1021 /* Named data stream */
1022 if (ctx->supported_features.named_data_streams)
1024 } else if (!(inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
1025 FILE_ATTRIBUTE_ENCRYPTED))
1026 && !(inode_is_symlink(inode)
1027 && !ctx->supported_features.reparse_points
1028 && ctx->supported_features.symlink_reparse_points))
1031 * Unnamed data stream. Skip if any of the following is true:
1033 * - file is a directory
1034 * - file is encrypted
1035 * - backend needs to create the file as UNIX symlink
1036 * - backend will extract the stream as externally
1037 * backed from the WIM archive itself
1039 if (ctx->apply_ops->will_back_from_wim) {
1040 int ret = (*ctx->apply_ops->will_back_from_wim)(dentry, ctx);
1041 if (ret > 0) /* Error? */
1043 if (ret < 0) /* Won't externally back? */
1050 case STREAM_TYPE_REPARSE_POINT:
1051 wimlib_assert(inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT);
1052 if (ctx->supported_features.reparse_points ||
1053 (inode_is_symlink(inode) &&
1054 ctx->supported_features.symlink_reparse_points))
1057 case STREAM_TYPE_EFSRPC_RAW_DATA:
1058 wimlib_assert(inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED);
1059 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY) {
1060 if (ctx->supported_features.encrypted_directories)
1063 if (ctx->supported_features.encrypted_files)
1069 return ref_stream(strm, dentry, ctx);
1074 dentry_ref_streams(struct wim_dentry *dentry, struct apply_ctx *ctx)
1076 struct wim_inode *inode = dentry->d_inode;
1077 for (unsigned i = 0; i < inode->i_num_streams; i++) {
1078 int ret = ref_stream_if_needed(dentry, inode,
1079 &inode->i_streams[i], ctx);
1083 inode->i_visited = 1;
1088 * Given a list of dentries to be extracted, build the list of blobs that need
1089 * to be extracted, and for each blob determine the streams to which that blob
1090 * will be extracted.
1092 * This also initializes the extract progress info with byte and blob
1095 * ctx->supported_features must be filled in.
1098 dentry_list_ref_streams(struct list_head *dentry_list, struct apply_ctx *ctx)
1100 struct wim_dentry *dentry;
1103 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
1104 ret = dentry_ref_streams(dentry, ctx);
1108 list_for_each_entry(dentry, dentry_list, d_extraction_list_node)
1109 dentry->d_inode->i_visited = 0;
1114 dentry_list_build_inode_alias_lists(struct list_head *dentry_list)
1116 struct wim_dentry *dentry;
1118 list_for_each_entry(dentry, dentry_list, d_extraction_list_node)
1119 dentry->d_inode->i_first_extraction_alias = NULL;
1121 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
1122 dentry->d_next_extraction_alias = dentry->d_inode->i_first_extraction_alias;
1123 dentry->d_inode->i_first_extraction_alias = dentry;
1128 inode_tally_features(const struct wim_inode *inode,
1129 struct wim_features *features)
1131 if (inode->i_attributes & FILE_ATTRIBUTE_READONLY)
1132 features->readonly_files++;
1133 if (inode->i_attributes & FILE_ATTRIBUTE_HIDDEN)
1134 features->hidden_files++;
1135 if (inode->i_attributes & FILE_ATTRIBUTE_SYSTEM)
1136 features->system_files++;
1137 if (inode->i_attributes & FILE_ATTRIBUTE_ARCHIVE)
1138 features->archive_files++;
1139 if (inode->i_attributes & FILE_ATTRIBUTE_COMPRESSED)
1140 features->compressed_files++;
1141 if (inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED) {
1142 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)
1143 features->encrypted_directories++;
1145 features->encrypted_files++;
1147 if (inode->i_attributes & FILE_ATTRIBUTE_NOT_CONTENT_INDEXED)
1148 features->not_context_indexed_files++;
1149 if (inode->i_attributes & FILE_ATTRIBUTE_SPARSE_FILE)
1150 features->sparse_files++;
1151 if (inode_has_named_data_stream(inode))
1152 features->named_data_streams++;
1153 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1154 features->reparse_points++;
1155 if (inode_is_symlink(inode))
1156 features->symlink_reparse_points++;
1158 features->other_reparse_points++;
1160 if (inode_has_security_descriptor(inode))
1161 features->security_descriptors++;
1162 if (inode_has_unix_data(inode))
1163 features->unix_data++;
1164 if (inode_has_object_id(inode))
1165 features->object_ids++;
1168 /* Tally features necessary to extract a dentry and the corresponding inode. */
1170 dentry_tally_features(struct wim_dentry *dentry, struct wim_features *features)
1172 struct wim_inode *inode = dentry->d_inode;
1174 if (dentry_has_short_name(dentry))
1175 features->short_names++;
1177 if (inode->i_visited) {
1178 features->hard_links++;
1180 inode_tally_features(inode, features);
1181 inode->i_visited = 1;
1185 /* Tally the features necessary to extract the specified dentries. */
1187 dentry_list_get_features(struct list_head *dentry_list,
1188 struct wim_features *features)
1190 struct wim_dentry *dentry;
1192 list_for_each_entry(dentry, dentry_list, d_extraction_list_node)
1193 dentry_tally_features(dentry, features);
1195 list_for_each_entry(dentry, dentry_list, d_extraction_list_node)
1196 dentry->d_inode->i_visited = 0;
1200 do_feature_check(const struct wim_features *required_features,
1201 const struct wim_features *supported_features,
1204 /* Encrypted files. */
1205 if (required_features->encrypted_files &&
1206 !supported_features->encrypted_files)
1207 WARNING("Ignoring EFS-encrypted data of %lu files",
1208 required_features->encrypted_files);
1210 /* Named data streams. */
1211 if (required_features->named_data_streams &&
1212 !supported_features->named_data_streams)
1213 WARNING("Ignoring named data streams of %lu files",
1214 required_features->named_data_streams);
1216 /* File attributes. */
1217 if (!(extract_flags & WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES)) {
1219 if (required_features->readonly_files &&
1220 !supported_features->readonly_files)
1221 WARNING("Ignoring FILE_ATTRIBUTE_READONLY of %lu files",
1222 required_features->readonly_files);
1224 if (required_features->hidden_files &&
1225 !supported_features->hidden_files)
1226 WARNING("Ignoring FILE_ATTRIBUTE_HIDDEN of %lu files",
1227 required_features->hidden_files);
1229 if (required_features->system_files &&
1230 !supported_features->system_files)
1231 WARNING("Ignoring FILE_ATTRIBUTE_SYSTEM of %lu files",
1232 required_features->system_files);
1234 /* Note: Don't bother the user about FILE_ATTRIBUTE_ARCHIVE.
1235 * We're an archive program, so theoretically we can do what we
1238 if (required_features->compressed_files &&
1239 !supported_features->compressed_files)
1240 WARNING("Ignoring FILE_ATTRIBUTE_COMPRESSED of %lu files",
1241 required_features->compressed_files);
1243 if (required_features->not_context_indexed_files &&
1244 !supported_features->not_context_indexed_files)
1245 WARNING("Ignoring FILE_ATTRIBUTE_NOT_CONTENT_INDEXED of %lu files",
1246 required_features->not_context_indexed_files);
1248 if (required_features->sparse_files &&
1249 !supported_features->sparse_files)
1250 WARNING("Ignoring FILE_ATTRIBUTE_SPARSE_FILE of %lu files",
1251 required_features->sparse_files);
1253 if (required_features->encrypted_directories &&
1254 !supported_features->encrypted_directories)
1255 WARNING("Ignoring FILE_ATTRIBUTE_ENCRYPTED of %lu directories",
1256 required_features->encrypted_directories);
1260 if (required_features->hard_links && !supported_features->hard_links)
1261 WARNING("Extracting %lu hard links as independent files",
1262 required_features->hard_links);
1264 /* Symbolic links and reparse points. */
1265 if ((extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS) &&
1266 required_features->symlink_reparse_points &&
1267 !supported_features->symlink_reparse_points &&
1268 !supported_features->reparse_points)
1270 ERROR("Extraction backend does not support symbolic links!");
1271 return WIMLIB_ERR_UNSUPPORTED;
1273 if (required_features->reparse_points &&
1274 !supported_features->reparse_points)
1276 if (supported_features->symlink_reparse_points) {
1277 if (required_features->other_reparse_points) {
1278 WARNING("Ignoring reparse data of %lu non-symlink/junction files",
1279 required_features->other_reparse_points);
1282 WARNING("Ignoring reparse data of %lu files",
1283 required_features->reparse_points);
1287 /* Security descriptors. */
1288 if (((extract_flags & (WIMLIB_EXTRACT_FLAG_STRICT_ACLS |
1289 WIMLIB_EXTRACT_FLAG_UNIX_DATA))
1290 == WIMLIB_EXTRACT_FLAG_STRICT_ACLS) &&
1291 required_features->security_descriptors &&
1292 !supported_features->security_descriptors)
1294 ERROR("Extraction backend does not support security descriptors!");
1295 return WIMLIB_ERR_UNSUPPORTED;
1297 if (!(extract_flags & WIMLIB_EXTRACT_FLAG_NO_ACLS) &&
1298 required_features->security_descriptors &&
1299 !supported_features->security_descriptors)
1300 WARNING("Ignoring Windows NT security descriptors of %lu files",
1301 required_features->security_descriptors);
1304 if ((extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) &&
1305 required_features->unix_data && !supported_features->unix_data)
1307 ERROR("Extraction backend does not support UNIX data!");
1308 return WIMLIB_ERR_UNSUPPORTED;
1311 if (required_features->unix_data &&
1312 !(extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA))
1314 WARNING("Ignoring UNIX metadata of %lu files",
1315 required_features->unix_data);
1319 if (required_features->object_ids && !supported_features->object_ids) {
1320 WARNING("Ignoring object IDs of %lu files",
1321 required_features->object_ids);
1325 if (required_features->short_names &&
1326 !supported_features->short_names)
1328 if (extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_SHORT_NAMES) {
1329 ERROR("Extraction backend does not support DOS names!");
1330 return WIMLIB_ERR_UNSUPPORTED;
1332 WARNING("Ignoring DOS names of %lu files",
1333 required_features->short_names);
1337 if ((extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_TIMESTAMPS) &&
1338 !supported_features->timestamps)
1340 ERROR("Extraction backend does not support timestamps!");
1341 return WIMLIB_ERR_UNSUPPORTED;
1347 static const struct apply_operations *
1348 select_apply_operations(int extract_flags)
1351 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS)
1352 return &ntfs_3g_apply_ops;
1355 return &win32_apply_ops;
1357 return &unix_apply_ops;
1362 extract_trees(WIMStruct *wim, struct wim_dentry **trees, size_t num_trees,
1363 const tchar *target, int extract_flags)
1365 const struct apply_operations *ops;
1366 struct apply_ctx *ctx;
1368 LIST_HEAD(dentry_list);
1370 if (extract_flags & WIMLIB_EXTRACT_FLAG_TO_STDOUT) {
1371 ret = extract_dentries_to_stdout(trees, num_trees,
1376 num_trees = remove_duplicate_trees(trees, num_trees);
1377 num_trees = remove_contained_trees(trees, num_trees);
1379 ops = select_apply_operations(extract_flags);
1381 if (num_trees > 1 && ops->single_tree_only) {
1382 ERROR("Extracting multiple directory trees "
1383 "at once is not supported in %s extraction mode!",
1385 ret = WIMLIB_ERR_UNSUPPORTED;
1389 ctx = CALLOC(1, ops->context_size);
1391 ret = WIMLIB_ERR_NOMEM;
1396 ctx->target = target;
1397 ctx->target_nchars = tstrlen(target);
1398 ctx->extract_flags = extract_flags;
1399 if (ctx->wim->progfunc) {
1400 ctx->progfunc = ctx->wim->progfunc;
1401 ctx->progctx = ctx->wim->progctx;
1402 ctx->progress.extract.image = wim->current_image;
1403 ctx->progress.extract.extract_flags = (extract_flags &
1404 WIMLIB_EXTRACT_MASK_PUBLIC);
1405 ctx->progress.extract.wimfile_name = wim->filename;
1406 ctx->progress.extract.image_name = wimlib_get_image_name(wim,
1407 wim->current_image);
1408 ctx->progress.extract.target = target;
1410 INIT_LIST_HEAD(&ctx->blob_list);
1411 filedes_invalidate(&ctx->tmpfile_fd);
1412 ctx->apply_ops = ops;
1414 ret = (*ops->get_supported_features)(target, &ctx->supported_features);
1418 build_dentry_list(&dentry_list, trees, num_trees,
1420 WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE));
1422 dentry_list_get_features(&dentry_list, &ctx->required_features);
1424 ret = do_feature_check(&ctx->required_features, &ctx->supported_features,
1425 ctx->extract_flags);
1429 ret = dentry_list_calculate_extraction_names(&dentry_list, ctx);
1433 if (unlikely(list_empty(&dentry_list))) {
1434 WARNING("There is nothing to extract!");
1438 ret = dentry_list_resolve_streams(&dentry_list, ctx);
1442 dentry_list_build_inode_alias_lists(&dentry_list);
1444 ret = dentry_list_ref_streams(&dentry_list, ctx);
1448 if (extract_flags & WIMLIB_EXTRACT_FLAG_FROM_PIPE) {
1449 /* When extracting from a pipe, the number of bytes of data to
1450 * extract can't be determined in the normal way (examining the
1451 * blob table), since at this point all we have is a set of
1452 * SHA-1 message digests of blobs that need to be extracted.
1453 * However, we can get a reasonably accurate estimate by taking
1454 * <TOTALBYTES> from the corresponding <IMAGE> in the WIM XML
1455 * data. This does assume that a full image is being extracted,
1456 * but currently there is no API for doing otherwise. (Also,
1457 * subtract <HARDLINKBYTES> from this if hard links are
1458 * supported by the extraction mode.) */
1459 ctx->progress.extract.total_bytes =
1460 xml_get_image_total_bytes(wim->xml_info,
1461 wim->current_image);
1462 if (ctx->supported_features.hard_links) {
1463 ctx->progress.extract.total_bytes -=
1464 xml_get_image_hard_link_bytes(wim->xml_info,
1465 wim->current_image);
1469 ret = extract_progress(ctx,
1470 ((extract_flags & WIMLIB_EXTRACT_FLAG_IMAGEMODE) ?
1471 WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN :
1472 WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN));
1476 ret = (*ops->extract)(&dentry_list, ctx);
1480 if (ctx->progress.extract.completed_bytes <
1481 ctx->progress.extract.total_bytes)
1483 ctx->progress.extract.completed_bytes =
1484 ctx->progress.extract.total_bytes;
1485 ret = extract_progress(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS);
1490 ret = extract_progress(ctx,
1491 ((extract_flags & WIMLIB_EXTRACT_FLAG_IMAGEMODE) ?
1492 WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END :
1493 WIMLIB_PROGRESS_MSG_EXTRACT_TREE_END));
1495 destroy_blob_list(&ctx->blob_list);
1496 destroy_dentry_list(&dentry_list);
1503 mkdir_if_needed(const tchar *target)
1505 if (!tmkdir(target, 0755))
1508 if (errno == EEXIST)
1512 /* _wmkdir() fails with EACCES if called on a drive root directory. */
1513 if (errno == EACCES)
1517 ERROR_WITH_ERRNO("Failed to create directory \"%"TS"\"", target);
1518 return WIMLIB_ERR_MKDIR;
1521 /* Make sure the extraction flags make sense, and update them if needed. */
1523 check_extract_flags(const WIMStruct *wim, int *extract_flags_p)
1525 int extract_flags = *extract_flags_p;
1527 /* Check for invalid flag combinations */
1529 if ((extract_flags &
1530 (WIMLIB_EXTRACT_FLAG_NO_ACLS |
1531 WIMLIB_EXTRACT_FLAG_STRICT_ACLS)) == (WIMLIB_EXTRACT_FLAG_NO_ACLS |
1532 WIMLIB_EXTRACT_FLAG_STRICT_ACLS))
1533 return WIMLIB_ERR_INVALID_PARAM;
1535 if ((extract_flags &
1536 (WIMLIB_EXTRACT_FLAG_RPFIX |
1537 WIMLIB_EXTRACT_FLAG_NORPFIX)) == (WIMLIB_EXTRACT_FLAG_RPFIX |
1538 WIMLIB_EXTRACT_FLAG_NORPFIX))
1539 return WIMLIB_ERR_INVALID_PARAM;
1541 #ifndef WITH_NTFS_3G
1542 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1543 ERROR("wimlib was compiled without support for NTFS-3G, so\n"
1544 " it cannot apply a WIM image directly to an NTFS volume.");
1545 return WIMLIB_ERR_UNSUPPORTED;
1549 if (extract_flags & WIMLIB_EXTRACT_FLAG_WIMBOOT) {
1552 return WIMLIB_ERR_NO_FILENAME;
1554 ERROR("WIMBoot extraction is only supported on Windows!");
1555 return WIMLIB_ERR_UNSUPPORTED;
1559 if (extract_flags & (WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS4K |
1560 WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS8K |
1561 WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS16K |
1562 WIMLIB_EXTRACT_FLAG_COMPACT_LZX))
1566 count += ((extract_flags & WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS4K) != 0);
1567 count += ((extract_flags & WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS8K) != 0);
1568 count += ((extract_flags & WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS16K) != 0);
1569 count += ((extract_flags & WIMLIB_EXTRACT_FLAG_COMPACT_LZX) != 0);
1571 ERROR("Only one compression format can be specified "
1572 "for compact-mode extraction!");
1573 return WIMLIB_ERR_INVALID_PARAM;
1575 if (extract_flags & WIMLIB_EXTRACT_FLAG_WIMBOOT) {
1576 ERROR("Compact-mode extraction and WIMBoot-mode "
1577 "extraction are mutually exclusive!");
1578 return WIMLIB_ERR_INVALID_PARAM;
1581 ERROR("Compact-mode extraction (System Compression) "
1582 "is only supported on Windows!");
1583 return WIMLIB_ERR_UNSUPPORTED;
1588 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_RPFIX |
1589 WIMLIB_EXTRACT_FLAG_NORPFIX |
1590 WIMLIB_EXTRACT_FLAG_IMAGEMODE)) ==
1591 WIMLIB_EXTRACT_FLAG_IMAGEMODE)
1593 /* For full-image extraction, do reparse point fixups by default
1594 * if the WIM header says they are enabled. */
1595 if (wim->hdr.flags & WIM_HDR_FLAG_RP_FIX)
1596 extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
1599 *extract_flags_p = extract_flags;
1603 struct append_dentry_ctx {
1604 struct wim_dentry **dentries;
1605 size_t num_dentries;
1606 size_t num_alloc_dentries;
1610 append_dentry_cb(struct wim_dentry *dentry, void *_ctx)
1612 struct append_dentry_ctx *ctx = _ctx;
1614 if (ctx->num_dentries == ctx->num_alloc_dentries) {
1615 struct wim_dentry **new_dentries;
1618 new_length = max(ctx->num_alloc_dentries + 8,
1619 ctx->num_alloc_dentries * 3 / 2);
1620 new_dentries = REALLOC(ctx->dentries,
1621 new_length * sizeof(ctx->dentries[0]));
1622 if (new_dentries == NULL)
1623 return WIMLIB_ERR_NOMEM;
1624 ctx->dentries = new_dentries;
1625 ctx->num_alloc_dentries = new_length;
1627 ctx->dentries[ctx->num_dentries++] = dentry;
1631 /* Append dentries matched by a path which can contain wildcard characters. */
1633 append_matched_dentries(WIMStruct *wim, const tchar *orig_pattern,
1634 int extract_flags, struct append_dentry_ctx *ctx)
1636 const size_t count_before = ctx->num_dentries;
1640 pattern = canonicalize_wim_path(orig_pattern);
1642 return WIMLIB_ERR_NOMEM;
1643 ret = expand_path_pattern(wim_get_current_root_dentry(wim), pattern,
1644 append_dentry_cb, ctx);
1646 if (ret || ctx->num_dentries > count_before)
1648 if (extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_GLOB) {
1649 ERROR("No matches for path pattern \"%"TS"\"", orig_pattern);
1650 return WIMLIB_ERR_PATH_DOES_NOT_EXIST;
1652 WARNING("No matches for path pattern \"%"TS"\"", orig_pattern);
1657 do_wimlib_extract_paths(WIMStruct *wim, int image, const tchar *target,
1658 const tchar * const *paths, size_t num_paths,
1662 struct wim_dentry **trees;
1665 if (wim == NULL || target == NULL || target[0] == T('\0') ||
1666 (num_paths != 0 && paths == NULL))
1667 return WIMLIB_ERR_INVALID_PARAM;
1669 ret = check_extract_flags(wim, &extract_flags);
1673 ret = select_wim_image(wim, image);
1677 ret = wim_checksum_unhashed_blobs(wim);
1681 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_NTFS |
1682 WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE)) ==
1683 (WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE))
1685 ret = mkdir_if_needed(target);
1690 if (extract_flags & WIMLIB_EXTRACT_FLAG_GLOB_PATHS) {
1692 struct append_dentry_ctx append_dentry_ctx = {
1695 .num_alloc_dentries = 0,
1698 for (size_t i = 0; i < num_paths; i++) {
1699 ret = append_matched_dentries(wim, paths[i],
1701 &append_dentry_ctx);
1703 trees = append_dentry_ctx.dentries;
1704 goto out_free_trees;
1707 trees = append_dentry_ctx.dentries;
1708 num_trees = append_dentry_ctx.num_dentries;
1710 trees = MALLOC(num_paths * sizeof(trees[0]));
1712 return WIMLIB_ERR_NOMEM;
1714 for (size_t i = 0; i < num_paths; i++) {
1716 tchar *path = canonicalize_wim_path(paths[i]);
1718 ret = WIMLIB_ERR_NOMEM;
1719 goto out_free_trees;
1722 trees[i] = get_dentry(wim, path,
1723 WIMLIB_CASE_PLATFORM_DEFAULT);
1725 if (trees[i] == NULL) {
1726 ERROR("Path \"%"TS"\" does not exist "
1728 paths[i], wim->current_image);
1729 ret = WIMLIB_ERR_PATH_DOES_NOT_EXIST;
1730 goto out_free_trees;
1733 num_trees = num_paths;
1736 if (num_trees == 0) {
1738 goto out_free_trees;
1741 ret = extract_trees(wim, trees, num_trees, target, extract_flags);
1748 extract_single_image(WIMStruct *wim, int image,
1749 const tchar *target, int extract_flags)
1751 const tchar *path = WIMLIB_WIM_ROOT_PATH;
1752 extract_flags |= WIMLIB_EXTRACT_FLAG_IMAGEMODE;
1753 return do_wimlib_extract_paths(wim, image, target, &path, 1, extract_flags);
1756 static const tchar * const filename_forbidden_chars =
1763 /* This function checks if it is okay to use a WIM image's name as a directory
1766 image_name_ok_as_dir(const tchar *image_name)
1768 return image_name && *image_name &&
1769 !tstrpbrk(image_name, filename_forbidden_chars) &&
1770 tstrcmp(image_name, T(".")) &&
1771 tstrcmp(image_name, T("..")) &&
1772 tstrlen(image_name) <= 128;
1775 /* Extracts all images from the WIM to the directory @target, with the images
1776 * placed in subdirectories named by their image names. */
1778 extract_all_images(WIMStruct *wim, const tchar *target, int extract_flags)
1780 size_t output_path_len = tstrlen(target);
1781 tchar buf[output_path_len + 1 + 128 + 1];
1784 const tchar *image_name;
1786 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1787 ERROR("Cannot extract multiple images in NTFS extraction mode.");
1788 return WIMLIB_ERR_INVALID_PARAM;
1791 ret = mkdir_if_needed(target);
1794 tmemcpy(buf, target, output_path_len);
1795 buf[output_path_len] = OS_PREFERRED_PATH_SEPARATOR;
1796 for (image = 1; image <= wim->hdr.image_count; image++) {
1797 image_name = wimlib_get_image_name(wim, image);
1798 if (image_name_ok_as_dir(image_name)) {
1799 tstrcpy(buf + output_path_len + 1, image_name);
1801 /* Image name is empty or contains forbidden characters.
1802 * Use image number instead. */
1803 tsprintf(buf + output_path_len + 1, T("%d"), image);
1805 ret = extract_single_image(wim, image, buf, extract_flags);
1813 do_wimlib_extract_image(WIMStruct *wim, int image, const tchar *target,
1816 if (extract_flags & (WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE |
1817 WIMLIB_EXTRACT_FLAG_TO_STDOUT |
1818 WIMLIB_EXTRACT_FLAG_GLOB_PATHS))
1819 return WIMLIB_ERR_INVALID_PARAM;
1821 if (image == WIMLIB_ALL_IMAGES)
1822 return extract_all_images(wim, target, extract_flags);
1824 return extract_single_image(wim, image, target, extract_flags);
1828 /****************************************************************************
1830 ****************************************************************************/
1833 wimlib_extract_paths(WIMStruct *wim, int image, const tchar *target,
1834 const tchar * const *paths, size_t num_paths,
1837 if (extract_flags & ~WIMLIB_EXTRACT_MASK_PUBLIC)
1838 return WIMLIB_ERR_INVALID_PARAM;
1840 return do_wimlib_extract_paths(wim, image, target, paths, num_paths,
1845 wimlib_extract_pathlist(WIMStruct *wim, int image, const tchar *target,
1846 const tchar *path_list_file, int extract_flags)
1853 ret = read_path_list_file(path_list_file, &paths, &num_paths, &mem);
1855 ERROR("Failed to read path list file \"%"TS"\"",
1860 ret = wimlib_extract_paths(wim, image, target,
1861 (const tchar * const *)paths, num_paths,
1869 wimlib_extract_image_from_pipe_with_progress(int pipe_fd,
1870 const tchar *image_num_or_name,
1871 const tchar *target,
1873 wimlib_progress_func_t progfunc,
1878 struct filedes *in_fd;
1882 if (extract_flags & ~WIMLIB_EXTRACT_MASK_PUBLIC)
1883 return WIMLIB_ERR_INVALID_PARAM;
1885 /* Read the WIM header from the pipe and get a WIMStruct to represent
1886 * the pipable WIM. Caveats: Unlike getting a WIMStruct with
1887 * wimlib_open_wim(), getting a WIMStruct in this way will result in an
1888 * empty blob table, no XML data read, and no filename set. */
1889 ret = open_wim_as_WIMStruct(&pipe_fd, WIMLIB_OPEN_FLAG_FROM_PIPE, &pwm,
1894 /* Sanity check to make sure this is a pipable WIM. */
1895 if (pwm->hdr.magic != PWM_MAGIC) {
1896 ERROR("The WIM being read from file descriptor %d "
1897 "is not pipable!", pipe_fd);
1898 ret = WIMLIB_ERR_NOT_PIPABLE;
1899 goto out_wimlib_free;
1902 /* Sanity check to make sure the first part of a pipable split WIM is
1903 * sent over the pipe first. */
1904 if (pwm->hdr.part_number != 1) {
1905 ERROR("The first part of the split WIM must be "
1906 "sent over the pipe first.");
1907 ret = WIMLIB_ERR_INVALID_PIPABLE_WIM;
1908 goto out_wimlib_free;
1911 in_fd = &pwm->in_fd;
1912 wimlib_assert(in_fd->offset == WIM_HEADER_DISK_SIZE);
1914 /* As mentioned, the WIMStruct we created from the pipe does not have
1915 * XML data yet. Fix this by reading the extra copy of the XML data
1916 * that directly follows the header in pipable WIMs. (Note: see
1917 * write_pipable_wim() for more details about the format of pipable
1920 u8 hash[SHA1_HASH_SIZE];
1922 ret = read_pwm_blob_header(pwm, hash,
1923 &pwm->hdr.xml_data_reshdr, NULL);
1925 goto out_wimlib_free;
1927 if (!(pwm->hdr.xml_data_reshdr.flags & WIM_RESHDR_FLAG_METADATA)) {
1928 ERROR("Expected XML data, but found non-metadata resource.");
1929 ret = WIMLIB_ERR_INVALID_PIPABLE_WIM;
1930 goto out_wimlib_free;
1933 ret = read_wim_xml_data(pwm);
1935 goto out_wimlib_free;
1937 if (xml_get_image_count(pwm->xml_info) != pwm->hdr.image_count) {
1938 ERROR("Image count in XML data is not the same as in WIM header.");
1939 ret = WIMLIB_ERR_IMAGE_COUNT;
1940 goto out_wimlib_free;
1944 /* Get image index (this may use the XML data that was just read to
1945 * resolve an image name). */
1946 if (image_num_or_name) {
1947 image = wimlib_resolve_image(pwm, image_num_or_name);
1948 if (image == WIMLIB_NO_IMAGE) {
1949 ERROR("\"%"TS"\" is not a valid image in the pipable WIM!",
1951 ret = WIMLIB_ERR_INVALID_IMAGE;
1952 goto out_wimlib_free;
1953 } else if (image == WIMLIB_ALL_IMAGES) {
1954 ERROR("Applying all images from a pipe is not supported!");
1955 ret = WIMLIB_ERR_INVALID_IMAGE;
1956 goto out_wimlib_free;
1959 if (pwm->hdr.image_count != 1) {
1960 ERROR("No image was specified, but the pipable WIM "
1961 "did not contain exactly 1 image");
1962 ret = WIMLIB_ERR_INVALID_IMAGE;
1963 goto out_wimlib_free;
1968 /* Load the needed metadata resource. */
1969 for (i = 1; i <= pwm->hdr.image_count; i++) {
1970 ret = handle_pwm_metadata_resource(pwm, i, i == image);
1972 goto out_wimlib_free;
1974 /* Extract the image. */
1975 extract_flags |= WIMLIB_EXTRACT_FLAG_FROM_PIPE;
1976 ret = do_wimlib_extract_image(pwm, image, target, extract_flags);
1977 /* Clean up and return. */
1985 wimlib_extract_image_from_pipe(int pipe_fd, const tchar *image_num_or_name,
1986 const tchar *target, int extract_flags)
1988 return wimlib_extract_image_from_pipe_with_progress(pipe_fd,
1997 wimlib_extract_image(WIMStruct *wim, int image, const tchar *target,
2000 if (extract_flags & ~WIMLIB_EXTRACT_MASK_PUBLIC)
2001 return WIMLIB_ERR_INVALID_PARAM;
2002 return do_wimlib_extract_image(wim, image, target, extract_flags);