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)
| 2757 | extraction_root=extraction_root) |
| 2758 | |
| 2759 | def chown(self, tarinfo, targetpath, numeric_owner): |
| 2760 | """Set owner of targetpath according to tarinfo. If numeric_owner |
| 2761 | is True, use .gid/.uid instead of .gname/.uname. If numeric_owner |
| 2762 | is False, fall back to .gid/.uid when the search based on name |
| 2763 | fails. |
| 2764 | """ |
| 2765 | if hasattr(os, "geteuid") and os.geteuid() == 0: |
| 2766 | # We have to be root to do so. |
| 2767 | g = tarinfo.gid |
| 2768 | u = tarinfo.uid |
| 2769 | if not numeric_owner: |
| 2770 | try: |
| 2771 | if grp and tarinfo.gname: |
| 2772 | g = grp.getgrnam(tarinfo.gname)[2] |
| 2773 | except KeyError: |
| 2774 | pass |
| 2775 | try: |
| 2776 | if pwd and tarinfo.uname: |
| 2777 | u = pwd.getpwnam(tarinfo.uname)[2] |
| 2778 | except KeyError: |
| 2779 | pass |
| 2780 | if g is None: |
| 2781 | g = -1 |
| 2782 | if u is None: |
| 2783 | u = -1 |
| 2784 | try: |
| 2785 | if tarinfo.issym() and hasattr(os, "lchown"): |
| 2786 | os.lchown(targetpath, u, g) |
| 2787 | else: |
| 2788 | os.chown(targetpath, u, g) |
| 2789 | except (OSError, OverflowError) as e: |
| 2790 | # OverflowError can be raised if an ID doesn't fit in 'id_t' |
| 2791 | raise ExtractError("could not change owner") from e |
| 2792 | |
| 2793 | def chmod(self, tarinfo, targetpath): |
| 2794 | """Set file permissions of targetpath according to tarinfo. |