#include <sys/cdefs.h>
#if !defined(lint)
__RCSID("$NetBSD: paths.c,v 1.8 2008/08/12 19:44:39 pooka Exp $");
#endif
#include <sys/hash.h>
#include <assert.h>
#include <errno.h>
#include <puffs.h>
#include <stdlib.h>
#include "puffs_priv.h"
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) {
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;
}
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;
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;
assert(pn->pn_po.po_len > pi->pi_old->po_len);
rv = pu->pu_pathbuild(pu, pi->pi_new, &pn->pn_po,
pi->pi_old->po_len, &localpo);
if (rv != 0)
abort();
puffs_path_buildhash(pu, &localpo);
oldpo = pn->pn_po;
pn->pn_po = localpo;
pu->pu_pathfree(pu, &oldpo);
return NULL;
}
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 (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;
}
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);
}
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;
if (!(c1->po_len > c2->po_len))
return 1;
p = c1->po_path;
if ((*(p + clen)) != '/')
return 1;
return 0;
}
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;
pcomp = po_comp->po_path;
pcomp += offset;
while (*pcomp == '/') {
pcomp++;
complen--;
}
if (complen == 2 && strcmp(pcomp, "..") == 0)
isdotdot = 1;
else
isdotdot = 0;
prelen = po_pre->po_len;
while (prelen > 0 && *((char *)po_pre->po_path + (prelen-1)) == '/') {
assert(isdotdot == 0);
prelen--;
}
if (isdotdot) {
char *slash;
slash = strrchr(po_pre->po_path, '/');
assert(slash != NULL);
plen = slash - (char *)po_pre->po_path;
if (plen == 0)
plen++;
path = malloc(plen + 1);
if (path == NULL)
return errno;
strlcpy(path, po_pre->po_path, plen+1);
} else {
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;
}
void
puffs_stdpath_freepath(struct puffs_usermount *pu, struct puffs_pathobj *po)
{
free(po->po_path);
}