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;
67 fb1a36c0 2022-01-09 op struct tls_config *tlsconf;
68 fb1a36c0 2022-01-09 op struct tls *ctx;
70 fb1a36c0 2022-01-09 op struct evbuffer *buf;
71 fb1a36c0 2022-01-09 op struct evbuffer *dirbuf;
72 fb1a36c0 2022-01-09 op uint32_t msize;
75 fb1a36c0 2022-01-09 op volatile sig_atomic_t resized;
77 fb1a36c0 2022-01-09 op int tty_width;
80 fb1a36c0 2022-01-09 op struct progress {
82 fb1a36c0 2022-01-09 op uint64_t done;
87 fb1a36c0 2022-01-09 op #define ASSERT_EMPTYBUF() assert(EVBUFFER_LENGTH(buf) == 0)
89 91cfa92a 2022-01-10 op #if !HAVE_READLINE
91 91cfa92a 2022-01-10 op readline(const char *prompt)
93 91cfa92a 2022-01-10 op char *ch, *line = NULL;
94 91cfa92a 2022-01-10 op size_t linesize = 0;
95 91cfa92a 2022-01-10 op ssize_t linelen;
97 91cfa92a 2022-01-10 op printf("%s", prompt);
98 91cfa92a 2022-01-10 op fflush(stdout);
100 91cfa92a 2022-01-10 op linelen = getline(&line, &linesize, stdin);
101 91cfa92a 2022-01-10 op if (linelen == -1)
104 91cfa92a 2022-01-10 op if ((ch = strchr(line, '\n')) != NULL)
110 91cfa92a 2022-01-10 op add_history(const char *line)
116 fb1a36c0 2022-01-09 op static char *
117 fb1a36c0 2022-01-09 op read_line(const char *prompt)
122 fb1a36c0 2022-01-09 op if ((line = readline(prompt)) == NULL)
124 fb1a36c0 2022-01-09 op /* XXX: trim spaces? */
125 fb1a36c0 2022-01-09 op if (*line == '\0') {
130 fb1a36c0 2022-01-09 op add_history(line);
135 9e7dd230 2022-01-12 op spawn(const char *argv0, ...)
140 9e7dd230 2022-01-12 op const char *argv[16], *last;
143 9e7dd230 2022-01-12 op memset(argv, 0, sizeof(argv));
145 9e7dd230 2022-01-12 op va_start(ap, argv0);
146 9e7dd230 2022-01-12 op argv[0] = argv0;
147 9e7dd230 2022-01-12 op for (i = 1; i < nitems(argv); ++i) {
148 9e7dd230 2022-01-12 op last = va_arg(ap, const char *);
149 9e7dd230 2022-01-12 op if (last == NULL)
151 9e7dd230 2022-01-12 op argv[i] = last;
155 9e7dd230 2022-01-12 op assert(last == NULL);
157 9e7dd230 2022-01-12 op switch (pid = fork()) {
159 9e7dd230 2022-01-12 op err(1, "fork");
160 9e7dd230 2022-01-12 op case 0: /* child */
161 9e7dd230 2022-01-12 op execvp(argv[0], (char *const *)argv);
162 9e7dd230 2022-01-12 op err(1, "execvp");
164 9e7dd230 2022-01-12 op waitpid(pid, &status, 0);
169 fb1a36c0 2022-01-09 op tty_resized(int signo)
174 fb1a36c0 2022-01-09 op static void __dead
175 fb1a36c0 2022-01-09 op usage(int ret)
177 dc8c1791 2022-01-10 op fprintf(stderr, "usage: %s [-c] [-C cert] [-K key] "
178 dc8c1791 2022-01-10 op "host[:port] [path]\n", getprogname());
179 fb1a36c0 2022-01-09 op fprintf(stderr, "kamid suite version " KAMID_VERSION "\n");
184 8d8fb849 2022-01-17 op nextfid(void)
188 8d8fb849 2022-01-17 op for (i = 0; ; ++i) {
189 8d8fb849 2022-01-17 op if (i != pwdfid)
195 fb1a36c0 2022-01-09 op do_send(void)
197 fb1a36c0 2022-01-09 op const void *buf;
198 fb1a36c0 2022-01-09 op size_t nbytes;
202 d33c93c4 2022-01-17 op hexdump("outgoing message", EVBUFFER_DATA(evb),
203 5565d021 2022-01-16 op EVBUFFER_LENGTH(evb));
205 fb1a36c0 2022-01-09 op while (EVBUFFER_LENGTH(evb) != 0) {
206 fb1a36c0 2022-01-09 op buf = EVBUFFER_DATA(evb);
207 fb1a36c0 2022-01-09 op nbytes = EVBUFFER_LENGTH(evb);
209 fb1a36c0 2022-01-09 op if (ctx == NULL) {
210 fb1a36c0 2022-01-09 op r = write(sock, buf, nbytes);
211 fb1a36c0 2022-01-09 op if (r == 0 || r == -1)
212 fb1a36c0 2022-01-09 op errx(1, "EOF");
214 fb1a36c0 2022-01-09 op r = tls_write(ctx, buf, nbytes);
215 fb1a36c0 2022-01-09 op if (r == TLS_WANT_POLLIN || r == TLS_WANT_POLLOUT)
218 fb1a36c0 2022-01-09 op errx(1, "tls: %s", tls_error(ctx));
221 fb1a36c0 2022-01-09 op evbuffer_drain(evb, r);
226 fb1a36c0 2022-01-09 op mustread(void *d, size_t len)
230 fb1a36c0 2022-01-09 op while (len != 0) {
231 fb1a36c0 2022-01-09 op if (ctx == NULL) {
232 fb1a36c0 2022-01-09 op r = read(sock, d, len);
233 fb1a36c0 2022-01-09 op if (r == 0 || r == -1)
234 fb1a36c0 2022-01-09 op errx(1, "EOF");
236 fb1a36c0 2022-01-09 op r = tls_read(ctx, d, len);
237 fb1a36c0 2022-01-09 op if (r == TLS_WANT_POLLIN || r == TLS_WANT_POLLOUT)
240 fb1a36c0 2022-01-09 op errx(1, "tls: %s", tls_error(ctx));
249 fb1a36c0 2022-01-09 op recv_msg(void)
251 fb1a36c0 2022-01-09 op uint32_t len, l;
252 fb1a36c0 2022-01-09 op char tmp[BUFSIZ];
254 fb1a36c0 2022-01-09 op mustread(&len, sizeof(len));
255 fb1a36c0 2022-01-09 op len = le32toh(len);
256 fb1a36c0 2022-01-09 op if (len < HEADERSIZE)
257 fb1a36c0 2022-01-09 op errx(1, "read message of invalid length %d", len);
259 fb1a36c0 2022-01-09 op len -= 4; /* skip the length just read */
261 fb1a36c0 2022-01-09 op while (len != 0) {
262 fb1a36c0 2022-01-09 op l = MIN(len, sizeof(tmp));
263 fb1a36c0 2022-01-09 op mustread(tmp, l);
265 fb1a36c0 2022-01-09 op evbuffer_add(buf, tmp, l);
269 5565d021 2022-01-16 op hexdump("incoming packet", EVBUFFER_DATA(buf),
270 5565d021 2022-01-16 op EVBUFFER_LENGTH(buf));
273 fb1a36c0 2022-01-09 op static uint64_t
274 fb1a36c0 2022-01-09 op np_read64(struct evbuffer *buf)
278 fb1a36c0 2022-01-09 op evbuffer_remove(buf, &n, sizeof(n));
279 fb1a36c0 2022-01-09 op return le64toh(n);
282 fb1a36c0 2022-01-09 op static uint32_t
283 fb1a36c0 2022-01-09 op np_read32(struct evbuffer *buf)
287 fb1a36c0 2022-01-09 op evbuffer_remove(buf, &n, sizeof(n));
288 fb1a36c0 2022-01-09 op return le32toh(n);
291 fb1a36c0 2022-01-09 op static uint16_t
292 fb1a36c0 2022-01-09 op np_read16(struct evbuffer *buf)
296 fb1a36c0 2022-01-09 op evbuffer_remove(buf, &n, sizeof(n));
297 fb1a36c0 2022-01-09 op return le16toh(n);
300 fb1a36c0 2022-01-09 op static uint16_t
301 fb1a36c0 2022-01-09 op np_read8(struct evbuffer *buf)
305 fb1a36c0 2022-01-09 op evbuffer_remove(buf, &n, sizeof(n));
309 fb1a36c0 2022-01-09 op static char *
310 fb1a36c0 2022-01-09 op np_readstr(struct evbuffer *buf)
312 fb1a36c0 2022-01-09 op uint16_t len;
315 fb1a36c0 2022-01-09 op len = np_read16(buf);
316 fb1a36c0 2022-01-09 op assert(EVBUFFER_LENGTH(buf) >= len);
318 fb1a36c0 2022-01-09 op if ((str = calloc(1, len+1)) == NULL)
319 fb1a36c0 2022-01-09 op err(1, "calloc");
320 fb1a36c0 2022-01-09 op evbuffer_remove(buf, str, len);
325 fb1a36c0 2022-01-09 op np_read_qid(struct evbuffer *buf, struct qid *qid)
327 fb1a36c0 2022-01-09 op assert(EVBUFFER_LENGTH(buf) >= QIDSIZE);
329 fb1a36c0 2022-01-09 op qid->type = np_read8(buf);
330 fb1a36c0 2022-01-09 op qid->vers = np_read32(buf);
331 fb1a36c0 2022-01-09 op qid->path = np_read64(buf);
335 fb1a36c0 2022-01-09 op np_read_stat(struct evbuffer *buf, struct np_stat *st)
337 fb1a36c0 2022-01-09 op uint16_t size;
339 fb1a36c0 2022-01-09 op memset(st, 0, sizeof(*st));
341 fb1a36c0 2022-01-09 op size = np_read16(buf);
342 fb1a36c0 2022-01-09 op if (size > EVBUFFER_LENGTH(buf))
345 fb1a36c0 2022-01-09 op st->type = np_read16(buf);
346 fb1a36c0 2022-01-09 op st->dev = np_read32(buf);
347 fb1a36c0 2022-01-09 op np_read_qid(buf, &st->qid);
348 fb1a36c0 2022-01-09 op st->mode = np_read32(buf);
349 fb1a36c0 2022-01-09 op st->atime = np_read32(buf);
350 fb1a36c0 2022-01-09 op st->mtime = np_read32(buf);
351 fb1a36c0 2022-01-09 op st->length = np_read64(buf);
352 fb1a36c0 2022-01-09 op st->name = np_readstr(buf);
353 fb1a36c0 2022-01-09 op st->uid = np_readstr(buf);
354 fb1a36c0 2022-01-09 op st->gid = np_readstr(buf);
355 fb1a36c0 2022-01-09 op st->muid = np_readstr(buf);
361 fb1a36c0 2022-01-09 op expect(uint8_t type)
365 fb1a36c0 2022-01-09 op t = np_read8(buf);
366 fb1a36c0 2022-01-09 op if (t == type)
369 fb1a36c0 2022-01-09 op if (t == Rerror) {
372 fb1a36c0 2022-01-09 op /* skip tag */
373 fb1a36c0 2022-01-09 op np_read16(buf);
375 fb1a36c0 2022-01-09 op err = np_readstr(buf);
376 fb1a36c0 2022-01-09 op errx(1, "expected %s, got error %s",
377 fb1a36c0 2022-01-09 op pp_msg_type(type), err);
380 fb1a36c0 2022-01-09 op errx(1, "expected %s, got msg type %s",
381 fb1a36c0 2022-01-09 op pp_msg_type(type), pp_msg_type(t));
385 fb1a36c0 2022-01-09 op expect2(uint8_t type, uint16_t tag)
389 fb1a36c0 2022-01-09 op expect(type);
391 fb1a36c0 2022-01-09 op t = np_read16(buf);
392 fb1a36c0 2022-01-09 op if (t == tag)
395 fb1a36c0 2022-01-09 op errx(1, "expected tag 0x%x, got 0x%x", tag, t);
398 8f9f99f7 2022-01-16 op static char *
399 8f9f99f7 2022-01-16 op check(uint8_t type, uint16_t tag)
401 8f9f99f7 2022-01-16 op uint16_t rtag;
402 8f9f99f7 2022-01-16 op uint8_t rtype;
404 8f9f99f7 2022-01-16 op rtype = np_read8(buf);
405 8f9f99f7 2022-01-16 op rtag = np_read16(buf);
406 8f9f99f7 2022-01-16 op if (rtype == type) {
407 8f9f99f7 2022-01-16 op if (rtag != tag)
408 8f9f99f7 2022-01-16 op errx(1, "expected tag 0x%x, got 0x%x", tag, rtag);
412 8f9f99f7 2022-01-16 op if (rtype == Rerror)
413 8f9f99f7 2022-01-16 op return np_readstr(buf);
415 8f9f99f7 2022-01-16 op errx(1, "expected %s, got msg type %s",
416 8f9f99f7 2022-01-16 op pp_msg_type(type), pp_msg_type(rtype));
420 fb1a36c0 2022-01-09 op do_version(void)
422 fb1a36c0 2022-01-09 op char *version;
424 fb1a36c0 2022-01-09 op tversion(VERSION9P, MSIZE9P);
427 fb1a36c0 2022-01-09 op expect2(Rversion, NOTAG);
429 fb1a36c0 2022-01-09 op msize = np_read32(buf);
430 fb1a36c0 2022-01-09 op version = np_readstr(buf);
432 315eafe8 2022-05-23 op if (msize > MSIZE9P || msize < 256)
433 fb1a36c0 2022-01-09 op errx(1, "got unexpected msize: %d", msize);
434 fb1a36c0 2022-01-09 op if (strcmp(version, VERSION9P))
435 fb1a36c0 2022-01-09 op errx(1, "unexpected 9p version: %s", version);
437 fb1a36c0 2022-01-09 op free(version);
438 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
442 fb1a36c0 2022-01-09 op do_attach(const char *path)
444 fb1a36c0 2022-01-09 op struct qid qid;
446 fb1a36c0 2022-01-09 op if (path == NULL)
449 fb1a36c0 2022-01-09 op tattach(pwdfid, NOFID, user, path);
452 fb1a36c0 2022-01-09 op expect2(Rattach, iota_tag);
453 fb1a36c0 2022-01-09 op np_read_qid(buf, &qid);
455 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
458 fb1a36c0 2022-01-09 op static uint32_t
459 fb1a36c0 2022-01-09 op do_open(uint32_t fid, uint8_t mode)
461 fb1a36c0 2022-01-09 op struct qid qid;
462 fb1a36c0 2022-01-09 op uint32_t iounit;
464 fb1a36c0 2022-01-09 op topen(fid, mode);
467 fb1a36c0 2022-01-09 op expect2(Ropen, iota_tag);
469 01fb39ab 2022-01-16 op np_read_qid(buf, &qid);
470 01fb39ab 2022-01-16 op iounit = np_read32(buf);
472 01fb39ab 2022-01-16 op ASSERT_EMPTYBUF();
474 01fb39ab 2022-01-16 op return iounit;
477 01fb39ab 2022-01-16 op static uint32_t
478 01fb39ab 2022-01-16 op do_create(uint32_t fid, const char *name, uint32_t perm, uint8_t mode)
480 01fb39ab 2022-01-16 op struct qid qid;
481 01fb39ab 2022-01-16 op uint32_t iounit;
483 01fb39ab 2022-01-16 op tcreate(fid, name, perm, mode);
486 01fb39ab 2022-01-16 op expect2(Rcreate, iota_tag);
488 fb1a36c0 2022-01-09 op np_read_qid(buf, &qid);
489 fb1a36c0 2022-01-09 op iounit = np_read32(buf);
491 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
493 fb1a36c0 2022-01-09 op return iounit;
497 fb1a36c0 2022-01-09 op do_clunk(uint32_t fid)
502 fb1a36c0 2022-01-09 op expect2(Rclunk, iota_tag);
504 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
507 1bb1bfca 2022-01-16 op static char *
508 fb1a36c0 2022-01-09 op dup_fid(int fid, int nfid)
510 fb1a36c0 2022-01-09 op uint16_t nwqid;
511 1bb1bfca 2022-01-16 op char *errstr;
513 fb1a36c0 2022-01-09 op twalk(fid, nfid, NULL, 0);
517 1bb1bfca 2022-01-16 op if ((errstr = check(Rwalk, iota_tag)) != NULL)
518 1bb1bfca 2022-01-16 op return errstr;
520 fb1a36c0 2022-01-09 op nwqid = np_read16(buf);
521 fb1a36c0 2022-01-09 op assert(nwqid == 0);
523 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
528 8f9f99f7 2022-01-16 op static char *
529 8f9f99f7 2022-01-16 op walk_path(int fid, int newfid, const char *path, int *missing,
530 8f9f99f7 2022-01-16 op struct qid *qid)
532 8f9f99f7 2022-01-16 op char *wnames[MAXWELEM], *p, *t, *errstr;
533 fb1a36c0 2022-01-09 op size_t nwname, i;
534 fb1a36c0 2022-01-09 op uint16_t nwqid;
536 fb1a36c0 2022-01-09 op if ((p = strdup(path)) == NULL)
537 fb1a36c0 2022-01-09 op err(1, "strdup");
540 fb1a36c0 2022-01-09 op /* strip initial ./ */
541 fb1a36c0 2022-01-09 op if (t[0] == '.' && t[1] == '/')
544 fb1a36c0 2022-01-09 op for (nwname = 0; nwname < nitems(wnames) &&
545 fb1a36c0 2022-01-09 op (wnames[nwname] = strsep(&t, "/")) != NULL;) {
546 fb1a36c0 2022-01-09 op if (*wnames[nwname] != '\0')
550 fb1a36c0 2022-01-09 op twalk(fid, newfid, (const char **)wnames, nwname);
554 8f9f99f7 2022-01-16 op *missing = nwname;
555 5fe03b5c 2022-01-21 op if ((errstr = check(Rwalk, iota_tag)) != NULL) {
557 8f9f99f7 2022-01-16 op return errstr;
560 fb1a36c0 2022-01-09 op nwqid = np_read16(buf);
561 fb1a36c0 2022-01-09 op assert(nwqid <= nwname);
563 fb1a36c0 2022-01-09 op /* consume all qids */
564 43d117b1 2022-01-16 op for (i = 0; i < nwqid; ++i)
565 fb1a36c0 2022-01-09 op np_read_qid(buf, qid);
569 8f9f99f7 2022-01-16 op *missing = nwname - nwqid;
574 fb1a36c0 2022-01-09 op do_stat(int fid, struct np_stat *st)
579 fb1a36c0 2022-01-09 op expect2(Rstat, iota_tag);
581 caa64af1 2022-02-02 op /* eat up the first two byte length */
582 caa64af1 2022-02-02 op np_read16(buf);
584 fb1a36c0 2022-01-09 op if (np_read_stat(buf, st) == -1)
585 fb1a36c0 2022-01-09 op errx(1, "invalid stat struct read");
587 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
590 c37e1cfe 2022-01-17 op static char *
591 c37e1cfe 2022-01-17 op do_wstat(int fid, const struct np_stat *st)
593 c37e1cfe 2022-01-17 op char *errstr;
595 c37e1cfe 2022-01-17 op twstat(fid, st);
599 c37e1cfe 2022-01-17 op if ((errstr = check(Rwstat, iota_tag)) != NULL)
600 c37e1cfe 2022-01-17 op return errstr;
602 c37e1cfe 2022-01-17 op ASSERT_EMPTYBUF();
607 fb1a36c0 2022-01-09 op static size_t
608 fb1a36c0 2022-01-09 op do_read(int fid, uint64_t off, uint32_t count, void *data)
612 fb1a36c0 2022-01-09 op tread(fid, off, count);
615 fb1a36c0 2022-01-09 op expect2(Rread, iota_tag);
617 fb1a36c0 2022-01-09 op r = np_read32(buf);
618 fb1a36c0 2022-01-09 op assert(r == EVBUFFER_LENGTH(buf));
619 fb1a36c0 2022-01-09 op assert(r <= count);
620 fb1a36c0 2022-01-09 op evbuffer_remove(buf, data, r);
622 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
627 01fb39ab 2022-01-16 op static size_t
628 01fb39ab 2022-01-16 op do_write(int fid, uint64_t off, uint32_t count, void *data)
632 01fb39ab 2022-01-16 op twrite(fid, off, data, count);
635 01fb39ab 2022-01-16 op expect2(Rwrite, iota_tag);
637 01fb39ab 2022-01-16 op r = np_read32(buf);
638 01fb39ab 2022-01-16 op assert(r <= count);
640 01fb39ab 2022-01-16 op ASSERT_EMPTYBUF();
646 fb1a36c0 2022-01-09 op draw_progress(const char *pre, const struct progress *p)
648 fb1a36c0 2022-01-09 op struct winsize ws;
655 fb1a36c0 2022-01-09 op perc = 100.0 * p->done / p->max;
656 fb1a36c0 2022-01-09 op if (!tty_p) {
657 fb1a36c0 2022-01-09 op fprintf(stderr, "%s: %d%%\n", pre, (int)perc);
661 fb1a36c0 2022-01-09 op if (resized) {
664 fb1a36c0 2022-01-09 op if (ioctl(0, TIOCGWINSZ, &ws) == -1)
666 fb1a36c0 2022-01-09 op tty_width = ws.ws_col;
668 fb1a36c0 2022-01-09 op w = tty_width;
670 fb1a36c0 2022-01-09 op if (pre == NULL ||
671 fb1a36c0 2022-01-09 op ((l = printf("\r%s ", pre)) == -1 || l >= w))
674 fb1a36c0 2022-01-09 op w -= l + 2 + 5; /* 2 for |, 5 for percentage + \n */
676 fb1a36c0 2022-01-09 op printf("%4d%%\n", (int)perc);
682 fb1a36c0 2022-01-09 op l = w * MIN(100.0, perc) / 100.0;
683 fb1a36c0 2022-01-09 op for (i = 0; i < l; i++)
685 fb1a36c0 2022-01-09 op for (; i < w; i++)
687 fb1a36c0 2022-01-09 op printf("|%4d%%", (int)perc);
689 fb1a36c0 2022-01-09 op fflush(stdout);
693 76521d26 2022-01-16 op fetch_fid(int fid, int fd, const char *name)
695 02d5b425 2022-05-23 op static char buf[MSIZE9P];
696 fb1a36c0 2022-01-09 op struct progress p = {0};
697 fb1a36c0 2022-01-09 op struct np_stat st;
701 fb1a36c0 2022-01-09 op do_stat(fid, &st);
702 fb1a36c0 2022-01-09 op do_open(fid, KOREAD);
704 fb1a36c0 2022-01-09 op p.max = st.length;
706 02d5b425 2022-05-23 op size_t len, off;
709 02d5b425 2022-05-23 op len = MIN(sizeof(buf), msize);
710 7bafb2fd 2022-08-28 op len -= IOHDRSZ; /* for the request' fields */
712 02d5b425 2022-05-23 op r = do_read(fid, p.done, len, buf);
716 b2263c45 2022-01-16 op for (off = 0; off < r; off += nw)
717 b2263c45 2022-01-16 op if ((nw = write(fd, buf + off, r - off)) == 0 ||
724 9e7dd230 2022-01-12 op draw_progress(name, &p);
727 fb1a36c0 2022-01-09 op /* throttle, for debugging purpose */
729 fb1a36c0 2022-01-09 op struct timespec ts = { 0, 500000000 };
730 fb1a36c0 2022-01-09 op nanosleep(&ts, NULL);
736 fb1a36c0 2022-01-09 op putchar('\n');
738 01fb39ab 2022-01-16 op do_clunk(fid);
743 16f1f3bf 2022-01-21 op send_fid(int fid, const char *fnam, int open_flags, int fd, const char *name)
745 02d5b425 2022-05-23 op static char buf[MSIZE9P];
746 01fb39ab 2022-01-16 op struct progress p = {0};
747 01fb39ab 2022-01-16 op struct stat sb;
749 02d5b425 2022-05-23 op size_t w, len;
751 01fb39ab 2022-01-16 op if (fstat(fd, &sb) == -1)
752 01fb39ab 2022-01-16 op err(1, "fstat");
754 01fb39ab 2022-01-16 op if (fnam != NULL)
755 01fb39ab 2022-01-16 op do_create(fid, fnam, 0644, KOWRITE);
757 16f1f3bf 2022-01-21 op do_open(fid, open_flags | KOWRITE);
759 01fb39ab 2022-01-16 op p.max = sb.st_size;
761 02d5b425 2022-05-23 op len = MIN(sizeof(buf), msize);
762 02d5b425 2022-05-23 op len -= HEADERSIZE + 4 + 4 + 8; /* for the request' fields */
764 02d5b425 2022-05-23 op r = read(fd, buf, len);
768 01fb39ab 2022-01-16 op err(1, "read");
770 01fb39ab 2022-01-16 op w = do_write(fid, p.done, r, buf);
773 01fb39ab 2022-01-16 op draw_progress(name, &p);
776 01fb39ab 2022-01-16 op /* throttle, for debugging purpose */
778 01fb39ab 2022-01-16 op struct timespec ts = { 0, 500000000 };
779 01fb39ab 2022-01-16 op nanosleep(&ts, NULL);
784 01fb39ab 2022-01-16 op putchar('\n');
785 fb1a36c0 2022-01-09 op do_clunk(fid);
789 9c48f915 2022-01-16 op woc_file(int fd, const char *prompt, const char *path)
791 9c48f915 2022-01-16 op struct qid qid;
792 9c48f915 2022-01-16 op const char *n = NULL;
793 9c48f915 2022-01-16 op char *errstr;
794 9c48f915 2022-01-16 op int nfid, miss;
796 8d8fb849 2022-01-17 op nfid = nextfid();
797 9c48f915 2022-01-16 op errstr = walk_path(pwdfid, nfid, path, &miss, &qid);
798 9c48f915 2022-01-16 op if (errstr != NULL && miss > 1) {
799 9c48f915 2022-01-16 op printf("%s: %s\n", path, errstr);
800 9c48f915 2022-01-16 op free(errstr);
804 9c48f915 2022-01-16 op if (errstr != NULL || miss == 1) {
805 9c48f915 2022-01-16 op char p[PATH_MAX], *dn;
808 9c48f915 2022-01-16 op * If it's only one component missing (the file name), walk
809 9c48f915 2022-01-16 op * to the parent directory and try to create the file.
812 9c48f915 2022-01-16 op if (strlcpy(p, path, sizeof(p)) >= sizeof(p)) {
813 9c48f915 2022-01-16 op printf("path too long: %s\n", path);
816 9c48f915 2022-01-16 op dn = dirname(p);
818 9c48f915 2022-01-16 op if (!strcmp(dn, ".")) {
819 9c48f915 2022-01-16 op errstr = dup_fid(pwdfid, nfid);
822 9c48f915 2022-01-16 op errstr = walk_path(pwdfid, nfid, dn, &miss, &qid);
824 9c48f915 2022-01-16 op if (errstr != NULL) {
825 9c48f915 2022-01-16 op printf("%s: %s\n", dn, errstr);
826 9c48f915 2022-01-16 op free(errstr);
830 9c48f915 2022-01-16 op if (miss != 0) {
831 9c48f915 2022-01-16 op printf("%s: not a directory\n", dn);
835 9c48f915 2022-01-16 op if ((n = strrchr(path, '/')) != NULL)
841 9c48f915 2022-01-16 op free(errstr);
843 9c48f915 2022-01-16 op if (miss > 1) {
844 9c48f915 2022-01-16 op printf("can't create %s: missing %d path component(s)\n",
849 16f1f3bf 2022-01-21 op send_fid(nfid, n, KOTRUNC, fd, prompt);
854 fb1a36c0 2022-01-09 op do_tls_connect(const char *host, const char *port)
856 fb1a36c0 2022-01-09 op int handshake;
858 fb1a36c0 2022-01-09 op if ((tlsconf = tls_config_new()) == NULL)
859 fb1a36c0 2022-01-09 op fatalx("tls_config_new");
860 fb1a36c0 2022-01-09 op tls_config_insecure_noverifycert(tlsconf);
861 fb1a36c0 2022-01-09 op tls_config_insecure_noverifyname(tlsconf);
863 3847d69e 2022-01-30 op if (keypath == NULL)
864 3847d69e 2022-01-30 op keypath = crtpath;
866 fb1a36c0 2022-01-09 op if (tls_config_set_keypair_file(tlsconf, crtpath, keypath) == -1)
867 fb1a36c0 2022-01-09 op fatalx("can't load certs (%s, %s)", crtpath, keypath);
869 fb1a36c0 2022-01-09 op if ((ctx = tls_client()) == NULL)
870 fb1a36c0 2022-01-09 op fatal("tls_client");
871 fb1a36c0 2022-01-09 op if (tls_configure(ctx, tlsconf) == -1)
872 fb1a36c0 2022-01-09 op fatalx("tls_configure: %s", tls_error(ctx));
874 fb1a36c0 2022-01-09 op if (tls_connect(ctx, host, port) == -1)
875 fb1a36c0 2022-01-09 op fatalx("can't connect to %s:%s: %s", host, port,
876 fb1a36c0 2022-01-09 op tls_error(ctx));
878 fb1a36c0 2022-01-09 op for (handshake = 0; !handshake;) {
879 fb1a36c0 2022-01-09 op switch (tls_handshake(ctx)) {
881 fb1a36c0 2022-01-09 op fatalx("tls_handshake: %s", tls_error(ctx));
883 fb1a36c0 2022-01-09 op handshake = 1;
890 fb1a36c0 2022-01-09 op do_ctxt_connect(const char *host, const char *port)
892 fb1a36c0 2022-01-09 op struct addrinfo hints, *res, *res0;
893 fb1a36c0 2022-01-09 op int error, saved_errno;
894 fb1a36c0 2022-01-09 op const char *cause = NULL;
896 fb1a36c0 2022-01-09 op memset(&hints, 0, sizeof(hints));
897 fb1a36c0 2022-01-09 op hints.ai_family = AF_UNSPEC;
898 fb1a36c0 2022-01-09 op hints.ai_socktype = SOCK_STREAM;
899 fb1a36c0 2022-01-09 op error = getaddrinfo(host, port, &hints, &res0);
901 fb1a36c0 2022-01-09 op errx(1, "%s", gai_strerror(error));
904 fb1a36c0 2022-01-09 op for (res = res0; res != NULL; res = res->ai_next) {
905 db066f1e 2022-01-12 op sock = socket(res->ai_family, res->ai_socktype|SOCK_CLOEXEC,
906 fb1a36c0 2022-01-09 op res->ai_protocol);
907 fb1a36c0 2022-01-09 op if (sock == -1) {
908 fb1a36c0 2022-01-09 op cause = "socket";
912 fb1a36c0 2022-01-09 op if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
913 fb1a36c0 2022-01-09 op cause = "connect";
914 fb1a36c0 2022-01-09 op saved_errno = errno;
916 fb1a36c0 2022-01-09 op errno = saved_errno;
924 fb1a36c0 2022-01-09 op if (sock == -1)
925 fb1a36c0 2022-01-09 op err(1, "%s", cause);
926 fb1a36c0 2022-01-09 op freeaddrinfo(res0);
930 fb1a36c0 2022-01-09 op do_connect(const char *connspec, const char *path)
933 fb1a36c0 2022-01-09 op const char *port;
935 fb1a36c0 2022-01-09 op host = xstrdup(connspec);
936 e22049cc 2022-08-28 op if ((t = strchr(host, '@')) != NULL) {
937 e22049cc 2022-08-28 op if (t == host)
938 e22049cc 2022-08-28 op errx(1, "invalid connection string: %s", connspec);
942 e22049cc 2022-08-28 op } else if ((user = getenv("USER")) == NULL)
943 e22049cc 2022-08-28 op errx(1, "USER not defined");
945 e22049cc 2022-08-28 op if ((t = strchr(host, ':')) != NULL) {
948 e22049cc 2022-08-28 op if (*port == '\0')
949 e22049cc 2022-08-28 op errx(1, "invalid connection string: %s", connspec);
951 fb1a36c0 2022-01-09 op port = "1337";
953 fb1a36c0 2022-01-09 op printf("connecting to %s:%s...", host, port);
954 fb1a36c0 2022-01-09 op fflush(stdout);
957 fb1a36c0 2022-01-09 op do_tls_connect(host, port);
959 fb1a36c0 2022-01-09 op do_ctxt_connect(host, port);
961 fb1a36c0 2022-01-09 op printf(" done!\n");
963 fb1a36c0 2022-01-09 op do_version();
964 fb1a36c0 2022-01-09 op do_attach(path);
968 64d8db89 2022-01-16 op tmp_file(char sfn[TMPFSTRLEN])
972 64d8db89 2022-01-16 op strlcpy(sfn, TMPFSTR, TMPFSTRLEN);
973 64d8db89 2022-01-16 op if ((tmpfd = mkstemp(sfn)) == -1) {
974 64d8db89 2022-01-16 op warn("mkstemp %s", sfn);
978 aa76fd7f 2022-01-16 op /* set the close-on-exec flag */
979 aa76fd7f 2022-01-16 op if (fcntl(tmpfd, F_SETFD, FD_CLOEXEC) == -1) {
980 aa76fd7f 2022-01-16 op warn("fcntl");
981 aa76fd7f 2022-01-16 op close(tmpfd);
985 64d8db89 2022-01-16 op return tmpfd;
988 1f5fb843 2022-01-16 op static inline const char *
989 1f5fb843 2022-01-16 op pp_perm(uint8_t x)
991 1f5fb843 2022-01-16 op switch (x & 0x7) {
993 1f5fb843 2022-01-16 op return "---";
995 1f5fb843 2022-01-16 op return "--x";
997 1f5fb843 2022-01-16 op return "-w-";
999 1f5fb843 2022-01-16 op return "-wx";
1001 1f5fb843 2022-01-16 op return "r--";
1003 1f5fb843 2022-01-16 op return "r-x";
1005 1f5fb843 2022-01-16 op return "rw-";
1007 1f5fb843 2022-01-16 op return "rwx";
1009 1f5fb843 2022-01-16 op /* unreachable, just for the compiler' happiness */
1010 1f5fb843 2022-01-16 op return "???";
1014 c37e1cfe 2022-01-17 op static inline void
1015 c37e1cfe 2022-01-17 op prepare_wstat(struct np_stat *st)
1017 c37e1cfe 2022-01-17 op memset(st, 0xFF, sizeof(*st));
1018 c37e1cfe 2022-01-17 op st->name = NULL;
1019 c37e1cfe 2022-01-17 op st->uid = NULL;
1020 c37e1cfe 2022-01-17 op st->gid = NULL;
1021 c37e1cfe 2022-01-17 op st->muid = NULL;
1025 fb1a36c0 2022-01-09 op cmd_bell(int argc, const char **argv)
1027 fb1a36c0 2022-01-09 op if (argc == 0) {
1028 fb1a36c0 2022-01-09 op bell = !bell;
1030 fb1a36c0 2022-01-09 op puts("bell mode enabled");
1032 fb1a36c0 2022-01-09 op puts("bell mode disabled");
1036 fb1a36c0 2022-01-09 op if (argc != 1)
1039 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "on")) {
1041 fb1a36c0 2022-01-09 op puts("bell mode enabled");
1045 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "off")) {
1047 fb1a36c0 2022-01-09 op puts("bell mode disabled");
1052 fb1a36c0 2022-01-09 op printf("bell [on | off]\n");
1056 fb1a36c0 2022-01-09 op cmd_bye(int argc, const char **argv)
1058 fb1a36c0 2022-01-09 op log_warnx("bye\n");
1063 fb1a36c0 2022-01-09 op cmd_cd(int argc, const char **argv)
1065 fb1a36c0 2022-01-09 op struct qid qid;
1066 8f9f99f7 2022-01-16 op int nfid, miss;
1067 8f9f99f7 2022-01-16 op char *errstr;
1069 fb1a36c0 2022-01-09 op if (argc != 1) {
1070 fb1a36c0 2022-01-09 op printf("usage: cd remote-path\n");
1074 8d8fb849 2022-01-17 op nfid = nextfid();
1075 8f9f99f7 2022-01-16 op errstr = walk_path(pwdfid, nfid, argv[0], &miss, &qid);
1076 8f9f99f7 2022-01-16 op if (errstr != NULL) {
1077 8f9f99f7 2022-01-16 op printf("%s: %s\n", argv[0], errstr);
1078 8f9f99f7 2022-01-16 op free(errstr);
1082 8f9f99f7 2022-01-16 op if (miss != 0 || !(qid.type & QTDIR)) {
1083 8f9f99f7 2022-01-16 op printf("%s: not a directory\n", argv[0]);
1084 fcd9d510 2022-01-16 op if (miss == 0)
1085 fcd9d510 2022-01-16 op do_clunk(nfid);
1089 8f9f99f7 2022-01-16 op do_clunk(pwdfid);
1090 8f9f99f7 2022-01-16 op pwdfid = nfid;
1094 9c48f915 2022-01-16 op cmd_edit(int argc, const char **argv)
1096 9c48f915 2022-01-16 op struct qid qid;
1097 9c48f915 2022-01-16 op int nfid, tmpfd, miss;
1098 9c48f915 2022-01-16 op char sfn[TMPFSTRLEN], p[PATH_MAX], *name, *errstr;
1099 9c48f915 2022-01-16 op const char *ed;
1101 9c48f915 2022-01-16 op if (argc != 1) {
1102 9c48f915 2022-01-16 op puts("usage: edit file");
1106 9c48f915 2022-01-16 op if ((ed = getenv("VISUAL")) == NULL &&
1107 9c48f915 2022-01-16 op (ed = getenv("EDITOR")) == NULL)
1110 8d8fb849 2022-01-17 op nfid = nextfid();
1111 9c48f915 2022-01-16 op errstr = walk_path(pwdfid, nfid, *argv, &miss, &qid);
1112 9c48f915 2022-01-16 op if (errstr != NULL) {
1113 9c48f915 2022-01-16 op printf("%s: %s\n", *argv, errstr);
1114 9c48f915 2022-01-16 op free(errstr);
1118 9c48f915 2022-01-16 op if (miss != 0 || qid.type != 0) {
1119 9c48f915 2022-01-16 op printf("%s: not a file\n", *argv);
1120 9c48f915 2022-01-16 op if (miss == 0)
1121 9c48f915 2022-01-16 op do_clunk(nfid);
1125 9c48f915 2022-01-16 op if ((tmpfd = tmp_file(sfn)) == -1) {
1126 9c48f915 2022-01-16 op do_clunk(nfid);
1130 9c48f915 2022-01-16 op strlcpy(p, *argv, sizeof(p));
1131 9c48f915 2022-01-16 op name = basename(p);
1133 02571aa1 2022-01-29 op if (fetch_fid(nfid, tmpfd, name)) {
1134 02571aa1 2022-01-29 op warn("failed fetch or can't write %s", sfn);
1137 9c48f915 2022-01-16 op close(tmpfd);
1139 9c48f915 2022-01-16 op spawn(ed, sfn, NULL);
1142 9c48f915 2022-01-16 op * Re-open the file because it's not guaranteed that the
1143 9c48f915 2022-01-16 op * file descriptor tmpfd is still associated with the file
1144 9c48f915 2022-01-16 op * pointed by sfn: it's not uncommon for editor to write
1145 9c48f915 2022-01-16 op * a backup file and then rename(2) it to the file name.
1147 9c48f915 2022-01-16 op if ((tmpfd = open(sfn, O_RDONLY)) == -1) {
1148 9c48f915 2022-01-16 op warn("can't open %s", sfn);
1152 9c48f915 2022-01-16 op woc_file(tmpfd, *argv, name);
1153 9c48f915 2022-01-16 op close(tmpfd);
1156 9c48f915 2022-01-16 op unlink(sfn);
1160 fb1a36c0 2022-01-09 op cmd_get(int argc, const char **argv)
1162 fb1a36c0 2022-01-09 op struct qid qid;
1163 fb1a36c0 2022-01-09 op const char *l;
1164 8f9f99f7 2022-01-16 op char *errstr;
1165 8f9f99f7 2022-01-16 op int nfid, fd, miss;
1167 fb1a36c0 2022-01-09 op if (argc != 1 && argc != 2) {
1168 fb1a36c0 2022-01-09 op printf("usage: get remote-file [local-file]\n");
1172 fb1a36c0 2022-01-09 op if (argc == 2)
1173 fb1a36c0 2022-01-09 op l = argv[1];
1174 fb1a36c0 2022-01-09 op else if ((l = strrchr(argv[0], '/')) != NULL)
1175 fb1a36c0 2022-01-09 op l++; /* skip / */
1177 2ba25711 2022-01-16 op l = argv[0];
1179 8d8fb849 2022-01-17 op nfid = nextfid();
1180 8f9f99f7 2022-01-16 op errstr = walk_path(pwdfid, nfid, argv[0], &miss, &qid);
1181 8f9f99f7 2022-01-16 op if (errstr != NULL) {
1182 8f9f99f7 2022-01-16 op printf("%s: %s\n", argv[0], errstr);
1183 8f9f99f7 2022-01-16 op free(errstr);
1187 8f9f99f7 2022-01-16 op if (miss != 0 || qid.type != 0) {
1188 8f9f99f7 2022-01-16 op printf("%s: not a file\n", argv[0]);
1189 fcd9d510 2022-01-16 op if (miss == 0)
1190 fcd9d510 2022-01-16 op do_clunk(nfid);
1194 76521d26 2022-01-16 op if ((fd = open(l, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0644)) == -1) {
1195 76521d26 2022-01-16 op warn("can't open %s", l);
1196 76521d26 2022-01-16 op do_clunk(nfid);
1200 02571aa1 2022-01-29 op if (fetch_fid(nfid, fd, l) == -1)
1201 02571aa1 2022-01-29 op warn("write %s", l);
1206 5565d021 2022-01-16 op cmd_hexdump(int argc, const char **argv)
1208 5565d021 2022-01-16 op if (argc == 0) {
1209 5565d021 2022-01-16 op xdump = !xdump;
1211 5565d021 2022-01-16 op puts("hexdump mode enabled");
1213 5565d021 2022-01-16 op puts("hexdump mode disabled");
1217 5565d021 2022-01-16 op if (argc > 1)
1220 5565d021 2022-01-16 op if (!strcmp(*argv, "on")) {
1222 5565d021 2022-01-16 op puts("hexdump mode enabled");
1226 5565d021 2022-01-16 op if (!strcmp(*argv, "off")) {
1228 5565d021 2022-01-16 op puts("hexdump mode disabled");
1233 5565d021 2022-01-16 op puts("usage: hexdump [on | off]");
1237 fb1a36c0 2022-01-09 op cmd_lcd(int argc, const char **argv)
1239 fb1a36c0 2022-01-09 op const char *dir;
1241 fb1a36c0 2022-01-09 op if (argc > 1) {
1242 d166ffc0 2022-01-16 op printf("usage: lcd [local-directory]\n");
1246 fb1a36c0 2022-01-09 op if (argc == 1)
1247 fb1a36c0 2022-01-09 op dir = *argv;
1249 fb1a36c0 2022-01-09 op if (argc == 0 && (dir = getenv("HOME")) == NULL) {
1250 fb1a36c0 2022-01-09 op printf("HOME is not defined\n");
1254 fb1a36c0 2022-01-09 op if (chdir(dir) == -1)
1255 fb1a36c0 2022-01-09 op printf("cd: %s: %s\n", dir, strerror(errno));
1259 fb1a36c0 2022-01-09 op cmd_lpwd(int argc, const char **argv)
1261 fb1a36c0 2022-01-09 op char path[PATH_MAX];
1263 d166ffc0 2022-01-16 op if (argc != 0) {
1264 d166ffc0 2022-01-16 op printf("usage: lpwd\n");
1268 fb1a36c0 2022-01-09 op if (getcwd(path, sizeof(path)) == NULL) {
1269 fb1a36c0 2022-01-09 op printf("lpwd: %s\n", strerror(errno));
1273 fb1a36c0 2022-01-09 op printf("%s\n", path);
1277 fb1a36c0 2022-01-09 op cmd_ls(int argc, const char **argv)
1279 fb1a36c0 2022-01-09 op struct np_stat st;
1280 4f52d9af 2022-01-16 op time_t now, mtime;
1281 4f52d9af 2022-01-16 op struct tm *tm;
1282 fb1a36c0 2022-01-09 op uint64_t off = 0;
1283 fb1a36c0 2022-01-09 op uint32_t len;
1285 4f52d9af 2022-01-16 op const char *timfmt;
1286 4f52d9af 2022-01-16 op char fmt[FMT_SCALED_STRSIZE], tim[13], *errstr;
1288 fb1a36c0 2022-01-09 op if (argc != 0) {
1289 fb1a36c0 2022-01-09 op printf("ls don't take arguments (yet)\n");
1293 4f52d9af 2022-01-16 op now = time(NULL);
1295 a9288ad9 2022-01-17 op nfid = nextfid();
1296 a9288ad9 2022-01-17 op if ((errstr = dup_fid(pwdfid, nfid)) != NULL) {
1297 1bb1bfca 2022-01-16 op printf(".: %s\n", errstr);
1298 1bb1bfca 2022-01-16 op free(errstr);
1302 a9288ad9 2022-01-17 op do_open(nfid, KOREAD);
1304 fb1a36c0 2022-01-09 op evbuffer_drain(dirbuf, EVBUFFER_LENGTH(dirbuf));
1307 7bafb2fd 2022-08-28 op tread(nfid, off, msize - IOHDRSZ);
1310 fb1a36c0 2022-01-09 op expect2(Rread, iota_tag);
1312 fb1a36c0 2022-01-09 op len = np_read32(buf);
1313 fb1a36c0 2022-01-09 op if (len == 0)
1316 fb1a36c0 2022-01-09 op evbuffer_add_buffer(dirbuf, buf);
1319 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
1322 fb1a36c0 2022-01-09 op while (EVBUFFER_LENGTH(dirbuf) != 0) {
1323 fb1a36c0 2022-01-09 op if (np_read_stat(dirbuf, &st) == -1)
1324 fb1a36c0 2022-01-09 op errx(1, "invalid stat struct read");
1326 fb1a36c0 2022-01-09 op if (fmt_scaled(st.length, fmt) == -1)
1327 fb1a36c0 2022-01-09 op strlcpy(fmt, "xxx", sizeof(fmt));
1329 4f52d9af 2022-01-16 op mtime = st.mtime;
1331 4f52d9af 2022-01-16 op if (now > mtime && (now - mtime) < 365/2 * 24 * 12 * 60)
1332 4f52d9af 2022-01-16 op timfmt = "%b %e %R";
1334 4f52d9af 2022-01-16 op timfmt = "%b %e %Y";
1336 4f52d9af 2022-01-16 op if ((tm = localtime(&mtime)) == NULL ||
1337 4f52d9af 2022-01-16 op strftime(tim, sizeof(tim), timfmt, tm) == 0)
1338 4f52d9af 2022-01-16 op strlcpy(tim, "unknown", sizeof(tim));
1340 1f5fb843 2022-01-16 op if (st.qid.type & QTDIR)
1341 1f5fb843 2022-01-16 op printf("d");
1343 1f5fb843 2022-01-16 op printf("-");
1344 1f5fb843 2022-01-16 op printf("%s", pp_perm(st.mode >> 6));
1345 1f5fb843 2022-01-16 op printf("%s", pp_perm(st.mode >> 3));
1346 1f5fb843 2022-01-16 op printf("%s", pp_perm(st.mode));
1347 d33c93c4 2022-01-17 op printf(" %8s %12s %s%s\n", fmt, tim, st.name,
1348 6470594e 2022-01-16 op st.qid.type & QTDIR ? "/" : "");
1350 fb1a36c0 2022-01-09 op free(st.name);
1351 fb1a36c0 2022-01-09 op free(st.uid);
1352 fb1a36c0 2022-01-09 op free(st.gid);
1353 fb1a36c0 2022-01-09 op free(st.muid);
1356 a9288ad9 2022-01-17 op do_clunk(nfid);
1360 9e7dd230 2022-01-12 op cmd_page(int argc, const char **argv)
1362 9e7dd230 2022-01-12 op struct qid qid;
1363 02571aa1 2022-01-29 op int nfid, tmpfd, miss, r;
1364 64d8db89 2022-01-16 op char sfn[TMPFSTRLEN], p[PATH_MAX], *name, *errstr;
1365 62f663bb 2022-01-16 op const char *pager;
1367 9e7dd230 2022-01-12 op if (argc != 1) {
1368 9e7dd230 2022-01-12 op puts("usage: page file");
1372 62f663bb 2022-01-16 op if ((pager = getenv("PAGER")) == NULL)
1373 62f663bb 2022-01-16 op pager = "less";
1375 8d8fb849 2022-01-17 op nfid = nextfid();
1376 8f9f99f7 2022-01-16 op errstr = walk_path(pwdfid, nfid, *argv, &miss, &qid);
1377 8f9f99f7 2022-01-16 op if (errstr != NULL) {
1378 8f9f99f7 2022-01-16 op printf("%s: %s\n", *argv, errstr);
1379 8f9f99f7 2022-01-16 op free(errstr);
1383 8f9f99f7 2022-01-16 op if (miss != 0 || qid.type != 0) {
1384 8f9f99f7 2022-01-16 op printf("%s: not a file\n", *argv);
1385 fcd9d510 2022-01-16 op if (miss == 0)
1386 fcd9d510 2022-01-16 op do_clunk(nfid);
1390 64d8db89 2022-01-16 op if ((tmpfd = tmp_file(sfn)) == -1) {
1391 9e7dd230 2022-01-12 op do_clunk(nfid);
1395 9e7dd230 2022-01-12 op strlcpy(p, *argv, sizeof(p));
1396 9e7dd230 2022-01-12 op name = basename(p);
1397 02571aa1 2022-01-29 op if ((r = fetch_fid(nfid, tmpfd, name)) == -1)
1398 02571aa1 2022-01-29 op warn("write %s", sfn);
1399 9e7dd230 2022-01-12 op close(tmpfd);
1400 02571aa1 2022-01-29 op if (r != -1)
1401 02571aa1 2022-01-29 op spawn(pager, sfn, NULL);
1402 9e7dd230 2022-01-12 op unlink(sfn);
1406 1c9ab7cf 2022-01-29 op cmd_pipe(int argc, const char **argv)
1408 1c9ab7cf 2022-01-29 op struct qid qid;
1410 1c9ab7cf 2022-01-29 op int nfid, tmpfd, miss, status;
1411 1c9ab7cf 2022-01-29 op int filedes[2]; /* read end, write end */
1412 1c9ab7cf 2022-01-29 op char *errstr;
1414 1c9ab7cf 2022-01-29 op if (argc < 2) {
1415 1c9ab7cf 2022-01-29 op puts("usage: pipe remote-file cmd [args...]");
1419 1c9ab7cf 2022-01-29 op nfid = nextfid();
1420 1c9ab7cf 2022-01-29 op errstr = walk_path(pwdfid, nfid, *argv, &miss, &qid);
1421 1c9ab7cf 2022-01-29 op if (errstr != NULL) {
1422 1c9ab7cf 2022-01-29 op printf("%s: %s\n", *argv, errstr);
1423 1c9ab7cf 2022-01-29 op free(errstr);
1427 1c9ab7cf 2022-01-29 op if (miss != 0 || qid.type != 0) {
1428 1c9ab7cf 2022-01-29 op printf("%s: not a file\n", *argv);
1429 1c9ab7cf 2022-01-29 op if (miss == 0)
1430 1c9ab7cf 2022-01-29 op do_clunk(nfid);
1434 1c9ab7cf 2022-01-29 op if (pipe(filedes) == -1)
1435 1c9ab7cf 2022-01-29 op err(1, "pipe");
1437 1c9ab7cf 2022-01-29 op switch (pid = vfork()) {
1439 1c9ab7cf 2022-01-29 op err(1, "vfork");
1441 1c9ab7cf 2022-01-29 op close(filedes[1]);
1442 1c9ab7cf 2022-01-29 op if (dup2(filedes[0], 0) == -1)
1443 1c9ab7cf 2022-01-29 op err(1, "dup2");
1444 1c9ab7cf 2022-01-29 op execvp(argv[1], (char *const *)argv + 1);
1445 1c9ab7cf 2022-01-29 op err(1, "execvp");
1448 1c9ab7cf 2022-01-29 op close(filedes[0]);
1449 1c9ab7cf 2022-01-29 op if (fetch_fid(nfid, filedes[1], *argv) == -1)
1450 1c9ab7cf 2022-01-29 op warnx("failed to fetch all the file");
1451 1c9ab7cf 2022-01-29 op close(filedes[1]);
1453 1c9ab7cf 2022-01-29 op waitpid(pid, &status, 0);
1457 01fb39ab 2022-01-16 op cmd_put(int argc, const char **argv)
1459 01fb39ab 2022-01-16 op struct qid qid;
1460 9c48f915 2022-01-16 op const char *l;
1463 01fb39ab 2022-01-16 op if (argc != 1 && argc != 2) {
1464 01fb39ab 2022-01-16 op printf("usage: put local-file [remote-file]\n");
1468 01fb39ab 2022-01-16 op if (argc == 2)
1469 01fb39ab 2022-01-16 op l = argv[1];
1470 01fb39ab 2022-01-16 op else if ((l = strrchr(argv[0], '/')) != NULL)
1471 01fb39ab 2022-01-16 op l++; /* skip / */
1473 01fb39ab 2022-01-16 op l = argv[0];
1475 01fb39ab 2022-01-16 op if ((fd = open(argv[0], O_RDONLY)) == -1) {
1476 01fb39ab 2022-01-16 op warn("%s", argv[0]);
1480 9c48f915 2022-01-16 op woc_file(fd, argv[0], l);
1485 c37e1cfe 2022-01-17 op cmd_rename(int argc, const char **argv)
1487 c37e1cfe 2022-01-17 op struct np_stat st;
1488 c37e1cfe 2022-01-17 op struct qid qid;
1489 c37e1cfe 2022-01-17 op char *errstr;
1490 c37e1cfe 2022-01-17 op int nfid, miss;
1492 c37e1cfe 2022-01-17 op if (argc != 2) {
1493 c37e1cfe 2022-01-17 op puts("usage: rename remote-file new-remote-name");
1497 8d8fb849 2022-01-17 op nfid = nextfid();
1498 c37e1cfe 2022-01-17 op errstr = walk_path(pwdfid, nfid, argv[0], &miss, &qid);
1499 c37e1cfe 2022-01-17 op if (errstr != NULL) {
1500 c37e1cfe 2022-01-17 op printf("%s: %s\n", argv[0], errstr);
1501 c37e1cfe 2022-01-17 op free(errstr);
1505 c37e1cfe 2022-01-17 op if (miss != 0) {
1506 c37e1cfe 2022-01-17 op printf("%s: not such file or directory\n", argv[0]);
1510 c37e1cfe 2022-01-17 op prepare_wstat(&st);
1511 c37e1cfe 2022-01-17 op st.name = (char *)argv[1];
1512 c37e1cfe 2022-01-17 op if ((errstr = do_wstat(nfid, &st)) != NULL) {
1513 c37e1cfe 2022-01-17 op printf("rename: %s\n", errstr);
1514 c37e1cfe 2022-01-17 op free(errstr);
1517 c37e1cfe 2022-01-17 op do_clunk(nfid);
1521 fb1a36c0 2022-01-09 op cmd_verbose(int argc, const char **argv)
1523 fb1a36c0 2022-01-09 op if (argc == 0) {
1524 fb1a36c0 2022-01-09 op log_setverbose(!log_getverbose());
1525 fb1a36c0 2022-01-09 op if (log_getverbose())
1526 fb1a36c0 2022-01-09 op puts("verbose mode enabled");
1528 fb1a36c0 2022-01-09 op puts("verbose mode disabled");
1532 fb1a36c0 2022-01-09 op if (argc != 1)
1535 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "on")) {
1536 fb1a36c0 2022-01-09 op log_setverbose(1);
1537 fb1a36c0 2022-01-09 op puts("verbose mode enabled");
1541 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "off")) {
1542 fb1a36c0 2022-01-09 op log_setverbose(0);
1543 fb1a36c0 2022-01-09 op puts("verbose mode disabled");
1548 fb1a36c0 2022-01-09 op printf("verbose [on | off]\n");
1552 fb1a36c0 2022-01-09 op excmd(int argc, const char **argv)
1554 fb1a36c0 2022-01-09 op struct cmd {
1555 fb1a36c0 2022-01-09 op const char *name;
1556 fb1a36c0 2022-01-09 op void (*fn)(int, const char **);
1557 fb1a36c0 2022-01-09 op } cmds[] = {
1558 fb1a36c0 2022-01-09 op {"bell", cmd_bell},
1559 fb1a36c0 2022-01-09 op {"bye", cmd_bye},
1560 fb1a36c0 2022-01-09 op {"cd", cmd_cd},
1561 9c48f915 2022-01-16 op {"edit", cmd_edit},
1562 fb1a36c0 2022-01-09 op {"get", cmd_get},
1563 5565d021 2022-01-16 op {"hexdump", cmd_hexdump},
1564 fb1a36c0 2022-01-09 op {"lcd", cmd_lcd},
1565 fb1a36c0 2022-01-09 op {"lpwd", cmd_lpwd},
1566 fb1a36c0 2022-01-09 op {"ls", cmd_ls},
1567 9e7dd230 2022-01-12 op {"page", cmd_page},
1568 1c9ab7cf 2022-01-29 op {"pipe", cmd_pipe},
1569 01fb39ab 2022-01-16 op {"put", cmd_put},
1570 fb1a36c0 2022-01-09 op {"quit", cmd_bye},
1571 c37e1cfe 2022-01-17 op {"rename", cmd_rename},
1572 fb1a36c0 2022-01-09 op {"verbose", cmd_verbose},
1576 fb1a36c0 2022-01-09 op if (argc == 0)
1578 fb1a36c0 2022-01-09 op for (i = 0; i < nitems(cmds); ++i) {
1579 fb1a36c0 2022-01-09 op if (!strcmp(cmds[i].name, *argv)) {
1580 fb1a36c0 2022-01-09 op cmds[i].fn(argc-1, argv+1);
1585 fb1a36c0 2022-01-09 op log_warnx("unknown command %s", *argv);
1589 fb1a36c0 2022-01-09 op main(int argc, char **argv)
1593 fb1a36c0 2022-01-09 op log_init(1, LOG_DAEMON);
1594 fb1a36c0 2022-01-09 op log_setverbose(0);
1595 fb1a36c0 2022-01-09 op log_procinit(getprogname());
1597 fb1a36c0 2022-01-09 op while ((ch = getopt(argc, argv, "C:cK:")) != -1) {
1598 fb1a36c0 2022-01-09 op switch (ch) {
1601 fb1a36c0 2022-01-09 op crtpath = optarg;
1608 fb1a36c0 2022-01-09 op keypath = optarg;
1614 fb1a36c0 2022-01-09 op argc -= optind;
1615 fb1a36c0 2022-01-09 op argv += optind;
1617 3847d69e 2022-01-30 op if (argc == 0 || (tls && crtpath == NULL))
1620 1c9ab7cf 2022-01-29 op signal(SIGPIPE, SIG_IGN);
1621 fb1a36c0 2022-01-09 op if (isatty(1)) {
1623 fb1a36c0 2022-01-09 op resized = 1;
1624 fb1a36c0 2022-01-09 op signal(SIGWINCH, tty_resized);
1627 fb1a36c0 2022-01-09 op if ((evb = evbuffer_new()) == NULL)
1628 fb1a36c0 2022-01-09 op fatal("evbuffer_new");
1630 fb1a36c0 2022-01-09 op if ((buf = evbuffer_new()) == NULL)
1631 fb1a36c0 2022-01-09 op fatal("evbuffer_new");
1633 fb1a36c0 2022-01-09 op if ((dirbuf = evbuffer_new()) == NULL)
1634 fb1a36c0 2022-01-09 op fatal("evbuferr_new");
1636 fb1a36c0 2022-01-09 op do_connect(argv[0], argv[1]);
1639 fb1a36c0 2022-01-09 op int argc = 0;
1640 fb1a36c0 2022-01-09 op char *line, *argv[16] = {0}, **ap;
1642 fb1a36c0 2022-01-09 op if ((line = read_line("kamiftp> ")) == NULL)
1645 fb1a36c0 2022-01-09 op for (argc = 0, ap = argv; ap < &argv[15] &&
1646 fb1a36c0 2022-01-09 op (*ap = strsep(&line, " \t")) != NULL;) {
1647 fb1a36c0 2022-01-09 op if (**ap != '\0')
1648 fb1a36c0 2022-01-09 op ap++, argc++;
1650 fb1a36c0 2022-01-09 op excmd(argc, (const char **)argv);
1653 fb1a36c0 2022-01-09 op printf("\a");
1654 fb1a36c0 2022-01-09 op fflush(stdout);
1660 fb1a36c0 2022-01-09 op printf("\n");