From da7eb3445cab5936455b8a119cb656113ca3d402 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Wed, 3 Jul 2019 18:50:33 +0200 Subject: cleanup: move tree node from path function to libfstree.a Signed-off-by: David Oberhollenzer --- lib/fstree/node_from_path.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 lib/fstree/node_from_path.c (limited to 'lib/fstree/node_from_path.c') diff --git a/lib/fstree/node_from_path.c b/lib/fstree/node_from_path.c new file mode 100644 index 0000000..e617059 --- /dev/null +++ b/lib/fstree/node_from_path.c @@ -0,0 +1,39 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +#include "fstree.h" + +#include +#include + +tree_node_t *fstree_node_from_path(fstree_t *fs, const char *path) +{ + tree_node_t *n = fs->root; + const char *end; + size_t len; + + while (path != NULL && *path != '\0') { + if (!S_ISDIR(n->mode)) { + errno = ENOTDIR; + return NULL; + } + + end = strchrnul(path, '/'); + len = end - path; + + for (n = n->data.dir->children; n != NULL; n = n->next) { + if (strncmp(path, n->name, len) != 0) + continue; + if (n->name[len] != '\0') + continue; + break; + } + + if (n == NULL) { + errno = ENOENT; + return NULL; + } + + path = *end ? (end + 1) : end; + } + + return n; +} -- cgit v1.2.3