- struct dentry *owner, *user, *tmp;
-
- owner = container_of(group->dentry_list, struct dentry,
- link_group_list);
- owner->ads_entries_status = ADS_ENTRIES_OWNER;
-
- list_for_each_entry_safe(user, tmp, group->dentry_list,
- link_group_list)
- {
- /* I would like it to be an error if two dentries are in the
- * same hard link group but have irreconcilable differences such
- * as different file permissions, but unfortunately some of M$'s
- * WIMs contain many instances of this error. This problem is
- * worked around here by splitting each offending dentry off
- * into its own hard link group. */
- if (share_dentry_ads(owner, user) != 0) {
- struct link_group *single;
- single = MALLOC(sizeof(struct link_group));
- if (!single)
- return WIMLIB_ERR_NOMEM;
- list_del(&user->link_group_list);
- INIT_LIST_HEAD(&user->link_group_list);
- single->link_group_id = 0;
- single->next = *bad_links;
- single->dentry_list = &user->link_group_list;
- *bad_links = single;
- user->ads_entries_status = ADS_ENTRIES_OWNER;
+ struct dentry *dentry;
+ struct hlist_node *cur, *tmp;
+ int ret;
+ size_t num_true_inodes;
+
+ wimlib_assert(inode->link_count == inode_link_count(inode));
+
+ LIST_HEAD(dentries_with_data_streams);
+ LIST_HEAD(dentries_with_no_data_streams);
+ HLIST_HEAD(true_inodes);
+
+ /* Create a list of dentries in the nominal inode that have at
+ * least one data stream with a non-zero hash, and another list that
+ * contains the dentries that have a zero hash for all data streams. */
+ inode_for_each_dentry(dentry, inode) {
+ for (unsigned i = 0; i <= dentry->d_inode->num_ads; i++) {
+ const u8 *hash;
+ hash = inode_stream_hash(dentry->d_inode, i);
+ if (!is_zero_hash(hash)) {
+ list_add(&dentry->tmp_list,
+ &dentries_with_data_streams);
+ goto next_dentry;
+ }
+ }
+ list_add(&dentry->tmp_list,
+ &dentries_with_no_data_streams);
+ next_dentry:
+ ;
+ }
+
+ /* If there are no dentries with data streams, we require the nominal
+ * inode to be a true inode */
+ if (list_empty(&dentries_with_data_streams)) {
+ #ifdef ENABLE_DEBUG
+ if (inode->link_count > 1) {
+ DEBUG("Found link group of size %u without "
+ "any data streams:", inode->link_count);
+ print_inode_dentries(inode);
+ DEBUG("We are going to interpret it as true "
+ "link group, provided that the dentries "
+ "are consistent.");
+ }
+ #endif
+ return fix_true_inode(inode, inode_list);
+ }
+
+ /* One or more dentries had data streams specified. We check each of
+ * these dentries for consistency with the others to form a set of true
+ * inodes. */
+ num_true_inodes = 0;
+ list_for_each_entry(dentry, &dentries_with_data_streams, tmp_list) {
+ /* Look for a true inode that is consistent with this dentry and
+ * add this dentry to it. Or, if none of the true inodes are
+ * consistent with this dentry, add a new one (if that happens,
+ * we have split the hard link group). */
+ hlist_for_each_entry(inode, cur, &true_inodes, hlist) {
+ if (ref_inodes_consistent(inode, dentry->d_inode)) {
+ inode_add_dentry(dentry, inode);
+ goto next_dentry_2;
+ }