]> arthur.barton.de Git - bup.git/blob - cmd/server-cmd.py
midx4: midx2 with idx backreferences
[bup.git] / cmd / server-cmd.py
1 #!/usr/bin/env python
2 import os, sys, struct
3 from bup import options, git
4 from bup.helpers import *
5
6 suspended_w = None
7 dumb_server_mode = False
8
9 def _set_mode():
10     global dumb_server_mode
11     dumb_server_mode = os.path.exists(git.repo('bup-dumb-server'))
12     debug1('bup server: serving in %s mode\n' 
13            % (dumb_server_mode and 'dumb' or 'smart'))
14
15
16 def init_dir(conn, arg):
17     git.init_repo(arg)
18     debug1('bup server: bupdir initialized: %r\n' % git.repodir)
19     _set_mode()
20     conn.ok()
21
22
23 def set_dir(conn, arg):
24     git.check_repo_or_die(arg)
25     debug1('bup server: bupdir is %r\n' % git.repodir)
26     _set_mode()
27     conn.ok()
28
29     
30 def list_indexes(conn, junk):
31     git.check_repo_or_die()
32     suffix = ''
33     if dumb_server_mode:
34         suffix = ' load'
35     for f in os.listdir(git.repo('objects/pack')):
36         if f.endswith('.idx'):
37             conn.write('%s%s\n' % (f, suffix))
38     conn.ok()
39
40
41 def send_index(conn, name):
42     git.check_repo_or_die()
43     assert(name.find('/') < 0)
44     assert(name.endswith('.idx'))
45     idx = git.open_idx(git.repo('objects/pack/%s' % name))
46     conn.write(struct.pack('!I', len(idx.map)))
47     conn.write(idx.map)
48     conn.ok()
49
50
51 def receive_objects_v2(conn, junk):
52     global suspended_w
53     git.check_repo_or_die()
54     suggested = set()
55     if suspended_w:
56         w = suspended_w
57         suspended_w = None
58     else:
59         if dumb_server_mode:
60             w = git.PackWriter(objcache_maker=None)
61         else:
62             w = git.PackWriter()
63     while 1:
64         ns = conn.read(4)
65         if not ns:
66             w.abort()
67             raise Exception('object read: expected length header, got EOF\n')
68         n = struct.unpack('!I', ns)[0]
69         #debug2('expecting %d bytes\n' % n)
70         if not n:
71             debug1('bup server: received %d object%s.\n' 
72                 % (w.count, w.count!=1 and "s" or ''))
73             fullpath = w.close(run_midx=not dumb_server_mode)
74             if fullpath:
75                 (dir, name) = os.path.split(fullpath)
76                 conn.write('%s.idx\n' % name)
77             conn.ok()
78             return
79         elif n == 0xffffffff:
80             debug2('bup server: receive-objects suspended.\n')
81             suspended_w = w
82             conn.ok()
83             return
84             
85         shar = conn.read(20)
86         crcr = struct.unpack('!I', conn.read(4))[0]
87         n -= 20 + 4
88         buf = conn.read(n)  # object sizes in bup are reasonably small
89         #debug2('read %d bytes\n' % n)
90         _check(w, n, len(buf), 'object read: expected %d bytes, got %d\n')
91         if not dumb_server_mode:
92             oldpack = w.exists(shar, want_source=True)
93             if oldpack:
94                 assert(not oldpack == True)
95                 assert(oldpack.endswith('.idx'))
96                 (dir,name) = os.path.split(oldpack)
97                 if not (name in suggested):
98                     debug1("bup server: suggesting index %s\n" % name)
99                     conn.write('index %s\n' % name)
100                     suggested.add(name)
101                 continue
102         nw, crc = w._raw_write((buf,), sha=shar)
103         _check(w, crcr, crc, 'object read: expected crc %d, got %d\n')
104     # NOTREACHED
105     
106
107 def _check(w, expected, actual, msg):
108     if expected != actual:
109         w.abort()
110         raise Exception(msg % (expected, actual))
111
112
113 def read_ref(conn, refname):
114     git.check_repo_or_die()
115     r = git.read_ref(refname)
116     conn.write('%s\n' % (r or '').encode('hex'))
117     conn.ok()
118
119
120 def update_ref(conn, refname):
121     git.check_repo_or_die()
122     newval = conn.readline().strip()
123     oldval = conn.readline().strip()
124     git.update_ref(refname, newval.decode('hex'), oldval.decode('hex'))
125     conn.ok()
126
127
128 cat_pipe = None
129 def cat(conn, id):
130     global cat_pipe
131     git.check_repo_or_die()
132     if not cat_pipe:
133         cat_pipe = git.CatPipe()
134     try:
135         for blob in cat_pipe.join(id):
136             conn.write(struct.pack('!I', len(blob)))
137             conn.write(blob)
138     except KeyError, e:
139         log('server: error: %s\n' % e)
140         conn.write('\0\0\0\0')
141         conn.error(e)
142     else:
143         conn.write('\0\0\0\0')
144         conn.ok()
145
146
147 optspec = """
148 bup server
149 """
150 o = options.Options(optspec)
151 (opt, flags, extra) = o.parse(sys.argv[1:])
152
153 if extra:
154     o.fatal('no arguments expected')
155
156 debug2('bup server: reading from stdin.\n')
157
158 commands = {
159     'init-dir': init_dir,
160     'set-dir': set_dir,
161     'list-indexes': list_indexes,
162     'send-index': send_index,
163     'receive-objects-v2': receive_objects_v2,
164     'read-ref': read_ref,
165     'update-ref': update_ref,
166     'cat': cat,
167 }
168
169 # FIXME: this protocol is totally lame and not at all future-proof.
170 # (Especially since we abort completely as soon as *anything* bad happens)
171 conn = Conn(sys.stdin, sys.stdout)
172 lr = linereader(conn)
173 for _line in lr:
174     line = _line.strip()
175     if not line:
176         continue
177     debug1('bup server: command: %r\n' % line)
178     words = line.split(' ', 1)
179     cmd = words[0]
180     rest = len(words)>1 and words[1] or ''
181     if cmd == 'quit':
182         break
183     else:
184         cmd = commands.get(cmd)
185         if cmd:
186             cmd(conn, rest)
187         else:
188             raise Exception('unknown server command: %r\n' % line)
189
190 debug1('bup server: done\n')