+ comp_ctx,
+ write_resource_flags,
+ progress_data);
+
+}
+#endif
+
+/*
+ * Write a list of streams to a WIM (@out_fd) using the compression type
+ * @out_ctype and up to @num_threads compressor threads.
+ */
+static int
+write_stream_list(struct list_head *stream_list,
+ struct wim_lookup_table *lookup_table,
+ struct filedes *out_fd, int out_ctype,
+ struct wimlib_lzx_context **comp_ctx,
+ int write_flags,
+ unsigned num_threads, wimlib_progress_func_t progress_func)
+{
+ struct wim_lookup_table_entry *lte;
+ size_t num_streams = 0;
+ u64 total_bytes = 0;
+ u64 total_compression_bytes = 0;
+ struct write_streams_progress_data progress_data;
+ int ret;
+ int write_resource_flags;
+ unsigned total_parts = 0;
+ WIMStruct *prev_wim_part = NULL;
+
+ if (list_empty(stream_list)) {
+ DEBUG("No streams to write.");
+ return 0;
+ }
+
+ write_resource_flags = write_flags_to_resource_flags(write_flags);
+
+ DEBUG("Writing stream list (offset = %"PRIu64", write_resource_flags=0x%08x)",
+ out_fd->offset, write_resource_flags);
+
+ sort_stream_list_by_sequential_order(stream_list,
+ offsetof(struct wim_lookup_table_entry,
+ write_streams_list));
+
+ /* Calculate the total size of the streams to be written. Note: this
+ * will be the uncompressed size, as we may not know the compressed size
+ * yet, and also this will assume that every unhashed stream will be
+ * written (which will not necessarily be the case). */
+ list_for_each_entry(lte, stream_list, write_streams_list) {
+ num_streams++;
+ total_bytes += wim_resource_size(lte);
+ if (out_ctype != WIMLIB_COMPRESSION_TYPE_NONE
+ && (wim_resource_compression_type(lte) != out_ctype ||
+ (write_resource_flags & WIMLIB_WRITE_RESOURCE_FLAG_RECOMPRESS)))
+ {
+ total_compression_bytes += wim_resource_size(lte);
+ }
+ if (lte->resource_location == RESOURCE_IN_WIM) {
+ if (prev_wim_part != lte->wim) {
+ prev_wim_part = lte->wim;
+ total_parts++;
+ }
+ }
+ }
+
+ memset(&progress_data, 0, sizeof(progress_data));
+ progress_data.progress_func = progress_func;
+
+ progress_data.progress.write_streams.total_bytes = total_bytes;
+ progress_data.progress.write_streams.total_streams = num_streams;
+ progress_data.progress.write_streams.completed_bytes = 0;
+ progress_data.progress.write_streams.completed_streams = 0;
+ progress_data.progress.write_streams.num_threads = num_threads;
+ progress_data.progress.write_streams.compression_type = out_ctype;
+ progress_data.progress.write_streams.total_parts = total_parts;
+ progress_data.progress.write_streams.completed_parts = 0;
+
+ progress_data.next_progress = 0;
+ progress_data.prev_wim_part = NULL;
+
+#ifdef ENABLE_MULTITHREADED_COMPRESSION
+ if (total_compression_bytes >= 2000000 && num_threads != 1)
+ ret = write_stream_list_parallel(stream_list,
+ lookup_table,
+ out_fd,
+ out_ctype,
+ comp_ctx,
+ write_resource_flags,
+ &progress_data,
+ num_threads);
+ else
+#endif
+ ret = write_stream_list_serial(stream_list,
+ lookup_table,
+ out_fd,
+ out_ctype,
+ comp_ctx,
+ write_resource_flags,
+ &progress_data);
+ if (ret == 0)
+ DEBUG("Successfully wrote stream list.");
+ else
+ DEBUG("Failed to write stream list.");
+ return ret;
+}
+
+struct stream_size_table {
+ struct hlist_head *array;
+ size_t num_entries;
+ size_t capacity;
+};
+
+static int
+init_stream_size_table(struct stream_size_table *tab, size_t capacity)
+{
+ tab->array = CALLOC(capacity, sizeof(tab->array[0]));
+ if (!tab->array)
+ return WIMLIB_ERR_NOMEM;
+ tab->num_entries = 0;
+ tab->capacity = capacity;
+ return 0;
+}
+
+static void
+destroy_stream_size_table(struct stream_size_table *tab)
+{
+ FREE(tab->array);
+}
+
+static int
+stream_size_table_insert(struct wim_lookup_table_entry *lte, void *_tab)
+{
+ struct stream_size_table *tab = _tab;
+ size_t pos;
+ struct wim_lookup_table_entry *same_size_lte;
+ struct hlist_node *tmp;
+
+ pos = hash_u64(wim_resource_size(lte)) % tab->capacity;
+ lte->unique_size = 1;
+ hlist_for_each_entry(same_size_lte, tmp, &tab->array[pos], hash_list_2) {
+ if (wim_resource_size(same_size_lte) == wim_resource_size(lte)) {
+ lte->unique_size = 0;
+ same_size_lte->unique_size = 0;
+ break;
+ }
+ }
+
+ hlist_add_head(<e->hash_list_2, &tab->array[pos]);
+ tab->num_entries++;
+ return 0;
+}
+
+struct find_streams_ctx {
+ WIMStruct *wim;
+ int write_flags;
+ struct list_head stream_list;
+ struct stream_size_table stream_size_tab;
+};
+
+static void
+lte_reference_for_logical_write(struct wim_lookup_table_entry *lte,
+ struct find_streams_ctx *ctx,
+ unsigned nref)
+{
+ if (lte->out_refcnt == 0) {
+ stream_size_table_insert(lte, &ctx->stream_size_tab);
+ list_add_tail(<e->write_streams_list, &ctx->stream_list);
+ }
+ lte->out_refcnt += nref;
+}
+
+static int
+do_lte_full_reference_for_logical_write(struct wim_lookup_table_entry *lte,
+ void *_ctx)
+{
+ struct find_streams_ctx *ctx = _ctx;
+ lte->out_refcnt = 0;
+ lte_reference_for_logical_write(lte, ctx,
+ (lte->refcnt ? lte->refcnt : 1));
+ return 0;
+}
+
+static int
+inode_find_streams_to_write(struct wim_inode *inode,
+ struct wim_lookup_table *table,
+ struct find_streams_ctx *ctx)
+{
+ struct wim_lookup_table_entry *lte;
+ unsigned i;
+
+ for (i = 0; i <= inode->i_num_ads; i++) {
+ lte = inode_stream_lte(inode, i, table);
+ if (lte)
+ lte_reference_for_logical_write(lte, ctx, inode->i_nlink);
+ else if (!is_zero_hash(inode_stream_hash(inode, i)))
+ return WIMLIB_ERR_RESOURCE_NOT_FOUND;
+ }
+ return 0;
+}
+
+static int
+image_find_streams_to_write(WIMStruct *wim)
+{
+ struct find_streams_ctx *ctx;
+ struct wim_image_metadata *imd;
+ struct wim_inode *inode;
+ struct wim_lookup_table_entry *lte;
+ int ret;
+
+ ctx = wim->private;
+ imd = wim_get_current_image_metadata(wim);
+
+ image_for_each_unhashed_stream(lte, imd)
+ lte->out_refcnt = 0;
+
+ /* Go through this image's inodes to find any streams that have not been
+ * found yet. */
+ image_for_each_inode(inode, imd) {
+ ret = inode_find_streams_to_write(inode, wim->lookup_table, ctx);
+ if (ret)
+ return ret;
+ }
+ return 0;
+}
+
+/*
+ * 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;
+ }
+ }