2 * win32_apply.c - Windows-specific code for applying files from a WIM image.
6 * Copyright (C) 2013-2016 Eric Biggers
8 * This file is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU Lesser General Public License as published by the Free
10 * Software Foundation; either version 3 of the License, or (at your option) any
13 * This file is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this file; if not, see http://www.gnu.org/licenses/.
28 #include "wimlib/win32_common.h"
30 #include "wimlib/apply.h"
31 #include "wimlib/assert.h"
32 #include "wimlib/blob_table.h"
33 #include "wimlib/capture.h" /* for mangle_pat() and match_pattern_list() */
34 #include "wimlib/dentry.h"
35 #include "wimlib/encoding.h"
36 #include "wimlib/error.h"
37 #include "wimlib/metadata.h"
38 #include "wimlib/paths.h"
39 #include "wimlib/pattern.h"
40 #include "wimlib/reparse.h"
41 #include "wimlib/textfile.h"
42 #include "wimlib/xml.h"
43 #include "wimlib/wimboot.h"
44 #include "wimlib/wof.h"
46 struct win32_apply_ctx {
48 /* Extract flags, the pointer to the WIMStruct, etc. */
49 struct apply_ctx common;
51 /* WIMBoot information, only filled in if WIMLIB_EXTRACT_FLAG_WIMBOOT
54 /* This array contains the WIM files registered with WOF on the
55 * target volume for this extraction operation. All WIMStructs
56 * in this array are distinct and have ->filename != NULL. */
60 u8 blob_table_hash[SHA1_HASH_SIZE];
64 bool have_wrong_version_wims;
65 bool have_uncompressed_wims;
66 bool have_unsupported_compressed_resources;
67 bool have_huge_resources;
70 /* External backing information */
71 struct string_set *prepopulate_pats;
72 void *mem_prepopulate_pats;
73 bool tried_to_load_prepopulate_list;
75 /* Open handle to the target directory */
78 /* NT namespace path to the target directory (buffer allocated) */
79 UNICODE_STRING target_ntpath;
81 /* Temporary buffer for building paths (buffer allocated) */
82 UNICODE_STRING pathbuf;
84 /* Object attributes to reuse for opening files in the target directory.
85 * (attr.ObjectName == &pathbuf) and (attr.RootDirectory == h_target).
87 OBJECT_ATTRIBUTES attr;
89 /* Temporary I/O status block for system calls */
92 /* Allocated buffer for creating "printable" paths from our
93 * target-relative NT paths */
94 wchar_t *print_buffer;
96 /* Allocated buffer for reading blob data when it cannot be extracted
100 /* Pointer to the next byte in @data_buffer to fill */
103 /* Size allocated in @data_buffer */
104 size_t data_buffer_size;
106 /* Current offset in the raw encrypted file being written */
107 size_t encrypted_offset;
109 /* Current size of the raw encrypted file being written */
110 size_t encrypted_size;
112 /* Temporary buffer for reparse data */
113 struct reparse_buffer_disk rpbuf;
115 /* Temporary buffer for reparse data of "fixed" absolute symbolic links
117 struct reparse_buffer_disk rpfixbuf;
119 /* Array of open handles to filesystem streams currently being written
121 HANDLE open_handles[MAX_OPEN_FILES];
123 /* Number of handles in @open_handles currently open (filled in from the
124 * beginning of the array) */
125 unsigned num_open_handles;
127 /* List of dentries, joined by @d_tmp_list, that need to have reparse
128 * data extracted as soon as the whole blob has been read into
130 struct list_head reparse_dentries;
132 /* List of dentries, joined by @d_tmp_list, that need to have raw
133 * encrypted data extracted as soon as the whole blob has been read into
135 struct list_head encrypted_dentries;
137 /* Number of files for which we didn't have permission to set the full
138 * security descriptor. */
139 unsigned long partial_security_descriptors;
141 /* Number of files for which we didn't have permission to set any part
142 * of the security descriptor. */
143 unsigned long no_security_descriptors;
145 /* Number of files for which we couldn't set the short name. */
146 unsigned long num_set_short_name_failures;
148 /* Number of files for which we couldn't remove the short name. */
149 unsigned long num_remove_short_name_failures;
151 /* Number of files on which we couldn't set System Compression. */
152 unsigned long num_system_compression_failures;
154 /* The number of files which, for compatibility with the Windows
155 * bootloader, were not compressed using the requested system
156 * compression format. This includes matches with the hardcoded pattern
157 * list only; it does not include matches with patterns in
158 * [PrepopulateList]. */
159 unsigned long num_system_compression_exclusions;
161 /* The Windows build number of the image being applied, or 0 if unknown.
163 u64 windows_build_number;
165 /* Have we tried to enable short name support on the target volume yet?
167 bool tried_to_enable_short_names;
170 /* Get the drive letter from a Windows path, or return the null character if the
171 * path is relative. */
173 get_drive_letter(const wchar_t *path)
175 /* Skip \\?\ prefix */
176 if (!wcsncmp(path, L"\\\\?\\", 4))
179 /* Return drive letter if valid */
180 if (((path[0] >= L'a' && path[0] <= L'z') ||
181 (path[0] >= L'A' && path[0] <= L'Z')) && path[1] == L':')
188 get_vol_flags(const wchar_t *target, DWORD *vol_flags_ret,
189 bool *short_names_supported_ret)
191 wchar_t filesystem_name[MAX_PATH + 1];
193 wchar_t *volume = NULL;
196 *short_names_supported_ret = false;
198 drive[0] = get_drive_letter(target);
206 if (!GetVolumeInformation(volume, NULL, 0, NULL, NULL,
207 vol_flags_ret, filesystem_name,
208 ARRAY_LEN(filesystem_name)))
210 win32_warning(GetLastError(),
211 L"Failed to get volume information for \"%ls\"",
216 if (wcsstr(filesystem_name, L"NTFS")) {
217 /* FILE_SUPPORTS_HARD_LINKS is only supported on Windows 7 and
218 * later. Force it on anyway if filesystem is NTFS. */
219 *vol_flags_ret |= FILE_SUPPORTS_HARD_LINKS;
221 /* There's no volume flag for short names, but according to the
222 * MS documentation they are only user-settable on NTFS. */
223 *short_names_supported_ret = true;
227 /* Is the image being extracted an OS image for Windows 10 or later? */
229 is_image_windows_10_or_later(struct win32_apply_ctx *ctx)
231 /* Note: if no build number is available, this returns false. */
232 return ctx->windows_build_number >= 10240;
235 static const wchar_t *
236 current_path(struct win32_apply_ctx *ctx);
239 build_extraction_path(const struct wim_dentry *dentry,
240 struct win32_apply_ctx *ctx);
243 report_dentry_apply_error(const struct wim_dentry *dentry,
244 struct win32_apply_ctx *ctx, int ret)
246 build_extraction_path(dentry, ctx);
247 return report_apply_error(&ctx->common, ret, current_path(ctx));
251 check_apply_error(const struct wim_dentry *dentry,
252 struct win32_apply_ctx *ctx, int ret)
255 ret = report_dentry_apply_error(dentry, ctx, ret);
260 win32_get_supported_features(const wchar_t *target,
261 struct wim_features *supported_features)
264 bool short_names_supported;
266 /* Query the features of the target volume. */
268 get_vol_flags(target, &vol_flags, &short_names_supported);
270 supported_features->readonly_files = 1;
271 supported_features->hidden_files = 1;
272 supported_features->system_files = 1;
273 supported_features->archive_files = 1;
275 if (vol_flags & FILE_FILE_COMPRESSION)
276 supported_features->compressed_files = 1;
278 if (vol_flags & FILE_SUPPORTS_ENCRYPTION) {
279 supported_features->encrypted_files = 1;
280 supported_features->encrypted_directories = 1;
283 supported_features->not_context_indexed_files = 1;
285 /* Don't do anything with FILE_SUPPORTS_SPARSE_FILES. */
287 if (vol_flags & FILE_NAMED_STREAMS)
288 supported_features->named_data_streams = 1;
290 if (vol_flags & FILE_SUPPORTS_HARD_LINKS)
291 supported_features->hard_links = 1;
293 if (vol_flags & FILE_SUPPORTS_REPARSE_POINTS)
294 supported_features->reparse_points = 1;
296 if (vol_flags & FILE_PERSISTENT_ACLS)
297 supported_features->security_descriptors = 1;
299 if (short_names_supported)
300 supported_features->short_names = 1;
302 supported_features->timestamps = 1;
304 /* Note: Windows does not support case sensitive filenames! At least
305 * not without changing the registry and rebooting... */
310 #define COMPACT_FLAGS (WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS4K | \
311 WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS8K | \
312 WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS16K | \
313 WIMLIB_EXTRACT_FLAG_COMPACT_LZX)
318 * If not done already, load the patterns from the [PrepopulateList] section of
319 * WimBootCompress.ini in the WIM image being extracted.
321 * Note: WimBootCompress.ini applies to both types of "external backing":
323 * - WIM backing ("WIMBoot" - Windows 8.1 and later)
324 * - File backing ("System Compression" - Windows 10 and later)
327 load_prepopulate_pats(struct win32_apply_ctx *ctx)
329 const wchar_t *path = L"\\Windows\\System32\\WimBootCompress.ini";
330 struct wim_dentry *dentry;
331 const struct blob_descriptor *blob;
334 struct string_set *s;
336 struct text_file_section sec;
338 if (ctx->tried_to_load_prepopulate_list)
341 ctx->tried_to_load_prepopulate_list = true;
343 dentry = get_dentry(ctx->common.wim, path, WIMLIB_CASE_INSENSITIVE);
345 (dentry->d_inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
346 FILE_ATTRIBUTE_REPARSE_POINT |
347 FILE_ATTRIBUTE_ENCRYPTED)) ||
348 !(blob = inode_get_blob_for_unnamed_data_stream(dentry->d_inode,
349 ctx->common.wim->blob_table)))
351 WARNING("%ls does not exist in the WIM image.\n"
352 " The default configuration will be used instead; it assumes that all\n"
353 " files are valid for external backing regardless of path, equivalent\n"
354 " to an empty [PrepopulateList] section.", path);
355 return WIMLIB_ERR_PATH_DOES_NOT_EXIST;
358 ret = read_blob_into_alloc_buf(blob, &buf);
362 s = CALLOC(1, sizeof(struct string_set));
365 return WIMLIB_ERR_NOMEM;
368 sec.name = T("PrepopulateList");
371 ret = do_load_text_file(path, buf, blob->size, &mem, &sec, 1,
372 LOAD_TEXT_FILE_REMOVE_QUOTES |
373 LOAD_TEXT_FILE_NO_WARNINGS,
375 STATIC_ASSERT(OS_PREFERRED_PATH_SEPARATOR == WIM_PATH_SEPARATOR);
381 ctx->prepopulate_pats = s;
382 ctx->mem_prepopulate_pats = mem;
386 /* Returns %true if the specified absolute path to a file in the WIM image can
387 * be subject to external backing when extracted. Otherwise returns %false. */
389 can_externally_back_path(const wchar_t *path, const struct win32_apply_ctx *ctx)
391 /* Does the path match a pattern given in the [PrepopulateList] section
392 * of WimBootCompress.ini? */
393 if (ctx->prepopulate_pats && match_pattern_list(path, ctx->prepopulate_pats))
396 /* Since we attempt to modify the SYSTEM registry after it's extracted
397 * (see end_wimboot_extraction()), it can't be extracted as externally
398 * backed. This extends to associated files such as SYSTEM.LOG that
399 * also must be writable in order to write to the registry. Normally,
400 * SYSTEM is in [PrepopulateList], and the SYSTEM.* files match patterns
401 * in [ExclusionList] and therefore are not captured in the WIM at all.
402 * However, a WIM that wasn't specifically captured in "WIMBoot mode"
403 * may contain SYSTEM.* files. So to make things "just work", hard-code
405 if (match_path(path, L"\\Windows\\System32\\config\\SYSTEM*", false))
411 /* Can the specified WIM resource be used as the source of an external backing
412 * for the wof.sys WIM provider? */
414 is_resource_valid_for_external_backing(const struct wim_resource_descriptor *rdesc,
415 struct win32_apply_ctx *ctx)
417 /* Must be the original WIM file format. This check excludes pipable
418 * resources and solid resources. It also excludes other resources
419 * contained in such files even if they would be otherwise compatible.
421 if (rdesc->wim->hdr.magic != WIM_MAGIC ||
422 rdesc->wim->hdr.wim_version != WIM_VERSION_DEFAULT)
424 ctx->wimboot.have_wrong_version_wims = true;
429 * Whitelist of compression types and chunk sizes supported by
430 * Microsoft's WOF driver.
433 * - Uncompressed WIMs result in BSOD. However, this only applies to
434 * the WIM file itself, not to uncompressed resources in a WIM file
435 * that is otherwise compressed.
436 * - XPRESS 64K sometimes appears to work, but sometimes it causes
437 * reads to fail with STATUS_UNSUCCESSFUL.
439 switch (rdesc->compression_type) {
440 case WIMLIB_COMPRESSION_TYPE_NONE:
441 if (rdesc->wim->compression_type == WIMLIB_COMPRESSION_TYPE_NONE) {
442 ctx->wimboot.have_uncompressed_wims = true;
446 case WIMLIB_COMPRESSION_TYPE_XPRESS:
447 switch (rdesc->chunk_size) {
454 ctx->wimboot.have_unsupported_compressed_resources = true;
458 case WIMLIB_COMPRESSION_TYPE_LZX:
459 switch (rdesc->chunk_size) {
463 ctx->wimboot.have_unsupported_compressed_resources = true;
468 ctx->wimboot.have_unsupported_compressed_resources = true;
472 /* Microsoft's WoF driver errors out if it tries to satisfy a read with
473 * ending offset >= 4 GiB from an externally backed file. */
474 if (rdesc->uncompressed_size > 4200000000) {
475 ctx->wimboot.have_huge_resources = true;
482 #define EXTERNAL_BACKING_NOT_ENABLED -1
483 #define EXTERNAL_BACKING_NOT_POSSIBLE -2
484 #define EXTERNAL_BACKING_EXCLUDED -3
487 * Determines whether the specified file will be externally backed. Returns a
488 * negative status code if no, 0 if yes, or a positive wimlib error code on
489 * error. If the file is excluded from external backing based on its path, then
490 * *excluded_dentry_ret is set to the dentry for the path that matched the
493 * Note that this logic applies to both types of "external backing":
495 * - WIM backing ("WIMBoot" - Windows 8.1 and later)
496 * - File backing ("System Compression" - Windows 10 and later)
498 * However, in the case of WIM backing we also need to validate that the WIM
499 * resource that would be the source of the backing is supported by the wof.sys
503 will_externally_back_inode(struct wim_inode *inode, struct win32_apply_ctx *ctx,
504 const struct wim_dentry **excluded_dentry_ret,
507 struct wim_dentry *dentry;
508 struct blob_descriptor *blob;
511 if (load_prepopulate_pats(ctx) == WIMLIB_ERR_NOMEM)
512 return WIMLIB_ERR_NOMEM;
514 if (inode->i_can_externally_back)
517 /* This may do redundant checks because the cached value
518 * i_can_externally_back is 2-state (as opposed to 3-state:
519 * unknown/no/yes). But most files can be externally backed, so this
522 if (inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
523 FILE_ATTRIBUTE_REPARSE_POINT |
524 FILE_ATTRIBUTE_ENCRYPTED))
525 return EXTERNAL_BACKING_NOT_POSSIBLE;
527 blob = inode_get_blob_for_unnamed_data_stream_resolved(inode);
530 return EXTERNAL_BACKING_NOT_POSSIBLE;
533 (blob->blob_location != BLOB_IN_WIM ||
534 !is_resource_valid_for_external_backing(blob->rdesc, ctx)))
535 return EXTERNAL_BACKING_NOT_POSSIBLE;
538 * We need to check the patterns in [PrepopulateList] against every name
539 * of the inode, in case any of them match.
542 inode_for_each_extraction_alias(dentry, inode) {
544 ret = calculate_dentry_full_path(dentry);
548 if (!can_externally_back_path(dentry->d_full_path, ctx)) {
549 if (excluded_dentry_ret)
550 *excluded_dentry_ret = dentry;
551 return EXTERNAL_BACKING_EXCLUDED;
555 inode->i_can_externally_back = 1;
560 * Determines if the unnamed data stream of a file will be created as a WIM
561 * external backing (a "WIMBoot pointer file"), as opposed to a standard
565 win32_will_back_from_wim(struct wim_dentry *dentry, struct apply_ctx *_ctx)
567 struct win32_apply_ctx *ctx = (struct win32_apply_ctx *)_ctx;
569 if (!(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_WIMBOOT))
570 return EXTERNAL_BACKING_NOT_ENABLED;
572 return will_externally_back_inode(dentry->d_inode, ctx, NULL, true);
575 /* Find the WOF registration information for the specified WIM file. */
576 static struct wimboot_wim *
577 find_wimboot_wim(WIMStruct *wim_to_find, struct win32_apply_ctx *ctx)
579 for (size_t i = 0; i < ctx->wimboot.num_wims; i++)
580 if (wim_to_find == ctx->wimboot.wims[i].wim)
581 return &ctx->wimboot.wims[i];
588 set_backed_from_wim(HANDLE h, struct wim_inode *inode, struct win32_apply_ctx *ctx)
591 const struct wim_dentry *excluded_dentry;
592 const struct blob_descriptor *blob;
593 const struct wimboot_wim *wimboot_wim;
595 ret = will_externally_back_inode(inode, ctx, &excluded_dentry, true);
596 if (ret > 0) /* Error. */
599 if (ret < 0 && ret != EXTERNAL_BACKING_EXCLUDED)
600 return 0; /* Not externally backing, other than due to exclusion. */
602 if (unlikely(ret == EXTERNAL_BACKING_EXCLUDED)) {
603 /* Not externally backing due to exclusion. */
604 union wimlib_progress_info info;
606 build_extraction_path(excluded_dentry, ctx);
608 info.wimboot_exclude.path_in_wim = excluded_dentry->d_full_path;
609 info.wimboot_exclude.extraction_path = current_path(ctx);
611 return call_progress(ctx->common.progfunc,
612 WIMLIB_PROGRESS_MSG_WIMBOOT_EXCLUDE,
613 &info, ctx->common.progctx);
616 /* Externally backing. */
618 blob = inode_get_blob_for_unnamed_data_stream_resolved(inode);
619 wimboot_wim = find_wimboot_wim(blob->rdesc->wim, ctx);
621 if (unlikely(!wimboot_set_pointer(h,
623 wimboot_wim->data_source_id,
624 wimboot_wim->blob_table_hash,
625 ctx->wimboot.wof_running)))
627 const DWORD err = GetLastError();
629 build_extraction_path(inode_first_extraction_dentry(inode), ctx);
630 win32_error(err, L"\"%ls\": Couldn't set WIMBoot pointer data",
632 return WIMLIB_ERR_WIMBOOT;
637 /* Calculates the SHA-1 message digest of the WIM's blob table. */
639 hash_blob_table(WIMStruct *wim, u8 hash[SHA1_HASH_SIZE])
641 return wim_reshdr_to_hash(&wim->hdr.blob_table_reshdr, wim, hash);
645 register_wim_with_wof(WIMStruct *wim, struct win32_apply_ctx *ctx)
647 struct wimboot_wim *p;
650 /* Check if already registered */
651 for (size_t i = 0; i < ctx->wimboot.num_wims; i++)
652 if (wim == ctx->wimboot.wims[i].wim)
655 /* Not yet registered */
657 p = REALLOC(ctx->wimboot.wims,
658 (ctx->wimboot.num_wims + 1) * sizeof(ctx->wimboot.wims[0]));
660 return WIMLIB_ERR_NOMEM;
661 ctx->wimboot.wims = p;
663 ctx->wimboot.wims[ctx->wimboot.num_wims].wim = wim;
665 ret = hash_blob_table(wim, ctx->wimboot.wims[ctx->wimboot.num_wims].blob_table_hash);
669 ret = wimboot_alloc_data_source_id(wim->filename,
671 ctx->common.wim->current_image,
673 &ctx->wimboot.wims[ctx->wimboot.num_wims].data_source_id,
674 &ctx->wimboot.wof_running);
678 ctx->wimboot.num_wims++;
682 /* Prepare for doing a "WIMBoot" extraction by registering each source WIM file
683 * with WOF on the target volume. */
685 start_wimboot_extraction(struct list_head *dentry_list, struct win32_apply_ctx *ctx)
688 struct wim_dentry *dentry;
690 if (!xml_get_wimboot(ctx->common.wim->xml_info,
691 ctx->common.wim->current_image))
692 WARNING("The WIM image is not marked as WIMBoot compatible. This usually\n"
693 " means it is not intended to be used to back a Windows operating\n"
694 " system. Proceeding anyway.");
696 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
697 struct blob_descriptor *blob;
699 ret = win32_will_back_from_wim(dentry, &ctx->common);
700 if (ret > 0) /* Error */
702 if (ret < 0) /* Won't externally back */
705 blob = inode_get_blob_for_unnamed_data_stream_resolved(dentry->d_inode);
706 ret = register_wim_with_wof(blob->rdesc->wim, ctx);
711 if (ctx->wimboot.have_wrong_version_wims) {
712 WARNING("At least one of the source WIM files uses a version of the WIM\n"
713 " file format that not supported by Microsoft's wof.sys driver.\n"
714 " Files whose data is contained in one of these WIM files will be\n"
715 " extracted as full files rather than externally backed.");
718 if (ctx->wimboot.have_uncompressed_wims) {
719 WARNING("At least one of the source WIM files is uncompressed. Files whose\n"
720 " data is contained in an uncompressed WIM file will be extracted as\n"
721 " full files rather than externally backed, since uncompressed WIM\n"
722 " files are not supported by Microsoft's wof.sys driver.");
725 if (ctx->wimboot.have_unsupported_compressed_resources) {
726 WARNING("At least one of the source WIM files uses a compression format that\n"
727 " is not supported by Microsoft's wof.sys driver. Files whose data is\n"
728 " contained in a compressed resource in one of these WIM files will be\n"
729 " extracted as full files rather than externally backed. (The\n"
730 " compression formats supported by wof.sys are: XPRESS 4K, XPRESS 8K,\n"
731 " XPRESS 16K, XPRESS 32K, and LZX 32K.)");
734 if (ctx->wimboot.have_huge_resources) {
735 WARNING("Some files exceeded 4.2 GB in size. Such files will be extracted\n"
736 " as full files rather than externally backed, since very large files\n"
737 " are not supported by Microsoft's wof.sys driver.");
744 build_win32_extraction_path(const struct wim_dentry *dentry,
745 struct win32_apply_ctx *ctx);
747 /* Sets WimBoot=1 in the extracted SYSTEM registry hive.
749 * WIMGAPI does this, and it's possible that it's important.
750 * But I don't know exactly what this value means to Windows. */
752 end_wimboot_extraction(struct win32_apply_ctx *ctx)
754 struct wim_dentry *dentry;
755 wchar_t subkeyname[32];
761 dentry = get_dentry(ctx->common.wim, L"\\Windows\\System32\\config\\SYSTEM",
762 WIMLIB_CASE_INSENSITIVE);
764 if (!dentry || !will_extract_dentry(dentry))
767 if (!will_extract_dentry(wim_get_current_root_dentry(ctx->common.wim)))
770 /* Not bothering to use the native routines (e.g. NtLoadKey()) for this.
771 * If this doesn't work, you probably also have many other problems. */
773 build_win32_extraction_path(dentry, ctx);
775 randomize_char_array_with_alnum(subkeyname, 20);
776 subkeyname[20] = L'\0';
778 res = RegLoadKey(HKEY_LOCAL_MACHINE, subkeyname, ctx->pathbuf.Buffer);
782 wcscpy(&subkeyname[20], L"\\Setup");
784 res = RegCreateKeyEx(HKEY_LOCAL_MACHINE, subkeyname, 0, NULL,
785 REG_OPTION_BACKUP_RESTORE, 0, NULL, &key, NULL);
791 res = RegSetValueEx(key, L"WimBoot", 0, REG_DWORD,
792 (const BYTE *)&value, sizeof(DWORD));
796 res = RegFlushKey(key);
799 res2 = RegCloseKey(key);
803 subkeyname[20] = L'\0';
804 RegUnLoadKey(HKEY_LOCAL_MACHINE, subkeyname);
808 win32_warning(res, L"Failed to set \\Setup: dword \"WimBoot\"=1 "
809 "value in registry hive \"%ls\"",
810 ctx->pathbuf.Buffer);
816 /* Returns the number of wide characters needed to represent the path to the
817 * specified @dentry, relative to the target directory, when extracted.
819 * Does not include null terminator (not needed for NtCreateFile). */
821 dentry_extraction_path_length(const struct wim_dentry *dentry)
824 const struct wim_dentry *d;
828 len += d->d_extraction_name_nchars + 1;
830 } while (!dentry_is_root(d) && will_extract_dentry(d));
832 return --len; /* No leading slash */
835 /* Returns the length of the longest string that might need to be appended to
836 * the path to an alias of an inode to open or create a named data stream.
838 * If the inode has no named data streams, this will be 0. Otherwise, this will
839 * be 1 plus the length of the longest-named data stream, since the data stream
840 * name must be separated from the path by the ':' character. */
842 inode_longest_named_data_stream_spec(const struct wim_inode *inode)
845 for (unsigned i = 0; i < inode->i_num_streams; i++) {
846 const struct wim_inode_stream *strm = &inode->i_streams[i];
847 if (!stream_is_named_data_stream(strm))
849 size_t len = utf16le_len_chars(strm->stream_name);
858 /* Find the length, in wide characters, of the longest path needed for
859 * extraction of any file in @dentry_list relative to the target directory.
861 * Accounts for named data streams, but does not include null terminator (not
862 * needed for NtCreateFile). */
864 compute_path_max(struct list_head *dentry_list)
867 const struct wim_dentry *dentry;
869 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
872 len = dentry_extraction_path_length(dentry);
874 /* Account for named data streams */
875 len += inode_longest_named_data_stream_spec(dentry->d_inode);
884 /* Build the path at which to extract the @dentry, relative to the target
887 * The path is saved in ctx->pathbuf. */
889 build_extraction_path(const struct wim_dentry *dentry,
890 struct win32_apply_ctx *ctx)
894 const struct wim_dentry *d;
896 len = dentry_extraction_path_length(dentry);
898 ctx->pathbuf.Length = len * sizeof(wchar_t);
899 p = ctx->pathbuf.Buffer + len;
901 !dentry_is_root(d->d_parent) && will_extract_dentry(d->d_parent);
904 p -= d->d_extraction_name_nchars;
905 if (d->d_extraction_name_nchars)
906 wmemcpy(p, d->d_extraction_name,
907 d->d_extraction_name_nchars);
910 /* No leading slash */
911 p -= d->d_extraction_name_nchars;
912 wmemcpy(p, d->d_extraction_name, d->d_extraction_name_nchars);
915 /* Build the path at which to extract the @dentry, relative to the target
916 * directory, adding the suffix for a named data stream.
918 * The path is saved in ctx->pathbuf. */
920 build_extraction_path_with_ads(const struct wim_dentry *dentry,
921 struct win32_apply_ctx *ctx,
922 const wchar_t *stream_name,
923 size_t stream_name_nchars)
927 build_extraction_path(dentry, ctx);
929 /* Add :NAME for named data stream */
930 p = ctx->pathbuf.Buffer + (ctx->pathbuf.Length / sizeof(wchar_t));
932 wmemcpy(p, stream_name, stream_name_nchars);
933 ctx->pathbuf.Length += (1 + stream_name_nchars) * sizeof(wchar_t);
936 /* Build the Win32 namespace path to the specified @dentry when extracted.
938 * The path is saved in ctx->pathbuf and will be null terminated.
940 * XXX: We could get rid of this if it wasn't needed for the file encryption
941 * APIs, and the registry manipulation in WIMBoot mode. */
943 build_win32_extraction_path(const struct wim_dentry *dentry,
944 struct win32_apply_ctx *ctx)
946 build_extraction_path(dentry, ctx);
948 /* Prepend target_ntpath to our relative path, then change \??\ into \\?\ */
950 memmove(ctx->pathbuf.Buffer +
951 (ctx->target_ntpath.Length / sizeof(wchar_t)) + 1,
952 ctx->pathbuf.Buffer, ctx->pathbuf.Length);
953 memcpy(ctx->pathbuf.Buffer, ctx->target_ntpath.Buffer,
954 ctx->target_ntpath.Length);
955 ctx->pathbuf.Buffer[ctx->target_ntpath.Length / sizeof(wchar_t)] = L'\\';
956 ctx->pathbuf.Length += ctx->target_ntpath.Length + sizeof(wchar_t);
957 ctx->pathbuf.Buffer[ctx->pathbuf.Length / sizeof(wchar_t)] = L'\0';
959 wimlib_assert(ctx->pathbuf.Length >= 4 * sizeof(wchar_t) &&
960 !wmemcmp(ctx->pathbuf.Buffer, L"\\??\\", 4));
962 ctx->pathbuf.Buffer[1] = L'\\';
966 /* Returns a "printable" representation of the last relative NT path that was
967 * constructed with build_extraction_path() or build_extraction_path_with_ads().
969 * This will be overwritten by the next call to this function. */
970 static const wchar_t *
971 current_path(struct win32_apply_ctx *ctx)
973 wchar_t *p = ctx->print_buffer;
975 p = wmempcpy(p, ctx->common.target, ctx->common.target_nchars);
977 p = wmempcpy(p, ctx->pathbuf.Buffer, ctx->pathbuf.Length / sizeof(wchar_t));
979 return ctx->print_buffer;
982 /* Open handle to the target directory if it is not already open. If the target
983 * directory does not exist, this creates it. */
985 open_target_directory(struct win32_apply_ctx *ctx)
992 ctx->attr.Length = sizeof(ctx->attr);
993 ctx->attr.RootDirectory = NULL;
994 ctx->attr.ObjectName = &ctx->target_ntpath;
996 /* Don't use FILE_OPEN_REPARSE_POINT here; we want the extraction to
997 * happen at the directory "pointed to" by the reparse point. */
998 status = NtCreateFile(&ctx->h_target,
1004 FILE_SHARE_VALID_FLAGS,
1006 FILE_DIRECTORY_FILE |
1007 FILE_OPEN_FOR_BACKUP_INTENT,
1010 if (!NT_SUCCESS(status)) {
1011 winnt_error(status, L"Can't open or create directory \"%ls\"",
1012 ctx->common.target);
1013 return WIMLIB_ERR_OPENDIR;
1015 ctx->attr.RootDirectory = ctx->h_target;
1016 ctx->attr.ObjectName = &ctx->pathbuf;
1021 close_target_directory(struct win32_apply_ctx *ctx)
1023 if (ctx->h_target) {
1024 NtClose(ctx->h_target);
1025 ctx->h_target = NULL;
1026 ctx->attr.RootDirectory = NULL;
1031 * Ensures the target directory exists and opens a handle to it, in preparation
1032 * of using paths relative to it.
1035 prepare_target(struct list_head *dentry_list, struct win32_apply_ctx *ctx)
1040 ret = win32_path_to_nt_path(ctx->common.target, &ctx->target_ntpath);
1044 ret = open_target_directory(ctx);
1048 path_max = compute_path_max(dentry_list);
1049 /* Add some extra for building Win32 paths for the file encryption APIs,
1050 * and ensure we have at least enough to potentially use a 8.3 name for
1051 * the last component. */
1052 path_max += max(2 + (ctx->target_ntpath.Length / sizeof(wchar_t)),
1055 ctx->pathbuf.MaximumLength = path_max * sizeof(wchar_t);
1056 ctx->pathbuf.Buffer = MALLOC(ctx->pathbuf.MaximumLength);
1057 if (!ctx->pathbuf.Buffer)
1058 return WIMLIB_ERR_NOMEM;
1060 ctx->print_buffer = MALLOC((ctx->common.target_nchars + 1 + path_max + 1) *
1062 if (!ctx->print_buffer)
1063 return WIMLIB_ERR_NOMEM;
1068 /* When creating an inode that will have a short (DOS) name, we create it using
1069 * the long name associated with the short name. This ensures that the short
1070 * name gets associated with the correct long name. */
1071 static struct wim_dentry *
1072 first_extraction_alias(const struct wim_inode *inode)
1074 struct wim_dentry *dentry;
1076 inode_for_each_extraction_alias(dentry, inode)
1077 if (dentry_has_short_name(dentry))
1079 return inode_first_extraction_dentry(inode);
1083 * Set or clear FILE_ATTRIBUTE_COMPRESSED if the inherited value is different
1084 * from the desired value.
1086 * Note that you can NOT override the inherited value of
1087 * FILE_ATTRIBUTE_COMPRESSED directly with NtCreateFile().
1090 adjust_compression_attribute(HANDLE h, const struct wim_dentry *dentry,
1091 struct win32_apply_ctx *ctx)
1093 const bool compressed = (dentry->d_inode->i_attributes &
1094 FILE_ATTRIBUTE_COMPRESSED);
1095 FILE_BASIC_INFORMATION info;
1096 USHORT compression_state;
1099 if (ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES)
1102 if (!ctx->common.supported_features.compressed_files)
1106 /* Get current attributes */
1107 status = NtQueryInformationFile(h, &ctx->iosb, &info, sizeof(info),
1108 FileBasicInformation);
1109 if (NT_SUCCESS(status) &&
1110 compressed == !!(info.FileAttributes & FILE_ATTRIBUTE_COMPRESSED))
1112 /* Nothing needs to be done. */
1116 /* Set the new compression state */
1119 compression_state = COMPRESSION_FORMAT_DEFAULT;
1121 compression_state = COMPRESSION_FORMAT_NONE;
1123 status = winnt_fsctl(h, FSCTL_SET_COMPRESSION,
1124 &compression_state, sizeof(USHORT), NULL, 0, NULL);
1125 if (NT_SUCCESS(status))
1128 winnt_error(status, L"Can't %s compression attribute on \"%ls\"",
1129 (compressed ? "set" : "clear"), current_path(ctx));
1130 return WIMLIB_ERR_SET_ATTRIBUTES;
1133 /* Try to enable short name support on the target volume. If successful, return
1134 * true. If unsuccessful, issue a warning and return false. */
1136 try_to_enable_short_names(const wchar_t *volume)
1139 FILE_FS_PERSISTENT_VOLUME_INFORMATION info;
1141 DWORD bytesReturned;
1143 h = CreateFile(volume, GENERIC_WRITE,
1144 FILE_SHARE_VALID_FLAGS, NULL, OPEN_EXISTING,
1145 FILE_FLAG_BACKUP_SEMANTICS, NULL);
1146 if (h == INVALID_HANDLE_VALUE)
1149 info.VolumeFlags = 0;
1150 info.FlagMask = PERSISTENT_VOLUME_STATE_SHORT_NAME_CREATION_DISABLED;
1154 bret = DeviceIoControl(h, FSCTL_SET_PERSISTENT_VOLUME_STATE,
1155 &info, sizeof(info), NULL, 0,
1156 &bytesReturned, NULL);
1165 win32_warning(GetLastError(),
1166 L"Failed to enable short name support on %ls",
1172 remove_conflicting_short_name(const struct wim_dentry *dentry, struct win32_apply_ctx *ctx)
1178 size_t bufsize = offsetof(FILE_NAME_INFORMATION, FileName) +
1179 (13 * sizeof(wchar_t));
1180 u8 buf[bufsize] _aligned_attribute(8);
1181 bool retried = false;
1182 FILE_NAME_INFORMATION *info = (FILE_NAME_INFORMATION *)buf;
1184 memset(buf, 0, bufsize);
1186 /* Build the path with the short name. */
1187 name = &ctx->pathbuf.Buffer[ctx->pathbuf.Length / sizeof(wchar_t)];
1188 while (name != ctx->pathbuf.Buffer && *(name - 1) != L'\\')
1190 end = mempcpy(name, dentry->d_short_name, dentry->d_short_name_nbytes);
1191 ctx->pathbuf.Length = ((u8 *)end - (u8 *)ctx->pathbuf.Buffer);
1193 /* Open the conflicting file (by short name). */
1194 status = NtOpenFile(&h, GENERIC_WRITE | DELETE,
1195 &ctx->attr, &ctx->iosb,
1196 FILE_SHARE_VALID_FLAGS,
1197 FILE_OPEN_REPARSE_POINT | FILE_OPEN_FOR_BACKUP_INTENT);
1198 if (!NT_SUCCESS(status)) {
1199 winnt_warning(status, L"Can't open \"%ls\"", current_path(ctx));
1204 WARNING("Overriding conflicting short name; path=\"%ls\"",
1208 /* Try to remove the short name on the conflicting file. */
1211 status = NtSetInformationFile(h, &ctx->iosb, info, bufsize,
1212 FileShortNameInformation);
1214 if (status == STATUS_INVALID_PARAMETER && !retried) {
1216 /* Microsoft forgot to make it possible to remove short names
1217 * until Windows 7. Oops. Use a random short name instead. */
1219 info->FileNameLength = 12 * sizeof(wchar_t);
1220 for (int i = 0; i < 8; i++)
1221 info->FileName[i] = 'A' + (rand() % 26);
1222 info->FileName[8] = L'.';
1223 info->FileName[9] = L'W';
1224 info->FileName[10] = L'L';
1225 info->FileName[11] = L'B';
1226 info->FileName[12] = L'\0';
1232 build_extraction_path(dentry, ctx);
1236 /* Set the short name on the open file @h which has been created at the location
1237 * indicated by @dentry.
1239 * Note that this may add, change, or remove the short name.
1241 * @h must be opened with DELETE access.
1243 * Returns 0 or WIMLIB_ERR_SET_SHORT_NAME. The latter only happens in
1244 * STRICT_SHORT_NAMES mode.
1247 set_short_name(HANDLE h, const struct wim_dentry *dentry,
1248 struct win32_apply_ctx *ctx)
1251 if (!ctx->common.supported_features.short_names)
1255 * Note: The size of the FILE_NAME_INFORMATION buffer must be such that
1256 * FileName contains at least 2 wide characters (4 bytes). Otherwise,
1257 * NtSetInformationFile() will return STATUS_INFO_LENGTH_MISMATCH. This
1258 * is despite the fact that FileNameLength can validly be 0 or 2 bytes,
1259 * with the former case being removing the existing short name if
1260 * present, rather than setting one.
1262 * The null terminator is seemingly optional, but to be safe we include
1263 * space for it and zero all unused space.
1266 size_t bufsize = offsetof(FILE_NAME_INFORMATION, FileName) +
1267 max(dentry->d_short_name_nbytes, sizeof(wchar_t)) +
1269 u8 buf[bufsize] _aligned_attribute(8);
1270 FILE_NAME_INFORMATION *info = (FILE_NAME_INFORMATION *)buf;
1272 bool tried_to_remove_existing = false;
1274 memset(buf, 0, bufsize);
1276 info->FileNameLength = dentry->d_short_name_nbytes;
1277 memcpy(info->FileName, dentry->d_short_name, dentry->d_short_name_nbytes);
1280 status = NtSetInformationFile(h, &ctx->iosb, info, bufsize,
1281 FileShortNameInformation);
1282 if (NT_SUCCESS(status))
1285 if (status == STATUS_SHORT_NAMES_NOT_ENABLED_ON_VOLUME) {
1286 if (dentry->d_short_name_nbytes == 0)
1288 if (!ctx->tried_to_enable_short_names) {
1292 ctx->tried_to_enable_short_names = true;
1294 ret = win32_get_drive_path(ctx->common.target,
1298 if (try_to_enable_short_names(volume))
1304 * Short names can conflict in several cases:
1306 * - a file being extracted has a short name conflicting with an
1309 * - a file being extracted has a short name conflicting with another
1310 * file being extracted (possible, but shouldn't happen)
1312 * - a file being extracted has a short name that conflicts with the
1313 * automatically generated short name of a file we previously
1314 * extracted, but failed to set the short name for. Sounds unlikely,
1315 * but this actually does happen fairly often on versions of Windows
1316 * prior to Windows 7 because they do not support removing short names
1319 if (unlikely(status == STATUS_OBJECT_NAME_COLLISION) &&
1320 dentry->d_short_name_nbytes && !tried_to_remove_existing)
1322 tried_to_remove_existing = true;
1323 status = remove_conflicting_short_name(dentry, ctx);
1324 if (NT_SUCCESS(status))
1328 /* By default, failure to set short names is not an error (since short
1329 * names aren't too important anymore...). */
1330 if (!(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_SHORT_NAMES)) {
1331 if (dentry->d_short_name_nbytes)
1332 ctx->num_set_short_name_failures++;
1334 ctx->num_remove_short_name_failures++;
1338 winnt_error(status, L"Can't set short name on \"%ls\"", current_path(ctx));
1339 return WIMLIB_ERR_SET_SHORT_NAME;
1343 * A wrapper around NtCreateFile() to make it slightly more usable...
1344 * This uses the path currently constructed in ctx->pathbuf.
1346 * Also, we always specify SYNCHRONIZE access, FILE_OPEN_FOR_BACKUP_INTENT, and
1347 * FILE_OPEN_REPARSE_POINT.
1350 do_create_file(PHANDLE FileHandle,
1351 ACCESS_MASK DesiredAccess,
1352 PLARGE_INTEGER AllocationSize,
1353 ULONG FileAttributes,
1354 ULONG CreateDisposition,
1355 ULONG CreateOptions,
1356 struct win32_apply_ctx *ctx)
1358 return NtCreateFile(FileHandle,
1359 DesiredAccess | SYNCHRONIZE,
1364 FILE_SHARE_VALID_FLAGS,
1367 FILE_OPEN_FOR_BACKUP_INTENT |
1368 FILE_OPEN_REPARSE_POINT,
1373 /* Like do_create_file(), but builds the extraction path of the @dentry first.
1376 create_file(PHANDLE FileHandle,
1377 ACCESS_MASK DesiredAccess,
1378 PLARGE_INTEGER AllocationSize,
1379 ULONG FileAttributes,
1380 ULONG CreateDisposition,
1381 ULONG CreateOptions,
1382 const struct wim_dentry *dentry,
1383 struct win32_apply_ctx *ctx)
1385 build_extraction_path(dentry, ctx);
1386 return do_create_file(FileHandle,
1396 delete_file_or_stream(struct win32_apply_ctx *ctx)
1400 ULONG perms = DELETE;
1401 ULONG flags = FILE_NON_DIRECTORY_FILE | FILE_DELETE_ON_CLOSE;
1403 /* First try opening the file with FILE_DELETE_ON_CLOSE. In most cases,
1404 * all we have to do is that plus close the file handle. */
1406 status = do_create_file(&h, perms, NULL, 0, FILE_OPEN, flags, ctx);
1408 if (unlikely(status == STATUS_CANNOT_DELETE)) {
1409 /* This error occurs for files with FILE_ATTRIBUTE_READONLY set.
1410 * Try an alternate approach: first open the file without
1411 * FILE_DELETE_ON_CLOSE, then reset the file attributes, then
1412 * set the "delete" disposition on the handle. */
1413 if (flags & FILE_DELETE_ON_CLOSE) {
1414 flags &= ~FILE_DELETE_ON_CLOSE;
1415 perms |= FILE_WRITE_ATTRIBUTES;
1420 if (unlikely(!NT_SUCCESS(status))) {
1421 winnt_error(status, L"Can't open \"%ls\" for deletion "
1422 "(perms=%x, flags=%x)",
1423 current_path(ctx), perms, flags);
1424 return WIMLIB_ERR_OPEN;
1427 if (unlikely(!(flags & FILE_DELETE_ON_CLOSE))) {
1429 FILE_BASIC_INFORMATION basic_info =
1430 { .FileAttributes = FILE_ATTRIBUTE_NORMAL };
1431 status = NtSetInformationFile(h, &ctx->iosb, &basic_info,
1433 FileBasicInformation);
1435 if (!NT_SUCCESS(status)) {
1436 winnt_error(status, L"Can't reset attributes of \"%ls\" "
1437 "to prepare for deletion", current_path(ctx));
1439 return WIMLIB_ERR_SET_ATTRIBUTES;
1442 FILE_DISPOSITION_INFORMATION disp_info =
1443 { .DoDeleteFile = TRUE };
1444 status = NtSetInformationFile(h, &ctx->iosb, &disp_info,
1446 FileDispositionInformation);
1447 if (!NT_SUCCESS(status)) {
1448 winnt_error(status, L"Can't set delete-on-close "
1449 "disposition on \"%ls\"", current_path(ctx));
1451 return WIMLIB_ERR_SET_ATTRIBUTES;
1455 status = NtClose(h);
1456 if (unlikely(!NT_SUCCESS(status))) {
1457 winnt_error(status, L"Error closing \"%ls\" after setting "
1458 "delete-on-close disposition", current_path(ctx));
1459 return WIMLIB_ERR_OPEN;
1466 * Create a nondirectory file or named data stream at the current path,
1467 * superseding any that already exists at that path. If successful, return an
1468 * open handle to the file or named data stream.
1471 supersede_file_or_stream(struct win32_apply_ctx *ctx, HANDLE *h_ret)
1474 bool retried = false;
1476 /* FILE_ATTRIBUTE_SYSTEM is needed to ensure that
1477 * FILE_ATTRIBUTE_ENCRYPTED doesn't get set before we want it to be. */
1479 status = do_create_file(h_ret,
1480 GENERIC_READ | GENERIC_WRITE | DELETE,
1482 FILE_ATTRIBUTE_SYSTEM,
1484 FILE_NON_DIRECTORY_FILE,
1486 if (likely(NT_SUCCESS(status)))
1489 /* STATUS_OBJECT_NAME_COLLISION means that the file or stream already
1490 * exists. Delete the existing file or stream, then try again.
1492 * Note: we don't use FILE_OVERWRITE_IF or FILE_SUPERSEDE because of
1493 * problems with certain file attributes, especially
1494 * FILE_ATTRIBUTE_ENCRYPTED. FILE_SUPERSEDE is also broken in the
1495 * Windows PE ramdisk. */
1496 if (status == STATUS_OBJECT_NAME_COLLISION && !retried) {
1497 int ret = delete_file_or_stream(ctx);
1503 winnt_error(status, L"Can't create \"%ls\"", current_path(ctx));
1504 return WIMLIB_ERR_OPEN;
1507 /* Set the reparse point @rpbuf of length @rpbuflen on the extracted file
1508 * corresponding to the WIM dentry @dentry. */
1510 do_set_reparse_point(const struct wim_dentry *dentry,
1511 const struct reparse_buffer_disk *rpbuf, u16 rpbuflen,
1512 struct win32_apply_ctx *ctx)
1517 status = create_file(&h, GENERIC_WRITE, NULL,
1518 0, FILE_OPEN, 0, dentry, ctx);
1519 if (!NT_SUCCESS(status))
1522 status = winnt_fsctl(h, FSCTL_SET_REPARSE_POINT,
1523 rpbuf, rpbuflen, NULL, 0, NULL);
1526 if (NT_SUCCESS(status))
1529 /* On Windows, by default only the Administrator can create symbolic
1530 * links for some reason. By default we just issue a warning if this
1531 * appears to be the problem. Use WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS
1532 * to get a hard error. */
1533 if (!(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS)
1534 && (status == STATUS_PRIVILEGE_NOT_HELD ||
1535 status == STATUS_ACCESS_DENIED)
1536 && (dentry->d_inode->i_reparse_tag == WIM_IO_REPARSE_TAG_SYMLINK ||
1537 dentry->d_inode->i_reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT))
1539 WARNING("Can't create symbolic link \"%ls\"! \n"
1540 " (Need Administrator rights, or at least "
1542 " SeCreateSymbolicLink privilege.)",
1548 winnt_error(status, L"Can't set reparse data on \"%ls\"",
1550 return WIMLIB_ERR_SET_REPARSE_DATA;
1554 * Create empty named data streams and potentially a reparse point for the
1555 * specified file, if any.
1557 * Since these won't have blob descriptors, they won't show up in the call to
1558 * extract_blob_list(). Hence the need for the special case.
1561 create_empty_streams(const struct wim_dentry *dentry,
1562 struct win32_apply_ctx *ctx)
1564 const struct wim_inode *inode = dentry->d_inode;
1567 for (unsigned i = 0; i < inode->i_num_streams; i++) {
1568 const struct wim_inode_stream *strm = &inode->i_streams[i];
1570 if (stream_blob_resolved(strm) != NULL)
1573 if (strm->stream_type == STREAM_TYPE_REPARSE_POINT &&
1574 ctx->common.supported_features.reparse_points)
1576 u8 buf[REPARSE_DATA_OFFSET] _aligned_attribute(8);
1577 struct reparse_buffer_disk *rpbuf =
1578 (struct reparse_buffer_disk *)buf;
1579 complete_reparse_point(rpbuf, inode, 0);
1580 ret = do_set_reparse_point(dentry, rpbuf,
1581 REPARSE_DATA_OFFSET, ctx);
1584 } else if (stream_is_named_data_stream(strm) &&
1585 ctx->common.supported_features.named_data_streams)
1589 build_extraction_path_with_ads(dentry, ctx,
1591 utf16le_len_chars(strm->stream_name));
1592 ret = supersede_file_or_stream(ctx, &h);
1594 build_extraction_path(dentry, ctx);
1606 * Creates the directory named by @dentry, or uses an existing directory at that
1607 * location. If necessary, sets the short name and/or fixes compression and
1608 * encryption attributes.
1610 * Returns 0, WIMLIB_ERR_MKDIR, or WIMLIB_ERR_SET_SHORT_NAME.
1613 create_directory(const struct wim_dentry *dentry, struct win32_apply_ctx *ctx)
1620 /* DELETE is needed for set_short_name(); GENERIC_READ and GENERIC_WRITE
1621 * are needed for adjust_compression_attribute(); WRITE_DAC is needed to
1622 * remove the directory's DACL if the directory already existed */
1623 perms = GENERIC_READ | GENERIC_WRITE | WRITE_DAC;
1624 if (!dentry_is_root(dentry))
1627 /* FILE_ATTRIBUTE_SYSTEM is needed to ensure that
1628 * FILE_ATTRIBUTE_ENCRYPTED doesn't get set before we want it to be. */
1630 status = create_file(&h, perms, NULL, FILE_ATTRIBUTE_SYSTEM,
1631 FILE_OPEN_IF, FILE_DIRECTORY_FILE, dentry, ctx);
1632 if (unlikely(!NT_SUCCESS(status))) {
1633 if (status == STATUS_ACCESS_DENIED) {
1634 if (perms & WRITE_DAC) {
1635 perms &= ~WRITE_DAC;
1638 if (perms & DELETE) {
1643 const wchar_t *path = current_path(ctx);
1644 winnt_error(status, L"Can't create directory \"%ls\"", path);
1646 /* Check for known issue with WindowsApps directory. */
1647 if (status == STATUS_ACCESS_DENIED &&
1648 (wcsstr(path, L"\\WindowsApps\\") ||
1649 wcsstr(path, L"\\InfusedApps\\"))) {
1651 "You seem to be trying to extract files to the WindowsApps directory.\n"
1652 " Windows 8.1 and later use new file permissions in this directory that\n"
1653 " cannot be overridden, even by backup/restore programs. To extract your\n"
1654 " files anyway, you need to choose a different target directory, delete\n"
1655 " the WindowsApps directory entirely, reformat the volume, do the\n"
1656 " extraction from a non-broken operating system such as Windows 7 or\n"
1657 " Linux, or wait for Microsoft to fix the design flaw in their operating\n"
1658 " system. This is *not* a bug in wimlib. See this thread for more\n"
1659 " information: https://wimlib.net/forums/viewtopic.php?f=1&t=261");
1661 return WIMLIB_ERR_MKDIR;
1664 if (ctx->iosb.Information == FILE_OPENED) {
1665 /* If we opened an existing directory, try to clear its file
1666 * attributes. As far as I know, this only actually makes a
1667 * difference in the case where a FILE_ATTRIBUTE_READONLY
1668 * directory has a named data stream which needs to be
1669 * extracted. You cannot create a named data stream of such a
1670 * directory, even though this contradicts Microsoft's
1671 * documentation for FILE_ATTRIBUTE_READONLY which states it is
1672 * not honored for directories! */
1673 if (!(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES)) {
1674 FILE_BASIC_INFORMATION basic_info =
1675 { .FileAttributes = FILE_ATTRIBUTE_NORMAL };
1676 NtSetInformationFile(h, &ctx->iosb, &basic_info,
1678 FileBasicInformation);
1681 /* Also try to remove the directory's DACL. This isn't supposed
1682 * to be necessary because we *always* use backup semantics.
1683 * However, there is a case where NtCreateFile() fails with
1684 * STATUS_ACCESS_DENIED when creating a named data stream that
1685 * was just deleted, using a directory-relative open. I have no
1686 * idea why Windows is broken in this case. */
1687 if (!(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_NO_ACLS)) {
1688 static const SECURITY_DESCRIPTOR_RELATIVE desc = {
1689 .Revision = SECURITY_DESCRIPTOR_REVISION1,
1690 .Control = SE_SELF_RELATIVE | SE_DACL_PRESENT,
1696 NtSetSecurityObject(h, DACL_SECURITY_INFORMATION,
1701 if (!dentry_is_root(dentry)) {
1702 ret = set_short_name(h, dentry, ctx);
1707 ret = adjust_compression_attribute(h, dentry, ctx);
1714 * Create all the directories being extracted, other than the target directory
1717 * Note: we don't honor directory hard links. However, we don't allow them to
1718 * exist in WIM images anyway (see inode_fixup.c).
1721 create_directories(struct list_head *dentry_list,
1722 struct win32_apply_ctx *ctx)
1724 const struct wim_dentry *dentry;
1727 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
1729 if (!(dentry->d_inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY))
1732 /* Note: Here we include files with
1733 * FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_REPARSE_POINT, but we
1734 * wait until later to actually set the reparse data. */
1736 ret = create_directory(dentry, ctx);
1739 ret = create_empty_streams(dentry, ctx);
1741 ret = check_apply_error(dentry, ctx, ret);
1745 ret = report_file_created(&ctx->common);
1753 * Creates the nondirectory file named by @dentry.
1755 * On success, returns an open handle to the file in @h_ret, with GENERIC_READ,
1756 * GENERIC_WRITE, and DELETE access. Also, the path to the file will be saved
1757 * in ctx->pathbuf. On failure, returns an error code.
1760 create_nondirectory_inode(HANDLE *h_ret, const struct wim_dentry *dentry,
1761 struct win32_apply_ctx *ctx)
1766 build_extraction_path(dentry, ctx);
1768 ret = supersede_file_or_stream(ctx, &h);
1772 ret = adjust_compression_attribute(h, dentry, ctx);
1776 ret = create_empty_streams(dentry, ctx);
1789 /* Creates a hard link at the location named by @dentry to the file represented
1790 * by the open handle @h. Or, if the target volume does not support hard links,
1791 * create a separate file instead. */
1793 create_link(HANDLE h, const struct wim_dentry *dentry,
1794 struct win32_apply_ctx *ctx)
1796 if (ctx->common.supported_features.hard_links) {
1798 build_extraction_path(dentry, ctx);
1800 size_t bufsize = offsetof(FILE_LINK_INFORMATION, FileName) +
1801 ctx->pathbuf.Length + sizeof(wchar_t);
1802 u8 buf[bufsize] _aligned_attribute(8);
1803 FILE_LINK_INFORMATION *info = (FILE_LINK_INFORMATION *)buf;
1806 info->ReplaceIfExists = TRUE;
1807 info->RootDirectory = ctx->attr.RootDirectory;
1808 info->FileNameLength = ctx->pathbuf.Length;
1809 memcpy(info->FileName, ctx->pathbuf.Buffer, ctx->pathbuf.Length);
1810 info->FileName[info->FileNameLength / 2] = L'\0';
1812 /* Note: the null terminator isn't actually necessary,
1813 * but if you don't add the extra character, you get
1814 * STATUS_INFO_LENGTH_MISMATCH when FileNameLength
1815 * happens to be 2 */
1817 status = NtSetInformationFile(h, &ctx->iosb, info, bufsize,
1818 FileLinkInformation);
1819 if (NT_SUCCESS(status))
1821 winnt_error(status, L"Failed to create link \"%ls\"",
1823 return WIMLIB_ERR_LINK;
1828 ret = create_nondirectory_inode(&h2, dentry, ctx);
1837 /* Given an inode (represented by the open handle @h) for which one link has
1838 * been created (named by @first_dentry), create the other links.
1840 * Or, if the target volume does not support hard links, create separate files.
1842 * Note: This uses ctx->pathbuf and does not reset it.
1845 create_links(HANDLE h, const struct wim_dentry *first_dentry,
1846 struct win32_apply_ctx *ctx)
1848 const struct wim_inode *inode = first_dentry->d_inode;
1849 const struct wim_dentry *dentry;
1852 inode_for_each_extraction_alias(dentry, inode) {
1853 if (dentry != first_dentry) {
1854 ret = create_link(h, dentry, ctx);
1862 /* Create a nondirectory file, including all links. */
1864 create_nondirectory(struct wim_inode *inode, struct win32_apply_ctx *ctx)
1866 struct wim_dentry *first_dentry;
1870 first_dentry = first_extraction_alias(inode);
1872 /* Create first link. */
1873 ret = create_nondirectory_inode(&h, first_dentry, ctx);
1877 /* Set short name. */
1878 ret = set_short_name(h, first_dentry, ctx);
1880 /* Create additional links, OR if hard links are not supported just
1881 * create more files. */
1883 ret = create_links(h, first_dentry, ctx);
1885 /* "WIMBoot" extraction: set external backing by the WIM file if needed. */
1886 if (!ret && unlikely(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_WIMBOOT))
1887 ret = set_backed_from_wim(h, inode, ctx);
1893 /* Create all the nondirectory files being extracted, including all aliases
1896 create_nondirectories(struct list_head *dentry_list, struct win32_apply_ctx *ctx)
1898 struct wim_dentry *dentry;
1899 struct wim_inode *inode;
1902 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
1903 inode = dentry->d_inode;
1904 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)
1906 /* Call create_nondirectory() only once per inode */
1907 if (dentry == inode_first_extraction_dentry(inode)) {
1908 ret = create_nondirectory(inode, ctx);
1909 ret = check_apply_error(dentry, ctx, ret);
1913 ret = report_file_created(&ctx->common);
1921 close_handles(struct win32_apply_ctx *ctx)
1923 for (unsigned i = 0; i < ctx->num_open_handles; i++)
1924 NtClose(ctx->open_handles[i]);
1927 /* Prepare to read the next blob, which has size @blob_size, into an in-memory
1930 prepare_data_buffer(struct win32_apply_ctx *ctx, u64 blob_size)
1932 if (blob_size > ctx->data_buffer_size) {
1933 /* Larger buffer needed. */
1935 if ((size_t)blob_size != blob_size)
1937 new_buffer = REALLOC(ctx->data_buffer, blob_size);
1940 ctx->data_buffer = new_buffer;
1941 ctx->data_buffer_size = blob_size;
1943 /* On the first call this changes data_buffer_ptr from NULL, which tells
1944 * extract_chunk() that the data buffer needs to be filled while reading
1945 * the stream data. */
1946 ctx->data_buffer_ptr = ctx->data_buffer;
1951 begin_extract_blob_instance(const struct blob_descriptor *blob,
1952 struct wim_dentry *dentry,
1953 const struct wim_inode_stream *strm,
1954 struct win32_apply_ctx *ctx)
1956 FILE_ALLOCATION_INFORMATION alloc_info;
1960 if (unlikely(strm->stream_type == STREAM_TYPE_REPARSE_POINT)) {
1961 /* We can't write the reparse point stream directly; we must set
1962 * it with FSCTL_SET_REPARSE_POINT, which requires that all the
1963 * data be available. So, stage the data in a buffer. */
1964 if (!prepare_data_buffer(ctx, blob->size))
1965 return WIMLIB_ERR_NOMEM;
1966 list_add_tail(&dentry->d_tmp_list, &ctx->reparse_dentries);
1970 if (unlikely(strm->stream_type == STREAM_TYPE_EFSRPC_RAW_DATA)) {
1971 /* We can't write encrypted files directly; we must use
1972 * WriteEncryptedFileRaw(), which requires providing the data
1973 * through a callback function. This can't easily be combined
1974 * with our own callback-based approach.
1976 * The current workaround is to simply read the blob into memory
1977 * and write the encrypted file from that.
1979 * TODO: This isn't sufficient for extremely large encrypted
1980 * files. Perhaps we should create an extra thread to write
1982 if (!prepare_data_buffer(ctx, blob->size))
1983 return WIMLIB_ERR_NOMEM;
1984 list_add_tail(&dentry->d_tmp_list, &ctx->encrypted_dentries);
1988 /* It's a data stream (may be unnamed or named). */
1989 wimlib_assert(strm->stream_type == STREAM_TYPE_DATA);
1991 if (ctx->num_open_handles == MAX_OPEN_FILES) {
1992 /* XXX: Fix this. But because of the checks in
1993 * extract_blob_list(), this can now only happen on a filesystem
1994 * that does not support hard links. */
1995 ERROR("Can't extract data: too many open files!");
1996 return WIMLIB_ERR_UNSUPPORTED;
2000 if (unlikely(stream_is_named(strm))) {
2001 build_extraction_path_with_ads(dentry, ctx,
2003 utf16le_len_chars(strm->stream_name));
2005 build_extraction_path(dentry, ctx);
2009 /* Open a new handle */
2010 status = do_create_file(&h,
2011 FILE_WRITE_DATA | SYNCHRONIZE,
2012 NULL, 0, FILE_OPEN_IF,
2013 FILE_SEQUENTIAL_ONLY |
2014 FILE_SYNCHRONOUS_IO_NONALERT,
2016 if (!NT_SUCCESS(status)) {
2017 winnt_error(status, L"Can't open \"%ls\" for writing",
2019 return WIMLIB_ERR_OPEN;
2022 ctx->open_handles[ctx->num_open_handles++] = h;
2024 /* Allocate space for the data. */
2025 alloc_info.AllocationSize.QuadPart = blob->size;
2026 NtSetInformationFile(h, &ctx->iosb, &alloc_info, sizeof(alloc_info),
2027 FileAllocationInformation);
2031 /* Given a Windows NT namespace path, such as \??\e:\Windows\System32, return a
2032 * pointer to the suffix of the path that begins with the device directly, such
2033 * as e:\Windows\System32. */
2034 static const wchar_t *
2035 skip_nt_toplevel_component(const wchar_t *path, size_t path_nchars)
2037 static const wchar_t * const dirs[] = {
2042 const wchar_t * const end = path + path_nchars;
2044 for (size_t i = 0; i < ARRAY_LEN(dirs); i++) {
2045 size_t len = wcslen(dirs[i]);
2046 if (len <= (end - path) && !wmemcmp(path, dirs[i], len)) {
2048 while (path != end && *path == L'\\')
2057 * Given a Windows NT namespace path, such as \??\e:\Windows\System32, return a
2058 * pointer to the suffix of the path that is device-relative but possibly with
2059 * leading slashes, such as \Windows\System32.
2061 * The path has an explicit length and is not necessarily null terminated.
2063 static const wchar_t *
2064 get_device_relative_path(const wchar_t *path, size_t path_nchars)
2066 const wchar_t * const orig_path = path;
2067 const wchar_t * const end = path + path_nchars;
2069 path = skip_nt_toplevel_component(path, path_nchars);
2070 if (path == orig_path)
2073 while (path != end && *path != L'\\')
2080 * Given a reparse point buffer for an inode for which the absolute link target
2081 * was relativized when it was archived, de-relative the link target to be
2082 * consistent with the actual extraction location.
2085 try_rpfix(struct reparse_buffer_disk *rpbuf, u16 *rpbuflen_p,
2086 struct win32_apply_ctx *ctx)
2088 struct link_reparse_point link;
2089 size_t orig_subst_name_nchars;
2090 const wchar_t *relpath;
2091 size_t relpath_nchars;
2092 size_t target_ntpath_nchars;
2093 size_t fixed_subst_name_nchars;
2094 const wchar_t *fixed_print_name;
2095 size_t fixed_print_name_nchars;
2097 /* Do nothing if the reparse data is invalid. */
2098 if (parse_link_reparse_point(rpbuf, *rpbuflen_p, &link))
2101 /* Do nothing if the reparse point is a relative symbolic link. */
2102 if (link_is_relative_symlink(&link))
2105 /* Build the new substitute name from the NT namespace path to the
2106 * target directory, then a path separator, then the "device relative"
2107 * part of the old substitute name. */
2109 orig_subst_name_nchars = link.substitute_name_nbytes / sizeof(wchar_t);
2111 relpath = get_device_relative_path(link.substitute_name,
2112 orig_subst_name_nchars);
2113 relpath_nchars = orig_subst_name_nchars -
2114 (relpath - link.substitute_name);
2116 target_ntpath_nchars = ctx->target_ntpath.Length / sizeof(wchar_t);
2118 /* If the target directory is a filesystem root, such as \??\C:\, then
2119 * it already will have a trailing slash. Don't include this slash if
2120 * we are already adding slashes via 'relpath'. This prevents an extra
2121 * slash from being generated each time the link is extracted. And
2122 * unlike on UNIX, the number of slashes in paths on Windows can be
2123 * significant; Windows won't understand the link target if it contains
2124 * too many slashes. */
2125 if (target_ntpath_nchars > 0 && relpath_nchars > 0 &&
2126 ctx->target_ntpath.Buffer[target_ntpath_nchars - 1] == L'\\')
2127 target_ntpath_nchars--;
2129 /* Also remove extra slashes from the beginning of 'relpath'. Normally
2130 * this isn't needed, but this is here to make the extra slash(es) added
2131 * by wimlib pre-v1.9.1 get removed automatically. */
2132 while (relpath_nchars >= 2 &&
2133 relpath[0] == L'\\' && relpath[1] == L'\\') {
2138 fixed_subst_name_nchars = target_ntpath_nchars + relpath_nchars;
2140 wchar_t fixed_subst_name[fixed_subst_name_nchars];
2142 wmemcpy(fixed_subst_name, ctx->target_ntpath.Buffer, target_ntpath_nchars);
2143 wmemcpy(&fixed_subst_name[target_ntpath_nchars], relpath, relpath_nchars);
2144 /* Doesn't need to be null-terminated. */
2146 /* Print name should be Win32, but not all NT names can even be
2147 * translated to Win32 names. But we can at least delete the top-level
2148 * directory, such as \??\, and this will have the expected result in
2149 * the usual case. */
2150 fixed_print_name = skip_nt_toplevel_component(fixed_subst_name,
2151 fixed_subst_name_nchars);
2152 fixed_print_name_nchars = fixed_subst_name_nchars - (fixed_print_name -
2155 link.substitute_name = fixed_subst_name;
2156 link.substitute_name_nbytes = fixed_subst_name_nchars * sizeof(wchar_t);
2157 link.print_name = (wchar_t *)fixed_print_name;
2158 link.print_name_nbytes = fixed_print_name_nchars * sizeof(wchar_t);
2159 make_link_reparse_point(&link, rpbuf, rpbuflen_p);
2162 /* Sets the reparse point on the specified file. This handles "fixing" the
2163 * targets of absolute symbolic links and junctions if WIMLIB_EXTRACT_FLAG_RPFIX
2166 set_reparse_point(const struct wim_dentry *dentry,
2167 const struct reparse_buffer_disk *rpbuf, u16 rpbuflen,
2168 struct win32_apply_ctx *ctx)
2170 if ((ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX)
2171 && !(dentry->d_inode->i_rp_flags & WIM_RP_FLAG_NOT_FIXED))
2173 memcpy(&ctx->rpfixbuf, rpbuf, rpbuflen);
2174 try_rpfix(&ctx->rpfixbuf, &rpbuflen, ctx);
2175 rpbuf = &ctx->rpfixbuf;
2177 return do_set_reparse_point(dentry, rpbuf, rpbuflen, ctx);
2181 /* Import the next block of raw encrypted data */
2183 import_encrypted_data(PBYTE pbData, PVOID pvCallbackContext, PULONG Length)
2185 struct win32_apply_ctx *ctx = pvCallbackContext;
2188 copy_len = min(ctx->encrypted_size - ctx->encrypted_offset, *Length);
2189 memcpy(pbData, &ctx->data_buffer[ctx->encrypted_offset], copy_len);
2190 ctx->encrypted_offset += copy_len;
2192 return ERROR_SUCCESS;
2196 * Write the raw encrypted data to the already-created file (or directory)
2197 * corresponding to @dentry.
2199 * The raw encrypted data is provided in ctx->data_buffer, and its size is
2200 * ctx->encrypted_size.
2202 * This function may close the target directory, in which case the caller needs
2203 * to re-open it if needed.
2206 extract_encrypted_file(const struct wim_dentry *dentry,
2207 struct win32_apply_ctx *ctx)
2214 /* Temporarily build a Win32 path for OpenEncryptedFileRaw() */
2215 build_win32_extraction_path(dentry, ctx);
2217 flags = CREATE_FOR_IMPORT | OVERWRITE_HIDDEN;
2218 if (dentry->d_inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)
2219 flags |= CREATE_FOR_DIR;
2223 err = OpenEncryptedFileRaw(ctx->pathbuf.Buffer, flags, &rawctx);
2224 if (err == ERROR_SHARING_VIOLATION && !retried) {
2225 /* This can be caused by the handle we have open to the target
2226 * directory. Try closing it temporarily. */
2227 close_target_directory(ctx);
2232 /* Restore the NT namespace path */
2233 build_extraction_path(dentry, ctx);
2235 if (err != ERROR_SUCCESS) {
2236 win32_error(err, L"Can't open \"%ls\" for encrypted import",
2238 return WIMLIB_ERR_OPEN;
2241 ctx->encrypted_offset = 0;
2243 err = WriteEncryptedFileRaw(import_encrypted_data, ctx, rawctx);
2245 CloseEncryptedFileRaw(rawctx);
2247 if (err != ERROR_SUCCESS) {
2248 win32_error(err, L"Can't import encrypted file \"%ls\"",
2250 return WIMLIB_ERR_WRITE;
2256 /* Called when starting to read a blob for extraction on Windows */
2258 begin_extract_blob(struct blob_descriptor *blob, void *_ctx)
2260 struct win32_apply_ctx *ctx = _ctx;
2261 const struct blob_extraction_target *targets = blob_extraction_targets(blob);
2264 ctx->num_open_handles = 0;
2265 ctx->data_buffer_ptr = NULL;
2266 INIT_LIST_HEAD(&ctx->reparse_dentries);
2267 INIT_LIST_HEAD(&ctx->encrypted_dentries);
2269 for (u32 i = 0; i < blob->out_refcnt; i++) {
2270 const struct wim_inode *inode = targets[i].inode;
2271 const struct wim_inode_stream *strm = targets[i].stream;
2272 struct wim_dentry *dentry;
2274 /* A copy of the blob needs to be extracted to @inode. */
2276 if (ctx->common.supported_features.hard_links) {
2277 dentry = inode_first_extraction_dentry(inode);
2278 ret = begin_extract_blob_instance(blob, dentry, strm, ctx);
2279 ret = check_apply_error(dentry, ctx, ret);
2283 /* Hard links not supported. Extract the blob
2284 * separately to each alias of the inode. */
2285 inode_for_each_extraction_alias(dentry, inode) {
2286 ret = begin_extract_blob_instance(blob, dentry, strm, ctx);
2287 ret = check_apply_error(dentry, ctx, ret);
2301 /* Called when the next chunk of a blob has been read for extraction on Windows
2304 extract_chunk(const void *chunk, size_t size, void *_ctx)
2306 struct win32_apply_ctx *ctx = _ctx;
2308 /* Write the data chunk to each open handle */
2309 for (unsigned i = 0; i < ctx->num_open_handles; i++) {
2310 u8 *bufptr = (u8 *)chunk;
2311 size_t bytes_remaining = size;
2313 while (bytes_remaining) {
2314 ULONG count = min(0xFFFFFFFF, bytes_remaining);
2316 status = NtWriteFile(ctx->open_handles[i],
2318 &ctx->iosb, bufptr, count,
2320 if (!NT_SUCCESS(status)) {
2321 winnt_error(status, L"Error writing data to target volume");
2322 return WIMLIB_ERR_WRITE;
2324 bufptr += ctx->iosb.Information;
2325 bytes_remaining -= ctx->iosb.Information;
2329 /* Copy the data chunk into the buffer (if needed) */
2330 if (ctx->data_buffer_ptr)
2331 ctx->data_buffer_ptr = mempcpy(ctx->data_buffer_ptr,
2337 get_system_compression_format(int extract_flags)
2339 if (extract_flags & WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS4K)
2340 return FILE_PROVIDER_COMPRESSION_FORMAT_XPRESS4K;
2342 if (extract_flags & WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS8K)
2343 return FILE_PROVIDER_COMPRESSION_FORMAT_XPRESS8K;
2345 if (extract_flags & WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS16K)
2346 return FILE_PROVIDER_COMPRESSION_FORMAT_XPRESS16K;
2348 return FILE_PROVIDER_COMPRESSION_FORMAT_LZX;
2352 static const wchar_t *
2353 get_system_compression_format_string(int format)
2356 case FILE_PROVIDER_COMPRESSION_FORMAT_XPRESS4K:
2358 case FILE_PROVIDER_COMPRESSION_FORMAT_XPRESS8K:
2360 case FILE_PROVIDER_COMPRESSION_FORMAT_XPRESS16K:
2361 return L"XPRESS16K";
2368 set_system_compression(HANDLE h, int format)
2372 struct wof_external_info wof_info;
2373 struct file_provider_external_info file_info;
2376 .version = WOF_CURRENT_VERSION,
2377 .provider = WOF_PROVIDER_FILE,
2380 .version = FILE_PROVIDER_CURRENT_VERSION,
2381 .compression_format = format,
2385 /* We intentionally use NtFsControlFile() rather than DeviceIoControl()
2386 * here because the "compressing this object would not save space"
2387 * status code does not map to a valid Win32 error code on older
2388 * versions of Windows (before Windows 10?). This can be a problem if
2389 * the WOFADK driver is being used rather than the regular WOF, since
2390 * WOFADK can be used on older versions of Windows. */
2391 status = winnt_fsctl(h, FSCTL_SET_EXTERNAL_BACKING,
2392 &in, sizeof(in), NULL, 0, NULL);
2394 if (status == 0xC000046F) /* "Compressing this object would not save space." */
2395 return STATUS_SUCCESS;
2400 /* Hard-coded list of files which the Windows bootloader may need to access
2401 * before the WOF driver has been loaded. */
2402 static wchar_t *bootloader_pattern_strings[] = {
2405 L"\\Windows\\AppPatch\\drvmain.sdb",
2406 L"\\Windows\\Boot\\DVD\\*",
2407 L"\\Windows\\Boot\\EFI\\*",
2408 L"\\Windows\\bootstat.dat",
2409 L"\\Windows\\Fonts\\vgaoem.fon",
2410 L"\\Windows\\Fonts\\vgasys.fon",
2411 L"\\Windows\\INF\\errata.inf",
2412 L"\\Windows\\System32\\config\\*",
2413 L"\\Windows\\System32\\ntkrnlpa.exe",
2414 L"\\Windows\\System32\\ntoskrnl.exe",
2415 L"\\Windows\\System32\\bootvid.dll",
2416 L"\\Windows\\System32\\ci.dll",
2417 L"\\Windows\\System32\\hal*.dll",
2418 L"\\Windows\\System32\\mcupdate_AuthenticAMD.dll",
2419 L"\\Windows\\System32\\mcupdate_GenuineIntel.dll",
2420 L"\\Windows\\System32\\pshed.dll",
2421 L"\\Windows\\System32\\apisetschema.dll",
2422 L"\\Windows\\System32\\api-ms-win*.dll",
2423 L"\\Windows\\System32\\ext-ms-win*.dll",
2424 L"\\Windows\\System32\\KernelBase.dll",
2425 L"\\Windows\\System32\\drivers\\*.sys",
2426 L"\\Windows\\System32\\*.nls",
2427 L"\\Windows\\System32\\kbd*.dll",
2428 L"\\Windows\\System32\\kd*.dll",
2429 L"\\Windows\\System32\\clfs.sys",
2430 L"\\Windows\\System32\\CodeIntegrity\\driver.stl",
2433 static const struct string_set bootloader_patterns = {
2434 .strings = bootloader_pattern_strings,
2435 .num_strings = ARRAY_LEN(bootloader_pattern_strings),
2439 set_system_compression_on_inode(struct wim_inode *inode, int format,
2440 struct win32_apply_ctx *ctx)
2442 bool retried = false;
2446 /* If it may be needed for compatibility with the Windows bootloader,
2447 * force this file to XPRESS4K or uncompressed format. The bootloader
2448 * of Windows 10 supports XPRESS4K only; older versions don't support
2449 * system compression at all. */
2450 if (!is_image_windows_10_or_later(ctx) ||
2451 format != FILE_PROVIDER_COMPRESSION_FORMAT_XPRESS4K)
2453 /* We need to check the patterns against every name of the
2454 * inode, in case any of them match. */
2455 struct wim_dentry *dentry;
2456 inode_for_each_extraction_alias(dentry, inode) {
2460 if (calculate_dentry_full_path(dentry)) {
2461 ERROR("Unable to compute file path!");
2462 return STATUS_NO_MEMORY;
2465 incompatible = match_pattern_list(dentry->d_full_path,
2466 &bootloader_patterns);
2467 FREE(dentry->d_full_path);
2468 dentry->d_full_path = NULL;
2473 warned = (ctx->num_system_compression_exclusions++ > 0);
2475 if (is_image_windows_10_or_later(ctx)) {
2476 /* Force to XPRESS4K */
2478 WARNING("For compatibility with the "
2479 "Windows bootloader, some "
2482 "using the XPRESS4K format "
2483 "instead of the %"TS" format\n"
2485 get_system_compression_format_string(format));
2487 format = FILE_PROVIDER_COMPRESSION_FORMAT_XPRESS4K;
2490 /* Force to uncompressed */
2492 WARNING("For compatibility with the "
2493 "Windows bootloader, some "
2495 " be compressed with"
2496 " system compression "
2497 "(\"compacted\").");
2499 return STATUS_SUCCESS;
2505 /* Open the extracted file. */
2506 status = create_file(&h, GENERIC_READ | GENERIC_WRITE, NULL,
2508 inode_first_extraction_dentry(inode), ctx);
2510 if (!NT_SUCCESS(status))
2513 /* Compress the file. If the attempt fails with "invalid device
2514 * request", then attach wof.sys (or wofadk.sys) and retry. */
2515 status = set_system_compression(h, format);
2516 if (unlikely(status == STATUS_INVALID_DEVICE_REQUEST && !retried)) {
2517 wchar_t drive_path[7];
2518 if (!win32_get_drive_path(ctx->common.target, drive_path) &&
2519 win32_try_to_attach_wof(drive_path + 4)) {
2530 * This function is called when doing a "compact-mode" extraction and we just
2531 * finished extracting a blob to one or more locations. For each location that
2532 * was the unnamed data stream of a file, this function compresses the
2533 * corresponding file using System Compression, if allowed.
2535 * Note: we're doing the compression immediately after extracting the data
2536 * rather than during a separate compression pass. This way should be faster
2537 * since the operating system should still have the file's data cached.
2539 * Note: we're having the operating system do the compression, which is not
2540 * ideal because wimlib could create the compressed data faster and more
2541 * efficiently (the compressed data format is identical to a WIM resource). But
2542 * we seemingly don't have a choice because WOF prevents applications from
2543 * creating its reparse points.
2546 handle_system_compression(struct blob_descriptor *blob, struct win32_apply_ctx *ctx)
2548 const struct blob_extraction_target *targets = blob_extraction_targets(blob);
2550 const int format = get_system_compression_format(ctx->common.extract_flags);
2552 for (u32 i = 0; i < blob->out_refcnt; i++) {
2553 struct wim_inode *inode = targets[i].inode;
2554 struct wim_inode_stream *strm = targets[i].stream;
2557 if (!stream_is_unnamed_data_stream(strm))
2560 if (will_externally_back_inode(inode, ctx, NULL, false) != 0)
2563 status = set_system_compression_on_inode(inode, format, ctx);
2564 if (likely(NT_SUCCESS(status)))
2567 if (status == STATUS_INVALID_DEVICE_REQUEST) {
2569 "The request to compress the extracted files using System Compression\n"
2570 " will not be honored because the operating system or target volume\n"
2571 " does not support it. System Compression is only supported on\n"
2572 " Windows 10 and later, and only on NTFS volumes.");
2573 ctx->common.extract_flags &= ~COMPACT_FLAGS;
2577 ctx->num_system_compression_failures++;
2578 if (ctx->num_system_compression_failures < 10) {
2579 winnt_warning(status, L"\"%ls\": Failed to compress "
2580 "extracted file using System Compression",
2582 } else if (ctx->num_system_compression_failures == 10) {
2583 WARNING("Suppressing further warnings about "
2584 "System Compression failures.");
2589 /* Called when a blob has been fully read for extraction on Windows */
2591 end_extract_blob(struct blob_descriptor *blob, int status, void *_ctx)
2593 struct win32_apply_ctx *ctx = _ctx;
2595 const struct wim_dentry *dentry;
2602 if (unlikely(ctx->common.extract_flags & COMPACT_FLAGS))
2603 handle_system_compression(blob, ctx);
2605 if (likely(!ctx->data_buffer_ptr))
2608 if (!list_empty(&ctx->reparse_dentries)) {
2609 if (blob->size > REPARSE_DATA_MAX_SIZE) {
2610 dentry = list_first_entry(&ctx->reparse_dentries,
2611 struct wim_dentry, d_tmp_list);
2612 build_extraction_path(dentry, ctx);
2613 ERROR("Reparse data of \"%ls\" has size "
2614 "%"PRIu64" bytes (exceeds %u bytes)",
2615 current_path(ctx), blob->size,
2616 REPARSE_DATA_MAX_SIZE);
2617 ret = WIMLIB_ERR_INVALID_REPARSE_DATA;
2618 return check_apply_error(dentry, ctx, ret);
2621 memcpy(ctx->rpbuf.rpdata, ctx->data_buffer, blob->size);
2623 list_for_each_entry(dentry, &ctx->reparse_dentries, d_tmp_list) {
2625 /* Reparse point header */
2626 complete_reparse_point(&ctx->rpbuf, dentry->d_inode,
2629 ret = set_reparse_point(dentry, &ctx->rpbuf,
2630 REPARSE_DATA_OFFSET + blob->size,
2632 ret = check_apply_error(dentry, ctx, ret);
2638 if (!list_empty(&ctx->encrypted_dentries)) {
2639 ctx->encrypted_size = blob->size;
2640 list_for_each_entry(dentry, &ctx->encrypted_dentries, d_tmp_list) {
2641 ret = extract_encrypted_file(dentry, ctx);
2642 ret = check_apply_error(dentry, ctx, ret);
2645 /* Re-open the target directory if needed. */
2646 ret = open_target_directory(ctx);
2655 /* Attributes that can't be set directly */
2656 #define SPECIAL_ATTRIBUTES \
2657 (FILE_ATTRIBUTE_REPARSE_POINT | \
2658 FILE_ATTRIBUTE_DIRECTORY | \
2659 FILE_ATTRIBUTE_ENCRYPTED | \
2660 FILE_ATTRIBUTE_SPARSE_FILE | \
2661 FILE_ATTRIBUTE_COMPRESSED)
2663 /* Set the security descriptor @desc, of @desc_size bytes, on the file with open
2666 set_security_descriptor(HANDLE h, const void *_desc,
2667 size_t desc_size, struct win32_apply_ctx *ctx)
2669 SECURITY_INFORMATION info;
2671 SECURITY_DESCRIPTOR_RELATIVE *desc;
2674 * Ideally, we would just pass in the security descriptor buffer as-is.
2675 * But it turns out that Windows can mess up the security descriptor
2676 * even when using the low-level NtSetSecurityObject() function:
2678 * - Windows will clear SE_DACL_AUTO_INHERITED if it is set in the
2679 * passed buffer. To actually get Windows to set
2680 * SE_DACL_AUTO_INHERITED, the application must set the non-persistent
2681 * flag SE_DACL_AUTO_INHERIT_REQ. As usual, Microsoft didn't bother
2682 * to properly document either of these flags. It's unclear how
2683 * important SE_DACL_AUTO_INHERITED actually is, but to be safe we use
2684 * the SE_DACL_AUTO_INHERIT_REQ workaround to set it if needed.
2686 * - The above also applies to the equivalent SACL flags,
2687 * SE_SACL_AUTO_INHERITED and SE_SACL_AUTO_INHERIT_REQ.
2689 * - If the application says that it's setting
2690 * DACL_SECURITY_INFORMATION, then Windows sets SE_DACL_PRESENT in the
2691 * resulting security descriptor, even if the security descriptor the
2692 * application provided did not have a DACL. This seems to be
2693 * unavoidable, since omitting DACL_SECURITY_INFORMATION would cause a
2694 * default DACL to remain. Fortunately, this behavior seems harmless,
2695 * since the resulting DACL will still be "null" --- but it will be
2696 * "the other representation of null".
2698 * - The above also applies to SACL_SECURITY_INFORMATION and
2699 * SE_SACL_PRESENT. Again, it's seemingly unavoidable but "harmless"
2700 * that Windows changes the representation of a "null SACL".
2702 if (likely(desc_size <= STACK_MAX)) {
2703 desc = alloca(desc_size);
2705 desc = MALLOC(desc_size);
2707 return STATUS_NO_MEMORY;
2710 memcpy(desc, _desc, desc_size);
2712 if (likely(desc_size >= 4)) {
2714 if (desc->Control & SE_DACL_AUTO_INHERITED)
2715 desc->Control |= SE_DACL_AUTO_INHERIT_REQ;
2717 if (desc->Control & SE_SACL_AUTO_INHERITED)
2718 desc->Control |= SE_SACL_AUTO_INHERIT_REQ;
2722 * More API insanity. We want to set the entire security descriptor
2723 * as-is. But all available APIs require specifying the specific parts
2724 * of the security descriptor being set. Especially annoying is that
2725 * mandatory integrity labels are part of the SACL, but they aren't set
2726 * with SACL_SECURITY_INFORMATION. Instead, applications must also
2727 * specify LABEL_SECURITY_INFORMATION (Windows Vista, Windows 7) or
2728 * BACKUP_SECURITY_INFORMATION (Windows 8). But at least older versions
2729 * of Windows don't error out if you provide these newer flags...
2731 * Also, if the process isn't running as Administrator, then it probably
2732 * doesn't have SE_RESTORE_PRIVILEGE. In this case, it will always get
2733 * the STATUS_PRIVILEGE_NOT_HELD error by trying to set the SACL, even
2734 * if the security descriptor it provided did not have a SACL. By
2735 * default, in this case we try to recover and set as much of the
2736 * security descriptor as possible --- potentially excluding the DACL, and
2737 * even the owner, as well as the SACL.
2740 info = OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
2741 DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION |
2742 LABEL_SECURITY_INFORMATION | BACKUP_SECURITY_INFORMATION;
2746 * It's also worth noting that SetFileSecurity() is unusable because it
2747 * doesn't request "backup semantics" when it opens the file internally.
2748 * NtSetSecurityObject() seems to be the best function to use in backup
2749 * applications. (SetSecurityInfo() should also work, but it's harder
2750 * to use and must call NtSetSecurityObject() internally anyway.
2751 * BackupWrite() is theoretically usable as well, but it's inflexible
2752 * and poorly documented.)
2756 status = NtSetSecurityObject(h, info, desc);
2757 if (NT_SUCCESS(status))
2758 goto out_maybe_free_desc;
2760 /* Failed to set the requested parts of the security descriptor. If the
2761 * error was permissions-related, try to set fewer parts of the security
2762 * descriptor, unless WIMLIB_EXTRACT_FLAG_STRICT_ACLS is enabled. */
2763 if ((status == STATUS_PRIVILEGE_NOT_HELD ||
2764 status == STATUS_ACCESS_DENIED) &&
2765 !(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_ACLS))
2767 if (info & SACL_SECURITY_INFORMATION) {
2768 info &= ~(SACL_SECURITY_INFORMATION |
2769 LABEL_SECURITY_INFORMATION |
2770 BACKUP_SECURITY_INFORMATION);
2771 ctx->partial_security_descriptors++;
2774 if (info & DACL_SECURITY_INFORMATION) {
2775 info &= ~DACL_SECURITY_INFORMATION;
2778 if (info & OWNER_SECURITY_INFORMATION) {
2779 info &= ~OWNER_SECURITY_INFORMATION;
2782 /* Nothing left except GROUP, and if we removed it we
2783 * wouldn't have anything at all. */
2786 /* No part of the security descriptor could be set, or
2787 * WIMLIB_EXTRACT_FLAG_STRICT_ACLS is enabled and the full security
2788 * descriptor could not be set. */
2789 if (!(info & SACL_SECURITY_INFORMATION))
2790 ctx->partial_security_descriptors--;
2791 ctx->no_security_descriptors++;
2793 out_maybe_free_desc:
2794 if (unlikely(desc_size > STACK_MAX))
2799 /* Set metadata on the open file @h from the WIM inode @inode. */
2801 do_apply_metadata_to_file(HANDLE h, const struct wim_inode *inode,
2802 struct win32_apply_ctx *ctx)
2804 FILE_BASIC_INFORMATION info;
2807 /* Set security descriptor if present and not in NO_ACLS mode */
2808 if (inode_has_security_descriptor(inode) &&
2809 !(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_NO_ACLS))
2811 const struct wim_security_data *sd;
2815 sd = wim_get_current_security_data(ctx->common.wim);
2816 desc = sd->descriptors[inode->i_security_id];
2817 desc_size = sd->sizes[inode->i_security_id];
2819 status = set_security_descriptor(h, desc, desc_size, ctx);
2820 if (!NT_SUCCESS(status) &&
2821 (ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_ACLS))
2824 L"Can't set security descriptor on \"%ls\"",
2826 return WIMLIB_ERR_SET_SECURITY;
2830 /* Set attributes and timestamps */
2831 info.CreationTime.QuadPart = inode->i_creation_time;
2832 info.LastAccessTime.QuadPart = inode->i_last_access_time;
2833 info.LastWriteTime.QuadPart = inode->i_last_write_time;
2834 info.ChangeTime.QuadPart = 0;
2835 if (ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES) {
2836 info.FileAttributes = FILE_ATTRIBUTE_NORMAL;
2838 info.FileAttributes = inode->i_attributes & ~SPECIAL_ATTRIBUTES;
2839 if (info.FileAttributes == 0)
2840 info.FileAttributes = FILE_ATTRIBUTE_NORMAL;
2843 status = NtSetInformationFile(h, &ctx->iosb, &info, sizeof(info),
2844 FileBasicInformation);
2845 /* On FAT volumes we get STATUS_INVALID_PARAMETER if we try to set
2846 * attributes on the root directory. (Apparently because FAT doesn't
2847 * actually have a place to store those attributes!) */
2848 if (!NT_SUCCESS(status)
2849 && !(status == STATUS_INVALID_PARAMETER &&
2850 dentry_is_root(inode_first_extraction_dentry(inode))))
2852 winnt_error(status, L"Can't set basic metadata on \"%ls\"",
2854 return WIMLIB_ERR_SET_ATTRIBUTES;
2861 apply_metadata_to_file(const struct wim_dentry *dentry,
2862 struct win32_apply_ctx *ctx)
2864 const struct wim_inode *inode = dentry->d_inode;
2870 perms = FILE_WRITE_ATTRIBUTES | WRITE_DAC |
2871 WRITE_OWNER | ACCESS_SYSTEM_SECURITY;
2873 build_extraction_path(dentry, ctx);
2875 /* Open a handle with as many relevant permissions as possible. */
2876 while (!NT_SUCCESS(status = do_create_file(&h, perms, NULL,
2877 0, FILE_OPEN, 0, ctx)))
2879 if (status == STATUS_PRIVILEGE_NOT_HELD ||
2880 status == STATUS_ACCESS_DENIED)
2882 if (perms & ACCESS_SYSTEM_SECURITY) {
2883 perms &= ~ACCESS_SYSTEM_SECURITY;
2886 if (perms & WRITE_DAC) {
2887 perms &= ~WRITE_DAC;
2890 if (perms & WRITE_OWNER) {
2891 perms &= ~WRITE_OWNER;
2895 winnt_error(status, L"Can't open \"%ls\" to set metadata",
2897 return WIMLIB_ERR_OPEN;
2900 ret = do_apply_metadata_to_file(h, inode, ctx);
2908 apply_metadata(struct list_head *dentry_list, struct win32_apply_ctx *ctx)
2910 const struct wim_dentry *dentry;
2913 /* We go in reverse so that metadata is set on all a directory's
2914 * children before the directory itself. This avoids any potential
2915 * problems with attributes, timestamps, or security descriptors. */
2916 list_for_each_entry_reverse(dentry, dentry_list, d_extraction_list_node)
2918 ret = apply_metadata_to_file(dentry, ctx);
2919 ret = check_apply_error(dentry, ctx, ret);
2922 ret = report_file_metadata_applied(&ctx->common);
2929 /* Issue warnings about problems during the extraction for which warnings were
2930 * not already issued (due to the high number of potential warnings if we issued
2931 * them per-file). */
2933 do_warnings(const struct win32_apply_ctx *ctx)
2935 if (ctx->partial_security_descriptors == 0
2936 && ctx->no_security_descriptors == 0
2937 && ctx->num_set_short_name_failures == 0
2939 && ctx->num_remove_short_name_failures == 0
2944 WARNING("Extraction to \"%ls\" complete, but with one or more warnings:",
2945 ctx->common.target);
2946 if (ctx->num_set_short_name_failures) {
2947 WARNING("- Could not set short names on %lu files or directories",
2948 ctx->num_set_short_name_failures);
2951 if (ctx->num_remove_short_name_failures) {
2952 WARNING("- Could not remove short names on %lu files or directories"
2953 " (This is expected on Vista and earlier)",
2954 ctx->num_remove_short_name_failures);
2957 if (ctx->partial_security_descriptors) {
2958 WARNING("- Could only partially set the security descriptor\n"
2959 " on %lu files or directories.",
2960 ctx->partial_security_descriptors);
2962 if (ctx->no_security_descriptors) {
2963 WARNING("- Could not set security descriptor at all\n"
2964 " on %lu files or directories.",
2965 ctx->no_security_descriptors);
2967 if (ctx->partial_security_descriptors || ctx->no_security_descriptors) {
2968 WARNING("To fully restore all security descriptors, run the program\n"
2969 " with Administrator rights.");
2974 count_dentries(const struct list_head *dentry_list)
2976 const struct list_head *cur;
2979 list_for_each(cur, dentry_list)
2985 /* Extract files from a WIM image to a directory on Windows */
2987 win32_extract(struct list_head *dentry_list, struct apply_ctx *_ctx)
2990 struct win32_apply_ctx *ctx = (struct win32_apply_ctx *)_ctx;
2993 ret = prepare_target(dentry_list, ctx);
2997 if (unlikely(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_WIMBOOT)) {
2998 ret = start_wimboot_extraction(dentry_list, ctx);
3003 ctx->windows_build_number = xml_get_windows_build_number(ctx->common.wim->xml_info,
3004 ctx->common.wim->current_image);
3006 dentry_count = count_dentries(dentry_list);
3008 ret = start_file_structure_phase(&ctx->common, dentry_count);
3012 ret = create_directories(dentry_list, ctx);
3016 ret = create_nondirectories(dentry_list, ctx);
3020 ret = end_file_structure_phase(&ctx->common);
3024 struct read_blob_callbacks cbs = {
3025 .begin_blob = begin_extract_blob,
3026 .consume_chunk = extract_chunk,
3027 .end_blob = end_extract_blob,
3030 ret = extract_blob_list(&ctx->common, &cbs);
3034 ret = start_file_metadata_phase(&ctx->common, dentry_count);
3038 ret = apply_metadata(dentry_list, ctx);
3042 ret = end_file_metadata_phase(&ctx->common);
3046 if (unlikely(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_WIMBOOT)) {
3047 ret = end_wimboot_extraction(ctx);
3054 close_target_directory(ctx);
3055 if (ctx->target_ntpath.Buffer)
3056 HeapFree(GetProcessHeap(), 0, ctx->target_ntpath.Buffer);
3057 FREE(ctx->pathbuf.Buffer);
3058 FREE(ctx->print_buffer);
3059 FREE(ctx->wimboot.wims);
3060 if (ctx->prepopulate_pats) {
3061 FREE(ctx->prepopulate_pats->strings);
3062 FREE(ctx->prepopulate_pats);
3064 FREE(ctx->mem_prepopulate_pats);
3065 FREE(ctx->data_buffer);
3069 const struct apply_operations win32_apply_ops = {
3071 .get_supported_features = win32_get_supported_features,
3072 .extract = win32_extract,
3073 .will_back_from_wim = win32_will_back_from_wim,
3074 .context_size = sizeof(struct win32_apply_ctx),
3077 #endif /* __WIN32__ */