Skip to content

Commit

Permalink
Fix nested subdirectory listing #1430 (#1433)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirill888 authored Nov 20, 2023
1 parent 23b275e commit d1d2268
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
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

0 comments on commit d1d2268

Please sign in to comment.