2 dc8c1791 2022-01-10 op * Copyright (c) 2021, 2022 Omar Polo <op@omarpolo.com>
4 fb1a36c0 2022-01-09 op * Permission to use, copy, modify, and distribute this software for any
5 fb1a36c0 2022-01-09 op * purpose with or without fee is hereby granted, provided that the above
6 fb1a36c0 2022-01-09 op * copyright notice and this permission notice appear in all copies.
8 fb1a36c0 2022-01-09 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 fb1a36c0 2022-01-09 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 fb1a36c0 2022-01-09 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 fb1a36c0 2022-01-09 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 fb1a36c0 2022-01-09 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 fb1a36c0 2022-01-09 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 fb1a36c0 2022-01-09 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 bbcba3ed 2022-01-10 op #include "compat.h"
19 fb1a36c0 2022-01-09 op #include <sys/ioctl.h>
20 fb1a36c0 2022-01-09 op #include <sys/types.h>
21 fb1a36c0 2022-01-09 op #include <sys/socket.h>
22 01fb39ab 2022-01-16 op #include <sys/stat.h>
23 9e7dd230 2022-01-12 op #include <sys/wait.h>
25 fb1a36c0 2022-01-09 op #include <assert.h>
26 fb1a36c0 2022-01-09 op #include <errno.h>
27 fb1a36c0 2022-01-09 op #include <fcntl.h>
28 fb1a36c0 2022-01-09 op #include <inttypes.h>
29 fb1a36c0 2022-01-09 op #include <netdb.h>
30 9e7dd230 2022-01-12 op #include <libgen.h>
31 fb1a36c0 2022-01-09 op #include <limits.h>
32 fb1a36c0 2022-01-09 op #include <signal.h>
33 fb1a36c0 2022-01-09 op #include <stdio.h>
34 fb1a36c0 2022-01-09 op #include <stdlib.h>
35 fb1a36c0 2022-01-09 op #include <string.h>
36 fb1a36c0 2022-01-09 op #include <syslog.h>
37 fb1a36c0 2022-01-09 op #include <tls.h>
38 fb1a36c0 2022-01-09 op #include <unistd.h>
40 91cfa92a 2022-01-10 op #ifdef HAVE_READLINE
41 fb1a36c0 2022-01-09 op #include <readline/readline.h>
42 fb1a36c0 2022-01-09 op #include <readline/history.h>
45 fb1a36c0 2022-01-09 op #ifndef nitems
46 fb1a36c0 2022-01-09 op #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
49 fb1a36c0 2022-01-09 op #include "kami.h"
50 fb1a36c0 2022-01-09 op #include "utils.h"
51 fb1a36c0 2022-01-09 op #include "log.h"
52 c37e1cfe 2022-01-17 op #include "9pclib.h"
54 64d8db89 2022-01-16 op #define TMPFSTR "/tmp/kamiftp.XXXXXXXXXX"
55 64d8db89 2022-01-16 op #define TMPFSTRLEN sizeof(TMPFSTR)
59 fb1a36c0 2022-01-09 op const char *crtpath;
60 fb1a36c0 2022-01-09 op const char *keypath;
63 fb1a36c0 2022-01-09 op struct tls_config *tlsconf;
64 fb1a36c0 2022-01-09 op struct tls *ctx;
66 fb1a36c0 2022-01-09 op struct evbuffer *buf;
67 fb1a36c0 2022-01-09 op struct evbuffer *dirbuf;
68 fb1a36c0 2022-01-09 op uint32_t msize;
71 fb1a36c0 2022-01-09 op volatile sig_atomic_t resized;
73 fb1a36c0 2022-01-09 op int tty_width;
76 fb1a36c0 2022-01-09 op struct progress {
78 fb1a36c0 2022-01-09 op uint64_t done;
83 fb1a36c0 2022-01-09 op #define ASSERT_EMPTYBUF() assert(EVBUFFER_LENGTH(buf) == 0)
85 91cfa92a 2022-01-10 op #if !HAVE_READLINE
87 91cfa92a 2022-01-10 op readline(const char *prompt)
89 91cfa92a 2022-01-10 op char *ch, *line = NULL;
90 91cfa92a 2022-01-10 op size_t linesize = 0;
91 91cfa92a 2022-01-10 op ssize_t linelen;
93 91cfa92a 2022-01-10 op printf("%s", prompt);
94 91cfa92a 2022-01-10 op fflush(stdout);
96 91cfa92a 2022-01-10 op linelen = getline(&line, &linesize, stdin);
97 91cfa92a 2022-01-10 op if (linelen == -1)
100 91cfa92a 2022-01-10 op if ((ch = strchr(line, '\n')) != NULL)
106 91cfa92a 2022-01-10 op add_history(const char *line)
112 fb1a36c0 2022-01-09 op static char *
113 fb1a36c0 2022-01-09 op read_line(const char *prompt)
118 fb1a36c0 2022-01-09 op if ((line = readline(prompt)) == NULL)
120 fb1a36c0 2022-01-09 op /* XXX: trim spaces? */
121 fb1a36c0 2022-01-09 op if (*line == '\0') {
126 fb1a36c0 2022-01-09 op add_history(line);
131 9e7dd230 2022-01-12 op spawn(const char *argv0, ...)
136 9e7dd230 2022-01-12 op const char *argv[16], *last;
139 9e7dd230 2022-01-12 op memset(argv, 0, sizeof(argv));
141 9e7dd230 2022-01-12 op va_start(ap, argv0);
142 9e7dd230 2022-01-12 op argv[0] = argv0;
143 9e7dd230 2022-01-12 op for (i = 1; i < nitems(argv); ++i) {
144 9e7dd230 2022-01-12 op last = va_arg(ap, const char *);
145 9e7dd230 2022-01-12 op if (last == NULL)
147 9e7dd230 2022-01-12 op argv[i] = last;
151 9e7dd230 2022-01-12 op assert(last == NULL);
153 9e7dd230 2022-01-12 op switch (pid = fork()) {
155 9e7dd230 2022-01-12 op err(1, "fork");
156 9e7dd230 2022-01-12 op case 0: /* child */
157 9e7dd230 2022-01-12 op execvp(argv[0], (char *const *)argv);
158 9e7dd230 2022-01-12 op err(1, "execvp");
160 9e7dd230 2022-01-12 op waitpid(pid, &status, 0);
165 fb1a36c0 2022-01-09 op tty_resized(int signo)
170 fb1a36c0 2022-01-09 op static void __dead
171 fb1a36c0 2022-01-09 op usage(int ret)
173 dc8c1791 2022-01-10 op fprintf(stderr, "usage: %s [-c] [-C cert] [-K key] "
174 dc8c1791 2022-01-10 op "host[:port] [path]\n", getprogname());
175 fb1a36c0 2022-01-09 op fprintf(stderr, "kamid suite version " KAMID_VERSION "\n");
180 8d8fb849 2022-01-17 op nextfid(void)
184 8d8fb849 2022-01-17 op for (i = 0; ; ++i) {
185 8d8fb849 2022-01-17 op if (i != pwdfid)
191 fb1a36c0 2022-01-09 op do_send(void)
193 fb1a36c0 2022-01-09 op const void *buf;
194 fb1a36c0 2022-01-09 op size_t nbytes;
198 d33c93c4 2022-01-17 op hexdump("outgoing message", EVBUFFER_DATA(evb),
199 5565d021 2022-01-16 op EVBUFFER_LENGTH(evb));
201 fb1a36c0 2022-01-09 op while (EVBUFFER_LENGTH(evb) != 0) {
202 fb1a36c0 2022-01-09 op buf = EVBUFFER_DATA(evb);
203 fb1a36c0 2022-01-09 op nbytes = EVBUFFER_LENGTH(evb);
205 fb1a36c0 2022-01-09 op if (ctx == NULL) {
206 fb1a36c0 2022-01-09 op r = write(sock, buf, nbytes);
207 fb1a36c0 2022-01-09 op if (r == 0 || r == -1)
208 fb1a36c0 2022-01-09 op errx(1, "EOF");
210 fb1a36c0 2022-01-09 op r = tls_write(ctx, buf, nbytes);
211 fb1a36c0 2022-01-09 op if (r == TLS_WANT_POLLIN || r == TLS_WANT_POLLOUT)
214 fb1a36c0 2022-01-09 op errx(1, "tls: %s", tls_error(ctx));
217 fb1a36c0 2022-01-09 op evbuffer_drain(evb, r);
222 fb1a36c0 2022-01-09 op mustread(void *d, size_t len)
226 fb1a36c0 2022-01-09 op while (len != 0) {
227 fb1a36c0 2022-01-09 op if (ctx == NULL) {
228 fb1a36c0 2022-01-09 op r = read(sock, d, len);
229 fb1a36c0 2022-01-09 op if (r == 0 || r == -1)
230 fb1a36c0 2022-01-09 op errx(1, "EOF");
232 fb1a36c0 2022-01-09 op r = tls_read(ctx, d, len);
233 fb1a36c0 2022-01-09 op if (r == TLS_WANT_POLLIN || r == TLS_WANT_POLLOUT)
236 fb1a36c0 2022-01-09 op errx(1, "tls: %s", tls_error(ctx));
245 fb1a36c0 2022-01-09 op recv_msg(void)
247 fb1a36c0 2022-01-09 op uint32_t len, l;
248 fb1a36c0 2022-01-09 op char tmp[BUFSIZ];
250 fb1a36c0 2022-01-09 op mustread(&len, sizeof(len));
251 fb1a36c0 2022-01-09 op len = le32toh(len);
252 fb1a36c0 2022-01-09 op if (len < HEADERSIZE)
253 fb1a36c0 2022-01-09 op errx(1, "read message of invalid length %d", len);
255 fb1a36c0 2022-01-09 op len -= 4; /* skip the length just read */
257 fb1a36c0 2022-01-09 op while (len != 0) {
258 fb1a36c0 2022-01-09 op l = MIN(len, sizeof(tmp));
259 fb1a36c0 2022-01-09 op mustread(tmp, l);
261 fb1a36c0 2022-01-09 op evbuffer_add(buf, tmp, l);
265 5565d021 2022-01-16 op hexdump("incoming packet", EVBUFFER_DATA(buf),
266 5565d021 2022-01-16 op EVBUFFER_LENGTH(buf));
269 fb1a36c0 2022-01-09 op static uint64_t
270 fb1a36c0 2022-01-09 op np_read64(struct evbuffer *buf)
274 fb1a36c0 2022-01-09 op evbuffer_remove(buf, &n, sizeof(n));
275 fb1a36c0 2022-01-09 op return le64toh(n);
278 fb1a36c0 2022-01-09 op static uint32_t
279 fb1a36c0 2022-01-09 op np_read32(struct evbuffer *buf)
283 fb1a36c0 2022-01-09 op evbuffer_remove(buf, &n, sizeof(n));
284 fb1a36c0 2022-01-09 op return le32toh(n);
287 fb1a36c0 2022-01-09 op static uint16_t
288 fb1a36c0 2022-01-09 op np_read16(struct evbuffer *buf)
292 fb1a36c0 2022-01-09 op evbuffer_remove(buf, &n, sizeof(n));
293 fb1a36c0 2022-01-09 op return le16toh(n);
296 fb1a36c0 2022-01-09 op static uint16_t
297 fb1a36c0 2022-01-09 op np_read8(struct evbuffer *buf)
301 fb1a36c0 2022-01-09 op evbuffer_remove(buf, &n, sizeof(n));
305 fb1a36c0 2022-01-09 op static char *
306 fb1a36c0 2022-01-09 op np_readstr(struct evbuffer *buf)
308 fb1a36c0 2022-01-09 op uint16_t len;
311 fb1a36c0 2022-01-09 op len = np_read16(buf);
312 fb1a36c0 2022-01-09 op assert(EVBUFFER_LENGTH(buf) >= len);
314 fb1a36c0 2022-01-09 op if ((str = calloc(1, len+1)) == NULL)
315 fb1a36c0 2022-01-09 op err(1, "calloc");
316 fb1a36c0 2022-01-09 op evbuffer_remove(buf, str, len);
321 fb1a36c0 2022-01-09 op np_read_qid(struct evbuffer *buf, struct qid *qid)
323 fb1a36c0 2022-01-09 op assert(EVBUFFER_LENGTH(buf) >= QIDSIZE);
325 fb1a36c0 2022-01-09 op qid->type = np_read8(buf);
326 fb1a36c0 2022-01-09 op qid->vers = np_read32(buf);
327 fb1a36c0 2022-01-09 op qid->path = np_read64(buf);
331 fb1a36c0 2022-01-09 op np_read_stat(struct evbuffer *buf, struct np_stat *st)
333 fb1a36c0 2022-01-09 op uint16_t size;
335 fb1a36c0 2022-01-09 op memset(st, 0, sizeof(*st));
337 fb1a36c0 2022-01-09 op size = np_read16(buf);
338 fb1a36c0 2022-01-09 op if (size > EVBUFFER_LENGTH(buf))
341 fb1a36c0 2022-01-09 op st->type = np_read16(buf);
342 fb1a36c0 2022-01-09 op st->dev = np_read32(buf);
343 fb1a36c0 2022-01-09 op np_read_qid(buf, &st->qid);
344 fb1a36c0 2022-01-09 op st->mode = np_read32(buf);
345 fb1a36c0 2022-01-09 op st->atime = np_read32(buf);
346 fb1a36c0 2022-01-09 op st->mtime = np_read32(buf);
347 fb1a36c0 2022-01-09 op st->length = np_read64(buf);
348 fb1a36c0 2022-01-09 op st->name = np_readstr(buf);
349 fb1a36c0 2022-01-09 op st->uid = np_readstr(buf);
350 fb1a36c0 2022-01-09 op st->gid = np_readstr(buf);
351 fb1a36c0 2022-01-09 op st->muid = np_readstr(buf);
357 fb1a36c0 2022-01-09 op expect(uint8_t type)
361 fb1a36c0 2022-01-09 op t = np_read8(buf);
362 fb1a36c0 2022-01-09 op if (t == type)
365 fb1a36c0 2022-01-09 op if (t == Rerror) {
368 fb1a36c0 2022-01-09 op /* skip tag */
369 fb1a36c0 2022-01-09 op np_read16(buf);
371 fb1a36c0 2022-01-09 op err = np_readstr(buf);
372 fb1a36c0 2022-01-09 op errx(1, "expected %s, got error %s",
373 fb1a36c0 2022-01-09 op pp_msg_type(type), err);
376 fb1a36c0 2022-01-09 op errx(1, "expected %s, got msg type %s",
377 fb1a36c0 2022-01-09 op pp_msg_type(type), pp_msg_type(t));
381 fb1a36c0 2022-01-09 op expect2(uint8_t type, uint16_t tag)
385 fb1a36c0 2022-01-09 op expect(type);
387 fb1a36c0 2022-01-09 op t = np_read16(buf);
388 fb1a36c0 2022-01-09 op if (t == tag)
391 fb1a36c0 2022-01-09 op errx(1, "expected tag 0x%x, got 0x%x", tag, t);
394 8f9f99f7 2022-01-16 op static char *
395 8f9f99f7 2022-01-16 op check(uint8_t type, uint16_t tag)
397 8f9f99f7 2022-01-16 op uint16_t rtag;
398 8f9f99f7 2022-01-16 op uint8_t rtype;
400 8f9f99f7 2022-01-16 op rtype = np_read8(buf);
401 8f9f99f7 2022-01-16 op rtag = np_read16(buf);
402 8f9f99f7 2022-01-16 op if (rtype == type) {
403 8f9f99f7 2022-01-16 op if (rtag != tag)
404 8f9f99f7 2022-01-16 op errx(1, "expected tag 0x%x, got 0x%x", tag, rtag);
408 8f9f99f7 2022-01-16 op if (rtype == Rerror)
409 8f9f99f7 2022-01-16 op return np_readstr(buf);
411 8f9f99f7 2022-01-16 op errx(1, "expected %s, got msg type %s",
412 8f9f99f7 2022-01-16 op pp_msg_type(type), pp_msg_type(rtype));
416 fb1a36c0 2022-01-09 op do_version(void)
418 fb1a36c0 2022-01-09 op char *version;
420 fb1a36c0 2022-01-09 op tversion(VERSION9P, MSIZE9P);
423 fb1a36c0 2022-01-09 op expect2(Rversion, NOTAG);
425 fb1a36c0 2022-01-09 op msize = np_read32(buf);
426 fb1a36c0 2022-01-09 op version = np_readstr(buf);
428 315eafe8 2022-05-23 op if (msize > MSIZE9P || msize < 256)
429 fb1a36c0 2022-01-09 op errx(1, "got unexpected msize: %d", msize);
430 fb1a36c0 2022-01-09 op if (strcmp(version, VERSION9P))
431 fb1a36c0 2022-01-09 op errx(1, "unexpected 9p version: %s", version);
433 fb1a36c0 2022-01-09 op free(version);
434 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
438 fb1a36c0 2022-01-09 op do_attach(const char *path)
440 fb1a36c0 2022-01-09 op const char *user;
441 fb1a36c0 2022-01-09 op struct qid qid;
443 fb1a36c0 2022-01-09 op if (path == NULL)
445 fb1a36c0 2022-01-09 op if ((user = getenv("USER")) == NULL)
446 fb1a36c0 2022-01-09 op user = "flan";
448 fb1a36c0 2022-01-09 op tattach(pwdfid, NOFID, user, path);
451 fb1a36c0 2022-01-09 op expect2(Rattach, iota_tag);
452 fb1a36c0 2022-01-09 op np_read_qid(buf, &qid);
454 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
457 fb1a36c0 2022-01-09 op static uint32_t
458 fb1a36c0 2022-01-09 op do_open(uint32_t fid, uint8_t mode)
460 fb1a36c0 2022-01-09 op struct qid qid;
461 fb1a36c0 2022-01-09 op uint32_t iounit;
463 fb1a36c0 2022-01-09 op topen(fid, mode);
466 fb1a36c0 2022-01-09 op expect2(Ropen, iota_tag);
468 01fb39ab 2022-01-16 op np_read_qid(buf, &qid);
469 01fb39ab 2022-01-16 op iounit = np_read32(buf);
471 01fb39ab 2022-01-16 op ASSERT_EMPTYBUF();
473 01fb39ab 2022-01-16 op return iounit;
476 01fb39ab 2022-01-16 op static uint32_t
477 01fb39ab 2022-01-16 op do_create(uint32_t fid, const char *name, uint32_t perm, uint8_t mode)
479 01fb39ab 2022-01-16 op struct qid qid;
480 01fb39ab 2022-01-16 op uint32_t iounit;
482 01fb39ab 2022-01-16 op tcreate(fid, name, perm, mode);
485 01fb39ab 2022-01-16 op expect2(Rcreate, iota_tag);
487 fb1a36c0 2022-01-09 op np_read_qid(buf, &qid);
488 fb1a36c0 2022-01-09 op iounit = np_read32(buf);
490 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
492 fb1a36c0 2022-01-09 op return iounit;
496 fb1a36c0 2022-01-09 op do_clunk(uint32_t fid)
501 fb1a36c0 2022-01-09 op expect2(Rclunk, iota_tag);
503 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
506 1bb1bfca 2022-01-16 op static char *
507 fb1a36c0 2022-01-09 op dup_fid(int fid, int nfid)
509 fb1a36c0 2022-01-09 op uint16_t nwqid;
510 1bb1bfca 2022-01-16 op char *errstr;
512 fb1a36c0 2022-01-09 op twalk(fid, nfid, NULL, 0);
516 1bb1bfca 2022-01-16 op if ((errstr = check(Rwalk, iota_tag)) != NULL)
517 1bb1bfca 2022-01-16 op return errstr;
519 fb1a36c0 2022-01-09 op nwqid = np_read16(buf);
520 fb1a36c0 2022-01-09 op assert(nwqid == 0);
522 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
527 8f9f99f7 2022-01-16 op static char *
528 8f9f99f7 2022-01-16 op walk_path(int fid, int newfid, const char *path, int *missing,
529 8f9f99f7 2022-01-16 op struct qid *qid)
531 8f9f99f7 2022-01-16 op char *wnames[MAXWELEM], *p, *t, *errstr;
532 fb1a36c0 2022-01-09 op size_t nwname, i;
533 fb1a36c0 2022-01-09 op uint16_t nwqid;
535 fb1a36c0 2022-01-09 op if ((p = strdup(path)) == NULL)
536 fb1a36c0 2022-01-09 op err(1, "strdup");
539 fb1a36c0 2022-01-09 op /* strip initial ./ */
540 fb1a36c0 2022-01-09 op if (t[0] == '.' && t[1] == '/')
543 fb1a36c0 2022-01-09 op for (nwname = 0; nwname < nitems(wnames) &&
544 fb1a36c0 2022-01-09 op (wnames[nwname] = strsep(&t, "/")) != NULL;) {
545 fb1a36c0 2022-01-09 op if (*wnames[nwname] != '\0')
549 fb1a36c0 2022-01-09 op twalk(fid, newfid, (const char **)wnames, nwname);
553 8f9f99f7 2022-01-16 op *missing = nwname;
554 5fe03b5c 2022-01-21 op if ((errstr = check(Rwalk, iota_tag)) != NULL) {
556 8f9f99f7 2022-01-16 op return errstr;
559 fb1a36c0 2022-01-09 op nwqid = np_read16(buf);
560 fb1a36c0 2022-01-09 op assert(nwqid <= nwname);
562 fb1a36c0 2022-01-09 op /* consume all qids */
563 43d117b1 2022-01-16 op for (i = 0; i < nwqid; ++i)
564 fb1a36c0 2022-01-09 op np_read_qid(buf, qid);
568 8f9f99f7 2022-01-16 op *missing = nwname - nwqid;
573 fb1a36c0 2022-01-09 op do_stat(int fid, struct np_stat *st)
578 fb1a36c0 2022-01-09 op expect2(Rstat, iota_tag);
580 caa64af1 2022-02-02 op /* eat up the first two byte length */
581 caa64af1 2022-02-02 op np_read16(buf);
583 fb1a36c0 2022-01-09 op if (np_read_stat(buf, st) == -1)
584 fb1a36c0 2022-01-09 op errx(1, "invalid stat struct read");
586 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
589 c37e1cfe 2022-01-17 op static char *
590 c37e1cfe 2022-01-17 op do_wstat(int fid, const struct np_stat *st)
592 c37e1cfe 2022-01-17 op char *errstr;
594 c37e1cfe 2022-01-17 op twstat(fid, st);
598 c37e1cfe 2022-01-17 op if ((errstr = check(Rwstat, iota_tag)) != NULL)
599 c37e1cfe 2022-01-17 op return errstr;
601 c37e1cfe 2022-01-17 op ASSERT_EMPTYBUF();
606 fb1a36c0 2022-01-09 op static size_t
607 fb1a36c0 2022-01-09 op do_read(int fid, uint64_t off, uint32_t count, void *data)
611 fb1a36c0 2022-01-09 op tread(fid, off, count);
614 fb1a36c0 2022-01-09 op expect2(Rread, iota_tag);
616 fb1a36c0 2022-01-09 op r = np_read32(buf);
617 fb1a36c0 2022-01-09 op assert(r == EVBUFFER_LENGTH(buf));
618 fb1a36c0 2022-01-09 op assert(r <= count);
619 fb1a36c0 2022-01-09 op evbuffer_remove(buf, data, r);
621 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
626 01fb39ab 2022-01-16 op static size_t
627 01fb39ab 2022-01-16 op do_write(int fid, uint64_t off, uint32_t count, void *data)
631 01fb39ab 2022-01-16 op twrite(fid, off, data, count);
634 01fb39ab 2022-01-16 op expect2(Rwrite, iota_tag);
636 01fb39ab 2022-01-16 op r = np_read32(buf);
637 01fb39ab 2022-01-16 op assert(r <= count);
639 01fb39ab 2022-01-16 op ASSERT_EMPTYBUF();
645 fb1a36c0 2022-01-09 op draw_progress(const char *pre, const struct progress *p)
647 fb1a36c0 2022-01-09 op struct winsize ws;
654 fb1a36c0 2022-01-09 op perc = 100.0 * p->done / p->max;
655 fb1a36c0 2022-01-09 op if (!tty_p) {
656 fb1a36c0 2022-01-09 op fprintf(stderr, "%s: %d%%\n", pre, (int)perc);
660 fb1a36c0 2022-01-09 op if (resized) {
663 fb1a36c0 2022-01-09 op if (ioctl(0, TIOCGWINSZ, &ws) == -1)
665 fb1a36c0 2022-01-09 op tty_width = ws.ws_col;
667 fb1a36c0 2022-01-09 op w = tty_width;
669 fb1a36c0 2022-01-09 op if (pre == NULL ||
670 fb1a36c0 2022-01-09 op ((l = printf("\r%s ", pre)) == -1 || l >= w))
673 fb1a36c0 2022-01-09 op w -= l + 2 + 5; /* 2 for |, 5 for percentage + \n */
675 fb1a36c0 2022-01-09 op printf("%4d%%\n", (int)perc);
681 fb1a36c0 2022-01-09 op l = w * MIN(100.0, perc) / 100.0;
682 fb1a36c0 2022-01-09 op for (i = 0; i < l; i++)
684 fb1a36c0 2022-01-09 op for (; i < w; i++)
686 fb1a36c0 2022-01-09 op printf("|%4d%%", (int)perc);
688 fb1a36c0 2022-01-09 op fflush(stdout);
692 76521d26 2022-01-16 op fetch_fid(int fid, int fd, const char *name)
694 02d5b425 2022-05-23 op static char buf[MSIZE9P];
695 fb1a36c0 2022-01-09 op struct progress p = {0};
696 fb1a36c0 2022-01-09 op struct np_stat st;
700 fb1a36c0 2022-01-09 op do_stat(fid, &st);
701 fb1a36c0 2022-01-09 op do_open(fid, KOREAD);
703 fb1a36c0 2022-01-09 op p.max = st.length;
705 02d5b425 2022-05-23 op size_t len, off;
708 02d5b425 2022-05-23 op len = MIN(sizeof(buf), msize);
709 02d5b425 2022-05-23 op len -= HEADERSIZE + 4; /* for the request' fields */
711 02d5b425 2022-05-23 op r = do_read(fid, p.done, len, buf);
715 b2263c45 2022-01-16 op for (off = 0; off < r; off += nw)
716 b2263c45 2022-01-16 op if ((nw = write(fd, buf + off, r - off)) == 0 ||
723 9e7dd230 2022-01-12 op draw_progress(name, &p);
726 fb1a36c0 2022-01-09 op /* throttle, for debugging purpose */
728 fb1a36c0 2022-01-09 op struct timespec ts = { 0, 500000000 };
729 fb1a36c0 2022-01-09 op nanosleep(&ts, NULL);
735 fb1a36c0 2022-01-09 op putchar('\n');
737 01fb39ab 2022-01-16 op do_clunk(fid);
742 16f1f3bf 2022-01-21 op send_fid(int fid, const char *fnam, int open_flags, int fd, const char *name)
744 02d5b425 2022-05-23 op static char buf[MSIZE9P];
745 01fb39ab 2022-01-16 op struct progress p = {0};
746 01fb39ab 2022-01-16 op struct stat sb;
748 02d5b425 2022-05-23 op size_t w, len;
750 01fb39ab 2022-01-16 op if (fstat(fd, &sb) == -1)
751 01fb39ab 2022-01-16 op err(1, "fstat");
753 01fb39ab 2022-01-16 op if (fnam != NULL)
754 01fb39ab 2022-01-16 op do_create(fid, fnam, 0644, KOWRITE);
756 16f1f3bf 2022-01-21 op do_open(fid, open_flags | KOWRITE);
758 01fb39ab 2022-01-16 op p.max = sb.st_size;
760 02d5b425 2022-05-23 op len = MIN(sizeof(buf), msize);
761 02d5b425 2022-05-23 op len -= HEADERSIZE + 4 + 4 + 8; /* for the request' fields */
763 02d5b425 2022-05-23 op r = read(fd, buf, len);
767 01fb39ab 2022-01-16 op err(1, "read");
769 01fb39ab 2022-01-16 op w = do_write(fid, p.done, r, buf);
772 01fb39ab 2022-01-16 op draw_progress(name, &p);
775 01fb39ab 2022-01-16 op /* throttle, for debugging purpose */
777 01fb39ab 2022-01-16 op struct timespec ts = { 0, 500000000 };
778 01fb39ab 2022-01-16 op nanosleep(&ts, NULL);
783 01fb39ab 2022-01-16 op putchar('\n');
784 fb1a36c0 2022-01-09 op do_clunk(fid);
788 9c48f915 2022-01-16 op woc_file(int fd, const char *prompt, const char *path)
790 9c48f915 2022-01-16 op struct qid qid;
791 9c48f915 2022-01-16 op const char *n = NULL;
792 9c48f915 2022-01-16 op char *errstr;
793 9c48f915 2022-01-16 op int nfid, miss;
795 8d8fb849 2022-01-17 op nfid = nextfid();
796 9c48f915 2022-01-16 op errstr = walk_path(pwdfid, nfid, path, &miss, &qid);
797 9c48f915 2022-01-16 op if (errstr != NULL && miss > 1) {
798 9c48f915 2022-01-16 op printf("%s: %s\n", path, errstr);
799 9c48f915 2022-01-16 op free(errstr);
803 9c48f915 2022-01-16 op if (errstr != NULL || miss == 1) {
804 9c48f915 2022-01-16 op char p[PATH_MAX], *dn;
807 9c48f915 2022-01-16 op * If it's only one component missing (the file name), walk
808 9c48f915 2022-01-16 op * to the parent directory and try to create the file.
811 9c48f915 2022-01-16 op if (strlcpy(p, path, sizeof(p)) >= sizeof(p)) {
812 9c48f915 2022-01-16 op printf("path too long: %s\n", path);
815 9c48f915 2022-01-16 op dn = dirname(p);
817 9c48f915 2022-01-16 op if (!strcmp(dn, ".")) {
818 9c48f915 2022-01-16 op errstr = dup_fid(pwdfid, nfid);
821 9c48f915 2022-01-16 op errstr = walk_path(pwdfid, nfid, dn, &miss, &qid);
823 9c48f915 2022-01-16 op if (errstr != NULL) {
824 9c48f915 2022-01-16 op printf("%s: %s\n", dn, errstr);
825 9c48f915 2022-01-16 op free(errstr);
829 9c48f915 2022-01-16 op if (miss != 0) {
830 9c48f915 2022-01-16 op printf("%s: not a directory\n", dn);
834 9c48f915 2022-01-16 op if ((n = strrchr(path, '/')) != NULL)
840 9c48f915 2022-01-16 op free(errstr);
842 9c48f915 2022-01-16 op if (miss > 1) {
843 9c48f915 2022-01-16 op printf("can't create %s: missing %d path component(s)\n",
848 16f1f3bf 2022-01-21 op send_fid(nfid, n, KOTRUNC, fd, prompt);
853 fb1a36c0 2022-01-09 op do_tls_connect(const char *host, const char *port)
855 fb1a36c0 2022-01-09 op int handshake;
857 fb1a36c0 2022-01-09 op if ((tlsconf = tls_config_new()) == NULL)
858 fb1a36c0 2022-01-09 op fatalx("tls_config_new");
859 fb1a36c0 2022-01-09 op tls_config_insecure_noverifycert(tlsconf);
860 fb1a36c0 2022-01-09 op tls_config_insecure_noverifyname(tlsconf);
862 3847d69e 2022-01-30 op if (keypath == NULL)
863 3847d69e 2022-01-30 op keypath = crtpath;
865 fb1a36c0 2022-01-09 op if (tls_config_set_keypair_file(tlsconf, crtpath, keypath) == -1)
866 fb1a36c0 2022-01-09 op fatalx("can't load certs (%s, %s)", crtpath, keypath);
868 fb1a36c0 2022-01-09 op if ((ctx = tls_client()) == NULL)
869 fb1a36c0 2022-01-09 op fatal("tls_client");
870 fb1a36c0 2022-01-09 op if (tls_configure(ctx, tlsconf) == -1)
871 fb1a36c0 2022-01-09 op fatalx("tls_configure: %s", tls_error(ctx));
873 fb1a36c0 2022-01-09 op if (tls_connect(ctx, host, port) == -1)
874 fb1a36c0 2022-01-09 op fatalx("can't connect to %s:%s: %s", host, port,
875 fb1a36c0 2022-01-09 op tls_error(ctx));
877 fb1a36c0 2022-01-09 op for (handshake = 0; !handshake;) {
878 fb1a36c0 2022-01-09 op switch (tls_handshake(ctx)) {
880 fb1a36c0 2022-01-09 op fatalx("tls_handshake: %s", tls_error(ctx));
882 fb1a36c0 2022-01-09 op handshake = 1;
889 fb1a36c0 2022-01-09 op do_ctxt_connect(const char *host, const char *port)
891 fb1a36c0 2022-01-09 op struct addrinfo hints, *res, *res0;
892 fb1a36c0 2022-01-09 op int error, saved_errno;
893 fb1a36c0 2022-01-09 op const char *cause = NULL;
895 fb1a36c0 2022-01-09 op memset(&hints, 0, sizeof(hints));
896 fb1a36c0 2022-01-09 op hints.ai_family = AF_UNSPEC;
897 fb1a36c0 2022-01-09 op hints.ai_socktype = SOCK_STREAM;
898 fb1a36c0 2022-01-09 op error = getaddrinfo(host, port, &hints, &res0);
900 fb1a36c0 2022-01-09 op errx(1, "%s", gai_strerror(error));
903 fb1a36c0 2022-01-09 op for (res = res0; res != NULL; res = res->ai_next) {
904 db066f1e 2022-01-12 op sock = socket(res->ai_family, res->ai_socktype|SOCK_CLOEXEC,
905 fb1a36c0 2022-01-09 op res->ai_protocol);
906 fb1a36c0 2022-01-09 op if (sock == -1) {
907 fb1a36c0 2022-01-09 op cause = "socket";
911 fb1a36c0 2022-01-09 op if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
912 fb1a36c0 2022-01-09 op cause = "connect";
913 fb1a36c0 2022-01-09 op saved_errno = errno;
915 fb1a36c0 2022-01-09 op errno = saved_errno;
923 fb1a36c0 2022-01-09 op if (sock == -1)
924 fb1a36c0 2022-01-09 op err(1, "%s", cause);
925 fb1a36c0 2022-01-09 op freeaddrinfo(res0);
929 fb1a36c0 2022-01-09 op do_connect(const char *connspec, const char *path)
931 fb1a36c0 2022-01-09 op char *host, *colon;
932 fb1a36c0 2022-01-09 op const char *port;
934 fb1a36c0 2022-01-09 op host = xstrdup(connspec);
935 fb1a36c0 2022-01-09 op if ((colon = strchr(host, ':')) != NULL) {
936 fb1a36c0 2022-01-09 op *colon = '\0';
937 fb1a36c0 2022-01-09 op port = ++colon;
939 fb1a36c0 2022-01-09 op port = "1337";
941 fb1a36c0 2022-01-09 op printf("connecting to %s:%s...", host, port);
942 fb1a36c0 2022-01-09 op fflush(stdout);
945 fb1a36c0 2022-01-09 op do_tls_connect(host, port);
947 fb1a36c0 2022-01-09 op do_ctxt_connect(host, port);
949 fb1a36c0 2022-01-09 op printf(" done!\n");
951 fb1a36c0 2022-01-09 op do_version();
952 fb1a36c0 2022-01-09 op do_attach(path);
958 64d8db89 2022-01-16 op tmp_file(char sfn[TMPFSTRLEN])
962 64d8db89 2022-01-16 op strlcpy(sfn, TMPFSTR, TMPFSTRLEN);
963 64d8db89 2022-01-16 op if ((tmpfd = mkstemp(sfn)) == -1) {
964 64d8db89 2022-01-16 op warn("mkstemp %s", sfn);
968 aa76fd7f 2022-01-16 op /* set the close-on-exec flag */
969 aa76fd7f 2022-01-16 op if (fcntl(tmpfd, F_SETFD, FD_CLOEXEC) == -1) {
970 aa76fd7f 2022-01-16 op warn("fcntl");
971 aa76fd7f 2022-01-16 op close(tmpfd);
975 64d8db89 2022-01-16 op return tmpfd;
978 1f5fb843 2022-01-16 op static inline const char *
979 1f5fb843 2022-01-16 op pp_perm(uint8_t x)
981 1f5fb843 2022-01-16 op switch (x & 0x7) {
983 1f5fb843 2022-01-16 op return "---";
985 1f5fb843 2022-01-16 op return "--x";
987 1f5fb843 2022-01-16 op return "-w-";
989 1f5fb843 2022-01-16 op return "-wx";
991 1f5fb843 2022-01-16 op return "r--";
993 1f5fb843 2022-01-16 op return "r-x";
995 1f5fb843 2022-01-16 op return "rw-";
997 1f5fb843 2022-01-16 op return "rwx";
999 1f5fb843 2022-01-16 op /* unreachable, just for the compiler' happiness */
1000 1f5fb843 2022-01-16 op return "???";
1004 c37e1cfe 2022-01-17 op static inline void
1005 c37e1cfe 2022-01-17 op prepare_wstat(struct np_stat *st)
1007 c37e1cfe 2022-01-17 op memset(st, 0xFF, sizeof(*st));
1008 c37e1cfe 2022-01-17 op st->name = NULL;
1009 c37e1cfe 2022-01-17 op st->uid = NULL;
1010 c37e1cfe 2022-01-17 op st->gid = NULL;
1011 c37e1cfe 2022-01-17 op st->muid = NULL;
1015 fb1a36c0 2022-01-09 op cmd_bell(int argc, const char **argv)
1017 fb1a36c0 2022-01-09 op if (argc == 0) {
1018 fb1a36c0 2022-01-09 op bell = !bell;
1020 fb1a36c0 2022-01-09 op puts("bell mode enabled");
1022 fb1a36c0 2022-01-09 op puts("bell mode disabled");
1026 fb1a36c0 2022-01-09 op if (argc != 1)
1029 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "on")) {
1031 fb1a36c0 2022-01-09 op puts("bell mode enabled");
1035 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "off")) {
1037 fb1a36c0 2022-01-09 op puts("bell mode disabled");
1042 fb1a36c0 2022-01-09 op printf("bell [on | off]\n");
1046 fb1a36c0 2022-01-09 op cmd_bye(int argc, const char **argv)
1048 fb1a36c0 2022-01-09 op log_warnx("bye\n");
1053 fb1a36c0 2022-01-09 op cmd_cd(int argc, const char **argv)
1055 fb1a36c0 2022-01-09 op struct qid qid;
1056 8f9f99f7 2022-01-16 op int nfid, miss;
1057 8f9f99f7 2022-01-16 op char *errstr;
1059 fb1a36c0 2022-01-09 op if (argc != 1) {
1060 fb1a36c0 2022-01-09 op printf("usage: cd remote-path\n");
1064 8d8fb849 2022-01-17 op nfid = nextfid();
1065 8f9f99f7 2022-01-16 op errstr = walk_path(pwdfid, nfid, argv[0], &miss, &qid);
1066 8f9f99f7 2022-01-16 op if (errstr != NULL) {
1067 8f9f99f7 2022-01-16 op printf("%s: %s\n", argv[0], errstr);
1068 8f9f99f7 2022-01-16 op free(errstr);
1072 8f9f99f7 2022-01-16 op if (miss != 0 || !(qid.type & QTDIR)) {
1073 8f9f99f7 2022-01-16 op printf("%s: not a directory\n", argv[0]);
1074 fcd9d510 2022-01-16 op if (miss == 0)
1075 fcd9d510 2022-01-16 op do_clunk(nfid);
1079 8f9f99f7 2022-01-16 op do_clunk(pwdfid);
1080 8f9f99f7 2022-01-16 op pwdfid = nfid;
1084 9c48f915 2022-01-16 op cmd_edit(int argc, const char **argv)
1086 9c48f915 2022-01-16 op struct qid qid;
1087 9c48f915 2022-01-16 op int nfid, tmpfd, miss;
1088 9c48f915 2022-01-16 op char sfn[TMPFSTRLEN], p[PATH_MAX], *name, *errstr;
1089 9c48f915 2022-01-16 op const char *ed;
1091 9c48f915 2022-01-16 op if (argc != 1) {
1092 9c48f915 2022-01-16 op puts("usage: edit file");
1096 9c48f915 2022-01-16 op if ((ed = getenv("VISUAL")) == NULL &&
1097 9c48f915 2022-01-16 op (ed = getenv("EDITOR")) == NULL)
1100 8d8fb849 2022-01-17 op nfid = nextfid();
1101 9c48f915 2022-01-16 op errstr = walk_path(pwdfid, nfid, *argv, &miss, &qid);
1102 9c48f915 2022-01-16 op if (errstr != NULL) {
1103 9c48f915 2022-01-16 op printf("%s: %s\n", *argv, errstr);
1104 9c48f915 2022-01-16 op free(errstr);
1108 9c48f915 2022-01-16 op if (miss != 0 || qid.type != 0) {
1109 9c48f915 2022-01-16 op printf("%s: not a file\n", *argv);
1110 9c48f915 2022-01-16 op if (miss == 0)
1111 9c48f915 2022-01-16 op do_clunk(nfid);
1115 9c48f915 2022-01-16 op if ((tmpfd = tmp_file(sfn)) == -1) {
1116 9c48f915 2022-01-16 op do_clunk(nfid);
1120 9c48f915 2022-01-16 op strlcpy(p, *argv, sizeof(p));
1121 9c48f915 2022-01-16 op name = basename(p);
1123 02571aa1 2022-01-29 op if (fetch_fid(nfid, tmpfd, name)) {
1124 02571aa1 2022-01-29 op warn("failed fetch or can't write %s", sfn);
1127 9c48f915 2022-01-16 op close(tmpfd);
1129 9c48f915 2022-01-16 op spawn(ed, sfn, NULL);
1132 9c48f915 2022-01-16 op * Re-open the file because it's not guaranteed that the
1133 9c48f915 2022-01-16 op * file descriptor tmpfd is still associated with the file
1134 9c48f915 2022-01-16 op * pointed by sfn: it's not uncommon for editor to write
1135 9c48f915 2022-01-16 op * a backup file and then rename(2) it to the file name.
1137 9c48f915 2022-01-16 op if ((tmpfd = open(sfn, O_RDONLY)) == -1) {
1138 9c48f915 2022-01-16 op warn("can't open %s", sfn);
1142 9c48f915 2022-01-16 op woc_file(tmpfd, *argv, name);
1143 9c48f915 2022-01-16 op close(tmpfd);
1146 9c48f915 2022-01-16 op unlink(sfn);
1150 fb1a36c0 2022-01-09 op cmd_get(int argc, const char **argv)
1152 fb1a36c0 2022-01-09 op struct qid qid;
1153 fb1a36c0 2022-01-09 op const char *l;
1154 8f9f99f7 2022-01-16 op char *errstr;
1155 8f9f99f7 2022-01-16 op int nfid, fd, miss;
1157 fb1a36c0 2022-01-09 op if (argc != 1 && argc != 2) {
1158 fb1a36c0 2022-01-09 op printf("usage: get remote-file [local-file]\n");
1162 fb1a36c0 2022-01-09 op if (argc == 2)
1163 fb1a36c0 2022-01-09 op l = argv[1];
1164 fb1a36c0 2022-01-09 op else if ((l = strrchr(argv[0], '/')) != NULL)
1165 fb1a36c0 2022-01-09 op l++; /* skip / */
1167 2ba25711 2022-01-16 op l = argv[0];
1169 8d8fb849 2022-01-17 op nfid = nextfid();
1170 8f9f99f7 2022-01-16 op errstr = walk_path(pwdfid, nfid, argv[0], &miss, &qid);
1171 8f9f99f7 2022-01-16 op if (errstr != NULL) {
1172 8f9f99f7 2022-01-16 op printf("%s: %s\n", argv[0], errstr);
1173 8f9f99f7 2022-01-16 op free(errstr);
1177 8f9f99f7 2022-01-16 op if (miss != 0 || qid.type != 0) {
1178 8f9f99f7 2022-01-16 op printf("%s: not a file\n", argv[0]);
1179 fcd9d510 2022-01-16 op if (miss == 0)
1180 fcd9d510 2022-01-16 op do_clunk(nfid);
1184 76521d26 2022-01-16 op if ((fd = open(l, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0644)) == -1) {
1185 76521d26 2022-01-16 op warn("can't open %s", l);
1186 76521d26 2022-01-16 op do_clunk(nfid);
1190 02571aa1 2022-01-29 op if (fetch_fid(nfid, fd, l) == -1)
1191 02571aa1 2022-01-29 op warn("write %s", l);
1196 5565d021 2022-01-16 op cmd_hexdump(int argc, const char **argv)
1198 5565d021 2022-01-16 op if (argc == 0) {
1199 5565d021 2022-01-16 op xdump = !xdump;
1201 5565d021 2022-01-16 op puts("hexdump mode enabled");
1203 5565d021 2022-01-16 op puts("hexdump mode disabled");
1207 5565d021 2022-01-16 op if (argc > 1)
1210 5565d021 2022-01-16 op if (!strcmp(*argv, "on")) {
1212 5565d021 2022-01-16 op puts("hexdump mode enabled");
1216 5565d021 2022-01-16 op if (!strcmp(*argv, "off")) {
1218 5565d021 2022-01-16 op puts("hexdump mode disabled");
1223 5565d021 2022-01-16 op puts("usage: hexdump [on | off]");
1227 fb1a36c0 2022-01-09 op cmd_lcd(int argc, const char **argv)
1229 fb1a36c0 2022-01-09 op const char *dir;
1231 fb1a36c0 2022-01-09 op if (argc > 1) {
1232 d166ffc0 2022-01-16 op printf("usage: lcd [local-directory]\n");
1236 fb1a36c0 2022-01-09 op if (argc == 1)
1237 fb1a36c0 2022-01-09 op dir = *argv;
1239 fb1a36c0 2022-01-09 op if (argc == 0 && (dir = getenv("HOME")) == NULL) {
1240 fb1a36c0 2022-01-09 op printf("HOME is not defined\n");
1244 fb1a36c0 2022-01-09 op if (chdir(dir) == -1)
1245 fb1a36c0 2022-01-09 op printf("cd: %s: %s\n", dir, strerror(errno));
1249 fb1a36c0 2022-01-09 op cmd_lpwd(int argc, const char **argv)
1251 fb1a36c0 2022-01-09 op char path[PATH_MAX];
1253 d166ffc0 2022-01-16 op if (argc != 0) {
1254 d166ffc0 2022-01-16 op printf("usage: lpwd\n");
1258 fb1a36c0 2022-01-09 op if (getcwd(path, sizeof(path)) == NULL) {
1259 fb1a36c0 2022-01-09 op printf("lpwd: %s\n", strerror(errno));
1263 fb1a36c0 2022-01-09 op printf("%s\n", path);
1267 fb1a36c0 2022-01-09 op cmd_ls(int argc, const char **argv)
1269 fb1a36c0 2022-01-09 op struct np_stat st;
1270 4f52d9af 2022-01-16 op time_t now, mtime;
1271 4f52d9af 2022-01-16 op struct tm *tm;
1272 fb1a36c0 2022-01-09 op uint64_t off = 0;
1273 fb1a36c0 2022-01-09 op uint32_t len;
1275 4f52d9af 2022-01-16 op const char *timfmt;
1276 4f52d9af 2022-01-16 op char fmt[FMT_SCALED_STRSIZE], tim[13], *errstr;
1278 fb1a36c0 2022-01-09 op if (argc != 0) {
1279 fb1a36c0 2022-01-09 op printf("ls don't take arguments (yet)\n");
1283 4f52d9af 2022-01-16 op now = time(NULL);
1285 a9288ad9 2022-01-17 op nfid = nextfid();
1286 a9288ad9 2022-01-17 op if ((errstr = dup_fid(pwdfid, nfid)) != NULL) {
1287 1bb1bfca 2022-01-16 op printf(".: %s\n", errstr);
1288 1bb1bfca 2022-01-16 op free(errstr);
1292 a9288ad9 2022-01-17 op do_open(nfid, KOREAD);
1294 fb1a36c0 2022-01-09 op evbuffer_drain(dirbuf, EVBUFFER_LENGTH(dirbuf));
1297 1610d487 2022-05-23 op tread(nfid, off, msize - 4);
1300 fb1a36c0 2022-01-09 op expect2(Rread, iota_tag);
1302 fb1a36c0 2022-01-09 op len = np_read32(buf);
1303 fb1a36c0 2022-01-09 op if (len == 0)
1306 fb1a36c0 2022-01-09 op evbuffer_add_buffer(dirbuf, buf);
1309 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
1312 fb1a36c0 2022-01-09 op while (EVBUFFER_LENGTH(dirbuf) != 0) {
1313 fb1a36c0 2022-01-09 op if (np_read_stat(dirbuf, &st) == -1)
1314 fb1a36c0 2022-01-09 op errx(1, "invalid stat struct read");
1316 fb1a36c0 2022-01-09 op if (fmt_scaled(st.length, fmt) == -1)
1317 fb1a36c0 2022-01-09 op strlcpy(fmt, "xxx", sizeof(fmt));
1319 4f52d9af 2022-01-16 op mtime = st.mtime;
1321 4f52d9af 2022-01-16 op if (now > mtime && (now - mtime) < 365/2 * 24 * 12 * 60)
1322 4f52d9af 2022-01-16 op timfmt = "%b %e %R";
1324 4f52d9af 2022-01-16 op timfmt = "%b %e %Y";
1326 4f52d9af 2022-01-16 op if ((tm = localtime(&mtime)) == NULL ||
1327 4f52d9af 2022-01-16 op strftime(tim, sizeof(tim), timfmt, tm) == 0)
1328 4f52d9af 2022-01-16 op strlcpy(tim, "unknown", sizeof(tim));
1330 1f5fb843 2022-01-16 op if (st.qid.type & QTDIR)
1331 1f5fb843 2022-01-16 op printf("d");
1333 1f5fb843 2022-01-16 op printf("-");
1334 1f5fb843 2022-01-16 op printf("%s", pp_perm(st.mode >> 6));
1335 1f5fb843 2022-01-16 op printf("%s", pp_perm(st.mode >> 3));
1336 1f5fb843 2022-01-16 op printf("%s", pp_perm(st.mode));
1337 d33c93c4 2022-01-17 op printf(" %8s %12s %s%s\n", fmt, tim, st.name,
1338 6470594e 2022-01-16 op st.qid.type & QTDIR ? "/" : "");
1340 fb1a36c0 2022-01-09 op free(st.name);
1341 fb1a36c0 2022-01-09 op free(st.uid);
1342 fb1a36c0 2022-01-09 op free(st.gid);
1343 fb1a36c0 2022-01-09 op free(st.muid);
1346 a9288ad9 2022-01-17 op do_clunk(nfid);
1350 9e7dd230 2022-01-12 op cmd_page(int argc, const char **argv)
1352 9e7dd230 2022-01-12 op struct qid qid;
1353 02571aa1 2022-01-29 op int nfid, tmpfd, miss, r;
1354 64d8db89 2022-01-16 op char sfn[TMPFSTRLEN], p[PATH_MAX], *name, *errstr;
1355 62f663bb 2022-01-16 op const char *pager;
1357 9e7dd230 2022-01-12 op if (argc != 1) {
1358 9e7dd230 2022-01-12 op puts("usage: page file");
1362 62f663bb 2022-01-16 op if ((pager = getenv("PAGER")) == NULL)
1363 62f663bb 2022-01-16 op pager = "less";
1365 8d8fb849 2022-01-17 op nfid = nextfid();
1366 8f9f99f7 2022-01-16 op errstr = walk_path(pwdfid, nfid, *argv, &miss, &qid);
1367 8f9f99f7 2022-01-16 op if (errstr != NULL) {
1368 8f9f99f7 2022-01-16 op printf("%s: %s\n", *argv, errstr);
1369 8f9f99f7 2022-01-16 op free(errstr);
1373 8f9f99f7 2022-01-16 op if (miss != 0 || qid.type != 0) {
1374 8f9f99f7 2022-01-16 op printf("%s: not a file\n", *argv);
1375 fcd9d510 2022-01-16 op if (miss == 0)
1376 fcd9d510 2022-01-16 op do_clunk(nfid);
1380 64d8db89 2022-01-16 op if ((tmpfd = tmp_file(sfn)) == -1) {
1381 9e7dd230 2022-01-12 op do_clunk(nfid);
1385 9e7dd230 2022-01-12 op strlcpy(p, *argv, sizeof(p));
1386 9e7dd230 2022-01-12 op name = basename(p);
1387 02571aa1 2022-01-29 op if ((r = fetch_fid(nfid, tmpfd, name)) == -1)
1388 02571aa1 2022-01-29 op warn("write %s", sfn);
1389 9e7dd230 2022-01-12 op close(tmpfd);
1390 02571aa1 2022-01-29 op if (r != -1)
1391 02571aa1 2022-01-29 op spawn(pager, sfn, NULL);
1392 9e7dd230 2022-01-12 op unlink(sfn);
1396 1c9ab7cf 2022-01-29 op cmd_pipe(int argc, const char **argv)
1398 1c9ab7cf 2022-01-29 op struct qid qid;
1400 1c9ab7cf 2022-01-29 op int nfid, tmpfd, miss, status;
1401 1c9ab7cf 2022-01-29 op int filedes[2]; /* read end, write end */
1402 1c9ab7cf 2022-01-29 op char *errstr;
1404 1c9ab7cf 2022-01-29 op if (argc < 2) {
1405 1c9ab7cf 2022-01-29 op puts("usage: pipe remote-file cmd [args...]");
1409 1c9ab7cf 2022-01-29 op nfid = nextfid();
1410 1c9ab7cf 2022-01-29 op errstr = walk_path(pwdfid, nfid, *argv, &miss, &qid);
1411 1c9ab7cf 2022-01-29 op if (errstr != NULL) {
1412 1c9ab7cf 2022-01-29 op printf("%s: %s\n", *argv, errstr);
1413 1c9ab7cf 2022-01-29 op free(errstr);
1417 1c9ab7cf 2022-01-29 op if (miss != 0 || qid.type != 0) {
1418 1c9ab7cf 2022-01-29 op printf("%s: not a file\n", *argv);
1419 1c9ab7cf 2022-01-29 op if (miss == 0)
1420 1c9ab7cf 2022-01-29 op do_clunk(nfid);
1424 1c9ab7cf 2022-01-29 op if (pipe(filedes) == -1)
1425 1c9ab7cf 2022-01-29 op err(1, "pipe");
1427 1c9ab7cf 2022-01-29 op switch (pid = vfork()) {
1429 1c9ab7cf 2022-01-29 op err(1, "vfork");
1431 1c9ab7cf 2022-01-29 op close(filedes[1]);
1432 1c9ab7cf 2022-01-29 op if (dup2(filedes[0], 0) == -1)
1433 1c9ab7cf 2022-01-29 op err(1, "dup2");
1434 1c9ab7cf 2022-01-29 op execvp(argv[1], (char *const *)argv + 1);
1435 1c9ab7cf 2022-01-29 op err(1, "execvp");
1438 1c9ab7cf 2022-01-29 op close(filedes[0]);
1439 1c9ab7cf 2022-01-29 op if (fetch_fid(nfid, filedes[1], *argv) == -1)
1440 1c9ab7cf 2022-01-29 op warnx("failed to fetch all the file");
1441 1c9ab7cf 2022-01-29 op close(filedes[1]);
1443 1c9ab7cf 2022-01-29 op waitpid(pid, &status, 0);
1447 01fb39ab 2022-01-16 op cmd_put(int argc, const char **argv)
1449 01fb39ab 2022-01-16 op struct qid qid;
1450 9c48f915 2022-01-16 op const char *l;
1453 01fb39ab 2022-01-16 op if (argc != 1 && argc != 2) {
1454 01fb39ab 2022-01-16 op printf("usage: put local-file [remote-file]\n");
1458 01fb39ab 2022-01-16 op if (argc == 2)
1459 01fb39ab 2022-01-16 op l = argv[1];
1460 01fb39ab 2022-01-16 op else if ((l = strrchr(argv[0], '/')) != NULL)
1461 01fb39ab 2022-01-16 op l++; /* skip / */
1463 01fb39ab 2022-01-16 op l = argv[0];
1465 01fb39ab 2022-01-16 op if ((fd = open(argv[0], O_RDONLY)) == -1) {
1466 01fb39ab 2022-01-16 op warn("%s", argv[0]);
1470 9c48f915 2022-01-16 op woc_file(fd, argv[0], l);
1475 c37e1cfe 2022-01-17 op cmd_rename(int argc, const char **argv)
1477 c37e1cfe 2022-01-17 op struct np_stat st;
1478 c37e1cfe 2022-01-17 op struct qid qid;
1479 c37e1cfe 2022-01-17 op char *errstr;
1480 c37e1cfe 2022-01-17 op int nfid, miss;
1482 c37e1cfe 2022-01-17 op if (argc != 2) {
1483 c37e1cfe 2022-01-17 op puts("usage: rename remote-file new-remote-name");
1487 8d8fb849 2022-01-17 op nfid = nextfid();
1488 c37e1cfe 2022-01-17 op errstr = walk_path(pwdfid, nfid, argv[0], &miss, &qid);
1489 c37e1cfe 2022-01-17 op if (errstr != NULL) {
1490 c37e1cfe 2022-01-17 op printf("%s: %s\n", argv[0], errstr);
1491 c37e1cfe 2022-01-17 op free(errstr);
1495 c37e1cfe 2022-01-17 op if (miss != 0) {
1496 c37e1cfe 2022-01-17 op printf("%s: not such file or directory\n", argv[0]);
1500 c37e1cfe 2022-01-17 op prepare_wstat(&st);
1501 c37e1cfe 2022-01-17 op st.name = (char *)argv[1];
1502 c37e1cfe 2022-01-17 op if ((errstr = do_wstat(nfid, &st)) != NULL) {
1503 c37e1cfe 2022-01-17 op printf("rename: %s\n", errstr);
1504 c37e1cfe 2022-01-17 op free(errstr);
1507 c37e1cfe 2022-01-17 op do_clunk(nfid);
1511 fb1a36c0 2022-01-09 op cmd_verbose(int argc, const char **argv)
1513 fb1a36c0 2022-01-09 op if (argc == 0) {
1514 fb1a36c0 2022-01-09 op log_setverbose(!log_getverbose());
1515 fb1a36c0 2022-01-09 op if (log_getverbose())
1516 fb1a36c0 2022-01-09 op puts("verbose mode enabled");
1518 fb1a36c0 2022-01-09 op puts("verbose mode disabled");
1522 fb1a36c0 2022-01-09 op if (argc != 1)
1525 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "on")) {
1526 fb1a36c0 2022-01-09 op log_setverbose(1);
1527 fb1a36c0 2022-01-09 op puts("verbose mode enabled");
1531 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "off")) {
1532 fb1a36c0 2022-01-09 op log_setverbose(0);
1533 fb1a36c0 2022-01-09 op puts("verbose mode disabled");
1538 fb1a36c0 2022-01-09 op printf("verbose [on | off]\n");
1542 fb1a36c0 2022-01-09 op excmd(int argc, const char **argv)
1544 fb1a36c0 2022-01-09 op struct cmd {
1545 fb1a36c0 2022-01-09 op const char *name;
1546 fb1a36c0 2022-01-09 op void (*fn)(int, const char **);
1547 fb1a36c0 2022-01-09 op } cmds[] = {
1548 fb1a36c0 2022-01-09 op {"bell", cmd_bell},
1549 fb1a36c0 2022-01-09 op {"bye", cmd_bye},
1550 fb1a36c0 2022-01-09 op {"cd", cmd_cd},
1551 9c48f915 2022-01-16 op {"edit", cmd_edit},
1552 fb1a36c0 2022-01-09 op {"get", cmd_get},
1553 5565d021 2022-01-16 op {"hexdump", cmd_hexdump},
1554 fb1a36c0 2022-01-09 op {"lcd", cmd_lcd},
1555 fb1a36c0 2022-01-09 op {"lpwd", cmd_lpwd},
1556 fb1a36c0 2022-01-09 op {"ls", cmd_ls},
1557 9e7dd230 2022-01-12 op {"page", cmd_page},
1558 1c9ab7cf 2022-01-29 op {"pipe", cmd_pipe},
1559 01fb39ab 2022-01-16 op {"put", cmd_put},
1560 fb1a36c0 2022-01-09 op {"quit", cmd_bye},
1561 c37e1cfe 2022-01-17 op {"rename", cmd_rename},
1562 fb1a36c0 2022-01-09 op {"verbose", cmd_verbose},
1566 fb1a36c0 2022-01-09 op if (argc == 0)
1568 fb1a36c0 2022-01-09 op for (i = 0; i < nitems(cmds); ++i) {
1569 fb1a36c0 2022-01-09 op if (!strcmp(cmds[i].name, *argv)) {
1570 fb1a36c0 2022-01-09 op cmds[i].fn(argc-1, argv+1);
1575 fb1a36c0 2022-01-09 op log_warnx("unknown command %s", *argv);
1579 fb1a36c0 2022-01-09 op main(int argc, char **argv)
1583 fb1a36c0 2022-01-09 op log_init(1, LOG_DAEMON);
1584 fb1a36c0 2022-01-09 op log_setverbose(0);
1585 fb1a36c0 2022-01-09 op log_procinit(getprogname());
1587 fb1a36c0 2022-01-09 op while ((ch = getopt(argc, argv, "C:cK:")) != -1) {
1588 fb1a36c0 2022-01-09 op switch (ch) {
1591 fb1a36c0 2022-01-09 op crtpath = optarg;
1598 fb1a36c0 2022-01-09 op keypath = optarg;
1604 fb1a36c0 2022-01-09 op argc -= optind;
1605 fb1a36c0 2022-01-09 op argv += optind;
1607 3847d69e 2022-01-30 op if (argc == 0 || (tls && crtpath == NULL))
1610 1c9ab7cf 2022-01-29 op signal(SIGPIPE, SIG_IGN);
1611 fb1a36c0 2022-01-09 op if (isatty(1)) {
1613 fb1a36c0 2022-01-09 op resized = 1;
1614 fb1a36c0 2022-01-09 op signal(SIGWINCH, tty_resized);
1617 fb1a36c0 2022-01-09 op if ((evb = evbuffer_new()) == NULL)
1618 fb1a36c0 2022-01-09 op fatal("evbuffer_new");
1620 fb1a36c0 2022-01-09 op if ((buf = evbuffer_new()) == NULL)
1621 fb1a36c0 2022-01-09 op fatal("evbuffer_new");
1623 fb1a36c0 2022-01-09 op if ((dirbuf = evbuffer_new()) == NULL)
1624 fb1a36c0 2022-01-09 op fatal("evbuferr_new");
1626 fb1a36c0 2022-01-09 op do_connect(argv[0], argv[1]);
1629 fb1a36c0 2022-01-09 op int argc = 0;
1630 fb1a36c0 2022-01-09 op char *line, *argv[16] = {0}, **ap;
1632 fb1a36c0 2022-01-09 op if ((line = read_line("kamiftp> ")) == NULL)
1635 fb1a36c0 2022-01-09 op for (argc = 0, ap = argv; ap < &argv[15] &&
1636 fb1a36c0 2022-01-09 op (*ap = strsep(&line, " \t")) != NULL;) {
1637 fb1a36c0 2022-01-09 op if (**ap != '\0')
1638 fb1a36c0 2022-01-09 op ap++, argc++;
1640 fb1a36c0 2022-01-09 op excmd(argc, (const char **)argv);
1643 fb1a36c0 2022-01-09 op printf("\a");
1644 fb1a36c0 2022-01-09 op fflush(stdout);
1650 fb1a36c0 2022-01-09 op printf("\n");