4 * Support for writing WIM files; write a WIM file, overwrite a WIM file, write
5 * compressed file resources, etc.
9 * Copyright (C) 2012, 2013 Eric Biggers
11 * This file is part of wimlib, a library for working with WIM files.
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)
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
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/.
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>
37 #include "wimlib/compress_chunks.h"
38 #include "wimlib/endianness.h"
39 #include "wimlib/error.h"
40 #include "wimlib/file_io.h"
41 #include "wimlib/header.h"
42 #include "wimlib/integrity.h"
43 #include "wimlib/lookup_table.h"
44 #include "wimlib/metadata.h"
45 #include "wimlib/resource.h"
46 #include "wimlib/win32.h"
47 #include "wimlib/write.h"
48 #include "wimlib/xml.h"
60 write_flags_to_resource_flags(int write_flags)
62 int write_resource_flags = 0;
64 if (write_flags & WIMLIB_WRITE_FLAG_RECOMPRESS)
65 write_resource_flags |= WIMLIB_WRITE_RESOURCE_FLAG_RECOMPRESS;
66 if (write_flags & WIMLIB_WRITE_FLAG_PIPABLE)
67 write_resource_flags |= WIMLIB_WRITE_RESOURCE_FLAG_PIPABLE;
68 if (write_flags & WIMLIB_WRITE_FLAG_PACK_STREAMS)
69 write_resource_flags |= WIMLIB_WRITE_RESOURCE_FLAG_PACK_STREAMS;
70 return write_resource_flags;
73 struct filter_context {
78 /* Determine specified stream should be filtered out from the write.
82 * < 0 : The stream should be hard-filtered; that is, not included in the
84 * 0 : The stream should not be filtered out.
85 * > 0 : The stream should be soft-filtered; that is, it already exists in the
86 * WIM file and may not need to be written again.
89 stream_filtered(const struct wim_lookup_table_entry *lte,
90 const struct filter_context *ctx)
92 int write_flags = ctx->write_flags;
93 WIMStruct *wim = ctx->wim;
98 if (write_flags & WIMLIB_WRITE_FLAG_OVERWRITE &&
99 lte->resource_location == RESOURCE_IN_WIM &&
100 lte->rspec->wim == wim)
103 if (write_flags & WIMLIB_WRITE_FLAG_SKIP_EXTERNAL_WIMS &&
104 lte->resource_location == RESOURCE_IN_WIM &&
105 lte->rspec->wim != wim)
112 stream_hard_filtered(const struct wim_lookup_table_entry *lte,
113 struct filter_context *ctx)
115 return stream_filtered(lte, ctx) < 0;
119 may_soft_filter_streams(const struct filter_context *ctx)
123 return ctx->write_flags & WIMLIB_WRITE_FLAG_OVERWRITE;
127 may_hard_filter_streams(const struct filter_context *ctx)
131 return ctx->write_flags & WIMLIB_WRITE_FLAG_SKIP_EXTERNAL_WIMS;
135 may_filter_streams(const struct filter_context *ctx)
137 return (may_soft_filter_streams(ctx) ||
138 may_hard_filter_streams(ctx));
142 /* Return true if the specified resource is compressed and the compressed data
143 * can be reused with the specified output parameters. */
145 can_raw_copy(const struct wim_lookup_table_entry *lte,
146 int write_resource_flags, int out_ctype, u32 out_chunk_size)
148 const struct wim_resource_spec *rspec;
150 if (write_resource_flags & WIMLIB_WRITE_RESOURCE_FLAG_RECOMPRESS)
153 if (out_ctype == WIMLIB_COMPRESSION_TYPE_NONE)
156 if (lte->resource_location != RESOURCE_IN_WIM)
161 if (rspec->is_pipable != !!(write_resource_flags & WIMLIB_WRITE_RESOURCE_FLAG_PIPABLE))
165 if (rspec->flags & WIM_RESHDR_FLAG_COMPRESSED) {
166 /* Normal compressed resource: Must use same compression type
168 return (rspec->wim->compression_type == out_ctype &&
169 rspec->wim->chunk_size == out_chunk_size);
172 if ((rspec->flags & WIM_RESHDR_FLAG_PACKED_STREAMS) &&
173 (write_resource_flags & WIMLIB_WRITE_RESOURCE_FLAG_PACK_STREAMS))
175 /* Packed resource: Such resources may contain multiple streams,
176 * and in general only a subset of them need to be written. As
177 * a heuristic, re-use the raw data if at least half the
178 * uncompressed size is being written. */
180 /* Note: packed resources contain a header that specifies the
181 * compression type and chunk size; therefore we don't need to
182 * check if they are compatible with @out_ctype and
183 * @out_chunk_size. */
185 struct wim_lookup_table_entry *res_stream;
188 list_for_each_entry(res_stream, &rspec->stream_list, rspec_node)
189 if (res_stream->will_be_in_output_wim)
190 write_size += res_stream->size;
192 return (write_size > rspec->uncompressed_size / 2);
198 filter_resource_flags(u8 flags)
200 return (flags & ~(WIM_RESHDR_FLAG_PACKED_STREAMS |
201 WIM_RESHDR_FLAG_COMPRESSED |
202 WIM_RESHDR_FLAG_SPANNED |
203 WIM_RESHDR_FLAG_FREE));
207 stream_set_out_reshdr_for_reuse(struct wim_lookup_table_entry *lte)
209 const struct wim_resource_spec *rspec;
211 wimlib_assert(lte->resource_location == RESOURCE_IN_WIM);
214 if (rspec->flags & WIM_RESHDR_FLAG_PACKED_STREAMS) {
216 wimlib_assert(lte->flags & WIM_RESHDR_FLAG_PACKED_STREAMS);
218 lte->out_reshdr.offset_in_wim = lte->offset_in_res;
219 lte->out_reshdr.uncompressed_size = 0;
220 lte->out_reshdr.size_in_wim = lte->size;
222 lte->out_res_offset_in_wim = rspec->offset_in_wim;
223 lte->out_res_size_in_wim = rspec->size_in_wim;
224 /*lte->out_res_uncompressed_size = rspec->uncompressed_size;*/
226 wimlib_assert(!(lte->flags & WIM_RESHDR_FLAG_PACKED_STREAMS));
228 lte->out_reshdr.offset_in_wim = rspec->offset_in_wim;
229 lte->out_reshdr.uncompressed_size = rspec->uncompressed_size;
230 lte->out_reshdr.size_in_wim = rspec->size_in_wim;
232 lte->out_reshdr.flags = lte->flags;
236 /* Write the header for a stream in a pipable WIM. */
238 write_pwm_stream_header(const struct wim_lookup_table_entry *lte,
239 struct filedes *out_fd,
240 int additional_reshdr_flags)
242 struct pwm_stream_hdr stream_hdr;
246 stream_hdr.magic = cpu_to_le64(PWM_STREAM_MAGIC);
247 stream_hdr.uncompressed_size = cpu_to_le64(lte->size);
248 if (additional_reshdr_flags & PWM_RESHDR_FLAG_UNHASHED) {
249 zero_out_hash(stream_hdr.hash);
251 wimlib_assert(!lte->unhashed);
252 copy_hash(stream_hdr.hash, lte->hash);
255 reshdr_flags = filter_resource_flags(lte->flags);
256 reshdr_flags |= additional_reshdr_flags;
257 stream_hdr.flags = cpu_to_le32(reshdr_flags);
258 ret = full_write(out_fd, &stream_hdr, sizeof(stream_hdr));
260 ERROR_WITH_ERRNO("Write error");
266 seek_and_truncate(struct filedes *out_fd, off_t offset)
268 if (filedes_seek(out_fd, offset) == -1 ||
269 ftruncate(out_fd->fd, offset))
271 ERROR_WITH_ERRNO("Failed to truncate output WIM file");
272 return WIMLIB_ERR_WRITE;
278 struct write_streams_progress_data {
279 wimlib_progress_func_t progress_func;
280 union wimlib_progress_info progress;
281 uint64_t next_progress;
282 WIMStruct *prev_wim_part;
286 do_write_streams_progress(struct write_streams_progress_data *progress_data,
287 struct wim_lookup_table_entry *lte,
288 bool stream_discarded)
290 union wimlib_progress_info *progress = &progress_data->progress;
293 if (stream_discarded) {
294 progress->write_streams.total_bytes -= lte->size;
295 if (progress_data->next_progress != ~(uint64_t)0 &&
296 progress_data->next_progress > progress->write_streams.total_bytes)
298 progress_data->next_progress = progress->write_streams.total_bytes;
301 progress->write_streams.completed_bytes += lte->size;
303 new_wim_part = false;
304 if (lte->resource_location == RESOURCE_IN_WIM &&
305 lte->rspec->wim != progress_data->prev_wim_part)
307 if (progress_data->prev_wim_part) {
309 progress->write_streams.completed_parts++;
311 progress_data->prev_wim_part = lte->rspec->wim;
313 progress->write_streams.completed_streams++;
314 if (progress_data->progress_func
315 && (progress->write_streams.completed_bytes >= progress_data->next_progress
318 progress_data->progress_func(WIMLIB_PROGRESS_MSG_WRITE_STREAMS,
320 if (progress_data->next_progress == progress->write_streams.total_bytes) {
321 progress_data->next_progress = ~(uint64_t)0;
323 progress_data->next_progress =
324 min(progress->write_streams.total_bytes,
325 progress->write_streams.completed_bytes +
326 progress->write_streams.total_bytes / 100);
331 struct write_streams_ctx {
332 /* File descriptor the streams are being written to. */
333 struct filedes *out_fd;
335 /* Lookup table for the WIMStruct on whose behalf the streams are being
337 struct wim_lookup_table *lookup_table;
339 /* Compression format to use. */
342 /* Maximum uncompressed chunk size in compressed resources to use. */
345 /* Flags that affect how the streams will be written. */
346 int write_resource_flags;
348 /* Data used for issuing WRITE_STREAMS progress. */
349 struct write_streams_progress_data progress_data;
351 struct filter_context *filter_ctx;
353 /* Upper bound on the total number of bytes that need to be compressed.
355 u64 num_bytes_to_compress;
357 /* Pointer to the chunk_compressor implementation being used for
358 * compressing chunks of data, or NULL if chunks are being written
360 struct chunk_compressor *compressor;
362 /* Buffer for dividing the read data into chunks of size
363 * @out_chunk_size. */
366 /* Number of bytes in @chunk_buf that are currently filled. */
367 size_t chunk_buf_filled;
369 /* List of streams that currently have chunks being compressed. */
370 struct list_head pending_streams;
372 /* Set to true if the stream currently being read was a duplicate, and
373 * therefore the corresponding stream entry needs to be freed once the
374 * read finishes. (In this case we add the duplicate entry to
375 * pending_streams rather than the entry being read.) */
376 bool stream_was_duplicate;
378 /* Current uncompressed offset in the resource being read. */
379 u64 cur_read_res_offset;
381 /* Uncompressed size of the resource currently being read. */
382 u64 cur_read_res_size;
384 /* Current uncompressed offset in the resource being written. */
385 u64 cur_write_res_offset;
387 /* Uncompressed size of resource currently being written. */
388 u64 cur_write_res_size;
390 /* Array that is filled in with compressed chunk sizes as a resource is
394 /* Index of next entry in @chunk_csizes to fill in. */
397 /* Number of entries in @chunk_csizes currently allocated. */
398 size_t num_alloc_chunks;
400 /* Offset in the output file of the start of the chunks of the resource
401 * currently being written. */
402 u64 chunks_start_offset;
406 get_chunk_entry_size(u64 res_size, int write_resource_flags)
408 if (res_size <= UINT32_MAX ||
409 (write_resource_flags & WIM_RESHDR_FLAG_PACKED_STREAMS))
415 /* Reserve space for the chunk table and prepare to accumulate the chunk table
418 begin_chunk_table(struct write_streams_ctx *ctx, u64 res_expected_size)
420 u64 expected_num_chunks;
421 u64 expected_num_chunk_entries;
425 /* Calculate the number of chunks and chunk entries that should be
426 * needed for the resource. These normally will be the final values,
427 * but in PACKED_STREAMS mode some of the streams we're planning to
428 * write into the resource may be duplicates, and therefore discarded,
429 * potentially decreasing the number of chunk entries needed. */
430 expected_num_chunks = DIV_ROUND_UP(res_expected_size, ctx->out_chunk_size);
431 expected_num_chunk_entries = expected_num_chunks;
432 if (!(ctx->write_resource_flags & WIMLIB_WRITE_RESOURCE_FLAG_PACK_STREAMS))
433 expected_num_chunk_entries--;
435 /* Make sure the chunk_csizes array is long enough to store the
436 * compressed size of each chunk. */
437 if (expected_num_chunks > ctx->num_alloc_chunks) {
438 u64 new_length = expected_num_chunks + 50;
440 if ((size_t)new_length != new_length) {
441 ERROR("Resource size too large (%"PRIu64" bytes!",
443 return WIMLIB_ERR_NOMEM;
446 FREE(ctx->chunk_csizes);
447 ctx->chunk_csizes = MALLOC(new_length * sizeof(ctx->chunk_csizes[0]));
448 if (ctx->chunk_csizes == NULL) {
449 ctx->num_alloc_chunks = 0;
450 return WIMLIB_ERR_NOMEM;
452 ctx->num_alloc_chunks = new_length;
455 ctx->chunk_index = 0;
457 if (!(ctx->write_resource_flags & WIMLIB_WRITE_RESOURCE_FLAG_PIPABLE)) {
458 /* Reserve space for the chunk table in the output file. In the
459 * case of packed resources this reserves the upper bound for
460 * the needed space, not necessarily the exact space which will
461 * prove to be needed. At this point, we just use @chunk_csizes
462 * for a buffer of 0's because the actual compressed chunk sizes
464 reserve_size = expected_num_chunk_entries *
465 get_chunk_entry_size(res_expected_size,
466 ctx->write_resource_flags);
467 if (ctx->write_resource_flags & WIMLIB_WRITE_RESOURCE_FLAG_PACK_STREAMS)
468 reserve_size += sizeof(struct alt_chunk_table_header_disk);
469 memset(ctx->chunk_csizes, 0, reserve_size);
470 ret = full_write(ctx->out_fd, ctx->chunk_csizes, reserve_size);
478 begin_write_resource(struct write_streams_ctx *ctx, u64 res_expected_size)
482 wimlib_assert(res_expected_size != 0);
484 if (ctx->compressor != NULL) {
485 ret = begin_chunk_table(ctx, res_expected_size);
490 /* Output file descriptor is now positioned at the offset at which to
491 * write the first chunk of the resource. */
492 ctx->chunks_start_offset = ctx->out_fd->offset;
493 ctx->cur_write_res_offset = 0;
494 ctx->cur_write_res_size = res_expected_size;
499 end_chunk_table(struct write_streams_ctx *ctx, u64 res_actual_size,
500 u64 *res_start_offset_ret, u64 *res_store_size_ret)
502 size_t actual_num_chunks;
503 size_t actual_num_chunk_entries;
504 size_t chunk_entry_size;
507 actual_num_chunks = ctx->chunk_index;
508 actual_num_chunk_entries = actual_num_chunks;
509 if (!(ctx->write_resource_flags & WIMLIB_WRITE_RESOURCE_FLAG_PACK_STREAMS))
510 actual_num_chunk_entries--;
512 chunk_entry_size = get_chunk_entry_size(res_actual_size,
513 ctx->write_resource_flags);
515 typedef le64 __attribute__((may_alias)) aliased_le64_t;
516 typedef le32 __attribute__((may_alias)) aliased_le32_t;
518 if (chunk_entry_size == 4) {
519 aliased_le32_t *entries = (aliased_le32_t*)ctx->chunk_csizes;
521 if (ctx->write_resource_flags & WIMLIB_WRITE_RESOURCE_FLAG_PACK_STREAMS) {
522 for (size_t i = 0; i < actual_num_chunk_entries; i++)
523 entries[i] = cpu_to_le32(ctx->chunk_csizes[i]);
525 u32 offset = ctx->chunk_csizes[0];
526 for (size_t i = 0; i < actual_num_chunk_entries; i++) {
527 u32 next_size = ctx->chunk_csizes[i + 1];
528 entries[i] = cpu_to_le32(offset);
533 aliased_le64_t *entries = (aliased_le64_t*)ctx->chunk_csizes;
535 if (ctx->write_resource_flags & WIMLIB_WRITE_RESOURCE_FLAG_PACK_STREAMS) {
536 for (size_t i = 0; i < actual_num_chunk_entries; i++)
537 entries[i] = cpu_to_le64(ctx->chunk_csizes[i]);
539 u64 offset = ctx->chunk_csizes[0];
540 for (size_t i = 0; i < actual_num_chunk_entries; i++) {
541 u64 next_size = ctx->chunk_csizes[i + 1];
542 entries[i] = cpu_to_le64(offset);
548 size_t chunk_table_size = actual_num_chunk_entries * chunk_entry_size;
549 u64 res_start_offset;
550 u64 res_end_offset = ctx->out_fd->offset;
552 if (ctx->write_resource_flags & WIMLIB_WRITE_RESOURCE_FLAG_PIPABLE) {
553 ret = full_write(ctx->out_fd, ctx->chunk_csizes, chunk_table_size);
556 res_end_offset = ctx->out_fd->offset;
557 res_start_offset = ctx->chunks_start_offset;
559 res_end_offset = ctx->out_fd->offset;
561 u64 chunk_table_offset;
563 chunk_table_offset = ctx->chunks_start_offset - chunk_table_size;
565 if (ctx->write_resource_flags & WIMLIB_WRITE_RESOURCE_FLAG_PACK_STREAMS) {
566 struct alt_chunk_table_header_disk hdr;
568 hdr.res_usize = cpu_to_le64(res_actual_size);
569 hdr.chunk_size = cpu_to_le32(ctx->out_chunk_size);
570 hdr.compression_format = cpu_to_le32(ctx->out_ctype);
572 BUILD_BUG_ON(WIMLIB_COMPRESSION_TYPE_LZX != 1);
573 BUILD_BUG_ON(WIMLIB_COMPRESSION_TYPE_XPRESS != 2);
574 BUILD_BUG_ON(WIMLIB_COMPRESSION_TYPE_LZMS != 3);
576 ret = full_pwrite(ctx->out_fd, &hdr, sizeof(hdr),
577 chunk_table_offset - sizeof(hdr));
580 res_start_offset = chunk_table_offset - sizeof(hdr);
582 res_start_offset = chunk_table_offset;
585 ret = full_pwrite(ctx->out_fd, ctx->chunk_csizes,
586 chunk_table_size, chunk_table_offset);
591 *res_start_offset_ret = res_start_offset;
592 *res_store_size_ret = res_end_offset - res_start_offset;
597 ERROR_WITH_ERRNO("Write error");
601 /* Finish writing a WIM resource by writing or updating the chunk table (if not
602 * writing the data uncompressed) and loading its metadata into @out_reshdr. */
604 end_write_resource(struct write_streams_ctx *ctx, struct wim_reshdr *out_reshdr)
608 u64 res_uncompressed_size;
609 u64 res_offset_in_wim;
611 wimlib_assert(ctx->cur_write_res_size == ctx->cur_write_res_offset);
612 res_uncompressed_size = ctx->cur_write_res_size;
614 if (ctx->compressor) {
615 ret = end_chunk_table(ctx, res_uncompressed_size,
616 &res_offset_in_wim, &res_size_in_wim);
620 res_offset_in_wim = ctx->chunks_start_offset;
621 res_size_in_wim = ctx->out_fd->offset - res_offset_in_wim;
623 out_reshdr->uncompressed_size = res_uncompressed_size;
624 out_reshdr->size_in_wim = res_size_in_wim;
625 out_reshdr->offset_in_wim = res_offset_in_wim;
626 DEBUG("Finished writing resource: %"PRIu64" => %"PRIu64" @ %"PRIu64"",
627 res_uncompressed_size, res_size_in_wim, res_offset_in_wim);
631 /* Begin processing a stream for writing. */
633 write_stream_begin_read(struct wim_lookup_table_entry *lte,
634 bool is_partial_res, void *_ctx)
636 struct write_streams_ctx *ctx = _ctx;
639 wimlib_assert(lte->size > 0);
641 ctx->cur_read_res_offset = 0;
642 ctx->cur_read_res_size = lte->size;
644 /* As an optimization, we allow some streams to be "unhashed", meaning
645 * their SHA1 message digests are unknown. This is the case with
646 * streams that are added by scanning a directry tree with
647 * wimlib_add_image(), for example. Since WIM uses single-instance
648 * streams, we don't know whether such each such stream really need to
649 * written until it is actually checksummed, unless it has a unique
650 * size. In such cases we read and checksum the stream in this
651 * function, thereby advancing ahead of read_stream_list(), which will
652 * still provide the data again to write_stream_process_chunk(). This
653 * is okay because an unhashed stream cannot be in a WIM resource, which
654 * might be costly to decompress. */
655 ctx->stream_was_duplicate = false;
656 if (ctx->lookup_table != NULL && lte->unhashed && !lte->unique_size) {
658 wimlib_assert(!is_partial_res);
659 wimlib_assert(ctx->lookup_table != NULL);
661 struct wim_lookup_table_entry *lte_new;
663 ret = hash_unhashed_stream(lte, ctx->lookup_table, <e_new);
666 if (lte_new != lte) {
667 /* Duplicate stream detected. */
669 if (lte_new->will_be_in_output_wim ||
670 stream_filtered(lte_new, ctx->filter_ctx))
672 /* The duplicate stream is already being
673 * included in the output WIM, or it would be
674 * filtered out if it had been. Skip writing
675 * this stream (and reading it again) entirely,
676 * passing its output reference count to the
677 * duplicate stream in the former case. */
678 DEBUG("Discarding duplicate stream of "
679 "length %"PRIu64, lte->size);
680 do_write_streams_progress(&ctx->progress_data,
682 list_del(<e->write_streams_list);
683 list_del(<e->lookup_table_list);
684 if (lte_new->will_be_in_output_wim)
685 lte_new->out_refcnt += lte->out_refcnt;
686 if (ctx->write_resource_flags & WIMLIB_WRITE_RESOURCE_FLAG_PACK_STREAMS)
687 ctx->cur_write_res_size -= lte->size;
688 free_lookup_table_entry(lte);
689 return BEGIN_STREAM_STATUS_SKIP_STREAM;
691 /* The duplicate stream can validly be written,
692 * but was not marked as such. Discard the
693 * current stream entry and use the duplicate,
694 * but actually freeing the current entry must
695 * wait until read_stream_list() has finished
696 * reading its data. */
697 DEBUG("Stream duplicate, but not already "
698 "selected for writing.");
699 list_replace(<e->write_streams_list,
700 <e_new->write_streams_list);
701 list_replace(<e->lookup_table_list,
702 <e_new->lookup_table_list);
703 lte_new->out_refcnt = lte->out_refcnt;
704 lte_new->will_be_in_output_wim = 1;
705 ctx->stream_was_duplicate = true;
710 list_move_tail(<e->write_streams_list, &ctx->pending_streams);
714 /* Write the next chunk of (typically compressed) data to the output WIM,
715 * handling the writing of the chunk table. */
717 write_chunk(struct write_streams_ctx *ctx, const void *cchunk,
718 size_t csize, size_t usize)
722 if (ctx->cur_write_res_offset == 0 &&
723 !(ctx->write_resource_flags & WIMLIB_WRITE_RESOURCE_FLAG_PACK_STREAMS))
725 /* Starting to write a new stream in non-packed mode. */
727 struct wim_lookup_table_entry *lte;
729 lte = list_entry(ctx->pending_streams.next,
730 struct wim_lookup_table_entry, write_streams_list);
732 if (ctx->write_resource_flags & WIMLIB_WRITE_RESOURCE_FLAG_PIPABLE) {
733 int additional_reshdr_flags = 0;
734 if (ctx->compressor != NULL)
735 additional_reshdr_flags |= WIM_RESHDR_FLAG_COMPRESSED;
737 DEBUG("Writing pipable WIM stream header "
738 "(offset=%"PRIu64")", ctx->out_fd->offset);
740 ret = write_pwm_stream_header(lte, ctx->out_fd,
741 additional_reshdr_flags);
746 ret = begin_write_resource(ctx, lte->size);
751 if (ctx->compressor != NULL) {
752 /* Record the compresed chunk size. */
753 wimlib_assert(ctx->chunk_index < ctx->num_alloc_chunks);
754 ctx->chunk_csizes[ctx->chunk_index++] = csize;
756 /* If writing a pipable WIM, before the chunk data write a chunk
757 * header that provides the compressed chunk size. */
758 if (ctx->write_resource_flags & WIMLIB_WRITE_RESOURCE_FLAG_PIPABLE) {
759 struct pwm_chunk_hdr chunk_hdr = {
760 .compressed_size = cpu_to_le32(csize),
762 ret = full_write(ctx->out_fd, &chunk_hdr,
769 /* Write the chunk data. */
770 ret = full_write(ctx->out_fd, cchunk, csize);
774 ctx->cur_write_res_offset += usize;
776 if (ctx->cur_write_res_offset == ctx->cur_write_res_size &&
777 !(ctx->write_resource_flags & WIMLIB_WRITE_RESOURCE_FLAG_PACK_STREAMS))
779 struct wim_lookup_table_entry *lte;
781 lte = list_entry(ctx->pending_streams.next,
782 struct wim_lookup_table_entry, write_streams_list);
783 wimlib_assert(ctx->cur_write_res_offset == lte->size);
785 /* Finished writing a stream in non-packed mode. */
787 do_write_streams_progress(&ctx->progress_data, lte, false);
789 ret = end_write_resource(ctx, <e->out_reshdr);
793 wimlib_assert(lte->out_reshdr.uncompressed_size == lte->size);
795 lte->out_reshdr.flags = filter_resource_flags(lte->flags);
796 if (ctx->compressor != NULL)
797 lte->out_reshdr.flags |= WIM_RESHDR_FLAG_COMPRESSED;
799 list_del(<e->write_streams_list);
800 ctx->cur_write_res_offset = 0;
806 ERROR_WITH_ERRNO("Write error");
811 submit_chunk_for_compression(struct write_streams_ctx *ctx,
812 const void *chunk, size_t size)
814 /* While we are unable to submit the chunk for compression (due to too
815 * many chunks already outstanding), retrieve and write the next
816 * compressed chunk. */
817 while (!ctx->compressor->submit_chunk(ctx->compressor, chunk, size)) {
824 bret = ctx->compressor->get_chunk(ctx->compressor,
825 &cchunk, &csize, &usize);
829 ret = write_chunk(ctx, cchunk, csize, usize);
836 /* Process the next chunk of data to be written to a WIM resource. */
838 write_stream_process_chunk(const void *chunk, size_t size, void *_ctx)
840 struct write_streams_ctx *ctx = _ctx;
842 const u8 *chunkptr, *chunkend;
844 wimlib_assert(size != 0);
846 if (ctx->compressor == NULL) {
847 /* Write chunk uncompressed. */
848 ret = write_chunk(ctx, chunk, size, size);
851 ctx->cur_read_res_offset += size;
855 /* Submit the chunk for compression, but take into account that the
856 * @size the chunk was provided in may not correspond to the
857 * @out_chunk_size being used for compression. */
859 chunkend = chunkptr + size;
861 const u8 *resized_chunk;
862 size_t needed_chunk_size;
864 if (ctx->write_resource_flags & WIMLIB_WRITE_RESOURCE_FLAG_PACK_STREAMS) {
865 needed_chunk_size = ctx->out_chunk_size;
867 u64 res_bytes_remaining;
869 res_bytes_remaining = ctx->cur_read_res_size -
870 ctx->cur_read_res_offset;
871 needed_chunk_size = min(ctx->out_chunk_size,
872 ctx->chunk_buf_filled +
873 res_bytes_remaining);
876 if (ctx->chunk_buf_filled == 0 &&
877 chunkend - chunkptr >= needed_chunk_size)
879 /* No intermediate buffering needed. */
880 resized_chunk = chunkptr;
881 chunkptr += needed_chunk_size;
882 ctx->cur_read_res_offset += needed_chunk_size;
884 /* Intermediate buffering needed. */
885 size_t bytes_consumed;
887 bytes_consumed = min(chunkend - chunkptr,
888 needed_chunk_size - ctx->chunk_buf_filled);
890 memcpy(&ctx->chunk_buf[ctx->chunk_buf_filled],
891 chunkptr, bytes_consumed);
893 resized_chunk = ctx->chunk_buf;
895 chunkptr += bytes_consumed;
896 ctx->cur_read_res_offset += bytes_consumed;
897 ctx->chunk_buf_filled += bytes_consumed;
898 if (ctx->chunk_buf_filled == needed_chunk_size) {
899 resized_chunk = ctx->chunk_buf;
900 ctx->chunk_buf_filled = 0;
907 ret = submit_chunk_for_compression(ctx, resized_chunk,
912 } while (chunkptr != chunkend);
916 /* Finish processing a stream for writing. It may not have been completely
917 * written yet, as the chunk_compressor implementation may still have chunks
918 * buffered or being compressed. */
920 write_stream_end_read(struct wim_lookup_table_entry *lte, int status, void *_ctx)
922 struct write_streams_ctx *ctx = _ctx;
924 wimlib_assert(ctx->cur_read_res_offset == ctx->cur_read_res_size);
925 if (ctx->stream_was_duplicate) {
926 free_lookup_table_entry(lte);
927 } else if (lte->unhashed && ctx->lookup_table != NULL) {
928 list_del(<e->unhashed_list);
929 lookup_table_insert(ctx->lookup_table, lte);
935 /* Compute statistics about a list of streams that will be written.
937 * Assumes the streams are sorted such that all streams located in each distinct
938 * WIM (specified by WIMStruct) are together. */
940 compute_stream_list_stats(struct list_head *stream_list,
941 struct write_streams_ctx *ctx)
943 struct wim_lookup_table_entry *lte;
947 WIMStruct *prev_wim_part = NULL;
949 list_for_each_entry(lte, stream_list, write_streams_list) {
951 total_bytes += lte->size;
952 if (lte->resource_location == RESOURCE_IN_WIM) {
953 if (prev_wim_part != lte->rspec->wim) {
954 prev_wim_part = lte->rspec->wim;
959 ctx->progress_data.progress.write_streams.total_bytes = total_bytes;
960 ctx->progress_data.progress.write_streams.total_streams = num_streams;
961 ctx->progress_data.progress.write_streams.completed_bytes = 0;
962 ctx->progress_data.progress.write_streams.completed_streams = 0;
963 ctx->progress_data.progress.write_streams.compression_type = ctx->out_ctype;
964 ctx->progress_data.progress.write_streams.total_parts = total_parts;
965 ctx->progress_data.progress.write_streams.completed_parts = 0;
966 ctx->progress_data.next_progress = 0;
967 ctx->progress_data.prev_wim_part = NULL;
970 /* Find streams in @stream_list that can be copied to the output WIM in raw form
971 * rather than compressed. Delete these streams from @stream_list, and move one
972 * per resource to @raw_copy_resources. Return the total uncompressed size of
973 * the streams that need to be compressed. */
975 find_raw_copy_resources(struct list_head *stream_list,
976 int write_resource_flags,
979 struct list_head *raw_copy_resources)
981 struct wim_lookup_table_entry *lte, *tmp;
982 u64 num_bytes_to_compress = 0;
984 INIT_LIST_HEAD(raw_copy_resources);
986 /* Initialize temporary raw_copy_ok flag. */
987 list_for_each_entry(lte, stream_list, write_streams_list)
988 if (lte->resource_location == RESOURCE_IN_WIM)
989 lte->rspec->raw_copy_ok = 0;
991 list_for_each_entry_safe(lte, tmp, stream_list, write_streams_list) {
992 if (lte->resource_location == RESOURCE_IN_WIM &&
993 lte->rspec->raw_copy_ok)
995 list_del(<e->write_streams_list);
996 } else if (can_raw_copy(lte, write_resource_flags,
997 out_ctype, out_chunk_size))
999 lte->rspec->raw_copy_ok = 1;
1000 list_move_tail(<e->write_streams_list,
1001 raw_copy_resources);
1003 num_bytes_to_compress += lte->size;
1007 return num_bytes_to_compress;
1010 /* Copy a raw compressed resource located in another WIM file to the WIM file
1013 write_raw_copy_resource(struct wim_resource_spec *in_rspec,
1014 struct filedes *out_fd)
1016 u64 cur_read_offset;
1017 u64 end_read_offset;
1018 u8 buf[BUFFER_SIZE];
1019 size_t bytes_to_read;
1021 struct filedes *in_fd;
1022 struct wim_lookup_table_entry *lte;
1023 u64 out_offset_in_wim;
1025 DEBUG("Copying raw compressed data (size_in_wim=%"PRIu64", "
1026 "uncompressed_size=%"PRIu64")",
1027 in_rspec->size_in_wim, in_rspec->uncompressed_size);
1029 /* Copy the raw data. */
1030 cur_read_offset = in_rspec->offset_in_wim;
1031 end_read_offset = cur_read_offset + in_rspec->size_in_wim;
1033 out_offset_in_wim = out_fd->offset;
1035 if (in_rspec->is_pipable) {
1036 if (cur_read_offset < sizeof(struct pwm_stream_hdr))
1037 return WIMLIB_ERR_INVALID_PIPABLE_WIM;
1038 cur_read_offset -= sizeof(struct pwm_stream_hdr);
1039 out_offset_in_wim += sizeof(struct pwm_stream_hdr);
1041 in_fd = &in_rspec->wim->in_fd;
1042 while (cur_read_offset != end_read_offset) {
1044 bytes_to_read = min(sizeof(buf), end_read_offset - cur_read_offset);
1046 ret = full_pread(in_fd, buf, bytes_to_read, cur_read_offset);
1050 ret = full_write(out_fd, buf, bytes_to_read);
1054 cur_read_offset += bytes_to_read;
1057 list_for_each_entry(lte, &in_rspec->stream_list, rspec_node) {
1058 if (lte->will_be_in_output_wim) {
1059 stream_set_out_reshdr_for_reuse(lte);
1060 if (in_rspec->flags & WIM_RESHDR_FLAG_PACKED_STREAMS)
1061 lte->out_res_offset_in_wim = out_offset_in_wim;
1063 lte->out_reshdr.offset_in_wim = out_offset_in_wim;
1070 /* Copy a list of raw compressed resources located other WIM file(s) to the WIM
1071 * file being written. */
1073 write_raw_copy_resources(struct list_head *raw_copy_resources,
1074 struct filedes *out_fd)
1076 struct wim_lookup_table_entry *lte;
1079 list_for_each_entry(lte, raw_copy_resources, write_streams_list) {
1080 ret = write_raw_copy_resource(lte->rspec, out_fd);
1087 /* Wait for and write all chunks pending in the compressor. */
1089 finish_remaining_chunks(struct write_streams_ctx *ctx)
1096 if (ctx->compressor == NULL)
1099 if (ctx->chunk_buf_filled != 0) {
1100 ret = submit_chunk_for_compression(ctx, ctx->chunk_buf,
1101 ctx->chunk_buf_filled);
1106 while (ctx->compressor->get_chunk(ctx->compressor, &cdata, &csize, &usize)) {
1107 ret = write_chunk(ctx, cdata, csize, usize);
1115 remove_zero_length_streams(struct list_head *stream_list)
1117 struct wim_lookup_table_entry *lte, *tmp;
1119 list_for_each_entry_safe(lte, tmp, stream_list, write_streams_list) {
1120 wimlib_assert(lte->will_be_in_output_wim);
1121 if (lte->size == 0) {
1122 list_del(<e->write_streams_list);
1123 lte->out_reshdr.offset_in_wim = 0;
1124 lte->out_reshdr.size_in_wim = 0;
1125 lte->out_reshdr.uncompressed_size = 0;
1126 lte->out_reshdr.flags = filter_resource_flags(lte->flags);
1132 write_stream_list(struct list_head *stream_list,
1133 struct filedes *out_fd,
1134 int write_resource_flags,
1137 unsigned num_threads,
1138 struct wim_lookup_table *lookup_table,
1139 struct filter_context *filter_ctx,
1140 struct wimlib_lzx_context **comp_ctx,
1141 wimlib_progress_func_t progress_func)
1144 struct write_streams_ctx ctx;
1145 struct list_head raw_copy_resources;
1147 remove_zero_length_streams(stream_list);
1149 if (list_empty(stream_list)) {
1150 DEBUG("No streams to write.");
1154 memset(&ctx, 0, sizeof(ctx));
1156 /* Pre-sorting the streams is required for compute_stream_list_stats().
1157 * Afterwards, read_stream_list() need not sort them again. */
1158 ret = sort_stream_list_by_sequential_order(stream_list,
1159 offsetof(struct wim_lookup_table_entry,
1160 write_streams_list));
1164 ctx.out_fd = out_fd;
1165 ctx.lookup_table = lookup_table;
1166 ctx.out_ctype = out_ctype;
1167 ctx.out_chunk_size = out_chunk_size;
1168 ctx.write_resource_flags = write_resource_flags;
1169 ctx.filter_ctx = filter_ctx;
1171 if (out_chunk_size <= STACK_MAX) {
1172 ctx.chunk_buf = alloca(out_chunk_size);
1174 ctx.chunk_buf = MALLOC(out_chunk_size);
1175 if (ctx.chunk_buf == NULL) {
1176 ret = WIMLIB_ERR_NOMEM;
1177 goto out_destroy_context;
1180 ctx.chunk_buf_filled = 0;
1182 compute_stream_list_stats(stream_list, &ctx);
1184 ctx.progress_data.progress_func = progress_func;
1186 ctx.num_bytes_to_compress = find_raw_copy_resources(stream_list,
1187 write_resource_flags,
1190 &raw_copy_resources);
1192 DEBUG("Writing stream list "
1193 "(offset = %"PRIu64", write_resource_flags=0x%08x, "
1194 "out_ctype=%d, out_chunk_size=%u, num_threads=%u, "
1195 "total_bytes=%"PRIu64", num_bytes_to_compress=%"PRIu64")",
1196 out_fd->offset, write_resource_flags,
1197 out_ctype, out_chunk_size, num_threads,
1198 ctx.progress_data.progress.write_streams.total_bytes,
1199 ctx.num_bytes_to_compress);
1201 if (ctx.num_bytes_to_compress == 0) {
1202 DEBUG("No compression needed; skipping to raw copy!");
1203 goto out_write_raw_copy_resources;
1206 /* Unless uncompressed output was required, allocate a chunk_compressor
1207 * to do compression. There are serial and parallel implementations of
1208 * the chunk_compressor interface. We default to parallel using the
1209 * specified number of threads, unless the upper bound on the number
1210 * bytes needing to be compressed is less 2000000 (heuristic value). */
1211 if (out_ctype != WIMLIB_COMPRESSION_TYPE_NONE) {
1213 if (ctx.num_bytes_to_compress >= 2000000) {
1214 ret = new_parallel_chunk_compressor(out_ctype,
1219 DEBUG("Couldn't create parallel chunk compressor "
1220 "(status %d)", ret);
1224 if (ctx.compressor == NULL) {
1225 if (out_ctype == WIMLIB_COMPRESSION_TYPE_LZX) {
1226 ret = wimlib_lzx_alloc_context(out_chunk_size,
1230 goto out_destroy_context;
1232 ret = new_serial_chunk_compressor(out_ctype, out_chunk_size,
1233 *comp_ctx, &ctx.compressor);
1235 goto out_destroy_context;
1240 ctx.progress_data.progress.write_streams.num_threads = ctx.compressor->num_threads;
1242 ctx.progress_data.progress.write_streams.num_threads = 1;
1244 DEBUG("Actually using %u threads",
1245 ctx.progress_data.progress.write_streams.num_threads);
1247 INIT_LIST_HEAD(&ctx.pending_streams);
1249 if (write_resource_flags & WIMLIB_WRITE_RESOURCE_FLAG_PACK_STREAMS) {
1250 ret = begin_write_resource(&ctx, ctx.num_bytes_to_compress);
1252 goto out_destroy_context;
1255 /* Read the list of streams needing to be compressed, using the
1256 * specified callbacks to execute processing of the data. */
1258 struct read_stream_list_callbacks cbs = {
1259 .begin_stream = write_stream_begin_read,
1260 .begin_stream_ctx = &ctx,
1261 .consume_chunk = write_stream_process_chunk,
1262 .consume_chunk_ctx = &ctx,
1263 .end_stream = write_stream_end_read,
1264 .end_stream_ctx = &ctx,
1267 ret = read_stream_list(stream_list,
1268 offsetof(struct wim_lookup_table_entry, write_streams_list),
1270 STREAM_LIST_ALREADY_SORTED |
1271 VERIFY_STREAM_HASHES |
1272 COMPUTE_MISSING_STREAM_HASHES);
1275 goto out_destroy_context;
1277 ret = finish_remaining_chunks(&ctx);
1279 goto out_destroy_context;
1281 if (write_resource_flags & WIMLIB_WRITE_RESOURCE_FLAG_PACK_STREAMS) {
1282 struct wim_reshdr reshdr;
1283 struct wim_lookup_table_entry *lte;
1286 ret = end_write_resource(&ctx, &reshdr);
1288 goto out_destroy_context;
1290 DEBUG("Ending packed resource: %lu %lu %lu.",
1291 reshdr.offset_in_wim,
1293 reshdr.uncompressed_size);
1296 list_for_each_entry(lte, &ctx.pending_streams, write_streams_list) {
1297 lte->out_reshdr.size_in_wim = lte->size;
1298 lte->out_reshdr.flags = filter_resource_flags(lte->flags);
1299 lte->out_reshdr.flags |= WIM_RESHDR_FLAG_PACKED_STREAMS;
1300 lte->out_reshdr.uncompressed_size = 0;
1301 lte->out_reshdr.offset_in_wim = offset_in_res;
1302 lte->out_res_offset_in_wim = reshdr.offset_in_wim;
1303 lte->out_res_size_in_wim = reshdr.size_in_wim;
1304 /*lte->out_res_uncompressed_size = reshdr.uncompressed_size;*/
1305 offset_in_res += lte->size;
1307 wimlib_assert(offset_in_res == reshdr.uncompressed_size);
1310 out_write_raw_copy_resources:
1311 /* Copy any compressed resources for which the raw data can be reused
1312 * without decompression. */
1313 ret = write_raw_copy_resources(&raw_copy_resources, ctx.out_fd);
1315 out_destroy_context:
1316 if (out_chunk_size > STACK_MAX)
1317 FREE(ctx.chunk_buf);
1318 FREE(ctx.chunk_csizes);
1320 ctx.compressor->destroy(ctx.compressor);
1321 DEBUG("Done (ret=%d)", ret);
1326 write_wim_resource(struct wim_lookup_table_entry *lte,
1327 struct filedes *out_fd,
1330 int write_resource_flags,
1331 struct wimlib_lzx_context **comp_ctx)
1333 LIST_HEAD(stream_list);
1334 list_add(<e->write_streams_list, &stream_list);
1335 lte->will_be_in_output_wim = 1;
1336 return write_stream_list(&stream_list,
1338 write_resource_flags & ~WIMLIB_WRITE_RESOURCE_FLAG_PACK_STREAMS,
1349 write_wim_resource_from_buffer(const void *buf, size_t buf_size,
1350 int reshdr_flags, struct filedes *out_fd,
1353 struct wim_reshdr *out_reshdr,
1355 int write_resource_flags,
1356 struct wimlib_lzx_context **comp_ctx)
1359 struct wim_lookup_table_entry *lte;
1361 /* Set up a temporary lookup table entry to provide to
1362 * write_wim_resource(). */
1364 lte = new_lookup_table_entry();
1366 return WIMLIB_ERR_NOMEM;
1368 lte->resource_location = RESOURCE_IN_ATTACHED_BUFFER;
1369 lte->attached_buffer = (void*)buf;
1370 lte->size = buf_size;
1371 lte->flags = reshdr_flags;
1373 if (write_resource_flags & WIMLIB_WRITE_RESOURCE_FLAG_PIPABLE) {
1374 sha1_buffer(buf, buf_size, lte->hash);
1380 ret = write_wim_resource(lte, out_fd, out_ctype, out_chunk_size,
1381 write_resource_flags, comp_ctx);
1385 copy_reshdr(out_reshdr, <e->out_reshdr);
1388 copy_hash(hash, lte->hash);
1391 lte->resource_location = RESOURCE_NONEXISTENT;
1392 free_lookup_table_entry(lte);
1396 struct stream_size_table {
1397 struct hlist_head *array;
1403 init_stream_size_table(struct stream_size_table *tab, size_t capacity)
1405 tab->array = CALLOC(capacity, sizeof(tab->array[0]));
1406 if (tab->array == NULL)
1407 return WIMLIB_ERR_NOMEM;
1408 tab->num_entries = 0;
1409 tab->capacity = capacity;
1414 destroy_stream_size_table(struct stream_size_table *tab)
1420 stream_size_table_insert(struct wim_lookup_table_entry *lte, void *_tab)
1422 struct stream_size_table *tab = _tab;
1424 struct wim_lookup_table_entry *same_size_lte;
1425 struct hlist_node *tmp;
1427 pos = hash_u64(lte->size) % tab->capacity;
1428 lte->unique_size = 1;
1429 hlist_for_each_entry(same_size_lte, tmp, &tab->array[pos], hash_list_2) {
1430 if (same_size_lte->size == lte->size) {
1431 lte->unique_size = 0;
1432 same_size_lte->unique_size = 0;
1437 hlist_add_head(<e->hash_list_2, &tab->array[pos]);
1442 struct find_streams_ctx {
1445 struct list_head stream_list;
1446 struct stream_size_table stream_size_tab;
1450 reference_stream_for_write(struct wim_lookup_table_entry *lte,
1451 struct list_head *stream_list, u32 nref)
1453 if (!lte->will_be_in_output_wim) {
1454 lte->out_refcnt = 0;
1455 list_add_tail(<e->write_streams_list, stream_list);
1456 lte->will_be_in_output_wim = 1;
1458 lte->out_refcnt += nref;
1462 fully_reference_stream_for_write(struct wim_lookup_table_entry *lte,
1465 struct list_head *stream_list = _stream_list;
1466 lte->will_be_in_output_wim = 0;
1467 reference_stream_for_write(lte, stream_list, lte->refcnt);
1472 inode_find_streams_to_reference(const struct wim_inode *inode,
1473 const struct wim_lookup_table *table,
1474 struct list_head *stream_list)
1476 struct wim_lookup_table_entry *lte;
1479 wimlib_assert(inode->i_nlink > 0);
1481 for (i = 0; i <= inode->i_num_ads; i++) {
1482 lte = inode_stream_lte(inode, i, table);
1484 reference_stream_for_write(lte, stream_list,
1486 else if (!is_zero_hash(inode_stream_hash(inode, i)))
1487 return WIMLIB_ERR_RESOURCE_NOT_FOUND;
1493 do_stream_set_not_in_output_wim(struct wim_lookup_table_entry *lte, void *_ignore)
1495 lte->will_be_in_output_wim = 0;
1500 image_find_streams_to_reference(WIMStruct *wim)
1502 struct wim_image_metadata *imd;
1503 struct wim_inode *inode;
1504 struct wim_lookup_table_entry *lte;
1505 struct list_head *stream_list;
1508 imd = wim_get_current_image_metadata(wim);
1510 image_for_each_unhashed_stream(lte, imd)
1511 lte->will_be_in_output_wim = 0;
1513 stream_list = wim->private;
1514 image_for_each_inode(inode, imd) {
1515 ret = inode_find_streams_to_reference(inode,
1525 prepare_unfiltered_list_of_streams_in_output_wim(WIMStruct *wim,
1528 struct list_head *stream_list_ret)
1532 INIT_LIST_HEAD(stream_list_ret);
1534 if (streams_ok && (image == WIMLIB_ALL_IMAGES ||
1535 (image == 1 && wim->hdr.image_count == 1)))
1537 /* Fast case: Assume that all streams are being written and
1538 * that the reference counts are correct. */
1539 struct wim_lookup_table_entry *lte;
1540 struct wim_image_metadata *imd;
1543 for_lookup_table_entry(wim->lookup_table,
1544 fully_reference_stream_for_write,
1547 for (i = 0; i < wim->hdr.image_count; i++) {
1548 imd = wim->image_metadata[i];
1549 image_for_each_unhashed_stream(lte, imd)
1550 fully_reference_stream_for_write(lte, stream_list_ret);
1553 /* Slow case: Walk through the images being written and
1554 * determine the streams referenced. */
1555 for_lookup_table_entry(wim->lookup_table,
1556 do_stream_set_not_in_output_wim, NULL);
1557 wim->private = stream_list_ret;
1558 ret = for_image(wim, image, image_find_streams_to_reference);
1566 struct insert_other_if_hard_filtered_ctx {
1567 struct stream_size_table *tab;
1568 struct filter_context *filter_ctx;
1572 insert_other_if_hard_filtered(struct wim_lookup_table_entry *lte, void *_ctx)
1574 struct insert_other_if_hard_filtered_ctx *ctx = _ctx;
1576 if (!lte->will_be_in_output_wim &&
1577 stream_hard_filtered(lte, ctx->filter_ctx))
1578 stream_size_table_insert(lte, ctx->tab);
1583 determine_stream_size_uniquity(struct list_head *stream_list,
1584 struct wim_lookup_table *lt,
1585 struct filter_context *filter_ctx)
1588 struct stream_size_table tab;
1589 struct wim_lookup_table_entry *lte;
1591 ret = init_stream_size_table(&tab, lt->capacity);
1595 if (may_hard_filter_streams(filter_ctx)) {
1596 struct insert_other_if_hard_filtered_ctx ctx = {
1598 .filter_ctx = filter_ctx,
1600 for_lookup_table_entry(lt, insert_other_if_hard_filtered, &ctx);
1603 list_for_each_entry(lte, stream_list, write_streams_list)
1604 stream_size_table_insert(lte, &tab);
1606 destroy_stream_size_table(&tab);
1611 filter_stream_list_for_write(struct list_head *stream_list,
1612 struct filter_context *filter_ctx)
1614 struct wim_lookup_table_entry *lte, *tmp;
1616 list_for_each_entry_safe(lte, tmp,
1617 stream_list, write_streams_list)
1619 int status = stream_filtered(lte, filter_ctx);
1626 /* Soft filtered. */
1628 /* Hard filtered. */
1629 lte->will_be_in_output_wim = 0;
1630 list_del(<e->lookup_table_list);
1632 list_del(<e->write_streams_list);
1638 * prepare_stream_list_for_write() -
1640 * Prepare the list of streams to write for writing a WIM containing the
1641 * specified image(s) with the specified write flags.
1644 * The WIMStruct on whose behalf the write is occurring.
1647 * Image(s) from the WIM to write; may be WIMLIB_ALL_IMAGES.
1650 * WIMLIB_WRITE_FLAG_* flags for the write operation:
1652 * STREAMS_OK: For writes of all images, assume that all streams in the
1653 * lookup table of @wim and the per-image lists of unhashed streams should
1654 * be taken as-is, and image metadata should not be searched for
1655 * references. This does not exclude filtering with OVERWRITE and
1656 * SKIP_EXTERNAL_WIMS, below.
1658 * OVERWRITE: Streams already present in @wim shall not be returned in
1661 * SKIP_EXTERNAL_WIMS: Streams already present in a WIM file, but not
1662 * @wim, shall be be returned in neither @stream_list_ret nor
1663 * @lookup_table_list_ret.
1666 * List of streams, linked by write_streams_list, that need to be written
1667 * will be returned here.
1669 * Note that this function assumes that unhashed streams will be written;
1670 * it does not take into account that they may become duplicates when
1673 * @lookup_table_list_ret
1674 * List of streams, linked by lookup_table_list, that need to be included
1675 * in the WIM's lookup table will be returned here. This will be a
1676 * superset of the streams in @stream_list_ret.
1678 * This list will be a proper superset of @stream_list_ret if and only if
1679 * WIMLIB_WRITE_FLAG_OVERWRITE was specified in @write_flags and some of
1680 * the streams that would otherwise need to be written were already located
1683 * All streams in this list will have @out_refcnt set to the number of
1684 * references to the stream in the output WIM. If
1685 * WIMLIB_WRITE_FLAG_STREAMS_OK was specified in @write_flags, @out_refcnt
1686 * may be as low as 0.
1689 * A context for queries of stream filter status with stream_filtered() is
1690 * returned in this location.
1692 * In addition, @will_be_in_output_wim will be set to 1 in all stream entries
1693 * inserted into @lookup_table_list_ret and to 0 in all stream entries in the
1694 * lookup table of @wim not inserted into @lookup_table_list_ret.
1696 * Still furthermore, @unique_size will be set to 1 on all stream entries in
1697 * @stream_list_ret that have unique size among all stream entries in
1698 * @stream_list_ret and among all stream entries in the lookup table of @wim
1699 * that are ineligible for being written due to filtering.
1701 * Returns 0 on success; nonzero on read error, memory allocation error, or
1705 prepare_stream_list_for_write(WIMStruct *wim, int image,
1707 struct list_head *stream_list_ret,
1708 struct list_head *lookup_table_list_ret,
1709 struct filter_context *filter_ctx_ret)
1712 struct wim_lookup_table_entry *lte;
1714 filter_ctx_ret->write_flags = write_flags;
1715 filter_ctx_ret->wim = wim;
1717 ret = prepare_unfiltered_list_of_streams_in_output_wim(
1720 write_flags & WIMLIB_WRITE_FLAG_STREAMS_OK,
1725 INIT_LIST_HEAD(lookup_table_list_ret);
1726 list_for_each_entry(lte, stream_list_ret, write_streams_list)
1727 list_add_tail(<e->lookup_table_list, lookup_table_list_ret);
1729 ret = determine_stream_size_uniquity(stream_list_ret, wim->lookup_table,
1734 if (may_filter_streams(filter_ctx_ret))
1735 filter_stream_list_for_write(stream_list_ret, filter_ctx_ret);
1741 write_wim_streams(WIMStruct *wim, int image, int write_flags,
1742 unsigned num_threads,
1743 wimlib_progress_func_t progress_func,
1744 struct list_head *stream_list_override,
1745 struct list_head *lookup_table_list_ret)
1748 struct list_head _stream_list;
1749 struct list_head *stream_list;
1750 struct wim_lookup_table_entry *lte;
1751 struct filter_context _filter_ctx;
1752 struct filter_context *filter_ctx;
1754 if (stream_list_override == NULL) {
1755 /* Normal case: prepare stream list from image(s) being written.
1757 stream_list = &_stream_list;
1758 filter_ctx = &_filter_ctx;
1759 ret = prepare_stream_list_for_write(wim, image, write_flags,
1761 lookup_table_list_ret,
1766 /* Currently only as a result of wimlib_split() being called:
1767 * use stream list already explicitly provided. Use existing
1768 * reference counts. */
1769 stream_list = stream_list_override;
1771 INIT_LIST_HEAD(lookup_table_list_ret);
1772 list_for_each_entry(lte, stream_list, write_streams_list) {
1773 lte->out_refcnt = lte->refcnt;
1774 lte->will_be_in_output_wim = 1;
1775 lte->unique_size = 0;
1776 list_add_tail(<e->lookup_table_list, lookup_table_list_ret);
1780 return write_stream_list(stream_list,
1782 write_flags_to_resource_flags(write_flags),
1783 wim->out_compression_type,
1784 wim->out_chunk_size,
1793 write_wim_metadata_resources(WIMStruct *wim, int image, int write_flags,
1794 wimlib_progress_func_t progress_func)
1799 int write_resource_flags;
1801 if (write_flags & WIMLIB_WRITE_FLAG_NO_METADATA) {
1802 DEBUG("Not writing any metadata resources.");
1806 write_resource_flags = write_flags_to_resource_flags(write_flags);
1808 write_resource_flags &= ~WIMLIB_WRITE_RESOURCE_FLAG_PACK_STREAMS;
1810 DEBUG("Writing metadata resources (offset=%"PRIu64")",
1811 wim->out_fd.offset);
1814 progress_func(WIMLIB_PROGRESS_MSG_WRITE_METADATA_BEGIN, NULL);
1816 if (image == WIMLIB_ALL_IMAGES) {
1818 end_image = wim->hdr.image_count;
1820 start_image = image;
1824 for (int i = start_image; i <= end_image; i++) {
1825 struct wim_image_metadata *imd;
1827 imd = wim->image_metadata[i - 1];
1828 /* Build a new metadata resource only if image was modified from
1829 * the original (or was newly added). Otherwise just copy the
1831 if (imd->modified) {
1832 DEBUG("Image %u was modified; building and writing new "
1833 "metadata resource", i);
1834 ret = write_metadata_resource(wim, i,
1835 write_resource_flags);
1836 } else if (write_flags & WIMLIB_WRITE_FLAG_OVERWRITE) {
1837 DEBUG("Image %u was not modified; re-using existing "
1838 "metadata resource.", i);
1839 stream_set_out_reshdr_for_reuse(imd->metadata_lte);
1842 DEBUG("Image %u was not modified; copying existing "
1843 "metadata resource.", i);
1844 ret = write_wim_resource(imd->metadata_lte,
1846 wim->out_compression_type,
1847 wim->out_chunk_size,
1848 write_resource_flags,
1855 progress_func(WIMLIB_PROGRESS_MSG_WRITE_METADATA_END, NULL);
1860 open_wim_writable(WIMStruct *wim, const tchar *path, int open_flags)
1863 DEBUG("Opening \"%"TS"\" for writing.", path);
1865 raw_fd = topen(path, open_flags | O_BINARY, 0644);
1867 ERROR_WITH_ERRNO("Failed to open \"%"TS"\" for writing", path);
1868 return WIMLIB_ERR_OPEN;
1870 filedes_init(&wim->out_fd, raw_fd);
1875 close_wim_writable(WIMStruct *wim, int write_flags)
1879 if (!(write_flags & WIMLIB_WRITE_FLAG_FILE_DESCRIPTOR)) {
1880 DEBUG("Closing WIM file.");
1881 if (filedes_valid(&wim->out_fd))
1882 if (filedes_close(&wim->out_fd))
1883 ret = WIMLIB_ERR_WRITE;
1885 filedes_invalidate(&wim->out_fd);
1890 cmp_streams_by_out_rspec(const void *p1, const void *p2)
1892 const struct wim_lookup_table_entry *lte1, *lte2;
1894 lte1 = *(const struct wim_lookup_table_entry**)p1;
1895 lte2 = *(const struct wim_lookup_table_entry**)p2;
1897 if (lte1->out_reshdr.flags & WIM_RESHDR_FLAG_PACKED_STREAMS) {
1898 if (lte2->out_reshdr.flags & WIM_RESHDR_FLAG_PACKED_STREAMS) {
1899 if (lte1->out_res_offset_in_wim != lte2->out_res_offset_in_wim)
1900 return cmp_u64(lte1->out_res_offset_in_wim,
1901 lte2->out_res_offset_in_wim);
1906 if (lte2->out_reshdr.flags & WIM_RESHDR_FLAG_PACKED_STREAMS)
1909 return cmp_u64(lte1->out_reshdr.offset_in_wim,
1910 lte2->out_reshdr.offset_in_wim);
1914 write_wim_lookup_table(WIMStruct *wim, int image, int write_flags,
1915 struct wim_reshdr *out_reshdr,
1916 struct list_head *lookup_table_list)
1920 /* Set output resource metadata for streams already present in WIM. */
1921 if (write_flags & WIMLIB_WRITE_FLAG_OVERWRITE) {
1922 struct wim_lookup_table_entry *lte;
1923 list_for_each_entry(lte, lookup_table_list, lookup_table_list)
1925 if (lte->resource_location == RESOURCE_IN_WIM &&
1926 lte->rspec->wim == wim)
1928 stream_set_out_reshdr_for_reuse(lte);
1933 ret = sort_stream_list(lookup_table_list,
1934 offsetof(struct wim_lookup_table_entry, lookup_table_list),
1935 cmp_streams_by_out_rspec);
1939 /* Add entries for metadata resources. */
1940 if (!(write_flags & WIMLIB_WRITE_FLAG_NO_METADATA)) {
1944 if (image == WIMLIB_ALL_IMAGES) {
1946 end_image = wim->hdr.image_count;
1948 start_image = image;
1952 /* Push metadata resource lookup table entries onto the front of
1953 * the list in reverse order, so that they're written in order.
1955 for (int i = end_image; i >= start_image; i--) {
1956 struct wim_lookup_table_entry *metadata_lte;
1958 metadata_lte = wim->image_metadata[i - 1]->metadata_lte;
1959 wimlib_assert(metadata_lte->out_reshdr.flags & WIM_RESHDR_FLAG_METADATA);
1960 metadata_lte->out_refcnt = 1;
1961 list_add(&metadata_lte->lookup_table_list, lookup_table_list);
1965 return write_wim_lookup_table_from_stream_list(lookup_table_list,
1967 wim->hdr.part_number,
1969 write_flags_to_resource_flags(write_flags),
1976 * Finish writing a WIM file: write the lookup table, xml data, and integrity
1977 * table, then overwrite the WIM header. By default, closes the WIM file
1978 * descriptor (@wim->out_fd) if successful.
1980 * write_flags is a bitwise OR of the following:
1982 * (public) WIMLIB_WRITE_FLAG_CHECK_INTEGRITY:
1983 * Include an integrity table.
1985 * (public) WIMLIB_WRITE_FLAG_FSYNC:
1986 * fsync() the output file before closing it.
1988 * (public) WIMLIB_WRITE_FLAG_PIPABLE:
1989 * Writing a pipable WIM, possibly to a pipe; include pipable WIM
1990 * stream headers before the lookup table and XML data, and also
1991 * write the WIM header at the end instead of seeking to the
1992 * beginning. Can't be combined with
1993 * WIMLIB_WRITE_FLAG_CHECK_INTEGRITY.
1995 * (private) WIMLIB_WRITE_FLAG_NO_LOOKUP_TABLE:
1996 * Don't write the lookup table.
1998 * (private) WIMLIB_WRITE_FLAG_REUSE_INTEGRITY_TABLE:
1999 * When (if) writing the integrity table, re-use entries from the
2000 * existing integrity table, if possible.
2002 * (private) WIMLIB_WRITE_FLAG_CHECKPOINT_AFTER_XML:
2003 * After writing the XML data but before writing the integrity
2004 * table, write a temporary WIM header and flush the stream so that
2005 * the WIM is less likely to become corrupted upon abrupt program
2007 * (private) WIMLIB_WRITE_FLAG_HEADER_AT_END:
2008 * Instead of overwriting the WIM header at the beginning of the
2009 * file, simply append it to the end of the file. (Used when
2011 * (private) WIMLIB_WRITE_FLAG_FILE_DESCRIPTOR:
2012 * Do not close the file descriptor @wim->out_fd on either success
2014 * (private) WIMLIB_WRITE_FLAG_USE_EXISTING_TOTALBYTES:
2015 * Use the existing <TOTALBYTES> stored in the in-memory XML
2016 * information, rather than setting it to the offset of the XML
2017 * data being written.
2020 finish_write(WIMStruct *wim, int image, int write_flags,
2021 wimlib_progress_func_t progress_func,
2022 struct list_head *lookup_table_list)
2026 int write_resource_flags;
2027 off_t old_lookup_table_end;
2028 off_t new_lookup_table_end;
2031 DEBUG("image=%d, write_flags=%08x", image, write_flags);
2033 write_resource_flags = write_flags_to_resource_flags(write_flags);
2035 /* In the WIM header, there is room for the resource entry for a
2036 * metadata resource labeled as the "boot metadata". This entry should
2037 * be zeroed out if there is no bootable image (boot_idx 0). Otherwise,
2038 * it should be a copy of the resource entry for the image that is
2039 * marked as bootable. This is not well documented... */
2040 if (wim->hdr.boot_idx == 0) {
2041 zero_reshdr(&wim->hdr.boot_metadata_reshdr);
2043 copy_reshdr(&wim->hdr.boot_metadata_reshdr,
2044 &wim->image_metadata[
2045 wim->hdr.boot_idx - 1]->metadata_lte->out_reshdr);
2048 /* Write lookup table. (Save old position first.) */
2049 old_lookup_table_end = wim->hdr.lookup_table_reshdr.offset_in_wim +
2050 wim->hdr.lookup_table_reshdr.size_in_wim;
2051 if (!(write_flags & WIMLIB_WRITE_FLAG_NO_LOOKUP_TABLE)) {
2052 ret = write_wim_lookup_table(wim, image, write_flags,
2053 &wim->hdr.lookup_table_reshdr,
2059 /* Write XML data. */
2060 xml_totalbytes = wim->out_fd.offset;
2061 if (write_flags & WIMLIB_WRITE_FLAG_USE_EXISTING_TOTALBYTES)
2062 xml_totalbytes = WIM_TOTALBYTES_USE_EXISTING;
2063 ret = write_wim_xml_data(wim, image, xml_totalbytes,
2064 &wim->hdr.xml_data_reshdr,
2065 write_resource_flags);
2069 /* Write integrity table (optional). */
2070 if (write_flags & WIMLIB_WRITE_FLAG_CHECK_INTEGRITY) {
2071 if (write_flags & WIMLIB_WRITE_FLAG_CHECKPOINT_AFTER_XML) {
2072 struct wim_header checkpoint_hdr;
2073 memcpy(&checkpoint_hdr, &wim->hdr, sizeof(struct wim_header));
2074 zero_reshdr(&checkpoint_hdr.integrity_table_reshdr);
2075 checkpoint_hdr.flags |= WIM_HDR_FLAG_WRITE_IN_PROGRESS;
2076 ret = write_wim_header_at_offset(&checkpoint_hdr,
2082 if (!(write_flags & WIMLIB_WRITE_FLAG_REUSE_INTEGRITY_TABLE))
2083 old_lookup_table_end = 0;
2085 new_lookup_table_end = wim->hdr.lookup_table_reshdr.offset_in_wim +
2086 wim->hdr.lookup_table_reshdr.size_in_wim;
2088 ret = write_integrity_table(wim,
2089 new_lookup_table_end,
2090 old_lookup_table_end,
2095 /* No integrity table. */
2096 zero_reshdr(&wim->hdr.integrity_table_reshdr);
2099 /* Now that all information in the WIM header has been determined, the
2100 * preliminary header written earlier can be overwritten, the header of
2101 * the existing WIM file can be overwritten, or the final header can be
2102 * written to the end of the pipable WIM. */
2103 wim->hdr.flags &= ~WIM_HDR_FLAG_WRITE_IN_PROGRESS;
2105 if (write_flags & WIMLIB_WRITE_FLAG_HEADER_AT_END)
2106 hdr_offset = wim->out_fd.offset;
2107 DEBUG("Writing new header @ %"PRIu64".", hdr_offset);
2108 ret = write_wim_header_at_offset(&wim->hdr, &wim->out_fd, hdr_offset);
2112 /* Possibly sync file data to disk before closing. On POSIX systems, it
2113 * is necessary to do this before using rename() to overwrite an
2114 * existing file with a new file. Otherwise, data loss would occur if
2115 * the system is abruptly terminated when the metadata for the rename
2116 * operation has been written to disk, but the new file data has not.
2118 if (write_flags & WIMLIB_WRITE_FLAG_FSYNC) {
2119 DEBUG("Syncing WIM file.");
2120 if (fsync(wim->out_fd.fd)) {
2121 ERROR_WITH_ERRNO("Error syncing data to WIM file");
2122 return WIMLIB_ERR_WRITE;
2126 if (close_wim_writable(wim, write_flags)) {
2127 ERROR_WITH_ERRNO("Failed to close the output WIM file");
2128 return WIMLIB_ERR_WRITE;
2134 #if defined(HAVE_SYS_FILE_H) && defined(HAVE_FLOCK)
2136 lock_wim(WIMStruct *wim, int fd)
2139 if (fd != -1 && !wim->wim_locked) {
2140 ret = flock(fd, LOCK_EX | LOCK_NB);
2142 if (errno == EWOULDBLOCK) {
2143 ERROR("`%"TS"' is already being modified or has been "
2144 "mounted read-write\n"
2145 " by another process!", wim->filename);
2146 ret = WIMLIB_ERR_ALREADY_LOCKED;
2148 WARNING_WITH_ERRNO("Failed to lock `%"TS"'",
2153 wim->wim_locked = 1;
2161 * write_pipable_wim():
2163 * Perform the intermediate stages of creating a "pipable" WIM (i.e. a WIM
2164 * capable of being applied from a pipe).
2166 * Pipable WIMs are a wimlib-specific modification of the WIM format such that
2167 * images can be applied from them sequentially when the file data is sent over
2168 * a pipe. In addition, a pipable WIM can be written sequentially to a pipe.
2169 * The modifications made to the WIM format for pipable WIMs are:
2171 * - Magic characters in header are "WLPWM\0\0\0" (wimlib pipable WIM) instead
2172 * of "MSWIM\0\0\0". This lets wimlib know that the WIM is pipable and also
2173 * stops other software from trying to read the file as a normal WIM.
2175 * - The header at the beginning of the file does not contain all the normal
2176 * information; in particular it will have all 0's for the lookup table and
2177 * XML data resource entries. This is because this information cannot be
2178 * determined until the lookup table and XML data have been written.
2179 * Consequently, wimlib will write the full header at the very end of the
2180 * file. The header at the end, however, is only used when reading the WIM
2181 * from a seekable file (not a pipe).
2183 * - An extra copy of the XML data is placed directly after the header. This
2184 * allows image names and sizes to be determined at an appropriate time when
2185 * reading the WIM from a pipe. This copy of the XML data is ignored if the
2186 * WIM is read from a seekable file (not a pipe).
2188 * - The format of resources, or streams, has been modified to allow them to be
2189 * used before the "lookup table" has been read. Each stream is prefixed with
2190 * a `struct pwm_stream_hdr' that is basically an abbreviated form of `struct
2191 * wim_lookup_table_entry_disk' that only contains the SHA1 message digest,
2192 * uncompressed stream size, and flags that indicate whether the stream is
2193 * compressed. The data of uncompressed streams then follows literally, while
2194 * the data of compressed streams follows in a modified format. Compressed
2195 * streams do not begin with a chunk table, since the chunk table cannot be
2196 * written until all chunks have been compressed. Instead, each compressed
2197 * chunk is prefixed by a `struct pwm_chunk_hdr' that gives its size.
2198 * Furthermore, the chunk table is written at the end of the resource instead
2199 * of the start. Note: chunk offsets are given in the chunk table as if the
2200 * `struct pwm_chunk_hdr's were not present; also, the chunk table is only
2201 * used if the WIM is being read from a seekable file (not a pipe).
2203 * - Metadata resources always come before other file resources (streams).
2204 * (This does not by itself constitute an incompatibility with normal WIMs,
2205 * since this is valid in normal WIMs.)
2207 * - At least up to the end of the file resources, all components must be packed
2208 * as tightly as possible; there cannot be any "holes" in the WIM. (This does
2209 * not by itself consititute an incompatibility with normal WIMs, since this
2210 * is valid in normal WIMs.)
2212 * Note: the lookup table, XML data, and header at the end are not used when
2213 * applying from a pipe. They exist to support functionality such as image
2214 * application and export when the WIM is *not* read from a pipe.
2216 * Layout of pipable WIM:
2218 * ---------+----------+--------------------+----------------+--------------+-----------+--------+
2219 * | Header | XML data | Metadata resources | File resources | Lookup table | XML data | Header |
2220 * ---------+----------+--------------------+----------------+--------------+-----------+--------+
2222 * Layout of normal WIM:
2224 * +--------+-----------------------------+-------------------------+
2225 * | Header | File and metadata resources | Lookup table | XML data |
2226 * +--------+-----------------------------+-------------------------+
2228 * An optional integrity table can follow the final XML data in both normal and
2229 * pipable WIMs. However, due to implementation details, wimlib currently can
2230 * only include an integrity table in a pipable WIM when writing it to a
2231 * seekable file (not a pipe).
2233 * Do note that since pipable WIMs are not supported by Microsoft's software,
2234 * wimlib does not create them unless explicitly requested (with
2235 * WIMLIB_WRITE_FLAG_PIPABLE) and as stated above they use different magic
2236 * characters to identify the file.
2239 write_pipable_wim(WIMStruct *wim, int image, int write_flags,
2240 unsigned num_threads, wimlib_progress_func_t progress_func,
2241 struct list_head *stream_list_override,
2242 struct list_head *lookup_table_list_ret)
2245 struct wim_reshdr xml_reshdr;
2247 WARNING("Creating a pipable WIM, which will "
2249 " with Microsoft's software (wimgapi/imagex/Dism).");
2251 /* At this point, the header at the beginning of the file has already
2254 /* For efficiency, when wimlib adds an image to the WIM with
2255 * wimlib_add_image(), the SHA1 message digests of files is not
2256 * calculated; instead, they are calculated while the files are being
2257 * written. However, this does not work when writing a pipable WIM,
2258 * since when writing a stream to a pipable WIM, its SHA1 message digest
2259 * needs to be known before the stream data is written. Therefore,
2260 * before getting much farther, we need to pre-calculate the SHA1
2261 * message digests of all streams that will be written. */
2262 ret = wim_checksum_unhashed_streams(wim);
2266 /* Write extra copy of the XML data. */
2267 ret = write_wim_xml_data(wim, image, WIM_TOTALBYTES_OMIT,
2269 WIMLIB_WRITE_RESOURCE_FLAG_PIPABLE);
2273 /* Write metadata resources for the image(s) being included in the
2275 ret = write_wim_metadata_resources(wim, image, write_flags,
2280 /* Write streams needed for the image(s) being included in the output
2281 * WIM, or streams needed for the split WIM part. */
2282 return write_wim_streams(wim, image, write_flags, num_threads,
2283 progress_func, stream_list_override,
2284 lookup_table_list_ret);
2286 /* The lookup table, XML data, and header at end are handled by
2287 * finish_write(). */
2290 /* Write a standalone WIM or split WIM (SWM) part to a new file or to a file
2293 write_wim_part(WIMStruct *wim,
2294 const void *path_or_fd,
2297 unsigned num_threads,
2298 wimlib_progress_func_t progress_func,
2299 unsigned part_number,
2300 unsigned total_parts,
2301 struct list_head *stream_list_override,
2305 struct wim_header hdr_save;
2306 struct list_head lookup_table_list;
2308 if (total_parts == 1)
2309 DEBUG("Writing standalone WIM.");
2311 DEBUG("Writing split WIM part %u/%u", part_number, total_parts);
2312 if (image == WIMLIB_ALL_IMAGES)
2313 DEBUG("Including all images.");
2315 DEBUG("Including image %d only.", image);
2316 if (write_flags & WIMLIB_WRITE_FLAG_FILE_DESCRIPTOR)
2317 DEBUG("File descriptor: %d", *(const int*)path_or_fd);
2319 DEBUG("Path: \"%"TS"\"", (const tchar*)path_or_fd);
2320 DEBUG("Write flags: 0x%08x", write_flags);
2322 if (write_flags & WIMLIB_WRITE_FLAG_CHECK_INTEGRITY)
2323 DEBUG("\tCHECK_INTEGRITY");
2325 if (write_flags & WIMLIB_WRITE_FLAG_REBUILD)
2328 if (write_flags & WIMLIB_WRITE_FLAG_RECOMPRESS)
2329 DEBUG("\tRECOMPRESS");
2331 if (write_flags & WIMLIB_WRITE_FLAG_FSYNC)
2334 if (write_flags & WIMLIB_WRITE_FLAG_SOFT_DELETE)
2337 if (write_flags & WIMLIB_WRITE_FLAG_IGNORE_READONLY_FLAG)
2338 DEBUG("\tIGNORE_READONLY_FLAG");
2340 if (write_flags & WIMLIB_WRITE_FLAG_PIPABLE)
2343 if (write_flags & WIMLIB_WRITE_FLAG_NOT_PIPABLE)
2344 DEBUG("\tNOT_PIPABLE");
2346 if (write_flags & WIMLIB_WRITE_FLAG_PACK_STREAMS)
2347 DEBUG("\tPACK_STREAMS");
2349 if (write_flags & WIMLIB_WRITE_FLAG_NO_PACK_STREAMS)
2350 DEBUG("\tNO_PACK_STREAMS");
2352 if (write_flags & WIMLIB_WRITE_FLAG_FILE_DESCRIPTOR)
2353 DEBUG("\tFILE_DESCRIPTOR");
2355 if (write_flags & WIMLIB_WRITE_FLAG_NO_METADATA)
2356 DEBUG("\tNO_METADATA");
2358 if (write_flags & WIMLIB_WRITE_FLAG_USE_EXISTING_TOTALBYTES)
2359 DEBUG("\tUSE_EXISTING_TOTALBYTES");
2361 if (num_threads == 0)
2362 DEBUG("Number of threads: autodetect");
2364 DEBUG("Number of threads: %u", num_threads);
2365 DEBUG("Progress function: %s", (progress_func ? "yes" : "no"));
2366 DEBUG("Stream list: %s", (stream_list_override ? "specified" : "autodetect"));
2367 DEBUG("GUID: %s", ((guid || wim->guid_set_explicitly) ?
2368 "specified" : "generate new"));
2370 /* Internally, this is always called with a valid part number and total
2372 wimlib_assert(total_parts >= 1);
2373 wimlib_assert(part_number >= 1 && part_number <= total_parts);
2375 /* A valid image (or all images) must be specified. */
2376 if (image != WIMLIB_ALL_IMAGES &&
2377 (image < 1 || image > wim->hdr.image_count))
2378 return WIMLIB_ERR_INVALID_IMAGE;
2380 /* If we need to write metadata resources, make sure the ::WIMStruct has
2381 * the needed information attached (e.g. is not a resource-only WIM,
2382 * such as a non-first part of a split WIM). */
2383 if (!wim_has_metadata(wim) &&
2384 !(write_flags & WIMLIB_WRITE_FLAG_NO_METADATA))
2385 return WIMLIB_ERR_METADATA_NOT_FOUND;
2387 /* Check for contradictory flags. */
2388 if ((write_flags & (WIMLIB_WRITE_FLAG_CHECK_INTEGRITY |
2389 WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY))
2390 == (WIMLIB_WRITE_FLAG_CHECK_INTEGRITY |
2391 WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY))
2392 return WIMLIB_ERR_INVALID_PARAM;
2394 if ((write_flags & (WIMLIB_WRITE_FLAG_PIPABLE |
2395 WIMLIB_WRITE_FLAG_NOT_PIPABLE))
2396 == (WIMLIB_WRITE_FLAG_PIPABLE |
2397 WIMLIB_WRITE_FLAG_NOT_PIPABLE))
2398 return WIMLIB_ERR_INVALID_PARAM;
2400 if ((write_flags & (WIMLIB_WRITE_FLAG_PACK_STREAMS |
2401 WIMLIB_WRITE_FLAG_NO_PACK_STREAMS))
2402 == (WIMLIB_WRITE_FLAG_PACK_STREAMS |
2403 WIMLIB_WRITE_FLAG_NO_PACK_STREAMS))
2404 return WIMLIB_ERR_INVALID_PARAM;
2406 /* Save previous header, then start initializing the new one. */
2407 memcpy(&hdr_save, &wim->hdr, sizeof(struct wim_header));
2409 /* Set default integrity, pipable, and packed stream flags. */
2410 if (!(write_flags & (WIMLIB_WRITE_FLAG_PIPABLE |
2411 WIMLIB_WRITE_FLAG_NOT_PIPABLE)))
2412 if (wim_is_pipable(wim)) {
2413 DEBUG("WIM is pipable; default to PIPABLE.");
2414 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
2417 if (!(write_flags & (WIMLIB_WRITE_FLAG_CHECK_INTEGRITY |
2418 WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY)))
2419 if (wim_has_integrity_table(wim)) {
2420 DEBUG("Integrity table present; default to CHECK_INTEGRITY.");
2421 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2424 if (!(write_flags & (WIMLIB_WRITE_FLAG_PACK_STREAMS |
2425 WIMLIB_WRITE_FLAG_NO_PACK_STREAMS)))
2426 if (wim->hdr.wim_version == WIM_VERSION_PACKED_STREAMS) {
2427 DEBUG("WIM version 3584; default to PACK_STREAMS.");
2428 write_flags |= WIMLIB_WRITE_FLAG_PACK_STREAMS;
2431 if ((write_flags & (WIMLIB_WRITE_FLAG_PIPABLE |
2432 WIMLIB_WRITE_FLAG_PACK_STREAMS))
2433 == (WIMLIB_WRITE_FLAG_PIPABLE |
2434 WIMLIB_WRITE_FLAG_PACK_STREAMS))
2435 return WIMLIB_ERR_INVALID_PARAM;
2437 /* Set appropriate magic number. */
2438 if (write_flags & WIMLIB_WRITE_FLAG_PIPABLE)
2439 wim->hdr.magic = PWM_MAGIC;
2441 wim->hdr.magic = WIM_MAGIC;
2443 /* Set appropriate version number. */
2444 if (write_flags & WIMLIB_WRITE_FLAG_PACK_STREAMS)
2445 wim->hdr.wim_version = WIM_VERSION_PACKED_STREAMS;
2447 wim->hdr.wim_version = WIM_VERSION_DEFAULT;
2449 /* Clear header flags that will be set automatically. */
2450 wim->hdr.flags &= ~(WIM_HDR_FLAG_METADATA_ONLY |
2451 WIM_HDR_FLAG_RESOURCE_ONLY |
2452 WIM_HDR_FLAG_SPANNED |
2453 WIM_HDR_FLAG_WRITE_IN_PROGRESS);
2455 /* Set SPANNED header flag if writing part of a split WIM. */
2456 if (total_parts != 1)
2457 wim->hdr.flags |= WIM_HDR_FLAG_SPANNED;
2459 /* Set part number and total parts of split WIM. This will be 1 and 1
2460 * if the WIM is standalone. */
2461 wim->hdr.part_number = part_number;
2462 wim->hdr.total_parts = total_parts;
2464 /* Set compression type if different. */
2465 if (wim->compression_type != wim->out_compression_type) {
2466 ret = set_wim_hdr_cflags(wim->out_compression_type, &wim->hdr);
2467 wimlib_assert(ret == 0);
2470 /* Set chunk size if different. */
2471 wim->hdr.chunk_size = wim->out_chunk_size;
2473 /* Use GUID if specified; otherwise generate a new one. */
2475 memcpy(wim->hdr.guid, guid, WIMLIB_GUID_LEN);
2476 else if (!wim->guid_set_explicitly)
2477 randomize_byte_array(wim->hdr.guid, WIMLIB_GUID_LEN);
2479 /* Clear references to resources that have not been written yet. */
2480 zero_reshdr(&wim->hdr.lookup_table_reshdr);
2481 zero_reshdr(&wim->hdr.xml_data_reshdr);
2482 zero_reshdr(&wim->hdr.boot_metadata_reshdr);
2483 zero_reshdr(&wim->hdr.integrity_table_reshdr);
2485 /* Set image count and boot index correctly for single image writes. */
2486 if (image != WIMLIB_ALL_IMAGES) {
2487 wim->hdr.image_count = 1;
2488 if (wim->hdr.boot_idx == image)
2489 wim->hdr.boot_idx = 1;
2491 wim->hdr.boot_idx = 0;
2494 /* Split WIMs can't be bootable. */
2495 if (total_parts != 1)
2496 wim->hdr.boot_idx = 0;
2498 /* Initialize output file descriptor. */
2499 if (write_flags & WIMLIB_WRITE_FLAG_FILE_DESCRIPTOR) {
2500 /* File descriptor was explicitly provided. Return error if
2501 * file descriptor is not seekable, unless writing a pipable WIM
2503 wim->out_fd.fd = *(const int*)path_or_fd;
2504 wim->out_fd.offset = 0;
2505 if (!filedes_is_seekable(&wim->out_fd)) {
2506 ret = WIMLIB_ERR_INVALID_PARAM;
2507 if (!(write_flags & WIMLIB_WRITE_FLAG_PIPABLE))
2508 goto out_restore_hdr;
2509 if (write_flags & WIMLIB_WRITE_FLAG_CHECK_INTEGRITY) {
2510 ERROR("Can't include integrity check when "
2511 "writing pipable WIM to pipe!");
2512 goto out_restore_hdr;
2517 /* Filename of WIM to write was provided; open file descriptor
2519 ret = open_wim_writable(wim, (const tchar*)path_or_fd,
2520 O_TRUNC | O_CREAT | O_RDWR);
2522 goto out_restore_hdr;
2525 /* Write initial header. This is merely a "dummy" header since it
2526 * doesn't have all the information yet, so it will be overwritten later
2527 * (unless writing a pipable WIM). */
2528 if (!(write_flags & WIMLIB_WRITE_FLAG_PIPABLE))
2529 wim->hdr.flags |= WIM_HDR_FLAG_WRITE_IN_PROGRESS;
2530 ret = write_wim_header(&wim->hdr, &wim->out_fd);
2531 wim->hdr.flags &= ~WIM_HDR_FLAG_WRITE_IN_PROGRESS;
2533 goto out_restore_hdr;
2535 /* Write metadata resources and streams. */
2536 if (!(write_flags & WIMLIB_WRITE_FLAG_PIPABLE)) {
2537 /* Default case: create a normal (non-pipable) WIM. */
2538 ret = write_wim_streams(wim, image, write_flags, num_threads,
2539 progress_func, stream_list_override,
2540 &lookup_table_list);
2542 goto out_restore_hdr;
2544 ret = write_wim_metadata_resources(wim, image, write_flags,
2547 goto out_restore_hdr;
2549 /* Non-default case: create pipable WIM. */
2550 ret = write_pipable_wim(wim, image, write_flags, num_threads,
2551 progress_func, stream_list_override,
2552 &lookup_table_list);
2554 goto out_restore_hdr;
2555 write_flags |= WIMLIB_WRITE_FLAG_HEADER_AT_END;
2559 /* Write lookup table, XML data, and (optional) integrity table. */
2560 ret = finish_write(wim, image, write_flags, progress_func,
2561 &lookup_table_list);
2563 memcpy(&wim->hdr, &hdr_save, sizeof(struct wim_header));
2564 (void)close_wim_writable(wim, write_flags);
2565 DEBUG("ret=%d", ret);
2569 /* Write a standalone WIM to a file or file descriptor. */
2571 write_standalone_wim(WIMStruct *wim, const void *path_or_fd,
2572 int image, int write_flags, unsigned num_threads,
2573 wimlib_progress_func_t progress_func)
2575 return write_wim_part(wim, path_or_fd, image, write_flags,
2576 num_threads, progress_func, 1, 1, NULL, NULL);
2579 /* API function documented in wimlib.h */
2581 wimlib_write(WIMStruct *wim, const tchar *path,
2582 int image, int write_flags, unsigned num_threads,
2583 wimlib_progress_func_t progress_func)
2586 return WIMLIB_ERR_INVALID_PARAM;
2588 write_flags &= WIMLIB_WRITE_MASK_PUBLIC;
2590 return write_standalone_wim(wim, path, image, write_flags,
2591 num_threads, progress_func);
2594 /* API function documented in wimlib.h */
2596 wimlib_write_to_fd(WIMStruct *wim, int fd,
2597 int image, int write_flags, unsigned num_threads,
2598 wimlib_progress_func_t progress_func)
2601 return WIMLIB_ERR_INVALID_PARAM;
2603 write_flags &= WIMLIB_WRITE_MASK_PUBLIC;
2604 write_flags |= WIMLIB_WRITE_FLAG_FILE_DESCRIPTOR;
2606 return write_standalone_wim(wim, &fd, image, write_flags,
2607 num_threads, progress_func);
2611 any_images_modified(WIMStruct *wim)
2613 for (int i = 0; i < wim->hdr.image_count; i++)
2614 if (wim->image_metadata[i]->modified)
2620 check_resource_offset(struct wim_lookup_table_entry *lte, void *_wim)
2622 const WIMStruct *wim = _wim;
2623 off_t end_offset = *(const off_t*)wim->private;
2625 if (lte->resource_location == RESOURCE_IN_WIM && lte->rspec->wim == wim &&
2626 lte->rspec->offset_in_wim + lte->rspec->size_in_wim > end_offset)
2627 return WIMLIB_ERR_RESOURCE_ORDER;
2631 /* Make sure no file or metadata resources are located after the XML data (or
2632 * integrity table if present)--- otherwise we can't safely overwrite the WIM in
2633 * place and we return WIMLIB_ERR_RESOURCE_ORDER. */
2635 check_resource_offsets(WIMStruct *wim, off_t end_offset)
2640 wim->private = &end_offset;
2641 ret = for_lookup_table_entry(wim->lookup_table, check_resource_offset, wim);
2645 for (i = 0; i < wim->hdr.image_count; i++) {
2646 ret = check_resource_offset(wim->image_metadata[i]->metadata_lte, wim);
2654 * Overwrite a WIM, possibly appending streams to it.
2656 * A WIM looks like (or is supposed to look like) the following:
2658 * Header (212 bytes)
2659 * Streams and metadata resources (variable size)
2660 * Lookup table (variable size)
2661 * XML data (variable size)
2662 * Integrity table (optional) (variable size)
2664 * If we are not adding any streams or metadata resources, the lookup table is
2665 * unchanged--- so we only need to overwrite the XML data, integrity table, and
2666 * header. This operation is potentially unsafe if the program is abruptly
2667 * terminated while the XML data or integrity table are being overwritten, but
2668 * before the new header has been written. To partially alleviate this problem,
2669 * a special flag (WIMLIB_WRITE_FLAG_CHECKPOINT_AFTER_XML) is passed to
2670 * finish_write() to cause a temporary WIM header to be written after the XML
2671 * data has been written. This may prevent the WIM from becoming corrupted if
2672 * the program is terminated while the integrity table is being calculated (but
2673 * no guarantees, due to write re-ordering...).
2675 * If we are adding new streams or images (metadata resources), the lookup table
2676 * needs to be changed, and those streams need to be written. In this case, we
2677 * try to perform a safe update of the WIM file by writing the streams *after*
2678 * the end of the previous WIM, then writing the new lookup table, XML data, and
2679 * (optionally) integrity table following the new streams. This will produce a
2680 * layout like the following:
2682 * Header (212 bytes)
2683 * (OLD) Streams and metadata resources (variable size)
2684 * (OLD) Lookup table (variable size)
2685 * (OLD) XML data (variable size)
2686 * (OLD) Integrity table (optional) (variable size)
2687 * (NEW) Streams and metadata resources (variable size)
2688 * (NEW) Lookup table (variable size)
2689 * (NEW) XML data (variable size)
2690 * (NEW) Integrity table (optional) (variable size)
2692 * At all points, the WIM is valid as nothing points to the new data yet. Then,
2693 * the header is overwritten to point to the new lookup table, XML data, and
2694 * integrity table, to produce the following layout:
2696 * Header (212 bytes)
2697 * Streams and metadata resources (variable size)
2698 * Nothing (variable size)
2699 * More Streams and metadata resources (variable size)
2700 * Lookup table (variable size)
2701 * XML data (variable size)
2702 * Integrity table (optional) (variable size)
2704 * This method allows an image to be appended to a large WIM very quickly, and
2705 * is is crash-safe except in the case of write re-ordering, but the
2706 * disadvantage is that a small hole is left in the WIM where the old lookup
2707 * table, xml data, and integrity table were. (These usually only take up a
2708 * small amount of space compared to the streams, however.)
2711 overwrite_wim_inplace(WIMStruct *wim, int write_flags,
2712 unsigned num_threads,
2713 wimlib_progress_func_t progress_func)
2717 u64 old_lookup_table_end, old_xml_begin, old_xml_end;
2718 struct wim_header hdr_save;
2719 struct list_head stream_list;
2720 struct list_head lookup_table_list;
2721 struct filter_context filter_ctx;
2723 DEBUG("Overwriting `%"TS"' in-place", wim->filename);
2725 /* Set default integrity flag. */
2726 if (!(write_flags & (WIMLIB_WRITE_FLAG_CHECK_INTEGRITY |
2727 WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY)))
2728 if (wim_has_integrity_table(wim))
2729 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2731 /* Set default packed flag. */
2732 if (!(write_flags & (WIMLIB_WRITE_FLAG_PACK_STREAMS |
2733 WIMLIB_WRITE_FLAG_NO_PACK_STREAMS)))
2734 if (wim->hdr.wim_version == WIM_VERSION_PACKED_STREAMS)
2735 write_flags |= WIMLIB_WRITE_FLAG_PACK_STREAMS;
2737 /* Set additional flags for overwrite. */
2738 write_flags |= WIMLIB_WRITE_FLAG_OVERWRITE |
2739 WIMLIB_WRITE_FLAG_STREAMS_OK;
2741 /* Make sure that the integrity table (if present) is after the XML
2742 * data, and that there are no stream resources, metadata resources, or
2743 * lookup tables after the XML data. Otherwise, these data would be
2745 old_xml_begin = wim->hdr.xml_data_reshdr.offset_in_wim;
2746 old_xml_end = old_xml_begin + wim->hdr.xml_data_reshdr.size_in_wim;
2747 old_lookup_table_end = wim->hdr.lookup_table_reshdr.offset_in_wim +
2748 wim->hdr.lookup_table_reshdr.size_in_wim;
2749 if (wim->hdr.integrity_table_reshdr.offset_in_wim != 0 &&
2750 wim->hdr.integrity_table_reshdr.offset_in_wim < old_xml_end) {
2751 WARNING("Didn't expect the integrity table to be before the XML data");
2752 return WIMLIB_ERR_RESOURCE_ORDER;
2755 if (old_lookup_table_end > old_xml_begin) {
2756 WARNING("Didn't expect the lookup table to be after the XML data");
2757 return WIMLIB_ERR_RESOURCE_ORDER;
2760 /* Set @old_wim_end, which indicates the point beyond which we don't
2761 * allow any file and metadata resources to appear without returning
2762 * WIMLIB_ERR_RESOURCE_ORDER (due to the fact that we would otherwise
2763 * overwrite these resources). */
2764 if (!wim->deletion_occurred && !any_images_modified(wim)) {
2765 /* If no images have been modified and no images have been
2766 * deleted, a new lookup table does not need to be written. We
2767 * shall write the new XML data and optional integrity table
2768 * immediately after the lookup table. Note that this may
2769 * overwrite an existing integrity table. */
2770 DEBUG("Skipping writing lookup table "
2771 "(no images modified or deleted)");
2772 old_wim_end = old_lookup_table_end;
2773 write_flags |= WIMLIB_WRITE_FLAG_NO_LOOKUP_TABLE |
2774 WIMLIB_WRITE_FLAG_CHECKPOINT_AFTER_XML;
2775 } else if (wim->hdr.integrity_table_reshdr.offset_in_wim != 0) {
2776 /* Old WIM has an integrity table; begin writing new streams
2778 old_wim_end = wim->hdr.integrity_table_reshdr.offset_in_wim +
2779 wim->hdr.integrity_table_reshdr.size_in_wim;
2781 /* No existing integrity table; begin writing new streams after
2782 * the old XML data. */
2783 old_wim_end = old_xml_end;
2786 ret = check_resource_offsets(wim, old_wim_end);
2790 ret = prepare_stream_list_for_write(wim, WIMLIB_ALL_IMAGES, write_flags,
2791 &stream_list, &lookup_table_list,
2796 ret = open_wim_writable(wim, wim->filename, O_RDWR);
2800 ret = lock_wim(wim, wim->out_fd.fd);
2804 /* Save original header so it can be restored in case of error */
2805 memcpy(&hdr_save, &wim->hdr, sizeof(struct wim_header));
2807 /* Set WIM_HDR_FLAG_WRITE_IN_PROGRESS flag in header. */
2808 wim->hdr.flags |= WIM_HDR_FLAG_WRITE_IN_PROGRESS;
2809 ret = write_wim_header_flags(wim->hdr.flags, &wim->out_fd);
2811 ERROR_WITH_ERRNO("Error updating WIM header flags");
2812 goto out_restore_memory_hdr;
2815 if (filedes_seek(&wim->out_fd, old_wim_end) == -1) {
2816 ERROR_WITH_ERRNO("Can't seek to end of WIM");
2817 ret = WIMLIB_ERR_WRITE;
2818 goto out_restore_physical_hdr;
2821 ret = write_stream_list(&stream_list,
2823 write_flags_to_resource_flags(write_flags),
2824 wim->compression_type,
2834 ret = write_wim_metadata_resources(wim, WIMLIB_ALL_IMAGES,
2835 write_flags, progress_func);
2839 write_flags |= WIMLIB_WRITE_FLAG_REUSE_INTEGRITY_TABLE;
2840 ret = finish_write(wim, WIMLIB_ALL_IMAGES, write_flags,
2841 progress_func, &lookup_table_list);
2845 goto out_unlock_wim;
2848 if (!(write_flags & WIMLIB_WRITE_FLAG_NO_LOOKUP_TABLE)) {
2849 WARNING("Truncating `%"TS"' to its original size (%"PRIu64" bytes)",
2850 wim->filename, old_wim_end);
2851 /* Return value of ftruncate() is ignored because this is
2852 * already an error path. */
2853 (void)ftruncate(wim->out_fd.fd, old_wim_end);
2855 out_restore_physical_hdr:
2856 (void)write_wim_header_flags(hdr_save.flags, &wim->out_fd);
2857 out_restore_memory_hdr:
2858 memcpy(&wim->hdr, &hdr_save, sizeof(struct wim_header));
2860 (void)close_wim_writable(wim, write_flags);
2862 wim->wim_locked = 0;
2867 overwrite_wim_via_tmpfile(WIMStruct *wim, int write_flags,
2868 unsigned num_threads,
2869 wimlib_progress_func_t progress_func)
2871 size_t wim_name_len;
2874 DEBUG("Overwriting `%"TS"' via a temporary file", wim->filename);
2876 /* Write the WIM to a temporary file in the same directory as the
2878 wim_name_len = tstrlen(wim->filename);
2879 tchar tmpfile[wim_name_len + 10];
2880 tmemcpy(tmpfile, wim->filename, wim_name_len);
2881 randomize_char_array_with_alnum(tmpfile + wim_name_len, 9);
2882 tmpfile[wim_name_len + 9] = T('\0');
2884 ret = wimlib_write(wim, tmpfile, WIMLIB_ALL_IMAGES,
2885 write_flags | WIMLIB_WRITE_FLAG_FSYNC,
2886 num_threads, progress_func);
2894 /* Rename the new WIM file to the original WIM file. Note: on Windows
2895 * this actually calls win32_rename_replacement(), not _wrename(), so
2896 * that removing the existing destination file can be handled. */
2897 DEBUG("Renaming `%"TS"' to `%"TS"'", tmpfile, wim->filename);
2898 ret = trename(tmpfile, wim->filename);
2900 ERROR_WITH_ERRNO("Failed to rename `%"TS"' to `%"TS"'",
2901 tmpfile, wim->filename);
2908 return WIMLIB_ERR_RENAME;
2911 if (progress_func) {
2912 union wimlib_progress_info progress;
2913 progress.rename.from = tmpfile;
2914 progress.rename.to = wim->filename;
2915 progress_func(WIMLIB_PROGRESS_MSG_RENAME, &progress);
2921 can_overwrite_wim_inplace(const WIMStruct *wim, int write_flags)
2923 if (write_flags & WIMLIB_WRITE_FLAG_REBUILD)
2926 if (wim->deletion_occurred && !(write_flags & WIMLIB_WRITE_FLAG_SOFT_DELETE))
2929 if (wim_is_pipable(wim) || (write_flags & WIMLIB_WRITE_FLAG_PIPABLE))
2932 if (wim->hdr.wim_version != WIM_VERSION_PACKED_STREAMS) {
2933 if (wim->compression_type != wim->out_compression_type)
2935 if (wim->chunk_size != wim->out_chunk_size)
2938 if (write_flags & WIMLIB_WRITE_FLAG_NO_PACK_STREAMS)
2945 /* API function documented in wimlib.h */
2947 wimlib_overwrite(WIMStruct *wim, int write_flags,
2948 unsigned num_threads,
2949 wimlib_progress_func_t progress_func)
2954 write_flags &= WIMLIB_WRITE_MASK_PUBLIC;
2956 if (write_flags & WIMLIB_WRITE_FLAG_FILE_DESCRIPTOR)
2957 return WIMLIB_ERR_INVALID_PARAM;
2960 return WIMLIB_ERR_NO_FILENAME;
2962 orig_hdr_flags = wim->hdr.flags;
2963 if (write_flags & WIMLIB_WRITE_FLAG_IGNORE_READONLY_FLAG)
2964 wim->hdr.flags &= ~WIM_HDR_FLAG_READONLY;
2965 ret = can_modify_wim(wim);
2966 wim->hdr.flags = orig_hdr_flags;
2970 if (can_overwrite_wim_inplace(wim, write_flags)) {
2971 ret = overwrite_wim_inplace(wim, write_flags, num_threads,
2973 if (ret != WIMLIB_ERR_RESOURCE_ORDER)
2975 WARNING("Falling back to re-building entire WIM");
2977 return overwrite_wim_via_tmpfile(wim, write_flags, num_threads,