- inode_for_each_dentry(other_dentry, dentry->d_inode) {
- if (dentry_has_short_name(other_dentry)
- && !other_dentry->skeleton_extracted
- && other_dentry->in_extraction_tree
- && !other_dentry->extraction_skipped)
- {
- DEBUG("Creating %"TS" before %"TS" "
- "to guarantee correct DOS name extraction",
- dentry_full_path(other_dentry),
- dentry_full_path(dentry));
- orig_dentry = dentry;
- dentry = other_dentry;
- break;
- }
- }
- }
-again:
- if (!build_extraction_path(path, dentry, ctx))
- return 0;
- ret = do_dentry_extract_skeleton(path, dentry, ctx);
- if (ret)
- return ret;
-
- dentry->skeleton_extracted = 1;
-
- if (orig_dentry) {
- dentry = orig_dentry;
- orig_dentry = NULL;
- goto again;
- }
- dentry->d_inode->i_dos_name_extracted = 1;
- return 0;
-}
-
-static int
-dentry_extract_dir_skeleton(struct wim_dentry *dentry, void *_ctx)
-{
- if (dentry->d_inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)
- return dentry_extract_skeleton(dentry, _ctx);
- return 0;
-}
-
-/* Create a file or directory, then immediately extract all streams. The WIM
- * may not be read sequentially by this function. */
-static int
-dentry_extract(struct wim_dentry *dentry, void *_ctx)
-{
- struct apply_ctx *ctx = _ctx;
- tchar path[ctx->ops->path_max];
- int ret;
-
- ret = dentry_extract_skeleton(dentry, ctx);
- if (ret)
- return ret;
-
- if (!build_extraction_path(path, dentry, ctx))
- return 0;
-
- return extract_streams(path, ctx, dentry, NULL, NULL);
-}
-
-/* Creates a temporary file opened for writing. The open file descriptor is
- * returned in @fd_ret and its name is returned in @name_ret (dynamically
- * allocated). */
-static int
-create_temporary_file(struct filedes *fd_ret, tchar **name_ret)
-{
- tchar *name;
- int raw_fd;
-
-retry:
- name = ttempnam(NULL, T("wimlib"));
- if (name == NULL) {
- ERROR_WITH_ERRNO("Failed to create temporary filename");
- return WIMLIB_ERR_NOMEM;
- }
-
- raw_fd = topen(name, O_WRONLY | O_CREAT | O_EXCL | O_BINARY, 0600);
-
- if (raw_fd < 0) {
- if (errno == EEXIST) {
- FREE(name);
- goto retry;
- }
- ERROR_WITH_ERRNO("Failed to open temporary file \"%"TS"\"", name);
- FREE(name);
- return WIMLIB_ERR_OPEN;
- }
-
- filedes_init(fd_ret, raw_fd);
- *name_ret = name;
- return 0;
-}
-
-/* Extract all instances of the stream @lte that are being extracted in this
- * call of extract_tree(), but actually read the stream data from @lte_override.
- */
-static int
-extract_stream_instances(struct wim_lookup_table_entry *lte,
- struct wim_lookup_table_entry *lte_override,
- struct apply_ctx *ctx)
-{
- struct wim_dentry **lte_dentries;
- tchar path[ctx->ops->path_max];
- size_t i;
- int ret;
-
- if (lte->out_refcnt <= ARRAY_LEN(lte->inline_lte_dentries))
- lte_dentries = lte->inline_lte_dentries;
- else
- lte_dentries = lte->lte_dentries;
-
- for (i = 0; i < lte->out_refcnt; i++) {
- struct wim_dentry *dentry = lte_dentries[i];
-
- if (dentry->tmp_flag)
- continue;
- if (!build_extraction_path(path, dentry, ctx))
- continue;
- ret = extract_streams(path, ctx, dentry, lte, lte_override);
- if (ret)
- goto out_clear_tmp_flags;
- dentry->tmp_flag = 1;
- }
- ret = 0;
-out_clear_tmp_flags:
- for (i = 0; i < lte->out_refcnt; i++)
- lte_dentries[i]->tmp_flag = 0;
- return ret;
-}
-
-/* Determine whether the specified stream needs to be extracted to a temporary
- * file or not.
- *
- * @lte->out_refcnt specifies the number of instances of this stream that must
- * be extracted.
- *
- * @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;