changelog shortlog tags branches changeset files revisions annotate raw help

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

changeset 122: 84abb5444a76
parent: c6c061169cb7
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 Ventiget;
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 "string.m";
11 include "venti.m";
12 include "vac.m";
13 
14 str: String;
15 venti: Venti;
16 vac: Vac;
17 
18 print, sprint, fprint, fildes: import sys;
19 Score, Session: import venti;
20 Dirtype, Datatype: import venti;
21 Entry, Entrysize, Vacfile: import vac;
22 
23 Ventiget: module {
24  init: fn(nil: ref Draw->Context, args: list of string);
25 };
26 
27 addr := "net!$venti!venti";
28 dflag := 0;
29 session: ref Session;
30 
31 init(nil: ref Draw->Context, args: list of string)
32 {
33  sys = load Sys Sys->PATH;
34  bufio = load Bufio Bufio->PATH;
35  arg := load Arg Arg->PATH;
36  str = load String String->PATH;
37  venti = load Venti Venti->PATH;
38  vac = load Vac Vac->PATH;
39 
40  venti->init();
41  vac->init();
42 
43  arg->init(args);
44  arg->setusage(sprint("%s [-d] [-a addr] [entry:]score", arg->progname()));
45  while((c := arg->opt()) != 0)
46  case c {
47  'a' => addr = arg->earg();
48  'd' => dflag++;
49  * => arg->usage();
50  }
51  args = arg->argv();
52  if(len args != 1)
53  arg->usage();
54 
55  (tag, scorestr) := str->splitstrr(hd args, ":");
56  if(tag != nil)
57  tag = tag[:len tag-1];
58  if(tag == nil)
59  tag = "entry";
60  if(tag != "entry")
61  error("bad score type: "+tag);
62 
63  (sok, score) := Score.parse(scorestr);
64  if(sok != 0)
65  error("bad score: "+scorestr);
66  say("have score");
67 
68  (cok, conn) := sys->dial(addr, nil);
69  if(cok < 0)
70  error(sprint("dialing %s: %r", addr));
71  say("have connection");
72 
73  fd := conn.dfd;
74  session = Session.new(fd);
75  if(session == nil)
76  error(sprint("handshake: %r"));
77  say("have handshake");
78 
79  d := session.read(score, Dirtype, Entrysize);
80  if(d == nil)
81  error(sprint("reading entry: %r"));
82  e := Entry.unpack(d);
83  if(e == nil)
84  error(sprint("unpacking entry: %r"));
85  say("have entry");
86 
87  bio := bufio->fopen(fildes(1), bufio->OWRITE);
88  if(bio == nil)
89  error(sprint("bufio fopen: %r"));
90 
91  say("reading");
92  buf := array[sys->ATOMICIO] of byte;
93  vf := Vacfile.new(session, e);
94  for(;;) {
95  rn := vf.read(buf, len buf);
96  if(rn == 0)
97  break;
98  if(rn < 0)
99  error(sprint("reading: %r"));
100  wn := bio.write(buf, rn);
101  if(wn != rn)
102  error(sprint("writing: %r"));
103  }
104  bok := bio.flush();
105  bio.close();
106  if(bok == bufio->ERROR || bok == bufio->EOF)
107  error(sprint("bufio close: %r"));
108  say("done");
109 }
110 
111 error(s: string)
112 {
113  fprint(fildes(2), "%s\n", s);
114  raise "fail:"+s;
115 }
116 
117 say(s: string)
118 {
119  if(dflag)
120  fprint(fildes(2), "%s\n", s);
121 }