-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfind.c
More file actions
151 lines (127 loc) · 3.74 KB
/
Copy pathfind.c
File metadata and controls
151 lines (127 loc) · 3.74 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* -*- c-set-style: "K&R"; c-basic-offset: 8 -*-
*
* This file is part of PRoot.
*
* Copyright (C) 2014 STMicroelectronics
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA.
*/
#include <sys/param.h> /* MAXSYMLINKS, */
#include <errno.h> /* E*, */
#include <dirent.h> /* DT_*, */
#include <assert.h> /* assert(3), */
#include <string.h> /* str*spn(3), */
#include <stdbool.h> /* bool, */
#include <fcntl.h> /* O_NOFOLLOW, O_CREATE, */
#include "vfs/find.h"
#include "vfs/node.h"
#include "vfs/symlink.h"
#include "vfs/children.h"
/**
* Find in @root file-system the node pointed to by @node. This
* function returns NULL if an error occurred, and *@error is set to
* -errno.
*/
static Node *follow_symlink_node(Node *root, Node *node, int *error, size_t symlink_count)
{
const char *symlink;
if (symlink_count++ > MAXSYMLINKS) {
*error = -ELOOP;
return NULL;
}
symlink = get_symlink(node, error);
if (symlink == NULL)
return NULL;
return find_node_(root, node->parent, symlink, 0, error, symlink_count);
}
/**
* Get @node's child with given @name. This function handles special
* names "." and "..", respectively @node and @node->parent. Also, it
* fills @node's children list if needed.
*/
static Node *get_child(Node *node, const char *name, ssize_t length)
{
Node *child = NULL;
assert(node->type == DT_DIR);
if (length < 0)
length = strlen(name);
if (length == 1 && strncmp(name, ".", length) == 0)
return node;
if (length == 2 && strncmp(name, "..", length) == 0)
return node->parent;
if (!node->children_filled)
(void) fill_children(node);
HASH_FIND(hh, node->children, name, length, child);
return child;
}
/**
* Find in @root file-system the node for @path, relatively to @from
* if not absolute. @flags is a bit mask that can contain O_NOFOLLOW
* and/or O_CREATE. This function returns NULL if an error occurred,
* and *@error is set to -errno.
*/
Node *find_node_(Node *root, Node *from, const char *path, int flags,
int *error, size_t symlink_count)
{
bool follow_symlink = ((flags & O_NOFOLLOW) == 0);
bool create = ((flags & O_CREAT) != 0);
Node *node;
if (path[0] == '/')
node = root;
else
node = from;
while (path[0] != '\0') {
Node *parent_node;
size_t length;
bool is_final;
if (node->type != DT_DIR) {
*error = -ENOTDIR;
return NULL;
}
/* Find component boundaries. */
path += strspn(path, "/");
length = strcspn(path, "/");
is_final = (path[length] == '\0');
parent_node = node;
node = get_child(node, path, length);
if (node == NULL) {
if (!is_final) {
*error = -ENOTDIR;
return NULL;
}
if (!create) {
*error = -ENOENT;
return NULL;
}
node = add_new_child(parent_node, path, length, 0 /* DT_UNKNOWN */);
if (node == NULL) {
*error = -ENOMEM;
return NULL;
}
/* Early exit; it's the final component
* anyway. */
break;
}
/* Move to the next component. */
path += length;
if (node->type == DT_LNK && (!is_final || follow_symlink)) {
node = follow_symlink_node(root, node, error, symlink_count);
if (node == NULL)
return NULL;
}
}
return node;
}