3 from bup import options, git
4 from bup.helpers import *
7 dumb_server_mode = False
10 def do_help(conn, junk):
11 conn.write('Commands:\n %s\n' % '\n '.join(sorted(commands)))
16 global dumb_server_mode
17 dumb_server_mode = os.path.exists(git.repo('bup-dumb-server'))
18 debug1('bup server: serving in %s mode\n'
19 % (dumb_server_mode and 'dumb' or 'smart'))
22 def init_dir(conn, arg):
24 debug1('bup server: bupdir initialized: %r\n' % git.repodir)
29 def set_dir(conn, arg):
30 git.check_repo_or_die(arg)
31 debug1('bup server: bupdir is %r\n' % git.repodir)
36 def list_indexes(conn, junk):
37 git.check_repo_or_die()
41 for f in os.listdir(git.repo('objects/pack')):
42 if f.endswith('.idx'):
43 conn.write('%s%s\n' % (f, suffix))
47 def send_index(conn, name):
48 git.check_repo_or_die()
49 assert(name.find('/') < 0)
50 assert(name.endswith('.idx'))
51 idx = git.open_idx(git.repo('objects/pack/%s' % name))
52 conn.write(struct.pack('!I', len(idx.map)))
57 def receive_objects_v2(conn, junk):
59 git.check_repo_or_die()
66 w = git.PackWriter(objcache_maker=None)
73 raise Exception('object read: expected length header, got EOF\n')
74 n = struct.unpack('!I', ns)[0]
75 #debug2('expecting %d bytes\n' % n)
77 debug1('bup server: received %d object%s.\n'
78 % (w.count, w.count!=1 and "s" or ''))
79 fullpath = w.close(run_midx=not dumb_server_mode)
81 (dir, name) = os.path.split(fullpath)
82 conn.write('%s.idx\n' % name)
86 debug2('bup server: receive-objects suspended.\n')
92 crcr = struct.unpack('!I', conn.read(4))[0]
94 buf = conn.read(n) # object sizes in bup are reasonably small
95 #debug2('read %d bytes\n' % n)
96 _check(w, n, len(buf), 'object read: expected %d bytes, got %d\n')
97 if not dumb_server_mode:
98 oldpack = w.exists(shar, want_source=True)
100 assert(not oldpack == True)
101 assert(oldpack.endswith('.idx'))
102 (dir,name) = os.path.split(oldpack)
103 if not (name in suggested):
104 debug1("bup server: suggesting index %s\n" % name)
105 debug1("bup server: because of object %s\n"
106 % shar.encode('hex'))
107 conn.write('index %s\n' % name)
110 nw, crc = w._raw_write((buf,), sha=shar)
111 _check(w, crcr, crc, 'object read: expected crc %d, got %d\n')
115 def _check(w, expected, actual, msg):
116 if expected != actual:
118 raise Exception(msg % (expected, actual))
121 def read_ref(conn, refname):
122 git.check_repo_or_die()
123 r = git.read_ref(refname)
124 conn.write('%s\n' % (r or '').encode('hex'))
128 def update_ref(conn, refname):
129 git.check_repo_or_die()
130 newval = conn.readline().strip()
131 oldval = conn.readline().strip()
132 git.update_ref(refname, newval.decode('hex'), oldval.decode('hex'))
139 git.check_repo_or_die()
141 cat_pipe = git.CatPipe()
143 for blob in cat_pipe.join(id):
144 conn.write(struct.pack('!I', len(blob)))
147 log('server: error: %s\n' % e)
148 conn.write('\0\0\0\0')
151 conn.write('\0\0\0\0')
158 o = options.Options(optspec)
159 (opt, flags, extra) = o.parse(sys.argv[1:])
162 o.fatal('no arguments expected')
164 debug2('bup server: reading from stdin.\n')
169 'init-dir': init_dir,
171 'list-indexes': list_indexes,
172 'send-index': send_index,
173 'receive-objects-v2': receive_objects_v2,
174 'read-ref': read_ref,
175 'update-ref': update_ref,
179 # FIXME: this protocol is totally lame and not at all future-proof.
180 # (Especially since we abort completely as soon as *anything* bad happens)
181 conn = Conn(sys.stdin, sys.stdout)
182 lr = linereader(conn)
187 debug1('bup server: command: %r\n' % line)
188 words = line.split(' ', 1)
190 rest = len(words)>1 and words[1] or ''
194 cmd = commands.get(cmd)
198 raise Exception('unknown server command: %r\n' % line)
200 debug1('bup server: done\n')