From 27ef603acc64849f084d95dc97891b489c13a097 Mon Sep 17 00:00:00 2001 From: fuleinist Date: Fri, 17 Apr 2026 08:06:11 +0800 Subject: [PATCH] Fix lsattr parser for filenames with spaces Use maxsplit=1 with line.split() to properly handle filenames containing spaces or newlines. Previously split() without maxsplit would incorrectly split './ok ok ok ok ok' into 6 parts instead of 2. Fixes: https://github.com/kellyjonbrazil/jc/issues/694 --- jc/parsers/lsattr.py | 6 +++++- tests/fixtures/ubuntu-20.04/lsattr-spaces.json | 1 + tests/fixtures/ubuntu-20.04/lsattr-spaces.out | 1 + tests/test_lsattr.py | 15 ++++++++++++++- 4 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 tests/fixtures/ubuntu-20.04/lsattr-spaces.json create mode 100644 tests/fixtures/ubuntu-20.04/lsattr-spaces.out diff --git a/jc/parsers/lsattr.py b/jc/parsers/lsattr.py index dc3a3794..cb2609e2 100644 --- a/jc/parsers/lsattr.py +++ b/jc/parsers/lsattr.py @@ -149,7 +149,11 @@ def parse( # attributes file # --------------e----- /etc/passwd - attributes, file = line.split() + # Use maxsplit=1 to handle filenames with spaces (e.g. "./ok ok ok ok ok") + parts = line.split(maxsplit=1) + if len(parts) != 2: + continue + attributes, file = parts line_output['file'] = file for attribute in list(attributes): attribute_key = ATTRIBUTES.get(attribute) diff --git a/tests/fixtures/ubuntu-20.04/lsattr-spaces.json b/tests/fixtures/ubuntu-20.04/lsattr-spaces.json new file mode 100644 index 00000000..c4074e45 --- /dev/null +++ b/tests/fixtures/ubuntu-20.04/lsattr-spaces.json @@ -0,0 +1 @@ +[{"file": "./ok ok ok ok ok", "extents": true}] diff --git a/tests/fixtures/ubuntu-20.04/lsattr-spaces.out b/tests/fixtures/ubuntu-20.04/lsattr-spaces.out new file mode 100644 index 00000000..2dae7e18 --- /dev/null +++ b/tests/fixtures/ubuntu-20.04/lsattr-spaces.out @@ -0,0 +1 @@ +--------------e----- ./ok ok ok ok ok diff --git a/tests/test_lsattr.py b/tests/test_lsattr.py index 8e74193b..98dc996b 100644 --- a/tests/test_lsattr.py +++ b/tests/test_lsattr.py @@ -18,6 +18,9 @@ class MyTests(unittest.TestCase): with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-20.04/lsattr.out'), 'r', encoding='utf-8') as f: ubuntu_20_4_lsattr = f.read() + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-20.04/lsattr-spaces.out'), 'r', encoding='utf-8') as f: + ubuntu_20_4_lsattr_spaces = f.read() + # output with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-20.04/lsattr-error.json'), 'r', encoding='utf-8') as f: ubuntu_20_4_lsattr_error_json = json.loads(f.read()) @@ -28,6 +31,9 @@ class MyTests(unittest.TestCase): with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-20.04/lsattr.json'), 'r', encoding='utf-8') as f: ubuntu_20_4_lsattr_json = json.loads(f.read()) + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-20.04/lsattr-spaces.json'), 'r', encoding='utf-8') as f: + ubuntu_20_4_lsattr_spaces_json = json.loads(f.read()) + def test_lsattr_nodata(self): """ Test 'lsattr' with no data @@ -52,5 +58,12 @@ class MyTests(unittest.TestCase): """ self.assertEqual(jc.parsers.lsattr.parse(self.ubuntu_20_4_lsattr, quiet=True), self.ubuntu_20_4_lsattr_json) + def test_lsattr_spaces(self): + """ + Test 'lsattr' with filenames containing spaces + """ + self.assertEqual(jc.parsers.lsattr.parse(self.ubuntu_20_4_lsattr_spaces, quiet=True), self.ubuntu_20_4_lsattr_spaces_json) + + if __name__ == '__main__': - unittest.main() + unittest.main() \ No newline at end of file