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
75 /* Keep in sync with wimlib.h */
76 #define WIMLIB_EXTRACT_MASK_PUBLIC \
77 (WIMLIB_EXTRACT_FLAG_NTFS | \
78 WIMLIB_EXTRACT_FLAG_HARDLINK | \
79 WIMLIB_EXTRACT_FLAG_SYMLINK | \
80 WIMLIB_EXTRACT_FLAG_VERBOSE | \
81 WIMLIB_EXTRACT_FLAG_SEQUENTIAL | \
82 WIMLIB_EXTRACT_FLAG_UNIX_DATA | \
83 WIMLIB_EXTRACT_FLAG_NO_ACLS | \
84 WIMLIB_EXTRACT_FLAG_STRICT_ACLS | \
85 WIMLIB_EXTRACT_FLAG_RPFIX | \
86 WIMLIB_EXTRACT_FLAG_NORPFIX | \
87 WIMLIB_EXTRACT_FLAG_TO_STDOUT | \
88 WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES | \
89 WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS | \
90 WIMLIB_EXTRACT_FLAG_STRICT_TIMESTAMPS | \
91 WIMLIB_EXTRACT_FLAG_STRICT_SHORT_NAMES | \
92 WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS | \
93 WIMLIB_EXTRACT_FLAG_RESUME | \
94 WIMLIB_EXTRACT_FLAG_FILE_ORDER | \
95 WIMLIB_EXTRACT_FLAG_GLOB_PATHS | \
96 WIMLIB_EXTRACT_FLAG_STRICT_GLOB | \
97 WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES | \
98 WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE)
100 /* Given a WIM dentry in the tree to be extracted, resolve all streams in the
101 * corresponding inode and set 'out_refcnt' in each to 0. */
103 dentry_resolve_and_zero_lte_refcnt(struct wim_dentry *dentry, void *_ctx)
105 struct apply_ctx *ctx = _ctx;
106 struct wim_inode *inode = dentry->d_inode;
107 struct wim_lookup_table_entry *lte;
111 if (dentry->extraction_skipped)
114 /* Special case: when extracting from a pipe, the WIM lookup table is
115 * initially empty, so "resolving" an inode's streams is initially not
116 * possible. However, we still need to keep track of which streams,
117 * identified by SHA1 message digests, need to be extracted, so we
118 * "resolve" the inode's streams anyway by allocating new entries. */
119 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_FROM_PIPE)
121 ret = inode_resolve_streams(inode, ctx->wim->lookup_table, force);
124 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
125 lte = inode_stream_lte_resolved(inode, i);
133 is_linked_extraction(const struct apply_ctx *ctx)
135 return 0 != (ctx->extract_flags & (WIMLIB_EXTRACT_FLAG_HARDLINK |
136 WIMLIB_EXTRACT_FLAG_SYMLINK));
140 can_extract_named_data_streams(const struct apply_ctx *ctx)
142 return ctx->supported_features.named_data_streams &&
143 !is_linked_extraction(ctx);
147 ref_stream_to_extract(struct wim_lookup_table_entry *lte,
148 struct wim_dentry *dentry, struct apply_ctx *ctx)
153 /* Tally the size only for each extraction of the stream (not hard
155 if (!(dentry->d_inode->i_visited &&
156 ctx->supported_features.hard_links) &&
157 (!is_linked_extraction(ctx) || (lte->out_refcnt == 0 &&
158 lte->extracted_file == NULL)))
160 ctx->progress.extract.total_bytes += lte->size;
161 ctx->progress.extract.num_streams++;
164 /* Add stream to the extraction_list only one time, even if it's going
165 * to be extracted to multiple locations. */
166 if (lte->out_refcnt == 0) {
167 list_add_tail(<e->extraction_list, &ctx->stream_list);
168 ctx->num_streams_remaining++;
171 if (!(ctx->extract_flags & WIMLIB_EXTRACT_FLAG_FILE_ORDER)) {
172 struct wim_dentry **lte_dentries;
174 /* Append dentry to this stream's array of dentries referencing
175 * it. Use inline array to avoid memory allocation until the
176 * number of dentries becomes too large. */
177 if (lte->out_refcnt < ARRAY_LEN(lte->inline_lte_dentries)) {
178 lte_dentries = lte->inline_lte_dentries;
180 struct wim_dentry **prev_lte_dentries;
181 size_t alloc_lte_dentries;
183 if (lte->out_refcnt == ARRAY_LEN(lte->inline_lte_dentries)) {
184 prev_lte_dentries = NULL;
185 alloc_lte_dentries = ARRAY_LEN(lte->inline_lte_dentries);
187 prev_lte_dentries = lte->lte_dentries;
188 alloc_lte_dentries = lte->alloc_lte_dentries;
191 if (lte->out_refcnt == alloc_lte_dentries) {
192 alloc_lte_dentries *= 2;
193 lte_dentries = REALLOC(prev_lte_dentries,
195 sizeof(lte_dentries[0]));
196 if (lte_dentries == NULL)
197 return WIMLIB_ERR_NOMEM;
198 if (prev_lte_dentries == NULL) {
200 lte->inline_lte_dentries,
201 sizeof(lte->inline_lte_dentries));
203 lte->lte_dentries = lte_dentries;
204 lte->alloc_lte_dentries = alloc_lte_dentries;
206 lte_dentries = lte->lte_dentries;
208 lte_dentries[lte->out_refcnt] = dentry;
214 /* Given a WIM dentry in the tree to be extracted, iterate through streams that
215 * need to be extracted. For each one, add it to the list of streams to be
216 * extracted (ctx->stream_list) if not already done so, and also update the
217 * progress information (ctx->progress) with the stream. Furthermore, if doing
218 * a sequential extraction, build a mapping from each the stream to the dentries
221 * This uses the i_visited member of the inodes (assumed to be 0 initially). */
223 dentry_add_streams_to_extract(struct wim_dentry *dentry, void *_ctx)
225 struct apply_ctx *ctx = _ctx;
226 struct wim_inode *inode = dentry->d_inode;
229 /* Don't process dentries marked as skipped. */
230 if (dentry->extraction_skipped)
233 /* The unnamed data stream will always be extracted, except in an
235 if (!inode_is_encrypted_directory(inode)) {
236 ret = ref_stream_to_extract(inode_unnamed_lte_resolved(inode),
242 /* Named data streams will be extracted only if supported in the current
243 * extraction mode and volume, and to avoid complications, if not doing
244 * a linked extraction. */
245 if (can_extract_named_data_streams(ctx)) {
246 for (u16 i = 0; i < inode->i_num_ads; i++) {
247 if (!ads_entry_is_named_stream(&inode->i_ads_entries[i]))
249 ret = ref_stream_to_extract(inode->i_ads_entries[i].lte,
255 inode->i_visited = 1;
259 /* Inform library user of progress of stream extraction following the successful
260 * extraction of a copy of the stream specified by @lte. */
262 update_extract_progress(struct apply_ctx *ctx,
263 const struct wim_lookup_table_entry *lte)
265 wimlib_progress_func_t progress_func = ctx->progress_func;
266 union wimlib_progress_info *progress = &ctx->progress;
268 progress->extract.completed_bytes += lte->size;
270 progress->extract.completed_bytes >= ctx->next_progress)
272 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS, progress);
273 if (progress->extract.completed_bytes >=
274 progress->extract.total_bytes)
276 ctx->next_progress = ~0ULL;
278 ctx->next_progress += progress->extract.total_bytes / 128;
279 if (ctx->next_progress > progress->extract.total_bytes)
280 ctx->next_progress = progress->extract.total_bytes;
286 /* Extract a symbolic link (not directly as reparse data), handling fixing up
287 * the target of absolute symbolic links and updating the extract progress.
289 * @inode must specify the WIM inode for a symbolic link or junction reparse
292 * @lte_override overrides the resource used as the reparse data for the
295 extract_symlink(const tchar *path, struct apply_ctx *ctx,
296 struct wim_inode *inode,
297 struct wim_lookup_table_entry *lte_override)
299 ssize_t bufsize = ctx->ops->path_max;
300 tchar target[bufsize];
306 /* If absolute symbolic link fixups requested, reserve space in the link
307 * target buffer for the absolute path of the target directory. */
308 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX)
310 buf += ctx->realtarget_nchars;
311 bufsize -= ctx->realtarget_nchars;
314 /* Translate the WIM inode's reparse data into the link target. */
315 sret = wim_inode_readlink(inode, buf, bufsize - 1, lte_override);
318 return WIMLIB_ERR_READLINK;
322 if ((ctx->extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX) &&
325 /* Fix absolute symbolic link target to point into the
326 * actual extraction destination. */
327 tmemcpy(target, ctx->realtarget, ctx->realtarget_nchars);
328 fixed_target = target;
330 /* Keep same link target. */
334 /* Call into the apply_operations to create the symbolic link. */
335 DEBUG("Creating symlink \"%"TS"\" => \"%"TS"\"",
337 ret = ctx->ops->create_symlink(fixed_target, path, ctx);
339 ERROR_WITH_ERRNO("Failed to create symlink "
340 "\"%"TS"\" => \"%"TS"\"", path, fixed_target);
344 /* Account for reparse data consumed. */
345 update_extract_progress(ctx,
346 (lte_override ? lte_override :
347 inode_unnamed_lte_resolved(inode)));
350 #endif /* !__WIN32__ */
352 /* Create a file, directory, or symbolic link. */
354 extract_inode(const tchar *path, struct apply_ctx *ctx, struct wim_inode *inode)
359 if (ctx->supported_features.symlink_reparse_points &&
360 !ctx->supported_features.reparse_points &&
361 inode_is_symlink(inode))
363 ret = extract_symlink(path, ctx, inode, NULL);
365 #endif /* !__WIN32__ */
366 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY) {
367 ret = ctx->ops->create_directory(path, ctx, &inode->extract_cookie);
369 ERROR_WITH_ERRNO("Failed to create the directory "
372 } else if ((inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED) &&
373 ctx->ops->extract_encrypted_stream_creates_file &&
374 ctx->supported_features.encrypted_files) {
375 ret = ctx->ops->extract_encrypted_stream(
376 path, inode_unnamed_lte_resolved(inode), ctx);
378 ERROR_WITH_ERRNO("Failed to create and extract "
379 "encrypted file \"%"TS"\"", path);
382 ret = ctx->ops->create_file(path, ctx, &inode->extract_cookie);
384 ERROR_WITH_ERRNO("Failed to create the file "
392 extract_hardlink(const tchar *oldpath, const tchar *newpath,
393 struct apply_ctx *ctx)
397 DEBUG("Creating hardlink \"%"TS"\" => \"%"TS"\"", newpath, oldpath);
398 ret = ctx->ops->create_hardlink(oldpath, newpath, ctx);
400 ERROR_WITH_ERRNO("Failed to create hardlink "
401 "\"%"TS"\" => \"%"TS"\"",
409 try_extract_rpfix(u8 *rpbuf,
411 const wchar_t *extract_root_realpath,
412 unsigned extract_root_realpath_nchars)
414 struct reparse_data rpdata;
416 size_t target_nchars;
417 size_t stripped_nchars;
418 wchar_t *stripped_target;
419 wchar_t stripped_target_nchars;
422 utf16lechar *new_target;
423 utf16lechar *new_print_name;
424 size_t new_target_nchars;
425 size_t new_print_name_nchars;
428 ret = parse_reparse_data(rpbuf, *rpbuflen_p, &rpdata);
432 if (extract_root_realpath[0] == L'\0' ||
433 extract_root_realpath[1] != L':' ||
434 extract_root_realpath[2] != L'\\')
435 return WIMLIB_ERR_REPARSE_POINT_FIXUP_FAILED;
437 ret = parse_substitute_name(rpdata.substitute_name,
438 rpdata.substitute_name_nbytes,
442 stripped_nchars = ret;
443 target = rpdata.substitute_name;
444 target_nchars = rpdata.substitute_name_nbytes / sizeof(utf16lechar);
445 stripped_target = target + stripped_nchars;
446 stripped_target_nchars = target_nchars - stripped_nchars;
448 new_target = alloca((6 + extract_root_realpath_nchars +
449 stripped_target_nchars) * sizeof(utf16lechar));
452 if (stripped_nchars == 6) {
453 /* Include \??\ prefix if it was present before */
454 p = wmempcpy(p, L"\\??\\", 4);
457 /* Print name excludes the \??\ if present. */
459 if (stripped_nchars != 0) {
460 /* Get drive letter from real path to extract root, if a drive
461 * letter was present before. */
462 *p++ = extract_root_realpath[0];
463 *p++ = extract_root_realpath[1];
465 /* Copy the rest of the extract root */
466 p = wmempcpy(p, extract_root_realpath + 2, extract_root_realpath_nchars - 2);
468 /* Append the stripped target */
469 p = wmempcpy(p, stripped_target, stripped_target_nchars);
470 new_target_nchars = p - new_target;
471 new_print_name_nchars = p - new_print_name;
473 if (new_target_nchars * sizeof(utf16lechar) >= REPARSE_POINT_MAX_SIZE ||
474 new_print_name_nchars * sizeof(utf16lechar) >= REPARSE_POINT_MAX_SIZE)
475 return WIMLIB_ERR_REPARSE_POINT_FIXUP_FAILED;
477 rpdata.substitute_name = new_target;
478 rpdata.substitute_name_nbytes = new_target_nchars * sizeof(utf16lechar);
479 rpdata.print_name = new_print_name;
480 rpdata.print_name_nbytes = new_print_name_nchars * sizeof(utf16lechar);
481 return make_reparse_buffer(&rpdata, rpbuf, rpbuflen_p);
483 #endif /* __WIN32__ */
485 /* Set reparse data on extracted file or directory that has
486 * FILE_ATTRIBUTE_REPARSE_POINT set. */
488 extract_reparse_data(const tchar *path, struct apply_ctx *ctx,
489 struct wim_inode *inode,
490 struct wim_lookup_table_entry *lte_override)
493 u8 rpbuf[REPARSE_POINT_MAX_SIZE];
496 ret = wim_inode_get_reparse_data(inode, rpbuf, &rpbuflen, lte_override);
501 /* Fix up target of absolute symbolic link or junction points so
502 * that they point into the actual extraction target. */
503 if ((ctx->extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX) &&
504 (inode->i_reparse_tag == WIM_IO_REPARSE_TAG_SYMLINK ||
505 inode->i_reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT) &&
506 !inode->i_not_rpfixed)
508 ret = try_extract_rpfix(rpbuf, &rpbuflen, ctx->realtarget,
509 ctx->realtarget_nchars);
510 if (ret && !(ctx->extract_flags &
511 WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS))
513 WARNING("Reparse point fixup of \"%"TS"\" "
522 ret = ctx->ops->set_reparse_data(path, rpbuf, rpbuflen, ctx);
524 /* On Windows, the SeCreateSymbolicLink privilege is required to create
525 * symbolic links. To be more friendly towards non-Administrator users,
526 * we merely warn the user if symbolic links cannot be created due to
527 * insufficient permissions or privileges, unless
528 * WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS was provided. */
530 if (ret && inode_is_symlink(inode) &&
531 (errno == EACCES || errno == EPERM) &&
532 !(ctx->extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS))
534 WARNING("Can't set reparse data on \"%"TS"\": "
536 " You may be trying to "
537 "extract a symbolic link without the\n"
538 " SeCreateSymbolicLink privilege, "
539 "which by default non-Administrator\n"
540 " accounts do not have.",
548 /* Account for reparse data consumed. */
549 update_extract_progress(ctx,
550 (lte_override ? lte_override :
551 inode_unnamed_lte_resolved(inode)));
555 ERROR_WITH_ERRNO("Failed to set reparse data on \"%"TS"\"", path);
560 * Extract zero or more streams to a file.
562 * This function operates slightly differently depending on whether @lte_spec is
563 * NULL or not. When @lte_spec is NULL, the behavior is to extract the default
564 * file contents (unnamed stream), and, if named data streams are supported in
565 * the extract mode and volume, any named data streams. When @lte_spec is not
566 * NULL, the behavior is to extract only all copies of the stream @lte_spec, and
567 * in addition use @lte_spec to set the reparse data or create the symbolic link
571 * Path to file to extract (as can be passed to apply_operations
576 * WIM dentry that corresponds to the file being extracted.
578 * If non-NULL, specifies the lookup table entry for a stream to extract,
579 * and only that stream will be extracted (although there may be more than
580 * one instance of it).
582 * Used only if @lte_spec != NULL; it is passed to the extraction functions
583 * rather than @lte_spec, allowing the location of the stream to be
584 * overridden. (This is used when the WIM is being read from a nonseekable
585 * file, such as a pipe, when streams need to be used more than once; each
586 * such stream is extracted to a temporary file.)
589 extract_streams(const tchar *path, struct apply_ctx *ctx,
590 struct wim_dentry *dentry,
591 struct wim_lookup_table_entry *lte_spec,
592 struct wim_lookup_table_entry *lte_override)
594 struct wim_inode *inode = dentry->d_inode;
595 struct wim_lookup_table_entry *lte;
596 file_spec_t file_spec;
599 if (dentry->was_hardlinked)
606 for (unsigned i = 0; i < SHA1_HASH_SIZE; i++)
607 p += sprintf(p, "%02x", lte_override->hash[i]);
608 DEBUG("Extracting stream SHA1=%s to \"%"TS"\"",
609 sha1_str, path, inode->i_ino);
611 DEBUG("Extracting streams to \"%"TS"\"", path, inode->i_ino);
615 if (ctx->ops->uses_cookies)
616 file_spec.cookie = inode->extract_cookie;
618 file_spec.path = path;
620 /* Unnamed data stream. */
621 lte = inode_unnamed_lte_resolved(inode);
622 if (lte && (!lte_spec || lte == lte_spec)) {
625 if (!(inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
626 FILE_ATTRIBUTE_REPARSE_POINT)))
628 if (inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED &&
629 ctx->supported_features.encrypted_files) {
630 if (!ctx->ops->extract_encrypted_stream_creates_file) {
631 ret = ctx->ops->extract_encrypted_stream(
637 ret = ctx->ops->extract_unnamed_stream(
638 file_spec, lte, ctx);
642 update_extract_progress(ctx, lte);
644 else if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT)
647 if (ctx->supported_features.reparse_points)
648 ret = extract_reparse_data(path, ctx, inode, lte);
650 else if ((inode_is_symlink(inode) &&
651 ctx->supported_features.symlink_reparse_points))
652 ret = extract_symlink(path, ctx, inode, lte);
659 /* Named data streams. */
660 if (can_extract_named_data_streams(ctx)) {
661 for (u16 i = 0; i < inode->i_num_ads; i++) {
662 struct wim_ads_entry *entry = &inode->i_ads_entries[i];
664 if (!ads_entry_is_named_stream(entry))
669 if (lte_spec && lte_spec != lte)
673 ret = ctx->ops->extract_named_stream(file_spec, entry->stream_name,
674 entry->stream_name_nbytes / 2,
678 update_extract_progress(ctx, lte);
684 ERROR_WITH_ERRNO("Failed to extract data of \"%"TS"\"", path);
688 /* Set attributes on an extracted file or directory if supported by the
689 * extraction mode. */
691 extract_file_attributes(const tchar *path, struct apply_ctx *ctx,
692 struct wim_dentry *dentry, unsigned pass)
696 if (ctx->ops->set_file_attributes &&
697 !(ctx->extract_flags & WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES) &&
698 !(dentry == ctx->extract_root && ctx->root_dentry_is_special)) {
699 u32 attributes = dentry->d_inode->i_attributes;
701 /* Clear unsupported attributes. */
702 attributes &= ctx->supported_attributes_mask;
704 if ((attributes & FILE_ATTRIBUTE_DIRECTORY &&
705 !ctx->supported_features.encrypted_directories) ||
706 (!(attributes & FILE_ATTRIBUTE_DIRECTORY) &&
707 !ctx->supported_features.encrypted_files))
709 attributes &= ~FILE_ATTRIBUTE_ENCRYPTED;
713 attributes = FILE_ATTRIBUTE_NORMAL;
715 ret = ctx->ops->set_file_attributes(path, attributes, ctx, pass);
717 ERROR_WITH_ERRNO("Failed to set attributes on "
726 /* Set or remove the short (DOS) name on an extracted file or directory if
727 * supported by the extraction mode. Since DOS names are unimportant and it's
728 * easy to run into problems setting them on Windows (SetFileShortName()
729 * requires SE_RESTORE privilege, which only the Administrator can request, and
730 * also requires DELETE access to the file), failure is ignored unless
731 * WIMLIB_EXTRACT_FLAG_STRICT_SHORT_NAMES is set. */
733 extract_short_name(const tchar *path, struct apply_ctx *ctx,
734 struct wim_dentry *dentry)
738 /* The root of the dentry tree being extracted may not be extracted to
739 * its original name, so its short name should be ignored. */
740 if (dentry == ctx->extract_root)
743 if (ctx->supported_features.short_names) {
744 ret = ctx->ops->set_short_name(path,
746 dentry->short_name_nbytes / 2,
748 if (ret && (ctx->extract_flags &
749 WIMLIB_EXTRACT_FLAG_STRICT_SHORT_NAMES))
751 ERROR_WITH_ERRNO("Failed to set short name of "
759 /* Set security descriptor, UNIX data, or neither on an extracted file, taking
760 * into account the current extraction mode and flags. */
762 extract_security(const tchar *path, struct apply_ctx *ctx,
763 struct wim_dentry *dentry)
766 struct wim_inode *inode = dentry->d_inode;
768 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_NO_ACLS)
771 if ((ctx->extract_root == dentry) && ctx->root_dentry_is_special)
775 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
776 struct wimlib_unix_data data;
778 ret = inode_get_unix_data(inode, &data, NULL);
782 ret = ctx->ops->set_unix_data(path, &data, ctx);
784 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_ACLS) {
785 ERROR_WITH_ERRNO("Failed to set UNIX owner, "
786 "group, and/or mode on "
790 WARNING_WITH_ERRNO("Failed to set UNIX owner, "
791 "group, and/or/mode on "
797 #endif /* __WIN32__ */
798 if (ctx->supported_features.security_descriptors &&
799 inode->i_security_id != -1)
801 const struct wim_security_data *sd;
805 sd = wim_const_security_data(ctx->wim);
806 desc = sd->descriptors[inode->i_security_id];
807 desc_size = sd->sizes[inode->i_security_id];
809 ret = ctx->ops->set_security_descriptor(path, desc,
812 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_ACLS) {
813 ERROR_WITH_ERRNO("Failed to set security "
814 "descriptor on \"%"TS"\"", path);
818 if (errno != EACCES) {
819 WARNING_WITH_ERRNO("Failed to set "
820 "security descriptor "
821 "on \"%"TS"\"", path);
824 ctx->no_security_descriptors++;
831 /* Set timestamps on an extracted file. Failure is warning-only unless
832 * WIMLIB_EXTRACT_FLAG_STRICT_TIMESTAMPS is set. */
834 extract_timestamps(const tchar *path, struct apply_ctx *ctx,
835 struct wim_dentry *dentry)
837 struct wim_inode *inode = dentry->d_inode;
840 if ((ctx->extract_root == dentry) && ctx->root_dentry_is_special)
843 if (ctx->ops->set_timestamps) {
844 ret = ctx->ops->set_timestamps(path,
845 inode->i_creation_time,
846 inode->i_last_write_time,
847 inode->i_last_access_time,
850 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_TIMESTAMPS) {
851 ERROR_WITH_ERRNO("Failed to set timestamps "
852 "on \"%"TS"\"", path);
855 WARNING_WITH_ERRNO("Failed to set timestamps "
856 "on \"%"TS"\"", path);
863 /* Check whether the extraction of a dentry should be skipped completely. */
865 dentry_is_supported(struct wim_dentry *dentry,
866 const struct wim_features *supported_features)
868 struct wim_inode *inode = dentry->d_inode;
870 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
871 return supported_features->reparse_points ||
872 (inode_is_symlink(inode) &&
873 supported_features->symlink_reparse_points);
875 if (inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED) {
876 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)
877 return supported_features->encrypted_directories != 0;
879 return supported_features->encrypted_files != 0;
884 /* Given a WIM dentry to extract, build the path to which to extract it, in the
885 * format understood by the callbacks in the apply_operations being used.
887 * Write the resulting path into @path, which must have room for at least
888 * ctx->ops->max_path characters including the null-terminator.
890 * Return %true if successful; %false if this WIM dentry doesn't actually need
891 * to be extracted or if the calculated path exceeds ctx->ops->max_path
894 * This function clobbers the tmp_list member of @dentry and its ancestors up
895 * until the extraction root. */
897 build_extraction_path(tchar path[], struct wim_dentry *dentry,
898 struct apply_ctx *ctx)
901 LIST_HEAD(ancestor_list);
903 const tchar *target_prefix;
904 size_t target_prefix_nchars;
905 struct wim_dentry *d;
907 if (dentry->extraction_skipped)
910 path_nchars = ctx->ops->path_prefix_nchars;
912 if (ctx->ops->requires_realtarget_in_paths) {
913 target_prefix = ctx->realtarget;
914 target_prefix_nchars = ctx->realtarget_nchars;
915 } else if (ctx->ops->requires_target_in_paths) {
916 target_prefix = ctx->target;
917 target_prefix_nchars = ctx->target_nchars;
919 target_prefix = NULL;
920 target_prefix_nchars = 0;
922 path_nchars += target_prefix_nchars;
924 for (d = dentry; d != ctx->extract_root; d = d->parent) {
925 if (!d->in_extraction_tree || d->extraction_skipped)
928 path_nchars += d->extraction_name_nchars + 1;
929 list_add(&d->tmp_list, &ancestor_list);
932 path_nchars++; /* null terminator */
934 if (path_nchars > ctx->ops->path_max) {
935 WARNING("\"%"TS"\": Path too long to extract",
936 dentry_full_path(dentry));
940 p = tmempcpy(p, ctx->ops->path_prefix, ctx->ops->path_prefix_nchars);
941 p = tmempcpy(p, target_prefix, target_prefix_nchars);
942 list_for_each_entry(d, &ancestor_list, tmp_list) {
943 *p++ = ctx->ops->path_separator;
944 p = tmempcpy(p, d->extraction_name, d->extraction_name_nchars);
947 wimlib_assert(p - path == path_nchars);
952 get_num_path_components(const tchar *path, tchar path_separator)
954 unsigned num_components = 0;
956 /* Ignore drive letter. */
957 if (path[0] != L'\0' && path[1] == L':')
962 while (*path == path_separator)
966 while (*path && *path != path_separator)
969 return num_components;
973 extract_multiimage_symlink(const tchar *oldpath, const tchar *newpath,
974 struct apply_ctx *ctx, struct wim_dentry *dentry)
976 size_t num_raw_path_components;
977 const struct wim_dentry *d;
978 size_t num_target_path_components;
983 num_raw_path_components = 0;
984 for (d = dentry; d != ctx->extract_root; d = d->parent)
985 num_raw_path_components++;
987 if (ctx->ops->requires_realtarget_in_paths)
988 num_target_path_components = get_num_path_components(ctx->realtarget,
989 ctx->ops->path_separator);
990 else if (ctx->ops->requires_target_in_paths)
991 num_target_path_components = get_num_path_components(ctx->target,
992 ctx->ops->path_separator);
994 num_target_path_components = 0;
996 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE) {
997 wimlib_assert(num_target_path_components > 0);
998 num_raw_path_components++;
999 num_target_path_components--;
1002 p_old = oldpath + ctx->ops->path_prefix_nchars;
1004 if (p_old[0] != L'\0' && p_old[1] == ':')
1007 while (*p_old == ctx->ops->path_separator)
1009 while (--num_target_path_components) {
1010 while (*p_old != ctx->ops->path_separator)
1012 while (*p_old == ctx->ops->path_separator)
1016 tchar symlink_target[tstrlen(p_old) + 3 * num_raw_path_components + 1];
1018 p = &symlink_target[0];
1019 while (num_raw_path_components--) {
1022 *p++ = ctx->ops->path_separator;
1025 DEBUG("Creating symlink \"%"TS"\" => \"%"TS"\"",
1026 newpath, symlink_target);
1027 ret = ctx->ops->create_symlink(symlink_target, newpath, ctx);
1029 ERROR_WITH_ERRNO("Failed to create symlink "
1030 "\"%"TS"\" => \"%"TS"\"",
1031 newpath, symlink_target);
1036 /* Create the "skeleton" of an extracted file or directory. Don't yet extract
1037 * data streams, reparse data (including symbolic links), timestamps, and
1038 * security descriptors. Basically, everything that doesn't require reading
1039 * non-metadata resources from the WIM file and isn't delayed until the final
1042 do_dentry_extract_skeleton(tchar path[], struct wim_dentry *dentry,
1043 struct apply_ctx *ctx)
1045 struct wim_inode *inode = dentry->d_inode;
1047 const tchar *oldpath;
1049 if (unlikely(is_linked_extraction(ctx))) {
1050 struct wim_lookup_table_entry *unnamed_lte;
1052 unnamed_lte = inode_unnamed_lte_resolved(dentry->d_inode);
1053 if (unnamed_lte && unnamed_lte->extracted_file) {
1054 oldpath = unnamed_lte->extracted_file;
1055 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_HARDLINK)
1062 /* Create hard link if this dentry corresponds to an already-extracted
1064 if (inode->i_extracted_file) {
1065 oldpath = inode->i_extracted_file;
1069 /* Skip symlinks unless they can be extracted as reparse points rather
1070 * than created directly. */
1071 if (inode_is_symlink(inode) && !ctx->supported_features.reparse_points)
1074 /* Create this file or directory unless it's the extraction root, which
1075 * was already created if necessary. */
1076 if (dentry != ctx->extract_root) {
1077 ret = extract_inode(path, ctx, inode);
1082 /* Create empty named data streams. */
1083 if (can_extract_named_data_streams(ctx)) {
1084 for (u16 i = 0; i < inode->i_num_ads; i++) {
1085 file_spec_t file_spec;
1086 struct wim_ads_entry *entry = &inode->i_ads_entries[i];
1088 if (!ads_entry_is_named_stream(entry))
1092 if (ctx->ops->uses_cookies)
1093 file_spec.cookie = inode->extract_cookie;
1095 file_spec.path = path;
1096 ret = ctx->ops->extract_named_stream(file_spec,
1098 entry->stream_name_nbytes / 2,
1101 ERROR_WITH_ERRNO("\"%"TS"\": failed to create "
1102 "empty named data stream",
1109 /* Set file attributes (if supported). */
1110 ret = extract_file_attributes(path, ctx, dentry, 0);
1114 /* Set or remove file short name (if supported). */
1115 ret = extract_short_name(path, ctx, dentry);
1119 /* If inode has multiple links and hard links are supported in this
1120 * extraction mode and volume, save the path to the extracted file in
1121 * case it's needed to create a hard link. */
1122 if (unlikely(is_linked_extraction(ctx))) {
1123 struct wim_lookup_table_entry *unnamed_lte;
1125 unnamed_lte = inode_unnamed_lte_resolved(dentry->d_inode);
1127 unnamed_lte->extracted_file = TSTRDUP(path);
1128 if (!unnamed_lte->extracted_file)
1129 return WIMLIB_ERR_NOMEM;
1131 } else if (inode->i_nlink > 1 && ctx->supported_features.hard_links) {
1132 inode->i_extracted_file = TSTRDUP(path);
1133 if (!inode->i_extracted_file)
1134 return WIMLIB_ERR_NOMEM;
1139 ret = extract_multiimage_symlink(oldpath, path, ctx, dentry);
1142 dentry->was_hardlinked = 1;
1146 ret = extract_hardlink(oldpath, path, ctx);
1149 dentry->was_hardlinked = 1;
1153 /* This is a wrapper around do_dentry_extract_skeleton() that handles building
1154 * the path, doing short name reordering. This is also idempotent; dentries
1155 * already processed have skeleton_extracted set and no action is taken. See
1156 * apply_operations.requires_short_name_reordering for more details about short
1157 * name reordering. */
1159 dentry_extract_skeleton(struct wim_dentry *dentry, void *_ctx)
1161 struct apply_ctx *ctx = _ctx;
1162 tchar path[ctx->ops->path_max];
1163 struct wim_dentry *orig_dentry;
1164 struct wim_dentry *other_dentry;
1167 if (dentry->skeleton_extracted)
1171 if (ctx->supported_features.short_names
1172 && ctx->ops->requires_short_name_reordering
1173 && !dentry_has_short_name(dentry)
1174 && !dentry->d_inode->i_dos_name_extracted)
1176 inode_for_each_dentry(other_dentry, dentry->d_inode) {
1177 if (dentry_has_short_name(other_dentry)
1178 && !other_dentry->skeleton_extracted
1179 && other_dentry->in_extraction_tree
1180 && !other_dentry->extraction_skipped)
1182 DEBUG("Creating %"TS" before %"TS" "
1183 "to guarantee correct DOS name extraction",
1184 dentry_full_path(other_dentry),
1185 dentry_full_path(dentry));
1186 orig_dentry = dentry;
1187 dentry = other_dentry;
1193 if (!build_extraction_path(path, dentry, ctx))
1195 ret = do_dentry_extract_skeleton(path, dentry, ctx);
1199 dentry->skeleton_extracted = 1;
1202 dentry = orig_dentry;
1206 dentry->d_inode->i_dos_name_extracted = 1;
1211 dentry_extract_dir_skeleton(struct wim_dentry *dentry, void *_ctx)
1213 if (dentry->d_inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)
1214 return dentry_extract_skeleton(dentry, _ctx);
1218 /* Create a file or directory, then immediately extract all streams. The WIM
1219 * may not be read sequentially by this function. */
1221 dentry_extract(struct wim_dentry *dentry, void *_ctx)
1223 struct apply_ctx *ctx = _ctx;
1224 tchar path[ctx->ops->path_max];
1227 ret = dentry_extract_skeleton(dentry, ctx);
1231 if (!build_extraction_path(path, dentry, ctx))
1234 return extract_streams(path, ctx, dentry, NULL, NULL);
1237 /* Creates a temporary file opened for writing. The open file descriptor is
1238 * returned in @fd_ret and its name is returned in @name_ret (dynamically
1241 create_temporary_file(struct filedes *fd_ret, tchar **name_ret)
1247 name = ttempnam(NULL, T("wimlib"));
1249 ERROR_WITH_ERRNO("Failed to create temporary filename");
1250 return WIMLIB_ERR_NOMEM;
1253 raw_fd = topen(name, O_WRONLY | O_CREAT | O_EXCL | O_BINARY, 0600);
1256 if (errno == EEXIST) {
1260 ERROR_WITH_ERRNO("Failed to open temporary file \"%"TS"\"", name);
1262 return WIMLIB_ERR_OPEN;
1265 filedes_init(fd_ret, raw_fd);
1270 /* Extract all instances of the stream @lte that are being extracted in this
1271 * call of extract_tree(), but actually read the stream data from @lte_override.
1274 extract_stream_instances(struct wim_lookup_table_entry *lte,
1275 struct wim_lookup_table_entry *lte_override,
1276 struct apply_ctx *ctx)
1278 struct wim_dentry **lte_dentries;
1279 tchar path[ctx->ops->path_max];
1283 if (lte->out_refcnt <= ARRAY_LEN(lte->inline_lte_dentries))
1284 lte_dentries = lte->inline_lte_dentries;
1286 lte_dentries = lte->lte_dentries;
1288 for (i = 0; i < lte->out_refcnt; i++) {
1289 struct wim_dentry *dentry = lte_dentries[i];
1291 if (dentry->tmp_flag)
1293 if (!build_extraction_path(path, dentry, ctx))
1295 ret = extract_streams(path, ctx, dentry, lte, lte_override);
1297 goto out_clear_tmp_flags;
1298 dentry->tmp_flag = 1;
1301 out_clear_tmp_flags:
1302 for (i = 0; i < lte->out_refcnt; i++)
1303 lte_dentries[i]->tmp_flag = 0;
1307 /* Determine whether the specified stream needs to be extracted to a temporary
1310 * @lte->out_refcnt specifies the number of instances of this stream that must
1313 * @is_partial_res is %true if this stream is just one of multiple in a single
1314 * WIM resource being extracted. */
1316 need_tmpfile_to_extract(struct wim_lookup_table_entry *lte,
1317 bool is_partial_res)
1319 /* Temporary file is always required when reading a partial resource,
1320 * since in that case we retrieve all the contained streams in one pass.
1325 /* Otherwise we don't need a temporary file if only a single instance of
1326 * the stream is needed. */
1327 if (lte->out_refcnt == 1)
1330 wimlib_assert(lte->out_refcnt >= 2);
1332 /* We also don't need a temporary file if random access to the stream is
1334 if (lte->resource_location != RESOURCE_IN_WIM ||
1335 filedes_is_seekable(<e->rspec->wim->in_fd))
1342 begin_extract_stream_to_tmpfile(struct wim_lookup_table_entry *lte,
1343 bool is_partial_res,
1346 struct apply_ctx *ctx = _ctx;
1349 if (!need_tmpfile_to_extract(lte, is_partial_res)) {
1350 DEBUG("Temporary file not needed "
1351 "for stream (size=%"PRIu64")", lte->size);
1352 ret = extract_stream_instances(lte, lte, ctx);
1356 return BEGIN_STREAM_STATUS_SKIP_STREAM;
1359 DEBUG("Temporary file needed for stream (size=%"PRIu64")", lte->size);
1360 return create_temporary_file(&ctx->tmpfile_fd, &ctx->tmpfile_name);
1364 end_extract_stream_to_tmpfile(struct wim_lookup_table_entry *lte,
1365 int status, void *_ctx)
1367 struct apply_ctx *ctx = _ctx;
1368 struct wim_lookup_table_entry lte_override;
1370 int errno_save = errno;
1372 ret = filedes_close(&ctx->tmpfile_fd);
1377 goto out_delete_tmpfile;
1381 ERROR_WITH_ERRNO("Error writing temporary file %"TS, ctx->tmpfile_name);
1382 ret = WIMLIB_ERR_WRITE;
1383 goto out_delete_tmpfile;
1386 /* Now that a full stream has been extracted to a temporary file,
1387 * extract all instances of it to the actual target. */
1389 memcpy(<e_override, lte, sizeof(struct wim_lookup_table_entry));
1390 lte_override.resource_location = RESOURCE_IN_FILE_ON_DISK;
1391 lte_override.file_on_disk = ctx->tmpfile_name;
1393 ret = extract_stream_instances(lte, <e_override, ctx);
1397 tunlink(ctx->tmpfile_name);
1398 FREE(ctx->tmpfile_name);
1403 /* Extracts a list of streams (ctx.stream_list), assuming that the directory
1404 * structure and empty files were already created. This relies on the
1405 * per-`struct wim_lookup_table_entry' list of dentries that reference each
1406 * stream that was constructed earlier. */
1408 extract_stream_list(struct apply_ctx *ctx)
1410 if (!(ctx->extract_flags & WIMLIB_EXTRACT_FLAG_FILE_ORDER)) {
1411 /* Sequential extraction: read the streams in the order in which
1412 * they appear in the WIM file. */
1413 struct read_stream_list_callbacks cbs = {
1414 .begin_stream = begin_extract_stream_to_tmpfile,
1415 .begin_stream_ctx = ctx,
1416 .consume_chunk = extract_chunk_to_fd,
1417 .consume_chunk_ctx = &ctx->tmpfile_fd,
1418 .end_stream = end_extract_stream_to_tmpfile,
1419 .end_stream_ctx = ctx,
1421 return read_stream_list(&ctx->stream_list,
1422 offsetof(struct wim_lookup_table_entry, extraction_list),
1423 &cbs, VERIFY_STREAM_HASHES);
1425 /* Extract the streams in unsorted order. */
1426 struct wim_lookup_table_entry *lte;
1429 list_for_each_entry(lte, &ctx->stream_list, extraction_list) {
1430 ret = extract_stream_instances(lte, lte, ctx);
1438 #define PWM_ALLOW_WIM_HDR 0x00001
1439 #define PWM_SILENT_EOF 0x00002
1441 /* Read the header from a stream in a pipable WIM. */
1443 read_pwm_stream_header(WIMStruct *pwm, struct wim_lookup_table_entry *lte,
1444 struct wim_resource_spec *rspec,
1445 int flags, struct wim_header_disk *hdr_ret)
1448 struct pwm_stream_hdr stream_hdr;
1449 struct wim_header_disk pwm_hdr;
1451 struct wim_reshdr reshdr;
1454 ret = full_read(&pwm->in_fd, &buf.stream_hdr, sizeof(buf.stream_hdr));
1458 if ((flags & PWM_ALLOW_WIM_HDR) && buf.stream_hdr.magic == PWM_MAGIC) {
1459 BUILD_BUG_ON(sizeof(buf.pwm_hdr) < sizeof(buf.stream_hdr));
1460 ret = full_read(&pwm->in_fd, &buf.stream_hdr + 1,
1461 sizeof(buf.pwm_hdr) - sizeof(buf.stream_hdr));
1465 lte->resource_location = RESOURCE_NONEXISTENT;
1466 memcpy(hdr_ret, &buf.pwm_hdr, sizeof(buf.pwm_hdr));
1470 if (le64_to_cpu(buf.stream_hdr.magic) != PWM_STREAM_MAGIC) {
1471 ERROR("Data read on pipe is invalid (expected stream header).");
1472 return WIMLIB_ERR_INVALID_PIPABLE_WIM;
1475 copy_hash(lte->hash, buf.stream_hdr.hash);
1477 reshdr.size_in_wim = 0;
1478 reshdr.flags = le32_to_cpu(buf.stream_hdr.flags);
1479 reshdr.offset_in_wim = pwm->in_fd.offset;
1480 reshdr.uncompressed_size = le64_to_cpu(buf.stream_hdr.uncompressed_size);
1481 wim_res_hdr_to_spec(&reshdr, pwm, rspec);
1482 lte_bind_wim_resource_spec(lte, rspec);
1483 lte->flags = rspec->flags;
1484 lte->size = rspec->uncompressed_size;
1485 lte->offset_in_res = 0;
1489 if (ret != WIMLIB_ERR_UNEXPECTED_END_OF_FILE || !(flags & PWM_SILENT_EOF))
1490 ERROR_WITH_ERRNO("Error reading pipable WIM from pipe");
1495 extract_streams_from_pipe(struct apply_ctx *ctx)
1497 struct wim_lookup_table_entry *found_lte;
1498 struct wim_resource_spec *rspec;
1499 struct wim_lookup_table_entry *needed_lte;
1500 struct wim_lookup_table *lookup_table;
1501 struct wim_header_disk pwm_hdr;
1505 ret = WIMLIB_ERR_NOMEM;
1506 found_lte = new_lookup_table_entry();
1507 if (found_lte == NULL)
1510 rspec = MALLOC(sizeof(struct wim_resource_spec));
1512 goto out_free_found_lte;
1514 lookup_table = ctx->wim->lookup_table;
1515 pwm_flags = PWM_ALLOW_WIM_HDR;
1516 if ((ctx->extract_flags & WIMLIB_EXTRACT_FLAG_RESUME))
1517 pwm_flags |= PWM_SILENT_EOF;
1518 memcpy(ctx->progress.extract.guid, ctx->wim->hdr.guid, WIM_GID_LEN);
1519 ctx->progress.extract.part_number = ctx->wim->hdr.part_number;
1520 ctx->progress.extract.total_parts = ctx->wim->hdr.total_parts;
1521 if (ctx->progress_func)
1522 ctx->progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN,
1524 while (ctx->num_streams_remaining) {
1525 if (found_lte->resource_location != RESOURCE_NONEXISTENT)
1526 lte_unbind_wim_resource_spec(found_lte);
1527 ret = read_pwm_stream_header(ctx->wim, found_lte, rspec,
1528 pwm_flags, &pwm_hdr);
1530 if (ret == WIMLIB_ERR_UNEXPECTED_END_OF_FILE &&
1531 (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_RESUME))
1535 goto out_free_found_lte;
1538 if ((found_lte->resource_location != RESOURCE_NONEXISTENT)
1539 && !(found_lte->flags & WIM_RESHDR_FLAG_METADATA)
1540 && (needed_lte = lookup_stream(lookup_table, found_lte->hash))
1541 && (needed_lte->out_refcnt))
1543 tchar *tmpfile_name = NULL;
1544 struct wim_lookup_table_entry *lte_override;
1545 struct wim_lookup_table_entry tmpfile_lte;
1547 needed_lte->offset_in_res = found_lte->offset_in_res;
1548 needed_lte->flags = found_lte->flags;
1549 needed_lte->size = found_lte->size;
1551 lte_unbind_wim_resource_spec(found_lte);
1552 lte_bind_wim_resource_spec(needed_lte, rspec);
1554 if (needed_lte->out_refcnt > 1) {
1556 struct filedes tmpfile_fd;
1558 /* Extract stream to temporary file. */
1559 ret = create_temporary_file(&tmpfile_fd, &tmpfile_name);
1561 lte_unbind_wim_resource_spec(needed_lte);
1562 goto out_free_found_lte;
1565 ret = extract_full_stream_to_fd(needed_lte,
1568 filedes_close(&tmpfile_fd);
1569 goto delete_tmpfile;
1572 if (filedes_close(&tmpfile_fd)) {
1573 ERROR_WITH_ERRNO("Error writing to temporary "
1574 "file \"%"TS"\"", tmpfile_name);
1575 ret = WIMLIB_ERR_WRITE;
1576 goto delete_tmpfile;
1578 memcpy(&tmpfile_lte, needed_lte,
1579 sizeof(struct wim_lookup_table_entry));
1580 tmpfile_lte.resource_location = RESOURCE_IN_FILE_ON_DISK;
1581 tmpfile_lte.file_on_disk = tmpfile_name;
1582 lte_override = &tmpfile_lte;
1584 lte_override = needed_lte;
1587 ret = extract_stream_instances(needed_lte, lte_override, ctx);
1589 lte_unbind_wim_resource_spec(needed_lte);
1591 tunlink(tmpfile_name);
1595 goto out_free_found_lte;
1596 ctx->num_streams_remaining--;
1597 } else if (found_lte->resource_location != RESOURCE_NONEXISTENT) {
1598 ret = skip_wim_stream(found_lte);
1600 goto out_free_found_lte;
1602 u16 part_number = le16_to_cpu(pwm_hdr.part_number);
1603 u16 total_parts = le16_to_cpu(pwm_hdr.total_parts);
1605 if (part_number != ctx->progress.extract.part_number ||
1606 total_parts != ctx->progress.extract.total_parts ||
1607 memcmp(pwm_hdr.guid, ctx->progress.extract.guid,
1610 ctx->progress.extract.part_number = part_number;
1611 ctx->progress.extract.total_parts = total_parts;
1612 memcpy(ctx->progress.extract.guid,
1613 pwm_hdr.guid, WIM_GID_LEN);
1614 if (ctx->progress_func) {
1616 WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN,
1625 if (found_lte->resource_location != RESOURCE_IN_WIM)
1627 free_lookup_table_entry(found_lte);
1636 /* Finish extracting a file, directory, or symbolic link by setting file
1637 * security and timestamps. */
1639 dentry_extract_final(struct wim_dentry *dentry, void *_ctx)
1641 struct apply_ctx *ctx = _ctx;
1643 tchar path[ctx->ops->path_max];
1645 if (!build_extraction_path(path, dentry, ctx))
1648 ret = extract_security(path, ctx, dentry);
1652 if (ctx->ops->requires_final_set_attributes_pass) {
1653 /* Set file attributes (if supported). */
1654 ret = extract_file_attributes(path, ctx, dentry, 1);
1659 return extract_timestamps(path, ctx, dentry);
1663 * Extract a WIM dentry to standard output.
1665 * This obviously doesn't make sense in all cases. We return an error if the
1666 * dentry does not correspond to a regular file. Otherwise we extract the
1667 * unnamed data stream only.
1670 extract_dentry_to_stdout(struct wim_dentry *dentry)
1673 if (dentry->d_inode->i_attributes & (FILE_ATTRIBUTE_REPARSE_POINT |
1674 FILE_ATTRIBUTE_DIRECTORY))
1676 ERROR("\"%"TS"\" is not a regular file and therefore cannot be "
1677 "extracted to standard output", dentry_full_path(dentry));
1678 ret = WIMLIB_ERR_NOT_A_REGULAR_FILE;
1680 struct wim_lookup_table_entry *lte;
1682 lte = inode_unnamed_lte_resolved(dentry->d_inode);
1684 struct filedes _stdout;
1685 filedes_init(&_stdout, STDOUT_FILENO);
1686 ret = extract_full_stream_to_fd(lte, &_stdout);
1693 static const utf16lechar replacement_char = cpu_to_le16(0xfffd);
1695 static const utf16lechar replacement_char = cpu_to_le16('?');
1699 file_name_valid(utf16lechar *name, size_t num_chars, bool fix)
1705 for (i = 0; i < num_chars; i++) {
1708 case cpu_to_le16('\\'):
1709 case cpu_to_le16(':'):
1710 case cpu_to_le16('*'):
1711 case cpu_to_le16('?'):
1712 case cpu_to_le16('"'):
1713 case cpu_to_le16('<'):
1714 case cpu_to_le16('>'):
1715 case cpu_to_le16('|'):
1717 case cpu_to_le16('/'):
1718 case cpu_to_le16('\0'):
1720 name[i] = replacement_char;
1727 if (name[num_chars - 1] == cpu_to_le16(' ') ||
1728 name[num_chars - 1] == cpu_to_le16('.'))
1731 name[num_chars - 1] = replacement_char;
1740 dentry_mark_skipped(struct wim_dentry *dentry, void *_ignore)
1742 dentry->extraction_skipped = 1;
1747 * dentry_calculate_extraction_path-
1749 * Calculate the actual filename component at which a WIM dentry will be
1750 * extracted, handling invalid filenames "properly".
1752 * dentry->extraction_name usually will be set the same as dentry->file_name (on
1753 * UNIX, converted into the platform's multibyte encoding). However, if the
1754 * file name contains characters that are not valid on the current platform or
1755 * has some other format that is not valid, leave dentry->extraction_name as
1756 * NULL and set dentry->extraction_skipped to indicate that this dentry should
1757 * not be extracted, unless the appropriate flag
1758 * WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES is set in the extract flags, in
1759 * which case a substitute filename will be created and set instead.
1761 * Conflicts with case-insensitive names on Windows are handled similarly; see
1765 dentry_calculate_extraction_path(struct wim_dentry *dentry, void *_args)
1767 struct apply_ctx *ctx = _args;
1770 if (dentry == ctx->extract_root || dentry->extraction_skipped)
1773 if (!dentry_is_supported(dentry, &ctx->supported_features))
1776 if (!ctx->ops->supports_case_sensitive_filenames)
1778 struct wim_dentry *other;
1779 list_for_each_entry(other, &dentry->case_insensitive_conflict_list,
1780 case_insensitive_conflict_list)
1782 if (!other->extraction_skipped) {
1783 if (ctx->extract_flags &
1784 WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS) {
1785 WARNING("\"%"TS"\" has the same "
1786 "case-insensitive name as "
1787 "\"%"TS"\"; extracting "
1788 "dummy name instead",
1789 dentry_full_path(dentry),
1790 dentry_full_path(other));
1793 WARNING("Not extracting \"%"TS"\": "
1794 "has same case-insensitive "
1795 "name as \"%"TS"\"",
1796 dentry_full_path(dentry),
1797 dentry_full_path(other));
1804 if (file_name_valid(dentry->file_name, dentry->file_name_nbytes / 2, false)) {
1805 #if TCHAR_IS_UTF16LE
1806 dentry->extraction_name = dentry->file_name;
1807 dentry->extraction_name_nchars = dentry->file_name_nbytes / 2;
1810 return utf16le_to_tstr(dentry->file_name,
1811 dentry->file_name_nbytes,
1812 &dentry->extraction_name,
1813 &dentry->extraction_name_nchars);
1816 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES)
1818 WARNING("\"%"TS"\" has an invalid filename "
1819 "that is not supported on this platform; "
1820 "extracting dummy name instead",
1821 dentry_full_path(dentry));
1824 WARNING("Not extracting \"%"TS"\": has an invalid filename "
1825 "that is not supported on this platform",
1826 dentry_full_path(dentry));
1833 utf16lechar utf16_name_copy[dentry->file_name_nbytes / 2];
1835 memcpy(utf16_name_copy, dentry->file_name, dentry->file_name_nbytes);
1836 file_name_valid(utf16_name_copy, dentry->file_name_nbytes / 2, true);
1839 size_t tchar_nchars;
1840 #if TCHAR_IS_UTF16LE
1841 tchar_name = utf16_name_copy;
1842 tchar_nchars = dentry->file_name_nbytes / 2;
1844 ret = utf16le_to_tstr(utf16_name_copy,
1845 dentry->file_name_nbytes,
1846 &tchar_name, &tchar_nchars);
1850 size_t fixed_name_num_chars = tchar_nchars;
1851 tchar fixed_name[tchar_nchars + 50];
1853 tmemcpy(fixed_name, tchar_name, tchar_nchars);
1854 fixed_name_num_chars += tsprintf(fixed_name + tchar_nchars,
1855 T(" (invalid filename #%lu)"),
1856 ++ctx->invalid_sequence);
1857 #if !TCHAR_IS_UTF16LE
1860 dentry->extraction_name = memdup(fixed_name,
1861 2 * fixed_name_num_chars + 2);
1862 if (!dentry->extraction_name)
1863 return WIMLIB_ERR_NOMEM;
1864 dentry->extraction_name_nchars = fixed_name_num_chars;
1869 for_dentry_in_tree(dentry, dentry_mark_skipped, NULL);
1873 /* Clean up dentry and inode structure after extraction. */
1875 dentry_reset_needs_extraction(struct wim_dentry *dentry, void *_ignore)
1877 struct wim_inode *inode = dentry->d_inode;
1879 dentry->in_extraction_tree = 0;
1880 dentry->extraction_skipped = 0;
1881 dentry->was_hardlinked = 0;
1882 dentry->skeleton_extracted = 0;
1883 inode->i_visited = 0;
1884 FREE(inode->i_extracted_file);
1885 inode->i_extracted_file = NULL;
1886 inode->i_dos_name_extracted = 0;
1887 if ((void*)dentry->extraction_name != (void*)dentry->file_name)
1888 FREE(dentry->extraction_name);
1889 dentry->extraction_name = NULL;
1893 /* Tally features necessary to extract a dentry and the corresponding inode. */
1895 dentry_tally_features(struct wim_dentry *dentry, void *_features)
1897 struct wim_features *features = _features;
1898 struct wim_inode *inode = dentry->d_inode;
1900 if (dentry->extraction_skipped)
1903 if (inode->i_attributes & FILE_ATTRIBUTE_ARCHIVE)
1904 features->archive_files++;
1905 if (inode->i_attributes & FILE_ATTRIBUTE_HIDDEN)
1906 features->hidden_files++;
1907 if (inode->i_attributes & FILE_ATTRIBUTE_SYSTEM)
1908 features->system_files++;
1909 if (inode->i_attributes & FILE_ATTRIBUTE_COMPRESSED)
1910 features->compressed_files++;
1911 if (inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED) {
1912 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)
1913 features->encrypted_directories++;
1915 features->encrypted_files++;
1917 if (inode->i_attributes & FILE_ATTRIBUTE_NOT_CONTENT_INDEXED)
1918 features->not_context_indexed_files++;
1919 if (inode->i_attributes & FILE_ATTRIBUTE_SPARSE_FILE)
1920 features->sparse_files++;
1921 if (inode_has_named_stream(inode))
1922 features->named_data_streams++;
1923 if (inode->i_visited)
1924 features->hard_links++;
1925 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1926 features->reparse_points++;
1927 if (inode_is_symlink(inode))
1928 features->symlink_reparse_points++;
1930 features->other_reparse_points++;
1932 if (inode->i_security_id != -1)
1933 features->security_descriptors++;
1934 if (dentry->short_name_nbytes)
1935 features->short_names++;
1936 if (inode_has_unix_data(inode))
1937 features->unix_data++;
1938 inode->i_visited = 1;
1942 /* Tally the features necessary to extract a dentry tree. */
1944 dentry_tree_get_features(struct wim_dentry *root, struct wim_features *features)
1946 memset(features, 0, sizeof(struct wim_features));
1947 for_dentry_in_tree(root, dentry_tally_features, features);
1948 dentry_tree_clear_inode_visited(root);
1952 compute_supported_attributes_mask(const struct wim_features *supported_features)
1956 if (!supported_features->archive_files)
1957 mask &= ~FILE_ATTRIBUTE_ARCHIVE;
1959 if (!supported_features->hidden_files)
1960 mask &= ~FILE_ATTRIBUTE_HIDDEN;
1962 if (!supported_features->system_files)
1963 mask &= ~FILE_ATTRIBUTE_SYSTEM;
1965 if (!supported_features->not_context_indexed_files)
1966 mask &= ~FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
1968 if (!supported_features->compressed_files)
1969 mask &= ~FILE_ATTRIBUTE_COMPRESSED;
1971 if (!supported_features->sparse_files)
1972 mask &= ~FILE_ATTRIBUTE_SPARSE_FILE;
1974 if (!supported_features->reparse_points)
1975 mask &= ~FILE_ATTRIBUTE_REPARSE_POINT;
1981 do_feature_check(const struct wim_features *required_features,
1982 const struct wim_features *supported_features,
1984 const struct apply_operations *ops,
1987 const tchar *loc = T("the extraction operation");;
1988 const tchar *mode = T("this extraction mode");
1991 /* We're an archive program, so theoretically we can do what we want
1992 * with FILE_ATTRIBUTE_ARCHIVE (which is a dumb flag anyway). Don't
1993 * bother the user about it. */
1995 if (required_features->archive_files && !supported_features->archive_files)
1998 "%lu files in %"TS" are marked as archived, but this attribute\n"
1999 " is not supported in %"TS".",
2000 required_features->archive_files, loc, mode);
2004 if (required_features->hidden_files && !supported_features->hidden_files)
2007 "%lu files in %"TS" are marked as hidden, but this\n"
2008 " attribute is not supported in %"TS".",
2009 required_features->hidden_files, loc, mode);
2012 if (required_features->system_files && !supported_features->system_files)
2015 "%lu files in %"TS" are marked as system files,\n"
2016 " but this attribute is not supported in %"TS".",
2017 required_features->system_files, loc, mode);
2020 if (required_features->compressed_files && !supported_features->compressed_files)
2023 "%lu files in %"TS" are marked as being transparently\n"
2024 " compressed, but transparent compression is not supported in\n"
2025 " %"TS". These files will be extracted as uncompressed.",
2026 required_features->compressed_files, loc, mode);
2029 if (required_features->encrypted_files && !supported_features->encrypted_files)
2032 "%lu files in %"TS" are marked as being encrypted,\n"
2033 " but encryption is not supported in %"TS". These files\n"
2034 " will not be extracted.",
2035 required_features->encrypted_files, loc, mode);
2038 if (required_features->encrypted_directories &&
2039 !supported_features->encrypted_directories)
2042 "%lu directories in %"TS" are marked as being encrypted,\n"
2043 " but encryption is not supported in %"TS".\n"
2044 " These directories will be extracted as unencrypted.",
2045 required_features->encrypted_directories, loc, mode);
2048 if (required_features->not_context_indexed_files &&
2049 !supported_features->not_context_indexed_files)
2052 "%lu files in %"TS" are marked as not content indexed,\n"
2053 " but this attribute is not supported in %"TS".",
2054 required_features->not_context_indexed_files, loc, mode);
2057 if (required_features->sparse_files && !supported_features->sparse_files)
2060 "%lu files in %"TS" are marked as sparse, but creating\n"
2061 " sparse files is not supported in %"TS". These files\n"
2062 " will be extracted as non-sparse.",
2063 required_features->sparse_files, loc, mode);
2066 if (required_features->named_data_streams &&
2067 !supported_features->named_data_streams)
2070 "%lu files in %"TS" contain one or more alternate (named)\n"
2071 " data streams, which are not supported in %"TS".\n"
2072 " Alternate data streams will NOT be extracted.",
2073 required_features->named_data_streams, loc, mode);
2076 if (unlikely(extract_flags & (WIMLIB_EXTRACT_FLAG_HARDLINK |
2077 WIMLIB_EXTRACT_FLAG_SYMLINK)) &&
2078 required_features->named_data_streams &&
2079 supported_features->named_data_streams)
2082 "%lu files in %"TS" contain one or more alternate (named)\n"
2083 " data streams, which are not supported in linked extraction mode.\n"
2084 " Alternate data streams will NOT be extracted.",
2085 required_features->named_data_streams, loc);
2088 if (required_features->hard_links && !supported_features->hard_links)
2091 "%lu files in %"TS" are hard links, but hard links are\n"
2092 " not supported in %"TS". Hard links will be extracted as\n"
2093 " duplicate copies of the linked files.",
2094 required_features->hard_links, loc, mode);
2097 if (required_features->reparse_points && !supported_features->reparse_points)
2099 if (supported_features->symlink_reparse_points) {
2100 if (required_features->other_reparse_points) {
2102 "%lu files in %"TS" are reparse points that are neither\n"
2103 " symbolic links nor junction points and are not supported in\n"
2104 " %"TS". These reparse points will not be extracted.",
2105 required_features->other_reparse_points, loc,
2110 "%lu files in %"TS" are reparse points, which are\n"
2111 " not supported in %"TS" and will not be extracted.",
2112 required_features->reparse_points, loc, mode);
2116 if (required_features->security_descriptors &&
2117 !supported_features->security_descriptors)
2120 "%lu files in %"TS" have Windows NT security descriptors,\n"
2121 " but extracting security descriptors is not supported in\n"
2122 " %"TS". No security descriptors will be extracted.",
2123 required_features->security_descriptors, loc, mode);
2126 if (required_features->short_names && !supported_features->short_names)
2129 "%lu files in %"TS" have short (DOS) names, but\n"
2130 " extracting short names is not supported in %"TS".\n"
2131 " Short names will not be extracted.\n",
2132 required_features->short_names, loc, mode);
2136 if ((extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) &&
2137 required_features->unix_data && !supported_features->unix_data)
2139 ERROR("Extracting UNIX data is not supported in %"TS, mode);
2140 return WIMLIB_ERR_UNSUPPORTED;
2142 if ((extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_SHORT_NAMES) &&
2143 required_features->short_names && !supported_features->short_names)
2145 ERROR("Extracting short names is not supported in %"TS"", mode);
2146 return WIMLIB_ERR_UNSUPPORTED;
2148 if ((extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_TIMESTAMPS) &&
2149 !ops->set_timestamps)
2151 ERROR("Extracting timestamps is not supported in %"TS"", mode);
2152 return WIMLIB_ERR_UNSUPPORTED;
2154 if (((extract_flags & (WIMLIB_EXTRACT_FLAG_STRICT_ACLS |
2155 WIMLIB_EXTRACT_FLAG_UNIX_DATA))
2156 == WIMLIB_EXTRACT_FLAG_STRICT_ACLS) &&
2157 required_features->security_descriptors &&
2158 !supported_features->security_descriptors)
2160 ERROR("Extracting security descriptors is not supported in %"TS, mode);
2161 return WIMLIB_ERR_UNSUPPORTED;
2164 if ((extract_flags & WIMLIB_EXTRACT_FLAG_HARDLINK) &&
2165 !supported_features->hard_links)
2167 ERROR("Hard link extraction mode requested, but "
2168 "%"TS" does not support hard links!", mode);
2169 return WIMLIB_ERR_UNSUPPORTED;
2172 if ((extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS) &&
2173 required_features->symlink_reparse_points &&
2174 !(supported_features->symlink_reparse_points ||
2175 supported_features->reparse_points))
2177 ERROR("Extracting symbolic links is not supported in %"TS, mode);
2178 return WIMLIB_ERR_UNSUPPORTED;
2181 if ((extract_flags & WIMLIB_EXTRACT_FLAG_SYMLINK) &&
2182 !supported_features->symlink_reparse_points)
2184 ERROR("Symbolic link extraction mode requested, but "
2185 "%"TS" does not support symbolic "
2187 return WIMLIB_ERR_UNSUPPORTED;
2193 do_extract_warnings(struct apply_ctx *ctx)
2195 if (ctx->partial_security_descriptors == 0 &&
2196 ctx->no_security_descriptors == 0)
2199 WARNING("Extraction to \"%"TS"\" complete, but with one or more warnings:",
2201 if (ctx->partial_security_descriptors != 0) {
2202 WARNING("- Could only partially set the security descriptor\n"
2203 " on %lu files or directories.",
2204 ctx->partial_security_descriptors);
2206 if (ctx->no_security_descriptors != 0) {
2207 WARNING("- Could not set security descriptor at all\n"
2208 " on %lu files or directories.",
2209 ctx->no_security_descriptors);
2212 WARNING("To fully restore all security descriptors, run the program\n"
2213 " with Administrator rights.");
2218 dentry_set_skipped(struct wim_dentry *dentry, void *_ignore)
2220 dentry->in_extraction_tree = 1;
2221 dentry->extraction_skipped = 1;
2226 dentry_set_not_skipped(struct wim_dentry *dentry, void *_ignore)
2228 dentry->in_extraction_tree = 1;
2229 dentry->extraction_skipped = 0;
2234 extract_trees(WIMStruct *wim, struct wim_dentry **trees, size_t num_trees,
2235 const tchar *target, int extract_flags,
2236 wimlib_progress_func_t progress_func)
2238 struct wim_features required_features;
2239 struct apply_ctx ctx;
2241 struct wim_lookup_table_entry *lte;
2243 /* Start initializing the apply_ctx. */
2244 memset(&ctx, 0, sizeof(struct apply_ctx));
2246 ctx.extract_flags = extract_flags;
2247 ctx.target = target;
2248 ctx.target_nchars = tstrlen(target);
2249 ctx.progress_func = progress_func;
2250 if (progress_func) {
2251 ctx.progress.extract.wimfile_name = wim->filename;
2252 ctx.progress.extract.image = wim->current_image;
2253 ctx.progress.extract.extract_flags = (extract_flags &
2254 WIMLIB_EXTRACT_MASK_PUBLIC);
2255 ctx.progress.extract.image_name = wimlib_get_image_name(wim,
2256 wim->current_image);
2257 ctx.progress.extract.target = target;
2259 INIT_LIST_HEAD(&ctx.stream_list);
2261 if (extract_flags & WIMLIB_EXTRACT_FLAG_FILEMODE) {
2262 /* File mode --- target is explicit. */
2263 wimlib_assert(num_trees == 1);
2264 ret = calculate_dentry_full_path(trees[0]);
2267 ctx.progress.extract.extract_root_wim_source_path = trees[0]->_full_path;
2268 ctx.extract_root = trees[0];
2269 for_dentry_in_tree(ctx.extract_root, dentry_set_not_skipped, NULL);
2271 /* Targets are to be set relative to the root of the image
2272 * (preserving original directory structure). */
2274 ctx.progress.extract.extract_root_wim_source_path = T("");
2275 ctx.extract_root = wim_root_dentry(wim);
2276 for_dentry_in_tree(ctx.extract_root, dentry_set_skipped, NULL);
2278 for (size_t i = 0; i < num_trees; i++) {
2279 struct wim_dentry *d;
2281 for_dentry_in_tree(trees[i], dentry_set_not_skipped, NULL);
2284 /* Extract directories up to image root if preserving
2285 * directory structure. */
2286 if (!(extract_flags & WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE)) {
2287 while (d != ctx.extract_root) {
2289 dentry_set_not_skipped(d, NULL);
2295 /* Select the appropriate apply_operations based on the
2296 * platform and extract_flags. */
2298 ctx.ops = &win32_apply_ops;
2300 ctx.ops = &unix_apply_ops;
2304 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS)
2305 ctx.ops = &ntfs_3g_apply_ops;
2308 /* Call the start_extract() callback. This gives the apply_operations
2309 * implementation a chance to do any setup needed to access the volume.
2310 * Furthermore, it's expected to set the supported features of this
2311 * extraction mode (ctx.supported_features), which are determined at
2312 * runtime as they may vary depending on the actual volume. These
2313 * features are then compared with the actual features extracting this
2314 * dentry tree requires. Some mismatches will merely produce warnings
2315 * and the unsupported data will be ignored; others will produce errors.
2317 ret = ctx.ops->start_extract(target, &ctx);
2319 goto out_dentry_reset_needs_extraction;
2321 /* Get and check the features required to extract the dentry tree. */
2322 dentry_tree_get_features(ctx.extract_root, &required_features);
2323 ret = do_feature_check(&required_features, &ctx.supported_features,
2324 extract_flags, ctx.ops, true);
2326 goto out_finish_or_abort_extract;
2328 ctx.supported_attributes_mask =
2329 compute_supported_attributes_mask(&ctx.supported_features);
2331 /* Figure out whether the root dentry is being extracted to the root of
2332 * a volume and therefore needs to be treated "specially", for example
2333 * not being explicitly created and not having attributes set. */
2334 if (ctx.ops->target_is_root && ctx.ops->root_directory_is_special)
2335 ctx.root_dentry_is_special = ctx.ops->target_is_root(target);
2337 /* Calculate the actual filename component of each extracted dentry. In
2338 * the process, set the dentry->extraction_skipped flag on dentries that
2339 * are being skipped because of filename or supported features problems. */
2340 ret = for_dentry_in_tree(ctx.extract_root,
2341 dentry_calculate_extraction_path, &ctx);
2343 goto out_dentry_reset_needs_extraction;
2345 /* Build the list of the streams that need to be extracted and
2346 * initialize ctx.progress.extract with stream information. */
2347 ret = for_dentry_in_tree(ctx.extract_root,
2348 dentry_resolve_and_zero_lte_refcnt, &ctx);
2350 goto out_dentry_reset_needs_extraction;
2352 ret = for_dentry_in_tree(ctx.extract_root,
2353 dentry_add_streams_to_extract, &ctx);
2355 goto out_teardown_stream_list;
2357 if (extract_flags & WIMLIB_EXTRACT_FLAG_FROM_PIPE) {
2358 /* When extracting from a pipe, the number of bytes of data to
2359 * extract can't be determined in the normal way (examining the
2360 * lookup table), since at this point all we have is a set of
2361 * SHA1 message digests of streams that need to be extracted.
2362 * However, we can get a reasonably accurate estimate by taking
2363 * <TOTALBYTES> from the corresponding <IMAGE> in the WIM XML
2364 * data. This does assume that a full image is being extracted,
2365 * but currently there is no API for doing otherwise. (Also,
2366 * subtract <HARDLINKBYTES> from this if hard links are
2367 * supported by the extraction mode.) */
2368 ctx.progress.extract.total_bytes =
2369 wim_info_get_image_total_bytes(wim->wim_info,
2370 wim->current_image);
2371 if (ctx.supported_features.hard_links) {
2372 ctx.progress.extract.total_bytes -=
2373 wim_info_get_image_hard_link_bytes(wim->wim_info,
2374 wim->current_image);
2378 /* Handle the special case of extracting a file to standard
2379 * output. In that case, "root" should be a single file, not a
2380 * directory tree. (If not, extract_dentry_to_stdout() will
2381 * return an error.) */
2382 if (extract_flags & WIMLIB_EXTRACT_FLAG_TO_STDOUT) {
2384 for (size_t i = 0; i < num_trees; i++) {
2385 ret = extract_dentry_to_stdout(trees[i]);
2389 goto out_teardown_stream_list;
2392 if (ctx.ops->realpath_works_on_nonexisting_files &&
2393 ((extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX) ||
2394 ctx.ops->requires_realtarget_in_paths))
2396 ctx.realtarget = realpath(target, NULL);
2397 if (!ctx.realtarget) {
2398 ret = WIMLIB_ERR_NOMEM;
2399 goto out_teardown_stream_list;
2401 ctx.realtarget_nchars = tstrlen(ctx.realtarget);
2404 if (progress_func) {
2406 if (extract_flags & WIMLIB_EXTRACT_FLAG_IMAGEMODE)
2407 msg = WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN;
2409 msg = WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN;
2410 progress_func(msg, &ctx.progress);
2413 if (!ctx.root_dentry_is_special)
2415 tchar path[ctx.ops->path_max];
2416 if (build_extraction_path(path, ctx.extract_root, &ctx))
2418 ret = extract_inode(path, &ctx, ctx.extract_root->d_inode);
2420 goto out_free_realtarget;
2424 /* If we need to fix up the targets of absolute symbolic links
2425 * (WIMLIB_EXTRACT_FLAG_RPFIX) or the extraction mode requires paths to
2426 * be absolute, use realpath() (or its replacement on Windows) to get
2427 * the absolute path to the extraction target. Note that this requires
2428 * the target directory to exist, unless
2429 * realpath_works_on_nonexisting_files is set in the apply_operations.
2431 if (!ctx.realtarget &&
2432 (((extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX) &&
2433 required_features.symlink_reparse_points) ||
2434 ctx.ops->requires_realtarget_in_paths))
2436 ctx.realtarget = realpath(target, NULL);
2437 if (!ctx.realtarget) {
2438 ret = WIMLIB_ERR_NOMEM;
2439 goto out_free_realtarget;
2441 ctx.realtarget_nchars = tstrlen(ctx.realtarget);
2444 if (ctx.ops->requires_short_name_reordering) {
2445 ret = for_dentry_in_tree(ctx.extract_root, dentry_extract_dir_skeleton,
2448 goto out_free_realtarget;
2451 /* Finally, the important part: extract the tree of files. */
2452 if (!(extract_flags & WIMLIB_EXTRACT_FLAG_FILE_ORDER)) {
2453 /* Sequential extraction requested, so two passes are needed
2454 * (one for directory structure, one for streams.) */
2456 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN,
2459 if (!(extract_flags & WIMLIB_EXTRACT_FLAG_RESUME)) {
2460 ret = for_dentry_in_tree(ctx.extract_root, dentry_extract_skeleton, &ctx);
2462 goto out_free_realtarget;
2465 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_END,
2467 if (extract_flags & WIMLIB_EXTRACT_FLAG_FROM_PIPE)
2468 ret = extract_streams_from_pipe(&ctx);
2470 ret = extract_stream_list(&ctx);
2472 goto out_free_realtarget;
2474 /* Sequential extraction was not requested, so we can make do
2475 * with one pass where we both create the files and extract
2478 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN,
2480 ret = for_dentry_in_tree(ctx.extract_root, dentry_extract, &ctx);
2482 goto out_free_realtarget;
2484 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_END,
2488 /* If the total number of bytes to extract was miscalculated, just jump
2489 * to the calculated number in order to avoid confusing the progress
2490 * function. This should only occur when extracting from a pipe. */
2491 if (ctx.progress.extract.completed_bytes != ctx.progress.extract.total_bytes)
2493 DEBUG("Calculated %"PRIu64" bytes to extract, but actually "
2494 "extracted %"PRIu64,
2495 ctx.progress.extract.total_bytes,
2496 ctx.progress.extract.completed_bytes);
2498 if (progress_func &&
2499 ctx.progress.extract.completed_bytes < ctx.progress.extract.total_bytes)
2501 ctx.progress.extract.completed_bytes = ctx.progress.extract.total_bytes;
2502 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS, &ctx.progress);
2505 /* Apply security descriptors and timestamps. This is done at the end,
2506 * and in a depth-first manner, to prevent timestamps from getting
2507 * changed by subsequent extract operations and to minimize the chance
2508 * of the restored security descriptors getting in our way. */
2510 progress_func(WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS,
2512 ret = for_dentry_in_tree_depth(ctx.extract_root, dentry_extract_final, &ctx);
2514 goto out_free_realtarget;
2516 if (progress_func) {
2518 if (extract_flags & WIMLIB_EXTRACT_FLAG_IMAGEMODE)
2519 msg = WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END;
2521 msg = WIMLIB_PROGRESS_MSG_EXTRACT_TREE_END;
2522 progress_func(msg, &ctx.progress);
2525 do_extract_warnings(&ctx);
2528 out_free_realtarget:
2529 FREE(ctx.realtarget);
2530 out_teardown_stream_list:
2531 /* Free memory allocated as part of the mapping from each
2532 * wim_lookup_table_entry to the dentries that reference it. */
2533 if (!(ctx.extract_flags & WIMLIB_EXTRACT_FLAG_FILE_ORDER))
2534 list_for_each_entry(lte, &ctx.stream_list, extraction_list)
2535 if (lte->out_refcnt > ARRAY_LEN(lte->inline_lte_dentries))
2536 FREE(lte->lte_dentries);
2537 out_finish_or_abort_extract:
2539 if (ctx.ops->abort_extract)
2540 ctx.ops->abort_extract(&ctx);
2542 if (ctx.ops->finish_extract)
2543 ret = ctx.ops->finish_extract(&ctx);
2545 out_dentry_reset_needs_extraction:
2546 for_dentry_in_tree(ctx.extract_root, dentry_reset_needs_extraction, NULL);
2550 /* Make sure the extraction flags make sense, and update them if needed. */
2552 check_extract_flags(const WIMStruct *wim, int *extract_flags_p)
2554 int extract_flags = *extract_flags_p;
2556 /* Check for invalid flag combinations */
2557 if ((extract_flags &
2558 (WIMLIB_EXTRACT_FLAG_SYMLINK |
2559 WIMLIB_EXTRACT_FLAG_HARDLINK)) == (WIMLIB_EXTRACT_FLAG_SYMLINK |
2560 WIMLIB_EXTRACT_FLAG_HARDLINK))
2561 return WIMLIB_ERR_INVALID_PARAM;
2563 if ((extract_flags &
2564 (WIMLIB_EXTRACT_FLAG_NO_ACLS |
2565 WIMLIB_EXTRACT_FLAG_STRICT_ACLS)) == (WIMLIB_EXTRACT_FLAG_NO_ACLS |
2566 WIMLIB_EXTRACT_FLAG_STRICT_ACLS))
2567 return WIMLIB_ERR_INVALID_PARAM;
2569 if ((extract_flags &
2570 (WIMLIB_EXTRACT_FLAG_RPFIX |
2571 WIMLIB_EXTRACT_FLAG_NORPFIX)) == (WIMLIB_EXTRACT_FLAG_RPFIX |
2572 WIMLIB_EXTRACT_FLAG_NORPFIX))
2573 return WIMLIB_ERR_INVALID_PARAM;
2575 if ((extract_flags &
2576 (WIMLIB_EXTRACT_FLAG_RESUME |
2577 WIMLIB_EXTRACT_FLAG_FROM_PIPE)) == WIMLIB_EXTRACT_FLAG_RESUME)
2578 return WIMLIB_ERR_INVALID_PARAM;
2580 #ifndef WITH_NTFS_3G
2581 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
2582 ERROR("wimlib was compiled without support for NTFS-3g, so\n"
2583 " we cannot apply a WIM image directly to a NTFS volume.");
2584 return WIMLIB_ERR_UNSUPPORTED;
2588 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_RPFIX |
2589 WIMLIB_EXTRACT_FLAG_NORPFIX)) == 0)
2591 /* Do reparse point fixups by default if the WIM header says
2592 * they are enabled. */
2593 if (wim->hdr.flags & WIM_HDR_FLAG_RP_FIX)
2594 extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
2597 /* TODO: Since UNIX data entries are stored in the file resources, in a
2598 * completely sequential extraction they may come up before the
2599 * corresponding file or symbolic link data. This needs to be handled
2601 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_UNIX_DATA |
2602 WIMLIB_EXTRACT_FLAG_FILE_ORDER))
2603 == WIMLIB_EXTRACT_FLAG_UNIX_DATA)
2605 if (extract_flags & WIMLIB_EXTRACT_FLAG_FROM_PIPE) {
2606 WARNING("Setting UNIX file/owner group may "
2607 "be impossible on some\n"
2609 "when applying from a pipe.");
2611 extract_flags |= WIMLIB_EXTRACT_FLAG_FILE_ORDER;
2612 WARNING("Disabling sequential extraction for "
2617 *extract_flags_p = extract_flags;
2622 get_wildcard_flags(int extract_flags)
2624 u32 wildcard_flags = 0;
2626 if (extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_GLOB)
2627 wildcard_flags |= WILDCARD_FLAG_ERROR_IF_NO_MATCH;
2629 wildcard_flags |= WILDCARD_FLAG_WARN_IF_NO_MATCH;
2631 if (default_ignore_case)
2632 wildcard_flags |= WILDCARD_FLAG_CASE_INSENSITIVE;
2634 return wildcard_flags;
2637 struct append_dentry_ctx {
2638 struct wim_dentry **dentries;
2639 size_t num_dentries;
2640 size_t num_alloc_dentries;
2644 append_dentry_cb(struct wim_dentry *dentry, void *_ctx)
2646 struct append_dentry_ctx *ctx = _ctx;
2648 if (ctx->num_dentries == ctx->num_alloc_dentries) {
2649 struct wim_dentry **new_dentries;
2652 new_length = max(ctx->num_alloc_dentries + 8,
2653 ctx->num_alloc_dentries * 3 / 2);
2654 new_dentries = REALLOC(ctx->dentries,
2655 new_length * sizeof(ctx->dentries[0]));
2656 if (new_dentries == NULL)
2657 return WIMLIB_ERR_NOMEM;
2658 ctx->dentries = new_dentries;
2659 ctx->num_alloc_dentries = new_length;
2661 ctx->dentries[ctx->num_dentries++] = dentry;
2666 do_wimlib_extract_paths(WIMStruct *wim,
2668 const tchar *target,
2669 const tchar * const *paths,
2672 wimlib_progress_func_t progress_func)
2675 struct wim_dentry **trees;
2678 if (wim == NULL || target == NULL || target[0] == T('\0') ||
2679 (num_paths != 0 && paths == NULL))
2680 return WIMLIB_ERR_INVALID_PARAM;
2682 ret = check_extract_flags(wim, &extract_flags);
2686 ret = select_wim_image(wim, image);
2690 ret = wim_checksum_unhashed_streams(wim);
2694 if (extract_flags & WIMLIB_EXTRACT_FLAG_GLOB_PATHS) {
2696 struct append_dentry_ctx append_dentry_ctx = {
2699 .num_alloc_dentries = 0,
2702 u32 wildcard_flags = get_wildcard_flags(extract_flags);
2704 for (size_t i = 0; i < num_paths; i++) {
2705 tchar *path = canonicalize_wim_path(paths[i]);
2707 ret = WIMLIB_ERR_NOMEM;
2708 trees = append_dentry_ctx.dentries;
2709 goto out_free_trees;
2711 ret = expand_wildcard(wim, path,
2717 trees = append_dentry_ctx.dentries;
2718 goto out_free_trees;
2721 trees = append_dentry_ctx.dentries;
2722 num_trees = append_dentry_ctx.num_dentries;
2724 trees = MALLOC(num_paths * sizeof(trees[0]));
2726 return WIMLIB_ERR_NOMEM;
2728 for (size_t i = 0; i < num_paths; i++) {
2730 tchar *path = canonicalize_wim_path(paths[i]);
2732 ret = WIMLIB_ERR_NOMEM;
2733 goto out_free_trees;
2736 trees[i] = get_dentry(wim, path,
2737 WIMLIB_CASE_PLATFORM_DEFAULT);
2739 if (trees[i] == NULL) {
2740 ERROR("Path \"%"TS"\" does not exist "
2742 paths[i], wim->current_image);
2743 ret = WIMLIB_ERR_PATH_DOES_NOT_EXIST;
2744 goto out_free_trees;
2747 num_trees = num_paths;
2750 if (num_trees == 0) {
2752 goto out_free_trees;
2755 ret = extract_trees(wim, trees, num_trees,
2756 target, extract_flags, progress_func);
2763 extract_single_image(WIMStruct *wim, int image,
2764 const tchar *target, int extract_flags,
2765 wimlib_progress_func_t progress_func)
2767 const tchar *path = T("");
2768 return do_wimlib_extract_paths(wim, image, target, &path, 1,
2769 extract_flags | WIMLIB_EXTRACT_FLAG_IMAGEMODE,
2773 static const tchar * const filename_forbidden_chars =
2782 /* This function checks if it is okay to use a WIM image's name as a directory
2785 image_name_ok_as_dir(const tchar *image_name)
2787 return image_name && *image_name &&
2788 !tstrpbrk(image_name, filename_forbidden_chars) &&
2789 tstrcmp(image_name, T(".")) &&
2790 tstrcmp(image_name, T(".."));
2793 /* Extracts all images from the WIM to the directory @target, with the images
2794 * placed in subdirectories named by their image names. */
2796 extract_all_images(WIMStruct *wim,
2797 const tchar *target,
2799 wimlib_progress_func_t progress_func)
2801 size_t image_name_max_len = max(xml_get_max_image_name_len(wim), 20);
2802 size_t output_path_len = tstrlen(target);
2803 tchar buf[output_path_len + 1 + image_name_max_len + 1];
2806 const tchar *image_name;
2809 extract_flags |= WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
2811 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
2812 ERROR("Cannot extract multiple images in NTFS extraction mode.");
2813 return WIMLIB_ERR_INVALID_PARAM;
2816 if (tstat(target, &stbuf)) {
2817 if (errno == ENOENT) {
2818 if (tmkdir(target, 0755)) {
2819 ERROR_WITH_ERRNO("Failed to create directory \"%"TS"\"", target);
2820 return WIMLIB_ERR_MKDIR;
2823 ERROR_WITH_ERRNO("Failed to stat \"%"TS"\"", target);
2824 return WIMLIB_ERR_STAT;
2826 } else if (!S_ISDIR(stbuf.st_mode)) {
2827 ERROR("\"%"TS"\" is not a directory", target);
2828 return WIMLIB_ERR_NOTDIR;
2831 tmemcpy(buf, target, output_path_len);
2832 buf[output_path_len] = OS_PREFERRED_PATH_SEPARATOR;
2833 for (image = 1; image <= wim->hdr.image_count; image++) {
2834 image_name = wimlib_get_image_name(wim, image);
2835 if (image_name_ok_as_dir(image_name)) {
2836 tstrcpy(buf + output_path_len + 1, image_name);
2838 /* Image name is empty or contains forbidden characters.
2839 * Use image number instead. */
2840 tsprintf(buf + output_path_len + 1, T("%d"), image);
2842 ret = extract_single_image(wim, image, buf, extract_flags,
2851 clear_lte_extracted_file(WIMStruct *wim, int extract_flags)
2853 if (unlikely(extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
2854 WIMLIB_EXTRACT_FLAG_HARDLINK)))
2855 for_lookup_table_entry(wim->lookup_table,
2856 lte_free_extracted_file, NULL);
2860 do_wimlib_extract_image(WIMStruct *wim,
2862 const tchar *target,
2864 wimlib_progress_func_t progress_func)
2868 if (extract_flags & (WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE |
2869 WIMLIB_EXTRACT_FLAG_TO_STDOUT))
2870 return WIMLIB_ERR_INVALID_PARAM;
2872 if (image == WIMLIB_ALL_IMAGES)
2873 ret = extract_all_images(wim, target, extract_flags,
2876 ret = extract_single_image(wim, image, target, extract_flags,
2879 clear_lte_extracted_file(wim, extract_flags);
2884 /****************************************************************************
2886 ****************************************************************************/
2888 /* Note: new code should use wimlib_extract_paths() instead of
2889 * wimlib_extract_files() if possible. */
2891 wimlib_extract_files(WIMStruct *wim,
2893 const struct wimlib_extract_command *cmds,
2895 int default_extract_flags,
2896 wimlib_progress_func_t progress_func)
2905 default_extract_flags |= WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE;
2907 for (size_t i = 0; i < num_cmds; i++) {
2908 int cmd_flags = (cmds[i].extract_flags |
2909 default_extract_flags);
2911 if (cmd_flags & ~WIMLIB_EXTRACT_MASK_PUBLIC)
2912 return WIMLIB_ERR_INVALID_PARAM;
2914 int cmd_link_flags = (cmd_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
2915 WIMLIB_EXTRACT_FLAG_HARDLINK));
2917 link_flags = cmd_link_flags;
2919 if (cmd_link_flags != link_flags) {
2920 ERROR("The same symlink or hardlink extraction mode "
2921 "must be set on all extraction commands!");
2922 return WIMLIB_ERR_INVALID_PARAM;
2925 all_flags |= cmd_flags;
2927 if (all_flags & WIMLIB_EXTRACT_FLAG_GLOB_PATHS) {
2928 ERROR("Glob paths not supported for wimlib_extract_files(). "
2929 "Use wimlib_extract_paths() instead.");
2930 return WIMLIB_ERR_INVALID_PARAM;
2933 for (size_t i = 0; i < num_cmds; i++) {
2934 int extract_flags = (cmds[i].extract_flags |
2935 default_extract_flags);
2936 const tchar *target = cmds[i].fs_dest_path;
2937 const tchar *wim_source_path = cmds[i].wim_source_path;
2939 ret = do_wimlib_extract_paths(wim, image, target,
2940 &wim_source_path, 1,
2941 extract_flags | WIMLIB_EXTRACT_FLAG_FILEMODE,
2947 clear_lte_extracted_file(wim, all_flags);
2952 wimlib_extract_paths(WIMStruct *wim,
2954 const tchar *target,
2955 const tchar * const *paths,
2958 wimlib_progress_func_t progress_func)
2962 if (extract_flags & ~WIMLIB_EXTRACT_MASK_PUBLIC)
2963 return WIMLIB_ERR_INVALID_PARAM;
2965 ret = do_wimlib_extract_paths(wim, image, target, paths, num_paths,
2966 extract_flags, progress_func);
2967 clear_lte_extracted_file(wim, extract_flags);
2972 wimlib_extract_pathlist(WIMStruct *wim, int image,
2973 const tchar *target,
2974 const tchar *path_list_file,
2976 wimlib_progress_func_t progress_func)
2983 ret = read_path_list_file(path_list_file, &paths, &num_paths, &mem);
2985 ERROR("Failed to read path list file \"%"TS"\"",
2990 ret = wimlib_extract_paths(wim, image, target,
2991 (const tchar * const *)paths, num_paths,
2992 extract_flags, progress_func);
2999 wimlib_extract_image_from_pipe(int pipe_fd, const tchar *image_num_or_name,
3000 const tchar *target, int extract_flags,
3001 wimlib_progress_func_t progress_func)
3005 struct filedes *in_fd;
3009 if (extract_flags & ~WIMLIB_EXTRACT_MASK_PUBLIC)
3010 return WIMLIB_ERR_INVALID_PARAM;
3012 if (extract_flags & WIMLIB_EXTRACT_FLAG_FILE_ORDER)
3013 return WIMLIB_ERR_INVALID_PARAM;
3015 /* Read the WIM header from the pipe and get a WIMStruct to represent
3016 * the pipable WIM. Caveats: Unlike getting a WIMStruct with
3017 * wimlib_open_wim(), getting a WIMStruct in this way will result in
3018 * an empty lookup table, no XML data read, and no filename set. */
3019 ret = open_wim_as_WIMStruct(&pipe_fd,
3020 WIMLIB_OPEN_FLAG_FROM_PIPE,
3021 &pwm, progress_func);
3025 /* Sanity check to make sure this is a pipable WIM. */
3026 if (pwm->hdr.magic != PWM_MAGIC) {
3027 ERROR("The WIM being read from file descriptor %d "
3028 "is not pipable!", pipe_fd);
3029 ret = WIMLIB_ERR_NOT_PIPABLE;
3030 goto out_wimlib_free;
3033 /* Sanity check to make sure the first part of a pipable split WIM is
3034 * sent over the pipe first. */
3035 if (pwm->hdr.part_number != 1) {
3036 ERROR("The first part of the split WIM must be "
3037 "sent over the pipe first.");
3038 ret = WIMLIB_ERR_INVALID_PIPABLE_WIM;
3039 goto out_wimlib_free;
3042 in_fd = &pwm->in_fd;
3043 wimlib_assert(in_fd->offset == WIM_HEADER_DISK_SIZE);
3045 /* As mentioned, the WIMStruct we created from the pipe does not have
3046 * XML data yet. Fix this by reading the extra copy of the XML data
3047 * that directly follows the header in pipable WIMs. (Note: see
3048 * write_pipable_wim() for more details about the format of pipable
3051 struct wim_lookup_table_entry xml_lte;
3052 struct wim_resource_spec xml_rspec;
3053 ret = read_pwm_stream_header(pwm, &xml_lte, &xml_rspec, 0, NULL);
3055 goto out_wimlib_free;
3057 if (!(xml_lte.flags & WIM_RESHDR_FLAG_METADATA))
3059 ERROR("Expected XML data, but found non-metadata "
3061 ret = WIMLIB_ERR_INVALID_PIPABLE_WIM;
3062 goto out_wimlib_free;
3065 wim_res_spec_to_hdr(&xml_rspec, &pwm->hdr.xml_data_reshdr);
3067 ret = read_wim_xml_data(pwm);
3069 goto out_wimlib_free;
3071 if (wim_info_get_num_images(pwm->wim_info) != pwm->hdr.image_count) {
3072 ERROR("Image count in XML data is not the same as in WIM header.");
3073 ret = WIMLIB_ERR_XML;
3074 goto out_wimlib_free;
3078 /* Get image index (this may use the XML data that was just read to
3079 * resolve an image name). */
3080 if (image_num_or_name) {
3081 image = wimlib_resolve_image(pwm, image_num_or_name);
3082 if (image == WIMLIB_NO_IMAGE) {
3083 ERROR("\"%"TS"\" is not a valid image in the pipable WIM!",
3085 ret = WIMLIB_ERR_INVALID_IMAGE;
3086 goto out_wimlib_free;
3087 } else if (image == WIMLIB_ALL_IMAGES) {
3088 ERROR("Applying all images from a pipe is not supported.");
3089 ret = WIMLIB_ERR_INVALID_IMAGE;
3090 goto out_wimlib_free;
3093 if (pwm->hdr.image_count != 1) {
3094 ERROR("No image was specified, but the pipable WIM "
3095 "did not contain exactly 1 image");
3096 ret = WIMLIB_ERR_INVALID_IMAGE;
3097 goto out_wimlib_free;
3102 /* Load the needed metadata resource. */
3103 for (i = 1; i <= pwm->hdr.image_count; i++) {
3104 struct wim_lookup_table_entry *metadata_lte;
3105 struct wim_image_metadata *imd;
3106 struct wim_resource_spec *metadata_rspec;
3108 metadata_lte = new_lookup_table_entry();
3109 if (metadata_lte == NULL) {
3110 ret = WIMLIB_ERR_NOMEM;
3111 goto out_wimlib_free;
3113 metadata_rspec = MALLOC(sizeof(struct wim_resource_spec));
3114 if (metadata_rspec == NULL) {
3115 ret = WIMLIB_ERR_NOMEM;
3116 free_lookup_table_entry(metadata_lte);
3117 goto out_wimlib_free;
3120 ret = read_pwm_stream_header(pwm, metadata_lte, metadata_rspec, 0, NULL);
3121 imd = pwm->image_metadata[i - 1];
3122 imd->metadata_lte = metadata_lte;
3124 FREE(metadata_rspec);
3125 goto out_wimlib_free;
3128 if (!(metadata_lte->flags & WIM_RESHDR_FLAG_METADATA)) {
3129 ERROR("Expected metadata resource, but found "
3130 "non-metadata stream.");
3131 ret = WIMLIB_ERR_INVALID_PIPABLE_WIM;
3132 goto out_wimlib_free;
3136 /* Metadata resource is for the images being extracted.
3137 * Parse it and save the metadata in memory. */
3138 ret = read_metadata_resource(pwm, imd);
3140 goto out_wimlib_free;
3143 /* Metadata resource is not for the image being
3144 * extracted. Skip over it. */
3145 ret = skip_wim_stream(metadata_lte);
3147 goto out_wimlib_free;
3150 /* Extract the image. */
3151 extract_flags |= WIMLIB_EXTRACT_FLAG_FROM_PIPE;
3152 ret = do_wimlib_extract_image(pwm, image, target,
3153 extract_flags, progress_func);
3154 /* Clean up and return. */
3161 wimlib_extract_image(WIMStruct *wim,
3163 const tchar *target,
3165 wimlib_progress_func_t progress_func)
3167 if (extract_flags & ~WIMLIB_EXTRACT_MASK_PUBLIC)
3168 return WIMLIB_ERR_INVALID_PARAM;
3169 return do_wimlib_extract_image(wim, image, target, extract_flags,