Fix places where path needs to be bytes on python3

These were discovered on python3 with fetch code that fails on errors.  Probably could be
provoked with particular sets of arguments to stat as well.
This commit is contained in:
Toshio Kuratomi 2017-04-17 15:05:10 -07:00
parent ad8cb903f4
commit 36d7c0c403
3 changed files with 22 additions and 12 deletions

View file

@ -915,10 +915,18 @@ class AnsibleModule(object):
return (uid, gid) return (uid, gid)
def find_mount_point(self, path): def find_mount_point(self, path):
path = os.path.realpath(os.path.expanduser(os.path.expandvars(path))) path_is_bytes = False
while not os.path.ismount(path): if isinstance(path, binary_type):
path = os.path.dirname(path) path_is_bytes = True
return path
b_path = os.path.realpath(to_bytes(os.path.expanduser(os.path.expandvars(path)), errors='surrogate_or_strict'))
while not os.path.ismount(b_path):
b_path = os.path.dirname(b_path)
if path_is_bytes:
return b_path
return to_text(b_path, errors='surrogate_or_strict')
def is_special_selinux_path(self, path): def is_special_selinux_path(self, path):
""" """

View file

@ -477,11 +477,11 @@ def main():
# resolved permissions # resolved permissions
for perm in [('readable', os.R_OK), ('writeable', os.W_OK), ('executable', os.X_OK)]: for perm in [('readable', os.R_OK), ('writeable', os.W_OK), ('executable', os.X_OK)]:
output[perm[0]] = os.access(path, perm[1]) output[perm[0]] = os.access(b_path, perm[1])
# symlink info # symlink info
if output.get('islnk'): if output.get('islnk'):
output['lnk_source'] = os.path.realpath(path) output['lnk_source'] = os.path.realpath(b_path)
try: # user data try: # user data
pw = pwd.getpwuid(st.st_uid) pw = pwd.getpwuid(st.st_uid)
@ -500,19 +500,19 @@ def main():
if get_md5: if get_md5:
# Will fail on FIPS-140 compliant systems # Will fail on FIPS-140 compliant systems
try: try:
output['md5'] = module.md5(path) output['md5'] = module.md5(b_path)
except ValueError: except ValueError:
output['md5'] = None output['md5'] = None
if get_checksum: if get_checksum:
output['checksum'] = module.digest_from_file(path, checksum_algorithm) output['checksum'] = module.digest_from_file(b_path, checksum_algorithm)
# try to get mime data if requested # try to get mime data if requested
if get_mime: if get_mime:
output['mimetype'] = output['charset'] = 'unknown' output['mimetype'] = output['charset'] = 'unknown'
mimecmd = module.get_bin_path('file') mimecmd = module.get_bin_path('file')
if mimecmd: if mimecmd:
mimecmd = [mimecmd, '-i', path] mimecmd = [mimecmd, '-i', b_path]
try: try:
rc, out, err = module.run_command(mimecmd) rc, out, err = module.run_command(mimecmd)
if rc == 0: if rc == 0:
@ -527,7 +527,7 @@ def main():
output['version'] = None output['version'] = None
output['attributes'] = [] output['attributes'] = []
output['attr_flags'] = '' output['attr_flags'] = ''
out = module.get_file_attributes(path) out = module.get_file_attributes(b_path)
for x in ('version', 'attributes', 'attr_flags'): for x in ('version', 'attributes', 'attr_flags'):
if x in out: if x in out:
output[x] = out[x] output[x] = out[x]

View file

@ -647,7 +647,7 @@ class TestModuleUtilsBasic(ModuleTestCase):
) )
def _mock_ismount(path): def _mock_ismount(path):
if path == '/': if path == b'/':
return True return True
return False return False
@ -655,7 +655,9 @@ class TestModuleUtilsBasic(ModuleTestCase):
self.assertEqual(am.find_mount_point('/root/fs/../mounted/path/to/whatever'), '/') self.assertEqual(am.find_mount_point('/root/fs/../mounted/path/to/whatever'), '/')
def _mock_ismount(path): def _mock_ismount(path):
if path == '/subdir/mount': if path == b'/subdir/mount':
return True
if path == b'/':
return True return True
return False return False