fs.c revision 1.13 1 1.13 pooka /* $NetBSD: fs.c,v 1.13 2007/07/01 18:40:16 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 *
15 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 1.1 pooka * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 1.1 pooka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 1.1 pooka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 1.1 pooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 1.1 pooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 1.1 pooka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.1 pooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 1.1 pooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 1.1 pooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 1.1 pooka * SUCH DAMAGE.
26 1.1 pooka */
27 1.1 pooka
28 1.1 pooka #include <sys/cdefs.h>
29 1.1 pooka #ifndef lint
30 1.13 pooka __RCSID("$NetBSD: fs.c,v 1.13 2007/07/01 18:40:16 pooka Exp $");
31 1.1 pooka #endif /* !lint */
32 1.1 pooka
33 1.1 pooka #include <err.h>
34 1.5 pooka #include <errno.h>
35 1.1 pooka #include <puffs.h>
36 1.1 pooka #include <signal.h>
37 1.1 pooka #include <stdio.h>
38 1.1 pooka #include <stdlib.h>
39 1.1 pooka #include <unistd.h>
40 1.1 pooka
41 1.1 pooka #include "psshfs.h"
42 1.1 pooka #include "sftp_proto.h"
43 1.1 pooka
44 1.8 pooka #define DO_IO(fname, a1, a2, a3, a4, rv) \
45 1.8 pooka puffs_framebuf_seekset(a2, 0); \
46 1.8 pooka *(a4) = 0; \
47 1.8 pooka rv = fname(a1, a2, a3, a4); \
48 1.8 pooka if (rv || a4 == 0) errx(1, "psshfs_domount io failed %d, %d", rv, *a4)
49 1.8 pooka
50 1.1 pooka int
51 1.2 pooka psshfs_domount(struct puffs_usermount *pu)
52 1.1 pooka {
53 1.4 pooka struct psshfs_ctx *pctx = puffs_getspecific(pu);
54 1.1 pooka struct psshfs_node *root = &pctx->psn_root;
55 1.3 pooka struct puffs_pathobj *po_root;
56 1.4 pooka struct puffs_node *pn_root;
57 1.1 pooka struct vattr va;
58 1.1 pooka struct vattr *rva;
59 1.8 pooka struct puffs_framebuf *pb;
60 1.1 pooka char *rootpath;
61 1.1 pooka uint32_t count;
62 1.8 pooka int rv, done;
63 1.1 pooka
64 1.8 pooka pb = psbuf_makeout();
65 1.1 pooka psbuf_put_1(pb, SSH_FXP_INIT);
66 1.1 pooka psbuf_put_4(pb, SFTP_PROTOVERSION);
67 1.8 pooka DO_IO(psbuf_write, pu, pb, pctx->sshfd, &done, rv);
68 1.1 pooka
69 1.8 pooka puffs_framebuf_recycle(pb);
70 1.8 pooka DO_IO(psbuf_read, pu, pb, pctx->sshfd, &done, rv);
71 1.8 pooka if (psbuf_get_type(pb) != SSH_FXP_VERSION)
72 1.8 pooka errx(1, "invalid server response: %d", psbuf_get_type(pb));
73 1.8 pooka pctx->protover = psbuf_get_reqid(pb);
74 1.1 pooka /* might contain some other stuff, but we're not interested */
75 1.1 pooka
76 1.1 pooka /* scope out our rootpath */
77 1.8 pooka psbuf_recycleout(pb);
78 1.1 pooka psbuf_put_1(pb, SSH_FXP_REALPATH);
79 1.1 pooka psbuf_put_4(pb, NEXTREQ(pctx));
80 1.1 pooka psbuf_put_str(pb, pctx->mountpath);
81 1.8 pooka DO_IO(psbuf_write, pu, pb, pctx->sshfd, &done, rv);
82 1.8 pooka
83 1.8 pooka puffs_framebuf_recycle(pb);
84 1.8 pooka DO_IO(psbuf_read, pu, pb, pctx->sshfd, &done, rv);
85 1.8 pooka if (psbuf_get_type(pb) != SSH_FXP_NAME)
86 1.1 pooka errx(1, "invalid server realpath response for \"%s\"",
87 1.1 pooka pctx->mountpath);
88 1.8 pooka if (psbuf_get_4(pb, &count) == -1)
89 1.1 pooka errx(1, "invalid realpath response: count");
90 1.8 pooka if (psbuf_get_str(pb, &rootpath, NULL) == -1)
91 1.1 pooka errx(1, "invalid realpath response: rootpath");
92 1.1 pooka
93 1.1 pooka /* stat the rootdir so that we know it's a dir */
94 1.8 pooka psbuf_recycleout(pb);
95 1.1 pooka psbuf_req_str(pb, SSH_FXP_LSTAT, NEXTREQ(pctx), rootpath);
96 1.8 pooka DO_IO(psbuf_write, pu, pb, pctx->sshfd, &done, rv);
97 1.8 pooka
98 1.8 pooka puffs_framebuf_recycle(pb);
99 1.8 pooka DO_IO(psbuf_read, pu, pb, pctx->sshfd, &done, rv);
100 1.1 pooka
101 1.1 pooka rv = psbuf_expect_attrs(pb, &va);
102 1.1 pooka if (rv)
103 1.1 pooka errx(1, "couldn't stat rootpath");
104 1.8 pooka puffs_framebuf_destroy(pb);
105 1.1 pooka
106 1.1 pooka if (puffs_mode2vt(va.va_mode) != VDIR)
107 1.1 pooka errx(1, "remote path (%s) not a directory", rootpath);
108 1.1 pooka
109 1.1 pooka pctx->nextino = 2;
110 1.1 pooka
111 1.1 pooka memset(root, 0, sizeof(struct psshfs_node));
112 1.4 pooka pn_root = puffs_pn_new(pu, root);
113 1.4 pooka if (pn_root == NULL)
114 1.4 pooka return errno;
115 1.4 pooka puffs_setroot(pu, pn_root);
116 1.3 pooka
117 1.3 pooka po_root = puffs_getrootpathobj(pu);
118 1.3 pooka if (po_root == NULL)
119 1.3 pooka err(1, "getrootpathobj");
120 1.3 pooka po_root->po_path = rootpath;
121 1.3 pooka po_root->po_len = strlen(rootpath);
122 1.1 pooka
123 1.4 pooka rva = &pn_root->pn_va;
124 1.1 pooka puffs_setvattr(rva, &va);
125 1.1 pooka rva->va_fileid = pctx->nextino++;
126 1.1 pooka rva->va_nlink = 0156; /* XXX */
127 1.1 pooka
128 1.1 pooka return 0;
129 1.1 pooka }
130 1.1 pooka
131 1.1 pooka int
132 1.12 pooka psshfs_fs_unmount(struct puffs_cc *pcc, int flags, const struct puffs_cid *pcid)
133 1.1 pooka {
134 1.1 pooka struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
135 1.4 pooka struct psshfs_ctx *pctx = puffs_getspecific(pu);
136 1.1 pooka
137 1.1 pooka kill(pctx->sshpid, SIGTERM);
138 1.1 pooka close(pctx->sshfd);
139 1.1 pooka return 0;
140 1.1 pooka }
141 1.5 pooka
142 1.5 pooka int
143 1.5 pooka psshfs_fs_nodetofh(struct puffs_cc *pcc, void *cookie,
144 1.5 pooka void *fid, size_t *fidsize)
145 1.5 pooka {
146 1.5 pooka struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
147 1.5 pooka struct psshfs_ctx *pctx = puffs_getspecific(pu);
148 1.5 pooka struct puffs_node *pn = cookie;
149 1.5 pooka struct psshfs_node *psn = pn->pn_data;
150 1.5 pooka struct psshfs_fid *pf = fid;
151 1.5 pooka
152 1.5 pooka pf->mounttime = pctx->mounttime;
153 1.5 pooka pf->node = pn;
154 1.5 pooka
155 1.10 pooka psn->stat |= PSN_HASFH;
156 1.5 pooka
157 1.5 pooka return 0;
158 1.5 pooka }
159 1.5 pooka
160 1.5 pooka int
161 1.5 pooka psshfs_fs_fhtonode(struct puffs_cc *pcc, void *fid, size_t fidsize,
162 1.13 pooka struct puffs_newinfo *pni)
163 1.5 pooka {
164 1.5 pooka struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
165 1.5 pooka struct psshfs_ctx *pctx = puffs_getspecific(pu);
166 1.5 pooka struct psshfs_fid *pf = fid;
167 1.5 pooka struct puffs_node *pn = pf->node;
168 1.5 pooka struct psshfs_node *psn;
169 1.5 pooka int rv;
170 1.5 pooka
171 1.5 pooka if (pf->mounttime != pctx->mounttime)
172 1.5 pooka return EINVAL;
173 1.5 pooka if (pn == 0)
174 1.5 pooka return EINVAL;
175 1.5 pooka psn = pn->pn_data;
176 1.10 pooka if ((psn->stat & PSN_HASFH) == 0)
177 1.5 pooka return EINVAL;
178 1.5 pooka
179 1.5 pooka /* update node attributes */
180 1.5 pooka rv = getnodeattr(pcc, pn);
181 1.6 pooka if (rv)
182 1.5 pooka return EINVAL;
183 1.5 pooka
184 1.13 pooka puffs_newinfo_setcookie(pni, pn);
185 1.13 pooka puffs_newinfo_setvtype(pni, pn->pn_va.va_type);
186 1.13 pooka puffs_newinfo_setsize(pni, pn->pn_va.va_size);
187 1.5 pooka
188 1.5 pooka return 0;
189 1.5 pooka }
190