+/* Process the next chunk of data to be written to a WIM resource. */
+static int
+write_blob_process_chunk(const void *chunk, size_t size, void *_ctx)
+{
+ struct write_blobs_ctx *ctx = _ctx;
+ int ret;
+ const u8 *chunkptr, *chunkend;
+
+ wimlib_assert(size != 0);
+
+ if (ctx->compressor == NULL) {
+ /* Write chunk uncompressed. */
+ ret = write_chunk(ctx, chunk, size, size);
+ if (ret)
+ return ret;
+ ctx->cur_read_blob_offset += size;
+ return 0;
+ }
+
+ /* Submit the chunk for compression, but take into account that the
+ * @size the chunk was provided in may not correspond to the
+ * @out_chunk_size being used for compression. */
+ chunkptr = chunk;
+ chunkend = chunkptr + size;
+ do {
+ size_t needed_chunk_size;
+ size_t bytes_consumed;
+
+ if (!ctx->cur_chunk_buf) {
+ ret = prepare_chunk_buffer(ctx);
+ if (ret)
+ return ret;
+ }
+
+ if (ctx->write_resource_flags & WRITE_RESOURCE_FLAG_SOLID) {
+ needed_chunk_size = ctx->out_chunk_size;
+ } else {
+ needed_chunk_size = min(ctx->out_chunk_size,
+ ctx->cur_chunk_buf_filled +
+ (ctx->cur_read_blob_size -
+ ctx->cur_read_blob_offset));
+ }
+
+ bytes_consumed = min(chunkend - chunkptr,
+ needed_chunk_size - ctx->cur_chunk_buf_filled);
+
+ memcpy(&ctx->cur_chunk_buf[ctx->cur_chunk_buf_filled],
+ chunkptr, bytes_consumed);
+
+ chunkptr += bytes_consumed;
+ ctx->cur_read_blob_offset += bytes_consumed;
+ ctx->cur_chunk_buf_filled += bytes_consumed;
+
+ if (ctx->cur_chunk_buf_filled == needed_chunk_size) {
+ ctx->compressor->signal_chunk_filled(ctx->compressor,
+ ctx->cur_chunk_buf_filled);
+ ctx->cur_chunk_buf = NULL;
+ ctx->cur_chunk_buf_filled = 0;
+ }
+ } while (chunkptr != chunkend);
+ return 0;
+}
+
+/* Finish processing a blob for writing. It may not have been completely
+ * written yet, as the chunk_compressor implementation may still have chunks
+ * buffered or being compressed. */
+static int
+write_blob_end_read(struct blob_descriptor *blob, int status, void *_ctx)
+{
+ struct write_blobs_ctx *ctx = _ctx;
+
+ wimlib_assert(ctx->cur_read_blob_offset == ctx->cur_read_blob_size || status);
+
+ if (!blob->will_be_in_output_wim) {
+ /* The blob was a duplicate. Now that its data has finished
+ * being read, it is being discarded in favor of the duplicate
+ * entry. It therefore is no longer needed, and we can fire the
+ * DONE_WITH_FILE callback because the file will not be read
+ * again.
+ *
+ * Note: we can't yet fire DONE_WITH_FILE for non-duplicate
+ * blobs, since it needs to be possible to re-read the file if
+ * it does not compress to less than its original size. */
+ if (!status)
+ status = done_with_blob(blob, ctx);
+ free_blob_descriptor(blob);
+ } else if (!status && blob->unhashed && ctx->blob_table != NULL) {
+ /* The blob was not a duplicate and was previously unhashed.
+ * Since we passed COMPUTE_MISSING_BLOB_HASHES to
+ * read_blob_list(), blob->hash is now computed and valid. So
+ * turn this blob into a "hashed" blob. */
+ list_del(&blob->unhashed_list);
+ blob_table_insert(ctx->blob_table, blob);
+ blob->unhashed = 0;
+ }
+ return status;
+}
+
+/* Compute statistics about a list of blobs that will be written.
+ *
+ * Assumes the blobs are sorted such that all blobs located in each distinct WIM
+ * (specified by WIMStruct) are together. */
+static void
+compute_blob_list_stats(struct list_head *blob_list,
+ struct write_blobs_ctx *ctx)
+{
+ struct blob_descriptor *blob;
+ u64 total_bytes = 0;
+ u64 num_blobs = 0;
+ u64 total_parts = 0;
+ WIMStruct *prev_wim_part = NULL;
+
+ list_for_each_entry(blob, blob_list, write_blobs_list) {
+ num_blobs++;
+ total_bytes += blob->size;
+ if (blob->blob_location == BLOB_IN_WIM) {
+ if (prev_wim_part != blob->rdesc->wim) {
+ prev_wim_part = blob->rdesc->wim;
+ total_parts++;
+ }
+ }
+ }
+ ctx->progress_data.progress.write_streams.total_bytes = total_bytes;
+ ctx->progress_data.progress.write_streams.total_streams = num_blobs;
+ ctx->progress_data.progress.write_streams.completed_bytes = 0;
+ ctx->progress_data.progress.write_streams.completed_streams = 0;
+ ctx->progress_data.progress.write_streams.compression_type = ctx->out_ctype;
+ ctx->progress_data.progress.write_streams.total_parts = total_parts;
+ ctx->progress_data.progress.write_streams.completed_parts = 0;
+ ctx->progress_data.next_progress = 0;
+}
+
+/* Find blobs in @blob_list that can be copied to the output WIM in raw form
+ * rather than compressed. Delete these blobs from @blob_list and move them to
+ * @raw_copy_blobs. Return the total uncompressed size of the blobs that need
+ * to be compressed. */
+static u64
+find_raw_copy_blobs(struct list_head *blob_list,
+ int write_resource_flags,
+ int out_ctype,
+ u32 out_chunk_size,
+ struct list_head *raw_copy_blobs)
+{
+ struct blob_descriptor *blob, *tmp;
+ u64 num_bytes_to_compress = 0;
+
+ INIT_LIST_HEAD(raw_copy_blobs);
+
+ /* Initialize temporary raw_copy_ok flag. */
+ list_for_each_entry(blob, blob_list, write_blobs_list)
+ if (blob->blob_location == BLOB_IN_WIM)
+ blob->rdesc->raw_copy_ok = 0;
+
+ list_for_each_entry_safe(blob, tmp, blob_list, write_blobs_list) {
+ if (blob->blob_location == BLOB_IN_WIM &&
+ blob->rdesc->raw_copy_ok)
+ {
+ list_move_tail(&blob->write_blobs_list,
+ raw_copy_blobs);
+ } else if (can_raw_copy(blob, write_resource_flags,
+ out_ctype, out_chunk_size))
+ {
+ blob->rdesc->raw_copy_ok = 1;
+ list_move_tail(&blob->write_blobs_list,
+ raw_copy_blobs);
+ } else {
+ num_bytes_to_compress += blob->size;
+ }
+ }
+
+ return num_bytes_to_compress;
+}
+
+/* Copy a raw compressed resource located in another WIM file to the WIM file
+ * being written. */