2 import sys, struct, mmap
3 from bup import options, git
4 from bup.helpers import *
9 def init_dir(conn, arg):
11 log('bup server: bupdir initialized: %r\n' % git.repodir)
15 def set_dir(conn, arg):
16 git.check_repo_or_die(arg)
17 log('bup server: bupdir is %r\n' % git.repodir)
21 def list_indexes(conn, junk):
22 git.check_repo_or_die()
23 for f in os.listdir(git.repo('objects/pack')):
24 if f.endswith('.idx'):
25 conn.write('%s\n' % f)
29 def send_index(conn, name):
30 git.check_repo_or_die()
31 assert(name.find('/') < 0)
32 assert(name.endswith('.idx'))
33 idx = git.PackIdx(git.repo('objects/pack/%s' % name))
34 conn.write(struct.pack('!I', len(idx.map)))
39 def receive_objects(conn, junk):
41 git.check_repo_or_die()
52 raise Exception('object read: expected length header, got EOF\n')
53 n = struct.unpack('!I', ns)[0]
54 #log('expecting %d bytes\n' % n)
56 log('bup server: received %d object%s.\n'
57 % (w.count, w.count!=1 and "s" or ''))
59 (dir, name) = os.path.split(fullpath)
60 conn.write('%s.idx\n' % name)
64 log('bup server: receive-objects suspended.\n')
69 buf = conn.read(n) # object sizes in bup are reasonably small
70 #log('read %d bytes\n' % n)
73 raise Exception('object read: expected %d bytes, got %d\n'
75 (type, content) = git._decode_packobj(buf)
76 sha = git.calc_hash(type, content)
77 oldpack = w.exists(sha)
78 if oldpack and (oldpack == True or oldpack.endswith('.midx')):
79 # FIXME: we shouldn't really have to know about midx files
80 # at this layer. But exists() on a midx doesn't return the
81 # packname (since it doesn't know)... probably we should just
82 # fix that deficiency of midx files eventually, although it'll
83 # make the files bigger. This method is certainly not very
85 w.objcache.refresh(skip_midx = True, forget_packs = True)
86 oldpack = w.objcache.exists(sha)
87 log('new suggestion: %r\n' % oldpack)
89 assert(oldpack != True)
90 assert(not oldpack.endswith('.midx'))
91 w.objcache.refresh(skip_midx = False)
93 assert(oldpack.endswith('.idx'))
94 (dir,name) = os.path.split(oldpack)
95 if not (name in suggested):
96 log("bup server: suggesting index %s\n" % name)
97 conn.write('index %s\n' % name)
104 def read_ref(conn, refname):
105 git.check_repo_or_die()
106 r = git.read_ref(refname)
107 conn.write('%s\n' % (r or '').encode('hex'))
111 def update_ref(conn, refname):
112 git.check_repo_or_die()
113 newval = conn.readline().strip()
114 oldval = conn.readline().strip()
115 git.update_ref(refname, newval.decode('hex'), oldval.decode('hex'))
120 git.check_repo_or_die()
122 for blob in git.cat(id):
123 conn.write(struct.pack('!I', len(blob)))
126 log('server: error: %s\n' % e)
127 conn.write('\0\0\0\0')
130 conn.write('\0\0\0\0')
137 o = options.Options('bup server', optspec)
138 (opt, flags, extra) = o.parse(sys.argv[1:])
141 o.fatal('no arguments expected')
143 log('bup server: reading from stdin.\n')
146 'init-dir': init_dir,
148 'list-indexes': list_indexes,
149 'send-index': send_index,
150 'receive-objects': receive_objects,
151 'read-ref': read_ref,
152 'update-ref': update_ref,
156 # FIXME: this protocol is totally lame and not at all future-proof.
157 # (Especially since we abort completely as soon as *anything* bad happens)
158 conn = Conn(sys.stdin, sys.stdout)
159 lr = linereader(conn)
164 log('bup server: command: %r\n' % line)
165 words = line.split(' ', 1)
167 rest = len(words)>1 and words[1] or ''
171 cmd = commands.get(cmd)
175 raise Exception('unknown server command: %r\n' % line)
177 log('bup server: done\n')