4 * Support for writing WIM files; write a WIM file, overwrite a WIM file, write
5 * compressed file resources, etc.
9 * Copyright (C) 2010 Carl Thijssen
10 * Copyright (C) 2012 Eric Biggers
12 * This file is part of wimlib, a library for working with WIM files.
14 * wimlib is free software; you can redistribute it and/or modify it under the
15 * terms of the GNU General Public License as published by the Free
16 * Software Foundation; either version 3 of the License, or (at your option)
19 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
20 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
21 * A PARTICULAR PURPOSE. See the GNU General Public License for more
24 * You should have received a copy of the GNU General Public License
25 * along with wimlib; if not, see http://www.gnu.org/licenses/.
28 #include "wimlib_internal.h"
31 #include "lookup_table.h"
36 #include <semaphore.h>
42 #include <ntfs-3g/attrib.h>
43 #include <ntfs-3g/inode.h>
44 #include <ntfs-3g/dir.h>
53 /* Reopens the FILE* for a WIM read-write. */
54 static int reopen_rw(WIMStruct *w)
58 if (fclose(w->fp) != 0)
59 ERROR_WITH_ERRNO("Failed to close the file `%s'", w->filename);
61 fp = fopen(w->filename, "r+b");
63 ERROR_WITH_ERRNO("Failed to open `%s' for reading and writing",
65 return WIMLIB_ERR_OPEN;
74 * Writes a WIM file to the original file that it was read from, overwriting it.
76 WIMLIBAPI int wimlib_overwrite(WIMStruct *w, int write_flags)
78 const char *wimfile_name;
83 return WIMLIB_ERR_INVALID_PARAM;
85 write_flags &= ~WIMLIB_WRITE_FLAG_NO_LOOKUP_TABLE;
87 wimfile_name = w->filename;
89 DEBUG("Replacing WIM file `%s'.", wimfile_name);
92 return WIMLIB_ERR_NO_FILENAME;
94 /* Write the WIM to a temporary file. */
95 /* XXX should the temporary file be somewhere else? */
96 wim_name_len = strlen(wimfile_name);
97 char tmpfile[wim_name_len + 10];
98 memcpy(tmpfile, wimfile_name, wim_name_len);
99 randomize_char_array_with_alnum(tmpfile + wim_name_len, 9);
100 tmpfile[wim_name_len + 9] = '\0';
102 ret = wimlib_write(w, tmpfile, WIM_ALL_IMAGES, write_flags);
104 ERROR("Failed to write the WIM file `%s'", tmpfile);
105 if (unlink(tmpfile) != 0)
106 WARNING("Failed to remove `%s'", tmpfile);
110 DEBUG("Closing original WIM file.");
111 /* Close the original WIM file that was opened for reading. */
113 if (fclose(w->fp) != 0) {
114 WARNING("Failed to close the file `%s'", wimfile_name);
119 DEBUG("Renaming `%s' to `%s'", tmpfile, wimfile_name);
121 /* Rename the new file to the old file .*/
122 if (rename(tmpfile, wimfile_name) != 0) {
123 ERROR_WITH_ERRNO("Failed to rename `%s' to `%s'",
124 tmpfile, wimfile_name);
125 /* Remove temporary file. */
126 if (unlink(tmpfile) != 0)
127 ERROR_WITH_ERRNO("Failed to remove `%s'", tmpfile);
128 return WIMLIB_ERR_RENAME;
131 if (write_flags & WIMLIB_WRITE_FLAG_VERBOSE)
132 printf("Successfully renamed `%s' to `%s'\n", tmpfile, wimfile_name);
137 static int check_resource_offset(struct lookup_table_entry *lte, void *arg)
139 u64 xml_data_offset = *(u64*)arg;
140 if (lte->resource_entry.offset > xml_data_offset) {
141 ERROR("The following resource is *after* the XML data:");
142 print_lookup_table_entry(lte);
143 return WIMLIB_ERR_RESOURCE_ORDER;
148 WIMLIBAPI int wimlib_overwrite_xml_and_header(WIMStruct *w, int write_flags)
152 u8 *integrity_table = NULL;
155 size_t bytes_written;
157 DEBUG("Overwriting XML and header of `%s', write_flags = %#x",
158 w->filename, write_flags);
161 return WIMLIB_ERR_NO_FILENAME;
163 write_flags &= ~WIMLIB_WRITE_FLAG_NO_LOOKUP_TABLE;
165 /* Make sure that the integrity table (if present) is after the XML
166 * data, and that there are no stream resources, metadata resources, or
167 * lookup tables after the XML data. Otherwise, these data would be
168 * destroyed by this function. */
169 if (w->hdr.integrity.offset != 0 &&
170 w->hdr.integrity.offset < w->hdr.xml_res_entry.offset) {
171 ERROR("Didn't expect the integrity table to be before the XML data");
172 return WIMLIB_ERR_RESOURCE_ORDER;
175 if (w->hdr.lookup_table_res_entry.offset >
176 w->hdr.xml_res_entry.offset) {
177 ERROR("Didn't expect the lookup table to be after the XML data");
178 return WIMLIB_ERR_RESOURCE_ORDER;
181 ret = for_lookup_table_entry(w->lookup_table, check_resource_offset,
182 &w->hdr.xml_res_entry.offset);
192 /* The old integrity table is still OK, as the SHA1 message digests in
193 * the integrity table include neither the header nor the XML data.
194 * Save it for later if it exists and an integrity table was required.
196 if ((write_flags & WIMLIB_WRITE_FLAG_CHECK_INTEGRITY)
197 && w->hdr.integrity.offset != 0)
199 DEBUG("Reading existing integrity table.");
200 integrity_table = MALLOC(w->hdr.integrity.size);
201 if (!integrity_table)
202 return WIMLIB_ERR_NOMEM;
204 ret = read_uncompressed_resource(fp, w->hdr.integrity.offset,
205 w->hdr.integrity.original_size,
209 DEBUG("Done reading existing integrity table.");
212 DEBUG("Overwriting XML data.");
213 /* Overwrite the XML data. */
214 if (fseeko(fp, w->hdr.xml_res_entry.offset, SEEK_SET) != 0) {
215 ERROR_WITH_ERRNO("Failed to seek to byte %"PRIu64" "
216 "for XML data", w->hdr.xml_res_entry.offset);
217 ret = WIMLIB_ERR_WRITE;
220 ret = write_xml_data(w->wim_info, WIM_ALL_IMAGES, fp, 0);
224 DEBUG("Updating XML resource entry.");
225 /* Update the XML resource entry in the WIM header. */
226 xml_end = ftello(fp);
228 ret = WIMLIB_ERR_WRITE;
231 xml_size = xml_end - w->hdr.xml_res_entry.offset;
232 w->hdr.xml_res_entry.size = xml_size;
233 w->hdr.xml_res_entry.original_size = xml_size;
234 /* XML data offset is unchanged. */
236 if (write_flags & WIMLIB_WRITE_FLAG_CHECK_INTEGRITY) {
237 DEBUG("Writing integrity table.");
238 w->hdr.integrity.offset = xml_end;
239 if (integrity_table) {
240 /* The existing integrity table was saved. */
241 bytes_written = fwrite(integrity_table, 1,
242 w->hdr.integrity.size, fp);
243 if (bytes_written != w->hdr.integrity.size) {
244 ERROR_WITH_ERRNO("Failed to write integrity "
246 ret = WIMLIB_ERR_WRITE;
249 FREE(integrity_table);
251 /* There was no existing integrity table, so a new one
252 * must be calculated. */
253 ret = write_integrity_table(fp, WIM_HEADER_DISK_SIZE,
254 w->hdr.lookup_table_res_entry.offset +
255 w->hdr.lookup_table_res_entry.size,
256 write_flags & WIMLIB_WRITE_FLAG_SHOW_PROGRESS);
260 off_t end_integrity = ftello(fp);
261 if (end_integrity == -1)
262 return WIMLIB_ERR_WRITE;
264 off_t integrity_size = end_integrity - xml_end;
265 w->hdr.integrity.size = integrity_size;
266 w->hdr.integrity.original_size = integrity_size;
267 w->hdr.integrity.flags = 0;
270 DEBUG("Truncating file to end of XML data.");
271 /* No integrity table to write. The file should be truncated
272 * because it's possible that the old file was longer (due to it
273 * including an integrity table, or due to its XML data being
275 if (fflush(fp) != 0) {
276 ERROR_WITH_ERRNO("Failed to flush stream for file `%s'",
278 return WIMLIB_ERR_WRITE;
280 if (ftruncate(fileno(fp), xml_end) != 0) {
281 ERROR_WITH_ERRNO("Failed to truncate `%s' to %"PRIu64" "
282 "bytes", w->filename, xml_end);
283 return WIMLIB_ERR_WRITE;
285 memset(&w->hdr.integrity, 0, sizeof(struct resource_entry));
288 DEBUG("Overwriting header.");
289 /* Overwrite the header. */
290 if (fseeko(fp, 0, SEEK_SET) != 0) {
291 ERROR_WITH_ERRNO("Failed to seek to beginning of `%s'",
293 return WIMLIB_ERR_WRITE;
296 ret = write_header(&w->hdr, fp);
300 DEBUG("Closing `%s'.", w->filename);
301 if (fclose(fp) != 0) {
302 ERROR_WITH_ERRNO("Failed to close `%s'", w->filename);
303 return WIMLIB_ERR_WRITE;
309 FREE(integrity_table);
314 /* Chunk table that's located at the beginning of each compressed resource in
315 * the WIM. (This is not the on-disk format; the on-disk format just has an
316 * array of offsets.) */
320 u64 original_resource_size;
321 u64 bytes_per_chunk_entry;
329 * Allocates and initializes a chunk table, and reserves space for it in the
333 begin_wim_resource_chunk_tab(const struct lookup_table_entry *lte,
336 struct chunk_table **chunk_tab_ret)
338 u64 size = wim_resource_size(lte);
339 u64 num_chunks = (size + WIM_CHUNK_SIZE - 1) / WIM_CHUNK_SIZE;
340 size_t alloc_size = sizeof(struct chunk_table) + num_chunks * sizeof(u64);
341 struct chunk_table *chunk_tab = CALLOC(1, alloc_size);
345 ERROR("Failed to allocate chunk table for %"PRIu64" byte "
347 ret = WIMLIB_ERR_NOMEM;
350 chunk_tab->file_offset = file_offset;
351 chunk_tab->num_chunks = num_chunks;
352 chunk_tab->original_resource_size = size;
353 chunk_tab->bytes_per_chunk_entry = (size >= (1ULL << 32)) ? 8 : 4;
354 chunk_tab->table_disk_size = chunk_tab->bytes_per_chunk_entry *
356 chunk_tab->cur_offset = 0;
357 chunk_tab->cur_offset_p = chunk_tab->offsets;
359 if (fwrite(chunk_tab, 1, chunk_tab->table_disk_size, out_fp) !=
360 chunk_tab->table_disk_size) {
361 ERROR_WITH_ERRNO("Failed to write chunk table in compressed "
363 ret = WIMLIB_ERR_WRITE;
369 *chunk_tab_ret = chunk_tab;
373 typedef int (*compress_func_t)(const void *, unsigned, void *, unsigned *);
375 compress_func_t get_compress_func(int out_ctype)
377 if (out_ctype == WIM_COMPRESSION_TYPE_LZX)
380 return xpress_compress;
385 * Compresses a chunk of a WIM resource.
387 * @chunk: Uncompressed data of the chunk.
388 * @chunk_size: Size of the uncompressed chunk in bytes.
389 * @compressed_chunk: Pointer to output buffer of size at least
390 * (@chunk_size - 1) bytes.
391 * @compressed_chunk_len_ret: Pointer to an unsigned int into which the size
392 * of the compressed chunk will be
394 * @ctype: Type of compression to use. Must be WIM_COMPRESSION_TYPE_LZX
395 * or WIM_COMPRESSION_TYPE_XPRESS.
397 * Returns zero if compressed succeeded, and nonzero if the chunk could not be
398 * compressed to any smaller than @chunk_size. This function cannot fail for
401 static int compress_chunk(const u8 chunk[], unsigned chunk_size,
402 u8 compressed_chunk[],
403 unsigned *compressed_chunk_len_ret,
406 compress_func_t compress = get_compress_func(ctype);
407 return (*compress)(chunk, chunk_size, compressed_chunk,
408 compressed_chunk_len_ret);
412 * Writes a chunk of a WIM resource to an output file.
414 * @chunk: Uncompressed data of the chunk.
415 * @chunk_size: Size of the chunk (<= WIM_CHUNK_SIZE)
416 * @out_fp: FILE * to write tho chunk to.
417 * @out_ctype: Compression type to use when writing the chunk (ignored if no
418 * chunk table provided)
419 * @chunk_tab: Pointer to chunk table being created. It is updated with the
420 * offset of the chunk we write.
422 * Returns 0 on success; nonzero on failure.
424 static int write_wim_resource_chunk(const u8 chunk[], unsigned chunk_size,
425 FILE *out_fp, int out_ctype,
426 struct chunk_table *chunk_tab)
429 unsigned out_chunk_size;
431 wimlib_assert(chunk_size <= WIM_CHUNK_SIZE);
435 out_chunk_size = chunk_size;
437 u8 *compressed_chunk = alloca(chunk_size);
440 ret = compress_chunk(chunk, chunk_size, compressed_chunk,
441 &out_chunk_size, out_ctype);
443 out_chunk = compressed_chunk;
446 out_chunk_size = chunk_size;
448 *chunk_tab->cur_offset_p++ = chunk_tab->cur_offset;
449 chunk_tab->cur_offset += out_chunk_size;
452 if (fwrite(out_chunk, 1, out_chunk_size, out_fp) != out_chunk_size) {
453 ERROR_WITH_ERRNO("Failed to write WIM resource chunk");
454 return WIMLIB_ERR_WRITE;
460 * Finishes a WIM chunk tale and writes it to the output file at the correct
463 * The final size of the full compressed resource is returned in the
464 * @compressed_size_p.
467 finish_wim_resource_chunk_tab(struct chunk_table *chunk_tab,
468 FILE *out_fp, u64 *compressed_size_p)
470 size_t bytes_written;
471 if (fseeko(out_fp, chunk_tab->file_offset, SEEK_SET) != 0) {
472 ERROR_WITH_ERRNO("Failed to seek to byte %"PRIu64" of output "
473 "WIM file", chunk_tab->file_offset);
474 return WIMLIB_ERR_WRITE;
477 if (chunk_tab->bytes_per_chunk_entry == 8) {
478 array_cpu_to_le64(chunk_tab->offsets, chunk_tab->num_chunks);
480 for (u64 i = 0; i < chunk_tab->num_chunks; i++)
481 ((u32*)chunk_tab->offsets)[i] =
482 cpu_to_le32(chunk_tab->offsets[i]);
484 bytes_written = fwrite((u8*)chunk_tab->offsets +
485 chunk_tab->bytes_per_chunk_entry,
486 1, chunk_tab->table_disk_size, out_fp);
487 if (bytes_written != chunk_tab->table_disk_size) {
488 ERROR_WITH_ERRNO("Failed to write chunk table in compressed "
490 return WIMLIB_ERR_WRITE;
492 if (fseeko(out_fp, 0, SEEK_END) != 0) {
493 ERROR_WITH_ERRNO("Failed to seek to end of output WIM file");
494 return WIMLIB_ERR_WRITE;
496 *compressed_size_p = chunk_tab->cur_offset + chunk_tab->table_disk_size;
500 static int prepare_resource_for_read(struct lookup_table_entry *lte
503 , ntfs_inode **ni_ret
507 if (lte->resource_location == RESOURCE_IN_FILE_ON_DISK
508 && !lte->file_on_disk_fp)
510 wimlib_assert(lte->file_on_disk);
511 lte->file_on_disk_fp = fopen(lte->file_on_disk, "rb");
512 if (!lte->file_on_disk_fp) {
513 ERROR_WITH_ERRNO("Failed to open the file `%s' for "
514 "reading", lte->file_on_disk);
515 return WIMLIB_ERR_OPEN;
519 else if (lte->resource_location == RESOURCE_IN_NTFS_VOLUME
522 struct ntfs_location *loc = lte->ntfs_loc;
525 ni = ntfs_pathname_to_inode(*loc->ntfs_vol_p, NULL, loc->path_utf8);
527 ERROR_WITH_ERRNO("Failed to open inode `%s' in NTFS "
528 "volume", loc->path_utf8);
529 return WIMLIB_ERR_NTFS_3G;
531 lte->attr = ntfs_attr_open(ni,
532 loc->is_reparse_point ? AT_REPARSE_POINT : AT_DATA,
533 (ntfschar*)loc->stream_name_utf16,
534 loc->stream_name_utf16_num_chars);
536 ERROR_WITH_ERRNO("Failed to open attribute of `%s' in "
537 "NTFS volume", loc->path_utf8);
538 ntfs_inode_close(ni);
539 return WIMLIB_ERR_NTFS_3G;
547 static void end_wim_resource_read(struct lookup_table_entry *lte
553 if (lte->resource_location == RESOURCE_IN_FILE_ON_DISK
554 && lte->file_on_disk_fp) {
555 fclose(lte->file_on_disk_fp);
556 lte->file_on_disk_fp = NULL;
559 else if (lte->resource_location == RESOURCE_IN_NTFS_VOLUME) {
561 ntfs_attr_close(lte->attr);
565 ntfs_inode_close(ni);
571 * Writes a WIM resource to a FILE * opened for writing. The resource may be
572 * written uncompressed or compressed depending on the @out_ctype parameter.
574 * If by chance the resource compresses to more than the original size (this may
575 * happen with random data or files than are pre-compressed), the resource is
576 * instead written uncompressed (and this is reflected in the @out_res_entry by
577 * removing the WIM_RESHDR_FLAG_COMPRESSED flag).
579 * @lte: The lookup table entry for the WIM resource.
580 * @out_fp: The FILE * to write the resource to.
581 * @out_ctype: The compression type of the resource to write. Note: if this is
582 * the same as the compression type of the WIM resource we
583 * need to read, we simply copy the data (i.e. we do not
584 * uncompress it, then compress it again).
585 * @out_res_entry: If non-NULL, a resource entry that is filled in with the
586 * offset, original size, compressed size, and compression flag
587 * of the output resource.
589 * Returns 0 on success; nonzero on failure.
591 int write_wim_resource(struct lookup_table_entry *lte,
592 FILE *out_fp, int out_ctype,
593 struct resource_entry *out_res_entry,
598 u64 old_compressed_size;
599 u64 new_compressed_size;
602 struct chunk_table *chunk_tab = NULL;
606 ntfs_inode *ni = NULL;
611 /* Original size of the resource */
612 original_size = wim_resource_size(lte);
614 /* Compressed size of the resource (as it exists now) */
615 old_compressed_size = wim_resource_compressed_size(lte);
617 /* Current offset in output file */
618 file_offset = ftello(out_fp);
619 if (file_offset == -1) {
620 ERROR_WITH_ERRNO("Failed to get offset in output "
622 return WIMLIB_ERR_WRITE;
625 /* Are the compression types the same? If so, do a raw copy (copy
626 * without decompressing and recompressing the data). */
627 raw = (wim_resource_compression_type(lte) == out_ctype
628 && out_ctype != WIM_COMPRESSION_TYPE_NONE);
631 flags |= WIMLIB_RESOURCE_FLAG_RAW;
632 bytes_remaining = old_compressed_size;
634 flags &= ~WIMLIB_RESOURCE_FLAG_RAW;
635 bytes_remaining = original_size;
638 /* Empty resource; nothing needs to be done, so just return success. */
639 if (bytes_remaining == 0)
642 /* Buffer for reading chunks for the resource */
643 u8 buf[min(WIM_CHUNK_SIZE, bytes_remaining)];
645 /* If we are writing a compressed resource and not doing a raw copy, we
646 * need to initialize the chunk table */
647 if (out_ctype != WIM_COMPRESSION_TYPE_NONE && !raw) {
648 ret = begin_wim_resource_chunk_tab(lte, out_fp, file_offset,
654 /* If the WIM resource is in an external file, open a FILE * to it so we
655 * don't have to open a temporary one in read_wim_resource() for each
658 ret = prepare_resource_for_read(lte, &ni);
660 ret = prepare_resource_for_read(lte);
665 /* If we aren't doing a raw copy, we will compute the SHA1 message
666 * digest of the resource as we read it, and verify it's the same as the
667 * hash given in the lookup table entry once we've finished reading the
673 /* While there are still bytes remaining in the WIM resource, read a
674 * chunk of the resource, update SHA1, then write that chunk using the
675 * desired compression type. */
678 u64 to_read = min(bytes_remaining, WIM_CHUNK_SIZE);
679 ret = read_wim_resource(lte, buf, to_read, offset, flags);
683 sha1_update(&ctx, buf, to_read);
684 ret = write_wim_resource_chunk(buf, to_read, out_fp,
685 out_ctype, chunk_tab);
688 bytes_remaining -= to_read;
690 } while (bytes_remaining);
692 /* Raw copy: The new compressed size is the same as the old compressed
695 * Using WIM_COMPRESSION_TYPE_NONE: The new compressed size is the
698 * Using a different compression type: Call
699 * finish_wim_resource_chunk_tab() and it will provide the new
703 new_compressed_size = old_compressed_size;
705 if (out_ctype == WIM_COMPRESSION_TYPE_NONE)
706 new_compressed_size = original_size;
708 ret = finish_wim_resource_chunk_tab(chunk_tab, out_fp,
709 &new_compressed_size);
715 /* Verify SHA1 message digest of the resource, unless we are doing a raw
716 * write (in which case we never even saw the uncompressed data). Or,
717 * if the hash we had before is all 0's, just re-set it to be the new
720 u8 md[SHA1_HASH_SIZE];
721 sha1_final(md, &ctx);
722 if (is_zero_hash(lte->hash)) {
723 copy_hash(lte->hash, md);
724 } else if (!hashes_equal(md, lte->hash)) {
725 ERROR("WIM resource has incorrect hash!");
726 if (lte->resource_location == RESOURCE_IN_FILE_ON_DISK) {
727 ERROR("We were reading it from `%s'; maybe it changed "
728 "while we were reading it.",
731 ret = WIMLIB_ERR_INVALID_RESOURCE_HASH;
736 if (!raw && new_compressed_size >= original_size &&
737 out_ctype != WIM_COMPRESSION_TYPE_NONE)
739 /* Oops! We compressed the resource to larger than the original
740 * size. Write the resource uncompressed instead. */
741 if (fseeko(out_fp, file_offset, SEEK_SET) != 0) {
742 ERROR_WITH_ERRNO("Failed to seek to byte %"PRIu64" "
743 "of output WIM file", file_offset);
744 ret = WIMLIB_ERR_WRITE;
747 ret = write_wim_resource(lte, out_fp, WIM_COMPRESSION_TYPE_NONE,
748 out_res_entry, flags);
751 if (fflush(out_fp) != 0) {
752 ERROR_WITH_ERRNO("Failed to flush output WIM file");
753 ret = WIMLIB_ERR_WRITE;
756 if (ftruncate(fileno(out_fp), file_offset + out_res_entry->size) != 0) {
757 ERROR_WITH_ERRNO("Failed to truncate output WIM file");
758 ret = WIMLIB_ERR_WRITE;
763 out_res_entry->size = new_compressed_size;
764 out_res_entry->original_size = original_size;
765 out_res_entry->offset = file_offset;
766 out_res_entry->flags = lte->resource_entry.flags
767 & ~WIM_RESHDR_FLAG_COMPRESSED;
768 if (out_ctype != WIM_COMPRESSION_TYPE_NONE)
769 out_res_entry->flags |= WIM_RESHDR_FLAG_COMPRESSED;
775 end_wim_resource_read(lte, ni);
777 end_wim_resource_read(lte);
785 struct shared_queue {
788 pthread_mutex_t lock;
795 static int shared_queue_init(struct shared_queue *q, unsigned size)
797 q->array = CALLOC(sizeof(q->array[0]), size);
799 return WIMLIB_ERR_NOMEM;
801 sem_init(&q->filled_slots, 0, 0);
802 sem_init(&q->empty_slots, 0, size);
803 pthread_mutex_init(&q->lock, NULL);
810 static void shared_queue_destroy(struct shared_queue *q)
812 sem_destroy(&q->filled_slots);
813 sem_destroy(&q->empty_slots);
814 pthread_mutex_destroy(&q->lock);
818 static void shared_queue_put(struct shared_queue *q, void *obj)
820 sem_wait(&q->empty_slots);
821 pthread_mutex_lock(&q->lock);
823 q->back = (q->back + 1) % q->size;
824 q->array[q->back] = obj;
826 sem_post(&q->filled_slots);
827 pthread_mutex_unlock(&q->lock);
830 static void *shared_queue_get(struct shared_queue *q)
832 sem_wait(&q->filled_slots);
833 pthread_mutex_lock(&q->lock);
835 void *obj = q->array[q->front];
836 q->array[q->front] = NULL;
837 q->front = (q->front + 1) % q->size;
839 sem_post(&q->empty_slots);
840 pthread_mutex_unlock(&q->lock);
844 static inline int shared_queue_get_filled(struct shared_queue *q)
847 sem_getvalue(&q->filled_slots, &sval);
851 struct compressor_thread_params {
852 struct shared_queue *res_to_compress_queue;
853 struct shared_queue *compressed_res_queue;
854 compress_func_t compress;
857 #define MAX_CHUNKS_PER_MSG 2
860 struct lookup_table_entry *lte;
861 u8 *uncompressed_chunks[MAX_CHUNKS_PER_MSG];
862 u8 *out_compressed_chunks[MAX_CHUNKS_PER_MSG];
863 u8 *compressed_chunks[MAX_CHUNKS_PER_MSG];
864 unsigned uncompressed_chunk_sizes[MAX_CHUNKS_PER_MSG];
865 unsigned compressed_chunk_sizes[MAX_CHUNKS_PER_MSG];
867 struct list_head list;
872 static void compress_chunks(struct message *msg, compress_func_t compress)
874 for (unsigned i = 0; i < msg->num_chunks; i++) {
875 DEBUG2("compress chunk %u of %u", i, msg->num_chunks);
876 int ret = compress(msg->uncompressed_chunks[i],
877 msg->uncompressed_chunk_sizes[i],
878 msg->compressed_chunks[i],
879 &msg->compressed_chunk_sizes[i]);
881 msg->out_compressed_chunks[i] = msg->compressed_chunks[i];
883 msg->out_compressed_chunks[i] = msg->uncompressed_chunks[i];
884 msg->compressed_chunk_sizes[i] = msg->uncompressed_chunk_sizes[i];
889 static void *compressor_thread_proc(void *arg)
891 struct compressor_thread_params *params = arg;
892 struct shared_queue *res_to_compress_queue = params->res_to_compress_queue;
893 struct shared_queue *compressed_res_queue = params->compressed_res_queue;
894 compress_func_t compress = params->compress;
897 DEBUG("Compressor thread ready");
898 while ((msg = shared_queue_get(res_to_compress_queue)) != NULL) {
899 compress_chunks(msg, compress);
900 shared_queue_put(compressed_res_queue, msg);
902 DEBUG("Compressor thread terminating");
905 static int write_stream_list_serial(struct list_head *stream_list,
906 FILE *out_fp, int out_ctype,
909 struct lookup_table_entry *lte;
912 list_for_each_entry(lte, stream_list, staging_list) {
913 ret = write_wim_resource(lte, out_fp, out_ctype,
914 <e->output_resource_entry, 0);
921 static int write_wim_chunks(struct message *msg, FILE *out_fp,
922 struct chunk_table *chunk_tab)
924 for (unsigned i = 0; i < msg->num_chunks; i++) {
925 unsigned chunk_csize = msg->compressed_chunk_sizes[i];
927 DEBUG2("Write wim chunk %u of %u (csize = %u)",
928 i, msg->num_chunks, chunk_csize);
930 if (fwrite(msg->out_compressed_chunks[i], 1, chunk_csize, out_fp)
933 ERROR_WITH_ERRNO("Failed to write WIM");
934 return WIMLIB_ERR_WRITE;
937 *chunk_tab->cur_offset_p++ = chunk_tab->cur_offset;
938 chunk_tab->cur_offset += chunk_csize;
944 * This function is executed by the main thread when the resources are being
945 * compressed in parallel. The main thread is in change of all reading of the
946 * uncompressed data and writing of the compressed data. The compressor threads
947 * *only* do compression from/to in-memory buffers.
949 * Each unit of work given to a compressor thread is up to MAX_CHUNKS_PER_MSG
950 * chunks of compressed data to compress, represented in a `struct message'.
951 * Each message is passed from the main thread to a worker thread through the
952 * res_to_compress_queue, and it is passed back through the
953 * compressed_res_queue.
955 static int main_writer_thread_proc(struct list_head *stream_list,
958 struct shared_queue *res_to_compress_queue,
959 struct shared_queue *compressed_res_queue,
965 struct message msgs[queue_size];
968 // Initially, all the messages are available to use.
969 LIST_HEAD(available_msgs);
970 for (size_t i = 0; i < ARRAY_LEN(msgs); i++)
971 list_add(&msgs[i].list, &available_msgs);
973 // outstanding_resources is the list of resources that currently have
974 // had chunks sent off for compression.
976 // The first stream in outstanding_resources is the stream that is
977 // currently being written (cur_lte).
979 // The last stream in outstanding_resources is the stream that is
980 // currently being read and chunks fed to the compressor threads
983 // Depending on the number of threads and the sizes of the resource,
984 // the outstanding streams list may contain streams between cur_lte and
985 // next_lte that have all their chunks compressed or being compressed,
986 // but haven't been written yet.
988 LIST_HEAD(outstanding_resources);
989 struct list_head *next_resource = stream_list->next;
990 struct lookup_table_entry *next_lte = container_of(next_resource,
991 struct lookup_table_entry,
993 next_resource = next_resource->next;
995 u64 next_num_chunks = wim_resource_chunks(next_lte);
996 INIT_LIST_HEAD(&next_lte->msg_list);
997 list_add_tail(&next_lte->staging_list, &outstanding_resources);
999 // As in write_wim_resource(), each resource we read is checksummed.
1000 SHA_CTX next_sha_ctx;
1001 sha1_init(&next_sha_ctx);
1002 u8 next_hash[SHA1_HASH_SIZE];
1004 // Resources that don't need any chunks compressed are added to this
1005 // list and written directly by the main thread.
1006 LIST_HEAD(my_resources);
1008 struct lookup_table_entry *cur_lte = next_lte;
1009 struct chunk_table *cur_chunk_tab = NULL;
1010 struct lookup_table_entry *lte;
1011 struct message *msg;
1014 ntfs_inode *ni = NULL;
1018 ret = prepare_resource_for_read(next_lte, &ni);
1020 ret = prepare_resource_for_read(next_lte);
1023 DEBUG("Initializing buffers for uncompressed "
1024 "and compressed data (%zu bytes needed)",
1025 queue_size * MAX_CHUNKS_PER_MSG * WIM_CHUNK_SIZE * 2);
1027 // Pre-allocate all the buffers that will be needed to do the chunk
1029 for (size_t i = 0; i < ARRAY_LEN(msgs); i++) {
1030 for (size_t j = 0; j < MAX_CHUNKS_PER_MSG; j++) {
1031 msgs[i].compressed_chunks[j] = MALLOC(WIM_CHUNK_SIZE);
1032 msgs[i].uncompressed_chunks[j] = MALLOC(WIM_CHUNK_SIZE);
1033 if (msgs[i].compressed_chunks[j] == NULL ||
1034 msgs[i].uncompressed_chunks[j] == NULL)
1036 ERROR("Could not allocate enough memory for "
1037 "multi-threaded compression");
1038 ret = WIMLIB_ERR_NOMEM;
1045 // Send chunks to the compressor threads until either (a) there
1046 // are no more messages available since they were all sent off,
1047 // or (b) there are no more resources that need to be
1049 while (!list_empty(&available_msgs) && next_lte != NULL) {
1051 // Get a message from the available messages
1053 msg = container_of(available_msgs.next,
1057 // ... and delete it from the available messages
1059 list_del(&msg->list);
1061 // Initialize the message with the chunks to
1063 msg->num_chunks = min(next_num_chunks - next_chunk,
1064 MAX_CHUNKS_PER_MSG);
1065 msg->lte = next_lte;
1066 msg->complete = false;
1067 msg->begin_chunk = next_chunk;
1069 unsigned size = WIM_CHUNK_SIZE;
1070 for (unsigned i = 0; i < msg->num_chunks; i++) {
1072 // Read chunk @next_chunk of the stream into the
1073 // message so that a compressor thread can
1076 if (next_chunk == next_num_chunks - 1 &&
1077 wim_resource_size(next_lte) % WIM_CHUNK_SIZE != 0)
1079 size = wim_resource_size(next_lte) % WIM_CHUNK_SIZE;
1083 DEBUG2("Read resource (size=%u, offset=%zu)",
1084 size, next_chunk * WIM_CHUNK_SIZE);
1086 msg->uncompressed_chunk_sizes[i] = size;
1088 ret = read_wim_resource(next_lte,
1089 msg->uncompressed_chunks[i],
1091 next_chunk * WIM_CHUNK_SIZE,
1095 sha1_update(&next_sha_ctx,
1096 msg->uncompressed_chunks[i], size);
1100 // Send the compression request
1101 list_add_tail(&msg->list, &next_lte->msg_list);
1102 shared_queue_put(res_to_compress_queue, msg);
1103 DEBUG2("Compression request sent");
1105 if (next_chunk != next_num_chunks)
1106 // More chunks to send for this resource
1109 // Done sending compression requests for a resource!
1110 // Check the SHA1 message digest.
1111 DEBUG2("Finalize SHA1 md (next_num_chunks=%zu)", next_num_chunks);
1112 sha1_final(next_hash, &next_sha_ctx);
1113 if (!hashes_equal(next_lte->hash, next_hash)) {
1114 ERROR("WIM resource has incorrect hash!");
1115 if (next_lte->resource_location == RESOURCE_IN_FILE_ON_DISK) {
1116 ERROR("We were reading it from `%s'; maybe it changed "
1117 "while we were reading it.",
1118 next_lte->file_on_disk);
1120 ret = WIMLIB_ERR_INVALID_RESOURCE_HASH;
1124 // Advance to the next resource.
1126 // If the next resource needs no compression, just write
1127 // it with this thread (not now though--- we could be in
1128 // the middle of writing another resource.) Keep doing
1129 // this until we either get to the end of the resources
1130 // list, or we get to a resource that needs compression.
1133 if (next_resource == stream_list) {
1138 end_wim_resource_read(next_lte, ni);
1141 end_wim_resource_read(next_lte);
1144 next_lte = container_of(next_resource,
1145 struct lookup_table_entry,
1147 next_resource = next_resource->next;
1148 if ((next_lte->resource_location == RESOURCE_IN_WIM
1149 && wimlib_get_compression_type(next_lte->wim) == out_ctype)
1150 || wim_resource_size(next_lte) == 0)
1152 list_add_tail(&next_lte->staging_list,
1155 list_add_tail(&next_lte->staging_list,
1156 &outstanding_resources);
1158 next_num_chunks = wim_resource_chunks(next_lte);
1159 sha1_init(&next_sha_ctx);
1160 INIT_LIST_HEAD(&next_lte->msg_list);
1162 ret = prepare_resource_for_read(next_lte, &ni);
1164 ret = prepare_resource_for_read(next_lte);
1168 DEBUG2("Updated next_lte");
1174 // If there are no outstanding resources, there are no more
1175 // resources that need to be written.
1176 if (list_empty(&outstanding_resources)) {
1177 DEBUG("No outstanding resources! Done");
1182 // Get the next message from the queue and process it.
1183 // The message will contain 1 or more data chunks that have been
1185 DEBUG2("Waiting for message");
1186 msg = shared_queue_get(compressed_res_queue);
1187 msg->complete = true;
1189 DEBUG2("Received msg (begin_chunk=%"PRIu64")", msg->begin_chunk);
1191 list_for_each_entry(msg, &cur_lte->msg_list, list) {
1192 DEBUG2("complete=%d", msg->complete);
1195 // Is this the next chunk in the current resource? If it's not
1196 // (i.e., an earlier chunk in a same or different resource
1197 // hasn't been compressed yet), do nothing, and keep this
1198 // message around until all earlier chunks are received.
1200 // Otherwise, write all the chunks we can.
1201 while (!list_empty(&cur_lte->msg_list)
1202 && (msg = container_of(cur_lte->msg_list.next,
1206 DEBUG2("Complete msg (begin_chunk=%"PRIu64")", msg->begin_chunk);
1207 if (msg->begin_chunk == 0) {
1208 DEBUG2("Begin chunk tab");
1209 // This is the first set of chunks. Leave space
1210 // for the chunk table in the output file.
1211 off_t cur_offset = ftello(out_fp);
1212 if (cur_offset == -1) {
1213 ret = WIMLIB_ERR_WRITE;
1216 ret = begin_wim_resource_chunk_tab(cur_lte,
1224 // Write the compressed chunks from the message.
1225 ret = write_wim_chunks(msg, out_fp, cur_chunk_tab);
1229 list_del(&msg->list);
1231 // This message is available to use for different chunks
1233 list_add(&msg->list, &available_msgs);
1235 // Was this the last chunk of the stream? If so,
1237 if (list_empty(&cur_lte->msg_list) &&
1238 msg->begin_chunk + msg->num_chunks == cur_chunk_tab->num_chunks)
1240 DEBUG2("Finish wim chunk tab");
1242 ret = finish_wim_resource_chunk_tab(cur_chunk_tab,
1249 cur_lte->output_resource_entry.size =
1252 cur_lte->output_resource_entry.original_size =
1253 cur_lte->resource_entry.original_size;
1255 cur_lte->output_resource_entry.offset =
1256 cur_chunk_tab->file_offset;
1258 cur_lte->output_resource_entry.flags =
1259 cur_lte->resource_entry.flags |
1260 WIM_RESHDR_FLAG_COMPRESSED;
1262 FREE(cur_chunk_tab);
1263 cur_chunk_tab = NULL;
1265 struct list_head *next = cur_lte->staging_list.next;
1266 list_del(&cur_lte->staging_list);
1268 if (next == &outstanding_resources) {
1269 DEBUG("No more outstanding resources");
1273 cur_lte = container_of(cur_lte->staging_list.next,
1274 struct lookup_table_entry,
1278 // Since we just finished writing a stream,
1279 // write any streams that have been added to the
1280 // my_resources list for direct writing by the
1281 // main thread (e.g. resources that don't need
1282 // to be compressed because the desired
1283 // compression type is the same as the previous
1284 // compression type).
1285 struct lookup_table_entry *tmp;
1286 list_for_each_entry_safe(lte,
1291 ret = write_wim_resource(lte,
1294 <e->output_resource_entry,
1296 list_del(<e->staging_list);
1306 end_wim_resource_read(cur_lte, ni);
1308 end_wim_resource_read(cur_lte);
1311 list_for_each_entry(lte, &my_resources, staging_list) {
1312 ret = write_wim_resource(lte, out_fp,
1314 <e->output_resource_entry,
1320 size_t num_available_msgs = 0;
1321 struct list_head *cur;
1323 list_for_each(cur, &available_msgs) {
1324 num_available_msgs++;
1327 while (num_available_msgs < ARRAY_LEN(msgs)) {
1328 shared_queue_get(compressed_res_queue);
1329 num_available_msgs++;
1333 DEBUG("Freeing messages");
1335 for (size_t i = 0; i < ARRAY_LEN(msgs); i++) {
1336 for (size_t j = 0; j < MAX_CHUNKS_PER_MSG; j++) {
1337 FREE(msgs[i].compressed_chunks[j]);
1338 FREE(msgs[i].uncompressed_chunks[j]);
1342 if (cur_chunk_tab != NULL)
1343 FREE(cur_chunk_tab);
1347 static int write_stream_list_parallel(struct list_head *stream_list,
1348 FILE *out_fp, int out_ctype,
1353 struct shared_queue res_to_compress_queue;
1354 struct shared_queue compressed_res_queue;
1356 nthreads = sysconf(_SC_NPROCESSORS_ONLN);
1358 WARNING("Could not determine number of processors! Assuming 1");
1362 wimlib_assert(stream_list->next != stream_list);
1365 pthread_t compressor_threads[nthreads];
1367 static const double MESSAGES_PER_THREAD = 2.0;
1368 size_t queue_size = (size_t)(nthreads * MESSAGES_PER_THREAD);
1370 DEBUG("Initializing shared queues (queue_size=%zu)", queue_size);
1372 ret = shared_queue_init(&res_to_compress_queue, queue_size);
1376 ret = shared_queue_init(&compressed_res_queue, queue_size);
1378 goto out_destroy_res_to_compress_queue;
1380 struct compressor_thread_params params;
1381 params.res_to_compress_queue = &res_to_compress_queue;
1382 params.compressed_res_queue = &compressed_res_queue;
1383 params.compress = get_compress_func(out_ctype);
1385 for (long i = 0; i < nthreads; i++) {
1386 DEBUG("pthread_create thread %ld", i);
1387 ret = pthread_create(&compressor_threads[i], NULL,
1388 compressor_thread_proc, ¶ms);
1390 ERROR_WITH_ERRNO("Failed to create compressor "
1397 if (write_flags & WIMLIB_WRITE_FLAG_VERBOSE) {
1398 printf("Writing compressed data using %ld threads...\n",
1402 ret = main_writer_thread_proc(stream_list,
1405 &res_to_compress_queue,
1406 &compressed_res_queue,
1410 for (long i = 0; i < nthreads; i++)
1411 shared_queue_put(&res_to_compress_queue, NULL);
1413 for (long i = 0; i < nthreads; i++) {
1414 if (pthread_join(compressor_threads[i], NULL)) {
1415 WARNING("Failed to join compressor thread %ld: %s",
1416 i, strerror(errno));
1420 shared_queue_destroy(&compressed_res_queue);
1421 out_destroy_res_to_compress_queue:
1422 shared_queue_destroy(&res_to_compress_queue);
1423 if (ret >= 0 && ret != WIMLIB_ERR_NOMEM)
1426 WARNING("Falling back to single-threaded compression");
1427 return write_stream_list_serial(stream_list, out_fp,
1428 out_ctype, write_flags);
1431 static int write_stream_list(struct list_head *stream_list, FILE *out_fp,
1432 int out_ctype, int write_flags)
1434 struct lookup_table_entry *lte;
1435 size_t num_streams = 0;
1437 bool compression_needed = false;
1439 list_for_each_entry(lte, stream_list, staging_list) {
1441 total_size += wim_resource_size(lte);
1442 if (!compression_needed
1443 && out_ctype != WIM_COMPRESSION_TYPE_NONE
1444 && (lte->resource_location != RESOURCE_IN_WIM
1445 || wimlib_get_compression_type(lte->wim) != out_ctype)
1446 && wim_resource_size(lte) != 0)
1447 compression_needed = true;
1450 if (write_flags & WIMLIB_WRITE_FLAG_VERBOSE) {
1451 printf("Preparing to write %zu streams "
1452 "(%"PRIu64" total bytes uncompressed)\n",
1453 num_streams, total_size);
1454 printf("Using compression type %s\n",
1455 wimlib_get_compression_type_string(out_ctype));
1458 if (compression_needed && total_size >= 1000000) {
1459 return write_stream_list_parallel(stream_list, out_fp,
1460 out_ctype, write_flags);
1462 if (write_flags & WIMLIB_WRITE_FLAG_VERBOSE) {
1463 puts("Using 1 thread (no compression needed)");
1466 return write_stream_list_serial(stream_list, out_fp,
1467 out_ctype, write_flags);
1472 static int dentry_find_streams_to_write(struct dentry *dentry,
1476 struct list_head *stream_list = w->private;
1477 struct lookup_table_entry *lte;
1478 for (unsigned i = 0; i <= dentry->d_inode->num_ads; i++) {
1479 lte = inode_stream_lte(dentry->d_inode, i, w->lookup_table);
1480 if (lte && ++lte->out_refcnt == 1)
1481 list_add(<e->staging_list, stream_list);
1486 static int find_streams_to_write(WIMStruct *w)
1488 return for_dentry_in_tree(wim_root_dentry(w),
1489 dentry_find_streams_to_write, w);
1492 static int write_wim_streams(WIMStruct *w, int image, int write_flags)
1495 LIST_HEAD(stream_list);
1497 w->private = &stream_list;
1498 for_image(w, image, find_streams_to_write);
1499 return write_stream_list(&stream_list, w->out_fp,
1500 wimlib_get_compression_type(w), write_flags);
1504 * Write the lookup table, xml data, and integrity table, then overwrite the WIM
1507 int finish_write(WIMStruct *w, int image, int write_flags)
1509 off_t lookup_table_offset;
1510 off_t xml_data_offset;
1511 off_t lookup_table_size;
1512 off_t integrity_offset;
1513 off_t xml_data_size;
1515 off_t integrity_size;
1517 struct wim_header hdr;
1518 FILE *out = w->out_fp;
1520 if (!(write_flags & WIMLIB_WRITE_FLAG_NO_LOOKUP_TABLE)) {
1521 /* Write the lookup table. */
1522 lookup_table_offset = ftello(out);
1523 if (lookup_table_offset == -1)
1524 return WIMLIB_ERR_WRITE;
1526 DEBUG("Writing lookup table (offset %"PRIu64")",
1527 lookup_table_offset);
1528 ret = write_lookup_table(w->lookup_table, out);
1533 xml_data_offset = ftello(out);
1534 if (xml_data_offset == -1)
1535 return WIMLIB_ERR_WRITE;
1537 /* @hdr will be the header for the new WIM. First copy all the data
1538 * from the header in the WIMStruct; then set all the fields that may
1539 * have changed, including the resource entries, boot index, and image
1541 memcpy(&hdr, &w->hdr, sizeof(struct wim_header));
1542 if (!(write_flags & WIMLIB_WRITE_FLAG_NO_LOOKUP_TABLE)) {
1543 lookup_table_size = xml_data_offset - lookup_table_offset;
1544 hdr.lookup_table_res_entry.offset = lookup_table_offset;
1545 hdr.lookup_table_res_entry.size = lookup_table_size;
1547 hdr.lookup_table_res_entry.original_size = hdr.lookup_table_res_entry.size;
1548 hdr.lookup_table_res_entry.flags = WIM_RESHDR_FLAG_METADATA;
1550 DEBUG("Writing XML data (offset %"PRIu64")", xml_data_offset);
1551 ret = write_xml_data(w->wim_info, image, out,
1552 (write_flags & WIMLIB_WRITE_FLAG_NO_LOOKUP_TABLE) ?
1553 wim_info_get_total_bytes(w->wim_info) : 0);
1557 integrity_offset = ftello(out);
1558 if (integrity_offset == -1)
1559 return WIMLIB_ERR_WRITE;
1560 xml_data_size = integrity_offset - xml_data_offset;
1562 hdr.xml_res_entry.offset = xml_data_offset;
1563 hdr.xml_res_entry.size = xml_data_size;
1564 hdr.xml_res_entry.original_size = xml_data_size;
1565 hdr.xml_res_entry.flags = 0;
1567 if (write_flags & WIMLIB_WRITE_FLAG_CHECK_INTEGRITY) {
1568 ret = write_integrity_table(out, WIM_HEADER_DISK_SIZE,
1570 write_flags & WIMLIB_WRITE_FLAG_SHOW_PROGRESS);
1573 end_offset = ftello(out);
1574 if (end_offset == -1)
1575 return WIMLIB_ERR_WRITE;
1576 integrity_size = end_offset - integrity_offset;
1577 hdr.integrity.offset = integrity_offset;
1578 hdr.integrity.size = integrity_size;
1579 hdr.integrity.original_size = integrity_size;
1581 hdr.integrity.offset = 0;
1582 hdr.integrity.size = 0;
1583 hdr.integrity.original_size = 0;
1585 hdr.integrity.flags = 0;
1587 DEBUG("Updating WIM header.");
1590 * In the WIM header, there is room for the resource entry for a
1591 * metadata resource labeled as the "boot metadata". This entry should
1592 * be zeroed out if there is no bootable image (boot_idx 0). Otherwise,
1593 * it should be a copy of the resource entry for the image that is
1594 * marked as bootable. This is not well documented...
1596 if (hdr.boot_idx == 0 || !w->image_metadata
1597 || (image != WIM_ALL_IMAGES && image != hdr.boot_idx)) {
1598 memset(&hdr.boot_metadata_res_entry, 0,
1599 sizeof(struct resource_entry));
1601 memcpy(&hdr.boot_metadata_res_entry,
1603 hdr.boot_idx - 1].metadata_lte->output_resource_entry,
1604 sizeof(struct resource_entry));
1607 /* Set image count and boot index correctly for single image writes */
1608 if (image != WIM_ALL_IMAGES) {
1609 hdr.image_count = 1;
1610 if (hdr.boot_idx == image)
1617 if (fseeko(out, 0, SEEK_SET) != 0)
1618 return WIMLIB_ERR_WRITE;
1620 ret = write_header(&hdr, out);
1624 DEBUG("Closing output file.");
1625 wimlib_assert(w->out_fp != NULL);
1626 if (fclose(w->out_fp) != 0) {
1627 ERROR_WITH_ERRNO("Failed to close the WIM file");
1628 ret = WIMLIB_ERR_WRITE;
1634 /* Open file stream and write dummy header for WIM. */
1635 int begin_write(WIMStruct *w, const char *path, int write_flags)
1638 DEBUG("Opening `%s' for new WIM", path);
1640 /* checking the integrity requires going back over the file to read it.
1642 * (It also would be possible to keep a running sha1sum as the file is
1643 * written-- this would be faster, but a bit more complicated) */
1644 if (write_flags & WIMLIB_WRITE_FLAG_CHECK_INTEGRITY)
1652 w->out_fp = fopen(path, mode);
1654 ERROR_WITH_ERRNO("Failed to open the file `%s' for writing",
1656 return WIMLIB_ERR_OPEN;
1659 /* Write dummy header. It will be overwritten later. */
1660 return write_header(&w->hdr, w->out_fp);
1663 /* Writes a stand-alone WIM to a file. */
1664 WIMLIBAPI int wimlib_write(WIMStruct *w, const char *path,
1665 int image, int write_flags)
1670 return WIMLIB_ERR_INVALID_PARAM;
1672 write_flags &= ~WIMLIB_WRITE_FLAG_NO_LOOKUP_TABLE;
1674 if (image != WIM_ALL_IMAGES &&
1675 (image < 1 || image > w->hdr.image_count))
1676 return WIMLIB_ERR_INVALID_IMAGE;
1679 if (w->hdr.total_parts != 1) {
1680 ERROR("Cannot call wimlib_write() on part of a split WIM");
1681 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
1684 if (image == WIM_ALL_IMAGES)
1685 DEBUG("Writing all images to `%s'.", path);
1687 DEBUG("Writing image %d to `%s'.", image, path);
1689 ret = begin_write(w, path, write_flags);
1693 for_lookup_table_entry(w->lookup_table, lte_zero_out_refcnt, NULL);
1695 ret = write_wim_streams(w, image, write_flags);
1698 /*ERROR("Failed to write WIM file resources to `%s'", path);*/
1702 ret = for_image(w, image, write_metadata_resource);
1705 /*ERROR("Failed to write WIM image metadata to `%s'", path);*/
1709 ret = finish_write(w, image, write_flags);
1713 if (write_flags & WIMLIB_WRITE_FLAG_VERBOSE)
1714 printf("Successfully wrote `%s'\n", path);