summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-07-03 18:50:33 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-07-03 18:50:33 +0200
commitda7eb3445cab5936455b8a119cb656113ca3d402 (patch)
treef3ab70532f0587788df8e691be0ec3b15db3db6c /lib
parente981f2e460092e5ae6ad58a60869b78d7f50ec10 (diff)
cleanup: move tree node from path function to libfstree.a
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib')
-rw-r--r--lib/Makemodule.am2
-rw-r--r--lib/fstree/node_from_path.c39
2 files changed, 40 insertions, 1 deletions
diff --git a/lib/Makemodule.am b/lib/Makemodule.am
index 6418568..23ce70d 100644
--- a/lib/Makemodule.am
+++ b/lib/Makemodule.am
@@ -3,7 +3,7 @@ libfstree_a_SOURCES += lib/fstree/fstree_sort.c lib/fstree/fstree_from_dir.c
libfstree_a_SOURCES += lib/fstree/gen_inode_table.c lib/fstree/get_path.c
libfstree_a_SOURCES += lib/fstree/node_stat.c lib/fstree/mknode.c
libfstree_a_SOURCES += lib/fstree/add_by_path.c lib/fstree/xattr.c
-libfstree_a_SOURCES += include/fstree.h
+libfstree_a_SOURCES += lib/fstree/node_from_path.c include/fstree.h
libfstree_a_CFLAGS = $(AM_CFLAGS)
libfstree_a_CPPFLAGS = $(AM_CPPFLAGS)
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 <string.h>
+#include <errno.h>
+
+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;
+}