Skip to content

Commit a663e61

Browse files
qkaisere3krisztian
andcommitted
feat(file_utils): add unlink function to FileSystem API.
Co-authored-by: Krisztián Fekete <1246751+e3krisztian@users.noreply.github.com>
1 parent 6cf2950 commit a663e61

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

tests/test_file_utils.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,30 @@ def test_open_outside_sandbox(self, sandbox: FileSystem):
609609
real_out_path = ".unblob-lost+found/_e90583b491d2138aab0c8a12478ee050701910fd80c84289ae747e7c/file"
610610
assert (sandbox.root / real_out_path).read_bytes() == b"content"
611611

612+
@pytest.mark.parametrize("path", [Path("ok-path"), Path("../outside-path")])
613+
def test_unlink(self, path: Path, sandbox: FileSystem):
614+
with sandbox.open(path) as f:
615+
f.write(b"content")
616+
sandbox.unlink(path)
617+
assert not (sandbox.root / path).exists()
618+
619+
def test_unlink_no_path_traversal(self, sandbox: FileSystem):
620+
path = Path("file")
621+
with sandbox.open(path) as f:
622+
f.write(b"content")
623+
624+
sandbox.unlink(path)
625+
assert not (sandbox.root / path).exists()
626+
assert sandbox.problems == []
627+
628+
def test_unlink_outside_sandbox(self, sandbox: FileSystem):
629+
path = Path("../file")
630+
(sandbox.root / path).touch()
631+
sandbox.unlink(path)
632+
633+
assert (sandbox.root / path).exists()
634+
assert sandbox.problems
635+
612636

613637
@pytest.mark.parametrize(
614638
"input_path, expected_path",

unblob/file_utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,3 +618,10 @@ def open( # noqa: A003
618618

619619
self._ensure_parent_dir(safe_path)
620620
return safe_path.open(mode)
621+
622+
def unlink(self, path):
623+
"""Delete file within extraction path."""
624+
logger.debug("unlink file", file_path=path)
625+
safe_path = self._get_extraction_path(path, "unlink")
626+
627+
safe_path.unlink(missing_ok=True)

0 commit comments

Comments
 (0)