+ struct write_streams_ctx *ctx = _ctx;
+ int ret;
+
+ wimlib_assert(lte->size > 0);
+
+ ctx->cur_read_res_offset = 0;
+ ctx->cur_read_res_size = lte->size;
+
+ /* As an optimization, we allow some streams to be "unhashed", meaning
+ * their SHA1 message digests are unknown. This is the case with
+ * streams that are added by scanning a directry tree with
+ * wimlib_add_image(), for example. Since WIM uses single-instance
+ * streams, we don't know whether such each such stream really need to
+ * written until it is actually checksummed, unless it has a unique
+ * size. In such cases we read and checksum the stream in this
+ * function, thereby advancing ahead of read_stream_list(), which will
+ * still provide the data again to write_stream_process_chunk(). This
+ * is okay because an unhashed stream cannot be in a WIM resource, which
+ * might be costly to decompress. */
+ ctx->stream_was_duplicate = false;
+ if (ctx->lookup_table != NULL && lte->unhashed && !lte->unique_size) {
+
+ wimlib_assert(!is_partial_res);
+ wimlib_assert(ctx->lookup_table != NULL);
+
+ struct wim_lookup_table_entry *lte_new;
+
+ ret = hash_unhashed_stream(lte, ctx->lookup_table, <e_new);
+ if (ret)
+ return ret;
+ if (lte_new != lte) {
+ /* Duplicate stream detected. */
+
+ if (lte_new->will_be_in_output_wim ||
+ stream_filtered(lte_new, ctx->filter_ctx))
+ {
+ /* The duplicate stream is already being
+ * included in the output WIM, or it would be
+ * filtered out if it had been. Skip writing
+ * this stream (and reading it again) entirely,
+ * passing its output reference count to the
+ * duplicate stream in the former case. */
+ DEBUG("Discarding duplicate stream of "
+ "length %"PRIu64, lte->size);
+ do_write_streams_progress(&ctx->progress_data,
+ lte, true);
+ list_del(<e->write_streams_list);
+ list_del(<e->lookup_table_list);
+ if (lte_new->will_be_in_output_wim)
+ lte_new->out_refcnt += lte->out_refcnt;
+ if (ctx->write_resource_flags & WIMLIB_WRITE_RESOURCE_FLAG_PACK_STREAMS)
+ ctx->cur_write_res_size -= lte->size;
+ free_lookup_table_entry(lte);
+ return BEGIN_STREAM_STATUS_SKIP_STREAM;
+ } else {
+ /* The duplicate stream can validly be written,
+ * but was not marked as such. Discard the
+ * current stream entry and use the duplicate,
+ * but actually freeing the current entry must
+ * wait until read_stream_list() has finished
+ * reading its data. */
+ DEBUG("Stream duplicate, but not already "
+ "selected for writing.");
+ list_replace(<e->write_streams_list,
+ <e_new->write_streams_list);
+ list_replace(<e->lookup_table_list,
+ <e_new->lookup_table_list);
+ lte_new->out_refcnt = lte->out_refcnt;
+ lte_new->will_be_in_output_wim = 1;
+ ctx->stream_was_duplicate = true;
+ lte = lte_new;
+ }
+ }