Fix for nonutf8 filenames causing crashes when setting permissions

Fixes #23861

(cherry picked from commit b26ee657b5)
pull/27279/head
Toshio Kuratomi 9 years ago
parent 795e75433e
commit 28fa228d93

@ -992,19 +992,19 @@ class AnsibleModule(object):
b_path = to_bytes(path, errors='surrogate_or_strict')
if expand:
b_path = os.path.expanduser(os.path.expandvars(b_path))
path = to_text(b_path, errors='surrogate_or_strict')
if owner is None:
return changed
orig_uid, orig_gid = self.user_and_group(path, expand)
orig_uid, orig_gid = self.user_and_group(b_path, expand)
try:
uid = int(owner)
except ValueError:
try:
uid = pwd.getpwnam(owner).pw_uid
except KeyError:
path = to_text(b_path)
self.fail_json(path=path, msg='chown failed: failed to look up user %s' % owner)
if orig_uid != uid:
if orig_uid != uid:
if diff is not None:
if 'before' not in diff:
diff['before'] = {}
@ -1018,6 +1018,7 @@ class AnsibleModule(object):
try:
os.lchown(b_path, uid, -1)
except OSError:
path = to_text(b_path)
self.fail_json(path=path, msg='chown failed')
changed = True
return changed
@ -1026,7 +1027,6 @@ class AnsibleModule(object):
b_path = to_bytes(path, errors='surrogate_or_strict')
if expand:
b_path = os.path.expanduser(os.path.expandvars(b_path))
path = to_text(b_path, errors='surrogate_or_strict')
if group is None:
return changed
orig_uid, orig_gid = self.user_and_group(b_path, expand)
@ -1036,9 +1036,10 @@ class AnsibleModule(object):
try:
gid = grp.getgrnam(group).gr_gid
except KeyError:
path = to_text(b_path)
self.fail_json(path=path, msg='chgrp failed: failed to look up group %s' % group)
if orig_gid != gid:
if orig_gid != gid:
if diff is not None:
if 'before' not in diff:
diff['before'] = {}
@ -1052,6 +1053,7 @@ class AnsibleModule(object):
try:
os.lchown(b_path, -1, gid)
except OSError:
path = to_text(b_path)
self.fail_json(path=path, msg='chgrp failed')
changed = True
return changed
@ -1060,7 +1062,6 @@ class AnsibleModule(object):
b_path = to_bytes(path, errors='surrogate_or_strict')
if expand:
b_path = os.path.expanduser(os.path.expandvars(b_path))
path = to_text(b_path, errors='surrogate_or_strict')
path_stat = os.lstat(b_path)
if mode is None:
@ -1074,12 +1075,14 @@ class AnsibleModule(object):
mode = self._symbolic_mode_to_octal(path_stat, mode)
except Exception:
e = get_exception()
path = to_text(b_path)
self.fail_json(path=path,
msg="mode must be in octal or symbolic form",
details=str(e))
if mode != stat.S_IMODE(mode):
# prevent mode from having extra info orbeing invalid long number
path = to_text(b_path)
self.fail_json(path=path, msg="Invalid mode supplied, only permission info is allowed", details=mode)
prev_mode = stat.S_IMODE(path_stat.st_mode)
@ -1123,6 +1126,7 @@ class AnsibleModule(object):
raise e
except Exception:
e = get_exception()
path = to_text(b_path)
self.fail_json(path=path, msg='chmod failed', details=str(e))
path_stat = os.lstat(b_path)
@ -1140,7 +1144,6 @@ class AnsibleModule(object):
b_path = to_bytes(path, errors='surrogate_or_strict')
if expand:
b_path = os.path.expanduser(os.path.expandvars(b_path))
path = to_text(b_path, errors='surrogate_or_strict')
existing = self.get_file_attributes(b_path)
@ -1165,6 +1168,7 @@ class AnsibleModule(object):
raise Exception("Error while setting attributes: %s" % (out + err))
except:
e = get_exception()
path = to_text(b_path)
self.fail_json(path=path, msg='chattr failed', details=str(e))
return changed

Loading…
Cancel
Save