+
+ ref_inode = ref_dentry->d_inode;
+ ref_inode->i_nlink = 1;
+ list_add_tail(&ref_inode->i_list, inode_list);
+
+ list_del(&inode->i_dentry);
+ list_add(&ref_inode->i_dentry, &ref_dentry->d_alias);
+
+ inode_for_each_dentry(dentry, ref_inode) {
+ if (dentry != ref_dentry) {
+ if (!inodes_consistent(ref_inode, dentry->d_inode)) {
+ inconsistent_inode(ref_inode);
+ return WIMLIB_ERR_INVALID_DENTRY;
+ }
+ /* Free the unneeded `struct wim_inode'. */
+ dentry->d_inode->i_hlist.next = NULL;
+ dentry->d_inode->i_hlist.pprev = NULL;
+ free_inode(dentry->d_inode);
+ dentry->d_inode = ref_inode;
+ ref_inode->i_nlink++;
+ }
+ }
+ ref_inode->i_creation_time = last_ctime;
+ ref_inode->i_last_write_time = last_mtime;
+ ref_inode->i_last_access_time = last_atime;
+ wimlib_assert(inode_link_count(ref_inode) == ref_inode->i_nlink);
+ return 0;
+}
+
+/*
+ * Fixes up a nominal inode.
+ *
+ * By a nominal inode we mean a group of two or more dentries that share the
+ * same hard link group ID.
+ *
+ * If dentries in the inode are found to be inconsistent, we may split the inode
+ * into several "true" inodes.
+ *
+ * After splitting up each nominal inode into the "true" inodes we will
+ * canonicalize the link group by getting rid of all the unnecessary `struct
+ * wim_inode's. There will be just one `struct wim_inode' for each hard link
+ * group remaining.
+ */
+static int
+fix_nominal_inode(struct wim_inode *inode, struct list_head *inode_list,
+ bool *ino_changes_needed)
+{
+ struct wim_dentry *dentry;
+ struct hlist_node *cur, *tmp;
+ int ret;
+ size_t num_true_inodes;
+
+ wimlib_assert(inode->i_nlink == 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->i_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->i_nlink > 1) {
+ DEBUG("Found link group of size %u without "
+ "any data streams:", inode->i_nlink);
+ 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);