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,
69 HANDLE hFile = win32_open_file_data_only(lte->file_on_disk);
70 if (hFile == INVALID_HANDLE_VALUE) {
72 ERROR("Failed to open \"%ls\"", lte->file_on_disk);
74 return WIMLIB_ERR_OPEN;
78 out_buf = alloca(WIM_CHUNK_SIZE);
82 bytes_remaining = size;
83 while (bytes_remaining) {
84 DWORD bytesToRead, bytesRead;
86 bytesToRead = min(WIM_CHUNK_SIZE, bytes_remaining);
87 if (!ReadFile(hFile, out_buf, bytesToRead, &bytesRead, NULL) ||
88 bytesRead != bytesToRead)
91 ERROR("Failed to read data from \"%ls\"", lte->file_on_disk);
93 ret = WIMLIB_ERR_READ;
96 bytes_remaining -= bytesRead;
98 ret = (*cb)(out_buf, bytesRead, ctx_or_buf);
102 out_buf += bytesRead;
109 struct win32_encrypted_read_ctx {
110 consume_data_callback_t read_prefix_cb;
111 void *read_prefix_ctx_or_buf;
119 win32_encrypted_export_cb(unsigned char *_data, void *_ctx, unsigned long len)
121 const void *data = _data;
122 struct win32_encrypted_read_ctx *ctx = _ctx;
125 DEBUG("len = %lu", len);
126 if (ctx->read_prefix_cb) {
127 /* The length of the buffer passed to the ReadEncryptedFileRaw()
128 * export callback is undocumented, so we assume it may be of
130 size_t bytes_to_buffer = min(ctx->bytes_remaining - ctx->buf_filled,
132 while (bytes_to_buffer) {
133 size_t bytes_to_copy_to_buf =
134 min(bytes_to_buffer, WIM_CHUNK_SIZE - ctx->buf_filled);
136 memcpy(ctx->buf + ctx->buf_filled, data,
137 bytes_to_copy_to_buf);
138 ctx->buf_filled += bytes_to_copy_to_buf;
139 data += bytes_to_copy_to_buf;
140 bytes_to_buffer -= bytes_to_copy_to_buf;
142 if (ctx->buf_filled == WIM_CHUNK_SIZE ||
143 ctx->buf_filled == ctx->bytes_remaining)
145 ret = (*ctx->read_prefix_cb)(ctx->buf,
147 ctx->read_prefix_ctx_or_buf);
149 ctx->wimlib_err_code = ret;
150 /* Shouldn't matter what error code is returned
151 * here, as long as it isn't ERROR_SUCCESS. */
152 return ERROR_READ_FAULT;
154 ctx->bytes_remaining -= ctx->buf_filled;
159 size_t len_to_copy = min(len, ctx->bytes_remaining);
160 ctx->read_prefix_ctx_or_buf = mempcpy(ctx->read_prefix_ctx_or_buf,
163 ctx->bytes_remaining -= len_to_copy;
165 return ERROR_SUCCESS;
169 read_win32_encrypted_file_prefix(const struct wim_lookup_table_entry *lte,
171 consume_data_callback_t cb,
175 struct win32_encrypted_read_ctx export_ctx;
180 DEBUG("Reading %"PRIu64" bytes from encryted file \"%ls\"",
181 size, lte->file_on_disk);
183 export_ctx.read_prefix_cb = cb;
184 export_ctx.read_prefix_ctx_or_buf = ctx_or_buf;
185 export_ctx.wimlib_err_code = 0;
187 export_ctx.buf = MALLOC(WIM_CHUNK_SIZE);
189 return WIMLIB_ERR_NOMEM;
191 export_ctx.buf = NULL;
193 export_ctx.buf_filled = 0;
194 export_ctx.bytes_remaining = size;
196 err = OpenEncryptedFileRawW(lte->file_on_disk, 0, &file_ctx);
197 if (err != ERROR_SUCCESS) {
198 ERROR("Failed to open encrypted file \"%ls\" for raw read",
201 ret = WIMLIB_ERR_OPEN;
204 err = ReadEncryptedFileRaw(win32_encrypted_export_cb,
205 &export_ctx, file_ctx);
206 if (err != ERROR_SUCCESS) {
207 ERROR("Failed to read encrypted file \"%ls\"",
210 ret = export_ctx.wimlib_err_code;
212 ret = WIMLIB_ERR_READ;
213 } else if (export_ctx.bytes_remaining != 0) {
214 ERROR("Only could read %"PRIu64" of %"PRIu64" bytes from "
215 "encryted file \"%ls\"",
216 size - export_ctx.bytes_remaining, size,
218 ret = WIMLIB_ERR_READ;
222 CloseEncryptedFileRaw(file_ctx);
224 FREE(export_ctx.buf);
230 FILETIME_to_u64(const FILETIME *ft)
232 return ((u64)ft->dwHighDateTime << 32) | (u64)ft->dwLowDateTime;
236 win32_get_short_name(struct wim_dentry *dentry, const wchar_t *path)
238 WIN32_FIND_DATAW dat;
242 /* If we can't read the short filename for some reason, we just ignore
243 * the error and assume the file has no short name. I don't think this
244 * should be an issue, since the short names are essentially obsolete
246 hFind = FindFirstFileW(path, &dat);
247 if (hFind != INVALID_HANDLE_VALUE) {
248 if (dat.cAlternateFileName[0] != L'\0') {
249 DEBUG("\"%ls\": short name \"%ls\"", path, dat.cAlternateFileName);
250 size_t short_name_nbytes = wcslen(dat.cAlternateFileName) *
252 size_t n = short_name_nbytes + sizeof(wchar_t);
253 dentry->short_name = MALLOC(n);
254 if (dentry->short_name) {
255 memcpy(dentry->short_name, dat.cAlternateFileName, n);
256 dentry->short_name_nbytes = short_name_nbytes;
258 ret = WIMLIB_ERR_NOMEM;
267 win32_get_security_descriptor(struct wim_dentry *dentry,
268 struct wim_sd_set *sd_set,
270 struct win32_capture_state *state,
273 SECURITY_INFORMATION requestedInformation;
279 requestedInformation = DACL_SECURITY_INFORMATION |
280 SACL_SECURITY_INFORMATION |
281 OWNER_SECURITY_INFORMATION |
282 GROUP_SECURITY_INFORMATION;
284 /* Request length of security descriptor */
285 status = GetFileSecurityW(path, requestedInformation,
286 NULL, 0, &lenNeeded);
287 err = GetLastError();
288 if (!status && err == ERROR_INSUFFICIENT_BUFFER) {
289 DWORD len = lenNeeded;
291 if (GetFileSecurityW(path, requestedInformation,
292 (PSECURITY_DESCRIPTOR)buf, len, &lenNeeded))
294 int security_id = sd_set_add_sd(sd_set, buf, len);
296 return WIMLIB_ERR_NOMEM;
298 dentry->d_inode->i_security_id = security_id;
302 err = GetLastError();
306 if (add_flags & WIMLIB_ADD_FLAG_STRICT_ACLS)
310 case ERROR_PRIVILEGE_NOT_HELD:
311 if (requestedInformation & SACL_SECURITY_INFORMATION) {
312 n = state->num_get_sacl_priv_notheld++;
313 requestedInformation &= ~SACL_SECURITY_INFORMATION;
314 if (n < MAX_GET_SACL_PRIV_NOTHELD_WARNINGS) {
316 "We don't have enough privileges to read the full security\n"
317 " descriptor of \"%ls\"!\n"
318 " Re-trying with SACL omitted.\n", path);
319 } else if (n == MAX_GET_SACL_PRIV_NOTHELD_WARNINGS) {
321 "Suppressing further privileges not held error messages when reading\n"
322 " security descriptors.");
327 case ERROR_ACCESS_DENIED:
328 n = state->num_get_sd_access_denied++;
329 if (n < MAX_GET_SD_ACCESS_DENIED_WARNINGS) {
330 WARNING("Failed to read security descriptor of \"%ls\": "
331 "Access denied!\n%ls", path, capture_access_denied_msg);
332 } else if (n == MAX_GET_SD_ACCESS_DENIED_WARNINGS) {
333 WARNING("Suppressing further access denied errors messages i"
334 "when reading security descriptors");
339 ERROR("Failed to read security descriptor of \"%ls\"", path);
341 return WIMLIB_ERR_READ;
346 win32_build_dentry_tree_recursive(struct wim_dentry **root_ret,
348 size_t path_num_chars,
349 struct add_image_params *params,
350 struct win32_capture_state *state,
353 /* Reads the directory entries of directory using a Win32 API and recursively
354 * calls win32_build_dentry_tree() on them. */
356 win32_recurse_directory(struct wim_dentry *root,
358 size_t dir_path_num_chars,
359 struct add_image_params *params,
360 struct win32_capture_state *state,
363 WIN32_FIND_DATAW dat;
368 DEBUG("Recurse to directory \"%ls\"", dir_path);
370 /* Begin reading the directory by calling FindFirstFileW. Unlike UNIX
371 * opendir(), FindFirstFileW has file globbing built into it. But this
372 * isn't what we actually want, so just add a dummy glob to get all
374 dir_path[dir_path_num_chars] = OS_PREFERRED_PATH_SEPARATOR;
375 dir_path[dir_path_num_chars + 1] = L'*';
376 dir_path[dir_path_num_chars + 2] = L'\0';
377 hFind = FindFirstFileW(dir_path, &dat);
378 dir_path[dir_path_num_chars] = L'\0';
380 if (hFind == INVALID_HANDLE_VALUE) {
381 err = GetLastError();
382 if (err == ERROR_FILE_NOT_FOUND) {
385 ERROR("Failed to read directory \"%ls\"", dir_path);
387 return WIMLIB_ERR_READ;
392 /* Skip . and .. entries */
393 if (dat.cFileName[0] == L'.' &&
394 (dat.cFileName[1] == L'\0' ||
395 (dat.cFileName[1] == L'.' &&
396 dat.cFileName[2] == L'\0')))
398 size_t filename_len = wcslen(dat.cFileName);
400 dir_path[dir_path_num_chars] = OS_PREFERRED_PATH_SEPARATOR;
401 wmemcpy(dir_path + dir_path_num_chars + 1,
405 struct wim_dentry *child;
406 size_t path_len = dir_path_num_chars + 1 + filename_len;
407 ret = win32_build_dentry_tree_recursive(&child,
413 dir_path[dir_path_num_chars] = L'\0';
417 dentry_add_child(root, child);
418 } while (FindNextFileW(hFind, &dat));
419 err = GetLastError();
420 if (err != ERROR_NO_MORE_FILES) {
421 ERROR("Failed to read directory \"%ls\"", dir_path);
424 ret = WIMLIB_ERR_READ;
431 /* Reparse point fixup status code */
433 /* Reparse point corresponded to an absolute symbolic link or junction
434 * point that pointed outside the directory tree being captured, and
435 * therefore was excluded. */
438 /* Reparse point was not fixed as it was either a relative symbolic
439 * link, a mount point, or something else we could not understand. */
442 /* Reparse point corresponded to an absolute symbolic link or junction
443 * point that pointed inside the directory tree being captured, where
444 * the target was specified by a "full" \??\ prefixed path, and
445 * therefore was fixed to be relative to the root of the directory tree
447 RP_FIXED_FULLPATH = 0x2,
449 /* Same as RP_FIXED_FULLPATH, except the absolute link target did not
450 * have the \??\ prefix. It may have begun with a drive letter though.
452 RP_FIXED_ABSPATH = 0x4,
454 /* Either RP_FIXED_FULLPATH or RP_FIXED_ABSPATH. */
455 RP_FIXED = RP_FIXED_FULLPATH | RP_FIXED_ABSPATH,
458 /* Given the "substitute name" target of a Windows reparse point, try doing a
459 * fixup where we change it to be absolute relative to the root of the directory
460 * tree being captured.
462 * Note that this is only executed when WIMLIB_ADD_FLAG_RPFIX has been
465 * @capture_root_ino and @capture_root_dev indicate the inode number and device
466 * of the root of the directory tree being captured. They are meant to identify
467 * this directory (as an alternative to its actual path, which could potentially
468 * be reached via multiple destinations due to other symbolic links). This may
469 * not work properly on FAT, which doesn't seem to supply proper inode numbers
470 * or file IDs. However, FAT doesn't support reparse points so this function
471 * wouldn't even be called anyway.
473 static enum rp_status
474 win32_capture_maybe_rpfix_target(wchar_t *target, u16 *target_nbytes_p,
475 u64 capture_root_ino, u64 capture_root_dev,
478 u16 target_nchars = *target_nbytes_p / 2;
479 size_t stripped_chars;
480 wchar_t *orig_target;
483 ret = parse_substitute_name(target, *target_nbytes_p, rptag);
486 stripped_chars = ret;
489 target[target_nchars] = L'\0';
490 orig_target = target;
491 target = capture_fixup_absolute_symlink(target + stripped_chars,
492 capture_root_ino, capture_root_dev);
495 target_nchars = wcslen(target);
496 wmemmove(orig_target + stripped_chars, target, target_nchars + 1);
497 *target_nbytes_p = (target_nchars + stripped_chars) * sizeof(wchar_t);
498 DEBUG("Fixed reparse point (new target: \"%ls\")", orig_target);
500 return RP_FIXED_FULLPATH;
502 return RP_FIXED_ABSPATH;
505 /* Returns: `enum rp_status' value on success; negative WIMLIB_ERR_* value on
508 win32_capture_try_rpfix(u8 *rpbuf, u16 *rpbuflen_p,
509 u64 capture_root_ino, u64 capture_root_dev,
512 struct reparse_data rpdata;
514 enum rp_status rp_status;
516 ret = parse_reparse_data(rpbuf, *rpbuflen_p, &rpdata);
520 rp_status = win32_capture_maybe_rpfix_target(rpdata.substitute_name,
521 &rpdata.substitute_name_nbytes,
524 le32_to_cpu(*(le32*)rpbuf));
525 if (rp_status & RP_FIXED) {
526 wimlib_assert(rpdata.substitute_name_nbytes % 2 == 0);
527 utf16lechar substitute_name_copy[rpdata.substitute_name_nbytes / 2];
528 wmemcpy(substitute_name_copy, rpdata.substitute_name,
529 rpdata.substitute_name_nbytes / 2);
530 rpdata.substitute_name = substitute_name_copy;
531 rpdata.print_name = substitute_name_copy;
532 rpdata.print_name_nbytes = rpdata.substitute_name_nbytes;
533 if (rp_status == RP_FIXED_FULLPATH) {
534 /* "full path", meaning \??\ prefixed. We should not
535 * include this prefix in the print name, as it is
536 * apparently meant for the filesystem driver only. */
537 rpdata.print_name += 4;
538 rpdata.print_name_nbytes -= 8;
540 ret = make_reparse_buffer(&rpdata, rpbuf, rpbuflen_p);
546 if (rp_status == RP_EXCLUDED) {
547 size_t print_name_nchars = rpdata.print_name_nbytes / 2;
548 wchar_t print_name0[print_name_nchars + 1];
549 print_name0[print_name_nchars] = L'\0';
550 wmemcpy(print_name0, rpdata.print_name, print_name_nchars);
551 WARNING("Ignoring %ls pointing out of capture directory:\n"
552 " \"%ls\" -> \"%ls\"\n"
553 " (Use --norpfix to capture all symbolic links "
554 "and junction points as-is)",
555 (rpdata.rptag == WIM_IO_REPARSE_TAG_SYMLINK) ?
556 L"absolute symbolic link" : L"junction point",
565 * Loads the reparse point data from a reparse point into memory, optionally
566 * fixing the targets of absolute symbolic links and junction points to be
567 * relative to the root of capture.
569 * @hFile: Open handle to the reparse point.
570 * @path: Path to the reparse point. Used for error messages only.
571 * @params: Additional parameters, including whether to do reparse point fixups
573 * @rpbuf: Buffer of length at least REPARSE_POINT_MAX_SIZE bytes into which
574 * the reparse point buffer will be loaded.
575 * @rpbuflen_ret: On success, the length of the reparse point buffer in bytes
576 * is written to this location.
579 * On success, returns an `enum rp_status' value that indicates if and/or
580 * how the reparse point fixup was done.
582 * On failure, returns a negative value that is a negated WIMLIB_ERR_*
586 win32_get_reparse_data(HANDLE hFile, const wchar_t *path,
587 struct add_image_params *params,
588 u8 *rpbuf, u16 *rpbuflen_ret)
595 DEBUG("Loading reparse data from \"%ls\"", path);
596 if (!DeviceIoControl(hFile, FSCTL_GET_REPARSE_POINT,
597 NULL, /* "Not used with this operation; set to NULL" */
598 0, /* "Not used with this operation; set to 0" */
599 rpbuf, /* "A pointer to a buffer that
600 receives the reparse point data */
601 REPARSE_POINT_MAX_SIZE, /* "The size of the output
606 DWORD err = GetLastError();
607 ERROR("Failed to get reparse data of \"%ls\"", path);
609 return -WIMLIB_ERR_READ;
611 if (bytesReturned < 8 || bytesReturned > REPARSE_POINT_MAX_SIZE) {
612 ERROR("Reparse data on \"%ls\" is invalid", path);
613 return -WIMLIB_ERR_INVALID_REPARSE_DATA;
616 rpbuflen = bytesReturned;
617 reparse_tag = le32_to_cpu(*(le32*)rpbuf);
618 if (params->add_flags & WIMLIB_ADD_FLAG_RPFIX &&
619 (reparse_tag == WIM_IO_REPARSE_TAG_SYMLINK ||
620 reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT))
622 /* Try doing reparse point fixup */
623 ret = win32_capture_try_rpfix(rpbuf,
625 params->capture_root_ino,
626 params->capture_root_dev,
631 *rpbuflen_ret = rpbuflen;
636 win32_tally_encrypted_size_cb(unsigned char *_data, void *_ctx,
640 return ERROR_SUCCESS;
644 win32_get_encrypted_file_size(const wchar_t *path, u64 *size_ret)
651 err = OpenEncryptedFileRawW(path, 0, &file_ctx);
652 if (err != ERROR_SUCCESS) {
653 ERROR("Failed to open encrypted file \"%ls\" for raw read", path);
655 return WIMLIB_ERR_OPEN;
657 err = ReadEncryptedFileRaw(win32_tally_encrypted_size_cb,
659 if (err != ERROR_SUCCESS) {
660 ERROR("Failed to read raw encrypted data from \"%ls\"", path);
662 ret = WIMLIB_ERR_READ;
666 CloseEncryptedFileRaw(file_ctx);
670 /* Scans an unnamed or named stream of a Win32 file (not a reparse point
671 * stream); calculates its SHA1 message digest and either creates a `struct
672 * wim_lookup_table_entry' in memory for it, or uses an existing 'struct
673 * wim_lookup_table_entry' for an identical stream.
675 * @path: Path to the file (UTF-16LE).
677 * @path_num_chars: Number of 2-byte characters in @path.
679 * @inode: WIM inode to save the stream into.
681 * @lookup_table: Stream lookup table for the WIM.
683 * @dat: A `WIN32_FIND_STREAM_DATA' structure that specifies the
686 * Returns 0 on success; nonzero on failure.
689 win32_capture_stream(const wchar_t *path,
690 size_t path_num_chars,
691 struct wim_inode *inode,
692 struct wim_lookup_table *lookup_table,
693 WIN32_FIND_STREAM_DATA *dat)
695 struct wim_ads_entry *ads_entry;
696 struct wim_lookup_table_entry *lte;
698 wchar_t *stream_name, *colon;
699 size_t stream_name_nchars;
700 bool is_named_stream;
703 size_t spath_buf_nbytes;
704 const wchar_t *relpath_prefix;
705 const wchar_t *colonchar;
707 DEBUG("Capture \"%ls\" stream \"%ls\"", path, dat->cStreamName);
709 /* The stream name should be returned as :NAME:TYPE */
710 stream_name = dat->cStreamName;
711 if (*stream_name != L':')
712 goto out_invalid_stream_name;
714 colon = wcschr(stream_name, L':');
716 goto out_invalid_stream_name;
718 if (wcscmp(colon + 1, L"$DATA")) {
719 /* Not a DATA stream */
726 stream_name_nchars = colon - stream_name;
727 is_named_stream = (stream_name_nchars != 0);
729 if (is_named_stream) {
730 /* Allocate an ADS entry for the named stream. */
731 ads_entry = inode_add_ads_utf16le(inode, stream_name,
732 stream_name_nchars * sizeof(wchar_t));
734 ret = WIMLIB_ERR_NOMEM;
739 /* If zero length stream, no lookup table entry needed. */
740 if ((u64)dat->StreamSize.QuadPart == 0) {
745 /* Create a UTF-16LE string @spath that gives the filename, then a
746 * colon, then the stream name. Or, if it's an unnamed stream, just the
747 * filename. It is MALLOC()'ed so that it can be saved in the
748 * wim_lookup_table_entry if needed.
750 * As yet another special case, relative paths need to be changed to
751 * begin with an explicit "./" so that, for example, a file t:ads, where
752 * :ads is the part we added, is not interpreted as a file on the t:
754 spath_nchars = path_num_chars;
755 relpath_prefix = L"";
757 if (is_named_stream) {
758 spath_nchars += 1 + stream_name_nchars;
760 if (path_num_chars == 1 && !is_any_path_separator(path[0])) {
762 static const wchar_t _relpath_prefix[] =
763 {L'.', OS_PREFERRED_PATH_SEPARATOR, L'\0'};
764 relpath_prefix = _relpath_prefix;
768 spath_buf_nbytes = (spath_nchars + 1) * sizeof(wchar_t);
769 spath = MALLOC(spath_buf_nbytes);
771 swprintf(spath, L"%ls%ls%ls%ls",
772 relpath_prefix, path, colonchar, stream_name);
774 /* Make a new wim_lookup_table_entry */
775 lte = new_lookup_table_entry();
777 ret = WIMLIB_ERR_NOMEM;
780 lte->file_on_disk = spath;
782 if (inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED && !is_named_stream) {
784 lte->resource_location = RESOURCE_WIN32_ENCRYPTED;
785 ret = win32_get_encrypted_file_size(path, &encrypted_size);
788 lte->resource_entry.original_size = encrypted_size;
790 lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
791 lte->resource_entry.original_size = (u64)dat->StreamSize.QuadPart;
795 if (is_named_stream) {
796 stream_id = ads_entry->stream_id;
797 ads_entry->lte = lte;
802 lookup_table_insert_unhashed(lookup_table, lte, inode, stream_id);
808 out_invalid_stream_name:
809 ERROR("Invalid stream name: \"%ls:%ls\"", path, dat->cStreamName);
810 ret = WIMLIB_ERR_READ;
814 /* Scans a Win32 file for unnamed and named data streams (not reparse point
817 * @path: Path to the file (UTF-16LE).
819 * @path_num_chars: Number of 2-byte characters in @path.
821 * @inode: WIM inode to save the stream into.
823 * @lookup_table: Stream lookup table for the WIM.
825 * @file_size: Size of unnamed data stream. (Used only if alternate
826 * data streams API appears to be unavailable.)
828 * @vol_flags: Flags that specify features of the volume being
831 * Returns 0 on success; nonzero on failure.
834 win32_capture_streams(const wchar_t *path,
835 size_t path_num_chars,
836 struct wim_inode *inode,
837 struct wim_lookup_table *lookup_table,
841 WIN32_FIND_STREAM_DATA dat;
846 DEBUG("Capturing streams from \"%ls\"", path);
848 if (win32func_FindFirstStreamW == NULL ||
849 !(vol_flags & FILE_NAMED_STREAMS))
852 hFind = win32func_FindFirstStreamW(path, FindStreamInfoStandard, &dat, 0);
853 if (hFind == INVALID_HANDLE_VALUE) {
854 err = GetLastError();
855 if (err == ERROR_CALL_NOT_IMPLEMENTED)
858 /* Seems legal for this to return ERROR_HANDLE_EOF on reparse
859 * points and directories */
860 if ((inode->i_attributes &
861 (FILE_ATTRIBUTE_REPARSE_POINT | FILE_ATTRIBUTE_DIRECTORY))
862 && err == ERROR_HANDLE_EOF)
864 DEBUG("ERROR_HANDLE_EOF (ok)");
867 if (err == ERROR_ACCESS_DENIED) {
868 WARNING("Failed to look up data streams "
869 "of \"%ls\": Access denied!\n%ls",
870 path, capture_access_denied_msg);
873 ERROR("Failed to look up data streams "
876 return WIMLIB_ERR_READ;
881 ret = win32_capture_stream(path,
887 } while (win32func_FindNextStreamW(hFind, &dat));
888 err = GetLastError();
889 if (err != ERROR_HANDLE_EOF) {
890 ERROR("Win32 API: Error reading data streams from \"%ls\"", path);
892 ret = WIMLIB_ERR_READ;
898 /* FindFirstStreamW() API is not available, or the volume does not
899 * support named streams. Only capture the unnamed data stream. */
900 DEBUG("Only capturing unnamed data stream");
901 if (inode->i_attributes &
902 (FILE_ATTRIBUTE_REPARSE_POINT | FILE_ATTRIBUTE_DIRECTORY))
906 /* Just create our own WIN32_FIND_STREAM_DATA for an unnamed
907 * stream to reduce the code to a call to the
908 * already-implemented win32_capture_stream() */
909 wcscpy(dat.cStreamName, L"::$DATA");
910 dat.StreamSize.QuadPart = file_size;
911 ret = win32_capture_stream(path,
920 win32_build_dentry_tree_recursive(struct wim_dentry **root_ret,
922 size_t path_num_chars,
923 struct add_image_params *params,
924 struct win32_capture_state *state,
927 struct wim_dentry *root = NULL;
928 struct wim_inode *inode;
936 params->progress.scan.cur_path = path;
938 if (exclude_path(path, path_num_chars, params->config, true)) {
939 if (params->add_flags & WIMLIB_ADD_FLAG_ROOT) {
940 ERROR("Cannot exclude the root directory from capture");
941 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
944 do_capture_progress(params, WIMLIB_SCAN_DENTRY_EXCLUDED);
950 if (path_num_chars >= 4 &&
951 !wmemcmp(path, L"\\\\?\\", 4) &&
952 path_num_chars + 1 - 4 > MAX_PATH &&
953 state->num_long_path_warnings < MAX_CAPTURE_LONG_PATH_WARNINGS)
955 WARNING("Path \"%ls\" exceeds MAX_PATH", path);
956 if (++state->num_long_path_warnings == MAX_CAPTURE_LONG_PATH_WARNINGS)
957 WARNING("Suppressing further warnings about long paths.");
961 do_capture_progress(params, WIMLIB_SCAN_DENTRY_OK);
963 HANDLE hFile = win32_open_existing_file(path,
964 FILE_READ_DATA | FILE_READ_ATTRIBUTES);
965 if (hFile == INVALID_HANDLE_VALUE) {
966 err = GetLastError();
967 ERROR("Win32 API: Failed to open \"%ls\"", path);
969 ret = WIMLIB_ERR_OPEN;
973 BY_HANDLE_FILE_INFORMATION file_info;
974 if (!GetFileInformationByHandle(hFile, &file_info)) {
975 err = GetLastError();
976 ERROR("Win32 API: Failed to get file information for \"%ls\"",
979 ret = WIMLIB_ERR_STAT;
980 goto out_close_handle;
983 if (file_info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
984 rpbuf = alloca(REPARSE_POINT_MAX_SIZE);
985 ret = win32_get_reparse_data(hFile, path, params,
988 /* WIMLIB_ERR_* (inverted) */
990 goto out_close_handle;
991 } else if (ret & RP_FIXED) {
993 } else if (ret == RP_EXCLUDED) {
995 goto out_close_handle;
1001 /* Create a WIM dentry with an associated inode, which may be shared.
1003 * However, we need to explicitly check for directories and files with
1004 * only 1 link and refuse to hard link them. This is because Windows
1005 * has a bug where it can return duplicate File IDs for files and
1006 * directories on the FAT filesystem. */
1007 ret = inode_table_new_dentry(¶ms->inode_table,
1008 path_basename_with_len(path, path_num_chars),
1009 ((u64)file_info.nFileIndexHigh << 32) |
1010 (u64)file_info.nFileIndexLow,
1011 file_info.dwVolumeSerialNumber,
1012 (file_info.nNumberOfLinks <= 1 ||
1013 (file_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)),
1016 goto out_close_handle;
1018 ret = win32_get_short_name(root, path);
1020 goto out_close_handle;
1022 inode = root->d_inode;
1024 if (inode->i_nlink > 1) /* Shared inode; nothing more to do */
1025 goto out_close_handle;
1027 inode->i_attributes = file_info.dwFileAttributes;
1028 inode->i_creation_time = FILETIME_to_u64(&file_info.ftCreationTime);
1029 inode->i_last_write_time = FILETIME_to_u64(&file_info.ftLastWriteTime);
1030 inode->i_last_access_time = FILETIME_to_u64(&file_info.ftLastAccessTime);
1031 inode->i_resolved = 1;
1033 params->add_flags &= ~WIMLIB_ADD_FLAG_ROOT;
1035 if (!(params->add_flags & WIMLIB_ADD_FLAG_NO_ACLS)
1036 && (vol_flags & FILE_PERSISTENT_ACLS))
1038 ret = win32_get_security_descriptor(root, ¶ms->sd_set,
1042 goto out_close_handle;
1045 file_size = ((u64)file_info.nFileSizeHigh << 32) |
1046 (u64)file_info.nFileSizeLow;
1050 /* Capture the unnamed data stream (only should be present for regular
1051 * files) and any alternate data streams. */
1052 ret = win32_capture_streams(path,
1055 params->lookup_table,
1061 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1062 /* Reparse point: set the reparse data (which we read already)
1064 inode->i_not_rpfixed = not_rpfixed;
1065 inode->i_reparse_tag = le32_to_cpu(*(le32*)rpbuf);
1066 ret = inode_set_unnamed_stream(inode, rpbuf + 8, rpbuflen - 8,
1067 params->lookup_table);
1068 } else if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY) {
1069 /* Directory (not a reparse point) --- recurse to children */
1070 ret = win32_recurse_directory(root,
1084 free_dentry_tree(root, params->lookup_table);
1089 win32_do_capture_warnings(const struct win32_capture_state *state,
1092 if (state->num_get_sacl_priv_notheld == 0 &&
1093 state->num_get_sd_access_denied == 0)
1097 WARNING("Built dentry tree successfully, but with the following problem(s):");
1098 if (state->num_get_sacl_priv_notheld != 0) {
1099 WARNING("Could not capture SACL (System Access Control List)\n"
1100 " on %lu files or directories.",
1101 state->num_get_sacl_priv_notheld);
1103 if (state->num_get_sd_access_denied != 0) {
1104 WARNING("Could not capture security descriptor at all\n"
1105 " on %lu files or directories.",
1106 state->num_get_sd_access_denied);
1109 "Try running the program as the Administrator to make sure all the\n"
1110 " desired metadata has been captured exactly. However, if you\n"
1111 " do not care about capturing security descriptors correctly, then\n"
1112 " nothing more needs to be done%ls\n",
1113 (add_flags & WIMLIB_ADD_FLAG_NO_ACLS) ? L"." :
1114 L", although you might consider\n"
1115 " using the --no-acls option to explicitly capture no security\n"
1119 #define WINDOWS_NT_MAX_PATH 32768
1121 /* Win32 version of capturing a directory tree */
1123 win32_build_dentry_tree(struct wim_dentry **root_ret,
1124 const wchar_t *root_disk_path,
1125 struct add_image_params *params)
1130 struct win32_capture_state state;
1133 bool need_prefix_free = false;
1135 if (!win32func_FindFirstStreamW) {
1136 WARNING("Running on Windows XP or earlier; "
1137 "alternate data streams will not be captured.");
1140 path_nchars = wcslen(root_disk_path);
1141 if (path_nchars > WINDOWS_NT_MAX_PATH)
1142 return WIMLIB_ERR_INVALID_PARAM;
1144 if (GetFileAttributesW(root_disk_path) == INVALID_FILE_ATTRIBUTES &&
1145 GetLastError() == ERROR_FILE_NOT_FOUND)
1147 ERROR("Capture directory \"%ls\" does not exist!",
1149 return WIMLIB_ERR_OPENDIR;
1152 ret = win32_get_file_and_vol_ids(root_disk_path,
1153 ¶ms->capture_root_ino,
1154 ¶ms->capture_root_dev);
1158 win32_get_vol_flags(root_disk_path, &vol_flags, NULL);
1160 /* WARNING: There is no check for overflow later when this buffer is
1161 * being used! But it's as long as the maximum path length understood
1162 * by Windows NT (which is NOT the same as MAX_PATH). */
1163 path = MALLOC(WINDOWS_NT_MAX_PATH * sizeof(wchar_t));
1165 return WIMLIB_ERR_NOMEM;
1167 /* Work around defective behavior in Windows where paths longer than 260
1168 * characters are not supported by default; instead they need to be
1169 * turned into absolute paths and prefixed with "\\?\". */
1171 if (wcsncmp(root_disk_path, L"\\\\?\\", 4)) {
1172 dret = GetFullPathName(root_disk_path, WINDOWS_NT_MAX_PATH - 4,
1175 if (dret == 0 || dret >= WINDOWS_NT_MAX_PATH - 4) {
1176 WARNING("Can't get full path name for \"%ls\"", root_disk_path);
1177 wmemcpy(path, root_disk_path, path_nchars + 1);
1179 wmemcpy(path, L"\\\\?\\", 4);
1180 path_nchars = 4 + dret;
1181 /* Update pattern prefix */
1182 if (params->config != NULL)
1184 params->config->_prefix = TSTRDUP(path);
1185 params->config->_prefix_num_tchars = path_nchars;
1186 if (params->config->_prefix == NULL)
1188 ret = WIMLIB_ERR_NOMEM;
1191 need_prefix_free = true;
1195 wmemcpy(path, root_disk_path, path_nchars + 1);
1198 memset(&state, 0, sizeof(state));
1199 ret = win32_build_dentry_tree_recursive(root_ret, path,
1200 path_nchars, params,
1202 if (need_prefix_free)
1203 FREE(params->config->_prefix);
1207 win32_do_capture_warnings(&state, params->add_flags);
1211 #endif /* __WIN32__ */