+/* 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 = cpu_to_le64(hdr->magic);
+ disk_hdr.hdr_size = cpu_to_le32(sizeof(struct wim_header_disk));
+ disk_hdr.wim_version = cpu_to_le32(hdr->wim_version);
+ disk_hdr.wim_flags = cpu_to_le32(hdr->flags);
+ if (hdr->flags & WIM_HDR_FLAG_COMPRESSION)
+ disk_hdr.chunk_size = cpu_to_le32(hdr->chunk_size);
+ else
+ disk_hdr.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_wim_reshdr(&hdr->lookup_table_reshdr, &disk_hdr.lookup_table_reshdr);
+ put_wim_reshdr(&hdr->xml_data_reshdr, &disk_hdr.xml_data_reshdr);
+ put_wim_reshdr(&hdr->boot_metadata_reshdr, &disk_hdr.boot_metadata_reshdr);
+ disk_hdr.boot_idx = cpu_to_le32(hdr->boot_idx);
+ put_wim_reshdr(&hdr->integrity_table_reshdr, &disk_hdr.integrity_table_reshdr);
+ memset(disk_hdr.unused, 0, sizeof(disk_hdr.unused));
+
+ if (offset == out_fd->offset)
+ ret = full_write(out_fd, &disk_hdr, sizeof(disk_hdr));
+ else
+ ret = full_pwrite(out_fd, &disk_hdr, sizeof(disk_hdr), offset);
+ if (ret)
+ ERROR_WITH_ERRNO("Failed to write WIM header");
+ return ret;
+}
+
+/* Writes the header for a WIM file at the output file descriptor's current
+ * offset. */
+int
+write_wim_header(const struct wim_header *hdr, struct filedes *out_fd)
+{
+ return write_wim_header_at_offset(hdr, out_fd, out_fd->offset);
+}