+static int sort_stream_list_by_wim_position(struct list_head *stream_list)
+{
+ struct list_head *cur;
+ size_t num_streams;
+ struct lookup_table_entry **array;
+ size_t i;
+ size_t array_size;
+
+ num_streams = 0;
+ list_for_each(cur, stream_list)
+ num_streams++;
+ array_size = num_streams * sizeof(array[0]);
+ array = MALLOC(array_size);
+ if (!array) {
+ ERROR("Failed to allocate %zu bytes to sort stream entries",
+ array_size);
+ return WIMLIB_ERR_NOMEM;
+ }
+ cur = stream_list->next;
+ for (i = 0; i < num_streams; i++) {
+ array[i] = container_of(cur, struct lookup_table_entry, staging_list);
+ cur = cur->next;
+ }
+
+ qsort(array, num_streams, sizeof(array[0]), cmp_streams_by_wim_position);
+
+ INIT_LIST_HEAD(stream_list);
+ for (i = 0; i < num_streams; i++)
+ list_add_tail(&array[i]->staging_list, stream_list);
+ FREE(array);
+ return 0;
+}
+
+static void calculate_bytes_to_extract(struct list_head *stream_list,
+ int extract_flags,
+ union wimlib_progress_info *progress)
+{
+ struct lookup_table_entry *lte;
+ u64 total_bytes = 0;
+ u64 num_streams = 0;
+
+ /* For each stream to be extracted... */
+ list_for_each_entry(lte, stream_list, staging_list) {
+ if (extract_flags &
+ (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
+ {
+ /* In the symlink or hard link extraction mode, each
+ * stream will be extracted one time regardless of how
+ * many dentries share the stream. */
+ wimlib_assert(!(extract_flags & WIMLIB_EXTRACT_FLAG_NTFS));
+ if (!lte->extracted_file) {
+ num_streams++;
+ total_bytes += wim_resource_size(lte);
+ }
+ } else {
+ num_streams += lte->out_refcnt;
+ total_bytes += lte->out_refcnt * wim_resource_size(lte);
+ }
+ }
+ progress->extract.num_streams = num_streams;
+ progress->extract.total_bytes = total_bytes;
+ progress->extract.completed_bytes = 0;
+}
+
+static void maybe_add_stream_for_extraction(struct lookup_table_entry *lte,
+ struct list_head *stream_list)
+{
+ if (++lte->out_refcnt == 1) {
+ INIT_LIST_HEAD(<e->inode_list);
+ list_add_tail(<e->staging_list, stream_list);
+ }
+}
+
+static void inode_find_streams_for_extraction(struct inode *inode,
+ struct list_head *stream_list,
+ int extract_flags)
+{
+ struct lookup_table_entry *lte;
+ bool inode_added = false;
+
+ lte = inode_unnamed_lte_resolved(inode);
+
+ if (lte) {
+ maybe_add_stream_for_extraction(lte, stream_list);
+ list_add_tail(&inode->lte_inode_list, <e->inode_list);
+ inode_added = true;
+ }
+#ifdef WITH_NTFS_3G
+ if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
+ for (unsigned i = 0; i < inode->num_ads; i++) {
+ if (inode->ads_entries[i].stream_name_len != 0) {
+ lte = inode->ads_entries[i].lte;
+ if (lte) {
+ maybe_add_stream_for_extraction(lte,
+ stream_list);
+ if (!inode_added) {
+ list_add_tail(&inode->lte_inode_list,
+ <e->inode_list);
+ inode_added = true;
+ }
+ }
+ }
+ }