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