diff options
author | Tim Burke <tim.burke@gmail.com> | 2021-05-27 21:41:58 -0700 |
---|---|---|
committer | Tim Burke <tim.burke@gmail.com> | 2021-05-27 21:52:28 -0700 |
commit | 6cad60fd562ce3716d8cfb18bbdf3bf46639c348 (patch) | |
tree | 9bbf0c4814276aeb82d092d33c1dd98d940db54f | |
parent | 8066019b71da7c9240404e8d80b03175a469dbf0 (diff) | |
download | xattr-6cad60fd562ce3716d8cfb18bbdf3bf46639c348.tar.gz |
tool: Dump non-utf8 data similar to what's done for NULs
Addresses #90.
Side-effect: dump non-ascii data as the bytes instead of the unicode
code point. This seems to better match user expectations when presented
with the dump formatting, anyway.
-rwxr-xr-x | xattr/tool.py | 26 |
1 files changed, 19 insertions, 7 deletions
diff --git a/xattr/tool.py b/xattr/tool.py index 89b9221..5bc4000 100755 --- a/xattr/tool.py +++ b/xattr/tool.py @@ -195,27 +195,39 @@ def main(): file_prefix = "" for attr_name in attr_names: + should_dump = False try: try: attr_value = decompress(attrs[attr_name]) except zlib.error: attr_value = attrs[attr_name] - attr_value = attr_value.decode('utf-8') + try: + if b'\0' in attr_value: + # force dumping + raise NullsInString + attr_value = attr_value.decode('utf-8') + except (UnicodeDecodeError, NullsInString): + attr_value = attr_value.decode('latin-1') + should_dump = True except KeyError: onError("%sNo such xattr: %s" % (file_prefix, attr_name)) continue if long_format: - try: - if '\0' in attr_value: - raise NullsInString - print("".join((file_prefix, "%s: " % (attr_name,), attr_value))) - except (UnicodeDecodeError, NullsInString): + if should_dump: print("".join((file_prefix, "%s:" % (attr_name,)))) print(_dump(attr_value)) + else: + print("".join((file_prefix, "%s: " % (attr_name,), attr_value))) else: if read: - print("".join((file_prefix, attr_value))) + if should_dump: + print(file_prefix, end="") + sys.stdout.flush() + with os.fdopen(sys.stdout.fileno(), 'wb', closefd=False) as fp: + fp.write(attr_value.encode('latin-1') + b'\n') + else: + print("".join((file_prefix, attr_value))) else: print("".join((file_prefix, attr_name))) |