3 bup_python="$(dirname "$0")/bup-python" || exit $?
4 exec "$bup_python" "$0" ${1+"$@"}
8 from bup import options, git
9 from bup.helpers import *
12 dumb_server_mode = False
15 def do_help(conn, junk):
16 conn.write('Commands:\n %s\n' % '\n '.join(sorted(commands)))
21 global dumb_server_mode
22 dumb_server_mode = os.path.exists(git.repo('bup-dumb-server'))
23 debug1('bup server: serving in %s mode\n'
24 % (dumb_server_mode and 'dumb' or 'smart'))
27 def _init_session(reinit_with_new_repopath=None):
28 if reinit_with_new_repopath is None and git.repodir:
30 git.check_repo_or_die(reinit_with_new_repopath)
31 # OK. we now know the path is a proper repository. Record this path in the
32 # environment so that subprocesses inherit it and know where to operate.
33 os.environ['BUP_DIR'] = git.repodir
34 debug1('bup server: bupdir is %r\n' % git.repodir)
38 def init_dir(conn, arg):
40 debug1('bup server: bupdir initialized: %r\n' % git.repodir)
45 def set_dir(conn, arg):
50 def list_indexes(conn, junk):
55 for f in os.listdir(git.repo('objects/pack')):
56 if f.endswith('.idx'):
57 conn.write('%s%s\n' % (f, suffix))
61 def send_index(conn, name):
63 assert(name.find('/') < 0)
64 assert(name.endswith('.idx'))
65 idx = git.open_idx(git.repo('objects/pack/%s' % name))
66 conn.write(struct.pack('!I', len(idx.map)))
71 def receive_objects_v2(conn, junk):
80 w = git.PackWriter(objcache_maker=None)
87 raise Exception('object read: expected length header, got EOF\n')
88 n = struct.unpack('!I', ns)[0]
89 #debug2('expecting %d bytes\n' % n)
91 debug1('bup server: received %d object%s.\n'
92 % (w.count, w.count!=1 and "s" or ''))
93 fullpath = w.close(run_midx=not dumb_server_mode)
95 (dir, name) = os.path.split(fullpath)
96 conn.write('%s.idx\n' % name)
100 debug2('bup server: receive-objects suspended.\n')
106 crcr = struct.unpack('!I', conn.read(4))[0]
108 buf = conn.read(n) # object sizes in bup are reasonably small
109 #debug2('read %d bytes\n' % n)
110 _check(w, n, len(buf), 'object read: expected %d bytes, got %d\n')
111 if not dumb_server_mode:
112 oldpack = w.exists(shar, want_source=True)
114 assert(not oldpack == True)
115 assert(oldpack.endswith('.idx'))
116 (dir,name) = os.path.split(oldpack)
117 if not (name in suggested):
118 debug1("bup server: suggesting index %s\n"
119 % git.shorten_hash(name))
120 debug1("bup server: because of object %s\n"
121 % shar.encode('hex'))
122 conn.write('index %s\n' % name)
125 nw, crc = w._raw_write((buf,), sha=shar)
126 _check(w, crcr, crc, 'object read: expected crc %d, got %d\n')
130 def _check(w, expected, actual, msg):
131 if expected != actual:
133 raise Exception(msg % (expected, actual))
136 def read_ref(conn, refname):
138 r = git.read_ref(refname)
139 conn.write('%s\n' % (r or '').encode('hex'))
143 def update_ref(conn, refname):
145 newval = conn.readline().strip()
146 oldval = conn.readline().strip()
147 git.update_ref(refname, newval.decode('hex'), oldval.decode('hex'))
156 cat_pipe = git.CatPipe()
158 for blob in cat_pipe.join(id):
159 conn.write(struct.pack('!I', len(blob)))
162 log('server: error: %s\n' % e)
163 conn.write('\0\0\0\0')
166 conn.write('\0\0\0\0')
173 o = options.Options(optspec)
174 (opt, flags, extra) = o.parse(sys.argv[1:])
177 o.fatal('no arguments expected')
179 debug2('bup server: reading from stdin.\n')
184 'init-dir': init_dir,
186 'list-indexes': list_indexes,
187 'send-index': send_index,
188 'receive-objects-v2': receive_objects_v2,
189 'read-ref': read_ref,
190 'update-ref': update_ref,
194 # FIXME: this protocol is totally lame and not at all future-proof.
195 # (Especially since we abort completely as soon as *anything* bad happens)
196 conn = Conn(sys.stdin, sys.stdout)
197 lr = linereader(conn)
202 debug1('bup server: command: %r\n' % line)
203 words = line.split(' ', 1)
205 rest = len(words)>1 and words[1] or ''
209 cmd = commands.get(cmd)
213 raise Exception('unknown server command: %r\n' % line)
215 debug1('bup server: done\n')