1 /* $NetBSD: paths.c,v 1.8 2008/08/12 19:44:39 pooka Exp $ */
2
3 /*
4 * Copyright (c) 2007 Antti Kantee. All Rights Reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 #if !defined(lint)
30 __RCSID("$NetBSD: paths.c,v 1.8 2008/08/12 19:44:39 pooka Exp $");
31 #endif /* !lint */
32
33 #include <sys/hash.h>
34
35 #include <assert.h>
36 #include <errno.h>
37 #include <puffs.h>
38 #include <stdlib.h>
39
40 #include "puffs_priv.h"
41
42 /*
43 * Generic routines for pathbuilding code
44 */
45
46 int
puffs_path_pcnbuild(struct puffs_usermount * pu,struct puffs_cn * pcn,puffs_cookie_t parent)47 puffs_path_pcnbuild(struct puffs_usermount *pu, struct puffs_cn *pcn,
48 puffs_cookie_t parent)
49 {
50 struct puffs_node *pn_parent = PU_CMAP(pu, parent);
51 struct puffs_cn pcn_orig;
52 struct puffs_pathobj po;
53 int rv;
54
55 assert(pn_parent->pn_po.po_path != NULL);
56 assert(pu->pu_flags & PUFFS_FLAG_BUILDPATH);
57
58 if (pu->pu_pathtransform) {
59 rv = pu->pu_pathtransform(pu, &pn_parent->pn_po, pcn, &po);
60 if (rv)
61 return rv;
62 } else {
63 po.po_path = pcn->pcn_name;
64 po.po_len = pcn->pcn_namelen;
65 }
66
67 if (pu->pu_namemod) {
68 /* XXX: gcc complains if I do assignment */
69 memcpy(&pcn_orig, pcn, sizeof(pcn_orig));
70 rv = pu->pu_namemod(pu, &pn_parent->pn_po, pcn);
71 if (rv)
72 return rv;
73 }
74
75 rv = pu->pu_pathbuild(pu, &pn_parent->pn_po, &po, 0,
76 &pcn->pcn_po_full);
77 puffs_path_buildhash(pu, &pcn->pcn_po_full);
78
79 if (pu->pu_pathtransform)
80 pu->pu_pathfree(pu, &po);
81
82 if (pu->pu_namemod && rv)
83 *pcn = pcn_orig;
84
85 return rv;
86 }
87
88 /*
89 * substitute all (child) patch prefixes. called from nodewalk, which
90 * in turn is called from rename
91 */
92 void *
puffs_path_prefixadj(struct puffs_usermount * pu,struct puffs_node * pn,void * arg)93 puffs_path_prefixadj(struct puffs_usermount *pu, struct puffs_node *pn,
94 void *arg)
95 {
96 struct puffs_pathinfo *pi = arg;
97 struct puffs_pathobj localpo;
98 struct puffs_pathobj oldpo;
99 int rv;
100
101 /* can't be a path prefix */
102 if (pn->pn_po.po_len < pi->pi_old->po_len)
103 return NULL;
104
105 if (pu->pu_pathcmp(pu, &pn->pn_po, pi->pi_old, pi->pi_old->po_len, 1))
106 return NULL;
107
108 /* otherwise we'd have two nodes with an equal path */
109 assert(pn->pn_po.po_len > pi->pi_old->po_len);
110
111 /* found a matching prefix */
112 rv = pu->pu_pathbuild(pu, pi->pi_new, &pn->pn_po,
113 pi->pi_old->po_len, &localpo);
114 /*
115 * XXX: technically we shouldn't fail, but this is the only
116 * sensible thing to do here. If the buildpath routine fails,
117 * we will have paths in an inconsistent state. Should fix this,
118 * either by having two separate passes or by doing other tricks
119 * to make an invalid path with BUILDPATHS acceptable.
120 */
121 if (rv != 0)
122 abort();
123
124 /* adjust hash sum */
125 puffs_path_buildhash(pu, &localpo);
126
127 /* out with the old and in with the new */
128 oldpo = pn->pn_po;
129 pn->pn_po = localpo;
130 pu->pu_pathfree(pu, &oldpo);
131
132 /* continue the walk */
133 return NULL;
134 }
135
136 /*
137 * called from nodewalk, checks for exact match
138 */
139 void *
puffs_path_walkcmp(struct puffs_usermount * pu,struct puffs_node * pn,void * arg)140 puffs_path_walkcmp(struct puffs_usermount *pu, struct puffs_node *pn, void *arg)
141 {
142 struct puffs_pathobj *po = arg;
143 struct puffs_pathobj po2;
144
145 if (po->po_len != PNPLEN(pn))
146 return NULL;
147
148 /*
149 * If hashing and the hash doesn't match, we know this is
150 * definitely not a match. Otherwise check for collisions.
151 */
152 if (pu->pu_flags & PUFFS_FLAG_HASHPATH)
153 if (pn->pn_po.po_hash != po->po_hash)
154 return NULL;
155
156 po2.po_path = PNPATH(pn);
157 po2.po_len = PNPLEN(pn);
158
159 if (pu->pu_pathcmp(pu, po, &po2, PNPLEN(pn), 0) == 0)
160 return pn;
161 return NULL;
162 }
163
164 /*
165 * Hash sum building routine. Use string hash if the buildpath routine
166 * is the standard one, otherwise use binary hashes. A bit whimsical
167 * way to choose the routine, but the binary works for strings also,
168 * so don't sweat it.
169 */
170 void
puffs_path_buildhash(struct puffs_usermount * pu,struct puffs_pathobj * po)171 puffs_path_buildhash(struct puffs_usermount *pu, struct puffs_pathobj *po)
172 {
173
174 if ((pu->pu_flags & PUFFS_FLAG_HASHPATH) == 0)
175 return;
176
177 if (pu->pu_pathbuild == puffs_stdpath_buildpath)
178 po->po_hash = hash32_strn(po->po_path, po->po_len,
179 HASH32_STR_INIT);
180 else
181 po->po_hash = hash32_buf(po->po_path, po->po_len,
182 HASH32_BUF_INIT);
183 }
184
185 /*
186 * Routines provided to file systems which consider a path a tuple of
187 * strings and / the component separator.
188 */
189
190 /*ARGSUSED*/
191 int
puffs_stdpath_cmppath(struct puffs_usermount * pu,struct puffs_pathobj * c1,struct puffs_pathobj * c2,size_t clen,int checkprefix)192 puffs_stdpath_cmppath(struct puffs_usermount *pu, struct puffs_pathobj *c1,
193 struct puffs_pathobj *c2, size_t clen, int checkprefix)
194 {
195 char *p;
196 int rv;
197
198 rv = strncmp(c1->po_path, c2->po_path, clen);
199 if (rv)
200 return 1;
201
202 if (checkprefix == 0)
203 return 0;
204
205 /* sanity for next step */
206 if (!(c1->po_len > c2->po_len))
207 return 1;
208
209 /* check if it's really a complete path prefix */
210 p = c1->po_path;
211 if ((*(p + clen)) != '/')
212 return 1;
213
214 return 0;
215 }
216
217 /*ARGSUSED*/
218 int
puffs_stdpath_buildpath(struct puffs_usermount * pu,const struct puffs_pathobj * po_pre,const struct puffs_pathobj * po_comp,size_t offset,struct puffs_pathobj * newpath)219 puffs_stdpath_buildpath(struct puffs_usermount *pu,
220 const struct puffs_pathobj *po_pre, const struct puffs_pathobj *po_comp,
221 size_t offset, struct puffs_pathobj *newpath)
222 {
223 char *path, *pcomp;
224 size_t plen, complen;
225 size_t prelen;
226 int isdotdot;
227
228 complen = po_comp->po_len - offset;
229
230 /* seek to correct place & remove all leading '/' from component */
231 pcomp = po_comp->po_path;
232 pcomp += offset;
233 while (*pcomp == '/') {
234 pcomp++;
235 complen--;
236 }
237
238 /* todotdot or nottodotdot */
239 if (complen == 2 && strcmp(pcomp, "..") == 0)
240 isdotdot = 1;
241 else
242 isdotdot = 0;
243
244 /*
245 * Strip trailing components from the preceending component.
246 * This is an issue only for the root node, which we might want
247 * to be at path "/" for some file systems.
248 */
249 prelen = po_pre->po_len;
250 while (prelen > 0 && *((char *)po_pre->po_path + (prelen-1)) == '/') {
251 assert(isdotdot == 0);
252 prelen--;
253 }
254
255 if (isdotdot) {
256 char *slash; /* sweet char of mine */
257
258 slash = strrchr(po_pre->po_path, '/');
259 assert(slash != NULL);
260
261 plen = slash - (char *)po_pre->po_path;
262
263 /*
264 * As the converse to not stripping the initial "/" above,
265 * don't nuke it here either.
266 */
267 if (plen == 0)
268 plen++;
269
270 path = malloc(plen + 1);
271 if (path == NULL)
272 return errno;
273
274 strlcpy(path, po_pre->po_path, plen+1);
275 } else {
276 /* + '/' + '\0' */
277 plen = prelen + 1 + complen;
278 path = malloc(plen + 1);
279 if (path == NULL)
280 return errno;
281
282 strlcpy(path, po_pre->po_path, prelen+1);
283 strcat(path, "/");
284 strncat(path, pcomp, complen);
285 }
286
287 newpath->po_path = path;
288 newpath->po_len = plen;
289
290 return 0;
291 }
292
293 /*ARGSUSED*/
294 void
puffs_stdpath_freepath(struct puffs_usermount * pu,struct puffs_pathobj * po)295 puffs_stdpath_freepath(struct puffs_usermount *pu, struct puffs_pathobj *po)
296 {
297
298 free(po->po_path);
299 }
300