Repository navigation
Replace the content of an existing file on a write - #455
Draft
shcheklein wants to merge 3 commits into
Draft
shcheklein wants to merge 3 commits into
shcheklein wants to merge 3 commits into
Conversation
A write to the path of an existing file added a second file with the same name, and reads then picked one of the two by ID. pipe_file(), put_file() and open(path, "wb") now replace the content of that file, which keeps its ID and sharing. With several files of the name, the one that reads return is replaced. A write to the path of a folder raises IsADirectoryError. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
shcheklein
commented
Dec 3, 2026
shcheklein
commented
Dec 3, 2026
shcheklein
left a comment
Member
Author
There was a problem hiding this comment.
[AI generated — Copilot]
5-pass review summary: important semantic fix for overwrite behavior, with real issues to address around MIME type clobbering, 2x request cost on new folders, in-process concurrent upload races, concurrent-delete 404s, and missing retry/truncation tests.
This was referenced Dec 3, 2026
… time A write looks for an existing file under all folders that share the parent path, as reads do. Threads of one filesystem that write the same path take turns, so they no longer create two files. A replace keeps the file's type, and a write onto a Google Docs file or a shortcut raises PermissionError before sending anything. A file deleted between the lookup and the replace raises FileNotFoundError. The live tests work in their own folder and cover an empty write, a lost answer to a replace, and two same-name parent folders. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues remain for the filesystem changes that replace existing content while preserving Drive file identity and sharing.
No open issues 🎉
What changed in this PR
Updates filesystem writes to replace existing file content instead of creating duplicates, preserving the Drive file’s ID, type, and sharing.
Changes:
- Matches write targets to read resolution and rejects folders, Google Workspace files, and shortcuts.
- Serializes same-path writes within one filesystem instance and retries content replacements after connection failures.
- Adds overwrite and concurrency tests and documents the changed behavior.
| File | Description |
|---|---|
pydrive2/test/test_fs.py |
Adds live coverage for replacement, identity preservation, retries, and rejected targets. |
pydrive2/test/test_fs_unit.py |
Tests concurrent writes and updates expected lookup requests. |
pydrive2/fs/spec.py |
Implements target lookup, validation, locking, and content replacement. |
docs/upgrading.rst |
Explains changed write behavior and errors. |
docs/fsspec.rst |
Documents replacement semantics and concurrency limits. |
Review effort: Balanced
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A filesystem write to the path of an existing file added a second file with the same name. Now it replaces the content of the file that is there.
Example
Drive allows two files with the same name. Reads of that path pick the file with the lowest ID, which on real Drive is effectively random. So a program that rewrote a file could keep reading the old content.
After: one
config.json, holding version 2. It is the same Drive file as before, so its ID, sharing and links do not change.The rules
pipe_file(),put_file(),open(path, "wb")andtouch()replace the content of the file at the path..jsonfile staysapplication/json.IsADirectoryError. It used to create a file next to the folder with the same name.PermissionErrorbefore any bytes are sent.mkdiris creating waits for it and then raisesIsADirectoryError. Other filesystem instances and other clients do not share this lock.FileNotFoundError.The fsspec guide and the upgrade guide describe this.
Cost
To find the file, a write first looks the name up in its folder: one more request per write. A recursive
putof 1,000 files into a new folder therefore sends 1,000 more lookups, although that folder is known to be empty. That can be optimised later: skip the lookup in folders this filesystem has just created.A repeated replace is safe: sending the same content to the same file again changes nothing. So a replace that loses its connection is repeated like the reads (#435).
Not changed
cp_fileonto an existing file still adds a second file. Drive cannot copy onto an existing file, so replacing would mean copying and then removing the old file (the file gets a new ID), or downloading and uploading the content.mvonto an existing file still raisesNotADirectoryError.Both are decisions for a separate change.
Validation
Live, on real Drive, each test in its own folder. The file is created with the core API, so its ID is known; then the filesystem writes to its path:
pipe_file,open("wb")and a 2 MiBput_file(a resumable upload) each leave the same file with the new content and itsapplication/jsontype, as the only file in the folder;touch()andpipe_file(path, b"")empty the file;IsADirectoryError, and no file appears;PermissionError.Offline, two threads race on one filesystem: a write to a new path waits for another thread's create and then replaces that file, and a write to a name that
mkdiris creating waits and raisesIsADirectoryError. Both tests fail without the lock.The live request count of
touch()on a new nested path goes from 5 to 6 listings: the file name lookup. Eight offline tests get that lookup as one more canned answer. The offline case of writing a file next to a folder of the same name is removed, because that now raises.The offline suite passes with the current google-auth and with 2.15.0 (536 passed).
🤖 Generated with Claude Code