]> arthur.barton.de Git - bup.git/blobdiff - lib/bup/metadata.py
Skip unset values in metadata repr
[bup.git] / lib / bup / metadata.py
index 9c6d46a987516f3522df5896bb64b156959215c1..c560abb9a9062d20549360f1d8f6126ad57738c8 100644 (file)
@@ -4,8 +4,12 @@
 #
 # This code is covered under the terms of the GNU Library General
 # Public License as described in the bup LICENSE file.
+
+from errno import EACCES, EINVAL, ENOTTY, ENOSYS, EOPNOTSUPP
+from io import BytesIO
+from time import gmtime, strftime
 import errno, os, sys, stat, time, pwd, grp, socket, struct
-from cStringIO import StringIO
+
 from bup import vint, xstat
 from bup.drecurse import recursive_dirlist
 from bup.helpers import add_error, mkdirp, log, is_superuser, format_filesize
@@ -186,6 +190,8 @@ _rec_tag_linux_xattr = 7      # getfattr(1) setfattr(1)
 _rec_tag_hardlink_target = 8 # hard link target path
 _rec_tag_common_v2 = 9 # times, user, group, type, perms, etc. (current)
 
+_warned_about_attr_einval = None
+
 
 class ApplyError(Exception):
     # Thrown when unable to apply any given bit of metadata to a path.
@@ -306,14 +312,14 @@ class Metadata:
         st = None
         try:
             st = xstat.lstat(path)
-        except OSError, e:
+        except OSError as e:
             if e.errno != errno.ENOENT:
                 raise
         if st:
             if stat.S_ISDIR(st.st_mode):
                 try:
                     os.rmdir(path)
-                except OSError, e:
+                except OSError as e:
                     if e.errno in (errno.ENOTEMPTY, errno.EEXIST):
                         msg = 'refusing to overwrite non-empty dir ' + path
                         raise Exception(msg)
@@ -323,24 +329,24 @@ class Metadata:
 
         if stat.S_ISREG(self.mode):
             assert(self._recognized_file_type())
-            fd = os.open(path, os.O_CREAT|os.O_WRONLY|os.O_EXCL, 0600)
+            fd = os.open(path, os.O_CREAT|os.O_WRONLY|os.O_EXCL, 0o600)
             os.close(fd)
         elif stat.S_ISDIR(self.mode):
             assert(self._recognized_file_type())
-            os.mkdir(path, 0700)
+            os.mkdir(path, 0o700)
         elif stat.S_ISCHR(self.mode):
             assert(self._recognized_file_type())
-            os.mknod(path, 0600 | stat.S_IFCHR, self.rdev)
+            os.mknod(path, 0o600 | stat.S_IFCHR, self.rdev)
         elif stat.S_ISBLK(self.mode):
             assert(self._recognized_file_type())
-            os.mknod(path, 0600 | stat.S_IFBLK, self.rdev)
+            os.mknod(path, 0o600 | stat.S_IFBLK, self.rdev)
         elif stat.S_ISFIFO(self.mode):
             assert(self._recognized_file_type())
-            os.mknod(path, 0600 | stat.S_IFIFO)
+            os.mknod(path, 0o600 | stat.S_IFIFO)
         elif stat.S_ISSOCK(self.mode):
             try:
-                os.mknod(path, 0600 | stat.S_IFSOCK)
-            except OSError, e:
+                os.mknod(path, 0o600 | stat.S_IFSOCK)
+            except OSError as e:
                 if e.errno in (errno.EINVAL, errno.EPERM):
                     s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
                     s.bind(path)
@@ -352,7 +358,7 @@ class Metadata:
                 # 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)
+                oldumask = os.umask((self.mode & 0o777) ^ 0o777)
                 try:
                     os.symlink(self.symlink_target, path)
                 finally:
@@ -372,7 +378,7 @@ class Metadata:
         if lutime and stat.S_ISLNK(self.mode):
             try:
                 lutime(path, (self.atime, self.mtime))
-            except OSError, e:
+            except OSError as e:
                 if e.errno == errno.EACCES:
                     raise ApplyError('lutime: %s' % e)
                 else:
@@ -380,7 +386,7 @@ class Metadata:
         else:
             try:
                 utime(path, (self.atime, self.mtime))
-            except OSError, e:
+            except OSError as e:
                 if e.errno == errno.EACCES:
                     raise ApplyError('utime: %s' % e)
                 else:
@@ -414,7 +420,7 @@ class Metadata:
         if uid != -1 or gid != -1:
             try:
                 os.lchown(path, uid, gid)
-            except OSError, e:
+            except OSError as e:
                 if e.errno == errno.EPERM:
                     add_error('lchown: %s' %  e)
                 elif sys.platform.startswith('cygwin') \
@@ -451,14 +457,16 @@ class Metadata:
         try:
             if stat.S_ISLNK(st.st_mode):
                 self.symlink_target = os.readlink(path)
-        except OSError, e:
+        except OSError as e:
             add_error('readlink: %s' % e)
 
     def _encode_symlink_target(self):
         return self.symlink_target
 
     def _load_symlink_target_rec(self, port):
-        self.symlink_target = vint.read_bvec(port)
+        target = vint.read_bvec(port)
+        self.symlink_target = target
+        self.size = len(target)
 
 
     ## Hardlink targets
@@ -486,7 +494,8 @@ class Metadata:
     # 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 posix1e or not posix1e.HAS_EXTENDED_CHECK:
+            return
         if not stat.S_ISLNK(st.st_mode):
             acls = None
             def_acls = None
@@ -497,7 +506,7 @@ class Metadata:
                     if stat.S_ISDIR(st.st_mode):
                         def_acl = posix1e.ACL(filedef=path)
                         def_acls = [def_acl, def_acl]
-            except EnvironmentError, e:
+            except EnvironmentError as e:
                 if e.errno not in (errno.EOPNOTSUPP, errno.ENOSYS):
                     raise
             if acls:
@@ -534,7 +543,7 @@ class Metadata:
         def apply_acl(acl_rep, kind):
             try:
                 acl = posix1e.ACL(text = acl_rep)
-            except IOError, e:
+            except IOError as e:
                 if e.errno == 0:
                     # pylibacl appears to return an IOError with errno
                     # set to 0 if a group referred to by the ACL rep
@@ -545,7 +554,7 @@ class Metadata:
                     raise
             try:
                 acl.applyto(path, kind)
-            except IOError, e:
+            except IOError as e:
                 if e.errno == errno.EPERM or e.errno == errno.EOPNOTSUPP:
                     raise ApplyError('POSIX1e ACL applyto: %s' % e)
                 else:
@@ -579,12 +588,20 @@ class Metadata:
                 attr = get_linux_file_attr(path)
                 if attr != 0:
                     self.linux_attr = attr
-            except OSError, e:
+            except OSError as e:
                 if e.errno == errno.EACCES:
                     add_error('read Linux attr: %s' % e)
-                elif e.errno in (errno.ENOTTY, errno.ENOSYS, errno.EOPNOTSUPP):
+                elif e.errno in (ENOTTY, ENOSYS, EOPNOTSUPP):
                     # Assume filesystem doesn't support attrs.
                     return
+                elif e.errno == EINVAL:
+                    global _warned_about_attr_einval
+                    if not _warned_about_attr_einval:
+                        log("Ignoring attr EINVAL;"
+                            + " if you're not using ntfs-3g, please report: "
+                            + repr(path) + '\n')
+                        _warned_about_attr_einval = True
+                    return
                 else:
                     raise
 
@@ -611,11 +628,14 @@ class Metadata:
                 return
             try:
                 set_linux_file_attr(path, self.linux_attr)
-            except OSError, e:
-                if e.errno in (errno.ENOTTY, errno.EOPNOTSUPP, errno.ENOSYS,
-                               errno.EACCES):
+            except OSError as e:
+                if e.errno in (EACCES, ENOTTY, EOPNOTSUPP, ENOSYS):
                     raise ApplyError('Linux chattr: %s (0x%s)'
                                      % (e, hex(self.linux_attr)))
+                elif e.errno == EINVAL:
+                    msg = "if you're not using ntfs-3g, please report"
+                    raise ApplyError('Linux chattr: %s (0x%s) (%s)'
+                                     % (e, hex(self.linux_attr), msg))
                 else:
                     raise
 
@@ -626,7 +646,7 @@ class Metadata:
         if not xattr: return
         try:
             self.linux_xattr = xattr.get_all(path, nofollow=True)
-        except EnvironmentError, e:
+        except EnvironmentError as e:
             if e.errno != errno.EOPNOTSUPP:
                 raise
 
@@ -645,7 +665,7 @@ class Metadata:
 
     def _load_linux_xattr_rec(self, file):
         data = vint.read_bvec(file)
-        memfile = StringIO(data)
+        memfile = BytesIO(data)
         result = []
         for i in range(vint.read_vuint(memfile)):
             key = vint.read_bvec(memfile)
@@ -663,7 +683,7 @@ class Metadata:
             return
         try:
             existing_xattrs = set(xattr.list(path, nofollow=True))
-        except IOError, e:
+        except IOError as e:
             if e.errno == errno.EACCES:
                 raise ApplyError('xattr.set %r: %s' % (path, e))
             else:
@@ -673,7 +693,7 @@ class Metadata:
                     or v != xattr.get(path, k, nofollow=True):
                 try:
                     xattr.set(path, k, v, nofollow=True)
-                except IOError, e:
+                except IOError as e:
                     if e.errno == errno.EPERM \
                             or e.errno == errno.EOPNOTSUPP:
                         raise ApplyError('xattr.set %r: %s' % (path, e))
@@ -683,8 +703,8 @@ class Metadata:
         for k in existing_xattrs:
             try:
                 xattr.remove(path, k, nofollow=True)
-            except IOError, e:
-                if e.errno == errno.EPERM:
+            except IOError as e:
+                if e.errno in (errno.EPERM, errno.EACCES):
                     raise ApplyError('xattr.remove %r: %s' % (path, e))
                 else:
                     raise
@@ -703,28 +723,30 @@ class Metadata:
 
     def __repr__(self):
         result = ['<%s instance at %s' % (self.__class__, hex(id(self)))]
-        if self.path:
+        if self.path is not None:
             result += ' path:' + repr(self.path)
-        if self.mode:
+        if self.mode is not None:
             result += ' mode:' + repr(xstat.mode_str(self.mode)
                                       + '(%s)' % hex(self.mode))
-        if self.uid:
+        if self.uid is not None:
             result += ' uid:' + str(self.uid)
-        if self.gid:
+        if self.gid is not None:
             result += ' gid:' + str(self.gid)
-        if self.user:
+        if self.user is not None:
             result += ' user:' + repr(self.user)
-        if self.group:
+        if self.group is not None:
             result += ' group:' + repr(self.group)
-        if self.size:
+        if self.size is not None:
             result += ' size:' + repr(self.size)
         for name, val in (('atime', self.atime),
                           ('mtime', self.mtime),
                           ('ctime', self.ctime)):
-            result += ' %s:%r' \
-                % (name,
-                   time.strftime('%Y-%m-%d %H:%M %z',
-                                 time.gmtime(xstat.fstime_floor_secs(val))))
+            if val is not None:
+                result += ' %s:%r (%d)' \
+                          % (name,
+                             strftime('%Y-%m-%d %H:%M %z',
+                                      gmtime(xstat.fstime_floor_secs(val))),
+                             val)
         result += '>'
         return ''.join(result)
 
@@ -745,7 +767,7 @@ class Metadata:
         vint.write_vuint(port, _rec_tag_end)
 
     def encode(self, include_path=True):
-        port = StringIO()
+        port = BytesIO()
         self.write(port, include_path)
         return port.getvalue()
 
@@ -809,7 +831,7 @@ class Metadata:
                                self._apply_linux_xattr_rec):
             try:
                 apply_metadata(path, restore_numeric_ids=num_ids)
-            except ApplyError, e:
+            except ApplyError as e:
                 add_error(e)
 
     def same_file(self, other):
@@ -921,7 +943,7 @@ def summary_str(meta, numeric_ids = False, classification = None,
         mode_str = xstat.mode_str(meta.mode)
         symlink_target = meta.symlink_target
         mtime_secs = xstat.fstime_floor_secs(meta.mtime)
-        mtime_str = time.strftime('%Y-%m-%d %H:%M', time.localtime(mtime_secs))
+        mtime_str = strftime('%Y-%m-%d %H:%M', time.localtime(mtime_secs))
         if meta.user and not numeric_ids:
             user_str = meta.user
         elif meta.uid != None: