3 bup_python="$(dirname "$0")/bup-python" || exit $?
4 exec "$bup_python" "$0" ${1+"$@"}
10 from bup import options, git
11 from bup.helpers import Conn, debug1, debug2, linereader, log
15 dumb_server_mode = False
18 def do_help(conn, junk):
19 conn.write('Commands:\n %s\n' % '\n '.join(sorted(commands)))
24 global dumb_server_mode
25 dumb_server_mode = os.path.exists(git.repo('bup-dumb-server'))
26 debug1('bup server: serving in %s mode\n'
27 % (dumb_server_mode and 'dumb' or 'smart'))
30 def _init_session(reinit_with_new_repopath=None):
31 if reinit_with_new_repopath is None and git.repodir:
33 git.check_repo_or_die(reinit_with_new_repopath)
34 # OK. we now know the path is a proper repository. Record this path in the
35 # environment so that subprocesses inherit it and know where to operate.
36 os.environ['BUP_DIR'] = git.repodir
37 debug1('bup server: bupdir is %r\n' % git.repodir)
41 def init_dir(conn, arg):
43 debug1('bup server: bupdir initialized: %r\n' % git.repodir)
48 def set_dir(conn, arg):
53 def list_indexes(conn, junk):
58 for f in os.listdir(git.repo('objects/pack')):
59 if f.endswith('.idx'):
60 conn.write('%s%s\n' % (f, suffix))
64 def send_index(conn, name):
66 assert(name.find('/') < 0)
67 assert(name.endswith('.idx'))
68 idx = git.open_idx(git.repo('objects/pack/%s' % name))
69 conn.write(struct.pack('!I', len(idx.map)))
74 def receive_objects_v2(conn, junk):
83 w = git.PackWriter(objcache_maker=None)
90 raise Exception('object read: expected length header, got EOF\n')
91 n = struct.unpack('!I', ns)[0]
92 #debug2('expecting %d bytes\n' % n)
94 debug1('bup server: received %d object%s.\n'
95 % (w.count, w.count!=1 and "s" or ''))
96 fullpath = w.close(run_midx=not dumb_server_mode)
98 (dir, name) = os.path.split(fullpath)
99 conn.write('%s.idx\n' % name)
102 elif n == 0xffffffff:
103 debug2('bup server: receive-objects suspended.\n')
109 crcr = struct.unpack('!I', conn.read(4))[0]
111 buf = conn.read(n) # object sizes in bup are reasonably small
112 #debug2('read %d bytes\n' % n)
113 _check(w, n, len(buf), 'object read: expected %d bytes, got %d\n')
114 if not dumb_server_mode:
115 oldpack = w.exists(shar, want_source=True)
117 assert(not oldpack == True)
118 assert(oldpack.endswith('.idx'))
119 (dir,name) = os.path.split(oldpack)
120 if not (name in suggested):
121 debug1("bup server: suggesting index %s\n"
122 % git.shorten_hash(name))
123 debug1("bup server: because of object %s\n"
124 % shar.encode('hex'))
125 conn.write('index %s\n' % name)
128 nw, crc = w._raw_write((buf,), sha=shar)
129 _check(w, crcr, crc, 'object read: expected crc %d, got %d\n')
133 def _check(w, expected, actual, msg):
134 if expected != actual:
136 raise Exception(msg % (expected, actual))
139 def read_ref(conn, refname):
141 r = git.read_ref(refname)
142 conn.write('%s\n' % (r or '').encode('hex'))
146 def update_ref(conn, refname):
148 newval = conn.readline().strip()
149 oldval = conn.readline().strip()
150 git.update_ref(refname, newval.decode('hex'), oldval.decode('hex'))
159 cat_pipe = git.CatPipe()
161 for blob in cat_pipe.join(id):
162 conn.write(struct.pack('!I', len(blob)))
164 except KeyError as e:
165 log('server: error: %s\n' % e)
166 conn.write('\0\0\0\0')
169 conn.write('\0\0\0\0')
176 o = options.Options(optspec)
177 (opt, flags, extra) = o.parse(sys.argv[1:])
180 o.fatal('no arguments expected')
182 debug2('bup server: reading from stdin.\n')
187 'init-dir': init_dir,
189 'list-indexes': list_indexes,
190 'send-index': send_index,
191 'receive-objects-v2': receive_objects_v2,
192 'read-ref': read_ref,
193 'update-ref': update_ref,
197 # FIXME: this protocol is totally lame and not at all future-proof.
198 # (Especially since we abort completely as soon as *anything* bad happens)
199 conn = Conn(sys.stdin, sys.stdout)
200 lr = linereader(conn)
205 debug1('bup server: command: %r\n' % line)
206 words = line.split(' ', 1)
208 rest = len(words)>1 and words[1] or ''
212 cmd = commands.get(cmd)
216 raise Exception('unknown server command: %r\n' % line)
218 debug1('bup server: done\n')