blob: 9140b2d7601e10cca638992fcea1eaeb589fdb9c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
* node_from_path.c
*
* Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
*/
#include "config.h"
#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;
}
|