[BACK]Return to paths.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / lib / libpuffs

File: [cvs.NetBSD.org] / src / lib / libpuffs / paths.c (download)

Revision 1.8, Tue Aug 12 19:44:39 2008 UTC (11 years, 3 months ago) by pooka
Branch: MAIN
CVS Tags: yamt-pagecache-tag8, yamt-pagecache-base9, yamt-pagecache-base8, yamt-pagecache-base7, yamt-pagecache-base6, yamt-pagecache-base5, yamt-pagecache-base4, yamt-pagecache-base3, yamt-pagecache-base2, yamt-pagecache-base, yamt-pagecache, wrstuden-revivesa-base-3, wrstuden-revivesa-base-2, tls-maxphys-base, tls-maxphys, tls-earlyentropy-base, tls-earlyentropy, riastradh-xf86-video-intel-2-7-1-pre-2-21-15, riastradh-drm2-base3, riastradh-drm2-base2, riastradh-drm2-base1, riastradh-drm2-base, riastradh-drm2, prg-localcount2-base3, prg-localcount2-base2, prg-localcount2-base1, prg-localcount2-base, prg-localcount2, phil-wifi-base, phil-wifi-20191119, phil-wifi-20190609, phil-wifi, pgoyette-localcount-base, pgoyette-localcount-20170426, pgoyette-localcount-20170320, pgoyette-localcount-20170107, pgoyette-localcount-20161104, pgoyette-localcount-20160806, pgoyette-localcount-20160726, pgoyette-localcount, pgoyette-compat-merge-20190127, pgoyette-compat-base, pgoyette-compat-20190127, pgoyette-compat-20190118, pgoyette-compat-1226, pgoyette-compat-1126, pgoyette-compat-1020, pgoyette-compat-0930, pgoyette-compat-0906, pgoyette-compat-0728, pgoyette-compat-0625, pgoyette-compat-0521, pgoyette-compat-0502, pgoyette-compat-0422, pgoyette-compat-0415, pgoyette-compat-0407, pgoyette-compat-0330, pgoyette-compat-0322, pgoyette-compat-0315, pgoyette-compat, perseant-stdc-iso10646-base, perseant-stdc-iso10646, netbsd-9-base, netbsd-9-0-RC1, netbsd-9, netbsd-8-base, netbsd-8-1-RELEASE, netbsd-8-1-RC1, netbsd-8-0-RELEASE, netbsd-8-0-RC2, netbsd-8-0-RC1, netbsd-8, netbsd-7-nhusb-base-20170116, netbsd-7-nhusb-base, netbsd-7-nhusb, netbsd-7-base, netbsd-7-2-RELEASE, netbsd-7-1-RELEASE, netbsd-7-1-RC2, netbsd-7-1-RC1, netbsd-7-1-2-RELEASE, netbsd-7-1-1-RELEASE, netbsd-7-1, netbsd-7-0-RELEASE, netbsd-7-0-RC3, netbsd-7-0-RC2, netbsd-7-0-RC1, netbsd-7-0-2-RELEASE, netbsd-7-0-1-RELEASE, netbsd-7-0, netbsd-7, netbsd-6-base, netbsd-6-1-RELEASE, netbsd-6-1-RC4, netbsd-6-1-RC3, netbsd-6-1-RC2, netbsd-6-1-RC1, netbsd-6-1-5-RELEASE, netbsd-6-1-4-RELEASE, netbsd-6-1-3-RELEASE, netbsd-6-1-2-RELEASE, netbsd-6-1-1-RELEASE, netbsd-6-1, netbsd-6-0-RELEASE, netbsd-6-0-RC2, netbsd-6-0-RC1, netbsd-6-0-6-RELEASE, netbsd-6-0-5-RELEASE, netbsd-6-0-4-RELEASE, netbsd-6-0-3-RELEASE, netbsd-6-0-2-RELEASE, netbsd-6-0-1-RELEASE, netbsd-6-0, netbsd-6, netbsd-5-base, netbsd-5-2-RELEASE, netbsd-5-2-RC1, netbsd-5-2-3-RELEASE, netbsd-5-2-2-RELEASE, netbsd-5-2-1-RELEASE, netbsd-5-2, netbsd-5-1-RELEASE, netbsd-5-1-RC4, netbsd-5-1-RC3, netbsd-5-1-RC2, netbsd-5-1-RC1, netbsd-5-1-5-RELEASE, netbsd-5-1-4-RELEASE, netbsd-5-1-3-RELEASE, netbsd-5-1-2-RELEASE, netbsd-5-1-1-RELEASE, netbsd-5-1, netbsd-5-0-RELEASE, netbsd-5-0-RC4, netbsd-5-0-RC3, netbsd-5-0-RC2, netbsd-5-0-RC1, netbsd-5-0-2-RELEASE, netbsd-5-0-1-RELEASE, netbsd-5-0, netbsd-5, matt-premerge-20091211, matt-nb8-mediatek-base, matt-nb8-mediatek, matt-nb6-plus-nbase, matt-nb6-plus-base, matt-nb6-plus, matt-nb5-pq3-base, matt-nb5-pq3, matt-nb5-mips64-u2-k2-k4-k7-k8-k9, matt-nb5-mips64-u1-k1-k5, matt-nb5-mips64-premerge-20101231, matt-nb5-mips64-premerge-20091211, matt-nb5-mips64-k15, matt-nb5-mips64, matt-nb4-mips64-k7-u2a-k9b, matt-mips64-premerge-20101231, matt-mips64-base2, localcount-20160914, jym-xensuspend-nbase, jym-xensuspend-base, jym-xensuspend, cherry-xenmp-base, cherry-xenmp, bouyer-socketcan-base1, bouyer-socketcan-base, bouyer-socketcan, bouyer-quota2-nbase, bouyer-quota2-base, bouyer-quota2, agc-symver-base, agc-symver, HEAD
Changes since 1.7: +3 -3 lines

Replace void * by puffs_cookie_t where appropriate.  No functional change.

/*	$NetBSD: paths.c,v 1.8 2008/08/12 19:44:39 pooka Exp $	*/

/*
 * Copyright (c) 2007  Antti Kantee.  All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
#if !defined(lint)
__RCSID("$NetBSD: paths.c,v 1.8 2008/08/12 19:44:39 pooka Exp $");
#endif /* !lint */

#include <sys/hash.h>

#include <assert.h>
#include <errno.h>
#include <puffs.h>
#include <stdlib.h>

#include "puffs_priv.h"

/*
 * Generic routines for pathbuilding code
 */

int
puffs_path_pcnbuild(struct puffs_usermount *pu, struct puffs_cn *pcn,
	puffs_cookie_t parent)
{
	struct puffs_node *pn_parent = PU_CMAP(pu, parent);
	struct puffs_cn pcn_orig;
	struct puffs_pathobj po;
	int rv;

	assert(pn_parent->pn_po.po_path != NULL);
	assert(pu->pu_flags & PUFFS_FLAG_BUILDPATH);

	if (pu->pu_pathtransform) {
		rv = pu->pu_pathtransform(pu, &pn_parent->pn_po, pcn, &po);
		if (rv)
			return rv;
	} else {
		po.po_path = pcn->pcn_name;
		po.po_len = pcn->pcn_namelen;
	}

	if (pu->pu_namemod) {
		/* XXX: gcc complains if I do assignment */
		memcpy(&pcn_orig, pcn, sizeof(pcn_orig));
		rv = pu->pu_namemod(pu, &pn_parent->pn_po, pcn);
		if (rv)
			return rv;
	}

	rv = pu->pu_pathbuild(pu, &pn_parent->pn_po, &po, 0,
	    &pcn->pcn_po_full);
	puffs_path_buildhash(pu, &pcn->pcn_po_full);

	if (pu->pu_pathtransform)
		pu->pu_pathfree(pu, &po);

	if (pu->pu_namemod && rv)
		*pcn = pcn_orig;

	return rv;
}

/*
 * substitute all (child) patch prefixes.  called from nodewalk, which
 * in turn is called from rename
 */
void *
puffs_path_prefixadj(struct puffs_usermount *pu, struct puffs_node *pn,
	void *arg)
{
	struct puffs_pathinfo *pi = arg;
	struct puffs_pathobj localpo;
	struct puffs_pathobj oldpo;
	int rv;

	/* can't be a path prefix */
	if (pn->pn_po.po_len < pi->pi_old->po_len)
		return NULL;

	if (pu->pu_pathcmp(pu, &pn->pn_po, pi->pi_old, pi->pi_old->po_len, 1))
		return NULL;

	/* otherwise we'd have two nodes with an equal path */
	assert(pn->pn_po.po_len > pi->pi_old->po_len);

	/* found a matching prefix */
	rv = pu->pu_pathbuild(pu, pi->pi_new, &pn->pn_po,
	    pi->pi_old->po_len, &localpo);
	/*
	 * XXX: technically we shouldn't fail, but this is the only
	 * sensible thing to do here.  If the buildpath routine fails,
	 * we will have paths in an inconsistent state.  Should fix this,
	 * either by having two separate passes or by doing other tricks
	 * to make an invalid path with BUILDPATHS acceptable.
	 */
	if (rv != 0)
		abort();

	/* adjust hash sum */
	puffs_path_buildhash(pu, &localpo);

	/* out with the old and in with the new */
	oldpo = pn->pn_po;
	pn->pn_po = localpo;
	pu->pu_pathfree(pu, &oldpo);

	/* continue the walk */
	return NULL;
}

/*
 * called from nodewalk, checks for exact match
 */
void *
puffs_path_walkcmp(struct puffs_usermount *pu, struct puffs_node *pn, void *arg)
{
	struct puffs_pathobj *po = arg;
	struct puffs_pathobj po2;

	if (po->po_len != PNPLEN(pn))
		return NULL;

	/*
	 * If hashing and the hash doesn't match, we know this is
	 * definitely not a match.  Otherwise check for collisions.
	 */
	if (pu->pu_flags & PUFFS_FLAG_HASHPATH)
		if (pn->pn_po.po_hash != po->po_hash)
			return NULL;

	po2.po_path = PNPATH(pn);
	po2.po_len = PNPLEN(pn);

	if (pu->pu_pathcmp(pu, po, &po2, PNPLEN(pn), 0) == 0)
		return pn;
	return NULL;
}

/*
 * Hash sum building routine.  Use string hash if the buildpath routine
 * is the standard one, otherwise use binary hashes.  A bit whimsical
 * way to choose the routine, but the binary works for strings also,
 * so don't sweat it.
 */
void
puffs_path_buildhash(struct puffs_usermount *pu, struct puffs_pathobj *po)
{

	if ((pu->pu_flags & PUFFS_FLAG_HASHPATH) == 0)
		return;

	if (pu->pu_pathbuild == puffs_stdpath_buildpath)
		po->po_hash = hash32_strn(po->po_path, po->po_len,
		    HASH32_STR_INIT);
	else
		po->po_hash = hash32_buf(po->po_path, po->po_len,
		    HASH32_BUF_INIT);
}

/*
 * Routines provided to file systems which consider a path a tuple of
 * strings and / the component separator.
 */

/*ARGSUSED*/
int
puffs_stdpath_cmppath(struct puffs_usermount *pu, struct puffs_pathobj *c1,
	struct puffs_pathobj *c2, size_t clen, int checkprefix)
{
	char *p;
	int rv;

	rv = strncmp(c1->po_path, c2->po_path, clen);
	if (rv)
		return 1;

	if (checkprefix == 0)
		return 0;

	/* sanity for next step */
	if (!(c1->po_len > c2->po_len))
		return 1;

	/* check if it's really a complete path prefix */
	p = c1->po_path;
	if ((*(p + clen)) != '/')
		return 1;

	return 0;
}

/*ARGSUSED*/
int
puffs_stdpath_buildpath(struct puffs_usermount *pu,
	const struct puffs_pathobj *po_pre, const struct puffs_pathobj *po_comp,
	size_t offset, struct puffs_pathobj *newpath)
{
	char *path, *pcomp;
	size_t plen, complen;
	size_t prelen;
	int isdotdot;

	complen = po_comp->po_len - offset;

	/* seek to correct place & remove all leading '/' from component */
	pcomp = po_comp->po_path;
	pcomp += offset;
	while (*pcomp == '/') {
		pcomp++;
		complen--;
	}

	/* todotdot or nottodotdot */
	if (complen == 2 && strcmp(pcomp, "..") == 0)
		isdotdot = 1;
	else
		isdotdot = 0;

	/*
	 * Strip trailing components from the preceending component.
	 * This is an issue only for the root node, which we might want
	 * to be at path "/" for some file systems.
	 */
	prelen = po_pre->po_len;
	while (prelen > 0 && *((char *)po_pre->po_path + (prelen-1)) == '/') {
		assert(isdotdot == 0);
		prelen--;
	}

	if (isdotdot) {
		char *slash; /* sweet char of mine */
		
		slash = strrchr(po_pre->po_path, '/');
		assert(slash != NULL);

		plen = slash - (char *)po_pre->po_path;

		/*
		 * As the converse to not stripping the initial "/" above,
		 * don't nuke it here either.
		 */
		if (plen == 0)
			plen++;

		path = malloc(plen + 1);
		if (path == NULL)
			return errno;

		strlcpy(path, po_pre->po_path, plen+1);
	} else {
		/* + '/' + '\0' */
		plen = prelen + 1 + complen;
		path = malloc(plen + 1);
		if (path == NULL)
			return errno;

		strlcpy(path, po_pre->po_path, prelen+1);
		strcat(path, "/");
		strncat(path, pcomp, complen);
	}

	newpath->po_path = path;
	newpath->po_len = plen;

	return 0;
}

/*ARGSUSED*/
void
puffs_stdpath_freepath(struct puffs_usermount *pu, struct puffs_pathobj *po)
{

	free(po->po_path);
}