suspended_w = None
dumb_server_mode = False
+
+def do_help(conn, junk):
+ conn.write('Commands:\n %s\n' % '\n '.join(sorted(commands)))
+ conn.ok()
+
+
def _set_mode():
global dumb_server_mode
dumb_server_mode = os.path.exists(git.repo('bup-dumb-server'))
- debug1('bup server: serving in %s mode\n'
+ debug1('bup server: Serving in %s mode.'
% (dumb_server_mode and 'dumb' or 'smart'))
+def _init_session(reinit_with_new_repopath=None):
+ if reinit_with_new_repopath is None and git.repodir:
+ return
+ git.check_repo_or_die(reinit_with_new_repopath)
+ # OK. we now know the path is a proper repository. Record this path in the
+ # environment so that subprocesses inherit it and know where to operate.
+ os.environ['BUP_DIR'] = git.repodir
+ debug1('bup server: bupdir is "%r".' % git.repodir)
+ _set_mode()
+
+
def init_dir(conn, arg):
git.init_repo(arg)
- debug1('bup server: bupdir initialized: %r\n' % git.repodir)
- _set_mode()
+ debug1('bup server: bupdir initialized: "%r".' % git.repodir)
+ _init_session(arg)
conn.ok()
def set_dir(conn, arg):
- git.check_repo_or_die(arg)
- debug1('bup server: bupdir is %r\n' % git.repodir)
- _set_mode()
+ _init_session(arg)
conn.ok()
def list_indexes(conn, junk):
- git.check_repo_or_die()
+ _init_session()
suffix = ''
if dumb_server_mode:
suffix = ' load'
def send_index(conn, name):
- git.check_repo_or_die()
+ _init_session()
assert(name.find('/') < 0)
assert(name.endswith('.idx'))
idx = git.open_idx(git.repo('objects/pack/%s' % name))
def receive_objects_v2(conn, junk):
global suspended_w
- git.check_repo_or_die()
+ _init_session()
suggested = set()
if suspended_w:
w = suspended_w
ns = conn.read(4)
if not ns:
w.abort()
- raise Exception('object read: expected length header, got EOF\n')
+ raise Exception('Object read: expected length header, got EOF')
n = struct.unpack('!I', ns)[0]
- #debug2('expecting %d bytes\n' % n)
+ #debug2('expecting %d bytes' % n)
if not n:
- debug1('bup server: received %d object%s.\n'
+ debug1('bup server: Received %d object%s.'
% (w.count, w.count!=1 and "s" or ''))
fullpath = w.close(run_midx=not dumb_server_mode)
if fullpath:
conn.ok()
return
elif n == 0xffffffff:
- debug2('bup server: receive-objects suspended.\n')
+ debug2('bup server: Receive-objects suspended.')
suspended_w = w
conn.ok()
return
-
+
shar = conn.read(20)
crcr = struct.unpack('!I', conn.read(4))[0]
n -= 20 + 4
buf = conn.read(n) # object sizes in bup are reasonably small
- #debug2('read %d bytes\n' % n)
- _check(w, n, len(buf), 'object read: expected %d bytes, got %d\n')
+ #debug2('read %d bytes' % n)
+ _check(w, n, len(buf), 'Object read: Expected %d bytes, got %d\n')
if not dumb_server_mode:
- oldpack = w.exists(shar)
+ oldpack = w.exists(shar, want_source=True)
if oldpack:
- if oldpack == True or oldpack.endswith('.midx'):
- # FIXME: we shouldn't really have to know about midx files
- # at this layer. But exists() on a midx doesn't return the
- # packname (since it doesn't know)... probably we should
- # just fix that deficiency of midx files eventually,
- # although it'll make the files bigger. This method is
- # certainly not very efficient.
- oldpack = w.objcache.packname_containing(shar)
- debug2('new suggestion: %r\n' % oldpack)
- w.objcache.refresh()
+ assert(not oldpack == True)
assert(oldpack.endswith('.idx'))
(dir,name) = os.path.split(oldpack)
if not (name in suggested):
- debug1("bup server: suggesting index %s\n" % name)
+ debug1("bup server: Suggesting index %s"
+ % git.shorten_hash(name))
+ debug1("bup server: because of object %s"
+ % shar.encode('hex'))
conn.write('index %s\n' % name)
suggested.add(name)
continue
nw, crc = w._raw_write((buf,), sha=shar)
- _check(w, crcr, crc, 'object read: expected crc %d, got %d\n')
+ _check(w, crcr, crc, 'Object read: Expected CRC %d, got %d\n')
# NOTREACHED
def read_ref(conn, refname):
- git.check_repo_or_die()
+ _init_session()
r = git.read_ref(refname)
conn.write('%s\n' % (r or '').encode('hex'))
conn.ok()
def update_ref(conn, refname):
- git.check_repo_or_die()
+ _init_session()
newval = conn.readline().strip()
oldval = conn.readline().strip()
git.update_ref(refname, newval.decode('hex'), oldval.decode('hex'))
cat_pipe = None
def cat(conn, id):
global cat_pipe
- git.check_repo_or_die()
+ _init_session()
if not cat_pipe:
cat_pipe = git.CatPipe()
try:
conn.write(struct.pack('!I', len(blob)))
conn.write(blob)
except KeyError, e:
- log('server: error: %s\n' % e)
+ log('bup server: Error: %s' % e)
conn.write('\0\0\0\0')
conn.error(e)
else:
if extra:
o.fatal('no arguments expected')
-debug2('bup server: reading from stdin.\n')
+debug2('bup server: Reading from stdin.')
commands = {
+ 'quit': None,
+ 'help': do_help,
'init-dir': init_dir,
'set-dir': set_dir,
'list-indexes': list_indexes,
line = _line.strip()
if not line:
continue
- debug1('bup server: command: %r\n' % line)
+ debug1('bup server: Command: "%r" ...' % line)
words = line.split(' ', 1)
cmd = words[0]
rest = len(words)>1 and words[1] or ''
if cmd:
cmd(conn, rest)
else:
- raise Exception('unknown server command: %r\n' % line)
+ raise Exception('Unknown server command: "%r"' % line)
-debug1('bup server: done\n')
+debug1('bup server: Done.')