+read_error:
+ ERROR_WITH_ERRNO("\"%"TS"\": Error reading header", filename);
+ return ret;
+}
+
+/* Writes the header for a WIM file at the specified offset. If the offset
+ * specified is the current one, the position is advanced by the size of the
+ * header. */
+int
+write_wim_header_at_offset(const struct wim_header *hdr, struct filedes *out_fd,
+ off_t offset)
+{
+ struct wim_header_disk disk_hdr _aligned_attribute(8);
+ int ret;
+
+ DEBUG("Writing %sWIM header at offset %"PRIu64,
+ ((hdr->magic == PWM_MAGIC) ? "pipable " : ""),
+ offset);
+
+ disk_hdr.magic = hdr->magic;
+ disk_hdr.hdr_size = cpu_to_le32(sizeof(struct wim_header_disk));
+ disk_hdr.wim_version = cpu_to_le32(WIM_VERSION);
+ disk_hdr.wim_flags = cpu_to_le32(hdr->flags);
+ disk_hdr.chunk_size = cpu_to_le32((hdr->flags & WIM_HDR_FLAG_COMPRESSION) ?
+ WIM_CHUNK_SIZE : 0);
+ memcpy(disk_hdr.guid, hdr->guid, WIM_GID_LEN);
+
+ disk_hdr.part_number = cpu_to_le16(hdr->part_number);
+ disk_hdr.total_parts = cpu_to_le16(hdr->total_parts);
+ disk_hdr.image_count = cpu_to_le32(hdr->image_count);
+ put_resource_entry(&hdr->lookup_table_res_entry, &disk_hdr.lookup_table_res_entry);
+ put_resource_entry(&hdr->xml_res_entry, &disk_hdr.xml_data_res_entry);
+ put_resource_entry(&hdr->boot_metadata_res_entry, &disk_hdr.boot_metadata_res_entry);
+ disk_hdr.boot_idx = cpu_to_le32(hdr->boot_idx);
+ put_resource_entry(&hdr->integrity, &disk_hdr.integrity_table_res_entry);
+ memset(disk_hdr.unused, 0, sizeof(disk_hdr.unused));
+
+ if (offset == out_fd->offset)
+ ret = full_write(out_fd, &disk_hdr, sizeof(disk_hdr));