4 * Support for extracting WIM images, or files or directories contained in a WIM
9 * Copyright (C) 2012-2017 Eric Biggers
11 * This file is free software; you can redistribute it and/or modify it under
12 * the terms of the GNU Lesser General Public License as published by the Free
13 * Software Foundation; either version 3 of the License, or (at your option) any
16 * This file is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this file; if not, see http://www.gnu.org/licenses/.
26 * This file provides the API functions wimlib_extract_image(),
27 * wimlib_extract_image_from_pipe(), wimlib_extract_paths(), and
28 * wimlib_extract_pathlist(). Internally, all end up calling
29 * do_wimlib_extract_paths() and extract_trees().
31 * Although wimlib supports multiple extraction modes/backends (NTFS-3G, UNIX,
32 * Win32), this file does not itself have code to extract files or directories
33 * to any specific target; instead, it handles generic functionality and relies
34 * on lower-level callback functions declared in `struct apply_operations' to do
35 * the actual extraction.
48 #include "wimlib/apply.h"
49 #include "wimlib/assert.h"
50 #include "wimlib/blob_table.h"
51 #include "wimlib/dentry.h"
52 #include "wimlib/encoding.h"
53 #include "wimlib/endianness.h"
54 #include "wimlib/error.h"
55 #include "wimlib/metadata.h"
56 #include "wimlib/object_id.h"
57 #include "wimlib/pathlist.h"
58 #include "wimlib/paths.h"
59 #include "wimlib/pattern.h"
60 #include "wimlib/reparse.h"
61 #include "wimlib/resource.h"
62 #include "wimlib/security.h"
63 #include "wimlib/unix_data.h"
64 #include "wimlib/wim.h"
65 #include "wimlib/win32.h" /* for realpath() equivalent */
66 #include "wimlib/xattr.h"
67 #include "wimlib/xml.h"
69 #define WIMLIB_EXTRACT_FLAG_FROM_PIPE 0x80000000
70 #define WIMLIB_EXTRACT_FLAG_IMAGEMODE 0x40000000
72 /* Keep in sync with wimlib.h */
73 #define WIMLIB_EXTRACT_MASK_PUBLIC \
74 (WIMLIB_EXTRACT_FLAG_NTFS | \
75 WIMLIB_EXTRACT_FLAG_UNIX_DATA | \
76 WIMLIB_EXTRACT_FLAG_NO_ACLS | \
77 WIMLIB_EXTRACT_FLAG_STRICT_ACLS | \
78 WIMLIB_EXTRACT_FLAG_RPFIX | \
79 WIMLIB_EXTRACT_FLAG_NORPFIX | \
80 WIMLIB_EXTRACT_FLAG_TO_STDOUT | \
81 WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES | \
82 WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS | \
83 WIMLIB_EXTRACT_FLAG_ALL_DATA_BLOBS | \
84 WIMLIB_EXTRACT_FLAG_ALL_METADATA_BLOBS | \
85 WIMLIB_EXTRACT_FLAG_STRICT_TIMESTAMPS | \
86 WIMLIB_EXTRACT_FLAG_STRICT_SHORT_NAMES | \
87 WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS | \
88 WIMLIB_EXTRACT_FLAG_GLOB_PATHS | \
89 WIMLIB_EXTRACT_FLAG_STRICT_GLOB | \
90 WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES | \
91 WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE | \
92 WIMLIB_EXTRACT_FLAG_WIMBOOT | \
93 WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS4K | \
94 WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS8K | \
95 WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS16K | \
96 WIMLIB_EXTRACT_FLAG_COMPACT_LZX \
99 /* Send WIMLIB_PROGRESS_MSG_EXTRACT_FILE_STRUCTURE or
100 * WIMLIB_PROGRESS_MSG_EXTRACT_METADATA. */
102 do_file_extract_progress(struct apply_ctx *ctx, enum wimlib_progress_msg msg)
104 ctx->count_until_file_progress = 500; /* Arbitrary value to limit calls */
105 return extract_progress(ctx, msg);
109 start_file_phase(struct apply_ctx *ctx, u64 end_file_count, enum wimlib_progress_msg msg)
111 ctx->progress.extract.current_file_count = 0;
112 ctx->progress.extract.end_file_count = end_file_count;
113 return do_file_extract_progress(ctx, msg);
117 start_file_structure_phase(struct apply_ctx *ctx, u64 end_file_count)
119 return start_file_phase(ctx, end_file_count, WIMLIB_PROGRESS_MSG_EXTRACT_FILE_STRUCTURE);
123 start_file_metadata_phase(struct apply_ctx *ctx, u64 end_file_count)
125 return start_file_phase(ctx, end_file_count, WIMLIB_PROGRESS_MSG_EXTRACT_METADATA);
129 end_file_phase(struct apply_ctx *ctx, enum wimlib_progress_msg msg)
131 ctx->progress.extract.current_file_count = ctx->progress.extract.end_file_count;
132 return do_file_extract_progress(ctx, msg);
136 end_file_structure_phase(struct apply_ctx *ctx)
138 return end_file_phase(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_FILE_STRUCTURE);
142 end_file_metadata_phase(struct apply_ctx *ctx)
144 return end_file_phase(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_METADATA);
147 /* Are all bytes in the specified buffer zero? */
149 is_all_zeroes(const u8 *p, const size_t size)
151 const u8 * const end = p + size;
153 for (; (uintptr_t)p % WORDBYTES && p != end; p++)
157 for (; end - p >= WORDBYTES; p += WORDBYTES)
158 if (*(const machine_word_t *)p)
161 for (; p != end; p++)
169 * Sparse regions should be detected at the granularity of the filesystem block
170 * size. For now just assume 4096 bytes, which is the default block size on
171 * NTFS and most Linux filesystems.
173 #define SPARSE_UNIT 4096
176 * Detect whether the specified buffer begins with a region of all zero bytes.
177 * Return %true if a zero region was found or %false if a nonzero region was
178 * found, and sets *len_ret to the length of the region. This operates at a
179 * granularity of SPARSE_UNIT bytes, meaning that to extend a zero region, there
180 * must be SPARSE_UNIT zero bytes with no interruption, but to extend a nonzero
181 * region, just one nonzero byte in the next SPARSE_UNIT bytes is sufficient.
183 * Note: besides compression, the WIM format doesn't yet have a way to
184 * efficiently represent zero regions, so that's why we need to detect them
185 * ourselves. Things will still fall apart badly on extremely large sparse
186 * files, but this is a start...
189 detect_sparse_region(const void *data, size_t size, size_t *len_ret)
191 const void *p = data;
192 const void * const end = data + size;
197 size_t n = min(end - p, SPARSE_UNIT);
198 bool z = is_all_zeroes(p, n);
200 if (len != 0 && z != zeroes)
211 #define PWM_FOUND_WIM_HDR (-1)
213 /* Read the header for a blob in a pipable WIM. If @pwm_hdr_ret is not NULL,
214 * also look for a pipable WIM header and return PWM_FOUND_WIM_HDR if found. */
216 read_pwm_blob_header(WIMStruct *pwm, u8 hash_ret[SHA1_HASH_SIZE],
217 struct wim_reshdr *reshdr_ret,
218 struct wim_header_disk *pwm_hdr_ret)
221 struct pwm_blob_hdr blob_hdr;
224 ret = full_read(&pwm->in_fd, &blob_hdr, sizeof(blob_hdr));
228 magic = le64_to_cpu(blob_hdr.magic);
230 if (magic == PWM_MAGIC && pwm_hdr_ret != NULL) {
231 memcpy(pwm_hdr_ret, &blob_hdr, sizeof(blob_hdr));
232 ret = full_read(&pwm->in_fd,
233 (u8 *)pwm_hdr_ret + sizeof(blob_hdr),
234 sizeof(*pwm_hdr_ret) - sizeof(blob_hdr));
237 return PWM_FOUND_WIM_HDR;
240 if (unlikely(magic != PWM_BLOB_MAGIC)) {
241 ERROR("Data read on pipe is invalid (expected blob header)");
242 return WIMLIB_ERR_INVALID_PIPABLE_WIM;
245 copy_hash(hash_ret, blob_hdr.hash);
247 reshdr_ret->size_in_wim = 0; /* Not available */
248 reshdr_ret->flags = le32_to_cpu(blob_hdr.flags);
249 reshdr_ret->offset_in_wim = pwm->in_fd.offset;
250 reshdr_ret->uncompressed_size = le64_to_cpu(blob_hdr.uncompressed_size);
252 if (unlikely(reshdr_ret->uncompressed_size == 0)) {
253 ERROR("Data read on pipe is invalid (resource is of 0 size)");
254 return WIMLIB_ERR_INVALID_PIPABLE_WIM;
260 if (ret == WIMLIB_ERR_UNEXPECTED_END_OF_FILE)
261 ERROR("The pipe ended before all needed data was sent!");
263 ERROR_WITH_ERRNO("Error reading pipable WIM from pipe");
268 read_blobs_from_pipe(struct apply_ctx *ctx, const struct read_blob_callbacks *cbs)
271 u8 hash[SHA1_HASH_SIZE];
272 struct wim_reshdr reshdr;
273 struct wim_header_disk pwm_hdr;
274 struct wim_resource_descriptor rdesc;
275 struct blob_descriptor *blob;
277 copy_guid(ctx->progress.extract.guid, ctx->wim->hdr.guid);
278 ctx->progress.extract.part_number = ctx->wim->hdr.part_number;
279 ctx->progress.extract.total_parts = ctx->wim->hdr.total_parts;
280 ret = extract_progress(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN);
284 while (ctx->num_blobs_remaining) {
286 ret = read_pwm_blob_header(ctx->wim, hash, &reshdr, &pwm_hdr);
288 if (ret == PWM_FOUND_WIM_HDR) {
289 u16 part_number = le16_to_cpu(pwm_hdr.part_number);
290 u16 total_parts = le16_to_cpu(pwm_hdr.total_parts);
292 if (part_number == ctx->progress.extract.part_number &&
293 total_parts == ctx->progress.extract.total_parts &&
294 guids_equal(pwm_hdr.guid, ctx->progress.extract.guid))
297 copy_guid(ctx->progress.extract.guid, pwm_hdr.guid);
298 ctx->progress.extract.part_number = part_number;
299 ctx->progress.extract.total_parts = total_parts;
300 ret = extract_progress(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN);
310 if (!(reshdr.flags & WIM_RESHDR_FLAG_METADATA)
311 && (blob = lookup_blob(ctx->wim->blob_table, hash))
312 && (blob->out_refcnt))
314 wim_reshdr_to_desc_and_blob(&reshdr, ctx->wim, &rdesc, blob);
315 ret = read_blob_with_sha1(blob, cbs);
316 blob_unset_is_located_in_wim_resource(blob);
319 ctx->num_blobs_remaining--;
321 wim_reshdr_to_desc(&reshdr, ctx->wim, &rdesc);
322 ret = skip_wim_resource(&rdesc);
332 handle_pwm_metadata_resource(WIMStruct *pwm, int image, bool is_needed)
334 struct blob_descriptor *blob;
335 struct wim_reshdr reshdr;
336 struct wim_resource_descriptor *rdesc;
339 ret = WIMLIB_ERR_NOMEM;
340 blob = new_blob_descriptor();
344 ret = read_pwm_blob_header(pwm, blob->hash, &reshdr, NULL);
348 ret = WIMLIB_ERR_INVALID_PIPABLE_WIM;
349 if (!(reshdr.flags & WIM_RESHDR_FLAG_METADATA)) {
350 ERROR("Expected metadata resource, but found non-metadata "
355 ret = WIMLIB_ERR_NOMEM;
356 rdesc = MALLOC(sizeof(*rdesc));
360 wim_reshdr_to_desc_and_blob(&reshdr, pwm, rdesc, blob);
363 ret = WIMLIB_ERR_NOMEM;
364 pwm->image_metadata[image - 1] = new_unloaded_image_metadata(blob);
365 if (!pwm->image_metadata[image - 1])
369 /* If the metadata resource is for the image being extracted, then parse
370 * it and save the metadata in memory. Otherwise, skip over it. */
372 ret = select_wim_image(pwm, image);
374 ret = skip_wim_resource(rdesc);
376 free_blob_descriptor(blob);
380 /* Creates a temporary file opened for writing. The open file descriptor is
381 * returned in @fd_ret and its name is returned in @name_ret (dynamically
384 create_temporary_file(struct filedes *fd_ret, tchar **name_ret)
391 name = _wtempnam(NULL, L"wimlib");
393 ERROR_WITH_ERRNO("Failed to create temporary filename");
394 return WIMLIB_ERR_NOMEM;
396 raw_fd = _wopen(name, O_WRONLY | O_CREAT | O_EXCL | O_BINARY |
397 _O_SHORT_LIVED, 0600);
398 if (raw_fd < 0 && errno == EEXIST) {
402 #else /* __WIN32__ */
403 const char *tmpdir = getenv("TMPDIR");
406 name = MALLOC(strlen(tmpdir) + 1 + 6 + 6 + 1);
408 return WIMLIB_ERR_NOMEM;
409 sprintf(name, "%s/wimlibXXXXXX", tmpdir);
410 raw_fd = mkstemp(name);
411 #endif /* !__WIN32__ */
414 ERROR_WITH_ERRNO("Failed to create temporary file "
417 return WIMLIB_ERR_OPEN;
420 filedes_init(fd_ret, raw_fd);
426 begin_extract_blob(struct blob_descriptor *blob, void *_ctx)
428 struct apply_ctx *ctx = _ctx;
430 if (unlikely(blob->out_refcnt > MAX_OPEN_FILES))
431 return create_temporary_file(&ctx->tmpfile_fd, &ctx->tmpfile_name);
433 return call_begin_blob(blob, ctx->saved_cbs);
437 extract_chunk(const struct blob_descriptor *blob, u64 offset,
438 const void *chunk, size_t size, void *_ctx)
440 struct apply_ctx *ctx = _ctx;
441 union wimlib_progress_info *progress = &ctx->progress;
442 bool last = (offset + size == blob->size);
445 if (likely(ctx->supported_features.hard_links)) {
446 progress->extract.completed_bytes +=
447 (u64)size * blob->out_refcnt;
449 progress->extract.completed_streams += blob->out_refcnt;
451 const struct blob_extraction_target *targets =
452 blob_extraction_targets(blob);
453 for (u32 i = 0; i < blob->out_refcnt; i++) {
454 const struct wim_inode *inode = targets[i].inode;
455 const struct wim_dentry *dentry;
457 inode_for_each_extraction_alias(dentry, inode) {
458 progress->extract.completed_bytes += size;
460 progress->extract.completed_streams++;
464 if (progress->extract.completed_bytes >= ctx->next_progress) {
466 ret = extract_progress(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS);
470 set_next_progress(progress->extract.completed_bytes,
471 progress->extract.total_bytes,
472 &ctx->next_progress);
475 if (unlikely(filedes_valid(&ctx->tmpfile_fd))) {
476 /* Just extracting to temporary file for now. */
477 ret = full_write(&ctx->tmpfile_fd, chunk, size);
479 ERROR_WITH_ERRNO("Error writing data to "
480 "temporary file \"%"TS"\"",
486 return call_continue_blob(blob, offset, chunk, size, ctx->saved_cbs);
489 /* Copy the blob's data from the temporary file to each of its targets.
491 * This is executed only in the very uncommon case that a blob is being
492 * extracted to more than MAX_OPEN_FILES targets! */
494 extract_from_tmpfile(const tchar *tmpfile_name,
495 const struct blob_descriptor *orig_blob,
496 const struct read_blob_callbacks *cbs)
498 struct blob_descriptor tmpfile_blob;
499 const struct blob_extraction_target *targets = blob_extraction_targets(orig_blob);
502 memcpy(&tmpfile_blob, orig_blob, sizeof(struct blob_descriptor));
503 tmpfile_blob.blob_location = BLOB_IN_FILE_ON_DISK;
504 tmpfile_blob.file_on_disk = (tchar *)tmpfile_name;
505 tmpfile_blob.out_refcnt = 1;
507 for (u32 i = 0; i < orig_blob->out_refcnt; i++) {
508 tmpfile_blob.inline_blob_extraction_targets[0] = targets[i];
509 ret = read_blob_with_cbs(&tmpfile_blob, cbs);
517 end_extract_blob(struct blob_descriptor *blob, int status, void *_ctx)
519 struct apply_ctx *ctx = _ctx;
521 if (unlikely(filedes_valid(&ctx->tmpfile_fd))) {
522 filedes_close(&ctx->tmpfile_fd);
524 status = extract_from_tmpfile(ctx->tmpfile_name, blob,
526 filedes_invalidate(&ctx->tmpfile_fd);
527 tunlink(ctx->tmpfile_name);
528 FREE(ctx->tmpfile_name);
532 return call_end_blob(blob, status, ctx->saved_cbs);
536 * Read the list of blobs to extract and feed their data into the specified
537 * callback functions.
539 * This handles checksumming each blob.
541 * This also handles sending WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS.
543 * This also works if the WIM is being read from a pipe.
545 * This also will split up blobs that will need to be extracted to more than
546 * MAX_OPEN_FILES locations, as measured by the 'out_refcnt' of each blob.
547 * Therefore, the apply_operations implementation need not worry about running
548 * out of file descriptors, unless it might open more than one file descriptor
549 * per 'blob_extraction_target' (e.g. Win32 currently might because the
550 * destination file system might not support hard links).
553 extract_blob_list(struct apply_ctx *ctx, const struct read_blob_callbacks *cbs)
555 struct read_blob_callbacks wrapper_cbs = {
556 .begin_blob = begin_extract_blob,
557 .continue_blob = extract_chunk,
558 .end_blob = end_extract_blob,
561 ctx->saved_cbs = cbs;
562 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_FROM_PIPE) {
563 return read_blobs_from_pipe(ctx, &wrapper_cbs);
565 return read_blob_list(&ctx->blob_list,
566 offsetof(struct blob_descriptor,
568 &wrapper_cbs, VERIFY_BLOB_HASHES);
573 extract_blob_to_stdout(struct blob_descriptor *blob)
575 struct filedes _stdout;
577 filedes_init(&_stdout, STDOUT_FILENO);
578 return extract_blob_to_fd(blob, &_stdout);
581 /* Extract a WIM dentry to standard output.
583 * This obviously doesn't make sense in all cases. We return an error if the
584 * dentry does not correspond to a regular file. Otherwise we extract the
585 * unnamed data stream only. */
587 extract_dentry_to_stdout(struct wim_dentry *dentry,
588 const struct blob_table *blob_table)
590 struct wim_inode *inode = dentry->d_inode;
591 struct blob_descriptor *blob;
593 if (inode->i_attributes & (FILE_ATTRIBUTE_REPARSE_POINT |
594 FILE_ATTRIBUTE_DIRECTORY |
595 FILE_ATTRIBUTE_ENCRYPTED))
597 ERROR("\"%"TS"\" is not a regular file and therefore cannot be "
598 "extracted to standard output", dentry_full_path(dentry));
599 return WIMLIB_ERR_NOT_A_REGULAR_FILE;
602 blob = inode_get_blob_for_unnamed_data_stream(inode, blob_table);
604 const u8 *hash = inode_get_hash_of_unnamed_data_stream(inode);
605 if (!is_zero_hash(hash))
606 return blob_not_found_error(inode, hash);
610 return extract_blob_to_stdout(blob);
614 extract_dentries_to_stdout(struct wim_dentry **dentries, size_t num_dentries,
615 const struct blob_table *blob_table)
617 for (size_t i = 0; i < num_dentries; i++) {
618 int ret = extract_dentry_to_stdout(dentries[i], blob_table);
625 /**********************************************************************/
628 * Removes duplicate dentries from the array.
630 * Returns the new number of dentries, packed at the front of the array.
633 remove_duplicate_trees(struct wim_dentry **trees, size_t num_trees)
636 for (i = 0; i < num_trees; i++) {
637 if (!trees[i]->d_tmp_flag) {
638 /* Found distinct dentry. */
639 trees[i]->d_tmp_flag = 1;
640 trees[j++] = trees[i];
643 for (i = 0; i < j; i++)
644 trees[i]->d_tmp_flag = 0;
649 * Remove dentries that are descendants of other dentries in the array.
651 * Returns the new number of dentries, packed at the front of the array.
654 remove_contained_trees(struct wim_dentry **trees, size_t num_trees)
657 for (i = 0; i < num_trees; i++)
658 trees[i]->d_tmp_flag = 1;
659 for (i = 0; i < num_trees; i++) {
660 struct wim_dentry *d = trees[i];
661 while (!dentry_is_root(d)) {
666 trees[j++] = trees[i];
670 trees[i]->d_tmp_flag = 0;
673 for (i = 0; i < j; i++)
674 trees[i]->d_tmp_flag = 0;
679 dentry_append_to_list(struct wim_dentry *dentry, void *_dentry_list)
681 struct list_head *dentry_list = _dentry_list;
682 list_add_tail(&dentry->d_extraction_list_node, dentry_list);
687 dentry_reset_extraction_list_node(struct wim_dentry *dentry)
689 dentry->d_extraction_list_node = (struct list_head){NULL, NULL};
693 dentry_delete_from_list(struct wim_dentry *dentry, void *_ignore)
695 if (will_extract_dentry(dentry)) {
696 list_del(&dentry->d_extraction_list_node);
697 dentry_reset_extraction_list_node(dentry);
703 * Build the preliminary list of dentries to be extracted.
705 * The list maintains the invariant that if d1 and d2 are in the list and d1 is
706 * an ancestor of d2, then d1 appears before d2 in the list.
709 build_dentry_list(struct list_head *dentry_list, struct wim_dentry **trees,
710 size_t num_trees, bool add_ancestors)
712 INIT_LIST_HEAD(dentry_list);
714 /* Add the trees recursively. */
715 for (size_t i = 0; i < num_trees; i++)
716 for_dentry_in_tree(trees[i], dentry_append_to_list, dentry_list);
718 /* If requested, add ancestors of the trees. */
720 for (size_t i = 0; i < num_trees; i++) {
721 struct wim_dentry *dentry = trees[i];
722 struct wim_dentry *ancestor;
723 struct list_head *place_after;
725 if (dentry_is_root(dentry))
728 place_after = dentry_list;
731 ancestor = ancestor->d_parent;
732 if (will_extract_dentry(ancestor)) {
733 place_after = &ancestor->d_extraction_list_node;
736 } while (!dentry_is_root(ancestor));
740 ancestor = ancestor->d_parent;
741 if (will_extract_dentry(ancestor))
743 list_add(&ancestor->d_extraction_list_node, place_after);
744 } while (!dentry_is_root(ancestor));
750 destroy_dentry_list(struct list_head *dentry_list)
752 struct wim_dentry *dentry, *tmp;
753 struct wim_inode *inode;
755 list_for_each_entry_safe(dentry, tmp, dentry_list, d_extraction_list_node) {
756 inode = dentry->d_inode;
757 dentry_reset_extraction_list_node(dentry);
758 inode->i_visited = 0;
759 inode->i_can_externally_back = 0;
760 if ((void *)dentry->d_extraction_name != (void *)dentry->d_name)
761 FREE(dentry->d_extraction_name);
762 dentry->d_extraction_name = NULL;
763 dentry->d_extraction_name_nchars = 0;
768 destroy_blob_list(struct list_head *blob_list)
770 struct blob_descriptor *blob;
772 list_for_each_entry(blob, blob_list, extraction_list)
773 if (blob->out_refcnt > ARRAY_LEN(blob->inline_blob_extraction_targets))
774 FREE(blob->blob_extraction_targets);
778 static const utf16lechar replacement_char = cpu_to_le16(0xfffd);
780 static const utf16lechar replacement_char = cpu_to_le16('?');
784 file_name_valid(utf16lechar *name, size_t num_chars, bool fix)
790 for (i = 0; i < num_chars; i++) {
791 switch (le16_to_cpu(name[i])) {
793 case '\x01'...'\x1F':
806 name[i] = replacement_char;
816 dentry_calculate_extraction_name(struct wim_dentry *dentry,
817 struct apply_ctx *ctx)
821 if (dentry_is_root(dentry))
825 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
826 dentry->d_extraction_name = dentry->d_name;
827 dentry->d_extraction_name_nchars = dentry->d_name_nbytes /
833 if (!ctx->supported_features.case_sensitive_filenames) {
834 struct wim_dentry *other;
835 dentry_for_each_ci_match(other, dentry) {
836 if (will_extract_dentry(other)) {
837 if (ctx->extract_flags &
838 WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS) {
839 WARNING("\"%"TS"\" has the same "
840 "case-insensitive name as "
841 "\"%"TS"\"; extracting "
842 "dummy name instead",
843 dentry_full_path(dentry),
844 dentry_full_path(other));
847 WARNING("Not extracting \"%"TS"\": "
848 "has same case-insensitive "
850 dentry_full_path(dentry),
851 dentry_full_path(other));
858 if (file_name_valid(dentry->d_name, dentry->d_name_nbytes / 2, false)) {
860 ret = utf16le_get_tstr(dentry->d_name,
861 dentry->d_name_nbytes,
862 (const tchar **)&dentry->d_extraction_name,
864 dentry->d_extraction_name_nchars = nbytes / sizeof(tchar);
867 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES)
869 WARNING("\"%"TS"\" has an invalid filename "
870 "that is not supported on this platform; "
871 "extracting dummy name instead",
872 dentry_full_path(dentry));
875 WARNING("Not extracting \"%"TS"\": has an invalid filename "
876 "that is not supported on this platform",
877 dentry_full_path(dentry));
884 utf16lechar utf16_name_copy[dentry->d_name_nbytes / 2];
886 memcpy(utf16_name_copy, dentry->d_name, dentry->d_name_nbytes);
887 file_name_valid(utf16_name_copy, dentry->d_name_nbytes / 2, true);
889 const tchar *tchar_name;
892 ret = utf16le_get_tstr(utf16_name_copy,
893 dentry->d_name_nbytes,
894 &tchar_name, &tchar_nchars);
898 tchar_nchars /= sizeof(tchar);
900 size_t fixed_name_num_chars = tchar_nchars;
901 tchar fixed_name[tchar_nchars + 50];
903 tmemcpy(fixed_name, tchar_name, tchar_nchars);
904 fixed_name_num_chars += tsprintf(fixed_name + tchar_nchars,
905 T(" (invalid filename #%lu)"),
906 ++ctx->invalid_sequence);
908 utf16le_put_tstr(tchar_name);
910 dentry->d_extraction_name = TSTRDUP(fixed_name);
911 if (!dentry->d_extraction_name)
912 return WIMLIB_ERR_NOMEM;
913 dentry->d_extraction_name_nchars = fixed_name_num_chars;
918 for_dentry_in_tree(dentry, dentry_delete_from_list, NULL);
923 * Calculate the actual filename component at which each WIM dentry will be
924 * extracted, with special handling for dentries that are unsupported by the
925 * extraction backend or have invalid names.
927 * ctx->supported_features must be filled in.
929 * Possible error codes: WIMLIB_ERR_NOMEM, WIMLIB_ERR_INVALID_UTF16_STRING
932 dentry_list_calculate_extraction_names(struct list_head *dentry_list,
933 struct apply_ctx *ctx)
935 struct list_head *prev, *cur;
937 /* Can't use list_for_each_entry() because a call to
938 * dentry_calculate_extraction_name() may delete the current dentry and
939 * its children from the list. */
943 struct wim_dentry *dentry;
947 if (cur == dentry_list)
950 dentry = list_entry(cur, struct wim_dentry, d_extraction_list_node);
952 ret = dentry_calculate_extraction_name(dentry, ctx);
956 if (prev->next == cur)
959 ; /* Current dentry and its children (which follow in
960 the list) were deleted. prev stays the same. */
966 dentry_resolve_streams(struct wim_dentry *dentry, int extract_flags,
967 struct blob_table *blob_table)
969 struct wim_inode *inode = dentry->d_inode;
970 struct blob_descriptor *blob;
974 /* Special case: when extracting from a pipe, the WIM blob table is
975 * initially empty, so "resolving" an inode's streams is initially not
976 * possible. However, we still need to keep track of which blobs,
977 * identified by SHA-1 message digests, need to be extracted, so we
978 * "resolve" the inode's streams anyway by allocating a 'struct
979 * blob_descriptor' for each one. */
980 if (extract_flags & WIMLIB_EXTRACT_FLAG_FROM_PIPE)
982 ret = inode_resolve_streams(inode, blob_table, force);
985 for (unsigned i = 0; i < inode->i_num_streams; i++) {
986 blob = stream_blob_resolved(&inode->i_streams[i]);
988 blob->out_refcnt = 0;
994 * For each dentry to be extracted, resolve all streams in the corresponding
995 * inode and set 'out_refcnt' in all referenced blob_descriptors to 0.
997 * Possible error codes: WIMLIB_ERR_RESOURCE_NOT_FOUND, WIMLIB_ERR_NOMEM.
1000 dentry_list_resolve_streams(struct list_head *dentry_list,
1001 struct apply_ctx *ctx)
1003 struct wim_dentry *dentry;
1006 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
1007 ret = dentry_resolve_streams(dentry,
1009 ctx->wim->blob_table);
1017 ref_stream(struct wim_inode_stream *strm, struct wim_dentry *dentry,
1018 struct apply_ctx *ctx)
1020 struct wim_inode *inode = dentry->d_inode;
1021 struct blob_descriptor *blob = stream_blob_resolved(strm);
1022 struct blob_extraction_target *targets;
1027 /* Tally the size only for each actual extraction of the stream (not
1028 * additional hard links to the inode). */
1029 if (inode->i_visited && ctx->supported_features.hard_links)
1032 ctx->progress.extract.total_bytes += blob->size;
1033 ctx->progress.extract.total_streams++;
1035 if (inode->i_visited)
1038 /* Add each blob to 'ctx->blob_list' only one time, regardless of how
1039 * many extraction targets it will have. */
1040 if (blob->out_refcnt == 0) {
1041 list_add_tail(&blob->extraction_list, &ctx->blob_list);
1042 ctx->num_blobs_remaining++;
1045 /* Set this stream as an extraction target of 'blob'. */
1047 if (blob->out_refcnt < ARRAY_LEN(blob->inline_blob_extraction_targets)) {
1048 targets = blob->inline_blob_extraction_targets;
1050 struct blob_extraction_target *prev_targets;
1051 size_t alloc_blob_extraction_targets;
1053 if (blob->out_refcnt == ARRAY_LEN(blob->inline_blob_extraction_targets)) {
1054 prev_targets = NULL;
1055 alloc_blob_extraction_targets = ARRAY_LEN(blob->inline_blob_extraction_targets);
1057 prev_targets = blob->blob_extraction_targets;
1058 alloc_blob_extraction_targets = blob->alloc_blob_extraction_targets;
1061 if (blob->out_refcnt == alloc_blob_extraction_targets) {
1062 alloc_blob_extraction_targets *= 2;
1063 targets = REALLOC(prev_targets,
1064 alloc_blob_extraction_targets *
1065 sizeof(targets[0]));
1067 return WIMLIB_ERR_NOMEM;
1068 if (!prev_targets) {
1070 blob->inline_blob_extraction_targets,
1071 sizeof(blob->inline_blob_extraction_targets));
1073 blob->blob_extraction_targets = targets;
1074 blob->alloc_blob_extraction_targets = alloc_blob_extraction_targets;
1076 targets = blob->blob_extraction_targets;
1078 targets[blob->out_refcnt].inode = inode;
1079 targets[blob->out_refcnt].stream = strm;
1085 ref_stream_if_needed(struct wim_dentry *dentry, struct wim_inode *inode,
1086 struct wim_inode_stream *strm, struct apply_ctx *ctx)
1088 bool need_stream = false;
1089 switch (strm->stream_type) {
1090 case STREAM_TYPE_DATA:
1091 if (stream_is_named(strm)) {
1092 /* Named data stream */
1093 if (ctx->supported_features.named_data_streams)
1095 } else if (!(inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
1096 FILE_ATTRIBUTE_ENCRYPTED))
1097 && !(inode_is_symlink(inode)
1098 && !ctx->supported_features.reparse_points
1099 && ctx->supported_features.symlink_reparse_points))
1102 * Unnamed data stream. Skip if any of the following is true:
1104 * - file is a directory
1105 * - file is encrypted
1106 * - backend needs to create the file as UNIX symlink
1107 * - backend will extract the stream as externally
1108 * backed from the WIM archive itself
1110 if (ctx->apply_ops->will_back_from_wim) {
1111 int ret = (*ctx->apply_ops->will_back_from_wim)(dentry, ctx);
1112 if (ret > 0) /* Error? */
1114 if (ret < 0) /* Won't externally back? */
1121 case STREAM_TYPE_REPARSE_POINT:
1122 wimlib_assert(inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT);
1123 if (ctx->supported_features.reparse_points ||
1124 (inode_is_symlink(inode) &&
1125 ctx->supported_features.symlink_reparse_points))
1128 case STREAM_TYPE_EFSRPC_RAW_DATA:
1129 wimlib_assert(inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED);
1130 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY) {
1131 if (ctx->supported_features.encrypted_directories)
1134 if (ctx->supported_features.encrypted_files)
1140 return ref_stream(strm, dentry, ctx);
1145 dentry_ref_streams(struct wim_dentry *dentry, struct apply_ctx *ctx)
1147 struct wim_inode *inode = dentry->d_inode;
1148 for (unsigned i = 0; i < inode->i_num_streams; i++) {
1149 int ret = ref_stream_if_needed(dentry, inode,
1150 &inode->i_streams[i], ctx);
1154 inode->i_visited = 1;
1159 * Given a list of dentries to be extracted, build the list of blobs that need
1160 * to be extracted, and for each blob determine the streams to which that blob
1161 * will be extracted.
1163 * This also initializes the extract progress info with byte and blob
1166 * ctx->supported_features must be filled in.
1169 dentry_list_ref_streams(struct list_head *dentry_list, struct apply_ctx *ctx)
1171 struct wim_dentry *dentry;
1174 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
1175 ret = dentry_ref_streams(dentry, ctx);
1179 list_for_each_entry(dentry, dentry_list, d_extraction_list_node)
1180 dentry->d_inode->i_visited = 0;
1185 dentry_list_build_inode_alias_lists(struct list_head *dentry_list)
1187 struct wim_dentry *dentry;
1189 list_for_each_entry(dentry, dentry_list, d_extraction_list_node)
1190 dentry->d_inode->i_first_extraction_alias = NULL;
1192 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
1193 dentry->d_next_extraction_alias = dentry->d_inode->i_first_extraction_alias;
1194 dentry->d_inode->i_first_extraction_alias = dentry;
1199 inode_tally_features(const struct wim_inode *inode,
1200 struct wim_features *features)
1202 if (inode->i_attributes & FILE_ATTRIBUTE_READONLY)
1203 features->readonly_files++;
1204 if (inode->i_attributes & FILE_ATTRIBUTE_HIDDEN)
1205 features->hidden_files++;
1206 if (inode->i_attributes & FILE_ATTRIBUTE_SYSTEM)
1207 features->system_files++;
1208 if (inode->i_attributes & FILE_ATTRIBUTE_ARCHIVE)
1209 features->archive_files++;
1210 if (inode->i_attributes & FILE_ATTRIBUTE_COMPRESSED)
1211 features->compressed_files++;
1212 if (inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED) {
1213 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)
1214 features->encrypted_directories++;
1216 features->encrypted_files++;
1218 if (inode->i_attributes & FILE_ATTRIBUTE_NOT_CONTENT_INDEXED)
1219 features->not_context_indexed_files++;
1220 if (inode->i_attributes & FILE_ATTRIBUTE_SPARSE_FILE)
1221 features->sparse_files++;
1222 if (inode_has_named_data_stream(inode))
1223 features->named_data_streams++;
1224 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1225 features->reparse_points++;
1226 if (inode_is_symlink(inode))
1227 features->symlink_reparse_points++;
1229 features->other_reparse_points++;
1231 if (inode_has_security_descriptor(inode))
1232 features->security_descriptors++;
1233 if (inode_has_unix_data(inode))
1234 features->unix_data++;
1235 if (inode_has_object_id(inode))
1236 features->object_ids++;
1237 if (inode_has_linux_xattrs(inode))
1238 features->linux_xattrs++;
1241 /* Tally features necessary to extract a dentry and the corresponding inode. */
1243 dentry_tally_features(struct wim_dentry *dentry, struct wim_features *features)
1245 struct wim_inode *inode = dentry->d_inode;
1247 if (dentry_has_short_name(dentry))
1248 features->short_names++;
1250 if (inode->i_visited) {
1251 features->hard_links++;
1253 inode_tally_features(inode, features);
1254 inode->i_visited = 1;
1258 /* Tally the features necessary to extract the specified dentries. */
1260 dentry_list_get_features(struct list_head *dentry_list,
1261 struct wim_features *features)
1263 struct wim_dentry *dentry;
1265 list_for_each_entry(dentry, dentry_list, d_extraction_list_node)
1266 dentry_tally_features(dentry, features);
1268 list_for_each_entry(dentry, dentry_list, d_extraction_list_node)
1269 dentry->d_inode->i_visited = 0;
1273 do_feature_check(const struct wim_features *required_features,
1274 const struct wim_features *supported_features,
1277 /* Encrypted files. */
1278 if (required_features->encrypted_files &&
1279 !supported_features->encrypted_files)
1280 WARNING("Ignoring EFS-encrypted data of %lu files",
1281 required_features->encrypted_files);
1283 /* Named data streams. */
1284 if (required_features->named_data_streams &&
1285 !supported_features->named_data_streams)
1286 WARNING("Ignoring named data streams of %lu files",
1287 required_features->named_data_streams);
1289 /* File attributes. */
1290 if (!(extract_flags & WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES)) {
1292 if (required_features->readonly_files &&
1293 !supported_features->readonly_files)
1294 WARNING("Ignoring FILE_ATTRIBUTE_READONLY of %lu files",
1295 required_features->readonly_files);
1297 if (required_features->hidden_files &&
1298 !supported_features->hidden_files)
1299 WARNING("Ignoring FILE_ATTRIBUTE_HIDDEN of %lu files",
1300 required_features->hidden_files);
1302 if (required_features->system_files &&
1303 !supported_features->system_files)
1304 WARNING("Ignoring FILE_ATTRIBUTE_SYSTEM of %lu files",
1305 required_features->system_files);
1307 /* Note: Don't bother the user about FILE_ATTRIBUTE_ARCHIVE.
1308 * We're an archive program, so theoretically we can do what we
1311 if (required_features->compressed_files &&
1312 !supported_features->compressed_files)
1313 WARNING("Ignoring FILE_ATTRIBUTE_COMPRESSED of %lu files",
1314 required_features->compressed_files);
1316 if (required_features->not_context_indexed_files &&
1317 !supported_features->not_context_indexed_files)
1318 WARNING("Ignoring FILE_ATTRIBUTE_NOT_CONTENT_INDEXED of %lu files",
1319 required_features->not_context_indexed_files);
1321 if (required_features->sparse_files &&
1322 !supported_features->sparse_files)
1323 WARNING("Ignoring FILE_ATTRIBUTE_SPARSE_FILE of %lu files",
1324 required_features->sparse_files);
1326 if (required_features->encrypted_directories &&
1327 !supported_features->encrypted_directories)
1328 WARNING("Ignoring FILE_ATTRIBUTE_ENCRYPTED of %lu directories",
1329 required_features->encrypted_directories);
1333 if (required_features->hard_links && !supported_features->hard_links)
1334 WARNING("Extracting %lu hard links as independent files",
1335 required_features->hard_links);
1337 /* Symbolic links and reparse points. */
1338 if ((extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS) &&
1339 required_features->symlink_reparse_points &&
1340 !supported_features->symlink_reparse_points &&
1341 !supported_features->reparse_points)
1343 ERROR("Extraction backend does not support symbolic links!");
1344 return WIMLIB_ERR_UNSUPPORTED;
1346 if (required_features->reparse_points &&
1347 !supported_features->reparse_points)
1349 if (supported_features->symlink_reparse_points) {
1350 if (required_features->other_reparse_points) {
1351 WARNING("Ignoring reparse data of %lu non-symlink/junction files",
1352 required_features->other_reparse_points);
1355 WARNING("Ignoring reparse data of %lu files",
1356 required_features->reparse_points);
1360 /* Security descriptors. */
1361 if (((extract_flags & (WIMLIB_EXTRACT_FLAG_STRICT_ACLS |
1362 WIMLIB_EXTRACT_FLAG_UNIX_DATA))
1363 == WIMLIB_EXTRACT_FLAG_STRICT_ACLS) &&
1364 required_features->security_descriptors &&
1365 !supported_features->security_descriptors)
1367 ERROR("Extraction backend does not support security descriptors!");
1368 return WIMLIB_ERR_UNSUPPORTED;
1370 if (!(extract_flags & WIMLIB_EXTRACT_FLAG_NO_ACLS) &&
1371 required_features->security_descriptors &&
1372 !supported_features->security_descriptors)
1373 WARNING("Ignoring Windows NT security descriptors of %lu files",
1374 required_features->security_descriptors);
1376 /* Standard UNIX metadata */
1377 if (required_features->unix_data &&
1378 (!supported_features->unix_data ||
1379 !(extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA)))
1381 if (extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
1382 ERROR("Requested UNIX metadata extraction, but "
1383 "extraction backend does not support it!");
1384 return WIMLIB_ERR_UNSUPPORTED;
1386 WARNING("Ignoring UNIX metadata (uid/gid/mode/rdev) of %lu files%"TS,
1387 required_features->unix_data,
1388 (supported_features->unix_data ?
1389 T("\n (use --unix-data mode to extract these)") : T("")));
1392 /* Linux-style extended attributes */
1393 if (required_features->linux_xattrs &&
1394 (!supported_features->linux_xattrs ||
1395 !(extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA)))
1397 WARNING("Ignoring Linux-style extended attributes of %lu files%"TS,
1398 required_features->linux_xattrs,
1399 (supported_features->linux_xattrs ?
1400 T("\n (use --unix-data mode to extract these)") : T("")));
1404 if (required_features->object_ids && !supported_features->object_ids) {
1405 WARNING("Ignoring object IDs of %lu files",
1406 required_features->object_ids);
1410 if (required_features->short_names &&
1411 !supported_features->short_names)
1413 if (extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_SHORT_NAMES) {
1414 ERROR("Extraction backend does not support DOS names!");
1415 return WIMLIB_ERR_UNSUPPORTED;
1417 WARNING("Ignoring DOS names of %lu files",
1418 required_features->short_names);
1422 if ((extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_TIMESTAMPS) &&
1423 !supported_features->timestamps)
1425 ERROR("Extraction backend does not support timestamps!");
1426 return WIMLIB_ERR_UNSUPPORTED;
1432 static const struct apply_operations *
1433 select_apply_operations(int extract_flags)
1436 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS)
1437 return &ntfs_3g_apply_ops;
1440 return &win32_apply_ops;
1442 return &unix_apply_ops;
1447 extract_trees(WIMStruct *wim, struct wim_dentry **trees, size_t num_trees,
1448 const tchar *target, int extract_flags)
1450 const struct apply_operations *ops;
1451 struct apply_ctx *ctx;
1453 LIST_HEAD(dentry_list);
1455 if (extract_flags & WIMLIB_EXTRACT_FLAG_TO_STDOUT) {
1456 ret = extract_dentries_to_stdout(trees, num_trees,
1461 num_trees = remove_duplicate_trees(trees, num_trees);
1462 num_trees = remove_contained_trees(trees, num_trees);
1464 ops = select_apply_operations(extract_flags);
1466 if (num_trees > 1 && ops->single_tree_only) {
1467 ERROR("Extracting multiple directory trees "
1468 "at once is not supported in %s extraction mode!",
1470 ret = WIMLIB_ERR_UNSUPPORTED;
1474 ctx = CALLOC(1, ops->context_size);
1476 ret = WIMLIB_ERR_NOMEM;
1481 ctx->target = target;
1482 ctx->target_nchars = tstrlen(target);
1483 ctx->extract_flags = extract_flags;
1484 if (ctx->wim->progfunc) {
1485 ctx->progfunc = ctx->wim->progfunc;
1486 ctx->progctx = ctx->wim->progctx;
1487 ctx->progress.extract.image = wim->current_image;
1488 ctx->progress.extract.extract_flags = (extract_flags &
1489 WIMLIB_EXTRACT_MASK_PUBLIC);
1490 ctx->progress.extract.wimfile_name = wim->filename;
1491 ctx->progress.extract.image_name = wimlib_get_image_name(wim,
1492 wim->current_image);
1493 ctx->progress.extract.target = target;
1495 INIT_LIST_HEAD(&ctx->blob_list);
1496 filedes_invalidate(&ctx->tmpfile_fd);
1497 ctx->apply_ops = ops;
1499 ret = (*ops->get_supported_features)(target, &ctx->supported_features);
1503 build_dentry_list(&dentry_list, trees, num_trees,
1505 WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE));
1507 dentry_list_get_features(&dentry_list, &ctx->required_features);
1509 ret = do_feature_check(&ctx->required_features, &ctx->supported_features,
1510 ctx->extract_flags);
1514 ret = dentry_list_calculate_extraction_names(&dentry_list, ctx);
1518 if (unlikely(list_empty(&dentry_list))) {
1519 WARNING("There is nothing to extract!");
1523 ret = dentry_list_resolve_streams(&dentry_list, ctx);
1527 dentry_list_build_inode_alias_lists(&dentry_list);
1529 ret = dentry_list_ref_streams(&dentry_list, ctx);
1533 if (extract_flags & WIMLIB_EXTRACT_FLAG_FROM_PIPE) {
1534 /* When extracting from a pipe, the number of bytes of data to
1535 * extract can't be determined in the normal way (examining the
1536 * blob table), since at this point all we have is a set of
1537 * SHA-1 message digests of blobs that need to be extracted.
1538 * However, we can get a reasonably accurate estimate by taking
1539 * <TOTALBYTES> from the corresponding <IMAGE> in the WIM XML
1540 * data. This does assume that a full image is being extracted,
1541 * but currently there is no API for doing otherwise. (Also,
1542 * subtract <HARDLINKBYTES> from this if hard links are
1543 * supported by the extraction mode.) */
1544 ctx->progress.extract.total_bytes =
1545 xml_get_image_total_bytes(wim->xml_info,
1546 wim->current_image);
1547 if (ctx->supported_features.hard_links) {
1548 ctx->progress.extract.total_bytes -=
1549 xml_get_image_hard_link_bytes(wim->xml_info,
1550 wim->current_image);
1554 ret = extract_progress(ctx,
1555 ((extract_flags & WIMLIB_EXTRACT_FLAG_IMAGEMODE) ?
1556 WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN :
1557 WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN));
1561 ret = (*ops->extract)(&dentry_list, ctx);
1565 if (ctx->progress.extract.completed_bytes <
1566 ctx->progress.extract.total_bytes)
1568 ctx->progress.extract.completed_bytes =
1569 ctx->progress.extract.total_bytes;
1570 ret = extract_progress(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS);
1575 ret = extract_progress(ctx,
1576 ((extract_flags & WIMLIB_EXTRACT_FLAG_IMAGEMODE) ?
1577 WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END :
1578 WIMLIB_PROGRESS_MSG_EXTRACT_TREE_END));
1580 destroy_blob_list(&ctx->blob_list);
1581 destroy_dentry_list(&dentry_list);
1588 mkdir_if_needed(const tchar *target)
1590 if (!tmkdir(target, 0755))
1593 if (errno == EEXIST)
1597 /* _wmkdir() fails with EACCES if called on a drive root directory. */
1598 if (errno == EACCES)
1602 ERROR_WITH_ERRNO("Failed to create directory \"%"TS"\"", target);
1603 return WIMLIB_ERR_MKDIR;
1606 /* Make sure the extraction flags make sense, and update them if needed. */
1608 check_extract_flags(const WIMStruct *wim, int *extract_flags_p)
1610 int extract_flags = *extract_flags_p;
1612 /* Check for invalid flag combinations */
1614 if ((extract_flags &
1615 (WIMLIB_EXTRACT_FLAG_NO_ACLS |
1616 WIMLIB_EXTRACT_FLAG_STRICT_ACLS)) == (WIMLIB_EXTRACT_FLAG_NO_ACLS |
1617 WIMLIB_EXTRACT_FLAG_STRICT_ACLS))
1618 return WIMLIB_ERR_INVALID_PARAM;
1620 if ((extract_flags &
1621 (WIMLIB_EXTRACT_FLAG_RPFIX |
1622 WIMLIB_EXTRACT_FLAG_NORPFIX)) == (WIMLIB_EXTRACT_FLAG_RPFIX |
1623 WIMLIB_EXTRACT_FLAG_NORPFIX))
1624 return WIMLIB_ERR_INVALID_PARAM;
1626 #ifndef WITH_NTFS_3G
1627 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1628 ERROR("wimlib was compiled without support for NTFS-3G, so\n"
1629 " it cannot apply a WIM image directly to an NTFS volume.");
1630 return WIMLIB_ERR_UNSUPPORTED;
1634 if (extract_flags & WIMLIB_EXTRACT_FLAG_WIMBOOT) {
1637 return WIMLIB_ERR_NO_FILENAME;
1639 ERROR("WIMBoot extraction is only supported on Windows!");
1640 return WIMLIB_ERR_UNSUPPORTED;
1644 if (extract_flags & (WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS4K |
1645 WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS8K |
1646 WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS16K |
1647 WIMLIB_EXTRACT_FLAG_COMPACT_LZX))
1651 count += ((extract_flags & WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS4K) != 0);
1652 count += ((extract_flags & WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS8K) != 0);
1653 count += ((extract_flags & WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS16K) != 0);
1654 count += ((extract_flags & WIMLIB_EXTRACT_FLAG_COMPACT_LZX) != 0);
1656 ERROR("Only one compression format can be specified "
1657 "for compact-mode extraction!");
1658 return WIMLIB_ERR_INVALID_PARAM;
1660 if (extract_flags & WIMLIB_EXTRACT_FLAG_WIMBOOT) {
1661 ERROR("Compact-mode extraction and WIMBoot-mode "
1662 "extraction are mutually exclusive!");
1663 return WIMLIB_ERR_INVALID_PARAM;
1666 ERROR("Compact-mode extraction (System Compression) "
1667 "is only supported on Windows!");
1668 return WIMLIB_ERR_UNSUPPORTED;
1673 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_RPFIX |
1674 WIMLIB_EXTRACT_FLAG_NORPFIX |
1675 WIMLIB_EXTRACT_FLAG_IMAGEMODE)) ==
1676 WIMLIB_EXTRACT_FLAG_IMAGEMODE)
1678 /* For full-image extraction, do reparse point fixups by default
1679 * if the WIM header says they are enabled. */
1680 if (wim->hdr.flags & WIM_HDR_FLAG_RP_FIX)
1681 extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
1684 *extract_flags_p = extract_flags;
1688 struct append_dentry_ctx {
1689 struct wim_dentry **dentries;
1690 size_t num_dentries;
1691 size_t num_alloc_dentries;
1695 append_dentry_cb(struct wim_dentry *dentry, void *_ctx)
1697 struct append_dentry_ctx *ctx = _ctx;
1699 if (ctx->num_dentries == ctx->num_alloc_dentries) {
1700 struct wim_dentry **new_dentries;
1703 new_length = max(ctx->num_alloc_dentries + 8,
1704 ctx->num_alloc_dentries * 3 / 2);
1705 new_dentries = REALLOC(ctx->dentries,
1706 new_length * sizeof(ctx->dentries[0]));
1707 if (new_dentries == NULL)
1708 return WIMLIB_ERR_NOMEM;
1709 ctx->dentries = new_dentries;
1710 ctx->num_alloc_dentries = new_length;
1712 ctx->dentries[ctx->num_dentries++] = dentry;
1716 /* Append dentries matched by a path which can contain wildcard characters. */
1718 append_matched_dentries(WIMStruct *wim, const tchar *orig_pattern,
1719 int extract_flags, struct append_dentry_ctx *ctx)
1721 const size_t count_before = ctx->num_dentries;
1725 pattern = canonicalize_wim_path(orig_pattern);
1727 return WIMLIB_ERR_NOMEM;
1728 ret = expand_path_pattern(wim_get_current_root_dentry(wim), pattern,
1729 append_dentry_cb, ctx);
1731 if (ret || ctx->num_dentries > count_before)
1733 if (extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_GLOB) {
1734 ERROR("No matches for path pattern \"%"TS"\"", orig_pattern);
1735 return WIMLIB_ERR_PATH_DOES_NOT_EXIST;
1737 WARNING("No matches for path pattern \"%"TS"\"", orig_pattern);
1742 do_wimlib_extract_paths(WIMStruct *wim, int image, const tchar *target,
1743 const tchar * const *paths, size_t num_paths,
1747 struct wim_dentry **trees;
1750 if (wim == NULL || target == NULL || target[0] == T('\0') ||
1751 (num_paths != 0 && paths == NULL))
1752 return WIMLIB_ERR_INVALID_PARAM;
1754 ret = check_extract_flags(wim, &extract_flags);
1758 ret = select_wim_image(wim, image);
1762 ret = wim_checksum_unhashed_blobs(wim);
1766 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_NTFS |
1767 WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE)) ==
1768 (WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE))
1770 ret = mkdir_if_needed(target);
1775 if (extract_flags & WIMLIB_EXTRACT_FLAG_GLOB_PATHS) {
1777 struct append_dentry_ctx append_dentry_ctx = {
1780 .num_alloc_dentries = 0,
1783 for (size_t i = 0; i < num_paths; i++) {
1784 ret = append_matched_dentries(wim, paths[i],
1786 &append_dentry_ctx);
1788 trees = append_dentry_ctx.dentries;
1789 goto out_free_trees;
1792 trees = append_dentry_ctx.dentries;
1793 num_trees = append_dentry_ctx.num_dentries;
1795 trees = MALLOC(num_paths * sizeof(trees[0]));
1797 return WIMLIB_ERR_NOMEM;
1799 for (size_t i = 0; i < num_paths; i++) {
1801 tchar *path = canonicalize_wim_path(paths[i]);
1803 ret = WIMLIB_ERR_NOMEM;
1804 goto out_free_trees;
1807 trees[i] = get_dentry(wim, path,
1808 WIMLIB_CASE_PLATFORM_DEFAULT);
1810 if (trees[i] == NULL) {
1811 ERROR("Path \"%"TS"\" does not exist "
1813 paths[i], wim->current_image);
1814 ret = WIMLIB_ERR_PATH_DOES_NOT_EXIST;
1815 goto out_free_trees;
1818 num_trees = num_paths;
1821 if (num_trees == 0) {
1823 goto out_free_trees;
1826 ret = extract_trees(wim, trees, num_trees, target, extract_flags);
1833 extract_single_image(WIMStruct *wim, int image,
1834 const tchar *target, int extract_flags)
1836 const tchar *path = WIMLIB_WIM_ROOT_PATH;
1837 extract_flags |= WIMLIB_EXTRACT_FLAG_IMAGEMODE;
1838 return do_wimlib_extract_paths(wim, image, target, &path, 1, extract_flags);
1841 static const tchar * const filename_forbidden_chars =
1848 /* This function checks if it is okay to use a WIM image's name as a directory
1851 image_name_ok_as_dir(const tchar *image_name)
1853 return image_name && *image_name &&
1854 !tstrpbrk(image_name, filename_forbidden_chars) &&
1855 tstrcmp(image_name, T(".")) &&
1856 tstrcmp(image_name, T("..")) &&
1857 tstrlen(image_name) <= 128;
1860 /* Extracts all images from the WIM to the directory @target, with the images
1861 * placed in subdirectories named by their image names. */
1863 extract_all_images(WIMStruct *wim, const tchar *target, int extract_flags)
1865 size_t output_path_len = tstrlen(target);
1866 tchar buf[output_path_len + 1 + 128 + 1];
1869 const tchar *image_name;
1871 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1872 ERROR("Cannot extract multiple images in NTFS extraction mode.");
1873 return WIMLIB_ERR_INVALID_PARAM;
1876 ret = mkdir_if_needed(target);
1879 tmemcpy(buf, target, output_path_len);
1880 buf[output_path_len] = OS_PREFERRED_PATH_SEPARATOR;
1881 for (image = 1; image <= wim->hdr.image_count; image++) {
1882 image_name = wimlib_get_image_name(wim, image);
1883 if (image_name_ok_as_dir(image_name)) {
1884 tstrcpy(buf + output_path_len + 1, image_name);
1886 /* Image name is empty or contains forbidden characters.
1887 * Use image number instead. */
1888 tsprintf(buf + output_path_len + 1, T("%d"), image);
1890 ret = extract_single_image(wim, image, buf, extract_flags);
1898 do_wimlib_extract_image(WIMStruct *wim, int image, const tchar *target,
1901 if (extract_flags & (WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE |
1902 WIMLIB_EXTRACT_FLAG_TO_STDOUT |
1903 WIMLIB_EXTRACT_FLAG_GLOB_PATHS))
1904 return WIMLIB_ERR_INVALID_PARAM;
1906 if (image == WIMLIB_ALL_IMAGES)
1907 return extract_all_images(wim, target, extract_flags);
1909 return extract_single_image(wim, image, target, extract_flags);
1913 /****************************************************************************
1915 ****************************************************************************/
1918 wimlib_extract_paths(WIMStruct *wim, int image, const tchar *target,
1919 const tchar * const *paths, size_t num_paths,
1922 if (extract_flags & ~WIMLIB_EXTRACT_MASK_PUBLIC)
1923 return WIMLIB_ERR_INVALID_PARAM;
1925 return do_wimlib_extract_paths(wim, image, target, paths, num_paths,
1930 wimlib_extract_pathlist(WIMStruct *wim, int image, const tchar *target,
1931 const tchar *path_list_file, int extract_flags)
1938 ret = read_path_list_file(path_list_file, &paths, &num_paths, &mem);
1940 ERROR("Failed to read path list file \"%"TS"\"",
1945 ret = wimlib_extract_paths(wim, image, target,
1946 (const tchar * const *)paths, num_paths,
1954 wimlib_extract_image_from_pipe_with_progress(int pipe_fd,
1955 const tchar *image_num_or_name,
1956 const tchar *target,
1958 wimlib_progress_func_t progfunc,
1963 struct filedes *in_fd;
1967 if (extract_flags & ~WIMLIB_EXTRACT_MASK_PUBLIC)
1968 return WIMLIB_ERR_INVALID_PARAM;
1970 /* Read the WIM header from the pipe and get a WIMStruct to represent
1971 * the pipable WIM. Caveats: Unlike getting a WIMStruct with
1972 * wimlib_open_wim(), getting a WIMStruct in this way will result in an
1973 * empty blob table, no XML data read, and no filename set. */
1974 ret = open_wim_as_WIMStruct(&pipe_fd, WIMLIB_OPEN_FLAG_FROM_PIPE, &pwm,
1979 /* Sanity check to make sure this is a pipable WIM. */
1980 if (pwm->hdr.magic != PWM_MAGIC) {
1981 ERROR("The WIM being read from file descriptor %d "
1982 "is not pipable!", pipe_fd);
1983 ret = WIMLIB_ERR_NOT_PIPABLE;
1984 goto out_wimlib_free;
1987 /* Sanity check to make sure the first part of a pipable split WIM is
1988 * sent over the pipe first. */
1989 if (pwm->hdr.part_number != 1) {
1990 ERROR("The first part of the split WIM must be "
1991 "sent over the pipe first.");
1992 ret = WIMLIB_ERR_INVALID_PIPABLE_WIM;
1993 goto out_wimlib_free;
1996 in_fd = &pwm->in_fd;
1997 wimlib_assert(in_fd->offset == WIM_HEADER_DISK_SIZE);
1999 /* As mentioned, the WIMStruct we created from the pipe does not have
2000 * XML data yet. Fix this by reading the extra copy of the XML data
2001 * that directly follows the header in pipable WIMs. (Note: see
2002 * write_pipable_wim() for more details about the format of pipable
2005 u8 hash[SHA1_HASH_SIZE];
2007 ret = read_pwm_blob_header(pwm, hash,
2008 &pwm->hdr.xml_data_reshdr, NULL);
2010 goto out_wimlib_free;
2012 if (!(pwm->hdr.xml_data_reshdr.flags & WIM_RESHDR_FLAG_METADATA)) {
2013 ERROR("Expected XML data, but found non-metadata resource.");
2014 ret = WIMLIB_ERR_INVALID_PIPABLE_WIM;
2015 goto out_wimlib_free;
2018 ret = read_wim_xml_data(pwm);
2020 goto out_wimlib_free;
2022 if (xml_get_image_count(pwm->xml_info) != pwm->hdr.image_count) {
2023 ERROR("Image count in XML data is not the same as in WIM header.");
2024 ret = WIMLIB_ERR_IMAGE_COUNT;
2025 goto out_wimlib_free;
2029 /* Get image index (this may use the XML data that was just read to
2030 * resolve an image name). */
2031 if (image_num_or_name) {
2032 image = wimlib_resolve_image(pwm, image_num_or_name);
2033 if (image == WIMLIB_NO_IMAGE) {
2034 ERROR("\"%"TS"\" is not a valid image in the pipable WIM!",
2036 ret = WIMLIB_ERR_INVALID_IMAGE;
2037 goto out_wimlib_free;
2038 } else if (image == WIMLIB_ALL_IMAGES) {
2039 ERROR("Applying all images from a pipe is not supported!");
2040 ret = WIMLIB_ERR_INVALID_IMAGE;
2041 goto out_wimlib_free;
2044 if (pwm->hdr.image_count != 1) {
2045 ERROR("No image was specified, but the pipable WIM "
2046 "did not contain exactly 1 image");
2047 ret = WIMLIB_ERR_INVALID_IMAGE;
2048 goto out_wimlib_free;
2053 /* Load the needed metadata resource. */
2054 for (i = 1; i <= pwm->hdr.image_count; i++) {
2055 ret = handle_pwm_metadata_resource(pwm, i, i == image);
2057 goto out_wimlib_free;
2059 /* Extract the image. */
2060 extract_flags |= WIMLIB_EXTRACT_FLAG_FROM_PIPE;
2061 ret = do_wimlib_extract_image(pwm, image, target, extract_flags);
2062 /* Clean up and return. */
2070 wimlib_extract_image_from_pipe(int pipe_fd, const tchar *image_num_or_name,
2071 const tchar *target, int extract_flags)
2073 return wimlib_extract_image_from_pipe_with_progress(pipe_fd,
2082 wimlib_extract_image(WIMStruct *wim, int image, const tchar *target,
2085 if (extract_flags & ~WIMLIB_EXTRACT_MASK_PUBLIC)
2086 return WIMLIB_ERR_INVALID_PARAM;
2087 return do_wimlib_extract_image(wim, image, target, extract_flags);
2090 /* Extract a list of blobs to the specified directory.
2092 * This is somewhat of a hack: we generate a temporary WIM in memory so that we
2093 * can just send it through the regular extraction code. */
2095 do_wimlib_extract_blobs(struct list_head *blob_list, const tchar *target,
2096 wimlib_progress_func_t progfunc, void *progctx,
2099 WIMStruct *tmp_wim = NULL;
2101 struct blob_descriptor *blob;
2102 tchar name_buf[SHA1_HASH_SIZE * 2 + 1];
2103 struct wim_dentry *root_dentry;
2104 struct wim_dentry *dentry;
2105 struct wim_dentry *existing;
2106 struct wim_image_metadata *imd;
2107 const tchar *root_path = WIMLIB_WIM_ROOT_PATH;
2109 if (extract_flags & WIMLIB_EXTRACT_FLAG_TO_STDOUT) {
2110 list_for_each_entry(blob, blob_list, extraction_list) {
2111 ret = extract_blob_to_stdout(blob);
2118 ret = wimlib_create_new_wim(WIMLIB_COMPRESSION_TYPE_NONE, &tmp_wim);
2122 wimlib_register_progress_function(tmp_wim, progfunc, progctx);
2124 ret = wimlib_add_empty_image(tmp_wim, NULL, NULL);
2128 ret = select_wim_image(tmp_wim, 1);
2132 ret = new_filler_directory(&root_dentry);
2136 imd = wim_get_current_image_metadata(tmp_wim);
2138 imd->root_dentry = root_dentry;
2140 list_for_each_entry(blob, blob_list, extraction_list) {
2142 sprint_hash(blob->hash, name_buf);
2144 ret = new_dentry_with_new_inode(name_buf, true, &dentry);
2148 ret = WIMLIB_ERR_NOMEM;
2149 if (!inode_add_stream(dentry->d_inode, STREAM_TYPE_DATA,
2150 NO_STREAM_NAME, blob))
2152 hlist_add_head(&dentry->d_inode->i_hlist_node, &imd->inode_list);
2154 existing = dentry_add_child(root_dentry, dentry);
2155 wimlib_assert(!existing);
2158 ret = do_wimlib_extract_paths(tmp_wim, 1, target, &root_path, 1,
2161 wimlib_free(tmp_wim);
2165 static struct blob_descriptor *
2166 lookup_data_or_metadata_blob(WIMStruct *wim, const u8 *hash)
2168 struct blob_descriptor *blob;
2170 blob = lookup_blob(wim->blob_table, hash);
2174 if (wim_has_metadata(wim))
2175 for (int i = 0; i < wim->hdr.image_count; i++)
2176 if (hashes_equal(hash, wim->image_metadata[i]-> metadata_blob->hash))
2177 return wim->image_metadata[i]->metadata_blob;
2182 add_blob_to_list(struct blob_descriptor *blob, void *_list)
2184 list_add(&blob->extraction_list, (struct list_head *)_list);
2188 /* Extract the specified blobs from a WIM to a directory. */
2190 wimlib_extract_blobs(WIMStruct *wim, const u8 *blob_sha1s, size_t num_blobs,
2191 const tchar *target, int extract_flags)
2195 struct blob_descriptor *blob;
2197 LIST_HEAD(blob_list);
2199 if (!wim || !target || !*target)
2200 return WIMLIB_ERR_INVALID_PARAM;
2202 if (num_blobs && !blob_sha1s)
2203 return WIMLIB_ERR_INVALID_PARAM;
2205 if (extract_flags & ~WIMLIB_EXTRACT_MASK_PUBLIC)
2206 return WIMLIB_ERR_INVALID_PARAM;
2208 extract_flags &= ~WIMLIB_EXTRACT_FLAG_GLOB_PATHS;
2210 if (extract_flags & (WIMLIB_EXTRACT_FLAG_ALL_DATA_BLOBS |
2211 WIMLIB_EXTRACT_FLAG_ALL_METADATA_BLOBS))
2213 /* Add all the data blobs. */
2214 if (extract_flags & WIMLIB_EXTRACT_FLAG_ALL_DATA_BLOBS)
2215 for_blob_in_table(wim->blob_table, add_blob_to_list,
2218 /* Also add the metadata blobs. */
2219 if ((extract_flags & WIMLIB_EXTRACT_FLAG_ALL_METADATA_BLOBS)
2220 && wim_has_metadata(wim))
2221 for (int i = 0; i < wim->hdr.image_count; i++)
2222 add_blob_to_list(wim->image_metadata[i]->metadata_blob,
2225 hashptr = blob_sha1s;
2226 hashend = hashptr + (num_blobs * SHA1_HASH_SIZE);
2228 /* Look up each blob requested. */
2229 for (; hashptr != hashend; hashptr += SHA1_HASH_SIZE) {
2231 blob = lookup_data_or_metadata_blob(wim, hashptr);
2233 if (wimlib_print_errors) {
2234 tchar hashstr[SHA1_HASH_SIZE * 2 + 1];
2235 sprint_hash(hashptr, hashstr);
2236 ERROR("Stream SHA1=%"TS" not found",
2239 return WIMLIB_ERR_RESOURCE_NOT_FOUND;
2241 add_blob_to_list(blob, &blob_list);
2245 /* Extract the blobs. */
2246 return do_wimlib_extract_blobs(&blob_list, target, wim->progfunc,
2247 wim->progctx, extract_flags);