+ 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);