changelog shortlog tags branches changeset files revisions annotate raw help

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

changeset 122: 84abb5444a76
parent: 7fd24f62e50a
child: 7f377ffc9ad3
author: Mechiel Lukkien <mechiel@ueber.net>
date: Fri, 17 Aug 2007 22:28:53 +0200
permissions: -rw-r--r--
description: remove dflag from vac library interface. doesn't belong there anymore.
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  * => arg->usage();
62  }
63  args = arg->argv();
64  if(len args != 0)
65  arg->usage();
66 
67  rcfg: ref Rcfg;
68  if(rflag) {
69  err: string;
70  (rcfg, err) = Rcfg.mk(prime, width, mod);
71  if(err != nil)
72  error("rabincfg: "+err);
73  }
74 
75  (cok, conn) := sys->dial(addr, nil);
76  if(cok < 0)
77  error(sprint("dialing %s: %r", addr));
78  say("have connection");
79 
80  session = Session.new(conn.dfd);
81  if(session == nil)
82  error(sprint("handshake: %r"));
83  say("have handshake");
84 
85  bio := bufio->fopen(fildes(0), bufio->OREAD);
86  if(bio == nil)
87  error(sprint("bufio open: %r"));
88 
89  rfile: ref Rfile;
90  if(rflag) {
91  err: string;
92  (rfile, err) = rabin->open(rcfg, bio, blockmin, blockmax);
93  if(err != nil)
94  error("rabin open: "+err);
95  }
96 
97  say("writing");
98  f := File.new(session, Datatype, blocksize, rflag);
99  for(;;) {
100  if(rflag) {
101  (d, nil, err) := rfile.read();
102  if(err != nil)
103  error("reading: "+err);
104  if(len d == 0)
105  break;
106  if(f.write(d) < 0)
107  error(sprint("writing: %r"));
108  } else {
109  buf := array[blocksize] of byte;
110  n := 0;
111  while(n < len buf) {
112  want := len buf - n;
113  have := bio.read(buf[n:], want);
114  if(have == 0)
115  break;
116  if(have < 0)
117  error(sprint("reading: %r"));
118  n += have;
119  }
120  if(dflag) say(sprint("have buf, length %d", n));
121 
122  if(f.write(buf[:n]) < 0)
123  error(sprint("writing: %r"));
124  if(n != len buf)
125  break;
126  }
127  }
128  bio.close();
129  e := f.finish();
130  if(e == nil)
131  error(sprint("flushing: %r"));
132  d := e.pack();
133 
134  (rok, rscore) := session.write(Dirtype, d);
135  if(rok < 0)
136  error(sprint("writing root score: %r"));
137  say("entry written, "+rscore.text());
138  print("entry:%s\n", rscore.text());
139 
140  if(session.sync() < 0)
141  error(sprint("syncing server: %r"));
142 }
143 
144 error(s: string)
145 {
146  fprint(fildes(2), "%s\n", s);
147  raise "fail:"+s;
148 }
149 
150 say(s: string)
151 {
152  if(dflag)
153  fprint(fildes(2), "%s\n", s);
154 }