]> wimlib.net Git - wimlib/blobdiff - src/hardlink.c
Detect directory hard links as early as possible
[wimlib] / src / hardlink.c
index 388d125a6e099a8438f751ab5a1399ccd2b36a45..042b0576a88cfab159a7d18184cd226c586ea606 100644 (file)
  * along with wimlib; if not, see http://www.gnu.org/licenses/.
  */
 
-#include "wimlib_internal.h"
-#include "dentry.h"
-#include "list.h"
-#include "lookup_table.h"
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#endif
+
+#include "wimlib/capture.h"
+#include "wimlib/dentry.h"
+#include "wimlib/error.h"
+#include "wimlib/lookup_table.h"
 
 /*                             NULL        NULL
  *                              ^           ^
@@ -101,6 +105,15 @@ inode_table_insert(struct wim_dentry *dentry, void *_table)
                pos = d_inode->i_ino % table->capacity;
                hlist_for_each_entry(inode, cur, &table->array[pos], i_hlist) {
                        if (inode->i_ino == d_inode->i_ino) {
+                               if (unlikely((inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY) ||
+                                            (d_inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)))
+                               {
+                                       ERROR("Unsupported directory hard link "
+                                             "\"%"TS"\" <=> \"%"TS"\"",
+                                             dentry_full_path(dentry),
+                                             dentry_full_path(inode_first_dentry(inode)));
+                                       return WIMLIB_ERR_INVALID_DENTRY;
+                               }
                                inode_add_dentry(dentry, inode);
                                inode->i_nlink++;
                                return 0;
@@ -342,6 +355,13 @@ fix_nominal_inode(struct wim_inode *inode, struct list_head *inode_list,
 
        wimlib_assert(inode->i_nlink == inode_link_count(inode));
 
+       if (inode->i_nlink > 1 &&
+           (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY))
+       {
+               ERROR("Found unsupported directory hard link!");
+               return WIMLIB_ERR_INVALID_DENTRY;
+       }
+
        LIST_HEAD(dentries_with_data_streams);
        LIST_HEAD(dentries_with_no_data_streams);
        HLIST_HEAD(true_inodes);
@@ -511,16 +531,19 @@ dentry_tree_fix_inodes(struct wim_dentry *root, struct list_head *inode_list)
        DEBUG("Inserting dentries into inode table");
        ret = init_inode_table(&inode_tab, 9001);
        if (ret)
-               return ret;
+               goto out;
 
-       for_dentry_in_tree(root, inode_table_insert, &inode_tab);
+       ret = for_dentry_in_tree(root, inode_table_insert, &inode_tab);
+       if (ret)
+               goto out_destroy_image_table;
 
        DEBUG("Cleaning up the hard link groups");
        ino_changes_needed = false;
        ret = fix_inodes(&inode_tab, inode_list, &ino_changes_needed);
-       destroy_inode_table(&inode_tab);
+       if (ret)
+               goto out_destroy_image_table;
 
-       if (ret == 0 && ino_changes_needed) {
+       if (ino_changes_needed) {
                u64 cur_ino = 1;
                struct wim_inode *inode;
 
@@ -532,6 +555,10 @@ dentry_tree_fix_inodes(struct wim_dentry *root, struct list_head *inode_list)
                                inode->i_ino = 0;
                }
        }
+       ret = 0;
+out_destroy_image_table:
+       destroy_inode_table(&inode_tab);
+out:
        return ret;
 }
 
@@ -545,7 +572,13 @@ inode_table_prepare_inode_list(struct wim_inode_table *table,
        struct hlist_node *cur, *tmp;
        u64 cur_ino = 1;
 
-       INIT_LIST_HEAD(head);
+       list_for_each_entry(inode, head, i_list) {
+               if (inode->i_nlink > 1)
+                       inode->i_ino = cur_ino++;
+               else
+                       inode->i_ino = 0;
+       }
+
        for (size_t i = 0; i < table->capacity; i++) {
                hlist_for_each_entry_safe(inode, cur, tmp, &table->array[i], i_hlist)
                {