Rev 2751 | Rev 2753 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2751 | Rev 2752 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2008 Jakub Jermar |
2 | * Copyright (c) 2008 Jakub Jermar |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
13 | * documentation and/or other materials provided with the distribution. |
14 | * - The name of the author may not be used to endorse or promote products |
14 | * - The name of the author may not be used to endorse or promote products |
15 | * derived from this software without specific prior written permission. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup fs |
29 | /** @addtogroup fs |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | 32 | ||
33 | /** |
33 | /** |
34 | * @file vfs_lookup.c |
34 | * @file vfs_lookup.c |
35 | * @brief |
35 | * @brief |
36 | */ |
36 | */ |
37 | 37 | ||
38 | #include <ipc/ipc.h> |
38 | #include <ipc/ipc.h> |
39 | #include <async.h> |
39 | #include <async.h> |
40 | #include <errno.h> |
40 | #include <errno.h> |
41 | #include <string.h> |
41 | #include <string.h> |
42 | #include <stdlib.h> |
42 | #include <stdlib.h> |
43 | #include <bool.h> |
43 | #include <bool.h> |
44 | #include <futex.h> |
44 | #include <futex.h> |
45 | #include <libadt/list.h> |
45 | #include <libadt/list.h> |
46 | #include <atomic.h> |
46 | #include <atomic.h> |
47 | #include "vfs.h" |
47 | #include "vfs.h" |
48 | 48 | ||
49 | #define min(a, b) ((a) < (b) ? (a) : (b)) |
49 | #define min(a, b) ((a) < (b) ? (a) : (b)) |
50 | 50 | ||
51 | /* Forward static declarations. */ |
51 | /* Forward static declarations. */ |
52 | static char *canonify(char *path); |
52 | static char *canonify(char *path, size_t *lenp); |
53 | 53 | ||
54 | atomic_t plb_futex = FUTEX_INITIALIZER; |
54 | atomic_t plb_futex = FUTEX_INITIALIZER; |
55 | link_t plb_head; /**< PLB entry ring buffer. */ |
55 | link_t plb_head; /**< PLB entry ring buffer. */ |
56 | uint8_t *plb = NULL; |
56 | uint8_t *plb = NULL; |
57 | 57 | ||
58 | /** Perform a path lookup. |
58 | /** Perform a path lookup. |
59 | * |
59 | * |
60 | * @param path Path to be resolved; it needn't be an ASCIIZ string. |
60 | * @param path Path to be resolved; it must be a NULL-terminated |
61 | * @param len Number of path characters pointed by path. |
61 | * string. |
62 | * @param lflag Flags to be used during lookup. |
62 | * @param lflag Flags to be used during lookup. |
63 | * @param result Empty structure where the lookup result will be stored. |
63 | * @param result Empty structure where the lookup result will be stored. |
64 | * Can be NULL. |
64 | * Can be NULL. |
65 | * @param altroot If non-empty, will be used instead of rootfs as the root |
65 | * @param altroot If non-empty, will be used instead of rootfs as the root |
66 | * of the whole VFS tree. |
66 | * of the whole VFS tree. |
67 | * |
67 | * |
68 | * @return EOK on success or an error code from errno.h. |
68 | * @return EOK on success or an error code from errno.h. |
69 | */ |
69 | */ |
70 | int vfs_lookup_internal(char *path, size_t len, int lflag, |
70 | int vfs_lookup_internal(char *path, int lflag, vfs_lookup_res_t *result, |
71 | vfs_lookup_res_t *result, vfs_pair_t *altroot) |
71 | vfs_pair_t *altroot) |
72 | { |
72 | { |
73 | vfs_pair_t *root; |
73 | vfs_pair_t *root; |
74 | 74 | ||
75 | if (!len) |
- | |
76 | return EINVAL; |
- | |
77 | - | ||
78 | if (altroot) |
75 | if (altroot) |
79 | root = altroot; |
76 | root = altroot; |
80 | else |
77 | else |
81 | root = (vfs_pair_t *) &rootfs; |
78 | root = (vfs_pair_t *) &rootfs; |
82 | 79 | ||
83 | if (!root->fs_handle) |
80 | if (!root->fs_handle) |
84 | return ENOENT; |
81 | return ENOENT; |
85 | 82 | ||
- | 83 | size_t len; |
|
- | 84 | path = canonify(path, &len); |
|
- | 85 | if (!path) |
|
- | 86 | return EINVAL; |
|
- | 87 | ||
86 | futex_down(&plb_futex); |
88 | futex_down(&plb_futex); |
87 | 89 | ||
88 | plb_entry_t entry; |
90 | plb_entry_t entry; |
89 | link_initialize(&entry.plb_link); |
91 | link_initialize(&entry.plb_link); |
90 | entry.len = len; |
92 | entry.len = len; |
91 | 93 | ||
92 | off_t first; /* the first free index */ |
94 | off_t first; /* the first free index */ |
93 | off_t last; /* the last free index */ |
95 | off_t last; /* the last free index */ |
94 | 96 | ||
95 | if (list_empty(&plb_head)) { |
97 | if (list_empty(&plb_head)) { |
96 | first = 0; |
98 | first = 0; |
97 | last = PLB_SIZE - 1; |
99 | last = PLB_SIZE - 1; |
98 | } else { |
100 | } else { |
99 | plb_entry_t *oldest = list_get_instance(plb_head.next, |
101 | plb_entry_t *oldest = list_get_instance(plb_head.next, |
100 | plb_entry_t, plb_link); |
102 | plb_entry_t, plb_link); |
101 | plb_entry_t *newest = list_get_instance(plb_head.prev, |
103 | plb_entry_t *newest = list_get_instance(plb_head.prev, |
102 | plb_entry_t, plb_link); |
104 | plb_entry_t, plb_link); |
103 | 105 | ||
104 | first = (newest->index + newest->len) % PLB_SIZE; |
106 | first = (newest->index + newest->len) % PLB_SIZE; |
105 | last = (oldest->index - 1) % PLB_SIZE; |
107 | last = (oldest->index - 1) % PLB_SIZE; |
106 | } |
108 | } |
107 | 109 | ||
108 | if (first <= last) { |
110 | if (first <= last) { |
109 | if ((last - first) + 1 < len) { |
111 | if ((last - first) + 1 < len) { |
110 | /* |
112 | /* |
111 | * The buffer cannot absorb the path. |
113 | * The buffer cannot absorb the path. |
112 | */ |
114 | */ |
113 | futex_up(&plb_futex); |
115 | futex_up(&plb_futex); |
114 | return ELIMIT; |
116 | return ELIMIT; |
115 | } |
117 | } |
116 | } else { |
118 | } else { |
117 | if (PLB_SIZE - ((first - last) + 1) < len) { |
119 | if (PLB_SIZE - ((first - last) + 1) < len) { |
118 | /* |
120 | /* |
119 | * The buffer cannot absorb the path. |
121 | * The buffer cannot absorb the path. |
120 | */ |
122 | */ |
121 | futex_up(&plb_futex); |
123 | futex_up(&plb_futex); |
122 | return ELIMIT; |
124 | return ELIMIT; |
123 | } |
125 | } |
124 | } |
126 | } |
125 | 127 | ||
126 | /* |
128 | /* |
127 | * We know the first free index in PLB and we also know that there is |
129 | * We know the first free index in PLB and we also know that there is |
128 | * enough space in the buffer to hold our path. |
130 | * enough space in the buffer to hold our path. |
129 | */ |
131 | */ |
130 | 132 | ||
131 | entry.index = first; |
133 | entry.index = first; |
132 | entry.len = len; |
134 | entry.len = len; |
133 | 135 | ||
134 | /* |
136 | /* |
135 | * Claim PLB space by inserting the entry into the PLB entry ring |
137 | * Claim PLB space by inserting the entry into the PLB entry ring |
136 | * buffer. |
138 | * buffer. |
137 | */ |
139 | */ |
138 | list_append(&entry.plb_link, &plb_head); |
140 | list_append(&entry.plb_link, &plb_head); |
139 | 141 | ||
140 | futex_up(&plb_futex); |
142 | futex_up(&plb_futex); |
141 | 143 | ||
142 | /* |
144 | /* |
143 | * Copy the path into PLB. |
145 | * Copy the path into PLB. |
144 | */ |
146 | */ |
145 | size_t cnt1 = min(len, (PLB_SIZE - first) + 1); |
147 | size_t cnt1 = min(len, (PLB_SIZE - first) + 1); |
146 | size_t cnt2 = len - cnt1; |
148 | size_t cnt2 = len - cnt1; |
147 | 149 | ||
148 | memcpy(&plb[first], path, cnt1); |
150 | memcpy(&plb[first], path, cnt1); |
149 | memcpy(plb, &path[cnt1], cnt2); |
151 | memcpy(plb, &path[cnt1], cnt2); |
150 | 152 | ||
151 | ipc_call_t answer; |
153 | ipc_call_t answer; |
152 | int phone = vfs_grab_phone(root->fs_handle); |
154 | int phone = vfs_grab_phone(root->fs_handle); |
153 | aid_t req = async_send_4(phone, VFS_LOOKUP, (ipcarg_t) first, |
155 | aid_t req = async_send_4(phone, VFS_LOOKUP, (ipcarg_t) first, |
154 | (ipcarg_t) (first + len - 1) % PLB_SIZE, |
156 | (ipcarg_t) (first + len - 1) % PLB_SIZE, |
155 | (ipcarg_t) root->dev_handle, (ipcarg_t) lflag, &answer); |
157 | (ipcarg_t) root->dev_handle, (ipcarg_t) lflag, &answer); |
156 | vfs_release_phone(phone); |
158 | vfs_release_phone(phone); |
157 | 159 | ||
158 | ipcarg_t rc; |
160 | ipcarg_t rc; |
159 | async_wait_for(req, &rc); |
161 | async_wait_for(req, &rc); |
160 | 162 | ||
161 | futex_down(&plb_futex); |
163 | futex_down(&plb_futex); |
162 | list_remove(&entry.plb_link); |
164 | list_remove(&entry.plb_link); |
163 | /* |
165 | /* |
164 | * Erasing the path from PLB will come handy for debugging purposes. |
166 | * Erasing the path from PLB will come handy for debugging purposes. |
165 | */ |
167 | */ |
166 | memset(&plb[first], 0, cnt1); |
168 | memset(&plb[first], 0, cnt1); |
167 | memset(plb, 0, cnt2); |
169 | memset(plb, 0, cnt2); |
168 | futex_up(&plb_futex); |
170 | futex_up(&plb_futex); |
169 | 171 | ||
170 | if ((rc == EOK) && result) { |
172 | if ((rc == EOK) && result) { |
171 | result->triplet.fs_handle = (int) IPC_GET_ARG1(answer); |
173 | result->triplet.fs_handle = (int) IPC_GET_ARG1(answer); |
172 | result->triplet.dev_handle = (int) IPC_GET_ARG2(answer); |
174 | result->triplet.dev_handle = (int) IPC_GET_ARG2(answer); |
173 | result->triplet.index = (int) IPC_GET_ARG3(answer); |
175 | result->triplet.index = (int) IPC_GET_ARG3(answer); |
174 | result->size = (size_t) IPC_GET_ARG4(answer); |
176 | result->size = (size_t) IPC_GET_ARG4(answer); |
175 | result->lnkcnt = (unsigned) IPC_GET_ARG5(answer); |
177 | result->lnkcnt = (unsigned) IPC_GET_ARG5(answer); |
176 | } |
178 | } |
177 | 179 | ||
178 | return rc; |
180 | return rc; |
179 | } |
181 | } |
180 | 182 | ||
181 | /** Token types used for tokenization of path. */ |
183 | /** Token types used for tokenization of path. */ |
182 | typedef enum { |
184 | typedef enum { |
183 | TK_INVALID, |
185 | TK_INVALID, |
184 | TK_SLASH, |
186 | TK_SLASH, |
185 | TK_DOT, |
187 | TK_DOT, |
186 | TK_DOTDOT, |
188 | TK_DOTDOT, |
187 | TK_COMP, |
189 | TK_COMP, |
188 | TK_NUL |
190 | TK_NUL |
189 | } tokval_t; |
191 | } tokval_t; |
190 | 192 | ||
191 | typedef struct { |
193 | typedef struct { |
192 | tokval_t kind; |
194 | tokval_t kind; |
193 | char *start; |
195 | char *start; |
194 | char *stop; |
196 | char *stop; |
195 | } token_t; |
197 | } token_t; |
196 | 198 | ||
197 | /** Fake up the TK_SLASH token. */ |
199 | /** Fake up the TK_SLASH token. */ |
198 | static token_t slash_token(char *start) |
200 | static token_t slash_token(char *start) |
199 | { |
201 | { |
200 | token_t ret; |
202 | token_t ret; |
201 | ret.kind = TK_SLASH; |
203 | ret.kind = TK_SLASH; |
202 | ret.start = start; |
204 | ret.start = start; |
203 | ret.stop = start; |
205 | ret.stop = start; |
204 | return ret; |
206 | return ret; |
205 | } |
207 | } |
206 | 208 | ||
207 | /** Given a token, return the next token. */ |
209 | /** Given a token, return the next token. */ |
208 | static token_t next_token(token_t *cur) |
210 | static token_t next_token(token_t *cur) |
209 | { |
211 | { |
210 | token_t ret; |
212 | token_t ret; |
211 | 213 | ||
212 | if (cur->stop[1] == '\0') { |
214 | if (cur->stop[1] == '\0') { |
213 | ret.kind = TK_NUL; |
215 | ret.kind = TK_NUL; |
214 | ret.start = cur->stop + 1; |
216 | ret.start = cur->stop + 1; |
215 | ret.stop = ret.start; |
217 | ret.stop = ret.start; |
216 | return ret; |
218 | return ret; |
217 | } |
219 | } |
218 | if (cur->stop[1] == '/') { |
220 | if (cur->stop[1] == '/') { |
219 | ret.kind = TK_SLASH; |
221 | ret.kind = TK_SLASH; |
220 | ret.start = cur->stop + 1; |
222 | ret.start = cur->stop + 1; |
221 | ret.stop = ret.start; |
223 | ret.stop = ret.start; |
222 | return ret; |
224 | return ret; |
223 | } |
225 | } |
224 | if (cur->stop[1] == '.' && (!cur->stop[2] || cur->stop[2] == '/')) { |
226 | if (cur->stop[1] == '.' && (!cur->stop[2] || cur->stop[2] == '/')) { |
225 | ret.kind = TK_DOT; |
227 | ret.kind = TK_DOT; |
226 | ret.start = cur->stop + 1; |
228 | ret.start = cur->stop + 1; |
227 | ret.stop = ret.start; |
229 | ret.stop = ret.start; |
228 | return ret; |
230 | return ret; |
229 | } |
231 | } |
230 | if (cur->stop[1] == '.' && cur->stop[2] == '.' && |
232 | if (cur->stop[1] == '.' && cur->stop[2] == '.' && |
231 | (!cur->stop[3] || cur->stop[3] == '/')) { |
233 | (!cur->stop[3] || cur->stop[3] == '/')) { |
232 | ret.kind = TK_DOTDOT; |
234 | ret.kind = TK_DOTDOT; |
233 | ret.start = cur->stop + 1; |
235 | ret.start = cur->stop + 1; |
234 | ret.stop = cur->stop + 2; |
236 | ret.stop = cur->stop + 2; |
235 | return ret; |
237 | return ret; |
236 | } |
238 | } |
237 | unsigned i; |
239 | unsigned i; |
238 | for (i = 1; cur->stop[i] && cur->stop[i] != '/'; i++) |
240 | for (i = 1; cur->stop[i] && cur->stop[i] != '/'; i++) |
239 | ; |
241 | ; |
240 | ret.kind = TK_COMP; |
242 | ret.kind = TK_COMP; |
241 | ret.start = &cur->stop[1]; |
243 | ret.start = &cur->stop[1]; |
242 | ret.stop = &cur->stop[i - 1]; |
244 | ret.stop = &cur->stop[i - 1]; |
243 | return ret; |
245 | return ret; |
244 | } |
246 | } |
245 | 247 | ||
246 | /** States used by canonify(). */ |
248 | /** States used by canonify(). */ |
247 | typedef enum { |
249 | typedef enum { |
248 | S_INI, |
250 | S_INI, |
249 | S_A, |
251 | S_A, |
250 | S_B, |
252 | S_B, |
251 | S_C, |
253 | S_C, |
252 | S_ACCEPT, |
254 | S_ACCEPT, |
253 | S_RESTART, |
255 | S_RESTART, |
254 | S_REJECT |
256 | S_REJECT |
255 | } state_t; |
257 | } state_t; |
256 | 258 | ||
257 | typedef struct { |
259 | typedef struct { |
258 | state_t s; |
260 | state_t s; |
259 | void (* f)(token_t *, token_t *, token_t *); |
261 | void (* f)(token_t *, token_t *, token_t *); |
260 | } change_state_t; |
262 | } change_state_t; |
261 | 263 | ||
262 | /* |
264 | /* |
263 | * Actions that can be performed when transitioning from one |
265 | * Actions that can be performed when transitioning from one |
264 | * state of canonify() to another. |
266 | * state of canonify() to another. |
265 | */ |
267 | */ |
266 | static void set_first_slash(token_t *t, token_t *tfsl, token_t *tlcomp) |
268 | static void set_first_slash(token_t *t, token_t *tfsl, token_t *tlcomp) |
267 | { |
269 | { |
268 | *tfsl = *t; |
270 | *tfsl = *t; |
269 | } |
271 | } |
270 | static void save_component(token_t *t, token_t *tfsl, token_t *tlcomp) |
272 | static void save_component(token_t *t, token_t *tfsl, token_t *tlcomp) |
271 | { |
273 | { |
272 | *tlcomp = *t; |
274 | *tlcomp = *t; |
273 | } |
275 | } |
274 | static void terminate_slash(token_t *t, token_t *tfsl, token_t *tlcomp) |
276 | static void terminate_slash(token_t *t, token_t *tfsl, token_t *tlcomp) |
275 | { |
277 | { |
- | 278 | if (tfsl->stop[1]) /* avoid writing to a well-formatted path */ |
|
276 | tfsl->stop[1] = '\0'; |
279 | tfsl->stop[1] = '\0'; |
277 | } |
280 | } |
278 | static void remove_trailing_slash(token_t *t, token_t *tfsl, token_t *tlcomp) |
281 | static void remove_trailing_slash(token_t *t, token_t *tfsl, token_t *tlcomp) |
279 | { |
282 | { |
280 | t->start[-1] = '\0'; |
283 | t->start[-1] = '\0'; |
281 | } |
284 | } |
282 | /** Eat the extra '/'.. |
285 | /** Eat the extra '/'.. |
283 | * |
286 | * |
284 | * @param t The current TK_SLASH token. |
287 | * @param t The current TK_SLASH token. |
285 | */ |
288 | */ |
286 | static void shift_slash(token_t *t, token_t *tfsl, token_t *tlcomp) |
289 | static void shift_slash(token_t *t, token_t *tfsl, token_t *tlcomp) |
287 | { |
290 | { |
288 | char *p = t->start; |
291 | char *p = t->start; |
289 | char *q = t->stop + 1; |
292 | char *q = t->stop + 1; |
290 | while ((*p++ = *q++)) |
293 | while ((*p++ = *q++)) |
291 | ; |
294 | ; |
292 | } |
295 | } |
293 | /** Eat the extra '.'. |
296 | /** Eat the extra '.'. |
294 | * |
297 | * |
295 | * @param t The current TK_DOT token. |
298 | * @param t The current TK_DOT token. |
296 | */ |
299 | */ |
297 | static void shift_dot(token_t *t, token_t *tfsl, token_t *tlcomp) |
300 | static void shift_dot(token_t *t, token_t *tfsl, token_t *tlcomp) |
298 | { |
301 | { |
299 | char *p = t->start; |
302 | char *p = t->start; |
300 | char *q = t->stop + 1; |
303 | char *q = t->stop + 1; |
301 | while ((*p++ = *q++)) |
304 | while ((*p++ = *q++)) |
302 | ; |
305 | ; |
303 | } |
306 | } |
304 | /** Collapse the TK_COMP TK_SLASH TK_DOTDOT pattern. |
307 | /** Collapse the TK_COMP TK_SLASH TK_DOTDOT pattern. |
305 | * |
308 | * |
306 | * @param t The current TK_DOTDOT token. |
309 | * @param t The current TK_DOTDOT token. |
307 | * @param tlcomp The last TK_COMP token. |
310 | * @param tlcomp The last TK_COMP token. |
308 | */ |
311 | */ |
309 | static void shift_dotdot(token_t *t, token_t *tfsl, token_t *tlcomp) |
312 | static void shift_dotdot(token_t *t, token_t *tfsl, token_t *tlcomp) |
310 | { |
313 | { |
311 | char *p = tlcomp->start; |
314 | char *p = tlcomp->start; |
312 | char *q = t->stop + 1; |
315 | char *q = t->stop + 1; |
313 | while ((*p++ = *q++)) |
316 | while ((*p++ = *q++)) |
314 | ; |
317 | ; |
315 | } |
318 | } |
316 | 319 | ||
317 | /** Transition function for canonify(). */ |
320 | /** Transition function for canonify(). */ |
318 | static change_state_t trans[4][6] = { |
321 | static change_state_t trans[4][6] = { |
319 | [S_INI] = { |
322 | [S_INI] = { |
320 | [TK_SLASH] = { |
323 | [TK_SLASH] = { |
321 | .s = S_A, |
324 | .s = S_A, |
322 | .f = set_first_slash, |
325 | .f = set_first_slash, |
323 | }, |
326 | }, |
324 | [TK_DOT] = { |
327 | [TK_DOT] = { |
325 | .s = S_REJECT, |
328 | .s = S_REJECT, |
326 | .f = NULL, |
329 | .f = NULL, |
327 | }, |
330 | }, |
328 | [TK_DOTDOT] = { |
331 | [TK_DOTDOT] = { |
329 | .s = S_REJECT, |
332 | .s = S_REJECT, |
330 | .f = NULL, |
333 | .f = NULL, |
331 | }, |
334 | }, |
332 | [TK_COMP] = { |
335 | [TK_COMP] = { |
333 | .s = S_REJECT, |
336 | .s = S_REJECT, |
334 | .f = NULL, |
337 | .f = NULL, |
335 | }, |
338 | }, |
336 | [TK_NUL] = { |
339 | [TK_NUL] = { |
337 | .s = S_REJECT, |
340 | .s = S_REJECT, |
338 | .f = NULL, |
341 | .f = NULL, |
339 | }, |
342 | }, |
340 | [TK_INVALID] = { |
343 | [TK_INVALID] = { |
341 | .s = S_REJECT, |
344 | .s = S_REJECT, |
342 | .f = NULL, |
345 | .f = NULL, |
343 | }, |
346 | }, |
344 | }, |
347 | }, |
345 | [S_A] = { |
348 | [S_A] = { |
346 | [TK_SLASH] = { |
349 | [TK_SLASH] = { |
347 | .s = S_A, |
350 | .s = S_A, |
348 | .f = set_first_slash, |
351 | .f = set_first_slash, |
349 | }, |
352 | }, |
350 | [TK_DOT] = { |
353 | [TK_DOT] = { |
351 | .s = S_A, |
354 | .s = S_A, |
352 | .f = NULL, |
355 | .f = NULL, |
353 | }, |
356 | }, |
354 | [TK_DOTDOT] = { |
357 | [TK_DOTDOT] = { |
355 | .s = S_A, |
358 | .s = S_A, |
356 | .f = NULL, |
359 | .f = NULL, |
357 | }, |
360 | }, |
358 | [TK_COMP] = { |
361 | [TK_COMP] = { |
359 | .s = S_B, |
362 | .s = S_B, |
360 | .f = save_component, |
363 | .f = save_component, |
361 | }, |
364 | }, |
362 | [TK_NUL] = { |
365 | [TK_NUL] = { |
363 | .s = S_ACCEPT, |
366 | .s = S_ACCEPT, |
364 | .f = terminate_slash, |
367 | .f = terminate_slash, |
365 | }, |
368 | }, |
366 | [TK_INVALID] = { |
369 | [TK_INVALID] = { |
367 | .s = S_REJECT, |
370 | .s = S_REJECT, |
368 | .f = NULL, |
371 | .f = NULL, |
369 | }, |
372 | }, |
370 | }, |
373 | }, |
371 | [S_B] = { |
374 | [S_B] = { |
372 | [TK_SLASH] = { |
375 | [TK_SLASH] = { |
373 | .s = S_C, |
376 | .s = S_C, |
374 | .f = NULL, |
377 | .f = NULL, |
375 | }, |
378 | }, |
376 | [TK_DOT] = { |
379 | [TK_DOT] = { |
377 | .s = S_REJECT, |
380 | .s = S_REJECT, |
378 | .f = NULL, |
381 | .f = NULL, |
379 | }, |
382 | }, |
380 | [TK_DOTDOT] = { |
383 | [TK_DOTDOT] = { |
381 | .s = S_REJECT, |
384 | .s = S_REJECT, |
382 | .f = NULL, |
385 | .f = NULL, |
383 | }, |
386 | }, |
384 | [TK_COMP] = { |
387 | [TK_COMP] = { |
385 | .s = S_REJECT, |
388 | .s = S_REJECT, |
386 | .f = NULL, |
389 | .f = NULL, |
387 | }, |
390 | }, |
388 | [TK_NUL] = { |
391 | [TK_NUL] = { |
389 | .s = S_ACCEPT, |
392 | .s = S_ACCEPT, |
390 | .f = NULL, |
393 | .f = NULL, |
391 | }, |
394 | }, |
392 | [TK_INVALID] = { |
395 | [TK_INVALID] = { |
393 | .s = S_REJECT, |
396 | .s = S_REJECT, |
394 | .f = NULL, |
397 | .f = NULL, |
395 | }, |
398 | }, |
396 | }, |
399 | }, |
397 | [S_C] = { |
400 | [S_C] = { |
398 | [TK_SLASH] = { |
401 | [TK_SLASH] = { |
399 | .s = S_RESTART, |
402 | .s = S_RESTART, |
400 | .f = shift_slash, |
403 | .f = shift_slash, |
401 | }, |
404 | }, |
402 | [TK_DOT] = { |
405 | [TK_DOT] = { |
403 | .s = S_RESTART, |
406 | .s = S_RESTART, |
404 | .f = shift_dot, |
407 | .f = shift_dot, |
405 | }, |
408 | }, |
406 | [TK_DOTDOT] = { |
409 | [TK_DOTDOT] = { |
407 | .s = S_RESTART, |
410 | .s = S_RESTART, |
408 | .f = shift_dotdot, |
411 | .f = shift_dotdot, |
409 | }, |
412 | }, |
410 | [TK_COMP] = { |
413 | [TK_COMP] = { |
411 | .s = S_B, |
414 | .s = S_B, |
412 | .f = save_component, |
415 | .f = save_component, |
413 | }, |
416 | }, |
414 | [TK_NUL] = { |
417 | [TK_NUL] = { |
415 | .s = S_ACCEPT, |
418 | .s = S_ACCEPT, |
416 | .f = remove_trailing_slash, |
419 | .f = remove_trailing_slash, |
417 | }, |
420 | }, |
418 | [TK_INVALID] = { |
421 | [TK_INVALID] = { |
419 | .s = S_REJECT, |
422 | .s = S_REJECT, |
420 | .f = NULL, |
423 | .f = NULL, |
421 | }, |
424 | }, |
422 | } |
425 | } |
423 | }; |
426 | }; |
424 | 427 | ||
425 | /** Canonify a file system path. |
428 | /** Canonify a file system path. |
426 | * |
429 | * |
427 | * A file system path is canonical, if the following holds: |
430 | * A file system path is canonical, if the following holds: |
428 | * 1) the path is absolute (i.e. a/b/c is not canonical) |
431 | * 1) the path is absolute (i.e. a/b/c is not canonical) |
429 | * 2) there is no trailing slash in the path (i.e. /a/b/c is not canonical) |
432 | * 2) there is no trailing slash in the path (i.e. /a/b/c is not canonical) |
430 | * 3) there is no extra slash in the path (i.e. /a//b/c is not canonical) |
433 | * 3) there is no extra slash in the path (i.e. /a//b/c is not canonical) |
431 | * 4) there is no '.' component in the path (i.e. /a/./b/c is not canonical) |
434 | * 4) there is no '.' component in the path (i.e. /a/./b/c is not canonical) |
432 | * 5) there is no '..' component in the path (i.e. /a/b/../c is not canonical) |
435 | * 5) there is no '..' component in the path (i.e. /a/b/../c is not canonical) |
433 | * |
436 | * |
434 | * This function makes a potentially non-canonical file system path canonical. |
437 | * This function makes a potentially non-canonical file system path canonical. |
435 | * It works in-place and requires a NULL-terminated input string. |
438 | * It works in-place and requires a NULL-terminated input string. |
436 | * |
439 | * |
437 | * @param Path to be canonified. |
440 | * @param path Path to be canonified. |
- | 441 | * @param lenp Pointer where the length of the final path will be |
|
- | 442 | * stored. Can be NULL. |
|
438 | * |
443 | * |
439 | * @return Canonified path or NULL on failure. |
444 | * @return Canonified path or NULL on failure. |
440 | */ |
445 | */ |
441 | char *canonify(char *path) |
446 | char *canonify(char *path, size_t *lenp) |
442 | { |
447 | { |
443 | state_t state; |
448 | state_t state; |
444 | token_t t; |
449 | token_t t; |
445 | token_t tfsl; /* first slash */ |
450 | token_t tfsl; /* first slash */ |
446 | token_t tlcomp; /* last component */ |
451 | token_t tlcomp; /* last component */ |
447 | if (*path != '/') |
452 | if (*path != '/') |
448 | return NULL; |
453 | return NULL; |
449 | tfsl = slash_token(path); |
454 | tfsl = slash_token(path); |
450 | restart: |
455 | restart: |
451 | state = S_INI; |
456 | state = S_INI; |
452 | t = tfsl; |
457 | t = tfsl; |
- | 458 | tlcomp = tfsl; |
|
453 | while (state != S_ACCEPT && state != S_RESTART && state != S_REJECT) { |
459 | while (state != S_ACCEPT && state != S_RESTART && state != S_REJECT) { |
454 | if (trans[state][t.kind].f) |
460 | if (trans[state][t.kind].f) |
455 | trans[state][t.kind].f(&t, &tfsl, &tlcomp); |
461 | trans[state][t.kind].f(&t, &tfsl, &tlcomp); |
456 | state = trans[state][t.kind].s; |
462 | state = trans[state][t.kind].s; |
457 | t = next_token(&t); |
463 | t = next_token(&t); |
458 | } |
464 | } |
459 | 465 | ||
460 | switch (state) { |
466 | switch (state) { |
461 | case S_RESTART: |
467 | case S_RESTART: |
462 | goto restart; |
468 | goto restart; |
463 | case S_REJECT: |
469 | case S_REJECT: |
464 | return NULL; |
470 | return NULL; |
465 | case S_ACCEPT: |
471 | case S_ACCEPT: |
- | 472 | if (lenp) |
|
- | 473 | *lenp = (size_t)((tlcomp.stop - tfsl.start) + 1); |
|
466 | return tfsl.start; |
474 | return tfsl.start; |
467 | default: |
475 | default: |
468 | abort(); |
476 | abort(); |
469 | } |
477 | } |
470 | } |
478 | } |
471 | 479 | ||
472 | /** |
480 | /** |
473 | * @} |
481 | * @} |
474 | */ |
482 | */ |
475 | 483 |