2 import errno, os, re, struct, time, zlib
4 from bup import git, ssh
5 from bup.helpers import (Conn, atomically_replaced_file, chunkyreader, debug1,
6 debug2, linereader, mkdirp, progress, qprogress)
12 class ClientError(Exception):
16 def _raw_write_bwlimit(f, buf, bwcount, bwtime):
19 return (len(buf), time.time())
21 # We want to write in reasonably large blocks, but not so large that
22 # they're likely to overflow a router's queue. So our bwlimit timing
23 # has to be pretty granular. Also, if it takes too long from one
24 # transmit to the next, we can't just make up for lost time to bring
25 # the average back up to bwlimit - that will risk overflowing the
26 # outbound queue, which defeats the purpose. So if we fall behind
27 # by more than one block delay, we shouldn't ever try to catch up.
28 for i in xrange(0,len(buf),4096):
30 next = max(now, bwtime + 1.0*bwcount/bwlimit)
34 bwcount = len(sub) # might be less than 4096
36 return (bwcount, bwtime)
39 def parse_remote(remote):
40 protocol = r'([a-z]+)://'
41 host = r'(?P<sb>\[)?((?(sb)[0-9a-f:]+|[^:/]+))(?(sb)\])'
45 '%s(?:%s%s)?%s' % (protocol, host, port, path), remote, re.I)
47 if not url_match.group(1) in ('ssh', 'bup', 'file'):
48 raise ClientError, 'unexpected protocol: %s' % url_match.group(1)
49 return url_match.group(1,3,4,5)
51 rs = remote.split(':', 1)
52 if len(rs) == 1 or rs[0] in ('', '-'):
53 return 'file', None, None, rs[-1]
55 return 'ssh', rs[0], None, rs[1]
59 def __init__(self, remote, create=False):
60 self._busy = self.conn = None
61 self.sock = self.p = self.pout = self.pin = None
62 is_reverse = os.environ.get('BUP_SERVER_REVERSE')
65 remote = '%s:' % is_reverse
66 (self.protocol, self.host, self.port, self.dir) = parse_remote(remote)
67 self.cachedir = git.repo('index-cache/%s'
68 % re.sub(r'[^@\w]', '_',
69 "%s:%s" % (self.host, self.dir)))
71 self.pout = os.fdopen(3, 'rb')
72 self.pin = os.fdopen(4, 'wb')
73 self.conn = Conn(self.pout, self.pin)
75 if self.protocol in ('ssh', 'file'):
77 # FIXME: ssh and file shouldn't use the same module
78 self.p = ssh.connect(self.host, self.port, 'server')
79 self.pout = self.p.stdout
80 self.pin = self.p.stdin
81 self.conn = Conn(self.pout, self.pin)
83 raise ClientError, 'connect: %s' % e, sys.exc_info()[2]
84 elif self.protocol == 'bup':
85 self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
86 self.sock.connect((self.host, atoi(self.port) or 1982))
87 self.sockw = self.sock.makefile('wb')
88 self.conn = DemuxConn(self.sock.fileno(), self.sockw)
90 self.dir = re.sub(r'[\r\n]', ' ', self.dir)
92 self.conn.write('init-dir %s\n' % self.dir)
94 self.conn.write('set-dir %s\n' % self.dir)
102 if e.errno == errno.EPIPE:
108 if self.conn and not self._busy:
109 self.conn.write('quit\n')
112 if self.sock and self.sockw:
114 self.sock.shutdown(socket.SHUT_WR)
125 raise ClientError('server tunnel returned exit code %d' % rv)
127 self.sock = self.p = self.pin = self.pout = None
133 raise ClientError('server exited unexpectedly with code %r'
136 return self.conn.check_ok()
137 except Exception as e:
138 raise ClientError, e, sys.exc_info()[2]
140 def check_busy(self):
142 raise ClientError('already busy with command %r' % self._busy)
144 def ensure_busy(self):
146 raise ClientError('expected to be busy, but not busy?!')
151 def sync_indexes(self):
154 mkdirp(self.cachedir)
155 # All cached idxs are extra until proven otherwise
157 for f in os.listdir(self.cachedir):
159 if f.endswith('.idx'):
162 conn.write('list-indexes\n')
163 for line in linereader(conn):
166 assert(line.find('/') < 0)
167 parts = line.split(' ')
169 if len(parts) == 2 and parts[1] == 'load' and idx not in extra:
170 # If the server requests that we load an idx and we don't
171 # already have a copy of it, it is needed
173 # Any idx that the server has heard of is proven not extra
177 debug1('client: removing extra indexes: %s\n' % extra)
179 os.unlink(os.path.join(self.cachedir, idx))
180 debug1('client: server requested load of: %s\n' % needed)
183 git.auto_midx(self.cachedir)
185 def sync_index(self, name):
186 #debug1('requesting %r\n' % name)
188 mkdirp(self.cachedir)
189 fn = os.path.join(self.cachedir, name)
190 if os.path.exists(fn):
191 msg = "won't request existing .idx, try `bup bloom --check %s`" % fn
192 raise ClientError(msg)
193 self.conn.write('send-index %s\n' % name)
194 n = struct.unpack('!I', self.conn.read(4))[0]
196 with atomically_replaced_file(fn, 'w') as f:
198 progress('Receiving index from server: %d/%d\r' % (count, n))
199 for b in chunkyreader(self.conn, n):
202 qprogress('Receiving index from server: %d/%d\r' % (count, n))
203 progress('Receiving index from server: %d/%d, done.\n' % (count, n))
206 def _make_objcache(self):
207 return git.PackIdxList(self.cachedir)
209 def _suggest_packs(self):
212 assert(ob == 'receive-objects-v2')
213 self.conn.write('\xff\xff\xff\xff') # suspend receive-objects-v2
215 for line in linereader(self.conn):
218 debug2('%s\n' % line)
219 if line.startswith('index '):
221 debug1('client: received index suggestion: %s\n'
222 % git.shorten_hash(idx))
223 suggested.append(idx)
225 assert(line.endswith('.idx'))
226 debug1('client: completed writing pack, idx: %s\n'
227 % git.shorten_hash(line))
228 suggested.append(line)
233 for idx in suggested:
235 git.auto_midx(self.cachedir)
238 self.conn.write('%s\n' % ob)
241 def new_packwriter(self, compression_level = 1):
244 self._busy = 'receive-objects-v2'
245 self.conn.write('receive-objects-v2\n')
246 return PackWriter_Remote(self.conn,
247 objcache_maker = self._make_objcache,
248 suggest_packs = self._suggest_packs,
250 onclose = self._not_busy,
251 ensure_busy = self.ensure_busy,
252 compression_level = compression_level)
254 def read_ref(self, refname):
256 self.conn.write('read-ref %s\n' % refname)
257 r = self.conn.readline().strip()
260 assert(len(r) == 40) # hexified sha
261 return r.decode('hex')
263 return None # nonexistent ref
265 def update_ref(self, refname, newval, oldval):
267 self.conn.write('update-ref %s\n%s\n%s\n'
268 % (refname, newval.encode('hex'),
269 (oldval or '').encode('hex')))
275 self.conn.write('cat %s\n' % re.sub(r'[\n\r]', '_', id))
277 sz = struct.unpack('!I', self.conn.read(4))[0]
279 yield self.conn.read(sz)
283 raise KeyError(str(e))
286 class PackWriter_Remote(git.PackWriter):
287 def __init__(self, conn, objcache_maker, suggest_packs,
290 compression_level=1):
291 git.PackWriter.__init__(self, objcache_maker)
293 self.filename = 'remote socket'
294 self.suggest_packs = suggest_packs
296 self.onclose = onclose
297 self.ensure_busy = ensure_busy
298 self._packopen = False
300 self._bwtime = time.time()
303 if not self._packopen:
305 self._packopen = True
308 if self._packopen and self.file:
309 self.file.write('\0\0\0\0')
310 self._packopen = False
311 self.onclose() # Unbusy
313 return self.suggest_packs() # Returns last idx received
321 raise ClientError("don't know how to abort remote pack writing")
323 def _raw_write(self, datalist, sha):
325 if not self._packopen:
328 data = ''.join(datalist)
331 crc = zlib.crc32(data) & 0xffffffff
332 outbuf = ''.join((struct.pack('!I', len(data) + 20 + 4),
334 struct.pack('!I', crc),
337 (self._bwcount, self._bwtime) = _raw_write_bwlimit(
338 self.file, outbuf, self._bwcount, self._bwtime)
340 raise ClientError, e, sys.exc_info()[2]
341 self.outbytes += len(data)
344 if self.file.has_input():
346 self.objcache.refresh()