3 # https://sourceware.org/bugzilla/show_bug.cgi?id=26034
4 export "BUP_ARGV_0"="$0"
7 export "BUP_ARGV_${arg_i}"="$arg"
11 # Here to end of preamble replaced during install
12 bup_python="$(dirname "$0")/../../config/bin/python" || exit $?
13 exec "$bup_python" "$0"
17 from __future__ import absolute_import
18 from binascii import hexlify, unhexlify
19 import os, struct, subprocess, sys
21 sys.path[:0] = [os.path.dirname(os.path.realpath(__file__)) + '/..']
23 from bup import compat, options, git, vfs, vint
24 from bup.compat import environ, hexstr
25 from bup.git import MissingObject
26 from bup.helpers import (Conn, debug1, debug2, linereader, lines_until_sentinel,
28 from bup.io import byte_stream, path_msg
29 from bup.repo import LocalRepo
33 dumb_server_mode = False
36 def do_help(conn, junk):
37 conn.write(b'Commands:\n %s\n' % b'\n '.join(sorted(commands)))
42 global dumb_server_mode
43 dumb_server_mode = os.path.exists(git.repo(b'bup-dumb-server'))
44 debug1('bup server: serving in %s mode\n'
45 % (dumb_server_mode and 'dumb' or 'smart'))
48 def _init_session(reinit_with_new_repopath=None):
50 if reinit_with_new_repopath is None and git.repodir:
54 git.check_repo_or_die(reinit_with_new_repopath)
58 # OK. we now know the path is a proper repository. Record this path in the
59 # environment so that subprocesses inherit it and know where to operate.
60 environ[b'BUP_DIR'] = git.repodir
61 debug1('bup server: bupdir is %s\n' % path_msg(git.repodir))
65 def init_dir(conn, arg):
67 debug1('bup server: bupdir initialized: %s\n' % path_msg(git.repodir))
72 def set_dir(conn, arg):
77 def list_indexes(conn, junk):
82 for f in os.listdir(git.repo(b'objects/pack')):
83 if f.endswith(b'.idx'):
84 conn.write(b'%s%s\n' % (f, suffix))
88 def send_index(conn, name):
90 assert name.find(b'/') < 0
91 assert name.endswith(b'.idx')
92 idx = git.open_idx(git.repo(b'objects/pack/%s' % name))
93 conn.write(struct.pack('!I', len(idx.map)))
98 def receive_objects_v2(conn, junk):
107 w = git.PackWriter(objcache_maker=None)
114 raise Exception('object read: expected length header, got EOF\n')
115 n = struct.unpack('!I', ns)[0]
116 #debug2('expecting %d bytes\n' % n)
118 debug1('bup server: received %d object%s.\n'
119 % (w.count, w.count!=1 and "s" or ''))
120 fullpath = w.close(run_midx=not dumb_server_mode)
122 (dir, name) = os.path.split(fullpath)
123 conn.write(b'%s.idx\n' % name)
126 elif n == 0xffffffff:
127 debug2('bup server: receive-objects suspended.\n')
133 crcr = struct.unpack('!I', conn.read(4))[0]
135 buf = conn.read(n) # object sizes in bup are reasonably small
136 #debug2('read %d bytes\n' % n)
137 _check(w, n, len(buf), 'object read: expected %d bytes, got %d\n')
138 if not dumb_server_mode:
139 oldpack = w.exists(shar, want_source=True)
141 assert(not oldpack == True)
142 assert(oldpack.endswith(b'.idx'))
143 (dir,name) = os.path.split(oldpack)
144 if not (name in suggested):
145 debug1("bup server: suggesting index %s\n"
146 % git.shorten_hash(name).decode('ascii'))
147 debug1("bup server: because of object %s\n"
149 conn.write(b'index %s\n' % name)
152 nw, crc = w._raw_write((buf,), sha=shar)
153 _check(w, crcr, crc, 'object read: expected crc %d, got %d\n')
157 def _check(w, expected, actual, msg):
158 if expected != actual:
160 raise Exception(msg % (expected, actual))
163 def read_ref(conn, refname):
165 r = git.read_ref(refname)
166 conn.write(b'%s\n' % hexlify(r) if r else b'')
170 def update_ref(conn, refname):
172 newval = conn.readline().strip()
173 oldval = conn.readline().strip()
174 git.update_ref(refname, unhexlify(newval), unhexlify(oldval))
180 for blob in git.cp().join(id):
181 conn.write(struct.pack('!I', len(blob)))
183 except KeyError as e:
184 log('server: error: %s\n' % e)
185 conn.write(b'\0\0\0\0')
188 conn.write(b'\0\0\0\0')
191 def cat_batch(conn, dummy):
194 # For now, avoid potential deadlock by just reading them all
195 for ref in tuple(lines_until_sentinel(conn, b'\n', Exception)):
197 it = cat_pipe.get(ref)
200 conn.write(b'missing\n')
202 conn.write(b'%s %s %d\n' % info)
207 def refs(conn, args):
208 limit_to_heads, limit_to_tags = args.split()
209 assert limit_to_heads in (b'0', b'1')
210 assert limit_to_tags in (b'0', b'1')
211 limit_to_heads = int(limit_to_heads)
212 limit_to_tags = int(limit_to_tags)
214 patterns = tuple(x[:-1] for x in lines_until_sentinel(conn, b'\n', Exception))
215 for name, oid in git.list_refs(patterns=patterns,
216 limit_to_heads=limit_to_heads,
217 limit_to_tags=limit_to_tags):
218 assert b'\n' not in name
219 conn.write(b'%s %s\n' % (hexlify(oid), name))
223 def rev_list(conn, _):
225 count = conn.readline()
227 raise Exception('Unexpected EOF while reading rev-list count')
228 assert count == b'\n'
230 fmt = conn.readline()
232 raise Exception('Unexpected EOF while reading rev-list format')
233 fmt = None if fmt == b'\n' else fmt[:-1]
234 refs = tuple(x[:-1] for x in lines_until_sentinel(conn, b'\n', Exception))
235 args = git.rev_list_invocation(refs, format=fmt)
236 p = subprocess.Popen(args, env=git._gitenv(git.repodir),
237 stdout=subprocess.PIPE)
239 out = p.stdout.read(64 * 1024)
244 rv = p.wait() # not fatal
246 msg = 'git rev-list returned error %d' % rv
251 def resolve(conn, args):
253 (flags,) = args.split()
255 want_meta = bool(flags & 1)
256 follow = bool(flags & 2)
257 have_parent = bool(flags & 4)
258 parent = vfs.read_resolution(conn) if have_parent else None
259 path = vint.read_bvec(conn)
261 raise Exception('Empty resolve path')
263 res = list(vfs.resolve(repo, path, parent=parent, want_meta=want_meta,
265 except vfs.IOError as ex:
267 if isinstance(res, vfs.IOError):
268 conn.write(b'\x00') # error
269 vfs.write_ioerror(conn, res)
271 conn.write(b'\x01') # success
272 vfs.write_resolution(conn, res)
278 o = options.Options(optspec)
279 (opt, flags, extra) = o.parse(compat.argv[1:])
282 o.fatal('no arguments expected')
284 debug2('bup server: reading from stdin.\n')
289 b'init-dir': init_dir,
291 b'list-indexes': list_indexes,
292 b'send-index': send_index,
293 b'receive-objects-v2': receive_objects_v2,
294 b'read-ref': read_ref,
295 b'update-ref': update_ref,
297 b'cat': join, # apocryphal alias
298 b'cat-batch' : cat_batch,
300 b'rev-list': rev_list,
304 # FIXME: this protocol is totally lame and not at all future-proof.
305 # (Especially since we abort completely as soon as *anything* bad happens)
307 conn = Conn(byte_stream(sys.stdin), byte_stream(sys.stdout))
308 lr = linereader(conn)
313 debug1('bup server: command: %r\n' % line)
314 words = line.split(b' ', 1)
316 rest = len(words)>1 and words[1] or b''
320 cmd = commands.get(cmd)
324 raise Exception('unknown server command: %r\n' % line)
326 debug1('bup server: done\n')