Skip to content
Open
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
11 changes: 8 additions & 3 deletions fsspec/implementations/tar.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def _index(self):
if ti.islnk() or ti.issym():
# extractfile follows links, so report the size of the target
info["size"] = self._link_target_size(ti)
out[name] = (info, ti.offset_data, orig_name)
out[name] = (info, ti, orig_name)

self.index = out
# TODO: save index to self.index_store here, if set
Expand Down Expand Up @@ -197,11 +197,16 @@ def _open(self, path, mode="rb", **kwargs):
member = self.index.get(path)
if member is None or member[0]["type"] == "directory":
raise self._not_a_file(path)
details, _, orig_name = member
details, tarinfo, orig_name = member
if details["type"] != "file":
raise ValueError("Can only handle regular files")
try:
out = self.tar.extractfile(orig_name)
# A name lookup scans tarfile's members in reverse. Reuse the
# indexed member when it has the exact name tarfile would look up.
# Keep the name lookup for unusual non-directory names ending in "/".
out = self.tar.extractfile(
tarinfo if tarinfo.name == orig_name else orig_name
)
except KeyError as exc:
# tarfile reports a missing link target as KeyError.
raise FileNotFoundError(path) from exc
Expand Down
24 changes: 23 additions & 1 deletion fsspec/implementations/tests/test_tar.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,12 @@ def test_dot_normalization_preserves_parent_components(tmp_path):


@pytest.mark.parametrize(
"names", [["./file.txt", "file.txt"], ["file.txt", "./file.txt"]]
"names",
[
["./file.txt", "file.txt"],
["file.txt", "./file.txt"],
["file.txt", "file.txt"],
],
)
def test_dot_normalization_duplicate_members(tmp_path, names):
path = tmp_path / "duplicates.tar"
Expand All @@ -586,6 +591,23 @@ def test_dot_normalization_duplicate_members(tmp_path, names):
assert fs.cat("./file.txt") == b"last"


def test_regular_member_with_trailing_slash_keeps_tarfile_name_lookup(tmp_path):
path = tmp_path / "trailing-slash.tar"
with tarfile.open(path, "w") as tar:
for name, data in [("file", b"one"), ("file/", b"two"), ("missing/", b"xxx")]:
info = tarfile.TarInfo(name)
info.size = len(data)
tar.addfile(info, BytesIO(data))

fs = TarFileSystem(str(path))
try:
assert fs.cat_file("file") == b"one"
with pytest.raises(FileNotFoundError, match="missing"):
fs.cat_file("missing")
finally:
fs.close()


def test_dot_normalization_preserves_links(tmp_path):
path = tmp_path / "dot-links.tar"
with tarfile.open(path, "w") as tar:
Expand Down
Loading