]> wimlib.net Git - wimlib/blob - src/write.c
join.c, write.c: Optimize WIMLIB_ALL_IMAGES writes (and joins)
[wimlib] / src / write.c
1 /*
2  * write.c
3  *
4  * Support for writing WIM files; write a WIM file, overwrite a WIM file, write
5  * compressed file resources, etc.
6  */
7
8 /*
9  * Copyright (C) 2012, 2013 Eric Biggers
10  *
11  * This file is part of wimlib, a library for working with WIM files.
12  *
13  * wimlib is free software; you can redistribute it and/or modify it under the
14  * terms of the GNU General Public License as published by the Free
15  * Software Foundation; either version 3 of the License, or (at your option)
16  * any later version.
17  *
18  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
19  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
20  * A PARTICULAR PURPOSE. See the GNU General Public License for more
21  * details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with wimlib; if not, see http://www.gnu.org/licenses/.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 #  include "config.h"
29 #endif
30
31 #if defined(HAVE_SYS_FILE_H) && defined(HAVE_FLOCK)
32 /* On BSD, this should be included before "wimlib/list.h" so that "wimlib/list.h" can
33  * overwrite the LIST_HEAD macro. */
34 #  include <sys/file.h>
35 #endif
36
37 #include "wimlib/endianness.h"
38 #include "wimlib/error.h"
39 #include "wimlib/file_io.h"
40 #include "wimlib/header.h"
41 #include "wimlib/integrity.h"
42 #include "wimlib/lookup_table.h"
43 #include "wimlib/metadata.h"
44 #include "wimlib/resource.h"
45 #include "wimlib/write.h"
46 #include "wimlib/xml.h"
47
48 #ifdef __WIN32__
49 #  include "wimlib/win32.h" /* win32_get_number_of_processors() */
50 #endif
51
52 #ifdef ENABLE_MULTITHREADED_COMPRESSION
53 #  include <pthread.h>
54 #endif
55
56 #include <errno.h>
57 #include <fcntl.h>
58 #include <limits.h>
59 #include <stdlib.h>
60 #include <unistd.h>
61
62 #ifdef HAVE_ALLOCA_H
63 #  include <alloca.h>
64 #endif
65
66
67 #ifndef __WIN32__
68 #  include <sys/uio.h> /* for `struct iovec' */
69 #endif
70
71 /* Chunk table that's located at the beginning of each compressed resource in
72  * the WIM.  (This is not the on-disk format; the on-disk format just has an
73  * array of offsets.) */
74 struct chunk_table {
75         u64 original_resource_size;
76         u64 num_chunks;
77         u64 table_disk_size;
78         unsigned bytes_per_chunk_entry;
79         void *cur_offset_p;
80         union {
81                 u32 cur_offset_u32;
82                 u64 cur_offset_u64;
83         };
84         /* Beginning of chunk offsets, in either 32-bit or 64-bit little endian
85          * integers, including the first offset of 0, which will not be written.
86          * */
87         u8 offsets[] _aligned_attribute(8);
88 };
89
90 /* Allocate and initializes a chunk table, then reserve space for it in the
91  * output file unless writing a pipable resource.  */
92 static int
93 begin_wim_resource_chunk_tab(const struct wim_lookup_table_entry *lte,
94                              struct filedes *out_fd,
95                              struct chunk_table **chunk_tab_ret,
96                              int resource_flags)
97 {
98         u64 size;
99         u64 num_chunks;
100         unsigned bytes_per_chunk_entry;
101         size_t alloc_size;
102         struct chunk_table *chunk_tab;
103         int ret;
104
105         size = wim_resource_size(lte);
106         num_chunks = wim_resource_chunks(lte);
107         bytes_per_chunk_entry = (size > (1ULL << 32)) ? 8 : 4;
108         alloc_size = sizeof(struct chunk_table) + num_chunks * sizeof(u64);
109         chunk_tab = CALLOC(1, alloc_size);
110
111         if (!chunk_tab) {
112                 ERROR("Failed to allocate chunk table for %"PRIu64" byte "
113                       "resource", size);
114                 return WIMLIB_ERR_NOMEM;
115         }
116         chunk_tab->num_chunks = num_chunks;
117         chunk_tab->original_resource_size = size;
118         chunk_tab->bytes_per_chunk_entry = bytes_per_chunk_entry;
119         chunk_tab->table_disk_size = chunk_tab->bytes_per_chunk_entry *
120                                      (num_chunks - 1);
121         chunk_tab->cur_offset_p = chunk_tab->offsets;
122
123         /* We don't know the correct offsets yet; so just write zeroes to
124          * reserve space for the table, so we can go back to it later after
125          * we've written the compressed chunks following it.
126          *
127          * Special case: if writing a pipable WIM, compressed resources are in a
128          * modified format (see comment above write_pipable_wim()) and do not
129          * have a chunk table at the beginning, so don't reserve any space for
130          * one.  */
131         if (!(resource_flags & WIMLIB_WRITE_RESOURCE_FLAG_PIPABLE)) {
132                 ret = full_write(out_fd, chunk_tab->offsets,
133                                  chunk_tab->table_disk_size);
134                 if (ret) {
135                         ERROR_WITH_ERRNO("Failed to write chunk table in compressed "
136                                          "file resource");
137                         FREE(chunk_tab);
138                         return ret;
139                 }
140         }
141         *chunk_tab_ret = chunk_tab;
142         return 0;
143 }
144
145 /* Add the offset for the next chunk to the chunk table being constructed for a
146  * compressed stream. */
147 static void
148 chunk_tab_record_chunk(struct chunk_table *chunk_tab, unsigned out_chunk_size)
149 {
150         if (chunk_tab->bytes_per_chunk_entry == 4) {
151                 *(le32*)chunk_tab->cur_offset_p = cpu_to_le32(chunk_tab->cur_offset_u32);
152                 chunk_tab->cur_offset_p = (le32*)chunk_tab->cur_offset_p + 1;
153                 chunk_tab->cur_offset_u32 += out_chunk_size;
154         } else {
155                 *(le64*)chunk_tab->cur_offset_p = cpu_to_le64(chunk_tab->cur_offset_u64);
156                 chunk_tab->cur_offset_p = (le64*)chunk_tab->cur_offset_p + 1;
157                 chunk_tab->cur_offset_u64 += out_chunk_size;
158         }
159 }
160
161 /*
162  * compress_func_t- Pointer to a function to compresses a chunk
163  *                  of a WIM resource.  This may be either
164  *                  wimlib_xpress_compress() (xpress-compress.c) or
165  *                  wimlib_lzx_compress() (lzx-compress.c).
166  *
167  * @chunk:        Uncompressed data of the chunk.
168  * @chunk_size:   Size of the uncompressed chunk, in bytes.
169  * @out:          Pointer to output buffer of size at least (@chunk_size - 1) bytes.
170  *
171  * Returns the size of the compressed data written to @out in bytes, or 0 if the
172  * data could not be compressed to (@chunk_size - 1) bytes or fewer.
173  *
174  * As a special requirement, the compression code is optimized for the WIM
175  * format and therefore requires (@chunk_size <= 32768).
176  *
177  * As another special requirement, the compression code will read up to 8 bytes
178  * off the end of the @chunk array for performance reasons.  The values of these
179  * bytes will not affect the output of the compression, but the calling code
180  * must make sure that the buffer holding the uncompressed chunk is actually at
181  * least (@chunk_size + 8) bytes, or at least that these extra bytes are in
182  * mapped memory that will not cause a memory access violation if accessed.
183  */
184 typedef unsigned (*compress_func_t)(const void *chunk, unsigned chunk_size,
185                                     void *out);
186
187 static compress_func_t
188 get_compress_func(int out_ctype)
189 {
190         if (out_ctype == WIMLIB_COMPRESSION_TYPE_LZX)
191                 return wimlib_lzx_compress;
192         else
193                 return wimlib_xpress_compress;
194 }
195
196 /* Finishes a WIM chunk table and writes it to the output file at the correct
197  * offset.  */
198 static int
199 finish_wim_resource_chunk_tab(struct chunk_table *chunk_tab,
200                               struct filedes *out_fd,
201                               off_t res_start_offset,
202                               int write_resource_flags)
203 {
204         int ret;
205
206         if (write_resource_flags & WIMLIB_WRITE_RESOURCE_FLAG_PIPABLE) {
207                 ret = full_write(out_fd,
208                                  chunk_tab->offsets +
209                                          chunk_tab->bytes_per_chunk_entry,
210                                  chunk_tab->table_disk_size);
211         } else {
212                 ret  = full_pwrite(out_fd,
213                                    chunk_tab->offsets +
214                                            chunk_tab->bytes_per_chunk_entry,
215                                    chunk_tab->table_disk_size,
216                                    res_start_offset);
217         }
218         if (ret) {
219                 ERROR_WITH_ERRNO("Failed to write chunk table in compressed "
220                                  "file resource");
221         }
222         return ret;
223 }
224
225 /* Write the header for a stream in a pipable WIM.
226  */
227 static int
228 write_pwm_stream_header(const struct wim_lookup_table_entry *lte,
229                         struct filedes *out_fd,
230                         int additional_reshdr_flags)
231 {
232         struct pwm_stream_hdr stream_hdr;
233         u32 reshdr_flags;
234         int ret;
235
236         stream_hdr.magic = PWM_STREAM_MAGIC;
237         stream_hdr.uncompressed_size = cpu_to_le64(lte->resource_entry.original_size);
238         if (additional_reshdr_flags & PWM_RESHDR_FLAG_UNHASHED) {
239                 zero_out_hash(stream_hdr.hash);
240         } else {
241                 wimlib_assert(!lte->unhashed);
242                 copy_hash(stream_hdr.hash, lte->hash);
243         }
244
245         reshdr_flags = lte->resource_entry.flags & ~WIM_RESHDR_FLAG_COMPRESSED;
246         reshdr_flags |= additional_reshdr_flags;
247         stream_hdr.flags = cpu_to_le32(reshdr_flags);
248         ret = full_write(out_fd, &stream_hdr, sizeof(stream_hdr));
249         if (ret)
250                 ERROR_WITH_ERRNO("Error writing stream header");
251         return ret;
252 }
253
254 static int
255 seek_and_truncate(struct filedes *out_fd, off_t offset)
256 {
257         if (filedes_seek(out_fd, offset) == -1 ||
258             ftruncate(out_fd->fd, offset))
259         {
260                 ERROR_WITH_ERRNO("Failed to truncate output WIM file");
261                 return WIMLIB_ERR_WRITE;
262         }
263         return 0;
264 }
265
266 static int
267 finalize_and_check_sha1(SHA_CTX *sha_ctx, struct wim_lookup_table_entry *lte)
268 {
269         u8 md[SHA1_HASH_SIZE];
270
271         sha1_final(md, sha_ctx);
272         if (lte->unhashed) {
273                 copy_hash(lte->hash, md);
274         } else if (!hashes_equal(md, lte->hash)) {
275                 ERROR("WIM resource has incorrect hash!");
276                 if (lte_filename_valid(lte)) {
277                         ERROR("We were reading it from \"%"TS"\"; maybe "
278                               "it changed while we were reading it.",
279                               lte->file_on_disk);
280                 }
281                 return WIMLIB_ERR_INVALID_RESOURCE_HASH;
282         }
283         return 0;
284 }
285
286 struct write_resource_ctx {
287         compress_func_t compress;
288         struct chunk_table *chunk_tab;
289         struct filedes *out_fd;
290         SHA_CTX sha_ctx;
291         bool doing_sha;
292         int resource_flags;
293 };
294
295 static int
296 write_resource_cb(const void *chunk, size_t chunk_size, void *_ctx)
297 {
298         struct write_resource_ctx *ctx = _ctx;
299         const void *out_chunk;
300         unsigned out_chunk_size;
301         int ret;
302
303         if (ctx->doing_sha)
304                 sha1_update(&ctx->sha_ctx, chunk, chunk_size);
305
306         out_chunk = chunk;
307         out_chunk_size = chunk_size;
308         if (ctx->compress) {
309                 void *compressed_chunk;
310                 unsigned compressed_size;
311
312                 /* Compress the chunk.  */
313                 compressed_chunk = alloca(chunk_size);
314                 compressed_size = (*ctx->compress)(chunk, chunk_size,
315                                                    compressed_chunk);
316
317                 /* Use compressed data if compression to less than input size
318                  * was successful.  */
319                 if (compressed_size) {
320                         out_chunk = compressed_chunk;
321                         out_chunk_size = compressed_size;
322                 }
323         }
324
325         if (ctx->chunk_tab) {
326                 /* Update chunk table accounting.  */
327                 chunk_tab_record_chunk(ctx->chunk_tab, out_chunk_size);
328
329                 /* If writing compressed chunks to a pipable WIM, before the
330                  * chunk data write a chunk header that provides the compressed
331                  * chunk size.  */
332                 if (ctx->resource_flags & WIMLIB_WRITE_RESOURCE_FLAG_PIPABLE) {
333                         struct pwm_chunk_hdr chunk_hdr = {
334                                 .compressed_size = cpu_to_le32(out_chunk_size),
335                         };
336                         ret = full_write(ctx->out_fd, &chunk_hdr,
337                                          sizeof(chunk_hdr));
338                         if (ret)
339                                 goto error;
340                 }
341         }
342
343         /* Write the chunk data.  */
344         ret = full_write(ctx->out_fd, out_chunk, out_chunk_size);
345         if (ret)
346                 goto error;
347         return 0;
348
349 error:
350         ERROR_WITH_ERRNO("Failed to write WIM resource chunk");
351         return ret;
352 }
353
354 /*
355  * write_wim_resource()-
356  *
357  * Write a resource to an output WIM.
358  *
359  * @lte:
360  *      Lookup table entry for the resource, which could be in another WIM, in
361  *      an external file, or in another location.
362  *
363  * @out_fd:
364  *      File descriptor opened to the output WIM.
365  *
366  * @out_ctype:
367  *      One of the WIMLIB_COMPRESSION_TYPE_* constants to indicate which
368  *      compression algorithm to use.
369  *
370  * @out_res_entry:
371  *      On success, this is filled in with the offset, flags, compressed size,
372  *      and uncompressed size of the resource in the output WIM.
373  *
374  * @write_resource_flags:
375  *      * WIMLIB_WRITE_RESOURCE_FLAG_RECOMPRESS to force data to be recompressed even
376  *        if it could otherwise be copied directly from the input;
377  *      * WIMLIB_WRITE_RESOURCE_FLAG_PIPABLE if writing a resource for a pipable WIM
378  *        (and the output file descriptor may be a pipe).
379  *
380  * Additional notes:  The SHA1 message digest of the uncompressed data is
381  * calculated (except when doing a raw copy --- see below).  If the @unhashed
382  * flag is set on the lookup table entry, this message digest is simply copied
383  * to it; otherwise, the message digest is compared with the existing one, and
384  * the function will fail if they do not match.
385  */
386 int
387 write_wim_resource(struct wim_lookup_table_entry *lte,
388                    struct filedes *out_fd, int out_ctype,
389                    struct resource_entry *out_res_entry,
390                    int resource_flags)
391 {
392         struct write_resource_ctx write_ctx;
393         off_t res_start_offset;
394         u64 read_size;
395         int ret;
396
397         /* Mask out any irrelevant flags, since this function also uses this
398          * variable to store WIMLIB_READ_RESOURCE flags.  */
399         resource_flags &= WIMLIB_WRITE_RESOURCE_MASK;
400
401         /* Get current position in output WIM.  */
402         res_start_offset = out_fd->offset;
403
404         /* If we are not forcing the data to be recompressed, and the input
405          * resource is located in a WIM with the same compression type as that
406          * desired other than no compression, we can simply copy the compressed
407          * data without recompressing it.  This also means we must skip
408          * calculating the SHA1, as we never will see the uncompressed data.  */
409         if (lte->resource_location == RESOURCE_IN_WIM &&
410             out_ctype == wim_resource_compression_type(lte) &&
411             out_ctype != WIMLIB_COMPRESSION_TYPE_NONE &&
412             !(resource_flags & WIMLIB_WRITE_RESOURCE_FLAG_RECOMPRESS))
413         {
414                 /* Normally we can request a RAW_FULL read, but if we're reading
415                  * from a pipable resource and writing a non-pipable resource or
416                  * vice versa, then a RAW_CHUNKS read needs to be requested so
417                  * that the written resource can be appropriately formatted.
418                  * However, in neither case is any actual decompression needed.
419                  */
420                 if (lte->is_pipable == !!(resource_flags &
421                                           WIMLIB_WRITE_RESOURCE_FLAG_PIPABLE))
422                         resource_flags |= WIMLIB_READ_RESOURCE_FLAG_RAW_FULL;
423                 else
424                         resource_flags |= WIMLIB_READ_RESOURCE_FLAG_RAW_CHUNKS;
425                 write_ctx.doing_sha = false;
426                 read_size = lte->resource_entry.size;
427         } else {
428                 write_ctx.doing_sha = true;
429                 sha1_init(&write_ctx.sha_ctx);
430                 read_size = lte->resource_entry.original_size;
431         }
432
433         /* If the output resource is to be compressed, initialize the chunk
434          * table and set the function to use for chunk compression.  Exceptions:
435          * no compression function is needed if doing a raw copy; also, no chunk
436          * table is needed if doing a *full* (not per-chunk) raw copy.  */
437         write_ctx.compress = NULL;
438         write_ctx.chunk_tab = NULL;
439         if (out_ctype != WIMLIB_COMPRESSION_TYPE_NONE) {
440                 if (!(resource_flags & WIMLIB_READ_RESOURCE_FLAG_RAW))
441                         write_ctx.compress = get_compress_func(out_ctype);
442                 if (!(resource_flags & WIMLIB_READ_RESOURCE_FLAG_RAW_FULL)) {
443                         ret = begin_wim_resource_chunk_tab(lte, out_fd,
444                                                            &write_ctx.chunk_tab,
445                                                            resource_flags);
446                         if (ret)
447                                 goto out;
448                 }
449         }
450
451         /* If writing a pipable resource, write the stream header and update
452          * @res_start_offset to be the end of the stream header.  */
453         if (resource_flags & WIMLIB_WRITE_RESOURCE_FLAG_PIPABLE) {
454                 int reshdr_flags = 0;
455                 if (out_ctype != WIMLIB_COMPRESSION_TYPE_NONE)
456                         reshdr_flags |= WIM_RESHDR_FLAG_COMPRESSED;
457                 ret = write_pwm_stream_header(lte, out_fd, reshdr_flags);
458                 if (ret)
459                         goto out_free_chunk_tab;
460                 res_start_offset = out_fd->offset;
461         }
462
463         /* Write the entire resource by reading the entire resource and feeding
464          * the data through the write_resource_cb function. */
465         write_ctx.out_fd = out_fd;
466         write_ctx.resource_flags = resource_flags;
467 try_write_again:
468         ret = read_resource_prefix(lte, read_size,
469                                    write_resource_cb, &write_ctx, resource_flags);
470         if (ret)
471                 goto out_free_chunk_tab;
472
473         /* Verify SHA1 message digest of the resource, or set the hash for the
474          * first time. */
475         if (write_ctx.doing_sha) {
476                 ret = finalize_and_check_sha1(&write_ctx.sha_ctx, lte);
477                 if (ret)
478                         goto out_free_chunk_tab;
479         }
480
481         /* Write chunk table if needed.  */
482         if (write_ctx.chunk_tab) {
483                 ret = finish_wim_resource_chunk_tab(write_ctx.chunk_tab,
484                                                     out_fd,
485                                                     res_start_offset,
486                                                     resource_flags);
487                 if (ret)
488                         goto out_free_chunk_tab;
489         }
490
491         /* Fill in out_res_entry with information about the newly written
492          * resource.  */
493         out_res_entry->size          = out_fd->offset - res_start_offset;
494         out_res_entry->flags         = lte->resource_entry.flags;
495         if (out_ctype == WIMLIB_COMPRESSION_TYPE_NONE)
496                 out_res_entry->flags &= ~WIM_RESHDR_FLAG_COMPRESSED;
497         else
498                 out_res_entry->flags |= WIM_RESHDR_FLAG_COMPRESSED;
499         out_res_entry->offset        = res_start_offset;
500         out_res_entry->original_size = wim_resource_size(lte);
501
502         /* Check for resources compressed to greater than their original size
503          * and write them uncompressed instead.  (But never do this if writing
504          * to a pipe, and don't bother if we did a raw copy.)  */
505         if (out_res_entry->size > out_res_entry->original_size &&
506             !(resource_flags & (WIMLIB_WRITE_RESOURCE_FLAG_PIPABLE |
507                                 WIMLIB_READ_RESOURCE_FLAG_RAW)))
508         {
509                 DEBUG("Compressed %"PRIu64" => %"PRIu64" bytes; "
510                       "writing uncompressed instead",
511                       out_res_entry->original_size, out_res_entry->size);
512                 ret = seek_and_truncate(out_fd, res_start_offset);
513                 if (ret)
514                         goto out_free_chunk_tab;
515                 out_ctype = WIMLIB_COMPRESSION_TYPE_NONE;
516                 FREE(write_ctx.chunk_tab);
517                 write_ctx.compress = NULL;
518                 write_ctx.chunk_tab = NULL;
519                 write_ctx.doing_sha = false;
520                 goto try_write_again;
521         }
522         if (resource_flags & (WIMLIB_READ_RESOURCE_FLAG_RAW)) {
523                 DEBUG("Copied raw compressed data "
524                       "(%"PRIu64" => %"PRIu64" bytes @ +%"PRIu64", flags=0x%02x)",
525                       out_res_entry->original_size, out_res_entry->size,
526                       out_res_entry->offset, out_res_entry->flags);
527         } else if (out_ctype != WIMLIB_COMPRESSION_TYPE_NONE) {
528                 DEBUG("Wrote compressed resource "
529                       "(%"PRIu64" => %"PRIu64" bytes @ +%"PRIu64", flags=0x%02x)",
530                       out_res_entry->original_size, out_res_entry->size,
531                       out_res_entry->offset, out_res_entry->flags);
532         } else {
533                 DEBUG("Wrote uncompressed resource "
534                       "(%"PRIu64" bytes @ +%"PRIu64", flags=0x%02x)",
535                       out_res_entry->original_size,
536                       out_res_entry->offset, out_res_entry->flags);
537         }
538         ret = 0;
539 out_free_chunk_tab:
540         FREE(write_ctx.chunk_tab);
541 out:
542         return ret;
543 }
544
545 /* Like write_wim_resource(), but the resource is specified by a buffer of
546  * uncompressed data rather a lookup table entry; also writes the SHA1 hash of
547  * the buffer to @hash_ret.  */
548 int
549 write_wim_resource_from_buffer(const void *buf, size_t buf_size,
550                                int reshdr_flags, struct filedes *out_fd,
551                                int out_ctype,
552                                struct resource_entry *out_res_entry,
553                                u8 *hash_ret, int write_resource_flags)
554 {
555         /* Set up a temporary lookup table entry to provide to
556          * write_wim_resource(). */
557         struct wim_lookup_table_entry lte;
558         int ret;
559
560         lte.resource_location            = RESOURCE_IN_ATTACHED_BUFFER;
561         lte.attached_buffer              = (void*)buf;
562         lte.resource_entry.original_size = buf_size;
563         lte.resource_entry.flags         = reshdr_flags;
564
565         if (write_resource_flags & WIMLIB_WRITE_RESOURCE_FLAG_PIPABLE) {
566                 sha1_buffer(buf, buf_size, lte.hash);
567                 lte.unhashed = 0;
568         } else {
569                 lte.unhashed = 1;
570         }
571
572         ret = write_wim_resource(&lte, out_fd, out_ctype, out_res_entry,
573                                  write_resource_flags);
574         if (ret)
575                 return ret;
576         if (hash_ret)
577                 copy_hash(hash_ret, lte.hash);
578         return 0;
579 }
580
581
582 #ifdef ENABLE_MULTITHREADED_COMPRESSION
583
584 /* Blocking shared queue (solves the producer-consumer problem) */
585 struct shared_queue {
586         unsigned size;
587         unsigned front;
588         unsigned back;
589         unsigned filled_slots;
590         void **array;
591         pthread_mutex_t lock;
592         pthread_cond_t msg_avail_cond;
593         pthread_cond_t space_avail_cond;
594 };
595
596 static int
597 shared_queue_init(struct shared_queue *q, unsigned size)
598 {
599         wimlib_assert(size != 0);
600         q->array = CALLOC(sizeof(q->array[0]), size);
601         if (!q->array)
602                 goto err;
603         q->filled_slots = 0;
604         q->front = 0;
605         q->back = size - 1;
606         q->size = size;
607         if (pthread_mutex_init(&q->lock, NULL)) {
608                 ERROR_WITH_ERRNO("Failed to initialize mutex");
609                 goto err;
610         }
611         if (pthread_cond_init(&q->msg_avail_cond, NULL)) {
612                 ERROR_WITH_ERRNO("Failed to initialize condition variable");
613                 goto err_destroy_lock;
614         }
615         if (pthread_cond_init(&q->space_avail_cond, NULL)) {
616                 ERROR_WITH_ERRNO("Failed to initialize condition variable");
617                 goto err_destroy_msg_avail_cond;
618         }
619         return 0;
620 err_destroy_msg_avail_cond:
621         pthread_cond_destroy(&q->msg_avail_cond);
622 err_destroy_lock:
623         pthread_mutex_destroy(&q->lock);
624 err:
625         return WIMLIB_ERR_NOMEM;
626 }
627
628 static void
629 shared_queue_destroy(struct shared_queue *q)
630 {
631         FREE(q->array);
632         pthread_mutex_destroy(&q->lock);
633         pthread_cond_destroy(&q->msg_avail_cond);
634         pthread_cond_destroy(&q->space_avail_cond);
635 }
636
637 static void
638 shared_queue_put(struct shared_queue *q, void *obj)
639 {
640         pthread_mutex_lock(&q->lock);
641         while (q->filled_slots == q->size)
642                 pthread_cond_wait(&q->space_avail_cond, &q->lock);
643
644         q->back = (q->back + 1) % q->size;
645         q->array[q->back] = obj;
646         q->filled_slots++;
647
648         pthread_cond_broadcast(&q->msg_avail_cond);
649         pthread_mutex_unlock(&q->lock);
650 }
651
652 static void *
653 shared_queue_get(struct shared_queue *q)
654 {
655         void *obj;
656
657         pthread_mutex_lock(&q->lock);
658         while (q->filled_slots == 0)
659                 pthread_cond_wait(&q->msg_avail_cond, &q->lock);
660
661         obj = q->array[q->front];
662         q->array[q->front] = NULL;
663         q->front = (q->front + 1) % q->size;
664         q->filled_slots--;
665
666         pthread_cond_broadcast(&q->space_avail_cond);
667         pthread_mutex_unlock(&q->lock);
668         return obj;
669 }
670
671 struct compressor_thread_params {
672         struct shared_queue *res_to_compress_queue;
673         struct shared_queue *compressed_res_queue;
674         compress_func_t compress;
675 };
676
677 #define MAX_CHUNKS_PER_MSG 2
678
679 struct message {
680         struct wim_lookup_table_entry *lte;
681         u8 *uncompressed_chunks[MAX_CHUNKS_PER_MSG];
682         u8 *compressed_chunks[MAX_CHUNKS_PER_MSG];
683         unsigned uncompressed_chunk_sizes[MAX_CHUNKS_PER_MSG];
684         struct iovec out_chunks[MAX_CHUNKS_PER_MSG];
685         unsigned num_chunks;
686         struct list_head list;
687         bool complete;
688         u64 begin_chunk;
689 };
690
691 static void
692 compress_chunks(struct message *msg, compress_func_t compress)
693 {
694         for (unsigned i = 0; i < msg->num_chunks; i++) {
695                 unsigned len = compress(msg->uncompressed_chunks[i],
696                                         msg->uncompressed_chunk_sizes[i],
697                                         msg->compressed_chunks[i]);
698                 void *out_chunk;
699                 unsigned out_len;
700                 if (len) {
701                         /* To be written compressed */
702                         out_chunk = msg->compressed_chunks[i];
703                         out_len = len;
704                 } else {
705                         /* To be written uncompressed */
706                         out_chunk = msg->uncompressed_chunks[i];
707                         out_len = msg->uncompressed_chunk_sizes[i];
708                 }
709                 msg->out_chunks[i].iov_base = out_chunk;
710                 msg->out_chunks[i].iov_len = out_len;
711         }
712 }
713
714 /* Compressor thread routine.  This is a lot simpler than the main thread
715  * routine: just repeatedly get a group of chunks from the
716  * res_to_compress_queue, compress them, and put them in the
717  * compressed_res_queue.  A NULL pointer indicates that the thread should stop.
718  * */
719 static void *
720 compressor_thread_proc(void *arg)
721 {
722         struct compressor_thread_params *params = arg;
723         struct shared_queue *res_to_compress_queue = params->res_to_compress_queue;
724         struct shared_queue *compressed_res_queue = params->compressed_res_queue;
725         compress_func_t compress = params->compress;
726         struct message *msg;
727
728         DEBUG("Compressor thread ready");
729         while ((msg = shared_queue_get(res_to_compress_queue)) != NULL) {
730                 compress_chunks(msg, compress);
731                 shared_queue_put(compressed_res_queue, msg);
732         }
733         DEBUG("Compressor thread terminating");
734         return NULL;
735 }
736 #endif /* ENABLE_MULTITHREADED_COMPRESSION */
737
738 struct write_streams_progress_data {
739         wimlib_progress_func_t progress_func;
740         union wimlib_progress_info progress;
741         uint64_t next_progress;
742         WIMStruct *prev_wim_part;
743 };
744
745 static void
746 do_write_streams_progress(struct write_streams_progress_data *progress_data,
747                           struct wim_lookup_table_entry *lte,
748                           bool stream_discarded)
749 {
750         union wimlib_progress_info *progress = &progress_data->progress;
751         bool new_wim_part;
752
753         if (stream_discarded) {
754                 progress->write_streams.total_bytes -= wim_resource_size(lte);
755                 if (progress_data->next_progress != ~(uint64_t)0 &&
756                     progress_data->next_progress > progress->write_streams.total_bytes)
757                 {
758                         progress_data->next_progress = progress->write_streams.total_bytes;
759                 }
760         } else {
761                 progress->write_streams.completed_bytes += wim_resource_size(lte);
762         }
763         new_wim_part = false;
764         if (lte->resource_location == RESOURCE_IN_WIM &&
765             lte->wim != progress_data->prev_wim_part)
766         {
767                 if (progress_data->prev_wim_part) {
768                         new_wim_part = true;
769                         progress->write_streams.completed_parts++;
770                 }
771                 progress_data->prev_wim_part = lte->wim;
772         }
773         progress->write_streams.completed_streams++;
774         if (progress_data->progress_func
775             && (progress->write_streams.completed_bytes >= progress_data->next_progress
776                 || new_wim_part))
777         {
778                 progress_data->progress_func(WIMLIB_PROGRESS_MSG_WRITE_STREAMS,
779                                              progress);
780                 if (progress_data->next_progress == progress->write_streams.total_bytes) {
781                         progress_data->next_progress = ~(uint64_t)0;
782                 } else {
783                         progress_data->next_progress =
784                                 min(progress->write_streams.total_bytes,
785                                     progress->write_streams.completed_bytes +
786                                         progress->write_streams.total_bytes / 100);
787                 }
788         }
789 }
790
791 struct serial_write_stream_ctx {
792         struct filedes *out_fd;
793         int out_ctype;
794         int write_resource_flags;
795 };
796
797 static int
798 serial_write_stream(struct wim_lookup_table_entry *lte, void *_ctx)
799 {
800         struct serial_write_stream_ctx *ctx = _ctx;
801         return write_wim_resource(lte, ctx->out_fd,
802                                   ctx->out_ctype, &lte->output_resource_entry,
803                                   ctx->write_resource_flags);
804 }
805
806
807 /* Write a list of streams, taking into account that some streams may be
808  * duplicates that are checksummed and discarded on the fly, and also delegating
809  * the actual writing of a stream to a function @write_stream_cb, which is
810  * passed the context @write_stream_ctx. */
811 static int
812 do_write_stream_list(struct list_head *stream_list,
813                      struct wim_lookup_table *lookup_table,
814                      int (*write_stream_cb)(struct wim_lookup_table_entry *, void *),
815                      void *write_stream_ctx,
816                      struct write_streams_progress_data *progress_data)
817 {
818         int ret = 0;
819         struct wim_lookup_table_entry *lte;
820         bool stream_discarded;
821
822         /* For each stream in @stream_list ... */
823         while (!list_empty(stream_list)) {
824                 stream_discarded = false;
825                 lte = container_of(stream_list->next,
826                                    struct wim_lookup_table_entry,
827                                    write_streams_list);
828                 list_del(&lte->write_streams_list);
829                 if (lte->unhashed && !lte->unique_size) {
830                         /* Unhashed stream that shares a size with some other
831                          * stream in the WIM we are writing.  The stream must be
832                          * checksummed to know if we need to write it or not. */
833                         struct wim_lookup_table_entry *tmp;
834                         u32 orig_refcnt = lte->out_refcnt;
835
836                         ret = hash_unhashed_stream(lte, lookup_table, &tmp);
837                         if (ret)
838                                 break;
839                         if (tmp != lte) {
840                                 lte = tmp;
841                                 /* We found a duplicate stream. */
842                                 if (orig_refcnt != tmp->out_refcnt) {
843                                         /* We have already written, or are going
844                                          * to write, the duplicate stream.  So
845                                          * just skip to the next stream. */
846                                         DEBUG("Discarding duplicate stream of length %"PRIu64,
847                                               wim_resource_size(lte));
848                                         lte->no_progress = 0;
849                                         stream_discarded = true;
850                                         goto skip_to_progress;
851                                 }
852                         }
853                 }
854
855                 /* Here, @lte is either a hashed stream or an unhashed stream
856                  * with a unique size.  In either case we know that the stream
857                  * has to be written.  In either case the SHA1 message digest
858                  * will be calculated over the stream while writing it; however,
859                  * in the former case this is done merely to check the data,
860                  * while in the latter case this is done because we do not have
861                  * the SHA1 message digest yet.  */
862                 wimlib_assert(lte->out_refcnt != 0);
863                 lte->deferred = 0;
864                 lte->no_progress = 0;
865                 ret = (*write_stream_cb)(lte, write_stream_ctx);
866                 if (ret)
867                         break;
868                 /* In parallel mode, some streams are deferred for later,
869                  * serialized processing; ignore them here. */
870                 if (lte->deferred)
871                         continue;
872                 if (lte->unhashed) {
873                         list_del(&lte->unhashed_list);
874                         lookup_table_insert(lookup_table, lte);
875                         lte->unhashed = 0;
876                 }
877         skip_to_progress:
878                 if (!lte->no_progress) {
879                         do_write_streams_progress(progress_data,
880                                                   lte, stream_discarded);
881                 }
882         }
883         return ret;
884 }
885
886 static int
887 do_write_stream_list_serial(struct list_head *stream_list,
888                             struct wim_lookup_table *lookup_table,
889                             struct filedes *out_fd,
890                             int out_ctype,
891                             int write_resource_flags,
892                             struct write_streams_progress_data *progress_data)
893 {
894         struct serial_write_stream_ctx ctx = {
895                 .out_fd = out_fd,
896                 .out_ctype = out_ctype,
897                 .write_resource_flags = write_resource_flags,
898         };
899         return do_write_stream_list(stream_list,
900                                     lookup_table,
901                                     serial_write_stream,
902                                     &ctx,
903                                     progress_data);
904 }
905
906 static inline int
907 write_flags_to_resource_flags(int write_flags)
908 {
909         int resource_flags = 0;
910
911         if (write_flags & WIMLIB_WRITE_FLAG_RECOMPRESS)
912                 resource_flags |= WIMLIB_WRITE_RESOURCE_FLAG_RECOMPRESS;
913         if (write_flags & WIMLIB_WRITE_FLAG_PIPABLE)
914                 resource_flags |= WIMLIB_WRITE_RESOURCE_FLAG_PIPABLE;
915         return resource_flags;
916 }
917
918 static int
919 write_stream_list_serial(struct list_head *stream_list,
920                          struct wim_lookup_table *lookup_table,
921                          struct filedes *out_fd,
922                          int out_ctype,
923                          int write_resource_flags,
924                          struct write_streams_progress_data *progress_data)
925 {
926         union wimlib_progress_info *progress = &progress_data->progress;
927         DEBUG("Writing stream list of size %"PRIu64" (serial version)",
928               progress->write_streams.total_streams);
929         progress->write_streams.num_threads = 1;
930         if (progress_data->progress_func) {
931                 progress_data->progress_func(WIMLIB_PROGRESS_MSG_WRITE_STREAMS,
932                                              progress);
933         }
934         return do_write_stream_list_serial(stream_list,
935                                            lookup_table,
936                                            out_fd,
937                                            out_ctype,
938                                            write_resource_flags,
939                                            progress_data);
940 }
941
942 #ifdef ENABLE_MULTITHREADED_COMPRESSION
943 static int
944 write_wim_chunks(struct message *msg, struct filedes *out_fd,
945                  struct chunk_table *chunk_tab,
946                  int write_resource_flags)
947 {
948         struct iovec *vecs;
949         struct pwm_chunk_hdr *chunk_hdrs;
950         unsigned nvecs;
951         int ret;
952
953         for (unsigned i = 0; i < msg->num_chunks; i++)
954                 chunk_tab_record_chunk(chunk_tab, msg->out_chunks[i].iov_len);
955
956         if (!(write_resource_flags & WIMLIB_WRITE_RESOURCE_FLAG_PIPABLE)) {
957                 nvecs = msg->num_chunks;
958                 vecs = msg->out_chunks;
959         } else {
960                 /* Special case:  If writing a compressed resource to a pipable
961                  * WIM, prefix each compressed chunk with a header that gives
962                  * its compressed size.  */
963                 nvecs = msg->num_chunks * 2;
964                 vecs = alloca(nvecs * sizeof(vecs[0]));
965                 chunk_hdrs = alloca(msg->num_chunks * sizeof(chunk_hdrs[0]));
966
967                 for (unsigned i = 0; i < msg->num_chunks; i++) {
968                         chunk_hdrs[i].compressed_size = cpu_to_le32(msg->out_chunks[i].iov_len);
969                         vecs[i * 2].iov_base = &chunk_hdrs[i];
970                         vecs[i * 2].iov_len = sizeof(chunk_hdrs[i]);
971                         vecs[i * 2 + 1].iov_base = msg->out_chunks[i].iov_base;
972                         vecs[i * 2 + 1].iov_len = msg->out_chunks[i].iov_len;
973                 }
974         }
975         ret = full_writev(out_fd, vecs, nvecs);
976         if (ret)
977                 ERROR_WITH_ERRNO("Failed to write WIM chunks");
978         return ret;
979 }
980
981 struct main_writer_thread_ctx {
982         struct list_head *stream_list;
983         struct wim_lookup_table *lookup_table;
984         struct filedes *out_fd;
985         off_t res_start_offset;
986         int out_ctype;
987         int write_resource_flags;
988         struct shared_queue *res_to_compress_queue;
989         struct shared_queue *compressed_res_queue;
990         size_t num_messages;
991         struct write_streams_progress_data *progress_data;
992
993         struct list_head available_msgs;
994         struct list_head outstanding_streams;
995         struct list_head serial_streams;
996         size_t num_outstanding_messages;
997
998         SHA_CTX next_sha_ctx;
999         u64 next_chunk;
1000         u64 next_num_chunks;
1001         struct wim_lookup_table_entry *next_lte;
1002
1003         struct message *msgs;
1004         struct message *next_msg;
1005         struct chunk_table *cur_chunk_tab;
1006 };
1007
1008 static int
1009 init_message(struct message *msg)
1010 {
1011         for (size_t i = 0; i < MAX_CHUNKS_PER_MSG; i++) {
1012                 msg->compressed_chunks[i] = MALLOC(WIM_CHUNK_SIZE);
1013                 msg->uncompressed_chunks[i] = MALLOC(WIM_CHUNK_SIZE);
1014                 if (msg->compressed_chunks[i] == NULL ||
1015                     msg->uncompressed_chunks[i] == NULL)
1016                         return WIMLIB_ERR_NOMEM;
1017         }
1018         return 0;
1019 }
1020
1021 static void
1022 destroy_message(struct message *msg)
1023 {
1024         for (size_t i = 0; i < MAX_CHUNKS_PER_MSG; i++) {
1025                 FREE(msg->compressed_chunks[i]);
1026                 FREE(msg->uncompressed_chunks[i]);
1027         }
1028 }
1029
1030 static void
1031 free_messages(struct message *msgs, size_t num_messages)
1032 {
1033         if (msgs) {
1034                 for (size_t i = 0; i < num_messages; i++)
1035                         destroy_message(&msgs[i]);
1036                 FREE(msgs);
1037         }
1038 }
1039
1040 static struct message *
1041 allocate_messages(size_t num_messages)
1042 {
1043         struct message *msgs;
1044
1045         msgs = CALLOC(num_messages, sizeof(struct message));
1046         if (!msgs)
1047                 return NULL;
1048         for (size_t i = 0; i < num_messages; i++) {
1049                 if (init_message(&msgs[i])) {
1050                         free_messages(msgs, num_messages);
1051                         return NULL;
1052                 }
1053         }
1054         return msgs;
1055 }
1056
1057 static void
1058 main_writer_thread_destroy_ctx(struct main_writer_thread_ctx *ctx)
1059 {
1060         while (ctx->num_outstanding_messages--)
1061                 shared_queue_get(ctx->compressed_res_queue);
1062         free_messages(ctx->msgs, ctx->num_messages);
1063         FREE(ctx->cur_chunk_tab);
1064 }
1065
1066 static int
1067 main_writer_thread_init_ctx(struct main_writer_thread_ctx *ctx)
1068 {
1069         /* Pre-allocate all the buffers that will be needed to do the chunk
1070          * compression. */
1071         ctx->msgs = allocate_messages(ctx->num_messages);
1072         if (!ctx->msgs)
1073                 return WIMLIB_ERR_NOMEM;
1074
1075         /* Initially, all the messages are available to use. */
1076         INIT_LIST_HEAD(&ctx->available_msgs);
1077         for (size_t i = 0; i < ctx->num_messages; i++)
1078                 list_add_tail(&ctx->msgs[i].list, &ctx->available_msgs);
1079
1080         /* outstanding_streams is the list of streams that currently have had
1081          * chunks sent off for compression.
1082          *
1083          * The first stream in outstanding_streams is the stream that is
1084          * currently being written.
1085          *
1086          * The last stream in outstanding_streams is the stream that is
1087          * currently being read and having chunks fed to the compressor threads.
1088          * */
1089         INIT_LIST_HEAD(&ctx->outstanding_streams);
1090         ctx->num_outstanding_messages = 0;
1091
1092         ctx->next_msg = NULL;
1093
1094         /* Resources that don't need any chunks compressed are added to this
1095          * list and written directly by the main thread. */
1096         INIT_LIST_HEAD(&ctx->serial_streams);
1097
1098         ctx->cur_chunk_tab = NULL;
1099
1100         return 0;
1101 }
1102
1103 static int
1104 receive_compressed_chunks(struct main_writer_thread_ctx *ctx)
1105 {
1106         struct message *msg;
1107         struct wim_lookup_table_entry *cur_lte;
1108         int ret;
1109
1110         wimlib_assert(!list_empty(&ctx->outstanding_streams));
1111         wimlib_assert(ctx->num_outstanding_messages != 0);
1112
1113         cur_lte = container_of(ctx->outstanding_streams.next,
1114                                struct wim_lookup_table_entry,
1115                                being_compressed_list);
1116
1117         /* Get the next message from the queue and process it.
1118          * The message will contain 1 or more data chunks that have been
1119          * compressed. */
1120         msg = shared_queue_get(ctx->compressed_res_queue);
1121         msg->complete = true;
1122         --ctx->num_outstanding_messages;
1123
1124         /* Is this the next chunk in the current resource?  If it's not
1125          * (i.e., an earlier chunk in a same or different resource
1126          * hasn't been compressed yet), do nothing, and keep this
1127          * message around until all earlier chunks are received.
1128          *
1129          * Otherwise, write all the chunks we can. */
1130         while (cur_lte != NULL &&
1131                !list_empty(&cur_lte->msg_list)
1132                && (msg = container_of(cur_lte->msg_list.next,
1133                                       struct message,
1134                                       list))->complete)
1135         {
1136                 list_move(&msg->list, &ctx->available_msgs);
1137                 if (msg->begin_chunk == 0) {
1138                         /* First set of chunks.  */
1139
1140                         /* Write pipable WIM stream header if needed.  */
1141                         if (ctx->write_resource_flags &
1142                             WIMLIB_WRITE_RESOURCE_FLAG_PIPABLE)
1143                         {
1144                                 ret = write_pwm_stream_header(cur_lte, ctx->out_fd,
1145                                                               WIM_RESHDR_FLAG_COMPRESSED);
1146                                 if (ret)
1147                                         return ret;
1148                         }
1149
1150                         /* Save current offset.  */
1151                         ctx->res_start_offset = ctx->out_fd->offset;
1152
1153                         /* Begin building the chunk table, and leave space for
1154                          * it if needed.  */
1155                         ret = begin_wim_resource_chunk_tab(cur_lte,
1156                                                            ctx->out_fd,
1157                                                            &ctx->cur_chunk_tab,
1158                                                            ctx->write_resource_flags);
1159                         if (ret)
1160                                 return ret;
1161
1162                 }
1163
1164                 /* Write the compressed chunks from the message. */
1165                 ret = write_wim_chunks(msg, ctx->out_fd, ctx->cur_chunk_tab,
1166                                        ctx->write_resource_flags);
1167                 if (ret)
1168                         return ret;
1169
1170                 /* Was this the last chunk of the stream?  If so, finish
1171                  * it. */
1172                 if (list_empty(&cur_lte->msg_list) &&
1173                     msg->begin_chunk + msg->num_chunks == ctx->cur_chunk_tab->num_chunks)
1174                 {
1175                         u64 res_csize;
1176
1177                         ret = finish_wim_resource_chunk_tab(ctx->cur_chunk_tab,
1178                                                             ctx->out_fd,
1179                                                             ctx->res_start_offset,
1180                                                             ctx->write_resource_flags);
1181                         if (ret)
1182                                 return ret;
1183
1184                         list_del(&cur_lte->being_compressed_list);
1185
1186                         res_csize = ctx->out_fd->offset - ctx->res_start_offset;
1187
1188                         FREE(ctx->cur_chunk_tab);
1189                         ctx->cur_chunk_tab = NULL;
1190
1191                         /* Check for resources compressed to greater than or
1192                          * equal to their original size and write them
1193                          * uncompressed instead.  (But never do this if writing
1194                          * to a pipe.)  */
1195                         if (res_csize >= wim_resource_size(cur_lte) &&
1196                             !(ctx->write_resource_flags & WIMLIB_WRITE_RESOURCE_FLAG_PIPABLE))
1197                         {
1198                                 DEBUG("Compressed %"PRIu64" => %"PRIu64" bytes; "
1199                                       "writing uncompressed instead",
1200                                       wim_resource_size(cur_lte), res_csize);
1201                                 ret = seek_and_truncate(ctx->out_fd, ctx->res_start_offset);
1202                                 if (ret)
1203                                         return ret;
1204                                 ret = write_wim_resource(cur_lte,
1205                                                          ctx->out_fd,
1206                                                          WIMLIB_COMPRESSION_TYPE_NONE,
1207                                                          &cur_lte->output_resource_entry,
1208                                                          ctx->write_resource_flags);
1209                                 if (ret)
1210                                         return ret;
1211                         } else {
1212                                 cur_lte->output_resource_entry.size =
1213                                         res_csize;
1214
1215                                 cur_lte->output_resource_entry.original_size =
1216                                         cur_lte->resource_entry.original_size;
1217
1218                                 cur_lte->output_resource_entry.offset =
1219                                         ctx->res_start_offset;
1220
1221                                 cur_lte->output_resource_entry.flags =
1222                                         cur_lte->resource_entry.flags |
1223                                                 WIM_RESHDR_FLAG_COMPRESSED;
1224
1225                                 DEBUG("Wrote compressed resource "
1226                                       "(%"PRIu64" => %"PRIu64" bytes @ +%"PRIu64", flags=0x%02x)",
1227                                       cur_lte->output_resource_entry.original_size,
1228                                       cur_lte->output_resource_entry.size,
1229                                       cur_lte->output_resource_entry.offset,
1230                                       cur_lte->output_resource_entry.flags);
1231                         }
1232
1233                         do_write_streams_progress(ctx->progress_data,
1234                                                   cur_lte, false);
1235
1236                         /* Since we just finished writing a stream, write any
1237                          * streams that have been added to the serial_streams
1238                          * list for direct writing by the main thread (e.g.
1239                          * resources that don't need to be compressed because
1240                          * the desired compression type is the same as the
1241                          * previous compression type). */
1242                         if (!list_empty(&ctx->serial_streams)) {
1243                                 ret = do_write_stream_list_serial(&ctx->serial_streams,
1244                                                                   ctx->lookup_table,
1245                                                                   ctx->out_fd,
1246                                                                   ctx->out_ctype,
1247                                                                   ctx->write_resource_flags,
1248                                                                   ctx->progress_data);
1249                                 if (ret)
1250                                         return ret;
1251                         }
1252
1253                         /* Advance to the next stream to write. */
1254                         if (list_empty(&ctx->outstanding_streams)) {
1255                                 cur_lte = NULL;
1256                         } else {
1257                                 cur_lte = container_of(ctx->outstanding_streams.next,
1258                                                        struct wim_lookup_table_entry,
1259                                                        being_compressed_list);
1260                         }
1261                 }
1262         }
1263         return 0;
1264 }
1265
1266 /* Called when the main thread has read a new chunk of data. */
1267 static int
1268 main_writer_thread_cb(const void *chunk, size_t chunk_size, void *_ctx)
1269 {
1270         struct main_writer_thread_ctx *ctx = _ctx;
1271         int ret;
1272         struct message *next_msg;
1273         u64 next_chunk_in_msg;
1274
1275         /* Update SHA1 message digest for the stream currently being read by the
1276          * main thread. */
1277         sha1_update(&ctx->next_sha_ctx, chunk, chunk_size);
1278
1279         /* We send chunks of data to the compressor chunks in batches which we
1280          * refer to as "messages".  @next_msg is the message that is currently
1281          * being prepared to send off.  If it is NULL, that indicates that we
1282          * need to start a new message. */
1283         next_msg = ctx->next_msg;
1284         if (!next_msg) {
1285                 /* We need to start a new message.  First check to see if there
1286                  * is a message available in the list of available messages.  If
1287                  * so, we can just take one.  If not, all the messages (there is
1288                  * a fixed number of them, proportional to the number of
1289                  * threads) have been sent off to the compressor threads, so we
1290                  * receive messages from the compressor threads containing
1291                  * compressed chunks of data.
1292                  *
1293                  * We may need to receive multiple messages before one is
1294                  * actually available to use because messages received that are
1295                  * *not* for the very next set of chunks to compress must be
1296                  * buffered until it's time to write those chunks. */
1297                 while (list_empty(&ctx->available_msgs)) {
1298                         ret = receive_compressed_chunks(ctx);
1299                         if (ret)
1300                                 return ret;
1301                 }
1302
1303                 next_msg = container_of(ctx->available_msgs.next,
1304                                         struct message, list);
1305                 list_del(&next_msg->list);
1306                 next_msg->complete = false;
1307                 next_msg->begin_chunk = ctx->next_chunk;
1308                 next_msg->num_chunks = min(MAX_CHUNKS_PER_MSG,
1309                                            ctx->next_num_chunks - ctx->next_chunk);
1310                 ctx->next_msg = next_msg;
1311         }
1312
1313         /* Fill in the next chunk to compress */
1314         next_chunk_in_msg = ctx->next_chunk - next_msg->begin_chunk;
1315
1316         next_msg->uncompressed_chunk_sizes[next_chunk_in_msg] = chunk_size;
1317         memcpy(next_msg->uncompressed_chunks[next_chunk_in_msg],
1318                chunk, chunk_size);
1319         ctx->next_chunk++;
1320         if (++next_chunk_in_msg == next_msg->num_chunks) {
1321                 /* Send off an array of chunks to compress */
1322                 list_add_tail(&next_msg->list, &ctx->next_lte->msg_list);
1323                 shared_queue_put(ctx->res_to_compress_queue, next_msg);
1324                 ++ctx->num_outstanding_messages;
1325                 ctx->next_msg = NULL;
1326         }
1327         return 0;
1328 }
1329
1330 static int
1331 main_writer_thread_finish(void *_ctx)
1332 {
1333         struct main_writer_thread_ctx *ctx = _ctx;
1334         int ret;
1335         while (ctx->num_outstanding_messages != 0) {
1336                 ret = receive_compressed_chunks(ctx);
1337                 if (ret)
1338                         return ret;
1339         }
1340         wimlib_assert(list_empty(&ctx->outstanding_streams));
1341         return do_write_stream_list_serial(&ctx->serial_streams,
1342                                            ctx->lookup_table,
1343                                            ctx->out_fd,
1344                                            ctx->out_ctype,
1345                                            ctx->write_resource_flags,
1346                                            ctx->progress_data);
1347 }
1348
1349 static int
1350 submit_stream_for_compression(struct wim_lookup_table_entry *lte,
1351                               struct main_writer_thread_ctx *ctx)
1352 {
1353         int ret;
1354
1355         /* Read the entire stream @lte, feeding its data chunks to the
1356          * compressor threads.  Also SHA1-sum the stream; this is required in
1357          * the case that @lte is unhashed, and a nice additional verification
1358          * when @lte is already hashed. */
1359         sha1_init(&ctx->next_sha_ctx);
1360         ctx->next_chunk = 0;
1361         ctx->next_num_chunks = wim_resource_chunks(lte);
1362         ctx->next_lte = lte;
1363         INIT_LIST_HEAD(&lte->msg_list);
1364         list_add_tail(&lte->being_compressed_list, &ctx->outstanding_streams);
1365         ret = read_resource_prefix(lte, wim_resource_size(lte),
1366                                    main_writer_thread_cb, ctx, 0);
1367         if (ret)
1368                 return ret;
1369         wimlib_assert(ctx->next_chunk == ctx->next_num_chunks);
1370         return finalize_and_check_sha1(&ctx->next_sha_ctx, lte);
1371 }
1372
1373 static int
1374 main_thread_process_next_stream(struct wim_lookup_table_entry *lte, void *_ctx)
1375 {
1376         struct main_writer_thread_ctx *ctx = _ctx;
1377         int ret;
1378
1379         if (wim_resource_size(lte) < 1000 ||
1380             ctx->out_ctype == WIMLIB_COMPRESSION_TYPE_NONE ||
1381             (lte->resource_location == RESOURCE_IN_WIM &&
1382              !(ctx->write_resource_flags & WIMLIB_WRITE_RESOURCE_FLAG_RECOMPRESS) &&
1383              lte->wim->compression_type == ctx->out_ctype))
1384         {
1385                 /* Stream is too small or isn't being compressed.  Process it by
1386                  * the main thread when we have a chance.  We can't necessarily
1387                  * process it right here, as the main thread could be in the
1388                  * middle of writing a different stream. */
1389                 list_add_tail(&lte->write_streams_list, &ctx->serial_streams);
1390                 lte->deferred = 1;
1391                 ret = 0;
1392         } else {
1393                 ret = submit_stream_for_compression(lte, ctx);
1394         }
1395         lte->no_progress = 1;
1396         return ret;
1397 }
1398
1399 static long
1400 get_default_num_threads(void)
1401 {
1402 #ifdef __WIN32__
1403         return win32_get_number_of_processors();
1404 #else
1405         return sysconf(_SC_NPROCESSORS_ONLN);
1406 #endif
1407 }
1408
1409 /* Equivalent to write_stream_list_serial(), except this takes a @num_threads
1410  * parameter and will perform compression using that many threads.  Falls
1411  * back to write_stream_list_serial() on certain errors, such as a failure to
1412  * create the number of threads requested.
1413  *
1414  * High level description of the algorithm for writing compressed streams in
1415  * parallel:  We perform compression on chunks of size WIM_CHUNK_SIZE bytes
1416  * rather than on full files.  The currently executing thread becomes the main
1417  * thread and is entirely in charge of reading the data to compress (which may
1418  * be in any location understood by the resource code--- such as in an external
1419  * file being captured, or in another WIM file from which an image is being
1420  * exported) and actually writing the compressed data to the output file.
1421  * Additional threads are "compressor threads" and all execute the
1422  * compressor_thread_proc, where they repeatedly retrieve buffers of data from
1423  * the main thread, compress them, and hand them back to the main thread.
1424  *
1425  * Certain streams, such as streams that do not need to be compressed (e.g.
1426  * input compression type same as output compression type) or streams of very
1427  * small size are placed in a list (main_writer_thread_ctx.serial_list) and
1428  * handled entirely by the main thread at an appropriate time.
1429  *
1430  * At any given point in time, multiple streams may be having chunks compressed
1431  * concurrently.  The stream that the main thread is currently *reading* may be
1432  * later in the list that the stream that the main thread is currently
1433  * *writing*.
1434  */
1435 static int
1436 write_stream_list_parallel(struct list_head *stream_list,
1437                            struct wim_lookup_table *lookup_table,
1438                            struct filedes *out_fd,
1439                            int out_ctype,
1440                            int write_resource_flags,
1441                            struct write_streams_progress_data *progress_data,
1442                            unsigned num_threads)
1443 {
1444         int ret;
1445         struct shared_queue res_to_compress_queue;
1446         struct shared_queue compressed_res_queue;
1447         pthread_t *compressor_threads = NULL;
1448         union wimlib_progress_info *progress = &progress_data->progress;
1449
1450         if (num_threads == 0) {
1451                 long nthreads = get_default_num_threads();
1452                 if (nthreads < 1 || nthreads > UINT_MAX) {
1453                         WARNING("Could not determine number of processors! Assuming 1");
1454                         goto out_serial;
1455                 } else if (nthreads == 1) {
1456                         goto out_serial_quiet;
1457                 } else {
1458                         num_threads = nthreads;
1459                 }
1460         }
1461
1462         DEBUG("Writing stream list of size %"PRIu64" "
1463               "(parallel version, num_threads=%u)",
1464               progress->write_streams.total_streams, num_threads);
1465
1466         progress->write_streams.num_threads = num_threads;
1467
1468         static const size_t MESSAGES_PER_THREAD = 2;
1469         size_t queue_size = (size_t)(num_threads * MESSAGES_PER_THREAD);
1470
1471         DEBUG("Initializing shared queues (queue_size=%zu)", queue_size);
1472
1473         ret = shared_queue_init(&res_to_compress_queue, queue_size);
1474         if (ret)
1475                 goto out_serial;
1476
1477         ret = shared_queue_init(&compressed_res_queue, queue_size);
1478         if (ret)
1479                 goto out_destroy_res_to_compress_queue;
1480
1481         struct compressor_thread_params params;
1482         params.res_to_compress_queue = &res_to_compress_queue;
1483         params.compressed_res_queue = &compressed_res_queue;
1484         params.compress = get_compress_func(out_ctype);
1485
1486         compressor_threads = MALLOC(num_threads * sizeof(pthread_t));
1487         if (!compressor_threads) {
1488                 ret = WIMLIB_ERR_NOMEM;
1489                 goto out_destroy_compressed_res_queue;
1490         }
1491
1492         for (unsigned i = 0; i < num_threads; i++) {
1493                 DEBUG("pthread_create thread %u of %u", i + 1, num_threads);
1494                 ret = pthread_create(&compressor_threads[i], NULL,
1495                                      compressor_thread_proc, &params);
1496                 if (ret != 0) {
1497                         ret = -1;
1498                         ERROR_WITH_ERRNO("Failed to create compressor "
1499                                          "thread %u of %u",
1500                                          i + 1, num_threads);
1501                         num_threads = i;
1502                         goto out_join;
1503                 }
1504         }
1505
1506         if (progress_data->progress_func) {
1507                 progress_data->progress_func(WIMLIB_PROGRESS_MSG_WRITE_STREAMS,
1508                                              progress);
1509         }
1510
1511         struct main_writer_thread_ctx ctx;
1512         ctx.stream_list           = stream_list;
1513         ctx.lookup_table          = lookup_table;
1514         ctx.out_fd                = out_fd;
1515         ctx.out_ctype             = out_ctype;
1516         ctx.res_to_compress_queue = &res_to_compress_queue;
1517         ctx.compressed_res_queue  = &compressed_res_queue;
1518         ctx.num_messages          = queue_size;
1519         ctx.write_resource_flags  = write_resource_flags;
1520         ctx.progress_data         = progress_data;
1521         ret = main_writer_thread_init_ctx(&ctx);
1522         if (ret)
1523                 goto out_join;
1524         ret = do_write_stream_list(stream_list, lookup_table,
1525                                    main_thread_process_next_stream,
1526                                    &ctx, progress_data);
1527         if (ret)
1528                 goto out_destroy_ctx;
1529
1530         /* The main thread has finished reading all streams that are going to be
1531          * compressed in parallel, and it now needs to wait for all remaining
1532          * chunks to be compressed so that the remaining streams can actually be
1533          * written to the output file.  Furthermore, any remaining streams that
1534          * had processing deferred to the main thread need to be handled.  These
1535          * tasks are done by the main_writer_thread_finish() function. */
1536         ret = main_writer_thread_finish(&ctx);
1537 out_destroy_ctx:
1538         main_writer_thread_destroy_ctx(&ctx);
1539 out_join:
1540         for (unsigned i = 0; i < num_threads; i++)
1541                 shared_queue_put(&res_to_compress_queue, NULL);
1542
1543         for (unsigned i = 0; i < num_threads; i++) {
1544                 if (pthread_join(compressor_threads[i], NULL)) {
1545                         WARNING_WITH_ERRNO("Failed to join compressor "
1546                                            "thread %u of %u",
1547                                            i + 1, num_threads);
1548                 }
1549         }
1550         FREE(compressor_threads);
1551 out_destroy_compressed_res_queue:
1552         shared_queue_destroy(&compressed_res_queue);
1553 out_destroy_res_to_compress_queue:
1554         shared_queue_destroy(&res_to_compress_queue);
1555         if (ret >= 0 && ret != WIMLIB_ERR_NOMEM)
1556                 return ret;
1557 out_serial:
1558         WARNING("Falling back to single-threaded compression");
1559 out_serial_quiet:
1560         return write_stream_list_serial(stream_list,
1561                                         lookup_table,
1562                                         out_fd,
1563                                         out_ctype,
1564                                         write_resource_flags,
1565                                         progress_data);
1566
1567 }
1568 #endif
1569
1570 /*
1571  * Write a list of streams to a WIM (@out_fd) using the compression type
1572  * @out_ctype and up to @num_threads compressor threads.
1573  */
1574 static int
1575 write_stream_list(struct list_head *stream_list,
1576                   struct wim_lookup_table *lookup_table,
1577                   struct filedes *out_fd, int out_ctype, int write_flags,
1578                   unsigned num_threads, wimlib_progress_func_t progress_func)
1579 {
1580         struct wim_lookup_table_entry *lte;
1581         size_t num_streams = 0;
1582         u64 total_bytes = 0;
1583         u64 total_compression_bytes = 0;
1584         struct write_streams_progress_data progress_data;
1585         int ret;
1586         int write_resource_flags;
1587         unsigned total_parts = 0;
1588         WIMStruct *prev_wim_part = NULL;
1589
1590         if (list_empty(stream_list))
1591                 return 0;
1592
1593         write_resource_flags = write_flags_to_resource_flags(write_flags);
1594
1595         DEBUG("write_resource_flags=0x%08x", write_resource_flags);
1596
1597         sort_stream_list_by_sequential_order(stream_list,
1598                                              offsetof(struct wim_lookup_table_entry,
1599                                                       write_streams_list));
1600
1601         /* Calculate the total size of the streams to be written.  Note: this
1602          * will be the uncompressed size, as we may not know the compressed size
1603          * yet, and also this will assume that every unhashed stream will be
1604          * written (which will not necessarily be the case). */
1605         list_for_each_entry(lte, stream_list, write_streams_list) {
1606                 num_streams++;
1607                 total_bytes += wim_resource_size(lte);
1608                 if (out_ctype != WIMLIB_COMPRESSION_TYPE_NONE
1609                        && (wim_resource_compression_type(lte) != out_ctype ||
1610                            (write_resource_flags & WIMLIB_WRITE_RESOURCE_FLAG_RECOMPRESS)))
1611                 {
1612                         total_compression_bytes += wim_resource_size(lte);
1613                 }
1614                 if (lte->resource_location == RESOURCE_IN_WIM) {
1615                         if (prev_wim_part != lte->wim) {
1616                                 prev_wim_part = lte->wim;
1617                                 total_parts++;
1618                         }
1619                 }
1620
1621         }
1622
1623         memset(&progress_data, 0, sizeof(progress_data));
1624         progress_data.progress_func = progress_func;
1625
1626         progress_data.progress.write_streams.total_bytes       = total_bytes;
1627         progress_data.progress.write_streams.total_streams     = num_streams;
1628         progress_data.progress.write_streams.completed_bytes   = 0;
1629         progress_data.progress.write_streams.completed_streams = 0;
1630         progress_data.progress.write_streams.num_threads       = num_threads;
1631         progress_data.progress.write_streams.compression_type  = out_ctype;
1632         progress_data.progress.write_streams.total_parts       = total_parts;
1633         progress_data.progress.write_streams.completed_parts   = 0;
1634
1635         progress_data.next_progress = 0;
1636         progress_data.prev_wim_part = NULL;
1637
1638 #ifdef ENABLE_MULTITHREADED_COMPRESSION
1639         if (total_compression_bytes >= 2000000 && num_threads != 1)
1640                 ret = write_stream_list_parallel(stream_list,
1641                                                  lookup_table,
1642                                                  out_fd,
1643                                                  out_ctype,
1644                                                  write_resource_flags,
1645                                                  &progress_data,
1646                                                  num_threads);
1647         else
1648 #endif
1649                 ret = write_stream_list_serial(stream_list,
1650                                                lookup_table,
1651                                                out_fd,
1652                                                out_ctype,
1653                                                write_resource_flags,
1654                                                &progress_data);
1655         if (ret == 0)
1656                 DEBUG("Successfully wrote stream list.");
1657         else
1658                 DEBUG("Failed to write stream list.");
1659         return ret;
1660 }
1661
1662 struct stream_size_table {
1663         struct hlist_head *array;
1664         size_t num_entries;
1665         size_t capacity;
1666 };
1667
1668 static int
1669 init_stream_size_table(struct stream_size_table *tab, size_t capacity)
1670 {
1671         tab->array = CALLOC(capacity, sizeof(tab->array[0]));
1672         if (!tab->array)
1673                 return WIMLIB_ERR_NOMEM;
1674         tab->num_entries = 0;
1675         tab->capacity = capacity;
1676         return 0;
1677 }
1678
1679 static void
1680 destroy_stream_size_table(struct stream_size_table *tab)
1681 {
1682         FREE(tab->array);
1683 }
1684
1685 static int
1686 stream_size_table_insert(struct wim_lookup_table_entry *lte, void *_tab)
1687 {
1688         struct stream_size_table *tab = _tab;
1689         size_t pos;
1690         struct wim_lookup_table_entry *same_size_lte;
1691         struct hlist_node *tmp;
1692
1693         pos = hash_u64(wim_resource_size(lte)) % tab->capacity;
1694         lte->unique_size = 1;
1695         hlist_for_each_entry(same_size_lte, tmp, &tab->array[pos], hash_list_2) {
1696                 if (wim_resource_size(same_size_lte) == wim_resource_size(lte)) {
1697                         lte->unique_size = 0;
1698                         same_size_lte->unique_size = 0;
1699                         break;
1700                 }
1701         }
1702
1703         hlist_add_head(&lte->hash_list_2, &tab->array[pos]);
1704         tab->num_entries++;
1705         return 0;
1706 }
1707
1708
1709 struct lte_overwrite_prepare_args {
1710         WIMStruct *wim;
1711         off_t end_offset;
1712         struct list_head stream_list;
1713         struct stream_size_table stream_size_tab;
1714 };
1715
1716 /* First phase of preparing streams for an in-place overwrite.  This is called
1717  * on all streams, both hashed and unhashed, except the metadata resources. */
1718 static int
1719 lte_overwrite_prepare(struct wim_lookup_table_entry *lte, void *_args)
1720 {
1721         struct lte_overwrite_prepare_args *args = _args;
1722
1723         wimlib_assert(!(lte->resource_entry.flags & WIM_RESHDR_FLAG_METADATA));
1724         if (lte->resource_location != RESOURCE_IN_WIM || lte->wim != args->wim)
1725                 list_add_tail(&lte->write_streams_list, &args->stream_list);
1726         lte->out_refcnt = lte->refcnt;
1727         stream_size_table_insert(lte, &args->stream_size_tab);
1728         return 0;
1729 }
1730
1731 /* Second phase of preparing streams for an in-place overwrite.  This is called
1732  * on existing metadata resources and hashed streams, but not unhashed streams.
1733  *
1734  * NOTE: lte->output_resource_entry is in union with lte->hash_list_2, so
1735  * lte_overwrite_prepare_2() must be called after lte_overwrite_prepare(), as
1736  * the latter uses lte->hash_list_2, while the former expects to set
1737  * lte->output_resource_entry. */
1738 static int
1739 lte_overwrite_prepare_2(struct wim_lookup_table_entry *lte, void *_args)
1740 {
1741         struct lte_overwrite_prepare_args *args = _args;
1742
1743         if (lte->resource_location == RESOURCE_IN_WIM && lte->wim == args->wim) {
1744                 /* We can't do an in place overwrite on the WIM if there are
1745                  * streams after the XML data. */
1746                 if (lte->resource_entry.offset +
1747                     lte->resource_entry.size > args->end_offset)
1748                 {
1749                         if (wimlib_print_errors) {
1750                                 ERROR("The following resource is after the XML data:");
1751                                 print_lookup_table_entry(lte, stderr);
1752                         }
1753                         return WIMLIB_ERR_RESOURCE_ORDER;
1754                 }
1755                 copy_resource_entry(&lte->output_resource_entry,
1756                                     &lte->resource_entry);
1757         }
1758         return 0;
1759 }
1760
1761 /* Given a WIM that we are going to overwrite in place with zero or more
1762  * additional streams added, construct a list the list of new unique streams
1763  * ('struct wim_lookup_table_entry's) that must be written, plus any unhashed
1764  * streams that need to be added but may be identical to other hashed or
1765  * unhashed streams.  These unhashed streams are checksummed while the streams
1766  * are being written.  To aid this process, the member @unique_size is set to 1
1767  * on streams that have a unique size and therefore must be written.
1768  *
1769  * The out_refcnt member of each 'struct wim_lookup_table_entry' is set to
1770  * indicate the number of times the stream is referenced in only the streams
1771  * that are being written; this may still be adjusted later when unhashed
1772  * streams are being resolved.
1773  */
1774 static int
1775 prepare_streams_for_overwrite(WIMStruct *wim, off_t end_offset,
1776                               struct list_head *stream_list)
1777 {
1778         int ret;
1779         struct lte_overwrite_prepare_args args;
1780         unsigned i;
1781
1782         args.wim = wim;
1783         args.end_offset = end_offset;
1784         ret = init_stream_size_table(&args.stream_size_tab,
1785                                      wim->lookup_table->capacity);
1786         if (ret)
1787                 return ret;
1788
1789         INIT_LIST_HEAD(&args.stream_list);
1790         for (i = 0; i < wim->hdr.image_count; i++) {
1791                 struct wim_image_metadata *imd;
1792                 struct wim_lookup_table_entry *lte;
1793
1794                 imd = wim->image_metadata[i];
1795                 image_for_each_unhashed_stream(lte, imd)
1796                         lte_overwrite_prepare(lte, &args);
1797         }
1798         for_lookup_table_entry(wim->lookup_table, lte_overwrite_prepare, &args);
1799         list_transfer(&args.stream_list, stream_list);
1800
1801         for (i = 0; i < wim->hdr.image_count; i++) {
1802                 ret = lte_overwrite_prepare_2(wim->image_metadata[i]->metadata_lte,
1803                                               &args);
1804                 if (ret)
1805                         goto out_destroy_stream_size_table;
1806         }
1807         ret = for_lookup_table_entry(wim->lookup_table,
1808                                      lte_overwrite_prepare_2, &args);
1809 out_destroy_stream_size_table:
1810         destroy_stream_size_table(&args.stream_size_tab);
1811         return ret;
1812 }
1813
1814
1815 struct find_streams_ctx {
1816         struct list_head stream_list;
1817         struct stream_size_table stream_size_tab;
1818 };
1819
1820 static void
1821 lte_reference_for_write(struct wim_lookup_table_entry *lte,
1822                         struct find_streams_ctx *ctx,
1823                         unsigned nref)
1824 {
1825         if (lte->out_refcnt == 0) {
1826                 if (lte->unhashed)
1827                         stream_size_table_insert(lte, &ctx->stream_size_tab);
1828                 list_add_tail(&lte->write_streams_list, &ctx->stream_list);
1829         }
1830         lte->out_refcnt += nref;
1831 }
1832
1833 static int
1834 do_lte_reference_for_write(struct wim_lookup_table_entry *lte, void *_ctx)
1835 {
1836         struct find_streams_ctx *ctx = _ctx;
1837         lte->out_refcnt = 0;
1838         lte_reference_for_write(lte, ctx, lte->refcnt);
1839         return 0;
1840 }
1841
1842 static void
1843 inode_find_streams_to_write(struct wim_inode *inode,
1844                             struct wim_lookup_table *table,
1845                             struct find_streams_ctx *ctx)
1846 {
1847         struct wim_lookup_table_entry *lte;
1848         unsigned i;
1849
1850         for (i = 0; i <= inode->i_num_ads; i++) {
1851                 lte = inode_stream_lte(inode, i, table);
1852                 if (lte)
1853                         lte_reference_for_write(lte, ctx, inode->i_nlink);
1854         }
1855 }
1856
1857 static int
1858 image_find_streams_to_write(WIMStruct *wim)
1859 {
1860         struct find_streams_ctx *ctx;
1861         struct wim_image_metadata *imd;
1862         struct wim_inode *inode;
1863         struct wim_lookup_table_entry *lte;
1864
1865         ctx = wim->private;
1866         imd = wim_get_current_image_metadata(wim);
1867
1868         image_for_each_unhashed_stream(lte, imd)
1869                 lte->out_refcnt = 0;
1870
1871         /* Go through this image's inodes to find any streams that have not been
1872          * found yet. */
1873         image_for_each_inode(inode, imd)
1874                 inode_find_streams_to_write(inode, wim->lookup_table, ctx);
1875         return 0;
1876 }
1877
1878 /* Given a WIM that from which one or all of the images is being written, build
1879  * the list of unique streams ('struct wim_lookup_table_entry's) that must be
1880  * written, plus any unhashed streams that need to be written but may be
1881  * identical to other hashed or unhashed streams being written.  These unhashed
1882  * streams are checksummed while the streams are being written.  To aid this
1883  * process, the member @unique_size is set to 1 on streams that have a unique
1884  * size and therefore must be written.
1885  *
1886  * The out_refcnt member of each 'struct wim_lookup_table_entry' is set to
1887  * indicate the number of times the stream is referenced in only the streams
1888  * that are being written; this may still be adjusted later when unhashed
1889  * streams are being resolved.
1890  */
1891 static int
1892 prepare_stream_list(WIMStruct *wim, int image, struct list_head *stream_list)
1893 {
1894         int ret;
1895         struct find_streams_ctx ctx;
1896
1897         DEBUG("Preparing list of streams to write for image %d.", image);
1898
1899         for_lookup_table_entry(wim->lookup_table, lte_zero_out_refcnt, NULL);
1900         ret = init_stream_size_table(&ctx.stream_size_tab,
1901                                      wim->lookup_table->capacity);
1902         if (ret)
1903                 return ret;
1904         for_lookup_table_entry(wim->lookup_table, stream_size_table_insert,
1905                                &ctx.stream_size_tab);
1906         INIT_LIST_HEAD(&ctx.stream_list);
1907         wim->private = &ctx;
1908
1909 #if 1
1910         /* Optimization enabled by default:  if we're writing all the images,
1911          * it's not strictly necessary to decompress, parse, and go through the
1912          * dentry tree in each image's metadata resource.  Instead, include all
1913          * the hashed streams referenced from the lookup table as well as all
1914          * unhashed streams referenced in the per-image list.  For 'out_refcnt'
1915          * for each stream, just copy the value from 'refcnt', which is the
1916          * reference count of that stream in the entire WIM.  */
1917         if (image == WIMLIB_ALL_IMAGES) {
1918                 struct wim_lookup_table_entry *lte;
1919                 struct wim_image_metadata *imd;
1920                 unsigned i;
1921
1922                 for_lookup_table_entry(wim->lookup_table,
1923                                        do_lte_reference_for_write, &ctx);
1924                 for (i = 0; i < wim->hdr.image_count; i++) {
1925                         imd = wim->image_metadata[i];
1926                         image_for_each_unhashed_stream(lte, imd)
1927                                 do_lte_reference_for_write(lte, &ctx);
1928                 }
1929                 ret = 0;
1930         } else
1931 #endif
1932                 ret = for_image(wim, image, image_find_streams_to_write);
1933
1934         destroy_stream_size_table(&ctx.stream_size_tab);
1935         if (ret)
1936                 return ret;
1937         list_transfer(&ctx.stream_list, stream_list);
1938         return 0;
1939 }
1940
1941 /* Writes the streams for the specified @image in @wim to @wim->out_fd.
1942  * Alternatively, if @stream_list_override is specified, it is taken to be the
1943  * list of streams to write (connected with 'write_streams_list') and @image is
1944  * ignored.  */
1945 static int
1946 write_wim_streams(WIMStruct *wim, int image, int write_flags,
1947                   unsigned num_threads,
1948                   wimlib_progress_func_t progress_func,
1949                   struct list_head *stream_list_override)
1950 {
1951         int ret;
1952         struct list_head _stream_list;
1953         struct list_head *stream_list;
1954         struct wim_lookup_table_entry *lte;
1955
1956         if (stream_list_override) {
1957                 stream_list = stream_list_override;
1958                 list_for_each_entry(lte, stream_list, write_streams_list) {
1959                         if (lte->refcnt)
1960                                 lte->out_refcnt = lte->refcnt;
1961                         else
1962                                 lte->out_refcnt = 1;
1963                 }
1964         } else {
1965                 stream_list = &_stream_list;
1966                 ret = prepare_stream_list(wim, image, stream_list);
1967                 if (ret)
1968                         return ret;
1969         }
1970         list_for_each_entry(lte, stream_list, write_streams_list)
1971                 lte->part_number = wim->hdr.part_number;
1972         return write_stream_list(stream_list,
1973                                  wim->lookup_table,
1974                                  &wim->out_fd,
1975                                  wim->compression_type,
1976                                  write_flags,
1977                                  num_threads,
1978                                  progress_func);
1979 }
1980
1981 static int
1982 write_wim_metadata_resources(WIMStruct *wim, int image, int write_flags,
1983                              wimlib_progress_func_t progress_func)
1984 {
1985         int ret;
1986         int start_image;
1987         int end_image;
1988         int write_resource_flags;
1989
1990         if (write_flags & WIMLIB_WRITE_FLAG_NO_METADATA)
1991                 return 0;
1992
1993         write_resource_flags = write_flags_to_resource_flags(write_flags);
1994
1995         DEBUG("Writing metadata resources (offset=%"PRIu64")",
1996               wim->out_fd.offset);
1997
1998         if (progress_func)
1999                 progress_func(WIMLIB_PROGRESS_MSG_WRITE_METADATA_BEGIN, NULL);
2000
2001         if (image == WIMLIB_ALL_IMAGES) {
2002                 start_image = 1;
2003                 end_image = wim->hdr.image_count;
2004         } else {
2005                 start_image = image;
2006                 end_image = image;
2007         }
2008
2009         for (int i = start_image; i <= end_image; i++) {
2010                 struct wim_image_metadata *imd;
2011
2012                 imd = wim->image_metadata[i - 1];
2013                 /* Build a new metadata resource only if image was modified from
2014                  * the original (or was newly added).  Otherwise just copy the
2015                  * existing one.  */
2016                 if (imd->modified) {
2017                         ret = write_metadata_resource(wim, i,
2018                                                       write_resource_flags);
2019                 } else {
2020                         ret = write_wim_resource(imd->metadata_lte,
2021                                                  &wim->out_fd,
2022                                                  wim->compression_type,
2023                                                  &imd->metadata_lte->output_resource_entry,
2024                                                  write_resource_flags);
2025                 }
2026                 if (ret)
2027                         return ret;
2028         }
2029         if (progress_func)
2030                 progress_func(WIMLIB_PROGRESS_MSG_WRITE_METADATA_END, NULL);
2031         return 0;
2032 }
2033
2034 static int
2035 open_wim_writable(WIMStruct *wim, const tchar *path, int open_flags)
2036 {
2037         int raw_fd;
2038         DEBUG("Opening \"%"TS"\" for writing.", path);
2039
2040         raw_fd = topen(path, open_flags | O_BINARY, 0644);
2041         if (raw_fd < 0) {
2042                 ERROR_WITH_ERRNO("Failed to open \"%"TS"\" for writing", path);
2043                 return WIMLIB_ERR_OPEN;
2044         }
2045         filedes_init(&wim->out_fd, raw_fd);
2046         return 0;
2047 }
2048
2049 static int
2050 close_wim_writable(WIMStruct *wim, int write_flags)
2051 {
2052         int ret = 0;
2053
2054         if (!(write_flags & WIMLIB_WRITE_FLAG_FILE_DESCRIPTOR))
2055                 if (filedes_valid(&wim->out_fd))
2056                         if (filedes_close(&wim->out_fd))
2057                                 ret = WIMLIB_ERR_WRITE;
2058         filedes_invalidate(&wim->out_fd);
2059         return ret;
2060 }
2061
2062 /*
2063  * Finish writing a WIM file: write the lookup table, xml data, and integrity
2064  * table, then overwrite the WIM header.  Always closes the WIM file descriptor
2065  * (wim->out_fd).
2066  *
2067  * write_flags is a bitwise OR of the following:
2068  *
2069  *      (public) WIMLIB_WRITE_FLAG_CHECK_INTEGRITY:
2070  *              Include an integrity table.
2071  *
2072  *      (public) WIMLIB_WRITE_FLAG_FSYNC:
2073  *              fsync() the output file before closing it.
2074  *
2075  *      (public)  WIMLIB_WRITE_FLAG_PIPABLE:
2076  *              Writing a pipable WIM, possibly to a pipe; include pipable WIM
2077  *              stream headers before the lookup table and XML data, and also
2078  *              write the WIM header at the end instead of seeking to the
2079  *              beginning.  Can't be combined with
2080  *              WIMLIB_WRITE_FLAG_CHECK_INTEGRITY.
2081  *
2082  *      (private) WIMLIB_WRITE_FLAG_NO_LOOKUP_TABLE:
2083  *              Don't write the lookup table.
2084  *
2085  *      (private) WIMLIB_WRITE_FLAG_REUSE_INTEGRITY_TABLE:
2086  *              When (if) writing the integrity table, re-use entries from the
2087  *              existing integrity table, if possible.
2088  *
2089  *      (private) WIMLIB_WRITE_FLAG_CHECKPOINT_AFTER_XML:
2090  *              After writing the XML data but before writing the integrity
2091  *              table, write a temporary WIM header and flush the stream so that
2092  *              the WIM is less likely to become corrupted upon abrupt program
2093  *              termination.
2094  *      (private) WIMLIB_WRITE_FLAG_HEADER_AT_END:
2095  *              Instead of overwriting the WIM header at the beginning of the
2096  *              file, simply append it to the end of the file.  (Used when
2097  *              writing to pipe.)
2098  *      (private) WIMLIB_WRITE_FLAG_USE_EXISTING_TOTALBYTES:
2099  *              Use the existing <TOTALBYTES> stored in the in-memory XML
2100  *              information, rather than setting it to the offset of the XML
2101  *              data being written.
2102  */
2103 static int
2104 finish_write(WIMStruct *wim, int image, int write_flags,
2105              wimlib_progress_func_t progress_func,
2106              struct list_head *stream_list_override)
2107 {
2108         int ret;
2109         off_t hdr_offset;
2110         int write_resource_flags;
2111         off_t old_lookup_table_end;
2112         off_t new_lookup_table_end;
2113         u64 xml_totalbytes;
2114
2115         write_resource_flags = write_flags_to_resource_flags(write_flags);
2116
2117         /* In the WIM header, there is room for the resource entry for a
2118          * metadata resource labeled as the "boot metadata".  This entry should
2119          * be zeroed out if there is no bootable image (boot_idx 0).  Otherwise,
2120          * it should be a copy of the resource entry for the image that is
2121          * marked as bootable.  This is not well documented...  */
2122         if (wim->hdr.boot_idx == 0) {
2123                 zero_resource_entry(&wim->hdr.boot_metadata_res_entry);
2124         } else {
2125                 copy_resource_entry(&wim->hdr.boot_metadata_res_entry,
2126                             &wim->image_metadata[wim->hdr.boot_idx- 1
2127                                         ]->metadata_lte->output_resource_entry);
2128         }
2129
2130         /* Write lookup table.  (Save old position first.)  */
2131         old_lookup_table_end = wim->hdr.lookup_table_res_entry.offset +
2132                                wim->hdr.lookup_table_res_entry.size;
2133         if (!(write_flags & WIMLIB_WRITE_FLAG_NO_LOOKUP_TABLE)) {
2134                 ret = write_wim_lookup_table(wim, image, write_flags,
2135                                              &wim->hdr.lookup_table_res_entry,
2136                                              stream_list_override);
2137                 if (ret)
2138                         goto out_close_wim;
2139         }
2140
2141         /* Write XML data.  */
2142         xml_totalbytes = wim->out_fd.offset;
2143         if (write_flags & WIMLIB_WRITE_FLAG_USE_EXISTING_TOTALBYTES)
2144                 xml_totalbytes = WIM_TOTALBYTES_USE_EXISTING;
2145         ret = write_wim_xml_data(wim, image, xml_totalbytes,
2146                                  &wim->hdr.xml_res_entry,
2147                                  write_resource_flags);
2148         if (ret)
2149                 goto out_close_wim;
2150
2151         if (write_flags & WIMLIB_WRITE_FLAG_CHECK_INTEGRITY) {
2152                 if (write_flags & WIMLIB_WRITE_FLAG_CHECKPOINT_AFTER_XML) {
2153                         struct wim_header checkpoint_hdr;
2154                         memcpy(&checkpoint_hdr, &wim->hdr, sizeof(struct wim_header));
2155                         zero_resource_entry(&checkpoint_hdr.integrity);
2156                         checkpoint_hdr.flags |= WIM_HDR_FLAG_WRITE_IN_PROGRESS;
2157                         ret = write_wim_header_at_offset(&checkpoint_hdr,
2158                                                          &wim->out_fd, 0);
2159                         if (ret)
2160                                 goto out_close_wim;
2161                 }
2162
2163                 if (!(write_flags & WIMLIB_WRITE_FLAG_REUSE_INTEGRITY_TABLE))
2164                         old_lookup_table_end = 0;
2165
2166                 new_lookup_table_end = wim->hdr.lookup_table_res_entry.offset +
2167                                        wim->hdr.lookup_table_res_entry.size;
2168
2169                 ret = write_integrity_table(wim,
2170                                             new_lookup_table_end,
2171                                             old_lookup_table_end,
2172                                             progress_func);
2173                 if (ret)
2174                         goto out_close_wim;
2175         } else {
2176                 zero_resource_entry(&wim->hdr.integrity);
2177         }
2178
2179         wim->hdr.flags &= ~WIM_HDR_FLAG_WRITE_IN_PROGRESS;
2180         hdr_offset = 0;
2181         if (write_flags & WIMLIB_WRITE_FLAG_HEADER_AT_END)
2182                 hdr_offset = wim->out_fd.offset;
2183         ret = write_wim_header_at_offset(&wim->hdr, &wim->out_fd, hdr_offset);
2184         if (ret)
2185                 goto out_close_wim;
2186
2187         if (write_flags & WIMLIB_WRITE_FLAG_FSYNC) {
2188                 if (fsync(wim->out_fd.fd)) {
2189                         ERROR_WITH_ERRNO("Error syncing data to WIM file");
2190                         ret = WIMLIB_ERR_WRITE;
2191                         goto out_close_wim;
2192                 }
2193         }
2194
2195         ret = 0;
2196 out_close_wim:
2197         if (close_wim_writable(wim, write_flags)) {
2198                 if (ret == 0) {
2199                         ERROR_WITH_ERRNO("Failed to close the output WIM file");
2200                         ret = WIMLIB_ERR_WRITE;
2201                 }
2202         }
2203         return ret;
2204 }
2205
2206 #if defined(HAVE_SYS_FILE_H) && defined(HAVE_FLOCK)
2207 int
2208 lock_wim(WIMStruct *wim, int fd)
2209 {
2210         int ret = 0;
2211         if (fd != -1 && !wim->wim_locked) {
2212                 ret = flock(fd, LOCK_EX | LOCK_NB);
2213                 if (ret != 0) {
2214                         if (errno == EWOULDBLOCK) {
2215                                 ERROR("`%"TS"' is already being modified or has been "
2216                                       "mounted read-write\n"
2217                                       "        by another process!", wim->filename);
2218                                 ret = WIMLIB_ERR_ALREADY_LOCKED;
2219                         } else {
2220                                 WARNING_WITH_ERRNO("Failed to lock `%"TS"'",
2221                                                    wim->filename);
2222                                 ret = 0;
2223                         }
2224                 } else {
2225                         wim->wim_locked = 1;
2226                 }
2227         }
2228         return ret;
2229 }
2230 #endif
2231
2232 /*
2233  * Perform the intermediate stages of creating a "pipable" WIM (i.e. a WIM
2234  * capable of being applied from a pipe).  Such a WIM looks like:
2235  *
2236  * Pipable WIMs are a wimlib-specific modification of the WIM format such that
2237  * images can be applied from them sequentially when the file data is sent over
2238  * a pipe.  In addition, a pipable WIM can be written sequentially to a pipe.
2239  * The modifications made to the WIM format for pipable WIMs are:
2240  *
2241  * - Magic characters in header are "WLPWM\0\0\0" (wimlib pipable WIM) instead
2242  *   of "MSWIM\0\0\0".  This lets wimlib know that the WIM is pipable and also
2243  *   should stop other software from trying to read the file as a normal WIM.
2244  *
2245  * - The header at the beginning of the file does not contain all the normal
2246  *   information; in particular it will have all 0's for the lookup table and
2247  *   XML data resource entries.  This is because this information cannot be
2248  *   determined until the lookup table and XML data have been written.
2249  *   Consequently, wimlib will write the full header at the very end of the
2250  *   file.  The header at the end, however, is only used when reading the WIM
2251  *   from a seekable file (not a pipe).
2252  *
2253  * - An extra copy of the XML data is placed directly after the header.  This
2254  *   allows image names and sizes to be determined at an appropriate time when
2255  *   reading the WIM from a pipe.  This copy of the XML data is ignored if the
2256  *   WIM is read from a seekable file (not a pipe).
2257  *
2258  * - The format of resources, or streams, has been modified to allow them to be
2259  *   used before the "lookup table" has been read.  Each stream is prefixed with
2260  *   a `struct pwm_stream_hdr' that is basically an abbreviated form of `struct
2261  *   wim_lookup_table_entry_disk' that only contains the SHA1 message digest,
2262  *   uncompressed stream size, and flags that indicate whether the stream is
2263  *   compressed.  The data of uncompressed streams then follows literally, while
2264  *   the data of compressed streams follows in a modified format.  Compressed
2265  *   streams have no chunk table, since the chunk table cannot be written until
2266  *   all chunks have been compressed; instead, each compressed chunk is prefixed
2267  *   by a `struct pwm_chunk_hdr' that gives its size.  However, the offsets are
2268  *   given in the chunk table as if these chunk headers were not present.
2269  *
2270  * - Metadata resources always come before other file resources (streams).
2271  *   (This does not by itself constitute an incompatibility with normal WIMs,
2272  *   since this is valid in normal WIMs.)
2273  *
2274  * - At least up to the end of the file resources, all components must be packed
2275  *   as tightly as possible; there cannot be any "holes" in the WIM.  (This does
2276  *   not by itself consititute an incompatibility with normal WIMs, since this
2277  *   is valid in normal WIMs.)
2278  *
2279  * Note: the lookup table, XML data, and header at the end are not used when
2280  * applying from a pipe.  They exist to support functionality such as image
2281  * application and export when the WIM is *not* read from a pipe.
2282  *
2283  *   Layout of pipable WIM:
2284  *
2285  * ----------+----------+--------------------+----------------+--------------+------------+--------+
2286  * | Header  | XML data | Metadata resources | File resources | Lookup table | XML data   | Header |
2287  * ----------+----------+--------------------+----------------+--------------+------------+--------+
2288  *
2289  *   Layout of normal WIM:
2290  *
2291  * +---------+--------------------+----------------+--------------+----------+
2292  * | Header  | Metadata resources | File resources | Lookup table | XML data |
2293  * +---------+--------------------+----------------+--------------+----------+
2294  *
2295  * Do note that since pipable WIMs are not supported by Microsoft's software,
2296  * wimlib does not create them unless explicitly requested (with
2297  * WIMLIB_WRITE_FLAG_PIPABLE) and as stated above they use different magic
2298  * characters to identify the file.
2299  */
2300 static int
2301 write_pipable_wim(WIMStruct *wim, int image, int write_flags,
2302                   unsigned num_threads, wimlib_progress_func_t progress_func,
2303                   struct list_head *stream_list_override)
2304 {
2305         int ret;
2306         struct resource_entry xml_res_entry;
2307
2308         WARNING("Creating a pipable WIM, which will "
2309                 "be incompatible\n"
2310                 "          with Microsoft's software (wimgapi/imagex/Dism).");
2311
2312         /* At this point, the header at the beginning of the file has already
2313          * been written.  */
2314
2315         /* For efficiency, when wimlib adds an image to the WIM with
2316          * wimlib_add_image(), the SHA1 message digests of files is not
2317          * calculated; instead, they are calculated while the files are being
2318          * written.  However, this does not work when writing a pipable WIM,
2319          * since when writing a stream to a pipable WIM, its SHA1 message digest
2320          * needs to be known before the stream data is written.  Therefore,
2321          * before getting much farther, we need to pre-calculate the SHA1
2322          * message digests of all streams that will be written.  */
2323         ret = wim_checksum_unhashed_streams(wim);
2324         if (ret)
2325                 return ret;
2326
2327         /* Write extra copy of the XML data.  */
2328         ret = write_wim_xml_data(wim, image, WIM_TOTALBYTES_OMIT,
2329                                  &xml_res_entry,
2330                                  WIMLIB_WRITE_RESOURCE_FLAG_PIPABLE);
2331         if (ret)
2332                 return ret;
2333
2334         /* Write metadata resources for the image(s) being included in the
2335          * output WIM.  */
2336         ret = write_wim_metadata_resources(wim, image, write_flags,
2337                                            progress_func);
2338         if (ret)
2339                 return ret;
2340
2341         /* Write streams needed for the image(s) being included in the output
2342          * WIM, or streams needed for the split WIM part.  */
2343         return write_wim_streams(wim, image, write_flags, num_threads,
2344                                  progress_func, stream_list_override);
2345
2346         /* The lookup table, XML data, and header at end are handled by
2347          * finish_write().  */
2348 }
2349
2350 /* Write a standalone WIM or split WIM (SWM) part to a new file or to a file
2351  * descriptor.  */
2352 int
2353 write_wim_part(WIMStruct *wim,
2354                const void *path_or_fd,
2355                int image,
2356                int write_flags,
2357                unsigned num_threads,
2358                wimlib_progress_func_t progress_func,
2359                unsigned part_number,
2360                unsigned total_parts,
2361                struct list_head *stream_list_override,
2362                const u8 *guid)
2363 {
2364         int ret;
2365         struct wim_header hdr_save;
2366         struct list_head lt_stream_list_override;
2367
2368         if (total_parts == 1)
2369                 DEBUG("Writing standalone WIM.");
2370         else
2371                 DEBUG("Writing split WIM part %u/%u", part_number, total_parts);
2372         if (image == WIMLIB_ALL_IMAGES)
2373                 DEBUG("Including all images.");
2374         else
2375                 DEBUG("Including image %d only.", image);
2376         if (write_flags & WIMLIB_WRITE_FLAG_FILE_DESCRIPTOR)
2377                 DEBUG("File descriptor: %d", *(const int*)path_or_fd);
2378         else
2379                 DEBUG("Path: \"%"TS"\"", (const tchar*)path_or_fd);
2380         DEBUG("Write flags: 0x%08x", write_flags);
2381         if (write_flags & WIMLIB_WRITE_FLAG_CHECK_INTEGRITY)
2382                 DEBUG("\tCHECK_INTEGRITY");
2383         if (write_flags & WIMLIB_WRITE_FLAG_REBUILD)
2384                 DEBUG("\tREBUILD");
2385         if (write_flags & WIMLIB_WRITE_FLAG_RECOMPRESS)
2386                 DEBUG("\tRECOMPRESS");
2387         if (write_flags & WIMLIB_WRITE_FLAG_FSYNC)
2388                 DEBUG("\tFSYNC");
2389         if (write_flags & WIMLIB_WRITE_FLAG_SOFT_DELETE)
2390                 DEBUG("\tFSYNC");
2391         if (write_flags & WIMLIB_WRITE_FLAG_IGNORE_READONLY_FLAG)
2392                 DEBUG("\tIGNORE_READONLY_FLAG");
2393         if (write_flags & WIMLIB_WRITE_FLAG_PIPABLE)
2394                 DEBUG("\tPIPABLE");
2395         if (write_flags & WIMLIB_WRITE_FLAG_FILE_DESCRIPTOR)
2396                 DEBUG("\tFILE_DESCRIPTOR");
2397         if (write_flags & WIMLIB_WRITE_FLAG_NO_METADATA)
2398                 DEBUG("\tNO_METADATA");
2399         if (write_flags & WIMLIB_WRITE_FLAG_USE_EXISTING_TOTALBYTES)
2400                 DEBUG("\tUSE_EXISTING_TOTALBYTES");
2401         if (num_threads == 0)
2402                 DEBUG("Number of threads: autodetect");
2403         else
2404                 DEBUG("Number of threads: %u", num_threads);
2405         DEBUG("Progress function: %s", (progress_func ? "yes" : "no"));
2406         DEBUG("Stream list:       %s", (stream_list_override ? "specified" : "autodetect"));
2407         DEBUG("GUID:              %s", (guid ? "specified" : "generate new"));
2408
2409         /* Internally, this is always called with a valid part number and total
2410          * parts.  */
2411         wimlib_assert(total_parts >= 1);
2412         wimlib_assert(part_number >= 1 && part_number <= total_parts);
2413
2414         /* A valid image (or all images) must be specified.  */
2415         if (image != WIMLIB_ALL_IMAGES &&
2416              (image < 1 || image > wim->hdr.image_count))
2417                 return WIMLIB_ERR_INVALID_IMAGE;
2418
2419         /* @wim must specify a standalone WIM, or at least the first part of a
2420          * split WIM.  */
2421         if (wim->hdr.part_number != 1)
2422                 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
2423
2424         /* Check for contradictory flags.  */
2425         if ((write_flags & (WIMLIB_WRITE_FLAG_CHECK_INTEGRITY |
2426                             WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY))
2427                                 == (WIMLIB_WRITE_FLAG_CHECK_INTEGRITY |
2428                                     WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY))
2429                 return WIMLIB_ERR_INVALID_PARAM;
2430
2431         if ((write_flags & (WIMLIB_WRITE_FLAG_PIPABLE |
2432                             WIMLIB_WRITE_FLAG_NOT_PIPABLE))
2433                                 == (WIMLIB_WRITE_FLAG_PIPABLE |
2434                                     WIMLIB_WRITE_FLAG_NOT_PIPABLE))
2435                 return WIMLIB_ERR_INVALID_PARAM;
2436
2437         /* Save previous header, then start initializing the new one.  */
2438         memcpy(&hdr_save, &wim->hdr, sizeof(struct wim_header));
2439
2440         /* Set default integrity and pipable flags.  */
2441         if (!(write_flags & (WIMLIB_WRITE_FLAG_PIPABLE |
2442                              WIMLIB_WRITE_FLAG_NOT_PIPABLE)))
2443                 if (wim_is_pipable(wim))
2444                         write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
2445
2446         if (!(write_flags & (WIMLIB_WRITE_FLAG_CHECK_INTEGRITY |
2447                              WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY)))
2448                 if (wim_has_integrity_table(wim))
2449                         write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2450
2451         /* Set appropriate magic number.  */
2452         if (write_flags & WIMLIB_WRITE_FLAG_PIPABLE)
2453                 wim->hdr.magic = PWM_MAGIC;
2454         else
2455                 wim->hdr.magic = WIM_MAGIC;
2456
2457         /* Clear header flags that will be set automatically.  */
2458         wim->hdr.flags &= ~(WIM_HDR_FLAG_METADATA_ONLY          |
2459                             WIM_HDR_FLAG_RESOURCE_ONLY          |
2460                             WIM_HDR_FLAG_SPANNED                |
2461                             WIM_HDR_FLAG_WRITE_IN_PROGRESS);
2462
2463         /* Set SPANNED header flag if writing part of a split WIM.  */
2464         if (total_parts != 1)
2465                 wim->hdr.flags |= WIM_HDR_FLAG_SPANNED;
2466
2467         /* Set part number and total parts of split WIM.  This will be 1 and 1
2468          * if the WIM is standalone.  */
2469         wim->hdr.part_number = part_number;
2470         wim->hdr.total_parts = total_parts;
2471
2472         /* Use GUID if specified; otherwise generate a new one.  */
2473         if (guid)
2474                 memcpy(wim->hdr.guid, guid, WIMLIB_GUID_LEN);
2475         else
2476                 randomize_byte_array(wim->hdr.guid, WIMLIB_GUID_LEN);
2477
2478         /* Clear references to resources that have not been written yet.  */
2479         zero_resource_entry(&wim->hdr.lookup_table_res_entry);
2480         zero_resource_entry(&wim->hdr.xml_res_entry);
2481         zero_resource_entry(&wim->hdr.boot_metadata_res_entry);
2482         zero_resource_entry(&wim->hdr.integrity);
2483
2484         /* Set image count and boot index correctly for single image writes.  */
2485         if (image != WIMLIB_ALL_IMAGES) {
2486                 wim->hdr.image_count = 1;
2487                 if (wim->hdr.boot_idx == image)
2488                         wim->hdr.boot_idx = 1;
2489                 else
2490                         wim->hdr.boot_idx = 0;
2491         }
2492
2493         /* Split WIMs can't be bootable.  */
2494         if (total_parts != 1)
2495                 wim->hdr.boot_idx = 0;
2496
2497         /* Initialize output file descriptor.  */
2498         if (write_flags & WIMLIB_WRITE_FLAG_FILE_DESCRIPTOR) {
2499                 /* File descriptor was explicitly provided.  Return error if
2500                  * file descriptor is not seekable, unless writing a pipable WIM
2501                  * was requested.  */
2502                 wim->out_fd.fd = *(const int*)path_or_fd;
2503                 wim->out_fd.offset = 0;
2504                 if (!filedes_is_seekable(&wim->out_fd)) {
2505                         ret = WIMLIB_ERR_INVALID_PARAM;
2506                         if (!(write_flags & WIMLIB_WRITE_FLAG_PIPABLE))
2507                                 goto out_restore_hdr;
2508                         if (write_flags & WIMLIB_WRITE_FLAG_CHECK_INTEGRITY) {
2509                                 ERROR("Can't include integrity check when "
2510                                       "writing pipable WIM to pipe!");
2511                                 goto out_restore_hdr;
2512                         }
2513                 }
2514
2515         } else {
2516                 /* Filename of WIM to write was provided; open file descriptor
2517                  * to it.  */
2518                 ret = open_wim_writable(wim, (const tchar*)path_or_fd,
2519                                         O_TRUNC | O_CREAT | O_RDWR);
2520                 if (ret)
2521                         goto out_restore_hdr;
2522         }
2523
2524         /* Write initial header.  This is merely a "dummy" header since it
2525          * doesn't have all the information yet, so it will be overwritten later
2526          * (unless writing a pipable WIM).  */
2527         if (!(write_flags & WIMLIB_WRITE_FLAG_PIPABLE))
2528                 wim->hdr.flags |= WIM_HDR_FLAG_WRITE_IN_PROGRESS;
2529         ret = write_wim_header(&wim->hdr, &wim->out_fd);
2530         wim->hdr.flags &= ~WIM_HDR_FLAG_WRITE_IN_PROGRESS;
2531         if (ret)
2532                 goto out_restore_hdr;
2533
2534         if (stream_list_override) {
2535                 struct wim_lookup_table_entry *lte;
2536                 INIT_LIST_HEAD(&lt_stream_list_override);
2537                 list_for_each_entry(lte, stream_list_override,
2538                                     write_streams_list)
2539                 {
2540                         list_add_tail(&lte->lookup_table_list,
2541                                       &lt_stream_list_override);
2542                 }
2543         }
2544
2545         /* Write metadata resources and streams.  */
2546         if (!(write_flags & WIMLIB_WRITE_FLAG_PIPABLE)) {
2547                 /* Default case: create a normal (non-pipable) WIM.  */
2548                 ret = write_wim_streams(wim, image, write_flags, num_threads,
2549                                         progress_func, stream_list_override);
2550                 if (ret)
2551                         goto out_restore_hdr;
2552
2553                 ret = write_wim_metadata_resources(wim, image, write_flags,
2554                                                    progress_func);
2555                 if (ret)
2556                         goto out_restore_hdr;
2557         } else {
2558                 /* Non-default case: create pipable WIM.  */
2559                 ret = write_pipable_wim(wim, image, write_flags, num_threads,
2560                                         progress_func, stream_list_override);
2561                 if (ret)
2562                         goto out_restore_hdr;
2563                 write_flags |= WIMLIB_WRITE_FLAG_HEADER_AT_END;
2564         }
2565
2566         if (stream_list_override)
2567                 stream_list_override = &lt_stream_list_override;
2568
2569         /* Write lookup table, XML data, and (optional) integrity table.  */
2570         ret = finish_write(wim, image, write_flags, progress_func,
2571                            stream_list_override);
2572 out_restore_hdr:
2573         memcpy(&wim->hdr, &hdr_save, sizeof(struct wim_header));
2574         close_wim_writable(wim, write_flags);
2575         return ret;
2576 }
2577
2578 /* Write a standalone WIM to a file or file descriptor.  */
2579 static int
2580 write_standalone_wim(WIMStruct *wim, const void *path_or_fd,
2581                      int image, int write_flags, unsigned num_threads,
2582                      wimlib_progress_func_t progress_func)
2583 {
2584         return write_wim_part(wim, path_or_fd, image, write_flags,
2585                               num_threads, progress_func, 1, 1, NULL, NULL);
2586 }
2587
2588 /* API function documented in wimlib.h  */
2589 WIMLIBAPI int
2590 wimlib_write(WIMStruct *wim, const tchar *path,
2591              int image, int write_flags, unsigned num_threads,
2592              wimlib_progress_func_t progress_func)
2593 {
2594         if (!path)
2595                 return WIMLIB_ERR_INVALID_PARAM;
2596
2597         write_flags &= WIMLIB_WRITE_MASK_PUBLIC;
2598
2599         return write_standalone_wim(wim, path, image, write_flags,
2600                                     num_threads, progress_func);
2601 }
2602
2603 /* API function documented in wimlib.h  */
2604 WIMLIBAPI int
2605 wimlib_write_to_fd(WIMStruct *wim, int fd,
2606                    int image, int write_flags, unsigned num_threads,
2607                    wimlib_progress_func_t progress_func)
2608 {
2609         if (fd < 0)
2610                 return WIMLIB_ERR_INVALID_PARAM;
2611
2612         write_flags &= WIMLIB_WRITE_MASK_PUBLIC;
2613         write_flags |= WIMLIB_WRITE_FLAG_FILE_DESCRIPTOR;
2614
2615         return write_standalone_wim(wim, &fd, image, write_flags,
2616                                     num_threads, progress_func);
2617 }
2618
2619 static bool
2620 any_images_modified(WIMStruct *wim)
2621 {
2622         for (int i = 0; i < wim->hdr.image_count; i++)
2623                 if (wim->image_metadata[i]->modified)
2624                         return true;
2625         return false;
2626 }
2627
2628 /*
2629  * Overwrite a WIM, possibly appending streams to it.
2630  *
2631  * A WIM looks like (or is supposed to look like) the following:
2632  *
2633  *                   Header (212 bytes)
2634  *                   Streams and metadata resources (variable size)
2635  *                   Lookup table (variable size)
2636  *                   XML data (variable size)
2637  *                   Integrity table (optional) (variable size)
2638  *
2639  * If we are not adding any streams or metadata resources, the lookup table is
2640  * unchanged--- so we only need to overwrite the XML data, integrity table, and
2641  * header.  This operation is potentially unsafe if the program is abruptly
2642  * terminated while the XML data or integrity table are being overwritten, but
2643  * before the new header has been written.  To partially alleviate this problem,
2644  * a special flag (WIMLIB_WRITE_FLAG_CHECKPOINT_AFTER_XML) is passed to
2645  * finish_write() to cause a temporary WIM header to be written after the XML
2646  * data has been written.  This may prevent the WIM from becoming corrupted if
2647  * the program is terminated while the integrity table is being calculated (but
2648  * no guarantees, due to write re-ordering...).
2649  *
2650  * If we are adding new streams or images (metadata resources), the lookup table
2651  * needs to be changed, and those streams need to be written.  In this case, we
2652  * try to perform a safe update of the WIM file by writing the streams *after*
2653  * the end of the previous WIM, then writing the new lookup table, XML data, and
2654  * (optionally) integrity table following the new streams.  This will produce a
2655  * layout like the following:
2656  *
2657  *                   Header (212 bytes)
2658  *                   (OLD) Streams and metadata resources (variable size)
2659  *                   (OLD) Lookup table (variable size)
2660  *                   (OLD) XML data (variable size)
2661  *                   (OLD) Integrity table (optional) (variable size)
2662  *                   (NEW) Streams and metadata resources (variable size)
2663  *                   (NEW) Lookup table (variable size)
2664  *                   (NEW) XML data (variable size)
2665  *                   (NEW) Integrity table (optional) (variable size)
2666  *
2667  * At all points, the WIM is valid as nothing points to the new data yet.  Then,
2668  * the header is overwritten to point to the new lookup table, XML data, and
2669  * integrity table, to produce the following layout:
2670  *
2671  *                   Header (212 bytes)
2672  *                   Streams and metadata resources (variable size)
2673  *                   Nothing (variable size)
2674  *                   More Streams and metadata resources (variable size)
2675  *                   Lookup table (variable size)
2676  *                   XML data (variable size)
2677  *                   Integrity table (optional) (variable size)
2678  *
2679  * This method allows an image to be appended to a large WIM very quickly, and
2680  * is is crash-safe except in the case of write re-ordering, but the
2681  * disadvantage is that a small hole is left in the WIM where the old lookup
2682  * table, xml data, and integrity table were.  (These usually only take up a
2683  * small amount of space compared to the streams, however.)
2684  */
2685 static int
2686 overwrite_wim_inplace(WIMStruct *wim, int write_flags,
2687                       unsigned num_threads,
2688                       wimlib_progress_func_t progress_func)
2689 {
2690         int ret;
2691         struct list_head stream_list;
2692         off_t old_wim_end;
2693         u64 old_lookup_table_end, old_xml_begin, old_xml_end;
2694
2695
2696         DEBUG("Overwriting `%"TS"' in-place", wim->filename);
2697
2698         /* Set default integrity flag.  */
2699         if (!(write_flags & (WIMLIB_WRITE_FLAG_CHECK_INTEGRITY |
2700                              WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY)))
2701                 if (wim_has_integrity_table(wim))
2702                         write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2703
2704         /* Make sure that the integrity table (if present) is after the XML
2705          * data, and that there are no stream resources, metadata resources, or
2706          * lookup tables after the XML data.  Otherwise, these data would be
2707          * overwritten. */
2708         old_xml_begin = wim->hdr.xml_res_entry.offset;
2709         old_xml_end = old_xml_begin + wim->hdr.xml_res_entry.size;
2710         old_lookup_table_end = wim->hdr.lookup_table_res_entry.offset +
2711                                wim->hdr.lookup_table_res_entry.size;
2712         if (wim->hdr.integrity.offset != 0 && wim->hdr.integrity.offset < old_xml_end) {
2713                 ERROR("Didn't expect the integrity table to be before the XML data");
2714                 return WIMLIB_ERR_RESOURCE_ORDER;
2715         }
2716
2717         if (old_lookup_table_end > old_xml_begin) {
2718                 ERROR("Didn't expect the lookup table to be after the XML data");
2719                 return WIMLIB_ERR_RESOURCE_ORDER;
2720         }
2721
2722         /* Set @old_wim_end, which indicates the point beyond which we don't
2723          * allow any file and metadata resources to appear without returning
2724          * WIMLIB_ERR_RESOURCE_ORDER (due to the fact that we would otherwise
2725          * overwrite these resources). */
2726         if (!wim->deletion_occurred && !any_images_modified(wim)) {
2727                 /* If no images have been modified and no images have been
2728                  * deleted, a new lookup table does not need to be written.  We
2729                  * shall write the new XML data and optional integrity table
2730                  * immediately after the lookup table.  Note that this may
2731                  * overwrite an existing integrity table. */
2732                 DEBUG("Skipping writing lookup table "
2733                       "(no images modified or deleted)");
2734                 old_wim_end = old_lookup_table_end;
2735                 write_flags |= WIMLIB_WRITE_FLAG_NO_LOOKUP_TABLE |
2736                                WIMLIB_WRITE_FLAG_CHECKPOINT_AFTER_XML;
2737         } else if (wim->hdr.integrity.offset) {
2738                 /* Old WIM has an integrity table; begin writing new streams
2739                  * after it. */
2740                 old_wim_end = wim->hdr.integrity.offset + wim->hdr.integrity.size;
2741         } else {
2742                 /* No existing integrity table; begin writing new streams after
2743                  * the old XML data. */
2744                 old_wim_end = old_xml_end;
2745         }
2746
2747         ret = prepare_streams_for_overwrite(wim, old_wim_end, &stream_list);
2748         if (ret)
2749                 return ret;
2750
2751         ret = open_wim_writable(wim, wim->filename, O_RDWR);
2752         if (ret)
2753                 return ret;
2754
2755         ret = lock_wim(wim, wim->out_fd.fd);
2756         if (ret) {
2757                 close_wim_writable(wim, write_flags);
2758                 return ret;
2759         }
2760
2761         /* Set WIM_HDR_FLAG_WRITE_IN_PROGRESS flag in header. */
2762         ret = write_wim_header_flags(wim->hdr.flags | WIM_HDR_FLAG_WRITE_IN_PROGRESS,
2763                                      &wim->out_fd);
2764         if (ret) {
2765                 ERROR_WITH_ERRNO("Error updating WIM header flags");
2766                 close_wim_writable(wim, write_flags);
2767                 goto out_unlock_wim;
2768         }
2769
2770         if (filedes_seek(&wim->out_fd, old_wim_end) == -1) {
2771                 ERROR_WITH_ERRNO("Can't seek to end of WIM");
2772                 close_wim_writable(wim, write_flags);
2773                 ret = WIMLIB_ERR_WRITE;
2774                 goto out_unlock_wim;
2775         }
2776
2777         DEBUG("Writing newly added streams (offset = %"PRIu64")",
2778               old_wim_end);
2779         ret = write_stream_list(&stream_list,
2780                                 wim->lookup_table,
2781                                 &wim->out_fd,
2782                                 wim->compression_type,
2783                                 write_flags,
2784                                 num_threads,
2785                                 progress_func);
2786         if (ret)
2787                 goto out_truncate;
2788
2789         for (unsigned i = 1; i <= wim->hdr.image_count; i++) {
2790                 if (wim->image_metadata[i - 1]->modified) {
2791                         ret = write_metadata_resource(wim, i, 0);
2792                         if (ret)
2793                                 goto out_truncate;
2794                 }
2795         }
2796         write_flags |= WIMLIB_WRITE_FLAG_REUSE_INTEGRITY_TABLE;
2797         ret = finish_write(wim, WIMLIB_ALL_IMAGES, write_flags,
2798                            progress_func, NULL);
2799 out_truncate:
2800         close_wim_writable(wim, write_flags);
2801         if (ret && !(write_flags & WIMLIB_WRITE_FLAG_NO_LOOKUP_TABLE)) {
2802                 WARNING("Truncating `%"TS"' to its original size (%"PRIu64" bytes)",
2803                         wim->filename, old_wim_end);
2804                 /* Return value of truncate() is ignored because this is already
2805                  * an error path. */
2806                 (void)ttruncate(wim->filename, old_wim_end);
2807         }
2808 out_unlock_wim:
2809         wim->wim_locked = 0;
2810         return ret;
2811 }
2812
2813 static int
2814 overwrite_wim_via_tmpfile(WIMStruct *wim, int write_flags,
2815                           unsigned num_threads,
2816                           wimlib_progress_func_t progress_func)
2817 {
2818         size_t wim_name_len;
2819         int ret;
2820
2821         DEBUG("Overwriting `%"TS"' via a temporary file", wim->filename);
2822
2823         /* Write the WIM to a temporary file in the same directory as the
2824          * original WIM. */
2825         wim_name_len = tstrlen(wim->filename);
2826         tchar tmpfile[wim_name_len + 10];
2827         tmemcpy(tmpfile, wim->filename, wim_name_len);
2828         randomize_char_array_with_alnum(tmpfile + wim_name_len, 9);
2829         tmpfile[wim_name_len + 9] = T('\0');
2830
2831         ret = wimlib_write(wim, tmpfile, WIMLIB_ALL_IMAGES,
2832                            write_flags | WIMLIB_WRITE_FLAG_FSYNC,
2833                            num_threads, progress_func);
2834         if (ret)
2835                 goto out_unlink;
2836
2837         close_wim(wim);
2838
2839         DEBUG("Renaming `%"TS"' to `%"TS"'", tmpfile, wim->filename);
2840         /* Rename the new file to the old file .*/
2841         if (trename(tmpfile, wim->filename) != 0) {
2842                 ERROR_WITH_ERRNO("Failed to rename `%"TS"' to `%"TS"'",
2843                                  tmpfile, wim->filename);
2844                 ret = WIMLIB_ERR_RENAME;
2845                 goto out_unlink;
2846         }
2847
2848         if (progress_func) {
2849                 union wimlib_progress_info progress;
2850                 progress.rename.from = tmpfile;
2851                 progress.rename.to = wim->filename;
2852                 progress_func(WIMLIB_PROGRESS_MSG_RENAME, &progress);
2853         }
2854         return 0;
2855
2856 out_unlink:
2857         /* Remove temporary file. */
2858         tunlink(tmpfile);
2859         return ret;
2860 }
2861
2862 /* API function documented in wimlib.h  */
2863 WIMLIBAPI int
2864 wimlib_overwrite(WIMStruct *wim, int write_flags,
2865                  unsigned num_threads,
2866                  wimlib_progress_func_t progress_func)
2867 {
2868         int ret;
2869         u32 orig_hdr_flags;
2870
2871         write_flags &= WIMLIB_WRITE_MASK_PUBLIC;
2872
2873         if (write_flags & WIMLIB_WRITE_FLAG_FILE_DESCRIPTOR)
2874                 return WIMLIB_ERR_INVALID_PARAM;
2875
2876         if (!wim->filename)
2877                 return WIMLIB_ERR_NO_FILENAME;
2878
2879         orig_hdr_flags = wim->hdr.flags;
2880         if (write_flags & WIMLIB_WRITE_FLAG_IGNORE_READONLY_FLAG)
2881                 wim->hdr.flags &= ~WIM_HDR_FLAG_READONLY;
2882         ret = can_modify_wim(wim);
2883         wim->hdr.flags = orig_hdr_flags;
2884         if (ret)
2885                 return ret;
2886
2887         if ((!wim->deletion_occurred || (write_flags & WIMLIB_WRITE_FLAG_SOFT_DELETE))
2888             && !(write_flags & (WIMLIB_WRITE_FLAG_REBUILD |
2889                                 WIMLIB_WRITE_FLAG_PIPABLE))
2890             && !(wim_is_pipable(wim)))
2891         {
2892                 ret = overwrite_wim_inplace(wim, write_flags, num_threads,
2893                                             progress_func);
2894                 if (ret != WIMLIB_ERR_RESOURCE_ORDER)
2895                         return ret;
2896                 WARNING("Falling back to re-building entire WIM");
2897         }
2898         return overwrite_wim_via_tmpfile(wim, write_flags, num_threads,
2899                                          progress_func);
2900 }