def _raw_write(self, datalist, sha): assert(self.file) if not self._packopen: self._open() self.ensure_busy() data = b''.join(datalist) assert(data) assert(sha) crc = zlib.crc32(data) & 0xffffffff outbuf = b''.join((struct.pack('!I', len(data) + 20 + 4), sha, struct.pack('!I', crc), data)) try: (self._bwcount, self._bwtime) = _raw_write_bwlimit( self.file, outbuf, self._bwcount, self._bwtime) except IOError as e: reraise(ClientError(e)) self.outbytes += len(data) self.count += 1 if self.file.has_input(): self.suggest_packs() self.objcache.refresh() return sha, crc
def check_ok(self): if self.p: rv = self.p.poll() if rv != None: raise ClientError('server exited unexpectedly with code %r' % rv) try: return self.conn.check_ok() except Exception as e: reraise(ClientError(e))
def __init__(self, remote, create=False): self.closed = False self._busy = self.conn = None self.sock = self.p = self.pout = self.pin = None try: is_reverse = environ.get(b'BUP_SERVER_REVERSE') if is_reverse: assert(not remote) remote = b'%s:' % is_reverse (self.protocol, self.host, self.port, self.dir) = parse_remote(remote) # The b'None' here matches python2's behavior of b'%s' % None == 'None', # python3 will (as of version 3.7.5) do the same for str ('%s' % None), # but crashes instead when doing b'%s' % None. cachehost = b'None' if self.host is None else self.host cachedir = b'None' if self.dir is None else self.dir self.cachedir = git.repo(b'index-cache/%s' % re.sub(br'[^@\w]', b'_', b'%s:%s' % (cachehost, cachedir))) if is_reverse: self.pout = os.fdopen(3, 'rb') self.pin = os.fdopen(4, 'wb') self.conn = Conn(self.pout, self.pin) else: if self.protocol in (b'ssh', b'file'): try: # FIXME: ssh and file shouldn't use the same module self.p = ssh.connect(self.host, self.port, b'server') self.pout = self.p.stdout self.pin = self.p.stdin self.conn = Conn(self.pout, self.pin) except OSError as e: reraise(ClientError('connect: %s' % e)) elif self.protocol == b'bup': self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.connect((self.host, 1982 if self.port is None else int(self.port))) self.sockw = self.sock.makefile('wb') self.conn = DemuxConn(self.sock.fileno(), self.sockw) self._available_commands = self._get_available_commands() self._require_command(b'init-dir') self._require_command(b'set-dir') if self.dir: self.dir = re.sub(br'[\r\n]', ' ', self.dir) if create: self.conn.write(b'init-dir %s\n' % self.dir) else: self.conn.write(b'set-dir %s\n' % self.dir) self.check_ok() self.sync_indexes() except BaseException as ex: with pending_raise(ex): self.close()
def __init__(self, remote, create=False): self._busy = self.conn = None self.sock = self.p = self.pout = self.pin = None (self.protocol, self.host, self.port, self.dir) = parse_remote(remote) if self.protocol == b'reverse': self.pout = os.fdopen(3, 'rb') self.pin = os.fdopen(4, 'wb') self.conn = Conn(self.pout, self.pin) if self.protocol in (b'ssh', b'file'): try: # FIXME: ssh and file shouldn't use the same module self.p = ssh.connect(self.host, self.port, b'server') self.pout = self.p.stdout self.pin = self.p.stdin self.conn = Conn(self.pout, self.pin) except OSError as e: reraise(ClientError('connect: %s' % e)) elif self.protocol == b'bup': self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.connect((self.host, 1982 if self.port is None else int(self.port))) self.sockw = self.sock.makefile('wb') self.conn = DemuxConn(self.sock.fileno(), self.sockw) self._available_commands = self._get_available_commands() self._require_command(b'init-dir') self._require_command(b'set-dir') if self.dir: self.dir = re.sub(br'[\r\n]', ' ', self.dir) if create: self.conn.write(b'init-dir %s\n' % self.dir) else: self.conn.write(b'set-dir %s\n' % self.dir) self.check_ok() # Set up the index-cache directory, prefer using the repo-id # if the remote repo has one (that can be accessed) repo_id = self.config(b'bup.repo-id') if repo_id is not None: self.cachedir = path.cachedir(repo_id) else: # The b'None' here matches python2's behavior of b'%s' % None == 'None', # python3 will (as of version 3.7.5) do the same for str ('%s' % None), # but crashes instead when doing b'%s' % None. cachehost = b'None' if self.host is None else self.host cachedir = b'None' if self.dir is None else self.dir self.cachedir = path.cachedir(re.sub(br'[^@\w]', b'_', b'%s:%s' % (cachehost, cachedir))) self.sync_indexes()
def _raw_write(self, datalist, sha): self._open() f = self.file # in case we get interrupted (eg. KeyboardInterrupt), it's best if # the file never has a *partial* blob. So let's make sure it's # all-or-nothing. (The blob shouldn't be very big anyway, thanks # to our hashsplit algorithm.) f.write() does its own buffering, # but that's okay because we'll flush it in _end(). oneblob = b''.join(datalist) try: f.write(oneblob) except IOError as e: reraise(GitError(e)) nw = len(oneblob) crc = zlib.crc32(oneblob) & 0xffffffff self._update_idx(sha, crc, nw) self.outbytes += nw self.count += 1 return nw, crc
def _join_queue_check(self): self._queue.join() if self.exc: self.join() # clean up the thread reraise(Exception(self.exc[1]), self.exc[2])