3 from bup import options, git
4 from bup.helpers import *
7 dumb_server_mode = False
10 global dumb_server_mode
11 dumb_server_mode = os.path.exists(git.repo('bup-dumb-server'))
12 debug1('bup server: serving in %s mode\n'
13 % (dumb_server_mode and 'dumb' or 'smart'))
16 def init_dir(conn, arg):
18 debug1('bup server: bupdir initialized: %r\n' % git.repodir)
23 def set_dir(conn, arg):
24 git.check_repo_or_die(arg)
25 debug1('bup server: bupdir is %r\n' % git.repodir)
30 def list_indexes(conn, junk):
31 git.check_repo_or_die()
35 for f in os.listdir(git.repo('objects/pack')):
36 if f.endswith('.idx'):
37 conn.write('%s%s\n' % (f, suffix))
41 def send_index(conn, name):
42 git.check_repo_or_die()
43 assert(name.find('/') < 0)
44 assert(name.endswith('.idx'))
45 idx = git.open_idx(git.repo('objects/pack/%s' % name))
46 conn.write(struct.pack('!I', len(idx.map)))
51 def receive_objects_v2(conn, junk):
53 git.check_repo_or_die()
60 w = git.PackWriter(objcache_maker=None)
67 raise Exception('object read: expected length header, got EOF\n')
68 n = struct.unpack('!I', ns)[0]
69 #debug2('expecting %d bytes\n' % n)
71 debug1('bup server: received %d object%s.\n'
72 % (w.count, w.count!=1 and "s" or ''))
73 fullpath = w.close(run_midx=not dumb_server_mode)
75 (dir, name) = os.path.split(fullpath)
76 conn.write('%s.idx\n' % name)
80 debug2('bup server: receive-objects suspended.\n')
86 crcr = struct.unpack('!I', conn.read(4))[0]
88 buf = conn.read(n) # object sizes in bup are reasonably small
89 #debug2('read %d bytes\n' % n)
90 _check(w, n, len(buf), 'object read: expected %d bytes, got %d\n')
91 if not dumb_server_mode:
92 oldpack = w.exists(shar)
94 if oldpack == True or oldpack.endswith('.midx'):
95 # FIXME: we shouldn't really have to know about midx files
96 # at this layer. But exists() on a midx doesn't return the
97 # packname (since it doesn't know)... probably we should
98 # just fix that deficiency of midx files eventually,
99 # although it'll make the files bigger. This method is
100 # certainly not very efficient.
101 oldpack = w.objcache.packname_containing(shar)
102 debug2('new suggestion: %r\n' % oldpack)
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" % name)
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')
168 'init-dir': init_dir,
170 'list-indexes': list_indexes,
171 'send-index': send_index,
172 'receive-objects-v2': receive_objects_v2,
173 'read-ref': read_ref,
174 'update-ref': update_ref,
178 # FIXME: this protocol is totally lame and not at all future-proof.
179 # (Especially since we abort completely as soon as *anything* bad happens)
180 conn = Conn(sys.stdin, sys.stdout)
181 lr = linereader(conn)
186 debug1('bup server: command: %r\n' % line)
187 words = line.split(' ', 1)
189 rest = len(words)>1 and words[1] or ''
193 cmd = commands.get(cmd)
197 raise Exception('unknown server command: %r\n' % line)
199 debug1('bup server: done\n')