+ }
+ }
+ list_move_tail(<e->write_streams_list, &ctx->pending_streams);
+ return 0;
+}
+
+/* Rewrite a stream that was just written compressed as uncompressed instead.
+ * This function is optional, but if a stream did not compress to less than its
+ * original size, it might as well be written uncompressed. */
+static int
+write_stream_uncompressed(struct wim_lookup_table_entry *lte,
+ struct filedes *out_fd)
+{
+ int ret;
+ u64 begin_offset = lte->out_reshdr.offset_in_wim;
+ u64 end_offset = out_fd->offset;
+
+ if (filedes_seek(out_fd, begin_offset) == -1)
+ return 0;
+
+ ret = extract_full_stream_to_fd(lte, out_fd);
+ if (ret) {
+ /* Error reading the uncompressed data. */
+ if (out_fd->offset == begin_offset &&
+ filedes_seek(out_fd, end_offset) != -1)
+ {
+ /* Nothing was actually written yet, and we successfully
+ * seeked to the end of the compressed resource, so
+ * don't issue a hard error; just keep the compressed
+ * resource instead. */
+ WARNING("Recovered compressed stream of "
+ "size %"PRIu64", continuing on.",
+ lte->size);
+ return 0;
+ }
+ return ret;
+ }
+
+ wimlib_assert(out_fd->offset - begin_offset == lte->size);
+
+ if (out_fd->offset < end_offset &&
+ 0 != ftruncate(out_fd->fd, out_fd->offset))
+ {
+ ERROR_WITH_ERRNO("Can't truncate output file to "
+ "offset %"PRIu64, out_fd->offset);
+ return WIMLIB_ERR_WRITE;
+ }
+
+ lte->out_reshdr.size_in_wim = lte->size;
+ lte->out_reshdr.flags &= ~(WIM_RESHDR_FLAG_COMPRESSED |
+ WIM_RESHDR_FLAG_PACKED_STREAMS);
+ return 0;
+}
+
+/* Returns true if the specified stream should be truncated from the WIM file
+ * and re-written as uncompressed. lte->out_reshdr must be filled in from the
+ * initial write of the stream. */
+static bool
+should_rewrite_stream_uncompressed(const struct write_streams_ctx *ctx,
+ const struct wim_lookup_table_entry *lte)
+{
+ /* If the compressed data is smaller than the uncompressed data, prefer
+ * the compressed data. */
+ if (lte->out_reshdr.size_in_wim < lte->out_reshdr.uncompressed_size)
+ return false;
+
+ /* If we're not actually writing compressed data, then there's no need
+ * for re-writing. */
+ if (!ctx->compressor)
+ return false;
+
+ /* If writing a pipable WIM, everything we write to the output is final
+ * (it might actually be a pipe!). */
+ if (ctx->write_resource_flags & WRITE_RESOURCE_FLAG_PIPABLE)
+ return false;
+
+ /* If the stream that would need to be re-read is located in a solid
+ * block in another WIM file, then re-reading it would be costly. So
+ * don't do it.
+ *
+ * Exception: if the compressed size happens to be *exactly* the same as
+ * the uncompressed size, then the stream *must* be written uncompressed
+ * in order to remain compatible with the Windows Overlay Filesystem
+ * Filter Driver (WOF).
+ *
+ * TODO: we are currently assuming that the optimization for
+ * single-chunk resources in maybe_rewrite_stream_uncompressed()
+ * prevents this case from being triggered too often. To fully prevent
+ * excessive decompressions in degenerate cases, we really should
+ * obtain the uncompressed data by decompressing the compressed data we
+ * wrote to the output file.
+ */
+ if ((lte->flags & WIM_RESHDR_FLAG_PACKED_STREAMS) &&
+ (lte->out_reshdr.size_in_wim != lte->out_reshdr.uncompressed_size))
+ return false;
+
+ return true;
+}
+
+static int
+maybe_rewrite_stream_uncompressed(struct write_streams_ctx *ctx,
+ struct wim_lookup_table_entry *lte)
+{
+ if (!should_rewrite_stream_uncompressed(ctx, lte))
+ return 0;
+
+ /* Regular (non-solid) WIM resources with exactly one chunk and
+ * compressed size equal to uncompressed size are exactly the same as
+ * the corresponding compressed data --- since there must be 0 entries
+ * in the chunk table and the only chunk must be stored uncompressed.
+ * In this case, there's no need to rewrite anything. */
+ if (ctx->chunk_index == 1 &&
+ lte->out_reshdr.size_in_wim == lte->out_reshdr.uncompressed_size)
+ {
+ lte->out_reshdr.flags &= ~WIM_RESHDR_FLAG_COMPRESSED;
+ return 0;
+ }
+
+ return write_stream_uncompressed(lte, ctx->out_fd);
+}
+
+/* Write the next chunk of (typically compressed) data to the output WIM,
+ * handling the writing of the chunk table. */
+static int
+write_chunk(struct write_streams_ctx *ctx, const void *cchunk,
+ size_t csize, size_t usize)
+{
+ int ret;
+
+ struct wim_lookup_table_entry *lte;
+ u32 completed_stream_count;
+ u32 completed_size;
+
+ lte = list_entry(ctx->pending_streams.next,
+ struct wim_lookup_table_entry, write_streams_list);
+
+ if (ctx->cur_write_stream_offset == 0 &&
+ !(ctx->write_resource_flags & WRITE_RESOURCE_FLAG_PACK_STREAMS))
+ {
+ /* Starting to write a new stream in non-packed mode. */
+
+ if (ctx->write_resource_flags & WRITE_RESOURCE_FLAG_PIPABLE) {
+ int additional_reshdr_flags = 0;
+ if (ctx->compressor != NULL)
+ additional_reshdr_flags |= WIM_RESHDR_FLAG_COMPRESSED;
+
+ DEBUG("Writing pipable WIM stream header "
+ "(offset=%"PRIu64")", ctx->out_fd->offset);
+
+ ret = write_pwm_stream_header(lte, ctx->out_fd,
+ additional_reshdr_flags);
+ if (ret)
+ return ret;
+ }
+
+ ret = begin_write_resource(ctx, lte->size);
+ if (ret)
+ return ret;
+ }
+
+ if (ctx->compressor != NULL) {
+ /* Record the compresed chunk size. */
+ wimlib_assert(ctx->chunk_index < ctx->num_alloc_chunks);
+ ctx->chunk_csizes[ctx->chunk_index++] = csize;
+
+ /* If writing a pipable WIM, before the chunk data write a chunk
+ * header that provides the compressed chunk size. */
+ if (ctx->write_resource_flags & WRITE_RESOURCE_FLAG_PIPABLE) {
+ struct pwm_chunk_hdr chunk_hdr = {
+ .compressed_size = cpu_to_le32(csize),
+ };
+ ret = full_write(ctx->out_fd, &chunk_hdr,
+ sizeof(chunk_hdr));
+ if (ret)
+ goto write_error;
+ }
+ }
+
+ /* Write the chunk data. */
+ ret = full_write(ctx->out_fd, cchunk, csize);
+ if (ret)
+ goto write_error;
+
+ ctx->cur_write_stream_offset += usize;
+
+ completed_size = usize;
+ completed_stream_count = 0;
+ if (ctx->write_resource_flags & WRITE_RESOURCE_FLAG_PACK_STREAMS) {
+ /* Wrote chunk in packed mode. It may have finished multiple
+ * streams. */
+ struct wim_lookup_table_entry *next_lte;
+
+ while (lte && ctx->cur_write_stream_offset >= lte->size) {
+
+ ctx->cur_write_stream_offset -= lte->size;
+
+ if (ctx->cur_write_stream_offset)
+ next_lte = list_entry(lte->write_streams_list.next,
+ struct wim_lookup_table_entry,
+ write_streams_list);
+ else
+ next_lte = NULL;
+
+ ret = done_with_stream(lte, ctx);
+ if (ret)
+ return ret;
+ list_move_tail(<e->write_streams_list, &ctx->pack_streams);
+ completed_stream_count++;
+
+ lte = next_lte;
+ }
+ } else {
+ /* Wrote chunk in non-packed mode. It may have finished a
+ * stream. */
+ if (ctx->cur_write_stream_offset == lte->size) {
+
+ wimlib_assert(ctx->cur_write_stream_offset ==
+ ctx->cur_write_res_size);
+
+ ret = end_write_resource(ctx, <e->out_reshdr);
+ if (ret)
+ return ret;
+
+ lte->out_reshdr.flags = filter_resource_flags(lte->flags);
+ if (ctx->compressor != NULL)
+ lte->out_reshdr.flags |= WIM_RESHDR_FLAG_COMPRESSED;
+
+ ret = maybe_rewrite_stream_uncompressed(ctx, lte);
+ if (ret)
+ return ret;
+
+ wimlib_assert(lte->out_reshdr.uncompressed_size == lte->size);
+
+ ctx->cur_write_stream_offset = 0;
+
+ ret = done_with_stream(lte, ctx);
+ if (ret)
+ return ret;
+ list_del(<e->write_streams_list);
+ completed_stream_count++;
+ }
+ }
+
+ return do_write_streams_progress(&ctx->progress_data,
+ completed_size, completed_stream_count,
+ false);
+
+write_error:
+ ERROR_WITH_ERRNO("Write error");
+ return ret;
+}
+
+static int
+submit_chunk_for_compression(struct write_streams_ctx *ctx,
+ const void *chunk, size_t size)
+{
+ /* While we are unable to submit the chunk for compression (due to too
+ * many chunks already outstanding), retrieve and write the next
+ * compressed chunk. */
+ while (!ctx->compressor->submit_chunk(ctx->compressor, chunk, size)) {
+ const void *cchunk;
+ u32 csize;
+ u32 usize;
+ bool bret;
+ int ret;