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"
41 struct win32_capture_state {
42 unsigned long num_get_sd_access_denied;
43 unsigned long num_get_sacl_priv_notheld;
44 unsigned long num_long_path_warnings;
48 read_win32_file_prefix(const struct wim_lookup_table_entry *lte,
50 consume_data_callback_t cb,
57 HANDLE hFile = win32_open_existing_file(lte->file_on_disk,
59 if (hFile == INVALID_HANDLE_VALUE) {
60 set_errno_from_GetLastError();
61 ERROR_WITH_ERRNO("Failed to open \"%ls\"", lte->file_on_disk);
62 return WIMLIB_ERR_OPEN;
65 bytes_remaining = size;
66 while (bytes_remaining) {
67 DWORD bytesToRead, bytesRead;
69 bytesToRead = min(sizeof(buf), bytes_remaining);
70 if (!ReadFile(hFile, buf, bytesToRead, &bytesRead, NULL) ||
71 bytesRead != bytesToRead)
73 set_errno_from_GetLastError();
74 ERROR_WITH_ERRNO("Failed to read data from \"%ls\"",
76 ret = WIMLIB_ERR_READ;
79 bytes_remaining -= bytesRead;
80 ret = (*cb)(buf, bytesRead, cb_ctx);
88 struct win32_encrypted_read_ctx {
89 consume_data_callback_t read_prefix_cb;
90 void *read_prefix_ctx;
96 win32_encrypted_export_cb(unsigned char *data, void *_ctx, unsigned long len)
98 struct win32_encrypted_read_ctx *ctx = _ctx;
100 size_t bytes_to_consume = min(len, ctx->bytes_remaining);
102 if (bytes_to_consume == 0)
103 return ERROR_SUCCESS;
105 ret = (*ctx->read_prefix_cb)(data, bytes_to_consume, ctx->read_prefix_ctx);
107 ctx->wimlib_err_code = ret;
108 /* Shouldn't matter what error code is returned here, as long as
109 * it isn't ERROR_SUCCESS. */
110 return ERROR_READ_FAULT;
112 ctx->bytes_remaining -= bytes_to_consume;
113 return ERROR_SUCCESS;
117 read_win32_encrypted_file_prefix(const struct wim_lookup_table_entry *lte,
119 consume_data_callback_t cb,
122 struct win32_encrypted_read_ctx export_ctx;
127 DEBUG("Reading %"PRIu64" bytes from encrypted file \"%ls\"",
128 size, lte->file_on_disk);
130 export_ctx.read_prefix_cb = cb;
131 export_ctx.read_prefix_ctx = cb_ctx;
132 export_ctx.wimlib_err_code = 0;
133 export_ctx.bytes_remaining = size;
135 err = OpenEncryptedFileRaw(lte->file_on_disk, 0, &file_ctx);
136 if (err != ERROR_SUCCESS) {
137 set_errno_from_win32_error(err);
138 ERROR_WITH_ERRNO("Failed to open encrypted file \"%ls\" "
139 "for raw read", lte->file_on_disk);
140 return WIMLIB_ERR_OPEN;
142 err = ReadEncryptedFileRaw(win32_encrypted_export_cb,
143 &export_ctx, file_ctx);
144 if (err != ERROR_SUCCESS) {
145 set_errno_from_win32_error(err);
146 ERROR_WITH_ERRNO("Failed to read encrypted file \"%ls\"",
148 ret = export_ctx.wimlib_err_code;
150 ret = WIMLIB_ERR_READ;
151 } else if (export_ctx.bytes_remaining != 0) {
152 ERROR("Only could read %"PRIu64" of %"PRIu64" bytes from "
153 "encryted file \"%ls\"",
154 size - export_ctx.bytes_remaining, size,
156 ret = WIMLIB_ERR_READ;
160 CloseEncryptedFileRaw(file_ctx);
166 FILETIME_to_u64(const FILETIME *ft)
168 return ((u64)ft->dwHighDateTime << 32) | (u64)ft->dwLowDateTime;
171 /* Load the short name of a file into a WIM dentry.
173 * If we can't read the short filename for some reason, we just ignore the error
174 * and assume the file has no short name. This shouldn't be an issue, since the
175 * short names are essentially obsolete anyway.
178 win32_get_short_name(HANDLE hFile, struct wim_dentry *dentry)
181 /* It's not any harder to just make the NtQueryInformationFile() system
182 * call ourselves, and it saves a dumb call to FindFirstFile() which of
183 * course has to create its own handle. */
185 IO_STATUS_BLOCK io_status;
186 u8 buf[128] _aligned_attribute(8);
187 const FILE_NAME_INFORMATION *info;
189 status = (*func_NtQueryInformationFile)(hFile, &io_status, buf, sizeof(buf),
190 FileAlternateNameInformation);
191 info = (const FILE_NAME_INFORMATION*)buf;
192 if (NT_SUCCESS(status) && info->FileNameLength != 0) {
193 dentry->short_name = utf16le_dupz(info->FileName,
194 info->FileNameLength);
195 if (!dentry->short_name)
196 return WIMLIB_ERR_NOMEM;
197 dentry->short_name_nbytes = info->FileNameLength;
203 win32_get_security_descriptor(HANDLE hFile,
205 struct wim_inode *inode,
206 struct wim_sd_set *sd_set,
207 struct win32_capture_state *state,
210 SECURITY_INFORMATION requestedInformation;
218 requestedInformation = DACL_SECURITY_INFORMATION |
219 SACL_SECURITY_INFORMATION |
220 OWNER_SECURITY_INFORMATION |
221 GROUP_SECURITY_INFORMATION;
223 bufsize = sizeof(_buf);
226 * We need the file's security descriptor in SECURITY_DESCRIPTOR_RELATIVE
227 * format, and we currently have a handle opened with as many relevant
228 * permissions as possible. At this point, on Windows there are a number of
229 * options for reading a file's security descriptor:
231 * GetFileSecurity(): This takes in a path and returns the
232 * SECURITY_DESCRIPTOR_RELATIVE. Problem: this uses an internal handle, not
233 * ours, and the handle created internally doesn't specify
234 * FILE_FLAG_BACKUP_SEMANTICS. Therefore there can be access denied errors on
235 * some files and directories, even when running as the Administrator.
237 * GetSecurityInfo(): This takes in a handle and returns the security
238 * descriptor split into a bunch of different parts. This should work, but it's
239 * dumb because we have to put the security descriptor back together again.
241 * BackupRead(): This can read the security descriptor, but this is a
242 * difficult-to-use API, probably only works as the Administrator, and the
243 * format of the returned data is not well documented.
245 * NtQuerySecurityObject(): This is exactly what we need, as it takes in a
246 * handle and returns the security descriptor in SECURITY_DESCRIPTOR_RELATIVE
247 * format. Only problem is that it's a ntdll function and therefore not
248 * officially part of the Win32 API. Oh well.
250 while (!(NT_SUCCESS(status = (*func_NtQuerySecurityObject)(hFile,
251 requestedInformation,
252 (PSECURITY_DESCRIPTOR)buf,
257 case STATUS_BUFFER_OVERFLOW:
258 wimlib_assert(buf == _buf);
259 buf = MALLOC(lenNeeded);
261 return WIMLIB_ERR_NOMEM;
264 case STATUS_PRIVILEGE_NOT_HELD:
265 case STATUS_ACCESS_DENIED:
266 if (add_flags & WIMLIB_ADD_FLAG_STRICT_ACLS) {
268 set_errno_from_nt_status(status);
269 ERROR_WITH_ERRNO("\"%ls\": Failed to "
270 "read security descriptor", path);
271 ret = WIMLIB_ERR_READ;
274 if (requestedInformation & SACL_SECURITY_INFORMATION) {
275 state->num_get_sacl_priv_notheld++;
276 requestedInformation &= ~SACL_SECURITY_INFORMATION;
279 state->num_get_sd_access_denied++;
285 inode->i_security_id = sd_set_add_sd(sd_set, buf, lenNeeded);
286 if (inode->i_security_id < 0)
287 ret = WIMLIB_ERR_NOMEM;
297 win32_build_dentry_tree_recursive(struct wim_dentry **root_ret,
299 size_t path_num_chars,
300 struct add_image_params *params,
301 struct win32_capture_state *state,
304 /* Reads the directory entries of directory and recursively calls
305 * win32_build_dentry_tree() on them. */
307 win32_recurse_directory(HANDLE hDir,
309 size_t dir_path_num_chars,
310 struct wim_dentry *root,
311 struct add_image_params *params,
312 struct win32_capture_state *state,
317 DEBUG("Recurse to directory \"%ls\"", dir_path);
319 /* Using NtQueryDirectoryFile() we can re-use the same open handle,
320 * which we opened with FILE_FLAG_BACKUP_SEMANTICS. */
323 IO_STATUS_BLOCK io_status;
324 const size_t bufsize = 8192;
327 buf = MALLOC(bufsize);
329 return WIMLIB_ERR_NOMEM;
331 while (NT_SUCCESS(status = (*func_NtQueryDirectoryFile)(hDir, NULL, NULL, NULL,
332 &io_status, buf, bufsize,
333 FileNamesInformation,
334 FALSE, NULL, FALSE)))
336 const FILE_NAMES_INFORMATION *info = buf;
338 if (!(info->FileNameLength == 2 && info->FileName[0] == L'.') &&
339 !(info->FileNameLength == 4 && info->FileName[0] == L'.' &&
340 info->FileName[1] == L'.'))
343 struct wim_dentry *child;
345 p = dir_path + dir_path_num_chars;
347 p = wmempcpy(p, info->FileName,
348 info->FileNameLength / 2);
351 ret = win32_build_dentry_tree_recursive(
359 dir_path[dir_path_num_chars] = L'\0';
364 dentry_add_child(root, child);
366 if (info->NextEntryOffset == 0)
368 info = (const FILE_NAMES_INFORMATION *)
369 ((const u8 *)info + info->NextEntryOffset);
373 if (status != STATUS_NO_MORE_FILES) {
374 set_errno_from_nt_status(status);
375 ERROR_WITH_ERRNO("Failed to read directory \"%ls\"", dir_path);
376 ret = WIMLIB_ERR_READ;
383 /* Reparse point fixup status code */
385 /* Reparse point corresponded to an absolute symbolic link or junction
386 * point that pointed outside the directory tree being captured, and
387 * therefore was excluded. */
390 /* Reparse point was not fixed as it was either a relative symbolic
391 * link, a mount point, or something else we could not understand. */
394 /* Reparse point corresponded to an absolute symbolic link or junction
395 * point that pointed inside the directory tree being captured, where
396 * the target was specified by a "full" \??\ prefixed path, and
397 * therefore was fixed to be relative to the root of the directory tree
399 RP_FIXED_FULLPATH = 0x2,
401 /* Same as RP_FIXED_FULLPATH, except the absolute link target did not
402 * have the \??\ prefix. It may have begun with a drive letter though.
404 RP_FIXED_ABSPATH = 0x4,
406 /* Either RP_FIXED_FULLPATH or RP_FIXED_ABSPATH. */
407 RP_FIXED = RP_FIXED_FULLPATH | RP_FIXED_ABSPATH,
410 /* Given the "substitute name" target of a Windows reparse point, try doing a
411 * fixup where we change it to be absolute relative to the root of the directory
412 * tree being captured.
414 * Note that this is only executed when WIMLIB_ADD_FLAG_RPFIX has been
417 * @capture_root_ino and @capture_root_dev indicate the inode number and device
418 * of the root of the directory tree being captured. They are meant to identify
419 * this directory (as an alternative to its actual path, which could potentially
420 * be reached via multiple destinations due to other symbolic links). This may
421 * not work properly on FAT, which doesn't seem to supply proper inode numbers
422 * or file IDs. However, FAT doesn't support reparse points so this function
423 * wouldn't even be called anyway.
425 static enum rp_status
426 win32_capture_maybe_rpfix_target(wchar_t *target, u16 *target_nbytes_p,
427 u64 capture_root_ino, u64 capture_root_dev,
430 u16 target_nchars = *target_nbytes_p / 2;
431 size_t stripped_chars;
432 wchar_t *orig_target;
435 ret = parse_substitute_name(target, *target_nbytes_p, rptag);
438 stripped_chars = ret;
441 target[target_nchars] = L'\0';
442 orig_target = target;
443 target = capture_fixup_absolute_symlink(target + stripped_chars,
444 capture_root_ino, capture_root_dev);
447 target_nchars = wcslen(target);
448 wmemmove(orig_target + stripped_chars, target, target_nchars + 1);
449 *target_nbytes_p = (target_nchars + stripped_chars) * sizeof(wchar_t);
450 DEBUG("Fixed reparse point (new target: \"%ls\")", orig_target);
452 return RP_FIXED_FULLPATH;
454 return RP_FIXED_ABSPATH;
457 /* Returns: `enum rp_status' value on success; negative WIMLIB_ERR_* value on
460 win32_capture_try_rpfix(u8 *rpbuf, u16 *rpbuflen_p,
461 u64 capture_root_ino, u64 capture_root_dev,
462 const wchar_t *path, struct add_image_params *params)
464 struct reparse_data rpdata;
466 enum rp_status rp_status;
468 ret = parse_reparse_data(rpbuf, *rpbuflen_p, &rpdata);
472 rp_status = win32_capture_maybe_rpfix_target(rpdata.substitute_name,
473 &rpdata.substitute_name_nbytes,
476 le32_to_cpu(*(le32*)rpbuf));
477 if (rp_status & RP_FIXED) {
478 wimlib_assert(rpdata.substitute_name_nbytes % 2 == 0);
479 utf16lechar substitute_name_copy[rpdata.substitute_name_nbytes / 2];
480 wmemcpy(substitute_name_copy, rpdata.substitute_name,
481 rpdata.substitute_name_nbytes / 2);
482 rpdata.substitute_name = substitute_name_copy;
483 rpdata.print_name = substitute_name_copy;
484 rpdata.print_name_nbytes = rpdata.substitute_name_nbytes;
485 if (rp_status == RP_FIXED_FULLPATH) {
486 /* "full path", meaning \??\ prefixed. We should not
487 * include this prefix in the print name, as it is
488 * apparently meant for the filesystem driver only. */
489 rpdata.print_name += 4;
490 rpdata.print_name_nbytes -= 8;
492 ret = make_reparse_buffer(&rpdata, rpbuf, rpbuflen_p);
498 if (rp_status == RP_EXCLUDED) {
499 /* Ignoring absolute symbolic link or junction point
500 * that points out of the tree to be captured. */
501 size_t print_name_nchars = rpdata.print_name_nbytes / 2;
502 wchar_t print_name0[print_name_nchars + 1];
503 print_name0[print_name_nchars] = L'\0';
504 wmemcpy(print_name0, rpdata.print_name, print_name_nchars);
506 params->progress.scan.cur_path = path;
507 params->progress.scan.symlink_target = print_name0;
508 do_capture_progress(params,
509 WIMLIB_SCAN_DENTRY_EXCLUDED_SYMLINK,
518 * Loads the reparse point data from a reparse point into memory, optionally
519 * fixing the targets of absolute symbolic links and junction points to be
520 * relative to the root of capture.
522 * @hFile: Open handle to the reparse point.
523 * @path: Path to the reparse point. Used for error messages only.
524 * @params: Additional parameters, including whether to do reparse point fixups
526 * @rpbuf: Buffer of length at least REPARSE_POINT_MAX_SIZE bytes into which
527 * the reparse point buffer will be loaded.
528 * @rpbuflen_ret: On success, the length of the reparse point buffer in bytes
529 * is written to this location.
532 * On success, returns an `enum rp_status' value that indicates if and/or
533 * how the reparse point fixup was done.
535 * On failure, returns a negative value that is a negated WIMLIB_ERR_*
539 win32_get_reparse_data(HANDLE hFile, const wchar_t *path,
540 struct add_image_params *params,
541 u8 *rpbuf, u16 *rpbuflen_ret)
548 DEBUG("Loading reparse data from \"%ls\"", path);
549 if (!DeviceIoControl(hFile, FSCTL_GET_REPARSE_POINT,
550 NULL, /* "Not used with this operation; set to NULL" */
551 0, /* "Not used with this operation; set to 0" */
552 rpbuf, /* "A pointer to a buffer that
553 receives the reparse point data */
554 REPARSE_POINT_MAX_SIZE, /* "The size of the output
559 set_errno_from_GetLastError();
560 ERROR_WITH_ERRNO("Failed to get reparse data of \"%ls\"", path);
561 return -WIMLIB_ERR_READ;
563 if (bytesReturned < 8 || bytesReturned > REPARSE_POINT_MAX_SIZE) {
564 ERROR("Reparse data on \"%ls\" is invalid", path);
565 return -WIMLIB_ERR_INVALID_REPARSE_DATA;
568 rpbuflen = bytesReturned;
569 reparse_tag = le32_to_cpu(*(le32*)rpbuf);
570 if (params->add_flags & WIMLIB_ADD_FLAG_RPFIX &&
571 (reparse_tag == WIM_IO_REPARSE_TAG_SYMLINK ||
572 reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT))
574 /* Try doing reparse point fixup */
575 ret = win32_capture_try_rpfix(rpbuf,
577 params->capture_root_ino,
578 params->capture_root_dev,
584 *rpbuflen_ret = rpbuflen;
589 win32_tally_encrypted_size_cb(unsigned char *_data, void *_size_ret,
592 *(u64*)_size_ret += len;
593 return ERROR_SUCCESS;
597 win32_get_encrypted_file_size(const wchar_t *path, u64 *size_ret)
603 err = OpenEncryptedFileRaw(path, 0, &file_ctx);
604 if (err != ERROR_SUCCESS) {
605 set_errno_from_win32_error(err);
606 ERROR_WITH_ERRNO("Failed to open encrypted file \"%ls\" "
607 "for raw read", path);
608 return WIMLIB_ERR_OPEN;
611 err = ReadEncryptedFileRaw(win32_tally_encrypted_size_cb,
613 if (err != ERROR_SUCCESS) {
614 set_errno_from_win32_error(err);
615 ERROR_WITH_ERRNO("Failed to read raw encrypted data from "
617 ret = WIMLIB_ERR_READ;
621 CloseEncryptedFileRaw(file_ctx);
625 /* Scans an unnamed or named stream of a Win32 file (not a reparse point
626 * stream); calculates its SHA1 message digest and either creates a `struct
627 * wim_lookup_table_entry' in memory for it, or uses an existing 'struct
628 * wim_lookup_table_entry' for an identical stream.
630 * @path: Path to the file (UTF-16LE).
632 * @path_num_chars: Number of 2-byte characters in @path.
634 * @inode: WIM inode to save the stream into.
636 * @unhashed_streams: List of unhashed streams that have been added to the WIM
639 * @dat: A `WIN32_FIND_STREAM_DATA' structure that specifies the
642 * Returns 0 on success; nonzero on failure.
645 win32_capture_stream(const wchar_t *path,
646 size_t path_num_chars,
647 struct wim_inode *inode,
648 struct list_head *unhashed_streams,
649 WIN32_FIND_STREAM_DATA *dat)
651 struct wim_ads_entry *ads_entry;
652 struct wim_lookup_table_entry *lte;
654 wchar_t *stream_name, *colon;
655 size_t stream_name_nchars;
656 bool is_named_stream;
659 size_t spath_buf_nbytes;
660 const wchar_t *relpath_prefix;
661 const wchar_t *colonchar;
663 DEBUG("Capture \"%ls\" stream \"%ls\"", path, dat->cStreamName);
665 /* The stream name should be returned as :NAME:TYPE */
666 stream_name = dat->cStreamName;
667 if (*stream_name != L':')
668 goto out_invalid_stream_name;
670 colon = wcschr(stream_name, L':');
672 goto out_invalid_stream_name;
674 if (wcscmp(colon + 1, L"$DATA")) {
675 /* Not a DATA stream */
682 stream_name_nchars = colon - stream_name;
683 is_named_stream = (stream_name_nchars != 0);
685 if (is_named_stream) {
686 /* Allocate an ADS entry for the named stream. */
687 ads_entry = inode_add_ads_utf16le(inode, stream_name,
688 stream_name_nchars * sizeof(wchar_t));
690 ret = WIMLIB_ERR_NOMEM;
695 /* If zero length stream, no lookup table entry needed. */
696 if ((u64)dat->StreamSize.QuadPart == 0) {
701 /* Create a UTF-16LE string @spath that gives the filename, then a
702 * colon, then the stream name. Or, if it's an unnamed stream, just the
703 * filename. It is MALLOC()'ed so that it can be saved in the
704 * wim_lookup_table_entry if needed.
706 * As yet another special case, relative paths need to be changed to
707 * begin with an explicit "./" so that, for example, a file t:ads, where
708 * :ads is the part we added, is not interpreted as a file on the t:
710 spath_nchars = path_num_chars;
711 relpath_prefix = L"";
713 if (is_named_stream) {
714 spath_nchars += 1 + stream_name_nchars;
716 if (path_num_chars == 1 && !is_any_path_separator(path[0])) {
718 static const wchar_t _relpath_prefix[] =
719 {L'.', OS_PREFERRED_PATH_SEPARATOR, L'\0'};
720 relpath_prefix = _relpath_prefix;
724 spath_buf_nbytes = (spath_nchars + 1) * sizeof(wchar_t);
725 spath = MALLOC(spath_buf_nbytes);
727 tsprintf(spath, L"%ls%ls%ls%ls",
728 relpath_prefix, path, colonchar, stream_name);
730 /* Make a new wim_lookup_table_entry */
731 lte = new_lookup_table_entry();
733 ret = WIMLIB_ERR_NOMEM;
736 lte->file_on_disk = spath;
738 if (inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED && !is_named_stream) {
740 lte->resource_location = RESOURCE_WIN32_ENCRYPTED;
741 ret = win32_get_encrypted_file_size(path, &encrypted_size);
744 lte->size = encrypted_size;
746 lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
747 lte->size = (u64)dat->StreamSize.QuadPart;
751 if (is_named_stream) {
752 stream_id = ads_entry->stream_id;
753 ads_entry->lte = lte;
758 add_unhashed_stream(lte, inode, stream_id, unhashed_streams);
764 out_invalid_stream_name:
765 ERROR("Invalid stream name: \"%ls:%ls\"", path, dat->cStreamName);
766 ret = WIMLIB_ERR_READ;
770 /* Load information about the streams of an open file into a WIM inode.
772 * We use the NtQueryInformationFile() system call instead of FindFirstStream()
773 * and FindNextStream(). This is done for two reasons:
775 * - FindFirstStream() opens its own handle to the file or directory and
776 * apparently does so without specifying FILE_FLAG_BACKUP_SEMANTICS, thereby
777 * causing access denied errors on certain files (even when running as the
779 * - FindFirstStream() and FindNextStream() is only available on Windows Vista
780 * and later, whereas the stream support in NtQueryInformationFile() was
781 * already present in Windows XP.
784 win32_capture_streams(HANDLE *hFile_p,
786 size_t path_num_chars,
787 struct wim_inode *inode,
788 struct list_head *unhashed_streams,
793 u8 _buf[8192] _aligned_attribute(8);
796 IO_STATUS_BLOCK io_status;
798 const FILE_STREAM_INFORMATION *info;
800 DEBUG("Capturing streams from \"%ls\"", path);
803 bufsize = sizeof(_buf);
805 if (!(vol_flags & FILE_NAMED_STREAMS))
808 /* Get a buffer containing the stream information. */
809 while (!NT_SUCCESS(status = (*func_NtQueryInformationFile)(*hFile_p,
813 FileStreamInformation)))
817 case STATUS_BUFFER_OVERFLOW:
823 newbuf = MALLOC(bufsize);
825 newbuf = REALLOC(buf, bufsize);
827 ret = WIMLIB_ERR_NOMEM;
833 case STATUS_NOT_IMPLEMENTED:
834 case STATUS_NOT_SUPPORTED:
835 case STATUS_INVALID_INFO_CLASS:
838 set_errno_from_nt_status(status);
839 ERROR_WITH_ERRNO("\"%ls\": Failed to query "
840 "stream information", path);
841 ret = WIMLIB_ERR_READ;
846 if (io_status.Information == 0) {
847 /* No stream information. */
852 if (inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED) {
853 /* OpenEncryptedFileRaw() seems to fail with
854 * ERROR_SHARING_VIOLATION if there are any handles opened to
856 CloseHandle(*hFile_p);
857 *hFile_p = INVALID_HANDLE_VALUE;
860 /* Parse one or more stream information structures. */
861 info = (const FILE_STREAM_INFORMATION *)buf;
863 WIN32_FIND_STREAM_DATA dat;
865 if (info->StreamNameLength <= sizeof(dat.cStreamName) - 2) {
866 dat.StreamSize = info->StreamSize;
867 memcpy(dat.cStreamName, info->StreamName, info->StreamNameLength);
868 dat.cStreamName[info->StreamNameLength / 2] = L'\0';
870 /* Capture the stream. */
871 ret = win32_capture_stream(path, path_num_chars, inode,
872 unhashed_streams, &dat);
876 if (info->NextEntryOffset == 0) {
877 /* No more stream information. */
880 /* Advance to next stream information. */
881 info = (const FILE_STREAM_INFORMATION *)
882 ((const u8 *)info + info->NextEntryOffset);
888 /* The volume does not support named streams. Only capture the unnamed
890 DEBUG("Only capturing unnamed data stream");
891 if (inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
892 FILE_ATTRIBUTE_REPARSE_POINT))
899 WIN32_FIND_STREAM_DATA dat;
901 wcscpy(dat.cStreamName, L"::$DATA");
902 dat.StreamSize.QuadPart = file_size;
903 ret = win32_capture_stream(path, path_num_chars,
904 inode, unhashed_streams, &dat);
907 /* Free buffer if allocated on heap. */
914 win32_build_dentry_tree_recursive(struct wim_dentry **root_ret,
916 size_t path_num_chars,
917 struct add_image_params *params,
918 struct win32_capture_state *state,
921 struct wim_dentry *root = NULL;
922 struct wim_inode *inode = NULL;
929 HANDLE hFile = INVALID_HANDLE_VALUE;
933 if (should_exclude_path(path + params->capture_root_nchars,
934 path_num_chars - params->capture_root_nchars,
941 desiredAccess = FILE_READ_DATA | FILE_READ_ATTRIBUTES |
942 READ_CONTROL | ACCESS_SYSTEM_SECURITY;
944 hFile = win32_open_existing_file(path, desiredAccess);
945 if (hFile == INVALID_HANDLE_VALUE) {
946 err = GetLastError();
947 if (err == ERROR_ACCESS_DENIED || err == ERROR_PRIVILEGE_NOT_HELD) {
948 if (desiredAccess & ACCESS_SYSTEM_SECURITY) {
949 desiredAccess &= ~ACCESS_SYSTEM_SECURITY;
952 if (desiredAccess & READ_CONTROL) {
953 desiredAccess &= ~READ_CONTROL;
957 set_errno_from_GetLastError();
958 ERROR_WITH_ERRNO("Failed to open \"%ls\" for reading", path);
959 ret = WIMLIB_ERR_OPEN;
963 BY_HANDLE_FILE_INFORMATION file_info;
964 if (!GetFileInformationByHandle(hFile, &file_info)) {
965 set_errno_from_GetLastError();
966 ERROR_WITH_ERRNO("Failed to get file information for \"%ls\"",
968 ret = WIMLIB_ERR_STAT;
972 if (file_info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
973 rpbuf = alloca(REPARSE_POINT_MAX_SIZE);
974 ret = win32_get_reparse_data(hFile, path, params,
977 /* WIMLIB_ERR_* (inverted) */
980 } else if (ret & RP_FIXED) {
982 } else if (ret == RP_EXCLUDED) {
990 /* Create a WIM dentry with an associated inode, which may be shared.
992 * However, we need to explicitly check for directories and files with
993 * only 1 link and refuse to hard link them. This is because Windows
994 * has a bug where it can return duplicate File IDs for files and
995 * directories on the FAT filesystem. */
996 ret = inode_table_new_dentry(params->inode_table,
997 path_basename_with_len(path, path_num_chars),
998 ((u64)file_info.nFileIndexHigh << 32) |
999 (u64)file_info.nFileIndexLow,
1000 file_info.dwVolumeSerialNumber,
1001 (file_info.nNumberOfLinks <= 1 ||
1002 (file_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)),
1007 ret = win32_get_short_name(hFile, root);
1011 inode = root->d_inode;
1013 if (inode->i_nlink > 1) {
1014 /* Shared inode; nothing more to do */
1018 inode->i_attributes = file_info.dwFileAttributes;
1019 inode->i_creation_time = FILETIME_to_u64(&file_info.ftCreationTime);
1020 inode->i_last_write_time = FILETIME_to_u64(&file_info.ftLastWriteTime);
1021 inode->i_last_access_time = FILETIME_to_u64(&file_info.ftLastAccessTime);
1022 inode->i_resolved = 1;
1024 params->add_flags &= ~WIMLIB_ADD_FLAG_ROOT;
1026 if (!(params->add_flags & WIMLIB_ADD_FLAG_NO_ACLS)
1027 && (vol_flags & FILE_PERSISTENT_ACLS))
1029 ret = win32_get_security_descriptor(hFile, path, inode,
1030 params->sd_set, state,
1036 file_size = ((u64)file_info.nFileSizeHigh << 32) |
1037 (u64)file_info.nFileSizeLow;
1040 /* Capture the unnamed data stream (only should be present for regular
1041 * files) and any alternate data streams. */
1042 ret = win32_capture_streams(&hFile,
1046 params->unhashed_streams,
1052 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1053 /* Reparse point: set the reparse data (which we read already)
1055 inode->i_not_rpfixed = not_rpfixed;
1056 inode->i_reparse_tag = le32_to_cpu(*(le32*)rpbuf);
1057 ret = inode_set_unnamed_stream(inode, rpbuf + 8, rpbuflen - 8,
1058 params->lookup_table);
1059 } else if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY) {
1060 /* Directory (not a reparse point) --- recurse to children */
1062 if (hFile == INVALID_HANDLE_VALUE) {
1063 /* Re-open handle that was closed to read raw encrypted
1065 hFile = win32_open_existing_file(path, FILE_READ_DATA);
1066 if (hFile == INVALID_HANDLE_VALUE) {
1067 set_errno_from_GetLastError();
1068 ERROR_WITH_ERRNO("Failed to reopen \"%ls\"",
1070 ret = WIMLIB_ERR_OPEN;
1074 ret = win32_recurse_directory(hFile,
1085 path[path_num_chars] = '\0';
1087 params->progress.scan.cur_path = path;
1089 do_capture_progress(params, WIMLIB_SCAN_DENTRY_EXCLUDED, NULL);
1091 do_capture_progress(params, WIMLIB_SCAN_DENTRY_OK, inode);
1093 if (hFile != INVALID_HANDLE_VALUE)
1098 free_dentry_tree(root, params->lookup_table);
1103 win32_do_capture_warnings(const wchar_t *path,
1104 const struct win32_capture_state *state,
1107 if (state->num_get_sacl_priv_notheld == 0 &&
1108 state->num_get_sd_access_denied == 0)
1111 WARNING("Scan of \"%ls\" complete, but with one or more warnings:", path);
1112 if (state->num_get_sacl_priv_notheld != 0) {
1113 WARNING("- Could not capture SACL (System Access Control List)\n"
1114 " on %lu files or directories.",
1115 state->num_get_sacl_priv_notheld);
1117 if (state->num_get_sd_access_denied != 0) {
1118 WARNING("- Could not capture security descriptor at all\n"
1119 " on %lu files or directories.",
1120 state->num_get_sd_access_denied);
1122 WARNING("To fully capture all security descriptors, run the program\n"
1123 " with Administrator rights.");
1126 #define WINDOWS_NT_MAX_PATH 32768
1128 /* Win32 version of capturing a directory tree */
1130 win32_build_dentry_tree(struct wim_dentry **root_ret,
1131 const wchar_t *root_disk_path,
1132 struct add_image_params *params)
1137 struct win32_capture_state state;
1141 path_nchars = wcslen(root_disk_path);
1142 if (path_nchars > WINDOWS_NT_MAX_PATH)
1143 return WIMLIB_ERR_INVALID_PARAM;
1145 ret = win32_get_file_and_vol_ids(root_disk_path,
1146 ¶ms->capture_root_ino,
1147 ¶ms->capture_root_dev);
1149 ERROR_WITH_ERRNO("Can't open %ls", root_disk_path);
1153 win32_get_vol_flags(root_disk_path, &vol_flags, NULL);
1155 /* WARNING: There is no check for overflow later when this buffer is
1156 * being used! But it's as long as the maximum path length understood
1157 * by Windows NT (which is NOT the same as MAX_PATH). */
1158 path = MALLOC((WINDOWS_NT_MAX_PATH + 1) * sizeof(wchar_t));
1160 return WIMLIB_ERR_NOMEM;
1162 /* Work around defective behavior in Windows where paths longer than 260
1163 * characters are not supported by default; instead they need to be
1164 * turned into absolute paths and prefixed with "\\?\". */
1166 if (wcsncmp(root_disk_path, L"\\\\?\\", 4)) {
1167 dret = GetFullPathName(root_disk_path, WINDOWS_NT_MAX_PATH - 3,
1170 if (dret == 0 || dret >= WINDOWS_NT_MAX_PATH - 3) {
1171 WARNING("Can't get full path name for \"%ls\"", root_disk_path);
1172 wmemcpy(path, root_disk_path, path_nchars + 1);
1174 wmemcpy(path, L"\\\\?\\", 4);
1175 path_nchars = 4 + dret;
1178 wmemcpy(path, root_disk_path, path_nchars + 1);
1181 /* Strip trailing slashes. If we don't do this, we may create a path
1182 * with multiple consecutive backslashes, which for some reason causes
1183 * Windows to report that the file cannot be found. */
1184 while (path_nchars >= 2 &&
1185 path[path_nchars - 1] == L'\\' &&
1186 path[path_nchars - 2] != L':')
1188 path[--path_nchars] = L'\0';
1191 params->capture_root_nchars = path_nchars;
1193 memset(&state, 0, sizeof(state));
1194 ret = win32_build_dentry_tree_recursive(root_ret, path,
1195 path_nchars, params,
1199 win32_do_capture_warnings(root_disk_path, &state, params->add_flags);
1203 #endif /* __WIN32__ */