Skip to content
Closed
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
16 changes: 14 additions & 2 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,13 @@ def test_skip_extension(
args = []
for suffix in skip_extension:
args += ["--skip-extension", suffix]
params = [*args, "--extract-dir", str(tmp_path), str(in_path)]
params = [
*args,
"--keep-extracted-chunks",
"--extract-dir",
str(tmp_path),
str(in_path),
]
result = runner.invoke(unblob.cli.cli, params)
assert extracted_files_count == len(list(tmp_path.rglob("*")))
assert result.exit_code == 0
Expand Down Expand Up @@ -409,7 +415,13 @@ def test_clear_skip_magics(
/ "__input__"
/ "apple.zip"
)
params = [*args, "--extract-dir", str(tmp_path), str(in_path)]
params = [
*args,
"--keep-extracted-chunks",
"--extract-dir",
str(tmp_path),
str(in_path),
]

process_file_mock = mock.MagicMock()
with mock.patch.object(unblob.cli, "process_file", process_file_mock):
Expand Down
10 changes: 8 additions & 2 deletions tests/test_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,9 @@ def test_process_file_prevents_double_extracts(tmp_path: Path, fw: Path):
# ├── hello
# └── world
fw_extract_root = tmp_path / "fw_extract_root"
config = ExtractionConfig(extract_root=fw_extract_root, entropy_depth=0)
config = ExtractionConfig(
extract_root=fw_extract_root, keep_extracted_chunks=True, entropy_depth=0
)
process_result = process_file(config, fw)
assert process_result.errors == []
extracted_fw_paths, outsiders = sort_paths(
Expand All @@ -331,7 +333,11 @@ def test_process_file_prevents_double_extracts(tmp_path: Path, fw: Path):
# ├── hello
# └── world
fw_extract_of_extract_root = tmp_path / "fw_extract_of_extract_root"
config = ExtractionConfig(extract_root=fw_extract_of_extract_root, entropy_depth=0)
config = ExtractionConfig(
extract_root=fw_extract_of_extract_root,
keep_extracted_chunks=True,
entropy_depth=0,
)
process_result = process_file(config, extracted_fw_zip)

# we expect exactly 1 problem reported, related to the extraction of "internal.zip"
Expand Down
14 changes: 12 additions & 2 deletions unblob/processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,8 +604,18 @@ def _extract_chunk(self, file, chunk: ValidChunk): # noqa: C901

extraction_reports = []
try:
if result := chunk.extract(inpath, extract_dir):
extraction_reports.extend(result.reports)
result = chunk.extract(inpath, extract_dir)
chunk_reports = result.reports if result else []
extraction_reports.extend(chunk_reports)
successfully_extracted = not chunk_reports

if (
successfully_extracted
and chunk.is_whole_file
and not self.config.keep_extracted_chunks
):
logger.debug("Removing successfully processed file.", path=inpath)
inpath.unlink()

if carved_path and not self.config.keep_extracted_chunks:
logger.debug("Removing extracted chunk", path=carved_path)
Expand Down