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_PATHMODE 0x20000000
73 #define WIMLIB_EXTRACT_MASK_PUBLIC 0x1fffffff
75 /* Given a WIM dentry in the tree to be extracted, resolve all streams in the
76 * corresponding inode and set 'out_refcnt' in each to 0. */
78 dentry_resolve_and_zero_lte_refcnt(struct wim_dentry *dentry, void *_ctx)
80 struct apply_ctx *ctx = _ctx;
81 struct wim_inode *inode = dentry->d_inode;
82 struct wim_lookup_table_entry *lte;
86 if (dentry->extraction_skipped)
89 /* Special case: when extracting from a pipe, the WIM lookup table is
90 * initially empty, so "resolving" an inode's streams is initially not
91 * possible. However, we still need to keep track of which streams,
92 * identified by SHA1 message digests, need to be extracted, so we
93 * "resolve" the inode's streams anyway by allocating new entries. */
94 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_FROM_PIPE)
96 ret = inode_resolve_streams(inode, ctx->wim->lookup_table, force);
99 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
100 lte = inode_stream_lte_resolved(inode, i);
108 is_linked_extraction(const struct apply_ctx *ctx)
110 return 0 != (ctx->extract_flags & (WIMLIB_EXTRACT_FLAG_HARDLINK |
111 WIMLIB_EXTRACT_FLAG_SYMLINK));
115 can_extract_named_data_streams(const struct apply_ctx *ctx)
117 return ctx->supported_features.named_data_streams &&
118 !is_linked_extraction(ctx);
122 ref_stream_to_extract(struct wim_lookup_table_entry *lte,
123 struct wim_dentry *dentry, struct apply_ctx *ctx)
128 /* Tally the size only for each extraction of the stream (not hard
130 if (!(dentry->d_inode->i_visited &&
131 ctx->supported_features.hard_links) &&
132 (!is_linked_extraction(ctx) || (lte->out_refcnt == 0 &&
133 lte->extracted_file == NULL)))
135 ctx->progress.extract.total_bytes += lte->size;
136 ctx->progress.extract.num_streams++;
139 /* Add stream to the extraction_list only one time, even if it's going
140 * to be extracted to multiple locations. */
141 if (lte->out_refcnt == 0) {
142 list_add_tail(<e->extraction_list, &ctx->stream_list);
143 ctx->num_streams_remaining++;
146 if (!(ctx->extract_flags & WIMLIB_EXTRACT_FLAG_FILE_ORDER)) {
147 struct wim_dentry **lte_dentries;
149 /* Append dentry to this stream's array of dentries referencing
150 * it. Use inline array to avoid memory allocation until the
151 * number of dentries becomes too large. */
152 if (lte->out_refcnt < ARRAY_LEN(lte->inline_lte_dentries)) {
153 lte_dentries = lte->inline_lte_dentries;
155 struct wim_dentry **prev_lte_dentries;
156 size_t alloc_lte_dentries;
158 if (lte->out_refcnt == ARRAY_LEN(lte->inline_lte_dentries)) {
159 prev_lte_dentries = NULL;
160 alloc_lte_dentries = ARRAY_LEN(lte->inline_lte_dentries);
162 prev_lte_dentries = lte->lte_dentries;
163 alloc_lte_dentries = lte->alloc_lte_dentries;
166 if (lte->out_refcnt == alloc_lte_dentries) {
167 alloc_lte_dentries *= 2;
168 lte_dentries = REALLOC(prev_lte_dentries,
170 sizeof(lte_dentries[0]));
171 if (lte_dentries == NULL)
172 return WIMLIB_ERR_NOMEM;
173 if (prev_lte_dentries == NULL) {
175 lte->inline_lte_dentries,
176 sizeof(lte->inline_lte_dentries));
178 lte->lte_dentries = lte_dentries;
179 lte->alloc_lte_dentries = alloc_lte_dentries;
181 lte_dentries = lte->lte_dentries;
183 lte_dentries[lte->out_refcnt] = dentry;
189 /* Given a WIM dentry in the tree to be extracted, iterate through streams that
190 * need to be extracted. For each one, add it to the list of streams to be
191 * extracted (ctx->stream_list) if not already done so, and also update the
192 * progress information (ctx->progress) with the stream. Furthermore, if doing
193 * a sequential extraction, build a mapping from each the stream to the dentries
196 * This uses the i_visited member of the inodes (assumed to be 0 initially). */
198 dentry_add_streams_to_extract(struct wim_dentry *dentry, void *_ctx)
200 struct apply_ctx *ctx = _ctx;
201 struct wim_inode *inode = dentry->d_inode;
204 /* Don't process dentries marked as skipped. */
205 if (dentry->extraction_skipped)
208 /* The unnamed data stream will always be extracted, except in an
210 if (!inode_is_encrypted_directory(inode)) {
211 ret = ref_stream_to_extract(inode_unnamed_lte_resolved(inode),
217 /* Named data streams will be extracted only if supported in the current
218 * extraction mode and volume, and to avoid complications, if not doing
219 * a linked extraction. */
220 if (can_extract_named_data_streams(ctx)) {
221 for (u16 i = 0; i < inode->i_num_ads; i++) {
222 if (!ads_entry_is_named_stream(&inode->i_ads_entries[i]))
224 ret = ref_stream_to_extract(inode->i_ads_entries[i].lte,
230 inode->i_visited = 1;
234 /* Inform library user of progress of stream extraction following the successful
235 * extraction of a copy of the stream specified by @lte. */
237 update_extract_progress(struct apply_ctx *ctx,
238 const struct wim_lookup_table_entry *lte)
240 wimlib_progress_func_t progress_func = ctx->progress_func;
241 union wimlib_progress_info *progress = &ctx->progress;
243 progress->extract.completed_bytes += lte->size;
245 progress->extract.completed_bytes >= ctx->next_progress)
247 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS, progress);
248 if (progress->extract.completed_bytes >=
249 progress->extract.total_bytes)
251 ctx->next_progress = ~0ULL;
253 ctx->next_progress += progress->extract.total_bytes / 128;
254 if (ctx->next_progress > progress->extract.total_bytes)
255 ctx->next_progress = progress->extract.total_bytes;
261 /* Extract a symbolic link (not directly as reparse data), handling fixing up
262 * the target of absolute symbolic links and updating the extract progress.
264 * @inode must specify the WIM inode for a symbolic link or junction reparse
267 * @lte_override overrides the resource used as the reparse data for the
270 extract_symlink(const tchar *path, struct apply_ctx *ctx,
271 struct wim_inode *inode,
272 struct wim_lookup_table_entry *lte_override)
274 ssize_t bufsize = ctx->ops->path_max;
275 tchar target[bufsize];
281 /* If absolute symbolic link fixups requested, reserve space in the link
282 * target buffer for the absolute path of the target directory. */
283 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX)
285 buf += ctx->realtarget_nchars;
286 bufsize -= ctx->realtarget_nchars;
289 /* Translate the WIM inode's reparse data into the link target. */
290 sret = wim_inode_readlink(inode, buf, bufsize - 1, lte_override);
293 return WIMLIB_ERR_READLINK;
297 if ((ctx->extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX) &&
300 /* Fix absolute symbolic link target to point into the
301 * actual extraction destination. */
302 tmemcpy(target, ctx->realtarget, ctx->realtarget_nchars);
303 fixed_target = target;
305 /* Keep same link target. */
309 /* Call into the apply_operations to create the symbolic link. */
310 DEBUG("Creating symlink \"%"TS"\" => \"%"TS"\"",
312 ret = ctx->ops->create_symlink(fixed_target, path, ctx);
314 ERROR_WITH_ERRNO("Failed to create symlink "
315 "\"%"TS"\" => \"%"TS"\"", path, fixed_target);
319 /* Account for reparse data consumed. */
320 update_extract_progress(ctx,
321 (lte_override ? lte_override :
322 inode_unnamed_lte_resolved(inode)));
325 #endif /* !__WIN32__ */
327 /* Create a file, directory, or symbolic link. */
329 extract_inode(const tchar *path, struct apply_ctx *ctx, struct wim_inode *inode)
334 if (ctx->supported_features.symlink_reparse_points &&
335 !ctx->supported_features.reparse_points &&
336 inode_is_symlink(inode))
338 ret = extract_symlink(path, ctx, inode, NULL);
340 #endif /* !__WIN32__ */
341 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY) {
342 ret = ctx->ops->create_directory(path, ctx, &inode->extract_cookie);
344 ERROR_WITH_ERRNO("Failed to create the directory "
347 } else if ((inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED) &&
348 ctx->ops->extract_encrypted_stream_creates_file &&
349 ctx->supported_features.encrypted_files) {
350 ret = ctx->ops->extract_encrypted_stream(
351 path, inode_unnamed_lte_resolved(inode), ctx);
353 ERROR_WITH_ERRNO("Failed to create and extract "
354 "encrypted file \"%"TS"\"", path);
357 ret = ctx->ops->create_file(path, ctx, &inode->extract_cookie);
359 ERROR_WITH_ERRNO("Failed to create the file "
367 extract_hardlink(const tchar *oldpath, const tchar *newpath,
368 struct apply_ctx *ctx)
372 DEBUG("Creating hardlink \"%"TS"\" => \"%"TS"\"", newpath, oldpath);
373 ret = ctx->ops->create_hardlink(oldpath, newpath, ctx);
375 ERROR_WITH_ERRNO("Failed to create hardlink "
376 "\"%"TS"\" => \"%"TS"\"",
384 try_extract_rpfix(u8 *rpbuf,
386 const wchar_t *extract_root_realpath,
387 unsigned extract_root_realpath_nchars)
389 struct reparse_data rpdata;
391 size_t target_nchars;
392 size_t stripped_nchars;
393 wchar_t *stripped_target;
394 wchar_t stripped_target_nchars;
397 utf16lechar *new_target;
398 utf16lechar *new_print_name;
399 size_t new_target_nchars;
400 size_t new_print_name_nchars;
403 ret = parse_reparse_data(rpbuf, *rpbuflen_p, &rpdata);
407 if (extract_root_realpath[0] == L'\0' ||
408 extract_root_realpath[1] != L':' ||
409 extract_root_realpath[2] != L'\\')
410 return WIMLIB_ERR_REPARSE_POINT_FIXUP_FAILED;
412 ret = parse_substitute_name(rpdata.substitute_name,
413 rpdata.substitute_name_nbytes,
417 stripped_nchars = ret;
418 target = rpdata.substitute_name;
419 target_nchars = rpdata.substitute_name_nbytes / sizeof(utf16lechar);
420 stripped_target = target + stripped_nchars;
421 stripped_target_nchars = target_nchars - stripped_nchars;
423 new_target = alloca((6 + extract_root_realpath_nchars +
424 stripped_target_nchars) * sizeof(utf16lechar));
427 if (stripped_nchars == 6) {
428 /* Include \??\ prefix if it was present before */
429 p = wmempcpy(p, L"\\??\\", 4);
432 /* Print name excludes the \??\ if present. */
434 if (stripped_nchars != 0) {
435 /* Get drive letter from real path to extract root, if a drive
436 * letter was present before. */
437 *p++ = extract_root_realpath[0];
438 *p++ = extract_root_realpath[1];
440 /* Copy the rest of the extract root */
441 p = wmempcpy(p, extract_root_realpath + 2, extract_root_realpath_nchars - 2);
443 /* Append the stripped target */
444 p = wmempcpy(p, stripped_target, stripped_target_nchars);
445 new_target_nchars = p - new_target;
446 new_print_name_nchars = p - new_print_name;
448 if (new_target_nchars * sizeof(utf16lechar) >= REPARSE_POINT_MAX_SIZE ||
449 new_print_name_nchars * sizeof(utf16lechar) >= REPARSE_POINT_MAX_SIZE)
450 return WIMLIB_ERR_REPARSE_POINT_FIXUP_FAILED;
452 rpdata.substitute_name = new_target;
453 rpdata.substitute_name_nbytes = new_target_nchars * sizeof(utf16lechar);
454 rpdata.print_name = new_print_name;
455 rpdata.print_name_nbytes = new_print_name_nchars * sizeof(utf16lechar);
456 return make_reparse_buffer(&rpdata, rpbuf, rpbuflen_p);
458 #endif /* __WIN32__ */
460 /* Set reparse data on extracted file or directory that has
461 * FILE_ATTRIBUTE_REPARSE_POINT set. */
463 extract_reparse_data(const tchar *path, struct apply_ctx *ctx,
464 struct wim_inode *inode,
465 struct wim_lookup_table_entry *lte_override)
468 u8 rpbuf[REPARSE_POINT_MAX_SIZE];
471 ret = wim_inode_get_reparse_data(inode, rpbuf, &rpbuflen, lte_override);
476 /* Fix up target of absolute symbolic link or junction points so
477 * that they point into the actual extraction target. */
478 if ((ctx->extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX) &&
479 (inode->i_reparse_tag == WIM_IO_REPARSE_TAG_SYMLINK ||
480 inode->i_reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT) &&
481 !inode->i_not_rpfixed)
483 ret = try_extract_rpfix(rpbuf, &rpbuflen, ctx->realtarget,
484 ctx->realtarget_nchars);
485 if (ret && !(ctx->extract_flags &
486 WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS))
488 WARNING("Reparse point fixup of \"%"TS"\" "
497 ret = ctx->ops->set_reparse_data(path, rpbuf, rpbuflen, ctx);
499 /* On Windows, the SeCreateSymbolicLink privilege is required to create
500 * symbolic links. To be more friendly towards non-Administrator users,
501 * we merely warn the user if symbolic links cannot be created due to
502 * insufficient permissions or privileges, unless
503 * WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS was provided. */
505 if (ret && inode_is_symlink(inode) &&
506 (errno == EACCES || errno == EPERM) &&
507 !(ctx->extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS))
509 WARNING("Can't set reparse data on \"%"TS"\": "
511 " You may be trying to "
512 "extract a symbolic link without the\n"
513 " SeCreateSymbolicLink privilege, "
514 "which by default non-Administrator\n"
515 " accounts do not have.",
523 /* Account for reparse data consumed. */
524 update_extract_progress(ctx,
525 (lte_override ? lte_override :
526 inode_unnamed_lte_resolved(inode)));
530 ERROR_WITH_ERRNO("Failed to set reparse data on \"%"TS"\"", path);
535 * Extract zero or more streams to a file.
537 * This function operates slightly differently depending on whether @lte_spec is
538 * NULL or not. When @lte_spec is NULL, the behavior is to extract the default
539 * file contents (unnamed stream), and, if named data streams are supported in
540 * the extract mode and volume, any named data streams. When @lte_spec is not
541 * NULL, the behavior is to extract only all copies of the stream @lte_spec, and
542 * in addition use @lte_spec to set the reparse data or create the symbolic link
546 * Path to file to extract (as can be passed to apply_operations
551 * WIM dentry that corresponds to the file being extracted.
553 * If non-NULL, specifies the lookup table entry for a stream to extract,
554 * and only that stream will be extracted (although there may be more than
555 * one instance of it).
557 * Used only if @lte_spec != NULL; it is passed to the extraction functions
558 * rather than @lte_spec, allowing the location of the stream to be
559 * overridden. (This is used when the WIM is being read from a nonseekable
560 * file, such as a pipe, when streams need to be used more than once; each
561 * such stream is extracted to a temporary file.)
564 extract_streams(const tchar *path, struct apply_ctx *ctx,
565 struct wim_dentry *dentry,
566 struct wim_lookup_table_entry *lte_spec,
567 struct wim_lookup_table_entry *lte_override)
569 struct wim_inode *inode = dentry->d_inode;
570 struct wim_lookup_table_entry *lte;
571 file_spec_t file_spec;
574 if (dentry->was_hardlinked)
581 for (unsigned i = 0; i < SHA1_HASH_SIZE; i++)
582 p += sprintf(p, "%02x", lte_override->hash[i]);
583 DEBUG("Extracting stream SHA1=%s to \"%"TS"\"",
584 sha1_str, path, inode->i_ino);
586 DEBUG("Extracting streams to \"%"TS"\"", path, inode->i_ino);
590 if (ctx->ops->uses_cookies)
591 file_spec.cookie = inode->extract_cookie;
593 file_spec.path = path;
595 /* Unnamed data stream. */
596 lte = inode_unnamed_lte_resolved(inode);
597 if (lte && (!lte_spec || lte == lte_spec)) {
600 if (!(inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
601 FILE_ATTRIBUTE_REPARSE_POINT)))
603 if (inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED &&
604 ctx->supported_features.encrypted_files) {
605 if (!ctx->ops->extract_encrypted_stream_creates_file) {
606 ret = ctx->ops->extract_encrypted_stream(
612 ret = ctx->ops->extract_unnamed_stream(
613 file_spec, lte, ctx);
617 update_extract_progress(ctx, lte);
619 else if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT)
622 if (ctx->supported_features.reparse_points)
623 ret = extract_reparse_data(path, ctx, inode, lte);
625 else if ((inode_is_symlink(inode) &&
626 ctx->supported_features.symlink_reparse_points))
627 ret = extract_symlink(path, ctx, inode, lte);
634 /* Named data streams. */
635 if (can_extract_named_data_streams(ctx)) {
636 for (u16 i = 0; i < inode->i_num_ads; i++) {
637 struct wim_ads_entry *entry = &inode->i_ads_entries[i];
639 if (!ads_entry_is_named_stream(entry))
644 if (lte_spec && lte_spec != lte)
648 ret = ctx->ops->extract_named_stream(file_spec, entry->stream_name,
649 entry->stream_name_nbytes / 2,
653 update_extract_progress(ctx, lte);
659 ERROR_WITH_ERRNO("Failed to extract data of \"%"TS"\"", path);
663 /* Set attributes on an extracted file or directory if supported by the
664 * extraction mode. */
666 extract_file_attributes(const tchar *path, struct apply_ctx *ctx,
667 struct wim_dentry *dentry, unsigned pass)
671 if (ctx->ops->set_file_attributes &&
672 !(dentry == ctx->extract_root && ctx->root_dentry_is_special)) {
673 u32 attributes = dentry->d_inode->i_attributes;
675 /* Clear unsupported attributes. */
676 attributes &= ctx->supported_attributes_mask;
678 if ((attributes & FILE_ATTRIBUTE_DIRECTORY &&
679 !ctx->supported_features.encrypted_directories) ||
680 (!(attributes & FILE_ATTRIBUTE_DIRECTORY) &&
681 !ctx->supported_features.encrypted_files))
683 attributes &= ~FILE_ATTRIBUTE_ENCRYPTED;
687 attributes = FILE_ATTRIBUTE_NORMAL;
689 ret = ctx->ops->set_file_attributes(path, attributes, ctx, pass);
691 ERROR_WITH_ERRNO("Failed to set attributes on "
700 /* Set or remove the short (DOS) name on an extracted file or directory if
701 * supported by the extraction mode. Since DOS names are unimportant and it's
702 * easy to run into problems setting them on Windows (SetFileShortName()
703 * requires SE_RESTORE privilege, which only the Administrator can request, and
704 * also requires DELETE access to the file), failure is ignored unless
705 * WIMLIB_EXTRACT_FLAG_STRICT_SHORT_NAMES is set. */
707 extract_short_name(const tchar *path, struct apply_ctx *ctx,
708 struct wim_dentry *dentry)
712 /* The root of the dentry tree being extracted may not be extracted to
713 * its original name, so its short name should be ignored. */
714 if (dentry == ctx->extract_root)
717 if (ctx->supported_features.short_names) {
718 ret = ctx->ops->set_short_name(path,
720 dentry->short_name_nbytes / 2,
722 if (ret && (ctx->extract_flags &
723 WIMLIB_EXTRACT_FLAG_STRICT_SHORT_NAMES))
725 ERROR_WITH_ERRNO("Failed to set short name of "
733 /* Set security descriptor, UNIX data, or neither on an extracted file, taking
734 * into account the current extraction mode and flags. */
736 extract_security(const tchar *path, struct apply_ctx *ctx,
737 struct wim_dentry *dentry)
740 struct wim_inode *inode = dentry->d_inode;
742 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_NO_ACLS)
745 if ((ctx->extract_root == dentry) && ctx->root_dentry_is_special)
749 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
750 struct wimlib_unix_data data;
752 ret = inode_get_unix_data(inode, &data, NULL);
756 ret = ctx->ops->set_unix_data(path, &data, ctx);
758 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_ACLS) {
759 ERROR_WITH_ERRNO("Failed to set UNIX owner, "
760 "group, and/or mode on "
764 WARNING_WITH_ERRNO("Failed to set UNIX owner, "
765 "group, and/or/mode on "
771 #endif /* __WIN32__ */
772 if (ctx->supported_features.security_descriptors &&
773 inode->i_security_id != -1)
775 const struct wim_security_data *sd;
779 sd = wim_const_security_data(ctx->wim);
780 desc = sd->descriptors[inode->i_security_id];
781 desc_size = sd->sizes[inode->i_security_id];
783 ret = ctx->ops->set_security_descriptor(path, desc,
786 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_ACLS) {
787 ERROR_WITH_ERRNO("Failed to set security "
788 "descriptor on \"%"TS"\"", path);
792 if (errno != EACCES) {
793 WARNING_WITH_ERRNO("Failed to set "
794 "security descriptor "
795 "on \"%"TS"\"", path);
798 ctx->no_security_descriptors++;
805 /* Set timestamps on an extracted file. Failure is warning-only unless
806 * WIMLIB_EXTRACT_FLAG_STRICT_TIMESTAMPS is set. */
808 extract_timestamps(const tchar *path, struct apply_ctx *ctx,
809 struct wim_dentry *dentry)
811 struct wim_inode *inode = dentry->d_inode;
814 if ((ctx->extract_root == dentry) && ctx->root_dentry_is_special)
817 if (ctx->ops->set_timestamps) {
818 ret = ctx->ops->set_timestamps(path,
819 inode->i_creation_time,
820 inode->i_last_write_time,
821 inode->i_last_access_time,
824 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_TIMESTAMPS) {
825 ERROR_WITH_ERRNO("Failed to set timestamps "
826 "on \"%"TS"\"", path);
829 WARNING_WITH_ERRNO("Failed to set timestamps "
830 "on \"%"TS"\"", path);
837 /* Check whether the extraction of a dentry should be skipped completely. */
839 dentry_is_supported(struct wim_dentry *dentry,
840 const struct wim_features *supported_features)
842 struct wim_inode *inode = dentry->d_inode;
844 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
845 return supported_features->reparse_points ||
846 (inode_is_symlink(inode) &&
847 supported_features->symlink_reparse_points);
849 if (inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED) {
850 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)
851 return supported_features->encrypted_directories != 0;
853 return supported_features->encrypted_files != 0;
858 /* Given a WIM dentry to extract, build the path to which to extract it, in the
859 * format understood by the callbacks in the apply_operations being used.
861 * Write the resulting path into @path, which must have room for at least
862 * ctx->ops->max_path characters including the null-terminator.
864 * Return %true if successful; %false if this WIM dentry doesn't actually need
865 * to be extracted or if the calculated path exceeds ctx->ops->max_path
868 * This function clobbers the tmp_list member of @dentry and its ancestors up
869 * until the extraction root. */
871 build_extraction_path(tchar path[], struct wim_dentry *dentry,
872 struct apply_ctx *ctx)
875 LIST_HEAD(ancestor_list);
877 const tchar *target_prefix;
878 size_t target_prefix_nchars;
879 struct wim_dentry *d;
881 if (dentry->extraction_skipped)
884 path_nchars = ctx->ops->path_prefix_nchars;
886 if (ctx->ops->requires_realtarget_in_paths) {
887 target_prefix = ctx->realtarget;
888 target_prefix_nchars = ctx->realtarget_nchars;
889 } else if (ctx->ops->requires_target_in_paths) {
890 target_prefix = ctx->target;
891 target_prefix_nchars = ctx->target_nchars;
893 target_prefix = NULL;
894 target_prefix_nchars = 0;
896 path_nchars += target_prefix_nchars;
898 for (d = dentry; d != ctx->extract_root; d = d->parent) {
899 path_nchars += d->extraction_name_nchars + 1;
900 list_add(&d->tmp_list, &ancestor_list);
903 path_nchars++; /* null terminator */
905 if (path_nchars > ctx->ops->path_max) {
906 WARNING("\"%"TS"\": Path too long to extract",
907 dentry_full_path(dentry));
911 p = tmempcpy(p, ctx->ops->path_prefix, ctx->ops->path_prefix_nchars);
912 p = tmempcpy(p, target_prefix, target_prefix_nchars);
913 list_for_each_entry(d, &ancestor_list, tmp_list) {
914 *p++ = ctx->ops->path_separator;
915 p = tmempcpy(p, d->extraction_name, d->extraction_name_nchars);
918 wimlib_assert(p - path == path_nchars);
923 get_num_path_components(const tchar *path, tchar path_separator)
925 unsigned num_components = 0;
927 /* Ignore drive letter. */
928 if (path[0] != L'\0' && path[1] == L':')
933 while (*path == path_separator)
937 while (*path && *path != path_separator)
940 return num_components;
944 extract_multiimage_symlink(const tchar *oldpath, const tchar *newpath,
945 struct apply_ctx *ctx, struct wim_dentry *dentry)
947 size_t num_raw_path_components;
948 const struct wim_dentry *d;
949 size_t num_target_path_components;
954 num_raw_path_components = 0;
955 for (d = dentry; d != ctx->extract_root; d = d->parent)
956 num_raw_path_components++;
958 if (ctx->ops->requires_realtarget_in_paths)
959 num_target_path_components = get_num_path_components(ctx->realtarget,
960 ctx->ops->path_separator);
961 else if (ctx->ops->requires_target_in_paths)
962 num_target_path_components = get_num_path_components(ctx->target,
963 ctx->ops->path_separator);
965 num_target_path_components = 0;
967 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE) {
968 wimlib_assert(num_target_path_components > 0);
969 num_raw_path_components++;
970 num_target_path_components--;
973 p_old = oldpath + ctx->ops->path_prefix_nchars;
975 if (p_old[0] != L'\0' && p_old[1] == ':')
978 while (*p_old == ctx->ops->path_separator)
980 while (--num_target_path_components) {
981 while (*p_old != ctx->ops->path_separator)
983 while (*p_old == ctx->ops->path_separator)
987 tchar symlink_target[tstrlen(p_old) + 3 * num_raw_path_components + 1];
989 p = &symlink_target[0];
990 while (num_raw_path_components--) {
993 *p++ = ctx->ops->path_separator;
996 DEBUG("Creating symlink \"%"TS"\" => \"%"TS"\"",
997 newpath, symlink_target);
998 ret = ctx->ops->create_symlink(symlink_target, newpath, ctx);
1000 ERROR_WITH_ERRNO("Failed to create symlink "
1001 "\"%"TS"\" => \"%"TS"\"",
1002 newpath, symlink_target);
1007 /* Create the "skeleton" of an extracted file or directory. Don't yet extract
1008 * data streams, reparse data (including symbolic links), timestamps, and
1009 * security descriptors. Basically, everything that doesn't require reading
1010 * non-metadata resources from the WIM file and isn't delayed until the final
1013 do_dentry_extract_skeleton(tchar path[], struct wim_dentry *dentry,
1014 struct apply_ctx *ctx)
1016 struct wim_inode *inode = dentry->d_inode;
1018 const tchar *oldpath;
1020 if (unlikely(is_linked_extraction(ctx))) {
1021 struct wim_lookup_table_entry *unnamed_lte;
1023 unnamed_lte = inode_unnamed_lte_resolved(dentry->d_inode);
1024 if (unnamed_lte && unnamed_lte->extracted_file) {
1025 oldpath = unnamed_lte->extracted_file;
1026 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_HARDLINK)
1033 /* Create hard link if this dentry corresponds to an already-extracted
1035 if (inode->i_extracted_file) {
1036 oldpath = inode->i_extracted_file;
1040 /* Skip symlinks unless they can be extracted as reparse points rather
1041 * than created directly. */
1042 if (inode_is_symlink(inode) && !ctx->supported_features.reparse_points)
1045 /* Create this file or directory unless it's the extraction root, which
1046 * was already created if necessary. */
1047 if (dentry != ctx->extract_root) {
1048 ret = extract_inode(path, ctx, inode);
1053 /* Create empty named data streams. */
1054 if (can_extract_named_data_streams(ctx)) {
1055 for (u16 i = 0; i < inode->i_num_ads; i++) {
1056 file_spec_t file_spec;
1057 struct wim_ads_entry *entry = &inode->i_ads_entries[i];
1059 if (!ads_entry_is_named_stream(entry))
1063 if (ctx->ops->uses_cookies)
1064 file_spec.cookie = inode->extract_cookie;
1066 file_spec.path = path;
1067 ret = ctx->ops->extract_named_stream(file_spec,
1069 entry->stream_name_nbytes / 2,
1072 ERROR_WITH_ERRNO("\"%"TS"\": failed to create "
1073 "empty named data stream",
1080 /* Set file attributes (if supported). */
1081 ret = extract_file_attributes(path, ctx, dentry, 0);
1085 /* Set or remove file short name (if supported). */
1086 ret = extract_short_name(path, ctx, dentry);
1090 /* If inode has multiple links and hard links are supported in this
1091 * extraction mode and volume, save the path to the extracted file in
1092 * case it's needed to create a hard link. */
1093 if (unlikely(is_linked_extraction(ctx))) {
1094 struct wim_lookup_table_entry *unnamed_lte;
1096 unnamed_lte = inode_unnamed_lte_resolved(dentry->d_inode);
1098 unnamed_lte->extracted_file = TSTRDUP(path);
1099 if (!unnamed_lte->extracted_file)
1100 return WIMLIB_ERR_NOMEM;
1102 } else if (inode->i_nlink > 1 && ctx->supported_features.hard_links) {
1103 inode->i_extracted_file = TSTRDUP(path);
1104 if (!inode->i_extracted_file)
1105 return WIMLIB_ERR_NOMEM;
1110 ret = extract_multiimage_symlink(oldpath, path, ctx, dentry);
1113 dentry->was_hardlinked = 1;
1117 ret = extract_hardlink(oldpath, path, ctx);
1120 dentry->was_hardlinked = 1;
1124 /* This is a wrapper around do_dentry_extract_skeleton() that handles building
1125 * the path, doing short name reordering. This is also idempotent; dentries
1126 * already processed have skeleton_extracted set and no action is taken. See
1127 * apply_operations.requires_short_name_reordering for more details about short
1128 * name reordering. */
1130 dentry_extract_skeleton(struct wim_dentry *dentry, void *_ctx)
1132 struct apply_ctx *ctx = _ctx;
1133 tchar path[ctx->ops->path_max];
1134 struct wim_dentry *orig_dentry;
1135 struct wim_dentry *other_dentry;
1138 if (dentry->skeleton_extracted)
1142 if (ctx->supported_features.short_names
1143 && ctx->ops->requires_short_name_reordering
1144 && !dentry_has_short_name(dentry)
1145 && !dentry->d_inode->i_dos_name_extracted)
1147 inode_for_each_dentry(other_dentry, dentry->d_inode) {
1148 if (dentry_has_short_name(other_dentry)
1149 && !other_dentry->skeleton_extracted
1150 && other_dentry->in_extraction_tree
1151 && !other_dentry->extraction_skipped)
1153 DEBUG("Creating %"TS" before %"TS" "
1154 "to guarantee correct DOS name extraction",
1155 dentry_full_path(other_dentry),
1156 dentry_full_path(dentry));
1157 orig_dentry = dentry;
1158 dentry = other_dentry;
1164 if (!build_extraction_path(path, dentry, ctx))
1166 ret = do_dentry_extract_skeleton(path, dentry, ctx);
1170 dentry->skeleton_extracted = 1;
1173 dentry = orig_dentry;
1177 dentry->d_inode->i_dos_name_extracted = 1;
1182 dentry_extract_dir_skeleton(struct wim_dentry *dentry, void *_ctx)
1184 if (dentry->d_inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)
1185 return dentry_extract_skeleton(dentry, _ctx);
1189 /* Create a file or directory, then immediately extract all streams. The WIM
1190 * may not be read sequentially by this function. */
1192 dentry_extract(struct wim_dentry *dentry, void *_ctx)
1194 struct apply_ctx *ctx = _ctx;
1195 tchar path[ctx->ops->path_max];
1198 ret = dentry_extract_skeleton(dentry, ctx);
1202 if (!build_extraction_path(path, dentry, ctx))
1205 return extract_streams(path, ctx, dentry, NULL, NULL);
1208 /* Creates a temporary file opened for writing. The open file descriptor is
1209 * returned in @fd_ret and its name is returned in @name_ret (dynamically
1212 create_temporary_file(struct filedes *fd_ret, tchar **name_ret)
1218 name = ttempnam(NULL, T("wimlib"));
1220 ERROR_WITH_ERRNO("Failed to create temporary filename");
1221 return WIMLIB_ERR_NOMEM;
1224 raw_fd = topen(name, O_WRONLY | O_CREAT | O_EXCL | O_BINARY, 0600);
1227 if (errno == EEXIST) {
1231 ERROR_WITH_ERRNO("Failed to open temporary file \"%"TS"\"", name);
1233 return WIMLIB_ERR_OPEN;
1236 filedes_init(fd_ret, raw_fd);
1241 /* Extract all instances of the stream @lte that are being extracted in this
1242 * call of extract_tree(), but actually read the stream data from @lte_override.
1245 extract_stream_instances(struct wim_lookup_table_entry *lte,
1246 struct wim_lookup_table_entry *lte_override,
1247 struct apply_ctx *ctx)
1249 struct wim_dentry **lte_dentries;
1250 tchar path[ctx->ops->path_max];
1254 if (lte->out_refcnt <= ARRAY_LEN(lte->inline_lte_dentries))
1255 lte_dentries = lte->inline_lte_dentries;
1257 lte_dentries = lte->lte_dentries;
1259 for (i = 0; i < lte->out_refcnt; i++) {
1260 struct wim_dentry *dentry = lte_dentries[i];
1262 if (dentry->tmp_flag)
1264 if (!build_extraction_path(path, dentry, ctx))
1266 ret = extract_streams(path, ctx, dentry, lte, lte_override);
1268 goto out_clear_tmp_flags;
1269 dentry->tmp_flag = 1;
1272 out_clear_tmp_flags:
1273 for (i = 0; i < lte->out_refcnt; i++)
1274 lte_dentries[i]->tmp_flag = 0;
1278 /* Determine whether the specified stream needs to be extracted to a temporary
1281 * @lte->out_refcnt specifies the number of instances of this stream that must
1284 * @is_partial_res is %true if this stream is just one of multiple in a single
1285 * WIM resource being extracted. */
1287 need_tmpfile_to_extract(struct wim_lookup_table_entry *lte,
1288 bool is_partial_res)
1290 /* Temporary file is always required when reading a partial resource,
1291 * since in that case we retrieve all the contained streams in one pass.
1296 /* Otherwise we don't need a temporary file if only a single instance of
1297 * the stream is needed. */
1298 if (lte->out_refcnt == 1)
1301 wimlib_assert(lte->out_refcnt >= 2);
1303 /* We also don't need a temporary file if random access to the stream is
1305 if (lte->resource_location != RESOURCE_IN_WIM ||
1306 filedes_is_seekable(<e->rspec->wim->in_fd))
1313 begin_extract_stream_to_tmpfile(struct wim_lookup_table_entry *lte,
1314 bool is_partial_res,
1317 struct apply_ctx *ctx = _ctx;
1320 if (!need_tmpfile_to_extract(lte, is_partial_res)) {
1321 DEBUG("Temporary file not needed "
1322 "for stream (size=%"PRIu64")", lte->size);
1323 ret = extract_stream_instances(lte, lte, ctx);
1327 return BEGIN_STREAM_STATUS_SKIP_STREAM;
1330 DEBUG("Temporary file needed for stream (size=%"PRIu64")", lte->size);
1331 return create_temporary_file(&ctx->tmpfile_fd, &ctx->tmpfile_name);
1335 end_extract_stream_to_tmpfile(struct wim_lookup_table_entry *lte,
1336 int status, void *_ctx)
1338 struct apply_ctx *ctx = _ctx;
1339 struct wim_lookup_table_entry lte_override;
1341 int errno_save = errno;
1343 ret = filedes_close(&ctx->tmpfile_fd);
1348 goto out_delete_tmpfile;
1352 ERROR_WITH_ERRNO("Error writing temporary file %"TS, ctx->tmpfile_name);
1353 ret = WIMLIB_ERR_WRITE;
1354 goto out_delete_tmpfile;
1357 /* Now that a full stream has been extracted to a temporary file,
1358 * extract all instances of it to the actual target. */
1360 memcpy(<e_override, lte, sizeof(struct wim_lookup_table_entry));
1361 lte_override.resource_location = RESOURCE_IN_FILE_ON_DISK;
1362 lte_override.file_on_disk = ctx->tmpfile_name;
1364 ret = extract_stream_instances(lte, <e_override, ctx);
1368 tunlink(ctx->tmpfile_name);
1369 FREE(ctx->tmpfile_name);
1374 /* Extracts a list of streams (ctx.stream_list), assuming that the directory
1375 * structure and empty files were already created. This relies on the
1376 * per-`struct wim_lookup_table_entry' list of dentries that reference each
1377 * stream that was constructed earlier. */
1379 extract_stream_list(struct apply_ctx *ctx)
1381 if (!(ctx->extract_flags & WIMLIB_EXTRACT_FLAG_FILE_ORDER)) {
1382 /* Sequential extraction: read the streams in the order in which
1383 * they appear in the WIM file. */
1384 struct read_stream_list_callbacks cbs = {
1385 .begin_stream = begin_extract_stream_to_tmpfile,
1386 .begin_stream_ctx = ctx,
1387 .consume_chunk = extract_chunk_to_fd,
1388 .consume_chunk_ctx = &ctx->tmpfile_fd,
1389 .end_stream = end_extract_stream_to_tmpfile,
1390 .end_stream_ctx = ctx,
1392 return read_stream_list(&ctx->stream_list,
1393 offsetof(struct wim_lookup_table_entry, extraction_list),
1394 &cbs, VERIFY_STREAM_HASHES);
1396 /* Extract the streams in unsorted order. */
1397 struct wim_lookup_table_entry *lte;
1400 list_for_each_entry(lte, &ctx->stream_list, extraction_list) {
1401 ret = extract_stream_instances(lte, lte, ctx);
1409 #define PWM_ALLOW_WIM_HDR 0x00001
1410 #define PWM_SILENT_EOF 0x00002
1412 /* Read the header from a stream in a pipable WIM. */
1414 read_pwm_stream_header(WIMStruct *pwm, struct wim_lookup_table_entry *lte,
1415 struct wim_resource_spec *rspec,
1416 int flags, struct wim_header_disk *hdr_ret)
1419 struct pwm_stream_hdr stream_hdr;
1420 struct wim_header_disk pwm_hdr;
1422 struct wim_reshdr reshdr;
1425 ret = full_read(&pwm->in_fd, &buf.stream_hdr, sizeof(buf.stream_hdr));
1429 if ((flags & PWM_ALLOW_WIM_HDR) && buf.stream_hdr.magic == PWM_MAGIC) {
1430 BUILD_BUG_ON(sizeof(buf.pwm_hdr) < sizeof(buf.stream_hdr));
1431 ret = full_read(&pwm->in_fd, &buf.stream_hdr + 1,
1432 sizeof(buf.pwm_hdr) - sizeof(buf.stream_hdr));
1436 lte->resource_location = RESOURCE_NONEXISTENT;
1437 memcpy(hdr_ret, &buf.pwm_hdr, sizeof(buf.pwm_hdr));
1441 if (le64_to_cpu(buf.stream_hdr.magic) != PWM_STREAM_MAGIC) {
1442 ERROR("Data read on pipe is invalid (expected stream header).");
1443 return WIMLIB_ERR_INVALID_PIPABLE_WIM;
1446 copy_hash(lte->hash, buf.stream_hdr.hash);
1448 reshdr.size_in_wim = 0;
1449 reshdr.flags = le32_to_cpu(buf.stream_hdr.flags);
1450 reshdr.offset_in_wim = pwm->in_fd.offset;
1451 reshdr.uncompressed_size = le64_to_cpu(buf.stream_hdr.uncompressed_size);
1452 wim_res_hdr_to_spec(&reshdr, pwm, rspec);
1453 lte_bind_wim_resource_spec(lte, rspec);
1454 lte->flags = rspec->flags;
1455 lte->size = rspec->uncompressed_size;
1456 lte->offset_in_res = 0;
1460 if (ret != WIMLIB_ERR_UNEXPECTED_END_OF_FILE || !(flags & PWM_SILENT_EOF))
1461 ERROR_WITH_ERRNO("Error reading pipable WIM from pipe");
1466 extract_streams_from_pipe(struct apply_ctx *ctx)
1468 struct wim_lookup_table_entry *found_lte;
1469 struct wim_resource_spec *rspec;
1470 struct wim_lookup_table_entry *needed_lte;
1471 struct wim_lookup_table *lookup_table;
1472 struct wim_header_disk pwm_hdr;
1476 ret = WIMLIB_ERR_NOMEM;
1477 found_lte = new_lookup_table_entry();
1478 if (found_lte == NULL)
1481 rspec = MALLOC(sizeof(struct wim_resource_spec));
1483 goto out_free_found_lte;
1485 lookup_table = ctx->wim->lookup_table;
1486 pwm_flags = PWM_ALLOW_WIM_HDR;
1487 if ((ctx->extract_flags & WIMLIB_EXTRACT_FLAG_RESUME))
1488 pwm_flags |= PWM_SILENT_EOF;
1489 memcpy(ctx->progress.extract.guid, ctx->wim->hdr.guid, WIM_GID_LEN);
1490 ctx->progress.extract.part_number = ctx->wim->hdr.part_number;
1491 ctx->progress.extract.total_parts = ctx->wim->hdr.total_parts;
1492 if (ctx->progress_func)
1493 ctx->progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN,
1495 while (ctx->num_streams_remaining) {
1496 if (found_lte->resource_location != RESOURCE_NONEXISTENT)
1497 lte_unbind_wim_resource_spec(found_lte);
1498 ret = read_pwm_stream_header(ctx->wim, found_lte, rspec,
1499 pwm_flags, &pwm_hdr);
1501 if (ret == WIMLIB_ERR_UNEXPECTED_END_OF_FILE &&
1502 (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_RESUME))
1506 goto out_free_found_lte;
1509 if ((found_lte->resource_location != RESOURCE_NONEXISTENT)
1510 && !(found_lte->flags & WIM_RESHDR_FLAG_METADATA)
1511 && (needed_lte = lookup_stream(lookup_table, found_lte->hash))
1512 && (needed_lte->out_refcnt))
1514 tchar *tmpfile_name = NULL;
1515 struct wim_lookup_table_entry *lte_override;
1516 struct wim_lookup_table_entry tmpfile_lte;
1518 needed_lte->offset_in_res = found_lte->offset_in_res;
1519 needed_lte->flags = found_lte->flags;
1520 needed_lte->size = found_lte->size;
1522 lte_unbind_wim_resource_spec(found_lte);
1523 lte_bind_wim_resource_spec(needed_lte, rspec);
1525 if (needed_lte->out_refcnt > 1) {
1527 struct filedes tmpfile_fd;
1529 /* Extract stream to temporary file. */
1530 ret = create_temporary_file(&tmpfile_fd, &tmpfile_name);
1532 lte_unbind_wim_resource_spec(needed_lte);
1533 goto out_free_found_lte;
1536 ret = extract_full_stream_to_fd(needed_lte,
1539 filedes_close(&tmpfile_fd);
1540 goto delete_tmpfile;
1543 if (filedes_close(&tmpfile_fd)) {
1544 ERROR_WITH_ERRNO("Error writing to temporary "
1545 "file \"%"TS"\"", tmpfile_name);
1546 ret = WIMLIB_ERR_WRITE;
1547 goto delete_tmpfile;
1549 memcpy(&tmpfile_lte, needed_lte,
1550 sizeof(struct wim_lookup_table_entry));
1551 tmpfile_lte.resource_location = RESOURCE_IN_FILE_ON_DISK;
1552 tmpfile_lte.file_on_disk = tmpfile_name;
1553 lte_override = &tmpfile_lte;
1555 lte_override = needed_lte;
1558 ret = extract_stream_instances(needed_lte, lte_override, ctx);
1560 lte_unbind_wim_resource_spec(needed_lte);
1562 tunlink(tmpfile_name);
1566 goto out_free_found_lte;
1567 ctx->num_streams_remaining--;
1568 } else if (found_lte->resource_location != RESOURCE_NONEXISTENT) {
1569 ret = skip_wim_stream(found_lte);
1571 goto out_free_found_lte;
1573 u16 part_number = le16_to_cpu(pwm_hdr.part_number);
1574 u16 total_parts = le16_to_cpu(pwm_hdr.total_parts);
1576 if (part_number != ctx->progress.extract.part_number ||
1577 total_parts != ctx->progress.extract.total_parts ||
1578 memcmp(pwm_hdr.guid, ctx->progress.extract.guid,
1581 ctx->progress.extract.part_number = part_number;
1582 ctx->progress.extract.total_parts = total_parts;
1583 memcpy(ctx->progress.extract.guid,
1584 pwm_hdr.guid, WIM_GID_LEN);
1585 if (ctx->progress_func) {
1587 WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN,
1596 if (found_lte->resource_location != RESOURCE_IN_WIM)
1598 free_lookup_table_entry(found_lte);
1607 /* Finish extracting a file, directory, or symbolic link by setting file
1608 * security and timestamps. */
1610 dentry_extract_final(struct wim_dentry *dentry, void *_ctx)
1612 struct apply_ctx *ctx = _ctx;
1614 tchar path[ctx->ops->path_max];
1616 if (!build_extraction_path(path, dentry, ctx))
1619 ret = extract_security(path, ctx, dentry);
1623 if (ctx->ops->requires_final_set_attributes_pass) {
1624 /* Set file attributes (if supported). */
1625 ret = extract_file_attributes(path, ctx, dentry, 1);
1630 return extract_timestamps(path, ctx, dentry);
1634 * Extract a WIM dentry to standard output.
1636 * This obviously doesn't make sense in all cases. We return an error if the
1637 * dentry does not correspond to a regular file. Otherwise we extract the
1638 * unnamed data stream only.
1641 extract_dentry_to_stdout(struct wim_dentry *dentry)
1644 if (dentry->d_inode->i_attributes & (FILE_ATTRIBUTE_REPARSE_POINT |
1645 FILE_ATTRIBUTE_DIRECTORY))
1647 ERROR("\"%"TS"\" is not a regular file and therefore cannot be "
1648 "extracted to standard output", dentry_full_path(dentry));
1649 ret = WIMLIB_ERR_NOT_A_REGULAR_FILE;
1651 struct wim_lookup_table_entry *lte;
1653 lte = inode_unnamed_lte_resolved(dentry->d_inode);
1655 struct filedes _stdout;
1656 filedes_init(&_stdout, STDOUT_FILENO);
1657 ret = extract_full_stream_to_fd(lte, &_stdout);
1664 static const utf16lechar replacement_char = cpu_to_le16(0xfffd);
1666 static const utf16lechar replacement_char = cpu_to_le16('?');
1670 file_name_valid(utf16lechar *name, size_t num_chars, bool fix)
1676 for (i = 0; i < num_chars; i++) {
1679 case cpu_to_le16('\\'):
1680 case cpu_to_le16(':'):
1681 case cpu_to_le16('*'):
1682 case cpu_to_le16('?'):
1683 case cpu_to_le16('"'):
1684 case cpu_to_le16('<'):
1685 case cpu_to_le16('>'):
1686 case cpu_to_le16('|'):
1688 case cpu_to_le16('/'):
1689 case cpu_to_le16('\0'):
1691 name[i] = replacement_char;
1698 if (name[num_chars - 1] == cpu_to_le16(' ') ||
1699 name[num_chars - 1] == cpu_to_le16('.'))
1702 name[num_chars - 1] = replacement_char;
1711 dentry_mark_skipped(struct wim_dentry *dentry, void *_ignore)
1713 dentry->extraction_skipped = 1;
1718 * dentry_calculate_extraction_path-
1720 * Calculate the actual filename component at which a WIM dentry will be
1721 * extracted, handling invalid filenames "properly".
1723 * dentry->extraction_name usually will be set the same as dentry->file_name (on
1724 * UNIX, converted into the platform's multibyte encoding). However, if the
1725 * file name contains characters that are not valid on the current platform or
1726 * has some other format that is not valid, leave dentry->extraction_name as
1727 * NULL and set dentry->extraction_skipped to indicate that this dentry should
1728 * not be extracted, unless the appropriate flag
1729 * WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES is set in the extract flags, in
1730 * which case a substitute filename will be created and set instead.
1732 * Conflicts with case-insensitive names on Windows are handled similarly; see
1736 dentry_calculate_extraction_path(struct wim_dentry *dentry, void *_args)
1738 struct apply_ctx *ctx = _args;
1741 if (dentry == ctx->extract_root || dentry->extraction_skipped)
1744 if (!dentry_is_supported(dentry, &ctx->supported_features))
1747 if (!ctx->ops->supports_case_sensitive_filenames)
1749 struct wim_dentry *other;
1750 list_for_each_entry(other, &dentry->case_insensitive_conflict_list,
1751 case_insensitive_conflict_list)
1753 if (!other->extraction_skipped) {
1754 if (ctx->extract_flags &
1755 WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS) {
1756 WARNING("\"%"TS"\" has the same "
1757 "case-insensitive name as "
1758 "\"%"TS"\"; extracting "
1759 "dummy name instead",
1760 dentry_full_path(dentry),
1761 dentry_full_path(other));
1764 WARNING("Not extracting \"%"TS"\": "
1765 "has same case-insensitive "
1766 "name as \"%"TS"\"",
1767 dentry_full_path(dentry),
1768 dentry_full_path(other));
1775 if (file_name_valid(dentry->file_name, dentry->file_name_nbytes / 2, false)) {
1776 #if TCHAR_IS_UTF16LE
1777 dentry->extraction_name = dentry->file_name;
1778 dentry->extraction_name_nchars = dentry->file_name_nbytes / 2;
1781 return utf16le_to_tstr(dentry->file_name,
1782 dentry->file_name_nbytes,
1783 &dentry->extraction_name,
1784 &dentry->extraction_name_nchars);
1787 if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES)
1789 WARNING("\"%"TS"\" has an invalid filename "
1790 "that is not supported on this platform; "
1791 "extracting dummy name instead",
1792 dentry_full_path(dentry));
1795 WARNING("Not extracting \"%"TS"\": has an invalid filename "
1796 "that is not supported on this platform",
1797 dentry_full_path(dentry));
1804 utf16lechar utf16_name_copy[dentry->file_name_nbytes / 2];
1806 memcpy(utf16_name_copy, dentry->file_name, dentry->file_name_nbytes);
1807 file_name_valid(utf16_name_copy, dentry->file_name_nbytes / 2, true);
1810 size_t tchar_nchars;
1811 #if TCHAR_IS_UTF16LE
1812 tchar_name = utf16_name_copy;
1813 tchar_nchars = dentry->file_name_nbytes / 2;
1815 ret = utf16le_to_tstr(utf16_name_copy,
1816 dentry->file_name_nbytes,
1817 &tchar_name, &tchar_nchars);
1821 size_t fixed_name_num_chars = tchar_nchars;
1822 tchar fixed_name[tchar_nchars + 50];
1824 tmemcpy(fixed_name, tchar_name, tchar_nchars);
1825 fixed_name_num_chars += tsprintf(fixed_name + tchar_nchars,
1826 T(" (invalid filename #%lu)"),
1827 ++ctx->invalid_sequence);
1828 #if !TCHAR_IS_UTF16LE
1831 dentry->extraction_name = memdup(fixed_name,
1832 2 * fixed_name_num_chars + 2);
1833 if (!dentry->extraction_name)
1834 return WIMLIB_ERR_NOMEM;
1835 dentry->extraction_name_nchars = fixed_name_num_chars;
1840 for_dentry_in_tree(dentry, dentry_mark_skipped, NULL);
1844 /* Clean up dentry and inode structure after extraction. */
1846 dentry_reset_needs_extraction(struct wim_dentry *dentry, void *_ignore)
1848 struct wim_inode *inode = dentry->d_inode;
1850 dentry->in_extraction_tree = 0;
1851 dentry->extraction_skipped = 0;
1852 dentry->was_hardlinked = 0;
1853 dentry->skeleton_extracted = 0;
1854 inode->i_visited = 0;
1855 FREE(inode->i_extracted_file);
1856 inode->i_extracted_file = NULL;
1857 inode->i_dos_name_extracted = 0;
1858 if ((void*)dentry->extraction_name != (void*)dentry->file_name)
1859 FREE(dentry->extraction_name);
1860 dentry->extraction_name = NULL;
1864 /* Tally features necessary to extract a dentry and the corresponding inode. */
1866 dentry_tally_features(struct wim_dentry *dentry, void *_features)
1868 struct wim_features *features = _features;
1869 struct wim_inode *inode = dentry->d_inode;
1871 if (dentry->extraction_skipped)
1874 if (inode->i_attributes & FILE_ATTRIBUTE_ARCHIVE)
1875 features->archive_files++;
1876 if (inode->i_attributes & FILE_ATTRIBUTE_HIDDEN)
1877 features->hidden_files++;
1878 if (inode->i_attributes & FILE_ATTRIBUTE_SYSTEM)
1879 features->system_files++;
1880 if (inode->i_attributes & FILE_ATTRIBUTE_COMPRESSED)
1881 features->compressed_files++;
1882 if (inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED) {
1883 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)
1884 features->encrypted_directories++;
1886 features->encrypted_files++;
1888 if (inode->i_attributes & FILE_ATTRIBUTE_NOT_CONTENT_INDEXED)
1889 features->not_context_indexed_files++;
1890 if (inode->i_attributes & FILE_ATTRIBUTE_SPARSE_FILE)
1891 features->sparse_files++;
1892 if (inode_has_named_stream(inode))
1893 features->named_data_streams++;
1894 if (inode->i_visited)
1895 features->hard_links++;
1896 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1897 features->reparse_points++;
1898 if (inode_is_symlink(inode))
1899 features->symlink_reparse_points++;
1901 features->other_reparse_points++;
1903 if (inode->i_security_id != -1)
1904 features->security_descriptors++;
1905 if (dentry->short_name_nbytes)
1906 features->short_names++;
1907 if (inode_has_unix_data(inode))
1908 features->unix_data++;
1909 inode->i_visited = 1;
1913 /* Tally the features necessary to extract a dentry tree. */
1915 dentry_tree_get_features(struct wim_dentry *root, struct wim_features *features)
1917 memset(features, 0, sizeof(struct wim_features));
1918 for_dentry_in_tree(root, dentry_tally_features, features);
1919 dentry_tree_clear_inode_visited(root);
1923 compute_supported_attributes_mask(const struct wim_features *supported_features)
1927 if (!supported_features->archive_files)
1928 mask &= ~FILE_ATTRIBUTE_ARCHIVE;
1930 if (!supported_features->hidden_files)
1931 mask &= ~FILE_ATTRIBUTE_HIDDEN;
1933 if (!supported_features->system_files)
1934 mask &= ~FILE_ATTRIBUTE_SYSTEM;
1936 if (!supported_features->not_context_indexed_files)
1937 mask &= ~FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
1939 if (!supported_features->compressed_files)
1940 mask &= ~FILE_ATTRIBUTE_COMPRESSED;
1942 if (!supported_features->sparse_files)
1943 mask &= ~FILE_ATTRIBUTE_SPARSE_FILE;
1945 if (!supported_features->reparse_points)
1946 mask &= ~FILE_ATTRIBUTE_REPARSE_POINT;
1952 do_feature_check(const struct wim_features *required_features,
1953 const struct wim_features *supported_features,
1955 const struct apply_operations *ops,
1956 const tchar *wim_source_path,
1960 const tchar *mode = T("this extraction mode");
1962 if (wim_source_path[0] == '\0')
1963 loc = T("the WIM image");
1965 loc = wim_source_path;
1968 /* We're an archive program, so theoretically we can do what we want
1969 * with FILE_ATTRIBUTE_ARCHIVE (which is a dumb flag anyway). Don't
1970 * bother the user about it. */
1972 if (required_features->archive_files && !supported_features->archive_files)
1975 "%lu files in %"TS" are marked as archived, but this attribute\n"
1976 " is not supported in %"TS".",
1977 required_features->archive_files, loc, mode);
1981 if (required_features->hidden_files && !supported_features->hidden_files)
1984 "%lu files in %"TS" are marked as hidden, but this\n"
1985 " attribute is not supported in %"TS".",
1986 required_features->hidden_files, loc, mode);
1989 if (required_features->system_files && !supported_features->system_files)
1992 "%lu files in %"TS" are marked as system files,\n"
1993 " but this attribute is not supported in %"TS".",
1994 required_features->system_files, loc, mode);
1997 if (required_features->compressed_files && !supported_features->compressed_files)
2000 "%lu files in %"TS" are marked as being transparently\n"
2001 " compressed, but transparent compression is not supported in\n"
2002 " %"TS". These files will be extracted as uncompressed.",
2003 required_features->compressed_files, loc, mode);
2006 if (required_features->encrypted_files && !supported_features->encrypted_files)
2009 "%lu files in %"TS" are marked as being encrypted,\n"
2010 " but encryption is not supported in %"TS". These files\n"
2011 " will not be extracted.",
2012 required_features->encrypted_files, loc, mode);
2015 if (required_features->encrypted_directories &&
2016 !supported_features->encrypted_directories)
2019 "%lu directories in %"TS" are marked as being encrypted,\n"
2020 " but encryption is not supported in %"TS".\n"
2021 " These directories will be extracted as unencrypted.",
2022 required_features->encrypted_directories, loc, mode);
2025 if (required_features->not_context_indexed_files &&
2026 !supported_features->not_context_indexed_files)
2029 "%lu files in %"TS" are marked as not content indexed,\n"
2030 " but this attribute is not supported in %"TS".",
2031 required_features->not_context_indexed_files, loc, mode);
2034 if (required_features->sparse_files && !supported_features->sparse_files)
2037 "%lu files in %"TS" are marked as sparse, but creating\n"
2038 " sparse files is not supported in %"TS". These files\n"
2039 " will be extracted as non-sparse.",
2040 required_features->sparse_files, loc, mode);
2043 if (required_features->named_data_streams &&
2044 !supported_features->named_data_streams)
2047 "%lu files in %"TS" contain one or more alternate (named)\n"
2048 " data streams, which are not supported in %"TS".\n"
2049 " Alternate data streams will NOT be extracted.",
2050 required_features->named_data_streams, loc, mode);
2053 if (unlikely(extract_flags & (WIMLIB_EXTRACT_FLAG_HARDLINK |
2054 WIMLIB_EXTRACT_FLAG_SYMLINK)) &&
2055 required_features->named_data_streams &&
2056 supported_features->named_data_streams)
2059 "%lu files in %"TS" contain one or more alternate (named)\n"
2060 " data streams, which are not supported in linked extraction mode.\n"
2061 " Alternate data streams will NOT be extracted.",
2062 required_features->named_data_streams, loc);
2065 if (required_features->hard_links && !supported_features->hard_links)
2068 "%lu files in %"TS" are hard links, but hard links are\n"
2069 " not supported in %"TS". Hard links will be extracted as\n"
2070 " duplicate copies of the linked files.",
2071 required_features->hard_links, loc, mode);
2074 if (required_features->reparse_points && !supported_features->reparse_points)
2076 if (supported_features->symlink_reparse_points) {
2077 if (required_features->other_reparse_points) {
2079 "%lu files in %"TS" are reparse points that are neither\n"
2080 " symbolic links nor junction points and are not supported in\n"
2081 " %"TS". These reparse points will not be extracted.",
2082 required_features->other_reparse_points, loc,
2087 "%lu files in %"TS" are reparse points, which are\n"
2088 " not supported in %"TS" and will not be extracted.",
2089 required_features->reparse_points, loc, mode);
2093 if (required_features->security_descriptors &&
2094 !supported_features->security_descriptors)
2097 "%lu files in %"TS" have Windows NT security descriptors,\n"
2098 " but extracting security descriptors is not supported in\n"
2099 " %"TS". No security descriptors will be extracted.",
2100 required_features->security_descriptors, loc, mode);
2103 if (required_features->short_names && !supported_features->short_names)
2106 "%lu files in %"TS" have short (DOS) names, but\n"
2107 " extracting short names is not supported in %"TS".\n"
2108 " Short names will not be extracted.\n",
2109 required_features->short_names, loc, mode);
2113 if ((extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) &&
2114 required_features->unix_data && !supported_features->unix_data)
2116 ERROR("Extracting UNIX data is not supported in %"TS, mode);
2117 return WIMLIB_ERR_UNSUPPORTED;
2119 if ((extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_SHORT_NAMES) &&
2120 required_features->short_names && !supported_features->short_names)
2122 ERROR("Extracting short names is not supported in %"TS"", mode);
2123 return WIMLIB_ERR_UNSUPPORTED;
2125 if ((extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_TIMESTAMPS) &&
2126 !ops->set_timestamps)
2128 ERROR("Extracting timestamps is not supported in %"TS"", mode);
2129 return WIMLIB_ERR_UNSUPPORTED;
2131 if (((extract_flags & (WIMLIB_EXTRACT_FLAG_STRICT_ACLS |
2132 WIMLIB_EXTRACT_FLAG_UNIX_DATA))
2133 == WIMLIB_EXTRACT_FLAG_STRICT_ACLS) &&
2134 required_features->security_descriptors &&
2135 !supported_features->security_descriptors)
2137 ERROR("Extracting security descriptors is not supported in %"TS, mode);
2138 return WIMLIB_ERR_UNSUPPORTED;
2141 if ((extract_flags & WIMLIB_EXTRACT_FLAG_HARDLINK) &&
2142 !supported_features->hard_links)
2144 ERROR("Hard link extraction mode requested, but "
2145 "%"TS" does not support hard links!", mode);
2146 return WIMLIB_ERR_UNSUPPORTED;
2149 if ((extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS) &&
2150 required_features->symlink_reparse_points &&
2151 !(supported_features->symlink_reparse_points ||
2152 supported_features->reparse_points))
2154 ERROR("Extracting symbolic links is not supported in %"TS, mode);
2155 return WIMLIB_ERR_UNSUPPORTED;
2158 if ((extract_flags & WIMLIB_EXTRACT_FLAG_SYMLINK) &&
2159 !supported_features->symlink_reparse_points)
2161 ERROR("Symbolic link extraction mode requested, but "
2162 "%"TS" does not support symbolic "
2164 return WIMLIB_ERR_UNSUPPORTED;
2170 do_extract_warnings(struct apply_ctx *ctx)
2172 if (ctx->partial_security_descriptors == 0 &&
2173 ctx->no_security_descriptors == 0)
2176 WARNING("Extraction to \"%"TS"\" complete, but with one or more warnings:",
2178 if (ctx->partial_security_descriptors != 0) {
2179 WARNING("- Could only partially set the security descriptor\n"
2180 " on %lu files or directories.",
2181 ctx->partial_security_descriptors);
2183 if (ctx->no_security_descriptors != 0) {
2184 WARNING("- Could not set security descriptor at all\n"
2185 " on %lu files or directories.",
2186 ctx->no_security_descriptors);
2189 WARNING("To fully restore all security descriptors, run the program\n"
2190 " with Administrator rights.");
2195 dentry_set_skipped(struct wim_dentry *dentry, void *_ignore)
2197 dentry->in_extraction_tree = 1;
2198 dentry->extraction_skipped = 1;
2203 dentry_set_not_skipped(struct wim_dentry *dentry, void *_ignore)
2205 dentry->in_extraction_tree = 1;
2206 dentry->extraction_skipped = 0;
2211 extract_trees(WIMStruct *wim, struct wim_dentry **trees, size_t num_trees,
2212 const tchar *wim_source_path, const tchar *target,
2213 int extract_flags, wimlib_progress_func_t progress_func)
2215 struct wim_features required_features;
2216 struct apply_ctx ctx;
2218 struct wim_lookup_table_entry *lte;
2220 wimlib_assert(num_trees != 0);
2221 wimlib_assert(target != NULL);
2224 wimlib_assert((extract_flags & WIMLIB_EXTRACT_FLAG_PATHMODE));
2226 /* Start initializing the apply_ctx. */
2227 memset(&ctx, 0, sizeof(struct apply_ctx));
2229 ctx.extract_flags = extract_flags;
2230 ctx.target = target;
2231 ctx.target_nchars = tstrlen(target);
2232 ctx.progress_func = progress_func;
2233 if (progress_func) {
2234 ctx.progress.extract.wimfile_name = wim->filename;
2235 ctx.progress.extract.image = wim->current_image;
2236 ctx.progress.extract.extract_flags = (extract_flags &
2237 WIMLIB_EXTRACT_MASK_PUBLIC);
2238 ctx.progress.extract.image_name = wimlib_get_image_name(wim,
2239 wim->current_image);
2240 ctx.progress.extract.extract_root_wim_source_path = wim_source_path;
2241 ctx.progress.extract.target = target;
2243 INIT_LIST_HEAD(&ctx.stream_list);
2245 if (extract_flags & WIMLIB_EXTRACT_FLAG_PATHMODE) {
2246 /* Path mode --- there may be multiple trees, and targets are
2247 * set relative to the root of the image.
2249 * Consider all dentries to be in the extraction tree, but
2250 * assume all to be skipped unless in one of the subtrees being
2251 * extracted or one of the ancestors of the subtrees up to the
2253 ctx.extract_root = wim_root_dentry(wim);
2254 for_dentry_in_tree(ctx.extract_root, dentry_set_skipped, NULL);
2256 for (size_t i = 0; i < num_trees; i++) {
2257 struct wim_dentry *d;
2259 for_dentry_in_tree(trees[i], dentry_set_not_skipped, NULL);
2261 while (d != ctx.extract_root) {
2263 dentry_set_not_skipped(d, NULL);
2267 ctx.extract_root = trees[0];
2268 for_dentry_in_tree(ctx.extract_root, dentry_set_not_skipped, NULL);
2271 /* Select the appropriate apply_operations based on the
2272 * platform and extract_flags. */
2274 ctx.ops = &win32_apply_ops;
2276 ctx.ops = &unix_apply_ops;
2280 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS)
2281 ctx.ops = &ntfs_3g_apply_ops;
2284 /* Call the start_extract() callback. This gives the apply_operations
2285 * implementation a chance to do any setup needed to access the volume.
2286 * Furthermore, it's expected to set the supported features of this
2287 * extraction mode (ctx.supported_features), which are determined at
2288 * runtime as they may vary depending on the actual volume. These
2289 * features are then compared with the actual features extracting this
2290 * dentry tree requires. Some mismatches will merely produce warnings
2291 * and the unsupported data will be ignored; others will produce errors.
2293 ret = ctx.ops->start_extract(target, &ctx);
2295 goto out_dentry_reset_needs_extraction;
2297 /* Get and check the features required to extract the dentry tree. */
2298 dentry_tree_get_features(ctx.extract_root, &required_features);
2299 ret = do_feature_check(&required_features, &ctx.supported_features,
2300 extract_flags, ctx.ops,
2302 !(extract_flags & WIMLIB_EXTRACT_FLAG_PATHMODE));
2304 goto out_finish_or_abort_extract;
2306 ctx.supported_attributes_mask =
2307 compute_supported_attributes_mask(&ctx.supported_features);
2309 /* Figure out whether the root dentry is being extracted to the root of
2310 * a volume and therefore needs to be treated "specially", for example
2311 * not being explicitly created and not having attributes set. */
2312 if (ctx.ops->target_is_root && ctx.ops->root_directory_is_special)
2313 ctx.root_dentry_is_special = ctx.ops->target_is_root(target);
2315 /* Calculate the actual filename component of each extracted dentry. In
2316 * the process, set the dentry->extraction_skipped flag on dentries that
2317 * are being skipped because of filename or supported features problems. */
2318 ret = for_dentry_in_tree(ctx.extract_root,
2319 dentry_calculate_extraction_path, &ctx);
2321 goto out_dentry_reset_needs_extraction;
2323 /* Build the list of the streams that need to be extracted and
2324 * initialize ctx.progress.extract with stream information. */
2325 ret = for_dentry_in_tree(ctx.extract_root,
2326 dentry_resolve_and_zero_lte_refcnt, &ctx);
2328 goto out_dentry_reset_needs_extraction;
2330 ret = for_dentry_in_tree(ctx.extract_root,
2331 dentry_add_streams_to_extract, &ctx);
2333 goto out_teardown_stream_list;
2335 if (extract_flags & WIMLIB_EXTRACT_FLAG_FROM_PIPE) {
2336 /* When extracting from a pipe, the number of bytes of data to
2337 * extract can't be determined in the normal way (examining the
2338 * lookup table), since at this point all we have is a set of
2339 * SHA1 message digests of streams that need to be extracted.
2340 * However, we can get a reasonably accurate estimate by taking
2341 * <TOTALBYTES> from the corresponding <IMAGE> in the WIM XML
2342 * data. This does assume that a full image is being extracted,
2343 * but currently there is no API for doing otherwise. (Also,
2344 * subtract <HARDLINKBYTES> from this if hard links are
2345 * supported by the extraction mode.) */
2346 ctx.progress.extract.total_bytes =
2347 wim_info_get_image_total_bytes(wim->wim_info,
2348 wim->current_image);
2349 if (ctx.supported_features.hard_links) {
2350 ctx.progress.extract.total_bytes -=
2351 wim_info_get_image_hard_link_bytes(wim->wim_info,
2352 wim->current_image);
2356 /* Handle the special case of extracting a file to standard
2357 * output. In that case, "root" should be a single file, not a
2358 * directory tree. (If not, extract_dentry_to_stdout() will
2359 * return an error.) */
2360 if (extract_flags & WIMLIB_EXTRACT_FLAG_TO_STDOUT) {
2362 for (size_t i = 0; i < num_trees; i++) {
2363 ret = extract_dentry_to_stdout(trees[i]);
2367 goto out_teardown_stream_list;
2370 if (ctx.ops->realpath_works_on_nonexisting_files &&
2371 ((extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX) ||
2372 ctx.ops->requires_realtarget_in_paths))
2374 ctx.realtarget = realpath(target, NULL);
2375 if (!ctx.realtarget) {
2376 ret = WIMLIB_ERR_NOMEM;
2377 goto out_teardown_stream_list;
2379 ctx.realtarget_nchars = tstrlen(ctx.realtarget);
2382 if (progress_func) {
2384 if (*wim_source_path || (extract_flags & WIMLIB_EXTRACT_FLAG_PATHMODE))
2385 msg = WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN;
2387 msg = WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN;
2388 progress_func(msg, &ctx.progress);
2391 if (!ctx.root_dentry_is_special)
2393 tchar path[ctx.ops->path_max];
2394 if (build_extraction_path(path, ctx.extract_root, &ctx))
2396 ret = extract_inode(path, &ctx, ctx.extract_root->d_inode);
2398 goto out_free_realtarget;
2402 /* If we need to fix up the targets of absolute symbolic links
2403 * (WIMLIB_EXTRACT_FLAG_RPFIX) or the extraction mode requires paths to
2404 * be absolute, use realpath() (or its replacement on Windows) to get
2405 * the absolute path to the extraction target. Note that this requires
2406 * the target directory to exist, unless
2407 * realpath_works_on_nonexisting_files is set in the apply_operations.
2409 if (!ctx.realtarget &&
2410 (((extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX) &&
2411 required_features.symlink_reparse_points) ||
2412 ctx.ops->requires_realtarget_in_paths))
2414 ctx.realtarget = realpath(target, NULL);
2415 if (!ctx.realtarget) {
2416 ret = WIMLIB_ERR_NOMEM;
2417 goto out_free_realtarget;
2419 ctx.realtarget_nchars = tstrlen(ctx.realtarget);
2422 if (ctx.ops->requires_short_name_reordering) {
2423 ret = for_dentry_in_tree(ctx.extract_root, dentry_extract_dir_skeleton,
2426 goto out_free_realtarget;
2429 /* Finally, the important part: extract the tree of files. */
2430 if (!(extract_flags & WIMLIB_EXTRACT_FLAG_FILE_ORDER)) {
2431 /* Sequential extraction requested, so two passes are needed
2432 * (one for directory structure, one for streams.) */
2434 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN,
2437 if (!(extract_flags & WIMLIB_EXTRACT_FLAG_RESUME)) {
2438 ret = for_dentry_in_tree(ctx.extract_root, dentry_extract_skeleton, &ctx);
2440 goto out_free_realtarget;
2443 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_END,
2445 if (extract_flags & WIMLIB_EXTRACT_FLAG_FROM_PIPE)
2446 ret = extract_streams_from_pipe(&ctx);
2448 ret = extract_stream_list(&ctx);
2450 goto out_free_realtarget;
2452 /* Sequential extraction was not requested, so we can make do
2453 * with one pass where we both create the files and extract
2456 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN,
2458 ret = for_dentry_in_tree(ctx.extract_root, dentry_extract, &ctx);
2460 goto out_free_realtarget;
2462 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_END,
2466 /* If the total number of bytes to extract was miscalculated, just jump
2467 * to the calculated number in order to avoid confusing the progress
2468 * function. This should only occur when extracting from a pipe. */
2469 if (ctx.progress.extract.completed_bytes != ctx.progress.extract.total_bytes)
2471 DEBUG("Calculated %"PRIu64" bytes to extract, but actually "
2472 "extracted %"PRIu64,
2473 ctx.progress.extract.total_bytes,
2474 ctx.progress.extract.completed_bytes);
2476 if (progress_func &&
2477 ctx.progress.extract.completed_bytes < ctx.progress.extract.total_bytes)
2479 ctx.progress.extract.completed_bytes = ctx.progress.extract.total_bytes;
2480 progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS, &ctx.progress);
2483 /* Apply security descriptors and timestamps. This is done at the end,
2484 * and in a depth-first manner, to prevent timestamps from getting
2485 * changed by subsequent extract operations and to minimize the chance
2486 * of the restored security descriptors getting in our way. */
2488 progress_func(WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS,
2490 ret = for_dentry_in_tree_depth(ctx.extract_root, dentry_extract_final, &ctx);
2492 goto out_free_realtarget;
2494 if (progress_func) {
2496 if (*wim_source_path || (extract_flags & WIMLIB_EXTRACT_FLAG_PATHMODE))
2497 msg = WIMLIB_PROGRESS_MSG_EXTRACT_TREE_END;
2499 msg = WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END;
2500 progress_func(msg, &ctx.progress);
2503 do_extract_warnings(&ctx);
2506 out_free_realtarget:
2507 FREE(ctx.realtarget);
2508 out_teardown_stream_list:
2509 /* Free memory allocated as part of the mapping from each
2510 * wim_lookup_table_entry to the dentries that reference it. */
2511 if (!(ctx.extract_flags & WIMLIB_EXTRACT_FLAG_FILE_ORDER))
2512 list_for_each_entry(lte, &ctx.stream_list, extraction_list)
2513 if (lte->out_refcnt > ARRAY_LEN(lte->inline_lte_dentries))
2514 FREE(lte->lte_dentries);
2515 out_finish_or_abort_extract:
2517 if (ctx.ops->abort_extract)
2518 ctx.ops->abort_extract(&ctx);
2520 if (ctx.ops->finish_extract)
2521 ret = ctx.ops->finish_extract(&ctx);
2523 out_dentry_reset_needs_extraction:
2524 for_dentry_in_tree(ctx.extract_root, dentry_reset_needs_extraction, NULL);
2529 * extract_tree - Extract a file or directory tree from the currently selected
2532 * @wim: WIMStruct for the WIM file, with the desired image selected
2533 * (as wim->current_image).
2536 * "Canonical" (i.e. no leading or trailing slashes, path
2537 * separators WIM_PATH_SEPARATOR) path inside the WIM image to
2538 * extract. An empty string means the full image.
2541 * Filesystem path to extract the file or directory tree to.
2542 * (Or, with WIMLIB_EXTRACT_FLAG_NTFS: the name of a NTFS volume.)
2545 * WIMLIB_EXTRACT_FLAG_*. Also, the private flag
2546 * WIMLIB_EXTRACT_FLAG_MULTI_IMAGE will be set if this is being
2547 * called through wimlib_extract_image() with WIMLIB_ALL_IMAGES as
2551 * If non-NULL, progress function for the extraction. The messages
2552 * that may be sent in this function are:
2554 * WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN or
2555 * WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN;
2556 * WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN;
2557 * WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_END;
2558 * WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY;
2559 * WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS;
2560 * WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS;
2561 * WIMLIB_PROGRESS_MSG_EXTRACT_TREE_END or
2562 * WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END.
2564 * Returns 0 on success; a positive WIMLIB_ERR_* code on failure.
2567 extract_tree(WIMStruct *wim, const tchar *wim_source_path,
2568 const tchar *target, int extract_flags,
2569 wimlib_progress_func_t progress_func)
2571 struct wim_dentry *root;
2573 root = get_dentry(wim, wim_source_path, WIMLIB_CASE_PLATFORM_DEFAULT);
2576 ERROR("Path \"%"TS"\" does not exist in WIM image %d",
2577 wim_source_path, wim->current_image);
2578 return WIMLIB_ERR_PATH_DOES_NOT_EXIST;
2580 return extract_trees(wim, &root, 1, wim_source_path,
2581 target, extract_flags, progress_func);
2584 /* Make sure the extraction flags make sense, and update them if needed. */
2586 check_extract_flags(int extract_flags,
2587 const u32 wim_header_flags,
2588 int *updated_extract_flags_ret)
2590 /* Check for invalid flag combinations */
2591 if ((extract_flags &
2592 (WIMLIB_EXTRACT_FLAG_SYMLINK |
2593 WIMLIB_EXTRACT_FLAG_HARDLINK)) == (WIMLIB_EXTRACT_FLAG_SYMLINK |
2594 WIMLIB_EXTRACT_FLAG_HARDLINK))
2595 return WIMLIB_ERR_INVALID_PARAM;
2597 if ((extract_flags &
2598 (WIMLIB_EXTRACT_FLAG_NO_ACLS |
2599 WIMLIB_EXTRACT_FLAG_STRICT_ACLS)) == (WIMLIB_EXTRACT_FLAG_NO_ACLS |
2600 WIMLIB_EXTRACT_FLAG_STRICT_ACLS))
2601 return WIMLIB_ERR_INVALID_PARAM;
2603 if ((extract_flags &
2604 (WIMLIB_EXTRACT_FLAG_RPFIX |
2605 WIMLIB_EXTRACT_FLAG_NORPFIX)) == (WIMLIB_EXTRACT_FLAG_RPFIX |
2606 WIMLIB_EXTRACT_FLAG_NORPFIX))
2607 return WIMLIB_ERR_INVALID_PARAM;
2609 if ((extract_flags &
2610 (WIMLIB_EXTRACT_FLAG_RESUME |
2611 WIMLIB_EXTRACT_FLAG_FROM_PIPE)) == WIMLIB_EXTRACT_FLAG_RESUME)
2612 return WIMLIB_ERR_INVALID_PARAM;
2614 #ifndef WITH_NTFS_3G
2615 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
2616 ERROR("wimlib was compiled without support for NTFS-3g, so\n"
2617 " we cannot apply a WIM image directly to a NTFS volume.");
2618 return WIMLIB_ERR_UNSUPPORTED;
2622 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_RPFIX |
2623 WIMLIB_EXTRACT_FLAG_NORPFIX)) == 0)
2625 /* Do reparse point fixups by default if the WIM header says
2626 * they are enabled and we are extracting a full image. */
2627 if (wim_header_flags & WIM_HDR_FLAG_RP_FIX)
2628 extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
2631 /* TODO: Since UNIX data entries are stored in the file resources, in a
2632 * completely sequential extraction they may come up before the
2633 * corresponding file or symbolic link data. This needs to be handled
2635 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_UNIX_DATA |
2636 WIMLIB_EXTRACT_FLAG_FILE_ORDER))
2637 == WIMLIB_EXTRACT_FLAG_UNIX_DATA)
2639 if (extract_flags & WIMLIB_EXTRACT_FLAG_FROM_PIPE) {
2640 WARNING("Setting UNIX file/owner group may "
2641 "be impossible on some\n"
2643 "when applying from a pipe.");
2645 extract_flags |= WIMLIB_EXTRACT_FLAG_FILE_ORDER;
2646 WARNING("Disabling sequential extraction for "
2651 if (updated_extract_flags_ret)
2652 *updated_extract_flags_ret = extract_flags;
2657 /* Internal function to execute extraction commands for a WIM image. The paths
2658 * in the extract commands are expected to be already "canonicalized". */
2660 do_wimlib_extract_files(WIMStruct *wim,
2662 struct wimlib_extract_command *cmds,
2664 wimlib_progress_func_t progress_func)
2667 bool found_link_cmd = false;
2668 bool found_nolink_cmd = false;
2670 /* Select the image from which we are extracting files */
2671 ret = select_wim_image(wim, image);
2675 /* Make sure there are no streams in the WIM that have not been
2676 * checksummed yet. */
2677 ret = wim_checksum_unhashed_streams(wim);
2681 /* Check for problems with the extraction commands */
2682 for (size_t i = 0; i < num_cmds; i++) {
2684 if (cmds[i].fs_dest_path[0] == T('\0'))
2685 return WIMLIB_ERR_INVALID_PARAM;
2687 if (cmds[i].extract_flags & WIMLIB_EXTRACT_FLAG_GLOB_PATHS)
2688 return WIMLIB_ERR_INVALID_PARAM;
2690 ret = check_extract_flags(cmds[i].extract_flags,
2692 &cmds[i].extract_flags);
2695 if (cmds[i].extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
2696 WIMLIB_EXTRACT_FLAG_HARDLINK)) {
2697 found_link_cmd = true;
2699 found_nolink_cmd = true;
2701 if (found_link_cmd && found_nolink_cmd) {
2702 ERROR("Symlink or hardlink extraction mode must "
2703 "be set on all extraction commands");
2704 return WIMLIB_ERR_INVALID_PARAM;
2708 /* Execute the extraction commands */
2709 for (size_t i = 0; i < num_cmds; i++) {
2710 ret = extract_tree(wim,
2711 cmds[i].wim_source_path,
2712 cmds[i].fs_dest_path,
2713 cmds[i].extract_flags,
2721 /* API function documented in wimlib.h */
2723 wimlib_extract_files(WIMStruct *wim,
2725 const struct wimlib_extract_command *cmds,
2727 int default_extract_flags,
2728 wimlib_progress_func_t progress_func)
2731 struct wimlib_extract_command *cmds_copy;
2734 default_extract_flags &= WIMLIB_EXTRACT_MASK_PUBLIC;
2739 cmds_copy = CALLOC(num_cmds, sizeof(cmds[0]));
2741 return WIMLIB_ERR_NOMEM;
2743 for (size_t i = 0; i < num_cmds; i++) {
2744 cmds_copy[i].extract_flags = (default_extract_flags |
2745 cmds[i].extract_flags)
2746 & WIMLIB_EXTRACT_MASK_PUBLIC;
2747 all_flags |= cmds_copy[i].extract_flags;
2749 cmds_copy[i].wim_source_path = canonicalize_wim_path(cmds[i].wim_source_path);
2750 if (!cmds_copy[i].wim_source_path) {
2751 ret = WIMLIB_ERR_NOMEM;
2752 goto out_free_cmds_copy;
2755 cmds_copy[i].fs_dest_path = canonicalize_fs_path(cmds[i].fs_dest_path);
2756 if (!cmds_copy[i].fs_dest_path) {
2757 ret = WIMLIB_ERR_NOMEM;
2758 goto out_free_cmds_copy;
2762 ret = do_wimlib_extract_files(wim, image,
2763 cmds_copy, num_cmds,
2766 if (all_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
2767 WIMLIB_EXTRACT_FLAG_HARDLINK))
2769 for_lookup_table_entry(wim->lookup_table,
2770 lte_free_extracted_file, NULL);
2773 for (size_t i = 0; i < num_cmds; i++) {
2774 FREE(cmds_copy[i].wim_source_path);
2775 FREE(cmds_copy[i].fs_dest_path);
2782 * Extracts an image from a WIM file.
2784 * @wim: WIMStruct for the WIM file.
2786 * @image: Number of the single image to extract.
2788 * @target: Directory or NTFS volume to extract the image to.
2790 * @extract_flags: Bitwise or of WIMLIB_EXTRACT_FLAG_*.
2792 * @progress_func: If non-NULL, a progress function to be called
2795 * Returns 0 on success; nonzero on failure.
2798 extract_single_image(WIMStruct *wim, int image,
2799 const tchar *target, int extract_flags,
2800 wimlib_progress_func_t progress_func)
2803 tchar *target_copy = canonicalize_fs_path(target);
2804 if (target_copy == NULL)
2805 return WIMLIB_ERR_NOMEM;
2806 struct wimlib_extract_command cmd = {
2807 .wim_source_path = T(""),
2808 .fs_dest_path = target_copy,
2809 .extract_flags = extract_flags,
2811 ret = do_wimlib_extract_files(wim, image, &cmd, 1, progress_func);
2816 static const tchar * const filename_forbidden_chars =
2825 /* This function checks if it is okay to use a WIM image's name as a directory
2828 image_name_ok_as_dir(const tchar *image_name)
2830 return image_name && *image_name &&
2831 !tstrpbrk(image_name, filename_forbidden_chars) &&
2832 tstrcmp(image_name, T(".")) &&
2833 tstrcmp(image_name, T(".."));
2836 /* Extracts all images from the WIM to the directory @target, with the images
2837 * placed in subdirectories named by their image names. */
2839 extract_all_images(WIMStruct *wim,
2840 const tchar *target,
2842 wimlib_progress_func_t progress_func)
2844 size_t image_name_max_len = max(xml_get_max_image_name_len(wim), 20);
2845 size_t output_path_len = tstrlen(target);
2846 tchar buf[output_path_len + 1 + image_name_max_len + 1];
2849 const tchar *image_name;
2852 extract_flags |= WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
2854 if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
2855 ERROR("Cannot extract multiple images in NTFS extraction mode.");
2856 return WIMLIB_ERR_INVALID_PARAM;
2859 if (tstat(target, &stbuf)) {
2860 if (errno == ENOENT) {
2861 if (tmkdir(target, 0755)) {
2862 ERROR_WITH_ERRNO("Failed to create directory \"%"TS"\"", target);
2863 return WIMLIB_ERR_MKDIR;
2866 ERROR_WITH_ERRNO("Failed to stat \"%"TS"\"", target);
2867 return WIMLIB_ERR_STAT;
2869 } else if (!S_ISDIR(stbuf.st_mode)) {
2870 ERROR("\"%"TS"\" is not a directory", target);
2871 return WIMLIB_ERR_NOTDIR;
2874 tmemcpy(buf, target, output_path_len);
2875 buf[output_path_len] = OS_PREFERRED_PATH_SEPARATOR;
2876 for (image = 1; image <= wim->hdr.image_count; image++) {
2877 image_name = wimlib_get_image_name(wim, image);
2878 if (image_name_ok_as_dir(image_name)) {
2879 tstrcpy(buf + output_path_len + 1, image_name);
2881 /* Image name is empty or contains forbidden characters.
2882 * Use image number instead. */
2883 tsprintf(buf + output_path_len + 1, T("%d"), image);
2885 ret = extract_single_image(wim, image, buf, extract_flags,
2894 do_wimlib_extract_image(WIMStruct *wim,
2896 const tchar *target,
2898 wimlib_progress_func_t progress_func)
2902 if (image == WIMLIB_ALL_IMAGES) {
2903 ret = extract_all_images(wim, target, extract_flags,
2906 ret = extract_single_image(wim, image, target, extract_flags,
2910 if (extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
2911 WIMLIB_EXTRACT_FLAG_HARDLINK))
2913 for_lookup_table_entry(wim->lookup_table,
2914 lte_free_extracted_file,
2920 /* API function documented in wimlib.h */
2922 wimlib_extract_image_from_pipe(int pipe_fd, const tchar *image_num_or_name,
2923 const tchar *target, int extract_flags,
2924 wimlib_progress_func_t progress_func)
2928 struct filedes *in_fd;
2932 extract_flags &= WIMLIB_EXTRACT_MASK_PUBLIC;
2934 if (extract_flags & WIMLIB_EXTRACT_FLAG_TO_STDOUT)
2935 return WIMLIB_ERR_INVALID_PARAM;
2937 /* Read the WIM header from the pipe and get a WIMStruct to represent
2938 * the pipable WIM. Caveats: Unlike getting a WIMStruct with
2939 * wimlib_open_wim(), getting a WIMStruct in this way will result in
2940 * an empty lookup table, no XML data read, and no filename set. */
2941 ret = open_wim_as_WIMStruct(&pipe_fd,
2942 WIMLIB_OPEN_FLAG_FROM_PIPE,
2943 &pwm, progress_func);
2947 /* Sanity check to make sure this is a pipable WIM. */
2948 if (pwm->hdr.magic != PWM_MAGIC) {
2949 ERROR("The WIM being read from file descriptor %d "
2950 "is not pipable!", pipe_fd);
2951 ret = WIMLIB_ERR_NOT_PIPABLE;
2952 goto out_wimlib_free;
2955 /* Sanity check to make sure the first part of a pipable split WIM is
2956 * sent over the pipe first. */
2957 if (pwm->hdr.part_number != 1) {
2958 ERROR("The first part of the split WIM must be "
2959 "sent over the pipe first.");
2960 ret = WIMLIB_ERR_INVALID_PIPABLE_WIM;
2961 goto out_wimlib_free;
2964 in_fd = &pwm->in_fd;
2965 wimlib_assert(in_fd->offset == WIM_HEADER_DISK_SIZE);
2967 /* As mentioned, the WIMStruct we created from the pipe does not have
2968 * XML data yet. Fix this by reading the extra copy of the XML data
2969 * that directly follows the header in pipable WIMs. (Note: see
2970 * write_pipable_wim() for more details about the format of pipable
2973 struct wim_lookup_table_entry xml_lte;
2974 struct wim_resource_spec xml_rspec;
2975 ret = read_pwm_stream_header(pwm, &xml_lte, &xml_rspec, 0, NULL);
2977 goto out_wimlib_free;
2979 if (!(xml_lte.flags & WIM_RESHDR_FLAG_METADATA))
2981 ERROR("Expected XML data, but found non-metadata "
2983 ret = WIMLIB_ERR_INVALID_PIPABLE_WIM;
2984 goto out_wimlib_free;
2987 wim_res_spec_to_hdr(&xml_rspec, &pwm->hdr.xml_data_reshdr);
2989 ret = read_wim_xml_data(pwm);
2991 goto out_wimlib_free;
2993 if (wim_info_get_num_images(pwm->wim_info) != pwm->hdr.image_count) {
2994 ERROR("Image count in XML data is not the same as in WIM header.");
2995 ret = WIMLIB_ERR_XML;
2996 goto out_wimlib_free;
3000 /* Get image index (this may use the XML data that was just read to
3001 * resolve an image name). */
3002 if (image_num_or_name) {
3003 image = wimlib_resolve_image(pwm, image_num_or_name);
3004 if (image == WIMLIB_NO_IMAGE) {
3005 ERROR("\"%"TS"\" is not a valid image in the pipable WIM!",
3007 ret = WIMLIB_ERR_INVALID_IMAGE;
3008 goto out_wimlib_free;
3009 } else if (image == WIMLIB_ALL_IMAGES) {
3010 ERROR("Applying all images from a pipe is not supported.");
3011 ret = WIMLIB_ERR_INVALID_IMAGE;
3012 goto out_wimlib_free;
3015 if (pwm->hdr.image_count != 1) {
3016 ERROR("No image was specified, but the pipable WIM "
3017 "did not contain exactly 1 image");
3018 ret = WIMLIB_ERR_INVALID_IMAGE;
3019 goto out_wimlib_free;
3024 /* Load the needed metadata resource. */
3025 for (i = 1; i <= pwm->hdr.image_count; i++) {
3026 struct wim_lookup_table_entry *metadata_lte;
3027 struct wim_image_metadata *imd;
3028 struct wim_resource_spec *metadata_rspec;
3030 metadata_lte = new_lookup_table_entry();
3031 if (metadata_lte == NULL) {
3032 ret = WIMLIB_ERR_NOMEM;
3033 goto out_wimlib_free;
3035 metadata_rspec = MALLOC(sizeof(struct wim_resource_spec));
3036 if (metadata_rspec == NULL) {
3037 ret = WIMLIB_ERR_NOMEM;
3038 free_lookup_table_entry(metadata_lte);
3039 goto out_wimlib_free;
3042 ret = read_pwm_stream_header(pwm, metadata_lte, metadata_rspec, 0, NULL);
3043 imd = pwm->image_metadata[i - 1];
3044 imd->metadata_lte = metadata_lte;
3046 FREE(metadata_rspec);
3047 goto out_wimlib_free;
3050 if (!(metadata_lte->flags & WIM_RESHDR_FLAG_METADATA)) {
3051 ERROR("Expected metadata resource, but found "
3052 "non-metadata stream.");
3053 ret = WIMLIB_ERR_INVALID_PIPABLE_WIM;
3054 goto out_wimlib_free;
3058 /* Metadata resource is for the images being extracted.
3059 * Parse it and save the metadata in memory. */
3060 ret = read_metadata_resource(pwm, imd);
3062 goto out_wimlib_free;
3065 /* Metadata resource is not for the image being
3066 * extracted. Skip over it. */
3067 ret = skip_wim_stream(metadata_lte);
3069 goto out_wimlib_free;
3072 /* Extract the image. */
3073 extract_flags |= WIMLIB_EXTRACT_FLAG_FROM_PIPE;
3074 ret = do_wimlib_extract_image(pwm, image, target,
3075 extract_flags, progress_func);
3076 /* Clean up and return. */
3082 /* API function documented in wimlib.h */
3084 wimlib_extract_image(WIMStruct *wim,
3086 const tchar *target,
3088 wimlib_progress_func_t progress_func)
3090 extract_flags &= WIMLIB_EXTRACT_MASK_PUBLIC;
3091 return do_wimlib_extract_image(wim, image, target, extract_flags,
3095 /* API function documented in wimlib.h */
3097 wimlib_extract_pathlist(WIMStruct *wim, int image,
3098 const tchar *target,
3099 const tchar *path_list_file,
3101 wimlib_progress_func_t progress_func)
3108 ret = read_path_list_file(path_list_file, &paths, &num_paths, &mem);
3110 ERROR("Failed to read path list file \"%"TS"\"",
3115 ret = wimlib_extract_paths(wim, image, target,
3116 (const tchar * const *)paths, num_paths,
3117 extract_flags, progress_func);
3123 struct append_dentry_ctx {
3124 struct wim_dentry **dentries;
3125 size_t num_dentries;
3126 size_t num_alloc_dentries;
3130 append_dentry_cb(struct wim_dentry *dentry, void *_ctx)
3132 struct append_dentry_ctx *ctx = _ctx;
3134 if (ctx->num_dentries == ctx->num_alloc_dentries) {
3135 struct wim_dentry **new_dentries;
3138 new_length = max(ctx->num_alloc_dentries + 8,
3139 ctx->num_alloc_dentries * 3 / 2);
3140 new_dentries = REALLOC(ctx->dentries,
3141 new_length * sizeof(ctx->dentries[0]));
3142 if (new_dentries == NULL)
3143 return WIMLIB_ERR_NOMEM;
3144 ctx->dentries = new_dentries;
3145 ctx->num_alloc_dentries = new_length;
3147 ctx->dentries[ctx->num_dentries++] = dentry;
3151 /* API function documented in wimlib.h */
3153 wimlib_extract_paths(WIMStruct *wim,
3155 const tchar *target,
3156 const tchar * const *paths,
3159 wimlib_progress_func_t progress_func)
3162 struct wim_dentry **trees;
3165 extract_flags &= WIMLIB_EXTRACT_MASK_PUBLIC;
3167 if (wim == NULL || target == NULL || target[0] == T('\0') ||
3168 (num_paths != 0 && paths == NULL))
3169 return WIMLIB_ERR_INVALID_PARAM;
3171 ret = check_extract_flags(extract_flags, wim->hdr.flags,
3176 ret = select_wim_image(wim, image);
3180 if (extract_flags & WIMLIB_EXTRACT_FLAG_GLOB_PATHS) {
3182 struct append_dentry_ctx append_dentry_ctx = {
3185 .num_alloc_dentries = 0,
3188 u32 wildcard_flags = 0;
3190 if (extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_GLOB)
3191 wildcard_flags |= WILDCARD_FLAG_ERROR_IF_NO_MATCH;
3193 wildcard_flags |= WILDCARD_FLAG_WARN_IF_NO_MATCH;
3195 if (default_ignore_case)
3196 wildcard_flags |= WILDCARD_FLAG_CASE_INSENSITIVE;
3198 for (size_t i = 0; i < num_paths; i++) {
3199 tchar *path = canonicalize_wim_path(paths[i]);
3201 ret = WIMLIB_ERR_NOMEM;
3202 trees = append_dentry_ctx.dentries;
3203 goto out_free_trees;
3205 ret = expand_wildcard(wim, path,
3211 trees = append_dentry_ctx.dentries;
3212 goto out_free_trees;
3215 trees = append_dentry_ctx.dentries;
3216 num_trees = append_dentry_ctx.num_dentries;
3218 trees = MALLOC(num_paths * sizeof(trees[0]));
3220 return WIMLIB_ERR_NOMEM;
3222 for (size_t i = 0; i < num_paths; i++) {
3224 tchar *path = canonicalize_wim_path(paths[i]);
3226 ret = WIMLIB_ERR_NOMEM;
3227 goto out_free_trees;
3230 trees[i] = get_dentry(wim, path,
3231 WIMLIB_CASE_PLATFORM_DEFAULT);
3233 if (trees[i] == NULL) {
3234 ERROR("Path \"%"TS"\" does not exist "
3236 paths[i], wim->current_image);
3237 ret = WIMLIB_ERR_PATH_DOES_NOT_EXIST;
3238 goto out_free_trees;
3241 num_trees = num_paths;
3244 if (num_trees == 0) {
3246 goto out_free_trees;
3249 ret = extract_trees(wim, trees, num_trees,
3252 ~WIMLIB_EXTRACT_FLAG_GLOB_PATHS)
3253 | WIMLIB_EXTRACT_FLAG_PATHMODE),
3256 if (extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
3257 WIMLIB_EXTRACT_FLAG_HARDLINK))
3259 for_lookup_table_entry(wim->lookup_table,
3260 lte_free_extracted_file,