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