Jelajahi Sumber

Fixed linux xattr issue

Jonas Borgström 14 tahun lalu
induk
melakukan
9e35c56adb
2 mengubah file dengan 16 tambahan dan 7 penghapusan
  1. 15 6
      darc/archive.py
  2. 1 1
      darc/test.py

+ 15 - 6
darc/archive.py

@@ -14,6 +14,7 @@ from .helpers import uid2user, user2uid, gid2group, group2gid, IntegrityError
 CHUNK_SIZE = 55001
 
 have_lchmod = hasattr(os, 'lchmod')
+linux = sys.platform == 'linux'
 
 
 class Archive(object):
@@ -147,12 +148,12 @@ class Archive(object):
     def restore_attrs(self, path, item, symlink=False):
         xattrs = item.get('xattrs')
         if xattrs:
-            try:
-                xa = xattr(path, XATTR_NOFOLLOW)
-                for k, v in xattrs.items():
+            xa = xattr(path, XATTR_NOFOLLOW)
+            for k, v in xattrs.items():
+                try:
                     xa.set(k, v)
-            except IOError:
-                pass
+                except KeyError:
+                    pass
         if have_lchmod:
             os.lchmod(path, item['mode'])
         elif not symlink:
@@ -196,7 +197,15 @@ class Archive(object):
             'atime': st.st_atime, 'mtime': st.st_mtime,
         }
         try:
-            item['xattrs'] = dict(xattr(path, XATTR_NOFOLLOW))
+            xa = xattr(path, XATTR_NOFOLLOW)
+            xattrs = {}
+            for key in xa:
+                # Only store the user namespace on Linux
+                if linux and not key.startswith('user'):
+                    continue
+                xattrs[key] = xa[key]
+            if xattrs:
+                item['xattrs'] = xattrs
         except IOError:
             pass
         return item

+ 1 - 1
darc/test.py

@@ -87,7 +87,7 @@ class Test(unittest.TestCase):
         self.create_regual_file('file1', size=1024*80)
         self.create_regual_file('dir2/file2', size=1024*80)
         x = xattr(os.path.join(self.input_path, 'file1'))
-        x.set('user:foo', 'bar')
+        x.set('user.foo', 'bar')
         os.symlink('somewhere', os.path.join(self.input_path, 'link1'))
         os.mkfifo(os.path.join(self.input_path, 'fifo1'))
         self.darc('create', self.store_path + '::test', 'input')