+
+ if (unlikely(filedes_valid(&ctx->tmpfile_fd))) {
+ /* Just extracting to temporary file for now. */
+ ret = full_write(&ctx->tmpfile_fd, chunk, size);
+ if (ret) {
+ ERROR_WITH_ERRNO("Error writing data to "
+ "temporary file \"%"TS"\"",
+ ctx->tmpfile_name);
+ }
+ return ret;
+ } else {
+ return (*ctx->saved_cbs->consume_chunk)(chunk, size,
+ ctx->saved_cbs->consume_chunk_ctx);
+ }
+}
+
+static int
+extract_from_tmpfile(const tchar *tmpfile_name, struct apply_ctx *ctx)
+{
+ struct wim_lookup_table_entry tmpfile_lte;
+ struct wim_lookup_table_entry *orig_lte = ctx->cur_stream;
+ const struct read_stream_list_callbacks *cbs = ctx->saved_cbs;
+ int ret;
+ const u32 orig_refcnt = orig_lte->out_refcnt;
+
+ BUILD_BUG_ON(MAX_OPEN_STREAMS < ARRAY_LEN(orig_lte->inline_stream_owners));
+
+ struct stream_owner *owners = orig_lte->stream_owners;
+
+ /* Copy the stream's data from the temporary file to each of its
+ * destinations.
+ *
+ * This is executed only in the very uncommon case that a
+ * single-instance stream is being extracted to more than
+ * MAX_OPEN_STREAMS locations! */
+
+ memcpy(&tmpfile_lte, orig_lte, sizeof(struct wim_lookup_table_entry));
+ tmpfile_lte.resource_location = RESOURCE_IN_FILE_ON_DISK;
+ tmpfile_lte.file_on_disk = ctx->tmpfile_name;
+ ret = 0;
+ for (u32 i = 0; i < orig_refcnt; i++) {
+
+ /* Note: it usually doesn't matter whether we pass the original
+ * stream entry to callbacks provided by the extraction backend
+ * as opposed to the tmpfile stream entry, since they shouldn't
+ * actually read data from the stream other than through the
+ * read_stream_prefix() call below. But for
+ * WIMLIB_EXTRACT_FLAG_WIMBOOT mode on Windows it does matter
+ * because it needs the original stream location in order to
+ * create the external backing reference. */
+
+ orig_lte->out_refcnt = 1;
+ orig_lte->inline_stream_owners[0] = owners[i];
+
+ ret = (*cbs->begin_stream)(orig_lte, cbs->begin_stream_ctx);
+ if (ret)
+ break;
+
+ /* Extra SHA-1 isn't necessary here, but it shouldn't hurt as
+ * this case is very rare anyway. */
+ ret = extract_stream(&tmpfile_lte, tmpfile_lte.size,
+ cbs->consume_chunk,
+ cbs->consume_chunk_ctx);
+
+ ret = (*cbs->end_stream)(orig_lte, ret, cbs->end_stream_ctx);
+ if (ret)
+ break;
+ }
+ FREE(owners);
+ orig_lte->out_refcnt = 0;
+ return ret;
+}
+
+static int
+end_extract_stream_wrapper(struct wim_lookup_table_entry *stream,
+ int status, void *_ctx)
+{
+ struct apply_ctx *ctx = _ctx;
+
+ if (unlikely(filedes_valid(&ctx->tmpfile_fd))) {
+ filedes_close(&ctx->tmpfile_fd);
+ if (!status)
+ status = extract_from_tmpfile(ctx->tmpfile_name, ctx);
+ filedes_invalidate(&ctx->tmpfile_fd);
+ tunlink(ctx->tmpfile_name);
+ FREE(ctx->tmpfile_name);
+ return status;
+ } else {
+ return (*ctx->saved_cbs->end_stream)(stream, status,
+ ctx->saved_cbs->end_stream_ctx);
+ }