buglvl = atoi(os.environ.get('BUP_DEBUG', 0))
-# If the platform doesn't have fdatasync (OS X), fall back to fsync.
-try:
- fdatasync = os.fdatasync
-except AttributeError:
- fdatasync = os.fsync
+if sys.platform.startswith('darwin'):
+ # Apparently fsync on OS X doesn't guarantee to sync all the way down
+ import fcntl
+ fdatasync = lambda fd : fcntl.fcntl(fd, fcntl.F_FULLFSYNC)
+else: # If the platform doesn't have fdatasync, fall back to fsync
+ try:
+ fdatasync = os.fdatasync
+ except AttributeError:
+ fdatasync = os.fsync
# Write (blockingly) to sockets that may or may not be in blocking mode.
raise IOError('select(fd) returned without being writable')
try:
sz = os.write(fd, buf)
- except OSError, e:
+ except OSError as e:
if e.errno != errno.EAGAIN:
raise
assert(sz >= 0)
os.makedirs(d, mode)
else:
os.makedirs(d)
- except OSError, e:
+ except OSError as e:
if e.errno == errno.EEXIST:
pass
else:
"""
try:
os.unlink(f)
- except OSError, e:
+ except OSError as e:
if e.errno != errno.ENOENT:
raise
yield readpipe(command + sub_args, preexec_fn=preexec_fn)
-def realpath(p):
- """Get the absolute path of a file.
+def resolve_parent(p):
+ """Return the absolute path of a file without following any final symlink.
Behaves like os.path.realpath, but doesn't follow a symlink for the last
element. (ie. if 'p' itself is a symlink, this one won't follow it, but it
def fmincore(fd):
"""Return the mincore() data for fd as a bytearray whose values can be
- tested via MINCORE_INCORE"""
+ tested via MINCORE_INCORE, or None if fd does not fully
+ support the operation."""
st = os.fstat(fd)
if (st.st_size == 0):
return bytearray(0)
for ci in xrange(chunk_count):
pos = _fmincore_chunk_size * ci;
msize = min(_fmincore_chunk_size, st.st_size - pos)
- m = mmap.mmap(fd, msize, mmap.MAP_PRIVATE, 0, 0, pos)
+ try:
+ m = mmap.mmap(fd, msize, mmap.MAP_PRIVATE, 0, 0, pos)
+ except mmap.error as ex:
+ if ex.errno == errno.EINVAL or ex.errno == errno.ENODEV:
+ # Perhaps the file was a pipe, i.e. "... | bup split ..."
+ return None
+ raise ex
_mincore(m, msize, 0, result, ci * pages_per_chunk);
return result
For now we expect a string that contains a float."""
try:
date = float(str)
- except ValueError, e:
+ except ValueError as e:
raise fatal('invalid date format (should be a float): %r' % e)
else:
return date
for flag in options:
(option, parameter) = flag
if option == '--exclude':
- excluded_paths.append(realpath(parameter))
+ excluded_paths.append(resolve_parent(parameter))
elif option == '--exclude-from':
try:
- f = open(realpath(parameter))
- except IOError, e:
+ f = open(resolve_parent(parameter))
+ except IOError as e:
raise fatal("couldn't read %s" % parameter)
for exclude_path in f.readlines():
# FIXME: perhaps this should be rstrip('\n')
- exclude_path = realpath(exclude_path.strip())
+ exclude_path = resolve_parent(exclude_path.strip())
if exclude_path:
excluded_paths.append(exclude_path)
return sorted(frozenset(excluded_paths))
if option == '--exclude-rx':
try:
excluded_patterns.append(re.compile(parameter))
- except re.error, ex:
+ except re.error as ex:
fatal('invalid --exclude-rx pattern (%s): %s' % (parameter, ex))
elif option == '--exclude-rx-from':
try:
- f = open(realpath(parameter))
- except IOError, e:
+ f = open(resolve_parent(parameter))
+ except IOError as e:
raise fatal("couldn't read %s" % parameter)
for pattern in f.readlines():
spattern = pattern.rstrip('\n')
continue
try:
excluded_patterns.append(re.compile(spattern))
- except re.error, ex:
+ except re.error as ex:
fatal('invalid --exclude-rx pattern (%s): %s' % (spattern, ex))
return excluded_patterns