/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

  • Committer: Andrew Bennetts
  • Date: 2010-03-18 23:11:15 UTC
  • mto: This revision was merged to the branch mainline in revision 5117.
  • Revision ID: andrew.bennetts@canonical.com-20100318231115-zrjij60gnj7qg6jw
Consolidate changes, try to minimise unnecessary changes and tidy up those that kept.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1893
1893
        return socket.gethostname().decode(get_user_encoding())
1894
1894
 
1895
1895
 
1896
 
@deprecated_function(deprecated_in((2, 2, 0)))
1897
 
def recv_all(socket, bytes):
1898
 
    """See bzrlib.tests.test_smart_transport._recv_all"""
1899
 
    return test_smart_transport._recv_all(socket, bytes)
1900
 
 
1901
 
 
1902
 
_MAX_SOCKET_CHUNK = 64 * 1024
 
1896
# We must not read/write any more than 64k at a time from/to a socket so we
 
1897
# don't risk "no buffer space available" errors on some platforms.  Windows in
 
1898
# particular is likely to throw WSAECONNABORTED or WSAENOBUFS if given too much
 
1899
# data at once.
 
1900
MAX_SOCKET_CHUNK = 64 * 1024
 
1901
 
 
1902
def read_bytes_from_socket(sock, report_activity,
 
1903
        max_read_size=MAX_SOCKET_CHUNK):
 
1904
    """Read up to max_read_size of bytes from sock and notify of progress.
 
1905
 
 
1906
    Translates "Connection reset by peer" into file-like EOF (return an
 
1907
    empty string rather than raise an error), and repeats the recv if
 
1908
    interrupted by a signal.
 
1909
    """
 
1910
    while 1:
 
1911
        try:
 
1912
            bytes = sock.recv(max_read_size)
 
1913
        except socket.error, e:
 
1914
            eno = e.args[0]
 
1915
            if eno == getattr(errno, "WSAECONNRESET", errno.ECONNRESET):
 
1916
                # The connection was closed by the other side.  Callers expect
 
1917
                # an empty string to signal end-of-stream.
 
1918
                return ""
 
1919
            elif eno == errno.EINTR:
 
1920
                # Retry the interrupted recv.
 
1921
                continue
 
1922
            raise
 
1923
        else:
 
1924
            report_activity(len(bytes), 'read')
 
1925
            return bytes
 
1926
 
 
1927
 
 
1928
def recv_all(socket, count):
 
1929
    """Receive an exact number of bytes.
 
1930
 
 
1931
    Regular Socket.recv() may return less than the requested number of bytes,
 
1932
    depending on what's in the OS buffer.  MSG_WAITALL is not available
 
1933
    on all platforms, but this should work everywhere.  This will return
 
1934
    less than the requested amount if the remote end closes.
 
1935
 
 
1936
    This isn't optimized and is intended mostly for use in testing.
 
1937
    """
 
1938
    b = ''
 
1939
    reporter = lambda n, rw: None
 
1940
    while len(b) < count:
 
1941
        new = read_bytes_from_socket(socket, reporter, count - len(b))
 
1942
        if new == '':
 
1943
            break # eof
 
1944
        b += new
 
1945
    return b
 
1946
 
1903
1947
 
1904
1948
def send_all(sock, bytes, report_activity=None):
1905
1949
    """Send all bytes on a socket.
1908
1952
    some platforms, and catches EINTR which may be thrown if the send is
1909
1953
    interrupted by a signal.
1910
1954
 
1911
 
    This is preferred to socket.sendall(), because it avoids bugs and provides
1912
 
    reasonable activity reporting.
 
1955
    This is preferred to socket.sendall(), because it avoids portability bugs
 
1956
    and provides activity reporting.
1913
1957
 
1914
1958
    :param report_activity: Call this as bytes are read, see
1915
1959
        Transport._report_activity
1918
1962
    byte_count = len(bytes)
1919
1963
    while sent_total < byte_count:
1920
1964
        try:
1921
 
            sent = sock.send(buffer(bytes, sent_total, _MAX_SOCKET_CHUNK))
 
1965
            sent = sock.send(buffer(bytes, sent_total, MAX_SOCKET_CHUNK))
1922
1966
        except socket.error, e:
1923
1967
            if e.args[0] != errno.EINTR:
1924
1968
                raise