1 #!/usr/bin/env python2.5
2 import sys, struct, mmap
7 def init_dir(conn, arg):
9 log('bup server: bupdir initialized: %r\n' % git.repodir)
13 def set_dir(conn, arg):
14 git.check_repo_or_die(arg)
15 log('bup server: bupdir is %r\n' % git.repodir)
19 def list_indexes(conn, junk):
20 git.check_repo_or_die()
21 for f in os.listdir(git.repo('objects/pack')):
22 if f.endswith('.idx'):
23 conn.write('%s\n' % f)
27 def send_index(conn, name):
28 git.check_repo_or_die()
29 assert(name.find('/') < 0)
30 assert(name.endswith('.idx'))
31 idx = git.PackIndex(git.repo('objects/pack/%s' % name))
32 conn.write(struct.pack('!I', len(idx.map)))
37 def receive_objects(conn, junk):
38 git.check_repo_or_die()
44 raise Exception('object read: expected length header, got EOF\n')
45 n = struct.unpack('!I', ns)[0]
46 #log('expecting %d bytes\n' % n)
48 log('bup server: received %d object%s.\n'
49 % (w.count, w.count!=1 and "s" or ''))
51 conn.write('%s\n' % id)
54 buf = conn.read(n) # object sizes in bup are reasonably small
55 #log('read %d bytes\n' % n)
58 raise Exception('object read: expected %d bytes, got %d\n'
64 def read_ref(conn, refname):
65 git.check_repo_or_die()
66 r = git.read_ref(refname)
67 conn.write('%s\n' % (r or '').encode('hex'))
71 def update_ref(conn, refname):
72 git.check_repo_or_die()
73 newval = conn.readline().strip()
74 oldval = conn.readline().strip()
75 git.update_ref(refname, newval.decode('hex'), oldval.decode('hex'))
80 git.check_repo_or_die()
81 for blob in git.cat(id):
82 conn.write(struct.pack('!I', len(blob)))
84 conn.write('\0\0\0\0')
91 o = options.Options('bup server', optspec)
92 (opt, flags, extra) = o.parse(sys.argv[1:])
95 log('bup server: no arguments expected\n')
98 log('bup server: reading from stdin.\n')
101 'init-dir': init_dir,
103 'list-indexes': list_indexes,
104 'send-index': send_index,
105 'receive-objects': receive_objects,
106 'read-ref': read_ref,
107 'update-ref': update_ref,
111 # FIXME: this protocol is totally lame and not at all future-proof.
112 # (Especially since we abort completely as soon as *anything* bad happens)
113 conn = Conn(sys.stdin, sys.stdout)
114 lr = linereader(conn)
119 log('bup server: command: %r\n' % line)
120 words = line.split(' ', 1)
122 rest = len(words)>1 and words[1] or ''
126 cmd = commands.get(cmd)
130 raise Exception('unknown server command: %r\n' % line)
132 log('bup server: done\n')