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