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;
61 58b141cf 2022-12-21 op struct tls_config *tlsconf;
62 58b141cf 2022-12-21 op struct tls *ctx;
64 fb1a36c0 2022-01-09 op struct evbuffer *buf;
65 fb1a36c0 2022-01-09 op struct evbuffer *dirbuf;
66 fb1a36c0 2022-01-09 op uint32_t msize;
70 fb1a36c0 2022-01-09 op volatile sig_atomic_t resized;
72 fb1a36c0 2022-01-09 op int tty_width;
75 fb1a36c0 2022-01-09 op struct progress {
77 fb1a36c0 2022-01-09 op uint64_t done;
82 fb1a36c0 2022-01-09 op #define ASSERT_EMPTYBUF() assert(EVBUFFER_LENGTH(buf) == 0)
85 fb1a36c0 2022-01-09 op read_line(const char *prompt)
90 fb1a36c0 2022-01-09 op if ((line = readline(prompt)) == NULL)
92 fb1a36c0 2022-01-09 op /* XXX: trim spaces? */
93 fb1a36c0 2022-01-09 op if (*line == '\0') {
98 fb1a36c0 2022-01-09 op add_history(line);
103 9e7dd230 2022-01-12 op spawn(const char *argv0, ...)
108 9e7dd230 2022-01-12 op const char *argv[16], *last;
111 9e7dd230 2022-01-12 op memset(argv, 0, sizeof(argv));
113 9e7dd230 2022-01-12 op va_start(ap, argv0);
114 9e7dd230 2022-01-12 op argv[0] = argv0;
115 9e7dd230 2022-01-12 op for (i = 1; i < nitems(argv); ++i) {
116 9e7dd230 2022-01-12 op last = va_arg(ap, const char *);
117 9e7dd230 2022-01-12 op if (last == NULL)
119 9e7dd230 2022-01-12 op argv[i] = last;
123 9e7dd230 2022-01-12 op assert(last == NULL);
125 9e7dd230 2022-01-12 op switch (pid = fork()) {
127 9e7dd230 2022-01-12 op err(1, "fork");
128 9e7dd230 2022-01-12 op case 0: /* child */
129 9e7dd230 2022-01-12 op execvp(argv[0], (char *const *)argv);
130 9e7dd230 2022-01-12 op err(1, "execvp");
132 9e7dd230 2022-01-12 op waitpid(pid, &status, 0);
137 fb1a36c0 2022-01-09 op tty_resized(int signo)
142 dd647997 2022-11-23 op static __dead void
143 fb1a36c0 2022-01-09 op usage(int ret)
145 ab9f44f7 2022-11-23 op fprintf(stderr, "usage: %s [-C cert] [-K key] [-o output] "
146 9f134f93 2022-11-23 op "[9p://][user@]host[:port][/path]\n", getprogname());
147 fb1a36c0 2022-01-09 op fprintf(stderr, "kamid suite version " KAMID_VERSION "\n");
152 8d8fb849 2022-01-17 op nextfid(void)
156 8d8fb849 2022-01-17 op for (i = 0; ; ++i) {
157 8d8fb849 2022-01-17 op if (i != pwdfid)
163 fb1a36c0 2022-01-09 op do_send(void)
165 58b141cf 2022-12-21 op const void *buf;
166 58b141cf 2022-12-21 op size_t nbytes;
170 d33c93c4 2022-01-17 op hexdump("outgoing message", EVBUFFER_DATA(evb),
171 5565d021 2022-01-16 op EVBUFFER_LENGTH(evb));
173 fb1a36c0 2022-01-09 op while (EVBUFFER_LENGTH(evb) != 0) {
174 58b141cf 2022-12-21 op buf = EVBUFFER_DATA(evb);
175 58b141cf 2022-12-21 op nbytes = EVBUFFER_LENGTH(evb);
177 58b141cf 2022-12-21 op if (ctx == NULL) {
178 58b141cf 2022-12-21 op r = write(sock, buf, nbytes);
179 58b141cf 2022-12-21 op if (r == 0 || r == -1)
180 58b141cf 2022-12-21 op errx(1, "EOF");
182 58b141cf 2022-12-21 op r = tls_write(ctx, buf, nbytes);
183 58b141cf 2022-12-21 op if (r == TLS_WANT_POLLIN || r == TLS_WANT_POLLOUT)
186 58b141cf 2022-12-21 op errx(1, "tls: %s", tls_error(ctx));
189 fb1a36c0 2022-01-09 op evbuffer_drain(evb, r);
194 9a4b05cc 2022-12-21 op mustread(void *buf, size_t len)
197 9a4b05cc 2022-12-21 op uint8_t *d = buf;
199 58b141cf 2022-12-21 op while (len != 0) {
200 58b141cf 2022-12-21 op if (ctx == NULL) {
201 58b141cf 2022-12-21 op r = read(sock, d, len);
202 58b141cf 2022-12-21 op if (r == 0 || r == -1)
203 58b141cf 2022-12-21 op errx(1, "EOF");
205 58b141cf 2022-12-21 op r = tls_read(ctx, d, len);
206 58b141cf 2022-12-21 op if (r == TLS_WANT_POLLIN || r == TLS_WANT_POLLOUT)
209 58b141cf 2022-12-21 op errx(1, "tls: %s", tls_error(ctx));
218 fb1a36c0 2022-01-09 op recv_msg(void)
220 fb1a36c0 2022-01-09 op uint32_t len, l;
221 fb1a36c0 2022-01-09 op char tmp[BUFSIZ];
223 58b141cf 2022-12-21 op mustread(&len, sizeof(len));
224 fb1a36c0 2022-01-09 op len = le32toh(len);
225 fb1a36c0 2022-01-09 op if (len < HEADERSIZE)
226 fb1a36c0 2022-01-09 op errx(1, "read message of invalid length %d", len);
228 fb1a36c0 2022-01-09 op len -= 4; /* skip the length just read */
230 fb1a36c0 2022-01-09 op while (len != 0) {
231 fb1a36c0 2022-01-09 op l = MIN(len, sizeof(tmp));
232 58b141cf 2022-12-21 op mustread(tmp, l);
234 58b141cf 2022-12-21 op evbuffer_add(buf, tmp, l);
238 5565d021 2022-01-16 op hexdump("incoming packet", EVBUFFER_DATA(buf),
239 5565d021 2022-01-16 op EVBUFFER_LENGTH(buf));
242 fb1a36c0 2022-01-09 op static uint64_t
243 fb1a36c0 2022-01-09 op np_read64(struct evbuffer *buf)
247 fb1a36c0 2022-01-09 op evbuffer_remove(buf, &n, sizeof(n));
248 fb1a36c0 2022-01-09 op return le64toh(n);
251 fb1a36c0 2022-01-09 op static uint32_t
252 fb1a36c0 2022-01-09 op np_read32(struct evbuffer *buf)
256 fb1a36c0 2022-01-09 op evbuffer_remove(buf, &n, sizeof(n));
257 fb1a36c0 2022-01-09 op return le32toh(n);
260 fb1a36c0 2022-01-09 op static uint16_t
261 fb1a36c0 2022-01-09 op np_read16(struct evbuffer *buf)
265 fb1a36c0 2022-01-09 op evbuffer_remove(buf, &n, sizeof(n));
266 fb1a36c0 2022-01-09 op return le16toh(n);
269 fb1a36c0 2022-01-09 op static uint16_t
270 fb1a36c0 2022-01-09 op np_read8(struct evbuffer *buf)
274 fb1a36c0 2022-01-09 op evbuffer_remove(buf, &n, sizeof(n));
278 fb1a36c0 2022-01-09 op static char *
279 fb1a36c0 2022-01-09 op np_readstr(struct evbuffer *buf)
281 fb1a36c0 2022-01-09 op uint16_t len;
284 fb1a36c0 2022-01-09 op len = np_read16(buf);
285 fb1a36c0 2022-01-09 op assert(EVBUFFER_LENGTH(buf) >= len);
287 fb1a36c0 2022-01-09 op if ((str = calloc(1, len+1)) == NULL)
288 fb1a36c0 2022-01-09 op err(1, "calloc");
289 fb1a36c0 2022-01-09 op evbuffer_remove(buf, str, len);
294 fb1a36c0 2022-01-09 op np_read_qid(struct evbuffer *buf, struct qid *qid)
296 fb1a36c0 2022-01-09 op assert(EVBUFFER_LENGTH(buf) >= QIDSIZE);
298 fb1a36c0 2022-01-09 op qid->type = np_read8(buf);
299 fb1a36c0 2022-01-09 op qid->vers = np_read32(buf);
300 fb1a36c0 2022-01-09 op qid->path = np_read64(buf);
304 fb1a36c0 2022-01-09 op np_read_stat(struct evbuffer *buf, struct np_stat *st)
306 fb1a36c0 2022-01-09 op uint16_t size;
308 fb1a36c0 2022-01-09 op memset(st, 0, sizeof(*st));
310 fb1a36c0 2022-01-09 op size = np_read16(buf);
311 fb1a36c0 2022-01-09 op if (size > EVBUFFER_LENGTH(buf))
314 fb1a36c0 2022-01-09 op st->type = np_read16(buf);
315 fb1a36c0 2022-01-09 op st->dev = np_read32(buf);
316 fb1a36c0 2022-01-09 op np_read_qid(buf, &st->qid);
317 fb1a36c0 2022-01-09 op st->mode = np_read32(buf);
318 fb1a36c0 2022-01-09 op st->atime = np_read32(buf);
319 fb1a36c0 2022-01-09 op st->mtime = np_read32(buf);
320 fb1a36c0 2022-01-09 op st->length = np_read64(buf);
321 fb1a36c0 2022-01-09 op st->name = np_readstr(buf);
322 fb1a36c0 2022-01-09 op st->uid = np_readstr(buf);
323 fb1a36c0 2022-01-09 op st->gid = np_readstr(buf);
324 fb1a36c0 2022-01-09 op st->muid = np_readstr(buf);
330 fb1a36c0 2022-01-09 op expect(uint8_t type)
334 fb1a36c0 2022-01-09 op t = np_read8(buf);
335 fb1a36c0 2022-01-09 op if (t == type)
338 fb1a36c0 2022-01-09 op if (t == Rerror) {
341 fb1a36c0 2022-01-09 op /* skip tag */
342 fb1a36c0 2022-01-09 op np_read16(buf);
344 fb1a36c0 2022-01-09 op err = np_readstr(buf);
345 fb1a36c0 2022-01-09 op errx(1, "expected %s, got error %s",
346 fb1a36c0 2022-01-09 op pp_msg_type(type), err);
349 fb1a36c0 2022-01-09 op errx(1, "expected %s, got msg type %s",
350 fb1a36c0 2022-01-09 op pp_msg_type(type), pp_msg_type(t));
354 fb1a36c0 2022-01-09 op expect2(uint8_t type, uint16_t tag)
358 fb1a36c0 2022-01-09 op expect(type);
360 fb1a36c0 2022-01-09 op t = np_read16(buf);
361 fb1a36c0 2022-01-09 op if (t == tag)
364 fb1a36c0 2022-01-09 op errx(1, "expected tag 0x%x, got 0x%x", tag, t);
367 8f9f99f7 2022-01-16 op static char *
368 8f9f99f7 2022-01-16 op check(uint8_t type, uint16_t tag)
370 8f9f99f7 2022-01-16 op uint16_t rtag;
371 8f9f99f7 2022-01-16 op uint8_t rtype;
373 8f9f99f7 2022-01-16 op rtype = np_read8(buf);
374 8f9f99f7 2022-01-16 op rtag = np_read16(buf);
375 8f9f99f7 2022-01-16 op if (rtype == type) {
376 8f9f99f7 2022-01-16 op if (rtag != tag)
377 8f9f99f7 2022-01-16 op errx(1, "expected tag 0x%x, got 0x%x", tag, rtag);
381 8f9f99f7 2022-01-16 op if (rtype == Rerror)
382 8f9f99f7 2022-01-16 op return np_readstr(buf);
384 8f9f99f7 2022-01-16 op errx(1, "expected %s, got msg type %s",
385 8f9f99f7 2022-01-16 op pp_msg_type(type), pp_msg_type(rtype));
389 fb1a36c0 2022-01-09 op do_version(void)
391 fb1a36c0 2022-01-09 op char *version;
393 fb1a36c0 2022-01-09 op tversion(VERSION9P, MSIZE9P);
396 fb1a36c0 2022-01-09 op expect2(Rversion, NOTAG);
398 fb1a36c0 2022-01-09 op msize = np_read32(buf);
399 fb1a36c0 2022-01-09 op version = np_readstr(buf);
401 315eafe8 2022-05-23 op if (msize > MSIZE9P || msize < 256)
402 fb1a36c0 2022-01-09 op errx(1, "got unexpected msize: %d", msize);
403 fb1a36c0 2022-01-09 op if (strcmp(version, VERSION9P))
404 fb1a36c0 2022-01-09 op errx(1, "unexpected 9p version: %s", version);
406 fb1a36c0 2022-01-09 op free(version);
407 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
411 14ae3944 2022-11-23 op do_attach(const char *user)
413 fb1a36c0 2022-01-09 op struct qid qid;
415 2c50e0f6 2022-11-23 op tattach(pwdfid, NOFID, user, "/");
418 fb1a36c0 2022-01-09 op expect2(Rattach, iota_tag);
419 fb1a36c0 2022-01-09 op np_read_qid(buf, &qid);
421 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
424 fb1a36c0 2022-01-09 op static uint32_t
425 fb1a36c0 2022-01-09 op do_open(uint32_t fid, uint8_t mode)
427 fb1a36c0 2022-01-09 op struct qid qid;
428 fb1a36c0 2022-01-09 op uint32_t iounit;
430 fb1a36c0 2022-01-09 op topen(fid, mode);
433 fb1a36c0 2022-01-09 op expect2(Ropen, iota_tag);
435 01fb39ab 2022-01-16 op np_read_qid(buf, &qid);
436 01fb39ab 2022-01-16 op iounit = np_read32(buf);
438 01fb39ab 2022-01-16 op ASSERT_EMPTYBUF();
440 01fb39ab 2022-01-16 op return iounit;
443 01fb39ab 2022-01-16 op static uint32_t
444 01fb39ab 2022-01-16 op do_create(uint32_t fid, const char *name, uint32_t perm, uint8_t mode)
446 01fb39ab 2022-01-16 op struct qid qid;
447 01fb39ab 2022-01-16 op uint32_t iounit;
449 01fb39ab 2022-01-16 op tcreate(fid, name, perm, mode);
452 01fb39ab 2022-01-16 op expect2(Rcreate, iota_tag);
454 fb1a36c0 2022-01-09 op np_read_qid(buf, &qid);
455 fb1a36c0 2022-01-09 op iounit = np_read32(buf);
457 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
459 fb1a36c0 2022-01-09 op return iounit;
463 fb1a36c0 2022-01-09 op do_clunk(uint32_t fid)
468 fb1a36c0 2022-01-09 op expect2(Rclunk, iota_tag);
470 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
473 1bb1bfca 2022-01-16 op static char *
474 fb1a36c0 2022-01-09 op dup_fid(int fid, int nfid)
476 fb1a36c0 2022-01-09 op uint16_t nwqid;
477 1bb1bfca 2022-01-16 op char *errstr;
479 fb1a36c0 2022-01-09 op twalk(fid, nfid, NULL, 0);
483 1bb1bfca 2022-01-16 op if ((errstr = check(Rwalk, iota_tag)) != NULL)
484 1bb1bfca 2022-01-16 op return errstr;
486 fb1a36c0 2022-01-09 op nwqid = np_read16(buf);
487 fb1a36c0 2022-01-09 op assert(nwqid == 0);
489 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
494 8f9f99f7 2022-01-16 op static char *
495 8f9f99f7 2022-01-16 op walk_path(int fid, int newfid, const char *path, int *missing,
496 8f9f99f7 2022-01-16 op struct qid *qid)
498 8f9f99f7 2022-01-16 op char *wnames[MAXWELEM], *p, *t, *errstr;
499 fb1a36c0 2022-01-09 op size_t nwname, i;
500 fb1a36c0 2022-01-09 op uint16_t nwqid;
502 fb1a36c0 2022-01-09 op if ((p = strdup(path)) == NULL)
503 fb1a36c0 2022-01-09 op err(1, "strdup");
506 fb1a36c0 2022-01-09 op /* strip initial ./ */
507 fb1a36c0 2022-01-09 op if (t[0] == '.' && t[1] == '/')
510 fb1a36c0 2022-01-09 op for (nwname = 0; nwname < nitems(wnames) &&
511 fb1a36c0 2022-01-09 op (wnames[nwname] = strsep(&t, "/")) != NULL;) {
512 fb1a36c0 2022-01-09 op if (*wnames[nwname] != '\0')
516 fb1a36c0 2022-01-09 op twalk(fid, newfid, (const char **)wnames, nwname);
520 8f9f99f7 2022-01-16 op *missing = nwname;
521 5fe03b5c 2022-01-21 op if ((errstr = check(Rwalk, iota_tag)) != NULL) {
523 8f9f99f7 2022-01-16 op return errstr;
526 fb1a36c0 2022-01-09 op nwqid = np_read16(buf);
527 fb1a36c0 2022-01-09 op assert(nwqid <= nwname);
529 fb1a36c0 2022-01-09 op /* consume all qids */
530 43d117b1 2022-01-16 op for (i = 0; i < nwqid; ++i)
531 fb1a36c0 2022-01-09 op np_read_qid(buf, qid);
535 8f9f99f7 2022-01-16 op *missing = nwname - nwqid;
540 fb1a36c0 2022-01-09 op do_stat(int fid, struct np_stat *st)
545 fb1a36c0 2022-01-09 op expect2(Rstat, iota_tag);
547 caa64af1 2022-02-02 op /* eat up the first two byte length */
548 caa64af1 2022-02-02 op np_read16(buf);
550 fb1a36c0 2022-01-09 op if (np_read_stat(buf, st) == -1)
551 fb1a36c0 2022-01-09 op errx(1, "invalid stat struct read");
553 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
556 c37e1cfe 2022-01-17 op static char *
557 c37e1cfe 2022-01-17 op do_wstat(int fid, const struct np_stat *st)
559 c37e1cfe 2022-01-17 op char *errstr;
561 c37e1cfe 2022-01-17 op twstat(fid, st);
565 c37e1cfe 2022-01-17 op if ((errstr = check(Rwstat, iota_tag)) != NULL)
566 c37e1cfe 2022-01-17 op return errstr;
568 c37e1cfe 2022-01-17 op ASSERT_EMPTYBUF();
573 f99c17f9 2022-11-23 op static char *
574 f99c17f9 2022-11-23 op do_remove(int fid)
576 f99c17f9 2022-11-23 op char *errstr;
578 f99c17f9 2022-11-23 op tremove(fid);
581 f99c17f9 2022-11-23 op if ((errstr = check(Rremove, iota_tag)) != NULL)
582 f99c17f9 2022-11-23 op return errstr;
584 f99c17f9 2022-11-23 op ASSERT_EMPTYBUF();
589 fb1a36c0 2022-01-09 op static size_t
590 fb1a36c0 2022-01-09 op do_read(int fid, uint64_t off, uint32_t count, void *data)
594 fb1a36c0 2022-01-09 op tread(fid, off, count);
597 fb1a36c0 2022-01-09 op expect2(Rread, iota_tag);
599 fb1a36c0 2022-01-09 op r = np_read32(buf);
600 fb1a36c0 2022-01-09 op assert(r == EVBUFFER_LENGTH(buf));
601 fb1a36c0 2022-01-09 op assert(r <= count);
602 fb1a36c0 2022-01-09 op evbuffer_remove(buf, data, r);
604 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
609 01fb39ab 2022-01-16 op static size_t
610 01fb39ab 2022-01-16 op do_write(int fid, uint64_t off, uint32_t count, void *data)
614 01fb39ab 2022-01-16 op twrite(fid, off, data, count);
617 01fb39ab 2022-01-16 op expect2(Rwrite, iota_tag);
619 01fb39ab 2022-01-16 op r = np_read32(buf);
620 01fb39ab 2022-01-16 op assert(r <= count);
622 01fb39ab 2022-01-16 op ASSERT_EMPTYBUF();
628 fb1a36c0 2022-01-09 op draw_progress(const char *pre, const struct progress *p)
630 fb1a36c0 2022-01-09 op struct winsize ws;
637 fb1a36c0 2022-01-09 op perc = 100.0 * p->done / p->max;
638 fb1a36c0 2022-01-09 op if (!tty_p) {
639 fb1a36c0 2022-01-09 op fprintf(stderr, "%s: %d%%\n", pre, (int)perc);
643 fb1a36c0 2022-01-09 op if (resized) {
646 fb1a36c0 2022-01-09 op if (ioctl(0, TIOCGWINSZ, &ws) == -1)
648 fb1a36c0 2022-01-09 op tty_width = ws.ws_col;
650 fb1a36c0 2022-01-09 op w = tty_width;
652 fb1a36c0 2022-01-09 op if (pre == NULL ||
653 5d0dedb3 2022-11-23 op ((l = fprintf(stderr, "\r%s ", pre)) == -1 || l >= w))
656 fb1a36c0 2022-01-09 op w -= l + 2 + 5; /* 2 for |, 5 for percentage + \n */
658 5d0dedb3 2022-11-23 op fprintf(stderr, "%4d%%\n", (int)perc);
662 5d0dedb3 2022-11-23 op fprintf(stderr, "|");
664 fb1a36c0 2022-01-09 op l = w * MIN(100.0, perc) / 100.0;
665 fb1a36c0 2022-01-09 op for (i = 0; i < l; i++)
666 5d0dedb3 2022-11-23 op fprintf(stderr, "*");
667 fb1a36c0 2022-01-09 op for (; i < w; i++)
668 5d0dedb3 2022-11-23 op fprintf(stderr, " ");
669 5d0dedb3 2022-11-23 op fprintf(stderr, "|%4d%%", (int)perc);
673 76521d26 2022-01-16 op fetch_fid(int fid, int fd, const char *name)
675 02d5b425 2022-05-23 op static char buf[MSIZE9P];
676 fb1a36c0 2022-01-09 op struct progress p = {0};
677 fb1a36c0 2022-01-09 op struct np_stat st;
681 fb1a36c0 2022-01-09 op do_stat(fid, &st);
682 fb1a36c0 2022-01-09 op do_open(fid, KOREAD);
684 fb1a36c0 2022-01-09 op p.max = st.length;
686 02d5b425 2022-05-23 op size_t len, off;
689 02d5b425 2022-05-23 op len = MIN(sizeof(buf), msize);
690 7bafb2fd 2022-08-28 op len -= IOHDRSZ; /* for the request' fields */
692 02d5b425 2022-05-23 op r = do_read(fid, p.done, len, buf);
696 b2263c45 2022-01-16 op for (off = 0; off < r; off += nw)
697 b2263c45 2022-01-16 op if ((nw = write(fd, buf + off, r - off)) == 0 ||
704 9e7dd230 2022-01-12 op draw_progress(name, &p);
707 fb1a36c0 2022-01-09 op /* throttle, for debugging purpose */
709 fb1a36c0 2022-01-09 op struct timespec ts = { 0, 500000000 };
710 fb1a36c0 2022-01-09 op nanosleep(&ts, NULL);
716 fb1a36c0 2022-01-09 op putchar('\n');
718 01fb39ab 2022-01-16 op do_clunk(fid);
723 16f1f3bf 2022-01-21 op send_fid(int fid, const char *fnam, int open_flags, int fd, const char *name)
725 02d5b425 2022-05-23 op static char buf[MSIZE9P];
726 01fb39ab 2022-01-16 op struct progress p = {0};
727 01fb39ab 2022-01-16 op struct stat sb;
729 02d5b425 2022-05-23 op size_t w, len;
731 01fb39ab 2022-01-16 op if (fstat(fd, &sb) == -1)
732 01fb39ab 2022-01-16 op err(1, "fstat");
734 01fb39ab 2022-01-16 op if (fnam != NULL)
735 01fb39ab 2022-01-16 op do_create(fid, fnam, 0644, KOWRITE);
737 16f1f3bf 2022-01-21 op do_open(fid, open_flags | KOWRITE);
739 01fb39ab 2022-01-16 op p.max = sb.st_size;
741 02d5b425 2022-05-23 op len = MIN(sizeof(buf), msize);
742 02d5b425 2022-05-23 op len -= HEADERSIZE + 4 + 4 + 8; /* for the request' fields */
744 02d5b425 2022-05-23 op r = read(fd, buf, len);
748 01fb39ab 2022-01-16 op err(1, "read");
750 01fb39ab 2022-01-16 op w = do_write(fid, p.done, r, buf);
753 01fb39ab 2022-01-16 op draw_progress(name, &p);
756 01fb39ab 2022-01-16 op /* throttle, for debugging purpose */
758 01fb39ab 2022-01-16 op struct timespec ts = { 0, 500000000 };
759 01fb39ab 2022-01-16 op nanosleep(&ts, NULL);
764 01fb39ab 2022-01-16 op putchar('\n');
765 fb1a36c0 2022-01-09 op do_clunk(fid);
769 9c48f915 2022-01-16 op woc_file(int fd, const char *prompt, const char *path)
771 9c48f915 2022-01-16 op struct qid qid;
772 9c48f915 2022-01-16 op const char *n = NULL;
773 9c48f915 2022-01-16 op char *errstr;
774 9c48f915 2022-01-16 op int nfid, miss;
776 8d8fb849 2022-01-17 op nfid = nextfid();
777 9c48f915 2022-01-16 op errstr = walk_path(pwdfid, nfid, path, &miss, &qid);
778 9c48f915 2022-01-16 op if (errstr != NULL && miss > 1) {
779 5d0dedb3 2022-11-23 op fprintf(stderr, "%s: %s\n", path, errstr);
780 9c48f915 2022-01-16 op free(errstr);
784 9c48f915 2022-01-16 op if (errstr != NULL || miss == 1) {
785 9c48f915 2022-01-16 op char p[PATH_MAX], *dn;
788 9c48f915 2022-01-16 op * If it's only one component missing (the file name), walk
789 9c48f915 2022-01-16 op * to the parent directory and try to create the file.
792 9c48f915 2022-01-16 op if (strlcpy(p, path, sizeof(p)) >= sizeof(p)) {
793 5d0dedb3 2022-11-23 op fprintf(stderr, "path too long: %s\n", path);
796 9c48f915 2022-01-16 op dn = dirname(p);
798 9c48f915 2022-01-16 op if (!strcmp(dn, ".")) {
799 9c48f915 2022-01-16 op errstr = dup_fid(pwdfid, nfid);
802 9c48f915 2022-01-16 op errstr = walk_path(pwdfid, nfid, dn, &miss, &qid);
804 9c48f915 2022-01-16 op if (errstr != NULL) {
805 5d0dedb3 2022-11-23 op fprintf(stderr, "%s: %s\n", dn, errstr);
806 9c48f915 2022-01-16 op free(errstr);
810 9c48f915 2022-01-16 op if (miss != 0) {
811 5d0dedb3 2022-11-23 op fprintf(stderr, "%s: not a directory\n", dn);
815 9c48f915 2022-01-16 op if ((n = strrchr(path, '/')) != NULL)
821 9c48f915 2022-01-16 op free(errstr);
823 9c48f915 2022-01-16 op if (miss > 1) {
824 5d0dedb3 2022-11-23 op fprintf(stderr, "can't create %s: missing %d path"
825 5d0dedb3 2022-11-23 op " component(s)\n", path, miss);
829 16f1f3bf 2022-01-21 op send_fid(nfid, n, KOTRUNC, fd, prompt);
834 58b141cf 2022-12-21 op do_tls_connect(const char *host, const char *port)
836 58b141cf 2022-12-21 op int handshake;
838 58b141cf 2022-12-21 op if ((tlsconf = tls_config_new()) == NULL)
839 58b141cf 2022-12-21 op fatalx("tls_config_new");
840 58b141cf 2022-12-21 op tls_config_insecure_noverifycert(tlsconf);
841 58b141cf 2022-12-21 op tls_config_insecure_noverifyname(tlsconf);
843 58b141cf 2022-12-21 op if (keypath == NULL)
844 58b141cf 2022-12-21 op keypath = crtpath;
846 58b141cf 2022-12-21 op if (tls_config_set_keypair_file(tlsconf, crtpath, keypath) == -1)
847 58b141cf 2022-12-21 op fatalx("can't load certs (%s, %s)", crtpath, keypath);
849 58b141cf 2022-12-21 op if ((ctx = tls_client()) == NULL)
850 58b141cf 2022-12-21 op fatal("tls_client");
851 58b141cf 2022-12-21 op if (tls_configure(ctx, tlsconf) == -1)
852 58b141cf 2022-12-21 op fatalx("tls_configure: %s", tls_error(ctx));
854 58b141cf 2022-12-21 op if (tls_connect(ctx, host, port) == -1)
855 58b141cf 2022-12-21 op fatalx("can't connect to %s:%s: %s", host, port,
856 58b141cf 2022-12-21 op tls_error(ctx));
858 58b141cf 2022-12-21 op for (handshake = 0; !handshake;) {
859 58b141cf 2022-12-21 op switch (tls_handshake(ctx)) {
861 58b141cf 2022-12-21 op fatalx("tls_handshake: %s", tls_error(ctx));
863 58b141cf 2022-12-21 op handshake = 1;
870 58b141cf 2022-12-21 op do_ctxt_connect(const char *host, const char *port)
872 fb1a36c0 2022-01-09 op struct addrinfo hints, *res, *res0;
873 58b141cf 2022-12-21 op int error, saved_errno;
874 fb1a36c0 2022-01-09 op const char *cause = NULL;
876 fb1a36c0 2022-01-09 op memset(&hints, 0, sizeof(hints));
877 fb1a36c0 2022-01-09 op hints.ai_family = AF_UNSPEC;
878 fb1a36c0 2022-01-09 op hints.ai_socktype = SOCK_STREAM;
879 fb1a36c0 2022-01-09 op error = getaddrinfo(host, port, &hints, &res0);
881 fb1a36c0 2022-01-09 op errx(1, "%s", gai_strerror(error));
884 fb1a36c0 2022-01-09 op for (res = res0; res != NULL; res = res->ai_next) {
885 db066f1e 2022-01-12 op sock = socket(res->ai_family, res->ai_socktype|SOCK_CLOEXEC,
886 fb1a36c0 2022-01-09 op res->ai_protocol);
887 fb1a36c0 2022-01-09 op if (sock == -1) {
888 fb1a36c0 2022-01-09 op cause = "socket";
892 fb1a36c0 2022-01-09 op if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
893 fb1a36c0 2022-01-09 op cause = "connect";
894 fb1a36c0 2022-01-09 op saved_errno = errno;
896 fb1a36c0 2022-01-09 op errno = saved_errno;
904 fb1a36c0 2022-01-09 op if (sock == -1)
905 fb1a36c0 2022-01-09 op err(1, "%s", cause);
906 58b141cf 2022-12-21 op freeaddrinfo(res0);
910 14ae3944 2022-11-23 op do_connect(const char *host, const char *port, const char *user)
912 14ae3944 2022-11-23 op struct qid qid;
913 14ae3944 2022-11-23 op int nfid, miss, fd;
914 14ae3944 2022-11-23 op char *errstr;
916 5d0dedb3 2022-11-23 op fprintf(stderr, "connecting to %s:%s...", host, port);
919 58b141cf 2022-12-21 op do_tls_connect(host, port);
921 58b141cf 2022-12-21 op do_ctxt_connect(host, port);
923 5d0dedb3 2022-11-23 op fprintf(stderr, " done!\n");
925 fb1a36c0 2022-01-09 op do_version();
926 14ae3944 2022-11-23 op do_attach(user);
930 64d8db89 2022-01-16 op tmp_file(char sfn[TMPFSTRLEN])
934 64d8db89 2022-01-16 op strlcpy(sfn, TMPFSTR, TMPFSTRLEN);
935 64d8db89 2022-01-16 op if ((tmpfd = mkstemp(sfn)) == -1) {
936 64d8db89 2022-01-16 op warn("mkstemp %s", sfn);
940 aa76fd7f 2022-01-16 op /* set the close-on-exec flag */
941 aa76fd7f 2022-01-16 op if (fcntl(tmpfd, F_SETFD, FD_CLOEXEC) == -1) {
942 aa76fd7f 2022-01-16 op warn("fcntl");
943 aa76fd7f 2022-01-16 op close(tmpfd);
947 64d8db89 2022-01-16 op return tmpfd;
950 1f5fb843 2022-01-16 op static inline const char *
951 1f5fb843 2022-01-16 op pp_perm(uint8_t x)
953 1f5fb843 2022-01-16 op switch (x & 0x7) {
955 1f5fb843 2022-01-16 op return "---";
957 1f5fb843 2022-01-16 op return "--x";
959 1f5fb843 2022-01-16 op return "-w-";
961 1f5fb843 2022-01-16 op return "-wx";
963 1f5fb843 2022-01-16 op return "r--";
965 1f5fb843 2022-01-16 op return "r-x";
967 1f5fb843 2022-01-16 op return "rw-";
969 1f5fb843 2022-01-16 op return "rwx";
971 1f5fb843 2022-01-16 op /* unreachable, just for the compiler' happiness */
972 1f5fb843 2022-01-16 op return "???";
976 c37e1cfe 2022-01-17 op static inline void
977 c37e1cfe 2022-01-17 op prepare_wstat(struct np_stat *st)
979 c37e1cfe 2022-01-17 op memset(st, 0xFF, sizeof(*st));
980 c37e1cfe 2022-01-17 op st->name = NULL;
981 c37e1cfe 2022-01-17 op st->uid = NULL;
982 c37e1cfe 2022-01-17 op st->gid = NULL;
983 c37e1cfe 2022-01-17 op st->muid = NULL;
987 d3e1ab0c 2022-11-24 op print_dirent(const struct np_stat *st)
989 d3e1ab0c 2022-11-24 op time_t mtime;
990 d3e1ab0c 2022-11-24 op struct tm *tm;
991 d3e1ab0c 2022-11-24 op const char *timfmt;
992 d3e1ab0c 2022-11-24 op char fmt[FMT_SCALED_STRSIZE], tim[13];
994 d3e1ab0c 2022-11-24 op if (fmt_scaled(st->length, fmt) == -1)
995 d3e1ab0c 2022-11-24 op strlcpy(fmt, "xxx", sizeof(fmt));
997 d3e1ab0c 2022-11-24 op mtime = st->mtime;
999 d3e1ab0c 2022-11-24 op if (now > mtime && (now - mtime) < 365/2 * 24 * 12 * 60)
1000 d3e1ab0c 2022-11-24 op timfmt = "%b %e %R";
1002 d3e1ab0c 2022-11-24 op timfmt = "%b %e %Y";
1004 d3e1ab0c 2022-11-24 op if ((tm = localtime(&mtime)) == NULL ||
1005 d3e1ab0c 2022-11-24 op strftime(tim, sizeof(tim), timfmt, tm) == 0)
1006 d3e1ab0c 2022-11-24 op strlcpy(tim, "unknown", sizeof(tim));
1008 d3e1ab0c 2022-11-24 op if (st->qid.type & QTDIR)
1009 d3e1ab0c 2022-11-24 op printf("d");
1011 d3e1ab0c 2022-11-24 op printf("-");
1012 d3e1ab0c 2022-11-24 op printf("%s", pp_perm(st->mode >> 6));
1013 d3e1ab0c 2022-11-24 op printf("%s", pp_perm(st->mode >> 3));
1014 d3e1ab0c 2022-11-24 op printf("%s", pp_perm(st->mode));
1015 d3e1ab0c 2022-11-24 op printf(" %8s %12s %s%s\n", fmt, tim, st->name,
1016 d3e1ab0c 2022-11-24 op st->qid.type & QTDIR ? "/" : "");
1022 d3e1ab0c 2022-11-24 op dir_listing(const char *path, int (*fn)(const struct np_stat *),
1023 d3e1ab0c 2022-11-24 op int printerr)
1025 12bfa80e 2022-12-21 op struct qid qid = {0, 0, QTDIR};
1026 d3e1ab0c 2022-11-24 op struct np_stat st;
1027 d3e1ab0c 2022-11-24 op uint64_t off = 0;
1028 d3e1ab0c 2022-11-24 op uint32_t len;
1029 12bfa80e 2022-12-21 op int nfid, r, miss = 0;
1030 d3e1ab0c 2022-11-24 op char *errstr;
1032 d3e1ab0c 2022-11-24 op now = time(NULL);
1033 d3e1ab0c 2022-11-24 op nfid = nextfid();
1035 12bfa80e 2022-12-21 op if (!strcmp(path, "."))
1036 12bfa80e 2022-12-21 op errstr = dup_fid(pwdfid, nfid);
1038 12bfa80e 2022-12-21 op errstr = walk_path(pwdfid, nfid, path, &miss, &qid);
1039 d3e1ab0c 2022-11-24 op if (errstr != NULL) {
1040 d3e1ab0c 2022-11-24 op if (printerr)
1041 d3e1ab0c 2022-11-24 op printf("%s: %s\n", path, errstr);
1042 d3e1ab0c 2022-11-24 op free(errstr);
1046 d3e1ab0c 2022-11-24 op if (printerr)
1047 d3e1ab0c 2022-11-24 op printf("%s: No such file or directory\n", path);
1050 d3e1ab0c 2022-11-24 op if (!(qid.type & QTDIR)) {
1051 d3e1ab0c 2022-11-24 op if (printerr)
1052 d3e1ab0c 2022-11-24 op printf("%s: not a directory\n", path);
1053 d3e1ab0c 2022-11-24 op do_clunk(nfid);
1057 d3e1ab0c 2022-11-24 op do_open(nfid, KOREAD);
1058 d3e1ab0c 2022-11-24 op evbuffer_drain(dirbuf, EVBUFFER_LENGTH(dirbuf));
1061 d3e1ab0c 2022-11-24 op tread(nfid, off, msize - IOHDRSZ);
1064 d3e1ab0c 2022-11-24 op expect2(Rread, iota_tag);
1066 d3e1ab0c 2022-11-24 op len = np_read32(buf);
1067 d3e1ab0c 2022-11-24 op if (len == 0)
1070 d3e1ab0c 2022-11-24 op evbuffer_add_buffer(dirbuf, buf);
1073 d3e1ab0c 2022-11-24 op ASSERT_EMPTYBUF();
1076 d3e1ab0c 2022-11-24 op while (EVBUFFER_LENGTH(dirbuf) != 0) {
1077 d3e1ab0c 2022-11-24 op if (np_read_stat(dirbuf, &st) == -1)
1078 d3e1ab0c 2022-11-24 op errx(1, "invalid stat struct read");
1080 d3e1ab0c 2022-11-24 op r = fn(&st);
1082 d3e1ab0c 2022-11-24 op free(st.name);
1083 d3e1ab0c 2022-11-24 op free(st.uid);
1084 d3e1ab0c 2022-11-24 op free(st.gid);
1085 d3e1ab0c 2022-11-24 op free(st.muid);
1087 d3e1ab0c 2022-11-24 op if (r == -1)
1091 d3e1ab0c 2022-11-24 op evbuffer_drain(dirbuf, EVBUFFER_LENGTH(dirbuf));
1092 d3e1ab0c 2022-11-24 op do_clunk(nfid);
1097 fb1a36c0 2022-01-09 op cmd_bell(int argc, const char **argv)
1099 fb1a36c0 2022-01-09 op if (argc == 0) {
1100 fb1a36c0 2022-01-09 op bell = !bell;
1102 fb1a36c0 2022-01-09 op puts("bell mode enabled");
1104 fb1a36c0 2022-01-09 op puts("bell mode disabled");
1108 fb1a36c0 2022-01-09 op if (argc != 1)
1111 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "on")) {
1113 fb1a36c0 2022-01-09 op puts("bell mode enabled");
1117 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "off")) {
1119 fb1a36c0 2022-01-09 op puts("bell mode disabled");
1124 fb1a36c0 2022-01-09 op printf("bell [on | off]\n");
1128 fb1a36c0 2022-01-09 op cmd_bye(int argc, const char **argv)
1130 fb1a36c0 2022-01-09 op log_warnx("bye\n");
1135 fb1a36c0 2022-01-09 op cmd_cd(int argc, const char **argv)
1137 fb1a36c0 2022-01-09 op struct qid qid;
1138 8f9f99f7 2022-01-16 op int nfid, miss;
1139 8f9f99f7 2022-01-16 op char *errstr;
1141 fb1a36c0 2022-01-09 op if (argc != 1) {
1142 fb1a36c0 2022-01-09 op printf("usage: cd remote-path\n");
1146 8d8fb849 2022-01-17 op nfid = nextfid();
1147 8f9f99f7 2022-01-16 op errstr = walk_path(pwdfid, nfid, argv[0], &miss, &qid);
1148 8f9f99f7 2022-01-16 op if (errstr != NULL) {
1149 8f9f99f7 2022-01-16 op printf("%s: %s\n", argv[0], errstr);
1150 8f9f99f7 2022-01-16 op free(errstr);
1154 8f9f99f7 2022-01-16 op if (miss != 0 || !(qid.type & QTDIR)) {
1155 8f9f99f7 2022-01-16 op printf("%s: not a directory\n", argv[0]);
1156 fcd9d510 2022-01-16 op if (miss == 0)
1157 fcd9d510 2022-01-16 op do_clunk(nfid);
1161 8f9f99f7 2022-01-16 op do_clunk(pwdfid);
1162 8f9f99f7 2022-01-16 op pwdfid = nfid;
1166 9c48f915 2022-01-16 op cmd_edit(int argc, const char **argv)
1168 9c48f915 2022-01-16 op struct qid qid;
1169 9c48f915 2022-01-16 op int nfid, tmpfd, miss;
1170 9c48f915 2022-01-16 op char sfn[TMPFSTRLEN], p[PATH_MAX], *name, *errstr;
1171 9c48f915 2022-01-16 op const char *ed;
1173 9c48f915 2022-01-16 op if (argc != 1) {
1174 9c48f915 2022-01-16 op puts("usage: edit file");
1178 9c48f915 2022-01-16 op if ((ed = getenv("VISUAL")) == NULL &&
1179 9c48f915 2022-01-16 op (ed = getenv("EDITOR")) == NULL)
1182 8d8fb849 2022-01-17 op nfid = nextfid();
1183 9c48f915 2022-01-16 op errstr = walk_path(pwdfid, nfid, *argv, &miss, &qid);
1184 9c48f915 2022-01-16 op if (errstr != NULL) {
1185 9c48f915 2022-01-16 op printf("%s: %s\n", *argv, errstr);
1186 9c48f915 2022-01-16 op free(errstr);
1190 9c48f915 2022-01-16 op if (miss != 0 || qid.type != 0) {
1191 9c48f915 2022-01-16 op printf("%s: not a file\n", *argv);
1192 9c48f915 2022-01-16 op if (miss == 0)
1193 9c48f915 2022-01-16 op do_clunk(nfid);
1197 9c48f915 2022-01-16 op if ((tmpfd = tmp_file(sfn)) == -1) {
1198 9c48f915 2022-01-16 op do_clunk(nfid);
1202 9c48f915 2022-01-16 op strlcpy(p, *argv, sizeof(p));
1203 9c48f915 2022-01-16 op name = basename(p);
1205 02571aa1 2022-01-29 op if (fetch_fid(nfid, tmpfd, name)) {
1206 02571aa1 2022-01-29 op warn("failed fetch or can't write %s", sfn);
1209 9c48f915 2022-01-16 op close(tmpfd);
1211 9c48f915 2022-01-16 op spawn(ed, sfn, NULL);
1214 9c48f915 2022-01-16 op * Re-open the file because it's not guaranteed that the
1215 9c48f915 2022-01-16 op * file descriptor tmpfd is still associated with the file
1216 9c48f915 2022-01-16 op * pointed by sfn: it's not uncommon for editor to write
1217 9c48f915 2022-01-16 op * a backup file and then rename(2) it to the file name.
1219 9c48f915 2022-01-16 op if ((tmpfd = open(sfn, O_RDONLY)) == -1) {
1220 9c48f915 2022-01-16 op warn("can't open %s", sfn);
1224 9c48f915 2022-01-16 op woc_file(tmpfd, *argv, name);
1225 9c48f915 2022-01-16 op close(tmpfd);
1228 9c48f915 2022-01-16 op unlink(sfn);
1232 fb1a36c0 2022-01-09 op cmd_get(int argc, const char **argv)
1234 fb1a36c0 2022-01-09 op struct qid qid;
1235 fb1a36c0 2022-01-09 op const char *l;
1236 8f9f99f7 2022-01-16 op char *errstr;
1237 8f9f99f7 2022-01-16 op int nfid, fd, miss;
1239 fb1a36c0 2022-01-09 op if (argc != 1 && argc != 2) {
1240 fb1a36c0 2022-01-09 op printf("usage: get remote-file [local-file]\n");
1244 fb1a36c0 2022-01-09 op if (argc == 2)
1245 fb1a36c0 2022-01-09 op l = argv[1];
1246 fb1a36c0 2022-01-09 op else if ((l = strrchr(argv[0], '/')) != NULL)
1247 fb1a36c0 2022-01-09 op l++; /* skip / */
1249 2ba25711 2022-01-16 op l = argv[0];
1251 8d8fb849 2022-01-17 op nfid = nextfid();
1252 8f9f99f7 2022-01-16 op errstr = walk_path(pwdfid, nfid, argv[0], &miss, &qid);
1253 8f9f99f7 2022-01-16 op if (errstr != NULL) {
1254 8f9f99f7 2022-01-16 op printf("%s: %s\n", argv[0], errstr);
1255 8f9f99f7 2022-01-16 op free(errstr);
1259 8f9f99f7 2022-01-16 op if (miss != 0 || qid.type != 0) {
1260 8f9f99f7 2022-01-16 op printf("%s: not a file\n", argv[0]);
1261 fcd9d510 2022-01-16 op if (miss == 0)
1262 fcd9d510 2022-01-16 op do_clunk(nfid);
1266 76521d26 2022-01-16 op if ((fd = open(l, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0644)) == -1) {
1267 76521d26 2022-01-16 op warn("can't open %s", l);
1268 76521d26 2022-01-16 op do_clunk(nfid);
1272 02571aa1 2022-01-29 op if (fetch_fid(nfid, fd, l) == -1)
1273 02571aa1 2022-01-29 op warn("write %s", l);
1278 5565d021 2022-01-16 op cmd_hexdump(int argc, const char **argv)
1280 5565d021 2022-01-16 op if (argc == 0) {
1281 5565d021 2022-01-16 op xdump = !xdump;
1283 5565d021 2022-01-16 op puts("hexdump mode enabled");
1285 5565d021 2022-01-16 op puts("hexdump mode disabled");
1289 5565d021 2022-01-16 op if (argc > 1)
1292 5565d021 2022-01-16 op if (!strcmp(*argv, "on")) {
1294 5565d021 2022-01-16 op puts("hexdump mode enabled");
1298 5565d021 2022-01-16 op if (!strcmp(*argv, "off")) {
1300 5565d021 2022-01-16 op puts("hexdump mode disabled");
1305 5565d021 2022-01-16 op puts("usage: hexdump [on | off]");
1309 fb1a36c0 2022-01-09 op cmd_lcd(int argc, const char **argv)
1311 fb1a36c0 2022-01-09 op const char *dir;
1313 fb1a36c0 2022-01-09 op if (argc > 1) {
1314 d166ffc0 2022-01-16 op printf("usage: lcd [local-directory]\n");
1318 fb1a36c0 2022-01-09 op if (argc == 1)
1319 fb1a36c0 2022-01-09 op dir = *argv;
1321 fb1a36c0 2022-01-09 op if (argc == 0 && (dir = getenv("HOME")) == NULL) {
1322 fb1a36c0 2022-01-09 op printf("HOME is not defined\n");
1326 fb1a36c0 2022-01-09 op if (chdir(dir) == -1)
1327 fb1a36c0 2022-01-09 op printf("cd: %s: %s\n", dir, strerror(errno));
1331 fb1a36c0 2022-01-09 op cmd_lpwd(int argc, const char **argv)
1333 fb1a36c0 2022-01-09 op char path[PATH_MAX];
1335 d166ffc0 2022-01-16 op if (argc != 0) {
1336 d166ffc0 2022-01-16 op printf("usage: lpwd\n");
1340 fb1a36c0 2022-01-09 op if (getcwd(path, sizeof(path)) == NULL) {
1341 fb1a36c0 2022-01-09 op printf("lpwd: %s\n", strerror(errno));
1345 fb1a36c0 2022-01-09 op printf("%s\n", path);
1349 fb1a36c0 2022-01-09 op cmd_ls(int argc, const char **argv)
1351 9c9e60d1 2022-11-23 op if (argc > 1) {
1352 9c9e60d1 2022-11-23 op puts("usage: ls [path]");
1356 d3e1ab0c 2022-11-24 op dir_listing(argc == 0 ? "." : argv[0], print_dirent, 1);
1360 9e7dd230 2022-01-12 op cmd_page(int argc, const char **argv)
1362 9e7dd230 2022-01-12 op struct qid qid;
1363 02571aa1 2022-01-29 op int nfid, tmpfd, miss, r;
1364 64d8db89 2022-01-16 op char sfn[TMPFSTRLEN], p[PATH_MAX], *name, *errstr;
1365 62f663bb 2022-01-16 op const char *pager;
1367 9e7dd230 2022-01-12 op if (argc != 1) {
1368 9e7dd230 2022-01-12 op puts("usage: page file");
1372 62f663bb 2022-01-16 op if ((pager = getenv("PAGER")) == NULL)
1373 62f663bb 2022-01-16 op pager = "less";
1375 8d8fb849 2022-01-17 op nfid = nextfid();
1376 8f9f99f7 2022-01-16 op errstr = walk_path(pwdfid, nfid, *argv, &miss, &qid);
1377 8f9f99f7 2022-01-16 op if (errstr != NULL) {
1378 8f9f99f7 2022-01-16 op printf("%s: %s\n", *argv, errstr);
1379 8f9f99f7 2022-01-16 op free(errstr);
1383 8f9f99f7 2022-01-16 op if (miss != 0 || qid.type != 0) {
1384 8f9f99f7 2022-01-16 op printf("%s: not a file\n", *argv);
1385 fcd9d510 2022-01-16 op if (miss == 0)
1386 fcd9d510 2022-01-16 op do_clunk(nfid);
1390 64d8db89 2022-01-16 op if ((tmpfd = tmp_file(sfn)) == -1) {
1391 9e7dd230 2022-01-12 op do_clunk(nfid);
1395 9e7dd230 2022-01-12 op strlcpy(p, *argv, sizeof(p));
1396 9e7dd230 2022-01-12 op name = basename(p);
1397 02571aa1 2022-01-29 op if ((r = fetch_fid(nfid, tmpfd, name)) == -1)
1398 02571aa1 2022-01-29 op warn("write %s", sfn);
1399 9e7dd230 2022-01-12 op close(tmpfd);
1400 02571aa1 2022-01-29 op if (r != -1)
1401 02571aa1 2022-01-29 op spawn(pager, sfn, NULL);
1402 9e7dd230 2022-01-12 op unlink(sfn);
1406 1c9ab7cf 2022-01-29 op cmd_pipe(int argc, const char **argv)
1408 1c9ab7cf 2022-01-29 op struct qid qid;
1410 1c9ab7cf 2022-01-29 op int nfid, tmpfd, miss, status;
1411 1c9ab7cf 2022-01-29 op int filedes[2]; /* read end, write end */
1412 1c9ab7cf 2022-01-29 op char *errstr;
1414 1c9ab7cf 2022-01-29 op if (argc < 2) {
1415 1c9ab7cf 2022-01-29 op puts("usage: pipe remote-file cmd [args...]");
1419 1c9ab7cf 2022-01-29 op nfid = nextfid();
1420 1c9ab7cf 2022-01-29 op errstr = walk_path(pwdfid, nfid, *argv, &miss, &qid);
1421 1c9ab7cf 2022-01-29 op if (errstr != NULL) {
1422 1c9ab7cf 2022-01-29 op printf("%s: %s\n", *argv, errstr);
1423 1c9ab7cf 2022-01-29 op free(errstr);
1427 1c9ab7cf 2022-01-29 op if (miss != 0 || qid.type != 0) {
1428 1c9ab7cf 2022-01-29 op printf("%s: not a file\n", *argv);
1429 1c9ab7cf 2022-01-29 op if (miss == 0)
1430 1c9ab7cf 2022-01-29 op do_clunk(nfid);
1434 1c9ab7cf 2022-01-29 op if (pipe(filedes) == -1)
1435 1c9ab7cf 2022-01-29 op err(1, "pipe");
1437 1c9ab7cf 2022-01-29 op switch (pid = vfork()) {
1439 1c9ab7cf 2022-01-29 op err(1, "vfork");
1441 1c9ab7cf 2022-01-29 op close(filedes[1]);
1442 1c9ab7cf 2022-01-29 op if (dup2(filedes[0], 0) == -1)
1443 1c9ab7cf 2022-01-29 op err(1, "dup2");
1444 1c9ab7cf 2022-01-29 op execvp(argv[1], (char *const *)argv + 1);
1445 1c9ab7cf 2022-01-29 op err(1, "execvp");
1448 1c9ab7cf 2022-01-29 op close(filedes[0]);
1449 1c9ab7cf 2022-01-29 op if (fetch_fid(nfid, filedes[1], *argv) == -1)
1450 1c9ab7cf 2022-01-29 op warnx("failed to fetch all the file");
1451 1c9ab7cf 2022-01-29 op close(filedes[1]);
1453 1c9ab7cf 2022-01-29 op waitpid(pid, &status, 0);
1457 01fb39ab 2022-01-16 op cmd_put(int argc, const char **argv)
1459 01fb39ab 2022-01-16 op struct qid qid;
1460 9c48f915 2022-01-16 op const char *l;
1463 01fb39ab 2022-01-16 op if (argc != 1 && argc != 2) {
1464 01fb39ab 2022-01-16 op printf("usage: put local-file [remote-file]\n");
1468 01fb39ab 2022-01-16 op if (argc == 2)
1469 01fb39ab 2022-01-16 op l = argv[1];
1470 01fb39ab 2022-01-16 op else if ((l = strrchr(argv[0], '/')) != NULL)
1471 01fb39ab 2022-01-16 op l++; /* skip / */
1473 01fb39ab 2022-01-16 op l = argv[0];
1475 01fb39ab 2022-01-16 op if ((fd = open(argv[0], O_RDONLY)) == -1) {
1476 01fb39ab 2022-01-16 op warn("%s", argv[0]);
1480 9c48f915 2022-01-16 op woc_file(fd, argv[0], l);
1485 c37e1cfe 2022-01-17 op cmd_rename(int argc, const char **argv)
1487 c37e1cfe 2022-01-17 op struct np_stat st;
1488 c37e1cfe 2022-01-17 op struct qid qid;
1489 c37e1cfe 2022-01-17 op char *errstr;
1490 c37e1cfe 2022-01-17 op int nfid, miss;
1492 c37e1cfe 2022-01-17 op if (argc != 2) {
1493 c37e1cfe 2022-01-17 op puts("usage: rename remote-file new-remote-name");
1497 8d8fb849 2022-01-17 op nfid = nextfid();
1498 c37e1cfe 2022-01-17 op errstr = walk_path(pwdfid, nfid, argv[0], &miss, &qid);
1499 c37e1cfe 2022-01-17 op if (errstr != NULL) {
1500 c37e1cfe 2022-01-17 op printf("%s: %s\n", argv[0], errstr);
1501 c37e1cfe 2022-01-17 op free(errstr);
1505 c37e1cfe 2022-01-17 op if (miss != 0) {
1506 c37e1cfe 2022-01-17 op printf("%s: not such file or directory\n", argv[0]);
1510 c37e1cfe 2022-01-17 op prepare_wstat(&st);
1511 c37e1cfe 2022-01-17 op st.name = (char *)argv[1];
1512 c37e1cfe 2022-01-17 op if ((errstr = do_wstat(nfid, &st)) != NULL) {
1513 c37e1cfe 2022-01-17 op printf("rename: %s\n", errstr);
1514 c37e1cfe 2022-01-17 op free(errstr);
1517 c37e1cfe 2022-01-17 op do_clunk(nfid);
1521 f99c17f9 2022-11-23 op cmd_rm(int argc, const char **argv)
1523 f99c17f9 2022-11-23 op struct qid qid;
1524 f99c17f9 2022-11-23 op char *errstr;
1525 f99c17f9 2022-11-23 op int nfid, miss;
1527 f99c17f9 2022-11-23 op if (argc == 0) {
1528 f99c17f9 2022-11-23 op puts("usage: rm file ...");
1532 f99c17f9 2022-11-23 op for (; *argv; ++argv, --argc) {
1533 f99c17f9 2022-11-23 op nfid = nextfid();
1534 f99c17f9 2022-11-23 op errstr = walk_path(pwdfid, nfid, *argv, &miss, &qid);
1535 f99c17f9 2022-11-23 op if (errstr != NULL) {
1536 f99c17f9 2022-11-23 op printf("%s: %s\n", *argv, errstr);
1537 f99c17f9 2022-11-23 op free(errstr);
1541 f99c17f9 2022-11-23 op printf("%s: not such file or directory\n", *argv);
1545 f99c17f9 2022-11-23 op if ((errstr = do_remove(nfid)) != NULL) {
1546 f99c17f9 2022-11-23 op printf("%s: %s\n", *argv, errstr);
1547 f99c17f9 2022-11-23 op free(errstr);
1554 fb1a36c0 2022-01-09 op cmd_verbose(int argc, const char **argv)
1556 fb1a36c0 2022-01-09 op if (argc == 0) {
1557 fb1a36c0 2022-01-09 op log_setverbose(!log_getverbose());
1558 fb1a36c0 2022-01-09 op if (log_getverbose())
1559 fb1a36c0 2022-01-09 op puts("verbose mode enabled");
1561 fb1a36c0 2022-01-09 op puts("verbose mode disabled");
1565 fb1a36c0 2022-01-09 op if (argc != 1)
1568 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "on")) {
1569 fb1a36c0 2022-01-09 op log_setverbose(1);
1570 fb1a36c0 2022-01-09 op puts("verbose mode enabled");
1574 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "off")) {
1575 fb1a36c0 2022-01-09 op log_setverbose(0);
1576 fb1a36c0 2022-01-09 op puts("verbose mode disabled");
1581 fb1a36c0 2022-01-09 op printf("verbose [on | off]\n");
1585 fb1a36c0 2022-01-09 op excmd(int argc, const char **argv)
1589 fb1a36c0 2022-01-09 op if (argc == 0)
1592 fb1a36c0 2022-01-09 op for (i = 0; i < nitems(cmds); ++i) {
1593 fb1a36c0 2022-01-09 op if (!strcmp(cmds[i].name, *argv)) {
1594 fb1a36c0 2022-01-09 op cmds[i].fn(argc-1, argv+1);
1599 fb1a36c0 2022-01-09 op log_warnx("unknown command %s", *argv);
1603 d26225c3 2022-12-21 op parsecmd(char *cmd, char **argv, int len)
1605 55e8c065 2022-11-23 op int escape, quote;
1606 55e8c065 2022-11-23 op int argc = 0;
1608 55e8c065 2022-11-23 op memset(argv, 0, sizeof(*argv) * len);
1610 55e8c065 2022-11-23 op while (argc < len) {
1611 55e8c065 2022-11-23 op while (isspace((unsigned char)*cmd))
1613 55e8c065 2022-11-23 op if (*cmd == '\0')
1616 55e8c065 2022-11-23 op argv[argc++] = cmd;
1617 55e8c065 2022-11-23 op escape = quote = 0;
1618 55e8c065 2022-11-23 op for (; *cmd != '\0'; ++cmd) {
1619 55e8c065 2022-11-23 op if (escape) {
1623 55e8c065 2022-11-23 op if (*cmd == '\\') {
1625 55e8c065 2022-11-23 op memmove(cmd, cmd + 1, strlen(cmd));
1629 55e8c065 2022-11-23 op if (*cmd == quote) {
1631 55e8c065 2022-11-23 op memmove(cmd, cmd + 1, strlen(cmd));
1635 55e8c065 2022-11-23 op if (*cmd == '\'' || *cmd == '"') {
1636 55e8c065 2022-11-23 op quote = *cmd;
1637 55e8c065 2022-11-23 op memmove(cmd, cmd + 1, strlen(cmd));
1644 55e8c065 2022-11-23 op if (isspace((unsigned char)*cmd))
1648 55e8c065 2022-11-23 op if (*cmd == '\0' && (escape || quote)) {
1649 55e8c065 2022-11-23 op fprintf(stderr, "unterminated %s\n",
1650 55e8c065 2022-11-23 op escape ? "escape" : "quote");
1654 55e8c065 2022-11-23 op if (*cmd == '\0')
1656 55e8c065 2022-11-23 op *cmd++ = '\0';
1659 55e8c065 2022-11-23 op if (*cmd != '\0') {
1660 55e8c065 2022-11-23 op fprintf(stderr, "too many arguments\n");
1663 55e8c065 2022-11-23 op return argc;
1667 df9bb54d 2022-11-23 op cd_or_fetch(const char *path, const char *outfile)
1669 14ae3944 2022-11-23 op struct qid qid;
1670 14ae3944 2022-11-23 op char *errstr;
1671 14ae3944 2022-11-23 op int fd, nfid, miss;
1673 14ae3944 2022-11-23 op while (*path == '/')
1675 14ae3944 2022-11-23 op if (*path == '\0')
1678 14ae3944 2022-11-23 op nfid = nextfid();
1679 14ae3944 2022-11-23 op errstr = walk_path(pwdfid, nfid, path, &miss, &qid);
1681 14ae3944 2022-11-23 op errx(1, "walk %s: %s", path, errstr);
1683 14ae3944 2022-11-23 op errc(1, ENOENT, "walk %s", path);
1685 14ae3944 2022-11-23 op if (qid.type & QTDIR) {
1686 df9bb54d 2022-11-23 op if (outfile)
1687 df9bb54d 2022-11-23 op errx(1, "can't fetch directory %s", path);
1688 14ae3944 2022-11-23 op do_clunk(pwdfid);
1689 14ae3944 2022-11-23 op pwdfid = nfid;
1693 df9bb54d 2022-11-23 op if (outfile == NULL) {
1694 df9bb54d 2022-11-23 op if ((outfile = strrchr(path, '/')) == NULL)
1695 df9bb54d 2022-11-23 op outfile = path;
1698 df9bb54d 2022-11-23 op if (*outfile == '\0')
1699 df9bb54d 2022-11-23 op errx(1, "invalid path: missing file name: %s",
1703 df9bb54d 2022-11-23 op if (strcmp(outfile, "-") != 0) {
1704 df9bb54d 2022-11-23 op fd = open(outfile, O_WRONLY|O_CREAT, 0644);
1705 df9bb54d 2022-11-23 op if (fd == -1)
1706 df9bb54d 2022-11-23 op err(1, "can't open for writing %s", outfile);
1710 df9bb54d 2022-11-23 op if (fetch_fid(nfid, fd, outfile) == -1)
1711 df9bb54d 2022-11-23 op err(1, "write %s", outfile);
1716 14ae3944 2022-11-23 op static const char *
1717 14ae3944 2022-11-23 op parse_addr(const char *url, const char **user,
1718 14ae3944 2022-11-23 op const char **port, const char **path)
1720 14ae3944 2022-11-23 op static char buf[PATH_MAX];
1721 14ae3944 2022-11-23 op char *host, *t;
1723 14ae3944 2022-11-23 op *user = *port = *path = NULL;
1726 14ae3944 2022-11-23 op if (strlcpy(buf, url, sizeof(buf)) >= sizeof(buf))
1727 14ae3944 2022-11-23 op errx(1, "connection string too long");
1729 9f134f93 2022-11-23 op if (!strncmp(host, "9p://", 5))
1732 14ae3944 2022-11-23 op if ((t = strchr(host, '/')) != NULL) {
1733 14ae3944 2022-11-23 op if (t == host)
1734 14ae3944 2022-11-23 op errx(1, "invalid connection string: %s", url);
1735 14ae3944 2022-11-23 op *t++ = '\0';
1736 14ae3944 2022-11-23 op if (*t != '\0')
1740 14ae3944 2022-11-23 op if ((t = strchr(host, '@')) != NULL) {
1741 14ae3944 2022-11-23 op if (t == host)
1742 14ae3944 2022-11-23 op errx(1, "invalid connection string: %s", url);
1743 14ae3944 2022-11-23 op *t++ = '\0';
1744 14ae3944 2022-11-23 op *user = host;
1746 14ae3944 2022-11-23 op } else if ((*user = getenv("USER")) == NULL)
1747 14ae3944 2022-11-23 op errx(1, "USER not defined");
1749 14ae3944 2022-11-23 op if ((t = strchr(host, ':')) != NULL) {
1750 14ae3944 2022-11-23 op *t++ = '\0';
1751 14ae3944 2022-11-23 op if (*t != '\0')
1754 14ae3944 2022-11-23 op if (*port == NULL)
1755 14ae3944 2022-11-23 op *port = "1337";
1757 14ae3944 2022-11-23 op return host;
1761 fb1a36c0 2022-01-09 op main(int argc, char **argv)
1763 14ae3944 2022-11-23 op const char *user, *host, *port, *path;
1764 df9bb54d 2022-11-23 op const char *outfile = NULL;
1767 fb1a36c0 2022-01-09 op log_init(1, LOG_DAEMON);
1768 fb1a36c0 2022-01-09 op log_setverbose(0);
1769 fb1a36c0 2022-01-09 op log_procinit(getprogname());
1771 df9bb54d 2022-11-23 op while ((ch = getopt(argc, argv, "C:cK:o:")) != -1) {
1772 fb1a36c0 2022-01-09 op switch (ch) {
1775 fb1a36c0 2022-01-09 op crtpath = optarg;
1777 ab9f44f7 2022-11-23 op case 'c': /* deprecated, remove after 0.3 */
1782 fb1a36c0 2022-01-09 op keypath = optarg;
1785 df9bb54d 2022-11-23 op outfile = optarg;
1791 fb1a36c0 2022-01-09 op argc -= optind;
1792 fb1a36c0 2022-01-09 op argv += optind;
1794 3847d69e 2022-01-30 op if (argc == 0 || (tls && crtpath == NULL))
1797 14ae3944 2022-11-23 op host = parse_addr(argv[0], &user, &port, &path);
1798 514e8d43 2022-11-23 op if (path == NULL && argv[1] != NULL) /* drop argv[1] after 0.3 */
1799 14ae3944 2022-11-23 op path = argv[1];
1800 df9bb54d 2022-11-23 op if (outfile && path == NULL)
1803 1c9ab7cf 2022-01-29 op signal(SIGPIPE, SIG_IGN);
1804 fb1a36c0 2022-01-09 op if (isatty(1)) {
1806 fb1a36c0 2022-01-09 op resized = 1;
1807 fb1a36c0 2022-01-09 op signal(SIGWINCH, tty_resized);
1810 fb1a36c0 2022-01-09 op if ((evb = evbuffer_new()) == NULL)
1811 fb1a36c0 2022-01-09 op fatal("evbuffer_new");
1813 fb1a36c0 2022-01-09 op if ((buf = evbuffer_new()) == NULL)
1814 fb1a36c0 2022-01-09 op fatal("evbuffer_new");
1816 fb1a36c0 2022-01-09 op if ((dirbuf = evbuffer_new()) == NULL)
1817 fb1a36c0 2022-01-09 op fatal("evbuferr_new");
1819 14ae3944 2022-11-23 op do_connect(host, port, user);
1821 df9bb54d 2022-11-23 op cd_or_fetch(path, outfile);
1823 d3e1ab0c 2022-11-24 op compl_setup();
1826 fb1a36c0 2022-01-09 op char *line, *argv[16] = {0}, **ap;
1828 fb1a36c0 2022-01-09 op if ((line = read_line("kamiftp> ")) == NULL)
1831 55e8c065 2022-11-23 op if ((argc = parsecmd(line, argv, nitems(argv) - 1)) == -1) {
1836 55e8c065 2022-11-23 op argv[argc] = NULL;
1837 fb1a36c0 2022-01-09 op excmd(argc, (const char **)argv);
1840 ca14d066 2022-11-23 op fprintf(stderr, "\a");
1845 fb1a36c0 2022-01-09 op printf("\n");