3 from bup import options, git
4 from bup.helpers import *
11 if os.path.exists(git.repo('bup-dumb-server')):
15 debug1('bup server: serving in %s mode\n' % server_mode)
18 def init_dir(conn, arg):
20 debug1('bup server: bupdir initialized: %r\n' % git.repodir)
25 def set_dir(conn, arg):
26 git.check_repo_or_die(arg)
27 debug1('bup server: bupdir is %r\n' % git.repodir)
32 def list_indexes(conn, junk):
34 git.check_repo_or_die()
36 if server_mode == 'dumb':
38 for f in os.listdir(git.repo('objects/pack')):
39 if f.endswith('.idx'):
40 conn.write('%s%s\n' % (f, suffix))
44 def send_index(conn, name):
45 git.check_repo_or_die()
46 assert(name.find('/') < 0)
47 assert(name.endswith('.idx'))
48 idx = git.open_idx(git.repo('objects/pack/%s' % name))
49 conn.write(struct.pack('!I', len(idx.map)))
54 def receive_objects_v2(conn, junk):
55 global suspended_w, server_mode
56 git.check_repo_or_die()
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=(server_mode=='smart'))
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)
92 raise Exception('object read: expected %d bytes, got %d\n'
94 if server_mode == 'smart':
95 oldpack = w.exists(shar)
98 # FIXME: we only suggest a single index per cycle, because the client
99 # is currently too dumb to download more than one per cycle anyway.
100 # Actually we should fix the client, but this is a minor optimization
101 # on the server side.
102 if not suggested and \
103 oldpack and (oldpack == True or oldpack.endswith('.midx')):
104 # FIXME: we shouldn't really have to know about midx files
105 # at this layer. But exists() on a midx doesn't return the
106 # packname (since it doesn't know)... probably we should just
107 # fix that deficiency of midx files eventually, although it'll
108 # make the files bigger. This method is certainly not very
110 oldpack = w.objcache.packname_containing(shar)
111 debug2('new suggestion: %r\n' % oldpack)
113 assert(oldpack != True)
114 assert(not oldpack.endswith('.midx'))
116 if not suggested and oldpack:
117 assert(oldpack.endswith('.idx'))
118 (dir,name) = os.path.split(oldpack)
119 if not (name in suggested):
120 debug1("bup server: suggesting index %s\n" % name)
121 conn.write('index %s\n' % name)
124 nw, crc = w._raw_write([buf], sha=shar)
125 _check(w, crcr, crc, 'object read: expected crc %d, got %d\n')
126 _check(w, n, nw, 'object read: expected %d bytes, 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):
137 git.check_repo_or_die()
138 r = git.read_ref(refname)
139 conn.write('%s\n' % (r or '').encode('hex'))
143 def update_ref(conn, refname):
144 git.check_repo_or_die()
145 newval = conn.readline().strip()
146 oldval = conn.readline().strip()
147 git.update_ref(refname, newval.decode('hex'), oldval.decode('hex'))
154 git.check_repo_or_die()
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('bup server', optspec)
174 (opt, flags, extra) = o.parse(sys.argv[1:])
177 o.fatal('no arguments expected')
179 debug2('bup server: reading from stdin.\n')
182 'init-dir': init_dir,
184 'list-indexes': list_indexes,
185 'send-index': send_index,
186 'receive-objects-v2': receive_objects_v2,
187 'read-ref': read_ref,
188 'update-ref': update_ref,
192 # FIXME: this protocol is totally lame and not at all future-proof.
193 # (Especially since we abort completely as soon as *anything* bad happens)
194 conn = Conn(sys.stdin, sys.stdout)
195 lr = linereader(conn)
200 debug1('bup server: command: %r\n' % line)
201 words = line.split(' ', 1)
203 rest = len(words)>1 and words[1] or ''
207 cmd = commands.get(cmd)
211 raise Exception('unknown server command: %r\n' % line)
213 debug1('bup server: done\n')