Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix file header detection in open_decompress #998

Merged
merged 2 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions dissect/target/helpers/fsutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -527,6 +529,7 @@ def open_decompress(
file = path.open("rb")
else:
file = fileobj
file.seek(0)

magic = file.read(5)
file.seek(0)
Expand Down
5 changes: 4 additions & 1 deletion tests/helpers/test_fsutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading