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
12 from bup.git import MissingObject
13 from bup.helpers import (Conn, debug1, debug2, linereader, lines_until_sentinel,
18 dumb_server_mode = False
21 def do_help(conn, junk):
22 conn.write('Commands:\n %s\n' % '\n '.join(sorted(commands)))
27 global dumb_server_mode
28 dumb_server_mode = os.path.exists(git.repo('bup-dumb-server'))
29 debug1('bup server: serving in %s mode\n'
30 % (dumb_server_mode and 'dumb' or 'smart'))
33 def _init_session(reinit_with_new_repopath=None):
34 if reinit_with_new_repopath is None and git.repodir:
36 git.check_repo_or_die(reinit_with_new_repopath)
37 # OK. we now know the path is a proper repository. Record this path in the
38 # environment so that subprocesses inherit it and know where to operate.
39 os.environ['BUP_DIR'] = git.repodir
40 debug1('bup server: bupdir is %r\n' % git.repodir)
44 def init_dir(conn, arg):
46 debug1('bup server: bupdir initialized: %r\n' % git.repodir)
51 def set_dir(conn, arg):
56 def list_indexes(conn, junk):
61 for f in os.listdir(git.repo('objects/pack')):
62 if f.endswith('.idx'):
63 conn.write('%s%s\n' % (f, suffix))
67 def send_index(conn, name):
69 assert(name.find('/') < 0)
70 assert(name.endswith('.idx'))
71 idx = git.open_idx(git.repo('objects/pack/%s' % name))
72 conn.write(struct.pack('!I', len(idx.map)))
77 def receive_objects_v2(conn, junk):
86 w = git.PackWriter(objcache_maker=None)
93 raise Exception('object read: expected length header, got EOF\n')
94 n = struct.unpack('!I', ns)[0]
95 #debug2('expecting %d bytes\n' % n)
97 debug1('bup server: received %d object%s.\n'
98 % (w.count, w.count!=1 and "s" or ''))
99 fullpath = w.close(run_midx=not dumb_server_mode)
101 (dir, name) = os.path.split(fullpath)
102 conn.write('%s.idx\n' % name)
105 elif n == 0xffffffff:
106 debug2('bup server: receive-objects suspended.\n')
112 crcr = struct.unpack('!I', conn.read(4))[0]
114 buf = conn.read(n) # object sizes in bup are reasonably small
115 #debug2('read %d bytes\n' % n)
116 _check(w, n, len(buf), 'object read: expected %d bytes, got %d\n')
117 if not dumb_server_mode:
118 oldpack = w.exists(shar, want_source=True)
120 assert(not oldpack == True)
121 assert(oldpack.endswith('.idx'))
122 (dir,name) = os.path.split(oldpack)
123 if not (name in suggested):
124 debug1("bup server: suggesting index %s\n"
125 % git.shorten_hash(name))
126 debug1("bup server: because of object %s\n"
127 % shar.encode('hex'))
128 conn.write('index %s\n' % name)
131 nw, crc = w._raw_write((buf,), sha=shar)
132 _check(w, crcr, crc, 'object read: expected crc %d, got %d\n')
136 def _check(w, expected, actual, msg):
137 if expected != actual:
139 raise Exception(msg % (expected, actual))
142 def read_ref(conn, refname):
144 r = git.read_ref(refname)
145 conn.write('%s\n' % (r or '').encode('hex'))
149 def update_ref(conn, refname):
151 newval = conn.readline().strip()
152 oldval = conn.readline().strip()
153 git.update_ref(refname, newval.decode('hex'), oldval.decode('hex'))
159 for blob in git.cp().join(id):
160 conn.write(struct.pack('!I', len(blob)))
162 except KeyError as e:
163 log('server: error: %s\n' % e)
164 conn.write('\0\0\0\0')
167 conn.write('\0\0\0\0')
170 def cat_batch(conn, dummy):
173 # For now, avoid potential deadlock by just reading them all
174 for ref in tuple(lines_until_sentinel(conn, '\n', Exception)):
176 it = cat_pipe.get(ref)
179 conn.write('missing\n')
181 conn.write('%s %s %d\n' % info)
186 def refs(conn, args):
187 limit_to_heads, limit_to_tags = args.split()
188 assert limit_to_heads in ('0', '1')
189 assert limit_to_tags in ('0', '1')
190 limit_to_heads = int(limit_to_heads)
191 limit_to_tags = int(limit_to_tags)
193 patterns = tuple(x[:-1] for x in lines_until_sentinel(conn, '\n', Exception))
194 for name, oid in git.list_refs(patterns=patterns,
195 limit_to_heads=limit_to_heads,
196 limit_to_tags=limit_to_tags):
197 assert '\n' not in name
198 conn.write('%s %s\n' % (oid.encode('hex'), name))
202 def rev_list(conn, _):
204 count = conn.readline()
206 raise Exception('Unexpected EOF while reading rev-list count')
207 count = None if count == '\n' else int(count)
208 fmt = conn.readline()
210 raise Exception('Unexpected EOF while reading rev-list format')
211 fmt = None if fmt == '\n' else fmt[:-1]
212 refs = tuple(x[:-1] for x in lines_until_sentinel(conn, '\n', Exception))
213 args = git.rev_list_invocation(refs, count=count, format=fmt)
214 p = subprocess.Popen(git.rev_list_invocation(refs, count=count, format=fmt),
215 preexec_fn=git._gitenv(git.repodir),
216 stdout=subprocess.PIPE)
218 out = p.stdout.read(64 * 1024)
222 rv = p.wait() # not fatal
224 msg = 'git rev-list returned error %d' % rv
233 o = options.Options(optspec)
234 (opt, flags, extra) = o.parse(sys.argv[1:])
237 o.fatal('no arguments expected')
239 debug2('bup server: reading from stdin.\n')
244 'init-dir': init_dir,
246 'list-indexes': list_indexes,
247 'send-index': send_index,
248 'receive-objects-v2': receive_objects_v2,
249 'read-ref': read_ref,
250 'update-ref': update_ref,
252 'cat': join, # apocryphal alias
253 'cat-batch' : cat_batch,
258 # FIXME: this protocol is totally lame and not at all future-proof.
259 # (Especially since we abort completely as soon as *anything* bad happens)
260 conn = Conn(sys.stdin, sys.stdout)
261 lr = linereader(conn)
266 debug1('bup server: command: %r\n' % line)
267 words = line.split(' ', 1)
269 rest = len(words)>1 and words[1] or ''
273 cmd = commands.get(cmd)
277 raise Exception('unknown server command: %r\n' % line)
279 debug1('bup server: done\n')