MCPcopy Index your code
hub / github.com/python/cpython / chown

Method chown

Lib/tarfile.py:2784–2816  ·  view source on GitHub ↗

Set owner of targetpath according to tarinfo. If numeric_owner is True, use .gid/.uid instead of .gname/.uname. If numeric_owner is False, fall back to .gid/.uid when the search based on name fails.

(self, tarinfo, targetpath, numeric_owner)

Source from the content-addressed store, hash-verified

2782 extraction_root=extraction_root)
2783
2784 def chown(self, tarinfo, targetpath, numeric_owner):
2785 """Set owner of targetpath according to tarinfo. If numeric_owner
2786 is True, use .gid/.uid instead of .gname/.uname. If numeric_owner
2787 is False, fall back to .gid/.uid when the search based on name
2788 fails.
2789 """
2790 if hasattr(os, "geteuid") and os.geteuid() == 0:
2791 # We have to be root to do so.
2792 g = tarinfo.gid
2793 u = tarinfo.uid
2794 if not numeric_owner:
2795 try:
2796 if grp and tarinfo.gname:
2797 g = grp.getgrnam(tarinfo.gname)[2]
2798 except KeyError:
2799 pass
2800 try:
2801 if pwd and tarinfo.uname:
2802 u = pwd.getpwnam(tarinfo.uname)[2]
2803 except KeyError:
2804 pass
2805 if g is None:
2806 g = -1
2807 if u is None:
2808 u = -1
2809 try:
2810 if tarinfo.issym() and hasattr(os, "lchown"):
2811 os.lchown(targetpath, u, g)
2812 else:
2813 os.chown(targetpath, u, g)
2814 except (OSError, OverflowError) as e:
2815 # OverflowError can be raised if an ID doesn't fit in 'id_t'
2816 raise ExtractError("could not change owner") from e
2817
2818 def chmod(self, tarinfo, targetpath):
2819 """Set file permissions of targetpath according to tarinfo.

Callers 15

extractallMethod · 0.95
_extract_memberMethod · 0.95
chownFunction · 0.80
flushMethod · 0.80
test_chownMethod · 0.80
test_chown_gidMethod · 0.80
test_chown_with_rootMethod · 0.80

Calls 2

ExtractErrorClass · 0.85
issymMethod · 0.80

Tested by 10

test_chownMethod · 0.64
test_chown_gidMethod · 0.64
test_chown_with_rootMethod · 0.64
test_chown_dir_fdMethod · 0.64
test_chownMethod · 0.64