2 * win32_apply.c - Windows-specific code for applying files from a WIM image.
6 * Copyright 2013-2023 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 https://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/dentry.h"
34 #include "wimlib/encoding.h"
35 #include "wimlib/error.h"
36 #include "wimlib/metadata.h"
37 #include "wimlib/object_id.h"
38 #include "wimlib/paths.h"
39 #include "wimlib/pattern.h"
40 #include "wimlib/reparse.h"
41 #include "wimlib/scan.h" /* for mangle_pat() and match_pattern_list() */
42 #include "wimlib/textfile.h"
43 #include "wimlib/wimboot.h"
44 #include "wimlib/wof.h"
45 #include "wimlib/xattr.h"
46 #include "wimlib/xml.h"
48 struct win32_apply_ctx {
50 /* Extract flags, the pointer to the WIMStruct, etc. */
51 struct apply_ctx common;
53 /* WIMBoot information, only filled in if WIMLIB_EXTRACT_FLAG_WIMBOOT
56 /* This array contains the WIM files registered with WOF on the
57 * target volume for this extraction operation. All WIMStructs
58 * in this array are distinct and have ->filename != NULL. */
62 u8 blob_table_hash[SHA1_HASH_SIZE];
66 bool have_wrong_version_wims;
67 bool have_uncompressed_wims;
68 bool have_unsupported_compressed_resources;
69 bool have_huge_resources;
72 /* External backing information */
73 struct string_list *prepopulate_pats;
74 void *mem_prepopulate_pats;
75 bool tried_to_load_prepopulate_list;
77 /* Open handle to the target directory */
80 /* NT namespace path to the target directory (buffer allocated) */
81 UNICODE_STRING target_ntpath;
83 /* Temporary buffer for building paths (buffer allocated) */
84 UNICODE_STRING pathbuf;
86 /* Object attributes to reuse for opening files in the target directory.
87 * (attr.ObjectName == &pathbuf) and (attr.RootDirectory == h_target).
89 OBJECT_ATTRIBUTES attr;
91 /* Temporary I/O status block for system calls */
94 /* Allocated buffer for creating "printable" paths from our
95 * target-relative NT paths */
96 wchar_t *print_buffer;
98 /* Allocated buffer for reading blob data when it cannot be extracted
102 /* Pointer to the next byte in @data_buffer to fill */
105 /* Size allocated in @data_buffer */
106 size_t data_buffer_size;
108 /* Current offset in the raw encrypted file being written */
109 size_t encrypted_offset;
111 /* Current size of the raw encrypted file being written */
112 size_t encrypted_size;
114 /* Temporary buffer for reparse data */
115 struct reparse_buffer_disk rpbuf;
117 /* Temporary buffer for reparse data of "fixed" absolute symbolic links
119 struct reparse_buffer_disk rpfixbuf;
121 /* Array of open handles to filesystem streams currently being written
123 HANDLE open_handles[MAX_OPEN_FILES];
125 /* Number of handles in @open_handles currently open (filled in from the
126 * beginning of the array) */
127 unsigned num_open_handles;
129 /* For each currently open stream, whether we're writing to it in
130 * "sparse" mode or not. */
131 bool is_sparse_stream[MAX_OPEN_FILES];
133 /* Whether is_sparse_stream[] is true for any currently open stream */
134 bool any_sparse_streams;
136 /* List of dentries, joined by @d_tmp_list, that need to have reparse
137 * data extracted as soon as the whole blob has been read into
139 struct list_head reparse_dentries;
141 /* List of dentries, joined by @d_tmp_list, that need to have raw
142 * encrypted data extracted as soon as the whole blob has been read into
144 struct list_head encrypted_dentries;
146 /* Number of files for which we didn't have permission to set the full
147 * security descriptor. */
148 unsigned long partial_security_descriptors;
150 /* Number of files for which we didn't have permission to set any part
151 * of the security descriptor. */
152 unsigned long no_security_descriptors;
154 /* Number of files for which we couldn't set the short name. */
155 unsigned long num_set_short_name_failures;
157 /* Number of files for which we couldn't remove the short name. */
158 unsigned long num_remove_short_name_failures;
160 /* Number of files on which we couldn't set System Compression. */
161 unsigned long num_system_compression_failures;
163 /* The number of files which, for compatibility with the Windows
164 * bootloader, were not compressed using the requested system
165 * compression format. This includes matches with the hardcoded pattern
166 * list only; it does not include matches with patterns in
167 * [PrepopulateList]. */
168 unsigned long num_system_compression_exclusions;
170 /* Number of files for which we couldn't set the object ID. */
171 unsigned long num_object_id_failures;
173 /* Number of files for which we couldn't set extended attributes. */
174 unsigned long num_xattr_failures;
176 /* The Windows build number of the image being applied, or 0 if unknown.
178 u64 windows_build_number;
180 /* Have we tried to enable short name support on the target volume yet?
182 bool tried_to_enable_short_names;
185 /* Get the drive letter from a Windows path, or return the null character if the
186 * path is relative. */
188 get_drive_letter(const wchar_t *path)
190 /* Skip \\?\ prefix */
191 if (!wcsncmp(path, L"\\\\?\\", 4))
194 /* Return drive letter if valid */
195 if (((path[0] >= L'a' && path[0] <= L'z') ||
196 (path[0] >= L'A' && path[0] <= L'Z')) && path[1] == L':')
203 get_vol_flags(const wchar_t *target, DWORD *vol_flags_ret,
204 bool *short_names_supported_ret)
206 wchar_t filesystem_name[MAX_PATH + 1];
208 wchar_t *volume = NULL;
211 *short_names_supported_ret = false;
213 drive[0] = get_drive_letter(target);
221 if (!GetVolumeInformation(volume, NULL, 0, NULL, NULL,
222 vol_flags_ret, filesystem_name,
223 ARRAY_LEN(filesystem_name)))
225 win32_warning(GetLastError(),
226 L"Failed to get volume information for \"%ls\"",
231 if (wcsstr(filesystem_name, L"NTFS")) {
233 * FILE_SUPPORTS_HARD_LINKS and
234 * FILE_SUPPORTS_EXTENDED_ATTRIBUTES are only supported on
235 * Windows 7 and later. Force them on anyway if the filesystem
238 *vol_flags_ret |= FILE_SUPPORTS_HARD_LINKS;
239 *vol_flags_ret |= FILE_SUPPORTS_EXTENDED_ATTRIBUTES;
241 /* There's no volume flag for short names, but according to the
242 * MS documentation they are only user-settable on NTFS. */
243 *short_names_supported_ret = true;
247 static const wchar_t *
248 current_path(struct win32_apply_ctx *ctx);
251 build_extraction_path(const struct wim_dentry *dentry,
252 struct win32_apply_ctx *ctx);
255 report_dentry_apply_error(const struct wim_dentry *dentry,
256 struct win32_apply_ctx *ctx, int ret)
258 build_extraction_path(dentry, ctx);
259 return report_apply_error(&ctx->common, ret, current_path(ctx));
263 check_apply_error(const struct wim_dentry *dentry,
264 struct win32_apply_ctx *ctx, int ret)
267 ret = report_dentry_apply_error(dentry, ctx, ret);
272 win32_get_supported_features(const wchar_t *target,
273 struct wim_features *supported_features)
276 bool short_names_supported;
278 /* Query the features of the target volume. */
280 get_vol_flags(target, &vol_flags, &short_names_supported);
282 supported_features->readonly_files = 1;
283 supported_features->hidden_files = 1;
284 supported_features->system_files = 1;
285 supported_features->archive_files = 1;
287 if (vol_flags & FILE_FILE_COMPRESSION)
288 supported_features->compressed_files = 1;
290 if (vol_flags & FILE_SUPPORTS_ENCRYPTION) {
291 supported_features->encrypted_files = 1;
292 supported_features->encrypted_directories = 1;
295 supported_features->not_context_indexed_files = 1;
297 if (vol_flags & FILE_SUPPORTS_SPARSE_FILES)
298 supported_features->sparse_files = 1;
300 if (vol_flags & FILE_NAMED_STREAMS)
301 supported_features->named_data_streams = 1;
303 if (vol_flags & FILE_SUPPORTS_HARD_LINKS)
304 supported_features->hard_links = 1;
306 if (vol_flags & FILE_SUPPORTS_REPARSE_POINTS)
307 supported_features->reparse_points = 1;
309 if (vol_flags & FILE_PERSISTENT_ACLS)
310 supported_features->security_descriptors = 1;
312 if (short_names_supported)
313 supported_features->short_names = 1;
315 if (vol_flags & FILE_SUPPORTS_OBJECT_IDS)
316 supported_features->object_ids = 1;
318 supported_features->timestamps = 1;
320 if (vol_flags & FILE_CASE_SENSITIVE_SEARCH) {
322 * The filesystem supports case-sensitive filenames. But does
323 * the operating system as well? This normally requires the
324 * registry setting ObCaseInsensitive=0. We can test it
325 * indirectly by attempting to open the "\SystemRoot" symbolic
326 * link using a name with the wrong case. If we get
327 * STATUS_OBJECT_NAME_NOT_FOUND instead of STATUS_ACCESS_DENIED,
328 * then case-sensitive names must be enabled.
331 OBJECT_ATTRIBUTES attr;
335 RtlInitUnicodeString(&path, L"\\systemroot");
336 InitializeObjectAttributes(&attr, &path, 0, NULL, NULL);
338 status = NtOpenSymbolicLinkObject(&h, 0, &attr);
339 if (status == STATUS_OBJECT_NAME_NOT_FOUND)
340 supported_features->case_sensitive_filenames = 1;
343 if (vol_flags & FILE_SUPPORTS_EXTENDED_ATTRIBUTES)
344 supported_features->xattrs = 1;
349 #define COMPACT_FLAGS (WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS4K | \
350 WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS8K | \
351 WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS16K | \
352 WIMLIB_EXTRACT_FLAG_COMPACT_LZX)
357 * If not done already, load the patterns from the [PrepopulateList] section of
358 * WimBootCompress.ini in the WIM image being extracted.
360 * Note: WimBootCompress.ini applies to both types of "external backing":
362 * - WIM backing ("WIMBoot" - Windows 8.1 and later)
363 * - File backing ("System Compression" - Windows 10 and later)
366 load_prepopulate_pats(struct win32_apply_ctx *ctx)
368 const wchar_t *path = L"\\Windows\\System32\\WimBootCompress.ini";
369 struct wim_dentry *dentry;
370 const struct blob_descriptor *blob;
373 struct string_list *strings;
375 struct text_file_section sec;
377 if (ctx->tried_to_load_prepopulate_list)
380 ctx->tried_to_load_prepopulate_list = true;
382 dentry = get_dentry(ctx->common.wim, path, WIMLIB_CASE_INSENSITIVE);
384 (dentry->d_inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
385 FILE_ATTRIBUTE_REPARSE_POINT |
386 FILE_ATTRIBUTE_ENCRYPTED)) ||
387 !(blob = inode_get_blob_for_unnamed_data_stream(dentry->d_inode,
388 ctx->common.wim->blob_table)))
390 WARNING("%ls does not exist in the WIM image.\n"
391 " The default configuration will be used instead; it assumes that all\n"
392 " files are valid for external backing regardless of path, equivalent\n"
393 " to an empty [PrepopulateList] section.", path);
394 return WIMLIB_ERR_PATH_DOES_NOT_EXIST;
397 ret = read_blob_into_alloc_buf(blob, &buf);
401 strings = CALLOC(1, sizeof(struct string_list));
404 return WIMLIB_ERR_NOMEM;
407 sec.name = T("PrepopulateList");
408 sec.strings = strings;
410 ret = load_text_file(path, buf, blob->size, &mem, &sec, 1,
411 LOAD_TEXT_FILE_REMOVE_QUOTES |
412 LOAD_TEXT_FILE_NO_WARNINGS,
414 STATIC_ASSERT(OS_PREFERRED_PATH_SEPARATOR == WIM_PATH_SEPARATOR);
420 ctx->prepopulate_pats = strings;
421 ctx->mem_prepopulate_pats = mem;
425 /* Returns %true if the specified absolute path to a file in the WIM image can
426 * be subject to external backing when extracted. Otherwise returns %false. */
428 can_externally_back_path(const wchar_t *path, const struct win32_apply_ctx *ctx)
430 /* Does the path match a pattern given in the [PrepopulateList] section
431 * of WimBootCompress.ini? */
432 if (ctx->prepopulate_pats && match_pattern_list(path, ctx->prepopulate_pats,
436 /* Since we attempt to modify the SYSTEM registry after it's extracted
437 * (see end_wimboot_extraction()), it can't be extracted as externally
438 * backed. This extends to associated files such as SYSTEM.LOG that
439 * also must be writable in order to write to the registry. Normally,
440 * SYSTEM is in [PrepopulateList], and the SYSTEM.* files match patterns
441 * in [ExclusionList] and therefore are not captured in the WIM at all.
442 * However, a WIM that wasn't specifically captured in "WIMBoot mode"
443 * may contain SYSTEM.* files. So to make things "just work", hard-code
445 if (match_path(path, L"\\Windows\\System32\\config\\SYSTEM*", 0))
451 /* Can the specified WIM resource be used as the source of an external backing
452 * for the wof.sys WIM provider? */
454 is_resource_valid_for_external_backing(const struct wim_resource_descriptor *rdesc,
455 struct win32_apply_ctx *ctx)
457 /* Must be the original WIM file format. This check excludes pipable
458 * resources and solid resources. It also excludes other resources
459 * contained in such files even if they would be otherwise compatible.
461 if (rdesc->wim->hdr.magic != WIM_MAGIC ||
462 rdesc->wim->hdr.wim_version != WIM_VERSION_DEFAULT)
464 ctx->wimboot.have_wrong_version_wims = true;
469 * Whitelist of compression types and chunk sizes supported by
470 * Microsoft's WOF driver.
473 * - Uncompressed WIMs result in BSOD. However, this only applies to
474 * the WIM file itself, not to uncompressed resources in a WIM file
475 * that is otherwise compressed.
476 * - XPRESS 64K sometimes appears to work, but sometimes it causes
477 * reads to fail with STATUS_UNSUCCESSFUL.
479 switch (rdesc->compression_type) {
480 case WIMLIB_COMPRESSION_TYPE_NONE:
481 if (rdesc->wim->compression_type == WIMLIB_COMPRESSION_TYPE_NONE) {
482 ctx->wimboot.have_uncompressed_wims = true;
486 case WIMLIB_COMPRESSION_TYPE_XPRESS:
487 switch (rdesc->chunk_size) {
494 ctx->wimboot.have_unsupported_compressed_resources = true;
498 case WIMLIB_COMPRESSION_TYPE_LZX:
499 switch (rdesc->chunk_size) {
503 ctx->wimboot.have_unsupported_compressed_resources = true;
508 ctx->wimboot.have_unsupported_compressed_resources = true;
512 /* Microsoft's WoF driver errors out if it tries to satisfy a read with
513 * ending offset >= 4 GiB from an externally backed file. */
514 if (rdesc->uncompressed_size > 4200000000) {
515 ctx->wimboot.have_huge_resources = true;
522 #define EXTERNAL_BACKING_NOT_ENABLED -1
523 #define EXTERNAL_BACKING_NOT_POSSIBLE -2
524 #define EXTERNAL_BACKING_EXCLUDED -3
527 * Determines whether the specified file will be externally backed. Returns a
528 * negative status code if no, 0 if yes, or a positive wimlib error code on
529 * error. If the file is excluded from external backing based on its path, then
530 * *excluded_dentry_ret is set to the dentry for the path that matched the
533 * Note that this logic applies to both types of "external backing":
535 * - WIM backing ("WIMBoot" - Windows 8.1 and later)
536 * - File backing ("System Compression" - Windows 10 and later)
538 * However, in the case of WIM backing we also need to validate that the WIM
539 * resource that would be the source of the backing is supported by the wof.sys
543 will_externally_back_inode(struct wim_inode *inode, struct win32_apply_ctx *ctx,
544 const struct wim_dentry **excluded_dentry_ret,
547 struct wim_dentry *dentry;
548 struct blob_descriptor *blob;
551 if (load_prepopulate_pats(ctx) == WIMLIB_ERR_NOMEM)
552 return WIMLIB_ERR_NOMEM;
554 if (inode->i_can_externally_back)
557 /* This may do redundant checks because the cached value
558 * i_can_externally_back is 2-state (as opposed to 3-state:
559 * unknown/no/yes). But most files can be externally backed, so this
562 if (inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
563 FILE_ATTRIBUTE_REPARSE_POINT |
564 FILE_ATTRIBUTE_ENCRYPTED))
565 return EXTERNAL_BACKING_NOT_POSSIBLE;
567 blob = inode_get_blob_for_unnamed_data_stream_resolved(inode);
570 return EXTERNAL_BACKING_NOT_POSSIBLE;
573 (blob->blob_location != BLOB_IN_WIM ||
574 !is_resource_valid_for_external_backing(blob->rdesc, ctx)))
575 return EXTERNAL_BACKING_NOT_POSSIBLE;
578 * We need to check the patterns in [PrepopulateList] against every name
579 * of the inode, in case any of them match.
582 inode_for_each_extraction_alias(dentry, inode) {
584 ret = calculate_dentry_full_path(dentry);
588 if (!can_externally_back_path(dentry->d_full_path, ctx)) {
589 if (excluded_dentry_ret)
590 *excluded_dentry_ret = dentry;
591 return EXTERNAL_BACKING_EXCLUDED;
595 inode->i_can_externally_back = 1;
600 * Determines if the unnamed data stream of a file will be created as a WIM
601 * external backing (a "WIMBoot pointer file"), as opposed to a standard
605 win32_will_back_from_wim(struct wim_dentry *dentry, struct apply_ctx *_ctx)
607 struct win32_apply_ctx *ctx = (struct win32_apply_ctx *)_ctx;
609 if (!(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_WIMBOOT))
610 return EXTERNAL_BACKING_NOT_ENABLED;
612 return will_externally_back_inode(dentry->d_inode, ctx, NULL, true);
615 /* Find the WOF registration information for the specified WIM file. */
616 static struct wimboot_wim *
617 find_wimboot_wim(WIMStruct *wim_to_find, struct win32_apply_ctx *ctx)
619 for (size_t i = 0; i < ctx->wimboot.num_wims; i++)
620 if (wim_to_find == ctx->wimboot.wims[i].wim)
621 return &ctx->wimboot.wims[i];
628 set_backed_from_wim(HANDLE h, struct wim_inode *inode, struct win32_apply_ctx *ctx)
631 const struct wim_dentry *excluded_dentry;
632 const struct blob_descriptor *blob;
633 const struct wimboot_wim *wimboot_wim;
635 ret = will_externally_back_inode(inode, ctx, &excluded_dentry, true);
636 if (ret > 0) /* Error. */
639 if (ret < 0 && ret != EXTERNAL_BACKING_EXCLUDED)
640 return 0; /* Not externally backing, other than due to exclusion. */
642 if (unlikely(ret == EXTERNAL_BACKING_EXCLUDED)) {
643 /* Not externally backing due to exclusion. */
644 union wimlib_progress_info info;
646 build_extraction_path(excluded_dentry, ctx);
648 info.wimboot_exclude.path_in_wim = excluded_dentry->d_full_path;
649 info.wimboot_exclude.extraction_path = current_path(ctx);
651 return call_progress(ctx->common.progfunc,
652 WIMLIB_PROGRESS_MSG_WIMBOOT_EXCLUDE,
653 &info, ctx->common.progctx);
656 /* Externally backing. */
658 blob = inode_get_blob_for_unnamed_data_stream_resolved(inode);
659 wimboot_wim = find_wimboot_wim(blob->rdesc->wim, ctx);
661 if (unlikely(!wimboot_set_pointer(h,
663 wimboot_wim->data_source_id,
664 wimboot_wim->blob_table_hash,
665 ctx->wimboot.wof_running)))
667 const DWORD err = GetLastError();
669 build_extraction_path(inode_first_extraction_dentry(inode), ctx);
670 win32_error(err, L"\"%ls\": Couldn't set WIMBoot pointer data",
672 return WIMLIB_ERR_WIMBOOT;
677 /* Calculates the SHA-1 message digest of the WIM's blob table. */
679 hash_blob_table(WIMStruct *wim, u8 hash[SHA1_HASH_SIZE])
681 return wim_reshdr_to_hash(&wim->hdr.blob_table_reshdr, wim, hash);
685 register_wim_with_wof(WIMStruct *wim, struct win32_apply_ctx *ctx)
687 struct wimboot_wim *p;
690 /* Check if already registered */
691 for (size_t i = 0; i < ctx->wimboot.num_wims; i++)
692 if (wim == ctx->wimboot.wims[i].wim)
695 /* Not yet registered */
697 p = REALLOC(ctx->wimboot.wims,
698 (ctx->wimboot.num_wims + 1) * sizeof(ctx->wimboot.wims[0]));
700 return WIMLIB_ERR_NOMEM;
701 ctx->wimboot.wims = p;
703 ctx->wimboot.wims[ctx->wimboot.num_wims].wim = wim;
705 ret = hash_blob_table(wim, ctx->wimboot.wims[ctx->wimboot.num_wims].blob_table_hash);
709 ret = wimboot_alloc_data_source_id(wim->filename,
711 ctx->common.wim->current_image,
713 &ctx->wimboot.wims[ctx->wimboot.num_wims].data_source_id,
714 &ctx->wimboot.wof_running);
718 ctx->wimboot.num_wims++;
722 /* Prepare for doing a "WIMBoot" extraction by registering each source WIM file
723 * with WOF on the target volume. */
725 start_wimboot_extraction(struct list_head *dentry_list, struct win32_apply_ctx *ctx)
728 struct wim_dentry *dentry;
730 if (!xml_get_wimboot(ctx->common.wim->xml_info,
731 ctx->common.wim->current_image))
732 WARNING("The WIM image is not marked as WIMBoot compatible. This usually\n"
733 " means it is not intended to be used to back a Windows operating\n"
734 " system. Proceeding anyway.");
736 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
737 struct blob_descriptor *blob;
739 ret = win32_will_back_from_wim(dentry, &ctx->common);
740 if (ret > 0) /* Error */
742 if (ret < 0) /* Won't externally back */
745 blob = inode_get_blob_for_unnamed_data_stream_resolved(dentry->d_inode);
746 ret = register_wim_with_wof(blob->rdesc->wim, ctx);
751 if (ctx->wimboot.have_wrong_version_wims) {
752 WARNING("At least one of the source WIM files uses a version of the WIM\n"
753 " file format that not supported by Microsoft's wof.sys driver.\n"
754 " Files whose data is contained in one of these WIM files will be\n"
755 " extracted as full files rather than externally backed.");
758 if (ctx->wimboot.have_uncompressed_wims) {
759 WARNING("At least one of the source WIM files is uncompressed. Files whose\n"
760 " data is contained in an uncompressed WIM file will be extracted as\n"
761 " full files rather than externally backed, since uncompressed WIM\n"
762 " files are not supported by Microsoft's wof.sys driver.");
765 if (ctx->wimboot.have_unsupported_compressed_resources) {
766 WARNING("At least one of the source WIM files uses a compression format that\n"
767 " is not supported by Microsoft's wof.sys driver. Files whose data is\n"
768 " contained in a compressed resource in one of these WIM files will be\n"
769 " extracted as full files rather than externally backed. (The\n"
770 " compression formats supported by wof.sys are: XPRESS 4K, XPRESS 8K,\n"
771 " XPRESS 16K, XPRESS 32K, and LZX 32K.)");
774 if (ctx->wimboot.have_huge_resources) {
775 WARNING("Some files exceeded 4.2 GB in size. Such files will be extracted\n"
776 " as full files rather than externally backed, since very large files\n"
777 " are not supported by Microsoft's wof.sys driver.");
784 build_win32_extraction_path(const struct wim_dentry *dentry,
785 struct win32_apply_ctx *ctx);
787 /* Sets WimBoot=1 in the extracted SYSTEM registry hive.
789 * WIMGAPI does this, and it's possible that it's important.
790 * But I don't know exactly what this value means to Windows. */
792 end_wimboot_extraction(struct win32_apply_ctx *ctx)
794 struct wim_dentry *dentry;
795 wchar_t subkeyname[32];
801 dentry = get_dentry(ctx->common.wim, L"\\Windows\\System32\\config\\SYSTEM",
802 WIMLIB_CASE_INSENSITIVE);
804 if (!dentry || !will_extract_dentry(dentry))
807 if (!will_extract_dentry(wim_get_current_root_dentry(ctx->common.wim)))
810 /* Not bothering to use the native routines (e.g. NtLoadKey()) for this.
811 * If this doesn't work, you probably also have many other problems. */
813 build_win32_extraction_path(dentry, ctx);
815 get_random_alnum_chars(subkeyname, 20);
816 subkeyname[20] = L'\0';
818 res = RegLoadKey(HKEY_LOCAL_MACHINE, subkeyname, ctx->pathbuf.Buffer);
822 wcscpy(&subkeyname[20], L"\\Setup");
824 res = RegCreateKeyEx(HKEY_LOCAL_MACHINE, subkeyname, 0, NULL,
825 REG_OPTION_BACKUP_RESTORE, 0, NULL, &key, NULL);
831 res = RegSetValueEx(key, L"WimBoot", 0, REG_DWORD,
832 (const BYTE *)&value, sizeof(DWORD));
836 res = RegFlushKey(key);
839 res2 = RegCloseKey(key);
843 subkeyname[20] = L'\0';
844 RegUnLoadKey(HKEY_LOCAL_MACHINE, subkeyname);
848 win32_warning(res, L"Failed to set \\Setup: dword \"WimBoot\"=1 "
849 "value in registry hive \"%ls\"",
850 ctx->pathbuf.Buffer);
856 /* Returns the number of wide characters needed to represent the path to the
857 * specified @dentry, relative to the target directory, when extracted.
859 * Does not include null terminator (not needed for NtCreateFile). */
861 dentry_extraction_path_length(const struct wim_dentry *dentry)
864 const struct wim_dentry *d;
868 len += d->d_extraction_name_nchars + 1;
870 } while (!dentry_is_root(d) && will_extract_dentry(d));
872 return --len; /* No leading slash */
875 /* Returns the length of the longest string that might need to be appended to
876 * the path to an alias of an inode to open or create a named data stream.
878 * If the inode has no named data streams, this will be 0. Otherwise, this will
879 * be 1 plus the length of the longest-named data stream, since the data stream
880 * name must be separated from the path by the ':' character. */
882 inode_longest_named_data_stream_spec(const struct wim_inode *inode)
885 for (unsigned i = 0; i < inode->i_num_streams; i++) {
886 const struct wim_inode_stream *strm = &inode->i_streams[i];
887 if (!stream_is_named_data_stream(strm))
889 size_t len = utf16le_len_chars(strm->stream_name);
898 /* Find the length, in wide characters, of the longest path needed for
899 * extraction of any file in @dentry_list relative to the target directory.
901 * Accounts for named data streams, but does not include null terminator (not
902 * needed for NtCreateFile). */
904 compute_path_max(struct list_head *dentry_list)
907 const struct wim_dentry *dentry;
909 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
912 len = dentry_extraction_path_length(dentry);
914 /* Account for named data streams */
915 len += inode_longest_named_data_stream_spec(dentry->d_inode);
924 /* Build the path at which to extract the @dentry, relative to the target
927 * The path is saved in ctx->pathbuf. */
929 build_extraction_path(const struct wim_dentry *dentry,
930 struct win32_apply_ctx *ctx)
934 const struct wim_dentry *d;
936 len = dentry_extraction_path_length(dentry);
938 ctx->pathbuf.Length = len * sizeof(wchar_t);
939 p = ctx->pathbuf.Buffer + len;
941 !dentry_is_root(d->d_parent) && will_extract_dentry(d->d_parent);
944 p -= d->d_extraction_name_nchars;
945 if (d->d_extraction_name_nchars)
946 wmemcpy(p, d->d_extraction_name,
947 d->d_extraction_name_nchars);
950 /* No leading slash */
951 p -= d->d_extraction_name_nchars;
952 wmemcpy(p, d->d_extraction_name, d->d_extraction_name_nchars);
955 /* Build the path at which to extract the @dentry, relative to the target
956 * directory, adding the suffix for a named data stream.
958 * The path is saved in ctx->pathbuf. */
960 build_extraction_path_with_ads(const struct wim_dentry *dentry,
961 struct win32_apply_ctx *ctx,
962 const wchar_t *stream_name,
963 size_t stream_name_nchars)
967 build_extraction_path(dentry, ctx);
969 /* Add :NAME for named data stream */
970 p = ctx->pathbuf.Buffer + (ctx->pathbuf.Length / sizeof(wchar_t));
972 wmemcpy(p, stream_name, stream_name_nchars);
973 ctx->pathbuf.Length += (1 + stream_name_nchars) * sizeof(wchar_t);
976 /* Build the Win32 namespace path to the specified @dentry when extracted.
978 * The path is saved in ctx->pathbuf and will be null terminated.
980 * XXX: We could get rid of this if it wasn't needed for the file encryption
981 * APIs, and the registry manipulation in WIMBoot mode. */
983 build_win32_extraction_path(const struct wim_dentry *dentry,
984 struct win32_apply_ctx *ctx)
986 build_extraction_path(dentry, ctx);
988 /* Prepend target_ntpath to our relative path, then change \??\ into \\?\ */
990 memmove(ctx->pathbuf.Buffer +
991 (ctx->target_ntpath.Length / sizeof(wchar_t)) + 1,
992 ctx->pathbuf.Buffer, ctx->pathbuf.Length);
993 memcpy(ctx->pathbuf.Buffer, ctx->target_ntpath.Buffer,
994 ctx->target_ntpath.Length);
995 ctx->pathbuf.Buffer[ctx->target_ntpath.Length / sizeof(wchar_t)] = L'\\';
996 ctx->pathbuf.Length += ctx->target_ntpath.Length + sizeof(wchar_t);
997 ctx->pathbuf.Buffer[ctx->pathbuf.Length / sizeof(wchar_t)] = L'\0';
999 wimlib_assert(ctx->pathbuf.Length >= 4 * sizeof(wchar_t) &&
1000 !wmemcmp(ctx->pathbuf.Buffer, L"\\??\\", 4));
1002 ctx->pathbuf.Buffer[1] = L'\\';
1006 /* Returns a "printable" representation of the last relative NT path that was
1007 * constructed with build_extraction_path() or build_extraction_path_with_ads().
1009 * This will be overwritten by the next call to this function. */
1010 static const wchar_t *
1011 current_path(struct win32_apply_ctx *ctx)
1013 wchar_t *p = ctx->print_buffer;
1015 p = wmempcpy(p, ctx->common.target, ctx->common.target_nchars);
1017 p = wmempcpy(p, ctx->pathbuf.Buffer, ctx->pathbuf.Length / sizeof(wchar_t));
1019 return ctx->print_buffer;
1022 /* Open handle to the target directory if it is not already open. If the target
1023 * directory does not exist, this creates it. */
1025 open_target_directory(struct win32_apply_ctx *ctx)
1032 ctx->attr.Length = sizeof(ctx->attr);
1033 ctx->attr.RootDirectory = NULL;
1034 ctx->attr.ObjectName = &ctx->target_ntpath;
1036 /* Don't use FILE_OPEN_REPARSE_POINT here; we want the extraction to
1037 * happen at the directory "pointed to" by the reparse point. */
1038 status = NtCreateFile(&ctx->h_target,
1044 FILE_SHARE_VALID_FLAGS,
1046 FILE_DIRECTORY_FILE | FILE_OPEN_FOR_BACKUP_INTENT,
1049 if (!NT_SUCCESS(status)) {
1050 winnt_error(status, L"Can't open or create directory \"%ls\"",
1051 ctx->common.target);
1052 return WIMLIB_ERR_OPENDIR;
1054 ctx->attr.RootDirectory = ctx->h_target;
1055 ctx->attr.ObjectName = &ctx->pathbuf;
1060 close_target_directory(struct win32_apply_ctx *ctx)
1062 if (ctx->h_target) {
1063 NtClose(ctx->h_target);
1064 ctx->h_target = NULL;
1065 ctx->attr.RootDirectory = NULL;
1070 * Ensures the target directory exists and opens a handle to it, in preparation
1071 * of using paths relative to it.
1074 prepare_target(struct list_head *dentry_list, struct win32_apply_ctx *ctx)
1079 ret = win32_path_to_nt_path(ctx->common.target, &ctx->target_ntpath);
1083 ret = open_target_directory(ctx);
1087 path_max = compute_path_max(dentry_list);
1088 /* Add some extra for building Win32 paths for the file encryption APIs,
1089 * and ensure we have at least enough to potentially use an 8.3 name for
1090 * the last component. */
1091 path_max += max(2 + (ctx->target_ntpath.Length / sizeof(wchar_t)),
1094 ctx->pathbuf.MaximumLength = path_max * sizeof(wchar_t);
1095 if (ctx->pathbuf.MaximumLength != path_max * sizeof(wchar_t)) {
1096 /* Paths are too long for a UNICODE_STRING! */
1097 ERROR("Some paths are too long to extract (> 32768 characters)!");
1098 return WIMLIB_ERR_UNSUPPORTED;
1101 ctx->pathbuf.Buffer = MALLOC(ctx->pathbuf.MaximumLength);
1102 if (!ctx->pathbuf.Buffer)
1103 return WIMLIB_ERR_NOMEM;
1105 ctx->print_buffer = MALLOC((ctx->common.target_nchars + 1 + path_max + 1) *
1107 if (!ctx->print_buffer)
1108 return WIMLIB_ERR_NOMEM;
1113 /* When creating an inode that will have a short (DOS) name, we create it using
1114 * the long name associated with the short name. This ensures that the short
1115 * name gets associated with the correct long name. */
1116 static struct wim_dentry *
1117 first_extraction_alias(const struct wim_inode *inode)
1119 struct wim_dentry *dentry;
1121 inode_for_each_extraction_alias(dentry, inode)
1122 if (dentry_has_short_name(dentry))
1124 return inode_first_extraction_dentry(inode);
1128 * Set or clear FILE_ATTRIBUTE_COMPRESSED if the inherited value is different
1129 * from the desired value.
1131 * Note that you can NOT override the inherited value of
1132 * FILE_ATTRIBUTE_COMPRESSED directly with NtCreateFile().
1135 adjust_compression_attribute(HANDLE h, const struct wim_dentry *dentry,
1136 struct win32_apply_ctx *ctx)
1138 const bool compressed = (dentry->d_inode->i_attributes &
1139 FILE_ATTRIBUTE_COMPRESSED);
1140 FILE_BASIC_INFORMATION info;
1141 USHORT compression_state;
1144 if (ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES)
1147 if (!ctx->common.supported_features.compressed_files)
1151 /* Get current attributes */
1152 status = NtQueryInformationFile(h, &ctx->iosb, &info, sizeof(info),
1153 FileBasicInformation);
1154 if (NT_SUCCESS(status) &&
1155 compressed == !!(info.FileAttributes & FILE_ATTRIBUTE_COMPRESSED))
1157 /* Nothing needs to be done. */
1161 /* Set the new compression state */
1164 compression_state = COMPRESSION_FORMAT_DEFAULT;
1166 compression_state = COMPRESSION_FORMAT_NONE;
1168 status = winnt_fsctl(h, FSCTL_SET_COMPRESSION,
1169 &compression_state, sizeof(USHORT), NULL, 0, NULL);
1170 if (NT_SUCCESS(status))
1173 winnt_error(status, L"Can't %s compression attribute on \"%ls\"",
1174 (compressed ? "set" : "clear"), current_path(ctx));
1175 return WIMLIB_ERR_SET_ATTRIBUTES;
1179 need_sparse_flag(const struct wim_inode *inode,
1180 const struct win32_apply_ctx *ctx)
1182 return (inode->i_attributes & FILE_ATTRIBUTE_SPARSE_FILE) &&
1183 ctx->common.supported_features.sparse_files;
1187 set_sparse_flag(HANDLE h, struct win32_apply_ctx *ctx)
1191 status = winnt_fsctl(h, FSCTL_SET_SPARSE, NULL, 0, NULL, 0, NULL);
1192 if (NT_SUCCESS(status))
1195 winnt_error(status, L"Can't set sparse flag on \"%ls\"",
1197 return WIMLIB_ERR_SET_ATTRIBUTES;
1200 /* Try to enable short name support on the target volume. If successful, return
1201 * true. If unsuccessful, issue a warning and return false. */
1203 try_to_enable_short_names(const wchar_t *volume)
1206 FILE_FS_PERSISTENT_VOLUME_INFORMATION info;
1208 DWORD bytesReturned;
1210 h = CreateFile(volume, GENERIC_WRITE,
1211 FILE_SHARE_VALID_FLAGS, NULL, OPEN_EXISTING,
1212 FILE_FLAG_BACKUP_SEMANTICS, NULL);
1213 if (h == INVALID_HANDLE_VALUE)
1216 info.VolumeFlags = 0;
1217 info.FlagMask = PERSISTENT_VOLUME_STATE_SHORT_NAME_CREATION_DISABLED;
1221 bret = DeviceIoControl(h, FSCTL_SET_PERSISTENT_VOLUME_STATE,
1222 &info, sizeof(info), NULL, 0,
1223 &bytesReturned, NULL);
1232 win32_warning(GetLastError(),
1233 L"Failed to enable short name support on %ls",
1239 remove_conflicting_short_name(const struct wim_dentry *dentry, struct win32_apply_ctx *ctx)
1245 size_t bufsize = offsetof(FILE_NAME_INFORMATION, FileName) +
1246 (13 * sizeof(wchar_t));
1247 u8 buf[bufsize] __attribute__((aligned(8)));
1248 bool retried = false;
1249 FILE_NAME_INFORMATION *info = (FILE_NAME_INFORMATION *)buf;
1251 memset(buf, 0, bufsize);
1253 /* Build the path with the short name. */
1254 name = &ctx->pathbuf.Buffer[ctx->pathbuf.Length / sizeof(wchar_t)];
1255 while (name != ctx->pathbuf.Buffer && *(name - 1) != L'\\')
1257 end = mempcpy(name, dentry->d_short_name, dentry->d_short_name_nbytes);
1258 ctx->pathbuf.Length = ((u8 *)end - (u8 *)ctx->pathbuf.Buffer);
1260 /* Open the conflicting file (by short name). */
1261 status = NtOpenFile(&h, GENERIC_WRITE | DELETE,
1262 &ctx->attr, &ctx->iosb,
1263 FILE_SHARE_VALID_FLAGS,
1264 FILE_OPEN_REPARSE_POINT | FILE_OPEN_FOR_BACKUP_INTENT);
1265 if (!NT_SUCCESS(status)) {
1266 winnt_warning(status, L"Can't open \"%ls\"", current_path(ctx));
1271 WARNING("Overriding conflicting short name; path=\"%ls\"",
1275 /* Try to remove the short name on the conflicting file. */
1278 status = NtSetInformationFile(h, &ctx->iosb, info, bufsize,
1279 FileShortNameInformation);
1281 if (status == STATUS_INVALID_PARAMETER && !retried) {
1282 /* Microsoft forgot to make it possible to remove short names
1283 * until Windows 7. Oops. Use a random short name instead. */
1284 get_random_alnum_chars(info->FileName, 8);
1285 wcscpy(&info->FileName[8], L".WLB");
1286 info->FileNameLength = 12 * sizeof(wchar_t);
1292 build_extraction_path(dentry, ctx);
1296 /* Set the short name on the open file @h which has been created at the location
1297 * indicated by @dentry.
1299 * Note that this may add, change, or remove the short name.
1301 * @h must be opened with DELETE access.
1303 * Returns 0 or WIMLIB_ERR_SET_SHORT_NAME. The latter only happens in
1304 * STRICT_SHORT_NAMES mode.
1307 set_short_name(HANDLE h, const struct wim_dentry *dentry,
1308 struct win32_apply_ctx *ctx)
1311 if (!ctx->common.supported_features.short_names)
1315 * Note: The size of the FILE_NAME_INFORMATION buffer must be such that
1316 * FileName contains at least 2 wide characters (4 bytes). Otherwise,
1317 * NtSetInformationFile() will return STATUS_INFO_LENGTH_MISMATCH. This
1318 * is despite the fact that FileNameLength can validly be 0 or 2 bytes,
1319 * with the former case being removing the existing short name if
1320 * present, rather than setting one.
1322 * The null terminator is seemingly optional, but to be safe we include
1323 * space for it and zero all unused space.
1326 size_t bufsize = offsetof(FILE_NAME_INFORMATION, FileName) +
1327 max(dentry->d_short_name_nbytes, sizeof(wchar_t)) +
1329 u8 buf[bufsize] __attribute__((aligned(8)));
1330 FILE_NAME_INFORMATION *info = (FILE_NAME_INFORMATION *)buf;
1332 bool tried_to_remove_existing = false;
1334 memset(buf, 0, bufsize);
1336 info->FileNameLength = dentry->d_short_name_nbytes;
1337 memcpy(info->FileName, dentry->d_short_name, dentry->d_short_name_nbytes);
1340 status = NtSetInformationFile(h, &ctx->iosb, info, bufsize,
1341 FileShortNameInformation);
1342 if (NT_SUCCESS(status))
1345 if (status == STATUS_SHORT_NAMES_NOT_ENABLED_ON_VOLUME) {
1346 if (dentry->d_short_name_nbytes == 0)
1348 if (!ctx->tried_to_enable_short_names) {
1352 ctx->tried_to_enable_short_names = true;
1354 ret = win32_get_drive_path(ctx->common.target,
1358 if (try_to_enable_short_names(volume))
1364 * Short names can conflict in several cases:
1366 * - a file being extracted has a short name conflicting with an
1369 * - a file being extracted has a short name conflicting with another
1370 * file being extracted (possible, but shouldn't happen)
1372 * - a file being extracted has a short name that conflicts with the
1373 * automatically generated short name of a file we previously
1374 * extracted, but failed to set the short name for. Sounds unlikely,
1375 * but this actually does happen fairly often on versions of Windows
1376 * prior to Windows 7 because they do not support removing short names
1379 if (unlikely(status == STATUS_OBJECT_NAME_COLLISION) &&
1380 dentry->d_short_name_nbytes && !tried_to_remove_existing)
1382 tried_to_remove_existing = true;
1383 status = remove_conflicting_short_name(dentry, ctx);
1384 if (NT_SUCCESS(status))
1388 /* By default, failure to set short names is not an error (since short
1389 * names aren't too important anymore...). */
1390 if (!(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_SHORT_NAMES)) {
1391 if (dentry->d_short_name_nbytes)
1392 ctx->num_set_short_name_failures++;
1394 ctx->num_remove_short_name_failures++;
1398 winnt_error(status, L"Can't set short name on \"%ls\"", current_path(ctx));
1399 return WIMLIB_ERR_SET_SHORT_NAME;
1403 * A wrapper around NtCreateFile() to make it slightly more usable...
1404 * This uses the path currently constructed in ctx->pathbuf.
1406 * Also, we always specify SYNCHRONIZE access, FILE_OPEN_FOR_BACKUP_INTENT, and
1407 * FILE_OPEN_REPARSE_POINT.
1410 do_create_file(PHANDLE FileHandle,
1411 ACCESS_MASK DesiredAccess,
1412 PLARGE_INTEGER AllocationSize,
1413 ULONG FileAttributes,
1414 ULONG CreateDisposition,
1415 ULONG CreateOptions,
1416 struct win32_apply_ctx *ctx)
1418 return NtCreateFile(FileHandle,
1419 DesiredAccess | SYNCHRONIZE,
1424 FILE_SHARE_VALID_FLAGS,
1427 FILE_OPEN_FOR_BACKUP_INTENT |
1428 FILE_OPEN_REPARSE_POINT,
1433 /* Like do_create_file(), but builds the extraction path of the @dentry first.
1436 create_file(PHANDLE FileHandle,
1437 ACCESS_MASK DesiredAccess,
1438 PLARGE_INTEGER AllocationSize,
1439 ULONG FileAttributes,
1440 ULONG CreateDisposition,
1441 ULONG CreateOptions,
1442 const struct wim_dentry *dentry,
1443 struct win32_apply_ctx *ctx)
1445 build_extraction_path(dentry, ctx);
1446 return do_create_file(FileHandle,
1456 delete_file_or_stream(struct win32_apply_ctx *ctx)
1460 ULONG perms = DELETE;
1461 ULONG flags = FILE_NON_DIRECTORY_FILE | FILE_DELETE_ON_CLOSE;
1463 /* First try opening the file with FILE_DELETE_ON_CLOSE. In most cases,
1464 * all we have to do is that plus close the file handle. */
1466 status = do_create_file(&h, perms, NULL, 0, FILE_OPEN, flags, ctx);
1468 if (unlikely(status == STATUS_CANNOT_DELETE)) {
1469 /* This error occurs for files with FILE_ATTRIBUTE_READONLY set.
1470 * Try an alternate approach: first open the file without
1471 * FILE_DELETE_ON_CLOSE, then reset the file attributes, then
1472 * set the "delete" disposition on the handle. */
1473 if (flags & FILE_DELETE_ON_CLOSE) {
1474 flags &= ~FILE_DELETE_ON_CLOSE;
1475 perms |= FILE_WRITE_ATTRIBUTES;
1480 if (unlikely(!NT_SUCCESS(status))) {
1481 winnt_error(status, L"Can't open \"%ls\" for deletion "
1482 "(perms=%x, flags=%x)",
1483 current_path(ctx), (u32)perms, (u32)flags);
1484 return WIMLIB_ERR_OPEN;
1487 if (unlikely(!(flags & FILE_DELETE_ON_CLOSE))) {
1489 FILE_BASIC_INFORMATION basic_info =
1490 { .FileAttributes = FILE_ATTRIBUTE_NORMAL };
1491 status = NtSetInformationFile(h, &ctx->iosb, &basic_info,
1493 FileBasicInformation);
1495 if (!NT_SUCCESS(status)) {
1496 winnt_error(status, L"Can't reset attributes of \"%ls\" "
1497 "to prepare for deletion", current_path(ctx));
1499 return WIMLIB_ERR_SET_ATTRIBUTES;
1502 FILE_DISPOSITION_INFORMATION disp_info =
1503 { .DoDeleteFile = TRUE };
1504 status = NtSetInformationFile(h, &ctx->iosb, &disp_info,
1506 FileDispositionInformation);
1507 if (!NT_SUCCESS(status)) {
1508 winnt_error(status, L"Can't set delete-on-close "
1509 "disposition on \"%ls\"", current_path(ctx));
1511 return WIMLIB_ERR_SET_ATTRIBUTES;
1515 status = NtClose(h);
1516 if (unlikely(!NT_SUCCESS(status))) {
1517 winnt_error(status, L"Error closing \"%ls\" after setting "
1518 "delete-on-close disposition", current_path(ctx));
1519 return WIMLIB_ERR_OPEN;
1526 * Create a nondirectory file or named data stream at the current path,
1527 * superseding any that already exists at that path. If successful, return an
1528 * open handle to the file or named data stream with the requested permissions.
1531 supersede_file_or_stream(struct win32_apply_ctx *ctx, DWORD perms,
1535 bool retried = false;
1537 /* FILE_ATTRIBUTE_SYSTEM is needed to ensure that
1538 * FILE_ATTRIBUTE_ENCRYPTED doesn't get set before we want it to be. */
1540 status = do_create_file(h_ret,
1543 FILE_ATTRIBUTE_SYSTEM,
1545 FILE_NON_DIRECTORY_FILE,
1547 if (likely(NT_SUCCESS(status)))
1550 /* STATUS_OBJECT_NAME_COLLISION means that the file or stream already
1551 * exists. Delete the existing file or stream, then try again.
1553 * Note: we don't use FILE_OVERWRITE_IF or FILE_SUPERSEDE because of
1554 * problems with certain file attributes, especially
1555 * FILE_ATTRIBUTE_ENCRYPTED. FILE_SUPERSEDE is also broken in the
1556 * Windows PE ramdisk. */
1557 if (status == STATUS_OBJECT_NAME_COLLISION && !retried) {
1558 int ret = delete_file_or_stream(ctx);
1564 winnt_error(status, L"Can't create \"%ls\"", current_path(ctx));
1565 return WIMLIB_ERR_OPEN;
1568 /* Set the reparse point @rpbuf of length @rpbuflen on the extracted file
1569 * corresponding to the WIM dentry @dentry. */
1571 do_set_reparse_point(const struct wim_dentry *dentry,
1572 const struct reparse_buffer_disk *rpbuf, u16 rpbuflen,
1573 struct win32_apply_ctx *ctx)
1578 status = create_file(&h, GENERIC_WRITE, NULL,
1579 0, FILE_OPEN, 0, dentry, ctx);
1580 if (!NT_SUCCESS(status))
1583 status = winnt_fsctl(h, FSCTL_SET_REPARSE_POINT,
1584 rpbuf, rpbuflen, NULL, 0, NULL);
1587 if (NT_SUCCESS(status))
1590 /* On Windows, by default only the Administrator can create symbolic
1591 * links for some reason. By default we just issue a warning if this
1592 * appears to be the problem. Use WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS
1593 * to get a hard error. */
1594 if (!(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS)
1595 && (status == STATUS_PRIVILEGE_NOT_HELD ||
1596 status == STATUS_ACCESS_DENIED)
1597 && (dentry->d_inode->i_reparse_tag == WIM_IO_REPARSE_TAG_SYMLINK ||
1598 dentry->d_inode->i_reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT))
1600 WARNING("Can't create symbolic link \"%ls\"! \n"
1601 " (Need Administrator rights, or at least "
1603 " SeCreateSymbolicLink privilege.)",
1609 winnt_error(status, L"Can't set reparse data on \"%ls\"",
1611 return WIMLIB_ERR_SET_REPARSE_DATA;
1615 * Create empty named data streams and potentially a reparse point for the
1616 * specified file, if any.
1618 * Since these won't have blob descriptors, they won't show up in the call to
1619 * extract_blob_list(). Hence the need for the special case.
1622 create_empty_streams(const struct wim_dentry *dentry,
1623 struct win32_apply_ctx *ctx)
1625 const struct wim_inode *inode = dentry->d_inode;
1628 for (unsigned i = 0; i < inode->i_num_streams; i++) {
1629 const struct wim_inode_stream *strm = &inode->i_streams[i];
1631 if (stream_blob_resolved(strm) != NULL)
1634 if (strm->stream_type == STREAM_TYPE_REPARSE_POINT &&
1635 ctx->common.supported_features.reparse_points)
1637 u8 buf[REPARSE_DATA_OFFSET] __attribute__((aligned(8)));
1638 struct reparse_buffer_disk *rpbuf =
1639 (struct reparse_buffer_disk *)buf;
1640 complete_reparse_point(rpbuf, inode, 0);
1641 ret = do_set_reparse_point(dentry, rpbuf,
1642 REPARSE_DATA_OFFSET, ctx);
1645 } else if (stream_is_named_data_stream(strm) &&
1646 ctx->common.supported_features.named_data_streams)
1650 build_extraction_path_with_ads(dentry, ctx,
1652 utf16le_len_chars(strm->stream_name));
1654 * Note: do not request any permissions on the handle.
1655 * Otherwise, we may encounter a Windows bug where the
1656 * parent directory DACL denies read access to the new
1657 * named data stream, even when using backup semantics!
1659 ret = supersede_file_or_stream(ctx, 0, &h);
1661 build_extraction_path(dentry, ctx);
1673 * Creates the directory named by @dentry, or uses an existing directory at that
1674 * location. If necessary, sets the short name and/or fixes compression and
1675 * encryption attributes.
1677 * Returns 0, WIMLIB_ERR_MKDIR, or WIMLIB_ERR_SET_SHORT_NAME.
1680 create_directory(const struct wim_dentry *dentry, struct win32_apply_ctx *ctx)
1687 /* DELETE is needed for set_short_name(); GENERIC_READ and GENERIC_WRITE
1688 * are needed for adjust_compression_attribute(). */
1689 perms = GENERIC_READ | GENERIC_WRITE;
1690 if (!dentry_is_root(dentry))
1693 /* FILE_ATTRIBUTE_SYSTEM is needed to ensure that
1694 * FILE_ATTRIBUTE_ENCRYPTED doesn't get set before we want it to be. */
1695 status = create_file(&h, perms, NULL, FILE_ATTRIBUTE_SYSTEM,
1696 FILE_OPEN_IF, FILE_DIRECTORY_FILE, dentry, ctx);
1697 if (unlikely(!NT_SUCCESS(status))) {
1698 const wchar_t *path = current_path(ctx);
1699 winnt_error(status, L"Can't create directory \"%ls\"", path);
1701 /* Check for known issue with WindowsApps directory. */
1702 if (status == STATUS_ACCESS_DENIED &&
1703 (wcsstr(path, L"\\WindowsApps\\") ||
1704 wcsstr(path, L"\\InfusedApps\\"))) {
1706 "You seem to be trying to extract files to the WindowsApps directory.\n"
1707 " Windows 8.1 and later use new file permissions in this directory that\n"
1708 " cannot be overridden, even by backup/restore programs. To extract your\n"
1709 " files anyway, you need to choose a different target directory, delete\n"
1710 " the WindowsApps directory entirely, reformat the volume, do the\n"
1711 " extraction from a non-broken operating system such as Windows 7 or\n"
1712 " Linux, or wait for Microsoft to fix the design flaw in their operating\n"
1713 " system. This is *not* a bug in wimlib. See this thread for more\n"
1714 " information: https://wimlib.net/forums/viewtopic.php?f=1&t=261");
1716 return WIMLIB_ERR_MKDIR;
1719 if (ctx->iosb.Information == FILE_OPENED) {
1720 /* If we opened an existing directory, try to clear its file
1721 * attributes. As far as I know, this only actually makes a
1722 * difference in the case where a FILE_ATTRIBUTE_READONLY
1723 * directory has a named data stream which needs to be
1724 * extracted. You cannot create a named data stream of such a
1725 * directory, even though this contradicts Microsoft's
1726 * documentation for FILE_ATTRIBUTE_READONLY which states it is
1727 * not honored for directories! */
1728 if (!(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES)) {
1729 FILE_BASIC_INFORMATION basic_info =
1730 { .FileAttributes = FILE_ATTRIBUTE_NORMAL };
1731 NtSetInformationFile(h, &ctx->iosb, &basic_info,
1733 FileBasicInformation);
1737 if (!dentry_is_root(dentry)) {
1738 ret = set_short_name(h, dentry, ctx);
1743 ret = adjust_compression_attribute(h, dentry, ctx);
1750 * Create all the directories being extracted, other than the target directory
1753 * Note: we don't honor directory hard links. However, we don't allow them to
1754 * exist in WIM images anyway (see inode_fixup.c).
1757 create_directories(struct list_head *dentry_list,
1758 struct win32_apply_ctx *ctx)
1760 const struct wim_dentry *dentry;
1763 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
1765 if (!(dentry->d_inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY))
1768 /* Note: Here we include files with
1769 * FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_REPARSE_POINT, but we
1770 * wait until later to actually set the reparse data. */
1772 ret = create_directory(dentry, ctx);
1775 ret = create_empty_streams(dentry, ctx);
1777 ret = check_apply_error(dentry, ctx, ret);
1781 ret = report_file_created(&ctx->common);
1789 * Creates the nondirectory file named by @dentry.
1791 * On success, returns an open handle to the file in @h_ret, with GENERIC_READ,
1792 * GENERIC_WRITE, and DELETE access. Also, the path to the file will be saved
1793 * in ctx->pathbuf. On failure, returns an error code.
1796 create_nondirectory_inode(HANDLE *h_ret, const struct wim_dentry *dentry,
1797 struct win32_apply_ctx *ctx)
1802 build_extraction_path(dentry, ctx);
1804 ret = supersede_file_or_stream(ctx,
1805 GENERIC_READ | GENERIC_WRITE | DELETE,
1810 ret = adjust_compression_attribute(h, dentry, ctx);
1814 if (need_sparse_flag(dentry->d_inode, ctx)) {
1815 ret = set_sparse_flag(h, ctx);
1820 ret = create_empty_streams(dentry, ctx);
1833 /* Creates a hard link at the location named by @dentry to the file represented
1834 * by the open handle @h. Or, if the target volume does not support hard links,
1835 * create a separate file instead. */
1837 create_link(HANDLE h, const struct wim_dentry *dentry,
1838 struct win32_apply_ctx *ctx)
1840 if (ctx->common.supported_features.hard_links) {
1842 build_extraction_path(dentry, ctx);
1844 size_t bufsize = offsetof(FILE_LINK_INFORMATION, FileName) +
1845 ctx->pathbuf.Length + sizeof(wchar_t);
1846 u8 buf[bufsize] __attribute__((aligned(8)));
1847 FILE_LINK_INFORMATION *info = (FILE_LINK_INFORMATION *)buf;
1850 info->ReplaceIfExists = TRUE;
1851 info->RootDirectory = ctx->attr.RootDirectory;
1852 info->FileNameLength = ctx->pathbuf.Length;
1853 memcpy(info->FileName, ctx->pathbuf.Buffer, ctx->pathbuf.Length);
1854 info->FileName[info->FileNameLength / 2] = L'\0';
1856 * Note: the null terminator isn't actually necessary, but if
1857 * you don't add the extra character, you get
1858 * STATUS_INFO_LENGTH_MISMATCH when FileNameLength is 2.
1862 * When fuzzing with wlfuzz.exe, creating a hard link sometimes
1863 * fails with STATUS_ACCESS_DENIED. However, it eventually
1864 * succeeds when re-attempted...
1868 status = NtSetInformationFile(h, &ctx->iosb, info,
1870 FileLinkInformation);
1871 if (NT_SUCCESS(status))
1874 winnt_error(status, L"Failed to create link \"%ls\"",
1876 return WIMLIB_ERR_LINK;
1881 ret = create_nondirectory_inode(&h2, dentry, ctx);
1890 /* Given an inode (represented by the open handle @h) for which one link has
1891 * been created (named by @first_dentry), create the other links.
1893 * Or, if the target volume does not support hard links, create separate files.
1895 * Note: This uses ctx->pathbuf and does not reset it.
1898 create_links(HANDLE h, const struct wim_dentry *first_dentry,
1899 struct win32_apply_ctx *ctx)
1901 const struct wim_inode *inode = first_dentry->d_inode;
1902 const struct wim_dentry *dentry;
1905 inode_for_each_extraction_alias(dentry, inode) {
1906 if (dentry != first_dentry) {
1907 ret = create_link(h, dentry, ctx);
1915 /* Create a nondirectory file, including all links. */
1917 create_nondirectory(struct wim_inode *inode, struct win32_apply_ctx *ctx)
1919 struct wim_dentry *first_dentry;
1923 first_dentry = first_extraction_alias(inode);
1925 /* Create first link. */
1926 ret = create_nondirectory_inode(&h, first_dentry, ctx);
1930 /* Set short name. */
1931 ret = set_short_name(h, first_dentry, ctx);
1933 /* Create additional links, OR if hard links are not supported just
1934 * create more files. */
1936 ret = create_links(h, first_dentry, ctx);
1938 /* "WIMBoot" extraction: set external backing by the WIM file if needed. */
1939 if (!ret && unlikely(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_WIMBOOT))
1940 ret = set_backed_from_wim(h, inode, ctx);
1946 /* Create all the nondirectory files being extracted, including all aliases
1949 create_nondirectories(struct list_head *dentry_list, struct win32_apply_ctx *ctx)
1951 struct wim_dentry *dentry;
1952 struct wim_inode *inode;
1955 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
1956 inode = dentry->d_inode;
1957 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)
1959 /* Call create_nondirectory() only once per inode */
1960 if (dentry == inode_first_extraction_dentry(inode)) {
1961 ret = create_nondirectory(inode, ctx);
1962 ret = check_apply_error(dentry, ctx, ret);
1966 ret = report_file_created(&ctx->common);
1974 close_handles(struct win32_apply_ctx *ctx)
1976 for (unsigned i = 0; i < ctx->num_open_handles; i++)
1977 NtClose(ctx->open_handles[i]);
1980 /* Prepare to read the next blob, which has size @blob_size, into an in-memory
1983 prepare_data_buffer(struct win32_apply_ctx *ctx, u64 blob_size)
1985 if (blob_size > ctx->data_buffer_size) {
1986 /* Larger buffer needed. */
1988 if ((size_t)blob_size != blob_size)
1990 new_buffer = REALLOC(ctx->data_buffer, blob_size);
1993 ctx->data_buffer = new_buffer;
1994 ctx->data_buffer_size = blob_size;
1996 /* On the first call this changes data_buffer_ptr from NULL, which tells
1997 * extract_chunk() that the data buffer needs to be filled while reading
1998 * the stream data. */
1999 ctx->data_buffer_ptr = ctx->data_buffer;
2004 begin_extract_blob_instance(const struct blob_descriptor *blob,
2005 struct wim_dentry *dentry,
2006 const struct wim_inode_stream *strm,
2007 struct win32_apply_ctx *ctx)
2012 if (unlikely(strm->stream_type == STREAM_TYPE_REPARSE_POINT)) {
2013 /* We can't write the reparse point stream directly; we must set
2014 * it with FSCTL_SET_REPARSE_POINT, which requires that all the
2015 * data be available. So, stage the data in a buffer. */
2016 if (!prepare_data_buffer(ctx, blob->size))
2017 return WIMLIB_ERR_NOMEM;
2018 list_add_tail(&dentry->d_tmp_list, &ctx->reparse_dentries);
2022 if (unlikely(strm->stream_type == STREAM_TYPE_EFSRPC_RAW_DATA)) {
2023 /* We can't write encrypted files directly; we must use
2024 * WriteEncryptedFileRaw(), which requires providing the data
2025 * through a callback function. This can't easily be combined
2026 * with our own callback-based approach.
2028 * The current workaround is to simply read the blob into memory
2029 * and write the encrypted file from that.
2031 * TODO: This isn't sufficient for extremely large encrypted
2032 * files. Perhaps we should create an extra thread to write
2034 if (!prepare_data_buffer(ctx, blob->size))
2035 return WIMLIB_ERR_NOMEM;
2036 list_add_tail(&dentry->d_tmp_list, &ctx->encrypted_dentries);
2040 /* It's a data stream (may be unnamed or named). */
2041 wimlib_assert(strm->stream_type == STREAM_TYPE_DATA);
2043 if (ctx->num_open_handles == MAX_OPEN_FILES) {
2044 /* XXX: Fix this. But because of the checks in
2045 * extract_blob_list(), this can now only happen on a filesystem
2046 * that does not support hard links. */
2047 ERROR("Can't extract data: too many open files!");
2048 return WIMLIB_ERR_UNSUPPORTED;
2052 if (unlikely(stream_is_named(strm))) {
2053 build_extraction_path_with_ads(dentry, ctx,
2055 utf16le_len_chars(strm->stream_name));
2057 build_extraction_path(dentry, ctx);
2061 /* Open a new handle */
2062 status = do_create_file(&h,
2063 FILE_WRITE_DATA | SYNCHRONIZE,
2064 NULL, 0, FILE_OPEN_IF,
2065 FILE_SEQUENTIAL_ONLY |
2066 FILE_SYNCHRONOUS_IO_NONALERT,
2068 if (!NT_SUCCESS(status)) {
2069 winnt_error(status, L"Can't open \"%ls\" for writing",
2071 return WIMLIB_ERR_OPEN;
2074 ctx->is_sparse_stream[ctx->num_open_handles] = false;
2075 if (need_sparse_flag(dentry->d_inode, ctx)) {
2076 /* If the stream is unnamed, then the sparse flag was already
2077 * set when the file was created. But if the stream is named,
2078 * then we need to set the sparse flag here. */
2079 if (unlikely(stream_is_named(strm))) {
2080 int ret = set_sparse_flag(h, ctx);
2086 ctx->is_sparse_stream[ctx->num_open_handles] = true;
2087 ctx->any_sparse_streams = true;
2089 /* Allocate space for the data. */
2090 FILE_ALLOCATION_INFORMATION info =
2091 { .AllocationSize = { .QuadPart = blob->size }};
2092 NtSetInformationFile(h, &ctx->iosb, &info, sizeof(info),
2093 FileAllocationInformation);
2095 ctx->open_handles[ctx->num_open_handles++] = h;
2099 /* Given a Windows NT namespace path, such as \??\e:\Windows\System32, return a
2100 * pointer to the suffix of the path that begins with the device directly, such
2101 * as e:\Windows\System32. */
2102 static const wchar_t *
2103 skip_nt_toplevel_component(const wchar_t *path, size_t path_nchars)
2105 static const wchar_t * const dirs[] = {
2110 const wchar_t * const end = path + path_nchars;
2112 for (size_t i = 0; i < ARRAY_LEN(dirs); i++) {
2113 size_t len = wcslen(dirs[i]);
2114 if (len <= (end - path) && !wmemcmp(path, dirs[i], len)) {
2116 while (path != end && *path == L'\\')
2125 * Given a Windows NT namespace path, such as \??\e:\Windows\System32, return a
2126 * pointer to the suffix of the path that is device-relative but possibly with
2127 * leading slashes, such as \Windows\System32.
2129 * The path has an explicit length and is not necessarily null terminated.
2131 static const wchar_t *
2132 get_device_relative_path(const wchar_t *path, size_t path_nchars)
2134 const wchar_t * const orig_path = path;
2135 const wchar_t * const end = path + path_nchars;
2137 path = skip_nt_toplevel_component(path, path_nchars);
2138 if (path == orig_path)
2141 while (path != end && *path != L'\\')
2148 * Given a reparse point buffer for an inode for which the absolute link target
2149 * was relativized when it was archived, de-relative the link target to be
2150 * consistent with the actual extraction location.
2153 try_rpfix(struct reparse_buffer_disk *rpbuf, u16 *rpbuflen_p,
2154 struct win32_apply_ctx *ctx)
2156 struct link_reparse_point link;
2157 size_t orig_subst_name_nchars;
2158 const wchar_t *relpath;
2159 size_t relpath_nchars;
2160 size_t target_ntpath_nchars;
2161 size_t fixed_subst_name_nchars;
2162 const wchar_t *fixed_print_name;
2163 size_t fixed_print_name_nchars;
2165 /* Do nothing if the reparse data is invalid. */
2166 if (parse_link_reparse_point(rpbuf, *rpbuflen_p, &link))
2169 /* Do nothing if the reparse point is a relative symbolic link. */
2170 if (link_is_relative_symlink(&link))
2173 /* Build the new substitute name from the NT namespace path to the
2174 * target directory, then a path separator, then the "device relative"
2175 * part of the old substitute name. */
2177 orig_subst_name_nchars = link.substitute_name_nbytes / sizeof(wchar_t);
2179 relpath = get_device_relative_path(link.substitute_name,
2180 orig_subst_name_nchars);
2181 relpath_nchars = orig_subst_name_nchars -
2182 (relpath - link.substitute_name);
2184 target_ntpath_nchars = ctx->target_ntpath.Length / sizeof(wchar_t);
2186 /* If the target directory is a filesystem root, such as \??\C:\, then
2187 * it already will have a trailing slash. Don't include this slash if
2188 * we are already adding slashes via 'relpath'. This prevents an extra
2189 * slash from being generated each time the link is extracted. And
2190 * unlike on UNIX, the number of slashes in paths on Windows can be
2191 * significant; Windows won't understand the link target if it contains
2192 * too many slashes. */
2193 if (target_ntpath_nchars > 0 && relpath_nchars > 0 &&
2194 ctx->target_ntpath.Buffer[target_ntpath_nchars - 1] == L'\\')
2195 target_ntpath_nchars--;
2197 /* Also remove extra slashes from the beginning of 'relpath'. Normally
2198 * this isn't needed, but this is here to make the extra slash(es) added
2199 * by wimlib pre-v1.9.1 get removed automatically. */
2200 while (relpath_nchars >= 2 &&
2201 relpath[0] == L'\\' && relpath[1] == L'\\') {
2206 fixed_subst_name_nchars = target_ntpath_nchars + relpath_nchars;
2208 wchar_t fixed_subst_name[fixed_subst_name_nchars];
2210 wmemcpy(fixed_subst_name, ctx->target_ntpath.Buffer, target_ntpath_nchars);
2211 wmemcpy(&fixed_subst_name[target_ntpath_nchars], relpath, relpath_nchars);
2212 /* Doesn't need to be null-terminated. */
2214 /* Print name should be Win32, but not all NT names can even be
2215 * translated to Win32 names. But we can at least delete the top-level
2216 * directory, such as \??\, and this will have the expected result in
2217 * the usual case. */
2218 fixed_print_name = skip_nt_toplevel_component(fixed_subst_name,
2219 fixed_subst_name_nchars);
2220 fixed_print_name_nchars = fixed_subst_name_nchars - (fixed_print_name -
2223 link.substitute_name = fixed_subst_name;
2224 link.substitute_name_nbytes = fixed_subst_name_nchars * sizeof(wchar_t);
2225 link.print_name = (wchar_t *)fixed_print_name;
2226 link.print_name_nbytes = fixed_print_name_nchars * sizeof(wchar_t);
2227 make_link_reparse_point(&link, rpbuf, rpbuflen_p);
2230 /* Sets the reparse point on the specified file. This handles "fixing" the
2231 * targets of absolute symbolic links and junctions if WIMLIB_EXTRACT_FLAG_RPFIX
2234 set_reparse_point(const struct wim_dentry *dentry,
2235 const struct reparse_buffer_disk *rpbuf, u16 rpbuflen,
2236 struct win32_apply_ctx *ctx)
2238 if ((ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX)
2239 && !(dentry->d_inode->i_rp_flags & WIM_RP_FLAG_NOT_FIXED))
2241 memcpy(&ctx->rpfixbuf, rpbuf, rpbuflen);
2242 try_rpfix(&ctx->rpfixbuf, &rpbuflen, ctx);
2243 rpbuf = &ctx->rpfixbuf;
2245 return do_set_reparse_point(dentry, rpbuf, rpbuflen, ctx);
2249 /* Import the next block of raw encrypted data */
2251 import_encrypted_data(PBYTE pbData, PVOID pvCallbackContext, PULONG Length)
2253 struct win32_apply_ctx *ctx = pvCallbackContext;
2256 copy_len = min(ctx->encrypted_size - ctx->encrypted_offset, *Length);
2257 memcpy(pbData, &ctx->data_buffer[ctx->encrypted_offset], copy_len);
2258 ctx->encrypted_offset += copy_len;
2260 return ERROR_SUCCESS;
2264 * Write the raw encrypted data to the already-created file (or directory)
2265 * corresponding to @dentry.
2267 * The raw encrypted data is provided in ctx->data_buffer, and its size is
2268 * ctx->encrypted_size.
2270 * This function may close the target directory, in which case the caller needs
2271 * to re-open it if needed.
2274 extract_encrypted_file(const struct wim_dentry *dentry,
2275 struct win32_apply_ctx *ctx)
2282 /* Temporarily build a Win32 path for OpenEncryptedFileRaw() */
2283 build_win32_extraction_path(dentry, ctx);
2285 flags = CREATE_FOR_IMPORT | OVERWRITE_HIDDEN;
2286 if (dentry->d_inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)
2287 flags |= CREATE_FOR_DIR;
2291 err = OpenEncryptedFileRaw(ctx->pathbuf.Buffer, flags, &rawctx);
2292 if (err == ERROR_SHARING_VIOLATION && !retried) {
2293 /* This can be caused by the handle we have open to the target
2294 * directory. Try closing it temporarily. */
2295 close_target_directory(ctx);
2300 /* Restore the NT namespace path */
2301 build_extraction_path(dentry, ctx);
2303 if (err != ERROR_SUCCESS) {
2304 win32_error(err, L"Can't open \"%ls\" for encrypted import",
2306 return WIMLIB_ERR_OPEN;
2309 ctx->encrypted_offset = 0;
2311 err = WriteEncryptedFileRaw(import_encrypted_data, ctx, rawctx);
2313 CloseEncryptedFileRaw(rawctx);
2315 if (err != ERROR_SUCCESS) {
2316 win32_error(err, L"Can't import encrypted file \"%ls\"",
2318 return WIMLIB_ERR_WRITE;
2324 /* Called when starting to read a blob for extraction */
2326 win32_begin_extract_blob(struct blob_descriptor *blob, void *_ctx)
2328 struct win32_apply_ctx *ctx = _ctx;
2329 const struct blob_extraction_target *targets = blob_extraction_targets(blob);
2332 ctx->num_open_handles = 0;
2333 ctx->data_buffer_ptr = NULL;
2334 ctx->any_sparse_streams = false;
2335 INIT_LIST_HEAD(&ctx->reparse_dentries);
2336 INIT_LIST_HEAD(&ctx->encrypted_dentries);
2338 for (u32 i = 0; i < blob->out_refcnt; i++) {
2339 const struct wim_inode *inode = targets[i].inode;
2340 const struct wim_inode_stream *strm = targets[i].stream;
2341 struct wim_dentry *dentry;
2343 /* A copy of the blob needs to be extracted to @inode. */
2345 if (ctx->common.supported_features.hard_links) {
2346 dentry = inode_first_extraction_dentry(inode);
2347 ret = begin_extract_blob_instance(blob, dentry, strm, ctx);
2348 ret = check_apply_error(dentry, ctx, ret);
2352 /* Hard links not supported. Extract the blob
2353 * separately to each alias of the inode. */
2354 inode_for_each_extraction_alias(dentry, inode) {
2355 ret = begin_extract_blob_instance(blob, dentry, strm, ctx);
2356 ret = check_apply_error(dentry, ctx, ret);
2371 pwrite_to_handle(HANDLE h, const void *data, size_t size, u64 offset)
2373 const void * const end = data + size;
2375 IO_STATUS_BLOCK iosb;
2378 for (p = data; p != end; p += iosb.Information,
2379 offset += iosb.Information)
2381 LARGE_INTEGER offs = { .QuadPart = offset };
2383 status = NtWriteFile(h, NULL, NULL, NULL, &iosb,
2384 (void *)p, min(INT32_MAX, end - p),
2386 if (!NT_SUCCESS(status)) {
2388 L"Error writing data to target volume");
2389 return WIMLIB_ERR_WRITE;
2395 /* Called when the next chunk of a blob has been read for extraction */
2397 win32_extract_chunk(const struct blob_descriptor *blob, u64 offset,
2398 const void *chunk, size_t size, void *_ctx)
2400 struct win32_apply_ctx *ctx = _ctx;
2401 const void * const end = chunk + size;
2409 * For sparse streams, only write nonzero regions. This lets the
2410 * filesystem use holes to represent zero regions.
2412 for (p = chunk; p != end; p += len, offset += len) {
2413 zeroes = maybe_detect_sparse_region(p, end - p, &len,
2414 ctx->any_sparse_streams);
2415 for (i = 0; i < ctx->num_open_handles; i++) {
2416 if (!zeroes || !ctx->is_sparse_stream[i]) {
2417 ret = pwrite_to_handle(ctx->open_handles[i],
2425 /* Copy the data chunk into the buffer (if needed) */
2426 if (ctx->data_buffer_ptr)
2427 ctx->data_buffer_ptr = mempcpy(ctx->data_buffer_ptr,
2433 get_system_compression_format(int extract_flags)
2435 if (extract_flags & WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS4K)
2436 return FILE_PROVIDER_COMPRESSION_XPRESS4K;
2438 if (extract_flags & WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS8K)
2439 return FILE_PROVIDER_COMPRESSION_XPRESS8K;
2441 if (extract_flags & WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS16K)
2442 return FILE_PROVIDER_COMPRESSION_XPRESS16K;
2444 return FILE_PROVIDER_COMPRESSION_LZX;
2448 static const wchar_t *
2449 get_system_compression_format_string(int format)
2452 case FILE_PROVIDER_COMPRESSION_XPRESS4K:
2454 case FILE_PROVIDER_COMPRESSION_XPRESS8K:
2456 case FILE_PROVIDER_COMPRESSION_XPRESS16K:
2457 return L"XPRESS16K";
2464 set_system_compression(HANDLE h, int format)
2468 WOF_EXTERNAL_INFO wof_info;
2469 FILE_PROVIDER_EXTERNAL_INFO_V1 file_info;
2472 .Version = WOF_CURRENT_VERSION,
2473 .Provider = WOF_PROVIDER_FILE,
2476 .Version = FILE_PROVIDER_CURRENT_VERSION,
2477 .Algorithm = format,
2481 /* We intentionally use NtFsControlFile() rather than DeviceIoControl()
2482 * here because the "compressing this object would not save space"
2483 * status code does not map to a valid Win32 error code on older
2484 * versions of Windows (before Windows 10?). This can be a problem if
2485 * the WOFADK driver is being used rather than the regular WOF, since
2486 * WOFADK can be used on older versions of Windows. */
2487 status = winnt_fsctl(h, FSCTL_SET_EXTERNAL_BACKING,
2488 &in, sizeof(in), NULL, 0, NULL);
2490 if (status == 0xC000046F) /* "Compressing this object would not save space." */
2491 return STATUS_SUCCESS;
2496 /* Hard-coded list of files which the Windows bootloader may need to access
2497 * before the WOF driver has been loaded. */
2498 static const wchar_t * const bootloader_pattern_strings[] = {
2501 L"\\Windows\\AppPatch\\drvmain.sdb",
2502 L"\\Windows\\Boot\\DVD\\*",
2503 L"\\Windows\\Boot\\EFI\\*",
2504 L"\\Windows\\bootstat.dat",
2505 L"\\Windows\\Fonts\\vgaoem.fon",
2506 L"\\Windows\\Fonts\\vgasys.fon",
2507 L"\\Windows\\INF\\errata.inf",
2508 L"\\Windows\\System32\\config\\*",
2509 L"\\Windows\\System32\\ntkrnlpa.exe",
2510 L"\\Windows\\System32\\ntoskrnl.exe",
2511 L"\\Windows\\System32\\bootvid.dll",
2512 L"\\Windows\\System32\\ci.dll",
2513 L"\\Windows\\System32\\hal*.dll",
2514 L"\\Windows\\System32\\mcupdate_AuthenticAMD.dll",
2515 L"\\Windows\\System32\\mcupdate_GenuineIntel.dll",
2516 L"\\Windows\\System32\\pshed.dll",
2517 L"\\Windows\\System32\\apisetschema.dll",
2518 L"\\Windows\\System32\\api-ms-win*.dll",
2519 L"\\Windows\\System32\\ext-ms-win*.dll",
2520 L"\\Windows\\System32\\KernelBase.dll",
2521 L"\\Windows\\System32\\drivers\\*.sys",
2522 L"\\Windows\\System32\\*.nls",
2523 L"\\Windows\\System32\\kbd*.dll",
2524 L"\\Windows\\System32\\kd*.dll",
2525 L"\\Windows\\System32\\clfs.sys",
2526 L"\\Windows\\System32\\CodeIntegrity\\driver.stl",
2529 static const struct string_list bootloader_patterns = {
2530 .strings = (wchar_t **)bootloader_pattern_strings,
2531 .num_strings = ARRAY_LEN(bootloader_pattern_strings),
2534 /* Returns true if the specified system compression format is supported by the
2535 * bootloader of the image being applied. */
2537 bootloader_supports_compression_format(struct win32_apply_ctx *ctx, int format)
2539 /* Windows 10 and later support XPRESS4K */
2540 if (format == FILE_PROVIDER_COMPRESSION_XPRESS4K)
2541 return ctx->windows_build_number >= 10240;
2544 * Windows 10 version 1903 and later support the other formats;
2545 * see https://wimlib.net/forums/viewtopic.php?f=1&t=444
2547 return ctx->windows_build_number >= 18362;
2551 set_system_compression_on_inode(struct wim_inode *inode, int format,
2552 struct win32_apply_ctx *ctx)
2554 bool retried = false;
2558 /* If it may be needed for compatibility with the Windows bootloader,
2559 * force this file to XPRESS4K or uncompressed format. */
2560 if (!bootloader_supports_compression_format(ctx, format)) {
2561 /* We need to check the patterns against every name of the
2562 * inode, in case any of them match. */
2563 struct wim_dentry *dentry;
2564 inode_for_each_extraction_alias(dentry, inode) {
2568 if (calculate_dentry_full_path(dentry)) {
2569 ERROR("Unable to compute file path!");
2570 return STATUS_NO_MEMORY;
2573 incompatible = match_pattern_list(dentry->d_full_path,
2574 &bootloader_patterns,
2576 FREE(dentry->d_full_path);
2577 dentry->d_full_path = NULL;
2582 warned = (ctx->num_system_compression_exclusions++ > 0);
2584 if (bootloader_supports_compression_format(ctx,
2585 FILE_PROVIDER_COMPRESSION_XPRESS4K))
2587 /* Force to XPRESS4K */
2589 WARNING("For compatibility with the "
2590 "Windows bootloader, some "
2593 "using the XPRESS4K format "
2594 "instead of the %"TS" format\n"
2596 get_system_compression_format_string(format));
2598 format = FILE_PROVIDER_COMPRESSION_XPRESS4K;
2601 /* Force to uncompressed */
2603 WARNING("For compatibility with the "
2604 "Windows bootloader, some "
2606 " be compressed with"
2607 " system compression "
2608 "(\"compacted\").");
2610 return STATUS_SUCCESS;
2616 /* Open the extracted file. */
2617 status = create_file(&h, GENERIC_READ | GENERIC_WRITE, NULL,
2619 inode_first_extraction_dentry(inode), ctx);
2621 if (!NT_SUCCESS(status))
2624 /* Compress the file. If the attempt fails with "invalid device
2625 * request", then attach wof.sys (or wofadk.sys) and retry. */
2626 status = set_system_compression(h, format);
2627 if (unlikely(status == STATUS_INVALID_DEVICE_REQUEST && !retried)) {
2628 wchar_t drive_path[7];
2629 if (!win32_get_drive_path(ctx->common.target, drive_path) &&
2630 win32_try_to_attach_wof(drive_path + 4)) {
2641 * This function is called when doing a "compact-mode" extraction and we just
2642 * finished extracting a blob to one or more locations. For each location that
2643 * was the unnamed data stream of a file, this function compresses the
2644 * corresponding file using System Compression, if allowed.
2646 * Note: we're doing the compression immediately after extracting the data
2647 * rather than during a separate compression pass. This way should be faster
2648 * since the operating system should still have the file's data cached.
2650 * Note: we're having the operating system do the compression, which is not
2651 * ideal because wimlib could create the compressed data faster and more
2652 * efficiently (the compressed data format is identical to a WIM resource). But
2653 * we seemingly don't have a choice because WOF prevents applications from
2654 * creating its reparse points.
2657 handle_system_compression(struct blob_descriptor *blob, struct win32_apply_ctx *ctx)
2659 const struct blob_extraction_target *targets = blob_extraction_targets(blob);
2661 const int format = get_system_compression_format(ctx->common.extract_flags);
2663 for (u32 i = 0; i < blob->out_refcnt; i++) {
2664 struct wim_inode *inode = targets[i].inode;
2665 struct wim_inode_stream *strm = targets[i].stream;
2668 if (!stream_is_unnamed_data_stream(strm))
2671 if (will_externally_back_inode(inode, ctx, NULL, false) != 0)
2674 status = set_system_compression_on_inode(inode, format, ctx);
2675 if (likely(NT_SUCCESS(status)))
2678 if (status == STATUS_INVALID_DEVICE_REQUEST) {
2680 "The request to compress the extracted files using System Compression\n"
2681 " will not be honored because the operating system or target volume\n"
2682 " does not support it. System Compression is only supported on\n"
2683 " Windows 10 and later, and only on NTFS volumes.");
2684 ctx->common.extract_flags &= ~COMPACT_FLAGS;
2688 ctx->num_system_compression_failures++;
2689 if (ctx->num_system_compression_failures < 10) {
2690 winnt_warning(status, L"\"%ls\": Failed to compress "
2691 "extracted file using System Compression",
2693 } else if (ctx->num_system_compression_failures == 10) {
2694 WARNING("Suppressing further warnings about "
2695 "System Compression failures.");
2700 /* Called when a blob has been fully read for extraction */
2702 win32_end_extract_blob(struct blob_descriptor *blob, int status, void *_ctx)
2704 struct win32_apply_ctx *ctx = _ctx;
2706 const struct wim_dentry *dentry;
2708 /* Extend sparse streams to their final size. */
2709 if (ctx->any_sparse_streams && !status) {
2710 for (unsigned i = 0; i < ctx->num_open_handles; i++) {
2711 FILE_END_OF_FILE_INFORMATION info =
2712 { .EndOfFile = { .QuadPart = blob->size } };
2715 if (!ctx->is_sparse_stream[i])
2718 ntstatus = NtSetInformationFile(ctx->open_handles[i],
2720 &info, sizeof(info),
2721 FileEndOfFileInformation);
2722 if (!NT_SUCCESS(ntstatus)) {
2723 winnt_error(ntstatus, L"Error writing data to "
2724 "target volume (while extending)");
2725 status = WIMLIB_ERR_WRITE;
2736 if (unlikely(ctx->common.extract_flags & COMPACT_FLAGS))
2737 handle_system_compression(blob, ctx);
2739 if (likely(!ctx->data_buffer_ptr))
2742 if (!list_empty(&ctx->reparse_dentries)) {
2743 if (blob->size > REPARSE_DATA_MAX_SIZE) {
2744 dentry = list_first_entry(&ctx->reparse_dentries,
2745 struct wim_dentry, d_tmp_list);
2746 build_extraction_path(dentry, ctx);
2747 ERROR("Reparse data of \"%ls\" has size "
2748 "%"PRIu64" bytes (exceeds %u bytes)",
2749 current_path(ctx), blob->size,
2750 REPARSE_DATA_MAX_SIZE);
2751 ret = WIMLIB_ERR_INVALID_REPARSE_DATA;
2752 return check_apply_error(dentry, ctx, ret);
2755 memcpy(ctx->rpbuf.rpdata, ctx->data_buffer, blob->size);
2757 list_for_each_entry(dentry, &ctx->reparse_dentries, d_tmp_list) {
2759 /* Reparse point header */
2760 complete_reparse_point(&ctx->rpbuf, dentry->d_inode,
2763 ret = set_reparse_point(dentry, &ctx->rpbuf,
2764 REPARSE_DATA_OFFSET + blob->size,
2766 ret = check_apply_error(dentry, ctx, ret);
2772 if (!list_empty(&ctx->encrypted_dentries)) {
2773 ctx->encrypted_size = blob->size;
2774 list_for_each_entry(dentry, &ctx->encrypted_dentries, d_tmp_list) {
2775 ret = extract_encrypted_file(dentry, ctx);
2776 ret = check_apply_error(dentry, ctx, ret);
2779 /* Re-open the target directory if needed. */
2780 ret = open_target_directory(ctx);
2789 /* Attributes that can't be set directly */
2790 #define SPECIAL_ATTRIBUTES \
2791 (FILE_ATTRIBUTE_REPARSE_POINT | \
2792 FILE_ATTRIBUTE_DIRECTORY | \
2793 FILE_ATTRIBUTE_ENCRYPTED | \
2794 FILE_ATTRIBUTE_SPARSE_FILE | \
2795 FILE_ATTRIBUTE_COMPRESSED)
2798 set_object_id(HANDLE h, const struct wim_inode *inode,
2799 struct win32_apply_ctx *ctx)
2801 const void *object_id;
2805 if (!ctx->common.supported_features.object_ids)
2808 object_id = inode_get_object_id(inode, &len);
2809 if (likely(object_id == NULL)) /* No object ID? */
2812 status = winnt_fsctl(h, FSCTL_SET_OBJECT_ID,
2813 object_id, len, NULL, 0, NULL);
2814 if (NT_SUCCESS(status))
2817 /* Object IDs must be unique within the filesystem. A duplicate might
2818 * occur if an image containing object IDs is applied twice to the same
2819 * filesystem. Arguably, the user should be warned in this case; but
2820 * the reality seems to be that nothing important cares about object IDs
2821 * except the Distributed Link Tracking Service... so for now these
2822 * failures are just ignored. */
2823 if (status == STATUS_DUPLICATE_NAME ||
2824 status == STATUS_OBJECT_NAME_COLLISION)
2827 ctx->num_object_id_failures++;
2828 if (ctx->num_object_id_failures < 10) {
2829 winnt_warning(status, L"Can't set object ID on \"%ls\"",
2831 } else if (ctx->num_object_id_failures == 10) {
2832 WARNING("Suppressing further warnings about failure to set "
2838 set_xattrs(HANDLE h, const struct wim_inode *inode, struct win32_apply_ctx *ctx)
2840 const void *entries, *entries_end;
2842 const struct wim_xattr_entry *entry;
2844 u8 _buf[1024] __attribute__((aligned(4)));
2846 FILE_FULL_EA_INFORMATION *ea, *ea_prev;
2850 if (!ctx->common.supported_features.xattrs)
2853 entries = inode_get_xattrs(inode, &len);
2854 if (likely(entries == NULL || len == 0)) /* No extended attributes? */
2856 entries_end = entries + len;
2859 for (entry = entries; (void *)entry < entries_end;
2860 entry = xattr_entry_next(entry)) {
2861 if (!valid_xattr_entry(entry, entries_end - (void *)entry)) {
2862 ERROR("\"%"TS"\": extended attribute is corrupt or unsupported",
2863 inode_any_full_path(inode));
2864 return WIMLIB_ERR_INVALID_XATTR;
2867 bufsize += ALIGN(offsetof(FILE_FULL_EA_INFORMATION, EaName) +
2868 entry->name_len + 1 +
2869 le16_to_cpu(entry->value_len), 4);
2872 if (unlikely(bufsize != (u32)bufsize)) {
2873 ERROR("\"%"TS"\": too many extended attributes to extract!",
2874 inode_any_full_path(inode));
2875 return WIMLIB_ERR_INVALID_XATTR;
2878 if (unlikely(bufsize > sizeof(_buf))) {
2879 buf = MALLOC(bufsize);
2881 return WIMLIB_ERR_NOMEM;
2885 ea = (FILE_FULL_EA_INFORMATION *)buf;
2886 for (entry = entries; (void *)entry < entries_end;
2887 entry = xattr_entry_next(entry)) {
2891 ea_prev->NextEntryOffset = (u8 *)ea - (u8 *)ea_prev;
2892 ea->Flags = entry->flags;
2893 ea->EaNameLength = entry->name_len;
2894 ea->EaValueLength = le16_to_cpu(entry->value_len);
2895 p = mempcpy(ea->EaName, entry->name,
2896 ea->EaNameLength + 1 + ea->EaValueLength);
2897 while ((uintptr_t)p & 3)
2900 ea = (FILE_FULL_EA_INFORMATION *)p;
2902 ea_prev->NextEntryOffset = 0;
2903 wimlib_assert((u8 *)ea - buf == bufsize);
2905 status = NtSetEaFile(h, &ctx->iosb, buf, bufsize);
2906 if (unlikely(!NT_SUCCESS(status))) {
2907 if (status == STATUS_EAS_NOT_SUPPORTED) {
2908 /* This happens with Samba. */
2909 WARNING("Filesystem advertised extended attribute (EA) support, but it doesn't\n"
2910 " work. EAs will not be extracted.");
2911 ctx->common.supported_features.xattrs = 0;
2912 } else if (status == STATUS_INVALID_EA_NAME) {
2913 ctx->num_xattr_failures++;
2914 if (ctx->num_xattr_failures < 5) {
2915 winnt_warning(status,
2916 L"Can't set extended attributes on \"%ls\"",
2918 } else if (ctx->num_xattr_failures == 5) {
2919 WARNING("Suppressing further warnings about "
2920 "failure to set extended attributes.");
2923 winnt_error(status, L"Can't set extended attributes on \"%ls\"",
2925 ret = WIMLIB_ERR_SET_XATTR;
2936 /* Set the security descriptor @desc, of @desc_size bytes, on the file with open
2939 set_security_descriptor(HANDLE h, const void *_desc,
2940 size_t desc_size, struct win32_apply_ctx *ctx)
2942 SECURITY_INFORMATION info;
2944 SECURITY_DESCRIPTOR_RELATIVE *desc;
2947 * Ideally, we would just pass in the security descriptor buffer as-is.
2948 * But it turns out that Windows can mess up the security descriptor
2949 * even when using the low-level NtSetSecurityObject() function:
2951 * - Windows will clear SE_DACL_AUTO_INHERITED if it is set in the
2952 * passed buffer. To actually get Windows to set
2953 * SE_DACL_AUTO_INHERITED, the application must set the non-persistent
2954 * flag SE_DACL_AUTO_INHERIT_REQ. As usual, Microsoft didn't bother
2955 * to properly document either of these flags. It's unclear how
2956 * important SE_DACL_AUTO_INHERITED actually is, but to be safe we use
2957 * the SE_DACL_AUTO_INHERIT_REQ workaround to set it if needed.
2959 * - The above also applies to the equivalent SACL flags,
2960 * SE_SACL_AUTO_INHERITED and SE_SACL_AUTO_INHERIT_REQ.
2962 * - If the application says that it's setting
2963 * DACL_SECURITY_INFORMATION, then Windows sets SE_DACL_PRESENT in the
2964 * resulting security descriptor, even if the security descriptor the
2965 * application provided did not have a DACL. This seems to be
2966 * unavoidable, since omitting DACL_SECURITY_INFORMATION would cause a
2967 * default DACL to remain. Fortunately, this behavior seems harmless,
2968 * since the resulting DACL will still be "null" --- but it will be
2969 * "the other representation of null".
2971 * - The above also applies to SACL_SECURITY_INFORMATION and
2972 * SE_SACL_PRESENT. Again, it's seemingly unavoidable but "harmless"
2973 * that Windows changes the representation of a "null SACL".
2975 if (likely(desc_size <= STACK_MAX)) {
2976 desc = alloca(desc_size);
2978 desc = MALLOC(desc_size);
2980 return STATUS_NO_MEMORY;
2983 memcpy(desc, _desc, desc_size);
2985 if (likely(desc_size >= 4)) {
2987 if (desc->Control & SE_DACL_AUTO_INHERITED)
2988 desc->Control |= SE_DACL_AUTO_INHERIT_REQ;
2990 if (desc->Control & SE_SACL_AUTO_INHERITED)
2991 desc->Control |= SE_SACL_AUTO_INHERIT_REQ;
2995 * More API insanity. We want to set the entire security descriptor
2996 * as-is. But all available APIs require specifying the specific parts
2997 * of the security descriptor being set. Especially annoying is that
2998 * mandatory integrity labels are part of the SACL, but they aren't set
2999 * with SACL_SECURITY_INFORMATION. Instead, applications must also
3000 * specify LABEL_SECURITY_INFORMATION (Windows Vista, Windows 7) or
3001 * BACKUP_SECURITY_INFORMATION (Windows 8). But at least older versions
3002 * of Windows don't error out if you provide these newer flags...
3004 * Also, if the process isn't running as Administrator, then it probably
3005 * doesn't have SE_RESTORE_PRIVILEGE. In this case, it will always get
3006 * the STATUS_PRIVILEGE_NOT_HELD error by trying to set the SACL, even
3007 * if the security descriptor it provided did not have a SACL. By
3008 * default, in this case we try to recover and set as much of the
3009 * security descriptor as possible --- potentially excluding the DACL, and
3010 * even the owner, as well as the SACL.
3013 info = OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
3014 DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION |
3015 LABEL_SECURITY_INFORMATION | BACKUP_SECURITY_INFORMATION;
3019 * It's also worth noting that SetFileSecurity() is unusable because it
3020 * doesn't request "backup semantics" when it opens the file internally.
3021 * NtSetSecurityObject() seems to be the best function to use in backup
3022 * applications. (SetSecurityInfo() should also work, but it's harder
3023 * to use and must call NtSetSecurityObject() internally anyway.
3024 * BackupWrite() is theoretically usable as well, but it's inflexible
3025 * and poorly documented.)
3029 status = NtSetSecurityObject(h, info, desc);
3030 if (NT_SUCCESS(status))
3031 goto out_maybe_free_desc;
3033 /* Failed to set the requested parts of the security descriptor. If the
3034 * error was permissions-related, try to set fewer parts of the security
3035 * descriptor, unless WIMLIB_EXTRACT_FLAG_STRICT_ACLS is enabled. */
3036 if ((status == STATUS_PRIVILEGE_NOT_HELD ||
3037 status == STATUS_ACCESS_DENIED) &&
3038 !(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_ACLS))
3040 if (info & SACL_SECURITY_INFORMATION) {
3041 info &= ~(SACL_SECURITY_INFORMATION |
3042 LABEL_SECURITY_INFORMATION |
3043 BACKUP_SECURITY_INFORMATION);
3044 ctx->partial_security_descriptors++;
3047 if (info & DACL_SECURITY_INFORMATION) {
3048 info &= ~DACL_SECURITY_INFORMATION;
3051 if (info & OWNER_SECURITY_INFORMATION) {
3052 info &= ~OWNER_SECURITY_INFORMATION;
3055 /* Nothing left except GROUP, and if we removed it we
3056 * wouldn't have anything at all. */
3059 /* No part of the security descriptor could be set, or
3060 * WIMLIB_EXTRACT_FLAG_STRICT_ACLS is enabled and the full security
3061 * descriptor could not be set. */
3062 if (!(info & SACL_SECURITY_INFORMATION))
3063 ctx->partial_security_descriptors--;
3064 ctx->no_security_descriptors++;
3066 out_maybe_free_desc:
3067 if (unlikely(desc_size > STACK_MAX))
3072 /* Set metadata on the open file @h from the WIM inode @inode. */
3074 do_apply_metadata_to_file(HANDLE h, const struct wim_inode *inode,
3075 struct win32_apply_ctx *ctx)
3077 FILE_BASIC_INFORMATION info;
3081 /* Set the file's object ID if present and object IDs are supported by
3082 * the filesystem. */
3083 set_object_id(h, inode, ctx);
3085 /* Set the file's extended attributes (EAs) if present and EAs are
3086 * supported by the filesystem. */
3087 ret = set_xattrs(h, inode, ctx);
3091 /* Set the file's security descriptor if present and we're not in
3093 if (inode_has_security_descriptor(inode) &&
3094 !(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_NO_ACLS))
3096 const struct wim_security_data *sd;
3100 sd = wim_get_current_security_data(ctx->common.wim);
3101 desc = sd->descriptors[inode->i_security_id];
3102 desc_size = sd->sizes[inode->i_security_id];
3104 status = set_security_descriptor(h, desc, desc_size, ctx);
3105 if (!NT_SUCCESS(status) &&
3106 (ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_ACLS))
3109 L"Can't set security descriptor on \"%ls\"",
3111 return WIMLIB_ERR_SET_SECURITY;
3115 /* Set attributes and timestamps */
3116 info.CreationTime.QuadPart = inode->i_creation_time;
3117 info.LastAccessTime.QuadPart = inode->i_last_access_time;
3118 info.LastWriteTime.QuadPart = inode->i_last_write_time;
3119 info.ChangeTime.QuadPart = 0;
3120 if (ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES) {
3121 info.FileAttributes = FILE_ATTRIBUTE_NORMAL;
3123 info.FileAttributes = inode->i_attributes & ~SPECIAL_ATTRIBUTES;
3124 if (info.FileAttributes == 0)
3125 info.FileAttributes = FILE_ATTRIBUTE_NORMAL;
3128 status = NtSetInformationFile(h, &ctx->iosb, &info, sizeof(info),
3129 FileBasicInformation);
3130 /* On FAT volumes we get STATUS_INVALID_PARAMETER if we try to set
3131 * attributes on the root directory. (Apparently because FAT doesn't
3132 * actually have a place to store those attributes!) */
3133 if (!NT_SUCCESS(status)
3134 && !(status == STATUS_INVALID_PARAMETER &&
3135 dentry_is_root(inode_first_extraction_dentry(inode))))
3137 winnt_error(status, L"Can't set basic metadata on \"%ls\"",
3139 return WIMLIB_ERR_SET_ATTRIBUTES;
3146 apply_metadata_to_file(const struct wim_dentry *dentry,
3147 struct win32_apply_ctx *ctx)
3149 const struct wim_inode *inode = dentry->d_inode;
3155 perms = FILE_WRITE_ATTRIBUTES | FILE_WRITE_EA | WRITE_DAC |
3156 WRITE_OWNER | ACCESS_SYSTEM_SECURITY;
3158 build_extraction_path(dentry, ctx);
3160 /* Open a handle with as many relevant permissions as possible. */
3161 while (!NT_SUCCESS(status = do_create_file(&h, perms, NULL,
3162 0, FILE_OPEN, 0, ctx)))
3164 if (status == STATUS_PRIVILEGE_NOT_HELD ||
3165 status == STATUS_ACCESS_DENIED)
3167 if (perms & ACCESS_SYSTEM_SECURITY) {
3168 perms &= ~ACCESS_SYSTEM_SECURITY;
3171 if (perms & WRITE_DAC) {
3172 perms &= ~WRITE_DAC;
3175 if (perms & WRITE_OWNER) {
3176 perms &= ~WRITE_OWNER;
3180 winnt_error(status, L"Can't open \"%ls\" to set metadata",
3182 return WIMLIB_ERR_OPEN;
3185 ret = do_apply_metadata_to_file(h, inode, ctx);
3193 apply_metadata(struct list_head *dentry_list, struct win32_apply_ctx *ctx)
3195 const struct wim_dentry *dentry;
3198 /* We go in reverse so that metadata is set on all a directory's
3199 * children before the directory itself. This avoids any potential
3200 * problems with attributes, timestamps, or security descriptors. */
3201 list_for_each_entry_reverse(dentry, dentry_list, d_extraction_list_node)
3203 ret = apply_metadata_to_file(dentry, ctx);
3204 ret = check_apply_error(dentry, ctx, ret);
3207 ret = report_file_metadata_applied(&ctx->common);
3214 /* Issue warnings about problems during the extraction for which warnings were
3215 * not already issued (due to the high number of potential warnings if we issued
3216 * them per-file). */
3218 do_warnings(const struct win32_apply_ctx *ctx)
3220 if (ctx->partial_security_descriptors == 0
3221 && ctx->no_security_descriptors == 0
3222 && ctx->num_set_short_name_failures == 0
3224 && ctx->num_remove_short_name_failures == 0
3229 WARNING("Extraction to \"%ls\" complete, but with one or more warnings:",
3230 ctx->common.target);
3231 if (ctx->num_set_short_name_failures) {
3232 WARNING("- Could not set short names on %lu files or directories",
3233 ctx->num_set_short_name_failures);
3236 if (ctx->num_remove_short_name_failures) {
3237 WARNING("- Could not remove short names on %lu files or directories"
3238 " (This is expected on Vista and earlier)",
3239 ctx->num_remove_short_name_failures);
3242 if (ctx->partial_security_descriptors) {
3243 WARNING("- Could only partially set the security descriptor\n"
3244 " on %lu files or directories.",
3245 ctx->partial_security_descriptors);
3247 if (ctx->no_security_descriptors) {
3248 WARNING("- Could not set security descriptor at all\n"
3249 " on %lu files or directories.",
3250 ctx->no_security_descriptors);
3252 if (ctx->partial_security_descriptors || ctx->no_security_descriptors) {
3253 WARNING("To fully restore all security descriptors, run the program\n"
3254 " with Administrator rights.");
3259 count_dentries(const struct list_head *dentry_list)
3261 const struct list_head *cur;
3264 list_for_each(cur, dentry_list)
3270 /* Extract files from a WIM image to a directory on Windows */
3272 win32_extract(struct list_head *dentry_list, struct apply_ctx *_ctx)
3275 struct win32_apply_ctx *ctx = (struct win32_apply_ctx *)_ctx;
3278 ret = prepare_target(dentry_list, ctx);
3282 if (unlikely(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_WIMBOOT)) {
3283 ret = start_wimboot_extraction(dentry_list, ctx);
3288 ctx->windows_build_number = xml_get_windows_build_number(ctx->common.wim->xml_info,
3289 ctx->common.wim->current_image);
3291 dentry_count = count_dentries(dentry_list);
3293 ret = start_file_structure_phase(&ctx->common, dentry_count);
3297 ret = create_directories(dentry_list, ctx);
3301 ret = create_nondirectories(dentry_list, ctx);
3305 ret = end_file_structure_phase(&ctx->common);
3309 struct read_blob_callbacks cbs = {
3310 .begin_blob = win32_begin_extract_blob,
3311 .continue_blob = win32_extract_chunk,
3312 .end_blob = win32_end_extract_blob,
3315 ret = extract_blob_list(&ctx->common, &cbs);
3319 ret = start_file_metadata_phase(&ctx->common, dentry_count);
3323 ret = apply_metadata(dentry_list, ctx);
3327 ret = end_file_metadata_phase(&ctx->common);
3331 if (unlikely(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_WIMBOOT)) {
3332 ret = end_wimboot_extraction(ctx);
3339 close_target_directory(ctx);
3340 if (ctx->target_ntpath.Buffer)
3341 HeapFree(GetProcessHeap(), 0, ctx->target_ntpath.Buffer);
3342 FREE(ctx->pathbuf.Buffer);
3343 FREE(ctx->print_buffer);
3344 FREE(ctx->wimboot.wims);
3345 if (ctx->prepopulate_pats) {
3346 FREE(ctx->prepopulate_pats->strings);
3347 FREE(ctx->prepopulate_pats);
3349 FREE(ctx->mem_prepopulate_pats);
3350 FREE(ctx->data_buffer);
3354 const struct apply_operations win32_apply_ops = {
3356 .get_supported_features = win32_get_supported_features,
3357 .extract = win32_extract,
3358 .will_back_from_wim = win32_will_back_from_wim,
3359 .context_size = sizeof(struct win32_apply_ctx),