1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Fix dead code in files module (#43479)

* Use pwd.getpwnam instead of non-existent pwd.getpwname

* Add specific exceptions to pwd and grp calls
This commit is contained in:
joren485 2018-09-04 16:06:57 +02:00 committed by Matt Martz
parent cf1006179d
commit cd5d484f7a
2 changed files with 11 additions and 11 deletions

View file

@ -496,13 +496,13 @@ def main():
try: # user data try: # user data
pw = pwd.getpwuid(st.st_uid) pw = pwd.getpwuid(st.st_uid)
output['pw_name'] = pw.pw_name output['pw_name'] = pw.pw_name
except: except (TypeError, KeyError):
pass pass
try: # group data try: # group data
grp_info = grp.getgrgid(st.st_gid) grp_info = grp.getgrgid(st.st_gid)
output['gr_name'] = grp_info.gr_name output['gr_name'] = grp_info.gr_name
except: except (KeyError, ValueError, OverflowError):
pass pass
# checksums # checksums

View file

@ -303,22 +303,22 @@ class ZipArchive(object):
run_gid = os.getgid() run_gid = os.getgid()
try: try:
run_owner = pwd.getpwuid(run_uid).pw_name run_owner = pwd.getpwuid(run_uid).pw_name
except: except (TypeError, KeyError):
run_owner = run_uid run_owner = run_uid
try: try:
run_group = grp.getgrgid(run_gid).gr_name run_group = grp.getgrgid(run_gid).gr_name
except: except (KeyError, ValueError, OverflowError):
run_group = run_gid run_group = run_gid
# Get future user ownership # Get future user ownership
fut_owner = fut_uid = None fut_owner = fut_uid = None
if self.file_args['owner']: if self.file_args['owner']:
try: try:
tpw = pwd.getpwname(self.file_args['owner']) tpw = pwd.getpwnam(self.file_args['owner'])
except: except KeyError:
try: try:
tpw = pwd.getpwuid(self.file_args['owner']) tpw = pwd.getpwuid(self.file_args['owner'])
except: except (TypeError, KeyError):
tpw = pwd.getpwuid(run_uid) tpw = pwd.getpwuid(run_uid)
fut_owner = tpw.pw_name fut_owner = tpw.pw_name
fut_uid = tpw.pw_uid fut_uid = tpw.pw_uid
@ -334,10 +334,10 @@ class ZipArchive(object):
if self.file_args['group']: if self.file_args['group']:
try: try:
tgr = grp.getgrnam(self.file_args['group']) tgr = grp.getgrnam(self.file_args['group'])
except: except (ValueError, KeyError):
try: try:
tgr = grp.getgrgid(self.file_args['group']) tgr = grp.getgrgid(self.file_args['group'])
except: except (KeyError, ValueError, OverflowError):
tgr = grp.getgrgid(run_gid) tgr = grp.getgrgid(run_gid)
fut_group = tgr.gr_name fut_group = tgr.gr_name
fut_gid = tgr.gr_gid fut_gid = tgr.gr_gid
@ -528,7 +528,7 @@ class ZipArchive(object):
owner = uid = None owner = uid = None
try: try:
owner = pwd.getpwuid(st.st_uid).pw_name owner = pwd.getpwuid(st.st_uid).pw_name
except: except (TypeError, KeyError):
uid = st.st_uid uid = st.st_uid
# If we are not root and requested owner is not our user, fail # If we are not root and requested owner is not our user, fail
@ -548,7 +548,7 @@ class ZipArchive(object):
group = gid = None group = gid = None
try: try:
group = grp.getgrgid(st.st_gid).gr_name group = grp.getgrgid(st.st_gid).gr_name
except: except (KeyError, ValueError, OverflowError):
gid = st.st_gid gid = st.st_gid
if run_uid != 0 and fut_gid not in groups: if run_uid != 0 and fut_gid not in groups: