paths.c revision 1.1 1 /* $NetBSD: paths.c,v 1.1 2007/01/15 00:39:02 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 * 3. The name of the company nor the name of the author may be used to
15 * endorse or promote products derived from this software without specific
16 * prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 #include <sys/cdefs.h>
32 #if !defined(lint)
33 __RCSID("$NetBSD: paths.c,v 1.1 2007/01/15 00:39:02 pooka Exp $");
34 #endif /* !lint */
35
36 #include <assert.h>
37 #include <errno.h>
38 #include <puffs.h>
39 #include <stdlib.h>
40
41 #include "puffs_priv.h"
42
43 /*
44 * Generic routines for pathbuilding code
45 */
46
47 int
48 puffs_path_pcnbuild(struct puffs_usermount *pu, struct puffs_cn *pcn,
49 void *parent)
50 {
51 struct puffs_node *pn_parent = PU_CMAP(pu, parent);
52 struct puffs_cn pcn_orig;
53 struct puffs_pathobj po;
54 int rv;
55
56 assert(pn_parent->pn_po.po_path != NULL);
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
78 if (pu->pu_pathtransform)
79 pu->pu_pathfree(pu, &po);
80
81 if (pu->pu_namemod && rv)
82 *pcn = pcn_orig;
83
84 return rv;
85 }
86
87 /*
88 * substitute all (child) patch prefixes. called from nodewalk, which
89 * in turn is called from rename
90 */
91 void *
92 puffs_path_prefixadj(struct puffs_usermount *pu, struct puffs_node *pn,
93 void *arg)
94 {
95 struct puffs_pathinfo *pi = arg;
96 struct puffs_pathobj localpo;
97 struct puffs_pathobj oldpo;
98 int rv;
99
100 /* can't be a path prefix */
101 if (pn->pn_po.po_len < pi->pi_old->po_len)
102 return NULL;
103
104 if (pu->pu_pathcmp(pu, &pn->pn_po, pi->pi_old, pi->pi_old->po_len))
105 return NULL;
106
107 /* otherwise we'd have two nodes with an equal path */
108 assert(pn->pn_po.po_len > pi->pi_old->po_len);
109
110 /* found a matching prefix */
111 rv = pu->pu_pathbuild(pu, pi->pi_new, &pn->pn_po,
112 pi->pi_old->po_len, &localpo);
113 /*
114 * XXX: technically we shouldn't fail, but this is the only
115 * sensible thing to do here. If the buildpath routine fails,
116 * we will have paths in an inconsistent state. Should fix this,
117 * either by having two separate passes or by doing other tricks
118 * to make an invalid path with BUILDPATHS acceptable.
119 */
120 if (rv != 0)
121 abort();
122
123 /* out with the old and in with the new */
124 oldpo = pn->pn_po;
125 pn->pn_po = localpo;
126 pu->pu_pathfree(pu, &oldpo);
127
128 /* continue the walk */
129 return NULL;
130 }
131
132
133 /*
134 * Routines provided to file systems which consider a path a tuple of
135 * strings and / the component separator.
136 */
137
138 /*ARGSUSED*/
139 int
140 puffs_path_cmppath(struct puffs_usermount *pu, struct puffs_pathobj *c1,
141 struct puffs_pathobj *c2, size_t clen)
142 {
143 char *p;
144 int rv;
145
146 rv = strncmp(c1->po_path, c2->po_path, clen);
147 if (rv)
148 return 1;
149
150 /* sanity for next step */
151 if (!(c1->po_len > c2->po_len))
152 return 1;
153
154 /* check if it's really a complete path prefix */
155 p = c1->po_path;
156 if ((*(p + clen)) != '/')
157 return 1;
158
159 return 0;
160 }
161
162 /*ARGSUSED*/
163 int
164 puffs_path_buildpath(struct puffs_usermount *pu, struct puffs_pathobj *po_pre,
165 struct puffs_pathobj *po_comp, size_t offset,
166 struct puffs_pathobj *newpath)
167 {
168 char *path, *pcomp;
169 size_t plen, complen;
170
171 complen = po_comp->po_len - offset;
172
173 /* seek to correct place & remove all leading '/' from component */
174 pcomp = po_comp->po_path;
175 pcomp += offset;
176 while (*pcomp == '/') {
177 pcomp++;
178 complen--;
179 }
180
181 /* + '/' + '\0' */
182 plen = po_pre->po_len + 1 + complen;
183 path = malloc(plen + 1);
184 if (path == NULL)
185 return errno;
186
187 strcpy(path, po_pre->po_path);
188 strcat(path, "/");
189 strncat(path, pcomp, complen);
190
191 newpath->po_path = path;
192 newpath->po_len = plen;
193
194 return 0;
195 }
196
197 /*ARGSUSED*/
198 void
199 puffs_path_freepath(struct puffs_usermount *pu, struct puffs_pathobj *po)
200 {
201
202 free(po->po_path);
203 }
204