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 ''))
60 (dir, name) = os.path.split(fullpath)
61 conn.write('%s.idx\n' % name)
65 log('bup server: receive-objects suspended.\n')
70 buf = conn.read(n) # object sizes in bup are reasonably small
71 #log('read %d bytes\n' % n)
74 raise Exception('object read: expected %d bytes, got %d\n'
76 (type, content) = git._decode_packobj(buf)
77 sha = git.calc_hash(type, content)
78 oldpack = w.exists(sha)
79 if oldpack and (oldpack == True or oldpack.endswith('.midx')):
80 # FIXME: we shouldn't really have to know about midx files
81 # at this layer. But exists() on a midx doesn't return the
82 # packname (since it doesn't know)... probably we should just
83 # fix that deficiency of midx files eventually, although it'll
84 # make the files bigger. This method is certainly not very
86 w.objcache.refresh(skip_midx = True)
87 oldpack = w.objcache.exists(sha)
88 log('new suggestion: %r\n' % oldpack)
90 assert(oldpack != True)
91 assert(not oldpack.endswith('.midx'))
92 w.objcache.refresh(skip_midx = False)
94 assert(oldpack.endswith('.idx'))
95 (dir,name) = os.path.split(oldpack)
96 if not (name in suggested):
97 log("bup server: suggesting index %s\n" % name)
98 conn.write('index %s\n' % name)
105 def read_ref(conn, refname):
106 git.check_repo_or_die()
107 r = git.read_ref(refname)
108 conn.write('%s\n' % (r or '').encode('hex'))
112 def update_ref(conn, refname):
113 git.check_repo_or_die()
114 newval = conn.readline().strip()
115 oldval = conn.readline().strip()
116 git.update_ref(refname, newval.decode('hex'), oldval.decode('hex'))
121 git.check_repo_or_die()
123 for blob in git.cat(id):
124 conn.write(struct.pack('!I', len(blob)))
127 log('server: error: %s\n' % e)
128 conn.write('\0\0\0\0')
131 conn.write('\0\0\0\0')
138 o = options.Options('bup server', optspec)
139 (opt, flags, extra) = o.parse(sys.argv[1:])
142 o.fatal('no arguments expected')
144 log('bup server: reading from stdin.\n')
147 'init-dir': init_dir,
149 'list-indexes': list_indexes,
150 'send-index': send_index,
151 'receive-objects': receive_objects,
152 'read-ref': read_ref,
153 'update-ref': update_ref,
157 # FIXME: this protocol is totally lame and not at all future-proof.
158 # (Especially since we abort completely as soon as *anything* bad happens)
159 conn = Conn(sys.stdin, sys.stdout)
160 lr = linereader(conn)
165 log('bup server: command: %r\n' % line)
166 words = line.split(' ', 1)
168 rest = len(words)>1 and words[1] or ''
172 cmd = commands.get(cmd)
176 raise Exception('unknown server command: %r\n' % line)
178 log('bup server: done\n')