+ if (blob->blob_location == BLOB_IN_WIM)
+ blob_stored_size = blob->rdesc->size_in_wim;
+ else
+ blob_stored_size = blob->size;
+
+ /* - Start first part if no parts have been started so far;
+ * - Start next part if adding this blob exceeds maximum part size,
+ * UNLESS the blob is metadata or if no blobs at all have been added
+ * to the current part.
+ */
+ if (swm_info->num_parts == 0 ||
+ ((swm_info->parts[swm_info->num_parts - 1].size +
+ blob_stored_size >= swm_info->max_part_size)
+ && !(blob->is_metadata ||
+ swm_info->parts[swm_info->num_parts - 1].size == 0)))
+ {
+ if (swm_info->num_parts == swm_info->num_alloc_parts) {
+ struct swm_part_info *parts;
+ size_t num_alloc_parts = swm_info->num_alloc_parts;
+
+ num_alloc_parts += 8;
+ parts = MALLOC(num_alloc_parts * sizeof(parts[0]));
+ if (!parts)
+ return WIMLIB_ERR_NOMEM;
+
+ for (unsigned i = 0; i < swm_info->num_parts; i++)
+ copy_part_info(&parts[i], &swm_info->parts[i]);
+
+ FREE(swm_info->parts);
+ swm_info->parts = parts;
+ swm_info->num_alloc_parts = num_alloc_parts;
+ }
+ swm_info->num_parts++;
+ INIT_LIST_HEAD(&swm_info->parts[swm_info->num_parts - 1].blob_list);
+ swm_info->parts[swm_info->num_parts - 1].size = 0;
+ }
+ swm_info->parts[swm_info->num_parts - 1].size += blob_stored_size;
+ if (!blob->is_metadata) {
+ list_add_tail(&blob->write_blobs_list,
+ &swm_info->parts[swm_info->num_parts - 1].blob_list);
+ }
+ swm_info->total_bytes += blob_stored_size;
+ return 0;
+}