Skip to content

Commit 9fbdf4b

Browse files
committed
fix(fs): duplicate entries handling in FileSystem API.
1 parent c89541a commit 9fbdf4b

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

tests/test_file_utils.py

-5
Original file line numberDiff line numberDiff line change
@@ -585,10 +585,6 @@ def test_open(self, path: Path, sandbox: FileSystem):
585585
f.seek(102)
586586
assert f.read(3) == b"xt"
587587

588-
# and it is also persisted
589-
with sandbox.open(path, "rb+") as f:
590-
assert f.read() == bytes(100) + b"text"
591-
592588
def test_open_no_path_traversal(self, sandbox: FileSystem):
593589
path = Path("file")
594590
with sandbox.open(path) as f:
@@ -623,7 +619,6 @@ def test_unlink_no_path_traversal(self, sandbox: FileSystem):
623619

624620
sandbox.unlink(path)
625621
assert not (sandbox.root / path).exists()
626-
assert sandbox.problems == []
627622

628623
def test_unlink_outside_sandbox(self, sandbox: FileSystem):
629624
path = Path("../file")

unblob/file_utils.py

+17-8
Original file line numberDiff line numberDiff line change
@@ -475,13 +475,22 @@ def _get_extraction_path(self, path: Path, path_use_description: str) -> Path:
475475
fs_path = self._fs_path(path)
476476

477477
if fs_path.absolute_path.exists():
478-
report = ExtractionProblem(
479-
path=str(fs_path.relative_path),
480-
problem=f"Attempting to create a file that already exists through {path_use_description}",
481-
resolution="Overwrite.",
482-
)
483-
fs_path.absolute_path.unlink()
484-
self.record_problem(report)
478+
if fs_path.absolute_path.is_file():
479+
report = ExtractionProblem(
480+
path=str(fs_path.relative_path),
481+
problem=f"Overwriting already existing file through {path_use_description}",
482+
resolution="Overwriting.",
483+
)
484+
self.record_problem(report)
485+
fs_path.absolute_path.unlink()
486+
487+
elif fs_path.absolute_path.is_dir():
488+
report = ExtractionProblem(
489+
path=str(fs_path.relative_path),
490+
problem=f"Attempting to create a directory that already exists through {path_use_description}",
491+
resolution="Ignore",
492+
)
493+
self.record_problem(report)
485494

486495
if not fs_path.is_safe:
487496
report = PathTraversalProblem(
@@ -550,7 +559,7 @@ def mknod(self, path: Path, mode=0o600, device=0):
550559
def _get_checked_link(self, src: Path, dst: Path) -> Optional[_FSLink]:
551560
link = _FSLink(root=self.root, src=src, dst=dst)
552561

553-
if link.src.absolute_path.exists():
562+
if link.dst.absolute_path.exists():
554563
self.record_problem(link.format_report("File already exists."))
555564
return None
556565
if not link.is_safe:

0 commit comments

Comments
 (0)