2 * win32_apply.c - Windows-specific code for applying files from a WIM image.
6 * Copyright (C) 2013 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 <aclapi.h> /* for SetSecurityInfo() */
32 #include "wimlib/win32_common.h"
34 #include "wimlib/apply.h"
35 #include "wimlib/dentry.h"
36 #include "wimlib/endianness.h"
37 #include "wimlib/error.h"
38 #include "wimlib/lookup_table.h"
39 #include "wimlib/metadata.h"
40 #include "wimlib/reparse.h"
41 #include "wimlib/security.h"
43 #define MAX_CREATE_HARD_LINK_WARNINGS 5
44 #define MAX_CREATE_SOFT_LINK_WARNINGS 5
46 #define MAX_SET_SD_ACCESS_DENIED_WARNINGS 1
47 #define MAX_SET_SACL_PRIV_NOTHELD_WARNINGS 1
49 static const wchar_t *apply_access_denied_msg =
50 L"If you are not running this program as the administrator, you may\n"
51 " need to do so, so that all data and metadata can be extracted\n"
52 " exactly as the origignal copy. However, if you do not care that\n"
53 " the security descriptors are extracted correctly, you could run\n"
54 " `wimlib-imagex apply' with the --no-acls flag instead.\n"
59 win32_extract_try_rpfix(u8 *rpbuf,
60 const wchar_t *extract_root_realpath,
61 unsigned extract_root_realpath_nchars)
63 struct reparse_data rpdata;
66 size_t stripped_nchars;
67 wchar_t *stripped_target;
68 wchar_t stripped_target_nchars;
71 utf16lechar *new_target;
72 utf16lechar *new_print_name;
73 size_t new_target_nchars;
74 size_t new_print_name_nchars;
77 ret = parse_reparse_data(rpbuf, 8 + le16_to_cpu(*(u16*)(rpbuf + 4)),
82 if (extract_root_realpath[0] == L'\0' ||
83 extract_root_realpath[1] != L':' ||
84 extract_root_realpath[2] != L'\\')
86 ERROR("Can't understand full path format \"%ls\". "
87 "Try turning reparse point fixups off...",
88 extract_root_realpath);
89 return WIMLIB_ERR_REPARSE_POINT_FIXUP_FAILED;
92 ret = parse_substitute_name(rpdata.substitute_name,
93 rpdata.substitute_name_nbytes,
97 stripped_nchars = ret;
98 target = rpdata.substitute_name;
99 target_nchars = rpdata.substitute_name_nbytes / sizeof(utf16lechar);
100 stripped_target = target + 6;
101 stripped_target_nchars = target_nchars - stripped_nchars;
103 new_target = alloca((6 + extract_root_realpath_nchars +
104 stripped_target_nchars) * sizeof(utf16lechar));
107 if (stripped_nchars == 6) {
108 /* Include \??\ prefix if it was present before */
109 wmemcpy(p, L"\\??\\", 4);
113 /* Print name excludes the \??\ if present. */
115 if (stripped_nchars != 0) {
116 /* Get drive letter from real path to extract root, if a drive
117 * letter was present before. */
118 *p++ = extract_root_realpath[0];
119 *p++ = extract_root_realpath[1];
121 /* Copy the rest of the extract root */
122 wmemcpy(p, extract_root_realpath + 2, extract_root_realpath_nchars - 2);
123 p += extract_root_realpath_nchars - 2;
125 /* Append the stripped target */
126 wmemcpy(p, stripped_target, stripped_target_nchars);
127 p += stripped_target_nchars;
128 new_target_nchars = p - new_target;
129 new_print_name_nchars = p - new_print_name;
131 if (new_target_nchars * sizeof(utf16lechar) >= REPARSE_POINT_MAX_SIZE ||
132 new_print_name_nchars * sizeof(utf16lechar) >= REPARSE_POINT_MAX_SIZE)
134 ERROR("Path names too long to do reparse point fixup!");
135 return WIMLIB_ERR_REPARSE_POINT_FIXUP_FAILED;
137 rpdata.substitute_name = new_target;
138 rpdata.substitute_name_nbytes = new_target_nchars * sizeof(utf16lechar);
139 rpdata.print_name = new_print_name;
140 rpdata.print_name_nbytes = new_print_name_nchars * sizeof(utf16lechar);
141 return make_reparse_buffer(&rpdata, rpbuf);
144 /* Wrapper around the FSCTL_SET_REPARSE_POINT ioctl to set the reparse data on
145 * an extracted reparse point. */
147 win32_set_reparse_data(HANDLE h,
148 const struct wim_inode *inode,
149 const struct wim_lookup_table_entry *lte,
151 struct apply_args *args)
154 u8 rpbuf[REPARSE_POINT_MAX_SIZE];
158 DEBUG("Setting reparse data on \"%ls\"", path);
160 ret = wim_inode_get_reparse_data(inode, rpbuf, &rpbuflen);
164 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX &&
165 (inode->i_reparse_tag == WIM_IO_REPARSE_TAG_SYMLINK ||
166 inode->i_reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT) &&
167 !inode->i_not_rpfixed)
169 ret = win32_extract_try_rpfix(rpbuf,
170 args->target_realpath,
171 args->target_realpath_len);
173 return WIMLIB_ERR_REPARSE_POINT_FIXUP_FAILED;
176 /* Set the reparse data on the open file using the
177 * FSCTL_SET_REPARSE_POINT ioctl.
179 * There are contradictions in Microsoft's documentation for this:
181 * "If hDevice was opened without specifying FILE_FLAG_OVERLAPPED,
182 * lpOverlapped is ignored."
184 * --- So setting lpOverlapped to NULL is okay since it's ignored.
186 * "If lpOverlapped is NULL, lpBytesReturned cannot be NULL. Even when an
187 * operation returns no output data and lpOutBuffer is NULL,
188 * DeviceIoControl makes use of lpBytesReturned. After such an
189 * operation, the value of lpBytesReturned is meaningless."
191 * --- So lpOverlapped not really ignored, as it affects another
192 * parameter. This is the actual behavior: lpBytesReturned must be
193 * specified, even though lpBytesReturned is documented as:
195 * "Not used with this operation; set to NULL."
197 if (!DeviceIoControl(h, FSCTL_SET_REPARSE_POINT, rpbuf,
200 &bytesReturned /* lpBytesReturned */,
201 NULL /* lpOverlapped */))
203 DWORD err = GetLastError();
204 if (err == ERROR_ACCESS_DENIED || err == ERROR_PRIVILEGE_NOT_HELD)
206 args->num_soft_links_failed++;
207 if (args->num_soft_links_failed <= MAX_CREATE_SOFT_LINK_WARNINGS) {
208 WARNING("Can't set reparse data on \"%ls\": Access denied!\n"
209 " You may be trying to extract a symbolic "
211 " SeCreateSymbolicLink privilege, which by "
212 "default non-Administrator\n"
213 " accounts do not have.", path);
215 if (args->num_hard_links_failed == MAX_CREATE_HARD_LINK_WARNINGS) {
216 WARNING("Suppressing further warnings regarding failure to extract\n"
217 " reparse points due to insufficient privileges...");
220 ERROR("Failed to set reparse data on \"%ls\"", path);
222 if (inode->i_reparse_tag == WIM_IO_REPARSE_TAG_SYMLINK ||
223 inode->i_reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT)
224 return WIMLIB_ERR_LINK;
226 return WIMLIB_ERR_WRITE;
232 /* Wrapper around the FSCTL_SET_COMPRESSION ioctl to change the
233 * FILE_ATTRIBUTE_COMPRESSED flag of a file or directory. */
235 win32_set_compression_state(HANDLE hFile, USHORT format, const wchar_t *path)
238 if (!DeviceIoControl(hFile, FSCTL_SET_COMPRESSION,
239 &format, sizeof(USHORT),
241 &bytesReturned, NULL))
243 /* Could be a warning only, but we only call this if the volume
244 * supports compression. So I'm calling this an error. */
245 DWORD err = GetLastError();
246 ERROR("Failed to set compression flag on \"%ls\"", path);
248 if (err == ERROR_ACCESS_DENIED || err == ERROR_PRIVILEGE_NOT_HELD)
249 return WIMLIB_ERR_INSUFFICIENT_PRIVILEGES_TO_EXTRACT;
251 return WIMLIB_ERR_WRITE;
256 /* Wrapper around FSCTL_SET_SPARSE ioctl to set a file as sparse. */
258 win32_set_sparse(HANDLE hFile, const wchar_t *path)
261 if (!DeviceIoControl(hFile, FSCTL_SET_SPARSE,
264 &bytesReturned, NULL))
266 /* Could be a warning only, but we only call this if the volume
267 * supports sparse files. So I'm calling this an error. */
268 DWORD err = GetLastError();
269 WARNING("Failed to set sparse flag on \"%ls\"", path);
271 if (err == ERROR_ACCESS_DENIED || err == ERROR_PRIVILEGE_NOT_HELD)
272 return WIMLIB_ERR_INSUFFICIENT_PRIVILEGES_TO_EXTRACT;
274 return WIMLIB_ERR_WRITE;
280 * Sets the security descriptor on an extracted file.
283 win32_set_security_data(const struct wim_inode *inode,
286 struct apply_args *args)
288 PSECURITY_DESCRIPTOR descriptor;
291 const struct wim_security_data *sd;
293 SECURITY_INFORMATION securityInformation = 0;
300 BOOL owner_defaulted;
301 BOOL group_defaulted;
307 sd = wim_const_security_data(args->w);
308 descriptor = sd->descriptors[inode->i_security_id];
310 GetSecurityDescriptorOwner(descriptor, &owner, &owner_defaulted);
312 securityInformation |= OWNER_SECURITY_INFORMATION;
314 GetSecurityDescriptorGroup(descriptor, &group, &group_defaulted);
316 securityInformation |= GROUP_SECURITY_INFORMATION;
318 GetSecurityDescriptorDacl(descriptor, &dacl_present,
319 &dacl, &dacl_defaulted);
321 securityInformation |= DACL_SECURITY_INFORMATION;
323 GetSecurityDescriptorSacl(descriptor, &sacl_present,
324 &sacl, &sacl_defaulted);
326 securityInformation |= SACL_SECURITY_INFORMATION;
329 if (securityInformation == 0)
331 if (SetSecurityInfo(hFile, SE_FILE_OBJECT,
332 securityInformation, owner, group, dacl, sacl))
334 err = GetLastError();
335 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_ACLS)
338 case ERROR_PRIVILEGE_NOT_HELD:
339 if (securityInformation & SACL_SECURITY_INFORMATION) {
340 n = args->num_set_sacl_priv_notheld++;
341 securityInformation &= ~SACL_SECURITY_INFORMATION;
343 if (n < MAX_SET_SACL_PRIV_NOTHELD_WARNINGS) {
345 "We don't have enough privileges to set the full security\n"
346 " descriptor on \"%ls\"!\n", path);
347 if (args->num_set_sd_access_denied +
348 args->num_set_sacl_priv_notheld == 1)
350 WARNING("%ls", apply_access_denied_msg);
352 WARNING("Re-trying with SACL omitted.\n", path);
353 } else if (n == MAX_SET_SACL_PRIV_NOTHELD_WARNINGS) {
355 "Suppressing further 'privileges not held' error messages when setting\n"
356 " security descriptors.");
361 case ERROR_INVALID_OWNER:
362 case ERROR_ACCESS_DENIED:
363 n = args->num_set_sd_access_denied++;
364 if (n < MAX_SET_SD_ACCESS_DENIED_WARNINGS) {
365 WARNING("Failed to set security descriptor on \"%ls\": "
366 "Access denied!\n", path);
367 if (args->num_set_sd_access_denied +
368 args->num_set_sacl_priv_notheld == 1)
370 WARNING("%ls", apply_access_denied_msg);
372 } else if (n == MAX_SET_SD_ACCESS_DENIED_WARNINGS) {
374 "Suppressing further access denied error messages when setting\n"
375 " security descriptors");
380 ERROR("Failed to set security descriptor on \"%ls\"", path);
382 if (err == ERROR_ACCESS_DENIED || err == ERROR_PRIVILEGE_NOT_HELD)
383 return WIMLIB_ERR_INSUFFICIENT_PRIVILEGES_TO_EXTRACT;
385 return WIMLIB_ERR_WRITE;
391 win32_extract_chunk(const void *buf, size_t len, void *arg)
393 HANDLE hStream = arg;
395 DWORD nbytes_written;
396 wimlib_assert(len <= 0xffffffff);
398 if (!WriteFile(hStream, buf, len, &nbytes_written, NULL) ||
399 nbytes_written != len)
401 DWORD err = GetLastError();
402 ERROR("WriteFile(): write error");
404 return WIMLIB_ERR_WRITE;
410 do_win32_extract_stream(HANDLE hStream, const struct wim_lookup_table_entry *lte)
412 return extract_wim_resource(lte, wim_resource_size(lte),
413 win32_extract_chunk, hStream);
416 struct win32_encrypted_extract_ctx {
417 const struct wim_lookup_table_entry *lte;
422 win32_encrypted_import_cb(unsigned char *data, void *_ctx,
423 unsigned long *len_p)
425 struct win32_encrypted_extract_ctx *ctx = _ctx;
426 unsigned long len = *len_p;
427 const struct wim_lookup_table_entry *lte = ctx->lte;
429 len = min(len, wim_resource_size(lte) - ctx->offset);
431 if (read_partial_wim_resource_into_buf(lte, len, ctx->offset, data))
432 return ERROR_READ_FAULT;
436 return ERROR_SUCCESS;
439 /* Create an encrypted file and extract the raw encrypted data to it.
441 * @path: Path to encrypted file to create.
442 * @lte: WIM lookup_table entry for the raw encrypted data.
444 * This is separate from do_win32_extract_stream() because the WIM is supposed
445 * to contain the *raw* encrypted data, which needs to be extracted ("imported")
446 * using the special APIs OpenEncryptedFileRawW(), WriteEncryptedFileRaw(), and
447 * CloseEncryptedFileRaw().
449 * Returns 0 on success; nonzero on failure.
452 do_win32_extract_encrypted_stream(const wchar_t *path,
453 const struct wim_lookup_table_entry *lte)
458 DEBUG("Opening file \"%ls\" to extract raw encrypted data", path);
460 ret = OpenEncryptedFileRawW(path, CREATE_FOR_IMPORT, &file_ctx);
462 ERROR("Failed to open \"%ls\" to write raw encrypted data", path);
464 return WIMLIB_ERR_OPEN;
468 struct win32_encrypted_extract_ctx ctx;
472 ret = WriteEncryptedFileRaw(win32_encrypted_import_cb, &ctx, file_ctx);
473 if (ret == ERROR_SUCCESS) {
476 ret = WIMLIB_ERR_WRITE;
477 ERROR("Failed to extract encrypted file \"%ls\"", path);
480 CloseEncryptedFileRaw(file_ctx);
485 path_is_root_of_drive(const wchar_t *path)
490 if (*path != L'/' && *path != L'\\') {
491 if (*(path + 1) == L':')
496 while (*path == L'/' || *path == L'\\')
498 return (*path == L'\0');
502 win32_mask_attributes(DWORD i_attributes)
504 return i_attributes & ~(FILE_ATTRIBUTE_SPARSE_FILE |
505 FILE_ATTRIBUTE_COMPRESSED |
506 FILE_ATTRIBUTE_REPARSE_POINT |
507 FILE_ATTRIBUTE_DIRECTORY |
508 FILE_ATTRIBUTE_ENCRYPTED |
509 FILE_FLAG_DELETE_ON_CLOSE |
510 FILE_FLAG_NO_BUFFERING |
511 FILE_FLAG_OPEN_NO_RECALL |
512 FILE_FLAG_OVERLAPPED |
513 FILE_FLAG_RANDOM_ACCESS |
514 /*FILE_FLAG_SESSION_AWARE |*/
515 FILE_FLAG_SEQUENTIAL_SCAN |
516 FILE_FLAG_WRITE_THROUGH);
520 win32_get_create_flags_and_attributes(DWORD i_attributes)
523 * Some attributes cannot be set by passing them to CreateFile(). In
526 * FILE_ATTRIBUTE_DIRECTORY:
527 * CreateDirectory() must be called instead of CreateFile().
529 * FILE_ATTRIBUTE_SPARSE_FILE:
531 * See: win32_set_sparse().
533 * FILE_ATTRIBUTE_COMPRESSED:
534 * Not clear from the documentation, but apparently this needs an
536 * See: win32_set_compressed().
538 * FILE_ATTRIBUTE_REPARSE_POINT:
539 * Needs an ioctl, with the reparse data specified.
540 * See: win32_set_reparse_data().
542 * In addition, clear any file flags in the attributes that we don't
543 * want, but also specify FILE_FLAG_OPEN_REPARSE_POINT and
544 * FILE_FLAG_BACKUP_SEMANTICS as we are a backup application.
546 return win32_mask_attributes(i_attributes) |
547 FILE_FLAG_OPEN_REPARSE_POINT |
548 FILE_FLAG_BACKUP_SEMANTICS;
551 /* Set compression and/or sparse attributes on a stream, if supported by the
554 win32_set_special_stream_attributes(HANDLE hFile, const struct wim_inode *inode,
555 struct wim_lookup_table_entry *unnamed_stream_lte,
556 const wchar_t *path, unsigned vol_flags)
560 if (inode->i_attributes & FILE_ATTRIBUTE_COMPRESSED) {
561 if (vol_flags & FILE_FILE_COMPRESSION) {
562 ret = win32_set_compression_state(hFile,
563 COMPRESSION_FORMAT_DEFAULT,
568 DEBUG("Cannot set compression attribute on \"%ls\": "
569 "volume does not support transparent compression",
574 if (inode->i_attributes & FILE_ATTRIBUTE_SPARSE_FILE) {
575 if (vol_flags & FILE_SUPPORTS_SPARSE_FILES) {
576 DEBUG("Setting sparse flag on \"%ls\"", path);
577 ret = win32_set_sparse(hFile, path);
581 DEBUG("Cannot set sparse attribute on \"%ls\": "
582 "volume does not support sparse files",
589 /* Pre-create directories; extract encrypted streams */
591 win32_begin_extract_unnamed_stream(const struct wim_inode *inode,
592 const struct wim_lookup_table_entry *lte,
594 DWORD *creationDisposition_ret,
595 unsigned int vol_flags)
600 /* Directories must be created with CreateDirectoryW(). Then the call
601 * to CreateFileW() will merely open the directory that was already
602 * created rather than creating a new file. */
603 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY &&
604 !path_is_root_of_drive(path)) {
605 if (!CreateDirectoryW(path, NULL)) {
606 err = GetLastError();
607 if (err != ERROR_ALREADY_EXISTS) {
608 ERROR("Failed to create directory \"%ls\"",
611 return WIMLIB_ERR_MKDIR;
614 DEBUG("Created directory \"%ls\"", path);
615 *creationDisposition_ret = OPEN_EXISTING;
617 if (inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED &&
618 vol_flags & FILE_SUPPORTS_ENCRYPTION)
620 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY) {
621 unsigned remaining_sharing_violations = 100;
622 while (!EncryptFile(path)) {
623 if (remaining_sharing_violations &&
624 err == ERROR_SHARING_VIOLATION)
626 WARNING("Couldn't encrypt directory \"%ls\" "
627 "due to sharing violation; re-trying "
628 "after 100 ms", path);
630 remaining_sharing_violations--;
632 err = GetLastError();
633 ERROR("Failed to encrypt directory \"%ls\"",
636 return WIMLIB_ERR_WRITE;
640 ret = do_win32_extract_encrypted_stream(path, lte);
643 DEBUG("Extracted encrypted file \"%ls\"", path);
645 *creationDisposition_ret = OPEN_EXISTING;
648 /* Set file attributes if we created the file. Otherwise, we haven't
649 * created the file set and we will set the attributes in the call to
652 * The FAT filesystem does not let you change the attributes of the root
653 * directory, so treat that as a special case and do not set attributes.
655 if (*creationDisposition_ret == OPEN_EXISTING &&
656 !path_is_root_of_drive(path))
658 if (!SetFileAttributesW(path,
659 win32_mask_attributes(inode->i_attributes)))
661 err = GetLastError();
662 ERROR("Failed to set attributes on \"%ls\"", path);
664 return WIMLIB_ERR_WRITE;
670 /* Set security descriptor and extract stream data or reparse data (skip the
671 * unnamed data stream of encrypted files, which was already extracted). */
673 win32_finish_extract_stream(HANDLE h, const struct wim_dentry *dentry,
674 const struct wim_lookup_table_entry *lte,
675 const wchar_t *stream_path,
676 const wchar_t *stream_name_utf16,
677 struct apply_args *args)
680 const struct wim_inode *inode = dentry->d_inode;
681 if (stream_name_utf16 == NULL) {
682 /* Unnamed stream. */
684 /* Set security descriptor, unless the extract_flags indicate
685 * not to or the volume does not supported it. Note that this
686 * is only done when the unnamed stream is being extracted, as
687 * security descriptors are per-file and not per-stream. */
688 if (inode->i_security_id >= 0 &&
689 !(args->extract_flags & WIMLIB_EXTRACT_FLAG_NO_ACLS)
690 && (args->vol_flags & FILE_PERSISTENT_ACLS))
692 ret = win32_set_security_data(inode, h, stream_path, args);
697 /* Handle reparse points. The data for them needs to be set
698 * using a special ioctl. Note that the reparse point may have
699 * been created using CreateFileW() in the case of
700 * non-directories or CreateDirectoryW() in the case of
701 * directories; but the ioctl works either way. Also, it is
702 * only this step that actually sets the
703 * FILE_ATTRIBUTE_REPARSE_POINT, as it is not valid to set it
704 * using SetFileAttributesW() or CreateFileW().
706 * If the volume does not support reparse points we simply
707 * ignore the reparse data. (N.B. the code currently doesn't
708 * actually reach this case because reparse points are skipped
709 * entirely on such volumes.) */
710 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
711 if (args->vol_flags & FILE_SUPPORTS_REPARSE_POINTS) {
712 ret = win32_set_reparse_data(h, inode,
718 DEBUG("Cannot set reparse data on \"%ls\": volume "
719 "does not support reparse points", stream_path);
721 } else if (lte != NULL &&
722 !(args->vol_flags & FILE_SUPPORTS_ENCRYPTION &&
723 inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED))
725 /* Extract the data of the unnamed stream, unless the
726 * lookup table entry is NULL (indicating an empty
727 * stream for which no data needs to be extracted), or
728 * the stream is encrypted and therefore was already
729 * extracted as a special case. */
730 ret = do_win32_extract_stream(h, lte);
735 if (dentry_has_short_name(dentry))
736 SetFileShortNameW(h, dentry->short_name);
737 else if (running_on_windows_7_or_later())
738 SetFileShortNameW(h, L"");
740 /* Extract the data for a named data stream. */
742 DEBUG("Extracting named data stream \"%ls\" (len = %"PRIu64")",
743 stream_path, wim_resource_size(lte));
744 ret = do_win32_extract_stream(h, lte);
751 win32_decrypt_file(HANDLE open_handle, const wchar_t *path)
754 /* We cannot call DecryptFileW() while there is an open handle to the
755 * file. So close it first. */
756 if (!CloseHandle(open_handle)) {
757 err = GetLastError();
758 ERROR("Failed to close handle for \"%ls\"", path);
760 return WIMLIB_ERR_WRITE;
762 if (!DecryptFileW(path, 0 /* reserved parameter; set to 0 */)) {
763 err = GetLastError();
764 ERROR("Failed to decrypt file \"%ls\"", path);
766 return WIMLIB_ERR_WRITE;
772 * Create and extract a stream to a file, or create a directory, using the
775 * This handles reparse points, directories, alternate data streams, encrypted
776 * files, compressed files, etc.
778 * @dentry: WIM dentry for the file or directory being extracted.
780 * @path: Path to extract the file to.
782 * @stream_name_utf16:
783 * Name of the stream, or NULL if the stream is unnamed. This will
784 * be called with a NULL stream_name_utf16 before any non-NULL
785 * stream_name_utf16's.
787 * @lte: WIM lookup table entry for the stream. May be NULL to indicate
788 * a stream of length 0.
790 * @args: Additional apply context, including flags indicating supported
793 * Returns 0 on success; nonzero on failure.
796 win32_extract_stream(const struct wim_dentry *dentry,
798 const wchar_t *stream_name_utf16,
799 struct wim_lookup_table_entry *lte,
800 struct apply_args *args)
802 wchar_t *stream_path;
806 DWORD creationDisposition = CREATE_ALWAYS;
807 DWORD requestedAccess;
808 BY_HANDLE_FILE_INFORMATION file_info;
809 unsigned remaining_sharing_violations = 1000;
810 const struct wim_inode *inode = dentry->d_inode;
812 if (stream_name_utf16) {
813 /* Named stream. Create a buffer that contains the UTF-16LE
814 * string [./]path:stream_name_utf16. This is needed to
815 * create and open the stream using CreateFileW(). I'm not
816 * aware of any other APIs to do this. Note: the '$DATA' suffix
817 * seems to be unneeded. Additional note: a "./" prefix needs
818 * to be added when the path is not absolute to avoid ambiguity
819 * with drive letters. */
820 size_t stream_path_nchars;
822 size_t stream_name_nchars;
823 const wchar_t *prefix;
825 path_nchars = wcslen(path);
826 stream_name_nchars = wcslen(stream_name_utf16);
827 stream_path_nchars = path_nchars + 1 + stream_name_nchars;
828 if (path[0] != cpu_to_le16(L'\0') &&
829 path[0] != cpu_to_le16(L'/') &&
830 path[0] != cpu_to_le16(L'\\') &&
831 path[1] != cpu_to_le16(L':'))
834 stream_path_nchars += 2;
838 stream_path = alloca((stream_path_nchars + 1) * sizeof(wchar_t));
839 swprintf(stream_path, L"%ls%ls:%ls",
840 prefix, path, stream_name_utf16);
842 /* Unnamed stream; its path is just the path to the file itself.
844 stream_path = (wchar_t*)path;
846 ret = win32_begin_extract_unnamed_stream(inode, lte, path,
847 &creationDisposition,
853 DEBUG("Opening \"%ls\"", stream_path);
854 /* DELETE access is needed for SetFileShortNameW(), for some reason. */
855 requestedAccess = GENERIC_READ | GENERIC_WRITE | DELETE |
856 ACCESS_SYSTEM_SECURITY;
858 /* Open the stream to be extracted. Depending on what we have set
859 * creationDisposition to, we may be creating this for the first time,
860 * or we may be opening on existing stream we already created using
861 * CreateDirectoryW() or OpenEncryptedFileRawW(). */
862 h = CreateFileW(stream_path,
867 win32_get_create_flags_and_attributes(inode->i_attributes),
869 if (h == INVALID_HANDLE_VALUE) {
870 err = GetLastError();
871 if (err == ERROR_ACCESS_DENIED &&
872 path_is_root_of_drive(stream_path))
877 if ((err == ERROR_PRIVILEGE_NOT_HELD ||
878 err == ERROR_ACCESS_DENIED) &&
879 (requestedAccess & ACCESS_SYSTEM_SECURITY))
881 /* Try opening the file again without privilege to
883 requestedAccess &= ~ACCESS_SYSTEM_SECURITY;
886 if (err == ERROR_SHARING_VIOLATION) {
887 if (remaining_sharing_violations) {
888 --remaining_sharing_violations;
889 /* This can happen when restoring encrypted directories
890 * for some reason. Probably a bug in EncryptFile(). */
891 WARNING("Couldn't open \"%ls\" due to sharing violation; "
892 "re-trying after 100ms", stream_path);
896 ERROR("Too many sharing violations; giving up...");
899 if (creationDisposition == OPEN_EXISTING)
900 ERROR("Failed to open \"%ls\"", stream_path);
902 ERROR("Failed to create \"%ls\"", stream_path);
905 ret = WIMLIB_ERR_OPEN;
909 /* Check the attributes of the file we just opened, and remove
910 * encryption or compression if either was set by default but is not
911 * supposed to be set based on the WIM inode attributes. */
912 if (!GetFileInformationByHandle(h, &file_info)) {
913 err = GetLastError();
914 ERROR("Failed to get attributes of \"%ls\"", stream_path);
916 ret = WIMLIB_ERR_STAT;
917 goto fail_close_handle;
920 /* Remove encryption? */
921 if (file_info.dwFileAttributes & FILE_ATTRIBUTE_ENCRYPTED &&
922 !(inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED))
924 /* File defaulted to encrypted due to being in an encrypted
925 * directory, but is not actually supposed to be encrypted.
927 * This is a workaround, because I'm not aware of any way to
928 * directly (e.g. with CreateFileW()) create an unencrypted file
929 * in a directory with FILE_ATTRIBUTE_ENCRYPTED set. */
930 ret = win32_decrypt_file(h, stream_path);
932 goto fail; /* win32_decrypt_file() closed the handle. */
933 creationDisposition = OPEN_EXISTING;
937 /* Remove compression? */
938 if (file_info.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED &&
939 !(inode->i_attributes & FILE_ATTRIBUTE_COMPRESSED))
941 /* Similar to the encrypted case, above, if the file defaulted
942 * to compressed due to being in an compressed directory, but is
943 * not actually supposed to be compressed, explicitly set the
944 * compression format to COMPRESSION_FORMAT_NONE. */
945 ret = win32_set_compression_state(h, COMPRESSION_FORMAT_NONE,
948 goto fail_close_handle;
951 /* Set compression and/or sparse attributes if needed */
952 ret = win32_set_special_stream_attributes(h, inode, lte, path,
956 goto fail_close_handle;
958 /* At this point we have at least created the needed stream with the
959 * appropriate attributes. We have yet to set the appropriate security
960 * descriptor and actually extract the stream data (other than for
961 * extracted files, which were already extracted).
962 * win32_finish_extract_stream() handles these additional steps. */
963 ret = win32_finish_extract_stream(h, dentry, lte, stream_path,
964 stream_name_utf16, args);
966 goto fail_close_handle;
968 /* Done extracting the stream. Close the handle and return. */
969 DEBUG("Closing \"%ls\"", stream_path);
970 if (!CloseHandle(h)) {
971 err = GetLastError();
972 ERROR("Failed to close \"%ls\"", stream_path);
974 ret = WIMLIB_ERR_WRITE;
982 ERROR("Error extracting \"%ls\"", stream_path);
988 * Creates a file, directory, or reparse point and extracts all streams to it
989 * (unnamed data stream and/or reparse point stream, plus any alternate data
990 * streams). Handles sparse, compressed, and/or encrypted files.
992 * @dentry: WIM dentry for this file or directory.
993 * @path: UTF-16LE external path to extract the inode to.
994 * @args: Additional extraction context.
996 * Returns 0 on success; nonzero on failure.
999 win32_extract_streams(const struct wim_dentry *dentry,
1000 const wchar_t *path, struct apply_args *args)
1002 struct wim_lookup_table_entry *unnamed_lte;
1004 const struct wim_inode *inode = dentry->d_inode;
1006 /* First extract the unnamed stream. */
1008 unnamed_lte = inode_unnamed_lte_resolved(inode);
1009 ret = win32_extract_stream(dentry, path, NULL, unnamed_lte, args);
1013 /* Extract any named streams, if supported by the volume. */
1015 if (!(args->vol_flags & FILE_NAMED_STREAMS))
1017 for (u16 i = 0; i < inode->i_num_ads; i++) {
1018 const struct wim_ads_entry *ads_entry = &inode->i_ads_entries[i];
1020 /* Skip the unnamed stream if it's in the ADS entries (we
1021 * already extracted it...) */
1022 if (ads_entry->stream_name_nbytes == 0)
1025 /* Skip special UNIX data entries (see documentation for
1026 * WIMLIB_ADD_FLAG_UNIX_DATA) */
1027 if (ads_entry->stream_name_nbytes == WIMLIB_UNIX_DATA_TAG_UTF16LE_NBYTES
1028 && !memcmp(ads_entry->stream_name,
1029 WIMLIB_UNIX_DATA_TAG_UTF16LE,
1030 WIMLIB_UNIX_DATA_TAG_UTF16LE_NBYTES))
1033 /* Extract the named stream */
1034 ret = win32_extract_stream(dentry,
1036 ads_entry->stream_name,
1047 dentry_clear_inode_visited(struct wim_dentry *dentry, void *_ignore)
1049 dentry->d_inode->i_visited = 0;
1054 dentry_get_features(struct wim_dentry *dentry, void *_features_p)
1057 DWORD *features_p = _features_p;
1058 struct wim_inode *inode = dentry->d_inode;
1060 if (inode->i_visited) {
1061 features |= FILE_SUPPORTS_HARD_LINKS;
1063 inode->i_visited = 1;
1064 if (inode->i_attributes & FILE_ATTRIBUTE_SPARSE_FILE)
1065 features |= FILE_SUPPORTS_SPARSE_FILES;
1066 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT)
1067 features |= FILE_SUPPORTS_REPARSE_POINTS;
1068 for (unsigned i = 0; i < inode->i_num_ads; i++)
1069 if (inode->i_ads_entries[i].stream_name_nbytes)
1070 features |= FILE_NAMED_STREAMS;
1071 if (inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED)
1072 features |= FILE_SUPPORTS_ENCRYPTION;
1073 if (inode->i_attributes & FILE_ATTRIBUTE_COMPRESSED)
1074 features |= FILE_FILE_COMPRESSION;
1075 if (inode->i_security_id != -1)
1076 features |= FILE_PERSISTENT_ACLS;
1078 *features_p |= features;
1082 /* If not done already, load the supported feature flags for the volume onto
1083 * which the image is being extracted, and warn the user about any missing
1084 * features that could be important. */
1086 win32_check_vol_flags(const wchar_t *output_path,
1087 struct wim_dentry *root, struct apply_args *args)
1089 DWORD dentry_features = 0;
1090 DWORD missing_features;
1092 if (args->have_vol_flags)
1095 for_dentry_in_tree(root, dentry_clear_inode_visited, NULL);
1096 for_dentry_in_tree(root, dentry_get_features, &dentry_features);
1098 win32_get_vol_flags(output_path, &args->vol_flags);
1099 args->have_vol_flags = true;
1101 missing_features = dentry_features & ~args->vol_flags;
1103 /* Warn the user about data that may not be extracted. */
1104 if (missing_features & FILE_SUPPORTS_SPARSE_FILES)
1105 WARNING("Volume does not support sparse files!\n"
1106 " Sparse files will be extracted as non-sparse.");
1107 if (missing_features & FILE_SUPPORTS_REPARSE_POINTS)
1108 WARNING("Volume does not support reparse points!\n"
1109 " Reparse point data will not be extracted.");
1110 if (missing_features & FILE_NAMED_STREAMS) {
1111 WARNING("Volume does not support named data streams!\n"
1112 " Named data streams will not be extracted.");
1114 if (missing_features & FILE_SUPPORTS_ENCRYPTION) {
1115 WARNING("Volume does not support encryption!\n"
1116 " Encrypted files will be extracted as raw data.");
1118 if (missing_features & FILE_FILE_COMPRESSION) {
1119 WARNING("Volume does not support transparent compression!\n"
1120 " Compressed files will be extracted as non-compressed.");
1122 if (missing_features & FILE_PERSISTENT_ACLS) {
1123 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_ACLS) {
1124 ERROR("Strict ACLs requested, but the volume does not "
1126 return WIMLIB_ERR_VOLUME_LACKS_FEATURES;
1128 WARNING("Volume does not support persistent ACLS!\n"
1129 " File permissions will not be extracted.");
1132 if (running_on_windows_7_or_later() &&
1133 (missing_features & FILE_SUPPORTS_HARD_LINKS))
1135 WARNING("Volume does not support hard links!\n"
1136 " Hard links will be extracted as duplicate files.");
1142 * Try extracting a hard link.
1144 * @output_path: Path to link to be extracted.
1146 * @inode: WIM inode that the link is to; inode->i_extracted_file
1147 * the path to a name of the file that has already been
1148 * extracted (we use this to create the hard link).
1150 * @args: Additional apply context, used here to keep track of
1151 * the number of times creating a hard link failed due to
1152 * ERROR_INVALID_FUNCTION. This error should indicate that hard
1153 * links are not supported by the volume, and we would like to
1154 * warn the user a few times, but not too many times.
1156 * Returns 0 if the hard link was successfully extracted. Returns
1157 * WIMLIB_ERR_LINK (> 0) if an error occurred, other than hard links possibly
1158 * being unsupported by the volume. Returns a negative value if creating the
1159 * hard link failed due to ERROR_INVALID_FUNCTION.
1162 win32_try_hard_link(const wchar_t *output_path, const struct wim_inode *inode,
1163 struct apply_args *args)
1167 /* There is a volume flag for this (FILE_SUPPORTS_HARD_LINKS),
1168 * but it's only available on Windows 7 and later.
1170 * Otherwise, CreateHardLinkW() will apparently return
1171 * ERROR_INVALID_FUNCTION if the volume does not support hard links. */
1173 DEBUG("Creating hard link \"%ls => %ls\"",
1174 output_path, inode->i_extracted_file);
1176 if (running_on_windows_7_or_later() &&
1177 !(args->vol_flags & FILE_SUPPORTS_HARD_LINKS))
1178 goto hard_links_unsupported;
1180 if (CreateHardLinkW(output_path, inode->i_extracted_file, NULL))
1183 err = GetLastError();
1184 if (err != ERROR_INVALID_FUNCTION) {
1185 ERROR("Can't create hard link \"%ls => %ls\"",
1186 output_path, inode->i_extracted_file);
1188 return WIMLIB_ERR_LINK;
1190 hard_links_unsupported:
1191 args->num_hard_links_failed++;
1192 if (args->num_hard_links_failed <= MAX_CREATE_HARD_LINK_WARNINGS) {
1193 if (running_on_windows_7_or_later())
1195 WARNING("Extracting duplicate copy of \"%ls\" "
1196 "rather than hard link", output_path);
1198 WARNING("Can't create hard link \"%ls\" => \"%ls\":\n"
1199 " Volume does not support hard links!\n"
1200 " Falling back to extracting a copy of the file.",
1201 output_path, inode->i_extracted_file);
1204 if (args->num_hard_links_failed == MAX_CREATE_HARD_LINK_WARNINGS)
1205 WARNING("Suppressing further hard linking warnings...");
1209 /* Extract a file, directory, reparse point, or hard link to an
1210 * already-extracted file using the Win32 API */
1212 win32_do_apply_dentry(const wchar_t *output_path,
1213 size_t output_path_num_chars,
1214 struct wim_dentry *dentry,
1215 struct apply_args *args)
1218 struct wim_inode *inode = dentry->d_inode;
1220 ret = win32_check_vol_flags(output_path, dentry, args);
1223 if (inode->i_nlink > 1 && inode->i_extracted_file != NULL) {
1224 /* Linked file, with another name already extracted. Create a
1226 ret = win32_try_hard_link(output_path, inode, args);
1229 /* Negative return value from win32_try_hard_link() indicates
1230 * that hard links are probably not supported by the volume.
1231 * Fall back to extracting a copy of the file. */
1234 /* If this is a reparse point and the volume does not support reparse
1235 * points, just skip it completely. */
1236 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT &&
1237 !(args->vol_flags & FILE_SUPPORTS_REPARSE_POINTS))
1239 WARNING("Not extracting reparse point \"%ls\"", output_path);
1241 /* Create the file, directory, or reparse point, and extract the
1243 ret = win32_extract_streams(dentry, output_path, args);
1247 if (inode->i_extracted_file == NULL) {
1248 const struct wim_lookup_table_entry *lte;
1250 /* Tally bytes extracted, including all alternate data streams,
1251 * unless we extracted a hard link (or, at least extracted a
1252 * name that was supposed to be a hard link) */
1253 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
1254 lte = inode_stream_lte_resolved(inode, i);
1256 args->progress.extract.completed_bytes +=
1257 wim_resource_size(lte);
1259 if (inode->i_nlink > 1) {
1260 /* Save extracted path for a later call to
1261 * CreateHardLinkW() if this inode has multiple links.
1263 inode->i_extracted_file = WSTRDUP(output_path);
1264 if (!inode->i_extracted_file)
1265 return WIMLIB_ERR_NOMEM;
1271 /* Set timestamps on an extracted file using the Win32 API */
1273 win32_do_apply_dentry_timestamps(const wchar_t *path,
1274 size_t path_num_chars,
1275 struct wim_dentry *dentry,
1276 struct apply_args *args)
1280 const struct wim_inode *inode = dentry->d_inode;
1282 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT &&
1283 !(args->vol_flags & FILE_SUPPORTS_REPARSE_POINTS))
1285 /* Skip reparse points not extracted */
1289 /* Windows doesn't let you change the timestamps of the root directory
1290 * (at least on FAT, which is dumb but expected since FAT doesn't store
1291 * any metadata about the root directory...) */
1292 if (path_is_root_of_drive(path))
1295 DEBUG("Opening \"%ls\" to set timestamps", path);
1296 h = win32_open_existing_file(path, FILE_WRITE_ATTRIBUTES);
1297 if (h == INVALID_HANDLE_VALUE) {
1298 err = GetLastError();
1302 FILETIME creationTime = {.dwLowDateTime = inode->i_creation_time & 0xffffffff,
1303 .dwHighDateTime = inode->i_creation_time >> 32};
1304 FILETIME lastAccessTime = {.dwLowDateTime = inode->i_last_access_time & 0xffffffff,
1305 .dwHighDateTime = inode->i_last_access_time >> 32};
1306 FILETIME lastWriteTime = {.dwLowDateTime = inode->i_last_write_time & 0xffffffff,
1307 .dwHighDateTime = inode->i_last_write_time >> 32};
1309 DEBUG("Calling SetFileTime() on \"%ls\"", path);
1310 if (!SetFileTime(h, &creationTime, &lastAccessTime, &lastWriteTime)) {
1311 err = GetLastError();
1315 DEBUG("Closing \"%ls\"", path);
1316 if (!CloseHandle(h)) {
1317 err = GetLastError();
1322 /* Only warn if setting timestamps failed; still return 0. */
1323 WARNING("Can't set timestamps on \"%ls\"", path);
1329 #endif /* __WIN32__ */