2 * win32_apply.c - Windows-specific code for applying files from a WIM image.
6 * Copyright (C) 2013, 2014 Eric Biggers
8 * This file is part of wimlib, a library for working with WIM files.
10 * wimlib is free software; you can redistribute it and/or modify it under the
11 * terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 3 of the License, or (at your option)
15 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
16 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 * A PARTICULAR PURPOSE. See the GNU General Public License for more
20 * You should have received a copy of the GNU General Public License
21 * along with wimlib; if not, see http://www.gnu.org/licenses/.
30 #include "wimlib/win32_common.h"
32 #include "wimlib/apply.h"
33 #include "wimlib/capture.h" /* for mangle_pat() and match_pattern_list() */
34 #include "wimlib/dentry.h"
35 #include "wimlib/error.h"
36 #include "wimlib/lookup_table.h"
37 #include "wimlib/metadata.h"
38 #include "wimlib/reparse.h"
39 #include "wimlib/textfile.h"
40 #include "wimlib/xml.h"
41 #include "wimlib/wimboot.h"
43 struct win32_apply_ctx {
45 /* Extract flags, the pointer to the WIMStruct, etc. */
46 struct apply_ctx common;
48 /* WIMBoot information, only filled in if WIMLIB_EXTRACT_FLAG_WIMBOOT
52 struct string_set *prepopulate_pats;
53 void *mem_prepopulate_pats;
54 u8 wim_lookup_table_hash[SHA1_HASH_SIZE];
58 /* Open handle to the target directory */
61 /* NT namespace path to the target directory (buffer allocated) */
62 UNICODE_STRING target_ntpath;
64 /* Temporary buffer for building paths (buffer allocated) */
65 UNICODE_STRING pathbuf;
67 /* Object attributes to reuse for opening files in the target directory.
68 * (attr.ObjectName == &pathbuf) and (attr.RootDirectory == h_target).
70 OBJECT_ATTRIBUTES attr;
72 /* Temporary I/O status block for system calls */
75 /* Allocated buffer for creating "printable" paths from our
76 * target-relative NT paths */
77 wchar_t *print_buffer;
79 /* Allocated buffer for reading stream data when it cannot be extracted
83 /* Pointer to the next byte in @data_buffer to fill */
86 /* Size allocated in @data_buffer */
87 size_t data_buffer_size;
89 /* Current offset in the raw encrypted file being written */
90 size_t encrypted_offset;
92 /* Current size of the raw encrypted file being written */
93 size_t encrypted_size;
95 /* Temporary buffer for reparse data */
96 struct reparse_buffer_disk rpbuf;
98 /* Temporary buffer for reparse data of "fixed" absolute symbolic links
100 struct reparse_buffer_disk rpfixbuf;
102 /* Array of open handles to filesystem streams currently being written
104 HANDLE open_handles[MAX_OPEN_STREAMS];
106 /* Number of handles in @open_handles currently open (filled in from the
107 * beginning of the array) */
108 unsigned num_open_handles;
110 /* List of dentries, joined by @tmp_list, that need to have reparse data
111 * extracted as soon as the whole stream has been read into
113 struct list_head reparse_dentries;
115 /* List of dentries, joined by @tmp_list, that need to have raw
116 * encrypted data extracted as soon as the whole stream has been read
117 * into @data_buffer. */
118 struct list_head encrypted_dentries;
120 /* Number of files for which we didn't have permission to set the full
121 * security descriptor. */
122 unsigned long partial_security_descriptors;
124 /* Number of files for which we didn't have permission to set any part
125 * of the security descriptor. */
126 unsigned long no_security_descriptors;
129 /* Get the drive letter from a Windows path, or return the null character if the
130 * path is relative. */
132 get_drive_letter(const wchar_t *path)
134 /* Skip \\?\ prefix */
135 if (!wcsncmp(path, L"\\\\?\\", 4))
138 /* Return drive letter if valid */
139 if (((path[0] >= L'a' && path[0] <= L'z') ||
140 (path[0] >= L'A' && path[0] <= L'Z')) && path[1] == L':')
147 get_vol_flags(const wchar_t *target, DWORD *vol_flags_ret,
148 bool *short_names_supported_ret)
150 wchar_t filesystem_name[MAX_PATH + 1];
152 wchar_t *volume = NULL;
155 *short_names_supported_ret = false;
157 drive[0] = get_drive_letter(target);
165 if (!GetVolumeInformation(volume, NULL, 0, NULL, NULL,
166 vol_flags_ret, filesystem_name,
167 ARRAY_LEN(filesystem_name)))
169 DWORD err = GetLastError();
170 set_errno_from_win32_error(err);
171 WARNING_WITH_ERRNO("Failed to get volume information for "
172 "\"%ls\" (err=%"PRIu32")",
177 if (wcsstr(filesystem_name, L"NTFS")) {
178 /* FILE_SUPPORTS_HARD_LINKS is only supported on Windows 7 and
179 * later. Force it on anyway if filesystem is NTFS. */
180 *vol_flags_ret |= FILE_SUPPORTS_HARD_LINKS;
182 /* There's no volume flag for short names, but according to the
183 * MS documentation they are only user-settable on NTFS. */
184 *short_names_supported_ret = true;
189 win32_get_supported_features(const wchar_t *target,
190 struct wim_features *supported_features)
193 bool short_names_supported;
195 /* Query the features of the target volume. */
197 get_vol_flags(target, &vol_flags, &short_names_supported);
199 supported_features->archive_files = 1;
200 supported_features->hidden_files = 1;
201 supported_features->system_files = 1;
203 if (vol_flags & FILE_FILE_COMPRESSION)
204 supported_features->compressed_files = 1;
206 if (vol_flags & FILE_SUPPORTS_ENCRYPTION) {
207 supported_features->encrypted_files = 1;
208 supported_features->encrypted_directories = 1;
211 supported_features->not_context_indexed_files = 1;
213 /* Don't do anything with FILE_SUPPORTS_SPARSE_FILES. */
215 if (vol_flags & FILE_NAMED_STREAMS)
216 supported_features->named_data_streams = 1;
218 if (vol_flags & FILE_SUPPORTS_HARD_LINKS)
219 supported_features->hard_links = 1;
221 if (vol_flags & FILE_SUPPORTS_REPARSE_POINTS)
222 supported_features->reparse_points = 1;
224 if (vol_flags & FILE_PERSISTENT_ACLS)
225 supported_features->security_descriptors = 1;
227 if (short_names_supported)
228 supported_features->short_names = 1;
230 supported_features->timestamps = 1;
232 /* Note: Windows does not support case sensitive filenames! At least
233 * not without changing the registry and rebooting... */
238 /* Load the patterns from [PrepopulateList] of WimBootCompresse.ini in the WIM
239 * image being extracted. */
241 load_prepopulate_pats(struct win32_apply_ctx *ctx)
243 const wchar_t *path = L"\\Windows\\System32\\WimBootCompress.ini";
244 struct wim_dentry *dentry;
245 struct wim_lookup_table_entry *lte;
248 struct string_set *s;
250 struct text_file_section sec;
252 dentry = get_dentry(ctx->common.wim, path, WIMLIB_CASE_INSENSITIVE);
254 (dentry->d_inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
255 FILE_ATTRIBUTE_REPARSE_POINT |
256 FILE_ATTRIBUTE_ENCRYPTED)) ||
257 !(lte = inode_unnamed_lte(dentry->d_inode, ctx->common.wim->lookup_table)))
259 WARNING("%ls does not exist in WIM image!", path);
260 return WIMLIB_ERR_PATH_DOES_NOT_EXIST;
263 ret = read_full_stream_into_alloc_buf(lte, &buf);
267 s = CALLOC(1, sizeof(struct string_set));
270 return WIMLIB_ERR_NOMEM;
273 sec.name = T("PrepopulateList");
276 ret = do_load_text_file(path, buf, lte->size, &mem, &sec, 1,
277 LOAD_TEXT_FILE_REMOVE_QUOTES |
278 LOAD_TEXT_FILE_NO_WARNINGS,
280 BUILD_BUG_ON(OS_PREFERRED_PATH_SEPARATOR != WIM_PATH_SEPARATOR);
286 ctx->wimboot.prepopulate_pats = s;
287 ctx->wimboot.mem_prepopulate_pats = mem;
291 /* Returns %true if the path to @dentry matches a pattern in [PrepopulateList]
292 * of WimBootCompress.ini. Otherwise returns %false.
294 * @dentry must have had its full path calculated. */
296 in_prepopulate_list(struct wim_dentry *dentry,
297 const struct win32_apply_ctx *ctx)
299 const struct string_set *pats = ctx->wimboot.prepopulate_pats;
301 if (!pats || !pats->num_strings)
304 return match_pattern_list(dentry->_full_path,
305 wcslen(dentry->_full_path), pats);
308 /* Calculates the SHA-1 message digest of the WIM's lookup table. */
310 hash_lookup_table(WIMStruct *wim, u8 hash[SHA1_HASH_SIZE])
312 return wim_reshdr_to_hash(&wim->hdr.lookup_table_reshdr, wim, hash);
315 /* Prepare for doing a "WIMBoot" extraction by loading patterns from
316 * [PrepopulateList] of WimBootCompress.ini and allocating a WOF data source ID
317 * on the target volume. */
319 start_wimboot_extraction(struct win32_apply_ctx *ctx)
322 WIMStruct *wim = ctx->common.wim;
324 ret = load_prepopulate_pats(ctx);
325 if (ret == WIMLIB_ERR_NOMEM)
328 if (!wim_info_get_wimboot(wim->wim_info,
330 WARNING("Image is not marked as WIMBoot compatible!");
332 ret = hash_lookup_table(ctx->common.wim,
333 ctx->wimboot.wim_lookup_table_hash);
337 return wimboot_alloc_data_source_id(wim->filename,
341 &ctx->wimboot.data_source_id,
342 &ctx->wimboot.wof_running);
345 /* Returns the number of wide characters needed to represent the path to the
346 * specified @dentry, relative to the target directory, when extracted.
348 * Does not include null terminator (not needed for NtCreateFile). */
350 dentry_extraction_path_length(const struct wim_dentry *dentry)
353 const struct wim_dentry *d;
357 len += d->d_extraction_name_nchars + 1;
359 } while (!dentry_is_root(d) && will_extract_dentry(d));
361 return --len; /* No leading slash */
364 /* Returns the length of the longest string that might need to be appended to
365 * the path to an alias of an inode to open or create a named data stream.
367 * If the inode has no named data streams, this will be 0. Otherwise, this will
368 * be 1 plus the length of the longest-named data stream, since the data stream
369 * name must be separated form the path by the ':' character. */
371 inode_longest_named_data_stream_spec(const struct wim_inode *inode)
374 for (u16 i = 0; i < inode->i_num_ads; i++) {
375 size_t len = inode->i_ads_entries[i].stream_name_nbytes;
380 max = 1 + (max / sizeof(wchar_t));
384 /* Find the length, in wide characters, of the longest path needed for
385 * extraction of any file in @dentry_list relative to the target directory.
387 * Accounts for named data streams, but does not include null terminator (not
388 * needed for NtCreateFile). */
390 compute_path_max(struct list_head *dentry_list)
393 const struct wim_dentry *dentry;
395 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
398 len = dentry_extraction_path_length(dentry);
400 /* Account for named data streams */
401 len += inode_longest_named_data_stream_spec(dentry->d_inode);
410 /* Build the path at which to extract the @dentry, relative to the target
413 * The path is saved in ctx->pathbuf. */
415 build_extraction_path(const struct wim_dentry *dentry,
416 struct win32_apply_ctx *ctx)
420 const struct wim_dentry *d;
422 len = dentry_extraction_path_length(dentry);
424 ctx->pathbuf.Length = len * sizeof(wchar_t);
425 p = ctx->pathbuf.Buffer + len;
427 !dentry_is_root(d->d_parent) && will_extract_dentry(d->d_parent);
430 p -= d->d_extraction_name_nchars;
431 wmemcpy(p, d->d_extraction_name, d->d_extraction_name_nchars);
434 /* No leading slash */
435 p -= d->d_extraction_name_nchars;
436 wmemcpy(p, d->d_extraction_name, d->d_extraction_name_nchars);
439 /* Build the path at which to extract the @dentry, relative to the target
440 * directory, adding the suffix for a named data stream.
442 * The path is saved in ctx->pathbuf. */
444 build_extraction_path_with_ads(const struct wim_dentry *dentry,
445 struct win32_apply_ctx *ctx,
446 const wchar_t *stream_name,
447 size_t stream_name_nchars)
451 build_extraction_path(dentry, ctx);
453 /* Add :NAME for named data stream */
454 p = ctx->pathbuf.Buffer + (ctx->pathbuf.Length / sizeof(wchar_t));
456 wmemcpy(p, stream_name, stream_name_nchars);
457 ctx->pathbuf.Length += (1 + stream_name_nchars) * sizeof(wchar_t);
460 /* Build the Win32 namespace path to the specified @dentry when extracted.
462 * The path is saved in ctx->pathbuf and will be null terminated.
464 * XXX: We could get rid of this if it wasn't needed for the file encryption
467 build_win32_extraction_path(const struct wim_dentry *dentry,
468 struct win32_apply_ctx *ctx)
470 build_extraction_path(dentry, ctx);
472 /* Prepend target_ntpath to our relative path, then change \??\ into \\?\ */
474 memmove(ctx->pathbuf.Buffer +
475 (ctx->target_ntpath.Length / sizeof(wchar_t)) + 1,
476 ctx->pathbuf.Buffer, ctx->pathbuf.Length);
477 memcpy(ctx->pathbuf.Buffer, ctx->target_ntpath.Buffer,
478 ctx->target_ntpath.Length);
479 ctx->pathbuf.Buffer[ctx->target_ntpath.Length / sizeof(wchar_t)] = L'\\';
480 ctx->pathbuf.Length += ctx->target_ntpath.Length + sizeof(wchar_t);
481 ctx->pathbuf.Buffer[ctx->pathbuf.Length / sizeof(wchar_t)] = L'\0';
483 wimlib_assert(ctx->pathbuf.Length >= 4 * sizeof(wchar_t) &&
484 !wmemcmp(ctx->pathbuf.Buffer, L"\\??\\", 4));
486 ctx->pathbuf.Buffer[1] = L'\\';
490 /* Returns a "printable" representation of the last relative NT path that was
491 * constructed with build_extraction_path() or build_extraction_path_with_ads().
493 * This will be overwritten by the next call to this function. */
494 static const wchar_t *
495 current_path(struct win32_apply_ctx *ctx)
497 wchar_t *p = ctx->print_buffer;
499 p = wmempcpy(p, ctx->common.target, ctx->common.target_nchars);
501 p = wmempcpy(p, ctx->pathbuf.Buffer, ctx->pathbuf.Length / sizeof(wchar_t));
503 return ctx->print_buffer;
507 * Ensures the target directory exists and opens a handle to it, in preparation
508 * of using paths relative to it.
511 prepare_target(struct list_head *dentry_list, struct win32_apply_ctx *ctx)
516 /* Open handle to the target directory (possibly creating it). */
518 if (func_RtlDosPathNameToNtPathName_U_WithStatus) {
519 status = (*func_RtlDosPathNameToNtPathName_U_WithStatus)(ctx->common.target,
523 if ((*func_RtlDosPathNameToNtPathName_U)(ctx->common.target,
526 status = STATUS_SUCCESS;
528 status = STATUS_NO_MEMORY;
530 if (!NT_SUCCESS(status)) {
531 if (status == STATUS_NO_MEMORY) {
532 return WIMLIB_ERR_NOMEM;
534 ERROR("\"%ls\": invalid path name "
535 "(status=0x%08"PRIx32")",
536 ctx->common.target, (u32)status);
537 return WIMLIB_ERR_INVALID_PARAM;
541 ctx->attr.Length = sizeof(ctx->attr);
542 ctx->attr.ObjectName = &ctx->target_ntpath;
544 status = (*func_NtCreateFile)(&ctx->h_target,
550 FILE_SHARE_VALID_FLAGS,
552 FILE_DIRECTORY_FILE |
553 FILE_OPEN_REPARSE_POINT |
554 FILE_OPEN_FOR_BACKUP_INTENT,
558 if (!NT_SUCCESS(status)) {
559 set_errno_from_nt_status(status);
560 ERROR_WITH_ERRNO("Can't open or create directory \"%ls\" "
561 "(status=0x%08"PRIx32")",
562 ctx->common.target, (u32)status);
563 return WIMLIB_ERR_OPENDIR;
566 path_max = compute_path_max(dentry_list);
568 /* Add some extra for building Win32 paths for the file encryption APIs
570 path_max += 2 + (ctx->target_ntpath.Length / sizeof(wchar_t));
572 ctx->pathbuf.MaximumLength = path_max * sizeof(wchar_t);
573 ctx->pathbuf.Buffer = MALLOC(ctx->pathbuf.MaximumLength);
574 if (!ctx->pathbuf.Buffer)
575 return WIMLIB_ERR_NOMEM;
577 ctx->attr.RootDirectory = ctx->h_target;
578 ctx->attr.ObjectName = &ctx->pathbuf;
580 ctx->print_buffer = MALLOC((ctx->common.target_nchars + 1 + path_max + 1) *
582 if (!ctx->print_buffer)
583 return WIMLIB_ERR_NOMEM;
588 /* When creating an inode that will have a short (DOS) name, we create it using
589 * the long name associated with the short name. This ensures that the short
590 * name gets associated with the correct long name. */
591 static const struct wim_dentry *
592 first_extraction_alias(const struct wim_inode *inode)
594 const struct list_head *next = inode->i_extraction_aliases.next;
595 const struct wim_dentry *dentry;
598 dentry = list_entry(next, struct wim_dentry,
599 d_extraction_alias_node);
600 if (dentry_has_short_name(dentry))
603 } while (next != &inode->i_extraction_aliases);
608 * Set or clear FILE_ATTRIBUTE_COMPRESSED if the inherited value is different
609 * from the desired value.
611 * Note that you can NOT override the inherited value of
612 * FILE_ATTRIBUTE_COMPRESSED directly with NtCreateFile().
615 adjust_compression_attribute(HANDLE h, const struct wim_dentry *dentry,
616 struct win32_apply_ctx *ctx)
618 const bool compressed = (dentry->d_inode->i_attributes &
619 FILE_ATTRIBUTE_COMPRESSED);
621 if (ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES)
624 if (!ctx->common.supported_features.compressed_files)
627 FILE_BASIC_INFORMATION info;
629 USHORT compression_state;
631 /* Get current attributes */
632 status = (*func_NtQueryInformationFile)(h, &ctx->iosb,
634 FileBasicInformation);
635 if (NT_SUCCESS(status) &&
636 compressed == !!(info.FileAttributes & FILE_ATTRIBUTE_COMPRESSED))
638 /* Nothing needs to be done. */
642 /* Set the new compression state */
645 compression_state = COMPRESSION_FORMAT_DEFAULT;
647 compression_state = COMPRESSION_FORMAT_NONE;
649 status = (*func_NtFsControlFile)(h,
654 FSCTL_SET_COMPRESSION,
659 if (NT_SUCCESS(status))
662 set_errno_from_nt_status(status);
663 ERROR_WITH_ERRNO("Can't %s compression attribute on \"%ls\" "
664 "(status=0x%08"PRIx32")",
665 (compressed ? "set" : "clear"),
666 current_path(ctx), status);
667 return WIMLIB_ERR_SET_ATTRIBUTES;
671 * Clear FILE_ATTRIBUTE_ENCRYPTED if the file or directory is not supposed to be
674 * You can provide FILE_ATTRIBUTE_ENCRYPTED to NtCreateFile() to set it on the
675 * created file. However, the file or directory will otherwise default to the
676 * encryption state of the parent directory. This function works around this
677 * limitation by using DecryptFile() to remove FILE_ATTRIBUTE_ENCRYPTED on files
678 * (and directories) that are not supposed to have it set.
680 * Regardless of whether it succeeds or fails, this function may close the
681 * handle to the file. If it does, it sets it to NULL.
684 maybe_clear_encryption_attribute(HANDLE *h_ret, const struct wim_dentry *dentry,
685 struct win32_apply_ctx *ctx)
687 if (dentry->d_inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED)
690 if (ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES)
693 if (!ctx->common.supported_features.encrypted_files)
696 FILE_BASIC_INFORMATION info;
700 /* Get current attributes */
701 status = (*func_NtQueryInformationFile)(*h_ret, &ctx->iosb,
703 FileBasicInformation);
704 if (NT_SUCCESS(status) &&
705 !(info.FileAttributes & FILE_ATTRIBUTE_ENCRYPTED))
707 /* Nothing needs to be done. */
711 /* Set the new encryption state */
713 /* Due to Windows' crappy file encryption APIs, we need to close the
714 * handle to the file so we don't get ERROR_SHARING_VIOLATION. We also
715 * hack together a Win32 path, although we will use the \\?\ prefix so
716 * it will actually be a NT path in disguise... */
717 (*func_NtClose)(*h_ret);
720 build_win32_extraction_path(dentry, ctx);
722 bret = DecryptFile(ctx->pathbuf.Buffer, 0);
724 /* Restore the NT namespace path */
725 build_extraction_path(dentry, ctx);
728 DWORD err = GetLastError();
729 set_errno_from_win32_error(err);
730 ERROR_WITH_ERRNO("Can't decrypt file \"%ls\" (err=%"PRIu32")",
731 current_path(ctx), (u32)err);
732 return WIMLIB_ERR_SET_ATTRIBUTES;
737 /* Set the short name on the open file @h which has been created at the location
738 * indicated by @dentry.
740 * Note that this may add, change, or remove the short name.
742 * @h must be opened with DELETE access.
744 * Returns 0 or WIMLIB_ERR_SET_SHORT_NAME. The latter only happens in
745 * STRICT_SHORT_NAMES mode.
748 set_short_name(HANDLE h, const struct wim_dentry *dentry,
749 struct win32_apply_ctx *ctx)
751 size_t bufsize = offsetof(FILE_NAME_INFORMATION, FileName) +
752 dentry->short_name_nbytes;
753 u8 buf[bufsize] _aligned_attribute(8);
754 FILE_NAME_INFORMATION *info = (FILE_NAME_INFORMATION *)buf;
757 info->FileNameLength = dentry->short_name_nbytes;
758 memcpy(info->FileName, dentry->short_name, dentry->short_name_nbytes);
760 status = (*func_NtSetInformationFile)(h, &ctx->iosb, info, bufsize,
761 FileShortNameInformation);
762 if (NT_SUCCESS(status))
765 /* By default, failure to set short names is not an error (since short
766 * names aren't too important anymore...). */
767 if (!(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_SHORT_NAMES))
770 if (status == STATUS_SHORT_NAMES_NOT_ENABLED_ON_VOLUME) {
771 if (dentry->short_name_nbytes == 0)
773 ERROR("Can't extract short name when short "
774 "names are not enabled on the volume!");
776 ERROR("Can't set short name on \"%ls\" (status=0x%08"PRIx32")",
777 current_path(ctx), (u32)status);
779 return WIMLIB_ERR_SET_SHORT_NAME;
783 * A wrapper around NtCreateFile() to make it slightly more usable...
784 * This uses the path currently constructed in ctx->pathbuf.
786 * Also, we always specify FILE_OPEN_FOR_BACKUP_INTENT and
787 * FILE_OPEN_REPARSE_POINT.
790 do_create_file(PHANDLE FileHandle,
791 ACCESS_MASK DesiredAccess,
792 PLARGE_INTEGER AllocationSize,
793 ULONG FileAttributes,
794 ULONG CreateDisposition,
796 struct win32_apply_ctx *ctx)
798 return (*func_NtCreateFile)(FileHandle,
804 FILE_SHARE_VALID_FLAGS,
807 FILE_OPEN_FOR_BACKUP_INTENT |
808 FILE_OPEN_REPARSE_POINT,
813 /* Like do_create_file(), but builds the extraction path of the @dentry first.
816 create_file(PHANDLE FileHandle,
817 ACCESS_MASK DesiredAccess,
818 PLARGE_INTEGER AllocationSize,
819 ULONG FileAttributes,
820 ULONG CreateDisposition,
822 const struct wim_dentry *dentry,
823 struct win32_apply_ctx *ctx)
825 build_extraction_path(dentry, ctx);
826 return do_create_file(FileHandle,
835 /* Create empty named data streams.
837 * Since these won't have 'struct wim_lookup_table_entry's, they won't show up
838 * in the call to extract_stream_list(). Hence the need for the special case.
841 create_any_empty_ads(const struct wim_dentry *dentry,
842 struct win32_apply_ctx *ctx)
844 const struct wim_inode *inode = dentry->d_inode;
845 LARGE_INTEGER allocation_size;
846 bool path_modified = false;
849 if (!ctx->common.supported_features.named_data_streams)
852 for (u16 i = 0; i < inode->i_num_ads; i++) {
853 const struct wim_ads_entry *entry;
857 entry = &inode->i_ads_entries[i];
860 if (!entry->stream_name_nbytes)
867 /* Probably setting the allocation size to 0 has no effect, but
868 * we might as well try. */
869 allocation_size.QuadPart = 0;
871 build_extraction_path_with_ads(dentry, ctx,
873 entry->stream_name_nbytes /
875 path_modified = true;
876 status = do_create_file(&h, FILE_WRITE_DATA, &allocation_size,
877 0, FILE_SUPERSEDE, 0, ctx);
878 if (!NT_SUCCESS(status)) {
879 set_errno_from_nt_status(status);
880 ERROR_WITH_ERRNO("Can't create \"%ls\" "
881 "(status=0x%08"PRIx32")",
882 current_path(ctx), (u32)status);
883 ret = WIMLIB_ERR_OPEN;
888 /* Restore the path to the dentry itself */
890 build_extraction_path(dentry, ctx);
895 * Creates the directory named by @dentry, or uses an existing directory at that
896 * location. If necessary, sets the short name and/or fixes compression and
897 * encryption attributes.
899 * Returns 0, WIMLIB_ERR_MKDIR, or WIMLIB_ERR_SET_SHORT_NAME.
902 create_directory(const struct wim_dentry *dentry,
903 struct win32_apply_ctx *ctx)
910 /* Special attributes:
912 * Use FILE_ATTRIBUTE_ENCRYPTED if the directory needs to have it set.
913 * This doesn't work for FILE_ATTRIBUTE_COMPRESSED (unfortunately).
915 * Don't specify FILE_ATTRIBUTE_DIRECTORY; it gets set anyway as a
916 * result of the FILE_DIRECTORY_FILE option. */
917 attrib = (dentry->d_inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED);
919 /* DELETE is needed for set_short_name().
920 * GENERIC_READ and GENERIC_WRITE are needed for
921 * adjust_compression_attribute(). */
922 status = create_file(&h, GENERIC_READ | GENERIC_WRITE | DELETE, NULL,
923 attrib, FILE_OPEN_IF, FILE_DIRECTORY_FILE,
925 if (!NT_SUCCESS(status)) {
926 set_errno_from_nt_status(status);
927 ERROR_WITH_ERRNO("Can't create directory \"%ls\" "
928 "(status=0x%08"PRIx32")",
929 current_path(ctx), (u32)status);
930 return WIMLIB_ERR_MKDIR;
933 ret = set_short_name(h, dentry, ctx);
936 ret = adjust_compression_attribute(h, dentry, ctx);
939 ret = maybe_clear_encryption_attribute(&h, dentry, ctx);
940 /* May close the handle!!! */
948 * Create all the directories being extracted, other than the target directory
951 * Note: we don't honor directory hard links. However, we don't allow them to
952 * exist in WIM images anyway (see inode_fixup.c).
955 create_directories(struct list_head *dentry_list,
956 struct win32_apply_ctx *ctx)
958 const struct wim_dentry *dentry;
961 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
963 if (!(dentry->d_inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY))
966 /* Note: Here we include files with
967 * FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_REPARSE_POINT, but we
968 * wait until later to actually set the reparse data. */
970 /* If the root dentry is being extracted, it was already done so
971 * it prepare_target(). */
972 if (dentry_is_root(dentry))
975 ret = create_directory(dentry, ctx);
979 ret = create_any_empty_ads(dentry, ctx);
987 * Creates the nondirectory file named by @dentry.
989 * On success, returns an open handle to the file in @h_ret, with GENERIC_READ,
990 * GENERIC_WRITE, and DELETE access. Also, the path to the file will be saved
991 * in ctx->pathbuf. On failure, returns WIMLIB_ERR_OPEN.
994 create_nondirectory_inode(HANDLE *h_ret, const struct wim_dentry *dentry,
995 struct win32_apply_ctx *ctx)
997 const struct wim_inode *inode;
1000 bool retried = false;
1002 inode = dentry->d_inode;
1004 /* If the file already exists and has FILE_ATTRIBUTE_SYSTEM and/or
1005 * FILE_ATTRIBUTE_HIDDEN, these must be specified in order to supersede
1008 * Normally the user shouldn't be trying to overwrite such files anyway,
1009 * but we at least provide FILE_ATTRIBUTE_SYSTEM and
1010 * FILE_ATTRIBUTE_HIDDEN if the WIM inode has those attributes so that
1011 * we catch the case where the user extracts the same files to the same
1012 * location more than one time.
1014 * Also specify FILE_ATTRIBUTE_ENCRYPTED if the file needs to be
1017 * In NO_ATTRIBUTES mode just don't specify any attributes at all.
1019 if (ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES) {
1022 attrib = (inode->i_attributes & (FILE_ATTRIBUTE_SYSTEM |
1023 FILE_ATTRIBUTE_HIDDEN |
1024 FILE_ATTRIBUTE_ENCRYPTED));
1026 build_extraction_path(dentry, ctx);
1028 status = do_create_file(h_ret, GENERIC_READ | GENERIC_WRITE | DELETE,
1029 NULL, attrib, FILE_SUPERSEDE,
1030 FILE_NON_DIRECTORY_FILE, ctx);
1031 if (NT_SUCCESS(status)) {
1034 ret = adjust_compression_attribute(*h_ret, dentry, ctx);
1036 (*func_NtClose)(*h_ret);
1040 ret = maybe_clear_encryption_attribute(h_ret, dentry, ctx);
1041 /* May close the handle!!! */
1045 (*func_NtClose)(*h_ret);
1050 /* Re-open the handle so that we can return it on
1052 status = do_create_file(h_ret,
1054 GENERIC_WRITE | DELETE,
1056 FILE_NON_DIRECTORY_FILE, ctx);
1057 if (!NT_SUCCESS(status))
1061 ret = create_any_empty_ads(dentry, ctx);
1063 (*func_NtClose)(*h_ret);
1069 if (status == STATUS_ACCESS_DENIED && !retried) {
1070 /* We also can't supersede an existing file that has
1071 * FILE_ATTRIBUTE_READONLY set; doing so causes NtCreateFile()
1072 * to return STATUS_ACCESS_DENIED . The only workaround seems
1073 * to be to explicitly remove FILE_ATTRIBUTE_READONLY on the
1074 * existing file, then try again. */
1076 FILE_BASIC_INFORMATION info;
1079 status = do_create_file(&h, FILE_WRITE_ATTRIBUTES, NULL, 0,
1080 FILE_OPEN, FILE_NON_DIRECTORY_FILE, ctx);
1081 if (!NT_SUCCESS(status))
1084 memset(&info, 0, sizeof(info));
1085 info.FileAttributes = FILE_ATTRIBUTE_NORMAL;
1087 status = (*func_NtSetInformationFile)(h, &ctx->iosb,
1088 &info, sizeof(info),
1089 FileBasicInformation);
1091 if (!NT_SUCCESS(status))
1097 set_errno_from_nt_status(status);
1098 ERROR_WITH_ERRNO("Can't create file \"%ls\" (status=0x%08"PRIx32")",
1099 current_path(ctx), (u32)status);
1100 return WIMLIB_ERR_OPEN;
1103 /* Creates a hard link at the location named by @dentry to the file represented
1104 * by the open handle @h. Or, if the target volume does not support hard links,
1105 * create a separate file instead. */
1107 create_link(HANDLE h, const struct wim_dentry *dentry,
1108 struct win32_apply_ctx *ctx)
1110 if (ctx->common.supported_features.hard_links) {
1112 build_extraction_path(dentry, ctx);
1114 size_t bufsize = offsetof(FILE_LINK_INFORMATION, FileName) +
1115 ctx->pathbuf.Length + sizeof(wchar_t);
1116 u8 buf[bufsize] _aligned_attribute(8);
1117 FILE_LINK_INFORMATION *info = (FILE_LINK_INFORMATION *)buf;
1120 info->ReplaceIfExists = TRUE;
1121 info->RootDirectory = ctx->attr.RootDirectory;
1122 info->FileNameLength = ctx->pathbuf.Length;
1123 memcpy(info->FileName, ctx->pathbuf.Buffer, ctx->pathbuf.Length);
1124 info->FileName[info->FileNameLength / 2] = L'\0';
1126 /* Note: the null terminator isn't actually necessary,
1127 * but if you don't add the extra character, you get
1128 * STATUS_INFO_LENGTH_MISMATCH when FileNameLength
1129 * happens to be 2 */
1131 status = (*func_NtSetInformationFile)(h, &ctx->iosb,
1133 FileLinkInformation);
1134 if (NT_SUCCESS(status))
1136 ERROR("Failed to create link \"%ls\" (status=0x%08"PRIx32")",
1137 current_path(ctx), (u32)status);
1138 return WIMLIB_ERR_LINK;
1143 ret = create_nondirectory_inode(&h2, dentry, ctx);
1147 (*func_NtClose)(h2);
1152 /* Given an inode (represented by the open handle @h) for which one link has
1153 * been created (named by @first_dentry), create the other links.
1155 * Or, if the target volume does not support hard links, create separate files.
1157 * Note: This uses ctx->pathbuf and does not reset it.
1160 create_links(HANDLE h, const struct wim_dentry *first_dentry,
1161 struct win32_apply_ctx *ctx)
1163 const struct wim_inode *inode;
1164 const struct list_head *next;
1165 const struct wim_dentry *dentry;
1168 inode = first_dentry->d_inode;
1169 next = inode->i_extraction_aliases.next;
1171 dentry = list_entry(next, struct wim_dentry,
1172 d_extraction_alias_node);
1173 if (dentry != first_dentry) {
1174 ret = create_link(h, dentry, ctx);
1179 } while (next != &inode->i_extraction_aliases);
1183 /* Create a nondirectory file, including all links. */
1185 create_nondirectory(const struct wim_inode *inode, struct win32_apply_ctx *ctx)
1187 const struct wim_dentry *first_dentry;
1191 first_dentry = first_extraction_alias(inode);
1193 /* Create first link. */
1194 ret = create_nondirectory_inode(&h, first_dentry, ctx);
1198 /* Set short name. */
1199 ret = set_short_name(h, first_dentry, ctx);
1201 /* Create additional links, OR if hard links are not supported just
1202 * create more files. */
1204 ret = create_links(h, first_dentry, ctx);
1210 /* Create all the nondirectory files being extracted, including all aliases
1213 create_nondirectories(struct list_head *dentry_list, struct win32_apply_ctx *ctx)
1215 const struct wim_dentry *dentry;
1216 const struct wim_inode *inode;
1219 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
1220 inode = dentry->d_inode;
1221 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)
1223 /* Call create_nondirectory() only once per inode */
1224 if (dentry != inode_first_extraction_dentry(inode))
1226 ret = create_nondirectory(inode, ctx);
1234 close_handles(struct win32_apply_ctx *ctx)
1236 for (unsigned i = 0; i < ctx->num_open_handles; i++)
1237 (*func_NtClose)(ctx->open_handles[i]);
1240 /* Prepare to read the next stream, which has size @stream_size, into an
1241 * in-memory buffer. */
1243 prepare_data_buffer(struct win32_apply_ctx *ctx, u64 stream_size)
1245 if (stream_size > ctx->data_buffer_size) {
1246 /* Larger buffer needed. */
1248 if ((size_t)stream_size != stream_size)
1249 return WIMLIB_ERR_NOMEM;
1250 new_buffer = REALLOC(ctx->data_buffer, stream_size);
1252 return WIMLIB_ERR_NOMEM;
1253 ctx->data_buffer = new_buffer;
1254 ctx->data_buffer_size = stream_size;
1256 /* On the first call this changes data_buffer_ptr from NULL, which tells
1257 * extract_chunk() that the data buffer needs to be filled while reading
1258 * the stream data. */
1259 ctx->data_buffer_ptr = ctx->data_buffer;
1264 begin_extract_stream_instance(const struct wim_lookup_table_entry *stream,
1265 struct wim_dentry *dentry,
1266 const wchar_t *stream_name,
1267 struct win32_apply_ctx *ctx)
1269 const struct wim_inode *inode = dentry->d_inode;
1270 size_t stream_name_nchars = 0;
1271 FILE_ALLOCATION_INFORMATION alloc_info;
1275 if (unlikely(stream_name))
1276 stream_name_nchars = wcslen(stream_name);
1278 if (unlikely(stream_name_nchars)) {
1279 build_extraction_path_with_ads(dentry, ctx,
1280 stream_name, stream_name_nchars);
1282 build_extraction_path(dentry, ctx);
1285 /* Reparse point? */
1286 if (unlikely(inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT)
1287 && (stream_name_nchars == 0))
1289 if (!ctx->common.supported_features.reparse_points)
1292 /* We can't write the reparse stream directly; we must set it
1293 * with FSCTL_SET_REPARSE_POINT, which requires that all the
1294 * data be available. So, stage the data in a buffer. */
1296 list_add_tail(&dentry->tmp_list, &ctx->reparse_dentries);
1297 return prepare_data_buffer(ctx, stream->size);
1300 /* Encrypted file? */
1301 if (unlikely(inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED)
1302 && (stream_name_nchars == 0))
1304 if (!ctx->common.supported_features.encrypted_files)
1307 /* We can't write encrypted file streams directly; we must use
1308 * WriteEncryptedFileRaw(), which requires providing the data
1309 * through a callback function. This can't easily be combined
1310 * with our own callback-based approach.
1312 * The current workaround is to simply read the stream into
1313 * memory and write the encrypted file from that.
1315 * TODO: This isn't sufficient for extremely large encrypted
1316 * files. Perhaps we should create an extra thread to write
1318 list_add_tail(&dentry->tmp_list, &ctx->encrypted_dentries);
1319 return prepare_data_buffer(ctx, stream->size);
1322 /* Extracting unnamed data stream in WIMBoot mode? */
1323 if (unlikely(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_WIMBOOT)
1324 && (stream_name_nchars == 0)
1325 && (stream->resource_location == RESOURCE_IN_WIM)
1326 && (stream->rspec->wim == ctx->common.wim)
1327 && (stream->size == stream->rspec->uncompressed_size))
1329 int ret = calculate_dentry_full_path(dentry);
1332 if (in_prepopulate_list(dentry, ctx)) {
1333 union wimlib_progress_info info;
1335 info.wimboot_exclude.path_in_wim = dentry->_full_path;
1336 info.wimboot_exclude.extraction_path = current_path(ctx);
1338 ret = call_progress(ctx->common.progfunc,
1339 WIMLIB_PROGRESS_MSG_WIMBOOT_EXCLUDE,
1340 &info, ctx->common.progctx);
1341 FREE(dentry->_full_path);
1342 dentry->_full_path = NULL;
1345 /* Go on and open the file for normal extraction. */
1347 FREE(dentry->_full_path);
1348 dentry->_full_path = NULL;
1349 return wimboot_set_pointer(&ctx->attr,
1352 ctx->wimboot.data_source_id,
1353 ctx->wimboot.wim_lookup_table_hash,
1354 ctx->wimboot.wof_running);
1358 if (ctx->num_open_handles == MAX_OPEN_STREAMS) {
1359 /* XXX: Fix this. But because of the checks in
1360 * extract_stream_list(), this can now only happen on a
1361 * filesystem that does not support hard links. */
1362 ERROR("Can't extract data: too many open files!");
1363 return WIMLIB_ERR_UNSUPPORTED;
1366 /* Open a new handle */
1367 status = do_create_file(&h,
1368 FILE_WRITE_DATA | SYNCHRONIZE,
1369 NULL, 0, FILE_OPEN_IF,
1370 FILE_SEQUENTIAL_ONLY |
1371 FILE_SYNCHRONOUS_IO_NONALERT,
1373 if (!NT_SUCCESS(status)) {
1374 set_errno_from_nt_status(status);
1375 ERROR_WITH_ERRNO("Can't open \"%ls\" for writing "
1376 "(status=0x%08"PRIx32")",
1377 current_path(ctx), (u32)status);
1378 return WIMLIB_ERR_OPEN;
1381 ctx->open_handles[ctx->num_open_handles++] = h;
1383 /* Allocate space for the data. */
1384 alloc_info.AllocationSize.QuadPart = stream->size;
1385 (*func_NtSetInformationFile)(h, &ctx->iosb,
1386 &alloc_info, sizeof(alloc_info),
1387 FileAllocationInformation);
1391 /* Set the reparse data @rpbuf of length @rpbuflen on the extracted file
1392 * corresponding to the WIM dentry @dentry. */
1394 do_set_reparse_data(const struct wim_dentry *dentry,
1395 const void *rpbuf, u16 rpbuflen,
1396 struct win32_apply_ctx *ctx)
1401 status = create_file(&h, GENERIC_WRITE, NULL,
1402 0, FILE_OPEN, 0, dentry, ctx);
1403 if (!NT_SUCCESS(status))
1406 status = (*func_NtFsControlFile)(h, NULL, NULL, NULL,
1407 &ctx->iosb, FSCTL_SET_REPARSE_POINT,
1408 (void *)rpbuf, rpbuflen,
1412 if (NT_SUCCESS(status))
1415 /* On Windows, by default only the Administrator can create symbolic
1416 * links for some reason. By default we just issue a warning if this
1417 * appears to be the problem. Use WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS
1418 * to get a hard error. */
1419 if (!(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS)
1420 && (status == STATUS_PRIVILEGE_NOT_HELD ||
1421 status == STATUS_ACCESS_DENIED)
1422 && (dentry->d_inode->i_reparse_tag == WIM_IO_REPARSE_TAG_SYMLINK ||
1423 dentry->d_inode->i_reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT))
1425 WARNING("Can't create symbolic link \"%ls\"! \n"
1426 " (Need Administrator rights, or at least "
1428 " SeCreateSymbolicLink privilege.)",
1434 set_errno_from_nt_status(status);
1435 ERROR_WITH_ERRNO("Can't set reparse data on \"%ls\" "
1436 "(status=0x%08"PRIx32")",
1437 current_path(ctx), (u32)status);
1438 return WIMLIB_ERR_SET_REPARSE_DATA;
1441 /* Given a Windows NT namespace path, such as \??\e:\Windows\System32, return a
1442 * pointer to the suffix of the path that begins with the device directly, such
1443 * as e:\Windows\System32. */
1444 static const wchar_t *
1445 skip_nt_toplevel_component(const wchar_t *path, size_t path_nchars)
1447 static const wchar_t * const dirs[] = {
1452 size_t first_dir_len = 0;
1453 const wchar_t * const end = path + path_nchars;
1455 for (size_t i = 0; i < ARRAY_LEN(dirs); i++) {
1456 size_t len = wcslen(dirs[i]);
1457 if (len <= (end - path) && !wcsnicmp(path, dirs[i], len)) {
1458 first_dir_len = len;
1462 if (first_dir_len == 0)
1464 path += first_dir_len;
1465 while (path != end && *path == L'\\')
1470 /* Given a Windows NT namespace path, such as \??\e:\Windows\System32, return a
1471 * pointer to the suffix of the path that is device-relative, such as
1474 * The path has an explicit length and is not necessarily null terminated.
1476 * If the path just something like \??\e: then the returned pointer will point
1477 * just past the colon. In this case the length of the result will be 0
1479 static const wchar_t *
1480 get_device_relative_path(const wchar_t *path, size_t path_nchars)
1482 const wchar_t * const orig_path = path;
1483 const wchar_t * const end = path + path_nchars;
1485 path = skip_nt_toplevel_component(path, path_nchars);
1486 if (path == orig_path)
1489 path = wmemchr(path, L'\\', (end - path));
1494 } while (path != end && *path == L'\\');
1499 * Given a reparse point buffer for a symbolic link or junction, adjust its
1500 * contents so that the target of the link is consistent with the new location
1504 try_rpfix(u8 *rpbuf, u16 *rpbuflen_p, struct win32_apply_ctx *ctx)
1506 struct reparse_data rpdata;
1507 size_t orig_subst_name_nchars;
1508 const wchar_t *relpath;
1509 size_t relpath_nchars;
1510 size_t target_ntpath_nchars;
1511 size_t fixed_subst_name_nchars;
1512 const wchar_t *fixed_print_name;
1513 size_t fixed_print_name_nchars;
1515 if (parse_reparse_data(rpbuf, *rpbuflen_p, &rpdata)) {
1516 /* Do nothing if the reparse data is invalid. */
1520 if (rpdata.rptag == WIM_IO_REPARSE_TAG_SYMLINK &&
1521 (rpdata.rpflags & SYMBOLIC_LINK_RELATIVE))
1523 /* Do nothing if it's a relative symbolic link. */
1527 /* Build the new substitute name from the NT namespace path to the
1528 * target directory, then a path separator, then the "device relative"
1529 * part of the old substitute name. */
1531 orig_subst_name_nchars = rpdata.substitute_name_nbytes / sizeof(wchar_t);
1533 relpath = get_device_relative_path(rpdata.substitute_name,
1534 orig_subst_name_nchars);
1535 relpath_nchars = orig_subst_name_nchars -
1536 (relpath - rpdata.substitute_name);
1538 target_ntpath_nchars = ctx->target_ntpath.Length / sizeof(wchar_t);
1540 fixed_subst_name_nchars = target_ntpath_nchars;
1542 fixed_subst_name_nchars += 1 + relpath_nchars;
1543 wchar_t fixed_subst_name[fixed_subst_name_nchars];
1545 wmemcpy(fixed_subst_name, ctx->target_ntpath.Buffer,
1546 target_ntpath_nchars);
1547 if (relpath_nchars) {
1548 fixed_subst_name[target_ntpath_nchars] = L'\\';
1549 wmemcpy(&fixed_subst_name[target_ntpath_nchars + 1],
1550 relpath, relpath_nchars);
1552 /* Doesn't need to be null-terminated. */
1554 /* Print name should be Win32, but not all NT names can even be
1555 * translated to Win32 names. But we can at least delete the top-level
1556 * directory, such as \??\, and this will have the expected result in
1557 * the usual case. */
1558 fixed_print_name = skip_nt_toplevel_component(fixed_subst_name,
1559 fixed_subst_name_nchars);
1560 fixed_print_name_nchars = fixed_subst_name_nchars - (fixed_print_name -
1563 rpdata.substitute_name = fixed_subst_name;
1564 rpdata.substitute_name_nbytes = fixed_subst_name_nchars * sizeof(wchar_t);
1565 rpdata.print_name = (wchar_t *)fixed_print_name;
1566 rpdata.print_name_nbytes = fixed_print_name_nchars * sizeof(wchar_t);
1567 make_reparse_buffer(&rpdata, rpbuf, rpbuflen_p);
1570 /* Sets reparse data on the specified file. This handles "fixing" the targets
1571 * of absolute symbolic links and junctions if WIMLIB_EXTRACT_FLAG_RPFIX was
1574 set_reparse_data(const struct wim_dentry *dentry,
1575 const void *_rpbuf, u16 rpbuflen, struct win32_apply_ctx *ctx)
1577 const struct wim_inode *inode = dentry->d_inode;
1578 const void *rpbuf = _rpbuf;
1580 if ((ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX)
1581 && !inode->i_not_rpfixed
1582 && (inode->i_reparse_tag == WIM_IO_REPARSE_TAG_SYMLINK ||
1583 inode->i_reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT))
1585 memcpy(&ctx->rpfixbuf, _rpbuf, rpbuflen);
1586 try_rpfix((u8 *)&ctx->rpfixbuf, &rpbuflen, ctx);
1587 rpbuf = &ctx->rpfixbuf;
1589 return do_set_reparse_data(dentry, rpbuf, rpbuflen, ctx);
1593 /* Import the next block of raw encrypted data */
1595 import_encrypted_data(PBYTE pbData, PVOID pvCallbackContext, PULONG Length)
1597 struct win32_apply_ctx *ctx = pvCallbackContext;
1600 copy_len = min(ctx->encrypted_size - ctx->encrypted_offset, *Length);
1601 memcpy(pbData, &ctx->data_buffer[ctx->encrypted_offset], copy_len);
1602 ctx->encrypted_offset += copy_len;
1604 return ERROR_SUCCESS;
1607 /* Write the raw encrypted data to the already-created file corresponding to
1610 * The raw encrypted data is provided in ctx->data_buffer, and its size is
1611 * ctx->encrypted_size. */
1613 extract_encrypted_file(const struct wim_dentry *dentry,
1614 struct win32_apply_ctx *ctx)
1619 /* Temporarily build a Win32 path for OpenEncryptedFileRaw() */
1620 build_win32_extraction_path(dentry, ctx);
1622 err = OpenEncryptedFileRaw(ctx->pathbuf.Buffer,
1623 CREATE_FOR_IMPORT, &rawctx);
1625 /* Restore the NT namespace path */
1626 build_extraction_path(dentry, ctx);
1628 if (err != ERROR_SUCCESS) {
1629 set_errno_from_win32_error(err);
1630 ERROR_WITH_ERRNO("Can't open \"%ls\" for encrypted import "
1631 "(err=%"PRIu32")", current_path(ctx), (u32)err);
1632 return WIMLIB_ERR_OPEN;
1635 ctx->encrypted_offset = 0;
1637 err = WriteEncryptedFileRaw(import_encrypted_data, ctx, rawctx);
1639 CloseEncryptedFileRaw(rawctx);
1641 if (err != ERROR_SUCCESS) {
1642 set_errno_from_win32_error(err);
1643 ERROR_WITH_ERRNO("Can't import encrypted file \"%ls\" "
1644 "(err=%"PRIu32")", current_path(ctx), (u32)err);
1645 return WIMLIB_ERR_WRITE;
1651 /* Called when starting to read a stream for extraction on Windows */
1653 begin_extract_stream(struct wim_lookup_table_entry *stream,
1654 u32 flags, void *_ctx)
1656 struct win32_apply_ctx *ctx = _ctx;
1657 const struct stream_owner *owners = stream_owners(stream);
1660 ctx->num_open_handles = 0;
1661 ctx->data_buffer_ptr = NULL;
1662 INIT_LIST_HEAD(&ctx->reparse_dentries);
1663 INIT_LIST_HEAD(&ctx->encrypted_dentries);
1665 for (u32 i = 0; i < stream->out_refcnt; i++) {
1666 const struct wim_inode *inode = owners[i].inode;
1667 const wchar_t *stream_name = owners[i].stream_name;
1668 struct wim_dentry *dentry;
1670 /* A copy of the stream needs to be extracted to @inode. */
1672 if (ctx->common.supported_features.hard_links) {
1673 dentry = inode_first_extraction_dentry(inode);
1674 ret = begin_extract_stream_instance(stream, dentry,
1679 /* Hard links not supported. Extract the stream
1680 * separately to each alias of the inode. */
1681 struct list_head *next;
1683 next = inode->i_extraction_aliases.next;
1685 dentry = list_entry(next, struct wim_dentry,
1686 d_extraction_alias_node);
1687 ret = begin_extract_stream_instance(stream,
1694 } while (next != &inode->i_extraction_aliases);
1698 if (unlikely(ctx->num_open_handles == 0 && ctx->data_buffer_ptr == NULL)) {
1699 /* The data of this stream isn't actually needed!
1700 * (This can happen in WIMBoot mode.) */
1701 return BEGIN_STREAM_STATUS_SKIP_STREAM;
1710 /* Called when the next chunk of a stream has been read for extraction on
1713 extract_chunk(const void *chunk, size_t size, void *_ctx)
1715 struct win32_apply_ctx *ctx = _ctx;
1717 /* Write the data chunk to each open handle */
1718 for (unsigned i = 0; i < ctx->num_open_handles; i++) {
1719 u8 *bufptr = (u8 *)chunk;
1720 size_t bytes_remaining = size;
1722 while (bytes_remaining) {
1723 ULONG count = min(0xFFFFFFFF, bytes_remaining);
1725 status = (*func_NtWriteFile)(ctx->open_handles[i],
1727 &ctx->iosb, bufptr, count,
1729 if (!NT_SUCCESS(status)) {
1730 set_errno_from_nt_status(status);
1731 ERROR_WITH_ERRNO("Error writing data to target "
1732 "volume (status=0x%08"PRIx32")",
1734 return WIMLIB_ERR_WRITE;
1736 bufptr += ctx->iosb.Information;
1737 bytes_remaining -= ctx->iosb.Information;
1741 /* Copy the data chunk into the buffer (if needed) */
1742 if (ctx->data_buffer_ptr)
1743 ctx->data_buffer_ptr = mempcpy(ctx->data_buffer_ptr,
1748 /* Called when a stream has been fully read for extraction on Windows */
1750 end_extract_stream(struct wim_lookup_table_entry *stream, int status, void *_ctx)
1752 struct win32_apply_ctx *ctx = _ctx;
1754 const struct wim_dentry *dentry;
1761 if (likely(!ctx->data_buffer_ptr))
1764 if (!list_empty(&ctx->reparse_dentries)) {
1765 if (stream->size > REPARSE_DATA_MAX_SIZE) {
1766 dentry = list_first_entry(&ctx->reparse_dentries,
1767 struct wim_dentry, tmp_list);
1768 build_extraction_path(dentry, ctx);
1769 ERROR("Reparse data of \"%ls\" has size "
1770 "%"PRIu64" bytes (exceeds %u bytes)",
1771 current_path(ctx), stream->size,
1772 REPARSE_DATA_MAX_SIZE);
1773 return WIMLIB_ERR_INVALID_REPARSE_DATA;
1775 /* In the WIM format, reparse streams are just the reparse data
1776 * and omit the header. But we can reconstruct the header. */
1777 memcpy(ctx->rpbuf.rpdata, ctx->data_buffer, stream->size);
1778 ctx->rpbuf.rpdatalen = stream->size;
1779 ctx->rpbuf.rpreserved = 0;
1780 list_for_each_entry(dentry, &ctx->reparse_dentries, tmp_list) {
1781 ctx->rpbuf.rptag = dentry->d_inode->i_reparse_tag;
1782 ret = set_reparse_data(dentry, &ctx->rpbuf,
1783 stream->size + REPARSE_DATA_OFFSET,
1790 if (!list_empty(&ctx->encrypted_dentries)) {
1791 ctx->encrypted_size = stream->size;
1792 list_for_each_entry(dentry, &ctx->encrypted_dentries, tmp_list) {
1793 ret = extract_encrypted_file(dentry, ctx);
1802 /* Attributes that can't be set directly */
1803 #define SPECIAL_ATTRIBUTES \
1804 (FILE_ATTRIBUTE_REPARSE_POINT | \
1805 FILE_ATTRIBUTE_DIRECTORY | \
1806 FILE_ATTRIBUTE_ENCRYPTED | \
1807 FILE_ATTRIBUTE_SPARSE_FILE | \
1808 FILE_ATTRIBUTE_COMPRESSED)
1810 /* Set the security descriptor @desc, of @desc_size bytes, on the file with open
1813 set_security_descriptor(HANDLE h, const void *desc,
1814 size_t desc_size, struct win32_apply_ctx *ctx)
1816 SECURITY_INFORMATION info;
1819 /* We really just want to set entire the security descriptor as-is, but
1820 * all available APIs require specifying the specific parts of the
1821 * descriptor being set. Start out by requesting all parts be set. If
1822 * permissions problems are encountered, fall back to omitting some
1823 * parts (first the SACL, then the DACL, then the owner), unless the
1824 * WIMLIB_EXTRACT_FLAG_STRICT_ACLS flag has been enabled. */
1825 info = OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
1826 DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION;
1828 /* Prefer NtSetSecurityObject() to SetFileSecurity(). SetFileSecurity()
1829 * itself necessarily uses NtSetSecurityObject() as the latter is the
1830 * underlying system call for setting security information, but
1831 * SetFileSecurity() opens the handle with NtCreateFile() without
1832 * FILE_OPEN_FILE_BACKUP_INTENT. Hence, access checks are done and due
1833 * to the Windows security model, even a process running as the
1834 * Administrator can have access denied. (Of course, this not mentioned
1835 * in the MS "documentation".) */
1837 status = (*func_NtSetSecurityObject)(h, info, (PSECURITY_DESCRIPTOR)desc);
1838 if (NT_SUCCESS(status))
1840 /* Failed to set the requested parts of the security descriptor. If the
1841 * error was permissions-related, try to set fewer parts of the security
1842 * descriptor, unless WIMLIB_EXTRACT_FLAG_STRICT_ACLS is enabled. */
1843 if ((status == STATUS_PRIVILEGE_NOT_HELD ||
1844 status == STATUS_ACCESS_DENIED) &&
1845 !(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_ACLS))
1847 if (info & SACL_SECURITY_INFORMATION) {
1848 info &= ~SACL_SECURITY_INFORMATION;
1849 ctx->partial_security_descriptors++;
1852 if (info & DACL_SECURITY_INFORMATION) {
1853 info &= ~DACL_SECURITY_INFORMATION;
1856 if (info & OWNER_SECURITY_INFORMATION) {
1857 info &= ~OWNER_SECURITY_INFORMATION;
1860 /* Nothing left except GROUP, and if we removed it we
1861 * wouldn't have anything at all. */
1864 /* No part of the security descriptor could be set, or
1865 * WIMLIB_EXTRACT_FLAG_STRICT_ACLS is enabled and the full security
1866 * descriptor could not be set. */
1867 if (!(info & SACL_SECURITY_INFORMATION))
1868 ctx->partial_security_descriptors--;
1869 ctx->no_security_descriptors++;
1873 /* Set metadata on the open file @h from the WIM inode @inode. */
1875 do_apply_metadata_to_file(HANDLE h, const struct wim_inode *inode,
1876 struct win32_apply_ctx *ctx)
1878 FILE_BASIC_INFORMATION info;
1881 /* Set security descriptor if present and not in NO_ACLS mode */
1882 if (inode->i_security_id >= 0 &&
1883 !(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_NO_ACLS))
1885 const struct wim_security_data *sd;
1889 sd = wim_get_current_security_data(ctx->common.wim);
1890 desc = sd->descriptors[inode->i_security_id];
1891 desc_size = sd->sizes[inode->i_security_id];
1893 status = set_security_descriptor(h, desc, desc_size, ctx);
1894 if (!NT_SUCCESS(status) &&
1895 (ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_ACLS))
1897 set_errno_from_nt_status(status);
1898 ERROR_WITH_ERRNO("Can't set security descriptor "
1899 "on \"%ls\" (status=0x%08"PRIx32")",
1900 current_path(ctx), (u32)status);
1901 return WIMLIB_ERR_SET_SECURITY;
1905 /* Set attributes and timestamps */
1906 info.CreationTime.QuadPart = inode->i_creation_time;
1907 info.LastAccessTime.QuadPart = inode->i_last_access_time;
1908 info.LastWriteTime.QuadPart = inode->i_last_write_time;
1909 info.ChangeTime.QuadPart = 0;
1910 if (ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES)
1911 info.FileAttributes = 0;
1913 info.FileAttributes = inode->i_attributes & ~SPECIAL_ATTRIBUTES;
1915 status = (*func_NtSetInformationFile)(h, &ctx->iosb,
1916 &info, sizeof(info),
1917 FileBasicInformation);
1918 /* On FAT volumes we get STATUS_INVALID_PARAMETER if we try to set
1919 * attributes on the root directory. (Apparently because FAT doesn't
1920 * actually have a place to store those attributes!) */
1921 if (!NT_SUCCESS(status)
1922 && !(status == STATUS_INVALID_PARAMETER &&
1923 dentry_is_root(inode_first_extraction_dentry(inode))))
1925 set_errno_from_nt_status(status);
1926 ERROR_WITH_ERRNO("Can't set basic metadata on \"%ls\" "
1927 "(status=0x%08"PRIx32")",
1928 current_path(ctx), (u32)status);
1929 return WIMLIB_ERR_SET_ATTRIBUTES;
1936 apply_metadata_to_file(const struct wim_dentry *dentry,
1937 struct win32_apply_ctx *ctx)
1939 const struct wim_inode *inode = dentry->d_inode;
1945 perms = FILE_WRITE_ATTRIBUTES | WRITE_DAC |
1946 WRITE_OWNER | ACCESS_SYSTEM_SECURITY;
1948 build_extraction_path(dentry, ctx);
1950 /* Open a handle with as many relevant permissions as possible. */
1951 while (!NT_SUCCESS(status = do_create_file(&h, perms, NULL,
1952 0, FILE_OPEN, 0, ctx)))
1954 if (status == STATUS_PRIVILEGE_NOT_HELD ||
1955 status == STATUS_ACCESS_DENIED)
1957 if (perms & ACCESS_SYSTEM_SECURITY) {
1958 perms &= ~ACCESS_SYSTEM_SECURITY;
1961 if (perms & WRITE_DAC) {
1962 perms &= ~WRITE_DAC;
1965 if (perms & WRITE_OWNER) {
1966 perms &= ~WRITE_OWNER;
1970 set_errno_from_nt_status(status);
1971 ERROR_WITH_ERRNO("Can't open \"%ls\" to set metadata "
1972 "(status=0x%08"PRIx32")",
1973 current_path(ctx), (u32)status);
1974 return WIMLIB_ERR_OPEN;
1977 ret = do_apply_metadata_to_file(h, inode, ctx);
1985 apply_metadata(struct list_head *dentry_list, struct win32_apply_ctx *ctx)
1987 const struct wim_dentry *dentry;
1990 /* We go in reverse so that metadata is set on all a directory's
1991 * children before the directory itself. This avoids any potential
1992 * problems with attributes, timestamps, or security descriptors. */
1993 list_for_each_entry_reverse(dentry, dentry_list, d_extraction_list_node)
1995 ret = apply_metadata_to_file(dentry, ctx);
2002 /* Issue warnings about problems during the extraction for which warnings were
2003 * not already issued (due to the high number of potential warnings if we issued
2004 * them per-file). */
2006 do_warnings(const struct win32_apply_ctx *ctx)
2008 if (ctx->partial_security_descriptors == 0 &&
2009 ctx->no_security_descriptors == 0)
2012 WARNING("Extraction to \"%ls\" complete, but with one or more warnings:",
2013 ctx->common.target);
2014 if (ctx->partial_security_descriptors != 0) {
2015 WARNING("- Could only partially set the security descriptor\n"
2016 " on %lu files or directories.",
2017 ctx->partial_security_descriptors);
2019 if (ctx->no_security_descriptors != 0) {
2020 WARNING("- Could not set security descriptor at all\n"
2021 " on %lu files or directories.",
2022 ctx->no_security_descriptors);
2024 WARNING("To fully restore all security descriptors, run the program\n"
2025 " with Administrator rights.");
2028 /* Extract files from a WIM image to a directory on Windows */
2030 win32_extract(struct list_head *dentry_list, struct apply_ctx *_ctx)
2033 struct win32_apply_ctx *ctx = (struct win32_apply_ctx *)_ctx;
2035 ret = prepare_target(dentry_list, ctx);
2039 if (ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_WIMBOOT) {
2040 ret = start_wimboot_extraction(ctx);
2045 ret = create_directories(dentry_list, ctx);
2049 ret = create_nondirectories(dentry_list, ctx);
2053 struct read_stream_list_callbacks cbs = {
2054 .begin_stream = begin_extract_stream,
2055 .begin_stream_ctx = ctx,
2056 .consume_chunk = extract_chunk,
2057 .consume_chunk_ctx = ctx,
2058 .end_stream = end_extract_stream,
2059 .end_stream_ctx = ctx,
2061 ret = extract_stream_list(&ctx->common, &cbs);
2065 ret = apply_metadata(dentry_list, ctx);
2072 (*func_NtClose)(ctx->h_target);
2073 if (ctx->target_ntpath.Buffer)
2074 HeapFree(GetProcessHeap(), 0, ctx->target_ntpath.Buffer);
2075 FREE(ctx->pathbuf.Buffer);
2076 FREE(ctx->print_buffer);
2077 if (ctx->wimboot.prepopulate_pats) {
2078 FREE(ctx->wimboot.prepopulate_pats->strings);
2079 FREE(ctx->wimboot.prepopulate_pats);
2081 FREE(ctx->wimboot.mem_prepopulate_pats);
2082 FREE(ctx->data_buffer);
2086 const struct apply_operations win32_apply_ops = {
2088 .get_supported_features = win32_get_supported_features,
2089 .extract = win32_extract,
2090 .context_size = sizeof(struct win32_apply_ctx),
2093 #endif /* __WIN32__ */