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 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/capture.h" /* for mangle_pat() and match_pattern_list() */
32 #include "wimlib/dentry.h"
33 #include "wimlib/error.h"
34 #include "wimlib/lookup_table.h"
35 #include "wimlib/metadata.h"
36 #include "wimlib/reparse.h"
37 #include "wimlib/textfile.h"
38 #include "wimlib/xml.h"
39 #include "wimlib/wildcard.h"
40 #include "wimlib/wimboot.h"
42 struct win32_apply_ctx {
44 /* Extract flags, the pointer to the WIMStruct, etc. */
45 struct apply_ctx common;
47 /* WIMBoot information, only filled in if WIMLIB_EXTRACT_FLAG_WIMBOOT
51 struct string_set *prepopulate_pats;
52 void *mem_prepopulate_pats;
53 u8 wim_lookup_table_hash[SHA1_HASH_SIZE];
55 bool tried_to_load_prepopulate_list;
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;
128 /* Number of files for which we couldn't set the short name. */
129 unsigned long num_short_name_failures;
131 /* Have we tried to enable short name support on the target volume yet?
133 bool tried_to_enable_short_names;
136 /* Get the drive letter from a Windows path, or return the null character if the
137 * path is relative. */
139 get_drive_letter(const wchar_t *path)
141 /* Skip \\?\ prefix */
142 if (!wcsncmp(path, L"\\\\?\\", 4))
145 /* Return drive letter if valid */
146 if (((path[0] >= L'a' && path[0] <= L'z') ||
147 (path[0] >= L'A' && path[0] <= L'Z')) && path[1] == L':')
154 get_vol_flags(const wchar_t *target, DWORD *vol_flags_ret,
155 bool *short_names_supported_ret)
157 wchar_t filesystem_name[MAX_PATH + 1];
159 wchar_t *volume = NULL;
162 *short_names_supported_ret = false;
164 drive[0] = get_drive_letter(target);
172 if (!GetVolumeInformation(volume, NULL, 0, NULL, NULL,
173 vol_flags_ret, filesystem_name,
174 ARRAY_LEN(filesystem_name)))
176 DWORD err = GetLastError();
177 set_errno_from_win32_error(err);
178 WARNING_WITH_ERRNO("Failed to get volume information for "
179 "\"%ls\" (err=%"PRIu32")",
184 if (wcsstr(filesystem_name, L"NTFS")) {
185 /* FILE_SUPPORTS_HARD_LINKS is only supported on Windows 7 and
186 * later. Force it on anyway if filesystem is NTFS. */
187 *vol_flags_ret |= FILE_SUPPORTS_HARD_LINKS;
189 /* There's no volume flag for short names, but according to the
190 * MS documentation they are only user-settable on NTFS. */
191 *short_names_supported_ret = true;
195 static const wchar_t *
196 current_path(struct win32_apply_ctx *ctx);
199 build_extraction_path(const struct wim_dentry *dentry,
200 struct win32_apply_ctx *ctx);
203 win32_get_supported_features(const wchar_t *target,
204 struct wim_features *supported_features)
207 bool short_names_supported;
209 /* Query the features of the target volume. */
211 get_vol_flags(target, &vol_flags, &short_names_supported);
213 supported_features->archive_files = 1;
214 supported_features->hidden_files = 1;
215 supported_features->system_files = 1;
217 if (vol_flags & FILE_FILE_COMPRESSION)
218 supported_features->compressed_files = 1;
220 if (vol_flags & FILE_SUPPORTS_ENCRYPTION) {
221 supported_features->encrypted_files = 1;
222 supported_features->encrypted_directories = 1;
225 supported_features->not_context_indexed_files = 1;
227 /* Don't do anything with FILE_SUPPORTS_SPARSE_FILES. */
229 if (vol_flags & FILE_NAMED_STREAMS)
230 supported_features->named_data_streams = 1;
232 if (vol_flags & FILE_SUPPORTS_HARD_LINKS)
233 supported_features->hard_links = 1;
235 if (vol_flags & FILE_SUPPORTS_REPARSE_POINTS)
236 supported_features->reparse_points = 1;
238 if (vol_flags & FILE_PERSISTENT_ACLS)
239 supported_features->security_descriptors = 1;
241 if (short_names_supported)
242 supported_features->short_names = 1;
244 supported_features->timestamps = 1;
246 /* Note: Windows does not support case sensitive filenames! At least
247 * not without changing the registry and rebooting... */
252 /* Load the patterns from [PrepopulateList] of WimBootCompress.ini in the WIM
253 * image being extracted. */
255 load_prepopulate_pats(struct win32_apply_ctx *ctx)
257 const wchar_t *path = L"\\Windows\\System32\\WimBootCompress.ini";
258 struct wim_dentry *dentry;
259 struct wim_lookup_table_entry *lte;
262 struct string_set *s;
264 struct text_file_section sec;
266 ctx->wimboot.tried_to_load_prepopulate_list = true;
268 dentry = get_dentry(ctx->common.wim, path, WIMLIB_CASE_INSENSITIVE);
270 (dentry->d_inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
271 FILE_ATTRIBUTE_REPARSE_POINT |
272 FILE_ATTRIBUTE_ENCRYPTED)) ||
273 !(lte = inode_unnamed_lte(dentry->d_inode, ctx->common.wim->lookup_table)))
275 WARNING("%ls does not exist in WIM image!", path);
276 return WIMLIB_ERR_PATH_DOES_NOT_EXIST;
279 ret = read_full_stream_into_alloc_buf(lte, &buf);
283 s = CALLOC(1, sizeof(struct string_set));
286 return WIMLIB_ERR_NOMEM;
289 sec.name = T("PrepopulateList");
292 ret = do_load_text_file(path, buf, lte->size, &mem, &sec, 1,
293 LOAD_TEXT_FILE_REMOVE_QUOTES |
294 LOAD_TEXT_FILE_NO_WARNINGS,
296 BUILD_BUG_ON(OS_PREFERRED_PATH_SEPARATOR != WIM_PATH_SEPARATOR);
302 ctx->wimboot.prepopulate_pats = s;
303 ctx->wimboot.mem_prepopulate_pats = mem;
307 /* Returns %true if the specified absolute path to a file in the WIM image
308 * matches a pattern in [PrepopulateList] of WimBootCompress.ini. Otherwise
311 in_prepopulate_list(const wchar_t *path, size_t path_nchars,
312 const struct win32_apply_ctx *ctx)
314 const struct string_set *pats = ctx->wimboot.prepopulate_pats;
316 if (!pats || !pats->num_strings)
319 return match_pattern_list(path, path_nchars, pats);
322 /* Returns %true if the specified absolute path to a file in the WIM image can
323 * be subject to external backing when extracted. Otherwise returns %false. */
325 can_externally_back_path(const wchar_t *path, size_t path_nchars,
326 const struct win32_apply_ctx *ctx)
328 if (in_prepopulate_list(path, path_nchars, ctx))
331 /* Since we attempt to modify the SYSTEM registry after it's extracted
332 * (see end_wimboot_extraction()), it can't be extracted as externally
333 * backed. This extends to associated files such as SYSTEM.LOG that
334 * also must be writable in order to write to the registry. Normally,
335 * SYSTEM is in [PrepopulateList], and the SYSTEM.* files match patterns
336 * in [ExclusionList] and therefore are not captured in the WIM at all.
337 * However, a WIM that wasn't specifically captured in "WIMBoot mode"
338 * may contain SYSTEM.* files. So to make things "just work", hard-code
340 if (match_path(path, path_nchars, L"\\Windows\\System32\\config\\SYSTEM*",
341 OS_PREFERRED_PATH_SEPARATOR, false))
347 #define WIM_BACKING_NOT_ENABLED -1
348 #define WIM_BACKING_NOT_POSSIBLE -2
349 #define WIM_BACKING_EXCLUDED -3
352 will_externally_back_inode(struct wim_inode *inode, struct win32_apply_ctx *ctx,
353 const struct wim_dentry **excluded_dentry_ret)
355 struct list_head *next;
356 struct wim_dentry *dentry;
357 struct wim_lookup_table_entry *stream;
360 if (inode->i_can_externally_back)
363 /* This may do redundant checks because the cached value
364 * i_can_externally_back is 2-state (as opposed to 3-state:
365 * unknown/no/yes). But most files can be externally backed, so this
368 if (inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
369 FILE_ATTRIBUTE_REPARSE_POINT |
370 FILE_ATTRIBUTE_ENCRYPTED))
371 return WIM_BACKING_NOT_POSSIBLE;
373 stream = inode_unnamed_lte_resolved(inode);
376 stream->resource_location != RESOURCE_IN_WIM ||
377 stream->rspec->wim != ctx->common.wim ||
378 stream->size != stream->rspec->uncompressed_size)
379 return WIM_BACKING_NOT_POSSIBLE;
382 * We need to check the patterns in [PrepopulateList] against every name
383 * of the inode, in case any of them match.
385 next = inode->i_extraction_aliases.next;
387 dentry = list_entry(next, struct wim_dentry,
388 d_extraction_alias_node);
390 ret = calculate_dentry_full_path(dentry);
394 if (!can_externally_back_path(dentry->_full_path,
395 wcslen(dentry->_full_path), ctx))
397 if (excluded_dentry_ret)
398 *excluded_dentry_ret = dentry;
399 return WIM_BACKING_EXCLUDED;
402 } while (next != &inode->i_extraction_aliases);
404 inode->i_can_externally_back = 1;
409 * Determines if the unnamed data stream of a file will be created as an
410 * external backing, as opposed to a standard extraction.
413 win32_will_externally_back(struct wim_dentry *dentry, struct apply_ctx *_ctx)
415 struct win32_apply_ctx *ctx = (struct win32_apply_ctx *)_ctx;
417 if (!(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_WIMBOOT))
418 return WIM_BACKING_NOT_ENABLED;
420 if (!ctx->wimboot.tried_to_load_prepopulate_list)
421 if (load_prepopulate_pats(ctx) == WIMLIB_ERR_NOMEM)
422 return WIMLIB_ERR_NOMEM;
424 return will_externally_back_inode(dentry->d_inode, ctx, NULL);
428 set_external_backing(HANDLE h, struct wim_inode *inode, struct win32_apply_ctx *ctx)
431 const struct wim_dentry *excluded_dentry;
433 ret = will_externally_back_inode(inode, ctx, &excluded_dentry);
434 if (ret > 0) /* Error. */
437 if (ret < 0 && ret != WIM_BACKING_EXCLUDED)
438 return 0; /* Not externally backing, other than due to exclusion. */
440 if (unlikely(ret == WIM_BACKING_EXCLUDED)) {
441 /* Not externally backing due to exclusion. */
442 union wimlib_progress_info info;
444 build_extraction_path(excluded_dentry, ctx);
446 info.wimboot_exclude.path_in_wim = excluded_dentry->_full_path;
447 info.wimboot_exclude.extraction_path = current_path(ctx);
449 return call_progress(ctx->common.progfunc,
450 WIMLIB_PROGRESS_MSG_WIMBOOT_EXCLUDE,
451 &info, ctx->common.progctx);
453 /* Externally backing. */
454 if (unlikely(!wimboot_set_pointer(h,
455 inode_unnamed_lte_resolved(inode),
456 ctx->wimboot.data_source_id,
457 ctx->wimboot.wim_lookup_table_hash,
458 ctx->wimboot.wof_running)))
460 const DWORD err = GetLastError();
462 build_extraction_path(inode_first_extraction_dentry(inode), ctx);
463 set_errno_from_win32_error(err);
464 ERROR_WITH_ERRNO("\"%ls\": Couldn't set WIMBoot "
465 "pointer data (err=%"PRIu32")",
466 current_path(ctx), (u32)err);
467 return WIMLIB_ERR_WIMBOOT;
473 /* Calculates the SHA-1 message digest of the WIM's lookup table. */
475 hash_lookup_table(WIMStruct *wim, u8 hash[SHA1_HASH_SIZE])
477 return wim_reshdr_to_hash(&wim->hdr.lookup_table_reshdr, wim, hash);
480 /* Prepare for doing a "WIMBoot" extraction by loading patterns from
481 * [PrepopulateList] of WimBootCompress.ini and allocating a WOF data source ID
482 * on the target volume. */
484 start_wimboot_extraction(struct win32_apply_ctx *ctx)
487 WIMStruct *wim = ctx->common.wim;
489 if (!ctx->wimboot.tried_to_load_prepopulate_list)
490 if (load_prepopulate_pats(ctx) == WIMLIB_ERR_NOMEM)
491 return WIMLIB_ERR_NOMEM;
493 if (!wim_info_get_wimboot(wim->wim_info, wim->current_image))
494 WARNING("Image is not marked as WIMBoot compatible!");
496 ret = hash_lookup_table(ctx->common.wim,
497 ctx->wimboot.wim_lookup_table_hash);
501 return wimboot_alloc_data_source_id(wim->filename,
505 &ctx->wimboot.data_source_id,
506 &ctx->wimboot.wof_running);
510 build_win32_extraction_path(const struct wim_dentry *dentry,
511 struct win32_apply_ctx *ctx);
513 /* Sets WimBoot=1 in the extracted SYSTEM registry hive.
515 * WIMGAPI does this, and it's possible that it's important.
516 * But I don't know exactly what this value means to Windows. */
518 end_wimboot_extraction(struct win32_apply_ctx *ctx)
520 struct wim_dentry *dentry;
521 wchar_t subkeyname[32];
527 dentry = get_dentry(ctx->common.wim, L"\\Windows\\System32\\config\\SYSTEM",
528 WIMLIB_CASE_INSENSITIVE);
530 if (!dentry || !will_extract_dentry(dentry))
533 if (!will_extract_dentry(wim_get_current_root_dentry(ctx->common.wim)))
536 /* Not bothering to use the native routines (e.g. NtLoadKey()) for this.
537 * If this doesn't work, you probably also have many other problems. */
539 build_win32_extraction_path(dentry, ctx);
541 randomize_char_array_with_alnum(subkeyname, 20);
542 subkeyname[20] = L'\0';
544 res = RegLoadKey(HKEY_LOCAL_MACHINE, subkeyname, ctx->pathbuf.Buffer);
548 wcscpy(&subkeyname[20], L"\\Setup");
550 res = RegCreateKeyEx(HKEY_LOCAL_MACHINE, subkeyname, 0, NULL,
551 REG_OPTION_BACKUP_RESTORE, 0, NULL, &key, NULL);
557 res = RegSetValueEx(key, L"WimBoot", 0, REG_DWORD,
558 (const BYTE *)&value, sizeof(DWORD));
562 res = RegFlushKey(key);
565 res2 = RegCloseKey(key);
569 subkeyname[20] = L'\0';
570 RegUnLoadKey(HKEY_LOCAL_MACHINE, subkeyname);
574 set_errno_from_win32_error(res);
575 WARNING_WITH_ERRNO("Failed to set \\Setup: dword \"WimBoot\"=1 value "
576 "in registry hive \"%ls\" (err=%"PRIu32")",
577 ctx->pathbuf.Buffer, (u32)res);
583 /* Returns the number of wide characters needed to represent the path to the
584 * specified @dentry, relative to the target directory, when extracted.
586 * Does not include null terminator (not needed for NtCreateFile). */
588 dentry_extraction_path_length(const struct wim_dentry *dentry)
591 const struct wim_dentry *d;
595 len += d->d_extraction_name_nchars + 1;
597 } while (!dentry_is_root(d) && will_extract_dentry(d));
599 return --len; /* No leading slash */
602 /* Returns the length of the longest string that might need to be appended to
603 * the path to an alias of an inode to open or create a named data stream.
605 * If the inode has no named data streams, this will be 0. Otherwise, this will
606 * be 1 plus the length of the longest-named data stream, since the data stream
607 * name must be separated from the path by the ':' character. */
609 inode_longest_named_data_stream_spec(const struct wim_inode *inode)
612 for (u16 i = 0; i < inode->i_num_ads; i++) {
613 size_t len = inode->i_ads_entries[i].stream_name_nbytes;
618 max = 1 + (max / sizeof(wchar_t));
622 /* Find the length, in wide characters, of the longest path needed for
623 * extraction of any file in @dentry_list relative to the target directory.
625 * Accounts for named data streams, but does not include null terminator (not
626 * needed for NtCreateFile). */
628 compute_path_max(struct list_head *dentry_list)
631 const struct wim_dentry *dentry;
633 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
636 len = dentry_extraction_path_length(dentry);
638 /* Account for named data streams */
639 len += inode_longest_named_data_stream_spec(dentry->d_inode);
648 /* Build the path at which to extract the @dentry, relative to the target
651 * The path is saved in ctx->pathbuf. */
653 build_extraction_path(const struct wim_dentry *dentry,
654 struct win32_apply_ctx *ctx)
658 const struct wim_dentry *d;
660 len = dentry_extraction_path_length(dentry);
662 ctx->pathbuf.Length = len * sizeof(wchar_t);
663 p = ctx->pathbuf.Buffer + len;
665 !dentry_is_root(d->d_parent) && will_extract_dentry(d->d_parent);
668 p -= d->d_extraction_name_nchars;
669 wmemcpy(p, d->d_extraction_name, d->d_extraction_name_nchars);
672 /* No leading slash */
673 p -= d->d_extraction_name_nchars;
674 wmemcpy(p, d->d_extraction_name, d->d_extraction_name_nchars);
677 /* Build the path at which to extract the @dentry, relative to the target
678 * directory, adding the suffix for a named data stream.
680 * The path is saved in ctx->pathbuf. */
682 build_extraction_path_with_ads(const struct wim_dentry *dentry,
683 struct win32_apply_ctx *ctx,
684 const wchar_t *stream_name,
685 size_t stream_name_nchars)
689 build_extraction_path(dentry, ctx);
691 /* Add :NAME for named data stream */
692 p = ctx->pathbuf.Buffer + (ctx->pathbuf.Length / sizeof(wchar_t));
694 wmemcpy(p, stream_name, stream_name_nchars);
695 ctx->pathbuf.Length += (1 + stream_name_nchars) * sizeof(wchar_t);
698 /* Build the Win32 namespace path to the specified @dentry when extracted.
700 * The path is saved in ctx->pathbuf and will be null terminated.
702 * XXX: We could get rid of this if it wasn't needed for the file encryption
703 * APIs, and the registry manipulation in WIMBoot mode. */
705 build_win32_extraction_path(const struct wim_dentry *dentry,
706 struct win32_apply_ctx *ctx)
708 build_extraction_path(dentry, ctx);
710 /* Prepend target_ntpath to our relative path, then change \??\ into \\?\ */
712 memmove(ctx->pathbuf.Buffer +
713 (ctx->target_ntpath.Length / sizeof(wchar_t)) + 1,
714 ctx->pathbuf.Buffer, ctx->pathbuf.Length);
715 memcpy(ctx->pathbuf.Buffer, ctx->target_ntpath.Buffer,
716 ctx->target_ntpath.Length);
717 ctx->pathbuf.Buffer[ctx->target_ntpath.Length / sizeof(wchar_t)] = L'\\';
718 ctx->pathbuf.Length += ctx->target_ntpath.Length + sizeof(wchar_t);
719 ctx->pathbuf.Buffer[ctx->pathbuf.Length / sizeof(wchar_t)] = L'\0';
721 wimlib_assert(ctx->pathbuf.Length >= 4 * sizeof(wchar_t) &&
722 !wmemcmp(ctx->pathbuf.Buffer, L"\\??\\", 4));
724 ctx->pathbuf.Buffer[1] = L'\\';
728 /* Returns a "printable" representation of the last relative NT path that was
729 * constructed with build_extraction_path() or build_extraction_path_with_ads().
731 * This will be overwritten by the next call to this function. */
732 static const wchar_t *
733 current_path(struct win32_apply_ctx *ctx)
735 wchar_t *p = ctx->print_buffer;
737 p = wmempcpy(p, ctx->common.target, ctx->common.target_nchars);
739 p = wmempcpy(p, ctx->pathbuf.Buffer, ctx->pathbuf.Length / sizeof(wchar_t));
741 return ctx->print_buffer;
745 * Ensures the target directory exists and opens a handle to it, in preparation
746 * of using paths relative to it.
749 prepare_target(struct list_head *dentry_list, struct win32_apply_ctx *ctx)
755 /* Open handle to the target directory (possibly creating it). */
757 ret = win32_path_to_nt_path(ctx->common.target, &ctx->target_ntpath);
761 ctx->attr.Length = sizeof(ctx->attr);
762 ctx->attr.ObjectName = &ctx->target_ntpath;
764 status = (*func_NtCreateFile)(&ctx->h_target,
770 FILE_SHARE_VALID_FLAGS,
772 FILE_DIRECTORY_FILE |
773 FILE_OPEN_REPARSE_POINT |
774 FILE_OPEN_FOR_BACKUP_INTENT,
778 if (!NT_SUCCESS(status)) {
779 set_errno_from_nt_status(status);
780 ERROR_WITH_ERRNO("Can't open or create directory \"%ls\" "
781 "(status=0x%08"PRIx32")",
782 ctx->common.target, (u32)status);
783 return WIMLIB_ERR_OPENDIR;
786 path_max = compute_path_max(dentry_list);
788 /* Add some extra for building Win32 paths for the file encryption APIs
790 path_max += 2 + (ctx->target_ntpath.Length / sizeof(wchar_t));
792 ctx->pathbuf.MaximumLength = path_max * sizeof(wchar_t);
793 ctx->pathbuf.Buffer = MALLOC(ctx->pathbuf.MaximumLength);
794 if (!ctx->pathbuf.Buffer)
795 return WIMLIB_ERR_NOMEM;
797 ctx->attr.RootDirectory = ctx->h_target;
798 ctx->attr.ObjectName = &ctx->pathbuf;
800 ctx->print_buffer = MALLOC((ctx->common.target_nchars + 1 + path_max + 1) *
802 if (!ctx->print_buffer)
803 return WIMLIB_ERR_NOMEM;
808 /* When creating an inode that will have a short (DOS) name, we create it using
809 * the long name associated with the short name. This ensures that the short
810 * name gets associated with the correct long name. */
811 static struct wim_dentry *
812 first_extraction_alias(const struct wim_inode *inode)
814 struct list_head *next = inode->i_extraction_aliases.next;
815 struct wim_dentry *dentry;
818 dentry = list_entry(next, struct wim_dentry,
819 d_extraction_alias_node);
820 if (dentry_has_short_name(dentry))
823 } while (next != &inode->i_extraction_aliases);
828 * Set or clear FILE_ATTRIBUTE_COMPRESSED if the inherited value is different
829 * from the desired value.
831 * Note that you can NOT override the inherited value of
832 * FILE_ATTRIBUTE_COMPRESSED directly with NtCreateFile().
835 adjust_compression_attribute(HANDLE h, const struct wim_dentry *dentry,
836 struct win32_apply_ctx *ctx)
838 const bool compressed = (dentry->d_inode->i_attributes &
839 FILE_ATTRIBUTE_COMPRESSED);
841 if (ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES)
844 if (!ctx->common.supported_features.compressed_files)
847 FILE_BASIC_INFORMATION info;
849 USHORT compression_state;
851 /* Get current attributes */
852 status = (*func_NtQueryInformationFile)(h, &ctx->iosb,
854 FileBasicInformation);
855 if (NT_SUCCESS(status) &&
856 compressed == !!(info.FileAttributes & FILE_ATTRIBUTE_COMPRESSED))
858 /* Nothing needs to be done. */
862 /* Set the new compression state */
865 compression_state = COMPRESSION_FORMAT_DEFAULT;
867 compression_state = COMPRESSION_FORMAT_NONE;
869 status = (*func_NtFsControlFile)(h,
874 FSCTL_SET_COMPRESSION,
879 if (NT_SUCCESS(status))
882 set_errno_from_nt_status(status);
883 ERROR_WITH_ERRNO("Can't %s compression attribute on \"%ls\" "
884 "(status=0x%08"PRIx32")",
885 (compressed ? "set" : "clear"),
886 current_path(ctx), status);
887 return WIMLIB_ERR_SET_ATTRIBUTES;
891 * Clear FILE_ATTRIBUTE_ENCRYPTED if the file or directory is not supposed to be
894 * You can provide FILE_ATTRIBUTE_ENCRYPTED to NtCreateFile() to set it on the
895 * created file. However, the file or directory will otherwise default to the
896 * encryption state of the parent directory. This function works around this
897 * limitation by using DecryptFile() to remove FILE_ATTRIBUTE_ENCRYPTED on files
898 * (and directories) that are not supposed to have it set.
900 * Regardless of whether it succeeds or fails, this function may close the
901 * handle to the file. If it does, it sets it to NULL.
904 maybe_clear_encryption_attribute(HANDLE *h_ptr, const struct wim_dentry *dentry,
905 struct win32_apply_ctx *ctx)
907 if (dentry->d_inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED)
910 if (ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES)
913 if (!ctx->common.supported_features.encrypted_files)
916 FILE_BASIC_INFORMATION info;
920 /* Get current attributes */
921 status = (*func_NtQueryInformationFile)(*h_ptr, &ctx->iosb,
923 FileBasicInformation);
924 if (NT_SUCCESS(status) &&
925 !(info.FileAttributes & FILE_ATTRIBUTE_ENCRYPTED))
927 /* Nothing needs to be done. */
931 /* Set the new encryption state */
933 /* Due to Windows' crappy file encryption APIs, we need to close the
934 * handle to the file so we don't get ERROR_SHARING_VIOLATION. We also
935 * hack together a Win32 path, although we will use the \\?\ prefix so
936 * it will actually be a NT path in disguise... */
937 (*func_NtClose)(*h_ptr);
940 build_win32_extraction_path(dentry, ctx);
942 bret = DecryptFile(ctx->pathbuf.Buffer, 0);
944 /* Restore the NT namespace path */
945 build_extraction_path(dentry, ctx);
948 DWORD err = GetLastError();
949 set_errno_from_win32_error(err);
950 ERROR_WITH_ERRNO("Can't decrypt file \"%ls\" (err=%"PRIu32")",
951 current_path(ctx), (u32)err);
952 return WIMLIB_ERR_SET_ATTRIBUTES;
957 /* Try to enable short name support on the target volume. If successful, return
958 * true. If unsuccessful, issue a warning and return false. */
960 try_to_enable_short_names(const wchar_t *volume)
963 FILE_FS_PERSISTENT_VOLUME_INFORMATION info;
967 h = CreateFile(volume, GENERIC_WRITE,
968 FILE_SHARE_VALID_FLAGS, NULL, OPEN_EXISTING,
969 FILE_FLAG_BACKUP_SEMANTICS, NULL);
970 if (h == INVALID_HANDLE_VALUE)
973 info.VolumeFlags = 0;
974 info.FlagMask = PERSISTENT_VOLUME_STATE_SHORT_NAME_CREATION_DISABLED;
978 bret = DeviceIoControl(h, FSCTL_SET_PERSISTENT_VOLUME_STATE,
979 &info, sizeof(info), NULL, 0,
980 &bytesReturned, NULL);
989 WARNING("Failed to enable short name support on %ls "
990 "(err=%"PRIu32")", volume + 4, (u32)GetLastError());
994 /* Set the short name on the open file @h which has been created at the location
995 * indicated by @dentry.
997 * Note that this may add, change, or remove the short name.
999 * @h must be opened with DELETE access.
1001 * Returns 0 or WIMLIB_ERR_SET_SHORT_NAME. The latter only happens in
1002 * STRICT_SHORT_NAMES mode.
1005 set_short_name(HANDLE h, const struct wim_dentry *dentry,
1006 struct win32_apply_ctx *ctx)
1009 if (!ctx->common.supported_features.short_names)
1013 * Note: The size of the FILE_NAME_INFORMATION buffer must be such that
1014 * FileName contains at least 2 wide characters (4 bytes). Otherwise,
1015 * NtSetInformationFile() will return STATUS_INFO_LENGTH_MISMATCH. This
1016 * is despite the fact that FileNameLength can validly be 0 or 2 bytes,
1017 * with the former case being removing the existing short name if
1018 * present, rather than setting one.
1020 * The null terminator is seemingly optional, but to be safe we include
1021 * space for it and zero all unused space.
1024 size_t bufsize = offsetof(FILE_NAME_INFORMATION, FileName) +
1025 max(dentry->short_name_nbytes, sizeof(wchar_t)) +
1027 u8 buf[bufsize] _aligned_attribute(8);
1028 FILE_NAME_INFORMATION *info = (FILE_NAME_INFORMATION *)buf;
1031 memset(buf, 0, bufsize);
1033 info->FileNameLength = dentry->short_name_nbytes;
1034 memcpy(info->FileName, dentry->short_name, dentry->short_name_nbytes);
1037 status = (*func_NtSetInformationFile)(h, &ctx->iosb, info, bufsize,
1038 FileShortNameInformation);
1039 if (NT_SUCCESS(status))
1042 if (status == STATUS_SHORT_NAMES_NOT_ENABLED_ON_VOLUME) {
1043 if (dentry->short_name_nbytes == 0)
1045 if (!ctx->tried_to_enable_short_names) {
1049 ctx->tried_to_enable_short_names = true;
1051 ret = win32_get_drive_path(ctx->common.target,
1055 if (try_to_enable_short_names(volume))
1060 /* By default, failure to set short names is not an error (since short
1061 * names aren't too important anymore...). */
1062 if (!(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_SHORT_NAMES)) {
1063 ctx->num_short_name_failures++;
1067 if (status == STATUS_SHORT_NAMES_NOT_ENABLED_ON_VOLUME) {
1068 ERROR("Can't set short name when short "
1069 "names are not enabled on the volume!");
1071 ERROR("Can't set short name on \"%ls\" (status=0x%08"PRIx32")",
1072 current_path(ctx), (u32)status);
1074 return WIMLIB_ERR_SET_SHORT_NAME;
1078 * A wrapper around NtCreateFile() to make it slightly more usable...
1079 * This uses the path currently constructed in ctx->pathbuf.
1081 * Also, we always specify FILE_OPEN_FOR_BACKUP_INTENT and
1082 * FILE_OPEN_REPARSE_POINT.
1085 do_create_file(PHANDLE FileHandle,
1086 ACCESS_MASK DesiredAccess,
1087 PLARGE_INTEGER AllocationSize,
1088 ULONG FileAttributes,
1089 ULONG CreateDisposition,
1090 ULONG CreateOptions,
1091 struct win32_apply_ctx *ctx)
1093 return (*func_NtCreateFile)(FileHandle,
1099 FILE_SHARE_VALID_FLAGS,
1102 FILE_OPEN_FOR_BACKUP_INTENT |
1103 FILE_OPEN_REPARSE_POINT,
1108 /* Like do_create_file(), but builds the extraction path of the @dentry first.
1111 create_file(PHANDLE FileHandle,
1112 ACCESS_MASK DesiredAccess,
1113 PLARGE_INTEGER AllocationSize,
1114 ULONG FileAttributes,
1115 ULONG CreateDisposition,
1116 ULONG CreateOptions,
1117 const struct wim_dentry *dentry,
1118 struct win32_apply_ctx *ctx)
1120 build_extraction_path(dentry, ctx);
1121 return do_create_file(FileHandle,
1130 /* Create empty named data streams.
1132 * Since these won't have 'struct wim_lookup_table_entry's, they won't show up
1133 * in the call to extract_stream_list(). Hence the need for the special case.
1136 create_any_empty_ads(const struct wim_dentry *dentry,
1137 struct win32_apply_ctx *ctx)
1139 const struct wim_inode *inode = dentry->d_inode;
1140 LARGE_INTEGER allocation_size;
1141 bool path_modified = false;
1144 if (!ctx->common.supported_features.named_data_streams)
1147 for (u16 i = 0; i < inode->i_num_ads; i++) {
1148 const struct wim_ads_entry *entry;
1152 entry = &inode->i_ads_entries[i];
1155 if (!entry->stream_name_nbytes)
1162 /* Probably setting the allocation size to 0 has no effect, but
1163 * we might as well try. */
1164 allocation_size.QuadPart = 0;
1166 build_extraction_path_with_ads(dentry, ctx,
1168 entry->stream_name_nbytes /
1170 path_modified = true;
1171 status = do_create_file(&h, FILE_WRITE_DATA, &allocation_size,
1172 0, FILE_SUPERSEDE, 0, ctx);
1173 if (!NT_SUCCESS(status)) {
1174 set_errno_from_nt_status(status);
1175 ERROR_WITH_ERRNO("Can't create \"%ls\" "
1176 "(status=0x%08"PRIx32")",
1177 current_path(ctx), (u32)status);
1178 ret = WIMLIB_ERR_OPEN;
1183 /* Restore the path to the dentry itself */
1185 build_extraction_path(dentry, ctx);
1190 * Creates the directory named by @dentry, or uses an existing directory at that
1191 * location. If necessary, sets the short name and/or fixes compression and
1192 * encryption attributes.
1194 * Returns 0, WIMLIB_ERR_MKDIR, or WIMLIB_ERR_SET_SHORT_NAME.
1197 create_directory(const struct wim_dentry *dentry,
1198 struct win32_apply_ctx *ctx)
1205 /* Special attributes:
1207 * Use FILE_ATTRIBUTE_ENCRYPTED if the directory needs to have it set.
1208 * This doesn't work for FILE_ATTRIBUTE_COMPRESSED (unfortunately).
1210 * Don't specify FILE_ATTRIBUTE_DIRECTORY; it gets set anyway as a
1211 * result of the FILE_DIRECTORY_FILE option. */
1212 attrib = (dentry->d_inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED);
1214 /* DELETE is needed for set_short_name().
1215 * GENERIC_READ and GENERIC_WRITE are needed for
1216 * adjust_compression_attribute(). */
1217 status = create_file(&h, GENERIC_READ | GENERIC_WRITE | DELETE, NULL,
1218 attrib, FILE_OPEN_IF, FILE_DIRECTORY_FILE,
1220 if (!NT_SUCCESS(status)) {
1221 set_errno_from_nt_status(status);
1222 ERROR_WITH_ERRNO("Can't create directory \"%ls\" "
1223 "(status=0x%08"PRIx32")",
1224 current_path(ctx), (u32)status);
1225 return WIMLIB_ERR_MKDIR;
1228 ret = set_short_name(h, dentry, ctx);
1231 ret = adjust_compression_attribute(h, dentry, ctx);
1234 ret = maybe_clear_encryption_attribute(&h, dentry, ctx);
1235 /* May close the handle!!! */
1243 * Create all the directories being extracted, other than the target directory
1246 * Note: we don't honor directory hard links. However, we don't allow them to
1247 * exist in WIM images anyway (see inode_fixup.c).
1250 create_directories(struct list_head *dentry_list,
1251 struct win32_apply_ctx *ctx)
1253 const struct wim_dentry *dentry;
1256 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
1258 if (!(dentry->d_inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY))
1261 /* Note: Here we include files with
1262 * FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_REPARSE_POINT, but we
1263 * wait until later to actually set the reparse data. */
1265 /* If the root dentry is being extracted, it was already done so
1266 * in prepare_target(). */
1267 if (!dentry_is_root(dentry)) {
1268 ret = create_directory(dentry, ctx);
1272 ret = create_any_empty_ads(dentry, ctx);
1277 ret = report_file_created(&ctx->common);
1285 * Creates the nondirectory file named by @dentry.
1287 * On success, returns an open handle to the file in @h_ret, with GENERIC_READ,
1288 * GENERIC_WRITE, and DELETE access. Also, the path to the file will be saved
1289 * in ctx->pathbuf. On failure, returns WIMLIB_ERR_OPEN.
1292 create_nondirectory_inode(HANDLE *h_ret, const struct wim_dentry *dentry,
1293 struct win32_apply_ctx *ctx)
1295 const struct wim_inode *inode;
1298 bool retried = false;
1300 inode = dentry->d_inode;
1302 /* If the file already exists and has FILE_ATTRIBUTE_SYSTEM and/or
1303 * FILE_ATTRIBUTE_HIDDEN, these must be specified in order to supersede
1306 * Normally the user shouldn't be trying to overwrite such files anyway,
1307 * but we at least provide FILE_ATTRIBUTE_SYSTEM and
1308 * FILE_ATTRIBUTE_HIDDEN if the WIM inode has those attributes so that
1309 * we catch the case where the user extracts the same files to the same
1310 * location more than one time.
1312 * Also specify FILE_ATTRIBUTE_ENCRYPTED if the file needs to be
1315 * In NO_ATTRIBUTES mode just don't specify any attributes at all.
1317 if (ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES) {
1320 attrib = (inode->i_attributes & (FILE_ATTRIBUTE_SYSTEM |
1321 FILE_ATTRIBUTE_HIDDEN |
1322 FILE_ATTRIBUTE_ENCRYPTED));
1324 build_extraction_path(dentry, ctx);
1326 status = do_create_file(h_ret, GENERIC_READ | GENERIC_WRITE | DELETE,
1327 NULL, attrib, FILE_SUPERSEDE,
1328 FILE_NON_DIRECTORY_FILE, ctx);
1329 if (NT_SUCCESS(status)) {
1332 ret = adjust_compression_attribute(*h_ret, dentry, ctx);
1334 (*func_NtClose)(*h_ret);
1338 ret = maybe_clear_encryption_attribute(h_ret, dentry, ctx);
1339 /* May close the handle!!! */
1343 (*func_NtClose)(*h_ret);
1348 /* Re-open the handle so that we can return it on
1350 status = do_create_file(h_ret,
1352 GENERIC_WRITE | DELETE,
1354 FILE_NON_DIRECTORY_FILE, ctx);
1355 if (!NT_SUCCESS(status))
1359 ret = create_any_empty_ads(dentry, ctx);
1361 (*func_NtClose)(*h_ret);
1367 if (status == STATUS_ACCESS_DENIED && !retried) {
1368 /* We also can't supersede an existing file that has
1369 * FILE_ATTRIBUTE_READONLY set; doing so causes NtCreateFile()
1370 * to return STATUS_ACCESS_DENIED . The only workaround seems
1371 * to be to explicitly remove FILE_ATTRIBUTE_READONLY on the
1372 * existing file, then try again. */
1374 FILE_BASIC_INFORMATION info;
1377 status = do_create_file(&h, FILE_WRITE_ATTRIBUTES, NULL, 0,
1378 FILE_OPEN, FILE_NON_DIRECTORY_FILE, ctx);
1379 if (!NT_SUCCESS(status))
1382 memset(&info, 0, sizeof(info));
1383 info.FileAttributes = FILE_ATTRIBUTE_NORMAL;
1385 status = (*func_NtSetInformationFile)(h, &ctx->iosb,
1386 &info, sizeof(info),
1387 FileBasicInformation);
1389 if (!NT_SUCCESS(status))
1395 set_errno_from_nt_status(status);
1396 ERROR_WITH_ERRNO("Can't create file \"%ls\" (status=0x%08"PRIx32")",
1397 current_path(ctx), (u32)status);
1398 return WIMLIB_ERR_OPEN;
1401 /* Creates a hard link at the location named by @dentry to the file represented
1402 * by the open handle @h. Or, if the target volume does not support hard links,
1403 * create a separate file instead. */
1405 create_link(HANDLE h, const struct wim_dentry *dentry,
1406 struct win32_apply_ctx *ctx)
1408 if (ctx->common.supported_features.hard_links) {
1410 build_extraction_path(dentry, ctx);
1412 size_t bufsize = offsetof(FILE_LINK_INFORMATION, FileName) +
1413 ctx->pathbuf.Length + sizeof(wchar_t);
1414 u8 buf[bufsize] _aligned_attribute(8);
1415 FILE_LINK_INFORMATION *info = (FILE_LINK_INFORMATION *)buf;
1418 info->ReplaceIfExists = TRUE;
1419 info->RootDirectory = ctx->attr.RootDirectory;
1420 info->FileNameLength = ctx->pathbuf.Length;
1421 memcpy(info->FileName, ctx->pathbuf.Buffer, ctx->pathbuf.Length);
1422 info->FileName[info->FileNameLength / 2] = L'\0';
1424 /* Note: the null terminator isn't actually necessary,
1425 * but if you don't add the extra character, you get
1426 * STATUS_INFO_LENGTH_MISMATCH when FileNameLength
1427 * happens to be 2 */
1429 status = (*func_NtSetInformationFile)(h, &ctx->iosb,
1431 FileLinkInformation);
1432 if (NT_SUCCESS(status))
1434 ERROR("Failed to create link \"%ls\" (status=0x%08"PRIx32")",
1435 current_path(ctx), (u32)status);
1436 return WIMLIB_ERR_LINK;
1441 ret = create_nondirectory_inode(&h2, dentry, ctx);
1445 (*func_NtClose)(h2);
1450 /* Given an inode (represented by the open handle @h) for which one link has
1451 * been created (named by @first_dentry), create the other links.
1453 * Or, if the target volume does not support hard links, create separate files.
1455 * Note: This uses ctx->pathbuf and does not reset it.
1458 create_links(HANDLE h, const struct wim_dentry *first_dentry,
1459 struct win32_apply_ctx *ctx)
1461 const struct wim_inode *inode;
1462 const struct list_head *next;
1463 const struct wim_dentry *dentry;
1466 inode = first_dentry->d_inode;
1467 next = inode->i_extraction_aliases.next;
1469 dentry = list_entry(next, struct wim_dentry,
1470 d_extraction_alias_node);
1471 if (dentry != first_dentry) {
1472 ret = create_link(h, dentry, ctx);
1477 } while (next != &inode->i_extraction_aliases);
1481 /* Create a nondirectory file, including all links. */
1483 create_nondirectory(struct wim_inode *inode, struct win32_apply_ctx *ctx)
1485 struct wim_dentry *first_dentry;
1489 first_dentry = first_extraction_alias(inode);
1491 /* Create first link. */
1492 ret = create_nondirectory_inode(&h, first_dentry, ctx);
1496 /* Set short name. */
1497 ret = set_short_name(h, first_dentry, ctx);
1499 /* Create additional links, OR if hard links are not supported just
1500 * create more files. */
1502 ret = create_links(h, first_dentry, ctx);
1504 /* "WIMBoot" extraction: set external backing by the WIM file if needed. */
1505 if (!ret && unlikely(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_WIMBOOT))
1506 ret = set_external_backing(h, inode, ctx);
1512 /* Create all the nondirectory files being extracted, including all aliases
1515 create_nondirectories(struct list_head *dentry_list, struct win32_apply_ctx *ctx)
1517 struct wim_dentry *dentry;
1518 struct wim_inode *inode;
1521 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
1522 inode = dentry->d_inode;
1523 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)
1525 /* Call create_nondirectory() only once per inode */
1526 if (dentry == inode_first_extraction_dentry(inode)) {
1527 ret = create_nondirectory(inode, ctx);
1531 ret = report_file_created(&ctx->common);
1539 close_handles(struct win32_apply_ctx *ctx)
1541 for (unsigned i = 0; i < ctx->num_open_handles; i++)
1542 (*func_NtClose)(ctx->open_handles[i]);
1545 /* Prepare to read the next stream, which has size @stream_size, into an
1546 * in-memory buffer. */
1548 prepare_data_buffer(struct win32_apply_ctx *ctx, u64 stream_size)
1550 if (stream_size > ctx->data_buffer_size) {
1551 /* Larger buffer needed. */
1553 if ((size_t)stream_size != stream_size)
1554 return WIMLIB_ERR_NOMEM;
1555 new_buffer = REALLOC(ctx->data_buffer, stream_size);
1557 return WIMLIB_ERR_NOMEM;
1558 ctx->data_buffer = new_buffer;
1559 ctx->data_buffer_size = stream_size;
1561 /* On the first call this changes data_buffer_ptr from NULL, which tells
1562 * extract_chunk() that the data buffer needs to be filled while reading
1563 * the stream data. */
1564 ctx->data_buffer_ptr = ctx->data_buffer;
1569 begin_extract_stream_instance(const struct wim_lookup_table_entry *stream,
1570 struct wim_dentry *dentry,
1571 const wchar_t *stream_name,
1572 struct win32_apply_ctx *ctx)
1574 const struct wim_inode *inode = dentry->d_inode;
1575 size_t stream_name_nchars = 0;
1576 FILE_ALLOCATION_INFORMATION alloc_info;
1580 if (unlikely(stream_name))
1581 stream_name_nchars = wcslen(stream_name);
1583 if (unlikely(stream_name_nchars)) {
1584 build_extraction_path_with_ads(dentry, ctx,
1585 stream_name, stream_name_nchars);
1587 build_extraction_path(dentry, ctx);
1590 /* Reparse point? */
1591 if (unlikely(inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT)
1592 && (stream_name_nchars == 0))
1594 if (!ctx->common.supported_features.reparse_points)
1597 /* We can't write the reparse stream directly; we must set it
1598 * with FSCTL_SET_REPARSE_POINT, which requires that all the
1599 * data be available. So, stage the data in a buffer. */
1601 list_add_tail(&dentry->tmp_list, &ctx->reparse_dentries);
1602 return prepare_data_buffer(ctx, stream->size);
1605 /* Encrypted file? */
1606 if (unlikely(inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED)
1607 && (stream_name_nchars == 0))
1609 if (!ctx->common.supported_features.encrypted_files)
1612 /* We can't write encrypted file streams directly; we must use
1613 * WriteEncryptedFileRaw(), which requires providing the data
1614 * through a callback function. This can't easily be combined
1615 * with our own callback-based approach.
1617 * The current workaround is to simply read the stream into
1618 * memory and write the encrypted file from that.
1620 * TODO: This isn't sufficient for extremely large encrypted
1621 * files. Perhaps we should create an extra thread to write
1623 list_add_tail(&dentry->tmp_list, &ctx->encrypted_dentries);
1624 return prepare_data_buffer(ctx, stream->size);
1627 if (ctx->num_open_handles == MAX_OPEN_STREAMS) {
1628 /* XXX: Fix this. But because of the checks in
1629 * extract_stream_list(), this can now only happen on a
1630 * filesystem that does not support hard links. */
1631 ERROR("Can't extract data: too many open files!");
1632 return WIMLIB_ERR_UNSUPPORTED;
1635 /* Open a new handle */
1636 status = do_create_file(&h,
1637 FILE_WRITE_DATA | SYNCHRONIZE,
1638 NULL, 0, FILE_OPEN_IF,
1639 FILE_SEQUENTIAL_ONLY |
1640 FILE_SYNCHRONOUS_IO_NONALERT,
1642 if (!NT_SUCCESS(status)) {
1643 set_errno_from_nt_status(status);
1644 ERROR_WITH_ERRNO("Can't open \"%ls\" for writing "
1645 "(status=0x%08"PRIx32")",
1646 current_path(ctx), (u32)status);
1647 return WIMLIB_ERR_OPEN;
1650 ctx->open_handles[ctx->num_open_handles++] = h;
1652 /* Allocate space for the data. */
1653 alloc_info.AllocationSize.QuadPart = stream->size;
1654 (*func_NtSetInformationFile)(h, &ctx->iosb,
1655 &alloc_info, sizeof(alloc_info),
1656 FileAllocationInformation);
1660 /* Set the reparse data @rpbuf of length @rpbuflen on the extracted file
1661 * corresponding to the WIM dentry @dentry. */
1663 do_set_reparse_data(const struct wim_dentry *dentry,
1664 const void *rpbuf, u16 rpbuflen,
1665 struct win32_apply_ctx *ctx)
1670 status = create_file(&h, GENERIC_WRITE, NULL,
1671 0, FILE_OPEN, 0, dentry, ctx);
1672 if (!NT_SUCCESS(status))
1675 status = (*func_NtFsControlFile)(h, NULL, NULL, NULL,
1676 &ctx->iosb, FSCTL_SET_REPARSE_POINT,
1677 (void *)rpbuf, rpbuflen,
1681 if (NT_SUCCESS(status))
1684 /* On Windows, by default only the Administrator can create symbolic
1685 * links for some reason. By default we just issue a warning if this
1686 * appears to be the problem. Use WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS
1687 * to get a hard error. */
1688 if (!(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS)
1689 && (status == STATUS_PRIVILEGE_NOT_HELD ||
1690 status == STATUS_ACCESS_DENIED)
1691 && (dentry->d_inode->i_reparse_tag == WIM_IO_REPARSE_TAG_SYMLINK ||
1692 dentry->d_inode->i_reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT))
1694 WARNING("Can't create symbolic link \"%ls\"! \n"
1695 " (Need Administrator rights, or at least "
1697 " SeCreateSymbolicLink privilege.)",
1703 set_errno_from_nt_status(status);
1704 ERROR_WITH_ERRNO("Can't set reparse data on \"%ls\" "
1705 "(status=0x%08"PRIx32")",
1706 current_path(ctx), (u32)status);
1707 return WIMLIB_ERR_SET_REPARSE_DATA;
1710 /* Given a Windows NT namespace path, such as \??\e:\Windows\System32, return a
1711 * pointer to the suffix of the path that begins with the device directly, such
1712 * as e:\Windows\System32. */
1713 static const wchar_t *
1714 skip_nt_toplevel_component(const wchar_t *path, size_t path_nchars)
1716 static const wchar_t * const dirs[] = {
1721 size_t first_dir_len = 0;
1722 const wchar_t * const end = path + path_nchars;
1724 for (size_t i = 0; i < ARRAY_LEN(dirs); i++) {
1725 size_t len = wcslen(dirs[i]);
1726 if (len <= (end - path) && !wcsnicmp(path, dirs[i], len)) {
1727 first_dir_len = len;
1731 if (first_dir_len == 0)
1733 path += first_dir_len;
1734 while (path != end && *path == L'\\')
1739 /* Given a Windows NT namespace path, such as \??\e:\Windows\System32, return a
1740 * pointer to the suffix of the path that is device-relative, such as
1743 * The path has an explicit length and is not necessarily null terminated.
1745 * If the path just something like \??\e: then the returned pointer will point
1746 * just past the colon. In this case the length of the result will be 0
1748 static const wchar_t *
1749 get_device_relative_path(const wchar_t *path, size_t path_nchars)
1751 const wchar_t * const orig_path = path;
1752 const wchar_t * const end = path + path_nchars;
1754 path = skip_nt_toplevel_component(path, path_nchars);
1755 if (path == orig_path)
1758 path = wmemchr(path, L'\\', (end - path));
1763 } while (path != end && *path == L'\\');
1768 * Given a reparse point buffer for a symbolic link or junction, adjust its
1769 * contents so that the target of the link is consistent with the new location
1773 try_rpfix(u8 *rpbuf, u16 *rpbuflen_p, struct win32_apply_ctx *ctx)
1775 struct reparse_data rpdata;
1776 size_t orig_subst_name_nchars;
1777 const wchar_t *relpath;
1778 size_t relpath_nchars;
1779 size_t target_ntpath_nchars;
1780 size_t fixed_subst_name_nchars;
1781 const wchar_t *fixed_print_name;
1782 size_t fixed_print_name_nchars;
1784 if (parse_reparse_data(rpbuf, *rpbuflen_p, &rpdata)) {
1785 /* Do nothing if the reparse data is invalid. */
1789 if (rpdata.rptag == WIM_IO_REPARSE_TAG_SYMLINK &&
1790 (rpdata.rpflags & SYMBOLIC_LINK_RELATIVE))
1792 /* Do nothing if it's a relative symbolic link. */
1796 /* Build the new substitute name from the NT namespace path to the
1797 * target directory, then a path separator, then the "device relative"
1798 * part of the old substitute name. */
1800 orig_subst_name_nchars = rpdata.substitute_name_nbytes / sizeof(wchar_t);
1802 relpath = get_device_relative_path(rpdata.substitute_name,
1803 orig_subst_name_nchars);
1804 relpath_nchars = orig_subst_name_nchars -
1805 (relpath - rpdata.substitute_name);
1807 target_ntpath_nchars = ctx->target_ntpath.Length / sizeof(wchar_t);
1809 fixed_subst_name_nchars = target_ntpath_nchars;
1811 fixed_subst_name_nchars += 1 + relpath_nchars;
1812 wchar_t fixed_subst_name[fixed_subst_name_nchars];
1814 wmemcpy(fixed_subst_name, ctx->target_ntpath.Buffer,
1815 target_ntpath_nchars);
1816 if (relpath_nchars) {
1817 fixed_subst_name[target_ntpath_nchars] = L'\\';
1818 wmemcpy(&fixed_subst_name[target_ntpath_nchars + 1],
1819 relpath, relpath_nchars);
1821 /* Doesn't need to be null-terminated. */
1823 /* Print name should be Win32, but not all NT names can even be
1824 * translated to Win32 names. But we can at least delete the top-level
1825 * directory, such as \??\, and this will have the expected result in
1826 * the usual case. */
1827 fixed_print_name = skip_nt_toplevel_component(fixed_subst_name,
1828 fixed_subst_name_nchars);
1829 fixed_print_name_nchars = fixed_subst_name_nchars - (fixed_print_name -
1832 rpdata.substitute_name = fixed_subst_name;
1833 rpdata.substitute_name_nbytes = fixed_subst_name_nchars * sizeof(wchar_t);
1834 rpdata.print_name = (wchar_t *)fixed_print_name;
1835 rpdata.print_name_nbytes = fixed_print_name_nchars * sizeof(wchar_t);
1836 make_reparse_buffer(&rpdata, rpbuf, rpbuflen_p);
1839 /* Sets reparse data on the specified file. This handles "fixing" the targets
1840 * of absolute symbolic links and junctions if WIMLIB_EXTRACT_FLAG_RPFIX was
1843 set_reparse_data(const struct wim_dentry *dentry,
1844 const void *_rpbuf, u16 rpbuflen, struct win32_apply_ctx *ctx)
1846 const struct wim_inode *inode = dentry->d_inode;
1847 const void *rpbuf = _rpbuf;
1849 if ((ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX)
1850 && !inode->i_not_rpfixed
1851 && (inode->i_reparse_tag == WIM_IO_REPARSE_TAG_SYMLINK ||
1852 inode->i_reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT))
1854 memcpy(&ctx->rpfixbuf, _rpbuf, rpbuflen);
1855 try_rpfix((u8 *)&ctx->rpfixbuf, &rpbuflen, ctx);
1856 rpbuf = &ctx->rpfixbuf;
1858 return do_set_reparse_data(dentry, rpbuf, rpbuflen, ctx);
1862 /* Import the next block of raw encrypted data */
1864 import_encrypted_data(PBYTE pbData, PVOID pvCallbackContext, PULONG Length)
1866 struct win32_apply_ctx *ctx = pvCallbackContext;
1869 copy_len = min(ctx->encrypted_size - ctx->encrypted_offset, *Length);
1870 memcpy(pbData, &ctx->data_buffer[ctx->encrypted_offset], copy_len);
1871 ctx->encrypted_offset += copy_len;
1873 return ERROR_SUCCESS;
1876 /* Write the raw encrypted data to the already-created file corresponding to
1879 * The raw encrypted data is provided in ctx->data_buffer, and its size is
1880 * ctx->encrypted_size. */
1882 extract_encrypted_file(const struct wim_dentry *dentry,
1883 struct win32_apply_ctx *ctx)
1888 /* Temporarily build a Win32 path for OpenEncryptedFileRaw() */
1889 build_win32_extraction_path(dentry, ctx);
1891 err = OpenEncryptedFileRaw(ctx->pathbuf.Buffer,
1892 CREATE_FOR_IMPORT, &rawctx);
1894 /* Restore the NT namespace path */
1895 build_extraction_path(dentry, ctx);
1897 if (err != ERROR_SUCCESS) {
1898 set_errno_from_win32_error(err);
1899 ERROR_WITH_ERRNO("Can't open \"%ls\" for encrypted import "
1900 "(err=%"PRIu32")", current_path(ctx), (u32)err);
1901 return WIMLIB_ERR_OPEN;
1904 ctx->encrypted_offset = 0;
1906 err = WriteEncryptedFileRaw(import_encrypted_data, ctx, rawctx);
1908 CloseEncryptedFileRaw(rawctx);
1910 if (err != ERROR_SUCCESS) {
1911 set_errno_from_win32_error(err);
1912 ERROR_WITH_ERRNO("Can't import encrypted file \"%ls\" "
1913 "(err=%"PRIu32")", current_path(ctx), (u32)err);
1914 return WIMLIB_ERR_WRITE;
1920 /* Called when starting to read a stream for extraction on Windows */
1922 begin_extract_stream(struct wim_lookup_table_entry *stream, void *_ctx)
1924 struct win32_apply_ctx *ctx = _ctx;
1925 const struct stream_owner *owners = stream_owners(stream);
1928 ctx->num_open_handles = 0;
1929 ctx->data_buffer_ptr = NULL;
1930 INIT_LIST_HEAD(&ctx->reparse_dentries);
1931 INIT_LIST_HEAD(&ctx->encrypted_dentries);
1933 for (u32 i = 0; i < stream->out_refcnt; i++) {
1934 const struct wim_inode *inode = owners[i].inode;
1935 const wchar_t *stream_name = owners[i].stream_name;
1936 struct wim_dentry *dentry;
1938 /* A copy of the stream needs to be extracted to @inode. */
1940 if (ctx->common.supported_features.hard_links) {
1941 dentry = inode_first_extraction_dentry(inode);
1942 ret = begin_extract_stream_instance(stream, dentry,
1947 /* Hard links not supported. Extract the stream
1948 * separately to each alias of the inode. */
1949 struct list_head *next;
1951 next = inode->i_extraction_aliases.next;
1953 dentry = list_entry(next, struct wim_dentry,
1954 d_extraction_alias_node);
1955 ret = begin_extract_stream_instance(stream,
1962 } while (next != &inode->i_extraction_aliases);
1973 /* Called when the next chunk of a stream has been read for extraction on
1976 extract_chunk(const void *chunk, size_t size, void *_ctx)
1978 struct win32_apply_ctx *ctx = _ctx;
1980 /* Write the data chunk to each open handle */
1981 for (unsigned i = 0; i < ctx->num_open_handles; i++) {
1982 u8 *bufptr = (u8 *)chunk;
1983 size_t bytes_remaining = size;
1985 while (bytes_remaining) {
1986 ULONG count = min(0xFFFFFFFF, bytes_remaining);
1988 status = (*func_NtWriteFile)(ctx->open_handles[i],
1990 &ctx->iosb, bufptr, count,
1992 if (!NT_SUCCESS(status)) {
1993 set_errno_from_nt_status(status);
1994 ERROR_WITH_ERRNO("Error writing data to target "
1995 "volume (status=0x%08"PRIx32")",
1997 return WIMLIB_ERR_WRITE;
1999 bufptr += ctx->iosb.Information;
2000 bytes_remaining -= ctx->iosb.Information;
2004 /* Copy the data chunk into the buffer (if needed) */
2005 if (ctx->data_buffer_ptr)
2006 ctx->data_buffer_ptr = mempcpy(ctx->data_buffer_ptr,
2011 /* Called when a stream has been fully read for extraction on Windows */
2013 end_extract_stream(struct wim_lookup_table_entry *stream, int status, void *_ctx)
2015 struct win32_apply_ctx *ctx = _ctx;
2017 const struct wim_dentry *dentry;
2024 if (likely(!ctx->data_buffer_ptr))
2027 if (!list_empty(&ctx->reparse_dentries)) {
2028 if (stream->size > REPARSE_DATA_MAX_SIZE) {
2029 dentry = list_first_entry(&ctx->reparse_dentries,
2030 struct wim_dentry, tmp_list);
2031 build_extraction_path(dentry, ctx);
2032 ERROR("Reparse data of \"%ls\" has size "
2033 "%"PRIu64" bytes (exceeds %u bytes)",
2034 current_path(ctx), stream->size,
2035 REPARSE_DATA_MAX_SIZE);
2036 return WIMLIB_ERR_INVALID_REPARSE_DATA;
2038 /* In the WIM format, reparse streams are just the reparse data
2039 * and omit the header. But we can reconstruct the header. */
2040 memcpy(ctx->rpbuf.rpdata, ctx->data_buffer, stream->size);
2041 ctx->rpbuf.rpdatalen = stream->size;
2042 ctx->rpbuf.rpreserved = 0;
2043 list_for_each_entry(dentry, &ctx->reparse_dentries, tmp_list) {
2044 ctx->rpbuf.rptag = dentry->d_inode->i_reparse_tag;
2045 ret = set_reparse_data(dentry, &ctx->rpbuf,
2046 stream->size + REPARSE_DATA_OFFSET,
2053 if (!list_empty(&ctx->encrypted_dentries)) {
2054 ctx->encrypted_size = stream->size;
2055 list_for_each_entry(dentry, &ctx->encrypted_dentries, tmp_list) {
2056 ret = extract_encrypted_file(dentry, ctx);
2065 /* Attributes that can't be set directly */
2066 #define SPECIAL_ATTRIBUTES \
2067 (FILE_ATTRIBUTE_REPARSE_POINT | \
2068 FILE_ATTRIBUTE_DIRECTORY | \
2069 FILE_ATTRIBUTE_ENCRYPTED | \
2070 FILE_ATTRIBUTE_SPARSE_FILE | \
2071 FILE_ATTRIBUTE_COMPRESSED)
2073 /* Set the security descriptor @desc, of @desc_size bytes, on the file with open
2076 set_security_descriptor(HANDLE h, const void *_desc,
2077 size_t desc_size, struct win32_apply_ctx *ctx)
2079 SECURITY_INFORMATION info;
2081 SECURITY_DESCRIPTOR_RELATIVE *desc;
2084 * Ideally, we would just pass in the security descriptor buffer as-is.
2085 * But it turns out that Windows can mess up the security descriptor
2086 * even when using the low-level NtSetSecurityObject() function:
2088 * - Windows will clear SE_DACL_AUTO_INHERITED if it is set in the
2089 * passed buffer. To actually get Windows to set
2090 * SE_DACL_AUTO_INHERITED, the application must set the non-persistent
2091 * flag SE_DACL_AUTO_INHERIT_REQ. As usual, Microsoft didn't bother
2092 * to properly document either of these flags. It's unclear how
2093 * important SE_DACL_AUTO_INHERITED actually is, but to be safe we use
2094 * the SE_DACL_AUTO_INHERIT_REQ workaround to set it if needed.
2096 * - The above also applies to the equivalent SACL flags,
2097 * SE_SACL_AUTO_INHERITED and SE_SACL_AUTO_INHERIT_REQ.
2099 * - If the application says that it's setting
2100 * DACL_SECURITY_INFORMATION, then Windows sets SE_DACL_PRESENT in the
2101 * resulting security descriptor, even if the security descriptor the
2102 * application provided did not have a DACL. This seems to be
2103 * unavoidable, since omitting DACL_SECURITY_INFORMATION would cause a
2104 * default DACL to remain. Fortunately, this behavior seems harmless,
2105 * since the resulting DACL will still be "null" --- but it will be
2106 * "the other representation of null".
2108 * - The above also applies to SACL_SECURITY_INFORMATION and
2109 * SE_SACL_PRESENT. Again, it's seemingly unavoidable but "harmless"
2110 * that Windows changes the representation of a "null SACL".
2112 if (likely(desc_size <= STACK_MAX)) {
2113 desc = alloca(desc_size);
2115 desc = MALLOC(desc_size);
2117 return STATUS_NO_MEMORY;
2120 memcpy(desc, _desc, desc_size);
2122 if (likely(desc_size >= 4)) {
2124 if (desc->Control & SE_DACL_AUTO_INHERITED)
2125 desc->Control |= SE_DACL_AUTO_INHERIT_REQ;
2127 if (desc->Control & SE_SACL_AUTO_INHERITED)
2128 desc->Control |= SE_SACL_AUTO_INHERIT_REQ;
2132 * More API insanity. We want to set the entire security descriptor
2133 * as-is. But all available APIs require specifying the specific parts
2134 * of the security descriptor being set. Especially annoying is that
2135 * mandatory integrity labels are part of the SACL, but they aren't set
2136 * with SACL_SECURITY_INFORMATION. Instead, applications must also
2137 * specify LABEL_SECURITY_INFORMATION (Windows Vista, Windows 7) or
2138 * BACKUP_SECURITY_INFORMATION (Windows 8). But at least older versions
2139 * of Windows don't error out if you provide these newer flags...
2141 * Also, if the process isn't running as Administrator, then it probably
2142 * doesn't have SE_RESTORE_PRIVILEGE. In this case, it will always get
2143 * the STATUS_PRIVILEGE_NOT_HELD error by trying to set the SACL, even
2144 * if the security descriptor it provided did not have a SACL. By
2145 * default, in this case we try to recover and set as much of the
2146 * security descriptor as possible --- potentially excluding the DACL, and
2147 * even the owner, as well as the SACL.
2150 info = OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
2151 DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION |
2152 LABEL_SECURITY_INFORMATION | BACKUP_SECURITY_INFORMATION;
2156 * It's also worth noting that SetFileSecurity() is unusable because it
2157 * doesn't request "backup semantics" when it opens the file internally.
2158 * NtSetSecurityObject() seems to be the best function to use in backup
2159 * applications. (SetSecurityInfo() should also work, but it's harder
2160 * to use and must call NtSetSecurityObject() internally anyway.
2161 * BackupWrite() is theoretically usable as well, but it's inflexible
2162 * and poorly documented.)
2166 status = (*func_NtSetSecurityObject)(h, info, desc);
2167 if (NT_SUCCESS(status))
2168 goto out_maybe_free_desc;
2170 /* Failed to set the requested parts of the security descriptor. If the
2171 * error was permissions-related, try to set fewer parts of the security
2172 * descriptor, unless WIMLIB_EXTRACT_FLAG_STRICT_ACLS is enabled. */
2173 if ((status == STATUS_PRIVILEGE_NOT_HELD ||
2174 status == STATUS_ACCESS_DENIED) &&
2175 !(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_ACLS))
2177 if (info & SACL_SECURITY_INFORMATION) {
2178 info &= ~(SACL_SECURITY_INFORMATION |
2179 LABEL_SECURITY_INFORMATION |
2180 BACKUP_SECURITY_INFORMATION);
2181 ctx->partial_security_descriptors++;
2184 if (info & DACL_SECURITY_INFORMATION) {
2185 info &= ~DACL_SECURITY_INFORMATION;
2188 if (info & OWNER_SECURITY_INFORMATION) {
2189 info &= ~OWNER_SECURITY_INFORMATION;
2192 /* Nothing left except GROUP, and if we removed it we
2193 * wouldn't have anything at all. */
2196 /* No part of the security descriptor could be set, or
2197 * WIMLIB_EXTRACT_FLAG_STRICT_ACLS is enabled and the full security
2198 * descriptor could not be set. */
2199 if (!(info & SACL_SECURITY_INFORMATION))
2200 ctx->partial_security_descriptors--;
2201 ctx->no_security_descriptors++;
2203 out_maybe_free_desc:
2204 if (unlikely(desc_size > STACK_MAX))
2209 /* Set metadata on the open file @h from the WIM inode @inode. */
2211 do_apply_metadata_to_file(HANDLE h, const struct wim_inode *inode,
2212 struct win32_apply_ctx *ctx)
2214 FILE_BASIC_INFORMATION info;
2217 /* Set security descriptor if present and not in NO_ACLS mode */
2218 if (inode->i_security_id >= 0 &&
2219 !(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_NO_ACLS))
2221 const struct wim_security_data *sd;
2225 sd = wim_get_current_security_data(ctx->common.wim);
2226 desc = sd->descriptors[inode->i_security_id];
2227 desc_size = sd->sizes[inode->i_security_id];
2229 status = set_security_descriptor(h, desc, desc_size, ctx);
2230 if (!NT_SUCCESS(status) &&
2231 (ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_ACLS))
2233 set_errno_from_nt_status(status);
2234 ERROR_WITH_ERRNO("Can't set security descriptor "
2235 "on \"%ls\" (status=0x%08"PRIx32")",
2236 current_path(ctx), (u32)status);
2237 return WIMLIB_ERR_SET_SECURITY;
2241 /* Set attributes and timestamps */
2242 info.CreationTime.QuadPart = inode->i_creation_time;
2243 info.LastAccessTime.QuadPart = inode->i_last_access_time;
2244 info.LastWriteTime.QuadPart = inode->i_last_write_time;
2245 info.ChangeTime.QuadPart = 0;
2246 if (ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES)
2247 info.FileAttributes = 0;
2249 info.FileAttributes = inode->i_attributes & ~SPECIAL_ATTRIBUTES;
2251 status = (*func_NtSetInformationFile)(h, &ctx->iosb,
2252 &info, sizeof(info),
2253 FileBasicInformation);
2254 /* On FAT volumes we get STATUS_INVALID_PARAMETER if we try to set
2255 * attributes on the root directory. (Apparently because FAT doesn't
2256 * actually have a place to store those attributes!) */
2257 if (!NT_SUCCESS(status)
2258 && !(status == STATUS_INVALID_PARAMETER &&
2259 dentry_is_root(inode_first_extraction_dentry(inode))))
2261 set_errno_from_nt_status(status);
2262 ERROR_WITH_ERRNO("Can't set basic metadata on \"%ls\" "
2263 "(status=0x%08"PRIx32")",
2264 current_path(ctx), (u32)status);
2265 return WIMLIB_ERR_SET_ATTRIBUTES;
2272 apply_metadata_to_file(const struct wim_dentry *dentry,
2273 struct win32_apply_ctx *ctx)
2275 const struct wim_inode *inode = dentry->d_inode;
2281 perms = FILE_WRITE_ATTRIBUTES | WRITE_DAC |
2282 WRITE_OWNER | ACCESS_SYSTEM_SECURITY;
2284 build_extraction_path(dentry, ctx);
2286 /* Open a handle with as many relevant permissions as possible. */
2287 while (!NT_SUCCESS(status = do_create_file(&h, perms, NULL,
2288 0, FILE_OPEN, 0, ctx)))
2290 if (status == STATUS_PRIVILEGE_NOT_HELD ||
2291 status == STATUS_ACCESS_DENIED)
2293 if (perms & ACCESS_SYSTEM_SECURITY) {
2294 perms &= ~ACCESS_SYSTEM_SECURITY;
2297 if (perms & WRITE_DAC) {
2298 perms &= ~WRITE_DAC;
2301 if (perms & WRITE_OWNER) {
2302 perms &= ~WRITE_OWNER;
2306 set_errno_from_nt_status(status);
2307 ERROR_WITH_ERRNO("Can't open \"%ls\" to set metadata "
2308 "(status=0x%08"PRIx32")",
2309 current_path(ctx), (u32)status);
2310 return WIMLIB_ERR_OPEN;
2313 ret = do_apply_metadata_to_file(h, inode, ctx);
2321 apply_metadata(struct list_head *dentry_list, struct win32_apply_ctx *ctx)
2323 const struct wim_dentry *dentry;
2326 /* We go in reverse so that metadata is set on all a directory's
2327 * children before the directory itself. This avoids any potential
2328 * problems with attributes, timestamps, or security descriptors. */
2329 list_for_each_entry_reverse(dentry, dentry_list, d_extraction_list_node)
2331 ret = apply_metadata_to_file(dentry, ctx);
2334 ret = report_file_metadata_applied(&ctx->common);
2341 /* Issue warnings about problems during the extraction for which warnings were
2342 * not already issued (due to the high number of potential warnings if we issued
2343 * them per-file). */
2345 do_warnings(const struct win32_apply_ctx *ctx)
2347 if (ctx->partial_security_descriptors == 0 &&
2348 ctx->no_security_descriptors == 0 &&
2349 ctx->num_short_name_failures == 0)
2352 WARNING("Extraction to \"%ls\" complete, but with one or more warnings:",
2353 ctx->common.target);
2354 if (ctx->num_short_name_failures) {
2355 WARNING("- Could not set short names on %lu files or directories",
2356 ctx->num_short_name_failures);
2358 if (ctx->partial_security_descriptors) {
2359 WARNING("- Could only partially set the security descriptor\n"
2360 " on %lu files or directories.",
2361 ctx->partial_security_descriptors);
2363 if (ctx->no_security_descriptors) {
2364 WARNING("- Could not set security descriptor at all\n"
2365 " on %lu files or directories.",
2366 ctx->no_security_descriptors);
2368 if (ctx->partial_security_descriptors || ctx->no_security_descriptors) {
2369 WARNING("To fully restore all security descriptors, run the program\n"
2370 " with Administrator rights.");
2375 count_dentries(const struct list_head *dentry_list)
2377 const struct list_head *cur;
2380 list_for_each(cur, dentry_list)
2386 /* Extract files from a WIM image to a directory on Windows */
2388 win32_extract(struct list_head *dentry_list, struct apply_ctx *_ctx)
2391 struct win32_apply_ctx *ctx = (struct win32_apply_ctx *)_ctx;
2392 uint64_t dentry_count;
2394 ret = prepare_target(dentry_list, ctx);
2398 if (unlikely(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_WIMBOOT)) {
2399 ret = start_wimboot_extraction(ctx);
2404 dentry_count = count_dentries(dentry_list);
2406 ret = start_file_structure_phase(&ctx->common, dentry_count);
2410 ret = create_directories(dentry_list, ctx);
2414 ret = create_nondirectories(dentry_list, ctx);
2418 ret = end_file_structure_phase(&ctx->common);
2422 struct read_stream_list_callbacks cbs = {
2423 .begin_stream = begin_extract_stream,
2424 .begin_stream_ctx = ctx,
2425 .consume_chunk = extract_chunk,
2426 .consume_chunk_ctx = ctx,
2427 .end_stream = end_extract_stream,
2428 .end_stream_ctx = ctx,
2430 ret = extract_stream_list(&ctx->common, &cbs);
2434 ret = start_file_metadata_phase(&ctx->common, dentry_count);
2438 ret = apply_metadata(dentry_list, ctx);
2442 ret = end_file_metadata_phase(&ctx->common);
2446 if (unlikely(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_WIMBOOT)) {
2447 ret = end_wimboot_extraction(ctx);
2455 (*func_NtClose)(ctx->h_target);
2456 if (ctx->target_ntpath.Buffer)
2457 HeapFree(GetProcessHeap(), 0, ctx->target_ntpath.Buffer);
2458 FREE(ctx->pathbuf.Buffer);
2459 FREE(ctx->print_buffer);
2460 if (ctx->wimboot.prepopulate_pats) {
2461 FREE(ctx->wimboot.prepopulate_pats->strings);
2462 FREE(ctx->wimboot.prepopulate_pats);
2464 FREE(ctx->wimboot.mem_prepopulate_pats);
2465 FREE(ctx->data_buffer);
2469 const struct apply_operations win32_apply_ops = {
2471 .get_supported_features = win32_get_supported_features,
2472 .extract = win32_extract,
2473 .will_externally_back = win32_will_externally_back,
2474 .context_size = sizeof(struct win32_apply_ctx),
2477 #endif /* __WIN32__ */