+static struct wim_inode *
+inode_table_get_inode(struct wim_inode_table *table, u64 ino, u64 devno)
+{
+ u64 hash = hash_u64(hash_u64(ino) + hash_u64(devno));
+ size_t pos = hash % table->capacity;
+ struct wim_inode *inode;
+ struct hlist_node *cur;
+
+ hlist_for_each_entry(inode, cur, &table->array[pos], i_hlist) {
+ if (inode->i_ino == ino && inode->i_devno == devno) {
+ DEBUG("Using existing inode {devno=%"PRIu64", ino=%"PRIu64"}",
+ devno, ino);
+ inode->i_nlink++;
+ return inode;
+ }
+ }
+ inode = new_timeless_inode();
+ if (inode) {
+ inode->i_ino = ino;
+ inode->i_devno = devno;
+ hlist_add_head(&inode->i_hlist, &table->array[pos]);
+ table->num_entries++;
+ }
+ return inode;
+}
+
+/* Given a directory entry with the name @name for the file with the inode
+ * number @ino and device number @devno, create a new WIM dentry with an
+ * associated inode, where the inode is shared if an inode with the same @ino
+ * and @devno has already been created. On success, the new WIM dentry is
+ * written to *dentry_ret, and its inode has i_nlink > 1 if a previously
+ * existing inode was used.
+ */
+int
+inode_table_new_dentry(struct wim_inode_table *table, const tchar *name,
+ u64 ino, u64 devno, struct wim_dentry **dentry_ret)
+{
+ struct wim_dentry *dentry;
+ struct wim_inode *inode;
+ int ret;
+
+ ret = new_dentry(name, &dentry);
+ if (ret)
+ return ret;
+
+ inode = inode_table_get_inode(table, ino, devno);
+ if (!inode) {
+ free_dentry(dentry);
+ return WIMLIB_ERR_NOMEM;
+ }
+ dentry->d_inode = inode;
+ inode_add_dentry(dentry, inode);
+ *dentry_ret = dentry;
+ return 0;
+}
+