Blame


1 fb1a36c0 2022-01-09 op /*
2 dc8c1791 2022-01-10 op * Copyright (c) 2021, 2022 Omar Polo <op@omarpolo.com>
3 fb1a36c0 2022-01-09 op *
4 fb1a36c0 2022-01-09 op * Permission to use, copy, modify, and distribute this software for any
5 fb1a36c0 2022-01-09 op * purpose with or without fee is hereby granted, provided that the above
6 fb1a36c0 2022-01-09 op * copyright notice and this permission notice appear in all copies.
7 fb1a36c0 2022-01-09 op *
8 fb1a36c0 2022-01-09 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 fb1a36c0 2022-01-09 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 fb1a36c0 2022-01-09 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 fb1a36c0 2022-01-09 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 fb1a36c0 2022-01-09 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 fb1a36c0 2022-01-09 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 fb1a36c0 2022-01-09 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 fb1a36c0 2022-01-09 op */
16 fb1a36c0 2022-01-09 op
17 bbcba3ed 2022-01-10 op #include "compat.h"
18 bbcba3ed 2022-01-10 op
19 fb1a36c0 2022-01-09 op #include <sys/ioctl.h>
20 fb1a36c0 2022-01-09 op #include <sys/types.h>
21 fb1a36c0 2022-01-09 op #include <sys/socket.h>
22 01fb39ab 2022-01-16 op #include <sys/stat.h>
23 9e7dd230 2022-01-12 op #include <sys/wait.h>
24 fb1a36c0 2022-01-09 op
25 fb1a36c0 2022-01-09 op #include <assert.h>
26 fb1a36c0 2022-01-09 op #include <errno.h>
27 fb1a36c0 2022-01-09 op #include <fcntl.h>
28 fb1a36c0 2022-01-09 op #include <inttypes.h>
29 fb1a36c0 2022-01-09 op #include <netdb.h>
30 9e7dd230 2022-01-12 op #include <libgen.h>
31 fb1a36c0 2022-01-09 op #include <limits.h>
32 fb1a36c0 2022-01-09 op #include <signal.h>
33 fb1a36c0 2022-01-09 op #include <stdio.h>
34 fb1a36c0 2022-01-09 op #include <stdlib.h>
35 fb1a36c0 2022-01-09 op #include <string.h>
36 fb1a36c0 2022-01-09 op #include <syslog.h>
37 fb1a36c0 2022-01-09 op #include <tls.h>
38 fb1a36c0 2022-01-09 op #include <unistd.h>
39 fb1a36c0 2022-01-09 op
40 91cfa92a 2022-01-10 op #ifdef HAVE_READLINE
41 fb1a36c0 2022-01-09 op #include <readline/readline.h>
42 fb1a36c0 2022-01-09 op #include <readline/history.h>
43 91cfa92a 2022-01-10 op #endif
44 fb1a36c0 2022-01-09 op
45 fb1a36c0 2022-01-09 op #ifndef nitems
46 fb1a36c0 2022-01-09 op #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
47 fb1a36c0 2022-01-09 op #endif
48 fb1a36c0 2022-01-09 op
49 fb1a36c0 2022-01-09 op #include "kami.h"
50 fb1a36c0 2022-01-09 op #include "utils.h"
51 fb1a36c0 2022-01-09 op #include "log.h"
52 c37e1cfe 2022-01-17 op #include "9pclib.h"
53 fb1a36c0 2022-01-09 op
54 64d8db89 2022-01-16 op #define TMPFSTR "/tmp/kamiftp.XXXXXXXXXX"
55 64d8db89 2022-01-16 op #define TMPFSTRLEN sizeof(TMPFSTR)
56 64d8db89 2022-01-16 op
57 fb1a36c0 2022-01-09 op /* flags */
58 fb1a36c0 2022-01-09 op int tls;
59 fb1a36c0 2022-01-09 op const char *crtpath;
60 fb1a36c0 2022-01-09 op const char *keypath;
61 e22049cc 2022-08-28 op
62 fb1a36c0 2022-01-09 op /* state */
63 8f7253b2 2022-11-22 op FILE *fp;
64 fb1a36c0 2022-01-09 op struct evbuffer *buf;
65 fb1a36c0 2022-01-09 op struct evbuffer *dirbuf;
66 fb1a36c0 2022-01-09 op uint32_t msize;
67 fb1a36c0 2022-01-09 op int bell;
68 fb1a36c0 2022-01-09 op
69 fb1a36c0 2022-01-09 op volatile sig_atomic_t resized;
70 fb1a36c0 2022-01-09 op int tty_p;
71 fb1a36c0 2022-01-09 op int tty_width;
72 5565d021 2022-01-16 op int xdump;
73 fb1a36c0 2022-01-09 op
74 fb1a36c0 2022-01-09 op struct progress {
75 fb1a36c0 2022-01-09 op uint64_t max;
76 fb1a36c0 2022-01-09 op uint64_t done;
77 fb1a36c0 2022-01-09 op };
78 fb1a36c0 2022-01-09 op
79 fb1a36c0 2022-01-09 op int pwdfid;
80 fb1a36c0 2022-01-09 op
81 fb1a36c0 2022-01-09 op #define ASSERT_EMPTYBUF() assert(EVBUFFER_LENGTH(buf) == 0)
82 fb1a36c0 2022-01-09 op
83 91cfa92a 2022-01-10 op #if !HAVE_READLINE
84 91cfa92a 2022-01-10 op char *
85 91cfa92a 2022-01-10 op readline(const char *prompt)
86 91cfa92a 2022-01-10 op {
87 91cfa92a 2022-01-10 op char *ch, *line = NULL;
88 91cfa92a 2022-01-10 op size_t linesize = 0;
89 91cfa92a 2022-01-10 op ssize_t linelen;
90 91cfa92a 2022-01-10 op
91 91cfa92a 2022-01-10 op printf("%s", prompt);
92 91cfa92a 2022-01-10 op fflush(stdout);
93 91cfa92a 2022-01-10 op
94 91cfa92a 2022-01-10 op linelen = getline(&line, &linesize, stdin);
95 91cfa92a 2022-01-10 op if (linelen == -1)
96 91cfa92a 2022-01-10 op return NULL;
97 91cfa92a 2022-01-10 op
98 91cfa92a 2022-01-10 op if ((ch = strchr(line, '\n')) != NULL)
99 91cfa92a 2022-01-10 op *ch = '\0';
100 91cfa92a 2022-01-10 op return line;
101 91cfa92a 2022-01-10 op }
102 91cfa92a 2022-01-10 op
103 91cfa92a 2022-01-10 op void
104 91cfa92a 2022-01-10 op add_history(const char *line)
105 91cfa92a 2022-01-10 op {
106 91cfa92a 2022-01-10 op return;
107 91cfa92a 2022-01-10 op }
108 91cfa92a 2022-01-10 op #endif
109 91cfa92a 2022-01-10 op
110 fb1a36c0 2022-01-09 op static char *
111 fb1a36c0 2022-01-09 op read_line(const char *prompt)
112 fb1a36c0 2022-01-09 op {
113 fb1a36c0 2022-01-09 op char *line;
114 fb1a36c0 2022-01-09 op
115 fb1a36c0 2022-01-09 op again:
116 fb1a36c0 2022-01-09 op if ((line = readline(prompt)) == NULL)
117 fb1a36c0 2022-01-09 op return NULL;
118 fb1a36c0 2022-01-09 op /* XXX: trim spaces? */
119 fb1a36c0 2022-01-09 op if (*line == '\0') {
120 fb1a36c0 2022-01-09 op free(line);
121 fb1a36c0 2022-01-09 op goto again;
122 fb1a36c0 2022-01-09 op }
123 fb1a36c0 2022-01-09 op
124 fb1a36c0 2022-01-09 op add_history(line);
125 fb1a36c0 2022-01-09 op return line;
126 9e7dd230 2022-01-12 op }
127 9e7dd230 2022-01-12 op
128 9e7dd230 2022-01-12 op static void
129 9e7dd230 2022-01-12 op spawn(const char *argv0, ...)
130 9e7dd230 2022-01-12 op {
131 9e7dd230 2022-01-12 op pid_t pid;
132 9e7dd230 2022-01-12 op size_t i;
133 9e7dd230 2022-01-12 op int status;
134 9e7dd230 2022-01-12 op const char *argv[16], *last;
135 9e7dd230 2022-01-12 op va_list ap;
136 9e7dd230 2022-01-12 op
137 9e7dd230 2022-01-12 op memset(argv, 0, sizeof(argv));
138 9e7dd230 2022-01-12 op
139 9e7dd230 2022-01-12 op va_start(ap, argv0);
140 9e7dd230 2022-01-12 op argv[0] = argv0;
141 9e7dd230 2022-01-12 op for (i = 1; i < nitems(argv); ++i) {
142 9e7dd230 2022-01-12 op last = va_arg(ap, const char *);
143 9e7dd230 2022-01-12 op if (last == NULL)
144 9e7dd230 2022-01-12 op break;
145 9e7dd230 2022-01-12 op argv[i] = last;
146 9e7dd230 2022-01-12 op }
147 9e7dd230 2022-01-12 op va_end(ap);
148 9e7dd230 2022-01-12 op
149 9e7dd230 2022-01-12 op assert(last == NULL);
150 9e7dd230 2022-01-12 op
151 9e7dd230 2022-01-12 op switch (pid = fork()) {
152 9e7dd230 2022-01-12 op case -1:
153 9e7dd230 2022-01-12 op err(1, "fork");
154 9e7dd230 2022-01-12 op case 0: /* child */
155 9e7dd230 2022-01-12 op execvp(argv[0], (char *const *)argv);
156 9e7dd230 2022-01-12 op err(1, "execvp");
157 9e7dd230 2022-01-12 op default:
158 9e7dd230 2022-01-12 op waitpid(pid, &status, 0);
159 9e7dd230 2022-01-12 op }
160 fb1a36c0 2022-01-09 op }
161 fb1a36c0 2022-01-09 op
162 8f7253b2 2022-11-22 op static int
163 8f7253b2 2022-11-22 op stdio_tls_write(void *arg, const char *buf, int len)
164 8f7253b2 2022-11-22 op {
165 8f7253b2 2022-11-22 op struct tls *ctx = arg;
166 8f7253b2 2022-11-22 op ssize_t ret;
167 8f7253b2 2022-11-22 op
168 8f7253b2 2022-11-22 op do {
169 8f7253b2 2022-11-22 op ret = tls_write(ctx, buf, len);
170 8f7253b2 2022-11-22 op } while (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT);
171 8f7253b2 2022-11-22 op
172 8f7253b2 2022-11-22 op if (ret == -1)
173 8f7253b2 2022-11-22 op warn("tls_write: %s", tls_error(ctx));
174 8f7253b2 2022-11-22 op
175 8f7253b2 2022-11-22 op return ret;
176 8f7253b2 2022-11-22 op }
177 8f7253b2 2022-11-22 op
178 8f7253b2 2022-11-22 op static int
179 8f7253b2 2022-11-22 op stdio_tls_read(void *arg, char *buf, int len)
180 8f7253b2 2022-11-22 op {
181 8f7253b2 2022-11-22 op struct tls *ctx = arg;
182 8f7253b2 2022-11-22 op ssize_t ret;
183 8f7253b2 2022-11-22 op
184 8f7253b2 2022-11-22 op do {
185 8f7253b2 2022-11-22 op ret = tls_read(ctx, buf, len);
186 8f7253b2 2022-11-22 op } while (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT);
187 8f7253b2 2022-11-22 op
188 8f7253b2 2022-11-22 op if (ret == -1)
189 8f7253b2 2022-11-22 op warn("tls_read: %s", tls_error(ctx));
190 8f7253b2 2022-11-22 op
191 8f7253b2 2022-11-22 op return ret;
192 8f7253b2 2022-11-22 op }
193 8f7253b2 2022-11-22 op
194 8f7253b2 2022-11-22 op static int
195 8f7253b2 2022-11-22 op stdio_tls_close(void *arg)
196 8f7253b2 2022-11-22 op {
197 8f7253b2 2022-11-22 op struct tls *ctx = arg;
198 8f7253b2 2022-11-22 op int ret;
199 8f7253b2 2022-11-22 op
200 8f7253b2 2022-11-22 op do {
201 8f7253b2 2022-11-22 op ret = tls_close(ctx);
202 8f7253b2 2022-11-22 op } while (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT);
203 8f7253b2 2022-11-22 op
204 8f7253b2 2022-11-22 op return ret;
205 8f7253b2 2022-11-22 op }
206 8f7253b2 2022-11-22 op
207 fb1a36c0 2022-01-09 op static void
208 fb1a36c0 2022-01-09 op tty_resized(int signo)
209 fb1a36c0 2022-01-09 op {
210 fb1a36c0 2022-01-09 op resized = 1;
211 fb1a36c0 2022-01-09 op }
212 fb1a36c0 2022-01-09 op
213 dd647997 2022-11-23 op static __dead void
214 fb1a36c0 2022-01-09 op usage(int ret)
215 fb1a36c0 2022-01-09 op {
216 ab9f44f7 2022-11-23 op fprintf(stderr, "usage: %s [-C cert] [-K key] [-o output] "
217 9f134f93 2022-11-23 op "[9p://][user@]host[:port][/path]\n", getprogname());
218 fb1a36c0 2022-01-09 op fprintf(stderr, "kamid suite version " KAMID_VERSION "\n");
219 fb1a36c0 2022-01-09 op exit(ret);
220 fb1a36c0 2022-01-09 op }
221 fb1a36c0 2022-01-09 op
222 8d8fb849 2022-01-17 op static int
223 8d8fb849 2022-01-17 op nextfid(void)
224 8d8fb849 2022-01-17 op {
225 8d8fb849 2022-01-17 op uint32_t i;
226 8d8fb849 2022-01-17 op
227 8d8fb849 2022-01-17 op for (i = 0; ; ++i) {
228 8d8fb849 2022-01-17 op if (i != pwdfid)
229 8d8fb849 2022-01-17 op return i;
230 8d8fb849 2022-01-17 op }
231 8d8fb849 2022-01-17 op }
232 8d8fb849 2022-01-17 op
233 fb1a36c0 2022-01-09 op static void
234 fb1a36c0 2022-01-09 op do_send(void)
235 fb1a36c0 2022-01-09 op {
236 8f7253b2 2022-11-22 op size_t r;
237 fb1a36c0 2022-01-09 op
238 5565d021 2022-01-16 op if (xdump)
239 d33c93c4 2022-01-17 op hexdump("outgoing message", EVBUFFER_DATA(evb),
240 5565d021 2022-01-16 op EVBUFFER_LENGTH(evb));
241 5565d021 2022-01-16 op
242 fb1a36c0 2022-01-09 op while (EVBUFFER_LENGTH(evb) != 0) {
243 8f7253b2 2022-11-22 op r = fwrite(EVBUFFER_DATA(evb), 1, EVBUFFER_LENGTH(evb), fp);
244 8f7253b2 2022-11-22 op if (r == 0)
245 8f7253b2 2022-11-22 op fatalx("unexpected EOF");
246 fb1a36c0 2022-01-09 op evbuffer_drain(evb, r);
247 fb1a36c0 2022-01-09 op }
248 fb1a36c0 2022-01-09 op }
249 fb1a36c0 2022-01-09 op
250 fb1a36c0 2022-01-09 op static void
251 fb1a36c0 2022-01-09 op mustread(void *d, size_t len)
252 fb1a36c0 2022-01-09 op {
253 8f7253b2 2022-11-22 op size_t r;
254 fb1a36c0 2022-01-09 op
255 8f7253b2 2022-11-22 op r = fread(d, 1, len, fp);
256 8f7253b2 2022-11-22 op if (r != len)
257 8f7253b2 2022-11-22 op errx(1, "unexpected EOF");
258 fb1a36c0 2022-01-09 op }
259 fb1a36c0 2022-01-09 op
260 fb1a36c0 2022-01-09 op static void
261 fb1a36c0 2022-01-09 op recv_msg(void)
262 fb1a36c0 2022-01-09 op {
263 8f7253b2 2022-11-22 op size_t r;
264 fb1a36c0 2022-01-09 op uint32_t len, l;
265 fb1a36c0 2022-01-09 op char tmp[BUFSIZ];
266 fb1a36c0 2022-01-09 op
267 8f7253b2 2022-11-22 op r = fread(&len, 1, sizeof(len), fp);
268 8f7253b2 2022-11-22 op if (r != sizeof(len))
269 8f7253b2 2022-11-22 op errx(1, "unexpected EOF");
270 8f7253b2 2022-11-22 op
271 fb1a36c0 2022-01-09 op len = le32toh(len);
272 fb1a36c0 2022-01-09 op if (len < HEADERSIZE)
273 fb1a36c0 2022-01-09 op errx(1, "read message of invalid length %d", len);
274 fb1a36c0 2022-01-09 op
275 fb1a36c0 2022-01-09 op len -= 4; /* skip the length just read */
276 fb1a36c0 2022-01-09 op
277 fb1a36c0 2022-01-09 op while (len != 0) {
278 fb1a36c0 2022-01-09 op l = MIN(len, sizeof(tmp));
279 8f7253b2 2022-11-22 op
280 8f7253b2 2022-11-22 op r = fread(tmp, 1, l, fp);
281 8f7253b2 2022-11-22 op if (r != l)
282 8f7253b2 2022-11-22 op errx(1, "unexpected EOF");
283 8f7253b2 2022-11-22 op len -= r;
284 8f7253b2 2022-11-22 op evbuffer_add(buf, tmp, r);
285 fb1a36c0 2022-01-09 op }
286 5565d021 2022-01-16 op
287 5565d021 2022-01-16 op if (xdump)
288 5565d021 2022-01-16 op hexdump("incoming packet", EVBUFFER_DATA(buf),
289 5565d021 2022-01-16 op EVBUFFER_LENGTH(buf));
290 fb1a36c0 2022-01-09 op }
291 fb1a36c0 2022-01-09 op
292 fb1a36c0 2022-01-09 op static uint64_t
293 fb1a36c0 2022-01-09 op np_read64(struct evbuffer *buf)
294 fb1a36c0 2022-01-09 op {
295 fb1a36c0 2022-01-09 op uint64_t n;
296 fb1a36c0 2022-01-09 op
297 fb1a36c0 2022-01-09 op evbuffer_remove(buf, &n, sizeof(n));
298 fb1a36c0 2022-01-09 op return le64toh(n);
299 fb1a36c0 2022-01-09 op }
300 fb1a36c0 2022-01-09 op
301 fb1a36c0 2022-01-09 op static uint32_t
302 fb1a36c0 2022-01-09 op np_read32(struct evbuffer *buf)
303 fb1a36c0 2022-01-09 op {
304 fb1a36c0 2022-01-09 op uint32_t n;
305 fb1a36c0 2022-01-09 op
306 fb1a36c0 2022-01-09 op evbuffer_remove(buf, &n, sizeof(n));
307 fb1a36c0 2022-01-09 op return le32toh(n);
308 fb1a36c0 2022-01-09 op }
309 fb1a36c0 2022-01-09 op
310 fb1a36c0 2022-01-09 op static uint16_t
311 fb1a36c0 2022-01-09 op np_read16(struct evbuffer *buf)
312 fb1a36c0 2022-01-09 op {
313 fb1a36c0 2022-01-09 op uint16_t n;
314 fb1a36c0 2022-01-09 op
315 fb1a36c0 2022-01-09 op evbuffer_remove(buf, &n, sizeof(n));
316 fb1a36c0 2022-01-09 op return le16toh(n);
317 fb1a36c0 2022-01-09 op }
318 fb1a36c0 2022-01-09 op
319 fb1a36c0 2022-01-09 op static uint16_t
320 fb1a36c0 2022-01-09 op np_read8(struct evbuffer *buf)
321 fb1a36c0 2022-01-09 op {
322 fb1a36c0 2022-01-09 op uint8_t n;
323 fb1a36c0 2022-01-09 op
324 fb1a36c0 2022-01-09 op evbuffer_remove(buf, &n, sizeof(n));
325 fb1a36c0 2022-01-09 op return n;
326 fb1a36c0 2022-01-09 op }
327 fb1a36c0 2022-01-09 op
328 fb1a36c0 2022-01-09 op static char *
329 fb1a36c0 2022-01-09 op np_readstr(struct evbuffer *buf)
330 fb1a36c0 2022-01-09 op {
331 fb1a36c0 2022-01-09 op uint16_t len;
332 fb1a36c0 2022-01-09 op char *str;
333 fb1a36c0 2022-01-09 op
334 fb1a36c0 2022-01-09 op len = np_read16(buf);
335 fb1a36c0 2022-01-09 op assert(EVBUFFER_LENGTH(buf) >= len);
336 fb1a36c0 2022-01-09 op
337 fb1a36c0 2022-01-09 op if ((str = calloc(1, len+1)) == NULL)
338 fb1a36c0 2022-01-09 op err(1, "calloc");
339 fb1a36c0 2022-01-09 op evbuffer_remove(buf, str, len);
340 fb1a36c0 2022-01-09 op return str;
341 fb1a36c0 2022-01-09 op }
342 fb1a36c0 2022-01-09 op
343 fb1a36c0 2022-01-09 op static void
344 fb1a36c0 2022-01-09 op np_read_qid(struct evbuffer *buf, struct qid *qid)
345 fb1a36c0 2022-01-09 op {
346 fb1a36c0 2022-01-09 op assert(EVBUFFER_LENGTH(buf) >= QIDSIZE);
347 fb1a36c0 2022-01-09 op
348 fb1a36c0 2022-01-09 op qid->type = np_read8(buf);
349 fb1a36c0 2022-01-09 op qid->vers = np_read32(buf);
350 fb1a36c0 2022-01-09 op qid->path = np_read64(buf);
351 fb1a36c0 2022-01-09 op }
352 fb1a36c0 2022-01-09 op
353 fb1a36c0 2022-01-09 op static int
354 fb1a36c0 2022-01-09 op np_read_stat(struct evbuffer *buf, struct np_stat *st)
355 fb1a36c0 2022-01-09 op {
356 fb1a36c0 2022-01-09 op uint16_t size;
357 fb1a36c0 2022-01-09 op
358 fb1a36c0 2022-01-09 op memset(st, 0, sizeof(*st));
359 fb1a36c0 2022-01-09 op
360 fb1a36c0 2022-01-09 op size = np_read16(buf);
361 fb1a36c0 2022-01-09 op if (size > EVBUFFER_LENGTH(buf))
362 fb1a36c0 2022-01-09 op return -1;
363 fb1a36c0 2022-01-09 op
364 fb1a36c0 2022-01-09 op st->type = np_read16(buf);
365 fb1a36c0 2022-01-09 op st->dev = np_read32(buf);
366 fb1a36c0 2022-01-09 op np_read_qid(buf, &st->qid);
367 fb1a36c0 2022-01-09 op st->mode = np_read32(buf);
368 fb1a36c0 2022-01-09 op st->atime = np_read32(buf);
369 fb1a36c0 2022-01-09 op st->mtime = np_read32(buf);
370 fb1a36c0 2022-01-09 op st->length = np_read64(buf);
371 fb1a36c0 2022-01-09 op st->name = np_readstr(buf);
372 fb1a36c0 2022-01-09 op st->uid = np_readstr(buf);
373 fb1a36c0 2022-01-09 op st->gid = np_readstr(buf);
374 fb1a36c0 2022-01-09 op st->muid = np_readstr(buf);
375 fb1a36c0 2022-01-09 op
376 fb1a36c0 2022-01-09 op return 0;
377 fb1a36c0 2022-01-09 op }
378 fb1a36c0 2022-01-09 op
379 fb1a36c0 2022-01-09 op static void
380 fb1a36c0 2022-01-09 op expect(uint8_t type)
381 fb1a36c0 2022-01-09 op {
382 fb1a36c0 2022-01-09 op uint8_t t;
383 fb1a36c0 2022-01-09 op
384 fb1a36c0 2022-01-09 op t = np_read8(buf);
385 fb1a36c0 2022-01-09 op if (t == type)
386 fb1a36c0 2022-01-09 op return;
387 fb1a36c0 2022-01-09 op
388 fb1a36c0 2022-01-09 op if (t == Rerror) {
389 fb1a36c0 2022-01-09 op char *err;
390 fb1a36c0 2022-01-09 op
391 fb1a36c0 2022-01-09 op /* skip tag */
392 fb1a36c0 2022-01-09 op np_read16(buf);
393 fb1a36c0 2022-01-09 op
394 fb1a36c0 2022-01-09 op err = np_readstr(buf);
395 fb1a36c0 2022-01-09 op errx(1, "expected %s, got error %s",
396 fb1a36c0 2022-01-09 op pp_msg_type(type), err);
397 fb1a36c0 2022-01-09 op }
398 fb1a36c0 2022-01-09 op
399 fb1a36c0 2022-01-09 op errx(1, "expected %s, got msg type %s",
400 fb1a36c0 2022-01-09 op pp_msg_type(type), pp_msg_type(t));
401 fb1a36c0 2022-01-09 op }
402 fb1a36c0 2022-01-09 op
403 fb1a36c0 2022-01-09 op static void
404 fb1a36c0 2022-01-09 op expect2(uint8_t type, uint16_t tag)
405 fb1a36c0 2022-01-09 op {
406 fb1a36c0 2022-01-09 op uint16_t t;
407 fb1a36c0 2022-01-09 op
408 fb1a36c0 2022-01-09 op expect(type);
409 fb1a36c0 2022-01-09 op
410 fb1a36c0 2022-01-09 op t = np_read16(buf);
411 fb1a36c0 2022-01-09 op if (t == tag)
412 fb1a36c0 2022-01-09 op return;
413 fb1a36c0 2022-01-09 op
414 fb1a36c0 2022-01-09 op errx(1, "expected tag 0x%x, got 0x%x", tag, t);
415 fb1a36c0 2022-01-09 op }
416 fb1a36c0 2022-01-09 op
417 8f9f99f7 2022-01-16 op static char *
418 8f9f99f7 2022-01-16 op check(uint8_t type, uint16_t tag)
419 8f9f99f7 2022-01-16 op {
420 8f9f99f7 2022-01-16 op uint16_t rtag;
421 8f9f99f7 2022-01-16 op uint8_t rtype;
422 8f9f99f7 2022-01-16 op
423 8f9f99f7 2022-01-16 op rtype = np_read8(buf);
424 8f9f99f7 2022-01-16 op rtag = np_read16(buf);
425 8f9f99f7 2022-01-16 op if (rtype == type) {
426 8f9f99f7 2022-01-16 op if (rtag != tag)
427 8f9f99f7 2022-01-16 op errx(1, "expected tag 0x%x, got 0x%x", tag, rtag);
428 8f9f99f7 2022-01-16 op return NULL;
429 8f9f99f7 2022-01-16 op }
430 8f9f99f7 2022-01-16 op
431 8f9f99f7 2022-01-16 op if (rtype == Rerror)
432 8f9f99f7 2022-01-16 op return np_readstr(buf);
433 8f9f99f7 2022-01-16 op
434 8f9f99f7 2022-01-16 op errx(1, "expected %s, got msg type %s",
435 8f9f99f7 2022-01-16 op pp_msg_type(type), pp_msg_type(rtype));
436 8f9f99f7 2022-01-16 op }
437 8f9f99f7 2022-01-16 op
438 fb1a36c0 2022-01-09 op static void
439 fb1a36c0 2022-01-09 op do_version(void)
440 fb1a36c0 2022-01-09 op {
441 fb1a36c0 2022-01-09 op char *version;
442 fb1a36c0 2022-01-09 op
443 fb1a36c0 2022-01-09 op tversion(VERSION9P, MSIZE9P);
444 fb1a36c0 2022-01-09 op do_send();
445 fb1a36c0 2022-01-09 op recv_msg();
446 fb1a36c0 2022-01-09 op expect2(Rversion, NOTAG);
447 fb1a36c0 2022-01-09 op
448 fb1a36c0 2022-01-09 op msize = np_read32(buf);
449 fb1a36c0 2022-01-09 op version = np_readstr(buf);
450 fb1a36c0 2022-01-09 op
451 315eafe8 2022-05-23 op if (msize > MSIZE9P || msize < 256)
452 fb1a36c0 2022-01-09 op errx(1, "got unexpected msize: %d", msize);
453 fb1a36c0 2022-01-09 op if (strcmp(version, VERSION9P))
454 fb1a36c0 2022-01-09 op errx(1, "unexpected 9p version: %s", version);
455 fb1a36c0 2022-01-09 op
456 fb1a36c0 2022-01-09 op free(version);
457 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
458 fb1a36c0 2022-01-09 op }
459 fb1a36c0 2022-01-09 op
460 fb1a36c0 2022-01-09 op static void
461 14ae3944 2022-11-23 op do_attach(const char *user)
462 fb1a36c0 2022-01-09 op {
463 fb1a36c0 2022-01-09 op struct qid qid;
464 fb1a36c0 2022-01-09 op
465 2c50e0f6 2022-11-23 op tattach(pwdfid, NOFID, user, "/");
466 fb1a36c0 2022-01-09 op do_send();
467 fb1a36c0 2022-01-09 op recv_msg();
468 fb1a36c0 2022-01-09 op expect2(Rattach, iota_tag);
469 fb1a36c0 2022-01-09 op np_read_qid(buf, &qid);
470 fb1a36c0 2022-01-09 op
471 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
472 fb1a36c0 2022-01-09 op }
473 fb1a36c0 2022-01-09 op
474 fb1a36c0 2022-01-09 op static uint32_t
475 fb1a36c0 2022-01-09 op do_open(uint32_t fid, uint8_t mode)
476 fb1a36c0 2022-01-09 op {
477 fb1a36c0 2022-01-09 op struct qid qid;
478 fb1a36c0 2022-01-09 op uint32_t iounit;
479 fb1a36c0 2022-01-09 op
480 fb1a36c0 2022-01-09 op topen(fid, mode);
481 fb1a36c0 2022-01-09 op do_send();
482 fb1a36c0 2022-01-09 op recv_msg();
483 fb1a36c0 2022-01-09 op expect2(Ropen, iota_tag);
484 01fb39ab 2022-01-16 op
485 01fb39ab 2022-01-16 op np_read_qid(buf, &qid);
486 01fb39ab 2022-01-16 op iounit = np_read32(buf);
487 01fb39ab 2022-01-16 op
488 01fb39ab 2022-01-16 op ASSERT_EMPTYBUF();
489 01fb39ab 2022-01-16 op
490 01fb39ab 2022-01-16 op return iounit;
491 01fb39ab 2022-01-16 op }
492 01fb39ab 2022-01-16 op
493 01fb39ab 2022-01-16 op static uint32_t
494 01fb39ab 2022-01-16 op do_create(uint32_t fid, const char *name, uint32_t perm, uint8_t mode)
495 01fb39ab 2022-01-16 op {
496 01fb39ab 2022-01-16 op struct qid qid;
497 01fb39ab 2022-01-16 op uint32_t iounit;
498 fb1a36c0 2022-01-09 op
499 01fb39ab 2022-01-16 op tcreate(fid, name, perm, mode);
500 01fb39ab 2022-01-16 op do_send();
501 01fb39ab 2022-01-16 op recv_msg();
502 01fb39ab 2022-01-16 op expect2(Rcreate, iota_tag);
503 01fb39ab 2022-01-16 op
504 fb1a36c0 2022-01-09 op np_read_qid(buf, &qid);
505 fb1a36c0 2022-01-09 op iounit = np_read32(buf);
506 fb1a36c0 2022-01-09 op
507 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
508 fb1a36c0 2022-01-09 op
509 fb1a36c0 2022-01-09 op return iounit;
510 fb1a36c0 2022-01-09 op }
511 fb1a36c0 2022-01-09 op
512 fb1a36c0 2022-01-09 op static void
513 fb1a36c0 2022-01-09 op do_clunk(uint32_t fid)
514 fb1a36c0 2022-01-09 op {
515 fb1a36c0 2022-01-09 op tclunk(fid);
516 fb1a36c0 2022-01-09 op do_send();
517 fb1a36c0 2022-01-09 op recv_msg();
518 fb1a36c0 2022-01-09 op expect2(Rclunk, iota_tag);
519 fb1a36c0 2022-01-09 op
520 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
521 fb1a36c0 2022-01-09 op }
522 fb1a36c0 2022-01-09 op
523 1bb1bfca 2022-01-16 op static char *
524 fb1a36c0 2022-01-09 op dup_fid(int fid, int nfid)
525 fb1a36c0 2022-01-09 op {
526 fb1a36c0 2022-01-09 op uint16_t nwqid;
527 1bb1bfca 2022-01-16 op char *errstr;
528 fb1a36c0 2022-01-09 op
529 fb1a36c0 2022-01-09 op twalk(fid, nfid, NULL, 0);
530 fb1a36c0 2022-01-09 op do_send();
531 fb1a36c0 2022-01-09 op recv_msg();
532 fb1a36c0 2022-01-09 op
533 1bb1bfca 2022-01-16 op if ((errstr = check(Rwalk, iota_tag)) != NULL)
534 1bb1bfca 2022-01-16 op return errstr;
535 1bb1bfca 2022-01-16 op
536 fb1a36c0 2022-01-09 op nwqid = np_read16(buf);
537 fb1a36c0 2022-01-09 op assert(nwqid == 0);
538 fb1a36c0 2022-01-09 op
539 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
540 1bb1bfca 2022-01-16 op
541 1bb1bfca 2022-01-16 op return NULL;
542 fb1a36c0 2022-01-09 op }
543 fb1a36c0 2022-01-09 op
544 8f9f99f7 2022-01-16 op static char *
545 8f9f99f7 2022-01-16 op walk_path(int fid, int newfid, const char *path, int *missing,
546 8f9f99f7 2022-01-16 op struct qid *qid)
547 fb1a36c0 2022-01-09 op {
548 8f9f99f7 2022-01-16 op char *wnames[MAXWELEM], *p, *t, *errstr;
549 fb1a36c0 2022-01-09 op size_t nwname, i;
550 fb1a36c0 2022-01-09 op uint16_t nwqid;
551 fb1a36c0 2022-01-09 op
552 fb1a36c0 2022-01-09 op if ((p = strdup(path)) == NULL)
553 fb1a36c0 2022-01-09 op err(1, "strdup");
554 fb1a36c0 2022-01-09 op t = p;
555 fb1a36c0 2022-01-09 op
556 fb1a36c0 2022-01-09 op /* strip initial ./ */
557 fb1a36c0 2022-01-09 op if (t[0] == '.' && t[1] == '/')
558 fb1a36c0 2022-01-09 op t += 2;
559 fb1a36c0 2022-01-09 op
560 fb1a36c0 2022-01-09 op for (nwname = 0; nwname < nitems(wnames) &&
561 fb1a36c0 2022-01-09 op (wnames[nwname] = strsep(&t, "/")) != NULL;) {
562 fb1a36c0 2022-01-09 op if (*wnames[nwname] != '\0')
563 fb1a36c0 2022-01-09 op nwname++;
564 fb1a36c0 2022-01-09 op }
565 fb1a36c0 2022-01-09 op
566 fb1a36c0 2022-01-09 op twalk(fid, newfid, (const char **)wnames, nwname);
567 fb1a36c0 2022-01-09 op do_send();
568 fb1a36c0 2022-01-09 op recv_msg();
569 fb1a36c0 2022-01-09 op
570 8f9f99f7 2022-01-16 op *missing = nwname;
571 5fe03b5c 2022-01-21 op if ((errstr = check(Rwalk, iota_tag)) != NULL) {
572 5fe03b5c 2022-01-21 op free(p);
573 8f9f99f7 2022-01-16 op return errstr;
574 5fe03b5c 2022-01-21 op }
575 8f9f99f7 2022-01-16 op
576 fb1a36c0 2022-01-09 op nwqid = np_read16(buf);
577 fb1a36c0 2022-01-09 op assert(nwqid <= nwname);
578 fb1a36c0 2022-01-09 op
579 fb1a36c0 2022-01-09 op /* consume all qids */
580 43d117b1 2022-01-16 op for (i = 0; i < nwqid; ++i)
581 fb1a36c0 2022-01-09 op np_read_qid(buf, qid);
582 fb1a36c0 2022-01-09 op
583 fb1a36c0 2022-01-09 op free(p);
584 fb1a36c0 2022-01-09 op
585 8f9f99f7 2022-01-16 op *missing = nwname - nwqid;
586 8f9f99f7 2022-01-16 op return NULL;
587 fb1a36c0 2022-01-09 op }
588 fb1a36c0 2022-01-09 op
589 fb1a36c0 2022-01-09 op static void
590 fb1a36c0 2022-01-09 op do_stat(int fid, struct np_stat *st)
591 fb1a36c0 2022-01-09 op {
592 fb1a36c0 2022-01-09 op tstat(fid);
593 fb1a36c0 2022-01-09 op do_send();
594 fb1a36c0 2022-01-09 op recv_msg();
595 fb1a36c0 2022-01-09 op expect2(Rstat, iota_tag);
596 fb1a36c0 2022-01-09 op
597 caa64af1 2022-02-02 op /* eat up the first two byte length */
598 caa64af1 2022-02-02 op np_read16(buf);
599 caa64af1 2022-02-02 op
600 fb1a36c0 2022-01-09 op if (np_read_stat(buf, st) == -1)
601 fb1a36c0 2022-01-09 op errx(1, "invalid stat struct read");
602 fb1a36c0 2022-01-09 op
603 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
604 fb1a36c0 2022-01-09 op }
605 c37e1cfe 2022-01-17 op
606 c37e1cfe 2022-01-17 op static char *
607 c37e1cfe 2022-01-17 op do_wstat(int fid, const struct np_stat *st)
608 c37e1cfe 2022-01-17 op {
609 c37e1cfe 2022-01-17 op char *errstr;
610 c37e1cfe 2022-01-17 op
611 c37e1cfe 2022-01-17 op twstat(fid, st);
612 c37e1cfe 2022-01-17 op do_send();
613 c37e1cfe 2022-01-17 op recv_msg();
614 fb1a36c0 2022-01-09 op
615 c37e1cfe 2022-01-17 op if ((errstr = check(Rwstat, iota_tag)) != NULL)
616 c37e1cfe 2022-01-17 op return errstr;
617 c37e1cfe 2022-01-17 op
618 c37e1cfe 2022-01-17 op ASSERT_EMPTYBUF();
619 c37e1cfe 2022-01-17 op
620 c37e1cfe 2022-01-17 op return NULL;
621 c37e1cfe 2022-01-17 op }
622 c37e1cfe 2022-01-17 op
623 fb1a36c0 2022-01-09 op static size_t
624 fb1a36c0 2022-01-09 op do_read(int fid, uint64_t off, uint32_t count, void *data)
625 fb1a36c0 2022-01-09 op {
626 fb1a36c0 2022-01-09 op uint32_t r;
627 fb1a36c0 2022-01-09 op
628 fb1a36c0 2022-01-09 op tread(fid, off, count);
629 fb1a36c0 2022-01-09 op do_send();
630 fb1a36c0 2022-01-09 op recv_msg();
631 fb1a36c0 2022-01-09 op expect2(Rread, iota_tag);
632 fb1a36c0 2022-01-09 op
633 fb1a36c0 2022-01-09 op r = np_read32(buf);
634 fb1a36c0 2022-01-09 op assert(r == EVBUFFER_LENGTH(buf));
635 fb1a36c0 2022-01-09 op assert(r <= count);
636 fb1a36c0 2022-01-09 op evbuffer_remove(buf, data, r);
637 fb1a36c0 2022-01-09 op
638 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
639 fb1a36c0 2022-01-09 op
640 fb1a36c0 2022-01-09 op return r;
641 fb1a36c0 2022-01-09 op }
642 fb1a36c0 2022-01-09 op
643 01fb39ab 2022-01-16 op static size_t
644 01fb39ab 2022-01-16 op do_write(int fid, uint64_t off, uint32_t count, void *data)
645 01fb39ab 2022-01-16 op {
646 01fb39ab 2022-01-16 op uint32_t r;
647 01fb39ab 2022-01-16 op
648 01fb39ab 2022-01-16 op twrite(fid, off, data, count);
649 01fb39ab 2022-01-16 op do_send();
650 01fb39ab 2022-01-16 op recv_msg();
651 01fb39ab 2022-01-16 op expect2(Rwrite, iota_tag);
652 01fb39ab 2022-01-16 op
653 01fb39ab 2022-01-16 op r = np_read32(buf);
654 01fb39ab 2022-01-16 op assert(r <= count);
655 01fb39ab 2022-01-16 op
656 01fb39ab 2022-01-16 op ASSERT_EMPTYBUF();
657 01fb39ab 2022-01-16 op
658 01fb39ab 2022-01-16 op return r;
659 01fb39ab 2022-01-16 op }
660 01fb39ab 2022-01-16 op
661 fb1a36c0 2022-01-09 op static void
662 fb1a36c0 2022-01-09 op draw_progress(const char *pre, const struct progress *p)
663 fb1a36c0 2022-01-09 op {
664 fb1a36c0 2022-01-09 op struct winsize ws;
665 fb1a36c0 2022-01-09 op int i, l, w;
666 fb1a36c0 2022-01-09 op double perc;
667 5565d021 2022-01-16 op
668 5565d021 2022-01-16 op if (xdump)
669 5565d021 2022-01-16 op return;
670 fb1a36c0 2022-01-09 op
671 fb1a36c0 2022-01-09 op perc = 100.0 * p->done / p->max;
672 fb1a36c0 2022-01-09 op if (!tty_p) {
673 fb1a36c0 2022-01-09 op fprintf(stderr, "%s: %d%%\n", pre, (int)perc);
674 fb1a36c0 2022-01-09 op return;
675 fb1a36c0 2022-01-09 op }
676 fb1a36c0 2022-01-09 op
677 fb1a36c0 2022-01-09 op if (resized) {
678 fb1a36c0 2022-01-09 op resized = 0;
679 fb1a36c0 2022-01-09 op
680 fb1a36c0 2022-01-09 op if (ioctl(0, TIOCGWINSZ, &ws) == -1)
681 fb1a36c0 2022-01-09 op return;
682 fb1a36c0 2022-01-09 op tty_width = ws.ws_col;
683 fb1a36c0 2022-01-09 op }
684 fb1a36c0 2022-01-09 op w = tty_width;
685 fb1a36c0 2022-01-09 op
686 fb1a36c0 2022-01-09 op if (pre == NULL ||
687 5d0dedb3 2022-11-23 op ((l = fprintf(stderr, "\r%s ", pre)) == -1 || l >= w))
688 fb1a36c0 2022-01-09 op return;
689 fb1a36c0 2022-01-09 op
690 fb1a36c0 2022-01-09 op w -= l + 2 + 5; /* 2 for |, 5 for percentage + \n */
691 fb1a36c0 2022-01-09 op if (w < 0) {
692 5d0dedb3 2022-11-23 op fprintf(stderr, "%4d%%\n", (int)perc);
693 fb1a36c0 2022-01-09 op return;
694 fb1a36c0 2022-01-09 op }
695 fb1a36c0 2022-01-09 op
696 5d0dedb3 2022-11-23 op fprintf(stderr, "|");
697 fb1a36c0 2022-01-09 op
698 fb1a36c0 2022-01-09 op l = w * MIN(100.0, perc) / 100.0;
699 fb1a36c0 2022-01-09 op for (i = 0; i < l; i++)
700 5d0dedb3 2022-11-23 op fprintf(stderr, "*");
701 fb1a36c0 2022-01-09 op for (; i < w; i++)
702 5d0dedb3 2022-11-23 op fprintf(stderr, " ");
703 5d0dedb3 2022-11-23 op fprintf(stderr, "|%4d%%", (int)perc);
704 fb1a36c0 2022-01-09 op }
705 fb1a36c0 2022-01-09 op
706 02571aa1 2022-01-29 op static int
707 76521d26 2022-01-16 op fetch_fid(int fid, int fd, const char *name)
708 fb1a36c0 2022-01-09 op {
709 02d5b425 2022-05-23 op static char buf[MSIZE9P];
710 fb1a36c0 2022-01-09 op struct progress p = {0};
711 fb1a36c0 2022-01-09 op struct np_stat st;
712 fb1a36c0 2022-01-09 op size_t r;
713 02571aa1 2022-01-29 op int ret = 0;
714 fb1a36c0 2022-01-09 op
715 fb1a36c0 2022-01-09 op do_stat(fid, &st);
716 fb1a36c0 2022-01-09 op do_open(fid, KOREAD);
717 fb1a36c0 2022-01-09 op
718 fb1a36c0 2022-01-09 op p.max = st.length;
719 fb1a36c0 2022-01-09 op for (;;) {
720 02d5b425 2022-05-23 op size_t len, off;
721 fb1a36c0 2022-01-09 op ssize_t nw;
722 fb1a36c0 2022-01-09 op
723 02d5b425 2022-05-23 op len = MIN(sizeof(buf), msize);
724 7bafb2fd 2022-08-28 op len -= IOHDRSZ; /* for the request' fields */
725 02d5b425 2022-05-23 op
726 02d5b425 2022-05-23 op r = do_read(fid, p.done, len, buf);
727 fb1a36c0 2022-01-09 op if (r == 0)
728 fb1a36c0 2022-01-09 op break;
729 fb1a36c0 2022-01-09 op
730 b2263c45 2022-01-16 op for (off = 0; off < r; off += nw)
731 b2263c45 2022-01-16 op if ((nw = write(fd, buf + off, r - off)) == 0 ||
732 02571aa1 2022-01-29 op nw == -1) {
733 02571aa1 2022-01-29 op ret = -1;
734 02571aa1 2022-01-29 op goto end;
735 02571aa1 2022-01-29 op }
736 fb1a36c0 2022-01-09 op
737 fb1a36c0 2022-01-09 op p.done += r;
738 9e7dd230 2022-01-12 op draw_progress(name, &p);
739 fb1a36c0 2022-01-09 op
740 fb1a36c0 2022-01-09 op #if 0
741 fb1a36c0 2022-01-09 op /* throttle, for debugging purpose */
742 fb1a36c0 2022-01-09 op {
743 fb1a36c0 2022-01-09 op struct timespec ts = { 0, 500000000 };
744 fb1a36c0 2022-01-09 op nanosleep(&ts, NULL);
745 fb1a36c0 2022-01-09 op }
746 fb1a36c0 2022-01-09 op #endif
747 fb1a36c0 2022-01-09 op }
748 fb1a36c0 2022-01-09 op
749 02571aa1 2022-01-29 op end:
750 fb1a36c0 2022-01-09 op putchar('\n');
751 01fb39ab 2022-01-16 op
752 01fb39ab 2022-01-16 op do_clunk(fid);
753 02571aa1 2022-01-29 op return ret;
754 01fb39ab 2022-01-16 op }
755 01fb39ab 2022-01-16 op
756 01fb39ab 2022-01-16 op static void
757 16f1f3bf 2022-01-21 op send_fid(int fid, const char *fnam, int open_flags, int fd, const char *name)
758 01fb39ab 2022-01-16 op {
759 02d5b425 2022-05-23 op static char buf[MSIZE9P];
760 01fb39ab 2022-01-16 op struct progress p = {0};
761 01fb39ab 2022-01-16 op struct stat sb;
762 01fb39ab 2022-01-16 op ssize_t r;
763 02d5b425 2022-05-23 op size_t w, len;
764 01fb39ab 2022-01-16 op
765 01fb39ab 2022-01-16 op if (fstat(fd, &sb) == -1)
766 01fb39ab 2022-01-16 op err(1, "fstat");
767 01fb39ab 2022-01-16 op
768 01fb39ab 2022-01-16 op if (fnam != NULL)
769 01fb39ab 2022-01-16 op do_create(fid, fnam, 0644, KOWRITE);
770 01fb39ab 2022-01-16 op else
771 16f1f3bf 2022-01-21 op do_open(fid, open_flags | KOWRITE);
772 01fb39ab 2022-01-16 op
773 01fb39ab 2022-01-16 op p.max = sb.st_size;
774 01fb39ab 2022-01-16 op for (;;) {
775 02d5b425 2022-05-23 op len = MIN(sizeof(buf), msize);
776 02d5b425 2022-05-23 op len -= HEADERSIZE + 4 + 4 + 8; /* for the request' fields */
777 02d5b425 2022-05-23 op
778 02d5b425 2022-05-23 op r = read(fd, buf, len);
779 01fb39ab 2022-01-16 op if (r == 0)
780 01fb39ab 2022-01-16 op break;
781 01fb39ab 2022-01-16 op if (r == -1)
782 01fb39ab 2022-01-16 op err(1, "read");
783 9e7dd230 2022-01-12 op
784 01fb39ab 2022-01-16 op w = do_write(fid, p.done, r, buf);
785 01fb39ab 2022-01-16 op p.done += w;
786 01fb39ab 2022-01-16 op
787 01fb39ab 2022-01-16 op draw_progress(name, &p);
788 01fb39ab 2022-01-16 op
789 01fb39ab 2022-01-16 op #if 0
790 01fb39ab 2022-01-16 op /* throttle, for debugging purpose */
791 01fb39ab 2022-01-16 op {
792 01fb39ab 2022-01-16 op struct timespec ts = { 0, 500000000 };
793 01fb39ab 2022-01-16 op nanosleep(&ts, NULL);
794 01fb39ab 2022-01-16 op }
795 01fb39ab 2022-01-16 op #endif
796 01fb39ab 2022-01-16 op }
797 01fb39ab 2022-01-16 op
798 01fb39ab 2022-01-16 op putchar('\n');
799 fb1a36c0 2022-01-09 op do_clunk(fid);
800 9c48f915 2022-01-16 op }
801 9c48f915 2022-01-16 op
802 9c48f915 2022-01-16 op static int
803 9c48f915 2022-01-16 op woc_file(int fd, const char *prompt, const char *path)
804 9c48f915 2022-01-16 op {
805 9c48f915 2022-01-16 op struct qid qid;
806 9c48f915 2022-01-16 op const char *n = NULL;
807 9c48f915 2022-01-16 op char *errstr;
808 9c48f915 2022-01-16 op int nfid, miss;
809 9c48f915 2022-01-16 op
810 8d8fb849 2022-01-17 op nfid = nextfid();
811 9c48f915 2022-01-16 op errstr = walk_path(pwdfid, nfid, path, &miss, &qid);
812 9c48f915 2022-01-16 op if (errstr != NULL && miss > 1) {
813 5d0dedb3 2022-11-23 op fprintf(stderr, "%s: %s\n", path, errstr);
814 9c48f915 2022-01-16 op free(errstr);
815 9c48f915 2022-01-16 op return -1;
816 9c48f915 2022-01-16 op }
817 9c48f915 2022-01-16 op
818 9c48f915 2022-01-16 op if (errstr != NULL || miss == 1) {
819 9c48f915 2022-01-16 op char p[PATH_MAX], *dn;
820 9c48f915 2022-01-16 op
821 9c48f915 2022-01-16 op /*
822 9c48f915 2022-01-16 op * If it's only one component missing (the file name), walk
823 9c48f915 2022-01-16 op * to the parent directory and try to create the file.
824 9c48f915 2022-01-16 op */
825 9c48f915 2022-01-16 op
826 9c48f915 2022-01-16 op if (strlcpy(p, path, sizeof(p)) >= sizeof(p)) {
827 5d0dedb3 2022-11-23 op fprintf(stderr, "path too long: %s\n", path);
828 9c48f915 2022-01-16 op return -1;
829 9c48f915 2022-01-16 op }
830 9c48f915 2022-01-16 op dn = dirname(p);
831 9c48f915 2022-01-16 op
832 9c48f915 2022-01-16 op if (!strcmp(dn, ".")) {
833 9c48f915 2022-01-16 op errstr = dup_fid(pwdfid, nfid);
834 9c48f915 2022-01-16 op miss = 0;
835 9c48f915 2022-01-16 op } else
836 9c48f915 2022-01-16 op errstr = walk_path(pwdfid, nfid, dn, &miss, &qid);
837 9c48f915 2022-01-16 op
838 9c48f915 2022-01-16 op if (errstr != NULL) {
839 5d0dedb3 2022-11-23 op fprintf(stderr, "%s: %s\n", dn, errstr);
840 9c48f915 2022-01-16 op free(errstr);
841 9c48f915 2022-01-16 op return -1;
842 9c48f915 2022-01-16 op }
843 9c48f915 2022-01-16 op
844 9c48f915 2022-01-16 op if (miss != 0) {
845 5d0dedb3 2022-11-23 op fprintf(stderr, "%s: not a directory\n", dn);
846 9c48f915 2022-01-16 op return -1;
847 9c48f915 2022-01-16 op }
848 9c48f915 2022-01-16 op
849 9c48f915 2022-01-16 op if ((n = strrchr(path, '/')) != NULL)
850 9c48f915 2022-01-16 op n++;
851 9c48f915 2022-01-16 op else
852 9c48f915 2022-01-16 op n = path;
853 9c48f915 2022-01-16 op }
854 9c48f915 2022-01-16 op
855 9c48f915 2022-01-16 op free(errstr);
856 9c48f915 2022-01-16 op
857 9c48f915 2022-01-16 op if (miss > 1) {
858 5d0dedb3 2022-11-23 op fprintf(stderr, "can't create %s: missing %d path"
859 5d0dedb3 2022-11-23 op " component(s)\n", path, miss);
860 9c48f915 2022-01-16 op return -1;
861 9c48f915 2022-01-16 op }
862 9c48f915 2022-01-16 op
863 16f1f3bf 2022-01-21 op send_fid(nfid, n, KOTRUNC, fd, prompt);
864 9c48f915 2022-01-16 op return 0;
865 fb1a36c0 2022-01-09 op }
866 fb1a36c0 2022-01-09 op
867 8f7253b2 2022-11-22 op static int
868 8f7253b2 2022-11-22 op dial(const char *host, const char *port)
869 fb1a36c0 2022-01-09 op {
870 fb1a36c0 2022-01-09 op struct addrinfo hints, *res, *res0;
871 8f7253b2 2022-11-22 op int sock, error, saved_errno;
872 fb1a36c0 2022-01-09 op const char *cause = NULL;
873 fb1a36c0 2022-01-09 op
874 fb1a36c0 2022-01-09 op memset(&hints, 0, sizeof(hints));
875 fb1a36c0 2022-01-09 op hints.ai_family = AF_UNSPEC;
876 fb1a36c0 2022-01-09 op hints.ai_socktype = SOCK_STREAM;
877 fb1a36c0 2022-01-09 op error = getaddrinfo(host, port, &hints, &res0);
878 fb1a36c0 2022-01-09 op if (error)
879 fb1a36c0 2022-01-09 op errx(1, "%s", gai_strerror(error));
880 fb1a36c0 2022-01-09 op
881 fb1a36c0 2022-01-09 op sock = -1;
882 fb1a36c0 2022-01-09 op for (res = res0; res != NULL; res = res->ai_next) {
883 db066f1e 2022-01-12 op sock = socket(res->ai_family, res->ai_socktype|SOCK_CLOEXEC,
884 fb1a36c0 2022-01-09 op res->ai_protocol);
885 fb1a36c0 2022-01-09 op if (sock == -1) {
886 fb1a36c0 2022-01-09 op cause = "socket";
887 fb1a36c0 2022-01-09 op continue;
888 fb1a36c0 2022-01-09 op }
889 fb1a36c0 2022-01-09 op
890 fb1a36c0 2022-01-09 op if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
891 fb1a36c0 2022-01-09 op cause = "connect";
892 fb1a36c0 2022-01-09 op saved_errno = errno;
893 fb1a36c0 2022-01-09 op close(sock);
894 fb1a36c0 2022-01-09 op errno = saved_errno;
895 fb1a36c0 2022-01-09 op sock = -1;
896 fb1a36c0 2022-01-09 op continue;
897 fb1a36c0 2022-01-09 op }
898 fb1a36c0 2022-01-09 op
899 fb1a36c0 2022-01-09 op break;
900 fb1a36c0 2022-01-09 op }
901 8f7253b2 2022-11-22 op freeaddrinfo(res0);
902 fb1a36c0 2022-01-09 op if (sock == -1)
903 fb1a36c0 2022-01-09 op err(1, "%s", cause);
904 8f7253b2 2022-11-22 op return sock;
905 fb1a36c0 2022-01-09 op }
906 fb1a36c0 2022-01-09 op
907 fb1a36c0 2022-01-09 op static void
908 14ae3944 2022-11-23 op do_connect(const char *host, const char *port, const char *user)
909 fb1a36c0 2022-01-09 op {
910 14ae3944 2022-11-23 op struct qid qid;
911 14ae3944 2022-11-23 op int nfid, miss, fd;
912 14ae3944 2022-11-23 op char *errstr;
913 fb1a36c0 2022-01-09 op
914 5d0dedb3 2022-11-23 op fprintf(stderr, "connecting to %s:%s...", host, port);
915 fb1a36c0 2022-01-09 op
916 8f7253b2 2022-11-22 op if (tls) {
917 8f7253b2 2022-11-22 op struct tls_config *conf;
918 8f7253b2 2022-11-22 op struct tls *ctx;
919 8f7253b2 2022-11-22 op int r;
920 fb1a36c0 2022-01-09 op
921 8f7253b2 2022-11-22 op if ((conf = tls_config_new()) == NULL)
922 8f7253b2 2022-11-22 op fatalx("failed to create TLS config");
923 8f7253b2 2022-11-22 op tls_config_insecure_noverifycert(conf);
924 8f7253b2 2022-11-22 op tls_config_insecure_noverifyname(conf);
925 8f7253b2 2022-11-22 op
926 8f7253b2 2022-11-22 op if (keypath == NULL)
927 8f7253b2 2022-11-22 op keypath = crtpath;
928 8f7253b2 2022-11-22 op
929 8f7253b2 2022-11-22 op if (tls_config_set_keypair_file(conf, crtpath, keypath) == -1)
930 8f7253b2 2022-11-22 op fatalx("failed to load certs: (%s, %s)", crtpath,
931 8f7253b2 2022-11-22 op keypath);
932 8f7253b2 2022-11-22 op
933 8f7253b2 2022-11-22 op if ((ctx = tls_client()) == NULL)
934 8f7253b2 2022-11-22 op fatalx("failed to create TLS client");
935 8f7253b2 2022-11-22 op if (tls_configure(ctx, conf) == -1)
936 8f7253b2 2022-11-22 op fatalx("failed to configure TLS client");
937 8f7253b2 2022-11-22 op tls_config_free(conf);
938 8f7253b2 2022-11-22 op
939 8f7253b2 2022-11-22 op if (tls_connect(ctx, host, port) == -1)
940 8f7253b2 2022-11-22 op fatalx("failed to connect to %s:%s: %s", host, port,
941 8f7253b2 2022-11-22 op tls_error(ctx));
942 8f7253b2 2022-11-22 op
943 8f7253b2 2022-11-22 op do {
944 8f7253b2 2022-11-22 op r = tls_handshake(ctx);
945 8f7253b2 2022-11-22 op } while (r == TLS_WANT_POLLIN || r == TLS_WANT_POLLOUT);
946 8f7253b2 2022-11-22 op fp = funopen(ctx, stdio_tls_read, stdio_tls_write, NULL,
947 8f7253b2 2022-11-22 op stdio_tls_close);
948 8f7253b2 2022-11-22 op if (fp == NULL)
949 8f7253b2 2022-11-22 op fatal("funopen");
950 8f7253b2 2022-11-22 op } else {
951 8f7253b2 2022-11-22 op int fd;
952 8f7253b2 2022-11-22 op
953 8f7253b2 2022-11-22 op fd = dial(host, port);
954 8f7253b2 2022-11-22 op if ((fp = fdopen(fd, "r+")) == NULL)
955 8f7253b2 2022-11-22 op fatal("fdopen");
956 8f7253b2 2022-11-22 op }
957 8f7253b2 2022-11-22 op
958 5d0dedb3 2022-11-23 op fprintf(stderr, " done!\n");
959 fb1a36c0 2022-01-09 op
960 fb1a36c0 2022-01-09 op do_version();
961 14ae3944 2022-11-23 op do_attach(user);
962 fb1a36c0 2022-01-09 op }
963 fb1a36c0 2022-01-09 op
964 64d8db89 2022-01-16 op static int
965 64d8db89 2022-01-16 op tmp_file(char sfn[TMPFSTRLEN])
966 64d8db89 2022-01-16 op {
967 64d8db89 2022-01-16 op int tmpfd;
968 64d8db89 2022-01-16 op
969 64d8db89 2022-01-16 op strlcpy(sfn, TMPFSTR, TMPFSTRLEN);
970 64d8db89 2022-01-16 op if ((tmpfd = mkstemp(sfn)) == -1) {
971 64d8db89 2022-01-16 op warn("mkstemp %s", sfn);
972 64d8db89 2022-01-16 op return -1;
973 64d8db89 2022-01-16 op }
974 64d8db89 2022-01-16 op
975 aa76fd7f 2022-01-16 op /* set the close-on-exec flag */
976 aa76fd7f 2022-01-16 op if (fcntl(tmpfd, F_SETFD, FD_CLOEXEC) == -1) {
977 aa76fd7f 2022-01-16 op warn("fcntl");
978 aa76fd7f 2022-01-16 op close(tmpfd);
979 aa76fd7f 2022-01-16 op return -1;
980 aa76fd7f 2022-01-16 op }
981 aa76fd7f 2022-01-16 op
982 64d8db89 2022-01-16 op return tmpfd;
983 64d8db89 2022-01-16 op }
984 64d8db89 2022-01-16 op
985 1f5fb843 2022-01-16 op static inline const char *
986 1f5fb843 2022-01-16 op pp_perm(uint8_t x)
987 1f5fb843 2022-01-16 op {
988 1f5fb843 2022-01-16 op switch (x & 0x7) {
989 1f5fb843 2022-01-16 op case 0x0:
990 1f5fb843 2022-01-16 op return "---";
991 1f5fb843 2022-01-16 op case 0x1:
992 1f5fb843 2022-01-16 op return "--x";
993 1f5fb843 2022-01-16 op case 0x2:
994 1f5fb843 2022-01-16 op return "-w-";
995 1f5fb843 2022-01-16 op case 0x3:
996 1f5fb843 2022-01-16 op return "-wx";
997 1f5fb843 2022-01-16 op case 0x4:
998 1f5fb843 2022-01-16 op return "r--";
999 1f5fb843 2022-01-16 op case 0x5:
1000 1f5fb843 2022-01-16 op return "r-x";
1001 1f5fb843 2022-01-16 op case 0x6:
1002 1f5fb843 2022-01-16 op return "rw-";
1003 1f5fb843 2022-01-16 op case 0x7:
1004 1f5fb843 2022-01-16 op return "rwx";
1005 1f5fb843 2022-01-16 op default:
1006 1f5fb843 2022-01-16 op /* unreachable, just for the compiler' happiness */
1007 1f5fb843 2022-01-16 op return "???";
1008 1f5fb843 2022-01-16 op }
1009 c37e1cfe 2022-01-17 op }
1010 c37e1cfe 2022-01-17 op
1011 c37e1cfe 2022-01-17 op static inline void
1012 c37e1cfe 2022-01-17 op prepare_wstat(struct np_stat *st)
1013 c37e1cfe 2022-01-17 op {
1014 c37e1cfe 2022-01-17 op memset(st, 0xFF, sizeof(*st));
1015 c37e1cfe 2022-01-17 op st->name = NULL;
1016 c37e1cfe 2022-01-17 op st->uid = NULL;
1017 c37e1cfe 2022-01-17 op st->gid = NULL;
1018 c37e1cfe 2022-01-17 op st->muid = NULL;
1019 1f5fb843 2022-01-16 op }
1020 1f5fb843 2022-01-16 op
1021 fb1a36c0 2022-01-09 op static void
1022 fb1a36c0 2022-01-09 op cmd_bell(int argc, const char **argv)
1023 fb1a36c0 2022-01-09 op {
1024 fb1a36c0 2022-01-09 op if (argc == 0) {
1025 fb1a36c0 2022-01-09 op bell = !bell;
1026 fb1a36c0 2022-01-09 op if (bell)
1027 fb1a36c0 2022-01-09 op puts("bell mode enabled");
1028 fb1a36c0 2022-01-09 op else
1029 fb1a36c0 2022-01-09 op puts("bell mode disabled");
1030 fb1a36c0 2022-01-09 op return;
1031 fb1a36c0 2022-01-09 op }
1032 fb1a36c0 2022-01-09 op
1033 fb1a36c0 2022-01-09 op if (argc != 1)
1034 fb1a36c0 2022-01-09 op goto usage;
1035 fb1a36c0 2022-01-09 op
1036 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "on")) {
1037 fb1a36c0 2022-01-09 op bell = 1;
1038 fb1a36c0 2022-01-09 op puts("bell mode enabled");
1039 fb1a36c0 2022-01-09 op return;
1040 fb1a36c0 2022-01-09 op }
1041 fb1a36c0 2022-01-09 op
1042 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "off")) {
1043 fb1a36c0 2022-01-09 op bell = 0;
1044 fb1a36c0 2022-01-09 op puts("bell mode disabled");
1045 fb1a36c0 2022-01-09 op return;
1046 fb1a36c0 2022-01-09 op }
1047 fb1a36c0 2022-01-09 op
1048 fb1a36c0 2022-01-09 op usage:
1049 fb1a36c0 2022-01-09 op printf("bell [on | off]\n");
1050 fb1a36c0 2022-01-09 op }
1051 fb1a36c0 2022-01-09 op
1052 fb1a36c0 2022-01-09 op static void
1053 fb1a36c0 2022-01-09 op cmd_bye(int argc, const char **argv)
1054 fb1a36c0 2022-01-09 op {
1055 fb1a36c0 2022-01-09 op log_warnx("bye\n");
1056 8f7253b2 2022-11-22 op fclose(fp);
1057 fb1a36c0 2022-01-09 op exit(0);
1058 fb1a36c0 2022-01-09 op }
1059 fb1a36c0 2022-01-09 op
1060 fb1a36c0 2022-01-09 op static void
1061 fb1a36c0 2022-01-09 op cmd_cd(int argc, const char **argv)
1062 fb1a36c0 2022-01-09 op {
1063 fb1a36c0 2022-01-09 op struct qid qid;
1064 8f9f99f7 2022-01-16 op int nfid, miss;
1065 8f9f99f7 2022-01-16 op char *errstr;
1066 fb1a36c0 2022-01-09 op
1067 fb1a36c0 2022-01-09 op if (argc != 1) {
1068 fb1a36c0 2022-01-09 op printf("usage: cd remote-path\n");
1069 fb1a36c0 2022-01-09 op return;
1070 fb1a36c0 2022-01-09 op }
1071 fb1a36c0 2022-01-09 op
1072 8d8fb849 2022-01-17 op nfid = nextfid();
1073 8f9f99f7 2022-01-16 op errstr = walk_path(pwdfid, nfid, argv[0], &miss, &qid);
1074 8f9f99f7 2022-01-16 op if (errstr != NULL) {
1075 8f9f99f7 2022-01-16 op printf("%s: %s\n", argv[0], errstr);
1076 8f9f99f7 2022-01-16 op free(errstr);
1077 8f9f99f7 2022-01-16 op return;
1078 8f9f99f7 2022-01-16 op }
1079 8f9f99f7 2022-01-16 op
1080 8f9f99f7 2022-01-16 op if (miss != 0 || !(qid.type & QTDIR)) {
1081 8f9f99f7 2022-01-16 op printf("%s: not a directory\n", argv[0]);
1082 fcd9d510 2022-01-16 op if (miss == 0)
1083 fcd9d510 2022-01-16 op do_clunk(nfid);
1084 8f9f99f7 2022-01-16 op return;
1085 fb1a36c0 2022-01-09 op }
1086 8f9f99f7 2022-01-16 op
1087 8f9f99f7 2022-01-16 op do_clunk(pwdfid);
1088 8f9f99f7 2022-01-16 op pwdfid = nfid;
1089 fb1a36c0 2022-01-09 op }
1090 fb1a36c0 2022-01-09 op
1091 fb1a36c0 2022-01-09 op static void
1092 9c48f915 2022-01-16 op cmd_edit(int argc, const char **argv)
1093 9c48f915 2022-01-16 op {
1094 9c48f915 2022-01-16 op struct qid qid;
1095 9c48f915 2022-01-16 op int nfid, tmpfd, miss;
1096 9c48f915 2022-01-16 op char sfn[TMPFSTRLEN], p[PATH_MAX], *name, *errstr;
1097 9c48f915 2022-01-16 op const char *ed;
1098 9c48f915 2022-01-16 op
1099 9c48f915 2022-01-16 op if (argc != 1) {
1100 9c48f915 2022-01-16 op puts("usage: edit file");
1101 9c48f915 2022-01-16 op return;
1102 9c48f915 2022-01-16 op }
1103 9c48f915 2022-01-16 op
1104 9c48f915 2022-01-16 op if ((ed = getenv("VISUAL")) == NULL &&
1105 9c48f915 2022-01-16 op (ed = getenv("EDITOR")) == NULL)
1106 9c48f915 2022-01-16 op ed = "ed";
1107 9c48f915 2022-01-16 op
1108 8d8fb849 2022-01-17 op nfid = nextfid();
1109 9c48f915 2022-01-16 op errstr = walk_path(pwdfid, nfid, *argv, &miss, &qid);
1110 9c48f915 2022-01-16 op if (errstr != NULL) {
1111 9c48f915 2022-01-16 op printf("%s: %s\n", *argv, errstr);
1112 9c48f915 2022-01-16 op free(errstr);
1113 9c48f915 2022-01-16 op return;
1114 9c48f915 2022-01-16 op }
1115 9c48f915 2022-01-16 op
1116 9c48f915 2022-01-16 op if (miss != 0 || qid.type != 0) {
1117 9c48f915 2022-01-16 op printf("%s: not a file\n", *argv);
1118 9c48f915 2022-01-16 op if (miss == 0)
1119 9c48f915 2022-01-16 op do_clunk(nfid);
1120 9c48f915 2022-01-16 op return;
1121 9c48f915 2022-01-16 op }
1122 9c48f915 2022-01-16 op
1123 9c48f915 2022-01-16 op if ((tmpfd = tmp_file(sfn)) == -1) {
1124 9c48f915 2022-01-16 op do_clunk(nfid);
1125 9c48f915 2022-01-16 op return;
1126 9c48f915 2022-01-16 op }
1127 9c48f915 2022-01-16 op
1128 9c48f915 2022-01-16 op strlcpy(p, *argv, sizeof(p));
1129 9c48f915 2022-01-16 op name = basename(p);
1130 9c48f915 2022-01-16 op
1131 02571aa1 2022-01-29 op if (fetch_fid(nfid, tmpfd, name)) {
1132 02571aa1 2022-01-29 op warn("failed fetch or can't write %s", sfn);
1133 02571aa1 2022-01-29 op goto end;
1134 02571aa1 2022-01-29 op }
1135 9c48f915 2022-01-16 op close(tmpfd);
1136 9c48f915 2022-01-16 op
1137 9c48f915 2022-01-16 op spawn(ed, sfn, NULL);
1138 9c48f915 2022-01-16 op
1139 9c48f915 2022-01-16 op /*
1140 9c48f915 2022-01-16 op * Re-open the file because it's not guaranteed that the
1141 9c48f915 2022-01-16 op * file descriptor tmpfd is still associated with the file
1142 9c48f915 2022-01-16 op * pointed by sfn: it's not uncommon for editor to write
1143 9c48f915 2022-01-16 op * a backup file and then rename(2) it to the file name.
1144 9c48f915 2022-01-16 op */
1145 9c48f915 2022-01-16 op if ((tmpfd = open(sfn, O_RDONLY)) == -1) {
1146 9c48f915 2022-01-16 op warn("can't open %s", sfn);
1147 9c48f915 2022-01-16 op goto end;
1148 9c48f915 2022-01-16 op }
1149 9c48f915 2022-01-16 op
1150 9c48f915 2022-01-16 op woc_file(tmpfd, *argv, name);
1151 9c48f915 2022-01-16 op close(tmpfd);
1152 9c48f915 2022-01-16 op
1153 9c48f915 2022-01-16 op end:
1154 9c48f915 2022-01-16 op unlink(sfn);
1155 9c48f915 2022-01-16 op }
1156 9c48f915 2022-01-16 op
1157 9c48f915 2022-01-16 op static void
1158 fb1a36c0 2022-01-09 op cmd_get(int argc, const char **argv)
1159 fb1a36c0 2022-01-09 op {
1160 fb1a36c0 2022-01-09 op struct qid qid;
1161 fb1a36c0 2022-01-09 op const char *l;
1162 8f9f99f7 2022-01-16 op char *errstr;
1163 8f9f99f7 2022-01-16 op int nfid, fd, miss;
1164 fb1a36c0 2022-01-09 op
1165 fb1a36c0 2022-01-09 op if (argc != 1 && argc != 2) {
1166 fb1a36c0 2022-01-09 op printf("usage: get remote-file [local-file]\n");
1167 fb1a36c0 2022-01-09 op return;
1168 fb1a36c0 2022-01-09 op }
1169 fb1a36c0 2022-01-09 op
1170 fb1a36c0 2022-01-09 op if (argc == 2)
1171 fb1a36c0 2022-01-09 op l = argv[1];
1172 fb1a36c0 2022-01-09 op else if ((l = strrchr(argv[0], '/')) != NULL)
1173 fb1a36c0 2022-01-09 op l++; /* skip / */
1174 fb1a36c0 2022-01-09 op else
1175 2ba25711 2022-01-16 op l = argv[0];
1176 fb1a36c0 2022-01-09 op
1177 8d8fb849 2022-01-17 op nfid = nextfid();
1178 8f9f99f7 2022-01-16 op errstr = walk_path(pwdfid, nfid, argv[0], &miss, &qid);
1179 8f9f99f7 2022-01-16 op if (errstr != NULL) {
1180 8f9f99f7 2022-01-16 op printf("%s: %s\n", argv[0], errstr);
1181 8f9f99f7 2022-01-16 op free(errstr);
1182 fb1a36c0 2022-01-09 op return;
1183 fb1a36c0 2022-01-09 op }
1184 fb1a36c0 2022-01-09 op
1185 8f9f99f7 2022-01-16 op if (miss != 0 || qid.type != 0) {
1186 8f9f99f7 2022-01-16 op printf("%s: not a file\n", argv[0]);
1187 fcd9d510 2022-01-16 op if (miss == 0)
1188 fcd9d510 2022-01-16 op do_clunk(nfid);
1189 fb1a36c0 2022-01-09 op return;
1190 fb1a36c0 2022-01-09 op }
1191 fb1a36c0 2022-01-09 op
1192 76521d26 2022-01-16 op if ((fd = open(l, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0644)) == -1) {
1193 76521d26 2022-01-16 op warn("can't open %s", l);
1194 76521d26 2022-01-16 op do_clunk(nfid);
1195 76521d26 2022-01-16 op return;
1196 76521d26 2022-01-16 op }
1197 76521d26 2022-01-16 op
1198 02571aa1 2022-01-29 op if (fetch_fid(nfid, fd, l) == -1)
1199 02571aa1 2022-01-29 op warn("write %s", l);
1200 76521d26 2022-01-16 op close(fd);
1201 5565d021 2022-01-16 op }
1202 5565d021 2022-01-16 op
1203 5565d021 2022-01-16 op static void
1204 5565d021 2022-01-16 op cmd_hexdump(int argc, const char **argv)
1205 5565d021 2022-01-16 op {
1206 5565d021 2022-01-16 op if (argc == 0) {
1207 5565d021 2022-01-16 op xdump = !xdump;
1208 5565d021 2022-01-16 op if (xdump)
1209 5565d021 2022-01-16 op puts("hexdump mode enabled");
1210 5565d021 2022-01-16 op else
1211 5565d021 2022-01-16 op puts("hexdump mode disabled");
1212 5565d021 2022-01-16 op return;
1213 5565d021 2022-01-16 op }
1214 5565d021 2022-01-16 op
1215 5565d021 2022-01-16 op if (argc > 1)
1216 5565d021 2022-01-16 op goto usage;
1217 5565d021 2022-01-16 op
1218 5565d021 2022-01-16 op if (!strcmp(*argv, "on")) {
1219 5565d021 2022-01-16 op xdump = 1;
1220 5565d021 2022-01-16 op puts("hexdump mode enabled");
1221 5565d021 2022-01-16 op return;
1222 5565d021 2022-01-16 op }
1223 5565d021 2022-01-16 op
1224 5565d021 2022-01-16 op if (!strcmp(*argv, "off")) {
1225 5565d021 2022-01-16 op xdump = 0;
1226 5565d021 2022-01-16 op puts("hexdump mode disabled");
1227 5565d021 2022-01-16 op return;
1228 5565d021 2022-01-16 op }
1229 5565d021 2022-01-16 op
1230 5565d021 2022-01-16 op usage:
1231 5565d021 2022-01-16 op puts("usage: hexdump [on | off]");
1232 fb1a36c0 2022-01-09 op }
1233 fb1a36c0 2022-01-09 op
1234 fb1a36c0 2022-01-09 op static void
1235 fb1a36c0 2022-01-09 op cmd_lcd(int argc, const char **argv)
1236 fb1a36c0 2022-01-09 op {
1237 fb1a36c0 2022-01-09 op const char *dir;
1238 fb1a36c0 2022-01-09 op
1239 fb1a36c0 2022-01-09 op if (argc > 1) {
1240 d166ffc0 2022-01-16 op printf("usage: lcd [local-directory]\n");
1241 fb1a36c0 2022-01-09 op return;
1242 fb1a36c0 2022-01-09 op }
1243 fb1a36c0 2022-01-09 op
1244 fb1a36c0 2022-01-09 op if (argc == 1)
1245 fb1a36c0 2022-01-09 op dir = *argv;
1246 fb1a36c0 2022-01-09 op
1247 fb1a36c0 2022-01-09 op if (argc == 0 && (dir = getenv("HOME")) == NULL) {
1248 fb1a36c0 2022-01-09 op printf("HOME is not defined\n");
1249 fb1a36c0 2022-01-09 op return;
1250 fb1a36c0 2022-01-09 op }
1251 fb1a36c0 2022-01-09 op
1252 fb1a36c0 2022-01-09 op if (chdir(dir) == -1)
1253 fb1a36c0 2022-01-09 op printf("cd: %s: %s\n", dir, strerror(errno));
1254 fb1a36c0 2022-01-09 op }
1255 fb1a36c0 2022-01-09 op
1256 fb1a36c0 2022-01-09 op static void
1257 fb1a36c0 2022-01-09 op cmd_lpwd(int argc, const char **argv)
1258 fb1a36c0 2022-01-09 op {
1259 fb1a36c0 2022-01-09 op char path[PATH_MAX];
1260 fb1a36c0 2022-01-09 op
1261 d166ffc0 2022-01-16 op if (argc != 0) {
1262 d166ffc0 2022-01-16 op printf("usage: lpwd\n");
1263 d166ffc0 2022-01-16 op return;
1264 d166ffc0 2022-01-16 op }
1265 d166ffc0 2022-01-16 op
1266 fb1a36c0 2022-01-09 op if (getcwd(path, sizeof(path)) == NULL) {
1267 fb1a36c0 2022-01-09 op printf("lpwd: %s\n", strerror(errno));
1268 fb1a36c0 2022-01-09 op return;
1269 fb1a36c0 2022-01-09 op }
1270 fb1a36c0 2022-01-09 op
1271 fb1a36c0 2022-01-09 op printf("%s\n", path);
1272 fb1a36c0 2022-01-09 op }
1273 fb1a36c0 2022-01-09 op
1274 fb1a36c0 2022-01-09 op static void
1275 fb1a36c0 2022-01-09 op cmd_ls(int argc, const char **argv)
1276 fb1a36c0 2022-01-09 op {
1277 fb1a36c0 2022-01-09 op struct np_stat st;
1278 4f52d9af 2022-01-16 op time_t now, mtime;
1279 4f52d9af 2022-01-16 op struct tm *tm;
1280 fb1a36c0 2022-01-09 op uint64_t off = 0;
1281 fb1a36c0 2022-01-09 op uint32_t len;
1282 a9288ad9 2022-01-17 op int nfid;
1283 4f52d9af 2022-01-16 op const char *timfmt;
1284 4f52d9af 2022-01-16 op char fmt[FMT_SCALED_STRSIZE], tim[13], *errstr;
1285 fb1a36c0 2022-01-09 op
1286 fb1a36c0 2022-01-09 op if (argc != 0) {
1287 fb1a36c0 2022-01-09 op printf("ls don't take arguments (yet)\n");
1288 fb1a36c0 2022-01-09 op return;
1289 fb1a36c0 2022-01-09 op }
1290 fb1a36c0 2022-01-09 op
1291 4f52d9af 2022-01-16 op now = time(NULL);
1292 4f52d9af 2022-01-16 op
1293 a9288ad9 2022-01-17 op nfid = nextfid();
1294 a9288ad9 2022-01-17 op if ((errstr = dup_fid(pwdfid, nfid)) != NULL) {
1295 1bb1bfca 2022-01-16 op printf(".: %s\n", errstr);
1296 1bb1bfca 2022-01-16 op free(errstr);
1297 1bb1bfca 2022-01-16 op return;
1298 1bb1bfca 2022-01-16 op }
1299 1bb1bfca 2022-01-16 op
1300 a9288ad9 2022-01-17 op do_open(nfid, KOREAD);
1301 fb1a36c0 2022-01-09 op
1302 fb1a36c0 2022-01-09 op evbuffer_drain(dirbuf, EVBUFFER_LENGTH(dirbuf));
1303 fb1a36c0 2022-01-09 op
1304 fb1a36c0 2022-01-09 op for (;;) {
1305 7bafb2fd 2022-08-28 op tread(nfid, off, msize - IOHDRSZ);
1306 fb1a36c0 2022-01-09 op do_send();
1307 fb1a36c0 2022-01-09 op recv_msg();
1308 fb1a36c0 2022-01-09 op expect2(Rread, iota_tag);
1309 fb1a36c0 2022-01-09 op
1310 fb1a36c0 2022-01-09 op len = np_read32(buf);
1311 fb1a36c0 2022-01-09 op if (len == 0)
1312 fb1a36c0 2022-01-09 op break;
1313 fb1a36c0 2022-01-09 op
1314 fb1a36c0 2022-01-09 op evbuffer_add_buffer(dirbuf, buf);
1315 fb1a36c0 2022-01-09 op off += len;
1316 fb1a36c0 2022-01-09 op
1317 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
1318 fb1a36c0 2022-01-09 op }
1319 fb1a36c0 2022-01-09 op
1320 fb1a36c0 2022-01-09 op while (EVBUFFER_LENGTH(dirbuf) != 0) {
1321 fb1a36c0 2022-01-09 op if (np_read_stat(dirbuf, &st) == -1)
1322 fb1a36c0 2022-01-09 op errx(1, "invalid stat struct read");
1323 fb1a36c0 2022-01-09 op
1324 fb1a36c0 2022-01-09 op if (fmt_scaled(st.length, fmt) == -1)
1325 fb1a36c0 2022-01-09 op strlcpy(fmt, "xxx", sizeof(fmt));
1326 4f52d9af 2022-01-16 op
1327 4f52d9af 2022-01-16 op mtime = st.mtime;
1328 4f52d9af 2022-01-16 op
1329 4f52d9af 2022-01-16 op if (now > mtime && (now - mtime) < 365/2 * 24 * 12 * 60)
1330 4f52d9af 2022-01-16 op timfmt = "%b %e %R";
1331 4f52d9af 2022-01-16 op else
1332 4f52d9af 2022-01-16 op timfmt = "%b %e %Y";
1333 fb1a36c0 2022-01-09 op
1334 4f52d9af 2022-01-16 op if ((tm = localtime(&mtime)) == NULL ||
1335 4f52d9af 2022-01-16 op strftime(tim, sizeof(tim), timfmt, tm) == 0)
1336 4f52d9af 2022-01-16 op strlcpy(tim, "unknown", sizeof(tim));
1337 4f52d9af 2022-01-16 op
1338 1f5fb843 2022-01-16 op if (st.qid.type & QTDIR)
1339 1f5fb843 2022-01-16 op printf("d");
1340 1f5fb843 2022-01-16 op else
1341 1f5fb843 2022-01-16 op printf("-");
1342 1f5fb843 2022-01-16 op printf("%s", pp_perm(st.mode >> 6));
1343 1f5fb843 2022-01-16 op printf("%s", pp_perm(st.mode >> 3));
1344 1f5fb843 2022-01-16 op printf("%s", pp_perm(st.mode));
1345 d33c93c4 2022-01-17 op printf(" %8s %12s %s%s\n", fmt, tim, st.name,
1346 6470594e 2022-01-16 op st.qid.type & QTDIR ? "/" : "");
1347 fb1a36c0 2022-01-09 op
1348 fb1a36c0 2022-01-09 op free(st.name);
1349 fb1a36c0 2022-01-09 op free(st.uid);
1350 fb1a36c0 2022-01-09 op free(st.gid);
1351 fb1a36c0 2022-01-09 op free(st.muid);
1352 fb1a36c0 2022-01-09 op }
1353 fb1a36c0 2022-01-09 op
1354 a9288ad9 2022-01-17 op do_clunk(nfid);
1355 fb1a36c0 2022-01-09 op }
1356 fb1a36c0 2022-01-09 op
1357 fb1a36c0 2022-01-09 op static void
1358 9e7dd230 2022-01-12 op cmd_page(int argc, const char **argv)
1359 9e7dd230 2022-01-12 op {
1360 9e7dd230 2022-01-12 op struct qid qid;
1361 02571aa1 2022-01-29 op int nfid, tmpfd, miss, r;
1362 64d8db89 2022-01-16 op char sfn[TMPFSTRLEN], p[PATH_MAX], *name, *errstr;
1363 62f663bb 2022-01-16 op const char *pager;
1364 9e7dd230 2022-01-12 op
1365 9e7dd230 2022-01-12 op if (argc != 1) {
1366 9e7dd230 2022-01-12 op puts("usage: page file");
1367 9e7dd230 2022-01-12 op return;
1368 9e7dd230 2022-01-12 op }
1369 62f663bb 2022-01-16 op
1370 62f663bb 2022-01-16 op if ((pager = getenv("PAGER")) == NULL)
1371 62f663bb 2022-01-16 op pager = "less";
1372 9e7dd230 2022-01-12 op
1373 8d8fb849 2022-01-17 op nfid = nextfid();
1374 8f9f99f7 2022-01-16 op errstr = walk_path(pwdfid, nfid, *argv, &miss, &qid);
1375 8f9f99f7 2022-01-16 op if (errstr != NULL) {
1376 8f9f99f7 2022-01-16 op printf("%s: %s\n", *argv, errstr);
1377 8f9f99f7 2022-01-16 op free(errstr);
1378 9e7dd230 2022-01-12 op return;
1379 9e7dd230 2022-01-12 op }
1380 9e7dd230 2022-01-12 op
1381 8f9f99f7 2022-01-16 op if (miss != 0 || qid.type != 0) {
1382 8f9f99f7 2022-01-16 op printf("%s: not a file\n", *argv);
1383 fcd9d510 2022-01-16 op if (miss == 0)
1384 fcd9d510 2022-01-16 op do_clunk(nfid);
1385 9e7dd230 2022-01-12 op return;
1386 9e7dd230 2022-01-12 op }
1387 9e7dd230 2022-01-12 op
1388 64d8db89 2022-01-16 op if ((tmpfd = tmp_file(sfn)) == -1) {
1389 9e7dd230 2022-01-12 op do_clunk(nfid);
1390 9e7dd230 2022-01-12 op return;
1391 9e7dd230 2022-01-12 op }
1392 9e7dd230 2022-01-12 op
1393 9e7dd230 2022-01-12 op strlcpy(p, *argv, sizeof(p));
1394 9e7dd230 2022-01-12 op name = basename(p);
1395 02571aa1 2022-01-29 op if ((r = fetch_fid(nfid, tmpfd, name)) == -1)
1396 02571aa1 2022-01-29 op warn("write %s", sfn);
1397 9e7dd230 2022-01-12 op close(tmpfd);
1398 02571aa1 2022-01-29 op if (r != -1)
1399 02571aa1 2022-01-29 op spawn(pager, sfn, NULL);
1400 9e7dd230 2022-01-12 op unlink(sfn);
1401 01fb39ab 2022-01-16 op }
1402 01fb39ab 2022-01-16 op
1403 01fb39ab 2022-01-16 op static void
1404 1c9ab7cf 2022-01-29 op cmd_pipe(int argc, const char **argv)
1405 1c9ab7cf 2022-01-29 op {
1406 1c9ab7cf 2022-01-29 op struct qid qid;
1407 1c9ab7cf 2022-01-29 op pid_t pid;
1408 1c9ab7cf 2022-01-29 op int nfid, tmpfd, miss, status;
1409 1c9ab7cf 2022-01-29 op int filedes[2]; /* read end, write end */
1410 1c9ab7cf 2022-01-29 op char *errstr;
1411 1c9ab7cf 2022-01-29 op
1412 1c9ab7cf 2022-01-29 op if (argc < 2) {
1413 1c9ab7cf 2022-01-29 op puts("usage: pipe remote-file cmd [args...]");
1414 1c9ab7cf 2022-01-29 op return;
1415 1c9ab7cf 2022-01-29 op }
1416 1c9ab7cf 2022-01-29 op
1417 1c9ab7cf 2022-01-29 op nfid = nextfid();
1418 1c9ab7cf 2022-01-29 op errstr = walk_path(pwdfid, nfid, *argv, &miss, &qid);
1419 1c9ab7cf 2022-01-29 op if (errstr != NULL) {
1420 1c9ab7cf 2022-01-29 op printf("%s: %s\n", *argv, errstr);
1421 1c9ab7cf 2022-01-29 op free(errstr);
1422 1c9ab7cf 2022-01-29 op return;
1423 1c9ab7cf 2022-01-29 op }
1424 1c9ab7cf 2022-01-29 op
1425 1c9ab7cf 2022-01-29 op if (miss != 0 || qid.type != 0) {
1426 1c9ab7cf 2022-01-29 op printf("%s: not a file\n", *argv);
1427 1c9ab7cf 2022-01-29 op if (miss == 0)
1428 1c9ab7cf 2022-01-29 op do_clunk(nfid);
1429 1c9ab7cf 2022-01-29 op return;
1430 1c9ab7cf 2022-01-29 op }
1431 1c9ab7cf 2022-01-29 op
1432 1c9ab7cf 2022-01-29 op if (pipe(filedes) == -1)
1433 1c9ab7cf 2022-01-29 op err(1, "pipe");
1434 1c9ab7cf 2022-01-29 op
1435 1c9ab7cf 2022-01-29 op switch (pid = vfork()) {
1436 1c9ab7cf 2022-01-29 op case -1:
1437 1c9ab7cf 2022-01-29 op err(1, "vfork");
1438 1c9ab7cf 2022-01-29 op case 0:
1439 1c9ab7cf 2022-01-29 op close(filedes[1]);
1440 1c9ab7cf 2022-01-29 op if (dup2(filedes[0], 0) == -1)
1441 1c9ab7cf 2022-01-29 op err(1, "dup2");
1442 1c9ab7cf 2022-01-29 op execvp(argv[1], (char *const *)argv + 1);
1443 1c9ab7cf 2022-01-29 op err(1, "execvp");
1444 1c9ab7cf 2022-01-29 op }
1445 1c9ab7cf 2022-01-29 op
1446 1c9ab7cf 2022-01-29 op close(filedes[0]);
1447 1c9ab7cf 2022-01-29 op if (fetch_fid(nfid, filedes[1], *argv) == -1)
1448 1c9ab7cf 2022-01-29 op warnx("failed to fetch all the file");
1449 1c9ab7cf 2022-01-29 op close(filedes[1]);
1450 1c9ab7cf 2022-01-29 op
1451 1c9ab7cf 2022-01-29 op waitpid(pid, &status, 0);
1452 1c9ab7cf 2022-01-29 op }
1453 1c9ab7cf 2022-01-29 op
1454 1c9ab7cf 2022-01-29 op static void
1455 01fb39ab 2022-01-16 op cmd_put(int argc, const char **argv)
1456 01fb39ab 2022-01-16 op {
1457 01fb39ab 2022-01-16 op struct qid qid;
1458 9c48f915 2022-01-16 op const char *l;
1459 9c48f915 2022-01-16 op int fd;
1460 01fb39ab 2022-01-16 op
1461 01fb39ab 2022-01-16 op if (argc != 1 && argc != 2) {
1462 01fb39ab 2022-01-16 op printf("usage: put local-file [remote-file]\n");
1463 01fb39ab 2022-01-16 op return;
1464 01fb39ab 2022-01-16 op }
1465 01fb39ab 2022-01-16 op
1466 01fb39ab 2022-01-16 op if (argc == 2)
1467 01fb39ab 2022-01-16 op l = argv[1];
1468 01fb39ab 2022-01-16 op else if ((l = strrchr(argv[0], '/')) != NULL)
1469 01fb39ab 2022-01-16 op l++; /* skip / */
1470 01fb39ab 2022-01-16 op else
1471 01fb39ab 2022-01-16 op l = argv[0];
1472 01fb39ab 2022-01-16 op
1473 01fb39ab 2022-01-16 op if ((fd = open(argv[0], O_RDONLY)) == -1) {
1474 01fb39ab 2022-01-16 op warn("%s", argv[0]);
1475 01fb39ab 2022-01-16 op return;
1476 01fb39ab 2022-01-16 op }
1477 01fb39ab 2022-01-16 op
1478 9c48f915 2022-01-16 op woc_file(fd, argv[0], l);
1479 01fb39ab 2022-01-16 op close(fd);
1480 c37e1cfe 2022-01-17 op }
1481 c37e1cfe 2022-01-17 op
1482 c37e1cfe 2022-01-17 op static void
1483 c37e1cfe 2022-01-17 op cmd_rename(int argc, const char **argv)
1484 c37e1cfe 2022-01-17 op {
1485 c37e1cfe 2022-01-17 op struct np_stat st;
1486 c37e1cfe 2022-01-17 op struct qid qid;
1487 c37e1cfe 2022-01-17 op char *errstr;
1488 c37e1cfe 2022-01-17 op int nfid, miss;
1489 c37e1cfe 2022-01-17 op
1490 c37e1cfe 2022-01-17 op if (argc != 2) {
1491 c37e1cfe 2022-01-17 op puts("usage: rename remote-file new-remote-name");
1492 c37e1cfe 2022-01-17 op return;
1493 c37e1cfe 2022-01-17 op }
1494 c37e1cfe 2022-01-17 op
1495 8d8fb849 2022-01-17 op nfid = nextfid();
1496 c37e1cfe 2022-01-17 op errstr = walk_path(pwdfid, nfid, argv[0], &miss, &qid);
1497 c37e1cfe 2022-01-17 op if (errstr != NULL) {
1498 c37e1cfe 2022-01-17 op printf("%s: %s\n", argv[0], errstr);
1499 c37e1cfe 2022-01-17 op free(errstr);
1500 c37e1cfe 2022-01-17 op return;
1501 c37e1cfe 2022-01-17 op }
1502 c37e1cfe 2022-01-17 op
1503 c37e1cfe 2022-01-17 op if (miss != 0) {
1504 c37e1cfe 2022-01-17 op printf("%s: not such file or directory\n", argv[0]);
1505 c37e1cfe 2022-01-17 op return;
1506 c37e1cfe 2022-01-17 op }
1507 c37e1cfe 2022-01-17 op
1508 c37e1cfe 2022-01-17 op prepare_wstat(&st);
1509 c37e1cfe 2022-01-17 op st.name = (char *)argv[1];
1510 c37e1cfe 2022-01-17 op if ((errstr = do_wstat(nfid, &st)) != NULL) {
1511 c37e1cfe 2022-01-17 op printf("rename: %s\n", errstr);
1512 c37e1cfe 2022-01-17 op free(errstr);
1513 c37e1cfe 2022-01-17 op }
1514 c37e1cfe 2022-01-17 op
1515 c37e1cfe 2022-01-17 op do_clunk(nfid);
1516 9e7dd230 2022-01-12 op }
1517 9e7dd230 2022-01-12 op
1518 9e7dd230 2022-01-12 op static void
1519 fb1a36c0 2022-01-09 op cmd_verbose(int argc, const char **argv)
1520 fb1a36c0 2022-01-09 op {
1521 fb1a36c0 2022-01-09 op if (argc == 0) {
1522 fb1a36c0 2022-01-09 op log_setverbose(!log_getverbose());
1523 fb1a36c0 2022-01-09 op if (log_getverbose())
1524 fb1a36c0 2022-01-09 op puts("verbose mode enabled");
1525 fb1a36c0 2022-01-09 op else
1526 fb1a36c0 2022-01-09 op puts("verbose mode disabled");
1527 fb1a36c0 2022-01-09 op return;
1528 fb1a36c0 2022-01-09 op }
1529 fb1a36c0 2022-01-09 op
1530 fb1a36c0 2022-01-09 op if (argc != 1)
1531 fb1a36c0 2022-01-09 op goto usage;
1532 fb1a36c0 2022-01-09 op
1533 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "on")) {
1534 fb1a36c0 2022-01-09 op log_setverbose(1);
1535 fb1a36c0 2022-01-09 op puts("verbose mode enabled");
1536 fb1a36c0 2022-01-09 op return;
1537 fb1a36c0 2022-01-09 op }
1538 fb1a36c0 2022-01-09 op
1539 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "off")) {
1540 fb1a36c0 2022-01-09 op log_setverbose(0);
1541 fb1a36c0 2022-01-09 op puts("verbose mode disabled");
1542 fb1a36c0 2022-01-09 op return;
1543 fb1a36c0 2022-01-09 op }
1544 fb1a36c0 2022-01-09 op
1545 fb1a36c0 2022-01-09 op usage:
1546 fb1a36c0 2022-01-09 op printf("verbose [on | off]\n");
1547 fb1a36c0 2022-01-09 op }
1548 fb1a36c0 2022-01-09 op
1549 fb1a36c0 2022-01-09 op static void
1550 fb1a36c0 2022-01-09 op excmd(int argc, const char **argv)
1551 fb1a36c0 2022-01-09 op {
1552 fb1a36c0 2022-01-09 op struct cmd {
1553 fb1a36c0 2022-01-09 op const char *name;
1554 fb1a36c0 2022-01-09 op void (*fn)(int, const char **);
1555 fb1a36c0 2022-01-09 op } cmds[] = {
1556 fb1a36c0 2022-01-09 op {"bell", cmd_bell},
1557 fb1a36c0 2022-01-09 op {"bye", cmd_bye},
1558 fb1a36c0 2022-01-09 op {"cd", cmd_cd},
1559 9c48f915 2022-01-16 op {"edit", cmd_edit},
1560 fb1a36c0 2022-01-09 op {"get", cmd_get},
1561 5565d021 2022-01-16 op {"hexdump", cmd_hexdump},
1562 fb1a36c0 2022-01-09 op {"lcd", cmd_lcd},
1563 fb1a36c0 2022-01-09 op {"lpwd", cmd_lpwd},
1564 fb1a36c0 2022-01-09 op {"ls", cmd_ls},
1565 9e7dd230 2022-01-12 op {"page", cmd_page},
1566 1c9ab7cf 2022-01-29 op {"pipe", cmd_pipe},
1567 01fb39ab 2022-01-16 op {"put", cmd_put},
1568 fb1a36c0 2022-01-09 op {"quit", cmd_bye},
1569 c37e1cfe 2022-01-17 op {"rename", cmd_rename},
1570 fb1a36c0 2022-01-09 op {"verbose", cmd_verbose},
1571 fb1a36c0 2022-01-09 op };
1572 fb1a36c0 2022-01-09 op size_t i;
1573 fb1a36c0 2022-01-09 op
1574 fb1a36c0 2022-01-09 op if (argc == 0)
1575 fb1a36c0 2022-01-09 op return;
1576 fb1a36c0 2022-01-09 op for (i = 0; i < nitems(cmds); ++i) {
1577 fb1a36c0 2022-01-09 op if (!strcmp(cmds[i].name, *argv)) {
1578 fb1a36c0 2022-01-09 op cmds[i].fn(argc-1, argv+1);
1579 fb1a36c0 2022-01-09 op return;
1580 fb1a36c0 2022-01-09 op }
1581 fb1a36c0 2022-01-09 op }
1582 fb1a36c0 2022-01-09 op
1583 fb1a36c0 2022-01-09 op log_warnx("unknown command %s", *argv);
1584 14ae3944 2022-11-23 op }
1585 14ae3944 2022-11-23 op
1586 14ae3944 2022-11-23 op static void
1587 df9bb54d 2022-11-23 op cd_or_fetch(const char *path, const char *outfile)
1588 14ae3944 2022-11-23 op {
1589 14ae3944 2022-11-23 op struct qid qid;
1590 14ae3944 2022-11-23 op char *errstr;
1591 14ae3944 2022-11-23 op int fd, nfid, miss;
1592 14ae3944 2022-11-23 op
1593 14ae3944 2022-11-23 op while (*path == '/')
1594 14ae3944 2022-11-23 op path++;
1595 14ae3944 2022-11-23 op if (*path == '\0')
1596 14ae3944 2022-11-23 op return;
1597 14ae3944 2022-11-23 op
1598 14ae3944 2022-11-23 op nfid = nextfid();
1599 14ae3944 2022-11-23 op errstr = walk_path(pwdfid, nfid, path, &miss, &qid);
1600 14ae3944 2022-11-23 op if (errstr)
1601 14ae3944 2022-11-23 op errx(1, "walk %s: %s", path, errstr);
1602 14ae3944 2022-11-23 op if (miss)
1603 14ae3944 2022-11-23 op errc(1, ENOENT, "walk %s", path);
1604 14ae3944 2022-11-23 op
1605 14ae3944 2022-11-23 op if (qid.type & QTDIR) {
1606 df9bb54d 2022-11-23 op if (outfile)
1607 df9bb54d 2022-11-23 op errx(1, "can't fetch directory %s", path);
1608 14ae3944 2022-11-23 op do_clunk(pwdfid);
1609 14ae3944 2022-11-23 op pwdfid = nfid;
1610 14ae3944 2022-11-23 op return;
1611 14ae3944 2022-11-23 op }
1612 14ae3944 2022-11-23 op
1613 df9bb54d 2022-11-23 op if (outfile == NULL) {
1614 df9bb54d 2022-11-23 op if ((outfile = strrchr(path, '/')) == NULL)
1615 df9bb54d 2022-11-23 op outfile = path;
1616 df9bb54d 2022-11-23 op else
1617 df9bb54d 2022-11-23 op outfile++;
1618 df9bb54d 2022-11-23 op if (*outfile == '\0')
1619 df9bb54d 2022-11-23 op errx(1, "invalid path: missing file name: %s",
1620 df9bb54d 2022-11-23 op path);
1621 df9bb54d 2022-11-23 op }
1622 14ae3944 2022-11-23 op
1623 df9bb54d 2022-11-23 op if (strcmp(outfile, "-") != 0) {
1624 df9bb54d 2022-11-23 op fd = open(outfile, O_WRONLY|O_CREAT, 0644);
1625 df9bb54d 2022-11-23 op if (fd == -1)
1626 df9bb54d 2022-11-23 op err(1, "can't open for writing %s", outfile);
1627 df9bb54d 2022-11-23 op } else
1628 df9bb54d 2022-11-23 op fd = 1;
1629 df9bb54d 2022-11-23 op
1630 df9bb54d 2022-11-23 op if (fetch_fid(nfid, fd, outfile) == -1)
1631 df9bb54d 2022-11-23 op err(1, "write %s", outfile);
1632 14ae3944 2022-11-23 op close(fd);
1633 14ae3944 2022-11-23 op fclose(fp);
1634 14ae3944 2022-11-23 op exit(0);
1635 fb1a36c0 2022-01-09 op }
1636 fb1a36c0 2022-01-09 op
1637 14ae3944 2022-11-23 op static const char *
1638 14ae3944 2022-11-23 op parse_addr(const char *url, const char **user,
1639 14ae3944 2022-11-23 op const char **port, const char **path)
1640 14ae3944 2022-11-23 op {
1641 14ae3944 2022-11-23 op static char buf[PATH_MAX];
1642 14ae3944 2022-11-23 op char *host, *t;
1643 14ae3944 2022-11-23 op
1644 14ae3944 2022-11-23 op *user = *port = *path = NULL;
1645 14ae3944 2022-11-23 op host = buf;
1646 14ae3944 2022-11-23 op
1647 14ae3944 2022-11-23 op if (strlcpy(buf, url, sizeof(buf)) >= sizeof(buf))
1648 14ae3944 2022-11-23 op errx(1, "connection string too long");
1649 14ae3944 2022-11-23 op
1650 9f134f93 2022-11-23 op if (!strncmp(host, "9p://", 5))
1651 9f134f93 2022-11-23 op host += 5;
1652 9f134f93 2022-11-23 op
1653 14ae3944 2022-11-23 op if ((t = strchr(host, '/')) != NULL) {
1654 14ae3944 2022-11-23 op if (t == host)
1655 14ae3944 2022-11-23 op errx(1, "invalid connection string: %s", url);
1656 14ae3944 2022-11-23 op *t++ = '\0';
1657 14ae3944 2022-11-23 op if (*t != '\0')
1658 14ae3944 2022-11-23 op *path = t;
1659 14ae3944 2022-11-23 op }
1660 14ae3944 2022-11-23 op
1661 14ae3944 2022-11-23 op if ((t = strchr(host, '@')) != NULL) {
1662 14ae3944 2022-11-23 op if (t == host)
1663 14ae3944 2022-11-23 op errx(1, "invalid connection string: %s", url);
1664 14ae3944 2022-11-23 op *t++ = '\0';
1665 14ae3944 2022-11-23 op *user = host;
1666 14ae3944 2022-11-23 op host = t;
1667 14ae3944 2022-11-23 op } else if ((*user = getenv("USER")) == NULL)
1668 14ae3944 2022-11-23 op errx(1, "USER not defined");
1669 14ae3944 2022-11-23 op
1670 14ae3944 2022-11-23 op if ((t = strchr(host, ':')) != NULL) {
1671 14ae3944 2022-11-23 op *t++ = '\0';
1672 14ae3944 2022-11-23 op if (*t != '\0')
1673 14ae3944 2022-11-23 op *port = t;
1674 14ae3944 2022-11-23 op }
1675 14ae3944 2022-11-23 op if (*port == NULL)
1676 14ae3944 2022-11-23 op *port = "1337";
1677 14ae3944 2022-11-23 op
1678 14ae3944 2022-11-23 op return host;
1679 14ae3944 2022-11-23 op }
1680 14ae3944 2022-11-23 op
1681 fb1a36c0 2022-01-09 op int
1682 fb1a36c0 2022-01-09 op main(int argc, char **argv)
1683 fb1a36c0 2022-01-09 op {
1684 14ae3944 2022-11-23 op const char *user, *host, *port, *path;
1685 df9bb54d 2022-11-23 op const char *outfile = NULL;
1686 14ae3944 2022-11-23 op int ch;
1687 fb1a36c0 2022-01-09 op
1688 fb1a36c0 2022-01-09 op log_init(1, LOG_DAEMON);
1689 fb1a36c0 2022-01-09 op log_setverbose(0);
1690 fb1a36c0 2022-01-09 op log_procinit(getprogname());
1691 fb1a36c0 2022-01-09 op
1692 df9bb54d 2022-11-23 op while ((ch = getopt(argc, argv, "C:cK:o:")) != -1) {
1693 fb1a36c0 2022-01-09 op switch (ch) {
1694 fb1a36c0 2022-01-09 op case 'C':
1695 ce28299c 2022-01-30 op tls = 1;
1696 fb1a36c0 2022-01-09 op crtpath = optarg;
1697 fb1a36c0 2022-01-09 op break;
1698 ab9f44f7 2022-11-23 op case 'c': /* deprecated, remove after 0.3 */
1699 fb1a36c0 2022-01-09 op tls = 1;
1700 fb1a36c0 2022-01-09 op break;
1701 fb1a36c0 2022-01-09 op case 'K':
1702 ce28299c 2022-01-30 op tls = 1;
1703 fb1a36c0 2022-01-09 op keypath = optarg;
1704 fb1a36c0 2022-01-09 op break;
1705 df9bb54d 2022-11-23 op case 'o':
1706 df9bb54d 2022-11-23 op outfile = optarg;
1707 df9bb54d 2022-11-23 op break;
1708 fb1a36c0 2022-01-09 op default:
1709 fb1a36c0 2022-01-09 op usage(1);
1710 fb1a36c0 2022-01-09 op }
1711 fb1a36c0 2022-01-09 op }
1712 fb1a36c0 2022-01-09 op argc -= optind;
1713 fb1a36c0 2022-01-09 op argv += optind;
1714 fb1a36c0 2022-01-09 op
1715 3847d69e 2022-01-30 op if (argc == 0 || (tls && crtpath == NULL))
1716 fb1a36c0 2022-01-09 op usage(1);
1717 fb1a36c0 2022-01-09 op
1718 14ae3944 2022-11-23 op host = parse_addr(argv[0], &user, &port, &path);
1719 514e8d43 2022-11-23 op if (path == NULL && argv[1] != NULL) /* drop argv[1] after 0.3 */
1720 14ae3944 2022-11-23 op path = argv[1];
1721 df9bb54d 2022-11-23 op if (outfile && path == NULL)
1722 df9bb54d 2022-11-23 op usage(1);
1723 14ae3944 2022-11-23 op
1724 1c9ab7cf 2022-01-29 op signal(SIGPIPE, SIG_IGN);
1725 fb1a36c0 2022-01-09 op if (isatty(1)) {
1726 fb1a36c0 2022-01-09 op tty_p = 1;
1727 fb1a36c0 2022-01-09 op resized = 1;
1728 fb1a36c0 2022-01-09 op signal(SIGWINCH, tty_resized);
1729 fb1a36c0 2022-01-09 op }
1730 fb1a36c0 2022-01-09 op
1731 fb1a36c0 2022-01-09 op if ((evb = evbuffer_new()) == NULL)
1732 fb1a36c0 2022-01-09 op fatal("evbuffer_new");
1733 fb1a36c0 2022-01-09 op
1734 fb1a36c0 2022-01-09 op if ((buf = evbuffer_new()) == NULL)
1735 fb1a36c0 2022-01-09 op fatal("evbuffer_new");
1736 fb1a36c0 2022-01-09 op
1737 fb1a36c0 2022-01-09 op if ((dirbuf = evbuffer_new()) == NULL)
1738 fb1a36c0 2022-01-09 op fatal("evbuferr_new");
1739 fb1a36c0 2022-01-09 op
1740 14ae3944 2022-11-23 op do_connect(host, port, user);
1741 14ae3944 2022-11-23 op if (path)
1742 df9bb54d 2022-11-23 op cd_or_fetch(path, outfile);
1743 fb1a36c0 2022-01-09 op
1744 fb1a36c0 2022-01-09 op for (;;) {
1745 fb1a36c0 2022-01-09 op int argc = 0;
1746 fb1a36c0 2022-01-09 op char *line, *argv[16] = {0}, **ap;
1747 fb1a36c0 2022-01-09 op
1748 fb1a36c0 2022-01-09 op if ((line = read_line("kamiftp> ")) == NULL)
1749 fb1a36c0 2022-01-09 op break;
1750 fb1a36c0 2022-01-09 op
1751 fb1a36c0 2022-01-09 op for (argc = 0, ap = argv; ap < &argv[15] &&
1752 fb1a36c0 2022-01-09 op (*ap = strsep(&line, " \t")) != NULL;) {
1753 fb1a36c0 2022-01-09 op if (**ap != '\0')
1754 fb1a36c0 2022-01-09 op ap++, argc++;
1755 fb1a36c0 2022-01-09 op }
1756 fb1a36c0 2022-01-09 op excmd(argc, (const char **)argv);
1757 fb1a36c0 2022-01-09 op
1758 ca14d066 2022-11-23 op if (bell)
1759 ca14d066 2022-11-23 op fprintf(stderr, "\a");
1760 fb1a36c0 2022-01-09 op
1761 fb1a36c0 2022-01-09 op free(line);
1762 fb1a36c0 2022-01-09 op }
1763 fb1a36c0 2022-01-09 op
1764 fb1a36c0 2022-01-09 op printf("\n");
1765 8f7253b2 2022-11-22 op fclose(fp);
1766 fb1a36c0 2022-01-09 op }