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