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 fb1a36c0 2022-01-09 op if (msize > MSIZE9P)
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 8f9f99f7 2022-01-16 op if ((errstr = check(Rwalk, iota_tag)) != NULL)
555 8f9f99f7 2022-01-16 op return errstr;
557 fb1a36c0 2022-01-09 op nwqid = np_read16(buf);
558 fb1a36c0 2022-01-09 op assert(nwqid <= nwname);
560 fb1a36c0 2022-01-09 op /* consume all qids */
561 43d117b1 2022-01-16 op for (i = 0; i < nwqid; ++i)
562 fb1a36c0 2022-01-09 op np_read_qid(buf, qid);
566 8f9f99f7 2022-01-16 op *missing = nwname - nwqid;
571 fb1a36c0 2022-01-09 op do_stat(int fid, struct np_stat *st)
576 fb1a36c0 2022-01-09 op expect2(Rstat, iota_tag);
578 fb1a36c0 2022-01-09 op if (np_read_stat(buf, st) == -1)
579 fb1a36c0 2022-01-09 op errx(1, "invalid stat struct read");
581 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
584 c37e1cfe 2022-01-17 op static char *
585 c37e1cfe 2022-01-17 op do_wstat(int fid, const struct np_stat *st)
587 c37e1cfe 2022-01-17 op char *errstr;
589 c37e1cfe 2022-01-17 op twstat(fid, st);
593 c37e1cfe 2022-01-17 op if ((errstr = check(Rwstat, iota_tag)) != NULL)
594 c37e1cfe 2022-01-17 op return errstr;
596 c37e1cfe 2022-01-17 op ASSERT_EMPTYBUF();
601 fb1a36c0 2022-01-09 op static size_t
602 fb1a36c0 2022-01-09 op do_read(int fid, uint64_t off, uint32_t count, void *data)
606 fb1a36c0 2022-01-09 op tread(fid, off, count);
609 fb1a36c0 2022-01-09 op expect2(Rread, iota_tag);
611 fb1a36c0 2022-01-09 op r = np_read32(buf);
612 fb1a36c0 2022-01-09 op assert(r == EVBUFFER_LENGTH(buf));
613 fb1a36c0 2022-01-09 op assert(r <= count);
614 fb1a36c0 2022-01-09 op evbuffer_remove(buf, data, r);
616 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
621 01fb39ab 2022-01-16 op static size_t
622 01fb39ab 2022-01-16 op do_write(int fid, uint64_t off, uint32_t count, void *data)
626 01fb39ab 2022-01-16 op twrite(fid, off, data, count);
629 01fb39ab 2022-01-16 op expect2(Rwrite, iota_tag);
631 01fb39ab 2022-01-16 op r = np_read32(buf);
632 01fb39ab 2022-01-16 op assert(r <= count);
634 01fb39ab 2022-01-16 op ASSERT_EMPTYBUF();
640 fb1a36c0 2022-01-09 op draw_progress(const char *pre, const struct progress *p)
642 fb1a36c0 2022-01-09 op struct winsize ws;
649 fb1a36c0 2022-01-09 op perc = 100.0 * p->done / p->max;
650 fb1a36c0 2022-01-09 op if (!tty_p) {
651 fb1a36c0 2022-01-09 op fprintf(stderr, "%s: %d%%\n", pre, (int)perc);
655 fb1a36c0 2022-01-09 op if (resized) {
658 fb1a36c0 2022-01-09 op if (ioctl(0, TIOCGWINSZ, &ws) == -1)
660 fb1a36c0 2022-01-09 op tty_width = ws.ws_col;
662 fb1a36c0 2022-01-09 op w = tty_width;
664 fb1a36c0 2022-01-09 op if (pre == NULL ||
665 fb1a36c0 2022-01-09 op ((l = printf("\r%s ", pre)) == -1 || l >= w))
668 fb1a36c0 2022-01-09 op w -= l + 2 + 5; /* 2 for |, 5 for percentage + \n */
670 fb1a36c0 2022-01-09 op printf("%4d%%\n", (int)perc);
676 fb1a36c0 2022-01-09 op l = w * MIN(100.0, perc) / 100.0;
677 fb1a36c0 2022-01-09 op for (i = 0; i < l; i++)
679 fb1a36c0 2022-01-09 op for (; i < w; i++)
681 fb1a36c0 2022-01-09 op printf("|%4d%%", (int)perc);
683 fb1a36c0 2022-01-09 op fflush(stdout);
687 76521d26 2022-01-16 op fetch_fid(int fid, int fd, const char *name)
689 fb1a36c0 2022-01-09 op struct progress p = {0};
690 fb1a36c0 2022-01-09 op struct np_stat st;
692 fb1a36c0 2022-01-09 op char buf[BUFSIZ];
694 fb1a36c0 2022-01-09 op do_stat(fid, &st);
695 fb1a36c0 2022-01-09 op do_open(fid, KOREAD);
697 fb1a36c0 2022-01-09 op p.max = st.length;
702 fb1a36c0 2022-01-09 op r = do_read(fid, p.done, sizeof(buf), buf);
706 b2263c45 2022-01-16 op for (off = 0; off < r; off += nw)
707 b2263c45 2022-01-16 op if ((nw = write(fd, buf + off, r - off)) == 0 ||
709 fb1a36c0 2022-01-09 op err(1, "write");
712 9e7dd230 2022-01-12 op draw_progress(name, &p);
715 fb1a36c0 2022-01-09 op /* throttle, for debugging purpose */
717 fb1a36c0 2022-01-09 op struct timespec ts = { 0, 500000000 };
718 fb1a36c0 2022-01-09 op nanosleep(&ts, NULL);
723 fb1a36c0 2022-01-09 op putchar('\n');
725 01fb39ab 2022-01-16 op do_clunk(fid);
729 01fb39ab 2022-01-16 op send_fid(int fid, const char *fnam, int fd, const char *name)
731 01fb39ab 2022-01-16 op struct progress p = {0};
732 01fb39ab 2022-01-16 op struct stat sb;
735 01fb39ab 2022-01-16 op char buf[BUFSIZ];
737 01fb39ab 2022-01-16 op if (fstat(fd, &sb) == -1)
738 01fb39ab 2022-01-16 op err(1, "fstat");
740 01fb39ab 2022-01-16 op if (fnam != NULL)
741 01fb39ab 2022-01-16 op do_create(fid, fnam, 0644, KOWRITE);
743 01fb39ab 2022-01-16 op do_open(fid, KOWRITE);
745 01fb39ab 2022-01-16 op p.max = sb.st_size;
747 01fb39ab 2022-01-16 op r = read(fd, buf, sizeof(buf));
751 01fb39ab 2022-01-16 op err(1, "read");
753 01fb39ab 2022-01-16 op w = do_write(fid, p.done, r, buf);
756 01fb39ab 2022-01-16 op draw_progress(name, &p);
759 01fb39ab 2022-01-16 op /* throttle, for debugging purpose */
761 01fb39ab 2022-01-16 op struct timespec ts = { 0, 500000000 };
762 01fb39ab 2022-01-16 op nanosleep(&ts, NULL);
767 01fb39ab 2022-01-16 op putchar('\n');
768 fb1a36c0 2022-01-09 op do_clunk(fid);
772 9c48f915 2022-01-16 op woc_file(int fd, const char *prompt, const char *path)
774 9c48f915 2022-01-16 op struct qid qid;
775 9c48f915 2022-01-16 op const char *n = NULL;
776 9c48f915 2022-01-16 op char *errstr;
777 9c48f915 2022-01-16 op int nfid, miss;
779 8d8fb849 2022-01-17 op nfid = nextfid();
780 9c48f915 2022-01-16 op errstr = walk_path(pwdfid, nfid, path, &miss, &qid);
781 9c48f915 2022-01-16 op if (errstr != NULL && miss > 1) {
782 9c48f915 2022-01-16 op printf("%s: %s\n", path, errstr);
783 9c48f915 2022-01-16 op free(errstr);
787 9c48f915 2022-01-16 op if (errstr != NULL || miss == 1) {
788 9c48f915 2022-01-16 op char p[PATH_MAX], *dn;
791 9c48f915 2022-01-16 op * If it's only one component missing (the file name), walk
792 9c48f915 2022-01-16 op * to the parent directory and try to create the file.
795 9c48f915 2022-01-16 op if (strlcpy(p, path, sizeof(p)) >= sizeof(p)) {
796 9c48f915 2022-01-16 op printf("path too long: %s\n", path);
799 9c48f915 2022-01-16 op dn = dirname(p);
801 9c48f915 2022-01-16 op if (!strcmp(dn, ".")) {
802 9c48f915 2022-01-16 op errstr = dup_fid(pwdfid, nfid);
805 9c48f915 2022-01-16 op errstr = walk_path(pwdfid, nfid, dn, &miss, &qid);
807 9c48f915 2022-01-16 op if (errstr != NULL) {
808 9c48f915 2022-01-16 op printf("%s: %s\n", dn, errstr);
809 9c48f915 2022-01-16 op free(errstr);
813 9c48f915 2022-01-16 op if (miss != 0) {
814 9c48f915 2022-01-16 op printf("%s: not a directory\n", dn);
818 9c48f915 2022-01-16 op if ((n = strrchr(path, '/')) != NULL)
824 9c48f915 2022-01-16 op free(errstr);
826 9c48f915 2022-01-16 op if (miss > 1) {
827 9c48f915 2022-01-16 op printf("can't create %s: missing %d path component(s)\n",
832 9c48f915 2022-01-16 op send_fid(nfid, n, fd, prompt);
837 fb1a36c0 2022-01-09 op do_tls_connect(const char *host, const char *port)
839 fb1a36c0 2022-01-09 op int handshake;
841 fb1a36c0 2022-01-09 op if ((tlsconf = tls_config_new()) == NULL)
842 fb1a36c0 2022-01-09 op fatalx("tls_config_new");
843 fb1a36c0 2022-01-09 op tls_config_insecure_noverifycert(tlsconf);
844 fb1a36c0 2022-01-09 op tls_config_insecure_noverifyname(tlsconf);
845 fb1a36c0 2022-01-09 op if (tls_config_set_keypair_file(tlsconf, crtpath, keypath) == -1)
846 fb1a36c0 2022-01-09 op fatalx("can't load certs (%s, %s)", crtpath, keypath);
848 fb1a36c0 2022-01-09 op if ((ctx = tls_client()) == NULL)
849 fb1a36c0 2022-01-09 op fatal("tls_client");
850 fb1a36c0 2022-01-09 op if (tls_configure(ctx, tlsconf) == -1)
851 fb1a36c0 2022-01-09 op fatalx("tls_configure: %s", tls_error(ctx));
853 fb1a36c0 2022-01-09 op if (tls_connect(ctx, host, port) == -1)
854 fb1a36c0 2022-01-09 op fatalx("can't connect to %s:%s: %s", host, port,
855 fb1a36c0 2022-01-09 op tls_error(ctx));
857 fb1a36c0 2022-01-09 op for (handshake = 0; !handshake;) {
858 fb1a36c0 2022-01-09 op switch (tls_handshake(ctx)) {
860 fb1a36c0 2022-01-09 op fatalx("tls_handshake: %s", tls_error(ctx));
862 fb1a36c0 2022-01-09 op handshake = 1;
869 fb1a36c0 2022-01-09 op do_ctxt_connect(const char *host, const char *port)
871 fb1a36c0 2022-01-09 op struct addrinfo hints, *res, *res0;
872 fb1a36c0 2022-01-09 op int error, saved_errno;
873 fb1a36c0 2022-01-09 op const char *cause = NULL;
875 fb1a36c0 2022-01-09 op memset(&hints, 0, sizeof(hints));
876 fb1a36c0 2022-01-09 op hints.ai_family = AF_UNSPEC;
877 fb1a36c0 2022-01-09 op hints.ai_socktype = SOCK_STREAM;
878 fb1a36c0 2022-01-09 op error = getaddrinfo(host, port, &hints, &res0);
880 fb1a36c0 2022-01-09 op errx(1, "%s", gai_strerror(error));
883 fb1a36c0 2022-01-09 op for (res = res0; res != NULL; res = res->ai_next) {
884 db066f1e 2022-01-12 op sock = socket(res->ai_family, res->ai_socktype|SOCK_CLOEXEC,
885 fb1a36c0 2022-01-09 op res->ai_protocol);
886 fb1a36c0 2022-01-09 op if (sock == -1) {
887 fb1a36c0 2022-01-09 op cause = "socket";
891 fb1a36c0 2022-01-09 op if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
892 fb1a36c0 2022-01-09 op cause = "connect";
893 fb1a36c0 2022-01-09 op saved_errno = errno;
895 fb1a36c0 2022-01-09 op errno = saved_errno;
903 fb1a36c0 2022-01-09 op if (sock == -1)
904 fb1a36c0 2022-01-09 op err(1, "%s", cause);
905 fb1a36c0 2022-01-09 op freeaddrinfo(res0);
909 fb1a36c0 2022-01-09 op do_connect(const char *connspec, const char *path)
911 fb1a36c0 2022-01-09 op char *host, *colon;
912 fb1a36c0 2022-01-09 op const char *port;
914 fb1a36c0 2022-01-09 op host = xstrdup(connspec);
915 fb1a36c0 2022-01-09 op if ((colon = strchr(host, ':')) != NULL) {
916 fb1a36c0 2022-01-09 op *colon = '\0';
917 fb1a36c0 2022-01-09 op port = ++colon;
919 fb1a36c0 2022-01-09 op port = "1337";
921 fb1a36c0 2022-01-09 op printf("connecting to %s:%s...", host, port);
922 fb1a36c0 2022-01-09 op fflush(stdout);
925 fb1a36c0 2022-01-09 op do_tls_connect(host, port);
927 fb1a36c0 2022-01-09 op do_ctxt_connect(host, port);
929 fb1a36c0 2022-01-09 op printf(" done!\n");
931 fb1a36c0 2022-01-09 op do_version();
932 fb1a36c0 2022-01-09 op do_attach(path);
938 64d8db89 2022-01-16 op tmp_file(char sfn[TMPFSTRLEN])
942 64d8db89 2022-01-16 op strlcpy(sfn, TMPFSTR, TMPFSTRLEN);
943 64d8db89 2022-01-16 op if ((tmpfd = mkstemp(sfn)) == -1) {
944 64d8db89 2022-01-16 op warn("mkstemp %s", sfn);
948 aa76fd7f 2022-01-16 op /* set the close-on-exec flag */
949 aa76fd7f 2022-01-16 op if (fcntl(tmpfd, F_SETFD, FD_CLOEXEC) == -1) {
950 aa76fd7f 2022-01-16 op warn("fcntl");
951 aa76fd7f 2022-01-16 op close(tmpfd);
955 64d8db89 2022-01-16 op return tmpfd;
958 1f5fb843 2022-01-16 op static inline const char *
959 1f5fb843 2022-01-16 op pp_perm(uint8_t x)
961 1f5fb843 2022-01-16 op switch (x & 0x7) {
963 1f5fb843 2022-01-16 op return "---";
965 1f5fb843 2022-01-16 op return "--x";
967 1f5fb843 2022-01-16 op return "-w-";
969 1f5fb843 2022-01-16 op return "-wx";
971 1f5fb843 2022-01-16 op return "r--";
973 1f5fb843 2022-01-16 op return "r-x";
975 1f5fb843 2022-01-16 op return "rw-";
977 1f5fb843 2022-01-16 op return "rwx";
979 1f5fb843 2022-01-16 op /* unreachable, just for the compiler' happiness */
980 1f5fb843 2022-01-16 op return "???";
984 c37e1cfe 2022-01-17 op static inline void
985 c37e1cfe 2022-01-17 op prepare_wstat(struct np_stat *st)
987 c37e1cfe 2022-01-17 op memset(st, 0xFF, sizeof(*st));
988 c37e1cfe 2022-01-17 op st->name = NULL;
989 c37e1cfe 2022-01-17 op st->uid = NULL;
990 c37e1cfe 2022-01-17 op st->gid = NULL;
991 c37e1cfe 2022-01-17 op st->muid = NULL;
995 fb1a36c0 2022-01-09 op cmd_bell(int argc, const char **argv)
997 fb1a36c0 2022-01-09 op if (argc == 0) {
998 fb1a36c0 2022-01-09 op bell = !bell;
1000 fb1a36c0 2022-01-09 op puts("bell mode enabled");
1002 fb1a36c0 2022-01-09 op puts("bell mode disabled");
1006 fb1a36c0 2022-01-09 op if (argc != 1)
1009 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "on")) {
1011 fb1a36c0 2022-01-09 op puts("bell mode enabled");
1015 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "off")) {
1017 fb1a36c0 2022-01-09 op puts("bell mode disabled");
1022 fb1a36c0 2022-01-09 op printf("bell [on | off]\n");
1026 fb1a36c0 2022-01-09 op cmd_bye(int argc, const char **argv)
1028 fb1a36c0 2022-01-09 op log_warnx("bye\n");
1033 fb1a36c0 2022-01-09 op cmd_cd(int argc, const char **argv)
1035 fb1a36c0 2022-01-09 op struct qid qid;
1036 8f9f99f7 2022-01-16 op int nfid, miss;
1037 8f9f99f7 2022-01-16 op char *errstr;
1039 fb1a36c0 2022-01-09 op if (argc != 1) {
1040 fb1a36c0 2022-01-09 op printf("usage: cd remote-path\n");
1044 8d8fb849 2022-01-17 op nfid = nextfid();
1045 8f9f99f7 2022-01-16 op errstr = walk_path(pwdfid, nfid, argv[0], &miss, &qid);
1046 8f9f99f7 2022-01-16 op if (errstr != NULL) {
1047 8f9f99f7 2022-01-16 op printf("%s: %s\n", argv[0], errstr);
1048 8f9f99f7 2022-01-16 op free(errstr);
1052 8f9f99f7 2022-01-16 op if (miss != 0 || !(qid.type & QTDIR)) {
1053 8f9f99f7 2022-01-16 op printf("%s: not a directory\n", argv[0]);
1054 fcd9d510 2022-01-16 op if (miss == 0)
1055 fcd9d510 2022-01-16 op do_clunk(nfid);
1059 8f9f99f7 2022-01-16 op do_clunk(pwdfid);
1060 8f9f99f7 2022-01-16 op pwdfid = nfid;
1064 9c48f915 2022-01-16 op cmd_edit(int argc, const char **argv)
1066 9c48f915 2022-01-16 op struct qid qid;
1067 9c48f915 2022-01-16 op int nfid, tmpfd, miss;
1068 9c48f915 2022-01-16 op char sfn[TMPFSTRLEN], p[PATH_MAX], *name, *errstr;
1069 9c48f915 2022-01-16 op const char *ed;
1071 9c48f915 2022-01-16 op if (argc != 1) {
1072 9c48f915 2022-01-16 op puts("usage: edit file");
1076 9c48f915 2022-01-16 op if ((ed = getenv("VISUAL")) == NULL &&
1077 9c48f915 2022-01-16 op (ed = getenv("EDITOR")) == NULL)
1080 8d8fb849 2022-01-17 op nfid = nextfid();
1081 9c48f915 2022-01-16 op errstr = walk_path(pwdfid, nfid, *argv, &miss, &qid);
1082 9c48f915 2022-01-16 op if (errstr != NULL) {
1083 9c48f915 2022-01-16 op printf("%s: %s\n", *argv, errstr);
1084 9c48f915 2022-01-16 op free(errstr);
1088 9c48f915 2022-01-16 op if (miss != 0 || qid.type != 0) {
1089 9c48f915 2022-01-16 op printf("%s: not a file\n", *argv);
1090 9c48f915 2022-01-16 op if (miss == 0)
1091 9c48f915 2022-01-16 op do_clunk(nfid);
1095 9c48f915 2022-01-16 op if ((tmpfd = tmp_file(sfn)) == -1) {
1096 9c48f915 2022-01-16 op do_clunk(nfid);
1100 9c48f915 2022-01-16 op strlcpy(p, *argv, sizeof(p));
1101 9c48f915 2022-01-16 op name = basename(p);
1103 9c48f915 2022-01-16 op fetch_fid(nfid, tmpfd, name);
1104 9c48f915 2022-01-16 op close(tmpfd);
1106 9c48f915 2022-01-16 op spawn(ed, sfn, NULL);
1109 9c48f915 2022-01-16 op * Re-open the file because it's not guaranteed that the
1110 9c48f915 2022-01-16 op * file descriptor tmpfd is still associated with the file
1111 9c48f915 2022-01-16 op * pointed by sfn: it's not uncommon for editor to write
1112 9c48f915 2022-01-16 op * a backup file and then rename(2) it to the file name.
1114 9c48f915 2022-01-16 op if ((tmpfd = open(sfn, O_RDONLY)) == -1) {
1115 9c48f915 2022-01-16 op warn("can't open %s", sfn);
1119 9c48f915 2022-01-16 op woc_file(tmpfd, *argv, name);
1120 9c48f915 2022-01-16 op close(tmpfd);
1123 9c48f915 2022-01-16 op unlink(sfn);
1127 fb1a36c0 2022-01-09 op cmd_get(int argc, const char **argv)
1129 fb1a36c0 2022-01-09 op struct qid qid;
1130 fb1a36c0 2022-01-09 op const char *l;
1131 8f9f99f7 2022-01-16 op char *errstr;
1132 8f9f99f7 2022-01-16 op int nfid, fd, miss;
1134 fb1a36c0 2022-01-09 op if (argc != 1 && argc != 2) {
1135 fb1a36c0 2022-01-09 op printf("usage: get remote-file [local-file]\n");
1139 fb1a36c0 2022-01-09 op if (argc == 2)
1140 fb1a36c0 2022-01-09 op l = argv[1];
1141 fb1a36c0 2022-01-09 op else if ((l = strrchr(argv[0], '/')) != NULL)
1142 fb1a36c0 2022-01-09 op l++; /* skip / */
1144 2ba25711 2022-01-16 op l = argv[0];
1146 8d8fb849 2022-01-17 op nfid = nextfid();
1147 8f9f99f7 2022-01-16 op errstr = walk_path(pwdfid, nfid, argv[0], &miss, &qid);
1148 8f9f99f7 2022-01-16 op if (errstr != NULL) {
1149 8f9f99f7 2022-01-16 op printf("%s: %s\n", argv[0], errstr);
1150 8f9f99f7 2022-01-16 op free(errstr);
1154 8f9f99f7 2022-01-16 op if (miss != 0 || qid.type != 0) {
1155 8f9f99f7 2022-01-16 op printf("%s: not a file\n", argv[0]);
1156 fcd9d510 2022-01-16 op if (miss == 0)
1157 fcd9d510 2022-01-16 op do_clunk(nfid);
1161 76521d26 2022-01-16 op if ((fd = open(l, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0644)) == -1) {
1162 76521d26 2022-01-16 op warn("can't open %s", l);
1163 76521d26 2022-01-16 op do_clunk(nfid);
1167 76521d26 2022-01-16 op fetch_fid(nfid, fd, l);
1172 5565d021 2022-01-16 op cmd_hexdump(int argc, const char **argv)
1174 5565d021 2022-01-16 op if (argc == 0) {
1175 5565d021 2022-01-16 op xdump = !xdump;
1177 5565d021 2022-01-16 op puts("hexdump mode enabled");
1179 5565d021 2022-01-16 op puts("hexdump mode disabled");
1183 5565d021 2022-01-16 op if (argc > 1)
1186 5565d021 2022-01-16 op if (!strcmp(*argv, "on")) {
1188 5565d021 2022-01-16 op puts("hexdump mode enabled");
1192 5565d021 2022-01-16 op if (!strcmp(*argv, "off")) {
1194 5565d021 2022-01-16 op puts("hexdump mode disabled");
1199 5565d021 2022-01-16 op puts("usage: hexdump [on | off]");
1203 fb1a36c0 2022-01-09 op cmd_lcd(int argc, const char **argv)
1205 fb1a36c0 2022-01-09 op const char *dir;
1207 fb1a36c0 2022-01-09 op if (argc > 1) {
1208 d166ffc0 2022-01-16 op printf("usage: lcd [local-directory]\n");
1212 fb1a36c0 2022-01-09 op if (argc == 1)
1213 fb1a36c0 2022-01-09 op dir = *argv;
1215 fb1a36c0 2022-01-09 op if (argc == 0 && (dir = getenv("HOME")) == NULL) {
1216 fb1a36c0 2022-01-09 op printf("HOME is not defined\n");
1220 fb1a36c0 2022-01-09 op if (chdir(dir) == -1)
1221 fb1a36c0 2022-01-09 op printf("cd: %s: %s\n", dir, strerror(errno));
1225 fb1a36c0 2022-01-09 op cmd_lpwd(int argc, const char **argv)
1227 fb1a36c0 2022-01-09 op char path[PATH_MAX];
1229 d166ffc0 2022-01-16 op if (argc != 0) {
1230 d166ffc0 2022-01-16 op printf("usage: lpwd\n");
1234 fb1a36c0 2022-01-09 op if (getcwd(path, sizeof(path)) == NULL) {
1235 fb1a36c0 2022-01-09 op printf("lpwd: %s\n", strerror(errno));
1239 fb1a36c0 2022-01-09 op printf("%s\n", path);
1243 fb1a36c0 2022-01-09 op cmd_ls(int argc, const char **argv)
1245 fb1a36c0 2022-01-09 op struct np_stat st;
1246 4f52d9af 2022-01-16 op time_t now, mtime;
1247 4f52d9af 2022-01-16 op struct tm *tm;
1248 fb1a36c0 2022-01-09 op uint64_t off = 0;
1249 fb1a36c0 2022-01-09 op uint32_t len;
1251 4f52d9af 2022-01-16 op const char *timfmt;
1252 4f52d9af 2022-01-16 op char fmt[FMT_SCALED_STRSIZE], tim[13], *errstr;
1254 fb1a36c0 2022-01-09 op if (argc != 0) {
1255 fb1a36c0 2022-01-09 op printf("ls don't take arguments (yet)\n");
1259 4f52d9af 2022-01-16 op now = time(NULL);
1261 a9288ad9 2022-01-17 op nfid = nextfid();
1262 a9288ad9 2022-01-17 op if ((errstr = dup_fid(pwdfid, nfid)) != NULL) {
1263 1bb1bfca 2022-01-16 op printf(".: %s\n", errstr);
1264 1bb1bfca 2022-01-16 op free(errstr);
1268 a9288ad9 2022-01-17 op do_open(nfid, KOREAD);
1270 fb1a36c0 2022-01-09 op evbuffer_drain(dirbuf, EVBUFFER_LENGTH(dirbuf));
1273 a9288ad9 2022-01-17 op tread(nfid, off, BUFSIZ);
1276 fb1a36c0 2022-01-09 op expect2(Rread, iota_tag);
1278 fb1a36c0 2022-01-09 op len = np_read32(buf);
1279 fb1a36c0 2022-01-09 op if (len == 0)
1282 fb1a36c0 2022-01-09 op evbuffer_add_buffer(dirbuf, buf);
1285 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
1288 fb1a36c0 2022-01-09 op while (EVBUFFER_LENGTH(dirbuf) != 0) {
1289 fb1a36c0 2022-01-09 op if (np_read_stat(dirbuf, &st) == -1)
1290 fb1a36c0 2022-01-09 op errx(1, "invalid stat struct read");
1292 fb1a36c0 2022-01-09 op if (fmt_scaled(st.length, fmt) == -1)
1293 fb1a36c0 2022-01-09 op strlcpy(fmt, "xxx", sizeof(fmt));
1295 4f52d9af 2022-01-16 op mtime = st.mtime;
1297 4f52d9af 2022-01-16 op if (now > mtime && (now - mtime) < 365/2 * 24 * 12 * 60)
1298 4f52d9af 2022-01-16 op timfmt = "%b %e %R";
1300 4f52d9af 2022-01-16 op timfmt = "%b %e %Y";
1302 4f52d9af 2022-01-16 op if ((tm = localtime(&mtime)) == NULL ||
1303 4f52d9af 2022-01-16 op strftime(tim, sizeof(tim), timfmt, tm) == 0)
1304 4f52d9af 2022-01-16 op strlcpy(tim, "unknown", sizeof(tim));
1306 1f5fb843 2022-01-16 op if (st.qid.type & QTDIR)
1307 1f5fb843 2022-01-16 op printf("d");
1309 1f5fb843 2022-01-16 op printf("-");
1310 1f5fb843 2022-01-16 op printf("%s", pp_perm(st.mode >> 6));
1311 1f5fb843 2022-01-16 op printf("%s", pp_perm(st.mode >> 3));
1312 1f5fb843 2022-01-16 op printf("%s", pp_perm(st.mode));
1313 d33c93c4 2022-01-17 op printf(" %8s %12s %s%s\n", fmt, tim, st.name,
1314 6470594e 2022-01-16 op st.qid.type & QTDIR ? "/" : "");
1316 fb1a36c0 2022-01-09 op free(st.name);
1317 fb1a36c0 2022-01-09 op free(st.uid);
1318 fb1a36c0 2022-01-09 op free(st.gid);
1319 fb1a36c0 2022-01-09 op free(st.muid);
1322 a9288ad9 2022-01-17 op do_clunk(nfid);
1326 9e7dd230 2022-01-12 op cmd_page(int argc, const char **argv)
1328 9e7dd230 2022-01-12 op struct qid qid;
1329 8f9f99f7 2022-01-16 op int nfid, tmpfd, miss;
1330 64d8db89 2022-01-16 op char sfn[TMPFSTRLEN], p[PATH_MAX], *name, *errstr;
1331 62f663bb 2022-01-16 op const char *pager;
1333 9e7dd230 2022-01-12 op if (argc != 1) {
1334 9e7dd230 2022-01-12 op puts("usage: page file");
1338 62f663bb 2022-01-16 op if ((pager = getenv("PAGER")) == NULL)
1339 62f663bb 2022-01-16 op pager = "less";
1341 8d8fb849 2022-01-17 op nfid = nextfid();
1342 8f9f99f7 2022-01-16 op errstr = walk_path(pwdfid, nfid, *argv, &miss, &qid);
1343 8f9f99f7 2022-01-16 op if (errstr != NULL) {
1344 8f9f99f7 2022-01-16 op printf("%s: %s\n", *argv, errstr);
1345 8f9f99f7 2022-01-16 op free(errstr);
1349 8f9f99f7 2022-01-16 op if (miss != 0 || qid.type != 0) {
1350 8f9f99f7 2022-01-16 op printf("%s: not a file\n", *argv);
1351 fcd9d510 2022-01-16 op if (miss == 0)
1352 fcd9d510 2022-01-16 op do_clunk(nfid);
1356 64d8db89 2022-01-16 op if ((tmpfd = tmp_file(sfn)) == -1) {
1357 9e7dd230 2022-01-12 op do_clunk(nfid);
1361 9e7dd230 2022-01-12 op strlcpy(p, *argv, sizeof(p));
1362 9e7dd230 2022-01-12 op name = basename(p);
1363 76521d26 2022-01-16 op fetch_fid(nfid, tmpfd, name);
1364 9e7dd230 2022-01-12 op close(tmpfd);
1365 62f663bb 2022-01-16 op spawn(pager, sfn, NULL);
1366 9e7dd230 2022-01-12 op unlink(sfn);
1370 01fb39ab 2022-01-16 op cmd_put(int argc, const char **argv)
1372 01fb39ab 2022-01-16 op struct qid qid;
1373 9c48f915 2022-01-16 op const char *l;
1376 01fb39ab 2022-01-16 op if (argc != 1 && argc != 2) {
1377 01fb39ab 2022-01-16 op printf("usage: put local-file [remote-file]\n");
1381 01fb39ab 2022-01-16 op if (argc == 2)
1382 01fb39ab 2022-01-16 op l = argv[1];
1383 01fb39ab 2022-01-16 op else if ((l = strrchr(argv[0], '/')) != NULL)
1384 01fb39ab 2022-01-16 op l++; /* skip / */
1386 01fb39ab 2022-01-16 op l = argv[0];
1388 01fb39ab 2022-01-16 op if ((fd = open(argv[0], O_RDONLY)) == -1) {
1389 01fb39ab 2022-01-16 op warn("%s", argv[0]);
1393 9c48f915 2022-01-16 op woc_file(fd, argv[0], l);
1398 c37e1cfe 2022-01-17 op cmd_rename(int argc, const char **argv)
1400 c37e1cfe 2022-01-17 op struct np_stat st;
1401 c37e1cfe 2022-01-17 op struct qid qid;
1402 c37e1cfe 2022-01-17 op char *errstr;
1403 c37e1cfe 2022-01-17 op int nfid, miss;
1405 c37e1cfe 2022-01-17 op if (argc != 2) {
1406 c37e1cfe 2022-01-17 op puts("usage: rename remote-file new-remote-name");
1410 8d8fb849 2022-01-17 op nfid = nextfid();
1411 c37e1cfe 2022-01-17 op errstr = walk_path(pwdfid, nfid, argv[0], &miss, &qid);
1412 c37e1cfe 2022-01-17 op if (errstr != NULL) {
1413 c37e1cfe 2022-01-17 op printf("%s: %s\n", argv[0], errstr);
1414 c37e1cfe 2022-01-17 op free(errstr);
1418 c37e1cfe 2022-01-17 op if (miss != 0) {
1419 c37e1cfe 2022-01-17 op printf("%s: not such file or directory\n", argv[0]);
1423 c37e1cfe 2022-01-17 op prepare_wstat(&st);
1424 c37e1cfe 2022-01-17 op st.name = (char *)argv[1];
1425 c37e1cfe 2022-01-17 op if ((errstr = do_wstat(nfid, &st)) != NULL) {
1426 c37e1cfe 2022-01-17 op printf("rename: %s\n", errstr);
1427 c37e1cfe 2022-01-17 op free(errstr);
1430 c37e1cfe 2022-01-17 op do_clunk(nfid);
1434 fb1a36c0 2022-01-09 op cmd_verbose(int argc, const char **argv)
1436 fb1a36c0 2022-01-09 op if (argc == 0) {
1437 fb1a36c0 2022-01-09 op log_setverbose(!log_getverbose());
1438 fb1a36c0 2022-01-09 op if (log_getverbose())
1439 fb1a36c0 2022-01-09 op puts("verbose mode enabled");
1441 fb1a36c0 2022-01-09 op puts("verbose mode disabled");
1445 fb1a36c0 2022-01-09 op if (argc != 1)
1448 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "on")) {
1449 fb1a36c0 2022-01-09 op log_setverbose(1);
1450 fb1a36c0 2022-01-09 op puts("verbose mode enabled");
1454 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "off")) {
1455 fb1a36c0 2022-01-09 op log_setverbose(0);
1456 fb1a36c0 2022-01-09 op puts("verbose mode disabled");
1461 fb1a36c0 2022-01-09 op printf("verbose [on | off]\n");
1465 fb1a36c0 2022-01-09 op excmd(int argc, const char **argv)
1467 fb1a36c0 2022-01-09 op struct cmd {
1468 fb1a36c0 2022-01-09 op const char *name;
1469 fb1a36c0 2022-01-09 op void (*fn)(int, const char **);
1470 fb1a36c0 2022-01-09 op } cmds[] = {
1471 fb1a36c0 2022-01-09 op {"bell", cmd_bell},
1472 fb1a36c0 2022-01-09 op {"bye", cmd_bye},
1473 fb1a36c0 2022-01-09 op {"cd", cmd_cd},
1474 9c48f915 2022-01-16 op {"edit", cmd_edit},
1475 fb1a36c0 2022-01-09 op {"get", cmd_get},
1476 5565d021 2022-01-16 op {"hexdump", cmd_hexdump},
1477 fb1a36c0 2022-01-09 op {"lcd", cmd_lcd},
1478 fb1a36c0 2022-01-09 op {"lpwd", cmd_lpwd},
1479 fb1a36c0 2022-01-09 op {"ls", cmd_ls},
1480 9e7dd230 2022-01-12 op {"page", cmd_page},
1481 01fb39ab 2022-01-16 op {"put", cmd_put},
1482 fb1a36c0 2022-01-09 op {"quit", cmd_bye},
1483 c37e1cfe 2022-01-17 op {"rename", cmd_rename},
1484 fb1a36c0 2022-01-09 op {"verbose", cmd_verbose},
1488 fb1a36c0 2022-01-09 op if (argc == 0)
1490 fb1a36c0 2022-01-09 op for (i = 0; i < nitems(cmds); ++i) {
1491 fb1a36c0 2022-01-09 op if (!strcmp(cmds[i].name, *argv)) {
1492 fb1a36c0 2022-01-09 op cmds[i].fn(argc-1, argv+1);
1497 fb1a36c0 2022-01-09 op log_warnx("unknown command %s", *argv);
1501 fb1a36c0 2022-01-09 op main(int argc, char **argv)
1505 fb1a36c0 2022-01-09 op log_init(1, LOG_DAEMON);
1506 fb1a36c0 2022-01-09 op log_setverbose(0);
1507 fb1a36c0 2022-01-09 op log_procinit(getprogname());
1509 fb1a36c0 2022-01-09 op while ((ch = getopt(argc, argv, "C:cK:")) != -1) {
1510 fb1a36c0 2022-01-09 op switch (ch) {
1512 fb1a36c0 2022-01-09 op crtpath = optarg;
1518 fb1a36c0 2022-01-09 op keypath = optarg;
1524 fb1a36c0 2022-01-09 op argc -= optind;
1525 fb1a36c0 2022-01-09 op argv += optind;
1527 fb1a36c0 2022-01-09 op if (argc == 0)
1530 fb1a36c0 2022-01-09 op if (isatty(1)) {
1532 fb1a36c0 2022-01-09 op resized = 1;
1533 fb1a36c0 2022-01-09 op signal(SIGWINCH, tty_resized);
1536 fb1a36c0 2022-01-09 op if ((evb = evbuffer_new()) == NULL)
1537 fb1a36c0 2022-01-09 op fatal("evbuffer_new");
1539 fb1a36c0 2022-01-09 op if ((buf = evbuffer_new()) == NULL)
1540 fb1a36c0 2022-01-09 op fatal("evbuffer_new");
1542 fb1a36c0 2022-01-09 op if ((dirbuf = evbuffer_new()) == NULL)
1543 fb1a36c0 2022-01-09 op fatal("evbuferr_new");
1545 fb1a36c0 2022-01-09 op do_connect(argv[0], argv[1]);
1548 fb1a36c0 2022-01-09 op int argc = 0;
1549 fb1a36c0 2022-01-09 op char *line, *argv[16] = {0}, **ap;
1551 fb1a36c0 2022-01-09 op if ((line = read_line("kamiftp> ")) == NULL)
1554 fb1a36c0 2022-01-09 op for (argc = 0, ap = argv; ap < &argv[15] &&
1555 fb1a36c0 2022-01-09 op (*ap = strsep(&line, " \t")) != NULL;) {
1556 fb1a36c0 2022-01-09 op if (**ap != '\0')
1557 fb1a36c0 2022-01-09 op ap++, argc++;
1559 fb1a36c0 2022-01-09 op excmd(argc, (const char **)argv);
1562 fb1a36c0 2022-01-09 op printf("\a");
1563 fb1a36c0 2022-01-09 op fflush(stdout);
1569 fb1a36c0 2022-01-09 op printf("\n");