xref: /plan9/sys/src/cmd/venti/read.c (revision 368c31ab13393dea083228fdd1c3445076f83a4b)
1 #include <u.h>
2 #include <libc.h>
3 #include <venti.h>
4 #include <libsec.h>
5 #include <thread.h>
6 
7 void
usage(void)8 usage(void)
9 {
10 	fprint(2, "usage: read [-h host] [-t type] score\n");
11 	threadexitsall("usage");
12 }
13 
14 void
threadmain(int argc,char * argv[])15 threadmain(int argc, char *argv[])
16 {
17 	int type, n;
18 	uchar score[VtScoreSize];
19 	uchar *buf;
20 	VtConn *z;
21 	char *host;
22 
23 	fmtinstall('F', vtfcallfmt);
24 	fmtinstall('V', vtscorefmt);
25 
26 	host = nil;
27 	type = -1;
28 	ARGBEGIN{
29 	case 'h':
30 		host = EARGF(usage());
31 		break;
32 	case 't':
33 		type = atoi(EARGF(usage()));
34 		break;
35 	default:
36 		usage();
37 		break;
38 	}ARGEND
39 
40 	if(argc != 1)
41 		usage();
42 
43 	if(vtparsescore(argv[0], nil, score) < 0)
44 		sysfatal("could not parse score '%s': %r", argv[0]);
45 
46 	buf = vtmallocz(VtMaxLumpSize);
47 
48 	z = vtdial(host);
49 	if(z == nil)
50 		sysfatal("could not connect to server: %r");
51 
52 	if(vtconnect(z) < 0)
53 		sysfatal("vtconnect: %r");
54 
55 	if(type == -1){
56 		n = -1;
57 		for(type=0; type<VtMaxType; type++){
58 			n = vtread(z, score, type, buf, VtMaxLumpSize);
59 			if(n >= 0){
60 				fprint(2, "venti/read%s%s %V %d\n", host ? " -h" : "", host ? host : "",
61 					score, type);
62 				break;
63 			}
64 		}
65 	}else
66 		n = vtread(z, score, type, buf, VtMaxLumpSize);
67 
68 	vthangup(z);
69 	if(n < 0)
70 		sysfatal("could not read block: %r");
71 	if(write(1, buf, n) != n)
72 		sysfatal("write: %r");
73 	threadexitsall(0);
74 }
75