xref: /plan9/sys/src/cmd/venti/mkroot.c (revision ec59a3ddbfceee0efe34584c2c9981a5e5ff1ec4)
1 #include "stdinc.h"
2 #include "dat.h"
3 #include "fns.h"
4 
5 char *host;
6 
7 void
8 usage(void)
9 {
10 	fprint(2, "usage: mkroot [-h host] name type score blocksize prev\n");
11 	exits("usage");
12 }
13 
14 int
15 main(int argc, char *argv[])
16 {
17 	uchar score[VtScoreSize];
18 	uchar buf[VtRootSize];
19 	VtSession *z;
20 	VtRoot root;
21 
22 	ARGBEGIN{
23 	case 'h':
24 		host = EARGF(usage());
25 		break;
26 	default:
27 		usage();
28 		break;
29 	}ARGEND
30 
31 	if(argc != 5)
32 		usage();
33 
34 	vtAttach();
35 	fmtinstall('V', vtScoreFmt);
36 	fmtinstall('R', vtErrFmt);
37 
38 	root.version = VtRootVersion;
39 	strecpy(root.name, root.name+sizeof root.name, argv[0]);
40 	strecpy(root.type, root.type+sizeof root.type, argv[1]);
41 	if(!vtParseScore(argv[2], strlen(argv[2]), root.score))
42 		vtFatal("bad score '%s'", argv[2]);
43 	root.blockSize = atoi(argv[3]);
44 	if(!vtParseScore(argv[4], strlen(argv[4]), root.prev))
45 		vtFatal("bad score '%s'", argv[4]);
46 	vtRootPack(&root, buf);
47 
48 	z = vtDial(host, 0);
49 	if(z == nil)
50 		vtFatal("could not connect to server: %R");
51 
52 	if(!vtConnect(z, 0))
53 		sysfatal("vtConnect: %r");
54 
55 	if(!vtWrite(z, score, VtRootType, buf, VtRootSize))
56 		vtFatal("vtWrite: %R");
57 	vtClose(z);
58 	print("%V\n", score);
59 	vtDetach();
60 	exits(0);
61 	return 0;
62 }
63