1802
1811
real_handlers[kind](abspath, relpath)
1814
def copy_ownership(dst, src=None):
1815
"""Copy usr/grp ownership from src file/dir to dst file/dir.
1817
If src is None, the containing directory is used as source. If chown
1818
fails, the error is ignored and a warning is printed.
1820
chown = getattr(os, 'chown', None)
1825
src = os.path.dirname(dst)
1831
chown(dst, s.st_uid, s.st_gid)
1833
trace.warning("Unable to copy ownership from '%s' to '%s': IOError: %s." % (src, dst, e))
1836
def mkdir_with_ownership(path, ownership_src=None):
1837
"""Create the directory 'path' with specified ownership.
1839
If ownership_src is given, copies (chown) usr/grp ownership
1840
from 'ownership_src' to 'path'. If ownership_src is None, use the
1841
containing dir ownership.
1844
copy_ownership(path, ownership_src)
1847
def open_with_ownership(filename, mode='r', bufsize=-1, ownership_src=None):
1848
"""Open the file 'filename' with the specified ownership.
1850
If ownership_src is specified, copy usr/grp ownership from ownership_src
1851
to filename. If ownership_src is None, copy ownership from containing
1853
Returns the opened file object.
1855
f = open(filename, mode, bufsize)
1856
copy_ownership(filename, ownership_src)
1805
1860
def path_prefix_key(path):
1806
1861
"""Generate a prefix-order path key for path.
1907
1962
return socket.gethostname().decode(get_user_encoding())
1910
def recv_all(socket, bytes):
1965
# We must not read/write any more than 64k at a time from/to a socket so we
1966
# don't risk "no buffer space available" errors on some platforms. Windows in
1967
# particular is likely to throw WSAECONNABORTED or WSAENOBUFS if given too much
1969
MAX_SOCKET_CHUNK = 64 * 1024
1971
def read_bytes_from_socket(sock, report_activity=None,
1972
max_read_size=MAX_SOCKET_CHUNK):
1973
"""Read up to max_read_size of bytes from sock and notify of progress.
1975
Translates "Connection reset by peer" into file-like EOF (return an
1976
empty string rather than raise an error), and repeats the recv if
1977
interrupted by a signal.
1981
bytes = sock.recv(max_read_size)
1982
except socket.error, e:
1984
if eno == getattr(errno, "WSAECONNRESET", errno.ECONNRESET):
1985
# The connection was closed by the other side. Callers expect
1986
# an empty string to signal end-of-stream.
1988
elif eno == errno.EINTR:
1989
# Retry the interrupted recv.
1993
if report_activity is not None:
1994
report_activity(len(bytes), 'read')
1998
def recv_all(socket, count):
1911
1999
"""Receive an exact number of bytes.
1913
2001
Regular Socket.recv() may return less than the requested number of bytes,
1914
dependning on what's in the OS buffer. MSG_WAITALL is not available
2002
depending on what's in the OS buffer. MSG_WAITALL is not available
1915
2003
on all platforms, but this should work everywhere. This will return
1916
2004
less than the requested amount if the remote end closes.
1918
2006
This isn't optimized and is intended mostly for use in testing.
1921
while len(b) < bytes:
1922
new = until_no_eintr(socket.recv, bytes - len(b))
2009
while len(b) < count:
2010
new = read_bytes_from_socket(socket, None, count - len(b))
1929
def send_all(socket, bytes, report_activity=None):
2017
def send_all(sock, bytes, report_activity=None):
1930
2018
"""Send all bytes on a socket.
1932
Regular socket.sendall() can give socket error 10053 on Windows. This
1933
implementation sends no more than 64k at a time, which avoids this problem.
2020
Breaks large blocks in smaller chunks to avoid buffering limitations on
2021
some platforms, and catches EINTR which may be thrown if the send is
2022
interrupted by a signal.
2024
This is preferred to socket.sendall(), because it avoids portability bugs
2025
and provides activity reporting.
1935
2027
:param report_activity: Call this as bytes are read, see
1936
2028
Transport._report_activity
1939
for pos in xrange(0, len(bytes), chunk_size):
1940
block = bytes[pos:pos+chunk_size]
1941
if report_activity is not None:
1942
report_activity(len(block), 'write')
1943
until_no_eintr(socket.sendall, block)
2031
byte_count = len(bytes)
2032
while sent_total < byte_count:
2034
sent = sock.send(buffer(bytes, sent_total, MAX_SOCKET_CHUNK))
2035
except socket.error, e:
2036
if e.args[0] != errno.EINTR:
2040
report_activity(sent, 'write')
1946
2043
def dereference_path(path):
2019
2116
def until_no_eintr(f, *a, **kw):
2020
"""Run f(*a, **kw), retrying if an EINTR error occurs."""
2117
"""Run f(*a, **kw), retrying if an EINTR error occurs.
2119
WARNING: you must be certain that it is safe to retry the call repeatedly
2120
if EINTR does occur. This is typically only true for low-level operations
2121
like os.read. If in any doubt, don't use this.
2123
Keep in mind that this is not a complete solution to EINTR. There is
2124
probably code in the Python standard library and other dependencies that
2125
may encounter EINTR if a signal arrives (and there is signal handler for
2126
that signal). So this function can reduce the impact for IO that bzrlib
2127
directly controls, but it is not a complete solution.
2021
2129
# Borrowed from Twisted's twisted.python.util.untilConcludes function.