Mini Shell

Direktori : /proc/self/root/proc/self/root/usr/lib/fixperms/
Upload File :
Current File : //proc/self/root/proc/self/root/usr/lib/fixperms/fixperms_ids.py

"""Handles caching user and group database info in memory"""
import grp
import pwd


class IDCache:
    """Handles caching user and group database info in memory"""

    # these lookups are fast, but when looking it up once per file chowned
    # to form verbose output, it adds up

    def __init__(self, role: str):
        self._grps = {}
        self._pwds = {}
        self._uid2name = {0: 'root'}
        self._gid2name = {0: 'root'}
        if role != 'WP3':
            self.getgrnam('nobody')
            self.getgrnam('mail')

    def getpwnam(self, name: str) -> pwd.struct_passwd:
        """Lookup and cache a struct_passwd by name

        Args:
            name (str): user name

        Raises:
            KeyError: user not found in /etc/passwd

        Returns:
            pwd.struct_passwd: pwd.getpwnam() result
        """
        try:
            return self._pwds[name]
        except KeyError:
            pass
        try:
            val = pwd.getpwnam(name)
        except KeyError as exc:
            raise KeyError(f"The {name!r} user is missing") from exc
        self._pwds[name] = val
        self._uid2name[val.pw_uid] = name
        return val

    def getgrnam(self, name: str) -> grp.struct_group:
        """Lookup and cache a struct_group by name

        Args:
            name (str): group name

        Raises:
            KeyError: group not found in /etc/groups

        Returns:
            grp.struct_group: grp.getgrnam() result
        """
        try:
            return self._grps[name]
        except KeyError:
            pass
        try:
            val = grp.getgrnam(name)
        except KeyError as exc:
            raise KeyError(f"The {name!r} group is missing") from exc
        self._grps[name] = val
        self._gid2name[val.gr_gid] = name
        return val

    def uid_label(self, uid: int) -> str:
        """Lookup and cache a user name by UID

        Args:
            uid (int): user ID

        Returns:
            str: user name to display, or string of the uid if it was missing
        """
        assert uid != -1
        try:
            return self._uid2name[uid]
        except KeyError:
            pass
        try:
            val = pwd.getpwuid(uid)
        except KeyError:
            self._uid2name[uid] = str(uid)
            return self._uid2name[uid]
        self._uid2name[uid] = val.pw_name
        self._pwds[val.pw_name] = val
        return self._uid2name[uid]

    def gid_label(self, gid: int) -> str:
        """Lookup and cache a group name by GID

        Args:
            gid (int): group ID

        Returns:
            str: group name to display, or string of the gid if it was missing
        """
        assert gid != -1
        try:
            return self._gid2name[gid]
        except KeyError:
            pass
        try:
            val = grp.getgrgid(gid)
        except KeyError:
            self._gid2name[gid] = str(gid)
            return self._gid2name[gid]
        self._gid2name[gid] = val.gr_name
        self._grps[val.gr_name] = val
        return self._gid2name[gid]

Zerion Mini Shell 1.0