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 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.
42 #include "wimlib/apply.h"
43 #include "wimlib/dentry.h"
44 #include "wimlib/encoding.h"
45 #include "wimlib/endianness.h"
46 #include "wimlib/error.h"
47 #include "wimlib/lookup_table.h"
48 #include "wimlib/metadata.h"
49 #include "wimlib/pathlist.h"
50 #include "wimlib/paths.h"
51 #include "wimlib/reparse.h"
52 #include "wimlib/resource.h"
53 #include "wimlib/security.h"
54 #include "wimlib/unix_data.h"
56 # include "wimlib/win32.h" /* for realpath() equivalent */
58 #include "wimlib/xml.h"
59 #include "wimlib/wildcard.h"
60 #include "wimlib/wim.h"
68 #define WIMLIB_EXTRACT_FLAG_FROM_PIPE 0x80000000
69 #define WIMLIB_EXTRACT_FLAG_IMAGEMODE 0x40000000
71 /* Keep in sync with wimlib.h */
72 #define WIMLIB_EXTRACT_MASK_PUBLIC \
73 (WIMLIB_EXTRACT_FLAG_NTFS | \
74 WIMLIB_EXTRACT_FLAG_UNIX_DATA | \
75 WIMLIB_EXTRACT_FLAG_NO_ACLS | \
76 WIMLIB_EXTRACT_FLAG_STRICT_ACLS | \
77 WIMLIB_EXTRACT_FLAG_RPFIX | \
78 WIMLIB_EXTRACT_FLAG_NORPFIX | \
79 WIMLIB_EXTRACT_FLAG_TO_STDOUT | \
80 WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES | \
81 WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS | \
82 WIMLIB_EXTRACT_FLAG_STRICT_TIMESTAMPS | \
83 WIMLIB_EXTRACT_FLAG_STRICT_SHORT_NAMES | \
84 WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS | \
85 WIMLIB_EXTRACT_FLAG_GLOB_PATHS | \
86 WIMLIB_EXTRACT_FLAG_STRICT_GLOB | \
87 WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES | \
88 WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE | \
89 WIMLIB_EXTRACT_FLAG_WIMBOOT)
91 /* Send WIMLIB_PROGRESS_MSG_EXTRACT_FILE_STRUCTURE or
92 * WIMLIB_PROGRESS_MSG_EXTRACT_METADATA. */
94 do_file_extract_progress(struct apply_ctx *ctx, enum wimlib_progress_msg msg)
96 ctx->count_until_file_progress = 500; /* Arbitrary value to limit calls */
97 return extract_progress(ctx, msg);
101 start_file_phase(struct apply_ctx *ctx, uint64_t end_file_count, enum wimlib_progress_msg msg)
103 ctx->progress.extract.current_file_count = 0;
104 ctx->progress.extract.end_file_count = end_file_count;
105 return do_file_extract_progress(ctx, msg);
109 start_file_structure_phase(struct apply_ctx *ctx, uint64_t end_file_count)
111 return start_file_phase(ctx, end_file_count, WIMLIB_PROGRESS_MSG_EXTRACT_FILE_STRUCTURE);
115 start_file_metadata_phase(struct apply_ctx *ctx, uint64_t end_file_count)
117 return start_file_phase(ctx, end_file_count, WIMLIB_PROGRESS_MSG_EXTRACT_METADATA);
121 end_file_phase(struct apply_ctx *ctx, enum wimlib_progress_msg msg)
123 ctx->progress.extract.current_file_count = ctx->progress.extract.end_file_count;
124 return do_file_extract_progress(ctx, msg);
128 end_file_structure_phase(struct apply_ctx *ctx)
130 return end_file_phase(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_FILE_STRUCTURE);
134 end_file_metadata_phase(struct apply_ctx *ctx)
136 return end_file_phase(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_METADATA);
139 /* Check whether the extraction of a dentry should be skipped completely. */
141 dentry_is_supported(struct wim_dentry *dentry,
142 const struct wim_features *supported_features)
144 struct wim_inode *inode = dentry->d_inode;
146 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
147 return supported_features->reparse_points ||
148 (inode_is_symlink(inode) &&
149 supported_features->symlink_reparse_points);
151 if (inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED) {
152 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)
153 return supported_features->encrypted_directories != 0;
155 return supported_features->encrypted_files != 0;
161 #define PWM_ALLOW_WIM_HDR 0x00001
163 /* Read the header from a stream in a pipable WIM. */
165 read_pwm_stream_header(WIMStruct *pwm, struct wim_lookup_table_entry *lte,
166 struct wim_resource_spec *rspec,
167 int flags, struct wim_header_disk *hdr_ret)
170 struct pwm_stream_hdr stream_hdr;
171 struct wim_header_disk pwm_hdr;
173 struct wim_reshdr reshdr;
176 ret = full_read(&pwm->in_fd, &buf.stream_hdr, sizeof(buf.stream_hdr));
180 if ((flags & PWM_ALLOW_WIM_HDR) &&
181 le64_to_cpu(buf.stream_hdr.magic) == PWM_MAGIC)
183 BUILD_BUG_ON(sizeof(buf.pwm_hdr) < sizeof(buf.stream_hdr));
184 ret = full_read(&pwm->in_fd, &buf.stream_hdr + 1,
185 sizeof(buf.pwm_hdr) - sizeof(buf.stream_hdr));
189 lte->resource_location = RESOURCE_NONEXISTENT;
190 memcpy(hdr_ret, &buf.pwm_hdr, sizeof(buf.pwm_hdr));
194 if (le64_to_cpu(buf.stream_hdr.magic) != PWM_STREAM_MAGIC) {
195 ERROR("Data read on pipe is invalid (expected stream header).");
196 return WIMLIB_ERR_INVALID_PIPABLE_WIM;
199 copy_hash(lte->hash, buf.stream_hdr.hash);
201 reshdr.size_in_wim = 0;
202 reshdr.flags = le32_to_cpu(buf.stream_hdr.flags);
203 reshdr.offset_in_wim = pwm->in_fd.offset;
204 reshdr.uncompressed_size = le64_to_cpu(buf.stream_hdr.uncompressed_size);
205 wim_res_hdr_to_spec(&reshdr, pwm, rspec);
206 lte_bind_wim_resource_spec(lte, rspec);
207 lte->flags = rspec->flags;
208 lte->size = rspec->uncompressed_size;
209 lte->offset_in_res = 0;
213 ERROR_WITH_ERRNO("Error reading pipable WIM from pipe");
218 load_streams_from_pipe(struct apply_ctx *ctx,
219 const struct read_stream_list_callbacks *cbs)
221 struct wim_lookup_table_entry *found_lte = NULL;
222 struct wim_resource_spec *rspec = NULL;
223 struct wim_lookup_table *lookup_table;
226 ret = WIMLIB_ERR_NOMEM;
227 found_lte = new_lookup_table_entry();
231 rspec = MALLOC(sizeof(struct wim_resource_spec));
235 lookup_table = ctx->wim->lookup_table;
236 memcpy(ctx->progress.extract.guid, ctx->wim->hdr.guid, WIM_GUID_LEN);
237 ctx->progress.extract.part_number = ctx->wim->hdr.part_number;
238 ctx->progress.extract.total_parts = ctx->wim->hdr.total_parts;
239 ret = extract_progress(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN);
243 while (ctx->num_streams_remaining) {
244 struct wim_header_disk pwm_hdr;
245 struct wim_lookup_table_entry *needed_lte;
247 if (found_lte->resource_location != RESOURCE_NONEXISTENT)
248 lte_unbind_wim_resource_spec(found_lte);
249 ret = read_pwm_stream_header(ctx->wim, found_lte, rspec,
250 PWM_ALLOW_WIM_HDR, &pwm_hdr);
254 if ((found_lte->resource_location != RESOURCE_NONEXISTENT)
255 && !(found_lte->flags & WIM_RESHDR_FLAG_METADATA)
256 && (needed_lte = lookup_stream(lookup_table, found_lte->hash))
257 && (needed_lte->out_refcnt))
259 needed_lte->offset_in_res = found_lte->offset_in_res;
260 needed_lte->flags = found_lte->flags;
261 needed_lte->size = found_lte->size;
263 lte_unbind_wim_resource_spec(found_lte);
264 lte_bind_wim_resource_spec(needed_lte, rspec);
266 ret = (*cbs->begin_stream)(needed_lte,
267 cbs->begin_stream_ctx);
269 lte_unbind_wim_resource_spec(needed_lte);
273 ret = extract_stream(needed_lte, needed_lte->size,
275 cbs->consume_chunk_ctx);
277 ret = (*cbs->end_stream)(needed_lte, ret,
278 cbs->end_stream_ctx);
279 lte_unbind_wim_resource_spec(needed_lte);
282 ctx->num_streams_remaining--;
283 } else if (found_lte->resource_location != RESOURCE_NONEXISTENT) {
284 ret = skip_wim_stream(found_lte);
288 u16 part_number = le16_to_cpu(pwm_hdr.part_number);
289 u16 total_parts = le16_to_cpu(pwm_hdr.total_parts);
291 if (part_number != ctx->progress.extract.part_number ||
292 total_parts != ctx->progress.extract.total_parts ||
293 memcmp(pwm_hdr.guid, ctx->progress.extract.guid,
296 ctx->progress.extract.part_number = part_number;
297 ctx->progress.extract.total_parts = total_parts;
298 memcpy(ctx->progress.extract.guid,
299 pwm_hdr.guid, WIM_GUID_LEN);
300 ret = extract_progress(ctx,
301 WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN);
309 if (found_lte && found_lte->resource_location != RESOURCE_IN_WIM)
311 free_lookup_table_entry(found_lte);
315 /* Creates a temporary file opened for writing. The open file descriptor is
316 * returned in @fd_ret and its name is returned in @name_ret (dynamically
319 create_temporary_file(struct filedes *fd_ret, tchar **name_ret)
326 name = ttempnam(NULL, T("wimlib"));
328 ERROR_WITH_ERRNO("Failed to create temporary filename");
329 return WIMLIB_ERR_NOMEM;
332 open_flags = O_WRONLY | O_CREAT | O_EXCL | O_BINARY;
334 open_flags |= _O_SHORT_LIVED;
336 raw_fd = topen(name, open_flags, 0600);
339 if (errno == EEXIST) {
343 ERROR_WITH_ERRNO("Failed to create temporary file "
346 return WIMLIB_ERR_OPEN;
349 filedes_init(fd_ret, raw_fd);
355 begin_extract_stream_wrapper(struct wim_lookup_table_entry *lte, void *_ctx)
357 struct apply_ctx *ctx = _ctx;
359 ctx->cur_stream = lte;
360 ctx->cur_stream_offset = 0;
362 if (unlikely(lte->out_refcnt > MAX_OPEN_STREAMS))
363 return create_temporary_file(&ctx->tmpfile_fd, &ctx->tmpfile_name);
365 return (*ctx->saved_cbs->begin_stream)(lte, ctx->saved_cbs->begin_stream_ctx);
369 extract_chunk_wrapper(const void *chunk, size_t size, void *_ctx)
371 struct apply_ctx *ctx = _ctx;
372 union wimlib_progress_info *progress = &ctx->progress;
375 ctx->cur_stream_offset += size;
377 if (likely(ctx->supported_features.hard_links)) {
378 progress->extract.completed_bytes +=
379 (u64)size * ctx->cur_stream->out_refcnt;
380 if (ctx->cur_stream_offset == ctx->cur_stream->size)
381 progress->extract.completed_streams += ctx->cur_stream->out_refcnt;
383 const struct stream_owner *owners = stream_owners(ctx->cur_stream);
384 for (u32 i = 0; i < ctx->cur_stream->out_refcnt; i++) {
385 const struct wim_inode *inode = owners[i].inode;
386 const struct wim_dentry *dentry;
388 list_for_each_entry(dentry,
389 &inode->i_extraction_aliases,
390 d_extraction_alias_node)
392 progress->extract.completed_bytes += size;
393 if (ctx->cur_stream_offset == ctx->cur_stream->size)
394 progress->extract.completed_streams++;
398 if (progress->extract.completed_bytes >= ctx->next_progress) {
400 ret = extract_progress(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS);
404 if (progress->extract.completed_bytes >=
405 progress->extract.total_bytes)
407 ctx->next_progress = UINT64_MAX;
409 /* Send new message as soon as another 1/128 of the
410 * total has been extracted. (Arbitrary number.) */
412 progress->extract.completed_bytes +
413 progress->extract.total_bytes / 128;
415 /* ... Unless that would be more than 5000000 bytes, in
416 * which case send the next after the next 5000000
417 * bytes. (Another arbitrary number.) */
418 if (progress->extract.completed_bytes + 5000000 <
421 progress->extract.completed_bytes + 5000000;
423 /* ... But always send a message as soon as we're
424 * completely done. */
425 if (progress->extract.total_bytes < ctx->next_progress)
426 ctx->next_progress = progress->extract.total_bytes;
430 if (unlikely(filedes_valid(&ctx->tmpfile_fd))) {
431 /* Just extracting to temporary file for now. */
432 ret = full_write(&ctx->tmpfile_fd, chunk, size);
434 ERROR_WITH_ERRNO("Error writing data to "
435 "temporary file \"%"TS"\"",
440 return (*ctx->saved_cbs->consume_chunk)(chunk, size,
441 ctx->saved_cbs->consume_chunk_ctx);
446 extract_from_tmpfile(const tchar *tmpfile_name, struct apply_ctx *ctx)
448 struct wim_lookup_table_entry tmpfile_lte;
449 struct wim_lookup_table_entry *orig_lte = ctx->cur_stream;
450 const struct read_stream_list_callbacks *cbs = ctx->saved_cbs;
452 const u32 orig_refcnt = orig_lte->out_refcnt;
454 BUILD_BUG_ON(MAX_OPEN_STREAMS < ARRAY_LEN(orig_lte->inline_stream_owners));
456 struct stream_owner *owners = orig_lte->stream_owners;
458 /* Copy the stream's data from the temporary file to each of its
461 * This is executed only in the very uncommon case that a
462 * single-instance stream is being extracted to more than
463 * MAX_OPEN_STREAMS locations! */
465 memcpy(&tmpfile_lte, orig_lte, sizeof(struct wim_lookup_table_entry));
466 tmpfile_lte.resource_location = RESOURCE_IN_FILE_ON_DISK;
467 tmpfile_lte.file_on_disk = ctx->tmpfile_name;
469 for (u32 i = 0; i < orig_refcnt; i++) {
471 /* Note: it usually doesn't matter whether we pass the original
472 * stream entry to callbacks provided by the extraction backend
473 * as opposed to the tmpfile stream entry, since they shouldn't
474 * actually read data from the stream other than through the
475 * read_stream_prefix() call below. But for
476 * WIMLIB_EXTRACT_FLAG_WIMBOOT mode on Windows it does matter
477 * because it needs the original stream location in order to
478 * create the external backing reference. */
480 orig_lte->out_refcnt = 1;
481 orig_lte->inline_stream_owners[0] = owners[i];
483 ret = (*cbs->begin_stream)(orig_lte, cbs->begin_stream_ctx);
487 /* Extra SHA-1 isn't necessary here, but it shouldn't hurt as
488 * this case is very rare anyway. */
489 ret = extract_stream(&tmpfile_lte, tmpfile_lte.size,
491 cbs->consume_chunk_ctx);
493 ret = (*cbs->end_stream)(orig_lte, ret, cbs->end_stream_ctx);
498 orig_lte->out_refcnt = 0;
503 end_extract_stream_wrapper(struct wim_lookup_table_entry *stream,
504 int status, void *_ctx)
506 struct apply_ctx *ctx = _ctx;
508 if (unlikely(filedes_valid(&ctx->tmpfile_fd))) {
509 filedes_close(&ctx->tmpfile_fd);
511 status = extract_from_tmpfile(ctx->tmpfile_name, ctx);
512 filedes_invalidate(&ctx->tmpfile_fd);
513 tunlink(ctx->tmpfile_name);
514 FREE(ctx->tmpfile_name);
517 return (*ctx->saved_cbs->end_stream)(stream, status,
518 ctx->saved_cbs->end_stream_ctx);
523 * Read the list of single-instance streams to extract and feed their data into
524 * the specified callback functions.
526 * This handles checksumming each stream.
528 * This also handles sending WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS.
530 * This also works if the WIM is being read from a pipe, whereas attempting to
531 * read streams directly (e.g. with read_full_stream_into_buf()) will not.
533 * This also will split up streams that will need to be extracted to more than
534 * MAX_OPEN_STREAMS locations, as measured by the 'out_refcnt' of each stream.
535 * Therefore, the apply_operations implementation need not worry about running
536 * out of file descriptors, unless it might open more than one file descriptor
537 * per nominal destination (e.g. Win32 currently might because the destination
538 * file system might not support hard links).
541 extract_stream_list(struct apply_ctx *ctx,
542 const struct read_stream_list_callbacks *cbs)
544 struct read_stream_list_callbacks wrapper_cbs = {
545 .begin_stream = begin_extract_stream_wrapper,
546 .begin_stream_ctx = ctx,
547 .consume_chunk = extract_chunk_wrapper,
548 .consume_chunk_ctx = ctx,
549 .end_stream = end_extract_stream_wrapper,
550 .end_stream_ctx = ctx,
552 ctx->saved_cbs = cbs;
553 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_FROM_PIPE) {
554 return load_streams_from_pipe(ctx, &wrapper_cbs);
556 return read_stream_list(&ctx->stream_list,
557 offsetof(struct wim_lookup_table_entry,
559 &wrapper_cbs, VERIFY_STREAM_HASHES);
563 /* Extract a WIM dentry to standard output.
565 * This obviously doesn't make sense in all cases. We return an error if the
566 * dentry does not correspond to a regular file. Otherwise we extract the
567 * unnamed data stream only. */
569 extract_dentry_to_stdout(struct wim_dentry *dentry,
570 const struct wim_lookup_table *lookup_table)
572 struct wim_inode *inode = dentry->d_inode;
573 struct wim_lookup_table_entry *lte;
574 struct filedes _stdout;
576 if (inode->i_attributes & (FILE_ATTRIBUTE_REPARSE_POINT |
577 FILE_ATTRIBUTE_DIRECTORY))
579 ERROR("\"%"TS"\" is not a regular file and therefore cannot be "
580 "extracted to standard output", dentry_full_path(dentry));
581 return WIMLIB_ERR_NOT_A_REGULAR_FILE;
584 lte = inode_unnamed_lte(inode, lookup_table);
586 const u8 *hash = inode_unnamed_stream_hash(inode);
587 if (!is_zero_hash(hash))
588 return stream_not_found_error(inode, hash);
592 filedes_init(&_stdout, STDOUT_FILENO);
593 return extract_full_stream_to_fd(lte, &_stdout);
597 extract_dentries_to_stdout(struct wim_dentry **dentries, size_t num_dentries,
598 const struct wim_lookup_table *lookup_table)
600 for (size_t i = 0; i < num_dentries; i++) {
601 int ret = extract_dentry_to_stdout(dentries[i], lookup_table);
608 /**********************************************************************/
611 * Removes duplicate dentries from the array.
613 * Returns the new number of dentries, packed at the front of the array.
616 remove_duplicate_trees(struct wim_dentry **trees, size_t num_trees)
619 for (i = 0; i < num_trees; i++) {
620 if (!trees[i]->tmp_flag) {
621 /* Found distinct dentry. */
622 trees[i]->tmp_flag = 1;
623 trees[j++] = trees[i];
626 for (i = 0; i < j; i++)
627 trees[i]->tmp_flag = 0;
632 * Remove dentries that are descendants of other dentries in the array.
634 * Returns the new number of dentries, packed at the front of the array.
637 remove_contained_trees(struct wim_dentry **trees, size_t num_trees)
640 for (i = 0; i < num_trees; i++)
641 trees[i]->tmp_flag = 1;
642 for (i = 0; i < num_trees; i++) {
643 struct wim_dentry *d = trees[i];
644 while (!dentry_is_root(d)) {
649 trees[j++] = trees[i];
653 trees[i]->tmp_flag = 0;
656 for (i = 0; i < j; i++)
657 trees[i]->tmp_flag = 0;
662 dentry_append_to_list(struct wim_dentry *dentry, void *_dentry_list)
664 struct list_head *dentry_list = _dentry_list;
665 list_add_tail(&dentry->d_extraction_list_node, dentry_list);
670 dentry_reset_extraction_list_node(struct wim_dentry *dentry)
672 dentry->d_extraction_list_node = (struct list_head){NULL, NULL};
676 dentry_delete_from_list(struct wim_dentry *dentry, void *_ignore)
678 list_del(&dentry->d_extraction_list_node);
679 dentry_reset_extraction_list_node(dentry);
684 * Build the preliminary list of dentries to be extracted.
686 * The list maintains the invariant that if d1 and d2 are in the list and d1 is
687 * an ancestor of d2, then d1 appears before d2 in the list.
690 build_dentry_list(struct list_head *dentry_list, struct wim_dentry **trees,
691 size_t num_trees, bool add_ancestors)
693 INIT_LIST_HEAD(dentry_list);
695 /* Add the trees recursively. */
696 for (size_t i = 0; i < num_trees; i++)
697 for_dentry_in_tree(trees[i], dentry_append_to_list, dentry_list);
699 /* If requested, add ancestors of the trees. */
701 for (size_t i = 0; i < num_trees; i++) {
702 struct wim_dentry *dentry = trees[i];
703 struct wim_dentry *ancestor;
704 struct list_head *place_after;
706 if (dentry_is_root(dentry))
709 place_after = dentry_list;
712 ancestor = ancestor->d_parent;
713 if (will_extract_dentry(ancestor)) {
714 place_after = &ancestor->d_extraction_list_node;
717 } while (!dentry_is_root(ancestor));
721 ancestor = ancestor->d_parent;
722 if (will_extract_dentry(ancestor))
724 list_add(&ancestor->d_extraction_list_node, place_after);
725 } while (!dentry_is_root(ancestor));
731 destroy_dentry_list(struct list_head *dentry_list)
733 struct wim_dentry *dentry, *tmp;
734 struct wim_inode *inode;
736 list_for_each_entry_safe(dentry, tmp, dentry_list, d_extraction_list_node) {
737 inode = dentry->d_inode;
738 dentry_reset_extraction_list_node(dentry);
739 inode->i_visited = 0;
740 inode->i_can_externally_back = 0;
741 if ((void *)dentry->d_extraction_name != (void *)dentry->file_name)
742 FREE(dentry->d_extraction_name);
743 dentry->d_extraction_name = NULL;
744 dentry->d_extraction_name_nchars = 0;
749 destroy_stream_list(struct list_head *stream_list)
751 struct wim_lookup_table_entry *lte;
753 list_for_each_entry(lte, stream_list, extraction_list)
754 if (lte->out_refcnt > ARRAY_LEN(lte->inline_stream_owners))
755 FREE(lte->stream_owners);
759 static const utf16lechar replacement_char = cpu_to_le16(0xfffd);
761 static const utf16lechar replacement_char = cpu_to_le16('?');
765 file_name_valid(utf16lechar *name, size_t num_chars, bool fix)
771 for (i = 0; i < num_chars; i++) {
774 case cpu_to_le16('\\'):
775 case cpu_to_le16(':'):
776 case cpu_to_le16('*'):
777 case cpu_to_le16('?'):
778 case cpu_to_le16('"'):
779 case cpu_to_le16('<'):
780 case cpu_to_le16('>'):
781 case cpu_to_le16('|'):
783 case cpu_to_le16('/'):
784 case cpu_to_le16('\0'):
786 name[i] = replacement_char;
793 if (name[num_chars - 1] == cpu_to_le16(' ') ||
794 name[num_chars - 1] == cpu_to_le16('.'))
797 name[num_chars - 1] = replacement_char;
806 dentry_calculate_extraction_name(struct wim_dentry *dentry,
807 struct apply_ctx *ctx)
811 if (!dentry_is_supported(dentry, &ctx->supported_features))
814 if (dentry_is_root(dentry))
818 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
819 dentry->d_extraction_name = dentry->file_name;
820 dentry->d_extraction_name_nchars = dentry->file_name_nbytes /
826 if (!ctx->supported_features.case_sensitive_filenames) {
827 struct wim_dentry *other;
828 list_for_each_entry(other, &dentry->d_ci_conflict_list,
831 if (will_extract_dentry(other)) {
832 if (ctx->extract_flags &
833 WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS) {
834 WARNING("\"%"TS"\" has the same "
835 "case-insensitive name as "
836 "\"%"TS"\"; extracting "
837 "dummy name instead",
838 dentry_full_path(dentry),
839 dentry_full_path(other));
842 WARNING("Not extracting \"%"TS"\": "
843 "has same case-insensitive "
845 dentry_full_path(dentry),
846 dentry_full_path(other));
853 if (file_name_valid(dentry->file_name, dentry->file_name_nbytes / 2, false)) {
854 ret = utf16le_get_tstr(dentry->file_name,
855 dentry->file_name_nbytes,
856 (const tchar **)&dentry->d_extraction_name,
857 &dentry->d_extraction_name_nchars);
858 dentry->d_extraction_name_nchars /= sizeof(tchar);
861 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES)
863 WARNING("\"%"TS"\" has an invalid filename "
864 "that is not supported on this platform; "
865 "extracting dummy name instead",
866 dentry_full_path(dentry));
869 WARNING("Not extracting \"%"TS"\": has an invalid filename "
870 "that is not supported on this platform",
871 dentry_full_path(dentry));
878 utf16lechar utf16_name_copy[dentry->file_name_nbytes / 2];
880 memcpy(utf16_name_copy, dentry->file_name, dentry->file_name_nbytes);
881 file_name_valid(utf16_name_copy, dentry->file_name_nbytes / 2, true);
883 const tchar *tchar_name;
886 ret = utf16le_get_tstr(utf16_name_copy,
887 dentry->file_name_nbytes,
888 &tchar_name, &tchar_nchars);
892 tchar_nchars /= sizeof(tchar);
894 size_t fixed_name_num_chars = tchar_nchars;
895 tchar fixed_name[tchar_nchars + 50];
897 tmemcpy(fixed_name, tchar_name, tchar_nchars);
898 fixed_name_num_chars += tsprintf(fixed_name + tchar_nchars,
899 T(" (invalid filename #%lu)"),
900 ++ctx->invalid_sequence);
902 utf16le_put_tstr(tchar_name);
904 dentry->d_extraction_name = memdup(fixed_name,
905 2 * fixed_name_num_chars + 2);
906 if (!dentry->d_extraction_name)
907 return WIMLIB_ERR_NOMEM;
908 dentry->d_extraction_name_nchars = fixed_name_num_chars;
913 for_dentry_in_tree(dentry, dentry_delete_from_list, NULL);
918 * Calculate the actual filename component at which each WIM dentry will be
919 * extracted, with special handling for dentries that are unsupported by the
920 * extraction backend or have invalid names.
922 * ctx->supported_features must be filled in.
924 * Possible error codes: WIMLIB_ERR_NOMEM, WIMLIB_ERR_INVALID_UTF16_STRING
927 dentry_list_calculate_extraction_names(struct list_head *dentry_list,
928 struct apply_ctx *ctx)
930 struct list_head *prev, *cur;
932 /* Can't use list_for_each_entry() because a call to
933 * dentry_calculate_extraction_name() may delete the current dentry and
934 * its children from the list. */
938 struct wim_dentry *dentry;
942 if (cur == dentry_list)
945 dentry = list_entry(cur, struct wim_dentry, d_extraction_list_node);
947 ret = dentry_calculate_extraction_name(dentry, ctx);
951 if (prev->next == cur)
954 ; /* Current dentry and its children (which follow in
955 the list) were deleted. prev stays the same. */
961 dentry_resolve_streams(struct wim_dentry *dentry, int extract_flags,
962 struct wim_lookup_table *lookup_table)
964 struct wim_inode *inode = dentry->d_inode;
965 struct wim_lookup_table_entry *lte;
969 /* Special case: when extracting from a pipe, the WIM lookup table is
970 * initially empty, so "resolving" an inode's streams is initially not
971 * possible. However, we still need to keep track of which streams,
972 * identified by SHA1 message digests, need to be extracted, so we
973 * "resolve" the inode's streams anyway by allocating new entries. */
974 if (extract_flags & WIMLIB_EXTRACT_FLAG_FROM_PIPE)
976 ret = inode_resolve_streams(inode, lookup_table, force);
979 for (u32 i = 0; i <= inode->i_num_ads; i++) {
980 lte = inode_stream_lte_resolved(inode, i);
988 * For each dentry to be extracted, resolve all streams in the corresponding
989 * inode and set 'out_refcnt' in each to 0.
991 * Possible error codes: WIMLIB_ERR_RESOURCE_NOT_FOUND, WIMLIB_ERR_NOMEM.
994 dentry_list_resolve_streams(struct list_head *dentry_list,
995 struct apply_ctx *ctx)
997 struct wim_dentry *dentry;
1000 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
1001 ret = dentry_resolve_streams(dentry,
1003 ctx->wim->lookup_table);
1011 ref_stream(struct wim_lookup_table_entry *lte, u32 stream_idx,
1012 struct wim_dentry *dentry, struct apply_ctx *ctx)
1014 struct wim_inode *inode = dentry->d_inode;
1015 struct stream_owner *stream_owners;
1020 /* Tally the size only for each extraction of the stream (not hard
1022 if (inode->i_visited && ctx->supported_features.hard_links)
1025 ctx->progress.extract.total_bytes += lte->size;
1026 ctx->progress.extract.total_streams++;
1028 if (inode->i_visited)
1031 /* Add stream to the dentry_list only one time, even if it's going
1032 * to be extracted to multiple inodes. */
1033 if (lte->out_refcnt == 0) {
1034 list_add_tail(<e->extraction_list, &ctx->stream_list);
1035 ctx->num_streams_remaining++;
1038 /* If inode not yet been visited, append it to the stream_owners array. */
1039 if (lte->out_refcnt < ARRAY_LEN(lte->inline_stream_owners)) {
1040 stream_owners = lte->inline_stream_owners;
1042 struct stream_owner *prev_stream_owners;
1043 size_t alloc_stream_owners;
1045 if (lte->out_refcnt == ARRAY_LEN(lte->inline_stream_owners)) {
1046 prev_stream_owners = NULL;
1047 alloc_stream_owners = ARRAY_LEN(lte->inline_stream_owners);
1049 prev_stream_owners = lte->stream_owners;
1050 alloc_stream_owners = lte->alloc_stream_owners;
1053 if (lte->out_refcnt == alloc_stream_owners) {
1054 alloc_stream_owners *= 2;
1055 stream_owners = REALLOC(prev_stream_owners,
1056 alloc_stream_owners *
1057 sizeof(stream_owners[0]));
1059 return WIMLIB_ERR_NOMEM;
1060 if (!prev_stream_owners) {
1061 memcpy(stream_owners,
1062 lte->inline_stream_owners,
1063 sizeof(lte->inline_stream_owners));
1065 lte->stream_owners = stream_owners;
1066 lte->alloc_stream_owners = alloc_stream_owners;
1068 stream_owners = lte->stream_owners;
1070 stream_owners[lte->out_refcnt].inode = inode;
1071 if (stream_idx == 0) {
1072 stream_owners[lte->out_refcnt].stream_name = NULL;
1074 stream_owners[lte->out_refcnt].stream_name =
1075 inode->i_ads_entries[stream_idx - 1].stream_name;
1082 ref_unnamed_stream(struct wim_dentry *dentry, struct apply_ctx *ctx)
1084 struct wim_inode *inode = dentry->d_inode;
1087 struct wim_lookup_table_entry *stream;
1089 if (unlikely(inode_is_encrypted_directory(inode)))
1092 if (unlikely(ctx->apply_ops->will_externally_back)) {
1093 ret = (*ctx->apply_ops->will_externally_back)(dentry, ctx);
1095 if (ret) /* Error */
1097 /* Will externally back */
1100 /* Won't externally back */
1103 stream = inode_unnamed_stream_resolved(inode, &stream_idx);
1104 return ref_stream(stream, stream_idx, dentry, ctx);
1108 dentry_ref_streams(struct wim_dentry *dentry, struct apply_ctx *ctx)
1110 struct wim_inode *inode = dentry->d_inode;
1113 /* The unnamed data stream will almost always be extracted, but there
1114 * exist cases in which it won't be. */
1115 ret = ref_unnamed_stream(dentry, ctx);
1119 /* Named data streams will be extracted only if supported in the current
1120 * extraction mode and volume, and to avoid complications, if not doing
1121 * a linked extraction. */
1122 if (ctx->supported_features.named_data_streams) {
1123 for (u16 i = 0; i < inode->i_num_ads; i++) {
1124 if (!ads_entry_is_named_stream(&inode->i_ads_entries[i]))
1126 ret = ref_stream(inode->i_ads_entries[i].lte, i + 1,
1132 inode->i_visited = 1;
1137 * For each dentry to be extracted, iterate through the data streams of the
1138 * corresponding inode. For each such stream that is not to be ignored due to
1139 * the supported features or extraction flags, add it to the list of streams to
1140 * be extracted (ctx->stream_list) if not already done so.
1142 * Also builds a mapping from each stream to the inodes referencing it.
1144 * This also initializes the extract progress info with byte and stream
1147 * ctx->supported_features must be filled in.
1149 * Possible error codes: WIMLIB_ERR_NOMEM.
1152 dentry_list_ref_streams(struct list_head *dentry_list, struct apply_ctx *ctx)
1154 struct wim_dentry *dentry;
1157 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
1158 ret = dentry_ref_streams(dentry, ctx);
1162 list_for_each_entry(dentry, dentry_list, d_extraction_list_node)
1163 dentry->d_inode->i_visited = 0;
1168 dentry_list_build_inode_alias_lists(struct list_head *dentry_list)
1170 struct wim_dentry *dentry;
1171 struct wim_inode *inode;
1173 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
1174 inode = dentry->d_inode;
1175 if (!inode->i_visited)
1176 INIT_LIST_HEAD(&inode->i_extraction_aliases);
1177 list_add_tail(&dentry->d_extraction_alias_node,
1178 &inode->i_extraction_aliases);
1179 inode->i_visited = 1;
1181 list_for_each_entry(dentry, dentry_list, d_extraction_list_node)
1182 dentry->d_inode->i_visited = 0;
1186 inode_tally_features(const struct wim_inode *inode,
1187 struct wim_features *features)
1189 if (inode->i_attributes & FILE_ATTRIBUTE_ARCHIVE)
1190 features->archive_files++;
1191 if (inode->i_attributes & FILE_ATTRIBUTE_HIDDEN)
1192 features->hidden_files++;
1193 if (inode->i_attributes & FILE_ATTRIBUTE_SYSTEM)
1194 features->system_files++;
1195 if (inode->i_attributes & FILE_ATTRIBUTE_COMPRESSED)
1196 features->compressed_files++;
1197 if (inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED) {
1198 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)
1199 features->encrypted_directories++;
1201 features->encrypted_files++;
1203 if (inode->i_attributes & FILE_ATTRIBUTE_NOT_CONTENT_INDEXED)
1204 features->not_context_indexed_files++;
1205 if (inode->i_attributes & FILE_ATTRIBUTE_SPARSE_FILE)
1206 features->sparse_files++;
1207 if (inode_has_named_stream(inode))
1208 features->named_data_streams++;
1209 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1210 features->reparse_points++;
1211 if (inode_is_symlink(inode))
1212 features->symlink_reparse_points++;
1214 features->other_reparse_points++;
1216 if (inode->i_security_id != -1)
1217 features->security_descriptors++;
1218 if (inode_has_unix_data(inode))
1219 features->unix_data++;
1222 /* Tally features necessary to extract a dentry and the corresponding inode. */
1224 dentry_tally_features(struct wim_dentry *dentry, struct wim_features *features)
1226 struct wim_inode *inode = dentry->d_inode;
1228 if (dentry_has_short_name(dentry))
1229 features->short_names++;
1231 if (inode->i_visited) {
1232 features->hard_links++;
1234 inode_tally_features(inode, features);
1235 inode->i_visited = 1;
1239 /* Tally the features necessary to extract the specified dentries. */
1241 dentry_list_get_features(struct list_head *dentry_list,
1242 struct wim_features *features)
1244 struct wim_dentry *dentry;
1246 list_for_each_entry(dentry, dentry_list, d_extraction_list_node)
1247 dentry_tally_features(dentry, features);
1249 list_for_each_entry(dentry, dentry_list, d_extraction_list_node)
1250 dentry->d_inode->i_visited = 0;
1254 do_feature_check(const struct wim_features *required_features,
1255 const struct wim_features *supported_features,
1258 /* File attributes. */
1259 if (!(extract_flags & WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES)) {
1260 /* Note: Don't bother the user about FILE_ATTRIBUTE_ARCHIVE.
1261 * We're an archive program, so theoretically we can do what we
1264 if (required_features->hidden_files &&
1265 !supported_features->hidden_files)
1266 WARNING("Ignoring FILE_ATTRIBUTE_HIDDEN of %lu files",
1267 required_features->hidden_files);
1269 if (required_features->system_files &&
1270 !supported_features->system_files)
1271 WARNING("Ignoring FILE_ATTRIBUTE_SYSTEM of %lu files",
1272 required_features->system_files);
1274 if (required_features->compressed_files &&
1275 !supported_features->compressed_files)
1276 WARNING("Ignoring FILE_ATTRIBUTE_COMPRESSED of %lu files",
1277 required_features->compressed_files);
1279 if (required_features->not_context_indexed_files &&
1280 !supported_features->not_context_indexed_files)
1281 WARNING("Ignoring FILE_ATTRIBUTE_NOT_CONTENT_INDEXED of %lu files",
1282 required_features->not_context_indexed_files);
1284 if (required_features->sparse_files &&
1285 !supported_features->sparse_files)
1286 WARNING("Ignoring FILE_ATTRIBUTE_SPARSE_FILE of %lu files",
1287 required_features->sparse_files);
1289 if (required_features->encrypted_directories &&
1290 !supported_features->encrypted_directories)
1291 WARNING("Ignoring FILE_ATTRIBUTE_ENCRYPTED of %lu directories",
1292 required_features->encrypted_directories);
1295 /* Encrypted files. */
1296 if (required_features->encrypted_files &&
1297 !supported_features->encrypted_files)
1298 WARNING("Ignoring %lu encrypted files",
1299 required_features->encrypted_files);
1301 /* Named data streams. */
1302 if (required_features->named_data_streams &&
1303 (!supported_features->named_data_streams))
1304 WARNING("Ignoring named data streams of %lu files",
1305 required_features->named_data_streams);
1308 if (required_features->hard_links && !supported_features->hard_links)
1309 WARNING("Extracting %lu hard links as independent files",
1310 required_features->hard_links);
1312 /* Symbolic links and reparse points. */
1313 if ((extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS) &&
1314 required_features->symlink_reparse_points &&
1315 !supported_features->symlink_reparse_points &&
1316 !supported_features->reparse_points)
1318 ERROR("Extraction backend does not support symbolic links!");
1319 return WIMLIB_ERR_UNSUPPORTED;
1321 if (required_features->reparse_points &&
1322 !supported_features->reparse_points)
1324 if (supported_features->symlink_reparse_points) {
1325 if (required_features->other_reparse_points) {
1326 WARNING("Ignoring %lu non-symlink/junction "
1327 "reparse point files",
1328 required_features->other_reparse_points);
1331 WARNING("Ignoring %lu reparse point files",
1332 required_features->reparse_points);
1336 /* Security descriptors. */
1337 if (((extract_flags & (WIMLIB_EXTRACT_FLAG_STRICT_ACLS |
1338 WIMLIB_EXTRACT_FLAG_UNIX_DATA))
1339 == WIMLIB_EXTRACT_FLAG_STRICT_ACLS) &&
1340 required_features->security_descriptors &&
1341 !supported_features->security_descriptors)
1343 ERROR("Extraction backend does not support security descriptors!");
1344 return WIMLIB_ERR_UNSUPPORTED;
1346 if (!(extract_flags & WIMLIB_EXTRACT_FLAG_NO_ACLS) &&
1347 required_features->security_descriptors &&
1348 !supported_features->security_descriptors)
1349 WARNING("Ignoring Windows NT security descriptors of %lu files",
1350 required_features->security_descriptors);
1353 if ((extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) &&
1354 required_features->unix_data && !supported_features->unix_data)
1356 ERROR("Extraction backend does not support UNIX data!");
1357 return WIMLIB_ERR_UNSUPPORTED;
1360 if (required_features->unix_data &&
1361 !(extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA))
1363 WARNING("Ignoring UNIX metadata of %lu files",
1364 required_features->unix_data);
1368 if (required_features->short_names &&
1369 !supported_features->short_names)
1371 if (extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_SHORT_NAMES) {
1372 ERROR("Extraction backend does not support DOS names!");
1373 return WIMLIB_ERR_UNSUPPORTED;
1375 WARNING("Ignoring DOS names of %lu files",
1376 required_features->short_names);
1380 if ((extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_TIMESTAMPS) &&
1381 !supported_features->timestamps)
1383 ERROR("Extraction backend does not support timestamps!");
1384 return WIMLIB_ERR_UNSUPPORTED;
1390 static const struct apply_operations *
1391 select_apply_operations(int extract_flags)
1394 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS)
1395 return &ntfs_3g_apply_ops;
1398 return &win32_apply_ops;
1400 return &unix_apply_ops;
1405 extract_trees(WIMStruct *wim, struct wim_dentry **trees, size_t num_trees,
1406 const tchar *target, int extract_flags)
1408 const struct apply_operations *ops;
1409 struct apply_ctx *ctx;
1411 LIST_HEAD(dentry_list);
1413 if (extract_flags & WIMLIB_EXTRACT_FLAG_TO_STDOUT) {
1414 ret = extract_dentries_to_stdout(trees, num_trees,
1419 num_trees = remove_duplicate_trees(trees, num_trees);
1420 num_trees = remove_contained_trees(trees, num_trees);
1422 ops = select_apply_operations(extract_flags);
1424 if (num_trees > 1 && ops->single_tree_only) {
1425 ERROR("Extracting multiple directory trees "
1426 "at once is not supported in %s extraction mode!",
1428 ret = WIMLIB_ERR_UNSUPPORTED;
1432 ctx = CALLOC(1, ops->context_size);
1434 ret = WIMLIB_ERR_NOMEM;
1439 ctx->target = target;
1440 ctx->target_nchars = tstrlen(target);
1441 ctx->extract_flags = extract_flags;
1442 if (ctx->wim->progfunc) {
1443 ctx->progfunc = ctx->wim->progfunc;
1444 ctx->progctx = ctx->wim->progctx;
1445 ctx->progress.extract.image = wim->current_image;
1446 ctx->progress.extract.extract_flags = (extract_flags &
1447 WIMLIB_EXTRACT_MASK_PUBLIC);
1448 ctx->progress.extract.wimfile_name = wim->filename;
1449 ctx->progress.extract.image_name = wimlib_get_image_name(wim,
1450 wim->current_image);
1451 ctx->progress.extract.target = target;
1453 INIT_LIST_HEAD(&ctx->stream_list);
1454 filedes_invalidate(&ctx->tmpfile_fd);
1455 ctx->apply_ops = ops;
1457 ret = (*ops->get_supported_features)(target, &ctx->supported_features);
1461 build_dentry_list(&dentry_list, trees, num_trees,
1463 WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE));
1465 dentry_list_get_features(&dentry_list, &ctx->required_features);
1467 ret = do_feature_check(&ctx->required_features, &ctx->supported_features,
1468 ctx->extract_flags);
1472 ret = dentry_list_calculate_extraction_names(&dentry_list, ctx);
1476 ret = dentry_list_resolve_streams(&dentry_list, ctx);
1480 dentry_list_build_inode_alias_lists(&dentry_list);
1482 ret = dentry_list_ref_streams(&dentry_list, ctx);
1486 if (extract_flags & WIMLIB_EXTRACT_FLAG_FROM_PIPE) {
1487 /* When extracting from a pipe, the number of bytes of data to
1488 * extract can't be determined in the normal way (examining the
1489 * lookup table), since at this point all we have is a set of
1490 * SHA1 message digests of streams that need to be extracted.
1491 * However, we can get a reasonably accurate estimate by taking
1492 * <TOTALBYTES> from the corresponding <IMAGE> in the WIM XML
1493 * data. This does assume that a full image is being extracted,
1494 * but currently there is no API for doing otherwise. (Also,
1495 * subtract <HARDLINKBYTES> from this if hard links are
1496 * supported by the extraction mode.) */
1497 ctx->progress.extract.total_bytes =
1498 wim_info_get_image_total_bytes(wim->wim_info,
1499 wim->current_image);
1500 if (ctx->supported_features.hard_links) {
1501 ctx->progress.extract.total_bytes -=
1502 wim_info_get_image_hard_link_bytes(wim->wim_info,
1503 wim->current_image);
1507 ret = extract_progress(ctx,
1508 ((extract_flags & WIMLIB_EXTRACT_FLAG_IMAGEMODE) ?
1509 WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN :
1510 WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN));
1514 ret = (*ops->extract)(&dentry_list, ctx);
1518 if (ctx->progress.extract.completed_bytes <
1519 ctx->progress.extract.total_bytes)
1521 ctx->progress.extract.completed_bytes =
1522 ctx->progress.extract.total_bytes;
1523 ret = extract_progress(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS);
1528 ret = extract_progress(ctx,
1529 ((extract_flags & WIMLIB_EXTRACT_FLAG_IMAGEMODE) ?
1530 WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END :
1531 WIMLIB_PROGRESS_MSG_EXTRACT_TREE_END));
1533 destroy_stream_list(&ctx->stream_list);
1534 destroy_dentry_list(&dentry_list);
1541 mkdir_if_needed(const tchar *target)
1543 if (!tmkdir(target, 0755))
1546 if (errno == EEXIST)
1550 /* _wmkdir() fails with EACCES if called on a drive root directory. */
1551 if (errno == EACCES)
1555 ERROR_WITH_ERRNO("Failed to create directory \"%"TS"\"", target);
1556 return WIMLIB_ERR_MKDIR;
1559 /* Make sure the extraction flags make sense, and update them if needed. */
1561 check_extract_flags(const WIMStruct *wim, int *extract_flags_p)
1563 int extract_flags = *extract_flags_p;
1565 /* Check for invalid flag combinations */
1567 if ((extract_flags &
1568 (WIMLIB_EXTRACT_FLAG_NO_ACLS |
1569 WIMLIB_EXTRACT_FLAG_STRICT_ACLS)) == (WIMLIB_EXTRACT_FLAG_NO_ACLS |
1570 WIMLIB_EXTRACT_FLAG_STRICT_ACLS))
1571 return WIMLIB_ERR_INVALID_PARAM;
1573 if ((extract_flags &
1574 (WIMLIB_EXTRACT_FLAG_RPFIX |
1575 WIMLIB_EXTRACT_FLAG_NORPFIX)) == (WIMLIB_EXTRACT_FLAG_RPFIX |
1576 WIMLIB_EXTRACT_FLAG_NORPFIX))
1577 return WIMLIB_ERR_INVALID_PARAM;
1579 #ifndef WITH_NTFS_3G
1580 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1581 ERROR("wimlib was compiled without support for NTFS-3g, so\n"
1582 " it cannot apply a WIM image directly to an NTFS volume.");
1583 return WIMLIB_ERR_UNSUPPORTED;
1587 if (extract_flags & WIMLIB_EXTRACT_FLAG_WIMBOOT) {
1590 return WIMLIB_ERR_NO_FILENAME;
1592 ERROR("WIMBoot extraction is only supported on Windows!");
1593 return WIMLIB_ERR_UNSUPPORTED;
1598 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_RPFIX |
1599 WIMLIB_EXTRACT_FLAG_NORPFIX |
1600 WIMLIB_EXTRACT_FLAG_IMAGEMODE)) ==
1601 WIMLIB_EXTRACT_FLAG_IMAGEMODE)
1603 /* For full-image extraction, do reparse point fixups by default
1604 * if the WIM header says they are enabled. */
1605 if (wim->hdr.flags & WIM_HDR_FLAG_RP_FIX)
1606 extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
1609 *extract_flags_p = extract_flags;
1614 get_wildcard_flags(int extract_flags)
1616 u32 wildcard_flags = 0;
1618 if (extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_GLOB)
1619 wildcard_flags |= WILDCARD_FLAG_ERROR_IF_NO_MATCH;
1621 wildcard_flags |= WILDCARD_FLAG_WARN_IF_NO_MATCH;
1623 if (default_ignore_case)
1624 wildcard_flags |= WILDCARD_FLAG_CASE_INSENSITIVE;
1626 return wildcard_flags;
1629 struct append_dentry_ctx {
1630 struct wim_dentry **dentries;
1631 size_t num_dentries;
1632 size_t num_alloc_dentries;
1636 append_dentry_cb(struct wim_dentry *dentry, void *_ctx)
1638 struct append_dentry_ctx *ctx = _ctx;
1640 if (ctx->num_dentries == ctx->num_alloc_dentries) {
1641 struct wim_dentry **new_dentries;
1644 new_length = max(ctx->num_alloc_dentries + 8,
1645 ctx->num_alloc_dentries * 3 / 2);
1646 new_dentries = REALLOC(ctx->dentries,
1647 new_length * sizeof(ctx->dentries[0]));
1648 if (new_dentries == NULL)
1649 return WIMLIB_ERR_NOMEM;
1650 ctx->dentries = new_dentries;
1651 ctx->num_alloc_dentries = new_length;
1653 ctx->dentries[ctx->num_dentries++] = dentry;
1658 do_wimlib_extract_paths(WIMStruct *wim, int image, const tchar *target,
1659 const tchar * const *paths, size_t num_paths,
1663 struct wim_dentry **trees;
1666 if (wim == NULL || target == NULL || target[0] == T('\0') ||
1667 (num_paths != 0 && paths == NULL))
1668 return WIMLIB_ERR_INVALID_PARAM;
1670 ret = check_extract_flags(wim, &extract_flags);
1674 ret = select_wim_image(wim, image);
1678 ret = wim_checksum_unhashed_streams(wim);
1682 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_NTFS |
1683 WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE)) ==
1684 (WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE))
1686 ret = mkdir_if_needed(target);
1691 if (extract_flags & WIMLIB_EXTRACT_FLAG_GLOB_PATHS) {
1693 struct append_dentry_ctx append_dentry_ctx = {
1696 .num_alloc_dentries = 0,
1699 u32 wildcard_flags = get_wildcard_flags(extract_flags);
1701 for (size_t i = 0; i < num_paths; i++) {
1702 tchar *path = canonicalize_wim_path(paths[i]);
1704 ret = WIMLIB_ERR_NOMEM;
1705 trees = append_dentry_ctx.dentries;
1706 goto out_free_trees;
1708 ret = expand_wildcard(wim, path,
1714 trees = append_dentry_ctx.dentries;
1715 goto out_free_trees;
1718 trees = append_dentry_ctx.dentries;
1719 num_trees = append_dentry_ctx.num_dentries;
1721 trees = MALLOC(num_paths * sizeof(trees[0]));
1723 return WIMLIB_ERR_NOMEM;
1725 for (size_t i = 0; i < num_paths; i++) {
1727 tchar *path = canonicalize_wim_path(paths[i]);
1729 ret = WIMLIB_ERR_NOMEM;
1730 goto out_free_trees;
1733 trees[i] = get_dentry(wim, path,
1734 WIMLIB_CASE_PLATFORM_DEFAULT);
1736 if (trees[i] == NULL) {
1737 ERROR("Path \"%"TS"\" does not exist "
1739 paths[i], wim->current_image);
1740 ret = WIMLIB_ERR_PATH_DOES_NOT_EXIST;
1741 goto out_free_trees;
1744 num_trees = num_paths;
1747 if (num_trees == 0) {
1749 goto out_free_trees;
1752 ret = extract_trees(wim, trees, num_trees, target, extract_flags);
1759 extract_single_image(WIMStruct *wim, int image,
1760 const tchar *target, int extract_flags)
1762 const tchar *path = WIMLIB_WIM_ROOT_PATH;
1763 extract_flags |= WIMLIB_EXTRACT_FLAG_IMAGEMODE;
1764 return do_wimlib_extract_paths(wim, image, target, &path, 1, extract_flags);
1767 static const tchar * const filename_forbidden_chars =
1776 /* This function checks if it is okay to use a WIM image's name as a directory
1779 image_name_ok_as_dir(const tchar *image_name)
1781 return image_name && *image_name &&
1782 !tstrpbrk(image_name, filename_forbidden_chars) &&
1783 tstrcmp(image_name, T(".")) &&
1784 tstrcmp(image_name, T(".."));
1787 /* Extracts all images from the WIM to the directory @target, with the images
1788 * placed in subdirectories named by their image names. */
1790 extract_all_images(WIMStruct *wim, const tchar *target, int extract_flags)
1792 size_t image_name_max_len = max(xml_get_max_image_name_len(wim), 20);
1793 size_t output_path_len = tstrlen(target);
1794 tchar buf[output_path_len + 1 + image_name_max_len + 1];
1797 const tchar *image_name;
1799 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1800 ERROR("Cannot extract multiple images in NTFS extraction mode.");
1801 return WIMLIB_ERR_INVALID_PARAM;
1804 ret = mkdir_if_needed(target);
1807 tmemcpy(buf, target, output_path_len);
1808 buf[output_path_len] = OS_PREFERRED_PATH_SEPARATOR;
1809 for (image = 1; image <= wim->hdr.image_count; image++) {
1810 image_name = wimlib_get_image_name(wim, image);
1811 if (image_name_ok_as_dir(image_name)) {
1812 tstrcpy(buf + output_path_len + 1, image_name);
1814 /* Image name is empty or contains forbidden characters.
1815 * Use image number instead. */
1816 tsprintf(buf + output_path_len + 1, T("%d"), image);
1818 ret = extract_single_image(wim, image, buf, extract_flags);
1826 do_wimlib_extract_image(WIMStruct *wim, int image, const tchar *target,
1829 if (extract_flags & (WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE |
1830 WIMLIB_EXTRACT_FLAG_TO_STDOUT |
1831 WIMLIB_EXTRACT_FLAG_GLOB_PATHS))
1832 return WIMLIB_ERR_INVALID_PARAM;
1834 if (image == WIMLIB_ALL_IMAGES)
1835 return extract_all_images(wim, target, extract_flags);
1837 return extract_single_image(wim, image, target, extract_flags);
1841 /****************************************************************************
1843 ****************************************************************************/
1846 wimlib_extract_paths(WIMStruct *wim, int image, const tchar *target,
1847 const tchar * const *paths, size_t num_paths,
1850 if (extract_flags & ~WIMLIB_EXTRACT_MASK_PUBLIC)
1851 return WIMLIB_ERR_INVALID_PARAM;
1853 return do_wimlib_extract_paths(wim, image, target, paths, num_paths,
1858 wimlib_extract_pathlist(WIMStruct *wim, int image, const tchar *target,
1859 const tchar *path_list_file, int extract_flags)
1866 ret = read_path_list_file(path_list_file, &paths, &num_paths, &mem);
1868 ERROR("Failed to read path list file \"%"TS"\"",
1873 ret = wimlib_extract_paths(wim, image, target,
1874 (const tchar * const *)paths, num_paths,
1882 wimlib_extract_image_from_pipe_with_progress(int pipe_fd,
1883 const tchar *image_num_or_name,
1884 const tchar *target,
1886 wimlib_progress_func_t progfunc,
1891 struct filedes *in_fd;
1895 if (extract_flags & ~WIMLIB_EXTRACT_MASK_PUBLIC)
1896 return WIMLIB_ERR_INVALID_PARAM;
1898 /* Read the WIM header from the pipe and get a WIMStruct to represent
1899 * the pipable WIM. Caveats: Unlike getting a WIMStruct with
1900 * wimlib_open_wim(), getting a WIMStruct in this way will result in
1901 * an empty lookup table, no XML data read, and no filename set. */
1902 ret = open_wim_as_WIMStruct(&pipe_fd, WIMLIB_OPEN_FLAG_FROM_PIPE, &pwm,
1907 /* Sanity check to make sure this is a pipable WIM. */
1908 if (pwm->hdr.magic != PWM_MAGIC) {
1909 ERROR("The WIM being read from file descriptor %d "
1910 "is not pipable!", pipe_fd);
1911 ret = WIMLIB_ERR_NOT_PIPABLE;
1912 goto out_wimlib_free;
1915 /* Sanity check to make sure the first part of a pipable split WIM is
1916 * sent over the pipe first. */
1917 if (pwm->hdr.part_number != 1) {
1918 ERROR("The first part of the split WIM must be "
1919 "sent over the pipe first.");
1920 ret = WIMLIB_ERR_INVALID_PIPABLE_WIM;
1921 goto out_wimlib_free;
1924 in_fd = &pwm->in_fd;
1925 wimlib_assert(in_fd->offset == WIM_HEADER_DISK_SIZE);
1927 /* As mentioned, the WIMStruct we created from the pipe does not have
1928 * XML data yet. Fix this by reading the extra copy of the XML data
1929 * that directly follows the header in pipable WIMs. (Note: see
1930 * write_pipable_wim() for more details about the format of pipable
1933 struct wim_lookup_table_entry xml_lte;
1934 struct wim_resource_spec xml_rspec;
1935 ret = read_pwm_stream_header(pwm, &xml_lte, &xml_rspec, 0, NULL);
1937 goto out_wimlib_free;
1939 if (!(xml_lte.flags & WIM_RESHDR_FLAG_METADATA))
1941 ERROR("Expected XML data, but found non-metadata "
1943 ret = WIMLIB_ERR_INVALID_PIPABLE_WIM;
1944 goto out_wimlib_free;
1947 wim_res_spec_to_hdr(&xml_rspec, &pwm->hdr.xml_data_reshdr);
1949 ret = read_wim_xml_data(pwm);
1951 goto out_wimlib_free;
1953 if (wim_info_get_num_images(pwm->wim_info) != pwm->hdr.image_count) {
1954 ERROR("Image count in XML data is not the same as in WIM header.");
1955 ret = WIMLIB_ERR_IMAGE_COUNT;
1956 goto out_wimlib_free;
1960 /* Get image index (this may use the XML data that was just read to
1961 * resolve an image name). */
1962 if (image_num_or_name) {
1963 image = wimlib_resolve_image(pwm, image_num_or_name);
1964 if (image == WIMLIB_NO_IMAGE) {
1965 ERROR("\"%"TS"\" is not a valid image in the pipable WIM!",
1967 ret = WIMLIB_ERR_INVALID_IMAGE;
1968 goto out_wimlib_free;
1969 } else if (image == WIMLIB_ALL_IMAGES) {
1970 ERROR("Applying all images from a pipe is not supported!");
1971 ret = WIMLIB_ERR_INVALID_IMAGE;
1972 goto out_wimlib_free;
1975 if (pwm->hdr.image_count != 1) {
1976 ERROR("No image was specified, but the pipable WIM "
1977 "did not contain exactly 1 image");
1978 ret = WIMLIB_ERR_INVALID_IMAGE;
1979 goto out_wimlib_free;
1984 /* Load the needed metadata resource. */
1985 for (i = 1; i <= pwm->hdr.image_count; i++) {
1986 struct wim_lookup_table_entry *metadata_lte;
1987 struct wim_image_metadata *imd;
1988 struct wim_resource_spec *metadata_rspec;
1990 metadata_lte = new_lookup_table_entry();
1991 if (metadata_lte == NULL) {
1992 ret = WIMLIB_ERR_NOMEM;
1993 goto out_wimlib_free;
1995 metadata_rspec = MALLOC(sizeof(struct wim_resource_spec));
1996 if (metadata_rspec == NULL) {
1997 ret = WIMLIB_ERR_NOMEM;
1998 free_lookup_table_entry(metadata_lte);
1999 goto out_wimlib_free;
2002 ret = read_pwm_stream_header(pwm, metadata_lte, metadata_rspec, 0, NULL);
2003 imd = pwm->image_metadata[i - 1];
2004 imd->metadata_lte = metadata_lte;
2006 FREE(metadata_rspec);
2007 goto out_wimlib_free;
2010 if (!(metadata_lte->flags & WIM_RESHDR_FLAG_METADATA)) {
2011 ERROR("Expected metadata resource, but found "
2012 "non-metadata stream.");
2013 ret = WIMLIB_ERR_INVALID_PIPABLE_WIM;
2014 goto out_wimlib_free;
2018 /* Metadata resource is for the image being extracted.
2019 * Parse it and save the metadata in memory. */
2020 ret = read_metadata_resource(pwm, imd);
2022 goto out_wimlib_free;
2025 /* Metadata resource is not for the image being
2026 * extracted. Skip over it. */
2027 ret = skip_wim_stream(metadata_lte);
2029 goto out_wimlib_free;
2032 /* Extract the image. */
2033 extract_flags |= WIMLIB_EXTRACT_FLAG_FROM_PIPE;
2034 ret = do_wimlib_extract_image(pwm, image, target, extract_flags);
2035 /* Clean up and return. */
2043 wimlib_extract_image_from_pipe(int pipe_fd, const tchar *image_num_or_name,
2044 const tchar *target, int extract_flags)
2046 return wimlib_extract_image_from_pipe_with_progress(pipe_fd,
2055 wimlib_extract_image(WIMStruct *wim, int image, const tchar *target,
2058 if (extract_flags & ~WIMLIB_EXTRACT_MASK_PUBLIC)
2059 return WIMLIB_ERR_INVALID_PARAM;
2060 return do_wimlib_extract_image(wim, image, target, extract_flags);