Skip to content
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

Avoid copying files with same name prefix when copying folder #576

Merged
merged 4 commits into from
Aug 31, 2023

Conversation

john-jam
Copy link
Contributor

@john-jam john-jam commented Aug 24, 2023

As described here, when copying a folder, the files with the same name prefix are also copied.

This PR fixes this behavior. I guess there's probably a better way to fix this though.

TODO:

  • Merge main once this PR is merged so the derived tests are executed in this repo.
  • Rerun the CI once this PR is merged to highlight this fix.


ind = min(indstar, indques, indbrace)
prefix = path[:ind].split("/")[-1]
return await super()._glob(path, prefix=prefix, **kwargs)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we apply the fix in the _find method below then this glob prefix construction no longer work for the edge case highlighted here.

This is because the the glob subdir* will create a subdir prefix and will be handled like a folder in the _find method (and add a trailing slash and miss the subdir.txt file).

Also, not sure to understand the initial benefit of this _glob method override. Was this just for caching?
Anyway, if another fix exists maybe this could be left as-is.

assert all(d.startswith(TEST_BUCKET + "/nested") for d in gcs.dircache)
# gcs.dircache.clear()
# gcs.glob(TEST_BUCKET + "/nested**1")
# assert all(d.startswith(TEST_BUCKET + "/nested") for d in gcs.dircache)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without the _glob method override, _find is now called for the parent folder (TEST_BUCKET) so other folder like test are now in the dircache.

@martindurant
Copy link
Member

@ianthomas23 , if you have time

gcsfs/core.py Outdated
@@ -1410,6 +1398,9 @@ async def _find(
else:
_prefix = key

if _prefix != "" and await self._isdir(f"{bucket}/{_prefix}"):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there no way to avoid isdir here? This doubles the number of calls for a simple list - and isdir surely is doing a list op anyway!

Copy link
Contributor Author

@john-jam john-jam Aug 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, it's not useful to double the calls there. That's why I initially indicated that a better solution should probably exist ;)

I updated the code with this commit to try out another solution inspired by the _find method in s3fs which first fetches the path as if it is a directory then calls the _info method on the path to check if it's a file and returns it accordingly. In gcsfs, we can't call _info because some tests are expecting versioned files (_info does not support versioned files) or a prefixed search. So I simply moved the initial implementation into a block executed if the first search (when we treat path as a directory) does not return anything. It still doubles the calls when we are searching a single file though.

Let me know if that works for you. This commit is tested against this PR to fix this test.

@martindurant martindurant merged commit 5b97087 into fsspec:main Aug 31, 2023
5 checks passed
@martindurant
Copy link
Member

Will be releasing tomorrow :)

@john-jam john-jam deleted the fix-new-derived-test branch August 31, 2023 15:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants