2 * win32_capture.c - Windows-specific code for capturing files into a WIM image.
6 * Copyright (C) 2013, 2014 Eric Biggers
8 * This file is part of wimlib, a library for working with WIM files.
10 * wimlib is free software; you can redistribute it and/or modify it under the
11 * terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 3 of the License, or (at your option)
15 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
16 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 * A PARTICULAR PURPOSE. See the GNU General Public License for more
20 * You should have received a copy of the GNU General Public License
21 * along with wimlib; if not, see http://www.gnu.org/licenses/.
30 #include "wimlib/win32_common.h"
32 #include "wimlib/capture.h"
33 #include "wimlib/dentry.h"
34 #include "wimlib/encoding.h"
35 #include "wimlib/endianness.h"
36 #include "wimlib/error.h"
37 #include "wimlib/lookup_table.h"
38 #include "wimlib/paths.h"
39 #include "wimlib/reparse.h"
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 winnt_openat(HANDLE cur_dir, const wchar_t *path, size_t path_nchars,
51 ACCESS_MASK perms, HANDLE *h_ret)
54 OBJECT_ATTRIBUTES attr;
58 name.Length = path_nchars * sizeof(wchar_t);
59 name.MaximumLength = name.Length + sizeof(wchar_t);
60 name.Buffer = (wchar_t *)path;
62 attr.Length = sizeof(attr);
63 attr.RootDirectory = cur_dir;
64 attr.ObjectName = &name;
66 attr.SecurityDescriptor = NULL;
67 attr.SecurityQualityOfService = NULL;
70 status = (*func_NtOpenFile)(h_ret, perms, &attr, &iosb,
74 FILE_OPEN_REPARSE_POINT |
75 FILE_OPEN_FOR_BACKUP_INTENT |
76 FILE_SYNCHRONOUS_IO_NONALERT);
77 if (!NT_SUCCESS(status)) {
78 if (status == STATUS_ACCESS_DENIED ||
79 status == STATUS_PRIVILEGE_NOT_HELD) {
80 if (perms & ACCESS_SYSTEM_SECURITY) {
81 perms &= ~ACCESS_SYSTEM_SECURITY;
84 if (perms & READ_CONTROL) {
85 perms &= ~READ_CONTROL;
94 read_win32_file_prefix(const struct wim_lookup_table_entry *lte,
96 consume_data_callback_t cb,
106 path = lte->file_on_disk;
107 status = winnt_openat(NULL, path, wcslen(path),
108 FILE_READ_DATA | SYNCHRONIZE, &h);
109 if (!NT_SUCCESS(status)) {
110 set_errno_from_nt_status(status);
111 ERROR_WITH_ERRNO("\"%ls\": Can't open for reading", path);
112 return WIMLIB_ERR_OPEN;
116 bytes_remaining = size;
117 while (bytes_remaining) {
118 IO_STATUS_BLOCK iosb;
121 count = min(sizeof(buf), bytes_remaining);
123 status = (*func_NtReadFile)(h, NULL, NULL, NULL,
124 &iosb, buf, count, NULL, NULL);
125 if (!NT_SUCCESS(status) || iosb.Information != count) {
126 set_errno_from_nt_status(status);
127 ERROR_WITH_ERRNO("\"%ls\": Error reading data", path);
128 ret = WIMLIB_ERR_READ;
132 bytes_remaining -= count;
133 ret = (*cb)(buf, count, cb_ctx);
141 struct win32_encrypted_read_ctx {
142 consume_data_callback_t read_prefix_cb;
143 void *read_prefix_ctx;
149 win32_encrypted_export_cb(unsigned char *data, void *_ctx, unsigned long len)
151 struct win32_encrypted_read_ctx *ctx = _ctx;
153 size_t bytes_to_consume = min(len, ctx->bytes_remaining);
155 if (bytes_to_consume == 0)
156 return ERROR_SUCCESS;
158 ret = (*ctx->read_prefix_cb)(data, bytes_to_consume, ctx->read_prefix_ctx);
160 ctx->wimlib_err_code = ret;
161 /* Shouldn't matter what error code is returned here, as long as
162 * it isn't ERROR_SUCCESS. */
163 return ERROR_READ_FAULT;
165 ctx->bytes_remaining -= bytes_to_consume;
166 return ERROR_SUCCESS;
170 read_win32_encrypted_file_prefix(const struct wim_lookup_table_entry *lte,
172 consume_data_callback_t cb,
175 struct win32_encrypted_read_ctx export_ctx;
180 DEBUG("Reading %"PRIu64" bytes from encrypted file \"%ls\"",
181 size, lte->file_on_disk);
183 export_ctx.read_prefix_cb = cb;
184 export_ctx.read_prefix_ctx = cb_ctx;
185 export_ctx.wimlib_err_code = 0;
186 export_ctx.bytes_remaining = size;
188 err = OpenEncryptedFileRaw(lte->file_on_disk, 0, &file_ctx);
189 if (err != ERROR_SUCCESS) {
190 set_errno_from_win32_error(err);
191 ERROR_WITH_ERRNO("Failed to open encrypted file \"%ls\" "
192 "for raw read", lte->file_on_disk);
193 return WIMLIB_ERR_OPEN;
195 err = ReadEncryptedFileRaw(win32_encrypted_export_cb,
196 &export_ctx, file_ctx);
197 if (err != ERROR_SUCCESS) {
198 set_errno_from_win32_error(err);
199 ERROR_WITH_ERRNO("Failed to read encrypted file \"%ls\"",
201 ret = export_ctx.wimlib_err_code;
203 ret = WIMLIB_ERR_READ;
204 } else if (export_ctx.bytes_remaining != 0) {
205 ERROR("Only could read %"PRIu64" of %"PRIu64" bytes from "
206 "encryted file \"%ls\"",
207 size - export_ctx.bytes_remaining, size,
209 ret = WIMLIB_ERR_READ;
213 CloseEncryptedFileRaw(file_ctx);
219 FILETIME_to_u64(const FILETIME *ft)
221 return ((u64)ft->dwHighDateTime << 32) | (u64)ft->dwLowDateTime;
224 /* Load the short name of a file into a WIM dentry.
226 * If we can't read the short filename for some reason, we just ignore the error
227 * and assume the file has no short name. This shouldn't be an issue, since the
228 * short names are essentially obsolete anyway.
231 win32_get_short_name(HANDLE hFile, struct wim_dentry *dentry)
234 /* It's not any harder to just make the NtQueryInformationFile() system
235 * call ourselves, and it saves a dumb call to FindFirstFile() which of
236 * course has to create its own handle. */
238 IO_STATUS_BLOCK io_status;
239 u8 buf[128] _aligned_attribute(8);
240 const FILE_NAME_INFORMATION *info;
242 status = (*func_NtQueryInformationFile)(hFile, &io_status, buf, sizeof(buf),
243 FileAlternateNameInformation);
244 info = (const FILE_NAME_INFORMATION*)buf;
245 if (NT_SUCCESS(status) && info->FileNameLength != 0) {
246 dentry->short_name = utf16le_dupz(info->FileName,
247 info->FileNameLength);
248 if (!dentry->short_name)
249 return WIMLIB_ERR_NOMEM;
250 dentry->short_name_nbytes = info->FileNameLength;
256 win32_get_security_descriptor(HANDLE h,
257 struct wim_inode *inode,
258 struct wim_sd_set *sd_set,
259 struct win32_capture_state *state,
262 SECURITY_INFORMATION requestedInformation;
270 requestedInformation = DACL_SECURITY_INFORMATION |
271 SACL_SECURITY_INFORMATION |
272 OWNER_SECURITY_INFORMATION |
273 GROUP_SECURITY_INFORMATION;
275 bufsize = sizeof(_buf);
278 * We need the file's security descriptor in SECURITY_DESCRIPTOR_RELATIVE
279 * format, and we currently have a handle opened with as many relevant
280 * permissions as possible. At this point, on Windows there are a number of
281 * options for reading a file's security descriptor:
283 * GetFileSecurity(): This takes in a path and returns the
284 * SECURITY_DESCRIPTOR_RELATIVE. Problem: this uses an internal handle, not
285 * ours, and the handle created internally doesn't specify
286 * FILE_FLAG_BACKUP_SEMANTICS. Therefore there can be access denied errors on
287 * some files and directories, even when running as the Administrator.
289 * GetSecurityInfo(): This takes in a handle and returns the security
290 * descriptor split into a bunch of different parts. This should work, but it's
291 * dumb because we have to put the security descriptor back together again.
293 * BackupRead(): This can read the security descriptor, but this is a
294 * difficult-to-use API, probably only works as the Administrator, and the
295 * format of the returned data is not well documented.
297 * NtQuerySecurityObject(): This is exactly what we need, as it takes in a
298 * handle and returns the security descriptor in SECURITY_DESCRIPTOR_RELATIVE
299 * format. Only problem is that it's a ntdll function and therefore not
300 * officially part of the Win32 API. Oh well.
302 while (!(NT_SUCCESS(status = (*func_NtQuerySecurityObject)(h,
303 requestedInformation,
304 (PSECURITY_DESCRIPTOR)buf,
309 case STATUS_BUFFER_OVERFLOW:
310 wimlib_assert(buf == _buf);
311 buf = MALLOC(lenNeeded);
313 return WIMLIB_ERR_NOMEM;
316 case STATUS_PRIVILEGE_NOT_HELD:
317 case STATUS_ACCESS_DENIED:
318 if (add_flags & WIMLIB_ADD_FLAG_STRICT_ACLS) {
320 set_errno_from_nt_status(status);
321 ret = WIMLIB_ERR_READ;
324 if (requestedInformation & SACL_SECURITY_INFORMATION) {
325 state->num_get_sacl_priv_notheld++;
326 requestedInformation &= ~SACL_SECURITY_INFORMATION;
329 state->num_get_sd_access_denied++;
335 inode->i_security_id = sd_set_add_sd(sd_set, buf, lenNeeded);
336 if (inode->i_security_id < 0)
337 ret = WIMLIB_ERR_NOMEM;
347 win32_build_dentry_tree_recursive(struct wim_dentry **root_ret,
350 size_t full_path_nchars,
351 const wchar_t *filename,
352 size_t filename_nchars,
353 struct add_image_params *params,
354 struct win32_capture_state *state,
357 /* Reads the directory entries of directory and recursively calls
358 * win32_build_dentry_tree() on them. */
360 win32_recurse_directory(HANDLE h,
362 size_t full_path_nchars,
363 struct wim_dentry *root,
364 struct add_image_params *params,
365 struct win32_capture_state *state,
370 /* Using NtQueryDirectoryFile() we can re-use the same open handle,
371 * which we opened with FILE_FLAG_BACKUP_SEMANTICS. */
374 IO_STATUS_BLOCK io_status;
375 const size_t bufsize = 8192;
378 buf = MALLOC(bufsize);
380 return WIMLIB_ERR_NOMEM;
382 while (NT_SUCCESS(status = (*func_NtQueryDirectoryFile)(h, NULL, NULL, NULL,
383 &io_status, buf, bufsize,
384 FileNamesInformation,
385 FALSE, NULL, FALSE)))
387 const FILE_NAMES_INFORMATION *info = buf;
389 if (!(info->FileNameLength == 2 && info->FileName[0] == L'.') &&
390 !(info->FileNameLength == 4 && info->FileName[0] == L'.' &&
391 info->FileName[1] == L'.'))
394 struct wim_dentry *child;
396 p = full_path + full_path_nchars;
398 p = wmempcpy(p, info->FileName,
399 info->FileNameLength / 2);
402 ret = win32_build_dentry_tree_recursive(
407 full_path + full_path_nchars + 1,
408 info->FileNameLength / 2,
413 full_path[full_path_nchars] = L'\0';
418 dentry_add_child(root, child);
420 if (info->NextEntryOffset == 0)
422 info = (const FILE_NAMES_INFORMATION *)
423 ((const u8 *)info + info->NextEntryOffset);
427 if (status != STATUS_NO_MORE_FILES) {
428 set_errno_from_nt_status(status);
429 ERROR_WITH_ERRNO("\"%ls\": Can't read directory", full_path);
430 ret = WIMLIB_ERR_READ;
437 /* Reparse point fixup status code */
439 /* Reparse point corresponded to an absolute symbolic link or junction
440 * point that pointed outside the directory tree being captured, and
441 * therefore was excluded. */
444 /* Reparse point was not fixed as it was either a relative symbolic
445 * link, a mount point, or something else we could not understand. */
448 /* Reparse point corresponded to an absolute symbolic link or junction
449 * point that pointed inside the directory tree being captured, where
450 * the target was specified by a "full" \??\ prefixed path, and
451 * therefore was fixed to be relative to the root of the directory tree
453 RP_FIXED_FULLPATH = 0x2,
455 /* Same as RP_FIXED_FULLPATH, except the absolute link target did not
456 * have the \??\ prefix. It may have begun with a drive letter though.
458 RP_FIXED_ABSPATH = 0x4,
460 /* Either RP_FIXED_FULLPATH or RP_FIXED_ABSPATH. */
461 RP_FIXED = RP_FIXED_FULLPATH | RP_FIXED_ABSPATH,
464 /* Given the "substitute name" target of a Windows reparse point, try doing a
465 * fixup where we change it to be absolute relative to the root of the directory
466 * tree being captured.
468 * Note that this is only executed when WIMLIB_ADD_FLAG_RPFIX has been
471 * @capture_root_ino and @capture_root_dev indicate the inode number and device
472 * of the root of the directory tree being captured. They are meant to identify
473 * this directory (as an alternative to its actual path, which could potentially
474 * be reached via multiple destinations due to other symbolic links). This may
475 * not work properly on FAT, which doesn't seem to supply proper inode numbers
476 * or file IDs. However, FAT doesn't support reparse points so this function
477 * wouldn't even be called anyway.
479 static enum rp_status
480 win32_capture_maybe_rpfix_target(wchar_t *target, u16 *target_nbytes_p,
481 u64 capture_root_ino, u64 capture_root_dev,
484 u16 target_nchars = *target_nbytes_p / 2;
485 size_t stripped_chars;
486 wchar_t *orig_target;
489 ret = parse_substitute_name(target, *target_nbytes_p, rptag);
492 stripped_chars = ret;
495 target[target_nchars] = L'\0';
496 orig_target = target;
497 target = capture_fixup_absolute_symlink(target + stripped_chars,
498 capture_root_ino, capture_root_dev);
501 target_nchars = wcslen(target);
502 wmemmove(orig_target + stripped_chars, target, target_nchars + 1);
503 *target_nbytes_p = (target_nchars + stripped_chars) * sizeof(wchar_t);
504 DEBUG("Fixed reparse point (new target: \"%ls\")", orig_target);
506 return RP_FIXED_FULLPATH;
508 return RP_FIXED_ABSPATH;
511 /* Returns: `enum rp_status' value on success; negative WIMLIB_ERR_* value on
514 win32_capture_try_rpfix(u8 *rpbuf, u16 *rpbuflen_p,
515 u64 capture_root_ino, u64 capture_root_dev,
516 const wchar_t *path, struct add_image_params *params)
518 struct reparse_data rpdata;
520 enum rp_status rp_status;
522 ret = parse_reparse_data(rpbuf, *rpbuflen_p, &rpdata);
526 rp_status = win32_capture_maybe_rpfix_target(rpdata.substitute_name,
527 &rpdata.substitute_name_nbytes,
530 le32_to_cpu(*(le32*)rpbuf));
531 if (rp_status & RP_FIXED) {
532 wimlib_assert(rpdata.substitute_name_nbytes % 2 == 0);
533 utf16lechar substitute_name_copy[rpdata.substitute_name_nbytes / 2];
534 wmemcpy(substitute_name_copy, rpdata.substitute_name,
535 rpdata.substitute_name_nbytes / 2);
536 rpdata.substitute_name = substitute_name_copy;
537 rpdata.print_name = substitute_name_copy;
538 rpdata.print_name_nbytes = rpdata.substitute_name_nbytes;
539 if (rp_status == RP_FIXED_FULLPATH) {
540 /* "full path", meaning \??\ prefixed. We should not
541 * include this prefix in the print name, as it is
542 * apparently meant for the filesystem driver only. */
543 rpdata.print_name += 4;
544 rpdata.print_name_nbytes -= 8;
546 ret = make_reparse_buffer(&rpdata, rpbuf, rpbuflen_p);
552 if (rp_status == RP_EXCLUDED) {
553 /* Ignoring absolute symbolic link or junction point
554 * that points out of the tree to be captured. */
555 size_t print_name_nchars = rpdata.print_name_nbytes / 2;
556 wchar_t print_name0[print_name_nchars + 1];
557 print_name0[print_name_nchars] = L'\0';
558 wmemcpy(print_name0, rpdata.print_name, print_name_nchars);
560 params->progress.scan.cur_path = path;
561 params->progress.scan.symlink_target = print_name0;
562 do_capture_progress(params,
563 WIMLIB_SCAN_DENTRY_EXCLUDED_SYMLINK,
572 * Loads the reparse point data from a reparse point into memory, optionally
573 * fixing the targets of absolute symbolic links and junction points to be
574 * relative to the root of capture.
576 * @h: Open handle to the reparse point.
577 * @path: Path to the reparse point file.
578 * @params: Additional parameters, including whether to do reparse point fixups
580 * @rpbuf: Buffer of length at least REPARSE_POINT_MAX_SIZE bytes into which
581 * the reparse point buffer will be loaded.
582 * @rpbuflen_ret: On success, the length of the reparse point buffer in bytes
583 * is written to this location.
586 * On success, returns an `enum rp_status' value that indicates if and/or
587 * how the reparse point fixup was done.
589 * On failure, returns a negative value that is a negated WIMLIB_ERR_*
593 win32_get_reparse_data(HANDLE h, const wchar_t *path,
594 struct add_image_params *params,
595 u8 *rpbuf, u16 *rpbuflen_ret)
602 if (!DeviceIoControl(h, FSCTL_GET_REPARSE_POINT,
603 NULL, /* "Not used with this operation; set to NULL" */
604 0, /* "Not used with this operation; set to 0" */
605 rpbuf, /* "A pointer to a buffer that
606 receives the reparse point data */
607 REPARSE_POINT_MAX_SIZE, /* "The size of the output
612 set_errno_from_GetLastError();
613 return -WIMLIB_ERR_READ;
616 if (bytesReturned < 8 || bytesReturned > REPARSE_POINT_MAX_SIZE) {
618 return -WIMLIB_ERR_INVALID_REPARSE_DATA;
621 rpbuflen = bytesReturned;
622 reparse_tag = le32_to_cpu(*(le32*)rpbuf);
623 if (params->add_flags & WIMLIB_ADD_FLAG_RPFIX &&
624 (reparse_tag == WIM_IO_REPARSE_TAG_SYMLINK ||
625 reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT))
627 /* Try doing reparse point fixup */
628 ret = win32_capture_try_rpfix(rpbuf,
630 params->capture_root_ino,
631 params->capture_root_dev,
637 *rpbuflen_ret = rpbuflen;
642 win32_tally_encrypted_size_cb(unsigned char *_data, void *_size_ret,
645 *(u64*)_size_ret += len;
646 return ERROR_SUCCESS;
650 win32_get_encrypted_file_size(const wchar_t *path, u64 *size_ret)
656 err = OpenEncryptedFileRaw(path, 0, &file_ctx);
657 if (err != ERROR_SUCCESS) {
658 set_errno_from_win32_error(err);
659 ERROR_WITH_ERRNO("Failed to open encrypted file \"%ls\" "
660 "for raw read", path);
661 return WIMLIB_ERR_OPEN;
664 err = ReadEncryptedFileRaw(win32_tally_encrypted_size_cb,
666 if (err != ERROR_SUCCESS) {
667 set_errno_from_win32_error(err);
668 ERROR_WITH_ERRNO("Failed to read raw encrypted data from "
670 ret = WIMLIB_ERR_READ;
674 CloseEncryptedFileRaw(file_ctx);
679 get_data_stream_name(const wchar_t *raw_stream_name, size_t raw_stream_name_nchars,
680 const wchar_t **stream_name_ret, size_t *stream_name_nchars_ret)
682 const wchar_t *sep, *type, *end;
684 /* The stream name should be returned as :NAME:TYPE */
685 if (raw_stream_name_nchars < 1)
687 if (raw_stream_name[0] != L':')
691 raw_stream_name_nchars--;
693 end = raw_stream_name + raw_stream_name_nchars;
695 sep = wmemchr(raw_stream_name, L':', raw_stream_name_nchars);
703 if (wmemcmp(type, L"$DATA", 5))
706 *stream_name_ret = raw_stream_name;
707 *stream_name_nchars_ret = sep - raw_stream_name;
712 build_stream_path(const wchar_t *path, size_t path_nchars,
713 const wchar_t *stream_name, size_t stream_name_nchars)
715 size_t stream_path_nchars;
716 wchar_t *stream_path;
719 stream_path_nchars = path_nchars;
720 if (stream_name_nchars)
721 stream_path_nchars += 1 + stream_name_nchars;
723 stream_path = MALLOC((stream_path_nchars + 1) * sizeof(wchar_t));
725 p = wmempcpy(stream_path, path, path_nchars);
726 if (stream_name_nchars) {
728 p = wmempcpy(p, stream_name, stream_name_nchars);
736 win32_capture_stream(const wchar_t *path, size_t path_nchars,
737 const wchar_t *raw_stream_name, size_t raw_stream_name_nchars,
739 struct wim_inode *inode,
740 struct list_head *unhashed_streams)
742 const wchar_t *stream_name;
743 size_t stream_name_nchars;
744 struct wim_ads_entry *ads_entry;
745 wchar_t *stream_path;
746 struct wim_lookup_table_entry *lte;
749 /* Given the raw stream name (which is something like
750 * L":streamname:$DATA", extract just the stream name part. */
751 if (!get_data_stream_name(raw_stream_name, raw_stream_name_nchars,
752 &stream_name, &stream_name_nchars))
755 /* If this is a named stream, allocate an ADS entry. */
756 if (stream_name_nchars) {
757 ads_entry = inode_add_ads_utf16le(inode, stream_name,
758 stream_name_nchars * sizeof(wchar_t));
760 return WIMLIB_ERR_NOMEM;
765 /* If the stream is empty, no lookup table entry is needed. */
766 if (stream_size == 0)
769 /* Build the path to the stream. For unnamed streams, this is simply
770 * the path to the file. For named streams, this is the path to the
771 * file, followed by a colon, followed by the stream name. */
772 stream_path = build_stream_path(path, path_nchars,
773 stream_name, stream_name_nchars);
775 return WIMLIB_ERR_NOMEM;
777 /* Set up the lookup table entry for the stream. */
778 lte = new_lookup_table_entry();
781 return WIMLIB_ERR_NOMEM;
783 lte->file_on_disk = stream_path;
784 lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
785 lte->size = stream_size;
786 if ((inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED) && !ads_entry) {
787 /* Special case for encrypted file. */
789 /* OpenEncryptedFileRaw() expects Win32 name, not NT name. */
790 lte->file_on_disk[1] = L'\\';
791 wimlib_assert(!wmemcmp(lte->file_on_disk, L"\\\\?\\", 4));
796 ret = win32_get_encrypted_file_size(lte->file_on_disk,
799 free_lookup_table_entry(lte);
802 lte->size = encrypted_size;
803 lte->resource_location = RESOURCE_WIN32_ENCRYPTED;
807 stream_id = ads_entry->stream_id;
808 ads_entry->lte = lte;
813 add_unhashed_stream(lte, inode, stream_id, unhashed_streams);
817 /* Load information about the streams of an open file into a WIM inode.
819 * We use the NtQueryInformationFile() system call instead of FindFirstStream()
820 * and FindNextStream(). This is done for two reasons:
822 * - FindFirstStream() opens its own handle to the file or directory and
823 * apparently does so without specifying FILE_FLAG_BACKUP_SEMANTICS, thereby
824 * causing access denied errors on certain files (even when running as the
826 * - FindFirstStream() and FindNextStream() is only available on Windows Vista
827 * and later, whereas the stream support in NtQueryInformationFile() was
828 * already present in Windows XP.
831 win32_capture_streams(HANDLE *hFile_p,
834 struct wim_inode *inode,
835 struct list_head *unhashed_streams,
840 u8 _buf[8192] _aligned_attribute(8);
843 IO_STATUS_BLOCK io_status;
845 const FILE_STREAM_INFORMATION *info;
847 DEBUG("Capturing streams from \"%ls\"", path);
850 bufsize = sizeof(_buf);
852 if (!(vol_flags & FILE_NAMED_STREAMS))
855 /* Get a buffer containing the stream information. */
856 while (!NT_SUCCESS(status = (*func_NtQueryInformationFile)(*hFile_p,
860 FileStreamInformation)))
864 case STATUS_BUFFER_OVERFLOW:
870 newbuf = MALLOC(bufsize);
872 newbuf = REALLOC(buf, bufsize);
874 ret = WIMLIB_ERR_NOMEM;
880 case STATUS_NOT_IMPLEMENTED:
881 case STATUS_NOT_SUPPORTED:
882 case STATUS_INVALID_INFO_CLASS:
885 set_errno_from_nt_status(status);
886 ERROR_WITH_ERRNO("\"%ls\": Failed to query "
887 "stream information", path);
888 ret = WIMLIB_ERR_READ;
893 if (io_status.Information == 0) {
894 /* No stream information. */
899 if (inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED) {
900 /* OpenEncryptedFileRaw() seems to fail with
901 * ERROR_SHARING_VIOLATION if there are any handles opened to
903 (*func_NtClose)(*hFile_p);
904 *hFile_p = INVALID_HANDLE_VALUE;
907 /* Parse one or more stream information structures. */
908 info = (const FILE_STREAM_INFORMATION *)buf;
910 /* Capture the stream. */
911 ret = win32_capture_stream(path, path_nchars,
913 info->StreamNameLength / 2,
914 info->StreamSize.QuadPart,
915 inode, unhashed_streams);
919 if (info->NextEntryOffset == 0) {
920 /* No more stream information. */
923 /* Advance to next stream information. */
924 info = (const FILE_STREAM_INFORMATION *)
925 ((const u8 *)info + info->NextEntryOffset);
931 /* The volume does not support named streams. Only capture the unnamed
933 DEBUG("Only capturing unnamed data stream");
934 if (inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
935 FILE_ATTRIBUTE_REPARSE_POINT))
941 ret = win32_capture_stream(path, path_nchars,
942 L"::$DATA", 7, file_size,
943 inode, unhashed_streams);
945 /* Free buffer if allocated on heap. */
952 win32_build_dentry_tree_recursive(struct wim_dentry **root_ret,
955 size_t full_path_nchars,
956 const wchar_t *filename,
957 size_t filename_nchars,
958 struct add_image_params *params,
959 struct win32_capture_state *state,
962 struct wim_dentry *root = NULL;
963 struct wim_inode *inode = NULL;
964 HANDLE h = INVALID_HANDLE_VALUE;
967 BY_HANDLE_FILE_INFORMATION file_info;
972 if (should_exclude_path(full_path + params->capture_root_nchars,
973 full_path_nchars - params->capture_root_nchars,
980 status = winnt_openat(cur_dir,
981 (cur_dir ? filename : full_path),
982 (cur_dir ? filename_nchars : full_path_nchars),
984 FILE_READ_ATTRIBUTES |
986 ACCESS_SYSTEM_SECURITY |
989 if (!NT_SUCCESS(status)) {
990 set_errno_from_nt_status(status);
991 ERROR_WITH_ERRNO("\"%ls\": Can't open file", full_path);
992 ret = WIMLIB_ERR_OPEN;
996 if (!GetFileInformationByHandle(h, &file_info)) {
997 set_errno_from_GetLastError();
998 ERROR_WITH_ERRNO("\"%ls\": Can't get file information", full_path);
999 ret = WIMLIB_ERR_STAT;
1003 if (file_info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1004 rpbuf = alloca(REPARSE_POINT_MAX_SIZE);
1005 ret = win32_get_reparse_data(h, full_path,
1006 params, rpbuf, &rpbuflen);
1008 /* WIMLIB_ERR_* (inverted) */
1010 ERROR_WITH_ERRNO("\"%ls\": Can't get reparse data",
1013 } else if (ret & RP_FIXED) {
1015 } else if (ret == RP_EXCLUDED) {
1023 /* Create a WIM dentry with an associated inode, which may be shared.
1025 * However, we need to explicitly check for directories and files with
1026 * only 1 link and refuse to hard link them. This is because Windows
1027 * has a bug where it can return duplicate File IDs for files and
1028 * directories on the FAT filesystem. */
1029 ret = inode_table_new_dentry(params->inode_table,
1031 ((u64)file_info.nFileIndexHigh << 32) |
1032 (u64)file_info.nFileIndexLow,
1033 file_info.dwVolumeSerialNumber,
1034 (file_info.nNumberOfLinks <= 1 ||
1035 (file_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)),
1040 ret = win32_get_short_name(h, root);
1042 ERROR_WITH_ERRNO("\"%ls\": Can't get short name", full_path);
1046 inode = root->d_inode;
1048 if (inode->i_nlink > 1) {
1049 /* Shared inode; nothing more to do */
1054 inode->i_attributes = file_info.dwFileAttributes;
1055 inode->i_creation_time = FILETIME_to_u64(&file_info.ftCreationTime);
1056 inode->i_last_write_time = FILETIME_to_u64(&file_info.ftLastWriteTime);
1057 inode->i_last_access_time = FILETIME_to_u64(&file_info.ftLastAccessTime);
1058 inode->i_resolved = 1;
1060 params->add_flags &= ~WIMLIB_ADD_FLAG_ROOT;
1062 if (!(params->add_flags & WIMLIB_ADD_FLAG_NO_ACLS)
1063 && (vol_flags & FILE_PERSISTENT_ACLS))
1065 ret = win32_get_security_descriptor(h, inode,
1066 params->sd_set, state,
1069 ERROR_WITH_ERRNO("\"%ls\": Can't "
1070 "read security descriptor", full_path);
1075 /* Capture the unnamed data stream (only should be present for regular
1076 * files) and any alternate data streams. */
1077 ret = win32_capture_streams(&h,
1081 params->unhashed_streams,
1082 ((u64)file_info.nFileSizeHigh << 32) |
1083 file_info.nFileSizeLow,
1088 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1089 /* Reparse point: set the reparse data (which we read already)
1091 inode->i_not_rpfixed = not_rpfixed;
1092 inode->i_reparse_tag = le32_to_cpu(*(le32*)rpbuf);
1093 ret = inode_set_unnamed_stream(inode, rpbuf + 8, rpbuflen - 8,
1094 params->lookup_table);
1095 } else if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY) {
1096 /* Directory (not a reparse point) --- recurse to children */
1098 if (h == INVALID_HANDLE_VALUE) {
1099 /* Re-open handle that was closed to read raw encrypted
1101 status = winnt_openat(cur_dir,
1103 filename : full_path),
1105 filename_nchars : full_path_nchars),
1106 FILE_LIST_DIRECTORY | SYNCHRONIZE,
1108 if (!NT_SUCCESS(status)) {
1109 set_errno_from_nt_status(status);
1110 ERROR_WITH_ERRNO("\"%ls\": Can't open file",
1112 ret = WIMLIB_ERR_OPEN;
1116 ret = win32_recurse_directory(h,
1128 params->progress.scan.cur_path = full_path;
1130 do_capture_progress(params, WIMLIB_SCAN_DENTRY_OK, inode);
1132 do_capture_progress(params, WIMLIB_SCAN_DENTRY_EXCLUDED, NULL);
1134 if (h != INVALID_HANDLE_VALUE)
1139 free_dentry_tree(root, params->lookup_table);
1144 win32_do_capture_warnings(const wchar_t *path,
1145 const struct win32_capture_state *state,
1148 if (state->num_get_sacl_priv_notheld == 0 &&
1149 state->num_get_sd_access_denied == 0)
1152 WARNING("Scan of \"%ls\" complete, but with one or more warnings:", path);
1153 if (state->num_get_sacl_priv_notheld != 0) {
1154 WARNING("- Could not capture SACL (System Access Control List)\n"
1155 " on %lu files or directories.",
1156 state->num_get_sacl_priv_notheld);
1158 if (state->num_get_sd_access_denied != 0) {
1159 WARNING("- Could not capture security descriptor at all\n"
1160 " on %lu files or directories.",
1161 state->num_get_sd_access_denied);
1163 WARNING("To fully capture all security descriptors, run the program\n"
1164 " with Administrator rights.");
1167 #define WINDOWS_NT_MAX_PATH 32768
1169 /* Win32 version of capturing a directory tree */
1171 win32_build_dentry_tree(struct wim_dentry **root_ret,
1172 const wchar_t *root_disk_path,
1173 struct add_image_params *params)
1178 struct win32_capture_state state;
1182 path_nchars = wcslen(root_disk_path);
1183 if (path_nchars > WINDOWS_NT_MAX_PATH)
1184 return WIMLIB_ERR_INVALID_PARAM;
1186 ret = win32_get_file_and_vol_ids(root_disk_path,
1187 ¶ms->capture_root_ino,
1188 ¶ms->capture_root_dev);
1190 ERROR_WITH_ERRNO("Can't open %ls", root_disk_path);
1194 win32_get_vol_flags(root_disk_path, &vol_flags, NULL);
1196 /* WARNING: There is no check for overflow later when this buffer is
1197 * being used! But it's as long as the maximum path length understood
1198 * by Windows NT (which is NOT the same as MAX_PATH). */
1199 path = MALLOC((WINDOWS_NT_MAX_PATH + 1) * sizeof(wchar_t));
1201 return WIMLIB_ERR_NOMEM;
1203 /* Translate into full path */
1204 dret = GetFullPathName(root_disk_path, WINDOWS_NT_MAX_PATH - 3,
1207 if (dret == 0 || dret >= WINDOWS_NT_MAX_PATH - 3) {
1208 ERROR("Can't get full path name for \"%ls\"", root_disk_path);
1209 return WIMLIB_ERR_UNSUPPORTED;
1212 /* Add \??\ prefix */
1213 wmemcpy(path, L"\\??\\", 4);
1214 path_nchars = dret + 4;
1216 /* Strip trailing slashes. If we don't do this, we may create a path
1217 * with multiple consecutive backslashes, which for some reason causes
1218 * Windows to report that the file cannot be found. */
1219 while (path_nchars >= 2 &&
1220 path[path_nchars - 1] == L'\\' &&
1221 path[path_nchars - 2] != L':')
1223 path[--path_nchars] = L'\0';
1226 params->capture_root_nchars = path_nchars;
1228 memset(&state, 0, sizeof(state));
1229 ret = win32_build_dentry_tree_recursive(root_ret, NULL,
1230 path, path_nchars, L"", 0,
1231 params, &state, vol_flags);
1234 win32_do_capture_warnings(root_disk_path, &state, params->add_flags);
1238 #endif /* __WIN32__ */