utoppya.c revision 1.1 1 /* $NetBSD: utoppya.c,v 1.1 2006/04/03 08:15:48 scw Exp $ */
2
3 /*-
4 * Copyright (c) 2006 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Steve C. Woodford.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <sys/ioctl.h>
42
43 #include <err.h>
44 #include <errno.h>
45 #include <fcntl.h>
46 #include <libgen.h>
47 #include <sysexits.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <strings.h>
51 #include <unistd.h>
52 #include <time.h>
53
54 #include <dev/usb/utoppy.h>
55
56 #define GLOBAL
57 #include "progressbar.h"
58
59 #define _PATH_DEV_UTOPPY "/dev/utoppy0"
60
61 /*
62 * This looks weird for a reason. The toppy protocol allows for data to be
63 * transferred in 65535-byte chunks only. Anything more than this has to be
64 * split within the driver. The following value leaves enough space for the
65 * packet header plus some alignmnent slop.
66 */
67 #define TOPPY_IO_SIZE 0xffec
68
69 static int toppy_fd;
70
71 static void cmd_df(int, char **);
72 static void cmd_ls(int, char **);
73 static void cmd_rm(int, char **);
74 static void cmd_mkdir(int, char **);
75 static void cmd_rename(int, char **);
76 static void cmd_get(int, char **);
77 static void cmd_put(int, char **);
78
79 static struct toppy_command {
80 const char *tc_cmd;
81 void (*tc_handler)(int, char **);
82 } toppy_commands[] = {
83 {"df", cmd_df},
84 {"ls", cmd_ls},
85 {"get", cmd_get},
86 {"mkdir", cmd_mkdir},
87 {"put", cmd_put},
88 {"rename", cmd_rename},
89 {"rm", cmd_rm},
90 {NULL, NULL}
91 };
92
93 static void
94 usage(void)
95 {
96
97 fprintf(stderr, "usage: %s [-f <path>] <cmd> ...\n",
98 getprogname());
99
100 exit(EX_USAGE);
101 }
102
103 int
104 main(int argc, char *argv[])
105 {
106 struct toppy_command *tc;
107 const char *devpath;
108 int ch;
109
110 setprogname(argv[0]);
111 devpath = _PATH_DEV_UTOPPY;
112
113 while ((ch = getopt(argc, argv, "f:")) != -1) {
114 switch (ch) {
115 case 'f':
116 devpath = optarg;
117 break;
118
119 default:
120 usage();
121 }
122 }
123 argc -= optind;
124 argv += optind;
125
126 if (argc == 0)
127 usage();
128
129 for (tc = toppy_commands; tc->tc_cmd != NULL; tc++)
130 if (strcasecmp(argv[0], tc->tc_cmd) == 0)
131 break;
132
133 if (tc->tc_cmd == NULL)
134 errx(EX_USAGE, "'%s' is not a valid command", argv[0]);
135
136 if ((toppy_fd = open(devpath, O_RDWR)) < 0)
137 err(EX_OSERR, "open(%s)", devpath);
138
139 (*tc->tc_handler)(argc, argv);
140
141 close(toppy_fd);
142
143 return (0);
144 }
145
146 static int
147 find_toppy_dirent(const char *path, struct utoppy_dirent *udp)
148 {
149 struct utoppy_dirent ud;
150 char *d, *b, dir[FILENAME_MAX];
151 ssize_t l;
152
153 strncpy(dir, path, sizeof(dir));
154 b = basename(dir);
155 d = dirname(dir);
156 if (strcmp(b, "/") == 0 || strcmp(b, ".") == 0 || strcmp(d, ".") == 0)
157 errx(EX_USAGE, "'%s' is not a valid Toppy pathname", path);
158
159 if (ioctl(toppy_fd, UTOPPYIOREADDIR, &d) < 0)
160 err(EX_OSERR, "ioctl(UTOPPYIOREADDIR, %s)", d);
161
162 if (udp == NULL)
163 udp = &ud;
164
165 while ((l = read(toppy_fd, udp, sizeof(*udp))) == sizeof(*udp)) {
166 if (strcmp(b, udp->ud_path) == 0)
167 break;
168 }
169
170 if (l < 0)
171 err(EX_OSERR, "read(TOPPYDIR, %s)", d);
172
173 if (l == 0)
174 return (0);
175
176 while (read(toppy_fd, &ud, sizeof(ud)) > 0)
177 ;
178
179 return (1);
180 }
181
182 static void
183 cmd_df(int argc, char **argv)
184 {
185 struct utoppy_stats us;
186
187 if (ioctl(toppy_fd, UTOPPYIOSTATS, &us) < 0)
188 err(EX_OSERR, "ioctl(UTOPPYIOSTATS)");
189
190 printf("Hard Disk Size: %lld MB\n", us.us_hdd_size / (1024 * 1024));
191 printf("Hard Disk Free: %lld MB\n", us.us_hdd_free / (1024 * 1024));
192 }
193
194 static void
195 cmd_ls(int argc, char **argv)
196 {
197 struct utoppy_dirent ud;
198 struct tm *tm;
199 char *dir, *ext, dirbuf[2], ex, ft, tmbuf[32];
200 ssize_t l;
201
202 if (argc == 1) {
203 dirbuf[0] = '/';
204 dirbuf[1] = '\0';
205 dir = dirbuf;
206 } else
207 if (argc == 2)
208 dir = argv[1];
209 else
210 errx(EX_USAGE, "usage: ls [toppy-pathname]");
211
212 if (ioctl(toppy_fd, UTOPPYIOREADDIR, &dir) < 0)
213 err(EX_OSERR, "ioctl(UTOPPYIOREADDIR, %s)", dir);
214
215 while ((l = read(toppy_fd, &ud, sizeof(ud))) == sizeof(ud)) {
216 switch (ud.ud_type) {
217 default:
218 ft = '?';
219 break;
220
221 case UTOPPY_DIRENT_DIRECTORY:
222 ft = 'd';
223 break;
224
225 case UTOPPY_DIRENT_FILE:
226 ft = '-';
227 break;
228 }
229
230 if ((ext = strrchr(ud.ud_path, '.')) != NULL &&
231 strcasecmp(ext, ".tap") == 0)
232 ex = 'x';
233 else
234 ex = '-';
235
236 tm = localtime(&ud.ud_mtime);
237 strftime(tmbuf, sizeof(tmbuf), "%b %e %G %R", tm);
238
239 printf("%crw%c %11lld %s %s\n", ft, ex, ud.ud_size, tmbuf,
240 ud.ud_path);
241 }
242
243 if (l < 0)
244 err(EX_OSERR, "read(utoppy_dirent)");
245 }
246
247 static void
248 cmd_rm(int argc, char **argv)
249 {
250 char *path;
251
252 if (argc != 2)
253 errx(EX_USAGE, "usage: rm <toppy-pathname>");
254
255 path = argv[1];
256
257 if (ioctl(toppy_fd, UTOPPYIODELETE, &path) < 0)
258 err(EX_OSERR, "ioctl(UTOPPYIODELETE, %s)", path);
259 }
260
261 static void
262 cmd_mkdir(int argc, char **argv)
263 {
264 char *path;
265
266 if (argc != 2)
267 errx(EX_USAGE, "usage: mkdir <toppy-pathname>");
268
269 path = argv[1];
270
271 if (find_toppy_dirent(path, NULL))
272 errx(EX_DATAERR, "'%s' already exists", path);
273
274 if (ioctl(toppy_fd, UTOPPYIOMKDIR, &path) < 0)
275 err(EX_OSERR, "ioctl(UTOPPYIOMKDIR, %s)", path);
276 }
277
278 static void
279 cmd_rename(int argc, char **argv)
280 {
281 struct utoppy_dirent ud;
282 struct utoppy_rename ur;
283 char *oldpath, *newpath, *o, *n;
284
285 if (argc != 3)
286 errx(EX_USAGE, "usage: rename <from> <to>");
287
288 o = oldpath = argv[1];
289 n = newpath = argv[2];
290
291 for (o = oldpath; *o != '\0'; o++)
292 if (*o == '\\')
293 *o = '/';
294 for (n = newpath; *n != '\0'; n++)
295 if (*n == '\\')
296 *n = '/';
297
298 for (o = oldpath; *o && *o == '\\'; o++)
299 ;
300 for (n = newpath; *n && *n == '\\'; n++)
301 ;
302
303 if (strcmp(n, o) == 0)
304 errx(EX_DATAERR, "'%s' and '%s' refer to the same file\n",
305 oldpath, newpath);
306
307 if (find_toppy_dirent(oldpath, &ud) == 0)
308 errx(EX_DATAERR, "'%s' does not exist on the Toppy", oldpath);
309
310 if (ud.ud_type != UTOPPY_DIRENT_FILE)
311 errx(EX_DATAERR, "%s: not a regular file", oldpath);
312
313 if (find_toppy_dirent(newpath, &ud))
314 errx(EX_DATAERR, "'%s' already exists", newpath);
315
316 ur.ur_old_path = o;
317 ur.ur_new_path = n;
318
319 if (ioctl(toppy_fd, UTOPPYIORENAME, &ur) < 0)
320 err(EX_OSERR, "ioctl(UTOPPYIORENAME, %s, %s)", oldpath,
321 newpath);
322 }
323
324
325 static void
326 init_progress(FILE *to, char *f, off_t fsize, off_t restart)
327 {
328 struct ttysize ts;
329
330 if (ioctl(fileno(to), TIOCGSIZE, &ts) == -1)
331 ttywidth = 80;
332 else
333 ttywidth = ts.ts_cols;
334
335 ttyout = to;
336 progress = 1;
337 bytes = 0;
338 filesize = fsize;
339 restart_point = restart;
340 prefix = f;
341 }
342
343 static void
344 cmd_get(int argc, char **argv)
345 {
346 struct utoppy_readfile ur;
347 struct utoppy_dirent ud;
348 struct stat st;
349 char *dst, dstbuf[FILENAME_MAX];
350 uint8_t *buf;
351 ssize_t l;
352 size_t rv;
353 int ch, turbo_mode = 0, reget = 0, progbar = 0;
354 FILE *ofp, *to;
355
356 optind = 1;
357 optreset = 1;
358
359 while ((ch = getopt(argc, argv, "prt")) != -1) {
360 switch (ch) {
361 case 'p':
362 progbar = 1;
363 break;
364 case 'r':
365 reget = 1;
366 break;
367 case 't':
368 turbo_mode = 1;
369 break;
370 default:
371 get_usage:
372 errx(EX_USAGE, "usage: get [-prt] <toppy-pathname> "
373 "[file | directory]");
374 }
375 }
376 argc -= optind;
377 argv += optind;
378
379 if (argc == 1)
380 dst = basename(argv[0]);
381 else
382 if (argc == 2) {
383 dst = argv[1];
384 if (stat(dst, &st) == 0 && S_ISDIR(st.st_mode)) {
385 snprintf(dstbuf, sizeof(dstbuf), "%s/%s", dst,
386 basename(argv[0]));
387 dst = dstbuf;
388 }
389 } else
390 goto get_usage;
391
392 ur.ur_path = argv[0];
393 ur.ur_offset = 0;
394
395 if ((buf = malloc(TOPPY_IO_SIZE)) == NULL)
396 err(EX_OSERR, "malloc(TOPPY_IO_SIZE)");
397
398 if (strcmp(dst, "-") == 0) {
399 ofp = stdout;
400 to = stderr;
401 if (reget)
402 warnx("Ignoring -r option in combination with stdout");
403 } else {
404 to = stdout;
405
406 if (reget) {
407 if (stat(dst, &st) < 0) {
408 if (errno != ENOENT)
409 err(EX_OSERR, "stat(%s)", dst);
410 } else
411 if (!S_ISREG(st.st_mode))
412 errx(EX_DATAERR, "-r only works with regular "
413 "files");
414 else
415 ur.ur_offset = st.st_size;
416 }
417
418 if ((ofp = fopen(dst, reget ? "a" : "w")) == NULL)
419 err(EX_OSERR, "fopen(%s)", dst);
420 }
421
422 if (progbar) {
423 if (find_toppy_dirent(ur.ur_path, &ud) == 0)
424 ud.ud_size = 0;
425 init_progress(to, dst, ud.ud_size, ur.ur_offset);
426 }
427
428 if (ioctl(toppy_fd, UTOPPYIOTURBO, &turbo_mode) < 0)
429 err(EX_OSERR, "ioctl(UTOPPYIOTURBO, %d)", turbo_mode);
430
431 if (ioctl(toppy_fd, UTOPPYIOREADFILE, &ur) < 0)
432 err(EX_OSERR, "ioctl(UTOPPYIOREADFILE, %s)", ur.ur_path);
433
434 if (progbar)
435 progressmeter(-1);
436
437 for (;;) {
438 while ((l = read(toppy_fd, buf, TOPPY_IO_SIZE)) < 0 &&
439 errno == EINTR)
440 ;
441
442 if (l <= 0)
443 break;
444
445 rv = fwrite(buf, 1, l, ofp);
446
447 if (rv != l) {
448 if (ofp != stdout)
449 fclose(ofp);
450 progressmeter(1);
451 err(EX_OSERR, "fwrite(%s)", dst);
452 }
453 bytes += l;
454 }
455
456 if (progbar)
457 progressmeter(1);
458
459 if (ofp != stdout)
460 fclose(ofp);
461
462 if (l < 0)
463 err(EX_OSERR, "read(TOPPY: ur.ur_path)");
464
465 free(buf);
466 }
467
468 static void
469 cmd_put(int argc, char **argv)
470 {
471 struct utoppy_writefile uw;
472 struct utoppy_dirent ud;
473 struct stat st;
474 char dstbuf[FILENAME_MAX];
475 char *src;
476 void *buf;
477 ssize_t rv;
478 size_t l;
479 int ch, turbo_mode = 0, reput = 0, progbar = 0;
480 FILE *ifp;
481
482 optind = 1;
483 optreset = 1;
484
485 while ((ch = getopt(argc, argv, "prt")) != -1) {
486 switch (ch) {
487 case 'p':
488 progbar = 1;
489 break;
490 case 'r':
491 reput = 1;
492 break;
493 case 't':
494 turbo_mode = 1;
495 break;
496 default:
497 put_usage:
498 errx(EX_USAGE, "usage: put [-prt] <local-pathname> "
499 "<toppy-pathname>");
500 }
501 }
502 argc -= optind;
503 argv += optind;
504
505 if (argc != 2)
506 goto put_usage;
507
508 src = argv[0];
509 uw.uw_path = argv[1];
510
511 if (stat(src, &st) < 0)
512 err(EX_OSERR, "%s", src);
513
514 if (!S_ISREG(st.st_mode))
515 errx(EX_DATAERR, "'%s' is not a regular file", src);
516
517 uw.uw_size = st.st_size;
518 uw.uw_mtime = st.st_mtime;
519 uw.uw_offset = 0;
520
521 if (find_toppy_dirent(uw.uw_path, &ud)) {
522 if (ud.ud_type == UTOPPY_DIRENT_DIRECTORY) {
523 snprintf(dstbuf, sizeof(dstbuf), "%s/%s", uw.uw_path,
524 basename(src));
525 uw.uw_path = dstbuf;
526 } else
527 if (ud.ud_type != UTOPPY_DIRENT_FILE)
528 errx(EX_DATAERR, "'%s' is not a regular file.",
529 uw.uw_path);
530 else
531 if (reput) {
532 if (ud.ud_size > uw.uw_size)
533 errx(EX_DATAERR, "'%s' is already larger than "
534 "'%s'", uw.uw_path, src);
535
536 uw.uw_size -= ud.ud_size;
537 uw.uw_offset = ud.ud_size;
538 }
539 }
540
541 if ((buf = malloc(TOPPY_IO_SIZE)) == NULL)
542 err(EX_OSERR, "malloc(TOPPY_IO_SIZE)");
543
544 if ((ifp = fopen(src, "r")) == NULL)
545 err(EX_OSERR, "fopen(%s)", src);
546
547 if (ioctl(toppy_fd, UTOPPYIOTURBO, &turbo_mode) < 0)
548 err(EX_OSERR, "ioctl(UTOPPYIOTURBO, %d)", turbo_mode);
549
550 if (ioctl(toppy_fd, UTOPPYIOWRITEFILE, &uw) < 0)
551 err(EX_OSERR, "ioctl(UTOPPYIOWRITEFILE, %s)", uw.uw_path);
552
553 if (progbar)
554 init_progress(stdout, src, st.st_size, uw.uw_offset);
555
556 if (progbar)
557 progressmeter(-1);
558
559 while ((l = fread(buf, 1, TOPPY_IO_SIZE, ifp)) > 0) {
560 rv = write(toppy_fd, buf, l);
561 if (rv != l) {
562 fclose(ifp);
563 if (progbar)
564 progressmeter(1);
565 err(EX_OSERR, "write(TOPPY: %s)", uw.uw_path);
566 }
567 bytes += l;
568 }
569
570 if (progbar)
571 progressmeter(1);
572
573 if (ferror(ifp))
574 err(EX_OSERR, "fread(%s)", src);
575
576 fclose(ifp);
577 free(buf);
578 }
579