+ 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;
+ }
+ }
+ }
+ list_move_tail(<e->write_streams_list, &ctx->pending_streams);
+ return 0;
+}
+
+/* Write the next chunk of (typically compressed) data to the output WIM,
+ * handling the writing of the chunk table. */
+static int
+write_chunk(struct write_streams_ctx *ctx, const void *cchunk,
+ size_t csize, size_t usize)
+{
+ int ret;
+
+ if (ctx->cur_write_res_offset == 0 &&
+ !(ctx->write_resource_flags & WIMLIB_WRITE_RESOURCE_FLAG_PACK_STREAMS))
+ {
+ /* Starting to write a new stream in non-packed mode. */
+
+ struct wim_lookup_table_entry *lte;
+
+ lte = list_entry(ctx->pending_streams.next,
+ struct wim_lookup_table_entry, write_streams_list);
+
+ if (ctx->write_resource_flags & WIMLIB_WRITE_RESOURCE_FLAG_PIPABLE) {
+ int additional_reshdr_flags = 0;
+ if (ctx->compressor != NULL)
+ additional_reshdr_flags |= WIM_RESHDR_FLAG_COMPRESSED;
+
+ DEBUG("Writing pipable WIM stream header "
+ "(offset=%"PRIu64")", ctx->out_fd->offset);
+
+ ret = write_pwm_stream_header(lte, ctx->out_fd,
+ additional_reshdr_flags);
+ if (ret)
+ return ret;
+ }
+
+ ret = begin_write_resource(ctx, lte->size);
+ if (ret)
+ return ret;
+ }
+
+ if (ctx->compressor != NULL) {
+ /* Record the compresed chunk size. */
+ wimlib_assert(ctx->chunk_index < ctx->num_alloc_chunks);
+ ctx->chunk_csizes[ctx->chunk_index++] = csize;
+
+ /* If writing a pipable WIM, before the chunk data write a chunk
+ * header that provides the compressed chunk size. */
+ if (ctx->write_resource_flags & WIMLIB_WRITE_RESOURCE_FLAG_PIPABLE) {
+ struct pwm_chunk_hdr chunk_hdr = {
+ .compressed_size = cpu_to_le32(csize),
+ };
+ ret = full_write(ctx->out_fd, &chunk_hdr,
+ sizeof(chunk_hdr));
+ if (ret)
+ goto error;
+ }
+ }
+
+ /* Write the chunk data. */
+ ret = full_write(ctx->out_fd, cchunk, csize);
+ if (ret)
+ goto error;
+
+ ctx->cur_write_res_offset += usize;
+
+ if (ctx->cur_write_res_offset == ctx->cur_write_res_size &&
+ !(ctx->write_resource_flags & WIMLIB_WRITE_RESOURCE_FLAG_PACK_STREAMS))
+ {
+ struct wim_lookup_table_entry *lte;
+
+ lte = list_entry(ctx->pending_streams.next,
+ struct wim_lookup_table_entry, write_streams_list);
+ wimlib_assert(ctx->cur_write_res_offset == lte->size);
+
+ /* Finished writing a stream in non-packed mode. */
+
+ do_write_streams_progress(&ctx->progress_data, lte, false);
+
+ ret = end_write_resource(ctx, <e->out_reshdr);
+ if (ret)
+ return ret;
+
+ wimlib_assert(lte->out_reshdr.uncompressed_size == lte->size);
+
+ lte->out_reshdr.flags = filter_resource_flags(lte->flags);
+ if (ctx->compressor != NULL)
+ lte->out_reshdr.flags |= WIM_RESHDR_FLAG_COMPRESSED;
+
+ list_del(<e->write_streams_list);
+ ctx->cur_write_res_offset = 0;
+ }
+
+ return 0;
+
+error:
+ ERROR_WITH_ERRNO("Write error");
+ return ret;