- * @is_partial_res is %true if this stream is just one of multiple in a single
- * WIM resource being extracted. */
-static bool
-need_tmpfile_to_extract(struct wim_lookup_table_entry *lte,
- bool is_partial_res)
-{
- /* Temporary file is always required when reading a partial resource,
- * since in that case we retrieve all the contained streams in one pass.
- * */
- if (is_partial_res)
- return true;
-
- /* Otherwise we don't need a temporary file if only a single instance of
- * the stream is needed. */
- if (lte->out_refcnt == 1)
- return false;
-
- wimlib_assert(lte->out_refcnt >= 2);
-
- /* We also don't need a temporary file if random access to the stream is
- * allowed. */
- if (lte->resource_location != RESOURCE_IN_WIM ||
- filedes_is_seekable(<e->rspec->wim->in_fd))
- return false;
-
- return true;
-}
-
-static int
-begin_extract_stream_to_tmpfile(struct wim_lookup_table_entry *lte,
- bool is_partial_res,
- void *_ctx)
-{
- struct apply_ctx *ctx = _ctx;
- int ret;
-
- if (!need_tmpfile_to_extract(lte, is_partial_res)) {
- DEBUG("Temporary file not needed "
- "for stream (size=%"PRIu64")", lte->size);
- ret = extract_stream_instances(lte, lte, ctx);
- if (ret)
- return ret;
-
- return BEGIN_STREAM_STATUS_SKIP_STREAM;
- }
-
- DEBUG("Temporary file needed for stream (size=%"PRIu64")", lte->size);
- return create_temporary_file(&ctx->tmpfile_fd, &ctx->tmpfile_name);
-}
-
-static int
-end_extract_stream_to_tmpfile(struct wim_lookup_table_entry *lte,
- int status, void *_ctx)
-{
- struct apply_ctx *ctx = _ctx;
- struct wim_lookup_table_entry lte_override;
- int ret;
- int errno_save = errno;
-
- ret = filedes_close(&ctx->tmpfile_fd);
-
- if (status) {
- ret = status;
- errno = errno_save;
- goto out_delete_tmpfile;
- }
-
- 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_FILE_ORDER)) {
- /* 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),
- &cbs, VERIFY_STREAM_HASHES);
- } 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 (le64_to_cpu(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)