subr.c revision 1.8 1 /* $NetBSD: subr.c,v 1.8 2007/02/09 23:36:17 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.8 2007/02/09 23:36:17 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 #ifdef SUPERREADDIR
88 struct readdirattr {
89 struct psshfs_node *psn;
90 int idx;
91 char entryname[MAXPATHLEN+1];
92 };
93
94 static void
95 readdir_getattr_resp(struct psshfs_ctx *pctx, struct psbuf *pb, void *arg)
96 {
97 struct readdirattr *rda = arg;
98 struct psshfs_node *psn = rda->psn;
99 struct psshfs_dir *pdir;
100 struct vattr va;
101
102 pdir = lookup(psn->dir, psn->denttot, rda->entryname);
103 if (!pdir)
104 goto out;
105
106 if (psbuf_expect_attrs(pb, &va))
107 goto out;
108
109 if (pdir->entry) {
110 struct psshfs_node *psn_targ;
111 psn_targ = pdir->entry->pn_data;
112
113 puffs_setvattr(&pdir->entry->pn_va, &va);
114 psn_targ->attrread = time(NULL);
115 } else {
116 puffs_setvattr(&pdir->va, &va);
117 pdir->attrread = time(NULL);
118 }
119
120 out:
121 free(rda);
122 psbuf_destroy(pb);
123 }
124
125 static void
126 readdir_getattr(struct psshfs_ctx *pctx, struct psshfs_node *psn,
127 const char *basepath, int idx)
128 {
129 char path[MAXPATHLEN+1];
130 struct psshfs_dir *pdir = psn->dir;
131 struct psbuf *pb;
132 struct readdirattr *rda;
133 const char *entryname = pdir[idx].entryname;
134 uint32_t reqid = NEXTREQ(pctx);
135
136 rda = emalloc(sizeof(struct readdirattr));
137 rda->psn = psn;
138 rda->idx = idx;
139 strlcpy(rda->entryname, entryname, sizeof(rda->entryname));
140
141 strcpy(path, basepath);
142 strcat(path, "/");
143 strlcat(path, entryname, sizeof(path));
144
145 pb = psbuf_make(PSB_OUT);
146 psbuf_req_str(pb, SSH_FXP_LSTAT, reqid, path);
147 pssh_outbuf_enqueue_nocc(pctx, pb, readdir_getattr_resp, rda, reqid);
148 }
149 #endif
150
151 int
152 sftp_readdir(struct puffs_cc *pcc, struct psshfs_ctx *pctx,
153 struct puffs_node *pn)
154 {
155 struct psshfs_node *psn = pn->pn_data;
156 struct psshfs_dir *olddir, *testd;
157 struct psbuf *pb;
158 uint32_t reqid = NEXTREQ(pctx);
159 uint32_t count;
160 char *dhand = NULL;
161 size_t dhandlen, nent;
162 char *longname;
163 int idx, rv;
164
165 assert(pn->pn_va.va_type == VDIR);
166
167 if (psn->dir && (time(NULL) - psn->dentread) < PSSHFS_REFRESHIVAL)
168 return 0;
169
170 puffs_inval_namecache_dir(puffs_cc_getusermount(pcc), pn);
171
172 pb = psbuf_make(PSB_OUT);
173 psbuf_req_str(pb, SSH_FXP_OPENDIR, reqid, PNPATH(pn));
174 pssh_outbuf_enqueue(pctx, pb, pcc, reqid);
175
176 puffs_cc_yield(pcc);
177
178 rv = psbuf_expect_handle(pb, &dhand, &dhandlen);
179 if (rv)
180 goto wayout;
181
182 /*
183 * Well, the following is O(n^2), so feel free to improve if it
184 * gets too taxing on your system.
185 */
186 olddir = psn->dir;
187 nent = psn->dentnext;
188
189 /*
190 * note: for the "getattr in batch" to work, this must be before
191 * the attribute-getting. Otherwise times for first entries in
192 * large directories might expire before the directory itself and
193 * result in one-by-one attribute fetching.
194 */
195 psn->dentread = time(NULL);
196
197 psn->dentnext = 0;
198 psn->denttot = 0;
199 psn->dir = NULL;
200 idx = 0;
201
202 for (;;) {
203 reqid = NEXTREQ(pctx);
204 psbuf_recycle(pb, PSB_OUT);
205 psbuf_req_data(pb, SSH_FXP_READDIR, reqid, dhand, dhandlen);
206 pssh_outbuf_enqueue(pctx, pb, pcc, reqid);
207
208 puffs_cc_yield(pcc);
209
210 /* check for EOF */
211 if (pb->type == SSH_FXP_STATUS) {
212 rv = psbuf_expect_status(pb);
213 goto out;
214 }
215 rv = psbuf_expect_name(pb, &count);
216 if (rv)
217 goto out;
218
219 for (; count--; idx++) {
220 if (idx == psn->denttot)
221 allocdirs(psn);
222 if (!psbuf_get_str(pb, &psn->dir[idx].entryname,
223 NULL)) {
224 rv = EPROTO;
225 goto out;
226 }
227 if (!psbuf_get_str(pb, &longname, NULL)) {
228 rv = EPROTO;
229 goto out;
230 }
231 if (!psbuf_get_vattr(pb, &psn->dir[idx].va)) {
232 rv = EPROTO;
233 goto out;
234 }
235 if (sscanf(longname, "%*s%d",
236 &psn->dir[idx].va.va_nlink) != 1) {
237 rv = EPROTO;
238 goto out;
239 }
240 free(longname);
241
242 testd = lookup(olddir, nent, psn->dir[idx].entryname);
243 if (testd) {
244 psn->dir[idx].entry = testd->entry;
245 psn->dir[idx].va = testd->va;
246 } else {
247 psn->dir[idx].entry = NULL;
248 psn->dir[idx].va.va_fileid = pctx->nextino++;
249 }
250 #ifdef SUPERREADDIR
251 /*
252 * XXX: there's a dangling pointer race here if
253 * the server responds to our queries out-of-order.
254 * fixxxme some day
255 */
256 readdir_getattr(pctx, psn, PNPATH(pn), idx);
257 #endif
258 psn->dir[idx].valid = 1;
259 }
260 }
261
262 out:
263 /* XXX: rv */
264 psn->dentnext = idx;
265 freedircache(olddir, nent);
266
267 reqid = NEXTREQ(pctx);
268 psbuf_recycle(pb, PSB_OUT);
269 psbuf_req_data(pb, SSH_FXP_CLOSE, reqid, dhand, dhandlen);
270 pssh_outbuf_enqueue(pctx, pb, pcc, reqid);
271
272 puffs_cc_yield(pcc);
273
274 /* EDONTCARE for the response */
275
276 wayout:
277 free(dhand);
278 psbuf_destroy(pb);
279 return rv;
280 }
281
282 struct puffs_node *
283 makenode(struct puffs_usermount *pu, struct puffs_node *parent,
284 struct psshfs_dir *pd, const struct vattr *vap)
285 {
286 struct psshfs_node *psn_parent = parent->pn_data;
287 struct psshfs_node *psn;
288 struct puffs_node *pn;
289
290 psn = emalloc(sizeof(struct psshfs_node));
291 memset(psn, 0, sizeof(struct psshfs_node));
292
293 pn = puffs_pn_new(pu, psn);
294 if (!pn) {
295 free(psn);
296 return NULL;
297 }
298 puffs_setvattr(&pn->pn_va, &pd->va);
299 psn->attrread = pd->attrread;
300 puffs_setvattr(&pn->pn_va, vap);
301
302 pd->entry = pn;
303 psn->parent = parent;
304 psn_parent->childcount++;
305
306 return pn;
307 }
308
309 struct puffs_node *
310 allocnode(struct puffs_usermount *pu, struct puffs_node *parent,
311 const char *entryname, const struct vattr *vap)
312 {
313 struct psshfs_ctx *pctx = pu->pu_privdata;
314 struct psshfs_dir *pd;
315 struct puffs_node *pn;
316
317 pd = direnter(parent, entryname);
318
319 pd->va.va_fileid = pctx->nextino++;
320 if (vap->va_type == VDIR) {
321 pd->va.va_nlink = 2;
322 parent->pn_va.va_nlink++;
323 } else {
324 pd->va.va_nlink = 1;
325 }
326
327 pn = makenode(pu, parent, pd, vap);
328 if (pn)
329 pd->va.va_fileid = pn->pn_va.va_fileid;
330
331 return pn;
332 }
333
334 struct psshfs_dir *
335 direnter(struct puffs_node *parent, const char *entryname)
336 {
337 struct psshfs_node *psn_parent = parent->pn_data;
338 struct psshfs_dir *pd;
339 int i;
340
341 /* create directory entry */
342 if (psn_parent->denttot == psn_parent->dentnext)
343 allocdirs(psn_parent);
344
345 i = psn_parent->dentnext;
346 pd = &psn_parent->dir[i];
347 pd->entryname = estrdup(entryname);
348 pd->valid = 1;
349 pd->attrread = 0;
350 puffs_vattr_null(&pd->va);
351 psn_parent->dentnext++;
352
353 return pd;
354 }
355
356 void
357 nukenode(struct puffs_node *node, const char *entryname, int destroy)
358 {
359 struct psshfs_node *psn, *psn_parent;
360 struct psshfs_dir *pd;
361
362 psn = node->pn_data;
363 psn_parent = psn->parent->pn_data;
364 psn_parent->childcount--;
365 pd = lookup(psn_parent->dir, psn_parent->dentnext, entryname);
366 assert(pd != NULL);
367 pd->valid = 0;
368 free(pd->entryname);
369 pd->entryname = NULL;
370
371 if (node->pn_va.va_type == VDIR) {
372 psn->parent->pn_va.va_nlink--;
373 if (destroy)
374 freedircache(psn->dir, psn->dentnext);
375 }
376
377 if (destroy)
378 puffs_pn_put(node);
379 }
380