+ struct stream_size_table stream_size_tab;
+};
+
+static void
+reference_stream_for_write(struct wim_lookup_table_entry *lte,
+ struct list_head *stream_list, u32 nref)
+{
+ if (!lte->will_be_in_output_wim) {
+ lte->out_refcnt = 0;
+ list_add_tail(<e->write_streams_list, stream_list);
+ lte->will_be_in_output_wim = 1;
+ }
+ lte->out_refcnt += nref;
+}
+
+static int
+fully_reference_stream_for_write(struct wim_lookup_table_entry *lte,
+ void *_stream_list)
+{
+ struct list_head *stream_list = _stream_list;
+ lte->will_be_in_output_wim = 0;
+ reference_stream_for_write(lte, stream_list, lte->refcnt);
+ return 0;
+}
+
+static int
+inode_find_streams_to_reference(const struct wim_inode *inode,
+ const struct wim_lookup_table *table,
+ struct list_head *stream_list)
+{
+ struct wim_lookup_table_entry *lte;
+ unsigned i;
+
+ wimlib_assert(inode->i_nlink > 0);
+
+ for (i = 0; i <= inode->i_num_ads; i++) {
+ lte = inode_stream_lte(inode, i, table);
+ if (lte)
+ reference_stream_for_write(lte, stream_list,
+ inode->i_nlink);
+ else if (!is_zero_hash(inode_stream_hash(inode, i)))
+ return WIMLIB_ERR_RESOURCE_NOT_FOUND;
+ }
+ return 0;
+}
+
+static int
+do_stream_set_not_in_output_wim(struct wim_lookup_table_entry *lte, void *_ignore)
+{
+ lte->will_be_in_output_wim = 0;
+ return 0;
+}
+
+static int
+image_find_streams_to_reference(WIMStruct *wim)
+{
+ struct wim_image_metadata *imd;
+ struct wim_inode *inode;
+ struct wim_lookup_table_entry *lte;
+ struct list_head *stream_list;
+ int ret;
+
+ imd = wim_get_current_image_metadata(wim);
+
+ image_for_each_unhashed_stream(lte, imd)
+ lte->will_be_in_output_wim = 0;
+
+ stream_list = wim->private;
+ image_for_each_inode(inode, imd) {
+ ret = inode_find_streams_to_reference(inode,
+ wim->lookup_table,
+ stream_list);
+ if (ret)
+ return ret;
+ }
+ return 0;
+}
+
+static int
+prepare_unfiltered_list_of_streams_in_output_wim(WIMStruct *wim,
+ int image,
+ int streams_ok,
+ struct list_head *stream_list_ret)
+{
+ int ret;
+
+ INIT_LIST_HEAD(stream_list_ret);
+
+ 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,
+ fully_reference_stream_for_write,
+ stream_list_ret);
+
+ for (i = 0; i < wim->hdr.image_count; i++) {
+ imd = wim->image_metadata[i];
+ image_for_each_unhashed_stream(lte, imd)
+ fully_reference_stream_for_write(lte, stream_list_ret);
+ }
+ } else {
+ /* Slow case: Walk through the images being written and
+ * determine the streams referenced. */
+ for_lookup_table_entry(wim->lookup_table,
+ do_stream_set_not_in_output_wim, NULL);
+ wim->private = stream_list_ret;
+ ret = for_image(wim, image, image_find_streams_to_reference);
+ if (ret)
+ return ret;
+ }
+
+ return 0;
+}
+
+struct insert_other_if_hard_filtered_ctx {
+ struct stream_size_table *tab;
+ struct filter_context *filter_ctx;
+};
+
+static int
+insert_other_if_hard_filtered(struct wim_lookup_table_entry *lte, void *_ctx)
+{
+ struct insert_other_if_hard_filtered_ctx *ctx = _ctx;
+
+ if (!lte->will_be_in_output_wim &&
+ stream_hard_filtered(lte, ctx->filter_ctx))
+ stream_size_table_insert(lte, ctx->tab);
+ return 0;
+}
+
+static int
+determine_stream_size_uniquity(struct list_head *stream_list,
+ struct wim_lookup_table *lt,
+ struct filter_context *filter_ctx)
+{
+ int ret;
+ struct stream_size_table tab;
+ struct wim_lookup_table_entry *lte;
+
+ ret = init_stream_size_table(&tab, 9001);
+ if (ret)
+ return ret;
+
+ if (may_hard_filter_streams(filter_ctx)) {
+ struct insert_other_if_hard_filtered_ctx ctx = {
+ .tab = &tab,
+ .filter_ctx = filter_ctx,
+ };
+ for_lookup_table_entry(lt, insert_other_if_hard_filtered, &ctx);
+ }
+
+ list_for_each_entry(lte, stream_list, write_streams_list)
+ stream_size_table_insert(lte, &tab);
+
+ destroy_stream_size_table(&tab);
+ return 0;
+}
+
+static void
+filter_stream_list_for_write(struct list_head *stream_list,
+ struct filter_context *filter_ctx)
+{
+ struct wim_lookup_table_entry *lte, *tmp;
+
+ list_for_each_entry_safe(lte, tmp,
+ stream_list, write_streams_list)
+ {
+ int status = stream_filtered(lte, filter_ctx);
+
+ if (status == 0) {
+ /* Not filtered. */
+ continue;
+ } else {
+ if (status > 0) {
+ /* Soft filtered. */
+ } else {
+ /* Hard filtered. */
+ lte->will_be_in_output_wim = 0;
+ list_del(<e->lookup_table_list);
+ }
+ list_del(<e->write_streams_list);
+ }
+ }
+}
+
+/*
+ * prepare_stream_list_for_write() -
+ *
+ * Prepare the list of streams to write for writing a WIM containing the
+ * specified image(s) with the specified write flags.
+ *
+ * @wim
+ * The WIMStruct on whose behalf the write is occurring.
+ *
+ * @image
+ * Image(s) from the WIM to write; may be WIMLIB_ALL_IMAGES.
+ *
+ * @write_flags
+ * WIMLIB_WRITE_FLAG_* flags for the write operation:
+ *
+ * STREAMS_OK: For writes of all images, assume that all streams in the
+ * lookup table of @wim and the per-image lists of unhashed streams should
+ * be taken as-is, and image metadata should not be searched for
+ * references. This does not exclude filtering with OVERWRITE and
+ * SKIP_EXTERNAL_WIMS, below.
+ *
+ * OVERWRITE: Streams already present in @wim shall not be returned in
+ * @stream_list_ret.
+ *
+ * SKIP_EXTERNAL_WIMS: Streams already present in a WIM file, but not
+ * @wim, shall be be returned in neither @stream_list_ret nor
+ * @lookup_table_list_ret.
+ *
+ * @stream_list_ret
+ * List of streams, linked by write_streams_list, that need to be written
+ * will be returned here.
+ *
+ * Note that this function assumes that unhashed streams will be written;
+ * it does not take into account that they may become duplicates when
+ * actually hashed.
+ *
+ * @lookup_table_list_ret
+ * List of streams, linked by lookup_table_list, that need to be included
+ * in the WIM's lookup table will be returned here. This will be a
+ * superset of the streams in @stream_list_ret.
+ *
+ * This list will be a proper superset of @stream_list_ret if and only if
+ * WIMLIB_WRITE_FLAG_OVERWRITE was specified in @write_flags and some of
+ * the streams that would otherwise need to be written were already located
+ * in the WIM file.
+ *
+ * All streams in this list will have @out_refcnt set to the number of
+ * references to the stream in the output WIM. If
+ * WIMLIB_WRITE_FLAG_STREAMS_OK was specified in @write_flags, @out_refcnt
+ * may be as low as 0.
+ *
+ * @filter_ctx_ret
+ * A context for queries of stream filter status with stream_filtered() is
+ * returned in this location.
+ *
+ * In addition, @will_be_in_output_wim will be set to 1 in all stream entries
+ * inserted into @lookup_table_list_ret and to 0 in all stream entries in the
+ * lookup table of @wim not inserted into @lookup_table_list_ret.
+ *
+ * Still furthermore, @unique_size will be set to 1 on all stream entries in
+ * @stream_list_ret that have unique size among all stream entries in
+ * @stream_list_ret and among all stream entries in the lookup table of @wim
+ * that are ineligible for being written due to filtering.
+ *
+ * Returns 0 on success; nonzero on read error, memory allocation error, or
+ * otherwise.
+ */
+static int
+prepare_stream_list_for_write(WIMStruct *wim, int image,
+ int write_flags,
+ struct list_head *stream_list_ret,
+ struct list_head *lookup_table_list_ret,
+ struct filter_context *filter_ctx_ret)
+{
+ int ret;
+ struct wim_lookup_table_entry *lte;
+
+ filter_ctx_ret->write_flags = write_flags;
+ filter_ctx_ret->wim = wim;
+
+ ret = prepare_unfiltered_list_of_streams_in_output_wim(
+ wim,
+ image,
+ write_flags & WIMLIB_WRITE_FLAG_STREAMS_OK,
+ stream_list_ret);
+ if (ret)
+ return ret;
+
+ INIT_LIST_HEAD(lookup_table_list_ret);
+ list_for_each_entry(lte, stream_list_ret, write_streams_list)
+ list_add_tail(<e->lookup_table_list, lookup_table_list_ret);
+
+ ret = determine_stream_size_uniquity(stream_list_ret, wim->lookup_table,
+ filter_ctx_ret);
+ if (ret)
+ return ret;
+
+ if (may_filter_streams(filter_ctx_ret))
+ filter_stream_list_for_write(stream_list_ret, filter_ctx_ret);
+
+ return 0;
+}
+
+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,
+ struct list_head *lookup_table_list_ret)
+{
+ int ret;
+ struct list_head _stream_list;
+ struct list_head *stream_list;
+ struct wim_lookup_table_entry *lte;
+ struct filter_context _filter_ctx;
+ struct filter_context *filter_ctx;
+
+ if (stream_list_override == NULL) {
+ /* Normal case: prepare stream list from image(s) being written.
+ */
+ stream_list = &_stream_list;
+ filter_ctx = &_filter_ctx;
+ ret = prepare_stream_list_for_write(wim, image, write_flags,
+ stream_list,
+ lookup_table_list_ret,
+ filter_ctx);
+ 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;
+ filter_ctx = NULL;
+ INIT_LIST_HEAD(lookup_table_list_ret);
+ list_for_each_entry(lte, stream_list, write_streams_list) {
+ lte->out_refcnt = lte->refcnt;
+ lte->will_be_in_output_wim = 1;
+ lte->unique_size = 0;
+ list_add_tail(<e->lookup_table_list, lookup_table_list_ret);
+ }
+ }
+
+ return wim_write_stream_list(wim,
+ stream_list,
+ write_flags,
+ num_threads,
+ filter_ctx,
+ 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);
+
+ write_resource_flags &= ~WRITE_RESOURCE_FLAG_PACK_STREAMS;
+
+ 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);
+ stream_set_out_reshdr_for_reuse(imd->metadata_lte);
+ 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->out_compression_type,
+ wim->out_chunk_size,
+ 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)) {
+ DEBUG("Closing WIM file.");
+ if (filedes_valid(&wim->out_fd))
+ if (filedes_close(&wim->out_fd))
+ ret = WIMLIB_ERR_WRITE;
+ }
+ filedes_invalidate(&wim->out_fd);
+ return ret;
+}
+
+static int
+cmp_streams_by_out_rspec(const void *p1, const void *p2)
+{
+ const struct wim_lookup_table_entry *lte1, *lte2;
+
+ lte1 = *(const struct wim_lookup_table_entry**)p1;
+ lte2 = *(const struct wim_lookup_table_entry**)p2;
+
+ if (lte1->out_reshdr.flags & WIM_RESHDR_FLAG_PACKED_STREAMS) {
+ if (lte2->out_reshdr.flags & WIM_RESHDR_FLAG_PACKED_STREAMS) {
+ if (lte1->out_res_offset_in_wim != lte2->out_res_offset_in_wim)
+ return cmp_u64(lte1->out_res_offset_in_wim,
+ lte2->out_res_offset_in_wim);
+ } else {
+ return 1;
+ }
+ } else {
+ if (lte2->out_reshdr.flags & WIM_RESHDR_FLAG_PACKED_STREAMS)
+ return -1;
+ }
+ return cmp_u64(lte1->out_reshdr.offset_in_wim,
+ lte2->out_reshdr.offset_in_wim);
+}
+
+static int
+write_wim_lookup_table(WIMStruct *wim, int image, int write_flags,
+ struct wim_reshdr *out_reshdr,
+ struct list_head *lookup_table_list)
+{
+ int ret;
+
+ /* Set output resource metadata for streams already present in WIM. */
+ if (write_flags & WIMLIB_WRITE_FLAG_OVERWRITE) {
+ struct wim_lookup_table_entry *lte;
+ list_for_each_entry(lte, lookup_table_list, lookup_table_list)
+ {
+ if (lte->resource_location == RESOURCE_IN_WIM &&
+ lte->rspec->wim == wim)
+ {
+ stream_set_out_reshdr_for_reuse(lte);
+ }
+ }
+ }
+
+ ret = sort_stream_list(lookup_table_list,
+ offsetof(struct wim_lookup_table_entry, lookup_table_list),
+ cmp_streams_by_out_rspec);
+ if (ret)
+ return ret;
+
+ /* Add entries for metadata resources. */
+ if (!(write_flags & WIMLIB_WRITE_FLAG_NO_METADATA)) {
+ int start_image;
+ int end_image;
+
+ if (image == WIMLIB_ALL_IMAGES) {
+ start_image = 1;
+ end_image = wim->hdr.image_count;
+ } else {
+ start_image = image;
+ end_image = image;
+ }
+
+ /* Push metadata resource lookup table entries onto the front of
+ * the list in reverse order, so that they're written in order.
+ */
+ for (int i = end_image; i >= start_image; i--) {
+ struct wim_lookup_table_entry *metadata_lte;
+
+ metadata_lte = wim->image_metadata[i - 1]->metadata_lte;
+ wimlib_assert(metadata_lte->out_reshdr.flags & WIM_RESHDR_FLAG_METADATA);
+ metadata_lte->out_refcnt = 1;
+ list_add(&metadata_lte->lookup_table_list, lookup_table_list);
+ }
+ }