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.'
19 % (dumb_server_mode and 'dumb' or 'smart'))
22 def _init_session(reinit_with_new_repopath=None):
23 if reinit_with_new_repopath is None and git.repodir:
25 git.check_repo_or_die(reinit_with_new_repopath)
26 # OK. we now know the path is a proper repository. Record this path in the
27 # environment so that subprocesses inherit it and know where to operate.
28 os.environ['BUP_DIR'] = git.repodir
29 debug1('bup server: bupdir is "%r".' % git.repodir)
33 def init_dir(conn, arg):
35 debug1('bup server: bupdir initialized: "%r".' % git.repodir)
40 def set_dir(conn, arg):
45 def list_indexes(conn, junk):
50 for f in os.listdir(git.repo('objects/pack')):
51 if f.endswith('.idx'):
52 conn.write('%s%s\n' % (f, suffix))
56 def send_index(conn, name):
58 assert(name.find('/') < 0)
59 assert(name.endswith('.idx'))
60 idx = git.open_idx(git.repo('objects/pack/%s' % name))
61 conn.write(struct.pack('!I', len(idx.map)))
66 def receive_objects_v2(conn, junk):
75 w = git.PackWriter(objcache_maker=None)
82 raise Exception('Object read: expected length header, got EOF')
83 n = struct.unpack('!I', ns)[0]
84 #debug2('expecting %d bytes' % n)
86 debug1('bup server: Received %d object%s.'
87 % (w.count, w.count!=1 and "s" or ''))
88 fullpath = w.close(run_midx=not dumb_server_mode)
90 (dir, name) = os.path.split(fullpath)
91 conn.write('%s.idx\n' % name)
95 debug2('bup server: Receive-objects suspended.')
101 crcr = struct.unpack('!I', conn.read(4))[0]
103 buf = conn.read(n) # object sizes in bup are reasonably small
104 #debug2('read %d bytes' % n)
105 _check(w, n, len(buf), 'Object read: Expected %d bytes, got %d\n')
106 if not dumb_server_mode:
107 oldpack = w.exists(shar, want_source=True)
109 assert(not oldpack == True)
110 assert(oldpack.endswith('.idx'))
111 (dir,name) = os.path.split(oldpack)
112 if not (name in suggested):
113 debug1("bup server: Suggesting index %s"
114 % git.shorten_hash(name))
115 debug1("bup server: because of object %s"
116 % shar.encode('hex'))
117 conn.write('index %s\n' % name)
120 nw, crc = w._raw_write((buf,), sha=shar)
121 _check(w, crcr, crc, 'Object read: Expected CRC %d, got %d\n')
125 def _check(w, expected, actual, msg):
126 if expected != actual:
128 raise Exception(msg % (expected, actual))
131 def read_ref(conn, refname):
133 r = git.read_ref(refname)
134 conn.write('%s\n' % (r or '').encode('hex'))
138 def update_ref(conn, refname):
140 newval = conn.readline().strip()
141 oldval = conn.readline().strip()
142 git.update_ref(refname, newval.decode('hex'), oldval.decode('hex'))
151 cat_pipe = git.CatPipe()
153 for blob in cat_pipe.join(id):
154 conn.write(struct.pack('!I', len(blob)))
157 log('bup server: Error: %s' % e)
158 conn.write('\0\0\0\0')
161 conn.write('\0\0\0\0')
168 o = options.Options(optspec)
169 (opt, flags, extra) = o.parse(sys.argv[1:])
172 o.fatal('no arguments expected')
174 debug2('bup server: Reading from stdin.')
179 'init-dir': init_dir,
181 'list-indexes': list_indexes,
182 'send-index': send_index,
183 'receive-objects-v2': receive_objects_v2,
184 'read-ref': read_ref,
185 'update-ref': update_ref,
189 # FIXME: this protocol is totally lame and not at all future-proof.
190 # (Especially since we abort completely as soon as *anything* bad happens)
191 conn = Conn(sys.stdin, sys.stdout)
192 lr = linereader(conn)
197 debug1('bup server: Command: "%r" ...' % line)
198 words = line.split(' ', 1)
200 rest = len(words)>1 and words[1] or ''
204 cmd = commands.get(cmd)
208 raise Exception('Unknown server command: "%r"' % line)
210 debug1('bup server: Done.')