+ const struct dentry *dentry = first_dentry;
+
+ ERROR("An inconsistent hard link group that we cannot correct has been "
+ "detected");
+ ERROR("The dentries are located at the following paths:");
+ do {
+ ERROR("`%s'", dentry->full_path_utf8);
+ } while ((dentry = container_of(dentry->link_group_list.next,
+ struct dentry,
+ link_group_list)) != first_dentry);
+}
+
+static bool ref_dentries_consistent(const struct dentry * restrict ref_dentry_1,
+ const struct dentry * restrict ref_dentry_2)
+{
+ wimlib_assert(ref_dentry_1 != ref_dentry_2);
+
+ if (ref_dentry_1->num_ads != ref_dentry_2->num_ads)
+ return false;
+ if (ref_dentry_1->security_id != ref_dentry_2->security_id
+ || ref_dentry_1->attributes != ref_dentry_2->attributes)
+ return false;
+ for (unsigned i = 0; i <= ref_dentry_1->num_ads; i++) {
+ const u8 *ref_1_hash, *ref_2_hash;
+ ref_1_hash = dentry_stream_hash_unresolved(ref_dentry_1, i);
+ ref_2_hash = dentry_stream_hash_unresolved(ref_dentry_2, i);
+ if (!hashes_equal(ref_1_hash, ref_2_hash))
+ return false;
+ if (i && !ads_entries_have_same_name(&ref_dentry_1->ads_entries[i - 1],
+ &ref_dentry_2->ads_entries[i - 1]))
+ return false;
+
+ }
+ return true;
+}
+
+static bool dentries_consistent(const struct dentry * restrict ref_dentry,
+ const struct dentry * restrict dentry)
+{
+ wimlib_assert(ref_dentry != dentry);
+
+ if (ref_dentry->num_ads != dentry->num_ads && dentry->num_ads != 0)
+ return false;
+ if (ref_dentry->security_id != dentry->security_id
+ || ref_dentry->attributes != dentry->attributes)
+ return false;
+ for (unsigned i = 0; i <= min(ref_dentry->num_ads, dentry->num_ads); i++) {
+ const u8 *ref_hash, *hash;
+ ref_hash = dentry_stream_hash_unresolved(ref_dentry, i);
+ hash = dentry_stream_hash_unresolved(dentry, i);
+ if (!hashes_equal(ref_hash, hash) && !is_zero_hash(hash))
+ return false;
+ if (i && !ads_entries_have_same_name(&ref_dentry->ads_entries[i - 1],
+ &dentry->ads_entries[i - 1]))
+ return false;
+ }
+ return true;
+}
+
+static int
+fix_true_link_group(struct dentry *first_dentry)
+{
+ struct dentry *dentry;
+ struct dentry *ref_dentry = NULL;
+ u64 last_ctime = 0;
+ u64 last_mtime = 0;
+ u64 last_atime = 0;
+
+ dentry = first_dentry;
+ do {
+ if (!ref_dentry || ref_dentry->num_ads == 0)
+ ref_dentry = dentry;
+ if (dentry->creation_time > last_ctime)
+ last_ctime = dentry->creation_time;
+ if (dentry->last_write_time > last_mtime)
+ last_mtime = dentry->last_write_time;
+ if (dentry->last_access_time > last_atime)
+ last_atime = dentry->last_access_time;
+ } while ((dentry = container_of(dentry->link_group_list.next,
+ struct dentry,
+ link_group_list)) != first_dentry);
+
+
+ ref_dentry->ads_entries_status = ADS_ENTRIES_OWNER;
+ dentry = first_dentry;
+ do {
+ if (dentry != ref_dentry) {
+ if (!dentries_consistent(ref_dentry, dentry)) {
+ inconsistent_link_group(first_dentry);
+ return WIMLIB_ERR_INVALID_DENTRY;
+ }
+ copy_hash(dentry->hash, ref_dentry->hash);
+ dentry_free_ads_entries(dentry);
+ dentry->num_ads = ref_dentry->num_ads;
+ dentry->ads_entries = ref_dentry->ads_entries;
+ dentry->ads_entries_status = ADS_ENTRIES_USER;
+ }
+ dentry->creation_time = last_ctime;
+ dentry->last_write_time = last_mtime;
+ dentry->last_access_time = last_atime;
+ } while ((dentry = container_of(dentry->link_group_list.next,
+ struct dentry,
+ link_group_list)) != first_dentry);
+ return 0;
+}
+
+static int
+fix_nominal_link_group(struct link_group *group,
+ struct link_group **new_groups)
+{
+ struct dentry *tmp, *dentry, *ref_dentry;
+ int ret;
+ size_t num_true_link_groups;