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 enum
8 01cea2ec 2008-06-14 rsc {
9 01cea2ec 2008-06-14 rsc Blocksize = 8192
10 01cea2ec 2008-06-14 rsc };
11 01cea2ec 2008-06-14 rsc
12 01cea2ec 2008-06-14 rsc int chatty;
13 01cea2ec 2008-06-14 rsc
14 01cea2ec 2008-06-14 rsc void
15 01cea2ec 2008-06-14 rsc usage(void)
16 01cea2ec 2008-06-14 rsc {
17 01cea2ec 2008-06-14 rsc fprint(2, "usage: writefile [-v] [-h host] < data\n");
18 01cea2ec 2008-06-14 rsc threadexitsall("usage");
19 01cea2ec 2008-06-14 rsc }
20 01cea2ec 2008-06-14 rsc
21 01cea2ec 2008-06-14 rsc void
22 01cea2ec 2008-06-14 rsc threadmain(int argc, char *argv[])
23 01cea2ec 2008-06-14 rsc {
24 01cea2ec 2008-06-14 rsc int n;
25 01cea2ec 2008-06-14 rsc uchar score[VtScoreSize];
26 01cea2ec 2008-06-14 rsc uchar *buf;
27 01cea2ec 2008-06-14 rsc char *host;
28 01cea2ec 2008-06-14 rsc vlong off;
29 01cea2ec 2008-06-14 rsc VtEntry e;
30 01cea2ec 2008-06-14 rsc VtRoot root;
31 01cea2ec 2008-06-14 rsc VtCache *c;
32 01cea2ec 2008-06-14 rsc VtConn *z;
33 01cea2ec 2008-06-14 rsc VtFile *f;
34 01cea2ec 2008-06-14 rsc
35 01cea2ec 2008-06-14 rsc quotefmtinstall();
36 01cea2ec 2008-06-14 rsc fmtinstall('F', vtfcallfmt);
37 01cea2ec 2008-06-14 rsc fmtinstall('V', vtscorefmt);
38 01cea2ec 2008-06-14 rsc
39 01cea2ec 2008-06-14 rsc host = nil;
40 01cea2ec 2008-06-14 rsc ARGBEGIN{
41 01cea2ec 2008-06-14 rsc case 'V':
42 01cea2ec 2008-06-14 rsc chattyventi++;
43 01cea2ec 2008-06-14 rsc break;
44 01cea2ec 2008-06-14 rsc case 'h':
45 01cea2ec 2008-06-14 rsc host = EARGF(usage());
46 01cea2ec 2008-06-14 rsc break;
47 01cea2ec 2008-06-14 rsc case 'v':
48 01cea2ec 2008-06-14 rsc chatty++;
49 01cea2ec 2008-06-14 rsc break;
50 01cea2ec 2008-06-14 rsc default:
51 01cea2ec 2008-06-14 rsc usage();
52 01cea2ec 2008-06-14 rsc break;
53 01cea2ec 2008-06-14 rsc }ARGEND
54 01cea2ec 2008-06-14 rsc
55 01cea2ec 2008-06-14 rsc if(argc != 0)
56 01cea2ec 2008-06-14 rsc usage();
57 01cea2ec 2008-06-14 rsc
58 01cea2ec 2008-06-14 rsc buf = vtmallocz(Blocksize);
59 01cea2ec 2008-06-14 rsc
60 01cea2ec 2008-06-14 rsc z = vtdial(host);
61 01cea2ec 2008-06-14 rsc if(z == nil)
62 01cea2ec 2008-06-14 rsc sysfatal("could not connect to server: %r");
63 01cea2ec 2008-06-14 rsc
64 01cea2ec 2008-06-14 rsc if(vtconnect(z) < 0)
65 01cea2ec 2008-06-14 rsc sysfatal("vtconnect: %r");
66 01cea2ec 2008-06-14 rsc
67 01cea2ec 2008-06-14 rsc // write file
68 db60da46 2011-11-08 rsc c = vtcachealloc(z, Blocksize*32);
69 01cea2ec 2008-06-14 rsc if(c == nil)
70 01cea2ec 2008-06-14 rsc sysfatal("vtcachealloc: %r");
71 01cea2ec 2008-06-14 rsc f = vtfilecreateroot(c, Blocksize, Blocksize, VtDataType);
72 01cea2ec 2008-06-14 rsc if(f == nil)
73 01cea2ec 2008-06-14 rsc sysfatal("vtfilecreateroot: %r");
74 01cea2ec 2008-06-14 rsc off = 0;
75 01cea2ec 2008-06-14 rsc vtfilelock(f, VtOWRITE);
76 01cea2ec 2008-06-14 rsc while((n = read(0, buf, Blocksize)) > 0){
77 01cea2ec 2008-06-14 rsc if(vtfilewrite(f, buf, n, off) != n)
78 01cea2ec 2008-06-14 rsc sysfatal("vtfilewrite: %r");
79 01cea2ec 2008-06-14 rsc off += n;
80 01cea2ec 2008-06-14 rsc if(vtfileflushbefore(f, off) < 0)
81 01cea2ec 2008-06-14 rsc sysfatal("vtfileflushbefore: %r");
82 01cea2ec 2008-06-14 rsc }
83 01cea2ec 2008-06-14 rsc if(vtfileflush(f) < 0)
84 01cea2ec 2008-06-14 rsc sysfatal("vtfileflush: %r");
85 01cea2ec 2008-06-14 rsc if(vtfilegetentry(f, &e) < 0)
86 01cea2ec 2008-06-14 rsc sysfatal("vtfilegetentry: %r");
87 01cea2ec 2008-06-14 rsc vtfileunlock(f);
88 fa325e9b 2020-01-10 cross
89 01cea2ec 2008-06-14 rsc // write directory entry
90 01cea2ec 2008-06-14 rsc memset(&root, 0, sizeof root);
91 01cea2ec 2008-06-14 rsc vtentrypack(&e, buf, 0);
92 01cea2ec 2008-06-14 rsc if(vtwrite(z, root.score, VtDirType, buf, VtEntrySize) < 0)
93 01cea2ec 2008-06-14 rsc sysfatal("vtwrite dir: %r");
94 fa325e9b 2020-01-10 cross
95 01cea2ec 2008-06-14 rsc // write root
96 01cea2ec 2008-06-14 rsc strcpy(root.name, "data");
97 01cea2ec 2008-06-14 rsc strcpy(root.type, "file");
98 01cea2ec 2008-06-14 rsc root.blocksize = Blocksize;
99 01cea2ec 2008-06-14 rsc vtrootpack(&root, buf);
100 01cea2ec 2008-06-14 rsc if(vtwrite(z, score, VtRootType, buf, VtRootSize) < 0)
101 01cea2ec 2008-06-14 rsc sysfatal("vtwrite root: %r");
102 fa325e9b 2020-01-10 cross
103 01cea2ec 2008-06-14 rsc print("file:%V\n", score);
104 01cea2ec 2008-06-14 rsc threadexitsall(0);
105 01cea2ec 2008-06-14 rsc }