2 * win32_capture.c - Windows-specific code for capturing files into 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 "wimlib/win32_common.h"
32 #include "wimlib/capture.h"
33 #include "wimlib/endianness.h"
34 #include "wimlib/error.h"
35 #include "wimlib/lookup_table.h"
36 #include "wimlib/paths.h"
37 #include "wimlib/reparse.h"
39 #define MAX_GET_SD_ACCESS_DENIED_WARNINGS 1
40 #define MAX_GET_SACL_PRIV_NOTHELD_WARNINGS 1
41 #define MAX_CAPTURE_LONG_PATH_WARNINGS 5
43 struct win32_capture_state {
44 unsigned long num_get_sd_access_denied;
45 unsigned long num_get_sacl_priv_notheld;
46 unsigned long num_long_path_warnings;
50 static const wchar_t *capture_access_denied_msg =
51 L" If you are not running this program as the administrator, you may\n"
52 " need to do so, so that all data and metadata can be backed up.\n"
53 " Otherwise, there may be no way to access the desired data or\n"
54 " metadata without taking ownership of the file or directory.\n"
58 read_win32_file_prefix(const struct wim_lookup_table_entry *lte,
60 consume_data_callback_t cb,
67 bool out_buf_malloced;
69 const size_t stack_max = 32768;
71 HANDLE hFile = win32_open_existing_file(lte->file_on_disk,
73 if (hFile == INVALID_HANDLE_VALUE) {
74 set_errno_from_GetLastError();
75 ERROR_WITH_ERRNO("Failed to open \"%ls\"", lte->file_on_disk);
76 return WIMLIB_ERR_OPEN;
79 out_buf_malloced = false;
81 if (in_chunk_size <= stack_max) {
82 out_buf = alloca(in_chunk_size);
84 out_buf = MALLOC(in_chunk_size);
85 if (out_buf == NULL) {
86 ret = WIMLIB_ERR_NOMEM;
87 goto out_close_handle;
89 out_buf_malloced = true;
95 bytes_remaining = size;
96 while (bytes_remaining) {
97 DWORD bytesToRead, bytesRead;
99 bytesToRead = min(in_chunk_size, bytes_remaining);
100 if (!ReadFile(hFile, out_buf, bytesToRead, &bytesRead, NULL) ||
101 bytesRead != bytesToRead)
103 set_errno_from_GetLastError();
104 ERROR_WITH_ERRNO("Failed to read data from \"%ls\"",
106 ret = WIMLIB_ERR_READ;
109 bytes_remaining -= bytesRead;
111 ret = (*cb)(out_buf, bytesRead, ctx_or_buf);
115 out_buf += bytesRead;
118 if (out_buf_malloced)
125 struct win32_encrypted_read_ctx {
126 consume_data_callback_t read_prefix_cb;
127 void *read_prefix_ctx_or_buf;
136 win32_encrypted_export_cb(unsigned char *_data, void *_ctx, unsigned long len)
138 const void *data = _data;
139 struct win32_encrypted_read_ctx *ctx = _ctx;
140 u32 in_chunk_size = ctx->in_chunk_size;
143 DEBUG("len = %lu", len);
144 if (ctx->read_prefix_cb) {
145 /* The length of the buffer passed to the ReadEncryptedFileRaw()
146 * export callback is undocumented, so we assume it may be of
148 size_t bytes_to_buffer = min(ctx->bytes_remaining - ctx->buf_filled,
150 while (bytes_to_buffer) {
151 size_t bytes_to_copy_to_buf =
152 min(bytes_to_buffer, in_chunk_size - ctx->buf_filled);
154 memcpy(ctx->buf + ctx->buf_filled, data,
155 bytes_to_copy_to_buf);
156 ctx->buf_filled += bytes_to_copy_to_buf;
157 data += bytes_to_copy_to_buf;
158 bytes_to_buffer -= bytes_to_copy_to_buf;
160 if (ctx->buf_filled == in_chunk_size ||
161 ctx->buf_filled == ctx->bytes_remaining)
163 ret = (*ctx->read_prefix_cb)(ctx->buf,
165 ctx->read_prefix_ctx_or_buf);
167 ctx->wimlib_err_code = ret;
168 /* Shouldn't matter what error code is returned
169 * here, as long as it isn't ERROR_SUCCESS. */
170 return ERROR_READ_FAULT;
172 ctx->bytes_remaining -= ctx->buf_filled;
177 size_t len_to_copy = min(len, ctx->bytes_remaining);
178 ctx->read_prefix_ctx_or_buf = mempcpy(ctx->read_prefix_ctx_or_buf,
181 ctx->bytes_remaining -= len_to_copy;
183 return ERROR_SUCCESS;
187 read_win32_encrypted_file_prefix(const struct wim_lookup_table_entry *lte,
189 consume_data_callback_t cb,
194 struct win32_encrypted_read_ctx export_ctx;
199 DEBUG("Reading %"PRIu64" bytes from encryted file \"%ls\"",
200 size, lte->file_on_disk);
202 export_ctx.read_prefix_cb = cb;
203 export_ctx.read_prefix_ctx_or_buf = ctx_or_buf;
204 export_ctx.wimlib_err_code = 0;
206 export_ctx.buf = MALLOC(in_chunk_size);
208 return WIMLIB_ERR_NOMEM;
210 export_ctx.buf = NULL;
212 export_ctx.buf_filled = 0;
213 export_ctx.bytes_remaining = size;
215 err = OpenEncryptedFileRaw(lte->file_on_disk, 0, &file_ctx);
216 if (err != ERROR_SUCCESS) {
217 set_errno_from_win32_error(err);
218 ERROR_WITH_ERRNO("Failed to open encrypted file \"%ls\" "
219 "for raw read", lte->file_on_disk);
220 ret = WIMLIB_ERR_OPEN;
223 err = ReadEncryptedFileRaw(win32_encrypted_export_cb,
224 &export_ctx, file_ctx);
225 if (err != ERROR_SUCCESS) {
226 set_errno_from_win32_error(err);
227 ERROR_WITH_ERRNO("Failed to read encrypted file \"%ls\"",
229 ret = export_ctx.wimlib_err_code;
231 ret = WIMLIB_ERR_READ;
232 } else if (export_ctx.bytes_remaining != 0) {
233 ERROR("Only could read %"PRIu64" of %"PRIu64" bytes from "
234 "encryted file \"%ls\"",
235 size - export_ctx.bytes_remaining, size,
237 ret = WIMLIB_ERR_READ;
241 CloseEncryptedFileRaw(file_ctx);
243 FREE(export_ctx.buf);
249 FILETIME_to_u64(const FILETIME *ft)
251 return ((u64)ft->dwHighDateTime << 32) | (u64)ft->dwLowDateTime;
254 /* Load the short name of a file into a WIM dentry.
256 * If we can't read the short filename for some reason, we just ignore the error
257 * and assume the file has no short name. This shouldn't be an issue, since the
258 * short names are essentially obsolete anyway.
261 win32_get_short_name(HANDLE hFile, const wchar_t *path, struct wim_dentry *dentry)
264 /* It's not any harder to just make the NtQueryInformationFile() system
265 * call ourselves, and it saves a dumb call to FindFirstFile() which of
266 * course has to create its own handle. */
268 if (func_NtQueryInformationFile) {
270 IO_STATUS_BLOCK io_status;
271 u8 buf[128] _aligned_attribute(8);
272 const FILE_NAME_INFORMATION *info;
274 status = (*func_NtQueryInformationFile)(hFile, &io_status, buf, sizeof(buf),
275 FileAlternateNameInformation);
276 info = (const FILE_NAME_INFORMATION*)buf;
277 if (status == STATUS_SUCCESS && info->FileNameLength != 0) {
278 dentry->short_name = MALLOC(info->FileNameLength + 2);
279 if (!dentry->short_name)
280 return WIMLIB_ERR_NOMEM;
281 memcpy(dentry->short_name, info->FileName,
282 info->FileNameLength);
283 dentry->short_name[info->FileNameLength / 2] = L'\0';
284 dentry->short_name_nbytes = info->FileNameLength;
290 WIN32_FIND_DATAW dat;
294 hFind = FindFirstFile(path, &dat);
295 if (hFind != INVALID_HANDLE_VALUE) {
296 if (dat.cAlternateFileName[0] != L'\0') {
297 DEBUG("\"%ls\": short name \"%ls\"", path, dat.cAlternateFileName);
298 size_t short_name_nbytes = wcslen(dat.cAlternateFileName) *
300 size_t n = short_name_nbytes + sizeof(wchar_t);
301 dentry->short_name = MALLOC(n);
302 if (dentry->short_name) {
303 memcpy(dentry->short_name, dat.cAlternateFileName, n);
304 dentry->short_name_nbytes = short_name_nbytes;
306 ret = WIMLIB_ERR_NOMEM;
315 * win32_query_security_descriptor() - Query a file's security descriptor
317 * We need the file's security descriptor in SECURITY_DESCRIPTOR_RELATIVE
318 * format, and we currently have a handle opened with as many relevant
319 * permissions as possible. At this point, on Windows there are a number of
320 * options for reading a file's security descriptor:
322 * GetFileSecurity(): This takes in a path and returns the
323 * SECURITY_DESCRIPTOR_RELATIVE. Problem: this uses an internal handle, not
324 * ours, and the handle created internally doesn't specify
325 * FILE_FLAG_BACKUP_SEMANTICS. Therefore there can be access denied errors on
326 * some files and directories, even when running as the Administrator.
328 * GetSecurityInfo(): This takes in a handle and returns the security
329 * descriptor split into a bunch of different parts. This should work, but it's
330 * dumb because we have to put the security descriptor back together again.
332 * BackupRead(): This can read the security descriptor, but this is a
333 * difficult-to-use API, probably only works as the Administrator, and the
334 * format of the returned data is not well documented.
336 * NtQuerySecurityObject(): This is exactly what we need, as it takes in a
337 * handle and returns the security descriptor in SECURITY_DESCRIPTOR_RELATIVE
338 * format. Only problem is that it's a ntdll function and therefore not
339 * officially part of the Win32 API. Oh well.
342 win32_query_security_descriptor(HANDLE hFile, const wchar_t *path,
343 SECURITY_INFORMATION requestedInformation,
344 SECURITY_DESCRIPTOR *buf,
345 DWORD bufsize, DWORD *lengthNeeded)
348 if (func_NtQuerySecurityObject) {
351 status = (*func_NtQuerySecurityObject)(hFile,
352 requestedInformation, buf,
353 bufsize, lengthNeeded);
354 /* Since it queries an already-open handle, NtQuerySecurityObject()
355 * apparently returns STATUS_ACCESS_DENIED rather than
356 * STATUS_PRIVILEGE_NOT_HELD. */
357 if (status == STATUS_ACCESS_DENIED)
358 return ERROR_PRIVILEGE_NOT_HELD;
360 return (*func_RtlNtStatusToDosError)(status);
363 if (GetFileSecurity(path, requestedInformation, buf,
364 bufsize, lengthNeeded))
365 return ERROR_SUCCESS;
367 return GetLastError();
371 win32_get_security_descriptor(HANDLE hFile,
373 struct wim_inode *inode,
374 struct wim_sd_set *sd_set,
375 struct win32_capture_state *state,
378 SECURITY_INFORMATION requestedInformation;
386 requestedInformation = DACL_SECURITY_INFORMATION |
387 SACL_SECURITY_INFORMATION |
388 OWNER_SECURITY_INFORMATION |
389 GROUP_SECURITY_INFORMATION;
391 bufsize = sizeof(_buf);
393 err = win32_query_security_descriptor(hFile, path,
394 requestedInformation,
395 (SECURITY_DESCRIPTOR*)buf,
396 bufsize, &lenNeeded);
399 goto have_descriptor;
400 case ERROR_INSUFFICIENT_BUFFER:
401 wimlib_assert(buf == _buf);
402 buf = MALLOC(lenNeeded);
404 return WIMLIB_ERR_NOMEM;
407 case ERROR_PRIVILEGE_NOT_HELD:
408 if (add_flags & WIMLIB_ADD_FLAG_STRICT_ACLS)
410 if (requestedInformation & SACL_SECURITY_INFORMATION) {
411 state->num_get_sacl_priv_notheld++;
412 requestedInformation &= ~SACL_SECURITY_INFORMATION;
416 case ERROR_ACCESS_DENIED:
417 if (add_flags & WIMLIB_ADD_FLAG_STRICT_ACLS)
419 state->num_get_sd_access_denied++;
424 set_errno_from_win32_error(err);
425 ERROR_WITH_ERRNO("Failed to read security descriptor of \"%ls\"", path);
426 ret = WIMLIB_ERR_READ;
432 inode->i_security_id = sd_set_add_sd(sd_set, buf, lenNeeded);
433 if (inode->i_security_id < 0)
434 ret = WIMLIB_ERR_NOMEM;
444 win32_build_dentry_tree_recursive(struct wim_dentry **root_ret,
446 size_t path_num_chars,
447 struct add_image_params *params,
448 struct win32_capture_state *state,
451 /* Reads the directory entries of directory and recursively calls
452 * win32_build_dentry_tree() on them. */
454 win32_recurse_directory(HANDLE hDir,
456 size_t dir_path_num_chars,
457 struct wim_dentry *root,
458 struct add_image_params *params,
459 struct win32_capture_state *state,
464 DEBUG("Recurse to directory \"%ls\"", dir_path);
466 /* Using NtQueryDirectoryFile() we can re-use the same open handle,
467 * which we opened with FILE_FLAG_BACKUP_SEMANTICS (probably not the
468 * case for the FindFirstFile() API; it's not documented). */
470 if (!func_NtQueryDirectoryFile)
471 goto use_FindFirstFile;
474 IO_STATUS_BLOCK io_status;
475 const size_t bufsize = 8192;
477 BOOL restartScan = TRUE;
478 const FILE_NAMES_INFORMATION *info;
480 buf = MALLOC(bufsize);
482 return WIMLIB_ERR_NOMEM;
484 status = (*func_NtQueryDirectoryFile)(hDir, NULL, NULL, NULL,
485 &io_status, buf, bufsize,
486 FileNamesInformation,
487 FALSE, NULL, restartScan);
489 if (status != STATUS_SUCCESS) {
490 if (status == STATUS_NO_MORE_FILES ||
491 status == STATUS_NO_MORE_ENTRIES ||
492 status == STATUS_NO_MORE_MATCHES) {
494 } else if (status == STATUS_NOT_IMPLEMENTED ||
495 status == STATUS_NOT_SUPPORTED ||
496 status == STATUS_INVALID_INFO_CLASS) {
498 goto use_FindFirstFile;
500 set_errno_from_nt_status(status);
501 ERROR_WITH_ERRNO("Failed to read directory "
502 "\"%ls\"", dir_path);
503 ret = WIMLIB_ERR_READ;
507 wimlib_assert(io_status.Information != 0);
508 info = (const FILE_NAMES_INFORMATION*)buf;
510 if (!(info->FileNameLength == 2 && info->FileName[0] == L'.') &&
511 !(info->FileNameLength == 4 && info->FileName[0] == L'.' &&
512 info->FileName[1] == L'.'))
515 struct wim_dentry *child;
517 p = dir_path + dir_path_num_chars;
519 p = wmempcpy(p, info->FileName,
520 info->FileNameLength / 2);
523 ret = win32_build_dentry_tree_recursive(
531 dir_path[dir_path_num_chars] = L'\0';
536 dentry_add_child(root, child);
538 if (info->NextEntryOffset == 0)
540 info = (const FILE_NAMES_INFORMATION*)
541 ((const u8*)info + info->NextEntryOffset);
551 WIN32_FIND_DATAW dat;
555 /* Begin reading the directory by calling FindFirstFileW. Unlike UNIX
556 * opendir(), FindFirstFileW has file globbing built into it. But this
557 * isn't what we actually want, so just add a dummy glob to get all
559 dir_path[dir_path_num_chars] = OS_PREFERRED_PATH_SEPARATOR;
560 dir_path[dir_path_num_chars + 1] = L'*';
561 dir_path[dir_path_num_chars + 2] = L'\0';
562 hFind = FindFirstFile(dir_path, &dat);
563 dir_path[dir_path_num_chars] = L'\0';
565 if (hFind == INVALID_HANDLE_VALUE) {
566 err = GetLastError();
567 if (err == ERROR_FILE_NOT_FOUND) {
570 set_errno_from_win32_error(err);
571 ERROR_WITH_ERRNO("Failed to read directory \"%ls\"",
573 return WIMLIB_ERR_READ;
578 /* Skip . and .. entries */
579 if (dat.cFileName[0] == L'.' &&
580 (dat.cFileName[1] == L'\0' ||
581 (dat.cFileName[1] == L'.' &&
582 dat.cFileName[2] == L'\0')))
584 size_t filename_len = wcslen(dat.cFileName);
586 dir_path[dir_path_num_chars] = OS_PREFERRED_PATH_SEPARATOR;
587 wmemcpy(dir_path + dir_path_num_chars + 1,
591 struct wim_dentry *child;
592 size_t path_len = dir_path_num_chars + 1 + filename_len;
593 ret = win32_build_dentry_tree_recursive(&child,
599 dir_path[dir_path_num_chars] = L'\0';
603 dentry_add_child(root, child);
604 } while (FindNextFile(hFind, &dat));
605 err = GetLastError();
606 if (err != ERROR_NO_MORE_FILES) {
607 set_errno_from_win32_error(err);
608 ERROR_WITH_ERRNO("Failed to read directory \"%ls\"", dir_path);
610 ret = WIMLIB_ERR_READ;
617 /* Reparse point fixup status code */
619 /* Reparse point corresponded to an absolute symbolic link or junction
620 * point that pointed outside the directory tree being captured, and
621 * therefore was excluded. */
624 /* Reparse point was not fixed as it was either a relative symbolic
625 * link, a mount point, or something else we could not understand. */
628 /* Reparse point corresponded to an absolute symbolic link or junction
629 * point that pointed inside the directory tree being captured, where
630 * the target was specified by a "full" \??\ prefixed path, and
631 * therefore was fixed to be relative to the root of the directory tree
633 RP_FIXED_FULLPATH = 0x2,
635 /* Same as RP_FIXED_FULLPATH, except the absolute link target did not
636 * have the \??\ prefix. It may have begun with a drive letter though.
638 RP_FIXED_ABSPATH = 0x4,
640 /* Either RP_FIXED_FULLPATH or RP_FIXED_ABSPATH. */
641 RP_FIXED = RP_FIXED_FULLPATH | RP_FIXED_ABSPATH,
644 /* Given the "substitute name" target of a Windows reparse point, try doing a
645 * fixup where we change it to be absolute relative to the root of the directory
646 * tree being captured.
648 * Note that this is only executed when WIMLIB_ADD_FLAG_RPFIX has been
651 * @capture_root_ino and @capture_root_dev indicate the inode number and device
652 * of the root of the directory tree being captured. They are meant to identify
653 * this directory (as an alternative to its actual path, which could potentially
654 * be reached via multiple destinations due to other symbolic links). This may
655 * not work properly on FAT, which doesn't seem to supply proper inode numbers
656 * or file IDs. However, FAT doesn't support reparse points so this function
657 * wouldn't even be called anyway.
659 static enum rp_status
660 win32_capture_maybe_rpfix_target(wchar_t *target, u16 *target_nbytes_p,
661 u64 capture_root_ino, u64 capture_root_dev,
664 u16 target_nchars = *target_nbytes_p / 2;
665 size_t stripped_chars;
666 wchar_t *orig_target;
669 ret = parse_substitute_name(target, *target_nbytes_p, rptag);
672 stripped_chars = ret;
675 target[target_nchars] = L'\0';
676 orig_target = target;
677 target = capture_fixup_absolute_symlink(target + stripped_chars,
678 capture_root_ino, capture_root_dev);
681 target_nchars = wcslen(target);
682 wmemmove(orig_target + stripped_chars, target, target_nchars + 1);
683 *target_nbytes_p = (target_nchars + stripped_chars) * sizeof(wchar_t);
684 DEBUG("Fixed reparse point (new target: \"%ls\")", orig_target);
686 return RP_FIXED_FULLPATH;
688 return RP_FIXED_ABSPATH;
691 /* Returns: `enum rp_status' value on success; negative WIMLIB_ERR_* value on
694 win32_capture_try_rpfix(u8 *rpbuf, u16 *rpbuflen_p,
695 u64 capture_root_ino, u64 capture_root_dev,
698 struct reparse_data rpdata;
700 enum rp_status rp_status;
702 ret = parse_reparse_data(rpbuf, *rpbuflen_p, &rpdata);
706 rp_status = win32_capture_maybe_rpfix_target(rpdata.substitute_name,
707 &rpdata.substitute_name_nbytes,
710 le32_to_cpu(*(le32*)rpbuf));
711 if (rp_status & RP_FIXED) {
712 wimlib_assert(rpdata.substitute_name_nbytes % 2 == 0);
713 utf16lechar substitute_name_copy[rpdata.substitute_name_nbytes / 2];
714 wmemcpy(substitute_name_copy, rpdata.substitute_name,
715 rpdata.substitute_name_nbytes / 2);
716 rpdata.substitute_name = substitute_name_copy;
717 rpdata.print_name = substitute_name_copy;
718 rpdata.print_name_nbytes = rpdata.substitute_name_nbytes;
719 if (rp_status == RP_FIXED_FULLPATH) {
720 /* "full path", meaning \??\ prefixed. We should not
721 * include this prefix in the print name, as it is
722 * apparently meant for the filesystem driver only. */
723 rpdata.print_name += 4;
724 rpdata.print_name_nbytes -= 8;
726 ret = make_reparse_buffer(&rpdata, rpbuf, rpbuflen_p);
732 if (rp_status == RP_EXCLUDED) {
733 size_t print_name_nchars = rpdata.print_name_nbytes / 2;
734 wchar_t print_name0[print_name_nchars + 1];
735 print_name0[print_name_nchars] = L'\0';
736 wmemcpy(print_name0, rpdata.print_name, print_name_nchars);
737 WARNING("Ignoring %ls pointing out of capture directory:\n"
738 " \"%ls\" -> \"%ls\"\n"
739 " (Use --norpfix to capture all symbolic links "
740 "and junction points as-is)",
741 (rpdata.rptag == WIM_IO_REPARSE_TAG_SYMLINK) ?
742 L"absolute symbolic link" : L"junction point",
751 * Loads the reparse point data from a reparse point into memory, optionally
752 * fixing the targets of absolute symbolic links and junction points to be
753 * relative to the root of capture.
755 * @hFile: Open handle to the reparse point.
756 * @path: Path to the reparse point. Used for error messages only.
757 * @params: Additional parameters, including whether to do reparse point fixups
759 * @rpbuf: Buffer of length at least REPARSE_POINT_MAX_SIZE bytes into which
760 * the reparse point buffer will be loaded.
761 * @rpbuflen_ret: On success, the length of the reparse point buffer in bytes
762 * is written to this location.
765 * On success, returns an `enum rp_status' value that indicates if and/or
766 * how the reparse point fixup was done.
768 * On failure, returns a negative value that is a negated WIMLIB_ERR_*
772 win32_get_reparse_data(HANDLE hFile, const wchar_t *path,
773 struct add_image_params *params,
774 u8 *rpbuf, u16 *rpbuflen_ret)
781 DEBUG("Loading reparse data from \"%ls\"", path);
782 if (!DeviceIoControl(hFile, FSCTL_GET_REPARSE_POINT,
783 NULL, /* "Not used with this operation; set to NULL" */
784 0, /* "Not used with this operation; set to 0" */
785 rpbuf, /* "A pointer to a buffer that
786 receives the reparse point data */
787 REPARSE_POINT_MAX_SIZE, /* "The size of the output
792 set_errno_from_GetLastError();
793 ERROR_WITH_ERRNO("Failed to get reparse data of \"%ls\"", path);
794 return -WIMLIB_ERR_READ;
796 if (bytesReturned < 8 || bytesReturned > REPARSE_POINT_MAX_SIZE) {
797 ERROR("Reparse data on \"%ls\" is invalid", path);
798 return -WIMLIB_ERR_INVALID_REPARSE_DATA;
801 rpbuflen = bytesReturned;
802 reparse_tag = le32_to_cpu(*(le32*)rpbuf);
803 if (params->add_flags & WIMLIB_ADD_FLAG_RPFIX &&
804 (reparse_tag == WIM_IO_REPARSE_TAG_SYMLINK ||
805 reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT))
807 /* Try doing reparse point fixup */
808 ret = win32_capture_try_rpfix(rpbuf,
810 params->capture_root_ino,
811 params->capture_root_dev,
816 *rpbuflen_ret = rpbuflen;
821 win32_tally_encrypted_size_cb(unsigned char *_data, void *_size_ret,
824 *(u64*)_size_ret += len;
825 return ERROR_SUCCESS;
829 win32_get_encrypted_file_size(const wchar_t *path, u64 *size_ret)
835 err = OpenEncryptedFileRaw(path, 0, &file_ctx);
836 if (err != ERROR_SUCCESS) {
837 set_errno_from_win32_error(err);
838 ERROR_WITH_ERRNO("Failed to open encrypted file \"%ls\" "
839 "for raw read", path);
840 return WIMLIB_ERR_OPEN;
843 err = ReadEncryptedFileRaw(win32_tally_encrypted_size_cb,
845 if (err != ERROR_SUCCESS) {
846 set_errno_from_win32_error(err);
847 ERROR_WITH_ERRNO("Failed to read raw encrypted data from "
849 ret = WIMLIB_ERR_READ;
853 CloseEncryptedFileRaw(file_ctx);
857 /* Scans an unnamed or named stream of a Win32 file (not a reparse point
858 * stream); calculates its SHA1 message digest and either creates a `struct
859 * wim_lookup_table_entry' in memory for it, or uses an existing 'struct
860 * wim_lookup_table_entry' for an identical stream.
862 * @path: Path to the file (UTF-16LE).
864 * @path_num_chars: Number of 2-byte characters in @path.
866 * @inode: WIM inode to save the stream into.
868 * @lookup_table: Stream lookup table for the WIM.
870 * @dat: A `WIN32_FIND_STREAM_DATA' structure that specifies the
873 * Returns 0 on success; nonzero on failure.
876 win32_capture_stream(const wchar_t *path,
877 size_t path_num_chars,
878 struct wim_inode *inode,
879 struct wim_lookup_table *lookup_table,
880 WIN32_FIND_STREAM_DATA *dat)
882 struct wim_ads_entry *ads_entry;
883 struct wim_lookup_table_entry *lte;
885 wchar_t *stream_name, *colon;
886 size_t stream_name_nchars;
887 bool is_named_stream;
890 size_t spath_buf_nbytes;
891 const wchar_t *relpath_prefix;
892 const wchar_t *colonchar;
894 DEBUG("Capture \"%ls\" stream \"%ls\"", path, dat->cStreamName);
896 /* The stream name should be returned as :NAME:TYPE */
897 stream_name = dat->cStreamName;
898 if (*stream_name != L':')
899 goto out_invalid_stream_name;
901 colon = wcschr(stream_name, L':');
903 goto out_invalid_stream_name;
905 if (wcscmp(colon + 1, L"$DATA")) {
906 /* Not a DATA stream */
913 stream_name_nchars = colon - stream_name;
914 is_named_stream = (stream_name_nchars != 0);
916 if (is_named_stream) {
917 /* Allocate an ADS entry for the named stream. */
918 ads_entry = inode_add_ads_utf16le(inode, stream_name,
919 stream_name_nchars * sizeof(wchar_t));
921 ret = WIMLIB_ERR_NOMEM;
926 /* If zero length stream, no lookup table entry needed. */
927 if ((u64)dat->StreamSize.QuadPart == 0) {
932 /* Create a UTF-16LE string @spath that gives the filename, then a
933 * colon, then the stream name. Or, if it's an unnamed stream, just the
934 * filename. It is MALLOC()'ed so that it can be saved in the
935 * wim_lookup_table_entry if needed.
937 * As yet another special case, relative paths need to be changed to
938 * begin with an explicit "./" so that, for example, a file t:ads, where
939 * :ads is the part we added, is not interpreted as a file on the t:
941 spath_nchars = path_num_chars;
942 relpath_prefix = L"";
944 if (is_named_stream) {
945 spath_nchars += 1 + stream_name_nchars;
947 if (path_num_chars == 1 && !is_any_path_separator(path[0])) {
949 static const wchar_t _relpath_prefix[] =
950 {L'.', OS_PREFERRED_PATH_SEPARATOR, L'\0'};
951 relpath_prefix = _relpath_prefix;
955 spath_buf_nbytes = (spath_nchars + 1) * sizeof(wchar_t);
956 spath = MALLOC(spath_buf_nbytes);
958 tsprintf(spath, L"%ls%ls%ls%ls",
959 relpath_prefix, path, colonchar, stream_name);
961 /* Make a new wim_lookup_table_entry */
962 lte = new_lookup_table_entry();
964 ret = WIMLIB_ERR_NOMEM;
967 lte->file_on_disk = spath;
969 if (inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED && !is_named_stream) {
971 lte->resource_location = RESOURCE_WIN32_ENCRYPTED;
972 ret = win32_get_encrypted_file_size(path, &encrypted_size);
975 lte->resource_entry.original_size = encrypted_size;
977 lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
978 lte->resource_entry.original_size = (u64)dat->StreamSize.QuadPart;
982 if (is_named_stream) {
983 stream_id = ads_entry->stream_id;
984 ads_entry->lte = lte;
989 lookup_table_insert_unhashed(lookup_table, lte, inode, stream_id);
995 out_invalid_stream_name:
996 ERROR("Invalid stream name: \"%ls:%ls\"", path, dat->cStreamName);
997 ret = WIMLIB_ERR_READ;
1001 /* Load information about the streams of an open file into a WIM inode.
1003 * By default, we use the NtQueryInformationFile() system call instead of
1004 * FindFirstStream() and FindNextStream(). This is done for two reasons:
1006 * - FindFirstStream() opens its own handle to the file or directory and
1007 * apparently does so without specifying FILE_FLAG_BACKUP_SEMANTICS, thereby
1008 * causing access denied errors on certain files (even when running as the
1010 * - FindFirstStream() and FindNextStream() is only available on Windows Vista
1011 * and later, whereas the stream support in NtQueryInformationFile() was
1012 * already present in Windows XP.
1015 win32_capture_streams(HANDLE *hFile_p,
1016 const wchar_t *path,
1017 size_t path_num_chars,
1018 struct wim_inode *inode,
1019 struct wim_lookup_table *lookup_table,
1023 WIN32_FIND_STREAM_DATA dat;
1026 u8 _buf[8192] _aligned_attribute(8);
1029 IO_STATUS_BLOCK io_status;
1031 const FILE_STREAM_INFORMATION *info;
1036 DEBUG("Capturing streams from \"%ls\"", path);
1038 if (!(vol_flags & FILE_NAMED_STREAMS))
1042 if (!func_NtQueryInformationFile)
1043 goto use_FindFirstStream;
1046 bufsize = sizeof(_buf);
1048 /* Get a buffer containing the stream information. */
1050 status = (*func_NtQueryInformationFile)(*hFile_p, &io_status,
1052 FileStreamInformation);
1053 if (status == STATUS_SUCCESS) {
1055 } else if (status == STATUS_BUFFER_OVERFLOW) {
1060 newbuf = MALLOC(bufsize);
1062 newbuf = REALLOC(buf, bufsize);
1065 ret = WIMLIB_ERR_NOMEM;
1069 } else if (status == STATUS_NOT_IMPLEMENTED ||
1070 status == STATUS_NOT_SUPPORTED ||
1071 status == STATUS_INVALID_INFO_CLASS) {
1072 goto use_FindFirstStream;
1074 set_errno_from_nt_status(status);
1075 ERROR_WITH_ERRNO("Failed to read streams of %ls", path);
1076 ret = WIMLIB_ERR_READ;
1081 if (io_status.Information == 0) {
1082 /* No stream information. */
1087 if (inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED) {
1088 /* OpenEncryptedFileRaw() seems to fail with
1089 * ERROR_SHARING_VIOLATION if there are any handles opened to
1091 CloseHandle(*hFile_p);
1092 *hFile_p = INVALID_HANDLE_VALUE;
1095 /* Parse one or more stream information structures. */
1096 info = (const FILE_STREAM_INFORMATION*)buf;
1098 if (info->StreamNameLength <= sizeof(dat.cStreamName) - 2) {
1099 dat.StreamSize = info->StreamSize;
1100 memcpy(dat.cStreamName, info->StreamName, info->StreamNameLength);
1101 dat.cStreamName[info->StreamNameLength / 2] = L'\0';
1103 /* Capture the stream. */
1104 ret = win32_capture_stream(path, path_num_chars, inode,
1105 lookup_table, &dat);
1109 if (info->NextEntryOffset == 0) {
1110 /* No more stream information. */
1114 /* Advance to next stream information. */
1115 info = (const FILE_STREAM_INFORMATION*)
1116 ((const u8*)info + info->NextEntryOffset);
1119 /* Free buffer if allocated on heap. */
1123 #endif /* WITH_NTDLL */
1125 use_FindFirstStream:
1126 if (win32func_FindFirstStreamW == NULL)
1128 hFind = win32func_FindFirstStreamW(path, FindStreamInfoStandard, &dat, 0);
1129 if (hFind == INVALID_HANDLE_VALUE) {
1130 err = GetLastError();
1131 if (err == ERROR_CALL_NOT_IMPLEMENTED ||
1132 err == ERROR_NOT_SUPPORTED ||
1133 err == ERROR_INVALID_FUNCTION ||
1134 err == ERROR_INVALID_PARAMETER)
1137 /* Seems legal for this to return ERROR_HANDLE_EOF on reparse
1138 * points and directories */
1139 if ((inode->i_attributes &
1140 (FILE_ATTRIBUTE_REPARSE_POINT | FILE_ATTRIBUTE_DIRECTORY))
1141 && err == ERROR_HANDLE_EOF)
1143 DEBUG("ERROR_HANDLE_EOF (ok)");
1146 if (err == ERROR_ACCESS_DENIED) {
1147 WARNING("Failed to look up data streams "
1148 "of \"%ls\": Access denied!\n%ls",
1149 path, capture_access_denied_msg);
1152 set_errno_from_win32_error(err);
1153 ERROR_WITH_ERRNO("Failed to look up data streams "
1154 "of \"%ls\"", path);
1155 return WIMLIB_ERR_READ;
1160 ret = win32_capture_stream(path,
1162 inode, lookup_table,
1165 goto out_find_close;
1166 } while (win32func_FindNextStreamW(hFind, &dat));
1167 err = GetLastError();
1168 if (err != ERROR_HANDLE_EOF) {
1169 set_errno_from_win32_error(err);
1170 ERROR_WITH_ERRNO("Error reading data streams from "
1172 ret = WIMLIB_ERR_READ;
1179 /* FindFirstStream() API is not available, or the volume does not
1180 * support named streams. Only capture the unnamed data stream. */
1181 DEBUG("Only capturing unnamed data stream");
1182 if (inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
1183 FILE_ATTRIBUTE_REPARSE_POINT))
1186 wcscpy(dat.cStreamName, L"::$DATA");
1187 dat.StreamSize.QuadPart = file_size;
1188 return win32_capture_stream(path, path_num_chars,
1189 inode, lookup_table, &dat);
1193 win32_build_dentry_tree_recursive(struct wim_dentry **root_ret,
1195 size_t path_num_chars,
1196 struct add_image_params *params,
1197 struct win32_capture_state *state,
1200 struct wim_dentry *root = NULL;
1201 struct wim_inode *inode;
1209 DWORD desiredAccess;
1211 params->progress.scan.cur_path = path;
1213 if (exclude_path(path, path_num_chars, params->config, true)) {
1214 if (params->add_flags & WIMLIB_ADD_FLAG_ROOT) {
1215 ERROR("Cannot exclude the root directory from capture");
1216 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
1219 do_capture_progress(params, WIMLIB_SCAN_DENTRY_EXCLUDED);
1225 if (path_num_chars >= 4 &&
1226 !wmemcmp(path, L"\\\\?\\", 4) &&
1227 path_num_chars + 1 - 4 > MAX_PATH &&
1228 state->num_long_path_warnings < MAX_CAPTURE_LONG_PATH_WARNINGS)
1230 WARNING("Path \"%ls\" exceeds MAX_PATH", path);
1231 if (++state->num_long_path_warnings == MAX_CAPTURE_LONG_PATH_WARNINGS)
1232 WARNING("Suppressing further warnings about long paths.");
1236 do_capture_progress(params, WIMLIB_SCAN_DENTRY_OK);
1238 desiredAccess = FILE_READ_DATA | FILE_READ_ATTRIBUTES |
1239 READ_CONTROL | ACCESS_SYSTEM_SECURITY;
1241 hFile = win32_open_existing_file(path, desiredAccess);
1242 if (hFile == INVALID_HANDLE_VALUE) {
1243 err = GetLastError();
1244 if (err == ERROR_ACCESS_DENIED || err == ERROR_PRIVILEGE_NOT_HELD) {
1245 if (desiredAccess & ACCESS_SYSTEM_SECURITY) {
1246 desiredAccess &= ~ACCESS_SYSTEM_SECURITY;
1249 if (desiredAccess & READ_CONTROL) {
1250 desiredAccess &= ~READ_CONTROL;
1254 set_errno_from_GetLastError();
1255 ERROR_WITH_ERRNO("Failed to open \"%ls\" for reading", path);
1256 ret = WIMLIB_ERR_OPEN;
1260 BY_HANDLE_FILE_INFORMATION file_info;
1261 if (!GetFileInformationByHandle(hFile, &file_info)) {
1262 set_errno_from_GetLastError();
1263 ERROR_WITH_ERRNO("Failed to get file information for \"%ls\"",
1265 ret = WIMLIB_ERR_STAT;
1266 goto out_close_handle;
1269 if (file_info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1270 rpbuf = alloca(REPARSE_POINT_MAX_SIZE);
1271 ret = win32_get_reparse_data(hFile, path, params,
1274 /* WIMLIB_ERR_* (inverted) */
1276 goto out_close_handle;
1277 } else if (ret & RP_FIXED) {
1279 } else if (ret == RP_EXCLUDED) {
1281 goto out_close_handle;
1287 /* Create a WIM dentry with an associated inode, which may be shared.
1289 * However, we need to explicitly check for directories and files with
1290 * only 1 link and refuse to hard link them. This is because Windows
1291 * has a bug where it can return duplicate File IDs for files and
1292 * directories on the FAT filesystem. */
1293 ret = inode_table_new_dentry(¶ms->inode_table,
1294 path_basename_with_len(path, path_num_chars),
1295 ((u64)file_info.nFileIndexHigh << 32) |
1296 (u64)file_info.nFileIndexLow,
1297 file_info.dwVolumeSerialNumber,
1298 (file_info.nNumberOfLinks <= 1 ||
1299 (file_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)),
1302 goto out_close_handle;
1304 ret = win32_get_short_name(hFile, path, root);
1306 goto out_close_handle;
1308 inode = root->d_inode;
1310 if (inode->i_nlink > 1) /* Shared inode; nothing more to do */
1311 goto out_close_handle;
1313 inode->i_attributes = file_info.dwFileAttributes;
1314 inode->i_creation_time = FILETIME_to_u64(&file_info.ftCreationTime);
1315 inode->i_last_write_time = FILETIME_to_u64(&file_info.ftLastWriteTime);
1316 inode->i_last_access_time = FILETIME_to_u64(&file_info.ftLastAccessTime);
1317 inode->i_resolved = 1;
1319 params->add_flags &= ~WIMLIB_ADD_FLAG_ROOT;
1321 if (!(params->add_flags & WIMLIB_ADD_FLAG_NO_ACLS)
1322 && (vol_flags & FILE_PERSISTENT_ACLS))
1324 ret = win32_get_security_descriptor(hFile, path, inode,
1325 ¶ms->sd_set, state,
1328 goto out_close_handle;
1331 file_size = ((u64)file_info.nFileSizeHigh << 32) |
1332 (u64)file_info.nFileSizeLow;
1335 /* Capture the unnamed data stream (only should be present for regular
1336 * files) and any alternate data streams. */
1337 ret = win32_capture_streams(&hFile,
1341 params->lookup_table,
1345 goto out_close_handle;
1347 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1348 /* Reparse point: set the reparse data (which we read already)
1350 inode->i_not_rpfixed = not_rpfixed;
1351 inode->i_reparse_tag = le32_to_cpu(*(le32*)rpbuf);
1352 ret = inode_set_unnamed_stream(inode, rpbuf + 8, rpbuflen - 8,
1353 params->lookup_table);
1354 } else if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY) {
1355 /* Directory (not a reparse point) --- recurse to children */
1357 if (hFile == INVALID_HANDLE_VALUE) {
1358 /* Re-open handle that was closed to read raw encrypted
1360 hFile = win32_open_existing_file(path, FILE_READ_DATA);
1361 if (hFile == INVALID_HANDLE_VALUE) {
1362 set_errno_from_GetLastError();
1363 ERROR_WITH_ERRNO("Failed to reopen \"%ls\"",
1365 ret = WIMLIB_ERR_OPEN;
1366 goto out_close_handle;
1369 ret = win32_recurse_directory(hFile,
1383 free_dentry_tree(root, params->lookup_table);
1388 win32_do_capture_warnings(const wchar_t *path,
1389 const struct win32_capture_state *state,
1392 if (state->num_get_sacl_priv_notheld == 0 &&
1393 state->num_get_sd_access_denied == 0)
1396 WARNING("Scan of \"%ls\" complete, but with one or more warnings:", path);
1397 if (state->num_get_sacl_priv_notheld != 0) {
1398 WARNING("- Could not capture SACL (System Access Control List)\n"
1399 " on %lu files or directories.",
1400 state->num_get_sacl_priv_notheld);
1402 if (state->num_get_sd_access_denied != 0) {
1403 WARNING("- Could not capture security descriptor at all\n"
1404 " on %lu files or directories.",
1405 state->num_get_sd_access_denied);
1407 WARNING("To fully capture all security descriptors, run the program\n"
1408 " with Administrator rights.");
1411 #define WINDOWS_NT_MAX_PATH 32768
1413 /* Win32 version of capturing a directory tree */
1415 win32_build_dentry_tree(struct wim_dentry **root_ret,
1416 const wchar_t *root_disk_path,
1417 struct add_image_params *params)
1422 struct win32_capture_state state;
1425 bool need_prefix_free = false;
1427 if (!win32func_FindFirstStreamW
1429 && !func_NtQueryInformationFile
1433 WARNING("Running on Windows XP or earlier; "
1434 "alternate data streams will not be captured.");
1437 path_nchars = wcslen(root_disk_path);
1438 if (path_nchars > WINDOWS_NT_MAX_PATH)
1439 return WIMLIB_ERR_INVALID_PARAM;
1441 ret = win32_get_file_and_vol_ids(root_disk_path,
1442 ¶ms->capture_root_ino,
1443 ¶ms->capture_root_dev);
1445 ERROR_WITH_ERRNO("Can't open %ls", root_disk_path);
1449 win32_get_vol_flags(root_disk_path, &vol_flags, NULL);
1451 /* WARNING: There is no check for overflow later when this buffer is
1452 * being used! But it's as long as the maximum path length understood
1453 * by Windows NT (which is NOT the same as MAX_PATH). */
1454 path = MALLOC(WINDOWS_NT_MAX_PATH * sizeof(wchar_t));
1456 return WIMLIB_ERR_NOMEM;
1458 /* Work around defective behavior in Windows where paths longer than 260
1459 * characters are not supported by default; instead they need to be
1460 * turned into absolute paths and prefixed with "\\?\". */
1462 if (wcsncmp(root_disk_path, L"\\\\?\\", 4)) {
1463 dret = GetFullPathName(root_disk_path, WINDOWS_NT_MAX_PATH - 4,
1466 if (dret == 0 || dret >= WINDOWS_NT_MAX_PATH - 4) {
1467 WARNING("Can't get full path name for \"%ls\"", root_disk_path);
1468 wmemcpy(path, root_disk_path, path_nchars + 1);
1470 wmemcpy(path, L"\\\\?\\", 4);
1471 path_nchars = 4 + dret;
1472 /* Update pattern prefix */
1473 if (params->config != NULL)
1475 params->config->_prefix = TSTRDUP(path);
1476 params->config->_prefix_num_tchars = path_nchars;
1477 if (params->config->_prefix == NULL)
1479 ret = WIMLIB_ERR_NOMEM;
1482 need_prefix_free = true;
1486 wmemcpy(path, root_disk_path, path_nchars + 1);
1489 memset(&state, 0, sizeof(state));
1490 ret = win32_build_dentry_tree_recursive(root_ret, path,
1491 path_nchars, params,
1493 if (need_prefix_free)
1494 FREE(params->config->_prefix);
1498 win32_do_capture_warnings(root_disk_path, &state, params->add_flags);
1502 #endif /* __WIN32__ */