stat: Handle colon in filename (#70259)

Handle colon appearing in filename while parsing the mimetype and charset
using file command.

Fixes: #70256

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
Abhijeet Kasurde 2020-06-26 00:51:50 +05:30 committed by GitHub
parent 3c9be97e16
commit 29169ae847
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 2 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- stat - handle colons in filename while parsing the mimetype output (https://github.com/ansible/ansible/issues/70256).

View file

@ -513,11 +513,11 @@ def main():
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', b_path] mimecmd = [mimecmd, '--mime-type', '--mime-encoding', b_path]
try: try:
rc, out, err = module.run_command(mimecmd) rc, out, err = module.run_command(mimecmd)
if rc == 0: if rc == 0:
mimetype, charset = out.split(':')[1].split(';') mimetype, charset = out.rsplit(':', 1)[1].split(';')
output['mimetype'] = mimetype.strip() output['mimetype'] = mimetype.strip()
output['charset'] = charset.split('=')[1].strip() output['charset'] = charset.split('=')[1].strip()
except Exception: except Exception:

View file

@ -155,3 +155,25 @@
- "'xgrp' in stat_result.stat" - "'xgrp' in stat_result.stat"
- "'xoth' in stat_result.stat" - "'xoth' in stat_result.stat"
- "'xusr' in stat_result.stat" - "'xusr' in stat_result.stat"
- name: make a new file with colon in filename
copy:
dest: "{{ output_dir }}/foo:bar.txt"
mode: '0644'
content: "hello world"
- name: check stat of a file with colon in name
stat:
path: "{{ output_dir }}/foo:bar.txt"
follow: True
register: stat_result
- debug:
var: stat_result
- assert:
that:
- "'changed' in stat_result"
- "stat_result.changed == false"
- "stat_result.stat.mimetype == 'text/plain'"
- "stat_result.stat.charset == 'us-ascii'"