Skip to content

Commit

Permalink
lib: Use common function to remove all children
Browse files Browse the repository at this point in the history
Prep for further cleanup.

Signed-off-by: Colin Walters <walters@verbum.org>
  • Loading branch information
cgwalters committed Jun 21, 2023
1 parent 8792e15 commit 447b119
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions libcomposefs/lcfs-writer.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <sys/param.h>
#include <assert.h>

static void lcfs_node_remove_all_children(struct lcfs_node_s *node);
static void lcfs_node_destroy(struct lcfs_node_s *node);

static int lcfs_close(struct lcfs_ctx_s *ctx);
Expand Down Expand Up @@ -846,10 +847,7 @@ void lcfs_node_unref(struct lcfs_node_s *node)
/* if we have a parent, that should have a real ref to us */
assert(node->parent == NULL);

while (node->children_size > 0) {
struct lcfs_node_s *child = node->children[0];
lcfs_node_remove_child_node(node, 0, child);
}
lcfs_node_remove_all_children(node);
free(node->children);

if (node->link_to)
Expand All @@ -867,14 +865,19 @@ void lcfs_node_unref(struct lcfs_node_s *node)
free(node);
}

/* Unlink all children (recursively) and then unref. Useful to handle refcount loops like dot and dotdot. */
static void lcfs_node_destroy(struct lcfs_node_s *node)
static void lcfs_node_remove_all_children(struct lcfs_node_s *node)
{
while (node->children_size > 0) {
struct lcfs_node_s *child = lcfs_node_ref(node->children[0]);
lcfs_node_remove_child_node(node, 0, child);
lcfs_node_destroy(child);
}
}

/* Unlink all children (recursively) and then unref. Useful to handle refcount loops like dot and dotdot. */
static void lcfs_node_destroy(struct lcfs_node_s *node)
{
lcfs_node_remove_all_children(node);
lcfs_node_unref(node);
};

Expand Down

0 comments on commit 447b119

Please sign in to comment.