Skip to content

Replace the content of an existing file on a write - #455

Draft
shcheklein wants to merge 3 commits into
mainfrom
write-replaces-existing-file
Draft

shcheklein wants to merge 3 commits into
mainfrom
write-replaces-existing-file

Conversation

@shcheklein

@shcheklein shcheklein commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

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

fs.pipe_file("root/config.json", b'{"version": 1}')
fs.pipe_file("root/config.json", b'{"version": 2}')

fs.ls("root")                    # before: ['root/config.json', 'root/config.json']
fs.cat_file("root/config.json")  # before: either version

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") and touch() replace the content of the file at the path.
  • When several files already share the path, the write replaces the one that reads return, also when the same-name files sit in two folders that share the parent path. Reading the path afterwards returns what was written.
  • The file keeps its type: a .json file stays application/json.
  • Writing to the path of a folder raises IsADirectoryError. It used to create a file next to the folder with the same name.
  • Writing to the path of a Google Docs file or a shortcut raises PermissionError before any bytes are sent.
  • Threads that write one path through the same filesystem take turns, so they leave one file. A write to a name that mkdir is creating waits for it and then raises IsADirectoryError. Other filesystem instances and other clients do not share this lock.
  • If another client deletes the file between the lookup and the replace, the write raises 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 put of 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_file onto 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.
  • mv onto an existing file still raises NotADirectoryError.

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 MiB put_file (a resumable upload) each leave the same file with the new content and its application/json type, as the only file in the folder;
  • touch() and pipe_file(path, b"") empty the file;
  • with two files of the name, the one with the lower ID gets the new content, reads return it, and the other file keeps its own;
  • with two same-name parent folders and the file in the one with the higher ID, the write replaces that file and adds nothing to the other folder;
  • a replace whose answer is lost is repeated and leaves one file with the new content;
  • a write to a folder path raises IsADirectoryError, and no file appears;
  • a write to the path of a Google Doc or of a shortcut raises 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 mkdir is creating waits and raises IsADirectoryError. 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

shcheklein and others added 2 commits December 2, 2026 19:29
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>
Comment thread pydrive2/fs/spec.py Resolved
Comment thread pydrive2/fs/spec.py Resolved
Comment thread pydrive2/test/test_fs.py Resolved

@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.

[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.

Comment thread pydrive2/fs/spec.py Resolved
Comment thread pydrive2/fs/spec.py Resolved
Comment thread pydrive2/test/test_fs.py Resolved
… 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>

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.

🟢 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

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