Skip to content

Fix nested subdirectory listing #1430 #1433

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

Merged
merged 2 commits into from
Nov 20, 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
20 changes: 14 additions & 6 deletions fsspec/implementations/reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -985,16 +985,24 @@ def _dircache_from_items(self):
elif len(part) == 1:
size = None
else:
_, start, size = part
_, _, size = part
par = path.rsplit("/", 1)[0] if "/" in path else ""
par0 = par
subdirs = [par0]
while par0 and par0 not in self.dircache:
# build parent directories
self.dircache[par0] = []
self.dircache.setdefault(
par0.rsplit("/", 1)[0] if "/" in par0 else "", []
).append({"name": par0, "type": "directory", "size": 0})
# collect parent directories
par0 = self._parent(par0)
subdirs.append(par0)

subdirs = subdirs[::-1]
for parent, child in zip(subdirs, subdirs[1:]):
# register newly discovered directories
assert child not in self.dircache
assert parent in self.dircache
self.dircache[parent].append(
{"name": child, "type": "directory", "size": 0}
)
self.dircache[child] = []

self.dircache[par].append({"name": path, "type": "file", "size": size})

Expand Down
11 changes: 10 additions & 1 deletion fsspec/implementations/tests/test_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@


def test_simple(server): # noqa: F811

refs = {
"a": b"data",
"b": (realfile, 0, 5),
Expand Down Expand Up @@ -53,6 +52,16 @@ def test_ls(server): # noqa: F811
}


def test_nested_dirs_ls():
# issue #1430
refs = {"a": "A", "B/C/b": "B", "B/C/d": "d", "B/_": "_"}
fs = fsspec.filesystem("reference", fo=refs)
assert len(fs.ls("")) == 2
assert set(e["name"] for e in fs.ls("")) == set(["a", "B"])
assert len(fs.ls("B")) == 2
assert set(e["name"] for e in fs.ls("B")) == set(["B/C", "B/_"])


def test_info(server): # noqa: F811
refs = {
"a": b"data",
Expand Down