From 427e0ef3b8a9f5d73fcf03dab9327f61980c6935 Mon Sep 17 00:00:00 2001 From: Yun Zheng Hu Date: Tue, 21 Jan 2025 17:37:55 +0000 Subject: [PATCH 1/2] Fix file header detection in open_decompress Previously file header could be misread if fileobj position was not at start, leading to incorrect file format detection. Now explicitly seek to beginning before reading the magic file header bytes. --- dissect/target/helpers/fsutil.py | 1 + tests/helpers/test_fsutil.py | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/dissect/target/helpers/fsutil.py b/dissect/target/helpers/fsutil.py index 1b1eec9b4..e2b1499d5 100644 --- a/dissect/target/helpers/fsutil.py +++ b/dissect/target/helpers/fsutil.py @@ -527,6 +527,7 @@ def open_decompress( file = path.open("rb") else: file = fileobj + file.seek(0) magic = file.read(5) file.seek(0) diff --git a/tests/helpers/test_fsutil.py b/tests/helpers/test_fsutil.py index 7a88886a2..d5fa89bdf 100644 --- a/tests/helpers/test_fsutil.py +++ b/tests/helpers/test_fsutil.py @@ -646,8 +646,11 @@ def test_pure_dissect_path__from_parts_no_fs_exception() -> None: ) def test_open_decompress(file_name: str, compressor: Callable, content: bytes) -> None: vfs = VirtualFilesystem() - vfs.map_file_fh(file_name, io.BytesIO(compressor(content))) + fh = io.BytesIO(compressor(content)) + vfs.map_file_fh(file_name, fh) assert fsutil.open_decompress(vfs.path(file_name)).read() == content + fh.seek(2) + assert fsutil.open_decompress(fileobj=fh).read() == content def test_open_decompress_text_modes() -> None: From efb3945ee780e812c91cb4ab294f609f22c942e3 Mon Sep 17 00:00:00 2001 From: Yun Zheng Hu Date: Tue, 21 Jan 2025 17:47:03 +0000 Subject: [PATCH 2/2] Fix example docstring to be rendered as code blocck --- dissect/target/helpers/fsutil.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/dissect/target/helpers/fsutil.py b/dissect/target/helpers/fsutil.py index e2b1499d5..f79155b12 100644 --- a/dissect/target/helpers/fsutil.py +++ b/dissect/target/helpers/fsutil.py @@ -512,10 +512,12 @@ def open_decompress( An binary or text IO stream, depending on the mode with which the file was opened. Example: - bytes_buf = open_decompress(Path("/dir/file.gz")).read() + .. code-block:: python - for line in open_decompress(Path("/dir/file.gz"), "rt"): - print(line) + bytes_buf = open_decompress(Path("/dir/file.gz")).read() + + for line in open_decompress(Path("/dir/file.gz"), "rt"): + print(line) """ if path and fileobj: raise ValueError("path and fileobj are mutually exclusive")