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