- if (ret) {
- ERROR_WITH_ERRNO("Error writing temporary file %"TS, ctx->tmpfile_name);
- ret = WIMLIB_ERR_WRITE;
- goto out_delete_tmpfile;
- }
-
- /* Now that a full stream has been extracted to a temporary file,
- * extract all instances of it to the actual target. */
-
- memcpy(<e_override, lte, sizeof(struct wim_lookup_table_entry));
- lte_override.resource_location = RESOURCE_IN_FILE_ON_DISK;
- lte_override.file_on_disk = ctx->tmpfile_name;
-
- ret = extract_stream_instances(lte, <e_override, ctx);
-
-out_delete_tmpfile:
- errno_save = errno;
- tunlink(ctx->tmpfile_name);
- FREE(ctx->tmpfile_name);
- errno = errno_save;
- return ret;
-}
-
-/* Extracts a list of streams (ctx.stream_list), assuming that the directory
- * structure and empty files were already created. This relies on the
- * per-`struct wim_lookup_table_entry' list of dentries that reference each
- * stream that was constructed earlier. */
-static int
-extract_stream_list(struct apply_ctx *ctx)
-{
- if (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_SEQUENTIAL) {
- /* Sequential extraction: read the streams in the order in which
- * they appear in the WIM file. */
- struct read_stream_list_callbacks cbs = {
- .begin_stream = begin_extract_stream_to_tmpfile,
- .begin_stream_ctx = ctx,
- .consume_chunk = extract_chunk_to_fd,
- .consume_chunk_ctx = &ctx->tmpfile_fd,
- .end_stream = end_extract_stream_to_tmpfile,
- .end_stream_ctx = ctx,
- };
- return read_stream_list(&ctx->stream_list,
- offsetof(struct wim_lookup_table_entry, extraction_list),
- 0, &cbs);
- } else {
- /* Extract the streams in unsorted order. */
- struct wim_lookup_table_entry *lte;
- int ret;
-
- list_for_each_entry(lte, &ctx->stream_list, extraction_list) {
- ret = extract_stream_instances(lte, lte, ctx);
- if (ret)
- return ret;
- }
- return 0;
- }
-}
-
-#define PWM_ALLOW_WIM_HDR 0x00001
-#define PWM_SILENT_EOF 0x00002
-
-/* Read the header from a stream in a pipable WIM. */
-static int
-read_pwm_stream_header(WIMStruct *pwm, struct wim_lookup_table_entry *lte,
- struct wim_resource_spec *rspec,
- int flags, struct wim_header_disk *hdr_ret)
-{
- union {
- struct pwm_stream_hdr stream_hdr;
- struct wim_header_disk pwm_hdr;
- } buf;
- struct wim_reshdr reshdr;
- int ret;
-
- ret = full_read(&pwm->in_fd, &buf.stream_hdr, sizeof(buf.stream_hdr));
- if (ret)
- goto read_error;
-
- if ((flags & PWM_ALLOW_WIM_HDR) && buf.stream_hdr.magic == PWM_MAGIC) {
- BUILD_BUG_ON(sizeof(buf.pwm_hdr) < sizeof(buf.stream_hdr));
- ret = full_read(&pwm->in_fd, &buf.stream_hdr + 1,
- sizeof(buf.pwm_hdr) - sizeof(buf.stream_hdr));
-
- if (ret)
- goto read_error;
- lte->resource_location = RESOURCE_NONEXISTENT;
- memcpy(hdr_ret, &buf.pwm_hdr, sizeof(buf.pwm_hdr));
- return 0;
- }
-
- if (buf.stream_hdr.magic != PWM_STREAM_MAGIC) {
- ERROR("Data read on pipe is invalid (expected stream header).");
- return WIMLIB_ERR_INVALID_PIPABLE_WIM;
- }
-
- copy_hash(lte->hash, buf.stream_hdr.hash);
-
- reshdr.size_in_wim = 0;
- reshdr.flags = le32_to_cpu(buf.stream_hdr.flags);
- reshdr.offset_in_wim = pwm->in_fd.offset;
- reshdr.uncompressed_size = le64_to_cpu(buf.stream_hdr.uncompressed_size);
- wim_res_hdr_to_spec(&reshdr, pwm, rspec);
- lte_bind_wim_resource_spec(lte, rspec);
- lte->flags = rspec->flags;
- lte->size = rspec->uncompressed_size;
- lte->offset_in_res = 0;
- return 0;
-
-read_error:
- if (ret != WIMLIB_ERR_UNEXPECTED_END_OF_FILE || !(flags & PWM_SILENT_EOF))
- ERROR_WITH_ERRNO("Error reading pipable WIM from pipe");
- return ret;
-}
-
-static int
-extract_streams_from_pipe(struct apply_ctx *ctx)
-{
- struct wim_lookup_table_entry *found_lte;
- struct wim_resource_spec *rspec;
- struct wim_lookup_table_entry *needed_lte;
- struct wim_lookup_table *lookup_table;
- struct wim_header_disk pwm_hdr;
- int ret;
- int pwm_flags;
-
- ret = WIMLIB_ERR_NOMEM;
- found_lte = new_lookup_table_entry();
- if (found_lte == NULL)
- goto out;
-
- rspec = MALLOC(sizeof(struct wim_resource_spec));
- if (rspec == NULL)
- goto out_free_found_lte;
-
- lookup_table = ctx->wim->lookup_table;
- pwm_flags = PWM_ALLOW_WIM_HDR;
- if ((ctx->extract_flags & WIMLIB_EXTRACT_FLAG_RESUME))
- pwm_flags |= PWM_SILENT_EOF;
- memcpy(ctx->progress.extract.guid, ctx->wim->hdr.guid, WIM_GID_LEN);
- ctx->progress.extract.part_number = ctx->wim->hdr.part_number;
- ctx->progress.extract.total_parts = ctx->wim->hdr.total_parts;
- if (ctx->progress_func)
- ctx->progress_func(WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN,
- &ctx->progress);
- while (ctx->num_streams_remaining) {
- if (found_lte->resource_location != RESOURCE_NONEXISTENT)
- lte_unbind_wim_resource_spec(found_lte);
- ret = read_pwm_stream_header(ctx->wim, found_lte, rspec,
- pwm_flags, &pwm_hdr);
- if (ret) {
- if (ret == WIMLIB_ERR_UNEXPECTED_END_OF_FILE &&
- (ctx->extract_flags & WIMLIB_EXTRACT_FLAG_RESUME))
- {
- goto resume_done;
- }
- goto out_free_found_lte;
- }
-
- if ((found_lte->resource_location != RESOURCE_NONEXISTENT)
- && !(found_lte->flags & WIM_RESHDR_FLAG_METADATA)
- && (needed_lte = lookup_resource(lookup_table, found_lte->hash))
- && (needed_lte->out_refcnt))
- {
- char *tmpfile_name = NULL;
- struct wim_lookup_table_entry *lte_override;
- struct wim_lookup_table_entry tmpfile_lte;
-
- needed_lte->offset_in_res = found_lte->offset_in_res;
- needed_lte->flags = found_lte->flags;
- needed_lte->size = found_lte->size;
-
- lte_unbind_wim_resource_spec(found_lte);
- lte_bind_wim_resource_spec(needed_lte, rspec);
-
- if (needed_lte->out_refcnt > 1) {
-
- struct filedes tmpfile_fd;
-
- /* Extract stream to temporary file. */
- ret = create_temporary_file(&tmpfile_fd, &tmpfile_name);
- if (ret)
- goto out_free_found_lte;
-
- ret = extract_stream_to_fd(needed_lte, &tmpfile_fd,
- needed_lte->size);
- if (ret) {
- filedes_close(&tmpfile_fd);
- goto delete_tmpfile;
- }
-
- if (filedes_close(&tmpfile_fd)) {
- ERROR_WITH_ERRNO("Error writing to temporary "
- "file \"%"TS"\"", tmpfile_name);
- ret = WIMLIB_ERR_WRITE;
- goto delete_tmpfile;
- }
- memcpy(&tmpfile_lte, needed_lte,
- sizeof(struct wim_lookup_table_entry));
- tmpfile_lte.resource_location = RESOURCE_IN_FILE_ON_DISK;
- tmpfile_lte.file_on_disk = tmpfile_name;
- lte_override = &tmpfile_lte;
- } else {
- lte_override = needed_lte;
- }
-
- ret = extract_stream_instances(needed_lte, lte_override, ctx);
- delete_tmpfile:
- lte_unbind_wim_resource_spec(needed_lte);
- if (tmpfile_name) {
- tunlink(tmpfile_name);
- FREE(tmpfile_name);
- }
- if (ret)
- goto out_free_found_lte;
- ctx->num_streams_remaining--;
- } else if (found_lte->resource_location != RESOURCE_NONEXISTENT) {
- ret = skip_wim_stream(found_lte);
- if (ret)
- goto out_free_found_lte;
- } else {
- u16 part_number = le16_to_cpu(pwm_hdr.part_number);
- u16 total_parts = le16_to_cpu(pwm_hdr.total_parts);
-
- if (part_number != ctx->progress.extract.part_number ||
- total_parts != ctx->progress.extract.total_parts ||
- memcmp(pwm_hdr.guid, ctx->progress.extract.guid,
- WIM_GID_LEN))
- {
- ctx->progress.extract.part_number = part_number;
- ctx->progress.extract.total_parts = total_parts;
- memcpy(ctx->progress.extract.guid,
- pwm_hdr.guid, WIM_GID_LEN);
- if (ctx->progress_func) {
- ctx->progress_func(
- WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN,
- &ctx->progress);
- }
-
- }