3 #include "win32_common.h"
4 #include "wimlib_internal.h"
5 #include "lookup_table.h"
7 #include "endianness.h"
9 #define MAX_GET_SD_ACCESS_DENIED_WARNINGS 1
10 #define MAX_GET_SACL_PRIV_NOTHELD_WARNINGS 1
11 struct win32_capture_state {
12 unsigned long num_get_sd_access_denied;
13 unsigned long num_get_sacl_priv_notheld;
17 static const wchar_t *capture_access_denied_msg =
18 L" If you are not running this program as the administrator, you may\n"
19 " need to do so, so that all data and metadata can be backed up.\n"
20 " Otherwise, there may be no way to access the desired data or\n"
21 " metadata without taking ownership of the file or directory.\n"
25 read_win32_file_prefix(const struct wim_lookup_table_entry *lte,
27 consume_data_callback_t cb,
36 HANDLE hFile = win32_open_file_data_only(lte->file_on_disk);
37 if (hFile == INVALID_HANDLE_VALUE) {
39 ERROR("Failed to open \"%ls\"", lte->file_on_disk);
41 return WIMLIB_ERR_OPEN;
45 out_buf = alloca(WIM_CHUNK_SIZE);
49 bytes_remaining = size;
50 while (bytes_remaining) {
51 DWORD bytesToRead, bytesRead;
53 bytesToRead = min(WIM_CHUNK_SIZE, bytes_remaining);
54 if (!ReadFile(hFile, out_buf, bytesToRead, &bytesRead, NULL) ||
55 bytesRead != bytesToRead)
58 ERROR("Failed to read data from \"%ls\"", lte->file_on_disk);
60 ret = WIMLIB_ERR_READ;
63 bytes_remaining -= bytesRead;
65 ret = (*cb)(out_buf, bytesRead, ctx_or_buf);
76 struct win32_encrypted_read_ctx {
77 consume_data_callback_t read_prefix_cb;
78 void *read_prefix_ctx_or_buf;
86 win32_encrypted_export_cb(unsigned char *_data, void *_ctx, unsigned long len)
88 const void *data = _data;
89 struct win32_encrypted_read_ctx *ctx = _ctx;
92 DEBUG("len = %lu", len);
93 if (ctx->read_prefix_cb) {
94 /* The length of the buffer passed to the ReadEncryptedFileRaw()
95 * export callback is undocumented, so we assume it may be of
97 size_t bytes_to_buffer = min(ctx->bytes_remaining - ctx->buf_filled,
99 while (bytes_to_buffer) {
100 size_t bytes_to_copy_to_buf =
101 min(bytes_to_buffer, WIM_CHUNK_SIZE - ctx->buf_filled);
103 memcpy(ctx->buf + ctx->buf_filled, data,
104 bytes_to_copy_to_buf);
105 ctx->buf_filled += bytes_to_copy_to_buf;
106 data += bytes_to_copy_to_buf;
107 bytes_to_buffer -= bytes_to_copy_to_buf;
109 if (ctx->buf_filled == WIM_CHUNK_SIZE ||
110 ctx->buf_filled == ctx->bytes_remaining)
112 ret = (*ctx->read_prefix_cb)(ctx->buf,
114 ctx->read_prefix_ctx_or_buf);
116 ctx->wimlib_err_code = ret;
117 /* Shouldn't matter what error code is returned
118 * here, as long as it isn't ERROR_SUCCESS. */
119 return ERROR_READ_FAULT;
121 ctx->bytes_remaining -= ctx->buf_filled;
126 size_t len_to_copy = min(len, ctx->bytes_remaining);
127 memcpy(ctx->read_prefix_ctx_or_buf, data, len_to_copy);
128 ctx->bytes_remaining -= len_to_copy;
129 ctx->read_prefix_ctx_or_buf += len_to_copy;
131 return ERROR_SUCCESS;
135 read_win32_encrypted_file_prefix(const struct wim_lookup_table_entry *lte,
137 consume_data_callback_t cb,
141 struct win32_encrypted_read_ctx export_ctx;
146 DEBUG("Reading %"PRIu64" bytes from encryted file \"%ls\"",
147 size, lte->file_on_disk);
149 export_ctx.read_prefix_cb = cb;
150 export_ctx.read_prefix_ctx_or_buf = ctx_or_buf;
151 export_ctx.wimlib_err_code = 0;
153 export_ctx.buf = MALLOC(WIM_CHUNK_SIZE);
155 return WIMLIB_ERR_NOMEM;
157 export_ctx.buf = NULL;
159 export_ctx.buf_filled = 0;
160 export_ctx.bytes_remaining = size;
162 err = OpenEncryptedFileRawW(lte->file_on_disk, 0, &file_ctx);
163 if (err != ERROR_SUCCESS) {
164 ERROR("Failed to open encrypted file \"%ls\" for raw read",
167 ret = WIMLIB_ERR_OPEN;
170 err = ReadEncryptedFileRaw(win32_encrypted_export_cb,
171 &export_ctx, file_ctx);
172 if (err != ERROR_SUCCESS) {
173 ERROR("Failed to read encrypted file \"%ls\"",
176 ret = export_ctx.wimlib_err_code;
178 ret = WIMLIB_ERR_READ;
179 } else if (export_ctx.bytes_remaining != 0) {
180 ERROR("Only could read %"PRIu64" of %"PRIu64" bytes from "
181 "encryted file \"%ls\"",
182 size - export_ctx.bytes_remaining, size,
184 ret = WIMLIB_ERR_READ;
188 CloseEncryptedFileRaw(file_ctx);
190 FREE(export_ctx.buf);
196 FILETIME_to_u64(const FILETIME *ft)
198 return ((u64)ft->dwHighDateTime << 32) | (u64)ft->dwLowDateTime;
202 win32_get_short_name(struct wim_dentry *dentry, const wchar_t *path)
204 WIN32_FIND_DATAW dat;
208 /* If we can't read the short filename for some reason, we just ignore
209 * the error and assume the file has no short name. I don't think this
210 * should be an issue, since the short names are essentially obsolete
212 hFind = FindFirstFileW(path, &dat);
213 if (hFind != INVALID_HANDLE_VALUE) {
214 if (dat.cAlternateFileName[0] != L'\0') {
215 DEBUG("\"%ls\": short name \"%ls\"", path, dat.cAlternateFileName);
216 size_t short_name_nbytes = wcslen(dat.cAlternateFileName) *
218 size_t n = short_name_nbytes + sizeof(wchar_t);
219 dentry->short_name = MALLOC(n);
220 if (dentry->short_name) {
221 memcpy(dentry->short_name, dat.cAlternateFileName, n);
222 dentry->short_name_nbytes = short_name_nbytes;
224 ret = WIMLIB_ERR_NOMEM;
233 win32_get_security_descriptor(struct wim_dentry *dentry,
234 struct sd_set *sd_set,
236 struct win32_capture_state *state,
239 SECURITY_INFORMATION requestedInformation;
245 requestedInformation = DACL_SECURITY_INFORMATION |
246 SACL_SECURITY_INFORMATION |
247 OWNER_SECURITY_INFORMATION |
248 GROUP_SECURITY_INFORMATION;
250 /* Request length of security descriptor */
251 status = GetFileSecurityW(path, requestedInformation,
252 NULL, 0, &lenNeeded);
253 err = GetLastError();
254 if (!status && err == ERROR_INSUFFICIENT_BUFFER) {
255 DWORD len = lenNeeded;
257 if (GetFileSecurityW(path, requestedInformation,
258 (PSECURITY_DESCRIPTOR)buf, len, &lenNeeded))
260 int security_id = sd_set_add_sd(sd_set, buf, len);
262 return WIMLIB_ERR_NOMEM;
264 dentry->d_inode->i_security_id = security_id;
268 err = GetLastError();
272 if (add_flags & WIMLIB_ADD_FLAG_STRICT_ACLS)
276 case ERROR_PRIVILEGE_NOT_HELD:
277 if (requestedInformation & SACL_SECURITY_INFORMATION) {
278 n = state->num_get_sacl_priv_notheld++;
279 requestedInformation &= ~SACL_SECURITY_INFORMATION;
280 if (n < MAX_GET_SACL_PRIV_NOTHELD_WARNINGS) {
282 "We don't have enough privileges to read the full security\n"
283 " descriptor of \"%ls\"!\n"
284 " Re-trying with SACL omitted.\n", path);
285 } else if (n == MAX_GET_SACL_PRIV_NOTHELD_WARNINGS) {
287 "Suppressing further privileges not held error messages when reading\n"
288 " security descriptors.");
293 case ERROR_ACCESS_DENIED:
294 n = state->num_get_sd_access_denied++;
295 if (n < MAX_GET_SD_ACCESS_DENIED_WARNINGS) {
296 WARNING("Failed to read security descriptor of \"%ls\": "
297 "Access denied!\n%ls", path, capture_access_denied_msg);
298 } else if (n == MAX_GET_SD_ACCESS_DENIED_WARNINGS) {
299 WARNING("Suppressing further access denied errors messages i"
300 "when reading security descriptors");
305 ERROR("Failed to read security descriptor of \"%ls\"", path);
307 return WIMLIB_ERR_READ;
312 win32_build_dentry_tree_recursive(struct wim_dentry **root_ret,
314 size_t path_num_chars,
315 struct add_image_params *params,
316 struct win32_capture_state *state,
319 /* Reads the directory entries of directory using a Win32 API and recursively
320 * calls win32_build_dentry_tree() on them. */
322 win32_recurse_directory(struct wim_dentry *root,
324 size_t dir_path_num_chars,
325 struct add_image_params *params,
326 struct win32_capture_state *state,
329 WIN32_FIND_DATAW dat;
334 DEBUG("Recurse to directory \"%ls\"", dir_path);
336 /* Begin reading the directory by calling FindFirstFileW. Unlike UNIX
337 * opendir(), FindFirstFileW has file globbing built into it. But this
338 * isn't what we actually want, so just add a dummy glob to get all
340 dir_path[dir_path_num_chars] = L'/';
341 dir_path[dir_path_num_chars + 1] = L'*';
342 dir_path[dir_path_num_chars + 2] = L'\0';
343 hFind = FindFirstFileW(dir_path, &dat);
344 dir_path[dir_path_num_chars] = L'\0';
346 if (hFind == INVALID_HANDLE_VALUE) {
347 err = GetLastError();
348 if (err == ERROR_FILE_NOT_FOUND) {
351 ERROR("Failed to read directory \"%ls\"", dir_path);
353 return WIMLIB_ERR_READ;
358 /* Skip . and .. entries */
359 if (dat.cFileName[0] == L'.' &&
360 (dat.cFileName[1] == L'\0' ||
361 (dat.cFileName[1] == L'.' &&
362 dat.cFileName[2] == L'\0')))
364 size_t filename_len = wcslen(dat.cFileName);
366 dir_path[dir_path_num_chars] = L'/';
367 wmemcpy(dir_path + dir_path_num_chars + 1,
371 struct wim_dentry *child;
372 size_t path_len = dir_path_num_chars + 1 + filename_len;
373 ret = win32_build_dentry_tree_recursive(&child,
379 dir_path[dir_path_num_chars] = L'\0';
383 dentry_add_child(root, child);
384 } while (FindNextFileW(hFind, &dat));
385 err = GetLastError();
386 if (err != ERROR_NO_MORE_FILES) {
387 ERROR("Failed to read directory \"%ls\"", dir_path);
390 ret = WIMLIB_ERR_READ;
398 win32_get_file_and_vol_ids(const wchar_t *path, u64 *ino_ret, u64 *dev_ret)
402 BY_HANDLE_FILE_INFORMATION file_info;
405 hFile = win32_open_existing_file(path, FILE_READ_ATTRIBUTES);
406 if (hFile == INVALID_HANDLE_VALUE) {
407 err = GetLastError();
408 if (err != ERROR_FILE_NOT_FOUND) {
409 WARNING("Failed to open \"%ls\" to get file "
410 "and volume IDs", path);
413 return WIMLIB_ERR_OPEN;
416 if (!GetFileInformationByHandle(hFile, &file_info)) {
417 err = GetLastError();
418 ERROR("Failed to get file information for \"%ls\"", path);
420 ret = WIMLIB_ERR_STAT;
422 *ino_ret = ((u64)file_info.nFileIndexHigh << 32) |
423 (u64)file_info.nFileIndexLow;
424 *dev_ret = file_info.dwVolumeSerialNumber;
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;
515 enum rp_status rp_status;
517 rpbuflen = *rpbuflen_p;
518 ret = parse_reparse_data(rpbuf, rpbuflen, &rpdata);
522 rp_status = win32_capture_maybe_rpfix_target(rpdata.substitute_name,
523 &rpdata.substitute_name_nbytes,
526 le32_to_cpu(*(u32*)rpbuf));
527 if (rp_status & RP_FIXED) {
528 wimlib_assert(rpdata.substitute_name_nbytes % 2 == 0);
529 utf16lechar substitute_name_copy[rpdata.substitute_name_nbytes / 2];
530 wmemcpy(substitute_name_copy, rpdata.substitute_name,
531 rpdata.substitute_name_nbytes / 2);
532 rpdata.substitute_name = substitute_name_copy;
533 rpdata.print_name = substitute_name_copy;
534 rpdata.print_name_nbytes = rpdata.substitute_name_nbytes;
535 if (rp_status == RP_FIXED_FULLPATH) {
536 /* "full path", meaning \??\ prefixed. We should not
537 * include this prefix in the print name, as it is
538 * apparently meant for the filesystem driver only. */
539 rpdata.print_name += 4;
540 rpdata.print_name_nbytes -= 8;
542 ret = make_reparse_buffer(&rpdata, rpbuf);
548 if (rp_status == RP_EXCLUDED) {
549 size_t print_name_nchars = rpdata.print_name_nbytes / 2;
550 wchar_t print_name0[print_name_nchars + 1];
551 print_name0[print_name_nchars] = L'\0';
552 wmemcpy(print_name0, rpdata.print_name, print_name_nchars);
553 WARNING("Ignoring %ls pointing out of capture directory:\n"
554 " \"%ls\" -> \"%ls\"\n"
555 " (Use --norpfix to capture all symbolic links "
556 "and junction points as-is)",
557 (rpdata.rptag == WIM_IO_REPARSE_TAG_SYMLINK) ?
558 L"absolute symbolic link" : L"junction point",
567 * Loads the reparse point data from a reparse point into memory, optionally
568 * fixing the targets of absolute symbolic links and junction points to be
569 * relative to the root of capture.
571 * @hFile: Open handle to the reparse point.
572 * @path: Path to the reparse point. Used for error messages only.
573 * @params: Additional parameters, including whether to do reparse point fixups
575 * @rpbuf: Buffer of length at least REPARSE_POINT_MAX_SIZE bytes into which
576 * the reparse point buffer will be loaded.
577 * @rpbuflen_ret: On success, the length of the reparse point buffer in bytes
578 * is written to this location.
581 * On success, returns an `enum rp_status' value that indicates if and/or
582 * how the reparse point fixup was done.
584 * On failure, returns a negative value that is a negated WIMLIB_ERR_*
588 win32_get_reparse_data(HANDLE hFile, const wchar_t *path,
589 struct add_image_params *params,
590 u8 *rpbuf, u16 *rpbuflen_ret)
597 DEBUG("Loading reparse data from \"%ls\"", path);
598 if (!DeviceIoControl(hFile, FSCTL_GET_REPARSE_POINT,
599 NULL, /* "Not used with this operation; set to NULL" */
600 0, /* "Not used with this operation; set to 0" */
601 rpbuf, /* "A pointer to a buffer that
602 receives the reparse point data */
603 REPARSE_POINT_MAX_SIZE, /* "The size of the output
608 DWORD err = GetLastError();
609 ERROR("Failed to get reparse data of \"%ls\"", path);
611 return -WIMLIB_ERR_READ;
613 if (bytesReturned < 8 || bytesReturned > REPARSE_POINT_MAX_SIZE) {
614 ERROR("Reparse data on \"%ls\" is invalid", path);
615 return -WIMLIB_ERR_INVALID_REPARSE_DATA;
618 rpbuflen = bytesReturned;
619 reparse_tag = le32_to_cpu(*(u32*)rpbuf);
620 if (params->add_flags & WIMLIB_ADD_FLAG_RPFIX &&
621 (reparse_tag == WIM_IO_REPARSE_TAG_SYMLINK ||
622 reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT))
624 /* Try doing reparse point fixup */
625 ret = win32_capture_try_rpfix(rpbuf,
627 params->capture_root_ino,
628 params->capture_root_dev,
633 *rpbuflen_ret = rpbuflen;
638 win32_tally_encrypted_size_cb(unsigned char *_data, void *_ctx,
642 return ERROR_SUCCESS;
646 win32_get_encrypted_file_size(const wchar_t *path, u64 *size_ret)
653 err = OpenEncryptedFileRawW(path, 0, &file_ctx);
654 if (err != ERROR_SUCCESS) {
655 ERROR("Failed to open encrypted file \"%ls\" for raw read", path);
657 return WIMLIB_ERR_OPEN;
659 err = ReadEncryptedFileRaw(win32_tally_encrypted_size_cb,
661 if (err != ERROR_SUCCESS) {
662 ERROR("Failed to read raw encrypted data from \"%ls\"", path);
664 ret = WIMLIB_ERR_READ;
668 CloseEncryptedFileRaw(file_ctx);
672 /* Scans an unnamed or named stream of a Win32 file (not a reparse point
673 * stream); calculates its SHA1 message digest and either creates a `struct
674 * wim_lookup_table_entry' in memory for it, or uses an existing 'struct
675 * wim_lookup_table_entry' for an identical stream.
677 * @path: Path to the file (UTF-16LE).
679 * @path_num_chars: Number of 2-byte characters in @path.
681 * @inode: WIM inode to save the stream into.
683 * @lookup_table: Stream lookup table for the WIM.
685 * @dat: A `WIN32_FIND_STREAM_DATA' structure that specifies the
688 * Returns 0 on success; nonzero on failure.
691 win32_capture_stream(const wchar_t *path,
692 size_t path_num_chars,
693 struct wim_inode *inode,
694 struct wim_lookup_table *lookup_table,
695 WIN32_FIND_STREAM_DATA *dat)
697 struct wim_ads_entry *ads_entry;
698 struct wim_lookup_table_entry *lte;
700 wchar_t *stream_name, *colon;
701 size_t stream_name_nchars;
702 bool is_named_stream;
705 size_t spath_buf_nbytes;
706 const wchar_t *relpath_prefix;
707 const wchar_t *colonchar;
709 DEBUG("Capture \"%ls\" stream \"%ls\"", path, dat->cStreamName);
711 /* The stream name should be returned as :NAME:TYPE */
712 stream_name = dat->cStreamName;
713 if (*stream_name != L':')
714 goto out_invalid_stream_name;
716 colon = wcschr(stream_name, L':');
718 goto out_invalid_stream_name;
720 if (wcscmp(colon + 1, L"$DATA")) {
721 /* Not a DATA stream */
728 stream_name_nchars = colon - stream_name;
729 is_named_stream = (stream_name_nchars != 0);
731 if (is_named_stream) {
732 /* Allocate an ADS entry for the named stream. */
733 ads_entry = inode_add_ads_utf16le(inode, stream_name,
734 stream_name_nchars * sizeof(wchar_t));
736 ret = WIMLIB_ERR_NOMEM;
741 /* If zero length stream, no lookup table entry needed. */
742 if ((u64)dat->StreamSize.QuadPart == 0) {
747 /* Create a UTF-16LE string @spath that gives the filename, then a
748 * colon, then the stream name. Or, if it's an unnamed stream, just the
749 * filename. It is MALLOC()'ed so that it can be saved in the
750 * wim_lookup_table_entry if needed.
752 * As yet another special case, relative paths need to be changed to
753 * begin with an explicit "./" so that, for example, a file t:ads, where
754 * :ads is the part we added, is not interpreted as a file on the t:
756 spath_nchars = path_num_chars;
757 relpath_prefix = L"";
759 if (is_named_stream) {
760 spath_nchars += 1 + stream_name_nchars;
762 if (path_num_chars == 1 &&
767 relpath_prefix = L"./";
771 spath_buf_nbytes = (spath_nchars + 1) * sizeof(wchar_t);
772 spath = MALLOC(spath_buf_nbytes);
774 swprintf(spath, L"%ls%ls%ls%ls",
775 relpath_prefix, path, colonchar, stream_name);
777 /* Make a new wim_lookup_table_entry */
778 lte = new_lookup_table_entry();
780 ret = WIMLIB_ERR_NOMEM;
783 lte->file_on_disk = spath;
785 if (inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED && !is_named_stream) {
787 lte->resource_location = RESOURCE_WIN32_ENCRYPTED;
788 ret = win32_get_encrypted_file_size(path, &encrypted_size);
791 lte->resource_entry.original_size = encrypted_size;
793 lte->resource_location = RESOURCE_WIN32;
794 lte->resource_entry.original_size = (u64)dat->StreamSize.QuadPart;
798 if (is_named_stream) {
799 stream_id = ads_entry->stream_id;
800 ads_entry->lte = lte;
805 lookup_table_insert_unhashed(lookup_table, lte, inode, stream_id);
811 out_invalid_stream_name:
812 ERROR("Invalid stream name: \"%ls:%ls\"", path, dat->cStreamName);
813 ret = WIMLIB_ERR_READ;
817 /* Scans a Win32 file for unnamed and named data streams (not reparse point
820 * @path: Path to the file (UTF-16LE).
822 * @path_num_chars: Number of 2-byte characters in @path.
824 * @inode: WIM inode to save the stream into.
826 * @lookup_table: Stream lookup table for the WIM.
828 * @file_size: Size of unnamed data stream. (Used only if alternate
829 * data streams API appears to be unavailable.)
831 * @vol_flags: Flags that specify features of the volume being
834 * Returns 0 on success; nonzero on failure.
837 win32_capture_streams(const wchar_t *path,
838 size_t path_num_chars,
839 struct wim_inode *inode,
840 struct wim_lookup_table *lookup_table,
844 WIN32_FIND_STREAM_DATA dat;
849 DEBUG("Capturing streams from \"%ls\"", path);
851 if (win32func_FindFirstStreamW == NULL ||
852 !(vol_flags & FILE_NAMED_STREAMS))
855 hFind = win32func_FindFirstStreamW(path, FindStreamInfoStandard, &dat, 0);
856 if (hFind == INVALID_HANDLE_VALUE) {
857 err = GetLastError();
858 if (err == ERROR_CALL_NOT_IMPLEMENTED)
861 /* Seems legal for this to return ERROR_HANDLE_EOF on reparse
862 * points and directories */
863 if ((inode->i_attributes &
864 (FILE_ATTRIBUTE_REPARSE_POINT | FILE_ATTRIBUTE_DIRECTORY))
865 && err == ERROR_HANDLE_EOF)
867 DEBUG("ERROR_HANDLE_EOF (ok)");
870 if (err == ERROR_ACCESS_DENIED) {
871 WARNING("Failed to look up data streams "
872 "of \"%ls\": Access denied!\n%ls",
873 path, capture_access_denied_msg);
876 ERROR("Failed to look up data streams "
879 return WIMLIB_ERR_READ;
884 ret = win32_capture_stream(path,
890 } while (win32func_FindNextStreamW(hFind, &dat));
891 err = GetLastError();
892 if (err != ERROR_HANDLE_EOF) {
893 ERROR("Win32 API: Error reading data streams from \"%ls\"", path);
895 ret = WIMLIB_ERR_READ;
901 /* FindFirstStreamW() API is not available, or the volume does not
902 * support named streams. Only capture the unnamed data stream. */
903 DEBUG("Only capturing unnamed data stream");
904 if (inode->i_attributes &
905 (FILE_ATTRIBUTE_REPARSE_POINT | FILE_ATTRIBUTE_DIRECTORY))
909 /* Just create our own WIN32_FIND_STREAM_DATA for an unnamed
910 * stream to reduce the code to a call to the
911 * already-implemented win32_capture_stream() */
912 wcscpy(dat.cStreamName, L"::$DATA");
913 dat.StreamSize.QuadPart = file_size;
914 ret = win32_capture_stream(path,
923 win32_build_dentry_tree_recursive(struct wim_dentry **root_ret,
925 size_t path_num_chars,
926 struct add_image_params *params,
927 struct win32_capture_state *state,
930 struct wim_dentry *root = NULL;
931 struct wim_inode *inode;
939 if (exclude_path(path, path_num_chars, params->config, true)) {
940 if (params->add_flags & WIMLIB_ADD_FLAG_ROOT) {
941 ERROR("Cannot exclude the root directory from capture");
942 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
945 if ((params->add_flags & WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE)
946 && params->progress_func)
948 union wimlib_progress_info info;
949 info.scan.cur_path = path;
950 info.scan.excluded = true;
951 params->progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
957 if ((params->add_flags & WIMLIB_ADD_FLAG_VERBOSE)
958 && params->progress_func)
960 union wimlib_progress_info info;
961 info.scan.cur_path = path;
962 info.scan.excluded = false;
963 params->progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
966 HANDLE hFile = win32_open_existing_file(path,
967 FILE_READ_DATA | FILE_READ_ATTRIBUTES);
968 if (hFile == INVALID_HANDLE_VALUE) {
969 err = GetLastError();
970 ERROR("Win32 API: Failed to open \"%ls\"", path);
972 ret = WIMLIB_ERR_OPEN;
976 BY_HANDLE_FILE_INFORMATION file_info;
977 if (!GetFileInformationByHandle(hFile, &file_info)) {
978 err = GetLastError();
979 ERROR("Win32 API: Failed to get file information for \"%ls\"",
982 ret = WIMLIB_ERR_STAT;
983 goto out_close_handle;
986 if (file_info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
987 rpbuf = alloca(REPARSE_POINT_MAX_SIZE);
988 ret = win32_get_reparse_data(hFile, path, params,
991 /* WIMLIB_ERR_* (inverted) */
993 goto out_close_handle;
994 } else if (ret & RP_FIXED) {
996 } else if (ret == RP_EXCLUDED) {
998 goto out_close_handle;
1004 /* Create a WIM dentry with an associated inode, which may be shared.
1006 * However, we need to explicitly check for directories and files with
1007 * only 1 link and refuse to hard link them. This is because Windows
1008 * has a bug where it can return duplicate File IDs for files and
1009 * directories on the FAT filesystem. */
1010 ret = inode_table_new_dentry(params->inode_table,
1011 path_basename_with_len(path, path_num_chars),
1012 ((u64)file_info.nFileIndexHigh << 32) |
1013 (u64)file_info.nFileIndexLow,
1014 file_info.dwVolumeSerialNumber,
1015 (file_info.nNumberOfLinks <= 1 ||
1016 (file_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)),
1019 goto out_close_handle;
1021 ret = win32_get_short_name(root, path);
1023 goto out_close_handle;
1025 inode = root->d_inode;
1027 if (inode->i_nlink > 1) /* Shared inode; nothing more to do */
1028 goto out_close_handle;
1030 inode->i_attributes = file_info.dwFileAttributes;
1031 inode->i_creation_time = FILETIME_to_u64(&file_info.ftCreationTime);
1032 inode->i_last_write_time = FILETIME_to_u64(&file_info.ftLastWriteTime);
1033 inode->i_last_access_time = FILETIME_to_u64(&file_info.ftLastAccessTime);
1034 inode->i_resolved = 1;
1036 params->add_flags &= ~WIMLIB_ADD_FLAG_ROOT;
1038 if (!(params->add_flags & WIMLIB_ADD_FLAG_NO_ACLS)
1039 && (vol_flags & FILE_PERSISTENT_ACLS))
1041 ret = win32_get_security_descriptor(root, params->sd_set,
1045 goto out_close_handle;
1048 file_size = ((u64)file_info.nFileSizeHigh << 32) |
1049 (u64)file_info.nFileSizeLow;
1053 /* Capture the unnamed data stream (only should be present for regular
1054 * files) and any alternate data streams. */
1055 ret = win32_capture_streams(path,
1058 params->lookup_table,
1064 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1065 /* Reparse point: set the reparse data (which we read already)
1067 inode->i_not_rpfixed = not_rpfixed;
1068 inode->i_reparse_tag = le32_to_cpu(*(u32*)rpbuf);
1069 ret = inode_set_unnamed_stream(inode, rpbuf + 8, rpbuflen - 8,
1070 params->lookup_table);
1071 } else if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY) {
1072 /* Directory (not a reparse point) --- recurse to children */
1073 ret = win32_recurse_directory(root,
1087 free_dentry_tree(root, params->lookup_table);
1092 win32_do_capture_warnings(const struct win32_capture_state *state,
1095 if (state->num_get_sacl_priv_notheld == 0 &&
1096 state->num_get_sd_access_denied == 0)
1100 WARNING("Built dentry tree successfully, but with the following problem(s):");
1101 if (state->num_get_sacl_priv_notheld != 0) {
1102 WARNING("Could not capture SACL (System Access Control List)\n"
1103 " on %lu files or directories.",
1104 state->num_get_sacl_priv_notheld);
1106 if (state->num_get_sd_access_denied != 0) {
1107 WARNING("Could not capture security descriptor at all\n"
1108 " on %lu files or directories.",
1109 state->num_get_sd_access_denied);
1112 "Try running the program as the Administrator to make sure all the\n"
1113 " desired metadata has been captured exactly. However, if you\n"
1114 " do not care about capturing security descriptors correctly, then\n"
1115 " nothing more needs to be done%ls\n",
1116 (add_flags & WIMLIB_ADD_FLAG_NO_ACLS) ? L"." :
1117 L", although you might consider\n"
1118 " passing the --no-acls flag to `wimlib-imagex capture' or\n"
1119 " `wimlib-imagex append' to explicitly capture no security\n"
1123 /* Win32 version of capturing a directory tree */
1125 win32_build_dentry_tree(struct wim_dentry **root_ret,
1126 const wchar_t *root_disk_path,
1127 struct add_image_params *params)
1132 struct win32_capture_state state;
1136 path_nchars = wcslen(root_disk_path);
1137 if (path_nchars > 32767)
1138 return WIMLIB_ERR_INVALID_PARAM;
1140 if (GetFileAttributesW(root_disk_path) == INVALID_FILE_ATTRIBUTES &&
1141 GetLastError() == ERROR_FILE_NOT_FOUND)
1143 ERROR("Capture directory \"%ls\" does not exist!",
1145 return WIMLIB_ERR_OPENDIR;
1148 ret = win32_get_file_and_vol_ids(root_disk_path,
1149 ¶ms->capture_root_ino,
1150 ¶ms->capture_root_dev);
1154 win32_get_vol_flags(root_disk_path, &vol_flags);
1156 /* There is no check for overflow later when this buffer is being used!
1157 * But the max path length on NTFS is 32767 characters, and paths need
1158 * to be written specially to even go past 260 characters, so we should
1159 * be okay with 32770 characters. */
1160 path = MALLOC(32770 * sizeof(wchar_t));
1162 return WIMLIB_ERR_NOMEM;
1164 wmemcpy(path, root_disk_path, path_nchars + 1);
1166 memset(&state, 0, sizeof(state));
1167 ret = win32_build_dentry_tree_recursive(root_ret, path,
1168 path_nchars, params,
1172 win32_do_capture_warnings(&state, params->add_flags);
1176 #endif /* __WIN32__ */