4 * Support for extracting WIM images, or files or directories contained in a WIM
9 * Copyright (C) 2012, 2013, 2014, 2015 Eric Biggers
11 * This file is free software; you can redistribute it and/or modify it under
12 * the terms of the GNU Lesser General Public License as published by the Free
13 * Software Foundation; either version 3 of the License, or (at your option) any
16 * This file is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this file; if not, see http://www.gnu.org/licenses/.
26 * This file provides the API functions wimlib_extract_image(),
27 * wimlib_extract_image_from_pipe(), wimlib_extract_paths(), and
28 * wimlib_extract_pathlist(). Internally, all end up calling
29 * do_wimlib_extract_paths() and extract_trees().
31 * Although wimlib supports multiple extraction modes/backends (NTFS-3g, UNIX,
32 * Win32), this file does not itself have code to extract files or directories
33 * to any specific target; instead, it handles generic functionality and relies
34 * on lower-level callback functions declared in `struct apply_operations' to do
35 * the actual extraction.
47 #include "wimlib/apply.h"
48 #include "wimlib/assert.h"
49 #include "wimlib/blob_table.h"
50 #include "wimlib/dentry.h"
51 #include "wimlib/encoding.h"
52 #include "wimlib/endianness.h"
53 #include "wimlib/error.h"
54 #include "wimlib/metadata.h"
55 #include "wimlib/pathlist.h"
56 #include "wimlib/paths.h"
57 #include "wimlib/reparse.h"
58 #include "wimlib/resource.h"
59 #include "wimlib/security.h"
60 #include "wimlib/unix_data.h"
61 #include "wimlib/wildcard.h"
62 #include "wimlib/wim.h"
63 #include "wimlib/win32.h" /* for realpath() equivalent */
64 #include "wimlib/xml.h"
66 #define WIMLIB_EXTRACT_FLAG_FROM_PIPE 0x80000000
67 #define WIMLIB_EXTRACT_FLAG_IMAGEMODE 0x40000000
69 /* Keep in sync with wimlib.h */
70 #define WIMLIB_EXTRACT_MASK_PUBLIC \
71 (WIMLIB_EXTRACT_FLAG_NTFS | \
72 WIMLIB_EXTRACT_FLAG_UNIX_DATA | \
73 WIMLIB_EXTRACT_FLAG_NO_ACLS | \
74 WIMLIB_EXTRACT_FLAG_STRICT_ACLS | \
75 WIMLIB_EXTRACT_FLAG_RPFIX | \
76 WIMLIB_EXTRACT_FLAG_NORPFIX | \
77 WIMLIB_EXTRACT_FLAG_TO_STDOUT | \
78 WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES | \
79 WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS | \
80 WIMLIB_EXTRACT_FLAG_STRICT_TIMESTAMPS | \
81 WIMLIB_EXTRACT_FLAG_STRICT_SHORT_NAMES | \
82 WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS | \
83 WIMLIB_EXTRACT_FLAG_GLOB_PATHS | \
84 WIMLIB_EXTRACT_FLAG_STRICT_GLOB | \
85 WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES | \
86 WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE | \
87 WIMLIB_EXTRACT_FLAG_WIMBOOT)
89 /* Send WIMLIB_PROGRESS_MSG_EXTRACT_FILE_STRUCTURE or
90 * WIMLIB_PROGRESS_MSG_EXTRACT_METADATA. */
92 do_file_extract_progress(struct apply_ctx *ctx, enum wimlib_progress_msg msg)
94 ctx->count_until_file_progress = 500; /* Arbitrary value to limit calls */
95 return extract_progress(ctx, msg);
99 start_file_phase(struct apply_ctx *ctx, u64 end_file_count, enum wimlib_progress_msg msg)
101 ctx->progress.extract.current_file_count = 0;
102 ctx->progress.extract.end_file_count = end_file_count;
103 return do_file_extract_progress(ctx, msg);
107 start_file_structure_phase(struct apply_ctx *ctx, u64 end_file_count)
109 return start_file_phase(ctx, end_file_count, WIMLIB_PROGRESS_MSG_EXTRACT_FILE_STRUCTURE);
113 start_file_metadata_phase(struct apply_ctx *ctx, u64 end_file_count)
115 return start_file_phase(ctx, end_file_count, WIMLIB_PROGRESS_MSG_EXTRACT_METADATA);
119 end_file_phase(struct apply_ctx *ctx, enum wimlib_progress_msg msg)
121 ctx->progress.extract.current_file_count = ctx->progress.extract.end_file_count;
122 return do_file_extract_progress(ctx, msg);
126 end_file_structure_phase(struct apply_ctx *ctx)
128 return end_file_phase(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_FILE_STRUCTURE);
132 end_file_metadata_phase(struct apply_ctx *ctx)
134 return end_file_phase(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_METADATA);
137 #define PWM_FOUND_WIM_HDR (-1)
139 /* Read the header for a blob in a pipable WIM. If @pwm_hdr_ret is not NULL,
140 * also look for a pipable WIM header and return PWM_FOUND_WIM_HDR if found. */
142 read_pwm_blob_header(WIMStruct *pwm, u8 hash_ret[SHA1_HASH_SIZE],
143 struct wim_reshdr *reshdr_ret,
144 struct wim_header_disk *pwm_hdr_ret)
147 struct pwm_blob_hdr blob_hdr;
150 ret = full_read(&pwm->in_fd, &blob_hdr, sizeof(blob_hdr));
154 magic = le64_to_cpu(blob_hdr.magic);
156 if (magic == PWM_MAGIC && pwm_hdr_ret != NULL) {
157 memcpy(pwm_hdr_ret, &blob_hdr, sizeof(blob_hdr));
158 ret = full_read(&pwm->in_fd,
159 (u8 *)pwm_hdr_ret + sizeof(blob_hdr),
160 sizeof(*pwm_hdr_ret) - sizeof(blob_hdr));
163 return PWM_FOUND_WIM_HDR;
166 if (unlikely(magic != PWM_BLOB_MAGIC)) {
167 ERROR("Data read on pipe is invalid (expected blob header)");
168 return WIMLIB_ERR_INVALID_PIPABLE_WIM;
171 copy_hash(hash_ret, blob_hdr.hash);
173 reshdr_ret->size_in_wim = 0; /* Not available */
174 reshdr_ret->flags = le32_to_cpu(blob_hdr.flags);
175 reshdr_ret->offset_in_wim = pwm->in_fd.offset;
176 reshdr_ret->uncompressed_size = le64_to_cpu(blob_hdr.uncompressed_size);
178 if (unlikely(reshdr_ret->uncompressed_size == 0)) {
179 ERROR("Data read on pipe is invalid (resource is of 0 size)");
180 return WIMLIB_ERR_INVALID_PIPABLE_WIM;
186 if (ret == WIMLIB_ERR_UNEXPECTED_END_OF_FILE)
187 ERROR("The pipe ended before all needed data was sent!");
189 ERROR_WITH_ERRNO("Error reading pipable WIM from pipe");
194 read_blobs_from_pipe(struct apply_ctx *ctx, const struct read_blob_callbacks *cbs)
197 u8 hash[SHA1_HASH_SIZE];
198 struct wim_reshdr reshdr;
199 struct wim_header_disk pwm_hdr;
200 struct wim_resource_descriptor rdesc;
201 struct blob_descriptor *blob;
203 copy_guid(ctx->progress.extract.guid, ctx->wim->hdr.guid);
204 ctx->progress.extract.part_number = ctx->wim->hdr.part_number;
205 ctx->progress.extract.total_parts = ctx->wim->hdr.total_parts;
206 ret = extract_progress(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN);
210 while (ctx->num_blobs_remaining) {
212 ret = read_pwm_blob_header(ctx->wim, hash, &reshdr, &pwm_hdr);
214 if (ret == PWM_FOUND_WIM_HDR) {
215 u16 part_number = le16_to_cpu(pwm_hdr.part_number);
216 u16 total_parts = le16_to_cpu(pwm_hdr.total_parts);
218 if (part_number == ctx->progress.extract.part_number &&
219 total_parts == ctx->progress.extract.total_parts &&
220 guids_equal(pwm_hdr.guid, ctx->progress.extract.guid))
223 copy_guid(ctx->progress.extract.guid, pwm_hdr.guid);
224 ctx->progress.extract.part_number = part_number;
225 ctx->progress.extract.total_parts = total_parts;
226 ret = extract_progress(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN);
236 if (!(reshdr.flags & WIM_RESHDR_FLAG_METADATA)
237 && (blob = lookup_blob(ctx->wim->blob_table, hash))
238 && (blob->out_refcnt))
240 wim_reshdr_to_desc_and_blob(&reshdr, ctx->wim, &rdesc, blob);
241 ret = read_blob_with_sha1(blob, cbs);
242 blob_unset_is_located_in_wim_resource(blob);
245 ctx->num_blobs_remaining--;
247 wim_reshdr_to_desc(&reshdr, ctx->wim, &rdesc);
248 ret = skip_wim_resource(&rdesc);
257 /* Creates a temporary file opened for writing. The open file descriptor is
258 * returned in @fd_ret and its name is returned in @name_ret (dynamically
261 create_temporary_file(struct filedes *fd_ret, tchar **name_ret)
268 name = ttempnam(NULL, T("wimlib"));
270 ERROR_WITH_ERRNO("Failed to create temporary filename");
271 return WIMLIB_ERR_NOMEM;
274 open_flags = O_WRONLY | O_CREAT | O_EXCL | O_BINARY;
276 open_flags |= _O_SHORT_LIVED;
278 raw_fd = topen(name, open_flags, 0600);
281 if (errno == EEXIST) {
285 ERROR_WITH_ERRNO("Failed to create temporary file "
288 return WIMLIB_ERR_OPEN;
291 filedes_init(fd_ret, raw_fd);
297 begin_extract_blob_wrapper(struct blob_descriptor *blob, void *_ctx)
299 struct apply_ctx *ctx = _ctx;
301 ctx->cur_blob = blob;
302 ctx->cur_blob_offset = 0;
304 if (unlikely(blob->out_refcnt > MAX_OPEN_FILES))
305 return create_temporary_file(&ctx->tmpfile_fd, &ctx->tmpfile_name);
307 return call_begin_blob(blob, ctx->saved_cbs);
311 extract_chunk_wrapper(const void *chunk, size_t size, void *_ctx)
313 struct apply_ctx *ctx = _ctx;
314 union wimlib_progress_info *progress = &ctx->progress;
317 ctx->cur_blob_offset += size;
319 if (likely(ctx->supported_features.hard_links)) {
320 progress->extract.completed_bytes +=
321 (u64)size * ctx->cur_blob->out_refcnt;
322 if (ctx->cur_blob_offset == ctx->cur_blob->size)
323 progress->extract.completed_streams += ctx->cur_blob->out_refcnt;
325 const struct blob_extraction_target *targets =
326 blob_extraction_targets(ctx->cur_blob);
327 for (u32 i = 0; i < ctx->cur_blob->out_refcnt; i++) {
328 const struct wim_inode *inode = targets[i].inode;
329 const struct wim_dentry *dentry;
331 inode_for_each_extraction_alias(dentry, inode) {
332 progress->extract.completed_bytes += size;
333 if (ctx->cur_blob_offset == ctx->cur_blob->size)
334 progress->extract.completed_streams++;
338 if (progress->extract.completed_bytes >= ctx->next_progress) {
340 ret = extract_progress(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS);
344 if (progress->extract.completed_bytes >=
345 progress->extract.total_bytes)
347 ctx->next_progress = UINT64_MAX;
349 /* Send new message as soon as another 1/128 of the
350 * total has been extracted. (Arbitrary number.) */
352 progress->extract.completed_bytes +
353 progress->extract.total_bytes / 128;
355 /* ... Unless that would be more than 5000000 bytes, in
356 * which case send the next after the next 5000000
357 * bytes. (Another arbitrary number.) */
358 if (progress->extract.completed_bytes + 5000000 <
361 progress->extract.completed_bytes + 5000000;
363 /* ... But always send a message as soon as we're
364 * completely done. */
365 if (progress->extract.total_bytes < ctx->next_progress)
366 ctx->next_progress = progress->extract.total_bytes;
370 if (unlikely(filedes_valid(&ctx->tmpfile_fd))) {
371 /* Just extracting to temporary file for now. */
372 ret = full_write(&ctx->tmpfile_fd, chunk, size);
374 ERROR_WITH_ERRNO("Error writing data to "
375 "temporary file \"%"TS"\"",
381 return call_consume_chunk(chunk, size, ctx->saved_cbs);
384 /* Copy the blob's data from the temporary file to each of its targets.
386 * This is executed only in the very uncommon case that a blob is being
387 * extracted to more than MAX_OPEN_FILES targets! */
389 extract_from_tmpfile(const tchar *tmpfile_name,
390 const struct blob_descriptor *orig_blob,
391 const struct read_blob_callbacks *cbs)
393 struct blob_descriptor tmpfile_blob;
394 const struct blob_extraction_target *targets = blob_extraction_targets(orig_blob);
397 memcpy(&tmpfile_blob, orig_blob, sizeof(struct blob_descriptor));
398 tmpfile_blob.blob_location = BLOB_IN_FILE_ON_DISK;
399 tmpfile_blob.file_on_disk = (tchar *)tmpfile_name;
400 tmpfile_blob.out_refcnt = 1;
402 for (u32 i = 0; i < orig_blob->out_refcnt; i++) {
403 tmpfile_blob.inline_blob_extraction_targets[0] = targets[i];
404 ret = read_blob_with_cbs(&tmpfile_blob, cbs);
412 end_extract_blob_wrapper(struct blob_descriptor *blob, int status, void *_ctx)
414 struct apply_ctx *ctx = _ctx;
416 if (unlikely(filedes_valid(&ctx->tmpfile_fd))) {
417 filedes_close(&ctx->tmpfile_fd);
419 status = extract_from_tmpfile(ctx->tmpfile_name, blob,
421 filedes_invalidate(&ctx->tmpfile_fd);
422 tunlink(ctx->tmpfile_name);
423 FREE(ctx->tmpfile_name);
427 return call_end_blob(blob, status, ctx->saved_cbs);
431 * Read the list of blobs to extract and feed their data into the specified
432 * callback functions.
434 * This handles checksumming each blob.
436 * This also handles sending WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS.
438 * This also works if the WIM is being read from a pipe.
440 * This also will split up blobs that will need to be extracted to more than
441 * MAX_OPEN_FILES locations, as measured by the 'out_refcnt' of each blob.
442 * Therefore, the apply_operations implementation need not worry about running
443 * out of file descriptors, unless it might open more than one file descriptor
444 * per 'blob_extraction_target' (e.g. Win32 currently might because the
445 * destination file system might not support hard links).
448 extract_blob_list(struct apply_ctx *ctx, const struct read_blob_callbacks *cbs)
450 struct read_blob_callbacks wrapper_cbs = {
451 .begin_blob = begin_extract_blob_wrapper,
452 .consume_chunk = extract_chunk_wrapper,
453 .end_blob = end_extract_blob_wrapper,
456 ctx->saved_cbs = cbs;
457 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_FROM_PIPE) {
458 return read_blobs_from_pipe(ctx, &wrapper_cbs);
460 return read_blob_list(&ctx->blob_list,
461 offsetof(struct blob_descriptor,
463 &wrapper_cbs, VERIFY_BLOB_HASHES);
467 /* Extract a WIM dentry to standard output.
469 * This obviously doesn't make sense in all cases. We return an error if the
470 * dentry does not correspond to a regular file. Otherwise we extract the
471 * unnamed data stream only. */
473 extract_dentry_to_stdout(struct wim_dentry *dentry,
474 const struct blob_table *blob_table)
476 struct wim_inode *inode = dentry->d_inode;
477 struct blob_descriptor *blob;
478 struct filedes _stdout;
480 if (inode->i_attributes & (FILE_ATTRIBUTE_REPARSE_POINT |
481 FILE_ATTRIBUTE_DIRECTORY |
482 FILE_ATTRIBUTE_ENCRYPTED))
484 ERROR("\"%"TS"\" is not a regular file and therefore cannot be "
485 "extracted to standard output", dentry_full_path(dentry));
486 return WIMLIB_ERR_NOT_A_REGULAR_FILE;
489 blob = inode_get_blob_for_unnamed_data_stream(inode, blob_table);
491 const u8 *hash = inode_get_hash_of_unnamed_data_stream(inode);
492 if (!is_zero_hash(hash))
493 return blob_not_found_error(inode, hash);
497 filedes_init(&_stdout, STDOUT_FILENO);
498 return extract_blob_to_fd(blob, &_stdout);
502 extract_dentries_to_stdout(struct wim_dentry **dentries, size_t num_dentries,
503 const struct blob_table *blob_table)
505 for (size_t i = 0; i < num_dentries; i++) {
506 int ret = extract_dentry_to_stdout(dentries[i], blob_table);
513 /**********************************************************************/
516 * Removes duplicate dentries from the array.
518 * Returns the new number of dentries, packed at the front of the array.
521 remove_duplicate_trees(struct wim_dentry **trees, size_t num_trees)
524 for (i = 0; i < num_trees; i++) {
525 if (!trees[i]->tmp_flag) {
526 /* Found distinct dentry. */
527 trees[i]->tmp_flag = 1;
528 trees[j++] = trees[i];
531 for (i = 0; i < j; i++)
532 trees[i]->tmp_flag = 0;
537 * Remove dentries that are descendants of other dentries in the array.
539 * Returns the new number of dentries, packed at the front of the array.
542 remove_contained_trees(struct wim_dentry **trees, size_t num_trees)
545 for (i = 0; i < num_trees; i++)
546 trees[i]->tmp_flag = 1;
547 for (i = 0; i < num_trees; i++) {
548 struct wim_dentry *d = trees[i];
549 while (!dentry_is_root(d)) {
554 trees[j++] = trees[i];
558 trees[i]->tmp_flag = 0;
561 for (i = 0; i < j; i++)
562 trees[i]->tmp_flag = 0;
567 dentry_append_to_list(struct wim_dentry *dentry, void *_dentry_list)
569 struct list_head *dentry_list = _dentry_list;
570 list_add_tail(&dentry->d_extraction_list_node, dentry_list);
575 dentry_reset_extraction_list_node(struct wim_dentry *dentry)
577 dentry->d_extraction_list_node = (struct list_head){NULL, NULL};
581 dentry_delete_from_list(struct wim_dentry *dentry, void *_ignore)
583 list_del(&dentry->d_extraction_list_node);
584 dentry_reset_extraction_list_node(dentry);
589 * Build the preliminary list of dentries to be extracted.
591 * The list maintains the invariant that if d1 and d2 are in the list and d1 is
592 * an ancestor of d2, then d1 appears before d2 in the list.
595 build_dentry_list(struct list_head *dentry_list, struct wim_dentry **trees,
596 size_t num_trees, bool add_ancestors)
598 INIT_LIST_HEAD(dentry_list);
600 /* Add the trees recursively. */
601 for (size_t i = 0; i < num_trees; i++)
602 for_dentry_in_tree(trees[i], dentry_append_to_list, dentry_list);
604 /* If requested, add ancestors of the trees. */
606 for (size_t i = 0; i < num_trees; i++) {
607 struct wim_dentry *dentry = trees[i];
608 struct wim_dentry *ancestor;
609 struct list_head *place_after;
611 if (dentry_is_root(dentry))
614 place_after = dentry_list;
617 ancestor = ancestor->d_parent;
618 if (will_extract_dentry(ancestor)) {
619 place_after = &ancestor->d_extraction_list_node;
622 } while (!dentry_is_root(ancestor));
626 ancestor = ancestor->d_parent;
627 if (will_extract_dentry(ancestor))
629 list_add(&ancestor->d_extraction_list_node, place_after);
630 } while (!dentry_is_root(ancestor));
636 destroy_dentry_list(struct list_head *dentry_list)
638 struct wim_dentry *dentry, *tmp;
639 struct wim_inode *inode;
641 list_for_each_entry_safe(dentry, tmp, dentry_list, d_extraction_list_node) {
642 inode = dentry->d_inode;
643 dentry_reset_extraction_list_node(dentry);
644 inode->i_visited = 0;
645 inode->i_can_externally_back = 0;
646 if ((void *)dentry->d_extraction_name != (void *)dentry->file_name)
647 FREE(dentry->d_extraction_name);
648 dentry->d_extraction_name = NULL;
649 dentry->d_extraction_name_nchars = 0;
654 destroy_blob_list(struct list_head *blob_list)
656 struct blob_descriptor *blob;
658 list_for_each_entry(blob, blob_list, extraction_list)
659 if (blob->out_refcnt > ARRAY_LEN(blob->inline_blob_extraction_targets))
660 FREE(blob->blob_extraction_targets);
664 static const utf16lechar replacement_char = cpu_to_le16(0xfffd);
666 static const utf16lechar replacement_char = cpu_to_le16('?');
670 file_name_valid(utf16lechar *name, size_t num_chars, bool fix)
676 for (i = 0; i < num_chars; i++) {
679 case cpu_to_le16('\\'):
680 case cpu_to_le16(':'):
681 case cpu_to_le16('*'):
682 case cpu_to_le16('?'):
683 case cpu_to_le16('"'):
684 case cpu_to_le16('<'):
685 case cpu_to_le16('>'):
686 case cpu_to_le16('|'):
688 case cpu_to_le16('/'):
689 case cpu_to_le16('\0'):
691 name[i] = replacement_char;
698 if (name[num_chars - 1] == cpu_to_le16(' ') ||
699 name[num_chars - 1] == cpu_to_le16('.'))
702 name[num_chars - 1] = replacement_char;
711 dentry_calculate_extraction_name(struct wim_dentry *dentry,
712 struct apply_ctx *ctx)
716 if (dentry_is_root(dentry))
720 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
721 dentry->d_extraction_name = dentry->file_name;
722 dentry->d_extraction_name_nchars = dentry->file_name_nbytes /
728 if (!ctx->supported_features.case_sensitive_filenames) {
729 struct wim_dentry *other;
730 list_for_each_entry(other, &dentry->d_ci_conflict_list,
733 if (will_extract_dentry(other)) {
734 if (ctx->extract_flags &
735 WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS) {
736 WARNING("\"%"TS"\" has the same "
737 "case-insensitive name as "
738 "\"%"TS"\"; extracting "
739 "dummy name instead",
740 dentry_full_path(dentry),
741 dentry_full_path(other));
744 WARNING("Not extracting \"%"TS"\": "
745 "has same case-insensitive "
747 dentry_full_path(dentry),
748 dentry_full_path(other));
755 if (file_name_valid(dentry->file_name, dentry->file_name_nbytes / 2, false)) {
756 ret = utf16le_get_tstr(dentry->file_name,
757 dentry->file_name_nbytes,
758 (const tchar **)&dentry->d_extraction_name,
759 &dentry->d_extraction_name_nchars);
760 dentry->d_extraction_name_nchars /= sizeof(tchar);
763 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES)
765 WARNING("\"%"TS"\" has an invalid filename "
766 "that is not supported on this platform; "
767 "extracting dummy name instead",
768 dentry_full_path(dentry));
771 WARNING("Not extracting \"%"TS"\": has an invalid filename "
772 "that is not supported on this platform",
773 dentry_full_path(dentry));
780 utf16lechar utf16_name_copy[dentry->file_name_nbytes / 2];
782 memcpy(utf16_name_copy, dentry->file_name, dentry->file_name_nbytes);
783 file_name_valid(utf16_name_copy, dentry->file_name_nbytes / 2, true);
785 const tchar *tchar_name;
788 ret = utf16le_get_tstr(utf16_name_copy,
789 dentry->file_name_nbytes,
790 &tchar_name, &tchar_nchars);
794 tchar_nchars /= sizeof(tchar);
796 size_t fixed_name_num_chars = tchar_nchars;
797 tchar fixed_name[tchar_nchars + 50];
799 tmemcpy(fixed_name, tchar_name, tchar_nchars);
800 fixed_name_num_chars += tsprintf(fixed_name + tchar_nchars,
801 T(" (invalid filename #%lu)"),
802 ++ctx->invalid_sequence);
804 utf16le_put_tstr(tchar_name);
806 dentry->d_extraction_name = TSTRDUP(fixed_name);
807 if (!dentry->d_extraction_name)
808 return WIMLIB_ERR_NOMEM;
809 dentry->d_extraction_name_nchars = fixed_name_num_chars;
814 for_dentry_in_tree(dentry, dentry_delete_from_list, NULL);
819 * Calculate the actual filename component at which each WIM dentry will be
820 * extracted, with special handling for dentries that are unsupported by the
821 * extraction backend or have invalid names.
823 * ctx->supported_features must be filled in.
825 * Possible error codes: WIMLIB_ERR_NOMEM, WIMLIB_ERR_INVALID_UTF16_STRING
828 dentry_list_calculate_extraction_names(struct list_head *dentry_list,
829 struct apply_ctx *ctx)
831 struct list_head *prev, *cur;
833 /* Can't use list_for_each_entry() because a call to
834 * dentry_calculate_extraction_name() may delete the current dentry and
835 * its children from the list. */
839 struct wim_dentry *dentry;
843 if (cur == dentry_list)
846 dentry = list_entry(cur, struct wim_dentry, d_extraction_list_node);
848 ret = dentry_calculate_extraction_name(dentry, ctx);
852 if (prev->next == cur)
855 ; /* Current dentry and its children (which follow in
856 the list) were deleted. prev stays the same. */
862 dentry_resolve_streams(struct wim_dentry *dentry, int extract_flags,
863 struct blob_table *blob_table)
865 struct wim_inode *inode = dentry->d_inode;
866 struct blob_descriptor *blob;
870 /* Special case: when extracting from a pipe, the WIM blob table is
871 * initially empty, so "resolving" an inode's streams is initially not
872 * possible. However, we still need to keep track of which blobs,
873 * identified by SHA-1 message digests, need to be extracted, so we
874 * "resolve" the inode's streams anyway by allocating a 'struct
875 * blob_descriptor' for each one. */
876 if (extract_flags & WIMLIB_EXTRACT_FLAG_FROM_PIPE)
878 ret = inode_resolve_streams(inode, blob_table, force);
881 for (unsigned i = 0; i < inode->i_num_streams; i++) {
882 blob = stream_blob_resolved(&inode->i_streams[i]);
884 blob->out_refcnt = 0;
890 * For each dentry to be extracted, resolve all streams in the corresponding
891 * inode and set 'out_refcnt' in all referenced blob_descriptors to 0.
893 * Possible error codes: WIMLIB_ERR_RESOURCE_NOT_FOUND, WIMLIB_ERR_NOMEM.
896 dentry_list_resolve_streams(struct list_head *dentry_list,
897 struct apply_ctx *ctx)
899 struct wim_dentry *dentry;
902 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
903 ret = dentry_resolve_streams(dentry,
905 ctx->wim->blob_table);
913 ref_stream(struct wim_inode_stream *strm, struct wim_dentry *dentry,
914 struct apply_ctx *ctx)
916 struct wim_inode *inode = dentry->d_inode;
917 struct blob_descriptor *blob = stream_blob_resolved(strm);
918 struct blob_extraction_target *targets;
923 /* Tally the size only for each actual extraction of the stream (not
924 * additional hard links to the inode). */
925 if (inode->i_visited && ctx->supported_features.hard_links)
928 ctx->progress.extract.total_bytes += blob->size;
929 ctx->progress.extract.total_streams++;
931 if (inode->i_visited)
934 /* Add each blob to 'ctx->blob_list' only one time, regardless of how
935 * many extraction targets it will have. */
936 if (blob->out_refcnt == 0) {
937 list_add_tail(&blob->extraction_list, &ctx->blob_list);
938 ctx->num_blobs_remaining++;
941 /* Set this stream as an extraction target of 'blob'. */
943 if (blob->out_refcnt < ARRAY_LEN(blob->inline_blob_extraction_targets)) {
944 targets = blob->inline_blob_extraction_targets;
946 struct blob_extraction_target *prev_targets;
947 size_t alloc_blob_extraction_targets;
949 if (blob->out_refcnt == ARRAY_LEN(blob->inline_blob_extraction_targets)) {
951 alloc_blob_extraction_targets = ARRAY_LEN(blob->inline_blob_extraction_targets);
953 prev_targets = blob->blob_extraction_targets;
954 alloc_blob_extraction_targets = blob->alloc_blob_extraction_targets;
957 if (blob->out_refcnt == alloc_blob_extraction_targets) {
958 alloc_blob_extraction_targets *= 2;
959 targets = REALLOC(prev_targets,
960 alloc_blob_extraction_targets *
963 return WIMLIB_ERR_NOMEM;
966 blob->inline_blob_extraction_targets,
967 sizeof(blob->inline_blob_extraction_targets));
969 blob->blob_extraction_targets = targets;
970 blob->alloc_blob_extraction_targets = alloc_blob_extraction_targets;
972 targets = blob->blob_extraction_targets;
974 targets[blob->out_refcnt].inode = inode;
975 targets[blob->out_refcnt].stream = strm;
981 ref_stream_if_needed(struct wim_dentry *dentry, struct wim_inode *inode,
982 struct wim_inode_stream *strm, struct apply_ctx *ctx)
984 bool need_stream = false;
985 switch (strm->stream_type) {
986 case STREAM_TYPE_DATA:
987 if (stream_is_named(strm)) {
988 /* Named data stream */
989 if (ctx->supported_features.named_data_streams)
991 } else if (!(inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
992 FILE_ATTRIBUTE_ENCRYPTED))
993 && !(inode_is_symlink(inode)
994 && !ctx->supported_features.reparse_points
995 && ctx->supported_features.symlink_reparse_points))
998 * Unnamed data stream. Skip if any of the following is true:
1000 * - file is a directory
1001 * - file is encrypted
1002 * - backend needs to create the file as UNIX symlink
1003 * - backend will extract the stream as externally backed
1005 if (ctx->apply_ops->will_externally_back) {
1006 int ret = (*ctx->apply_ops->will_externally_back)(dentry, ctx);
1007 if (ret > 0) /* Error? */
1009 if (ret < 0) /* Won't externally back? */
1016 case STREAM_TYPE_REPARSE_POINT:
1017 wimlib_assert(inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT);
1018 if (ctx->supported_features.reparse_points ||
1019 (inode_is_symlink(inode) &&
1020 ctx->supported_features.symlink_reparse_points))
1023 case STREAM_TYPE_EFSRPC_RAW_DATA:
1024 wimlib_assert(inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED);
1025 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY) {
1026 if (ctx->supported_features.encrypted_directories)
1029 if (ctx->supported_features.encrypted_files)
1035 return ref_stream(strm, dentry, ctx);
1040 dentry_ref_streams(struct wim_dentry *dentry, struct apply_ctx *ctx)
1042 struct wim_inode *inode = dentry->d_inode;
1043 for (unsigned i = 0; i < inode->i_num_streams; i++) {
1044 int ret = ref_stream_if_needed(dentry, inode,
1045 &inode->i_streams[i], ctx);
1049 inode->i_visited = 1;
1054 * Given a list of dentries to be extracted, build the list of blobs that need
1055 * to be extracted, and for each blob determine the streams to which that blob
1056 * will be extracted.
1058 * This also initializes the extract progress info with byte and blob
1061 * ctx->supported_features must be filled in.
1064 dentry_list_ref_streams(struct list_head *dentry_list, struct apply_ctx *ctx)
1066 struct wim_dentry *dentry;
1069 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
1070 ret = dentry_ref_streams(dentry, ctx);
1074 list_for_each_entry(dentry, dentry_list, d_extraction_list_node)
1075 dentry->d_inode->i_visited = 0;
1080 dentry_list_build_inode_alias_lists(struct list_head *dentry_list)
1082 struct wim_dentry *dentry;
1084 list_for_each_entry(dentry, dentry_list, d_extraction_list_node)
1085 dentry->d_inode->i_first_extraction_alias = NULL;
1087 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
1088 dentry->d_next_extraction_alias = dentry->d_inode->i_first_extraction_alias;
1089 dentry->d_inode->i_first_extraction_alias = dentry;
1094 inode_tally_features(const struct wim_inode *inode,
1095 struct wim_features *features)
1097 if (inode->i_attributes & FILE_ATTRIBUTE_ARCHIVE)
1098 features->archive_files++;
1099 if (inode->i_attributes & FILE_ATTRIBUTE_HIDDEN)
1100 features->hidden_files++;
1101 if (inode->i_attributes & FILE_ATTRIBUTE_SYSTEM)
1102 features->system_files++;
1103 if (inode->i_attributes & FILE_ATTRIBUTE_COMPRESSED)
1104 features->compressed_files++;
1105 if (inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED) {
1106 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)
1107 features->encrypted_directories++;
1109 features->encrypted_files++;
1111 if (inode->i_attributes & FILE_ATTRIBUTE_NOT_CONTENT_INDEXED)
1112 features->not_context_indexed_files++;
1113 if (inode->i_attributes & FILE_ATTRIBUTE_SPARSE_FILE)
1114 features->sparse_files++;
1115 if (inode_has_named_data_stream(inode))
1116 features->named_data_streams++;
1117 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1118 features->reparse_points++;
1119 if (inode_is_symlink(inode))
1120 features->symlink_reparse_points++;
1122 features->other_reparse_points++;
1124 if (inode_has_security_descriptor(inode))
1125 features->security_descriptors++;
1126 if (inode_has_unix_data(inode))
1127 features->unix_data++;
1130 /* Tally features necessary to extract a dentry and the corresponding inode. */
1132 dentry_tally_features(struct wim_dentry *dentry, struct wim_features *features)
1134 struct wim_inode *inode = dentry->d_inode;
1136 if (dentry_has_short_name(dentry))
1137 features->short_names++;
1139 if (inode->i_visited) {
1140 features->hard_links++;
1142 inode_tally_features(inode, features);
1143 inode->i_visited = 1;
1147 /* Tally the features necessary to extract the specified dentries. */
1149 dentry_list_get_features(struct list_head *dentry_list,
1150 struct wim_features *features)
1152 struct wim_dentry *dentry;
1154 list_for_each_entry(dentry, dentry_list, d_extraction_list_node)
1155 dentry_tally_features(dentry, features);
1157 list_for_each_entry(dentry, dentry_list, d_extraction_list_node)
1158 dentry->d_inode->i_visited = 0;
1162 do_feature_check(const struct wim_features *required_features,
1163 const struct wim_features *supported_features,
1166 /* Encrypted files. */
1167 if (required_features->encrypted_files &&
1168 !supported_features->encrypted_files)
1169 WARNING("Ignoring EFS-encrypted data of %lu files",
1170 required_features->encrypted_files);
1172 /* Named data streams. */
1173 if (required_features->named_data_streams &&
1174 !supported_features->named_data_streams)
1175 WARNING("Ignoring named data streams of %lu files",
1176 required_features->named_data_streams);
1178 /* File attributes. */
1179 if (!(extract_flags & WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES)) {
1180 /* Note: Don't bother the user about FILE_ATTRIBUTE_ARCHIVE.
1181 * We're an archive program, so theoretically we can do what we
1184 if (required_features->hidden_files &&
1185 !supported_features->hidden_files)
1186 WARNING("Ignoring FILE_ATTRIBUTE_HIDDEN of %lu files",
1187 required_features->hidden_files);
1189 if (required_features->system_files &&
1190 !supported_features->system_files)
1191 WARNING("Ignoring FILE_ATTRIBUTE_SYSTEM of %lu files",
1192 required_features->system_files);
1194 if (required_features->compressed_files &&
1195 !supported_features->compressed_files)
1196 WARNING("Ignoring FILE_ATTRIBUTE_COMPRESSED of %lu files",
1197 required_features->compressed_files);
1199 if (required_features->not_context_indexed_files &&
1200 !supported_features->not_context_indexed_files)
1201 WARNING("Ignoring FILE_ATTRIBUTE_NOT_CONTENT_INDEXED of %lu files",
1202 required_features->not_context_indexed_files);
1204 if (required_features->sparse_files &&
1205 !supported_features->sparse_files)
1206 WARNING("Ignoring FILE_ATTRIBUTE_SPARSE_FILE of %lu files",
1207 required_features->sparse_files);
1209 if (required_features->encrypted_directories &&
1210 !supported_features->encrypted_directories)
1211 WARNING("Ignoring FILE_ATTRIBUTE_ENCRYPTED of %lu directories",
1212 required_features->encrypted_directories);
1216 if (required_features->hard_links && !supported_features->hard_links)
1217 WARNING("Extracting %lu hard links as independent files",
1218 required_features->hard_links);
1220 /* Symbolic links and reparse points. */
1221 if ((extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS) &&
1222 required_features->symlink_reparse_points &&
1223 !supported_features->symlink_reparse_points &&
1224 !supported_features->reparse_points)
1226 ERROR("Extraction backend does not support symbolic links!");
1227 return WIMLIB_ERR_UNSUPPORTED;
1229 if (required_features->reparse_points &&
1230 !supported_features->reparse_points)
1232 if (supported_features->symlink_reparse_points) {
1233 if (required_features->other_reparse_points) {
1234 WARNING("Ignoring reparse data of %lu non-symlink/junction files",
1235 required_features->other_reparse_points);
1238 WARNING("Ignoring reparse data of %lu files",
1239 required_features->reparse_points);
1243 /* Security descriptors. */
1244 if (((extract_flags & (WIMLIB_EXTRACT_FLAG_STRICT_ACLS |
1245 WIMLIB_EXTRACT_FLAG_UNIX_DATA))
1246 == WIMLIB_EXTRACT_FLAG_STRICT_ACLS) &&
1247 required_features->security_descriptors &&
1248 !supported_features->security_descriptors)
1250 ERROR("Extraction backend does not support security descriptors!");
1251 return WIMLIB_ERR_UNSUPPORTED;
1253 if (!(extract_flags & WIMLIB_EXTRACT_FLAG_NO_ACLS) &&
1254 required_features->security_descriptors &&
1255 !supported_features->security_descriptors)
1256 WARNING("Ignoring Windows NT security descriptors of %lu files",
1257 required_features->security_descriptors);
1260 if ((extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) &&
1261 required_features->unix_data && !supported_features->unix_data)
1263 ERROR("Extraction backend does not support UNIX data!");
1264 return WIMLIB_ERR_UNSUPPORTED;
1267 if (required_features->unix_data &&
1268 !(extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA))
1270 WARNING("Ignoring UNIX metadata of %lu files",
1271 required_features->unix_data);
1275 if (required_features->short_names &&
1276 !supported_features->short_names)
1278 if (extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_SHORT_NAMES) {
1279 ERROR("Extraction backend does not support DOS names!");
1280 return WIMLIB_ERR_UNSUPPORTED;
1282 WARNING("Ignoring DOS names of %lu files",
1283 required_features->short_names);
1287 if ((extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_TIMESTAMPS) &&
1288 !supported_features->timestamps)
1290 ERROR("Extraction backend does not support timestamps!");
1291 return WIMLIB_ERR_UNSUPPORTED;
1297 static const struct apply_operations *
1298 select_apply_operations(int extract_flags)
1301 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS)
1302 return &ntfs_3g_apply_ops;
1305 return &win32_apply_ops;
1307 return &unix_apply_ops;
1312 extract_trees(WIMStruct *wim, struct wim_dentry **trees, size_t num_trees,
1313 const tchar *target, int extract_flags)
1315 const struct apply_operations *ops;
1316 struct apply_ctx *ctx;
1318 LIST_HEAD(dentry_list);
1320 if (extract_flags & WIMLIB_EXTRACT_FLAG_TO_STDOUT) {
1321 ret = extract_dentries_to_stdout(trees, num_trees,
1326 num_trees = remove_duplicate_trees(trees, num_trees);
1327 num_trees = remove_contained_trees(trees, num_trees);
1329 ops = select_apply_operations(extract_flags);
1331 if (num_trees > 1 && ops->single_tree_only) {
1332 ERROR("Extracting multiple directory trees "
1333 "at once is not supported in %s extraction mode!",
1335 ret = WIMLIB_ERR_UNSUPPORTED;
1339 ctx = CALLOC(1, ops->context_size);
1341 ret = WIMLIB_ERR_NOMEM;
1346 ctx->target = target;
1347 ctx->target_nchars = tstrlen(target);
1348 ctx->extract_flags = extract_flags;
1349 if (ctx->wim->progfunc) {
1350 ctx->progfunc = ctx->wim->progfunc;
1351 ctx->progctx = ctx->wim->progctx;
1352 ctx->progress.extract.image = wim->current_image;
1353 ctx->progress.extract.extract_flags = (extract_flags &
1354 WIMLIB_EXTRACT_MASK_PUBLIC);
1355 ctx->progress.extract.wimfile_name = wim->filename;
1356 ctx->progress.extract.image_name = wimlib_get_image_name(wim,
1357 wim->current_image);
1358 ctx->progress.extract.target = target;
1360 INIT_LIST_HEAD(&ctx->blob_list);
1361 filedes_invalidate(&ctx->tmpfile_fd);
1362 ctx->apply_ops = ops;
1364 ret = (*ops->get_supported_features)(target, &ctx->supported_features);
1368 build_dentry_list(&dentry_list, trees, num_trees,
1370 WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE));
1372 dentry_list_get_features(&dentry_list, &ctx->required_features);
1374 ret = do_feature_check(&ctx->required_features, &ctx->supported_features,
1375 ctx->extract_flags);
1379 ret = dentry_list_calculate_extraction_names(&dentry_list, ctx);
1383 if (unlikely(list_empty(&dentry_list))) {
1384 WARNING("There is nothing to extract!");
1388 ret = dentry_list_resolve_streams(&dentry_list, ctx);
1392 dentry_list_build_inode_alias_lists(&dentry_list);
1394 ret = dentry_list_ref_streams(&dentry_list, ctx);
1398 if (extract_flags & WIMLIB_EXTRACT_FLAG_FROM_PIPE) {
1399 /* When extracting from a pipe, the number of bytes of data to
1400 * extract can't be determined in the normal way (examining the
1401 * blob table), since at this point all we have is a set of
1402 * SHA-1 message digests of blobs that need to be extracted.
1403 * However, we can get a reasonably accurate estimate by taking
1404 * <TOTALBYTES> from the corresponding <IMAGE> in the WIM XML
1405 * data. This does assume that a full image is being extracted,
1406 * but currently there is no API for doing otherwise. (Also,
1407 * subtract <HARDLINKBYTES> from this if hard links are
1408 * supported by the extraction mode.) */
1409 ctx->progress.extract.total_bytes =
1410 wim_info_get_image_total_bytes(wim->wim_info,
1411 wim->current_image);
1412 if (ctx->supported_features.hard_links) {
1413 ctx->progress.extract.total_bytes -=
1414 wim_info_get_image_hard_link_bytes(wim->wim_info,
1415 wim->current_image);
1419 ret = extract_progress(ctx,
1420 ((extract_flags & WIMLIB_EXTRACT_FLAG_IMAGEMODE) ?
1421 WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN :
1422 WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN));
1426 ret = (*ops->extract)(&dentry_list, ctx);
1430 if (ctx->progress.extract.completed_bytes <
1431 ctx->progress.extract.total_bytes)
1433 ctx->progress.extract.completed_bytes =
1434 ctx->progress.extract.total_bytes;
1435 ret = extract_progress(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS);
1440 ret = extract_progress(ctx,
1441 ((extract_flags & WIMLIB_EXTRACT_FLAG_IMAGEMODE) ?
1442 WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END :
1443 WIMLIB_PROGRESS_MSG_EXTRACT_TREE_END));
1445 destroy_blob_list(&ctx->blob_list);
1446 destroy_dentry_list(&dentry_list);
1453 mkdir_if_needed(const tchar *target)
1455 if (!tmkdir(target, 0755))
1458 if (errno == EEXIST)
1462 /* _wmkdir() fails with EACCES if called on a drive root directory. */
1463 if (errno == EACCES)
1467 ERROR_WITH_ERRNO("Failed to create directory \"%"TS"\"", target);
1468 return WIMLIB_ERR_MKDIR;
1471 /* Make sure the extraction flags make sense, and update them if needed. */
1473 check_extract_flags(const WIMStruct *wim, int *extract_flags_p)
1475 int extract_flags = *extract_flags_p;
1477 /* Check for invalid flag combinations */
1479 if ((extract_flags &
1480 (WIMLIB_EXTRACT_FLAG_NO_ACLS |
1481 WIMLIB_EXTRACT_FLAG_STRICT_ACLS)) == (WIMLIB_EXTRACT_FLAG_NO_ACLS |
1482 WIMLIB_EXTRACT_FLAG_STRICT_ACLS))
1483 return WIMLIB_ERR_INVALID_PARAM;
1485 if ((extract_flags &
1486 (WIMLIB_EXTRACT_FLAG_RPFIX |
1487 WIMLIB_EXTRACT_FLAG_NORPFIX)) == (WIMLIB_EXTRACT_FLAG_RPFIX |
1488 WIMLIB_EXTRACT_FLAG_NORPFIX))
1489 return WIMLIB_ERR_INVALID_PARAM;
1491 #ifndef WITH_NTFS_3G
1492 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1493 ERROR("wimlib was compiled without support for NTFS-3g, so\n"
1494 " it cannot apply a WIM image directly to an NTFS volume.");
1495 return WIMLIB_ERR_UNSUPPORTED;
1499 if (extract_flags & WIMLIB_EXTRACT_FLAG_WIMBOOT) {
1502 return WIMLIB_ERR_NO_FILENAME;
1504 ERROR("WIMBoot extraction is only supported on Windows!");
1505 return WIMLIB_ERR_UNSUPPORTED;
1510 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_RPFIX |
1511 WIMLIB_EXTRACT_FLAG_NORPFIX |
1512 WIMLIB_EXTRACT_FLAG_IMAGEMODE)) ==
1513 WIMLIB_EXTRACT_FLAG_IMAGEMODE)
1515 /* For full-image extraction, do reparse point fixups by default
1516 * if the WIM header says they are enabled. */
1517 if (wim->hdr.flags & WIM_HDR_FLAG_RP_FIX)
1518 extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
1521 *extract_flags_p = extract_flags;
1526 get_wildcard_flags(int extract_flags)
1528 u32 wildcard_flags = 0;
1530 if (extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_GLOB)
1531 wildcard_flags |= WILDCARD_FLAG_ERROR_IF_NO_MATCH;
1533 wildcard_flags |= WILDCARD_FLAG_WARN_IF_NO_MATCH;
1535 if (default_ignore_case)
1536 wildcard_flags |= WILDCARD_FLAG_CASE_INSENSITIVE;
1538 return wildcard_flags;
1541 struct append_dentry_ctx {
1542 struct wim_dentry **dentries;
1543 size_t num_dentries;
1544 size_t num_alloc_dentries;
1548 append_dentry_cb(struct wim_dentry *dentry, void *_ctx)
1550 struct append_dentry_ctx *ctx = _ctx;
1552 if (ctx->num_dentries == ctx->num_alloc_dentries) {
1553 struct wim_dentry **new_dentries;
1556 new_length = max(ctx->num_alloc_dentries + 8,
1557 ctx->num_alloc_dentries * 3 / 2);
1558 new_dentries = REALLOC(ctx->dentries,
1559 new_length * sizeof(ctx->dentries[0]));
1560 if (new_dentries == NULL)
1561 return WIMLIB_ERR_NOMEM;
1562 ctx->dentries = new_dentries;
1563 ctx->num_alloc_dentries = new_length;
1565 ctx->dentries[ctx->num_dentries++] = dentry;
1570 do_wimlib_extract_paths(WIMStruct *wim, int image, const tchar *target,
1571 const tchar * const *paths, size_t num_paths,
1575 struct wim_dentry **trees;
1578 if (wim == NULL || target == NULL || target[0] == T('\0') ||
1579 (num_paths != 0 && paths == NULL))
1580 return WIMLIB_ERR_INVALID_PARAM;
1582 ret = check_extract_flags(wim, &extract_flags);
1586 ret = select_wim_image(wim, image);
1590 ret = wim_checksum_unhashed_blobs(wim);
1594 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_NTFS |
1595 WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE)) ==
1596 (WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE))
1598 ret = mkdir_if_needed(target);
1603 if (extract_flags & WIMLIB_EXTRACT_FLAG_GLOB_PATHS) {
1605 struct append_dentry_ctx append_dentry_ctx = {
1608 .num_alloc_dentries = 0,
1611 u32 wildcard_flags = get_wildcard_flags(extract_flags);
1613 for (size_t i = 0; i < num_paths; i++) {
1614 tchar *path = canonicalize_wim_path(paths[i]);
1616 ret = WIMLIB_ERR_NOMEM;
1617 trees = append_dentry_ctx.dentries;
1618 goto out_free_trees;
1620 ret = expand_wildcard(wim, path,
1626 trees = append_dentry_ctx.dentries;
1627 goto out_free_trees;
1630 trees = append_dentry_ctx.dentries;
1631 num_trees = append_dentry_ctx.num_dentries;
1633 trees = MALLOC(num_paths * sizeof(trees[0]));
1635 return WIMLIB_ERR_NOMEM;
1637 for (size_t i = 0; i < num_paths; i++) {
1639 tchar *path = canonicalize_wim_path(paths[i]);
1641 ret = WIMLIB_ERR_NOMEM;
1642 goto out_free_trees;
1645 trees[i] = get_dentry(wim, path,
1646 WIMLIB_CASE_PLATFORM_DEFAULT);
1648 if (trees[i] == NULL) {
1649 ERROR("Path \"%"TS"\" does not exist "
1651 paths[i], wim->current_image);
1652 ret = WIMLIB_ERR_PATH_DOES_NOT_EXIST;
1653 goto out_free_trees;
1656 num_trees = num_paths;
1659 if (num_trees == 0) {
1661 goto out_free_trees;
1664 ret = extract_trees(wim, trees, num_trees, target, extract_flags);
1671 extract_single_image(WIMStruct *wim, int image,
1672 const tchar *target, int extract_flags)
1674 const tchar *path = WIMLIB_WIM_ROOT_PATH;
1675 extract_flags |= WIMLIB_EXTRACT_FLAG_IMAGEMODE;
1676 return do_wimlib_extract_paths(wim, image, target, &path, 1, extract_flags);
1679 static const tchar * const filename_forbidden_chars =
1688 /* This function checks if it is okay to use a WIM image's name as a directory
1691 image_name_ok_as_dir(const tchar *image_name)
1693 return image_name && *image_name &&
1694 !tstrpbrk(image_name, filename_forbidden_chars) &&
1695 tstrcmp(image_name, T(".")) &&
1696 tstrcmp(image_name, T(".."));
1699 /* Extracts all images from the WIM to the directory @target, with the images
1700 * placed in subdirectories named by their image names. */
1702 extract_all_images(WIMStruct *wim, const tchar *target, int extract_flags)
1704 size_t image_name_max_len = max(xml_get_max_image_name_len(wim), 20);
1705 size_t output_path_len = tstrlen(target);
1706 tchar buf[output_path_len + 1 + image_name_max_len + 1];
1709 const tchar *image_name;
1711 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1712 ERROR("Cannot extract multiple images in NTFS extraction mode.");
1713 return WIMLIB_ERR_INVALID_PARAM;
1716 ret = mkdir_if_needed(target);
1719 tmemcpy(buf, target, output_path_len);
1720 buf[output_path_len] = OS_PREFERRED_PATH_SEPARATOR;
1721 for (image = 1; image <= wim->hdr.image_count; image++) {
1722 image_name = wimlib_get_image_name(wim, image);
1723 if (image_name_ok_as_dir(image_name)) {
1724 tstrcpy(buf + output_path_len + 1, image_name);
1726 /* Image name is empty or contains forbidden characters.
1727 * Use image number instead. */
1728 tsprintf(buf + output_path_len + 1, T("%d"), image);
1730 ret = extract_single_image(wim, image, buf, extract_flags);
1738 do_wimlib_extract_image(WIMStruct *wim, int image, const tchar *target,
1741 if (extract_flags & (WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE |
1742 WIMLIB_EXTRACT_FLAG_TO_STDOUT |
1743 WIMLIB_EXTRACT_FLAG_GLOB_PATHS))
1744 return WIMLIB_ERR_INVALID_PARAM;
1746 if (image == WIMLIB_ALL_IMAGES)
1747 return extract_all_images(wim, target, extract_flags);
1749 return extract_single_image(wim, image, target, extract_flags);
1753 /****************************************************************************
1755 ****************************************************************************/
1758 wimlib_extract_paths(WIMStruct *wim, int image, const tchar *target,
1759 const tchar * const *paths, size_t num_paths,
1762 if (extract_flags & ~WIMLIB_EXTRACT_MASK_PUBLIC)
1763 return WIMLIB_ERR_INVALID_PARAM;
1765 return do_wimlib_extract_paths(wim, image, target, paths, num_paths,
1770 wimlib_extract_pathlist(WIMStruct *wim, int image, const tchar *target,
1771 const tchar *path_list_file, int extract_flags)
1778 ret = read_path_list_file(path_list_file, &paths, &num_paths, &mem);
1780 ERROR("Failed to read path list file \"%"TS"\"",
1785 ret = wimlib_extract_paths(wim, image, target,
1786 (const tchar * const *)paths, num_paths,
1794 wimlib_extract_image_from_pipe_with_progress(int pipe_fd,
1795 const tchar *image_num_or_name,
1796 const tchar *target,
1798 wimlib_progress_func_t progfunc,
1803 struct filedes *in_fd;
1807 if (extract_flags & ~WIMLIB_EXTRACT_MASK_PUBLIC)
1808 return WIMLIB_ERR_INVALID_PARAM;
1810 /* Read the WIM header from the pipe and get a WIMStruct to represent
1811 * the pipable WIM. Caveats: Unlike getting a WIMStruct with
1812 * wimlib_open_wim(), getting a WIMStruct in this way will result in an
1813 * empty blob table, no XML data read, and no filename set. */
1814 ret = open_wim_as_WIMStruct(&pipe_fd, WIMLIB_OPEN_FLAG_FROM_PIPE, &pwm,
1819 /* Sanity check to make sure this is a pipable WIM. */
1820 if (pwm->hdr.magic != PWM_MAGIC) {
1821 ERROR("The WIM being read from file descriptor %d "
1822 "is not pipable!", pipe_fd);
1823 ret = WIMLIB_ERR_NOT_PIPABLE;
1824 goto out_wimlib_free;
1827 /* Sanity check to make sure the first part of a pipable split WIM is
1828 * sent over the pipe first. */
1829 if (pwm->hdr.part_number != 1) {
1830 ERROR("The first part of the split WIM must be "
1831 "sent over the pipe first.");
1832 ret = WIMLIB_ERR_INVALID_PIPABLE_WIM;
1833 goto out_wimlib_free;
1836 in_fd = &pwm->in_fd;
1837 wimlib_assert(in_fd->offset == WIM_HEADER_DISK_SIZE);
1839 /* As mentioned, the WIMStruct we created from the pipe does not have
1840 * XML data yet. Fix this by reading the extra copy of the XML data
1841 * that directly follows the header in pipable WIMs. (Note: see
1842 * write_pipable_wim() for more details about the format of pipable
1845 u8 hash[SHA1_HASH_SIZE];
1847 ret = read_pwm_blob_header(pwm, hash,
1848 &pwm->hdr.xml_data_reshdr, NULL);
1850 goto out_wimlib_free;
1852 if (!(pwm->hdr.xml_data_reshdr.flags & WIM_RESHDR_FLAG_METADATA)) {
1853 ERROR("Expected XML data, but found non-metadata resource.");
1854 ret = WIMLIB_ERR_INVALID_PIPABLE_WIM;
1855 goto out_wimlib_free;
1858 ret = read_wim_xml_data(pwm);
1860 goto out_wimlib_free;
1862 if (wim_info_get_num_images(pwm->wim_info) != pwm->hdr.image_count) {
1863 ERROR("Image count in XML data is not the same as in WIM header.");
1864 ret = WIMLIB_ERR_IMAGE_COUNT;
1865 goto out_wimlib_free;
1869 /* Get image index (this may use the XML data that was just read to
1870 * resolve an image name). */
1871 if (image_num_or_name) {
1872 image = wimlib_resolve_image(pwm, image_num_or_name);
1873 if (image == WIMLIB_NO_IMAGE) {
1874 ERROR("\"%"TS"\" is not a valid image in the pipable WIM!",
1876 ret = WIMLIB_ERR_INVALID_IMAGE;
1877 goto out_wimlib_free;
1878 } else if (image == WIMLIB_ALL_IMAGES) {
1879 ERROR("Applying all images from a pipe is not supported!");
1880 ret = WIMLIB_ERR_INVALID_IMAGE;
1881 goto out_wimlib_free;
1884 if (pwm->hdr.image_count != 1) {
1885 ERROR("No image was specified, but the pipable WIM "
1886 "did not contain exactly 1 image");
1887 ret = WIMLIB_ERR_INVALID_IMAGE;
1888 goto out_wimlib_free;
1893 /* Load the needed metadata resource. */
1894 for (i = 1; i <= pwm->hdr.image_count; i++) {
1895 struct wim_image_metadata *imd;
1896 struct wim_reshdr reshdr;
1897 struct wim_resource_descriptor *metadata_rdesc;
1899 imd = pwm->image_metadata[i - 1];
1901 ret = WIMLIB_ERR_NOMEM;
1902 imd->metadata_blob = new_blob_descriptor();
1903 if (!imd->metadata_blob)
1904 goto out_wimlib_free;
1906 imd->metadata_blob->is_metadata = 1;
1908 ret = read_pwm_blob_header(pwm, imd->metadata_blob->hash,
1911 goto out_wimlib_free;
1913 if (!(reshdr.flags & WIM_RESHDR_FLAG_METADATA)) {
1914 ERROR("Expected metadata resource, but found "
1915 "non-metadata resource");
1916 ret = WIMLIB_ERR_INVALID_PIPABLE_WIM;
1917 goto out_wimlib_free;
1920 ret = WIMLIB_ERR_NOMEM;
1921 metadata_rdesc = MALLOC(sizeof(struct wim_resource_descriptor));
1922 if (!metadata_rdesc)
1923 goto out_wimlib_free;
1924 wim_reshdr_to_desc_and_blob(&reshdr, pwm, metadata_rdesc,
1925 imd->metadata_blob);
1928 /* Metadata resource is for the image being extracted.
1929 * Parse it and save the metadata in memory. */
1930 ret = read_metadata_resource(imd);
1932 goto out_wimlib_free;
1935 /* Metadata resource is not for the image being
1936 * extracted. Skip over it. */
1937 ret = skip_wim_resource(metadata_rdesc);
1939 goto out_wimlib_free;
1942 /* Extract the image. */
1943 extract_flags |= WIMLIB_EXTRACT_FLAG_FROM_PIPE;
1944 ret = do_wimlib_extract_image(pwm, image, target, extract_flags);
1945 /* Clean up and return. */
1953 wimlib_extract_image_from_pipe(int pipe_fd, const tchar *image_num_or_name,
1954 const tchar *target, int extract_flags)
1956 return wimlib_extract_image_from_pipe_with_progress(pipe_fd,
1965 wimlib_extract_image(WIMStruct *wim, int image, const tchar *target,
1968 if (extract_flags & ~WIMLIB_EXTRACT_MASK_PUBLIC)
1969 return WIMLIB_ERR_INVALID_PARAM;
1970 return do_wimlib_extract_image(wim, image, target, extract_flags);