From 268defa6efa18fc4b9a226c5d58f0e50ce2d0846 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Thu, 6 Apr 2023 15:03:19 +0200 Subject: libfstree: simplify hard link resolution code We do not allow hard links to directories, so we can toss the special case handling code for that. The visited mechanism was pointless anyway, because we don't even descend down hard links in the recursive tree handling functions. Signed-off-by: David Oberhollenzer --- include/fstree.h | 3 --- lib/fstree/src/hardlink.c | 28 ++++------------------------ lib/fstree/src/post_process.c | 4 ---- 3 files changed, 4 insertions(+), 31 deletions(-) diff --git a/include/fstree.h b/include/fstree.h index 430574d..ef6dd4b 100644 --- a/include/fstree.h +++ b/include/fstree.h @@ -53,9 +53,6 @@ struct dir_info_t { /* Set to true for implicitly generated directories. */ bool created_implicitly; - - /* Used by recursive tree walking code to avoid hard link loops */ - bool visited; }; /* A node in a file system tree */ diff --git a/lib/fstree/src/hardlink.c b/lib/fstree/src/hardlink.c index 9686219..fc3c4d0 100644 --- a/lib/fstree/src/hardlink.c +++ b/lib/fstree/src/hardlink.c @@ -11,23 +11,23 @@ #include #include -#include #include static int resolve_link(fstree_t *fs, tree_node_t *node) { tree_node_t *start = node; - while (node->mode == FSTREE_MODE_HARD_LINK || - node->mode == FSTREE_MODE_HARD_LINK_RESOLVED) { + for (;;) { if (node->mode == FSTREE_MODE_HARD_LINK_RESOLVED) { node = node->data.target_node; - } else { + } else if (node->mode == FSTREE_MODE_HARD_LINK) { node = fstree_get_node_by_path(fs, fs->root, node->data.target, false, false); if (node == NULL) return -1; + } else { + break; } if (node == start) { @@ -60,21 +60,11 @@ static int resolve_hard_links_dfs(fstree_t *fs, tree_node_t *n) if (n->mode == FSTREE_MODE_HARD_LINK) { if (resolve_link(fs, n)) goto fail_link; - - assert(n->mode == FSTREE_MODE_HARD_LINK_RESOLVED); - it = n->data.target_node; - - if (S_ISDIR(it->mode) && it->data.dir.visited) - goto fail_link_loop; } else if (S_ISDIR(n->mode)) { - n->data.dir.visited = true; - for (it = n->data.dir.children; it != NULL; it = it->next) { if (resolve_hard_links_dfs(fs, it)) return -1; } - - n->data.dir.visited = false; } return 0; @@ -86,16 +76,6 @@ fail_link: { free(path); } return -1; -fail_link_loop: { - char *npath = fstree_get_path(n); - char *tpath = fstree_get_path(it); - fprintf(stderr, "Hard link loop detected in '%s' -> '%s'\n", - npath == NULL ? n->name : npath, - tpath == NULL ? it->name : tpath); - free(npath); - free(tpath); -} - return -1; } tree_node_t *fstree_add_hard_link(fstree_t *fs, const char *path, diff --git a/lib/fstree/src/post_process.c b/lib/fstree/src/post_process.c index 940f93d..f614906 100644 --- a/lib/fstree/src/post_process.c +++ b/lib/fstree/src/post_process.c @@ -9,7 +9,6 @@ #include #include -#include #include #include @@ -120,9 +119,6 @@ static void reorder_hard_links(fstree_t *fs) if (tgt_idx <= i) continue; - /* TODO ? */ - assert(!S_ISDIR(tgt->mode)); - for (j = tgt_idx; j > i; --j) { fs->inodes[j] = fs->inodes[j - 1]; fs->inodes[j]->inode_num += 1; -- cgit v1.2.3