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