3 bup_python="$(dirname "$0")/bup-python" || exit $?
4 exec "$bup_python" "$0" ${1+"$@"}
8 from __future__ import absolute_import
9 import os, sys, struct, subprocess
11 from bup import options, git, vfs, vint
12 from bup.git import MissingObject
13 from bup.helpers import (Conn, debug1, debug2, linereader, lines_until_sentinel,
15 from bup.repo import LocalRepo
19 dumb_server_mode = False
22 def do_help(conn, junk):
23 conn.write('Commands:\n %s\n' % '\n '.join(sorted(commands)))
28 global dumb_server_mode
29 dumb_server_mode = os.path.exists(git.repo('bup-dumb-server'))
30 debug1('bup server: serving in %s mode\n'
31 % (dumb_server_mode and 'dumb' or 'smart'))
34 def _init_session(reinit_with_new_repopath=None):
36 if reinit_with_new_repopath is None and git.repodir:
40 git.check_repo_or_die(reinit_with_new_repopath)
44 # OK. we now know the path is a proper repository. Record this path in the
45 # environment so that subprocesses inherit it and know where to operate.
46 os.environ['BUP_DIR'] = git.repodir
47 debug1('bup server: bupdir is %r\n' % git.repodir)
51 def init_dir(conn, arg):
53 debug1('bup server: bupdir initialized: %r\n' % git.repodir)
58 def set_dir(conn, arg):
63 def list_indexes(conn, junk):
68 for f in os.listdir(git.repo('objects/pack')):
69 if f.endswith('.idx'):
70 conn.write('%s%s\n' % (f, suffix))
74 def send_index(conn, name):
76 assert(name.find('/') < 0)
77 assert(name.endswith('.idx'))
78 idx = git.open_idx(git.repo('objects/pack/%s' % name))
79 conn.write(struct.pack('!I', len(idx.map)))
84 def receive_objects_v2(conn, junk):
93 w = git.PackWriter(objcache_maker=None)
100 raise Exception('object read: expected length header, got EOF\n')
101 n = struct.unpack('!I', ns)[0]
102 #debug2('expecting %d bytes\n' % n)
104 debug1('bup server: received %d object%s.\n'
105 % (w.count, w.count!=1 and "s" or ''))
106 fullpath = w.close(run_midx=not dumb_server_mode)
108 (dir, name) = os.path.split(fullpath)
109 conn.write('%s.idx\n' % name)
112 elif n == 0xffffffff:
113 debug2('bup server: receive-objects suspended.\n')
119 crcr = struct.unpack('!I', conn.read(4))[0]
121 buf = conn.read(n) # object sizes in bup are reasonably small
122 #debug2('read %d bytes\n' % n)
123 _check(w, n, len(buf), 'object read: expected %d bytes, got %d\n')
124 if not dumb_server_mode:
125 oldpack = w.exists(shar, want_source=True)
127 assert(not oldpack == True)
128 assert(oldpack.endswith('.idx'))
129 (dir,name) = os.path.split(oldpack)
130 if not (name in suggested):
131 debug1("bup server: suggesting index %s\n"
132 % git.shorten_hash(name))
133 debug1("bup server: because of object %s\n"
134 % shar.encode('hex'))
135 conn.write('index %s\n' % name)
138 nw, crc = w._raw_write((buf,), sha=shar)
139 _check(w, crcr, crc, 'object read: expected crc %d, got %d\n')
143 def _check(w, expected, actual, msg):
144 if expected != actual:
146 raise Exception(msg % (expected, actual))
149 def read_ref(conn, refname):
151 r = git.read_ref(refname)
152 conn.write('%s\n' % (r or '').encode('hex'))
156 def update_ref(conn, refname):
158 newval = conn.readline().strip()
159 oldval = conn.readline().strip()
160 git.update_ref(refname, newval.decode('hex'), oldval.decode('hex'))
166 for blob in git.cp().join(id):
167 conn.write(struct.pack('!I', len(blob)))
169 except KeyError as e:
170 log('server: error: %s\n' % e)
171 conn.write('\0\0\0\0')
174 conn.write('\0\0\0\0')
177 def cat_batch(conn, dummy):
180 # For now, avoid potential deadlock by just reading them all
181 for ref in tuple(lines_until_sentinel(conn, '\n', Exception)):
183 it = cat_pipe.get(ref)
186 conn.write('missing\n')
188 conn.write('%s %s %d\n' % info)
193 def refs(conn, args):
194 limit_to_heads, limit_to_tags = args.split()
195 assert limit_to_heads in ('0', '1')
196 assert limit_to_tags in ('0', '1')
197 limit_to_heads = int(limit_to_heads)
198 limit_to_tags = int(limit_to_tags)
200 patterns = tuple(x[:-1] for x in lines_until_sentinel(conn, '\n', Exception))
201 for name, oid in git.list_refs(patterns=patterns,
202 limit_to_heads=limit_to_heads,
203 limit_to_tags=limit_to_tags):
204 assert '\n' not in name
205 conn.write('%s %s\n' % (oid.encode('hex'), name))
209 def rev_list(conn, _):
211 count = conn.readline()
213 raise Exception('Unexpected EOF while reading rev-list count')
214 count = None if count == '\n' else int(count)
215 fmt = conn.readline()
217 raise Exception('Unexpected EOF while reading rev-list format')
218 fmt = None if fmt == '\n' else fmt[:-1]
219 refs = tuple(x[:-1] for x in lines_until_sentinel(conn, '\n', Exception))
220 args = git.rev_list_invocation(refs, count=count, format=fmt)
221 p = subprocess.Popen(git.rev_list_invocation(refs, count=count, format=fmt),
222 preexec_fn=git._gitenv(git.repodir),
223 stdout=subprocess.PIPE)
225 out = p.stdout.read(64 * 1024)
230 rv = p.wait() # not fatal
232 msg = 'git rev-list returned error %d' % rv
237 def resolve(conn, args):
239 (flags,) = args.split()
241 want_meta = bool(flags & 1)
242 follow = bool(flags & 2)
243 have_parent = bool(flags & 4)
244 parent = vfs.read_resolution(conn) if have_parent else None
245 path = vint.read_bvec(conn)
247 raise Exception('Empty resolve path')
249 res = list(vfs.resolve(repo, path, parent=parent, want_meta=want_meta,
251 except vfs.IOError as ex:
253 if isinstance(res, vfs.IOError):
254 conn.write(b'\0') # error
255 vfs.write_ioerror(conn, res)
257 conn.write(b'\1') # success
258 vfs.write_resolution(conn, res)
264 o = options.Options(optspec)
265 (opt, flags, extra) = o.parse(sys.argv[1:])
268 o.fatal('no arguments expected')
270 debug2('bup server: reading from stdin.\n')
275 'init-dir': init_dir,
277 'list-indexes': list_indexes,
278 'send-index': send_index,
279 'receive-objects-v2': receive_objects_v2,
280 'read-ref': read_ref,
281 'update-ref': update_ref,
283 'cat': join, # apocryphal alias
284 'cat-batch' : cat_batch,
286 'rev-list': rev_list,
290 # FIXME: this protocol is totally lame and not at all future-proof.
291 # (Especially since we abort completely as soon as *anything* bad happens)
292 conn = Conn(sys.stdin, sys.stdout)
293 lr = linereader(conn)
298 debug1('bup server: command: %r\n' % line)
299 words = line.split(' ', 1)
301 rest = len(words)>1 and words[1] or ''
305 cmd = commands.get(cmd)
309 raise Exception('unknown server command: %r\n' % line)
311 debug1('bup server: done\n')