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 # 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'] = arg
34 debug1('bup server: bupdir is %r\n' % git.repodir)
39 def list_indexes(conn, junk):
40 git.check_repo_or_die()
44 for f in os.listdir(git.repo('objects/pack')):
45 if f.endswith('.idx'):
46 conn.write('%s%s\n' % (f, suffix))
50 def send_index(conn, name):
51 git.check_repo_or_die()
52 assert(name.find('/') < 0)
53 assert(name.endswith('.idx'))
54 idx = git.open_idx(git.repo('objects/pack/%s' % name))
55 conn.write(struct.pack('!I', len(idx.map)))
60 def receive_objects_v2(conn, junk):
62 git.check_repo_or_die()
69 w = git.PackWriter(objcache_maker=None)
76 raise Exception('object read: expected length header, got EOF\n')
77 n = struct.unpack('!I', ns)[0]
78 #debug2('expecting %d bytes\n' % n)
80 debug1('bup server: received %d object%s.\n'
81 % (w.count, w.count!=1 and "s" or ''))
82 fullpath = w.close(run_midx=not dumb_server_mode)
84 (dir, name) = os.path.split(fullpath)
85 conn.write('%s.idx\n' % name)
89 debug2('bup server: receive-objects suspended.\n')
95 crcr = struct.unpack('!I', conn.read(4))[0]
97 buf = conn.read(n) # object sizes in bup are reasonably small
98 #debug2('read %d bytes\n' % n)
99 _check(w, n, len(buf), 'object read: expected %d bytes, got %d\n')
100 if not dumb_server_mode:
101 oldpack = w.exists(shar, want_source=True)
103 assert(not oldpack == True)
104 assert(oldpack.endswith('.idx'))
105 (dir,name) = os.path.split(oldpack)
106 if not (name in suggested):
107 debug1("bup server: suggesting index %s\n"
108 % git.shorten_hash(name))
109 debug1("bup server: because of object %s\n"
110 % shar.encode('hex'))
111 conn.write('index %s\n' % name)
114 nw, crc = w._raw_write((buf,), sha=shar)
115 _check(w, crcr, crc, 'object read: expected crc %d, got %d\n')
119 def _check(w, expected, actual, msg):
120 if expected != actual:
122 raise Exception(msg % (expected, actual))
125 def read_ref(conn, refname):
126 git.check_repo_or_die()
127 r = git.read_ref(refname)
128 conn.write('%s\n' % (r or '').encode('hex'))
132 def update_ref(conn, refname):
133 git.check_repo_or_die()
134 newval = conn.readline().strip()
135 oldval = conn.readline().strip()
136 git.update_ref(refname, newval.decode('hex'), oldval.decode('hex'))
143 git.check_repo_or_die()
145 cat_pipe = git.CatPipe()
147 for blob in cat_pipe.join(id):
148 conn.write(struct.pack('!I', len(blob)))
151 log('server: error: %s\n' % e)
152 conn.write('\0\0\0\0')
155 conn.write('\0\0\0\0')
162 o = options.Options(optspec)
163 (opt, flags, extra) = o.parse(sys.argv[1:])
166 o.fatal('no arguments expected')
168 debug2('bup server: reading from stdin.\n')
173 'init-dir': init_dir,
175 'list-indexes': list_indexes,
176 'send-index': send_index,
177 'receive-objects-v2': receive_objects_v2,
178 'read-ref': read_ref,
179 'update-ref': update_ref,
183 # FIXME: this protocol is totally lame and not at all future-proof.
184 # (Especially since we abort completely as soon as *anything* bad happens)
185 conn = Conn(sys.stdin, sys.stdout)
186 lr = linereader(conn)
191 debug1('bup server: command: %r\n' % line)
192 words = line.split(' ', 1)
194 rest = len(words)>1 and words[1] or ''
198 cmd = commands.get(cmd)
202 raise Exception('unknown server command: %r\n' % line)
204 debug1('bup server: done\n')