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)
| 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. |