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 #ifndef nitems
46 fb1a36c0 2022-01-09 op #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
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"
54 64d8db89 2022-01-16 op #define TMPFSTR "/tmp/kamiftp.XXXXXXXXXX"
55 64d8db89 2022-01-16 op #define TMPFSTRLEN sizeof(TMPFSTR)
59 fb1a36c0 2022-01-09 op const char *crtpath;
60 fb1a36c0 2022-01-09 op const char *keypath;
68 fb1a36c0 2022-01-09 op struct evbuffer *buf;
69 fb1a36c0 2022-01-09 op struct evbuffer *dirbuf;
70 fb1a36c0 2022-01-09 op uint32_t msize;
73 fb1a36c0 2022-01-09 op volatile sig_atomic_t resized;
75 fb1a36c0 2022-01-09 op int tty_width;
78 fb1a36c0 2022-01-09 op struct progress {
80 fb1a36c0 2022-01-09 op uint64_t done;
85 fb1a36c0 2022-01-09 op #define ASSERT_EMPTYBUF() assert(EVBUFFER_LENGTH(buf) == 0)
87 91cfa92a 2022-01-10 op #if !HAVE_READLINE
89 91cfa92a 2022-01-10 op readline(const char *prompt)
91 91cfa92a 2022-01-10 op char *ch, *line = NULL;
92 91cfa92a 2022-01-10 op size_t linesize = 0;
93 91cfa92a 2022-01-10 op ssize_t linelen;
95 91cfa92a 2022-01-10 op printf("%s", prompt);
96 91cfa92a 2022-01-10 op fflush(stdout);
98 91cfa92a 2022-01-10 op linelen = getline(&line, &linesize, stdin);
99 91cfa92a 2022-01-10 op if (linelen == -1)
102 91cfa92a 2022-01-10 op if ((ch = strchr(line, '\n')) != NULL)
108 91cfa92a 2022-01-10 op add_history(const char *line)
114 fb1a36c0 2022-01-09 op static char *
115 fb1a36c0 2022-01-09 op read_line(const char *prompt)
120 fb1a36c0 2022-01-09 op if ((line = readline(prompt)) == NULL)
122 fb1a36c0 2022-01-09 op /* XXX: trim spaces? */
123 fb1a36c0 2022-01-09 op if (*line == '\0') {
128 fb1a36c0 2022-01-09 op add_history(line);
133 9e7dd230 2022-01-12 op spawn(const char *argv0, ...)
138 9e7dd230 2022-01-12 op const char *argv[16], *last;
141 9e7dd230 2022-01-12 op memset(argv, 0, sizeof(argv));
143 9e7dd230 2022-01-12 op va_start(ap, argv0);
144 9e7dd230 2022-01-12 op argv[0] = argv0;
145 9e7dd230 2022-01-12 op for (i = 1; i < nitems(argv); ++i) {
146 9e7dd230 2022-01-12 op last = va_arg(ap, const char *);
147 9e7dd230 2022-01-12 op if (last == NULL)
149 9e7dd230 2022-01-12 op argv[i] = last;
153 9e7dd230 2022-01-12 op assert(last == NULL);
155 9e7dd230 2022-01-12 op switch (pid = fork()) {
157 9e7dd230 2022-01-12 op err(1, "fork");
158 9e7dd230 2022-01-12 op case 0: /* child */
159 9e7dd230 2022-01-12 op execvp(argv[0], (char *const *)argv);
160 9e7dd230 2022-01-12 op err(1, "execvp");
162 9e7dd230 2022-01-12 op waitpid(pid, &status, 0);
167 8f7253b2 2022-11-22 op stdio_tls_write(void *arg, const char *buf, int len)
169 8f7253b2 2022-11-22 op struct tls *ctx = arg;
173 8f7253b2 2022-11-22 op ret = tls_write(ctx, buf, len);
174 8f7253b2 2022-11-22 op } while (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT);
176 8f7253b2 2022-11-22 op if (ret == -1)
177 8f7253b2 2022-11-22 op warn("tls_write: %s", tls_error(ctx));
183 8f7253b2 2022-11-22 op stdio_tls_read(void *arg, char *buf, int len)
185 8f7253b2 2022-11-22 op struct tls *ctx = arg;
189 8f7253b2 2022-11-22 op ret = tls_read(ctx, buf, len);
190 8f7253b2 2022-11-22 op } while (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT);
192 8f7253b2 2022-11-22 op if (ret == -1)
193 8f7253b2 2022-11-22 op warn("tls_read: %s", tls_error(ctx));
199 8f7253b2 2022-11-22 op stdio_tls_close(void *arg)
201 8f7253b2 2022-11-22 op struct tls *ctx = arg;
205 8f7253b2 2022-11-22 op ret = tls_close(ctx);
206 8f7253b2 2022-11-22 op } while (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT);
212 fb1a36c0 2022-01-09 op tty_resized(int signo)
217 fb1a36c0 2022-01-09 op static void __dead
218 fb1a36c0 2022-01-09 op usage(int ret)
220 dc8c1791 2022-01-10 op fprintf(stderr, "usage: %s [-c] [-C cert] [-K key] "
221 2afde960 2022-11-23 op "[user@]host[:port][/path]\n", getprogname());
222 fb1a36c0 2022-01-09 op fprintf(stderr, "kamid suite version " KAMID_VERSION "\n");
227 8d8fb849 2022-01-17 op nextfid(void)
231 8d8fb849 2022-01-17 op for (i = 0; ; ++i) {
232 8d8fb849 2022-01-17 op if (i != pwdfid)
238 fb1a36c0 2022-01-09 op do_send(void)
243 d33c93c4 2022-01-17 op hexdump("outgoing message", EVBUFFER_DATA(evb),
244 5565d021 2022-01-16 op EVBUFFER_LENGTH(evb));
246 fb1a36c0 2022-01-09 op while (EVBUFFER_LENGTH(evb) != 0) {
247 8f7253b2 2022-11-22 op r = fwrite(EVBUFFER_DATA(evb), 1, EVBUFFER_LENGTH(evb), fp);
249 8f7253b2 2022-11-22 op fatalx("unexpected EOF");
250 fb1a36c0 2022-01-09 op evbuffer_drain(evb, r);
255 fb1a36c0 2022-01-09 op mustread(void *d, size_t len)
259 8f7253b2 2022-11-22 op r = fread(d, 1, len, fp);
260 8f7253b2 2022-11-22 op if (r != len)
261 8f7253b2 2022-11-22 op errx(1, "unexpected EOF");
265 fb1a36c0 2022-01-09 op recv_msg(void)
268 fb1a36c0 2022-01-09 op uint32_t len, l;
269 fb1a36c0 2022-01-09 op char tmp[BUFSIZ];
271 8f7253b2 2022-11-22 op r = fread(&len, 1, sizeof(len), fp);
272 8f7253b2 2022-11-22 op if (r != sizeof(len))
273 8f7253b2 2022-11-22 op errx(1, "unexpected EOF");
275 fb1a36c0 2022-01-09 op len = le32toh(len);
276 fb1a36c0 2022-01-09 op if (len < HEADERSIZE)
277 fb1a36c0 2022-01-09 op errx(1, "read message of invalid length %d", len);
279 fb1a36c0 2022-01-09 op len -= 4; /* skip the length just read */
281 fb1a36c0 2022-01-09 op while (len != 0) {
282 fb1a36c0 2022-01-09 op l = MIN(len, sizeof(tmp));
284 8f7253b2 2022-11-22 op r = fread(tmp, 1, l, fp);
286 8f7253b2 2022-11-22 op errx(1, "unexpected EOF");
288 8f7253b2 2022-11-22 op evbuffer_add(buf, tmp, r);
292 5565d021 2022-01-16 op hexdump("incoming packet", EVBUFFER_DATA(buf),
293 5565d021 2022-01-16 op EVBUFFER_LENGTH(buf));
296 fb1a36c0 2022-01-09 op static uint64_t
297 fb1a36c0 2022-01-09 op np_read64(struct evbuffer *buf)
301 fb1a36c0 2022-01-09 op evbuffer_remove(buf, &n, sizeof(n));
302 fb1a36c0 2022-01-09 op return le64toh(n);
305 fb1a36c0 2022-01-09 op static uint32_t
306 fb1a36c0 2022-01-09 op np_read32(struct evbuffer *buf)
310 fb1a36c0 2022-01-09 op evbuffer_remove(buf, &n, sizeof(n));
311 fb1a36c0 2022-01-09 op return le32toh(n);
314 fb1a36c0 2022-01-09 op static uint16_t
315 fb1a36c0 2022-01-09 op np_read16(struct evbuffer *buf)
319 fb1a36c0 2022-01-09 op evbuffer_remove(buf, &n, sizeof(n));
320 fb1a36c0 2022-01-09 op return le16toh(n);
323 fb1a36c0 2022-01-09 op static uint16_t
324 fb1a36c0 2022-01-09 op np_read8(struct evbuffer *buf)
328 fb1a36c0 2022-01-09 op evbuffer_remove(buf, &n, sizeof(n));
332 fb1a36c0 2022-01-09 op static char *
333 fb1a36c0 2022-01-09 op np_readstr(struct evbuffer *buf)
335 fb1a36c0 2022-01-09 op uint16_t len;
338 fb1a36c0 2022-01-09 op len = np_read16(buf);
339 fb1a36c0 2022-01-09 op assert(EVBUFFER_LENGTH(buf) >= len);
341 fb1a36c0 2022-01-09 op if ((str = calloc(1, len+1)) == NULL)
342 fb1a36c0 2022-01-09 op err(1, "calloc");
343 fb1a36c0 2022-01-09 op evbuffer_remove(buf, str, len);
348 fb1a36c0 2022-01-09 op np_read_qid(struct evbuffer *buf, struct qid *qid)
350 fb1a36c0 2022-01-09 op assert(EVBUFFER_LENGTH(buf) >= QIDSIZE);
352 fb1a36c0 2022-01-09 op qid->type = np_read8(buf);
353 fb1a36c0 2022-01-09 op qid->vers = np_read32(buf);
354 fb1a36c0 2022-01-09 op qid->path = np_read64(buf);
358 fb1a36c0 2022-01-09 op np_read_stat(struct evbuffer *buf, struct np_stat *st)
360 fb1a36c0 2022-01-09 op uint16_t size;
362 fb1a36c0 2022-01-09 op memset(st, 0, sizeof(*st));
364 fb1a36c0 2022-01-09 op size = np_read16(buf);
365 fb1a36c0 2022-01-09 op if (size > EVBUFFER_LENGTH(buf))
368 fb1a36c0 2022-01-09 op st->type = np_read16(buf);
369 fb1a36c0 2022-01-09 op st->dev = np_read32(buf);
370 fb1a36c0 2022-01-09 op np_read_qid(buf, &st->qid);
371 fb1a36c0 2022-01-09 op st->mode = np_read32(buf);
372 fb1a36c0 2022-01-09 op st->atime = np_read32(buf);
373 fb1a36c0 2022-01-09 op st->mtime = np_read32(buf);
374 fb1a36c0 2022-01-09 op st->length = np_read64(buf);
375 fb1a36c0 2022-01-09 op st->name = np_readstr(buf);
376 fb1a36c0 2022-01-09 op st->uid = np_readstr(buf);
377 fb1a36c0 2022-01-09 op st->gid = np_readstr(buf);
378 fb1a36c0 2022-01-09 op st->muid = np_readstr(buf);
384 fb1a36c0 2022-01-09 op expect(uint8_t type)
388 fb1a36c0 2022-01-09 op t = np_read8(buf);
389 fb1a36c0 2022-01-09 op if (t == type)
392 fb1a36c0 2022-01-09 op if (t == Rerror) {
395 fb1a36c0 2022-01-09 op /* skip tag */
396 fb1a36c0 2022-01-09 op np_read16(buf);
398 fb1a36c0 2022-01-09 op err = np_readstr(buf);
399 fb1a36c0 2022-01-09 op errx(1, "expected %s, got error %s",
400 fb1a36c0 2022-01-09 op pp_msg_type(type), err);
403 fb1a36c0 2022-01-09 op errx(1, "expected %s, got msg type %s",
404 fb1a36c0 2022-01-09 op pp_msg_type(type), pp_msg_type(t));
408 fb1a36c0 2022-01-09 op expect2(uint8_t type, uint16_t tag)
412 fb1a36c0 2022-01-09 op expect(type);
414 fb1a36c0 2022-01-09 op t = np_read16(buf);
415 fb1a36c0 2022-01-09 op if (t == tag)
418 fb1a36c0 2022-01-09 op errx(1, "expected tag 0x%x, got 0x%x", tag, t);
421 8f9f99f7 2022-01-16 op static char *
422 8f9f99f7 2022-01-16 op check(uint8_t type, uint16_t tag)
424 8f9f99f7 2022-01-16 op uint16_t rtag;
425 8f9f99f7 2022-01-16 op uint8_t rtype;
427 8f9f99f7 2022-01-16 op rtype = np_read8(buf);
428 8f9f99f7 2022-01-16 op rtag = np_read16(buf);
429 8f9f99f7 2022-01-16 op if (rtype == type) {
430 8f9f99f7 2022-01-16 op if (rtag != tag)
431 8f9f99f7 2022-01-16 op errx(1, "expected tag 0x%x, got 0x%x", tag, rtag);
435 8f9f99f7 2022-01-16 op if (rtype == Rerror)
436 8f9f99f7 2022-01-16 op return np_readstr(buf);
438 8f9f99f7 2022-01-16 op errx(1, "expected %s, got msg type %s",
439 8f9f99f7 2022-01-16 op pp_msg_type(type), pp_msg_type(rtype));
443 fb1a36c0 2022-01-09 op do_version(void)
445 fb1a36c0 2022-01-09 op char *version;
447 fb1a36c0 2022-01-09 op tversion(VERSION9P, MSIZE9P);
450 fb1a36c0 2022-01-09 op expect2(Rversion, NOTAG);
452 fb1a36c0 2022-01-09 op msize = np_read32(buf);
453 fb1a36c0 2022-01-09 op version = np_readstr(buf);
455 315eafe8 2022-05-23 op if (msize > MSIZE9P || msize < 256)
456 fb1a36c0 2022-01-09 op errx(1, "got unexpected msize: %d", msize);
457 fb1a36c0 2022-01-09 op if (strcmp(version, VERSION9P))
458 fb1a36c0 2022-01-09 op errx(1, "unexpected 9p version: %s", version);
460 fb1a36c0 2022-01-09 op free(version);
461 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
465 fb1a36c0 2022-01-09 op do_attach(const char *path)
467 fb1a36c0 2022-01-09 op struct qid qid;
469 fb1a36c0 2022-01-09 op if (path == NULL)
472 fb1a36c0 2022-01-09 op tattach(pwdfid, NOFID, user, path);
475 fb1a36c0 2022-01-09 op expect2(Rattach, iota_tag);
476 fb1a36c0 2022-01-09 op np_read_qid(buf, &qid);
478 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
481 fb1a36c0 2022-01-09 op static uint32_t
482 fb1a36c0 2022-01-09 op do_open(uint32_t fid, uint8_t mode)
484 fb1a36c0 2022-01-09 op struct qid qid;
485 fb1a36c0 2022-01-09 op uint32_t iounit;
487 fb1a36c0 2022-01-09 op topen(fid, mode);
490 fb1a36c0 2022-01-09 op expect2(Ropen, iota_tag);
492 01fb39ab 2022-01-16 op np_read_qid(buf, &qid);
493 01fb39ab 2022-01-16 op iounit = np_read32(buf);
495 01fb39ab 2022-01-16 op ASSERT_EMPTYBUF();
497 01fb39ab 2022-01-16 op return iounit;
500 01fb39ab 2022-01-16 op static uint32_t
501 01fb39ab 2022-01-16 op do_create(uint32_t fid, const char *name, uint32_t perm, uint8_t mode)
503 01fb39ab 2022-01-16 op struct qid qid;
504 01fb39ab 2022-01-16 op uint32_t iounit;
506 01fb39ab 2022-01-16 op tcreate(fid, name, perm, mode);
509 01fb39ab 2022-01-16 op expect2(Rcreate, iota_tag);
511 fb1a36c0 2022-01-09 op np_read_qid(buf, &qid);
512 fb1a36c0 2022-01-09 op iounit = np_read32(buf);
514 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
516 fb1a36c0 2022-01-09 op return iounit;
520 fb1a36c0 2022-01-09 op do_clunk(uint32_t fid)
525 fb1a36c0 2022-01-09 op expect2(Rclunk, iota_tag);
527 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
530 1bb1bfca 2022-01-16 op static char *
531 fb1a36c0 2022-01-09 op dup_fid(int fid, int nfid)
533 fb1a36c0 2022-01-09 op uint16_t nwqid;
534 1bb1bfca 2022-01-16 op char *errstr;
536 fb1a36c0 2022-01-09 op twalk(fid, nfid, NULL, 0);
540 1bb1bfca 2022-01-16 op if ((errstr = check(Rwalk, iota_tag)) != NULL)
541 1bb1bfca 2022-01-16 op return errstr;
543 fb1a36c0 2022-01-09 op nwqid = np_read16(buf);
544 fb1a36c0 2022-01-09 op assert(nwqid == 0);
546 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
551 8f9f99f7 2022-01-16 op static char *
552 8f9f99f7 2022-01-16 op walk_path(int fid, int newfid, const char *path, int *missing,
553 8f9f99f7 2022-01-16 op struct qid *qid)
555 8f9f99f7 2022-01-16 op char *wnames[MAXWELEM], *p, *t, *errstr;
556 fb1a36c0 2022-01-09 op size_t nwname, i;
557 fb1a36c0 2022-01-09 op uint16_t nwqid;
559 fb1a36c0 2022-01-09 op if ((p = strdup(path)) == NULL)
560 fb1a36c0 2022-01-09 op err(1, "strdup");
563 fb1a36c0 2022-01-09 op /* strip initial ./ */
564 fb1a36c0 2022-01-09 op if (t[0] == '.' && t[1] == '/')
567 fb1a36c0 2022-01-09 op for (nwname = 0; nwname < nitems(wnames) &&
568 fb1a36c0 2022-01-09 op (wnames[nwname] = strsep(&t, "/")) != NULL;) {
569 fb1a36c0 2022-01-09 op if (*wnames[nwname] != '\0')
573 fb1a36c0 2022-01-09 op twalk(fid, newfid, (const char **)wnames, nwname);
577 8f9f99f7 2022-01-16 op *missing = nwname;
578 5fe03b5c 2022-01-21 op if ((errstr = check(Rwalk, iota_tag)) != NULL) {
580 8f9f99f7 2022-01-16 op return errstr;
583 fb1a36c0 2022-01-09 op nwqid = np_read16(buf);
584 fb1a36c0 2022-01-09 op assert(nwqid <= nwname);
586 fb1a36c0 2022-01-09 op /* consume all qids */
587 43d117b1 2022-01-16 op for (i = 0; i < nwqid; ++i)
588 fb1a36c0 2022-01-09 op np_read_qid(buf, qid);
592 8f9f99f7 2022-01-16 op *missing = nwname - nwqid;
597 fb1a36c0 2022-01-09 op do_stat(int fid, struct np_stat *st)
602 fb1a36c0 2022-01-09 op expect2(Rstat, iota_tag);
604 caa64af1 2022-02-02 op /* eat up the first two byte length */
605 caa64af1 2022-02-02 op np_read16(buf);
607 fb1a36c0 2022-01-09 op if (np_read_stat(buf, st) == -1)
608 fb1a36c0 2022-01-09 op errx(1, "invalid stat struct read");
610 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
613 c37e1cfe 2022-01-17 op static char *
614 c37e1cfe 2022-01-17 op do_wstat(int fid, const struct np_stat *st)
616 c37e1cfe 2022-01-17 op char *errstr;
618 c37e1cfe 2022-01-17 op twstat(fid, st);
622 c37e1cfe 2022-01-17 op if ((errstr = check(Rwstat, iota_tag)) != NULL)
623 c37e1cfe 2022-01-17 op return errstr;
625 c37e1cfe 2022-01-17 op ASSERT_EMPTYBUF();
630 fb1a36c0 2022-01-09 op static size_t
631 fb1a36c0 2022-01-09 op do_read(int fid, uint64_t off, uint32_t count, void *data)
635 fb1a36c0 2022-01-09 op tread(fid, off, count);
638 fb1a36c0 2022-01-09 op expect2(Rread, iota_tag);
640 fb1a36c0 2022-01-09 op r = np_read32(buf);
641 fb1a36c0 2022-01-09 op assert(r == EVBUFFER_LENGTH(buf));
642 fb1a36c0 2022-01-09 op assert(r <= count);
643 fb1a36c0 2022-01-09 op evbuffer_remove(buf, data, r);
645 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
650 01fb39ab 2022-01-16 op static size_t
651 01fb39ab 2022-01-16 op do_write(int fid, uint64_t off, uint32_t count, void *data)
655 01fb39ab 2022-01-16 op twrite(fid, off, data, count);
658 01fb39ab 2022-01-16 op expect2(Rwrite, iota_tag);
660 01fb39ab 2022-01-16 op r = np_read32(buf);
661 01fb39ab 2022-01-16 op assert(r <= count);
663 01fb39ab 2022-01-16 op ASSERT_EMPTYBUF();
669 fb1a36c0 2022-01-09 op draw_progress(const char *pre, const struct progress *p)
671 fb1a36c0 2022-01-09 op struct winsize ws;
678 fb1a36c0 2022-01-09 op perc = 100.0 * p->done / p->max;
679 fb1a36c0 2022-01-09 op if (!tty_p) {
680 fb1a36c0 2022-01-09 op fprintf(stderr, "%s: %d%%\n", pre, (int)perc);
684 fb1a36c0 2022-01-09 op if (resized) {
687 fb1a36c0 2022-01-09 op if (ioctl(0, TIOCGWINSZ, &ws) == -1)
689 fb1a36c0 2022-01-09 op tty_width = ws.ws_col;
691 fb1a36c0 2022-01-09 op w = tty_width;
693 fb1a36c0 2022-01-09 op if (pre == NULL ||
694 fb1a36c0 2022-01-09 op ((l = printf("\r%s ", pre)) == -1 || l >= w))
697 fb1a36c0 2022-01-09 op w -= l + 2 + 5; /* 2 for |, 5 for percentage + \n */
699 fb1a36c0 2022-01-09 op printf("%4d%%\n", (int)perc);
705 fb1a36c0 2022-01-09 op l = w * MIN(100.0, perc) / 100.0;
706 fb1a36c0 2022-01-09 op for (i = 0; i < l; i++)
708 fb1a36c0 2022-01-09 op for (; i < w; i++)
710 fb1a36c0 2022-01-09 op printf("|%4d%%", (int)perc);
712 fb1a36c0 2022-01-09 op fflush(stdout);
716 76521d26 2022-01-16 op fetch_fid(int fid, int fd, const char *name)
718 02d5b425 2022-05-23 op static char buf[MSIZE9P];
719 fb1a36c0 2022-01-09 op struct progress p = {0};
720 fb1a36c0 2022-01-09 op struct np_stat st;
724 fb1a36c0 2022-01-09 op do_stat(fid, &st);
725 fb1a36c0 2022-01-09 op do_open(fid, KOREAD);
727 fb1a36c0 2022-01-09 op p.max = st.length;
729 02d5b425 2022-05-23 op size_t len, off;
732 02d5b425 2022-05-23 op len = MIN(sizeof(buf), msize);
733 7bafb2fd 2022-08-28 op len -= IOHDRSZ; /* for the request' fields */
735 02d5b425 2022-05-23 op r = do_read(fid, p.done, len, buf);
739 b2263c45 2022-01-16 op for (off = 0; off < r; off += nw)
740 b2263c45 2022-01-16 op if ((nw = write(fd, buf + off, r - off)) == 0 ||
747 9e7dd230 2022-01-12 op draw_progress(name, &p);
750 fb1a36c0 2022-01-09 op /* throttle, for debugging purpose */
752 fb1a36c0 2022-01-09 op struct timespec ts = { 0, 500000000 };
753 fb1a36c0 2022-01-09 op nanosleep(&ts, NULL);
759 fb1a36c0 2022-01-09 op putchar('\n');
761 01fb39ab 2022-01-16 op do_clunk(fid);
766 16f1f3bf 2022-01-21 op send_fid(int fid, const char *fnam, int open_flags, int fd, const char *name)
768 02d5b425 2022-05-23 op static char buf[MSIZE9P];
769 01fb39ab 2022-01-16 op struct progress p = {0};
770 01fb39ab 2022-01-16 op struct stat sb;
772 02d5b425 2022-05-23 op size_t w, len;
774 01fb39ab 2022-01-16 op if (fstat(fd, &sb) == -1)
775 01fb39ab 2022-01-16 op err(1, "fstat");
777 01fb39ab 2022-01-16 op if (fnam != NULL)
778 01fb39ab 2022-01-16 op do_create(fid, fnam, 0644, KOWRITE);
780 16f1f3bf 2022-01-21 op do_open(fid, open_flags | KOWRITE);
782 01fb39ab 2022-01-16 op p.max = sb.st_size;
784 02d5b425 2022-05-23 op len = MIN(sizeof(buf), msize);
785 02d5b425 2022-05-23 op len -= HEADERSIZE + 4 + 4 + 8; /* for the request' fields */
787 02d5b425 2022-05-23 op r = read(fd, buf, len);
791 01fb39ab 2022-01-16 op err(1, "read");
793 01fb39ab 2022-01-16 op w = do_write(fid, p.done, r, buf);
796 01fb39ab 2022-01-16 op draw_progress(name, &p);
799 01fb39ab 2022-01-16 op /* throttle, for debugging purpose */
801 01fb39ab 2022-01-16 op struct timespec ts = { 0, 500000000 };
802 01fb39ab 2022-01-16 op nanosleep(&ts, NULL);
807 01fb39ab 2022-01-16 op putchar('\n');
808 fb1a36c0 2022-01-09 op do_clunk(fid);
812 9c48f915 2022-01-16 op woc_file(int fd, const char *prompt, const char *path)
814 9c48f915 2022-01-16 op struct qid qid;
815 9c48f915 2022-01-16 op const char *n = NULL;
816 9c48f915 2022-01-16 op char *errstr;
817 9c48f915 2022-01-16 op int nfid, miss;
819 8d8fb849 2022-01-17 op nfid = nextfid();
820 9c48f915 2022-01-16 op errstr = walk_path(pwdfid, nfid, path, &miss, &qid);
821 9c48f915 2022-01-16 op if (errstr != NULL && miss > 1) {
822 9c48f915 2022-01-16 op printf("%s: %s\n", path, errstr);
823 9c48f915 2022-01-16 op free(errstr);
827 9c48f915 2022-01-16 op if (errstr != NULL || miss == 1) {
828 9c48f915 2022-01-16 op char p[PATH_MAX], *dn;
831 9c48f915 2022-01-16 op * If it's only one component missing (the file name), walk
832 9c48f915 2022-01-16 op * to the parent directory and try to create the file.
835 9c48f915 2022-01-16 op if (strlcpy(p, path, sizeof(p)) >= sizeof(p)) {
836 9c48f915 2022-01-16 op printf("path too long: %s\n", path);
839 9c48f915 2022-01-16 op dn = dirname(p);
841 9c48f915 2022-01-16 op if (!strcmp(dn, ".")) {
842 9c48f915 2022-01-16 op errstr = dup_fid(pwdfid, nfid);
845 9c48f915 2022-01-16 op errstr = walk_path(pwdfid, nfid, dn, &miss, &qid);
847 9c48f915 2022-01-16 op if (errstr != NULL) {
848 9c48f915 2022-01-16 op printf("%s: %s\n", dn, errstr);
849 9c48f915 2022-01-16 op free(errstr);
853 9c48f915 2022-01-16 op if (miss != 0) {
854 9c48f915 2022-01-16 op printf("%s: not a directory\n", dn);
858 9c48f915 2022-01-16 op if ((n = strrchr(path, '/')) != NULL)
864 9c48f915 2022-01-16 op free(errstr);
866 9c48f915 2022-01-16 op if (miss > 1) {
867 9c48f915 2022-01-16 op printf("can't create %s: missing %d path component(s)\n",
872 16f1f3bf 2022-01-21 op send_fid(nfid, n, KOTRUNC, fd, prompt);
877 8f7253b2 2022-11-22 op dial(const char *host, const char *port)
879 fb1a36c0 2022-01-09 op struct addrinfo hints, *res, *res0;
880 8f7253b2 2022-11-22 op int sock, error, saved_errno;
881 fb1a36c0 2022-01-09 op const char *cause = NULL;
883 fb1a36c0 2022-01-09 op memset(&hints, 0, sizeof(hints));
884 fb1a36c0 2022-01-09 op hints.ai_family = AF_UNSPEC;
885 fb1a36c0 2022-01-09 op hints.ai_socktype = SOCK_STREAM;
886 fb1a36c0 2022-01-09 op error = getaddrinfo(host, port, &hints, &res0);
888 fb1a36c0 2022-01-09 op errx(1, "%s", gai_strerror(error));
891 fb1a36c0 2022-01-09 op for (res = res0; res != NULL; res = res->ai_next) {
892 db066f1e 2022-01-12 op sock = socket(res->ai_family, res->ai_socktype|SOCK_CLOEXEC,
893 fb1a36c0 2022-01-09 op res->ai_protocol);
894 fb1a36c0 2022-01-09 op if (sock == -1) {
895 fb1a36c0 2022-01-09 op cause = "socket";
899 fb1a36c0 2022-01-09 op if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
900 fb1a36c0 2022-01-09 op cause = "connect";
901 fb1a36c0 2022-01-09 op saved_errno = errno;
903 fb1a36c0 2022-01-09 op errno = saved_errno;
910 8f7253b2 2022-11-22 op freeaddrinfo(res0);
911 fb1a36c0 2022-01-09 op if (sock == -1)
912 fb1a36c0 2022-01-09 op err(1, "%s", cause);
917 fb1a36c0 2022-01-09 op do_connect(const char *connspec, const char *path)
919 2afde960 2022-11-23 op char pathbuf[PATH_MAX];
921 fb1a36c0 2022-01-09 op const char *port;
923 fb1a36c0 2022-01-09 op host = xstrdup(connspec);
925 2afde960 2022-11-23 op if ((t = strchr(host, '/')) != NULL) {
926 2afde960 2022-11-23 op if (t == host)
927 2afde960 2022-11-23 op errx(1, "invalid connection string: %s", connspec);
928 2afde960 2022-11-23 op if (strlcpy(pathbuf, t, sizeof(pathbuf)) >= sizeof(pathbuf))
929 2afde960 2022-11-23 op errx(1, "path too long: %s", t);
930 2afde960 2022-11-23 op path = pathbuf;
934 e22049cc 2022-08-28 op if ((t = strchr(host, '@')) != NULL) {
935 e22049cc 2022-08-28 op if (t == host)
936 e22049cc 2022-08-28 op errx(1, "invalid connection string: %s", connspec);
940 e22049cc 2022-08-28 op } else if ((user = getenv("USER")) == NULL)
941 e22049cc 2022-08-28 op errx(1, "USER not defined");
943 e22049cc 2022-08-28 op if ((t = strchr(host, ':')) != NULL) {
946 e22049cc 2022-08-28 op if (*port == '\0')
947 e22049cc 2022-08-28 op errx(1, "invalid connection string: %s", connspec);
949 fb1a36c0 2022-01-09 op port = "1337";
951 fb1a36c0 2022-01-09 op printf("connecting to %s:%s...", host, port);
952 fb1a36c0 2022-01-09 op fflush(stdout);
955 8f7253b2 2022-11-22 op struct tls_config *conf;
956 8f7253b2 2022-11-22 op struct tls *ctx;
959 8f7253b2 2022-11-22 op if ((conf = tls_config_new()) == NULL)
960 8f7253b2 2022-11-22 op fatalx("failed to create TLS config");
961 8f7253b2 2022-11-22 op tls_config_insecure_noverifycert(conf);
962 8f7253b2 2022-11-22 op tls_config_insecure_noverifyname(conf);
964 8f7253b2 2022-11-22 op if (keypath == NULL)
965 8f7253b2 2022-11-22 op keypath = crtpath;
967 8f7253b2 2022-11-22 op if (tls_config_set_keypair_file(conf, crtpath, keypath) == -1)
968 8f7253b2 2022-11-22 op fatalx("failed to load certs: (%s, %s)", crtpath,
971 8f7253b2 2022-11-22 op if ((ctx = tls_client()) == NULL)
972 8f7253b2 2022-11-22 op fatalx("failed to create TLS client");
973 8f7253b2 2022-11-22 op if (tls_configure(ctx, conf) == -1)
974 8f7253b2 2022-11-22 op fatalx("failed to configure TLS client");
975 8f7253b2 2022-11-22 op tls_config_free(conf);
977 8f7253b2 2022-11-22 op if (tls_connect(ctx, host, port) == -1)
978 8f7253b2 2022-11-22 op fatalx("failed to connect to %s:%s: %s", host, port,
979 8f7253b2 2022-11-22 op tls_error(ctx));
982 8f7253b2 2022-11-22 op r = tls_handshake(ctx);
983 8f7253b2 2022-11-22 op } while (r == TLS_WANT_POLLIN || r == TLS_WANT_POLLOUT);
984 8f7253b2 2022-11-22 op fp = funopen(ctx, stdio_tls_read, stdio_tls_write, NULL,
985 8f7253b2 2022-11-22 op stdio_tls_close);
986 8f7253b2 2022-11-22 op if (fp == NULL)
987 8f7253b2 2022-11-22 op fatal("funopen");
991 8f7253b2 2022-11-22 op fd = dial(host, port);
992 8f7253b2 2022-11-22 op if ((fp = fdopen(fd, "r+")) == NULL)
993 8f7253b2 2022-11-22 op fatal("fdopen");
996 fb1a36c0 2022-01-09 op printf(" done!\n");
998 fb1a36c0 2022-01-09 op do_version();
999 fb1a36c0 2022-01-09 op do_attach(path);
1003 64d8db89 2022-01-16 op tmp_file(char sfn[TMPFSTRLEN])
1007 64d8db89 2022-01-16 op strlcpy(sfn, TMPFSTR, TMPFSTRLEN);
1008 64d8db89 2022-01-16 op if ((tmpfd = mkstemp(sfn)) == -1) {
1009 64d8db89 2022-01-16 op warn("mkstemp %s", sfn);
1013 aa76fd7f 2022-01-16 op /* set the close-on-exec flag */
1014 aa76fd7f 2022-01-16 op if (fcntl(tmpfd, F_SETFD, FD_CLOEXEC) == -1) {
1015 aa76fd7f 2022-01-16 op warn("fcntl");
1016 aa76fd7f 2022-01-16 op close(tmpfd);
1020 64d8db89 2022-01-16 op return tmpfd;
1023 1f5fb843 2022-01-16 op static inline const char *
1024 1f5fb843 2022-01-16 op pp_perm(uint8_t x)
1026 1f5fb843 2022-01-16 op switch (x & 0x7) {
1028 1f5fb843 2022-01-16 op return "---";
1030 1f5fb843 2022-01-16 op return "--x";
1032 1f5fb843 2022-01-16 op return "-w-";
1034 1f5fb843 2022-01-16 op return "-wx";
1036 1f5fb843 2022-01-16 op return "r--";
1038 1f5fb843 2022-01-16 op return "r-x";
1040 1f5fb843 2022-01-16 op return "rw-";
1042 1f5fb843 2022-01-16 op return "rwx";
1044 1f5fb843 2022-01-16 op /* unreachable, just for the compiler' happiness */
1045 1f5fb843 2022-01-16 op return "???";
1049 c37e1cfe 2022-01-17 op static inline void
1050 c37e1cfe 2022-01-17 op prepare_wstat(struct np_stat *st)
1052 c37e1cfe 2022-01-17 op memset(st, 0xFF, sizeof(*st));
1053 c37e1cfe 2022-01-17 op st->name = NULL;
1054 c37e1cfe 2022-01-17 op st->uid = NULL;
1055 c37e1cfe 2022-01-17 op st->gid = NULL;
1056 c37e1cfe 2022-01-17 op st->muid = NULL;
1060 fb1a36c0 2022-01-09 op cmd_bell(int argc, const char **argv)
1062 fb1a36c0 2022-01-09 op if (argc == 0) {
1063 fb1a36c0 2022-01-09 op bell = !bell;
1065 fb1a36c0 2022-01-09 op puts("bell mode enabled");
1067 fb1a36c0 2022-01-09 op puts("bell mode disabled");
1071 fb1a36c0 2022-01-09 op if (argc != 1)
1074 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "on")) {
1076 fb1a36c0 2022-01-09 op puts("bell mode enabled");
1080 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "off")) {
1082 fb1a36c0 2022-01-09 op puts("bell mode disabled");
1087 fb1a36c0 2022-01-09 op printf("bell [on | off]\n");
1091 fb1a36c0 2022-01-09 op cmd_bye(int argc, const char **argv)
1093 fb1a36c0 2022-01-09 op log_warnx("bye\n");
1099 fb1a36c0 2022-01-09 op cmd_cd(int argc, const char **argv)
1101 fb1a36c0 2022-01-09 op struct qid qid;
1102 8f9f99f7 2022-01-16 op int nfid, miss;
1103 8f9f99f7 2022-01-16 op char *errstr;
1105 fb1a36c0 2022-01-09 op if (argc != 1) {
1106 fb1a36c0 2022-01-09 op printf("usage: cd remote-path\n");
1110 8d8fb849 2022-01-17 op nfid = nextfid();
1111 8f9f99f7 2022-01-16 op errstr = walk_path(pwdfid, nfid, argv[0], &miss, &qid);
1112 8f9f99f7 2022-01-16 op if (errstr != NULL) {
1113 8f9f99f7 2022-01-16 op printf("%s: %s\n", argv[0], errstr);
1114 8f9f99f7 2022-01-16 op free(errstr);
1118 8f9f99f7 2022-01-16 op if (miss != 0 || !(qid.type & QTDIR)) {
1119 8f9f99f7 2022-01-16 op printf("%s: not a directory\n", argv[0]);
1120 fcd9d510 2022-01-16 op if (miss == 0)
1121 fcd9d510 2022-01-16 op do_clunk(nfid);
1125 8f9f99f7 2022-01-16 op do_clunk(pwdfid);
1126 8f9f99f7 2022-01-16 op pwdfid = nfid;
1130 9c48f915 2022-01-16 op cmd_edit(int argc, const char **argv)
1132 9c48f915 2022-01-16 op struct qid qid;
1133 9c48f915 2022-01-16 op int nfid, tmpfd, miss;
1134 9c48f915 2022-01-16 op char sfn[TMPFSTRLEN], p[PATH_MAX], *name, *errstr;
1135 9c48f915 2022-01-16 op const char *ed;
1137 9c48f915 2022-01-16 op if (argc != 1) {
1138 9c48f915 2022-01-16 op puts("usage: edit file");
1142 9c48f915 2022-01-16 op if ((ed = getenv("VISUAL")) == NULL &&
1143 9c48f915 2022-01-16 op (ed = getenv("EDITOR")) == NULL)
1146 8d8fb849 2022-01-17 op nfid = nextfid();
1147 9c48f915 2022-01-16 op errstr = walk_path(pwdfid, nfid, *argv, &miss, &qid);
1148 9c48f915 2022-01-16 op if (errstr != NULL) {
1149 9c48f915 2022-01-16 op printf("%s: %s\n", *argv, errstr);
1150 9c48f915 2022-01-16 op free(errstr);
1154 9c48f915 2022-01-16 op if (miss != 0 || qid.type != 0) {
1155 9c48f915 2022-01-16 op printf("%s: not a file\n", *argv);
1156 9c48f915 2022-01-16 op if (miss == 0)
1157 9c48f915 2022-01-16 op do_clunk(nfid);
1161 9c48f915 2022-01-16 op if ((tmpfd = tmp_file(sfn)) == -1) {
1162 9c48f915 2022-01-16 op do_clunk(nfid);
1166 9c48f915 2022-01-16 op strlcpy(p, *argv, sizeof(p));
1167 9c48f915 2022-01-16 op name = basename(p);
1169 02571aa1 2022-01-29 op if (fetch_fid(nfid, tmpfd, name)) {
1170 02571aa1 2022-01-29 op warn("failed fetch or can't write %s", sfn);
1173 9c48f915 2022-01-16 op close(tmpfd);
1175 9c48f915 2022-01-16 op spawn(ed, sfn, NULL);
1178 9c48f915 2022-01-16 op * Re-open the file because it's not guaranteed that the
1179 9c48f915 2022-01-16 op * file descriptor tmpfd is still associated with the file
1180 9c48f915 2022-01-16 op * pointed by sfn: it's not uncommon for editor to write
1181 9c48f915 2022-01-16 op * a backup file and then rename(2) it to the file name.
1183 9c48f915 2022-01-16 op if ((tmpfd = open(sfn, O_RDONLY)) == -1) {
1184 9c48f915 2022-01-16 op warn("can't open %s", sfn);
1188 9c48f915 2022-01-16 op woc_file(tmpfd, *argv, name);
1189 9c48f915 2022-01-16 op close(tmpfd);
1192 9c48f915 2022-01-16 op unlink(sfn);
1196 fb1a36c0 2022-01-09 op cmd_get(int argc, const char **argv)
1198 fb1a36c0 2022-01-09 op struct qid qid;
1199 fb1a36c0 2022-01-09 op const char *l;
1200 8f9f99f7 2022-01-16 op char *errstr;
1201 8f9f99f7 2022-01-16 op int nfid, fd, miss;
1203 fb1a36c0 2022-01-09 op if (argc != 1 && argc != 2) {
1204 fb1a36c0 2022-01-09 op printf("usage: get remote-file [local-file]\n");
1208 fb1a36c0 2022-01-09 op if (argc == 2)
1209 fb1a36c0 2022-01-09 op l = argv[1];
1210 fb1a36c0 2022-01-09 op else if ((l = strrchr(argv[0], '/')) != NULL)
1211 fb1a36c0 2022-01-09 op l++; /* skip / */
1213 2ba25711 2022-01-16 op l = argv[0];
1215 8d8fb849 2022-01-17 op nfid = nextfid();
1216 8f9f99f7 2022-01-16 op errstr = walk_path(pwdfid, nfid, argv[0], &miss, &qid);
1217 8f9f99f7 2022-01-16 op if (errstr != NULL) {
1218 8f9f99f7 2022-01-16 op printf("%s: %s\n", argv[0], errstr);
1219 8f9f99f7 2022-01-16 op free(errstr);
1223 8f9f99f7 2022-01-16 op if (miss != 0 || qid.type != 0) {
1224 8f9f99f7 2022-01-16 op printf("%s: not a file\n", argv[0]);
1225 fcd9d510 2022-01-16 op if (miss == 0)
1226 fcd9d510 2022-01-16 op do_clunk(nfid);
1230 76521d26 2022-01-16 op if ((fd = open(l, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0644)) == -1) {
1231 76521d26 2022-01-16 op warn("can't open %s", l);
1232 76521d26 2022-01-16 op do_clunk(nfid);
1236 02571aa1 2022-01-29 op if (fetch_fid(nfid, fd, l) == -1)
1237 02571aa1 2022-01-29 op warn("write %s", l);
1242 5565d021 2022-01-16 op cmd_hexdump(int argc, const char **argv)
1244 5565d021 2022-01-16 op if (argc == 0) {
1245 5565d021 2022-01-16 op xdump = !xdump;
1247 5565d021 2022-01-16 op puts("hexdump mode enabled");
1249 5565d021 2022-01-16 op puts("hexdump mode disabled");
1253 5565d021 2022-01-16 op if (argc > 1)
1256 5565d021 2022-01-16 op if (!strcmp(*argv, "on")) {
1258 5565d021 2022-01-16 op puts("hexdump mode enabled");
1262 5565d021 2022-01-16 op if (!strcmp(*argv, "off")) {
1264 5565d021 2022-01-16 op puts("hexdump mode disabled");
1269 5565d021 2022-01-16 op puts("usage: hexdump [on | off]");
1273 fb1a36c0 2022-01-09 op cmd_lcd(int argc, const char **argv)
1275 fb1a36c0 2022-01-09 op const char *dir;
1277 fb1a36c0 2022-01-09 op if (argc > 1) {
1278 d166ffc0 2022-01-16 op printf("usage: lcd [local-directory]\n");
1282 fb1a36c0 2022-01-09 op if (argc == 1)
1283 fb1a36c0 2022-01-09 op dir = *argv;
1285 fb1a36c0 2022-01-09 op if (argc == 0 && (dir = getenv("HOME")) == NULL) {
1286 fb1a36c0 2022-01-09 op printf("HOME is not defined\n");
1290 fb1a36c0 2022-01-09 op if (chdir(dir) == -1)
1291 fb1a36c0 2022-01-09 op printf("cd: %s: %s\n", dir, strerror(errno));
1295 fb1a36c0 2022-01-09 op cmd_lpwd(int argc, const char **argv)
1297 fb1a36c0 2022-01-09 op char path[PATH_MAX];
1299 d166ffc0 2022-01-16 op if (argc != 0) {
1300 d166ffc0 2022-01-16 op printf("usage: lpwd\n");
1304 fb1a36c0 2022-01-09 op if (getcwd(path, sizeof(path)) == NULL) {
1305 fb1a36c0 2022-01-09 op printf("lpwd: %s\n", strerror(errno));
1309 fb1a36c0 2022-01-09 op printf("%s\n", path);
1313 fb1a36c0 2022-01-09 op cmd_ls(int argc, const char **argv)
1315 fb1a36c0 2022-01-09 op struct np_stat st;
1316 4f52d9af 2022-01-16 op time_t now, mtime;
1317 4f52d9af 2022-01-16 op struct tm *tm;
1318 fb1a36c0 2022-01-09 op uint64_t off = 0;
1319 fb1a36c0 2022-01-09 op uint32_t len;
1321 4f52d9af 2022-01-16 op const char *timfmt;
1322 4f52d9af 2022-01-16 op char fmt[FMT_SCALED_STRSIZE], tim[13], *errstr;
1324 fb1a36c0 2022-01-09 op if (argc != 0) {
1325 fb1a36c0 2022-01-09 op printf("ls don't take arguments (yet)\n");
1329 4f52d9af 2022-01-16 op now = time(NULL);
1331 a9288ad9 2022-01-17 op nfid = nextfid();
1332 a9288ad9 2022-01-17 op if ((errstr = dup_fid(pwdfid, nfid)) != NULL) {
1333 1bb1bfca 2022-01-16 op printf(".: %s\n", errstr);
1334 1bb1bfca 2022-01-16 op free(errstr);
1338 a9288ad9 2022-01-17 op do_open(nfid, KOREAD);
1340 fb1a36c0 2022-01-09 op evbuffer_drain(dirbuf, EVBUFFER_LENGTH(dirbuf));
1343 7bafb2fd 2022-08-28 op tread(nfid, off, msize - IOHDRSZ);
1346 fb1a36c0 2022-01-09 op expect2(Rread, iota_tag);
1348 fb1a36c0 2022-01-09 op len = np_read32(buf);
1349 fb1a36c0 2022-01-09 op if (len == 0)
1352 fb1a36c0 2022-01-09 op evbuffer_add_buffer(dirbuf, buf);
1355 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
1358 fb1a36c0 2022-01-09 op while (EVBUFFER_LENGTH(dirbuf) != 0) {
1359 fb1a36c0 2022-01-09 op if (np_read_stat(dirbuf, &st) == -1)
1360 fb1a36c0 2022-01-09 op errx(1, "invalid stat struct read");
1362 fb1a36c0 2022-01-09 op if (fmt_scaled(st.length, fmt) == -1)
1363 fb1a36c0 2022-01-09 op strlcpy(fmt, "xxx", sizeof(fmt));
1365 4f52d9af 2022-01-16 op mtime = st.mtime;
1367 4f52d9af 2022-01-16 op if (now > mtime && (now - mtime) < 365/2 * 24 * 12 * 60)
1368 4f52d9af 2022-01-16 op timfmt = "%b %e %R";
1370 4f52d9af 2022-01-16 op timfmt = "%b %e %Y";
1372 4f52d9af 2022-01-16 op if ((tm = localtime(&mtime)) == NULL ||
1373 4f52d9af 2022-01-16 op strftime(tim, sizeof(tim), timfmt, tm) == 0)
1374 4f52d9af 2022-01-16 op strlcpy(tim, "unknown", sizeof(tim));
1376 1f5fb843 2022-01-16 op if (st.qid.type & QTDIR)
1377 1f5fb843 2022-01-16 op printf("d");
1379 1f5fb843 2022-01-16 op printf("-");
1380 1f5fb843 2022-01-16 op printf("%s", pp_perm(st.mode >> 6));
1381 1f5fb843 2022-01-16 op printf("%s", pp_perm(st.mode >> 3));
1382 1f5fb843 2022-01-16 op printf("%s", pp_perm(st.mode));
1383 d33c93c4 2022-01-17 op printf(" %8s %12s %s%s\n", fmt, tim, st.name,
1384 6470594e 2022-01-16 op st.qid.type & QTDIR ? "/" : "");
1386 fb1a36c0 2022-01-09 op free(st.name);
1387 fb1a36c0 2022-01-09 op free(st.uid);
1388 fb1a36c0 2022-01-09 op free(st.gid);
1389 fb1a36c0 2022-01-09 op free(st.muid);
1392 a9288ad9 2022-01-17 op do_clunk(nfid);
1396 9e7dd230 2022-01-12 op cmd_page(int argc, const char **argv)
1398 9e7dd230 2022-01-12 op struct qid qid;
1399 02571aa1 2022-01-29 op int nfid, tmpfd, miss, r;
1400 64d8db89 2022-01-16 op char sfn[TMPFSTRLEN], p[PATH_MAX], *name, *errstr;
1401 62f663bb 2022-01-16 op const char *pager;
1403 9e7dd230 2022-01-12 op if (argc != 1) {
1404 9e7dd230 2022-01-12 op puts("usage: page file");
1408 62f663bb 2022-01-16 op if ((pager = getenv("PAGER")) == NULL)
1409 62f663bb 2022-01-16 op pager = "less";
1411 8d8fb849 2022-01-17 op nfid = nextfid();
1412 8f9f99f7 2022-01-16 op errstr = walk_path(pwdfid, nfid, *argv, &miss, &qid);
1413 8f9f99f7 2022-01-16 op if (errstr != NULL) {
1414 8f9f99f7 2022-01-16 op printf("%s: %s\n", *argv, errstr);
1415 8f9f99f7 2022-01-16 op free(errstr);
1419 8f9f99f7 2022-01-16 op if (miss != 0 || qid.type != 0) {
1420 8f9f99f7 2022-01-16 op printf("%s: not a file\n", *argv);
1421 fcd9d510 2022-01-16 op if (miss == 0)
1422 fcd9d510 2022-01-16 op do_clunk(nfid);
1426 64d8db89 2022-01-16 op if ((tmpfd = tmp_file(sfn)) == -1) {
1427 9e7dd230 2022-01-12 op do_clunk(nfid);
1431 9e7dd230 2022-01-12 op strlcpy(p, *argv, sizeof(p));
1432 9e7dd230 2022-01-12 op name = basename(p);
1433 02571aa1 2022-01-29 op if ((r = fetch_fid(nfid, tmpfd, name)) == -1)
1434 02571aa1 2022-01-29 op warn("write %s", sfn);
1435 9e7dd230 2022-01-12 op close(tmpfd);
1436 02571aa1 2022-01-29 op if (r != -1)
1437 02571aa1 2022-01-29 op spawn(pager, sfn, NULL);
1438 9e7dd230 2022-01-12 op unlink(sfn);
1442 1c9ab7cf 2022-01-29 op cmd_pipe(int argc, const char **argv)
1444 1c9ab7cf 2022-01-29 op struct qid qid;
1446 1c9ab7cf 2022-01-29 op int nfid, tmpfd, miss, status;
1447 1c9ab7cf 2022-01-29 op int filedes[2]; /* read end, write end */
1448 1c9ab7cf 2022-01-29 op char *errstr;
1450 1c9ab7cf 2022-01-29 op if (argc < 2) {
1451 1c9ab7cf 2022-01-29 op puts("usage: pipe remote-file cmd [args...]");
1455 1c9ab7cf 2022-01-29 op nfid = nextfid();
1456 1c9ab7cf 2022-01-29 op errstr = walk_path(pwdfid, nfid, *argv, &miss, &qid);
1457 1c9ab7cf 2022-01-29 op if (errstr != NULL) {
1458 1c9ab7cf 2022-01-29 op printf("%s: %s\n", *argv, errstr);
1459 1c9ab7cf 2022-01-29 op free(errstr);
1463 1c9ab7cf 2022-01-29 op if (miss != 0 || qid.type != 0) {
1464 1c9ab7cf 2022-01-29 op printf("%s: not a file\n", *argv);
1465 1c9ab7cf 2022-01-29 op if (miss == 0)
1466 1c9ab7cf 2022-01-29 op do_clunk(nfid);
1470 1c9ab7cf 2022-01-29 op if (pipe(filedes) == -1)
1471 1c9ab7cf 2022-01-29 op err(1, "pipe");
1473 1c9ab7cf 2022-01-29 op switch (pid = vfork()) {
1475 1c9ab7cf 2022-01-29 op err(1, "vfork");
1477 1c9ab7cf 2022-01-29 op close(filedes[1]);
1478 1c9ab7cf 2022-01-29 op if (dup2(filedes[0], 0) == -1)
1479 1c9ab7cf 2022-01-29 op err(1, "dup2");
1480 1c9ab7cf 2022-01-29 op execvp(argv[1], (char *const *)argv + 1);
1481 1c9ab7cf 2022-01-29 op err(1, "execvp");
1484 1c9ab7cf 2022-01-29 op close(filedes[0]);
1485 1c9ab7cf 2022-01-29 op if (fetch_fid(nfid, filedes[1], *argv) == -1)
1486 1c9ab7cf 2022-01-29 op warnx("failed to fetch all the file");
1487 1c9ab7cf 2022-01-29 op close(filedes[1]);
1489 1c9ab7cf 2022-01-29 op waitpid(pid, &status, 0);
1493 01fb39ab 2022-01-16 op cmd_put(int argc, const char **argv)
1495 01fb39ab 2022-01-16 op struct qid qid;
1496 9c48f915 2022-01-16 op const char *l;
1499 01fb39ab 2022-01-16 op if (argc != 1 && argc != 2) {
1500 01fb39ab 2022-01-16 op printf("usage: put local-file [remote-file]\n");
1504 01fb39ab 2022-01-16 op if (argc == 2)
1505 01fb39ab 2022-01-16 op l = argv[1];
1506 01fb39ab 2022-01-16 op else if ((l = strrchr(argv[0], '/')) != NULL)
1507 01fb39ab 2022-01-16 op l++; /* skip / */
1509 01fb39ab 2022-01-16 op l = argv[0];
1511 01fb39ab 2022-01-16 op if ((fd = open(argv[0], O_RDONLY)) == -1) {
1512 01fb39ab 2022-01-16 op warn("%s", argv[0]);
1516 9c48f915 2022-01-16 op woc_file(fd, argv[0], l);
1521 c37e1cfe 2022-01-17 op cmd_rename(int argc, const char **argv)
1523 c37e1cfe 2022-01-17 op struct np_stat st;
1524 c37e1cfe 2022-01-17 op struct qid qid;
1525 c37e1cfe 2022-01-17 op char *errstr;
1526 c37e1cfe 2022-01-17 op int nfid, miss;
1528 c37e1cfe 2022-01-17 op if (argc != 2) {
1529 c37e1cfe 2022-01-17 op puts("usage: rename remote-file new-remote-name");
1533 8d8fb849 2022-01-17 op nfid = nextfid();
1534 c37e1cfe 2022-01-17 op errstr = walk_path(pwdfid, nfid, argv[0], &miss, &qid);
1535 c37e1cfe 2022-01-17 op if (errstr != NULL) {
1536 c37e1cfe 2022-01-17 op printf("%s: %s\n", argv[0], errstr);
1537 c37e1cfe 2022-01-17 op free(errstr);
1541 c37e1cfe 2022-01-17 op if (miss != 0) {
1542 c37e1cfe 2022-01-17 op printf("%s: not such file or directory\n", argv[0]);
1546 c37e1cfe 2022-01-17 op prepare_wstat(&st);
1547 c37e1cfe 2022-01-17 op st.name = (char *)argv[1];
1548 c37e1cfe 2022-01-17 op if ((errstr = do_wstat(nfid, &st)) != NULL) {
1549 c37e1cfe 2022-01-17 op printf("rename: %s\n", errstr);
1550 c37e1cfe 2022-01-17 op free(errstr);
1553 c37e1cfe 2022-01-17 op do_clunk(nfid);
1557 fb1a36c0 2022-01-09 op cmd_verbose(int argc, const char **argv)
1559 fb1a36c0 2022-01-09 op if (argc == 0) {
1560 fb1a36c0 2022-01-09 op log_setverbose(!log_getverbose());
1561 fb1a36c0 2022-01-09 op if (log_getverbose())
1562 fb1a36c0 2022-01-09 op puts("verbose mode enabled");
1564 fb1a36c0 2022-01-09 op puts("verbose mode disabled");
1568 fb1a36c0 2022-01-09 op if (argc != 1)
1571 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "on")) {
1572 fb1a36c0 2022-01-09 op log_setverbose(1);
1573 fb1a36c0 2022-01-09 op puts("verbose mode enabled");
1577 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "off")) {
1578 fb1a36c0 2022-01-09 op log_setverbose(0);
1579 fb1a36c0 2022-01-09 op puts("verbose mode disabled");
1584 fb1a36c0 2022-01-09 op printf("verbose [on | off]\n");
1588 fb1a36c0 2022-01-09 op excmd(int argc, const char **argv)
1590 fb1a36c0 2022-01-09 op struct cmd {
1591 fb1a36c0 2022-01-09 op const char *name;
1592 fb1a36c0 2022-01-09 op void (*fn)(int, const char **);
1593 fb1a36c0 2022-01-09 op } cmds[] = {
1594 fb1a36c0 2022-01-09 op {"bell", cmd_bell},
1595 fb1a36c0 2022-01-09 op {"bye", cmd_bye},
1596 fb1a36c0 2022-01-09 op {"cd", cmd_cd},
1597 9c48f915 2022-01-16 op {"edit", cmd_edit},
1598 fb1a36c0 2022-01-09 op {"get", cmd_get},
1599 5565d021 2022-01-16 op {"hexdump", cmd_hexdump},
1600 fb1a36c0 2022-01-09 op {"lcd", cmd_lcd},
1601 fb1a36c0 2022-01-09 op {"lpwd", cmd_lpwd},
1602 fb1a36c0 2022-01-09 op {"ls", cmd_ls},
1603 9e7dd230 2022-01-12 op {"page", cmd_page},
1604 1c9ab7cf 2022-01-29 op {"pipe", cmd_pipe},
1605 01fb39ab 2022-01-16 op {"put", cmd_put},
1606 fb1a36c0 2022-01-09 op {"quit", cmd_bye},
1607 c37e1cfe 2022-01-17 op {"rename", cmd_rename},
1608 fb1a36c0 2022-01-09 op {"verbose", cmd_verbose},
1612 fb1a36c0 2022-01-09 op if (argc == 0)
1614 fb1a36c0 2022-01-09 op for (i = 0; i < nitems(cmds); ++i) {
1615 fb1a36c0 2022-01-09 op if (!strcmp(cmds[i].name, *argv)) {
1616 fb1a36c0 2022-01-09 op cmds[i].fn(argc-1, argv+1);
1621 fb1a36c0 2022-01-09 op log_warnx("unknown command %s", *argv);
1625 fb1a36c0 2022-01-09 op main(int argc, char **argv)
1629 fb1a36c0 2022-01-09 op log_init(1, LOG_DAEMON);
1630 fb1a36c0 2022-01-09 op log_setverbose(0);
1631 fb1a36c0 2022-01-09 op log_procinit(getprogname());
1633 fb1a36c0 2022-01-09 op while ((ch = getopt(argc, argv, "C:cK:")) != -1) {
1634 fb1a36c0 2022-01-09 op switch (ch) {
1637 fb1a36c0 2022-01-09 op crtpath = optarg;
1644 fb1a36c0 2022-01-09 op keypath = optarg;
1650 fb1a36c0 2022-01-09 op argc -= optind;
1651 fb1a36c0 2022-01-09 op argv += optind;
1653 3847d69e 2022-01-30 op if (argc == 0 || (tls && crtpath == NULL))
1656 1c9ab7cf 2022-01-29 op signal(SIGPIPE, SIG_IGN);
1657 fb1a36c0 2022-01-09 op if (isatty(1)) {
1659 fb1a36c0 2022-01-09 op resized = 1;
1660 fb1a36c0 2022-01-09 op signal(SIGWINCH, tty_resized);
1663 fb1a36c0 2022-01-09 op if ((evb = evbuffer_new()) == NULL)
1664 fb1a36c0 2022-01-09 op fatal("evbuffer_new");
1666 fb1a36c0 2022-01-09 op if ((buf = evbuffer_new()) == NULL)
1667 fb1a36c0 2022-01-09 op fatal("evbuffer_new");
1669 fb1a36c0 2022-01-09 op if ((dirbuf = evbuffer_new()) == NULL)
1670 fb1a36c0 2022-01-09 op fatal("evbuferr_new");
1672 fb1a36c0 2022-01-09 op do_connect(argv[0], argv[1]);
1675 fb1a36c0 2022-01-09 op int argc = 0;
1676 fb1a36c0 2022-01-09 op char *line, *argv[16] = {0}, **ap;
1678 fb1a36c0 2022-01-09 op if ((line = read_line("kamiftp> ")) == NULL)
1681 fb1a36c0 2022-01-09 op for (argc = 0, ap = argv; ap < &argv[15] &&
1682 fb1a36c0 2022-01-09 op (*ap = strsep(&line, " \t")) != NULL;) {
1683 fb1a36c0 2022-01-09 op if (**ap != '\0')
1684 fb1a36c0 2022-01-09 op ap++, argc++;
1686 fb1a36c0 2022-01-09 op excmd(argc, (const char **)argv);
1689 fb1a36c0 2022-01-09 op printf("\a");
1690 fb1a36c0 2022-01-09 op fflush(stdout);
1696 fb1a36c0 2022-01-09 op printf("\n");