+
+ wimlib_assert(num_true_link_groups != 0);
+
+ /* If there were dentries with no data streams, we require there to only
+ * be one true link group so that we know which link group to assign the
+ *
+ * streamless dentries to. */
+ if (!list_empty(&dentries_with_no_data_streams)) {
+ if (num_true_link_groups != 1) {
+ ERROR("Hard link group ambiguity detected!");
+ ERROR("We split up hard link group 0x%"PRIx64" due to "
+ "inconsistencies,");
+ ERROR("but dentries with no stream information remained. "
+ "We don't know which true hard link");
+ ERROR("group to assign them to.");
+ return WIMLIB_ERR_INVALID_DENTRY;
+ }
+ ref_dentry = container_of(true_link_groups.next,
+ struct dentry,
+ tmp_list);
+ list_for_each_entry(dentry, &dentries_with_no_data_streams,
+ tmp_list)
+ {
+ list_add(&dentry->link_group_list,
+ &ref_dentry->link_group_list);
+ }
+ }
+
+ list_for_each_entry(dentry, &true_link_groups, tmp_list) {
+ ret = fix_true_link_group(dentry);
+ if (ret != 0)
+ return ret;
+ }
+
+ /* Make new `struct link_group's for the new link groups */
+ for (head = true_link_groups.next->next;
+ head != &true_link_groups;
+ head = head->next)
+ {
+ dentry = container_of(head, struct dentry, tmp_list);
+ group = MALLOC(sizeof(*group));
+ if (!group) {
+ ERROR("Out of memory");
+ return WIMLIB_ERR_NOMEM;
+ }
+ group->link_group_id = link_group_id;
+ group->dentry_list = &dentry->link_group_list;
+ group->next = *new_groups;
+ *new_groups = group;
+ } while ((head = head->next) != &true_link_groups);
+ return 0;
+}
+
+/*
+ * Goes through each link group and shares the ads_entries (Alternate Data
+ * Stream entries) field of each dentry between members of a hard link group.
+ *
+ * In the process, the dentries in each link group are checked for consistency.
+ * If they contain data features that indicate they cannot really be in the same
+ * hard link group, this should be an error, but in reality this case needs to
+ * be handled, so we split the dentries into different hard link groups.
+ *
+ * One of the dentries in the group is arbitrarily assigned the role of "owner"
+ * (ADS_ENTRIES_OWNER), while the others are "users" (ADS_ENTRIES_USER).
+ */
+int fix_link_groups(struct link_group_table *table)
+{
+ for (u64 i = 0; i < table->capacity; i++) {
+ struct link_group *group = table->array[i];
+ while (group) {
+ int ret;
+ ret = fix_nominal_link_group(group, &table->extra_groups);
+ if (ret != 0)
+ return ret;
+ group = group->next;
+ }
+ }
+ return 0;