2 * win32_apply.c - Windows-specific code for applying files from a WIM image.
6 * Copyright (C) 2013-2018 Eric Biggers
8 * This file is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU Lesser General Public License as published by the Free
10 * Software Foundation; either version 3 of the License, or (at your option) any
13 * This file is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this file; if not, see http://www.gnu.org/licenses/.
28 #include "wimlib/win32_common.h"
30 #include "wimlib/apply.h"
31 #include "wimlib/assert.h"
32 #include "wimlib/blob_table.h"
33 #include "wimlib/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 /* Is the image being extracted an OS image for Windows 10 or later? */
249 is_image_windows_10_or_later(struct win32_apply_ctx *ctx)
251 /* Note: if no build number is available, this returns false. */
252 return ctx->windows_build_number >= 10240;
255 static const wchar_t *
256 current_path(struct win32_apply_ctx *ctx);
259 build_extraction_path(const struct wim_dentry *dentry,
260 struct win32_apply_ctx *ctx);
263 report_dentry_apply_error(const struct wim_dentry *dentry,
264 struct win32_apply_ctx *ctx, int ret)
266 build_extraction_path(dentry, ctx);
267 return report_apply_error(&ctx->common, ret, current_path(ctx));
271 check_apply_error(const struct wim_dentry *dentry,
272 struct win32_apply_ctx *ctx, int ret)
275 ret = report_dentry_apply_error(dentry, ctx, ret);
280 win32_get_supported_features(const wchar_t *target,
281 struct wim_features *supported_features)
284 bool short_names_supported;
286 /* Query the features of the target volume. */
288 get_vol_flags(target, &vol_flags, &short_names_supported);
290 supported_features->readonly_files = 1;
291 supported_features->hidden_files = 1;
292 supported_features->system_files = 1;
293 supported_features->archive_files = 1;
295 if (vol_flags & FILE_FILE_COMPRESSION)
296 supported_features->compressed_files = 1;
298 if (vol_flags & FILE_SUPPORTS_ENCRYPTION) {
299 supported_features->encrypted_files = 1;
300 supported_features->encrypted_directories = 1;
303 supported_features->not_context_indexed_files = 1;
305 if (vol_flags & FILE_SUPPORTS_SPARSE_FILES)
306 supported_features->sparse_files = 1;
308 if (vol_flags & FILE_NAMED_STREAMS)
309 supported_features->named_data_streams = 1;
311 if (vol_flags & FILE_SUPPORTS_HARD_LINKS)
312 supported_features->hard_links = 1;
314 if (vol_flags & FILE_SUPPORTS_REPARSE_POINTS)
315 supported_features->reparse_points = 1;
317 if (vol_flags & FILE_PERSISTENT_ACLS)
318 supported_features->security_descriptors = 1;
320 if (short_names_supported)
321 supported_features->short_names = 1;
323 if (vol_flags & FILE_SUPPORTS_OBJECT_IDS)
324 supported_features->object_ids = 1;
326 supported_features->timestamps = 1;
328 if (vol_flags & FILE_CASE_SENSITIVE_SEARCH) {
330 * The filesystem supports case-sensitive filenames. But does
331 * the operating system as well? This normally requires the
332 * registry setting ObCaseInsensitive=0. We can test it
333 * indirectly by attempting to open the "\SystemRoot" symbolic
334 * link using a name with the wrong case. If we get
335 * STATUS_OBJECT_NAME_NOT_FOUND instead of STATUS_ACCESS_DENIED,
336 * then case-sensitive names must be enabled.
339 OBJECT_ATTRIBUTES attr;
343 RtlInitUnicodeString(&path, L"\\systemroot");
344 InitializeObjectAttributes(&attr, &path, 0, NULL, NULL);
346 status = NtOpenSymbolicLinkObject(&h, 0, &attr);
347 if (status == STATUS_OBJECT_NAME_NOT_FOUND)
348 supported_features->case_sensitive_filenames = 1;
351 if (vol_flags & FILE_SUPPORTS_EXTENDED_ATTRIBUTES)
352 supported_features->xattrs = 1;
357 #define COMPACT_FLAGS (WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS4K | \
358 WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS8K | \
359 WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS16K | \
360 WIMLIB_EXTRACT_FLAG_COMPACT_LZX)
365 * If not done already, load the patterns from the [PrepopulateList] section of
366 * WimBootCompress.ini in the WIM image being extracted.
368 * Note: WimBootCompress.ini applies to both types of "external backing":
370 * - WIM backing ("WIMBoot" - Windows 8.1 and later)
371 * - File backing ("System Compression" - Windows 10 and later)
374 load_prepopulate_pats(struct win32_apply_ctx *ctx)
376 const wchar_t *path = L"\\Windows\\System32\\WimBootCompress.ini";
377 struct wim_dentry *dentry;
378 const struct blob_descriptor *blob;
381 struct string_list *strings;
383 struct text_file_section sec;
385 if (ctx->tried_to_load_prepopulate_list)
388 ctx->tried_to_load_prepopulate_list = true;
390 dentry = get_dentry(ctx->common.wim, path, WIMLIB_CASE_INSENSITIVE);
392 (dentry->d_inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
393 FILE_ATTRIBUTE_REPARSE_POINT |
394 FILE_ATTRIBUTE_ENCRYPTED)) ||
395 !(blob = inode_get_blob_for_unnamed_data_stream(dentry->d_inode,
396 ctx->common.wim->blob_table)))
398 WARNING("%ls does not exist in the WIM image.\n"
399 " The default configuration will be used instead; it assumes that all\n"
400 " files are valid for external backing regardless of path, equivalent\n"
401 " to an empty [PrepopulateList] section.", path);
402 return WIMLIB_ERR_PATH_DOES_NOT_EXIST;
405 ret = read_blob_into_alloc_buf(blob, &buf);
409 strings = CALLOC(1, sizeof(struct string_list));
412 return WIMLIB_ERR_NOMEM;
415 sec.name = T("PrepopulateList");
416 sec.strings = strings;
418 ret = do_load_text_file(path, buf, blob->size, &mem, &sec, 1,
419 LOAD_TEXT_FILE_REMOVE_QUOTES |
420 LOAD_TEXT_FILE_NO_WARNINGS,
422 STATIC_ASSERT(OS_PREFERRED_PATH_SEPARATOR == WIM_PATH_SEPARATOR);
428 ctx->prepopulate_pats = strings;
429 ctx->mem_prepopulate_pats = mem;
433 /* Returns %true if the specified absolute path to a file in the WIM image can
434 * be subject to external backing when extracted. Otherwise returns %false. */
436 can_externally_back_path(const wchar_t *path, const struct win32_apply_ctx *ctx)
438 /* Does the path match a pattern given in the [PrepopulateList] section
439 * of WimBootCompress.ini? */
440 if (ctx->prepopulate_pats && match_pattern_list(path, ctx->prepopulate_pats,
444 /* Since we attempt to modify the SYSTEM registry after it's extracted
445 * (see end_wimboot_extraction()), it can't be extracted as externally
446 * backed. This extends to associated files such as SYSTEM.LOG that
447 * also must be writable in order to write to the registry. Normally,
448 * SYSTEM is in [PrepopulateList], and the SYSTEM.* files match patterns
449 * in [ExclusionList] and therefore are not captured in the WIM at all.
450 * However, a WIM that wasn't specifically captured in "WIMBoot mode"
451 * may contain SYSTEM.* files. So to make things "just work", hard-code
453 if (match_path(path, L"\\Windows\\System32\\config\\SYSTEM*", 0))
459 /* Can the specified WIM resource be used as the source of an external backing
460 * for the wof.sys WIM provider? */
462 is_resource_valid_for_external_backing(const struct wim_resource_descriptor *rdesc,
463 struct win32_apply_ctx *ctx)
465 /* Must be the original WIM file format. This check excludes pipable
466 * resources and solid resources. It also excludes other resources
467 * contained in such files even if they would be otherwise compatible.
469 if (rdesc->wim->hdr.magic != WIM_MAGIC ||
470 rdesc->wim->hdr.wim_version != WIM_VERSION_DEFAULT)
472 ctx->wimboot.have_wrong_version_wims = true;
477 * Whitelist of compression types and chunk sizes supported by
478 * Microsoft's WOF driver.
481 * - Uncompressed WIMs result in BSOD. However, this only applies to
482 * the WIM file itself, not to uncompressed resources in a WIM file
483 * that is otherwise compressed.
484 * - XPRESS 64K sometimes appears to work, but sometimes it causes
485 * reads to fail with STATUS_UNSUCCESSFUL.
487 switch (rdesc->compression_type) {
488 case WIMLIB_COMPRESSION_TYPE_NONE:
489 if (rdesc->wim->compression_type == WIMLIB_COMPRESSION_TYPE_NONE) {
490 ctx->wimboot.have_uncompressed_wims = true;
494 case WIMLIB_COMPRESSION_TYPE_XPRESS:
495 switch (rdesc->chunk_size) {
502 ctx->wimboot.have_unsupported_compressed_resources = true;
506 case WIMLIB_COMPRESSION_TYPE_LZX:
507 switch (rdesc->chunk_size) {
511 ctx->wimboot.have_unsupported_compressed_resources = true;
516 ctx->wimboot.have_unsupported_compressed_resources = true;
520 /* Microsoft's WoF driver errors out if it tries to satisfy a read with
521 * ending offset >= 4 GiB from an externally backed file. */
522 if (rdesc->uncompressed_size > 4200000000) {
523 ctx->wimboot.have_huge_resources = true;
530 #define EXTERNAL_BACKING_NOT_ENABLED -1
531 #define EXTERNAL_BACKING_NOT_POSSIBLE -2
532 #define EXTERNAL_BACKING_EXCLUDED -3
535 * Determines whether the specified file will be externally backed. Returns a
536 * negative status code if no, 0 if yes, or a positive wimlib error code on
537 * error. If the file is excluded from external backing based on its path, then
538 * *excluded_dentry_ret is set to the dentry for the path that matched the
541 * Note that this logic applies to both types of "external backing":
543 * - WIM backing ("WIMBoot" - Windows 8.1 and later)
544 * - File backing ("System Compression" - Windows 10 and later)
546 * However, in the case of WIM backing we also need to validate that the WIM
547 * resource that would be the source of the backing is supported by the wof.sys
551 will_externally_back_inode(struct wim_inode *inode, struct win32_apply_ctx *ctx,
552 const struct wim_dentry **excluded_dentry_ret,
555 struct wim_dentry *dentry;
556 struct blob_descriptor *blob;
559 if (load_prepopulate_pats(ctx) == WIMLIB_ERR_NOMEM)
560 return WIMLIB_ERR_NOMEM;
562 if (inode->i_can_externally_back)
565 /* This may do redundant checks because the cached value
566 * i_can_externally_back is 2-state (as opposed to 3-state:
567 * unknown/no/yes). But most files can be externally backed, so this
570 if (inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
571 FILE_ATTRIBUTE_REPARSE_POINT |
572 FILE_ATTRIBUTE_ENCRYPTED))
573 return EXTERNAL_BACKING_NOT_POSSIBLE;
575 blob = inode_get_blob_for_unnamed_data_stream_resolved(inode);
578 return EXTERNAL_BACKING_NOT_POSSIBLE;
581 (blob->blob_location != BLOB_IN_WIM ||
582 !is_resource_valid_for_external_backing(blob->rdesc, ctx)))
583 return EXTERNAL_BACKING_NOT_POSSIBLE;
586 * We need to check the patterns in [PrepopulateList] against every name
587 * of the inode, in case any of them match.
590 inode_for_each_extraction_alias(dentry, inode) {
592 ret = calculate_dentry_full_path(dentry);
596 if (!can_externally_back_path(dentry->d_full_path, ctx)) {
597 if (excluded_dentry_ret)
598 *excluded_dentry_ret = dentry;
599 return EXTERNAL_BACKING_EXCLUDED;
603 inode->i_can_externally_back = 1;
608 * Determines if the unnamed data stream of a file will be created as a WIM
609 * external backing (a "WIMBoot pointer file"), as opposed to a standard
613 win32_will_back_from_wim(struct wim_dentry *dentry, struct apply_ctx *_ctx)
615 struct win32_apply_ctx *ctx = (struct win32_apply_ctx *)_ctx;
617 if (!(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_WIMBOOT))
618 return EXTERNAL_BACKING_NOT_ENABLED;
620 return will_externally_back_inode(dentry->d_inode, ctx, NULL, true);
623 /* Find the WOF registration information for the specified WIM file. */
624 static struct wimboot_wim *
625 find_wimboot_wim(WIMStruct *wim_to_find, struct win32_apply_ctx *ctx)
627 for (size_t i = 0; i < ctx->wimboot.num_wims; i++)
628 if (wim_to_find == ctx->wimboot.wims[i].wim)
629 return &ctx->wimboot.wims[i];
636 set_backed_from_wim(HANDLE h, struct wim_inode *inode, struct win32_apply_ctx *ctx)
639 const struct wim_dentry *excluded_dentry;
640 const struct blob_descriptor *blob;
641 const struct wimboot_wim *wimboot_wim;
643 ret = will_externally_back_inode(inode, ctx, &excluded_dentry, true);
644 if (ret > 0) /* Error. */
647 if (ret < 0 && ret != EXTERNAL_BACKING_EXCLUDED)
648 return 0; /* Not externally backing, other than due to exclusion. */
650 if (unlikely(ret == EXTERNAL_BACKING_EXCLUDED)) {
651 /* Not externally backing due to exclusion. */
652 union wimlib_progress_info info;
654 build_extraction_path(excluded_dentry, ctx);
656 info.wimboot_exclude.path_in_wim = excluded_dentry->d_full_path;
657 info.wimboot_exclude.extraction_path = current_path(ctx);
659 return call_progress(ctx->common.progfunc,
660 WIMLIB_PROGRESS_MSG_WIMBOOT_EXCLUDE,
661 &info, ctx->common.progctx);
664 /* Externally backing. */
666 blob = inode_get_blob_for_unnamed_data_stream_resolved(inode);
667 wimboot_wim = find_wimboot_wim(blob->rdesc->wim, ctx);
669 if (unlikely(!wimboot_set_pointer(h,
671 wimboot_wim->data_source_id,
672 wimboot_wim->blob_table_hash,
673 ctx->wimboot.wof_running)))
675 const DWORD err = GetLastError();
677 build_extraction_path(inode_first_extraction_dentry(inode), ctx);
678 win32_error(err, L"\"%ls\": Couldn't set WIMBoot pointer data",
680 return WIMLIB_ERR_WIMBOOT;
685 /* Calculates the SHA-1 message digest of the WIM's blob table. */
687 hash_blob_table(WIMStruct *wim, u8 hash[SHA1_HASH_SIZE])
689 return wim_reshdr_to_hash(&wim->hdr.blob_table_reshdr, wim, hash);
693 register_wim_with_wof(WIMStruct *wim, struct win32_apply_ctx *ctx)
695 struct wimboot_wim *p;
698 /* Check if already registered */
699 for (size_t i = 0; i < ctx->wimboot.num_wims; i++)
700 if (wim == ctx->wimboot.wims[i].wim)
703 /* Not yet registered */
705 p = REALLOC(ctx->wimboot.wims,
706 (ctx->wimboot.num_wims + 1) * sizeof(ctx->wimboot.wims[0]));
708 return WIMLIB_ERR_NOMEM;
709 ctx->wimboot.wims = p;
711 ctx->wimboot.wims[ctx->wimboot.num_wims].wim = wim;
713 ret = hash_blob_table(wim, ctx->wimboot.wims[ctx->wimboot.num_wims].blob_table_hash);
717 ret = wimboot_alloc_data_source_id(wim->filename,
719 ctx->common.wim->current_image,
721 &ctx->wimboot.wims[ctx->wimboot.num_wims].data_source_id,
722 &ctx->wimboot.wof_running);
726 ctx->wimboot.num_wims++;
730 /* Prepare for doing a "WIMBoot" extraction by registering each source WIM file
731 * with WOF on the target volume. */
733 start_wimboot_extraction(struct list_head *dentry_list, struct win32_apply_ctx *ctx)
736 struct wim_dentry *dentry;
738 if (!xml_get_wimboot(ctx->common.wim->xml_info,
739 ctx->common.wim->current_image))
740 WARNING("The WIM image is not marked as WIMBoot compatible. This usually\n"
741 " means it is not intended to be used to back a Windows operating\n"
742 " system. Proceeding anyway.");
744 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
745 struct blob_descriptor *blob;
747 ret = win32_will_back_from_wim(dentry, &ctx->common);
748 if (ret > 0) /* Error */
750 if (ret < 0) /* Won't externally back */
753 blob = inode_get_blob_for_unnamed_data_stream_resolved(dentry->d_inode);
754 ret = register_wim_with_wof(blob->rdesc->wim, ctx);
759 if (ctx->wimboot.have_wrong_version_wims) {
760 WARNING("At least one of the source WIM files uses a version of the WIM\n"
761 " file format that not supported by Microsoft's wof.sys driver.\n"
762 " Files whose data is contained in one of these WIM files will be\n"
763 " extracted as full files rather than externally backed.");
766 if (ctx->wimboot.have_uncompressed_wims) {
767 WARNING("At least one of the source WIM files is uncompressed. Files whose\n"
768 " data is contained in an uncompressed WIM file will be extracted as\n"
769 " full files rather than externally backed, since uncompressed WIM\n"
770 " files are not supported by Microsoft's wof.sys driver.");
773 if (ctx->wimboot.have_unsupported_compressed_resources) {
774 WARNING("At least one of the source WIM files uses a compression format that\n"
775 " is not supported by Microsoft's wof.sys driver. Files whose data is\n"
776 " contained in a compressed resource in one of these WIM files will be\n"
777 " extracted as full files rather than externally backed. (The\n"
778 " compression formats supported by wof.sys are: XPRESS 4K, XPRESS 8K,\n"
779 " XPRESS 16K, XPRESS 32K, and LZX 32K.)");
782 if (ctx->wimboot.have_huge_resources) {
783 WARNING("Some files exceeded 4.2 GB in size. Such files will be extracted\n"
784 " as full files rather than externally backed, since very large files\n"
785 " are not supported by Microsoft's wof.sys driver.");
792 build_win32_extraction_path(const struct wim_dentry *dentry,
793 struct win32_apply_ctx *ctx);
795 /* Sets WimBoot=1 in the extracted SYSTEM registry hive.
797 * WIMGAPI does this, and it's possible that it's important.
798 * But I don't know exactly what this value means to Windows. */
800 end_wimboot_extraction(struct win32_apply_ctx *ctx)
802 struct wim_dentry *dentry;
803 wchar_t subkeyname[32];
809 dentry = get_dentry(ctx->common.wim, L"\\Windows\\System32\\config\\SYSTEM",
810 WIMLIB_CASE_INSENSITIVE);
812 if (!dentry || !will_extract_dentry(dentry))
815 if (!will_extract_dentry(wim_get_current_root_dentry(ctx->common.wim)))
818 /* Not bothering to use the native routines (e.g. NtLoadKey()) for this.
819 * If this doesn't work, you probably also have many other problems. */
821 build_win32_extraction_path(dentry, ctx);
823 get_random_alnum_chars(subkeyname, 20);
824 subkeyname[20] = L'\0';
826 res = RegLoadKey(HKEY_LOCAL_MACHINE, subkeyname, ctx->pathbuf.Buffer);
830 wcscpy(&subkeyname[20], L"\\Setup");
832 res = RegCreateKeyEx(HKEY_LOCAL_MACHINE, subkeyname, 0, NULL,
833 REG_OPTION_BACKUP_RESTORE, 0, NULL, &key, NULL);
839 res = RegSetValueEx(key, L"WimBoot", 0, REG_DWORD,
840 (const BYTE *)&value, sizeof(DWORD));
844 res = RegFlushKey(key);
847 res2 = RegCloseKey(key);
851 subkeyname[20] = L'\0';
852 RegUnLoadKey(HKEY_LOCAL_MACHINE, subkeyname);
856 win32_warning(res, L"Failed to set \\Setup: dword \"WimBoot\"=1 "
857 "value in registry hive \"%ls\"",
858 ctx->pathbuf.Buffer);
864 /* Returns the number of wide characters needed to represent the path to the
865 * specified @dentry, relative to the target directory, when extracted.
867 * Does not include null terminator (not needed for NtCreateFile). */
869 dentry_extraction_path_length(const struct wim_dentry *dentry)
872 const struct wim_dentry *d;
876 len += d->d_extraction_name_nchars + 1;
878 } while (!dentry_is_root(d) && will_extract_dentry(d));
880 return --len; /* No leading slash */
883 /* Returns the length of the longest string that might need to be appended to
884 * the path to an alias of an inode to open or create a named data stream.
886 * If the inode has no named data streams, this will be 0. Otherwise, this will
887 * be 1 plus the length of the longest-named data stream, since the data stream
888 * name must be separated from the path by the ':' character. */
890 inode_longest_named_data_stream_spec(const struct wim_inode *inode)
893 for (unsigned i = 0; i < inode->i_num_streams; i++) {
894 const struct wim_inode_stream *strm = &inode->i_streams[i];
895 if (!stream_is_named_data_stream(strm))
897 size_t len = utf16le_len_chars(strm->stream_name);
906 /* Find the length, in wide characters, of the longest path needed for
907 * extraction of any file in @dentry_list relative to the target directory.
909 * Accounts for named data streams, but does not include null terminator (not
910 * needed for NtCreateFile). */
912 compute_path_max(struct list_head *dentry_list)
915 const struct wim_dentry *dentry;
917 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
920 len = dentry_extraction_path_length(dentry);
922 /* Account for named data streams */
923 len += inode_longest_named_data_stream_spec(dentry->d_inode);
932 /* Build the path at which to extract the @dentry, relative to the target
935 * The path is saved in ctx->pathbuf. */
937 build_extraction_path(const struct wim_dentry *dentry,
938 struct win32_apply_ctx *ctx)
942 const struct wim_dentry *d;
944 len = dentry_extraction_path_length(dentry);
946 ctx->pathbuf.Length = len * sizeof(wchar_t);
947 p = ctx->pathbuf.Buffer + len;
949 !dentry_is_root(d->d_parent) && will_extract_dentry(d->d_parent);
952 p -= d->d_extraction_name_nchars;
953 if (d->d_extraction_name_nchars)
954 wmemcpy(p, d->d_extraction_name,
955 d->d_extraction_name_nchars);
958 /* No leading slash */
959 p -= d->d_extraction_name_nchars;
960 wmemcpy(p, d->d_extraction_name, d->d_extraction_name_nchars);
963 /* Build the path at which to extract the @dentry, relative to the target
964 * directory, adding the suffix for a named data stream.
966 * The path is saved in ctx->pathbuf. */
968 build_extraction_path_with_ads(const struct wim_dentry *dentry,
969 struct win32_apply_ctx *ctx,
970 const wchar_t *stream_name,
971 size_t stream_name_nchars)
975 build_extraction_path(dentry, ctx);
977 /* Add :NAME for named data stream */
978 p = ctx->pathbuf.Buffer + (ctx->pathbuf.Length / sizeof(wchar_t));
980 wmemcpy(p, stream_name, stream_name_nchars);
981 ctx->pathbuf.Length += (1 + stream_name_nchars) * sizeof(wchar_t);
984 /* Build the Win32 namespace path to the specified @dentry when extracted.
986 * The path is saved in ctx->pathbuf and will be null terminated.
988 * XXX: We could get rid of this if it wasn't needed for the file encryption
989 * APIs, and the registry manipulation in WIMBoot mode. */
991 build_win32_extraction_path(const struct wim_dentry *dentry,
992 struct win32_apply_ctx *ctx)
994 build_extraction_path(dentry, ctx);
996 /* Prepend target_ntpath to our relative path, then change \??\ into \\?\ */
998 memmove(ctx->pathbuf.Buffer +
999 (ctx->target_ntpath.Length / sizeof(wchar_t)) + 1,
1000 ctx->pathbuf.Buffer, ctx->pathbuf.Length);
1001 memcpy(ctx->pathbuf.Buffer, ctx->target_ntpath.Buffer,
1002 ctx->target_ntpath.Length);
1003 ctx->pathbuf.Buffer[ctx->target_ntpath.Length / sizeof(wchar_t)] = L'\\';
1004 ctx->pathbuf.Length += ctx->target_ntpath.Length + sizeof(wchar_t);
1005 ctx->pathbuf.Buffer[ctx->pathbuf.Length / sizeof(wchar_t)] = L'\0';
1007 wimlib_assert(ctx->pathbuf.Length >= 4 * sizeof(wchar_t) &&
1008 !wmemcmp(ctx->pathbuf.Buffer, L"\\??\\", 4));
1010 ctx->pathbuf.Buffer[1] = L'\\';
1014 /* Returns a "printable" representation of the last relative NT path that was
1015 * constructed with build_extraction_path() or build_extraction_path_with_ads().
1017 * This will be overwritten by the next call to this function. */
1018 static const wchar_t *
1019 current_path(struct win32_apply_ctx *ctx)
1021 wchar_t *p = ctx->print_buffer;
1023 p = wmempcpy(p, ctx->common.target, ctx->common.target_nchars);
1025 p = wmempcpy(p, ctx->pathbuf.Buffer, ctx->pathbuf.Length / sizeof(wchar_t));
1027 return ctx->print_buffer;
1030 /* Open handle to the target directory if it is not already open. If the target
1031 * directory does not exist, this creates it. */
1033 open_target_directory(struct win32_apply_ctx *ctx)
1040 ctx->attr.Length = sizeof(ctx->attr);
1041 ctx->attr.RootDirectory = NULL;
1042 ctx->attr.ObjectName = &ctx->target_ntpath;
1044 /* Don't use FILE_OPEN_REPARSE_POINT here; we want the extraction to
1045 * happen at the directory "pointed to" by the reparse point. */
1046 status = NtCreateFile(&ctx->h_target,
1052 FILE_SHARE_VALID_FLAGS,
1054 FILE_DIRECTORY_FILE | FILE_OPEN_FOR_BACKUP_INTENT,
1057 if (!NT_SUCCESS(status)) {
1058 winnt_error(status, L"Can't open or create directory \"%ls\"",
1059 ctx->common.target);
1060 return WIMLIB_ERR_OPENDIR;
1062 ctx->attr.RootDirectory = ctx->h_target;
1063 ctx->attr.ObjectName = &ctx->pathbuf;
1068 close_target_directory(struct win32_apply_ctx *ctx)
1070 if (ctx->h_target) {
1071 NtClose(ctx->h_target);
1072 ctx->h_target = NULL;
1073 ctx->attr.RootDirectory = NULL;
1078 * Ensures the target directory exists and opens a handle to it, in preparation
1079 * of using paths relative to it.
1082 prepare_target(struct list_head *dentry_list, struct win32_apply_ctx *ctx)
1087 ret = win32_path_to_nt_path(ctx->common.target, &ctx->target_ntpath);
1091 ret = open_target_directory(ctx);
1095 path_max = compute_path_max(dentry_list);
1096 /* Add some extra for building Win32 paths for the file encryption APIs,
1097 * and ensure we have at least enough to potentially use a 8.3 name for
1098 * the last component. */
1099 path_max += max(2 + (ctx->target_ntpath.Length / sizeof(wchar_t)),
1102 ctx->pathbuf.MaximumLength = path_max * sizeof(wchar_t);
1103 if (ctx->pathbuf.MaximumLength != path_max * sizeof(wchar_t)) {
1104 /* Paths are too long for a UNICODE_STRING! */
1105 ERROR("Some paths are too long to extract (> 32768 characters)!");
1106 return WIMLIB_ERR_UNSUPPORTED;
1109 ctx->pathbuf.Buffer = MALLOC(ctx->pathbuf.MaximumLength);
1110 if (!ctx->pathbuf.Buffer)
1111 return WIMLIB_ERR_NOMEM;
1113 ctx->print_buffer = MALLOC((ctx->common.target_nchars + 1 + path_max + 1) *
1115 if (!ctx->print_buffer)
1116 return WIMLIB_ERR_NOMEM;
1121 /* When creating an inode that will have a short (DOS) name, we create it using
1122 * the long name associated with the short name. This ensures that the short
1123 * name gets associated with the correct long name. */
1124 static struct wim_dentry *
1125 first_extraction_alias(const struct wim_inode *inode)
1127 struct wim_dentry *dentry;
1129 inode_for_each_extraction_alias(dentry, inode)
1130 if (dentry_has_short_name(dentry))
1132 return inode_first_extraction_dentry(inode);
1136 * Set or clear FILE_ATTRIBUTE_COMPRESSED if the inherited value is different
1137 * from the desired value.
1139 * Note that you can NOT override the inherited value of
1140 * FILE_ATTRIBUTE_COMPRESSED directly with NtCreateFile().
1143 adjust_compression_attribute(HANDLE h, const struct wim_dentry *dentry,
1144 struct win32_apply_ctx *ctx)
1146 const bool compressed = (dentry->d_inode->i_attributes &
1147 FILE_ATTRIBUTE_COMPRESSED);
1148 FILE_BASIC_INFORMATION info;
1149 USHORT compression_state;
1152 if (ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES)
1155 if (!ctx->common.supported_features.compressed_files)
1159 /* Get current attributes */
1160 status = NtQueryInformationFile(h, &ctx->iosb, &info, sizeof(info),
1161 FileBasicInformation);
1162 if (NT_SUCCESS(status) &&
1163 compressed == !!(info.FileAttributes & FILE_ATTRIBUTE_COMPRESSED))
1165 /* Nothing needs to be done. */
1169 /* Set the new compression state */
1172 compression_state = COMPRESSION_FORMAT_DEFAULT;
1174 compression_state = COMPRESSION_FORMAT_NONE;
1176 status = winnt_fsctl(h, FSCTL_SET_COMPRESSION,
1177 &compression_state, sizeof(USHORT), NULL, 0, NULL);
1178 if (NT_SUCCESS(status))
1181 winnt_error(status, L"Can't %s compression attribute on \"%ls\"",
1182 (compressed ? "set" : "clear"), current_path(ctx));
1183 return WIMLIB_ERR_SET_ATTRIBUTES;
1187 need_sparse_flag(const struct wim_inode *inode,
1188 const struct win32_apply_ctx *ctx)
1190 return (inode->i_attributes & FILE_ATTRIBUTE_SPARSE_FILE) &&
1191 ctx->common.supported_features.sparse_files;
1195 set_sparse_flag(HANDLE h, struct win32_apply_ctx *ctx)
1199 status = winnt_fsctl(h, FSCTL_SET_SPARSE, NULL, 0, NULL, 0, NULL);
1200 if (NT_SUCCESS(status))
1203 winnt_error(status, L"Can't set sparse flag on \"%ls\"",
1205 return WIMLIB_ERR_SET_ATTRIBUTES;
1208 /* Try to enable short name support on the target volume. If successful, return
1209 * true. If unsuccessful, issue a warning and return false. */
1211 try_to_enable_short_names(const wchar_t *volume)
1214 FILE_FS_PERSISTENT_VOLUME_INFORMATION info;
1216 DWORD bytesReturned;
1218 h = CreateFile(volume, GENERIC_WRITE,
1219 FILE_SHARE_VALID_FLAGS, NULL, OPEN_EXISTING,
1220 FILE_FLAG_BACKUP_SEMANTICS, NULL);
1221 if (h == INVALID_HANDLE_VALUE)
1224 info.VolumeFlags = 0;
1225 info.FlagMask = PERSISTENT_VOLUME_STATE_SHORT_NAME_CREATION_DISABLED;
1229 bret = DeviceIoControl(h, FSCTL_SET_PERSISTENT_VOLUME_STATE,
1230 &info, sizeof(info), NULL, 0,
1231 &bytesReturned, NULL);
1240 win32_warning(GetLastError(),
1241 L"Failed to enable short name support on %ls",
1247 remove_conflicting_short_name(const struct wim_dentry *dentry, struct win32_apply_ctx *ctx)
1253 size_t bufsize = offsetof(FILE_NAME_INFORMATION, FileName) +
1254 (13 * sizeof(wchar_t));
1255 u8 buf[bufsize] _aligned_attribute(8);
1256 bool retried = false;
1257 FILE_NAME_INFORMATION *info = (FILE_NAME_INFORMATION *)buf;
1259 memset(buf, 0, bufsize);
1261 /* Build the path with the short name. */
1262 name = &ctx->pathbuf.Buffer[ctx->pathbuf.Length / sizeof(wchar_t)];
1263 while (name != ctx->pathbuf.Buffer && *(name - 1) != L'\\')
1265 end = mempcpy(name, dentry->d_short_name, dentry->d_short_name_nbytes);
1266 ctx->pathbuf.Length = ((u8 *)end - (u8 *)ctx->pathbuf.Buffer);
1268 /* Open the conflicting file (by short name). */
1269 status = NtOpenFile(&h, GENERIC_WRITE | DELETE,
1270 &ctx->attr, &ctx->iosb,
1271 FILE_SHARE_VALID_FLAGS,
1272 FILE_OPEN_REPARSE_POINT | FILE_OPEN_FOR_BACKUP_INTENT);
1273 if (!NT_SUCCESS(status)) {
1274 winnt_warning(status, L"Can't open \"%ls\"", current_path(ctx));
1279 WARNING("Overriding conflicting short name; path=\"%ls\"",
1283 /* Try to remove the short name on the conflicting file. */
1286 status = NtSetInformationFile(h, &ctx->iosb, info, bufsize,
1287 FileShortNameInformation);
1289 if (status == STATUS_INVALID_PARAMETER && !retried) {
1290 /* Microsoft forgot to make it possible to remove short names
1291 * until Windows 7. Oops. Use a random short name instead. */
1292 get_random_alnum_chars(info->FileName, 8);
1293 wcscpy(&info->FileName[8], L".WLB");
1294 info->FileNameLength = 12 * sizeof(wchar_t);
1300 build_extraction_path(dentry, ctx);
1304 /* Set the short name on the open file @h which has been created at the location
1305 * indicated by @dentry.
1307 * Note that this may add, change, or remove the short name.
1309 * @h must be opened with DELETE access.
1311 * Returns 0 or WIMLIB_ERR_SET_SHORT_NAME. The latter only happens in
1312 * STRICT_SHORT_NAMES mode.
1315 set_short_name(HANDLE h, const struct wim_dentry *dentry,
1316 struct win32_apply_ctx *ctx)
1319 if (!ctx->common.supported_features.short_names)
1323 * Note: The size of the FILE_NAME_INFORMATION buffer must be such that
1324 * FileName contains at least 2 wide characters (4 bytes). Otherwise,
1325 * NtSetInformationFile() will return STATUS_INFO_LENGTH_MISMATCH. This
1326 * is despite the fact that FileNameLength can validly be 0 or 2 bytes,
1327 * with the former case being removing the existing short name if
1328 * present, rather than setting one.
1330 * The null terminator is seemingly optional, but to be safe we include
1331 * space for it and zero all unused space.
1334 size_t bufsize = offsetof(FILE_NAME_INFORMATION, FileName) +
1335 max(dentry->d_short_name_nbytes, sizeof(wchar_t)) +
1337 u8 buf[bufsize] _aligned_attribute(8);
1338 FILE_NAME_INFORMATION *info = (FILE_NAME_INFORMATION *)buf;
1340 bool tried_to_remove_existing = false;
1342 memset(buf, 0, bufsize);
1344 info->FileNameLength = dentry->d_short_name_nbytes;
1345 memcpy(info->FileName, dentry->d_short_name, dentry->d_short_name_nbytes);
1348 status = NtSetInformationFile(h, &ctx->iosb, info, bufsize,
1349 FileShortNameInformation);
1350 if (NT_SUCCESS(status))
1353 if (status == STATUS_SHORT_NAMES_NOT_ENABLED_ON_VOLUME) {
1354 if (dentry->d_short_name_nbytes == 0)
1356 if (!ctx->tried_to_enable_short_names) {
1360 ctx->tried_to_enable_short_names = true;
1362 ret = win32_get_drive_path(ctx->common.target,
1366 if (try_to_enable_short_names(volume))
1372 * Short names can conflict in several cases:
1374 * - a file being extracted has a short name conflicting with an
1377 * - a file being extracted has a short name conflicting with another
1378 * file being extracted (possible, but shouldn't happen)
1380 * - a file being extracted has a short name that conflicts with the
1381 * automatically generated short name of a file we previously
1382 * extracted, but failed to set the short name for. Sounds unlikely,
1383 * but this actually does happen fairly often on versions of Windows
1384 * prior to Windows 7 because they do not support removing short names
1387 if (unlikely(status == STATUS_OBJECT_NAME_COLLISION) &&
1388 dentry->d_short_name_nbytes && !tried_to_remove_existing)
1390 tried_to_remove_existing = true;
1391 status = remove_conflicting_short_name(dentry, ctx);
1392 if (NT_SUCCESS(status))
1396 /* By default, failure to set short names is not an error (since short
1397 * names aren't too important anymore...). */
1398 if (!(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_SHORT_NAMES)) {
1399 if (dentry->d_short_name_nbytes)
1400 ctx->num_set_short_name_failures++;
1402 ctx->num_remove_short_name_failures++;
1406 winnt_error(status, L"Can't set short name on \"%ls\"", current_path(ctx));
1407 return WIMLIB_ERR_SET_SHORT_NAME;
1411 * A wrapper around NtCreateFile() to make it slightly more usable...
1412 * This uses the path currently constructed in ctx->pathbuf.
1414 * Also, we always specify SYNCHRONIZE access, FILE_OPEN_FOR_BACKUP_INTENT, and
1415 * FILE_OPEN_REPARSE_POINT.
1418 do_create_file(PHANDLE FileHandle,
1419 ACCESS_MASK DesiredAccess,
1420 PLARGE_INTEGER AllocationSize,
1421 ULONG FileAttributes,
1422 ULONG CreateDisposition,
1423 ULONG CreateOptions,
1424 struct win32_apply_ctx *ctx)
1426 return NtCreateFile(FileHandle,
1427 DesiredAccess | SYNCHRONIZE,
1432 FILE_SHARE_VALID_FLAGS,
1435 FILE_OPEN_FOR_BACKUP_INTENT |
1436 FILE_OPEN_REPARSE_POINT,
1441 /* Like do_create_file(), but builds the extraction path of the @dentry first.
1444 create_file(PHANDLE FileHandle,
1445 ACCESS_MASK DesiredAccess,
1446 PLARGE_INTEGER AllocationSize,
1447 ULONG FileAttributes,
1448 ULONG CreateDisposition,
1449 ULONG CreateOptions,
1450 const struct wim_dentry *dentry,
1451 struct win32_apply_ctx *ctx)
1453 build_extraction_path(dentry, ctx);
1454 return do_create_file(FileHandle,
1464 delete_file_or_stream(struct win32_apply_ctx *ctx)
1468 ULONG perms = DELETE;
1469 ULONG flags = FILE_NON_DIRECTORY_FILE | FILE_DELETE_ON_CLOSE;
1471 /* First try opening the file with FILE_DELETE_ON_CLOSE. In most cases,
1472 * all we have to do is that plus close the file handle. */
1474 status = do_create_file(&h, perms, NULL, 0, FILE_OPEN, flags, ctx);
1476 if (unlikely(status == STATUS_CANNOT_DELETE)) {
1477 /* This error occurs for files with FILE_ATTRIBUTE_READONLY set.
1478 * Try an alternate approach: first open the file without
1479 * FILE_DELETE_ON_CLOSE, then reset the file attributes, then
1480 * set the "delete" disposition on the handle. */
1481 if (flags & FILE_DELETE_ON_CLOSE) {
1482 flags &= ~FILE_DELETE_ON_CLOSE;
1483 perms |= FILE_WRITE_ATTRIBUTES;
1488 if (unlikely(!NT_SUCCESS(status))) {
1489 winnt_error(status, L"Can't open \"%ls\" for deletion "
1490 "(perms=%x, flags=%x)",
1491 current_path(ctx), perms, flags);
1492 return WIMLIB_ERR_OPEN;
1495 if (unlikely(!(flags & FILE_DELETE_ON_CLOSE))) {
1497 FILE_BASIC_INFORMATION basic_info =
1498 { .FileAttributes = FILE_ATTRIBUTE_NORMAL };
1499 status = NtSetInformationFile(h, &ctx->iosb, &basic_info,
1501 FileBasicInformation);
1503 if (!NT_SUCCESS(status)) {
1504 winnt_error(status, L"Can't reset attributes of \"%ls\" "
1505 "to prepare for deletion", current_path(ctx));
1507 return WIMLIB_ERR_SET_ATTRIBUTES;
1510 FILE_DISPOSITION_INFORMATION disp_info =
1511 { .DoDeleteFile = TRUE };
1512 status = NtSetInformationFile(h, &ctx->iosb, &disp_info,
1514 FileDispositionInformation);
1515 if (!NT_SUCCESS(status)) {
1516 winnt_error(status, L"Can't set delete-on-close "
1517 "disposition on \"%ls\"", current_path(ctx));
1519 return WIMLIB_ERR_SET_ATTRIBUTES;
1523 status = NtClose(h);
1524 if (unlikely(!NT_SUCCESS(status))) {
1525 winnt_error(status, L"Error closing \"%ls\" after setting "
1526 "delete-on-close disposition", current_path(ctx));
1527 return WIMLIB_ERR_OPEN;
1534 * Create a nondirectory file or named data stream at the current path,
1535 * superseding any that already exists at that path. If successful, return an
1536 * open handle to the file or named data stream with the requested permissions.
1539 supersede_file_or_stream(struct win32_apply_ctx *ctx, DWORD perms,
1543 bool retried = false;
1545 /* FILE_ATTRIBUTE_SYSTEM is needed to ensure that
1546 * FILE_ATTRIBUTE_ENCRYPTED doesn't get set before we want it to be. */
1548 status = do_create_file(h_ret,
1551 FILE_ATTRIBUTE_SYSTEM,
1553 FILE_NON_DIRECTORY_FILE,
1555 if (likely(NT_SUCCESS(status)))
1558 /* STATUS_OBJECT_NAME_COLLISION means that the file or stream already
1559 * exists. Delete the existing file or stream, then try again.
1561 * Note: we don't use FILE_OVERWRITE_IF or FILE_SUPERSEDE because of
1562 * problems with certain file attributes, especially
1563 * FILE_ATTRIBUTE_ENCRYPTED. FILE_SUPERSEDE is also broken in the
1564 * Windows PE ramdisk. */
1565 if (status == STATUS_OBJECT_NAME_COLLISION && !retried) {
1566 int ret = delete_file_or_stream(ctx);
1572 winnt_error(status, L"Can't create \"%ls\"", current_path(ctx));
1573 return WIMLIB_ERR_OPEN;
1576 /* Set the reparse point @rpbuf of length @rpbuflen on the extracted file
1577 * corresponding to the WIM dentry @dentry. */
1579 do_set_reparse_point(const struct wim_dentry *dentry,
1580 const struct reparse_buffer_disk *rpbuf, u16 rpbuflen,
1581 struct win32_apply_ctx *ctx)
1586 status = create_file(&h, GENERIC_WRITE, NULL,
1587 0, FILE_OPEN, 0, dentry, ctx);
1588 if (!NT_SUCCESS(status))
1591 status = winnt_fsctl(h, FSCTL_SET_REPARSE_POINT,
1592 rpbuf, rpbuflen, NULL, 0, NULL);
1595 if (NT_SUCCESS(status))
1598 /* On Windows, by default only the Administrator can create symbolic
1599 * links for some reason. By default we just issue a warning if this
1600 * appears to be the problem. Use WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS
1601 * to get a hard error. */
1602 if (!(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS)
1603 && (status == STATUS_PRIVILEGE_NOT_HELD ||
1604 status == STATUS_ACCESS_DENIED)
1605 && (dentry->d_inode->i_reparse_tag == WIM_IO_REPARSE_TAG_SYMLINK ||
1606 dentry->d_inode->i_reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT))
1608 WARNING("Can't create symbolic link \"%ls\"! \n"
1609 " (Need Administrator rights, or at least "
1611 " SeCreateSymbolicLink privilege.)",
1617 winnt_error(status, L"Can't set reparse data on \"%ls\"",
1619 return WIMLIB_ERR_SET_REPARSE_DATA;
1623 * Create empty named data streams and potentially a reparse point for the
1624 * specified file, if any.
1626 * Since these won't have blob descriptors, they won't show up in the call to
1627 * extract_blob_list(). Hence the need for the special case.
1630 create_empty_streams(const struct wim_dentry *dentry,
1631 struct win32_apply_ctx *ctx)
1633 const struct wim_inode *inode = dentry->d_inode;
1636 for (unsigned i = 0; i < inode->i_num_streams; i++) {
1637 const struct wim_inode_stream *strm = &inode->i_streams[i];
1639 if (stream_blob_resolved(strm) != NULL)
1642 if (strm->stream_type == STREAM_TYPE_REPARSE_POINT &&
1643 ctx->common.supported_features.reparse_points)
1645 u8 buf[REPARSE_DATA_OFFSET] _aligned_attribute(8);
1646 struct reparse_buffer_disk *rpbuf =
1647 (struct reparse_buffer_disk *)buf;
1648 complete_reparse_point(rpbuf, inode, 0);
1649 ret = do_set_reparse_point(dentry, rpbuf,
1650 REPARSE_DATA_OFFSET, ctx);
1653 } else if (stream_is_named_data_stream(strm) &&
1654 ctx->common.supported_features.named_data_streams)
1658 build_extraction_path_with_ads(dentry, ctx,
1660 utf16le_len_chars(strm->stream_name));
1662 * Note: do not request any permissions on the handle.
1663 * Otherwise, we may encounter a Windows bug where the
1664 * parent directory DACL denies read access to the new
1665 * named data stream, even when using backup semantics!
1667 ret = supersede_file_or_stream(ctx, 0, &h);
1669 build_extraction_path(dentry, ctx);
1681 * Creates the directory named by @dentry, or uses an existing directory at that
1682 * location. If necessary, sets the short name and/or fixes compression and
1683 * encryption attributes.
1685 * Returns 0, WIMLIB_ERR_MKDIR, or WIMLIB_ERR_SET_SHORT_NAME.
1688 create_directory(const struct wim_dentry *dentry, struct win32_apply_ctx *ctx)
1695 /* DELETE is needed for set_short_name(); GENERIC_READ and GENERIC_WRITE
1696 * are needed for adjust_compression_attribute(). */
1697 perms = GENERIC_READ | GENERIC_WRITE;
1698 if (!dentry_is_root(dentry))
1701 /* FILE_ATTRIBUTE_SYSTEM is needed to ensure that
1702 * FILE_ATTRIBUTE_ENCRYPTED doesn't get set before we want it to be. */
1703 status = create_file(&h, perms, NULL, FILE_ATTRIBUTE_SYSTEM,
1704 FILE_OPEN_IF, FILE_DIRECTORY_FILE, dentry, ctx);
1705 if (unlikely(!NT_SUCCESS(status))) {
1706 const wchar_t *path = current_path(ctx);
1707 winnt_error(status, L"Can't create directory \"%ls\"", path);
1709 /* Check for known issue with WindowsApps directory. */
1710 if (status == STATUS_ACCESS_DENIED &&
1711 (wcsstr(path, L"\\WindowsApps\\") ||
1712 wcsstr(path, L"\\InfusedApps\\"))) {
1714 "You seem to be trying to extract files to the WindowsApps directory.\n"
1715 " Windows 8.1 and later use new file permissions in this directory that\n"
1716 " cannot be overridden, even by backup/restore programs. To extract your\n"
1717 " files anyway, you need to choose a different target directory, delete\n"
1718 " the WindowsApps directory entirely, reformat the volume, do the\n"
1719 " extraction from a non-broken operating system such as Windows 7 or\n"
1720 " Linux, or wait for Microsoft to fix the design flaw in their operating\n"
1721 " system. This is *not* a bug in wimlib. See this thread for more\n"
1722 " information: https://wimlib.net/forums/viewtopic.php?f=1&t=261");
1724 return WIMLIB_ERR_MKDIR;
1727 if (ctx->iosb.Information == FILE_OPENED) {
1728 /* If we opened an existing directory, try to clear its file
1729 * attributes. As far as I know, this only actually makes a
1730 * difference in the case where a FILE_ATTRIBUTE_READONLY
1731 * directory has a named data stream which needs to be
1732 * extracted. You cannot create a named data stream of such a
1733 * directory, even though this contradicts Microsoft's
1734 * documentation for FILE_ATTRIBUTE_READONLY which states it is
1735 * not honored for directories! */
1736 if (!(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES)) {
1737 FILE_BASIC_INFORMATION basic_info =
1738 { .FileAttributes = FILE_ATTRIBUTE_NORMAL };
1739 NtSetInformationFile(h, &ctx->iosb, &basic_info,
1741 FileBasicInformation);
1745 if (!dentry_is_root(dentry)) {
1746 ret = set_short_name(h, dentry, ctx);
1751 ret = adjust_compression_attribute(h, dentry, ctx);
1758 * Create all the directories being extracted, other than the target directory
1761 * Note: we don't honor directory hard links. However, we don't allow them to
1762 * exist in WIM images anyway (see inode_fixup.c).
1765 create_directories(struct list_head *dentry_list,
1766 struct win32_apply_ctx *ctx)
1768 const struct wim_dentry *dentry;
1771 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
1773 if (!(dentry->d_inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY))
1776 /* Note: Here we include files with
1777 * FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_REPARSE_POINT, but we
1778 * wait until later to actually set the reparse data. */
1780 ret = create_directory(dentry, ctx);
1783 ret = create_empty_streams(dentry, ctx);
1785 ret = check_apply_error(dentry, ctx, ret);
1789 ret = report_file_created(&ctx->common);
1797 * Creates the nondirectory file named by @dentry.
1799 * On success, returns an open handle to the file in @h_ret, with GENERIC_READ,
1800 * GENERIC_WRITE, and DELETE access. Also, the path to the file will be saved
1801 * in ctx->pathbuf. On failure, returns an error code.
1804 create_nondirectory_inode(HANDLE *h_ret, const struct wim_dentry *dentry,
1805 struct win32_apply_ctx *ctx)
1810 build_extraction_path(dentry, ctx);
1812 ret = supersede_file_or_stream(ctx,
1813 GENERIC_READ | GENERIC_WRITE | DELETE,
1818 ret = adjust_compression_attribute(h, dentry, ctx);
1822 if (need_sparse_flag(dentry->d_inode, ctx)) {
1823 ret = set_sparse_flag(h, ctx);
1828 ret = create_empty_streams(dentry, ctx);
1841 /* Creates a hard link at the location named by @dentry to the file represented
1842 * by the open handle @h. Or, if the target volume does not support hard links,
1843 * create a separate file instead. */
1845 create_link(HANDLE h, const struct wim_dentry *dentry,
1846 struct win32_apply_ctx *ctx)
1848 if (ctx->common.supported_features.hard_links) {
1850 build_extraction_path(dentry, ctx);
1852 size_t bufsize = offsetof(FILE_LINK_INFORMATION, FileName) +
1853 ctx->pathbuf.Length + sizeof(wchar_t);
1854 u8 buf[bufsize] _aligned_attribute(8);
1855 FILE_LINK_INFORMATION *info = (FILE_LINK_INFORMATION *)buf;
1858 info->ReplaceIfExists = TRUE;
1859 info->RootDirectory = ctx->attr.RootDirectory;
1860 info->FileNameLength = ctx->pathbuf.Length;
1861 memcpy(info->FileName, ctx->pathbuf.Buffer, ctx->pathbuf.Length);
1862 info->FileName[info->FileNameLength / 2] = L'\0';
1864 /* Note: the null terminator isn't actually necessary,
1865 * but if you don't add the extra character, you get
1866 * STATUS_INFO_LENGTH_MISMATCH when FileNameLength
1867 * happens to be 2 */
1869 status = NtSetInformationFile(h, &ctx->iosb, info, bufsize,
1870 FileLinkInformation);
1871 if (NT_SUCCESS(status))
1873 winnt_error(status, L"Failed to create link \"%ls\"",
1875 return WIMLIB_ERR_LINK;
1880 ret = create_nondirectory_inode(&h2, dentry, ctx);
1889 /* Given an inode (represented by the open handle @h) for which one link has
1890 * been created (named by @first_dentry), create the other links.
1892 * Or, if the target volume does not support hard links, create separate files.
1894 * Note: This uses ctx->pathbuf and does not reset it.
1897 create_links(HANDLE h, const struct wim_dentry *first_dentry,
1898 struct win32_apply_ctx *ctx)
1900 const struct wim_inode *inode = first_dentry->d_inode;
1901 const struct wim_dentry *dentry;
1904 inode_for_each_extraction_alias(dentry, inode) {
1905 if (dentry != first_dentry) {
1906 ret = create_link(h, dentry, ctx);
1914 /* Create a nondirectory file, including all links. */
1916 create_nondirectory(struct wim_inode *inode, struct win32_apply_ctx *ctx)
1918 struct wim_dentry *first_dentry;
1922 first_dentry = first_extraction_alias(inode);
1924 /* Create first link. */
1925 ret = create_nondirectory_inode(&h, first_dentry, ctx);
1929 /* Set short name. */
1930 ret = set_short_name(h, first_dentry, ctx);
1932 /* Create additional links, OR if hard links are not supported just
1933 * create more files. */
1935 ret = create_links(h, first_dentry, ctx);
1937 /* "WIMBoot" extraction: set external backing by the WIM file if needed. */
1938 if (!ret && unlikely(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_WIMBOOT))
1939 ret = set_backed_from_wim(h, inode, ctx);
1945 /* Create all the nondirectory files being extracted, including all aliases
1948 create_nondirectories(struct list_head *dentry_list, struct win32_apply_ctx *ctx)
1950 struct wim_dentry *dentry;
1951 struct wim_inode *inode;
1954 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
1955 inode = dentry->d_inode;
1956 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)
1958 /* Call create_nondirectory() only once per inode */
1959 if (dentry == inode_first_extraction_dentry(inode)) {
1960 ret = create_nondirectory(inode, ctx);
1961 ret = check_apply_error(dentry, ctx, ret);
1965 ret = report_file_created(&ctx->common);
1973 close_handles(struct win32_apply_ctx *ctx)
1975 for (unsigned i = 0; i < ctx->num_open_handles; i++)
1976 NtClose(ctx->open_handles[i]);
1979 /* Prepare to read the next blob, which has size @blob_size, into an in-memory
1982 prepare_data_buffer(struct win32_apply_ctx *ctx, u64 blob_size)
1984 if (blob_size > ctx->data_buffer_size) {
1985 /* Larger buffer needed. */
1987 if ((size_t)blob_size != blob_size)
1989 new_buffer = REALLOC(ctx->data_buffer, blob_size);
1992 ctx->data_buffer = new_buffer;
1993 ctx->data_buffer_size = blob_size;
1995 /* On the first call this changes data_buffer_ptr from NULL, which tells
1996 * extract_chunk() that the data buffer needs to be filled while reading
1997 * the stream data. */
1998 ctx->data_buffer_ptr = ctx->data_buffer;
2003 begin_extract_blob_instance(const struct blob_descriptor *blob,
2004 struct wim_dentry *dentry,
2005 const struct wim_inode_stream *strm,
2006 struct win32_apply_ctx *ctx)
2011 if (unlikely(strm->stream_type == STREAM_TYPE_REPARSE_POINT)) {
2012 /* We can't write the reparse point stream directly; we must set
2013 * it with FSCTL_SET_REPARSE_POINT, which requires that all the
2014 * data be available. So, stage the data in a buffer. */
2015 if (!prepare_data_buffer(ctx, blob->size))
2016 return WIMLIB_ERR_NOMEM;
2017 list_add_tail(&dentry->d_tmp_list, &ctx->reparse_dentries);
2021 if (unlikely(strm->stream_type == STREAM_TYPE_EFSRPC_RAW_DATA)) {
2022 /* We can't write encrypted files directly; we must use
2023 * WriteEncryptedFileRaw(), which requires providing the data
2024 * through a callback function. This can't easily be combined
2025 * with our own callback-based approach.
2027 * The current workaround is to simply read the blob into memory
2028 * and write the encrypted file from that.
2030 * TODO: This isn't sufficient for extremely large encrypted
2031 * files. Perhaps we should create an extra thread to write
2033 if (!prepare_data_buffer(ctx, blob->size))
2034 return WIMLIB_ERR_NOMEM;
2035 list_add_tail(&dentry->d_tmp_list, &ctx->encrypted_dentries);
2039 /* It's a data stream (may be unnamed or named). */
2040 wimlib_assert(strm->stream_type == STREAM_TYPE_DATA);
2042 if (ctx->num_open_handles == MAX_OPEN_FILES) {
2043 /* XXX: Fix this. But because of the checks in
2044 * extract_blob_list(), this can now only happen on a filesystem
2045 * that does not support hard links. */
2046 ERROR("Can't extract data: too many open files!");
2047 return WIMLIB_ERR_UNSUPPORTED;
2051 if (unlikely(stream_is_named(strm))) {
2052 build_extraction_path_with_ads(dentry, ctx,
2054 utf16le_len_chars(strm->stream_name));
2056 build_extraction_path(dentry, ctx);
2060 /* Open a new handle */
2061 status = do_create_file(&h,
2062 FILE_WRITE_DATA | SYNCHRONIZE,
2063 NULL, 0, FILE_OPEN_IF,
2064 FILE_SEQUENTIAL_ONLY |
2065 FILE_SYNCHRONOUS_IO_NONALERT,
2067 if (!NT_SUCCESS(status)) {
2068 winnt_error(status, L"Can't open \"%ls\" for writing",
2070 return WIMLIB_ERR_OPEN;
2073 ctx->is_sparse_stream[ctx->num_open_handles] = false;
2074 if (need_sparse_flag(dentry->d_inode, ctx)) {
2075 /* If the stream is unnamed, then the sparse flag was already
2076 * set when the file was created. But if the stream is named,
2077 * then we need to set the sparse flag here. */
2078 if (unlikely(stream_is_named(strm))) {
2079 int ret = set_sparse_flag(h, ctx);
2085 ctx->is_sparse_stream[ctx->num_open_handles] = true;
2086 ctx->any_sparse_streams = true;
2088 /* Allocate space for the data. */
2089 FILE_ALLOCATION_INFORMATION info =
2090 { .AllocationSize = { .QuadPart = blob->size }};
2091 NtSetInformationFile(h, &ctx->iosb, &info, sizeof(info),
2092 FileAllocationInformation);
2094 ctx->open_handles[ctx->num_open_handles++] = h;
2098 /* Given a Windows NT namespace path, such as \??\e:\Windows\System32, return a
2099 * pointer to the suffix of the path that begins with the device directly, such
2100 * as e:\Windows\System32. */
2101 static const wchar_t *
2102 skip_nt_toplevel_component(const wchar_t *path, size_t path_nchars)
2104 static const wchar_t * const dirs[] = {
2109 const wchar_t * const end = path + path_nchars;
2111 for (size_t i = 0; i < ARRAY_LEN(dirs); i++) {
2112 size_t len = wcslen(dirs[i]);
2113 if (len <= (end - path) && !wmemcmp(path, dirs[i], len)) {
2115 while (path != end && *path == L'\\')
2124 * Given a Windows NT namespace path, such as \??\e:\Windows\System32, return a
2125 * pointer to the suffix of the path that is device-relative but possibly with
2126 * leading slashes, such as \Windows\System32.
2128 * The path has an explicit length and is not necessarily null terminated.
2130 static const wchar_t *
2131 get_device_relative_path(const wchar_t *path, size_t path_nchars)
2133 const wchar_t * const orig_path = path;
2134 const wchar_t * const end = path + path_nchars;
2136 path = skip_nt_toplevel_component(path, path_nchars);
2137 if (path == orig_path)
2140 while (path != end && *path != L'\\')
2147 * Given a reparse point buffer for an inode for which the absolute link target
2148 * was relativized when it was archived, de-relative the link target to be
2149 * consistent with the actual extraction location.
2152 try_rpfix(struct reparse_buffer_disk *rpbuf, u16 *rpbuflen_p,
2153 struct win32_apply_ctx *ctx)
2155 struct link_reparse_point link;
2156 size_t orig_subst_name_nchars;
2157 const wchar_t *relpath;
2158 size_t relpath_nchars;
2159 size_t target_ntpath_nchars;
2160 size_t fixed_subst_name_nchars;
2161 const wchar_t *fixed_print_name;
2162 size_t fixed_print_name_nchars;
2164 /* Do nothing if the reparse data is invalid. */
2165 if (parse_link_reparse_point(rpbuf, *rpbuflen_p, &link))
2168 /* Do nothing if the reparse point is a relative symbolic link. */
2169 if (link_is_relative_symlink(&link))
2172 /* Build the new substitute name from the NT namespace path to the
2173 * target directory, then a path separator, then the "device relative"
2174 * part of the old substitute name. */
2176 orig_subst_name_nchars = link.substitute_name_nbytes / sizeof(wchar_t);
2178 relpath = get_device_relative_path(link.substitute_name,
2179 orig_subst_name_nchars);
2180 relpath_nchars = orig_subst_name_nchars -
2181 (relpath - link.substitute_name);
2183 target_ntpath_nchars = ctx->target_ntpath.Length / sizeof(wchar_t);
2185 /* If the target directory is a filesystem root, such as \??\C:\, then
2186 * it already will have a trailing slash. Don't include this slash if
2187 * we are already adding slashes via 'relpath'. This prevents an extra
2188 * slash from being generated each time the link is extracted. And
2189 * unlike on UNIX, the number of slashes in paths on Windows can be
2190 * significant; Windows won't understand the link target if it contains
2191 * too many slashes. */
2192 if (target_ntpath_nchars > 0 && relpath_nchars > 0 &&
2193 ctx->target_ntpath.Buffer[target_ntpath_nchars - 1] == L'\\')
2194 target_ntpath_nchars--;
2196 /* Also remove extra slashes from the beginning of 'relpath'. Normally
2197 * this isn't needed, but this is here to make the extra slash(es) added
2198 * by wimlib pre-v1.9.1 get removed automatically. */
2199 while (relpath_nchars >= 2 &&
2200 relpath[0] == L'\\' && relpath[1] == L'\\') {
2205 fixed_subst_name_nchars = target_ntpath_nchars + relpath_nchars;
2207 wchar_t fixed_subst_name[fixed_subst_name_nchars];
2209 wmemcpy(fixed_subst_name, ctx->target_ntpath.Buffer, target_ntpath_nchars);
2210 wmemcpy(&fixed_subst_name[target_ntpath_nchars], relpath, relpath_nchars);
2211 /* Doesn't need to be null-terminated. */
2213 /* Print name should be Win32, but not all NT names can even be
2214 * translated to Win32 names. But we can at least delete the top-level
2215 * directory, such as \??\, and this will have the expected result in
2216 * the usual case. */
2217 fixed_print_name = skip_nt_toplevel_component(fixed_subst_name,
2218 fixed_subst_name_nchars);
2219 fixed_print_name_nchars = fixed_subst_name_nchars - (fixed_print_name -
2222 link.substitute_name = fixed_subst_name;
2223 link.substitute_name_nbytes = fixed_subst_name_nchars * sizeof(wchar_t);
2224 link.print_name = (wchar_t *)fixed_print_name;
2225 link.print_name_nbytes = fixed_print_name_nchars * sizeof(wchar_t);
2226 make_link_reparse_point(&link, rpbuf, rpbuflen_p);
2229 /* Sets the reparse point on the specified file. This handles "fixing" the
2230 * targets of absolute symbolic links and junctions if WIMLIB_EXTRACT_FLAG_RPFIX
2233 set_reparse_point(const struct wim_dentry *dentry,
2234 const struct reparse_buffer_disk *rpbuf, u16 rpbuflen,
2235 struct win32_apply_ctx *ctx)
2237 if ((ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX)
2238 && !(dentry->d_inode->i_rp_flags & WIM_RP_FLAG_NOT_FIXED))
2240 memcpy(&ctx->rpfixbuf, rpbuf, rpbuflen);
2241 try_rpfix(&ctx->rpfixbuf, &rpbuflen, ctx);
2242 rpbuf = &ctx->rpfixbuf;
2244 return do_set_reparse_point(dentry, rpbuf, rpbuflen, ctx);
2248 /* Import the next block of raw encrypted data */
2250 import_encrypted_data(PBYTE pbData, PVOID pvCallbackContext, PULONG Length)
2252 struct win32_apply_ctx *ctx = pvCallbackContext;
2255 copy_len = min(ctx->encrypted_size - ctx->encrypted_offset, *Length);
2256 memcpy(pbData, &ctx->data_buffer[ctx->encrypted_offset], copy_len);
2257 ctx->encrypted_offset += copy_len;
2259 return ERROR_SUCCESS;
2263 * Write the raw encrypted data to the already-created file (or directory)
2264 * corresponding to @dentry.
2266 * The raw encrypted data is provided in ctx->data_buffer, and its size is
2267 * ctx->encrypted_size.
2269 * This function may close the target directory, in which case the caller needs
2270 * to re-open it if needed.
2273 extract_encrypted_file(const struct wim_dentry *dentry,
2274 struct win32_apply_ctx *ctx)
2281 /* Temporarily build a Win32 path for OpenEncryptedFileRaw() */
2282 build_win32_extraction_path(dentry, ctx);
2284 flags = CREATE_FOR_IMPORT | OVERWRITE_HIDDEN;
2285 if (dentry->d_inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)
2286 flags |= CREATE_FOR_DIR;
2290 err = OpenEncryptedFileRaw(ctx->pathbuf.Buffer, flags, &rawctx);
2291 if (err == ERROR_SHARING_VIOLATION && !retried) {
2292 /* This can be caused by the handle we have open to the target
2293 * directory. Try closing it temporarily. */
2294 close_target_directory(ctx);
2299 /* Restore the NT namespace path */
2300 build_extraction_path(dentry, ctx);
2302 if (err != ERROR_SUCCESS) {
2303 win32_error(err, L"Can't open \"%ls\" for encrypted import",
2305 return WIMLIB_ERR_OPEN;
2308 ctx->encrypted_offset = 0;
2310 err = WriteEncryptedFileRaw(import_encrypted_data, ctx, rawctx);
2312 CloseEncryptedFileRaw(rawctx);
2314 if (err != ERROR_SUCCESS) {
2315 win32_error(err, L"Can't import encrypted file \"%ls\"",
2317 return WIMLIB_ERR_WRITE;
2323 /* Called when starting to read a blob for extraction */
2325 win32_begin_extract_blob(struct blob_descriptor *blob, void *_ctx)
2327 struct win32_apply_ctx *ctx = _ctx;
2328 const struct blob_extraction_target *targets = blob_extraction_targets(blob);
2331 ctx->num_open_handles = 0;
2332 ctx->data_buffer_ptr = NULL;
2333 ctx->any_sparse_streams = false;
2334 INIT_LIST_HEAD(&ctx->reparse_dentries);
2335 INIT_LIST_HEAD(&ctx->encrypted_dentries);
2337 for (u32 i = 0; i < blob->out_refcnt; i++) {
2338 const struct wim_inode *inode = targets[i].inode;
2339 const struct wim_inode_stream *strm = targets[i].stream;
2340 struct wim_dentry *dentry;
2342 /* A copy of the blob needs to be extracted to @inode. */
2344 if (ctx->common.supported_features.hard_links) {
2345 dentry = inode_first_extraction_dentry(inode);
2346 ret = begin_extract_blob_instance(blob, dentry, strm, ctx);
2347 ret = check_apply_error(dentry, ctx, ret);
2351 /* Hard links not supported. Extract the blob
2352 * separately to each alias of the inode. */
2353 inode_for_each_extraction_alias(dentry, inode) {
2354 ret = begin_extract_blob_instance(blob, dentry, strm, ctx);
2355 ret = check_apply_error(dentry, ctx, ret);
2370 pwrite_to_handle(HANDLE h, const void *data, size_t size, u64 offset)
2372 const void * const end = data + size;
2374 IO_STATUS_BLOCK iosb;
2377 for (p = data; p != end; p += iosb.Information,
2378 offset += iosb.Information)
2380 LARGE_INTEGER offs = { .QuadPart = offset };
2382 status = NtWriteFile(h, NULL, NULL, NULL, &iosb,
2383 (void *)p, min(INT32_MAX, end - p),
2385 if (!NT_SUCCESS(status)) {
2387 L"Error writing data to target volume");
2388 return WIMLIB_ERR_WRITE;
2394 /* Called when the next chunk of a blob has been read for extraction */
2396 win32_extract_chunk(const struct blob_descriptor *blob, u64 offset,
2397 const void *chunk, size_t size, void *_ctx)
2399 struct win32_apply_ctx *ctx = _ctx;
2400 const void * const end = chunk + size;
2408 * For sparse streams, only write nonzero regions. This lets the
2409 * filesystem use holes to represent zero regions.
2411 for (p = chunk; p != end; p += len, offset += len) {
2412 zeroes = maybe_detect_sparse_region(p, end - p, &len,
2413 ctx->any_sparse_streams);
2414 for (i = 0; i < ctx->num_open_handles; i++) {
2415 if (!zeroes || !ctx->is_sparse_stream[i]) {
2416 ret = pwrite_to_handle(ctx->open_handles[i],
2424 /* Copy the data chunk into the buffer (if needed) */
2425 if (ctx->data_buffer_ptr)
2426 ctx->data_buffer_ptr = mempcpy(ctx->data_buffer_ptr,
2432 get_system_compression_format(int extract_flags)
2434 if (extract_flags & WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS4K)
2435 return FILE_PROVIDER_COMPRESSION_FORMAT_XPRESS4K;
2437 if (extract_flags & WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS8K)
2438 return FILE_PROVIDER_COMPRESSION_FORMAT_XPRESS8K;
2440 if (extract_flags & WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS16K)
2441 return FILE_PROVIDER_COMPRESSION_FORMAT_XPRESS16K;
2443 return FILE_PROVIDER_COMPRESSION_FORMAT_LZX;
2447 static const wchar_t *
2448 get_system_compression_format_string(int format)
2451 case FILE_PROVIDER_COMPRESSION_FORMAT_XPRESS4K:
2453 case FILE_PROVIDER_COMPRESSION_FORMAT_XPRESS8K:
2455 case FILE_PROVIDER_COMPRESSION_FORMAT_XPRESS16K:
2456 return L"XPRESS16K";
2463 set_system_compression(HANDLE h, int format)
2467 struct wof_external_info wof_info;
2468 struct file_provider_external_info file_info;
2471 .version = WOF_CURRENT_VERSION,
2472 .provider = WOF_PROVIDER_FILE,
2475 .version = FILE_PROVIDER_CURRENT_VERSION,
2476 .compression_format = format,
2480 /* We intentionally use NtFsControlFile() rather than DeviceIoControl()
2481 * here because the "compressing this object would not save space"
2482 * status code does not map to a valid Win32 error code on older
2483 * versions of Windows (before Windows 10?). This can be a problem if
2484 * the WOFADK driver is being used rather than the regular WOF, since
2485 * WOFADK can be used on older versions of Windows. */
2486 status = winnt_fsctl(h, FSCTL_SET_EXTERNAL_BACKING,
2487 &in, sizeof(in), NULL, 0, NULL);
2489 if (status == 0xC000046F) /* "Compressing this object would not save space." */
2490 return STATUS_SUCCESS;
2495 /* Hard-coded list of files which the Windows bootloader may need to access
2496 * before the WOF driver has been loaded. */
2497 static const wchar_t * const bootloader_pattern_strings[] = {
2500 L"\\Windows\\AppPatch\\drvmain.sdb",
2501 L"\\Windows\\Boot\\DVD\\*",
2502 L"\\Windows\\Boot\\EFI\\*",
2503 L"\\Windows\\bootstat.dat",
2504 L"\\Windows\\Fonts\\vgaoem.fon",
2505 L"\\Windows\\Fonts\\vgasys.fon",
2506 L"\\Windows\\INF\\errata.inf",
2507 L"\\Windows\\System32\\config\\*",
2508 L"\\Windows\\System32\\ntkrnlpa.exe",
2509 L"\\Windows\\System32\\ntoskrnl.exe",
2510 L"\\Windows\\System32\\bootvid.dll",
2511 L"\\Windows\\System32\\ci.dll",
2512 L"\\Windows\\System32\\hal*.dll",
2513 L"\\Windows\\System32\\mcupdate_AuthenticAMD.dll",
2514 L"\\Windows\\System32\\mcupdate_GenuineIntel.dll",
2515 L"\\Windows\\System32\\pshed.dll",
2516 L"\\Windows\\System32\\apisetschema.dll",
2517 L"\\Windows\\System32\\api-ms-win*.dll",
2518 L"\\Windows\\System32\\ext-ms-win*.dll",
2519 L"\\Windows\\System32\\KernelBase.dll",
2520 L"\\Windows\\System32\\drivers\\*.sys",
2521 L"\\Windows\\System32\\*.nls",
2522 L"\\Windows\\System32\\kbd*.dll",
2523 L"\\Windows\\System32\\kd*.dll",
2524 L"\\Windows\\System32\\clfs.sys",
2525 L"\\Windows\\System32\\CodeIntegrity\\driver.stl",
2528 static const struct string_list bootloader_patterns = {
2529 .strings = (wchar_t **)bootloader_pattern_strings,
2530 .num_strings = ARRAY_LEN(bootloader_pattern_strings),
2534 set_system_compression_on_inode(struct wim_inode *inode, int format,
2535 struct win32_apply_ctx *ctx)
2537 bool retried = false;
2541 /* If it may be needed for compatibility with the Windows bootloader,
2542 * force this file to XPRESS4K or uncompressed format. The bootloader
2543 * of Windows 10 supports XPRESS4K only; older versions don't support
2544 * system compression at all. */
2545 if (!is_image_windows_10_or_later(ctx) ||
2546 format != FILE_PROVIDER_COMPRESSION_FORMAT_XPRESS4K)
2548 /* We need to check the patterns against every name of the
2549 * inode, in case any of them match. */
2550 struct wim_dentry *dentry;
2551 inode_for_each_extraction_alias(dentry, inode) {
2555 if (calculate_dentry_full_path(dentry)) {
2556 ERROR("Unable to compute file path!");
2557 return STATUS_NO_MEMORY;
2560 incompatible = match_pattern_list(dentry->d_full_path,
2561 &bootloader_patterns,
2563 FREE(dentry->d_full_path);
2564 dentry->d_full_path = NULL;
2569 warned = (ctx->num_system_compression_exclusions++ > 0);
2571 if (is_image_windows_10_or_later(ctx)) {
2572 /* Force to XPRESS4K */
2574 WARNING("For compatibility with the "
2575 "Windows bootloader, some "
2578 "using the XPRESS4K format "
2579 "instead of the %"TS" format\n"
2581 get_system_compression_format_string(format));
2583 format = FILE_PROVIDER_COMPRESSION_FORMAT_XPRESS4K;
2586 /* Force to uncompressed */
2588 WARNING("For compatibility with the "
2589 "Windows bootloader, some "
2591 " be compressed with"
2592 " system compression "
2593 "(\"compacted\").");
2595 return STATUS_SUCCESS;
2601 /* Open the extracted file. */
2602 status = create_file(&h, GENERIC_READ | GENERIC_WRITE, NULL,
2604 inode_first_extraction_dentry(inode), ctx);
2606 if (!NT_SUCCESS(status))
2609 /* Compress the file. If the attempt fails with "invalid device
2610 * request", then attach wof.sys (or wofadk.sys) and retry. */
2611 status = set_system_compression(h, format);
2612 if (unlikely(status == STATUS_INVALID_DEVICE_REQUEST && !retried)) {
2613 wchar_t drive_path[7];
2614 if (!win32_get_drive_path(ctx->common.target, drive_path) &&
2615 win32_try_to_attach_wof(drive_path + 4)) {
2626 * This function is called when doing a "compact-mode" extraction and we just
2627 * finished extracting a blob to one or more locations. For each location that
2628 * was the unnamed data stream of a file, this function compresses the
2629 * corresponding file using System Compression, if allowed.
2631 * Note: we're doing the compression immediately after extracting the data
2632 * rather than during a separate compression pass. This way should be faster
2633 * since the operating system should still have the file's data cached.
2635 * Note: we're having the operating system do the compression, which is not
2636 * ideal because wimlib could create the compressed data faster and more
2637 * efficiently (the compressed data format is identical to a WIM resource). But
2638 * we seemingly don't have a choice because WOF prevents applications from
2639 * creating its reparse points.
2642 handle_system_compression(struct blob_descriptor *blob, struct win32_apply_ctx *ctx)
2644 const struct blob_extraction_target *targets = blob_extraction_targets(blob);
2646 const int format = get_system_compression_format(ctx->common.extract_flags);
2648 for (u32 i = 0; i < blob->out_refcnt; i++) {
2649 struct wim_inode *inode = targets[i].inode;
2650 struct wim_inode_stream *strm = targets[i].stream;
2653 if (!stream_is_unnamed_data_stream(strm))
2656 if (will_externally_back_inode(inode, ctx, NULL, false) != 0)
2659 status = set_system_compression_on_inode(inode, format, ctx);
2660 if (likely(NT_SUCCESS(status)))
2663 if (status == STATUS_INVALID_DEVICE_REQUEST) {
2665 "The request to compress the extracted files using System Compression\n"
2666 " will not be honored because the operating system or target volume\n"
2667 " does not support it. System Compression is only supported on\n"
2668 " Windows 10 and later, and only on NTFS volumes.");
2669 ctx->common.extract_flags &= ~COMPACT_FLAGS;
2673 ctx->num_system_compression_failures++;
2674 if (ctx->num_system_compression_failures < 10) {
2675 winnt_warning(status, L"\"%ls\": Failed to compress "
2676 "extracted file using System Compression",
2678 } else if (ctx->num_system_compression_failures == 10) {
2679 WARNING("Suppressing further warnings about "
2680 "System Compression failures.");
2685 /* Called when a blob has been fully read for extraction */
2687 win32_end_extract_blob(struct blob_descriptor *blob, int status, void *_ctx)
2689 struct win32_apply_ctx *ctx = _ctx;
2691 const struct wim_dentry *dentry;
2693 /* Extend sparse streams to their final size. */
2694 if (ctx->any_sparse_streams && !status) {
2695 for (unsigned i = 0; i < ctx->num_open_handles; i++) {
2696 FILE_END_OF_FILE_INFORMATION info =
2697 { .EndOfFile = { .QuadPart = blob->size } };
2700 if (!ctx->is_sparse_stream[i])
2703 ntstatus = NtSetInformationFile(ctx->open_handles[i],
2705 &info, sizeof(info),
2706 FileEndOfFileInformation);
2707 if (!NT_SUCCESS(ntstatus)) {
2708 winnt_error(ntstatus, L"Error writing data to "
2709 "target volume (while extending)");
2710 status = WIMLIB_ERR_WRITE;
2721 if (unlikely(ctx->common.extract_flags & COMPACT_FLAGS))
2722 handle_system_compression(blob, ctx);
2724 if (likely(!ctx->data_buffer_ptr))
2727 if (!list_empty(&ctx->reparse_dentries)) {
2728 if (blob->size > REPARSE_DATA_MAX_SIZE) {
2729 dentry = list_first_entry(&ctx->reparse_dentries,
2730 struct wim_dentry, d_tmp_list);
2731 build_extraction_path(dentry, ctx);
2732 ERROR("Reparse data of \"%ls\" has size "
2733 "%"PRIu64" bytes (exceeds %u bytes)",
2734 current_path(ctx), blob->size,
2735 REPARSE_DATA_MAX_SIZE);
2736 ret = WIMLIB_ERR_INVALID_REPARSE_DATA;
2737 return check_apply_error(dentry, ctx, ret);
2740 memcpy(ctx->rpbuf.rpdata, ctx->data_buffer, blob->size);
2742 list_for_each_entry(dentry, &ctx->reparse_dentries, d_tmp_list) {
2744 /* Reparse point header */
2745 complete_reparse_point(&ctx->rpbuf, dentry->d_inode,
2748 ret = set_reparse_point(dentry, &ctx->rpbuf,
2749 REPARSE_DATA_OFFSET + blob->size,
2751 ret = check_apply_error(dentry, ctx, ret);
2757 if (!list_empty(&ctx->encrypted_dentries)) {
2758 ctx->encrypted_size = blob->size;
2759 list_for_each_entry(dentry, &ctx->encrypted_dentries, d_tmp_list) {
2760 ret = extract_encrypted_file(dentry, ctx);
2761 ret = check_apply_error(dentry, ctx, ret);
2764 /* Re-open the target directory if needed. */
2765 ret = open_target_directory(ctx);
2774 /* Attributes that can't be set directly */
2775 #define SPECIAL_ATTRIBUTES \
2776 (FILE_ATTRIBUTE_REPARSE_POINT | \
2777 FILE_ATTRIBUTE_DIRECTORY | \
2778 FILE_ATTRIBUTE_ENCRYPTED | \
2779 FILE_ATTRIBUTE_SPARSE_FILE | \
2780 FILE_ATTRIBUTE_COMPRESSED)
2783 set_object_id(HANDLE h, const struct wim_inode *inode,
2784 struct win32_apply_ctx *ctx)
2786 const void *object_id;
2790 if (!ctx->common.supported_features.object_ids)
2793 object_id = inode_get_object_id(inode, &len);
2794 if (likely(object_id == NULL)) /* No object ID? */
2797 status = winnt_fsctl(h, FSCTL_SET_OBJECT_ID,
2798 object_id, len, NULL, 0, NULL);
2799 if (NT_SUCCESS(status))
2802 /* Object IDs must be unique within the filesystem. A duplicate might
2803 * occur if an image containing object IDs is applied twice to the same
2804 * filesystem. Arguably, the user should be warned in this case; but
2805 * the reality seems to be that nothing important cares about object IDs
2806 * except the Distributed Link Tracking Service... so for now these
2807 * failures are just ignored. */
2808 if (status == STATUS_DUPLICATE_NAME ||
2809 status == STATUS_OBJECT_NAME_COLLISION)
2812 ctx->num_object_id_failures++;
2813 if (ctx->num_object_id_failures < 10) {
2814 winnt_warning(status, L"Can't set object ID on \"%ls\"",
2816 } else if (ctx->num_object_id_failures == 10) {
2817 WARNING("Suppressing further warnings about failure to set "
2823 set_xattrs(HANDLE h, const struct wim_inode *inode, struct win32_apply_ctx *ctx)
2825 const void *entries, *entries_end;
2827 const struct wim_xattr_entry *entry;
2829 u8 _buf[1024] _aligned_attribute(4);
2831 FILE_FULL_EA_INFORMATION *ea, *ea_prev;
2835 if (!ctx->common.supported_features.xattrs)
2838 entries = inode_get_xattrs(inode, &len);
2839 if (likely(entries == NULL || len == 0)) /* No extended attributes? */
2841 entries_end = entries + len;
2844 for (entry = entries; (void *)entry < entries_end;
2845 entry = xattr_entry_next(entry)) {
2846 if (!valid_xattr_entry(entry, entries_end - (void *)entry)) {
2847 ERROR("\"%"TS"\": extended attribute is corrupt or unsupported",
2848 inode_any_full_path(inode));
2849 return WIMLIB_ERR_INVALID_XATTR;
2852 bufsize += ALIGN(offsetof(FILE_FULL_EA_INFORMATION, EaName) +
2853 entry->name_len + 1 +
2854 le16_to_cpu(entry->value_len), 4);
2857 if (unlikely(bufsize != (u32)bufsize)) {
2858 ERROR("\"%"TS"\": too many extended attributes to extract!",
2859 inode_any_full_path(inode));
2860 return WIMLIB_ERR_INVALID_XATTR;
2863 if (unlikely(bufsize > sizeof(_buf))) {
2864 buf = MALLOC(bufsize);
2866 return WIMLIB_ERR_NOMEM;
2870 ea = (FILE_FULL_EA_INFORMATION *)buf;
2871 for (entry = entries; (void *)entry < entries_end;
2872 entry = xattr_entry_next(entry)) {
2876 ea_prev->NextEntryOffset = (u8 *)ea - (u8 *)ea_prev;
2877 ea->Flags = entry->flags;
2878 ea->EaNameLength = entry->name_len;
2879 ea->EaValueLength = le16_to_cpu(entry->value_len);
2880 p = mempcpy(ea->EaName, entry->name,
2881 ea->EaNameLength + 1 + ea->EaValueLength);
2882 while ((uintptr_t)p & 3)
2885 ea = (FILE_FULL_EA_INFORMATION *)p;
2887 ea_prev->NextEntryOffset = 0;
2888 wimlib_assert((u8 *)ea - buf == bufsize);
2890 status = NtSetEaFile(h, &ctx->iosb, buf, bufsize);
2891 if (unlikely(!NT_SUCCESS(status))) {
2892 if (status == STATUS_EAS_NOT_SUPPORTED) {
2893 /* This happens with Samba. */
2894 WARNING("Filesystem advertised extended attribute (EA) support, but it doesn't\n"
2895 " work. EAs will not be extracted.");
2896 ctx->common.supported_features.xattrs = 0;
2897 } else if (status == STATUS_INVALID_EA_NAME) {
2898 ctx->num_xattr_failures++;
2899 if (ctx->num_xattr_failures < 5) {
2900 winnt_warning(status,
2901 L"Can't set extended attributes on \"%ls\"",
2903 } else if (ctx->num_xattr_failures == 5) {
2904 WARNING("Suppressing further warnings about "
2905 "failure to set extended attributes.");
2908 winnt_error(status, L"Can't set extended attributes on \"%ls\"",
2910 ret = WIMLIB_ERR_SET_XATTR;
2921 /* Set the security descriptor @desc, of @desc_size bytes, on the file with open
2924 set_security_descriptor(HANDLE h, const void *_desc,
2925 size_t desc_size, struct win32_apply_ctx *ctx)
2927 SECURITY_INFORMATION info;
2929 SECURITY_DESCRIPTOR_RELATIVE *desc;
2932 * Ideally, we would just pass in the security descriptor buffer as-is.
2933 * But it turns out that Windows can mess up the security descriptor
2934 * even when using the low-level NtSetSecurityObject() function:
2936 * - Windows will clear SE_DACL_AUTO_INHERITED if it is set in the
2937 * passed buffer. To actually get Windows to set
2938 * SE_DACL_AUTO_INHERITED, the application must set the non-persistent
2939 * flag SE_DACL_AUTO_INHERIT_REQ. As usual, Microsoft didn't bother
2940 * to properly document either of these flags. It's unclear how
2941 * important SE_DACL_AUTO_INHERITED actually is, but to be safe we use
2942 * the SE_DACL_AUTO_INHERIT_REQ workaround to set it if needed.
2944 * - The above also applies to the equivalent SACL flags,
2945 * SE_SACL_AUTO_INHERITED and SE_SACL_AUTO_INHERIT_REQ.
2947 * - If the application says that it's setting
2948 * DACL_SECURITY_INFORMATION, then Windows sets SE_DACL_PRESENT in the
2949 * resulting security descriptor, even if the security descriptor the
2950 * application provided did not have a DACL. This seems to be
2951 * unavoidable, since omitting DACL_SECURITY_INFORMATION would cause a
2952 * default DACL to remain. Fortunately, this behavior seems harmless,
2953 * since the resulting DACL will still be "null" --- but it will be
2954 * "the other representation of null".
2956 * - The above also applies to SACL_SECURITY_INFORMATION and
2957 * SE_SACL_PRESENT. Again, it's seemingly unavoidable but "harmless"
2958 * that Windows changes the representation of a "null SACL".
2960 if (likely(desc_size <= STACK_MAX)) {
2961 desc = alloca(desc_size);
2963 desc = MALLOC(desc_size);
2965 return STATUS_NO_MEMORY;
2968 memcpy(desc, _desc, desc_size);
2970 if (likely(desc_size >= 4)) {
2972 if (desc->Control & SE_DACL_AUTO_INHERITED)
2973 desc->Control |= SE_DACL_AUTO_INHERIT_REQ;
2975 if (desc->Control & SE_SACL_AUTO_INHERITED)
2976 desc->Control |= SE_SACL_AUTO_INHERIT_REQ;
2980 * More API insanity. We want to set the entire security descriptor
2981 * as-is. But all available APIs require specifying the specific parts
2982 * of the security descriptor being set. Especially annoying is that
2983 * mandatory integrity labels are part of the SACL, but they aren't set
2984 * with SACL_SECURITY_INFORMATION. Instead, applications must also
2985 * specify LABEL_SECURITY_INFORMATION (Windows Vista, Windows 7) or
2986 * BACKUP_SECURITY_INFORMATION (Windows 8). But at least older versions
2987 * of Windows don't error out if you provide these newer flags...
2989 * Also, if the process isn't running as Administrator, then it probably
2990 * doesn't have SE_RESTORE_PRIVILEGE. In this case, it will always get
2991 * the STATUS_PRIVILEGE_NOT_HELD error by trying to set the SACL, even
2992 * if the security descriptor it provided did not have a SACL. By
2993 * default, in this case we try to recover and set as much of the
2994 * security descriptor as possible --- potentially excluding the DACL, and
2995 * even the owner, as well as the SACL.
2998 info = OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
2999 DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION |
3000 LABEL_SECURITY_INFORMATION | BACKUP_SECURITY_INFORMATION;
3004 * It's also worth noting that SetFileSecurity() is unusable because it
3005 * doesn't request "backup semantics" when it opens the file internally.
3006 * NtSetSecurityObject() seems to be the best function to use in backup
3007 * applications. (SetSecurityInfo() should also work, but it's harder
3008 * to use and must call NtSetSecurityObject() internally anyway.
3009 * BackupWrite() is theoretically usable as well, but it's inflexible
3010 * and poorly documented.)
3014 status = NtSetSecurityObject(h, info, desc);
3015 if (NT_SUCCESS(status))
3016 goto out_maybe_free_desc;
3018 /* Failed to set the requested parts of the security descriptor. If the
3019 * error was permissions-related, try to set fewer parts of the security
3020 * descriptor, unless WIMLIB_EXTRACT_FLAG_STRICT_ACLS is enabled. */
3021 if ((status == STATUS_PRIVILEGE_NOT_HELD ||
3022 status == STATUS_ACCESS_DENIED) &&
3023 !(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_ACLS))
3025 if (info & SACL_SECURITY_INFORMATION) {
3026 info &= ~(SACL_SECURITY_INFORMATION |
3027 LABEL_SECURITY_INFORMATION |
3028 BACKUP_SECURITY_INFORMATION);
3029 ctx->partial_security_descriptors++;
3032 if (info & DACL_SECURITY_INFORMATION) {
3033 info &= ~DACL_SECURITY_INFORMATION;
3036 if (info & OWNER_SECURITY_INFORMATION) {
3037 info &= ~OWNER_SECURITY_INFORMATION;
3040 /* Nothing left except GROUP, and if we removed it we
3041 * wouldn't have anything at all. */
3044 /* No part of the security descriptor could be set, or
3045 * WIMLIB_EXTRACT_FLAG_STRICT_ACLS is enabled and the full security
3046 * descriptor could not be set. */
3047 if (!(info & SACL_SECURITY_INFORMATION))
3048 ctx->partial_security_descriptors--;
3049 ctx->no_security_descriptors++;
3051 out_maybe_free_desc:
3052 if (unlikely(desc_size > STACK_MAX))
3057 /* Set metadata on the open file @h from the WIM inode @inode. */
3059 do_apply_metadata_to_file(HANDLE h, const struct wim_inode *inode,
3060 struct win32_apply_ctx *ctx)
3062 FILE_BASIC_INFORMATION info;
3066 /* Set the file's object ID if present and object IDs are supported by
3067 * the filesystem. */
3068 set_object_id(h, inode, ctx);
3070 /* Set the file's extended attributes (EAs) if present and EAs are
3071 * supported by the filesystem. */
3072 ret = set_xattrs(h, inode, ctx);
3076 /* Set the file's security descriptor if present and we're not in
3078 if (inode_has_security_descriptor(inode) &&
3079 !(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_NO_ACLS))
3081 const struct wim_security_data *sd;
3085 sd = wim_get_current_security_data(ctx->common.wim);
3086 desc = sd->descriptors[inode->i_security_id];
3087 desc_size = sd->sizes[inode->i_security_id];
3089 status = set_security_descriptor(h, desc, desc_size, ctx);
3090 if (!NT_SUCCESS(status) &&
3091 (ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_ACLS))
3094 L"Can't set security descriptor on \"%ls\"",
3096 return WIMLIB_ERR_SET_SECURITY;
3100 /* Set attributes and timestamps */
3101 info.CreationTime.QuadPart = inode->i_creation_time;
3102 info.LastAccessTime.QuadPart = inode->i_last_access_time;
3103 info.LastWriteTime.QuadPart = inode->i_last_write_time;
3104 info.ChangeTime.QuadPart = 0;
3105 if (ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES) {
3106 info.FileAttributes = FILE_ATTRIBUTE_NORMAL;
3108 info.FileAttributes = inode->i_attributes & ~SPECIAL_ATTRIBUTES;
3109 if (info.FileAttributes == 0)
3110 info.FileAttributes = FILE_ATTRIBUTE_NORMAL;
3113 status = NtSetInformationFile(h, &ctx->iosb, &info, sizeof(info),
3114 FileBasicInformation);
3115 /* On FAT volumes we get STATUS_INVALID_PARAMETER if we try to set
3116 * attributes on the root directory. (Apparently because FAT doesn't
3117 * actually have a place to store those attributes!) */
3118 if (!NT_SUCCESS(status)
3119 && !(status == STATUS_INVALID_PARAMETER &&
3120 dentry_is_root(inode_first_extraction_dentry(inode))))
3122 winnt_error(status, L"Can't set basic metadata on \"%ls\"",
3124 return WIMLIB_ERR_SET_ATTRIBUTES;
3131 apply_metadata_to_file(const struct wim_dentry *dentry,
3132 struct win32_apply_ctx *ctx)
3134 const struct wim_inode *inode = dentry->d_inode;
3140 perms = FILE_WRITE_ATTRIBUTES | FILE_WRITE_EA | WRITE_DAC |
3141 WRITE_OWNER | ACCESS_SYSTEM_SECURITY;
3143 build_extraction_path(dentry, ctx);
3145 /* Open a handle with as many relevant permissions as possible. */
3146 while (!NT_SUCCESS(status = do_create_file(&h, perms, NULL,
3147 0, FILE_OPEN, 0, ctx)))
3149 if (status == STATUS_PRIVILEGE_NOT_HELD ||
3150 status == STATUS_ACCESS_DENIED)
3152 if (perms & ACCESS_SYSTEM_SECURITY) {
3153 perms &= ~ACCESS_SYSTEM_SECURITY;
3156 if (perms & WRITE_DAC) {
3157 perms &= ~WRITE_DAC;
3160 if (perms & WRITE_OWNER) {
3161 perms &= ~WRITE_OWNER;
3165 winnt_error(status, L"Can't open \"%ls\" to set metadata",
3167 return WIMLIB_ERR_OPEN;
3170 ret = do_apply_metadata_to_file(h, inode, ctx);
3178 apply_metadata(struct list_head *dentry_list, struct win32_apply_ctx *ctx)
3180 const struct wim_dentry *dentry;
3183 /* We go in reverse so that metadata is set on all a directory's
3184 * children before the directory itself. This avoids any potential
3185 * problems with attributes, timestamps, or security descriptors. */
3186 list_for_each_entry_reverse(dentry, dentry_list, d_extraction_list_node)
3188 ret = apply_metadata_to_file(dentry, ctx);
3189 ret = check_apply_error(dentry, ctx, ret);
3192 ret = report_file_metadata_applied(&ctx->common);
3199 /* Issue warnings about problems during the extraction for which warnings were
3200 * not already issued (due to the high number of potential warnings if we issued
3201 * them per-file). */
3203 do_warnings(const struct win32_apply_ctx *ctx)
3205 if (ctx->partial_security_descriptors == 0
3206 && ctx->no_security_descriptors == 0
3207 && ctx->num_set_short_name_failures == 0
3209 && ctx->num_remove_short_name_failures == 0
3214 WARNING("Extraction to \"%ls\" complete, but with one or more warnings:",
3215 ctx->common.target);
3216 if (ctx->num_set_short_name_failures) {
3217 WARNING("- Could not set short names on %lu files or directories",
3218 ctx->num_set_short_name_failures);
3221 if (ctx->num_remove_short_name_failures) {
3222 WARNING("- Could not remove short names on %lu files or directories"
3223 " (This is expected on Vista and earlier)",
3224 ctx->num_remove_short_name_failures);
3227 if (ctx->partial_security_descriptors) {
3228 WARNING("- Could only partially set the security descriptor\n"
3229 " on %lu files or directories.",
3230 ctx->partial_security_descriptors);
3232 if (ctx->no_security_descriptors) {
3233 WARNING("- Could not set security descriptor at all\n"
3234 " on %lu files or directories.",
3235 ctx->no_security_descriptors);
3237 if (ctx->partial_security_descriptors || ctx->no_security_descriptors) {
3238 WARNING("To fully restore all security descriptors, run the program\n"
3239 " with Administrator rights.");
3244 count_dentries(const struct list_head *dentry_list)
3246 const struct list_head *cur;
3249 list_for_each(cur, dentry_list)
3255 /* Extract files from a WIM image to a directory on Windows */
3257 win32_extract(struct list_head *dentry_list, struct apply_ctx *_ctx)
3260 struct win32_apply_ctx *ctx = (struct win32_apply_ctx *)_ctx;
3263 ret = prepare_target(dentry_list, ctx);
3267 if (unlikely(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_WIMBOOT)) {
3268 ret = start_wimboot_extraction(dentry_list, ctx);
3273 ctx->windows_build_number = xml_get_windows_build_number(ctx->common.wim->xml_info,
3274 ctx->common.wim->current_image);
3276 dentry_count = count_dentries(dentry_list);
3278 ret = start_file_structure_phase(&ctx->common, dentry_count);
3282 ret = create_directories(dentry_list, ctx);
3286 ret = create_nondirectories(dentry_list, ctx);
3290 ret = end_file_structure_phase(&ctx->common);
3294 struct read_blob_callbacks cbs = {
3295 .begin_blob = win32_begin_extract_blob,
3296 .continue_blob = win32_extract_chunk,
3297 .end_blob = win32_end_extract_blob,
3300 ret = extract_blob_list(&ctx->common, &cbs);
3304 ret = start_file_metadata_phase(&ctx->common, dentry_count);
3308 ret = apply_metadata(dentry_list, ctx);
3312 ret = end_file_metadata_phase(&ctx->common);
3316 if (unlikely(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_WIMBOOT)) {
3317 ret = end_wimboot_extraction(ctx);
3324 close_target_directory(ctx);
3325 if (ctx->target_ntpath.Buffer)
3326 HeapFree(GetProcessHeap(), 0, ctx->target_ntpath.Buffer);
3327 FREE(ctx->pathbuf.Buffer);
3328 FREE(ctx->print_buffer);
3329 FREE(ctx->wimboot.wims);
3330 if (ctx->prepopulate_pats) {
3331 FREE(ctx->prepopulate_pats->strings);
3332 FREE(ctx->prepopulate_pats);
3334 FREE(ctx->mem_prepopulate_pats);
3335 FREE(ctx->data_buffer);
3339 const struct apply_operations win32_apply_ops = {
3341 .get_supported_features = win32_get_supported_features,
3342 .extract = win32_extract,
3343 .will_back_from_wim = win32_will_back_from_wim,
3344 .context_size = sizeof(struct win32_apply_ctx),
3347 #endif /* __WIN32__ */