4 * Support for writing WIM files; write a WIM file, overwrite a WIM file, write
5 * compressed file resources, etc.
7 * Copyright (C) 2010 Carl Thijssen
8 * Copyright (C) 2012 Eric Biggers
10 * wimlib - Library for working with WIM files
12 * This library is free software; you can redistribute it and/or modify it under
13 * the terms of the GNU Lesser General Public License as published by the Free
14 * Software Foundation; either version 2.1 of the License, or (at your option) any
17 * This library is distributed in the hope that it will be useful, but WITHOUT ANY
18 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
19 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License along
22 * with this library; if not, write to the Free Software Foundation, Inc., 59
23 * Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "wimlib_internal.h"
27 #include "lookup_table.h"
37 /* Used for buffering FILE IO */
38 #define BUFFER_SIZE 4096
41 * Copies bytes between two file streams.
43 * Copies @len bytes from @in to @out, at the current position in @out, and at
44 * an offset of @in_offset in @in.
46 int copy_between_files(FILE *in, off_t in_offset, FILE *out, size_t len)
51 if (fseeko(in, in_offset, SEEK_SET) != 0) {
52 ERROR("Failed to seek to byte %"PRIu64" of input file: %m\n",
54 return WIMLIB_ERR_READ;
56 /* To reduce memory usage and improve speed, read and write BUFFER_SIZE
59 n = min(len, BUFFER_SIZE);
60 if (fread(buf, 1, n, in) != n) {
62 ERROR("Unexpected EOF when copying data "
65 ERROR("Error copying data between files: %m\n");
67 return WIMLIB_ERR_READ;
70 if (fwrite(buf, 1, n, out) != n) {
71 ERROR("Error copying data between files: %m\n");
72 return WIMLIB_ERR_WRITE;
81 * Uncompresses a WIM file resource and writes it uncompressed to a file stream.
83 * @in: The file stream that contains the file resource.
84 * @size: The size of the resource in the input file.
85 * @original_size: The original (uncompressed) size of the resource.
86 * @offset: The offset of the start of the resource in @in.
87 * @input_ctype: The compression type of the resource in @in.
88 * @out: The file stream to write the file resource to.
90 static int uncompress_resource(FILE *in, u64 size, u64 original_size,
91 off_t offset, int input_ctype, FILE *out)
94 u8 buf[WIM_CHUNK_SIZE];
95 /* Determine how many compressed chunks the file is divided into. */
98 u64 uncompressed_offset;
99 u64 uncompressed_chunk_size;
101 num_chunks = (original_size + WIM_CHUNK_SIZE - 1) / WIM_CHUNK_SIZE;
103 for (i = 0; i < num_chunks; i++) {
105 uncompressed_offset = i * WIM_CHUNK_SIZE;
106 uncompressed_chunk_size = min(WIM_CHUNK_SIZE,
107 original_size - uncompressed_offset);
109 ret = read_resource(in, size, original_size, offset, input_ctype,
110 uncompressed_chunk_size,
111 uncompressed_offset, buf);
115 if (fwrite(buf, 1, uncompressed_chunk_size, out) !=
116 uncompressed_chunk_size) {
117 ERROR("Failed to write file resource: %m\n");
118 return WIMLIB_ERR_WRITE;
125 * Transfers a file resource between two files, writing it compressed. The file
126 * resource in the input file may be either compressed or uncompressed.
127 * Alternatively, the input resource may be in-memory, but it must be
130 * @in: The file stream that contains the file resource. Ignored
131 * if uncompressed_resource != NULL.
132 * @uncompressed_resource: If this pointer is not NULL, it points to an
133 * array of @original_size bytes that are
134 * the uncompressed input resource.
135 * @size: The size of the resource in the input file.
136 * @original_size: The original (uncompressed) size of the resource.
137 * @offset: The offset of the start of the resource in @in. Ignored
138 * if uncompressed_resource != NULL.
139 * @input_ctype: The compression type of the resource in @in. Ignored if
140 * uncompressed_resource != NULL.
141 * @out: The file stream to write the file resource to.
142 * @output_type: The compression type to use when writing the resource to
144 * @new_size_ret: A location into which the new compressed size of the file
145 * resource in returned.
147 static int recompress_resource(FILE *in, const u8 uncompressed_resource[],
148 u64 size, u64 original_size,
149 off_t offset, int input_ctype, FILE *out,
150 int output_ctype, u64 *new_size_ret)
153 int (*compress)(const void *, uint, void *, uint *);
154 if (output_ctype == WIM_COMPRESSION_TYPE_LZX)
155 compress = lzx_compress;
157 compress = xpress_compress;
159 u8 uncompressed_buf[WIM_CHUNK_SIZE];
160 u8 compressed_buf[WIM_CHUNK_SIZE - 1];
162 /* Determine how many compressed chunks the file needs to be divided
164 u64 num_chunks = (original_size + WIM_CHUNK_SIZE - 1) / WIM_CHUNK_SIZE;
166 u64 num_chunk_entries = num_chunks - 1;
168 /* Size of the chunk entries--- 8 bytes for files over 4GB, otherwise 4
170 uint chunk_entry_size = (original_size >= (u64)1 << 32) ? 8 : 4;
172 /* Array in which to construct the chunk offset table. */
173 u64 chunk_offsets[num_chunk_entries];
175 /* Offset of the start of the chunk table in the output file. */
176 off_t chunk_tab_offset = ftello(out);
178 /* Total size of the chunk table (as written to the file) */
179 u64 chunk_tab_size = chunk_entry_size * num_chunk_entries;
181 /* Reserve space for the chunk table. */
182 if (fwrite(chunk_offsets, 1, chunk_tab_size, out) != chunk_tab_size) {
183 ERROR("Failed to write chunk offset table: %m\n");
184 return WIMLIB_ERR_WRITE;
187 /* Read each chunk of the file, compress it, write it to the output
188 * file, and update th chunk offset table. */
189 u64 cur_chunk_offset = 0;
190 for (u64 i = 0; i < num_chunks; i++) {
192 u64 uncompressed_offset = i * WIM_CHUNK_SIZE;
193 u64 uncompressed_chunk_size = min(WIM_CHUNK_SIZE,
194 original_size - uncompressed_offset);
196 const u8 *uncompressed_p;
197 if (uncompressed_resource != NULL) {
198 uncompressed_p = uncompressed_resource +
202 /* Read chunk i of the file into uncompressed_buf. */
203 ret = read_resource(in, size, original_size, offset, input_ctype,
204 uncompressed_chunk_size,
209 uncompressed_p = uncompressed_buf;
213 chunk_offsets[i - 1] = cur_chunk_offset;
217 ret = compress(uncompressed_p, uncompressed_chunk_size,
218 compressed_buf, &compressed_len);
220 /* if compress() returned nonzero, the compressed chunk would
221 * have been at least as large as the uncompressed chunk. In
222 * this situation, the WIM format requires that the uncompressed
223 * chunk be written instead. */
224 const u8 *buf_to_write;
227 buf_to_write = compressed_buf;
228 len_to_write = compressed_len;
230 buf_to_write = uncompressed_p;
231 len_to_write = uncompressed_chunk_size;
234 if (fwrite(buf_to_write, 1, len_to_write, out) != len_to_write) {
235 ERROR("Failed to write compressed file resource: %m\n");
236 return WIMLIB_ERR_WRITE;
238 cur_chunk_offset += len_to_write;
241 /* The chunk offset after the last chunk, plus the size of the chunk
242 * table, gives the total compressed size of the resource. */
243 *new_size_ret = cur_chunk_offset + chunk_tab_size;
245 /* Now that all entries of the chunk table are determined, rewind the
246 * stream to where the chunk table was, and write it back out. */
248 if (fseeko(out, chunk_tab_offset, SEEK_SET) != 0) {
249 ERROR("Failed to seek to beginning of chunk table: %m\n");
250 return WIMLIB_ERR_READ;
253 if (chunk_entry_size == 8) {
254 array_to_le64(chunk_offsets, num_chunk_entries);
256 if (fwrite(chunk_offsets, 1, chunk_tab_size, out) !=
258 ERROR("Failed to write chunk table: %m\n");
259 return WIMLIB_ERR_WRITE;
262 u32 chunk_entries_small[num_chunk_entries];
263 for (u64 i = 0; i < num_chunk_entries; i++)
264 chunk_entries_small[i] = to_le32(chunk_offsets[i]);
265 if (fwrite(chunk_entries_small, 1, chunk_tab_size, out) !=
267 ERROR("Failed to write chunk table: %m\n");
268 return WIMLIB_ERR_WRITE;
272 if (fseeko(out, 0, SEEK_END) != 0) {
273 ERROR("Failed to seek to end of output file: %m\n");
274 return WIMLIB_ERR_WRITE;
280 int write_resource_from_memory(const u8 resource[], int out_ctype,
281 u64 resource_original_size, FILE *out,
282 u64 *resource_size_ret)
284 if (out_ctype == WIM_COMPRESSION_TYPE_NONE) {
285 if (fwrite(resource, 1, resource_original_size, out) !=
286 resource_original_size) {
287 ERROR("Failed to write resource of length "
289 resource_original_size);
290 return WIMLIB_ERR_WRITE;
292 *resource_size_ret = resource_original_size;
295 return recompress_resource(NULL, resource, resource_original_size,
296 resource_original_size, 0, 0, out, out_ctype,
303 * Transfers a file resource from a FILE* opened for reading to a FILE* opened
304 * for writing, possibly changing the compression type.
306 * @in: The FILE* that contains the file resource.
307 * @size: The (compressed) size of the file resource.
308 * @original_size: The uncompressed size of the file resource.
309 * @offset: The offset of the file resource in the input file.
310 * @input_ctype: The compression type of the file resource in the input
312 * @out: The FILE* for the output file. The file resource is
313 * written at the current position of @out.
314 * @output_ctype: The compression type to which the file resource will be
316 * @output_res_entry: A pointer to a resource entry that, upon successful
317 * return of this function, will have the size,
318 * original size, offset, and flags fields filled
319 * in for the file resource written to the output
322 static int transfer_file_resource(FILE *in, u64 size, u64 original_size,
323 off_t offset, int input_ctype, FILE *out,
325 struct resource_entry *output_res_entry)
329 /* Handle zero-length files */
330 if (original_size == 0) {
331 memset(output_res_entry, 0, sizeof(*output_res_entry));
335 /* Get current offset in the output file. */
336 output_res_entry->offset = ftello(out);
337 if (output_res_entry->offset == -1) {
338 ERROR("Failed to get output position: %m\n");
339 return WIMLIB_ERR_WRITE;
342 if (output_ctype == input_ctype) {
343 /* The same compression types; simply copy the resource. */
345 ret = copy_between_files(in, offset, out, size);
348 output_res_entry->size = size;
350 /* Different compression types. */
352 if (output_ctype == WIM_COMPRESSION_TYPE_NONE) {
353 /* Uncompress a compressed file resource */
354 ret = uncompress_resource(in, size,
355 original_size, offset,
359 output_res_entry->size = original_size;
362 /* Compress an uncompressed file resource, or compress a
363 * compressed file resource using a different
364 * compression type (the latter is currently unsupported
365 * since only LZX compression is supported. */
366 ret = recompress_resource(in, NULL, size, original_size,
367 offset, input_ctype, out,
368 output_ctype, &new_size);
371 output_res_entry->size = new_size;
376 output_res_entry->original_size = original_size;
377 if (output_ctype == WIM_COMPRESSION_TYPE_NONE)
378 output_res_entry->flags = 0;
380 output_res_entry->flags = WIM_RESHDR_FLAG_COMPRESSED;
385 * Writes a file resource to the output file.
387 * @dentry: The dentry for the file resource.
388 * @wim_p: A pointer to the WIMStruct. The fields of interest to this
389 * function are the input and output file streams and the lookup table.
390 * @return zero on success, nonzero on failure.
392 static int write_file_resource(struct dentry *dentry, void *wim_p)
397 struct lookup_table_entry *lte;
401 struct resource_entry *input_res_entry;
402 struct resource_entry *output_res_entry;
409 /* Directories don't need file resources. */
410 if (dentry_is_directory(dentry))
413 /* Get the lookup entry for the file resource. */
414 lte = wim_lookup_resource(w, dentry);
418 /* No need to write file resources twice. (This indicates file
419 * resources that are part of a hard link set.) */
420 if (++lte->out_refcnt != 1)
423 out_wim_ctype = wimlib_get_compression_type(w);
424 output_res_entry = <e->output_resource_entry;
426 /* Figure out if we can read the resource from the WIM file, or
427 * if we have to read it from the filesystem outside. */
428 if (lte->file_on_disk) {
430 /* Read from disk (uncompressed) */
432 len = lte->resource_entry.original_size;
434 in = fopen(lte->file_on_disk, "rb");
436 ERROR("Failed to open the file `%s': %m\n",
438 return WIMLIB_ERR_OPEN;
442 puts(lte->file_on_disk);
444 ret = transfer_file_resource(in, len, len, 0,
445 WIM_COMPRESSION_TYPE_NONE, out,
446 out_wim_ctype, output_res_entry);
450 /* Read from input WIM (possibly compressed) */
452 /* It may be a different WIM file, in the case of
453 * exporting images from one WIM file to another */
454 if (lte->other_wim_fp) {
455 /* Different WIM file. */
456 in = lte->other_wim_fp;
457 in_wim_ctype = lte->other_wim_ctype;
461 in_wim_ctype = out_wim_ctype;
463 input_res_entry = <e->resource_entry;
464 input_res_ctype = resource_compression_type(
466 input_res_entry->flags);
468 ret = transfer_file_resource(in,
469 input_res_entry->size,
470 input_res_entry->original_size,
471 input_res_entry->offset,
480 /* Reopens the FILE* for a WIM read-write. */
481 static int reopen_rw(WIMStruct *w)
485 if (fclose(w->fp) != 0)
486 ERROR("Failed to close the file `%s': %m\n", w->filename);
487 fp = fopen(w->filename, "r+b");
489 ERROR("Failed to open `%s' for reading and writing: "
490 "%m\n", w->filename);
491 return WIMLIB_ERR_OPEN;
500 * Writes a WIM file to the original file that it was read from, overwriting it.
502 WIMLIBAPI int wimlib_overwrite(WIMStruct *w, int flags)
504 const char *wimfile_name;
508 wimfile_name = w->filename;
510 DEBUG("Replacing WIM file `%s'\n", wimfile_name);
513 return WIMLIB_ERR_NO_FILENAME;
515 /* Write the WIM to a temporary file. */
516 /* XXX should the temporary file be somewhere else? */
517 wim_name_len = strlen(wimfile_name);
518 char tmpfile[wim_name_len + 10];
519 memcpy(tmpfile, wimfile_name, wim_name_len);
520 randomize_char_array_with_alnum(tmpfile + wim_name_len, 9);
521 tmpfile[wim_name_len + 9] = '\0';
523 ret = wimlib_write(w, tmpfile, WIM_ALL_IMAGES, flags);
525 ERROR("Failed to write the WIM file `%s'!\n", tmpfile);
529 DEBUG("Closing original WIM file.\n");
530 /* Close the original WIM file that was opened for reading. */
532 if (fclose(w->fp) != 0) {
533 WARNING("Failed to close the file `%s'\n",
539 DEBUG("Renaming `%s' to `%s'\n", tmpfile, wimfile_name);
541 /* Rename the new file to the old file .*/
542 if (rename(tmpfile, wimfile_name) != 0) {
543 ERROR("Failed to rename `%s' to `%s': %m\n", tmpfile,
545 /* Remove temporary file. */
546 if (unlink(tmpfile) != 0)
547 ERROR("Failed to remove `%s': %m\n", tmpfile);
548 return WIMLIB_ERR_RENAME;
555 WIMLIBAPI int wimlib_overwrite_xml_and_header(WIMStruct *w, int flags)
559 u8 *integrity_table = NULL;
562 size_t bytes_written;
564 DEBUG("Overwriting XML and header of `%s', flags = %d\n",
567 return WIMLIB_ERR_NO_FILENAME;
575 /* The old integrity table is still OK, as the SHA1 message digests in
576 * the integrity table include neither the header nor the XML data.
577 * Save it for later if it exists and an integrity table was required.
579 if (flags & WIMLIB_WRITE_FLAG_CHECK_INTEGRITY &&
580 w->hdr.integrity.offset != 0) {
581 DEBUG("Reading existing integrity table.\n");
582 integrity_table = MALLOC(w->hdr.integrity.size);
583 if (!integrity_table)
584 return WIMLIB_ERR_NOMEM;
586 ret = read_uncompressed_resource(fp, w->hdr.integrity.offset,
587 w->hdr.integrity.original_size,
591 DEBUG("Done reading existing integrity table.\n");
594 DEBUG("Overwriting XML data.\n");
595 /* Overwrite the XML data. */
596 if (fseeko(fp, w->hdr.xml_res_entry.offset, SEEK_SET) != 0) {
597 ERROR("Failed to seek to byte %"PRIu64" for XML data: "
598 "%m\n", w->hdr.xml_res_entry.offset);
599 ret = WIMLIB_ERR_WRITE;
602 ret = write_xml_data(w->wim_info, WIM_ALL_IMAGES, fp);
606 DEBUG("Updating XML resource entry.\n");
607 /* Update the XML resource entry in the WIM header. */
608 xml_end = ftello(fp);
610 ret = WIMLIB_ERR_WRITE;
613 xml_size = xml_end - w->hdr.xml_res_entry.offset;
614 w->hdr.xml_res_entry.size = xml_size;
615 w->hdr.xml_res_entry.original_size = xml_size;
617 if (flags & WIMLIB_WRITE_FLAG_CHECK_INTEGRITY) {
618 DEBUG("Writing integrity table.\n");
619 w->hdr.integrity.offset = xml_end;
620 if (integrity_table) {
621 /* The existing integrity table was saved. */
622 bytes_written = fwrite(integrity_table, 1,
623 w->hdr.integrity.size, fp);
624 if (bytes_written != w->hdr.integrity.size) {
625 ERROR("Failed to write integrity table: %m\n");
626 ret = WIMLIB_ERR_WRITE;
629 FREE(integrity_table);
631 /* There was no existing integrity table, so a new one
632 * must be calculated. */
633 ret = write_integrity_table(fp, WIM_HEADER_DISK_SIZE,
634 w->hdr.lookup_table_res_entry.offset +
635 w->hdr.lookup_table_res_entry.size,
636 flags & WIMLIB_WRITE_FLAG_SHOW_PROGRESS);
640 off_t integrity_size = ftello(fp) - xml_end;
641 w->hdr.integrity.size = integrity_size;
642 w->hdr.integrity.original_size = integrity_size;
643 w->hdr.integrity.flags = 0;
646 DEBUG("Truncating file to end of XML data.\n");
647 /* No integrity table to write. The file should be truncated
648 * because it's possible that the old file was longer (due to it
649 * including an integrity table, or due to its XML data being
651 if (fflush(fp) != 0) {
652 ERROR("Failed to flush stream for file `%s': %m\n",
654 return WIMLIB_ERR_WRITE;
656 if (ftruncate(fileno(fp), xml_end) != 0) {
657 ERROR("Failed to truncate `%s' to %"PRIu64" "
659 w->filename, xml_end);
660 return WIMLIB_ERR_WRITE;
662 memset(&w->hdr.integrity, 0, sizeof(struct resource_entry));
665 DEBUG("Overwriting header.\n");
666 /* Overwrite the header. */
667 if (fseeko(fp, 0, SEEK_SET) != 0) {
668 ERROR("Failed to seek to beginning of `%s': %m\n",
670 return WIMLIB_ERR_WRITE;
673 ret = write_header(&w->hdr, fp);
677 DEBUG("Closing file.\n");
678 if (fclose(fp) != 0) {
679 ERROR("Failed to close `%s': %m\n", w->filename);
680 return WIMLIB_ERR_WRITE;
686 FREE(integrity_table);
690 /* Write the metadata resource for the current image. */
691 int write_metadata_resource(WIMStruct *w)
699 struct lookup_table_entry *lte;
700 struct resource_entry *res_entry;
701 off_t metadata_offset;
702 u64 metadata_original_size;
703 u64 metadata_compressed_size;
705 u8 hash[WIM_HASH_SIZE];
707 DEBUG("Writing metadata resource for image %u\n", w->current_image);
710 root = wim_root_dentry(w);
711 metadata_ctype = wimlib_get_compression_type(w);
712 metadata_offset = ftello(out);
713 if (metadata_offset == -1)
714 return WIMLIB_ERR_WRITE;
716 subdir_offset = 8 + root->length + 8;
717 calculate_subdir_offsets(root, &subdir_offset);
718 metadata_original_size = subdir_offset;
719 buf = MALLOC(metadata_original_size);
721 ERROR("Failed to allocate %"PRIu64" bytes for "
722 "metadata resource\n", metadata_original_size);
723 return WIMLIB_ERR_NOMEM;
727 /* Write the security data. */
728 p = write_security_data(wim_security_data(w), p);
730 p = put_u32(p, 8); /* Total length of security data. */
731 p = put_u32(p, 0); /* Number of security data entries. */
734 DEBUG("Writing dentry tree.\n");
735 p = write_dentry_tree(root, p);
737 /* Like file resources, the lookup table entry for a metadata resource
738 * uses for the hash code a SHA1 message digest of its uncompressed
740 sha1_buffer(buf, metadata_original_size, hash);
742 ret = write_resource_from_memory(buf,
744 metadata_original_size,
746 &metadata_compressed_size);
751 /* Update the lookup table entry, including the hash and output resource
752 * entry fields, for this image's metadata resource. */
753 lte = wim_metadata_lookup_table_entry(w);
754 res_entry = <e->output_resource_entry;
756 if (memcmp(hash, lte->hash, WIM_HASH_SIZE) != 0) {
757 lookup_table_unlink(w->lookup_table, lte);
758 memcpy(lte->hash, hash, WIM_HASH_SIZE);
759 lookup_table_insert(w->lookup_table, lte);
761 res_entry->original_size = metadata_original_size;
762 res_entry->offset = metadata_offset;
763 res_entry->size = metadata_compressed_size;
764 res_entry->flags = WIM_RESHDR_FLAG_METADATA;
765 if (metadata_ctype != WIM_COMPRESSION_TYPE_NONE)
766 res_entry->flags |= WIM_RESHDR_FLAG_COMPRESSED;
770 /* Write the file resources for the current image. */
771 static int write_file_resources(WIMStruct *w)
774 DEBUG("Writing file resources for image %u\n", w->current_image);
775 return for_dentry_in_tree(wim_root_dentry(w), write_file_resource, w);
778 /* Write lookup table, xml data, lookup table, and rewrite header
780 * write_lt is zero iff the lookup table is not to be written; i.e. it is
781 * handled elsewhere. */
782 int finish_write(WIMStruct *w, int image, int flags, int write_lt)
784 off_t lookup_table_offset;
785 off_t xml_data_offset;
786 off_t lookup_table_size;
787 off_t integrity_offset;
790 off_t integrity_size;
793 struct wim_header hdr;
794 FILE *out = w->out_fp;
797 lookup_table_offset = ftello(out);
798 if (lookup_table_offset == -1)
799 return WIMLIB_ERR_WRITE;
801 DEBUG("Writing lookup table.\n");
802 /* Write the lookup table. */
803 ret = write_lookup_table(w->lookup_table, out);
808 DEBUG("Writing XML data.\n");
810 xml_data_offset = ftello(out);
811 if (xml_data_offset == -1)
812 return WIMLIB_ERR_WRITE;
814 /* @hdr will be the header for the new WIM. First copy all the data
815 * from the header in the WIMStruct; then set all the fields that may
816 * have changed, including the resource entries, boot index, and image
818 memcpy(&hdr, &w->hdr, sizeof(struct wim_header));
820 lookup_table_size = xml_data_offset - lookup_table_offset;
821 hdr.lookup_table_res_entry.offset = lookup_table_offset;
822 hdr.lookup_table_res_entry.size = lookup_table_size;
824 hdr.lookup_table_res_entry.original_size = hdr.lookup_table_res_entry.size;
825 hdr.lookup_table_res_entry.flags = WIM_RESHDR_FLAG_METADATA;
827 ret = write_xml_data(w->wim_info, image, out);
831 integrity_offset = ftello(out);
832 if (integrity_offset == -1)
833 return WIMLIB_ERR_WRITE;
834 xml_data_size = integrity_offset - xml_data_offset;
836 hdr.xml_res_entry.offset = xml_data_offset;
837 hdr.xml_res_entry.size = xml_data_size;
838 hdr.xml_res_entry.original_size = xml_data_size;
839 hdr.xml_res_entry.flags = 0;
841 if (flags & WIMLIB_WRITE_FLAG_CHECK_INTEGRITY) {
842 ret = write_integrity_table(out, WIM_HEADER_DISK_SIZE,
844 flags & WIMLIB_WRITE_FLAG_SHOW_PROGRESS);
847 end_offset = ftello(out);
848 if (end_offset == -1)
849 return WIMLIB_ERR_WRITE;
850 integrity_size = end_offset - integrity_offset;
851 hdr.integrity.offset = integrity_offset;
852 hdr.integrity.size = integrity_size;
853 hdr.integrity.original_size = integrity_size;
855 hdr.integrity.offset = 0;
856 hdr.integrity.size = 0;
857 hdr.integrity.original_size = 0;
859 hdr.integrity.flags = 0;
861 DEBUG("Updating WIM header.\n");
865 * In the WIM header, there is room for the resource entry for a
866 * metadata resource labeled as the "boot metadata". This entry should
867 * be zeroed out if there is no bootable image (boot_idx 0). Otherwise,
868 * it should be a copy of the resource entry for the image that is
869 * marked as bootable. This is not well documented...
871 if (hdr.boot_idx == 0 || !w->image_metadata
872 || (image != WIM_ALL_IMAGES && image != hdr.boot_idx)) {
873 memset(&hdr.boot_metadata_res_entry, 0,
874 sizeof(struct resource_entry));
876 memcpy(&hdr.boot_metadata_res_entry,
877 &w->image_metadata[hdr.boot_idx - 1].lookup_table_entry->
878 output_resource_entry,
879 sizeof(struct resource_entry));
882 /* Set image count and boot index correctly for single image writes */
883 if (image != WIM_ALL_IMAGES) {
885 if (hdr.boot_idx == image)
892 if (fseeko(out, 0, SEEK_SET) != 0)
893 return WIMLIB_ERR_WRITE;
895 return write_header(&hdr, out);
898 /* Open file stream and write dummy header for WIM. */
899 int begin_write(WIMStruct *w, const char *path, int flags)
902 DEBUG("Opening `%s' for new WIM\n", path);
904 /* checking the integrity requires going back over the file to read it.
906 * (It also would be possible to keep a running sha1sum as the file
907 * as written-- this would be faster, but a bit more complicated) */
908 if (flags & WIMLIB_WRITE_FLAG_CHECK_INTEGRITY)
913 w->out_fp = fopen(path, mode);
915 ERROR("Failed to open the file `%s' for writing!\n", path);
916 return WIMLIB_ERR_OPEN;
919 /* Write dummy header. It will be overwritten later. */
920 return write_header(&w->hdr, w->out_fp);
923 /* Writes the WIM to a file. */
924 WIMLIBAPI int wimlib_write(WIMStruct *w, const char *path, int image, int flags)
928 if (image != WIM_ALL_IMAGES &&
929 (image < 1 || image > w->hdr.image_count))
930 return WIMLIB_ERR_INVALID_IMAGE;
932 if (image == WIM_ALL_IMAGES)
933 DEBUG("Writing all images to `%s'\n", path);
935 DEBUG("Writing image %d to `%s'\n", image, path);
937 ret = begin_write(w, path, flags);
941 for_lookup_table_entry(w->lookup_table, zero_out_refcnts, NULL);
943 ret = for_image(w, image, write_file_resources);
945 ERROR("Failed to write file resources!\n");
949 ret = for_image(w, image, write_metadata_resource);
952 ERROR("Failed to write image metadata!\n");
956 ret = finish_write(w, image, flags, 1);
959 DEBUG("Closing output file.\n");
960 if (w->out_fp != NULL) {
961 if (fclose(w->out_fp) != 0) {
962 ERROR("Failed to close the file `%s': %m\n", path);
963 ret = WIMLIB_ERR_WRITE;