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"
105 % git.shorten_hash(name))
106 debug1("bup server: because of object %s\n"
107 % shar.encode('hex'))
108 conn.write('index %s\n' % name)
111 nw, crc = w._raw_write((buf,), sha=shar)
112 _check(w, crcr, crc, 'object read: expected crc %d, got %d\n')
116 def _check(w, expected, actual, msg):
117 if expected != actual:
119 raise Exception(msg % (expected, actual))
122 def read_ref(conn, refname):
123 git.check_repo_or_die()
124 r = git.read_ref(refname)
125 conn.write('%s\n' % (r or '').encode('hex'))
129 def update_ref(conn, refname):
130 git.check_repo_or_die()
131 newval = conn.readline().strip()
132 oldval = conn.readline().strip()
133 git.update_ref(refname, newval.decode('hex'), oldval.decode('hex'))
140 git.check_repo_or_die()
142 cat_pipe = git.CatPipe()
144 for blob in cat_pipe.join(id):
145 conn.write(struct.pack('!I', len(blob)))
148 log('server: error: %s\n' % e)
149 conn.write('\0\0\0\0')
152 conn.write('\0\0\0\0')
159 o = options.Options(optspec)
160 (opt, flags, extra) = o.parse(sys.argv[1:])
163 o.fatal('no arguments expected')
165 debug2('bup server: reading from stdin.\n')
170 'init-dir': init_dir,
172 'list-indexes': list_indexes,
173 'send-index': send_index,
174 'receive-objects-v2': receive_objects_v2,
175 'read-ref': read_ref,
176 'update-ref': update_ref,
180 # FIXME: this protocol is totally lame and not at all future-proof.
181 # (Especially since we abort completely as soon as *anything* bad happens)
182 conn = Conn(sys.stdin, sys.stdout)
183 lr = linereader(conn)
188 debug1('bup server: command: %r\n' % line)
189 words = line.split(' ', 1)
191 rest = len(words)>1 and words[1] or ''
195 cmd = commands.get(cmd)
199 raise Exception('unknown server command: %r\n' % line)
201 debug1('bup server: done\n')