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