#
# This code is covered under the terms of the GNU Library General
# Public License as described in the bup LICENSE file.
-
-import errno, os, sys, stat, pwd, grp, struct, xattr, posix1e, re
-
+import errno, os, sys, stat, pwd, grp, struct, re
from cStringIO import StringIO
from bup import vint
from bup.drecurse import recursive_dirlist
from bup.xstat import utime, lutime, lstat, FSTime
import bup._helpers as _helpers
-if _helpers.get_linux_file_attr:
+try:
+ import xattr
+except ImportError:
+ log('Warning: Linux xattr support missing; install python-pyxattr.\n')
+ xattr = None
+if xattr:
+ try:
+ xattr.get_all
+ except AttributeError:
+ log('Warning: python-xattr module is too old; '
+ 'install python-pyxattr instead.\n')
+ xattr = None
+try:
+ import posix1e
+except ImportError:
+ log('Warning: POSIX ACL support missing; install python-pylibacl.\n')
+ posix1e = None
+try:
from bup._helpers import get_linux_file_attr, set_linux_file_attr
+except ImportError:
+ # No need for a warning here; the only reason they won't exist is that we're
+ # not on Linux, in which case files don't have any linux attrs anyway, so
+ # lacking the functions isn't a problem.
+ get_linux_file_attr = set_linux_file_attr = None
+
# WARNING: the metadata encoding is *not* stable yet. Caveat emptor!
# Take everything after any '/../'.
pos = result.rfind('/../')
- if(pos != -1):
+ if pos != -1:
result = result[result.rfind('/../') + 4:]
# Take everything after any remaining '../'.
st = None
try:
st = lstat(path)
- except IOError, e:
+ except OSError, e:
if e.errno != errno.ENOENT:
raise
if st:
os.unlink(path)
if stat.S_ISREG(self.mode):
- os.mknod(path, 0600 | stat.S_IFREG)
+ fd = os.open(path, os.O_CREAT|os.O_WRONLY|os.O_EXCL, 0600)
+ os.close(fd)
elif stat.S_ISDIR(self.mode):
os.mkdir(path, 0700)
elif stat.S_ISCHR(self.mode):
elif stat.S_ISFIFO(self.mode):
os.mknod(path, 0600 | stat.S_IFIFO)
elif stat.S_ISLNK(self.mode):
- if(self.symlink_target and create_symlinks):
- os.symlink(self.symlink_target, path)
+ if self.symlink_target and create_symlinks:
+ # on MacOS, symlink() permissions depend on umask, and there's no
+ # way to chown a symlink after creating it, so we have to
+ # be careful here!
+ oldumask = os.umask((self.mode & 0777) ^ 0777)
+ try:
+ os.symlink(self.symlink_target, path)
+ finally:
+ os.umask(oldumask)
# FIXME: S_ISDOOR, S_IFMPB, S_IFCMP, S_IFNWK, ... see stat(2).
# Otherwise, do nothing.
if stat.S_ISLNK(self.mode):
try:
lutime(path, (self.atime, self.mtime))
- except IOError, e:
+ except OSError, e:
if e.errno == errno.EACCES:
raise ApplyError('lutime: %s' % e)
else:
else:
try:
utime(path, (self.atime, self.mtime))
- except IOError, e:
+ except OSError, e:
if e.errno == errno.EACCES:
raise ApplyError('utime: %s' % e)
else:
def _add_symlink_target(self, path, st):
try:
- if(stat.S_ISLNK(st.st_mode)):
+ if stat.S_ISLNK(st.st_mode):
self.symlink_target = os.readlink(path)
except OSError, e:
add_error('readlink: %s', e)
# The numeric/text distinction only matters when reading/restoring
# a stored record.
def _add_posix1e_acl(self, path, st):
+ if not posix1e: return
if not stat.S_ISLNK(st.st_mode):
try:
if posix1e.has_extended(path):
num_flags = posix1e.TEXT_ABBREVIATE | posix1e.TEXT_NUMERIC_IDS
acl_reps = [acls[0].to_any_text('', '\n', txt_flags),
acls[1].to_any_text('', '\n', num_flags)]
- if(len(acls) < 3):
+ if len(acls) < 3:
acl_reps += ['', '']
else:
acl_reps.append(acls[2].to_any_text('', '\n', txt_flags))
def _load_posix1e_acl_rec(self, port):
data = vint.read_bvec(port)
acl_reps = vint.unpack('ssss', data)
- if(acl_reps[2] == ''):
+ if acl_reps[2] == '':
acl_reps = acl_reps[:2]
self.posix1e_acl = [posix1e.ACL(text=x) for x in acl_reps]
def _apply_posix1e_acl_rec(self, path, restore_numeric_ids=False):
- if(self.posix1e_acl):
+ if not posix1e:
+ if self.posix1e_acl:
+ add_error("%s: can't restore ACLs; posix1e support missing.\n"
+ % path)
+ return
+ if self.posix1e_acl:
acls = self.posix1e_acl
- if(len(acls) > 2):
+ if len(acls) > 2:
if restore_numeric_ids:
acls[3].applyto(path, posix1e.ACL_TYPE_DEFAULT)
else:
## Linux attributes (lsattr(1), chattr(1))
def _add_linux_attr(self, path, st):
+ if not get_linux_file_attr: return
if stat.S_ISREG(st.st_mode) or stat.S_ISDIR(st.st_mode):
try:
attr = get_linux_file_attr(path)
- if(attr != 0):
+ if attr != 0:
self.linux_attr = attr
- except IOError, e:
+ except OSError, e:
if e.errno == errno.EACCES:
add_error('read Linux attr: %s' % e)
elif e.errno == errno.ENOTTY: # Inappropriate ioctl for device.
self.linux_attr = vint.unpack('V', data)[0]
def _apply_linux_attr_rec(self, path, restore_numeric_ids=False):
- if(self.linux_attr):
+ if self.linux_attr:
+ if not set_linux_file_attr:
+ add_error("%s: can't restore linuxattrs: "
+ "linuxattr support missing.\n" % path)
+ return
set_linux_file_attr(path, self.linux_attr)
## Linux extended attributes (getfattr(1), setfattr(1))
def _add_linux_xattr(self, path, st):
+ if not xattr: return
try:
self.linux_xattr = xattr.get_all(path, nofollow=True)
except EnvironmentError, e:
self.linux_xattr = result
def _apply_linux_xattr_rec(self, path, restore_numeric_ids=False):
+ if not xattr:
+ if self.linux_xattr:
+ add_error("%s: can't restore xattr; xattr support missing.\n"
+ % path)
+ return
existing_xattrs = set(xattr.list(path, nofollow=True))
- if(self.linux_xattr):
+ if self.linux_xattr:
for k, v in self.linux_xattr:
if k not in existing_xattrs \
or v != xattr.get(path, k, nofollow=True):
self.posix1e_acl_default = None
def write(self, port, include_path=True):
- records = [(_rec_tag_path, self._encode_path())] if include_path else []
+ records = include_path and [(_rec_tag_path, self._encode_path())] or []
records.extend([(_rec_tag_common, self._encode_common()),
(_rec_tag_symlink_target, self._encode_symlink_target()),
(_rec_tag_posix1e_acl, self._encode_posix1e_acl()),
tag = vint.read_vuint(port)
try: # From here on, EOF is an error.
result = Metadata()
- while(True): # only exit is error (exception) or _rec_tag_end
+ while True: # only exit is error (exception) or _rec_tag_end
if tag == _rec_tag_path:
result._load_path_rec(port)
elif tag == _rec_tag_common:
def from_path(path, statinfo=None, archive_path=None, save_symlinks=True):
result = Metadata()
result.path = archive_path
- st = statinfo if statinfo else lstat(path)
+ st = statinfo or lstat(path)
result._add_common(path, st)
- if(save_symlinks):
+ if save_symlinks:
result._add_symlink_target(path, st)
result._add_posix1e_acl(path, st)
result._add_linux_attr(path, st)
# Issue top-level rewrite warnings.
for path in paths:
safe_path = _clean_up_path_for_archive(path)
- if(safe_path != path):
+ if safe_path != path:
log('archiving "%s" as "%s"\n' % (path, safe_path))
start_dir = os.getcwd()