+ /* Verify stream names and hashes are the same */
+ for (u16 i = 0; i < d1->num_ads; i++) {
+ if (strcmp(d1->ads_entries[i].stream_name_utf8,
+ d2->ads_entries[i].stream_name_utf8) != 0)
+ return false;
+ if (!hashes_equal(d1->ads_entries[i].hash,
+ d2->ads_entries[i].hash))
+ return false;
+ }
+ return true;
+}
+
+/* Share the alternate stream entries between hard-linked dentries. */
+static int share_dentry_ads(struct dentry *owner, struct dentry *user)
+{
+ const char *mismatch_type;
+ wimlib_assert(owner->num_ads == 0 ||
+ owner->ads_entries != user->ads_entries);
+ if (owner->attributes != user->attributes) {
+ mismatch_type = "attributes";
+ goto mismatch;
+ }
+ if (owner->attributes & FILE_ATTRIBUTE_DIRECTORY) {
+ WARNING("`%s' is hard-linked to `%s', which is a directory ",
+ user->full_path_utf8, owner->full_path_utf8);
+ return WIMLIB_ERR_INVALID_DENTRY;
+ }
+ if (owner->security_id != user->security_id) {
+ mismatch_type = "security ID";
+ goto mismatch;
+ }
+ if (!hashes_equal(owner->hash, user->hash)) {
+ mismatch_type = "main file resource";
+ goto mismatch;
+ }
+ if (!dentries_have_same_ads(owner, user)) {
+ mismatch_type = "Alternate Stream Entries";
+ goto mismatch;
+ }
+ dentry_free_ads_entries(user);
+ user->ads_entries = owner->ads_entries;
+ user->ads_entries_status = ADS_ENTRIES_USER;
+ return 0;
+mismatch:
+ WARNING("Dentries `%s' and `%s' in the same hard-link group but "
+ "do not share the same %s",
+ owner->full_path_utf8, user->full_path_utf8,
+ mismatch_type);
+ return WIMLIB_ERR_INVALID_DENTRY;
+}
+
+static int link_group_free_duplicate_data(struct link_group *group,
+ struct link_group **bad_links)
+{
+ 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;
+ }
+ }
+ 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 link_groups_free_duplicate_data(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 = link_group_free_duplicate_data(group,
+ &table->singles);
+ if (ret != 0)
+ return ret;
+ group = group->next;
+ }
+ }
+ return 0;