4 * Support for extracting WIM images, or files or directories contained in a WIM
9 * Copyright (C) 2012, 2013, 2014 Eric Biggers
11 * This file is part of wimlib, a library for working with WIM files.
13 * wimlib is free software; you can redistribute it and/or modify it under the
14 * terms of the GNU General Public License as published by the Free
15 * Software Foundation; either version 3 of the License, or (at your option)
18 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
19 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
20 * A PARTICULAR PURPOSE. See the GNU General Public License for more
23 * You should have received a copy of the GNU General Public License
24 * along with wimlib; if not, see http://www.gnu.org/licenses/.
28 * This file provides the API functions wimlib_extract_image(),
29 * wimlib_extract_image_from_pipe(), wimlib_extract_paths(), and
30 * wimlib_extract_pathlist(). Internally, all end up calling
31 * do_wimlib_extract_paths() and extract_trees().
33 * Although wimlib supports multiple extraction modes/backends (NTFS-3g, UNIX,
34 * Win32), this file does not itself have code to extract files or directories
35 * to any specific target; instead, it handles generic functionality and relies
36 * on lower-level callback functions declared in `struct apply_operations' to do
37 * the actual extraction.
44 #include "wimlib/apply.h"
45 #include "wimlib/dentry.h"
46 #include "wimlib/encoding.h"
47 #include "wimlib/endianness.h"
48 #include "wimlib/error.h"
49 #include "wimlib/lookup_table.h"
50 #include "wimlib/metadata.h"
51 #include "wimlib/pathlist.h"
52 #include "wimlib/paths.h"
53 #include "wimlib/reparse.h"
54 #include "wimlib/resource.h"
55 #include "wimlib/security.h"
56 #include "wimlib/unix_data.h"
58 # include "wimlib/win32.h" /* for realpath() equivalent */
60 #include "wimlib/xml.h"
61 #include "wimlib/wildcard.h"
62 #include "wimlib/wim.h"
70 #define WIMLIB_EXTRACT_FLAG_FROM_PIPE 0x80000000
71 #define WIMLIB_EXTRACT_FLAG_IMAGEMODE 0x40000000
73 /* Keep in sync with wimlib.h */
74 #define WIMLIB_EXTRACT_MASK_PUBLIC \
75 (WIMLIB_EXTRACT_FLAG_NTFS | \
76 WIMLIB_EXTRACT_FLAG_UNIX_DATA | \
77 WIMLIB_EXTRACT_FLAG_NO_ACLS | \
78 WIMLIB_EXTRACT_FLAG_STRICT_ACLS | \
79 WIMLIB_EXTRACT_FLAG_RPFIX | \
80 WIMLIB_EXTRACT_FLAG_NORPFIX | \
81 WIMLIB_EXTRACT_FLAG_TO_STDOUT | \
82 WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES | \
83 WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS | \
84 WIMLIB_EXTRACT_FLAG_STRICT_TIMESTAMPS | \
85 WIMLIB_EXTRACT_FLAG_STRICT_SHORT_NAMES | \
86 WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS | \
87 WIMLIB_EXTRACT_FLAG_GLOB_PATHS | \
88 WIMLIB_EXTRACT_FLAG_STRICT_GLOB | \
89 WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES | \
90 WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE | \
91 WIMLIB_EXTRACT_FLAG_WIMBOOT)
93 /* Send WIMLIB_PROGRESS_MSG_EXTRACT_FILE_STRUCTURE or
94 * WIMLIB_PROGRESS_MSG_EXTRACT_METADATA. */
96 do_file_extract_progress(struct apply_ctx *ctx, enum wimlib_progress_msg msg)
98 ctx->count_until_file_progress = 512; /* Arbitrary value to limit calls */
99 return extract_progress(ctx, msg);
102 /* Check whether the extraction of a dentry should be skipped completely. */
104 dentry_is_supported(struct wim_dentry *dentry,
105 const struct wim_features *supported_features)
107 struct wim_inode *inode = dentry->d_inode;
109 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
110 return supported_features->reparse_points ||
111 (inode_is_symlink(inode) &&
112 supported_features->symlink_reparse_points);
114 if (inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED) {
115 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)
116 return supported_features->encrypted_directories != 0;
118 return supported_features->encrypted_files != 0;
124 #define PWM_ALLOW_WIM_HDR 0x00001
126 /* Read the header from a stream in a pipable WIM. */
128 read_pwm_stream_header(WIMStruct *pwm, struct wim_lookup_table_entry *lte,
129 struct wim_resource_spec *rspec,
130 int flags, struct wim_header_disk *hdr_ret)
133 struct pwm_stream_hdr stream_hdr;
134 struct wim_header_disk pwm_hdr;
136 struct wim_reshdr reshdr;
139 ret = full_read(&pwm->in_fd, &buf.stream_hdr, sizeof(buf.stream_hdr));
143 if ((flags & PWM_ALLOW_WIM_HDR) &&
144 le64_to_cpu(buf.stream_hdr.magic) == PWM_MAGIC)
146 BUILD_BUG_ON(sizeof(buf.pwm_hdr) < sizeof(buf.stream_hdr));
147 ret = full_read(&pwm->in_fd, &buf.stream_hdr + 1,
148 sizeof(buf.pwm_hdr) - sizeof(buf.stream_hdr));
152 lte->resource_location = RESOURCE_NONEXISTENT;
153 memcpy(hdr_ret, &buf.pwm_hdr, sizeof(buf.pwm_hdr));
157 if (le64_to_cpu(buf.stream_hdr.magic) != PWM_STREAM_MAGIC) {
158 ERROR("Data read on pipe is invalid (expected stream header).");
159 return WIMLIB_ERR_INVALID_PIPABLE_WIM;
162 copy_hash(lte->hash, buf.stream_hdr.hash);
164 reshdr.size_in_wim = 0;
165 reshdr.flags = le32_to_cpu(buf.stream_hdr.flags);
166 reshdr.offset_in_wim = pwm->in_fd.offset;
167 reshdr.uncompressed_size = le64_to_cpu(buf.stream_hdr.uncompressed_size);
168 wim_res_hdr_to_spec(&reshdr, pwm, rspec);
169 lte_bind_wim_resource_spec(lte, rspec);
170 lte->flags = rspec->flags;
171 lte->size = rspec->uncompressed_size;
172 lte->offset_in_res = 0;
176 ERROR_WITH_ERRNO("Error reading pipable WIM from pipe");
181 load_streams_from_pipe(struct apply_ctx *ctx,
182 const struct read_stream_list_callbacks *cbs)
184 struct wim_lookup_table_entry *found_lte = NULL;
185 struct wim_resource_spec *rspec = NULL;
186 struct wim_lookup_table *lookup_table;
189 ret = WIMLIB_ERR_NOMEM;
190 found_lte = new_lookup_table_entry();
194 rspec = MALLOC(sizeof(struct wim_resource_spec));
198 lookup_table = ctx->wim->lookup_table;
199 memcpy(ctx->progress.extract.guid, ctx->wim->hdr.guid, WIM_GUID_LEN);
200 ctx->progress.extract.part_number = ctx->wim->hdr.part_number;
201 ctx->progress.extract.total_parts = ctx->wim->hdr.total_parts;
202 ret = extract_progress(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN);
206 while (ctx->num_streams_remaining) {
207 struct wim_header_disk pwm_hdr;
208 struct wim_lookup_table_entry *needed_lte;
210 if (found_lte->resource_location != RESOURCE_NONEXISTENT)
211 lte_unbind_wim_resource_spec(found_lte);
212 ret = read_pwm_stream_header(ctx->wim, found_lte, rspec,
213 PWM_ALLOW_WIM_HDR, &pwm_hdr);
217 if ((found_lte->resource_location != RESOURCE_NONEXISTENT)
218 && !(found_lte->flags & WIM_RESHDR_FLAG_METADATA)
219 && (needed_lte = lookup_stream(lookup_table, found_lte->hash))
220 && (needed_lte->out_refcnt))
222 needed_lte->offset_in_res = found_lte->offset_in_res;
223 needed_lte->flags = found_lte->flags;
224 needed_lte->size = found_lte->size;
226 lte_unbind_wim_resource_spec(found_lte);
227 lte_bind_wim_resource_spec(needed_lte, rspec);
229 ret = (*cbs->begin_stream)(needed_lte,
230 cbs->begin_stream_ctx);
232 lte_unbind_wim_resource_spec(needed_lte);
236 ret = extract_stream(needed_lte, needed_lte->size,
238 cbs->consume_chunk_ctx);
240 ret = (*cbs->end_stream)(needed_lte, ret,
241 cbs->end_stream_ctx);
242 lte_unbind_wim_resource_spec(needed_lte);
245 ctx->num_streams_remaining--;
246 } else if (found_lte->resource_location != RESOURCE_NONEXISTENT) {
247 ret = skip_wim_stream(found_lte);
251 u16 part_number = le16_to_cpu(pwm_hdr.part_number);
252 u16 total_parts = le16_to_cpu(pwm_hdr.total_parts);
254 if (part_number != ctx->progress.extract.part_number ||
255 total_parts != ctx->progress.extract.total_parts ||
256 memcmp(pwm_hdr.guid, ctx->progress.extract.guid,
259 ctx->progress.extract.part_number = part_number;
260 ctx->progress.extract.total_parts = total_parts;
261 memcpy(ctx->progress.extract.guid,
262 pwm_hdr.guid, WIM_GUID_LEN);
263 ret = extract_progress(ctx,
264 WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN);
272 if (found_lte && found_lte->resource_location != RESOURCE_IN_WIM)
274 free_lookup_table_entry(found_lte);
278 /* Creates a temporary file opened for writing. The open file descriptor is
279 * returned in @fd_ret and its name is returned in @name_ret (dynamically
282 create_temporary_file(struct filedes *fd_ret, tchar **name_ret)
289 name = ttempnam(NULL, T("wimlib"));
291 ERROR_WITH_ERRNO("Failed to create temporary filename");
292 return WIMLIB_ERR_NOMEM;
295 open_flags = O_WRONLY | O_CREAT | O_EXCL | O_BINARY;
297 open_flags |= _O_SHORT_LIVED;
299 raw_fd = topen(name, open_flags, 0600);
302 if (errno == EEXIST) {
306 ERROR_WITH_ERRNO("Failed to create temporary file "
309 return WIMLIB_ERR_OPEN;
312 filedes_init(fd_ret, raw_fd);
318 begin_extract_stream_wrapper(struct wim_lookup_table_entry *lte, void *_ctx)
320 struct apply_ctx *ctx = _ctx;
322 ctx->cur_stream = lte;
323 ctx->cur_stream_offset = 0;
325 if (unlikely(lte->out_refcnt > MAX_OPEN_STREAMS))
326 return create_temporary_file(&ctx->tmpfile_fd, &ctx->tmpfile_name);
328 return (*ctx->saved_cbs->begin_stream)(lte, ctx->saved_cbs->begin_stream_ctx);
332 extract_chunk_wrapper(const void *chunk, size_t size, void *_ctx)
334 struct apply_ctx *ctx = _ctx;
335 union wimlib_progress_info *progress = &ctx->progress;
338 ctx->cur_stream_offset += size;
340 if (likely(ctx->supported_features.hard_links)) {
341 progress->extract.completed_bytes +=
342 (u64)size * ctx->cur_stream->out_refcnt;
343 if (ctx->cur_stream_offset == ctx->cur_stream->size)
344 progress->extract.completed_streams += ctx->cur_stream->out_refcnt;
346 const struct stream_owner *owners = stream_owners(ctx->cur_stream);
347 for (u32 i = 0; i < ctx->cur_stream->out_refcnt; i++) {
348 const struct wim_inode *inode = owners[i].inode;
349 const struct wim_dentry *dentry;
351 list_for_each_entry(dentry,
352 &inode->i_extraction_aliases,
353 d_extraction_alias_node)
355 progress->extract.completed_bytes += size;
356 if (ctx->cur_stream_offset == ctx->cur_stream->size)
357 progress->extract.completed_streams++;
361 if (progress->extract.completed_bytes >= ctx->next_progress) {
363 ret = extract_progress(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS);
367 if (progress->extract.completed_bytes >=
368 progress->extract.total_bytes)
370 ctx->next_progress = UINT64_MAX;
372 /* Send new message as soon as another 1/128 of the
373 * total has been extracted. (Arbitrary number.) */
375 progress->extract.completed_bytes +
376 progress->extract.total_bytes / 128;
378 /* ... Unless that would be more than 5000000 bytes, in
379 * which case send the next after the next 5000000
380 * bytes. (Another arbitrary number.) */
381 if (progress->extract.completed_bytes + 5000000 <
384 progress->extract.completed_bytes + 5000000;
386 /* ... But always send a message as soon as we're
387 * completely done. */
388 if (progress->extract.total_bytes < ctx->next_progress)
389 ctx->next_progress = progress->extract.total_bytes;
393 if (unlikely(filedes_valid(&ctx->tmpfile_fd))) {
394 /* Just extracting to temporary file for now. */
395 ret = full_write(&ctx->tmpfile_fd, chunk, size);
397 ERROR_WITH_ERRNO("Error writing data to "
398 "temporary file \"%"TS"\"",
403 return (*ctx->saved_cbs->consume_chunk)(chunk, size,
404 ctx->saved_cbs->consume_chunk_ctx);
409 extract_from_tmpfile(const tchar *tmpfile_name, struct apply_ctx *ctx)
411 struct wim_lookup_table_entry tmpfile_lte;
412 struct wim_lookup_table_entry *orig_lte = ctx->cur_stream;
413 const struct read_stream_list_callbacks *cbs = ctx->saved_cbs;
415 const u32 orig_refcnt = orig_lte->out_refcnt;
417 BUILD_BUG_ON(MAX_OPEN_STREAMS < ARRAY_LEN(orig_lte->inline_stream_owners));
419 struct stream_owner *owners = orig_lte->stream_owners;
421 /* Copy the stream's data from the temporary file to each of its
424 * This is executed only in the very uncommon case that a
425 * single-instance stream is being extracted to more than
426 * MAX_OPEN_STREAMS locations! */
428 memcpy(&tmpfile_lte, orig_lte, sizeof(struct wim_lookup_table_entry));
429 tmpfile_lte.resource_location = RESOURCE_IN_FILE_ON_DISK;
430 tmpfile_lte.file_on_disk = ctx->tmpfile_name;
432 for (u32 i = 0; i < orig_refcnt; i++) {
434 /* Note: it usually doesn't matter whether we pass the original
435 * stream entry to callbacks provided by the extraction backend
436 * as opposed to the tmpfile stream entry, since they shouldn't
437 * actually read data from the stream other than through the
438 * read_stream_prefix() call below. But for
439 * WIMLIB_EXTRACT_FLAG_WIMBOOT mode on Windows it does matter
440 * because it needs the original stream location in order to
441 * create the external backing reference. */
443 orig_lte->out_refcnt = 1;
444 orig_lte->inline_stream_owners[0] = owners[i];
446 ret = (*cbs->begin_stream)(orig_lte, cbs->begin_stream_ctx);
450 /* Extra SHA-1 isn't necessary here, but it shouldn't hurt as
451 * this case is very rare anyway. */
452 ret = extract_stream(&tmpfile_lte, tmpfile_lte.size,
454 cbs->consume_chunk_ctx);
456 ret = (*cbs->end_stream)(orig_lte, ret, cbs->end_stream_ctx);
461 orig_lte->out_refcnt = 0;
466 end_extract_stream_wrapper(struct wim_lookup_table_entry *stream,
467 int status, void *_ctx)
469 struct apply_ctx *ctx = _ctx;
471 if (unlikely(filedes_valid(&ctx->tmpfile_fd))) {
472 filedes_close(&ctx->tmpfile_fd);
474 status = extract_from_tmpfile(ctx->tmpfile_name, ctx);
475 filedes_invalidate(&ctx->tmpfile_fd);
476 tunlink(ctx->tmpfile_name);
477 FREE(ctx->tmpfile_name);
480 return (*ctx->saved_cbs->end_stream)(stream, status,
481 ctx->saved_cbs->end_stream_ctx);
486 * Read the list of single-instance streams to extract and feed their data into
487 * the specified callback functions.
489 * This handles checksumming each stream.
491 * This also handles sending WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS.
493 * This also works if the WIM is being read from a pipe, whereas attempting to
494 * read streams directly (e.g. with read_full_stream_into_buf()) will not.
496 * This also will split up streams that will need to be extracted to more than
497 * MAX_OPEN_STREAMS locations, as measured by the 'out_refcnt' of each stream.
498 * Therefore, the apply_operations implementation need not worry about running
499 * out of file descriptors, unless it might open more than one file descriptor
500 * per nominal destination (e.g. Win32 currently might because the destination
501 * file system might not support hard links).
504 extract_stream_list(struct apply_ctx *ctx,
505 const struct read_stream_list_callbacks *cbs)
507 struct read_stream_list_callbacks wrapper_cbs = {
508 .begin_stream = begin_extract_stream_wrapper,
509 .begin_stream_ctx = ctx,
510 .consume_chunk = extract_chunk_wrapper,
511 .consume_chunk_ctx = ctx,
512 .end_stream = end_extract_stream_wrapper,
513 .end_stream_ctx = ctx,
515 ctx->saved_cbs = cbs;
516 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_FROM_PIPE) {
517 return load_streams_from_pipe(ctx, &wrapper_cbs);
519 return read_stream_list(&ctx->stream_list,
520 offsetof(struct wim_lookup_table_entry,
522 &wrapper_cbs, VERIFY_STREAM_HASHES);
526 /* Extract a WIM dentry to standard output.
528 * This obviously doesn't make sense in all cases. We return an error if the
529 * dentry does not correspond to a regular file. Otherwise we extract the
530 * unnamed data stream only. */
532 extract_dentry_to_stdout(struct wim_dentry *dentry,
533 const struct wim_lookup_table *lookup_table)
535 struct wim_inode *inode = dentry->d_inode;
536 struct wim_lookup_table_entry *lte;
537 struct filedes _stdout;
539 if (inode->i_attributes & (FILE_ATTRIBUTE_REPARSE_POINT |
540 FILE_ATTRIBUTE_DIRECTORY))
542 ERROR("\"%"TS"\" is not a regular file and therefore cannot be "
543 "extracted to standard output", dentry_full_path(dentry));
544 return WIMLIB_ERR_NOT_A_REGULAR_FILE;
547 lte = inode_unnamed_lte(inode, lookup_table);
549 const u8 *hash = inode_unnamed_stream_hash(inode);
550 if (!is_zero_hash(hash))
551 return stream_not_found_error(inode, hash);
555 filedes_init(&_stdout, STDOUT_FILENO);
556 return extract_full_stream_to_fd(lte, &_stdout);
560 extract_dentries_to_stdout(struct wim_dentry **dentries, size_t num_dentries,
561 const struct wim_lookup_table *lookup_table)
563 for (size_t i = 0; i < num_dentries; i++) {
564 int ret = extract_dentry_to_stdout(dentries[i], lookup_table);
571 /**********************************************************************/
574 * Removes duplicate dentries from the array.
576 * Returns the new number of dentries, packed at the front of the array.
579 remove_duplicate_trees(struct wim_dentry **trees, size_t num_trees)
582 for (i = 0; i < num_trees; i++) {
583 if (!trees[i]->tmp_flag) {
584 /* Found distinct dentry. */
585 trees[i]->tmp_flag = 1;
586 trees[j++] = trees[i];
589 for (i = 0; i < j; i++)
590 trees[i]->tmp_flag = 0;
595 * Remove dentries that are descendants of other dentries in the array.
597 * Returns the new number of dentries, packed at the front of the array.
600 remove_contained_trees(struct wim_dentry **trees, size_t num_trees)
603 for (i = 0; i < num_trees; i++)
604 trees[i]->tmp_flag = 1;
605 for (i = 0; i < num_trees; i++) {
606 struct wim_dentry *d = trees[i];
607 while (!dentry_is_root(d)) {
612 trees[j++] = trees[i];
616 trees[i]->tmp_flag = 0;
619 for (i = 0; i < j; i++)
620 trees[i]->tmp_flag = 0;
625 dentry_append_to_list(struct wim_dentry *dentry, void *_dentry_list)
627 struct list_head *dentry_list = _dentry_list;
628 list_add_tail(&dentry->d_extraction_list_node, dentry_list);
633 dentry_reset_extraction_list_node(struct wim_dentry *dentry)
635 dentry->d_extraction_list_node = (struct list_head){NULL, NULL};
639 dentry_delete_from_list(struct wim_dentry *dentry, void *_ignore)
641 list_del(&dentry->d_extraction_list_node);
642 dentry_reset_extraction_list_node(dentry);
647 * Build the preliminary list of dentries to be extracted.
649 * The list maintains the invariant that if d1 and d2 are in the list and d1 is
650 * an ancestor of d2, then d1 appears before d2 in the list.
653 build_dentry_list(struct list_head *dentry_list, struct wim_dentry **trees,
654 size_t num_trees, bool add_ancestors)
656 INIT_LIST_HEAD(dentry_list);
658 /* Add the trees recursively. */
659 for (size_t i = 0; i < num_trees; i++)
660 for_dentry_in_tree(trees[i], dentry_append_to_list, dentry_list);
662 /* If requested, add ancestors of the trees. */
664 for (size_t i = 0; i < num_trees; i++) {
665 struct wim_dentry *dentry = trees[i];
666 struct wim_dentry *ancestor;
667 struct list_head *place_after;
669 if (dentry_is_root(dentry))
672 place_after = dentry_list;
675 ancestor = ancestor->d_parent;
676 if (will_extract_dentry(ancestor)) {
677 place_after = &ancestor->d_extraction_list_node;
680 } while (!dentry_is_root(ancestor));
684 ancestor = ancestor->d_parent;
685 if (will_extract_dentry(ancestor))
687 list_add(&ancestor->d_extraction_list_node, place_after);
688 } while (!dentry_is_root(ancestor));
694 destroy_dentry_list(struct list_head *dentry_list)
696 struct wim_dentry *dentry, *tmp;
697 struct wim_inode *inode;
699 list_for_each_entry_safe(dentry, tmp, dentry_list, d_extraction_list_node) {
700 inode = dentry->d_inode;
701 dentry_reset_extraction_list_node(dentry);
702 inode->i_visited = 0;
703 if ((void *)dentry->d_extraction_name != (void *)dentry->file_name)
704 FREE(dentry->d_extraction_name);
705 dentry->d_extraction_name = NULL;
706 dentry->d_extraction_name_nchars = 0;
711 destroy_stream_list(struct list_head *stream_list)
713 struct wim_lookup_table_entry *lte;
715 list_for_each_entry(lte, stream_list, extraction_list)
716 if (lte->out_refcnt > ARRAY_LEN(lte->inline_stream_owners))
717 FREE(lte->stream_owners);
721 static const utf16lechar replacement_char = cpu_to_le16(0xfffd);
723 static const utf16lechar replacement_char = cpu_to_le16('?');
727 file_name_valid(utf16lechar *name, size_t num_chars, bool fix)
733 for (i = 0; i < num_chars; i++) {
736 case cpu_to_le16('\\'):
737 case cpu_to_le16(':'):
738 case cpu_to_le16('*'):
739 case cpu_to_le16('?'):
740 case cpu_to_le16('"'):
741 case cpu_to_le16('<'):
742 case cpu_to_le16('>'):
743 case cpu_to_le16('|'):
745 case cpu_to_le16('/'):
746 case cpu_to_le16('\0'):
748 name[i] = replacement_char;
755 if (name[num_chars - 1] == cpu_to_le16(' ') ||
756 name[num_chars - 1] == cpu_to_le16('.'))
759 name[num_chars - 1] = replacement_char;
768 dentry_calculate_extraction_name(struct wim_dentry *dentry,
769 struct apply_ctx *ctx)
773 if (!dentry_is_supported(dentry, &ctx->supported_features))
776 if (dentry_is_root(dentry))
780 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
781 dentry->d_extraction_name = dentry->file_name;
782 dentry->d_extraction_name_nchars = dentry->file_name_nbytes /
788 if (!ctx->supported_features.case_sensitive_filenames) {
789 struct wim_dentry *other;
790 list_for_each_entry(other, &dentry->d_ci_conflict_list,
793 if (will_extract_dentry(other)) {
794 if (ctx->extract_flags &
795 WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS) {
796 WARNING("\"%"TS"\" has the same "
797 "case-insensitive name as "
798 "\"%"TS"\"; extracting "
799 "dummy name instead",
800 dentry_full_path(dentry),
801 dentry_full_path(other));
804 WARNING("Not extracting \"%"TS"\": "
805 "has same case-insensitive "
807 dentry_full_path(dentry),
808 dentry_full_path(other));
815 if (file_name_valid(dentry->file_name, dentry->file_name_nbytes / 2, false)) {
816 ret = utf16le_get_tstr(dentry->file_name,
817 dentry->file_name_nbytes,
818 (const tchar **)&dentry->d_extraction_name,
819 &dentry->d_extraction_name_nchars);
820 dentry->d_extraction_name_nchars /= sizeof(tchar);
823 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES)
825 WARNING("\"%"TS"\" has an invalid filename "
826 "that is not supported on this platform; "
827 "extracting dummy name instead",
828 dentry_full_path(dentry));
831 WARNING("Not extracting \"%"TS"\": has an invalid filename "
832 "that is not supported on this platform",
833 dentry_full_path(dentry));
840 utf16lechar utf16_name_copy[dentry->file_name_nbytes / 2];
842 memcpy(utf16_name_copy, dentry->file_name, dentry->file_name_nbytes);
843 file_name_valid(utf16_name_copy, dentry->file_name_nbytes / 2, true);
845 const tchar *tchar_name;
848 ret = utf16le_get_tstr(utf16_name_copy,
849 dentry->file_name_nbytes,
850 &tchar_name, &tchar_nchars);
854 tchar_nchars /= sizeof(tchar);
856 size_t fixed_name_num_chars = tchar_nchars;
857 tchar fixed_name[tchar_nchars + 50];
859 tmemcpy(fixed_name, tchar_name, tchar_nchars);
860 fixed_name_num_chars += tsprintf(fixed_name + tchar_nchars,
861 T(" (invalid filename #%lu)"),
862 ++ctx->invalid_sequence);
864 utf16le_put_tstr(tchar_name);
866 dentry->d_extraction_name = memdup(fixed_name,
867 2 * fixed_name_num_chars + 2);
868 if (!dentry->d_extraction_name)
869 return WIMLIB_ERR_NOMEM;
870 dentry->d_extraction_name_nchars = fixed_name_num_chars;
875 for_dentry_in_tree(dentry, dentry_delete_from_list, NULL);
880 * Calculate the actual filename component at which each WIM dentry will be
881 * extracted, with special handling for dentries that are unsupported by the
882 * extraction backend or have invalid names.
884 * ctx->supported_features must be filled in.
886 * Possible error codes: WIMLIB_ERR_NOMEM, WIMLIB_ERR_INVALID_UTF16_STRING
889 dentry_list_calculate_extraction_names(struct list_head *dentry_list,
890 struct apply_ctx *ctx)
892 struct list_head *prev, *cur;
894 /* Can't use list_for_each_entry() because a call to
895 * dentry_calculate_extraction_name() may delete the current dentry and
896 * its children from the list. */
900 struct wim_dentry *dentry;
904 if (cur == dentry_list)
907 dentry = list_entry(cur, struct wim_dentry, d_extraction_list_node);
909 ret = dentry_calculate_extraction_name(dentry, ctx);
913 if (prev->next == cur)
916 ; /* Current dentry and its children (which follow in
917 the list) were deleted. prev stays the same. */
923 dentry_resolve_streams(struct wim_dentry *dentry, int extract_flags,
924 struct wim_lookup_table *lookup_table)
926 struct wim_inode *inode = dentry->d_inode;
927 struct wim_lookup_table_entry *lte;
931 /* Special case: when extracting from a pipe, the WIM lookup table is
932 * initially empty, so "resolving" an inode's streams is initially not
933 * possible. However, we still need to keep track of which streams,
934 * identified by SHA1 message digests, need to be extracted, so we
935 * "resolve" the inode's streams anyway by allocating new entries. */
936 if (extract_flags & WIMLIB_EXTRACT_FLAG_FROM_PIPE)
938 ret = inode_resolve_streams(inode, lookup_table, force);
941 for (u32 i = 0; i <= inode->i_num_ads; i++) {
942 lte = inode_stream_lte_resolved(inode, i);
950 * For each dentry to be extracted, resolve all streams in the corresponding
951 * inode and set 'out_refcnt' in each to 0.
953 * Possible error codes: WIMLIB_ERR_RESOURCE_NOT_FOUND, WIMLIB_ERR_NOMEM.
956 dentry_list_resolve_streams(struct list_head *dentry_list,
957 struct apply_ctx *ctx)
959 struct wim_dentry *dentry;
962 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
963 ret = dentry_resolve_streams(dentry,
965 ctx->wim->lookup_table);
973 ref_stream(struct wim_lookup_table_entry *lte, u32 stream_idx,
974 struct wim_dentry *dentry, struct apply_ctx *ctx)
976 struct wim_inode *inode = dentry->d_inode;
977 struct stream_owner *stream_owners;
982 /* Tally the size only for each extraction of the stream (not hard
984 if (inode->i_visited && ctx->supported_features.hard_links)
987 ctx->progress.extract.total_bytes += lte->size;
988 ctx->progress.extract.total_streams++;
990 if (inode->i_visited)
993 /* Add stream to the dentry_list only one time, even if it's going
994 * to be extracted to multiple inodes. */
995 if (lte->out_refcnt == 0) {
996 list_add_tail(<e->extraction_list, &ctx->stream_list);
997 ctx->num_streams_remaining++;
1000 /* If inode not yet been visited, append it to the stream_owners array. */
1001 if (lte->out_refcnt < ARRAY_LEN(lte->inline_stream_owners)) {
1002 stream_owners = lte->inline_stream_owners;
1004 struct stream_owner *prev_stream_owners;
1005 size_t alloc_stream_owners;
1007 if (lte->out_refcnt == ARRAY_LEN(lte->inline_stream_owners)) {
1008 prev_stream_owners = NULL;
1009 alloc_stream_owners = ARRAY_LEN(lte->inline_stream_owners);
1011 prev_stream_owners = lte->stream_owners;
1012 alloc_stream_owners = lte->alloc_stream_owners;
1015 if (lte->out_refcnt == alloc_stream_owners) {
1016 alloc_stream_owners *= 2;
1017 stream_owners = REALLOC(prev_stream_owners,
1018 alloc_stream_owners *
1019 sizeof(stream_owners[0]));
1021 return WIMLIB_ERR_NOMEM;
1022 if (!prev_stream_owners) {
1023 memcpy(stream_owners,
1024 lte->inline_stream_owners,
1025 sizeof(lte->inline_stream_owners));
1027 lte->stream_owners = stream_owners;
1028 lte->alloc_stream_owners = alloc_stream_owners;
1030 stream_owners = lte->stream_owners;
1032 stream_owners[lte->out_refcnt].inode = inode;
1033 if (stream_idx == 0) {
1034 stream_owners[lte->out_refcnt].stream_name = NULL;
1036 stream_owners[lte->out_refcnt].stream_name =
1037 inode->i_ads_entries[stream_idx - 1].stream_name;
1044 ref_unnamed_stream(struct wim_dentry *dentry, struct apply_ctx *ctx)
1046 struct wim_inode *inode = dentry->d_inode;
1049 struct wim_lookup_table_entry *stream;
1051 if (unlikely(inode_is_encrypted_directory(inode)))
1054 if (unlikely(ctx->apply_ops->will_externally_back)) {
1055 ret = (*ctx->apply_ops->will_externally_back)(dentry, ctx);
1057 if (ret) /* Error */
1059 /* Will externally back */
1062 /* Won't externally back */
1065 stream = inode_unnamed_stream_resolved(inode, &stream_idx);
1066 return ref_stream(stream, stream_idx, dentry, ctx);
1070 dentry_ref_streams(struct wim_dentry *dentry, struct apply_ctx *ctx)
1072 struct wim_inode *inode = dentry->d_inode;
1075 /* The unnamed data stream will almost always be extracted, but there
1076 * exist cases in which it won't be. */
1077 ret = ref_unnamed_stream(dentry, ctx);
1081 /* Named data streams will be extracted only if supported in the current
1082 * extraction mode and volume, and to avoid complications, if not doing
1083 * a linked extraction. */
1084 if (ctx->supported_features.named_data_streams) {
1085 for (u16 i = 0; i < inode->i_num_ads; i++) {
1086 if (!ads_entry_is_named_stream(&inode->i_ads_entries[i]))
1088 ret = ref_stream(inode->i_ads_entries[i].lte, i + 1,
1094 inode->i_visited = 1;
1099 * For each dentry to be extracted, iterate through the data streams of the
1100 * corresponding inode. For each such stream that is not to be ignored due to
1101 * the supported features or extraction flags, add it to the list of streams to
1102 * be extracted (ctx->stream_list) if not already done so.
1104 * Also builds a mapping from each stream to the inodes referencing it.
1106 * This also initializes the extract progress info with byte and stream
1109 * ctx->supported_features must be filled in.
1111 * Possible error codes: WIMLIB_ERR_NOMEM.
1114 dentry_list_ref_streams(struct list_head *dentry_list, struct apply_ctx *ctx)
1116 struct wim_dentry *dentry;
1119 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
1120 ret = dentry_ref_streams(dentry, ctx);
1124 list_for_each_entry(dentry, dentry_list, d_extraction_list_node)
1125 dentry->d_inode->i_visited = 0;
1130 dentry_list_build_inode_alias_lists(struct list_head *dentry_list)
1132 struct wim_dentry *dentry;
1133 struct wim_inode *inode;
1135 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
1136 inode = dentry->d_inode;
1137 if (!inode->i_visited)
1138 INIT_LIST_HEAD(&inode->i_extraction_aliases);
1139 list_add_tail(&dentry->d_extraction_alias_node,
1140 &inode->i_extraction_aliases);
1141 inode->i_visited = 1;
1143 list_for_each_entry(dentry, dentry_list, d_extraction_list_node)
1144 dentry->d_inode->i_visited = 0;
1148 inode_tally_features(const struct wim_inode *inode,
1149 struct wim_features *features)
1151 if (inode->i_attributes & FILE_ATTRIBUTE_ARCHIVE)
1152 features->archive_files++;
1153 if (inode->i_attributes & FILE_ATTRIBUTE_HIDDEN)
1154 features->hidden_files++;
1155 if (inode->i_attributes & FILE_ATTRIBUTE_SYSTEM)
1156 features->system_files++;
1157 if (inode->i_attributes & FILE_ATTRIBUTE_COMPRESSED)
1158 features->compressed_files++;
1159 if (inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED) {
1160 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)
1161 features->encrypted_directories++;
1163 features->encrypted_files++;
1165 if (inode->i_attributes & FILE_ATTRIBUTE_NOT_CONTENT_INDEXED)
1166 features->not_context_indexed_files++;
1167 if (inode->i_attributes & FILE_ATTRIBUTE_SPARSE_FILE)
1168 features->sparse_files++;
1169 if (inode_has_named_stream(inode))
1170 features->named_data_streams++;
1171 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1172 features->reparse_points++;
1173 if (inode_is_symlink(inode))
1174 features->symlink_reparse_points++;
1176 features->other_reparse_points++;
1178 if (inode->i_security_id != -1)
1179 features->security_descriptors++;
1180 if (inode_has_unix_data(inode))
1181 features->unix_data++;
1184 /* Tally features necessary to extract a dentry and the corresponding inode. */
1186 dentry_tally_features(struct wim_dentry *dentry, struct wim_features *features)
1188 struct wim_inode *inode = dentry->d_inode;
1190 if (dentry_has_short_name(dentry))
1191 features->short_names++;
1193 if (inode->i_visited) {
1194 features->hard_links++;
1196 inode_tally_features(inode, features);
1197 inode->i_visited = 1;
1201 /* Tally the features necessary to extract the specified dentries. */
1203 dentry_list_get_features(struct list_head *dentry_list,
1204 struct wim_features *features)
1206 struct wim_dentry *dentry;
1208 list_for_each_entry(dentry, dentry_list, d_extraction_list_node)
1209 dentry_tally_features(dentry, features);
1211 list_for_each_entry(dentry, dentry_list, d_extraction_list_node)
1212 dentry->d_inode->i_visited = 0;
1216 do_feature_check(const struct wim_features *required_features,
1217 const struct wim_features *supported_features,
1220 /* File attributes. */
1221 if (!(extract_flags & WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES)) {
1222 /* Note: Don't bother the user about FILE_ATTRIBUTE_ARCHIVE.
1223 * We're an archive program, so theoretically we can do what we
1226 if (required_features->hidden_files &&
1227 !supported_features->hidden_files)
1228 WARNING("Ignoring FILE_ATTRIBUTE_HIDDEN of %lu files",
1229 required_features->hidden_files);
1231 if (required_features->system_files &&
1232 !supported_features->system_files)
1233 WARNING("Ignoring FILE_ATTRIBUTE_SYSTEM of %lu files",
1234 required_features->system_files);
1236 if (required_features->compressed_files &&
1237 !supported_features->compressed_files)
1238 WARNING("Ignoring FILE_ATTRIBUTE_COMPRESSED of %lu files",
1239 required_features->compressed_files);
1241 if (required_features->not_context_indexed_files &&
1242 !supported_features->not_context_indexed_files)
1243 WARNING("Ignoring FILE_ATTRIBUTE_NOT_CONTENT_INDEXED of %lu files",
1244 required_features->not_context_indexed_files);
1246 if (required_features->sparse_files &&
1247 !supported_features->sparse_files)
1248 WARNING("Ignoring FILE_ATTRIBUTE_SPARSE_FILE of %lu files",
1249 required_features->sparse_files);
1251 if (required_features->encrypted_directories &&
1252 !supported_features->encrypted_directories)
1253 WARNING("Ignoring FILE_ATTRIBUTE_ENCRYPTED of %lu directories",
1254 required_features->encrypted_directories);
1257 /* Encrypted files. */
1258 if (required_features->encrypted_files &&
1259 !supported_features->encrypted_files)
1260 WARNING("Ignoring %lu encrypted files",
1261 required_features->encrypted_files);
1263 /* Named data streams. */
1264 if (required_features->named_data_streams &&
1265 (!supported_features->named_data_streams))
1266 WARNING("Ignoring named data streams of %lu files",
1267 required_features->named_data_streams);
1270 if (required_features->hard_links && !supported_features->hard_links)
1271 WARNING("Extracting %lu hard links as independent files",
1272 required_features->hard_links);
1274 /* Symbolic links and reparse points. */
1275 if ((extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS) &&
1276 required_features->symlink_reparse_points &&
1277 !supported_features->symlink_reparse_points &&
1278 !supported_features->reparse_points)
1280 ERROR("Extraction backend does not support symbolic links!");
1281 return WIMLIB_ERR_UNSUPPORTED;
1283 if (required_features->reparse_points &&
1284 !supported_features->reparse_points)
1286 if (supported_features->symlink_reparse_points) {
1287 if (required_features->other_reparse_points) {
1288 WARNING("Ignoring %lu non-symlink/junction "
1289 "reparse point files",
1290 required_features->other_reparse_points);
1293 WARNING("Ignoring %lu reparse point files",
1294 required_features->reparse_points);
1298 /* Security descriptors. */
1299 if (((extract_flags & (WIMLIB_EXTRACT_FLAG_STRICT_ACLS |
1300 WIMLIB_EXTRACT_FLAG_UNIX_DATA))
1301 == WIMLIB_EXTRACT_FLAG_STRICT_ACLS) &&
1302 required_features->security_descriptors &&
1303 !supported_features->security_descriptors)
1305 ERROR("Extraction backend does not support security descriptors!");
1306 return WIMLIB_ERR_UNSUPPORTED;
1308 if (!(extract_flags & WIMLIB_EXTRACT_FLAG_NO_ACLS) &&
1309 required_features->security_descriptors &&
1310 !supported_features->security_descriptors)
1311 WARNING("Ignoring Windows NT security descriptors of %lu files",
1312 required_features->security_descriptors);
1315 if ((extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) &&
1316 required_features->unix_data && !supported_features->unix_data)
1318 ERROR("Extraction backend does not support UNIX data!");
1319 return WIMLIB_ERR_UNSUPPORTED;
1322 if (required_features->unix_data &&
1323 !(extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA))
1325 WARNING("Ignoring UNIX metadata of %lu files",
1326 required_features->unix_data);
1330 if (required_features->short_names &&
1331 !supported_features->short_names)
1333 if (extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_SHORT_NAMES) {
1334 ERROR("Extraction backend does not support DOS names!");
1335 return WIMLIB_ERR_UNSUPPORTED;
1337 WARNING("Ignoring DOS names of %lu files",
1338 required_features->short_names);
1342 if ((extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_TIMESTAMPS) &&
1343 !supported_features->timestamps)
1345 ERROR("Extraction backend does not support timestamps!");
1346 return WIMLIB_ERR_UNSUPPORTED;
1352 static const struct apply_operations *
1353 select_apply_operations(int extract_flags)
1356 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS)
1357 return &ntfs_3g_apply_ops;
1360 return &win32_apply_ops;
1362 return &unix_apply_ops;
1367 extract_trees(WIMStruct *wim, struct wim_dentry **trees, size_t num_trees,
1368 const tchar *target, int extract_flags)
1370 const struct apply_operations *ops;
1371 struct apply_ctx *ctx;
1373 LIST_HEAD(dentry_list);
1375 if (extract_flags & WIMLIB_EXTRACT_FLAG_TO_STDOUT) {
1376 ret = extract_dentries_to_stdout(trees, num_trees,
1381 num_trees = remove_duplicate_trees(trees, num_trees);
1382 num_trees = remove_contained_trees(trees, num_trees);
1384 ops = select_apply_operations(extract_flags);
1386 if (num_trees > 1 && ops->single_tree_only) {
1387 ERROR("Extracting multiple directory trees "
1388 "at once is not supported in %s extraction mode!",
1390 ret = WIMLIB_ERR_UNSUPPORTED;
1394 ctx = CALLOC(1, ops->context_size);
1396 ret = WIMLIB_ERR_NOMEM;
1401 ctx->target = target;
1402 ctx->target_nchars = tstrlen(target);
1403 ctx->extract_flags = extract_flags;
1404 if (ctx->wim->progfunc) {
1405 ctx->progfunc = ctx->wim->progfunc;
1406 ctx->progctx = ctx->wim->progctx;
1407 ctx->progress.extract.image = wim->current_image;
1408 ctx->progress.extract.extract_flags = (extract_flags &
1409 WIMLIB_EXTRACT_MASK_PUBLIC);
1410 ctx->progress.extract.wimfile_name = wim->filename;
1411 ctx->progress.extract.image_name = wimlib_get_image_name(wim,
1412 wim->current_image);
1413 ctx->progress.extract.target = target;
1415 INIT_LIST_HEAD(&ctx->stream_list);
1416 filedes_invalidate(&ctx->tmpfile_fd);
1417 ctx->apply_ops = ops;
1419 ret = (*ops->get_supported_features)(target, &ctx->supported_features);
1423 build_dentry_list(&dentry_list, trees, num_trees,
1425 WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE));
1427 dentry_list_get_features(&dentry_list, &ctx->required_features);
1429 ret = do_feature_check(&ctx->required_features, &ctx->supported_features,
1430 ctx->extract_flags);
1434 ret = dentry_list_calculate_extraction_names(&dentry_list, ctx);
1438 ret = dentry_list_resolve_streams(&dentry_list, ctx);
1442 ret = dentry_list_ref_streams(&dentry_list, ctx);
1446 dentry_list_build_inode_alias_lists(&dentry_list);
1448 if (extract_flags & WIMLIB_EXTRACT_FLAG_FROM_PIPE) {
1449 /* When extracting from a pipe, the number of bytes of data to
1450 * extract can't be determined in the normal way (examining the
1451 * lookup table), since at this point all we have is a set of
1452 * SHA1 message digests of streams that need to be extracted.
1453 * However, we can get a reasonably accurate estimate by taking
1454 * <TOTALBYTES> from the corresponding <IMAGE> in the WIM XML
1455 * data. This does assume that a full image is being extracted,
1456 * but currently there is no API for doing otherwise. (Also,
1457 * subtract <HARDLINKBYTES> from this if hard links are
1458 * supported by the extraction mode.) */
1459 ctx->progress.extract.total_bytes =
1460 wim_info_get_image_total_bytes(wim->wim_info,
1461 wim->current_image);
1462 if (ctx->supported_features.hard_links) {
1463 ctx->progress.extract.total_bytes -=
1464 wim_info_get_image_hard_link_bytes(wim->wim_info,
1465 wim->current_image);
1469 ret = extract_progress(ctx,
1470 ((extract_flags & WIMLIB_EXTRACT_FLAG_IMAGEMODE) ?
1471 WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN :
1472 WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN));
1476 ret = (*ops->extract)(&dentry_list, ctx);
1480 if (ctx->progress.extract.completed_bytes <
1481 ctx->progress.extract.total_bytes)
1483 ctx->progress.extract.completed_bytes =
1484 ctx->progress.extract.total_bytes;
1485 ret = extract_progress(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS);
1490 ret = extract_progress(ctx,
1491 ((extract_flags & WIMLIB_EXTRACT_FLAG_IMAGEMODE) ?
1492 WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END :
1493 WIMLIB_PROGRESS_MSG_EXTRACT_TREE_END));
1495 destroy_stream_list(&ctx->stream_list);
1496 destroy_dentry_list(&dentry_list);
1503 mkdir_if_needed(const tchar *target)
1505 if (!tmkdir(target, 0755))
1508 if (errno == EEXIST)
1512 /* _wmkdir() fails with EACCES if called on a drive root directory. */
1513 if (errno == EACCES)
1517 ERROR_WITH_ERRNO("Failed to create directory \"%"TS"\"", target);
1518 return WIMLIB_ERR_MKDIR;
1521 /* Make sure the extraction flags make sense, and update them if needed. */
1523 check_extract_flags(const WIMStruct *wim, int *extract_flags_p)
1525 int extract_flags = *extract_flags_p;
1527 /* Check for invalid flag combinations */
1529 if ((extract_flags &
1530 (WIMLIB_EXTRACT_FLAG_NO_ACLS |
1531 WIMLIB_EXTRACT_FLAG_STRICT_ACLS)) == (WIMLIB_EXTRACT_FLAG_NO_ACLS |
1532 WIMLIB_EXTRACT_FLAG_STRICT_ACLS))
1533 return WIMLIB_ERR_INVALID_PARAM;
1535 if ((extract_flags &
1536 (WIMLIB_EXTRACT_FLAG_RPFIX |
1537 WIMLIB_EXTRACT_FLAG_NORPFIX)) == (WIMLIB_EXTRACT_FLAG_RPFIX |
1538 WIMLIB_EXTRACT_FLAG_NORPFIX))
1539 return WIMLIB_ERR_INVALID_PARAM;
1541 #ifndef WITH_NTFS_3G
1542 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1543 ERROR("wimlib was compiled without support for NTFS-3g, so\n"
1544 " it cannot apply a WIM image directly to an NTFS volume.");
1545 return WIMLIB_ERR_UNSUPPORTED;
1549 if (extract_flags & WIMLIB_EXTRACT_FLAG_WIMBOOT) {
1552 return WIMLIB_ERR_NO_FILENAME;
1554 ERROR("WIMBoot extraction is only supported on Windows!");
1555 return WIMLIB_ERR_UNSUPPORTED;
1560 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_RPFIX |
1561 WIMLIB_EXTRACT_FLAG_NORPFIX |
1562 WIMLIB_EXTRACT_FLAG_IMAGEMODE)) ==
1563 WIMLIB_EXTRACT_FLAG_IMAGEMODE)
1565 /* For full-image extraction, do reparse point fixups by default
1566 * if the WIM header says they are enabled. */
1567 if (wim->hdr.flags & WIM_HDR_FLAG_RP_FIX)
1568 extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
1571 *extract_flags_p = extract_flags;
1576 get_wildcard_flags(int extract_flags)
1578 u32 wildcard_flags = 0;
1580 if (extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_GLOB)
1581 wildcard_flags |= WILDCARD_FLAG_ERROR_IF_NO_MATCH;
1583 wildcard_flags |= WILDCARD_FLAG_WARN_IF_NO_MATCH;
1585 if (default_ignore_case)
1586 wildcard_flags |= WILDCARD_FLAG_CASE_INSENSITIVE;
1588 return wildcard_flags;
1591 struct append_dentry_ctx {
1592 struct wim_dentry **dentries;
1593 size_t num_dentries;
1594 size_t num_alloc_dentries;
1598 append_dentry_cb(struct wim_dentry *dentry, void *_ctx)
1600 struct append_dentry_ctx *ctx = _ctx;
1602 if (ctx->num_dentries == ctx->num_alloc_dentries) {
1603 struct wim_dentry **new_dentries;
1606 new_length = max(ctx->num_alloc_dentries + 8,
1607 ctx->num_alloc_dentries * 3 / 2);
1608 new_dentries = REALLOC(ctx->dentries,
1609 new_length * sizeof(ctx->dentries[0]));
1610 if (new_dentries == NULL)
1611 return WIMLIB_ERR_NOMEM;
1612 ctx->dentries = new_dentries;
1613 ctx->num_alloc_dentries = new_length;
1615 ctx->dentries[ctx->num_dentries++] = dentry;
1620 do_wimlib_extract_paths(WIMStruct *wim, int image, const tchar *target,
1621 const tchar * const *paths, size_t num_paths,
1625 struct wim_dentry **trees;
1628 if (wim == NULL || target == NULL || target[0] == T('\0') ||
1629 (num_paths != 0 && paths == NULL))
1630 return WIMLIB_ERR_INVALID_PARAM;
1632 ret = check_extract_flags(wim, &extract_flags);
1636 ret = select_wim_image(wim, image);
1640 ret = wim_checksum_unhashed_streams(wim);
1644 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_NTFS |
1645 WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE)) ==
1646 (WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE))
1648 ret = mkdir_if_needed(target);
1653 if (extract_flags & WIMLIB_EXTRACT_FLAG_GLOB_PATHS) {
1655 struct append_dentry_ctx append_dentry_ctx = {
1658 .num_alloc_dentries = 0,
1661 u32 wildcard_flags = get_wildcard_flags(extract_flags);
1663 for (size_t i = 0; i < num_paths; i++) {
1664 tchar *path = canonicalize_wim_path(paths[i]);
1666 ret = WIMLIB_ERR_NOMEM;
1667 trees = append_dentry_ctx.dentries;
1668 goto out_free_trees;
1670 ret = expand_wildcard(wim, path,
1676 trees = append_dentry_ctx.dentries;
1677 goto out_free_trees;
1680 trees = append_dentry_ctx.dentries;
1681 num_trees = append_dentry_ctx.num_dentries;
1683 trees = MALLOC(num_paths * sizeof(trees[0]));
1685 return WIMLIB_ERR_NOMEM;
1687 for (size_t i = 0; i < num_paths; i++) {
1689 tchar *path = canonicalize_wim_path(paths[i]);
1691 ret = WIMLIB_ERR_NOMEM;
1692 goto out_free_trees;
1695 trees[i] = get_dentry(wim, path,
1696 WIMLIB_CASE_PLATFORM_DEFAULT);
1698 if (trees[i] == NULL) {
1699 ERROR("Path \"%"TS"\" does not exist "
1701 paths[i], wim->current_image);
1702 ret = WIMLIB_ERR_PATH_DOES_NOT_EXIST;
1703 goto out_free_trees;
1706 num_trees = num_paths;
1709 if (num_trees == 0) {
1711 goto out_free_trees;
1714 ret = extract_trees(wim, trees, num_trees, target, extract_flags);
1721 extract_single_image(WIMStruct *wim, int image,
1722 const tchar *target, int extract_flags)
1724 const tchar *path = WIMLIB_WIM_ROOT_PATH;
1725 extract_flags |= WIMLIB_EXTRACT_FLAG_IMAGEMODE;
1726 return do_wimlib_extract_paths(wim, image, target, &path, 1, extract_flags);
1729 static const tchar * const filename_forbidden_chars =
1738 /* This function checks if it is okay to use a WIM image's name as a directory
1741 image_name_ok_as_dir(const tchar *image_name)
1743 return image_name && *image_name &&
1744 !tstrpbrk(image_name, filename_forbidden_chars) &&
1745 tstrcmp(image_name, T(".")) &&
1746 tstrcmp(image_name, T(".."));
1749 /* Extracts all images from the WIM to the directory @target, with the images
1750 * placed in subdirectories named by their image names. */
1752 extract_all_images(WIMStruct *wim, const tchar *target, int extract_flags)
1754 size_t image_name_max_len = max(xml_get_max_image_name_len(wim), 20);
1755 size_t output_path_len = tstrlen(target);
1756 tchar buf[output_path_len + 1 + image_name_max_len + 1];
1759 const tchar *image_name;
1761 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1762 ERROR("Cannot extract multiple images in NTFS extraction mode.");
1763 return WIMLIB_ERR_INVALID_PARAM;
1766 ret = mkdir_if_needed(target);
1769 tmemcpy(buf, target, output_path_len);
1770 buf[output_path_len] = OS_PREFERRED_PATH_SEPARATOR;
1771 for (image = 1; image <= wim->hdr.image_count; image++) {
1772 image_name = wimlib_get_image_name(wim, image);
1773 if (image_name_ok_as_dir(image_name)) {
1774 tstrcpy(buf + output_path_len + 1, image_name);
1776 /* Image name is empty or contains forbidden characters.
1777 * Use image number instead. */
1778 tsprintf(buf + output_path_len + 1, T("%d"), image);
1780 ret = extract_single_image(wim, image, buf, extract_flags);
1788 do_wimlib_extract_image(WIMStruct *wim, int image, const tchar *target,
1791 if (extract_flags & (WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE |
1792 WIMLIB_EXTRACT_FLAG_TO_STDOUT |
1793 WIMLIB_EXTRACT_FLAG_GLOB_PATHS))
1794 return WIMLIB_ERR_INVALID_PARAM;
1796 if (image == WIMLIB_ALL_IMAGES)
1797 return extract_all_images(wim, target, extract_flags);
1799 return extract_single_image(wim, image, target, extract_flags);
1803 /****************************************************************************
1805 ****************************************************************************/
1808 wimlib_extract_paths(WIMStruct *wim, int image, const tchar *target,
1809 const tchar * const *paths, size_t num_paths,
1812 if (extract_flags & ~WIMLIB_EXTRACT_MASK_PUBLIC)
1813 return WIMLIB_ERR_INVALID_PARAM;
1815 return do_wimlib_extract_paths(wim, image, target, paths, num_paths,
1820 wimlib_extract_pathlist(WIMStruct *wim, int image, const tchar *target,
1821 const tchar *path_list_file, int extract_flags)
1828 ret = read_path_list_file(path_list_file, &paths, &num_paths, &mem);
1830 ERROR("Failed to read path list file \"%"TS"\"",
1835 ret = wimlib_extract_paths(wim, image, target,
1836 (const tchar * const *)paths, num_paths,
1844 wimlib_extract_image_from_pipe_with_progress(int pipe_fd,
1845 const tchar *image_num_or_name,
1846 const tchar *target,
1848 wimlib_progress_func_t progfunc,
1853 struct filedes *in_fd;
1857 if (extract_flags & ~WIMLIB_EXTRACT_MASK_PUBLIC)
1858 return WIMLIB_ERR_INVALID_PARAM;
1860 /* Read the WIM header from the pipe and get a WIMStruct to represent
1861 * the pipable WIM. Caveats: Unlike getting a WIMStruct with
1862 * wimlib_open_wim(), getting a WIMStruct in this way will result in
1863 * an empty lookup table, no XML data read, and no filename set. */
1864 ret = open_wim_as_WIMStruct(&pipe_fd, WIMLIB_OPEN_FLAG_FROM_PIPE, &pwm,
1869 /* Sanity check to make sure this is a pipable WIM. */
1870 if (pwm->hdr.magic != PWM_MAGIC) {
1871 ERROR("The WIM being read from file descriptor %d "
1872 "is not pipable!", pipe_fd);
1873 ret = WIMLIB_ERR_NOT_PIPABLE;
1874 goto out_wimlib_free;
1877 /* Sanity check to make sure the first part of a pipable split WIM is
1878 * sent over the pipe first. */
1879 if (pwm->hdr.part_number != 1) {
1880 ERROR("The first part of the split WIM must be "
1881 "sent over the pipe first.");
1882 ret = WIMLIB_ERR_INVALID_PIPABLE_WIM;
1883 goto out_wimlib_free;
1886 in_fd = &pwm->in_fd;
1887 wimlib_assert(in_fd->offset == WIM_HEADER_DISK_SIZE);
1889 /* As mentioned, the WIMStruct we created from the pipe does not have
1890 * XML data yet. Fix this by reading the extra copy of the XML data
1891 * that directly follows the header in pipable WIMs. (Note: see
1892 * write_pipable_wim() for more details about the format of pipable
1895 struct wim_lookup_table_entry xml_lte;
1896 struct wim_resource_spec xml_rspec;
1897 ret = read_pwm_stream_header(pwm, &xml_lte, &xml_rspec, 0, NULL);
1899 goto out_wimlib_free;
1901 if (!(xml_lte.flags & WIM_RESHDR_FLAG_METADATA))
1903 ERROR("Expected XML data, but found non-metadata "
1905 ret = WIMLIB_ERR_INVALID_PIPABLE_WIM;
1906 goto out_wimlib_free;
1909 wim_res_spec_to_hdr(&xml_rspec, &pwm->hdr.xml_data_reshdr);
1911 ret = read_wim_xml_data(pwm);
1913 goto out_wimlib_free;
1915 if (wim_info_get_num_images(pwm->wim_info) != pwm->hdr.image_count) {
1916 ERROR("Image count in XML data is not the same as in WIM header.");
1917 ret = WIMLIB_ERR_IMAGE_COUNT;
1918 goto out_wimlib_free;
1922 /* Get image index (this may use the XML data that was just read to
1923 * resolve an image name). */
1924 if (image_num_or_name) {
1925 image = wimlib_resolve_image(pwm, image_num_or_name);
1926 if (image == WIMLIB_NO_IMAGE) {
1927 ERROR("\"%"TS"\" is not a valid image in the pipable WIM!",
1929 ret = WIMLIB_ERR_INVALID_IMAGE;
1930 goto out_wimlib_free;
1931 } else if (image == WIMLIB_ALL_IMAGES) {
1932 ERROR("Applying all images from a pipe is not supported!");
1933 ret = WIMLIB_ERR_INVALID_IMAGE;
1934 goto out_wimlib_free;
1937 if (pwm->hdr.image_count != 1) {
1938 ERROR("No image was specified, but the pipable WIM "
1939 "did not contain exactly 1 image");
1940 ret = WIMLIB_ERR_INVALID_IMAGE;
1941 goto out_wimlib_free;
1946 /* Load the needed metadata resource. */
1947 for (i = 1; i <= pwm->hdr.image_count; i++) {
1948 struct wim_lookup_table_entry *metadata_lte;
1949 struct wim_image_metadata *imd;
1950 struct wim_resource_spec *metadata_rspec;
1952 metadata_lte = new_lookup_table_entry();
1953 if (metadata_lte == NULL) {
1954 ret = WIMLIB_ERR_NOMEM;
1955 goto out_wimlib_free;
1957 metadata_rspec = MALLOC(sizeof(struct wim_resource_spec));
1958 if (metadata_rspec == NULL) {
1959 ret = WIMLIB_ERR_NOMEM;
1960 free_lookup_table_entry(metadata_lte);
1961 goto out_wimlib_free;
1964 ret = read_pwm_stream_header(pwm, metadata_lte, metadata_rspec, 0, NULL);
1965 imd = pwm->image_metadata[i - 1];
1966 imd->metadata_lte = metadata_lte;
1968 FREE(metadata_rspec);
1969 goto out_wimlib_free;
1972 if (!(metadata_lte->flags & WIM_RESHDR_FLAG_METADATA)) {
1973 ERROR("Expected metadata resource, but found "
1974 "non-metadata stream.");
1975 ret = WIMLIB_ERR_INVALID_PIPABLE_WIM;
1976 goto out_wimlib_free;
1980 /* Metadata resource is for the image being extracted.
1981 * Parse it and save the metadata in memory. */
1982 ret = read_metadata_resource(pwm, imd);
1984 goto out_wimlib_free;
1987 /* Metadata resource is not for the image being
1988 * extracted. Skip over it. */
1989 ret = skip_wim_stream(metadata_lte);
1991 goto out_wimlib_free;
1994 /* Extract the image. */
1995 extract_flags |= WIMLIB_EXTRACT_FLAG_FROM_PIPE;
1996 ret = do_wimlib_extract_image(pwm, image, target, extract_flags);
1997 /* Clean up and return. */
2005 wimlib_extract_image_from_pipe(int pipe_fd, const tchar *image_num_or_name,
2006 const tchar *target, int extract_flags)
2008 return wimlib_extract_image_from_pipe_with_progress(pipe_fd,
2017 wimlib_extract_image(WIMStruct *wim, int image, const tchar *target,
2020 if (extract_flags & ~WIMLIB_EXTRACT_MASK_PUBLIC)
2021 return WIMLIB_ERR_INVALID_PARAM;
2022 return do_wimlib_extract_image(wim, image, target, extract_flags);