+void put_dentry(struct dentry *dentry)
+{
+ if (dentry->link_group_master_status == GROUP_MASTER) {
+ struct dentry *new_master;
+ list_for_each_entry(new_master, &dentry->link_group_list,
+ link_group_list)
+ {
+ if (new_master->link_group_master_status == GROUP_SLAVE) {
+ new_master->link_group_master_status = GROUP_MASTER;
+ dentry->link_group_master_status = GROUP_SLAVE;
+ break;
+ }
+ }
+ }
+ list_del(&dentry->link_group_list);
+ free_dentry(dentry);
+}
+
+static bool dentries_have_same_ads(const struct dentry *d1,
+ const struct dentry *d2)
+{
+ /* 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 (memcmp(d1->ads_entries[i].hash,
+ d2->ads_entries[i].hash,
+ WIM_HASH_SIZE) != 0)
+ return false;
+ }
+ return true;
+}
+
+/* Share the alternate stream entries between hard-linked dentries. */
+int share_dentry_ads(struct dentry *master, struct dentry *slave)
+{
+ const char *mismatch_type;
+ wimlib_assert(master->num_ads == 0 ||
+ master->ads_entries != slave->ads_entries);
+ if (master->attributes != slave->attributes) {
+ mismatch_type = "attributes";
+ goto mismatch;
+ }
+ if (master->security_id != slave->security_id) {
+ mismatch_type = "security ID";
+ goto mismatch;
+ }
+ if (memcmp(master->hash, slave->hash, WIM_HASH_SIZE) != 0) {
+ mismatch_type = "main file resource";
+ goto mismatch;
+ }
+ if (!dentries_have_same_ads(master, slave)) {
+ mismatch_type = "Alternate Stream Entries";
+ goto mismatch;
+ }
+ dentry_free_ads_entries(slave);
+ slave->ads_entries = master->ads_entries;
+ slave->link_group_master_status = GROUP_SLAVE;
+ return 0;
+mismatch:
+ ERROR("Dentries `%s' and `%s' in the same hard-link group but "
+ "do not share the same %s",
+ master->full_path_utf8, slave->full_path_utf8,
+ mismatch_type);
+ return WIMLIB_ERR_INVALID_DENTRY;
+}
+