+ /* Special case: when extracting from a pipe, the WIM lookup table is
+ * initially empty, so "resolving" an inode's streams is initially not
+ * possible. However, we still need to keep track of which streams,
+ * identified by SHA1 message digests, need to be extracted, so we
+ * "resolve" the inode's streams anyway by allocating new entries. */
+ if (extract_flags & WIMLIB_EXTRACT_FLAG_FROM_PIPE)
+ force = true;
+ ret = inode_resolve_streams(inode, lookup_table, force);
+ if (ret)
+ return ret;
+ for (u32 i = 0; i <= inode->i_num_ads; i++) {
+ lte = inode_stream_lte_resolved(inode, i);
+ if (lte)
+ lte->out_refcnt = 0;
+ }
+ return 0;
+}
+
+/*
+ * For each dentry to be extracted, resolve all streams in the corresponding
+ * inode and set 'out_refcnt' in each to 0.
+ *
+ * Possible error codes: WIMLIB_ERR_RESOURCE_NOT_FOUND, WIMLIB_ERR_NOMEM.
+ */
+static int
+dentry_list_resolve_streams(struct list_head *dentry_list,
+ struct apply_ctx *ctx)
+{
+ struct wim_dentry *dentry;
+ int ret;
+
+ list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
+ ret = dentry_resolve_streams(dentry,
+ ctx->extract_flags,
+ ctx->wim->lookup_table);
+ if (ret)
+ return ret;
+ }
+ return 0;
+}
+
+static int
+ref_stream(struct wim_lookup_table_entry *lte, u32 stream_idx,
+ struct wim_dentry *dentry, struct apply_ctx *ctx)
+{
+ struct wim_inode *inode = dentry->d_inode;
+ struct stream_owner *stream_owners;
+
+ if (!lte)
+ return 0;
+
+ /* Tally the size only for each extraction of the stream (not hard
+ * links). */
+ if (inode->i_visited && ctx->supported_features.hard_links)
+ return 0;
+
+ ctx->progress.extract.total_bytes += lte->size;
+ ctx->progress.extract.num_streams++;
+
+ if (inode->i_visited)
+ return 0;
+
+ /* Add stream to the dentry_list only one time, even if it's going
+ * to be extracted to multiple inodes. */
+ if (lte->out_refcnt == 0) {
+ list_add_tail(<e->extraction_list, &ctx->stream_list);
+ ctx->num_streams_remaining++;
+ }
+
+ /* If inode not yet been visited, append it to the stream_owners array. */
+ if (lte->out_refcnt < ARRAY_LEN(lte->inline_stream_owners)) {
+ stream_owners = lte->inline_stream_owners;
+ } else {
+ struct stream_owner *prev_stream_owners;
+ size_t alloc_stream_owners;
+
+ if (lte->out_refcnt == ARRAY_LEN(lte->inline_stream_owners)) {
+ prev_stream_owners = NULL;
+ alloc_stream_owners = ARRAY_LEN(lte->inline_stream_owners);
+ } else {
+ prev_stream_owners = lte->stream_owners;
+ alloc_stream_owners = lte->alloc_stream_owners;
+ }
+
+ if (lte->out_refcnt == alloc_stream_owners) {
+ alloc_stream_owners *= 2;
+ stream_owners = REALLOC(prev_stream_owners,
+ alloc_stream_owners *
+ sizeof(stream_owners[0]));
+ if (!stream_owners)
+ return WIMLIB_ERR_NOMEM;
+ if (!prev_stream_owners) {
+ memcpy(stream_owners,
+ lte->inline_stream_owners,
+ sizeof(lte->inline_stream_owners));
+ }
+ lte->stream_owners = stream_owners;
+ lte->alloc_stream_owners = alloc_stream_owners;
+ }
+ stream_owners = lte->stream_owners;
+ }
+ stream_owners[lte->out_refcnt].inode = inode;
+ if (stream_idx == 0) {
+ stream_owners[lte->out_refcnt].stream_name = NULL;
+ } else {
+ stream_owners[lte->out_refcnt].stream_name =
+ inode->i_ads_entries[stream_idx - 1].stream_name;
+ }
+ lte->out_refcnt++;