14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
from stat import (S_ISREG, S_ISDIR, S_ISLNK, ST_MODE, ST_SIZE,
21
S_ISCHR, S_ISBLK, S_ISFIFO, S_ISSOCK)
26
25
from bzrlib.lazy_import import lazy_import
27
26
lazy_import(globals(), """
29
27
from datetime import datetime
31
from ntpath import (abspath as _nt_abspath,
33
normpath as _nt_normpath,
34
realpath as _nt_realpath,
35
splitdrive as _nt_splitdrive,
31
# We need to import both shutil and rmtree as we export the later on posix
32
# and need the former on windows
34
from shutil import rmtree
37
# We need to import both tempfile and mkdtemp as we export the later on posix
38
# and need the former on windows
45
from tempfile import (
40
from tempfile import mkdtemp
50
43
from bzrlib import (
51
from bzrlib.symbol_versioning import (
57
56
# sha and md5 modules are deprecated in python2.6 but hashlib is available as
59
58
if sys.version_info < (2, 5):
320
321
# /path => C:/path
321
322
path = unicode(path)
322
323
# check for absolute path
323
drive = _nt_splitdrive(path)[0]
324
drive = ntpath.splitdrive(path)[0]
324
325
if drive == '' and path[:2] not in('//','\\\\'):
325
326
cwd = os.getcwdu()
326
327
# we cannot simply os.path.join cwd and path
327
328
# because os.path.join('C:','/path') produce '/path'
328
329
# and this is incorrect
329
330
if path[:1] in ('/','\\'):
330
cwd = _nt_splitdrive(cwd)[0]
331
cwd = ntpath.splitdrive(cwd)[0]
332
333
path = cwd + '\\' + path
333
return _win32_fixdrive(_nt_normpath(path).replace('\\', '/'))
334
return _win32_fixdrive(ntpath.normpath(path).replace('\\', '/'))
336
337
def _win32_realpath(path):
337
# Real _nt_realpath doesn't have a problem with a unicode cwd
338
return _win32_fixdrive(_nt_realpath(unicode(path)).replace('\\', '/'))
338
# Real ntpath.realpath doesn't have a problem with a unicode cwd
339
return _win32_fixdrive(ntpath.realpath(unicode(path)).replace('\\', '/'))
341
342
def _win32_pathjoin(*args):
342
return _nt_join(*args).replace('\\', '/')
343
return ntpath.join(*args).replace('\\', '/')
345
346
def _win32_normpath(path):
346
return _win32_fixdrive(_nt_normpath(unicode(path)).replace('\\', '/'))
347
return _win32_fixdrive(ntpath.normpath(unicode(path)).replace('\\', '/'))
349
350
def _win32_getcwd():
453
456
input_encoding = getattr(sys.stdin, 'encoding', None)
454
457
if not input_encoding:
455
458
output_encoding = get_user_encoding()
456
mutter('encoding stdout as osutils.get_user_encoding() %r',
460
mutter('encoding stdout as osutils.get_user_encoding() %r',
459
463
output_encoding = input_encoding
460
mutter('encoding stdout as sys.stdin encoding %r', output_encoding)
465
mutter('encoding stdout as sys.stdin encoding %r',
462
mutter('encoding stdout as sys.stdout encoding %r', output_encoding)
469
mutter('encoding stdout as sys.stdout encoding %r', output_encoding)
463
470
if output_encoding == 'cp0':
464
471
# invalid encoding (cp0 means 'no codepage' on Windows)
465
472
output_encoding = get_user_encoding()
466
mutter('cp0 is invalid encoding.'
474
mutter('cp0 is invalid encoding.'
467
475
' encoding stdout as osutils.get_user_encoding() %r',
1223
1233
# but for now, we haven't optimized...
1224
1234
return [canonical_relpath(base, p) for p in paths]
1237
def decode_filename(filename):
1238
"""Decode the filename using the filesystem encoding
1240
If it is unicode, it is returned.
1241
Otherwise it is decoded from the the filesystem's encoding. If decoding
1242
fails, a errors.BadFilenameEncoding exception is raised.
1244
if type(filename) is unicode:
1247
return filename.decode(_fs_enc)
1248
except UnicodeDecodeError:
1249
raise errors.BadFilenameEncoding(filename, _fs_enc)
1226
1252
def safe_unicode(unicode_or_utf8_string):
1227
1253
"""Coerce unicode_or_utf8_string into unicode.
1824
1861
real_handlers[kind](abspath, relpath)
1864
def copy_ownership_from_path(dst, src=None):
1865
"""Copy usr/grp ownership from src file/dir to dst file/dir.
1867
If src is None, the containing directory is used as source. If chown
1868
fails, the error is ignored and a warning is printed.
1870
chown = getattr(os, 'chown', None)
1875
src = os.path.dirname(dst)
1881
chown(dst, s.st_uid, s.st_gid)
1884
'Unable to copy ownership from "%s" to "%s". '
1885
'You may want to set it manually.', src, dst)
1886
trace.log_exception_quietly()
1827
1889
def path_prefix_key(path):
1828
1890
"""Generate a prefix-order path key for path.
1929
1995
return socket.gethostname().decode(get_user_encoding())
1932
def recv_all(socket, bytes):
1998
# We must not read/write any more than 64k at a time from/to a socket so we
1999
# don't risk "no buffer space available" errors on some platforms. Windows in
2000
# particular is likely to throw WSAECONNABORTED or WSAENOBUFS if given too much
2002
MAX_SOCKET_CHUNK = 64 * 1024
2004
_end_of_stream_errors = [errno.ECONNRESET]
2005
for _eno in ['WSAECONNRESET', 'WSAECONNABORTED']:
2006
_eno = getattr(errno, _eno, None)
2007
if _eno is not None:
2008
_end_of_stream_errors.append(_eno)
2012
def read_bytes_from_socket(sock, report_activity=None,
2013
max_read_size=MAX_SOCKET_CHUNK):
2014
"""Read up to max_read_size of bytes from sock and notify of progress.
2016
Translates "Connection reset by peer" into file-like EOF (return an
2017
empty string rather than raise an error), and repeats the recv if
2018
interrupted by a signal.
2022
bytes = sock.recv(max_read_size)
2023
except socket.error, e:
2025
if eno in _end_of_stream_errors:
2026
# The connection was closed by the other side. Callers expect
2027
# an empty string to signal end-of-stream.
2029
elif eno == errno.EINTR:
2030
# Retry the interrupted recv.
2034
if report_activity is not None:
2035
report_activity(len(bytes), 'read')
2039
def recv_all(socket, count):
1933
2040
"""Receive an exact number of bytes.
1935
2042
Regular Socket.recv() may return less than the requested number of bytes,
1936
dependning on what's in the OS buffer. MSG_WAITALL is not available
2043
depending on what's in the OS buffer. MSG_WAITALL is not available
1937
2044
on all platforms, but this should work everywhere. This will return
1938
2045
less than the requested amount if the remote end closes.
1940
2047
This isn't optimized and is intended mostly for use in testing.
1943
while len(b) < bytes:
1944
new = until_no_eintr(socket.recv, bytes - len(b))
2050
while len(b) < count:
2051
new = read_bytes_from_socket(socket, None, count - len(b))
1951
def send_all(socket, bytes, report_activity=None):
2058
def send_all(sock, bytes, report_activity=None):
1952
2059
"""Send all bytes on a socket.
1954
Regular socket.sendall() can give socket error 10053 on Windows. This
1955
implementation sends no more than 64k at a time, which avoids this problem.
2061
Breaks large blocks in smaller chunks to avoid buffering limitations on
2062
some platforms, and catches EINTR which may be thrown if the send is
2063
interrupted by a signal.
2065
This is preferred to socket.sendall(), because it avoids portability bugs
2066
and provides activity reporting.
1957
2068
:param report_activity: Call this as bytes are read, see
1958
2069
Transport._report_activity
1961
for pos in xrange(0, len(bytes), chunk_size):
1962
block = bytes[pos:pos+chunk_size]
1963
if report_activity is not None:
1964
report_activity(len(block), 'write')
1965
until_no_eintr(socket.sendall, block)
2072
byte_count = len(bytes)
2073
while sent_total < byte_count:
2075
sent = sock.send(buffer(bytes, sent_total, MAX_SOCKET_CHUNK))
2076
except socket.error, e:
2077
if e.args[0] != errno.EINTR:
2081
report_activity(sent, 'write')
2084
def connect_socket(address):
2085
# Slight variation of the socket.create_connection() function (provided by
2086
# python-2.6) that can fail if getaddrinfo returns an empty list. We also
2087
# provide it for previous python versions. Also, we don't use the timeout
2088
# parameter (provided by the python implementation) so we don't implement
2090
err = socket.error('getaddrinfo returns an empty list')
2091
host, port = address
2092
for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM):
2093
af, socktype, proto, canonname, sa = res
2096
sock = socket.socket(af, socktype, proto)
2100
except socket.error, err:
2101
# 'err' is now the most recent error
2102
if sock is not None:
1968
2107
def dereference_path(path):
2041
2182
def until_no_eintr(f, *a, **kw):
2042
"""Run f(*a, **kw), retrying if an EINTR error occurs."""
2183
"""Run f(*a, **kw), retrying if an EINTR error occurs.
2185
WARNING: you must be certain that it is safe to retry the call repeatedly
2186
if EINTR does occur. This is typically only true for low-level operations
2187
like os.read. If in any doubt, don't use this.
2189
Keep in mind that this is not a complete solution to EINTR. There is
2190
probably code in the Python standard library and other dependencies that
2191
may encounter EINTR if a signal arrives (and there is signal handler for
2192
that signal). So this function can reduce the impact for IO that bzrlib
2193
directly controls, but it is not a complete solution.
2043
2195
# Borrowed from Twisted's twisted.python.util.untilConcludes function.
2204
2358
return os.fdopen(os.open(filename, flags), mode, bufsize)
2206
2360
open_file = open
2363
def getuser_unicode():
2364
"""Return the username as unicode.
2367
user_encoding = get_user_encoding()
2368
username = getpass.getuser().decode(user_encoding)
2369
except UnicodeDecodeError:
2370
raise errors.BzrError("Can't decode username as %s." % \
2375
def available_backup_name(base, exists):
2376
"""Find a non-existing backup file name.
2378
This will *not* create anything, this only return a 'free' entry. This
2379
should be used for checking names in a directory below a locked
2380
tree/branch/repo to avoid race conditions. This is LBYL (Look Before You
2381
Leap) and generally discouraged.
2383
:param base: The base name.
2385
:param exists: A callable returning True if the path parameter exists.
2388
name = "%s.~%d~" % (base, counter)
2391
name = "%s.~%d~" % (base, counter)
2395
def set_fd_cloexec(fd):
2396
"""Set a Unix file descriptor's FD_CLOEXEC flag. Do nothing if platform
2397
support for this is not available.
2401
old = fcntl.fcntl(fd, fcntl.F_GETFD)
2402
fcntl.fcntl(fd, fcntl.F_SETFD, old | fcntl.FD_CLOEXEC)
2403
except (ImportError, AttributeError):
2404
# Either the fcntl module or specific constants are not present