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 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 fb1a36c0 2022-01-09 op if (np_read_stat(buf, st) == -1)
581 fb1a36c0 2022-01-09 op errx(1, "invalid stat struct read");
583 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
586 c37e1cfe 2022-01-17 op static char *
587 c37e1cfe 2022-01-17 op do_wstat(int fid, const struct np_stat *st)
589 c37e1cfe 2022-01-17 op char *errstr;
591 c37e1cfe 2022-01-17 op twstat(fid, st);
595 c37e1cfe 2022-01-17 op if ((errstr = check(Rwstat, iota_tag)) != NULL)
596 c37e1cfe 2022-01-17 op return errstr;
598 c37e1cfe 2022-01-17 op ASSERT_EMPTYBUF();
603 fb1a36c0 2022-01-09 op static size_t
604 fb1a36c0 2022-01-09 op do_read(int fid, uint64_t off, uint32_t count, void *data)
608 fb1a36c0 2022-01-09 op tread(fid, off, count);
611 fb1a36c0 2022-01-09 op expect2(Rread, iota_tag);
613 fb1a36c0 2022-01-09 op r = np_read32(buf);
614 fb1a36c0 2022-01-09 op assert(r == EVBUFFER_LENGTH(buf));
615 fb1a36c0 2022-01-09 op assert(r <= count);
616 fb1a36c0 2022-01-09 op evbuffer_remove(buf, data, r);
618 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
623 01fb39ab 2022-01-16 op static size_t
624 01fb39ab 2022-01-16 op do_write(int fid, uint64_t off, uint32_t count, void *data)
628 01fb39ab 2022-01-16 op twrite(fid, off, data, count);
631 01fb39ab 2022-01-16 op expect2(Rwrite, iota_tag);
633 01fb39ab 2022-01-16 op r = np_read32(buf);
634 01fb39ab 2022-01-16 op assert(r <= count);
636 01fb39ab 2022-01-16 op ASSERT_EMPTYBUF();
642 fb1a36c0 2022-01-09 op draw_progress(const char *pre, const struct progress *p)
644 fb1a36c0 2022-01-09 op struct winsize ws;
651 fb1a36c0 2022-01-09 op perc = 100.0 * p->done / p->max;
652 fb1a36c0 2022-01-09 op if (!tty_p) {
653 fb1a36c0 2022-01-09 op fprintf(stderr, "%s: %d%%\n", pre, (int)perc);
657 fb1a36c0 2022-01-09 op if (resized) {
660 fb1a36c0 2022-01-09 op if (ioctl(0, TIOCGWINSZ, &ws) == -1)
662 fb1a36c0 2022-01-09 op tty_width = ws.ws_col;
664 fb1a36c0 2022-01-09 op w = tty_width;
666 fb1a36c0 2022-01-09 op if (pre == NULL ||
667 fb1a36c0 2022-01-09 op ((l = printf("\r%s ", pre)) == -1 || l >= w))
670 fb1a36c0 2022-01-09 op w -= l + 2 + 5; /* 2 for |, 5 for percentage + \n */
672 fb1a36c0 2022-01-09 op printf("%4d%%\n", (int)perc);
678 fb1a36c0 2022-01-09 op l = w * MIN(100.0, perc) / 100.0;
679 fb1a36c0 2022-01-09 op for (i = 0; i < l; i++)
681 fb1a36c0 2022-01-09 op for (; i < w; i++)
683 fb1a36c0 2022-01-09 op printf("|%4d%%", (int)perc);
685 fb1a36c0 2022-01-09 op fflush(stdout);
689 76521d26 2022-01-16 op fetch_fid(int fid, int fd, const char *name)
691 fb1a36c0 2022-01-09 op struct progress p = {0};
692 fb1a36c0 2022-01-09 op struct np_stat st;
694 fb1a36c0 2022-01-09 op char buf[BUFSIZ];
696 fb1a36c0 2022-01-09 op do_stat(fid, &st);
697 fb1a36c0 2022-01-09 op do_open(fid, KOREAD);
699 fb1a36c0 2022-01-09 op p.max = st.length;
704 fb1a36c0 2022-01-09 op r = do_read(fid, p.done, sizeof(buf), buf);
708 b2263c45 2022-01-16 op for (off = 0; off < r; off += nw)
709 b2263c45 2022-01-16 op if ((nw = write(fd, buf + off, r - off)) == 0 ||
711 fb1a36c0 2022-01-09 op err(1, "write");
714 9e7dd230 2022-01-12 op draw_progress(name, &p);
717 fb1a36c0 2022-01-09 op /* throttle, for debugging purpose */
719 fb1a36c0 2022-01-09 op struct timespec ts = { 0, 500000000 };
720 fb1a36c0 2022-01-09 op nanosleep(&ts, NULL);
725 fb1a36c0 2022-01-09 op putchar('\n');
727 01fb39ab 2022-01-16 op do_clunk(fid);
731 16f1f3bf 2022-01-21 op send_fid(int fid, const char *fnam, int open_flags, int fd, const char *name)
733 01fb39ab 2022-01-16 op struct progress p = {0};
734 01fb39ab 2022-01-16 op struct stat sb;
737 01fb39ab 2022-01-16 op char buf[BUFSIZ];
739 01fb39ab 2022-01-16 op if (fstat(fd, &sb) == -1)
740 01fb39ab 2022-01-16 op err(1, "fstat");
742 01fb39ab 2022-01-16 op if (fnam != NULL)
743 01fb39ab 2022-01-16 op do_create(fid, fnam, 0644, KOWRITE);
745 16f1f3bf 2022-01-21 op do_open(fid, open_flags | KOWRITE);
747 01fb39ab 2022-01-16 op p.max = sb.st_size;
749 01fb39ab 2022-01-16 op r = read(fd, buf, sizeof(buf));
753 01fb39ab 2022-01-16 op err(1, "read");
755 01fb39ab 2022-01-16 op w = do_write(fid, p.done, r, buf);
758 01fb39ab 2022-01-16 op draw_progress(name, &p);
761 01fb39ab 2022-01-16 op /* throttle, for debugging purpose */
763 01fb39ab 2022-01-16 op struct timespec ts = { 0, 500000000 };
764 01fb39ab 2022-01-16 op nanosleep(&ts, NULL);
769 01fb39ab 2022-01-16 op putchar('\n');
770 fb1a36c0 2022-01-09 op do_clunk(fid);
774 9c48f915 2022-01-16 op woc_file(int fd, const char *prompt, const char *path)
776 9c48f915 2022-01-16 op struct qid qid;
777 9c48f915 2022-01-16 op const char *n = NULL;
778 9c48f915 2022-01-16 op char *errstr;
779 9c48f915 2022-01-16 op int nfid, miss;
781 8d8fb849 2022-01-17 op nfid = nextfid();
782 9c48f915 2022-01-16 op errstr = walk_path(pwdfid, nfid, path, &miss, &qid);
783 9c48f915 2022-01-16 op if (errstr != NULL && miss > 1) {
784 9c48f915 2022-01-16 op printf("%s: %s\n", path, errstr);
785 9c48f915 2022-01-16 op free(errstr);
789 9c48f915 2022-01-16 op if (errstr != NULL || miss == 1) {
790 9c48f915 2022-01-16 op char p[PATH_MAX], *dn;
793 9c48f915 2022-01-16 op * If it's only one component missing (the file name), walk
794 9c48f915 2022-01-16 op * to the parent directory and try to create the file.
797 9c48f915 2022-01-16 op if (strlcpy(p, path, sizeof(p)) >= sizeof(p)) {
798 9c48f915 2022-01-16 op printf("path too long: %s\n", path);
801 9c48f915 2022-01-16 op dn = dirname(p);
803 9c48f915 2022-01-16 op if (!strcmp(dn, ".")) {
804 9c48f915 2022-01-16 op errstr = dup_fid(pwdfid, nfid);
807 9c48f915 2022-01-16 op errstr = walk_path(pwdfid, nfid, dn, &miss, &qid);
809 9c48f915 2022-01-16 op if (errstr != NULL) {
810 9c48f915 2022-01-16 op printf("%s: %s\n", dn, errstr);
811 9c48f915 2022-01-16 op free(errstr);
815 9c48f915 2022-01-16 op if (miss != 0) {
816 9c48f915 2022-01-16 op printf("%s: not a directory\n", dn);
820 9c48f915 2022-01-16 op if ((n = strrchr(path, '/')) != NULL)
826 9c48f915 2022-01-16 op free(errstr);
828 9c48f915 2022-01-16 op if (miss > 1) {
829 9c48f915 2022-01-16 op printf("can't create %s: missing %d path component(s)\n",
834 16f1f3bf 2022-01-21 op send_fid(nfid, n, KOTRUNC, fd, prompt);
839 fb1a36c0 2022-01-09 op do_tls_connect(const char *host, const char *port)
841 fb1a36c0 2022-01-09 op int handshake;
843 fb1a36c0 2022-01-09 op if ((tlsconf = tls_config_new()) == NULL)
844 fb1a36c0 2022-01-09 op fatalx("tls_config_new");
845 fb1a36c0 2022-01-09 op tls_config_insecure_noverifycert(tlsconf);
846 fb1a36c0 2022-01-09 op tls_config_insecure_noverifyname(tlsconf);
847 fb1a36c0 2022-01-09 op if (tls_config_set_keypair_file(tlsconf, crtpath, keypath) == -1)
848 fb1a36c0 2022-01-09 op fatalx("can't load certs (%s, %s)", crtpath, keypath);
850 fb1a36c0 2022-01-09 op if ((ctx = tls_client()) == NULL)
851 fb1a36c0 2022-01-09 op fatal("tls_client");
852 fb1a36c0 2022-01-09 op if (tls_configure(ctx, tlsconf) == -1)
853 fb1a36c0 2022-01-09 op fatalx("tls_configure: %s", tls_error(ctx));
855 fb1a36c0 2022-01-09 op if (tls_connect(ctx, host, port) == -1)
856 fb1a36c0 2022-01-09 op fatalx("can't connect to %s:%s: %s", host, port,
857 fb1a36c0 2022-01-09 op tls_error(ctx));
859 fb1a36c0 2022-01-09 op for (handshake = 0; !handshake;) {
860 fb1a36c0 2022-01-09 op switch (tls_handshake(ctx)) {
862 fb1a36c0 2022-01-09 op fatalx("tls_handshake: %s", tls_error(ctx));
864 fb1a36c0 2022-01-09 op handshake = 1;
871 fb1a36c0 2022-01-09 op do_ctxt_connect(const char *host, const char *port)
873 fb1a36c0 2022-01-09 op struct addrinfo hints, *res, *res0;
874 fb1a36c0 2022-01-09 op int error, saved_errno;
875 fb1a36c0 2022-01-09 op const char *cause = NULL;
877 fb1a36c0 2022-01-09 op memset(&hints, 0, sizeof(hints));
878 fb1a36c0 2022-01-09 op hints.ai_family = AF_UNSPEC;
879 fb1a36c0 2022-01-09 op hints.ai_socktype = SOCK_STREAM;
880 fb1a36c0 2022-01-09 op error = getaddrinfo(host, port, &hints, &res0);
882 fb1a36c0 2022-01-09 op errx(1, "%s", gai_strerror(error));
885 fb1a36c0 2022-01-09 op for (res = res0; res != NULL; res = res->ai_next) {
886 db066f1e 2022-01-12 op sock = socket(res->ai_family, res->ai_socktype|SOCK_CLOEXEC,
887 fb1a36c0 2022-01-09 op res->ai_protocol);
888 fb1a36c0 2022-01-09 op if (sock == -1) {
889 fb1a36c0 2022-01-09 op cause = "socket";
893 fb1a36c0 2022-01-09 op if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
894 fb1a36c0 2022-01-09 op cause = "connect";
895 fb1a36c0 2022-01-09 op saved_errno = errno;
897 fb1a36c0 2022-01-09 op errno = saved_errno;
905 fb1a36c0 2022-01-09 op if (sock == -1)
906 fb1a36c0 2022-01-09 op err(1, "%s", cause);
907 fb1a36c0 2022-01-09 op freeaddrinfo(res0);
911 fb1a36c0 2022-01-09 op do_connect(const char *connspec, const char *path)
913 fb1a36c0 2022-01-09 op char *host, *colon;
914 fb1a36c0 2022-01-09 op const char *port;
916 fb1a36c0 2022-01-09 op host = xstrdup(connspec);
917 fb1a36c0 2022-01-09 op if ((colon = strchr(host, ':')) != NULL) {
918 fb1a36c0 2022-01-09 op *colon = '\0';
919 fb1a36c0 2022-01-09 op port = ++colon;
921 fb1a36c0 2022-01-09 op port = "1337";
923 fb1a36c0 2022-01-09 op printf("connecting to %s:%s...", host, port);
924 fb1a36c0 2022-01-09 op fflush(stdout);
927 fb1a36c0 2022-01-09 op do_tls_connect(host, port);
929 fb1a36c0 2022-01-09 op do_ctxt_connect(host, port);
931 fb1a36c0 2022-01-09 op printf(" done!\n");
933 fb1a36c0 2022-01-09 op do_version();
934 fb1a36c0 2022-01-09 op do_attach(path);
940 64d8db89 2022-01-16 op tmp_file(char sfn[TMPFSTRLEN])
944 64d8db89 2022-01-16 op strlcpy(sfn, TMPFSTR, TMPFSTRLEN);
945 64d8db89 2022-01-16 op if ((tmpfd = mkstemp(sfn)) == -1) {
946 64d8db89 2022-01-16 op warn("mkstemp %s", sfn);
950 aa76fd7f 2022-01-16 op /* set the close-on-exec flag */
951 aa76fd7f 2022-01-16 op if (fcntl(tmpfd, F_SETFD, FD_CLOEXEC) == -1) {
952 aa76fd7f 2022-01-16 op warn("fcntl");
953 aa76fd7f 2022-01-16 op close(tmpfd);
957 64d8db89 2022-01-16 op return tmpfd;
960 1f5fb843 2022-01-16 op static inline const char *
961 1f5fb843 2022-01-16 op pp_perm(uint8_t x)
963 1f5fb843 2022-01-16 op switch (x & 0x7) {
965 1f5fb843 2022-01-16 op return "---";
967 1f5fb843 2022-01-16 op return "--x";
969 1f5fb843 2022-01-16 op return "-w-";
971 1f5fb843 2022-01-16 op return "-wx";
973 1f5fb843 2022-01-16 op return "r--";
975 1f5fb843 2022-01-16 op return "r-x";
977 1f5fb843 2022-01-16 op return "rw-";
979 1f5fb843 2022-01-16 op return "rwx";
981 1f5fb843 2022-01-16 op /* unreachable, just for the compiler' happiness */
982 1f5fb843 2022-01-16 op return "???";
986 c37e1cfe 2022-01-17 op static inline void
987 c37e1cfe 2022-01-17 op prepare_wstat(struct np_stat *st)
989 c37e1cfe 2022-01-17 op memset(st, 0xFF, sizeof(*st));
990 c37e1cfe 2022-01-17 op st->name = NULL;
991 c37e1cfe 2022-01-17 op st->uid = NULL;
992 c37e1cfe 2022-01-17 op st->gid = NULL;
993 c37e1cfe 2022-01-17 op st->muid = NULL;
997 fb1a36c0 2022-01-09 op cmd_bell(int argc, const char **argv)
999 fb1a36c0 2022-01-09 op if (argc == 0) {
1000 fb1a36c0 2022-01-09 op bell = !bell;
1002 fb1a36c0 2022-01-09 op puts("bell mode enabled");
1004 fb1a36c0 2022-01-09 op puts("bell mode disabled");
1008 fb1a36c0 2022-01-09 op if (argc != 1)
1011 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "on")) {
1013 fb1a36c0 2022-01-09 op puts("bell mode enabled");
1017 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "off")) {
1019 fb1a36c0 2022-01-09 op puts("bell mode disabled");
1024 fb1a36c0 2022-01-09 op printf("bell [on | off]\n");
1028 fb1a36c0 2022-01-09 op cmd_bye(int argc, const char **argv)
1030 fb1a36c0 2022-01-09 op log_warnx("bye\n");
1035 fb1a36c0 2022-01-09 op cmd_cd(int argc, const char **argv)
1037 fb1a36c0 2022-01-09 op struct qid qid;
1038 8f9f99f7 2022-01-16 op int nfid, miss;
1039 8f9f99f7 2022-01-16 op char *errstr;
1041 fb1a36c0 2022-01-09 op if (argc != 1) {
1042 fb1a36c0 2022-01-09 op printf("usage: cd remote-path\n");
1046 8d8fb849 2022-01-17 op nfid = nextfid();
1047 8f9f99f7 2022-01-16 op errstr = walk_path(pwdfid, nfid, argv[0], &miss, &qid);
1048 8f9f99f7 2022-01-16 op if (errstr != NULL) {
1049 8f9f99f7 2022-01-16 op printf("%s: %s\n", argv[0], errstr);
1050 8f9f99f7 2022-01-16 op free(errstr);
1054 8f9f99f7 2022-01-16 op if (miss != 0 || !(qid.type & QTDIR)) {
1055 8f9f99f7 2022-01-16 op printf("%s: not a directory\n", argv[0]);
1056 fcd9d510 2022-01-16 op if (miss == 0)
1057 fcd9d510 2022-01-16 op do_clunk(nfid);
1061 8f9f99f7 2022-01-16 op do_clunk(pwdfid);
1062 8f9f99f7 2022-01-16 op pwdfid = nfid;
1066 9c48f915 2022-01-16 op cmd_edit(int argc, const char **argv)
1068 9c48f915 2022-01-16 op struct qid qid;
1069 9c48f915 2022-01-16 op int nfid, tmpfd, miss;
1070 9c48f915 2022-01-16 op char sfn[TMPFSTRLEN], p[PATH_MAX], *name, *errstr;
1071 9c48f915 2022-01-16 op const char *ed;
1073 9c48f915 2022-01-16 op if (argc != 1) {
1074 9c48f915 2022-01-16 op puts("usage: edit file");
1078 9c48f915 2022-01-16 op if ((ed = getenv("VISUAL")) == NULL &&
1079 9c48f915 2022-01-16 op (ed = getenv("EDITOR")) == NULL)
1082 8d8fb849 2022-01-17 op nfid = nextfid();
1083 9c48f915 2022-01-16 op errstr = walk_path(pwdfid, nfid, *argv, &miss, &qid);
1084 9c48f915 2022-01-16 op if (errstr != NULL) {
1085 9c48f915 2022-01-16 op printf("%s: %s\n", *argv, errstr);
1086 9c48f915 2022-01-16 op free(errstr);
1090 9c48f915 2022-01-16 op if (miss != 0 || qid.type != 0) {
1091 9c48f915 2022-01-16 op printf("%s: not a file\n", *argv);
1092 9c48f915 2022-01-16 op if (miss == 0)
1093 9c48f915 2022-01-16 op do_clunk(nfid);
1097 9c48f915 2022-01-16 op if ((tmpfd = tmp_file(sfn)) == -1) {
1098 9c48f915 2022-01-16 op do_clunk(nfid);
1102 9c48f915 2022-01-16 op strlcpy(p, *argv, sizeof(p));
1103 9c48f915 2022-01-16 op name = basename(p);
1105 9c48f915 2022-01-16 op fetch_fid(nfid, tmpfd, name);
1106 9c48f915 2022-01-16 op close(tmpfd);
1108 9c48f915 2022-01-16 op spawn(ed, sfn, NULL);
1111 9c48f915 2022-01-16 op * Re-open the file because it's not guaranteed that the
1112 9c48f915 2022-01-16 op * file descriptor tmpfd is still associated with the file
1113 9c48f915 2022-01-16 op * pointed by sfn: it's not uncommon for editor to write
1114 9c48f915 2022-01-16 op * a backup file and then rename(2) it to the file name.
1116 9c48f915 2022-01-16 op if ((tmpfd = open(sfn, O_RDONLY)) == -1) {
1117 9c48f915 2022-01-16 op warn("can't open %s", sfn);
1121 9c48f915 2022-01-16 op woc_file(tmpfd, *argv, name);
1122 9c48f915 2022-01-16 op close(tmpfd);
1125 9c48f915 2022-01-16 op unlink(sfn);
1129 fb1a36c0 2022-01-09 op cmd_get(int argc, const char **argv)
1131 fb1a36c0 2022-01-09 op struct qid qid;
1132 fb1a36c0 2022-01-09 op const char *l;
1133 8f9f99f7 2022-01-16 op char *errstr;
1134 8f9f99f7 2022-01-16 op int nfid, fd, miss;
1136 fb1a36c0 2022-01-09 op if (argc != 1 && argc != 2) {
1137 fb1a36c0 2022-01-09 op printf("usage: get remote-file [local-file]\n");
1141 fb1a36c0 2022-01-09 op if (argc == 2)
1142 fb1a36c0 2022-01-09 op l = argv[1];
1143 fb1a36c0 2022-01-09 op else if ((l = strrchr(argv[0], '/')) != NULL)
1144 fb1a36c0 2022-01-09 op l++; /* skip / */
1146 2ba25711 2022-01-16 op l = argv[0];
1148 8d8fb849 2022-01-17 op nfid = nextfid();
1149 8f9f99f7 2022-01-16 op errstr = walk_path(pwdfid, nfid, argv[0], &miss, &qid);
1150 8f9f99f7 2022-01-16 op if (errstr != NULL) {
1151 8f9f99f7 2022-01-16 op printf("%s: %s\n", argv[0], errstr);
1152 8f9f99f7 2022-01-16 op free(errstr);
1156 8f9f99f7 2022-01-16 op if (miss != 0 || qid.type != 0) {
1157 8f9f99f7 2022-01-16 op printf("%s: not a file\n", argv[0]);
1158 fcd9d510 2022-01-16 op if (miss == 0)
1159 fcd9d510 2022-01-16 op do_clunk(nfid);
1163 76521d26 2022-01-16 op if ((fd = open(l, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0644)) == -1) {
1164 76521d26 2022-01-16 op warn("can't open %s", l);
1165 76521d26 2022-01-16 op do_clunk(nfid);
1169 76521d26 2022-01-16 op fetch_fid(nfid, fd, l);
1174 5565d021 2022-01-16 op cmd_hexdump(int argc, const char **argv)
1176 5565d021 2022-01-16 op if (argc == 0) {
1177 5565d021 2022-01-16 op xdump = !xdump;
1179 5565d021 2022-01-16 op puts("hexdump mode enabled");
1181 5565d021 2022-01-16 op puts("hexdump mode disabled");
1185 5565d021 2022-01-16 op if (argc > 1)
1188 5565d021 2022-01-16 op if (!strcmp(*argv, "on")) {
1190 5565d021 2022-01-16 op puts("hexdump mode enabled");
1194 5565d021 2022-01-16 op if (!strcmp(*argv, "off")) {
1196 5565d021 2022-01-16 op puts("hexdump mode disabled");
1201 5565d021 2022-01-16 op puts("usage: hexdump [on | off]");
1205 fb1a36c0 2022-01-09 op cmd_lcd(int argc, const char **argv)
1207 fb1a36c0 2022-01-09 op const char *dir;
1209 fb1a36c0 2022-01-09 op if (argc > 1) {
1210 d166ffc0 2022-01-16 op printf("usage: lcd [local-directory]\n");
1214 fb1a36c0 2022-01-09 op if (argc == 1)
1215 fb1a36c0 2022-01-09 op dir = *argv;
1217 fb1a36c0 2022-01-09 op if (argc == 0 && (dir = getenv("HOME")) == NULL) {
1218 fb1a36c0 2022-01-09 op printf("HOME is not defined\n");
1222 fb1a36c0 2022-01-09 op if (chdir(dir) == -1)
1223 fb1a36c0 2022-01-09 op printf("cd: %s: %s\n", dir, strerror(errno));
1227 fb1a36c0 2022-01-09 op cmd_lpwd(int argc, const char **argv)
1229 fb1a36c0 2022-01-09 op char path[PATH_MAX];
1231 d166ffc0 2022-01-16 op if (argc != 0) {
1232 d166ffc0 2022-01-16 op printf("usage: lpwd\n");
1236 fb1a36c0 2022-01-09 op if (getcwd(path, sizeof(path)) == NULL) {
1237 fb1a36c0 2022-01-09 op printf("lpwd: %s\n", strerror(errno));
1241 fb1a36c0 2022-01-09 op printf("%s\n", path);
1245 fb1a36c0 2022-01-09 op cmd_ls(int argc, const char **argv)
1247 fb1a36c0 2022-01-09 op struct np_stat st;
1248 4f52d9af 2022-01-16 op time_t now, mtime;
1249 4f52d9af 2022-01-16 op struct tm *tm;
1250 fb1a36c0 2022-01-09 op uint64_t off = 0;
1251 fb1a36c0 2022-01-09 op uint32_t len;
1253 4f52d9af 2022-01-16 op const char *timfmt;
1254 4f52d9af 2022-01-16 op char fmt[FMT_SCALED_STRSIZE], tim[13], *errstr;
1256 fb1a36c0 2022-01-09 op if (argc != 0) {
1257 fb1a36c0 2022-01-09 op printf("ls don't take arguments (yet)\n");
1261 4f52d9af 2022-01-16 op now = time(NULL);
1263 a9288ad9 2022-01-17 op nfid = nextfid();
1264 a9288ad9 2022-01-17 op if ((errstr = dup_fid(pwdfid, nfid)) != NULL) {
1265 1bb1bfca 2022-01-16 op printf(".: %s\n", errstr);
1266 1bb1bfca 2022-01-16 op free(errstr);
1270 a9288ad9 2022-01-17 op do_open(nfid, KOREAD);
1272 fb1a36c0 2022-01-09 op evbuffer_drain(dirbuf, EVBUFFER_LENGTH(dirbuf));
1275 a9288ad9 2022-01-17 op tread(nfid, off, BUFSIZ);
1278 fb1a36c0 2022-01-09 op expect2(Rread, iota_tag);
1280 fb1a36c0 2022-01-09 op len = np_read32(buf);
1281 fb1a36c0 2022-01-09 op if (len == 0)
1284 fb1a36c0 2022-01-09 op evbuffer_add_buffer(dirbuf, buf);
1287 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
1290 fb1a36c0 2022-01-09 op while (EVBUFFER_LENGTH(dirbuf) != 0) {
1291 fb1a36c0 2022-01-09 op if (np_read_stat(dirbuf, &st) == -1)
1292 fb1a36c0 2022-01-09 op errx(1, "invalid stat struct read");
1294 fb1a36c0 2022-01-09 op if (fmt_scaled(st.length, fmt) == -1)
1295 fb1a36c0 2022-01-09 op strlcpy(fmt, "xxx", sizeof(fmt));
1297 4f52d9af 2022-01-16 op mtime = st.mtime;
1299 4f52d9af 2022-01-16 op if (now > mtime && (now - mtime) < 365/2 * 24 * 12 * 60)
1300 4f52d9af 2022-01-16 op timfmt = "%b %e %R";
1302 4f52d9af 2022-01-16 op timfmt = "%b %e %Y";
1304 4f52d9af 2022-01-16 op if ((tm = localtime(&mtime)) == NULL ||
1305 4f52d9af 2022-01-16 op strftime(tim, sizeof(tim), timfmt, tm) == 0)
1306 4f52d9af 2022-01-16 op strlcpy(tim, "unknown", sizeof(tim));
1308 1f5fb843 2022-01-16 op if (st.qid.type & QTDIR)
1309 1f5fb843 2022-01-16 op printf("d");
1311 1f5fb843 2022-01-16 op printf("-");
1312 1f5fb843 2022-01-16 op printf("%s", pp_perm(st.mode >> 6));
1313 1f5fb843 2022-01-16 op printf("%s", pp_perm(st.mode >> 3));
1314 1f5fb843 2022-01-16 op printf("%s", pp_perm(st.mode));
1315 d33c93c4 2022-01-17 op printf(" %8s %12s %s%s\n", fmt, tim, st.name,
1316 6470594e 2022-01-16 op st.qid.type & QTDIR ? "/" : "");
1318 fb1a36c0 2022-01-09 op free(st.name);
1319 fb1a36c0 2022-01-09 op free(st.uid);
1320 fb1a36c0 2022-01-09 op free(st.gid);
1321 fb1a36c0 2022-01-09 op free(st.muid);
1324 a9288ad9 2022-01-17 op do_clunk(nfid);
1328 9e7dd230 2022-01-12 op cmd_page(int argc, const char **argv)
1330 9e7dd230 2022-01-12 op struct qid qid;
1331 8f9f99f7 2022-01-16 op int nfid, tmpfd, miss;
1332 64d8db89 2022-01-16 op char sfn[TMPFSTRLEN], p[PATH_MAX], *name, *errstr;
1333 62f663bb 2022-01-16 op const char *pager;
1335 9e7dd230 2022-01-12 op if (argc != 1) {
1336 9e7dd230 2022-01-12 op puts("usage: page file");
1340 62f663bb 2022-01-16 op if ((pager = getenv("PAGER")) == NULL)
1341 62f663bb 2022-01-16 op pager = "less";
1343 8d8fb849 2022-01-17 op nfid = nextfid();
1344 8f9f99f7 2022-01-16 op errstr = walk_path(pwdfid, nfid, *argv, &miss, &qid);
1345 8f9f99f7 2022-01-16 op if (errstr != NULL) {
1346 8f9f99f7 2022-01-16 op printf("%s: %s\n", *argv, errstr);
1347 8f9f99f7 2022-01-16 op free(errstr);
1351 8f9f99f7 2022-01-16 op if (miss != 0 || qid.type != 0) {
1352 8f9f99f7 2022-01-16 op printf("%s: not a file\n", *argv);
1353 fcd9d510 2022-01-16 op if (miss == 0)
1354 fcd9d510 2022-01-16 op do_clunk(nfid);
1358 64d8db89 2022-01-16 op if ((tmpfd = tmp_file(sfn)) == -1) {
1359 9e7dd230 2022-01-12 op do_clunk(nfid);
1363 9e7dd230 2022-01-12 op strlcpy(p, *argv, sizeof(p));
1364 9e7dd230 2022-01-12 op name = basename(p);
1365 76521d26 2022-01-16 op fetch_fid(nfid, tmpfd, name);
1366 9e7dd230 2022-01-12 op close(tmpfd);
1367 62f663bb 2022-01-16 op spawn(pager, sfn, NULL);
1368 9e7dd230 2022-01-12 op unlink(sfn);
1372 01fb39ab 2022-01-16 op cmd_put(int argc, const char **argv)
1374 01fb39ab 2022-01-16 op struct qid qid;
1375 9c48f915 2022-01-16 op const char *l;
1378 01fb39ab 2022-01-16 op if (argc != 1 && argc != 2) {
1379 01fb39ab 2022-01-16 op printf("usage: put local-file [remote-file]\n");
1383 01fb39ab 2022-01-16 op if (argc == 2)
1384 01fb39ab 2022-01-16 op l = argv[1];
1385 01fb39ab 2022-01-16 op else if ((l = strrchr(argv[0], '/')) != NULL)
1386 01fb39ab 2022-01-16 op l++; /* skip / */
1388 01fb39ab 2022-01-16 op l = argv[0];
1390 01fb39ab 2022-01-16 op if ((fd = open(argv[0], O_RDONLY)) == -1) {
1391 01fb39ab 2022-01-16 op warn("%s", argv[0]);
1395 9c48f915 2022-01-16 op woc_file(fd, argv[0], l);
1400 c37e1cfe 2022-01-17 op cmd_rename(int argc, const char **argv)
1402 c37e1cfe 2022-01-17 op struct np_stat st;
1403 c37e1cfe 2022-01-17 op struct qid qid;
1404 c37e1cfe 2022-01-17 op char *errstr;
1405 c37e1cfe 2022-01-17 op int nfid, miss;
1407 c37e1cfe 2022-01-17 op if (argc != 2) {
1408 c37e1cfe 2022-01-17 op puts("usage: rename remote-file new-remote-name");
1412 8d8fb849 2022-01-17 op nfid = nextfid();
1413 c37e1cfe 2022-01-17 op errstr = walk_path(pwdfid, nfid, argv[0], &miss, &qid);
1414 c37e1cfe 2022-01-17 op if (errstr != NULL) {
1415 c37e1cfe 2022-01-17 op printf("%s: %s\n", argv[0], errstr);
1416 c37e1cfe 2022-01-17 op free(errstr);
1420 c37e1cfe 2022-01-17 op if (miss != 0) {
1421 c37e1cfe 2022-01-17 op printf("%s: not such file or directory\n", argv[0]);
1425 c37e1cfe 2022-01-17 op prepare_wstat(&st);
1426 c37e1cfe 2022-01-17 op st.name = (char *)argv[1];
1427 c37e1cfe 2022-01-17 op if ((errstr = do_wstat(nfid, &st)) != NULL) {
1428 c37e1cfe 2022-01-17 op printf("rename: %s\n", errstr);
1429 c37e1cfe 2022-01-17 op free(errstr);
1432 c37e1cfe 2022-01-17 op do_clunk(nfid);
1436 fb1a36c0 2022-01-09 op cmd_verbose(int argc, const char **argv)
1438 fb1a36c0 2022-01-09 op if (argc == 0) {
1439 fb1a36c0 2022-01-09 op log_setverbose(!log_getverbose());
1440 fb1a36c0 2022-01-09 op if (log_getverbose())
1441 fb1a36c0 2022-01-09 op puts("verbose mode enabled");
1443 fb1a36c0 2022-01-09 op puts("verbose mode disabled");
1447 fb1a36c0 2022-01-09 op if (argc != 1)
1450 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "on")) {
1451 fb1a36c0 2022-01-09 op log_setverbose(1);
1452 fb1a36c0 2022-01-09 op puts("verbose mode enabled");
1456 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "off")) {
1457 fb1a36c0 2022-01-09 op log_setverbose(0);
1458 fb1a36c0 2022-01-09 op puts("verbose mode disabled");
1463 fb1a36c0 2022-01-09 op printf("verbose [on | off]\n");
1467 fb1a36c0 2022-01-09 op excmd(int argc, const char **argv)
1469 fb1a36c0 2022-01-09 op struct cmd {
1470 fb1a36c0 2022-01-09 op const char *name;
1471 fb1a36c0 2022-01-09 op void (*fn)(int, const char **);
1472 fb1a36c0 2022-01-09 op } cmds[] = {
1473 fb1a36c0 2022-01-09 op {"bell", cmd_bell},
1474 fb1a36c0 2022-01-09 op {"bye", cmd_bye},
1475 fb1a36c0 2022-01-09 op {"cd", cmd_cd},
1476 9c48f915 2022-01-16 op {"edit", cmd_edit},
1477 fb1a36c0 2022-01-09 op {"get", cmd_get},
1478 5565d021 2022-01-16 op {"hexdump", cmd_hexdump},
1479 fb1a36c0 2022-01-09 op {"lcd", cmd_lcd},
1480 fb1a36c0 2022-01-09 op {"lpwd", cmd_lpwd},
1481 fb1a36c0 2022-01-09 op {"ls", cmd_ls},
1482 9e7dd230 2022-01-12 op {"page", cmd_page},
1483 01fb39ab 2022-01-16 op {"put", cmd_put},
1484 fb1a36c0 2022-01-09 op {"quit", cmd_bye},
1485 c37e1cfe 2022-01-17 op {"rename", cmd_rename},
1486 fb1a36c0 2022-01-09 op {"verbose", cmd_verbose},
1490 fb1a36c0 2022-01-09 op if (argc == 0)
1492 fb1a36c0 2022-01-09 op for (i = 0; i < nitems(cmds); ++i) {
1493 fb1a36c0 2022-01-09 op if (!strcmp(cmds[i].name, *argv)) {
1494 fb1a36c0 2022-01-09 op cmds[i].fn(argc-1, argv+1);
1499 fb1a36c0 2022-01-09 op log_warnx("unknown command %s", *argv);
1503 fb1a36c0 2022-01-09 op main(int argc, char **argv)
1507 fb1a36c0 2022-01-09 op log_init(1, LOG_DAEMON);
1508 fb1a36c0 2022-01-09 op log_setverbose(0);
1509 fb1a36c0 2022-01-09 op log_procinit(getprogname());
1511 fb1a36c0 2022-01-09 op while ((ch = getopt(argc, argv, "C:cK:")) != -1) {
1512 fb1a36c0 2022-01-09 op switch (ch) {
1514 fb1a36c0 2022-01-09 op crtpath = optarg;
1520 fb1a36c0 2022-01-09 op keypath = optarg;
1526 fb1a36c0 2022-01-09 op argc -= optind;
1527 fb1a36c0 2022-01-09 op argv += optind;
1529 fb1a36c0 2022-01-09 op if (argc == 0)
1532 fb1a36c0 2022-01-09 op if (isatty(1)) {
1534 fb1a36c0 2022-01-09 op resized = 1;
1535 fb1a36c0 2022-01-09 op signal(SIGWINCH, tty_resized);
1538 fb1a36c0 2022-01-09 op if ((evb = evbuffer_new()) == NULL)
1539 fb1a36c0 2022-01-09 op fatal("evbuffer_new");
1541 fb1a36c0 2022-01-09 op if ((buf = evbuffer_new()) == NULL)
1542 fb1a36c0 2022-01-09 op fatal("evbuffer_new");
1544 fb1a36c0 2022-01-09 op if ((dirbuf = evbuffer_new()) == NULL)
1545 fb1a36c0 2022-01-09 op fatal("evbuferr_new");
1547 fb1a36c0 2022-01-09 op do_connect(argv[0], argv[1]);
1550 fb1a36c0 2022-01-09 op int argc = 0;
1551 fb1a36c0 2022-01-09 op char *line, *argv[16] = {0}, **ap;
1553 fb1a36c0 2022-01-09 op if ((line = read_line("kamiftp> ")) == NULL)
1556 fb1a36c0 2022-01-09 op for (argc = 0, ap = argv; ap < &argv[15] &&
1557 fb1a36c0 2022-01-09 op (*ap = strsep(&line, " \t")) != NULL;) {
1558 fb1a36c0 2022-01-09 op if (**ap != '\0')
1559 fb1a36c0 2022-01-09 op ap++, argc++;
1561 fb1a36c0 2022-01-09 op excmd(argc, (const char **)argv);
1564 fb1a36c0 2022-01-09 op printf("\a");
1565 fb1a36c0 2022-01-09 op fflush(stdout);
1571 fb1a36c0 2022-01-09 op printf("\n");