2 * win32_apply.c - Windows-specific code for applying files from a WIM image.
6 * Copyright (C) 2013, 2014, 2015 Eric Biggers
8 * This file is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU Lesser General Public License as published by the Free
10 * Software Foundation; either version 3 of the License, or (at your option) any
13 * This file is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this file; if not, see http://www.gnu.org/licenses/.
28 #include "wimlib/win32_common.h"
30 #include "wimlib/apply.h"
31 #include "wimlib/assert.h"
32 #include "wimlib/blob_table.h"
33 #include "wimlib/capture.h" /* for mangle_pat() and match_pattern_list() */
34 #include "wimlib/dentry.h"
35 #include "wimlib/encoding.h"
36 #include "wimlib/error.h"
37 #include "wimlib/metadata.h"
38 #include "wimlib/paths.h"
39 #include "wimlib/reparse.h"
40 #include "wimlib/textfile.h"
41 #include "wimlib/xml.h"
42 #include "wimlib/wildcard.h"
43 #include "wimlib/wimboot.h"
45 struct win32_apply_ctx {
47 /* Extract flags, the pointer to the WIMStruct, etc. */
48 struct apply_ctx common;
50 /* WIMBoot information, only filled in if WIMLIB_EXTRACT_FLAG_WIMBOOT
53 /* This array contains the WIM files registered with WOF on the
54 * target volume for this extraction operation. All WIMStructs
55 * in this array are distinct and have ->filename != NULL. */
59 u8 blob_table_hash[SHA1_HASH_SIZE];
62 struct string_set *prepopulate_pats;
63 void *mem_prepopulate_pats;
65 bool tried_to_load_prepopulate_list;
68 /* Open handle to the target directory */
71 /* NT namespace path to the target directory (buffer allocated) */
72 UNICODE_STRING target_ntpath;
74 /* Temporary buffer for building paths (buffer allocated) */
75 UNICODE_STRING pathbuf;
77 /* Object attributes to reuse for opening files in the target directory.
78 * (attr.ObjectName == &pathbuf) and (attr.RootDirectory == h_target).
80 OBJECT_ATTRIBUTES attr;
82 /* Temporary I/O status block for system calls */
85 /* Allocated buffer for creating "printable" paths from our
86 * target-relative NT paths */
87 wchar_t *print_buffer;
89 /* Allocated buffer for reading blob data when it cannot be extracted
93 /* Pointer to the next byte in @data_buffer to fill */
96 /* Size allocated in @data_buffer */
97 size_t data_buffer_size;
99 /* Current offset in the raw encrypted file being written */
100 size_t encrypted_offset;
102 /* Current size of the raw encrypted file being written */
103 size_t encrypted_size;
105 /* Temporary buffer for reparse data */
106 struct reparse_buffer_disk rpbuf;
108 /* Temporary buffer for reparse data of "fixed" absolute symbolic links
110 struct reparse_buffer_disk rpfixbuf;
112 /* Array of open handles to filesystem streams currently being written
114 HANDLE open_handles[MAX_OPEN_FILES];
116 /* Number of handles in @open_handles currently open (filled in from the
117 * beginning of the array) */
118 unsigned num_open_handles;
120 /* List of dentries, joined by @tmp_list, that need to have reparse data
121 * extracted as soon as the whole blob has been read into @data_buffer.
123 struct list_head reparse_dentries;
125 /* List of dentries, joined by @tmp_list, that need to have raw
126 * encrypted data extracted as soon as the whole blob has been read into
128 struct list_head encrypted_dentries;
130 /* Number of files for which we didn't have permission to set the full
131 * security descriptor. */
132 unsigned long partial_security_descriptors;
134 /* Number of files for which we didn't have permission to set any part
135 * of the security descriptor. */
136 unsigned long no_security_descriptors;
138 /* Number of files for which we couldn't set the short name. */
139 unsigned long num_set_short_name_failures;
141 /* Number of files for which we couldn't remove the short name. */
142 unsigned long num_remove_short_name_failures;
144 /* Have we tried to enable short name support on the target volume yet?
146 bool tried_to_enable_short_names;
149 /* Get the drive letter from a Windows path, or return the null character if the
150 * path is relative. */
152 get_drive_letter(const wchar_t *path)
154 /* Skip \\?\ prefix */
155 if (!wcsncmp(path, L"\\\\?\\", 4))
158 /* Return drive letter if valid */
159 if (((path[0] >= L'a' && path[0] <= L'z') ||
160 (path[0] >= L'A' && path[0] <= L'Z')) && path[1] == L':')
167 get_vol_flags(const wchar_t *target, DWORD *vol_flags_ret,
168 bool *short_names_supported_ret)
170 wchar_t filesystem_name[MAX_PATH + 1];
172 wchar_t *volume = NULL;
175 *short_names_supported_ret = false;
177 drive[0] = get_drive_letter(target);
185 if (!GetVolumeInformation(volume, NULL, 0, NULL, NULL,
186 vol_flags_ret, filesystem_name,
187 ARRAY_LEN(filesystem_name)))
189 win32_warning(GetLastError(),
190 L"Failed to get volume information for \"%ls\"",
195 if (wcsstr(filesystem_name, L"NTFS")) {
196 /* FILE_SUPPORTS_HARD_LINKS is only supported on Windows 7 and
197 * later. Force it on anyway if filesystem is NTFS. */
198 *vol_flags_ret |= FILE_SUPPORTS_HARD_LINKS;
200 /* There's no volume flag for short names, but according to the
201 * MS documentation they are only user-settable on NTFS. */
202 *short_names_supported_ret = true;
206 static const wchar_t *
207 current_path(struct win32_apply_ctx *ctx);
210 build_extraction_path(const struct wim_dentry *dentry,
211 struct win32_apply_ctx *ctx);
214 report_dentry_apply_error(const struct wim_dentry *dentry,
215 struct win32_apply_ctx *ctx, int ret)
217 build_extraction_path(dentry, ctx);
218 return report_apply_error(&ctx->common, ret, current_path(ctx));
222 check_apply_error(const struct wim_dentry *dentry,
223 struct win32_apply_ctx *ctx, int ret)
226 ret = report_dentry_apply_error(dentry, ctx, ret);
231 win32_get_supported_features(const wchar_t *target,
232 struct wim_features *supported_features)
235 bool short_names_supported;
237 /* Query the features of the target volume. */
239 get_vol_flags(target, &vol_flags, &short_names_supported);
241 supported_features->archive_files = 1;
242 supported_features->hidden_files = 1;
243 supported_features->system_files = 1;
245 if (vol_flags & FILE_FILE_COMPRESSION)
246 supported_features->compressed_files = 1;
248 if (vol_flags & FILE_SUPPORTS_ENCRYPTION) {
249 supported_features->encrypted_files = 1;
250 supported_features->encrypted_directories = 1;
253 supported_features->not_context_indexed_files = 1;
255 /* Don't do anything with FILE_SUPPORTS_SPARSE_FILES. */
257 if (vol_flags & FILE_NAMED_STREAMS)
258 supported_features->named_data_streams = 1;
260 if (vol_flags & FILE_SUPPORTS_HARD_LINKS)
261 supported_features->hard_links = 1;
263 if (vol_flags & FILE_SUPPORTS_REPARSE_POINTS)
264 supported_features->reparse_points = 1;
266 if (vol_flags & FILE_PERSISTENT_ACLS)
267 supported_features->security_descriptors = 1;
269 if (short_names_supported)
270 supported_features->short_names = 1;
272 supported_features->timestamps = 1;
274 /* Note: Windows does not support case sensitive filenames! At least
275 * not without changing the registry and rebooting... */
280 /* Load the patterns from [PrepopulateList] of WimBootCompress.ini in the WIM
281 * image being extracted. */
283 load_prepopulate_pats(struct win32_apply_ctx *ctx)
285 const wchar_t *path = L"\\Windows\\System32\\WimBootCompress.ini";
286 struct wim_dentry *dentry;
287 struct blob_descriptor *blob;
290 struct string_set *s;
292 struct text_file_section sec;
294 ctx->wimboot.tried_to_load_prepopulate_list = true;
296 dentry = get_dentry(ctx->common.wim, path, WIMLIB_CASE_INSENSITIVE);
298 (dentry->d_inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
299 FILE_ATTRIBUTE_REPARSE_POINT |
300 FILE_ATTRIBUTE_ENCRYPTED)) ||
301 !(blob = inode_get_blob_for_unnamed_data_stream(dentry->d_inode,
302 ctx->common.wim->blob_table)))
304 WARNING("%ls does not exist in WIM image!", path);
305 return WIMLIB_ERR_PATH_DOES_NOT_EXIST;
308 ret = read_full_blob_into_alloc_buf(blob, &buf);
312 s = CALLOC(1, sizeof(struct string_set));
315 return WIMLIB_ERR_NOMEM;
318 sec.name = T("PrepopulateList");
321 ret = do_load_text_file(path, buf, blob->size, &mem, &sec, 1,
322 LOAD_TEXT_FILE_REMOVE_QUOTES |
323 LOAD_TEXT_FILE_NO_WARNINGS,
325 BUILD_BUG_ON(OS_PREFERRED_PATH_SEPARATOR != WIM_PATH_SEPARATOR);
331 ctx->wimboot.prepopulate_pats = s;
332 ctx->wimboot.mem_prepopulate_pats = mem;
336 /* Returns %true if the specified absolute path to a file in the WIM image
337 * matches a pattern in [PrepopulateList] of WimBootCompress.ini. Otherwise
340 in_prepopulate_list(const wchar_t *path, size_t path_nchars,
341 const struct win32_apply_ctx *ctx)
343 const struct string_set *pats = ctx->wimboot.prepopulate_pats;
345 if (!pats || !pats->num_strings)
348 return match_pattern_list(path, path_nchars, pats);
351 /* Returns %true if the specified absolute path to a file in the WIM image can
352 * be subject to external backing when extracted. Otherwise returns %false. */
354 can_externally_back_path(const wchar_t *path, size_t path_nchars,
355 const struct win32_apply_ctx *ctx)
357 if (in_prepopulate_list(path, path_nchars, ctx))
360 /* Since we attempt to modify the SYSTEM registry after it's extracted
361 * (see end_wimboot_extraction()), it can't be extracted as externally
362 * backed. This extends to associated files such as SYSTEM.LOG that
363 * also must be writable in order to write to the registry. Normally,
364 * SYSTEM is in [PrepopulateList], and the SYSTEM.* files match patterns
365 * in [ExclusionList] and therefore are not captured in the WIM at all.
366 * However, a WIM that wasn't specifically captured in "WIMBoot mode"
367 * may contain SYSTEM.* files. So to make things "just work", hard-code
369 if (match_path(path, path_nchars, L"\\Windows\\System32\\config\\SYSTEM*",
370 OS_PREFERRED_PATH_SEPARATOR, false))
377 is_resource_valid_for_external_backing(const struct wim_resource_descriptor *rdesc)
379 /* Must be the original WIM file format. This check excludes pipable
380 * resources and solid resources. It also excludes other resources
381 * contained in such files even if they would be otherwise compatible.
383 if (rdesc->wim->hdr.magic != WIM_MAGIC ||
384 rdesc->wim->hdr.wim_version != WIM_VERSION_DEFAULT)
388 * Whitelist of compression types and chunk sizes supported by
389 * Microsoft's WOF driver.
392 * - Uncompressed WIMs result in BSOD. However, this only applies to
393 * the WIM file itself, not to uncompressed resources in a WIM file
394 * that is otherwise compressed.
395 * - XPRESS 64K sometimes appears to work, but sometimes it causes
396 * reads to fail with STATUS_UNSUCCESSFUL.
398 switch (rdesc->compression_type) {
399 case WIMLIB_COMPRESSION_TYPE_NONE:
400 if (rdesc->wim->compression_type == WIMLIB_COMPRESSION_TYPE_NONE)
403 case WIMLIB_COMPRESSION_TYPE_XPRESS:
404 switch (rdesc->chunk_size) {
414 case WIMLIB_COMPRESSION_TYPE_LZX:
415 switch (rdesc->chunk_size) {
426 /* Microsoft's WoF driver errors out if it tries to satisfy a read with
427 * ending offset >= 4 GiB from an externally backed file. */
428 if (rdesc->uncompressed_size > 4200000000)
434 #define WIM_BACKING_NOT_ENABLED -1
435 #define WIM_BACKING_NOT_POSSIBLE -2
436 #define WIM_BACKING_EXCLUDED -3
439 will_externally_back_inode(struct wim_inode *inode, struct win32_apply_ctx *ctx,
440 const struct wim_dentry **excluded_dentry_ret)
442 struct list_head *next;
443 struct wim_dentry *dentry;
444 struct blob_descriptor *blob;
447 if (inode->i_can_externally_back)
450 /* This may do redundant checks because the cached value
451 * i_can_externally_back is 2-state (as opposed to 3-state:
452 * unknown/no/yes). But most files can be externally backed, so this
455 if (inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
456 FILE_ATTRIBUTE_REPARSE_POINT |
457 FILE_ATTRIBUTE_ENCRYPTED))
458 return WIM_BACKING_NOT_POSSIBLE;
460 blob = inode_get_blob_for_unnamed_data_stream_resolved(inode);
462 if (!blob || blob->blob_location != BLOB_IN_WIM ||
463 !is_resource_valid_for_external_backing(blob->rdesc, ctx))
464 return WIM_BACKING_NOT_POSSIBLE;
467 * We need to check the patterns in [PrepopulateList] against every name
468 * of the inode, in case any of them match.
470 next = inode->i_extraction_aliases.next;
472 dentry = list_entry(next, struct wim_dentry,
473 d_extraction_alias_node);
475 ret = calculate_dentry_full_path(dentry);
479 if (!can_externally_back_path(dentry->_full_path,
480 wcslen(dentry->_full_path), ctx))
482 if (excluded_dentry_ret)
483 *excluded_dentry_ret = dentry;
484 return WIM_BACKING_EXCLUDED;
487 } while (next != &inode->i_extraction_aliases);
489 inode->i_can_externally_back = 1;
494 * Determines if the unnamed data stream of a file will be created as an
495 * external backing, as opposed to a standard extraction.
498 win32_will_externally_back(struct wim_dentry *dentry, struct apply_ctx *_ctx)
500 struct win32_apply_ctx *ctx = (struct win32_apply_ctx *)_ctx;
502 if (!(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_WIMBOOT))
503 return WIM_BACKING_NOT_ENABLED;
505 if (!ctx->wimboot.tried_to_load_prepopulate_list)
506 if (load_prepopulate_pats(ctx) == WIMLIB_ERR_NOMEM)
507 return WIMLIB_ERR_NOMEM;
509 return will_externally_back_inode(dentry->d_inode, ctx, NULL);
512 /* Find the WOF registration information for the specified WIM file. */
513 static struct wimboot_wim *
514 find_wimboot_wim(WIMStruct *wim_to_find, struct win32_apply_ctx *ctx)
516 for (size_t i = 0; i < ctx->wimboot.num_wims; i++)
517 if (wim_to_find == ctx->wimboot.wims[i].wim)
518 return &ctx->wimboot.wims[i];
525 set_external_backing(HANDLE h, struct wim_inode *inode, struct win32_apply_ctx *ctx)
528 const struct wim_dentry *excluded_dentry;
529 const struct blob_descriptor *blob;
530 const struct wimboot_wim *wimboot_wim;
532 ret = will_externally_back_inode(inode, ctx, &excluded_dentry);
533 if (ret > 0) /* Error. */
536 if (ret < 0 && ret != WIM_BACKING_EXCLUDED)
537 return 0; /* Not externally backing, other than due to exclusion. */
539 if (unlikely(ret == WIM_BACKING_EXCLUDED)) {
540 /* Not externally backing due to exclusion. */
541 union wimlib_progress_info info;
543 build_extraction_path(excluded_dentry, ctx);
545 info.wimboot_exclude.path_in_wim = excluded_dentry->_full_path;
546 info.wimboot_exclude.extraction_path = current_path(ctx);
548 return call_progress(ctx->common.progfunc,
549 WIMLIB_PROGRESS_MSG_WIMBOOT_EXCLUDE,
550 &info, ctx->common.progctx);
553 /* Externally backing. */
555 blob = inode_get_blob_for_unnamed_data_stream_resolved(inode);
556 wimboot_wim = find_wimboot_wim(blob->rdesc->wim, ctx);
558 if (unlikely(!wimboot_set_pointer(h,
560 wimboot_wim->data_source_id,
561 wimboot_wim->blob_table_hash,
562 ctx->wimboot.wof_running)))
564 const DWORD err = GetLastError();
566 build_extraction_path(inode_first_extraction_dentry(inode), ctx);
567 win32_error(err, L"\"%ls\": Couldn't set WIMBoot pointer data",
569 return WIMLIB_ERR_WIMBOOT;
574 /* Calculates the SHA-1 message digest of the WIM's blob table. */
576 hash_blob_table(WIMStruct *wim, u8 hash[SHA1_HASH_SIZE])
578 return wim_reshdr_to_hash(&wim->hdr.blob_table_reshdr, wim, hash);
582 register_wim_with_wof(WIMStruct *wim, struct win32_apply_ctx *ctx)
584 struct wimboot_wim *p;
587 /* Check if already registered */
588 for (size_t i = 0; i < ctx->wimboot.num_wims; i++)
589 if (wim == ctx->wimboot.wims[i].wim)
592 /* Not yet registered */
594 p = REALLOC(ctx->wimboot.wims,
595 (ctx->wimboot.num_wims + 1) * sizeof(ctx->wimboot.wims[0]));
597 return WIMLIB_ERR_NOMEM;
598 ctx->wimboot.wims = p;
600 ctx->wimboot.wims[ctx->wimboot.num_wims].wim = wim;
602 ret = hash_blob_table(wim, ctx->wimboot.wims[ctx->wimboot.num_wims].blob_table_hash);
606 ret = wimboot_alloc_data_source_id(wim->filename,
608 ctx->common.wim->current_image,
610 &ctx->wimboot.wims[ctx->wimboot.num_wims].data_source_id,
611 &ctx->wimboot.wof_running);
615 ctx->wimboot.num_wims++;
619 /* Prepare for doing a "WIMBoot" extraction by loading patterns from
620 * [PrepopulateList] of WimBootCompress.ini and registering each source WIM file
621 * with WOF on the target volume. */
623 start_wimboot_extraction(struct list_head *dentry_list, struct win32_apply_ctx *ctx)
626 struct wim_dentry *dentry;
628 if (!ctx->wimboot.tried_to_load_prepopulate_list)
629 if (load_prepopulate_pats(ctx) == WIMLIB_ERR_NOMEM)
630 return WIMLIB_ERR_NOMEM;
632 if (!wim_info_get_wimboot(ctx->common.wim->wim_info,
633 ctx->common.wim->current_image))
634 WARNING("Image is not marked as WIMBoot compatible!");
636 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
637 struct blob_descriptor *blob;
639 ret = win32_will_externally_back(dentry, &ctx->common);
640 if (ret > 0) /* Error */
642 if (ret < 0) /* Won't externally back */
645 blob = inode_get_blob_for_unnamed_data_stream_resolved(dentry->d_inode);
646 ret = register_wim_with_wof(blob->rdesc->wim, ctx);
655 build_win32_extraction_path(const struct wim_dentry *dentry,
656 struct win32_apply_ctx *ctx);
658 /* Sets WimBoot=1 in the extracted SYSTEM registry hive.
660 * WIMGAPI does this, and it's possible that it's important.
661 * But I don't know exactly what this value means to Windows. */
663 end_wimboot_extraction(struct win32_apply_ctx *ctx)
665 struct wim_dentry *dentry;
666 wchar_t subkeyname[32];
672 dentry = get_dentry(ctx->common.wim, L"\\Windows\\System32\\config\\SYSTEM",
673 WIMLIB_CASE_INSENSITIVE);
675 if (!dentry || !will_extract_dentry(dentry))
678 if (!will_extract_dentry(wim_get_current_root_dentry(ctx->common.wim)))
681 /* Not bothering to use the native routines (e.g. NtLoadKey()) for this.
682 * If this doesn't work, you probably also have many other problems. */
684 build_win32_extraction_path(dentry, ctx);
686 randomize_char_array_with_alnum(subkeyname, 20);
687 subkeyname[20] = L'\0';
689 res = RegLoadKey(HKEY_LOCAL_MACHINE, subkeyname, ctx->pathbuf.Buffer);
693 wcscpy(&subkeyname[20], L"\\Setup");
695 res = RegCreateKeyEx(HKEY_LOCAL_MACHINE, subkeyname, 0, NULL,
696 REG_OPTION_BACKUP_RESTORE, 0, NULL, &key, NULL);
702 res = RegSetValueEx(key, L"WimBoot", 0, REG_DWORD,
703 (const BYTE *)&value, sizeof(DWORD));
707 res = RegFlushKey(key);
710 res2 = RegCloseKey(key);
714 subkeyname[20] = L'\0';
715 RegUnLoadKey(HKEY_LOCAL_MACHINE, subkeyname);
719 win32_warning(res, L"Failed to set \\Setup: dword \"WimBoot\"=1 "
720 "value in registry hive \"%ls\"",
721 ctx->pathbuf.Buffer);
727 /* Returns the number of wide characters needed to represent the path to the
728 * specified @dentry, relative to the target directory, when extracted.
730 * Does not include null terminator (not needed for NtCreateFile). */
732 dentry_extraction_path_length(const struct wim_dentry *dentry)
735 const struct wim_dentry *d;
739 len += d->d_extraction_name_nchars + 1;
741 } while (!dentry_is_root(d) && will_extract_dentry(d));
743 return --len; /* No leading slash */
746 /* Returns the length of the longest string that might need to be appended to
747 * the path to an alias of an inode to open or create a named data stream.
749 * If the inode has no named data streams, this will be 0. Otherwise, this will
750 * be 1 plus the length of the longest-named data stream, since the data stream
751 * name must be separated from the path by the ':' character. */
753 inode_longest_named_data_stream_spec(const struct wim_inode *inode)
756 for (unsigned i = 0; i < inode->i_num_streams; i++) {
757 const struct wim_inode_stream *strm = &inode->i_streams[i];
758 if (!stream_is_named_data_stream(strm))
760 size_t len = utf16le_len_chars(strm->stream_name);
769 /* Find the length, in wide characters, of the longest path needed for
770 * extraction of any file in @dentry_list relative to the target directory.
772 * Accounts for named data streams, but does not include null terminator (not
773 * needed for NtCreateFile). */
775 compute_path_max(struct list_head *dentry_list)
778 const struct wim_dentry *dentry;
780 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
783 len = dentry_extraction_path_length(dentry);
785 /* Account for named data streams */
786 len += inode_longest_named_data_stream_spec(dentry->d_inode);
795 /* Build the path at which to extract the @dentry, relative to the target
798 * The path is saved in ctx->pathbuf. */
800 build_extraction_path(const struct wim_dentry *dentry,
801 struct win32_apply_ctx *ctx)
805 const struct wim_dentry *d;
807 len = dentry_extraction_path_length(dentry);
809 ctx->pathbuf.Length = len * sizeof(wchar_t);
810 p = ctx->pathbuf.Buffer + len;
812 !dentry_is_root(d->d_parent) && will_extract_dentry(d->d_parent);
815 p -= d->d_extraction_name_nchars;
816 wmemcpy(p, d->d_extraction_name, d->d_extraction_name_nchars);
819 /* No leading slash */
820 p -= d->d_extraction_name_nchars;
821 wmemcpy(p, d->d_extraction_name, d->d_extraction_name_nchars);
824 /* Build the path at which to extract the @dentry, relative to the target
825 * directory, adding the suffix for a named data stream.
827 * The path is saved in ctx->pathbuf. */
829 build_extraction_path_with_ads(const struct wim_dentry *dentry,
830 struct win32_apply_ctx *ctx,
831 const wchar_t *stream_name,
832 size_t stream_name_nchars)
836 build_extraction_path(dentry, ctx);
838 /* Add :NAME for named data stream */
839 p = ctx->pathbuf.Buffer + (ctx->pathbuf.Length / sizeof(wchar_t));
841 wmemcpy(p, stream_name, stream_name_nchars);
842 ctx->pathbuf.Length += (1 + stream_name_nchars) * sizeof(wchar_t);
845 /* Build the Win32 namespace path to the specified @dentry when extracted.
847 * The path is saved in ctx->pathbuf and will be null terminated.
849 * XXX: We could get rid of this if it wasn't needed for the file encryption
850 * APIs, and the registry manipulation in WIMBoot mode. */
852 build_win32_extraction_path(const struct wim_dentry *dentry,
853 struct win32_apply_ctx *ctx)
855 build_extraction_path(dentry, ctx);
857 /* Prepend target_ntpath to our relative path, then change \??\ into \\?\ */
859 memmove(ctx->pathbuf.Buffer +
860 (ctx->target_ntpath.Length / sizeof(wchar_t)) + 1,
861 ctx->pathbuf.Buffer, ctx->pathbuf.Length);
862 memcpy(ctx->pathbuf.Buffer, ctx->target_ntpath.Buffer,
863 ctx->target_ntpath.Length);
864 ctx->pathbuf.Buffer[ctx->target_ntpath.Length / sizeof(wchar_t)] = L'\\';
865 ctx->pathbuf.Length += ctx->target_ntpath.Length + sizeof(wchar_t);
866 ctx->pathbuf.Buffer[ctx->pathbuf.Length / sizeof(wchar_t)] = L'\0';
868 wimlib_assert(ctx->pathbuf.Length >= 4 * sizeof(wchar_t) &&
869 !wmemcmp(ctx->pathbuf.Buffer, L"\\??\\", 4));
871 ctx->pathbuf.Buffer[1] = L'\\';
875 /* Returns a "printable" representation of the last relative NT path that was
876 * constructed with build_extraction_path() or build_extraction_path_with_ads().
878 * This will be overwritten by the next call to this function. */
879 static const wchar_t *
880 current_path(struct win32_apply_ctx *ctx)
882 wchar_t *p = ctx->print_buffer;
884 p = wmempcpy(p, ctx->common.target, ctx->common.target_nchars);
886 p = wmempcpy(p, ctx->pathbuf.Buffer, ctx->pathbuf.Length / sizeof(wchar_t));
888 return ctx->print_buffer;
891 /* Open handle to the target directory if it is not already open. If the target
892 * directory does not exist, this creates it. */
894 open_target_directory(struct win32_apply_ctx *ctx)
901 ctx->attr.Length = sizeof(ctx->attr);
902 ctx->attr.RootDirectory = NULL;
903 ctx->attr.ObjectName = &ctx->target_ntpath;
904 status = (*func_NtCreateFile)(&ctx->h_target,
910 FILE_SHARE_VALID_FLAGS,
912 FILE_DIRECTORY_FILE |
913 FILE_OPEN_REPARSE_POINT |
914 FILE_OPEN_FOR_BACKUP_INTENT,
917 if (!NT_SUCCESS(status)) {
918 winnt_error(status, L"Can't open or create directory \"%ls\"",
920 return WIMLIB_ERR_OPENDIR;
922 ctx->attr.RootDirectory = ctx->h_target;
923 ctx->attr.ObjectName = &ctx->pathbuf;
928 close_target_directory(struct win32_apply_ctx *ctx)
931 (*func_NtClose)(ctx->h_target);
932 ctx->h_target = NULL;
933 ctx->attr.RootDirectory = NULL;
938 * Ensures the target directory exists and opens a handle to it, in preparation
939 * of using paths relative to it.
942 prepare_target(struct list_head *dentry_list, struct win32_apply_ctx *ctx)
947 ret = win32_path_to_nt_path(ctx->common.target, &ctx->target_ntpath);
951 ret = open_target_directory(ctx);
955 path_max = compute_path_max(dentry_list);
956 /* Add some extra for building Win32 paths for the file encryption APIs,
957 * and ensure we have at least enough to potentially use a 8.3 name for
958 * the last component. */
959 path_max += max(2 + (ctx->target_ntpath.Length / sizeof(wchar_t)),
962 ctx->pathbuf.MaximumLength = path_max * sizeof(wchar_t);
963 ctx->pathbuf.Buffer = MALLOC(ctx->pathbuf.MaximumLength);
964 if (!ctx->pathbuf.Buffer)
965 return WIMLIB_ERR_NOMEM;
967 ctx->print_buffer = MALLOC((ctx->common.target_nchars + 1 + path_max + 1) *
969 if (!ctx->print_buffer)
970 return WIMLIB_ERR_NOMEM;
975 /* When creating an inode that will have a short (DOS) name, we create it using
976 * the long name associated with the short name. This ensures that the short
977 * name gets associated with the correct long name. */
978 static struct wim_dentry *
979 first_extraction_alias(const struct wim_inode *inode)
981 struct list_head *next = inode->i_extraction_aliases.next;
982 struct wim_dentry *dentry;
985 dentry = list_entry(next, struct wim_dentry,
986 d_extraction_alias_node);
987 if (dentry_has_short_name(dentry))
990 } while (next != &inode->i_extraction_aliases);
995 * Set or clear FILE_ATTRIBUTE_COMPRESSED if the inherited value is different
996 * from the desired value.
998 * Note that you can NOT override the inherited value of
999 * FILE_ATTRIBUTE_COMPRESSED directly with NtCreateFile().
1002 adjust_compression_attribute(HANDLE h, const struct wim_dentry *dentry,
1003 struct win32_apply_ctx *ctx)
1005 const bool compressed = (dentry->d_inode->i_attributes &
1006 FILE_ATTRIBUTE_COMPRESSED);
1008 if (ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES)
1011 if (!ctx->common.supported_features.compressed_files)
1014 FILE_BASIC_INFORMATION info;
1016 USHORT compression_state;
1018 /* Get current attributes */
1019 status = (*func_NtQueryInformationFile)(h, &ctx->iosb,
1020 &info, sizeof(info),
1021 FileBasicInformation);
1022 if (NT_SUCCESS(status) &&
1023 compressed == !!(info.FileAttributes & FILE_ATTRIBUTE_COMPRESSED))
1025 /* Nothing needs to be done. */
1029 /* Set the new compression state */
1032 compression_state = COMPRESSION_FORMAT_DEFAULT;
1034 compression_state = COMPRESSION_FORMAT_NONE;
1036 status = (*func_NtFsControlFile)(h,
1041 FSCTL_SET_COMPRESSION,
1046 if (NT_SUCCESS(status))
1049 winnt_error(status, L"Can't %s compression attribute on \"%ls\"",
1050 (compressed ? "set" : "clear"), current_path(ctx));
1051 return WIMLIB_ERR_SET_ATTRIBUTES;
1054 /* Try to enable short name support on the target volume. If successful, return
1055 * true. If unsuccessful, issue a warning and return false. */
1057 try_to_enable_short_names(const wchar_t *volume)
1060 FILE_FS_PERSISTENT_VOLUME_INFORMATION info;
1062 DWORD bytesReturned;
1064 h = CreateFile(volume, GENERIC_WRITE,
1065 FILE_SHARE_VALID_FLAGS, NULL, OPEN_EXISTING,
1066 FILE_FLAG_BACKUP_SEMANTICS, NULL);
1067 if (h == INVALID_HANDLE_VALUE)
1070 info.VolumeFlags = 0;
1071 info.FlagMask = PERSISTENT_VOLUME_STATE_SHORT_NAME_CREATION_DISABLED;
1075 bret = DeviceIoControl(h, FSCTL_SET_PERSISTENT_VOLUME_STATE,
1076 &info, sizeof(info), NULL, 0,
1077 &bytesReturned, NULL);
1086 win32_warning(GetLastError(),
1087 L"Failed to enable short name support on %ls",
1093 remove_conflicting_short_name(const struct wim_dentry *dentry, struct win32_apply_ctx *ctx)
1099 size_t bufsize = offsetof(FILE_NAME_INFORMATION, FileName) +
1100 (13 * sizeof(wchar_t));
1101 u8 buf[bufsize] _aligned_attribute(8);
1102 bool retried = false;
1103 FILE_NAME_INFORMATION *info = (FILE_NAME_INFORMATION *)buf;
1105 memset(buf, 0, bufsize);
1107 /* Build the path with the short name. */
1108 name = &ctx->pathbuf.Buffer[ctx->pathbuf.Length / sizeof(wchar_t)];
1109 while (name != ctx->pathbuf.Buffer && *(name - 1) != L'\\')
1111 end = mempcpy(name, dentry->short_name, dentry->short_name_nbytes);
1112 ctx->pathbuf.Length = ((u8 *)end - (u8 *)ctx->pathbuf.Buffer);
1114 /* Open the conflicting file (by short name). */
1115 status = (*func_NtOpenFile)(&h, GENERIC_WRITE | DELETE,
1116 &ctx->attr, &ctx->iosb,
1117 FILE_SHARE_VALID_FLAGS,
1118 FILE_OPEN_REPARSE_POINT | FILE_OPEN_FOR_BACKUP_INTENT);
1119 if (!NT_SUCCESS(status)) {
1120 winnt_warning(status, L"Can't open \"%ls\"", current_path(ctx));
1125 WARNING("Overriding conflicting short name; path=\"%ls\"",
1129 /* Try to remove the short name on the conflicting file. */
1132 status = (*func_NtSetInformationFile)(h, &ctx->iosb, info, bufsize,
1133 FileShortNameInformation);
1135 if (status == STATUS_INVALID_PARAMETER && !retried) {
1137 /* Microsoft forgot to make it possible to remove short names
1138 * until Windows 7. Oops. Use a random short name instead. */
1140 info->FileNameLength = 12 * sizeof(wchar_t);
1141 for (int i = 0; i < 8; i++)
1142 info->FileName[i] = 'A' + (rand() % 26);
1143 info->FileName[8] = L'.';
1144 info->FileName[9] = L'W';
1145 info->FileName[10] = L'L';
1146 info->FileName[11] = L'B';
1147 info->FileName[12] = L'\0';
1153 build_extraction_path(dentry, ctx);
1157 /* Set the short name on the open file @h which has been created at the location
1158 * indicated by @dentry.
1160 * Note that this may add, change, or remove the short name.
1162 * @h must be opened with DELETE access.
1164 * Returns 0 or WIMLIB_ERR_SET_SHORT_NAME. The latter only happens in
1165 * STRICT_SHORT_NAMES mode.
1168 set_short_name(HANDLE h, const struct wim_dentry *dentry,
1169 struct win32_apply_ctx *ctx)
1172 if (!ctx->common.supported_features.short_names)
1176 * Note: The size of the FILE_NAME_INFORMATION buffer must be such that
1177 * FileName contains at least 2 wide characters (4 bytes). Otherwise,
1178 * NtSetInformationFile() will return STATUS_INFO_LENGTH_MISMATCH. This
1179 * is despite the fact that FileNameLength can validly be 0 or 2 bytes,
1180 * with the former case being removing the existing short name if
1181 * present, rather than setting one.
1183 * The null terminator is seemingly optional, but to be safe we include
1184 * space for it and zero all unused space.
1187 size_t bufsize = offsetof(FILE_NAME_INFORMATION, FileName) +
1188 max(dentry->short_name_nbytes, sizeof(wchar_t)) +
1190 u8 buf[bufsize] _aligned_attribute(8);
1191 FILE_NAME_INFORMATION *info = (FILE_NAME_INFORMATION *)buf;
1193 bool tried_to_remove_existing = false;
1195 memset(buf, 0, bufsize);
1197 info->FileNameLength = dentry->short_name_nbytes;
1198 memcpy(info->FileName, dentry->short_name, dentry->short_name_nbytes);
1201 status = (*func_NtSetInformationFile)(h, &ctx->iosb, info, bufsize,
1202 FileShortNameInformation);
1203 if (NT_SUCCESS(status))
1206 if (status == STATUS_SHORT_NAMES_NOT_ENABLED_ON_VOLUME) {
1207 if (dentry->short_name_nbytes == 0)
1209 if (!ctx->tried_to_enable_short_names) {
1213 ctx->tried_to_enable_short_names = true;
1215 ret = win32_get_drive_path(ctx->common.target,
1219 if (try_to_enable_short_names(volume))
1225 * Short names can conflict in several cases:
1227 * - a file being extracted has a short name conflicting with an
1230 * - a file being extracted has a short name conflicting with another
1231 * file being extracted (possible, but shouldn't happen)
1233 * - a file being extracted has a short name that conflicts with the
1234 * automatically generated short name of a file we previously
1235 * extracted, but failed to set the short name for. Sounds unlikely,
1236 * but this actually does happen fairly often on versions of Windows
1237 * prior to Windows 7 because they do not support removing short names
1240 if (unlikely(status == STATUS_OBJECT_NAME_COLLISION) &&
1241 dentry->short_name_nbytes && !tried_to_remove_existing)
1243 tried_to_remove_existing = true;
1244 status = remove_conflicting_short_name(dentry, ctx);
1245 if (NT_SUCCESS(status))
1249 /* By default, failure to set short names is not an error (since short
1250 * names aren't too important anymore...). */
1251 if (!(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_SHORT_NAMES)) {
1252 if (dentry->short_name_nbytes)
1253 ctx->num_set_short_name_failures++;
1255 ctx->num_remove_short_name_failures++;
1259 winnt_error(status, L"Can't set short name on \"%ls\"", current_path(ctx));
1260 return WIMLIB_ERR_SET_SHORT_NAME;
1264 * A wrapper around NtCreateFile() to make it slightly more usable...
1265 * This uses the path currently constructed in ctx->pathbuf.
1267 * Also, we always specify FILE_OPEN_FOR_BACKUP_INTENT and
1268 * FILE_OPEN_REPARSE_POINT.
1271 do_create_file(PHANDLE FileHandle,
1272 ACCESS_MASK DesiredAccess,
1273 PLARGE_INTEGER AllocationSize,
1274 ULONG FileAttributes,
1275 ULONG CreateDisposition,
1276 ULONG CreateOptions,
1277 struct win32_apply_ctx *ctx)
1279 return (*func_NtCreateFile)(FileHandle,
1285 FILE_SHARE_VALID_FLAGS,
1288 FILE_OPEN_FOR_BACKUP_INTENT |
1289 FILE_OPEN_REPARSE_POINT,
1294 /* Like do_create_file(), but builds the extraction path of the @dentry first.
1297 create_file(PHANDLE FileHandle,
1298 ACCESS_MASK DesiredAccess,
1299 PLARGE_INTEGER AllocationSize,
1300 ULONG FileAttributes,
1301 ULONG CreateDisposition,
1302 ULONG CreateOptions,
1303 const struct wim_dentry *dentry,
1304 struct win32_apply_ctx *ctx)
1306 build_extraction_path(dentry, ctx);
1307 return do_create_file(FileHandle,
1317 delete_file_or_stream(struct win32_apply_ctx *ctx)
1321 FILE_DISPOSITION_INFORMATION disposition_info;
1322 FILE_BASIC_INFORMATION basic_info;
1323 bool retried = false;
1325 status = do_create_file(&h,
1330 FILE_NON_DIRECTORY_FILE,
1332 if (unlikely(!NT_SUCCESS(status))) {
1333 winnt_error(status, L"Can't open \"%ls\" for deletion",
1335 return WIMLIB_ERR_OPEN;
1339 disposition_info.DoDeleteFile = TRUE;
1340 status = (*func_NtSetInformationFile)(h, &ctx->iosb,
1342 sizeof(disposition_info),
1343 FileDispositionInformation);
1345 if (likely(NT_SUCCESS(status)))
1348 if (status == STATUS_CANNOT_DELETE && !retried) {
1349 /* Clear file attributes and try again. This is necessary for
1350 * FILE_ATTRIBUTE_READONLY files. */
1351 status = do_create_file(&h,
1352 FILE_WRITE_ATTRIBUTES | DELETE,
1356 FILE_NON_DIRECTORY_FILE,
1358 if (!NT_SUCCESS(status)) {
1360 L"Can't open \"%ls\" to reset attributes",
1362 return WIMLIB_ERR_OPEN;
1364 memset(&basic_info, 0, sizeof(basic_info));
1365 basic_info.FileAttributes = FILE_ATTRIBUTE_NORMAL;
1366 status = (*func_NtSetInformationFile)(h, &ctx->iosb,
1369 FileBasicInformation);
1370 if (!NT_SUCCESS(status)) {
1372 L"Can't reset file attributes on \"%ls\"",
1375 return WIMLIB_ERR_SET_ATTRIBUTES;
1380 winnt_error(status, L"Can't delete \"%ls\"", current_path(ctx));
1381 return WIMLIB_ERR_OPEN;
1385 * Create a nondirectory file or named data stream at the current path,
1386 * superseding any that already exists at that path. If successful, return an
1387 * open handle to the file or named data stream.
1390 supersede_file_or_stream(struct win32_apply_ctx *ctx, HANDLE *h_ret)
1393 bool retried = false;
1395 /* FILE_ATTRIBUTE_SYSTEM is needed to ensure that
1396 * FILE_ATTRIBUTE_ENCRYPTED doesn't get set before we want it to be. */
1398 status = do_create_file(h_ret,
1399 GENERIC_READ | GENERIC_WRITE | DELETE,
1401 FILE_ATTRIBUTE_SYSTEM,
1403 FILE_NON_DIRECTORY_FILE,
1405 if (likely(NT_SUCCESS(status)))
1408 /* STATUS_OBJECT_NAME_COLLISION means that the file or stream already
1409 * exists. Delete the existing file or stream, then try again.
1411 * Note: we don't use FILE_OVERWRITE_IF or FILE_SUPERSEDE because of
1412 * problems with certain file attributes, especially
1413 * FILE_ATTRIBUTE_ENCRYPTED. FILE_SUPERSEDE is also broken in the
1414 * Windows PE ramdisk. */
1415 if (status == STATUS_OBJECT_NAME_COLLISION && !retried) {
1416 int ret = delete_file_or_stream(ctx);
1422 winnt_error(status, L"Can't create \"%ls\"", current_path(ctx));
1423 return WIMLIB_ERR_OPEN;
1427 * Create empty named data streams for the specified file, if there are any.
1429 * Since these won't have blob descriptors, they won't show up in the call to
1430 * extract_blob_list(). Hence the need for the special case.
1433 create_empty_named_data_streams(const struct wim_dentry *dentry,
1434 struct win32_apply_ctx *ctx)
1436 const struct wim_inode *inode = dentry->d_inode;
1437 bool path_modified = false;
1440 if (!ctx->common.supported_features.named_data_streams)
1443 for (unsigned i = 0; i < inode->i_num_streams; i++) {
1444 const struct wim_inode_stream *strm = &inode->i_streams[i];
1447 if (!stream_is_named_data_stream(strm) ||
1448 stream_blob_resolved(strm) != NULL)
1451 build_extraction_path_with_ads(dentry, ctx,
1453 utf16le_len_chars(strm->stream_name));
1454 path_modified = true;
1455 ret = supersede_file_or_stream(ctx, &h);
1460 /* Restore the path to the dentry itself */
1462 build_extraction_path(dentry, ctx);
1467 * Creates the directory named by @dentry, or uses an existing directory at that
1468 * location. If necessary, sets the short name and/or fixes compression and
1469 * encryption attributes.
1471 * Returns 0, WIMLIB_ERR_MKDIR, or WIMLIB_ERR_SET_SHORT_NAME.
1474 create_directory(const struct wim_dentry *dentry, struct win32_apply_ctx *ctx)
1481 /* DELETE is needed for set_short_name(); GENERIC_READ and GENERIC_WRITE
1482 * are needed for adjust_compression_attribute(). */
1483 perms = GENERIC_READ | GENERIC_WRITE;
1484 if (!dentry_is_root(dentry))
1487 /* FILE_ATTRIBUTE_SYSTEM is needed to ensure that
1488 * FILE_ATTRIBUTE_ENCRYPTED doesn't get set before we want it to be. */
1489 status = create_file(&h, perms, NULL, FILE_ATTRIBUTE_SYSTEM,
1490 FILE_OPEN_IF, FILE_DIRECTORY_FILE, dentry, ctx);
1491 if (!NT_SUCCESS(status)) {
1492 winnt_error(status, L"Can't create directory \"%ls\"",
1494 return WIMLIB_ERR_MKDIR;
1497 if (ctx->iosb.Information == FILE_OPENED) {
1498 /* If we opened an existing directory, try to clear its file
1499 * attributes. As far as I know, this only actually makes a
1500 * difference in the case where a FILE_ATTRIBUTE_READONLY
1501 * directory has a named data stream which needs to be
1502 * extracted. You cannot create a named data stream of such a
1503 * directory, even though this contradicts Microsoft's
1504 * documentation for FILE_ATTRIBUTE_READONLY which states it is
1505 * not honored for directories! */
1506 FILE_BASIC_INFORMATION basic_info = { .FileAttributes = FILE_ATTRIBUTE_NORMAL };
1507 (*func_NtSetInformationFile)(h, &ctx->iosb, &basic_info,
1508 sizeof(basic_info), FileBasicInformation);
1511 if (!dentry_is_root(dentry)) {
1512 ret = set_short_name(h, dentry, ctx);
1517 ret = adjust_compression_attribute(h, dentry, ctx);
1524 * Create all the directories being extracted, other than the target directory
1527 * Note: we don't honor directory hard links. However, we don't allow them to
1528 * exist in WIM images anyway (see inode_fixup.c).
1531 create_directories(struct list_head *dentry_list,
1532 struct win32_apply_ctx *ctx)
1534 const struct wim_dentry *dentry;
1537 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
1539 if (!(dentry->d_inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY))
1542 /* Note: Here we include files with
1543 * FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_REPARSE_POINT, but we
1544 * wait until later to actually set the reparse data. */
1546 ret = create_directory(dentry, ctx);
1549 ret = create_empty_named_data_streams(dentry, ctx);
1551 ret = check_apply_error(dentry, ctx, ret);
1555 ret = report_file_created(&ctx->common);
1563 * Creates the nondirectory file named by @dentry.
1565 * On success, returns an open handle to the file in @h_ret, with GENERIC_READ,
1566 * GENERIC_WRITE, and DELETE access. Also, the path to the file will be saved
1567 * in ctx->pathbuf. On failure, returns an error code.
1570 create_nondirectory_inode(HANDLE *h_ret, const struct wim_dentry *dentry,
1571 struct win32_apply_ctx *ctx)
1576 build_extraction_path(dentry, ctx);
1578 ret = supersede_file_or_stream(ctx, &h);
1582 ret = adjust_compression_attribute(h, dentry, ctx);
1586 ret = create_empty_named_data_streams(dentry, ctx);
1599 /* Creates a hard link at the location named by @dentry to the file represented
1600 * by the open handle @h. Or, if the target volume does not support hard links,
1601 * create a separate file instead. */
1603 create_link(HANDLE h, const struct wim_dentry *dentry,
1604 struct win32_apply_ctx *ctx)
1606 if (ctx->common.supported_features.hard_links) {
1608 build_extraction_path(dentry, ctx);
1610 size_t bufsize = offsetof(FILE_LINK_INFORMATION, FileName) +
1611 ctx->pathbuf.Length + sizeof(wchar_t);
1612 u8 buf[bufsize] _aligned_attribute(8);
1613 FILE_LINK_INFORMATION *info = (FILE_LINK_INFORMATION *)buf;
1616 info->ReplaceIfExists = TRUE;
1617 info->RootDirectory = ctx->attr.RootDirectory;
1618 info->FileNameLength = ctx->pathbuf.Length;
1619 memcpy(info->FileName, ctx->pathbuf.Buffer, ctx->pathbuf.Length);
1620 info->FileName[info->FileNameLength / 2] = L'\0';
1622 /* Note: the null terminator isn't actually necessary,
1623 * but if you don't add the extra character, you get
1624 * STATUS_INFO_LENGTH_MISMATCH when FileNameLength
1625 * happens to be 2 */
1627 status = (*func_NtSetInformationFile)(h, &ctx->iosb,
1629 FileLinkInformation);
1630 if (NT_SUCCESS(status))
1632 winnt_error(status, L"Failed to create link \"%ls\"",
1634 return WIMLIB_ERR_LINK;
1639 ret = create_nondirectory_inode(&h2, dentry, ctx);
1643 (*func_NtClose)(h2);
1648 /* Given an inode (represented by the open handle @h) for which one link has
1649 * been created (named by @first_dentry), create the other links.
1651 * Or, if the target volume does not support hard links, create separate files.
1653 * Note: This uses ctx->pathbuf and does not reset it.
1656 create_links(HANDLE h, const struct wim_dentry *first_dentry,
1657 struct win32_apply_ctx *ctx)
1659 const struct wim_inode *inode;
1660 const struct list_head *next;
1661 const struct wim_dentry *dentry;
1664 inode = first_dentry->d_inode;
1665 next = inode->i_extraction_aliases.next;
1667 dentry = list_entry(next, struct wim_dentry,
1668 d_extraction_alias_node);
1669 if (dentry != first_dentry) {
1670 ret = create_link(h, dentry, ctx);
1675 } while (next != &inode->i_extraction_aliases);
1679 /* Create a nondirectory file, including all links. */
1681 create_nondirectory(struct wim_inode *inode, struct win32_apply_ctx *ctx)
1683 struct wim_dentry *first_dentry;
1687 first_dentry = first_extraction_alias(inode);
1689 /* Create first link. */
1690 ret = create_nondirectory_inode(&h, first_dentry, ctx);
1694 /* Set short name. */
1695 ret = set_short_name(h, first_dentry, ctx);
1697 /* Create additional links, OR if hard links are not supported just
1698 * create more files. */
1700 ret = create_links(h, first_dentry, ctx);
1702 /* "WIMBoot" extraction: set external backing by the WIM file if needed. */
1703 if (!ret && unlikely(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_WIMBOOT))
1704 ret = set_external_backing(h, inode, ctx);
1710 /* Create all the nondirectory files being extracted, including all aliases
1713 create_nondirectories(struct list_head *dentry_list, struct win32_apply_ctx *ctx)
1715 struct wim_dentry *dentry;
1716 struct wim_inode *inode;
1719 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
1720 inode = dentry->d_inode;
1721 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)
1723 /* Call create_nondirectory() only once per inode */
1724 if (dentry == inode_first_extraction_dentry(inode)) {
1725 ret = create_nondirectory(inode, ctx);
1726 ret = check_apply_error(dentry, ctx, ret);
1730 ret = report_file_created(&ctx->common);
1738 close_handles(struct win32_apply_ctx *ctx)
1740 for (unsigned i = 0; i < ctx->num_open_handles; i++)
1741 (*func_NtClose)(ctx->open_handles[i]);
1744 /* Prepare to read the next blob, which has size @blob_size, into an in-memory
1747 prepare_data_buffer(struct win32_apply_ctx *ctx, u64 blob_size)
1749 if (blob_size > ctx->data_buffer_size) {
1750 /* Larger buffer needed. */
1752 if ((size_t)blob_size != blob_size)
1754 new_buffer = REALLOC(ctx->data_buffer, blob_size);
1757 ctx->data_buffer = new_buffer;
1758 ctx->data_buffer_size = blob_size;
1760 /* On the first call this changes data_buffer_ptr from NULL, which tells
1761 * extract_chunk() that the data buffer needs to be filled while reading
1762 * the stream data. */
1763 ctx->data_buffer_ptr = ctx->data_buffer;
1768 begin_extract_blob_instance(const struct blob_descriptor *blob,
1769 struct wim_dentry *dentry,
1770 const struct wim_inode_stream *strm,
1771 struct win32_apply_ctx *ctx)
1773 FILE_ALLOCATION_INFORMATION alloc_info;
1777 if (unlikely(strm->stream_type == STREAM_TYPE_REPARSE_POINT)) {
1778 /* We can't write the reparse point stream directly; we must set
1779 * it with FSCTL_SET_REPARSE_POINT, which requires that all the
1780 * data be available. So, stage the data in a buffer. */
1781 if (!prepare_data_buffer(ctx, blob->size))
1782 return WIMLIB_ERR_NOMEM;
1783 list_add_tail(&dentry->tmp_list, &ctx->reparse_dentries);
1787 if (unlikely(strm->stream_type == STREAM_TYPE_EFSRPC_RAW_DATA)) {
1788 /* We can't write encrypted files directly; we must use
1789 * WriteEncryptedFileRaw(), which requires providing the data
1790 * through a callback function. This can't easily be combined
1791 * with our own callback-based approach.
1793 * The current workaround is to simply read the blob into memory
1794 * and write the encrypted file from that.
1796 * TODO: This isn't sufficient for extremely large encrypted
1797 * files. Perhaps we should create an extra thread to write
1799 if (!prepare_data_buffer(ctx, blob->size))
1800 return WIMLIB_ERR_NOMEM;
1801 list_add_tail(&dentry->tmp_list, &ctx->encrypted_dentries);
1805 /* It's a data stream (may be unnamed or named). */
1806 wimlib_assert(strm->stream_type == STREAM_TYPE_DATA);
1808 if (ctx->num_open_handles == MAX_OPEN_FILES) {
1809 /* XXX: Fix this. But because of the checks in
1810 * extract_blob_list(), this can now only happen on a filesystem
1811 * that does not support hard links. */
1812 ERROR("Can't extract data: too many open files!");
1813 return WIMLIB_ERR_UNSUPPORTED;
1817 if (unlikely(stream_is_named(strm))) {
1818 build_extraction_path_with_ads(dentry, ctx,
1820 utf16le_len_chars(strm->stream_name));
1822 build_extraction_path(dentry, ctx);
1826 /* Open a new handle */
1827 status = do_create_file(&h,
1828 FILE_WRITE_DATA | SYNCHRONIZE,
1829 NULL, 0, FILE_OPEN_IF,
1830 FILE_SEQUENTIAL_ONLY |
1831 FILE_SYNCHRONOUS_IO_NONALERT,
1833 if (!NT_SUCCESS(status)) {
1834 winnt_error(status, L"Can't open \"%ls\" for writing",
1836 return WIMLIB_ERR_OPEN;
1839 ctx->open_handles[ctx->num_open_handles++] = h;
1841 /* Allocate space for the data. */
1842 alloc_info.AllocationSize.QuadPart = blob->size;
1843 (*func_NtSetInformationFile)(h, &ctx->iosb,
1844 &alloc_info, sizeof(alloc_info),
1845 FileAllocationInformation);
1849 /* Set the reparse data @rpbuf of length @rpbuflen on the extracted file
1850 * corresponding to the WIM dentry @dentry. */
1852 do_set_reparse_data(const struct wim_dentry *dentry,
1853 const void *rpbuf, u16 rpbuflen,
1854 struct win32_apply_ctx *ctx)
1859 status = create_file(&h, GENERIC_WRITE, NULL,
1860 0, FILE_OPEN, 0, dentry, ctx);
1861 if (!NT_SUCCESS(status))
1864 status = (*func_NtFsControlFile)(h, NULL, NULL, NULL,
1865 &ctx->iosb, FSCTL_SET_REPARSE_POINT,
1866 (void *)rpbuf, rpbuflen,
1870 if (NT_SUCCESS(status))
1873 /* On Windows, by default only the Administrator can create symbolic
1874 * links for some reason. By default we just issue a warning if this
1875 * appears to be the problem. Use WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS
1876 * to get a hard error. */
1877 if (!(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS)
1878 && (status == STATUS_PRIVILEGE_NOT_HELD ||
1879 status == STATUS_ACCESS_DENIED)
1880 && (dentry->d_inode->i_reparse_tag == WIM_IO_REPARSE_TAG_SYMLINK ||
1881 dentry->d_inode->i_reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT))
1883 WARNING("Can't create symbolic link \"%ls\"! \n"
1884 " (Need Administrator rights, or at least "
1886 " SeCreateSymbolicLink privilege.)",
1892 winnt_error(status, L"Can't set reparse data on \"%ls\"",
1894 return WIMLIB_ERR_SET_REPARSE_DATA;
1897 /* Given a Windows NT namespace path, such as \??\e:\Windows\System32, return a
1898 * pointer to the suffix of the path that begins with the device directly, such
1899 * as e:\Windows\System32. */
1900 static const wchar_t *
1901 skip_nt_toplevel_component(const wchar_t *path, size_t path_nchars)
1903 static const wchar_t * const dirs[] = {
1908 size_t first_dir_len = 0;
1909 const wchar_t * const end = path + path_nchars;
1911 for (size_t i = 0; i < ARRAY_LEN(dirs); i++) {
1912 size_t len = wcslen(dirs[i]);
1913 if (len <= (end - path) && !wcsnicmp(path, dirs[i], len)) {
1914 first_dir_len = len;
1918 if (first_dir_len == 0)
1920 path += first_dir_len;
1921 while (path != end && *path == L'\\')
1926 /* Given a Windows NT namespace path, such as \??\e:\Windows\System32, return a
1927 * pointer to the suffix of the path that is device-relative, such as
1930 * The path has an explicit length and is not necessarily null terminated.
1932 * If the path just something like \??\e: then the returned pointer will point
1933 * just past the colon. In this case the length of the result will be 0
1935 static const wchar_t *
1936 get_device_relative_path(const wchar_t *path, size_t path_nchars)
1938 const wchar_t * const orig_path = path;
1939 const wchar_t * const end = path + path_nchars;
1941 path = skip_nt_toplevel_component(path, path_nchars);
1942 if (path == orig_path)
1945 path = wmemchr(path, L'\\', (end - path));
1950 } while (path != end && *path == L'\\');
1955 * Given a reparse point buffer for a symbolic link or junction, adjust its
1956 * contents so that the target of the link is consistent with the new location
1960 try_rpfix(u8 *rpbuf, u16 *rpbuflen_p, struct win32_apply_ctx *ctx)
1962 struct reparse_data rpdata;
1963 size_t orig_subst_name_nchars;
1964 const wchar_t *relpath;
1965 size_t relpath_nchars;
1966 size_t target_ntpath_nchars;
1967 size_t fixed_subst_name_nchars;
1968 const wchar_t *fixed_print_name;
1969 size_t fixed_print_name_nchars;
1971 if (parse_reparse_data(rpbuf, *rpbuflen_p, &rpdata)) {
1972 /* Do nothing if the reparse data is invalid. */
1976 if (rpdata.rptag == WIM_IO_REPARSE_TAG_SYMLINK &&
1977 (rpdata.rpflags & SYMBOLIC_LINK_RELATIVE))
1979 /* Do nothing if it's a relative symbolic link. */
1983 /* Build the new substitute name from the NT namespace path to the
1984 * target directory, then a path separator, then the "device relative"
1985 * part of the old substitute name. */
1987 orig_subst_name_nchars = rpdata.substitute_name_nbytes / sizeof(wchar_t);
1989 relpath = get_device_relative_path(rpdata.substitute_name,
1990 orig_subst_name_nchars);
1991 relpath_nchars = orig_subst_name_nchars -
1992 (relpath - rpdata.substitute_name);
1994 target_ntpath_nchars = ctx->target_ntpath.Length / sizeof(wchar_t);
1996 fixed_subst_name_nchars = target_ntpath_nchars;
1998 fixed_subst_name_nchars += 1 + relpath_nchars;
1999 wchar_t fixed_subst_name[fixed_subst_name_nchars];
2001 wmemcpy(fixed_subst_name, ctx->target_ntpath.Buffer,
2002 target_ntpath_nchars);
2003 if (relpath_nchars) {
2004 fixed_subst_name[target_ntpath_nchars] = L'\\';
2005 wmemcpy(&fixed_subst_name[target_ntpath_nchars + 1],
2006 relpath, relpath_nchars);
2008 /* Doesn't need to be null-terminated. */
2010 /* Print name should be Win32, but not all NT names can even be
2011 * translated to Win32 names. But we can at least delete the top-level
2012 * directory, such as \??\, and this will have the expected result in
2013 * the usual case. */
2014 fixed_print_name = skip_nt_toplevel_component(fixed_subst_name,
2015 fixed_subst_name_nchars);
2016 fixed_print_name_nchars = fixed_subst_name_nchars - (fixed_print_name -
2019 rpdata.substitute_name = fixed_subst_name;
2020 rpdata.substitute_name_nbytes = fixed_subst_name_nchars * sizeof(wchar_t);
2021 rpdata.print_name = (wchar_t *)fixed_print_name;
2022 rpdata.print_name_nbytes = fixed_print_name_nchars * sizeof(wchar_t);
2023 make_reparse_buffer(&rpdata, rpbuf, rpbuflen_p);
2026 /* Sets reparse data on the specified file. This handles "fixing" the targets
2027 * of absolute symbolic links and junctions if WIMLIB_EXTRACT_FLAG_RPFIX was
2030 set_reparse_data(const struct wim_dentry *dentry,
2031 const void *_rpbuf, u16 rpbuflen, struct win32_apply_ctx *ctx)
2033 const struct wim_inode *inode = dentry->d_inode;
2034 const void *rpbuf = _rpbuf;
2036 if ((ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX)
2037 && !inode->i_not_rpfixed
2038 && (inode->i_reparse_tag == WIM_IO_REPARSE_TAG_SYMLINK ||
2039 inode->i_reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT))
2041 memcpy(&ctx->rpfixbuf, _rpbuf, rpbuflen);
2042 try_rpfix((u8 *)&ctx->rpfixbuf, &rpbuflen, ctx);
2043 rpbuf = &ctx->rpfixbuf;
2045 return do_set_reparse_data(dentry, rpbuf, rpbuflen, ctx);
2049 /* Import the next block of raw encrypted data */
2051 import_encrypted_data(PBYTE pbData, PVOID pvCallbackContext, PULONG Length)
2053 struct win32_apply_ctx *ctx = pvCallbackContext;
2056 copy_len = min(ctx->encrypted_size - ctx->encrypted_offset, *Length);
2057 memcpy(pbData, &ctx->data_buffer[ctx->encrypted_offset], copy_len);
2058 ctx->encrypted_offset += copy_len;
2060 return ERROR_SUCCESS;
2064 * Write the raw encrypted data to the already-created file (or directory)
2065 * corresponding to @dentry.
2067 * The raw encrypted data is provided in ctx->data_buffer, and its size is
2068 * ctx->encrypted_size.
2070 * This function may close the target directory, in which case the caller needs
2071 * to re-open it if needed.
2074 extract_encrypted_file(const struct wim_dentry *dentry,
2075 struct win32_apply_ctx *ctx)
2082 /* Temporarily build a Win32 path for OpenEncryptedFileRaw() */
2083 build_win32_extraction_path(dentry, ctx);
2085 flags = CREATE_FOR_IMPORT | OVERWRITE_HIDDEN;
2086 if (dentry->d_inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)
2087 flags |= CREATE_FOR_DIR;
2091 err = OpenEncryptedFileRaw(ctx->pathbuf.Buffer, flags, &rawctx);
2092 if (err == ERROR_SHARING_VIOLATION && !retried) {
2093 /* This can be caused by the handle we have open to the target
2094 * directory. Try closing it temporarily. */
2095 close_target_directory(ctx);
2100 /* Restore the NT namespace path */
2101 build_extraction_path(dentry, ctx);
2103 if (err != ERROR_SUCCESS) {
2104 win32_error(err, L"Can't open \"%ls\" for encrypted import",
2106 return WIMLIB_ERR_OPEN;
2109 ctx->encrypted_offset = 0;
2111 err = WriteEncryptedFileRaw(import_encrypted_data, ctx, rawctx);
2113 CloseEncryptedFileRaw(rawctx);
2115 if (err != ERROR_SUCCESS) {
2116 win32_error(err, L"Can't import encrypted file \"%ls\"",
2118 return WIMLIB_ERR_WRITE;
2124 /* Called when starting to read a blob for extraction on Windows */
2126 begin_extract_blob(struct blob_descriptor *blob, void *_ctx)
2128 struct win32_apply_ctx *ctx = _ctx;
2129 const struct blob_extraction_target *targets = blob_extraction_targets(blob);
2132 ctx->num_open_handles = 0;
2133 ctx->data_buffer_ptr = NULL;
2134 INIT_LIST_HEAD(&ctx->reparse_dentries);
2135 INIT_LIST_HEAD(&ctx->encrypted_dentries);
2137 for (u32 i = 0; i < blob->out_refcnt; i++) {
2138 const struct wim_inode *inode = targets[i].inode;
2139 const struct wim_inode_stream *strm = targets[i].stream;
2140 struct wim_dentry *dentry;
2142 /* A copy of the blob needs to be extracted to @inode. */
2144 if (ctx->common.supported_features.hard_links) {
2145 dentry = inode_first_extraction_dentry(inode);
2146 ret = begin_extract_blob_instance(blob, dentry, strm, ctx);
2147 ret = check_apply_error(dentry, ctx, ret);
2151 /* Hard links not supported. Extract the blob
2152 * separately to each alias of the inode. */
2153 struct list_head *next;
2155 next = inode->i_extraction_aliases.next;
2157 dentry = list_entry(next, struct wim_dentry,
2158 d_extraction_alias_node);
2159 ret = begin_extract_blob_instance(blob, dentry, strm, ctx);
2160 ret = check_apply_error(dentry, ctx, ret);
2164 } while (next != &inode->i_extraction_aliases);
2175 /* Called when the next chunk of a blob has been read for extraction on Windows
2178 extract_chunk(const void *chunk, size_t size, void *_ctx)
2180 struct win32_apply_ctx *ctx = _ctx;
2182 /* Write the data chunk to each open handle */
2183 for (unsigned i = 0; i < ctx->num_open_handles; i++) {
2184 u8 *bufptr = (u8 *)chunk;
2185 size_t bytes_remaining = size;
2187 while (bytes_remaining) {
2188 ULONG count = min(0xFFFFFFFF, bytes_remaining);
2190 status = (*func_NtWriteFile)(ctx->open_handles[i],
2192 &ctx->iosb, bufptr, count,
2194 if (!NT_SUCCESS(status)) {
2195 winnt_error(status, L"Error writing data to target volume");
2196 return WIMLIB_ERR_WRITE;
2198 bufptr += ctx->iosb.Information;
2199 bytes_remaining -= ctx->iosb.Information;
2203 /* Copy the data chunk into the buffer (if needed) */
2204 if (ctx->data_buffer_ptr)
2205 ctx->data_buffer_ptr = mempcpy(ctx->data_buffer_ptr,
2210 /* Called when a blob has been fully read for extraction on Windows */
2212 end_extract_blob(struct blob_descriptor *blob, int status, void *_ctx)
2214 struct win32_apply_ctx *ctx = _ctx;
2216 const struct wim_dentry *dentry;
2223 if (likely(!ctx->data_buffer_ptr))
2226 if (!list_empty(&ctx->reparse_dentries)) {
2227 if (blob->size > REPARSE_DATA_MAX_SIZE) {
2228 dentry = list_first_entry(&ctx->reparse_dentries,
2229 struct wim_dentry, tmp_list);
2230 build_extraction_path(dentry, ctx);
2231 ERROR("Reparse data of \"%ls\" has size "
2232 "%"PRIu64" bytes (exceeds %u bytes)",
2233 current_path(ctx), blob->size,
2234 REPARSE_DATA_MAX_SIZE);
2235 ret = WIMLIB_ERR_INVALID_REPARSE_DATA;
2236 return check_apply_error(dentry, ctx, ret);
2238 /* In the WIM format, reparse point streams are just the reparse
2239 * data and omit the header. But we can reconstruct the header.
2241 memcpy(ctx->rpbuf.rpdata, ctx->data_buffer, blob->size);
2242 ctx->rpbuf.rpdatalen = blob->size;
2243 ctx->rpbuf.rpreserved = 0;
2244 list_for_each_entry(dentry, &ctx->reparse_dentries, tmp_list) {
2245 ctx->rpbuf.rptag = dentry->d_inode->i_reparse_tag;
2246 ret = set_reparse_data(dentry, &ctx->rpbuf,
2247 blob->size + REPARSE_DATA_OFFSET,
2249 ret = check_apply_error(dentry, ctx, ret);
2255 if (!list_empty(&ctx->encrypted_dentries)) {
2256 ctx->encrypted_size = blob->size;
2257 list_for_each_entry(dentry, &ctx->encrypted_dentries, tmp_list) {
2258 ret = extract_encrypted_file(dentry, ctx);
2259 ret = check_apply_error(dentry, ctx, ret);
2262 /* Re-open the target directory if needed. */
2263 ret = open_target_directory(ctx);
2272 /* Attributes that can't be set directly */
2273 #define SPECIAL_ATTRIBUTES \
2274 (FILE_ATTRIBUTE_REPARSE_POINT | \
2275 FILE_ATTRIBUTE_DIRECTORY | \
2276 FILE_ATTRIBUTE_ENCRYPTED | \
2277 FILE_ATTRIBUTE_SPARSE_FILE | \
2278 FILE_ATTRIBUTE_COMPRESSED)
2280 /* Set the security descriptor @desc, of @desc_size bytes, on the file with open
2283 set_security_descriptor(HANDLE h, const void *_desc,
2284 size_t desc_size, struct win32_apply_ctx *ctx)
2286 SECURITY_INFORMATION info;
2288 SECURITY_DESCRIPTOR_RELATIVE *desc;
2291 * Ideally, we would just pass in the security descriptor buffer as-is.
2292 * But it turns out that Windows can mess up the security descriptor
2293 * even when using the low-level NtSetSecurityObject() function:
2295 * - Windows will clear SE_DACL_AUTO_INHERITED if it is set in the
2296 * passed buffer. To actually get Windows to set
2297 * SE_DACL_AUTO_INHERITED, the application must set the non-persistent
2298 * flag SE_DACL_AUTO_INHERIT_REQ. As usual, Microsoft didn't bother
2299 * to properly document either of these flags. It's unclear how
2300 * important SE_DACL_AUTO_INHERITED actually is, but to be safe we use
2301 * the SE_DACL_AUTO_INHERIT_REQ workaround to set it if needed.
2303 * - The above also applies to the equivalent SACL flags,
2304 * SE_SACL_AUTO_INHERITED and SE_SACL_AUTO_INHERIT_REQ.
2306 * - If the application says that it's setting
2307 * DACL_SECURITY_INFORMATION, then Windows sets SE_DACL_PRESENT in the
2308 * resulting security descriptor, even if the security descriptor the
2309 * application provided did not have a DACL. This seems to be
2310 * unavoidable, since omitting DACL_SECURITY_INFORMATION would cause a
2311 * default DACL to remain. Fortunately, this behavior seems harmless,
2312 * since the resulting DACL will still be "null" --- but it will be
2313 * "the other representation of null".
2315 * - The above also applies to SACL_SECURITY_INFORMATION and
2316 * SE_SACL_PRESENT. Again, it's seemingly unavoidable but "harmless"
2317 * that Windows changes the representation of a "null SACL".
2319 if (likely(desc_size <= STACK_MAX)) {
2320 desc = alloca(desc_size);
2322 desc = MALLOC(desc_size);
2324 return STATUS_NO_MEMORY;
2327 memcpy(desc, _desc, desc_size);
2329 if (likely(desc_size >= 4)) {
2331 if (desc->Control & SE_DACL_AUTO_INHERITED)
2332 desc->Control |= SE_DACL_AUTO_INHERIT_REQ;
2334 if (desc->Control & SE_SACL_AUTO_INHERITED)
2335 desc->Control |= SE_SACL_AUTO_INHERIT_REQ;
2339 * More API insanity. We want to set the entire security descriptor
2340 * as-is. But all available APIs require specifying the specific parts
2341 * of the security descriptor being set. Especially annoying is that
2342 * mandatory integrity labels are part of the SACL, but they aren't set
2343 * with SACL_SECURITY_INFORMATION. Instead, applications must also
2344 * specify LABEL_SECURITY_INFORMATION (Windows Vista, Windows 7) or
2345 * BACKUP_SECURITY_INFORMATION (Windows 8). But at least older versions
2346 * of Windows don't error out if you provide these newer flags...
2348 * Also, if the process isn't running as Administrator, then it probably
2349 * doesn't have SE_RESTORE_PRIVILEGE. In this case, it will always get
2350 * the STATUS_PRIVILEGE_NOT_HELD error by trying to set the SACL, even
2351 * if the security descriptor it provided did not have a SACL. By
2352 * default, in this case we try to recover and set as much of the
2353 * security descriptor as possible --- potentially excluding the DACL, and
2354 * even the owner, as well as the SACL.
2357 info = OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
2358 DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION |
2359 LABEL_SECURITY_INFORMATION | BACKUP_SECURITY_INFORMATION;
2363 * It's also worth noting that SetFileSecurity() is unusable because it
2364 * doesn't request "backup semantics" when it opens the file internally.
2365 * NtSetSecurityObject() seems to be the best function to use in backup
2366 * applications. (SetSecurityInfo() should also work, but it's harder
2367 * to use and must call NtSetSecurityObject() internally anyway.
2368 * BackupWrite() is theoretically usable as well, but it's inflexible
2369 * and poorly documented.)
2373 status = (*func_NtSetSecurityObject)(h, info, desc);
2374 if (NT_SUCCESS(status))
2375 goto out_maybe_free_desc;
2377 /* Failed to set the requested parts of the security descriptor. If the
2378 * error was permissions-related, try to set fewer parts of the security
2379 * descriptor, unless WIMLIB_EXTRACT_FLAG_STRICT_ACLS is enabled. */
2380 if ((status == STATUS_PRIVILEGE_NOT_HELD ||
2381 status == STATUS_ACCESS_DENIED) &&
2382 !(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_ACLS))
2384 if (info & SACL_SECURITY_INFORMATION) {
2385 info &= ~(SACL_SECURITY_INFORMATION |
2386 LABEL_SECURITY_INFORMATION |
2387 BACKUP_SECURITY_INFORMATION);
2388 ctx->partial_security_descriptors++;
2391 if (info & DACL_SECURITY_INFORMATION) {
2392 info &= ~DACL_SECURITY_INFORMATION;
2395 if (info & OWNER_SECURITY_INFORMATION) {
2396 info &= ~OWNER_SECURITY_INFORMATION;
2399 /* Nothing left except GROUP, and if we removed it we
2400 * wouldn't have anything at all. */
2403 /* No part of the security descriptor could be set, or
2404 * WIMLIB_EXTRACT_FLAG_STRICT_ACLS is enabled and the full security
2405 * descriptor could not be set. */
2406 if (!(info & SACL_SECURITY_INFORMATION))
2407 ctx->partial_security_descriptors--;
2408 ctx->no_security_descriptors++;
2410 out_maybe_free_desc:
2411 if (unlikely(desc_size > STACK_MAX))
2416 /* Set metadata on the open file @h from the WIM inode @inode. */
2418 do_apply_metadata_to_file(HANDLE h, const struct wim_inode *inode,
2419 struct win32_apply_ctx *ctx)
2421 FILE_BASIC_INFORMATION info;
2424 /* Set security descriptor if present and not in NO_ACLS mode */
2425 if (inode->i_security_id >= 0 &&
2426 !(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_NO_ACLS))
2428 const struct wim_security_data *sd;
2432 sd = wim_get_current_security_data(ctx->common.wim);
2433 desc = sd->descriptors[inode->i_security_id];
2434 desc_size = sd->sizes[inode->i_security_id];
2436 status = set_security_descriptor(h, desc, desc_size, ctx);
2437 if (!NT_SUCCESS(status) &&
2438 (ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_ACLS))
2441 L"Can't set security descriptor on \"%ls\"",
2443 return WIMLIB_ERR_SET_SECURITY;
2447 /* Set attributes and timestamps */
2448 info.CreationTime.QuadPart = inode->i_creation_time;
2449 info.LastAccessTime.QuadPart = inode->i_last_access_time;
2450 info.LastWriteTime.QuadPart = inode->i_last_write_time;
2451 info.ChangeTime.QuadPart = 0;
2452 if (ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES) {
2453 info.FileAttributes = FILE_ATTRIBUTE_NORMAL;
2455 info.FileAttributes = inode->i_attributes & ~SPECIAL_ATTRIBUTES;
2456 if (info.FileAttributes == 0)
2457 info.FileAttributes = FILE_ATTRIBUTE_NORMAL;
2460 status = (*func_NtSetInformationFile)(h, &ctx->iosb,
2461 &info, sizeof(info),
2462 FileBasicInformation);
2463 /* On FAT volumes we get STATUS_INVALID_PARAMETER if we try to set
2464 * attributes on the root directory. (Apparently because FAT doesn't
2465 * actually have a place to store those attributes!) */
2466 if (!NT_SUCCESS(status)
2467 && !(status == STATUS_INVALID_PARAMETER &&
2468 dentry_is_root(inode_first_extraction_dentry(inode))))
2470 winnt_error(status, L"Can't set basic metadata on \"%ls\"",
2472 return WIMLIB_ERR_SET_ATTRIBUTES;
2479 apply_metadata_to_file(const struct wim_dentry *dentry,
2480 struct win32_apply_ctx *ctx)
2482 const struct wim_inode *inode = dentry->d_inode;
2488 perms = FILE_WRITE_ATTRIBUTES | WRITE_DAC |
2489 WRITE_OWNER | ACCESS_SYSTEM_SECURITY;
2491 build_extraction_path(dentry, ctx);
2493 /* Open a handle with as many relevant permissions as possible. */
2494 while (!NT_SUCCESS(status = do_create_file(&h, perms, NULL,
2495 0, FILE_OPEN, 0, ctx)))
2497 if (status == STATUS_PRIVILEGE_NOT_HELD ||
2498 status == STATUS_ACCESS_DENIED)
2500 if (perms & ACCESS_SYSTEM_SECURITY) {
2501 perms &= ~ACCESS_SYSTEM_SECURITY;
2504 if (perms & WRITE_DAC) {
2505 perms &= ~WRITE_DAC;
2508 if (perms & WRITE_OWNER) {
2509 perms &= ~WRITE_OWNER;
2513 winnt_error(status, L"Can't open \"%ls\" to set metadata",
2515 return WIMLIB_ERR_OPEN;
2518 ret = do_apply_metadata_to_file(h, inode, ctx);
2526 apply_metadata(struct list_head *dentry_list, struct win32_apply_ctx *ctx)
2528 const struct wim_dentry *dentry;
2531 /* We go in reverse so that metadata is set on all a directory's
2532 * children before the directory itself. This avoids any potential
2533 * problems with attributes, timestamps, or security descriptors. */
2534 list_for_each_entry_reverse(dentry, dentry_list, d_extraction_list_node)
2536 ret = apply_metadata_to_file(dentry, ctx);
2537 ret = check_apply_error(dentry, ctx, ret);
2540 ret = report_file_metadata_applied(&ctx->common);
2547 /* Issue warnings about problems during the extraction for which warnings were
2548 * not already issued (due to the high number of potential warnings if we issued
2549 * them per-file). */
2551 do_warnings(const struct win32_apply_ctx *ctx)
2553 if (ctx->partial_security_descriptors == 0
2554 && ctx->no_security_descriptors == 0
2555 && ctx->num_set_short_name_failures == 0
2557 && ctx->num_remove_short_name_failures == 0
2562 WARNING("Extraction to \"%ls\" complete, but with one or more warnings:",
2563 ctx->common.target);
2564 if (ctx->num_set_short_name_failures) {
2565 WARNING("- Could not set short names on %lu files or directories",
2566 ctx->num_set_short_name_failures);
2569 if (ctx->num_remove_short_name_failures) {
2570 WARNING("- Could not remove short names on %lu files or directories"
2571 " (This is expected on Vista and earlier)",
2572 ctx->num_remove_short_name_failures);
2575 if (ctx->partial_security_descriptors) {
2576 WARNING("- Could only partially set the security descriptor\n"
2577 " on %lu files or directories.",
2578 ctx->partial_security_descriptors);
2580 if (ctx->no_security_descriptors) {
2581 WARNING("- Could not set security descriptor at all\n"
2582 " on %lu files or directories.",
2583 ctx->no_security_descriptors);
2585 if (ctx->partial_security_descriptors || ctx->no_security_descriptors) {
2586 WARNING("To fully restore all security descriptors, run the program\n"
2587 " with Administrator rights.");
2592 count_dentries(const struct list_head *dentry_list)
2594 const struct list_head *cur;
2597 list_for_each(cur, dentry_list)
2603 /* Extract files from a WIM image to a directory on Windows */
2605 win32_extract(struct list_head *dentry_list, struct apply_ctx *_ctx)
2608 struct win32_apply_ctx *ctx = (struct win32_apply_ctx *)_ctx;
2611 ret = prepare_target(dentry_list, ctx);
2615 if (unlikely(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_WIMBOOT)) {
2616 ret = start_wimboot_extraction(dentry_list, ctx);
2621 dentry_count = count_dentries(dentry_list);
2623 ret = start_file_structure_phase(&ctx->common, dentry_count);
2627 ret = create_directories(dentry_list, ctx);
2631 ret = create_nondirectories(dentry_list, ctx);
2635 ret = end_file_structure_phase(&ctx->common);
2639 struct read_blob_list_callbacks cbs = {
2640 .begin_blob = begin_extract_blob,
2641 .begin_blob_ctx = ctx,
2642 .consume_chunk = extract_chunk,
2643 .consume_chunk_ctx = ctx,
2644 .end_blob = end_extract_blob,
2645 .end_blob_ctx = ctx,
2647 ret = extract_blob_list(&ctx->common, &cbs);
2651 ret = start_file_metadata_phase(&ctx->common, dentry_count);
2655 ret = apply_metadata(dentry_list, ctx);
2659 ret = end_file_metadata_phase(&ctx->common);
2663 if (unlikely(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_WIMBOOT)) {
2664 ret = end_wimboot_extraction(ctx);
2671 close_target_directory(ctx);
2672 if (ctx->target_ntpath.Buffer)
2673 HeapFree(GetProcessHeap(), 0, ctx->target_ntpath.Buffer);
2674 FREE(ctx->pathbuf.Buffer);
2675 FREE(ctx->print_buffer);
2676 FREE(ctx->wimboot.wims);
2677 if (ctx->wimboot.prepopulate_pats) {
2678 FREE(ctx->wimboot.prepopulate_pats->strings);
2679 FREE(ctx->wimboot.prepopulate_pats);
2681 FREE(ctx->wimboot.mem_prepopulate_pats);
2682 FREE(ctx->data_buffer);
2686 const struct apply_operations win32_apply_ops = {
2688 .get_supported_features = win32_get_supported_features,
2689 .extract = win32_extract,
2690 .will_externally_back = win32_will_externally_back,
2691 .context_size = sizeof(struct win32_apply_ctx),
2694 #endif /* __WIN32__ */