diff options
author | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2023-04-06 15:03:19 +0200 |
---|---|---|
committer | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2023-04-06 18:07:59 +0200 |
commit | 268defa6efa18fc4b9a226c5d58f0e50ce2d0846 (patch) | |
tree | 64a8feff1c29ec8f004fb44a33bfb542634aee18 | |
parent | c42e4fbbc6b175f843c4d0eeaaa9ead2d0e746b6 (diff) |
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 <david.oberhollenzer@sigma-star.at>
-rw-r--r-- | include/fstree.h | 3 | ||||
-rw-r--r-- | lib/fstree/src/hardlink.c | 28 | ||||
-rw-r--r-- | 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 <string.h> #include <stdlib.h> -#include <assert.h> #include <errno.h> 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 <stdlib.h> #include <string.h> -#include <assert.h> #include <stdio.h> #include <errno.h> @@ -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; |