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 HANDLE hFile = win32_open_existing_file(lte->file_on_disk,
69 if (hFile == INVALID_HANDLE_VALUE) {
70 set_errno_from_GetLastError();
71 ERROR_WITH_ERRNO("Failed to open \"%ls\"", lte->file_on_disk);
72 return WIMLIB_ERR_OPEN;
75 bytes_remaining = size;
76 while (bytes_remaining) {
77 DWORD bytesToRead, bytesRead;
79 bytesToRead = min(sizeof(buf), bytes_remaining);
80 if (!ReadFile(hFile, buf, bytesToRead, &bytesRead, NULL) ||
81 bytesRead != bytesToRead)
83 set_errno_from_GetLastError();
84 ERROR_WITH_ERRNO("Failed to read data from \"%ls\"",
86 ret = WIMLIB_ERR_READ;
89 bytes_remaining -= bytesRead;
90 ret = (*cb)(buf, bytesRead, cb_ctx);
99 struct win32_encrypted_read_ctx {
100 consume_data_callback_t read_prefix_cb;
101 void *read_prefix_ctx;
107 win32_encrypted_export_cb(unsigned char *data, void *_ctx, unsigned long len)
109 struct win32_encrypted_read_ctx *ctx = _ctx;
111 size_t bytes_to_consume = min(len, ctx->bytes_remaining);
113 if (bytes_to_consume == 0)
114 return ERROR_SUCCESS;
116 ret = (*ctx->read_prefix_cb)(data, bytes_to_consume, ctx->read_prefix_ctx);
118 ctx->wimlib_err_code = ret;
119 /* Shouldn't matter what error code is returned here, as long as
120 * it isn't ERROR_SUCCESS. */
121 return ERROR_READ_FAULT;
123 ctx->bytes_remaining -= bytes_to_consume;
124 return ERROR_SUCCESS;
128 read_win32_encrypted_file_prefix(const struct wim_lookup_table_entry *lte,
130 consume_data_callback_t cb,
133 struct win32_encrypted_read_ctx export_ctx;
138 DEBUG("Reading %"PRIu64" bytes from encryted file \"%ls\"",
139 size, lte->file_on_disk);
141 export_ctx.read_prefix_cb = cb;
142 export_ctx.read_prefix_ctx = cb_ctx;
143 export_ctx.wimlib_err_code = 0;
144 export_ctx.bytes_remaining = size;
146 err = OpenEncryptedFileRaw(lte->file_on_disk, 0, &file_ctx);
147 if (err != ERROR_SUCCESS) {
148 set_errno_from_win32_error(err);
149 ERROR_WITH_ERRNO("Failed to open encrypted file \"%ls\" "
150 "for raw read", lte->file_on_disk);
151 return WIMLIB_ERR_OPEN;
153 err = ReadEncryptedFileRaw(win32_encrypted_export_cb,
154 &export_ctx, file_ctx);
155 if (err != ERROR_SUCCESS) {
156 set_errno_from_win32_error(err);
157 ERROR_WITH_ERRNO("Failed to read encrypted file \"%ls\"",
159 ret = export_ctx.wimlib_err_code;
161 ret = WIMLIB_ERR_READ;
162 } else if (export_ctx.bytes_remaining != 0) {
163 ERROR("Only could read %"PRIu64" of %"PRIu64" bytes from "
164 "encryted file \"%ls\"",
165 size - export_ctx.bytes_remaining, size,
167 ret = WIMLIB_ERR_READ;
171 CloseEncryptedFileRaw(file_ctx);
177 FILETIME_to_u64(const FILETIME *ft)
179 return ((u64)ft->dwHighDateTime << 32) | (u64)ft->dwLowDateTime;
182 /* Load the short name of a file into a WIM dentry.
184 * If we can't read the short filename for some reason, we just ignore the error
185 * and assume the file has no short name. This shouldn't be an issue, since the
186 * short names are essentially obsolete anyway.
189 win32_get_short_name(HANDLE hFile, const wchar_t *path, struct wim_dentry *dentry)
192 /* It's not any harder to just make the NtQueryInformationFile() system
193 * call ourselves, and it saves a dumb call to FindFirstFile() which of
194 * course has to create its own handle. */
196 if (func_NtQueryInformationFile) {
198 IO_STATUS_BLOCK io_status;
199 u8 buf[128] _aligned_attribute(8);
200 const FILE_NAME_INFORMATION *info;
202 status = (*func_NtQueryInformationFile)(hFile, &io_status, buf, sizeof(buf),
203 FileAlternateNameInformation);
204 info = (const FILE_NAME_INFORMATION*)buf;
205 if (status == STATUS_SUCCESS && info->FileNameLength != 0) {
206 dentry->short_name = MALLOC(info->FileNameLength + 2);
207 if (!dentry->short_name)
208 return WIMLIB_ERR_NOMEM;
209 memcpy(dentry->short_name, info->FileName,
210 info->FileNameLength);
211 dentry->short_name[info->FileNameLength / 2] = L'\0';
212 dentry->short_name_nbytes = info->FileNameLength;
218 WIN32_FIND_DATAW dat;
222 hFind = FindFirstFile(path, &dat);
223 if (hFind != INVALID_HANDLE_VALUE) {
224 if (dat.cAlternateFileName[0] != L'\0') {
225 DEBUG("\"%ls\": short name \"%ls\"", path, dat.cAlternateFileName);
226 size_t short_name_nbytes = wcslen(dat.cAlternateFileName) *
228 size_t n = short_name_nbytes + sizeof(wchar_t);
229 dentry->short_name = MALLOC(n);
230 if (dentry->short_name) {
231 memcpy(dentry->short_name, dat.cAlternateFileName, n);
232 dentry->short_name_nbytes = short_name_nbytes;
234 ret = WIMLIB_ERR_NOMEM;
243 * win32_query_security_descriptor() - Query a file's security descriptor
245 * We need the file's security descriptor in SECURITY_DESCRIPTOR_RELATIVE
246 * format, and we currently have a handle opened with as many relevant
247 * permissions as possible. At this point, on Windows there are a number of
248 * options for reading a file's security descriptor:
250 * GetFileSecurity(): This takes in a path and returns the
251 * SECURITY_DESCRIPTOR_RELATIVE. Problem: this uses an internal handle, not
252 * ours, and the handle created internally doesn't specify
253 * FILE_FLAG_BACKUP_SEMANTICS. Therefore there can be access denied errors on
254 * some files and directories, even when running as the Administrator.
256 * GetSecurityInfo(): This takes in a handle and returns the security
257 * descriptor split into a bunch of different parts. This should work, but it's
258 * dumb because we have to put the security descriptor back together again.
260 * BackupRead(): This can read the security descriptor, but this is a
261 * difficult-to-use API, probably only works as the Administrator, and the
262 * format of the returned data is not well documented.
264 * NtQuerySecurityObject(): This is exactly what we need, as it takes in a
265 * handle and returns the security descriptor in SECURITY_DESCRIPTOR_RELATIVE
266 * format. Only problem is that it's a ntdll function and therefore not
267 * officially part of the Win32 API. Oh well.
270 win32_query_security_descriptor(HANDLE hFile, const wchar_t *path,
271 SECURITY_INFORMATION requestedInformation,
272 SECURITY_DESCRIPTOR *buf,
273 DWORD bufsize, DWORD *lengthNeeded)
276 if (func_NtQuerySecurityObject) {
279 status = (*func_NtQuerySecurityObject)(hFile,
280 requestedInformation, buf,
281 bufsize, lengthNeeded);
282 /* Since it queries an already-open handle, NtQuerySecurityObject()
283 * apparently returns STATUS_ACCESS_DENIED rather than
284 * STATUS_PRIVILEGE_NOT_HELD. */
285 if (status == STATUS_ACCESS_DENIED)
286 return ERROR_PRIVILEGE_NOT_HELD;
288 return (*func_RtlNtStatusToDosError)(status);
291 if (GetFileSecurity(path, requestedInformation, buf,
292 bufsize, lengthNeeded))
293 return ERROR_SUCCESS;
295 return GetLastError();
299 win32_get_security_descriptor(HANDLE hFile,
301 struct wim_inode *inode,
302 struct wim_sd_set *sd_set,
303 struct win32_capture_state *state,
306 SECURITY_INFORMATION requestedInformation;
314 requestedInformation = DACL_SECURITY_INFORMATION |
315 SACL_SECURITY_INFORMATION |
316 OWNER_SECURITY_INFORMATION |
317 GROUP_SECURITY_INFORMATION;
319 bufsize = sizeof(_buf);
321 err = win32_query_security_descriptor(hFile, path,
322 requestedInformation,
323 (SECURITY_DESCRIPTOR*)buf,
324 bufsize, &lenNeeded);
327 goto have_descriptor;
328 case ERROR_INSUFFICIENT_BUFFER:
329 wimlib_assert(buf == _buf);
330 buf = MALLOC(lenNeeded);
332 return WIMLIB_ERR_NOMEM;
335 case ERROR_PRIVILEGE_NOT_HELD:
336 if (add_flags & WIMLIB_ADD_FLAG_STRICT_ACLS)
338 if (requestedInformation & SACL_SECURITY_INFORMATION) {
339 state->num_get_sacl_priv_notheld++;
340 requestedInformation &= ~SACL_SECURITY_INFORMATION;
344 case ERROR_ACCESS_DENIED:
345 if (add_flags & WIMLIB_ADD_FLAG_STRICT_ACLS)
347 state->num_get_sd_access_denied++;
352 set_errno_from_win32_error(err);
353 ERROR_WITH_ERRNO("Failed to read security descriptor of \"%ls\"", path);
354 ret = WIMLIB_ERR_READ;
360 inode->i_security_id = sd_set_add_sd(sd_set, buf, lenNeeded);
361 if (inode->i_security_id < 0)
362 ret = WIMLIB_ERR_NOMEM;
372 win32_build_dentry_tree_recursive(struct wim_dentry **root_ret,
374 size_t path_num_chars,
375 struct add_image_params *params,
376 struct win32_capture_state *state,
379 /* Reads the directory entries of directory and recursively calls
380 * win32_build_dentry_tree() on them. */
382 win32_recurse_directory(HANDLE hDir,
384 size_t dir_path_num_chars,
385 struct wim_dentry *root,
386 struct add_image_params *params,
387 struct win32_capture_state *state,
392 DEBUG("Recurse to directory \"%ls\"", dir_path);
394 /* Using NtQueryDirectoryFile() we can re-use the same open handle,
395 * which we opened with FILE_FLAG_BACKUP_SEMANTICS (probably not the
396 * case for the FindFirstFile() API; it's not documented). */
398 if (!func_NtQueryDirectoryFile)
399 goto use_FindFirstFile;
402 IO_STATUS_BLOCK io_status;
403 const size_t bufsize = 8192;
405 BOOL restartScan = TRUE;
406 const FILE_NAMES_INFORMATION *info;
408 buf = MALLOC(bufsize);
410 return WIMLIB_ERR_NOMEM;
412 status = (*func_NtQueryDirectoryFile)(hDir, NULL, NULL, NULL,
413 &io_status, buf, bufsize,
414 FileNamesInformation,
415 FALSE, NULL, restartScan);
417 if (status != STATUS_SUCCESS) {
418 if (status == STATUS_NO_MORE_FILES ||
419 status == STATUS_NO_MORE_ENTRIES ||
420 status == STATUS_NO_MORE_MATCHES) {
422 } else if (status == STATUS_NOT_IMPLEMENTED ||
423 status == STATUS_NOT_SUPPORTED ||
424 status == STATUS_INVALID_INFO_CLASS) {
426 goto use_FindFirstFile;
428 set_errno_from_nt_status(status);
429 ERROR_WITH_ERRNO("Failed to read directory "
430 "\"%ls\"", dir_path);
431 ret = WIMLIB_ERR_READ;
435 wimlib_assert(io_status.Information != 0);
436 info = (const FILE_NAMES_INFORMATION*)buf;
438 if (!(info->FileNameLength == 2 && info->FileName[0] == L'.') &&
439 !(info->FileNameLength == 4 && info->FileName[0] == L'.' &&
440 info->FileName[1] == L'.'))
443 struct wim_dentry *child;
445 p = dir_path + dir_path_num_chars;
447 p = wmempcpy(p, info->FileName,
448 info->FileNameLength / 2);
451 ret = win32_build_dentry_tree_recursive(
459 dir_path[dir_path_num_chars] = L'\0';
464 dentry_add_child(root, child);
466 if (info->NextEntryOffset == 0)
468 info = (const FILE_NAMES_INFORMATION*)
469 ((const u8*)info + info->NextEntryOffset);
479 WIN32_FIND_DATAW dat;
483 /* Begin reading the directory by calling FindFirstFileW. Unlike UNIX
484 * opendir(), FindFirstFileW has file globbing built into it. But this
485 * isn't what we actually want, so just add a dummy glob to get all
487 dir_path[dir_path_num_chars] = OS_PREFERRED_PATH_SEPARATOR;
488 dir_path[dir_path_num_chars + 1] = L'*';
489 dir_path[dir_path_num_chars + 2] = L'\0';
490 hFind = FindFirstFile(dir_path, &dat);
491 dir_path[dir_path_num_chars] = L'\0';
493 if (hFind == INVALID_HANDLE_VALUE) {
494 err = GetLastError();
495 if (err == ERROR_FILE_NOT_FOUND) {
498 set_errno_from_win32_error(err);
499 ERROR_WITH_ERRNO("Failed to read directory \"%ls\"",
501 return WIMLIB_ERR_READ;
506 /* Skip . and .. entries */
507 if (dat.cFileName[0] == L'.' &&
508 (dat.cFileName[1] == L'\0' ||
509 (dat.cFileName[1] == L'.' &&
510 dat.cFileName[2] == L'\0')))
512 size_t filename_len = wcslen(dat.cFileName);
514 dir_path[dir_path_num_chars] = OS_PREFERRED_PATH_SEPARATOR;
515 wmemcpy(dir_path + dir_path_num_chars + 1,
519 struct wim_dentry *child;
520 size_t path_len = dir_path_num_chars + 1 + filename_len;
521 ret = win32_build_dentry_tree_recursive(&child,
527 dir_path[dir_path_num_chars] = L'\0';
531 dentry_add_child(root, child);
532 } while (FindNextFile(hFind, &dat));
533 err = GetLastError();
534 if (err != ERROR_NO_MORE_FILES) {
535 set_errno_from_win32_error(err);
536 ERROR_WITH_ERRNO("Failed to read directory \"%ls\"", dir_path);
538 ret = WIMLIB_ERR_READ;
545 /* Reparse point fixup status code */
547 /* Reparse point corresponded to an absolute symbolic link or junction
548 * point that pointed outside the directory tree being captured, and
549 * therefore was excluded. */
552 /* Reparse point was not fixed as it was either a relative symbolic
553 * link, a mount point, or something else we could not understand. */
556 /* Reparse point corresponded to an absolute symbolic link or junction
557 * point that pointed inside the directory tree being captured, where
558 * the target was specified by a "full" \??\ prefixed path, and
559 * therefore was fixed to be relative to the root of the directory tree
561 RP_FIXED_FULLPATH = 0x2,
563 /* Same as RP_FIXED_FULLPATH, except the absolute link target did not
564 * have the \??\ prefix. It may have begun with a drive letter though.
566 RP_FIXED_ABSPATH = 0x4,
568 /* Either RP_FIXED_FULLPATH or RP_FIXED_ABSPATH. */
569 RP_FIXED = RP_FIXED_FULLPATH | RP_FIXED_ABSPATH,
572 /* Given the "substitute name" target of a Windows reparse point, try doing a
573 * fixup where we change it to be absolute relative to the root of the directory
574 * tree being captured.
576 * Note that this is only executed when WIMLIB_ADD_FLAG_RPFIX has been
579 * @capture_root_ino and @capture_root_dev indicate the inode number and device
580 * of the root of the directory tree being captured. They are meant to identify
581 * this directory (as an alternative to its actual path, which could potentially
582 * be reached via multiple destinations due to other symbolic links). This may
583 * not work properly on FAT, which doesn't seem to supply proper inode numbers
584 * or file IDs. However, FAT doesn't support reparse points so this function
585 * wouldn't even be called anyway.
587 static enum rp_status
588 win32_capture_maybe_rpfix_target(wchar_t *target, u16 *target_nbytes_p,
589 u64 capture_root_ino, u64 capture_root_dev,
592 u16 target_nchars = *target_nbytes_p / 2;
593 size_t stripped_chars;
594 wchar_t *orig_target;
597 ret = parse_substitute_name(target, *target_nbytes_p, rptag);
600 stripped_chars = ret;
603 target[target_nchars] = L'\0';
604 orig_target = target;
605 target = capture_fixup_absolute_symlink(target + stripped_chars,
606 capture_root_ino, capture_root_dev);
609 target_nchars = wcslen(target);
610 wmemmove(orig_target + stripped_chars, target, target_nchars + 1);
611 *target_nbytes_p = (target_nchars + stripped_chars) * sizeof(wchar_t);
612 DEBUG("Fixed reparse point (new target: \"%ls\")", orig_target);
614 return RP_FIXED_FULLPATH;
616 return RP_FIXED_ABSPATH;
619 /* Returns: `enum rp_status' value on success; negative WIMLIB_ERR_* value on
622 win32_capture_try_rpfix(u8 *rpbuf, u16 *rpbuflen_p,
623 u64 capture_root_ino, u64 capture_root_dev,
624 const wchar_t *path, struct add_image_params *params)
626 struct reparse_data rpdata;
628 enum rp_status rp_status;
630 ret = parse_reparse_data(rpbuf, *rpbuflen_p, &rpdata);
634 rp_status = win32_capture_maybe_rpfix_target(rpdata.substitute_name,
635 &rpdata.substitute_name_nbytes,
638 le32_to_cpu(*(le32*)rpbuf));
639 if (rp_status & RP_FIXED) {
640 wimlib_assert(rpdata.substitute_name_nbytes % 2 == 0);
641 utf16lechar substitute_name_copy[rpdata.substitute_name_nbytes / 2];
642 wmemcpy(substitute_name_copy, rpdata.substitute_name,
643 rpdata.substitute_name_nbytes / 2);
644 rpdata.substitute_name = substitute_name_copy;
645 rpdata.print_name = substitute_name_copy;
646 rpdata.print_name_nbytes = rpdata.substitute_name_nbytes;
647 if (rp_status == RP_FIXED_FULLPATH) {
648 /* "full path", meaning \??\ prefixed. We should not
649 * include this prefix in the print name, as it is
650 * apparently meant for the filesystem driver only. */
651 rpdata.print_name += 4;
652 rpdata.print_name_nbytes -= 8;
654 ret = make_reparse_buffer(&rpdata, rpbuf, rpbuflen_p);
660 if (rp_status == RP_EXCLUDED) {
661 /* Ignoring absolute symbolic link or junction point
662 * that points out of the tree to be captured. */
663 size_t print_name_nchars = rpdata.print_name_nbytes / 2;
664 wchar_t print_name0[print_name_nchars + 1];
665 print_name0[print_name_nchars] = L'\0';
666 wmemcpy(print_name0, rpdata.print_name, print_name_nchars);
668 params->progress.scan.cur_path = path;
669 params->progress.scan.symlink_target = print_name0;
670 do_capture_progress(params,
671 WIMLIB_SCAN_DENTRY_EXCLUDED_SYMLINK,
680 * Loads the reparse point data from a reparse point into memory, optionally
681 * fixing the targets of absolute symbolic links and junction points to be
682 * relative to the root of capture.
684 * @hFile: Open handle to the reparse point.
685 * @path: Path to the reparse point. Used for error messages only.
686 * @params: Additional parameters, including whether to do reparse point fixups
688 * @rpbuf: Buffer of length at least REPARSE_POINT_MAX_SIZE bytes into which
689 * the reparse point buffer will be loaded.
690 * @rpbuflen_ret: On success, the length of the reparse point buffer in bytes
691 * is written to this location.
694 * On success, returns an `enum rp_status' value that indicates if and/or
695 * how the reparse point fixup was done.
697 * On failure, returns a negative value that is a negated WIMLIB_ERR_*
701 win32_get_reparse_data(HANDLE hFile, const wchar_t *path,
702 struct add_image_params *params,
703 u8 *rpbuf, u16 *rpbuflen_ret)
710 DEBUG("Loading reparse data from \"%ls\"", path);
711 if (!DeviceIoControl(hFile, FSCTL_GET_REPARSE_POINT,
712 NULL, /* "Not used with this operation; set to NULL" */
713 0, /* "Not used with this operation; set to 0" */
714 rpbuf, /* "A pointer to a buffer that
715 receives the reparse point data */
716 REPARSE_POINT_MAX_SIZE, /* "The size of the output
721 set_errno_from_GetLastError();
722 ERROR_WITH_ERRNO("Failed to get reparse data of \"%ls\"", path);
723 return -WIMLIB_ERR_READ;
725 if (bytesReturned < 8 || bytesReturned > REPARSE_POINT_MAX_SIZE) {
726 ERROR("Reparse data on \"%ls\" is invalid", path);
727 return -WIMLIB_ERR_INVALID_REPARSE_DATA;
730 rpbuflen = bytesReturned;
731 reparse_tag = le32_to_cpu(*(le32*)rpbuf);
732 if (params->add_flags & WIMLIB_ADD_FLAG_RPFIX &&
733 (reparse_tag == WIM_IO_REPARSE_TAG_SYMLINK ||
734 reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT))
736 /* Try doing reparse point fixup */
737 ret = win32_capture_try_rpfix(rpbuf,
739 params->capture_root_ino,
740 params->capture_root_dev,
746 *rpbuflen_ret = rpbuflen;
751 win32_tally_encrypted_size_cb(unsigned char *_data, void *_size_ret,
754 *(u64*)_size_ret += len;
755 return ERROR_SUCCESS;
759 win32_get_encrypted_file_size(const wchar_t *path, u64 *size_ret)
765 err = OpenEncryptedFileRaw(path, 0, &file_ctx);
766 if (err != ERROR_SUCCESS) {
767 set_errno_from_win32_error(err);
768 ERROR_WITH_ERRNO("Failed to open encrypted file \"%ls\" "
769 "for raw read", path);
770 return WIMLIB_ERR_OPEN;
773 err = ReadEncryptedFileRaw(win32_tally_encrypted_size_cb,
775 if (err != ERROR_SUCCESS) {
776 set_errno_from_win32_error(err);
777 ERROR_WITH_ERRNO("Failed to read raw encrypted data from "
779 ret = WIMLIB_ERR_READ;
783 CloseEncryptedFileRaw(file_ctx);
787 /* Scans an unnamed or named stream of a Win32 file (not a reparse point
788 * stream); calculates its SHA1 message digest and either creates a `struct
789 * wim_lookup_table_entry' in memory for it, or uses an existing 'struct
790 * wim_lookup_table_entry' for an identical stream.
792 * @path: Path to the file (UTF-16LE).
794 * @path_num_chars: Number of 2-byte characters in @path.
796 * @inode: WIM inode to save the stream into.
798 * @lookup_table: Stream lookup table for the WIM.
800 * @dat: A `WIN32_FIND_STREAM_DATA' structure that specifies the
803 * Returns 0 on success; nonzero on failure.
806 win32_capture_stream(const wchar_t *path,
807 size_t path_num_chars,
808 struct wim_inode *inode,
809 struct wim_lookup_table *lookup_table,
810 WIN32_FIND_STREAM_DATA *dat)
812 struct wim_ads_entry *ads_entry;
813 struct wim_lookup_table_entry *lte;
815 wchar_t *stream_name, *colon;
816 size_t stream_name_nchars;
817 bool is_named_stream;
820 size_t spath_buf_nbytes;
821 const wchar_t *relpath_prefix;
822 const wchar_t *colonchar;
824 DEBUG("Capture \"%ls\" stream \"%ls\"", path, dat->cStreamName);
826 /* The stream name should be returned as :NAME:TYPE */
827 stream_name = dat->cStreamName;
828 if (*stream_name != L':')
829 goto out_invalid_stream_name;
831 colon = wcschr(stream_name, L':');
833 goto out_invalid_stream_name;
835 if (wcscmp(colon + 1, L"$DATA")) {
836 /* Not a DATA stream */
843 stream_name_nchars = colon - stream_name;
844 is_named_stream = (stream_name_nchars != 0);
846 if (is_named_stream) {
847 /* Allocate an ADS entry for the named stream. */
848 ads_entry = inode_add_ads_utf16le(inode, stream_name,
849 stream_name_nchars * sizeof(wchar_t));
851 ret = WIMLIB_ERR_NOMEM;
856 /* If zero length stream, no lookup table entry needed. */
857 if ((u64)dat->StreamSize.QuadPart == 0) {
862 /* Create a UTF-16LE string @spath that gives the filename, then a
863 * colon, then the stream name. Or, if it's an unnamed stream, just the
864 * filename. It is MALLOC()'ed so that it can be saved in the
865 * wim_lookup_table_entry if needed.
867 * As yet another special case, relative paths need to be changed to
868 * begin with an explicit "./" so that, for example, a file t:ads, where
869 * :ads is the part we added, is not interpreted as a file on the t:
871 spath_nchars = path_num_chars;
872 relpath_prefix = L"";
874 if (is_named_stream) {
875 spath_nchars += 1 + stream_name_nchars;
877 if (path_num_chars == 1 && !is_any_path_separator(path[0])) {
879 static const wchar_t _relpath_prefix[] =
880 {L'.', OS_PREFERRED_PATH_SEPARATOR, L'\0'};
881 relpath_prefix = _relpath_prefix;
885 spath_buf_nbytes = (spath_nchars + 1) * sizeof(wchar_t);
886 spath = MALLOC(spath_buf_nbytes);
888 tsprintf(spath, L"%ls%ls%ls%ls",
889 relpath_prefix, path, colonchar, stream_name);
891 /* Make a new wim_lookup_table_entry */
892 lte = new_lookup_table_entry();
894 ret = WIMLIB_ERR_NOMEM;
897 lte->file_on_disk = spath;
899 if (inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED && !is_named_stream) {
901 lte->resource_location = RESOURCE_WIN32_ENCRYPTED;
902 ret = win32_get_encrypted_file_size(path, &encrypted_size);
905 lte->size = encrypted_size;
907 lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
908 lte->size = (u64)dat->StreamSize.QuadPart;
912 if (is_named_stream) {
913 stream_id = ads_entry->stream_id;
914 ads_entry->lte = lte;
919 lookup_table_insert_unhashed(lookup_table, lte, inode, stream_id);
925 out_invalid_stream_name:
926 ERROR("Invalid stream name: \"%ls:%ls\"", path, dat->cStreamName);
927 ret = WIMLIB_ERR_READ;
931 /* Load information about the streams of an open file into a WIM inode.
933 * By default, we use the NtQueryInformationFile() system call instead of
934 * FindFirstStream() and FindNextStream(). This is done for two reasons:
936 * - FindFirstStream() opens its own handle to the file or directory and
937 * apparently does so without specifying FILE_FLAG_BACKUP_SEMANTICS, thereby
938 * causing access denied errors on certain files (even when running as the
940 * - FindFirstStream() and FindNextStream() is only available on Windows Vista
941 * and later, whereas the stream support in NtQueryInformationFile() was
942 * already present in Windows XP.
945 win32_capture_streams(HANDLE *hFile_p,
947 size_t path_num_chars,
948 struct wim_inode *inode,
949 struct wim_lookup_table *lookup_table,
953 WIN32_FIND_STREAM_DATA dat;
956 u8 _buf[8192] _aligned_attribute(8);
959 IO_STATUS_BLOCK io_status;
961 const FILE_STREAM_INFORMATION *info;
966 DEBUG("Capturing streams from \"%ls\"", path);
968 if (!(vol_flags & FILE_NAMED_STREAMS))
972 if (!func_NtQueryInformationFile)
973 goto use_FindFirstStream;
976 bufsize = sizeof(_buf);
978 /* Get a buffer containing the stream information. */
980 status = (*func_NtQueryInformationFile)(*hFile_p, &io_status,
982 FileStreamInformation);
983 if (status == STATUS_SUCCESS) {
985 } else if (status == STATUS_BUFFER_OVERFLOW) {
990 newbuf = MALLOC(bufsize);
992 newbuf = REALLOC(buf, bufsize);
995 ret = WIMLIB_ERR_NOMEM;
999 } else if (status == STATUS_NOT_IMPLEMENTED ||
1000 status == STATUS_NOT_SUPPORTED ||
1001 status == STATUS_INVALID_INFO_CLASS) {
1002 goto use_FindFirstStream;
1004 set_errno_from_nt_status(status);
1005 ERROR_WITH_ERRNO("Failed to read streams of %ls", path);
1006 ret = WIMLIB_ERR_READ;
1011 if (io_status.Information == 0) {
1012 /* No stream information. */
1017 if (inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED) {
1018 /* OpenEncryptedFileRaw() seems to fail with
1019 * ERROR_SHARING_VIOLATION if there are any handles opened to
1021 CloseHandle(*hFile_p);
1022 *hFile_p = INVALID_HANDLE_VALUE;
1025 /* Parse one or more stream information structures. */
1026 info = (const FILE_STREAM_INFORMATION*)buf;
1028 if (info->StreamNameLength <= sizeof(dat.cStreamName) - 2) {
1029 dat.StreamSize = info->StreamSize;
1030 memcpy(dat.cStreamName, info->StreamName, info->StreamNameLength);
1031 dat.cStreamName[info->StreamNameLength / 2] = L'\0';
1033 /* Capture the stream. */
1034 ret = win32_capture_stream(path, path_num_chars, inode,
1035 lookup_table, &dat);
1039 if (info->NextEntryOffset == 0) {
1040 /* No more stream information. */
1044 /* Advance to next stream information. */
1045 info = (const FILE_STREAM_INFORMATION*)
1046 ((const u8*)info + info->NextEntryOffset);
1049 /* Free buffer if allocated on heap. */
1053 #endif /* WITH_NTDLL */
1055 use_FindFirstStream:
1056 if (win32func_FindFirstStreamW == NULL)
1058 hFind = win32func_FindFirstStreamW(path, FindStreamInfoStandard, &dat, 0);
1059 if (hFind == INVALID_HANDLE_VALUE) {
1060 err = GetLastError();
1061 if (err == ERROR_CALL_NOT_IMPLEMENTED ||
1062 err == ERROR_NOT_SUPPORTED ||
1063 err == ERROR_INVALID_FUNCTION ||
1064 err == ERROR_INVALID_PARAMETER)
1067 /* Seems legal for this to return ERROR_HANDLE_EOF on reparse
1068 * points and directories */
1069 if ((inode->i_attributes &
1070 (FILE_ATTRIBUTE_REPARSE_POINT | FILE_ATTRIBUTE_DIRECTORY))
1071 && err == ERROR_HANDLE_EOF)
1073 DEBUG("ERROR_HANDLE_EOF (ok)");
1076 if (err == ERROR_ACCESS_DENIED) {
1077 WARNING("Failed to look up data streams "
1078 "of \"%ls\": Access denied!\n%ls",
1079 path, capture_access_denied_msg);
1082 set_errno_from_win32_error(err);
1083 ERROR_WITH_ERRNO("Failed to look up data streams "
1084 "of \"%ls\"", path);
1085 return WIMLIB_ERR_READ;
1090 ret = win32_capture_stream(path,
1092 inode, lookup_table,
1095 goto out_find_close;
1096 } while (win32func_FindNextStreamW(hFind, &dat));
1097 err = GetLastError();
1098 if (err != ERROR_HANDLE_EOF) {
1099 set_errno_from_win32_error(err);
1100 ERROR_WITH_ERRNO("Error reading data streams from "
1102 ret = WIMLIB_ERR_READ;
1109 /* FindFirstStream() API is not available, or the volume does not
1110 * support named streams. Only capture the unnamed data stream. */
1111 DEBUG("Only capturing unnamed data stream");
1112 if (inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
1113 FILE_ATTRIBUTE_REPARSE_POINT))
1116 wcscpy(dat.cStreamName, L"::$DATA");
1117 dat.StreamSize.QuadPart = file_size;
1118 return win32_capture_stream(path, path_num_chars,
1119 inode, lookup_table, &dat);
1123 win32_build_dentry_tree_recursive(struct wim_dentry **root_ret,
1125 size_t path_num_chars,
1126 struct add_image_params *params,
1127 struct win32_capture_state *state,
1130 struct wim_dentry *root = NULL;
1131 struct wim_inode *inode = NULL;
1138 HANDLE hFile = INVALID_HANDLE_VALUE;
1139 DWORD desiredAccess;
1142 if (exclude_path(path, path_num_chars, params->config, true)) {
1143 if (params->add_flags & WIMLIB_ADD_FLAG_ROOT) {
1144 ERROR("Cannot exclude the root directory from capture");
1145 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
1153 if (path_num_chars >= 4 &&
1154 !wmemcmp(path, L"\\\\?\\", 4) &&
1155 path_num_chars + 1 - 4 > MAX_PATH &&
1156 state->num_long_path_warnings < MAX_CAPTURE_LONG_PATH_WARNINGS)
1158 WARNING("Path \"%ls\" exceeds MAX_PATH", path);
1159 if (++state->num_long_path_warnings == MAX_CAPTURE_LONG_PATH_WARNINGS)
1160 WARNING("Suppressing further warnings about long paths.");
1164 desiredAccess = FILE_READ_DATA | FILE_READ_ATTRIBUTES |
1165 READ_CONTROL | ACCESS_SYSTEM_SECURITY;
1167 hFile = win32_open_existing_file(path, desiredAccess);
1168 if (hFile == INVALID_HANDLE_VALUE) {
1169 err = GetLastError();
1170 if (err == ERROR_ACCESS_DENIED || err == ERROR_PRIVILEGE_NOT_HELD) {
1171 if (desiredAccess & ACCESS_SYSTEM_SECURITY) {
1172 desiredAccess &= ~ACCESS_SYSTEM_SECURITY;
1175 if (desiredAccess & READ_CONTROL) {
1176 desiredAccess &= ~READ_CONTROL;
1180 set_errno_from_GetLastError();
1181 ERROR_WITH_ERRNO("Failed to open \"%ls\" for reading", path);
1182 ret = WIMLIB_ERR_OPEN;
1186 BY_HANDLE_FILE_INFORMATION file_info;
1187 if (!GetFileInformationByHandle(hFile, &file_info)) {
1188 set_errno_from_GetLastError();
1189 ERROR_WITH_ERRNO("Failed to get file information for \"%ls\"",
1191 ret = WIMLIB_ERR_STAT;
1195 if (file_info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1196 rpbuf = alloca(REPARSE_POINT_MAX_SIZE);
1197 ret = win32_get_reparse_data(hFile, path, params,
1200 /* WIMLIB_ERR_* (inverted) */
1203 } else if (ret & RP_FIXED) {
1205 } else if (ret == RP_EXCLUDED) {
1213 /* Create a WIM dentry with an associated inode, which may be shared.
1215 * However, we need to explicitly check for directories and files with
1216 * only 1 link and refuse to hard link them. This is because Windows
1217 * has a bug where it can return duplicate File IDs for files and
1218 * directories on the FAT filesystem. */
1219 ret = inode_table_new_dentry(¶ms->inode_table,
1220 path_basename_with_len(path, path_num_chars),
1221 ((u64)file_info.nFileIndexHigh << 32) |
1222 (u64)file_info.nFileIndexLow,
1223 file_info.dwVolumeSerialNumber,
1224 (file_info.nNumberOfLinks <= 1 ||
1225 (file_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)),
1230 ret = win32_get_short_name(hFile, path, root);
1234 inode = root->d_inode;
1236 if (inode->i_nlink > 1) {
1237 /* Shared inode; nothing more to do */
1241 inode->i_attributes = file_info.dwFileAttributes;
1242 inode->i_creation_time = FILETIME_to_u64(&file_info.ftCreationTime);
1243 inode->i_last_write_time = FILETIME_to_u64(&file_info.ftLastWriteTime);
1244 inode->i_last_access_time = FILETIME_to_u64(&file_info.ftLastAccessTime);
1245 inode->i_resolved = 1;
1247 params->add_flags &= ~WIMLIB_ADD_FLAG_ROOT;
1249 if (!(params->add_flags & WIMLIB_ADD_FLAG_NO_ACLS)
1250 && (vol_flags & FILE_PERSISTENT_ACLS))
1252 ret = win32_get_security_descriptor(hFile, path, inode,
1253 ¶ms->sd_set, state,
1259 file_size = ((u64)file_info.nFileSizeHigh << 32) |
1260 (u64)file_info.nFileSizeLow;
1263 /* Capture the unnamed data stream (only should be present for regular
1264 * files) and any alternate data streams. */
1265 ret = win32_capture_streams(&hFile,
1269 params->lookup_table,
1275 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1276 /* Reparse point: set the reparse data (which we read already)
1278 inode->i_not_rpfixed = not_rpfixed;
1279 inode->i_reparse_tag = le32_to_cpu(*(le32*)rpbuf);
1280 ret = inode_set_unnamed_stream(inode, rpbuf + 8, rpbuflen - 8,
1281 params->lookup_table);
1282 } else if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY) {
1283 /* Directory (not a reparse point) --- recurse to children */
1285 if (hFile == INVALID_HANDLE_VALUE) {
1286 /* Re-open handle that was closed to read raw encrypted
1288 hFile = win32_open_existing_file(path, FILE_READ_DATA);
1289 if (hFile == INVALID_HANDLE_VALUE) {
1290 set_errno_from_GetLastError();
1291 ERROR_WITH_ERRNO("Failed to reopen \"%ls\"",
1293 ret = WIMLIB_ERR_OPEN;
1297 ret = win32_recurse_directory(hFile,
1308 path[path_num_chars] = '\0';
1310 params->progress.scan.cur_path = path;
1312 do_capture_progress(params, WIMLIB_SCAN_DENTRY_EXCLUDED, NULL);
1314 do_capture_progress(params, WIMLIB_SCAN_DENTRY_OK, inode);
1316 if (hFile != INVALID_HANDLE_VALUE)
1321 free_dentry_tree(root, params->lookup_table);
1326 win32_do_capture_warnings(const wchar_t *path,
1327 const struct win32_capture_state *state,
1330 if (state->num_get_sacl_priv_notheld == 0 &&
1331 state->num_get_sd_access_denied == 0)
1334 WARNING("Scan of \"%ls\" complete, but with one or more warnings:", path);
1335 if (state->num_get_sacl_priv_notheld != 0) {
1336 WARNING("- Could not capture SACL (System Access Control List)\n"
1337 " on %lu files or directories.",
1338 state->num_get_sacl_priv_notheld);
1340 if (state->num_get_sd_access_denied != 0) {
1341 WARNING("- Could not capture security descriptor at all\n"
1342 " on %lu files or directories.",
1343 state->num_get_sd_access_denied);
1345 WARNING("To fully capture all security descriptors, run the program\n"
1346 " with Administrator rights.");
1349 #define WINDOWS_NT_MAX_PATH 32768
1351 /* Win32 version of capturing a directory tree */
1353 win32_build_dentry_tree(struct wim_dentry **root_ret,
1354 const wchar_t *root_disk_path,
1355 struct add_image_params *params)
1360 struct win32_capture_state state;
1364 if (!win32func_FindFirstStreamW
1366 && !func_NtQueryInformationFile
1370 WARNING("Running on Windows XP or earlier; "
1371 "alternate data streams will not be captured.");
1374 path_nchars = wcslen(root_disk_path);
1375 if (path_nchars > WINDOWS_NT_MAX_PATH)
1376 return WIMLIB_ERR_INVALID_PARAM;
1378 ret = win32_get_file_and_vol_ids(root_disk_path,
1379 ¶ms->capture_root_ino,
1380 ¶ms->capture_root_dev);
1382 ERROR_WITH_ERRNO("Can't open %ls", root_disk_path);
1386 win32_get_vol_flags(root_disk_path, &vol_flags, NULL);
1388 /* WARNING: There is no check for overflow later when this buffer is
1389 * being used! But it's as long as the maximum path length understood
1390 * by Windows NT (which is NOT the same as MAX_PATH). */
1391 path = MALLOC((WINDOWS_NT_MAX_PATH + 1) * sizeof(wchar_t));
1393 return WIMLIB_ERR_NOMEM;
1395 /* Work around defective behavior in Windows where paths longer than 260
1396 * characters are not supported by default; instead they need to be
1397 * turned into absolute paths and prefixed with "\\?\". */
1399 if (wcsncmp(root_disk_path, L"\\\\?\\", 4)) {
1400 dret = GetFullPathName(root_disk_path, WINDOWS_NT_MAX_PATH - 3,
1403 if (dret == 0 || dret >= WINDOWS_NT_MAX_PATH - 3) {
1404 WARNING("Can't get full path name for \"%ls\"", root_disk_path);
1405 wmemcpy(path, root_disk_path, path_nchars + 1);
1407 wmemcpy(path, L"\\\\?\\", 4);
1408 path_nchars = 4 + dret;
1411 wmemcpy(path, root_disk_path, path_nchars + 1);
1414 /* Strip trailing slashes. */
1415 while (path_nchars >= 2 &&
1416 is_any_path_separator(path[path_nchars - 1]) &&
1417 path[path_nchars - 2] != L':')
1419 path[--path_nchars] = L'\0';
1422 /* Update pattern prefix. */
1423 if (params->config != NULL)
1425 params->config->_prefix = TSTRDUP(path);
1426 params->config->_prefix_num_tchars = path_nchars;
1427 if (params->config->_prefix == NULL)
1429 ret = WIMLIB_ERR_NOMEM;
1434 memset(&state, 0, sizeof(state));
1435 ret = win32_build_dentry_tree_recursive(root_ret, path,
1436 path_nchars, params,
1438 if (params->config != NULL)
1439 FREE(params->config->_prefix);
1443 win32_do_capture_warnings(root_disk_path, &state, params->add_flags);
1447 #endif /* __WIN32__ */