-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse_path.c
205 lines (167 loc) · 4.33 KB
/
parse_path.c
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/**
* Copyright (c) 2016, 2022 Tim Kuijsten
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE 700
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef PATH_MAX
#define PATH_MAX 1024
#endif
#include "parse_path.h"
/*
* Modify n into an absolute path and write the results in c. "." and ".." will
* be resolved and extraneous slashes will be removed.
*
* Note that if n is absolute then c is ignored, otherwise c must be a null
* terminated absolute path. csize must be the total number of bytes available
* in c and must be >= 2. c and n may point to the same address.
*
* If comps is not NULL it will be updated to contain the number of resulting
* components in c.
*
* Returns the new length of c on success excluding the terminating null (which
* is always >= 1) or (size_t)-1 on failure. If the returned value is >= csize
* there was not enough space in c to write the terminating null byte.
*/
size_t
resolvepath(char *c, size_t csize, const char *n, int *comps)
{
char *cp;
size_t i, j;
int comp;
if (csize <= 1)
return (size_t)-1;
if (comps == NULL)
comps = ∁
*comps = 0;
/*
* cp invariant:
* Let cp point to the position where a next "/comp" should be written.
* Points to the slash in the case of a root only, else one position
* after the last character of the last component (which should be
* '\0').
*/
cp = c;
if (*n == '/') {
cp[0] = '/';
n++;
} else {
if (c[0] != '/')
return (size_t)-1;
i = resolvepath(c, csize, c, comps);
if (i == (size_t)-1 || i >= csize)
return i;
cp = &c[i - 1];
/* maintain cp invariant */
if (i > 1)
cp++;
}
for(;;) {
j = strcspn(n, "/");
if (j == 1 && n[0] == '.') {
n += 1;
continue;
} else if (j == 2 && n[0] == '.' && n[1] == '.') {
while (*cp != '/')
cp--;
if (cp > c)
*cp = '\0';
n += 2;
if (*comps > 0)
*comps -= 1;
continue;
}
if (j > 0) {
/* append */
if (csize - (cp - c) < j + 2)
return (cp - c) + j + 1;
*cp = '/';
cp++;
for (i = 0; i < j; i++)
cp[i] = n[i];
cp += j;
n += j;
*comps += 1;
}
j = strspn(n, "/");
if (j == 0) {
if (cp == c)
cp++;
*cp = '\0';
return cp - c;
}
n += j;
/* support c == n so terminate cp after n is increased */
if (cp > c)
*cp = '\0';
}
}
/*
* Parse an absolute path that may consist of a database and a collection name.
*
* Return 0 on success, -1 on failure.
*/
int
parse_path(path_t *p, const char *path)
{
int i;
char *coll;
if (*path != '/')
return -1;
p->dbname[0] = '\0';
p->collname[0] = '\0';
coll = strchr(path + 1, '/');
if (coll == NULL) {
i = snprintf(p->dbname, sizeof(p->dbname), "%s", path + 1);
if ((size_t)i >= sizeof(p->dbname))
return -1;
} else {
i = snprintf(p->dbname, sizeof(p->dbname), "%.*s",
(int)(coll - (path + 1)), path + 1);
if ((size_t)i >= sizeof(p->dbname))
return -1;
i = snprintf(p->collname, sizeof(p->collname), "%s", coll + 1);
if ((size_t)i >= sizeof(p->collname))
return -1;
}
return 0;
}
/*
* Parse paths into "*ps" using the current path from cpath.
*
* Returns 0 on success, -1 on failure. The caller should always free(3) *ps.
*/
int
parse_paths(path_t *ps[], const path_t cpath, const char **av, size_t avlen)
{
char p[PATH_MAX];
size_t i;
*ps = calloc(avlen, sizeof(path_t));
if (*ps == NULL)
return -1;
for (i = 0; i < avlen; i++) {
if ((size_t)snprintf(p, sizeof(p), "/%s/%s", cpath.dbname,
cpath.collname) >= sizeof(p))
return -1;
if (resolvepath(p, sizeof(p), av[i], NULL) == (size_t)-1)
return -1;
if (parse_path(&(*ps)[i], p) == -1)
return -1;
}
return 0;
}