4 * Support for extracting WIM images, or files or directories contained in a WIM
9 * Copyright (C) 2012, 2013 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_files(), and wimlib_extract_image_from_pipe(). Internally,
30 * all end up calling extract_tree() zero or more times to extract a tree of
31 * files from the currently selected WIM image to the specified target directory
34 * Although wimlib supports multiple extraction modes/backends (NTFS-3g, UNIX,
35 * Win32), this file does not itself have code to extract files or directories
36 * to any specific target; instead, it handles generic functionality and relies
37 * on lower-level callback functions declared in `struct apply_operations' to do
38 * the actual extraction.
45 #include "wimlib/apply.h"
46 #include "wimlib/dentry.h"
47 #include "wimlib/encoding.h"
48 #include "wimlib/endianness.h"
49 #include "wimlib/error.h"
50 #include "wimlib/lookup_table.h"
51 #include "wimlib/metadata.h"
52 #include "wimlib/pathlist.h"
53 #include "wimlib/paths.h"
54 #include "wimlib/reparse.h"
55 #include "wimlib/resource.h"
56 #include "wimlib/security.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_MULTI_IMAGE 0x80000000
71 #define WIMLIB_EXTRACT_FLAG_FROM_PIPE 0x40000000
72 #define WIMLIB_EXTRACT_FLAG_FILEMODE 0x20000000
73 #define WIMLIB_EXTRACT_FLAG_IMAGEMODE 0x10000000
74 #define WIMLIB_EXTRACT_MASK_PUBLIC 0x0fffffff
76 /* Given a WIM dentry in the tree to be extracted, resolve all streams in the
77 * corresponding inode and set 'out_refcnt' in each to 0. */
79 dentry_resolve_and_zero_lte_refcnt(struct wim_dentry *dentry, void *_ctx)
81 struct apply_ctx *ctx = _ctx;
82 struct wim_inode *inode = dentry->d_inode;
83 struct wim_lookup_table_entry *lte;
87 if (dentry->extraction_skipped)
90 /* Special case: when extracting from a pipe, the WIM lookup table is
91 * initially empty, so "resolving" an inode's streams is initially not
92 * possible. However, we still need to keep track of which streams,
93 * identified by SHA1 message digests, need to be extracted, so we
94 * "resolve" the inode's streams anyway by allocating new entries. */
95 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_FROM_PIPE)
97 ret = inode_resolve_streams(inode, ctx->wim->lookup_table, force);
100 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
101 lte = inode_stream_lte_resolved(inode, i);
109 is_linked_extraction(const struct apply_ctx *ctx)
111 return 0 != (ctx->extract_flags & (WIMLIB_EXTRACT_FLAG_HARDLINK |
112 WIMLIB_EXTRACT_FLAG_SYMLINK));
116 can_extract_named_data_streams(const struct apply_ctx *ctx)
118 return ctx->supported_features.named_data_streams &&
119 !is_linked_extraction(ctx);
123 ref_stream_to_extract(struct wim_lookup_table_entry *lte,
124 struct wim_dentry *dentry, struct apply_ctx *ctx)
129 /* Tally the size only for each extraction of the stream (not hard
131 if (!(dentry->d_inode->i_visited &&
132 ctx->supported_features.hard_links) &&
133 (!is_linked_extraction(ctx) || (lte->out_refcnt == 0 &&
134 lte->extracted_file == NULL)))
136 ctx->progress.extract.total_bytes += lte->size;
137 ctx->progress.extract.num_streams++;
140 /* Add stream to the extraction_list only one time, even if it's going
141 * to be extracted to multiple locations. */
142 if (lte->out_refcnt == 0) {
143 list_add_tail(<e->extraction_list, &ctx->stream_list);
144 ctx->num_streams_remaining++;
147 if (!(ctx->extract_flags & WIMLIB_EXTRACT_FLAG_FILE_ORDER)) {
148 struct wim_dentry **lte_dentries;
150 /* Append dentry to this stream's array of dentries referencing
151 * it. Use inline array to avoid memory allocation until the
152 * number of dentries becomes too large. */
153 if (lte->out_refcnt < ARRAY_LEN(lte->inline_lte_dentries)) {
154 lte_dentries = lte->inline_lte_dentries;
156 struct wim_dentry **prev_lte_dentries;
157 size_t alloc_lte_dentries;
159 if (lte->out_refcnt == ARRAY_LEN(lte->inline_lte_dentries)) {
160 prev_lte_dentries = NULL;
161 alloc_lte_dentries = ARRAY_LEN(lte->inline_lte_dentries);
163 prev_lte_dentries = lte->lte_dentries;
164 alloc_lte_dentries = lte->alloc_lte_dentries;
167 if (lte->out_refcnt == alloc_lte_dentries) {
168 alloc_lte_dentries *= 2;
169 lte_dentries = REALLOC(prev_lte_dentries,
171 sizeof(lte_dentries[0]));
172 if (lte_dentries == NULL)
173 return WIMLIB_ERR_NOMEM;
174 if (prev_lte_dentries == NULL) {
176 lte->inline_lte_dentries,
177 sizeof(lte->inline_lte_dentries));
179 lte->lte_dentries = lte_dentries;
180 lte->alloc_lte_dentries = alloc_lte_dentries;
182 lte_dentries = lte->lte_dentries;
184 lte_dentries[lte->out_refcnt] = dentry;
190 /* Given a WIM dentry in the tree to be extracted, iterate through streams that
191 * need to be extracted. For each one, add it to the list of streams to be
192 * extracted (ctx->stream_list) if not already done so, and also update the
193 * progress information (ctx->progress) with the stream. Furthermore, if doing
194 * a sequential extraction, build a mapping from each the stream to the dentries
197 * This uses the i_visited member of the inodes (assumed to be 0 initially). */
199 dentry_add_streams_to_extract(struct wim_dentry *dentry, void *_ctx)
201 struct apply_ctx *ctx = _ctx;
202 struct wim_inode *inode = dentry->d_inode;
205 /* Don't process dentries marked as skipped. */
206 if (dentry->extraction_skipped)
209 /* The unnamed data stream will always be extracted, except in an
211 if (!inode_is_encrypted_directory(inode)) {
212 ret = ref_stream_to_extract(inode_unnamed_lte_resolved(inode),
218 /* Named data streams will be extracted only if supported in the current
219 * extraction mode and volume, and to avoid complications, if not doing
220 * a linked extraction. */
221 if (can_extract_named_data_streams(ctx)) {
222 for (u16 i = 0; i < inode->i_num_ads; i++) {
223 if (!ads_entry_is_named_stream(&inode->i_ads_entries[i]))
225 ret = ref_stream_to_extract(inode->i_ads_entries[i].lte,
231 inode->i_visited = 1;
235 /* Inform library user of progress of stream extraction following the successful
236 * extraction of a copy of the stream specified by @lte. */
238 update_extract_progress(struct apply_ctx *ctx,
239 const struct wim_lookup_table_entry *lte)
241 wimlib_progress_func_t progress_func = ctx->progress_func;
242 union wimlib_progress_info *progress = &ctx->progress;
244 progress->extract.completed_bytes += lte->size;
246 progress->extract.completed_bytes >= ctx->next_progress)
248 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS, progress);
249 if (progress->extract.completed_bytes >=
250 progress->extract.total_bytes)
252 ctx->next_progress = ~0ULL;
254 ctx->next_progress += progress->extract.total_bytes / 128;
255 if (ctx->next_progress > progress->extract.total_bytes)
256 ctx->next_progress = progress->extract.total_bytes;
262 /* Extract a symbolic link (not directly as reparse data), handling fixing up
263 * the target of absolute symbolic links and updating the extract progress.
265 * @inode must specify the WIM inode for a symbolic link or junction reparse
268 * @lte_override overrides the resource used as the reparse data for the
271 extract_symlink(const tchar *path, struct apply_ctx *ctx,
272 struct wim_inode *inode,
273 struct wim_lookup_table_entry *lte_override)
275 ssize_t bufsize = ctx->ops->path_max;
276 tchar target[bufsize];
282 /* If absolute symbolic link fixups requested, reserve space in the link
283 * target buffer for the absolute path of the target directory. */
284 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX)
286 buf += ctx->realtarget_nchars;
287 bufsize -= ctx->realtarget_nchars;
290 /* Translate the WIM inode's reparse data into the link target. */
291 sret = wim_inode_readlink(inode, buf, bufsize - 1, lte_override);
294 return WIMLIB_ERR_READLINK;
298 if ((ctx->extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX) &&
301 /* Fix absolute symbolic link target to point into the
302 * actual extraction destination. */
303 tmemcpy(target, ctx->realtarget, ctx->realtarget_nchars);
304 fixed_target = target;
306 /* Keep same link target. */
310 /* Call into the apply_operations to create the symbolic link. */
311 DEBUG("Creating symlink \"%"TS"\" => \"%"TS"\"",
313 ret = ctx->ops->create_symlink(fixed_target, path, ctx);
315 ERROR_WITH_ERRNO("Failed to create symlink "
316 "\"%"TS"\" => \"%"TS"\"", path, fixed_target);
320 /* Account for reparse data consumed. */
321 update_extract_progress(ctx,
322 (lte_override ? lte_override :
323 inode_unnamed_lte_resolved(inode)));
326 #endif /* !__WIN32__ */
328 /* Create a file, directory, or symbolic link. */
330 extract_inode(const tchar *path, struct apply_ctx *ctx, struct wim_inode *inode)
335 if (ctx->supported_features.symlink_reparse_points &&
336 !ctx->supported_features.reparse_points &&
337 inode_is_symlink(inode))
339 ret = extract_symlink(path, ctx, inode, NULL);
341 #endif /* !__WIN32__ */
342 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY) {
343 ret = ctx->ops->create_directory(path, ctx, &inode->extract_cookie);
345 ERROR_WITH_ERRNO("Failed to create the directory "
348 } else if ((inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED) &&
349 ctx->ops->extract_encrypted_stream_creates_file &&
350 ctx->supported_features.encrypted_files) {
351 ret = ctx->ops->extract_encrypted_stream(
352 path, inode_unnamed_lte_resolved(inode), ctx);
354 ERROR_WITH_ERRNO("Failed to create and extract "
355 "encrypted file \"%"TS"\"", path);
358 ret = ctx->ops->create_file(path, ctx, &inode->extract_cookie);
360 ERROR_WITH_ERRNO("Failed to create the file "
368 extract_hardlink(const tchar *oldpath, const tchar *newpath,
369 struct apply_ctx *ctx)
373 DEBUG("Creating hardlink \"%"TS"\" => \"%"TS"\"", newpath, oldpath);
374 ret = ctx->ops->create_hardlink(oldpath, newpath, ctx);
376 ERROR_WITH_ERRNO("Failed to create hardlink "
377 "\"%"TS"\" => \"%"TS"\"",
385 try_extract_rpfix(u8 *rpbuf,
387 const wchar_t *extract_root_realpath,
388 unsigned extract_root_realpath_nchars)
390 struct reparse_data rpdata;
392 size_t target_nchars;
393 size_t stripped_nchars;
394 wchar_t *stripped_target;
395 wchar_t stripped_target_nchars;
398 utf16lechar *new_target;
399 utf16lechar *new_print_name;
400 size_t new_target_nchars;
401 size_t new_print_name_nchars;
404 ret = parse_reparse_data(rpbuf, *rpbuflen_p, &rpdata);
408 if (extract_root_realpath[0] == L'\0' ||
409 extract_root_realpath[1] != L':' ||
410 extract_root_realpath[2] != L'\\')
411 return WIMLIB_ERR_REPARSE_POINT_FIXUP_FAILED;
413 ret = parse_substitute_name(rpdata.substitute_name,
414 rpdata.substitute_name_nbytes,
418 stripped_nchars = ret;
419 target = rpdata.substitute_name;
420 target_nchars = rpdata.substitute_name_nbytes / sizeof(utf16lechar);
421 stripped_target = target + stripped_nchars;
422 stripped_target_nchars = target_nchars - stripped_nchars;
424 new_target = alloca((6 + extract_root_realpath_nchars +
425 stripped_target_nchars) * sizeof(utf16lechar));
428 if (stripped_nchars == 6) {
429 /* Include \??\ prefix if it was present before */
430 p = wmempcpy(p, L"\\??\\", 4);
433 /* Print name excludes the \??\ if present. */
435 if (stripped_nchars != 0) {
436 /* Get drive letter from real path to extract root, if a drive
437 * letter was present before. */
438 *p++ = extract_root_realpath[0];
439 *p++ = extract_root_realpath[1];
441 /* Copy the rest of the extract root */
442 p = wmempcpy(p, extract_root_realpath + 2, extract_root_realpath_nchars - 2);
444 /* Append the stripped target */
445 p = wmempcpy(p, stripped_target, stripped_target_nchars);
446 new_target_nchars = p - new_target;
447 new_print_name_nchars = p - new_print_name;
449 if (new_target_nchars * sizeof(utf16lechar) >= REPARSE_POINT_MAX_SIZE ||
450 new_print_name_nchars * sizeof(utf16lechar) >= REPARSE_POINT_MAX_SIZE)
451 return WIMLIB_ERR_REPARSE_POINT_FIXUP_FAILED;
453 rpdata.substitute_name = new_target;
454 rpdata.substitute_name_nbytes = new_target_nchars * sizeof(utf16lechar);
455 rpdata.print_name = new_print_name;
456 rpdata.print_name_nbytes = new_print_name_nchars * sizeof(utf16lechar);
457 return make_reparse_buffer(&rpdata, rpbuf, rpbuflen_p);
459 #endif /* __WIN32__ */
461 /* Set reparse data on extracted file or directory that has
462 * FILE_ATTRIBUTE_REPARSE_POINT set. */
464 extract_reparse_data(const tchar *path, struct apply_ctx *ctx,
465 struct wim_inode *inode,
466 struct wim_lookup_table_entry *lte_override)
469 u8 rpbuf[REPARSE_POINT_MAX_SIZE];
472 ret = wim_inode_get_reparse_data(inode, rpbuf, &rpbuflen, lte_override);
477 /* Fix up target of absolute symbolic link or junction points so
478 * that they point into the actual extraction target. */
479 if ((ctx->extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX) &&
480 (inode->i_reparse_tag == WIM_IO_REPARSE_TAG_SYMLINK ||
481 inode->i_reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT) &&
482 !inode->i_not_rpfixed)
484 ret = try_extract_rpfix(rpbuf, &rpbuflen, ctx->realtarget,
485 ctx->realtarget_nchars);
486 if (ret && !(ctx->extract_flags &
487 WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS))
489 WARNING("Reparse point fixup of \"%"TS"\" "
498 ret = ctx->ops->set_reparse_data(path, rpbuf, rpbuflen, ctx);
500 /* On Windows, the SeCreateSymbolicLink privilege is required to create
501 * symbolic links. To be more friendly towards non-Administrator users,
502 * we merely warn the user if symbolic links cannot be created due to
503 * insufficient permissions or privileges, unless
504 * WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS was provided. */
506 if (ret && inode_is_symlink(inode) &&
507 (errno == EACCES || errno == EPERM) &&
508 !(ctx->extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS))
510 WARNING("Can't set reparse data on \"%"TS"\": "
512 " You may be trying to "
513 "extract a symbolic link without the\n"
514 " SeCreateSymbolicLink privilege, "
515 "which by default non-Administrator\n"
516 " accounts do not have.",
524 /* Account for reparse data consumed. */
525 update_extract_progress(ctx,
526 (lte_override ? lte_override :
527 inode_unnamed_lte_resolved(inode)));
531 ERROR_WITH_ERRNO("Failed to set reparse data on \"%"TS"\"", path);
536 * Extract zero or more streams to a file.
538 * This function operates slightly differently depending on whether @lte_spec is
539 * NULL or not. When @lte_spec is NULL, the behavior is to extract the default
540 * file contents (unnamed stream), and, if named data streams are supported in
541 * the extract mode and volume, any named data streams. When @lte_spec is not
542 * NULL, the behavior is to extract only all copies of the stream @lte_spec, and
543 * in addition use @lte_spec to set the reparse data or create the symbolic link
547 * Path to file to extract (as can be passed to apply_operations
552 * WIM dentry that corresponds to the file being extracted.
554 * If non-NULL, specifies the lookup table entry for a stream to extract,
555 * and only that stream will be extracted (although there may be more than
556 * one instance of it).
558 * Used only if @lte_spec != NULL; it is passed to the extraction functions
559 * rather than @lte_spec, allowing the location of the stream to be
560 * overridden. (This is used when the WIM is being read from a nonseekable
561 * file, such as a pipe, when streams need to be used more than once; each
562 * such stream is extracted to a temporary file.)
565 extract_streams(const tchar *path, struct apply_ctx *ctx,
566 struct wim_dentry *dentry,
567 struct wim_lookup_table_entry *lte_spec,
568 struct wim_lookup_table_entry *lte_override)
570 struct wim_inode *inode = dentry->d_inode;
571 struct wim_lookup_table_entry *lte;
572 file_spec_t file_spec;
575 if (dentry->was_hardlinked)
582 for (unsigned i = 0; i < SHA1_HASH_SIZE; i++)
583 p += sprintf(p, "%02x", lte_override->hash[i]);
584 DEBUG("Extracting stream SHA1=%s to \"%"TS"\"",
585 sha1_str, path, inode->i_ino);
587 DEBUG("Extracting streams to \"%"TS"\"", path, inode->i_ino);
591 if (ctx->ops->uses_cookies)
592 file_spec.cookie = inode->extract_cookie;
594 file_spec.path = path;
596 /* Unnamed data stream. */
597 lte = inode_unnamed_lte_resolved(inode);
598 if (lte && (!lte_spec || lte == lte_spec)) {
601 if (!(inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
602 FILE_ATTRIBUTE_REPARSE_POINT)))
604 if (inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED &&
605 ctx->supported_features.encrypted_files) {
606 if (!ctx->ops->extract_encrypted_stream_creates_file) {
607 ret = ctx->ops->extract_encrypted_stream(
613 ret = ctx->ops->extract_unnamed_stream(
614 file_spec, lte, ctx);
618 update_extract_progress(ctx, lte);
620 else if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT)
623 if (ctx->supported_features.reparse_points)
624 ret = extract_reparse_data(path, ctx, inode, lte);
626 else if ((inode_is_symlink(inode) &&
627 ctx->supported_features.symlink_reparse_points))
628 ret = extract_symlink(path, ctx, inode, lte);
635 /* Named data streams. */
636 if (can_extract_named_data_streams(ctx)) {
637 for (u16 i = 0; i < inode->i_num_ads; i++) {
638 struct wim_ads_entry *entry = &inode->i_ads_entries[i];
640 if (!ads_entry_is_named_stream(entry))
645 if (lte_spec && lte_spec != lte)
649 ret = ctx->ops->extract_named_stream(file_spec, entry->stream_name,
650 entry->stream_name_nbytes / 2,
654 update_extract_progress(ctx, lte);
660 ERROR_WITH_ERRNO("Failed to extract data of \"%"TS"\"", path);
664 /* Set attributes on an extracted file or directory if supported by the
665 * extraction mode. */
667 extract_file_attributes(const tchar *path, struct apply_ctx *ctx,
668 struct wim_dentry *dentry, unsigned pass)
672 if (ctx->ops->set_file_attributes &&
673 !(ctx->extract_flags & WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES) &&
674 !(dentry == ctx->extract_root && ctx->root_dentry_is_special)) {
675 u32 attributes = dentry->d_inode->i_attributes;
677 /* Clear unsupported attributes. */
678 attributes &= ctx->supported_attributes_mask;
680 if ((attributes & FILE_ATTRIBUTE_DIRECTORY &&
681 !ctx->supported_features.encrypted_directories) ||
682 (!(attributes & FILE_ATTRIBUTE_DIRECTORY) &&
683 !ctx->supported_features.encrypted_files))
685 attributes &= ~FILE_ATTRIBUTE_ENCRYPTED;
689 attributes = FILE_ATTRIBUTE_NORMAL;
691 ret = ctx->ops->set_file_attributes(path, attributes, ctx, pass);
693 ERROR_WITH_ERRNO("Failed to set attributes on "
702 /* Set or remove the short (DOS) name on an extracted file or directory if
703 * supported by the extraction mode. Since DOS names are unimportant and it's
704 * easy to run into problems setting them on Windows (SetFileShortName()
705 * requires SE_RESTORE privilege, which only the Administrator can request, and
706 * also requires DELETE access to the file), failure is ignored unless
707 * WIMLIB_EXTRACT_FLAG_STRICT_SHORT_NAMES is set. */
709 extract_short_name(const tchar *path, struct apply_ctx *ctx,
710 struct wim_dentry *dentry)
714 /* The root of the dentry tree being extracted may not be extracted to
715 * its original name, so its short name should be ignored. */
716 if (dentry == ctx->extract_root)
719 if (ctx->supported_features.short_names) {
720 ret = ctx->ops->set_short_name(path,
722 dentry->short_name_nbytes / 2,
724 if (ret && (ctx->extract_flags &
725 WIMLIB_EXTRACT_FLAG_STRICT_SHORT_NAMES))
727 ERROR_WITH_ERRNO("Failed to set short name of "
735 /* Set security descriptor, UNIX data, or neither on an extracted file, taking
736 * into account the current extraction mode and flags. */
738 extract_security(const tchar *path, struct apply_ctx *ctx,
739 struct wim_dentry *dentry)
742 struct wim_inode *inode = dentry->d_inode;
744 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_NO_ACLS)
747 if ((ctx->extract_root == dentry) && ctx->root_dentry_is_special)
751 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
752 struct wimlib_unix_data data;
754 ret = inode_get_unix_data(inode, &data, NULL);
758 ret = ctx->ops->set_unix_data(path, &data, ctx);
760 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_ACLS) {
761 ERROR_WITH_ERRNO("Failed to set UNIX owner, "
762 "group, and/or mode on "
766 WARNING_WITH_ERRNO("Failed to set UNIX owner, "
767 "group, and/or/mode on "
773 #endif /* __WIN32__ */
774 if (ctx->supported_features.security_descriptors &&
775 inode->i_security_id != -1)
777 const struct wim_security_data *sd;
781 sd = wim_const_security_data(ctx->wim);
782 desc = sd->descriptors[inode->i_security_id];
783 desc_size = sd->sizes[inode->i_security_id];
785 ret = ctx->ops->set_security_descriptor(path, desc,
788 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_ACLS) {
789 ERROR_WITH_ERRNO("Failed to set security "
790 "descriptor on \"%"TS"\"", path);
794 if (errno != EACCES) {
795 WARNING_WITH_ERRNO("Failed to set "
796 "security descriptor "
797 "on \"%"TS"\"", path);
800 ctx->no_security_descriptors++;
807 /* Set timestamps on an extracted file. Failure is warning-only unless
808 * WIMLIB_EXTRACT_FLAG_STRICT_TIMESTAMPS is set. */
810 extract_timestamps(const tchar *path, struct apply_ctx *ctx,
811 struct wim_dentry *dentry)
813 struct wim_inode *inode = dentry->d_inode;
816 if ((ctx->extract_root == dentry) && ctx->root_dentry_is_special)
819 if (ctx->ops->set_timestamps) {
820 ret = ctx->ops->set_timestamps(path,
821 inode->i_creation_time,
822 inode->i_last_write_time,
823 inode->i_last_access_time,
826 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_TIMESTAMPS) {
827 ERROR_WITH_ERRNO("Failed to set timestamps "
828 "on \"%"TS"\"", path);
831 WARNING_WITH_ERRNO("Failed to set timestamps "
832 "on \"%"TS"\"", path);
839 /* Check whether the extraction of a dentry should be skipped completely. */
841 dentry_is_supported(struct wim_dentry *dentry,
842 const struct wim_features *supported_features)
844 struct wim_inode *inode = dentry->d_inode;
846 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
847 return supported_features->reparse_points ||
848 (inode_is_symlink(inode) &&
849 supported_features->symlink_reparse_points);
851 if (inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED) {
852 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)
853 return supported_features->encrypted_directories != 0;
855 return supported_features->encrypted_files != 0;
860 /* Given a WIM dentry to extract, build the path to which to extract it, in the
861 * format understood by the callbacks in the apply_operations being used.
863 * Write the resulting path into @path, which must have room for at least
864 * ctx->ops->max_path characters including the null-terminator.
866 * Return %true if successful; %false if this WIM dentry doesn't actually need
867 * to be extracted or if the calculated path exceeds ctx->ops->max_path
870 * This function clobbers the tmp_list member of @dentry and its ancestors up
871 * until the extraction root. */
873 build_extraction_path(tchar path[], struct wim_dentry *dentry,
874 struct apply_ctx *ctx)
877 LIST_HEAD(ancestor_list);
879 const tchar *target_prefix;
880 size_t target_prefix_nchars;
881 struct wim_dentry *d;
883 if (dentry->extraction_skipped)
886 path_nchars = ctx->ops->path_prefix_nchars;
888 if (ctx->ops->requires_realtarget_in_paths) {
889 target_prefix = ctx->realtarget;
890 target_prefix_nchars = ctx->realtarget_nchars;
891 } else if (ctx->ops->requires_target_in_paths) {
892 target_prefix = ctx->target;
893 target_prefix_nchars = ctx->target_nchars;
895 target_prefix = NULL;
896 target_prefix_nchars = 0;
898 path_nchars += target_prefix_nchars;
900 for (d = dentry; d != ctx->extract_root; d = d->parent) {
901 if (!d->in_extraction_tree || d->extraction_skipped)
904 path_nchars += d->extraction_name_nchars + 1;
905 list_add(&d->tmp_list, &ancestor_list);
908 path_nchars++; /* null terminator */
910 if (path_nchars > ctx->ops->path_max) {
911 WARNING("\"%"TS"\": Path too long to extract",
912 dentry_full_path(dentry));
916 p = tmempcpy(p, ctx->ops->path_prefix, ctx->ops->path_prefix_nchars);
917 p = tmempcpy(p, target_prefix, target_prefix_nchars);
918 list_for_each_entry(d, &ancestor_list, tmp_list) {
919 *p++ = ctx->ops->path_separator;
920 p = tmempcpy(p, d->extraction_name, d->extraction_name_nchars);
923 wimlib_assert(p - path == path_nchars);
928 get_num_path_components(const tchar *path, tchar path_separator)
930 unsigned num_components = 0;
932 /* Ignore drive letter. */
933 if (path[0] != L'\0' && path[1] == L':')
938 while (*path == path_separator)
942 while (*path && *path != path_separator)
945 return num_components;
949 extract_multiimage_symlink(const tchar *oldpath, const tchar *newpath,
950 struct apply_ctx *ctx, struct wim_dentry *dentry)
952 size_t num_raw_path_components;
953 const struct wim_dentry *d;
954 size_t num_target_path_components;
959 num_raw_path_components = 0;
960 for (d = dentry; d != ctx->extract_root; d = d->parent)
961 num_raw_path_components++;
963 if (ctx->ops->requires_realtarget_in_paths)
964 num_target_path_components = get_num_path_components(ctx->realtarget,
965 ctx->ops->path_separator);
966 else if (ctx->ops->requires_target_in_paths)
967 num_target_path_components = get_num_path_components(ctx->target,
968 ctx->ops->path_separator);
970 num_target_path_components = 0;
972 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE) {
973 wimlib_assert(num_target_path_components > 0);
974 num_raw_path_components++;
975 num_target_path_components--;
978 p_old = oldpath + ctx->ops->path_prefix_nchars;
980 if (p_old[0] != L'\0' && p_old[1] == ':')
983 while (*p_old == ctx->ops->path_separator)
985 while (--num_target_path_components) {
986 while (*p_old != ctx->ops->path_separator)
988 while (*p_old == ctx->ops->path_separator)
992 tchar symlink_target[tstrlen(p_old) + 3 * num_raw_path_components + 1];
994 p = &symlink_target[0];
995 while (num_raw_path_components--) {
998 *p++ = ctx->ops->path_separator;
1001 DEBUG("Creating symlink \"%"TS"\" => \"%"TS"\"",
1002 newpath, symlink_target);
1003 ret = ctx->ops->create_symlink(symlink_target, newpath, ctx);
1005 ERROR_WITH_ERRNO("Failed to create symlink "
1006 "\"%"TS"\" => \"%"TS"\"",
1007 newpath, symlink_target);
1012 /* Create the "skeleton" of an extracted file or directory. Don't yet extract
1013 * data streams, reparse data (including symbolic links), timestamps, and
1014 * security descriptors. Basically, everything that doesn't require reading
1015 * non-metadata resources from the WIM file and isn't delayed until the final
1018 do_dentry_extract_skeleton(tchar path[], struct wim_dentry *dentry,
1019 struct apply_ctx *ctx)
1021 struct wim_inode *inode = dentry->d_inode;
1023 const tchar *oldpath;
1025 if (unlikely(is_linked_extraction(ctx))) {
1026 struct wim_lookup_table_entry *unnamed_lte;
1028 unnamed_lte = inode_unnamed_lte_resolved(dentry->d_inode);
1029 if (unnamed_lte && unnamed_lte->extracted_file) {
1030 oldpath = unnamed_lte->extracted_file;
1031 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_HARDLINK)
1038 /* Create hard link if this dentry corresponds to an already-extracted
1040 if (inode->i_extracted_file) {
1041 oldpath = inode->i_extracted_file;
1045 /* Skip symlinks unless they can be extracted as reparse points rather
1046 * than created directly. */
1047 if (inode_is_symlink(inode) && !ctx->supported_features.reparse_points)
1050 /* Create this file or directory unless it's the extraction root, which
1051 * was already created if necessary. */
1052 if (dentry != ctx->extract_root) {
1053 ret = extract_inode(path, ctx, inode);
1058 /* Create empty named data streams. */
1059 if (can_extract_named_data_streams(ctx)) {
1060 for (u16 i = 0; i < inode->i_num_ads; i++) {
1061 file_spec_t file_spec;
1062 struct wim_ads_entry *entry = &inode->i_ads_entries[i];
1064 if (!ads_entry_is_named_stream(entry))
1068 if (ctx->ops->uses_cookies)
1069 file_spec.cookie = inode->extract_cookie;
1071 file_spec.path = path;
1072 ret = ctx->ops->extract_named_stream(file_spec,
1074 entry->stream_name_nbytes / 2,
1077 ERROR_WITH_ERRNO("\"%"TS"\": failed to create "
1078 "empty named data stream",
1085 /* Set file attributes (if supported). */
1086 ret = extract_file_attributes(path, ctx, dentry, 0);
1090 /* Set or remove file short name (if supported). */
1091 ret = extract_short_name(path, ctx, dentry);
1095 /* If inode has multiple links and hard links are supported in this
1096 * extraction mode and volume, save the path to the extracted file in
1097 * case it's needed to create a hard link. */
1098 if (unlikely(is_linked_extraction(ctx))) {
1099 struct wim_lookup_table_entry *unnamed_lte;
1101 unnamed_lte = inode_unnamed_lte_resolved(dentry->d_inode);
1103 unnamed_lte->extracted_file = TSTRDUP(path);
1104 if (!unnamed_lte->extracted_file)
1105 return WIMLIB_ERR_NOMEM;
1107 } else if (inode->i_nlink > 1 && ctx->supported_features.hard_links) {
1108 inode->i_extracted_file = TSTRDUP(path);
1109 if (!inode->i_extracted_file)
1110 return WIMLIB_ERR_NOMEM;
1115 ret = extract_multiimage_symlink(oldpath, path, ctx, dentry);
1118 dentry->was_hardlinked = 1;
1122 ret = extract_hardlink(oldpath, path, ctx);
1125 dentry->was_hardlinked = 1;
1129 /* This is a wrapper around do_dentry_extract_skeleton() that handles building
1130 * the path, doing short name reordering. This is also idempotent; dentries
1131 * already processed have skeleton_extracted set and no action is taken. See
1132 * apply_operations.requires_short_name_reordering for more details about short
1133 * name reordering. */
1135 dentry_extract_skeleton(struct wim_dentry *dentry, void *_ctx)
1137 struct apply_ctx *ctx = _ctx;
1138 tchar path[ctx->ops->path_max];
1139 struct wim_dentry *orig_dentry;
1140 struct wim_dentry *other_dentry;
1143 if (dentry->skeleton_extracted)
1147 if (ctx->supported_features.short_names
1148 && ctx->ops->requires_short_name_reordering
1149 && !dentry_has_short_name(dentry)
1150 && !dentry->d_inode->i_dos_name_extracted)
1152 inode_for_each_dentry(other_dentry, dentry->d_inode) {
1153 if (dentry_has_short_name(other_dentry)
1154 && !other_dentry->skeleton_extracted
1155 && other_dentry->in_extraction_tree
1156 && !other_dentry->extraction_skipped)
1158 DEBUG("Creating %"TS" before %"TS" "
1159 "to guarantee correct DOS name extraction",
1160 dentry_full_path(other_dentry),
1161 dentry_full_path(dentry));
1162 orig_dentry = dentry;
1163 dentry = other_dentry;
1169 if (!build_extraction_path(path, dentry, ctx))
1171 ret = do_dentry_extract_skeleton(path, dentry, ctx);
1175 dentry->skeleton_extracted = 1;
1178 dentry = orig_dentry;
1182 dentry->d_inode->i_dos_name_extracted = 1;
1187 dentry_extract_dir_skeleton(struct wim_dentry *dentry, void *_ctx)
1189 if (dentry->d_inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)
1190 return dentry_extract_skeleton(dentry, _ctx);
1194 /* Create a file or directory, then immediately extract all streams. The WIM
1195 * may not be read sequentially by this function. */
1197 dentry_extract(struct wim_dentry *dentry, void *_ctx)
1199 struct apply_ctx *ctx = _ctx;
1200 tchar path[ctx->ops->path_max];
1203 ret = dentry_extract_skeleton(dentry, ctx);
1207 if (!build_extraction_path(path, dentry, ctx))
1210 return extract_streams(path, ctx, dentry, NULL, NULL);
1213 /* Creates a temporary file opened for writing. The open file descriptor is
1214 * returned in @fd_ret and its name is returned in @name_ret (dynamically
1217 create_temporary_file(struct filedes *fd_ret, tchar **name_ret)
1223 name = ttempnam(NULL, T("wimlib"));
1225 ERROR_WITH_ERRNO("Failed to create temporary filename");
1226 return WIMLIB_ERR_NOMEM;
1229 raw_fd = topen(name, O_WRONLY | O_CREAT | O_EXCL | O_BINARY, 0600);
1232 if (errno == EEXIST) {
1236 ERROR_WITH_ERRNO("Failed to open temporary file \"%"TS"\"", name);
1238 return WIMLIB_ERR_OPEN;
1241 filedes_init(fd_ret, raw_fd);
1246 /* Extract all instances of the stream @lte that are being extracted in this
1247 * call of extract_tree(), but actually read the stream data from @lte_override.
1250 extract_stream_instances(struct wim_lookup_table_entry *lte,
1251 struct wim_lookup_table_entry *lte_override,
1252 struct apply_ctx *ctx)
1254 struct wim_dentry **lte_dentries;
1255 tchar path[ctx->ops->path_max];
1259 if (lte->out_refcnt <= ARRAY_LEN(lte->inline_lte_dentries))
1260 lte_dentries = lte->inline_lte_dentries;
1262 lte_dentries = lte->lte_dentries;
1264 for (i = 0; i < lte->out_refcnt; i++) {
1265 struct wim_dentry *dentry = lte_dentries[i];
1267 if (dentry->tmp_flag)
1269 if (!build_extraction_path(path, dentry, ctx))
1271 ret = extract_streams(path, ctx, dentry, lte, lte_override);
1273 goto out_clear_tmp_flags;
1274 dentry->tmp_flag = 1;
1277 out_clear_tmp_flags:
1278 for (i = 0; i < lte->out_refcnt; i++)
1279 lte_dentries[i]->tmp_flag = 0;
1283 /* Determine whether the specified stream needs to be extracted to a temporary
1286 * @lte->out_refcnt specifies the number of instances of this stream that must
1289 * @is_partial_res is %true if this stream is just one of multiple in a single
1290 * WIM resource being extracted. */
1292 need_tmpfile_to_extract(struct wim_lookup_table_entry *lte,
1293 bool is_partial_res)
1295 /* Temporary file is always required when reading a partial resource,
1296 * since in that case we retrieve all the contained streams in one pass.
1301 /* Otherwise we don't need a temporary file if only a single instance of
1302 * the stream is needed. */
1303 if (lte->out_refcnt == 1)
1306 wimlib_assert(lte->out_refcnt >= 2);
1308 /* We also don't need a temporary file if random access to the stream is
1310 if (lte->resource_location != RESOURCE_IN_WIM ||
1311 filedes_is_seekable(<e->rspec->wim->in_fd))
1318 begin_extract_stream_to_tmpfile(struct wim_lookup_table_entry *lte,
1319 bool is_partial_res,
1322 struct apply_ctx *ctx = _ctx;
1325 if (!need_tmpfile_to_extract(lte, is_partial_res)) {
1326 DEBUG("Temporary file not needed "
1327 "for stream (size=%"PRIu64")", lte->size);
1328 ret = extract_stream_instances(lte, lte, ctx);
1332 return BEGIN_STREAM_STATUS_SKIP_STREAM;
1335 DEBUG("Temporary file needed for stream (size=%"PRIu64")", lte->size);
1336 return create_temporary_file(&ctx->tmpfile_fd, &ctx->tmpfile_name);
1340 end_extract_stream_to_tmpfile(struct wim_lookup_table_entry *lte,
1341 int status, void *_ctx)
1343 struct apply_ctx *ctx = _ctx;
1344 struct wim_lookup_table_entry lte_override;
1346 int errno_save = errno;
1348 ret = filedes_close(&ctx->tmpfile_fd);
1353 goto out_delete_tmpfile;
1357 ERROR_WITH_ERRNO("Error writing temporary file %"TS, ctx->tmpfile_name);
1358 ret = WIMLIB_ERR_WRITE;
1359 goto out_delete_tmpfile;
1362 /* Now that a full stream has been extracted to a temporary file,
1363 * extract all instances of it to the actual target. */
1365 memcpy(<e_override, lte, sizeof(struct wim_lookup_table_entry));
1366 lte_override.resource_location = RESOURCE_IN_FILE_ON_DISK;
1367 lte_override.file_on_disk = ctx->tmpfile_name;
1369 ret = extract_stream_instances(lte, <e_override, ctx);
1373 tunlink(ctx->tmpfile_name);
1374 FREE(ctx->tmpfile_name);
1379 /* Extracts a list of streams (ctx.stream_list), assuming that the directory
1380 * structure and empty files were already created. This relies on the
1381 * per-`struct wim_lookup_table_entry' list of dentries that reference each
1382 * stream that was constructed earlier. */
1384 extract_stream_list(struct apply_ctx *ctx)
1386 if (!(ctx->extract_flags & WIMLIB_EXTRACT_FLAG_FILE_ORDER)) {
1387 /* Sequential extraction: read the streams in the order in which
1388 * they appear in the WIM file. */
1389 struct read_stream_list_callbacks cbs = {
1390 .begin_stream = begin_extract_stream_to_tmpfile,
1391 .begin_stream_ctx = ctx,
1392 .consume_chunk = extract_chunk_to_fd,
1393 .consume_chunk_ctx = &ctx->tmpfile_fd,
1394 .end_stream = end_extract_stream_to_tmpfile,
1395 .end_stream_ctx = ctx,
1397 return read_stream_list(&ctx->stream_list,
1398 offsetof(struct wim_lookup_table_entry, extraction_list),
1399 &cbs, VERIFY_STREAM_HASHES);
1401 /* Extract the streams in unsorted order. */
1402 struct wim_lookup_table_entry *lte;
1405 list_for_each_entry(lte, &ctx->stream_list, extraction_list) {
1406 ret = extract_stream_instances(lte, lte, ctx);
1414 #define PWM_ALLOW_WIM_HDR 0x00001
1415 #define PWM_SILENT_EOF 0x00002
1417 /* Read the header from a stream in a pipable WIM. */
1419 read_pwm_stream_header(WIMStruct *pwm, struct wim_lookup_table_entry *lte,
1420 struct wim_resource_spec *rspec,
1421 int flags, struct wim_header_disk *hdr_ret)
1424 struct pwm_stream_hdr stream_hdr;
1425 struct wim_header_disk pwm_hdr;
1427 struct wim_reshdr reshdr;
1430 ret = full_read(&pwm->in_fd, &buf.stream_hdr, sizeof(buf.stream_hdr));
1434 if ((flags & PWM_ALLOW_WIM_HDR) && buf.stream_hdr.magic == PWM_MAGIC) {
1435 BUILD_BUG_ON(sizeof(buf.pwm_hdr) < sizeof(buf.stream_hdr));
1436 ret = full_read(&pwm->in_fd, &buf.stream_hdr + 1,
1437 sizeof(buf.pwm_hdr) - sizeof(buf.stream_hdr));
1441 lte->resource_location = RESOURCE_NONEXISTENT;
1442 memcpy(hdr_ret, &buf.pwm_hdr, sizeof(buf.pwm_hdr));
1446 if (le64_to_cpu(buf.stream_hdr.magic) != PWM_STREAM_MAGIC) {
1447 ERROR("Data read on pipe is invalid (expected stream header).");
1448 return WIMLIB_ERR_INVALID_PIPABLE_WIM;
1451 copy_hash(lte->hash, buf.stream_hdr.hash);
1453 reshdr.size_in_wim = 0;
1454 reshdr.flags = le32_to_cpu(buf.stream_hdr.flags);
1455 reshdr.offset_in_wim = pwm->in_fd.offset;
1456 reshdr.uncompressed_size = le64_to_cpu(buf.stream_hdr.uncompressed_size);
1457 wim_res_hdr_to_spec(&reshdr, pwm, rspec);
1458 lte_bind_wim_resource_spec(lte, rspec);
1459 lte->flags = rspec->flags;
1460 lte->size = rspec->uncompressed_size;
1461 lte->offset_in_res = 0;
1465 if (ret != WIMLIB_ERR_UNEXPECTED_END_OF_FILE || !(flags & PWM_SILENT_EOF))
1466 ERROR_WITH_ERRNO("Error reading pipable WIM from pipe");
1471 extract_streams_from_pipe(struct apply_ctx *ctx)
1473 struct wim_lookup_table_entry *found_lte;
1474 struct wim_resource_spec *rspec;
1475 struct wim_lookup_table_entry *needed_lte;
1476 struct wim_lookup_table *lookup_table;
1477 struct wim_header_disk pwm_hdr;
1481 ret = WIMLIB_ERR_NOMEM;
1482 found_lte = new_lookup_table_entry();
1483 if (found_lte == NULL)
1486 rspec = MALLOC(sizeof(struct wim_resource_spec));
1488 goto out_free_found_lte;
1490 lookup_table = ctx->wim->lookup_table;
1491 pwm_flags = PWM_ALLOW_WIM_HDR;
1492 if ((ctx->extract_flags & WIMLIB_EXTRACT_FLAG_RESUME))
1493 pwm_flags |= PWM_SILENT_EOF;
1494 memcpy(ctx->progress.extract.guid, ctx->wim->hdr.guid, WIM_GID_LEN);
1495 ctx->progress.extract.part_number = ctx->wim->hdr.part_number;
1496 ctx->progress.extract.total_parts = ctx->wim->hdr.total_parts;
1497 if (ctx->progress_func)
1498 ctx->progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN,
1500 while (ctx->num_streams_remaining) {
1501 if (found_lte->resource_location != RESOURCE_NONEXISTENT)
1502 lte_unbind_wim_resource_spec(found_lte);
1503 ret = read_pwm_stream_header(ctx->wim, found_lte, rspec,
1504 pwm_flags, &pwm_hdr);
1506 if (ret == WIMLIB_ERR_UNEXPECTED_END_OF_FILE &&
1507 (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_RESUME))
1511 goto out_free_found_lte;
1514 if ((found_lte->resource_location != RESOURCE_NONEXISTENT)
1515 && !(found_lte->flags & WIM_RESHDR_FLAG_METADATA)
1516 && (needed_lte = lookup_stream(lookup_table, found_lte->hash))
1517 && (needed_lte->out_refcnt))
1519 tchar *tmpfile_name = NULL;
1520 struct wim_lookup_table_entry *lte_override;
1521 struct wim_lookup_table_entry tmpfile_lte;
1523 needed_lte->offset_in_res = found_lte->offset_in_res;
1524 needed_lte->flags = found_lte->flags;
1525 needed_lte->size = found_lte->size;
1527 lte_unbind_wim_resource_spec(found_lte);
1528 lte_bind_wim_resource_spec(needed_lte, rspec);
1530 if (needed_lte->out_refcnt > 1) {
1532 struct filedes tmpfile_fd;
1534 /* Extract stream to temporary file. */
1535 ret = create_temporary_file(&tmpfile_fd, &tmpfile_name);
1537 lte_unbind_wim_resource_spec(needed_lte);
1538 goto out_free_found_lte;
1541 ret = extract_full_stream_to_fd(needed_lte,
1544 filedes_close(&tmpfile_fd);
1545 goto delete_tmpfile;
1548 if (filedes_close(&tmpfile_fd)) {
1549 ERROR_WITH_ERRNO("Error writing to temporary "
1550 "file \"%"TS"\"", tmpfile_name);
1551 ret = WIMLIB_ERR_WRITE;
1552 goto delete_tmpfile;
1554 memcpy(&tmpfile_lte, needed_lte,
1555 sizeof(struct wim_lookup_table_entry));
1556 tmpfile_lte.resource_location = RESOURCE_IN_FILE_ON_DISK;
1557 tmpfile_lte.file_on_disk = tmpfile_name;
1558 lte_override = &tmpfile_lte;
1560 lte_override = needed_lte;
1563 ret = extract_stream_instances(needed_lte, lte_override, ctx);
1565 lte_unbind_wim_resource_spec(needed_lte);
1567 tunlink(tmpfile_name);
1571 goto out_free_found_lte;
1572 ctx->num_streams_remaining--;
1573 } else if (found_lte->resource_location != RESOURCE_NONEXISTENT) {
1574 ret = skip_wim_stream(found_lte);
1576 goto out_free_found_lte;
1578 u16 part_number = le16_to_cpu(pwm_hdr.part_number);
1579 u16 total_parts = le16_to_cpu(pwm_hdr.total_parts);
1581 if (part_number != ctx->progress.extract.part_number ||
1582 total_parts != ctx->progress.extract.total_parts ||
1583 memcmp(pwm_hdr.guid, ctx->progress.extract.guid,
1586 ctx->progress.extract.part_number = part_number;
1587 ctx->progress.extract.total_parts = total_parts;
1588 memcpy(ctx->progress.extract.guid,
1589 pwm_hdr.guid, WIM_GID_LEN);
1590 if (ctx->progress_func) {
1592 WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN,
1601 if (found_lte->resource_location != RESOURCE_IN_WIM)
1603 free_lookup_table_entry(found_lte);
1612 /* Finish extracting a file, directory, or symbolic link by setting file
1613 * security and timestamps. */
1615 dentry_extract_final(struct wim_dentry *dentry, void *_ctx)
1617 struct apply_ctx *ctx = _ctx;
1619 tchar path[ctx->ops->path_max];
1621 if (!build_extraction_path(path, dentry, ctx))
1624 ret = extract_security(path, ctx, dentry);
1628 if (ctx->ops->requires_final_set_attributes_pass) {
1629 /* Set file attributes (if supported). */
1630 ret = extract_file_attributes(path, ctx, dentry, 1);
1635 return extract_timestamps(path, ctx, dentry);
1639 * Extract a WIM dentry to standard output.
1641 * This obviously doesn't make sense in all cases. We return an error if the
1642 * dentry does not correspond to a regular file. Otherwise we extract the
1643 * unnamed data stream only.
1646 extract_dentry_to_stdout(struct wim_dentry *dentry)
1649 if (dentry->d_inode->i_attributes & (FILE_ATTRIBUTE_REPARSE_POINT |
1650 FILE_ATTRIBUTE_DIRECTORY))
1652 ERROR("\"%"TS"\" is not a regular file and therefore cannot be "
1653 "extracted to standard output", dentry_full_path(dentry));
1654 ret = WIMLIB_ERR_NOT_A_REGULAR_FILE;
1656 struct wim_lookup_table_entry *lte;
1658 lte = inode_unnamed_lte_resolved(dentry->d_inode);
1660 struct filedes _stdout;
1661 filedes_init(&_stdout, STDOUT_FILENO);
1662 ret = extract_full_stream_to_fd(lte, &_stdout);
1669 static const utf16lechar replacement_char = cpu_to_le16(0xfffd);
1671 static const utf16lechar replacement_char = cpu_to_le16('?');
1675 file_name_valid(utf16lechar *name, size_t num_chars, bool fix)
1681 for (i = 0; i < num_chars; i++) {
1684 case cpu_to_le16('\\'):
1685 case cpu_to_le16(':'):
1686 case cpu_to_le16('*'):
1687 case cpu_to_le16('?'):
1688 case cpu_to_le16('"'):
1689 case cpu_to_le16('<'):
1690 case cpu_to_le16('>'):
1691 case cpu_to_le16('|'):
1693 case cpu_to_le16('/'):
1694 case cpu_to_le16('\0'):
1696 name[i] = replacement_char;
1703 if (name[num_chars - 1] == cpu_to_le16(' ') ||
1704 name[num_chars - 1] == cpu_to_le16('.'))
1707 name[num_chars - 1] = replacement_char;
1716 dentry_mark_skipped(struct wim_dentry *dentry, void *_ignore)
1718 dentry->extraction_skipped = 1;
1723 * dentry_calculate_extraction_path-
1725 * Calculate the actual filename component at which a WIM dentry will be
1726 * extracted, handling invalid filenames "properly".
1728 * dentry->extraction_name usually will be set the same as dentry->file_name (on
1729 * UNIX, converted into the platform's multibyte encoding). However, if the
1730 * file name contains characters that are not valid on the current platform or
1731 * has some other format that is not valid, leave dentry->extraction_name as
1732 * NULL and set dentry->extraction_skipped to indicate that this dentry should
1733 * not be extracted, unless the appropriate flag
1734 * WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES is set in the extract flags, in
1735 * which case a substitute filename will be created and set instead.
1737 * Conflicts with case-insensitive names on Windows are handled similarly; see
1741 dentry_calculate_extraction_path(struct wim_dentry *dentry, void *_args)
1743 struct apply_ctx *ctx = _args;
1746 if (dentry == ctx->extract_root || dentry->extraction_skipped)
1749 if (!dentry_is_supported(dentry, &ctx->supported_features))
1752 if (!ctx->ops->supports_case_sensitive_filenames)
1754 struct wim_dentry *other;
1755 list_for_each_entry(other, &dentry->case_insensitive_conflict_list,
1756 case_insensitive_conflict_list)
1758 if (!other->extraction_skipped) {
1759 if (ctx->extract_flags &
1760 WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS) {
1761 WARNING("\"%"TS"\" has the same "
1762 "case-insensitive name as "
1763 "\"%"TS"\"; extracting "
1764 "dummy name instead",
1765 dentry_full_path(dentry),
1766 dentry_full_path(other));
1769 WARNING("Not extracting \"%"TS"\": "
1770 "has same case-insensitive "
1771 "name as \"%"TS"\"",
1772 dentry_full_path(dentry),
1773 dentry_full_path(other));
1780 if (file_name_valid(dentry->file_name, dentry->file_name_nbytes / 2, false)) {
1781 #if TCHAR_IS_UTF16LE
1782 dentry->extraction_name = dentry->file_name;
1783 dentry->extraction_name_nchars = dentry->file_name_nbytes / 2;
1786 return utf16le_to_tstr(dentry->file_name,
1787 dentry->file_name_nbytes,
1788 &dentry->extraction_name,
1789 &dentry->extraction_name_nchars);
1792 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES)
1794 WARNING("\"%"TS"\" has an invalid filename "
1795 "that is not supported on this platform; "
1796 "extracting dummy name instead",
1797 dentry_full_path(dentry));
1800 WARNING("Not extracting \"%"TS"\": has an invalid filename "
1801 "that is not supported on this platform",
1802 dentry_full_path(dentry));
1809 utf16lechar utf16_name_copy[dentry->file_name_nbytes / 2];
1811 memcpy(utf16_name_copy, dentry->file_name, dentry->file_name_nbytes);
1812 file_name_valid(utf16_name_copy, dentry->file_name_nbytes / 2, true);
1815 size_t tchar_nchars;
1816 #if TCHAR_IS_UTF16LE
1817 tchar_name = utf16_name_copy;
1818 tchar_nchars = dentry->file_name_nbytes / 2;
1820 ret = utf16le_to_tstr(utf16_name_copy,
1821 dentry->file_name_nbytes,
1822 &tchar_name, &tchar_nchars);
1826 size_t fixed_name_num_chars = tchar_nchars;
1827 tchar fixed_name[tchar_nchars + 50];
1829 tmemcpy(fixed_name, tchar_name, tchar_nchars);
1830 fixed_name_num_chars += tsprintf(fixed_name + tchar_nchars,
1831 T(" (invalid filename #%lu)"),
1832 ++ctx->invalid_sequence);
1833 #if !TCHAR_IS_UTF16LE
1836 dentry->extraction_name = memdup(fixed_name,
1837 2 * fixed_name_num_chars + 2);
1838 if (!dentry->extraction_name)
1839 return WIMLIB_ERR_NOMEM;
1840 dentry->extraction_name_nchars = fixed_name_num_chars;
1845 for_dentry_in_tree(dentry, dentry_mark_skipped, NULL);
1849 /* Clean up dentry and inode structure after extraction. */
1851 dentry_reset_needs_extraction(struct wim_dentry *dentry, void *_ignore)
1853 struct wim_inode *inode = dentry->d_inode;
1855 dentry->in_extraction_tree = 0;
1856 dentry->extraction_skipped = 0;
1857 dentry->was_hardlinked = 0;
1858 dentry->skeleton_extracted = 0;
1859 inode->i_visited = 0;
1860 FREE(inode->i_extracted_file);
1861 inode->i_extracted_file = NULL;
1862 inode->i_dos_name_extracted = 0;
1863 if ((void*)dentry->extraction_name != (void*)dentry->file_name)
1864 FREE(dentry->extraction_name);
1865 dentry->extraction_name = NULL;
1869 /* Tally features necessary to extract a dentry and the corresponding inode. */
1871 dentry_tally_features(struct wim_dentry *dentry, void *_features)
1873 struct wim_features *features = _features;
1874 struct wim_inode *inode = dentry->d_inode;
1876 if (dentry->extraction_skipped)
1879 if (inode->i_attributes & FILE_ATTRIBUTE_ARCHIVE)
1880 features->archive_files++;
1881 if (inode->i_attributes & FILE_ATTRIBUTE_HIDDEN)
1882 features->hidden_files++;
1883 if (inode->i_attributes & FILE_ATTRIBUTE_SYSTEM)
1884 features->system_files++;
1885 if (inode->i_attributes & FILE_ATTRIBUTE_COMPRESSED)
1886 features->compressed_files++;
1887 if (inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED) {
1888 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)
1889 features->encrypted_directories++;
1891 features->encrypted_files++;
1893 if (inode->i_attributes & FILE_ATTRIBUTE_NOT_CONTENT_INDEXED)
1894 features->not_context_indexed_files++;
1895 if (inode->i_attributes & FILE_ATTRIBUTE_SPARSE_FILE)
1896 features->sparse_files++;
1897 if (inode_has_named_stream(inode))
1898 features->named_data_streams++;
1899 if (inode->i_visited)
1900 features->hard_links++;
1901 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1902 features->reparse_points++;
1903 if (inode_is_symlink(inode))
1904 features->symlink_reparse_points++;
1906 features->other_reparse_points++;
1908 if (inode->i_security_id != -1)
1909 features->security_descriptors++;
1910 if (dentry->short_name_nbytes)
1911 features->short_names++;
1912 if (inode_has_unix_data(inode))
1913 features->unix_data++;
1914 inode->i_visited = 1;
1918 /* Tally the features necessary to extract a dentry tree. */
1920 dentry_tree_get_features(struct wim_dentry *root, struct wim_features *features)
1922 memset(features, 0, sizeof(struct wim_features));
1923 for_dentry_in_tree(root, dentry_tally_features, features);
1924 dentry_tree_clear_inode_visited(root);
1928 compute_supported_attributes_mask(const struct wim_features *supported_features)
1932 if (!supported_features->archive_files)
1933 mask &= ~FILE_ATTRIBUTE_ARCHIVE;
1935 if (!supported_features->hidden_files)
1936 mask &= ~FILE_ATTRIBUTE_HIDDEN;
1938 if (!supported_features->system_files)
1939 mask &= ~FILE_ATTRIBUTE_SYSTEM;
1941 if (!supported_features->not_context_indexed_files)
1942 mask &= ~FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
1944 if (!supported_features->compressed_files)
1945 mask &= ~FILE_ATTRIBUTE_COMPRESSED;
1947 if (!supported_features->sparse_files)
1948 mask &= ~FILE_ATTRIBUTE_SPARSE_FILE;
1950 if (!supported_features->reparse_points)
1951 mask &= ~FILE_ATTRIBUTE_REPARSE_POINT;
1957 do_feature_check(const struct wim_features *required_features,
1958 const struct wim_features *supported_features,
1960 const struct apply_operations *ops,
1963 const tchar *loc = T("the extraction operation");;
1964 const tchar *mode = T("this extraction mode");
1967 /* We're an archive program, so theoretically we can do what we want
1968 * with FILE_ATTRIBUTE_ARCHIVE (which is a dumb flag anyway). Don't
1969 * bother the user about it. */
1971 if (required_features->archive_files && !supported_features->archive_files)
1974 "%lu files in %"TS" are marked as archived, but this attribute\n"
1975 " is not supported in %"TS".",
1976 required_features->archive_files, loc, mode);
1980 if (required_features->hidden_files && !supported_features->hidden_files)
1983 "%lu files in %"TS" are marked as hidden, but this\n"
1984 " attribute is not supported in %"TS".",
1985 required_features->hidden_files, loc, mode);
1988 if (required_features->system_files && !supported_features->system_files)
1991 "%lu files in %"TS" are marked as system files,\n"
1992 " but this attribute is not supported in %"TS".",
1993 required_features->system_files, loc, mode);
1996 if (required_features->compressed_files && !supported_features->compressed_files)
1999 "%lu files in %"TS" are marked as being transparently\n"
2000 " compressed, but transparent compression is not supported in\n"
2001 " %"TS". These files will be extracted as uncompressed.",
2002 required_features->compressed_files, loc, mode);
2005 if (required_features->encrypted_files && !supported_features->encrypted_files)
2008 "%lu files in %"TS" are marked as being encrypted,\n"
2009 " but encryption is not supported in %"TS". These files\n"
2010 " will not be extracted.",
2011 required_features->encrypted_files, loc, mode);
2014 if (required_features->encrypted_directories &&
2015 !supported_features->encrypted_directories)
2018 "%lu directories in %"TS" are marked as being encrypted,\n"
2019 " but encryption is not supported in %"TS".\n"
2020 " These directories will be extracted as unencrypted.",
2021 required_features->encrypted_directories, loc, mode);
2024 if (required_features->not_context_indexed_files &&
2025 !supported_features->not_context_indexed_files)
2028 "%lu files in %"TS" are marked as not content indexed,\n"
2029 " but this attribute is not supported in %"TS".",
2030 required_features->not_context_indexed_files, loc, mode);
2033 if (required_features->sparse_files && !supported_features->sparse_files)
2036 "%lu files in %"TS" are marked as sparse, but creating\n"
2037 " sparse files is not supported in %"TS". These files\n"
2038 " will be extracted as non-sparse.",
2039 required_features->sparse_files, loc, mode);
2042 if (required_features->named_data_streams &&
2043 !supported_features->named_data_streams)
2046 "%lu files in %"TS" contain one or more alternate (named)\n"
2047 " data streams, which are not supported in %"TS".\n"
2048 " Alternate data streams will NOT be extracted.",
2049 required_features->named_data_streams, loc, mode);
2052 if (unlikely(extract_flags & (WIMLIB_EXTRACT_FLAG_HARDLINK |
2053 WIMLIB_EXTRACT_FLAG_SYMLINK)) &&
2054 required_features->named_data_streams &&
2055 supported_features->named_data_streams)
2058 "%lu files in %"TS" contain one or more alternate (named)\n"
2059 " data streams, which are not supported in linked extraction mode.\n"
2060 " Alternate data streams will NOT be extracted.",
2061 required_features->named_data_streams, loc);
2064 if (required_features->hard_links && !supported_features->hard_links)
2067 "%lu files in %"TS" are hard links, but hard links are\n"
2068 " not supported in %"TS". Hard links will be extracted as\n"
2069 " duplicate copies of the linked files.",
2070 required_features->hard_links, loc, mode);
2073 if (required_features->reparse_points && !supported_features->reparse_points)
2075 if (supported_features->symlink_reparse_points) {
2076 if (required_features->other_reparse_points) {
2078 "%lu files in %"TS" are reparse points that are neither\n"
2079 " symbolic links nor junction points and are not supported in\n"
2080 " %"TS". These reparse points will not be extracted.",
2081 required_features->other_reparse_points, loc,
2086 "%lu files in %"TS" are reparse points, which are\n"
2087 " not supported in %"TS" and will not be extracted.",
2088 required_features->reparse_points, loc, mode);
2092 if (required_features->security_descriptors &&
2093 !supported_features->security_descriptors)
2096 "%lu files in %"TS" have Windows NT security descriptors,\n"
2097 " but extracting security descriptors is not supported in\n"
2098 " %"TS". No security descriptors will be extracted.",
2099 required_features->security_descriptors, loc, mode);
2102 if (required_features->short_names && !supported_features->short_names)
2105 "%lu files in %"TS" have short (DOS) names, but\n"
2106 " extracting short names is not supported in %"TS".\n"
2107 " Short names will not be extracted.\n",
2108 required_features->short_names, loc, mode);
2112 if ((extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) &&
2113 required_features->unix_data && !supported_features->unix_data)
2115 ERROR("Extracting UNIX data is not supported in %"TS, mode);
2116 return WIMLIB_ERR_UNSUPPORTED;
2118 if ((extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_SHORT_NAMES) &&
2119 required_features->short_names && !supported_features->short_names)
2121 ERROR("Extracting short names is not supported in %"TS"", mode);
2122 return WIMLIB_ERR_UNSUPPORTED;
2124 if ((extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_TIMESTAMPS) &&
2125 !ops->set_timestamps)
2127 ERROR("Extracting timestamps is not supported in %"TS"", mode);
2128 return WIMLIB_ERR_UNSUPPORTED;
2130 if (((extract_flags & (WIMLIB_EXTRACT_FLAG_STRICT_ACLS |
2131 WIMLIB_EXTRACT_FLAG_UNIX_DATA))
2132 == WIMLIB_EXTRACT_FLAG_STRICT_ACLS) &&
2133 required_features->security_descriptors &&
2134 !supported_features->security_descriptors)
2136 ERROR("Extracting security descriptors is not supported in %"TS, mode);
2137 return WIMLIB_ERR_UNSUPPORTED;
2140 if ((extract_flags & WIMLIB_EXTRACT_FLAG_HARDLINK) &&
2141 !supported_features->hard_links)
2143 ERROR("Hard link extraction mode requested, but "
2144 "%"TS" does not support hard links!", mode);
2145 return WIMLIB_ERR_UNSUPPORTED;
2148 if ((extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS) &&
2149 required_features->symlink_reparse_points &&
2150 !(supported_features->symlink_reparse_points ||
2151 supported_features->reparse_points))
2153 ERROR("Extracting symbolic links is not supported in %"TS, mode);
2154 return WIMLIB_ERR_UNSUPPORTED;
2157 if ((extract_flags & WIMLIB_EXTRACT_FLAG_SYMLINK) &&
2158 !supported_features->symlink_reparse_points)
2160 ERROR("Symbolic link extraction mode requested, but "
2161 "%"TS" does not support symbolic "
2163 return WIMLIB_ERR_UNSUPPORTED;
2169 do_extract_warnings(struct apply_ctx *ctx)
2171 if (ctx->partial_security_descriptors == 0 &&
2172 ctx->no_security_descriptors == 0)
2175 WARNING("Extraction to \"%"TS"\" complete, but with one or more warnings:",
2177 if (ctx->partial_security_descriptors != 0) {
2178 WARNING("- Could only partially set the security descriptor\n"
2179 " on %lu files or directories.",
2180 ctx->partial_security_descriptors);
2182 if (ctx->no_security_descriptors != 0) {
2183 WARNING("- Could not set security descriptor at all\n"
2184 " on %lu files or directories.",
2185 ctx->no_security_descriptors);
2188 WARNING("To fully restore all security descriptors, run the program\n"
2189 " with Administrator rights.");
2194 dentry_set_skipped(struct wim_dentry *dentry, void *_ignore)
2196 dentry->in_extraction_tree = 1;
2197 dentry->extraction_skipped = 1;
2202 dentry_set_not_skipped(struct wim_dentry *dentry, void *_ignore)
2204 dentry->in_extraction_tree = 1;
2205 dentry->extraction_skipped = 0;
2210 extract_trees(WIMStruct *wim, struct wim_dentry **trees, size_t num_trees,
2211 const tchar *target, int extract_flags,
2212 wimlib_progress_func_t progress_func)
2214 struct wim_features required_features;
2215 struct apply_ctx ctx;
2217 struct wim_lookup_table_entry *lte;
2219 /* Start initializing the apply_ctx. */
2220 memset(&ctx, 0, sizeof(struct apply_ctx));
2222 ctx.extract_flags = extract_flags;
2223 ctx.target = target;
2224 ctx.target_nchars = tstrlen(target);
2225 ctx.progress_func = progress_func;
2226 if (progress_func) {
2227 ctx.progress.extract.wimfile_name = wim->filename;
2228 ctx.progress.extract.image = wim->current_image;
2229 ctx.progress.extract.extract_flags = (extract_flags &
2230 WIMLIB_EXTRACT_MASK_PUBLIC);
2231 ctx.progress.extract.image_name = wimlib_get_image_name(wim,
2232 wim->current_image);
2233 ctx.progress.extract.target = target;
2235 INIT_LIST_HEAD(&ctx.stream_list);
2237 if (extract_flags & WIMLIB_EXTRACT_FLAG_FILEMODE) {
2238 /* File mode --- target is explicit. */
2239 wimlib_assert(num_trees == 1);
2240 ret = calculate_dentry_full_path(trees[0]);
2243 ctx.progress.extract.extract_root_wim_source_path = trees[0]->_full_path;
2244 ctx.extract_root = trees[0];
2245 for_dentry_in_tree(ctx.extract_root, dentry_set_not_skipped, NULL);
2247 /* Targets are to be set relative to the root of the image
2248 * (preserving original directory structure). */
2250 ctx.progress.extract.extract_root_wim_source_path = T("");
2251 ctx.extract_root = wim_root_dentry(wim);
2252 for_dentry_in_tree(ctx.extract_root, dentry_set_skipped, NULL);
2254 for (size_t i = 0; i < num_trees; i++) {
2255 struct wim_dentry *d;
2257 for_dentry_in_tree(trees[i], dentry_set_not_skipped, NULL);
2260 /* Extract directories up to image root if preserving
2261 * directory structure. */
2262 if (!(extract_flags & WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE)) {
2263 while (d != ctx.extract_root) {
2265 dentry_set_not_skipped(d, NULL);
2271 /* Select the appropriate apply_operations based on the
2272 * platform and extract_flags. */
2274 ctx.ops = &win32_apply_ops;
2276 ctx.ops = &unix_apply_ops;
2280 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS)
2281 ctx.ops = &ntfs_3g_apply_ops;
2284 /* Call the start_extract() callback. This gives the apply_operations
2285 * implementation a chance to do any setup needed to access the volume.
2286 * Furthermore, it's expected to set the supported features of this
2287 * extraction mode (ctx.supported_features), which are determined at
2288 * runtime as they may vary depending on the actual volume. These
2289 * features are then compared with the actual features extracting this
2290 * dentry tree requires. Some mismatches will merely produce warnings
2291 * and the unsupported data will be ignored; others will produce errors.
2293 ret = ctx.ops->start_extract(target, &ctx);
2295 goto out_dentry_reset_needs_extraction;
2297 /* Get and check the features required to extract the dentry tree. */
2298 dentry_tree_get_features(ctx.extract_root, &required_features);
2299 ret = do_feature_check(&required_features, &ctx.supported_features,
2300 extract_flags, ctx.ops, true);
2302 goto out_finish_or_abort_extract;
2304 ctx.supported_attributes_mask =
2305 compute_supported_attributes_mask(&ctx.supported_features);
2307 /* Figure out whether the root dentry is being extracted to the root of
2308 * a volume and therefore needs to be treated "specially", for example
2309 * not being explicitly created and not having attributes set. */
2310 if (ctx.ops->target_is_root && ctx.ops->root_directory_is_special)
2311 ctx.root_dentry_is_special = ctx.ops->target_is_root(target);
2313 /* Calculate the actual filename component of each extracted dentry. In
2314 * the process, set the dentry->extraction_skipped flag on dentries that
2315 * are being skipped because of filename or supported features problems. */
2316 ret = for_dentry_in_tree(ctx.extract_root,
2317 dentry_calculate_extraction_path, &ctx);
2319 goto out_dentry_reset_needs_extraction;
2321 /* Build the list of the streams that need to be extracted and
2322 * initialize ctx.progress.extract with stream information. */
2323 ret = for_dentry_in_tree(ctx.extract_root,
2324 dentry_resolve_and_zero_lte_refcnt, &ctx);
2326 goto out_dentry_reset_needs_extraction;
2328 ret = for_dentry_in_tree(ctx.extract_root,
2329 dentry_add_streams_to_extract, &ctx);
2331 goto out_teardown_stream_list;
2333 if (extract_flags & WIMLIB_EXTRACT_FLAG_FROM_PIPE) {
2334 /* When extracting from a pipe, the number of bytes of data to
2335 * extract can't be determined in the normal way (examining the
2336 * lookup table), since at this point all we have is a set of
2337 * SHA1 message digests of streams that need to be extracted.
2338 * However, we can get a reasonably accurate estimate by taking
2339 * <TOTALBYTES> from the corresponding <IMAGE> in the WIM XML
2340 * data. This does assume that a full image is being extracted,
2341 * but currently there is no API for doing otherwise. (Also,
2342 * subtract <HARDLINKBYTES> from this if hard links are
2343 * supported by the extraction mode.) */
2344 ctx.progress.extract.total_bytes =
2345 wim_info_get_image_total_bytes(wim->wim_info,
2346 wim->current_image);
2347 if (ctx.supported_features.hard_links) {
2348 ctx.progress.extract.total_bytes -=
2349 wim_info_get_image_hard_link_bytes(wim->wim_info,
2350 wim->current_image);
2354 /* Handle the special case of extracting a file to standard
2355 * output. In that case, "root" should be a single file, not a
2356 * directory tree. (If not, extract_dentry_to_stdout() will
2357 * return an error.) */
2358 if (extract_flags & WIMLIB_EXTRACT_FLAG_TO_STDOUT) {
2360 for (size_t i = 0; i < num_trees; i++) {
2361 ret = extract_dentry_to_stdout(trees[i]);
2365 goto out_teardown_stream_list;
2368 if (ctx.ops->realpath_works_on_nonexisting_files &&
2369 ((extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX) ||
2370 ctx.ops->requires_realtarget_in_paths))
2372 ctx.realtarget = realpath(target, NULL);
2373 if (!ctx.realtarget) {
2374 ret = WIMLIB_ERR_NOMEM;
2375 goto out_teardown_stream_list;
2377 ctx.realtarget_nchars = tstrlen(ctx.realtarget);
2380 if (progress_func) {
2382 if (extract_flags & WIMLIB_EXTRACT_FLAG_IMAGEMODE)
2383 msg = WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN;
2385 msg = WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN;
2386 progress_func(msg, &ctx.progress);
2389 if (!ctx.root_dentry_is_special)
2391 tchar path[ctx.ops->path_max];
2392 if (build_extraction_path(path, ctx.extract_root, &ctx))
2394 ret = extract_inode(path, &ctx, ctx.extract_root->d_inode);
2396 goto out_free_realtarget;
2400 /* If we need to fix up the targets of absolute symbolic links
2401 * (WIMLIB_EXTRACT_FLAG_RPFIX) or the extraction mode requires paths to
2402 * be absolute, use realpath() (or its replacement on Windows) to get
2403 * the absolute path to the extraction target. Note that this requires
2404 * the target directory to exist, unless
2405 * realpath_works_on_nonexisting_files is set in the apply_operations.
2407 if (!ctx.realtarget &&
2408 (((extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX) &&
2409 required_features.symlink_reparse_points) ||
2410 ctx.ops->requires_realtarget_in_paths))
2412 ctx.realtarget = realpath(target, NULL);
2413 if (!ctx.realtarget) {
2414 ret = WIMLIB_ERR_NOMEM;
2415 goto out_free_realtarget;
2417 ctx.realtarget_nchars = tstrlen(ctx.realtarget);
2420 if (ctx.ops->requires_short_name_reordering) {
2421 ret = for_dentry_in_tree(ctx.extract_root, dentry_extract_dir_skeleton,
2424 goto out_free_realtarget;
2427 /* Finally, the important part: extract the tree of files. */
2428 if (!(extract_flags & WIMLIB_EXTRACT_FLAG_FILE_ORDER)) {
2429 /* Sequential extraction requested, so two passes are needed
2430 * (one for directory structure, one for streams.) */
2432 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN,
2435 if (!(extract_flags & WIMLIB_EXTRACT_FLAG_RESUME)) {
2436 ret = for_dentry_in_tree(ctx.extract_root, dentry_extract_skeleton, &ctx);
2438 goto out_free_realtarget;
2441 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_END,
2443 if (extract_flags & WIMLIB_EXTRACT_FLAG_FROM_PIPE)
2444 ret = extract_streams_from_pipe(&ctx);
2446 ret = extract_stream_list(&ctx);
2448 goto out_free_realtarget;
2450 /* Sequential extraction was not requested, so we can make do
2451 * with one pass where we both create the files and extract
2454 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN,
2456 ret = for_dentry_in_tree(ctx.extract_root, dentry_extract, &ctx);
2458 goto out_free_realtarget;
2460 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_END,
2464 /* If the total number of bytes to extract was miscalculated, just jump
2465 * to the calculated number in order to avoid confusing the progress
2466 * function. This should only occur when extracting from a pipe. */
2467 if (ctx.progress.extract.completed_bytes != ctx.progress.extract.total_bytes)
2469 DEBUG("Calculated %"PRIu64" bytes to extract, but actually "
2470 "extracted %"PRIu64,
2471 ctx.progress.extract.total_bytes,
2472 ctx.progress.extract.completed_bytes);
2474 if (progress_func &&
2475 ctx.progress.extract.completed_bytes < ctx.progress.extract.total_bytes)
2477 ctx.progress.extract.completed_bytes = ctx.progress.extract.total_bytes;
2478 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS, &ctx.progress);
2481 /* Apply security descriptors and timestamps. This is done at the end,
2482 * and in a depth-first manner, to prevent timestamps from getting
2483 * changed by subsequent extract operations and to minimize the chance
2484 * of the restored security descriptors getting in our way. */
2486 progress_func(WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS,
2488 ret = for_dentry_in_tree_depth(ctx.extract_root, dentry_extract_final, &ctx);
2490 goto out_free_realtarget;
2492 if (progress_func) {
2494 if (extract_flags & WIMLIB_EXTRACT_FLAG_IMAGEMODE)
2495 msg = WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END;
2497 msg = WIMLIB_PROGRESS_MSG_EXTRACT_TREE_END;
2498 progress_func(msg, &ctx.progress);
2501 do_extract_warnings(&ctx);
2504 out_free_realtarget:
2505 FREE(ctx.realtarget);
2506 out_teardown_stream_list:
2507 /* Free memory allocated as part of the mapping from each
2508 * wim_lookup_table_entry to the dentries that reference it. */
2509 if (!(ctx.extract_flags & WIMLIB_EXTRACT_FLAG_FILE_ORDER))
2510 list_for_each_entry(lte, &ctx.stream_list, extraction_list)
2511 if (lte->out_refcnt > ARRAY_LEN(lte->inline_lte_dentries))
2512 FREE(lte->lte_dentries);
2513 out_finish_or_abort_extract:
2515 if (ctx.ops->abort_extract)
2516 ctx.ops->abort_extract(&ctx);
2518 if (ctx.ops->finish_extract)
2519 ret = ctx.ops->finish_extract(&ctx);
2521 out_dentry_reset_needs_extraction:
2522 for_dentry_in_tree(ctx.extract_root, dentry_reset_needs_extraction, NULL);
2526 /* Make sure the extraction flags make sense, and update them if needed. */
2528 check_extract_flags(const WIMStruct *wim, int *extract_flags_p)
2530 int extract_flags = *extract_flags_p;
2532 /* Check for invalid flag combinations */
2533 if ((extract_flags &
2534 (WIMLIB_EXTRACT_FLAG_SYMLINK |
2535 WIMLIB_EXTRACT_FLAG_HARDLINK)) == (WIMLIB_EXTRACT_FLAG_SYMLINK |
2536 WIMLIB_EXTRACT_FLAG_HARDLINK))
2537 return WIMLIB_ERR_INVALID_PARAM;
2539 if ((extract_flags &
2540 (WIMLIB_EXTRACT_FLAG_NO_ACLS |
2541 WIMLIB_EXTRACT_FLAG_STRICT_ACLS)) == (WIMLIB_EXTRACT_FLAG_NO_ACLS |
2542 WIMLIB_EXTRACT_FLAG_STRICT_ACLS))
2543 return WIMLIB_ERR_INVALID_PARAM;
2545 if ((extract_flags &
2546 (WIMLIB_EXTRACT_FLAG_RPFIX |
2547 WIMLIB_EXTRACT_FLAG_NORPFIX)) == (WIMLIB_EXTRACT_FLAG_RPFIX |
2548 WIMLIB_EXTRACT_FLAG_NORPFIX))
2549 return WIMLIB_ERR_INVALID_PARAM;
2551 if ((extract_flags &
2552 (WIMLIB_EXTRACT_FLAG_RESUME |
2553 WIMLIB_EXTRACT_FLAG_FROM_PIPE)) == WIMLIB_EXTRACT_FLAG_RESUME)
2554 return WIMLIB_ERR_INVALID_PARAM;
2556 #ifndef WITH_NTFS_3G
2557 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
2558 ERROR("wimlib was compiled without support for NTFS-3g, so\n"
2559 " we cannot apply a WIM image directly to a NTFS volume.");
2560 return WIMLIB_ERR_UNSUPPORTED;
2564 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_RPFIX |
2565 WIMLIB_EXTRACT_FLAG_NORPFIX)) == 0)
2567 /* Do reparse point fixups by default if the WIM header says
2568 * they are enabled. */
2569 if (wim->hdr.flags & WIM_HDR_FLAG_RP_FIX)
2570 extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
2573 /* TODO: Since UNIX data entries are stored in the file resources, in a
2574 * completely sequential extraction they may come up before the
2575 * corresponding file or symbolic link data. This needs to be handled
2577 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_UNIX_DATA |
2578 WIMLIB_EXTRACT_FLAG_FILE_ORDER))
2579 == WIMLIB_EXTRACT_FLAG_UNIX_DATA)
2581 if (extract_flags & WIMLIB_EXTRACT_FLAG_FROM_PIPE) {
2582 WARNING("Setting UNIX file/owner group may "
2583 "be impossible on some\n"
2585 "when applying from a pipe.");
2587 extract_flags |= WIMLIB_EXTRACT_FLAG_FILE_ORDER;
2588 WARNING("Disabling sequential extraction for "
2593 *extract_flags_p = extract_flags;
2598 get_wildcard_flags(int extract_flags)
2600 u32 wildcard_flags = 0;
2602 if (extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_GLOB)
2603 wildcard_flags |= WILDCARD_FLAG_ERROR_IF_NO_MATCH;
2605 wildcard_flags |= WILDCARD_FLAG_WARN_IF_NO_MATCH;
2607 if (default_ignore_case)
2608 wildcard_flags |= WILDCARD_FLAG_CASE_INSENSITIVE;
2610 return wildcard_flags;
2613 struct append_dentry_ctx {
2614 struct wim_dentry **dentries;
2615 size_t num_dentries;
2616 size_t num_alloc_dentries;
2620 append_dentry_cb(struct wim_dentry *dentry, void *_ctx)
2622 struct append_dentry_ctx *ctx = _ctx;
2624 if (ctx->num_dentries == ctx->num_alloc_dentries) {
2625 struct wim_dentry **new_dentries;
2628 new_length = max(ctx->num_alloc_dentries + 8,
2629 ctx->num_alloc_dentries * 3 / 2);
2630 new_dentries = REALLOC(ctx->dentries,
2631 new_length * sizeof(ctx->dentries[0]));
2632 if (new_dentries == NULL)
2633 return WIMLIB_ERR_NOMEM;
2634 ctx->dentries = new_dentries;
2635 ctx->num_alloc_dentries = new_length;
2637 ctx->dentries[ctx->num_dentries++] = dentry;
2642 do_wimlib_extract_paths(WIMStruct *wim,
2644 const tchar *target,
2645 const tchar * const *paths,
2648 wimlib_progress_func_t progress_func)
2651 struct wim_dentry **trees;
2654 if (wim == NULL || target == NULL || target[0] == T('\0') ||
2655 (num_paths != 0 && paths == NULL))
2656 return WIMLIB_ERR_INVALID_PARAM;
2658 ret = check_extract_flags(wim, &extract_flags);
2662 ret = select_wim_image(wim, image);
2666 ret = wim_checksum_unhashed_streams(wim);
2670 if (extract_flags & WIMLIB_EXTRACT_FLAG_GLOB_PATHS) {
2672 struct append_dentry_ctx append_dentry_ctx = {
2675 .num_alloc_dentries = 0,
2678 u32 wildcard_flags = get_wildcard_flags(extract_flags);
2680 for (size_t i = 0; i < num_paths; i++) {
2681 tchar *path = canonicalize_wim_path(paths[i]);
2683 ret = WIMLIB_ERR_NOMEM;
2684 trees = append_dentry_ctx.dentries;
2685 goto out_free_trees;
2687 ret = expand_wildcard(wim, path,
2693 trees = append_dentry_ctx.dentries;
2694 goto out_free_trees;
2697 trees = append_dentry_ctx.dentries;
2698 num_trees = append_dentry_ctx.num_dentries;
2700 trees = MALLOC(num_paths * sizeof(trees[0]));
2702 return WIMLIB_ERR_NOMEM;
2704 for (size_t i = 0; i < num_paths; i++) {
2706 tchar *path = canonicalize_wim_path(paths[i]);
2708 ret = WIMLIB_ERR_NOMEM;
2709 goto out_free_trees;
2712 trees[i] = get_dentry(wim, path,
2713 WIMLIB_CASE_PLATFORM_DEFAULT);
2715 if (trees[i] == NULL) {
2716 ERROR("Path \"%"TS"\" does not exist "
2718 paths[i], wim->current_image);
2719 ret = WIMLIB_ERR_PATH_DOES_NOT_EXIST;
2720 goto out_free_trees;
2723 num_trees = num_paths;
2726 if (num_trees == 0) {
2728 goto out_free_trees;
2731 ret = extract_trees(wim, trees, num_trees,
2732 target, extract_flags, progress_func);
2739 extract_single_image(WIMStruct *wim, int image,
2740 const tchar *target, int extract_flags,
2741 wimlib_progress_func_t progress_func)
2743 const tchar *path = T("");
2744 return do_wimlib_extract_paths(wim, image, target, &path, 1,
2745 extract_flags | WIMLIB_EXTRACT_FLAG_IMAGEMODE,
2749 static const tchar * const filename_forbidden_chars =
2758 /* This function checks if it is okay to use a WIM image's name as a directory
2761 image_name_ok_as_dir(const tchar *image_name)
2763 return image_name && *image_name &&
2764 !tstrpbrk(image_name, filename_forbidden_chars) &&
2765 tstrcmp(image_name, T(".")) &&
2766 tstrcmp(image_name, T(".."));
2769 /* Extracts all images from the WIM to the directory @target, with the images
2770 * placed in subdirectories named by their image names. */
2772 extract_all_images(WIMStruct *wim,
2773 const tchar *target,
2775 wimlib_progress_func_t progress_func)
2777 size_t image_name_max_len = max(xml_get_max_image_name_len(wim), 20);
2778 size_t output_path_len = tstrlen(target);
2779 tchar buf[output_path_len + 1 + image_name_max_len + 1];
2782 const tchar *image_name;
2785 extract_flags |= WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
2787 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
2788 ERROR("Cannot extract multiple images in NTFS extraction mode.");
2789 return WIMLIB_ERR_INVALID_PARAM;
2792 if (tstat(target, &stbuf)) {
2793 if (errno == ENOENT) {
2794 if (tmkdir(target, 0755)) {
2795 ERROR_WITH_ERRNO("Failed to create directory \"%"TS"\"", target);
2796 return WIMLIB_ERR_MKDIR;
2799 ERROR_WITH_ERRNO("Failed to stat \"%"TS"\"", target);
2800 return WIMLIB_ERR_STAT;
2802 } else if (!S_ISDIR(stbuf.st_mode)) {
2803 ERROR("\"%"TS"\" is not a directory", target);
2804 return WIMLIB_ERR_NOTDIR;
2807 tmemcpy(buf, target, output_path_len);
2808 buf[output_path_len] = OS_PREFERRED_PATH_SEPARATOR;
2809 for (image = 1; image <= wim->hdr.image_count; image++) {
2810 image_name = wimlib_get_image_name(wim, image);
2811 if (image_name_ok_as_dir(image_name)) {
2812 tstrcpy(buf + output_path_len + 1, image_name);
2814 /* Image name is empty or contains forbidden characters.
2815 * Use image number instead. */
2816 tsprintf(buf + output_path_len + 1, T("%d"), image);
2818 ret = extract_single_image(wim, image, buf, extract_flags,
2827 clear_lte_extracted_file(WIMStruct *wim, int extract_flags)
2829 if (unlikely(extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
2830 WIMLIB_EXTRACT_FLAG_HARDLINK)))
2831 for_lookup_table_entry(wim->lookup_table,
2832 lte_free_extracted_file, NULL);
2836 do_wimlib_extract_image(WIMStruct *wim,
2838 const tchar *target,
2840 wimlib_progress_func_t progress_func)
2844 if (extract_flags & (WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE |
2845 WIMLIB_EXTRACT_FLAG_TO_STDOUT))
2846 return WIMLIB_ERR_INVALID_PARAM;
2848 if (image == WIMLIB_ALL_IMAGES)
2849 ret = extract_all_images(wim, target, extract_flags,
2852 ret = extract_single_image(wim, image, target, extract_flags,
2855 clear_lte_extracted_file(wim, extract_flags);
2860 /****************************************************************************
2862 ****************************************************************************/
2864 /* Note: new code should use wimlib_extract_paths() instead of
2865 * wimlib_extract_files() if possible. */
2867 wimlib_extract_files(WIMStruct *wim,
2869 const struct wimlib_extract_command *cmds,
2871 int default_extract_flags,
2872 wimlib_progress_func_t progress_func)
2881 default_extract_flags |= WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE;
2883 for (size_t i = 0; i < num_cmds; i++) {
2884 int cmd_flags = ((cmds[i].extract_flags |
2885 default_extract_flags) &
2886 WIMLIB_EXTRACT_MASK_PUBLIC);
2887 int cmd_link_flags = (cmd_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
2888 WIMLIB_EXTRACT_FLAG_HARDLINK));
2890 link_flags = cmd_link_flags;
2892 if (cmd_link_flags != link_flags) {
2893 ERROR("The same symlink or hardlink extraction mode "
2894 "must be set on all extraction commands!");
2895 return WIMLIB_ERR_INVALID_PARAM;
2898 all_flags |= cmd_flags;
2900 if (all_flags & WIMLIB_EXTRACT_FLAG_GLOB_PATHS) {
2901 ERROR("Glob paths not supported for wimlib_extract_files(). "
2902 "Use wimlib_extract_paths() instead.");
2903 return WIMLIB_ERR_INVALID_PARAM;
2906 for (size_t i = 0; i < num_cmds; i++) {
2907 int extract_flags = ((cmds[i].extract_flags |
2908 default_extract_flags) &
2909 WIMLIB_EXTRACT_MASK_PUBLIC);
2910 const tchar *target = cmds[i].fs_dest_path;
2911 const tchar *wim_source_path = cmds[i].wim_source_path;
2913 ret = do_wimlib_extract_paths(wim, image, target,
2914 &wim_source_path, 1,
2915 extract_flags | WIMLIB_EXTRACT_FLAG_FILEMODE,
2921 clear_lte_extracted_file(wim, all_flags);
2926 wimlib_extract_paths(WIMStruct *wim,
2928 const tchar *target,
2929 const tchar * const *paths,
2932 wimlib_progress_func_t progress_func)
2936 extract_flags &= WIMLIB_EXTRACT_MASK_PUBLIC;
2938 ret = do_wimlib_extract_paths(wim, image, target, paths, num_paths,
2939 extract_flags, progress_func);
2940 clear_lte_extracted_file(wim, extract_flags);
2945 wimlib_extract_pathlist(WIMStruct *wim, int image,
2946 const tchar *target,
2947 const tchar *path_list_file,
2949 wimlib_progress_func_t progress_func)
2956 ret = read_path_list_file(path_list_file, &paths, &num_paths, &mem);
2958 ERROR("Failed to read path list file \"%"TS"\"",
2963 ret = wimlib_extract_paths(wim, image, target,
2964 (const tchar * const *)paths, num_paths,
2965 extract_flags, progress_func);
2972 wimlib_extract_image_from_pipe(int pipe_fd, const tchar *image_num_or_name,
2973 const tchar *target, int extract_flags,
2974 wimlib_progress_func_t progress_func)
2978 struct filedes *in_fd;
2982 extract_flags &= WIMLIB_EXTRACT_MASK_PUBLIC;
2984 if (extract_flags & WIMLIB_EXTRACT_FLAG_FILE_ORDER)
2985 return WIMLIB_ERR_INVALID_PARAM;
2987 /* Read the WIM header from the pipe and get a WIMStruct to represent
2988 * the pipable WIM. Caveats: Unlike getting a WIMStruct with
2989 * wimlib_open_wim(), getting a WIMStruct in this way will result in
2990 * an empty lookup table, no XML data read, and no filename set. */
2991 ret = open_wim_as_WIMStruct(&pipe_fd,
2992 WIMLIB_OPEN_FLAG_FROM_PIPE,
2993 &pwm, progress_func);
2997 /* Sanity check to make sure this is a pipable WIM. */
2998 if (pwm->hdr.magic != PWM_MAGIC) {
2999 ERROR("The WIM being read from file descriptor %d "
3000 "is not pipable!", pipe_fd);
3001 ret = WIMLIB_ERR_NOT_PIPABLE;
3002 goto out_wimlib_free;
3005 /* Sanity check to make sure the first part of a pipable split WIM is
3006 * sent over the pipe first. */
3007 if (pwm->hdr.part_number != 1) {
3008 ERROR("The first part of the split WIM must be "
3009 "sent over the pipe first.");
3010 ret = WIMLIB_ERR_INVALID_PIPABLE_WIM;
3011 goto out_wimlib_free;
3014 in_fd = &pwm->in_fd;
3015 wimlib_assert(in_fd->offset == WIM_HEADER_DISK_SIZE);
3017 /* As mentioned, the WIMStruct we created from the pipe does not have
3018 * XML data yet. Fix this by reading the extra copy of the XML data
3019 * that directly follows the header in pipable WIMs. (Note: see
3020 * write_pipable_wim() for more details about the format of pipable
3023 struct wim_lookup_table_entry xml_lte;
3024 struct wim_resource_spec xml_rspec;
3025 ret = read_pwm_stream_header(pwm, &xml_lte, &xml_rspec, 0, NULL);
3027 goto out_wimlib_free;
3029 if (!(xml_lte.flags & WIM_RESHDR_FLAG_METADATA))
3031 ERROR("Expected XML data, but found non-metadata "
3033 ret = WIMLIB_ERR_INVALID_PIPABLE_WIM;
3034 goto out_wimlib_free;
3037 wim_res_spec_to_hdr(&xml_rspec, &pwm->hdr.xml_data_reshdr);
3039 ret = read_wim_xml_data(pwm);
3041 goto out_wimlib_free;
3043 if (wim_info_get_num_images(pwm->wim_info) != pwm->hdr.image_count) {
3044 ERROR("Image count in XML data is not the same as in WIM header.");
3045 ret = WIMLIB_ERR_XML;
3046 goto out_wimlib_free;
3050 /* Get image index (this may use the XML data that was just read to
3051 * resolve an image name). */
3052 if (image_num_or_name) {
3053 image = wimlib_resolve_image(pwm, image_num_or_name);
3054 if (image == WIMLIB_NO_IMAGE) {
3055 ERROR("\"%"TS"\" is not a valid image in the pipable WIM!",
3057 ret = WIMLIB_ERR_INVALID_IMAGE;
3058 goto out_wimlib_free;
3059 } else if (image == WIMLIB_ALL_IMAGES) {
3060 ERROR("Applying all images from a pipe is not supported.");
3061 ret = WIMLIB_ERR_INVALID_IMAGE;
3062 goto out_wimlib_free;
3065 if (pwm->hdr.image_count != 1) {
3066 ERROR("No image was specified, but the pipable WIM "
3067 "did not contain exactly 1 image");
3068 ret = WIMLIB_ERR_INVALID_IMAGE;
3069 goto out_wimlib_free;
3074 /* Load the needed metadata resource. */
3075 for (i = 1; i <= pwm->hdr.image_count; i++) {
3076 struct wim_lookup_table_entry *metadata_lte;
3077 struct wim_image_metadata *imd;
3078 struct wim_resource_spec *metadata_rspec;
3080 metadata_lte = new_lookup_table_entry();
3081 if (metadata_lte == NULL) {
3082 ret = WIMLIB_ERR_NOMEM;
3083 goto out_wimlib_free;
3085 metadata_rspec = MALLOC(sizeof(struct wim_resource_spec));
3086 if (metadata_rspec == NULL) {
3087 ret = WIMLIB_ERR_NOMEM;
3088 free_lookup_table_entry(metadata_lte);
3089 goto out_wimlib_free;
3092 ret = read_pwm_stream_header(pwm, metadata_lte, metadata_rspec, 0, NULL);
3093 imd = pwm->image_metadata[i - 1];
3094 imd->metadata_lte = metadata_lte;
3096 FREE(metadata_rspec);
3097 goto out_wimlib_free;
3100 if (!(metadata_lte->flags & WIM_RESHDR_FLAG_METADATA)) {
3101 ERROR("Expected metadata resource, but found "
3102 "non-metadata stream.");
3103 ret = WIMLIB_ERR_INVALID_PIPABLE_WIM;
3104 goto out_wimlib_free;
3108 /* Metadata resource is for the images being extracted.
3109 * Parse it and save the metadata in memory. */
3110 ret = read_metadata_resource(pwm, imd);
3112 goto out_wimlib_free;
3115 /* Metadata resource is not for the image being
3116 * extracted. Skip over it. */
3117 ret = skip_wim_stream(metadata_lte);
3119 goto out_wimlib_free;
3122 /* Extract the image. */
3123 extract_flags |= WIMLIB_EXTRACT_FLAG_FROM_PIPE;
3124 ret = do_wimlib_extract_image(pwm, image, target,
3125 extract_flags, progress_func);
3126 /* Clean up and return. */
3133 wimlib_extract_image(WIMStruct *wim,
3135 const tchar *target,
3137 wimlib_progress_func_t progress_func)
3139 extract_flags &= WIMLIB_EXTRACT_MASK_PUBLIC;
3140 return do_wimlib_extract_image(wim, image, target, extract_flags,