4 * All the library code specific to native Windows builds is in here.
8 * Copyright (C) 2013 Eric Biggers
10 * This file is part of wimlib, a library for working with WIM files.
12 * wimlib is free software; you can redistribute it and/or modify it under the
13 * terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 3 of the License, or (at your option)
17 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
18 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19 * A PARTICULAR PURPOSE. See the GNU General Public License for more
22 * You should have received a copy of the GNU General Public License
23 * along with wimlib; if not, see http://www.gnu.org/licenses/.
32 #include <shlwapi.h> /* for PathMatchSpecW() */
33 #include <aclapi.h> /* for SetSecurityInfo() */
34 #ifdef ERROR /* windows.h defines this */
40 #include "lookup_table.h"
42 #include "endianness.h"
43 #include "buffer_io.h"
48 #define MAX_GET_SD_ACCESS_DENIED_WARNINGS 1
49 #define MAX_GET_SACL_PRIV_NOTHELD_WARNINGS 1
50 #define MAX_CREATE_HARD_LINK_WARNINGS 5
51 struct win32_capture_state {
52 unsigned long num_get_sd_access_denied;
53 unsigned long num_get_sacl_priv_notheld;
56 #define MAX_SET_SD_ACCESS_DENIED_WARNINGS 1
57 #define MAX_SET_SACL_PRIV_NOTHELD_WARNINGS 1
59 #ifdef ENABLE_ERROR_MESSAGES
61 win32_error(u32 err_code)
65 nchars = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
66 FORMAT_MESSAGE_ALLOCATE_BUFFER,
68 (wchar_t*)&buffer, 0, NULL);
70 ERROR("Error printing error message! "
71 "Computer will self-destruct in 3 seconds.");
73 ERROR("Win32 error: %ls", buffer);
77 #else /* ENABLE_ERROR_MESSAGES */
78 # define win32_error(err_code)
79 #endif /* !ENABLE_ERROR_MESSAGES */
82 win32_error_to_errno(DWORD err_code)
84 /* This mapping is that used in Cygwin.
85 * Some of these choices are arbitrary. */
87 case ERROR_ACCESS_DENIED:
89 case ERROR_ACTIVE_CONNECTIONS:
91 case ERROR_ALREADY_EXISTS:
93 case ERROR_BAD_DEVICE:
95 case ERROR_BAD_EXE_FORMAT:
97 case ERROR_BAD_NETPATH:
99 case ERROR_BAD_NET_NAME:
101 case ERROR_BAD_NET_RESP:
103 case ERROR_BAD_PATHNAME:
109 case ERROR_BAD_USERNAME:
111 case ERROR_BEGINNING_OF_MEDIA:
113 case ERROR_BROKEN_PIPE:
117 case ERROR_BUS_RESET:
119 case ERROR_CALL_NOT_IMPLEMENTED:
121 case ERROR_CANNOT_MAKE:
123 case ERROR_CHILD_NOT_COMPLETE:
125 case ERROR_COMMITMENT_LIMIT:
129 case ERROR_DEVICE_DOOR_OPEN:
131 case ERROR_DEVICE_IN_USE:
133 case ERROR_DEVICE_REQUIRES_CLEANING:
135 case ERROR_DIRECTORY:
137 case ERROR_DIR_NOT_EMPTY:
139 case ERROR_DISK_CORRUPT:
141 case ERROR_DISK_FULL:
147 case ERROR_EAS_DIDNT_FIT:
149 case ERROR_EAS_NOT_SUPPORTED:
151 case ERROR_EA_LIST_INCONSISTENT:
153 case ERROR_EA_TABLE_FULL:
155 case ERROR_END_OF_MEDIA:
157 case ERROR_EOM_OVERFLOW:
159 case ERROR_EXE_MACHINE_TYPE_MISMATCH:
161 case ERROR_EXE_MARKED_INVALID:
163 case ERROR_FILEMARK_DETECTED:
165 case ERROR_FILENAME_EXCED_RANGE:
167 case ERROR_FILE_CORRUPT:
169 case ERROR_FILE_EXISTS:
171 case ERROR_FILE_INVALID:
173 case ERROR_FILE_NOT_FOUND:
175 case ERROR_HANDLE_DISK_FULL:
178 case ERROR_HANDLE_EOF:
181 case ERROR_INVALID_ADDRESS:
183 case ERROR_INVALID_AT_INTERRUPT_TIME:
185 case ERROR_INVALID_BLOCK_LENGTH:
187 case ERROR_INVALID_DATA:
189 case ERROR_INVALID_DRIVE:
191 case ERROR_INVALID_EA_NAME:
193 case ERROR_INVALID_EXE_SIGNATURE:
196 case ERROR_INVALID_FUNCTION:
199 case ERROR_INVALID_HANDLE:
201 case ERROR_INVALID_NAME:
203 case ERROR_INVALID_PARAMETER:
205 case ERROR_INVALID_SIGNAL_NUMBER:
207 case ERROR_IOPL_NOT_ENABLED:
209 case ERROR_IO_DEVICE:
211 case ERROR_IO_INCOMPLETE:
213 case ERROR_IO_PENDING:
215 case ERROR_LOCK_VIOLATION:
217 case ERROR_MAX_THRDS_REACHED:
219 case ERROR_META_EXPANSION_TOO_LONG:
221 case ERROR_MOD_NOT_FOUND:
224 case ERROR_MORE_DATA:
227 case ERROR_NEGATIVE_SEEK:
229 case ERROR_NETNAME_DELETED:
233 case ERROR_NONE_MAPPED:
235 case ERROR_NONPAGED_SYSTEM_RESOURCES:
238 case ERROR_NOT_CONNECTED:
241 case ERROR_NOT_ENOUGH_MEMORY:
243 case ERROR_NOT_OWNER:
246 case ERROR_NOT_READY:
249 case ERROR_NOT_SAME_DEVICE:
251 case ERROR_NOT_SUPPORTED:
255 case ERROR_NO_DATA_DETECTED:
258 case ERROR_NO_MEDIA_IN_DRIVE:
262 case ERROR_NO_MORE_FILES:
266 case ERROR_NO_MORE_ITEMS:
269 case ERROR_NO_MORE_SEARCH_HANDLES:
271 case ERROR_NO_PROC_SLOTS:
273 case ERROR_NO_SIGNAL_SENT:
275 case ERROR_NO_SYSTEM_RESOURCES:
279 case ERROR_OPEN_FAILED:
281 case ERROR_OPEN_FILES:
283 case ERROR_OUTOFMEMORY:
285 case ERROR_PAGED_SYSTEM_RESOURCES:
287 case ERROR_PAGEFILE_QUOTA:
289 case ERROR_PATH_NOT_FOUND:
291 case ERROR_PIPE_BUSY:
293 case ERROR_PIPE_CONNECTED:
296 case ERROR_PIPE_LISTENING:
298 case ERROR_PIPE_NOT_CONNECTED:
301 case ERROR_POSSIBLE_DEADLOCK:
303 case ERROR_PRIVILEGE_NOT_HELD:
305 case ERROR_PROCESS_ABORTED:
307 case ERROR_PROC_NOT_FOUND:
310 case ERROR_REM_NOT_LIST:
313 case ERROR_SECTOR_NOT_FOUND:
317 case ERROR_SETMARK_DETECTED:
319 case ERROR_SHARING_BUFFER_EXCEEDED:
321 case ERROR_SHARING_VIOLATION:
323 case ERROR_SIGNAL_PENDING:
325 case ERROR_SIGNAL_REFUSED:
328 case ERROR_SXS_CANT_GEN_ACTCTX:
331 case ERROR_THREAD_1_INACTIVE:
333 case ERROR_TOO_MANY_LINKS:
335 case ERROR_TOO_MANY_OPEN_FILES:
337 case ERROR_WAIT_NO_CHILDREN:
339 case ERROR_WORKING_SET_QUOTA:
341 case ERROR_WRITE_PROTECT:
349 set_errno_from_GetLastError()
351 errno = win32_error_to_errno(GetLastError());
354 /* Pointers to functions that are not available on all targetted versions of
355 * Windows (XP and later). NOTE: The WINAPI annotations seem to be important; I
356 * assume it specifies a certain calling convention. */
358 /* Vista and later */
359 static HANDLE (WINAPI *win32func_FindFirstStreamW)(LPCWSTR lpFileName,
360 STREAM_INFO_LEVELS InfoLevel,
361 LPVOID lpFindStreamData,
362 DWORD dwFlags) = NULL;
364 /* Vista and later */
365 static BOOL (WINAPI *win32func_FindNextStreamW)(HANDLE hFindStream,
366 LPVOID lpFindStreamData) = NULL;
368 static HMODULE hKernel32 = NULL;
370 /* Try to dynamically load some functions */
376 if (hKernel32 == NULL) {
377 DEBUG("Loading Kernel32.dll");
378 hKernel32 = LoadLibraryW(L"Kernel32.dll");
379 if (hKernel32 == NULL) {
380 err = GetLastError();
381 WARNING("Can't load Kernel32.dll");
387 DEBUG("Looking for FindFirstStreamW");
388 win32func_FindFirstStreamW = (void*)GetProcAddress(hKernel32, "FindFirstStreamW");
389 if (!win32func_FindFirstStreamW) {
390 WARNING("Could not find function FindFirstStreamW() in Kernel32.dll!");
391 WARNING("Capturing alternate data streams will not be supported.");
395 DEBUG("Looking for FindNextStreamW");
396 win32func_FindNextStreamW = (void*)GetProcAddress(hKernel32, "FindNextStreamW");
397 if (!win32func_FindNextStreamW) {
398 WARNING("Could not find function FindNextStreamW() in Kernel32.dll!");
399 WARNING("Capturing alternate data streams will not be supported.");
400 win32func_FindFirstStreamW = NULL;
405 win32_global_cleanup()
407 if (hKernel32 != NULL) {
408 DEBUG("Closing Kernel32.dll");
409 FreeLibrary(hKernel32);
414 static const wchar_t *capture_access_denied_msg =
415 L" If you are not running this program as the administrator, you may\n"
416 " need to do so, so that all data and metadata can be backed up.\n"
417 " Otherwise, there may be no way to access the desired data or\n"
418 " metadata without taking ownership of the file or directory.\n"
421 static const wchar_t *apply_access_denied_msg =
422 L"If you are not running this program as the administrator, you may\n"
423 " need to do so, so that all data and metadata can be extracted\n"
424 " exactly as the origignal copy. However, if you do not care that\n"
425 " the security descriptors are extracted correctly, you could run\n"
426 " `wimlib-imagex apply' with the --no-acls flag instead.\n"
430 win32_open_existing_file(const wchar_t *path, DWORD dwDesiredAccess)
432 return CreateFileW(path,
435 NULL, /* lpSecurityAttributes */
437 FILE_FLAG_BACKUP_SEMANTICS |
438 FILE_FLAG_OPEN_REPARSE_POINT,
439 NULL /* hTemplateFile */);
443 win32_open_file_data_only(const wchar_t *path)
445 return win32_open_existing_file(path, FILE_READ_DATA);
449 read_win32_file_prefix(const struct wim_lookup_table_entry *lte,
451 consume_data_callback_t cb,
460 HANDLE hFile = win32_open_file_data_only(lte->file_on_disk);
461 if (hFile == INVALID_HANDLE_VALUE) {
462 err = GetLastError();
463 ERROR("Failed to open \"%ls\"", lte->file_on_disk);
465 return WIMLIB_ERR_OPEN;
469 out_buf = alloca(WIM_CHUNK_SIZE);
471 out_buf = ctx_or_buf;
473 bytes_remaining = size;
474 while (bytes_remaining) {
475 DWORD bytesToRead, bytesRead;
477 bytesToRead = min(WIM_CHUNK_SIZE, bytes_remaining);
478 if (!ReadFile(hFile, out_buf, bytesToRead, &bytesRead, NULL) ||
479 bytesRead != bytesToRead)
481 err = GetLastError();
482 ERROR("Failed to read data from \"%ls\"", lte->file_on_disk);
484 ret = WIMLIB_ERR_READ;
487 bytes_remaining -= bytesRead;
489 ret = (*cb)(out_buf, bytesRead, ctx_or_buf);
493 out_buf += bytesRead;
500 struct win32_encrypted_read_ctx {
501 consume_data_callback_t read_prefix_cb;
502 void *read_prefix_ctx_or_buf;
510 win32_encrypted_export_cb(unsigned char *_data, void *_ctx, unsigned long len)
512 const void *data = _data;
513 struct win32_encrypted_read_ctx *ctx = _ctx;
516 DEBUG("len = %lu", len);
517 if (ctx->read_prefix_cb) {
518 /* The length of the buffer passed to the ReadEncryptedFileRaw()
519 * export callback is undocumented, so we assume it may be of
521 size_t bytes_to_buffer = min(ctx->bytes_remaining - ctx->buf_filled,
523 while (bytes_to_buffer) {
524 size_t bytes_to_copy_to_buf =
525 min(bytes_to_buffer, WIM_CHUNK_SIZE - ctx->buf_filled);
527 memcpy(ctx->buf + ctx->buf_filled, data,
528 bytes_to_copy_to_buf);
529 ctx->buf_filled += bytes_to_copy_to_buf;
530 data += bytes_to_copy_to_buf;
531 bytes_to_buffer -= bytes_to_copy_to_buf;
533 if (ctx->buf_filled == WIM_CHUNK_SIZE ||
534 ctx->buf_filled == ctx->bytes_remaining)
536 ret = (*ctx->read_prefix_cb)(ctx->buf,
538 ctx->read_prefix_ctx_or_buf);
540 ctx->wimlib_err_code = ret;
541 /* Shouldn't matter what error code is returned
542 * here, as long as it isn't ERROR_SUCCESS. */
543 return ERROR_READ_FAULT;
545 ctx->bytes_remaining -= ctx->buf_filled;
550 size_t len_to_copy = min(len, ctx->bytes_remaining);
551 memcpy(ctx->read_prefix_ctx_or_buf, data, len_to_copy);
552 ctx->bytes_remaining -= len_to_copy;
553 ctx->read_prefix_ctx_or_buf += len_to_copy;
555 return ERROR_SUCCESS;
559 read_win32_encrypted_file_prefix(const struct wim_lookup_table_entry *lte,
561 consume_data_callback_t cb,
565 struct win32_encrypted_read_ctx export_ctx;
570 DEBUG("Reading %"PRIu64" bytes from encryted file \"%ls\"",
571 size, lte->file_on_disk);
573 export_ctx.read_prefix_cb = cb;
574 export_ctx.read_prefix_ctx_or_buf = ctx_or_buf;
575 export_ctx.wimlib_err_code = 0;
577 export_ctx.buf = MALLOC(WIM_CHUNK_SIZE);
579 return WIMLIB_ERR_NOMEM;
581 export_ctx.buf = NULL;
583 export_ctx.buf_filled = 0;
584 export_ctx.bytes_remaining = size;
586 err = OpenEncryptedFileRawW(lte->file_on_disk, 0, &file_ctx);
587 if (err != ERROR_SUCCESS) {
588 ERROR("Failed to open encrypted file \"%ls\" for raw read",
591 ret = WIMLIB_ERR_OPEN;
594 err = ReadEncryptedFileRaw(win32_encrypted_export_cb,
595 &export_ctx, file_ctx);
596 if (err != ERROR_SUCCESS) {
597 ERROR("Failed to read encrypted file \"%ls\"",
600 ret = export_ctx.wimlib_err_code;
602 ret = WIMLIB_ERR_READ;
603 } else if (export_ctx.bytes_remaining != 0) {
604 ERROR("Only could read %"PRIu64" of %"PRIu64" bytes from "
605 "encryted file \"%ls\"",
606 size - export_ctx.bytes_remaining, size,
608 ret = WIMLIB_ERR_READ;
612 CloseEncryptedFileRaw(file_ctx);
614 FREE(export_ctx.buf);
618 /* Given a path, which may not yet exist, get a set of flags that describe the
619 * features of the volume the path is on. */
621 win32_get_vol_flags(const wchar_t *path, unsigned *vol_flags_ret)
627 if (path[0] != L'\0' && path[0] != L'\\' &&
628 path[0] != L'/' && path[1] == L':')
630 /* Path starts with a drive letter; use it. */
631 volume = alloca(4 * sizeof(wchar_t));
637 /* Path does not start with a drive letter; use the volume of
638 * the current working directory. */
641 bret = GetVolumeInformationW(volume, /* lpRootPathName */
642 NULL, /* lpVolumeNameBuffer */
643 0, /* nVolumeNameSize */
644 NULL, /* lpVolumeSerialNumber */
645 NULL, /* lpMaximumComponentLength */
646 &vol_flags, /* lpFileSystemFlags */
647 NULL, /* lpFileSystemNameBuffer */
648 0); /* nFileSystemNameSize */
650 DWORD err = GetLastError();
651 WARNING("Failed to get volume information for path \"%ls\"", path);
653 vol_flags = 0xffffffff;
656 DEBUG("using vol_flags = %x", vol_flags);
657 *vol_flags_ret = vol_flags;
663 FILETIME_to_u64(const FILETIME *ft)
665 return ((u64)ft->dwHighDateTime << 32) | (u64)ft->dwLowDateTime;
669 win32_get_short_name(struct wim_dentry *dentry, const wchar_t *path)
671 WIN32_FIND_DATAW dat;
675 /* If we can't read the short filename for some reason, we just ignore
676 * the error and assume the file has no short name. I don't think this
677 * should be an issue, since the short names are essentially obsolete
679 hFind = FindFirstFileW(path, &dat);
680 if (hFind != INVALID_HANDLE_VALUE) {
681 if (dat.cAlternateFileName[0] != L'\0') {
682 DEBUG("\"%ls\": short name \"%ls\"", path, dat.cAlternateFileName);
683 size_t short_name_nbytes = wcslen(dat.cAlternateFileName) *
685 size_t n = short_name_nbytes + sizeof(wchar_t);
686 dentry->short_name = MALLOC(n);
687 if (dentry->short_name) {
688 memcpy(dentry->short_name, dat.cAlternateFileName, n);
689 dentry->short_name_nbytes = short_name_nbytes;
691 ret = WIMLIB_ERR_NOMEM;
700 win32_get_security_descriptor(struct wim_dentry *dentry,
701 struct sd_set *sd_set,
703 struct win32_capture_state *state,
706 SECURITY_INFORMATION requestedInformation;
712 requestedInformation = DACL_SECURITY_INFORMATION |
713 SACL_SECURITY_INFORMATION |
714 OWNER_SECURITY_INFORMATION |
715 GROUP_SECURITY_INFORMATION;
717 /* Request length of security descriptor */
718 status = GetFileSecurityW(path, requestedInformation,
719 NULL, 0, &lenNeeded);
720 err = GetLastError();
721 if (!status && err == ERROR_INSUFFICIENT_BUFFER) {
722 DWORD len = lenNeeded;
724 if (GetFileSecurityW(path, requestedInformation,
725 (PSECURITY_DESCRIPTOR)buf, len, &lenNeeded))
727 int security_id = sd_set_add_sd(sd_set, buf, len);
729 return WIMLIB_ERR_NOMEM;
731 dentry->d_inode->i_security_id = security_id;
735 err = GetLastError();
739 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS)
743 case ERROR_PRIVILEGE_NOT_HELD:
744 if (requestedInformation & SACL_SECURITY_INFORMATION) {
745 n = state->num_get_sacl_priv_notheld++;
746 requestedInformation &= ~SACL_SECURITY_INFORMATION;
747 if (n < MAX_GET_SACL_PRIV_NOTHELD_WARNINGS) {
749 "We don't have enough privileges to read the full security\n"
750 " descriptor of \"%ls\"!\n"
751 " Re-trying with SACL omitted.\n", path);
752 } else if (n == MAX_GET_SACL_PRIV_NOTHELD_WARNINGS) {
754 "Suppressing further privileges not held error messages when reading\n"
755 " security descriptors.");
760 case ERROR_ACCESS_DENIED:
761 n = state->num_get_sd_access_denied++;
762 if (n < MAX_GET_SD_ACCESS_DENIED_WARNINGS) {
763 WARNING("Failed to read security descriptor of \"%ls\": "
764 "Access denied!\n%ls", path, capture_access_denied_msg);
765 } else if (n == MAX_GET_SD_ACCESS_DENIED_WARNINGS) {
766 WARNING("Suppressing further access denied errors messages i"
767 "when reading security descriptors");
772 ERROR("Failed to read security descriptor of \"%ls\"", path);
774 return WIMLIB_ERR_READ;
779 win32_build_dentry_tree_recursive(struct wim_dentry **root_ret,
781 size_t path_num_chars,
782 struct add_image_params *params,
783 struct win32_capture_state *state,
786 /* Reads the directory entries of directory using a Win32 API and recursively
787 * calls win32_build_dentry_tree() on them. */
789 win32_recurse_directory(struct wim_dentry *root,
791 size_t dir_path_num_chars,
792 struct add_image_params *params,
793 struct win32_capture_state *state,
796 WIN32_FIND_DATAW dat;
801 DEBUG("Recurse to directory \"%ls\"", dir_path);
803 /* Begin reading the directory by calling FindFirstFileW. Unlike UNIX
804 * opendir(), FindFirstFileW has file globbing built into it. But this
805 * isn't what we actually want, so just add a dummy glob to get all
807 dir_path[dir_path_num_chars] = L'/';
808 dir_path[dir_path_num_chars + 1] = L'*';
809 dir_path[dir_path_num_chars + 2] = L'\0';
810 hFind = FindFirstFileW(dir_path, &dat);
811 dir_path[dir_path_num_chars] = L'\0';
813 if (hFind == INVALID_HANDLE_VALUE) {
814 err = GetLastError();
815 if (err == ERROR_FILE_NOT_FOUND) {
818 ERROR("Failed to read directory \"%ls\"", dir_path);
820 return WIMLIB_ERR_READ;
825 /* Skip . and .. entries */
826 if (dat.cFileName[0] == L'.' &&
827 (dat.cFileName[1] == L'\0' ||
828 (dat.cFileName[1] == L'.' &&
829 dat.cFileName[2] == L'\0')))
831 size_t filename_len = wcslen(dat.cFileName);
833 dir_path[dir_path_num_chars] = L'/';
834 wmemcpy(dir_path + dir_path_num_chars + 1,
838 struct wim_dentry *child;
839 size_t path_len = dir_path_num_chars + 1 + filename_len;
840 ret = win32_build_dentry_tree_recursive(&child,
846 dir_path[dir_path_num_chars] = L'\0';
850 dentry_add_child(root, child);
851 } while (FindNextFileW(hFind, &dat));
852 err = GetLastError();
853 if (err != ERROR_NO_MORE_FILES) {
854 ERROR("Failed to read directory \"%ls\"", dir_path);
857 ret = WIMLIB_ERR_READ;
865 win32_get_file_and_vol_ids(const wchar_t *path, u64 *ino_ret, u64 *dev_ret)
869 BY_HANDLE_FILE_INFORMATION file_info;
872 hFile = win32_open_existing_file(path, FILE_READ_ATTRIBUTES);
873 if (hFile == INVALID_HANDLE_VALUE) {
874 err = GetLastError();
875 if (err != ERROR_FILE_NOT_FOUND) {
876 WARNING("Failed to open \"%ls\" to get file "
877 "and volume IDs", path);
880 return WIMLIB_ERR_OPEN;
883 if (!GetFileInformationByHandle(hFile, &file_info)) {
884 err = GetLastError();
885 ERROR("Failed to get file information for \"%ls\"", path);
887 ret = WIMLIB_ERR_STAT;
889 *ino_ret = ((u64)file_info.nFileIndexHigh << 32) |
890 (u64)file_info.nFileIndexLow;
891 *dev_ret = file_info.dwVolumeSerialNumber;
898 /* Reparse point fixup status code */
900 /* Reparse point corresponded to an absolute symbolic link or junction
901 * point that pointed outside the directory tree being captured, and
902 * therefore was excluded. */
905 /* Reparse point was not fixed as it was either a relative symbolic
906 * link, a mount point, or something else we could not understand. */
909 /* Reparse point corresponded to an absolute symbolic link or junction
910 * point that pointed inside the directory tree being captured, where
911 * the target was specified by a "full" \??\ prefixed path, and
912 * therefore was fixed to be relative to the root of the directory tree
914 RP_FIXED_FULLPATH = 0x2,
916 /* Same as RP_FIXED_FULLPATH, except the absolute link target did not
917 * have the \??\ prefix. It may have begun with a drive letter though.
919 RP_FIXED_ABSPATH = 0x4,
921 /* Either RP_FIXED_FULLPATH or RP_FIXED_ABSPATH. */
922 RP_FIXED = RP_FIXED_FULLPATH | RP_FIXED_ABSPATH,
925 /* Given the "substitute name" target of a Windows reparse point, try doing a
926 * fixup where we change it to be absolute relative to the root of the directory
927 * tree being captured.
929 * Note that this is only executed when WIMLIB_ADD_IMAGE_FLAG_RPFIX has been
932 * @capture_root_ino and @capture_root_dev indicate the inode number and device
933 * of the root of the directory tree being captured. They are meant to identify
934 * this directory (as an alternative to its actual path, which could potentially
935 * be reached via multiple destinations due to other symbolic links). This may
936 * not work properly on FAT, which doesn't seem to supply proper inode numbers
937 * or file IDs. However, FAT doesn't support reparse points so this function
938 * wouldn't even be called anyway.
940 static enum rp_status
941 win32_capture_maybe_rpfix_target(wchar_t *target, u16 *target_nbytes_p,
942 u64 capture_root_ino, u64 capture_root_dev,
945 u16 target_nchars = *target_nbytes_p / 2;
946 size_t stripped_chars;
947 wchar_t *orig_target;
950 ret = parse_substitute_name(target, *target_nbytes_p, rptag);
953 stripped_chars = ret;
956 target[target_nchars] = L'\0';
957 orig_target = target;
958 target = capture_fixup_absolute_symlink(target + stripped_chars,
959 capture_root_ino, capture_root_dev);
962 target_nchars = wcslen(target);
963 wmemmove(orig_target + stripped_chars, target, target_nchars + 1);
964 *target_nbytes_p = (target_nchars + stripped_chars) * sizeof(wchar_t);
965 DEBUG("Fixed reparse point (new target: \"%ls\")", orig_target);
967 return RP_FIXED_FULLPATH;
969 return RP_FIXED_ABSPATH;
972 /* Returns: `enum rp_status' value on success; negative WIMLIB_ERR_* value on
975 win32_capture_try_rpfix(u8 *rpbuf, u16 *rpbuflen_p,
976 u64 capture_root_ino, u64 capture_root_dev,
979 struct reparse_data rpdata;
982 enum rp_status rp_status;
984 rpbuflen = *rpbuflen_p;
985 ret = parse_reparse_data(rpbuf, rpbuflen, &rpdata);
989 rp_status = win32_capture_maybe_rpfix_target(rpdata.substitute_name,
990 &rpdata.substitute_name_nbytes,
993 le32_to_cpu(*(u32*)rpbuf));
994 if (rp_status & RP_FIXED) {
995 wimlib_assert(rpdata.substitute_name_nbytes % 2 == 0);
996 utf16lechar substitute_name_copy[rpdata.substitute_name_nbytes / 2];
997 wmemcpy(substitute_name_copy, rpdata.substitute_name,
998 rpdata.substitute_name_nbytes / 2);
999 rpdata.substitute_name = substitute_name_copy;
1000 rpdata.print_name = substitute_name_copy;
1001 rpdata.print_name_nbytes = rpdata.substitute_name_nbytes;
1002 if (rp_status == RP_FIXED_FULLPATH) {
1003 /* "full path", meaning \??\ prefixed. We should not
1004 * include this prefix in the print name, as it is
1005 * apparently meant for the filesystem driver only. */
1006 rpdata.print_name += 4;
1007 rpdata.print_name_nbytes -= 8;
1009 ret = make_reparse_buffer(&rpdata, rpbuf);
1015 if (rp_status == RP_EXCLUDED) {
1016 size_t print_name_nchars = rpdata.print_name_nbytes / 2;
1017 wchar_t print_name0[print_name_nchars + 1];
1018 print_name0[print_name_nchars] = L'\0';
1019 wmemcpy(print_name0, rpdata.print_name, print_name_nchars);
1020 WARNING("Ignoring %ls pointing out of capture directory:\n"
1021 " \"%ls\" -> \"%ls\"\n"
1022 " (Use --norpfix to capture all symbolic links "
1023 "and junction points as-is)",
1024 (rpdata.rptag == WIM_IO_REPARSE_TAG_SYMLINK) ?
1025 L"absolute symbolic link" : L"junction point",
1034 * Loads the reparse point data from a reparse point into memory, optionally
1035 * fixing the targets of absolute symbolic links and junction points to be
1036 * relative to the root of capture.
1038 * @hFile: Open handle to the reparse point.
1039 * @path: Path to the reparse point. Used for error messages only.
1040 * @params: Additional parameters, including whether to do reparse point fixups
1042 * @rpbuf: Buffer of length at least REPARSE_POINT_MAX_SIZE bytes into which
1043 * the reparse point buffer will be loaded.
1044 * @rpbuflen_ret: On success, the length of the reparse point buffer in bytes
1045 * is written to this location.
1048 * On success, returns an `enum rp_status' value that indicates if and/or
1049 * how the reparse point fixup was done.
1051 * On failure, returns a negative value that is a negated WIMLIB_ERR_*
1055 win32_get_reparse_data(HANDLE hFile, const wchar_t *path,
1056 struct add_image_params *params,
1057 u8 *rpbuf, u16 *rpbuflen_ret)
1059 DWORD bytesReturned;
1064 DEBUG("Loading reparse data from \"%ls\"", path);
1065 if (!DeviceIoControl(hFile, FSCTL_GET_REPARSE_POINT,
1066 NULL, /* "Not used with this operation; set to NULL" */
1067 0, /* "Not used with this operation; set to 0" */
1068 rpbuf, /* "A pointer to a buffer that
1069 receives the reparse point data */
1070 REPARSE_POINT_MAX_SIZE, /* "The size of the output
1075 DWORD err = GetLastError();
1076 ERROR("Failed to get reparse data of \"%ls\"", path);
1078 return -WIMLIB_ERR_READ;
1080 if (bytesReturned < 8 || bytesReturned > REPARSE_POINT_MAX_SIZE) {
1081 ERROR("Reparse data on \"%ls\" is invalid", path);
1082 return -WIMLIB_ERR_INVALID_REPARSE_DATA;
1085 rpbuflen = bytesReturned;
1086 reparse_tag = le32_to_cpu(*(u32*)rpbuf);
1087 if (params->add_image_flags & WIMLIB_ADD_IMAGE_FLAG_RPFIX &&
1088 (reparse_tag == WIM_IO_REPARSE_TAG_SYMLINK ||
1089 reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT))
1091 /* Try doing reparse point fixup */
1092 ret = win32_capture_try_rpfix(rpbuf,
1094 params->capture_root_ino,
1095 params->capture_root_dev,
1100 *rpbuflen_ret = rpbuflen;
1105 win32_tally_encrypted_size_cb(unsigned char *_data, void *_ctx,
1109 return ERROR_SUCCESS;
1113 win32_get_encrypted_file_size(const wchar_t *path, u64 *size_ret)
1120 err = OpenEncryptedFileRawW(path, 0, &file_ctx);
1121 if (err != ERROR_SUCCESS) {
1122 ERROR("Failed to open encrypted file \"%ls\" for raw read", path);
1124 return WIMLIB_ERR_OPEN;
1126 err = ReadEncryptedFileRaw(win32_tally_encrypted_size_cb,
1127 size_ret, file_ctx);
1128 if (err != ERROR_SUCCESS) {
1129 ERROR("Failed to read raw encrypted data from \"%ls\"", path);
1131 ret = WIMLIB_ERR_READ;
1135 CloseEncryptedFileRaw(file_ctx);
1139 /* Scans an unnamed or named stream of a Win32 file (not a reparse point
1140 * stream); calculates its SHA1 message digest and either creates a `struct
1141 * wim_lookup_table_entry' in memory for it, or uses an existing 'struct
1142 * wim_lookup_table_entry' for an identical stream.
1144 * @path: Path to the file (UTF-16LE).
1146 * @path_num_chars: Number of 2-byte characters in @path.
1148 * @inode: WIM inode to save the stream into.
1150 * @lookup_table: Stream lookup table for the WIM.
1152 * @dat: A `WIN32_FIND_STREAM_DATA' structure that specifies the
1155 * Returns 0 on success; nonzero on failure.
1158 win32_capture_stream(const wchar_t *path,
1159 size_t path_num_chars,
1160 struct wim_inode *inode,
1161 struct wim_lookup_table *lookup_table,
1162 WIN32_FIND_STREAM_DATA *dat)
1164 struct wim_ads_entry *ads_entry;
1165 struct wim_lookup_table_entry *lte;
1167 wchar_t *stream_name, *colon;
1168 size_t stream_name_nchars;
1169 bool is_named_stream;
1171 size_t spath_nchars;
1172 size_t spath_buf_nbytes;
1173 const wchar_t *relpath_prefix;
1174 const wchar_t *colonchar;
1176 DEBUG("Capture \"%ls\" stream \"%ls\"", path, dat->cStreamName);
1178 /* The stream name should be returned as :NAME:TYPE */
1179 stream_name = dat->cStreamName;
1180 if (*stream_name != L':')
1181 goto out_invalid_stream_name;
1183 colon = wcschr(stream_name, L':');
1185 goto out_invalid_stream_name;
1187 if (wcscmp(colon + 1, L"$DATA")) {
1188 /* Not a DATA stream */
1195 stream_name_nchars = colon - stream_name;
1196 is_named_stream = (stream_name_nchars != 0);
1198 if (is_named_stream) {
1199 /* Allocate an ADS entry for the named stream. */
1200 ads_entry = inode_add_ads_utf16le(inode, stream_name,
1201 stream_name_nchars * sizeof(wchar_t));
1203 ret = WIMLIB_ERR_NOMEM;
1208 /* If zero length stream, no lookup table entry needed. */
1209 if ((u64)dat->StreamSize.QuadPart == 0) {
1214 /* Create a UTF-16LE string @spath that gives the filename, then a
1215 * colon, then the stream name. Or, if it's an unnamed stream, just the
1216 * filename. It is MALLOC()'ed so that it can be saved in the
1217 * wim_lookup_table_entry if needed.
1219 * As yet another special case, relative paths need to be changed to
1220 * begin with an explicit "./" so that, for example, a file t:ads, where
1221 * :ads is the part we added, is not interpreted as a file on the t:
1223 spath_nchars = path_num_chars;
1224 relpath_prefix = L"";
1226 if (is_named_stream) {
1227 spath_nchars += 1 + stream_name_nchars;
1229 if (path_num_chars == 1 &&
1234 relpath_prefix = L"./";
1238 spath_buf_nbytes = (spath_nchars + 1) * sizeof(wchar_t);
1239 spath = MALLOC(spath_buf_nbytes);
1241 swprintf(spath, L"%ls%ls%ls%ls",
1242 relpath_prefix, path, colonchar, stream_name);
1244 /* Make a new wim_lookup_table_entry */
1245 lte = new_lookup_table_entry();
1247 ret = WIMLIB_ERR_NOMEM;
1248 goto out_free_spath;
1250 lte->file_on_disk = spath;
1252 if (inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED && !is_named_stream) {
1254 lte->resource_location = RESOURCE_WIN32_ENCRYPTED;
1255 ret = win32_get_encrypted_file_size(path, &encrypted_size);
1257 goto out_free_spath;
1258 lte->resource_entry.original_size = encrypted_size;
1260 lte->resource_location = RESOURCE_WIN32;
1261 lte->resource_entry.original_size = (u64)dat->StreamSize.QuadPart;
1265 if (is_named_stream) {
1266 stream_id = ads_entry->stream_id;
1267 ads_entry->lte = lte;
1272 lookup_table_insert_unhashed(lookup_table, lte, inode, stream_id);
1278 out_invalid_stream_name:
1279 ERROR("Invalid stream name: \"%ls:%ls\"", path, dat->cStreamName);
1280 ret = WIMLIB_ERR_READ;
1284 /* Scans a Win32 file for unnamed and named data streams (not reparse point
1287 * @path: Path to the file (UTF-16LE).
1289 * @path_num_chars: Number of 2-byte characters in @path.
1291 * @inode: WIM inode to save the stream into.
1293 * @lookup_table: Stream lookup table for the WIM.
1295 * @file_size: Size of unnamed data stream. (Used only if alternate
1296 * data streams API appears to be unavailable.)
1298 * @vol_flags: Flags that specify features of the volume being
1301 * Returns 0 on success; nonzero on failure.
1304 win32_capture_streams(const wchar_t *path,
1305 size_t path_num_chars,
1306 struct wim_inode *inode,
1307 struct wim_lookup_table *lookup_table,
1311 WIN32_FIND_STREAM_DATA dat;
1316 DEBUG("Capturing streams from \"%ls\"", path);
1318 if (win32func_FindFirstStreamW == NULL ||
1319 !(vol_flags & FILE_NAMED_STREAMS))
1322 hFind = win32func_FindFirstStreamW(path, FindStreamInfoStandard, &dat, 0);
1323 if (hFind == INVALID_HANDLE_VALUE) {
1324 err = GetLastError();
1325 if (err == ERROR_CALL_NOT_IMPLEMENTED)
1328 /* Seems legal for this to return ERROR_HANDLE_EOF on reparse
1329 * points and directories */
1330 if ((inode->i_attributes &
1331 (FILE_ATTRIBUTE_REPARSE_POINT | FILE_ATTRIBUTE_DIRECTORY))
1332 && err == ERROR_HANDLE_EOF)
1334 DEBUG("ERROR_HANDLE_EOF (ok)");
1337 if (err == ERROR_ACCESS_DENIED) {
1338 WARNING("Failed to look up data streams "
1339 "of \"%ls\": Access denied!\n%ls",
1340 path, capture_access_denied_msg);
1343 ERROR("Failed to look up data streams "
1344 "of \"%ls\"", path);
1346 return WIMLIB_ERR_READ;
1351 ret = win32_capture_stream(path,
1353 inode, lookup_table,
1356 goto out_find_close;
1357 } while (win32func_FindNextStreamW(hFind, &dat));
1358 err = GetLastError();
1359 if (err != ERROR_HANDLE_EOF) {
1360 ERROR("Win32 API: Error reading data streams from \"%ls\"", path);
1362 ret = WIMLIB_ERR_READ;
1368 /* FindFirstStreamW() API is not available, or the volume does not
1369 * support named streams. Only capture the unnamed data stream. */
1370 DEBUG("Only capturing unnamed data stream");
1371 if (inode->i_attributes &
1372 (FILE_ATTRIBUTE_REPARSE_POINT | FILE_ATTRIBUTE_DIRECTORY))
1376 /* Just create our own WIN32_FIND_STREAM_DATA for an unnamed
1377 * stream to reduce the code to a call to the
1378 * already-implemented win32_capture_stream() */
1379 wcscpy(dat.cStreamName, L"::$DATA");
1380 dat.StreamSize.QuadPart = file_size;
1381 ret = win32_capture_stream(path,
1383 inode, lookup_table,
1390 win32_build_dentry_tree_recursive(struct wim_dentry **root_ret,
1392 size_t path_num_chars,
1393 struct add_image_params *params,
1394 struct win32_capture_state *state,
1397 struct wim_dentry *root = NULL;
1398 struct wim_inode *inode;
1406 if (exclude_path(path, path_num_chars, params->config, true)) {
1407 if (params->add_image_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT) {
1408 ERROR("Cannot exclude the root directory from capture");
1409 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
1412 if ((params->add_image_flags & WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE)
1413 && params->progress_func)
1415 union wimlib_progress_info info;
1416 info.scan.cur_path = path;
1417 info.scan.excluded = true;
1418 params->progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
1424 if ((params->add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
1425 && params->progress_func)
1427 union wimlib_progress_info info;
1428 info.scan.cur_path = path;
1429 info.scan.excluded = false;
1430 params->progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
1433 HANDLE hFile = win32_open_existing_file(path,
1434 FILE_READ_DATA | FILE_READ_ATTRIBUTES);
1435 if (hFile == INVALID_HANDLE_VALUE) {
1436 err = GetLastError();
1437 ERROR("Win32 API: Failed to open \"%ls\"", path);
1439 ret = WIMLIB_ERR_OPEN;
1443 BY_HANDLE_FILE_INFORMATION file_info;
1444 if (!GetFileInformationByHandle(hFile, &file_info)) {
1445 err = GetLastError();
1446 ERROR("Win32 API: Failed to get file information for \"%ls\"",
1449 ret = WIMLIB_ERR_STAT;
1450 goto out_close_handle;
1453 if (file_info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1454 rpbuf = alloca(REPARSE_POINT_MAX_SIZE);
1455 ret = win32_get_reparse_data(hFile, path, params,
1458 /* WIMLIB_ERR_* (inverted) */
1460 goto out_close_handle;
1461 } else if (ret & RP_FIXED) {
1463 } else if (ret == RP_EXCLUDED) {
1465 goto out_close_handle;
1471 /* Create a WIM dentry with an associated inode, which may be shared.
1473 * However, we need to explicitly check for directories and files with
1474 * only 1 link and refuse to hard link them. This is because Windows
1475 * has a bug where it can return duplicate File IDs for files and
1476 * directories on the FAT filesystem. */
1477 ret = inode_table_new_dentry(params->inode_table,
1478 path_basename_with_len(path, path_num_chars),
1479 ((u64)file_info.nFileIndexHigh << 32) |
1480 (u64)file_info.nFileIndexLow,
1481 file_info.dwVolumeSerialNumber,
1482 (file_info.nNumberOfLinks <= 1 ||
1483 (file_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)),
1486 goto out_close_handle;
1488 ret = win32_get_short_name(root, path);
1490 goto out_close_handle;
1492 inode = root->d_inode;
1494 if (inode->i_nlink > 1) /* Shared inode; nothing more to do */
1495 goto out_close_handle;
1497 inode->i_attributes = file_info.dwFileAttributes;
1498 inode->i_creation_time = FILETIME_to_u64(&file_info.ftCreationTime);
1499 inode->i_last_write_time = FILETIME_to_u64(&file_info.ftLastWriteTime);
1500 inode->i_last_access_time = FILETIME_to_u64(&file_info.ftLastAccessTime);
1501 inode->i_resolved = 1;
1503 params->add_image_flags &= ~(WIMLIB_ADD_IMAGE_FLAG_ROOT | WIMLIB_ADD_IMAGE_FLAG_SOURCE);
1505 if (!(params->add_image_flags & WIMLIB_ADD_IMAGE_FLAG_NO_ACLS)
1506 && (vol_flags & FILE_PERSISTENT_ACLS))
1508 ret = win32_get_security_descriptor(root, params->sd_set,
1510 params->add_image_flags);
1512 goto out_close_handle;
1515 file_size = ((u64)file_info.nFileSizeHigh << 32) |
1516 (u64)file_info.nFileSizeLow;
1520 /* Capture the unnamed data stream (only should be present for regular
1521 * files) and any alternate data streams. */
1522 ret = win32_capture_streams(path,
1525 params->lookup_table,
1531 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1532 /* Reparse point: set the reparse data (which we read already)
1534 inode->i_not_rpfixed = not_rpfixed;
1535 inode->i_reparse_tag = le32_to_cpu(*(u32*)rpbuf);
1536 ret = inode_set_unnamed_stream(inode, rpbuf + 8, rpbuflen - 8,
1537 params->lookup_table);
1538 } else if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY) {
1539 /* Directory (not a reparse point) --- recurse to children */
1540 ret = win32_recurse_directory(root,
1554 free_dentry_tree(root, params->lookup_table);
1559 win32_do_capture_warnings(const struct win32_capture_state *state,
1560 int add_image_flags)
1562 if (state->num_get_sacl_priv_notheld == 0 &&
1563 state->num_get_sd_access_denied == 0)
1567 WARNING("Built dentry tree successfully, but with the following problem(s):");
1568 if (state->num_get_sacl_priv_notheld != 0) {
1569 WARNING("Could not capture SACL (System Access Control List)\n"
1570 " on %lu files or directories.",
1571 state->num_get_sacl_priv_notheld);
1573 if (state->num_get_sd_access_denied != 0) {
1574 WARNING("Could not capture security descriptor at all\n"
1575 " on %lu files or directories.",
1576 state->num_get_sd_access_denied);
1579 "Try running the program as the Administrator to make sure all the\n"
1580 " desired metadata has been captured exactly. However, if you\n"
1581 " do not care about capturing security descriptors correctly, then\n"
1582 " nothing more needs to be done%ls\n",
1583 (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_NO_ACLS) ? L"." :
1584 L", although you might consider\n"
1585 " passing the --no-acls flag to `wimlib-imagex capture' or\n"
1586 " `wimlib-imagex append' to explicitly capture no security\n"
1590 /* Win32 version of capturing a directory tree */
1592 win32_build_dentry_tree(struct wim_dentry **root_ret,
1593 const wchar_t *root_disk_path,
1594 struct add_image_params *params)
1599 struct win32_capture_state state;
1603 path_nchars = wcslen(root_disk_path);
1604 if (path_nchars > 32767)
1605 return WIMLIB_ERR_INVALID_PARAM;
1607 if (GetFileAttributesW(root_disk_path) == INVALID_FILE_ATTRIBUTES &&
1608 GetLastError() == ERROR_FILE_NOT_FOUND)
1610 ERROR("Capture directory \"%ls\" does not exist!",
1612 return WIMLIB_ERR_OPENDIR;
1615 ret = win32_get_file_and_vol_ids(root_disk_path,
1616 ¶ms->capture_root_ino,
1617 ¶ms->capture_root_dev);
1621 win32_get_vol_flags(root_disk_path, &vol_flags);
1623 /* There is no check for overflow later when this buffer is being used!
1624 * But the max path length on NTFS is 32767 characters, and paths need
1625 * to be written specially to even go past 260 characters, so we should
1626 * be okay with 32770 characters. */
1627 path = MALLOC(32770 * sizeof(wchar_t));
1629 return WIMLIB_ERR_NOMEM;
1631 wmemcpy(path, root_disk_path, path_nchars + 1);
1633 memset(&state, 0, sizeof(state));
1634 ret = win32_build_dentry_tree_recursive(root_ret, path,
1635 path_nchars, params,
1639 win32_do_capture_warnings(&state, params->add_image_flags);
1644 win32_extract_try_rpfix(u8 *rpbuf,
1645 const wchar_t *extract_root_realpath,
1646 unsigned extract_root_realpath_nchars)
1648 struct reparse_data rpdata;
1650 size_t target_nchars;
1651 size_t stripped_nchars;
1652 wchar_t *stripped_target;
1653 wchar_t stripped_target_nchars;
1656 utf16lechar *new_target;
1657 utf16lechar *new_print_name;
1658 size_t new_target_nchars;
1659 size_t new_print_name_nchars;
1662 ret = parse_reparse_data(rpbuf, 8 + le16_to_cpu(*(u16*)(rpbuf + 4)),
1667 if (extract_root_realpath[0] == L'\0' ||
1668 extract_root_realpath[1] != L':' ||
1669 extract_root_realpath[2] != L'\\')
1671 ERROR("Can't understand full path format \"%ls\". "
1672 "Try turning reparse point fixups off...",
1673 extract_root_realpath);
1674 return WIMLIB_ERR_REPARSE_POINT_FIXUP_FAILED;
1677 ret = parse_substitute_name(rpdata.substitute_name,
1678 rpdata.substitute_name_nbytes,
1682 stripped_nchars = ret;
1683 target = rpdata.substitute_name;
1684 target_nchars = rpdata.substitute_name_nbytes / sizeof(utf16lechar);
1685 stripped_target = target + 6;
1686 stripped_target_nchars = target_nchars - stripped_nchars;
1688 new_target = alloca((6 + extract_root_realpath_nchars +
1689 stripped_target_nchars) * sizeof(utf16lechar));
1692 if (stripped_nchars == 6) {
1693 /* Include \??\ prefix if it was present before */
1694 wmemcpy(p, L"\\??\\", 4);
1698 /* Print name excludes the \??\ if present. */
1700 if (stripped_nchars != 0) {
1701 /* Get drive letter from real path to extract root, if a drive
1702 * letter was present before. */
1703 *p++ = extract_root_realpath[0];
1704 *p++ = extract_root_realpath[1];
1706 /* Copy the rest of the extract root */
1707 wmemcpy(p, extract_root_realpath + 2, extract_root_realpath_nchars - 2);
1708 p += extract_root_realpath_nchars - 2;
1710 /* Append the stripped target */
1711 wmemcpy(p, stripped_target, stripped_target_nchars);
1712 p += stripped_target_nchars;
1713 new_target_nchars = p - new_target;
1714 new_print_name_nchars = p - new_print_name;
1716 if (new_target_nchars * sizeof(utf16lechar) >= REPARSE_POINT_MAX_SIZE ||
1717 new_print_name_nchars * sizeof(utf16lechar) >= REPARSE_POINT_MAX_SIZE)
1719 ERROR("Path names too long to do reparse point fixup!");
1720 return WIMLIB_ERR_REPARSE_POINT_FIXUP_FAILED;
1722 rpdata.substitute_name = new_target;
1723 rpdata.substitute_name_nbytes = new_target_nchars * sizeof(utf16lechar);
1724 rpdata.print_name = new_print_name;
1725 rpdata.print_name_nbytes = new_print_name_nchars * sizeof(utf16lechar);
1726 return make_reparse_buffer(&rpdata, rpbuf);
1729 /* Wrapper around the FSCTL_SET_REPARSE_POINT ioctl to set the reparse data on
1730 * an extracted reparse point. */
1732 win32_set_reparse_data(HANDLE h,
1733 const struct wim_inode *inode,
1734 const struct wim_lookup_table_entry *lte,
1735 const wchar_t *path,
1736 const struct apply_args *args)
1739 u8 rpbuf[REPARSE_POINT_MAX_SIZE];
1740 DWORD bytesReturned;
1742 DEBUG("Setting reparse data on \"%ls\"", path);
1744 ret = wim_inode_get_reparse_data(inode, rpbuf);
1748 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX &&
1749 (inode->i_reparse_tag == WIM_IO_REPARSE_TAG_SYMLINK ||
1750 inode->i_reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT) &&
1751 !inode->i_not_rpfixed)
1753 ret = win32_extract_try_rpfix(rpbuf,
1754 args->target_realpath,
1755 args->target_realpath_len);
1757 return WIMLIB_ERR_REPARSE_POINT_FIXUP_FAILED;
1760 /* Set the reparse data on the open file using the
1761 * FSCTL_SET_REPARSE_POINT ioctl.
1763 * There are contradictions in Microsoft's documentation for this:
1765 * "If hDevice was opened without specifying FILE_FLAG_OVERLAPPED,
1766 * lpOverlapped is ignored."
1768 * --- So setting lpOverlapped to NULL is okay since it's ignored.
1770 * "If lpOverlapped is NULL, lpBytesReturned cannot be NULL. Even when an
1771 * operation returns no output data and lpOutBuffer is NULL,
1772 * DeviceIoControl makes use of lpBytesReturned. After such an
1773 * operation, the value of lpBytesReturned is meaningless."
1775 * --- So lpOverlapped not really ignored, as it affects another
1776 * parameter. This is the actual behavior: lpBytesReturned must be
1777 * specified, even though lpBytesReturned is documented as:
1779 * "Not used with this operation; set to NULL."
1781 if (!DeviceIoControl(h, FSCTL_SET_REPARSE_POINT, rpbuf,
1782 8 + le16_to_cpu(*(u16*)(rpbuf + 4)),
1784 &bytesReturned /* lpBytesReturned */,
1785 NULL /* lpOverlapped */))
1787 DWORD err = GetLastError();
1788 ERROR("Failed to set reparse data on \"%ls\"", path);
1790 if (err == ERROR_ACCESS_DENIED || err == ERROR_PRIVILEGE_NOT_HELD)
1791 return WIMLIB_ERR_INSUFFICIENT_PRIVILEGES_TO_EXTRACT;
1792 else if (inode->i_reparse_tag == WIM_IO_REPARSE_TAG_SYMLINK ||
1793 inode->i_reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT)
1794 return WIMLIB_ERR_LINK;
1796 return WIMLIB_ERR_WRITE;
1801 /* Wrapper around the FSCTL_SET_COMPRESSION ioctl to change the
1802 * FILE_ATTRIBUTE_COMPRESSED flag of a file or directory. */
1804 win32_set_compression_state(HANDLE hFile, USHORT format, const wchar_t *path)
1806 DWORD bytesReturned;
1807 if (!DeviceIoControl(hFile, FSCTL_SET_COMPRESSION,
1808 &format, sizeof(USHORT),
1810 &bytesReturned, NULL))
1812 /* Could be a warning only, but we only call this if the volume
1813 * supports compression. So I'm calling this an error. */
1814 DWORD err = GetLastError();
1815 ERROR("Failed to set compression flag on \"%ls\"", path);
1817 if (err == ERROR_ACCESS_DENIED || err == ERROR_PRIVILEGE_NOT_HELD)
1818 return WIMLIB_ERR_INSUFFICIENT_PRIVILEGES_TO_EXTRACT;
1820 return WIMLIB_ERR_WRITE;
1825 /* Wrapper around FSCTL_SET_SPARSE ioctl to set a file as sparse. */
1827 win32_set_sparse(HANDLE hFile, const wchar_t *path)
1829 DWORD bytesReturned;
1830 if (!DeviceIoControl(hFile, FSCTL_SET_SPARSE,
1833 &bytesReturned, NULL))
1835 /* Could be a warning only, but we only call this if the volume
1836 * supports sparse files. So I'm calling this an error. */
1837 DWORD err = GetLastError();
1838 WARNING("Failed to set sparse flag on \"%ls\"", path);
1840 if (err == ERROR_ACCESS_DENIED || err == ERROR_PRIVILEGE_NOT_HELD)
1841 return WIMLIB_ERR_INSUFFICIENT_PRIVILEGES_TO_EXTRACT;
1843 return WIMLIB_ERR_WRITE;
1849 * Sets the security descriptor on an extracted file.
1852 win32_set_security_data(const struct wim_inode *inode,
1854 const wchar_t *path,
1855 struct apply_args *args)
1857 PSECURITY_DESCRIPTOR descriptor;
1860 const struct wim_security_data *sd;
1862 SECURITY_INFORMATION securityInformation = 0;
1869 BOOL owner_defaulted;
1870 BOOL group_defaulted;
1872 BOOL dacl_defaulted;
1874 BOOL sacl_defaulted;
1876 sd = wim_const_security_data(args->w);
1877 descriptor = sd->descriptors[inode->i_security_id];
1879 GetSecurityDescriptorOwner(descriptor, &owner, &owner_defaulted);
1881 securityInformation |= OWNER_SECURITY_INFORMATION;
1883 GetSecurityDescriptorGroup(descriptor, &group, &group_defaulted);
1885 securityInformation |= GROUP_SECURITY_INFORMATION;
1887 GetSecurityDescriptorDacl(descriptor, &dacl_present,
1888 &dacl, &dacl_defaulted);
1890 securityInformation |= DACL_SECURITY_INFORMATION;
1892 GetSecurityDescriptorSacl(descriptor, &sacl_present,
1893 &sacl, &sacl_defaulted);
1895 securityInformation |= SACL_SECURITY_INFORMATION;
1898 if (securityInformation == 0)
1900 if (SetSecurityInfo(hFile, SE_FILE_OBJECT,
1901 securityInformation, owner, group, dacl, sacl))
1903 err = GetLastError();
1904 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_ACLS)
1907 case ERROR_PRIVILEGE_NOT_HELD:
1908 if (securityInformation & SACL_SECURITY_INFORMATION) {
1909 n = args->num_set_sacl_priv_notheld++;
1910 securityInformation &= ~SACL_SECURITY_INFORMATION;
1912 if (n < MAX_SET_SACL_PRIV_NOTHELD_WARNINGS) {
1914 "We don't have enough privileges to set the full security\n"
1915 " descriptor on \"%ls\"!\n", path);
1916 if (args->num_set_sd_access_denied +
1917 args->num_set_sacl_priv_notheld == 1)
1919 WARNING("%ls", apply_access_denied_msg);
1921 WARNING("Re-trying with SACL omitted.\n", path);
1922 } else if (n == MAX_GET_SACL_PRIV_NOTHELD_WARNINGS) {
1924 "Suppressing further 'privileges not held' error messages when setting\n"
1925 " security descriptors.");
1930 case ERROR_INVALID_OWNER:
1931 case ERROR_ACCESS_DENIED:
1932 n = args->num_set_sd_access_denied++;
1933 if (n < MAX_SET_SD_ACCESS_DENIED_WARNINGS) {
1934 WARNING("Failed to set security descriptor on \"%ls\": "
1935 "Access denied!\n", path);
1936 if (args->num_set_sd_access_denied +
1937 args->num_set_sacl_priv_notheld == 1)
1939 WARNING("%ls", apply_access_denied_msg);
1941 } else if (n == MAX_SET_SD_ACCESS_DENIED_WARNINGS) {
1943 "Suppressing further access denied error messages when setting\n"
1944 " security descriptors");
1949 ERROR("Failed to set security descriptor on \"%ls\"", path);
1951 if (err == ERROR_ACCESS_DENIED || err == ERROR_PRIVILEGE_NOT_HELD)
1952 return WIMLIB_ERR_INSUFFICIENT_PRIVILEGES_TO_EXTRACT;
1954 return WIMLIB_ERR_WRITE;
1960 win32_extract_chunk(const void *buf, size_t len, void *arg)
1962 HANDLE hStream = arg;
1964 DWORD nbytes_written;
1965 wimlib_assert(len <= 0xffffffff);
1967 if (!WriteFile(hStream, buf, len, &nbytes_written, NULL) ||
1968 nbytes_written != len)
1970 DWORD err = GetLastError();
1971 ERROR("WriteFile(): write error");
1973 return WIMLIB_ERR_WRITE;
1979 do_win32_extract_stream(HANDLE hStream, const struct wim_lookup_table_entry *lte)
1981 return extract_wim_resource(lte, wim_resource_size(lte),
1982 win32_extract_chunk, hStream);
1985 struct win32_encrypted_extract_ctx {
1986 const struct wim_lookup_table_entry *lte;
1991 win32_encrypted_import_cb(unsigned char *data, void *_ctx,
1992 unsigned long *len_p)
1994 struct win32_encrypted_extract_ctx *ctx = _ctx;
1995 unsigned long len = *len_p;
1996 const struct wim_lookup_table_entry *lte = ctx->lte;
1998 len = min(len, wim_resource_size(lte) - ctx->offset);
2000 if (read_partial_wim_resource_into_buf(lte, len, ctx->offset, data))
2001 return ERROR_READ_FAULT;
2005 return ERROR_SUCCESS;
2008 /* Create an encrypted file and extract the raw encrypted data to it.
2010 * @path: Path to encrypted file to create.
2011 * @lte: WIM lookup_table entry for the raw encrypted data.
2013 * This is separate from do_win32_extract_stream() because the WIM is supposed
2014 * to contain the *raw* encrypted data, which needs to be extracted ("imported")
2015 * using the special APIs OpenEncryptedFileRawW(), WriteEncryptedFileRaw(), and
2016 * CloseEncryptedFileRaw().
2018 * Returns 0 on success; nonzero on failure.
2021 do_win32_extract_encrypted_stream(const wchar_t *path,
2022 const struct wim_lookup_table_entry *lte)
2027 DEBUG("Opening file \"%ls\" to extract raw encrypted data", path);
2029 ret = OpenEncryptedFileRawW(path, CREATE_FOR_IMPORT, &file_ctx);
2031 ERROR("Failed to open \"%ls\" to write raw encrypted data", path);
2033 return WIMLIB_ERR_OPEN;
2037 struct win32_encrypted_extract_ctx ctx;
2041 ret = WriteEncryptedFileRaw(win32_encrypted_import_cb, &ctx, file_ctx);
2042 if (ret == ERROR_SUCCESS) {
2045 ret = WIMLIB_ERR_WRITE;
2046 ERROR("Failed to extract encrypted file \"%ls\"", path);
2049 CloseEncryptedFileRaw(file_ctx);
2054 path_is_root_of_drive(const wchar_t *path)
2059 if (*path != L'/' && *path != L'\\') {
2060 if (*(path + 1) == L':')
2065 while (*path == L'/' || *path == L'\\')
2067 return (*path == L'\0');
2071 win32_mask_attributes(DWORD i_attributes)
2073 return i_attributes & ~(FILE_ATTRIBUTE_SPARSE_FILE |
2074 FILE_ATTRIBUTE_COMPRESSED |
2075 FILE_ATTRIBUTE_REPARSE_POINT |
2076 FILE_ATTRIBUTE_DIRECTORY |
2077 FILE_ATTRIBUTE_ENCRYPTED |
2078 FILE_FLAG_DELETE_ON_CLOSE |
2079 FILE_FLAG_NO_BUFFERING |
2080 FILE_FLAG_OPEN_NO_RECALL |
2081 FILE_FLAG_OVERLAPPED |
2082 FILE_FLAG_RANDOM_ACCESS |
2083 /*FILE_FLAG_SESSION_AWARE |*/
2084 FILE_FLAG_SEQUENTIAL_SCAN |
2085 FILE_FLAG_WRITE_THROUGH);
2089 win32_get_create_flags_and_attributes(DWORD i_attributes)
2092 * Some attributes cannot be set by passing them to CreateFile(). In
2095 * FILE_ATTRIBUTE_DIRECTORY:
2096 * CreateDirectory() must be called instead of CreateFile().
2098 * FILE_ATTRIBUTE_SPARSE_FILE:
2100 * See: win32_set_sparse().
2102 * FILE_ATTRIBUTE_COMPRESSED:
2103 * Not clear from the documentation, but apparently this needs an
2105 * See: win32_set_compressed().
2107 * FILE_ATTRIBUTE_REPARSE_POINT:
2108 * Needs an ioctl, with the reparse data specified.
2109 * See: win32_set_reparse_data().
2111 * In addition, clear any file flags in the attributes that we don't
2112 * want, but also specify FILE_FLAG_OPEN_REPARSE_POINT and
2113 * FILE_FLAG_BACKUP_SEMANTICS as we are a backup application.
2115 return win32_mask_attributes(i_attributes) |
2116 FILE_FLAG_OPEN_REPARSE_POINT |
2117 FILE_FLAG_BACKUP_SEMANTICS;
2120 /* Set compression and/or sparse attributes on a stream, if supported by the
2123 win32_set_special_stream_attributes(HANDLE hFile, const struct wim_inode *inode,
2124 struct wim_lookup_table_entry *unnamed_stream_lte,
2125 const wchar_t *path, unsigned vol_flags)
2129 if (inode->i_attributes & FILE_ATTRIBUTE_COMPRESSED) {
2130 if (vol_flags & FILE_FILE_COMPRESSION) {
2131 ret = win32_set_compression_state(hFile,
2132 COMPRESSION_FORMAT_DEFAULT,
2137 DEBUG("Cannot set compression attribute on \"%ls\": "
2138 "volume does not support transparent compression",
2143 if (inode->i_attributes & FILE_ATTRIBUTE_SPARSE_FILE) {
2144 if (vol_flags & FILE_SUPPORTS_SPARSE_FILES) {
2145 DEBUG("Setting sparse flag on \"%ls\"", path);
2146 ret = win32_set_sparse(hFile, path);
2150 DEBUG("Cannot set sparse attribute on \"%ls\": "
2151 "volume does not support sparse files",
2158 /* Pre-create directories; extract encrypted streams */
2160 win32_begin_extract_unnamed_stream(const struct wim_inode *inode,
2161 const struct wim_lookup_table_entry *lte,
2162 const wchar_t *path,
2163 DWORD *creationDisposition_ret,
2164 unsigned int vol_flags)
2169 /* Directories must be created with CreateDirectoryW(). Then the call
2170 * to CreateFileW() will merely open the directory that was already
2171 * created rather than creating a new file. */
2172 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY &&
2173 !path_is_root_of_drive(path)) {
2174 if (!CreateDirectoryW(path, NULL)) {
2175 err = GetLastError();
2176 if (err != ERROR_ALREADY_EXISTS) {
2177 ERROR("Failed to create directory \"%ls\"",
2180 return WIMLIB_ERR_MKDIR;
2183 DEBUG("Created directory \"%ls\"", path);
2184 *creationDisposition_ret = OPEN_EXISTING;
2186 if (inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED &&
2187 vol_flags & FILE_SUPPORTS_ENCRYPTION)
2189 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY) {
2190 unsigned remaining_sharing_violations = 100;
2191 while (!EncryptFile(path)) {
2192 if (remaining_sharing_violations &&
2193 err == ERROR_SHARING_VIOLATION)
2195 WARNING("Couldn't encrypt directory \"%ls\" "
2196 "due to sharing violation; re-trying "
2197 "after 100 ms", path);
2199 remaining_sharing_violations--;
2201 err = GetLastError();
2202 ERROR("Failed to encrypt directory \"%ls\"",
2205 return WIMLIB_ERR_WRITE;
2209 ret = do_win32_extract_encrypted_stream(path, lte);
2212 DEBUG("Extracted encrypted file \"%ls\"", path);
2214 *creationDisposition_ret = OPEN_EXISTING;
2217 /* Set file attributes if we created the file. Otherwise, we haven't
2218 * created the file set and we will set the attributes in the call to
2221 * The FAT filesystem does not let you change the attributes of the root
2222 * directory, so treat that as a special case and do not set attributes.
2224 if (*creationDisposition_ret == OPEN_EXISTING &&
2225 !path_is_root_of_drive(path))
2227 if (!SetFileAttributesW(path,
2228 win32_mask_attributes(inode->i_attributes)))
2230 err = GetLastError();
2231 ERROR("Failed to set attributes on \"%ls\"", path);
2233 return WIMLIB_ERR_WRITE;
2239 /* Set security descriptor and extract stream data or reparse data (skip the
2240 * unnamed data stream of encrypted files, which was already extracted). */
2242 win32_finish_extract_stream(HANDLE h, const struct wim_dentry *dentry,
2243 const struct wim_lookup_table_entry *lte,
2244 const wchar_t *stream_path,
2245 const wchar_t *stream_name_utf16,
2246 struct apply_args *args)
2249 const struct wim_inode *inode = dentry->d_inode;
2250 const wchar_t *short_name;
2251 if (stream_name_utf16 == NULL) {
2252 /* Unnamed stream. */
2254 /* Set security descriptor, unless the extract_flags indicate
2255 * not to or the volume does not supported it. Note that this
2256 * is only done when the unnamed stream is being extracted, as
2257 * security descriptors are per-file and not per-stream. */
2258 if (inode->i_security_id >= 0 &&
2259 !(args->extract_flags & WIMLIB_EXTRACT_FLAG_NO_ACLS)
2260 && (args->vol_flags & FILE_PERSISTENT_ACLS))
2262 ret = win32_set_security_data(inode, h, stream_path, args);
2267 /* Handle reparse points. The data for them needs to be set
2268 * using a special ioctl. Note that the reparse point may have
2269 * been created using CreateFileW() in the case of
2270 * non-directories or CreateDirectoryW() in the case of
2271 * directories; but the ioctl works either way. Also, it is
2272 * only this step that actually sets the
2273 * FILE_ATTRIBUTE_REPARSE_POINT, as it is not valid to set it
2274 * using SetFileAttributesW() or CreateFileW().
2276 * If the volume does not support reparse points we simply
2277 * ignore the reparse data. (N.B. the code currently doesn't
2278 * actually reach this case because reparse points are skipped
2279 * entirely on such volumes.) */
2280 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
2281 if (args->vol_flags & FILE_SUPPORTS_REPARSE_POINTS) {
2282 ret = win32_set_reparse_data(h, inode,
2288 DEBUG("Cannot set reparse data on \"%ls\": volume "
2289 "does not support reparse points", stream_path);
2291 } else if (lte != NULL &&
2292 !(args->vol_flags & FILE_SUPPORTS_ENCRYPTION &&
2293 inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED))
2295 /* Extract the data of the unnamed stream, unless the
2296 * lookup table entry is NULL (indicating an empty
2297 * stream for which no data needs to be extracted), or
2298 * the stream is encrypted and therefore was already
2299 * extracted as a special case. */
2300 ret = do_win32_extract_stream(h, lte);
2305 if (dentry_has_short_name(dentry))
2306 short_name = dentry->short_name;
2309 /* Set short name */
2310 if (!SetFileShortNameW(h, short_name)) {
2312 DWORD err = GetLastError();
2313 ERROR("Could not set short name on \"%ls\"", stream_path);
2318 /* Extract the data for a named data stream. */
2320 DEBUG("Extracting named data stream \"%ls\" (len = %"PRIu64")",
2321 stream_path, wim_resource_size(lte));
2322 ret = do_win32_extract_stream(h, lte);
2329 win32_decrypt_file(HANDLE open_handle, const wchar_t *path)
2332 /* We cannot call DecryptFileW() while there is an open handle to the
2333 * file. So close it first. */
2334 if (!CloseHandle(open_handle)) {
2335 err = GetLastError();
2336 ERROR("Failed to close handle for \"%ls\"", path);
2338 return WIMLIB_ERR_WRITE;
2340 if (!DecryptFileW(path, 0 /* reserved parameter; set to 0 */)) {
2341 err = GetLastError();
2342 ERROR("Failed to decrypt file \"%ls\"", path);
2344 return WIMLIB_ERR_WRITE;
2350 * Create and extract a stream to a file, or create a directory, using the
2353 * This handles reparse points, directories, alternate data streams, encrypted
2354 * files, compressed files, etc.
2356 * @dentry: WIM dentry for the file or directory being extracted.
2358 * @path: Path to extract the file to.
2360 * @stream_name_utf16:
2361 * Name of the stream, or NULL if the stream is unnamed. This will
2362 * be called with a NULL stream_name_utf16 before any non-NULL
2363 * stream_name_utf16's.
2365 * @lte: WIM lookup table entry for the stream. May be NULL to indicate
2366 * a stream of length 0.
2368 * @args: Additional apply context, including flags indicating supported
2371 * Returns 0 on success; nonzero on failure.
2374 win32_extract_stream(const struct wim_dentry *dentry,
2375 const wchar_t *path,
2376 const wchar_t *stream_name_utf16,
2377 struct wim_lookup_table_entry *lte,
2378 struct apply_args *args)
2380 wchar_t *stream_path;
2384 DWORD creationDisposition = CREATE_ALWAYS;
2385 DWORD requestedAccess;
2386 BY_HANDLE_FILE_INFORMATION file_info;
2387 unsigned remaining_sharing_violations = 1000;
2388 const struct wim_inode *inode = dentry->d_inode;
2390 if (stream_name_utf16) {
2391 /* Named stream. Create a buffer that contains the UTF-16LE
2392 * string [./]path:stream_name_utf16. This is needed to
2393 * create and open the stream using CreateFileW(). I'm not
2394 * aware of any other APIs to do this. Note: the '$DATA' suffix
2395 * seems to be unneeded. Additional note: a "./" prefix needs
2396 * to be added when the path is not absolute to avoid ambiguity
2397 * with drive letters. */
2398 size_t stream_path_nchars;
2400 size_t stream_name_nchars;
2401 const wchar_t *prefix;
2403 path_nchars = wcslen(path);
2404 stream_name_nchars = wcslen(stream_name_utf16);
2405 stream_path_nchars = path_nchars + 1 + stream_name_nchars;
2406 if (path[0] != cpu_to_le16(L'\0') &&
2407 path[0] != cpu_to_le16(L'/') &&
2408 path[0] != cpu_to_le16(L'\\') &&
2409 path[1] != cpu_to_le16(L':'))
2412 stream_path_nchars += 2;
2416 stream_path = alloca((stream_path_nchars + 1) * sizeof(wchar_t));
2417 swprintf(stream_path, L"%ls%ls:%ls",
2418 prefix, path, stream_name_utf16);
2420 /* Unnamed stream; its path is just the path to the file itself.
2422 stream_path = (wchar_t*)path;
2424 ret = win32_begin_extract_unnamed_stream(inode, lte, path,
2425 &creationDisposition,
2431 DEBUG("Opening \"%ls\"", stream_path);
2432 /* DELETE access is needed for SetFileShortNameW(), for some reason. */
2433 requestedAccess = GENERIC_READ | GENERIC_WRITE | DELETE |
2434 ACCESS_SYSTEM_SECURITY;
2436 /* Open the stream to be extracted. Depending on what we have set
2437 * creationDisposition to, we may be creating this for the first time,
2438 * or we may be opening on existing stream we already created using
2439 * CreateDirectoryW() or OpenEncryptedFileRawW(). */
2440 h = CreateFileW(stream_path,
2444 creationDisposition,
2445 win32_get_create_flags_and_attributes(inode->i_attributes),
2447 if (h == INVALID_HANDLE_VALUE) {
2448 err = GetLastError();
2449 if (err == ERROR_ACCESS_DENIED &&
2450 path_is_root_of_drive(stream_path))
2455 if ((err == ERROR_PRIVILEGE_NOT_HELD ||
2456 err == ERROR_ACCESS_DENIED) &&
2457 (requestedAccess & ACCESS_SYSTEM_SECURITY))
2459 /* Try opening the file again without privilege to
2461 requestedAccess &= ~ACCESS_SYSTEM_SECURITY;
2462 goto try_open_again;
2464 if (err == ERROR_SHARING_VIOLATION) {
2465 if (remaining_sharing_violations) {
2466 --remaining_sharing_violations;
2467 /* This can happen when restoring encrypted directories
2468 * for some reason. Probably a bug in EncryptFile(). */
2469 WARNING("Couldn't open \"%ls\" due to sharing violation; "
2470 "re-trying after 100ms", stream_path);
2472 goto try_open_again;
2474 ERROR("Too many sharing violations; giving up...");
2477 if (creationDisposition == OPEN_EXISTING)
2478 ERROR("Failed to open \"%ls\"", stream_path);
2480 ERROR("Failed to create \"%ls\"", stream_path);
2483 ret = WIMLIB_ERR_OPEN;
2487 /* Check the attributes of the file we just opened, and remove
2488 * encryption or compression if either was set by default but is not
2489 * supposed to be set based on the WIM inode attributes. */
2490 if (!GetFileInformationByHandle(h, &file_info)) {
2491 err = GetLastError();
2492 ERROR("Failed to get attributes of \"%ls\"", stream_path);
2494 ret = WIMLIB_ERR_STAT;
2495 goto fail_close_handle;
2498 /* Remove encryption? */
2499 if (file_info.dwFileAttributes & FILE_ATTRIBUTE_ENCRYPTED &&
2500 !(inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED))
2502 /* File defaulted to encrypted due to being in an encrypted
2503 * directory, but is not actually supposed to be encrypted.
2505 * This is a workaround, because I'm not aware of any way to
2506 * directly (e.g. with CreateFileW()) create an unencrypted file
2507 * in a directory with FILE_ATTRIBUTE_ENCRYPTED set. */
2508 ret = win32_decrypt_file(h, stream_path);
2510 goto fail; /* win32_decrypt_file() closed the handle. */
2511 creationDisposition = OPEN_EXISTING;
2512 goto try_open_again;
2515 /* Remove compression? */
2516 if (file_info.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED &&
2517 !(inode->i_attributes & FILE_ATTRIBUTE_COMPRESSED))
2519 /* Similar to the encrypted case, above, if the file defaulted
2520 * to compressed due to being in an compressed directory, but is
2521 * not actually supposed to be compressed, explicitly set the
2522 * compression format to COMPRESSION_FORMAT_NONE. */
2523 ret = win32_set_compression_state(h, COMPRESSION_FORMAT_NONE,
2526 goto fail_close_handle;
2529 /* Set compression and/or sparse attributes if needed */
2530 ret = win32_set_special_stream_attributes(h, inode, lte, path,
2534 goto fail_close_handle;
2536 /* At this point we have at least created the needed stream with the
2537 * appropriate attributes. We have yet to set the appropriate security
2538 * descriptor and actually extract the stream data (other than for
2539 * extracted files, which were already extracted).
2540 * win32_finish_extract_stream() handles these additional steps. */
2541 ret = win32_finish_extract_stream(h, dentry, lte, stream_path,
2542 stream_name_utf16, args);
2544 goto fail_close_handle;
2546 /* Done extracting the stream. Close the handle and return. */
2547 DEBUG("Closing \"%ls\"", stream_path);
2548 if (!CloseHandle(h)) {
2549 err = GetLastError();
2550 ERROR("Failed to close \"%ls\"", stream_path);
2552 ret = WIMLIB_ERR_WRITE;
2560 ERROR("Error extracting \"%ls\"", stream_path);
2566 * Creates a file, directory, or reparse point and extracts all streams to it
2567 * (unnamed data stream and/or reparse point stream, plus any alternate data
2568 * streams). Handles sparse, compressed, and/or encrypted files.
2570 * @dentry: WIM dentry for this file or directory.
2571 * @path: UTF-16LE external path to extract the inode to.
2572 * @args: Additional extraction context.
2574 * Returns 0 on success; nonzero on failure.
2577 win32_extract_streams(const struct wim_dentry *dentry,
2578 const wchar_t *path, struct apply_args *args)
2580 struct wim_lookup_table_entry *unnamed_lte;
2582 const struct wim_inode *inode = dentry->d_inode;
2584 /* First extract the unnamed stream. */
2586 unnamed_lte = inode_unnamed_lte_resolved(inode);
2587 ret = win32_extract_stream(dentry, path, NULL, unnamed_lte, args);
2591 /* Extract any named streams, if supported by the volume. */
2593 if (!(args->vol_flags & FILE_NAMED_STREAMS))
2595 for (u16 i = 0; i < inode->i_num_ads; i++) {
2596 const struct wim_ads_entry *ads_entry = &inode->i_ads_entries[i];
2598 /* Skip the unnamed stream if it's in the ADS entries (we
2599 * already extracted it...) */
2600 if (ads_entry->stream_name_nbytes == 0)
2603 /* Skip special UNIX data entries (see documentation for
2604 * WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) */
2605 if (ads_entry->stream_name_nbytes == WIMLIB_UNIX_DATA_TAG_UTF16LE_NBYTES
2606 && !memcmp(ads_entry->stream_name,
2607 WIMLIB_UNIX_DATA_TAG_UTF16LE,
2608 WIMLIB_UNIX_DATA_TAG_UTF16LE_NBYTES))
2611 /* Extract the named stream */
2612 ret = win32_extract_stream(dentry,
2614 ads_entry->stream_name,
2624 /* If not done already, load the supported feature flags for the volume onto
2625 * which the image is being extracted, and warn the user about any missing
2626 * features that could be important. */
2628 win32_check_vol_flags(const wchar_t *output_path, struct apply_args *args)
2630 if (args->have_vol_flags)
2633 win32_get_vol_flags(output_path, &args->vol_flags);
2634 args->have_vol_flags = true;
2635 /* Warn the user about data that may not be extracted. */
2636 if (!(args->vol_flags & FILE_SUPPORTS_SPARSE_FILES))
2637 WARNING("Volume does not support sparse files!\n"
2638 " Sparse files will be extracted as non-sparse.");
2639 if (!(args->vol_flags & FILE_SUPPORTS_REPARSE_POINTS))
2640 WARNING("Volume does not support reparse points!\n"
2641 " Reparse point data will not be extracted.");
2642 if (!(args->vol_flags & FILE_NAMED_STREAMS)) {
2643 WARNING("Volume does not support named data streams!\n"
2644 " Named data streams will not be extracted.");
2646 if (!(args->vol_flags & FILE_SUPPORTS_ENCRYPTION)) {
2647 WARNING("Volume does not support encryption!\n"
2648 " Encrypted files will be extracted as raw data.");
2650 if (!(args->vol_flags & FILE_FILE_COMPRESSION)) {
2651 WARNING("Volume does not support transparent compression!\n"
2652 " Compressed files will be extracted as non-compressed.");
2654 if (!(args->vol_flags & FILE_PERSISTENT_ACLS)) {
2655 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_ACLS) {
2656 ERROR("Strict ACLs requested, but the volume does not "
2658 return WIMLIB_ERR_VOLUME_LACKS_FEATURES;
2660 WARNING("Volume does not support persistent ACLS!\n"
2661 " File permissions will not be extracted.");
2668 * Try extracting a hard link.
2670 * @output_path: Path to link to be extracted.
2672 * @inode: WIM inode that the link is to; inode->i_extracted_file
2673 * the path to a name of the file that has already been
2674 * extracted (we use this to create the hard link).
2676 * @args: Additional apply context, used here to keep track of
2677 * the number of times creating a hard link failed due to
2678 * ERROR_INVALID_FUNCTION. This error should indicate that hard
2679 * links are not supported by the volume, and we would like to
2680 * warn the user a few times, but not too many times.
2682 * Returns 0 if the hard link was successfully extracted. Returns
2683 * WIMLIB_ERR_LINK (> 0) if an error occurred, other than hard links possibly
2684 * being unsupported by the volume. Returns a negative value if creating the
2685 * hard link failed due to ERROR_INVALID_FUNCTION.
2688 win32_try_hard_link(const wchar_t *output_path, const struct wim_inode *inode,
2689 struct apply_args *args)
2693 /* There is a volume flag for this (FILE_SUPPORTS_HARD_LINKS),
2694 * but it's only available on Windows 7 and later. So no use
2695 * even checking it, really. Instead, CreateHardLinkW() will
2696 * apparently return ERROR_INVALID_FUNCTION if the volume does
2697 * not support hard links. */
2698 DEBUG("Creating hard link \"%ls => %ls\"",
2699 output_path, inode->i_extracted_file);
2700 if (CreateHardLinkW(output_path, inode->i_extracted_file, NULL))
2703 err = GetLastError();
2704 if (err != ERROR_INVALID_FUNCTION) {
2705 ERROR("Can't create hard link \"%ls => %ls\"",
2706 output_path, inode->i_extracted_file);
2708 return WIMLIB_ERR_LINK;
2710 args->num_hard_links_failed++;
2711 if (args->num_hard_links_failed < MAX_CREATE_HARD_LINK_WARNINGS) {
2712 WARNING("Can't create hard link \"%ls => %ls\":\n"
2713 " Volume does not support hard links!\n"
2714 " Falling back to extracting a copy of the file.",
2715 output_path, inode->i_extracted_file);
2716 } else if (args->num_hard_links_failed == MAX_CREATE_HARD_LINK_WARNINGS) {
2717 WARNING("Suppressing further hard linking warnings...");
2723 /* Extract a file, directory, reparse point, or hard link to an
2724 * already-extracted file using the Win32 API */
2726 win32_do_apply_dentry(const wchar_t *output_path,
2727 size_t output_path_num_chars,
2728 struct wim_dentry *dentry,
2729 struct apply_args *args)
2732 struct wim_inode *inode = dentry->d_inode;
2734 ret = win32_check_vol_flags(output_path, args);
2737 if (inode->i_nlink > 1 && inode->i_extracted_file != NULL) {
2738 /* Linked file, with another name already extracted. Create a
2740 ret = win32_try_hard_link(output_path, inode, args);
2743 /* Negative return value from win32_try_hard_link() indicates
2744 * that hard links are probably not supported by the volume.
2745 * Fall back to extracting a copy of the file. */
2748 /* If this is a reparse point and the volume does not support reparse
2749 * points, just skip it completely. */
2750 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT &&
2751 !(args->vol_flags & FILE_SUPPORTS_REPARSE_POINTS))
2753 WARNING("Skipping extraction of reparse point \"%ls\":\n"
2754 " Not supported by destination filesystem",
2757 /* Create the file, directory, or reparse point, and extract the
2759 ret = win32_extract_streams(dentry, output_path, args);
2763 if (inode->i_extracted_file == NULL) {
2764 const struct wim_lookup_table_entry *lte;
2766 /* Tally bytes extracted, including all alternate data streams,
2767 * unless we extracted a hard link (or, at least extracted a
2768 * name that was supposed to be a hard link) */
2769 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
2770 lte = inode_stream_lte_resolved(inode, i);
2772 args->progress.extract.completed_bytes +=
2773 wim_resource_size(lte);
2775 if (inode->i_nlink > 1) {
2776 /* Save extracted path for a later call to
2777 * CreateHardLinkW() if this inode has multiple links.
2779 inode->i_extracted_file = WSTRDUP(output_path);
2780 if (!inode->i_extracted_file)
2781 return WIMLIB_ERR_NOMEM;
2787 /* Set timestamps on an extracted file using the Win32 API */
2789 win32_do_apply_dentry_timestamps(const wchar_t *path,
2790 size_t path_num_chars,
2791 const struct wim_dentry *dentry,
2792 const struct apply_args *args)
2796 const struct wim_inode *inode = dentry->d_inode;
2798 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT &&
2799 !(args->vol_flags & FILE_SUPPORTS_REPARSE_POINTS))
2801 /* Skip reparse points not extracted */
2805 /* Windows doesn't let you change the timestamps of the root directory
2806 * (at least on FAT, which is dumb but expected since FAT doesn't store
2807 * any metadata about the root directory...) */
2808 if (path_is_root_of_drive(path))
2811 DEBUG("Opening \"%ls\" to set timestamps", path);
2812 h = win32_open_existing_file(path, FILE_WRITE_ATTRIBUTES);
2813 if (h == INVALID_HANDLE_VALUE) {
2814 err = GetLastError();
2818 FILETIME creationTime = {.dwLowDateTime = inode->i_creation_time & 0xffffffff,
2819 .dwHighDateTime = inode->i_creation_time >> 32};
2820 FILETIME lastAccessTime = {.dwLowDateTime = inode->i_last_access_time & 0xffffffff,
2821 .dwHighDateTime = inode->i_last_access_time >> 32};
2822 FILETIME lastWriteTime = {.dwLowDateTime = inode->i_last_write_time & 0xffffffff,
2823 .dwHighDateTime = inode->i_last_write_time >> 32};
2825 DEBUG("Calling SetFileTime() on \"%ls\"", path);
2826 if (!SetFileTime(h, &creationTime, &lastAccessTime, &lastWriteTime)) {
2827 err = GetLastError();
2831 DEBUG("Closing \"%ls\"", path);
2832 if (!CloseHandle(h)) {
2833 err = GetLastError();
2838 /* Only warn if setting timestamps failed; still return 0. */
2839 WARNING("Can't set timestamps on \"%ls\"", path);
2845 /* Replacement for POSIX fsync() */
2852 h = (HANDLE)_get_osfhandle(fd);
2853 if (h == INVALID_HANDLE_VALUE)
2855 if (!FlushFileBuffers(h))
2859 set_errno_from_GetLastError();
2864 /* Use the Win32 API to get the number of processors */
2866 win32_get_number_of_processors()
2868 SYSTEM_INFO sysinfo;
2869 GetSystemInfo(&sysinfo);
2870 return sysinfo.dwNumberOfProcessors;
2873 /* Replacement for POSIX-2008 realpath(). Warning: partial functionality only
2874 * (resolved_path must be NULL). Also I highly doubt that GetFullPathName
2875 * really does the right thing under all circumstances. */
2877 realpath(const wchar_t *path, wchar_t *resolved_path)
2881 wimlib_assert(resolved_path == NULL);
2883 ret = GetFullPathNameW(path, 0, NULL, NULL);
2885 err = GetLastError();
2889 resolved_path = TMALLOC(ret);
2892 ret = GetFullPathNameW(path, ret, resolved_path, NULL);
2894 err = GetLastError();
2895 free(resolved_path);
2896 resolved_path = NULL;
2901 errno = win32_error_to_errno(err);
2903 return resolved_path;
2906 /* rename() on Windows fails if the destination file exists. And we need to
2907 * make it work on wide characters. Fix it. */
2909 win32_rename_replacement(const wchar_t *oldpath, const wchar_t *newpath)
2911 if (MoveFileExW(oldpath, newpath, MOVEFILE_REPLACE_EXISTING)) {
2914 set_errno_from_GetLastError();
2919 /* Replacement for POSIX fnmatch() (partial functionality only) */
2921 fnmatch(const wchar_t *pattern, const wchar_t *string, int flags)
2923 if (PathMatchSpecW(string, pattern))
2929 /* truncate() replacement */
2931 win32_truncate_replacement(const wchar_t *path, off_t size)
2933 DWORD err = NO_ERROR;
2934 LARGE_INTEGER liOffset;
2936 HANDLE h = win32_open_existing_file(path, GENERIC_WRITE);
2937 if (h == INVALID_HANDLE_VALUE)
2940 liOffset.QuadPart = size;
2941 if (!SetFilePointerEx(h, liOffset, NULL, FILE_BEGIN))
2942 goto fail_close_handle;
2944 if (!SetEndOfFile(h))
2945 goto fail_close_handle;
2950 err = GetLastError();
2953 if (err == NO_ERROR)
2954 err = GetLastError();
2955 errno = win32_error_to_errno(err);
2960 /* This really could be replaced with _wcserror_s, but this doesn't seem to
2961 * actually be available in MSVCRT.DLL on Windows XP (perhaps it's statically
2962 * linked in by Visual Studio...?). */
2964 win32_strerror_r_replacement(int errnum, wchar_t *buf, size_t buflen)
2966 static pthread_mutex_t strerror_lock = PTHREAD_MUTEX_INITIALIZER;
2968 pthread_mutex_lock(&strerror_lock);
2969 mbstowcs(buf, strerror(errnum), buflen);
2970 buf[buflen - 1] = '\0';
2971 pthread_mutex_unlock(&strerror_lock);
2976 do_pread_or_pwrite(int fd, void *buf, size_t count, off_t offset,
2980 LARGE_INTEGER orig_offset;
2981 DWORD bytes_read_or_written;
2982 LARGE_INTEGER relative_offset;
2983 OVERLAPPED overlapped;
2986 wimlib_assert(count <= 0xffffffff);
2988 h = (HANDLE)_get_osfhandle(fd);
2989 if (h == INVALID_HANDLE_VALUE)
2992 /* Get original position */
2993 relative_offset.QuadPart = 0;
2994 if (!SetFilePointerEx(h, relative_offset, &orig_offset, FILE_CURRENT))
2997 memset(&overlapped, 0, sizeof(overlapped));
2998 overlapped.Offset = offset;
2999 overlapped.OffsetHigh = offset >> 32;
3001 /* Do the read or write at the specified offset */
3003 bret = WriteFile(h, buf, count, &bytes_read_or_written, &overlapped);
3005 bret = ReadFile(h, buf, count, &bytes_read_or_written, &overlapped);
3009 /* Restore the original position */
3010 if (!SetFilePointerEx(h, orig_offset, NULL, FILE_BEGIN))
3013 return bytes_read_or_written;
3015 set_errno_from_GetLastError();
3020 /* Dumb Windows implementation of pread(). It temporarily changes the file
3021 * offset, so it is not safe to use with readers/writers on the same file
3024 win32_pread(int fd, void *buf, size_t count, off_t offset)
3026 return do_pread_or_pwrite(fd, buf, count, offset, false);
3029 /* Dumb Windows implementation of pwrite(). It temporarily changes the file
3030 * offset, so it is not safe to use with readers/writers on the same file
3033 win32_pwrite(int fd, const void *buf, size_t count, off_t offset)
3035 return do_pread_or_pwrite(fd, (void*)buf, count, offset, true);
3038 /* Dumb Windows implementation of writev(). It writes the vectors one at a
3041 win32_writev(int fd, const struct iovec *iov, int iovcnt)
3043 ssize_t total_bytes_written = 0;
3049 for (int i = 0; i < iovcnt; i++) {
3050 ssize_t bytes_written;
3052 bytes_written = write(fd, iov[i].iov_base, iov[i].iov_len);
3053 if (bytes_written >= 0)
3054 total_bytes_written += bytes_written;
3055 if (bytes_written != iov[i].iov_len) {
3056 if (total_bytes_written == 0)
3057 total_bytes_written = -1;
3061 return total_bytes_written;
3064 #endif /* __WIN32__ */