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/pathlist.h"
57 #include "wimlib/paths.h"
58 #include "wimlib/pattern.h"
59 #include "wimlib/reparse.h"
60 #include "wimlib/resource.h"
61 #include "wimlib/security.h"
62 #include "wimlib/unix_data.h"
63 #include "wimlib/wim.h"
64 #include "wimlib/win32.h" /* for realpath() equivalent */
65 #include "wimlib/xml.h"
67 #define WIMLIB_EXTRACT_FLAG_FROM_PIPE 0x80000000
68 #define WIMLIB_EXTRACT_FLAG_IMAGEMODE 0x40000000
70 /* Keep in sync with wimlib.h */
71 #define WIMLIB_EXTRACT_MASK_PUBLIC \
72 (WIMLIB_EXTRACT_FLAG_NTFS | \
73 WIMLIB_EXTRACT_FLAG_UNIX_DATA | \
74 WIMLIB_EXTRACT_FLAG_NO_ACLS | \
75 WIMLIB_EXTRACT_FLAG_STRICT_ACLS | \
76 WIMLIB_EXTRACT_FLAG_RPFIX | \
77 WIMLIB_EXTRACT_FLAG_NORPFIX | \
78 WIMLIB_EXTRACT_FLAG_TO_STDOUT | \
79 WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES | \
80 WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS | \
81 WIMLIB_EXTRACT_FLAG_STRICT_TIMESTAMPS | \
82 WIMLIB_EXTRACT_FLAG_STRICT_SHORT_NAMES | \
83 WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS | \
84 WIMLIB_EXTRACT_FLAG_GLOB_PATHS | \
85 WIMLIB_EXTRACT_FLAG_STRICT_GLOB | \
86 WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES | \
87 WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE | \
88 WIMLIB_EXTRACT_FLAG_WIMBOOT | \
89 WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS4K | \
90 WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS8K | \
91 WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS16K | \
92 WIMLIB_EXTRACT_FLAG_COMPACT_LZX \
95 /* Send WIMLIB_PROGRESS_MSG_EXTRACT_FILE_STRUCTURE or
96 * WIMLIB_PROGRESS_MSG_EXTRACT_METADATA. */
98 do_file_extract_progress(struct apply_ctx *ctx, enum wimlib_progress_msg msg)
100 ctx->count_until_file_progress = 500; /* Arbitrary value to limit calls */
101 return extract_progress(ctx, msg);
105 start_file_phase(struct apply_ctx *ctx, u64 end_file_count, enum wimlib_progress_msg msg)
107 ctx->progress.extract.current_file_count = 0;
108 ctx->progress.extract.end_file_count = end_file_count;
109 return do_file_extract_progress(ctx, msg);
113 start_file_structure_phase(struct apply_ctx *ctx, u64 end_file_count)
115 return start_file_phase(ctx, end_file_count, WIMLIB_PROGRESS_MSG_EXTRACT_FILE_STRUCTURE);
119 start_file_metadata_phase(struct apply_ctx *ctx, u64 end_file_count)
121 return start_file_phase(ctx, end_file_count, WIMLIB_PROGRESS_MSG_EXTRACT_METADATA);
125 end_file_phase(struct apply_ctx *ctx, enum wimlib_progress_msg msg)
127 ctx->progress.extract.current_file_count = ctx->progress.extract.end_file_count;
128 return do_file_extract_progress(ctx, msg);
132 end_file_structure_phase(struct apply_ctx *ctx)
134 return end_file_phase(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_FILE_STRUCTURE);
138 end_file_metadata_phase(struct apply_ctx *ctx)
140 return end_file_phase(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_METADATA);
143 #define PWM_FOUND_WIM_HDR (-1)
145 /* Read the header for a blob in a pipable WIM. If @pwm_hdr_ret is not NULL,
146 * also look for a pipable WIM header and return PWM_FOUND_WIM_HDR if found. */
148 read_pwm_blob_header(WIMStruct *pwm, u8 hash_ret[SHA1_HASH_SIZE],
149 struct wim_reshdr *reshdr_ret,
150 struct wim_header_disk *pwm_hdr_ret)
153 struct pwm_blob_hdr blob_hdr;
156 ret = full_read(&pwm->in_fd, &blob_hdr, sizeof(blob_hdr));
160 magic = le64_to_cpu(blob_hdr.magic);
162 if (magic == PWM_MAGIC && pwm_hdr_ret != NULL) {
163 memcpy(pwm_hdr_ret, &blob_hdr, sizeof(blob_hdr));
164 ret = full_read(&pwm->in_fd,
165 (u8 *)pwm_hdr_ret + sizeof(blob_hdr),
166 sizeof(*pwm_hdr_ret) - sizeof(blob_hdr));
169 return PWM_FOUND_WIM_HDR;
172 if (unlikely(magic != PWM_BLOB_MAGIC)) {
173 ERROR("Data read on pipe is invalid (expected blob header)");
174 return WIMLIB_ERR_INVALID_PIPABLE_WIM;
177 copy_hash(hash_ret, blob_hdr.hash);
179 reshdr_ret->size_in_wim = 0; /* Not available */
180 reshdr_ret->flags = le32_to_cpu(blob_hdr.flags);
181 reshdr_ret->offset_in_wim = pwm->in_fd.offset;
182 reshdr_ret->uncompressed_size = le64_to_cpu(blob_hdr.uncompressed_size);
184 if (unlikely(reshdr_ret->uncompressed_size == 0)) {
185 ERROR("Data read on pipe is invalid (resource is of 0 size)");
186 return WIMLIB_ERR_INVALID_PIPABLE_WIM;
192 if (ret == WIMLIB_ERR_UNEXPECTED_END_OF_FILE)
193 ERROR("The pipe ended before all needed data was sent!");
195 ERROR_WITH_ERRNO("Error reading pipable WIM from pipe");
200 read_blobs_from_pipe(struct apply_ctx *ctx, const struct read_blob_callbacks *cbs)
203 u8 hash[SHA1_HASH_SIZE];
204 struct wim_reshdr reshdr;
205 struct wim_header_disk pwm_hdr;
206 struct wim_resource_descriptor rdesc;
207 struct blob_descriptor *blob;
209 copy_guid(ctx->progress.extract.guid, ctx->wim->hdr.guid);
210 ctx->progress.extract.part_number = ctx->wim->hdr.part_number;
211 ctx->progress.extract.total_parts = ctx->wim->hdr.total_parts;
212 ret = extract_progress(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN);
216 while (ctx->num_blobs_remaining) {
218 ret = read_pwm_blob_header(ctx->wim, hash, &reshdr, &pwm_hdr);
220 if (ret == PWM_FOUND_WIM_HDR) {
221 u16 part_number = le16_to_cpu(pwm_hdr.part_number);
222 u16 total_parts = le16_to_cpu(pwm_hdr.total_parts);
224 if (part_number == ctx->progress.extract.part_number &&
225 total_parts == ctx->progress.extract.total_parts &&
226 guids_equal(pwm_hdr.guid, ctx->progress.extract.guid))
229 copy_guid(ctx->progress.extract.guid, pwm_hdr.guid);
230 ctx->progress.extract.part_number = part_number;
231 ctx->progress.extract.total_parts = total_parts;
232 ret = extract_progress(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN);
242 if (!(reshdr.flags & WIM_RESHDR_FLAG_METADATA)
243 && (blob = lookup_blob(ctx->wim->blob_table, hash))
244 && (blob->out_refcnt))
246 wim_reshdr_to_desc_and_blob(&reshdr, ctx->wim, &rdesc, blob);
247 ret = read_blob_with_sha1(blob, cbs);
248 blob_unset_is_located_in_wim_resource(blob);
251 ctx->num_blobs_remaining--;
253 wim_reshdr_to_desc(&reshdr, ctx->wim, &rdesc);
254 ret = skip_wim_resource(&rdesc);
264 handle_pwm_metadata_resource(WIMStruct *pwm, int image, bool is_needed)
266 struct blob_descriptor *blob;
267 struct wim_reshdr reshdr;
268 struct wim_resource_descriptor *rdesc;
271 ret = WIMLIB_ERR_NOMEM;
272 blob = new_blob_descriptor();
276 ret = read_pwm_blob_header(pwm, blob->hash, &reshdr, NULL);
280 ret = WIMLIB_ERR_INVALID_PIPABLE_WIM;
281 if (!(reshdr.flags & WIM_RESHDR_FLAG_METADATA)) {
282 ERROR("Expected metadata resource, but found non-metadata "
287 ret = WIMLIB_ERR_NOMEM;
288 rdesc = MALLOC(sizeof(*rdesc));
292 wim_reshdr_to_desc_and_blob(&reshdr, pwm, rdesc, blob);
295 ret = WIMLIB_ERR_NOMEM;
296 pwm->image_metadata[image - 1] = new_unloaded_image_metadata(blob);
297 if (!pwm->image_metadata[image - 1])
301 /* If the metadata resource is for the image being extracted, then parse
302 * it and save the metadata in memory. Otherwise, skip over it. */
304 ret = select_wim_image(pwm, image);
306 ret = skip_wim_resource(rdesc);
308 free_blob_descriptor(blob);
312 /* Creates a temporary file opened for writing. The open file descriptor is
313 * returned in @fd_ret and its name is returned in @name_ret (dynamically
316 create_temporary_file(struct filedes *fd_ret, tchar **name_ret)
323 name = _wtempnam(NULL, L"wimlib");
325 ERROR_WITH_ERRNO("Failed to create temporary filename");
326 return WIMLIB_ERR_NOMEM;
328 raw_fd = _wopen(name, O_WRONLY | O_CREAT | O_EXCL | O_BINARY |
329 _O_SHORT_LIVED, 0600);
330 if (raw_fd < 0 && errno == EEXIST) {
334 #else /* __WIN32__ */
335 const char *tmpdir = getenv("TMPDIR");
338 name = MALLOC(strlen(tmpdir) + 1 + 6 + 6 + 1);
340 return WIMLIB_ERR_NOMEM;
341 sprintf(name, "%s/wimlibXXXXXX", tmpdir);
342 raw_fd = mkstemp(name);
343 #endif /* !__WIN32__ */
346 ERROR_WITH_ERRNO("Failed to create temporary file "
349 return WIMLIB_ERR_OPEN;
352 filedes_init(fd_ret, raw_fd);
358 begin_extract_blob_wrapper(struct blob_descriptor *blob, void *_ctx)
360 struct apply_ctx *ctx = _ctx;
362 ctx->cur_blob = blob;
363 ctx->cur_blob_offset = 0;
365 if (unlikely(blob->out_refcnt > MAX_OPEN_FILES))
366 return create_temporary_file(&ctx->tmpfile_fd, &ctx->tmpfile_name);
368 return call_begin_blob(blob, ctx->saved_cbs);
372 extract_chunk_wrapper(const void *chunk, size_t size, void *_ctx)
374 struct apply_ctx *ctx = _ctx;
375 union wimlib_progress_info *progress = &ctx->progress;
378 ctx->cur_blob_offset += size;
380 if (likely(ctx->supported_features.hard_links)) {
381 progress->extract.completed_bytes +=
382 (u64)size * ctx->cur_blob->out_refcnt;
383 if (ctx->cur_blob_offset == ctx->cur_blob->size)
384 progress->extract.completed_streams += ctx->cur_blob->out_refcnt;
386 const struct blob_extraction_target *targets =
387 blob_extraction_targets(ctx->cur_blob);
388 for (u32 i = 0; i < ctx->cur_blob->out_refcnt; i++) {
389 const struct wim_inode *inode = targets[i].inode;
390 const struct wim_dentry *dentry;
392 inode_for_each_extraction_alias(dentry, inode) {
393 progress->extract.completed_bytes += size;
394 if (ctx->cur_blob_offset == ctx->cur_blob->size)
395 progress->extract.completed_streams++;
399 if (progress->extract.completed_bytes >= ctx->next_progress) {
401 ret = extract_progress(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS);
405 set_next_progress(progress->extract.completed_bytes,
406 progress->extract.total_bytes,
407 &ctx->next_progress);
410 if (unlikely(filedes_valid(&ctx->tmpfile_fd))) {
411 /* Just extracting to temporary file for now. */
412 ret = full_write(&ctx->tmpfile_fd, chunk, size);
414 ERROR_WITH_ERRNO("Error writing data to "
415 "temporary file \"%"TS"\"",
421 return call_consume_chunk(chunk, size, ctx->saved_cbs);
424 /* Copy the blob's data from the temporary file to each of its targets.
426 * This is executed only in the very uncommon case that a blob is being
427 * extracted to more than MAX_OPEN_FILES targets! */
429 extract_from_tmpfile(const tchar *tmpfile_name,
430 const struct blob_descriptor *orig_blob,
431 const struct read_blob_callbacks *cbs)
433 struct blob_descriptor tmpfile_blob;
434 const struct blob_extraction_target *targets = blob_extraction_targets(orig_blob);
437 memcpy(&tmpfile_blob, orig_blob, sizeof(struct blob_descriptor));
438 tmpfile_blob.blob_location = BLOB_IN_FILE_ON_DISK;
439 tmpfile_blob.file_on_disk = (tchar *)tmpfile_name;
440 tmpfile_blob.out_refcnt = 1;
442 for (u32 i = 0; i < orig_blob->out_refcnt; i++) {
443 tmpfile_blob.inline_blob_extraction_targets[0] = targets[i];
444 ret = read_blob_with_cbs(&tmpfile_blob, cbs);
452 end_extract_blob_wrapper(struct blob_descriptor *blob, int status, void *_ctx)
454 struct apply_ctx *ctx = _ctx;
456 if (unlikely(filedes_valid(&ctx->tmpfile_fd))) {
457 filedes_close(&ctx->tmpfile_fd);
459 status = extract_from_tmpfile(ctx->tmpfile_name, blob,
461 filedes_invalidate(&ctx->tmpfile_fd);
462 tunlink(ctx->tmpfile_name);
463 FREE(ctx->tmpfile_name);
467 return call_end_blob(blob, status, ctx->saved_cbs);
471 * Read the list of blobs to extract and feed their data into the specified
472 * callback functions.
474 * This handles checksumming each blob.
476 * This also handles sending WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS.
478 * This also works if the WIM is being read from a pipe.
480 * This also will split up blobs that will need to be extracted to more than
481 * MAX_OPEN_FILES locations, as measured by the 'out_refcnt' of each blob.
482 * Therefore, the apply_operations implementation need not worry about running
483 * out of file descriptors, unless it might open more than one file descriptor
484 * per 'blob_extraction_target' (e.g. Win32 currently might because the
485 * destination file system might not support hard links).
488 extract_blob_list(struct apply_ctx *ctx, const struct read_blob_callbacks *cbs)
490 struct read_blob_callbacks wrapper_cbs = {
491 .begin_blob = begin_extract_blob_wrapper,
492 .consume_chunk = extract_chunk_wrapper,
493 .end_blob = end_extract_blob_wrapper,
496 ctx->saved_cbs = cbs;
497 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_FROM_PIPE) {
498 return read_blobs_from_pipe(ctx, &wrapper_cbs);
500 return read_blob_list(&ctx->blob_list,
501 offsetof(struct blob_descriptor,
503 &wrapper_cbs, VERIFY_BLOB_HASHES);
507 /* Extract a WIM dentry to standard output.
509 * This obviously doesn't make sense in all cases. We return an error if the
510 * dentry does not correspond to a regular file. Otherwise we extract the
511 * unnamed data stream only. */
513 extract_dentry_to_stdout(struct wim_dentry *dentry,
514 const struct blob_table *blob_table)
516 struct wim_inode *inode = dentry->d_inode;
517 struct blob_descriptor *blob;
518 struct filedes _stdout;
520 if (inode->i_attributes & (FILE_ATTRIBUTE_REPARSE_POINT |
521 FILE_ATTRIBUTE_DIRECTORY |
522 FILE_ATTRIBUTE_ENCRYPTED))
524 ERROR("\"%"TS"\" is not a regular file and therefore cannot be "
525 "extracted to standard output", dentry_full_path(dentry));
526 return WIMLIB_ERR_NOT_A_REGULAR_FILE;
529 blob = inode_get_blob_for_unnamed_data_stream(inode, blob_table);
531 const u8 *hash = inode_get_hash_of_unnamed_data_stream(inode);
532 if (!is_zero_hash(hash))
533 return blob_not_found_error(inode, hash);
537 filedes_init(&_stdout, STDOUT_FILENO);
538 return extract_blob_to_fd(blob, &_stdout);
542 extract_dentries_to_stdout(struct wim_dentry **dentries, size_t num_dentries,
543 const struct blob_table *blob_table)
545 for (size_t i = 0; i < num_dentries; i++) {
546 int ret = extract_dentry_to_stdout(dentries[i], blob_table);
553 /**********************************************************************/
556 * Removes duplicate dentries from the array.
558 * Returns the new number of dentries, packed at the front of the array.
561 remove_duplicate_trees(struct wim_dentry **trees, size_t num_trees)
564 for (i = 0; i < num_trees; i++) {
565 if (!trees[i]->d_tmp_flag) {
566 /* Found distinct dentry. */
567 trees[i]->d_tmp_flag = 1;
568 trees[j++] = trees[i];
571 for (i = 0; i < j; i++)
572 trees[i]->d_tmp_flag = 0;
577 * Remove dentries that are descendants of other dentries in the array.
579 * Returns the new number of dentries, packed at the front of the array.
582 remove_contained_trees(struct wim_dentry **trees, size_t num_trees)
585 for (i = 0; i < num_trees; i++)
586 trees[i]->d_tmp_flag = 1;
587 for (i = 0; i < num_trees; i++) {
588 struct wim_dentry *d = trees[i];
589 while (!dentry_is_root(d)) {
594 trees[j++] = trees[i];
598 trees[i]->d_tmp_flag = 0;
601 for (i = 0; i < j; i++)
602 trees[i]->d_tmp_flag = 0;
607 dentry_append_to_list(struct wim_dentry *dentry, void *_dentry_list)
609 struct list_head *dentry_list = _dentry_list;
610 list_add_tail(&dentry->d_extraction_list_node, dentry_list);
615 dentry_reset_extraction_list_node(struct wim_dentry *dentry)
617 dentry->d_extraction_list_node = (struct list_head){NULL, NULL};
621 dentry_delete_from_list(struct wim_dentry *dentry, void *_ignore)
623 if (will_extract_dentry(dentry)) {
624 list_del(&dentry->d_extraction_list_node);
625 dentry_reset_extraction_list_node(dentry);
631 * Build the preliminary list of dentries to be extracted.
633 * The list maintains the invariant that if d1 and d2 are in the list and d1 is
634 * an ancestor of d2, then d1 appears before d2 in the list.
637 build_dentry_list(struct list_head *dentry_list, struct wim_dentry **trees,
638 size_t num_trees, bool add_ancestors)
640 INIT_LIST_HEAD(dentry_list);
642 /* Add the trees recursively. */
643 for (size_t i = 0; i < num_trees; i++)
644 for_dentry_in_tree(trees[i], dentry_append_to_list, dentry_list);
646 /* If requested, add ancestors of the trees. */
648 for (size_t i = 0; i < num_trees; i++) {
649 struct wim_dentry *dentry = trees[i];
650 struct wim_dentry *ancestor;
651 struct list_head *place_after;
653 if (dentry_is_root(dentry))
656 place_after = dentry_list;
659 ancestor = ancestor->d_parent;
660 if (will_extract_dentry(ancestor)) {
661 place_after = &ancestor->d_extraction_list_node;
664 } while (!dentry_is_root(ancestor));
668 ancestor = ancestor->d_parent;
669 if (will_extract_dentry(ancestor))
671 list_add(&ancestor->d_extraction_list_node, place_after);
672 } while (!dentry_is_root(ancestor));
678 destroy_dentry_list(struct list_head *dentry_list)
680 struct wim_dentry *dentry, *tmp;
681 struct wim_inode *inode;
683 list_for_each_entry_safe(dentry, tmp, dentry_list, d_extraction_list_node) {
684 inode = dentry->d_inode;
685 dentry_reset_extraction_list_node(dentry);
686 inode->i_visited = 0;
687 inode->i_can_externally_back = 0;
688 if ((void *)dentry->d_extraction_name != (void *)dentry->d_name)
689 FREE(dentry->d_extraction_name);
690 dentry->d_extraction_name = NULL;
691 dentry->d_extraction_name_nchars = 0;
696 destroy_blob_list(struct list_head *blob_list)
698 struct blob_descriptor *blob;
700 list_for_each_entry(blob, blob_list, extraction_list)
701 if (blob->out_refcnt > ARRAY_LEN(blob->inline_blob_extraction_targets))
702 FREE(blob->blob_extraction_targets);
706 static const utf16lechar replacement_char = cpu_to_le16(0xfffd);
708 static const utf16lechar replacement_char = cpu_to_le16('?');
712 file_name_valid(utf16lechar *name, size_t num_chars, bool fix)
718 for (i = 0; i < num_chars; i++) {
719 switch (le16_to_cpu(name[i])) {
721 case '\x01'...'\x1F':
734 name[i] = replacement_char;
744 dentry_calculate_extraction_name(struct wim_dentry *dentry,
745 struct apply_ctx *ctx)
749 if (dentry_is_root(dentry))
753 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
754 dentry->d_extraction_name = dentry->d_name;
755 dentry->d_extraction_name_nchars = dentry->d_name_nbytes /
761 if (!ctx->supported_features.case_sensitive_filenames) {
762 struct wim_dentry *other;
763 list_for_each_entry(other, &dentry->d_ci_conflict_list,
766 if (will_extract_dentry(other)) {
767 if (ctx->extract_flags &
768 WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS) {
769 WARNING("\"%"TS"\" has the same "
770 "case-insensitive name as "
771 "\"%"TS"\"; extracting "
772 "dummy name instead",
773 dentry_full_path(dentry),
774 dentry_full_path(other));
777 WARNING("Not extracting \"%"TS"\": "
778 "has same case-insensitive "
780 dentry_full_path(dentry),
781 dentry_full_path(other));
788 if (file_name_valid(dentry->d_name, dentry->d_name_nbytes / 2, false)) {
790 ret = utf16le_get_tstr(dentry->d_name,
791 dentry->d_name_nbytes,
792 (const tchar **)&dentry->d_extraction_name,
794 dentry->d_extraction_name_nchars = nbytes / sizeof(tchar);
797 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES)
799 WARNING("\"%"TS"\" has an invalid filename "
800 "that is not supported on this platform; "
801 "extracting dummy name instead",
802 dentry_full_path(dentry));
805 WARNING("Not extracting \"%"TS"\": has an invalid filename "
806 "that is not supported on this platform",
807 dentry_full_path(dentry));
814 utf16lechar utf16_name_copy[dentry->d_name_nbytes / 2];
816 memcpy(utf16_name_copy, dentry->d_name, dentry->d_name_nbytes);
817 file_name_valid(utf16_name_copy, dentry->d_name_nbytes / 2, true);
819 const tchar *tchar_name;
822 ret = utf16le_get_tstr(utf16_name_copy,
823 dentry->d_name_nbytes,
824 &tchar_name, &tchar_nchars);
828 tchar_nchars /= sizeof(tchar);
830 size_t fixed_name_num_chars = tchar_nchars;
831 tchar fixed_name[tchar_nchars + 50];
833 tmemcpy(fixed_name, tchar_name, tchar_nchars);
834 fixed_name_num_chars += tsprintf(fixed_name + tchar_nchars,
835 T(" (invalid filename #%lu)"),
836 ++ctx->invalid_sequence);
838 utf16le_put_tstr(tchar_name);
840 dentry->d_extraction_name = TSTRDUP(fixed_name);
841 if (!dentry->d_extraction_name)
842 return WIMLIB_ERR_NOMEM;
843 dentry->d_extraction_name_nchars = fixed_name_num_chars;
848 for_dentry_in_tree(dentry, dentry_delete_from_list, NULL);
853 * Calculate the actual filename component at which each WIM dentry will be
854 * extracted, with special handling for dentries that are unsupported by the
855 * extraction backend or have invalid names.
857 * ctx->supported_features must be filled in.
859 * Possible error codes: WIMLIB_ERR_NOMEM, WIMLIB_ERR_INVALID_UTF16_STRING
862 dentry_list_calculate_extraction_names(struct list_head *dentry_list,
863 struct apply_ctx *ctx)
865 struct list_head *prev, *cur;
867 /* Can't use list_for_each_entry() because a call to
868 * dentry_calculate_extraction_name() may delete the current dentry and
869 * its children from the list. */
873 struct wim_dentry *dentry;
877 if (cur == dentry_list)
880 dentry = list_entry(cur, struct wim_dentry, d_extraction_list_node);
882 ret = dentry_calculate_extraction_name(dentry, ctx);
886 if (prev->next == cur)
889 ; /* Current dentry and its children (which follow in
890 the list) were deleted. prev stays the same. */
896 dentry_resolve_streams(struct wim_dentry *dentry, int extract_flags,
897 struct blob_table *blob_table)
899 struct wim_inode *inode = dentry->d_inode;
900 struct blob_descriptor *blob;
904 /* Special case: when extracting from a pipe, the WIM blob table is
905 * initially empty, so "resolving" an inode's streams is initially not
906 * possible. However, we still need to keep track of which blobs,
907 * identified by SHA-1 message digests, need to be extracted, so we
908 * "resolve" the inode's streams anyway by allocating a 'struct
909 * blob_descriptor' for each one. */
910 if (extract_flags & WIMLIB_EXTRACT_FLAG_FROM_PIPE)
912 ret = inode_resolve_streams(inode, blob_table, force);
915 for (unsigned i = 0; i < inode->i_num_streams; i++) {
916 blob = stream_blob_resolved(&inode->i_streams[i]);
918 blob->out_refcnt = 0;
924 * For each dentry to be extracted, resolve all streams in the corresponding
925 * inode and set 'out_refcnt' in all referenced blob_descriptors to 0.
927 * Possible error codes: WIMLIB_ERR_RESOURCE_NOT_FOUND, WIMLIB_ERR_NOMEM.
930 dentry_list_resolve_streams(struct list_head *dentry_list,
931 struct apply_ctx *ctx)
933 struct wim_dentry *dentry;
936 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
937 ret = dentry_resolve_streams(dentry,
939 ctx->wim->blob_table);
947 ref_stream(struct wim_inode_stream *strm, struct wim_dentry *dentry,
948 struct apply_ctx *ctx)
950 struct wim_inode *inode = dentry->d_inode;
951 struct blob_descriptor *blob = stream_blob_resolved(strm);
952 struct blob_extraction_target *targets;
957 /* Tally the size only for each actual extraction of the stream (not
958 * additional hard links to the inode). */
959 if (inode->i_visited && ctx->supported_features.hard_links)
962 ctx->progress.extract.total_bytes += blob->size;
963 ctx->progress.extract.total_streams++;
965 if (inode->i_visited)
968 /* Add each blob to 'ctx->blob_list' only one time, regardless of how
969 * many extraction targets it will have. */
970 if (blob->out_refcnt == 0) {
971 list_add_tail(&blob->extraction_list, &ctx->blob_list);
972 ctx->num_blobs_remaining++;
975 /* Set this stream as an extraction target of 'blob'. */
977 if (blob->out_refcnt < ARRAY_LEN(blob->inline_blob_extraction_targets)) {
978 targets = blob->inline_blob_extraction_targets;
980 struct blob_extraction_target *prev_targets;
981 size_t alloc_blob_extraction_targets;
983 if (blob->out_refcnt == ARRAY_LEN(blob->inline_blob_extraction_targets)) {
985 alloc_blob_extraction_targets = ARRAY_LEN(blob->inline_blob_extraction_targets);
987 prev_targets = blob->blob_extraction_targets;
988 alloc_blob_extraction_targets = blob->alloc_blob_extraction_targets;
991 if (blob->out_refcnt == alloc_blob_extraction_targets) {
992 alloc_blob_extraction_targets *= 2;
993 targets = REALLOC(prev_targets,
994 alloc_blob_extraction_targets *
997 return WIMLIB_ERR_NOMEM;
1000 blob->inline_blob_extraction_targets,
1001 sizeof(blob->inline_blob_extraction_targets));
1003 blob->blob_extraction_targets = targets;
1004 blob->alloc_blob_extraction_targets = alloc_blob_extraction_targets;
1006 targets = blob->blob_extraction_targets;
1008 targets[blob->out_refcnt].inode = inode;
1009 targets[blob->out_refcnt].stream = strm;
1015 ref_stream_if_needed(struct wim_dentry *dentry, struct wim_inode *inode,
1016 struct wim_inode_stream *strm, struct apply_ctx *ctx)
1018 bool need_stream = false;
1019 switch (strm->stream_type) {
1020 case STREAM_TYPE_DATA:
1021 if (stream_is_named(strm)) {
1022 /* Named data stream */
1023 if (ctx->supported_features.named_data_streams)
1025 } else if (!(inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
1026 FILE_ATTRIBUTE_ENCRYPTED))
1027 && !(inode_is_symlink(inode)
1028 && !ctx->supported_features.reparse_points
1029 && ctx->supported_features.symlink_reparse_points))
1032 * Unnamed data stream. Skip if any of the following is true:
1034 * - file is a directory
1035 * - file is encrypted
1036 * - backend needs to create the file as UNIX symlink
1037 * - backend will extract the stream as externally
1038 * backed from the WIM archive itself
1040 if (ctx->apply_ops->will_back_from_wim) {
1041 int ret = (*ctx->apply_ops->will_back_from_wim)(dentry, ctx);
1042 if (ret > 0) /* Error? */
1044 if (ret < 0) /* Won't externally back? */
1051 case STREAM_TYPE_REPARSE_POINT:
1052 wimlib_assert(inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT);
1053 if (ctx->supported_features.reparse_points ||
1054 (inode_is_symlink(inode) &&
1055 ctx->supported_features.symlink_reparse_points))
1058 case STREAM_TYPE_EFSRPC_RAW_DATA:
1059 wimlib_assert(inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED);
1060 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY) {
1061 if (ctx->supported_features.encrypted_directories)
1064 if (ctx->supported_features.encrypted_files)
1070 return ref_stream(strm, dentry, ctx);
1075 dentry_ref_streams(struct wim_dentry *dentry, struct apply_ctx *ctx)
1077 struct wim_inode *inode = dentry->d_inode;
1078 for (unsigned i = 0; i < inode->i_num_streams; i++) {
1079 int ret = ref_stream_if_needed(dentry, inode,
1080 &inode->i_streams[i], ctx);
1084 inode->i_visited = 1;
1089 * Given a list of dentries to be extracted, build the list of blobs that need
1090 * to be extracted, and for each blob determine the streams to which that blob
1091 * will be extracted.
1093 * This also initializes the extract progress info with byte and blob
1096 * ctx->supported_features must be filled in.
1099 dentry_list_ref_streams(struct list_head *dentry_list, struct apply_ctx *ctx)
1101 struct wim_dentry *dentry;
1104 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
1105 ret = dentry_ref_streams(dentry, ctx);
1109 list_for_each_entry(dentry, dentry_list, d_extraction_list_node)
1110 dentry->d_inode->i_visited = 0;
1115 dentry_list_build_inode_alias_lists(struct list_head *dentry_list)
1117 struct wim_dentry *dentry;
1119 list_for_each_entry(dentry, dentry_list, d_extraction_list_node)
1120 dentry->d_inode->i_first_extraction_alias = NULL;
1122 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
1123 dentry->d_next_extraction_alias = dentry->d_inode->i_first_extraction_alias;
1124 dentry->d_inode->i_first_extraction_alias = dentry;
1129 inode_tally_features(const struct wim_inode *inode,
1130 struct wim_features *features)
1132 if (inode->i_attributes & FILE_ATTRIBUTE_READONLY)
1133 features->readonly_files++;
1134 if (inode->i_attributes & FILE_ATTRIBUTE_HIDDEN)
1135 features->hidden_files++;
1136 if (inode->i_attributes & FILE_ATTRIBUTE_SYSTEM)
1137 features->system_files++;
1138 if (inode->i_attributes & FILE_ATTRIBUTE_ARCHIVE)
1139 features->archive_files++;
1140 if (inode->i_attributes & FILE_ATTRIBUTE_COMPRESSED)
1141 features->compressed_files++;
1142 if (inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED) {
1143 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)
1144 features->encrypted_directories++;
1146 features->encrypted_files++;
1148 if (inode->i_attributes & FILE_ATTRIBUTE_NOT_CONTENT_INDEXED)
1149 features->not_context_indexed_files++;
1150 if (inode->i_attributes & FILE_ATTRIBUTE_SPARSE_FILE)
1151 features->sparse_files++;
1152 if (inode_has_named_data_stream(inode))
1153 features->named_data_streams++;
1154 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1155 features->reparse_points++;
1156 if (inode_is_symlink(inode))
1157 features->symlink_reparse_points++;
1159 features->other_reparse_points++;
1161 if (inode_has_security_descriptor(inode))
1162 features->security_descriptors++;
1163 if (inode_has_unix_data(inode))
1164 features->unix_data++;
1167 /* Tally features necessary to extract a dentry and the corresponding inode. */
1169 dentry_tally_features(struct wim_dentry *dentry, struct wim_features *features)
1171 struct wim_inode *inode = dentry->d_inode;
1173 if (dentry_has_short_name(dentry))
1174 features->short_names++;
1176 if (inode->i_visited) {
1177 features->hard_links++;
1179 inode_tally_features(inode, features);
1180 inode->i_visited = 1;
1184 /* Tally the features necessary to extract the specified dentries. */
1186 dentry_list_get_features(struct list_head *dentry_list,
1187 struct wim_features *features)
1189 struct wim_dentry *dentry;
1191 list_for_each_entry(dentry, dentry_list, d_extraction_list_node)
1192 dentry_tally_features(dentry, features);
1194 list_for_each_entry(dentry, dentry_list, d_extraction_list_node)
1195 dentry->d_inode->i_visited = 0;
1199 do_feature_check(const struct wim_features *required_features,
1200 const struct wim_features *supported_features,
1203 /* Encrypted files. */
1204 if (required_features->encrypted_files &&
1205 !supported_features->encrypted_files)
1206 WARNING("Ignoring EFS-encrypted data of %lu files",
1207 required_features->encrypted_files);
1209 /* Named data streams. */
1210 if (required_features->named_data_streams &&
1211 !supported_features->named_data_streams)
1212 WARNING("Ignoring named data streams of %lu files",
1213 required_features->named_data_streams);
1215 /* File attributes. */
1216 if (!(extract_flags & WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES)) {
1218 if (required_features->readonly_files &&
1219 !supported_features->readonly_files)
1220 WARNING("Ignoring FILE_ATTRIBUTE_READONLY of %lu files",
1221 required_features->readonly_files);
1223 if (required_features->hidden_files &&
1224 !supported_features->hidden_files)
1225 WARNING("Ignoring FILE_ATTRIBUTE_HIDDEN of %lu files",
1226 required_features->hidden_files);
1228 if (required_features->system_files &&
1229 !supported_features->system_files)
1230 WARNING("Ignoring FILE_ATTRIBUTE_SYSTEM of %lu files",
1231 required_features->system_files);
1233 /* Note: Don't bother the user about FILE_ATTRIBUTE_ARCHIVE.
1234 * We're an archive program, so theoretically we can do what we
1237 if (required_features->compressed_files &&
1238 !supported_features->compressed_files)
1239 WARNING("Ignoring FILE_ATTRIBUTE_COMPRESSED of %lu files",
1240 required_features->compressed_files);
1242 if (required_features->not_context_indexed_files &&
1243 !supported_features->not_context_indexed_files)
1244 WARNING("Ignoring FILE_ATTRIBUTE_NOT_CONTENT_INDEXED of %lu files",
1245 required_features->not_context_indexed_files);
1247 if (required_features->sparse_files &&
1248 !supported_features->sparse_files)
1249 WARNING("Ignoring FILE_ATTRIBUTE_SPARSE_FILE of %lu files",
1250 required_features->sparse_files);
1252 if (required_features->encrypted_directories &&
1253 !supported_features->encrypted_directories)
1254 WARNING("Ignoring FILE_ATTRIBUTE_ENCRYPTED of %lu directories",
1255 required_features->encrypted_directories);
1259 if (required_features->hard_links && !supported_features->hard_links)
1260 WARNING("Extracting %lu hard links as independent files",
1261 required_features->hard_links);
1263 /* Symbolic links and reparse points. */
1264 if ((extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS) &&
1265 required_features->symlink_reparse_points &&
1266 !supported_features->symlink_reparse_points &&
1267 !supported_features->reparse_points)
1269 ERROR("Extraction backend does not support symbolic links!");
1270 return WIMLIB_ERR_UNSUPPORTED;
1272 if (required_features->reparse_points &&
1273 !supported_features->reparse_points)
1275 if (supported_features->symlink_reparse_points) {
1276 if (required_features->other_reparse_points) {
1277 WARNING("Ignoring reparse data of %lu non-symlink/junction files",
1278 required_features->other_reparse_points);
1281 WARNING("Ignoring reparse data of %lu files",
1282 required_features->reparse_points);
1286 /* Security descriptors. */
1287 if (((extract_flags & (WIMLIB_EXTRACT_FLAG_STRICT_ACLS |
1288 WIMLIB_EXTRACT_FLAG_UNIX_DATA))
1289 == WIMLIB_EXTRACT_FLAG_STRICT_ACLS) &&
1290 required_features->security_descriptors &&
1291 !supported_features->security_descriptors)
1293 ERROR("Extraction backend does not support security descriptors!");
1294 return WIMLIB_ERR_UNSUPPORTED;
1296 if (!(extract_flags & WIMLIB_EXTRACT_FLAG_NO_ACLS) &&
1297 required_features->security_descriptors &&
1298 !supported_features->security_descriptors)
1299 WARNING("Ignoring Windows NT security descriptors of %lu files",
1300 required_features->security_descriptors);
1303 if ((extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) &&
1304 required_features->unix_data && !supported_features->unix_data)
1306 ERROR("Extraction backend does not support UNIX data!");
1307 return WIMLIB_ERR_UNSUPPORTED;
1310 if (required_features->unix_data &&
1311 !(extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA))
1313 WARNING("Ignoring UNIX metadata of %lu files",
1314 required_features->unix_data);
1318 if (required_features->short_names &&
1319 !supported_features->short_names)
1321 if (extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_SHORT_NAMES) {
1322 ERROR("Extraction backend does not support DOS names!");
1323 return WIMLIB_ERR_UNSUPPORTED;
1325 WARNING("Ignoring DOS names of %lu files",
1326 required_features->short_names);
1330 if ((extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_TIMESTAMPS) &&
1331 !supported_features->timestamps)
1333 ERROR("Extraction backend does not support timestamps!");
1334 return WIMLIB_ERR_UNSUPPORTED;
1340 static const struct apply_operations *
1341 select_apply_operations(int extract_flags)
1344 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS)
1345 return &ntfs_3g_apply_ops;
1348 return &win32_apply_ops;
1350 return &unix_apply_ops;
1355 extract_trees(WIMStruct *wim, struct wim_dentry **trees, size_t num_trees,
1356 const tchar *target, int extract_flags)
1358 const struct apply_operations *ops;
1359 struct apply_ctx *ctx;
1361 LIST_HEAD(dentry_list);
1363 if (extract_flags & WIMLIB_EXTRACT_FLAG_TO_STDOUT) {
1364 ret = extract_dentries_to_stdout(trees, num_trees,
1369 num_trees = remove_duplicate_trees(trees, num_trees);
1370 num_trees = remove_contained_trees(trees, num_trees);
1372 ops = select_apply_operations(extract_flags);
1374 if (num_trees > 1 && ops->single_tree_only) {
1375 ERROR("Extracting multiple directory trees "
1376 "at once is not supported in %s extraction mode!",
1378 ret = WIMLIB_ERR_UNSUPPORTED;
1382 ctx = CALLOC(1, ops->context_size);
1384 ret = WIMLIB_ERR_NOMEM;
1389 ctx->target = target;
1390 ctx->target_nchars = tstrlen(target);
1391 ctx->extract_flags = extract_flags;
1392 if (ctx->wim->progfunc) {
1393 ctx->progfunc = ctx->wim->progfunc;
1394 ctx->progctx = ctx->wim->progctx;
1395 ctx->progress.extract.image = wim->current_image;
1396 ctx->progress.extract.extract_flags = (extract_flags &
1397 WIMLIB_EXTRACT_MASK_PUBLIC);
1398 ctx->progress.extract.wimfile_name = wim->filename;
1399 ctx->progress.extract.image_name = wimlib_get_image_name(wim,
1400 wim->current_image);
1401 ctx->progress.extract.target = target;
1403 INIT_LIST_HEAD(&ctx->blob_list);
1404 filedes_invalidate(&ctx->tmpfile_fd);
1405 ctx->apply_ops = ops;
1407 ret = (*ops->get_supported_features)(target, &ctx->supported_features);
1411 build_dentry_list(&dentry_list, trees, num_trees,
1413 WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE));
1415 dentry_list_get_features(&dentry_list, &ctx->required_features);
1417 ret = do_feature_check(&ctx->required_features, &ctx->supported_features,
1418 ctx->extract_flags);
1422 ret = dentry_list_calculate_extraction_names(&dentry_list, ctx);
1426 if (unlikely(list_empty(&dentry_list))) {
1427 WARNING("There is nothing to extract!");
1431 ret = dentry_list_resolve_streams(&dentry_list, ctx);
1435 dentry_list_build_inode_alias_lists(&dentry_list);
1437 ret = dentry_list_ref_streams(&dentry_list, ctx);
1441 if (extract_flags & WIMLIB_EXTRACT_FLAG_FROM_PIPE) {
1442 /* When extracting from a pipe, the number of bytes of data to
1443 * extract can't be determined in the normal way (examining the
1444 * blob table), since at this point all we have is a set of
1445 * SHA-1 message digests of blobs that need to be extracted.
1446 * However, we can get a reasonably accurate estimate by taking
1447 * <TOTALBYTES> from the corresponding <IMAGE> in the WIM XML
1448 * data. This does assume that a full image is being extracted,
1449 * but currently there is no API for doing otherwise. (Also,
1450 * subtract <HARDLINKBYTES> from this if hard links are
1451 * supported by the extraction mode.) */
1452 ctx->progress.extract.total_bytes =
1453 xml_get_image_total_bytes(wim->xml_info,
1454 wim->current_image);
1455 if (ctx->supported_features.hard_links) {
1456 ctx->progress.extract.total_bytes -=
1457 xml_get_image_hard_link_bytes(wim->xml_info,
1458 wim->current_image);
1462 ret = extract_progress(ctx,
1463 ((extract_flags & WIMLIB_EXTRACT_FLAG_IMAGEMODE) ?
1464 WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN :
1465 WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN));
1469 ret = (*ops->extract)(&dentry_list, ctx);
1473 if (ctx->progress.extract.completed_bytes <
1474 ctx->progress.extract.total_bytes)
1476 ctx->progress.extract.completed_bytes =
1477 ctx->progress.extract.total_bytes;
1478 ret = extract_progress(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS);
1483 ret = extract_progress(ctx,
1484 ((extract_flags & WIMLIB_EXTRACT_FLAG_IMAGEMODE) ?
1485 WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END :
1486 WIMLIB_PROGRESS_MSG_EXTRACT_TREE_END));
1488 destroy_blob_list(&ctx->blob_list);
1489 destroy_dentry_list(&dentry_list);
1496 mkdir_if_needed(const tchar *target)
1498 if (!tmkdir(target, 0755))
1501 if (errno == EEXIST)
1505 /* _wmkdir() fails with EACCES if called on a drive root directory. */
1506 if (errno == EACCES)
1510 ERROR_WITH_ERRNO("Failed to create directory \"%"TS"\"", target);
1511 return WIMLIB_ERR_MKDIR;
1514 /* Make sure the extraction flags make sense, and update them if needed. */
1516 check_extract_flags(const WIMStruct *wim, int *extract_flags_p)
1518 int extract_flags = *extract_flags_p;
1520 /* Check for invalid flag combinations */
1522 if ((extract_flags &
1523 (WIMLIB_EXTRACT_FLAG_NO_ACLS |
1524 WIMLIB_EXTRACT_FLAG_STRICT_ACLS)) == (WIMLIB_EXTRACT_FLAG_NO_ACLS |
1525 WIMLIB_EXTRACT_FLAG_STRICT_ACLS))
1526 return WIMLIB_ERR_INVALID_PARAM;
1528 if ((extract_flags &
1529 (WIMLIB_EXTRACT_FLAG_RPFIX |
1530 WIMLIB_EXTRACT_FLAG_NORPFIX)) == (WIMLIB_EXTRACT_FLAG_RPFIX |
1531 WIMLIB_EXTRACT_FLAG_NORPFIX))
1532 return WIMLIB_ERR_INVALID_PARAM;
1534 #ifndef WITH_NTFS_3G
1535 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1536 ERROR("wimlib was compiled without support for NTFS-3g, so\n"
1537 " it cannot apply a WIM image directly to an NTFS volume.");
1538 return WIMLIB_ERR_UNSUPPORTED;
1542 if (extract_flags & WIMLIB_EXTRACT_FLAG_WIMBOOT) {
1545 return WIMLIB_ERR_NO_FILENAME;
1547 ERROR("WIMBoot extraction is only supported on Windows!");
1548 return WIMLIB_ERR_UNSUPPORTED;
1552 if (extract_flags & (WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS4K |
1553 WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS8K |
1554 WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS16K |
1555 WIMLIB_EXTRACT_FLAG_COMPACT_LZX))
1559 count += ((extract_flags & WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS4K) != 0);
1560 count += ((extract_flags & WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS8K) != 0);
1561 count += ((extract_flags & WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS16K) != 0);
1562 count += ((extract_flags & WIMLIB_EXTRACT_FLAG_COMPACT_LZX) != 0);
1564 ERROR("Only one compression format can be specified "
1565 "for compact-mode extraction!");
1566 return WIMLIB_ERR_INVALID_PARAM;
1568 if (extract_flags & WIMLIB_EXTRACT_FLAG_WIMBOOT) {
1569 ERROR("Compact-mode extraction and WIMBoot-mode "
1570 "extraction are mutually exclusive!");
1571 return WIMLIB_ERR_INVALID_PARAM;
1574 ERROR("Compact-mode extraction (System Compression) "
1575 "is only supported on Windows!");
1576 return WIMLIB_ERR_UNSUPPORTED;
1581 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_RPFIX |
1582 WIMLIB_EXTRACT_FLAG_NORPFIX |
1583 WIMLIB_EXTRACT_FLAG_IMAGEMODE)) ==
1584 WIMLIB_EXTRACT_FLAG_IMAGEMODE)
1586 /* For full-image extraction, do reparse point fixups by default
1587 * if the WIM header says they are enabled. */
1588 if (wim->hdr.flags & WIM_HDR_FLAG_RP_FIX)
1589 extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
1592 *extract_flags_p = extract_flags;
1596 struct append_dentry_ctx {
1597 struct wim_dentry **dentries;
1598 size_t num_dentries;
1599 size_t num_alloc_dentries;
1603 append_dentry_cb(struct wim_dentry *dentry, void *_ctx)
1605 struct append_dentry_ctx *ctx = _ctx;
1607 if (ctx->num_dentries == ctx->num_alloc_dentries) {
1608 struct wim_dentry **new_dentries;
1611 new_length = max(ctx->num_alloc_dentries + 8,
1612 ctx->num_alloc_dentries * 3 / 2);
1613 new_dentries = REALLOC(ctx->dentries,
1614 new_length * sizeof(ctx->dentries[0]));
1615 if (new_dentries == NULL)
1616 return WIMLIB_ERR_NOMEM;
1617 ctx->dentries = new_dentries;
1618 ctx->num_alloc_dentries = new_length;
1620 ctx->dentries[ctx->num_dentries++] = dentry;
1624 /* Append dentries matched by a path which can contain wildcard characters. */
1626 append_matched_dentries(WIMStruct *wim, const tchar *orig_pattern,
1627 int extract_flags, struct append_dentry_ctx *ctx)
1629 const size_t count_before = ctx->num_dentries;
1633 pattern = canonicalize_wim_path(orig_pattern);
1635 return WIMLIB_ERR_NOMEM;
1636 ret = expand_path_pattern(wim_get_current_root_dentry(wim), pattern,
1637 append_dentry_cb, ctx);
1639 if (ret || ctx->num_dentries > count_before)
1641 if (extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_GLOB) {
1642 ERROR("No matches for path pattern \"%"TS"\"", orig_pattern);
1643 return WIMLIB_ERR_PATH_DOES_NOT_EXIST;
1645 WARNING("No matches for path pattern \"%"TS"\"", orig_pattern);
1650 do_wimlib_extract_paths(WIMStruct *wim, int image, const tchar *target,
1651 const tchar * const *paths, size_t num_paths,
1655 struct wim_dentry **trees;
1658 if (wim == NULL || target == NULL || target[0] == T('\0') ||
1659 (num_paths != 0 && paths == NULL))
1660 return WIMLIB_ERR_INVALID_PARAM;
1662 ret = check_extract_flags(wim, &extract_flags);
1666 ret = select_wim_image(wim, image);
1670 ret = wim_checksum_unhashed_blobs(wim);
1674 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_NTFS |
1675 WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE)) ==
1676 (WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE))
1678 ret = mkdir_if_needed(target);
1683 if (extract_flags & WIMLIB_EXTRACT_FLAG_GLOB_PATHS) {
1685 struct append_dentry_ctx append_dentry_ctx = {
1688 .num_alloc_dentries = 0,
1691 for (size_t i = 0; i < num_paths; i++) {
1692 ret = append_matched_dentries(wim, paths[i],
1694 &append_dentry_ctx);
1696 trees = append_dentry_ctx.dentries;
1697 goto out_free_trees;
1700 trees = append_dentry_ctx.dentries;
1701 num_trees = append_dentry_ctx.num_dentries;
1703 trees = MALLOC(num_paths * sizeof(trees[0]));
1705 return WIMLIB_ERR_NOMEM;
1707 for (size_t i = 0; i < num_paths; i++) {
1709 tchar *path = canonicalize_wim_path(paths[i]);
1711 ret = WIMLIB_ERR_NOMEM;
1712 goto out_free_trees;
1715 trees[i] = get_dentry(wim, path,
1716 WIMLIB_CASE_PLATFORM_DEFAULT);
1718 if (trees[i] == NULL) {
1719 ERROR("Path \"%"TS"\" does not exist "
1721 paths[i], wim->current_image);
1722 ret = WIMLIB_ERR_PATH_DOES_NOT_EXIST;
1723 goto out_free_trees;
1726 num_trees = num_paths;
1729 if (num_trees == 0) {
1731 goto out_free_trees;
1734 ret = extract_trees(wim, trees, num_trees, target, extract_flags);
1741 extract_single_image(WIMStruct *wim, int image,
1742 const tchar *target, int extract_flags)
1744 const tchar *path = WIMLIB_WIM_ROOT_PATH;
1745 extract_flags |= WIMLIB_EXTRACT_FLAG_IMAGEMODE;
1746 return do_wimlib_extract_paths(wim, image, target, &path, 1, extract_flags);
1749 static const tchar * const filename_forbidden_chars =
1758 /* This function checks if it is okay to use a WIM image's name as a directory
1761 image_name_ok_as_dir(const tchar *image_name)
1763 return image_name && *image_name &&
1764 !tstrpbrk(image_name, filename_forbidden_chars) &&
1765 tstrcmp(image_name, T(".")) &&
1766 tstrcmp(image_name, T("..")) &&
1767 tstrlen(image_name) <= 128;
1770 /* Extracts all images from the WIM to the directory @target, with the images
1771 * placed in subdirectories named by their image names. */
1773 extract_all_images(WIMStruct *wim, const tchar *target, int extract_flags)
1775 size_t output_path_len = tstrlen(target);
1776 tchar buf[output_path_len + 1 + 128 + 1];
1779 const tchar *image_name;
1781 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1782 ERROR("Cannot extract multiple images in NTFS extraction mode.");
1783 return WIMLIB_ERR_INVALID_PARAM;
1786 ret = mkdir_if_needed(target);
1789 tmemcpy(buf, target, output_path_len);
1790 buf[output_path_len] = OS_PREFERRED_PATH_SEPARATOR;
1791 for (image = 1; image <= wim->hdr.image_count; image++) {
1792 image_name = wimlib_get_image_name(wim, image);
1793 if (image_name_ok_as_dir(image_name)) {
1794 tstrcpy(buf + output_path_len + 1, image_name);
1796 /* Image name is empty or contains forbidden characters.
1797 * Use image number instead. */
1798 tsprintf(buf + output_path_len + 1, T("%d"), image);
1800 ret = extract_single_image(wim, image, buf, extract_flags);
1808 do_wimlib_extract_image(WIMStruct *wim, int image, const tchar *target,
1811 if (extract_flags & (WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE |
1812 WIMLIB_EXTRACT_FLAG_TO_STDOUT |
1813 WIMLIB_EXTRACT_FLAG_GLOB_PATHS))
1814 return WIMLIB_ERR_INVALID_PARAM;
1816 if (image == WIMLIB_ALL_IMAGES)
1817 return extract_all_images(wim, target, extract_flags);
1819 return extract_single_image(wim, image, target, extract_flags);
1823 /****************************************************************************
1825 ****************************************************************************/
1828 wimlib_extract_paths(WIMStruct *wim, int image, const tchar *target,
1829 const tchar * const *paths, size_t num_paths,
1832 if (extract_flags & ~WIMLIB_EXTRACT_MASK_PUBLIC)
1833 return WIMLIB_ERR_INVALID_PARAM;
1835 return do_wimlib_extract_paths(wim, image, target, paths, num_paths,
1840 wimlib_extract_pathlist(WIMStruct *wim, int image, const tchar *target,
1841 const tchar *path_list_file, int extract_flags)
1848 ret = read_path_list_file(path_list_file, &paths, &num_paths, &mem);
1850 ERROR("Failed to read path list file \"%"TS"\"",
1855 ret = wimlib_extract_paths(wim, image, target,
1856 (const tchar * const *)paths, num_paths,
1864 wimlib_extract_image_from_pipe_with_progress(int pipe_fd,
1865 const tchar *image_num_or_name,
1866 const tchar *target,
1868 wimlib_progress_func_t progfunc,
1873 struct filedes *in_fd;
1877 if (extract_flags & ~WIMLIB_EXTRACT_MASK_PUBLIC)
1878 return WIMLIB_ERR_INVALID_PARAM;
1880 /* Read the WIM header from the pipe and get a WIMStruct to represent
1881 * the pipable WIM. Caveats: Unlike getting a WIMStruct with
1882 * wimlib_open_wim(), getting a WIMStruct in this way will result in an
1883 * empty blob table, no XML data read, and no filename set. */
1884 ret = open_wim_as_WIMStruct(&pipe_fd, WIMLIB_OPEN_FLAG_FROM_PIPE, &pwm,
1889 /* Sanity check to make sure this is a pipable WIM. */
1890 if (pwm->hdr.magic != PWM_MAGIC) {
1891 ERROR("The WIM being read from file descriptor %d "
1892 "is not pipable!", pipe_fd);
1893 ret = WIMLIB_ERR_NOT_PIPABLE;
1894 goto out_wimlib_free;
1897 /* Sanity check to make sure the first part of a pipable split WIM is
1898 * sent over the pipe first. */
1899 if (pwm->hdr.part_number != 1) {
1900 ERROR("The first part of the split WIM must be "
1901 "sent over the pipe first.");
1902 ret = WIMLIB_ERR_INVALID_PIPABLE_WIM;
1903 goto out_wimlib_free;
1906 in_fd = &pwm->in_fd;
1907 wimlib_assert(in_fd->offset == WIM_HEADER_DISK_SIZE);
1909 /* As mentioned, the WIMStruct we created from the pipe does not have
1910 * XML data yet. Fix this by reading the extra copy of the XML data
1911 * that directly follows the header in pipable WIMs. (Note: see
1912 * write_pipable_wim() for more details about the format of pipable
1915 u8 hash[SHA1_HASH_SIZE];
1917 ret = read_pwm_blob_header(pwm, hash,
1918 &pwm->hdr.xml_data_reshdr, NULL);
1920 goto out_wimlib_free;
1922 if (!(pwm->hdr.xml_data_reshdr.flags & WIM_RESHDR_FLAG_METADATA)) {
1923 ERROR("Expected XML data, but found non-metadata resource.");
1924 ret = WIMLIB_ERR_INVALID_PIPABLE_WIM;
1925 goto out_wimlib_free;
1928 ret = read_wim_xml_data(pwm);
1930 goto out_wimlib_free;
1932 if (xml_get_image_count(pwm->xml_info) != pwm->hdr.image_count) {
1933 ERROR("Image count in XML data is not the same as in WIM header.");
1934 ret = WIMLIB_ERR_IMAGE_COUNT;
1935 goto out_wimlib_free;
1939 /* Get image index (this may use the XML data that was just read to
1940 * resolve an image name). */
1941 if (image_num_or_name) {
1942 image = wimlib_resolve_image(pwm, image_num_or_name);
1943 if (image == WIMLIB_NO_IMAGE) {
1944 ERROR("\"%"TS"\" is not a valid image in the pipable WIM!",
1946 ret = WIMLIB_ERR_INVALID_IMAGE;
1947 goto out_wimlib_free;
1948 } else if (image == WIMLIB_ALL_IMAGES) {
1949 ERROR("Applying all images from a pipe is not supported!");
1950 ret = WIMLIB_ERR_INVALID_IMAGE;
1951 goto out_wimlib_free;
1954 if (pwm->hdr.image_count != 1) {
1955 ERROR("No image was specified, but the pipable WIM "
1956 "did not contain exactly 1 image");
1957 ret = WIMLIB_ERR_INVALID_IMAGE;
1958 goto out_wimlib_free;
1963 /* Load the needed metadata resource. */
1964 for (i = 1; i <= pwm->hdr.image_count; i++) {
1965 ret = handle_pwm_metadata_resource(pwm, i, i == image);
1967 goto out_wimlib_free;
1969 /* Extract the image. */
1970 extract_flags |= WIMLIB_EXTRACT_FLAG_FROM_PIPE;
1971 ret = do_wimlib_extract_image(pwm, image, target, extract_flags);
1972 /* Clean up and return. */
1980 wimlib_extract_image_from_pipe(int pipe_fd, const tchar *image_num_or_name,
1981 const tchar *target, int extract_flags)
1983 return wimlib_extract_image_from_pipe_with_progress(pipe_fd,
1992 wimlib_extract_image(WIMStruct *wim, int image, const tchar *target,
1995 if (extract_flags & ~WIMLIB_EXTRACT_MASK_PUBLIC)
1996 return WIMLIB_ERR_INVALID_PARAM;
1997 return do_wimlib_extract_image(wim, image, target, extract_flags);