Skip to content

Remove empty Drive folders through fsspec rmdir - #414

Open
shcheklein wants to merge 3 commits into
fix/fs-create-directoriesfrom
fix/fs-remove-empty-directory
Open

shcheklein wants to merge 3 commits into
fix/fs-create-directoriesfrom
fix/fs-remove-empty-directory

Conversation

@shcheklein

Copy link
Copy Markdown
Member

PyDrive2 inherited fsspec's empty default implementation of rmdir, so the call returned successfully without sending a request to Drive. DVC's filesystem wrapper delegates directly to this API, including during directory cleanup. This PR builds on #409 alongside #411.

rmdir resolves the current target, refuses ambiguous names and protected roots, and checks the child listing before using the configured trash or permanent-delete operation. Nonempty folders raise OSError(ENOTEMPTY). Permanent removal includes trashed children in the check. Empty intermediate pages, incomplete searches and retries are covered. As Drive uses separate list and mutation requests, applications should coordinate concurrent writers.

Offline request tests verify the target ID, child filters, failure paths and absence of mutations on refusal. A live Drive test checks that a nonempty parent survives and a fresh reader sees the empty child disappear.

@shcheklein

Copy link
Copy Markdown
Member Author

The reason rmdir("root/empty") appeared to succeed is that fsspec's inherited implementation just returns. It sends no request. DVC's generic filesystem forwards rmdir to PyDrive2 and dvc-data's checkout cleanup invokes it. This PR gives that call empty-directory semantics and respects the remote's trash policy.

I ran dvc-data's checkout directory-cleanup path with the real dvc-gdrive adapter and a simulated Drive. It removed an empty directory and preserved a populated sibling in both trash and permanent-delete modes, at fsspec 2024.2.0 and 2026.9.0. DVC object garbage collection has a separate per-object remove path. Drive's child listing and folder mutation are separate requests, so concurrent writers must be coordinated for this operation.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

The resolver does not check search completeness, so an omitted same-named sibling can cause rmdir to delete a folder at an ambiguous path; completeness validation and a no-mutation regression test are needed.

1 open issue
What changed in this PR

This PR replaces fsspec’s inherited no-op rmdir with Drive folder removal, supporting directory cleanup by callers such as DVC.

Changes:

  • Resolves removal targets afresh and checks folder emptiness before trashing or permanently deleting.
  • Adds offline request tests and a live Drive test for removal and refusal paths.
  • Documents trash behavior and the need to coordinate concurrent writers.
File Description
pydrive2/​test/​test_fs.py Adds live coverage for empty-folder removal and nonempty-parent preservation.
pydrive2/​test/​test_fs_unit.py Tests resolution, protected roots, pagination, incomplete child listings, and retries.
pydrive2/​fs/​spec.py Implements rmdir and retry-aware emptiness checks.
docs/​fsspec.rst Documents removal semantics and concurrency limitations.

Review effort: Balanced

Comment thread pydrive2/fs/spec.py
if base:
# Name duplicates and external renames must be checked before
# deleting. A cached folder ID alone cannot establish uniqueness.
match = self._resolve_path(base, _DirectoryCache())

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Needs a closer look

The name resolver ignores incompleteSearch, so a partial lookup can falsely establish folder uniqueness and allow rmdir to delete an ambiguously named folder.

1 open issue

Review effort: Balanced

This branch has not been deployed

No deployments
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