Blame


1 01cea2ec 2008-06-14 rsc #include <u.h>
2 01cea2ec 2008-06-14 rsc #include <libc.h>
3 01cea2ec 2008-06-14 rsc #include <venti.h>
4 01cea2ec 2008-06-14 rsc #include <libsec.h>
5 01cea2ec 2008-06-14 rsc #include <thread.h>
6 01cea2ec 2008-06-14 rsc
7 01cea2ec 2008-06-14 rsc int chatty;
8 01cea2ec 2008-06-14 rsc
9 01cea2ec 2008-06-14 rsc void
10 01cea2ec 2008-06-14 rsc usage(void)
11 01cea2ec 2008-06-14 rsc {
12 01cea2ec 2008-06-14 rsc fprint(2, "usage: readfile [-v] [-h host] score\n");
13 01cea2ec 2008-06-14 rsc threadexitsall("usage");
14 01cea2ec 2008-06-14 rsc }
15 01cea2ec 2008-06-14 rsc
16 01cea2ec 2008-06-14 rsc void
17 01cea2ec 2008-06-14 rsc threadmain(int argc, char *argv[])
18 01cea2ec 2008-06-14 rsc {
19 01cea2ec 2008-06-14 rsc int n;
20 01cea2ec 2008-06-14 rsc uchar score[VtScoreSize];
21 01cea2ec 2008-06-14 rsc uchar *buf;
22 01cea2ec 2008-06-14 rsc char *host, *type;
23 01cea2ec 2008-06-14 rsc vlong off;
24 01cea2ec 2008-06-14 rsc VtEntry e;
25 01cea2ec 2008-06-14 rsc VtRoot root;
26 01cea2ec 2008-06-14 rsc VtCache *c;
27 01cea2ec 2008-06-14 rsc VtConn *z;
28 01cea2ec 2008-06-14 rsc VtFile *f;
29 01cea2ec 2008-06-14 rsc
30 01cea2ec 2008-06-14 rsc quotefmtinstall();
31 01cea2ec 2008-06-14 rsc fmtinstall('F', vtfcallfmt);
32 01cea2ec 2008-06-14 rsc fmtinstall('V', vtscorefmt);
33 01cea2ec 2008-06-14 rsc
34 01cea2ec 2008-06-14 rsc host = nil;
35 01cea2ec 2008-06-14 rsc ARGBEGIN{
36 01cea2ec 2008-06-14 rsc case 'V':
37 01cea2ec 2008-06-14 rsc chattyventi++;
38 01cea2ec 2008-06-14 rsc break;
39 01cea2ec 2008-06-14 rsc case 'h':
40 01cea2ec 2008-06-14 rsc host = EARGF(usage());
41 01cea2ec 2008-06-14 rsc break;
42 01cea2ec 2008-06-14 rsc case 'v':
43 01cea2ec 2008-06-14 rsc chatty++;
44 01cea2ec 2008-06-14 rsc break;
45 01cea2ec 2008-06-14 rsc default:
46 01cea2ec 2008-06-14 rsc usage();
47 01cea2ec 2008-06-14 rsc break;
48 01cea2ec 2008-06-14 rsc }ARGEND
49 01cea2ec 2008-06-14 rsc
50 01cea2ec 2008-06-14 rsc if(argc != 1)
51 01cea2ec 2008-06-14 rsc usage();
52 01cea2ec 2008-06-14 rsc
53 01cea2ec 2008-06-14 rsc type = nil;
54 01cea2ec 2008-06-14 rsc if(vtparsescore(argv[0], &type, score) < 0)
55 01cea2ec 2008-06-14 rsc sysfatal("could not parse score '%s': %r", argv[0]);
56 01cea2ec 2008-06-14 rsc if(type == nil || strcmp(type, "file") != 0)
57 01cea2ec 2008-06-14 rsc sysfatal("bad score - not file:...");
58 01cea2ec 2008-06-14 rsc
59 01cea2ec 2008-06-14 rsc buf = vtmallocz(VtMaxLumpSize);
60 01cea2ec 2008-06-14 rsc
61 01cea2ec 2008-06-14 rsc z = vtdial(host);
62 01cea2ec 2008-06-14 rsc if(z == nil)
63 01cea2ec 2008-06-14 rsc sysfatal("could not connect to server: %r");
64 01cea2ec 2008-06-14 rsc
65 01cea2ec 2008-06-14 rsc if(vtconnect(z) < 0)
66 01cea2ec 2008-06-14 rsc sysfatal("vtconnect: %r");
67 01cea2ec 2008-06-14 rsc
68 01cea2ec 2008-06-14 rsc // root block ...
69 01cea2ec 2008-06-14 rsc n = vtread(z, score, VtRootType, buf, VtMaxLumpSize);
70 01cea2ec 2008-06-14 rsc if(n < 0)
71 01cea2ec 2008-06-14 rsc sysfatal("could not read root %V: %r", score);
72 01cea2ec 2008-06-14 rsc if(n != VtRootSize)
73 01cea2ec 2008-06-14 rsc sysfatal("root block %V is wrong size %d != %d", score, n, VtRootSize);
74 01cea2ec 2008-06-14 rsc if(vtrootunpack(&root, buf) < 0)
75 01cea2ec 2008-06-14 rsc sysfatal("unpacking root block %V: %r", score);
76 01cea2ec 2008-06-14 rsc if(strcmp(root.type, "file") != 0)
77 01cea2ec 2008-06-14 rsc sysfatal("bad root type %q (not 'file')", root.type);
78 01cea2ec 2008-06-14 rsc if(chatty)
79 01cea2ec 2008-06-14 rsc fprint(2, "%V: %q %q %V %d %V\n",
80 01cea2ec 2008-06-14 rsc score, root.name, root.type,
81 01cea2ec 2008-06-14 rsc root.score, root.blocksize, root.prev);
82 01cea2ec 2008-06-14 rsc
83 01cea2ec 2008-06-14 rsc // ... points at entry block
84 01cea2ec 2008-06-14 rsc n = vtread(z, root.score, VtDirType, buf, VtMaxLumpSize);
85 01cea2ec 2008-06-14 rsc if(n < 0)
86 01cea2ec 2008-06-14 rsc sysfatal("could not read entry %V: %r", root.score);
87 01cea2ec 2008-06-14 rsc if(n != VtEntrySize)
88 01cea2ec 2008-06-14 rsc sysfatal("dir block %V is wrong size %d != %d", root.score, n, VtEntrySize);
89 01cea2ec 2008-06-14 rsc if(vtentryunpack(&e, buf, 0) < 0)
90 01cea2ec 2008-06-14 rsc sysfatal("unpacking dir block %V: %r", root.score);
91 01cea2ec 2008-06-14 rsc if((e.type&VtTypeBaseMask) != VtDataType)
92 01cea2ec 2008-06-14 rsc sysfatal("not a single file");
93 01cea2ec 2008-06-14 rsc
94 01cea2ec 2008-06-14 rsc // open and read file
95 01cea2ec 2008-06-14 rsc c = vtcachealloc(z, root.blocksize, 32);
96 01cea2ec 2008-06-14 rsc if(c == nil)
97 01cea2ec 2008-06-14 rsc sysfatal("vtcachealloc: %r");
98 01cea2ec 2008-06-14 rsc f = vtfileopenroot(c, &e);
99 01cea2ec 2008-06-14 rsc if(f == nil)
100 01cea2ec 2008-06-14 rsc sysfatal("vtfileopenroot: %r");
101 01cea2ec 2008-06-14 rsc off = 0;
102 01cea2ec 2008-06-14 rsc vtfilelock(f, VtOREAD);
103 01cea2ec 2008-06-14 rsc while((n = vtfileread(f, buf, VtMaxLumpSize, off)) > 0){
104 01cea2ec 2008-06-14 rsc write(1, buf, n);
105 01cea2ec 2008-06-14 rsc off += n;
106 01cea2ec 2008-06-14 rsc }
107 01cea2ec 2008-06-14 rsc threadexitsall(0);
108 01cea2ec 2008-06-14 rsc }