4 * Support for extracting WIM images, or files or directories contained in a WIM
9 * Copyright (C) 2012, 2013, 2014 Eric Biggers
11 * This file is part of wimlib, a library for working with WIM files.
13 * wimlib is free software; you can redistribute it and/or modify it under the
14 * terms of the GNU General Public License as published by the Free
15 * Software Foundation; either version 3 of the License, or (at your option)
18 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
19 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
20 * A PARTICULAR PURPOSE. See the GNU General Public License for more
23 * You should have received a copy of the GNU General Public License
24 * along with wimlib; if not, see http://www.gnu.org/licenses/.
28 * This file provides the API functions wimlib_extract_image(),
29 * wimlib_extract_image_from_pipe(), wimlib_extract_paths(), and
30 * wimlib_extract_pathlist(). Internally, all end up calling
31 * do_wimlib_extract_paths() and extract_trees().
33 * Although wimlib supports multiple extraction modes/backends (NTFS-3g, UNIX,
34 * Win32), this file does not itself have code to extract files or directories
35 * to any specific target; instead, it handles generic functionality and relies
36 * on lower-level callback functions declared in `struct apply_operations' to do
37 * the actual extraction.
44 #include "wimlib/apply.h"
45 #include "wimlib/dentry.h"
46 #include "wimlib/encoding.h"
47 #include "wimlib/endianness.h"
48 #include "wimlib/error.h"
49 #include "wimlib/lookup_table.h"
50 #include "wimlib/metadata.h"
51 #include "wimlib/pathlist.h"
52 #include "wimlib/paths.h"
53 #include "wimlib/reparse.h"
54 #include "wimlib/resource.h"
55 #include "wimlib/security.h"
57 # include "wimlib/win32.h" /* for realpath() equivalent */
59 #include "wimlib/xml.h"
60 #include "wimlib/wildcard.h"
61 #include "wimlib/wim.h"
69 #define WIMLIB_EXTRACT_FLAG_MULTI_IMAGE 0x80000000
70 #define WIMLIB_EXTRACT_FLAG_FROM_PIPE 0x40000000
71 #define WIMLIB_EXTRACT_FLAG_IMAGEMODE 0x20000000
73 /* Keep in sync with wimlib.h */
74 #define WIMLIB_EXTRACT_MASK_PUBLIC \
75 (WIMLIB_EXTRACT_FLAG_NTFS | \
76 WIMLIB_EXTRACT_FLAG_HARDLINK | \
77 WIMLIB_EXTRACT_FLAG_SYMLINK | \
78 WIMLIB_EXTRACT_FLAG_VERBOSE | \
79 WIMLIB_EXTRACT_FLAG_SEQUENTIAL | \
80 WIMLIB_EXTRACT_FLAG_UNIX_DATA | \
81 WIMLIB_EXTRACT_FLAG_NO_ACLS | \
82 WIMLIB_EXTRACT_FLAG_STRICT_ACLS | \
83 WIMLIB_EXTRACT_FLAG_RPFIX | \
84 WIMLIB_EXTRACT_FLAG_NORPFIX | \
85 WIMLIB_EXTRACT_FLAG_TO_STDOUT | \
86 WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES | \
87 WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS | \
88 WIMLIB_EXTRACT_FLAG_STRICT_TIMESTAMPS | \
89 WIMLIB_EXTRACT_FLAG_STRICT_SHORT_NAMES | \
90 WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS | \
91 WIMLIB_EXTRACT_FLAG_RESUME | \
92 WIMLIB_EXTRACT_FLAG_FILE_ORDER | \
93 WIMLIB_EXTRACT_FLAG_GLOB_PATHS | \
94 WIMLIB_EXTRACT_FLAG_STRICT_GLOB | \
95 WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES | \
96 WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE | \
97 WIMLIB_EXTRACT_FLAG_WIMBOOT)
100 dentry_in_list(const struct wim_dentry *dentry)
102 return dentry->extraction_list.next != NULL;
106 is_linked_extraction(const struct apply_ctx *ctx)
108 return 0 != (ctx->extract_flags & (WIMLIB_EXTRACT_FLAG_HARDLINK |
109 WIMLIB_EXTRACT_FLAG_SYMLINK));
113 can_extract_named_data_streams(const struct apply_ctx *ctx)
115 return ctx->supported_features.named_data_streams &&
116 !is_linked_extraction(ctx);
118 /* Inform library user of progress of stream extraction following the successful
119 * extraction of a copy of the stream specified by @lte. */
121 update_extract_progress(struct apply_ctx *ctx,
122 const struct wim_lookup_table_entry *lte)
124 wimlib_progress_func_t progress_func = ctx->progress_func;
125 union wimlib_progress_info *progress = &ctx->progress;
127 progress->extract.completed_bytes += lte->size;
129 progress->extract.completed_bytes >= ctx->next_progress)
131 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS, progress);
132 if (progress->extract.completed_bytes >=
133 progress->extract.total_bytes)
135 ctx->next_progress = ~0ULL;
137 ctx->next_progress += progress->extract.total_bytes / 128;
138 if (ctx->next_progress > progress->extract.total_bytes)
139 ctx->next_progress = progress->extract.total_bytes;
145 /* Extract a symbolic link (not directly as reparse data), handling fixing up
146 * the target of absolute symbolic links and updating the extract progress.
148 * @inode must specify the WIM inode for a symbolic link or junction reparse
151 * @lte_override overrides the resource used as the reparse data for the
154 extract_symlink(const tchar *path, struct apply_ctx *ctx,
155 struct wim_inode *inode,
156 struct wim_lookup_table_entry *lte_override)
158 ssize_t bufsize = ctx->ops->path_max;
159 tchar target[bufsize];
165 /* If absolute symbolic link fixups requested, reserve space in the link
166 * target buffer for the absolute path of the target directory. */
167 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX)
169 buf += ctx->realtarget_nchars;
170 bufsize -= ctx->realtarget_nchars;
173 /* Translate the WIM inode's reparse data into the link target. */
174 sret = wim_inode_readlink(inode, buf, bufsize - 1, lte_override);
177 return WIMLIB_ERR_READLINK;
181 if ((ctx->extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX) &&
184 /* Fix absolute symbolic link target to point into the
185 * actual extraction destination. */
186 tmemcpy(target, ctx->realtarget, ctx->realtarget_nchars);
187 fixed_target = target;
189 /* Keep same link target. */
193 /* Call into the apply_operations to create the symbolic link. */
194 DEBUG("Creating symlink \"%"TS"\" => \"%"TS"\"",
196 ret = ctx->ops->create_symlink(fixed_target, path, ctx);
198 ERROR_WITH_ERRNO("Failed to create symlink "
199 "\"%"TS"\" => \"%"TS"\"", path, fixed_target);
203 /* Account for reparse data consumed. */
204 update_extract_progress(ctx,
205 (lte_override ? lte_override :
206 inode_unnamed_lte_resolved(inode)));
209 #endif /* !__WIN32__ */
211 /* Create a file, directory, or symbolic link. */
213 extract_inode(const tchar *path, struct apply_ctx *ctx, struct wim_inode *inode)
218 if (ctx->supported_features.symlink_reparse_points &&
219 !ctx->supported_features.reparse_points &&
220 inode_is_symlink(inode))
222 ret = extract_symlink(path, ctx, inode, NULL);
224 #endif /* !__WIN32__ */
225 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY) {
226 ret = ctx->ops->create_directory(path, ctx, &inode->extract_cookie);
228 ERROR_WITH_ERRNO("Failed to create the directory "
231 } else if ((inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED) &&
232 ctx->ops->extract_encrypted_stream_creates_file &&
233 ctx->supported_features.encrypted_files) {
234 ret = ctx->ops->extract_encrypted_stream(
235 path, inode_unnamed_lte_resolved(inode), ctx);
237 ERROR_WITH_ERRNO("Failed to create and extract "
238 "encrypted file \"%"TS"\"", path);
241 ret = ctx->ops->create_file(path, ctx, &inode->extract_cookie);
243 ERROR_WITH_ERRNO("Failed to create the file "
251 extract_hardlink(const tchar *oldpath, const tchar *newpath,
252 struct apply_ctx *ctx)
256 DEBUG("Creating hardlink \"%"TS"\" => \"%"TS"\"", newpath, oldpath);
257 ret = ctx->ops->create_hardlink(oldpath, newpath, ctx);
259 ERROR_WITH_ERRNO("Failed to create hardlink "
260 "\"%"TS"\" => \"%"TS"\"",
268 try_extract_rpfix(u8 *rpbuf,
270 const wchar_t *extract_root_realpath,
271 unsigned extract_root_realpath_nchars)
273 struct reparse_data rpdata;
275 size_t target_nchars;
276 size_t stripped_nchars;
277 wchar_t *stripped_target;
278 wchar_t stripped_target_nchars;
281 utf16lechar *new_target;
282 utf16lechar *new_print_name;
283 size_t new_target_nchars;
284 size_t new_print_name_nchars;
287 ret = parse_reparse_data(rpbuf, *rpbuflen_p, &rpdata);
291 if (extract_root_realpath[0] == L'\0' ||
292 extract_root_realpath[1] != L':' ||
293 extract_root_realpath[2] != L'\\')
294 return WIMLIB_ERR_REPARSE_POINT_FIXUP_FAILED;
296 ret = parse_substitute_name(rpdata.substitute_name,
297 rpdata.substitute_name_nbytes,
301 stripped_nchars = ret;
302 target = rpdata.substitute_name;
303 target_nchars = rpdata.substitute_name_nbytes / sizeof(utf16lechar);
304 stripped_target = target + stripped_nchars;
305 stripped_target_nchars = target_nchars - stripped_nchars;
307 new_target = alloca((6 + extract_root_realpath_nchars +
308 stripped_target_nchars) * sizeof(utf16lechar));
311 if (stripped_nchars == 6) {
312 /* Include \??\ prefix if it was present before */
313 p = wmempcpy(p, L"\\??\\", 4);
316 /* Print name excludes the \??\ if present. */
318 if (stripped_nchars != 0) {
319 /* Get drive letter from real path to extract root, if a drive
320 * letter was present before. */
321 *p++ = extract_root_realpath[0];
322 *p++ = extract_root_realpath[1];
324 /* Copy the rest of the extract root */
325 p = wmempcpy(p, extract_root_realpath + 2, extract_root_realpath_nchars - 2);
327 /* Append the stripped target */
328 p = wmempcpy(p, stripped_target, stripped_target_nchars);
329 new_target_nchars = p - new_target;
330 new_print_name_nchars = p - new_print_name;
332 if (new_target_nchars * sizeof(utf16lechar) >= REPARSE_POINT_MAX_SIZE ||
333 new_print_name_nchars * sizeof(utf16lechar) >= REPARSE_POINT_MAX_SIZE)
334 return WIMLIB_ERR_REPARSE_POINT_FIXUP_FAILED;
336 rpdata.substitute_name = new_target;
337 rpdata.substitute_name_nbytes = new_target_nchars * sizeof(utf16lechar);
338 rpdata.print_name = new_print_name;
339 rpdata.print_name_nbytes = new_print_name_nchars * sizeof(utf16lechar);
340 return make_reparse_buffer(&rpdata, rpbuf, rpbuflen_p);
342 #endif /* __WIN32__ */
344 /* Set reparse data on extracted file or directory that has
345 * FILE_ATTRIBUTE_REPARSE_POINT set. */
347 extract_reparse_data(const tchar *path, struct apply_ctx *ctx,
348 struct wim_inode *inode,
349 struct wim_lookup_table_entry *lte_override)
352 u8 rpbuf[REPARSE_POINT_MAX_SIZE];
355 ret = wim_inode_get_reparse_data(inode, rpbuf, &rpbuflen, lte_override);
360 /* Fix up target of absolute symbolic link or junction points so
361 * that they point into the actual extraction target. */
362 if ((ctx->extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX) &&
363 (inode->i_reparse_tag == WIM_IO_REPARSE_TAG_SYMLINK ||
364 inode->i_reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT) &&
365 !inode->i_not_rpfixed)
367 ret = try_extract_rpfix(rpbuf, &rpbuflen, ctx->realtarget,
368 ctx->realtarget_nchars);
369 if (ret && !(ctx->extract_flags &
370 WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS))
372 WARNING("Reparse point fixup of \"%"TS"\" "
381 ret = ctx->ops->set_reparse_data(path, rpbuf, rpbuflen, ctx);
383 /* On Windows, the SeCreateSymbolicLink privilege is required to create
384 * symbolic links. To be more friendly towards non-Administrator users,
385 * we merely warn the user if symbolic links cannot be created due to
386 * insufficient permissions or privileges, unless
387 * WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS was provided. */
389 if (ret && inode_is_symlink(inode) &&
390 (errno == EACCES || errno == EPERM) &&
391 !(ctx->extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS))
393 WARNING("Can't set reparse data on \"%"TS"\": "
395 " You may be trying to "
396 "extract a symbolic link without the\n"
397 " SeCreateSymbolicLink privilege, "
398 "which by default non-Administrator\n"
399 " accounts do not have.",
407 /* Account for reparse data consumed. */
408 update_extract_progress(ctx,
409 (lte_override ? lte_override :
410 inode_unnamed_lte_resolved(inode)));
414 ERROR_WITH_ERRNO("Failed to set reparse data on \"%"TS"\"", path);
419 * Extract zero or more streams to a file.
421 * This function operates slightly differently depending on whether @lte_spec is
422 * NULL or not. When @lte_spec is NULL, the behavior is to extract the default
423 * file contents (unnamed stream), and, if named data streams are supported in
424 * the extract mode and volume, any named data streams. When @lte_spec is not
425 * NULL, the behavior is to extract only all copies of the stream @lte_spec, and
426 * in addition use @lte_spec to set the reparse data or create the symbolic link
430 * Path to file to extract (as can be passed to apply_operations
435 * WIM dentry that corresponds to the file being extracted.
437 * If non-NULL, specifies the lookup table entry for a stream to extract,
438 * and only that stream will be extracted (although there may be more than
439 * one instance of it).
441 * Used only if @lte_spec != NULL; it is passed to the extraction functions
442 * rather than @lte_spec, allowing the location of the stream to be
443 * overridden. (This is used when the WIM is being read from a nonseekable
444 * file, such as a pipe, when streams need to be used more than once; each
445 * such stream is extracted to a temporary file.)
448 extract_streams(const tchar *path, struct apply_ctx *ctx,
449 struct wim_dentry *dentry,
450 struct wim_lookup_table_entry *lte_spec,
451 struct wim_lookup_table_entry *lte_override)
453 struct wim_inode *inode = dentry->d_inode;
454 struct wim_lookup_table_entry *lte;
455 file_spec_t file_spec;
458 if (dentry->was_linked)
465 for (unsigned i = 0; i < SHA1_HASH_SIZE; i++)
466 p += sprintf(p, "%02x", lte_override->hash[i]);
467 DEBUG("Extracting stream SHA1=%s to \"%"TS"\"",
468 sha1_str, path, inode->i_ino);
470 DEBUG("Extracting streams to \"%"TS"\"", path, inode->i_ino);
474 if (ctx->ops->uses_cookies)
475 file_spec.cookie = inode->extract_cookie;
477 file_spec.path = path;
479 /* Unnamed data stream. */
480 lte = inode_unnamed_lte_resolved(inode);
481 if (lte && (!lte_spec || lte == lte_spec)) {
484 if (!(inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
485 FILE_ATTRIBUTE_REPARSE_POINT)))
487 if (inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED &&
488 ctx->supported_features.encrypted_files) {
489 if (!ctx->ops->extract_encrypted_stream_creates_file) {
490 ret = ctx->ops->extract_encrypted_stream(
496 ret = ctx->ops->extract_unnamed_stream(
502 update_extract_progress(ctx, lte);
504 else if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT)
507 if (ctx->supported_features.reparse_points)
508 ret = extract_reparse_data(path, ctx, inode, lte);
510 else if ((inode_is_symlink(inode) &&
511 ctx->supported_features.symlink_reparse_points))
512 ret = extract_symlink(path, ctx, inode, lte);
519 /* Named data streams. */
520 if (can_extract_named_data_streams(ctx)) {
521 for (u16 i = 0; i < inode->i_num_ads; i++) {
522 struct wim_ads_entry *entry = &inode->i_ads_entries[i];
524 if (!ads_entry_is_named_stream(entry))
529 if (lte_spec && lte_spec != lte)
533 ret = ctx->ops->extract_named_stream(file_spec, entry->stream_name,
534 entry->stream_name_nbytes / 2,
538 update_extract_progress(ctx, lte);
544 ERROR_WITH_ERRNO("Failed to extract data of \"%"TS"\"", path);
548 /* Set attributes on an extracted file or directory if supported by the
549 * extraction mode. */
551 extract_file_attributes(const tchar *path, struct apply_ctx *ctx,
552 struct wim_dentry *dentry, unsigned pass)
556 if (ctx->ops->set_file_attributes &&
557 !(ctx->extract_flags & WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES) &&
558 !(dentry == ctx->target_dentry && ctx->root_dentry_is_special)) {
559 u32 attributes = dentry->d_inode->i_attributes;
561 /* Clear unsupported attributes. */
562 attributes &= ctx->supported_attributes_mask;
564 if ((attributes & FILE_ATTRIBUTE_DIRECTORY &&
565 !ctx->supported_features.encrypted_directories) ||
566 (!(attributes & FILE_ATTRIBUTE_DIRECTORY) &&
567 !ctx->supported_features.encrypted_files))
569 attributes &= ~FILE_ATTRIBUTE_ENCRYPTED;
573 attributes = FILE_ATTRIBUTE_NORMAL;
575 ret = ctx->ops->set_file_attributes(path, attributes, ctx, pass);
577 ERROR_WITH_ERRNO("Failed to set attributes on "
586 /* Set or remove the short (DOS) name on an extracted file or directory if
587 * supported by the extraction mode. Since DOS names are unimportant and it's
588 * easy to run into problems setting them on Windows (SetFileShortName()
589 * requires SE_RESTORE privilege, which only the Administrator can request, and
590 * also requires DELETE access to the file), failure is ignored unless
591 * WIMLIB_EXTRACT_FLAG_STRICT_SHORT_NAMES is set. */
593 extract_short_name(const tchar *path, struct apply_ctx *ctx,
594 struct wim_dentry *dentry)
598 /* The root of the dentry tree being extracted may not be extracted to
599 * its original name, so its short name should be ignored. */
600 if (dentry == ctx->target_dentry)
603 if (ctx->supported_features.short_names) {
604 ret = ctx->ops->set_short_name(path,
606 dentry->short_name_nbytes / 2,
608 if (ret && (ctx->extract_flags &
609 WIMLIB_EXTRACT_FLAG_STRICT_SHORT_NAMES))
611 ERROR_WITH_ERRNO("Failed to set short name of "
619 /* Set security descriptor, UNIX data, or neither on an extracted file, taking
620 * into account the current extraction mode and flags. */
622 extract_security(const tchar *path, struct apply_ctx *ctx,
623 struct wim_dentry *dentry)
626 struct wim_inode *inode = dentry->d_inode;
628 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_NO_ACLS)
631 if ((ctx->target_dentry == dentry) && ctx->root_dentry_is_special)
635 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
636 struct wimlib_unix_data data;
638 ret = inode_get_unix_data(inode, &data, NULL);
642 ret = ctx->ops->set_unix_data(path, &data, ctx);
644 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_ACLS) {
645 ERROR_WITH_ERRNO("Failed to set UNIX owner, "
646 "group, and/or mode on "
650 WARNING_WITH_ERRNO("Failed to set UNIX owner, "
651 "group, and/or/mode on "
657 #endif /* __WIN32__ */
658 if (ctx->supported_features.security_descriptors &&
659 inode->i_security_id != -1)
661 const struct wim_security_data *sd;
665 sd = wim_const_security_data(ctx->wim);
666 desc = sd->descriptors[inode->i_security_id];
667 desc_size = sd->sizes[inode->i_security_id];
669 ret = ctx->ops->set_security_descriptor(path, desc,
672 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_ACLS) {
673 ERROR_WITH_ERRNO("Failed to set security "
674 "descriptor on \"%"TS"\"", path);
678 if (errno != EACCES) {
679 WARNING_WITH_ERRNO("Failed to set "
680 "security descriptor "
681 "on \"%"TS"\"", path);
684 ctx->no_security_descriptors++;
691 /* Set timestamps on an extracted file. Failure is warning-only unless
692 * WIMLIB_EXTRACT_FLAG_STRICT_TIMESTAMPS is set. */
694 extract_timestamps(const tchar *path, struct apply_ctx *ctx,
695 struct wim_dentry *dentry)
697 struct wim_inode *inode = dentry->d_inode;
700 if ((ctx->target_dentry == dentry) && ctx->root_dentry_is_special)
703 if (ctx->ops->set_timestamps) {
704 ret = ctx->ops->set_timestamps(path,
705 inode->i_creation_time,
706 inode->i_last_write_time,
707 inode->i_last_access_time,
710 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_TIMESTAMPS) {
711 ERROR_WITH_ERRNO("Failed to set timestamps "
712 "on \"%"TS"\"", path);
715 WARNING_WITH_ERRNO("Failed to set timestamps "
716 "on \"%"TS"\"", path);
723 /* Check whether the extraction of a dentry should be skipped completely. */
725 dentry_is_supported(struct wim_dentry *dentry,
726 const struct wim_features *supported_features)
728 struct wim_inode *inode = dentry->d_inode;
730 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
731 return supported_features->reparse_points ||
732 (inode_is_symlink(inode) &&
733 supported_features->symlink_reparse_points);
735 if (inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED) {
736 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)
737 return supported_features->encrypted_directories != 0;
739 return supported_features->encrypted_files != 0;
744 /* Given a WIM dentry to extract, build the path to which to extract it, in the
745 * format understood by the callbacks in the apply_operations being used.
747 * Write the resulting path into @path, which must have room for at least
748 * ctx->ops->path_max characters.
750 * Return %true if successful; %false if this WIM dentry doesn't actually need
751 * to be extracted or if the calculated path exceeds ctx->ops->max_path
754 * This function clobbers the tmp_list member of @dentry and its ancestors up
755 * until the extraction root. */
757 build_extraction_path(tchar path[], struct wim_dentry *dentry,
758 const struct apply_ctx *ctx)
761 LIST_HEAD(ancestor_list);
763 const tchar *target_prefix;
764 size_t target_prefix_nchars;
765 struct wim_dentry *d;
767 path_nchars = ctx->ops->path_prefix_nchars;
769 if (ctx->ops->requires_realtarget_in_paths) {
770 target_prefix = ctx->realtarget;
771 target_prefix_nchars = ctx->realtarget_nchars;
772 } else if (ctx->ops->requires_target_in_paths) {
773 target_prefix = ctx->target;
774 target_prefix_nchars = ctx->target_nchars;
776 target_prefix = NULL;
777 target_prefix_nchars = 0;
779 path_nchars += target_prefix_nchars;
781 for (d = dentry; d != ctx->target_dentry; d = d->parent) {
782 if (!dentry_in_list(d))
785 path_nchars += d->extraction_name_nchars + 1;
786 list_add(&d->tmp_list, &ancestor_list);
789 path_nchars++; /* null terminator */
791 if (path_nchars > ctx->ops->path_max) {
792 WARNING("\"%"TS"\": Path too long to extract",
793 dentry_full_path(dentry));
797 p = tmempcpy(p, ctx->ops->path_prefix, ctx->ops->path_prefix_nchars);
798 p = tmempcpy(p, target_prefix, target_prefix_nchars);
799 list_for_each_entry(d, &ancestor_list, tmp_list) {
800 *p++ = ctx->ops->path_separator;
801 p = tmempcpy(p, d->extraction_name, d->extraction_name_nchars);
804 wimlib_assert(p - path == path_nchars);
809 get_num_path_components(const tchar *path, tchar path_separator)
811 unsigned num_components = 0;
813 /* Ignore drive letter. */
814 if (path[0] != L'\0' && path[1] == L':')
819 while (*path == path_separator)
823 while (*path && *path != path_separator)
826 return num_components;
830 extract_multiimage_symlink(const tchar *oldpath, const tchar *newpath,
831 struct apply_ctx *ctx, struct wim_dentry *dentry)
833 size_t num_raw_path_components;
834 const struct wim_dentry *d;
835 size_t num_target_path_components;
840 num_raw_path_components = 0;
841 for (d = dentry; d != ctx->target_dentry; d = d->parent)
842 num_raw_path_components++;
844 if (ctx->ops->requires_realtarget_in_paths)
845 num_target_path_components = get_num_path_components(ctx->realtarget,
846 ctx->ops->path_separator);
847 else if (ctx->ops->requires_target_in_paths)
848 num_target_path_components = get_num_path_components(ctx->target,
849 ctx->ops->path_separator);
851 num_target_path_components = 0;
853 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE) {
854 wimlib_assert(num_target_path_components > 0);
855 num_raw_path_components++;
856 num_target_path_components--;
859 p_old = oldpath + ctx->ops->path_prefix_nchars;
861 if (p_old[0] != L'\0' && p_old[1] == ':')
864 while (*p_old == ctx->ops->path_separator)
866 while (--num_target_path_components) {
867 while (*p_old != ctx->ops->path_separator)
869 while (*p_old == ctx->ops->path_separator)
873 tchar symlink_target[tstrlen(p_old) + 3 * num_raw_path_components + 1];
875 p = &symlink_target[0];
876 while (num_raw_path_components--) {
879 *p++ = ctx->ops->path_separator;
882 DEBUG("Creating symlink \"%"TS"\" => \"%"TS"\"",
883 newpath, symlink_target);
884 ret = ctx->ops->create_symlink(symlink_target, newpath, ctx);
886 ERROR_WITH_ERRNO("Failed to create symlink "
887 "\"%"TS"\" => \"%"TS"\"",
888 newpath, symlink_target);
893 /* Create the "skeleton" of an extracted file or directory. Don't yet extract
894 * data streams, reparse data (including symbolic links), timestamps, and
895 * security descriptors. Basically, everything that doesn't require reading
896 * non-metadata resources from the WIM file and isn't delayed until the final
899 do_dentry_extract_skeleton(tchar path[], struct wim_dentry *dentry,
900 struct apply_ctx *ctx)
902 struct wim_inode *inode = dentry->d_inode;
904 const tchar *oldpath;
906 if (unlikely(is_linked_extraction(ctx))) {
907 struct wim_lookup_table_entry *unnamed_lte;
909 unnamed_lte = inode_unnamed_lte_resolved(dentry->d_inode);
910 if (unnamed_lte && unnamed_lte->extracted_file) {
911 oldpath = unnamed_lte->extracted_file;
912 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_HARDLINK)
919 /* Create hard link if this dentry corresponds to an already-extracted
921 if (inode->i_extracted_file) {
922 oldpath = inode->i_extracted_file;
926 /* Skip symlinks unless they can be extracted as reparse points rather
927 * than created directly. */
928 if (inode_is_symlink(inode) && !ctx->supported_features.reparse_points)
931 /* Create this file or directory unless it's the extraction root, which
932 * was already created if necessary. */
933 if (dentry != ctx->target_dentry) {
934 ret = extract_inode(path, ctx, inode);
939 /* Create empty named data streams. */
940 if (can_extract_named_data_streams(ctx)) {
941 for (u16 i = 0; i < inode->i_num_ads; i++) {
942 file_spec_t file_spec;
943 struct wim_ads_entry *entry = &inode->i_ads_entries[i];
945 if (!ads_entry_is_named_stream(entry))
949 if (ctx->ops->uses_cookies)
950 file_spec.cookie = inode->extract_cookie;
952 file_spec.path = path;
953 ret = ctx->ops->extract_named_stream(file_spec,
955 entry->stream_name_nbytes / 2,
958 ERROR_WITH_ERRNO("\"%"TS"\": failed to create "
959 "empty named data stream",
966 /* Set file attributes (if supported). */
967 ret = extract_file_attributes(path, ctx, dentry, 0);
971 /* Set or remove file short name (if supported). */
972 ret = extract_short_name(path, ctx, dentry);
976 /* If inode has multiple links and hard links are supported in this
977 * extraction mode and volume, save the path to the extracted file in
978 * case it's needed to create a hard link. */
979 if (unlikely(is_linked_extraction(ctx))) {
980 struct wim_lookup_table_entry *unnamed_lte;
982 unnamed_lte = inode_unnamed_lte_resolved(dentry->d_inode);
984 unnamed_lte->extracted_file = TSTRDUP(path);
985 if (!unnamed_lte->extracted_file)
986 return WIMLIB_ERR_NOMEM;
988 } else if (inode->i_nlink > 1 && ctx->supported_features.hard_links) {
989 inode->i_extracted_file = TSTRDUP(path);
990 if (!inode->i_extracted_file)
991 return WIMLIB_ERR_NOMEM;
996 ret = extract_multiimage_symlink(oldpath, path, ctx, dentry);
999 dentry->was_linked = 1;
1003 ret = extract_hardlink(oldpath, path, ctx);
1006 dentry->was_linked = 1;
1010 /* This is a wrapper around do_dentry_extract_skeleton() that handles building
1011 * the path, doing short name reordering. This is also idempotent; dentries
1012 * already processed have skeleton_extracted set and no action is taken. See
1013 * apply_operations.requires_short_name_reordering for more details about short
1014 * name reordering. */
1016 dentry_extract_skeleton(struct wim_dentry *dentry, struct apply_ctx *ctx)
1018 tchar path[ctx->ops->path_max];
1019 struct wim_dentry *orig_dentry;
1020 struct wim_dentry *other_dentry;
1023 if (dentry->skeleton_extracted)
1027 if (ctx->supported_features.short_names
1028 && ctx->ops->requires_short_name_reordering
1029 && !dentry_has_short_name(dentry)
1030 && !dentry->d_inode->i_dos_name_extracted)
1032 inode_for_each_dentry(other_dentry, dentry->d_inode) {
1033 if (dentry_has_short_name(other_dentry)
1034 && !other_dentry->skeleton_extracted
1035 && dentry_in_list(other_dentry))
1037 DEBUG("Creating %"TS" before %"TS" "
1038 "to guarantee correct DOS name extraction",
1039 dentry_full_path(other_dentry),
1040 dentry_full_path(dentry));
1041 orig_dentry = dentry;
1042 dentry = other_dentry;
1048 if (!build_extraction_path(path, dentry, ctx))
1050 ret = do_dentry_extract_skeleton(path, dentry, ctx);
1054 dentry->skeleton_extracted = 1;
1057 dentry = orig_dentry;
1061 dentry->d_inode->i_dos_name_extracted = 1;
1065 /* Create a file or directory, then immediately extract all streams. The WIM
1066 * may not be read sequentially by this function. */
1068 dentry_extract(struct wim_dentry *dentry, struct apply_ctx *ctx)
1070 tchar path[ctx->ops->path_max];
1073 ret = dentry_extract_skeleton(dentry, ctx);
1077 if (!build_extraction_path(path, dentry, ctx))
1080 return extract_streams(path, ctx, dentry, NULL, NULL);
1083 /* Finish extracting a file, directory, or symbolic link by setting file
1084 * security and timestamps. */
1086 dentry_extract_final(struct wim_dentry *dentry, struct apply_ctx *ctx)
1089 tchar path[ctx->ops->path_max];
1091 if (!build_extraction_path(path, dentry, ctx))
1094 ret = extract_security(path, ctx, dentry);
1098 if (ctx->ops->requires_final_set_attributes_pass) {
1099 /* Set file attributes (if supported). */
1100 ret = extract_file_attributes(path, ctx, dentry, 1);
1105 return extract_timestamps(path, ctx, dentry);
1109 extract_structure(struct list_head *dentry_list, struct apply_ctx *ctx)
1111 struct wim_dentry *dentry;
1114 list_for_each_entry(dentry, dentry_list, extraction_list) {
1115 ret = dentry_extract_skeleton(dentry, ctx);
1123 extract_dir_structure(struct list_head *dentry_list, struct apply_ctx *ctx)
1125 struct wim_dentry *dentry;
1128 list_for_each_entry(dentry, dentry_list, extraction_list) {
1129 if (dentry_is_directory(dentry)) {
1130 ret = dentry_extract_skeleton(dentry, ctx);
1139 extract_dentries(struct list_head *dentry_list, struct apply_ctx *ctx)
1141 struct wim_dentry *dentry;
1144 list_for_each_entry(dentry, dentry_list, extraction_list) {
1145 ret = dentry_extract(dentry, ctx);
1153 extract_final_metadata(struct list_head *dentry_list, struct apply_ctx *ctx)
1155 struct wim_dentry *dentry;
1158 list_for_each_entry_reverse(dentry, dentry_list, extraction_list) {
1159 ret = dentry_extract_final(dentry, ctx);
1166 /* Creates a temporary file opened for writing. The open file descriptor is
1167 * returned in @fd_ret and its name is returned in @name_ret (dynamically
1170 create_temporary_file(struct filedes *fd_ret, tchar **name_ret)
1177 name = ttempnam(NULL, T("wimlib"));
1179 ERROR_WITH_ERRNO("Failed to create temporary filename");
1180 return WIMLIB_ERR_NOMEM;
1183 open_flags = O_WRONLY | O_CREAT | O_EXCL | O_BINARY;
1185 open_flags |= _O_SHORT_LIVED;
1187 raw_fd = topen(name, open_flags, 0600);
1190 if (errno == EEXIST) {
1194 ERROR_WITH_ERRNO("Failed to open temporary file \"%"TS"\"", name);
1196 return WIMLIB_ERR_OPEN;
1199 filedes_init(fd_ret, raw_fd);
1204 /* Extract all instances of the stream @lte that are being extracted in this
1205 * call of extract_tree(), but actually read the stream data from @lte_override.
1208 extract_stream_instances(struct wim_lookup_table_entry *lte,
1209 struct wim_lookup_table_entry *lte_override,
1210 struct apply_ctx *ctx)
1212 struct wim_dentry **lte_dentries;
1213 tchar path[ctx->ops->path_max];
1217 if (lte->out_refcnt <= ARRAY_LEN(lte->inline_lte_dentries))
1218 lte_dentries = lte->inline_lte_dentries;
1220 lte_dentries = lte->lte_dentries;
1222 for (i = 0; i < lte->out_refcnt; i++) {
1223 struct wim_dentry *dentry = lte_dentries[i];
1225 if (dentry->tmp_flag)
1227 if (!build_extraction_path(path, dentry, ctx))
1229 ret = extract_streams(path, ctx, dentry, lte, lte_override);
1231 goto out_clear_tmp_flags;
1232 dentry->tmp_flag = 1;
1235 out_clear_tmp_flags:
1236 for (i = 0; i < lte->out_refcnt; i++)
1237 lte_dentries[i]->tmp_flag = 0;
1241 /* Determine whether the specified stream needs to be extracted to a temporary
1244 * @lte->out_refcnt specifies the number of instances of this stream that must
1247 * @is_partial_res is %true if this stream is just one of multiple in a single
1248 * WIM resource being extracted. */
1250 need_tmpfile_to_extract(struct wim_lookup_table_entry *lte,
1251 bool is_partial_res)
1253 /* Temporary file is always required when reading a partial resource,
1254 * since in that case we retrieve all the contained streams in one pass.
1259 /* Otherwise we don't need a temporary file if only a single instance of
1260 * the stream is needed. */
1261 if (lte->out_refcnt == 1)
1264 wimlib_assert(lte->out_refcnt >= 2);
1266 /* We also don't need a temporary file if random access to the stream is
1268 if (lte->resource_location != RESOURCE_IN_WIM ||
1269 filedes_is_seekable(<e->rspec->wim->in_fd))
1276 begin_extract_stream_to_tmpfile(struct wim_lookup_table_entry *lte,
1277 bool is_partial_res,
1280 struct apply_ctx *ctx = _ctx;
1283 if (!need_tmpfile_to_extract(lte, is_partial_res)) {
1284 DEBUG("Temporary file not needed "
1285 "for stream (size=%"PRIu64")", lte->size);
1286 ret = extract_stream_instances(lte, lte, ctx);
1290 return BEGIN_STREAM_STATUS_SKIP_STREAM;
1293 DEBUG("Temporary file needed for stream (size=%"PRIu64")", lte->size);
1294 return create_temporary_file(&ctx->tmpfile_fd, &ctx->tmpfile_name);
1298 end_extract_stream_to_tmpfile(struct wim_lookup_table_entry *lte,
1299 int status, void *_ctx)
1301 struct apply_ctx *ctx = _ctx;
1302 struct wim_lookup_table_entry lte_override;
1304 int errno_save = errno;
1306 ret = filedes_close(&ctx->tmpfile_fd);
1311 goto out_delete_tmpfile;
1315 ERROR_WITH_ERRNO("Error writing temporary file %"TS, ctx->tmpfile_name);
1316 ret = WIMLIB_ERR_WRITE;
1317 goto out_delete_tmpfile;
1320 /* Now that a full stream has been extracted to a temporary file,
1321 * extract all instances of it to the actual target. */
1323 memcpy(<e_override, lte, sizeof(struct wim_lookup_table_entry));
1324 lte_override.resource_location = RESOURCE_IN_FILE_ON_DISK;
1325 lte_override.file_on_disk = ctx->tmpfile_name;
1327 ret = extract_stream_instances(lte, <e_override, ctx);
1331 tunlink(ctx->tmpfile_name);
1332 FREE(ctx->tmpfile_name);
1337 /* Extracts a list of streams (ctx.stream_list), assuming that the directory
1338 * structure and empty files were already created. This relies on the
1339 * per-`struct wim_lookup_table_entry' list of dentries that reference each
1340 * stream that was constructed earlier. */
1342 extract_stream_list(struct apply_ctx *ctx)
1344 struct read_stream_list_callbacks cbs = {
1345 .begin_stream = begin_extract_stream_to_tmpfile,
1346 .begin_stream_ctx = ctx,
1347 .consume_chunk = extract_chunk_to_fd,
1348 .consume_chunk_ctx = &ctx->tmpfile_fd,
1349 .end_stream = end_extract_stream_to_tmpfile,
1350 .end_stream_ctx = ctx,
1352 return read_stream_list(&ctx->stream_list,
1353 offsetof(struct wim_lookup_table_entry, extraction_list),
1354 &cbs, VERIFY_STREAM_HASHES);
1357 #define PWM_ALLOW_WIM_HDR 0x00001
1358 #define PWM_SILENT_EOF 0x00002
1360 /* Read the header from a stream in a pipable WIM. */
1362 read_pwm_stream_header(WIMStruct *pwm, struct wim_lookup_table_entry *lte,
1363 struct wim_resource_spec *rspec,
1364 int flags, struct wim_header_disk *hdr_ret)
1367 struct pwm_stream_hdr stream_hdr;
1368 struct wim_header_disk pwm_hdr;
1370 struct wim_reshdr reshdr;
1373 ret = full_read(&pwm->in_fd, &buf.stream_hdr, sizeof(buf.stream_hdr));
1377 if ((flags & PWM_ALLOW_WIM_HDR) && buf.stream_hdr.magic == PWM_MAGIC) {
1378 BUILD_BUG_ON(sizeof(buf.pwm_hdr) < sizeof(buf.stream_hdr));
1379 ret = full_read(&pwm->in_fd, &buf.stream_hdr + 1,
1380 sizeof(buf.pwm_hdr) - sizeof(buf.stream_hdr));
1384 lte->resource_location = RESOURCE_NONEXISTENT;
1385 memcpy(hdr_ret, &buf.pwm_hdr, sizeof(buf.pwm_hdr));
1389 if (le64_to_cpu(buf.stream_hdr.magic) != PWM_STREAM_MAGIC) {
1390 ERROR("Data read on pipe is invalid (expected stream header).");
1391 return WIMLIB_ERR_INVALID_PIPABLE_WIM;
1394 copy_hash(lte->hash, buf.stream_hdr.hash);
1396 reshdr.size_in_wim = 0;
1397 reshdr.flags = le32_to_cpu(buf.stream_hdr.flags);
1398 reshdr.offset_in_wim = pwm->in_fd.offset;
1399 reshdr.uncompressed_size = le64_to_cpu(buf.stream_hdr.uncompressed_size);
1400 wim_res_hdr_to_spec(&reshdr, pwm, rspec);
1401 lte_bind_wim_resource_spec(lte, rspec);
1402 lte->flags = rspec->flags;
1403 lte->size = rspec->uncompressed_size;
1404 lte->offset_in_res = 0;
1408 if (ret != WIMLIB_ERR_UNEXPECTED_END_OF_FILE || !(flags & PWM_SILENT_EOF))
1409 ERROR_WITH_ERRNO("Error reading pipable WIM from pipe");
1414 extract_streams_from_pipe(struct apply_ctx *ctx)
1416 struct wim_lookup_table_entry *found_lte;
1417 struct wim_resource_spec *rspec;
1418 struct wim_lookup_table_entry *needed_lte;
1419 struct wim_lookup_table *lookup_table;
1420 struct wim_header_disk pwm_hdr;
1424 ret = WIMLIB_ERR_NOMEM;
1425 found_lte = new_lookup_table_entry();
1426 if (found_lte == NULL)
1429 rspec = MALLOC(sizeof(struct wim_resource_spec));
1431 goto out_free_found_lte;
1433 lookup_table = ctx->wim->lookup_table;
1434 pwm_flags = PWM_ALLOW_WIM_HDR;
1435 if ((ctx->extract_flags & WIMLIB_EXTRACT_FLAG_RESUME))
1436 pwm_flags |= PWM_SILENT_EOF;
1437 memcpy(ctx->progress.extract.guid, ctx->wim->hdr.guid, WIM_GID_LEN);
1438 ctx->progress.extract.part_number = ctx->wim->hdr.part_number;
1439 ctx->progress.extract.total_parts = ctx->wim->hdr.total_parts;
1440 if (ctx->progress_func)
1441 ctx->progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN,
1443 while (ctx->num_streams_remaining) {
1444 if (found_lte->resource_location != RESOURCE_NONEXISTENT)
1445 lte_unbind_wim_resource_spec(found_lte);
1446 ret = read_pwm_stream_header(ctx->wim, found_lte, rspec,
1447 pwm_flags, &pwm_hdr);
1449 if (ret == WIMLIB_ERR_UNEXPECTED_END_OF_FILE &&
1450 (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_RESUME))
1454 goto out_free_found_lte;
1457 if ((found_lte->resource_location != RESOURCE_NONEXISTENT)
1458 && !(found_lte->flags & WIM_RESHDR_FLAG_METADATA)
1459 && (needed_lte = lookup_stream(lookup_table, found_lte->hash))
1460 && (needed_lte->out_refcnt))
1462 tchar *tmpfile_name = NULL;
1463 struct wim_lookup_table_entry *lte_override;
1464 struct wim_lookup_table_entry tmpfile_lte;
1466 needed_lte->offset_in_res = found_lte->offset_in_res;
1467 needed_lte->flags = found_lte->flags;
1468 needed_lte->size = found_lte->size;
1470 lte_unbind_wim_resource_spec(found_lte);
1471 lte_bind_wim_resource_spec(needed_lte, rspec);
1473 if (needed_lte->out_refcnt > 1) {
1475 struct filedes tmpfile_fd;
1477 /* Extract stream to temporary file. */
1478 ret = create_temporary_file(&tmpfile_fd, &tmpfile_name);
1480 lte_unbind_wim_resource_spec(needed_lte);
1481 goto out_free_found_lte;
1484 ret = extract_full_stream_to_fd(needed_lte,
1487 filedes_close(&tmpfile_fd);
1488 goto delete_tmpfile;
1491 if (filedes_close(&tmpfile_fd)) {
1492 ERROR_WITH_ERRNO("Error writing to temporary "
1493 "file \"%"TS"\"", tmpfile_name);
1494 ret = WIMLIB_ERR_WRITE;
1495 goto delete_tmpfile;
1497 memcpy(&tmpfile_lte, needed_lte,
1498 sizeof(struct wim_lookup_table_entry));
1499 tmpfile_lte.resource_location = RESOURCE_IN_FILE_ON_DISK;
1500 tmpfile_lte.file_on_disk = tmpfile_name;
1501 lte_override = &tmpfile_lte;
1503 lte_override = needed_lte;
1506 ret = extract_stream_instances(needed_lte, lte_override, ctx);
1508 lte_unbind_wim_resource_spec(needed_lte);
1510 tunlink(tmpfile_name);
1514 goto out_free_found_lte;
1515 ctx->num_streams_remaining--;
1516 } else if (found_lte->resource_location != RESOURCE_NONEXISTENT) {
1517 ret = skip_wim_stream(found_lte);
1519 goto out_free_found_lte;
1521 u16 part_number = le16_to_cpu(pwm_hdr.part_number);
1522 u16 total_parts = le16_to_cpu(pwm_hdr.total_parts);
1524 if (part_number != ctx->progress.extract.part_number ||
1525 total_parts != ctx->progress.extract.total_parts ||
1526 memcmp(pwm_hdr.guid, ctx->progress.extract.guid,
1529 ctx->progress.extract.part_number = part_number;
1530 ctx->progress.extract.total_parts = total_parts;
1531 memcpy(ctx->progress.extract.guid,
1532 pwm_hdr.guid, WIM_GID_LEN);
1533 if (ctx->progress_func) {
1535 WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN,
1544 if (found_lte->resource_location != RESOURCE_IN_WIM)
1546 free_lookup_table_entry(found_lte);
1555 /* Extract a WIM dentry to standard output.
1557 * This obviously doesn't make sense in all cases. We return an error if the
1558 * dentry does not correspond to a regular file. Otherwise we extract the
1559 * unnamed data stream only. */
1561 extract_dentry_to_stdout(struct wim_dentry *dentry,
1562 const struct wim_lookup_table *lookup_table)
1564 struct wim_inode *inode = dentry->d_inode;
1565 struct wim_lookup_table_entry *lte;
1566 struct filedes _stdout;
1568 if (inode->i_attributes & (FILE_ATTRIBUTE_REPARSE_POINT |
1569 FILE_ATTRIBUTE_DIRECTORY))
1571 ERROR("\"%"TS"\" is not a regular file and therefore cannot be "
1572 "extracted to standard output", dentry_full_path(dentry));
1573 return WIMLIB_ERR_NOT_A_REGULAR_FILE;
1576 lte = inode_unnamed_lte(inode, lookup_table);
1578 const u8 *hash = inode_unnamed_stream_hash(inode);
1579 if (!is_zero_hash(hash))
1580 return stream_not_found_error(inode, hash);
1584 filedes_init(&_stdout, STDOUT_FILENO);
1585 return extract_full_stream_to_fd(lte, &_stdout);
1589 extract_dentries_to_stdout(struct wim_dentry **dentries, size_t num_dentries,
1590 const struct wim_lookup_table *lookup_table)
1592 for (size_t i = 0; i < num_dentries; i++) {
1593 int ret = extract_dentry_to_stdout(dentries[i], lookup_table);
1600 /**********************************************************************/
1603 * Removes duplicate dentries from the array.
1605 * Returns the new number of dentries, packed at the front of the array.
1608 remove_duplicate_trees(struct wim_dentry **trees, size_t num_trees)
1611 for (i = 0; i < num_trees; i++) {
1612 if (!trees[i]->tmp_flag) {
1613 /* Found distinct dentry. */
1614 trees[i]->tmp_flag = 1;
1615 trees[j++] = trees[i];
1618 for (i = 0; i < j; i++)
1619 trees[i]->tmp_flag = 0;
1624 * Remove dentries that are descendants of other dentries in the array.
1626 * Returns the new number of dentries, packed at the front of the array.
1629 remove_contained_trees(struct wim_dentry **trees, size_t num_trees)
1632 for (i = 0; i < num_trees; i++)
1633 trees[i]->tmp_flag = 1;
1634 for (i = 0; i < num_trees; i++) {
1635 struct wim_dentry *d = trees[i];
1636 while (!dentry_is_root(d)) {
1639 goto tree_contained;
1641 trees[j++] = trees[i];
1645 trees[i]->tmp_flag = 0;
1648 for (i = 0; i < j; i++)
1649 trees[i]->tmp_flag = 0;
1654 dentry_append_to_list(struct wim_dentry *dentry, void *_dentry_list)
1656 struct list_head *dentry_list = _dentry_list;
1657 list_add_tail(&dentry->extraction_list, dentry_list);
1662 dentry_reset_extraction_list_node(struct wim_dentry *dentry)
1664 dentry->extraction_list = (struct list_head){NULL, NULL};
1668 dentry_delete_from_list(struct wim_dentry *dentry, void *_ignore)
1670 list_del(&dentry->extraction_list);
1671 dentry_reset_extraction_list_node(dentry);
1676 * Build the preliminary list of dentries to be extracted.
1678 * The list maintains the invariant that if d1 and d2 are in the list and d1 is
1679 * an ancestor of d2, then d1 appears before d2 in the list.
1682 build_dentry_list(struct list_head *dentry_list, struct wim_dentry **trees,
1683 size_t num_trees, bool add_ancestors)
1685 INIT_LIST_HEAD(dentry_list);
1687 /* Add the trees recursively. */
1688 for (size_t i = 0; i < num_trees; i++)
1689 for_dentry_in_tree(trees[i], dentry_append_to_list, dentry_list);
1691 /* If requested, add ancestors of the trees. */
1692 if (add_ancestors) {
1693 for (size_t i = 0; i < num_trees; i++) {
1694 struct wim_dentry *dentry = trees[i];
1695 struct wim_dentry *ancestor;
1696 struct list_head *place_after;
1698 if (dentry_is_root(dentry))
1701 place_after = dentry_list;
1704 ancestor = ancestor->parent;
1705 if (dentry_in_list(ancestor)) {
1706 place_after = &ancestor->extraction_list;
1709 } while (!dentry_is_root(ancestor));
1713 ancestor = ancestor->parent;
1714 if (dentry_in_list(ancestor))
1716 list_add(&ancestor->extraction_list, place_after);
1717 } while (!dentry_is_root(ancestor));
1722 static const struct apply_operations *
1723 select_apply_operations(int extract_flags)
1726 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS)
1727 return &ntfs_3g_apply_ops;
1730 return &win32_apply_ops;
1732 return &unix_apply_ops;
1737 static const utf16lechar replacement_char = cpu_to_le16(0xfffd);
1739 static const utf16lechar replacement_char = cpu_to_le16('?');
1743 file_name_valid(utf16lechar *name, size_t num_chars, bool fix)
1749 for (i = 0; i < num_chars; i++) {
1752 case cpu_to_le16('\\'):
1753 case cpu_to_le16(':'):
1754 case cpu_to_le16('*'):
1755 case cpu_to_le16('?'):
1756 case cpu_to_le16('"'):
1757 case cpu_to_le16('<'):
1758 case cpu_to_le16('>'):
1759 case cpu_to_le16('|'):
1761 case cpu_to_le16('/'):
1762 case cpu_to_le16('\0'):
1764 name[i] = replacement_char;
1771 if (name[num_chars - 1] == cpu_to_le16(' ') ||
1772 name[num_chars - 1] == cpu_to_le16('.'))
1775 name[num_chars - 1] = replacement_char;
1784 dentry_calculate_extraction_name(struct wim_dentry *dentry,
1785 struct apply_ctx *ctx)
1789 if (dentry == ctx->target_dentry)
1792 if (!dentry_is_supported(dentry, &ctx->supported_features))
1795 if (!ctx->ops->supports_case_sensitive_filenames)
1797 struct wim_dentry *other;
1798 list_for_each_entry(other, &dentry->d_ci_conflict_list,
1801 if (dentry_in_list(other)) {
1802 if (ctx->extract_flags &
1803 WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS) {
1804 WARNING("\"%"TS"\" has the same "
1805 "case-insensitive name as "
1806 "\"%"TS"\"; extracting "
1807 "dummy name instead",
1808 dentry_full_path(dentry),
1809 dentry_full_path(other));
1812 WARNING("Not extracting \"%"TS"\": "
1813 "has same case-insensitive "
1814 "name as \"%"TS"\"",
1815 dentry_full_path(dentry),
1816 dentry_full_path(other));
1823 if (file_name_valid(dentry->file_name, dentry->file_name_nbytes / 2, false)) {
1824 #if TCHAR_IS_UTF16LE
1825 dentry->extraction_name = dentry->file_name;
1826 dentry->extraction_name_nchars = dentry->file_name_nbytes / 2;
1829 return utf16le_to_tstr(dentry->file_name,
1830 dentry->file_name_nbytes,
1831 &dentry->extraction_name,
1832 &dentry->extraction_name_nchars);
1835 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES)
1837 WARNING("\"%"TS"\" has an invalid filename "
1838 "that is not supported on this platform; "
1839 "extracting dummy name instead",
1840 dentry_full_path(dentry));
1843 WARNING("Not extracting \"%"TS"\": has an invalid filename "
1844 "that is not supported on this platform",
1845 dentry_full_path(dentry));
1852 utf16lechar utf16_name_copy[dentry->file_name_nbytes / 2];
1854 memcpy(utf16_name_copy, dentry->file_name, dentry->file_name_nbytes);
1855 file_name_valid(utf16_name_copy, dentry->file_name_nbytes / 2, true);
1858 size_t tchar_nchars;
1859 #if TCHAR_IS_UTF16LE
1860 tchar_name = utf16_name_copy;
1861 tchar_nchars = dentry->file_name_nbytes / 2;
1863 ret = utf16le_to_tstr(utf16_name_copy,
1864 dentry->file_name_nbytes,
1865 &tchar_name, &tchar_nchars);
1869 size_t fixed_name_num_chars = tchar_nchars;
1870 tchar fixed_name[tchar_nchars + 50];
1872 tmemcpy(fixed_name, tchar_name, tchar_nchars);
1873 fixed_name_num_chars += tsprintf(fixed_name + tchar_nchars,
1874 T(" (invalid filename #%lu)"),
1875 ++ctx->invalid_sequence);
1876 #if !TCHAR_IS_UTF16LE
1879 dentry->extraction_name = memdup(fixed_name,
1880 2 * fixed_name_num_chars + 2);
1881 if (!dentry->extraction_name)
1882 return WIMLIB_ERR_NOMEM;
1883 dentry->extraction_name_nchars = fixed_name_num_chars;
1888 for_dentry_in_tree(dentry, dentry_delete_from_list, NULL);
1893 * Calculate the actual filename component at which each WIM dentry will be
1894 * extracted, with special handling for dentries that are unsupported by the
1895 * extraction backend or have invalid names.
1897 * Note: this has a dependency on start_extract() being called because
1898 * ctx.supported_features must be filled in in order to determine whether each
1899 * dentry is supported.
1901 * Possible error codes: WIMLIB_ERR_NOMEM, WIMLIB_ERR_INVALID_UTF16_STRING
1904 dentry_list_calculate_extraction_names(struct list_head *dentry_list,
1905 struct apply_ctx *ctx)
1907 struct list_head *prev, *cur;
1909 /* Can't use list_for_each_entry() because a call to
1910 * dentry_calculate_extraction_name() may delete the current dentry and
1911 * its children from the list. */
1915 struct wim_dentry *dentry;
1919 if (cur == dentry_list)
1922 dentry = list_entry(cur, struct wim_dentry, extraction_list);
1924 ret = dentry_calculate_extraction_name(dentry, ctx);
1928 if (prev->next == cur)
1931 ; /* Current dentry and its children (which follow in
1932 the list) were deleted. prev stays the same. */
1938 dentry_resolve_streams(struct wim_dentry *dentry, int extract_flags,
1939 struct wim_lookup_table *lookup_table)
1941 struct wim_inode *inode = dentry->d_inode;
1942 struct wim_lookup_table_entry *lte;
1946 /* Special case: when extracting from a pipe, the WIM lookup table is
1947 * initially empty, so "resolving" an inode's streams is initially not
1948 * possible. However, we still need to keep track of which streams,
1949 * identified by SHA1 message digests, need to be extracted, so we
1950 * "resolve" the inode's streams anyway by allocating new entries. */
1951 if (extract_flags & WIMLIB_EXTRACT_FLAG_FROM_PIPE)
1953 ret = inode_resolve_streams(inode, lookup_table, force);
1956 for (u32 i = 0; i <= inode->i_num_ads; i++) {
1957 lte = inode_stream_lte_resolved(inode, i);
1959 lte->out_refcnt = 0;
1965 * For each dentry to be extracted, resolve all streams in the corresponding
1966 * inode and set 'out_refcnt' in each to 0.
1968 * Possible error codes: WIMLIB_ERR_RESOURCE_NOT_FOUND, WIMLIB_ERR_NOMEM.
1971 dentry_list_resolve_streams(struct list_head *dentry_list,
1972 struct apply_ctx *ctx)
1974 struct wim_dentry *dentry;
1977 list_for_each_entry(dentry, dentry_list, extraction_list) {
1978 ret = dentry_resolve_streams(dentry,
1980 ctx->wim->lookup_table);
1988 ref_stream(struct wim_lookup_table_entry *lte,
1989 struct wim_dentry *dentry, struct apply_ctx *ctx)
1994 /* Tally the size only for each extraction of the stream (not hard
1996 if (!(dentry->d_inode->i_visited &&
1997 ctx->supported_features.hard_links) &&
1998 (!is_linked_extraction(ctx) || (lte->out_refcnt == 0 &&
1999 lte->extracted_file == NULL)))
2001 ctx->progress.extract.total_bytes += lte->size;
2002 ctx->progress.extract.num_streams++;
2005 /* Add stream to the dentry_list only one time, even if it's going
2006 * to be extracted to multiple locations. */
2007 if (lte->out_refcnt == 0) {
2008 list_add_tail(<e->extraction_list, &ctx->stream_list);
2009 ctx->num_streams_remaining++;
2012 if (!(ctx->extract_flags & WIMLIB_EXTRACT_FLAG_FILE_ORDER)) {
2013 struct wim_dentry **lte_dentries;
2015 /* Append dentry to this stream's array of dentries referencing
2016 * it. Use inline array to avoid memory allocation until the
2017 * number of dentries becomes too large. */
2018 if (lte->out_refcnt < ARRAY_LEN(lte->inline_lte_dentries)) {
2019 lte_dentries = lte->inline_lte_dentries;
2021 struct wim_dentry **prev_lte_dentries;
2022 size_t alloc_lte_dentries;
2024 if (lte->out_refcnt == ARRAY_LEN(lte->inline_lte_dentries)) {
2025 prev_lte_dentries = NULL;
2026 alloc_lte_dentries = ARRAY_LEN(lte->inline_lte_dentries);
2028 prev_lte_dentries = lte->lte_dentries;
2029 alloc_lte_dentries = lte->alloc_lte_dentries;
2032 if (lte->out_refcnt == alloc_lte_dentries) {
2033 alloc_lte_dentries *= 2;
2034 lte_dentries = REALLOC(prev_lte_dentries,
2035 alloc_lte_dentries *
2036 sizeof(lte_dentries[0]));
2037 if (lte_dentries == NULL)
2038 return WIMLIB_ERR_NOMEM;
2039 if (prev_lte_dentries == NULL) {
2040 memcpy(lte_dentries,
2041 lte->inline_lte_dentries,
2042 sizeof(lte->inline_lte_dentries));
2044 lte->lte_dentries = lte_dentries;
2045 lte->alloc_lte_dentries = alloc_lte_dentries;
2047 lte_dentries = lte->lte_dentries;
2049 lte_dentries[lte->out_refcnt] = dentry;
2056 dentry_ref_streams(struct wim_dentry *dentry, struct apply_ctx *ctx)
2058 struct wim_inode *inode = dentry->d_inode;
2061 /* The unnamed data stream will always be extracted, except in an
2063 if (!inode_is_encrypted_directory(inode)) {
2064 ret = ref_stream(inode_unnamed_lte_resolved(inode),
2070 /* Named data streams will be extracted only if supported in the current
2071 * extraction mode and volume, and to avoid complications, if not doing
2072 * a linked extraction. */
2073 if (can_extract_named_data_streams(ctx)) {
2074 for (u16 i = 0; i < inode->i_num_ads; i++) {
2075 if (!ads_entry_is_named_stream(&inode->i_ads_entries[i]))
2077 ret = ref_stream(inode->i_ads_entries[i].lte,
2083 inode->i_visited = 1;
2088 * For each dentry to be extracted, iterate through the data streams of the
2089 * corresponding inode. For each such stream that is not to be ignored due to
2090 * the supported features or extraction flags, add it to the list of streams to
2091 * be extracted (ctx->stream_list) if not already done so.
2093 * Also, if doing a sequential extraction, build a mapping from each stream to
2094 * the dentries referencing it.
2096 * This also initializes the extract progress info with byte and stream
2099 * Note: This has a dependency on start_extract being called because
2100 * ctx.supported_features must be filled in in order to determine whether named
2101 * data streams are supported.
2103 * Note: this uses the i_visited member of the inodes (assumed to be 0
2104 * initially), but does not reset it.
2106 * Possible error codes: WIMLIB_ERR_NOMEM.
2109 dentry_list_ref_streams(struct list_head *dentry_list, struct apply_ctx *ctx)
2111 struct wim_dentry *dentry;
2114 list_for_each_entry(dentry, dentry_list, extraction_list) {
2115 ret = dentry_ref_streams(dentry, ctx);
2122 /* Tally features necessary to extract a dentry and the corresponding inode. */
2124 dentry_tally_features(struct wim_dentry *dentry, struct wim_features *features)
2126 struct wim_inode *inode = dentry->d_inode;
2128 if (inode->i_attributes & FILE_ATTRIBUTE_ARCHIVE)
2129 features->archive_files++;
2130 if (inode->i_attributes & FILE_ATTRIBUTE_HIDDEN)
2131 features->hidden_files++;
2132 if (inode->i_attributes & FILE_ATTRIBUTE_SYSTEM)
2133 features->system_files++;
2134 if (inode->i_attributes & FILE_ATTRIBUTE_COMPRESSED)
2135 features->compressed_files++;
2136 if (inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED) {
2137 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)
2138 features->encrypted_directories++;
2140 features->encrypted_files++;
2142 if (inode->i_attributes & FILE_ATTRIBUTE_NOT_CONTENT_INDEXED)
2143 features->not_context_indexed_files++;
2144 if (inode->i_attributes & FILE_ATTRIBUTE_SPARSE_FILE)
2145 features->sparse_files++;
2146 if (inode_has_named_stream(inode))
2147 features->named_data_streams++;
2148 if (inode->i_visited)
2149 features->hard_links++;
2150 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
2151 features->reparse_points++;
2152 if (inode_is_symlink(inode))
2153 features->symlink_reparse_points++;
2155 features->other_reparse_points++;
2157 if (inode->i_security_id != -1)
2158 features->security_descriptors++;
2159 if (dentry->short_name_nbytes)
2160 features->short_names++;
2161 if (inode_has_unix_data(inode))
2162 features->unix_data++;
2163 inode->i_visited = 1;
2166 /* Tally the features necessary to extract the specified dentries. */
2168 dentry_list_get_features(struct list_head *dentry_list,
2169 struct wim_features *features)
2171 struct wim_dentry *dentry;
2173 memset(features, 0, sizeof(struct wim_features));
2175 list_for_each_entry(dentry, dentry_list, extraction_list)
2176 dentry_tally_features(dentry, features);
2178 list_for_each_entry(dentry, dentry_list, extraction_list)
2179 dentry->d_inode->i_visited = 0;
2183 compute_supported_attributes_mask(const struct wim_features *supported_features)
2185 u32 mask = (u32)~0UL;
2187 if (!supported_features->archive_files)
2188 mask &= ~FILE_ATTRIBUTE_ARCHIVE;
2190 if (!supported_features->hidden_files)
2191 mask &= ~FILE_ATTRIBUTE_HIDDEN;
2193 if (!supported_features->system_files)
2194 mask &= ~FILE_ATTRIBUTE_SYSTEM;
2196 if (!supported_features->not_context_indexed_files)
2197 mask &= ~FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
2199 if (!supported_features->compressed_files)
2200 mask &= ~FILE_ATTRIBUTE_COMPRESSED;
2202 if (!supported_features->sparse_files)
2203 mask &= ~FILE_ATTRIBUTE_SPARSE_FILE;
2205 if (!supported_features->reparse_points)
2206 mask &= ~FILE_ATTRIBUTE_REPARSE_POINT;
2212 do_feature_check(const struct wim_features *required_features,
2213 const struct wim_features *supported_features,
2214 int extract_flags, const struct apply_operations *ops)
2216 /* File attributes. */
2217 if (!(extract_flags & WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES)) {
2218 /* Note: Don't bother the user about FILE_ATTRIBUTE_ARCHIVE.
2219 * We're an archive program, so theoretically we can do what we
2222 if (required_features->hidden_files &&
2223 !supported_features->hidden_files)
2224 WARNING("Ignoring FILE_ATTRIBUTE_HIDDEN of %lu files",
2225 required_features->hidden_files);
2227 if (required_features->system_files &&
2228 !supported_features->system_files)
2229 WARNING("Ignoring FILE_ATTRIBUTE_SYSTEM of %lu files",
2230 required_features->system_files);
2232 if (required_features->compressed_files &&
2233 !supported_features->compressed_files)
2234 WARNING("Ignoring FILE_ATTRIBUTE_COMPRESSED of %lu files",
2235 required_features->compressed_files);
2237 if (required_features->not_context_indexed_files &&
2238 !supported_features->not_context_indexed_files)
2239 WARNING("Ignoring FILE_ATTRIBUTE_NOT_CONTENT_INDEXED of %lu files",
2240 required_features->not_context_indexed_files);
2242 if (required_features->sparse_files &&
2243 !supported_features->sparse_files)
2244 WARNING("Ignoring FILE_ATTRIBUTE_SPARSE_FILE of %lu files",
2245 required_features->sparse_files);
2247 if (required_features->encrypted_directories &&
2248 !supported_features->encrypted_directories)
2249 WARNING("Ignoring FILE_ATTRIBUTE_ENCRYPTED of %lu directories",
2250 required_features->encrypted_directories);
2253 /* Encrypted files. */
2254 if (required_features->encrypted_files &&
2255 !supported_features->encrypted_files)
2256 WARNING("Ignoring %lu encrypted files",
2257 required_features->encrypted_files);
2259 /* Named data streams. */
2260 if (required_features->named_data_streams &&
2261 (!supported_features->named_data_streams ||
2262 (extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
2263 WIMLIB_EXTRACT_FLAG_HARDLINK))))
2264 WARNING("Ignoring named data streams of %lu files",
2265 required_features->named_data_streams);
2268 if ((extract_flags & WIMLIB_EXTRACT_FLAG_HARDLINK) &&
2269 !supported_features->hard_links)
2271 ERROR("Extraction backend does not support hard links!");
2272 return WIMLIB_ERR_UNSUPPORTED;
2274 if (required_features->hard_links && !supported_features->hard_links)
2275 WARNING("Extracting %lu hard links as independent files",
2276 required_features->hard_links);
2278 /* Symbolic links and reparse points. */
2279 if ((extract_flags & WIMLIB_EXTRACT_FLAG_SYMLINK) &&
2280 !supported_features->symlink_reparse_points)
2282 ERROR("Extraction backend does not support symbolic links!");
2283 return WIMLIB_ERR_UNSUPPORTED;
2285 if ((extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS) &&
2286 required_features->symlink_reparse_points &&
2287 !supported_features->symlink_reparse_points &&
2288 !supported_features->reparse_points)
2290 ERROR("Extraction backend does not support symbolic links!");
2291 return WIMLIB_ERR_UNSUPPORTED;
2293 if (required_features->reparse_points &&
2294 !supported_features->reparse_points)
2296 if (supported_features->symlink_reparse_points) {
2297 if (required_features->other_reparse_points) {
2298 WARNING("Ignoring %lu non-symlink/junction "
2299 "reparse point files",
2300 required_features->other_reparse_points);
2303 WARNING("Ignoring %lu reparse point files",
2304 required_features->reparse_points);
2308 /* Security descriptors. */
2309 if (((extract_flags & (WIMLIB_EXTRACT_FLAG_STRICT_ACLS |
2310 WIMLIB_EXTRACT_FLAG_UNIX_DATA))
2311 == WIMLIB_EXTRACT_FLAG_STRICT_ACLS) &&
2312 required_features->security_descriptors &&
2313 !supported_features->security_descriptors)
2315 ERROR("Extraction backend does not support security descriptors!");
2316 return WIMLIB_ERR_UNSUPPORTED;
2318 if (!(extract_flags & WIMLIB_EXTRACT_FLAG_NO_ACLS) &&
2319 required_features->security_descriptors &&
2320 !supported_features->security_descriptors)
2321 WARNING("Ignoring Windows NT security descriptors of %lu files",
2322 required_features->security_descriptors);
2325 if ((extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) &&
2326 required_features->unix_data && !supported_features->unix_data)
2328 ERROR("Extraction backend does not support UNIX data!");
2329 return WIMLIB_ERR_UNSUPPORTED;
2333 if (required_features->short_names &&
2334 !supported_features->short_names)
2336 if (extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_SHORT_NAMES) {
2337 ERROR("Extraction backend does not support DOS names!");
2338 return WIMLIB_ERR_UNSUPPORTED;
2340 WARNING("Ignoring DOS names of %lu files",
2341 required_features->short_names);
2345 if ((extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_TIMESTAMPS) &&
2346 !ops->set_timestamps)
2348 ERROR("Extraction backend does not support timestamps!");
2349 return WIMLIB_ERR_UNSUPPORTED;
2356 do_extract_warnings(struct apply_ctx *ctx)
2358 if (ctx->partial_security_descriptors == 0 &&
2359 ctx->no_security_descriptors == 0)
2362 WARNING("Extraction to \"%"TS"\" complete, but with one or more warnings:",
2364 if (ctx->partial_security_descriptors != 0) {
2365 WARNING("- Could only partially set the security descriptor\n"
2366 " on %lu files or directories.",
2367 ctx->partial_security_descriptors);
2369 if (ctx->no_security_descriptors != 0) {
2370 WARNING("- Could not set security descriptor at all\n"
2371 " on %lu files or directories.",
2372 ctx->no_security_descriptors);
2375 WARNING("To fully restore all security descriptors, run the program\n"
2376 " with Administrator rights.");
2381 destroy_dentry_list(struct list_head *dentry_list)
2383 struct wim_dentry *dentry, *tmp;
2384 struct wim_inode *inode;
2386 list_for_each_entry_safe(dentry, tmp, dentry_list, extraction_list) {
2387 inode = dentry->d_inode;
2388 dentry_reset_extraction_list_node(dentry);
2389 dentry->was_linked = 0;
2390 dentry->skeleton_extracted = 0;
2391 inode->i_visited = 0;
2392 FREE(inode->i_extracted_file);
2393 inode->i_extracted_file = NULL;
2394 inode->i_dos_name_extracted = 0;
2395 if ((void*)dentry->extraction_name != (void*)dentry->file_name)
2396 FREE(dentry->extraction_name);
2397 dentry->extraction_name = NULL;
2402 destroy_stream_list(struct list_head *stream_list)
2404 struct wim_lookup_table_entry *lte;
2406 list_for_each_entry(lte, stream_list, extraction_list)
2407 if (lte->out_refcnt > ARRAY_LEN(lte->inline_lte_dentries))
2408 FREE(lte->lte_dentries);
2412 extract_trees(WIMStruct *wim, struct wim_dentry **trees, size_t num_trees,
2413 const tchar *target, int extract_flags,
2414 wimlib_progress_func_t progress_func)
2417 struct apply_ctx ctx;
2418 struct list_head dentry_list;
2419 struct wim_features required_features;
2421 /* Handle stdout extraction as a separate case. */
2422 if (extract_flags & WIMLIB_EXTRACT_FLAG_TO_STDOUT)
2423 return extract_dentries_to_stdout(trees, num_trees,
2426 /* Start initializing the apply_ctx. */
2427 memset(&ctx, 0, sizeof(struct apply_ctx));
2429 ctx.extract_flags = extract_flags;
2430 ctx.target = target;
2431 ctx.target_nchars = tstrlen(target);
2432 ctx.progress_func = progress_func;
2433 if (progress_func) {
2434 ctx.progress.extract.wimfile_name = wim->filename;
2435 ctx.progress.extract.image = wim->current_image;
2436 ctx.progress.extract.extract_flags = (extract_flags &
2437 WIMLIB_EXTRACT_MASK_PUBLIC);
2438 ctx.progress.extract.image_name = wimlib_get_image_name(wim,
2439 wim->current_image);
2440 ctx.progress.extract.target = target;
2443 ctx.target_dentry = wim_root_dentry(wim);
2444 /* Note: ctx.target_dentry represents the dentry that gets extracted to
2445 * @target. There may be none, in which case it gets set to the image
2446 * root and never matches any of the dentries actually being extracted.
2449 num_trees = remove_duplicate_trees(trees, num_trees);
2451 /* All trees are now distinct. */
2453 num_trees = remove_contained_trees(trees, num_trees);
2455 /* All trees are now distinct and non-overlapping. */
2457 /* Build list of dentries to be extracted. */
2458 build_dentry_list(&dentry_list, trees, num_trees,
2459 !(extract_flags & WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE));
2461 /* Select the appropriate apply_operations based on the platform and
2463 ctx.ops = select_apply_operations(extract_flags);
2465 /* Figure out whether the root dentry is being extracted to the root of
2466 * a volume and therefore needs to be treated "specially", for example
2467 * not being explicitly created and not having attributes set. */
2468 if (ctx.ops->target_is_root && ctx.ops->root_directory_is_special)
2469 ctx.root_dentry_is_special = ctx.ops->target_is_root(target);
2471 /* Call the start_extract() callback. This gives the apply_operations
2472 * implementation a chance to do any setup needed to access the volume.
2473 * Furthermore, start_extract() is expected to set the supported
2474 * features of this extraction mode (ctx.supported_features), which are
2475 * determined at runtime as they may vary depending on the actual
2477 ret = ctx.ops->start_extract(target, &ctx);
2479 goto out_destroy_dentry_list;
2481 /* Get and check the features required to extract the dentries. */
2482 dentry_list_get_features(&dentry_list, &required_features);
2483 ret = do_feature_check(&required_features, &ctx.supported_features,
2484 extract_flags, ctx.ops);
2486 goto out_finish_or_abort_extract;
2488 ctx.supported_attributes_mask =
2489 compute_supported_attributes_mask(&ctx.supported_features);
2491 /* Calculate extraction name for each dentry and remove subtrees that
2492 * can't be extracted due to naming problems. */
2493 ret = dentry_list_calculate_extraction_names(&dentry_list, &ctx);
2495 goto out_finish_or_abort_extract;
2497 /* Build list of streams to extract. */
2498 ret = dentry_list_resolve_streams(&dentry_list, &ctx);
2500 goto out_finish_or_abort_extract;
2501 INIT_LIST_HEAD(&ctx.stream_list);
2502 ret = dentry_list_ref_streams(&dentry_list, &ctx);
2504 goto out_destroy_stream_list;
2506 if (extract_flags & WIMLIB_EXTRACT_FLAG_FROM_PIPE) {
2507 /* When extracting from a pipe, the number of bytes of data to
2508 * extract can't be determined in the normal way (examining the
2509 * lookup table), since at this point all we have is a set of
2510 * SHA1 message digests of streams that need to be extracted.
2511 * However, we can get a reasonably accurate estimate by taking
2512 * <TOTALBYTES> from the corresponding <IMAGE> in the WIM XML
2513 * data. This does assume that a full image is being extracted,
2514 * but currently there is no API for doing otherwise. (Also,
2515 * subtract <HARDLINKBYTES> from this if hard links are
2516 * supported by the extraction mode.) */
2517 ctx.progress.extract.total_bytes =
2518 wim_info_get_image_total_bytes(wim->wim_info,
2519 wim->current_image);
2520 if (ctx.supported_features.hard_links) {
2521 ctx.progress.extract.total_bytes -=
2522 wim_info_get_image_hard_link_bytes(wim->wim_info,
2523 wim->current_image);
2527 if (ctx.ops->realpath_works_on_nonexisting_files &&
2528 ((extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX) ||
2529 ctx.ops->requires_realtarget_in_paths))
2531 ctx.realtarget = realpath(target, NULL);
2532 if (!ctx.realtarget) {
2533 ret = WIMLIB_ERR_NOMEM;
2534 goto out_destroy_stream_list;
2536 ctx.realtarget_nchars = tstrlen(ctx.realtarget);
2538 /* Strip trailing slashes. If we don't do this, we may create a
2539 * path with multiple consecutive backslashes, which for some
2540 * reason causes Windows to report that the file cannot be found.
2542 while (ctx.realtarget_nchars >= 2
2543 && ctx.realtarget[ctx.realtarget_nchars - 1] == L'\\'
2544 && ctx.realtarget[ctx.realtarget_nchars - 2] != L':')
2546 ctx.realtarget[--ctx.realtarget_nchars] = L'\0';
2551 if (progress_func) {
2553 if (extract_flags & WIMLIB_EXTRACT_FLAG_IMAGEMODE)
2554 msg = WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN;
2556 msg = WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN;
2557 progress_func(msg, &ctx.progress);
2560 if (!ctx.root_dentry_is_special) {
2561 tchar path[ctx.ops->path_max];
2562 if (build_extraction_path(path, ctx.target_dentry, &ctx)) {
2563 ret = extract_inode(path, &ctx, ctx.target_dentry->d_inode);
2565 goto out_free_realtarget;
2569 /* If we need to fix up the targets of absolute symbolic links
2570 * (WIMLIB_EXTRACT_FLAG_RPFIX) or the extraction mode requires paths to
2571 * be absolute, use realpath() (or its replacement on Windows) to get
2572 * the absolute path to the extraction target. Note that this requires
2573 * the target directory to exist, unless
2574 * realpath_works_on_nonexisting_files is set in the apply_operations.
2576 if (!ctx.realtarget &&
2577 (((extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX) &&
2578 required_features.symlink_reparse_points) ||
2579 ctx.ops->requires_realtarget_in_paths))
2581 ctx.realtarget = realpath(target, NULL);
2582 if (!ctx.realtarget) {
2583 ret = WIMLIB_ERR_NOMEM;
2584 goto out_free_realtarget;
2586 ctx.realtarget_nchars = tstrlen(ctx.realtarget);
2589 if (ctx.ops->requires_short_name_reordering) {
2591 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN,
2593 ret = extract_dir_structure(&dentry_list, &ctx);
2595 goto out_free_realtarget;
2598 /* Finally, the important part: extract the tree of files. */
2599 if (!(extract_flags & WIMLIB_EXTRACT_FLAG_FILE_ORDER)) {
2600 /* Sequential extraction requested, so two passes are needed
2601 * (one for file structure, one for streams.) */
2602 if (progress_func && !ctx.ops->requires_short_name_reordering)
2603 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN,
2606 if (!(extract_flags & WIMLIB_EXTRACT_FLAG_RESUME)) {
2607 ret = extract_structure(&dentry_list, &ctx);
2609 goto out_free_realtarget;
2612 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_END,
2614 if (extract_flags & WIMLIB_EXTRACT_FLAG_FROM_PIPE)
2615 ret = extract_streams_from_pipe(&ctx);
2617 ret = extract_stream_list(&ctx);
2619 goto out_free_realtarget;
2621 /* Sequential extraction was not requested, so we can make do
2622 * with one pass where we both create the files and extract
2624 if (progress_func && !ctx.ops->requires_short_name_reordering)
2625 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN,
2627 ret = extract_dentries(&dentry_list, &ctx);
2629 goto out_free_realtarget;
2631 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_END,
2635 /* If the total number of bytes to extract was miscalculated, just jump
2636 * to the calculated number in order to avoid confusing the progress
2637 * function. This should only occur when extracting from a pipe. */
2638 if (ctx.progress.extract.completed_bytes != ctx.progress.extract.total_bytes)
2640 DEBUG("Calculated %"PRIu64" bytes to extract, but actually "
2641 "extracted %"PRIu64,
2642 ctx.progress.extract.total_bytes,
2643 ctx.progress.extract.completed_bytes);
2645 if (progress_func &&
2646 ctx.progress.extract.completed_bytes < ctx.progress.extract.total_bytes)
2648 ctx.progress.extract.completed_bytes = ctx.progress.extract.total_bytes;
2649 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS, &ctx.progress);
2652 /* Apply security descriptors and timestamps. This is done at the end,
2653 * and in a depth-first manner, to prevent timestamps from getting
2654 * changed by subsequent extract operations and to minimize the chance
2655 * of the restored security descriptors getting in our way. */
2657 progress_func(WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS,
2659 ret = extract_final_metadata(&dentry_list, &ctx);
2661 goto out_free_realtarget;
2663 if (progress_func) {
2665 if (extract_flags & WIMLIB_EXTRACT_FLAG_IMAGEMODE)
2666 msg = WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END;
2668 msg = WIMLIB_PROGRESS_MSG_EXTRACT_TREE_END;
2669 progress_func(msg, &ctx.progress);
2671 do_extract_warnings(&ctx);
2673 out_free_realtarget:
2674 FREE(ctx.realtarget);
2675 out_destroy_stream_list:
2676 if (!(ctx.extract_flags & WIMLIB_EXTRACT_FLAG_FILE_ORDER))
2677 destroy_stream_list(&ctx.stream_list);
2678 out_finish_or_abort_extract:
2680 if (ctx.ops->abort_extract)
2681 ctx.ops->abort_extract(&ctx);
2683 if (ctx.ops->finish_extract)
2684 ret = ctx.ops->finish_extract(&ctx);
2686 out_destroy_dentry_list:
2687 destroy_dentry_list(&dentry_list);
2692 mkdir_if_needed(const tchar *target)
2695 if (tstat(target, &stbuf)) {
2696 if (errno == ENOENT) {
2697 if (tmkdir(target, 0755)) {
2698 ERROR_WITH_ERRNO("Failed to create directory "
2699 "\"%"TS"\"", target);
2700 return WIMLIB_ERR_MKDIR;
2703 ERROR_WITH_ERRNO("Failed to stat \"%"TS"\"", target);
2704 return WIMLIB_ERR_STAT;
2706 } else if (!S_ISDIR(stbuf.st_mode)) {
2707 ERROR("\"%"TS"\" is not a directory", target);
2708 return WIMLIB_ERR_NOTDIR;
2713 /* Make sure the extraction flags make sense, and update them if needed. */
2715 check_extract_flags(const WIMStruct *wim, int *extract_flags_p)
2717 int extract_flags = *extract_flags_p;
2719 /* Check for invalid flag combinations */
2720 if ((extract_flags &
2721 (WIMLIB_EXTRACT_FLAG_SYMLINK |
2722 WIMLIB_EXTRACT_FLAG_HARDLINK)) == (WIMLIB_EXTRACT_FLAG_SYMLINK |
2723 WIMLIB_EXTRACT_FLAG_HARDLINK))
2724 return WIMLIB_ERR_INVALID_PARAM;
2726 if ((extract_flags &
2727 (WIMLIB_EXTRACT_FLAG_NO_ACLS |
2728 WIMLIB_EXTRACT_FLAG_STRICT_ACLS)) == (WIMLIB_EXTRACT_FLAG_NO_ACLS |
2729 WIMLIB_EXTRACT_FLAG_STRICT_ACLS))
2730 return WIMLIB_ERR_INVALID_PARAM;
2732 if ((extract_flags &
2733 (WIMLIB_EXTRACT_FLAG_RPFIX |
2734 WIMLIB_EXTRACT_FLAG_NORPFIX)) == (WIMLIB_EXTRACT_FLAG_RPFIX |
2735 WIMLIB_EXTRACT_FLAG_NORPFIX))
2736 return WIMLIB_ERR_INVALID_PARAM;
2738 if ((extract_flags &
2739 (WIMLIB_EXTRACT_FLAG_RESUME |
2740 WIMLIB_EXTRACT_FLAG_FROM_PIPE)) == WIMLIB_EXTRACT_FLAG_RESUME)
2741 return WIMLIB_ERR_INVALID_PARAM;
2743 #ifndef WITH_NTFS_3G
2744 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
2745 ERROR("wimlib was compiled without support for NTFS-3g, so\n"
2746 " it cannot apply a WIM image directly to a NTFS volume.");
2747 return WIMLIB_ERR_UNSUPPORTED;
2752 if (extract_flags & WIMLIB_EXTRACT_FLAG_WIMBOOT) {
2753 ERROR("WIMBoot extraction is only supported on Windows!");
2754 return WIMLIB_ERR_UNSUPPORTED;
2758 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_RPFIX |
2759 WIMLIB_EXTRACT_FLAG_NORPFIX |
2760 WIMLIB_EXTRACT_FLAG_IMAGEMODE)) ==
2761 WIMLIB_EXTRACT_FLAG_IMAGEMODE)
2763 /* For full-image extraction, do reparse point fixups by default
2764 * if the WIM header says they are enabled. */
2765 if (wim->hdr.flags & WIM_HDR_FLAG_RP_FIX)
2766 extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
2769 /* TODO: Since UNIX data entries are stored in the file resources, in a
2770 * completely sequential extraction they may come up before the
2771 * corresponding file or symbolic link data. This needs to be handled
2773 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_UNIX_DATA |
2774 WIMLIB_EXTRACT_FLAG_FILE_ORDER))
2775 == WIMLIB_EXTRACT_FLAG_UNIX_DATA)
2777 if (extract_flags & WIMLIB_EXTRACT_FLAG_FROM_PIPE) {
2778 WARNING("Setting UNIX file/owner group may "
2779 "be impossible on some\n"
2781 "when applying from a pipe.");
2783 extract_flags |= WIMLIB_EXTRACT_FLAG_FILE_ORDER;
2784 WARNING("Disabling sequential extraction for "
2789 *extract_flags_p = extract_flags;
2794 get_wildcard_flags(int extract_flags)
2796 u32 wildcard_flags = 0;
2798 if (extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_GLOB)
2799 wildcard_flags |= WILDCARD_FLAG_ERROR_IF_NO_MATCH;
2801 wildcard_flags |= WILDCARD_FLAG_WARN_IF_NO_MATCH;
2803 if (default_ignore_case)
2804 wildcard_flags |= WILDCARD_FLAG_CASE_INSENSITIVE;
2806 return wildcard_flags;
2809 struct append_dentry_ctx {
2810 struct wim_dentry **dentries;
2811 size_t num_dentries;
2812 size_t num_alloc_dentries;
2816 append_dentry_cb(struct wim_dentry *dentry, void *_ctx)
2818 struct append_dentry_ctx *ctx = _ctx;
2820 if (ctx->num_dentries == ctx->num_alloc_dentries) {
2821 struct wim_dentry **new_dentries;
2824 new_length = max(ctx->num_alloc_dentries + 8,
2825 ctx->num_alloc_dentries * 3 / 2);
2826 new_dentries = REALLOC(ctx->dentries,
2827 new_length * sizeof(ctx->dentries[0]));
2828 if (new_dentries == NULL)
2829 return WIMLIB_ERR_NOMEM;
2830 ctx->dentries = new_dentries;
2831 ctx->num_alloc_dentries = new_length;
2833 ctx->dentries[ctx->num_dentries++] = dentry;
2838 do_wimlib_extract_paths(WIMStruct *wim, int image, const tchar *target,
2839 const tchar * const *paths, size_t num_paths,
2840 int extract_flags, wimlib_progress_func_t progress_func)
2843 struct wim_dentry **trees;
2846 if (wim == NULL || target == NULL || target[0] == T('\0') ||
2847 (num_paths != 0 && paths == NULL))
2848 return WIMLIB_ERR_INVALID_PARAM;
2850 ret = check_extract_flags(wim, &extract_flags);
2854 ret = select_wim_image(wim, image);
2858 ret = wim_checksum_unhashed_streams(wim);
2862 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_NTFS |
2863 WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE)) ==
2864 (WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE))
2866 ret = mkdir_if_needed(target);
2871 if (extract_flags & WIMLIB_EXTRACT_FLAG_GLOB_PATHS) {
2873 struct append_dentry_ctx append_dentry_ctx = {
2876 .num_alloc_dentries = 0,
2879 u32 wildcard_flags = get_wildcard_flags(extract_flags);
2881 for (size_t i = 0; i < num_paths; i++) {
2882 tchar *path = canonicalize_wim_path(paths[i]);
2884 ret = WIMLIB_ERR_NOMEM;
2885 trees = append_dentry_ctx.dentries;
2886 goto out_free_trees;
2888 ret = expand_wildcard(wim, path,
2894 trees = append_dentry_ctx.dentries;
2895 goto out_free_trees;
2898 trees = append_dentry_ctx.dentries;
2899 num_trees = append_dentry_ctx.num_dentries;
2901 trees = MALLOC(num_paths * sizeof(trees[0]));
2903 return WIMLIB_ERR_NOMEM;
2905 for (size_t i = 0; i < num_paths; i++) {
2907 tchar *path = canonicalize_wim_path(paths[i]);
2909 ret = WIMLIB_ERR_NOMEM;
2910 goto out_free_trees;
2913 trees[i] = get_dentry(wim, path,
2914 WIMLIB_CASE_PLATFORM_DEFAULT);
2916 if (trees[i] == NULL) {
2917 ERROR("Path \"%"TS"\" does not exist "
2919 paths[i], wim->current_image);
2920 ret = WIMLIB_ERR_PATH_DOES_NOT_EXIST;
2921 goto out_free_trees;
2924 num_trees = num_paths;
2927 if (num_trees == 0) {
2929 goto out_free_trees;
2932 ret = extract_trees(wim, trees, num_trees,
2933 target, extract_flags, progress_func);
2940 extract_single_image(WIMStruct *wim, int image,
2941 const tchar *target, int extract_flags,
2942 wimlib_progress_func_t progress_func)
2944 const tchar *path = WIMLIB_WIM_ROOT_PATH;
2945 extract_flags |= WIMLIB_EXTRACT_FLAG_IMAGEMODE;
2946 return do_wimlib_extract_paths(wim, image, target, &path, 1,
2947 extract_flags, progress_func);
2950 static const tchar * const filename_forbidden_chars =
2959 /* This function checks if it is okay to use a WIM image's name as a directory
2962 image_name_ok_as_dir(const tchar *image_name)
2964 return image_name && *image_name &&
2965 !tstrpbrk(image_name, filename_forbidden_chars) &&
2966 tstrcmp(image_name, T(".")) &&
2967 tstrcmp(image_name, T(".."));
2970 /* Extracts all images from the WIM to the directory @target, with the images
2971 * placed in subdirectories named by their image names. */
2973 extract_all_images(WIMStruct *wim,
2974 const tchar *target,
2976 wimlib_progress_func_t progress_func)
2978 size_t image_name_max_len = max(xml_get_max_image_name_len(wim), 20);
2979 size_t output_path_len = tstrlen(target);
2980 tchar buf[output_path_len + 1 + image_name_max_len + 1];
2983 const tchar *image_name;
2985 extract_flags |= WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
2987 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
2988 ERROR("Cannot extract multiple images in NTFS extraction mode.");
2989 return WIMLIB_ERR_INVALID_PARAM;
2992 ret = mkdir_if_needed(target);
2995 tmemcpy(buf, target, output_path_len);
2996 buf[output_path_len] = OS_PREFERRED_PATH_SEPARATOR;
2997 for (image = 1; image <= wim->hdr.image_count; image++) {
2998 image_name = wimlib_get_image_name(wim, image);
2999 if (image_name_ok_as_dir(image_name)) {
3000 tstrcpy(buf + output_path_len + 1, image_name);
3002 /* Image name is empty or contains forbidden characters.
3003 * Use image number instead. */
3004 tsprintf(buf + output_path_len + 1, T("%d"), image);
3006 ret = extract_single_image(wim, image, buf, extract_flags,
3015 clear_lte_extracted_file(WIMStruct *wim, int extract_flags)
3017 if (unlikely(extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
3018 WIMLIB_EXTRACT_FLAG_HARDLINK)))
3019 for_lookup_table_entry(wim->lookup_table,
3020 lte_free_extracted_file, NULL);
3024 do_wimlib_extract_image(WIMStruct *wim,
3026 const tchar *target,
3028 wimlib_progress_func_t progress_func)
3032 if (extract_flags & (WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE |
3033 WIMLIB_EXTRACT_FLAG_TO_STDOUT |
3034 WIMLIB_EXTRACT_FLAG_GLOB_PATHS))
3035 return WIMLIB_ERR_INVALID_PARAM;
3037 if (image == WIMLIB_ALL_IMAGES)
3038 ret = extract_all_images(wim, target, extract_flags,
3041 ret = extract_single_image(wim, image, target, extract_flags,
3044 clear_lte_extracted_file(wim, extract_flags);
3049 /****************************************************************************
3051 ****************************************************************************/
3054 wimlib_extract_paths(WIMStruct *wim, int image, const tchar *target,
3055 const tchar * const *paths, size_t num_paths,
3056 int extract_flags, wimlib_progress_func_t progress_func)
3060 if (extract_flags & ~WIMLIB_EXTRACT_MASK_PUBLIC)
3061 return WIMLIB_ERR_INVALID_PARAM;
3063 ret = do_wimlib_extract_paths(wim, image, target, paths, num_paths,
3064 extract_flags, progress_func);
3065 clear_lte_extracted_file(wim, extract_flags);
3070 wimlib_extract_pathlist(WIMStruct *wim, int image, const tchar *target,
3071 const tchar *path_list_file, int extract_flags,
3072 wimlib_progress_func_t progress_func)
3079 ret = read_path_list_file(path_list_file, &paths, &num_paths, &mem);
3081 ERROR("Failed to read path list file \"%"TS"\"",
3086 ret = wimlib_extract_paths(wim, image, target,
3087 (const tchar * const *)paths, num_paths,
3088 extract_flags, progress_func);
3095 wimlib_extract_image_from_pipe(int pipe_fd, const tchar *image_num_or_name,
3096 const tchar *target, int extract_flags,
3097 wimlib_progress_func_t progress_func)
3101 struct filedes *in_fd;
3105 if (extract_flags & ~WIMLIB_EXTRACT_MASK_PUBLIC)
3106 return WIMLIB_ERR_INVALID_PARAM;
3108 if (extract_flags & WIMLIB_EXTRACT_FLAG_FILE_ORDER)
3109 return WIMLIB_ERR_INVALID_PARAM;
3111 /* Read the WIM header from the pipe and get a WIMStruct to represent
3112 * the pipable WIM. Caveats: Unlike getting a WIMStruct with
3113 * wimlib_open_wim(), getting a WIMStruct in this way will result in
3114 * an empty lookup table, no XML data read, and no filename set. */
3115 ret = open_wim_as_WIMStruct(&pipe_fd,
3116 WIMLIB_OPEN_FLAG_FROM_PIPE,
3117 &pwm, progress_func);
3121 /* Sanity check to make sure this is a pipable WIM. */
3122 if (pwm->hdr.magic != PWM_MAGIC) {
3123 ERROR("The WIM being read from file descriptor %d "
3124 "is not pipable!", pipe_fd);
3125 ret = WIMLIB_ERR_NOT_PIPABLE;
3126 goto out_wimlib_free;
3129 /* Sanity check to make sure the first part of a pipable split WIM is
3130 * sent over the pipe first. */
3131 if (pwm->hdr.part_number != 1) {
3132 ERROR("The first part of the split WIM must be "
3133 "sent over the pipe first.");
3134 ret = WIMLIB_ERR_INVALID_PIPABLE_WIM;
3135 goto out_wimlib_free;
3138 in_fd = &pwm->in_fd;
3139 wimlib_assert(in_fd->offset == WIM_HEADER_DISK_SIZE);
3141 /* As mentioned, the WIMStruct we created from the pipe does not have
3142 * XML data yet. Fix this by reading the extra copy of the XML data
3143 * that directly follows the header in pipable WIMs. (Note: see
3144 * write_pipable_wim() for more details about the format of pipable
3147 struct wim_lookup_table_entry xml_lte;
3148 struct wim_resource_spec xml_rspec;
3149 ret = read_pwm_stream_header(pwm, &xml_lte, &xml_rspec, 0, NULL);
3151 goto out_wimlib_free;
3153 if (!(xml_lte.flags & WIM_RESHDR_FLAG_METADATA))
3155 ERROR("Expected XML data, but found non-metadata "
3157 ret = WIMLIB_ERR_INVALID_PIPABLE_WIM;
3158 goto out_wimlib_free;
3161 wim_res_spec_to_hdr(&xml_rspec, &pwm->hdr.xml_data_reshdr);
3163 ret = read_wim_xml_data(pwm);
3165 goto out_wimlib_free;
3167 if (wim_info_get_num_images(pwm->wim_info) != pwm->hdr.image_count) {
3168 ERROR("Image count in XML data is not the same as in WIM header.");
3169 ret = WIMLIB_ERR_IMAGE_COUNT;
3170 goto out_wimlib_free;
3174 /* Get image index (this may use the XML data that was just read to
3175 * resolve an image name). */
3176 if (image_num_or_name) {
3177 image = wimlib_resolve_image(pwm, image_num_or_name);
3178 if (image == WIMLIB_NO_IMAGE) {
3179 ERROR("\"%"TS"\" is not a valid image in the pipable WIM!",
3181 ret = WIMLIB_ERR_INVALID_IMAGE;
3182 goto out_wimlib_free;
3183 } else if (image == WIMLIB_ALL_IMAGES) {
3184 ERROR("Applying all images from a pipe is not supported!");
3185 ret = WIMLIB_ERR_INVALID_IMAGE;
3186 goto out_wimlib_free;
3189 if (pwm->hdr.image_count != 1) {
3190 ERROR("No image was specified, but the pipable WIM "
3191 "did not contain exactly 1 image");
3192 ret = WIMLIB_ERR_INVALID_IMAGE;
3193 goto out_wimlib_free;
3198 /* Load the needed metadata resource. */
3199 for (i = 1; i <= pwm->hdr.image_count; i++) {
3200 struct wim_lookup_table_entry *metadata_lte;
3201 struct wim_image_metadata *imd;
3202 struct wim_resource_spec *metadata_rspec;
3204 metadata_lte = new_lookup_table_entry();
3205 if (metadata_lte == NULL) {
3206 ret = WIMLIB_ERR_NOMEM;
3207 goto out_wimlib_free;
3209 metadata_rspec = MALLOC(sizeof(struct wim_resource_spec));
3210 if (metadata_rspec == NULL) {
3211 ret = WIMLIB_ERR_NOMEM;
3212 free_lookup_table_entry(metadata_lte);
3213 goto out_wimlib_free;
3216 ret = read_pwm_stream_header(pwm, metadata_lte, metadata_rspec, 0, NULL);
3217 imd = pwm->image_metadata[i - 1];
3218 imd->metadata_lte = metadata_lte;
3220 FREE(metadata_rspec);
3221 goto out_wimlib_free;
3224 if (!(metadata_lte->flags & WIM_RESHDR_FLAG_METADATA)) {
3225 ERROR("Expected metadata resource, but found "
3226 "non-metadata stream.");
3227 ret = WIMLIB_ERR_INVALID_PIPABLE_WIM;
3228 goto out_wimlib_free;
3232 /* Metadata resource is for the image being extracted.
3233 * Parse it and save the metadata in memory. */
3234 ret = read_metadata_resource(pwm, imd);
3236 goto out_wimlib_free;
3239 /* Metadata resource is not for the image being
3240 * extracted. Skip over it. */
3241 ret = skip_wim_stream(metadata_lte);
3243 goto out_wimlib_free;
3246 /* Extract the image. */
3247 extract_flags |= WIMLIB_EXTRACT_FLAG_FROM_PIPE;
3248 ret = do_wimlib_extract_image(pwm, image, target,
3249 extract_flags, progress_func);
3250 /* Clean up and return. */
3257 wimlib_extract_image(WIMStruct *wim, int image, const tchar *target,
3258 int extract_flags, wimlib_progress_func_t progress_func)
3260 if (extract_flags & ~WIMLIB_EXTRACT_MASK_PUBLIC)
3261 return WIMLIB_ERR_INVALID_PARAM;
3262 return do_wimlib_extract_image(wim, image, target, extract_flags,