+ }
+ #endif
+
+ if (ctx.compressor == NULL) {
+ ret = new_serial_chunk_compressor(out_ctype, out_chunk_size,
+ &ctx.compressor);
+ if (ret)
+ goto out_destroy_context;
+ }
+ }
+
+ if (ctx.compressor)
+ ctx.progress_data.progress.write_streams.num_threads = ctx.compressor->num_threads;
+ else
+ ctx.progress_data.progress.write_streams.num_threads = 1;
+
+ DEBUG("Actually using %u threads",
+ ctx.progress_data.progress.write_streams.num_threads);
+
+ INIT_LIST_HEAD(&ctx.pending_streams);
+
+ if (ctx.progress_data.progress_func) {
+ (*ctx.progress_data.progress_func)(WIMLIB_PROGRESS_MSG_WRITE_STREAMS,
+ &ctx.progress_data.progress);
+ }
+
+ if (write_resource_flags & WRITE_RESOURCE_FLAG_PACK_STREAMS) {
+ ret = begin_write_resource(&ctx, ctx.num_bytes_to_compress);
+ if (ret)
+ goto out_destroy_context;
+ }
+
+ /* Read the list of streams needing to be compressed, using the
+ * specified callbacks to execute processing of the data. */
+
+ struct read_stream_list_callbacks cbs = {
+ .begin_stream = write_stream_begin_read,
+ .begin_stream_ctx = &ctx,
+ .consume_chunk = write_stream_process_chunk,
+ .consume_chunk_ctx = &ctx,
+ .end_stream = write_stream_end_read,
+ .end_stream_ctx = &ctx,
+ };
+
+ ret = read_stream_list(stream_list,
+ offsetof(struct wim_lookup_table_entry, write_streams_list),
+ &cbs,
+ STREAM_LIST_ALREADY_SORTED |
+ VERIFY_STREAM_HASHES |
+ COMPUTE_MISSING_STREAM_HASHES);
+
+ if (ret)
+ goto out_destroy_context;
+
+ ret = finish_remaining_chunks(&ctx);
+ if (ret)
+ goto out_destroy_context;
+
+ if (write_resource_flags & WRITE_RESOURCE_FLAG_PACK_STREAMS) {
+ struct wim_reshdr reshdr;
+ struct wim_lookup_table_entry *lte;
+ u64 offset_in_res;
+
+ ret = end_write_resource(&ctx, &reshdr);
+ if (ret)
+ goto out_destroy_context;
+
+ DEBUG("Ending packed resource: %lu %lu %lu.",
+ reshdr.offset_in_wim,
+ reshdr.size_in_wim,
+ reshdr.uncompressed_size);
+
+ offset_in_res = 0;
+ list_for_each_entry(lte, &ctx.pending_streams, write_streams_list) {
+ lte->out_reshdr.size_in_wim = lte->size;
+ lte->out_reshdr.flags = filter_resource_flags(lte->flags);
+ lte->out_reshdr.flags |= WIM_RESHDR_FLAG_PACKED_STREAMS;
+ lte->out_reshdr.uncompressed_size = 0;
+ lte->out_reshdr.offset_in_wim = offset_in_res;
+ lte->out_res_offset_in_wim = reshdr.offset_in_wim;
+ lte->out_res_size_in_wim = reshdr.size_in_wim;
+ /*lte->out_res_uncompressed_size = reshdr.uncompressed_size;*/
+ offset_in_res += lte->size;
+ }
+ wimlib_assert(offset_in_res == reshdr.uncompressed_size);
+ }
+
+out_write_raw_copy_resources:
+ /* Copy any compressed resources for which the raw data can be reused
+ * without decompression. */
+ ret = write_raw_copy_resources(&raw_copy_resources, ctx.out_fd,
+ &ctx.progress_data);
+
+out_destroy_context:
+ if (out_chunk_size > STACK_MAX)
+ FREE(ctx.chunk_buf);
+ FREE(ctx.chunk_csizes);
+ if (ctx.compressor)
+ ctx.compressor->destroy(ctx.compressor);
+ DEBUG("Done (ret=%d)", ret);
+ return ret;
+}
+
+static int
+write_wim_resource(struct wim_lookup_table_entry *lte,
+ struct filedes *out_fd,
+ int out_ctype,
+ u32 out_chunk_size,
+ int write_resource_flags)
+{
+ LIST_HEAD(stream_list);
+ list_add(<e->write_streams_list, &stream_list);
+ lte->will_be_in_output_wim = 1;
+ return write_stream_list(&stream_list,
+ out_fd,
+ write_resource_flags & ~WRITE_RESOURCE_FLAG_PACK_STREAMS,
+ out_ctype,
+ out_chunk_size,
+ 1,
+ NULL,
+ NULL,
+ NULL);
+}
+
+int
+write_wim_resource_from_buffer(const void *buf, size_t buf_size,
+ int reshdr_flags, struct filedes *out_fd,
+ int out_ctype,
+ u32 out_chunk_size,
+ struct wim_reshdr *out_reshdr,
+ u8 *hash,
+ int write_resource_flags)
+{
+ int ret;
+ struct wim_lookup_table_entry *lte;
+
+ /* Set up a temporary lookup table entry to provide to
+ * write_wim_resource(). */
+
+ lte = new_lookup_table_entry();
+ if (lte == NULL)
+ return WIMLIB_ERR_NOMEM;
+
+ lte->resource_location = RESOURCE_IN_ATTACHED_BUFFER;
+ lte->attached_buffer = (void*)buf;
+ lte->size = buf_size;
+ lte->flags = reshdr_flags;
+
+ if (write_resource_flags & WRITE_RESOURCE_FLAG_PIPABLE) {
+ sha1_buffer(buf, buf_size, lte->hash);
+ lte->unhashed = 0;
+ } else {
+ lte->unhashed = 1;
+ }
+
+ ret = write_wim_resource(lte, out_fd, out_ctype, out_chunk_size,
+ write_resource_flags);
+ if (ret)
+ goto out_free_lte;
+
+ copy_reshdr(out_reshdr, <e->out_reshdr);
+
+ if (hash)
+ copy_hash(hash, lte->hash);
+ ret = 0;
+out_free_lte:
+ lte->resource_location = RESOURCE_NONEXISTENT;
+ free_lookup_table_entry(lte);
+ 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 == NULL)
+ 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(lte->size) % tab->capacity;
+ lte->unique_size = 1;
+ hlist_for_each_entry(same_size_lte, tmp, &tab->array[pos], hash_list_2) {
+ if (same_size_lte->size == lte->size) {
+ 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
+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, lt->capacity);
+ 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 write_stream_list(stream_list,
+ &wim->out_fd,
+ write_flags_to_resource_flags(write_flags),
+ wim->out_compression_type,
+ wim->out_chunk_size,
+ num_threads,
+ wim->lookup_table,
+ 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);
+ }
+ }
+
+ return write_wim_lookup_table_from_stream_list(lookup_table_list,
+ &wim->out_fd,
+ wim->hdr.part_number,
+ out_reshdr,
+ write_flags_to_resource_flags(write_flags));
+}
+
+/*
+ * finish_write():
+ *
+ * Finish writing a WIM file: write the lookup table, xml data, and integrity
+ * table, then overwrite the WIM header. By default, closes the WIM file
+ * descriptor (@wim->out_fd) if successful.
+ *
+ * write_flags is a bitwise OR of the following:
+ *
+ * (public) WIMLIB_WRITE_FLAG_CHECK_INTEGRITY:
+ * Include an integrity table.
+ *
+ * (public) WIMLIB_WRITE_FLAG_FSYNC:
+ * fsync() the output file before closing it.
+ *
+ * (public) WIMLIB_WRITE_FLAG_PIPABLE:
+ * Writing a pipable WIM, possibly to a pipe; include pipable WIM
+ * stream headers before the lookup table and XML data, and also
+ * write the WIM header at the end instead of seeking to the
+ * beginning. Can't be combined with
+ * WIMLIB_WRITE_FLAG_CHECK_INTEGRITY.
+ *
+ * (private) WIMLIB_WRITE_FLAG_NO_LOOKUP_TABLE:
+ * Don't write the lookup table.
+ *
+ * (private) WIMLIB_WRITE_FLAG_REUSE_INTEGRITY_TABLE:
+ * When (if) writing the integrity table, re-use entries from the
+ * existing integrity table, if possible.
+ *
+ * (private) WIMLIB_WRITE_FLAG_CHECKPOINT_AFTER_XML:
+ * After writing the XML data but before writing the integrity
+ * table, write a temporary WIM header and flush the stream so that
+ * the WIM is less likely to become corrupted upon abrupt program
+ * termination.
+ * (private) WIMLIB_WRITE_FLAG_HEADER_AT_END:
+ * Instead of overwriting the WIM header at the beginning of the
+ * file, simply append it to the end of the file. (Used when
+ * writing to pipe.)
+ * (private) WIMLIB_WRITE_FLAG_FILE_DESCRIPTOR:
+ * Do not close the file descriptor @wim->out_fd on either success
+ * on failure.
+ * (private) WIMLIB_WRITE_FLAG_USE_EXISTING_TOTALBYTES:
+ * Use the existing <TOTALBYTES> stored in the in-memory XML
+ * information, rather than setting it to the offset of the XML
+ * data being written.
+ */
+static int
+finish_write(WIMStruct *wim, int image, int write_flags,
+ wimlib_progress_func_t progress_func,
+ struct list_head *lookup_table_list)
+{
+ int ret;
+ off_t hdr_offset;
+ int write_resource_flags;
+ off_t old_lookup_table_end;
+ off_t new_lookup_table_end;
+ u64 xml_totalbytes;
+
+ DEBUG("image=%d, write_flags=%08x", image, write_flags);
+
+ write_resource_flags = write_flags_to_resource_flags(write_flags);
+
+ /* In the WIM header, there is room for the resource entry for a
+ * metadata resource labeled as the "boot metadata". This entry should
+ * be zeroed out if there is no bootable image (boot_idx 0). Otherwise,
+ * it should be a copy of the resource entry for the image that is
+ * marked as bootable. This is not well documented... */
+ if (wim->hdr.boot_idx == 0) {
+ zero_reshdr(&wim->hdr.boot_metadata_reshdr);
+ } else {
+ copy_reshdr(&wim->hdr.boot_metadata_reshdr,
+ &wim->image_metadata[
+ wim->hdr.boot_idx - 1]->metadata_lte->out_reshdr);
+ }
+
+ /* Write lookup table. (Save old position first.) */
+ old_lookup_table_end = wim->hdr.lookup_table_reshdr.offset_in_wim +
+ wim->hdr.lookup_table_reshdr.size_in_wim;
+ if (!(write_flags & WIMLIB_WRITE_FLAG_NO_LOOKUP_TABLE)) {
+ ret = write_wim_lookup_table(wim, image, write_flags,
+ &wim->hdr.lookup_table_reshdr,
+ lookup_table_list);
+ if (ret)
+ return ret;
+ }
+
+ /* Write XML data. */
+ xml_totalbytes = wim->out_fd.offset;
+ if (write_flags & WIMLIB_WRITE_FLAG_USE_EXISTING_TOTALBYTES)
+ xml_totalbytes = WIM_TOTALBYTES_USE_EXISTING;
+ ret = write_wim_xml_data(wim, image, xml_totalbytes,
+ &wim->hdr.xml_data_reshdr,
+ write_resource_flags);
+ if (ret)
+ return ret;