subr.c revision 1.7 1 /* $NetBSD: subr.c,v 1.7 2007/01/15 00:42:21 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.7 2007/01/15 00:42:21 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 puffs_inval_namecache_dir(puffs_cc_getusermount(pcc), pn);
107
108 pb = psbuf_make(PSB_OUT);
109 psbuf_req_str(pb, SSH_FXP_OPENDIR, reqid, PNPATH(pn));
110 pssh_outbuf_enqueue(pctx, pb, pcc, reqid);
111
112 puffs_cc_yield(pcc);
113
114 rv = psbuf_expect_handle(pb, &dhand, &dhandlen);
115 if (rv)
116 goto wayout;
117
118 /*
119 * Well, the following is O(n^2), so feel free to improve if it
120 * gets too taxing on your system.
121 */
122 olddir = psn->dir;
123 nent = psn->dentnext;
124
125 psn->dentnext = 0;
126 psn->denttot = 0;
127 psn->dir = NULL;
128 idx = 0;
129
130 for (;;) {
131 reqid = NEXTREQ(pctx);
132 psbuf_recycle(pb, PSB_OUT);
133 psbuf_req_data(pb, SSH_FXP_READDIR, reqid, dhand, dhandlen);
134 pssh_outbuf_enqueue(pctx, pb, pcc, reqid);
135
136 puffs_cc_yield(pcc);
137
138 /* check for EOF */
139 if (pb->type == SSH_FXP_STATUS) {
140 rv = psbuf_expect_status(pb);
141 goto out;
142 }
143 rv = psbuf_expect_name(pb, &count);
144 if (rv)
145 goto out;
146
147 for (; count--; idx++) {
148 if (idx == psn->denttot)
149 allocdirs(psn);
150 if (!psbuf_get_str(pb, &psn->dir[idx].entryname,
151 NULL)) {
152 rv = EPROTO;
153 goto out;
154 }
155 if (!psbuf_get_str(pb, &longname, NULL)) {
156 rv = EPROTO;
157 goto out;
158 }
159 if (!psbuf_get_vattr(pb, &psn->dir[idx].va)) {
160 rv = EPROTO;
161 goto out;
162 }
163 if (sscanf(longname, "%*s%d",
164 &psn->dir[idx].va.va_nlink) != 1) {
165 rv = EPROTO;
166 goto out;
167 }
168 free(longname);
169
170 testd = lookup(olddir, nent, psn->dir[idx].entryname);
171 if (testd) {
172 psn->dir[idx].entry = testd->entry;
173 psn->dir[idx].va.va_fileid
174 = testd->va.va_fileid;
175 } else {
176 psn->dir[idx].entry = NULL;
177 psn->dir[idx].va.va_fileid = pctx->nextino++;
178 }
179 psn->dir[idx].valid = 1;
180 }
181 }
182
183 out:
184 /* XXX: rv */
185 psn->dentnext = idx;
186 psn->dentread = time(NULL);
187 freedircache(olddir, nent);
188
189 reqid = NEXTREQ(pctx);
190 psbuf_recycle(pb, PSB_OUT);
191 psbuf_req_data(pb, SSH_FXP_CLOSE, reqid, dhand, dhandlen);
192 pssh_outbuf_enqueue(pctx, pb, pcc, reqid);
193
194 puffs_cc_yield(pcc);
195
196 /* EDONTCARE for the response */
197
198 wayout:
199 free(dhand);
200 psbuf_destroy(pb);
201 return rv;
202 }
203
204 struct puffs_node *
205 makenode(struct puffs_usermount *pu, struct puffs_node *parent,
206 struct psshfs_dir *pd, const struct vattr *vap)
207 {
208 struct psshfs_node *psn_parent = parent->pn_data;
209 struct psshfs_node *psn;
210 struct puffs_node *pn;
211
212 psn = emalloc(sizeof(struct psshfs_node));
213 memset(psn, 0, sizeof(struct psshfs_node));
214
215 pn = puffs_pn_new(pu, psn);
216 if (!pn) {
217 free(psn);
218 return NULL;
219 }
220 puffs_setvattr(&pn->pn_va, &pd->va);
221 puffs_setvattr(&pn->pn_va, vap);
222
223 pd->entry = pn;
224 psn->parent = parent;
225 psn_parent->childcount++;
226
227 return pn;
228 }
229
230 struct puffs_node *
231 allocnode(struct puffs_usermount *pu, struct puffs_node *parent,
232 const char *entryname, const struct vattr *vap)
233 {
234 struct psshfs_ctx *pctx = pu->pu_privdata;
235 struct psshfs_dir *pd;
236 struct puffs_node *pn;
237
238 pd = direnter(parent, entryname);
239
240 pd->va.va_fileid = pctx->nextino++;
241 if (vap->va_type == VDIR) {
242 pd->va.va_nlink = 2;
243 parent->pn_va.va_nlink++;
244 } else {
245 pd->va.va_nlink = 1;
246 }
247
248 pn = makenode(pu, parent, pd, vap);
249 if (pn)
250 pd->va.va_fileid = pn->pn_va.va_fileid;
251
252 return pn;
253 }
254
255 struct psshfs_dir *
256 direnter(struct puffs_node *parent, const char *entryname)
257 {
258 struct psshfs_node *psn_parent = parent->pn_data;
259 struct psshfs_dir *pd;
260 int i;
261
262 /* create directory entry */
263 if (psn_parent->denttot == psn_parent->dentnext)
264 allocdirs(psn_parent);
265
266 i = psn_parent->dentnext;
267 pd = &psn_parent->dir[i];
268 pd->entryname = estrdup(entryname);
269 pd->valid = 1;
270 puffs_vattr_null(&pd->va);
271 psn_parent->dentnext++;
272
273 return pd;
274 }
275
276 void
277 nukenode(struct puffs_node *node, const char *entryname, int destroy)
278 {
279 struct psshfs_node *psn, *psn_parent;
280 struct psshfs_dir *pd;
281
282 psn = node->pn_data;
283 psn_parent = psn->parent->pn_data;
284 psn_parent->childcount--;
285 pd = lookup(psn_parent->dir, psn_parent->dentnext, entryname);
286 assert(pd != NULL);
287 pd->valid = 0;
288 free(pd->entryname);
289 pd->entryname = NULL;
290
291 if (node->pn_va.va_type == VDIR) {
292 psn->parent->pn_va.va_nlink--;
293 if (destroy)
294 freedircache(psn->dir, psn->dentnext);
295 }
296
297 if (destroy)
298 puffs_pn_put(node);
299 }
300