Repository navigation
Replace an existing file with cp_file and mv, as rclone does - #470
Draft
shcheklein wants to merge 4 commits into
Draft
shcheklein wants to merge 4 commits into
shcheklein wants to merge 4 commits into
Conversation
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
cp_file onto a file the filesystem knows copies, then trashes the old file (deleted with trash_only=False), after looking the path up once more. mv already looks the target up, so it moves the source onto any file there and removes the old file after the move. Copying or moving a file onto itself changes nothing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
🟡 Changes recommended
Critical cache inconsistencies after copy or move operations, including cleanup failures, can cause subsequent writes to update the wrong file or overwrite moved content.
3 open issues
What changed in this PR
Extends the filesystem’s known-file replacement behavior to copying and moving files on Drive.
Changes:
- Makes
cp_file()replace known targets andmv()replace existing file targets. - Removes old targets after successful operations, honoring
trash_only. - Adds replacement tests and updates user documentation.
| File | Description |
|---|---|
| pydrive2/test/test_fs.py | Adds live replacement and request-count tests. |
| pydrive2/test/test_fs_unit.py | Tests replacement, self-operations, and subsequent writes. |
| pydrive2/fs/spec.py | Implements copy and move replacement behavior. |
| docs/upgrading.rst | Documents changed overwrite behavior. |
| docs/fsspec.rst | Explains replacement rules and file identity. |
Review effort: Balanced
The copied or moved file is remembered even when trashing the old file fails, the moved file is forgotten at its old path, and a file left at the path with a lower ID than the copy stays the one writes replace. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
🟡 Changes recommended
The cache-dependent self-copy guard can create duplicates for files with multiple or unresolved parents, requiring a cache-independent path comparison and regression test.
1 open issue
3 resolved since last review
Review effort: Balanced
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.


Stacked on #455 (base branch
write-replaces-existing-file).cp_fileneeds #455's known-file cache to find a target without a lookup on every copy. Merge #455 first; then this PR needs a rebase onto main that drops #455's commits.Before and after
With
trash_only=Falsethe old file is deleted instead.What it does, as rclone does
Drive can't copy or move onto an existing file, so the old file is removed afterwards. rclone's Drive backend does the same (drive.go
Copycopies, then removes the existing object;operations.Movedeletes the existing destination, then moves).cp_filereplaces a file the filesystem knows at the path, by the same rule as writes in Replace a known file on a write, without a lookup per write #455: files it wrote, copied or found by path (exists(),info(), a read). Before removing anything it looks the path up once more, so only a file that's still there is removed. For any other path it copies without asking Drive, as today, so a file it doesn't know can still end up duplicated. The copy is a new file with its own ID, so the old file's sharing and links don't carry over.mvalready looked the target up (exists()), so it replaces any file there. Unlike rclone, it moves first and removes the old file afterwards, so a failed move leaves the target untouched. The moved file keeps its ID.mvmoves the source into it, andcp_filebehaves as before;NotADirectoryError;Requests (counted in the live tests)
cp_file, target knowncp_file, target unknownmv, same foldermv, another folderTests
Four live tests on real Drive. All four fail on #455's code (run, test commit only, branch since deleted):
cp_fileafter a write to the target: 4 requests; one target with the source's bytes; old file trashed.cp_fileafterexists()found the target,trash_only=False: the old file is deleted.mvonto a file another client made, in another folder: the moved file keeps its ID; the source path is gone; old file trashed.mvonto a file in the same folder,trash_only=False: 4 requests; old file deleted.The offline test that expected
NotADirectoryErrorformvonto a file now covers the replacement, plus a folder source (still raises) and a file onto itself. #455's offline cp-then-write test now writes to the copy.Docs:
fsspec.rst(Writing files) and the upgrade guide.Overlaps
#453 (rm) and #456 also edit
pydrive2/fs/spec.py.🤖 Generated with Claude Code