+ref_stream(struct wim_lookup_table_entry *lte,
+ struct wim_dentry *dentry, struct apply_ctx *ctx)
+{
+ if (!lte)
+ return 0;
+
+ /* Tally the size only for each extraction of the stream (not hard
+ * links). */
+ if (!(dentry->d_inode->i_visited &&
+ ctx->supported_features.hard_links) &&
+ (!is_linked_extraction(ctx) || (lte->out_refcnt == 0 &&
+ lte->extracted_file == NULL)))
+ {
+ ctx->progress.extract.total_bytes += lte->size;
+ ctx->progress.extract.num_streams++;
+ }
+
+ /* Add stream to the dentry_list only one time, even if it's going
+ * to be extracted to multiple locations. */
+ if (lte->out_refcnt == 0) {
+ list_add_tail(<e->extraction_list, &ctx->stream_list);
+ ctx->num_streams_remaining++;
+ }
+
+ if (!(ctx->extract_flags & WIMLIB_EXTRACT_FLAG_FILE_ORDER)) {
+ struct wim_dentry **lte_dentries;
+
+ /* Append dentry to this stream's array of dentries referencing
+ * it. Use inline array to avoid memory allocation until the
+ * number of dentries becomes too large. */
+ if (lte->out_refcnt < ARRAY_LEN(lte->inline_lte_dentries)) {
+ lte_dentries = lte->inline_lte_dentries;
+ } else {
+ struct wim_dentry **prev_lte_dentries;
+ size_t alloc_lte_dentries;
+
+ if (lte->out_refcnt == ARRAY_LEN(lte->inline_lte_dentries)) {
+ prev_lte_dentries = NULL;
+ alloc_lte_dentries = ARRAY_LEN(lte->inline_lte_dentries);
+ } else {
+ prev_lte_dentries = lte->lte_dentries;
+ alloc_lte_dentries = lte->alloc_lte_dentries;
+ }
+
+ if (lte->out_refcnt == alloc_lte_dentries) {
+ alloc_lte_dentries *= 2;
+ lte_dentries = REALLOC(prev_lte_dentries,
+ alloc_lte_dentries *
+ sizeof(lte_dentries[0]));
+ if (lte_dentries == NULL)
+ return WIMLIB_ERR_NOMEM;
+ if (prev_lte_dentries == NULL) {
+ memcpy(lte_dentries,
+ lte->inline_lte_dentries,
+ sizeof(lte->inline_lte_dentries));
+ }
+ lte->lte_dentries = lte_dentries;
+ lte->alloc_lte_dentries = alloc_lte_dentries;
+ }
+ lte_dentries = lte->lte_dentries;
+ }
+ lte_dentries[lte->out_refcnt] = dentry;
+ }
+ lte->out_refcnt++;
+ return 0;
+}
+
+static int
+dentry_ref_streams(struct wim_dentry *dentry, struct apply_ctx *ctx)
+{
+ struct wim_inode *inode = dentry->d_inode;
+ int ret;
+
+ /* The unnamed data stream will always be extracted, except in an
+ * unlikely case. */
+ if (!inode_is_encrypted_directory(inode)) {
+ ret = ref_stream(inode_unnamed_lte_resolved(inode),
+ dentry, ctx);
+ if (ret)
+ return ret;
+ }
+
+ /* Named data streams will be extracted only if supported in the current
+ * extraction mode and volume, and to avoid complications, if not doing
+ * a linked extraction. */
+ if (can_extract_named_data_streams(ctx)) {
+ for (u16 i = 0; i < inode->i_num_ads; i++) {
+ if (!ads_entry_is_named_stream(&inode->i_ads_entries[i]))
+ continue;
+ ret = ref_stream(inode->i_ads_entries[i].lte,
+ dentry, ctx);
+ if (ret)
+ return ret;
+ }
+ }
+ inode->i_visited = 1;
+ return 0;
+}
+
+/*
+ * For each dentry to be extracted, iterate through the data streams of the
+ * corresponding inode. For each such stream that is not to be ignored due to
+ * the supported features or extraction flags, add it to the list of streams to
+ * be extracted (ctx->stream_list) if not already done so.
+ *
+ * Also, if doing a sequential extraction, build a mapping from each stream to
+ * the dentries referencing it.
+ *
+ * This also initializes the extract progress info with byte and stream
+ * information.
+ *
+ * Note: This has a dependency on start_extract being called because
+ * ctx.supported_features must be filled in in order to determine whether named
+ * data streams are supported.
+ *
+ * Note: this uses the i_visited member of the inodes (assumed to be 0
+ * initially), but does not reset it.
+ *
+ * Possible error codes: WIMLIB_ERR_NOMEM.
+ */
+static int
+dentry_list_ref_streams(struct list_head *dentry_list, struct apply_ctx *ctx)
+{
+ struct wim_dentry *dentry;
+ int ret;
+
+ list_for_each_entry(dentry, dentry_list, extraction_list) {
+ ret = dentry_ref_streams(dentry, ctx);
+ if (ret)
+ return ret;
+ }
+ return 0;
+}
+
+/* Tally features necessary to extract a dentry and the corresponding inode. */
+static void
+dentry_tally_features(struct wim_dentry *dentry, struct wim_features *features)