+ 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,
+ write_flags,
+ num_threads,
+ progress_func);
+}
+
+static int
+write_wim_metadata_resources(WIMStruct *wim, int image, int write_flags,
+ wimlib_progress_func_t progress_func)
+{
+ int ret;
+ int start_image;
+ int end_image;
+ int write_resource_flags;
+
+ if (write_flags & WIMLIB_WRITE_FLAG_NO_METADATA) {
+ DEBUG("Not writing any metadata resources.");
+ return 0;
+ }
+
+ write_resource_flags = write_flags_to_resource_flags(write_flags);
+
+ DEBUG("Writing metadata resources (offset=%"PRIu64")",
+ wim->out_fd.offset);
+
+ if (progress_func)
+ progress_func(WIMLIB_PROGRESS_MSG_WRITE_METADATA_BEGIN, NULL);
+
+ if (image == WIMLIB_ALL_IMAGES) {
+ start_image = 1;
+ end_image = wim->hdr.image_count;
+ } else {
+ start_image = image;
+ end_image = image;
+ }
+
+ for (int i = start_image; i <= end_image; i++) {
+ struct wim_image_metadata *imd;
+
+ imd = wim->image_metadata[i - 1];
+ /* Build a new metadata resource only if image was modified from
+ * the original (or was newly added). Otherwise just copy the
+ * existing one. */
+ if (imd->modified) {
+ DEBUG("Image %u was modified; building and writing new "
+ "metadata resource", i);
+ ret = write_metadata_resource(wim, i,
+ write_resource_flags);
+ } else if (write_flags & WIMLIB_WRITE_FLAG_OVERWRITE) {
+ DEBUG("Image %u was not modified; re-using existing "
+ "metadata resource.", i);
+ copy_resource_entry(&imd->metadata_lte->output_resource_entry,
+ &imd->metadata_lte->resource_entry);
+ ret = 0;
+ } else {
+ DEBUG("Image %u was not modified; copying existing "
+ "metadata resource.", i);
+ ret = write_wim_resource(imd->metadata_lte,
+ &wim->out_fd,
+ wim->compression_type,
+ &imd->metadata_lte->output_resource_entry,
+ write_resource_flags);
+ }
+ if (ret)
+ return ret;
+ }
+ if (progress_func)
+ progress_func(WIMLIB_PROGRESS_MSG_WRITE_METADATA_END, NULL);
+ return 0;
+}
+
+static int
+open_wim_writable(WIMStruct *wim, const tchar *path, int open_flags)
+{
+ int raw_fd;
+ DEBUG("Opening \"%"TS"\" for writing.", path);
+
+ raw_fd = topen(path, open_flags | O_BINARY, 0644);
+ if (raw_fd < 0) {
+ ERROR_WITH_ERRNO("Failed to open \"%"TS"\" for writing", path);
+ return WIMLIB_ERR_OPEN;
+ }
+ filedes_init(&wim->out_fd, raw_fd);
+ return 0;
+}
+
+static int
+close_wim_writable(WIMStruct *wim, int write_flags)
+{
+ int ret = 0;
+
+ if (!(write_flags & WIMLIB_WRITE_FLAG_FILE_DESCRIPTOR))
+ if (filedes_valid(&wim->out_fd))
+ if (filedes_close(&wim->out_fd))
+ ret = WIMLIB_ERR_WRITE;
+ filedes_invalidate(&wim->out_fd);
+ return ret;