2 dc8c1791 2022-01-10 op * Copyright (c) 2021, 2022 Omar Polo <op@omarpolo.com>
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.
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.
17 bbcba3ed 2022-01-10 op #include "compat.h"
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>
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>
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>
45 fb1a36c0 2022-01-09 op #include "kami.h"
46 fb1a36c0 2022-01-09 op #include "utils.h"
47 fb1a36c0 2022-01-09 op #include "log.h"
48 c37e1cfe 2022-01-17 op #include "9pclib.h"
50 d3e1ab0c 2022-11-24 op #include "kamiftp.h"
52 64d8db89 2022-01-16 op #define TMPFSTR "/tmp/kamiftp.XXXXXXXXXX"
53 64d8db89 2022-01-16 op #define TMPFSTRLEN sizeof(TMPFSTR)
57 fb1a36c0 2022-01-09 op const char *crtpath;
58 fb1a36c0 2022-01-09 op const char *keypath;
62 fb1a36c0 2022-01-09 op struct evbuffer *buf;
63 fb1a36c0 2022-01-09 op struct evbuffer *dirbuf;
64 fb1a36c0 2022-01-09 op uint32_t msize;
68 fb1a36c0 2022-01-09 op volatile sig_atomic_t resized;
70 fb1a36c0 2022-01-09 op int tty_width;
73 fb1a36c0 2022-01-09 op struct progress {
75 fb1a36c0 2022-01-09 op uint64_t done;
80 fb1a36c0 2022-01-09 op #define ASSERT_EMPTYBUF() assert(EVBUFFER_LENGTH(buf) == 0)
83 fb1a36c0 2022-01-09 op read_line(const char *prompt)
88 fb1a36c0 2022-01-09 op if ((line = readline(prompt)) == NULL)
90 fb1a36c0 2022-01-09 op /* XXX: trim spaces? */
91 fb1a36c0 2022-01-09 op if (*line == '\0') {
96 fb1a36c0 2022-01-09 op add_history(line);
101 9e7dd230 2022-01-12 op spawn(const char *argv0, ...)
106 9e7dd230 2022-01-12 op const char *argv[16], *last;
109 9e7dd230 2022-01-12 op memset(argv, 0, sizeof(argv));
111 9e7dd230 2022-01-12 op va_start(ap, argv0);
112 9e7dd230 2022-01-12 op argv[0] = argv0;
113 9e7dd230 2022-01-12 op for (i = 1; i < nitems(argv); ++i) {
114 9e7dd230 2022-01-12 op last = va_arg(ap, const char *);
115 9e7dd230 2022-01-12 op if (last == NULL)
117 9e7dd230 2022-01-12 op argv[i] = last;
121 9e7dd230 2022-01-12 op assert(last == NULL);
123 9e7dd230 2022-01-12 op switch (pid = fork()) {
125 9e7dd230 2022-01-12 op err(1, "fork");
126 9e7dd230 2022-01-12 op case 0: /* child */
127 9e7dd230 2022-01-12 op execvp(argv[0], (char *const *)argv);
128 9e7dd230 2022-01-12 op err(1, "execvp");
130 9e7dd230 2022-01-12 op waitpid(pid, &status, 0);
135 8f7253b2 2022-11-22 op stdio_tls_write(void *arg, const char *buf, int len)
137 8f7253b2 2022-11-22 op struct tls *ctx = arg;
141 8f7253b2 2022-11-22 op ret = tls_write(ctx, buf, len);
142 8f7253b2 2022-11-22 op } while (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT);
144 8f7253b2 2022-11-22 op if (ret == -1)
145 8f7253b2 2022-11-22 op warn("tls_write: %s", tls_error(ctx));
151 8f7253b2 2022-11-22 op stdio_tls_read(void *arg, char *buf, int len)
153 8f7253b2 2022-11-22 op struct tls *ctx = arg;
157 8f7253b2 2022-11-22 op ret = tls_read(ctx, buf, len);
158 8f7253b2 2022-11-22 op } while (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT);
160 8f7253b2 2022-11-22 op if (ret == -1)
161 8f7253b2 2022-11-22 op warn("tls_read: %s", tls_error(ctx));
167 8f7253b2 2022-11-22 op stdio_tls_close(void *arg)
169 8f7253b2 2022-11-22 op struct tls *ctx = arg;
173 8f7253b2 2022-11-22 op ret = tls_close(ctx);
174 8f7253b2 2022-11-22 op } while (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT);
180 fb1a36c0 2022-01-09 op tty_resized(int signo)
185 dd647997 2022-11-23 op static __dead void
186 fb1a36c0 2022-01-09 op usage(int ret)
188 ab9f44f7 2022-11-23 op fprintf(stderr, "usage: %s [-C cert] [-K key] [-o output] "
189 9f134f93 2022-11-23 op "[9p://][user@]host[:port][/path]\n", getprogname());
190 fb1a36c0 2022-01-09 op fprintf(stderr, "kamid suite version " KAMID_VERSION "\n");
195 8d8fb849 2022-01-17 op nextfid(void)
199 8d8fb849 2022-01-17 op for (i = 0; ; ++i) {
200 8d8fb849 2022-01-17 op if (i != pwdfid)
206 fb1a36c0 2022-01-09 op do_send(void)
211 d33c93c4 2022-01-17 op hexdump("outgoing message", EVBUFFER_DATA(evb),
212 5565d021 2022-01-16 op EVBUFFER_LENGTH(evb));
214 fb1a36c0 2022-01-09 op while (EVBUFFER_LENGTH(evb) != 0) {
215 8f7253b2 2022-11-22 op r = fwrite(EVBUFFER_DATA(evb), 1, EVBUFFER_LENGTH(evb), fp);
217 8f7253b2 2022-11-22 op fatalx("unexpected EOF");
218 fb1a36c0 2022-01-09 op evbuffer_drain(evb, r);
223 fb1a36c0 2022-01-09 op mustread(void *d, size_t len)
227 8f7253b2 2022-11-22 op r = fread(d, 1, len, fp);
228 8f7253b2 2022-11-22 op if (r != len)
229 8f7253b2 2022-11-22 op errx(1, "unexpected EOF");
233 fb1a36c0 2022-01-09 op recv_msg(void)
236 fb1a36c0 2022-01-09 op uint32_t len, l;
237 fb1a36c0 2022-01-09 op char tmp[BUFSIZ];
239 8f7253b2 2022-11-22 op r = fread(&len, 1, sizeof(len), fp);
240 8f7253b2 2022-11-22 op if (r != sizeof(len))
241 8f7253b2 2022-11-22 op errx(1, "unexpected EOF");
243 fb1a36c0 2022-01-09 op len = le32toh(len);
244 fb1a36c0 2022-01-09 op if (len < HEADERSIZE)
245 fb1a36c0 2022-01-09 op errx(1, "read message of invalid length %d", len);
247 fb1a36c0 2022-01-09 op len -= 4; /* skip the length just read */
249 fb1a36c0 2022-01-09 op while (len != 0) {
250 fb1a36c0 2022-01-09 op l = MIN(len, sizeof(tmp));
252 8f7253b2 2022-11-22 op r = fread(tmp, 1, l, fp);
254 8f7253b2 2022-11-22 op errx(1, "unexpected EOF");
256 8f7253b2 2022-11-22 op evbuffer_add(buf, tmp, r);
260 5565d021 2022-01-16 op hexdump("incoming packet", EVBUFFER_DATA(buf),
261 5565d021 2022-01-16 op EVBUFFER_LENGTH(buf));
264 fb1a36c0 2022-01-09 op static uint64_t
265 fb1a36c0 2022-01-09 op np_read64(struct evbuffer *buf)
269 fb1a36c0 2022-01-09 op evbuffer_remove(buf, &n, sizeof(n));
270 fb1a36c0 2022-01-09 op return le64toh(n);
273 fb1a36c0 2022-01-09 op static uint32_t
274 fb1a36c0 2022-01-09 op np_read32(struct evbuffer *buf)
278 fb1a36c0 2022-01-09 op evbuffer_remove(buf, &n, sizeof(n));
279 fb1a36c0 2022-01-09 op return le32toh(n);
282 fb1a36c0 2022-01-09 op static uint16_t
283 fb1a36c0 2022-01-09 op np_read16(struct evbuffer *buf)
287 fb1a36c0 2022-01-09 op evbuffer_remove(buf, &n, sizeof(n));
288 fb1a36c0 2022-01-09 op return le16toh(n);
291 fb1a36c0 2022-01-09 op static uint16_t
292 fb1a36c0 2022-01-09 op np_read8(struct evbuffer *buf)
296 fb1a36c0 2022-01-09 op evbuffer_remove(buf, &n, sizeof(n));
300 fb1a36c0 2022-01-09 op static char *
301 fb1a36c0 2022-01-09 op np_readstr(struct evbuffer *buf)
303 fb1a36c0 2022-01-09 op uint16_t len;
306 fb1a36c0 2022-01-09 op len = np_read16(buf);
307 fb1a36c0 2022-01-09 op assert(EVBUFFER_LENGTH(buf) >= len);
309 fb1a36c0 2022-01-09 op if ((str = calloc(1, len+1)) == NULL)
310 fb1a36c0 2022-01-09 op err(1, "calloc");
311 fb1a36c0 2022-01-09 op evbuffer_remove(buf, str, len);
316 fb1a36c0 2022-01-09 op np_read_qid(struct evbuffer *buf, struct qid *qid)
318 fb1a36c0 2022-01-09 op assert(EVBUFFER_LENGTH(buf) >= QIDSIZE);
320 fb1a36c0 2022-01-09 op qid->type = np_read8(buf);
321 fb1a36c0 2022-01-09 op qid->vers = np_read32(buf);
322 fb1a36c0 2022-01-09 op qid->path = np_read64(buf);
326 fb1a36c0 2022-01-09 op np_read_stat(struct evbuffer *buf, struct np_stat *st)
328 fb1a36c0 2022-01-09 op uint16_t size;
330 fb1a36c0 2022-01-09 op memset(st, 0, sizeof(*st));
332 fb1a36c0 2022-01-09 op size = np_read16(buf);
333 fb1a36c0 2022-01-09 op if (size > EVBUFFER_LENGTH(buf))
336 fb1a36c0 2022-01-09 op st->type = np_read16(buf);
337 fb1a36c0 2022-01-09 op st->dev = np_read32(buf);
338 fb1a36c0 2022-01-09 op np_read_qid(buf, &st->qid);
339 fb1a36c0 2022-01-09 op st->mode = np_read32(buf);
340 fb1a36c0 2022-01-09 op st->atime = np_read32(buf);
341 fb1a36c0 2022-01-09 op st->mtime = np_read32(buf);
342 fb1a36c0 2022-01-09 op st->length = np_read64(buf);
343 fb1a36c0 2022-01-09 op st->name = np_readstr(buf);
344 fb1a36c0 2022-01-09 op st->uid = np_readstr(buf);
345 fb1a36c0 2022-01-09 op st->gid = np_readstr(buf);
346 fb1a36c0 2022-01-09 op st->muid = np_readstr(buf);
352 fb1a36c0 2022-01-09 op expect(uint8_t type)
356 fb1a36c0 2022-01-09 op t = np_read8(buf);
357 fb1a36c0 2022-01-09 op if (t == type)
360 fb1a36c0 2022-01-09 op if (t == Rerror) {
363 fb1a36c0 2022-01-09 op /* skip tag */
364 fb1a36c0 2022-01-09 op np_read16(buf);
366 fb1a36c0 2022-01-09 op err = np_readstr(buf);
367 fb1a36c0 2022-01-09 op errx(1, "expected %s, got error %s",
368 fb1a36c0 2022-01-09 op pp_msg_type(type), err);
371 fb1a36c0 2022-01-09 op errx(1, "expected %s, got msg type %s",
372 fb1a36c0 2022-01-09 op pp_msg_type(type), pp_msg_type(t));
376 fb1a36c0 2022-01-09 op expect2(uint8_t type, uint16_t tag)
380 fb1a36c0 2022-01-09 op expect(type);
382 fb1a36c0 2022-01-09 op t = np_read16(buf);
383 fb1a36c0 2022-01-09 op if (t == tag)
386 fb1a36c0 2022-01-09 op errx(1, "expected tag 0x%x, got 0x%x", tag, t);
389 8f9f99f7 2022-01-16 op static char *
390 8f9f99f7 2022-01-16 op check(uint8_t type, uint16_t tag)
392 8f9f99f7 2022-01-16 op uint16_t rtag;
393 8f9f99f7 2022-01-16 op uint8_t rtype;
395 8f9f99f7 2022-01-16 op rtype = np_read8(buf);
396 8f9f99f7 2022-01-16 op rtag = np_read16(buf);
397 8f9f99f7 2022-01-16 op if (rtype == type) {
398 8f9f99f7 2022-01-16 op if (rtag != tag)
399 8f9f99f7 2022-01-16 op errx(1, "expected tag 0x%x, got 0x%x", tag, rtag);
403 8f9f99f7 2022-01-16 op if (rtype == Rerror)
404 8f9f99f7 2022-01-16 op return np_readstr(buf);
406 8f9f99f7 2022-01-16 op errx(1, "expected %s, got msg type %s",
407 8f9f99f7 2022-01-16 op pp_msg_type(type), pp_msg_type(rtype));
411 fb1a36c0 2022-01-09 op do_version(void)
413 fb1a36c0 2022-01-09 op char *version;
415 fb1a36c0 2022-01-09 op tversion(VERSION9P, MSIZE9P);
418 fb1a36c0 2022-01-09 op expect2(Rversion, NOTAG);
420 fb1a36c0 2022-01-09 op msize = np_read32(buf);
421 fb1a36c0 2022-01-09 op version = np_readstr(buf);
423 315eafe8 2022-05-23 op if (msize > MSIZE9P || msize < 256)
424 fb1a36c0 2022-01-09 op errx(1, "got unexpected msize: %d", msize);
425 fb1a36c0 2022-01-09 op if (strcmp(version, VERSION9P))
426 fb1a36c0 2022-01-09 op errx(1, "unexpected 9p version: %s", version);
428 fb1a36c0 2022-01-09 op free(version);
429 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
433 14ae3944 2022-11-23 op do_attach(const char *user)
435 fb1a36c0 2022-01-09 op struct qid qid;
437 2c50e0f6 2022-11-23 op tattach(pwdfid, NOFID, user, "/");
440 fb1a36c0 2022-01-09 op expect2(Rattach, iota_tag);
441 fb1a36c0 2022-01-09 op np_read_qid(buf, &qid);
443 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
446 fb1a36c0 2022-01-09 op static uint32_t
447 fb1a36c0 2022-01-09 op do_open(uint32_t fid, uint8_t mode)
449 fb1a36c0 2022-01-09 op struct qid qid;
450 fb1a36c0 2022-01-09 op uint32_t iounit;
452 fb1a36c0 2022-01-09 op topen(fid, mode);
455 fb1a36c0 2022-01-09 op expect2(Ropen, iota_tag);
457 01fb39ab 2022-01-16 op np_read_qid(buf, &qid);
458 01fb39ab 2022-01-16 op iounit = np_read32(buf);
460 01fb39ab 2022-01-16 op ASSERT_EMPTYBUF();
462 01fb39ab 2022-01-16 op return iounit;
465 01fb39ab 2022-01-16 op static uint32_t
466 01fb39ab 2022-01-16 op do_create(uint32_t fid, const char *name, uint32_t perm, uint8_t mode)
468 01fb39ab 2022-01-16 op struct qid qid;
469 01fb39ab 2022-01-16 op uint32_t iounit;
471 01fb39ab 2022-01-16 op tcreate(fid, name, perm, mode);
474 01fb39ab 2022-01-16 op expect2(Rcreate, iota_tag);
476 fb1a36c0 2022-01-09 op np_read_qid(buf, &qid);
477 fb1a36c0 2022-01-09 op iounit = np_read32(buf);
479 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
481 fb1a36c0 2022-01-09 op return iounit;
485 fb1a36c0 2022-01-09 op do_clunk(uint32_t fid)
490 fb1a36c0 2022-01-09 op expect2(Rclunk, iota_tag);
492 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
495 1bb1bfca 2022-01-16 op static char *
496 fb1a36c0 2022-01-09 op dup_fid(int fid, int nfid)
498 fb1a36c0 2022-01-09 op uint16_t nwqid;
499 1bb1bfca 2022-01-16 op char *errstr;
501 fb1a36c0 2022-01-09 op twalk(fid, nfid, NULL, 0);
505 1bb1bfca 2022-01-16 op if ((errstr = check(Rwalk, iota_tag)) != NULL)
506 1bb1bfca 2022-01-16 op return errstr;
508 fb1a36c0 2022-01-09 op nwqid = np_read16(buf);
509 fb1a36c0 2022-01-09 op assert(nwqid == 0);
511 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
516 8f9f99f7 2022-01-16 op static char *
517 8f9f99f7 2022-01-16 op walk_path(int fid, int newfid, const char *path, int *missing,
518 8f9f99f7 2022-01-16 op struct qid *qid)
520 8f9f99f7 2022-01-16 op char *wnames[MAXWELEM], *p, *t, *errstr;
521 fb1a36c0 2022-01-09 op size_t nwname, i;
522 fb1a36c0 2022-01-09 op uint16_t nwqid;
524 fb1a36c0 2022-01-09 op if ((p = strdup(path)) == NULL)
525 fb1a36c0 2022-01-09 op err(1, "strdup");
528 fb1a36c0 2022-01-09 op /* strip initial ./ */
529 fb1a36c0 2022-01-09 op if (t[0] == '.' && t[1] == '/')
532 fb1a36c0 2022-01-09 op for (nwname = 0; nwname < nitems(wnames) &&
533 fb1a36c0 2022-01-09 op (wnames[nwname] = strsep(&t, "/")) != NULL;) {
534 fb1a36c0 2022-01-09 op if (*wnames[nwname] != '\0')
538 fb1a36c0 2022-01-09 op twalk(fid, newfid, (const char **)wnames, nwname);
542 8f9f99f7 2022-01-16 op *missing = nwname;
543 5fe03b5c 2022-01-21 op if ((errstr = check(Rwalk, iota_tag)) != NULL) {
545 8f9f99f7 2022-01-16 op return errstr;
548 fb1a36c0 2022-01-09 op nwqid = np_read16(buf);
549 fb1a36c0 2022-01-09 op assert(nwqid <= nwname);
551 fb1a36c0 2022-01-09 op /* consume all qids */
552 43d117b1 2022-01-16 op for (i = 0; i < nwqid; ++i)
553 fb1a36c0 2022-01-09 op np_read_qid(buf, qid);
557 8f9f99f7 2022-01-16 op *missing = nwname - nwqid;
562 fb1a36c0 2022-01-09 op do_stat(int fid, struct np_stat *st)
567 fb1a36c0 2022-01-09 op expect2(Rstat, iota_tag);
569 caa64af1 2022-02-02 op /* eat up the first two byte length */
570 caa64af1 2022-02-02 op np_read16(buf);
572 fb1a36c0 2022-01-09 op if (np_read_stat(buf, st) == -1)
573 fb1a36c0 2022-01-09 op errx(1, "invalid stat struct read");
575 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
578 c37e1cfe 2022-01-17 op static char *
579 c37e1cfe 2022-01-17 op do_wstat(int fid, const struct np_stat *st)
581 c37e1cfe 2022-01-17 op char *errstr;
583 c37e1cfe 2022-01-17 op twstat(fid, st);
587 c37e1cfe 2022-01-17 op if ((errstr = check(Rwstat, iota_tag)) != NULL)
588 c37e1cfe 2022-01-17 op return errstr;
590 c37e1cfe 2022-01-17 op ASSERT_EMPTYBUF();
595 f99c17f9 2022-11-23 op static char *
596 f99c17f9 2022-11-23 op do_remove(int fid)
598 f99c17f9 2022-11-23 op char *errstr;
600 f99c17f9 2022-11-23 op tremove(fid);
603 f99c17f9 2022-11-23 op if ((errstr = check(Rremove, iota_tag)) != NULL)
604 f99c17f9 2022-11-23 op return errstr;
606 f99c17f9 2022-11-23 op ASSERT_EMPTYBUF();
611 fb1a36c0 2022-01-09 op static size_t
612 fb1a36c0 2022-01-09 op do_read(int fid, uint64_t off, uint32_t count, void *data)
616 fb1a36c0 2022-01-09 op tread(fid, off, count);
619 fb1a36c0 2022-01-09 op expect2(Rread, iota_tag);
621 fb1a36c0 2022-01-09 op r = np_read32(buf);
622 fb1a36c0 2022-01-09 op assert(r == EVBUFFER_LENGTH(buf));
623 fb1a36c0 2022-01-09 op assert(r <= count);
624 fb1a36c0 2022-01-09 op evbuffer_remove(buf, data, r);
626 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
631 01fb39ab 2022-01-16 op static size_t
632 01fb39ab 2022-01-16 op do_write(int fid, uint64_t off, uint32_t count, void *data)
636 01fb39ab 2022-01-16 op twrite(fid, off, data, count);
639 01fb39ab 2022-01-16 op expect2(Rwrite, iota_tag);
641 01fb39ab 2022-01-16 op r = np_read32(buf);
642 01fb39ab 2022-01-16 op assert(r <= count);
644 01fb39ab 2022-01-16 op ASSERT_EMPTYBUF();
650 fb1a36c0 2022-01-09 op draw_progress(const char *pre, const struct progress *p)
652 fb1a36c0 2022-01-09 op struct winsize ws;
659 fb1a36c0 2022-01-09 op perc = 100.0 * p->done / p->max;
660 fb1a36c0 2022-01-09 op if (!tty_p) {
661 fb1a36c0 2022-01-09 op fprintf(stderr, "%s: %d%%\n", pre, (int)perc);
665 fb1a36c0 2022-01-09 op if (resized) {
668 fb1a36c0 2022-01-09 op if (ioctl(0, TIOCGWINSZ, &ws) == -1)
670 fb1a36c0 2022-01-09 op tty_width = ws.ws_col;
672 fb1a36c0 2022-01-09 op w = tty_width;
674 fb1a36c0 2022-01-09 op if (pre == NULL ||
675 5d0dedb3 2022-11-23 op ((l = fprintf(stderr, "\r%s ", pre)) == -1 || l >= w))
678 fb1a36c0 2022-01-09 op w -= l + 2 + 5; /* 2 for |, 5 for percentage + \n */
680 5d0dedb3 2022-11-23 op fprintf(stderr, "%4d%%\n", (int)perc);
684 5d0dedb3 2022-11-23 op fprintf(stderr, "|");
686 fb1a36c0 2022-01-09 op l = w * MIN(100.0, perc) / 100.0;
687 fb1a36c0 2022-01-09 op for (i = 0; i < l; i++)
688 5d0dedb3 2022-11-23 op fprintf(stderr, "*");
689 fb1a36c0 2022-01-09 op for (; i < w; i++)
690 5d0dedb3 2022-11-23 op fprintf(stderr, " ");
691 5d0dedb3 2022-11-23 op fprintf(stderr, "|%4d%%", (int)perc);
695 76521d26 2022-01-16 op fetch_fid(int fid, int fd, const char *name)
697 02d5b425 2022-05-23 op static char buf[MSIZE9P];
698 fb1a36c0 2022-01-09 op struct progress p = {0};
699 fb1a36c0 2022-01-09 op struct np_stat st;
703 fb1a36c0 2022-01-09 op do_stat(fid, &st);
704 fb1a36c0 2022-01-09 op do_open(fid, KOREAD);
706 fb1a36c0 2022-01-09 op p.max = st.length;
708 02d5b425 2022-05-23 op size_t len, off;
711 02d5b425 2022-05-23 op len = MIN(sizeof(buf), msize);
712 7bafb2fd 2022-08-28 op len -= IOHDRSZ; /* for the request' fields */
714 02d5b425 2022-05-23 op r = do_read(fid, p.done, len, buf);
718 b2263c45 2022-01-16 op for (off = 0; off < r; off += nw)
719 b2263c45 2022-01-16 op if ((nw = write(fd, buf + off, r - off)) == 0 ||
726 9e7dd230 2022-01-12 op draw_progress(name, &p);
729 fb1a36c0 2022-01-09 op /* throttle, for debugging purpose */
731 fb1a36c0 2022-01-09 op struct timespec ts = { 0, 500000000 };
732 fb1a36c0 2022-01-09 op nanosleep(&ts, NULL);
738 fb1a36c0 2022-01-09 op putchar('\n');
740 01fb39ab 2022-01-16 op do_clunk(fid);
745 16f1f3bf 2022-01-21 op send_fid(int fid, const char *fnam, int open_flags, int fd, const char *name)
747 02d5b425 2022-05-23 op static char buf[MSIZE9P];
748 01fb39ab 2022-01-16 op struct progress p = {0};
749 01fb39ab 2022-01-16 op struct stat sb;
751 02d5b425 2022-05-23 op size_t w, len;
753 01fb39ab 2022-01-16 op if (fstat(fd, &sb) == -1)
754 01fb39ab 2022-01-16 op err(1, "fstat");
756 01fb39ab 2022-01-16 op if (fnam != NULL)
757 01fb39ab 2022-01-16 op do_create(fid, fnam, 0644, KOWRITE);
759 16f1f3bf 2022-01-21 op do_open(fid, open_flags | KOWRITE);
761 01fb39ab 2022-01-16 op p.max = sb.st_size;
763 02d5b425 2022-05-23 op len = MIN(sizeof(buf), msize);
764 02d5b425 2022-05-23 op len -= HEADERSIZE + 4 + 4 + 8; /* for the request' fields */
766 02d5b425 2022-05-23 op r = read(fd, buf, len);
770 01fb39ab 2022-01-16 op err(1, "read");
772 01fb39ab 2022-01-16 op w = do_write(fid, p.done, r, buf);
775 01fb39ab 2022-01-16 op draw_progress(name, &p);
778 01fb39ab 2022-01-16 op /* throttle, for debugging purpose */
780 01fb39ab 2022-01-16 op struct timespec ts = { 0, 500000000 };
781 01fb39ab 2022-01-16 op nanosleep(&ts, NULL);
786 01fb39ab 2022-01-16 op putchar('\n');
787 fb1a36c0 2022-01-09 op do_clunk(fid);
791 9c48f915 2022-01-16 op woc_file(int fd, const char *prompt, const char *path)
793 9c48f915 2022-01-16 op struct qid qid;
794 9c48f915 2022-01-16 op const char *n = NULL;
795 9c48f915 2022-01-16 op char *errstr;
796 9c48f915 2022-01-16 op int nfid, miss;
798 8d8fb849 2022-01-17 op nfid = nextfid();
799 9c48f915 2022-01-16 op errstr = walk_path(pwdfid, nfid, path, &miss, &qid);
800 9c48f915 2022-01-16 op if (errstr != NULL && miss > 1) {
801 5d0dedb3 2022-11-23 op fprintf(stderr, "%s: %s\n", path, errstr);
802 9c48f915 2022-01-16 op free(errstr);
806 9c48f915 2022-01-16 op if (errstr != NULL || miss == 1) {
807 9c48f915 2022-01-16 op char p[PATH_MAX], *dn;
810 9c48f915 2022-01-16 op * If it's only one component missing (the file name), walk
811 9c48f915 2022-01-16 op * to the parent directory and try to create the file.
814 9c48f915 2022-01-16 op if (strlcpy(p, path, sizeof(p)) >= sizeof(p)) {
815 5d0dedb3 2022-11-23 op fprintf(stderr, "path too long: %s\n", path);
818 9c48f915 2022-01-16 op dn = dirname(p);
820 9c48f915 2022-01-16 op if (!strcmp(dn, ".")) {
821 9c48f915 2022-01-16 op errstr = dup_fid(pwdfid, nfid);
824 9c48f915 2022-01-16 op errstr = walk_path(pwdfid, nfid, dn, &miss, &qid);
826 9c48f915 2022-01-16 op if (errstr != NULL) {
827 5d0dedb3 2022-11-23 op fprintf(stderr, "%s: %s\n", dn, errstr);
828 9c48f915 2022-01-16 op free(errstr);
832 9c48f915 2022-01-16 op if (miss != 0) {
833 5d0dedb3 2022-11-23 op fprintf(stderr, "%s: not a directory\n", dn);
837 9c48f915 2022-01-16 op if ((n = strrchr(path, '/')) != NULL)
843 9c48f915 2022-01-16 op free(errstr);
845 9c48f915 2022-01-16 op if (miss > 1) {
846 5d0dedb3 2022-11-23 op fprintf(stderr, "can't create %s: missing %d path"
847 5d0dedb3 2022-11-23 op " component(s)\n", path, miss);
851 16f1f3bf 2022-01-21 op send_fid(nfid, n, KOTRUNC, fd, prompt);
856 8f7253b2 2022-11-22 op dial(const char *host, const char *port)
858 fb1a36c0 2022-01-09 op struct addrinfo hints, *res, *res0;
859 8f7253b2 2022-11-22 op int sock, error, saved_errno;
860 fb1a36c0 2022-01-09 op const char *cause = NULL;
862 fb1a36c0 2022-01-09 op memset(&hints, 0, sizeof(hints));
863 fb1a36c0 2022-01-09 op hints.ai_family = AF_UNSPEC;
864 fb1a36c0 2022-01-09 op hints.ai_socktype = SOCK_STREAM;
865 fb1a36c0 2022-01-09 op error = getaddrinfo(host, port, &hints, &res0);
867 fb1a36c0 2022-01-09 op errx(1, "%s", gai_strerror(error));
870 fb1a36c0 2022-01-09 op for (res = res0; res != NULL; res = res->ai_next) {
871 db066f1e 2022-01-12 op sock = socket(res->ai_family, res->ai_socktype|SOCK_CLOEXEC,
872 fb1a36c0 2022-01-09 op res->ai_protocol);
873 fb1a36c0 2022-01-09 op if (sock == -1) {
874 fb1a36c0 2022-01-09 op cause = "socket";
878 fb1a36c0 2022-01-09 op if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
879 fb1a36c0 2022-01-09 op cause = "connect";
880 fb1a36c0 2022-01-09 op saved_errno = errno;
882 fb1a36c0 2022-01-09 op errno = saved_errno;
889 8f7253b2 2022-11-22 op freeaddrinfo(res0);
890 fb1a36c0 2022-01-09 op if (sock == -1)
891 fb1a36c0 2022-01-09 op err(1, "%s", cause);
896 14ae3944 2022-11-23 op do_connect(const char *host, const char *port, const char *user)
898 14ae3944 2022-11-23 op struct qid qid;
899 14ae3944 2022-11-23 op int nfid, miss, fd;
900 14ae3944 2022-11-23 op char *errstr;
902 5d0dedb3 2022-11-23 op fprintf(stderr, "connecting to %s:%s...", host, port);
905 8f7253b2 2022-11-22 op struct tls_config *conf;
906 8f7253b2 2022-11-22 op struct tls *ctx;
909 8f7253b2 2022-11-22 op if ((conf = tls_config_new()) == NULL)
910 8f7253b2 2022-11-22 op fatalx("failed to create TLS config");
911 8f7253b2 2022-11-22 op tls_config_insecure_noverifycert(conf);
912 8f7253b2 2022-11-22 op tls_config_insecure_noverifyname(conf);
914 8f7253b2 2022-11-22 op if (keypath == NULL)
915 8f7253b2 2022-11-22 op keypath = crtpath;
917 8f7253b2 2022-11-22 op if (tls_config_set_keypair_file(conf, crtpath, keypath) == -1)
918 8f7253b2 2022-11-22 op fatalx("failed to load certs: (%s, %s)", crtpath,
921 8f7253b2 2022-11-22 op if ((ctx = tls_client()) == NULL)
922 8f7253b2 2022-11-22 op fatalx("failed to create TLS client");
923 8f7253b2 2022-11-22 op if (tls_configure(ctx, conf) == -1)
924 8f7253b2 2022-11-22 op fatalx("failed to configure TLS client");
925 8f7253b2 2022-11-22 op tls_config_free(conf);
927 8f7253b2 2022-11-22 op if (tls_connect(ctx, host, port) == -1)
928 8f7253b2 2022-11-22 op fatalx("failed to connect to %s:%s: %s", host, port,
929 8f7253b2 2022-11-22 op tls_error(ctx));
932 8f7253b2 2022-11-22 op r = tls_handshake(ctx);
933 8f7253b2 2022-11-22 op } while (r == TLS_WANT_POLLIN || r == TLS_WANT_POLLOUT);
934 8f7253b2 2022-11-22 op fp = funopen(ctx, stdio_tls_read, stdio_tls_write, NULL,
935 8f7253b2 2022-11-22 op stdio_tls_close);
936 8f7253b2 2022-11-22 op if (fp == NULL)
937 8f7253b2 2022-11-22 op fatal("funopen");
941 8f7253b2 2022-11-22 op fd = dial(host, port);
942 8f7253b2 2022-11-22 op if ((fp = fdopen(fd, "r+")) == NULL)
943 8f7253b2 2022-11-22 op fatal("fdopen");
946 5d0dedb3 2022-11-23 op fprintf(stderr, " done!\n");
948 fb1a36c0 2022-01-09 op do_version();
949 14ae3944 2022-11-23 op do_attach(user);
953 64d8db89 2022-01-16 op tmp_file(char sfn[TMPFSTRLEN])
957 64d8db89 2022-01-16 op strlcpy(sfn, TMPFSTR, TMPFSTRLEN);
958 64d8db89 2022-01-16 op if ((tmpfd = mkstemp(sfn)) == -1) {
959 64d8db89 2022-01-16 op warn("mkstemp %s", sfn);
963 aa76fd7f 2022-01-16 op /* set the close-on-exec flag */
964 aa76fd7f 2022-01-16 op if (fcntl(tmpfd, F_SETFD, FD_CLOEXEC) == -1) {
965 aa76fd7f 2022-01-16 op warn("fcntl");
966 aa76fd7f 2022-01-16 op close(tmpfd);
970 64d8db89 2022-01-16 op return tmpfd;
973 1f5fb843 2022-01-16 op static inline const char *
974 1f5fb843 2022-01-16 op pp_perm(uint8_t x)
976 1f5fb843 2022-01-16 op switch (x & 0x7) {
978 1f5fb843 2022-01-16 op return "---";
980 1f5fb843 2022-01-16 op return "--x";
982 1f5fb843 2022-01-16 op return "-w-";
984 1f5fb843 2022-01-16 op return "-wx";
986 1f5fb843 2022-01-16 op return "r--";
988 1f5fb843 2022-01-16 op return "r-x";
990 1f5fb843 2022-01-16 op return "rw-";
992 1f5fb843 2022-01-16 op return "rwx";
994 1f5fb843 2022-01-16 op /* unreachable, just for the compiler' happiness */
995 1f5fb843 2022-01-16 op return "???";
999 c37e1cfe 2022-01-17 op static inline void
1000 c37e1cfe 2022-01-17 op prepare_wstat(struct np_stat *st)
1002 c37e1cfe 2022-01-17 op memset(st, 0xFF, sizeof(*st));
1003 c37e1cfe 2022-01-17 op st->name = NULL;
1004 c37e1cfe 2022-01-17 op st->uid = NULL;
1005 c37e1cfe 2022-01-17 op st->gid = NULL;
1006 c37e1cfe 2022-01-17 op st->muid = NULL;
1010 d3e1ab0c 2022-11-24 op print_dirent(const struct np_stat *st)
1012 d3e1ab0c 2022-11-24 op time_t mtime;
1013 d3e1ab0c 2022-11-24 op struct tm *tm;
1014 d3e1ab0c 2022-11-24 op const char *timfmt;
1015 d3e1ab0c 2022-11-24 op char fmt[FMT_SCALED_STRSIZE], tim[13];
1017 d3e1ab0c 2022-11-24 op if (fmt_scaled(st->length, fmt) == -1)
1018 d3e1ab0c 2022-11-24 op strlcpy(fmt, "xxx", sizeof(fmt));
1020 d3e1ab0c 2022-11-24 op mtime = st->mtime;
1022 d3e1ab0c 2022-11-24 op if (now > mtime && (now - mtime) < 365/2 * 24 * 12 * 60)
1023 d3e1ab0c 2022-11-24 op timfmt = "%b %e %R";
1025 d3e1ab0c 2022-11-24 op timfmt = "%b %e %Y";
1027 d3e1ab0c 2022-11-24 op if ((tm = localtime(&mtime)) == NULL ||
1028 d3e1ab0c 2022-11-24 op strftime(tim, sizeof(tim), timfmt, tm) == 0)
1029 d3e1ab0c 2022-11-24 op strlcpy(tim, "unknown", sizeof(tim));
1031 d3e1ab0c 2022-11-24 op if (st->qid.type & QTDIR)
1032 d3e1ab0c 2022-11-24 op printf("d");
1034 d3e1ab0c 2022-11-24 op printf("-");
1035 d3e1ab0c 2022-11-24 op printf("%s", pp_perm(st->mode >> 6));
1036 d3e1ab0c 2022-11-24 op printf("%s", pp_perm(st->mode >> 3));
1037 d3e1ab0c 2022-11-24 op printf("%s", pp_perm(st->mode));
1038 d3e1ab0c 2022-11-24 op printf(" %8s %12s %s%s\n", fmt, tim, st->name,
1039 d3e1ab0c 2022-11-24 op st->qid.type & QTDIR ? "/" : "");
1045 d3e1ab0c 2022-11-24 op dir_listing(const char *path, int (*fn)(const struct np_stat *),
1046 d3e1ab0c 2022-11-24 op int printerr)
1048 d3e1ab0c 2022-11-24 op struct qid qid;
1049 d3e1ab0c 2022-11-24 op struct np_stat st;
1050 d3e1ab0c 2022-11-24 op uint64_t off = 0;
1051 d3e1ab0c 2022-11-24 op uint32_t len;
1052 d3e1ab0c 2022-11-24 op int nfid, miss, r;
1053 d3e1ab0c 2022-11-24 op char *errstr;
1055 d3e1ab0c 2022-11-24 op now = time(NULL);
1056 d3e1ab0c 2022-11-24 op nfid = nextfid();
1058 d3e1ab0c 2022-11-24 op errstr = walk_path(pwdfid, nfid, path, &miss, &qid);
1059 d3e1ab0c 2022-11-24 op if (errstr != NULL) {
1060 d3e1ab0c 2022-11-24 op if (printerr)
1061 d3e1ab0c 2022-11-24 op printf("%s: %s\n", path, errstr);
1062 d3e1ab0c 2022-11-24 op free(errstr);
1066 d3e1ab0c 2022-11-24 op if (printerr)
1067 d3e1ab0c 2022-11-24 op printf("%s: No such file or directory\n", path);
1070 d3e1ab0c 2022-11-24 op if (!(qid.type & QTDIR)) {
1071 d3e1ab0c 2022-11-24 op if (printerr)
1072 d3e1ab0c 2022-11-24 op printf("%s: not a directory\n", path);
1073 d3e1ab0c 2022-11-24 op do_clunk(nfid);
1077 d3e1ab0c 2022-11-24 op do_open(nfid, KOREAD);
1078 d3e1ab0c 2022-11-24 op evbuffer_drain(dirbuf, EVBUFFER_LENGTH(dirbuf));
1081 d3e1ab0c 2022-11-24 op tread(nfid, off, msize - IOHDRSZ);
1084 d3e1ab0c 2022-11-24 op expect2(Rread, iota_tag);
1086 d3e1ab0c 2022-11-24 op len = np_read32(buf);
1087 d3e1ab0c 2022-11-24 op if (len == 0)
1090 d3e1ab0c 2022-11-24 op evbuffer_add_buffer(dirbuf, buf);
1093 d3e1ab0c 2022-11-24 op ASSERT_EMPTYBUF();
1096 d3e1ab0c 2022-11-24 op while (EVBUFFER_LENGTH(dirbuf) != 0) {
1097 d3e1ab0c 2022-11-24 op if (np_read_stat(dirbuf, &st) == -1)
1098 d3e1ab0c 2022-11-24 op errx(1, "invalid stat struct read");
1100 d3e1ab0c 2022-11-24 op r = fn(&st);
1102 d3e1ab0c 2022-11-24 op free(st.name);
1103 d3e1ab0c 2022-11-24 op free(st.uid);
1104 d3e1ab0c 2022-11-24 op free(st.gid);
1105 d3e1ab0c 2022-11-24 op free(st.muid);
1107 d3e1ab0c 2022-11-24 op if (r == -1)
1111 d3e1ab0c 2022-11-24 op evbuffer_drain(dirbuf, EVBUFFER_LENGTH(dirbuf));
1112 d3e1ab0c 2022-11-24 op do_clunk(nfid);
1117 fb1a36c0 2022-01-09 op cmd_bell(int argc, const char **argv)
1119 fb1a36c0 2022-01-09 op if (argc == 0) {
1120 fb1a36c0 2022-01-09 op bell = !bell;
1122 fb1a36c0 2022-01-09 op puts("bell mode enabled");
1124 fb1a36c0 2022-01-09 op puts("bell mode disabled");
1128 fb1a36c0 2022-01-09 op if (argc != 1)
1131 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "on")) {
1133 fb1a36c0 2022-01-09 op puts("bell mode enabled");
1137 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "off")) {
1139 fb1a36c0 2022-01-09 op puts("bell mode disabled");
1144 fb1a36c0 2022-01-09 op printf("bell [on | off]\n");
1148 fb1a36c0 2022-01-09 op cmd_bye(int argc, const char **argv)
1150 fb1a36c0 2022-01-09 op log_warnx("bye\n");
1156 fb1a36c0 2022-01-09 op cmd_cd(int argc, const char **argv)
1158 fb1a36c0 2022-01-09 op struct qid qid;
1159 8f9f99f7 2022-01-16 op int nfid, miss;
1160 8f9f99f7 2022-01-16 op char *errstr;
1162 fb1a36c0 2022-01-09 op if (argc != 1) {
1163 fb1a36c0 2022-01-09 op printf("usage: cd remote-path\n");
1167 8d8fb849 2022-01-17 op nfid = nextfid();
1168 8f9f99f7 2022-01-16 op errstr = walk_path(pwdfid, nfid, argv[0], &miss, &qid);
1169 8f9f99f7 2022-01-16 op if (errstr != NULL) {
1170 8f9f99f7 2022-01-16 op printf("%s: %s\n", argv[0], errstr);
1171 8f9f99f7 2022-01-16 op free(errstr);
1175 8f9f99f7 2022-01-16 op if (miss != 0 || !(qid.type & QTDIR)) {
1176 8f9f99f7 2022-01-16 op printf("%s: not a directory\n", argv[0]);
1177 fcd9d510 2022-01-16 op if (miss == 0)
1178 fcd9d510 2022-01-16 op do_clunk(nfid);
1182 8f9f99f7 2022-01-16 op do_clunk(pwdfid);
1183 8f9f99f7 2022-01-16 op pwdfid = nfid;
1187 9c48f915 2022-01-16 op cmd_edit(int argc, const char **argv)
1189 9c48f915 2022-01-16 op struct qid qid;
1190 9c48f915 2022-01-16 op int nfid, tmpfd, miss;
1191 9c48f915 2022-01-16 op char sfn[TMPFSTRLEN], p[PATH_MAX], *name, *errstr;
1192 9c48f915 2022-01-16 op const char *ed;
1194 9c48f915 2022-01-16 op if (argc != 1) {
1195 9c48f915 2022-01-16 op puts("usage: edit file");
1199 9c48f915 2022-01-16 op if ((ed = getenv("VISUAL")) == NULL &&
1200 9c48f915 2022-01-16 op (ed = getenv("EDITOR")) == NULL)
1203 8d8fb849 2022-01-17 op nfid = nextfid();
1204 9c48f915 2022-01-16 op errstr = walk_path(pwdfid, nfid, *argv, &miss, &qid);
1205 9c48f915 2022-01-16 op if (errstr != NULL) {
1206 9c48f915 2022-01-16 op printf("%s: %s\n", *argv, errstr);
1207 9c48f915 2022-01-16 op free(errstr);
1211 9c48f915 2022-01-16 op if (miss != 0 || qid.type != 0) {
1212 9c48f915 2022-01-16 op printf("%s: not a file\n", *argv);
1213 9c48f915 2022-01-16 op if (miss == 0)
1214 9c48f915 2022-01-16 op do_clunk(nfid);
1218 9c48f915 2022-01-16 op if ((tmpfd = tmp_file(sfn)) == -1) {
1219 9c48f915 2022-01-16 op do_clunk(nfid);
1223 9c48f915 2022-01-16 op strlcpy(p, *argv, sizeof(p));
1224 9c48f915 2022-01-16 op name = basename(p);
1226 02571aa1 2022-01-29 op if (fetch_fid(nfid, tmpfd, name)) {
1227 02571aa1 2022-01-29 op warn("failed fetch or can't write %s", sfn);
1230 9c48f915 2022-01-16 op close(tmpfd);
1232 9c48f915 2022-01-16 op spawn(ed, sfn, NULL);
1235 9c48f915 2022-01-16 op * Re-open the file because it's not guaranteed that the
1236 9c48f915 2022-01-16 op * file descriptor tmpfd is still associated with the file
1237 9c48f915 2022-01-16 op * pointed by sfn: it's not uncommon for editor to write
1238 9c48f915 2022-01-16 op * a backup file and then rename(2) it to the file name.
1240 9c48f915 2022-01-16 op if ((tmpfd = open(sfn, O_RDONLY)) == -1) {
1241 9c48f915 2022-01-16 op warn("can't open %s", sfn);
1245 9c48f915 2022-01-16 op woc_file(tmpfd, *argv, name);
1246 9c48f915 2022-01-16 op close(tmpfd);
1249 9c48f915 2022-01-16 op unlink(sfn);
1253 fb1a36c0 2022-01-09 op cmd_get(int argc, const char **argv)
1255 fb1a36c0 2022-01-09 op struct qid qid;
1256 fb1a36c0 2022-01-09 op const char *l;
1257 8f9f99f7 2022-01-16 op char *errstr;
1258 8f9f99f7 2022-01-16 op int nfid, fd, miss;
1260 fb1a36c0 2022-01-09 op if (argc != 1 && argc != 2) {
1261 fb1a36c0 2022-01-09 op printf("usage: get remote-file [local-file]\n");
1265 fb1a36c0 2022-01-09 op if (argc == 2)
1266 fb1a36c0 2022-01-09 op l = argv[1];
1267 fb1a36c0 2022-01-09 op else if ((l = strrchr(argv[0], '/')) != NULL)
1268 fb1a36c0 2022-01-09 op l++; /* skip / */
1270 2ba25711 2022-01-16 op l = argv[0];
1272 8d8fb849 2022-01-17 op nfid = nextfid();
1273 8f9f99f7 2022-01-16 op errstr = walk_path(pwdfid, nfid, argv[0], &miss, &qid);
1274 8f9f99f7 2022-01-16 op if (errstr != NULL) {
1275 8f9f99f7 2022-01-16 op printf("%s: %s\n", argv[0], errstr);
1276 8f9f99f7 2022-01-16 op free(errstr);
1280 8f9f99f7 2022-01-16 op if (miss != 0 || qid.type != 0) {
1281 8f9f99f7 2022-01-16 op printf("%s: not a file\n", argv[0]);
1282 fcd9d510 2022-01-16 op if (miss == 0)
1283 fcd9d510 2022-01-16 op do_clunk(nfid);
1287 76521d26 2022-01-16 op if ((fd = open(l, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0644)) == -1) {
1288 76521d26 2022-01-16 op warn("can't open %s", l);
1289 76521d26 2022-01-16 op do_clunk(nfid);
1293 02571aa1 2022-01-29 op if (fetch_fid(nfid, fd, l) == -1)
1294 02571aa1 2022-01-29 op warn("write %s", l);
1299 5565d021 2022-01-16 op cmd_hexdump(int argc, const char **argv)
1301 5565d021 2022-01-16 op if (argc == 0) {
1302 5565d021 2022-01-16 op xdump = !xdump;
1304 5565d021 2022-01-16 op puts("hexdump mode enabled");
1306 5565d021 2022-01-16 op puts("hexdump mode disabled");
1310 5565d021 2022-01-16 op if (argc > 1)
1313 5565d021 2022-01-16 op if (!strcmp(*argv, "on")) {
1315 5565d021 2022-01-16 op puts("hexdump mode enabled");
1319 5565d021 2022-01-16 op if (!strcmp(*argv, "off")) {
1321 5565d021 2022-01-16 op puts("hexdump mode disabled");
1326 5565d021 2022-01-16 op puts("usage: hexdump [on | off]");
1330 fb1a36c0 2022-01-09 op cmd_lcd(int argc, const char **argv)
1332 fb1a36c0 2022-01-09 op const char *dir;
1334 fb1a36c0 2022-01-09 op if (argc > 1) {
1335 d166ffc0 2022-01-16 op printf("usage: lcd [local-directory]\n");
1339 fb1a36c0 2022-01-09 op if (argc == 1)
1340 fb1a36c0 2022-01-09 op dir = *argv;
1342 fb1a36c0 2022-01-09 op if (argc == 0 && (dir = getenv("HOME")) == NULL) {
1343 fb1a36c0 2022-01-09 op printf("HOME is not defined\n");
1347 fb1a36c0 2022-01-09 op if (chdir(dir) == -1)
1348 fb1a36c0 2022-01-09 op printf("cd: %s: %s\n", dir, strerror(errno));
1352 fb1a36c0 2022-01-09 op cmd_lpwd(int argc, const char **argv)
1354 fb1a36c0 2022-01-09 op char path[PATH_MAX];
1356 d166ffc0 2022-01-16 op if (argc != 0) {
1357 d166ffc0 2022-01-16 op printf("usage: lpwd\n");
1361 fb1a36c0 2022-01-09 op if (getcwd(path, sizeof(path)) == NULL) {
1362 fb1a36c0 2022-01-09 op printf("lpwd: %s\n", strerror(errno));
1366 fb1a36c0 2022-01-09 op printf("%s\n", path);
1370 fb1a36c0 2022-01-09 op cmd_ls(int argc, const char **argv)
1372 9c9e60d1 2022-11-23 op if (argc > 1) {
1373 9c9e60d1 2022-11-23 op puts("usage: ls [path]");
1377 d3e1ab0c 2022-11-24 op dir_listing(argc == 0 ? "." : argv[0], print_dirent, 1);
1381 9e7dd230 2022-01-12 op cmd_page(int argc, const char **argv)
1383 9e7dd230 2022-01-12 op struct qid qid;
1384 02571aa1 2022-01-29 op int nfid, tmpfd, miss, r;
1385 64d8db89 2022-01-16 op char sfn[TMPFSTRLEN], p[PATH_MAX], *name, *errstr;
1386 62f663bb 2022-01-16 op const char *pager;
1388 9e7dd230 2022-01-12 op if (argc != 1) {
1389 9e7dd230 2022-01-12 op puts("usage: page file");
1393 62f663bb 2022-01-16 op if ((pager = getenv("PAGER")) == NULL)
1394 62f663bb 2022-01-16 op pager = "less";
1396 8d8fb849 2022-01-17 op nfid = nextfid();
1397 8f9f99f7 2022-01-16 op errstr = walk_path(pwdfid, nfid, *argv, &miss, &qid);
1398 8f9f99f7 2022-01-16 op if (errstr != NULL) {
1399 8f9f99f7 2022-01-16 op printf("%s: %s\n", *argv, errstr);
1400 8f9f99f7 2022-01-16 op free(errstr);
1404 8f9f99f7 2022-01-16 op if (miss != 0 || qid.type != 0) {
1405 8f9f99f7 2022-01-16 op printf("%s: not a file\n", *argv);
1406 fcd9d510 2022-01-16 op if (miss == 0)
1407 fcd9d510 2022-01-16 op do_clunk(nfid);
1411 64d8db89 2022-01-16 op if ((tmpfd = tmp_file(sfn)) == -1) {
1412 9e7dd230 2022-01-12 op do_clunk(nfid);
1416 9e7dd230 2022-01-12 op strlcpy(p, *argv, sizeof(p));
1417 9e7dd230 2022-01-12 op name = basename(p);
1418 02571aa1 2022-01-29 op if ((r = fetch_fid(nfid, tmpfd, name)) == -1)
1419 02571aa1 2022-01-29 op warn("write %s", sfn);
1420 9e7dd230 2022-01-12 op close(tmpfd);
1421 02571aa1 2022-01-29 op if (r != -1)
1422 02571aa1 2022-01-29 op spawn(pager, sfn, NULL);
1423 9e7dd230 2022-01-12 op unlink(sfn);
1427 1c9ab7cf 2022-01-29 op cmd_pipe(int argc, const char **argv)
1429 1c9ab7cf 2022-01-29 op struct qid qid;
1431 1c9ab7cf 2022-01-29 op int nfid, tmpfd, miss, status;
1432 1c9ab7cf 2022-01-29 op int filedes[2]; /* read end, write end */
1433 1c9ab7cf 2022-01-29 op char *errstr;
1435 1c9ab7cf 2022-01-29 op if (argc < 2) {
1436 1c9ab7cf 2022-01-29 op puts("usage: pipe remote-file cmd [args...]");
1440 1c9ab7cf 2022-01-29 op nfid = nextfid();
1441 1c9ab7cf 2022-01-29 op errstr = walk_path(pwdfid, nfid, *argv, &miss, &qid);
1442 1c9ab7cf 2022-01-29 op if (errstr != NULL) {
1443 1c9ab7cf 2022-01-29 op printf("%s: %s\n", *argv, errstr);
1444 1c9ab7cf 2022-01-29 op free(errstr);
1448 1c9ab7cf 2022-01-29 op if (miss != 0 || qid.type != 0) {
1449 1c9ab7cf 2022-01-29 op printf("%s: not a file\n", *argv);
1450 1c9ab7cf 2022-01-29 op if (miss == 0)
1451 1c9ab7cf 2022-01-29 op do_clunk(nfid);
1455 1c9ab7cf 2022-01-29 op if (pipe(filedes) == -1)
1456 1c9ab7cf 2022-01-29 op err(1, "pipe");
1458 1c9ab7cf 2022-01-29 op switch (pid = vfork()) {
1460 1c9ab7cf 2022-01-29 op err(1, "vfork");
1462 1c9ab7cf 2022-01-29 op close(filedes[1]);
1463 1c9ab7cf 2022-01-29 op if (dup2(filedes[0], 0) == -1)
1464 1c9ab7cf 2022-01-29 op err(1, "dup2");
1465 1c9ab7cf 2022-01-29 op execvp(argv[1], (char *const *)argv + 1);
1466 1c9ab7cf 2022-01-29 op err(1, "execvp");
1469 1c9ab7cf 2022-01-29 op close(filedes[0]);
1470 1c9ab7cf 2022-01-29 op if (fetch_fid(nfid, filedes[1], *argv) == -1)
1471 1c9ab7cf 2022-01-29 op warnx("failed to fetch all the file");
1472 1c9ab7cf 2022-01-29 op close(filedes[1]);
1474 1c9ab7cf 2022-01-29 op waitpid(pid, &status, 0);
1478 01fb39ab 2022-01-16 op cmd_put(int argc, const char **argv)
1480 01fb39ab 2022-01-16 op struct qid qid;
1481 9c48f915 2022-01-16 op const char *l;
1484 01fb39ab 2022-01-16 op if (argc != 1 && argc != 2) {
1485 01fb39ab 2022-01-16 op printf("usage: put local-file [remote-file]\n");
1489 01fb39ab 2022-01-16 op if (argc == 2)
1490 01fb39ab 2022-01-16 op l = argv[1];
1491 01fb39ab 2022-01-16 op else if ((l = strrchr(argv[0], '/')) != NULL)
1492 01fb39ab 2022-01-16 op l++; /* skip / */
1494 01fb39ab 2022-01-16 op l = argv[0];
1496 01fb39ab 2022-01-16 op if ((fd = open(argv[0], O_RDONLY)) == -1) {
1497 01fb39ab 2022-01-16 op warn("%s", argv[0]);
1501 9c48f915 2022-01-16 op woc_file(fd, argv[0], l);
1506 c37e1cfe 2022-01-17 op cmd_rename(int argc, const char **argv)
1508 c37e1cfe 2022-01-17 op struct np_stat st;
1509 c37e1cfe 2022-01-17 op struct qid qid;
1510 c37e1cfe 2022-01-17 op char *errstr;
1511 c37e1cfe 2022-01-17 op int nfid, miss;
1513 c37e1cfe 2022-01-17 op if (argc != 2) {
1514 c37e1cfe 2022-01-17 op puts("usage: rename remote-file new-remote-name");
1518 8d8fb849 2022-01-17 op nfid = nextfid();
1519 c37e1cfe 2022-01-17 op errstr = walk_path(pwdfid, nfid, argv[0], &miss, &qid);
1520 c37e1cfe 2022-01-17 op if (errstr != NULL) {
1521 c37e1cfe 2022-01-17 op printf("%s: %s\n", argv[0], errstr);
1522 c37e1cfe 2022-01-17 op free(errstr);
1526 c37e1cfe 2022-01-17 op if (miss != 0) {
1527 c37e1cfe 2022-01-17 op printf("%s: not such file or directory\n", argv[0]);
1531 c37e1cfe 2022-01-17 op prepare_wstat(&st);
1532 c37e1cfe 2022-01-17 op st.name = (char *)argv[1];
1533 c37e1cfe 2022-01-17 op if ((errstr = do_wstat(nfid, &st)) != NULL) {
1534 c37e1cfe 2022-01-17 op printf("rename: %s\n", errstr);
1535 c37e1cfe 2022-01-17 op free(errstr);
1538 c37e1cfe 2022-01-17 op do_clunk(nfid);
1542 f99c17f9 2022-11-23 op cmd_rm(int argc, const char **argv)
1544 f99c17f9 2022-11-23 op struct qid qid;
1545 f99c17f9 2022-11-23 op char *errstr;
1546 f99c17f9 2022-11-23 op int nfid, miss;
1548 f99c17f9 2022-11-23 op if (argc == 0) {
1549 f99c17f9 2022-11-23 op puts("usage: rm file ...");
1553 f99c17f9 2022-11-23 op for (; *argv; ++argv, --argc) {
1554 f99c17f9 2022-11-23 op nfid = nextfid();
1555 f99c17f9 2022-11-23 op errstr = walk_path(pwdfid, nfid, *argv, &miss, &qid);
1556 f99c17f9 2022-11-23 op if (errstr != NULL) {
1557 f99c17f9 2022-11-23 op printf("%s: %s\n", *argv, errstr);
1558 f99c17f9 2022-11-23 op free(errstr);
1562 f99c17f9 2022-11-23 op printf("%s: not such file or directory\n", *argv);
1566 f99c17f9 2022-11-23 op if ((errstr = do_remove(nfid)) != NULL) {
1567 f99c17f9 2022-11-23 op printf("%s: %s\n", *argv, errstr);
1568 f99c17f9 2022-11-23 op free(errstr);
1575 fb1a36c0 2022-01-09 op cmd_verbose(int argc, const char **argv)
1577 fb1a36c0 2022-01-09 op if (argc == 0) {
1578 fb1a36c0 2022-01-09 op log_setverbose(!log_getverbose());
1579 fb1a36c0 2022-01-09 op if (log_getverbose())
1580 fb1a36c0 2022-01-09 op puts("verbose mode enabled");
1582 fb1a36c0 2022-01-09 op puts("verbose mode disabled");
1586 fb1a36c0 2022-01-09 op if (argc != 1)
1589 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "on")) {
1590 fb1a36c0 2022-01-09 op log_setverbose(1);
1591 fb1a36c0 2022-01-09 op puts("verbose mode enabled");
1595 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "off")) {
1596 fb1a36c0 2022-01-09 op log_setverbose(0);
1597 fb1a36c0 2022-01-09 op puts("verbose mode disabled");
1602 fb1a36c0 2022-01-09 op printf("verbose [on | off]\n");
1606 fb1a36c0 2022-01-09 op excmd(int argc, const char **argv)
1610 fb1a36c0 2022-01-09 op if (argc == 0)
1613 fb1a36c0 2022-01-09 op for (i = 0; i < nitems(cmds); ++i) {
1614 fb1a36c0 2022-01-09 op if (!strcmp(cmds[i].name, *argv)) {
1615 fb1a36c0 2022-01-09 op cmds[i].fn(argc-1, argv+1);
1620 fb1a36c0 2022-01-09 op log_warnx("unknown command %s", *argv);
1624 55e8c065 2022-11-23 op parsecmd(char *cmd, char **argv, size_t len)
1626 55e8c065 2022-11-23 op int escape, quote;
1627 55e8c065 2022-11-23 op int argc = 0;
1629 55e8c065 2022-11-23 op memset(argv, 0, sizeof(*argv) * len);
1631 55e8c065 2022-11-23 op while (argc < len) {
1632 55e8c065 2022-11-23 op while (isspace((unsigned char)*cmd))
1634 55e8c065 2022-11-23 op if (*cmd == '\0')
1637 55e8c065 2022-11-23 op argv[argc++] = cmd;
1638 55e8c065 2022-11-23 op escape = quote = 0;
1639 55e8c065 2022-11-23 op for (; *cmd != '\0'; ++cmd) {
1640 55e8c065 2022-11-23 op if (escape) {
1644 55e8c065 2022-11-23 op if (*cmd == '\\') {
1646 55e8c065 2022-11-23 op memmove(cmd, cmd + 1, strlen(cmd));
1650 55e8c065 2022-11-23 op if (*cmd == quote) {
1652 55e8c065 2022-11-23 op memmove(cmd, cmd + 1, strlen(cmd));
1656 55e8c065 2022-11-23 op if (*cmd == '\'' || *cmd == '"') {
1657 55e8c065 2022-11-23 op quote = *cmd;
1658 55e8c065 2022-11-23 op memmove(cmd, cmd + 1, strlen(cmd));
1665 55e8c065 2022-11-23 op if (isspace((unsigned char)*cmd))
1669 55e8c065 2022-11-23 op if (*cmd == '\0' && (escape || quote)) {
1670 55e8c065 2022-11-23 op fprintf(stderr, "unterminated %s\n",
1671 55e8c065 2022-11-23 op escape ? "escape" : "quote");
1675 55e8c065 2022-11-23 op if (*cmd == '\0')
1677 55e8c065 2022-11-23 op *cmd++ = '\0';
1680 55e8c065 2022-11-23 op if (*cmd != '\0') {
1681 55e8c065 2022-11-23 op fprintf(stderr, "too many arguments\n");
1684 55e8c065 2022-11-23 op return argc;
1688 df9bb54d 2022-11-23 op cd_or_fetch(const char *path, const char *outfile)
1690 14ae3944 2022-11-23 op struct qid qid;
1691 14ae3944 2022-11-23 op char *errstr;
1692 14ae3944 2022-11-23 op int fd, nfid, miss;
1694 14ae3944 2022-11-23 op while (*path == '/')
1696 14ae3944 2022-11-23 op if (*path == '\0')
1699 14ae3944 2022-11-23 op nfid = nextfid();
1700 14ae3944 2022-11-23 op errstr = walk_path(pwdfid, nfid, path, &miss, &qid);
1702 14ae3944 2022-11-23 op errx(1, "walk %s: %s", path, errstr);
1704 14ae3944 2022-11-23 op errc(1, ENOENT, "walk %s", path);
1706 14ae3944 2022-11-23 op if (qid.type & QTDIR) {
1707 df9bb54d 2022-11-23 op if (outfile)
1708 df9bb54d 2022-11-23 op errx(1, "can't fetch directory %s", path);
1709 14ae3944 2022-11-23 op do_clunk(pwdfid);
1710 14ae3944 2022-11-23 op pwdfid = nfid;
1714 df9bb54d 2022-11-23 op if (outfile == NULL) {
1715 df9bb54d 2022-11-23 op if ((outfile = strrchr(path, '/')) == NULL)
1716 df9bb54d 2022-11-23 op outfile = path;
1719 df9bb54d 2022-11-23 op if (*outfile == '\0')
1720 df9bb54d 2022-11-23 op errx(1, "invalid path: missing file name: %s",
1724 df9bb54d 2022-11-23 op if (strcmp(outfile, "-") != 0) {
1725 df9bb54d 2022-11-23 op fd = open(outfile, O_WRONLY|O_CREAT, 0644);
1726 df9bb54d 2022-11-23 op if (fd == -1)
1727 df9bb54d 2022-11-23 op err(1, "can't open for writing %s", outfile);
1731 df9bb54d 2022-11-23 op if (fetch_fid(nfid, fd, outfile) == -1)
1732 df9bb54d 2022-11-23 op err(1, "write %s", outfile);
1738 14ae3944 2022-11-23 op static const char *
1739 14ae3944 2022-11-23 op parse_addr(const char *url, const char **user,
1740 14ae3944 2022-11-23 op const char **port, const char **path)
1742 14ae3944 2022-11-23 op static char buf[PATH_MAX];
1743 14ae3944 2022-11-23 op char *host, *t;
1745 14ae3944 2022-11-23 op *user = *port = *path = NULL;
1748 14ae3944 2022-11-23 op if (strlcpy(buf, url, sizeof(buf)) >= sizeof(buf))
1749 14ae3944 2022-11-23 op errx(1, "connection string too long");
1751 9f134f93 2022-11-23 op if (!strncmp(host, "9p://", 5))
1754 14ae3944 2022-11-23 op if ((t = strchr(host, '/')) != NULL) {
1755 14ae3944 2022-11-23 op if (t == host)
1756 14ae3944 2022-11-23 op errx(1, "invalid connection string: %s", url);
1757 14ae3944 2022-11-23 op *t++ = '\0';
1758 14ae3944 2022-11-23 op if (*t != '\0')
1762 14ae3944 2022-11-23 op if ((t = strchr(host, '@')) != NULL) {
1763 14ae3944 2022-11-23 op if (t == host)
1764 14ae3944 2022-11-23 op errx(1, "invalid connection string: %s", url);
1765 14ae3944 2022-11-23 op *t++ = '\0';
1766 14ae3944 2022-11-23 op *user = host;
1768 14ae3944 2022-11-23 op } else if ((*user = getenv("USER")) == NULL)
1769 14ae3944 2022-11-23 op errx(1, "USER not defined");
1771 14ae3944 2022-11-23 op if ((t = strchr(host, ':')) != NULL) {
1772 14ae3944 2022-11-23 op *t++ = '\0';
1773 14ae3944 2022-11-23 op if (*t != '\0')
1776 14ae3944 2022-11-23 op if (*port == NULL)
1777 14ae3944 2022-11-23 op *port = "1337";
1779 14ae3944 2022-11-23 op return host;
1783 fb1a36c0 2022-01-09 op main(int argc, char **argv)
1785 14ae3944 2022-11-23 op const char *user, *host, *port, *path;
1786 df9bb54d 2022-11-23 op const char *outfile = NULL;
1789 fb1a36c0 2022-01-09 op log_init(1, LOG_DAEMON);
1790 fb1a36c0 2022-01-09 op log_setverbose(0);
1791 fb1a36c0 2022-01-09 op log_procinit(getprogname());
1793 df9bb54d 2022-11-23 op while ((ch = getopt(argc, argv, "C:cK:o:")) != -1) {
1794 fb1a36c0 2022-01-09 op switch (ch) {
1797 fb1a36c0 2022-01-09 op crtpath = optarg;
1799 ab9f44f7 2022-11-23 op case 'c': /* deprecated, remove after 0.3 */
1804 fb1a36c0 2022-01-09 op keypath = optarg;
1807 df9bb54d 2022-11-23 op outfile = optarg;
1813 fb1a36c0 2022-01-09 op argc -= optind;
1814 fb1a36c0 2022-01-09 op argv += optind;
1816 3847d69e 2022-01-30 op if (argc == 0 || (tls && crtpath == NULL))
1819 14ae3944 2022-11-23 op host = parse_addr(argv[0], &user, &port, &path);
1820 514e8d43 2022-11-23 op if (path == NULL && argv[1] != NULL) /* drop argv[1] after 0.3 */
1821 14ae3944 2022-11-23 op path = argv[1];
1822 df9bb54d 2022-11-23 op if (outfile && path == NULL)
1825 1c9ab7cf 2022-01-29 op signal(SIGPIPE, SIG_IGN);
1826 fb1a36c0 2022-01-09 op if (isatty(1)) {
1828 fb1a36c0 2022-01-09 op resized = 1;
1829 fb1a36c0 2022-01-09 op signal(SIGWINCH, tty_resized);
1832 fb1a36c0 2022-01-09 op if ((evb = evbuffer_new()) == NULL)
1833 fb1a36c0 2022-01-09 op fatal("evbuffer_new");
1835 fb1a36c0 2022-01-09 op if ((buf = evbuffer_new()) == NULL)
1836 fb1a36c0 2022-01-09 op fatal("evbuffer_new");
1838 fb1a36c0 2022-01-09 op if ((dirbuf = evbuffer_new()) == NULL)
1839 fb1a36c0 2022-01-09 op fatal("evbuferr_new");
1841 14ae3944 2022-11-23 op do_connect(host, port, user);
1843 df9bb54d 2022-11-23 op cd_or_fetch(path, outfile);
1845 d3e1ab0c 2022-11-24 op compl_setup();
1848 fb1a36c0 2022-01-09 op char *line, *argv[16] = {0}, **ap;
1850 fb1a36c0 2022-01-09 op if ((line = read_line("kamiftp> ")) == NULL)
1853 55e8c065 2022-11-23 op if ((argc = parsecmd(line, argv, nitems(argv) - 1)) == -1) {
1858 55e8c065 2022-11-23 op argv[argc] = NULL;
1859 fb1a36c0 2022-01-09 op excmd(argc, (const char **)argv);
1862 ca14d066 2022-11-23 op fprintf(stderr, "\a");
1867 fb1a36c0 2022-01-09 op printf("\n");