+/*
+ * Build a list of streams (via `struct wim_lookup_table_entry's) included in
+ * the "logical write" of the WIM, meaning all streams that are referenced at
+ * least once by dentries in the the image(s) being written. 'out_refcnt' on
+ * each stream being included in the logical write is set to the number of
+ * references from dentries in the image(s). Furthermore, 'unique_size' on each
+ * stream being included in the logical write is set to indicate whether that
+ * stream has a unique size relative to the streams being included in the
+ * logical write. Still furthermore, 'part_number' on each stream being
+ * included in the logical write is set to the part number given in the
+ * in-memory header of @p wim.
+ *
+ * This is considered a "logical write" because it does not take into account
+ * filtering out streams already present in the WIM (in the case of an in place
+ * overwrite) or present in other WIMs (in case of creating delta WIM).
+ */
+static int
+prepare_logical_stream_list(WIMStruct *wim, int image, bool streams_ok,
+ struct find_streams_ctx *ctx)
+{
+ int ret;
+ struct wim_lookup_table_entry *lte;
+
+ if (streams_ok && (image == WIMLIB_ALL_IMAGES ||
+ (image == 1 && wim->hdr.image_count == 1)))
+ {
+ /* Fast case: Assume that all streams are being written and
+ * that the reference counts are correct. */
+ struct wim_lookup_table_entry *lte;
+ struct wim_image_metadata *imd;
+ unsigned i;
+
+ for_lookup_table_entry(wim->lookup_table,
+ do_lte_full_reference_for_logical_write, ctx);
+ for (i = 0; i < wim->hdr.image_count; i++) {
+ imd = wim->image_metadata[i];
+ image_for_each_unhashed_stream(lte, imd)
+ do_lte_full_reference_for_logical_write(lte, ctx);
+ }
+ } else {
+ /* Slow case: Walk through the images being written and
+ * determine the streams referenced. */
+ for_lookup_table_entry(wim->lookup_table, lte_zero_out_refcnt, NULL);
+ wim->private = ctx;
+ ret = for_image(wim, image, image_find_streams_to_write);
+ if (ret)
+ return ret;
+ }
+
+ list_for_each_entry(lte, &ctx->stream_list, write_streams_list)
+ lte->part_number = wim->hdr.part_number;
+ return 0;
+}
+
+static int
+process_filtered_stream(struct wim_lookup_table_entry *lte, void *_ctx)
+{
+ struct find_streams_ctx *ctx = _ctx;
+ u16 filtered = 0;
+
+ /* Calculate and set lte->filtered. */
+ if (lte->resource_location == RESOURCE_IN_WIM) {
+ if (lte->wim == ctx->wim &&
+ (ctx->write_flags & WIMLIB_WRITE_FLAG_OVERWRITE))
+ filtered |= FILTERED_SAME_WIM;
+ if (lte->wim != ctx->wim &&
+ (ctx->write_flags & WIMLIB_WRITE_FLAG_SKIP_EXTERNAL_WIMS))
+ filtered |= FILTERED_EXTERNAL_WIM;
+ }
+ lte->filtered = filtered;
+
+ /* Filtered streams get inserted into the stream size table too, unless
+ * they already were. This is because streams that are checksummed
+ * on-the-fly during the write should not be written if they are
+ * duplicates of filtered stream. */
+ if (lte->filtered && lte->out_refcnt == 0)
+ stream_size_table_insert(lte, &ctx->stream_size_tab);
+ return 0;
+}
+
+static int
+mark_stream_not_filtered(struct wim_lookup_table_entry *lte, void *_ignore)
+{
+ lte->filtered = 0;
+ return 0;
+}
+
+/* Given the list of streams to include in a logical write of a WIM, handle
+ * filtering out streams already present in the WIM or already present in
+ * external WIMs, depending on the write flags provided. */
+static void
+handle_stream_filtering(struct find_streams_ctx *ctx)
+{
+ struct wim_lookup_table_entry *lte, *tmp;
+
+ if (!(ctx->write_flags & (WIMLIB_WRITE_FLAG_OVERWRITE |
+ WIMLIB_WRITE_FLAG_SKIP_EXTERNAL_WIMS)))
+ {
+ for_lookup_table_entry(ctx->wim->lookup_table,
+ mark_stream_not_filtered, ctx);
+ return;
+ }
+
+ for_lookup_table_entry(ctx->wim->lookup_table,
+ process_filtered_stream, ctx);
+
+ /* Streams in logical write list that were filtered can be removed. */
+ list_for_each_entry_safe(lte, tmp, &ctx->stream_list,
+ write_streams_list)
+ if (lte->filtered)
+ list_del(<e->write_streams_list);
+}
+
+/* Prepares list of streams to write for the specified WIM image(s). This wraps
+ * around prepare_logical_stream_list() to handle filtering out streams already
+ * present in the WIM or already present in external WIMs, depending on the
+ * write flags provided.
+ *
+ * Note: some additional data is stored in each `struct wim_lookup_table_entry':
+ *
+ * - 'out_refcnt' is set to the number of references found for the logical write.
+ * This will be nonzero on all streams in the list returned by this function,
+ * but will also be nonzero on streams not in the list that were included in
+ * the logical write list, but filtered out from the returned list.
+ * - 'filtered' is set to nonzero if the stream was filtered. Filtered streams
+ * are not included in the list of streams returned by this function.
+ * - 'unique_size' is set if the stream has a unique size among all streams in
+ * the logical write plus any filtered streams in the entire WIM that could
+ * potentially turn out to have the same checksum as a yet-to-be-checksummed
+ * stream being written.
+ */
+static int
+prepare_stream_list(WIMStruct *wim, int image, int write_flags,
+ struct list_head *stream_list)
+{
+ int ret;
+ bool streams_ok;
+ struct find_streams_ctx ctx;
+
+ INIT_LIST_HEAD(&ctx.stream_list);
+ ret = init_stream_size_table(&ctx.stream_size_tab,
+ wim->lookup_table->capacity);
+ if (ret)
+ return ret;
+ ctx.write_flags = write_flags;
+ ctx.wim = wim;
+
+ streams_ok = ((write_flags & WIMLIB_WRITE_FLAG_STREAMS_OK) != 0);
+
+ ret = prepare_logical_stream_list(wim, image, streams_ok, &ctx);
+ if (ret)
+ goto out_destroy_table;
+
+ handle_stream_filtering(&ctx);
+ list_transfer(&ctx.stream_list, stream_list);
+ ret = 0;
+out_destroy_table:
+ destroy_stream_size_table(&ctx.stream_size_tab);
+ return ret;
+}
+
+static int
+write_wim_streams(WIMStruct *wim, int image, int write_flags,
+ unsigned num_threads,
+ wimlib_progress_func_t progress_func,
+ struct list_head *stream_list_override)
+{
+ int ret;
+ struct list_head _stream_list;
+ struct list_head *stream_list;
+ struct wim_lookup_table_entry *lte;
+
+ if (stream_list_override == NULL) {
+ /* Normal case: prepare stream list from image(s) being written.
+ */
+ stream_list = &_stream_list;
+ ret = prepare_stream_list(wim, image, write_flags, stream_list);
+ if (ret)
+ return ret;
+ } else {
+ /* Currently only as a result of wimlib_split() being called:
+ * use stream list already explicitly provided. Use existing
+ * reference counts. */
+ stream_list = stream_list_override;
+ list_for_each_entry(lte, stream_list, write_streams_list) {
+ lte->out_refcnt = (lte->refcnt ? lte->refcnt : 1);
+ lte->part_number = wim->hdr.part_number;
+ }
+ }
+
+ return write_stream_list(stream_list,
+ wim->lookup_table,
+ &wim->out_fd,
+ wim->compression_type,
+ &wim->lzx_context,
+ write_flags,
+ num_threads,
+ progress_func);
+}
+
+static int
+write_wim_metadata_resources(WIMStruct *wim, int image, int write_flags,