- w->write_metadata = true;
- for (int i = 0; i < w->hdr.image_count; i++) {
-
- struct lookup_table_entry *metadata_lte;
-
- metadata_lte = w->image_metadata[i].lookup_table_entry;
- ret = copy_resource(metadata_lte, w);
- if (ret != 0)
- return ret;
- size_remaining -= metadata_lte->resource_entry.size;
- total_bytes_written += metadata_lte->resource_entry.size;
- if (lte_chain_tail)
- lte_chain_tail->next_lte_in_swm = metadata_lte;
- else
- lte_chain_head = metadata_lte;
- lte_chain_tail = metadata_lte;
- }
- w->write_metadata = false;
-
- struct args args = {
- .w = w,
- .swm_base_name = name,
- .swm_base_name_len = swm_base_name_len,
- .swm_suffix = swm_suffix,
- .lte_chain_head = lte_chain_head,
- .lte_chain_tail = lte_chain_tail,
- .part_number = 1,
- .write_flags = write_flags,
- .size_remaining = size_remaining,
- .part_size = part_size,
- .total_bytes = total_bytes,
- .total_bytes_written = total_bytes_written,
- };
-
- ret = for_lookup_table_entry(w->lookup_table, copy_resource_to_swm, &args);
- if (ret != 0)
- return ret;
-
- ret = finish_swm(w, args.lte_chain_head, write_flags);
- if (ret != 0)
- return ret;
-
-
- /* The swms are all ready now, except the total_parts and part_number
- * fields in their headers are wrong (we don't know the total parts
- * until they are all written). Fix them. */
- int total_parts = args.part_number;
- for (int i = 1; i <= total_parts; i++) {
- const char *p;
- if (i == 1) {
- p = swm_name;
- } else {
- sprintf(name + swm_base_name_len, "%d", i);
- p = strcat(name, swm_suffix);
- }
-
- FILE *fp = fopen(p, "r+b");
- if (!fp) {
- ERROR("Failed to open `%s': %m\n", p);
- return WIMLIB_ERR_OPEN;
- }
- char buf[4];
- put_u16(buf, i);
- put_u16(buf + 2, total_parts);
-
- if (fseek(fp, 40, SEEK_SET) != 0 ||
- fwrite(buf, 1, sizeof(buf), fp) != sizeof(buf)
- || fclose(fp) != 0) {
- ERROR("Error overwriting header of `%s': %m\n", name);
- return WIMLIB_ERR_WRITE;
- }
- }
- if (write_flags & WIMLIB_OPEN_FLAG_SHOW_PROGRESS)
- printf("Done!\n");
- wimlib_free(w);
- return 0;