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