Skip to content
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
21 changes: 20 additions & 1 deletion src/gdrive_fsspec/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,15 @@ def ls(self, path, detail=False, trashed=False):
files = self._list_directory_by_id(
file_id, trashed=trashed, path_prefix=pref
)
if files:
if (
path == ""
and not files
and self.drive is None
and file_id != "root"
and not self._file_id_exists(file_id)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This gets rechecked on every call. That is ... OK, the cache doesn't have a way to say "tried but not found".

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Right — agreed. The recheck only runs in the narrow case (empty listing of a custom root), and the dircache indeed has no way to record a negative, so it's the cheapest correct option.

):
raise FileNotFoundError(path)
if files or path == "":
self.dircache[pref] = files
else:
raise FileNotFoundError(path)
Expand Down Expand Up @@ -316,6 +324,17 @@ def info(self, path, trashed=False):
}
return super().info(path, trashed=trashed)

def _file_id_exists(self, file_id):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Basically the same as self._list_directory_by_id ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not quite — _list_directory_by_id lists the children of a directory (files.list with a parents filter) and returns [] for a missing id, which is exactly the ambiguity this disambiguates. _file_id_exists does a metadata-only files.get (fields=id) to ask whether the id itself resolves. A missing custom root then raises FileNotFoundError while an empty real root still lists [].

try:
self.files.get(
fileId=file_id, supportsAllDrives=True, fields="id"
).execute()
except HttpError as e:
if e.resp.status == 404:
return False
raise
return True

def _drive_kw(self):
if self.drive is not None:
return dict(
Expand Down
33 changes: 33 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,39 @@ def test_invalidate_cache_all(anon_fs):
assert anon_fs.dircache == {}


def test_ls_empty_root(anon_fs):
anon_fs._list_directory_by_id = mock.Mock(return_value=[])

assert anon_fs.ls("") == []
assert anon_fs.dircache[""] == []


def test_ls_empty_custom_root(anon_fs):
anon_fs.root_file_id = "empty-root"
anon_fs._list_directory_by_id = mock.Mock(return_value=[])
anon_fs._file_id_exists = mock.Mock(return_value=True)

assert anon_fs.ls("") == []
assert anon_fs.dircache[""] == []


def test_ls_missing_custom_root(anon_fs):
anon_fs.root_file_id = "missing-root"
anon_fs._list_directory_by_id = mock.Mock(return_value=[])
anon_fs._file_id_exists = mock.Mock(return_value=False)

with pytest.raises(FileNotFoundError):
anon_fs.ls("")
assert "" not in anon_fs.dircache


def test_ls_missing_path_on_empty_root(anon_fs):
anon_fs._list_directory_by_id = mock.Mock(return_value=[])

with pytest.raises(FileNotFoundError):
anon_fs.ls("missing")


def test_drive_id_from_name_single_match(anon_fs):
anon_fs.drives = [{"id": "1", "name": "foo"}, {"id": "2", "name": "bar"}]
assert anon_fs._drive_id_from_name("foo") == "1"
Expand Down