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>
9 01cea2ec 2008-06-14 rsc Blocksize = 8192
17 01cea2ec 2008-06-14 rsc fprint(2, "usage: writefile [-v] [-h host] < data\n");
18 01cea2ec 2008-06-14 rsc threadexitsall("usage");
22 01cea2ec 2008-06-14 rsc threadmain(int argc, char *argv[])
25 01cea2ec 2008-06-14 rsc uchar score[VtScoreSize];
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);
42 01cea2ec 2008-06-14 rsc chattyventi++;
45 01cea2ec 2008-06-14 rsc host = EARGF(usage());
55 01cea2ec 2008-06-14 rsc if(argc != 0)
58 01cea2ec 2008-06-14 rsc buf = vtmallocz(Blocksize);
60 01cea2ec 2008-06-14 rsc z = vtdial(host);
62 01cea2ec 2008-06-14 rsc sysfatal("could not connect to server: %r");
64 01cea2ec 2008-06-14 rsc if(vtconnect(z) < 0)
65 01cea2ec 2008-06-14 rsc sysfatal("vtconnect: %r");
67 01cea2ec 2008-06-14 rsc // write file
68 01cea2ec 2008-06-14 rsc c = vtcachealloc(z, Blocksize, 32);
70 01cea2ec 2008-06-14 rsc sysfatal("vtcachealloc: %r");
71 01cea2ec 2008-06-14 rsc f = vtfilecreateroot(c, Blocksize, Blocksize, VtDataType);
73 01cea2ec 2008-06-14 rsc sysfatal("vtfilecreateroot: %r");
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");
80 01cea2ec 2008-06-14 rsc if(vtfileflushbefore(f, off) < 0)
81 01cea2ec 2008-06-14 rsc sysfatal("vtfileflushbefore: %r");
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);
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");
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");
103 01cea2ec 2008-06-14 rsc print("file:%V\n", score);
104 01cea2ec 2008-06-14 rsc threadexitsall(0);