+ * The current workaround is to simply read the stream into
+ * memory and write the encrypted file from that.
+ *
+ * TODO: This isn't sufficient for extremely large encrypted
+ * files. Perhaps we should create an extra thread to write
+ * such files... */
+ if (!prepare_data_buffer(ctx, stream->size))
+ return WIMLIB_ERR_NOMEM;
+ list_add_tail(&dentry->tmp_list, &ctx->encrypted_dentries);
+ return 0;
+ }
+
+ if (ctx->num_open_handles == MAX_OPEN_STREAMS) {
+ /* XXX: Fix this. But because of the checks in
+ * extract_stream_list(), this can now only happen on a
+ * filesystem that does not support hard links. */
+ ERROR("Can't extract data: too many open files!");
+ return WIMLIB_ERR_UNSUPPORTED;
+ }
+
+ /* Open a new handle */
+ status = do_create_file(&h,
+ FILE_WRITE_DATA | SYNCHRONIZE,
+ NULL, 0, FILE_OPEN_IF,
+ FILE_SEQUENTIAL_ONLY |
+ FILE_SYNCHRONOUS_IO_NONALERT,
+ ctx);
+ if (!NT_SUCCESS(status)) {
+ set_errno_from_nt_status(status);
+ ERROR_WITH_ERRNO("Can't open \"%ls\" for writing "
+ "(status=0x%08"PRIx32")",
+ current_path(ctx), (u32)status);
+ return WIMLIB_ERR_OPEN;
+ }
+
+ ctx->open_handles[ctx->num_open_handles++] = h;
+
+ /* Allocate space for the data. */
+ alloc_info.AllocationSize.QuadPart = stream->size;
+ (*func_NtSetInformationFile)(h, &ctx->iosb,
+ &alloc_info, sizeof(alloc_info),
+ FileAllocationInformation);
+ return 0;
+}
+
+/* Set the reparse data @rpbuf of length @rpbuflen on the extracted file
+ * corresponding to the WIM dentry @dentry. */
+static int
+do_set_reparse_data(const struct wim_dentry *dentry,
+ const void *rpbuf, u16 rpbuflen,
+ struct win32_apply_ctx *ctx)
+{
+ NTSTATUS status;
+ HANDLE h;
+
+ status = create_file(&h, GENERIC_WRITE, NULL,
+ 0, FILE_OPEN, 0, dentry, ctx);
+ if (!NT_SUCCESS(status))
+ goto fail;
+
+ status = (*func_NtFsControlFile)(h, NULL, NULL, NULL,
+ &ctx->iosb, FSCTL_SET_REPARSE_POINT,
+ (void *)rpbuf, rpbuflen,
+ NULL, 0);
+ (*func_NtClose)(h);
+
+ if (NT_SUCCESS(status))
+ return 0;
+
+ /* On Windows, by default only the Administrator can create symbolic
+ * links for some reason. By default we just issue a warning if this
+ * appears to be the problem. Use WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS
+ * to get a hard error. */
+ if (!(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS)
+ && (status == STATUS_PRIVILEGE_NOT_HELD ||
+ status == STATUS_ACCESS_DENIED)
+ && (dentry->d_inode->i_reparse_tag == WIM_IO_REPARSE_TAG_SYMLINK ||
+ dentry->d_inode->i_reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT))
+ {
+ WARNING("Can't create symbolic link \"%ls\"! \n"
+ " (Need Administrator rights, or at least "
+ "the\n"
+ " SeCreateSymbolicLink privilege.)",
+ current_path(ctx));
+ return 0;
+ }
+
+fail:
+ set_errno_from_nt_status(status);
+ ERROR_WITH_ERRNO("Can't set reparse data on \"%ls\" "
+ "(status=0x%08"PRIx32")",
+ current_path(ctx), (u32)status);
+ return WIMLIB_ERR_SET_REPARSE_DATA;
+}
+
+/* Given a Windows NT namespace path, such as \??\e:\Windows\System32, return a
+ * pointer to the suffix of the path that begins with the device directly, such
+ * as e:\Windows\System32. */
+static const wchar_t *
+skip_nt_toplevel_component(const wchar_t *path, size_t path_nchars)
+{
+ static const wchar_t * const dirs[] = {
+ L"\\??\\",
+ L"\\DosDevices\\",
+ L"\\Device\\",
+ };
+ size_t first_dir_len = 0;
+ const wchar_t * const end = path + path_nchars;
+
+ for (size_t i = 0; i < ARRAY_LEN(dirs); i++) {
+ size_t len = wcslen(dirs[i]);
+ if (len <= (end - path) && !wcsnicmp(path, dirs[i], len)) {
+ first_dir_len = len;
+ break;
+ }
+ }
+ if (first_dir_len == 0)
+ return path;
+ path += first_dir_len;
+ while (path != end && *path == L'\\')
+ path++;
+ return path;
+}
+
+/* Given a Windows NT namespace path, such as \??\e:\Windows\System32, return a
+ * pointer to the suffix of the path that is device-relative, such as
+ * Windows\System32.
+ *
+ * The path has an explicit length and is not necessarily null terminated.
+ *
+ * If the path just something like \??\e: then the returned pointer will point
+ * just past the colon. In this case the length of the result will be 0
+ * characters. */
+static const wchar_t *
+get_device_relative_path(const wchar_t *path, size_t path_nchars)
+{
+ const wchar_t * const orig_path = path;
+ const wchar_t * const end = path + path_nchars;
+
+ path = skip_nt_toplevel_component(path, path_nchars);
+ if (path == orig_path)
+ return orig_path;
+
+ path = wmemchr(path, L'\\', (end - path));
+ if (!path)
+ return end;
+ do {
+ path++;
+ } while (path != end && *path == L'\\');
+ return path;
+}
+
+/*
+ * Given a reparse point buffer for a symbolic link or junction, adjust its
+ * contents so that the target of the link is consistent with the new location
+ * of the files.
+ */
+static void
+try_rpfix(u8 *rpbuf, u16 *rpbuflen_p, struct win32_apply_ctx *ctx)
+{
+ struct reparse_data rpdata;
+ size_t orig_subst_name_nchars;
+ const wchar_t *relpath;
+ size_t relpath_nchars;
+ size_t target_ntpath_nchars;
+ size_t fixed_subst_name_nchars;
+ const wchar_t *fixed_print_name;
+ size_t fixed_print_name_nchars;
+
+ if (parse_reparse_data(rpbuf, *rpbuflen_p, &rpdata)) {
+ /* Do nothing if the reparse data is invalid. */
+ return;
+ }
+
+ if (rpdata.rptag == WIM_IO_REPARSE_TAG_SYMLINK &&
+ (rpdata.rpflags & SYMBOLIC_LINK_RELATIVE))
+ {
+ /* Do nothing if it's a relative symbolic link. */
+ return;
+ }
+
+ /* Build the new substitute name from the NT namespace path to the
+ * target directory, then a path separator, then the "device relative"
+ * part of the old substitute name. */
+
+ orig_subst_name_nchars = rpdata.substitute_name_nbytes / sizeof(wchar_t);
+
+ relpath = get_device_relative_path(rpdata.substitute_name,
+ orig_subst_name_nchars);
+ relpath_nchars = orig_subst_name_nchars -
+ (relpath - rpdata.substitute_name);
+
+ target_ntpath_nchars = ctx->target_ntpath.Length / sizeof(wchar_t);
+
+ fixed_subst_name_nchars = target_ntpath_nchars;
+ if (relpath_nchars)
+ fixed_subst_name_nchars += 1 + relpath_nchars;
+ wchar_t fixed_subst_name[fixed_subst_name_nchars];
+
+ wmemcpy(fixed_subst_name, ctx->target_ntpath.Buffer,
+ target_ntpath_nchars);
+ if (relpath_nchars) {
+ fixed_subst_name[target_ntpath_nchars] = L'\\';
+ wmemcpy(&fixed_subst_name[target_ntpath_nchars + 1],
+ relpath, relpath_nchars);
+ }
+ /* Doesn't need to be null-terminated. */
+
+ /* Print name should be Win32, but not all NT names can even be
+ * translated to Win32 names. But we can at least delete the top-level
+ * directory, such as \??\, and this will have the expected result in
+ * the usual case. */
+ fixed_print_name = skip_nt_toplevel_component(fixed_subst_name,
+ fixed_subst_name_nchars);
+ fixed_print_name_nchars = fixed_subst_name_nchars - (fixed_print_name -
+ fixed_subst_name);
+
+ rpdata.substitute_name = fixed_subst_name;
+ rpdata.substitute_name_nbytes = fixed_subst_name_nchars * sizeof(wchar_t);
+ rpdata.print_name = (wchar_t *)fixed_print_name;
+ rpdata.print_name_nbytes = fixed_print_name_nchars * sizeof(wchar_t);
+ make_reparse_buffer(&rpdata, rpbuf, rpbuflen_p);
+}
+
+/* Sets reparse data on the specified file. This handles "fixing" the targets
+ * of absolute symbolic links and junctions if WIMLIB_EXTRACT_FLAG_RPFIX was
+ * specified. */
+static int
+set_reparse_data(const struct wim_dentry *dentry,
+ const void *_rpbuf, u16 rpbuflen, struct win32_apply_ctx *ctx)
+{
+ const struct wim_inode *inode = dentry->d_inode;
+ const void *rpbuf = _rpbuf;
+
+ if ((ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX)
+ && !inode->i_not_rpfixed
+ && (inode->i_reparse_tag == WIM_IO_REPARSE_TAG_SYMLINK ||
+ inode->i_reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT))
+ {
+ memcpy(&ctx->rpfixbuf, _rpbuf, rpbuflen);
+ try_rpfix((u8 *)&ctx->rpfixbuf, &rpbuflen, ctx);
+ rpbuf = &ctx->rpfixbuf;
+ }
+ return do_set_reparse_data(dentry, rpbuf, rpbuflen, ctx);
+
+}
+
+/* Import the next block of raw encrypted data */
+static DWORD WINAPI
+import_encrypted_data(PBYTE pbData, PVOID pvCallbackContext, PULONG Length)
+{
+ struct win32_apply_ctx *ctx = pvCallbackContext;
+ ULONG copy_len;
+
+ copy_len = min(ctx->encrypted_size - ctx->encrypted_offset, *Length);
+ memcpy(pbData, &ctx->data_buffer[ctx->encrypted_offset], copy_len);
+ ctx->encrypted_offset += copy_len;
+ *Length = copy_len;
+ return ERROR_SUCCESS;
+}
+
+/* Write the raw encrypted data to the already-created file corresponding to
+ * @dentry.
+ *
+ * The raw encrypted data is provided in ctx->data_buffer, and its size is
+ * ctx->encrypted_size. */
+static int
+extract_encrypted_file(const struct wim_dentry *dentry,
+ struct win32_apply_ctx *ctx)
+{
+ void *rawctx;
+ DWORD err;
+
+ /* Temporarily build a Win32 path for OpenEncryptedFileRaw() */
+ build_win32_extraction_path(dentry, ctx);
+
+ err = OpenEncryptedFileRaw(ctx->pathbuf.Buffer,
+ CREATE_FOR_IMPORT, &rawctx);
+
+ /* Restore the NT namespace path */
+ build_extraction_path(dentry, ctx);
+
+ if (err != ERROR_SUCCESS) {
+ set_errno_from_win32_error(err);
+ ERROR_WITH_ERRNO("Can't open \"%ls\" for encrypted import "
+ "(err=%"PRIu32")", current_path(ctx), (u32)err);
+ return WIMLIB_ERR_OPEN;
+ }
+
+ ctx->encrypted_offset = 0;
+
+ err = WriteEncryptedFileRaw(import_encrypted_data, ctx, rawctx);
+
+ CloseEncryptedFileRaw(rawctx);
+
+ if (err != ERROR_SUCCESS) {
+ set_errno_from_win32_error(err);
+ ERROR_WITH_ERRNO("Can't import encrypted file \"%ls\" "
+ "(err=%"PRIu32")", current_path(ctx), (u32)err);
+ return WIMLIB_ERR_WRITE;
+ }
+
+ return 0;
+}
+
+/* Called when starting to read a stream for extraction on Windows */
+static int
+begin_extract_stream(struct wim_lookup_table_entry *stream, void *_ctx)
+{
+ struct win32_apply_ctx *ctx = _ctx;
+ const struct stream_owner *owners = stream_owners(stream);
+ int ret;
+
+ ctx->num_open_handles = 0;
+ ctx->data_buffer_ptr = NULL;
+ INIT_LIST_HEAD(&ctx->reparse_dentries);
+ INIT_LIST_HEAD(&ctx->encrypted_dentries);
+
+ for (u32 i = 0; i < stream->out_refcnt; i++) {
+ const struct wim_inode *inode = owners[i].inode;
+ const wchar_t *stream_name = owners[i].stream_name;
+ struct wim_dentry *dentry;
+
+ /* A copy of the stream needs to be extracted to @inode. */
+
+ if (ctx->common.supported_features.hard_links) {
+ dentry = inode_first_extraction_dentry(inode);
+ ret = begin_extract_stream_instance(stream, dentry,
+ stream_name, ctx);
+ ret = check_apply_error(dentry, ctx, ret);
+ if (ret)
+ goto fail;
+ } else {
+ /* Hard links not supported. Extract the stream
+ * separately to each alias of the inode. */
+ struct list_head *next;
+
+ next = inode->i_extraction_aliases.next;
+ do {
+ dentry = list_entry(next, struct wim_dentry,
+ d_extraction_alias_node);
+ ret = begin_extract_stream_instance(stream,
+ dentry,
+ stream_name,
+ ctx);
+ ret = check_apply_error(dentry, ctx, ret);
+ if (ret)
+ goto fail;
+ next = next->next;
+ } while (next != &inode->i_extraction_aliases);
+ }
+ }
+
+ return 0;
+
+fail:
+ close_handles(ctx);
+ return ret;
+}
+
+/* Called when the next chunk of a stream has been read for extraction on
+ * Windows */
+static int
+extract_chunk(const void *chunk, size_t size, void *_ctx)
+{
+ struct win32_apply_ctx *ctx = _ctx;
+
+ /* Write the data chunk to each open handle */
+ for (unsigned i = 0; i < ctx->num_open_handles; i++) {
+ u8 *bufptr = (u8 *)chunk;
+ size_t bytes_remaining = size;
+ NTSTATUS status;
+ while (bytes_remaining) {
+ ULONG count = min(0xFFFFFFFF, bytes_remaining);
+
+ status = (*func_NtWriteFile)(ctx->open_handles[i],
+ NULL, NULL, NULL,
+ &ctx->iosb, bufptr, count,
+ NULL, NULL);
+ if (!NT_SUCCESS(status)) {
+ set_errno_from_nt_status(status);
+ ERROR_WITH_ERRNO("Error writing data to target "
+ "volume (status=0x%08"PRIx32")",
+ (u32)status);
+ return WIMLIB_ERR_WRITE;