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 5d0dedb3 2022-11-23 op fprintf(stderr, "connecting to %s:%s...", host, port);
915 58b141cf 2022-12-21 op do_tls_connect(host, port);
917 58b141cf 2022-12-21 op do_ctxt_connect(host, port);
919 5d0dedb3 2022-11-23 op fprintf(stderr, " done!\n");
921 fb1a36c0 2022-01-09 op do_version();
922 14ae3944 2022-11-23 op do_attach(user);
926 64d8db89 2022-01-16 op tmp_file(char sfn[TMPFSTRLEN])
930 64d8db89 2022-01-16 op strlcpy(sfn, TMPFSTR, TMPFSTRLEN);
931 64d8db89 2022-01-16 op if ((tmpfd = mkstemp(sfn)) == -1) {
932 64d8db89 2022-01-16 op warn("mkstemp %s", sfn);
936 aa76fd7f 2022-01-16 op /* set the close-on-exec flag */
937 aa76fd7f 2022-01-16 op if (fcntl(tmpfd, F_SETFD, FD_CLOEXEC) == -1) {
938 aa76fd7f 2022-01-16 op warn("fcntl");
939 aa76fd7f 2022-01-16 op close(tmpfd);
943 64d8db89 2022-01-16 op return tmpfd;
946 1f5fb843 2022-01-16 op static inline const char *
947 1f5fb843 2022-01-16 op pp_perm(uint8_t x)
949 1f5fb843 2022-01-16 op switch (x & 0x7) {
951 1f5fb843 2022-01-16 op return "---";
953 1f5fb843 2022-01-16 op return "--x";
955 1f5fb843 2022-01-16 op return "-w-";
957 1f5fb843 2022-01-16 op return "-wx";
959 1f5fb843 2022-01-16 op return "r--";
961 1f5fb843 2022-01-16 op return "r-x";
963 1f5fb843 2022-01-16 op return "rw-";
965 1f5fb843 2022-01-16 op return "rwx";
967 1f5fb843 2022-01-16 op /* unreachable, just for the compiler' happiness */
968 1f5fb843 2022-01-16 op return "???";
972 c37e1cfe 2022-01-17 op static inline void
973 c37e1cfe 2022-01-17 op prepare_wstat(struct np_stat *st)
975 c37e1cfe 2022-01-17 op memset(st, 0xFF, sizeof(*st));
976 c37e1cfe 2022-01-17 op st->name = NULL;
977 c37e1cfe 2022-01-17 op st->uid = NULL;
978 c37e1cfe 2022-01-17 op st->gid = NULL;
979 c37e1cfe 2022-01-17 op st->muid = NULL;
983 d3e1ab0c 2022-11-24 op print_dirent(const struct np_stat *st)
985 d3e1ab0c 2022-11-24 op time_t mtime;
986 d3e1ab0c 2022-11-24 op struct tm *tm;
987 d3e1ab0c 2022-11-24 op const char *timfmt;
988 d3e1ab0c 2022-11-24 op char fmt[FMT_SCALED_STRSIZE], tim[13];
990 d3e1ab0c 2022-11-24 op if (fmt_scaled(st->length, fmt) == -1)
991 d3e1ab0c 2022-11-24 op strlcpy(fmt, "xxx", sizeof(fmt));
993 d3e1ab0c 2022-11-24 op mtime = st->mtime;
995 d3e1ab0c 2022-11-24 op if (now > mtime && (now - mtime) < 365/2 * 24 * 12 * 60)
996 d3e1ab0c 2022-11-24 op timfmt = "%b %e %R";
998 d3e1ab0c 2022-11-24 op timfmt = "%b %e %Y";
1000 d3e1ab0c 2022-11-24 op if ((tm = localtime(&mtime)) == NULL ||
1001 d3e1ab0c 2022-11-24 op strftime(tim, sizeof(tim), timfmt, tm) == 0)
1002 d3e1ab0c 2022-11-24 op strlcpy(tim, "unknown", sizeof(tim));
1004 d3e1ab0c 2022-11-24 op if (st->qid.type & QTDIR)
1005 d3e1ab0c 2022-11-24 op printf("d");
1007 d3e1ab0c 2022-11-24 op printf("-");
1008 d3e1ab0c 2022-11-24 op printf("%s", pp_perm(st->mode >> 6));
1009 d3e1ab0c 2022-11-24 op printf("%s", pp_perm(st->mode >> 3));
1010 d3e1ab0c 2022-11-24 op printf("%s", pp_perm(st->mode));
1011 d3e1ab0c 2022-11-24 op printf(" %8s %12s %s%s\n", fmt, tim, st->name,
1012 d3e1ab0c 2022-11-24 op st->qid.type & QTDIR ? "/" : "");
1018 d3e1ab0c 2022-11-24 op dir_listing(const char *path, int (*fn)(const struct np_stat *),
1019 d3e1ab0c 2022-11-24 op int printerr)
1021 12bfa80e 2022-12-21 op struct qid qid = {0, 0, QTDIR};
1022 d3e1ab0c 2022-11-24 op struct np_stat st;
1023 d3e1ab0c 2022-11-24 op uint64_t off = 0;
1024 d3e1ab0c 2022-11-24 op uint32_t len;
1025 12bfa80e 2022-12-21 op int nfid, r, miss = 0;
1026 d3e1ab0c 2022-11-24 op char *errstr;
1028 d3e1ab0c 2022-11-24 op now = time(NULL);
1029 d3e1ab0c 2022-11-24 op nfid = nextfid();
1031 12bfa80e 2022-12-21 op if (!strcmp(path, "."))
1032 12bfa80e 2022-12-21 op errstr = dup_fid(pwdfid, nfid);
1034 12bfa80e 2022-12-21 op errstr = walk_path(pwdfid, nfid, path, &miss, &qid);
1035 d3e1ab0c 2022-11-24 op if (errstr != NULL) {
1036 d3e1ab0c 2022-11-24 op if (printerr)
1037 d3e1ab0c 2022-11-24 op printf("%s: %s\n", path, errstr);
1038 d3e1ab0c 2022-11-24 op free(errstr);
1042 d3e1ab0c 2022-11-24 op if (printerr)
1043 d3e1ab0c 2022-11-24 op printf("%s: No such file or directory\n", path);
1046 d3e1ab0c 2022-11-24 op if (!(qid.type & QTDIR)) {
1047 d3e1ab0c 2022-11-24 op if (printerr)
1048 d3e1ab0c 2022-11-24 op printf("%s: not a directory\n", path);
1049 d3e1ab0c 2022-11-24 op do_clunk(nfid);
1053 d3e1ab0c 2022-11-24 op do_open(nfid, KOREAD);
1054 d3e1ab0c 2022-11-24 op evbuffer_drain(dirbuf, EVBUFFER_LENGTH(dirbuf));
1057 d3e1ab0c 2022-11-24 op tread(nfid, off, msize - IOHDRSZ);
1060 d3e1ab0c 2022-11-24 op expect2(Rread, iota_tag);
1062 d3e1ab0c 2022-11-24 op len = np_read32(buf);
1063 d3e1ab0c 2022-11-24 op if (len == 0)
1066 d3e1ab0c 2022-11-24 op evbuffer_add_buffer(dirbuf, buf);
1069 d3e1ab0c 2022-11-24 op ASSERT_EMPTYBUF();
1072 d3e1ab0c 2022-11-24 op while (EVBUFFER_LENGTH(dirbuf) != 0) {
1073 d3e1ab0c 2022-11-24 op if (np_read_stat(dirbuf, &st) == -1)
1074 d3e1ab0c 2022-11-24 op errx(1, "invalid stat struct read");
1076 d3e1ab0c 2022-11-24 op r = fn(&st);
1078 d3e1ab0c 2022-11-24 op free(st.name);
1079 d3e1ab0c 2022-11-24 op free(st.uid);
1080 d3e1ab0c 2022-11-24 op free(st.gid);
1081 d3e1ab0c 2022-11-24 op free(st.muid);
1083 d3e1ab0c 2022-11-24 op if (r == -1)
1087 d3e1ab0c 2022-11-24 op evbuffer_drain(dirbuf, EVBUFFER_LENGTH(dirbuf));
1088 d3e1ab0c 2022-11-24 op do_clunk(nfid);
1093 fb1a36c0 2022-01-09 op cmd_bell(int argc, const char **argv)
1095 fb1a36c0 2022-01-09 op if (argc == 0) {
1096 fb1a36c0 2022-01-09 op bell = !bell;
1098 fb1a36c0 2022-01-09 op puts("bell mode enabled");
1100 fb1a36c0 2022-01-09 op puts("bell mode disabled");
1104 fb1a36c0 2022-01-09 op if (argc != 1)
1107 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "on")) {
1109 fb1a36c0 2022-01-09 op puts("bell mode enabled");
1113 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "off")) {
1115 fb1a36c0 2022-01-09 op puts("bell mode disabled");
1120 fb1a36c0 2022-01-09 op printf("bell [on | off]\n");
1124 fb1a36c0 2022-01-09 op cmd_bye(int argc, const char **argv)
1126 fb1a36c0 2022-01-09 op log_warnx("bye\n");
1131 fb1a36c0 2022-01-09 op cmd_cd(int argc, const char **argv)
1133 fb1a36c0 2022-01-09 op struct qid qid;
1134 8f9f99f7 2022-01-16 op int nfid, miss;
1135 8f9f99f7 2022-01-16 op char *errstr;
1137 fb1a36c0 2022-01-09 op if (argc != 1) {
1138 fb1a36c0 2022-01-09 op printf("usage: cd remote-path\n");
1142 8d8fb849 2022-01-17 op nfid = nextfid();
1143 8f9f99f7 2022-01-16 op errstr = walk_path(pwdfid, nfid, argv[0], &miss, &qid);
1144 8f9f99f7 2022-01-16 op if (errstr != NULL) {
1145 8f9f99f7 2022-01-16 op printf("%s: %s\n", argv[0], errstr);
1146 8f9f99f7 2022-01-16 op free(errstr);
1150 8f9f99f7 2022-01-16 op if (miss != 0 || !(qid.type & QTDIR)) {
1151 8f9f99f7 2022-01-16 op printf("%s: not a directory\n", argv[0]);
1152 fcd9d510 2022-01-16 op if (miss == 0)
1153 fcd9d510 2022-01-16 op do_clunk(nfid);
1157 8f9f99f7 2022-01-16 op do_clunk(pwdfid);
1158 8f9f99f7 2022-01-16 op pwdfid = nfid;
1162 9c48f915 2022-01-16 op cmd_edit(int argc, const char **argv)
1164 9c48f915 2022-01-16 op struct qid qid;
1165 9c48f915 2022-01-16 op int nfid, tmpfd, miss;
1166 9c48f915 2022-01-16 op char sfn[TMPFSTRLEN], p[PATH_MAX], *name, *errstr;
1167 9c48f915 2022-01-16 op const char *ed;
1169 9c48f915 2022-01-16 op if (argc != 1) {
1170 9c48f915 2022-01-16 op puts("usage: edit file");
1174 9c48f915 2022-01-16 op if ((ed = getenv("VISUAL")) == NULL &&
1175 9c48f915 2022-01-16 op (ed = getenv("EDITOR")) == NULL)
1178 8d8fb849 2022-01-17 op nfid = nextfid();
1179 9c48f915 2022-01-16 op errstr = walk_path(pwdfid, nfid, *argv, &miss, &qid);
1180 9c48f915 2022-01-16 op if (errstr != NULL) {
1181 9c48f915 2022-01-16 op printf("%s: %s\n", *argv, errstr);
1182 9c48f915 2022-01-16 op free(errstr);
1186 9c48f915 2022-01-16 op if (miss != 0 || qid.type != 0) {
1187 9c48f915 2022-01-16 op printf("%s: not a file\n", *argv);
1188 9c48f915 2022-01-16 op if (miss == 0)
1189 9c48f915 2022-01-16 op do_clunk(nfid);
1193 9c48f915 2022-01-16 op if ((tmpfd = tmp_file(sfn)) == -1) {
1194 9c48f915 2022-01-16 op do_clunk(nfid);
1198 9c48f915 2022-01-16 op strlcpy(p, *argv, sizeof(p));
1199 9c48f915 2022-01-16 op name = basename(p);
1201 02571aa1 2022-01-29 op if (fetch_fid(nfid, tmpfd, name)) {
1202 02571aa1 2022-01-29 op warn("failed fetch or can't write %s", sfn);
1205 9c48f915 2022-01-16 op close(tmpfd);
1207 9c48f915 2022-01-16 op spawn(ed, sfn, NULL);
1210 9c48f915 2022-01-16 op * Re-open the file because it's not guaranteed that the
1211 9c48f915 2022-01-16 op * file descriptor tmpfd is still associated with the file
1212 9c48f915 2022-01-16 op * pointed by sfn: it's not uncommon for editor to write
1213 9c48f915 2022-01-16 op * a backup file and then rename(2) it to the file name.
1215 9c48f915 2022-01-16 op if ((tmpfd = open(sfn, O_RDONLY)) == -1) {
1216 9c48f915 2022-01-16 op warn("can't open %s", sfn);
1220 9c48f915 2022-01-16 op woc_file(tmpfd, *argv, name);
1221 9c48f915 2022-01-16 op close(tmpfd);
1224 9c48f915 2022-01-16 op unlink(sfn);
1228 fb1a36c0 2022-01-09 op cmd_get(int argc, const char **argv)
1230 fb1a36c0 2022-01-09 op struct qid qid;
1231 fb1a36c0 2022-01-09 op const char *l;
1232 8f9f99f7 2022-01-16 op char *errstr;
1233 8f9f99f7 2022-01-16 op int nfid, fd, miss;
1235 fb1a36c0 2022-01-09 op if (argc != 1 && argc != 2) {
1236 fb1a36c0 2022-01-09 op printf("usage: get remote-file [local-file]\n");
1240 fb1a36c0 2022-01-09 op if (argc == 2)
1241 fb1a36c0 2022-01-09 op l = argv[1];
1242 fb1a36c0 2022-01-09 op else if ((l = strrchr(argv[0], '/')) != NULL)
1243 fb1a36c0 2022-01-09 op l++; /* skip / */
1245 2ba25711 2022-01-16 op l = argv[0];
1247 8d8fb849 2022-01-17 op nfid = nextfid();
1248 8f9f99f7 2022-01-16 op errstr = walk_path(pwdfid, nfid, argv[0], &miss, &qid);
1249 8f9f99f7 2022-01-16 op if (errstr != NULL) {
1250 8f9f99f7 2022-01-16 op printf("%s: %s\n", argv[0], errstr);
1251 8f9f99f7 2022-01-16 op free(errstr);
1255 8f9f99f7 2022-01-16 op if (miss != 0 || qid.type != 0) {
1256 8f9f99f7 2022-01-16 op printf("%s: not a file\n", argv[0]);
1257 fcd9d510 2022-01-16 op if (miss == 0)
1258 fcd9d510 2022-01-16 op do_clunk(nfid);
1262 76521d26 2022-01-16 op if ((fd = open(l, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0644)) == -1) {
1263 76521d26 2022-01-16 op warn("can't open %s", l);
1264 76521d26 2022-01-16 op do_clunk(nfid);
1268 02571aa1 2022-01-29 op if (fetch_fid(nfid, fd, l) == -1)
1269 02571aa1 2022-01-29 op warn("write %s", l);
1274 5565d021 2022-01-16 op cmd_hexdump(int argc, const char **argv)
1276 5565d021 2022-01-16 op if (argc == 0) {
1277 5565d021 2022-01-16 op xdump = !xdump;
1279 5565d021 2022-01-16 op puts("hexdump mode enabled");
1281 5565d021 2022-01-16 op puts("hexdump mode disabled");
1285 5565d021 2022-01-16 op if (argc > 1)
1288 5565d021 2022-01-16 op if (!strcmp(*argv, "on")) {
1290 5565d021 2022-01-16 op puts("hexdump mode enabled");
1294 5565d021 2022-01-16 op if (!strcmp(*argv, "off")) {
1296 5565d021 2022-01-16 op puts("hexdump mode disabled");
1301 5565d021 2022-01-16 op puts("usage: hexdump [on | off]");
1305 fb1a36c0 2022-01-09 op cmd_lcd(int argc, const char **argv)
1307 fb1a36c0 2022-01-09 op const char *dir;
1309 fb1a36c0 2022-01-09 op if (argc > 1) {
1310 d166ffc0 2022-01-16 op printf("usage: lcd [local-directory]\n");
1314 fb1a36c0 2022-01-09 op if (argc == 1)
1315 fb1a36c0 2022-01-09 op dir = *argv;
1317 fb1a36c0 2022-01-09 op if (argc == 0 && (dir = getenv("HOME")) == NULL) {
1318 fb1a36c0 2022-01-09 op printf("HOME is not defined\n");
1322 fb1a36c0 2022-01-09 op if (chdir(dir) == -1)
1323 fb1a36c0 2022-01-09 op printf("cd: %s: %s\n", dir, strerror(errno));
1327 fb1a36c0 2022-01-09 op cmd_lpwd(int argc, const char **argv)
1329 fb1a36c0 2022-01-09 op char path[PATH_MAX];
1331 d166ffc0 2022-01-16 op if (argc != 0) {
1332 d166ffc0 2022-01-16 op printf("usage: lpwd\n");
1336 fb1a36c0 2022-01-09 op if (getcwd(path, sizeof(path)) == NULL) {
1337 fb1a36c0 2022-01-09 op printf("lpwd: %s\n", strerror(errno));
1341 fb1a36c0 2022-01-09 op printf("%s\n", path);
1345 fb1a36c0 2022-01-09 op cmd_ls(int argc, const char **argv)
1347 9c9e60d1 2022-11-23 op if (argc > 1) {
1348 9c9e60d1 2022-11-23 op puts("usage: ls [path]");
1352 d3e1ab0c 2022-11-24 op dir_listing(argc == 0 ? "." : argv[0], print_dirent, 1);
1356 9e7dd230 2022-01-12 op cmd_page(int argc, const char **argv)
1358 9e7dd230 2022-01-12 op struct qid qid;
1359 02571aa1 2022-01-29 op int nfid, tmpfd, miss, r;
1360 64d8db89 2022-01-16 op char sfn[TMPFSTRLEN], p[PATH_MAX], *name, *errstr;
1361 62f663bb 2022-01-16 op const char *pager;
1363 9e7dd230 2022-01-12 op if (argc != 1) {
1364 9e7dd230 2022-01-12 op puts("usage: page file");
1368 62f663bb 2022-01-16 op if ((pager = getenv("PAGER")) == NULL)
1369 62f663bb 2022-01-16 op pager = "less";
1371 8d8fb849 2022-01-17 op nfid = nextfid();
1372 8f9f99f7 2022-01-16 op errstr = walk_path(pwdfid, nfid, *argv, &miss, &qid);
1373 8f9f99f7 2022-01-16 op if (errstr != NULL) {
1374 8f9f99f7 2022-01-16 op printf("%s: %s\n", *argv, errstr);
1375 8f9f99f7 2022-01-16 op free(errstr);
1379 8f9f99f7 2022-01-16 op if (miss != 0 || qid.type != 0) {
1380 8f9f99f7 2022-01-16 op printf("%s: not a file\n", *argv);
1381 fcd9d510 2022-01-16 op if (miss == 0)
1382 fcd9d510 2022-01-16 op do_clunk(nfid);
1386 64d8db89 2022-01-16 op if ((tmpfd = tmp_file(sfn)) == -1) {
1387 9e7dd230 2022-01-12 op do_clunk(nfid);
1391 9e7dd230 2022-01-12 op strlcpy(p, *argv, sizeof(p));
1392 9e7dd230 2022-01-12 op name = basename(p);
1393 02571aa1 2022-01-29 op if ((r = fetch_fid(nfid, tmpfd, name)) == -1)
1394 02571aa1 2022-01-29 op warn("write %s", sfn);
1395 9e7dd230 2022-01-12 op close(tmpfd);
1396 02571aa1 2022-01-29 op if (r != -1)
1397 02571aa1 2022-01-29 op spawn(pager, sfn, NULL);
1398 9e7dd230 2022-01-12 op unlink(sfn);
1402 1c9ab7cf 2022-01-29 op cmd_pipe(int argc, const char **argv)
1404 1c9ab7cf 2022-01-29 op struct qid qid;
1406 3cf47875 2022-12-21 op int nfid, miss, status;
1407 1c9ab7cf 2022-01-29 op int filedes[2]; /* read end, write end */
1408 1c9ab7cf 2022-01-29 op char *errstr;
1410 1c9ab7cf 2022-01-29 op if (argc < 2) {
1411 1c9ab7cf 2022-01-29 op puts("usage: pipe remote-file cmd [args...]");
1415 1c9ab7cf 2022-01-29 op nfid = nextfid();
1416 1c9ab7cf 2022-01-29 op errstr = walk_path(pwdfid, nfid, *argv, &miss, &qid);
1417 1c9ab7cf 2022-01-29 op if (errstr != NULL) {
1418 1c9ab7cf 2022-01-29 op printf("%s: %s\n", *argv, errstr);
1419 1c9ab7cf 2022-01-29 op free(errstr);
1423 1c9ab7cf 2022-01-29 op if (miss != 0 || qid.type != 0) {
1424 1c9ab7cf 2022-01-29 op printf("%s: not a file\n", *argv);
1425 1c9ab7cf 2022-01-29 op if (miss == 0)
1426 1c9ab7cf 2022-01-29 op do_clunk(nfid);
1430 1c9ab7cf 2022-01-29 op if (pipe(filedes) == -1)
1431 1c9ab7cf 2022-01-29 op err(1, "pipe");
1433 1c9ab7cf 2022-01-29 op switch (pid = vfork()) {
1435 1c9ab7cf 2022-01-29 op err(1, "vfork");
1437 1c9ab7cf 2022-01-29 op close(filedes[1]);
1438 1c9ab7cf 2022-01-29 op if (dup2(filedes[0], 0) == -1)
1439 1c9ab7cf 2022-01-29 op err(1, "dup2");
1440 1c9ab7cf 2022-01-29 op execvp(argv[1], (char *const *)argv + 1);
1441 1c9ab7cf 2022-01-29 op err(1, "execvp");
1444 1c9ab7cf 2022-01-29 op close(filedes[0]);
1445 1c9ab7cf 2022-01-29 op if (fetch_fid(nfid, filedes[1], *argv) == -1)
1446 1c9ab7cf 2022-01-29 op warnx("failed to fetch all the file");
1447 1c9ab7cf 2022-01-29 op close(filedes[1]);
1449 1c9ab7cf 2022-01-29 op waitpid(pid, &status, 0);
1453 01fb39ab 2022-01-16 op cmd_put(int argc, const char **argv)
1455 9c48f915 2022-01-16 op const char *l;
1458 01fb39ab 2022-01-16 op if (argc != 1 && argc != 2) {
1459 01fb39ab 2022-01-16 op printf("usage: put local-file [remote-file]\n");
1463 01fb39ab 2022-01-16 op if (argc == 2)
1464 01fb39ab 2022-01-16 op l = argv[1];
1465 01fb39ab 2022-01-16 op else if ((l = strrchr(argv[0], '/')) != NULL)
1466 01fb39ab 2022-01-16 op l++; /* skip / */
1468 01fb39ab 2022-01-16 op l = argv[0];
1470 01fb39ab 2022-01-16 op if ((fd = open(argv[0], O_RDONLY)) == -1) {
1471 01fb39ab 2022-01-16 op warn("%s", argv[0]);
1475 9c48f915 2022-01-16 op woc_file(fd, argv[0], l);
1480 c37e1cfe 2022-01-17 op cmd_rename(int argc, const char **argv)
1482 c37e1cfe 2022-01-17 op struct np_stat st;
1483 c37e1cfe 2022-01-17 op struct qid qid;
1484 c37e1cfe 2022-01-17 op char *errstr;
1485 c37e1cfe 2022-01-17 op int nfid, miss;
1487 c37e1cfe 2022-01-17 op if (argc != 2) {
1488 c37e1cfe 2022-01-17 op puts("usage: rename remote-file new-remote-name");
1492 8d8fb849 2022-01-17 op nfid = nextfid();
1493 c37e1cfe 2022-01-17 op errstr = walk_path(pwdfid, nfid, argv[0], &miss, &qid);
1494 c37e1cfe 2022-01-17 op if (errstr != NULL) {
1495 c37e1cfe 2022-01-17 op printf("%s: %s\n", argv[0], errstr);
1496 c37e1cfe 2022-01-17 op free(errstr);
1500 c37e1cfe 2022-01-17 op if (miss != 0) {
1501 c37e1cfe 2022-01-17 op printf("%s: not such file or directory\n", argv[0]);
1505 c37e1cfe 2022-01-17 op prepare_wstat(&st);
1506 c37e1cfe 2022-01-17 op st.name = (char *)argv[1];
1507 c37e1cfe 2022-01-17 op if ((errstr = do_wstat(nfid, &st)) != NULL) {
1508 c37e1cfe 2022-01-17 op printf("rename: %s\n", errstr);
1509 c37e1cfe 2022-01-17 op free(errstr);
1512 c37e1cfe 2022-01-17 op do_clunk(nfid);
1516 f99c17f9 2022-11-23 op cmd_rm(int argc, const char **argv)
1518 f99c17f9 2022-11-23 op struct qid qid;
1519 f99c17f9 2022-11-23 op char *errstr;
1520 f99c17f9 2022-11-23 op int nfid, miss;
1522 f99c17f9 2022-11-23 op if (argc == 0) {
1523 f99c17f9 2022-11-23 op puts("usage: rm file ...");
1527 f99c17f9 2022-11-23 op for (; *argv; ++argv, --argc) {
1528 f99c17f9 2022-11-23 op nfid = nextfid();
1529 f99c17f9 2022-11-23 op errstr = walk_path(pwdfid, nfid, *argv, &miss, &qid);
1530 f99c17f9 2022-11-23 op if (errstr != NULL) {
1531 f99c17f9 2022-11-23 op printf("%s: %s\n", *argv, errstr);
1532 f99c17f9 2022-11-23 op free(errstr);
1536 f99c17f9 2022-11-23 op printf("%s: not such file or directory\n", *argv);
1540 f99c17f9 2022-11-23 op if ((errstr = do_remove(nfid)) != NULL) {
1541 f99c17f9 2022-11-23 op printf("%s: %s\n", *argv, errstr);
1542 f99c17f9 2022-11-23 op free(errstr);
1549 fb1a36c0 2022-01-09 op cmd_verbose(int argc, const char **argv)
1551 fb1a36c0 2022-01-09 op if (argc == 0) {
1552 fb1a36c0 2022-01-09 op log_setverbose(!log_getverbose());
1553 fb1a36c0 2022-01-09 op if (log_getverbose())
1554 fb1a36c0 2022-01-09 op puts("verbose mode enabled");
1556 fb1a36c0 2022-01-09 op puts("verbose mode disabled");
1560 fb1a36c0 2022-01-09 op if (argc != 1)
1563 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "on")) {
1564 fb1a36c0 2022-01-09 op log_setverbose(1);
1565 fb1a36c0 2022-01-09 op puts("verbose mode enabled");
1569 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "off")) {
1570 fb1a36c0 2022-01-09 op log_setverbose(0);
1571 fb1a36c0 2022-01-09 op puts("verbose mode disabled");
1576 fb1a36c0 2022-01-09 op printf("verbose [on | off]\n");
1580 fb1a36c0 2022-01-09 op excmd(int argc, const char **argv)
1584 fb1a36c0 2022-01-09 op if (argc == 0)
1587 fb1a36c0 2022-01-09 op for (i = 0; i < nitems(cmds); ++i) {
1588 fb1a36c0 2022-01-09 op if (!strcmp(cmds[i].name, *argv)) {
1589 fb1a36c0 2022-01-09 op cmds[i].fn(argc-1, argv+1);
1594 fb1a36c0 2022-01-09 op log_warnx("unknown command %s", *argv);
1598 d26225c3 2022-12-21 op parsecmd(char *cmd, char **argv, int len)
1600 55e8c065 2022-11-23 op int escape, quote;
1601 55e8c065 2022-11-23 op int argc = 0;
1603 55e8c065 2022-11-23 op memset(argv, 0, sizeof(*argv) * len);
1605 55e8c065 2022-11-23 op while (argc < len) {
1606 55e8c065 2022-11-23 op while (isspace((unsigned char)*cmd))
1608 55e8c065 2022-11-23 op if (*cmd == '\0')
1611 55e8c065 2022-11-23 op argv[argc++] = cmd;
1612 55e8c065 2022-11-23 op escape = quote = 0;
1613 55e8c065 2022-11-23 op for (; *cmd != '\0'; ++cmd) {
1614 55e8c065 2022-11-23 op if (escape) {
1618 55e8c065 2022-11-23 op if (*cmd == '\\') {
1620 55e8c065 2022-11-23 op memmove(cmd, cmd + 1, strlen(cmd));
1624 55e8c065 2022-11-23 op if (*cmd == quote) {
1626 55e8c065 2022-11-23 op memmove(cmd, cmd + 1, strlen(cmd));
1630 55e8c065 2022-11-23 op if (*cmd == '\'' || *cmd == '"') {
1631 55e8c065 2022-11-23 op quote = *cmd;
1632 55e8c065 2022-11-23 op memmove(cmd, cmd + 1, strlen(cmd));
1639 55e8c065 2022-11-23 op if (isspace((unsigned char)*cmd))
1643 55e8c065 2022-11-23 op if (*cmd == '\0' && (escape || quote)) {
1644 55e8c065 2022-11-23 op fprintf(stderr, "unterminated %s\n",
1645 55e8c065 2022-11-23 op escape ? "escape" : "quote");
1649 55e8c065 2022-11-23 op if (*cmd == '\0')
1651 55e8c065 2022-11-23 op *cmd++ = '\0';
1654 55e8c065 2022-11-23 op if (*cmd != '\0') {
1655 55e8c065 2022-11-23 op fprintf(stderr, "too many arguments\n");
1658 55e8c065 2022-11-23 op return argc;
1662 df9bb54d 2022-11-23 op cd_or_fetch(const char *path, const char *outfile)
1664 14ae3944 2022-11-23 op struct qid qid;
1665 14ae3944 2022-11-23 op char *errstr;
1666 14ae3944 2022-11-23 op int fd, nfid, miss;
1668 14ae3944 2022-11-23 op while (*path == '/')
1670 14ae3944 2022-11-23 op if (*path == '\0')
1673 14ae3944 2022-11-23 op nfid = nextfid();
1674 14ae3944 2022-11-23 op errstr = walk_path(pwdfid, nfid, path, &miss, &qid);
1676 14ae3944 2022-11-23 op errx(1, "walk %s: %s", path, errstr);
1678 14ae3944 2022-11-23 op errc(1, ENOENT, "walk %s", path);
1680 14ae3944 2022-11-23 op if (qid.type & QTDIR) {
1681 df9bb54d 2022-11-23 op if (outfile)
1682 df9bb54d 2022-11-23 op errx(1, "can't fetch directory %s", path);
1683 14ae3944 2022-11-23 op do_clunk(pwdfid);
1684 14ae3944 2022-11-23 op pwdfid = nfid;
1688 df9bb54d 2022-11-23 op if (outfile == NULL) {
1689 df9bb54d 2022-11-23 op if ((outfile = strrchr(path, '/')) == NULL)
1690 df9bb54d 2022-11-23 op outfile = path;
1693 df9bb54d 2022-11-23 op if (*outfile == '\0')
1694 df9bb54d 2022-11-23 op errx(1, "invalid path: missing file name: %s",
1698 df9bb54d 2022-11-23 op if (strcmp(outfile, "-") != 0) {
1699 df9bb54d 2022-11-23 op fd = open(outfile, O_WRONLY|O_CREAT, 0644);
1700 df9bb54d 2022-11-23 op if (fd == -1)
1701 df9bb54d 2022-11-23 op err(1, "can't open for writing %s", outfile);
1705 df9bb54d 2022-11-23 op if (fetch_fid(nfid, fd, outfile) == -1)
1706 df9bb54d 2022-11-23 op err(1, "write %s", outfile);
1711 14ae3944 2022-11-23 op static const char *
1712 14ae3944 2022-11-23 op parse_addr(const char *url, const char **user,
1713 14ae3944 2022-11-23 op const char **port, const char **path)
1715 14ae3944 2022-11-23 op static char buf[PATH_MAX];
1716 14ae3944 2022-11-23 op char *host, *t;
1718 14ae3944 2022-11-23 op *user = *port = *path = NULL;
1721 14ae3944 2022-11-23 op if (strlcpy(buf, url, sizeof(buf)) >= sizeof(buf))
1722 14ae3944 2022-11-23 op errx(1, "connection string too long");
1724 9f134f93 2022-11-23 op if (!strncmp(host, "9p://", 5))
1727 14ae3944 2022-11-23 op if ((t = strchr(host, '/')) != NULL) {
1728 14ae3944 2022-11-23 op if (t == host)
1729 14ae3944 2022-11-23 op errx(1, "invalid connection string: %s", url);
1730 14ae3944 2022-11-23 op *t++ = '\0';
1731 14ae3944 2022-11-23 op if (*t != '\0')
1735 14ae3944 2022-11-23 op if ((t = strchr(host, '@')) != NULL) {
1736 14ae3944 2022-11-23 op if (t == host)
1737 14ae3944 2022-11-23 op errx(1, "invalid connection string: %s", url);
1738 14ae3944 2022-11-23 op *t++ = '\0';
1739 14ae3944 2022-11-23 op *user = host;
1741 14ae3944 2022-11-23 op } else if ((*user = getenv("USER")) == NULL)
1742 14ae3944 2022-11-23 op errx(1, "USER not defined");
1744 14ae3944 2022-11-23 op if ((t = strchr(host, ':')) != NULL) {
1745 14ae3944 2022-11-23 op *t++ = '\0';
1746 14ae3944 2022-11-23 op if (*t != '\0')
1749 14ae3944 2022-11-23 op if (*port == NULL)
1750 14ae3944 2022-11-23 op *port = "1337";
1752 14ae3944 2022-11-23 op return host;
1756 fb1a36c0 2022-01-09 op main(int argc, char **argv)
1758 14ae3944 2022-11-23 op const char *user, *host, *port, *path;
1759 df9bb54d 2022-11-23 op const char *outfile = NULL;
1762 fb1a36c0 2022-01-09 op log_init(1, LOG_DAEMON);
1763 fb1a36c0 2022-01-09 op log_setverbose(0);
1764 fb1a36c0 2022-01-09 op log_procinit(getprogname());
1766 df9bb54d 2022-11-23 op while ((ch = getopt(argc, argv, "C:cK:o:")) != -1) {
1767 fb1a36c0 2022-01-09 op switch (ch) {
1770 fb1a36c0 2022-01-09 op crtpath = optarg;
1772 ab9f44f7 2022-11-23 op case 'c': /* deprecated, remove after 0.3 */
1777 fb1a36c0 2022-01-09 op keypath = optarg;
1780 df9bb54d 2022-11-23 op outfile = optarg;
1786 fb1a36c0 2022-01-09 op argc -= optind;
1787 fb1a36c0 2022-01-09 op argv += optind;
1789 3847d69e 2022-01-30 op if (argc == 0 || (tls && crtpath == NULL))
1792 14ae3944 2022-11-23 op host = parse_addr(argv[0], &user, &port, &path);
1793 514e8d43 2022-11-23 op if (path == NULL && argv[1] != NULL) /* drop argv[1] after 0.3 */
1794 14ae3944 2022-11-23 op path = argv[1];
1795 df9bb54d 2022-11-23 op if (outfile && path == NULL)
1798 1c9ab7cf 2022-01-29 op signal(SIGPIPE, SIG_IGN);
1799 fb1a36c0 2022-01-09 op if (isatty(1)) {
1801 fb1a36c0 2022-01-09 op resized = 1;
1802 fb1a36c0 2022-01-09 op signal(SIGWINCH, tty_resized);
1805 fb1a36c0 2022-01-09 op if ((evb = evbuffer_new()) == NULL)
1806 fb1a36c0 2022-01-09 op fatal("evbuffer_new");
1808 fb1a36c0 2022-01-09 op if ((buf = evbuffer_new()) == NULL)
1809 fb1a36c0 2022-01-09 op fatal("evbuffer_new");
1811 fb1a36c0 2022-01-09 op if ((dirbuf = evbuffer_new()) == NULL)
1812 fb1a36c0 2022-01-09 op fatal("evbuferr_new");
1814 14ae3944 2022-11-23 op do_connect(host, port, user);
1816 df9bb54d 2022-11-23 op cd_or_fetch(path, outfile);
1818 d3e1ab0c 2022-11-24 op compl_setup();
1821 3cf47875 2022-12-21 op char *line, *argv[16] = {0};
1823 fb1a36c0 2022-01-09 op if ((line = read_line("kamiftp> ")) == NULL)
1826 55e8c065 2022-11-23 op if ((argc = parsecmd(line, argv, nitems(argv) - 1)) == -1) {
1831 55e8c065 2022-11-23 op argv[argc] = NULL;
1832 fb1a36c0 2022-01-09 op excmd(argc, (const char **)argv);
1835 ca14d066 2022-11-23 op fprintf(stderr, "\a");
1840 fb1a36c0 2022-01-09 op printf("\n");