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

add unlink function to FileSystem API. #681

Merged
merged 1 commit into from
Nov 17, 2023
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
24 changes: 24 additions & 0 deletions tests/test_file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,30 @@ def test_open_outside_sandbox(self, sandbox: FileSystem):
real_out_path = ".unblob-lost+found/_e90583b491d2138aab0c8a12478ee050701910fd80c84289ae747e7c/file"
assert (sandbox.root / real_out_path).read_bytes() == b"content"

@pytest.mark.parametrize("path", [Path("ok-path"), Path("../outside-path")])
def test_unlink(self, path: Path, sandbox: FileSystem):
with sandbox.open(path) as f:
f.write(b"content")
sandbox.unlink(path)
assert not (sandbox.root / path).exists()

def test_unlink_no_path_traversal(self, sandbox: FileSystem):
path = Path("file")
with sandbox.open(path) as f:
f.write(b"content")

sandbox.unlink(path)
assert not (sandbox.root / path).exists()
assert sandbox.problems == []

def test_unlink_outside_sandbox(self, sandbox: FileSystem):
path = Path("../file")
(sandbox.root / path).touch()
sandbox.unlink(path)

assert (sandbox.root / path).exists()
assert sandbox.problems


@pytest.mark.parametrize(
"input_path, expected_path",
Expand Down
7 changes: 7 additions & 0 deletions unblob/file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,3 +618,10 @@ def open( # noqa: A003

self._ensure_parent_dir(safe_path)
return safe_path.open(mode)

def unlink(self, path):
"""Delete file within extraction path."""
logger.debug("unlink file", file_path=path)
safe_path = self._get_extraction_path(path, "unlink")

safe_path.unlink(missing_ok=True)
Loading