changelog shortlog tags branches changeset files revisions annotate raw help

Mercurial > hg > ventivac / appl/cmd/venti/put.b

changeset 121: 7fd24f62e50a
parent: c6c061169cb7
child: 84abb5444a76
author: Mechiel Lukkien <mechiel@ueber.net>
date: Fri, 17 Aug 2007 22:25:28 +0200
permissions: -rw-r--r--
description: move default rabin parameters to vac.m
1 implement Ventiput;
2 
3 include "sys.m";
4  sys: Sys;
5 include "draw.m";
6 include "bufio.m";
7  bufio: Bufio;
8  Iobuf: import bufio;
9 include "arg.m";
10 include "venti.m";
11 include "vac.m";
12 include "rabin.m";
13 
14 venti: Venti;
15 vac: Vac;
16 rabin: Rabin;
17 
18 print, sprint, fprint, fildes: import sys;
19 Score, Session: import venti;
20 Dirtype, Datatype: import venti;
21 Entry, File: import vac;
22 Rcfg, Rfile: import rabin;
23 
24 Ventiput: module {
25  init: fn(nil: ref Draw->Context, args: list of string);
26 };
27 
28 addr := "net!$venti!venti";
29 dflag := 0;
30 rflag := 0;
31 blocksize := vac->Dsize;
32 session: ref Session;
33 
34 init(nil: ref Draw->Context, args: list of string)
35 {
36  sys = load Sys Sys->PATH;
37  bufio = load Bufio Bufio->PATH;
38  arg := load Arg Arg->PATH;
39  venti = load Venti Venti->PATH;
40  vac = load Vac Vac->PATH;
41  rabin = load Rabin Rabin->PATH;
42 
43  venti->init();
44  vac->init();
45  rabin->init(bufio);
46 
47  prime := Vac->Rabinprime;
48  mod := Vac->Rabinmod;
49  width := Vac->Rabinwidth;
50  blockmin := Vac->Rabinblockmin;
51  blockmax := Vac->Rabinblockmax;
52 
53  arg->init(args);
54  arg->setusage(sprint("%s [-dr] [-a addr] [-b blocksize]", arg->progname()));
55  while((c := arg->opt()) != 0)
56  case c {
57  'a' => addr = arg->earg();
58  'b' => blocksize = int arg->earg();
59  'r' => rflag++;
60  'd' => dflag++;
61  vac->dflag++;
62  * => arg->usage();
63  }
64  args = arg->argv();
65  if(len args != 0)
66  arg->usage();
67 
68  rcfg: ref Rcfg;
69  if(rflag) {
70  err: string;
71  (rcfg, err) = Rcfg.mk(prime, width, mod);
72  if(err != nil)
73  error("rabincfg: "+err);
74  }
75 
76  (cok, conn) := sys->dial(addr, nil);
77  if(cok < 0)
78  error(sprint("dialing %s: %r", addr));
79  say("have connection");
80 
81  session = Session.new(conn.dfd);
82  if(session == nil)
83  error(sprint("handshake: %r"));
84  say("have handshake");
85 
86  bio := bufio->fopen(fildes(0), bufio->OREAD);
87  if(bio == nil)
88  error(sprint("bufio open: %r"));
89 
90  rfile: ref Rfile;
91  if(rflag) {
92  err: string;
93  (rfile, err) = rabin->open(rcfg, bio, blockmin, blockmax);
94  if(err != nil)
95  error("rabin open: "+err);
96  }
97 
98  say("writing");
99  f := File.new(session, Datatype, blocksize, rflag);
100  for(;;) {
101  if(rflag) {
102  (d, nil, err) := rfile.read();
103  if(err != nil)
104  error("reading: "+err);
105  if(len d == 0)
106  break;
107  if(f.write(d) < 0)
108  error(sprint("writing: %r"));
109  } else {
110  buf := array[blocksize] of byte;
111  n := 0;
112  while(n < len buf) {
113  want := len buf - n;
114  have := bio.read(buf[n:], want);
115  if(have == 0)
116  break;
117  if(have < 0)
118  error(sprint("reading: %r"));
119  n += have;
120  }
121  if(dflag) say(sprint("have buf, length %d", n));
122 
123  if(f.write(buf[:n]) < 0)
124  error(sprint("writing: %r"));
125  if(n != len buf)
126  break;
127  }
128  }
129  bio.close();
130  e := f.finish();
131  if(e == nil)
132  error(sprint("flushing: %r"));
133  d := e.pack();
134 
135  (rok, rscore) := session.write(Dirtype, d);
136  if(rok < 0)
137  error(sprint("writing root score: %r"));
138  say("entry written, "+rscore.text());
139  print("entry:%s\n", rscore.text());
140 
141  if(session.sync() < 0)
142  error(sprint("syncing server: %r"));
143 }
144 
145 error(s: string)
146 {
147  fprint(fildes(2), "%s\n", s);
148  raise "fail:"+s;
149 }
150 
151 say(s: string)
152 {
153  if(dflag)
154  fprint(fildes(2), "%s\n", s);
155 }