Blame


1 fb1a36c0 2022-01-09 op /*
2 dc8c1791 2022-01-10 op * Copyright (c) 2021, 2022 Omar Polo <op@omarpolo.com>
3 fb1a36c0 2022-01-09 op *
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.
7 fb1a36c0 2022-01-09 op *
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.
15 fb1a36c0 2022-01-09 op */
16 fb1a36c0 2022-01-09 op
17 bbcba3ed 2022-01-10 op #include "compat.h"
18 bbcba3ed 2022-01-10 op
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>
24 fb1a36c0 2022-01-09 op
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>
39 fb1a36c0 2022-01-09 op
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>
43 91cfa92a 2022-01-10 op #endif
44 fb1a36c0 2022-01-09 op
45 fb1a36c0 2022-01-09 op #include "kami.h"
46 fb1a36c0 2022-01-09 op #include "utils.h"
47 fb1a36c0 2022-01-09 op #include "log.h"
48 c37e1cfe 2022-01-17 op #include "9pclib.h"
49 fb1a36c0 2022-01-09 op
50 d3e1ab0c 2022-11-24 op #include "kamiftp.h"
51 d3e1ab0c 2022-11-24 op
52 64d8db89 2022-01-16 op #define TMPFSTR "/tmp/kamiftp.XXXXXXXXXX"
53 64d8db89 2022-01-16 op #define TMPFSTRLEN sizeof(TMPFSTR)
54 64d8db89 2022-01-16 op
55 fb1a36c0 2022-01-09 op /* flags */
56 fb1a36c0 2022-01-09 op int tls;
57 fb1a36c0 2022-01-09 op const char *crtpath;
58 fb1a36c0 2022-01-09 op const char *keypath;
59 e22049cc 2022-08-28 op
60 fb1a36c0 2022-01-09 op /* state */
61 8f7253b2 2022-11-22 op FILE *fp;
62 fb1a36c0 2022-01-09 op struct evbuffer *buf;
63 fb1a36c0 2022-01-09 op struct evbuffer *dirbuf;
64 fb1a36c0 2022-01-09 op uint32_t msize;
65 fb1a36c0 2022-01-09 op int bell;
66 d3e1ab0c 2022-11-24 op time_t now;
67 fb1a36c0 2022-01-09 op
68 fb1a36c0 2022-01-09 op volatile sig_atomic_t resized;
69 fb1a36c0 2022-01-09 op int tty_p;
70 fb1a36c0 2022-01-09 op int tty_width;
71 5565d021 2022-01-16 op int xdump;
72 fb1a36c0 2022-01-09 op
73 fb1a36c0 2022-01-09 op struct progress {
74 fb1a36c0 2022-01-09 op uint64_t max;
75 fb1a36c0 2022-01-09 op uint64_t done;
76 fb1a36c0 2022-01-09 op };
77 fb1a36c0 2022-01-09 op
78 fb1a36c0 2022-01-09 op int pwdfid;
79 fb1a36c0 2022-01-09 op
80 fb1a36c0 2022-01-09 op #define ASSERT_EMPTYBUF() assert(EVBUFFER_LENGTH(buf) == 0)
81 91cfa92a 2022-01-10 op
82 fb1a36c0 2022-01-09 op static char *
83 fb1a36c0 2022-01-09 op read_line(const char *prompt)
84 fb1a36c0 2022-01-09 op {
85 fb1a36c0 2022-01-09 op char *line;
86 fb1a36c0 2022-01-09 op
87 fb1a36c0 2022-01-09 op again:
88 fb1a36c0 2022-01-09 op if ((line = readline(prompt)) == NULL)
89 fb1a36c0 2022-01-09 op return NULL;
90 fb1a36c0 2022-01-09 op /* XXX: trim spaces? */
91 fb1a36c0 2022-01-09 op if (*line == '\0') {
92 fb1a36c0 2022-01-09 op free(line);
93 fb1a36c0 2022-01-09 op goto again;
94 fb1a36c0 2022-01-09 op }
95 fb1a36c0 2022-01-09 op
96 fb1a36c0 2022-01-09 op add_history(line);
97 fb1a36c0 2022-01-09 op return line;
98 9e7dd230 2022-01-12 op }
99 9e7dd230 2022-01-12 op
100 9e7dd230 2022-01-12 op static void
101 9e7dd230 2022-01-12 op spawn(const char *argv0, ...)
102 9e7dd230 2022-01-12 op {
103 9e7dd230 2022-01-12 op pid_t pid;
104 9e7dd230 2022-01-12 op size_t i;
105 9e7dd230 2022-01-12 op int status;
106 9e7dd230 2022-01-12 op const char *argv[16], *last;
107 9e7dd230 2022-01-12 op va_list ap;
108 9e7dd230 2022-01-12 op
109 9e7dd230 2022-01-12 op memset(argv, 0, sizeof(argv));
110 9e7dd230 2022-01-12 op
111 9e7dd230 2022-01-12 op va_start(ap, argv0);
112 9e7dd230 2022-01-12 op argv[0] = argv0;
113 9e7dd230 2022-01-12 op for (i = 1; i < nitems(argv); ++i) {
114 9e7dd230 2022-01-12 op last = va_arg(ap, const char *);
115 9e7dd230 2022-01-12 op if (last == NULL)
116 9e7dd230 2022-01-12 op break;
117 9e7dd230 2022-01-12 op argv[i] = last;
118 9e7dd230 2022-01-12 op }
119 9e7dd230 2022-01-12 op va_end(ap);
120 9e7dd230 2022-01-12 op
121 9e7dd230 2022-01-12 op assert(last == NULL);
122 9e7dd230 2022-01-12 op
123 9e7dd230 2022-01-12 op switch (pid = fork()) {
124 9e7dd230 2022-01-12 op case -1:
125 9e7dd230 2022-01-12 op err(1, "fork");
126 9e7dd230 2022-01-12 op case 0: /* child */
127 9e7dd230 2022-01-12 op execvp(argv[0], (char *const *)argv);
128 9e7dd230 2022-01-12 op err(1, "execvp");
129 9e7dd230 2022-01-12 op default:
130 9e7dd230 2022-01-12 op waitpid(pid, &status, 0);
131 9e7dd230 2022-01-12 op }
132 fb1a36c0 2022-01-09 op }
133 fb1a36c0 2022-01-09 op
134 8f7253b2 2022-11-22 op static int
135 8f7253b2 2022-11-22 op stdio_tls_write(void *arg, const char *buf, int len)
136 8f7253b2 2022-11-22 op {
137 8f7253b2 2022-11-22 op struct tls *ctx = arg;
138 8f7253b2 2022-11-22 op ssize_t ret;
139 8f7253b2 2022-11-22 op
140 8f7253b2 2022-11-22 op do {
141 8f7253b2 2022-11-22 op ret = tls_write(ctx, buf, len);
142 8f7253b2 2022-11-22 op } while (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT);
143 8f7253b2 2022-11-22 op
144 8f7253b2 2022-11-22 op if (ret == -1)
145 8f7253b2 2022-11-22 op warn("tls_write: %s", tls_error(ctx));
146 8f7253b2 2022-11-22 op
147 8f7253b2 2022-11-22 op return ret;
148 8f7253b2 2022-11-22 op }
149 8f7253b2 2022-11-22 op
150 8f7253b2 2022-11-22 op static int
151 8f7253b2 2022-11-22 op stdio_tls_read(void *arg, char *buf, int len)
152 8f7253b2 2022-11-22 op {
153 8f7253b2 2022-11-22 op struct tls *ctx = arg;
154 8f7253b2 2022-11-22 op ssize_t ret;
155 8f7253b2 2022-11-22 op
156 8f7253b2 2022-11-22 op do {
157 8f7253b2 2022-11-22 op ret = tls_read(ctx, buf, len);
158 8f7253b2 2022-11-22 op } while (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT);
159 8f7253b2 2022-11-22 op
160 8f7253b2 2022-11-22 op if (ret == -1)
161 8f7253b2 2022-11-22 op warn("tls_read: %s", tls_error(ctx));
162 8f7253b2 2022-11-22 op
163 8f7253b2 2022-11-22 op return ret;
164 8f7253b2 2022-11-22 op }
165 8f7253b2 2022-11-22 op
166 8f7253b2 2022-11-22 op static int
167 8f7253b2 2022-11-22 op stdio_tls_close(void *arg)
168 8f7253b2 2022-11-22 op {
169 8f7253b2 2022-11-22 op struct tls *ctx = arg;
170 8f7253b2 2022-11-22 op int ret;
171 8f7253b2 2022-11-22 op
172 8f7253b2 2022-11-22 op do {
173 8f7253b2 2022-11-22 op ret = tls_close(ctx);
174 8f7253b2 2022-11-22 op } while (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT);
175 8f7253b2 2022-11-22 op
176 8f7253b2 2022-11-22 op return ret;
177 8f7253b2 2022-11-22 op }
178 8f7253b2 2022-11-22 op
179 fb1a36c0 2022-01-09 op static void
180 fb1a36c0 2022-01-09 op tty_resized(int signo)
181 fb1a36c0 2022-01-09 op {
182 fb1a36c0 2022-01-09 op resized = 1;
183 fb1a36c0 2022-01-09 op }
184 fb1a36c0 2022-01-09 op
185 dd647997 2022-11-23 op static __dead void
186 fb1a36c0 2022-01-09 op usage(int ret)
187 fb1a36c0 2022-01-09 op {
188 ab9f44f7 2022-11-23 op fprintf(stderr, "usage: %s [-C cert] [-K key] [-o output] "
189 9f134f93 2022-11-23 op "[9p://][user@]host[:port][/path]\n", getprogname());
190 fb1a36c0 2022-01-09 op fprintf(stderr, "kamid suite version " KAMID_VERSION "\n");
191 fb1a36c0 2022-01-09 op exit(ret);
192 fb1a36c0 2022-01-09 op }
193 fb1a36c0 2022-01-09 op
194 8d8fb849 2022-01-17 op static int
195 8d8fb849 2022-01-17 op nextfid(void)
196 8d8fb849 2022-01-17 op {
197 8d8fb849 2022-01-17 op uint32_t i;
198 8d8fb849 2022-01-17 op
199 8d8fb849 2022-01-17 op for (i = 0; ; ++i) {
200 8d8fb849 2022-01-17 op if (i != pwdfid)
201 8d8fb849 2022-01-17 op return i;
202 8d8fb849 2022-01-17 op }
203 8d8fb849 2022-01-17 op }
204 8d8fb849 2022-01-17 op
205 fb1a36c0 2022-01-09 op static void
206 fb1a36c0 2022-01-09 op do_send(void)
207 fb1a36c0 2022-01-09 op {
208 8f7253b2 2022-11-22 op size_t r;
209 fb1a36c0 2022-01-09 op
210 5565d021 2022-01-16 op if (xdump)
211 d33c93c4 2022-01-17 op hexdump("outgoing message", EVBUFFER_DATA(evb),
212 5565d021 2022-01-16 op EVBUFFER_LENGTH(evb));
213 5565d021 2022-01-16 op
214 fb1a36c0 2022-01-09 op while (EVBUFFER_LENGTH(evb) != 0) {
215 8f7253b2 2022-11-22 op r = fwrite(EVBUFFER_DATA(evb), 1, EVBUFFER_LENGTH(evb), fp);
216 8f7253b2 2022-11-22 op if (r == 0)
217 8f7253b2 2022-11-22 op fatalx("unexpected EOF");
218 fb1a36c0 2022-01-09 op evbuffer_drain(evb, r);
219 fb1a36c0 2022-01-09 op }
220 fb1a36c0 2022-01-09 op }
221 fb1a36c0 2022-01-09 op
222 fb1a36c0 2022-01-09 op static void
223 fb1a36c0 2022-01-09 op mustread(void *d, size_t len)
224 fb1a36c0 2022-01-09 op {
225 8f7253b2 2022-11-22 op size_t r;
226 fb1a36c0 2022-01-09 op
227 8f7253b2 2022-11-22 op r = fread(d, 1, len, fp);
228 8f7253b2 2022-11-22 op if (r != len)
229 8f7253b2 2022-11-22 op errx(1, "unexpected EOF");
230 fb1a36c0 2022-01-09 op }
231 fb1a36c0 2022-01-09 op
232 fb1a36c0 2022-01-09 op static void
233 fb1a36c0 2022-01-09 op recv_msg(void)
234 fb1a36c0 2022-01-09 op {
235 8f7253b2 2022-11-22 op size_t r;
236 fb1a36c0 2022-01-09 op uint32_t len, l;
237 fb1a36c0 2022-01-09 op char tmp[BUFSIZ];
238 fb1a36c0 2022-01-09 op
239 8f7253b2 2022-11-22 op r = fread(&len, 1, sizeof(len), fp);
240 8f7253b2 2022-11-22 op if (r != sizeof(len))
241 8f7253b2 2022-11-22 op errx(1, "unexpected EOF");
242 8f7253b2 2022-11-22 op
243 fb1a36c0 2022-01-09 op len = le32toh(len);
244 fb1a36c0 2022-01-09 op if (len < HEADERSIZE)
245 fb1a36c0 2022-01-09 op errx(1, "read message of invalid length %d", len);
246 fb1a36c0 2022-01-09 op
247 fb1a36c0 2022-01-09 op len -= 4; /* skip the length just read */
248 fb1a36c0 2022-01-09 op
249 fb1a36c0 2022-01-09 op while (len != 0) {
250 fb1a36c0 2022-01-09 op l = MIN(len, sizeof(tmp));
251 8f7253b2 2022-11-22 op
252 8f7253b2 2022-11-22 op r = fread(tmp, 1, l, fp);
253 8f7253b2 2022-11-22 op if (r != l)
254 8f7253b2 2022-11-22 op errx(1, "unexpected EOF");
255 8f7253b2 2022-11-22 op len -= r;
256 8f7253b2 2022-11-22 op evbuffer_add(buf, tmp, r);
257 fb1a36c0 2022-01-09 op }
258 5565d021 2022-01-16 op
259 5565d021 2022-01-16 op if (xdump)
260 5565d021 2022-01-16 op hexdump("incoming packet", EVBUFFER_DATA(buf),
261 5565d021 2022-01-16 op EVBUFFER_LENGTH(buf));
262 fb1a36c0 2022-01-09 op }
263 fb1a36c0 2022-01-09 op
264 fb1a36c0 2022-01-09 op static uint64_t
265 fb1a36c0 2022-01-09 op np_read64(struct evbuffer *buf)
266 fb1a36c0 2022-01-09 op {
267 fb1a36c0 2022-01-09 op uint64_t n;
268 fb1a36c0 2022-01-09 op
269 fb1a36c0 2022-01-09 op evbuffer_remove(buf, &n, sizeof(n));
270 fb1a36c0 2022-01-09 op return le64toh(n);
271 fb1a36c0 2022-01-09 op }
272 fb1a36c0 2022-01-09 op
273 fb1a36c0 2022-01-09 op static uint32_t
274 fb1a36c0 2022-01-09 op np_read32(struct evbuffer *buf)
275 fb1a36c0 2022-01-09 op {
276 fb1a36c0 2022-01-09 op uint32_t n;
277 fb1a36c0 2022-01-09 op
278 fb1a36c0 2022-01-09 op evbuffer_remove(buf, &n, sizeof(n));
279 fb1a36c0 2022-01-09 op return le32toh(n);
280 fb1a36c0 2022-01-09 op }
281 fb1a36c0 2022-01-09 op
282 fb1a36c0 2022-01-09 op static uint16_t
283 fb1a36c0 2022-01-09 op np_read16(struct evbuffer *buf)
284 fb1a36c0 2022-01-09 op {
285 fb1a36c0 2022-01-09 op uint16_t n;
286 fb1a36c0 2022-01-09 op
287 fb1a36c0 2022-01-09 op evbuffer_remove(buf, &n, sizeof(n));
288 fb1a36c0 2022-01-09 op return le16toh(n);
289 fb1a36c0 2022-01-09 op }
290 fb1a36c0 2022-01-09 op
291 fb1a36c0 2022-01-09 op static uint16_t
292 fb1a36c0 2022-01-09 op np_read8(struct evbuffer *buf)
293 fb1a36c0 2022-01-09 op {
294 fb1a36c0 2022-01-09 op uint8_t n;
295 fb1a36c0 2022-01-09 op
296 fb1a36c0 2022-01-09 op evbuffer_remove(buf, &n, sizeof(n));
297 fb1a36c0 2022-01-09 op return n;
298 fb1a36c0 2022-01-09 op }
299 fb1a36c0 2022-01-09 op
300 fb1a36c0 2022-01-09 op static char *
301 fb1a36c0 2022-01-09 op np_readstr(struct evbuffer *buf)
302 fb1a36c0 2022-01-09 op {
303 fb1a36c0 2022-01-09 op uint16_t len;
304 fb1a36c0 2022-01-09 op char *str;
305 fb1a36c0 2022-01-09 op
306 fb1a36c0 2022-01-09 op len = np_read16(buf);
307 fb1a36c0 2022-01-09 op assert(EVBUFFER_LENGTH(buf) >= len);
308 fb1a36c0 2022-01-09 op
309 fb1a36c0 2022-01-09 op if ((str = calloc(1, len+1)) == NULL)
310 fb1a36c0 2022-01-09 op err(1, "calloc");
311 fb1a36c0 2022-01-09 op evbuffer_remove(buf, str, len);
312 fb1a36c0 2022-01-09 op return str;
313 fb1a36c0 2022-01-09 op }
314 fb1a36c0 2022-01-09 op
315 fb1a36c0 2022-01-09 op static void
316 fb1a36c0 2022-01-09 op np_read_qid(struct evbuffer *buf, struct qid *qid)
317 fb1a36c0 2022-01-09 op {
318 fb1a36c0 2022-01-09 op assert(EVBUFFER_LENGTH(buf) >= QIDSIZE);
319 fb1a36c0 2022-01-09 op
320 fb1a36c0 2022-01-09 op qid->type = np_read8(buf);
321 fb1a36c0 2022-01-09 op qid->vers = np_read32(buf);
322 fb1a36c0 2022-01-09 op qid->path = np_read64(buf);
323 fb1a36c0 2022-01-09 op }
324 fb1a36c0 2022-01-09 op
325 fb1a36c0 2022-01-09 op static int
326 fb1a36c0 2022-01-09 op np_read_stat(struct evbuffer *buf, struct np_stat *st)
327 fb1a36c0 2022-01-09 op {
328 fb1a36c0 2022-01-09 op uint16_t size;
329 fb1a36c0 2022-01-09 op
330 fb1a36c0 2022-01-09 op memset(st, 0, sizeof(*st));
331 fb1a36c0 2022-01-09 op
332 fb1a36c0 2022-01-09 op size = np_read16(buf);
333 fb1a36c0 2022-01-09 op if (size > EVBUFFER_LENGTH(buf))
334 fb1a36c0 2022-01-09 op return -1;
335 fb1a36c0 2022-01-09 op
336 fb1a36c0 2022-01-09 op st->type = np_read16(buf);
337 fb1a36c0 2022-01-09 op st->dev = np_read32(buf);
338 fb1a36c0 2022-01-09 op np_read_qid(buf, &st->qid);
339 fb1a36c0 2022-01-09 op st->mode = np_read32(buf);
340 fb1a36c0 2022-01-09 op st->atime = np_read32(buf);
341 fb1a36c0 2022-01-09 op st->mtime = np_read32(buf);
342 fb1a36c0 2022-01-09 op st->length = np_read64(buf);
343 fb1a36c0 2022-01-09 op st->name = np_readstr(buf);
344 fb1a36c0 2022-01-09 op st->uid = np_readstr(buf);
345 fb1a36c0 2022-01-09 op st->gid = np_readstr(buf);
346 fb1a36c0 2022-01-09 op st->muid = np_readstr(buf);
347 fb1a36c0 2022-01-09 op
348 fb1a36c0 2022-01-09 op return 0;
349 fb1a36c0 2022-01-09 op }
350 fb1a36c0 2022-01-09 op
351 fb1a36c0 2022-01-09 op static void
352 fb1a36c0 2022-01-09 op expect(uint8_t type)
353 fb1a36c0 2022-01-09 op {
354 fb1a36c0 2022-01-09 op uint8_t t;
355 fb1a36c0 2022-01-09 op
356 fb1a36c0 2022-01-09 op t = np_read8(buf);
357 fb1a36c0 2022-01-09 op if (t == type)
358 fb1a36c0 2022-01-09 op return;
359 fb1a36c0 2022-01-09 op
360 fb1a36c0 2022-01-09 op if (t == Rerror) {
361 fb1a36c0 2022-01-09 op char *err;
362 fb1a36c0 2022-01-09 op
363 fb1a36c0 2022-01-09 op /* skip tag */
364 fb1a36c0 2022-01-09 op np_read16(buf);
365 fb1a36c0 2022-01-09 op
366 fb1a36c0 2022-01-09 op err = np_readstr(buf);
367 fb1a36c0 2022-01-09 op errx(1, "expected %s, got error %s",
368 fb1a36c0 2022-01-09 op pp_msg_type(type), err);
369 fb1a36c0 2022-01-09 op }
370 fb1a36c0 2022-01-09 op
371 fb1a36c0 2022-01-09 op errx(1, "expected %s, got msg type %s",
372 fb1a36c0 2022-01-09 op pp_msg_type(type), pp_msg_type(t));
373 fb1a36c0 2022-01-09 op }
374 fb1a36c0 2022-01-09 op
375 fb1a36c0 2022-01-09 op static void
376 fb1a36c0 2022-01-09 op expect2(uint8_t type, uint16_t tag)
377 fb1a36c0 2022-01-09 op {
378 fb1a36c0 2022-01-09 op uint16_t t;
379 fb1a36c0 2022-01-09 op
380 fb1a36c0 2022-01-09 op expect(type);
381 fb1a36c0 2022-01-09 op
382 fb1a36c0 2022-01-09 op t = np_read16(buf);
383 fb1a36c0 2022-01-09 op if (t == tag)
384 fb1a36c0 2022-01-09 op return;
385 fb1a36c0 2022-01-09 op
386 fb1a36c0 2022-01-09 op errx(1, "expected tag 0x%x, got 0x%x", tag, t);
387 fb1a36c0 2022-01-09 op }
388 fb1a36c0 2022-01-09 op
389 8f9f99f7 2022-01-16 op static char *
390 8f9f99f7 2022-01-16 op check(uint8_t type, uint16_t tag)
391 8f9f99f7 2022-01-16 op {
392 8f9f99f7 2022-01-16 op uint16_t rtag;
393 8f9f99f7 2022-01-16 op uint8_t rtype;
394 8f9f99f7 2022-01-16 op
395 8f9f99f7 2022-01-16 op rtype = np_read8(buf);
396 8f9f99f7 2022-01-16 op rtag = np_read16(buf);
397 8f9f99f7 2022-01-16 op if (rtype == type) {
398 8f9f99f7 2022-01-16 op if (rtag != tag)
399 8f9f99f7 2022-01-16 op errx(1, "expected tag 0x%x, got 0x%x", tag, rtag);
400 8f9f99f7 2022-01-16 op return NULL;
401 8f9f99f7 2022-01-16 op }
402 8f9f99f7 2022-01-16 op
403 8f9f99f7 2022-01-16 op if (rtype == Rerror)
404 8f9f99f7 2022-01-16 op return np_readstr(buf);
405 8f9f99f7 2022-01-16 op
406 8f9f99f7 2022-01-16 op errx(1, "expected %s, got msg type %s",
407 8f9f99f7 2022-01-16 op pp_msg_type(type), pp_msg_type(rtype));
408 8f9f99f7 2022-01-16 op }
409 8f9f99f7 2022-01-16 op
410 fb1a36c0 2022-01-09 op static void
411 fb1a36c0 2022-01-09 op do_version(void)
412 fb1a36c0 2022-01-09 op {
413 fb1a36c0 2022-01-09 op char *version;
414 fb1a36c0 2022-01-09 op
415 fb1a36c0 2022-01-09 op tversion(VERSION9P, MSIZE9P);
416 fb1a36c0 2022-01-09 op do_send();
417 fb1a36c0 2022-01-09 op recv_msg();
418 fb1a36c0 2022-01-09 op expect2(Rversion, NOTAG);
419 fb1a36c0 2022-01-09 op
420 fb1a36c0 2022-01-09 op msize = np_read32(buf);
421 fb1a36c0 2022-01-09 op version = np_readstr(buf);
422 fb1a36c0 2022-01-09 op
423 315eafe8 2022-05-23 op if (msize > MSIZE9P || msize < 256)
424 fb1a36c0 2022-01-09 op errx(1, "got unexpected msize: %d", msize);
425 fb1a36c0 2022-01-09 op if (strcmp(version, VERSION9P))
426 fb1a36c0 2022-01-09 op errx(1, "unexpected 9p version: %s", version);
427 fb1a36c0 2022-01-09 op
428 fb1a36c0 2022-01-09 op free(version);
429 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
430 fb1a36c0 2022-01-09 op }
431 fb1a36c0 2022-01-09 op
432 fb1a36c0 2022-01-09 op static void
433 14ae3944 2022-11-23 op do_attach(const char *user)
434 fb1a36c0 2022-01-09 op {
435 fb1a36c0 2022-01-09 op struct qid qid;
436 fb1a36c0 2022-01-09 op
437 2c50e0f6 2022-11-23 op tattach(pwdfid, NOFID, user, "/");
438 fb1a36c0 2022-01-09 op do_send();
439 fb1a36c0 2022-01-09 op recv_msg();
440 fb1a36c0 2022-01-09 op expect2(Rattach, iota_tag);
441 fb1a36c0 2022-01-09 op np_read_qid(buf, &qid);
442 fb1a36c0 2022-01-09 op
443 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
444 fb1a36c0 2022-01-09 op }
445 fb1a36c0 2022-01-09 op
446 fb1a36c0 2022-01-09 op static uint32_t
447 fb1a36c0 2022-01-09 op do_open(uint32_t fid, uint8_t mode)
448 fb1a36c0 2022-01-09 op {
449 fb1a36c0 2022-01-09 op struct qid qid;
450 fb1a36c0 2022-01-09 op uint32_t iounit;
451 fb1a36c0 2022-01-09 op
452 fb1a36c0 2022-01-09 op topen(fid, mode);
453 fb1a36c0 2022-01-09 op do_send();
454 fb1a36c0 2022-01-09 op recv_msg();
455 fb1a36c0 2022-01-09 op expect2(Ropen, iota_tag);
456 01fb39ab 2022-01-16 op
457 01fb39ab 2022-01-16 op np_read_qid(buf, &qid);
458 01fb39ab 2022-01-16 op iounit = np_read32(buf);
459 01fb39ab 2022-01-16 op
460 01fb39ab 2022-01-16 op ASSERT_EMPTYBUF();
461 01fb39ab 2022-01-16 op
462 01fb39ab 2022-01-16 op return iounit;
463 01fb39ab 2022-01-16 op }
464 01fb39ab 2022-01-16 op
465 01fb39ab 2022-01-16 op static uint32_t
466 01fb39ab 2022-01-16 op do_create(uint32_t fid, const char *name, uint32_t perm, uint8_t mode)
467 01fb39ab 2022-01-16 op {
468 01fb39ab 2022-01-16 op struct qid qid;
469 01fb39ab 2022-01-16 op uint32_t iounit;
470 fb1a36c0 2022-01-09 op
471 01fb39ab 2022-01-16 op tcreate(fid, name, perm, mode);
472 01fb39ab 2022-01-16 op do_send();
473 01fb39ab 2022-01-16 op recv_msg();
474 01fb39ab 2022-01-16 op expect2(Rcreate, iota_tag);
475 01fb39ab 2022-01-16 op
476 fb1a36c0 2022-01-09 op np_read_qid(buf, &qid);
477 fb1a36c0 2022-01-09 op iounit = np_read32(buf);
478 fb1a36c0 2022-01-09 op
479 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
480 fb1a36c0 2022-01-09 op
481 fb1a36c0 2022-01-09 op return iounit;
482 fb1a36c0 2022-01-09 op }
483 fb1a36c0 2022-01-09 op
484 fb1a36c0 2022-01-09 op static void
485 fb1a36c0 2022-01-09 op do_clunk(uint32_t fid)
486 fb1a36c0 2022-01-09 op {
487 fb1a36c0 2022-01-09 op tclunk(fid);
488 fb1a36c0 2022-01-09 op do_send();
489 fb1a36c0 2022-01-09 op recv_msg();
490 fb1a36c0 2022-01-09 op expect2(Rclunk, iota_tag);
491 fb1a36c0 2022-01-09 op
492 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
493 fb1a36c0 2022-01-09 op }
494 fb1a36c0 2022-01-09 op
495 1bb1bfca 2022-01-16 op static char *
496 fb1a36c0 2022-01-09 op dup_fid(int fid, int nfid)
497 fb1a36c0 2022-01-09 op {
498 fb1a36c0 2022-01-09 op uint16_t nwqid;
499 1bb1bfca 2022-01-16 op char *errstr;
500 fb1a36c0 2022-01-09 op
501 fb1a36c0 2022-01-09 op twalk(fid, nfid, NULL, 0);
502 fb1a36c0 2022-01-09 op do_send();
503 fb1a36c0 2022-01-09 op recv_msg();
504 fb1a36c0 2022-01-09 op
505 1bb1bfca 2022-01-16 op if ((errstr = check(Rwalk, iota_tag)) != NULL)
506 1bb1bfca 2022-01-16 op return errstr;
507 1bb1bfca 2022-01-16 op
508 fb1a36c0 2022-01-09 op nwqid = np_read16(buf);
509 fb1a36c0 2022-01-09 op assert(nwqid == 0);
510 fb1a36c0 2022-01-09 op
511 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
512 1bb1bfca 2022-01-16 op
513 1bb1bfca 2022-01-16 op return NULL;
514 fb1a36c0 2022-01-09 op }
515 fb1a36c0 2022-01-09 op
516 8f9f99f7 2022-01-16 op static char *
517 8f9f99f7 2022-01-16 op walk_path(int fid, int newfid, const char *path, int *missing,
518 8f9f99f7 2022-01-16 op struct qid *qid)
519 fb1a36c0 2022-01-09 op {
520 8f9f99f7 2022-01-16 op char *wnames[MAXWELEM], *p, *t, *errstr;
521 fb1a36c0 2022-01-09 op size_t nwname, i;
522 fb1a36c0 2022-01-09 op uint16_t nwqid;
523 fb1a36c0 2022-01-09 op
524 fb1a36c0 2022-01-09 op if ((p = strdup(path)) == NULL)
525 fb1a36c0 2022-01-09 op err(1, "strdup");
526 fb1a36c0 2022-01-09 op t = p;
527 fb1a36c0 2022-01-09 op
528 fb1a36c0 2022-01-09 op /* strip initial ./ */
529 fb1a36c0 2022-01-09 op if (t[0] == '.' && t[1] == '/')
530 fb1a36c0 2022-01-09 op t += 2;
531 fb1a36c0 2022-01-09 op
532 fb1a36c0 2022-01-09 op for (nwname = 0; nwname < nitems(wnames) &&
533 fb1a36c0 2022-01-09 op (wnames[nwname] = strsep(&t, "/")) != NULL;) {
534 fb1a36c0 2022-01-09 op if (*wnames[nwname] != '\0')
535 fb1a36c0 2022-01-09 op nwname++;
536 fb1a36c0 2022-01-09 op }
537 fb1a36c0 2022-01-09 op
538 fb1a36c0 2022-01-09 op twalk(fid, newfid, (const char **)wnames, nwname);
539 fb1a36c0 2022-01-09 op do_send();
540 fb1a36c0 2022-01-09 op recv_msg();
541 fb1a36c0 2022-01-09 op
542 8f9f99f7 2022-01-16 op *missing = nwname;
543 5fe03b5c 2022-01-21 op if ((errstr = check(Rwalk, iota_tag)) != NULL) {
544 5fe03b5c 2022-01-21 op free(p);
545 8f9f99f7 2022-01-16 op return errstr;
546 5fe03b5c 2022-01-21 op }
547 8f9f99f7 2022-01-16 op
548 fb1a36c0 2022-01-09 op nwqid = np_read16(buf);
549 fb1a36c0 2022-01-09 op assert(nwqid <= nwname);
550 fb1a36c0 2022-01-09 op
551 fb1a36c0 2022-01-09 op /* consume all qids */
552 43d117b1 2022-01-16 op for (i = 0; i < nwqid; ++i)
553 fb1a36c0 2022-01-09 op np_read_qid(buf, qid);
554 fb1a36c0 2022-01-09 op
555 fb1a36c0 2022-01-09 op free(p);
556 fb1a36c0 2022-01-09 op
557 8f9f99f7 2022-01-16 op *missing = nwname - nwqid;
558 8f9f99f7 2022-01-16 op return NULL;
559 fb1a36c0 2022-01-09 op }
560 fb1a36c0 2022-01-09 op
561 fb1a36c0 2022-01-09 op static void
562 fb1a36c0 2022-01-09 op do_stat(int fid, struct np_stat *st)
563 fb1a36c0 2022-01-09 op {
564 fb1a36c0 2022-01-09 op tstat(fid);
565 fb1a36c0 2022-01-09 op do_send();
566 fb1a36c0 2022-01-09 op recv_msg();
567 fb1a36c0 2022-01-09 op expect2(Rstat, iota_tag);
568 fb1a36c0 2022-01-09 op
569 caa64af1 2022-02-02 op /* eat up the first two byte length */
570 caa64af1 2022-02-02 op np_read16(buf);
571 caa64af1 2022-02-02 op
572 fb1a36c0 2022-01-09 op if (np_read_stat(buf, st) == -1)
573 fb1a36c0 2022-01-09 op errx(1, "invalid stat struct read");
574 fb1a36c0 2022-01-09 op
575 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
576 fb1a36c0 2022-01-09 op }
577 c37e1cfe 2022-01-17 op
578 c37e1cfe 2022-01-17 op static char *
579 c37e1cfe 2022-01-17 op do_wstat(int fid, const struct np_stat *st)
580 c37e1cfe 2022-01-17 op {
581 c37e1cfe 2022-01-17 op char *errstr;
582 c37e1cfe 2022-01-17 op
583 c37e1cfe 2022-01-17 op twstat(fid, st);
584 c37e1cfe 2022-01-17 op do_send();
585 c37e1cfe 2022-01-17 op recv_msg();
586 fb1a36c0 2022-01-09 op
587 c37e1cfe 2022-01-17 op if ((errstr = check(Rwstat, iota_tag)) != NULL)
588 c37e1cfe 2022-01-17 op return errstr;
589 c37e1cfe 2022-01-17 op
590 c37e1cfe 2022-01-17 op ASSERT_EMPTYBUF();
591 c37e1cfe 2022-01-17 op
592 c37e1cfe 2022-01-17 op return NULL;
593 c37e1cfe 2022-01-17 op }
594 f99c17f9 2022-11-23 op
595 f99c17f9 2022-11-23 op static char *
596 f99c17f9 2022-11-23 op do_remove(int fid)
597 f99c17f9 2022-11-23 op {
598 f99c17f9 2022-11-23 op char *errstr;
599 f99c17f9 2022-11-23 op
600 f99c17f9 2022-11-23 op tremove(fid);
601 f99c17f9 2022-11-23 op do_send();
602 f99c17f9 2022-11-23 op recv_msg();
603 f99c17f9 2022-11-23 op if ((errstr = check(Rremove, iota_tag)) != NULL)
604 f99c17f9 2022-11-23 op return errstr;
605 c37e1cfe 2022-01-17 op
606 f99c17f9 2022-11-23 op ASSERT_EMPTYBUF();
607 f99c17f9 2022-11-23 op
608 f99c17f9 2022-11-23 op return NULL;
609 f99c17f9 2022-11-23 op }
610 f99c17f9 2022-11-23 op
611 fb1a36c0 2022-01-09 op static size_t
612 fb1a36c0 2022-01-09 op do_read(int fid, uint64_t off, uint32_t count, void *data)
613 fb1a36c0 2022-01-09 op {
614 fb1a36c0 2022-01-09 op uint32_t r;
615 fb1a36c0 2022-01-09 op
616 fb1a36c0 2022-01-09 op tread(fid, off, count);
617 fb1a36c0 2022-01-09 op do_send();
618 fb1a36c0 2022-01-09 op recv_msg();
619 fb1a36c0 2022-01-09 op expect2(Rread, iota_tag);
620 fb1a36c0 2022-01-09 op
621 fb1a36c0 2022-01-09 op r = np_read32(buf);
622 fb1a36c0 2022-01-09 op assert(r == EVBUFFER_LENGTH(buf));
623 fb1a36c0 2022-01-09 op assert(r <= count);
624 fb1a36c0 2022-01-09 op evbuffer_remove(buf, data, r);
625 fb1a36c0 2022-01-09 op
626 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
627 fb1a36c0 2022-01-09 op
628 fb1a36c0 2022-01-09 op return r;
629 fb1a36c0 2022-01-09 op }
630 fb1a36c0 2022-01-09 op
631 01fb39ab 2022-01-16 op static size_t
632 01fb39ab 2022-01-16 op do_write(int fid, uint64_t off, uint32_t count, void *data)
633 01fb39ab 2022-01-16 op {
634 01fb39ab 2022-01-16 op uint32_t r;
635 01fb39ab 2022-01-16 op
636 01fb39ab 2022-01-16 op twrite(fid, off, data, count);
637 01fb39ab 2022-01-16 op do_send();
638 01fb39ab 2022-01-16 op recv_msg();
639 01fb39ab 2022-01-16 op expect2(Rwrite, iota_tag);
640 01fb39ab 2022-01-16 op
641 01fb39ab 2022-01-16 op r = np_read32(buf);
642 01fb39ab 2022-01-16 op assert(r <= count);
643 01fb39ab 2022-01-16 op
644 01fb39ab 2022-01-16 op ASSERT_EMPTYBUF();
645 01fb39ab 2022-01-16 op
646 01fb39ab 2022-01-16 op return r;
647 01fb39ab 2022-01-16 op }
648 01fb39ab 2022-01-16 op
649 fb1a36c0 2022-01-09 op static void
650 fb1a36c0 2022-01-09 op draw_progress(const char *pre, const struct progress *p)
651 fb1a36c0 2022-01-09 op {
652 fb1a36c0 2022-01-09 op struct winsize ws;
653 fb1a36c0 2022-01-09 op int i, l, w;
654 fb1a36c0 2022-01-09 op double perc;
655 5565d021 2022-01-16 op
656 5565d021 2022-01-16 op if (xdump)
657 5565d021 2022-01-16 op return;
658 fb1a36c0 2022-01-09 op
659 fb1a36c0 2022-01-09 op perc = 100.0 * p->done / p->max;
660 fb1a36c0 2022-01-09 op if (!tty_p) {
661 fb1a36c0 2022-01-09 op fprintf(stderr, "%s: %d%%\n", pre, (int)perc);
662 fb1a36c0 2022-01-09 op return;
663 fb1a36c0 2022-01-09 op }
664 fb1a36c0 2022-01-09 op
665 fb1a36c0 2022-01-09 op if (resized) {
666 fb1a36c0 2022-01-09 op resized = 0;
667 fb1a36c0 2022-01-09 op
668 fb1a36c0 2022-01-09 op if (ioctl(0, TIOCGWINSZ, &ws) == -1)
669 fb1a36c0 2022-01-09 op return;
670 fb1a36c0 2022-01-09 op tty_width = ws.ws_col;
671 fb1a36c0 2022-01-09 op }
672 fb1a36c0 2022-01-09 op w = tty_width;
673 fb1a36c0 2022-01-09 op
674 fb1a36c0 2022-01-09 op if (pre == NULL ||
675 5d0dedb3 2022-11-23 op ((l = fprintf(stderr, "\r%s ", pre)) == -1 || l >= w))
676 fb1a36c0 2022-01-09 op return;
677 fb1a36c0 2022-01-09 op
678 fb1a36c0 2022-01-09 op w -= l + 2 + 5; /* 2 for |, 5 for percentage + \n */
679 fb1a36c0 2022-01-09 op if (w < 0) {
680 5d0dedb3 2022-11-23 op fprintf(stderr, "%4d%%\n", (int)perc);
681 fb1a36c0 2022-01-09 op return;
682 fb1a36c0 2022-01-09 op }
683 fb1a36c0 2022-01-09 op
684 5d0dedb3 2022-11-23 op fprintf(stderr, "|");
685 fb1a36c0 2022-01-09 op
686 fb1a36c0 2022-01-09 op l = w * MIN(100.0, perc) / 100.0;
687 fb1a36c0 2022-01-09 op for (i = 0; i < l; i++)
688 5d0dedb3 2022-11-23 op fprintf(stderr, "*");
689 fb1a36c0 2022-01-09 op for (; i < w; i++)
690 5d0dedb3 2022-11-23 op fprintf(stderr, " ");
691 5d0dedb3 2022-11-23 op fprintf(stderr, "|%4d%%", (int)perc);
692 fb1a36c0 2022-01-09 op }
693 fb1a36c0 2022-01-09 op
694 02571aa1 2022-01-29 op static int
695 76521d26 2022-01-16 op fetch_fid(int fid, int fd, const char *name)
696 fb1a36c0 2022-01-09 op {
697 02d5b425 2022-05-23 op static char buf[MSIZE9P];
698 fb1a36c0 2022-01-09 op struct progress p = {0};
699 fb1a36c0 2022-01-09 op struct np_stat st;
700 fb1a36c0 2022-01-09 op size_t r;
701 02571aa1 2022-01-29 op int ret = 0;
702 fb1a36c0 2022-01-09 op
703 fb1a36c0 2022-01-09 op do_stat(fid, &st);
704 fb1a36c0 2022-01-09 op do_open(fid, KOREAD);
705 fb1a36c0 2022-01-09 op
706 fb1a36c0 2022-01-09 op p.max = st.length;
707 fb1a36c0 2022-01-09 op for (;;) {
708 02d5b425 2022-05-23 op size_t len, off;
709 fb1a36c0 2022-01-09 op ssize_t nw;
710 fb1a36c0 2022-01-09 op
711 02d5b425 2022-05-23 op len = MIN(sizeof(buf), msize);
712 7bafb2fd 2022-08-28 op len -= IOHDRSZ; /* for the request' fields */
713 02d5b425 2022-05-23 op
714 02d5b425 2022-05-23 op r = do_read(fid, p.done, len, buf);
715 fb1a36c0 2022-01-09 op if (r == 0)
716 fb1a36c0 2022-01-09 op break;
717 fb1a36c0 2022-01-09 op
718 b2263c45 2022-01-16 op for (off = 0; off < r; off += nw)
719 b2263c45 2022-01-16 op if ((nw = write(fd, buf + off, r - off)) == 0 ||
720 02571aa1 2022-01-29 op nw == -1) {
721 02571aa1 2022-01-29 op ret = -1;
722 02571aa1 2022-01-29 op goto end;
723 02571aa1 2022-01-29 op }
724 fb1a36c0 2022-01-09 op
725 fb1a36c0 2022-01-09 op p.done += r;
726 9e7dd230 2022-01-12 op draw_progress(name, &p);
727 fb1a36c0 2022-01-09 op
728 fb1a36c0 2022-01-09 op #if 0
729 fb1a36c0 2022-01-09 op /* throttle, for debugging purpose */
730 fb1a36c0 2022-01-09 op {
731 fb1a36c0 2022-01-09 op struct timespec ts = { 0, 500000000 };
732 fb1a36c0 2022-01-09 op nanosleep(&ts, NULL);
733 fb1a36c0 2022-01-09 op }
734 fb1a36c0 2022-01-09 op #endif
735 fb1a36c0 2022-01-09 op }
736 fb1a36c0 2022-01-09 op
737 02571aa1 2022-01-29 op end:
738 fb1a36c0 2022-01-09 op putchar('\n');
739 01fb39ab 2022-01-16 op
740 01fb39ab 2022-01-16 op do_clunk(fid);
741 02571aa1 2022-01-29 op return ret;
742 01fb39ab 2022-01-16 op }
743 01fb39ab 2022-01-16 op
744 01fb39ab 2022-01-16 op static void
745 16f1f3bf 2022-01-21 op send_fid(int fid, const char *fnam, int open_flags, int fd, const char *name)
746 01fb39ab 2022-01-16 op {
747 02d5b425 2022-05-23 op static char buf[MSIZE9P];
748 01fb39ab 2022-01-16 op struct progress p = {0};
749 01fb39ab 2022-01-16 op struct stat sb;
750 01fb39ab 2022-01-16 op ssize_t r;
751 02d5b425 2022-05-23 op size_t w, len;
752 01fb39ab 2022-01-16 op
753 01fb39ab 2022-01-16 op if (fstat(fd, &sb) == -1)
754 01fb39ab 2022-01-16 op err(1, "fstat");
755 01fb39ab 2022-01-16 op
756 01fb39ab 2022-01-16 op if (fnam != NULL)
757 01fb39ab 2022-01-16 op do_create(fid, fnam, 0644, KOWRITE);
758 01fb39ab 2022-01-16 op else
759 16f1f3bf 2022-01-21 op do_open(fid, open_flags | KOWRITE);
760 01fb39ab 2022-01-16 op
761 01fb39ab 2022-01-16 op p.max = sb.st_size;
762 01fb39ab 2022-01-16 op for (;;) {
763 02d5b425 2022-05-23 op len = MIN(sizeof(buf), msize);
764 02d5b425 2022-05-23 op len -= HEADERSIZE + 4 + 4 + 8; /* for the request' fields */
765 02d5b425 2022-05-23 op
766 02d5b425 2022-05-23 op r = read(fd, buf, len);
767 01fb39ab 2022-01-16 op if (r == 0)
768 01fb39ab 2022-01-16 op break;
769 01fb39ab 2022-01-16 op if (r == -1)
770 01fb39ab 2022-01-16 op err(1, "read");
771 9e7dd230 2022-01-12 op
772 01fb39ab 2022-01-16 op w = do_write(fid, p.done, r, buf);
773 01fb39ab 2022-01-16 op p.done += w;
774 01fb39ab 2022-01-16 op
775 01fb39ab 2022-01-16 op draw_progress(name, &p);
776 01fb39ab 2022-01-16 op
777 01fb39ab 2022-01-16 op #if 0
778 01fb39ab 2022-01-16 op /* throttle, for debugging purpose */
779 01fb39ab 2022-01-16 op {
780 01fb39ab 2022-01-16 op struct timespec ts = { 0, 500000000 };
781 01fb39ab 2022-01-16 op nanosleep(&ts, NULL);
782 01fb39ab 2022-01-16 op }
783 01fb39ab 2022-01-16 op #endif
784 01fb39ab 2022-01-16 op }
785 01fb39ab 2022-01-16 op
786 01fb39ab 2022-01-16 op putchar('\n');
787 fb1a36c0 2022-01-09 op do_clunk(fid);
788 9c48f915 2022-01-16 op }
789 9c48f915 2022-01-16 op
790 9c48f915 2022-01-16 op static int
791 9c48f915 2022-01-16 op woc_file(int fd, const char *prompt, const char *path)
792 9c48f915 2022-01-16 op {
793 9c48f915 2022-01-16 op struct qid qid;
794 9c48f915 2022-01-16 op const char *n = NULL;
795 9c48f915 2022-01-16 op char *errstr;
796 9c48f915 2022-01-16 op int nfid, miss;
797 9c48f915 2022-01-16 op
798 8d8fb849 2022-01-17 op nfid = nextfid();
799 9c48f915 2022-01-16 op errstr = walk_path(pwdfid, nfid, path, &miss, &qid);
800 9c48f915 2022-01-16 op if (errstr != NULL && miss > 1) {
801 5d0dedb3 2022-11-23 op fprintf(stderr, "%s: %s\n", path, errstr);
802 9c48f915 2022-01-16 op free(errstr);
803 9c48f915 2022-01-16 op return -1;
804 9c48f915 2022-01-16 op }
805 9c48f915 2022-01-16 op
806 9c48f915 2022-01-16 op if (errstr != NULL || miss == 1) {
807 9c48f915 2022-01-16 op char p[PATH_MAX], *dn;
808 9c48f915 2022-01-16 op
809 9c48f915 2022-01-16 op /*
810 9c48f915 2022-01-16 op * If it's only one component missing (the file name), walk
811 9c48f915 2022-01-16 op * to the parent directory and try to create the file.
812 9c48f915 2022-01-16 op */
813 9c48f915 2022-01-16 op
814 9c48f915 2022-01-16 op if (strlcpy(p, path, sizeof(p)) >= sizeof(p)) {
815 5d0dedb3 2022-11-23 op fprintf(stderr, "path too long: %s\n", path);
816 9c48f915 2022-01-16 op return -1;
817 9c48f915 2022-01-16 op }
818 9c48f915 2022-01-16 op dn = dirname(p);
819 9c48f915 2022-01-16 op
820 9c48f915 2022-01-16 op if (!strcmp(dn, ".")) {
821 9c48f915 2022-01-16 op errstr = dup_fid(pwdfid, nfid);
822 9c48f915 2022-01-16 op miss = 0;
823 9c48f915 2022-01-16 op } else
824 9c48f915 2022-01-16 op errstr = walk_path(pwdfid, nfid, dn, &miss, &qid);
825 9c48f915 2022-01-16 op
826 9c48f915 2022-01-16 op if (errstr != NULL) {
827 5d0dedb3 2022-11-23 op fprintf(stderr, "%s: %s\n", dn, errstr);
828 9c48f915 2022-01-16 op free(errstr);
829 9c48f915 2022-01-16 op return -1;
830 9c48f915 2022-01-16 op }
831 9c48f915 2022-01-16 op
832 9c48f915 2022-01-16 op if (miss != 0) {
833 5d0dedb3 2022-11-23 op fprintf(stderr, "%s: not a directory\n", dn);
834 9c48f915 2022-01-16 op return -1;
835 9c48f915 2022-01-16 op }
836 9c48f915 2022-01-16 op
837 9c48f915 2022-01-16 op if ((n = strrchr(path, '/')) != NULL)
838 9c48f915 2022-01-16 op n++;
839 9c48f915 2022-01-16 op else
840 9c48f915 2022-01-16 op n = path;
841 9c48f915 2022-01-16 op }
842 9c48f915 2022-01-16 op
843 9c48f915 2022-01-16 op free(errstr);
844 9c48f915 2022-01-16 op
845 9c48f915 2022-01-16 op if (miss > 1) {
846 5d0dedb3 2022-11-23 op fprintf(stderr, "can't create %s: missing %d path"
847 5d0dedb3 2022-11-23 op " component(s)\n", path, miss);
848 9c48f915 2022-01-16 op return -1;
849 9c48f915 2022-01-16 op }
850 9c48f915 2022-01-16 op
851 16f1f3bf 2022-01-21 op send_fid(nfid, n, KOTRUNC, fd, prompt);
852 9c48f915 2022-01-16 op return 0;
853 fb1a36c0 2022-01-09 op }
854 fb1a36c0 2022-01-09 op
855 8f7253b2 2022-11-22 op static int
856 8f7253b2 2022-11-22 op dial(const char *host, const char *port)
857 fb1a36c0 2022-01-09 op {
858 fb1a36c0 2022-01-09 op struct addrinfo hints, *res, *res0;
859 8f7253b2 2022-11-22 op int sock, error, saved_errno;
860 fb1a36c0 2022-01-09 op const char *cause = NULL;
861 fb1a36c0 2022-01-09 op
862 fb1a36c0 2022-01-09 op memset(&hints, 0, sizeof(hints));
863 fb1a36c0 2022-01-09 op hints.ai_family = AF_UNSPEC;
864 fb1a36c0 2022-01-09 op hints.ai_socktype = SOCK_STREAM;
865 fb1a36c0 2022-01-09 op error = getaddrinfo(host, port, &hints, &res0);
866 fb1a36c0 2022-01-09 op if (error)
867 fb1a36c0 2022-01-09 op errx(1, "%s", gai_strerror(error));
868 fb1a36c0 2022-01-09 op
869 fb1a36c0 2022-01-09 op sock = -1;
870 fb1a36c0 2022-01-09 op for (res = res0; res != NULL; res = res->ai_next) {
871 db066f1e 2022-01-12 op sock = socket(res->ai_family, res->ai_socktype|SOCK_CLOEXEC,
872 fb1a36c0 2022-01-09 op res->ai_protocol);
873 fb1a36c0 2022-01-09 op if (sock == -1) {
874 fb1a36c0 2022-01-09 op cause = "socket";
875 fb1a36c0 2022-01-09 op continue;
876 fb1a36c0 2022-01-09 op }
877 fb1a36c0 2022-01-09 op
878 fb1a36c0 2022-01-09 op if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
879 fb1a36c0 2022-01-09 op cause = "connect";
880 fb1a36c0 2022-01-09 op saved_errno = errno;
881 fb1a36c0 2022-01-09 op close(sock);
882 fb1a36c0 2022-01-09 op errno = saved_errno;
883 fb1a36c0 2022-01-09 op sock = -1;
884 fb1a36c0 2022-01-09 op continue;
885 fb1a36c0 2022-01-09 op }
886 fb1a36c0 2022-01-09 op
887 fb1a36c0 2022-01-09 op break;
888 fb1a36c0 2022-01-09 op }
889 8f7253b2 2022-11-22 op freeaddrinfo(res0);
890 fb1a36c0 2022-01-09 op if (sock == -1)
891 fb1a36c0 2022-01-09 op err(1, "%s", cause);
892 8f7253b2 2022-11-22 op return sock;
893 fb1a36c0 2022-01-09 op }
894 fb1a36c0 2022-01-09 op
895 fb1a36c0 2022-01-09 op static void
896 14ae3944 2022-11-23 op do_connect(const char *host, const char *port, const char *user)
897 fb1a36c0 2022-01-09 op {
898 14ae3944 2022-11-23 op struct qid qid;
899 14ae3944 2022-11-23 op int nfid, miss, fd;
900 14ae3944 2022-11-23 op char *errstr;
901 fb1a36c0 2022-01-09 op
902 5d0dedb3 2022-11-23 op fprintf(stderr, "connecting to %s:%s...", host, port);
903 fb1a36c0 2022-01-09 op
904 8f7253b2 2022-11-22 op if (tls) {
905 8f7253b2 2022-11-22 op struct tls_config *conf;
906 8f7253b2 2022-11-22 op struct tls *ctx;
907 8f7253b2 2022-11-22 op int r;
908 fb1a36c0 2022-01-09 op
909 8f7253b2 2022-11-22 op if ((conf = tls_config_new()) == NULL)
910 8f7253b2 2022-11-22 op fatalx("failed to create TLS config");
911 8f7253b2 2022-11-22 op tls_config_insecure_noverifycert(conf);
912 8f7253b2 2022-11-22 op tls_config_insecure_noverifyname(conf);
913 8f7253b2 2022-11-22 op
914 8f7253b2 2022-11-22 op if (keypath == NULL)
915 8f7253b2 2022-11-22 op keypath = crtpath;
916 8f7253b2 2022-11-22 op
917 8f7253b2 2022-11-22 op if (tls_config_set_keypair_file(conf, crtpath, keypath) == -1)
918 8f7253b2 2022-11-22 op fatalx("failed to load certs: (%s, %s)", crtpath,
919 8f7253b2 2022-11-22 op keypath);
920 8f7253b2 2022-11-22 op
921 8f7253b2 2022-11-22 op if ((ctx = tls_client()) == NULL)
922 8f7253b2 2022-11-22 op fatalx("failed to create TLS client");
923 8f7253b2 2022-11-22 op if (tls_configure(ctx, conf) == -1)
924 8f7253b2 2022-11-22 op fatalx("failed to configure TLS client");
925 8f7253b2 2022-11-22 op tls_config_free(conf);
926 8f7253b2 2022-11-22 op
927 8f7253b2 2022-11-22 op if (tls_connect(ctx, host, port) == -1)
928 8f7253b2 2022-11-22 op fatalx("failed to connect to %s:%s: %s", host, port,
929 8f7253b2 2022-11-22 op tls_error(ctx));
930 8f7253b2 2022-11-22 op
931 8f7253b2 2022-11-22 op do {
932 8f7253b2 2022-11-22 op r = tls_handshake(ctx);
933 8f7253b2 2022-11-22 op } while (r == TLS_WANT_POLLIN || r == TLS_WANT_POLLOUT);
934 8f7253b2 2022-11-22 op fp = funopen(ctx, stdio_tls_read, stdio_tls_write, NULL,
935 8f7253b2 2022-11-22 op stdio_tls_close);
936 8f7253b2 2022-11-22 op if (fp == NULL)
937 8f7253b2 2022-11-22 op fatal("funopen");
938 8f7253b2 2022-11-22 op } else {
939 8f7253b2 2022-11-22 op int fd;
940 8f7253b2 2022-11-22 op
941 8f7253b2 2022-11-22 op fd = dial(host, port);
942 8f7253b2 2022-11-22 op if ((fp = fdopen(fd, "r+")) == NULL)
943 8f7253b2 2022-11-22 op fatal("fdopen");
944 8f7253b2 2022-11-22 op }
945 8f7253b2 2022-11-22 op
946 5d0dedb3 2022-11-23 op fprintf(stderr, " done!\n");
947 fb1a36c0 2022-01-09 op
948 fb1a36c0 2022-01-09 op do_version();
949 14ae3944 2022-11-23 op do_attach(user);
950 fb1a36c0 2022-01-09 op }
951 fb1a36c0 2022-01-09 op
952 64d8db89 2022-01-16 op static int
953 64d8db89 2022-01-16 op tmp_file(char sfn[TMPFSTRLEN])
954 64d8db89 2022-01-16 op {
955 64d8db89 2022-01-16 op int tmpfd;
956 64d8db89 2022-01-16 op
957 64d8db89 2022-01-16 op strlcpy(sfn, TMPFSTR, TMPFSTRLEN);
958 64d8db89 2022-01-16 op if ((tmpfd = mkstemp(sfn)) == -1) {
959 64d8db89 2022-01-16 op warn("mkstemp %s", sfn);
960 64d8db89 2022-01-16 op return -1;
961 64d8db89 2022-01-16 op }
962 64d8db89 2022-01-16 op
963 aa76fd7f 2022-01-16 op /* set the close-on-exec flag */
964 aa76fd7f 2022-01-16 op if (fcntl(tmpfd, F_SETFD, FD_CLOEXEC) == -1) {
965 aa76fd7f 2022-01-16 op warn("fcntl");
966 aa76fd7f 2022-01-16 op close(tmpfd);
967 aa76fd7f 2022-01-16 op return -1;
968 aa76fd7f 2022-01-16 op }
969 aa76fd7f 2022-01-16 op
970 64d8db89 2022-01-16 op return tmpfd;
971 64d8db89 2022-01-16 op }
972 64d8db89 2022-01-16 op
973 1f5fb843 2022-01-16 op static inline const char *
974 1f5fb843 2022-01-16 op pp_perm(uint8_t x)
975 1f5fb843 2022-01-16 op {
976 1f5fb843 2022-01-16 op switch (x & 0x7) {
977 1f5fb843 2022-01-16 op case 0x0:
978 1f5fb843 2022-01-16 op return "---";
979 1f5fb843 2022-01-16 op case 0x1:
980 1f5fb843 2022-01-16 op return "--x";
981 1f5fb843 2022-01-16 op case 0x2:
982 1f5fb843 2022-01-16 op return "-w-";
983 1f5fb843 2022-01-16 op case 0x3:
984 1f5fb843 2022-01-16 op return "-wx";
985 1f5fb843 2022-01-16 op case 0x4:
986 1f5fb843 2022-01-16 op return "r--";
987 1f5fb843 2022-01-16 op case 0x5:
988 1f5fb843 2022-01-16 op return "r-x";
989 1f5fb843 2022-01-16 op case 0x6:
990 1f5fb843 2022-01-16 op return "rw-";
991 1f5fb843 2022-01-16 op case 0x7:
992 1f5fb843 2022-01-16 op return "rwx";
993 1f5fb843 2022-01-16 op default:
994 1f5fb843 2022-01-16 op /* unreachable, just for the compiler' happiness */
995 1f5fb843 2022-01-16 op return "???";
996 1f5fb843 2022-01-16 op }
997 c37e1cfe 2022-01-17 op }
998 c37e1cfe 2022-01-17 op
999 c37e1cfe 2022-01-17 op static inline void
1000 c37e1cfe 2022-01-17 op prepare_wstat(struct np_stat *st)
1001 c37e1cfe 2022-01-17 op {
1002 c37e1cfe 2022-01-17 op memset(st, 0xFF, sizeof(*st));
1003 c37e1cfe 2022-01-17 op st->name = NULL;
1004 c37e1cfe 2022-01-17 op st->uid = NULL;
1005 c37e1cfe 2022-01-17 op st->gid = NULL;
1006 c37e1cfe 2022-01-17 op st->muid = NULL;
1007 1f5fb843 2022-01-16 op }
1008 1f5fb843 2022-01-16 op
1009 d3e1ab0c 2022-11-24 op static int
1010 d3e1ab0c 2022-11-24 op print_dirent(const struct np_stat *st)
1011 d3e1ab0c 2022-11-24 op {
1012 d3e1ab0c 2022-11-24 op time_t mtime;
1013 d3e1ab0c 2022-11-24 op struct tm *tm;
1014 d3e1ab0c 2022-11-24 op const char *timfmt;
1015 d3e1ab0c 2022-11-24 op char fmt[FMT_SCALED_STRSIZE], tim[13];
1016 d3e1ab0c 2022-11-24 op
1017 d3e1ab0c 2022-11-24 op if (fmt_scaled(st->length, fmt) == -1)
1018 d3e1ab0c 2022-11-24 op strlcpy(fmt, "xxx", sizeof(fmt));
1019 d3e1ab0c 2022-11-24 op
1020 d3e1ab0c 2022-11-24 op mtime = st->mtime;
1021 d3e1ab0c 2022-11-24 op
1022 d3e1ab0c 2022-11-24 op if (now > mtime && (now - mtime) < 365/2 * 24 * 12 * 60)
1023 d3e1ab0c 2022-11-24 op timfmt = "%b %e %R";
1024 d3e1ab0c 2022-11-24 op else
1025 d3e1ab0c 2022-11-24 op timfmt = "%b %e %Y";
1026 d3e1ab0c 2022-11-24 op
1027 d3e1ab0c 2022-11-24 op if ((tm = localtime(&mtime)) == NULL ||
1028 d3e1ab0c 2022-11-24 op strftime(tim, sizeof(tim), timfmt, tm) == 0)
1029 d3e1ab0c 2022-11-24 op strlcpy(tim, "unknown", sizeof(tim));
1030 d3e1ab0c 2022-11-24 op
1031 d3e1ab0c 2022-11-24 op if (st->qid.type & QTDIR)
1032 d3e1ab0c 2022-11-24 op printf("d");
1033 d3e1ab0c 2022-11-24 op else
1034 d3e1ab0c 2022-11-24 op printf("-");
1035 d3e1ab0c 2022-11-24 op printf("%s", pp_perm(st->mode >> 6));
1036 d3e1ab0c 2022-11-24 op printf("%s", pp_perm(st->mode >> 3));
1037 d3e1ab0c 2022-11-24 op printf("%s", pp_perm(st->mode));
1038 d3e1ab0c 2022-11-24 op printf(" %8s %12s %s%s\n", fmt, tim, st->name,
1039 d3e1ab0c 2022-11-24 op st->qid.type & QTDIR ? "/" : "");
1040 d3e1ab0c 2022-11-24 op
1041 d3e1ab0c 2022-11-24 op return 0;
1042 d3e1ab0c 2022-11-24 op }
1043 d3e1ab0c 2022-11-24 op
1044 d3e1ab0c 2022-11-24 op int
1045 d3e1ab0c 2022-11-24 op dir_listing(const char *path, int (*fn)(const struct np_stat *),
1046 d3e1ab0c 2022-11-24 op int printerr)
1047 d3e1ab0c 2022-11-24 op {
1048 d3e1ab0c 2022-11-24 op struct qid qid;
1049 d3e1ab0c 2022-11-24 op struct np_stat st;
1050 d3e1ab0c 2022-11-24 op uint64_t off = 0;
1051 d3e1ab0c 2022-11-24 op uint32_t len;
1052 d3e1ab0c 2022-11-24 op int nfid, miss, r;
1053 d3e1ab0c 2022-11-24 op char *errstr;
1054 d3e1ab0c 2022-11-24 op
1055 d3e1ab0c 2022-11-24 op now = time(NULL);
1056 d3e1ab0c 2022-11-24 op nfid = nextfid();
1057 d3e1ab0c 2022-11-24 op
1058 d3e1ab0c 2022-11-24 op errstr = walk_path(pwdfid, nfid, path, &miss, &qid);
1059 d3e1ab0c 2022-11-24 op if (errstr != NULL) {
1060 d3e1ab0c 2022-11-24 op if (printerr)
1061 d3e1ab0c 2022-11-24 op printf("%s: %s\n", path, errstr);
1062 d3e1ab0c 2022-11-24 op free(errstr);
1063 d3e1ab0c 2022-11-24 op return -1;
1064 d3e1ab0c 2022-11-24 op }
1065 d3e1ab0c 2022-11-24 op if (miss) {
1066 d3e1ab0c 2022-11-24 op if (printerr)
1067 d3e1ab0c 2022-11-24 op printf("%s: No such file or directory\n", path);
1068 d3e1ab0c 2022-11-24 op return -1;
1069 d3e1ab0c 2022-11-24 op }
1070 d3e1ab0c 2022-11-24 op if (!(qid.type & QTDIR)) {
1071 d3e1ab0c 2022-11-24 op if (printerr)
1072 d3e1ab0c 2022-11-24 op printf("%s: not a directory\n", path);
1073 d3e1ab0c 2022-11-24 op do_clunk(nfid);
1074 d3e1ab0c 2022-11-24 op return -1;
1075 d3e1ab0c 2022-11-24 op }
1076 d3e1ab0c 2022-11-24 op
1077 d3e1ab0c 2022-11-24 op do_open(nfid, KOREAD);
1078 d3e1ab0c 2022-11-24 op evbuffer_drain(dirbuf, EVBUFFER_LENGTH(dirbuf));
1079 d3e1ab0c 2022-11-24 op
1080 d3e1ab0c 2022-11-24 op for (;;) {
1081 d3e1ab0c 2022-11-24 op tread(nfid, off, msize - IOHDRSZ);
1082 d3e1ab0c 2022-11-24 op do_send();
1083 d3e1ab0c 2022-11-24 op recv_msg();
1084 d3e1ab0c 2022-11-24 op expect2(Rread, iota_tag);
1085 d3e1ab0c 2022-11-24 op
1086 d3e1ab0c 2022-11-24 op len = np_read32(buf);
1087 d3e1ab0c 2022-11-24 op if (len == 0)
1088 d3e1ab0c 2022-11-24 op break;
1089 d3e1ab0c 2022-11-24 op
1090 d3e1ab0c 2022-11-24 op evbuffer_add_buffer(dirbuf, buf);
1091 d3e1ab0c 2022-11-24 op off += len;
1092 d3e1ab0c 2022-11-24 op
1093 d3e1ab0c 2022-11-24 op ASSERT_EMPTYBUF();
1094 d3e1ab0c 2022-11-24 op }
1095 d3e1ab0c 2022-11-24 op
1096 d3e1ab0c 2022-11-24 op while (EVBUFFER_LENGTH(dirbuf) != 0) {
1097 d3e1ab0c 2022-11-24 op if (np_read_stat(dirbuf, &st) == -1)
1098 d3e1ab0c 2022-11-24 op errx(1, "invalid stat struct read");
1099 d3e1ab0c 2022-11-24 op
1100 d3e1ab0c 2022-11-24 op r = fn(&st);
1101 d3e1ab0c 2022-11-24 op
1102 d3e1ab0c 2022-11-24 op free(st.name);
1103 d3e1ab0c 2022-11-24 op free(st.uid);
1104 d3e1ab0c 2022-11-24 op free(st.gid);
1105 d3e1ab0c 2022-11-24 op free(st.muid);
1106 d3e1ab0c 2022-11-24 op
1107 d3e1ab0c 2022-11-24 op if (r == -1)
1108 d3e1ab0c 2022-11-24 op break;
1109 d3e1ab0c 2022-11-24 op }
1110 d3e1ab0c 2022-11-24 op
1111 d3e1ab0c 2022-11-24 op evbuffer_drain(dirbuf, EVBUFFER_LENGTH(dirbuf));
1112 d3e1ab0c 2022-11-24 op do_clunk(nfid);
1113 d3e1ab0c 2022-11-24 op return 0;
1114 d3e1ab0c 2022-11-24 op }
1115 d3e1ab0c 2022-11-24 op
1116 d3e1ab0c 2022-11-24 op void
1117 fb1a36c0 2022-01-09 op cmd_bell(int argc, const char **argv)
1118 fb1a36c0 2022-01-09 op {
1119 fb1a36c0 2022-01-09 op if (argc == 0) {
1120 fb1a36c0 2022-01-09 op bell = !bell;
1121 fb1a36c0 2022-01-09 op if (bell)
1122 fb1a36c0 2022-01-09 op puts("bell mode enabled");
1123 fb1a36c0 2022-01-09 op else
1124 fb1a36c0 2022-01-09 op puts("bell mode disabled");
1125 fb1a36c0 2022-01-09 op return;
1126 fb1a36c0 2022-01-09 op }
1127 fb1a36c0 2022-01-09 op
1128 fb1a36c0 2022-01-09 op if (argc != 1)
1129 fb1a36c0 2022-01-09 op goto usage;
1130 fb1a36c0 2022-01-09 op
1131 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "on")) {
1132 fb1a36c0 2022-01-09 op bell = 1;
1133 fb1a36c0 2022-01-09 op puts("bell mode enabled");
1134 fb1a36c0 2022-01-09 op return;
1135 fb1a36c0 2022-01-09 op }
1136 fb1a36c0 2022-01-09 op
1137 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "off")) {
1138 fb1a36c0 2022-01-09 op bell = 0;
1139 fb1a36c0 2022-01-09 op puts("bell mode disabled");
1140 fb1a36c0 2022-01-09 op return;
1141 fb1a36c0 2022-01-09 op }
1142 fb1a36c0 2022-01-09 op
1143 fb1a36c0 2022-01-09 op usage:
1144 fb1a36c0 2022-01-09 op printf("bell [on | off]\n");
1145 fb1a36c0 2022-01-09 op }
1146 fb1a36c0 2022-01-09 op
1147 d3e1ab0c 2022-11-24 op void
1148 fb1a36c0 2022-01-09 op cmd_bye(int argc, const char **argv)
1149 fb1a36c0 2022-01-09 op {
1150 fb1a36c0 2022-01-09 op log_warnx("bye\n");
1151 8f7253b2 2022-11-22 op fclose(fp);
1152 fb1a36c0 2022-01-09 op exit(0);
1153 fb1a36c0 2022-01-09 op }
1154 fb1a36c0 2022-01-09 op
1155 d3e1ab0c 2022-11-24 op void
1156 fb1a36c0 2022-01-09 op cmd_cd(int argc, const char **argv)
1157 fb1a36c0 2022-01-09 op {
1158 fb1a36c0 2022-01-09 op struct qid qid;
1159 8f9f99f7 2022-01-16 op int nfid, miss;
1160 8f9f99f7 2022-01-16 op char *errstr;
1161 fb1a36c0 2022-01-09 op
1162 fb1a36c0 2022-01-09 op if (argc != 1) {
1163 fb1a36c0 2022-01-09 op printf("usage: cd remote-path\n");
1164 fb1a36c0 2022-01-09 op return;
1165 fb1a36c0 2022-01-09 op }
1166 fb1a36c0 2022-01-09 op
1167 8d8fb849 2022-01-17 op nfid = nextfid();
1168 8f9f99f7 2022-01-16 op errstr = walk_path(pwdfid, nfid, argv[0], &miss, &qid);
1169 8f9f99f7 2022-01-16 op if (errstr != NULL) {
1170 8f9f99f7 2022-01-16 op printf("%s: %s\n", argv[0], errstr);
1171 8f9f99f7 2022-01-16 op free(errstr);
1172 8f9f99f7 2022-01-16 op return;
1173 8f9f99f7 2022-01-16 op }
1174 8f9f99f7 2022-01-16 op
1175 8f9f99f7 2022-01-16 op if (miss != 0 || !(qid.type & QTDIR)) {
1176 8f9f99f7 2022-01-16 op printf("%s: not a directory\n", argv[0]);
1177 fcd9d510 2022-01-16 op if (miss == 0)
1178 fcd9d510 2022-01-16 op do_clunk(nfid);
1179 8f9f99f7 2022-01-16 op return;
1180 fb1a36c0 2022-01-09 op }
1181 8f9f99f7 2022-01-16 op
1182 8f9f99f7 2022-01-16 op do_clunk(pwdfid);
1183 8f9f99f7 2022-01-16 op pwdfid = nfid;
1184 fb1a36c0 2022-01-09 op }
1185 fb1a36c0 2022-01-09 op
1186 d3e1ab0c 2022-11-24 op void
1187 9c48f915 2022-01-16 op cmd_edit(int argc, const char **argv)
1188 9c48f915 2022-01-16 op {
1189 9c48f915 2022-01-16 op struct qid qid;
1190 9c48f915 2022-01-16 op int nfid, tmpfd, miss;
1191 9c48f915 2022-01-16 op char sfn[TMPFSTRLEN], p[PATH_MAX], *name, *errstr;
1192 9c48f915 2022-01-16 op const char *ed;
1193 9c48f915 2022-01-16 op
1194 9c48f915 2022-01-16 op if (argc != 1) {
1195 9c48f915 2022-01-16 op puts("usage: edit file");
1196 9c48f915 2022-01-16 op return;
1197 9c48f915 2022-01-16 op }
1198 9c48f915 2022-01-16 op
1199 9c48f915 2022-01-16 op if ((ed = getenv("VISUAL")) == NULL &&
1200 9c48f915 2022-01-16 op (ed = getenv("EDITOR")) == NULL)
1201 9c48f915 2022-01-16 op ed = "ed";
1202 9c48f915 2022-01-16 op
1203 8d8fb849 2022-01-17 op nfid = nextfid();
1204 9c48f915 2022-01-16 op errstr = walk_path(pwdfid, nfid, *argv, &miss, &qid);
1205 9c48f915 2022-01-16 op if (errstr != NULL) {
1206 9c48f915 2022-01-16 op printf("%s: %s\n", *argv, errstr);
1207 9c48f915 2022-01-16 op free(errstr);
1208 9c48f915 2022-01-16 op return;
1209 9c48f915 2022-01-16 op }
1210 9c48f915 2022-01-16 op
1211 9c48f915 2022-01-16 op if (miss != 0 || qid.type != 0) {
1212 9c48f915 2022-01-16 op printf("%s: not a file\n", *argv);
1213 9c48f915 2022-01-16 op if (miss == 0)
1214 9c48f915 2022-01-16 op do_clunk(nfid);
1215 9c48f915 2022-01-16 op return;
1216 9c48f915 2022-01-16 op }
1217 9c48f915 2022-01-16 op
1218 9c48f915 2022-01-16 op if ((tmpfd = tmp_file(sfn)) == -1) {
1219 9c48f915 2022-01-16 op do_clunk(nfid);
1220 9c48f915 2022-01-16 op return;
1221 9c48f915 2022-01-16 op }
1222 9c48f915 2022-01-16 op
1223 9c48f915 2022-01-16 op strlcpy(p, *argv, sizeof(p));
1224 9c48f915 2022-01-16 op name = basename(p);
1225 9c48f915 2022-01-16 op
1226 02571aa1 2022-01-29 op if (fetch_fid(nfid, tmpfd, name)) {
1227 02571aa1 2022-01-29 op warn("failed fetch or can't write %s", sfn);
1228 02571aa1 2022-01-29 op goto end;
1229 02571aa1 2022-01-29 op }
1230 9c48f915 2022-01-16 op close(tmpfd);
1231 9c48f915 2022-01-16 op
1232 9c48f915 2022-01-16 op spawn(ed, sfn, NULL);
1233 9c48f915 2022-01-16 op
1234 9c48f915 2022-01-16 op /*
1235 9c48f915 2022-01-16 op * Re-open the file because it's not guaranteed that the
1236 9c48f915 2022-01-16 op * file descriptor tmpfd is still associated with the file
1237 9c48f915 2022-01-16 op * pointed by sfn: it's not uncommon for editor to write
1238 9c48f915 2022-01-16 op * a backup file and then rename(2) it to the file name.
1239 9c48f915 2022-01-16 op */
1240 9c48f915 2022-01-16 op if ((tmpfd = open(sfn, O_RDONLY)) == -1) {
1241 9c48f915 2022-01-16 op warn("can't open %s", sfn);
1242 9c48f915 2022-01-16 op goto end;
1243 9c48f915 2022-01-16 op }
1244 9c48f915 2022-01-16 op
1245 9c48f915 2022-01-16 op woc_file(tmpfd, *argv, name);
1246 9c48f915 2022-01-16 op close(tmpfd);
1247 9c48f915 2022-01-16 op
1248 9c48f915 2022-01-16 op end:
1249 9c48f915 2022-01-16 op unlink(sfn);
1250 9c48f915 2022-01-16 op }
1251 9c48f915 2022-01-16 op
1252 d3e1ab0c 2022-11-24 op void
1253 fb1a36c0 2022-01-09 op cmd_get(int argc, const char **argv)
1254 fb1a36c0 2022-01-09 op {
1255 fb1a36c0 2022-01-09 op struct qid qid;
1256 fb1a36c0 2022-01-09 op const char *l;
1257 8f9f99f7 2022-01-16 op char *errstr;
1258 8f9f99f7 2022-01-16 op int nfid, fd, miss;
1259 fb1a36c0 2022-01-09 op
1260 fb1a36c0 2022-01-09 op if (argc != 1 && argc != 2) {
1261 fb1a36c0 2022-01-09 op printf("usage: get remote-file [local-file]\n");
1262 fb1a36c0 2022-01-09 op return;
1263 fb1a36c0 2022-01-09 op }
1264 fb1a36c0 2022-01-09 op
1265 fb1a36c0 2022-01-09 op if (argc == 2)
1266 fb1a36c0 2022-01-09 op l = argv[1];
1267 fb1a36c0 2022-01-09 op else if ((l = strrchr(argv[0], '/')) != NULL)
1268 fb1a36c0 2022-01-09 op l++; /* skip / */
1269 fb1a36c0 2022-01-09 op else
1270 2ba25711 2022-01-16 op l = argv[0];
1271 fb1a36c0 2022-01-09 op
1272 8d8fb849 2022-01-17 op nfid = nextfid();
1273 8f9f99f7 2022-01-16 op errstr = walk_path(pwdfid, nfid, argv[0], &miss, &qid);
1274 8f9f99f7 2022-01-16 op if (errstr != NULL) {
1275 8f9f99f7 2022-01-16 op printf("%s: %s\n", argv[0], errstr);
1276 8f9f99f7 2022-01-16 op free(errstr);
1277 fb1a36c0 2022-01-09 op return;
1278 fb1a36c0 2022-01-09 op }
1279 fb1a36c0 2022-01-09 op
1280 8f9f99f7 2022-01-16 op if (miss != 0 || qid.type != 0) {
1281 8f9f99f7 2022-01-16 op printf("%s: not a file\n", argv[0]);
1282 fcd9d510 2022-01-16 op if (miss == 0)
1283 fcd9d510 2022-01-16 op do_clunk(nfid);
1284 fb1a36c0 2022-01-09 op return;
1285 fb1a36c0 2022-01-09 op }
1286 fb1a36c0 2022-01-09 op
1287 76521d26 2022-01-16 op if ((fd = open(l, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0644)) == -1) {
1288 76521d26 2022-01-16 op warn("can't open %s", l);
1289 76521d26 2022-01-16 op do_clunk(nfid);
1290 76521d26 2022-01-16 op return;
1291 76521d26 2022-01-16 op }
1292 76521d26 2022-01-16 op
1293 02571aa1 2022-01-29 op if (fetch_fid(nfid, fd, l) == -1)
1294 02571aa1 2022-01-29 op warn("write %s", l);
1295 76521d26 2022-01-16 op close(fd);
1296 5565d021 2022-01-16 op }
1297 5565d021 2022-01-16 op
1298 d3e1ab0c 2022-11-24 op void
1299 5565d021 2022-01-16 op cmd_hexdump(int argc, const char **argv)
1300 5565d021 2022-01-16 op {
1301 5565d021 2022-01-16 op if (argc == 0) {
1302 5565d021 2022-01-16 op xdump = !xdump;
1303 5565d021 2022-01-16 op if (xdump)
1304 5565d021 2022-01-16 op puts("hexdump mode enabled");
1305 5565d021 2022-01-16 op else
1306 5565d021 2022-01-16 op puts("hexdump mode disabled");
1307 5565d021 2022-01-16 op return;
1308 5565d021 2022-01-16 op }
1309 5565d021 2022-01-16 op
1310 5565d021 2022-01-16 op if (argc > 1)
1311 5565d021 2022-01-16 op goto usage;
1312 5565d021 2022-01-16 op
1313 5565d021 2022-01-16 op if (!strcmp(*argv, "on")) {
1314 5565d021 2022-01-16 op xdump = 1;
1315 5565d021 2022-01-16 op puts("hexdump mode enabled");
1316 5565d021 2022-01-16 op return;
1317 5565d021 2022-01-16 op }
1318 5565d021 2022-01-16 op
1319 5565d021 2022-01-16 op if (!strcmp(*argv, "off")) {
1320 5565d021 2022-01-16 op xdump = 0;
1321 5565d021 2022-01-16 op puts("hexdump mode disabled");
1322 5565d021 2022-01-16 op return;
1323 5565d021 2022-01-16 op }
1324 5565d021 2022-01-16 op
1325 5565d021 2022-01-16 op usage:
1326 5565d021 2022-01-16 op puts("usage: hexdump [on | off]");
1327 fb1a36c0 2022-01-09 op }
1328 fb1a36c0 2022-01-09 op
1329 d3e1ab0c 2022-11-24 op void
1330 fb1a36c0 2022-01-09 op cmd_lcd(int argc, const char **argv)
1331 fb1a36c0 2022-01-09 op {
1332 fb1a36c0 2022-01-09 op const char *dir;
1333 fb1a36c0 2022-01-09 op
1334 fb1a36c0 2022-01-09 op if (argc > 1) {
1335 d166ffc0 2022-01-16 op printf("usage: lcd [local-directory]\n");
1336 fb1a36c0 2022-01-09 op return;
1337 fb1a36c0 2022-01-09 op }
1338 fb1a36c0 2022-01-09 op
1339 fb1a36c0 2022-01-09 op if (argc == 1)
1340 fb1a36c0 2022-01-09 op dir = *argv;
1341 fb1a36c0 2022-01-09 op
1342 fb1a36c0 2022-01-09 op if (argc == 0 && (dir = getenv("HOME")) == NULL) {
1343 fb1a36c0 2022-01-09 op printf("HOME is not defined\n");
1344 fb1a36c0 2022-01-09 op return;
1345 fb1a36c0 2022-01-09 op }
1346 fb1a36c0 2022-01-09 op
1347 fb1a36c0 2022-01-09 op if (chdir(dir) == -1)
1348 fb1a36c0 2022-01-09 op printf("cd: %s: %s\n", dir, strerror(errno));
1349 fb1a36c0 2022-01-09 op }
1350 fb1a36c0 2022-01-09 op
1351 d3e1ab0c 2022-11-24 op void
1352 fb1a36c0 2022-01-09 op cmd_lpwd(int argc, const char **argv)
1353 fb1a36c0 2022-01-09 op {
1354 fb1a36c0 2022-01-09 op char path[PATH_MAX];
1355 fb1a36c0 2022-01-09 op
1356 d166ffc0 2022-01-16 op if (argc != 0) {
1357 d166ffc0 2022-01-16 op printf("usage: lpwd\n");
1358 d166ffc0 2022-01-16 op return;
1359 d166ffc0 2022-01-16 op }
1360 d166ffc0 2022-01-16 op
1361 fb1a36c0 2022-01-09 op if (getcwd(path, sizeof(path)) == NULL) {
1362 fb1a36c0 2022-01-09 op printf("lpwd: %s\n", strerror(errno));
1363 fb1a36c0 2022-01-09 op return;
1364 fb1a36c0 2022-01-09 op }
1365 fb1a36c0 2022-01-09 op
1366 fb1a36c0 2022-01-09 op printf("%s\n", path);
1367 fb1a36c0 2022-01-09 op }
1368 fb1a36c0 2022-01-09 op
1369 d3e1ab0c 2022-11-24 op void
1370 fb1a36c0 2022-01-09 op cmd_ls(int argc, const char **argv)
1371 fb1a36c0 2022-01-09 op {
1372 9c9e60d1 2022-11-23 op if (argc > 1) {
1373 9c9e60d1 2022-11-23 op puts("usage: ls [path]");
1374 fb1a36c0 2022-01-09 op return;
1375 fb1a36c0 2022-01-09 op }
1376 fb1a36c0 2022-01-09 op
1377 d3e1ab0c 2022-11-24 op dir_listing(argc == 0 ? "." : argv[0], print_dirent, 1);
1378 fb1a36c0 2022-01-09 op }
1379 fb1a36c0 2022-01-09 op
1380 d3e1ab0c 2022-11-24 op void
1381 9e7dd230 2022-01-12 op cmd_page(int argc, const char **argv)
1382 9e7dd230 2022-01-12 op {
1383 9e7dd230 2022-01-12 op struct qid qid;
1384 02571aa1 2022-01-29 op int nfid, tmpfd, miss, r;
1385 64d8db89 2022-01-16 op char sfn[TMPFSTRLEN], p[PATH_MAX], *name, *errstr;
1386 62f663bb 2022-01-16 op const char *pager;
1387 9e7dd230 2022-01-12 op
1388 9e7dd230 2022-01-12 op if (argc != 1) {
1389 9e7dd230 2022-01-12 op puts("usage: page file");
1390 9e7dd230 2022-01-12 op return;
1391 9e7dd230 2022-01-12 op }
1392 62f663bb 2022-01-16 op
1393 62f663bb 2022-01-16 op if ((pager = getenv("PAGER")) == NULL)
1394 62f663bb 2022-01-16 op pager = "less";
1395 9e7dd230 2022-01-12 op
1396 8d8fb849 2022-01-17 op nfid = nextfid();
1397 8f9f99f7 2022-01-16 op errstr = walk_path(pwdfid, nfid, *argv, &miss, &qid);
1398 8f9f99f7 2022-01-16 op if (errstr != NULL) {
1399 8f9f99f7 2022-01-16 op printf("%s: %s\n", *argv, errstr);
1400 8f9f99f7 2022-01-16 op free(errstr);
1401 9e7dd230 2022-01-12 op return;
1402 9e7dd230 2022-01-12 op }
1403 9e7dd230 2022-01-12 op
1404 8f9f99f7 2022-01-16 op if (miss != 0 || qid.type != 0) {
1405 8f9f99f7 2022-01-16 op printf("%s: not a file\n", *argv);
1406 fcd9d510 2022-01-16 op if (miss == 0)
1407 fcd9d510 2022-01-16 op do_clunk(nfid);
1408 9e7dd230 2022-01-12 op return;
1409 9e7dd230 2022-01-12 op }
1410 9e7dd230 2022-01-12 op
1411 64d8db89 2022-01-16 op if ((tmpfd = tmp_file(sfn)) == -1) {
1412 9e7dd230 2022-01-12 op do_clunk(nfid);
1413 9e7dd230 2022-01-12 op return;
1414 9e7dd230 2022-01-12 op }
1415 9e7dd230 2022-01-12 op
1416 9e7dd230 2022-01-12 op strlcpy(p, *argv, sizeof(p));
1417 9e7dd230 2022-01-12 op name = basename(p);
1418 02571aa1 2022-01-29 op if ((r = fetch_fid(nfid, tmpfd, name)) == -1)
1419 02571aa1 2022-01-29 op warn("write %s", sfn);
1420 9e7dd230 2022-01-12 op close(tmpfd);
1421 02571aa1 2022-01-29 op if (r != -1)
1422 02571aa1 2022-01-29 op spawn(pager, sfn, NULL);
1423 9e7dd230 2022-01-12 op unlink(sfn);
1424 01fb39ab 2022-01-16 op }
1425 01fb39ab 2022-01-16 op
1426 d3e1ab0c 2022-11-24 op void
1427 1c9ab7cf 2022-01-29 op cmd_pipe(int argc, const char **argv)
1428 1c9ab7cf 2022-01-29 op {
1429 1c9ab7cf 2022-01-29 op struct qid qid;
1430 1c9ab7cf 2022-01-29 op pid_t pid;
1431 1c9ab7cf 2022-01-29 op int nfid, tmpfd, miss, status;
1432 1c9ab7cf 2022-01-29 op int filedes[2]; /* read end, write end */
1433 1c9ab7cf 2022-01-29 op char *errstr;
1434 1c9ab7cf 2022-01-29 op
1435 1c9ab7cf 2022-01-29 op if (argc < 2) {
1436 1c9ab7cf 2022-01-29 op puts("usage: pipe remote-file cmd [args...]");
1437 1c9ab7cf 2022-01-29 op return;
1438 1c9ab7cf 2022-01-29 op }
1439 1c9ab7cf 2022-01-29 op
1440 1c9ab7cf 2022-01-29 op nfid = nextfid();
1441 1c9ab7cf 2022-01-29 op errstr = walk_path(pwdfid, nfid, *argv, &miss, &qid);
1442 1c9ab7cf 2022-01-29 op if (errstr != NULL) {
1443 1c9ab7cf 2022-01-29 op printf("%s: %s\n", *argv, errstr);
1444 1c9ab7cf 2022-01-29 op free(errstr);
1445 1c9ab7cf 2022-01-29 op return;
1446 1c9ab7cf 2022-01-29 op }
1447 1c9ab7cf 2022-01-29 op
1448 1c9ab7cf 2022-01-29 op if (miss != 0 || qid.type != 0) {
1449 1c9ab7cf 2022-01-29 op printf("%s: not a file\n", *argv);
1450 1c9ab7cf 2022-01-29 op if (miss == 0)
1451 1c9ab7cf 2022-01-29 op do_clunk(nfid);
1452 1c9ab7cf 2022-01-29 op return;
1453 1c9ab7cf 2022-01-29 op }
1454 1c9ab7cf 2022-01-29 op
1455 1c9ab7cf 2022-01-29 op if (pipe(filedes) == -1)
1456 1c9ab7cf 2022-01-29 op err(1, "pipe");
1457 1c9ab7cf 2022-01-29 op
1458 1c9ab7cf 2022-01-29 op switch (pid = vfork()) {
1459 1c9ab7cf 2022-01-29 op case -1:
1460 1c9ab7cf 2022-01-29 op err(1, "vfork");
1461 1c9ab7cf 2022-01-29 op case 0:
1462 1c9ab7cf 2022-01-29 op close(filedes[1]);
1463 1c9ab7cf 2022-01-29 op if (dup2(filedes[0], 0) == -1)
1464 1c9ab7cf 2022-01-29 op err(1, "dup2");
1465 1c9ab7cf 2022-01-29 op execvp(argv[1], (char *const *)argv + 1);
1466 1c9ab7cf 2022-01-29 op err(1, "execvp");
1467 1c9ab7cf 2022-01-29 op }
1468 1c9ab7cf 2022-01-29 op
1469 1c9ab7cf 2022-01-29 op close(filedes[0]);
1470 1c9ab7cf 2022-01-29 op if (fetch_fid(nfid, filedes[1], *argv) == -1)
1471 1c9ab7cf 2022-01-29 op warnx("failed to fetch all the file");
1472 1c9ab7cf 2022-01-29 op close(filedes[1]);
1473 1c9ab7cf 2022-01-29 op
1474 1c9ab7cf 2022-01-29 op waitpid(pid, &status, 0);
1475 1c9ab7cf 2022-01-29 op }
1476 1c9ab7cf 2022-01-29 op
1477 d3e1ab0c 2022-11-24 op void
1478 01fb39ab 2022-01-16 op cmd_put(int argc, const char **argv)
1479 01fb39ab 2022-01-16 op {
1480 01fb39ab 2022-01-16 op struct qid qid;
1481 9c48f915 2022-01-16 op const char *l;
1482 9c48f915 2022-01-16 op int fd;
1483 01fb39ab 2022-01-16 op
1484 01fb39ab 2022-01-16 op if (argc != 1 && argc != 2) {
1485 01fb39ab 2022-01-16 op printf("usage: put local-file [remote-file]\n");
1486 01fb39ab 2022-01-16 op return;
1487 01fb39ab 2022-01-16 op }
1488 01fb39ab 2022-01-16 op
1489 01fb39ab 2022-01-16 op if (argc == 2)
1490 01fb39ab 2022-01-16 op l = argv[1];
1491 01fb39ab 2022-01-16 op else if ((l = strrchr(argv[0], '/')) != NULL)
1492 01fb39ab 2022-01-16 op l++; /* skip / */
1493 01fb39ab 2022-01-16 op else
1494 01fb39ab 2022-01-16 op l = argv[0];
1495 01fb39ab 2022-01-16 op
1496 01fb39ab 2022-01-16 op if ((fd = open(argv[0], O_RDONLY)) == -1) {
1497 01fb39ab 2022-01-16 op warn("%s", argv[0]);
1498 01fb39ab 2022-01-16 op return;
1499 01fb39ab 2022-01-16 op }
1500 01fb39ab 2022-01-16 op
1501 9c48f915 2022-01-16 op woc_file(fd, argv[0], l);
1502 01fb39ab 2022-01-16 op close(fd);
1503 c37e1cfe 2022-01-17 op }
1504 c37e1cfe 2022-01-17 op
1505 d3e1ab0c 2022-11-24 op void
1506 c37e1cfe 2022-01-17 op cmd_rename(int argc, const char **argv)
1507 c37e1cfe 2022-01-17 op {
1508 c37e1cfe 2022-01-17 op struct np_stat st;
1509 c37e1cfe 2022-01-17 op struct qid qid;
1510 c37e1cfe 2022-01-17 op char *errstr;
1511 c37e1cfe 2022-01-17 op int nfid, miss;
1512 c37e1cfe 2022-01-17 op
1513 c37e1cfe 2022-01-17 op if (argc != 2) {
1514 c37e1cfe 2022-01-17 op puts("usage: rename remote-file new-remote-name");
1515 c37e1cfe 2022-01-17 op return;
1516 c37e1cfe 2022-01-17 op }
1517 c37e1cfe 2022-01-17 op
1518 8d8fb849 2022-01-17 op nfid = nextfid();
1519 c37e1cfe 2022-01-17 op errstr = walk_path(pwdfid, nfid, argv[0], &miss, &qid);
1520 c37e1cfe 2022-01-17 op if (errstr != NULL) {
1521 c37e1cfe 2022-01-17 op printf("%s: %s\n", argv[0], errstr);
1522 c37e1cfe 2022-01-17 op free(errstr);
1523 c37e1cfe 2022-01-17 op return;
1524 c37e1cfe 2022-01-17 op }
1525 c37e1cfe 2022-01-17 op
1526 c37e1cfe 2022-01-17 op if (miss != 0) {
1527 c37e1cfe 2022-01-17 op printf("%s: not such file or directory\n", argv[0]);
1528 c37e1cfe 2022-01-17 op return;
1529 c37e1cfe 2022-01-17 op }
1530 c37e1cfe 2022-01-17 op
1531 c37e1cfe 2022-01-17 op prepare_wstat(&st);
1532 c37e1cfe 2022-01-17 op st.name = (char *)argv[1];
1533 c37e1cfe 2022-01-17 op if ((errstr = do_wstat(nfid, &st)) != NULL) {
1534 c37e1cfe 2022-01-17 op printf("rename: %s\n", errstr);
1535 c37e1cfe 2022-01-17 op free(errstr);
1536 c37e1cfe 2022-01-17 op }
1537 c37e1cfe 2022-01-17 op
1538 c37e1cfe 2022-01-17 op do_clunk(nfid);
1539 f99c17f9 2022-11-23 op }
1540 f99c17f9 2022-11-23 op
1541 d3e1ab0c 2022-11-24 op void
1542 f99c17f9 2022-11-23 op cmd_rm(int argc, const char **argv)
1543 f99c17f9 2022-11-23 op {
1544 f99c17f9 2022-11-23 op struct qid qid;
1545 f99c17f9 2022-11-23 op char *errstr;
1546 f99c17f9 2022-11-23 op int nfid, miss;
1547 f99c17f9 2022-11-23 op
1548 f99c17f9 2022-11-23 op if (argc == 0) {
1549 f99c17f9 2022-11-23 op puts("usage: rm file ...");
1550 f99c17f9 2022-11-23 op return;
1551 f99c17f9 2022-11-23 op }
1552 f99c17f9 2022-11-23 op
1553 f99c17f9 2022-11-23 op for (; *argv; ++argv, --argc) {
1554 f99c17f9 2022-11-23 op nfid = nextfid();
1555 f99c17f9 2022-11-23 op errstr = walk_path(pwdfid, nfid, *argv, &miss, &qid);
1556 f99c17f9 2022-11-23 op if (errstr != NULL) {
1557 f99c17f9 2022-11-23 op printf("%s: %s\n", *argv, errstr);
1558 f99c17f9 2022-11-23 op free(errstr);
1559 f99c17f9 2022-11-23 op continue;
1560 f99c17f9 2022-11-23 op }
1561 f99c17f9 2022-11-23 op if (miss) {
1562 f99c17f9 2022-11-23 op printf("%s: not such file or directory\n", *argv);
1563 f99c17f9 2022-11-23 op continue;
1564 f99c17f9 2022-11-23 op }
1565 f99c17f9 2022-11-23 op
1566 f99c17f9 2022-11-23 op if ((errstr = do_remove(nfid)) != NULL) {
1567 f99c17f9 2022-11-23 op printf("%s: %s\n", *argv, errstr);
1568 f99c17f9 2022-11-23 op free(errstr);
1569 f99c17f9 2022-11-23 op continue;
1570 f99c17f9 2022-11-23 op }
1571 f99c17f9 2022-11-23 op }
1572 9e7dd230 2022-01-12 op }
1573 9e7dd230 2022-01-12 op
1574 d3e1ab0c 2022-11-24 op void
1575 fb1a36c0 2022-01-09 op cmd_verbose(int argc, const char **argv)
1576 fb1a36c0 2022-01-09 op {
1577 fb1a36c0 2022-01-09 op if (argc == 0) {
1578 fb1a36c0 2022-01-09 op log_setverbose(!log_getverbose());
1579 fb1a36c0 2022-01-09 op if (log_getverbose())
1580 fb1a36c0 2022-01-09 op puts("verbose mode enabled");
1581 fb1a36c0 2022-01-09 op else
1582 fb1a36c0 2022-01-09 op puts("verbose mode disabled");
1583 fb1a36c0 2022-01-09 op return;
1584 fb1a36c0 2022-01-09 op }
1585 fb1a36c0 2022-01-09 op
1586 fb1a36c0 2022-01-09 op if (argc != 1)
1587 fb1a36c0 2022-01-09 op goto usage;
1588 fb1a36c0 2022-01-09 op
1589 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "on")) {
1590 fb1a36c0 2022-01-09 op log_setverbose(1);
1591 fb1a36c0 2022-01-09 op puts("verbose mode enabled");
1592 fb1a36c0 2022-01-09 op return;
1593 fb1a36c0 2022-01-09 op }
1594 fb1a36c0 2022-01-09 op
1595 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "off")) {
1596 fb1a36c0 2022-01-09 op log_setverbose(0);
1597 fb1a36c0 2022-01-09 op puts("verbose mode disabled");
1598 fb1a36c0 2022-01-09 op return;
1599 fb1a36c0 2022-01-09 op }
1600 fb1a36c0 2022-01-09 op
1601 fb1a36c0 2022-01-09 op usage:
1602 fb1a36c0 2022-01-09 op printf("verbose [on | off]\n");
1603 fb1a36c0 2022-01-09 op }
1604 fb1a36c0 2022-01-09 op
1605 fb1a36c0 2022-01-09 op static void
1606 fb1a36c0 2022-01-09 op excmd(int argc, const char **argv)
1607 fb1a36c0 2022-01-09 op {
1608 fb1a36c0 2022-01-09 op size_t i;
1609 fb1a36c0 2022-01-09 op
1610 fb1a36c0 2022-01-09 op if (argc == 0)
1611 fb1a36c0 2022-01-09 op return;
1612 d3e1ab0c 2022-11-24 op
1613 fb1a36c0 2022-01-09 op for (i = 0; i < nitems(cmds); ++i) {
1614 fb1a36c0 2022-01-09 op if (!strcmp(cmds[i].name, *argv)) {
1615 fb1a36c0 2022-01-09 op cmds[i].fn(argc-1, argv+1);
1616 fb1a36c0 2022-01-09 op return;
1617 fb1a36c0 2022-01-09 op }
1618 fb1a36c0 2022-01-09 op }
1619 fb1a36c0 2022-01-09 op
1620 fb1a36c0 2022-01-09 op log_warnx("unknown command %s", *argv);
1621 14ae3944 2022-11-23 op }
1622 14ae3944 2022-11-23 op
1623 55e8c065 2022-11-23 op static int
1624 55e8c065 2022-11-23 op parsecmd(char *cmd, char **argv, size_t len)
1625 55e8c065 2022-11-23 op {
1626 55e8c065 2022-11-23 op int escape, quote;
1627 55e8c065 2022-11-23 op int argc = 0;
1628 55e8c065 2022-11-23 op
1629 55e8c065 2022-11-23 op memset(argv, 0, sizeof(*argv) * len);
1630 55e8c065 2022-11-23 op
1631 55e8c065 2022-11-23 op while (argc < len) {
1632 55e8c065 2022-11-23 op while (isspace((unsigned char)*cmd))
1633 55e8c065 2022-11-23 op cmd++;
1634 55e8c065 2022-11-23 op if (*cmd == '\0')
1635 55e8c065 2022-11-23 op break;
1636 55e8c065 2022-11-23 op
1637 55e8c065 2022-11-23 op argv[argc++] = cmd;
1638 55e8c065 2022-11-23 op escape = quote = 0;
1639 55e8c065 2022-11-23 op for (; *cmd != '\0'; ++cmd) {
1640 55e8c065 2022-11-23 op if (escape) {
1641 55e8c065 2022-11-23 op escape = 0;
1642 55e8c065 2022-11-23 op continue;
1643 55e8c065 2022-11-23 op }
1644 55e8c065 2022-11-23 op if (*cmd == '\\') {
1645 55e8c065 2022-11-23 op escape = 1;
1646 55e8c065 2022-11-23 op memmove(cmd, cmd + 1, strlen(cmd));
1647 55e8c065 2022-11-23 op cmd--;
1648 55e8c065 2022-11-23 op continue;
1649 55e8c065 2022-11-23 op }
1650 55e8c065 2022-11-23 op if (*cmd == quote) {
1651 55e8c065 2022-11-23 op quote = 0;
1652 55e8c065 2022-11-23 op memmove(cmd, cmd + 1, strlen(cmd));
1653 55e8c065 2022-11-23 op cmd--;
1654 55e8c065 2022-11-23 op continue;
1655 55e8c065 2022-11-23 op }
1656 55e8c065 2022-11-23 op if (*cmd == '\'' || *cmd == '"') {
1657 55e8c065 2022-11-23 op quote = *cmd;
1658 55e8c065 2022-11-23 op memmove(cmd, cmd + 1, strlen(cmd));
1659 55e8c065 2022-11-23 op cmd--;
1660 55e8c065 2022-11-23 op continue;
1661 55e8c065 2022-11-23 op }
1662 55e8c065 2022-11-23 op if (quote)
1663 55e8c065 2022-11-23 op continue;
1664 55e8c065 2022-11-23 op
1665 55e8c065 2022-11-23 op if (isspace((unsigned char)*cmd))
1666 55e8c065 2022-11-23 op break;
1667 55e8c065 2022-11-23 op }
1668 55e8c065 2022-11-23 op
1669 55e8c065 2022-11-23 op if (*cmd == '\0' && (escape || quote)) {
1670 55e8c065 2022-11-23 op fprintf(stderr, "unterminated %s\n",
1671 55e8c065 2022-11-23 op escape ? "escape" : "quote");
1672 55e8c065 2022-11-23 op return -1;
1673 55e8c065 2022-11-23 op }
1674 55e8c065 2022-11-23 op
1675 55e8c065 2022-11-23 op if (*cmd == '\0')
1676 55e8c065 2022-11-23 op break;
1677 55e8c065 2022-11-23 op *cmd++ = '\0';
1678 55e8c065 2022-11-23 op }
1679 55e8c065 2022-11-23 op
1680 55e8c065 2022-11-23 op if (*cmd != '\0') {
1681 55e8c065 2022-11-23 op fprintf(stderr, "too many arguments\n");
1682 55e8c065 2022-11-23 op return -1;
1683 55e8c065 2022-11-23 op }
1684 55e8c065 2022-11-23 op return argc;
1685 55e8c065 2022-11-23 op }
1686 55e8c065 2022-11-23 op
1687 14ae3944 2022-11-23 op static void
1688 df9bb54d 2022-11-23 op cd_or_fetch(const char *path, const char *outfile)
1689 14ae3944 2022-11-23 op {
1690 14ae3944 2022-11-23 op struct qid qid;
1691 14ae3944 2022-11-23 op char *errstr;
1692 14ae3944 2022-11-23 op int fd, nfid, miss;
1693 14ae3944 2022-11-23 op
1694 14ae3944 2022-11-23 op while (*path == '/')
1695 14ae3944 2022-11-23 op path++;
1696 14ae3944 2022-11-23 op if (*path == '\0')
1697 14ae3944 2022-11-23 op return;
1698 14ae3944 2022-11-23 op
1699 14ae3944 2022-11-23 op nfid = nextfid();
1700 14ae3944 2022-11-23 op errstr = walk_path(pwdfid, nfid, path, &miss, &qid);
1701 14ae3944 2022-11-23 op if (errstr)
1702 14ae3944 2022-11-23 op errx(1, "walk %s: %s", path, errstr);
1703 14ae3944 2022-11-23 op if (miss)
1704 14ae3944 2022-11-23 op errc(1, ENOENT, "walk %s", path);
1705 14ae3944 2022-11-23 op
1706 14ae3944 2022-11-23 op if (qid.type & QTDIR) {
1707 df9bb54d 2022-11-23 op if (outfile)
1708 df9bb54d 2022-11-23 op errx(1, "can't fetch directory %s", path);
1709 14ae3944 2022-11-23 op do_clunk(pwdfid);
1710 14ae3944 2022-11-23 op pwdfid = nfid;
1711 14ae3944 2022-11-23 op return;
1712 14ae3944 2022-11-23 op }
1713 14ae3944 2022-11-23 op
1714 df9bb54d 2022-11-23 op if (outfile == NULL) {
1715 df9bb54d 2022-11-23 op if ((outfile = strrchr(path, '/')) == NULL)
1716 df9bb54d 2022-11-23 op outfile = path;
1717 df9bb54d 2022-11-23 op else
1718 df9bb54d 2022-11-23 op outfile++;
1719 df9bb54d 2022-11-23 op if (*outfile == '\0')
1720 df9bb54d 2022-11-23 op errx(1, "invalid path: missing file name: %s",
1721 df9bb54d 2022-11-23 op path);
1722 df9bb54d 2022-11-23 op }
1723 14ae3944 2022-11-23 op
1724 df9bb54d 2022-11-23 op if (strcmp(outfile, "-") != 0) {
1725 df9bb54d 2022-11-23 op fd = open(outfile, O_WRONLY|O_CREAT, 0644);
1726 df9bb54d 2022-11-23 op if (fd == -1)
1727 df9bb54d 2022-11-23 op err(1, "can't open for writing %s", outfile);
1728 df9bb54d 2022-11-23 op } else
1729 df9bb54d 2022-11-23 op fd = 1;
1730 df9bb54d 2022-11-23 op
1731 df9bb54d 2022-11-23 op if (fetch_fid(nfid, fd, outfile) == -1)
1732 df9bb54d 2022-11-23 op err(1, "write %s", outfile);
1733 14ae3944 2022-11-23 op close(fd);
1734 14ae3944 2022-11-23 op fclose(fp);
1735 14ae3944 2022-11-23 op exit(0);
1736 fb1a36c0 2022-01-09 op }
1737 fb1a36c0 2022-01-09 op
1738 14ae3944 2022-11-23 op static const char *
1739 14ae3944 2022-11-23 op parse_addr(const char *url, const char **user,
1740 14ae3944 2022-11-23 op const char **port, const char **path)
1741 14ae3944 2022-11-23 op {
1742 14ae3944 2022-11-23 op static char buf[PATH_MAX];
1743 14ae3944 2022-11-23 op char *host, *t;
1744 14ae3944 2022-11-23 op
1745 14ae3944 2022-11-23 op *user = *port = *path = NULL;
1746 14ae3944 2022-11-23 op host = buf;
1747 14ae3944 2022-11-23 op
1748 14ae3944 2022-11-23 op if (strlcpy(buf, url, sizeof(buf)) >= sizeof(buf))
1749 14ae3944 2022-11-23 op errx(1, "connection string too long");
1750 14ae3944 2022-11-23 op
1751 9f134f93 2022-11-23 op if (!strncmp(host, "9p://", 5))
1752 9f134f93 2022-11-23 op host += 5;
1753 9f134f93 2022-11-23 op
1754 14ae3944 2022-11-23 op if ((t = strchr(host, '/')) != NULL) {
1755 14ae3944 2022-11-23 op if (t == host)
1756 14ae3944 2022-11-23 op errx(1, "invalid connection string: %s", url);
1757 14ae3944 2022-11-23 op *t++ = '\0';
1758 14ae3944 2022-11-23 op if (*t != '\0')
1759 14ae3944 2022-11-23 op *path = t;
1760 14ae3944 2022-11-23 op }
1761 14ae3944 2022-11-23 op
1762 14ae3944 2022-11-23 op if ((t = strchr(host, '@')) != NULL) {
1763 14ae3944 2022-11-23 op if (t == host)
1764 14ae3944 2022-11-23 op errx(1, "invalid connection string: %s", url);
1765 14ae3944 2022-11-23 op *t++ = '\0';
1766 14ae3944 2022-11-23 op *user = host;
1767 14ae3944 2022-11-23 op host = t;
1768 14ae3944 2022-11-23 op } else if ((*user = getenv("USER")) == NULL)
1769 14ae3944 2022-11-23 op errx(1, "USER not defined");
1770 14ae3944 2022-11-23 op
1771 14ae3944 2022-11-23 op if ((t = strchr(host, ':')) != NULL) {
1772 14ae3944 2022-11-23 op *t++ = '\0';
1773 14ae3944 2022-11-23 op if (*t != '\0')
1774 14ae3944 2022-11-23 op *port = t;
1775 14ae3944 2022-11-23 op }
1776 14ae3944 2022-11-23 op if (*port == NULL)
1777 14ae3944 2022-11-23 op *port = "1337";
1778 14ae3944 2022-11-23 op
1779 14ae3944 2022-11-23 op return host;
1780 14ae3944 2022-11-23 op }
1781 14ae3944 2022-11-23 op
1782 fb1a36c0 2022-01-09 op int
1783 fb1a36c0 2022-01-09 op main(int argc, char **argv)
1784 fb1a36c0 2022-01-09 op {
1785 14ae3944 2022-11-23 op const char *user, *host, *port, *path;
1786 df9bb54d 2022-11-23 op const char *outfile = NULL;
1787 14ae3944 2022-11-23 op int ch;
1788 fb1a36c0 2022-01-09 op
1789 fb1a36c0 2022-01-09 op log_init(1, LOG_DAEMON);
1790 fb1a36c0 2022-01-09 op log_setverbose(0);
1791 fb1a36c0 2022-01-09 op log_procinit(getprogname());
1792 fb1a36c0 2022-01-09 op
1793 df9bb54d 2022-11-23 op while ((ch = getopt(argc, argv, "C:cK:o:")) != -1) {
1794 fb1a36c0 2022-01-09 op switch (ch) {
1795 fb1a36c0 2022-01-09 op case 'C':
1796 ce28299c 2022-01-30 op tls = 1;
1797 fb1a36c0 2022-01-09 op crtpath = optarg;
1798 fb1a36c0 2022-01-09 op break;
1799 ab9f44f7 2022-11-23 op case 'c': /* deprecated, remove after 0.3 */
1800 fb1a36c0 2022-01-09 op tls = 1;
1801 fb1a36c0 2022-01-09 op break;
1802 fb1a36c0 2022-01-09 op case 'K':
1803 ce28299c 2022-01-30 op tls = 1;
1804 fb1a36c0 2022-01-09 op keypath = optarg;
1805 fb1a36c0 2022-01-09 op break;
1806 df9bb54d 2022-11-23 op case 'o':
1807 df9bb54d 2022-11-23 op outfile = optarg;
1808 df9bb54d 2022-11-23 op break;
1809 fb1a36c0 2022-01-09 op default:
1810 fb1a36c0 2022-01-09 op usage(1);
1811 fb1a36c0 2022-01-09 op }
1812 fb1a36c0 2022-01-09 op }
1813 fb1a36c0 2022-01-09 op argc -= optind;
1814 fb1a36c0 2022-01-09 op argv += optind;
1815 fb1a36c0 2022-01-09 op
1816 3847d69e 2022-01-30 op if (argc == 0 || (tls && crtpath == NULL))
1817 fb1a36c0 2022-01-09 op usage(1);
1818 fb1a36c0 2022-01-09 op
1819 14ae3944 2022-11-23 op host = parse_addr(argv[0], &user, &port, &path);
1820 514e8d43 2022-11-23 op if (path == NULL && argv[1] != NULL) /* drop argv[1] after 0.3 */
1821 14ae3944 2022-11-23 op path = argv[1];
1822 df9bb54d 2022-11-23 op if (outfile && path == NULL)
1823 df9bb54d 2022-11-23 op usage(1);
1824 14ae3944 2022-11-23 op
1825 1c9ab7cf 2022-01-29 op signal(SIGPIPE, SIG_IGN);
1826 fb1a36c0 2022-01-09 op if (isatty(1)) {
1827 fb1a36c0 2022-01-09 op tty_p = 1;
1828 fb1a36c0 2022-01-09 op resized = 1;
1829 fb1a36c0 2022-01-09 op signal(SIGWINCH, tty_resized);
1830 fb1a36c0 2022-01-09 op }
1831 fb1a36c0 2022-01-09 op
1832 fb1a36c0 2022-01-09 op if ((evb = evbuffer_new()) == NULL)
1833 fb1a36c0 2022-01-09 op fatal("evbuffer_new");
1834 fb1a36c0 2022-01-09 op
1835 fb1a36c0 2022-01-09 op if ((buf = evbuffer_new()) == NULL)
1836 fb1a36c0 2022-01-09 op fatal("evbuffer_new");
1837 fb1a36c0 2022-01-09 op
1838 fb1a36c0 2022-01-09 op if ((dirbuf = evbuffer_new()) == NULL)
1839 fb1a36c0 2022-01-09 op fatal("evbuferr_new");
1840 fb1a36c0 2022-01-09 op
1841 14ae3944 2022-11-23 op do_connect(host, port, user);
1842 14ae3944 2022-11-23 op if (path)
1843 df9bb54d 2022-11-23 op cd_or_fetch(path, outfile);
1844 fb1a36c0 2022-01-09 op
1845 d3e1ab0c 2022-11-24 op compl_setup();
1846 fb1a36c0 2022-01-09 op for (;;) {
1847 55e8c065 2022-11-23 op int argc;
1848 fb1a36c0 2022-01-09 op char *line, *argv[16] = {0}, **ap;
1849 fb1a36c0 2022-01-09 op
1850 fb1a36c0 2022-01-09 op if ((line = read_line("kamiftp> ")) == NULL)
1851 fb1a36c0 2022-01-09 op break;
1852 fb1a36c0 2022-01-09 op
1853 55e8c065 2022-11-23 op if ((argc = parsecmd(line, argv, nitems(argv) - 1)) == -1) {
1854 55e8c065 2022-11-23 op free(line);
1855 55e8c065 2022-11-23 op continue;
1856 fb1a36c0 2022-01-09 op }
1857 55e8c065 2022-11-23 op
1858 55e8c065 2022-11-23 op argv[argc] = NULL;
1859 fb1a36c0 2022-01-09 op excmd(argc, (const char **)argv);
1860 fb1a36c0 2022-01-09 op
1861 ca14d066 2022-11-23 op if (bell)
1862 ca14d066 2022-11-23 op fprintf(stderr, "\a");
1863 fb1a36c0 2022-01-09 op
1864 fb1a36c0 2022-01-09 op free(line);
1865 fb1a36c0 2022-01-09 op }
1866 fb1a36c0 2022-01-09 op
1867 fb1a36c0 2022-01-09 op printf("\n");
1868 8f7253b2 2022-11-22 op fclose(fp);
1869 fb1a36c0 2022-01-09 op }