+ DEBUG("Number of threads: %u", num_threads);
+ DEBUG("Progress function: %s", (progress_func ? "yes" : "no"));
+ DEBUG("Stream list: %s", (stream_list_override ? "specified" : "autodetect"));
+ DEBUG("GUID: %s", ((guid || wim->guid_set_explicitly) ?
+ "specified" : "generate new"));
+
+ /* Internally, this is always called with a valid part number and total
+ * parts. */
+ wimlib_assert(total_parts >= 1);
+ wimlib_assert(part_number >= 1 && part_number <= total_parts);
+
+ /* A valid image (or all images) must be specified. */
+ if (image != WIMLIB_ALL_IMAGES &&
+ (image < 1 || image > wim->hdr.image_count))
+ return WIMLIB_ERR_INVALID_IMAGE;
+
+ /* If we need to write metadata resources, make sure the ::WIMStruct has
+ * the needed information attached (e.g. is not a resource-only WIM,
+ * such as a non-first part of a split WIM). */
+ if (!wim_has_metadata(wim) &&
+ !(write_flags & WIMLIB_WRITE_FLAG_NO_METADATA))
+ return WIMLIB_ERR_METADATA_NOT_FOUND;
+
+ /* Check for contradictory flags. */
+ if ((write_flags & (WIMLIB_WRITE_FLAG_CHECK_INTEGRITY |
+ WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY))
+ == (WIMLIB_WRITE_FLAG_CHECK_INTEGRITY |
+ WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY))
+ return WIMLIB_ERR_INVALID_PARAM;
+
+ if ((write_flags & (WIMLIB_WRITE_FLAG_PIPABLE |
+ WIMLIB_WRITE_FLAG_NOT_PIPABLE))
+ == (WIMLIB_WRITE_FLAG_PIPABLE |
+ WIMLIB_WRITE_FLAG_NOT_PIPABLE))
+ return WIMLIB_ERR_INVALID_PARAM;
+
+ if ((write_flags & (WIMLIB_WRITE_FLAG_PACK_STREAMS |
+ WIMLIB_WRITE_FLAG_NO_PACK_STREAMS))
+ == (WIMLIB_WRITE_FLAG_PACK_STREAMS |
+ WIMLIB_WRITE_FLAG_NO_PACK_STREAMS))
+ return WIMLIB_ERR_INVALID_PARAM;
+
+ /* Save previous header, then start initializing the new one. */
+ memcpy(&hdr_save, &wim->hdr, sizeof(struct wim_header));
+
+ /* Set default integrity, pipable, and packed stream flags. */
+ if (!(write_flags & (WIMLIB_WRITE_FLAG_PIPABLE |
+ WIMLIB_WRITE_FLAG_NOT_PIPABLE)))
+ if (wim_is_pipable(wim)) {
+ DEBUG("WIM is pipable; default to PIPABLE.");
+ write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
+ }
+
+ if (!(write_flags & (WIMLIB_WRITE_FLAG_CHECK_INTEGRITY |
+ WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY)))
+ if (wim_has_integrity_table(wim)) {
+ DEBUG("Integrity table present; default to CHECK_INTEGRITY.");
+ write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
+ }
+
+ if (!(write_flags & (WIMLIB_WRITE_FLAG_PACK_STREAMS |
+ WIMLIB_WRITE_FLAG_NO_PACK_STREAMS)))
+ if (wim->hdr.wim_version == WIM_VERSION_PACKED_STREAMS) {
+ DEBUG("WIM version 3584; default to PACK_STREAMS.");
+ write_flags |= WIMLIB_WRITE_FLAG_PACK_STREAMS;
+ }
+
+ if ((write_flags & (WIMLIB_WRITE_FLAG_PIPABLE |
+ WIMLIB_WRITE_FLAG_PACK_STREAMS))
+ == (WIMLIB_WRITE_FLAG_PIPABLE |
+ WIMLIB_WRITE_FLAG_PACK_STREAMS))
+ {
+ ERROR("Cannot specify both PIPABLE and PACK_STREAMS!");
+ return WIMLIB_ERR_INVALID_PARAM;
+ }
+
+ /* Set appropriate magic number. */
+ if (write_flags & WIMLIB_WRITE_FLAG_PIPABLE)
+ wim->hdr.magic = PWM_MAGIC;
+ else
+ wim->hdr.magic = WIM_MAGIC;
+
+ /* Set appropriate version number. */
+ if (write_flags & WIMLIB_WRITE_FLAG_PACK_STREAMS)
+ wim->hdr.wim_version = WIM_VERSION_PACKED_STREAMS;
+ else
+ wim->hdr.wim_version = WIM_VERSION_DEFAULT;
+
+ /* Clear header flags that will be set automatically. */
+ wim->hdr.flags &= ~(WIM_HDR_FLAG_METADATA_ONLY |
+ WIM_HDR_FLAG_RESOURCE_ONLY |
+ WIM_HDR_FLAG_SPANNED |
+ WIM_HDR_FLAG_WRITE_IN_PROGRESS);
+
+ /* Set SPANNED header flag if writing part of a split WIM. */
+ if (total_parts != 1)
+ wim->hdr.flags |= WIM_HDR_FLAG_SPANNED;
+
+ /* Set part number and total parts of split WIM. This will be 1 and 1
+ * if the WIM is standalone. */
+ wim->hdr.part_number = part_number;
+ wim->hdr.total_parts = total_parts;
+
+ /* Set compression type if different. */
+ if (wim->compression_type != wim->out_compression_type) {
+ ret = set_wim_hdr_cflags(wim->out_compression_type, &wim->hdr);
+ wimlib_assert(ret == 0);
+ }
+
+ /* Set chunk size if different. */
+ wim->hdr.chunk_size = wim->out_chunk_size;
+
+ /* Use GUID if specified; otherwise generate a new one. */
+ if (guid)
+ memcpy(wim->hdr.guid, guid, WIMLIB_GUID_LEN);
+ else if (!wim->guid_set_explicitly)
+ randomize_byte_array(wim->hdr.guid, WIMLIB_GUID_LEN);
+
+ /* Clear references to resources that have not been written yet. */
+ zero_reshdr(&wim->hdr.lookup_table_reshdr);
+ zero_reshdr(&wim->hdr.xml_data_reshdr);
+ zero_reshdr(&wim->hdr.boot_metadata_reshdr);
+ zero_reshdr(&wim->hdr.integrity_table_reshdr);
+
+ /* Set image count and boot index correctly for single image writes. */
+ if (image != WIMLIB_ALL_IMAGES) {
+ wim->hdr.image_count = 1;
+ if (wim->hdr.boot_idx == image)
+ wim->hdr.boot_idx = 1;
+ else
+ wim->hdr.boot_idx = 0;
+ }
+
+ /* Split WIMs can't be bootable. */
+ if (total_parts != 1)
+ wim->hdr.boot_idx = 0;
+
+ /* Initialize output file descriptor. */
+ if (write_flags & WIMLIB_WRITE_FLAG_FILE_DESCRIPTOR) {
+ /* File descriptor was explicitly provided. Return error if
+ * file descriptor is not seekable, unless writing a pipable WIM
+ * was requested. */
+ wim->out_fd.fd = *(const int*)path_or_fd;
+ wim->out_fd.offset = 0;
+ if (!filedes_is_seekable(&wim->out_fd)) {
+ ret = WIMLIB_ERR_INVALID_PARAM;
+ if (!(write_flags & WIMLIB_WRITE_FLAG_PIPABLE))
+ goto out_restore_hdr;
+ if (write_flags & WIMLIB_WRITE_FLAG_CHECK_INTEGRITY) {
+ ERROR("Can't include integrity check when "
+ "writing pipable WIM to pipe!");
+ goto out_restore_hdr;
+ }
+ }
+
+ } else {
+ /* Filename of WIM to write was provided; open file descriptor
+ * to it. */
+ ret = open_wim_writable(wim, (const tchar*)path_or_fd,
+ O_TRUNC | O_CREAT | O_RDWR);
+ if (ret)
+ goto out_restore_hdr;
+ }
+
+ /* Write initial header. This is merely a "dummy" header since it
+ * doesn't have all the information yet, so it will be overwritten later
+ * (unless writing a pipable WIM). */
+ if (!(write_flags & WIMLIB_WRITE_FLAG_PIPABLE))
+ wim->hdr.flags |= WIM_HDR_FLAG_WRITE_IN_PROGRESS;
+ ret = write_wim_header(&wim->hdr, &wim->out_fd);
+ wim->hdr.flags &= ~WIM_HDR_FLAG_WRITE_IN_PROGRESS;