Skip to content

Commit

Permalink
Ensure dircache directories contain their subdirectories
Browse files Browse the repository at this point in the history
  • Loading branch information
ianthomas23 committed Sep 19, 2023
1 parent d6e3938 commit 17f035e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
7 changes: 7 additions & 0 deletions s3fs/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,13 @@ async def _find(self, path, maxdepth=None, withdirs=None, detail=False, prefix="
thisdircache[ppar].append(d)
if par in sdirs:
thisdircache[par].append(o)

# Explicitly add directories to their parents in the dircache
for d in dirs:
par = self._parent(d["name"])
if par in thisdircache:
thisdircache[par].append(d)

if not prefix:
for k, v in thisdircache.items():
if k not in self.dircache and len(k) >= len(path):
Expand Down
25 changes: 25 additions & 0 deletions s3fs/tests/test_s3fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2687,3 +2687,28 @@ def test_rm_invalidates_cache(s3):
s3.rm_file(fn)
assert not s3.exists(fn)
assert fn not in s3.ls(test_bucket_name)


def test_cache_handles_find_with_maxdepth(s3):
# Issue 773: invalidate_cache should not be needed when find is called with different maxdepth
base_name = test_bucket_name + "/main"
dir = base_name + "/dir1/fileB"
file = base_name + "/fileA"
s3.touch(dir)
s3.touch(file)

# Find with maxdepth=None
f = s3.find(base_name, maxdepth=None, withdirs=False)
assert base_name + "/fileA" in f
assert base_name + "/dir1" not in f
assert base_name + "/dir1/fileB" in f

# Find with maxdepth=1.
# Performed twice with cache invalidated between them which should give same result
for _ in range(2):
f = s3.find(base_name, maxdepth=1, withdirs=True)
assert base_name + "/fileA" in f
assert base_name + "/dir1" in f
assert base_name + "/dir1/fileB" not in f

s3.invalidate_cache()

0 comments on commit 17f035e

Please sign in to comment.