Home | History | Annotate | Line # | Download | only in libpuffs
paths.c revision 1.6
      1 /*	$NetBSD: paths.c,v 1.6 2007/05/01 15:58:00 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.6 2007/05/01 15:58:00 pooka Exp $");
     34 #endif /* !lint */
     35 
     36 #include <sys/hash.h>
     37 
     38 #include <assert.h>
     39 #include <errno.h>
     40 #include <puffs.h>
     41 #include <stdlib.h>
     42 
     43 #include "puffs_priv.h"
     44 
     45 /*
     46  * Generic routines for pathbuilding code
     47  */
     48 
     49 int
     50 puffs_path_pcnbuild(struct puffs_usermount *pu, struct puffs_cn *pcn,
     51 	void *parent)
     52 {
     53 	struct puffs_node *pn_parent = PU_CMAP(pu, parent);
     54 	struct puffs_cn pcn_orig;
     55 	struct puffs_pathobj po;
     56 	int rv;
     57 
     58 	assert(pn_parent->pn_po.po_path != NULL);
     59 	assert(pu->pu_flags & PUFFS_FLAG_BUILDPATH);
     60 
     61 	if (pu->pu_pathtransform) {
     62 		rv = pu->pu_pathtransform(pu, &pn_parent->pn_po, pcn, &po);
     63 		if (rv)
     64 			return rv;
     65 	} else {
     66 		po.po_path = pcn->pcn_name;
     67 		po.po_len = pcn->pcn_namelen;
     68 	}
     69 
     70 	if (pu->pu_namemod) {
     71 		/* XXX: gcc complains if I do assignment */
     72 		memcpy(&pcn_orig, pcn, sizeof(pcn_orig));
     73 		rv = pu->pu_namemod(pu, &pn_parent->pn_po, pcn);
     74 		if (rv)
     75 			return rv;
     76 	}
     77 
     78 	rv = pu->pu_pathbuild(pu, &pn_parent->pn_po, &po, 0,
     79 	    &pcn->pcn_po_full);
     80 	puffs_path_buildhash(pu, &pcn->pcn_po_full);
     81 
     82 	if (pu->pu_pathtransform)
     83 		pu->pu_pathfree(pu, &po);
     84 
     85 	if (pu->pu_namemod && rv)
     86 		*pcn = pcn_orig;
     87 
     88 	return rv;
     89 }
     90 
     91 /*
     92  * substitute all (child) patch prefixes.  called from nodewalk, which
     93  * in turn is called from rename
     94  */
     95 void *
     96 puffs_path_prefixadj(struct puffs_usermount *pu, struct puffs_node *pn,
     97 	void *arg)
     98 {
     99 	struct puffs_pathinfo *pi = arg;
    100 	struct puffs_pathobj localpo;
    101 	struct puffs_pathobj oldpo;
    102 	int rv;
    103 
    104 	/* can't be a path prefix */
    105 	if (pn->pn_po.po_len < pi->pi_old->po_len)
    106 		return NULL;
    107 
    108 	if (pu->pu_pathcmp(pu, &pn->pn_po, pi->pi_old, pi->pi_old->po_len, 1))
    109 		return NULL;
    110 
    111 	/* otherwise we'd have two nodes with an equal path */
    112 	assert(pn->pn_po.po_len > pi->pi_old->po_len);
    113 
    114 	/* found a matching prefix */
    115 	rv = pu->pu_pathbuild(pu, pi->pi_new, &pn->pn_po,
    116 	    pi->pi_old->po_len, &localpo);
    117 	/*
    118 	 * XXX: technically we shouldn't fail, but this is the only
    119 	 * sensible thing to do here.  If the buildpath routine fails,
    120 	 * we will have paths in an inconsistent state.  Should fix this,
    121 	 * either by having two separate passes or by doing other tricks
    122 	 * to make an invalid path with BUILDPATHS acceptable.
    123 	 */
    124 	if (rv != 0)
    125 		abort();
    126 
    127 	/* adjust hash sum */
    128 	puffs_path_buildhash(pu, &localpo);
    129 
    130 	/* out with the old and in with the new */
    131 	oldpo = pn->pn_po;
    132 	pn->pn_po = localpo;
    133 	pu->pu_pathfree(pu, &oldpo);
    134 
    135 	/* continue the walk */
    136 	return NULL;
    137 }
    138 
    139 /*
    140  * called from nodewalk, checks for exact match
    141  */
    142 void *
    143 puffs_path_walkcmp(struct puffs_usermount *pu, struct puffs_node *pn, void *arg)
    144 {
    145 	struct puffs_pathobj *po = arg;
    146 	struct puffs_pathobj po2;
    147 
    148 	if (po->po_len != PNPLEN(pn))
    149 		return NULL;
    150 
    151 	/*
    152 	 * If hashing and the hash doesn't match, we know this is
    153 	 * definitely not a match.  Otherwise check for collisions.
    154 	 */
    155 	if (pu->pu_flags & PUFFS_FLAG_HASHPATH)
    156 		if (pn->pn_po.po_hash != po->po_hash)
    157 			return NULL;
    158 
    159 	po2.po_path = PNPATH(pn);
    160 	po2.po_len = PNPLEN(pn);
    161 
    162 	if (pu->pu_pathcmp(pu, po, &po2, PNPLEN(pn), 0) == 0)
    163 		return pn;
    164 	return NULL;
    165 }
    166 
    167 /*
    168  * Hash sum building routine.  Use string hash if the buildpath routine
    169  * is the standard one, otherwise use binary hashes.  A bit whimsical
    170  * way to choose the routine, but the binary works for strings also,
    171  * so don't sweat it.
    172  */
    173 void
    174 puffs_path_buildhash(struct puffs_usermount *pu, struct puffs_pathobj *po)
    175 {
    176 
    177 	if ((pu->pu_flags & PUFFS_FLAG_HASHPATH) == 0)
    178 		return;
    179 
    180 	if (pu->pu_pathbuild == puffs_stdpath_buildpath)
    181 		po->po_hash = hash32_strn(po->po_path, po->po_len,
    182 		    HASH32_STR_INIT);
    183 	else
    184 		po->po_hash = hash32_buf(po->po_path, po->po_len,
    185 		    HASH32_BUF_INIT);
    186 }
    187 
    188 /*
    189  * Routines provided to file systems which consider a path a tuple of
    190  * strings and / the component separator.
    191  */
    192 
    193 /*ARGSUSED*/
    194 int
    195 puffs_stdpath_cmppath(struct puffs_usermount *pu, struct puffs_pathobj *c1,
    196 	struct puffs_pathobj *c2, size_t clen, int checkprefix)
    197 {
    198 	char *p;
    199 	int rv;
    200 
    201 	rv = strncmp(c1->po_path, c2->po_path, clen);
    202 	if (rv)
    203 		return 1;
    204 
    205 	if (checkprefix == 0)
    206 		return 0;
    207 
    208 	/* sanity for next step */
    209 	if (!(c1->po_len > c2->po_len))
    210 		return 1;
    211 
    212 	/* check if it's really a complete path prefix */
    213 	p = c1->po_path;
    214 	if ((*(p + clen)) != '/')
    215 		return 1;
    216 
    217 	return 0;
    218 }
    219 
    220 /*ARGSUSED*/
    221 int
    222 puffs_stdpath_buildpath(struct puffs_usermount *pu,
    223 	const struct puffs_pathobj *po_pre, const struct puffs_pathobj *po_comp,
    224 	size_t offset, struct puffs_pathobj *newpath)
    225 {
    226 	char *path, *pcomp;
    227 	size_t plen, complen;
    228 	size_t prelen;
    229 	int isdotdot;
    230 
    231 	complen = po_comp->po_len - offset;
    232 
    233 	/* seek to correct place & remove all leading '/' from component */
    234 	pcomp = po_comp->po_path;
    235 	pcomp += offset;
    236 	while (*pcomp == '/') {
    237 		pcomp++;
    238 		complen--;
    239 	}
    240 
    241 	/* todotdot or nottodotdot */
    242 	if (complen == 2 && strcmp(pcomp, "..") == 0)
    243 		isdotdot = 1;
    244 	else
    245 		isdotdot = 0;
    246 
    247 	/*
    248 	 * Strip trailing components from the preceending component.
    249 	 * This is an issue only for the root node, which we might want
    250 	 * to be at path "/" for some file systems.
    251 	 */
    252 	prelen = po_pre->po_len;
    253 	while (prelen > 0 && *((char *)po_pre->po_path + (prelen-1)) == '/') {
    254 		assert(isdotdot == 0);
    255 		prelen--;
    256 	}
    257 
    258 	if (isdotdot) {
    259 		char *slash; /* sweet char of mine */
    260 
    261 		slash = strrchr(po_pre->po_path, '/');
    262 		assert(slash != NULL);
    263 
    264 		plen = slash - (char *)po_pre->po_path;
    265 
    266 		/*
    267 		 * As the converse to not stripping the initial "/" above,
    268 		 * don't nuke it here either.
    269 		 */
    270 		if (plen == 0)
    271 			plen++;
    272 
    273 		path = malloc(plen + 1);
    274 		if (path == NULL)
    275 			return errno;
    276 
    277 		strlcpy(path, po_pre->po_path, plen+1);
    278 	} else {
    279 		/* + '/' + '\0' */
    280 		plen = prelen + 1 + complen;
    281 		path = malloc(plen + 1);
    282 		if (path == NULL)
    283 			return errno;
    284 
    285 		strlcpy(path, po_pre->po_path, prelen+1);
    286 		strcat(path, "/");
    287 		strncat(path, pcomp, complen);
    288 	}
    289 
    290 	newpath->po_path = path;
    291 	newpath->po_len = plen;
    292 
    293 	return 0;
    294 }
    295 
    296 /*ARGSUSED*/
    297 void
    298 puffs_stdpath_freepath(struct puffs_usermount *pu, struct puffs_pathobj *po)
    299 {
    300 
    301 	free(po->po_path);
    302 }
    303