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>
12 01cea2ec 2008-06-14 rsc fprint(2, "usage: readfile [-v] [-h host] score\n");
13 01cea2ec 2008-06-14 rsc threadexitsall("usage");
17 01cea2ec 2008-06-14 rsc threadmain(int argc, char *argv[])
20 01cea2ec 2008-06-14 rsc uchar score[VtScoreSize];
22 01cea2ec 2008-06-14 rsc char *host, *type;
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);
37 01cea2ec 2008-06-14 rsc chattyventi++;
40 01cea2ec 2008-06-14 rsc host = EARGF(usage());
50 01cea2ec 2008-06-14 rsc if(argc != 1)
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:...");
59 01cea2ec 2008-06-14 rsc buf = vtmallocz(VtMaxLumpSize);
61 01cea2ec 2008-06-14 rsc z = vtdial(host);
63 01cea2ec 2008-06-14 rsc sysfatal("could not connect to server: %r");
65 01cea2ec 2008-06-14 rsc if(vtconnect(z) < 0)
66 01cea2ec 2008-06-14 rsc sysfatal("vtconnect: %r");
68 01cea2ec 2008-06-14 rsc // root block ...
69 01cea2ec 2008-06-14 rsc n = vtread(z, score, VtRootType, buf, VtMaxLumpSize);
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);
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);
83 01cea2ec 2008-06-14 rsc // ... points at entry block
84 01cea2ec 2008-06-14 rsc n = vtread(z, root.score, VtDirType, buf, VtMaxLumpSize);
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");
94 01cea2ec 2008-06-14 rsc // open and read file
95 01cea2ec 2008-06-14 rsc c = vtcachealloc(z, root.blocksize, 32);
97 01cea2ec 2008-06-14 rsc sysfatal("vtcachealloc: %r");
98 01cea2ec 2008-06-14 rsc f = vtfileopenroot(c, &e);
100 01cea2ec 2008-06-14 rsc sysfatal("vtfileopenroot: %r");
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);
107 01cea2ec 2008-06-14 rsc threadexitsall(0);