- fp = w->fp;
-
- /* The old integrity table is still OK, as the SHA1 message digests in
- * the integrity table include neither the header nor the XML data.
- * Save it for later if it exists and an integrity table was required.
- * */
- if ((write_flags & WIMLIB_WRITE_FLAG_CHECK_INTEGRITY)
- && w->hdr.integrity.offset != 0)
- {
- DEBUG("Reading existing integrity table.");
- integrity_table = MALLOC(w->hdr.integrity.size);
- if (!integrity_table)
- return WIMLIB_ERR_NOMEM;
-
- ret = read_uncompressed_resource(fp, w->hdr.integrity.offset,
- w->hdr.integrity.original_size,
- integrity_table);
- if (ret != 0)
- goto err;
- DEBUG("Done reading existing integrity table.");
- }
-
- DEBUG("Overwriting XML data.");
- /* Overwrite the XML data. */
- if (fseeko(fp, w->hdr.xml_res_entry.offset, SEEK_SET) != 0) {
- ERROR_WITH_ERRNO("Failed to seek to byte %"PRIu64" "
- "for XML data", w->hdr.xml_res_entry.offset);
- ret = WIMLIB_ERR_WRITE;
- goto err;
- }
- ret = write_xml_data(w->wim_info, WIM_ALL_IMAGES, fp, 0);
- if (ret != 0)
- goto err;
-
- DEBUG("Updating XML resource entry.");
- /* Update the XML resource entry in the WIM header. */
- xml_end = ftello(fp);
- if (xml_end == -1) {
- ret = WIMLIB_ERR_WRITE;
- goto err;
- }
- xml_size = xml_end - w->hdr.xml_res_entry.offset;
- w->hdr.xml_res_entry.size = xml_size;
- w->hdr.xml_res_entry.original_size = xml_size;
- /* XML data offset is unchanged. */
-
- if (write_flags & WIMLIB_WRITE_FLAG_CHECK_INTEGRITY) {
- DEBUG("Writing integrity table.");
- w->hdr.integrity.offset = xml_end;
- if (integrity_table) {
- /* The existing integrity table was saved. */
- bytes_written = fwrite(integrity_table, 1,
- w->hdr.integrity.size, fp);
- if (bytes_written != w->hdr.integrity.size) {
- ERROR_WITH_ERRNO("Failed to write integrity "
- "table");
- ret = WIMLIB_ERR_WRITE;
- goto err;
- }
- FREE(integrity_table);
- } else {
- /* There was no existing integrity table, so a new one
- * must be calculated. */
- ret = write_integrity_table(fp, WIM_HEADER_DISK_SIZE,
- w->hdr.lookup_table_res_entry.offset +
- w->hdr.lookup_table_res_entry.size,
- write_flags & WIMLIB_WRITE_FLAG_SHOW_PROGRESS);
- if (ret != 0)
- return ret;
-
- off_t end_integrity = ftello(fp);
- if (end_integrity == -1)
- return WIMLIB_ERR_WRITE;
-
- off_t integrity_size = end_integrity - xml_end;
- w->hdr.integrity.size = integrity_size;
- w->hdr.integrity.original_size = integrity_size;
- w->hdr.integrity.flags = 0;
- }
- } else {
- DEBUG("Truncating file to end of XML data.");
- /* No integrity table to write. The file should be truncated
- * because it's possible that the old file was longer (due to it
- * including an integrity table, or due to its XML data being
- * longer) */
- if (fflush(fp) != 0) {
- ERROR_WITH_ERRNO("Failed to flush stream for file `%s'",
- w->filename);
- return WIMLIB_ERR_WRITE;
- }
- if (ftruncate(fileno(fp), xml_end) != 0) {
- ERROR_WITH_ERRNO("Failed to truncate `%s' to %"PRIu64" "
- "bytes", w->filename, xml_end);
- return WIMLIB_ERR_WRITE;
- }
- memset(&w->hdr.integrity, 0, sizeof(struct resource_entry));
- }
-
- DEBUG("Overwriting header.");
- /* Overwrite the header. */
- if (fseeko(fp, 0, SEEK_SET) != 0) {
- ERROR_WITH_ERRNO("Failed to seek to beginning of `%s'",
- w->filename);
- return WIMLIB_ERR_WRITE;
- }
-
- ret = write_header(&w->hdr, fp);