3 from bup import options, git
4 from bup.helpers import *
9 def init_dir(conn, arg):
11 debug1('bup server: bupdir initialized: %r\n' % git.repodir)
15 def set_dir(conn, arg):
16 git.check_repo_or_die(arg)
17 debug1('bup server: bupdir is %r\n' % git.repodir)
21 def list_indexes(conn, junk):
22 git.check_repo_or_die()
23 for f in os.listdir(git.repo('objects/pack')):
24 if f.endswith('.idx'):
25 conn.write('%s\n' % f)
29 def send_index(conn, name):
30 git.check_repo_or_die()
31 assert(name.find('/') < 0)
32 assert(name.endswith('.idx'))
33 idx = git.open_idx(git.repo('objects/pack/%s' % name))
34 conn.write(struct.pack('!I', len(idx.map)))
39 def receive_objects_v2(conn, junk):
41 git.check_repo_or_die()
52 raise Exception('object read: expected length header, got EOF\n')
53 n = struct.unpack('!I', ns)[0]
54 #debug2('expecting %d bytes\n' % n)
56 debug1('bup server: received %d object%s.\n'
57 % (w.count, w.count!=1 and "s" or ''))
60 (dir, name) = os.path.split(fullpath)
61 conn.write('%s.idx\n' % name)
65 debug2('bup server: receive-objects suspended.\n')
71 crcr = struct.unpack('!I', conn.read(4))[0]
73 buf = conn.read(n) # object sizes in bup are reasonably small
74 #debug2('read %d bytes\n' % n)
77 raise Exception('object read: expected %d bytes, got %d\n'
79 oldpack = w.exists(shar)
80 # FIXME: we only suggest a single index per cycle, because the client
81 # is currently too dumb to download more than one per cycle anyway.
82 # Actually we should fix the client, but this is a minor optimization
84 if not suggested and \
85 oldpack and (oldpack == True or oldpack.endswith('.midx')):
86 # FIXME: we shouldn't really have to know about midx files
87 # at this layer. But exists() on a midx doesn't return the
88 # packname (since it doesn't know)... probably we should just
89 # fix that deficiency of midx files eventually, although it'll
90 # make the files bigger. This method is certainly not very
92 oldpack = w.objcache.packname_containing(shar)
93 debug2('new suggestion: %r\n' % oldpack)
95 assert(oldpack != True)
96 assert(not oldpack.endswith('.midx'))
98 if not suggested and oldpack:
99 assert(oldpack.endswith('.idx'))
100 (dir,name) = os.path.split(oldpack)
101 if not (name in suggested):
102 debug1("bup server: suggesting index %s\n" % name)
103 conn.write('index %s\n' % name)
106 nw, crc = w._raw_write([buf], sha=shar)
107 _check(w, crcr, crc, 'object read: expected crc %d, got %d\n')
108 _check(w, n, nw, 'object read: expected %d bytes, got %d\n')
112 def _check(w, expected, actual, msg):
113 if expected != actual:
115 raise Exception(msg % (expected, actual))
118 def read_ref(conn, refname):
119 git.check_repo_or_die()
120 r = git.read_ref(refname)
121 conn.write('%s\n' % (r or '').encode('hex'))
125 def update_ref(conn, refname):
126 git.check_repo_or_die()
127 newval = conn.readline().strip()
128 oldval = conn.readline().strip()
129 git.update_ref(refname, newval.decode('hex'), oldval.decode('hex'))
136 git.check_repo_or_die()
138 cat_pipe = git.CatPipe()
140 for blob in cat_pipe.join(id):
141 conn.write(struct.pack('!I', len(blob)))
144 log('server: error: %s\n' % e)
145 conn.write('\0\0\0\0')
148 conn.write('\0\0\0\0')
155 o = options.Options('bup server', optspec)
156 (opt, flags, extra) = o.parse(sys.argv[1:])
159 o.fatal('no arguments expected')
161 debug2('bup server: reading from stdin.\n')
164 'init-dir': init_dir,
166 'list-indexes': list_indexes,
167 'send-index': send_index,
168 'receive-objects-v2': receive_objects_v2,
169 'read-ref': read_ref,
170 'update-ref': update_ref,
174 # FIXME: this protocol is totally lame and not at all future-proof.
175 # (Especially since we abort completely as soon as *anything* bad happens)
176 conn = Conn(sys.stdin, sys.stdout)
177 lr = linereader(conn)
182 debug1('bup server: command: %r\n' % line)
183 words = line.split(' ', 1)
185 rest = len(words)>1 and words[1] or ''
189 cmd = commands.get(cmd)
193 raise Exception('unknown server command: %r\n' % line)
195 debug1('bup server: done\n')