<sys/ioccom.h>, <sys/ioctl.h>
[minix3.git] / lib / libpuffs / path_puffs.c
blob70a64ad6cc6a726d6b60c0d63755a8e39adf684a
1 /*
2 * Copyright (c) 2007 Antti Kantee. All Rights Reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
14 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 * SUCH DAMAGE.
26 #include <sys/cdefs.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
30 #include <sys/hash.h>
32 #include <assert.h>
33 #include <errno.h>
34 #include <puffs.h>
35 #include <stdlib.h>
36 #include <string.h>
38 #include "puffs.h"
39 #include "puffs_priv.h"
43 * Generic routines for pathbuilding code
46 int
47 puffs_path_pcnbuild(struct puffs_usermount *pu, struct puffs_cn *pcn,
48 puffs_cookie_t parent)
50 struct puffs_node *pn_parent = PU_CMAP(pu, parent);
51 struct puffs_cn pcn_orig;
52 struct puffs_pathobj po;
53 int rv;
55 assert(pn_parent->pn_po.po_path != NULL);
56 assert(pu->pu_flags & PUFFS_FLAG_BUILDPATH);
57 pcn_orig = *pcn;
59 if (pu->pu_pathtransform) {
60 rv = pu->pu_pathtransform(pu, &pn_parent->pn_po, pcn, &po);
61 if (rv)
62 return rv;
63 } else {
64 po.po_path = pcn->pcn_name;
65 po.po_len = pcn->pcn_namelen;
68 if (pu->pu_namemod) {
69 rv = pu->pu_namemod(pu, &pn_parent->pn_po, pcn);
70 if (rv)
71 return rv;
74 rv = pu->pu_pathbuild(pu, &pn_parent->pn_po, &po, 0,
75 &pcn->pcn_po_full);
76 puffs_path_buildhash(pu, &pcn->pcn_po_full);
78 if (pu->pu_pathtransform)
79 pu->pu_pathfree(pu, &po);
81 if (pu->pu_namemod && rv)
82 *pcn = pcn_orig;
84 return rv;
88 * substitute all (child) patch prefixes. called from nodewalk, which
89 * in turn is called from rename
91 void *
92 puffs_path_prefixadj(struct puffs_usermount *pu, struct puffs_node *pn,
93 void *arg)
95 struct puffs_pathinfo *pi = arg;
96 struct puffs_pathobj localpo;
97 struct puffs_pathobj oldpo;
98 int rv;
100 /* can't be a path prefix */
101 if (pn->pn_po.po_len < pi->pi_old->po_len)
102 return NULL;
104 if (pu->pu_pathcmp(pu, &pn->pn_po, pi->pi_old, pi->pi_old->po_len, 1))
105 return NULL;
107 /* otherwise we'd have two nodes with an equal path */
108 assert(pn->pn_po.po_len > pi->pi_old->po_len);
110 /* found a matching prefix */
111 rv = pu->pu_pathbuild(pu, pi->pi_new, &pn->pn_po,
112 pi->pi_old->po_len, &localpo);
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.
120 if (rv != 0)
121 abort();
123 /* adjust hash sum */
124 puffs_path_buildhash(pu, &localpo);
126 /* out with the old and in with the new */
127 oldpo = pn->pn_po;
128 pn->pn_po = localpo;
129 pu->pu_pathfree(pu, &oldpo);
131 /* continue the walk */
132 return NULL;
136 * called from nodewalk, checks for exact match
138 void *
139 puffs_path_walkcmp(struct puffs_usermount *pu, struct puffs_node *pn, void *arg)
141 struct puffs_pathobj *po = arg;
142 struct puffs_pathobj po2;
144 if (po->po_len != PNPLEN(pn))
145 return NULL;
148 * If hashing and the hash doesn't match, we know this is
149 * definitely not a match. Otherwise check for collisions.
151 if (pu->pu_flags & PUFFS_FLAG_HASHPATH)
152 if (pn->pn_po.po_hash != po->po_hash)
153 return NULL;
155 po2.po_path = PNPATH(pn);
156 po2.po_len = PNPLEN(pn);
158 if (pu->pu_pathcmp(pu, po, &po2, PNPLEN(pn), 0) == 0)
159 return pn;
160 return NULL;
164 * Hash sum building routine. Use string hash if the buildpath routine
165 * is the standard one, otherwise use binary hashes. A bit whimsical
166 * way to choose the routine, but the binary works for strings also,
167 * so don't sweat it.
169 void
170 puffs_path_buildhash(struct puffs_usermount *pu, struct puffs_pathobj *po)
173 if ((pu->pu_flags & PUFFS_FLAG_HASHPATH) == 0)
174 return;
176 if (pu->pu_pathbuild == puffs_stdpath_buildpath)
177 po->po_hash = hash32_strn(po->po_path, po->po_len,
178 HASH32_STR_INIT);
179 else
180 po->po_hash = hash32_buf(po->po_path, po->po_len,
181 HASH32_BUF_INIT);
185 * Routines provided to file systems which consider a path a tuple of
186 * strings and / the component separator.
189 /*ARGSUSED*/
191 puffs_stdpath_cmppath(struct puffs_usermount *pu, struct puffs_pathobj *c1,
192 struct puffs_pathobj *c2, size_t clen, int checkprefix)
194 char *p;
195 int rv;
197 rv = strncmp(c1->po_path, c2->po_path, clen);
198 if (rv)
199 return 1;
201 if (checkprefix == 0)
202 return 0;
204 /* sanity for next step */
205 if (!(c1->po_len > c2->po_len))
206 return 1;
208 /* check if it's really a complete path prefix */
209 p = c1->po_path;
210 if ((*(p + clen)) != '/')
211 return 1;
213 return 0;
216 /*ARGSUSED*/
218 puffs_stdpath_buildpath(struct puffs_usermount *pu,
219 const struct puffs_pathobj *po_pre, const struct puffs_pathobj *po_comp,
220 size_t offset, struct puffs_pathobj *newpath)
222 char *path, *pcomp;
223 size_t plen, complen;
224 size_t prelen;
225 int isdotdot;
227 complen = po_comp->po_len - offset;
229 /* seek to correct place & remove all leading '/' from component */
230 pcomp = po_comp->po_path;
231 pcomp += offset;
232 while (*pcomp == '/') {
233 pcomp++;
234 complen--;
237 /* todotdot or nottodotdot */
238 if (complen == 2 && strcmp(pcomp, "..") == 0)
239 isdotdot = 1;
240 else
241 isdotdot = 0;
244 * Strip trailing components from the preceending component.
245 * This is an issue only for the root node, which we might want
246 * to be at path "/" for some file systems.
248 prelen = po_pre->po_len;
249 while (prelen > 0 && *((char *)po_pre->po_path + (prelen-1)) == '/') {
250 assert(isdotdot == 0);
251 prelen--;
254 if (isdotdot) {
255 char *slash; /* sweet char of mine */
257 slash = strrchr(po_pre->po_path, '/');
258 assert(slash != NULL);
260 plen = slash - (char *)po_pre->po_path;
263 * As the converse to not stripping the initial "/" above,
264 * don't nuke it here either.
266 if (plen == 0)
267 plen++;
269 path = malloc(plen + 1);
270 if (path == NULL)
271 return errno;
273 strlcpy(path, po_pre->po_path, plen+1);
274 } else {
275 /* + '/' + '\0' */
276 plen = prelen + 1 + complen;
277 path = malloc(plen + 1);
278 if (path == NULL)
279 return errno;
281 strlcpy(path, po_pre->po_path, prelen+1);
282 strcat(path, "/");
283 strncat(path, pcomp, complen);
286 newpath->po_path = path;
287 newpath->po_len = plen;
289 return 0;
292 /*ARGSUSED*/
293 void
294 puffs_stdpath_freepath(struct puffs_usermount *pu, struct puffs_pathobj *po)
297 free(po->po_path);