Преглед изворни кода

Item: disallow None value for .user/group/chunks/chunks_healthy

If we do not know the value, just do not have that key/value pair in the item.
Thomas Waldmann пре 3 година
родитељ
комит
f2b085787b
5 измењених фајлова са 22 додато и 25 уклоњено
  1. 14 9
      src/borg/archive.py
  2. 2 2
      src/borg/archiver.py
  3. 2 2
      src/borg/helpers/parseformat.py
  4. 4 4
      src/borg/item.pyx
  5. 0 8
      src/borg/testsuite/item.py

+ 14 - 9
src/borg/archive.py

@@ -392,14 +392,14 @@ def get_item_uid_gid(item, *, numeric, uid_forced=None, gid_forced=None, uid_def
     if uid_forced is not None:
         uid = uid_forced
     else:
-        uid = None if numeric else user2uid(item.user)
+        uid = None if numeric else user2uid(item.get('user'))
         uid = item.uid if uid is None else uid
         if uid < 0:
             uid = uid_default
     if gid_forced is not None:
         gid = gid_forced
     else:
-        gid = None if numeric else group2gid(item.group)
+        gid = None if numeric else group2gid(item.get('group'))
         gid = item.gid if gid is None else gid
         if gid < 0:
             gid = gid_default
@@ -1089,11 +1089,13 @@ class MetadataCollector:
         if not self.nobirthtime and hasattr(st, 'st_birthtime'):
             # sadly, there's no stat_result.st_birthtime_ns
             attrs['birthtime'] = safe_ns(int(st.st_birthtime * 10**9))
-        if self.numeric_ids:
-            attrs['user'] = attrs['group'] = None
-        else:
-            attrs['user'] = uid2user(st.st_uid)
-            attrs['group'] = gid2group(st.st_gid)
+        if not self.numeric_ids:
+            user = uid2user(st.st_uid)
+            if user is not None:
+                attrs['user'] = user
+            group = gid2group(st.st_gid)
+            if group is not None:
+                attrs['group'] = group
         return attrs
 
     def stat_ext_attrs(self, st, path, fd=None):
@@ -1426,8 +1428,11 @@ class TarfileObjectProcessors:
                 return safe_ns(int(float(s) * 1e9))
 
             item = Item(path=make_path_safe(tarinfo.name), mode=tarinfo.mode | type,
-                        uid=tarinfo.uid, gid=tarinfo.gid, user=tarinfo.uname or None, group=tarinfo.gname or None,
-                        mtime=s_to_ns(tarinfo.mtime))
+                        uid=tarinfo.uid, gid=tarinfo.gid, mtime=s_to_ns(tarinfo.mtime))
+            if tarinfo.uname:
+                item.user = tarinfo.uname
+            if tarinfo.gname:
+                item.group = tarinfo.gname
             if ph:
                 # note: for mtime this is a bit redundant as it is already done by tarfile module,
                 #       but we just do it in our way to be consistent for sure.

+ 2 - 2
src/borg/archiver.py

@@ -1355,8 +1355,8 @@ class Archiver:
             tarinfo.mode = stat.S_IMODE(item.mode)
             tarinfo.uid = item.uid
             tarinfo.gid = item.gid
-            tarinfo.uname = item.user or ''
-            tarinfo.gname = item.group or ''
+            tarinfo.uname = item.get('user', '')
+            tarinfo.gname = item.get('group',  '')
             # The linkname in tar has 2 uses:
             # for symlinks it means the destination, while for hardlinks it refers to the file.
             # Since hardlinks in tar have a different type code (LNKTYPE) the format might

+ 2 - 2
src/borg/helpers/parseformat.py

@@ -808,8 +808,8 @@ class ItemFormatter(BaseFormatter):
         hlid = bin_to_hex(hlid) if hlid else ''
         item_data['type'] = item_type
         item_data['mode'] = mode
-        item_data['user'] = item.user or item.uid
-        item_data['group'] = item.group or item.gid
+        item_data['user'] = item.get('user', str(item.uid))
+        item_data['group'] = item.get('group', str(item.gid))
         item_data['uid'] = item.uid
         item_data['gid'] = item.gid
         item_data['path'] = remove_surrogates(item.path)

+ 4 - 4
src/borg/item.pyx

@@ -249,8 +249,8 @@ class Item(PropDict):
 
     path = PropDict._make_property('path', str, 'surrogate-escaped str')
     source = PropDict._make_property('source', str, 'surrogate-escaped str')
-    user = PropDict._make_property('user', (str, type(None)), 'surrogate-escaped str or None')
-    group = PropDict._make_property('group', (str, type(None)), 'surrogate-escaped str or None')
+    user = PropDict._make_property('user', str, 'surrogate-escaped str')
+    group = PropDict._make_property('group', str, 'surrogate-escaped str')
 
     acl_access = PropDict._make_property('acl_access', bytes)
     acl_default = PropDict._make_property('acl_default', bytes)
@@ -274,8 +274,8 @@ class Item(PropDict):
     hlid = PropDict._make_property('hlid', bytes)  # hard link id: same value means same hard link.
     hardlink_master = PropDict._make_property('hardlink_master', bool)  # legacy
 
-    chunks = PropDict._make_property('chunks', (list, type(None)), 'list or None')
-    chunks_healthy = PropDict._make_property('chunks_healthy', (list, type(None)), 'list or None')
+    chunks = PropDict._make_property('chunks', list, 'list')
+    chunks_healthy = PropDict._make_property('chunks_healthy', list, 'list')
 
     xattrs = PropDict._make_property('xattrs', StableDict)
 

+ 0 - 8
src/borg/testsuite/item.py

@@ -89,14 +89,6 @@ def test_item_mptimestamp_property():
     assert item.as_dict() == {'atime': Timestamp.from_unix_nano(big)}
 
 
-def test_item_user_group_none():
-    item = Item()
-    item.user = None
-    assert item.user is None
-    item.group = None
-    assert item.group is None
-
-
 def test_item_se_str_property():
     # start simple
     item = Item()