Home | History | Annotate | Line # | Download | only in mount_psshfs
subr.c revision 1.4
      1  1.4  pooka /*      $NetBSD: subr.c,v 1.4 2007/01/09 12:34:20 pooka Exp $        */
      2  1.1  pooka 
      3  1.1  pooka /*
      4  1.1  pooka  * Copyright (c) 2006  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 #ifndef lint
     33  1.4  pooka __RCSID("$NetBSD: subr.c,v 1.4 2007/01/09 12:34:20 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 #include <util.h>
     41  1.1  pooka 
     42  1.1  pooka #include "psshfs.h"
     43  1.1  pooka #include "sftp_proto.h"
     44  1.1  pooka 
     45  1.1  pooka static void
     46  1.1  pooka freedircache(struct psshfs_dir *base, size_t count)
     47  1.1  pooka {
     48  1.1  pooka 	int i;
     49  1.1  pooka 
     50  1.1  pooka 	for (i = 0; i < count; i++) {
     51  1.1  pooka 		free(base[i].entryname);
     52  1.1  pooka 		base[i].entryname = NULL;
     53  1.1  pooka 	}
     54  1.1  pooka 
     55  1.1  pooka 	free(base);
     56  1.1  pooka }
     57  1.1  pooka 
     58  1.1  pooka #define ENTRYCHUNK 16
     59  1.1  pooka static void
     60  1.1  pooka allocdirs(struct psshfs_node *psn)
     61  1.1  pooka {
     62  1.1  pooka 	size_t oldtot = psn->denttot;
     63  1.1  pooka 
     64  1.1  pooka 	psn->denttot += ENTRYCHUNK;
     65  1.1  pooka 	psn->dir = erealloc(psn->dir,
     66  1.1  pooka 	    psn->denttot * sizeof(struct psshfs_dir));
     67  1.1  pooka 	memset(psn->dir + oldtot, 0, ENTRYCHUNK * sizeof(struct psshfs_dir));
     68  1.1  pooka }
     69  1.1  pooka 
     70  1.1  pooka struct psshfs_dir *
     71  1.1  pooka lookup(struct psshfs_dir *basedir, size_t ndir, const char *name)
     72  1.1  pooka {
     73  1.1  pooka 	struct psshfs_dir *ent;
     74  1.1  pooka 	int i;
     75  1.1  pooka 
     76  1.1  pooka 	for (i = 0; i < ndir; i++) {
     77  1.1  pooka 		ent = &basedir[i];
     78  1.1  pooka 		if (ent->valid != 1)
     79  1.1  pooka 			continue;
     80  1.1  pooka 		if (strcmp(ent->entryname, name) == 0)
     81  1.1  pooka 			return ent;
     82  1.1  pooka 	}
     83  1.1  pooka 
     84  1.1  pooka 	return NULL;
     85  1.1  pooka }
     86  1.1  pooka 
     87  1.1  pooka int
     88  1.1  pooka sftp_readdir(struct puffs_cc *pcc, struct psshfs_ctx *pctx,
     89  1.1  pooka 	struct puffs_node *pn)
     90  1.1  pooka {
     91  1.1  pooka 	struct psshfs_node *psn = pn->pn_data;
     92  1.1  pooka 	struct psshfs_dir *olddir, *testd;
     93  1.1  pooka 	struct psbuf *pb;
     94  1.1  pooka 	uint32_t reqid = NEXTREQ(pctx);
     95  1.1  pooka 	uint32_t count;
     96  1.1  pooka 	char *dhand = NULL;
     97  1.1  pooka 	size_t dhandlen, nent;
     98  1.1  pooka 	char *longname;
     99  1.1  pooka 	int idx, rv;
    100  1.1  pooka 
    101  1.1  pooka 	assert(pn->pn_va.va_type == VDIR);
    102  1.1  pooka 
    103  1.1  pooka 	if (psn->dir && (time(NULL) - psn->dentread) < PSSHFS_REFRESHIVAL)
    104  1.1  pooka 		return 0;
    105  1.1  pooka 
    106  1.1  pooka 	pb = psbuf_make(PSB_OUT);
    107  1.1  pooka 	psbuf_req_str(pb, SSH_FXP_OPENDIR, reqid, pn->pn_path);
    108  1.1  pooka 	pssh_outbuf_enqueue(pctx, pb, pcc, reqid);
    109  1.1  pooka 
    110  1.1  pooka 	puffs_cc_yield(pcc);
    111  1.1  pooka 
    112  1.1  pooka 	rv = psbuf_expect_handle(pb, &dhand, &dhandlen);
    113  1.1  pooka 	if (rv)
    114  1.1  pooka 		goto wayout;
    115  1.1  pooka 
    116  1.1  pooka 	/*
    117  1.1  pooka 	 * Well, the following is O(n^2), so feel free to improve if it
    118  1.1  pooka 	 * gets too taxing on your system.
    119  1.1  pooka 	 */
    120  1.1  pooka 	olddir = psn->dir;
    121  1.1  pooka 	nent = psn->dentnext;
    122  1.1  pooka 
    123  1.1  pooka 	psn->dentnext = 0;
    124  1.1  pooka 	psn->denttot = 0;
    125  1.1  pooka 	psn->dir = NULL;
    126  1.1  pooka 	idx = 0;
    127  1.1  pooka 
    128  1.1  pooka 	for (;;) {
    129  1.1  pooka 		reqid = NEXTREQ(pctx);
    130  1.1  pooka 		psbuf_recycle(pb, PSB_OUT);
    131  1.1  pooka 		psbuf_req_data(pb, SSH_FXP_READDIR, reqid, dhand, dhandlen);
    132  1.1  pooka 		pssh_outbuf_enqueue(pctx, pb, pcc, reqid);
    133  1.1  pooka 
    134  1.1  pooka 		puffs_cc_yield(pcc);
    135  1.1  pooka 
    136  1.1  pooka 		/* check for EOF */
    137  1.1  pooka 		if (pb->type == SSH_FXP_STATUS) {
    138  1.1  pooka 			rv = psbuf_expect_status(pb);
    139  1.1  pooka 			goto out;
    140  1.1  pooka 		}
    141  1.1  pooka 		rv = psbuf_expect_name(pb, &count);
    142  1.1  pooka 		if (rv)
    143  1.1  pooka 			goto out;
    144  1.1  pooka 
    145  1.1  pooka 		for (; count--; idx++) {
    146  1.1  pooka 			if (idx == psn->denttot)
    147  1.1  pooka 				allocdirs(psn);
    148  1.1  pooka 			if (!psbuf_get_str(pb, &psn->dir[idx].entryname,
    149  1.1  pooka 			    NULL)) {
    150  1.1  pooka 				rv = EPROTO;
    151  1.1  pooka 				goto out;
    152  1.1  pooka 			}
    153  1.1  pooka 			if (!psbuf_get_str(pb, &longname, NULL)) {
    154  1.1  pooka 				rv = EPROTO;
    155  1.1  pooka 				goto out;
    156  1.1  pooka 			}
    157  1.1  pooka 			if (!psbuf_get_vattr(pb, &psn->dir[idx].va)) {
    158  1.1  pooka 				rv = EPROTO;
    159  1.1  pooka 				goto out;
    160  1.1  pooka 			}
    161  1.1  pooka 			if (sscanf(longname, "%*s%d",
    162  1.1  pooka 			    &psn->dir[idx].va.va_nlink) != 1) {
    163  1.1  pooka 				rv = EPROTO;
    164  1.1  pooka 				goto out;
    165  1.1  pooka 			}
    166  1.1  pooka 			free(longname);
    167  1.1  pooka 
    168  1.1  pooka 			testd = lookup(olddir, nent, psn->dir[idx].entryname);
    169  1.1  pooka 			if (testd) {
    170  1.1  pooka 				psn->dir[idx].entry = testd->entry;
    171  1.3  pooka 				psn->dir[idx].va.va_fileid
    172  1.3  pooka 				    = testd->va.va_fileid;
    173  1.1  pooka 			} else {
    174  1.1  pooka 				psn->dir[idx].entry = NULL;
    175  1.1  pooka 				psn->dir[idx].va.va_fileid = pctx->nextino++;
    176  1.1  pooka 			}
    177  1.1  pooka 			psn->dir[idx].valid = 1;
    178  1.1  pooka 		}
    179  1.1  pooka 	}
    180  1.1  pooka 
    181  1.1  pooka  out:
    182  1.1  pooka 	/* XXX: rv */
    183  1.1  pooka 	psn->dentnext = idx;
    184  1.1  pooka 	psn->dentread = time(NULL);
    185  1.1  pooka 	freedircache(olddir, nent);
    186  1.1  pooka 
    187  1.1  pooka 	reqid = NEXTREQ(pctx);
    188  1.1  pooka 	psbuf_recycle(pb, PSB_OUT);
    189  1.1  pooka 	psbuf_req_data(pb, SSH_FXP_CLOSE, reqid, dhand, dhandlen);
    190  1.1  pooka 	pssh_outbuf_enqueue(pctx, pb, pcc, reqid);
    191  1.1  pooka 
    192  1.1  pooka 	puffs_cc_yield(pcc);
    193  1.1  pooka 
    194  1.1  pooka 	/* EDONTCARE for the response */
    195  1.1  pooka 
    196  1.1  pooka  wayout:
    197  1.1  pooka 	free(dhand);
    198  1.1  pooka 	psbuf_destroy(pb);
    199  1.1  pooka 	return rv;
    200  1.1  pooka }
    201  1.1  pooka 
    202  1.1  pooka struct puffs_node *
    203  1.1  pooka makenode(struct puffs_usermount *pu, struct puffs_node *parent,
    204  1.1  pooka 	struct psshfs_dir *pd, const struct vattr *vap)
    205  1.1  pooka {
    206  1.1  pooka 	struct psshfs_node *psn_parent = parent->pn_data;
    207  1.1  pooka 	struct psshfs_node *psn;
    208  1.1  pooka 	struct puffs_node *pn;
    209  1.1  pooka 
    210  1.1  pooka 	psn = emalloc(sizeof(struct psshfs_node));
    211  1.1  pooka 	memset(psn, 0, sizeof(struct psshfs_node));
    212  1.1  pooka 
    213  1.1  pooka 	pn = puffs_pn_new(pu, psn);
    214  1.1  pooka 	if (!pn) {
    215  1.1  pooka 		free(psn);
    216  1.1  pooka 		return NULL;
    217  1.1  pooka 	}
    218  1.1  pooka 	puffs_setvattr(&pn->pn_va, &pd->va);
    219  1.1  pooka 	puffs_setvattr(&pn->pn_va, vap);
    220  1.1  pooka 
    221  1.1  pooka 	pd->entry = pn;
    222  1.1  pooka 	psn->parent = parent;
    223  1.1  pooka 	psn_parent->childcount++;
    224  1.1  pooka 
    225  1.1  pooka 	return pn;
    226  1.1  pooka }
    227  1.1  pooka 
    228  1.1  pooka struct puffs_node *
    229  1.1  pooka allocnode(struct puffs_usermount *pu, struct puffs_node *parent,
    230  1.1  pooka 	const char *entryname, const struct vattr *vap)
    231  1.1  pooka {
    232  1.1  pooka 	struct psshfs_ctx *pctx = pu->pu_privdata;
    233  1.1  pooka 	struct psshfs_dir *pd;
    234  1.3  pooka 	struct puffs_node *pn;
    235  1.1  pooka 
    236  1.1  pooka 	pd = direnter(parent, entryname);
    237  1.1  pooka 
    238  1.1  pooka 	pd->va.va_fileid = pctx->nextino++;
    239  1.2  pooka 	if (vap->va_type == VDIR) {
    240  1.1  pooka 		pd->va.va_nlink = 2;
    241  1.2  pooka 		parent->pn_va.va_nlink++;
    242  1.2  pooka 	} else {
    243  1.1  pooka 		pd->va.va_nlink = 1;
    244  1.2  pooka 	}
    245  1.1  pooka 
    246  1.3  pooka 	pn = makenode(pu, parent, pd, vap);
    247  1.3  pooka 	if (pn)
    248  1.3  pooka 		pd->va.va_fileid = pn->pn_va.va_fileid;
    249  1.3  pooka 
    250  1.3  pooka 	return pn;
    251  1.1  pooka }
    252  1.1  pooka 
    253  1.1  pooka struct psshfs_dir *
    254  1.1  pooka direnter(struct puffs_node *parent, const char *entryname)
    255  1.1  pooka {
    256  1.1  pooka 	struct psshfs_node *psn_parent = parent->pn_data;
    257  1.1  pooka 	struct psshfs_dir *pd;
    258  1.1  pooka 	int i;
    259  1.1  pooka 
    260  1.1  pooka 	/* create directory entry */
    261  1.1  pooka 	if (psn_parent->denttot == psn_parent->dentnext)
    262  1.1  pooka 		allocdirs(psn_parent);
    263  1.1  pooka 
    264  1.1  pooka 	i = psn_parent->dentnext;
    265  1.1  pooka 	pd = &psn_parent->dir[i];
    266  1.1  pooka 	pd->entryname = estrdup(entryname);
    267  1.1  pooka 	pd->valid = 1;
    268  1.4  pooka 	puffs_vattr_null(&pd->va);
    269  1.1  pooka 	psn_parent->dentnext++;
    270  1.1  pooka 
    271  1.1  pooka 	return pd;
    272  1.1  pooka }
    273  1.1  pooka 
    274  1.1  pooka void
    275  1.1  pooka nukenode(struct puffs_node *node, const char *entryname)
    276  1.1  pooka {
    277  1.1  pooka 	struct psshfs_node *psn, *psn_parent;
    278  1.1  pooka 	struct psshfs_dir *pd;
    279  1.1  pooka 
    280  1.1  pooka 	psn = node->pn_data;
    281  1.1  pooka 	psn_parent = psn->parent->pn_data;
    282  1.1  pooka 	psn_parent->childcount--;
    283  1.1  pooka 	pd = lookup(psn_parent->dir, psn_parent->dentnext, entryname);
    284  1.1  pooka 	assert(pd != NULL);
    285  1.1  pooka 	pd->valid = 0;
    286  1.1  pooka 	free(pd->entryname);
    287  1.1  pooka 	pd->entryname = NULL;
    288  1.1  pooka 
    289  1.1  pooka 	if (node->pn_va.va_type == VDIR) {
    290  1.1  pooka 		psn->parent->pn_va.va_nlink--;
    291  1.1  pooka 		freedircache(psn->dir, psn->dentnext);
    292  1.1  pooka 	}
    293  1.1  pooka }
    294