- long size_remaining = part_size;
- u64 total_bytes_written = 0;
- u64 total_bytes;
-
- ret = wimlib_open_wim(wimfile, flags, &w);
- if (ret != 0)
- return ret;
-
- total_bytes = wim_info_get_total_bytes(w->wim_info);
-
- if (flags & WIMLIB_OPEN_FLAG_CHECK_INTEGRITY)
- write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
- if (flags & WIMLIB_OPEN_FLAG_SHOW_PROGRESS)
- write_flags |= WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
+ if (lte_is_partial(lte)) {
+ ERROR("Splitting of WIM containing packed streams is not supported.\n"
+ " Export it in the default format first.");
+ return WIMLIB_ERR_UNSUPPORTED;
+ }
+ if (lte->resource_location == RESOURCE_IN_WIM)
+ stream_size = lte->rspec->size_in_wim;
+ else
+ stream_size = lte->size;
+
+ /* - Start first part if no parts have been started so far;
+ * - Start next part if adding this stream exceeds maximum part size,
+ * UNLESS the stream is metadata or if no streams at all have been
+ * added to the current part.
+ */
+ if (swm_info->num_parts == 0 ||
+ ((swm_info->parts[swm_info->num_parts - 1].size +
+ stream_size >= swm_info->max_part_size)
+ && !((lte->flags & WIM_RESHDR_FLAG_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].stream_list);
+ swm_info->parts[swm_info->num_parts - 1].size = 0;
+ }
+ swm_info->parts[swm_info->num_parts - 1].size += stream_size;
+ if (!(lte->flags & WIM_RESHDR_FLAG_METADATA)) {
+ list_add_tail(<e->write_streams_list,
+ &swm_info->parts[swm_info->num_parts - 1].stream_list);
+ }
+ swm_info->total_bytes += stream_size;
+ return 0;
+}