Skip to content

Commit

Permalink
container: Do not leave stale temporary dirs
Browse files Browse the repository at this point in the history
Do not leave stale temporary directories when conversion fails
unexpectedly. Instead, wrap the conversion operation in a context
manager that wipes the temporary dir afterwards.

Fixes #317
  • Loading branch information
apyrgio committed Feb 8, 2023
1 parent 285fa9e commit ebef341
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
44 changes: 32 additions & 12 deletions dangerzone/isolation_provider/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
import logging
import os
import pathlib
import pipes
import platform
import shutil
Expand Down Expand Up @@ -217,6 +218,37 @@ def _convert(
document: Document,
ocr_lang: Optional[str],
stdout_callback: Optional[Callable] = None,
) -> bool:
# This path needs to be permanent, and may already exist from previous runs.
os.makedirs(self.cache_dir, exist_ok=True)

# Create a temporary directory inside the cache directory for this run. Then,
# create some subdirectories for the various stages of the file conversion:
#
# * pixel: Where the RGB data will be stored
# * safe: Where the final PDF file will be stored
with tempfile.TemporaryDirectory(dir=self.cache_dir) as tmp_dir:
tmp_dir = pathlib.Path(tmp_dir)
pixel_dir = tmp_dir / "pixels"
pixel_dir.mkdir()
safe_dir = tmp_dir / "safe"
safe_dir.mkdir()

return self._convert_with_tmpdirs(
document=document,
pixel_dir=pixel_dir,
safe_dir=safe_dir,
ocr_lang=ocr_lang,
stdout_callback=stdout_callback,
)

def _convert_with_tmpdirs(
self,
document: Document,
pixel_dir: pathlib.Path,
safe_dir: pathlib.Path,
ocr_lang: Optional[str],
stdout_callback: Optional[Callable] = None,
) -> bool:
success = False

Expand All @@ -225,15 +257,6 @@ def _convert(
else:
ocr = "0"

dz_tmp = os.path.join(self.cache_dir, "tmp")
os.makedirs(dz_tmp, exist_ok=True)

tmpdir = tempfile.TemporaryDirectory(dir=dz_tmp)
pixel_dir = os.path.join(tmpdir.name, "pixels")
safe_dir = os.path.join(tmpdir.name, "safe")
os.makedirs(pixel_dir, exist_ok=True)
os.makedirs(safe_dir, exist_ok=True)

# Convert document to pixels
command = [
"/usr/bin/python3",
Expand Down Expand Up @@ -284,9 +307,6 @@ def _convert(
# We did it
success = True

# Clean up
tmpdir.cleanup()

return success

def get_max_parallel_conversions(self) -> int:
Expand Down
4 changes: 4 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ def run_cli(
if tmp_path is not None:
os.chdir(cwd)

if cache_dir.exists():
stale_files = list(cache_dir.iterdir())
assert not stale_files

return CLIResult.reclass_click_result(result, args)


Expand Down

0 comments on commit ebef341

Please sign in to comment.