Skip to content

Recursive get and copy use the IDs from their listing, without a lookup per file - #468

Open
shcheklein wants to merge 9 commits into
mainfrom
get-by-listed-id
Open

shcheklein wants to merge 9 commits into
mainfrom
get-by-listed-id

Conversation

@shcheklein

@shcheklein shcheklein commented Dec 5, 2026 •

Copy link
Copy Markdown
Member

A recursive get or copy lists the tree, which already returns every file's Drive ID. It then looked each file up by name again before downloading or copying it:

fs.get("root/data", "local/", recursive=True)

For a folder with 1,000 files and a subfolder with one more:

Before After
fs.get("root/data", "local/", recursive=True) 2,007 requests (3 listings, 1,003 lookups, 1,001 downloads) 1,006 (the listing find sends, plus one download per file)
fs.copy("root/data", "root/copy", recursive=True) 2,023 requests (1,006 lookups) 1,022 (5 lookups: the paths and the destination)
fs.get("root/data/*.csv", "local/") with 1,000 matches 4,002 requests (a type check of 2 requests and a lookup per match) 1,002 (the listing, plus one download per match)

The glob row follows from an offline count with three matches: 14 requests before, 5 after.

rclone does the same: a listed object is opened by its listed ID (files/<id>?alt=media), with no lookup per file.

How

get and copy run fsspec's own code against a view of the filesystem made for that one call (_ListedTransfer). The view's find records the ID and type of each listed path. Its isdir, get_file and cp_file answer from that record, so fsspec's type filter for globs and maxdepth and each transfer need no lookup. Same-name files keep the lowest-ID rule, so the result is the file a lookup of that path would pick.

Some cases fall back to lookups:

  • Names that contain a slash, and everything below a folder with such a name, are not recorded. A lookup splits paths at slashes and can't reach them, so they behave as before. Without this, a file named ../escaped.txt would be found by its listed ID, and with an fsspec older than its destination check, a recursive get would write it above the target.
  • An item with more than one parent folder, which older Drive files can have, turns the record off for the call. A listing of several folders places such an item under only one of them.
  • The copy destination is checked with a lookup, because fsspec decides where the tree goes by its current type.
  • A subclass that overrides expand_path, glob, find, isdir, get_file or cp_file gets fsspec's plain transfer.

The folder a call starts from is looked up as before; the folder cache that find just filled answers that lookup, so it costs no request. Callbacks get the filesystem itself, so a find, get_file or nested get they call looks paths up again. The ID hint reaches private helpers only, after the path is validated, so the public methods keep their signatures. get and copy use functools.wraps, so inspect.signature shows fsspec's options.

Nothing is kept after the call returns, and other calls on the same filesystem never see the record. It takes about 160 bytes per file while the call runs: 151 to 159 bytes measured on Python 3.11, 3.13 and 3.15 with 5,000 paths and 33-character IDs. The record shares its path strings with the listing result that fsspec holds anyway.

Behaviour change

Inside one call, a file is read by the ID it had when it was listed:

  • another client trashes a listed file, or puts a new file at its path, during the call: the call still reads the listed file, because Drive serves a trashed file by its ID. Before, the lookup found the new file, or none;
  • a listed file is deleted for good: FileNotFoundError, as before. A recursive copy skips it, as before. When a copy request gets 404, the source is checked by ID (one request, only then). Only a missing source becomes FileNotFoundError; a 404 because the destination folder disappeared still raises Drive's error, as on main.

docs/fsspec.rst describes this under "Directory cache".

Not covered

These still look each file up by name:

  • a manual find() followed by get_file() calls;
  • DVC, which downloads one object at a time with get_file (dvc-objects generic._get) and never calls a recursive get;
  • rm, uploads, and get/copy called with lists of paths, which skip fsspec's listing.

Tests

Live, on real Drive. A shared tree holds a file, a subfolder with a file, and two files of one name created through the core API.

  • Recursive get: sends exactly what find sends for the same tree, plus one download per file. The same-name file is the one cat_file returns.
  • get of a glob: sends exactly what glob sends, plus one download per match.
  • Recursive copy: never searches Drive for a source file's name and sends one copy per file.
  • A file deleted by another client between the listing and its download makes get raise FileNotFoundError.
  • Names with a slash: a recursive copy of a tree holding ../escaped.txt and a folder x/y with a file inside copies only what a lookup finds, as on main.

Offline, for what real Drive can't be made to send on demand:

  • a name with a slash is looked up;
  • a file with two parents makes the call look every file up, so both copies download what a lookup finds;
  • a callback that lists the folder again and downloads a replaced file gets the new file, while the transfer keeps the listed one;
  • a later get_file looks the path up again;
  • a copy destination that became a folder after the listing gets the tree inside it;
  • a subclass whose glob keeps only CSV files downloads only those;
  • a copy request answered with 404 raises FileNotFoundError from cp_file when the source is gone, and Drive's error when the source still exists;
  • a recursive copy skips a deleted source and copies the rest.

On main's code, the get count test (10 requests where 7 are expected) and the copy test (searches for 'same.txt') fail. That was checked on the fake Drive harness, and the result of the same run on real Drive is in the first comment. Two offline tests that counted the old lookup now count the new requests.

Conflicts with open PRs

Textual conflicts are likely in pydrive2/fs/spec.py with #453, #455 and #456, all of which edit find, _lookup_item, cp_file or nearby code. They're also likely at the end of the get tests in pydrive2/test/test_fs.py. #456 changes how get_file downloads but not how it finds the ID, so the two compose: a recursive get becomes one streamed download per file. answered_requests is one more copy of the HTTP-observing fixture that #464 plans to merge into one.

🤖 Generated with Claude Code

shcheklein and others added 2 commits December 4, 2026 18:46
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A recursive get or copy listed the tree and then looked every file up by
name again: 2,007 requests for 1,000 files where 1,006 do. The IDs the
listing returns are kept on the calling thread until the call returns.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@shcheklein shcheklein changed the title Download and copy listed files by their listed IDs in recursive get and copy Recursive get and copy use the IDs from their listing, without a lookup per file Dec 5, 2026
@shcheklein

Copy link
Copy Markdown
Member Author

AI generated · Claude · The new live tests on main's code, on real Drive: the test commit alone (417ae10) on a temporary branch, run by hand. Ubuntu 3.11 job. The run was cancelled after this job, and the branch deleted.

FAILED test_recursive_get_after_making_a_tree_sends_only_its_listing_and_downloads - AssertionError: assert 10 == (4 + 3)
FAILED test_recursive_copy_after_making_a_tree_searches_for_no_file_name - assert ["('1PROyPJSg...e='same.txt'"] == []
========== 2 failed, 101 passed, 551 deselected in 465.46s (0:07:45) ===========

On main, the recursive get sent 3 lookups more than its listing and downloads, one per file, and the recursive copy searched Drive for the source file names. The other two new tests pass on main: a later get_file looks the path up, and a file deleted mid-call raises FileNotFoundError. They pin behaviour this PR must keep.

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

Transfer ID caching can select a folder instead of the intended same-name file, while nested transfers reuse and contaminate the outer ID map, risking stale reads or failures.

2 open issues
What changed in this PR

This PR reduces Drive requests by reusing listing IDs during recursive and glob-based filesystem transfers.

Changes:

  • Reuses thread-local IDs during get and copy.
  • Translates copy-request 404 errors to FileNotFoundError.
  • Adds transfer tests and documents concurrent-change behavior.
File Description
pydrive2/​test/​test_fs.py Adds live request-count, duplicate-name, and deletion tests.
pydrive2/​test/​test_fs_unit.py Updates expected transfer request counts.
pydrive2/​fs/​spec.py Reuses listed IDs and translates copy errors.
docs/​fsspec.rst Documents ID lifetime and deletion behavior.

Review effort: Balanced

Comment thread pydrive2/fs/spec.py Resolved
Comment thread pydrive2/fs/spec.py Resolved
The start of a recursive call came from a folders-only lookup, so an older
file of the same name could lose to the folder; its lookup is served from
the folder cache anyway. A get or copy started from a callback now lists
with its own IDs instead of the outer call's.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

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

Unsafe remote names can cause recursive downloads to overwrite files outside the destination, and copy-request 404 handling lacks regression coverage.

2 open issues
2 resolved since last review

Review effort: Balanced

Comment thread pydrive2/fs/spec.py Resolved
Comment thread pydrive2/fs/spec.py Resolved
A lookup splits paths at slashes, so it cannot reach a Drive name such as
"../escaped.txt" or anything below a folder named "x/y". Remembering those
let a recursive get or copy reach them by ID, and an older fsspec then
wrote "../escaped.txt" above the target. They now go through the lookup
as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

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

Human assessment is needed because copy-error handling can misclassify destination failures as missing sources, silently skipping existing files during recursive copies.

No open issues 🎉

2 resolved since last review

Review effort: Balanced

A 404 on the copy request also comes when the destination folder was
removed. Main raised Drive's error then, and a recursive copy would now
have skipped the file. On a 404 the source is checked by ID, and only a
missing source becomes FileNotFoundError.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@shcheklein

Copy link
Copy Markdown
Member Author

AI generated · Claude · On the third review's summary, "copy-error handling can misclassify destination failures as missing sources": agreed, and fixed in the latest commit. A 404 on the copy request also comes when the destination folder was removed. Main raised Drive's error in that case, while b51b634 turned it into FileNotFoundError, so a recursive copy would have skipped the file. Now cp_file checks the source by ID after a 404 (one request, only on that path). Only a missing source becomes FileNotFoundError; otherwise Drive's error is raised. New offline test test_cp_file_after_the_destination_folder_is_deleted_raises_drive_error, which fails with the blanket translation. The two deleted-source tests now also expect the check request.

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

Human assessment is needed because an unpublished finding identifies documentation that could mislead callers about detecting incomplete recursive copies when deleted sources are skipped by default.

No open issues 🎉

Review effort: Balanced

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@shcheklein

Copy link
Copy Markdown
Member Author

AI generated · Claude · On the fourth review's summary, about the documentation and skipped deleted sources: agreed. The guide said a file deleted mid-call raises FileNotFoundError, which holds for get() only. b662738 says that a recursive copy() skips it, because fsspec's copy ignores missing files when recursive=True unless on_error="raise" is passed. That's how main behaves when the lookup misses.

@shcheklein shcheklein self-assigned this Dec 5, 2026
@shcheklein
shcheklein marked this pull request as ready for review December 5, 2026 23:24

@shcheklein shcheklein left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Codex review of the listed-ID transfer change. I verified the behavior in the inline findings with fsspec 2024.2.0 and 2026.9.0.

Comment thread pydrive2/fs/spec.py Resolved
Comment thread pydrive2/fs/spec.py Resolved
Comment thread pydrive2/fs/spec.py Resolved
Comment thread pydrive2/fs/spec.py Resolved
Comment thread pydrive2/test/test_fs.py Resolved
get() and copy() run fsspec's code against a view that records what its
own listings return. The view answers the isdir() filter, get_file() and
cp_file() from that record, so a glob sends no lookup per match. Callbacks
reach the filesystem itself and look paths up again. A listed item with
several parents turns the record off, because a listing of several
folders places it under only one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@shcheklein shcheklein left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Codex re-review of 9e85e42. The updated implementation addresses the five earlier comments. I reproduced the four findings below with fsspec 2024.2.0 and 2026.9.0. The filesystem unit suite passed on both versions.

Comment thread pydrive2/fs/spec.py Resolved
Comment thread pydrive2/fs/spec.py Resolved
Comment thread pydrive2/fs/spec.py Resolved
Comment thread pydrive2/fs/spec.py Resolved
shcheklein and others added 2 commits December 6, 2026 10:02
A subclass that overrides a hook the transfer replaces keeps fsspec's
own transfer. The copy destination is checked as before, the ID hint
goes through private helpers after the path is validated, and the
listing and the record share each path string.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

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